test.c 1.6 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924349253492634927349283492934930349313493234933349343493534936349373493834939349403494134942349433494434945349463494734948349493495034951349523495334954349553495634957349583495934960349613496234963349643496534966349673496834969349703497134972349733497434975349763497734978349793498034981349823498334984349853498634987349883498934990349913499234993349943499534996349973499834999350003500135002350033500435005350063500735008350093501035011350123501335014350153501635017350183501935020350213502235023350243502535026350273502835029350303503135032350333503435035350363503735038350393504035041350423504335044350453504635047350483504935050350513505235053350543505535056350573505835059350603506135062350633506435065350663506735068350693507035071350723507335074350753507635077350783507935080350813508235083350843508535086350873508835089350903509135092350933509435095350963509735098350993510035101351023510335104351053510635107351083510935110351113511235113351143511535116351173511835119351203512135122351233512435125351263512735128351293513035131351323513335134351353513635137351383513935140351413514235143351443514535146351473514835149351503515135152351533515435155351563515735158351593516035161351623516335164351653516635167351683516935170351713517235173351743517535176351773517835179351803518135182351833518435185351863518735188351893519035191351923519335194351953519635197351983519935200352013520235203352043520535206352073520835209352103521135212352133521435215352163521735218352193522035221352223522335224352253522635227352283522935230352313523235233352343523535236352373523835239352403524135242352433524435245352463524735248352493525035251352523525335254352553525635257352583525935260352613526235263352643526535266352673526835269352703527135272352733527435275352763527735278352793528035281352823528335284352853528635287352883528935290352913529235293352943529535296352973529835299353003530135302353033530435305353063530735308353093531035311353123531335314353153531635317353183531935320353213532235323353243532535326353273532835329353303533135332353333533435335353363533735338353393534035341353423534335344353453534635347353483534935350353513535235353353543535535356353573535835359353603536135362353633536435365353663536735368353693537035371353723537335374353753537635377353783537935380353813538235383353843538535386353873538835389353903539135392353933539435395353963539735398353993540035401354023540335404354053540635407354083540935410354113541235413354143541535416354173541835419354203542135422354233542435425354263542735428354293543035431354323543335434354353543635437354383543935440354413544235443354443544535446354473544835449354503545135452354533545435455354563545735458354593546035461354623546335464354653546635467354683546935470354713547235473354743547535476354773547835479354803548135482354833548435485354863548735488354893549035491354923549335494354953549635497354983549935500355013550235503355043550535506355073550835509355103551135512355133551435515355163551735518355193552035521355223552335524355253552635527355283552935530355313553235533355343553535536355373553835539355403554135542355433554435545355463554735548355493555035551355523555335554355553555635557355583555935560355613556235563355643556535566355673556835569355703557135572355733557435575355763557735578355793558035581355823558335584355853558635587355883558935590355913559235593355943559535596355973559835599356003560135602356033560435605356063560735608356093561035611356123561335614356153561635617356183561935620356213562235623356243562535626356273562835629356303563135632356333563435635356363563735638356393564035641356423564335644356453564635647356483564935650356513565235653356543565535656356573565835659356603566135662356633566435665356663566735668356693567035671356723567335674356753567635677356783567935680356813568235683356843568535686356873568835689356903569135692356933569435695356963569735698356993570035701357023570335704357053570635707357083570935710357113571235713357143571535716357173571835719357203572135722357233572435725357263572735728357293573035731357323573335734357353573635737357383573935740357413574235743357443574535746357473574835749357503575135752357533575435755357563575735758357593576035761357623576335764357653576635767357683576935770357713577235773357743577535776357773577835779357803578135782357833578435785357863578735788357893579035791357923579335794357953579635797357983579935800358013580235803358043580535806358073580835809358103581135812358133581435815358163581735818358193582035821358223582335824358253582635827358283582935830358313583235833358343583535836358373583835839358403584135842358433584435845358463584735848358493585035851358523585335854358553585635857358583585935860358613586235863358643586535866358673586835869358703587135872358733587435875358763587735878358793588035881358823588335884358853588635887358883588935890358913589235893358943589535896358973589835899359003590135902359033590435905359063590735908359093591035911359123591335914359153591635917359183591935920359213592235923359243592535926359273592835929359303593135932359333593435935359363593735938359393594035941359423594335944359453594635947359483594935950359513595235953359543595535956359573595835959359603596135962359633596435965359663596735968359693597035971359723597335974359753597635977359783597935980359813598235983359843598535986359873598835989359903599135992359933599435995359963599735998359993600036001360023600336004360053600636007360083600936010360113601236013360143601536016360173601836019360203602136022360233602436025360263602736028360293603036031360323603336034360353603636037360383603936040360413604236043360443604536046360473604836049360503605136052360533605436055360563605736058360593606036061360623606336064360653606636067360683606936070360713607236073360743607536076360773607836079360803608136082360833608436085360863608736088360893609036091360923609336094360953609636097360983609936100361013610236103361043610536106361073610836109361103611136112361133611436115361163611736118361193612036121361223612336124361253612636127361283612936130361313613236133361343613536136361373613836139361403614136142361433614436145361463614736148361493615036151361523615336154361553615636157361583615936160361613616236163361643616536166361673616836169361703617136172361733617436175361763617736178361793618036181361823618336184361853618636187361883618936190361913619236193361943619536196361973619836199362003620136202362033620436205362063620736208362093621036211362123621336214362153621636217362183621936220362213622236223362243622536226362273622836229362303623136232362333623436235362363623736238362393624036241362423624336244362453624636247362483624936250362513625236253362543625536256362573625836259362603626136262362633626436265362663626736268362693627036271362723627336274362753627636277362783627936280362813628236283362843628536286362873628836289362903629136292362933629436295362963629736298362993630036301363023630336304363053630636307363083630936310363113631236313363143631536316363173631836319363203632136322363233632436325363263632736328363293633036331363323633336334363353633636337363383633936340363413634236343363443634536346363473634836349363503635136352363533635436355363563635736358363593636036361363623636336364363653636636367363683636936370363713637236373363743637536376363773637836379363803638136382363833638436385363863638736388363893639036391363923639336394363953639636397363983639936400364013640236403364043640536406364073640836409364103641136412364133641436415364163641736418364193642036421364223642336424364253642636427364283642936430364313643236433364343643536436364373643836439364403644136442364433644436445364463644736448364493645036451364523645336454364553645636457364583645936460364613646236463364643646536466364673646836469364703647136472364733647436475364763647736478364793648036481364823648336484364853648636487364883648936490364913649236493364943649536496364973649836499365003650136502365033650436505365063650736508365093651036511365123651336514365153651636517365183651936520365213652236523365243652536526365273652836529365303653136532365333653436535365363653736538365393654036541365423654336544365453654636547365483654936550365513655236553365543655536556365573655836559365603656136562365633656436565365663656736568365693657036571365723657336574365753657636577365783657936580365813658236583365843658536586365873658836589365903659136592365933659436595365963659736598365993660036601366023660336604366053660636607366083660936610366113661236613366143661536616366173661836619366203662136622366233662436625366263662736628366293663036631366323663336634366353663636637366383663936640366413664236643366443664536646366473664836649366503665136652366533665436655366563665736658366593666036661366623666336664366653666636667366683666936670366713667236673366743667536676366773667836679366803668136682366833668436685366863668736688366893669036691366923669336694366953669636697366983669936700367013670236703367043670536706367073670836709367103671136712367133671436715367163671736718367193672036721367223672336724367253672636727367283672936730367313673236733367343673536736367373673836739367403674136742367433674436745367463674736748367493675036751367523675336754367553675636757367583675936760367613676236763367643676536766367673676836769367703677136772367733677436775367763677736778367793678036781367823678336784367853678636787367883678936790367913679236793367943679536796367973679836799368003680136802368033680436805368063680736808368093681036811368123681336814368153681636817368183681936820368213682236823368243682536826368273682836829368303683136832368333683436835368363683736838368393684036841368423684336844368453684636847368483684936850368513685236853368543685536856368573685836859368603686136862368633686436865368663686736868368693687036871368723687336874368753687636877368783687936880368813688236883368843688536886368873688836889368903689136892368933689436895368963689736898368993690036901369023690336904369053690636907369083690936910369113691236913369143691536916369173691836919369203692136922369233692436925369263692736928369293693036931369323693336934369353693636937369383693936940369413694236943369443694536946369473694836949369503695136952369533695436955369563695736958369593696036961369623696336964369653696636967369683696936970369713697236973369743697536976369773697836979369803698136982369833698436985369863698736988369893699036991369923699336994369953699636997369983699937000370013700237003370043700537006370073700837009370103701137012370133701437015370163701737018370193702037021370223702337024370253702637027370283702937030370313703237033370343703537036370373703837039370403704137042370433704437045370463704737048370493705037051370523705337054370553705637057370583705937060370613706237063370643706537066370673706837069370703707137072370733707437075370763707737078370793708037081370823708337084370853708637087370883708937090370913709237093370943709537096370973709837099371003710137102371033710437105371063710737108371093711037111371123711337114371153711637117371183711937120371213712237123371243712537126371273712837129371303713137132371333713437135371363713737138371393714037141371423714337144371453714637147371483714937150371513715237153371543715537156371573715837159371603716137162371633716437165371663716737168371693717037171371723717337174371753717637177371783717937180371813718237183371843718537186371873718837189371903719137192371933719437195371963719737198371993720037201372023720337204372053720637207372083720937210372113721237213372143721537216372173721837219372203722137222372233722437225372263722737228372293723037231372323723337234372353723637237372383723937240372413724237243372443724537246372473724837249372503725137252372533725437255372563725737258372593726037261372623726337264372653726637267372683726937270372713727237273372743727537276372773727837279372803728137282372833728437285372863728737288372893729037291372923729337294372953729637297372983729937300373013730237303373043730537306373073730837309373103731137312373133731437315373163731737318373193732037321373223732337324373253732637327373283732937330373313733237333373343733537336373373733837339373403734137342373433734437345373463734737348373493735037351373523735337354373553735637357373583735937360373613736237363373643736537366373673736837369373703737137372373733737437375373763737737378373793738037381373823738337384373853738637387373883738937390373913739237393373943739537396373973739837399374003740137402374033740437405374063740737408374093741037411374123741337414374153741637417374183741937420374213742237423374243742537426374273742837429374303743137432374333743437435374363743737438374393744037441374423744337444374453744637447374483744937450374513745237453374543745537456374573745837459374603746137462374633746437465374663746737468374693747037471374723747337474374753747637477374783747937480374813748237483374843748537486374873748837489374903749137492374933749437495374963749737498374993750037501375023750337504375053750637507375083750937510375113751237513375143751537516375173751837519375203752137522375233752437525375263752737528375293753037531375323753337534375353753637537375383753937540375413754237543375443754537546375473754837549375503755137552375533755437555375563755737558375593756037561375623756337564375653756637567375683756937570375713757237573375743757537576375773757837579375803758137582375833758437585375863758737588375893759037591375923759337594375953759637597375983759937600376013760237603376043760537606376073760837609376103761137612376133761437615376163761737618376193762037621376223762337624376253762637627376283762937630376313763237633376343763537636376373763837639376403764137642376433764437645376463764737648376493765037651376523765337654376553765637657376583765937660376613766237663376643766537666376673766837669376703767137672376733767437675376763767737678376793768037681376823768337684376853768637687376883768937690376913769237693376943769537696376973769837699377003770137702377033770437705377063770737708377093771037711377123771337714377153771637717377183771937720377213772237723377243772537726377273772837729377303773137732377333773437735377363773737738377393774037741377423774337744377453774637747377483774937750377513775237753377543775537756377573775837759377603776137762377633776437765377663776737768377693777037771377723777337774377753777637777377783777937780377813778237783377843778537786377873778837789377903779137792377933779437795377963779737798377993780037801378023780337804378053780637807378083780937810378113781237813378143781537816378173781837819378203782137822378233782437825378263782737828378293783037831378323783337834378353783637837378383783937840378413784237843378443784537846378473784837849378503785137852378533785437855378563785737858378593786037861378623786337864378653786637867378683786937870378713787237873378743787537876378773787837879378803788137882378833788437885378863788737888378893789037891378923789337894378953789637897378983789937900379013790237903379043790537906379073790837909379103791137912379133791437915379163791737918379193792037921379223792337924379253792637927379283792937930379313793237933379343793537936379373793837939379403794137942379433794437945379463794737948379493795037951379523795337954379553795637957379583795937960379613796237963379643796537966379673796837969379703797137972379733797437975379763797737978379793798037981379823798337984379853798637987379883798937990379913799237993379943799537996379973799837999380003800138002380033800438005380063800738008380093801038011380123801338014380153801638017380183801938020380213802238023380243802538026380273802838029380303803138032380333803438035380363803738038380393804038041380423804338044380453804638047380483804938050380513805238053380543805538056380573805838059380603806138062380633806438065380663806738068380693807038071380723807338074380753807638077380783807938080380813808238083380843808538086380873808838089380903809138092380933809438095380963809738098380993810038101381023810338104381053810638107381083810938110381113811238113381143811538116381173811838119381203812138122381233812438125381263812738128381293813038131381323813338134381353813638137381383813938140381413814238143381443814538146381473814838149381503815138152381533815438155381563815738158381593816038161381623816338164381653816638167381683816938170381713817238173381743817538176381773817838179381803818138182381833818438185381863818738188381893819038191381923819338194381953819638197381983819938200382013820238203382043820538206382073820838209382103821138212382133821438215382163821738218382193822038221382223822338224382253822638227382283822938230382313823238233382343823538236382373823838239382403824138242382433824438245382463824738248382493825038251382523825338254382553825638257382583825938260382613826238263382643826538266382673826838269382703827138272382733827438275382763827738278382793828038281382823828338284382853828638287382883828938290382913829238293382943829538296382973829838299383003830138302383033830438305383063830738308383093831038311383123831338314383153831638317383183831938320383213832238323383243832538326383273832838329383303833138332383333833438335383363833738338383393834038341383423834338344383453834638347383483834938350383513835238353383543835538356383573835838359383603836138362383633836438365383663836738368383693837038371383723837338374383753837638377383783837938380383813838238383383843838538386383873838838389383903839138392383933839438395383963839738398383993840038401384023840338404384053840638407384083840938410384113841238413384143841538416384173841838419384203842138422384233842438425384263842738428384293843038431384323843338434384353843638437384383843938440384413844238443384443844538446384473844838449384503845138452384533845438455384563845738458384593846038461384623846338464384653846638467384683846938470384713847238473384743847538476384773847838479384803848138482384833848438485384863848738488384893849038491384923849338494384953849638497384983849938500385013850238503385043850538506385073850838509385103851138512385133851438515385163851738518385193852038521385223852338524385253852638527385283852938530385313853238533385343853538536385373853838539385403854138542385433854438545385463854738548385493855038551385523855338554385553855638557385583855938560385613856238563385643856538566385673856838569385703857138572385733857438575385763857738578385793858038581385823858338584385853858638587385883858938590385913859238593385943859538596385973859838599386003860138602386033860438605386063860738608386093861038611386123861338614386153861638617386183861938620386213862238623386243862538626386273862838629386303863138632386333863438635386363863738638386393864038641386423864338644386453864638647386483864938650386513865238653386543865538656386573865838659386603866138662386633866438665386663866738668386693867038671386723867338674386753867638677386783867938680386813868238683386843868538686386873868838689386903869138692386933869438695386963869738698386993870038701387023870338704387053870638707387083870938710387113871238713387143871538716387173871838719387203872138722387233872438725387263872738728387293873038731387323873338734387353873638737387383873938740387413874238743387443874538746387473874838749387503875138752387533875438755387563875738758387593876038761387623876338764387653876638767387683876938770387713877238773387743877538776387773877838779387803878138782387833878438785387863878738788387893879038791387923879338794387953879638797387983879938800388013880238803388043880538806388073880838809388103881138812388133881438815388163881738818388193882038821388223882338824388253882638827388283882938830388313883238833388343883538836388373883838839388403884138842388433884438845388463884738848388493885038851388523885338854388553885638857388583885938860388613886238863388643886538866388673886838869388703887138872388733887438875388763887738878388793888038881388823888338884388853888638887388883888938890388913889238893388943889538896388973889838899389003890138902389033890438905389063890738908389093891038911389123891338914389153891638917389183891938920389213892238923389243892538926389273892838929389303893138932389333893438935389363893738938389393894038941389423894338944389453894638947389483894938950389513895238953389543895538956389573895838959389603896138962389633896438965389663896738968389693897038971389723897338974389753897638977389783897938980389813898238983389843898538986389873898838989389903899138992389933899438995389963899738998389993900039001390023900339004390053900639007390083900939010390113901239013390143901539016390173901839019390203902139022390233902439025390263902739028390293903039031390323903339034390353903639037390383903939040390413904239043390443904539046390473904839049390503905139052390533905439055390563905739058390593906039061390623906339064390653906639067390683906939070390713907239073390743907539076390773907839079390803908139082390833908439085390863908739088390893909039091390923909339094390953909639097390983909939100391013910239103391043910539106391073910839109391103911139112391133911439115391163911739118391193912039121391223912339124391253912639127391283912939130391313913239133391343913539136391373913839139391403914139142391433914439145391463914739148391493915039151391523915339154391553915639157391583915939160391613916239163391643916539166391673916839169391703917139172391733917439175391763917739178391793918039181391823918339184391853918639187391883918939190391913919239193391943919539196391973919839199392003920139202392033920439205392063920739208392093921039211392123921339214392153921639217392183921939220392213922239223392243922539226392273922839229392303923139232392333923439235392363923739238392393924039241392423924339244392453924639247392483924939250392513925239253392543925539256392573925839259392603926139262392633926439265392663926739268392693927039271392723927339274392753927639277392783927939280392813928239283392843928539286392873928839289392903929139292392933929439295392963929739298392993930039301393023930339304393053930639307393083930939310393113931239313393143931539316393173931839319393203932139322393233932439325393263932739328393293933039331393323933339334393353933639337393383933939340393413934239343393443934539346393473934839349393503935139352393533935439355393563935739358393593936039361393623936339364393653936639367393683936939370393713937239373393743937539376393773937839379393803938139382393833938439385393863938739388393893939039391393923939339394393953939639397393983939939400394013940239403394043940539406394073940839409394103941139412394133941439415394163941739418394193942039421394223942339424394253942639427394283942939430394313943239433394343943539436394373943839439394403944139442394433944439445394463944739448394493945039451394523945339454394553945639457394583945939460394613946239463394643946539466394673946839469394703947139472394733947439475394763947739478394793948039481394823948339484394853948639487394883948939490394913949239493394943949539496394973949839499395003950139502395033950439505395063950739508395093951039511395123951339514395153951639517395183951939520395213952239523395243952539526395273952839529395303953139532395333953439535395363953739538395393954039541395423954339544395453954639547395483954939550395513955239553395543955539556395573955839559395603956139562395633956439565395663956739568395693957039571395723957339574395753957639577395783957939580395813958239583395843958539586395873958839589395903959139592395933959439595395963959739598395993960039601396023960339604396053960639607396083960939610396113961239613396143961539616396173961839619396203962139622396233962439625396263962739628396293963039631396323963339634396353963639637396383963939640396413964239643396443964539646396473964839649396503965139652396533965439655396563965739658396593966039661396623966339664396653966639667396683966939670396713967239673396743967539676396773967839679396803968139682396833968439685396863968739688396893969039691396923969339694396953969639697396983969939700397013970239703397043970539706397073970839709397103971139712397133971439715397163971739718397193972039721397223972339724397253972639727397283972939730397313973239733397343973539736397373973839739397403974139742397433974439745397463974739748397493975039751397523975339754397553975639757397583975939760397613976239763397643976539766397673976839769397703977139772397733977439775397763977739778397793978039781397823978339784397853978639787397883978939790397913979239793397943979539796397973979839799398003980139802398033980439805398063980739808398093981039811398123981339814398153981639817398183981939820398213982239823398243982539826398273982839829398303983139832398333983439835398363983739838398393984039841398423984339844398453984639847398483984939850398513985239853398543985539856398573985839859398603986139862398633986439865398663986739868398693987039871398723987339874398753987639877398783987939880398813988239883398843988539886398873988839889398903989139892398933989439895398963989739898398993990039901399023990339904399053990639907399083990939910399113991239913399143991539916399173991839919399203992139922399233992439925399263992739928399293993039931399323993339934399353993639937399383993939940399413994239943399443994539946399473994839949399503995139952399533995439955399563995739958399593996039961399623996339964399653996639967399683996939970399713997239973399743997539976399773997839979399803998139982399833998439985399863998739988399893999039991399923999339994399953999639997399983999940000400014000240003400044000540006400074000840009400104001140012400134001440015400164001740018400194002040021400224002340024400254002640027400284002940030400314003240033400344003540036400374003840039400404004140042400434004440045400464004740048400494005040051400524005340054400554005640057400584005940060400614006240063400644006540066400674006840069400704007140072400734007440075400764007740078400794008040081400824008340084400854008640087400884008940090400914009240093400944009540096400974009840099401004010140102401034010440105401064010740108401094011040111401124011340114401154011640117401184011940120401214012240123401244012540126401274012840129401304013140132401334013440135401364013740138401394014040141401424014340144401454014640147401484014940150401514015240153401544015540156401574015840159401604016140162401634016440165401664016740168401694017040171401724017340174401754017640177401784017940180401814018240183401844018540186401874018840189401904019140192401934019440195401964019740198401994020040201402024020340204402054020640207402084020940210402114021240213402144021540216402174021840219402204022140222402234022440225402264022740228402294023040231402324023340234402354023640237402384023940240402414024240243402444024540246402474024840249402504025140252402534025440255402564025740258402594026040261402624026340264402654026640267402684026940270402714027240273402744027540276402774027840279402804028140282402834028440285402864028740288402894029040291402924029340294402954029640297402984029940300403014030240303403044030540306403074030840309403104031140312403134031440315403164031740318403194032040321403224032340324403254032640327403284032940330403314033240333403344033540336403374033840339403404034140342403434034440345403464034740348403494035040351403524035340354403554035640357403584035940360403614036240363403644036540366403674036840369403704037140372403734037440375403764037740378403794038040381403824038340384403854038640387403884038940390403914039240393403944039540396403974039840399404004040140402404034040440405404064040740408404094041040411404124041340414404154041640417404184041940420404214042240423404244042540426404274042840429404304043140432404334043440435404364043740438404394044040441404424044340444404454044640447404484044940450404514045240453404544045540456404574045840459404604046140462404634046440465404664046740468404694047040471404724047340474404754047640477404784047940480404814048240483404844048540486404874048840489404904049140492404934049440495404964049740498404994050040501405024050340504405054050640507405084050940510405114051240513405144051540516405174051840519405204052140522405234052440525405264052740528405294053040531405324053340534405354053640537405384053940540405414054240543405444054540546405474054840549405504055140552405534055440555405564055740558405594056040561405624056340564405654056640567405684056940570405714057240573405744057540576405774057840579405804058140582405834058440585405864058740588405894059040591405924059340594405954059640597405984059940600406014060240603406044060540606406074060840609406104061140612406134061440615406164061740618406194062040621406224062340624406254062640627406284062940630406314063240633406344063540636406374063840639406404064140642406434064440645406464064740648406494065040651406524065340654406554065640657406584065940660406614066240663406644066540666406674066840669406704067140672406734067440675406764067740678406794068040681406824068340684406854068640687406884068940690406914069240693406944069540696406974069840699407004070140702407034070440705407064070740708407094071040711407124071340714407154071640717407184071940720407214072240723407244072540726407274072840729407304073140732407334073440735407364073740738407394074040741407424074340744407454074640747407484074940750407514075240753407544075540756407574075840759407604076140762407634076440765407664076740768407694077040771407724077340774407754077640777407784077940780407814078240783407844078540786407874078840789407904079140792407934079440795407964079740798407994080040801408024080340804408054080640807408084080940810408114081240813408144081540816408174081840819408204082140822408234082440825408264082740828408294083040831408324083340834408354083640837408384083940840408414084240843408444084540846408474084840849408504085140852408534085440855408564085740858408594086040861408624086340864408654086640867408684086940870408714087240873408744087540876408774087840879408804088140882408834088440885408864088740888408894089040891408924089340894408954089640897408984089940900409014090240903409044090540906409074090840909409104091140912409134091440915409164091740918409194092040921409224092340924409254092640927409284092940930409314093240933409344093540936409374093840939409404094140942409434094440945409464094740948409494095040951409524095340954409554095640957409584095940960409614096240963409644096540966409674096840969409704097140972409734097440975409764097740978409794098040981409824098340984409854098640987409884098940990409914099240993409944099540996409974099840999410004100141002410034100441005410064100741008410094101041011410124101341014410154101641017410184101941020410214102241023410244102541026410274102841029410304103141032410334103441035410364103741038410394104041041410424104341044410454104641047410484104941050410514105241053410544105541056410574105841059410604106141062410634106441065410664106741068410694107041071410724107341074410754107641077410784107941080410814108241083410844108541086410874108841089410904109141092410934109441095410964109741098410994110041101411024110341104411054110641107411084110941110411114111241113411144111541116411174111841119411204112141122411234112441125411264112741128411294113041131411324113341134411354113641137411384113941140411414114241143411444114541146411474114841149411504115141152411534115441155411564115741158411594116041161411624116341164411654116641167411684116941170411714117241173411744117541176411774117841179411804118141182411834118441185411864118741188411894119041191411924119341194411954119641197411984119941200412014120241203412044120541206412074120841209412104121141212412134121441215412164121741218412194122041221412224122341224412254122641227412284122941230412314123241233412344123541236412374123841239412404124141242412434124441245412464124741248412494125041251412524125341254412554125641257412584125941260412614126241263412644126541266412674126841269412704127141272412734127441275412764127741278412794128041281412824128341284412854128641287412884128941290412914129241293412944129541296412974129841299413004130141302413034130441305413064130741308413094131041311413124131341314413154131641317413184131941320413214132241323413244132541326413274132841329413304133141332413334133441335413364133741338413394134041341413424134341344413454134641347413484134941350413514135241353413544135541356413574135841359413604136141362413634136441365413664136741368413694137041371413724137341374413754137641377413784137941380413814138241383413844138541386413874138841389413904139141392413934139441395413964139741398413994140041401414024140341404414054140641407414084140941410414114141241413414144141541416414174141841419414204142141422414234142441425414264142741428414294143041431414324143341434414354143641437414384143941440414414144241443414444144541446414474144841449414504145141452414534145441455414564145741458414594146041461414624146341464414654146641467414684146941470414714147241473414744147541476414774147841479414804148141482414834148441485414864148741488414894149041491414924149341494414954149641497414984149941500415014150241503415044150541506415074150841509415104151141512415134151441515415164151741518415194152041521415224152341524415254152641527415284152941530415314153241533415344153541536415374153841539415404154141542415434154441545415464154741548415494155041551415524155341554415554155641557415584155941560415614156241563415644156541566415674156841569415704157141572415734157441575415764157741578415794158041581415824158341584415854158641587415884158941590415914159241593415944159541596415974159841599416004160141602416034160441605416064160741608416094161041611416124161341614416154161641617416184161941620416214162241623416244162541626416274162841629416304163141632416334163441635416364163741638416394164041641416424164341644416454164641647416484164941650416514165241653416544165541656416574165841659416604166141662416634166441665416664166741668416694167041671416724167341674416754167641677416784167941680416814168241683416844168541686416874168841689416904169141692416934169441695416964169741698416994170041701417024170341704417054170641707417084170941710417114171241713417144171541716417174171841719417204172141722417234172441725417264172741728417294173041731417324173341734417354173641737417384173941740417414174241743417444174541746417474174841749417504175141752417534175441755417564175741758417594176041761417624176341764417654176641767417684176941770417714177241773417744177541776417774177841779417804178141782417834178441785417864178741788417894179041791417924179341794417954179641797417984179941800418014180241803418044180541806418074180841809418104181141812418134181441815418164181741818418194182041821418224182341824418254182641827418284182941830418314183241833418344183541836418374183841839418404184141842418434184441845418464184741848418494185041851418524185341854418554185641857418584185941860418614186241863418644186541866418674186841869418704187141872418734187441875418764187741878418794188041881418824188341884418854188641887418884188941890418914189241893418944189541896418974189841899419004190141902419034190441905419064190741908419094191041911419124191341914419154191641917419184191941920419214192241923419244192541926419274192841929419304193141932419334193441935419364193741938419394194041941419424194341944419454194641947419484194941950419514195241953419544195541956419574195841959419604196141962419634196441965419664196741968419694197041971419724197341974419754197641977419784197941980419814198241983419844198541986419874198841989419904199141992419934199441995419964199741998419994200042001420024200342004420054200642007420084200942010420114201242013420144201542016420174201842019420204202142022420234202442025420264202742028420294203042031420324203342034420354203642037420384203942040420414204242043420444204542046420474204842049420504205142052420534205442055420564205742058420594206042061420624206342064420654206642067420684206942070420714207242073420744207542076420774207842079420804208142082420834208442085420864208742088420894209042091420924209342094420954209642097420984209942100421014210242103421044210542106421074210842109421104211142112421134211442115421164211742118421194212042121421224212342124421254212642127421284212942130421314213242133421344213542136421374213842139421404214142142421434214442145421464214742148421494215042151421524215342154421554215642157421584215942160421614216242163421644216542166421674216842169421704217142172421734217442175421764217742178421794218042181421824218342184421854218642187421884218942190421914219242193421944219542196421974219842199422004220142202422034220442205422064220742208422094221042211422124221342214422154221642217422184221942220422214222242223422244222542226422274222842229422304223142232422334223442235422364223742238422394224042241422424224342244422454224642247422484224942250422514225242253422544225542256422574225842259422604226142262422634226442265422664226742268422694227042271422724227342274422754227642277422784227942280422814228242283422844228542286422874228842289422904229142292422934229442295422964229742298422994230042301423024230342304423054230642307423084230942310423114231242313423144231542316423174231842319423204232142322423234232442325423264232742328423294233042331423324233342334423354233642337423384233942340423414234242343423444234542346423474234842349423504235142352423534235442355423564235742358423594236042361423624236342364423654236642367423684236942370423714237242373423744237542376423774237842379423804238142382423834238442385423864238742388423894239042391423924239342394423954239642397423984239942400424014240242403424044240542406424074240842409424104241142412424134241442415424164241742418424194242042421424224242342424424254242642427424284242942430424314243242433424344243542436424374243842439424404244142442424434244442445424464244742448424494245042451424524245342454424554245642457424584245942460424614246242463424644246542466424674246842469424704247142472424734247442475424764247742478424794248042481424824248342484424854248642487424884248942490424914249242493424944249542496424974249842499425004250142502425034250442505425064250742508425094251042511425124251342514425154251642517425184251942520425214252242523425244252542526425274252842529425304253142532425334253442535425364253742538425394254042541425424254342544425454254642547425484254942550425514255242553425544255542556425574255842559425604256142562425634256442565425664256742568425694257042571425724257342574425754257642577425784257942580425814258242583425844258542586425874258842589425904259142592425934259442595425964259742598425994260042601426024260342604426054260642607426084260942610426114261242613426144261542616426174261842619426204262142622426234262442625426264262742628426294263042631426324263342634426354263642637426384263942640426414264242643426444264542646426474264842649426504265142652426534265442655426564265742658426594266042661426624266342664426654266642667426684266942670426714267242673426744267542676426774267842679426804268142682426834268442685426864268742688426894269042691426924269342694426954269642697426984269942700427014270242703427044270542706427074270842709427104271142712427134271442715427164271742718427194272042721427224272342724427254272642727427284272942730427314273242733427344273542736427374273842739427404274142742427434274442745427464274742748427494275042751427524275342754427554275642757427584275942760427614276242763427644276542766427674276842769427704277142772427734277442775427764277742778427794278042781427824278342784427854278642787427884278942790427914279242793427944279542796427974279842799428004280142802428034280442805428064280742808428094281042811428124281342814428154281642817428184281942820428214282242823428244282542826428274282842829428304283142832428334283442835428364283742838428394284042841428424284342844428454284642847428484284942850428514285242853428544285542856428574285842859428604286142862428634286442865428664286742868428694287042871428724287342874428754287642877428784287942880428814288242883428844288542886428874288842889428904289142892428934289442895428964289742898428994290042901429024290342904429054290642907429084290942910429114291242913429144291542916429174291842919429204292142922429234292442925429264292742928429294293042931429324293342934429354293642937429384293942940429414294242943429444294542946429474294842949429504295142952429534295442955429564295742958429594296042961429624296342964429654296642967429684296942970429714297242973429744297542976429774297842979429804298142982429834298442985429864298742988429894299042991429924299342994429954299642997429984299943000430014300243003430044300543006430074300843009430104301143012430134301443015430164301743018430194302043021430224302343024430254302643027430284302943030430314303243033430344303543036430374303843039430404304143042430434304443045430464304743048430494305043051430524305343054430554305643057430584305943060430614306243063430644306543066430674306843069430704307143072430734307443075430764307743078430794308043081430824308343084430854308643087430884308943090430914309243093430944309543096430974309843099431004310143102431034310443105431064310743108431094311043111431124311343114431154311643117431184311943120431214312243123431244312543126431274312843129431304313143132431334313443135431364313743138431394314043141431424314343144431454314643147431484314943150431514315243153431544315543156431574315843159431604316143162431634316443165431664316743168431694317043171431724317343174431754317643177431784317943180431814318243183431844318543186431874318843189431904319143192431934319443195431964319743198431994320043201432024320343204432054320643207432084320943210432114321243213432144321543216432174321843219432204322143222432234322443225432264322743228432294323043231432324323343234432354323643237432384323943240432414324243243432444324543246432474324843249432504325143252432534325443255432564325743258432594326043261432624326343264432654326643267432684326943270432714327243273432744327543276432774327843279432804328143282432834328443285432864328743288432894329043291432924329343294432954329643297432984329943300433014330243303433044330543306433074330843309433104331143312433134331443315433164331743318433194332043321433224332343324433254332643327433284332943330433314333243333433344333543336433374333843339433404334143342433434334443345433464334743348433494335043351433524335343354433554335643357433584335943360433614336243363433644336543366433674336843369433704337143372433734337443375433764337743378433794338043381433824338343384433854338643387433884338943390433914339243393433944339543396433974339843399434004340143402434034340443405434064340743408434094341043411434124341343414434154341643417434184341943420434214342243423434244342543426434274342843429434304343143432434334343443435434364343743438434394344043441434424344343444434454344643447434484344943450434514345243453434544345543456434574345843459434604346143462434634346443465434664346743468434694347043471434724347343474434754347643477434784347943480434814348243483434844348543486434874348843489434904349143492434934349443495434964349743498434994350043501435024350343504435054350643507435084350943510435114351243513435144351543516435174351843519435204352143522435234352443525435264352743528435294353043531435324353343534435354353643537435384353943540435414354243543435444354543546435474354843549435504355143552435534355443555435564355743558435594356043561435624356343564435654356643567435684356943570435714357243573435744357543576435774357843579435804358143582435834358443585435864358743588435894359043591435924359343594435954359643597435984359943600436014360243603436044360543606436074360843609436104361143612436134361443615436164361743618436194362043621436224362343624436254362643627436284362943630436314363243633436344363543636436374363843639436404364143642436434364443645436464364743648436494365043651436524365343654436554365643657436584365943660436614366243663436644366543666436674366843669436704367143672436734367443675436764367743678436794368043681436824368343684436854368643687436884368943690436914369243693436944369543696436974369843699437004370143702437034370443705437064370743708437094371043711437124371343714437154371643717437184371943720437214372243723437244372543726437274372843729437304373143732437334373443735437364373743738437394374043741437424374343744437454374643747437484374943750437514375243753437544375543756437574375843759437604376143762437634376443765437664376743768437694377043771437724377343774437754377643777437784377943780437814378243783437844378543786437874378843789437904379143792437934379443795437964379743798437994380043801438024380343804438054380643807438084380943810438114381243813438144381543816438174381843819438204382143822438234382443825438264382743828438294383043831438324383343834438354383643837438384383943840438414384243843438444384543846438474384843849438504385143852438534385443855438564385743858438594386043861438624386343864438654386643867438684386943870438714387243873438744387543876438774387843879438804388143882438834388443885438864388743888438894389043891438924389343894438954389643897438984389943900439014390243903439044390543906439074390843909439104391143912439134391443915439164391743918439194392043921439224392343924439254392643927439284392943930439314393243933439344393543936439374393843939439404394143942439434394443945439464394743948439494395043951439524395343954439554395643957439584395943960439614396243963439644396543966439674396843969439704397143972439734397443975439764397743978439794398043981439824398343984439854398643987439884398943990439914399243993439944399543996439974399843999440004400144002440034400444005440064400744008440094401044011440124401344014440154401644017440184401944020440214402244023440244402544026440274402844029440304403144032440334403444035440364403744038440394404044041440424404344044440454404644047440484404944050440514405244053440544405544056440574405844059440604406144062440634406444065440664406744068440694407044071440724407344074440754407644077440784407944080440814408244083440844408544086440874408844089440904409144092440934409444095440964409744098440994410044101441024410344104441054410644107441084410944110441114411244113441144411544116441174411844119441204412144122441234412444125441264412744128441294413044131441324413344134441354413644137441384413944140441414414244143441444414544146441474414844149441504415144152441534415444155441564415744158441594416044161441624416344164441654416644167441684416944170441714417244173441744417544176441774417844179441804418144182441834418444185441864418744188441894419044191441924419344194441954419644197441984419944200442014420244203442044420544206442074420844209442104421144212442134421444215442164421744218442194422044221442224422344224442254422644227442284422944230442314423244233442344423544236442374423844239442404424144242442434424444245442464424744248442494425044251442524425344254442554425644257442584425944260442614426244263442644426544266442674426844269442704427144272442734427444275442764427744278442794428044281442824428344284442854428644287442884428944290442914429244293442944429544296442974429844299443004430144302443034430444305443064430744308443094431044311443124431344314443154431644317443184431944320443214432244323443244432544326443274432844329443304433144332443334433444335443364433744338443394434044341443424434344344443454434644347443484434944350443514435244353443544435544356443574435844359443604436144362443634436444365443664436744368443694437044371443724437344374443754437644377443784437944380443814438244383443844438544386443874438844389443904439144392443934439444395443964439744398443994440044401444024440344404444054440644407444084440944410444114441244413444144441544416444174441844419444204442144422444234442444425444264442744428444294443044431444324443344434444354443644437444384443944440444414444244443444444444544446444474444844449444504445144452444534445444455444564445744458444594446044461444624446344464444654446644467444684446944470444714447244473444744447544476444774447844479444804448144482444834448444485444864448744488444894449044491444924449344494444954449644497444984449944500445014450244503445044450544506445074450844509445104451144512445134451444515445164451744518445194452044521445224452344524445254452644527445284452944530445314453244533445344453544536445374453844539445404454144542445434454444545445464454744548445494455044551445524455344554445554455644557445584455944560445614456244563445644456544566445674456844569445704457144572445734457444575445764457744578445794458044581445824458344584445854458644587445884458944590445914459244593445944459544596445974459844599446004460144602446034460444605446064460744608446094461044611446124461344614446154461644617446184461944620446214462244623446244462544626446274462844629446304463144632446334463444635446364463744638446394464044641446424464344644446454464644647446484464944650446514465244653446544465544656446574465844659446604466144662446634466444665446664466744668446694467044671446724467344674446754467644677446784467944680446814468244683446844468544686446874468844689446904469144692446934469444695446964469744698446994470044701447024470344704447054470644707447084470944710447114471244713447144471544716447174471844719447204472144722447234472444725447264472744728447294473044731447324473344734447354473644737447384473944740447414474244743447444474544746447474474844749447504475144752447534475444755447564475744758447594476044761447624476344764447654476644767447684476944770447714477244773447744477544776447774477844779447804478144782447834478444785447864478744788447894479044791447924479344794447954479644797447984479944800448014480244803448044480544806448074480844809448104481144812448134481444815448164481744818448194482044821448224482344824448254482644827448284482944830448314483244833448344483544836448374483844839448404484144842448434484444845448464484744848448494485044851448524485344854448554485644857448584485944860448614486244863448644486544866448674486844869448704487144872448734487444875448764487744878448794488044881448824488344884448854488644887448884488944890448914489244893448944489544896448974489844899449004490144902449034490444905449064490744908449094491044911449124491344914449154491644917449184491944920449214492244923449244492544926449274492844929449304493144932449334493444935449364493744938449394494044941449424494344944449454494644947449484494944950449514495244953449544495544956449574495844959449604496144962449634496444965449664496744968449694497044971449724497344974449754497644977449784497944980449814498244983449844498544986449874498844989449904499144992449934499444995449964499744998449994500045001450024500345004450054500645007450084500945010450114501245013450144501545016450174501845019450204502145022450234502445025450264502745028450294503045031450324503345034450354503645037450384503945040450414504245043450444504545046450474504845049450504505145052450534505445055450564505745058450594506045061450624506345064450654506645067450684506945070450714507245073450744507545076450774507845079450804508145082450834508445085450864508745088450894509045091450924509345094450954509645097450984509945100451014510245103451044510545106451074510845109451104511145112451134511445115451164511745118451194512045121451224512345124451254512645127451284512945130451314513245133451344513545136451374513845139451404514145142451434514445145451464514745148451494515045151451524515345154451554515645157451584515945160451614516245163451644516545166451674516845169451704517145172451734517445175451764517745178451794518045181451824518345184451854518645187451884518945190451914519245193451944519545196451974519845199452004520145202452034520445205452064520745208452094521045211452124521345214452154521645217452184521945220452214522245223452244522545226452274522845229452304523145232452334523445235452364523745238452394524045241452424524345244452454524645247452484524945250452514525245253452544525545256452574525845259452604526145262452634526445265452664526745268452694527045271452724527345274452754527645277452784527945280452814528245283452844528545286452874528845289452904529145292452934529445295452964529745298452994530045301453024530345304453054530645307453084530945310453114531245313453144531545316453174531845319453204532145322453234532445325453264532745328453294533045331453324533345334453354533645337453384533945340453414534245343453444534545346453474534845349453504535145352453534535445355453564535745358453594536045361453624536345364453654536645367453684536945370453714537245373453744537545376453774537845379453804538145382453834538445385453864538745388453894539045391453924539345394453954539645397453984539945400454014540245403454044540545406454074540845409454104541145412454134541445415454164541745418454194542045421454224542345424454254542645427454284542945430454314543245433454344543545436454374543845439454404544145442454434544445445454464544745448454494545045451454524545345454454554545645457454584545945460454614546245463454644546545466454674546845469454704547145472454734547445475454764547745478454794548045481454824548345484454854548645487454884548945490454914549245493454944549545496454974549845499455004550145502455034550445505455064550745508455094551045511455124551345514455154551645517455184551945520455214552245523455244552545526455274552845529455304553145532455334553445535455364553745538455394554045541455424554345544455454554645547455484554945550455514555245553455544555545556455574555845559455604556145562455634556445565455664556745568455694557045571455724557345574455754557645577455784557945580455814558245583455844558545586455874558845589455904559145592455934559445595455964559745598455994560045601456024560345604456054560645607456084560945610456114561245613456144561545616456174561845619456204562145622456234562445625456264562745628456294563045631456324563345634456354563645637456384563945640456414564245643456444564545646456474564845649456504565145652456534565445655456564565745658456594566045661456624566345664456654566645667456684566945670456714567245673456744567545676456774567845679456804568145682456834568445685456864568745688456894569045691456924569345694456954569645697456984569945700457014570245703457044570545706457074570845709457104571145712457134571445715457164571745718457194572045721457224572345724457254572645727457284572945730457314573245733457344573545736457374573845739457404574145742457434574445745457464574745748457494575045751457524575345754457554575645757457584575945760457614576245763457644576545766457674576845769457704577145772457734577445775457764577745778457794578045781457824578345784457854578645787457884578945790457914579245793457944579545796457974579845799458004580145802458034580445805458064580745808458094581045811458124581345814458154581645817458184581945820458214582245823458244582545826458274582845829458304583145832458334583445835458364583745838458394584045841458424584345844458454584645847458484584945850458514585245853458544585545856458574585845859458604586145862458634586445865458664586745868458694587045871458724587345874458754587645877458784587945880458814588245883458844588545886458874588845889458904589145892458934589445895458964589745898458994590045901459024590345904459054590645907459084590945910459114591245913459144591545916459174591845919459204592145922459234592445925459264592745928459294593045931459324593345934459354593645937459384593945940459414594245943459444594545946459474594845949459504595145952459534595445955459564595745958459594596045961459624596345964459654596645967459684596945970459714597245973459744597545976459774597845979459804598145982459834598445985459864598745988459894599045991459924599345994459954599645997459984599946000460014600246003460044600546006460074600846009460104601146012460134601446015460164601746018460194602046021460224602346024460254602646027460284602946030460314603246033460344603546036460374603846039460404604146042460434604446045460464604746048460494605046051460524605346054460554605646057460584605946060460614606246063460644606546066460674606846069460704607146072460734607446075460764607746078460794608046081460824608346084460854608646087460884608946090460914609246093460944609546096460974609846099461004610146102461034610446105461064610746108461094611046111461124611346114461154611646117461184611946120461214612246123461244612546126461274612846129461304613146132461334613446135461364613746138461394614046141461424614346144461454614646147461484614946150461514615246153461544615546156461574615846159461604616146162461634616446165461664616746168461694617046171461724617346174461754617646177461784617946180461814618246183461844618546186461874618846189461904619146192461934619446195461964619746198461994620046201462024620346204462054620646207462084620946210462114621246213462144621546216462174621846219462204622146222462234622446225462264622746228462294623046231462324623346234462354623646237462384623946240462414624246243462444624546246462474624846249462504625146252462534625446255462564625746258462594626046261462624626346264462654626646267462684626946270462714627246273462744627546276462774627846279462804628146282462834628446285462864628746288462894629046291462924629346294462954629646297462984629946300463014630246303463044630546306463074630846309463104631146312463134631446315463164631746318463194632046321463224632346324463254632646327463284632946330463314633246333463344633546336463374633846339463404634146342463434634446345463464634746348463494635046351463524635346354463554635646357463584635946360463614636246363463644636546366463674636846369463704637146372463734637446375463764637746378463794638046381463824638346384463854638646387463884638946390463914639246393463944639546396463974639846399464004640146402464034640446405464064640746408464094641046411464124641346414464154641646417464184641946420464214642246423464244642546426464274642846429464304643146432464334643446435464364643746438464394644046441464424644346444464454644646447464484644946450464514645246453464544645546456464574645846459464604646146462464634646446465464664646746468464694647046471464724647346474464754647646477464784647946480464814648246483464844648546486464874648846489464904649146492464934649446495464964649746498464994650046501465024650346504465054650646507465084650946510465114651246513465144651546516465174651846519465204652146522465234652446525465264652746528465294653046531465324653346534465354653646537465384653946540465414654246543465444654546546465474654846549465504655146552465534655446555465564655746558465594656046561465624656346564465654656646567465684656946570465714657246573465744657546576465774657846579465804658146582465834658446585465864658746588465894659046591465924659346594465954659646597465984659946600466014660246603466044660546606466074660846609466104661146612466134661446615466164661746618466194662046621466224662346624466254662646627466284662946630466314663246633466344663546636466374663846639466404664146642466434664446645466464664746648466494665046651466524665346654466554665646657466584665946660466614666246663466644666546666466674666846669466704667146672466734667446675466764667746678466794668046681466824668346684466854668646687466884668946690466914669246693466944669546696466974669846699467004670146702467034670446705467064670746708467094671046711467124671346714467154671646717467184671946720467214672246723467244672546726467274672846729467304673146732467334673446735467364673746738467394674046741467424674346744467454674646747467484674946750467514675246753467544675546756467574675846759467604676146762467634676446765467664676746768467694677046771467724677346774467754677646777467784677946780467814678246783467844678546786467874678846789467904679146792467934679446795467964679746798467994680046801468024680346804468054680646807468084680946810468114681246813468144681546816468174681846819468204682146822468234682446825468264682746828468294683046831468324683346834468354683646837468384683946840468414684246843468444684546846468474684846849468504685146852468534685446855468564685746858468594686046861468624686346864468654686646867468684686946870468714687246873468744687546876468774687846879468804688146882468834688446885468864688746888468894689046891468924689346894468954689646897468984689946900469014690246903469044690546906469074690846909469104691146912469134691446915469164691746918469194692046921469224692346924469254692646927469284692946930469314693246933469344693546936469374693846939469404694146942469434694446945469464694746948469494695046951469524695346954469554695646957469584695946960469614696246963469644696546966469674696846969469704697146972469734697446975469764697746978469794698046981469824698346984469854698646987469884698946990469914699246993469944699546996469974699846999470004700147002470034700447005470064700747008470094701047011470124701347014470154701647017470184701947020470214702247023470244702547026470274702847029470304703147032470334703447035470364703747038470394704047041470424704347044470454704647047470484704947050470514705247053470544705547056470574705847059470604706147062470634706447065470664706747068470694707047071470724707347074470754707647077470784707947080470814708247083470844708547086470874708847089470904709147092470934709447095470964709747098470994710047101471024710347104
  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() WC_DO_NOTHING
  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 HAVE_ARIA
  596. #include "wolfssl/wolfcrypt/port/aria/aria-crypt.h"
  597. void printOutput(const char *strName, unsigned char *data, unsigned int dataSz);
  598. WOLFSSL_TEST_SUBROUTINE int ariagcm_test(MC_ALGID);
  599. #endif
  600. #ifdef WOLF_CRYPTO_CB
  601. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cryptocb_test(void);
  602. #endif
  603. #ifdef WOLFSSL_CERT_PIV
  604. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t certpiv_test(void);
  605. #endif
  606. #ifdef WOLFSSL_AES_SIV
  607. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_siv_test(void);
  608. #endif
  609. /* General big buffer size for many tests. */
  610. #define FOURK_BUF 4096
  611. #define ERROR_OUT(err, eLabel) do { ret = (err); goto eLabel; } while (0)
  612. static void render_error_message(const char* msg, wc_test_ret_t es)
  613. {
  614. (void)msg;
  615. (void)es;
  616. #ifdef WOLFSSL_LINUXKM
  617. #define err_sys_printf lkm_printf
  618. #else
  619. #define err_sys_printf printf
  620. #endif
  621. switch (WC_TEST_RET_DEC_TAG(es)) {
  622. case WC_TEST_RET_TAG_NC:
  623. err_sys_printf("%s error L=%d\n", msg, WC_TEST_RET_DEC_LN(es));
  624. break;
  625. case WC_TEST_RET_TAG_EC:
  626. #ifdef NO_ERROR_STRINGS
  627. err_sys_printf("%s error L=%d code=%d\n", msg,
  628. WC_TEST_RET_DEC_LN(es), -WC_TEST_RET_DEC_I(es));
  629. #else
  630. err_sys_printf("%s error L=%d code=%d (%s)\n", msg,
  631. WC_TEST_RET_DEC_LN(es), -WC_TEST_RET_DEC_I(es),
  632. wc_GetErrorString(-WC_TEST_RET_DEC_I(es)));
  633. #endif
  634. break;
  635. case WC_TEST_RET_TAG_ERRNO:
  636. {
  637. /* strerror_r() comes in two mutually incompatible flavors, a native glibc
  638. * flavor that always returns a non-null char pointer that must be used
  639. * directly, and a POSIX flavor that returns an error int, and iff success,
  640. * stores an error string in the supplied buffer. this is all most
  641. * infelicitous...
  642. */
  643. #if !defined(STRING_USER) && !defined(NO_ERROR_STRINGS) && \
  644. (defined(__STDC_VERSION__) && (__STDC_VERSION__ > 199901L)) && \
  645. ((defined(__GLIBC__) && (__GLIBC__ >= 2)) || \
  646. (defined(__USE_XOPEN2K) && \
  647. defined(_POSIX_C_SOURCE) && \
  648. (_POSIX_C_SOURCE >= 200112L)))
  649. char errno_buf[64], *errno_string;
  650. /* precisely mirror the gate used in glibc string.h */
  651. #if defined __USE_XOPEN2K && !defined __USE_GNU
  652. if (strerror_r(WC_TEST_RET_DEC_I(es),
  653. errno_buf, sizeof(errno_buf)) != 0)
  654. XSTRLCPY(errno_buf, "?", sizeof(errno_buf));
  655. errno_string = errno_buf;
  656. #else
  657. errno_string = strerror_r(WC_TEST_RET_DEC_I(es),
  658. errno_buf, sizeof(errno_buf));
  659. #endif
  660. err_sys_printf("%s error L=%d errno=%d (%s)\n", msg,
  661. WC_TEST_RET_DEC_LN(es), WC_TEST_RET_DEC_I(es),
  662. errno_string);
  663. #else /* can't figure out how to strerror_r(), or don't want error strings */
  664. err_sys_printf("%s error L=%d errno=%d\n", msg,
  665. WC_TEST_RET_DEC_LN(es), WC_TEST_RET_DEC_I(es));
  666. #endif
  667. break;
  668. }
  669. case WC_TEST_RET_TAG_I:
  670. err_sys_printf("%s error L=%d i=%d\n", msg,
  671. WC_TEST_RET_DEC_LN(es), WC_TEST_RET_DEC_I(es));
  672. break;
  673. }
  674. #undef err_sys_printf
  675. }
  676. static void print_fiducials(void);
  677. #ifdef HAVE_STACK_SIZE
  678. static THREAD_RETURN err_sys(const char* msg, int es)
  679. #else
  680. static wc_test_ret_t err_sys(const char* msg, wc_test_ret_t es)
  681. #endif
  682. {
  683. render_error_message(msg, es);
  684. print_fiducials();
  685. #ifdef WOLFSSL_LINUXKM
  686. EXIT_TEST(es);
  687. #else
  688. EXIT_TEST(-1);
  689. #endif
  690. }
  691. #ifndef HAVE_WOLFCRYPT_TEST_OPTIONS
  692. /* func_args from test.h, so don't have to pull in other stuff */
  693. typedef struct func_args {
  694. int argc;
  695. char** argv;
  696. wc_test_ret_t return_code;
  697. } func_args;
  698. #endif /* !HAVE_WOLFCRYPT_TEST_OPTIONS */
  699. #if defined(HAVE_FIPS) && !defined(WOLFSSL_LINUXKM)
  700. static void myFipsCb(int ok, int err, const char* hash)
  701. {
  702. printf("in my Fips callback, ok = %d, err = %d\n", ok, err);
  703. printf("message = %s\n", wc_GetErrorString(err));
  704. printf("hash = %s\n", hash);
  705. if (err == IN_CORE_FIPS_E) {
  706. printf("In core integrity hash check failure, copy above hash\n");
  707. printf("into verifyCore[] in fips_test.c and rebuild\n");
  708. }
  709. }
  710. #endif /* HAVE_FIPS && !WOLFSSL_LINUXKM */
  711. #ifdef WOLFSSL_STATIC_MEMORY
  712. #if defined(WOLFSSL_STATIC_MEMORY_TEST_SZ)
  713. static byte gTestMemory[WOLFSSL_STATIC_MEMORY_TEST_SZ];
  714. #elif defined(BENCH_EMBEDDED)
  715. static byte gTestMemory[14000];
  716. #elif defined(WOLFSSL_CERT_EXT)
  717. static byte gTestMemory[140000];
  718. #elif (defined(WOLFSSL_SP_MATH_ALL) || defined(USE_FAST_MATH)) && \
  719. !defined(ALT_ECC_SIZE)
  720. static byte gTestMemory[160000];
  721. #else
  722. static byte gTestMemory[80000];
  723. #endif
  724. #endif
  725. #ifdef WOLFSSL_PB
  726. static int wolfssl_pb_print(const char* msg, ...)
  727. {
  728. int ret;
  729. va_list args;
  730. char tmpBuf[80];
  731. va_start(args, msg);
  732. ret = vsprint(tmpBuf, msg, args);
  733. va_end(args);
  734. fnDumpStringToSystemLog(tmpBuf);
  735. return ret;
  736. }
  737. #endif /* WOLFSSL_PB */
  738. #if defined(WOLF_CRYPTO_CB) && !defined(HAVE_HASHDRBG) && \
  739. !defined(WC_NO_RNG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  740. /* Enable support for RNG with crypto callback */
  741. static int rng_crypto_cb(int thisDevId, wc_CryptoInfo* info, void* ctx)
  742. {
  743. int rc = CRYPTOCB_UNAVAILABLE;
  744. if (info->algo_type == WC_ALGO_TYPE_RNG) {
  745. rc = wc_GenerateSeed(&info->rng.rng->seed, info->rng.out, info->rng.sz);
  746. }
  747. (void)ctx;
  748. (void)thisDevId;
  749. return rc;
  750. }
  751. #endif
  752. /* optional macro to add sleep between tests */
  753. #ifndef TEST_SLEEP
  754. #define TEST_SLEEP() WC_DO_NOTHING
  755. #else
  756. #define TEST_PASS test_pass
  757. #include <stdarg.h> /* for var args */
  758. static WC_INLINE void test_pass(const char* fmt, ...)
  759. {
  760. va_list args;
  761. va_start(args, fmt);
  762. STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK(max_relative_stack, vprintf(fmt, args));
  763. va_end(args);
  764. PRINT_HEAP_CHECKPOINT();
  765. TEST_SLEEP();
  766. ASSERT_RESTORED_VECTOR_REGISTERS(exit(1););
  767. }
  768. #endif
  769. /* set test pass output to printf if not overriden */
  770. #ifndef TEST_PASS
  771. /* redirect to printf */
  772. #define TEST_PASS(...) { \
  773. if (STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK \
  774. (max_relative_stack, printf(__VA_ARGS__)) < 0) { \
  775. return err_sys("post-test check failed", WC_TEST_RET_ENC_NC);\
  776. } \
  777. PRINT_HEAP_CHECKPOINT(); \
  778. ASSERT_RESTORED_VECTOR_REGISTERS(exit(1);); \
  779. }
  780. #endif
  781. #ifdef TEST_ALWAYS_RUN_TO_END
  782. #define TEST_FAIL(msg, retval) do { last_failed_test_ret = (retval); render_error_message(msg, retval); } while (0)
  783. #elif !defined(TEST_FAIL)
  784. #define TEST_FAIL(msg, retval) return err_sys(msg, retval)
  785. #endif
  786. #ifdef HAVE_STACK_SIZE
  787. THREAD_RETURN WOLFSSL_THREAD wolfcrypt_test(void* args)
  788. #else
  789. wc_test_ret_t wolfcrypt_test(void* args)
  790. #endif
  791. {
  792. wc_test_ret_t ret;
  793. #if defined(WOLFSSL_TRACK_MEMORY_VERBOSE) && !defined(WOLFSSL_STATIC_MEMORY)
  794. long heap_baselineAllocs, heap_baselineBytes;
  795. #endif
  796. #ifdef TEST_ALWAYS_RUN_TO_END
  797. int last_failed_test_ret = 0;
  798. #endif
  799. STACK_SIZE_INIT();
  800. #if defined(WOLFSSL_TRACK_MEMORY_VERBOSE) && !defined(WOLFSSL_STATIC_MEMORY)
  801. (void)wolfCrypt_heap_peakAllocs_checkpoint();
  802. heap_baselineAllocs = wolfCrypt_heap_peakAllocs_checkpoint();
  803. (void)wolfCrypt_heap_peakBytes_checkpoint();
  804. heap_baselineBytes = wolfCrypt_heap_peakBytes_checkpoint();
  805. #endif
  806. printf("------------------------------------------------------------------------------\n");
  807. printf(" wolfSSL version %s\n", LIBWOLFSSL_VERSION_STRING);
  808. printf("------------------------------------------------------------------------------\n");
  809. if (args) {
  810. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  811. int ch;
  812. #endif
  813. ((func_args*)args)->return_code = -1; /* error state */
  814. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  815. while ((ch = mygetopt(((func_args*)args)->argc, ((func_args*)args)->argv, "s:m:a:h")) != -1) {
  816. switch(ch) {
  817. case 's':
  818. #ifdef HAVE_STACK_SIZE_VERBOSE
  819. max_relative_stack = (ssize_t)atoi(myoptarg);
  820. break;
  821. #else
  822. return err_sys("-s (max relative stack bytes) requires HAVE_STACK_SIZE_VERBOSE (--enable-stacksize=verbose).", WC_TEST_RET_ENC_NC);
  823. #endif
  824. case 'm':
  825. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  826. max_relative_heap_bytes = (ssize_t)atoi(myoptarg);
  827. break;
  828. #else
  829. return err_sys("-m (max relative heap memory bytes) requires WOLFSSL_TRACK_MEMORY_VERBOSE (--enable-trackmemory=verbose).", WC_TEST_RET_ENC_NC);
  830. #endif
  831. case 'a':
  832. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  833. max_relative_heap_allocs = (ssize_t)atoi(myoptarg);
  834. break;
  835. #else
  836. return err_sys("-a (max relative heap allocs) requires WOLFSSL_TRACK_MEMORY_VERBOSE (--enable-trackmemory=verbose).", WC_TEST_RET_ENC_NC);
  837. #endif
  838. case 'h':
  839. return err_sys("\
  840. options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\
  841. [-a max_relative_heap_allocs] [-h]\n", 0);
  842. default:
  843. return err_sys("unknown test option. try -h.", WC_TEST_RET_ENC_NC);
  844. }
  845. }
  846. #endif
  847. }
  848. #ifdef WOLFSSL_STATIC_MEMORY
  849. if (wc_LoadStaticMemory(&HEAP_HINT, gTestMemory, sizeof(gTestMemory),
  850. WOLFMEM_GENERAL, 1) != 0) {
  851. printf("unable to load static memory.\n");
  852. return(EXIT_FAILURE);
  853. }
  854. #endif
  855. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
  856. wolfSSL_Debugging_ON();
  857. #endif
  858. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  859. wc_SetLoggingHeap(HEAP_HINT);
  860. #endif
  861. #if defined(HAVE_FIPS) && !defined(WOLFSSL_LINUXKM)
  862. wolfCrypt_SetCb_fips(myFipsCb);
  863. #endif
  864. #if !defined(NO_BIG_INT)
  865. if (CheckCtcSettings() != 1) {
  866. printf("Sizeof mismatch (build) %x != (run) %lx\n",
  867. CTC_SETTINGS, (unsigned long)CheckRunTimeSettings());
  868. return err_sys("Build vs runtime math mismatch\n", WC_TEST_RET_ENC_NC);
  869. }
  870. #if defined(USE_FAST_MATH) && \
  871. (!defined(NO_RSA) || !defined(NO_DH) || defined(HAVE_ECC))
  872. if (CheckFastMathSettings() != 1)
  873. return err_sys("Build vs runtime fastmath FP_MAX_BITS mismatch\n",
  874. WC_TEST_RET_ENC_NC);
  875. #endif /* USE_FAST_MATH */
  876. #endif /* !NO_BIG_INT */
  877. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  878. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  879. initDefaultName();
  880. #endif
  881. #ifdef WOLFSSL_ASYNC_CRYPT
  882. ret = wolfAsync_DevOpen(&devId);
  883. if (ret < 0) {
  884. printf("Async device open failed\nRunning without async\n");
  885. }
  886. #else
  887. (void)devId;
  888. #endif /* WOLFSSL_ASYNC_CRYPT */
  889. #ifdef WOLF_CRYPTO_CB
  890. #ifdef HAVE_INTEL_QA_SYNC
  891. devId = wc_CryptoCb_InitIntelQa();
  892. if (INVALID_DEVID == devId) {
  893. printf("Couldn't init the Intel QA\n");
  894. }
  895. #endif
  896. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  897. devId = wc_CryptoCb_InitOcteon();
  898. if (INVALID_DEVID == devId) {
  899. printf("Couldn't init the Cavium Octeon\n");
  900. }
  901. #endif
  902. #endif
  903. #if defined(WOLF_CRYPTO_CB) && !defined(HAVE_HASHDRBG) && \
  904. !defined(WC_NO_RNG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  905. if (devId == INVALID_DEVID) {
  906. /* for testing RNG with crypto callback register function */
  907. devId = 100; /* any value beside -2 (INVALID_DEVID) */
  908. wc_CryptoCb_RegisterDevice(devId, rng_crypto_cb, NULL);
  909. }
  910. #endif
  911. #ifdef HAVE_SELFTEST
  912. if ( (ret = wolfCrypt_SelfTest()) != 0)
  913. TEST_FAIL("CAVP selftest failed!\n", ret);
  914. else
  915. TEST_PASS("CAVP selftest passed!\n");
  916. #endif
  917. if ( (ret = error_test()) != 0)
  918. TEST_FAIL("error test failed!\n", ret);
  919. else
  920. TEST_PASS("error test passed!\n");
  921. if ( (ret = memory_test()) != 0)
  922. TEST_FAIL("MEMORY test failed!\n", ret);
  923. else
  924. TEST_PASS("MEMORY test passed!\n");
  925. #ifndef NO_CODING
  926. if ( (ret = base64_test()) != 0)
  927. TEST_FAIL("base64 test failed!\n", ret);
  928. else
  929. TEST_PASS("base64 test passed!\n");
  930. #ifdef WOLFSSL_BASE16
  931. if ( (ret = base16_test()) != 0)
  932. TEST_FAIL("base16 test failed!\n", ret);
  933. else
  934. TEST_PASS("base16 test passed!\n");
  935. #endif
  936. #endif /* !NO_CODING */
  937. #ifndef NO_ASN
  938. if ( (ret = asn_test()) != 0)
  939. TEST_FAIL("asn test failed!\n", ret);
  940. else
  941. TEST_PASS("asn test passed!\n");
  942. #endif
  943. #ifndef WC_NO_RNG
  944. if ( (ret = random_test()) != 0)
  945. TEST_FAIL("RANDOM test failed!\n", ret);
  946. else
  947. TEST_PASS("RANDOM test passed!\n");
  948. #endif /* WC_NO_RNG */
  949. #ifndef NO_MD5
  950. if ( (ret = md5_test()) != 0)
  951. TEST_FAIL("MD5 test failed!\n", ret);
  952. else
  953. TEST_PASS("MD5 test passed!\n");
  954. #endif
  955. #ifdef WOLFSSL_MD2
  956. if ( (ret = md2_test()) != 0)
  957. TEST_FAIL("MD2 test failed!\n", ret);
  958. else
  959. TEST_PASS("MD2 test passed!\n");
  960. #endif
  961. #ifndef NO_MD4
  962. if ( (ret = md4_test()) != 0)
  963. TEST_FAIL("MD4 test failed!\n", ret);
  964. else
  965. TEST_PASS("MD4 test passed!\n");
  966. #endif
  967. #ifndef NO_SHA
  968. if ( (ret = sha_test()) != 0)
  969. TEST_FAIL("SHA test failed!\n", ret);
  970. else
  971. TEST_PASS("SHA test passed!\n");
  972. #endif
  973. #ifdef WOLFSSL_SHA224
  974. if ( (ret = sha224_test()) != 0)
  975. TEST_FAIL("SHA-224 test failed!\n", ret);
  976. else
  977. TEST_PASS("SHA-224 test passed!\n");
  978. #endif
  979. #ifndef NO_SHA256
  980. if ( (ret = sha256_test()) != 0)
  981. TEST_FAIL("SHA-256 test failed!\n", ret);
  982. else
  983. TEST_PASS("SHA-256 test passed!\n");
  984. #endif
  985. #ifdef WOLFSSL_SHA384
  986. if ( (ret = sha384_test()) != 0)
  987. TEST_FAIL("SHA-384 test failed!\n", ret);
  988. else
  989. TEST_PASS("SHA-384 test passed!\n");
  990. #endif
  991. #ifdef WOLFSSL_SHA512
  992. if ((ret = sha512_test()) != 0) {
  993. TEST_FAIL("SHA-512 test failed!\n", ret);
  994. }
  995. else {
  996. TEST_PASS("SHA-512 test passed!\n");
  997. }
  998. #if !defined(WOLFSSL_NOSHA512_224) && \
  999. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  1000. if ((ret = sha512_224_test()) != 0) {
  1001. TEST_FAIL("SHA-512/224 test failed!\n", ret);
  1002. }
  1003. else
  1004. TEST_PASS("SHA-512/224 test passed!\n");
  1005. #endif /* !defined(WOLFSSL_NOSHA512_224) && !FIPS ... */
  1006. #if !defined(WOLFSSL_NOSHA512_256) && \
  1007. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  1008. if ((ret = sha512_256_test()) != 0) {
  1009. TEST_FAIL("SHA-512/256 test failed!\n", ret);
  1010. }
  1011. else
  1012. TEST_PASS("SHA-512/256 test passed!\n");
  1013. #endif /* !defined(WOLFSSL_NOSHA512_256) & !FIPS ... */
  1014. #endif /* WOLFSSL_SHA512 */
  1015. #ifdef WOLFSSL_SHA3
  1016. if ( (ret = sha3_test()) != 0)
  1017. TEST_FAIL("SHA-3 test failed!\n", ret);
  1018. else
  1019. TEST_PASS("SHA-3 test passed!\n");
  1020. #endif
  1021. #ifdef WOLFSSL_SHAKE128
  1022. if ( (ret = shake128_test()) != 0)
  1023. TEST_FAIL("SHAKE128 test failed!\n", ret);
  1024. else
  1025. TEST_PASS("SHAKE128 test passed!\n");
  1026. #endif
  1027. #ifdef WOLFSSL_SHAKE256
  1028. if ( (ret = shake256_test()) != 0)
  1029. TEST_FAIL("SHAKE256 test failed!\n", ret);
  1030. else
  1031. TEST_PASS("SHAKE256 test passed!\n");
  1032. #endif
  1033. #ifdef WOLFSSL_SM3
  1034. if ( (ret = sm3_test()) != 0)
  1035. return err_sys("SM-3 test failed!\n", ret);
  1036. else
  1037. TEST_PASS("SM-3 test passed!\n");
  1038. #endif
  1039. #ifndef NO_HASH_WRAPPER
  1040. if ( (ret = hash_test()) != 0)
  1041. TEST_FAIL("Hash test failed!\n", ret);
  1042. else
  1043. TEST_PASS("Hash test passed!\n");
  1044. #endif
  1045. #ifdef WOLFSSL_RIPEMD
  1046. if ( (ret = ripemd_test()) != 0)
  1047. TEST_FAIL("RIPEMD test failed!\n", ret);
  1048. else
  1049. TEST_PASS("RIPEMD test passed!\n");
  1050. #endif
  1051. #ifdef HAVE_BLAKE2
  1052. if ( (ret = blake2b_test()) != 0)
  1053. TEST_FAIL("BLAKE2b test failed!\n", ret);
  1054. else
  1055. TEST_PASS("BLAKE2b test passed!\n");
  1056. #endif
  1057. #ifdef HAVE_BLAKE2S
  1058. if ( (ret = blake2s_test()) != 0)
  1059. TEST_FAIL("BLAKE2s test failed!\n", ret);
  1060. else
  1061. TEST_PASS("BLAKE2s test passed!\n");
  1062. #endif
  1063. #ifndef NO_HMAC
  1064. #if !defined(NO_MD5) && !(defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) \
  1065. && (HAVE_FIPS_VERSION >= 5))
  1066. if ( (ret = hmac_md5_test()) != 0)
  1067. TEST_FAIL("HMAC-MD5 test failed!\n", ret);
  1068. else
  1069. TEST_PASS("HMAC-MD5 test passed!\n");
  1070. #endif
  1071. #ifndef NO_SHA
  1072. if ( (ret = hmac_sha_test()) != 0)
  1073. TEST_FAIL("HMAC-SHA test failed!\n", ret);
  1074. else
  1075. TEST_PASS("HMAC-SHA test passed!\n");
  1076. #endif
  1077. #ifdef WOLFSSL_SHA224
  1078. if ( (ret = hmac_sha224_test()) != 0)
  1079. TEST_FAIL("HMAC-SHA224 test failed!\n", ret);
  1080. else
  1081. TEST_PASS("HMAC-SHA224 test passed!\n");
  1082. #endif
  1083. #ifndef NO_SHA256
  1084. if ( (ret = hmac_sha256_test()) != 0)
  1085. TEST_FAIL("HMAC-SHA256 test failed!\n", ret);
  1086. else
  1087. TEST_PASS("HMAC-SHA256 test passed!\n");
  1088. #endif
  1089. #ifdef WOLFSSL_SHA384
  1090. if ( (ret = hmac_sha384_test()) != 0)
  1091. TEST_FAIL("HMAC-SHA384 test failed!\n", ret);
  1092. else
  1093. TEST_PASS("HMAC-SHA384 test passed!\n");
  1094. #endif
  1095. #ifdef WOLFSSL_SHA512
  1096. if ( (ret = hmac_sha512_test()) != 0)
  1097. TEST_FAIL("HMAC-SHA512 test failed!\n", ret);
  1098. else
  1099. TEST_PASS("HMAC-SHA512 test passed!\n");
  1100. #endif
  1101. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  1102. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  1103. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  1104. if ( (ret = hmac_sha3_test()) != 0)
  1105. TEST_FAIL("HMAC-SHA3 test failed!\n", ret);
  1106. else
  1107. TEST_PASS("HMAC-SHA3 test passed!\n");
  1108. #endif
  1109. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  1110. PRIVATE_KEY_UNLOCK();
  1111. if ( (ret = hkdf_test()) != 0)
  1112. TEST_FAIL("HMAC-KDF test failed!\n", ret);
  1113. else
  1114. TEST_PASS("HMAC-KDF test passed!\n");
  1115. PRIVATE_KEY_LOCK();
  1116. #endif
  1117. #endif /* !NO_HMAC */
  1118. #ifdef WOLFSSL_WOLFSSH
  1119. PRIVATE_KEY_UNLOCK();
  1120. if ( (ret = sshkdf_test()) != 0)
  1121. TEST_FAIL("SSH-KDF test failed!\n", ret);
  1122. else
  1123. TEST_PASS("SSH-KDF test passed!\n");
  1124. PRIVATE_KEY_LOCK();
  1125. #endif /* WOLFSSL_WOLFSSH */
  1126. #ifdef WOLFSSL_TLS13
  1127. PRIVATE_KEY_UNLOCK();
  1128. if ( (ret = tls13_kdf_test()) != 0)
  1129. TEST_FAIL("TLSv1.3 KDF test failed!\n", ret);
  1130. else
  1131. TEST_PASS("TLSv1.3 KDF test passed!\n");
  1132. PRIVATE_KEY_LOCK();
  1133. #endif /* WOLFSSL_TLS13 */
  1134. #if defined(HAVE_X963_KDF) && defined(HAVE_ECC)
  1135. if ( (ret = x963kdf_test()) != 0)
  1136. TEST_FAIL("X963-KDF test failed!\n", ret);
  1137. else
  1138. TEST_PASS("X963-KDF test passed!\n");
  1139. #endif
  1140. #if defined(HAVE_HPKE) && defined(HAVE_ECC) && defined(HAVE_AESGCM)
  1141. if ( (ret = hpke_test()) != 0)
  1142. TEST_FAIL("HPKE test failed!\n", ret);
  1143. else
  1144. TEST_PASS("HPKE test passed!\n");
  1145. #endif
  1146. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128) && \
  1147. !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  1148. if ( (ret = gmac_test()) != 0)
  1149. TEST_FAIL("GMAC test failed!\n", ret);
  1150. else
  1151. TEST_PASS("GMAC test passed!\n");
  1152. #endif
  1153. #ifdef WC_RC2
  1154. if ( (ret = rc2_test()) != 0)
  1155. TEST_FAIL("RC2 test failed!\n", ret);
  1156. else
  1157. TEST_PASS("RC2 test passed!\n");
  1158. #endif
  1159. #ifndef NO_RC4
  1160. if ( (ret = arc4_test()) != 0)
  1161. TEST_FAIL("ARC4 test failed!\n", ret);
  1162. else
  1163. TEST_PASS("ARC4 test passed!\n");
  1164. #endif
  1165. #ifdef HAVE_CHACHA
  1166. if ( (ret = chacha_test()) != 0)
  1167. TEST_FAIL("Chacha test failed!\n", ret);
  1168. else
  1169. TEST_PASS("Chacha test passed!\n");
  1170. #endif
  1171. #ifdef HAVE_XCHACHA
  1172. if ( (ret = XChaCha_test()) != 0)
  1173. TEST_FAIL("XChacha test failed!\n", ret);
  1174. else
  1175. TEST_PASS("XChacha test passed!\n");
  1176. #endif
  1177. #ifdef HAVE_POLY1305
  1178. if ( (ret = poly1305_test()) != 0)
  1179. TEST_FAIL("POLY1305 test failed!\n", ret);
  1180. else
  1181. TEST_PASS("POLY1305 test passed!\n");
  1182. #endif
  1183. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  1184. if ( (ret = chacha20_poly1305_aead_test()) != 0)
  1185. TEST_FAIL("ChaCha20-Poly1305 AEAD test failed!\n", ret);
  1186. else
  1187. TEST_PASS("ChaCha20-Poly1305 AEAD test passed!\n");
  1188. #endif
  1189. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  1190. if ( (ret = XChaCha20Poly1305_test()) != 0)
  1191. TEST_FAIL("XChaCha20-Poly1305 AEAD test failed!\n", ret);
  1192. else
  1193. TEST_PASS("XChaCha20-Poly1305 AEAD test passed!\n");
  1194. #endif
  1195. #ifndef NO_DES3
  1196. if ( (ret = des_test()) != 0)
  1197. TEST_FAIL("DES test failed!\n", ret);
  1198. else
  1199. TEST_PASS("DES test passed!\n");
  1200. #endif
  1201. #ifndef NO_DES3
  1202. if ( (ret = des3_test()) != 0)
  1203. TEST_FAIL("DES3 test failed!\n", ret);
  1204. else
  1205. TEST_PASS("DES3 test passed!\n");
  1206. #endif
  1207. #ifndef NO_AES
  1208. if ( (ret = aes_test()) != 0)
  1209. TEST_FAIL("AES test failed!\n", ret);
  1210. else
  1211. TEST_PASS("AES test passed!\n");
  1212. #ifdef WOLFSSL_AES_192
  1213. if ( (ret = aes192_test()) != 0)
  1214. TEST_FAIL("AES192 test failed!\n", ret);
  1215. else
  1216. TEST_PASS("AES192 test passed!\n");
  1217. #endif
  1218. #ifdef WOLFSSL_AES_256
  1219. if ( (ret = aes256_test()) != 0)
  1220. TEST_FAIL("AES256 test failed!\n", ret);
  1221. else
  1222. TEST_PASS("AES256 test passed!\n");
  1223. #endif
  1224. #ifdef WOLFSSL_AES_OFB
  1225. if ( (ret = aesofb_test()) != 0)
  1226. TEST_FAIL("AES-OFB test failed!\n", ret);
  1227. else
  1228. TEST_PASS("AESOFB test passed!\n");
  1229. #endif
  1230. #ifdef HAVE_AESGCM
  1231. #if !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO)
  1232. if ( (ret = aesgcm_test()) != 0)
  1233. TEST_FAIL("AES-GCM test failed!\n", ret);
  1234. #endif
  1235. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT) && \
  1236. !defined(WOLFSSL_KCAPI_AES) && !(defined(WOLF_CRYPTO_CB) && \
  1237. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  1238. if ((ret = aesgcm_default_test()) != 0) {
  1239. TEST_FAIL("AES-GCM test failed!\n", ret);
  1240. }
  1241. #endif
  1242. if (ret == 0) {
  1243. TEST_PASS("AES-GCM test passed!\n");
  1244. }
  1245. #endif
  1246. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  1247. if ( (ret = aesccm_test()) != 0)
  1248. TEST_FAIL("AES-CCM test failed!\n", ret);
  1249. else
  1250. TEST_PASS("AES-CCM test passed!\n");
  1251. #endif
  1252. #ifdef HAVE_AES_KEYWRAP
  1253. if ( (ret = aeskeywrap_test()) != 0)
  1254. TEST_FAIL("AES Key Wrap test failed!\n", ret);
  1255. else
  1256. TEST_PASS("AES Key Wrap test passed!\n");
  1257. #endif
  1258. #ifdef WOLFSSL_AES_SIV
  1259. if ( (ret = aes_siv_test()) != 0)
  1260. TEST_FAIL("AES-SIV test failed!\n", ret);
  1261. else
  1262. TEST_PASS("AES-SIV test passed!\n");
  1263. #endif
  1264. #endif
  1265. #ifdef HAVE_ARIA
  1266. if ( (ret = ariagcm_test(MC_ALGID_ARIA_128BITKEY)) != 0)
  1267. TEST_FAIL("ARIA128 test failed!\n", ret);
  1268. else
  1269. TEST_PASS("ARIA128 test passed!\n");
  1270. if ( (ret = ariagcm_test(MC_ALGID_ARIA_192BITKEY)) != 0)
  1271. TEST_FAIL("ARIA192 test failed!\n", ret);
  1272. else
  1273. TEST_PASS("ARIA192 test passed!\n");
  1274. if ( (ret = ariagcm_test(MC_ALGID_ARIA_256BITKEY)) != 0)
  1275. TEST_FAIL("ARIA256 test failed!\n", ret);
  1276. else
  1277. TEST_PASS("ARIA256 test passed!\n");
  1278. #endif
  1279. #ifdef HAVE_CAMELLIA
  1280. if ( (ret = camellia_test()) != 0)
  1281. TEST_FAIL("CAMELLIA test failed!\n", ret);
  1282. else
  1283. TEST_PASS("CAMELLIA test passed!\n");
  1284. #endif
  1285. #ifdef WOLFSSL_SM4
  1286. if ( (ret = sm4_test()) != 0)
  1287. return err_sys("SM-4 test failed!\n", ret);
  1288. else
  1289. TEST_PASS("SM-4 test passed!\n");
  1290. #endif
  1291. #if !defined(NO_RSA)
  1292. #ifdef WC_RSA_NO_PADDING
  1293. if ( (ret = rsa_no_pad_test()) != 0)
  1294. TEST_FAIL("RSA NOPAD test failed!\n", ret);
  1295. else
  1296. TEST_PASS("RSA NOPAD test passed!\n");
  1297. #endif
  1298. if ( (ret = rsa_test()) != 0)
  1299. TEST_FAIL("RSA test failed!\n", ret);
  1300. else
  1301. TEST_PASS("RSA test passed!\n");
  1302. #endif
  1303. #ifndef NO_DH
  1304. PRIVATE_KEY_UNLOCK();
  1305. if ( (ret = dh_test()) != 0)
  1306. TEST_FAIL("DH test failed!\n", ret);
  1307. else
  1308. TEST_PASS("DH test passed!\n");
  1309. PRIVATE_KEY_LOCK();
  1310. #endif
  1311. #ifndef NO_DSA
  1312. if ( (ret = dsa_test()) != 0)
  1313. TEST_FAIL("DSA test failed!\n", ret);
  1314. else
  1315. TEST_PASS("DSA test passed!\n");
  1316. #endif
  1317. #ifdef WOLFCRYPT_HAVE_SRP
  1318. if ( (ret = srp_test()) != 0)
  1319. TEST_FAIL("SRP test failed!\n", ret);
  1320. else
  1321. TEST_PASS("SRP test passed!\n");
  1322. #endif
  1323. #ifndef NO_PWDBASED
  1324. if ( (ret = pwdbased_test()) != 0)
  1325. TEST_FAIL("PWDBASED test failed!\n", ret);
  1326. else
  1327. TEST_PASS("PWDBASED test passed!\n");
  1328. #endif
  1329. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  1330. if ( (ret = openssl_test()) != 0)
  1331. TEST_FAIL("OPENSSL test failed!\n", ret);
  1332. else
  1333. TEST_PASS("OPENSSL test passed!\n");
  1334. if ( (ret = openSSL_evpMD_test()) != 0)
  1335. TEST_FAIL("OPENSSL (EVP MD) test failed!\n", ret);
  1336. else
  1337. TEST_PASS("OPENSSL (EVP MD) passed!\n");
  1338. if ( (ret = openssl_pkey0_test()) != 0)
  1339. TEST_FAIL("OPENSSL (PKEY0) test failed!\n", ret);
  1340. else
  1341. TEST_PASS("OPENSSL (PKEY0) passed!\n");
  1342. if ( (ret = openssl_pkey1_test()) != 0)
  1343. TEST_FAIL("OPENSSL (PKEY1) test failed!\n", ret);
  1344. else
  1345. TEST_PASS("OPENSSL (PKEY1) passed!\n");
  1346. #if !defined(WOLF_CRYPTO_CB_ONLY_RSA) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  1347. if ( (ret = openssl_evpSig_test()) != 0)
  1348. TEST_FAIL("OPENSSL (EVP Sign/Verify) test failed!\n", ret);
  1349. else
  1350. TEST_PASS("OPENSSL (EVP Sign/Verify) passed!\n");
  1351. #endif
  1352. #endif
  1353. #if defined(HAVE_ECC)
  1354. PRIVATE_KEY_UNLOCK();
  1355. if ( (ret = ecc_test()) != 0)
  1356. TEST_FAIL("ECC test failed!\n", ret);
  1357. else
  1358. TEST_PASS("ECC test passed!\n");
  1359. PRIVATE_KEY_LOCK();
  1360. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  1361. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  1362. if ( (ret = ecc_encrypt_test()) != 0)
  1363. TEST_FAIL("ECC Enc test failed!\n", ret);
  1364. else
  1365. TEST_PASS("ECC Enc test passed!\n");
  1366. #endif
  1367. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  1368. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  1369. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  1370. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ECC_SECP)
  1371. /* skip for ATECC508/608A, cannot import private key buffers */
  1372. if ( (ret = ecc_test_buffers()) != 0)
  1373. TEST_FAIL("ECC buffer test failed!\n", ret);
  1374. else
  1375. TEST_PASS("ECC buffer test passed!\n");
  1376. #endif
  1377. #endif
  1378. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  1379. !defined(NO_FILESYSTEM)
  1380. if ( (ret = cert_test()) != 0)
  1381. TEST_FAIL("CERT test failed!\n", ret);
  1382. else
  1383. TEST_PASS("CERT test passed!\n");
  1384. #endif
  1385. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  1386. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(WOLFSSL_GEN_CERT)
  1387. if ( (ret = certext_test()) != 0)
  1388. TEST_FAIL("CERT EXT test failed!\n", ret);
  1389. else
  1390. TEST_PASS("CERT EXT test passed!\n");
  1391. #endif
  1392. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  1393. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  1394. if ( (ret = decodedCertCache_test()) != 0)
  1395. TEST_FAIL("DECODED CERT CACHE test failed!\n", ret);
  1396. else
  1397. TEST_PASS("DECODED CERT CACHE test passed!\n");
  1398. #endif
  1399. #ifdef HAVE_CURVE25519
  1400. if ( (ret = curve25519_test()) != 0)
  1401. TEST_FAIL("CURVE25519 test failed!\n", ret);
  1402. else
  1403. TEST_PASS("CURVE25519 test passed!\n");
  1404. #endif
  1405. #ifdef HAVE_ED25519
  1406. if ( (ret = ed25519_test()) != 0)
  1407. TEST_FAIL("ED25519 test failed!\n", ret);
  1408. else
  1409. TEST_PASS("ED25519 test passed!\n");
  1410. #endif
  1411. #ifdef HAVE_CURVE448
  1412. if ( (ret = curve448_test()) != 0)
  1413. TEST_FAIL("CURVE448 test failed!\n", ret);
  1414. else
  1415. TEST_PASS("CURVE448 test passed!\n");
  1416. #endif
  1417. #ifdef HAVE_ED448
  1418. if ( (ret = ed448_test()) != 0)
  1419. TEST_FAIL("ED448 test failed!\n", ret);
  1420. else
  1421. TEST_PASS("ED448 test passed!\n");
  1422. #endif
  1423. #ifdef WOLFSSL_HAVE_KYBER
  1424. if ( (ret = kyber_test()) != 0)
  1425. TEST_FAIL("KYBER test failed!\n", ret);
  1426. else
  1427. TEST_PASS("KYBER test passed!\n");
  1428. #endif
  1429. #ifdef WOLFCRYPT_HAVE_ECCSI
  1430. if ( (ret = eccsi_test()) != 0)
  1431. TEST_FAIL("ECCSI test failed!\n", ret);
  1432. else
  1433. TEST_PASS("ECCSI test passed!\n");
  1434. #endif
  1435. #ifdef WOLFCRYPT_HAVE_SAKKE
  1436. if ( (ret = sakke_test()) != 0)
  1437. TEST_FAIL("SAKKE test failed!\n", ret);
  1438. else
  1439. TEST_PASS("SAKKE test passed!\n");
  1440. #endif
  1441. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  1442. if ( (ret = cmac_test()) != 0)
  1443. TEST_FAIL("CMAC test failed!\n", ret);
  1444. else
  1445. TEST_PASS("CMAC test passed!\n");
  1446. #endif
  1447. #if defined(WOLFSSL_SIPHASH)
  1448. if ( (ret = siphash_test()) != 0)
  1449. TEST_FAIL("SipHash test failed!\n", ret);
  1450. else
  1451. TEST_PASS("SipHash test passed!\n");
  1452. #endif
  1453. #ifdef HAVE_LIBZ
  1454. if ( (ret = compress_test()) != 0)
  1455. TEST_FAIL("COMPRESS test failed!\n", ret);
  1456. else
  1457. TEST_PASS("COMPRESS test passed!\n");
  1458. #endif
  1459. #ifdef HAVE_PKCS7
  1460. #ifndef NO_PKCS7_ENCRYPTED_DATA
  1461. if ( (ret = pkcs7encrypted_test()) != 0)
  1462. TEST_FAIL("PKCS7encrypted test failed!\n", ret);
  1463. else
  1464. TEST_PASS("PKCS7encrypted test passed!\n");
  1465. #endif
  1466. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  1467. if ( (ret = pkcs7compressed_test()) != 0)
  1468. TEST_FAIL("PKCS7compressed test failed!\n", ret);
  1469. else
  1470. TEST_PASS("PKCS7compressed test passed!\n");
  1471. #endif
  1472. if ( (ret = pkcs7signed_test()) != 0)
  1473. TEST_FAIL("PKCS7signed test failed!\n", ret);
  1474. else
  1475. TEST_PASS("PKCS7signed test passed!\n");
  1476. if ( (ret = pkcs7enveloped_test()) != 0)
  1477. TEST_FAIL("PKCS7enveloped test failed!\n", ret);
  1478. else
  1479. TEST_PASS("PKCS7enveloped test passed!\n");
  1480. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  1481. if ( (ret = pkcs7authenveloped_test()) != 0)
  1482. TEST_FAIL("PKCS7authenveloped test failed!\n", ret);
  1483. else
  1484. TEST_PASS("PKCS7authenveloped test passed!\n");
  1485. #endif
  1486. #endif
  1487. #if defined(WOLFSSL_PUBLIC_MP) && \
  1488. ((defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  1489. defined(USE_FAST_MATH))
  1490. if ( (ret = mp_test()) != 0)
  1491. TEST_FAIL("mp test failed!\n", ret);
  1492. else
  1493. TEST_PASS("mp test passed!\n");
  1494. #endif
  1495. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  1496. if ( (ret = prime_test()) != 0)
  1497. TEST_FAIL("prime test failed!\n", ret);
  1498. else
  1499. TEST_PASS("prime test passed!\n");
  1500. #endif
  1501. #if defined(ASN_BER_TO_DER) && \
  1502. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  1503. defined(OPENSSL_EXTRA_X509_SMALL))
  1504. if ( (ret = berder_test()) != 0)
  1505. TEST_FAIL("ber-der test failed!\n", ret);
  1506. else
  1507. TEST_PASS("ber-der test passed!\n");
  1508. #endif
  1509. if ( (ret = logging_test()) != 0)
  1510. TEST_FAIL("logging test failed!\n", ret);
  1511. else
  1512. TEST_PASS("logging test passed!\n");
  1513. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  1514. if ( (ret = time_test()) != 0)
  1515. TEST_FAIL("time test failed!\n", ret);
  1516. else
  1517. TEST_PASS("time test passed!\n");
  1518. #endif
  1519. #if defined(__INCLUDE_NUTTX_CONFIG_H)
  1520. if ((ret = wolfcrypt_mutex_test()) != 0)
  1521. #else
  1522. if ((ret = mutex_test()) != 0)
  1523. #endif
  1524. TEST_FAIL("mutex test failed!\n", ret);
  1525. else
  1526. TEST_PASS("mutex test passed!\n");
  1527. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  1528. if ( (ret = memcb_test()) != 0)
  1529. TEST_FAIL("memcb test failed!\n", ret);
  1530. else
  1531. TEST_PASS("memcb test passed!\n");
  1532. #endif
  1533. #ifdef WOLFSSL_CAAM_BLOB
  1534. if ( (ret = blob_test()) != 0)
  1535. TEST_FAIL("blob test failed!\n", ret);
  1536. else
  1537. TEST_PASS("blob test passed!\n");
  1538. #endif
  1539. #if defined(WOLF_CRYPTO_CB) && \
  1540. !(defined(HAVE_INTEL_QAT_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC) || \
  1541. defined(WOLFSSL_QNX_CAAM))
  1542. if ( (ret = cryptocb_test()) != 0)
  1543. TEST_FAIL("crypto callback test failed!\n", ret);
  1544. else
  1545. TEST_PASS("crypto callback test passed!\n");
  1546. #endif
  1547. #ifdef WOLFSSL_CERT_PIV
  1548. if ( (ret = certpiv_test()) != 0)
  1549. TEST_FAIL("cert piv test failed!\n", ret);
  1550. else
  1551. TEST_PASS("cert piv test passed!\n");
  1552. #endif
  1553. #ifdef WOLF_CRYPTO_CB
  1554. #ifdef HAVE_INTEL_QA_SYNC
  1555. wc_CryptoCb_CleanupIntelQa(&devId);
  1556. #endif
  1557. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  1558. wc_CryptoCb_CleanupOcteon(&devId);
  1559. #endif
  1560. #endif
  1561. #ifdef WOLFSSL_ASYNC_CRYPT
  1562. wolfAsync_DevClose(&devId);
  1563. #endif
  1564. /* cleanup the thread if fixed point cache is enabled and have thread local */
  1565. #if defined(HAVE_THREAD_LS) && defined(HAVE_ECC) && defined(FP_ECC)
  1566. wc_ecc_fp_free();
  1567. #endif
  1568. #ifdef TEST_ALWAYS_RUN_TO_END
  1569. if (last_failed_test_ret != 0)
  1570. ret = last_failed_test_ret;
  1571. #endif
  1572. if (args)
  1573. ((func_args*)args)->return_code = ret;
  1574. TEST_PASS("Test complete\n");
  1575. EXIT_TEST(ret);
  1576. }
  1577. #ifndef NO_MAIN_DRIVER
  1578. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  1579. int myoptind = 0;
  1580. char* myoptarg = NULL;
  1581. #endif
  1582. /* so overall tests can pull in test function */
  1583. #if defined(WOLFSSL_ESPIDF) || defined(_WIN32_WCE)
  1584. wc_test_ret_t wolf_test_task(void)
  1585. #else
  1586. #ifndef NO_MAIN_FUNCTION
  1587. int main(int argc, char** argv)
  1588. {
  1589. return (int)wolfcrypt_test_main(argc, argv);
  1590. }
  1591. #endif
  1592. wc_test_ret_t wolfcrypt_test_main(int argc, char** argv)
  1593. #endif
  1594. {
  1595. wc_test_ret_t ret;
  1596. func_args args;
  1597. #if defined(WOLFSSL_ESPIDF) || defined(WOLFSSL_SE050)
  1598. /* set dummy wallclock time. */
  1599. struct timeval utctime;
  1600. struct timezone tz;
  1601. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1602. utctime.tv_usec = 0;
  1603. tz.tz_minuteswest = 0;
  1604. tz.tz_dsttime = 0;
  1605. settimeofday(&utctime, &tz);
  1606. #endif
  1607. #ifdef WOLFSSL_APACHE_MYNEWT
  1608. #ifdef ARCH_sim
  1609. mcu_sim_parse_args(argc, argv);
  1610. #endif
  1611. sysinit();
  1612. /* set dummy wallclock time. */
  1613. struct os_timeval utctime;
  1614. struct os_timezone tz;
  1615. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1616. utctime.tv_usec = 0;
  1617. tz.tz_minuteswest = 0;
  1618. tz.tz_dsttime = 0;
  1619. os_settimeofday(&utctime, &tz);
  1620. #endif
  1621. #ifdef WOLFSSL_ZEPHYR
  1622. /* set dummy wallclock time. */
  1623. struct timespec utctime;
  1624. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1625. utctime.tv_nsec = 0;
  1626. clock_settime(CLOCK_REALTIME, &utctime);
  1627. #endif
  1628. #ifdef DEVKITPRO
  1629. void *framebuffer;
  1630. GXRModeObj *rmode = NULL;
  1631. VIDEO_Init();
  1632. WPAD_Init();
  1633. rmode = VIDEO_GetPreferredMode(NULL);
  1634. #pragma GCC diagnostic ignored "-Wbad-function-cast"
  1635. framebuffer = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
  1636. #pragma GCC diagnostic pop
  1637. console_init(framebuffer,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
  1638. VIDEO_Configure(rmode);
  1639. VIDEO_SetNextFramebuffer(framebuffer);
  1640. VIDEO_SetBlack(FALSE);
  1641. VIDEO_Flush();
  1642. VIDEO_WaitVSync();
  1643. if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
  1644. #endif
  1645. #ifdef HAVE_WNR
  1646. if ((ret = wc_InitNetRandom(wnrConfigFile, NULL, 5000)) != 0) {
  1647. err_sys("Whitewood netRandom global config failed",
  1648. WC_TEST_RET_ENC_EC(ret));
  1649. return WC_TEST_RET_ENC_EC(ret);
  1650. }
  1651. #endif
  1652. #ifndef WOLFSSL_ESPIDF
  1653. args.argc = argc;
  1654. args.argv = argv;
  1655. #endif
  1656. if ((ret = wolfCrypt_Init()) != 0) {
  1657. printf("wolfCrypt_Init failed %d\n", (int)ret);
  1658. err_sys("Error with wolfCrypt_Init!\n", WC_TEST_RET_ENC_EC(ret));
  1659. }
  1660. #ifdef HAVE_WC_INTROSPECTION
  1661. printf("Math: %s\n", wc_GetMathInfo());
  1662. #endif
  1663. #ifdef WC_RNG_SEED_CB
  1664. wc_SetSeed_Cb(wc_GenerateSeed);
  1665. #endif
  1666. #ifdef HAVE_STACK_SIZE
  1667. StackSizeCheck(&args, wolfcrypt_test);
  1668. #else
  1669. wolfcrypt_test(&args);
  1670. #endif
  1671. if ((ret = wolfCrypt_Cleanup()) != 0) {
  1672. printf("wolfCrypt_Cleanup failed %d\n", (int)ret);
  1673. err_sys("Error with wolfCrypt_Cleanup!\n", WC_TEST_RET_ENC_EC(ret));
  1674. }
  1675. #ifdef HAVE_WNR
  1676. if ((ret = wc_FreeNetRandom()) < 0)
  1677. err_sys("Failed to free netRandom context",
  1678. WC_TEST_RET_ENC_EC(ret));
  1679. #endif /* HAVE_WNR */
  1680. #ifdef DOLPHIN_EMULATOR
  1681. /* Returning from main panics the emulator. Just hang
  1682. * and let the user force quit the emulator window. */
  1683. printf("args.return_code: %d\n", args.return_code);
  1684. printf("Testing complete. You may close the window now\n");
  1685. while (1);
  1686. #endif
  1687. #ifdef WOLFSSL_ESPIDF
  1688. /* ESP_LOGI to print takes up a lot less memory than printf */
  1689. ESP_LOGI("wolfcrypt_test", "Exiting main with return code: % d\n", args.return_code);
  1690. #endif
  1691. /* everything else will use printf */
  1692. #if !defined(WOLFSSL_ESPIDF)
  1693. /* gate this for target platforms wishing to avoid printf reference */
  1694. printf("Exiting main with return code: %ld\n", (long int)args.return_code);
  1695. #endif
  1696. return args.return_code;
  1697. } /* wolfcrypt_test_main or wolf_test_task */
  1698. #endif /* NO_MAIN_DRIVER */
  1699. /* helper to save DER, convert to PEM and save PEM */
  1700. #if !defined(NO_ASN) && (defined(HAVE_ECC) || !defined(NO_DSA) || \
  1701. (!defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)))) \
  1702. && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  1703. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1704. #define SaveDerAndPem(d, dSz, fD, fP, pT) _SaveDerAndPem(d, dSz, fD, fP, pT, WC_TEST_RET_LN)
  1705. #else
  1706. #define SaveDerAndPem(d, dSz, fD, fP, pT) _SaveDerAndPem(d, dSz, NULL, NULL, pT, WC_TEST_RET_LN)
  1707. #endif
  1708. static wc_test_ret_t _SaveDerAndPem(const byte* der, int derSz,
  1709. const char* fileDer, const char* filePem, int pemType, int calling_line)
  1710. {
  1711. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1712. wc_test_ret_t ret;
  1713. XFILE derFile;
  1714. derFile = XFOPEN(fileDer, "wb");
  1715. if (!derFile) {
  1716. return WC_TEST_RET_ENC(calling_line, 0, WC_TEST_RET_TAG_I);
  1717. }
  1718. ret = (int)XFWRITE(der, 1, derSz, derFile);
  1719. XFCLOSE(derFile);
  1720. if (ret != derSz) {
  1721. return WC_TEST_RET_ENC(calling_line, 1, WC_TEST_RET_TAG_I);
  1722. }
  1723. #endif
  1724. #ifdef WOLFSSL_DER_TO_PEM
  1725. if (filePem) {
  1726. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1727. XFILE pemFile;
  1728. #endif
  1729. byte* pem;
  1730. int pemSz;
  1731. /* calculate PEM size */
  1732. pemSz = wc_DerToPem(der, derSz, NULL, 0, pemType);
  1733. if (pemSz < 0) {
  1734. return WC_TEST_RET_ENC(calling_line, 2, WC_TEST_RET_TAG_I);
  1735. }
  1736. pem = (byte*)XMALLOC(pemSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1737. if (pem == NULL) {
  1738. return WC_TEST_RET_ENC(calling_line, 3, WC_TEST_RET_TAG_I);
  1739. }
  1740. /* Convert to PEM */
  1741. pemSz = wc_DerToPem(der, derSz, pem, pemSz, pemType);
  1742. if (pemSz < 0) {
  1743. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1744. return WC_TEST_RET_ENC(calling_line, 4, WC_TEST_RET_TAG_I);
  1745. }
  1746. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1747. pemFile = XFOPEN(filePem, "wb");
  1748. if (!pemFile) {
  1749. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1750. return WC_TEST_RET_ENC(calling_line, 5, WC_TEST_RET_TAG_I);
  1751. }
  1752. ret = (int)XFWRITE(pem, 1, pemSz, pemFile);
  1753. XFCLOSE(pemFile);
  1754. if (ret != pemSz) {
  1755. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1756. return WC_TEST_RET_ENC(calling_line, 6, WC_TEST_RET_TAG_I);
  1757. }
  1758. #endif
  1759. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1760. }
  1761. #endif /* WOLFSSL_DER_TO_PEM */
  1762. /* suppress unused variable warnings */
  1763. (void)der;
  1764. (void)derSz;
  1765. (void)filePem;
  1766. (void)fileDer;
  1767. (void)pemType;
  1768. (void)calling_line;
  1769. return 0;
  1770. }
  1771. #endif /* WOLFSSL_KEY_GEN || WOLFSSL_CERT_GEN */
  1772. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t error_test(void)
  1773. {
  1774. const char* errStr;
  1775. char out[WOLFSSL_MAX_ERROR_SZ];
  1776. const char* unknownStr = wc_GetErrorString(0);
  1777. #ifdef NO_ERROR_STRINGS
  1778. /* Ensure a valid error code's string matches an invalid code's.
  1779. * The string is that error strings are not available.
  1780. */
  1781. errStr = wc_GetErrorString(OPEN_RAN_E);
  1782. wc_ErrorString(OPEN_RAN_E, out);
  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. #else
  1788. int i;
  1789. int j = 0;
  1790. /* Values that are not or no longer error codes. */
  1791. int missing[] = { -122, -123, -124, -127, -128, -129, -159,
  1792. -163, -164, -165, -166, -167, -168, -169, -233,
  1793. 0 };
  1794. /* Check that all errors have a string and it's the same through the two
  1795. * APIs. Check that the values that are not errors map to the unknown
  1796. * string.
  1797. */
  1798. for (i = MAX_CODE_E-1; i >= WC_LAST_E; i--) {
  1799. errStr = wc_GetErrorString(i);
  1800. wc_ErrorString(i, out);
  1801. if (i != missing[j]) {
  1802. if (XSTRCMP(errStr, unknownStr) == 0)
  1803. return WC_TEST_RET_ENC_NC;
  1804. if (XSTRCMP(out, unknownStr) == 0)
  1805. return WC_TEST_RET_ENC_NC;
  1806. if (XSTRCMP(errStr, out) != 0)
  1807. return WC_TEST_RET_ENC_NC;
  1808. if (XSTRLEN(errStr) >= WOLFSSL_MAX_ERROR_SZ)
  1809. return WC_TEST_RET_ENC_NC;
  1810. }
  1811. else {
  1812. j++;
  1813. if (XSTRCMP(errStr, unknownStr) != 0)
  1814. return WC_TEST_RET_ENC_NC;
  1815. if (XSTRCMP(out, unknownStr) != 0)
  1816. return WC_TEST_RET_ENC_NC;
  1817. }
  1818. }
  1819. /* Check if the next possible value has been given a string. */
  1820. errStr = wc_GetErrorString(i);
  1821. wc_ErrorString(i, out);
  1822. if (XSTRCMP(errStr, unknownStr) != 0)
  1823. return WC_TEST_RET_ENC_NC;
  1824. if (XSTRCMP(out, unknownStr) != 0)
  1825. return WC_TEST_RET_ENC_NC;
  1826. #endif
  1827. return 0;
  1828. }
  1829. #ifndef NO_CODING
  1830. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t base64_test(void)
  1831. {
  1832. wc_test_ret_t ret;
  1833. WOLFSSL_SMALL_STACK_STATIC const byte good[] = "A+Gd\0\0\0";
  1834. WOLFSSL_SMALL_STACK_STATIC const byte goodEnd[] = "A+Gd \r\n";
  1835. WOLFSSL_SMALL_STACK_STATIC const byte good_spaces[] = " A + G d \0";
  1836. byte out[128];
  1837. word32 outLen;
  1838. #ifdef WOLFSSL_BASE64_ENCODE
  1839. byte data[3];
  1840. word32 dataLen;
  1841. byte longData[79] = { 0 };
  1842. WOLFSSL_SMALL_STACK_STATIC const byte symbols[] = "+/A=";
  1843. #endif
  1844. WOLFSSL_SMALL_STACK_STATIC const byte badSmall[] = "AAA!Gdj=";
  1845. WOLFSSL_SMALL_STACK_STATIC const byte badLarge[] = "AAA~Gdj=";
  1846. WOLFSSL_SMALL_STACK_STATIC const byte badEOL[] = "A+Gd!AA";
  1847. WOLFSSL_SMALL_STACK_STATIC const byte badPadding[] = "AA=A";
  1848. WOLFSSL_SMALL_STACK_STATIC const byte badChar[] = ",-.:;<=>?@[\\]^_`";
  1849. byte goodChar[] =
  1850. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1851. "abcdefghijklmnopqrstuvwxyz"
  1852. "0123456789+/;";
  1853. byte charTest[] = "A+Gd\0\0\0";
  1854. int i;
  1855. /* Good Base64 encodings. */
  1856. outLen = sizeof(out);
  1857. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  1858. if (ret != 0)
  1859. return WC_TEST_RET_ENC_EC(ret);
  1860. outLen = sizeof(out);
  1861. ret = Base64_Decode(goodEnd, sizeof(goodEnd), out, &outLen);
  1862. if (ret != 0)
  1863. return WC_TEST_RET_ENC_EC(ret);
  1864. outLen = sizeof(goodChar);
  1865. ret = Base64_Decode(goodChar, sizeof(goodChar), goodChar, &outLen);
  1866. if (ret != 0)
  1867. return WC_TEST_RET_ENC_EC(ret);
  1868. if (outLen != 64 / 4 * 3)
  1869. return WC_TEST_RET_ENC_NC;
  1870. outLen = sizeof(out);
  1871. ret = Base64_Decode(good_spaces, sizeof(good_spaces), out, &outLen);
  1872. if (ret != 0)
  1873. return WC_TEST_RET_ENC_EC(ret);
  1874. /* Bad parameters. */
  1875. outLen = 1;
  1876. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  1877. if (ret != BAD_FUNC_ARG)
  1878. return WC_TEST_RET_ENC_EC(ret);
  1879. outLen = sizeof(out);
  1880. ret = Base64_Decode(badEOL, sizeof(badEOL), out, &outLen);
  1881. if (ret != ASN_INPUT_E)
  1882. return WC_TEST_RET_ENC_EC(ret);
  1883. outLen = sizeof(out);
  1884. ret = Base64_Decode(badPadding, sizeof(badPadding), out, &outLen);
  1885. if (ret != ASN_INPUT_E)
  1886. return WC_TEST_RET_ENC_EC(ret);
  1887. /* Bad character at each offset 0-3. */
  1888. for (i = 0; i < 4; i++) {
  1889. outLen = sizeof(out);
  1890. ret = Base64_Decode(badSmall + i, 4, out, &outLen);
  1891. if (ret != ASN_INPUT_E)
  1892. return WC_TEST_RET_ENC_I(i);
  1893. ret = Base64_Decode(badLarge + i, 4, out, &outLen);
  1894. if (ret != ASN_INPUT_E)
  1895. return WC_TEST_RET_ENC_I(i);
  1896. }
  1897. /* Invalid character less than 0x2b */
  1898. for (i = 1; i < 0x2b; i++) {
  1899. outLen = sizeof(out);
  1900. charTest[0] = (byte)i;
  1901. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1902. if (ret != ASN_INPUT_E)
  1903. return WC_TEST_RET_ENC_I(i);
  1904. }
  1905. /* Bad characters in range 0x2b - 0x7a. */
  1906. for (i = 0; i < (int)sizeof(badChar) - 1; i++) {
  1907. outLen = sizeof(out);
  1908. charTest[0] = badChar[i];
  1909. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1910. if (ret != ASN_INPUT_E)
  1911. return WC_TEST_RET_ENC_I(i);
  1912. }
  1913. /* Invalid character greater than 0x7a */
  1914. for (i = 0x7b; i < 0x100; i++) {
  1915. outLen = sizeof(out);
  1916. charTest[0] = (byte)i;
  1917. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1918. if (ret != ASN_INPUT_E)
  1919. return WC_TEST_RET_ENC_I(i);
  1920. }
  1921. #ifdef WOLFSSL_BASE64_ENCODE
  1922. /* Decode and encode all symbols - non-alphanumeric. */
  1923. dataLen = sizeof(data);
  1924. ret = Base64_Decode(symbols, sizeof(symbols), data, &dataLen);
  1925. if (ret != 0)
  1926. return WC_TEST_RET_ENC_EC(ret);
  1927. outLen = sizeof(out);
  1928. ret = Base64_Encode(data, dataLen, NULL, &outLen);
  1929. if (ret != LENGTH_ONLY_E)
  1930. return WC_TEST_RET_ENC_EC(ret);
  1931. outLen = sizeof(out);
  1932. ret = Base64_Encode(data, dataLen, out, &outLen);
  1933. if (ret != 0)
  1934. return WC_TEST_RET_ENC_EC(ret);
  1935. outLen = 7;
  1936. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  1937. if (ret != BUFFER_E)
  1938. return WC_TEST_RET_ENC_EC(ret);
  1939. outLen = sizeof(out);
  1940. ret = Base64_EncodeEsc(data, dataLen, NULL, &outLen);
  1941. if (ret != LENGTH_ONLY_E)
  1942. return WC_TEST_RET_ENC_EC(ret);
  1943. outLen = sizeof(out);
  1944. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  1945. if (ret != 0)
  1946. return WC_TEST_RET_ENC_EC(ret);
  1947. outLen = sizeof(out);
  1948. ret = Base64_Encode_NoNl(data, dataLen, out, &outLen);
  1949. if (ret != 0)
  1950. return WC_TEST_RET_ENC_EC(ret);
  1951. /* Data that results in an encoding longer than one line. */
  1952. outLen = sizeof(out);
  1953. dataLen = sizeof(longData);
  1954. ret = Base64_Encode(longData, dataLen, out, &outLen);
  1955. if (ret != 0)
  1956. return WC_TEST_RET_ENC_EC(ret);
  1957. outLen = sizeof(out);
  1958. ret = Base64_EncodeEsc(longData, dataLen, out, &outLen);
  1959. if (ret != 0)
  1960. return WC_TEST_RET_ENC_EC(ret);
  1961. outLen = sizeof(out);
  1962. ret = Base64_Encode_NoNl(longData, dataLen, out, &outLen);
  1963. if (ret != 0)
  1964. return WC_TEST_RET_ENC_EC(ret);
  1965. #endif
  1966. return 0;
  1967. }
  1968. #ifdef WOLFSSL_BASE16
  1969. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t base16_test(void)
  1970. {
  1971. wc_test_ret_t ret;
  1972. WOLFSSL_SMALL_STACK_STATIC const byte testData[] = "SomeDataToEncode\n";
  1973. WOLFSSL_SMALL_STACK_STATIC const byte encodedTestData[] = "536F6D6544617461546F456E636F64650A00";
  1974. byte encoded[40];
  1975. word32 encodedLen;
  1976. byte plain[40];
  1977. word32 len;
  1978. /* length returned includes null termination */
  1979. encodedLen = sizeof(encoded);
  1980. ret = Base16_Encode(testData, sizeof(testData), encoded, &encodedLen);
  1981. if (ret != 0)
  1982. return WC_TEST_RET_ENC_EC(ret);
  1983. len = (word32)XSTRLEN((char*)encoded);
  1984. if (len != encodedLen - 1)
  1985. return WC_TEST_RET_ENC_NC;
  1986. len = sizeof(plain);
  1987. ret = Base16_Decode(encoded, encodedLen - 1, plain, &len);
  1988. if (ret != 0)
  1989. return WC_TEST_RET_ENC_EC(ret);
  1990. if (len != sizeof(testData) || XMEMCMP(testData, plain, len) != 0)
  1991. return WC_TEST_RET_ENC_NC;
  1992. if (encodedLen != sizeof(encodedTestData) ||
  1993. XMEMCMP(encoded, encodedTestData, encodedLen) != 0) {
  1994. return WC_TEST_RET_ENC_NC;
  1995. }
  1996. return 0;
  1997. }
  1998. #endif /* WOLFSSL_BASE16 */
  1999. #endif /* !NO_CODING */
  2000. #ifndef NO_ASN
  2001. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t asn_test(void)
  2002. {
  2003. wc_test_ret_t ret;
  2004. /* ASN1 encoded date buffer */
  2005. WOLFSSL_SMALL_STACK_STATIC const byte dateBuf[] = {0x17, 0x0d, 0x31, 0x36, 0x30, 0x38, 0x31, 0x31,
  2006. 0x32, 0x30, 0x30, 0x37, 0x33, 0x37, 0x5a};
  2007. byte format;
  2008. int length;
  2009. const byte* datePart;
  2010. #ifndef NO_ASN_TIME
  2011. struct tm timearg;
  2012. time_t now;
  2013. #endif
  2014. ret = wc_GetDateInfo(dateBuf, (int)sizeof(dateBuf), &datePart, &format,
  2015. &length);
  2016. if (ret != 0)
  2017. return WC_TEST_RET_ENC_EC(ret);
  2018. #ifndef NO_ASN_TIME
  2019. /* Parameter Validation tests. */
  2020. if ((ret = wc_GetTime(NULL, sizeof(now))) != BAD_FUNC_ARG)
  2021. return WC_TEST_RET_ENC_EC(ret);
  2022. if ((ret = wc_GetTime(&now, 0)) != BUFFER_E)
  2023. return WC_TEST_RET_ENC_EC(ret);
  2024. now = 0;
  2025. if ((ret = wc_GetTime(&now, sizeof(now))) != 0) {
  2026. return WC_TEST_RET_ENC_EC(ret);
  2027. }
  2028. if (now == 0) {
  2029. printf("RTC/Time not set!\n");
  2030. return WC_TEST_RET_ENC_NC;
  2031. }
  2032. ret = wc_GetDateAsCalendarTime(datePart, length, format, &timearg);
  2033. if (ret != 0)
  2034. return WC_TEST_RET_ENC_EC(ret);
  2035. #endif /* !NO_ASN_TIME */
  2036. return 0;
  2037. }
  2038. #endif /* !NO_ASN */
  2039. #ifdef WOLFSSL_MD2
  2040. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md2_test(void)
  2041. {
  2042. wc_test_ret_t ret = 0;
  2043. Md2 md2;
  2044. byte hash[MD2_DIGEST_SIZE];
  2045. testVector a, b, c, d, e, f, g;
  2046. testVector test_md2[7];
  2047. int times = sizeof(test_md2) / sizeof(testVector), i;
  2048. a.input = "";
  2049. a.output = "\x83\x50\xe5\xa3\xe2\x4c\x15\x3d\xf2\x27\x5c\x9f\x80\x69"
  2050. "\x27\x73";
  2051. a.inLen = XSTRLEN(a.input);
  2052. a.outLen = MD2_DIGEST_SIZE;
  2053. b.input = "a";
  2054. b.output = "\x32\xec\x01\xec\x4a\x6d\xac\x72\xc0\xab\x96\xfb\x34\xc0"
  2055. "\xb5\xd1";
  2056. b.inLen = XSTRLEN(b.input);
  2057. b.outLen = MD2_DIGEST_SIZE;
  2058. c.input = "abc";
  2059. c.output = "\xda\x85\x3b\x0d\x3f\x88\xd9\x9b\x30\x28\x3a\x69\xe6\xde"
  2060. "\xd6\xbb";
  2061. c.inLen = XSTRLEN(c.input);
  2062. c.outLen = MD2_DIGEST_SIZE;
  2063. d.input = "message digest";
  2064. d.output = "\xab\x4f\x49\x6b\xfb\x2a\x53\x0b\x21\x9f\xf3\x30\x31\xfe"
  2065. "\x06\xb0";
  2066. d.inLen = XSTRLEN(d.input);
  2067. d.outLen = MD2_DIGEST_SIZE;
  2068. e.input = "abcdefghijklmnopqrstuvwxyz";
  2069. e.output = "\x4e\x8d\xdf\xf3\x65\x02\x92\xab\x5a\x41\x08\xc3\xaa\x47"
  2070. "\x94\x0b";
  2071. e.inLen = XSTRLEN(e.input);
  2072. e.outLen = MD2_DIGEST_SIZE;
  2073. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  2074. "6789";
  2075. f.output = "\xda\x33\xde\xf2\xa4\x2d\xf1\x39\x75\x35\x28\x46\xc3\x03"
  2076. "\x38\xcd";
  2077. f.inLen = XSTRLEN(f.input);
  2078. f.outLen = MD2_DIGEST_SIZE;
  2079. g.input = "1234567890123456789012345678901234567890123456789012345678"
  2080. "9012345678901234567890";
  2081. g.output = "\xd5\x97\x6f\x79\xd8\x3d\x3a\x0d\xc9\x80\x6c\x3c\x66\xf3"
  2082. "\xef\xd8";
  2083. g.inLen = XSTRLEN(g.input);
  2084. g.outLen = MD2_DIGEST_SIZE;
  2085. test_md2[0] = a;
  2086. test_md2[1] = b;
  2087. test_md2[2] = c;
  2088. test_md2[3] = d;
  2089. test_md2[4] = e;
  2090. test_md2[5] = f;
  2091. test_md2[6] = g;
  2092. wc_InitMd2(&md2);
  2093. for (i = 0; i < times; ++i) {
  2094. wc_Md2Update(&md2, (byte*)test_md2[i].input, (word32)test_md2[i].inLen);
  2095. wc_Md2Final(&md2, hash);
  2096. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0)
  2097. return WC_TEST_RET_ENC_I(i);
  2098. }
  2099. for (i = 0; i < times; ++i) {
  2100. ret = wc_Md2Hash((byte*)test_md2[i].input, (word32)test_md2[i].inLen, hash);
  2101. if (ret != 0) {
  2102. return WC_TEST_RET_ENC_I(i);
  2103. }
  2104. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0) {
  2105. return WC_TEST_RET_ENC_I(i);
  2106. }
  2107. }
  2108. return 0;
  2109. }
  2110. #endif
  2111. #ifndef NO_MD5
  2112. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md5_test(void)
  2113. {
  2114. wc_test_ret_t ret = 0;
  2115. wc_Md5 md5, md5Copy;
  2116. byte hash[WC_MD5_DIGEST_SIZE];
  2117. byte hashcopy[WC_MD5_DIGEST_SIZE];
  2118. testVector a, b, c, d, e, f;
  2119. testVector test_md5[6];
  2120. int times = sizeof(test_md5) / sizeof(testVector), i;
  2121. a.input = "";
  2122. a.output = "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42"
  2123. "\x7e";
  2124. a.inLen = XSTRLEN(a.input);
  2125. a.outLen = WC_MD5_DIGEST_SIZE;
  2126. b.input = "abc";
  2127. b.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  2128. "\x72";
  2129. b.inLen = XSTRLEN(b.input);
  2130. b.outLen = WC_MD5_DIGEST_SIZE;
  2131. c.input = "message digest";
  2132. c.output = "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
  2133. "\xd0";
  2134. c.inLen = XSTRLEN(c.input);
  2135. c.outLen = WC_MD5_DIGEST_SIZE;
  2136. d.input = "abcdefghijklmnopqrstuvwxyz";
  2137. d.output = "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
  2138. "\x3b";
  2139. d.inLen = XSTRLEN(d.input);
  2140. d.outLen = WC_MD5_DIGEST_SIZE;
  2141. e.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  2142. "6789";
  2143. e.output = "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
  2144. "\x9f";
  2145. e.inLen = XSTRLEN(e.input);
  2146. e.outLen = WC_MD5_DIGEST_SIZE;
  2147. f.input = "1234567890123456789012345678901234567890123456789012345678"
  2148. "9012345678901234567890";
  2149. f.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  2150. "\x7a";
  2151. f.inLen = XSTRLEN(f.input);
  2152. f.outLen = WC_MD5_DIGEST_SIZE;
  2153. test_md5[0] = a;
  2154. test_md5[1] = b;
  2155. test_md5[2] = c;
  2156. test_md5[3] = d;
  2157. test_md5[4] = e;
  2158. test_md5[5] = f;
  2159. ret = wc_InitMd5_ex(&md5, HEAP_HINT, devId);
  2160. if (ret != 0)
  2161. return WC_TEST_RET_ENC_EC(ret);
  2162. ret = wc_InitMd5_ex(&md5Copy, HEAP_HINT, devId);
  2163. if (ret != 0) {
  2164. wc_Md5Free(&md5);
  2165. return WC_TEST_RET_ENC_EC(ret);
  2166. }
  2167. for (i = 0; i < times; ++i) {
  2168. ret = wc_Md5Update(&md5, (byte*)test_md5[i].input,
  2169. (word32)test_md5[i].inLen);
  2170. if (ret != 0)
  2171. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2172. ret = wc_Md5GetHash(&md5, hashcopy);
  2173. if (ret != 0)
  2174. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2175. ret = wc_Md5Copy(&md5, &md5Copy);
  2176. if (ret != 0)
  2177. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2178. ret = wc_Md5Final(&md5, hash);
  2179. if (ret != 0)
  2180. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2181. wc_Md5Free(&md5Copy);
  2182. if (XMEMCMP(hash, test_md5[i].output, WC_MD5_DIGEST_SIZE) != 0)
  2183. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2184. if (XMEMCMP(hash, hashcopy, WC_MD5_DIGEST_SIZE) != 0)
  2185. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2186. }
  2187. #ifndef NO_LARGE_HASH_TEST
  2188. /* BEGIN LARGE HASH TEST */ {
  2189. byte large_input[1024];
  2190. const char* large_digest =
  2191. "\x44\xd0\x88\xce\xf1\x36\xd1\x78\xe9\xc8\xba\x84\xc3\xfd\xf6\xca";
  2192. for (i = 0; i < (int)sizeof(large_input); i++) {
  2193. large_input[i] = (byte)(i & 0xFF);
  2194. }
  2195. times = 100;
  2196. #ifdef WOLFSSL_PIC32MZ_HASH
  2197. wc_Md5SizeSet(&md5, times * sizeof(large_input));
  2198. #endif
  2199. for (i = 0; i < times; ++i) {
  2200. ret = wc_Md5Update(&md5, (byte*)large_input,
  2201. (word32)sizeof(large_input));
  2202. if (ret != 0)
  2203. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2204. }
  2205. ret = wc_Md5Final(&md5, hash);
  2206. if (ret != 0)
  2207. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2208. if (XMEMCMP(hash, large_digest, WC_MD5_DIGEST_SIZE) != 0)
  2209. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  2210. } /* END LARGE HASH TEST */
  2211. #endif /* NO_LARGE_HASH_TEST */
  2212. exit:
  2213. wc_Md5Free(&md5);
  2214. wc_Md5Free(&md5Copy);
  2215. return ret;
  2216. }
  2217. #endif /* NO_MD5 */
  2218. #ifndef NO_MD4
  2219. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t md4_test(void)
  2220. {
  2221. Md4 md4;
  2222. byte hash[MD4_DIGEST_SIZE];
  2223. testVector a, b, c, d, e, f, g;
  2224. testVector test_md4[7];
  2225. int times = sizeof(test_md4) / sizeof(testVector), i;
  2226. a.input = "";
  2227. a.output = "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"
  2228. "\xc0";
  2229. a.inLen = XSTRLEN(a.input);
  2230. a.outLen = MD4_DIGEST_SIZE;
  2231. b.input = "a";
  2232. b.output = "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"
  2233. "\x24";
  2234. b.inLen = XSTRLEN(b.input);
  2235. b.outLen = MD4_DIGEST_SIZE;
  2236. c.input = "abc";
  2237. c.output = "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"
  2238. "\x9d";
  2239. c.inLen = XSTRLEN(c.input);
  2240. c.outLen = MD4_DIGEST_SIZE;
  2241. d.input = "message digest";
  2242. d.output = "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"
  2243. "\x4b";
  2244. d.inLen = XSTRLEN(d.input);
  2245. d.outLen = MD4_DIGEST_SIZE;
  2246. e.input = "abcdefghijklmnopqrstuvwxyz";
  2247. e.output = "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"
  2248. "\xa9";
  2249. e.inLen = XSTRLEN(e.input);
  2250. e.outLen = MD4_DIGEST_SIZE;
  2251. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  2252. "6789";
  2253. f.output = "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"
  2254. "\xe4";
  2255. f.inLen = XSTRLEN(f.input);
  2256. f.outLen = MD4_DIGEST_SIZE;
  2257. g.input = "1234567890123456789012345678901234567890123456789012345678"
  2258. "9012345678901234567890";
  2259. g.output = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"
  2260. "\x36";
  2261. g.inLen = XSTRLEN(g.input);
  2262. g.outLen = MD4_DIGEST_SIZE;
  2263. test_md4[0] = a;
  2264. test_md4[1] = b;
  2265. test_md4[2] = c;
  2266. test_md4[3] = d;
  2267. test_md4[4] = e;
  2268. test_md4[5] = f;
  2269. test_md4[6] = g;
  2270. wc_InitMd4(&md4);
  2271. for (i = 0; i < times; ++i) {
  2272. wc_Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen);
  2273. wc_Md4Final(&md4, hash);
  2274. if (XMEMCMP(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0)
  2275. return WC_TEST_RET_ENC_I(i);
  2276. }
  2277. return 0;
  2278. }
  2279. #endif /* NO_MD4 */
  2280. #ifndef NO_SHA
  2281. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha_test(void)
  2282. {
  2283. wc_test_ret_t ret = 0;
  2284. wc_Sha sha, shaCopy;
  2285. byte hash[WC_SHA_DIGEST_SIZE];
  2286. byte hashcopy[WC_SHA_DIGEST_SIZE];
  2287. testVector a, b, c, d, e;
  2288. testVector test_sha[5];
  2289. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2290. a.input = "";
  2291. a.output = "\xda\x39\xa3\xee\x5e\x6b\x4b\x0d\x32\x55\xbf\xef\x95\x60\x18"
  2292. "\x90\xaf\xd8\x07\x09";
  2293. a.inLen = XSTRLEN(a.input);
  2294. a.outLen = WC_SHA_DIGEST_SIZE;
  2295. b.input = "abc";
  2296. b.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  2297. "\x6C\x9C\xD0\xD8\x9D";
  2298. b.inLen = XSTRLEN(b.input);
  2299. b.outLen = WC_SHA_DIGEST_SIZE;
  2300. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2301. c.output = "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
  2302. "\xE5\xE5\x46\x70\xF1";
  2303. c.inLen = XSTRLEN(c.input);
  2304. c.outLen = WC_SHA_DIGEST_SIZE;
  2305. d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2306. "aaaaaa";
  2307. d.output = "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
  2308. "\x2A\x25\xEC\x64\x4D";
  2309. d.inLen = XSTRLEN(d.input);
  2310. d.outLen = WC_SHA_DIGEST_SIZE;
  2311. e.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2312. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2313. "aaaaaaaaaa";
  2314. e.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  2315. "\x53\x99\x5E\x26\xA0";
  2316. e.inLen = XSTRLEN(e.input);
  2317. e.outLen = WC_SHA_DIGEST_SIZE;
  2318. test_sha[0] = a;
  2319. test_sha[1] = b;
  2320. test_sha[2] = c;
  2321. test_sha[3] = d;
  2322. test_sha[4] = e;
  2323. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  2324. if (ret != 0)
  2325. return WC_TEST_RET_ENC_EC(ret);
  2326. ret = wc_InitSha_ex(&shaCopy, HEAP_HINT, devId);
  2327. if (ret != 0) {
  2328. wc_ShaFree(&sha);
  2329. return WC_TEST_RET_ENC_EC(ret);
  2330. }
  2331. for (i = 0; i < times; ++i) {
  2332. ret = wc_ShaUpdate(&sha, (byte*)test_sha[i].input,
  2333. (word32)test_sha[i].inLen);
  2334. if (ret != 0)
  2335. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2336. ret = wc_ShaGetHash(&sha, hashcopy);
  2337. if (ret != 0)
  2338. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2339. ret = wc_ShaCopy(&sha, &shaCopy);
  2340. if (ret != 0)
  2341. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2342. ret = wc_ShaFinal(&sha, hash);
  2343. if (ret != 0)
  2344. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2345. wc_ShaFree(&shaCopy);
  2346. if (XMEMCMP(hash, test_sha[i].output, WC_SHA_DIGEST_SIZE) != 0)
  2347. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2348. if (XMEMCMP(hash, hashcopy, WC_SHA_DIGEST_SIZE) != 0)
  2349. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2350. }
  2351. #ifndef NO_LARGE_HASH_TEST
  2352. /* BEGIN LARGE HASH TEST */ {
  2353. byte large_input[1024];
  2354. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  2355. defined(HASH_SIZE_LIMIT)
  2356. const char* large_digest =
  2357. "\x1d\x6a\x5a\xf6\xe5\x7c\x86\xce\x7f\x7c\xaf\xd5\xdb\x08\xcd\x59"
  2358. "\x15\x8c\x6d\xb6";
  2359. #else
  2360. const char* large_digest =
  2361. "\x8b\x77\x02\x48\x39\xe8\xdb\xd3\x9a\xf4\x05\x24\x66\x12\x2d\x9e"
  2362. "\xc5\xd9\x0a\xac";
  2363. #endif
  2364. for (i = 0; i < (int)sizeof(large_input); i++) {
  2365. large_input[i] = (byte)(i & 0xFF);
  2366. }
  2367. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  2368. defined(HASH_SIZE_LIMIT)
  2369. times = 20;
  2370. #else
  2371. times = 100;
  2372. #endif
  2373. #ifdef WOLFSSL_PIC32MZ_HASH
  2374. wc_ShaSizeSet(&sha, times * sizeof(large_input));
  2375. #endif
  2376. for (i = 0; i < times; ++i) {
  2377. ret = wc_ShaUpdate(&sha, (byte*)large_input,
  2378. (word32)sizeof(large_input));
  2379. if (ret != 0)
  2380. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2381. }
  2382. ret = wc_ShaFinal(&sha, hash);
  2383. if (ret != 0)
  2384. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2385. if (XMEMCMP(hash, large_digest, WC_SHA_DIGEST_SIZE) != 0)
  2386. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  2387. } /* END LARGE HASH TEST */
  2388. #endif /* NO_LARGE_HASH_TEST */
  2389. exit:
  2390. wc_ShaFree(&sha);
  2391. wc_ShaFree(&shaCopy);
  2392. return ret;
  2393. }
  2394. #endif /* NO_SHA */
  2395. #ifdef WOLFSSL_RIPEMD
  2396. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ripemd_test(void)
  2397. {
  2398. RipeMd ripemd;
  2399. wc_test_ret_t ret;
  2400. byte hash[RIPEMD_DIGEST_SIZE];
  2401. testVector a, b, c, d;
  2402. testVector test_ripemd[4];
  2403. int times = sizeof(test_ripemd) / sizeof(struct testVector), i;
  2404. a.input = "abc";
  2405. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  2406. "\xb0\x87\xf1\x5a\x0b\xfc";
  2407. a.inLen = XSTRLEN(a.input);
  2408. a.outLen = RIPEMD_DIGEST_SIZE;
  2409. b.input = "message digest";
  2410. b.output = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
  2411. "\x5f\xfa\x21\x59\x5f\x36";
  2412. b.inLen = XSTRLEN(b.input);
  2413. b.outLen = RIPEMD_DIGEST_SIZE;
  2414. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2415. c.output = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
  2416. "\xf4\x9a\xda\x62\xeb\x2b";
  2417. c.inLen = XSTRLEN(c.input);
  2418. c.outLen = RIPEMD_DIGEST_SIZE;
  2419. d.input = "12345678901234567890123456789012345678901234567890123456"
  2420. "789012345678901234567890";
  2421. d.output = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
  2422. "\x82\xbf\x63\x32\x6b\xfb";
  2423. d.inLen = XSTRLEN(d.input);
  2424. d.outLen = RIPEMD_DIGEST_SIZE;
  2425. test_ripemd[0] = a;
  2426. test_ripemd[1] = b;
  2427. test_ripemd[2] = c;
  2428. test_ripemd[3] = d;
  2429. ret = wc_InitRipeMd(&ripemd);
  2430. if (ret != 0) {
  2431. return WC_TEST_RET_ENC_EC(ret);
  2432. }
  2433. for (i = 0; i < times; ++i) {
  2434. ret = wc_RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input,
  2435. (word32)test_ripemd[i].inLen);
  2436. if (ret != 0) {
  2437. return WC_TEST_RET_ENC_I(i);
  2438. }
  2439. ret = wc_RipeMdFinal(&ripemd, hash);
  2440. if (ret != 0) {
  2441. return WC_TEST_RET_ENC_I(i);
  2442. }
  2443. if (XMEMCMP(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0)
  2444. return WC_TEST_RET_ENC_I(i);
  2445. }
  2446. return 0;
  2447. }
  2448. #endif /* WOLFSSL_RIPEMD */
  2449. #ifdef HAVE_BLAKE2
  2450. #define BLAKE2B_TESTS 3
  2451. static const byte blake2b_vec[BLAKE2B_TESTS][BLAKE2B_OUTBYTES] =
  2452. {
  2453. {
  2454. 0x78, 0x6A, 0x02, 0xF7, 0x42, 0x01, 0x59, 0x03,
  2455. 0xC6, 0xC6, 0xFD, 0x85, 0x25, 0x52, 0xD2, 0x72,
  2456. 0x91, 0x2F, 0x47, 0x40, 0xE1, 0x58, 0x47, 0x61,
  2457. 0x8A, 0x86, 0xE2, 0x17, 0xF7, 0x1F, 0x54, 0x19,
  2458. 0xD2, 0x5E, 0x10, 0x31, 0xAF, 0xEE, 0x58, 0x53,
  2459. 0x13, 0x89, 0x64, 0x44, 0x93, 0x4E, 0xB0, 0x4B,
  2460. 0x90, 0x3A, 0x68, 0x5B, 0x14, 0x48, 0xB7, 0x55,
  2461. 0xD5, 0x6F, 0x70, 0x1A, 0xFE, 0x9B, 0xE2, 0xCE
  2462. },
  2463. {
  2464. 0x2F, 0xA3, 0xF6, 0x86, 0xDF, 0x87, 0x69, 0x95,
  2465. 0x16, 0x7E, 0x7C, 0x2E, 0x5D, 0x74, 0xC4, 0xC7,
  2466. 0xB6, 0xE4, 0x8F, 0x80, 0x68, 0xFE, 0x0E, 0x44,
  2467. 0x20, 0x83, 0x44, 0xD4, 0x80, 0xF7, 0x90, 0x4C,
  2468. 0x36, 0x96, 0x3E, 0x44, 0x11, 0x5F, 0xE3, 0xEB,
  2469. 0x2A, 0x3A, 0xC8, 0x69, 0x4C, 0x28, 0xBC, 0xB4,
  2470. 0xF5, 0xA0, 0xF3, 0x27, 0x6F, 0x2E, 0x79, 0x48,
  2471. 0x7D, 0x82, 0x19, 0x05, 0x7A, 0x50, 0x6E, 0x4B
  2472. },
  2473. {
  2474. 0x1C, 0x08, 0x79, 0x8D, 0xC6, 0x41, 0xAB, 0xA9,
  2475. 0xDE, 0xE4, 0x35, 0xE2, 0x25, 0x19, 0xA4, 0x72,
  2476. 0x9A, 0x09, 0xB2, 0xBF, 0xE0, 0xFF, 0x00, 0xEF,
  2477. 0x2D, 0xCD, 0x8E, 0xD6, 0xF8, 0xA0, 0x7D, 0x15,
  2478. 0xEA, 0xF4, 0xAE, 0xE5, 0x2B, 0xBF, 0x18, 0xAB,
  2479. 0x56, 0x08, 0xA6, 0x19, 0x0F, 0x70, 0xB9, 0x04,
  2480. 0x86, 0xC8, 0xA7, 0xD4, 0x87, 0x37, 0x10, 0xB1,
  2481. 0x11, 0x5D, 0x3D, 0xEB, 0xBB, 0x43, 0x27, 0xB5
  2482. }
  2483. };
  2484. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blake2b_test(void)
  2485. {
  2486. Blake2b b2b;
  2487. byte digest[64];
  2488. byte input[64];
  2489. int i, ret;
  2490. for (i = 0; i < (int)sizeof(input); i++)
  2491. input[i] = (byte)i;
  2492. for (i = 0; i < BLAKE2B_TESTS; i++) {
  2493. ret = wc_InitBlake2b(&b2b, 64);
  2494. if (ret != 0)
  2495. return WC_TEST_RET_ENC_I(i);
  2496. ret = wc_Blake2bUpdate(&b2b, input, i);
  2497. if (ret != 0)
  2498. return WC_TEST_RET_ENC_I(i);
  2499. ret = wc_Blake2bFinal(&b2b, digest, 64);
  2500. if (ret != 0)
  2501. return WC_TEST_RET_ENC_I(i);
  2502. if (XMEMCMP(digest, blake2b_vec[i], 64) != 0) {
  2503. return WC_TEST_RET_ENC_I(i);
  2504. }
  2505. }
  2506. return 0;
  2507. }
  2508. #endif /* HAVE_BLAKE2 */
  2509. #ifdef HAVE_BLAKE2S
  2510. #define BLAKE2S_TESTS 3
  2511. static const byte blake2s_vec[BLAKE2S_TESTS][BLAKE2S_OUTBYTES] =
  2512. {
  2513. {
  2514. 0x69, 0x21, 0x7a, 0x30, 0x79, 0x90, 0x80, 0x94,
  2515. 0xe1, 0x11, 0x21, 0xd0, 0x42, 0x35, 0x4a, 0x7c,
  2516. 0x1f, 0x55, 0xb6, 0x48, 0x2c, 0xa1, 0xa5, 0x1e,
  2517. 0x1b, 0x25, 0x0d, 0xfd, 0x1e, 0xd0, 0xee, 0xf9,
  2518. },
  2519. {
  2520. 0xe3, 0x4d, 0x74, 0xdb, 0xaf, 0x4f, 0xf4, 0xc6,
  2521. 0xab, 0xd8, 0x71, 0xcc, 0x22, 0x04, 0x51, 0xd2,
  2522. 0xea, 0x26, 0x48, 0x84, 0x6c, 0x77, 0x57, 0xfb,
  2523. 0xaa, 0xc8, 0x2f, 0xe5, 0x1a, 0xd6, 0x4b, 0xea,
  2524. },
  2525. {
  2526. 0xdd, 0xad, 0x9a, 0xb1, 0x5d, 0xac, 0x45, 0x49,
  2527. 0xba, 0x42, 0xf4, 0x9d, 0x26, 0x24, 0x96, 0xbe,
  2528. 0xf6, 0xc0, 0xba, 0xe1, 0xdd, 0x34, 0x2a, 0x88,
  2529. 0x08, 0xf8, 0xea, 0x26, 0x7c, 0x6e, 0x21, 0x0c,
  2530. }
  2531. };
  2532. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blake2s_test(void)
  2533. {
  2534. Blake2s b2s;
  2535. byte digest[32];
  2536. byte input[64];
  2537. int i, ret;
  2538. for (i = 0; i < (int)sizeof(input); i++)
  2539. input[i] = (byte)i;
  2540. for (i = 0; i < BLAKE2S_TESTS; i++) {
  2541. ret = wc_InitBlake2s(&b2s, 32);
  2542. if (ret != 0)
  2543. return WC_TEST_RET_ENC_I(i);
  2544. ret = wc_Blake2sUpdate(&b2s, input, i);
  2545. if (ret != 0)
  2546. return WC_TEST_RET_ENC_I(i);
  2547. ret = wc_Blake2sFinal(&b2s, digest, 32);
  2548. if (ret != 0)
  2549. return WC_TEST_RET_ENC_I(i);
  2550. if (XMEMCMP(digest, blake2s_vec[i], 32) != 0) {
  2551. return WC_TEST_RET_ENC_I(i);
  2552. }
  2553. }
  2554. return 0;
  2555. }
  2556. #endif /* HAVE_BLAKE2S */
  2557. #ifdef WOLFSSL_SHA224
  2558. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha224_test(void)
  2559. {
  2560. wc_Sha224 sha, shaCopy;
  2561. byte hash[WC_SHA224_DIGEST_SIZE];
  2562. byte hashcopy[WC_SHA224_DIGEST_SIZE];
  2563. wc_test_ret_t ret = 0;
  2564. testVector a, b, c;
  2565. testVector test_sha[3];
  2566. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2567. a.input = "";
  2568. a.output = "\xd1\x4a\x02\x8c\x2a\x3a\x2b\xc9\x47\x61\x02\xbb\x28\x82\x34"
  2569. "\xc4\x15\xa2\xb0\x1f\x82\x8e\xa6\x2a\xc5\xb3\xe4\x2f";
  2570. a.inLen = XSTRLEN(a.input);
  2571. a.outLen = WC_SHA224_DIGEST_SIZE;
  2572. b.input = "abc";
  2573. b.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2\x55"
  2574. "\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  2575. b.inLen = XSTRLEN(b.input);
  2576. b.outLen = WC_SHA224_DIGEST_SIZE;
  2577. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2578. c.output = "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  2579. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  2580. c.inLen = XSTRLEN(c.input);
  2581. c.outLen = WC_SHA224_DIGEST_SIZE;
  2582. test_sha[0] = a;
  2583. test_sha[1] = b;
  2584. test_sha[2] = c;
  2585. ret = wc_InitSha224_ex(&sha, HEAP_HINT, devId);
  2586. if (ret != 0)
  2587. return WC_TEST_RET_ENC_EC(ret);
  2588. ret = wc_InitSha224_ex(&shaCopy, HEAP_HINT, devId);
  2589. if (ret != 0) {
  2590. wc_Sha224Free(&sha);
  2591. return WC_TEST_RET_ENC_EC(ret);
  2592. }
  2593. for (i = 0; i < times; ++i) {
  2594. ret = wc_Sha224Update(&sha, (byte*)test_sha[i].input,
  2595. (word32)test_sha[i].inLen);
  2596. if (ret != 0)
  2597. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2598. ret = wc_Sha224GetHash(&sha, hashcopy);
  2599. if (ret != 0)
  2600. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2601. ret = wc_Sha224Copy(&sha, &shaCopy);
  2602. if (ret != 0)
  2603. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2604. ret = wc_Sha224Final(&sha, hash);
  2605. if (ret != 0)
  2606. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2607. wc_Sha224Free(&shaCopy);
  2608. if (XMEMCMP(hash, test_sha[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  2609. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2610. if (XMEMCMP(hash, hashcopy, WC_SHA224_DIGEST_SIZE) != 0)
  2611. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2612. }
  2613. exit:
  2614. wc_Sha224Free(&sha);
  2615. wc_Sha224Free(&shaCopy);
  2616. return ret;
  2617. }
  2618. #endif
  2619. #ifndef NO_SHA256
  2620. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha256_test(void)
  2621. {
  2622. wc_Sha256 sha, shaCopy;
  2623. byte hash[WC_SHA256_DIGEST_SIZE];
  2624. byte hashcopy[WC_SHA256_DIGEST_SIZE];
  2625. wc_test_ret_t ret = 0;
  2626. testVector a, b, c;
  2627. testVector test_sha[3];
  2628. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2629. a.input = "";
  2630. a.output = "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9"
  2631. "\x24\x27\xae\x41\xe4\x64\x9b\x93\x4c\xa4\x95\x99\x1b\x78\x52"
  2632. "\xb8\x55";
  2633. a.inLen = XSTRLEN(a.input);
  2634. a.outLen = WC_SHA256_DIGEST_SIZE;
  2635. b.input = "abc";
  2636. b.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  2637. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  2638. "\x15\xAD";
  2639. b.inLen = XSTRLEN(b.input);
  2640. b.outLen = WC_SHA256_DIGEST_SIZE;
  2641. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2642. c.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  2643. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  2644. "\x06\xC1";
  2645. c.inLen = XSTRLEN(c.input);
  2646. c.outLen = WC_SHA256_DIGEST_SIZE;
  2647. test_sha[0] = a;
  2648. test_sha[1] = b;
  2649. test_sha[2] = c;
  2650. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  2651. if (ret != 0)
  2652. return WC_TEST_RET_ENC_EC(ret);
  2653. ret = wc_InitSha256_ex(&shaCopy, HEAP_HINT, devId);
  2654. if (ret != 0) {
  2655. wc_Sha256Free(&sha);
  2656. return WC_TEST_RET_ENC_EC(ret);
  2657. }
  2658. for (i = 0; i < times; ++i) {
  2659. ret = wc_Sha256Update(&sha, (byte*)test_sha[i].input,
  2660. (word32)test_sha[i].inLen);
  2661. if (ret != 0) {
  2662. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2663. }
  2664. ret = wc_Sha256GetHash(&sha, hashcopy);
  2665. if (ret != 0)
  2666. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2667. ret = wc_Sha256Copy(&sha, &shaCopy);
  2668. if (ret != 0)
  2669. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2670. ret = wc_Sha256Final(&sha, hash);
  2671. if (ret != 0)
  2672. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2673. wc_Sha256Free(&shaCopy);
  2674. if (XMEMCMP(hash, test_sha[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  2675. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2676. if (XMEMCMP(hash, hashcopy, WC_SHA256_DIGEST_SIZE) != 0)
  2677. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2678. }
  2679. #ifndef NO_LARGE_HASH_TEST
  2680. /* BEGIN LARGE HASH TEST */ {
  2681. byte large_input[1024];
  2682. #ifdef HASH_SIZE_LIMIT
  2683. const char* large_digest =
  2684. "\xa4\x75\x9e\x7a\xa2\x03\x38\x32\x88\x66\xa2\xea\x17\xea\xf8\xc7"
  2685. "\xfe\x4e\xc6\xbb\xe3\xbb\x71\xce\xe7\xdf\x7c\x04\x61\xb3\xc2\x2f";
  2686. #else
  2687. const char* large_digest =
  2688. "\x27\x78\x3e\x87\x96\x3a\x4e\xfb\x68\x29\xb5\x31\xc9\xba\x57\xb4"
  2689. "\x4f\x45\x79\x7f\x67\x70\xbd\x63\x7f\xbf\x0d\x80\x7c\xbd\xba\xe0";
  2690. #endif
  2691. for (i = 0; i < (int)sizeof(large_input); i++) {
  2692. large_input[i] = (byte)(i & 0xFF);
  2693. }
  2694. #ifdef HASH_SIZE_LIMIT
  2695. times = 20;
  2696. #else
  2697. times = 100;
  2698. #endif
  2699. #ifdef WOLFSSL_PIC32MZ_HASH
  2700. wc_Sha256SizeSet(&sha, times * sizeof(large_input));
  2701. #endif
  2702. for (i = 0; i < times; ++i) {
  2703. ret = wc_Sha256Update(&sha, (byte*)large_input,
  2704. (word32)sizeof(large_input));
  2705. if (ret != 0)
  2706. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2707. }
  2708. ret = wc_Sha256Final(&sha, hash);
  2709. if (ret != 0)
  2710. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2711. if (XMEMCMP(hash, large_digest, WC_SHA256_DIGEST_SIZE) != 0)
  2712. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  2713. } /* END LARGE HASH TEST */
  2714. #endif /* NO_LARGE_HASH_TEST */
  2715. exit:
  2716. wc_Sha256Free(&sha);
  2717. wc_Sha256Free(&shaCopy);
  2718. return ret;
  2719. }
  2720. #endif
  2721. #ifdef WOLFSSL_SHA512
  2722. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_test(void)
  2723. {
  2724. /*
  2725. ** See https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA512.pdf
  2726. */
  2727. wc_Sha512 sha, shaCopy;
  2728. byte hash[WC_SHA512_DIGEST_SIZE];
  2729. byte hashcopy[WC_SHA512_DIGEST_SIZE];
  2730. wc_test_ret_t ret = 0;
  2731. testVector a, b, c;
  2732. testVector test_sha[3];
  2733. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2734. a.input = "";
  2735. a.output = "\xcf\x83\xe1\x35\x7e\xef\xb8\xbd\xf1\x54\x28\x50\xd6\x6d\x80"
  2736. "\x07\xd6\x20\xe4\x05\x0b\x57\x15\xdc\x83\xf4\xa9\x21\xd3\x6c"
  2737. "\xe9\xce\x47\xd0\xd1\x3c\x5d\x85\xf2\xb0\xff\x83\x18\xd2\x87"
  2738. "\x7e\xec\x2f\x63\xb9\x31\xbd\x47\x41\x7a\x81\xa5\x38\x32\x7a"
  2739. "\xf9\x27\xda\x3e";
  2740. a.inLen = XSTRLEN(a.input);
  2741. a.outLen = WC_SHA512_DIGEST_SIZE;
  2742. b.input = "abc";
  2743. b.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  2744. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  2745. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  2746. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  2747. "\xa5\x4c\xa4\x9f";
  2748. b.inLen = XSTRLEN(b.input);
  2749. b.outLen = WC_SHA512_DIGEST_SIZE;
  2750. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  2751. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  2752. c.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  2753. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  2754. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  2755. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  2756. "\x87\x4b\xe9\x09";
  2757. c.inLen = XSTRLEN(c.input);
  2758. c.outLen = WC_SHA512_DIGEST_SIZE;
  2759. test_sha[0] = a;
  2760. test_sha[1] = b;
  2761. test_sha[2] = c;
  2762. ret = wc_InitSha512_ex(&sha, HEAP_HINT, devId);
  2763. if (ret != 0)
  2764. return WC_TEST_RET_ENC_EC(ret);
  2765. ret = wc_InitSha512_ex(&shaCopy, HEAP_HINT, devId);
  2766. if (ret != 0) {
  2767. wc_Sha512Free(&sha);
  2768. return WC_TEST_RET_ENC_EC(ret);
  2769. }
  2770. for (i = 0; i < times; ++i) {
  2771. ret = wc_Sha512Update(&sha, (byte*)test_sha[i].input,
  2772. (word32)test_sha[i].inLen);
  2773. if (ret != 0)
  2774. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2775. ret = wc_Sha512GetHash(&sha, hashcopy);
  2776. if (ret != 0)
  2777. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2778. ret = wc_Sha512Copy(&sha, &shaCopy);
  2779. if (ret != 0)
  2780. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2781. ret = wc_Sha512Final(&sha, hash);
  2782. if (ret != 0)
  2783. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2784. wc_Sha512Free(&shaCopy);
  2785. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  2786. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2787. if (XMEMCMP(hash, hashcopy, WC_SHA512_DIGEST_SIZE) != 0)
  2788. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2789. }
  2790. #ifndef NO_LARGE_HASH_TEST
  2791. /* BEGIN LARGE HASH TEST */ {
  2792. byte large_input[1024];
  2793. #ifdef HASH_SIZE_LIMIT
  2794. const char* large_digest =
  2795. "\x30\x9B\x96\xA6\xE9\x43\x78\x30\xA3\x71\x51\x61\xC1\xEB\xE1\xBE"
  2796. "\xC8\xA5\xF9\x13\x5A\xD6\x6D\x9E\x46\x31\x31\x67\x8D\xE2\xC0\x0B"
  2797. "\x2A\x1A\x03\xE1\xF3\x48\xA7\x33\xBD\x49\xF8\xFF\xF1\xC2\xC2\x95"
  2798. "\xCB\xF0\xAF\x87\x61\x85\x58\x63\x6A\xCA\x70\x9C\x8B\x83\x3F\x5D";
  2799. #else
  2800. const char* large_digest =
  2801. "\x5a\x1f\x73\x90\xbd\x8c\xe4\x63\x54\xce\xa0\x9b\xef\x32\x78\x2d"
  2802. "\x2e\xe7\x0d\x5e\x2f\x9d\x15\x1b\xdd\x2d\xde\x65\x0c\x7b\xfa\x83"
  2803. "\x5e\x80\x02\x13\x84\xb8\x3f\xff\x71\x62\xb5\x09\x89\x63\xe1\xdc"
  2804. "\xa5\xdc\xfc\xfa\x9d\x1a\x4d\xc0\xfa\x3a\x14\xf6\x01\x51\x90\xa4";
  2805. #endif
  2806. for (i = 0; i < (int)sizeof(large_input); i++) {
  2807. large_input[i] = (byte)(i & 0xFF);
  2808. }
  2809. #ifdef HASH_SIZE_LIMIT
  2810. times = 20;
  2811. #else
  2812. times = 100;
  2813. #endif
  2814. for (i = 0; i < times; ++i) {
  2815. ret = wc_Sha512Update(&sha, (byte*)large_input,
  2816. (word32)sizeof(large_input));
  2817. if (ret != 0)
  2818. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2819. }
  2820. ret = wc_Sha512Final(&sha, hash);
  2821. if (ret != 0)
  2822. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2823. if (XMEMCMP(hash, large_digest, WC_SHA512_DIGEST_SIZE) != 0)
  2824. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  2825. #ifndef NO_UNALIGNED_MEMORY_TEST
  2826. /* Unaligned memory access test */
  2827. for (i = 1; i < 16; i++) {
  2828. ret = wc_Sha512Update(&sha, (byte*)large_input + i,
  2829. (word32)sizeof(large_input) - i);
  2830. if (ret != 0)
  2831. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2832. ret = wc_Sha512Final(&sha, hash);
  2833. }
  2834. #endif
  2835. } /* END LARGE HASH TEST */
  2836. #endif /* NO_LARGE_HASH_TEST */
  2837. exit:
  2838. wc_Sha512Free(&sha);
  2839. wc_Sha512Free(&shaCopy);
  2840. return ret;
  2841. }
  2842. #if !defined(WOLFSSL_NOSHA512_224) && \
  2843. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  2844. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_224_test(void)
  2845. {
  2846. /*
  2847. ** See https://csrc.nist.gov/Projects/cryptographic-standards-and-guidelines/example-values
  2848. **
  2849. ** NIST SHA512/224 Document Example:
  2850. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA512_224.pdf
  2851. */
  2852. wc_Sha512 sha, shaCopy;
  2853. byte hash[WC_SHA512_224_DIGEST_SIZE];
  2854. byte hashcopy[WC_SHA512_224_DIGEST_SIZE];
  2855. wc_test_ret_t ret = 0;
  2856. testVector a, b, c;
  2857. testVector test_sha[3];
  2858. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2859. a.input = "";
  2860. a.output = "\x6e\xd0\xdd\x02"
  2861. "\x80\x6f\xa8\x9e" "\x25\xde\x06\x0c"
  2862. "\x19\xd3\xac\x86" "\xca\xbb\x87\xd6"
  2863. "\xa0\xdd\xd0\x5c" "\x33\x3b\x84\xf4";
  2864. a.inLen = XSTRLEN(a.input);
  2865. a.outLen = WC_SHA512_224_DIGEST_SIZE;
  2866. /*
  2867. ** See page 1 in above document for the SHA512/224 "abc" test:
  2868. */
  2869. b.input = "abc";
  2870. /*
  2871. ** See page 1 in above document for the SHA512/224 "abc" test digest:
  2872. */
  2873. b.output = "\x46\x34\x27\x0f"
  2874. "\x70\x7b\x6a\x54" "\xda\xae\x75\x30"
  2875. "\x46\x08\x42\xe2" "\x0e\x37\xed\x26"
  2876. "\x5c\xee\xe9\xa4" "\x3e\x89\x24\xaa";
  2877. b.inLen = XSTRLEN(b.input);
  2878. b.outLen = WC_SHA512_224_DIGEST_SIZE;
  2879. /*
  2880. ** See page 4 in above for the 2-block test:
  2881. */
  2882. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  2883. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  2884. /*
  2885. ** see page 9 in above document for the 2-block test message digest:
  2886. */
  2887. c.output = "\x23\xfe\xc5\xbb"
  2888. "\x94\xd6\x0b\x23" "\x30\x81\x92\x64"
  2889. "\x0b\x0c\x45\x33" "\x35\xd6\x64\x73"
  2890. "\x4f\xe4\x0e\x72" "\x68\x67\x4a\xf9";
  2891. c.inLen = XSTRLEN(c.input);
  2892. c.outLen = WC_SHA512_224_DIGEST_SIZE;
  2893. test_sha[0] = a;
  2894. test_sha[1] = b;
  2895. test_sha[2] = c;
  2896. ret = wc_InitSha512_224_ex(&sha, HEAP_HINT, devId);
  2897. if (ret != 0)
  2898. return WC_TEST_RET_ENC_EC(ret);
  2899. ret = wc_InitSha512_224_ex(&shaCopy, HEAP_HINT, devId);
  2900. if (ret != 0) {
  2901. wc_Sha512_224Free(&sha);
  2902. return WC_TEST_RET_ENC_EC(ret);
  2903. }
  2904. for (i = 0; i < times; ++i) {
  2905. ret = wc_Sha512_224Update(&sha, (byte*)test_sha[i].input,
  2906. (word32)test_sha[i].inLen);
  2907. if (ret != 0)
  2908. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2909. ret = wc_Sha512_224GetHash(&sha, hashcopy);
  2910. if (ret != 0)
  2911. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2912. ret = wc_Sha512_224Copy(&sha, &shaCopy);
  2913. if (ret != 0)
  2914. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2915. ret = wc_Sha512_224Final(&sha, hash);
  2916. if (ret != 0)
  2917. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2918. wc_Sha512_224Free(&shaCopy);
  2919. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_224_DIGEST_SIZE) != 0)
  2920. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2921. if (XMEMCMP(hash, hashcopy, WC_SHA512_224_DIGEST_SIZE) != 0)
  2922. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  2923. }
  2924. #ifndef NO_LARGE_HASH_TEST
  2925. /* BEGIN LARGE HASH TEST */ {
  2926. byte large_input[1024];
  2927. #ifdef HASH_SIZE_LIMIT
  2928. const char* large_digest =
  2929. "\x98\x68\xc3\xd9\xb9\xef\x17\x53"
  2930. "\x43\x66\x0e\x60\xdf\x29\xf8\xef"
  2931. "\x96\xe3\x93\x34\x8c\x6f\xc0\xeb"
  2932. "\x14\x6c\xcf\x6a";
  2933. #else
  2934. const char* large_digest =
  2935. "\x26\x5f\x98\xd1\x76\x49\x71\x4e"
  2936. "\x82\xb7\x9d\x52\x32\x67\x9d\x56"
  2937. "\x91\xf5\x88\xc3\x05\xbb\x3f\x90"
  2938. "\xe2\x4e\x85\x05";
  2939. #endif
  2940. for (i = 0; i < (int)sizeof(large_input); i++) {
  2941. large_input[i] = (byte)(i & 0xFF);
  2942. }
  2943. #ifdef HASH_SIZE_LIMIT
  2944. times = 20;
  2945. #else
  2946. times = 100;
  2947. #endif
  2948. for (i = 0; i < times; ++i) {
  2949. ret = wc_Sha512_224Update(&sha, (byte*)large_input,
  2950. (word32)sizeof(large_input));
  2951. if (ret != 0)
  2952. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2953. }
  2954. ret = wc_Sha512_224Final(&sha, hash);
  2955. if (ret != 0)
  2956. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2957. if (XMEMCMP(hash, large_digest, WC_SHA512_224_DIGEST_SIZE) != 0)
  2958. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  2959. #ifndef NO_UNALIGNED_MEMORY_TEST
  2960. /* Unaligned memory access test */
  2961. for (i = 1; i < 16; i++) {
  2962. ret = wc_Sha512_224Update(&sha, (byte*)large_input + i,
  2963. (word32)sizeof(large_input) - i);
  2964. if (ret != 0)
  2965. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  2966. ret = wc_Sha512_224Final(&sha, hash);
  2967. }
  2968. #endif
  2969. } /* END LARGE HASH TEST */
  2970. #endif /* NO_LARGE_HASH_TEST */
  2971. exit:
  2972. wc_Sha512_224Free(&sha);
  2973. wc_Sha512_224Free(&shaCopy);
  2974. return ret;
  2975. } /* sha512_224_test */
  2976. #endif /* !defined(WOLFSSL_NOSHA512_224) && !FIPS ... */
  2977. #if !defined(WOLFSSL_NOSHA512_256) && \
  2978. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  2979. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha512_256_test(void)
  2980. {
  2981. /*
  2982. ** See https://csrc.nist.gov/Projects/cryptographic-standards-and-guidelines/example-values
  2983. ** NIST SHA512/256 Document Example:
  2984. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA512_256.pdf
  2985. */
  2986. wc_Sha512 sha, shaCopy;
  2987. byte hash[WC_SHA512_256_DIGEST_SIZE];
  2988. byte hashcopy[WC_SHA512_256_DIGEST_SIZE];
  2989. wc_test_ret_t ret = 0;
  2990. testVector a, b, c;
  2991. testVector test_sha[3];
  2992. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2993. a.input = "";
  2994. a.output = "\xc6\x72\xb8\xd1" "\xef\x56\xed\x28"
  2995. "\xab\x87\xc3\x62" "\x2c\x51\x14\x06"
  2996. "\x9b\xdd\x3a\xd7" "\xb8\xf9\x73\x74"
  2997. "\x98\xd0\xc0\x1e" "\xce\xf0\x96\x7a";
  2998. a.inLen = XSTRLEN(a.input);
  2999. a.outLen = WC_SHA512_256_DIGEST_SIZE;
  3000. /*
  3001. ** See page 1 of above document for "abc" example:
  3002. */
  3003. b.input = "abc";
  3004. /*
  3005. ** See page 4 of above document for "abc" example digest:
  3006. */
  3007. b.output = "\x53\x04\x8e\x26" "\x81\x94\x1e\xf9"
  3008. "\x9b\x2e\x29\xb7" "\x6b\x4c\x7d\xab"
  3009. "\xe4\xc2\xd0\xc6" "\x34\xfc\x6d\x46"
  3010. "\xe0\xe2\xf1\x31" "\x07\xe7\xaf\x23";
  3011. b.inLen = XSTRLEN(b.input);
  3012. b.outLen = WC_SHA512_256_DIGEST_SIZE;
  3013. /*
  3014. ** See page 4 of above document for Two Block Message Sample:
  3015. */
  3016. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  3017. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  3018. /*
  3019. ** See page 10 of above document for Two Block Message Sample digest:
  3020. */
  3021. c.output = "\x39\x28\xe1\x84" "\xfb\x86\x90\xf8"
  3022. "\x40\xda\x39\x88" "\x12\x1d\x31\xbe"
  3023. "\x65\xcb\x9d\x3e" "\xf8\x3e\xe6\x14"
  3024. "\x6f\xea\xc8\x61" "\xe1\x9b\x56\x3a";
  3025. c.inLen = XSTRLEN(c.input);
  3026. c.outLen = WC_SHA512_256_DIGEST_SIZE;
  3027. test_sha[0] = a;
  3028. test_sha[1] = b;
  3029. test_sha[2] = c;
  3030. ret = wc_InitSha512_256_ex(&sha, HEAP_HINT, devId);
  3031. if (ret != 0)
  3032. return WC_TEST_RET_ENC_EC(ret);
  3033. ret = wc_InitSha512_256_ex(&shaCopy, HEAP_HINT, devId);
  3034. if (ret != 0) {
  3035. wc_Sha512_256Free(&sha);
  3036. return WC_TEST_RET_ENC_EC(ret);
  3037. }
  3038. for (i = 0; i < times; ++i) {
  3039. ret = wc_Sha512_256Update(&sha, (byte*)test_sha[i].input,
  3040. (word32)test_sha[i].inLen);
  3041. if (ret != 0)
  3042. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3043. ret = wc_Sha512_256GetHash(&sha, hashcopy);
  3044. if (ret != 0)
  3045. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3046. ret = wc_Sha512_256Copy(&sha, &shaCopy);
  3047. if (ret != 0)
  3048. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3049. ret = wc_Sha512_256Final(&sha, hash);
  3050. if (ret != 0)
  3051. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3052. wc_Sha512_256Free(&shaCopy);
  3053. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_256_DIGEST_SIZE) != 0)
  3054. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3055. if (XMEMCMP(hash, hashcopy, WC_SHA512_256_DIGEST_SIZE) != 0)
  3056. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3057. }
  3058. #ifndef NO_LARGE_HASH_TEST
  3059. /* BEGIN LARGE HASH TEST */ {
  3060. byte large_input[1024];
  3061. #ifdef HASH_SIZE_LIMIT
  3062. const char* large_digest =
  3063. "\x49\xcc\xbc\x7a\x93\x0b\x02\xb8"
  3064. "\xad\x9a\x46\x51\x00\x1f\x13\x80"
  3065. "\x35\x84\x36\xf1\xf2\x3c\xeb\xd8"
  3066. "\x41\xd4\x06\x8b\x1d\x19\xad\x72";
  3067. #else
  3068. const char* large_digest =
  3069. "\x7a\xe3\x84\x05\xcb\x06\x22\x08"
  3070. "\x7e\x2c\x65\x89\x1f\x26\x45\xfd"
  3071. "\xad\xbc\x2e\x29\x83\x12\x84\x4b"
  3072. "\xf2\xa0\xde\xbe\x06\x11\xd7\x44";
  3073. #endif
  3074. for (i = 0; i < (int)sizeof(large_input); i++) {
  3075. large_input[i] = (byte)(i & 0xFF);
  3076. }
  3077. #ifdef HASH_SIZE_LIMIT
  3078. times = 20;
  3079. #else
  3080. times = 100;
  3081. #endif
  3082. for (i = 0; i < times; ++i) {
  3083. ret = wc_Sha512_256Update(&sha, (byte*)large_input,
  3084. (word32)sizeof(large_input));
  3085. if (ret != 0)
  3086. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3087. }
  3088. ret = wc_Sha512_256Final(&sha, hash);
  3089. if (ret != 0)
  3090. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3091. if (XMEMCMP(hash, large_digest, WC_SHA512_256_DIGEST_SIZE) != 0)
  3092. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3093. #ifndef NO_UNALIGNED_MEMORY_TEST
  3094. /* Unaligned memory access test */
  3095. for (i = 1; i < 16; i++) {
  3096. ret = wc_Sha512_256Update(&sha, (byte*)large_input + i,
  3097. (word32)sizeof(large_input) - i);
  3098. if (ret != 0)
  3099. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3100. ret = wc_Sha512_256Final(&sha, hash);
  3101. }
  3102. #endif
  3103. } /* END LARGE HASH TEST */
  3104. #endif /* NO_LARGE_HASH_TEST */
  3105. exit:
  3106. wc_Sha512_256Free(&sha);
  3107. wc_Sha512_256Free(&shaCopy);
  3108. return ret;
  3109. } /* sha512_256_test */
  3110. #endif /* !defined(WOLFSSL_NOSHA512_256) && !FIPS ... */
  3111. #endif /* WOLFSSL_SHA512 */
  3112. #ifdef WOLFSSL_SHA384
  3113. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha384_test(void)
  3114. {
  3115. wc_Sha384 sha, shaCopy;
  3116. byte hash[WC_SHA384_DIGEST_SIZE];
  3117. byte hashcopy[WC_SHA384_DIGEST_SIZE];
  3118. wc_test_ret_t ret = 0;
  3119. testVector a, b, c;
  3120. testVector test_sha[3];
  3121. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3122. a.input = "";
  3123. a.output = "\x38\xb0\x60\xa7\x51\xac\x96\x38\x4c\xd9\x32\x7e\xb1\xb1\xe3"
  3124. "\x6a\x21\xfd\xb7\x11\x14\xbe\x07\x43\x4c\x0c\xc7\xbf\x63\xf6"
  3125. "\xe1\xda\x27\x4e\xde\xbf\xe7\x6f\x65\xfb\xd5\x1a\xd2\xf1\x48"
  3126. "\x98\xb9\x5b";
  3127. a.inLen = XSTRLEN(a.input);
  3128. a.outLen = WC_SHA384_DIGEST_SIZE;
  3129. b.input = "abc";
  3130. b.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  3131. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  3132. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  3133. "\xc8\x25\xa7";
  3134. b.inLen = XSTRLEN(b.input);
  3135. b.outLen = WC_SHA384_DIGEST_SIZE;
  3136. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  3137. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  3138. c.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  3139. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  3140. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  3141. "\x74\x60\x39";
  3142. c.inLen = XSTRLEN(c.input);
  3143. c.outLen = WC_SHA384_DIGEST_SIZE;
  3144. test_sha[0] = a;
  3145. test_sha[1] = b;
  3146. test_sha[2] = c;
  3147. ret = wc_InitSha384_ex(&sha, HEAP_HINT, devId);
  3148. if (ret != 0)
  3149. return WC_TEST_RET_ENC_EC(ret);
  3150. ret = wc_InitSha384_ex(&shaCopy, HEAP_HINT, devId);
  3151. if (ret != 0) {
  3152. wc_Sha384Free(&sha);
  3153. return WC_TEST_RET_ENC_EC(ret);
  3154. }
  3155. for (i = 0; i < times; ++i) {
  3156. ret = wc_Sha384Update(&sha, (byte*)test_sha[i].input,
  3157. (word32)test_sha[i].inLen);
  3158. if (ret != 0)
  3159. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3160. ret = wc_Sha384GetHash(&sha, hashcopy);
  3161. if (ret != 0)
  3162. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3163. ret = wc_Sha384Copy(&sha, &shaCopy);
  3164. if (ret != 0)
  3165. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3166. ret = wc_Sha384Final(&sha, hash);
  3167. if (ret != 0)
  3168. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3169. wc_Sha384Free(&shaCopy);
  3170. if (XMEMCMP(hash, test_sha[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  3171. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3172. if (XMEMCMP(hash, hashcopy, WC_SHA384_DIGEST_SIZE) != 0)
  3173. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3174. }
  3175. #ifndef NO_LARGE_HASH_TEST
  3176. /* BEGIN LARGE HASH TEST */ {
  3177. byte large_input[1024];
  3178. #ifdef HASH_SIZE_LIMIT
  3179. const char* large_digest =
  3180. "\xB5\xAD\x66\x6F\xD9\x58\x5E\x68\xDD\x5E\x30\xD3\x95\x72\x33\xA4"
  3181. "\xE9\x4B\x99\x3A\xEF\xF8\xE1\xBF\x1F\x05\x32\xAA\x16\x00\x82\xEC"
  3182. "\x15\xDA\xF2\x75\xEE\xE9\x06\xAF\x52\x8A\x5C\xEF\x72\x81\x80\xD6";
  3183. #else
  3184. const char* large_digest =
  3185. "\x37\x01\xdb\xff\x1e\x40\x4f\xe1\xe2\xea\x0b\x40\xbb\x3b\x39\x9a"
  3186. "\xcc\xe8\x44\x8e\x7e\xe5\x64\xb5\x6b\x7f\x56\x64\xa7\x2b\x84\xe3"
  3187. "\xc5\xd7\x79\x03\x25\x90\xf7\xa4\x58\xcb\x97\xa8\x8b\xb1\xa4\x81";
  3188. #endif
  3189. for (i = 0; i < (int)sizeof(large_input); i++) {
  3190. large_input[i] = (byte)(i & 0xFF);
  3191. }
  3192. #ifdef HASH_SIZE_LIMIT
  3193. times = 20;
  3194. #else
  3195. times = 100;
  3196. #endif
  3197. for (i = 0; i < times; ++i) {
  3198. ret = wc_Sha384Update(&sha, (byte*)large_input,
  3199. (word32)sizeof(large_input));
  3200. if (ret != 0)
  3201. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3202. }
  3203. ret = wc_Sha384Final(&sha, hash);
  3204. if (ret != 0)
  3205. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3206. if (XMEMCMP(hash, large_digest, WC_SHA384_DIGEST_SIZE) != 0)
  3207. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3208. } /* END LARGE HASH TEST */
  3209. #endif /* NO_LARGE_HASH_TEST */
  3210. exit:
  3211. wc_Sha384Free(&sha);
  3212. wc_Sha384Free(&shaCopy);
  3213. return ret;
  3214. }
  3215. #endif /* WOLFSSL_SHA384 */
  3216. #ifdef WOLFSSL_SHA3
  3217. #ifndef WOLFSSL_NOSHA3_224
  3218. static wc_test_ret_t sha3_224_test(void)
  3219. {
  3220. wc_Sha3 sha;
  3221. byte hash[WC_SHA3_224_DIGEST_SIZE];
  3222. byte hashcopy[WC_SHA3_224_DIGEST_SIZE];
  3223. testVector a, b, c;
  3224. testVector test_sha[3];
  3225. wc_test_ret_t ret = 0;
  3226. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3227. a.input = "";
  3228. a.output = "\x6b\x4e\x03\x42\x36\x67\xdb\xb7\x3b\x6e\x15\x45\x4f\x0e\xb1"
  3229. "\xab\xd4\x59\x7f\x9a\x1b\x07\x8e\x3f\x5b\x5a\x6b\xc7";
  3230. a.inLen = XSTRLEN(a.input);
  3231. a.outLen = WC_SHA3_224_DIGEST_SIZE;
  3232. b.input = "abc";
  3233. b.output = "\xe6\x42\x82\x4c\x3f\x8c\xf2\x4a\xd0\x92\x34\xee\x7d\x3c\x76"
  3234. "\x6f\xc9\xa3\xa5\x16\x8d\x0c\x94\xad\x73\xb4\x6f\xdf";
  3235. b.inLen = XSTRLEN(b.input);
  3236. b.outLen = WC_SHA3_224_DIGEST_SIZE;
  3237. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3238. c.output = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55\x74\x49\x44\x79"
  3239. "\xba\x5c\x7e\x7a\xb7\x6e\xf2\x64\xea\xd0\xfc\xce\x33";
  3240. c.inLen = XSTRLEN(c.input);
  3241. c.outLen = WC_SHA3_224_DIGEST_SIZE;
  3242. test_sha[0] = a;
  3243. test_sha[1] = b;
  3244. test_sha[2] = c;
  3245. ret = wc_InitSha3_224(&sha, HEAP_HINT, devId);
  3246. if (ret != 0)
  3247. return WC_TEST_RET_ENC_EC(ret);
  3248. for (i = 0; i < times; ++i) {
  3249. ret = wc_Sha3_224_Update(&sha, (byte*)test_sha[i].input,
  3250. (word32)test_sha[i].inLen);
  3251. if (ret != 0)
  3252. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3253. ret = wc_Sha3_224_GetHash(&sha, hashcopy);
  3254. if (ret != 0)
  3255. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3256. ret = wc_Sha3_224_Final(&sha, hash);
  3257. if (ret != 0)
  3258. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3259. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_224_DIGEST_SIZE) != 0)
  3260. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3261. if (XMEMCMP(hash, hashcopy, WC_SHA3_224_DIGEST_SIZE) != 0)
  3262. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3263. }
  3264. #ifndef NO_LARGE_HASH_TEST
  3265. /* BEGIN LARGE HASH TEST */ {
  3266. byte large_input[1024];
  3267. const char* large_digest =
  3268. "\x13\xe5\xd3\x98\x7b\x94\xda\x41\x12\xc7\x1e\x92\x3a\x19"
  3269. "\x21\x20\x86\x6f\x24\xbf\x0a\x31\xbc\xfd\xd6\x70\x36\xf3";
  3270. for (i = 0; i < (int)sizeof(large_input); i++) {
  3271. large_input[i] = (byte)(i & 0xFF);
  3272. }
  3273. times = 100;
  3274. for (i = 0; i < times; ++i) {
  3275. ret = wc_Sha3_224_Update(&sha, (byte*)large_input,
  3276. (word32)sizeof(large_input));
  3277. if (ret != 0)
  3278. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3279. }
  3280. ret = wc_Sha3_224_Final(&sha, hash);
  3281. if (ret != 0)
  3282. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3283. if (XMEMCMP(hash, large_digest, WC_SHA3_224_DIGEST_SIZE) != 0)
  3284. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3285. } /* END LARGE HASH TEST */
  3286. #endif /* NO_LARGE_HASH_TEST */
  3287. exit:
  3288. wc_Sha3_224_Free(&sha);
  3289. return ret;
  3290. }
  3291. #endif /* WOLFSSL_NOSHA3_224 */
  3292. #ifndef WOLFSSL_NOSHA3_256
  3293. static wc_test_ret_t sha3_256_test(void)
  3294. {
  3295. wc_Sha3 sha;
  3296. byte hash[WC_SHA3_256_DIGEST_SIZE];
  3297. byte hashcopy[WC_SHA3_256_DIGEST_SIZE];
  3298. testVector a, b, c;
  3299. testVector test_sha[3];
  3300. wc_test_ret_t ret = 0;
  3301. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3302. byte large_input[1024];
  3303. const char* large_digest =
  3304. "\xdc\x90\xc0\xb1\x25\xdb\x2c\x34\x81\xa3\xff\xbc\x1e\x2e\x87\xeb"
  3305. "\x6d\x70\x85\x61\xe0\xe9\x63\x61\xff\xe5\x84\x4b\x1f\x68\x05\x15";
  3306. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  3307. /* test vector with hash of empty string */
  3308. const char* Keccak256EmptyOut =
  3309. "\xc5\xd2\x46\x01\x86\xf7\x23\x3c\x92\x7e\x7d\xb2\xdc\xc7\x03\xc0"
  3310. "\xe5\x00\xb6\x53\xca\x82\x27\x3b\x7b\xfa\xd8\x04\x5d\x85\xa4\x70";
  3311. #endif
  3312. /*
  3313. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-256_Msg0.pdf
  3314. */
  3315. a.input = "";
  3316. a.output = "\xa7\xff\xc6\xf8\xbf\x1e\xd7\x66\x51\xc1\x47\x56\xa0\x61\xd6"
  3317. "\x62\xf5\x80\xff\x4d\xe4\x3b\x49\xfa\x82\xd8\x0a\x4b\x80\xf8"
  3318. "\x43\x4a";
  3319. a.inLen = XSTRLEN(a.input);
  3320. a.outLen = WC_SHA3_256_DIGEST_SIZE;
  3321. b.input = "abc";
  3322. b.output = "\x3a\x98\x5d\xa7\x4f\xe2\x25\xb2\x04\x5c\x17\x2d\x6b\xd3\x90"
  3323. "\xbd\x85\x5f\x08\x6e\x3e\x9d\x52\x5b\x46\xbf\xe2\x45\x11\x43"
  3324. "\x15\x32";
  3325. b.inLen = XSTRLEN(b.input);
  3326. b.outLen = WC_SHA3_256_DIGEST_SIZE;
  3327. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3328. c.output = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8\x23\x5e"
  3329. "\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32\xdd\x97\x49\x6d"
  3330. "\x33\x76";
  3331. c.inLen = XSTRLEN(c.input);
  3332. c.outLen = WC_SHA3_256_DIGEST_SIZE;
  3333. test_sha[0] = a;
  3334. test_sha[1] = b;
  3335. test_sha[2] = c;
  3336. ret = wc_InitSha3_256(&sha, HEAP_HINT, devId);
  3337. if (ret != 0)
  3338. return WC_TEST_RET_ENC_EC(ret);
  3339. for (i = 0; i < times; ++i) {
  3340. ret = wc_Sha3_256_Update(&sha, (byte*)test_sha[i].input,
  3341. (word32)test_sha[i].inLen);
  3342. if (ret != 0)
  3343. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3344. ret = wc_Sha3_256_GetHash(&sha, hashcopy);
  3345. if (ret != 0)
  3346. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3347. ret = wc_Sha3_256_Final(&sha, hash);
  3348. if (ret != 0)
  3349. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3350. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_256_DIGEST_SIZE) != 0)
  3351. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3352. if (XMEMCMP(hash, hashcopy, WC_SHA3_256_DIGEST_SIZE) != 0)
  3353. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3354. }
  3355. #ifndef NO_LARGE_HASH_TEST
  3356. /* BEGIN LARGE HASH TEST */ {
  3357. for (i = 0; i < (int)sizeof(large_input); i++) {
  3358. large_input[i] = (byte)(i & 0xFF);
  3359. }
  3360. times = 100;
  3361. for (i = 0; i < times; ++i) {
  3362. ret = wc_Sha3_256_Update(&sha, (byte*)large_input,
  3363. (word32)sizeof(large_input));
  3364. if (ret != 0)
  3365. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3366. }
  3367. ret = wc_Sha3_256_Final(&sha, hash);
  3368. if (ret != 0)
  3369. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3370. if (XMEMCMP(hash, large_digest, WC_SHA3_256_DIGEST_SIZE) != 0)
  3371. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3372. } /* END LARGE HASH TEST */
  3373. #endif /* NO_LARGE_HASH_TEST */
  3374. /* this is a software only variant of SHA3 not supported by external hardware devices */
  3375. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  3376. /* Test for Keccak256 */
  3377. ret = wc_Sha3_SetFlags(&sha, WC_HASH_SHA3_KECCAK256);
  3378. if (ret != 0) {
  3379. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3380. }
  3381. ret = wc_Sha3_256_Update(&sha, (byte*)"", 0);
  3382. if (ret != 0) {
  3383. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3384. }
  3385. ret = wc_Sha3_256_Final(&sha, hash);
  3386. if (ret != 0) {
  3387. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3388. }
  3389. if (XMEMCMP(hash, Keccak256EmptyOut, WC_SHA3_256_DIGEST_SIZE) != 0) {
  3390. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3391. }
  3392. #endif /* WOLFSSL_HASH_FLAGS && !WOLFSSL_ASYNC_CRYPT */
  3393. exit:
  3394. wc_Sha3_256_Free(&sha);
  3395. return ret;
  3396. }
  3397. #endif /* WOLFSSL_NOSHA3_256 */
  3398. #ifndef WOLFSSL_NOSHA3_384
  3399. static wc_test_ret_t sha3_384_test(void)
  3400. {
  3401. wc_Sha3 sha;
  3402. byte hash[WC_SHA3_384_DIGEST_SIZE];
  3403. byte buf[64];
  3404. #ifndef NO_INTM_HASH_TEST
  3405. byte hashcopy[WC_SHA3_384_DIGEST_SIZE];
  3406. #endif
  3407. testVector a, b, c;
  3408. testVector test_sha[3];
  3409. wc_test_ret_t ret;
  3410. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3411. /*
  3412. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-384_Msg0.pdf
  3413. */
  3414. a.input = "";
  3415. a.output = "\x0c\x63\xa7\x5b\x84\x5e\x4f\x7d\x01\x10\x7d\x85\x2e\x4c\x24"
  3416. "\x85\xc5\x1a\x50\xaa\xaa\x94\xfc\x61\x99\x5e\x71\xbb\xee\x98"
  3417. "\x3a\x2a\xc3\x71\x38\x31\x26\x4a\xdb\x47\xfb\x6b\xd1\xe0\x58"
  3418. "\xd5\xf0\x04";
  3419. a.inLen = XSTRLEN(a.input);
  3420. a.outLen = WC_SHA3_384_DIGEST_SIZE;
  3421. #if defined(WOLFSSL_AFALG_XILINX_SHA3) || defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  3422. /* NIST test vector with a length that is a multiple of 4 */
  3423. b.input = "\x7d\x80\xb1\x60\xc4\xb5\x36\xa3\xbe\xb7\x99\x80\x59\x93\x44"
  3424. "\x04\x7c\x5f\x82\xa1\xdf\xc3\xee\xd4";
  3425. b.output = "\x04\x1c\xc5\x86\x1b\xa3\x34\x56\x3c\x61\xd4\xef\x97\x10\xd4"
  3426. "\x89\x6c\x31\x1c\x92\xed\xbe\x0d\x7c\xd5\x3e\x80\x3b\xf2\xf4"
  3427. "\xeb\x60\x57\x23\x55\x70\x77\x0c\xe8\x7c\x55\x20\xd7\xec\x14"
  3428. "\x19\x87\x22";
  3429. b.inLen = XSTRLEN(b.input);
  3430. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  3431. #else
  3432. b.input = "abc";
  3433. b.output = "\xec\x01\x49\x82\x88\x51\x6f\xc9\x26\x45\x9f\x58\xe2\xc6\xad"
  3434. "\x8d\xf9\xb4\x73\xcb\x0f\xc0\x8c\x25\x96\xda\x7c\xf0\xe4\x9b"
  3435. "\xe4\xb2\x98\xd8\x8c\xea\x92\x7a\xc7\xf5\x39\xf1\xed\xf2\x28"
  3436. "\x37\x6d\x25";
  3437. b.inLen = XSTRLEN(b.input);
  3438. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  3439. #endif
  3440. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3441. c.output = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7\x8a\x49"
  3442. "\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd\xbc\x32\xb9\xd4"
  3443. "\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe\xa1\x9e\xef\x51\xac\xd0"
  3444. "\x65\x7c\x22";
  3445. c.inLen = XSTRLEN(c.input);
  3446. c.outLen = WC_SHA3_384_DIGEST_SIZE;
  3447. #if defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  3448. test_sha[0] = b; /* hardware acc. pre-Versal can not handle "" string */
  3449. #else
  3450. test_sha[0] = a;
  3451. #endif
  3452. test_sha[1] = b;
  3453. test_sha[2] = c;
  3454. ret = wc_InitSha3_384(&sha, HEAP_HINT, devId);
  3455. if (ret != 0)
  3456. return WC_TEST_RET_ENC_EC(ret);
  3457. for (i = 0; i < times; ++i) {
  3458. XMEMCPY(buf, test_sha[i].input, test_sha[i].inLen);
  3459. ret = wc_Sha3_384_Update(&sha, buf,
  3460. (word32)test_sha[i].inLen);
  3461. if (ret != 0)
  3462. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3463. #ifndef NO_INTM_HASH_TEST
  3464. ret = wc_Sha3_384_GetHash(&sha, hashcopy);
  3465. if (ret != 0)
  3466. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3467. #endif
  3468. ret = wc_Sha3_384_Final(&sha, hash);
  3469. if (ret != 0)
  3470. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3471. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_384_DIGEST_SIZE) != 0)
  3472. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3473. #ifndef NO_INTM_HASH_TEST
  3474. if (XMEMCMP(hash, hashcopy, WC_SHA3_384_DIGEST_SIZE) != 0)
  3475. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3476. #endif
  3477. }
  3478. #ifndef NO_LARGE_HASH_TEST
  3479. /* BEGIN LARGE HASH TEST */ {
  3480. byte large_input[1024];
  3481. const char* large_digest =
  3482. "\x30\x44\xec\x17\xef\x47\x9f\x55\x36\x11\xd6\x3f\x8a\x31\x5a\x71"
  3483. "\x8a\x71\xa7\x1d\x8e\x84\xe8\x6c\x24\x02\x2f\x7a\x08\x4e\xea\xd7"
  3484. "\x42\x36\x5d\xa8\xc2\xb7\x42\xad\xec\x19\xfb\xca\xc6\x64\xb3\xa4";
  3485. for (i = 0; i < (int)sizeof(large_input); i++) {
  3486. large_input[i] = (byte)(i & 0xFF);
  3487. }
  3488. times = 100;
  3489. for (i = 0; i < times; ++i) {
  3490. ret = wc_Sha3_384_Update(&sha, (byte*)large_input,
  3491. (word32)sizeof(large_input));
  3492. if (ret != 0)
  3493. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3494. }
  3495. ret = wc_Sha3_384_Final(&sha, hash);
  3496. if (ret != 0)
  3497. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3498. if (XMEMCMP(hash, large_digest, WC_SHA3_384_DIGEST_SIZE) != 0)
  3499. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3500. } /* END LARGE HASH TEST */
  3501. #endif /* NO_LARGE_HASH_TEST */
  3502. exit:
  3503. wc_Sha3_384_Free(&sha);
  3504. return ret;
  3505. }
  3506. #endif /* WOLFSSL_NOSHA3_384 */
  3507. #ifndef WOLFSSL_NOSHA3_512
  3508. static wc_test_ret_t sha3_512_test(void)
  3509. {
  3510. wc_Sha3 sha;
  3511. byte hash[WC_SHA3_512_DIGEST_SIZE];
  3512. byte hashcopy[WC_SHA3_512_DIGEST_SIZE];
  3513. testVector a, b, c;
  3514. testVector test_sha[3];
  3515. wc_test_ret_t ret;
  3516. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3517. /*
  3518. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-512_Msg0.pdf
  3519. */
  3520. a.input = "";
  3521. a.output = "\xa6\x9f\x73\xcc\xa2\x3a\x9a\xc5\xc8\xb5\x67\xdc\x18\x5a\x75"
  3522. "\x6e\x97\xc9\x82\x16\x4f\xe2\x58\x59\xe0\xd1\xdc\xc1\x47\x5c"
  3523. "\x80\xa6\x15\xb2\x12\x3a\xf1\xf5\xf9\x4c\x11\xe3\xe9\x40\x2c"
  3524. "\x3a\xc5\x58\xf5\x00\x19\x9d\x95\xb6\xd3\xe3\x01\x75\x85\x86"
  3525. "\x28\x1d\xcd\x26";
  3526. a.inLen = XSTRLEN(a.input);
  3527. a.outLen = WC_SHA3_512_DIGEST_SIZE;
  3528. b.input = "abc";
  3529. b.output = "\xb7\x51\x85\x0b\x1a\x57\x16\x8a\x56\x93\xcd\x92\x4b\x6b\x09"
  3530. "\x6e\x08\xf6\x21\x82\x74\x44\xf7\x0d\x88\x4f\x5d\x02\x40\xd2"
  3531. "\x71\x2e\x10\xe1\x16\xe9\x19\x2a\xf3\xc9\x1a\x7e\xc5\x76\x47"
  3532. "\xe3\x93\x40\x57\x34\x0b\x4c\xf4\x08\xd5\xa5\x65\x92\xf8\x27"
  3533. "\x4e\xec\x53\xf0";
  3534. b.inLen = XSTRLEN(b.input);
  3535. b.outLen = WC_SHA3_512_DIGEST_SIZE;
  3536. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3537. c.output = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10\xfc\xa8"
  3538. "\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7\xec\x2f\x1e\x91"
  3539. "\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53\x02\xba\x1b\x0d\x8d\xc7"
  3540. "\x8c\x08\x63\x46\xb5\x33\xb4\x9c\x03\x0d\x99\xa2\x7d\xaf\x11"
  3541. "\x39\xd6\xe7\x5e";
  3542. c.inLen = XSTRLEN(c.input);
  3543. c.outLen = WC_SHA3_512_DIGEST_SIZE;
  3544. test_sha[0] = a;
  3545. test_sha[1] = b;
  3546. test_sha[2] = c;
  3547. ret = wc_InitSha3_512(&sha, HEAP_HINT, devId);
  3548. if (ret != 0)
  3549. return WC_TEST_RET_ENC_EC(ret);
  3550. for (i = 0; i < times; ++i) {
  3551. ret = wc_Sha3_512_Update(&sha, (byte*)test_sha[i].input,
  3552. (word32)test_sha[i].inLen);
  3553. if (ret != 0)
  3554. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3555. ret = wc_Sha3_512_GetHash(&sha, hashcopy);
  3556. if (ret != 0)
  3557. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3558. ret = wc_Sha3_512_Final(&sha, hash);
  3559. if (ret != 0)
  3560. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3561. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_512_DIGEST_SIZE) != 0)
  3562. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3563. if (XMEMCMP(hash, hashcopy, WC_SHA3_512_DIGEST_SIZE) != 0)
  3564. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3565. }
  3566. #ifndef NO_LARGE_HASH_TEST
  3567. /* BEGIN LARGE HASH TEST */ {
  3568. byte large_input[1024];
  3569. const char* large_digest =
  3570. "\x9c\x13\x26\xb6\x26\xb2\x94\x31\xbc\xf4\x34\xe9\x6f\xf2\xd6\x29"
  3571. "\x9a\xd0\x9b\x32\x63\x2f\x18\xa7\x5f\x23\xc9\x60\xc2\x32\x0c\xbc"
  3572. "\x57\x77\x33\xf1\x83\x81\x8a\xd3\x15\x7c\x93\xdc\x80\x9f\xed\x61"
  3573. "\x41\xa7\x5b\xfd\x32\x0e\x38\x15\xb0\x46\x3b\x7a\x4f\xfd\x44\x88";
  3574. for (i = 0; i < (int)sizeof(large_input); i++) {
  3575. large_input[i] = (byte)(i & 0xFF);
  3576. }
  3577. times = 100;
  3578. for (i = 0; i < times; ++i) {
  3579. ret = wc_Sha3_512_Update(&sha, (byte*)large_input,
  3580. (word32)sizeof(large_input));
  3581. if (ret != 0)
  3582. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3583. }
  3584. ret = wc_Sha3_512_Final(&sha, hash);
  3585. if (ret != 0)
  3586. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3587. if (XMEMCMP(hash, large_digest, WC_SHA3_512_DIGEST_SIZE) != 0)
  3588. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3589. } /* END LARGE HASH TEST */
  3590. #endif /* NO_LARGE_HASH_TEST */
  3591. exit:
  3592. wc_Sha3_512_Free(&sha);
  3593. return ret;
  3594. }
  3595. #endif /* WOLFSSL_NOSHA3_512 */
  3596. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sha3_test(void)
  3597. {
  3598. wc_test_ret_t ret;
  3599. (void)ret;
  3600. #ifndef WOLFSSL_NOSHA3_224
  3601. if ((ret = sha3_224_test()) != 0)
  3602. return ret;
  3603. #endif
  3604. #ifndef WOLFSSL_NOSHA3_256
  3605. if ((ret = sha3_256_test()) != 0)
  3606. return ret;
  3607. #endif
  3608. #ifndef WOLFSSL_NOSHA3_384
  3609. if ((ret = sha3_384_test()) != 0)
  3610. return ret;
  3611. #endif
  3612. #ifndef WOLFSSL_NOSHA3_512
  3613. if ((ret = sha3_512_test()) != 0)
  3614. return ret;
  3615. #endif
  3616. return 0;
  3617. }
  3618. #endif /* WOLFSSL_SHA3 */
  3619. #ifdef WOLFSSL_SHAKE128
  3620. static wc_test_ret_t shake128_absorb_test(wc_Shake* sha, byte *large_input_buf,
  3621. size_t large_input_buf_size)
  3622. {
  3623. testVector a, b, c, d, e;
  3624. testVector test_sha[5];
  3625. wc_test_ret_t ret = 0;
  3626. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3627. static const char large_digest[] =
  3628. "\x2b\xd1\x69\x9f\xb3\x75\x40\x74\xb8\xb2\xd2\x0b\x92\x47\x9b\xfe"
  3629. "\xc9\x91\x48\xbe\xda\xa4\x09\xd7\x61\x35\x18\x05\x07\x71\xa5\x61"
  3630. "\x4d\xc4\x94\xad\xbe\x04\x7d\xad\x95\x2f\xeb\x2c\xc0\x10\x67\x43"
  3631. "\x40\xf1\x4a\x58\x1c\x54\xfa\x24\x1c\x1a\x4e\x8d\x9b\xbc\xea\xa7"
  3632. "\x32\xf2\x4c\xc7\x86\x05\x36\xdc\xb4\x42\xd8\x35\xd1\xb4\xa2\x79"
  3633. "\xa2\xe6\xee\x67\x4f\xbf\x2a\x93\x41\x88\x25\x56\x29\x90\x1a\x06"
  3634. "\xba\xfe\x9f\xa6\x1a\x74\xe8\x7e\x85\x4a\xc8\x58\x60\xb1\x7b\x18"
  3635. "\xdf\x77\x59\x46\x04\xc1\xff\x4b\x9b\xcb\xad\xfe\x91\x28\xf0\x01"
  3636. "\xc1\x33\xd0\x99\x99\x2e\x0c\x86\x84\x67\x4d\x37\xa4\x42\x45\x10"
  3637. "\xdc\x8f\xdb\x6f\xa6\x9b\xee\x8a\x60\xa5\x1f\x95\x3f\x8f\xf5\x31"
  3638. "\x4b\x1d\x48\x1e\x45\xff\x79\x5c\xbe\x72\xfc\x56\xed\x6d\x1a\x99"
  3639. "\x7f\x23\x7c\xd1\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b"
  3640. "\xa3\x60\x51\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41"
  3641. "\x77\x2c\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59"
  3642. "\x0d\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  3643. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4\xcb\x87\xd8\xb7\x9d\xa8"
  3644. "\xbf\xc5\x2e\x5e\xfc\xd3\x6c\x45\xd4\x5d\x72\x0f\x66\xeb\x67\x86"
  3645. "\xfa\x6c\xd6\x80\xa4\x23\xcb\x5d\xed\x3c\xde\xdc\x5b\x3d\xca\x95"
  3646. "\x43\x4b\xdc\xe8\x49\xd3\xe1\x01\xd4\xf1\xe4\x47\xcf\x56\xba\x71"
  3647. "\xb4\x69\xed\xe7\xdb\x0f\x89\xd6\xbb\xcd\x1a\xff\xb4\xbe\x72\x26"
  3648. "\xdc\x76\x79\xb3\x1a\x4b\xe6\x8d\x9b\x8e\xd9\xe9\xe6\xf9\xff\xa5";
  3649. byte hash[sizeof(large_digest) - 1];
  3650. /*
  3651. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE128_Msg0.pdf
  3652. */
  3653. a.input = "";
  3654. a.output = "\x7f\x9c\x2b\xa4\xe8\x8f\x82\x7d\x61\x60\x45\x50\x76\x05\x85"
  3655. "\x3e\xd7\x3b\x80\x93\xf6\xef\xbc\x88\xeb\x1a\x6e\xac\xfa\x66"
  3656. "\xef\x26\x3c\xb1\xee\xa9\x88\x00\x4b\x93\x10\x3c\xfb\x0a\xee"
  3657. "\xfd\x2a\x68\x6e\x01\xfa\x4a\x58\xe8\xa3\x63\x9c\xa8\xa1\xe3"
  3658. "\xf9\xae\x57\xe2\x35\xb8\xcc\x87\x3c\x23\xdc\x62\xb8\xd2\x60"
  3659. "\x16\x9a\xfa\x2f\x75\xab\x91\x6a\x58\xd9\x74\x91\x88\x35\xd2"
  3660. "\x5e\x6a\x43\x50\x85\xb2\xba\xdf\xd6\xdf\xaa\xc3\x59\xa5\xef"
  3661. "\xbb\x7b\xcc\x4b\x59\xd5\x38\xdf\x9a\x04\x30\x2e\x10\xc8\xbc"
  3662. "\x1c\xbf\x1a\x0b\x3a\x51\x20\xea\x17\xcd\xa7\xcf\xad\x76\x5f"
  3663. "\x56\x23\x47\x4d\x36\x8c\xcc\xa8\xaf\x00\x07\xcd\x9f\x5e\x4c"
  3664. "\x84\x9f\x16\x7a\x58\x0b\x14\xaa\xbd\xef\xae\xe7\xee\xf4\x7c"
  3665. "\xb0\xfc\xa9";
  3666. a.inLen = XSTRLEN(a.input);
  3667. a.outLen = WC_SHA3_128_BLOCK_SIZE;
  3668. b.input = "abc";
  3669. b.output = "\x58\x81\x09\x2d\xd8\x18\xbf\x5c\xf8\xa3\xdd\xb7\x93\xfb\xcb"
  3670. "\xa7\x40\x97\xd5\xc5\x26\xa6\xd3\x5f\x97\xb8\x33\x51\x94\x0f"
  3671. "\x2c\xc8\x44\xc5\x0a\xf3\x2a\xcd\x3f\x2c\xdd\x06\x65\x68\x70"
  3672. "\x6f\x50\x9b\xc1\xbd\xde\x58\x29\x5d\xae\x3f\x89\x1a\x9a\x0f"
  3673. "\xca\x57\x83\x78\x9a\x41\xf8\x61\x12\x14\xce\x61\x23\x94\xdf"
  3674. "\x28\x6a\x62\xd1\xa2\x25\x2a\xa9\x4d\xb9\xc5\x38\x95\x6c\x71"
  3675. "\x7d\xc2\xbe\xd4\xf2\x32\xa0\x29\x4c\x85\x7c\x73\x0a\xa1\x60"
  3676. "\x67\xac\x10\x62\xf1\x20\x1f\xb0\xd3\x77\xcf\xb9\xcd\xe4\xc6"
  3677. "\x35\x99\xb2\x7f\x34\x62\xbb\xa4\xa0\xed\x29\x6c\x80\x1f\x9f"
  3678. "\xf7\xf5\x73\x02\xbb\x30\x76\xee\x14\x5f\x97\xa3\x2a\xe6\x8e"
  3679. "\x76\xab\x66\xc4\x8d\x51\x67\x5b\xd4\x9a\xcc\x29\x08\x2f\x56"
  3680. "\x47\x58\x4e";
  3681. b.inLen = XSTRLEN(b.input);
  3682. b.outLen = WC_SHA3_128_BLOCK_SIZE;
  3683. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3684. c.output = "\x1a\x96\x18\x2b\x50\xfb\x8c\x7e\x74\xe0\xa7\x07\x78\x8f\x55"
  3685. "\xe9\x82\x09\xb8\xd9\x1f\xad\xe8\xf3\x2f\x8d\xd5\xcf\xf7\xbf"
  3686. "\x21\xf5\x4e\xe5\xf1\x95\x50\x82\x5a\x6e\x07\x00\x30\x51\x9e"
  3687. "\x94\x42\x63\xac\x1c\x67\x65\x28\x70\x65\x62\x1f\x9f\xcb\x32"
  3688. "\x01\x72\x3e\x32\x23\xb6\x3a\x46\xc2\x93\x8a\xa9\x53\xba\x84"
  3689. "\x01\xd0\xea\x77\xb8\xd2\x64\x90\x77\x55\x66\x40\x7b\x95\x67"
  3690. "\x3c\x0f\x4c\xc1\xce\x9f\xd9\x66\x14\x8d\x7e\xfd\xff\x26\xbb"
  3691. "\xf9\xf4\x8a\x21\xc6\xda\x35\xbf\xaa\x54\x56\x54\xf7\x0a\xe5"
  3692. "\x86\xff\x10\x13\x14\x20\x77\x14\x83\xec\x92\xed\xab\x40\x8c"
  3693. "\x76\x7b\xf4\xc5\xb4\xff\xfa\xa8\x0c\x8c\xa2\x14\xd8\x4c\x4d"
  3694. "\xc7\x00\xd0\xc5\x06\x30\xb2\xff\xc3\x79\x3e\xa4\xd8\x72\x58"
  3695. "\xb4\xc9\x54";
  3696. c.inLen = XSTRLEN(c.input);
  3697. c.outLen = WC_SHA3_128_BLOCK_SIZE;
  3698. /* Taken from NIST CAVP test vectors - full rate output. */
  3699. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  3700. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  3701. "\x85\xe0";
  3702. d.output = "\x93\x68\xf0\x15\x10\x92\x44\xeb\x02\x47\xfa\x3a\x0e\x57\xf5"
  3703. "\x2e\xa7\xd9\xeb\xa2\x3d\xae\x7a\x19\x7f\x0a\x29\xe9\x22\x55"
  3704. "\x06\x05\x98\x16\xb7\x84\x48\xb6\x49\x7a\x76\xeb\x96\x2d\xb3"
  3705. "\xf8\x4d\x37\x60\xf1\xfe\xb4\xbd\xc1\xfd\x4a\xc9\x4e\x91\x7a"
  3706. "\xc2\xea\x5e\x4f\x38\x37\x4a\xa5\x6e\x4f\x47\x67\xb8\xd7\x83"
  3707. "\x1b\x2d\x51\x49\x5a\xb8\xea\xb7\xc9\x82\x20\xaf\x13\x41\x5a"
  3708. "\x59\xbb\x7c\x17\x7a\xcd\x62\x8e\xf0\xff\xe3\x6c\xeb\x18\x59"
  3709. "\x5d\x14\x4c\xbf\x25\xef\xc0\x6c\xd9\x56\xa5\x78\x20\x6e\xa8"
  3710. "\xf9\x14\x5e\xf9\xce\x19\x50\x6a\x9d\x04\x4e\xc7\x00\x79\x9f"
  3711. "\xa1\x41\x9b\xaf\x60\x52\xc0\xc1\xb4\x45\xf8\x35\x17\x57\xb0"
  3712. "\xd0\x22\x87\x21\x89\xe2\xc0\x27\x3f\x82\xd9\x69\x69\x66\x3e"
  3713. "\x55\x4d\x09";
  3714. d.inLen = 32;
  3715. d.outLen = WC_SHA3_128_BLOCK_SIZE;
  3716. /* Taken from NIST CAVP test vectors - more than one output block. */
  3717. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  3718. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  3719. "\x47\xe4";
  3720. e.output = "\xe1\x7e\xab\x0d\xa4\x04\xf9\xb6\xac\xc0\x84\x97\x2f\xc5\x79"
  3721. "\xe8\x6d\xaa\x76\x10\xa5\xe1\x7c\x23\x2f\x79\x19\x83\x96\xfd"
  3722. "\x01\xc2\x4c\x34\xbb\x54\xf4\xb0\x1e\xf7\x40\xb4\x25\x33\x4a"
  3723. "\x55\xdd\x24\x81\x3d\xc8\xea\x86\xf5\x6e\xf7\x27\x67\x26\x2b"
  3724. "\xf2\x25\x74\x8c\xcc\x3d\x9f\x48\x6f\xfb\x72\x8f\x4e\xad\x29"
  3725. "\x60\xc9\x6c\x3e\x44\x63\x86\xea\xce\x21\x9c\x84\x28\x16\x11"
  3726. "\x63\x58\xb0\xf4\x2d\x7d\xff\xf7\xdd\x24\x11\xfa\x2a\x56\x79"
  3727. "\xfd\x7a\x94\x77\x45\x75\xba\xf9\xfc\xad\x68\xa1\x9e\x30\xd1"
  3728. "\x49\xb0\x59\xb5\x9c\x44\x6c\x4e\xdc\xa5\x9b\xc5\xa4\x79\x9d"
  3729. "\xc4\x65\xaa\x9e\x78\x2c\xed\x9f\x21\xc5\x5d\xe2\x42\xdd\x25"
  3730. "\xd0\xd9\xde\x60\xd0\x9f\xf8\x6a\xba\xf3\xa0\x3a\x76\x71\xb3"
  3731. "\x05\x42\xdf\xbe\x72\xfc\x56\xed\x6d\x1a\x99\x7f\x23\x7c\xd1"
  3732. "\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b\xa3\x60\x51"
  3733. "\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41\x77\x2c"
  3734. "\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59\x0d"
  3735. "\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  3736. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4\xcb\x87\xd8\xb7\x9d"
  3737. "\xa8\xbf\xc5\x2e\x5e\xfc\xd3\x6c\x45\xd4\x5d\x72\x0f\x66\xeb"
  3738. "\x67\x86\xfa\x6c\xd6\x80\xa4\x23\xcb\x5d\xed\x3c\xde\xdc\x5b"
  3739. "\x3d\xca\x95\x43\x4b\xdc\xe8\x49\xd3\xe1\x01\xd4\xf1\xe4\x47"
  3740. "\xcf\x56\xba\x71\xb4\x69\xed\xe7\xdb\x0f\x89\xd6\xbb\xcd\x1a"
  3741. "\xff\xb4\xbe\x72\x26\xdc\x76\x79\xb3\x1a\x4b\xe6\x8d\x9b\x8e"
  3742. "\xd9\xe9\xe6\xf9\xff\xa5";
  3743. e.inLen = 32;
  3744. e.outLen = 2 * WC_SHA3_128_BLOCK_SIZE;
  3745. test_sha[0] = a;
  3746. test_sha[1] = b;
  3747. test_sha[2] = c;
  3748. test_sha[3] = d;
  3749. test_sha[4] = e;
  3750. for (i = 0; i < times; ++i) {
  3751. ret = wc_InitShake128(sha, HEAP_HINT, devId);
  3752. if (ret != 0)
  3753. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3754. ret = wc_Shake128_Absorb(sha, (byte*)test_sha[i].input,
  3755. (word32)test_sha[i].inLen);
  3756. if (ret != 0)
  3757. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3758. ret = wc_Shake128_SqueezeBlocks(sha, hash,
  3759. (word32)test_sha[i].outLen / WC_SHA3_128_BLOCK_SIZE);
  3760. if (ret != 0)
  3761. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3762. if (XMEMCMP(hash, test_sha[i].output, (word32)test_sha[i].outLen) != 0)
  3763. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3764. }
  3765. #ifndef NO_LARGE_HASH_TEST
  3766. /* BEGIN LARGE HASH TEST */ {
  3767. for (i = 0; i < (int)large_input_buf_size; i++) {
  3768. large_input_buf[i] = (byte)(i & 0xFF);
  3769. }
  3770. ret = wc_InitShake128(sha, HEAP_HINT, devId);
  3771. if (ret != 0)
  3772. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3773. /* Absorb is non-incremental. */
  3774. ret = wc_Shake128_Absorb(sha, (byte*)large_input_buf,
  3775. (word32)large_input_buf_size);
  3776. if (ret != 0)
  3777. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3778. /* Able to squeeze out blocks incrementally. */
  3779. ret = wc_Shake128_SqueezeBlocks(sha, hash, 1);
  3780. if (ret != 0)
  3781. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3782. ret = wc_Shake128_SqueezeBlocks(sha, hash,
  3783. ((word32)sizeof(hash) / WC_SHA3_128_BLOCK_SIZE) - 1);
  3784. if (ret != 0)
  3785. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3786. if (XMEMCMP(hash, large_digest, sizeof(hash)) != 0)
  3787. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3788. } /* END LARGE HASH TEST */
  3789. #endif /* NO_LARGE_HASH_TEST */
  3790. exit:
  3791. return ret;
  3792. }
  3793. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake128_test(void)
  3794. {
  3795. wc_Shake sha;
  3796. byte hash[250];
  3797. testVector a, b, c, d, e;
  3798. testVector test_sha[5];
  3799. wc_test_ret_t ret = 0;
  3800. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3801. #define SHAKE128_LARGE_INPUT_BUFSIZ 1024
  3802. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  3803. byte *large_input = NULL;
  3804. #else
  3805. byte large_input[SHAKE128_LARGE_INPUT_BUFSIZ];
  3806. #endif
  3807. static const char large_digest[] =
  3808. "\x88\xd7\x0e\x86\x46\x72\x6b\x3d\x7d\x22\xe1\xa9\x2d\x02\xdb\x35"
  3809. "\x92\x4f\x1b\x03\x90\xee\xa3\xce\xd1\x3a\x08\x3a\xd7\x4e\x10\xdf"
  3810. "\x09\x67\x33\x35\x4f\xdd\x38\x50\x5b\xcb\x75\xc7\xba\x65\xe5\xe8"
  3811. "\xb8\x76\xde\xc5\xee\xd7\xf1\x65\x93\x4e\x5e\xc4\xb1\xd7\x6b\xee"
  3812. "\x4b\x57\x48\xf5\x38\x49\x9e\x45\xa0\xf7\x32\xe9\x05\x26\x6a\x10"
  3813. "\x70\xd4\x7c\x19\x01\x1f\x6d\x37\xba\x7b\x74\xc2\xbc\xb6\xbc\x74"
  3814. "\xa3\x66\x6c\x9b\x11\x84\x9d\x4a\x36\xbc\x8a\x0d\x4c\xe3\x39\xfa"
  3815. "\xfa\x1b";
  3816. /*
  3817. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE128_Msg0.pdf
  3818. */
  3819. a.input = "";
  3820. a.output = "\x7f\x9c\x2b\xa4\xe8\x8f\x82\x7d\x61\x60\x45\x50\x76\x05\x85"
  3821. "\x3e\xd7\x3b\x80\x93\xf6\xef\xbc\x88\xeb\x1a\x6e\xac\xfa\x66"
  3822. "\xef\x26\x3c\xb1\xee\xa9\x88\x00\x4b\x93\x10\x3c\xfb\x0a\xee"
  3823. "\xfd\x2a\x68\x6e\x01\xfa\x4a\x58\xe8\xa3\x63\x9c\xa8\xa1\xe3"
  3824. "\xf9\xae\x57\xe2\x35\xb8\xcc\x87\x3c\x23\xdc\x62\xb8\xd2\x60"
  3825. "\x16\x9a\xfa\x2f\x75\xab\x91\x6a\x58\xd9\x74\x91\x88\x35\xd2"
  3826. "\x5e\x6a\x43\x50\x85\xb2\xba\xdf\xd6\xdf\xaa\xc3\x59\xa5\xef"
  3827. "\xbb\x7b\xcc\x4b\x59\xd5\x38\xdf\x9a";
  3828. a.inLen = XSTRLEN(a.input);
  3829. a.outLen = 114;
  3830. b.input = "abc";
  3831. b.output = "\x58\x81\x09\x2d\xd8\x18\xbf\x5c\xf8\xa3\xdd\xb7\x93\xfb\xcb"
  3832. "\xa7\x40\x97\xd5\xc5\x26\xa6\xd3\x5f\x97\xb8\x33\x51\x94\x0f"
  3833. "\x2c\xc8\x44\xc5\x0a\xf3\x2a\xcd\x3f\x2c\xdd\x06\x65\x68\x70"
  3834. "\x6f\x50\x9b\xc1\xbd\xde\x58\x29\x5d\xae\x3f\x89\x1a\x9a\x0f"
  3835. "\xca\x57\x83\x78\x9a\x41\xf8\x61\x12\x14\xce\x61\x23\x94\xdf"
  3836. "\x28\x6a\x62\xd1\xa2\x25\x2a\xa9\x4d\xb9\xc5\x38\x95\x6c\x71"
  3837. "\x7d\xc2\xbe\xd4\xf2\x32\xa0\x29\x4c\x85\x7c\x73\x0a\xa1\x60"
  3838. "\x67\xac\x10\x62\xf1\x20\x1f\xb0\xd3";
  3839. b.inLen = XSTRLEN(b.input);
  3840. b.outLen = 114;
  3841. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3842. c.output = "\x1a\x96\x18\x2b\x50\xfb\x8c\x7e\x74\xe0\xa7\x07\x78\x8f\x55"
  3843. "\xe9\x82\x09\xb8\xd9\x1f\xad\xe8\xf3\x2f\x8d\xd5\xcf\xf7\xbf"
  3844. "\x21\xf5\x4e\xe5\xf1\x95\x50\x82\x5a\x6e\x07\x00\x30\x51\x9e"
  3845. "\x94\x42\x63\xac\x1c\x67\x65\x28\x70\x65\x62\x1f\x9f\xcb\x32"
  3846. "\x01\x72\x3e\x32\x23\xb6\x3a\x46\xc2\x93\x8a\xa9\x53\xba\x84"
  3847. "\x01\xd0\xea\x77\xb8\xd2\x64\x90\x77\x55\x66\x40\x7b\x95\x67"
  3848. "\x3c\x0f\x4c\xc1\xce\x9f\xd9\x66\x14\x8d\x7e\xfd\xff\x26\xbb"
  3849. "\xf9\xf4\x8a\x21\xc6\xda\x35\xbf\xaa";
  3850. c.inLen = XSTRLEN(c.input);
  3851. c.outLen = 114;
  3852. /* Taken from NIST CAVP test vectors - full rate output. */
  3853. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  3854. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  3855. "\x85\xe0";
  3856. d.output = "\x93\x68\xf0\x15\x10\x92\x44\xeb\x02\x47\xfa\x3a\x0e\x57\xf5"
  3857. "\x2e\xa7\xd9\xeb\xa2\x3d\xae\x7a\x19\x7f\x0a\x29\xe9\x22\x55"
  3858. "\x06\x05\x98\x16\xb7\x84\x48\xb6\x49\x7a\x76\xeb\x96\x2d\xb3"
  3859. "\xf8\x4d\x37\x60\xf1\xfe\xb4\xbd\xc1\xfd\x4a\xc9\x4e\x91\x7a"
  3860. "\xc2\xea\x5e\x4f\x38\x37\x4a\xa5\x6e\x4f\x47\x67\xb8\xd7\x83"
  3861. "\x1b\x2d\x51\x49\x5a\xb8\xea\xb7\xc9\x82\x20\xaf\x13\x41\x5a"
  3862. "\x59\xbb\x7c\x17\x7a\xcd\x62\x8e\xf0\xff\xe3\x6c\xeb\x18\x59"
  3863. "\x5d\x14\x4c\xbf\x25\xef\xc0\x6c\xd9\x56\xa5\x78\x20\x6e\xa8"
  3864. "\xf9\x14\x5e\xf9\xce\x19\x50\x6a\x9d\x04\x4e\xc7\x00\x79\x9f"
  3865. "\xa1";
  3866. d.inLen = 32;
  3867. d.outLen = 136;
  3868. /* Taken from NIST CAVP test vectors - more than one output block. */
  3869. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  3870. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  3871. "\x47\xe4";
  3872. e.output = "\xe1\x7e\xab\x0d\xa4\x04\xf9\xb6\xac\xc0\x84\x97\x2f\xc5\x79"
  3873. "\xe8\x6d\xaa\x76\x10\xa5\xe1\x7c\x23\x2f\x79\x19\x83\x96\xfd"
  3874. "\x01\xc2\x4c\x34\xbb\x54\xf4\xb0\x1e\xf7\x40\xb4\x25\x33\x4a"
  3875. "\x55\xdd\x24\x81\x3d\xc8\xea\x86\xf5\x6e\xf7\x27\x67\x26\x2b"
  3876. "\xf2\x25\x74\x8c\xcc\x3d\x9f\x48\x6f\xfb\x72\x8f\x4e\xad\x29"
  3877. "\x60\xc9\x6c\x3e\x44\x63\x86\xea\xce\x21\x9c\x84\x28\x16\x11"
  3878. "\x63\x58\xb0\xf4\x2d\x7d\xff\xf7\xdd\x24\x11\xfa\x2a\x56\x79"
  3879. "\xfd\x7a\x94\x77\x45\x75\xba\xf9\xfc\xad\x68\xa1\x9e\x30\xd1"
  3880. "\x49\xb0\x59\xb5\x9c\x44\x6c\x4e\xdc\xa5\x9b\xc5\xa4\x79\x9d"
  3881. "\xc4\x65\xaa\x9e\x78\x2c\xed\x9f\x21\xc5\x5d\xe2\x42\xdd\x25"
  3882. "\xd0\xd9\xde\x60\xd0\x9f\xf8\x6a\xba\xf3\xa0\x3a\x76\x71\xb3"
  3883. "\x05\x42\xdf\xbe\x72\xfc\x56\xed\x6d\x1a\x99\x7f\x23\x7c\xd1"
  3884. "\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b\xa3\x60\x51"
  3885. "\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41\x77\x2c"
  3886. "\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59\x0d"
  3887. "\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  3888. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4";
  3889. e.inLen = 32;
  3890. e.outLen = 250;
  3891. test_sha[0] = a;
  3892. test_sha[1] = b;
  3893. test_sha[2] = c;
  3894. test_sha[3] = d;
  3895. test_sha[4] = e;
  3896. ret = wc_InitShake128(&sha, HEAP_HINT, devId);
  3897. if (ret != 0)
  3898. return WC_TEST_RET_ENC_EC(ret);
  3899. for (i = 0; i < times; ++i) {
  3900. ret = wc_Shake128_Update(&sha, (byte*)test_sha[i].input,
  3901. (word32)test_sha[i].inLen);
  3902. if (ret != 0)
  3903. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3904. ret = wc_Shake128_Final(&sha, hash, (word32)test_sha[i].outLen);
  3905. if (ret != 0)
  3906. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3907. if (XMEMCMP(hash, test_sha[i].output, test_sha[i].outLen) != 0)
  3908. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  3909. }
  3910. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  3911. large_input = (byte *)XMALLOC(SHAKE128_LARGE_INPUT_BUFSIZ, NULL,
  3912. DYNAMIC_TYPE_TMP_BUFFER);
  3913. if (large_input == NULL)
  3914. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  3915. #endif
  3916. #ifndef NO_LARGE_HASH_TEST
  3917. /* BEGIN LARGE HASH TEST */ {
  3918. for (i = 0; i < SHAKE128_LARGE_INPUT_BUFSIZ; i++) {
  3919. large_input[i] = (byte)(i & 0xFF);
  3920. }
  3921. times = 100;
  3922. for (i = 0; i < times; ++i) {
  3923. ret = wc_Shake128_Update(&sha, (byte*)large_input,
  3924. SHAKE128_LARGE_INPUT_BUFSIZ);
  3925. if (ret != 0)
  3926. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3927. }
  3928. ret = wc_Shake128_Final(&sha, hash, (word32)sizeof(hash));
  3929. if (ret != 0)
  3930. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  3931. if (XMEMCMP(hash, large_digest, sizeof(large_digest) - 1) != 0)
  3932. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  3933. } /* END LARGE HASH TEST */
  3934. #endif /* NO_LARGE_HASH_TEST */
  3935. ret = shake128_absorb_test(&sha, large_input, SHAKE128_LARGE_INPUT_BUFSIZ);
  3936. exit:
  3937. wc_Shake128_Free(&sha);
  3938. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  3939. if (large_input != NULL)
  3940. XFREE(large_input, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3941. #endif
  3942. return ret;
  3943. }
  3944. #endif
  3945. #ifdef WOLFSSL_SHAKE256
  3946. static wc_test_ret_t shake256_absorb_test(wc_Shake* sha, byte *large_input_buf,
  3947. size_t large_input_buf_size)
  3948. {
  3949. testVector a, b, c, d, e;
  3950. testVector test_sha[5];
  3951. wc_test_ret_t ret = 0;
  3952. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3953. static const char large_digest[] =
  3954. "\x21\x25\x8e\xae\x6e\x4f\xa7\xe1\xb9\x6d\xa7\xc9\x7d\x46\x03\x69"
  3955. "\x29\x0d\x81\x49\xba\x5d\xaf\x37\xfd\xeb\x25\x52\x1d\xd9\xbd\x65"
  3956. "\xfa\x99\xb9\xd1\x70\x6b\xeb\xd4\xc1\x2c\xea\x24\x20\x27\xa7\xcd"
  3957. "\xfa\xe1\x81\xd9\xd5\xc1\x1c\xc7\xe9\x70\xc3\xc7\x21\x6f\x32\x22"
  3958. "\xe3\x27\xdb\x58\x5e\xea\x18\x2d\x63\x4d\x14\x6c\x94\xcf\x2b\x7e"
  3959. "\x6e\x2a\x74\xf3\xe0\xac\xb3\xb2\xcc\xef\x38\xe9\xe7\x35\xb3\xc5"
  3960. "\x77\x9d\xff\xe3\x08\x8e\xf8\x2c\x89\xbb\x45\x22\x16\x99\x91\xc0"
  3961. "\xe7\x71\x57\x75\xc5\xb1\xc6\xaf\x27\xcb\x64\x8c\xc4\xee\x3d\x5f"
  3962. "\x4c\x35\xfb\x1c\xf3\xf8\x0e\xfd\x5e\xfc\x07\xd8\x4d\x55\x32\x49"
  3963. "\x45\x0d\xab\x4a\x49\xc4\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93"
  3964. "\x7a\xe6\x6b\xb4\x36\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43"
  3965. "\x2f\x3b\xfc\x09\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48"
  3966. "\x3d\x0e\xda\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08"
  3967. "\xd9\xdc\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a"
  3968. "\xb7\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  3969. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c\x68\x1b\x30\xdd\xc4\xe6"
  3970. "\x83\x8b\x0f\x23\x58\x7e\x06\x5f\x4a\x2b\xed\xc9\x6c\x97\x68\x44";
  3971. byte hash[sizeof(large_digest) - 1];
  3972. /*
  3973. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE256_Msg0.pdf
  3974. */
  3975. a.input = "";
  3976. a.output = "\x46\xb9\xdd\x2b\x0b\xa8\x8d\x13\x23\x3b\x3f\xeb\x74\x3e\xeb"
  3977. "\x24\x3f\xcd\x52\xea\x62\xb8\x1b\x82\xb5\x0c\x27\x64\x6e\xd5"
  3978. "\x76\x2f\xd7\x5d\xc4\xdd\xd8\xc0\xf2\x00\xcb\x05\x01\x9d\x67"
  3979. "\xb5\x92\xf6\xfc\x82\x1c\x49\x47\x9a\xb4\x86\x40\x29\x2e\xac"
  3980. "\xb3\xb7\xc4\xbe\x14\x1e\x96\x61\x6f\xb1\x39\x57\x69\x2c\xc7"
  3981. "\xed\xd0\xb4\x5a\xe3\xdc\x07\x22\x3c\x8e\x92\x93\x7b\xef\x84"
  3982. "\xbc\x0e\xab\x86\x28\x53\x34\x9e\xc7\x55\x46\xf5\x8f\xb7\xc2"
  3983. "\x77\x5c\x38\x46\x2c\x50\x10\xd8\x46\xc1\x85\xc1\x51\x11\xe5"
  3984. "\x95\x52\x2a\x6b\xcd\x16\xcf\x86\xf3\xd1\x22\x10\x9e\x3b\x1f"
  3985. "\xdd";
  3986. a.inLen = XSTRLEN(a.input);
  3987. a.outLen = WC_SHA3_256_BLOCK_SIZE;
  3988. b.input = "abc";
  3989. b.output = "\x48\x33\x66\x60\x13\x60\xa8\x77\x1c\x68\x63\x08\x0c\xc4\x11"
  3990. "\x4d\x8d\xb4\x45\x30\xf8\xf1\xe1\xee\x4f\x94\xea\x37\xe7\x8b"
  3991. "\x57\x39\xd5\xa1\x5b\xef\x18\x6a\x53\x86\xc7\x57\x44\xc0\x52"
  3992. "\x7e\x1f\xaa\x9f\x87\x26\xe4\x62\xa1\x2a\x4f\xeb\x06\xbd\x88"
  3993. "\x01\xe7\x51\xe4\x13\x85\x14\x12\x04\xf3\x29\x97\x9f\xd3\x04"
  3994. "\x7a\x13\xc5\x65\x77\x24\xad\xa6\x4d\x24\x70\x15\x7b\x3c\xdc"
  3995. "\x28\x86\x20\x94\x4d\x78\xdb\xcd\xdb\xd9\x12\x99\x3f\x09\x13"
  3996. "\xf1\x64\xfb\x2c\xe9\x51\x31\xa2\xd0\x9a\x3e\x6d\x51\xcb\xfc"
  3997. "\x62\x27\x20\xd7\xa7\x5c\x63\x34\xe8\xa2\xd7\xec\x71\xa7\xcc"
  3998. "\x29";
  3999. b.inLen = XSTRLEN(b.input);
  4000. b.outLen = WC_SHA3_256_BLOCK_SIZE;
  4001. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4002. c.output = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f\x6f\x87"
  4003. "\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b\xe5\xd4\xfd\x2e"
  4004. "\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59\xaa\x80\x60\xf1\xf9\xbc"
  4005. "\x99\x6c\x05\xac\xa3\xc6\x96\xa8\xb6\x62\x79\xdc\x67\x2c\x74"
  4006. "\x0b\xb2\x24\xec\x37\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55"
  4007. "\xf5\x1d\x97\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a"
  4008. "\xf2\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67\x60"
  4009. "\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4\x9d\xd3\xef\x7e\x18\x2b"
  4010. "\x15\x24\xdf\x82\xea\x1c\xef\xe1\xc6\xc3\x96\x61\x75\xf0\x22"
  4011. "\x8d";
  4012. c.inLen = XSTRLEN(c.input);
  4013. c.outLen = WC_SHA3_256_BLOCK_SIZE;
  4014. /* Taken from NIST CAVP test vectors - full rate output. */
  4015. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  4016. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  4017. "\x85\xe0";
  4018. d.output = "\x00\x64\x8a\xfb\xc5\xe6\x51\x64\x9d\xb1\xfd\x82\x93\x6b\x00"
  4019. "\xdb\xbc\x12\x2f\xb4\xc8\x77\x86\x0d\x38\x5c\x49\x50\xd5\x6d"
  4020. "\xe7\xe0\x96\xd6\x13\xd7\xa3\xf2\x7e\xd8\xf2\x63\x34\xb0\xcc"
  4021. "\xc1\x40\x7b\x41\xdc\xcb\x23\xdf\xaa\x52\x98\x18\xd1\x12\x5c"
  4022. "\xd5\x34\x80\x92\x52\x43\x66\xb8\x5f\xab\xb9\x7c\x6c\xd1\xe6"
  4023. "\x06\x6f\x45\x9b\xcc\x56\x6d\xa8\x7e\xc9\xb7\xba\x36\x79\x2d"
  4024. "\x11\x8a\xc3\x9a\x4c\xce\xf6\x19\x2b\xbf\x3a\x54\xaf\x18\xe5"
  4025. "\x7b\x0c\x14\x61\x01\xf6\xae\xaa\x82\x2b\xc4\xb4\xc9\x70\x8b"
  4026. "\x09\xf0\xb3\xba\xb4\x1b\xcc\xe9\x64\xd9\x99\xd1\x10\x7b\xd7"
  4027. "\xc2";
  4028. d.inLen = 32;
  4029. d.outLen = WC_SHA3_256_BLOCK_SIZE;
  4030. /* Taken from NIST CAVP test vectors - more than one output block. */
  4031. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  4032. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  4033. "\x47\xe4";
  4034. e.output = "\x2e\x97\x5f\x6a\x8a\x14\xf0\x70\x4d\x51\xb1\x36\x67\xd8\x19"
  4035. "\x5c\x21\x9f\x71\xe6\x34\x56\x96\xc4\x9f\xa4\xb9\xd0\x8e\x92"
  4036. "\x25\xd3\xd3\x93\x93\x42\x51\x52\xc9\x7e\x71\xdd\x24\x60\x1c"
  4037. "\x11\xab\xcf\xa0\xf1\x2f\x53\xc6\x80\xbd\x3a\xe7\x57\xb8\x13"
  4038. "\x4a\x9c\x10\xd4\x29\x61\x58\x69\x21\x7f\xdd\x58\x85\xc4\xdb"
  4039. "\x17\x49\x85\x70\x3a\x6d\x6d\xe9\x4a\x66\x7e\xac\x30\x23\x44"
  4040. "\x3a\x83\x37\xae\x1b\xc6\x01\xb7\x6d\x7d\x38\xec\x3c\x34\x46"
  4041. "\x31\x05\xf0\xd3\x94\x9d\x78\xe5\x62\xa0\x39\xe4\x46\x95\x48"
  4042. "\xb6\x09\x39\x5d\xe5\xa4\xfd\x43\xc4\x6c\xa9\xfd\x6e\xe2\x9a"
  4043. "\xda\x5e\xfc\x07\xd8\x4d\x55\x32\x49\x45\x0d\xab\x4a\x49\xc4"
  4044. "\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93\x7a\xe6\x6b\xb4\x36"
  4045. "\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43\x2f\x3b\xfc\x09"
  4046. "\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48\x3d\x0e\xda"
  4047. "\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08\xd9\xdc"
  4048. "\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a\xb7"
  4049. "\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  4050. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c\x68\x1b\x30\xdd\xc4"
  4051. "\xe6\x83\x8b\x0f\x23\x58\x7e\x06\x5f\x4a\x2b\xed\xc9\x6c\x97"
  4052. "\x68\x44";
  4053. e.inLen = 32;
  4054. e.outLen = 2 * WC_SHA3_256_BLOCK_SIZE;
  4055. test_sha[0] = a;
  4056. test_sha[1] = b;
  4057. test_sha[2] = c;
  4058. test_sha[3] = d;
  4059. test_sha[4] = e;
  4060. for (i = 0; i < times; ++i) {
  4061. ret = wc_InitShake256(sha, HEAP_HINT, devId);
  4062. if (ret != 0)
  4063. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4064. ret = wc_Shake256_Absorb(sha, (byte*)test_sha[i].input,
  4065. (word32)test_sha[i].inLen);
  4066. if (ret != 0)
  4067. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4068. ret = wc_Shake256_SqueezeBlocks(sha, hash,
  4069. (word32)test_sha[i].outLen / WC_SHA3_256_BLOCK_SIZE);
  4070. if (ret != 0)
  4071. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4072. if (XMEMCMP(hash, test_sha[i].output, (word32)test_sha[i].outLen) != 0)
  4073. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4074. }
  4075. #ifndef NO_LARGE_HASH_TEST
  4076. /* BEGIN LARGE HASH TEST */ {
  4077. for (i = 0; i < (int)large_input_buf_size; i++) {
  4078. large_input_buf[i] = (byte)(i & 0xFF);
  4079. }
  4080. ret = wc_InitShake256(sha, HEAP_HINT, devId);
  4081. if (ret != 0)
  4082. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4083. /* Absorb is non-incremental. */
  4084. ret = wc_Shake256_Absorb(sha, large_input_buf,
  4085. (word32)large_input_buf_size);
  4086. if (ret != 0)
  4087. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4088. /* Able to squeeze out blocks incrementally. */
  4089. ret = wc_Shake256_SqueezeBlocks(sha, hash, 1);
  4090. if (ret != 0)
  4091. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4092. ret = wc_Shake256_SqueezeBlocks(sha, hash,
  4093. ((word32)sizeof(hash) / WC_SHA3_256_BLOCK_SIZE) - 1);
  4094. if (ret != 0)
  4095. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4096. if (XMEMCMP(hash, large_digest, sizeof(hash)) != 0)
  4097. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4098. } /* END LARGE HASH TEST */
  4099. #endif /* NO_LARGE_HASH_TEST */
  4100. exit:
  4101. return ret;
  4102. }
  4103. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t shake256_test(void)
  4104. {
  4105. wc_Shake sha;
  4106. byte hash[250];
  4107. testVector a, b, c, d, e;
  4108. testVector test_sha[5];
  4109. wc_test_ret_t ret = 0;
  4110. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  4111. #define SHAKE256_LARGE_INPUT_BUFSIZ 1024
  4112. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4113. byte *large_input = NULL;
  4114. #else
  4115. byte large_input[SHAKE256_LARGE_INPUT_BUFSIZ];
  4116. #endif
  4117. const char* large_digest =
  4118. "\x90\x32\x4a\xcc\xd1\xdf\xb8\x0b\x79\x1f\xb8\xc8\x5b\x54\xc8\xe7"
  4119. "\x45\xf5\x60\x6b\x38\x26\xb2\x0a\xee\x38\x01\xf3\xd9\xfa\x96\x9f"
  4120. "\x6a\xd7\x15\xdf\xb6\xc2\xf4\x20\x33\x44\x55\xe8\x2a\x09\x2b\x68"
  4121. "\x2e\x18\x65\x5e\x65\x93\x28\xbc\xb1\x9e\xe2\xb1\x92\xea\x98\xac"
  4122. "\x21\xef\x4c\xe1\xb4\xb7\xbe\x81\x5c\x1d\xd3\xb7\x17\xe5\xbb\xc5"
  4123. "\x8c\x68\xb7\xfb\xac\x55\x8a\x9b\x4d\x91\xe4\x9f\x72\xbb\x6e\x38"
  4124. "\xaf\x21\x7d\x21\xaa\x98\x4e\x75\xc4\xb4\x1c\x7c\x50\x45\x54\xf9"
  4125. "\xea\x26";
  4126. /*
  4127. ** https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHAKE256_Msg0.pdf
  4128. */
  4129. a.input = "";
  4130. a.output = "\x46\xb9\xdd\x2b\x0b\xa8\x8d\x13\x23\x3b\x3f\xeb\x74\x3e\xeb"
  4131. "\x24\x3f\xcd\x52\xea\x62\xb8\x1b\x82\xb5\x0c\x27\x64\x6e\xd5"
  4132. "\x76\x2f\xd7\x5d\xc4\xdd\xd8\xc0\xf2\x00\xcb\x05\x01\x9d\x67"
  4133. "\xb5\x92\xf6\xfc\x82\x1c\x49\x47\x9a\xb4\x86\x40\x29\x2e\xac"
  4134. "\xb3\xb7\xc4\xbe\x14\x1e\x96\x61\x6f\xb1\x39\x57\x69\x2c\xc7"
  4135. "\xed\xd0\xb4\x5a\xe3\xdc\x07\x22\x3c\x8e\x92\x93\x7b\xef\x84"
  4136. "\xbc\x0e\xab\x86\x28\x53\x34\x9e\xc7\x55\x46\xf5\x8f\xb7\xc2"
  4137. "\x77\x5c\x38\x46\x2c\x50\x10\xd8\x46";
  4138. a.inLen = XSTRLEN(a.input);
  4139. a.outLen = 114;
  4140. b.input = "abc";
  4141. b.output = "\x48\x33\x66\x60\x13\x60\xa8\x77\x1c\x68\x63\x08\x0c\xc4\x11"
  4142. "\x4d\x8d\xb4\x45\x30\xf8\xf1\xe1\xee\x4f\x94\xea\x37\xe7\x8b"
  4143. "\x57\x39\xd5\xa1\x5b\xef\x18\x6a\x53\x86\xc7\x57\x44\xc0\x52"
  4144. "\x7e\x1f\xaa\x9f\x87\x26\xe4\x62\xa1\x2a\x4f\xeb\x06\xbd\x88"
  4145. "\x01\xe7\x51\xe4\x13\x85\x14\x12\x04\xf3\x29\x97\x9f\xd3\x04"
  4146. "\x7a\x13\xc5\x65\x77\x24\xad\xa6\x4d\x24\x70\x15\x7b\x3c\xdc"
  4147. "\x28\x86\x20\x94\x4d\x78\xdb\xcd\xdb\xd9\x12\x99\x3f\x09\x13"
  4148. "\xf1\x64\xfb\x2c\xe9\x51\x31\xa2\xd0";
  4149. b.inLen = XSTRLEN(b.input);
  4150. b.outLen = 114;
  4151. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4152. c.output = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f\x6f\x87"
  4153. "\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b\xe5\xd4\xfd\x2e"
  4154. "\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59\xaa\x80\x60\xf1\xf9\xbc"
  4155. "\x99\x6c\x05\xac\xa3\xc6\x96\xa8\xb6\x62\x79\xdc\x67\x2c\x74"
  4156. "\x0b\xb2\x24\xec\x37\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55"
  4157. "\xf5\x1d\x97\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a"
  4158. "\xf2\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67\x60"
  4159. "\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  4160. c.inLen = XSTRLEN(c.input);
  4161. c.outLen = 114;
  4162. /* Taken from NIST CAVP test vectors - full rate output. */
  4163. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  4164. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  4165. "\x85\xe0";
  4166. d.output = "\x00\x64\x8a\xfb\xc5\xe6\x51\x64\x9d\xb1\xfd\x82\x93\x6b\x00"
  4167. "\xdb\xbc\x12\x2f\xb4\xc8\x77\x86\x0d\x38\x5c\x49\x50\xd5\x6d"
  4168. "\xe7\xe0\x96\xd6\x13\xd7\xa3\xf2\x7e\xd8\xf2\x63\x34\xb0\xcc"
  4169. "\xc1\x40\x7b\x41\xdc\xcb\x23\xdf\xaa\x52\x98\x18\xd1\x12\x5c"
  4170. "\xd5\x34\x80\x92\x52\x43\x66\xb8\x5f\xab\xb9\x7c\x6c\xd1\xe6"
  4171. "\x06\x6f\x45\x9b\xcc\x56\x6d\xa8\x7e\xc9\xb7\xba\x36\x79\x2d"
  4172. "\x11\x8a\xc3\x9a\x4c\xce\xf6\x19\x2b\xbf\x3a\x54\xaf\x18\xe5"
  4173. "\x7b\x0c\x14\x61\x01\xf6\xae\xaa\x82\x2b\xc4\xb4\xc9\x70\x8b"
  4174. "\x09\xf0\xb3\xba\xb4\x1b\xcc\xe9\x64\xd9\x99\xd1\x10\x7b\xd7"
  4175. "\xc2";
  4176. d.inLen = 32;
  4177. d.outLen = 136;
  4178. /* Taken from NIST CAVP test vectors - more than one output block. */
  4179. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  4180. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  4181. "\x47\xe4";
  4182. e.output = "\x2e\x97\x5f\x6a\x8a\x14\xf0\x70\x4d\x51\xb1\x36\x67\xd8\x19"
  4183. "\x5c\x21\x9f\x71\xe6\x34\x56\x96\xc4\x9f\xa4\xb9\xd0\x8e\x92"
  4184. "\x25\xd3\xd3\x93\x93\x42\x51\x52\xc9\x7e\x71\xdd\x24\x60\x1c"
  4185. "\x11\xab\xcf\xa0\xf1\x2f\x53\xc6\x80\xbd\x3a\xe7\x57\xb8\x13"
  4186. "\x4a\x9c\x10\xd4\x29\x61\x58\x69\x21\x7f\xdd\x58\x85\xc4\xdb"
  4187. "\x17\x49\x85\x70\x3a\x6d\x6d\xe9\x4a\x66\x7e\xac\x30\x23\x44"
  4188. "\x3a\x83\x37\xae\x1b\xc6\x01\xb7\x6d\x7d\x38\xec\x3c\x34\x46"
  4189. "\x31\x05\xf0\xd3\x94\x9d\x78\xe5\x62\xa0\x39\xe4\x46\x95\x48"
  4190. "\xb6\x09\x39\x5d\xe5\xa4\xfd\x43\xc4\x6c\xa9\xfd\x6e\xe2\x9a"
  4191. "\xda\x5e\xfc\x07\xd8\x4d\x55\x32\x49\x45\x0d\xab\x4a\x49\xc4"
  4192. "\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93\x7a\xe6\x6b\xb4\x36"
  4193. "\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43\x2f\x3b\xfc\x09"
  4194. "\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48\x3d\x0e\xda"
  4195. "\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08\xd9\xdc"
  4196. "\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a\xb7"
  4197. "\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  4198. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c";
  4199. e.inLen = 32;
  4200. e.outLen = 250;
  4201. test_sha[0] = a;
  4202. test_sha[1] = b;
  4203. test_sha[2] = c;
  4204. test_sha[3] = d;
  4205. test_sha[4] = e;
  4206. ret = wc_InitShake256(&sha, HEAP_HINT, devId);
  4207. if (ret != 0)
  4208. return WC_TEST_RET_ENC_EC(ret);
  4209. for (i = 0; i < times; ++i) {
  4210. ret = wc_Shake256_Update(&sha, (byte*)test_sha[i].input,
  4211. (word32)test_sha[i].inLen);
  4212. if (ret != 0)
  4213. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4214. ret = wc_Shake256_Final(&sha, hash, (word32)test_sha[i].outLen);
  4215. if (ret != 0)
  4216. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4217. if (XMEMCMP(hash, test_sha[i].output, test_sha[i].outLen) != 0)
  4218. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4219. }
  4220. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4221. large_input = (byte *)XMALLOC(SHAKE256_LARGE_INPUT_BUFSIZ, NULL,
  4222. DYNAMIC_TYPE_TMP_BUFFER);
  4223. if (large_input == NULL)
  4224. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  4225. #endif
  4226. #ifndef NO_LARGE_HASH_TEST
  4227. /* BEGIN LARGE HASH TEST */ {
  4228. for (i = 0; i < SHAKE256_LARGE_INPUT_BUFSIZ; i++) {
  4229. large_input[i] = (byte)(i & 0xFF);
  4230. }
  4231. times = 100;
  4232. for (i = 0; i < times; ++i) {
  4233. ret = wc_Shake256_Update(&sha, (byte*)large_input,
  4234. SHAKE256_LARGE_INPUT_BUFSIZ);
  4235. if (ret != 0)
  4236. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4237. }
  4238. ret = wc_Shake256_Final(&sha, hash, (word32)sizeof(hash));
  4239. if (ret != 0)
  4240. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4241. if (XMEMCMP(hash, large_digest, 114) != 0)
  4242. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  4243. } /* END LARGE HASH TEST */
  4244. #endif /* NO_LARGE_HASH_TEST */
  4245. ret = shake256_absorb_test(&sha, large_input, SHAKE256_LARGE_INPUT_BUFSIZ);
  4246. exit:
  4247. wc_Shake256_Free(&sha);
  4248. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  4249. if (large_input != NULL)
  4250. XFREE(large_input, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  4251. #endif
  4252. return ret;
  4253. }
  4254. #endif
  4255. #ifdef WOLFSSL_SM3
  4256. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sm3_test(void)
  4257. {
  4258. wc_Sm3 sm3, sm3Copy;
  4259. byte hash[WC_SM3_DIGEST_SIZE];
  4260. byte hashGet[WC_SM3_DIGEST_SIZE];
  4261. byte hashCopy[WC_SM3_DIGEST_SIZE];
  4262. wc_test_ret_t ret = 0;
  4263. testVector a, b, c;
  4264. testVector test_sm3[3];
  4265. int times = sizeof(test_sm3) / sizeof(struct testVector), i;
  4266. a.input = "";
  4267. a.output = "\x1a\xb2\x1d\x83\x55\xcf\xa1\x7f\x8e\x61\x19\x48\x31\xe8\x1a"
  4268. "\x8f\x22\xbe\xc8\xc7\x28\xfe\xfb\x74\x7e\xd0\x35\xeb\x50\x82"
  4269. "\xaa\x2b";
  4270. a.inLen = XSTRLEN(a.input);
  4271. a.outLen = WC_SM3_DIGEST_SIZE;
  4272. b.input = "abc";
  4273. b.output = "\x66\xc7\xf0\xf4\x62\xee\xed\xd9\xd1\xf2\xd4\x6b\xdc\x10\xe4"
  4274. "\xe2\x41\x67\xc4\x87\x5c\xf2\xf7\xa2\x29\x7d\xa0\x2b\x8f\x4b"
  4275. "\xa8\xe0";
  4276. b.inLen = XSTRLEN(b.input);
  4277. b.outLen = WC_SM3_DIGEST_SIZE;
  4278. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4279. c.output = "\x63\x9b\x6c\xc5\xe6\x4d\x9e\x37\xa3\x90\xb1\x92\xdf\x4f\xa1"
  4280. "\xea\x07\x20\xab\x74\x7f\xf6\x92\xb9\xf3\x8c\x4e\x66\xad\x7b"
  4281. "\x8c\x05";
  4282. c.inLen = XSTRLEN(c.input);
  4283. c.outLen = WC_SM3_DIGEST_SIZE;
  4284. test_sm3[0] = a;
  4285. test_sm3[1] = b;
  4286. test_sm3[2] = c;
  4287. ret = wc_InitSm3(&sm3, HEAP_HINT, devId);
  4288. if (ret != 0)
  4289. return WC_TEST_RET_ENC_EC(ret);
  4290. ret = wc_InitSm3(&sm3Copy, HEAP_HINT, devId);
  4291. if (ret != 0) {
  4292. wc_Sm3Free(&sm3);
  4293. return WC_TEST_RET_ENC_EC(ret);
  4294. }
  4295. /* Test all the KATs. */
  4296. for (i = 0; i < times; ++i) {
  4297. ret = wc_Sm3Update(&sm3, (byte*)test_sm3[i].input,
  4298. (word32)test_sm3[i].inLen);
  4299. if (ret != 0) {
  4300. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4301. }
  4302. /* Get the final hash but leave ready for more updates. */
  4303. ret = wc_Sm3GetHash(&sm3, hashGet);
  4304. if (ret != 0)
  4305. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4306. /* Make a copy of the hash. */
  4307. ret = wc_Sm3Copy(&sm3, &sm3Copy);
  4308. if (ret != 0)
  4309. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4310. /* Get the final hash with original. */
  4311. ret = wc_Sm3Final(&sm3, hash);
  4312. if (ret != 0)
  4313. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4314. /* Get the final hash with copy. */
  4315. ret = wc_Sm3Final(&sm3Copy, hashCopy);
  4316. if (ret != 0)
  4317. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4318. /* Dispose of copy. */
  4319. wc_Sm3Free(&sm3Copy);
  4320. /* Check hashes match expected. */
  4321. if (XMEMCMP(hash, test_sm3[i].output, WC_SM3_DIGEST_SIZE) != 0)
  4322. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4323. if (XMEMCMP(hash, hashGet, WC_SM3_DIGEST_SIZE) != 0)
  4324. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4325. if (XMEMCMP(hash, hashCopy, WC_SM3_DIGEST_SIZE) != 0)
  4326. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4327. }
  4328. #ifndef NO_LARGE_HASH_TEST
  4329. {
  4330. word32 sz;
  4331. byte large_input[1024];
  4332. #ifdef HASH_SIZE_LIMIT
  4333. const char* large_digest =
  4334. "\x6c\x42\x57\x64\x8e\x45\xf3\xb6\xc0\x83\xd3\x41\x83\x66\x51\xb4"
  4335. "\x50\xfe\x06\xb5\xb7\x1e\xd5\x0d\x41\xfc\x1e\xe5\xc6\x57\x95\x0f";
  4336. times = 20;
  4337. #else
  4338. const char* large_digest =
  4339. "\x34\x51\x3c\xde\x7c\x30\xb7\xc5\xaa\x97\x3b\xed\xb3\x16\xb9\x76"
  4340. "\x35\x46\x14\x80\x2a\x57\xca\xd9\x48\xf9\x93\xcc\x1f\xdd\xab\x79";
  4341. times = 100;
  4342. #endif
  4343. /* Set large input to something. */
  4344. for (i = 0; i < (int)sizeof(large_input); i++) {
  4345. large_input[i] = (byte)(i & 0xFF);
  4346. }
  4347. /* Hash a large number of times. */
  4348. for (i = 0; i < times; ++i) {
  4349. ret = wc_Sm3Update(&sm3, (byte*)large_input,
  4350. (word32)sizeof(large_input));
  4351. if (ret != 0)
  4352. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4353. }
  4354. /* Calculate hash and compare to expected. */
  4355. ret = wc_Sm3Final(&sm3, hash);
  4356. if (ret != 0)
  4357. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4358. if (XMEMCMP(hash, large_digest, WC_SM3_DIGEST_SIZE) != 0)
  4359. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  4360. /* Check updating with various sizes works. */
  4361. for (sz = 1; sz <= 64; sz++) {
  4362. /* Hash a large number of times. */
  4363. for (i = 0; i < times; ++i) {
  4364. word32 o;
  4365. /* Update sz bytes at a time from large input buffer. */
  4366. for (o = 0; o + sz <= (word32)sizeof(large_input); o += sz) {
  4367. ret = wc_Sm3Update(&sm3, (byte*)(large_input + o), sz);
  4368. if (ret != 0)
  4369. ERROR_OUT(WC_TEST_RET_ENC_I(o), exit);
  4370. }
  4371. /* Check for left-overs. */
  4372. if (o < (word32)sizeof(large_input)) {
  4373. ret = wc_Sm3Update(&sm3, (byte*)(large_input + o),
  4374. (word32)sizeof(large_input) - o);
  4375. if (ret != 0)
  4376. ERROR_OUT(WC_TEST_RET_ENC_I(i), exit);
  4377. }
  4378. }
  4379. /* Calculate hash and compare to expected. */
  4380. ret = wc_Sm3Final(&sm3, hash);
  4381. if (ret != 0)
  4382. ERROR_OUT(WC_TEST_RET_ENC_I(sz), exit);
  4383. if (XMEMCMP(hash, large_digest, WC_SM3_DIGEST_SIZE) != 0)
  4384. ERROR_OUT(WC_TEST_RET_ENC_I(sz), exit);
  4385. }
  4386. }
  4387. #endif /* NO_LARGE_HASH_TEST */
  4388. exit:
  4389. wc_Sm3Free(&sm3);
  4390. wc_Sm3Free(&sm3Copy);
  4391. return ret;
  4392. }
  4393. #endif
  4394. #ifndef NO_HASH_WRAPPER
  4395. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hash_test(void)
  4396. {
  4397. wc_HashAlg hash;
  4398. int ret, exp_ret;
  4399. int i, j;
  4400. int digestSz;
  4401. byte data[] = "0123456789abcdef0123456789abcdef0123456";
  4402. byte out[WC_MAX_DIGEST_SIZE];
  4403. byte hashOut[WC_MAX_DIGEST_SIZE];
  4404. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  4405. enum wc_HashType hashType;
  4406. #endif
  4407. enum wc_HashType typesGood[] = { WC_HASH_TYPE_MD5, WC_HASH_TYPE_SHA,
  4408. WC_HASH_TYPE_SHA224, WC_HASH_TYPE_SHA256,
  4409. WC_HASH_TYPE_SHA384, WC_HASH_TYPE_SHA512,
  4410. WC_HASH_TYPE_SHA3_224,
  4411. WC_HASH_TYPE_SHA3_256,
  4412. WC_HASH_TYPE_SHA3_384,
  4413. WC_HASH_TYPE_SHA3_512 };
  4414. enum wc_HashType typesNoImpl[] = {
  4415. #ifdef NO_MD5
  4416. WC_HASH_TYPE_MD5,
  4417. #endif
  4418. #ifdef NO_SHA
  4419. WC_HASH_TYPE_SHA,
  4420. #endif
  4421. #ifndef WOLFSSL_SHA224
  4422. WC_HASH_TYPE_SHA224,
  4423. #endif
  4424. #ifdef NO_SHA256
  4425. WC_HASH_TYPE_SHA256,
  4426. #endif
  4427. #ifndef WOLFSSL_SHA384
  4428. WC_HASH_TYPE_SHA384,
  4429. #endif
  4430. #ifndef WOLFSSL_SHA512
  4431. WC_HASH_TYPE_SHA512,
  4432. #endif
  4433. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_224)
  4434. WC_HASH_TYPE_SHA3_224,
  4435. #endif
  4436. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_256)
  4437. WC_HASH_TYPE_SHA3_256,
  4438. #endif
  4439. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_384)
  4440. WC_HASH_TYPE_SHA3_384,
  4441. #endif
  4442. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_512)
  4443. WC_HASH_TYPE_SHA3_512,
  4444. #endif
  4445. WC_HASH_TYPE_NONE
  4446. };
  4447. enum wc_HashType typesBad[] = { WC_HASH_TYPE_NONE, WC_HASH_TYPE_MD5_SHA,
  4448. WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4 };
  4449. enum wc_HashType typesHashBad[] = { WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4,
  4450. WC_HASH_TYPE_BLAKE2B,
  4451. WC_HASH_TYPE_NONE };
  4452. /* Parameter Validation testing. */
  4453. ret = wc_HashInit(NULL, WC_HASH_TYPE_SHA256);
  4454. if (ret != BAD_FUNC_ARG)
  4455. return WC_TEST_RET_ENC_EC(ret);
  4456. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  4457. if (ret != BAD_FUNC_ARG)
  4458. return WC_TEST_RET_ENC_EC(ret);
  4459. ret = wc_HashUpdate(&hash, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  4460. if (ret != BAD_FUNC_ARG)
  4461. return WC_TEST_RET_ENC_EC(ret);
  4462. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, data, sizeof(data));
  4463. if (ret != BAD_FUNC_ARG)
  4464. return WC_TEST_RET_ENC_EC(ret);
  4465. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, NULL);
  4466. if (ret != BAD_FUNC_ARG)
  4467. return WC_TEST_RET_ENC_EC(ret);
  4468. ret = wc_HashFinal(&hash, WC_HASH_TYPE_SHA256, NULL);
  4469. if (ret != BAD_FUNC_ARG)
  4470. return WC_TEST_RET_ENC_EC(ret);
  4471. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, out);
  4472. if (ret != BAD_FUNC_ARG)
  4473. return WC_TEST_RET_ENC_EC(ret);
  4474. /* Try invalid hash algorithms. */
  4475. for (i = 0; i < (int)(sizeof(typesBad)/sizeof(*typesBad)); i++) {
  4476. ret = wc_HashInit(&hash, typesBad[i]);
  4477. if (ret != BAD_FUNC_ARG)
  4478. return WC_TEST_RET_ENC_I(i);
  4479. ret = wc_HashUpdate(&hash, typesBad[i], data, sizeof(data));
  4480. if (ret != BAD_FUNC_ARG)
  4481. return WC_TEST_RET_ENC_I(i);
  4482. ret = wc_HashFinal(&hash, typesBad[i], out);
  4483. if (ret != BAD_FUNC_ARG)
  4484. return WC_TEST_RET_ENC_I(i);
  4485. wc_HashFree(&hash, typesBad[i]);
  4486. }
  4487. /* Try valid hash algorithms. */
  4488. for (i = 0, j = 0; i < (int)(sizeof(typesGood)/sizeof(*typesGood)); i++) {
  4489. exp_ret = 0;
  4490. if (typesGood[i] == typesNoImpl[j]) {
  4491. /* Recognized but no implementation compiled in. */
  4492. exp_ret = HASH_TYPE_E;
  4493. j++;
  4494. }
  4495. ret = wc_HashInit(&hash, typesGood[i]);
  4496. if (ret != exp_ret)
  4497. return WC_TEST_RET_ENC_I(i);
  4498. ret = wc_HashUpdate(&hash, typesGood[i], data, sizeof(data));
  4499. if (ret != exp_ret)
  4500. return WC_TEST_RET_ENC_I(i);
  4501. ret = wc_HashFinal(&hash, typesGood[i], out);
  4502. if (ret != exp_ret)
  4503. return WC_TEST_RET_ENC_I(i);
  4504. wc_HashFree(&hash, typesGood[i]);
  4505. digestSz = wc_HashGetDigestSize(typesGood[i]);
  4506. if (exp_ret < 0 && digestSz != exp_ret)
  4507. return WC_TEST_RET_ENC_I(i);
  4508. if (exp_ret == 0 && digestSz < 0)
  4509. return WC_TEST_RET_ENC_I(i);
  4510. if (exp_ret == 0) {
  4511. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut,
  4512. digestSz - 1);
  4513. if (ret != BUFFER_E)
  4514. return WC_TEST_RET_ENC_I(i);
  4515. }
  4516. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut, digestSz);
  4517. if (ret != exp_ret)
  4518. return WC_TEST_RET_ENC_I(i);
  4519. if (exp_ret == 0 && XMEMCMP(out, hashOut, digestSz) != 0)
  4520. return WC_TEST_RET_ENC_I(i);
  4521. ret = wc_HashGetBlockSize(typesGood[i]);
  4522. if (exp_ret < 0 && ret != exp_ret)
  4523. return WC_TEST_RET_ENC_I(i);
  4524. if (exp_ret == 0 && ret < 0)
  4525. return WC_TEST_RET_ENC_I(i);
  4526. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  4527. ret = wc_HashGetOID(typesGood[i]);
  4528. if (ret == BAD_FUNC_ARG ||
  4529. (exp_ret == 0 && ret == HASH_TYPE_E) ||
  4530. (exp_ret != 0 && ret != HASH_TYPE_E)) {
  4531. return WC_TEST_RET_ENC_I(i);
  4532. }
  4533. hashType = wc_OidGetHash(ret);
  4534. if (exp_ret == 0 && hashType != typesGood[i])
  4535. return WC_TEST_RET_ENC_I(i);
  4536. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  4537. }
  4538. for (i = 0; i < (int)(sizeof(typesHashBad)/sizeof(*typesHashBad)); i++) {
  4539. ret = wc_Hash(typesHashBad[i], data, sizeof(data), out, sizeof(out));
  4540. if ((ret != BAD_FUNC_ARG) && (ret != BUFFER_E) && (ret != HASH_TYPE_E))
  4541. return WC_TEST_RET_ENC_I(i);
  4542. }
  4543. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  4544. ret = wc_HashGetOID(WC_HASH_TYPE_MD2);
  4545. #ifdef WOLFSSL_MD2
  4546. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  4547. return WC_TEST_RET_ENC_EC(ret);
  4548. #else
  4549. if (ret != HASH_TYPE_E)
  4550. return WC_TEST_RET_ENC_EC(ret);
  4551. #endif
  4552. hashType = wc_OidGetHash(646); /* Md2h */
  4553. #ifdef WOLFSSL_MD2
  4554. if (hashType != WC_HASH_TYPE_MD2)
  4555. return WC_TEST_RET_ENC_NC;
  4556. #else
  4557. if (hashType != WC_HASH_TYPE_NONE)
  4558. return WC_TEST_RET_ENC_NC;
  4559. #endif
  4560. ret = wc_HashGetOID(WC_HASH_TYPE_MD5_SHA);
  4561. #ifndef NO_MD5
  4562. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  4563. return WC_TEST_RET_ENC_EC(ret);
  4564. #else
  4565. if (ret != HASH_TYPE_E)
  4566. return WC_TEST_RET_ENC_EC(ret);
  4567. #endif
  4568. ret = wc_HashGetOID(WC_HASH_TYPE_MD4);
  4569. if (ret != BAD_FUNC_ARG)
  4570. return WC_TEST_RET_ENC_EC(ret);
  4571. ret = wc_HashGetOID(WC_HASH_TYPE_NONE);
  4572. if (ret != BAD_FUNC_ARG)
  4573. return WC_TEST_RET_ENC_EC(ret);
  4574. hashType = wc_OidGetHash(0);
  4575. if (hashType != WC_HASH_TYPE_NONE)
  4576. return WC_TEST_RET_ENC_NC;
  4577. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  4578. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD2);
  4579. #ifdef WOLFSSL_MD2
  4580. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  4581. return WC_TEST_RET_ENC_EC(ret);
  4582. #else
  4583. if (ret != HASH_TYPE_E)
  4584. return WC_TEST_RET_ENC_EC(ret);
  4585. #endif
  4586. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD2);
  4587. #ifdef WOLFSSL_MD2
  4588. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  4589. return WC_TEST_RET_ENC_EC(ret);
  4590. #else
  4591. if (ret != HASH_TYPE_E)
  4592. return WC_TEST_RET_ENC_EC(ret);
  4593. #endif
  4594. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD4);
  4595. #ifndef NO_MD4
  4596. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  4597. return WC_TEST_RET_ENC_EC(ret);
  4598. #else
  4599. if (ret != HASH_TYPE_E)
  4600. return WC_TEST_RET_ENC_EC(ret);
  4601. #endif
  4602. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD4);
  4603. #ifndef NO_MD4
  4604. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  4605. return WC_TEST_RET_ENC_EC(ret);
  4606. #else
  4607. if (ret != HASH_TYPE_E)
  4608. return WC_TEST_RET_ENC_EC(ret);
  4609. #endif
  4610. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD5_SHA);
  4611. #if !defined(NO_MD5) && !defined(NO_SHA)
  4612. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  4613. return WC_TEST_RET_ENC_EC(ret);
  4614. #else
  4615. if (ret != HASH_TYPE_E)
  4616. return WC_TEST_RET_ENC_EC(ret);
  4617. #endif
  4618. ret = wc_HashGetBlockSize(WC_HASH_TYPE_BLAKE2B);
  4619. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  4620. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  4621. return WC_TEST_RET_ENC_EC(ret);
  4622. #else
  4623. if (ret != HASH_TYPE_E)
  4624. return WC_TEST_RET_ENC_EC(ret);
  4625. #endif
  4626. ret = wc_HashGetDigestSize(WC_HASH_TYPE_BLAKE2B);
  4627. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  4628. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  4629. return WC_TEST_RET_ENC_EC(ret);
  4630. #else
  4631. if (ret != HASH_TYPE_E)
  4632. return WC_TEST_RET_ENC_EC(ret);
  4633. #endif
  4634. ret = wc_HashGetBlockSize(WC_HASH_TYPE_NONE);
  4635. if (ret != BAD_FUNC_ARG)
  4636. return WC_TEST_RET_ENC_EC(ret);
  4637. ret = wc_HashGetDigestSize(WC_HASH_TYPE_NONE);
  4638. if (ret != BAD_FUNC_ARG)
  4639. return WC_TEST_RET_ENC_EC(ret);
  4640. #if !defined(NO_CERTS) && !defined(NO_ASN)
  4641. #if defined(WOLFSSL_MD2) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  4642. ret = wc_GetCTC_HashOID(MD2);
  4643. if (ret == 0)
  4644. return WC_TEST_RET_ENC_EC(ret);
  4645. #endif
  4646. #ifndef NO_MD5
  4647. ret = wc_GetCTC_HashOID(WC_MD5);
  4648. if (ret == 0)
  4649. return WC_TEST_RET_ENC_EC(ret);
  4650. #endif
  4651. #ifndef NO_SHA
  4652. ret = wc_GetCTC_HashOID(WC_SHA);
  4653. if (ret == 0)
  4654. return WC_TEST_RET_ENC_EC(ret);
  4655. #endif
  4656. #ifdef WOLFSSL_SHA224
  4657. ret = wc_GetCTC_HashOID(WC_SHA224);
  4658. if (ret == 0)
  4659. return WC_TEST_RET_ENC_EC(ret);
  4660. #endif
  4661. #ifndef NO_SHA256
  4662. ret = wc_GetCTC_HashOID(WC_SHA256);
  4663. if (ret == 0)
  4664. return WC_TEST_RET_ENC_EC(ret);
  4665. #endif
  4666. #ifdef WOLFSSL_SHA384
  4667. ret = wc_GetCTC_HashOID(WC_SHA384);
  4668. if (ret == 0)
  4669. return WC_TEST_RET_ENC_EC(ret);
  4670. #endif
  4671. #ifdef WOLFSSL_SHA512
  4672. ret = wc_GetCTC_HashOID(WC_SHA512);
  4673. if (ret == 0)
  4674. return WC_TEST_RET_ENC_EC(ret);
  4675. #endif
  4676. ret = wc_GetCTC_HashOID(-1);
  4677. if (ret != 0)
  4678. return WC_TEST_RET_ENC_EC(ret);
  4679. #endif
  4680. return 0;
  4681. }
  4682. #endif /* !NO_HASH_WRAPPER */
  4683. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS) && \
  4684. defined(HAVE_FIPS_VERSION) && \
  4685. (HAVE_FIPS_VERSION >= 5))
  4686. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_md5_test(void)
  4687. {
  4688. Hmac hmac;
  4689. byte hash[WC_MD5_DIGEST_SIZE];
  4690. const char* keys[]=
  4691. {
  4692. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  4693. "Jefe",
  4694. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4695. };
  4696. testVector a, b, c;
  4697. testVector test_hmac[3];
  4698. wc_test_ret_t ret;
  4699. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4700. a.input = "Hi There";
  4701. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  4702. "\x9d";
  4703. a.inLen = XSTRLEN(a.input);
  4704. a.outLen = WC_MD5_DIGEST_SIZE;
  4705. b.input = "what do ya want for nothing?";
  4706. b.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
  4707. "\x38";
  4708. b.inLen = XSTRLEN(b.input);
  4709. b.outLen = WC_MD5_DIGEST_SIZE;
  4710. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4711. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4712. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4713. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4714. c.output = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
  4715. "\xf6";
  4716. c.inLen = XSTRLEN(c.input);
  4717. c.outLen = WC_MD5_DIGEST_SIZE;
  4718. test_hmac[0] = a;
  4719. test_hmac[1] = b;
  4720. test_hmac[2] = c;
  4721. for (i = 0; i < times; ++i) {
  4722. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4723. if (i == 1) {
  4724. continue; /* cavium can't handle short keys, fips not allowed */
  4725. }
  4726. #endif
  4727. ret = wc_HmacInit(&hmac, HEAP_HINT, devId);
  4728. if (ret != 0)
  4729. return WC_TEST_RET_ENC_EC(ret);
  4730. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[i],
  4731. (word32)XSTRLEN(keys[i]));
  4732. if (ret != 0)
  4733. return WC_TEST_RET_ENC_EC(ret);
  4734. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4735. (word32)test_hmac[i].inLen);
  4736. if (ret != 0)
  4737. return WC_TEST_RET_ENC_EC(ret);
  4738. ret = wc_HmacFinal(&hmac, hash);
  4739. if (ret != 0)
  4740. return WC_TEST_RET_ENC_EC(ret);
  4741. if (XMEMCMP(hash, test_hmac[i].output, WC_MD5_DIGEST_SIZE) != 0)
  4742. return WC_TEST_RET_ENC_I(i);
  4743. wc_HmacFree(&hmac);
  4744. }
  4745. #ifndef HAVE_FIPS
  4746. if ((ret = wc_HmacSizeByType(WC_MD5)) != WC_MD5_DIGEST_SIZE)
  4747. return WC_TEST_RET_ENC_EC(ret);
  4748. #endif
  4749. return 0;
  4750. }
  4751. #endif /* !NO_HMAC && !NO_MD5 && (!HAVE_FIPS || (HAVE_FIPS_VERSION < 5)) */
  4752. #if !defined(NO_HMAC) && !defined(NO_SHA)
  4753. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha_test(void)
  4754. {
  4755. Hmac hmac;
  4756. byte hash[WC_SHA_DIGEST_SIZE];
  4757. const char* keys[]=
  4758. {
  4759. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4760. "\x0b\x0b\x0b",
  4761. "Jefe",
  4762. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4763. "\xAA\xAA\xAA"
  4764. };
  4765. testVector a, b, c;
  4766. testVector test_hmac[3];
  4767. wc_test_ret_t ret;
  4768. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4769. a.input = "Hi There";
  4770. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  4771. "\x8e\xf1\x46\xbe\x00";
  4772. a.inLen = XSTRLEN(a.input);
  4773. a.outLen = WC_SHA_DIGEST_SIZE;
  4774. b.input = "what do ya want for nothing?";
  4775. b.output = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf"
  4776. "\x9c\x25\x9a\x7c\x79";
  4777. b.inLen = XSTRLEN(b.input);
  4778. b.outLen = WC_SHA_DIGEST_SIZE;
  4779. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4780. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4781. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4782. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4783. c.output = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b"
  4784. "\x4f\x63\xf1\x75\xd3";
  4785. c.inLen = XSTRLEN(c.input);
  4786. c.outLen = WC_SHA_DIGEST_SIZE;
  4787. test_hmac[0] = a;
  4788. test_hmac[1] = b;
  4789. test_hmac[2] = c;
  4790. for (i = 0; i < times; ++i) {
  4791. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4792. if (i == 1)
  4793. continue; /* cavium can't handle short keys, fips not allowed */
  4794. #endif
  4795. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  4796. return WC_TEST_RET_ENC_EC(ret);
  4797. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[i],
  4798. (word32)XSTRLEN(keys[i]));
  4799. if (ret != 0)
  4800. return WC_TEST_RET_ENC_EC(ret);
  4801. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4802. (word32)test_hmac[i].inLen);
  4803. if (ret != 0)
  4804. return WC_TEST_RET_ENC_EC(ret);
  4805. ret = wc_HmacFinal(&hmac, hash);
  4806. if (ret != 0)
  4807. return WC_TEST_RET_ENC_EC(ret);
  4808. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA_DIGEST_SIZE) != 0)
  4809. return WC_TEST_RET_ENC_I(i);
  4810. wc_HmacFree(&hmac);
  4811. }
  4812. #ifndef HAVE_FIPS
  4813. if ((ret = wc_HmacSizeByType(WC_SHA)) != WC_SHA_DIGEST_SIZE)
  4814. return WC_TEST_RET_ENC_EC(ret);
  4815. #endif
  4816. return 0;
  4817. }
  4818. #endif
  4819. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  4820. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha224_test(void)
  4821. {
  4822. Hmac hmac;
  4823. byte hash[WC_SHA224_DIGEST_SIZE];
  4824. const char* keys[]=
  4825. {
  4826. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4827. "\x0b\x0b\x0b",
  4828. "Jefe",
  4829. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4830. "\xAA\xAA\xAA",
  4831. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4832. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4833. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4834. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4835. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4836. };
  4837. testVector a, b, c, d;
  4838. testVector test_hmac[4];
  4839. wc_test_ret_t ret;
  4840. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4841. a.input = "Hi There";
  4842. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  4843. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  4844. a.inLen = XSTRLEN(a.input);
  4845. a.outLen = WC_SHA224_DIGEST_SIZE;
  4846. b.input = "what do ya want for nothing?";
  4847. b.output = "\xa3\x0e\x01\x09\x8b\xc6\xdb\xbf\x45\x69\x0f\x3a\x7e\x9e\x6d"
  4848. "\x0f\x8b\xbe\xa2\xa3\x9e\x61\x48\x00\x8f\xd0\x5e\x44";
  4849. b.inLen = XSTRLEN(b.input);
  4850. b.outLen = WC_SHA224_DIGEST_SIZE;
  4851. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4852. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4853. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4854. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4855. c.output = "\x7f\xb3\xcb\x35\x88\xc6\xc1\xf6\xff\xa9\x69\x4d\x7d\x6a\xd2"
  4856. "\x64\x93\x65\xb0\xc1\xf6\x5d\x69\xd1\xec\x83\x33\xea";
  4857. c.inLen = XSTRLEN(c.input);
  4858. c.outLen = WC_SHA224_DIGEST_SIZE;
  4859. d.input = "Big Key Input";
  4860. d.output = "\xe7\x4e\x2b\x8a\xa9\xf0\x37\x2f\xed\xae\x70\x0c\x49\x47\xf1"
  4861. "\x46\x54\xa7\x32\x6b\x55\x01\x87\xd2\xc8\x02\x0e\x3a";
  4862. d.inLen = XSTRLEN(d.input);
  4863. d.outLen = WC_SHA224_DIGEST_SIZE;
  4864. test_hmac[0] = a;
  4865. test_hmac[1] = b;
  4866. test_hmac[2] = c;
  4867. test_hmac[3] = d;
  4868. for (i = 0; i < times; ++i) {
  4869. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4870. if (i == 1)
  4871. continue; /* cavium can't handle short keys, fips not allowed */
  4872. #endif
  4873. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  4874. return WC_TEST_RET_ENC_EC(ret);
  4875. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[i],
  4876. (word32)XSTRLEN(keys[i]));
  4877. if (ret != 0)
  4878. return WC_TEST_RET_ENC_EC(ret);
  4879. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4880. (word32)test_hmac[i].inLen);
  4881. if (ret != 0)
  4882. return WC_TEST_RET_ENC_EC(ret);
  4883. ret = wc_HmacFinal(&hmac, hash);
  4884. if (ret != 0)
  4885. return WC_TEST_RET_ENC_EC(ret);
  4886. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  4887. return WC_TEST_RET_ENC_I(i);
  4888. wc_HmacFree(&hmac);
  4889. }
  4890. #ifndef HAVE_FIPS
  4891. if ((ret = wc_HmacSizeByType(WC_SHA224)) != WC_SHA224_DIGEST_SIZE)
  4892. return WC_TEST_RET_ENC_EC(ret);
  4893. #endif
  4894. return 0;
  4895. }
  4896. #endif
  4897. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  4898. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha256_test(void)
  4899. {
  4900. Hmac hmac;
  4901. byte hash[WC_SHA256_DIGEST_SIZE];
  4902. const char* keys[]=
  4903. {
  4904. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4905. "\x0b\x0b\x0b",
  4906. "Jefe",
  4907. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4908. "\xAA\xAA\xAA",
  4909. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4910. "\xAA\xAA\xAA",
  4911. };
  4912. testVector a, b, c, d;
  4913. testVector test_hmac[4];
  4914. wc_test_ret_t ret;
  4915. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4916. a.input = "Hi There";
  4917. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  4918. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  4919. "\xcf\xf7";
  4920. a.inLen = XSTRLEN(a.input);
  4921. a.outLen = WC_SHA256_DIGEST_SIZE;
  4922. b.input = "what do ya want for nothing?";
  4923. b.output = "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75"
  4924. "\xc7\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec"
  4925. "\x38\x43";
  4926. b.inLen = XSTRLEN(b.input);
  4927. b.outLen = WC_SHA256_DIGEST_SIZE;
  4928. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4929. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4930. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4931. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4932. c.output = "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81"
  4933. "\xa7\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5"
  4934. "\x65\xfe";
  4935. c.inLen = XSTRLEN(c.input);
  4936. c.outLen = WC_SHA256_DIGEST_SIZE;
  4937. d.input = 0;
  4938. d.output = "\x86\xe5\x4f\xd4\x48\x72\x5d\x7e\x5d\xcf\xe2\x23\x53\xc8\x28"
  4939. "\xaf\x48\x78\x1e\xb4\x8c\xae\x81\x06\xa7\xe1\xd4\x98\x94\x9f"
  4940. "\x3e\x46";
  4941. d.inLen = 0;
  4942. d.outLen = WC_SHA256_DIGEST_SIZE;
  4943. test_hmac[0] = a;
  4944. test_hmac[1] = b;
  4945. test_hmac[2] = c;
  4946. test_hmac[3] = d;
  4947. for (i = 0; i < times; ++i) {
  4948. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4949. if (i == 1)
  4950. continue; /* cavium can't handle short keys, fips not allowed */
  4951. #endif
  4952. #if defined(HAVE_INTEL_QA) || defined(HAVE_CAVIUM)
  4953. if (i == 3)
  4954. continue; /* QuickAssist can't handle empty HMAC */
  4955. #endif
  4956. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4957. return WC_TEST_RET_ENC_I(i);
  4958. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[i],
  4959. (word32)XSTRLEN(keys[i]));
  4960. if (ret != 0)
  4961. return WC_TEST_RET_ENC_I(i);
  4962. if (test_hmac[i].input != NULL) {
  4963. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4964. (word32)test_hmac[i].inLen);
  4965. if (ret != 0)
  4966. return WC_TEST_RET_ENC_I(i);
  4967. }
  4968. ret = wc_HmacFinal(&hmac, hash);
  4969. if (ret != 0)
  4970. return WC_TEST_RET_ENC_I(i);
  4971. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  4972. return WC_TEST_RET_ENC_I(i);
  4973. wc_HmacFree(&hmac);
  4974. }
  4975. #ifndef HAVE_FIPS
  4976. if ((ret = wc_HmacSizeByType(WC_SHA256)) != WC_SHA256_DIGEST_SIZE)
  4977. return WC_TEST_RET_ENC_EC(ret);
  4978. if ((ret = wc_HmacSizeByType(21)) != BAD_FUNC_ARG)
  4979. return WC_TEST_RET_ENC_EC(ret);
  4980. #endif
  4981. if ((ret = wolfSSL_GetHmacMaxSize()) != WC_MAX_DIGEST_SIZE)
  4982. return WC_TEST_RET_ENC_EC(ret);
  4983. return 0;
  4984. }
  4985. #endif
  4986. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  4987. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha384_test(void)
  4988. {
  4989. Hmac hmac;
  4990. byte hash[WC_SHA384_DIGEST_SIZE];
  4991. const char* keys[]=
  4992. {
  4993. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4994. "\x0b\x0b\x0b",
  4995. "Jefe",
  4996. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4997. "\xAA\xAA\xAA",
  4998. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4999. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5000. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5001. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5002. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5003. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5004. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5005. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5006. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5007. };
  5008. testVector a, b, c, d;
  5009. testVector test_hmac[4];
  5010. wc_test_ret_t ret;
  5011. int times = sizeof(test_hmac) / sizeof(testVector), i;
  5012. a.input = "Hi There";
  5013. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  5014. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  5015. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  5016. "\xfa\x9c\xb6";
  5017. a.inLen = XSTRLEN(a.input);
  5018. a.outLen = WC_SHA384_DIGEST_SIZE;
  5019. b.input = "what do ya want for nothing?";
  5020. b.output = "\xaf\x45\xd2\xe3\x76\x48\x40\x31\x61\x7f\x78\xd2\xb5\x8a\x6b"
  5021. "\x1b\x9c\x7e\xf4\x64\xf5\xa0\x1b\x47\xe4\x2e\xc3\x73\x63\x22"
  5022. "\x44\x5e\x8e\x22\x40\xca\x5e\x69\xe2\xc7\x8b\x32\x39\xec\xfa"
  5023. "\xb2\x16\x49";
  5024. b.inLen = XSTRLEN(b.input);
  5025. b.outLen = WC_SHA384_DIGEST_SIZE;
  5026. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5027. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5028. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5029. "\xDD\xDD\xDD\xDD\xDD\xDD";
  5030. c.output = "\x88\x06\x26\x08\xd3\xe6\xad\x8a\x0a\xa2\xac\xe0\x14\xc8\xa8"
  5031. "\x6f\x0a\xa6\x35\xd9\x47\xac\x9f\xeb\xe8\x3e\xf4\xe5\x59\x66"
  5032. "\x14\x4b\x2a\x5a\xb3\x9d\xc1\x38\x14\xb9\x4e\x3a\xb6\xe1\x01"
  5033. "\xa3\x4f\x27";
  5034. c.inLen = XSTRLEN(c.input);
  5035. c.outLen = WC_SHA384_DIGEST_SIZE;
  5036. d.input = "Big Key Input";
  5037. d.output = "\xd2\x3d\x29\x6e\xf5\x1e\x23\x23\x49\x18\xb3\xbf\x4c\x38\x7b"
  5038. "\x31\x21\x17\xbb\x09\x73\x27\xf8\x12\x9d\xe9\xc6\x5d\xf9\x54"
  5039. "\xd6\x38\x5a\x68\x53\x14\xee\xe0\xa6\x4f\x36\x7e\xb2\xf3\x1a"
  5040. "\x57\x41\x69";
  5041. d.inLen = XSTRLEN(d.input);
  5042. d.outLen = WC_SHA384_DIGEST_SIZE;
  5043. test_hmac[0] = a;
  5044. test_hmac[1] = b;
  5045. test_hmac[2] = c;
  5046. test_hmac[3] = d;
  5047. for (i = 0; i < times; ++i) {
  5048. #if defined(HAVE_FIPS)
  5049. if (i == 1)
  5050. continue; /* fips not allowed */
  5051. #endif
  5052. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  5053. return WC_TEST_RET_ENC_EC(ret);
  5054. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[i],
  5055. (word32)XSTRLEN(keys[i]));
  5056. if (ret != 0)
  5057. return WC_TEST_RET_ENC_EC(ret);
  5058. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  5059. (word32)test_hmac[i].inLen);
  5060. if (ret != 0)
  5061. return WC_TEST_RET_ENC_EC(ret);
  5062. ret = wc_HmacFinal(&hmac, hash);
  5063. if (ret != 0)
  5064. return WC_TEST_RET_ENC_EC(ret);
  5065. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  5066. return WC_TEST_RET_ENC_I(i);
  5067. wc_HmacFree(&hmac);
  5068. }
  5069. #ifndef HAVE_FIPS
  5070. if ((ret = wc_HmacSizeByType(WC_SHA384)) != WC_SHA384_DIGEST_SIZE)
  5071. return WC_TEST_RET_ENC_EC(ret);
  5072. #endif
  5073. return 0;
  5074. }
  5075. #endif
  5076. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA512)
  5077. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha512_test(void)
  5078. {
  5079. Hmac hmac;
  5080. byte hash[WC_SHA512_DIGEST_SIZE];
  5081. const char* keys[]=
  5082. {
  5083. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5084. "\x0b\x0b\x0b",
  5085. "Jefe",
  5086. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  5087. "\xAA\xAA\xAA",
  5088. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5089. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5090. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5091. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5092. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5093. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5094. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5095. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5096. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5097. };
  5098. testVector a, b, c, d;
  5099. testVector test_hmac[4];
  5100. wc_test_ret_t ret;
  5101. int times = sizeof(test_hmac) / sizeof(testVector), i;
  5102. a.input = "Hi There";
  5103. a.output = "\x87\xaa\x7c\xde\xa5\xef\x61\x9d\x4f\xf0\xb4\x24\x1a\x1d\x6c"
  5104. "\xb0\x23\x79\xf4\xe2\xce\x4e\xc2\x78\x7a\xd0\xb3\x05\x45\xe1"
  5105. "\x7c\xde\xda\xa8\x33\xb7\xd6\xb8\xa7\x02\x03\x8b\x27\x4e\xae"
  5106. "\xa3\xf4\xe4\xbe\x9d\x91\x4e\xeb\x61\xf1\x70\x2e\x69\x6c\x20"
  5107. "\x3a\x12\x68\x54";
  5108. a.inLen = XSTRLEN(a.input);
  5109. a.outLen = WC_SHA512_DIGEST_SIZE;
  5110. b.input = "what do ya want for nothing?";
  5111. b.output = "\x16\x4b\x7a\x7b\xfc\xf8\x19\xe2\xe3\x95\xfb\xe7\x3b\x56\xe0"
  5112. "\xa3\x87\xbd\x64\x22\x2e\x83\x1f\xd6\x10\x27\x0c\xd7\xea\x25"
  5113. "\x05\x54\x97\x58\xbf\x75\xc0\x5a\x99\x4a\x6d\x03\x4f\x65\xf8"
  5114. "\xf0\xe6\xfd\xca\xea\xb1\xa3\x4d\x4a\x6b\x4b\x63\x6e\x07\x0a"
  5115. "\x38\xbc\xe7\x37";
  5116. b.inLen = XSTRLEN(b.input);
  5117. b.outLen = WC_SHA512_DIGEST_SIZE;
  5118. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5119. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5120. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  5121. "\xDD\xDD\xDD\xDD\xDD\xDD";
  5122. c.output = "\xfa\x73\xb0\x08\x9d\x56\xa2\x84\xef\xb0\xf0\x75\x6c\x89\x0b"
  5123. "\xe9\xb1\xb5\xdb\xdd\x8e\xe8\x1a\x36\x55\xf8\x3e\x33\xb2\x27"
  5124. "\x9d\x39\xbf\x3e\x84\x82\x79\xa7\x22\xc8\x06\xb4\x85\xa4\x7e"
  5125. "\x67\xc8\x07\xb9\x46\xa3\x37\xbe\xe8\x94\x26\x74\x27\x88\x59"
  5126. "\xe1\x32\x92\xfb";
  5127. c.inLen = XSTRLEN(c.input);
  5128. c.outLen = WC_SHA512_DIGEST_SIZE;
  5129. d.input = "Big Key Input";
  5130. d.output = "\x3f\xa9\xc9\xe1\xbd\xbb\x04\x55\x1f\xef\xcc\x92\x33\x08\xeb"
  5131. "\xcf\xc1\x9a\x5b\x5b\xc0\x7c\x86\x84\xae\x8c\x40\xaf\xb1\x27"
  5132. "\x87\x38\x92\x04\xa8\xed\xd7\xd7\x07\xa9\x85\xa0\xc2\xcd\x30"
  5133. "\xc0\x56\x14\x49\xbc\x2f\x69\x15\x6a\x97\xd8\x79\x2f\xb3\x3b"
  5134. "\x1e\x18\xfe\xfa";
  5135. d.inLen = XSTRLEN(d.input);
  5136. d.outLen = WC_SHA512_DIGEST_SIZE;
  5137. test_hmac[0] = a;
  5138. test_hmac[1] = b;
  5139. test_hmac[2] = c;
  5140. test_hmac[3] = d;
  5141. for (i = 0; i < times; ++i) {
  5142. #if defined(HAVE_FIPS)
  5143. if (i == 1)
  5144. continue; /* fips not allowed */
  5145. #endif
  5146. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  5147. return WC_TEST_RET_ENC_EC(ret);
  5148. ret = wc_HmacSetKey(&hmac, WC_SHA512, (byte*)keys[i],
  5149. (word32)XSTRLEN(keys[i]));
  5150. if (ret != 0)
  5151. return WC_TEST_RET_ENC_EC(ret);
  5152. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  5153. (word32)test_hmac[i].inLen);
  5154. if (ret != 0)
  5155. return WC_TEST_RET_ENC_EC(ret);
  5156. ret = wc_HmacFinal(&hmac, hash);
  5157. if (ret != 0)
  5158. return WC_TEST_RET_ENC_EC(ret);
  5159. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  5160. return WC_TEST_RET_ENC_I(i);
  5161. wc_HmacFree(&hmac);
  5162. }
  5163. #ifndef HAVE_FIPS
  5164. if ((ret = wc_HmacSizeByType(WC_SHA512)) != WC_SHA512_DIGEST_SIZE)
  5165. return WC_TEST_RET_ENC_EC(ret);
  5166. #endif
  5167. return 0;
  5168. }
  5169. #endif
  5170. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  5171. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  5172. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  5173. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hmac_sha3_test(void)
  5174. {
  5175. Hmac hmac;
  5176. byte hash[WC_SHA3_512_DIGEST_SIZE];
  5177. const char* key[4] =
  5178. {
  5179. "Jefe",
  5180. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  5181. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  5182. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  5183. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
  5184. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5185. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5186. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5187. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5188. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5189. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5190. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5191. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5192. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5193. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  5194. };
  5195. const char* input[4] =
  5196. {
  5197. "what do ya want for nothing?",
  5198. "Hi There",
  5199. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  5200. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  5201. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  5202. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  5203. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd",
  5204. "Big Key Input"
  5205. };
  5206. const int hashType[4] =
  5207. {
  5208. WC_SHA3_224, WC_SHA3_256, WC_SHA3_384, WC_SHA3_512
  5209. };
  5210. const int hashSz[4] =
  5211. {
  5212. WC_SHA3_224_DIGEST_SIZE, WC_SHA3_256_DIGEST_SIZE,
  5213. WC_SHA3_384_DIGEST_SIZE, WC_SHA3_512_DIGEST_SIZE
  5214. };
  5215. const char* output[16] =
  5216. {
  5217. /* key = jefe, input = what do ya want for nothing? */
  5218. /* HMAC-SHA3-224 */
  5219. "\x7f\xdb\x8d\xd8\x8b\xd2\xf6\x0d\x1b\x79\x86\x34\xad\x38\x68\x11"
  5220. "\xc2\xcf\xc8\x5b\xfa\xf5\xd5\x2b\xba\xce\x5e\x66",
  5221. /* HMAC-SHA3-256 */
  5222. "\xc7\xd4\x07\x2e\x78\x88\x77\xae\x35\x96\xbb\xb0\xda\x73\xb8\x87"
  5223. "\xc9\x17\x1f\x93\x09\x5b\x29\x4a\xe8\x57\xfb\xe2\x64\x5e\x1b\xa5",
  5224. /* HMAC-SHA3-384 */
  5225. "\xf1\x10\x1f\x8c\xbf\x97\x66\xfd\x67\x64\xd2\xed\x61\x90\x3f\x21"
  5226. "\xca\x9b\x18\xf5\x7c\xf3\xe1\xa2\x3c\xa1\x35\x08\xa9\x32\x43\xce"
  5227. "\x48\xc0\x45\xdc\x00\x7f\x26\xa2\x1b\x3f\x5e\x0e\x9d\xf4\xc2\x0a",
  5228. /* HMAC-SHA3-512 */
  5229. "\x5a\x4b\xfe\xab\x61\x66\x42\x7c\x7a\x36\x47\xb7\x47\x29\x2b\x83"
  5230. "\x84\x53\x7c\xdb\x89\xaf\xb3\xbf\x56\x65\xe4\xc5\xe7\x09\x35\x0b"
  5231. "\x28\x7b\xae\xc9\x21\xfd\x7c\xa0\xee\x7a\x0c\x31\xd0\x22\xa9\x5e"
  5232. "\x1f\xc9\x2b\xa9\xd7\x7d\xf8\x83\x96\x02\x75\xbe\xb4\xe6\x20\x24",
  5233. /* key = 0b..., input = Hi There */
  5234. /* HMAC-SHA3-224 */
  5235. "\x3b\x16\x54\x6b\xbc\x7b\xe2\x70\x6a\x03\x1d\xca\xfd\x56\x37\x3d"
  5236. "\x98\x84\x36\x76\x41\xd8\xc5\x9a\xf3\xc8\x60\xf7",
  5237. /* HMAC-SHA3-256 */
  5238. "\xba\x85\x19\x23\x10\xdf\xfa\x96\xe2\xa3\xa4\x0e\x69\x77\x43\x51"
  5239. "\x14\x0b\xb7\x18\x5e\x12\x02\xcd\xcc\x91\x75\x89\xf9\x5e\x16\xbb",
  5240. /* HMAC-SHA3-384 */
  5241. "\x68\xd2\xdc\xf7\xfd\x4d\xdd\x0a\x22\x40\xc8\xa4\x37\x30\x5f\x61"
  5242. "\xfb\x73\x34\xcf\xb5\xd0\x22\x6e\x1b\xc2\x7d\xc1\x0a\x2e\x72\x3a"
  5243. "\x20\xd3\x70\xb4\x77\x43\x13\x0e\x26\xac\x7e\x3d\x53\x28\x86\xbd",
  5244. /* HMAC-SHA3-512 */
  5245. "\xeb\x3f\xbd\x4b\x2e\xaa\xb8\xf5\xc5\x04\xbd\x3a\x41\x46\x5a\xac"
  5246. "\xec\x15\x77\x0a\x7c\xab\xac\x53\x1e\x48\x2f\x86\x0b\x5e\xc7\xba"
  5247. "\x47\xcc\xb2\xc6\xf2\xaf\xce\x8f\x88\xd2\x2b\x6d\xc6\x13\x80\xf2"
  5248. "\x3a\x66\x8f\xd3\x88\x8b\xb8\x05\x37\xc0\xa0\xb8\x64\x07\x68\x9e",
  5249. /* key = aa..., output = dd... */
  5250. /* HMAC-SHA3-224 */
  5251. "\x67\x6c\xfc\x7d\x16\x15\x36\x38\x78\x03\x90\x69\x2b\xe1\x42\xd2"
  5252. "\xdf\x7c\xe9\x24\xb9\x09\xc0\xc0\x8d\xbf\xdc\x1a",
  5253. /* HMAC-SHA3-256 */
  5254. "\x84\xec\x79\x12\x4a\x27\x10\x78\x65\xce\xdd\x8b\xd8\x2d\xa9\x96"
  5255. "\x5e\x5e\xd8\xc3\x7b\x0a\xc9\x80\x05\xa7\xf3\x9e\xd5\x8a\x42\x07",
  5256. /* HMAC-SHA3-384 */
  5257. "\x27\x5c\xd0\xe6\x61\xbb\x8b\x15\x1c\x64\xd2\x88\xf1\xf7\x82\xfb"
  5258. "\x91\xa8\xab\xd5\x68\x58\xd7\x2b\xab\xb2\xd4\x76\xf0\x45\x83\x73"
  5259. "\xb4\x1b\x6a\xb5\xbf\x17\x4b\xec\x42\x2e\x53\xfc\x31\x35\xac\x6e",
  5260. /* HMAC-SHA3-512 */
  5261. "\x30\x9e\x99\xf9\xec\x07\x5e\xc6\xc6\xd4\x75\xed\xa1\x18\x06\x87"
  5262. "\xfc\xf1\x53\x11\x95\x80\x2a\x99\xb5\x67\x74\x49\xa8\x62\x51\x82"
  5263. "\x85\x1c\xb3\x32\xaf\xb6\xa8\x9c\x41\x13\x25\xfb\xcb\xcd\x42\xaf"
  5264. "\xcb\x7b\x6e\x5a\xab\x7e\xa4\x2c\x66\x0f\x97\xfd\x85\x84\xbf\x03",
  5265. /* key = big key, input = Big Key Input */
  5266. /* HMAC-SHA3-224 */
  5267. "\x29\xe0\x5e\x46\xc4\xa4\x5e\x46\x74\xbf\xd7\x2d\x1a\xd8\x66\xdb"
  5268. "\x2d\x0d\x10\x4e\x2b\xfa\xad\x53\x7d\x15\x69\x8b",
  5269. /* HMAC-SHA3-256 */
  5270. "\xb5\x5b\x8d\x64\xb6\x9c\x21\xd0\xbf\x20\x5c\xa2\xf7\xb9\xb1\x4e"
  5271. "\x88\x21\x61\x2c\x66\xc3\x91\xae\x6c\x95\x16\x85\x83\xe6\xf4\x9b",
  5272. /* HMAC-SHA3-384 */
  5273. "\xaa\x91\xb3\xa6\x2f\x56\xa1\xbe\x8c\x3e\x74\x38\xdb\x58\xd9\xd3"
  5274. "\x34\xde\xa0\x60\x6d\x8d\x46\xe0\xec\xa9\xf6\x06\x35\x14\xe6\xed"
  5275. "\x83\xe6\x7c\x77\x24\x6c\x11\xb5\x90\x82\xb5\x75\xda\x7b\x83\x2d",
  5276. /* HMAC-SHA3-512 */
  5277. "\x1c\xc3\xa9\x24\x4a\x4a\x3f\xbd\xc7\x20\x00\x16\x9b\x79\x47\x03"
  5278. "\x78\x75\x2c\xb5\xf1\x2e\x62\x7c\xbe\xef\x4e\x8f\x0b\x11\x2b\x32"
  5279. "\xa0\xee\xc9\xd0\x4d\x64\x64\x0b\x37\xf4\xdd\x66\xf7\x8b\xb3\xad"
  5280. "\x52\x52\x6b\x65\x12\xde\x0d\x7c\xc0\x8b\x60\x01\x6c\x37\xd7\xa8"
  5281. };
  5282. int i = 0, iMax = sizeof(input) / sizeof(input[0]),
  5283. j, jMax = sizeof(hashType) / sizeof(hashType[0]),
  5284. ret;
  5285. #ifdef HAVE_FIPS
  5286. /* FIPS requires a minimum length for HMAC keys, and "Jefe" is too
  5287. * short. Skip it in FIPS builds. */
  5288. i = 1;
  5289. #endif
  5290. for (; i < iMax; i++) {
  5291. for (j = 0; j < jMax; j++) {
  5292. if ((ret = wc_HmacInit(&hmac, HEAP_HINT, devId)) != 0)
  5293. return WC_TEST_RET_ENC_EC(ret);
  5294. ret = wc_HmacSetKey(&hmac, hashType[j], (byte*)key[i],
  5295. (word32)XSTRLEN(key[i]));
  5296. if (ret != 0)
  5297. return WC_TEST_RET_ENC_EC(ret);
  5298. ret = wc_HmacUpdate(&hmac, (byte*)input[i],
  5299. (word32)XSTRLEN(input[i]));
  5300. if (ret != 0)
  5301. return WC_TEST_RET_ENC_EC(ret);
  5302. ret = wc_HmacFinal(&hmac, hash);
  5303. if (ret != 0)
  5304. return WC_TEST_RET_ENC_EC(ret);
  5305. if (XMEMCMP(hash, output[(i*jMax) + j], hashSz[j]) != 0)
  5306. return WC_TEST_RET_ENC_NC;
  5307. wc_HmacFree(&hmac);
  5308. if (i > 0)
  5309. continue;
  5310. #ifndef HAVE_FIPS
  5311. ret = wc_HmacSizeByType(hashType[j]);
  5312. if (ret != hashSz[j])
  5313. return WC_TEST_RET_ENC_EC(ret);
  5314. #endif
  5315. }
  5316. }
  5317. return 0;
  5318. }
  5319. #endif
  5320. #ifdef WC_RC2
  5321. typedef struct rc2TestVector {
  5322. const char* input;
  5323. const char* output;
  5324. const char* key; /* Key, variable up to 128 bytes */
  5325. const char* iv; /* IV, 8-bytes */
  5326. int inLen;
  5327. int outLen;
  5328. int keyLen;
  5329. int effectiveKeyBits; /* Up to 1024 bits supported */
  5330. } rc2TestVector;
  5331. static wc_test_ret_t rc2_ecb_test(void)
  5332. {
  5333. wc_test_ret_t ret = 0;
  5334. byte cipher[RC2_BLOCK_SIZE];
  5335. byte plain[RC2_BLOCK_SIZE];
  5336. rc2TestVector a, b, c, d, e, f, g, h;
  5337. rc2TestVector test_rc2[8];
  5338. int times = sizeof(test_rc2) / sizeof(rc2TestVector), i;
  5339. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5340. a.output = "\xeb\xb7\x73\xf9\x93\x27\x8e\xff";
  5341. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5342. a.inLen = RC2_BLOCK_SIZE;
  5343. a.outLen = RC2_BLOCK_SIZE;
  5344. a.keyLen = 8;
  5345. a.effectiveKeyBits = 63;
  5346. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff";
  5347. b.output = "\x27\x8b\x27\xe4\x2e\x2f\x0d\x49";
  5348. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  5349. b.inLen = RC2_BLOCK_SIZE;
  5350. b.outLen = RC2_BLOCK_SIZE;
  5351. b.keyLen = 8;
  5352. b.effectiveKeyBits = 64;
  5353. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01";
  5354. c.output = "\x30\x64\x9e\xdf\x9b\xe7\xd2\xc2";
  5355. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  5356. c.inLen = RC2_BLOCK_SIZE;
  5357. c.outLen = RC2_BLOCK_SIZE;
  5358. c.keyLen = 8;
  5359. c.effectiveKeyBits = 64;
  5360. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5361. d.output = "\x61\xa8\xa2\x44\xad\xac\xcc\xf0";
  5362. d.key = "\x88";
  5363. d.inLen = RC2_BLOCK_SIZE;
  5364. d.outLen = RC2_BLOCK_SIZE;
  5365. d.keyLen = 1;
  5366. d.effectiveKeyBits = 64;
  5367. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5368. e.output = "\x6c\xcf\x43\x08\x97\x4c\x26\x7f";
  5369. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  5370. e.inLen = RC2_BLOCK_SIZE;
  5371. e.outLen = RC2_BLOCK_SIZE;
  5372. e.keyLen = 7;
  5373. e.effectiveKeyBits = 64;
  5374. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5375. f.output = "\x1a\x80\x7d\x27\x2b\xbe\x5d\xb1";
  5376. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  5377. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  5378. f.inLen = RC2_BLOCK_SIZE;
  5379. f.outLen = RC2_BLOCK_SIZE;
  5380. f.keyLen = 16;
  5381. f.effectiveKeyBits = 64;
  5382. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5383. g.output = "\x22\x69\x55\x2a\xb0\xf8\x5c\xa6";
  5384. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  5385. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  5386. g.inLen = RC2_BLOCK_SIZE;
  5387. g.outLen = RC2_BLOCK_SIZE;
  5388. g.keyLen = 16;
  5389. g.effectiveKeyBits = 128;
  5390. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5391. h.output = "\x5b\x78\xd3\xa4\x3d\xff\xf1\xf1";
  5392. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  5393. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  5394. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  5395. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  5396. "\x1e";
  5397. h.inLen = RC2_BLOCK_SIZE;
  5398. h.outLen = RC2_BLOCK_SIZE;
  5399. h.keyLen = 33;
  5400. h.effectiveKeyBits = 129;
  5401. a.iv = b.iv = c.iv = d.iv = e.iv = f.iv = g.iv = h.iv = NULL;
  5402. test_rc2[0] = a;
  5403. test_rc2[1] = b;
  5404. test_rc2[2] = c;
  5405. test_rc2[3] = d;
  5406. test_rc2[4] = e;
  5407. test_rc2[5] = f;
  5408. test_rc2[6] = g;
  5409. test_rc2[7] = h;
  5410. for (i = 0; i < times; ++i) {
  5411. Rc2 enc;
  5412. XMEMSET(cipher, 0, RC2_BLOCK_SIZE);
  5413. XMEMSET(plain, 0, RC2_BLOCK_SIZE);
  5414. ret = wc_Rc2SetKey(&enc, (byte*)test_rc2[i].key, test_rc2[i].keyLen,
  5415. NULL, test_rc2[i].effectiveKeyBits);
  5416. if (ret != 0) {
  5417. return WC_TEST_RET_ENC_EC(ret);
  5418. }
  5419. /* ECB encrypt */
  5420. ret = wc_Rc2EcbEncrypt(&enc, cipher, (byte*)test_rc2[i].input,
  5421. (word32)test_rc2[i].outLen);
  5422. if (ret != 0) {
  5423. return WC_TEST_RET_ENC_EC(ret);
  5424. }
  5425. if (XMEMCMP(cipher, test_rc2[i].output, test_rc2[i].outLen)) {
  5426. return WC_TEST_RET_ENC_NC;
  5427. }
  5428. /* ECB decrypt */
  5429. ret = wc_Rc2EcbDecrypt(&enc, plain, cipher, RC2_BLOCK_SIZE);
  5430. if (ret != 0) {
  5431. return WC_TEST_RET_ENC_EC(ret);
  5432. }
  5433. if (XMEMCMP(plain, test_rc2[i].input, RC2_BLOCK_SIZE)) {
  5434. return WC_TEST_RET_ENC_NC;
  5435. }
  5436. }
  5437. return 0;
  5438. }
  5439. static wc_test_ret_t rc2_cbc_test(void)
  5440. {
  5441. wc_test_ret_t ret = 0;
  5442. byte cipher[128];
  5443. byte plain[128];
  5444. rc2TestVector a, b, c, d, e, f, g, h, i;
  5445. rc2TestVector test_rc2[9];
  5446. int times = sizeof(test_rc2) / sizeof(rc2TestVector), j;
  5447. /* key length = 7, effective key bits = 63 */
  5448. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  5449. "\x00\x00\x00\x00\x00\x00\x00\x00";
  5450. a.output = "\xEB\xB7\x73\xF9\x93\x27\x8E\xFF"
  5451. "\xF0\x51\x77\x8B\x65\xDB\x13\x57";
  5452. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5453. a.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5454. a.inLen = RC2_BLOCK_SIZE*2;
  5455. a.outLen = RC2_BLOCK_SIZE*2;
  5456. a.keyLen = 8;
  5457. a.effectiveKeyBits = 63;
  5458. /* key length = 8, effective key bits = 64, all 0xFF */
  5459. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff"
  5460. "\xff\xff\xff\xff\xff\xff\xff\xff";
  5461. b.output = "\xA3\xA1\x12\x65\x4F\x81\xC5\xCD"
  5462. "\xB6\x94\x3E\xEA\x3E\x8B\x9D\x1F";
  5463. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  5464. b.iv = "\xff\xff\xff\xff\xff\xff\xff\xff";
  5465. b.inLen = RC2_BLOCK_SIZE*2;
  5466. b.outLen = RC2_BLOCK_SIZE*2;
  5467. b.keyLen = 8;
  5468. b.effectiveKeyBits = 64;
  5469. /* key length = 8, effective key bits = 64 */
  5470. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01"
  5471. "\x10\x00\x00\x00\x00\x00\x00\x01";
  5472. c.output = "\xB5\x70\x14\xA2\x5F\x40\xE3\x6D"
  5473. "\x81\x99\x8D\xE0\xB5\xD5\x3A\x05";
  5474. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  5475. c.iv = "\x30\x00\x00\x00\x00\x00\x00\x00";
  5476. c.inLen = RC2_BLOCK_SIZE*2;
  5477. c.outLen = RC2_BLOCK_SIZE*2;
  5478. c.keyLen = 8;
  5479. c.effectiveKeyBits = 64;
  5480. /* key length = 1, effective key bits = 64 */
  5481. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  5482. "\x00\x00\x00\x00\x00\x00\x00\x00";
  5483. d.output = "\x61\xA8\xA2\x44\xAD\xAC\xCC\xF0"
  5484. "\x6D\x19\xE8\xF1\xFC\xE7\x38\x87";
  5485. d.key = "\x88";
  5486. d.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5487. d.inLen = RC2_BLOCK_SIZE*2;
  5488. d.outLen = RC2_BLOCK_SIZE*2;
  5489. d.keyLen = 1;
  5490. d.effectiveKeyBits = 64;
  5491. /* key length = 7, effective key bits = 64 */
  5492. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  5493. "\x00\x00\x00\x00\x00\x00\x00\x00";
  5494. e.output = "\x6C\xCF\x43\x08\x97\x4C\x26\x7F"
  5495. "\xCC\x3C\x53\x57\x7C\xA1\xA4\x4B";
  5496. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  5497. e.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5498. e.inLen = RC2_BLOCK_SIZE*2;
  5499. e.outLen = RC2_BLOCK_SIZE*2;
  5500. e.keyLen = 7;
  5501. e.effectiveKeyBits = 64;
  5502. /* key length = 16, effective key bits = 64 */
  5503. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  5504. "\x00\x00\x00\x00\x00\x00\x00\x00";
  5505. f.output = "\x1A\x80\x7D\x27\x2B\xBE\x5D\xB1"
  5506. "\x64\xEF\xE1\xC3\xB8\xAD\xFB\xBA";
  5507. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  5508. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  5509. f.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5510. f.inLen = RC2_BLOCK_SIZE*2;
  5511. f.outLen = RC2_BLOCK_SIZE*2;
  5512. f.keyLen = 16;
  5513. f.effectiveKeyBits = 64;
  5514. /* key length = 16, effective bits = 128 */
  5515. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  5516. "\x00\x00\x00\x00\x00\x00\x00\x00";
  5517. g.output = "\x22\x69\x55\x2A\xB0\xF8\x5C\xA6"
  5518. "\x53\x6E\xFD\x2D\x89\xE1\x2A\x73";
  5519. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  5520. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  5521. g.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5522. g.inLen = RC2_BLOCK_SIZE*2;
  5523. g.outLen = RC2_BLOCK_SIZE*2;
  5524. g.keyLen = 16;
  5525. g.effectiveKeyBits = 128;
  5526. /* key length = 33, effective bits = 129 */
  5527. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  5528. "\x00\x00\x00\x00\x00\x00\x00\x00";
  5529. h.output = "\x5B\x78\xD3\xA4\x3D\xFF\xF1\xF1"
  5530. "\x45\x30\xA8\xD5\xC7\x7C\x46\x19";
  5531. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  5532. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  5533. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  5534. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  5535. "\x1e";
  5536. h.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5537. h.inLen = RC2_BLOCK_SIZE*2;
  5538. h.outLen = RC2_BLOCK_SIZE*2;
  5539. h.keyLen = 33;
  5540. h.effectiveKeyBits = 129;
  5541. /* key length = 10, effective bits = 40 */
  5542. i.input = "\x11\x22\x33\x44\x55\x66\x77\x88"
  5543. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00"
  5544. "\x11\x22\x33\x44\x55\x66\x77\x88"
  5545. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00";
  5546. i.output = "\x71\x2D\x11\x99\xC9\xA0\x78\x4F"
  5547. "\xCD\xF1\x1E\x3D\xFD\x21\x7E\xDB"
  5548. "\xB2\x6E\x0D\xA4\x72\xBC\x31\x51"
  5549. "\x48\xEF\x4E\x68\x3B\xDC\xCD\x7D";
  5550. i.key = "\x26\x1E\x57\x8E\xC9\x62\xBF\xB8"
  5551. "\x3E\x96";
  5552. i.iv = "\x01\x02\x03\x04\x05\x06\x07\x08";
  5553. i.inLen = RC2_BLOCK_SIZE*4;
  5554. i.outLen = RC2_BLOCK_SIZE*4;
  5555. i.keyLen = 10;
  5556. i.effectiveKeyBits = 40;
  5557. test_rc2[0] = a;
  5558. test_rc2[1] = b;
  5559. test_rc2[2] = c;
  5560. test_rc2[3] = d;
  5561. test_rc2[4] = e;
  5562. test_rc2[5] = f;
  5563. test_rc2[6] = g;
  5564. test_rc2[7] = h;
  5565. test_rc2[8] = i;
  5566. for (j = 0; j < times; ++j) {
  5567. Rc2 rc2;
  5568. XMEMSET(cipher, 0, sizeof(cipher));
  5569. XMEMSET(plain, 0, sizeof(plain));
  5570. ret = wc_Rc2SetKey(&rc2, (byte*)test_rc2[j].key, test_rc2[j].keyLen,
  5571. (byte*)test_rc2[j].iv, test_rc2[j].effectiveKeyBits);
  5572. if (ret != 0) {
  5573. return WC_TEST_RET_ENC_EC(ret);
  5574. }
  5575. ret = wc_Rc2CbcEncrypt(&rc2, cipher, (byte*)test_rc2[j].input,
  5576. test_rc2[j].inLen);
  5577. if (ret != 0) {
  5578. return WC_TEST_RET_ENC_EC(ret);
  5579. }
  5580. if (XMEMCMP(cipher, (byte*)test_rc2[j].output, test_rc2[j].outLen)) {
  5581. return WC_TEST_RET_ENC_NC;
  5582. }
  5583. /* reset IV for decrypt, since overriden by encrypt operation */
  5584. ret = wc_Rc2SetIV(&rc2, (byte*)test_rc2[j].iv);
  5585. if (ret != 0) {
  5586. return WC_TEST_RET_ENC_EC(ret);
  5587. }
  5588. ret = wc_Rc2CbcDecrypt(&rc2, plain, cipher, test_rc2[j].outLen);
  5589. if (ret != 0) {
  5590. return WC_TEST_RET_ENC_EC(ret);
  5591. }
  5592. if (XMEMCMP(plain, (byte*)test_rc2[j].input, test_rc2[j].inLen)) {
  5593. return WC_TEST_RET_ENC_NC;
  5594. }
  5595. }
  5596. return 0;
  5597. }
  5598. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rc2_test(void)
  5599. {
  5600. wc_test_ret_t ret = 0;
  5601. ret = rc2_ecb_test();
  5602. if (ret != 0) {
  5603. return ret;
  5604. }
  5605. return rc2_cbc_test();
  5606. }
  5607. #endif
  5608. #ifndef NO_RC4
  5609. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t arc4_test(void)
  5610. {
  5611. byte cipher[16];
  5612. byte plain[16];
  5613. wc_test_ret_t ret;
  5614. const char* keys[] =
  5615. {
  5616. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  5617. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  5618. "\x00\x00\x00\x00\x00\x00\x00\x00",
  5619. "\xef\x01\x23\x45"
  5620. };
  5621. testVector a, b, c, d;
  5622. testVector test_arc4[4];
  5623. int times = sizeof(test_arc4) / sizeof(testVector), i;
  5624. a.input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  5625. a.output = "\x75\xb7\x87\x80\x99\xe0\xc5\x96";
  5626. a.inLen = 8;
  5627. a.outLen = 8;
  5628. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5629. b.output = "\x74\x94\xc2\xe7\x10\x4b\x08\x79";
  5630. b.inLen = 8;
  5631. b.outLen = 8;
  5632. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  5633. c.output = "\xde\x18\x89\x41\xa3\x37\x5d\x3a";
  5634. c.inLen = 8;
  5635. c.outLen = 8;
  5636. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  5637. d.output = "\xd6\xa1\x41\xa7\xec\x3c\x38\xdf\xbd\x61";
  5638. d.inLen = 10;
  5639. d.outLen = 10;
  5640. test_arc4[0] = a;
  5641. test_arc4[1] = b;
  5642. test_arc4[2] = c;
  5643. test_arc4[3] = d;
  5644. for (i = 0; i < times; ++i) {
  5645. Arc4 enc;
  5646. Arc4 dec;
  5647. int keylen = 8; /* XSTRLEN with key 0x00 not good */
  5648. if (i == 3)
  5649. keylen = 4;
  5650. ret = wc_Arc4Init(&enc, HEAP_HINT, devId);
  5651. if (ret != 0)
  5652. return WC_TEST_RET_ENC_EC(ret);
  5653. ret = wc_Arc4Init(&dec, HEAP_HINT, devId);
  5654. if (ret != 0)
  5655. return WC_TEST_RET_ENC_EC(ret);
  5656. ret = wc_Arc4SetKey(&enc, (byte*)keys[i], keylen);
  5657. if (ret != 0)
  5658. return WC_TEST_RET_ENC_EC(ret);
  5659. ret = wc_Arc4SetKey(&dec, (byte*)keys[i], keylen);
  5660. if (ret != 0)
  5661. return WC_TEST_RET_ENC_EC(ret);
  5662. ret = wc_Arc4Process(&enc, cipher, (byte*)test_arc4[i].input,
  5663. (word32)test_arc4[i].outLen);
  5664. if (ret != 0)
  5665. return WC_TEST_RET_ENC_EC(ret);
  5666. ret = wc_Arc4Process(&dec, plain, cipher, (word32)test_arc4[i].outLen);
  5667. if (ret != 0)
  5668. return WC_TEST_RET_ENC_EC(ret);
  5669. if (XMEMCMP(plain, test_arc4[i].input, test_arc4[i].outLen))
  5670. return WC_TEST_RET_ENC_I(i);
  5671. if (XMEMCMP(cipher, test_arc4[i].output, test_arc4[i].outLen))
  5672. return WC_TEST_RET_ENC_I(i);
  5673. wc_Arc4Free(&enc);
  5674. wc_Arc4Free(&dec);
  5675. }
  5676. return 0;
  5677. }
  5678. #endif
  5679. #ifdef HAVE_CHACHA
  5680. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t chacha_test(void)
  5681. {
  5682. ChaCha enc;
  5683. ChaCha dec;
  5684. byte cipher[128];
  5685. byte plain[128];
  5686. byte sliver[64];
  5687. byte input[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  5688. word32 keySz = 32;
  5689. wc_test_ret_t ret = 0;
  5690. int i;
  5691. int times = 4;
  5692. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  5693. {
  5694. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5695. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5696. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5697. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5698. };
  5699. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  5700. {
  5701. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5702. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5703. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5704. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  5705. };
  5706. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  5707. {
  5708. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5709. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5710. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5711. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5712. };
  5713. /* 128 bit key */
  5714. WOLFSSL_SMALL_STACK_STATIC const byte key4[] =
  5715. {
  5716. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5717. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5718. };
  5719. const byte* keys[] = {key1, key2, key3, key4};
  5720. WOLFSSL_SMALL_STACK_STATIC const byte ivs1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  5721. WOLFSSL_SMALL_STACK_STATIC const byte ivs2[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  5722. WOLFSSL_SMALL_STACK_STATIC const byte ivs3[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00};
  5723. WOLFSSL_SMALL_STACK_STATIC const byte ivs4[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  5724. const byte* ivs[] = {ivs1, ivs2, ivs3, ivs4};
  5725. #ifndef BENCH_EMBEDDED
  5726. WOLFSSL_SMALL_STACK_STATIC const byte cipher_big_result[] = {
  5727. 0x06, 0xa6, 0x5d, 0x31, 0x21, 0x6c, 0xdb, 0x37, 0x48, 0x7c, 0x01, 0x9d,
  5728. 0x72, 0xdf, 0x0a, 0x5b, 0x64, 0x74, 0x20, 0xba, 0x9e, 0xe0, 0x26, 0x7a,
  5729. 0xbf, 0xdf, 0x83, 0x34, 0x3b, 0x4f, 0x94, 0x3f, 0x37, 0x89, 0xaf, 0x00,
  5730. 0xdf, 0x0f, 0x2e, 0x75, 0x16, 0x41, 0xf6, 0x7a, 0x86, 0x94, 0x9d, 0x32,
  5731. 0x56, 0xf0, 0x79, 0x71, 0x68, 0x6f, 0xa6, 0x6b, 0xc6, 0x59, 0x49, 0xf6,
  5732. 0x10, 0x34, 0x03, 0x03, 0x16, 0x53, 0x9a, 0x98, 0x2a, 0x46, 0xde, 0x17,
  5733. 0x06, 0x65, 0x70, 0xca, 0x0a, 0x1f, 0xab, 0x80, 0x26, 0x96, 0x3f, 0x3e,
  5734. 0x7a, 0x3c, 0xa8, 0x87, 0xbb, 0x65, 0xdd, 0x5e, 0x07, 0x7b, 0x34, 0xe0,
  5735. 0x56, 0xda, 0x32, 0x13, 0x30, 0xc9, 0x0c, 0xd7, 0xba, 0xe4, 0x1f, 0xa6,
  5736. 0x91, 0x4f, 0x72, 0x9f, 0xd9, 0x5c, 0x62, 0x7d, 0xa6, 0xc2, 0xbc, 0x87,
  5737. 0xae, 0x64, 0x11, 0x94, 0x3b, 0xbc, 0x6c, 0x23, 0xbd, 0x7d, 0x00, 0xb4,
  5738. 0x99, 0xf2, 0x68, 0xb5, 0x59, 0x70, 0x93, 0xad, 0x69, 0xd0, 0xb1, 0x28,
  5739. 0x70, 0x92, 0xeb, 0xec, 0x39, 0x80, 0x82, 0xde, 0x44, 0xe2, 0x8a, 0x26,
  5740. 0xb3, 0xe9, 0x45, 0xcf, 0x83, 0x76, 0x9f, 0x6a, 0xa0, 0x46, 0x4a, 0x3d,
  5741. 0x26, 0x56, 0xaf, 0x49, 0x41, 0x26, 0x1b, 0x6a, 0x41, 0x37, 0x65, 0x91,
  5742. 0x72, 0xc4, 0xe7, 0x3c, 0x17, 0x31, 0xae, 0x2e, 0x2b, 0x31, 0x45, 0xe4,
  5743. 0x93, 0xd3, 0x10, 0xaa, 0xc5, 0x62, 0xd5, 0x11, 0x4b, 0x57, 0x1d, 0xad,
  5744. 0x48, 0x06, 0xd0, 0x0d, 0x98, 0xa5, 0xc6, 0x5b, 0xd0, 0x9e, 0x22, 0xc0,
  5745. 0x00, 0x32, 0x5a, 0xf5, 0x1c, 0x89, 0x6d, 0x54, 0x97, 0x55, 0x6b, 0x46,
  5746. 0xc5, 0xc7, 0xc4, 0x48, 0x9c, 0xbf, 0x47, 0xdc, 0x03, 0xc4, 0x1b, 0xcb,
  5747. 0x65, 0xa6, 0x91, 0x9d, 0x6d, 0xf1, 0xb0, 0x7a, 0x4d, 0x3b, 0x03, 0x95,
  5748. 0xf4, 0x8b, 0x0b, 0xae, 0x39, 0xff, 0x3f, 0xf6, 0xc0, 0x14, 0x18, 0x8a,
  5749. 0xe5, 0x19, 0xbd, 0xc1, 0xb4, 0x05, 0x4e, 0x29, 0x2f, 0x0b, 0x33, 0x76,
  5750. 0x28, 0x16, 0xa4, 0xa6, 0x93, 0x04, 0xb5, 0x55, 0x6b, 0x89, 0x3d, 0xa5,
  5751. 0x0f, 0xd3, 0xad, 0xfa, 0xd9, 0xfd, 0x05, 0x5d, 0x48, 0x94, 0x25, 0x5a,
  5752. 0x2c, 0x9a, 0x94, 0x80, 0xb0, 0xe7, 0xcb, 0x4d, 0x77, 0xbf, 0xca, 0xd8,
  5753. 0x55, 0x48, 0xbd, 0x66, 0xb1, 0x85, 0x81, 0xb1, 0x37, 0x79, 0xab, 0x52,
  5754. 0x08, 0x14, 0x12, 0xac, 0xcd, 0x45, 0x4d, 0x53, 0x6b, 0xca, 0x96, 0xc7,
  5755. 0x3b, 0x2f, 0x73, 0xb1, 0x5a, 0x23, 0xbd, 0x65, 0xd5, 0xea, 0x17, 0xb3,
  5756. 0xdc, 0xa1, 0x17, 0x1b, 0x2d, 0xb3, 0x9c, 0xd0, 0xdb, 0x41, 0x77, 0xef,
  5757. 0x93, 0x20, 0x52, 0x3e, 0x9d, 0xf5, 0xbf, 0x33, 0xf7, 0x52, 0xc1, 0x90,
  5758. 0xa0, 0x15, 0x17, 0xce, 0xf7, 0xf7, 0xd0, 0x3a, 0x3b, 0xd1, 0x72, 0x56,
  5759. 0x31, 0x81, 0xae, 0x60, 0xab, 0x40, 0xc1, 0xd1, 0x28, 0x77, 0x53, 0xac,
  5760. 0x9f, 0x11, 0x0a, 0x88, 0x36, 0x4b, 0xda, 0x57, 0xa7, 0x28, 0x5c, 0x85,
  5761. 0xd3, 0x85, 0x9b, 0x79, 0xad, 0x05, 0x1c, 0x37, 0x14, 0x5e, 0x0d, 0xd0,
  5762. 0x23, 0x03, 0x42, 0x1d, 0x48, 0x5d, 0xc5, 0x3c, 0x5a, 0x08, 0xa9, 0x0d,
  5763. 0x6e, 0x82, 0x7c, 0x2e, 0x3c, 0x41, 0xcc, 0x96, 0x8e, 0xad, 0xee, 0x2a,
  5764. 0x61, 0x0b, 0x16, 0x0f, 0xa9, 0x24, 0x40, 0x85, 0xbc, 0x9f, 0x28, 0x8d,
  5765. 0xe6, 0x68, 0x4d, 0x8f, 0x30, 0x48, 0xd9, 0x73, 0x73, 0x6c, 0x9a, 0x7f,
  5766. 0x67, 0xf7, 0xde, 0x4c, 0x0a, 0x8b, 0xe4, 0xb3, 0x08, 0x2a, 0x52, 0xda,
  5767. 0x54, 0xee, 0xcd, 0xb5, 0x62, 0x4a, 0x26, 0x20, 0xfb, 0x40, 0xbb, 0x39,
  5768. 0x3a, 0x0f, 0x09, 0xe8, 0x00, 0xd1, 0x24, 0x97, 0x60, 0xe9, 0x83, 0x83,
  5769. 0xfe, 0x9f, 0x9c, 0x15, 0xcf, 0x69, 0x03, 0x9f, 0x03, 0xe1, 0xe8, 0x6e,
  5770. 0xbd, 0x87, 0x58, 0x68, 0xee, 0xec, 0xd8, 0x29, 0x46, 0x23, 0x49, 0x92,
  5771. 0x72, 0x95, 0x5b, 0x49, 0xca, 0xe0, 0x45, 0x59, 0xb2, 0xca, 0xf4, 0xfc,
  5772. 0xb7, 0x59, 0x37, 0x49, 0x28, 0xbc, 0xf3, 0xd7, 0x61, 0xbc, 0x4b, 0xf3,
  5773. 0xa9, 0x4b, 0x2f, 0x05, 0xa8, 0x01, 0xa5, 0xdc, 0x00, 0x6e, 0x01, 0xb6,
  5774. 0x45, 0x3c, 0xd5, 0x49, 0x7d, 0x5c, 0x25, 0xe8, 0x31, 0x87, 0xb2, 0xb9,
  5775. 0xbf, 0xb3, 0x01, 0x62, 0x0c, 0xd0, 0x48, 0x77, 0xa2, 0x34, 0x0f, 0x16,
  5776. 0x22, 0x28, 0xee, 0x54, 0x08, 0x93, 0x3b, 0xe4, 0xde, 0x7e, 0x63, 0xf7,
  5777. 0x97, 0x16, 0x5d, 0x71, 0x58, 0xc2, 0x2e, 0xf2, 0x36, 0xa6, 0x12, 0x65,
  5778. 0x94, 0x17, 0xac, 0x66, 0x23, 0x7e, 0xc6, 0x72, 0x79, 0x24, 0xce, 0x8f,
  5779. 0x55, 0x19, 0x97, 0x44, 0xfc, 0x55, 0xec, 0x85, 0x26, 0x27, 0xdb, 0x38,
  5780. 0xb1, 0x42, 0x0a, 0xdd, 0x05, 0x99, 0x28, 0xeb, 0x03, 0x6c, 0x9a, 0xe9,
  5781. 0x17, 0xf6, 0x2c, 0xb0, 0xfe, 0xe7, 0xa4, 0xa7, 0x31, 0xda, 0x4d, 0xb0,
  5782. 0x29, 0xdb, 0xdd, 0x8d, 0x12, 0x13, 0x9c, 0xb4, 0xcc, 0x83, 0x97, 0xfb,
  5783. 0x1a, 0xdc, 0x08, 0xd6, 0x30, 0x62, 0xe8, 0xeb, 0x8b, 0x61, 0xcb, 0x1d,
  5784. 0x06, 0xe3, 0xa5, 0x4d, 0x35, 0xdb, 0x59, 0xa8, 0x2d, 0x87, 0x27, 0x44,
  5785. 0x6f, 0xc0, 0x38, 0x97, 0xe4, 0x85, 0x00, 0x02, 0x09, 0xf6, 0x69, 0x3a,
  5786. 0xcf, 0x08, 0x1b, 0x21, 0xbb, 0x79, 0xb1, 0xa1, 0x34, 0x09, 0xe0, 0x80,
  5787. 0xca, 0xb0, 0x78, 0x8a, 0x11, 0x97, 0xd4, 0x07, 0xbe, 0x1b, 0x6a, 0x5d,
  5788. 0xdb, 0xd6, 0x1f, 0x76, 0x6b, 0x16, 0xf0, 0x58, 0x84, 0x5f, 0x59, 0xce,
  5789. 0x62, 0x34, 0xc3, 0xdf, 0x94, 0xb8, 0x2f, 0x84, 0x68, 0xf0, 0xb8, 0x51,
  5790. 0xd9, 0x6d, 0x8e, 0x4a, 0x1d, 0xe6, 0x5c, 0xd8, 0x86, 0x25, 0xe3, 0x24,
  5791. 0xfd, 0x21, 0x61, 0x13, 0x48, 0x3e, 0xf6, 0x7d, 0xa6, 0x71, 0x9b, 0xd2,
  5792. 0x6e, 0xe6, 0xd2, 0x08, 0x94, 0x62, 0x6c, 0x98, 0xfe, 0x2f, 0x9c, 0x88,
  5793. 0x7e, 0x78, 0x15, 0x02, 0x00, 0xf0, 0xba, 0x24, 0x91, 0xf2, 0xdc, 0x47,
  5794. 0x51, 0x4d, 0x15, 0x5e, 0x91, 0x5f, 0x57, 0x5b, 0x1d, 0x35, 0x24, 0x45,
  5795. 0x75, 0x9b, 0x88, 0x75, 0xf1, 0x2f, 0x85, 0xe7, 0x89, 0xd1, 0x01, 0xb4,
  5796. 0xc8, 0x18, 0xb7, 0x97, 0xef, 0x4b, 0x90, 0xf4, 0xbf, 0x10, 0x27, 0x3c,
  5797. 0x60, 0xff, 0xc4, 0x94, 0x20, 0x2f, 0x93, 0x4b, 0x4d, 0xe3, 0x80, 0xf7,
  5798. 0x2c, 0x71, 0xd9, 0xe3, 0x68, 0xb4, 0x77, 0x2b, 0xc7, 0x0d, 0x39, 0x92,
  5799. 0xef, 0x91, 0x0d, 0xb2, 0x11, 0x50, 0x0e, 0xe8, 0xad, 0x3b, 0xf6, 0xb5,
  5800. 0xc6, 0x14, 0x4d, 0x33, 0x53, 0xa7, 0x60, 0x15, 0xc7, 0x27, 0x51, 0xdc,
  5801. 0x54, 0x29, 0xa7, 0x0d, 0x6a, 0x7b, 0x72, 0x13, 0xad, 0x7d, 0x41, 0x19,
  5802. 0x4e, 0x42, 0x49, 0xcc, 0x42, 0xe4, 0xbd, 0x99, 0x13, 0xd9, 0x7f, 0xf3,
  5803. 0x38, 0xa4, 0xb6, 0x33, 0xed, 0x07, 0x48, 0x7e, 0x8e, 0x82, 0xfe, 0x3a,
  5804. 0x9d, 0x75, 0x93, 0xba, 0x25, 0x4e, 0x37, 0x3c, 0x0c, 0xd5, 0x69, 0xa9,
  5805. 0x2d, 0x9e, 0xfd, 0xe8, 0xbb, 0xf5, 0x0c, 0xe2, 0x86, 0xb9, 0x5e, 0x6f,
  5806. 0x28, 0xe4, 0x19, 0xb3, 0x0b, 0xa4, 0x86, 0xd7, 0x24, 0xd0, 0xb8, 0x89,
  5807. 0x7b, 0x76, 0xec, 0x05, 0x10, 0x5b, 0x68, 0xe9, 0x58, 0x66, 0xa3, 0xc5,
  5808. 0xb6, 0x63, 0x20, 0x0e, 0x0e, 0xea, 0x3d, 0x61, 0x5e, 0xda, 0x3d, 0x3c,
  5809. 0xf9, 0xfd, 0xed, 0xa9, 0xdb, 0x52, 0x94, 0x8a, 0x00, 0xca, 0x3c, 0x8d,
  5810. 0x66, 0x8f, 0xb0, 0xf0, 0x5a, 0xca, 0x3f, 0x63, 0x71, 0xbf, 0xca, 0x99,
  5811. 0x37, 0x9b, 0x75, 0x97, 0x89, 0x10, 0x6e, 0xcf, 0xf2, 0xf5, 0xe3, 0xd5,
  5812. 0x45, 0x9b, 0xad, 0x10, 0x71, 0x6c, 0x5f, 0x6f, 0x7f, 0x22, 0x77, 0x18,
  5813. 0x2f, 0xf9, 0x99, 0xc5, 0x69, 0x58, 0x03, 0x12, 0x86, 0x82, 0x3e, 0xbf,
  5814. 0xc2, 0x12, 0x35, 0x43, 0xa3, 0xd9, 0x18, 0x4f, 0x41, 0x11, 0x6b, 0xf3,
  5815. 0x67, 0xaf, 0x3d, 0x78, 0xe4, 0x22, 0x2d, 0xb3, 0x48, 0x43, 0x31, 0x1d,
  5816. 0xef, 0xa8, 0xba, 0x49, 0x8e, 0xa9, 0xa7, 0xb6, 0x18, 0x77, 0x84, 0xca,
  5817. 0xbd, 0xa2, 0x02, 0x1b, 0x6a, 0xf8, 0x5f, 0xda, 0xff, 0xcf, 0x01, 0x6a,
  5818. 0x86, 0x69, 0xa9, 0xe9, 0xcb, 0x60, 0x1e, 0x15, 0xdc, 0x8f, 0x5d, 0x39,
  5819. 0xb5, 0xce, 0x55, 0x5f, 0x47, 0x97, 0xb1, 0x19, 0x6e, 0x21, 0xd6, 0x13,
  5820. 0x39, 0xb2, 0x24, 0xe0, 0x62, 0x82, 0x9f, 0xed, 0x12, 0x81, 0xed, 0xee,
  5821. 0xab, 0xd0, 0x2f, 0x19, 0x89, 0x3f, 0x57, 0x2e, 0xc2, 0xe2, 0x67, 0xe8,
  5822. 0xae, 0x03, 0x56, 0xba, 0xd4, 0xd0, 0xa4, 0x89, 0x03, 0x06, 0x5b, 0xcc,
  5823. 0xf2, 0x22, 0xb8, 0x0e, 0x76, 0x79, 0x4a, 0x42, 0x1d, 0x37, 0x51, 0x5a,
  5824. 0xaa, 0x46, 0x6c, 0x2a, 0xdd, 0x66, 0xfe, 0xc6, 0x68, 0xc3, 0x38, 0xa2,
  5825. 0xae, 0x5b, 0x98, 0x24, 0x5d, 0x43, 0x05, 0x82, 0x38, 0x12, 0xd3, 0xd1,
  5826. 0x75, 0x2d, 0x4f, 0x61, 0xbd, 0xb9, 0x10, 0x87, 0x44, 0x2a, 0x78, 0x07,
  5827. 0xff, 0xf4, 0x0f, 0xa1, 0xf3, 0x68, 0x9f, 0xbe, 0xae, 0xa2, 0x91, 0xf0,
  5828. 0xc7, 0x55, 0x7a, 0x52, 0xd5, 0xa3, 0x8d, 0x6f, 0xe4, 0x90, 0x5c, 0xf3,
  5829. 0x5f, 0xce, 0x3d, 0x23, 0xf9, 0x8e, 0xae, 0x14, 0xfb, 0x82, 0x9a, 0xa3,
  5830. 0x04, 0x5f, 0xbf, 0xad, 0x3e, 0xf2, 0x97, 0x0a, 0x60, 0x40, 0x70, 0x19,
  5831. 0x72, 0xad, 0x66, 0xfb, 0x78, 0x1b, 0x84, 0x6c, 0x98, 0xbc, 0x8c, 0xf8,
  5832. 0x4f, 0xcb, 0xb5, 0xf6, 0xaf, 0x7a, 0xb7, 0x93, 0xef, 0x67, 0x48, 0x02,
  5833. 0x2c, 0xcb, 0xe6, 0x77, 0x0f, 0x7b, 0xc1, 0xee, 0xc5, 0xb6, 0x2d, 0x7e,
  5834. 0x62, 0xa0, 0xc0, 0xa7, 0xa5, 0x80, 0x31, 0x92, 0x50, 0xa1, 0x28, 0x22,
  5835. 0x95, 0x03, 0x17, 0xd1, 0x0f, 0xf6, 0x08, 0xe5, 0xec
  5836. };
  5837. #define CHACHA_BIG_TEST_SIZE 1305
  5838. #if !defined(WOLFSSL_SMALL_STACK) || defined(WOLFSSL_NO_MALLOC)
  5839. byte cipher_big[CHACHA_BIG_TEST_SIZE] = {0};
  5840. byte plain_big[CHACHA_BIG_TEST_SIZE] = {0};
  5841. byte input_big[CHACHA_BIG_TEST_SIZE] = {0};
  5842. #else
  5843. byte* cipher_big;
  5844. byte* plain_big;
  5845. byte* input_big;
  5846. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  5847. int block_size;
  5848. #endif /* BENCH_EMBEDDED */
  5849. byte a[] = {0x76,0xb8,0xe0,0xad,0xa0,0xf1,0x3d,0x90};
  5850. byte b[] = {0x45,0x40,0xf0,0x5a,0x9f,0x1f,0xb2,0x96};
  5851. byte c[] = {0xde,0x9c,0xba,0x7b,0xf3,0xd6,0x9e,0xf5};
  5852. byte d[] = {0x89,0x67,0x09,0x52,0x60,0x83,0x64,0xfd};
  5853. byte* test_chacha[4];
  5854. test_chacha[0] = a;
  5855. test_chacha[1] = b;
  5856. test_chacha[2] = c;
  5857. test_chacha[3] = d;
  5858. #ifndef BENCH_EMBEDDED
  5859. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  5860. cipher_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  5861. DYNAMIC_TYPE_TMP_BUFFER);
  5862. if (cipher_big == NULL) {
  5863. return MEMORY_E;
  5864. }
  5865. plain_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  5866. DYNAMIC_TYPE_TMP_BUFFER);
  5867. if (plain_big == NULL) {
  5868. return MEMORY_E;
  5869. }
  5870. input_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  5871. DYNAMIC_TYPE_TMP_BUFFER);
  5872. if (input_big == NULL) {
  5873. return MEMORY_E;
  5874. }
  5875. XMEMSET(cipher_big, 0, CHACHA_BIG_TEST_SIZE);
  5876. XMEMSET(plain_big, 0, CHACHA_BIG_TEST_SIZE);
  5877. XMEMSET(input_big, 0, CHACHA_BIG_TEST_SIZE);
  5878. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  5879. #endif /* BENCH_EMBEDDED */
  5880. for (i = 0; i < times; ++i) {
  5881. if (i < 3) {
  5882. keySz = 32;
  5883. }
  5884. else {
  5885. keySz = 16;
  5886. }
  5887. XMEMCPY(plain, keys[i], keySz);
  5888. XMEMSET(cipher, 0, 32);
  5889. XMEMCPY(cipher + 4, ivs[i], 8);
  5890. ret |= wc_Chacha_SetKey(&enc, keys[i], keySz);
  5891. ret |= wc_Chacha_SetKey(&dec, keys[i], keySz);
  5892. if (ret != 0)
  5893. return ret;
  5894. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  5895. ret |= wc_Chacha_SetIV(&dec, cipher, 0);
  5896. if (ret != 0)
  5897. return ret;
  5898. XMEMCPY(plain, input, 8);
  5899. ret |= wc_Chacha_Process(&enc, cipher, plain, (word32)8);
  5900. ret |= wc_Chacha_Process(&dec, plain, cipher, (word32)8);
  5901. if (ret != 0)
  5902. return ret;
  5903. if (XMEMCMP(test_chacha[i], cipher, 8))
  5904. return WC_TEST_RET_ENC_I(i);
  5905. if (XMEMCMP(plain, input, 8))
  5906. return WC_TEST_RET_ENC_I(i);
  5907. }
  5908. /* test of starting at a different counter
  5909. encrypts all of the information and decrypts starting at 2nd chunk */
  5910. XMEMSET(plain, 0, sizeof(plain));
  5911. XMEMSET(sliver, 1, sizeof(sliver)); /* set as 1's to not match plain */
  5912. XMEMSET(cipher, 0, sizeof(cipher));
  5913. XMEMCPY(cipher + 4, ivs[0], 8);
  5914. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  5915. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  5916. if (ret != 0)
  5917. return ret;
  5918. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  5919. ret |= wc_Chacha_SetIV(&dec, cipher, 1);
  5920. if (ret != 0)
  5921. return ret;
  5922. ret |= wc_Chacha_Process(&enc, cipher, plain, sizeof(plain));
  5923. ret |= wc_Chacha_Process(&dec, sliver, cipher + 64, sizeof(sliver));
  5924. if (ret != 0)
  5925. return ret;
  5926. if (XMEMCMP(plain + 64, sliver, 64))
  5927. return WC_TEST_RET_ENC_NC;
  5928. #ifndef BENCH_EMBEDDED
  5929. /* test of encrypting more data */
  5930. keySz = 32;
  5931. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  5932. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  5933. if (ret != 0)
  5934. return ret;
  5935. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  5936. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  5937. if (ret != 0)
  5938. return ret;
  5939. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big, CHACHA_BIG_TEST_SIZE);
  5940. ret |= wc_Chacha_Process(&dec, plain_big, cipher_big,
  5941. CHACHA_BIG_TEST_SIZE);
  5942. if (ret != 0)
  5943. return ret;
  5944. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  5945. return WC_TEST_RET_ENC_NC;
  5946. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  5947. return WC_TEST_RET_ENC_NC;
  5948. for (i = 0; i < 18; ++i) {
  5949. /* this will test all paths
  5950. * block sizes: 1 3 7 15 31 63 127 255 511 (i = 0- 8)
  5951. * 2 4 8 16 32 64 128 256 512 (i = 9-17)
  5952. */
  5953. block_size = (2 << (i%9)) - (i<9?1:0);
  5954. keySz = 32;
  5955. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  5956. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  5957. if (ret != 0)
  5958. return ret;
  5959. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  5960. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  5961. if (ret != 0)
  5962. return ret;
  5963. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big , block_size);
  5964. ret |= wc_Chacha_Process(&dec, plain_big , cipher_big, block_size);
  5965. if (ret != 0)
  5966. return ret;
  5967. if (XMEMCMP(plain_big, input_big, block_size))
  5968. return WC_TEST_RET_ENC_I(i);
  5969. if (XMEMCMP(cipher_big, cipher_big_result, block_size))
  5970. return WC_TEST_RET_ENC_I(i);
  5971. }
  5972. /* Streaming test */
  5973. for (i = 1; i <= (int)CHACHA_CHUNK_BYTES + 1; i++) {
  5974. int j, rem;
  5975. ret = wc_Chacha_SetKey(&enc, keys[0], keySz);
  5976. if (ret != 0)
  5977. return WC_TEST_RET_ENC_EC(ret);
  5978. ret = wc_Chacha_SetKey(&dec, keys[0], keySz);
  5979. if (ret != 0)
  5980. return WC_TEST_RET_ENC_EC(ret);
  5981. ret = wc_Chacha_SetIV(&enc, ivs[2], 0);
  5982. if (ret != 0)
  5983. return WC_TEST_RET_ENC_EC(ret);
  5984. ret = wc_Chacha_SetIV(&dec, ivs[2], 0);
  5985. if (ret != 0)
  5986. return WC_TEST_RET_ENC_EC(ret);
  5987. for (j = 0; j < CHACHA_BIG_TEST_SIZE - i; j+= i) {
  5988. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, i);
  5989. if (ret != 0)
  5990. return WC_TEST_RET_ENC_EC(ret);
  5991. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, i);
  5992. if (ret != 0)
  5993. return WC_TEST_RET_ENC_EC(ret);
  5994. }
  5995. rem = CHACHA_BIG_TEST_SIZE - j;
  5996. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, rem);
  5997. if (ret != 0)
  5998. return WC_TEST_RET_ENC_EC(ret);
  5999. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, rem);
  6000. if (ret != 0)
  6001. return WC_TEST_RET_ENC_EC(ret);
  6002. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  6003. return WC_TEST_RET_ENC_NC;
  6004. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  6005. return WC_TEST_RET_ENC_NC;
  6006. }
  6007. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6008. XFREE(cipher_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6009. XFREE(plain_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6010. XFREE(input_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6011. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  6012. #endif /* BENCH_EMBEDDED */
  6013. return 0;
  6014. }
  6015. #endif /* HAVE_CHACHA */
  6016. #ifdef HAVE_POLY1305
  6017. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t poly1305_test(void)
  6018. {
  6019. wc_test_ret_t ret = 0;
  6020. int i;
  6021. byte tag[16];
  6022. Poly1305 enc;
  6023. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  6024. {
  6025. 0x43,0x72,0x79,0x70,0x74,0x6f,0x67,0x72,
  6026. 0x61,0x70,0x68,0x69,0x63,0x20,0x46,0x6f,
  6027. 0x72,0x75,0x6d,0x20,0x52,0x65,0x73,0x65,
  6028. 0x61,0x72,0x63,0x68,0x20,0x47,0x72,0x6f,
  6029. 0x75,0x70
  6030. };
  6031. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  6032. {
  6033. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x77,0x6f,0x72,
  6034. 0x6c,0x64,0x21
  6035. };
  6036. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  6037. {
  6038. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6039. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6040. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6041. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6042. };
  6043. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] =
  6044. {
  6045. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  6046. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  6047. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  6048. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  6049. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  6050. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  6051. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  6052. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  6053. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  6054. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  6055. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  6056. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  6057. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  6058. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  6059. 0x61,0x16
  6060. };
  6061. WOLFSSL_SMALL_STACK_STATIC const byte msg5[] =
  6062. {
  6063. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  6064. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  6065. };
  6066. WOLFSSL_SMALL_STACK_STATIC const byte msg6[] =
  6067. {
  6068. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  6069. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  6070. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  6071. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  6072. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  6073. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  6074. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  6075. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  6076. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  6077. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  6078. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  6079. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  6080. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  6081. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  6082. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  6083. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  6084. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  6085. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  6086. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  6087. 0x61,0x16
  6088. };
  6089. byte additional[] =
  6090. {
  6091. 0x50,0x51,0x52,0x53,0xc0,0xc1,0xc2,0xc3,
  6092. 0xc4,0xc5,0xc6,0xc7
  6093. };
  6094. WOLFSSL_SMALL_STACK_STATIC const byte correct0[] =
  6095. {
  6096. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  6097. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  6098. };
  6099. WOLFSSL_SMALL_STACK_STATIC const byte correct1[] =
  6100. {
  6101. 0xa8,0x06,0x1d,0xc1,0x30,0x51,0x36,0xc6,
  6102. 0xc2,0x2b,0x8b,0xaf,0x0c,0x01,0x27,0xa9
  6103. };
  6104. WOLFSSL_SMALL_STACK_STATIC const byte correct2[] =
  6105. {
  6106. 0xa6,0xf7,0x45,0x00,0x8f,0x81,0xc9,0x16,
  6107. 0xa2,0x0d,0xcc,0x74,0xee,0xf2,0xb2,0xf0
  6108. };
  6109. WOLFSSL_SMALL_STACK_STATIC const byte correct3[] =
  6110. {
  6111. 0x49,0xec,0x78,0x09,0x0e,0x48,0x1e,0xc6,
  6112. 0xc2,0x6b,0x33,0xb9,0x1c,0xcc,0x03,0x07
  6113. };
  6114. WOLFSSL_SMALL_STACK_STATIC const byte correct4[] =
  6115. {
  6116. 0x1a,0xe1,0x0b,0x59,0x4f,0x09,0xe2,0x6a,
  6117. 0x7e,0x90,0x2e,0xcb,0xd0,0x60,0x06,0x91
  6118. };
  6119. WOLFSSL_SMALL_STACK_STATIC const byte correct5[] =
  6120. {
  6121. 0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6122. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6123. };
  6124. WOLFSSL_SMALL_STACK_STATIC const byte correct6[] =
  6125. {
  6126. 0xea,0x11,0x5c,0x4f,0xd0,0xc0,0x10,0xae,
  6127. 0xf7,0xdf,0xda,0x77,0xa2,0xe9,0xaf,0xca
  6128. };
  6129. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  6130. 0x85,0xd6,0xbe,0x78,0x57,0x55,0x6d,0x33,
  6131. 0x7f,0x44,0x52,0xfe,0x42,0xd5,0x06,0xa8,
  6132. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  6133. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  6134. };
  6135. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  6136. 0x74,0x68,0x69,0x73,0x20,0x69,0x73,0x20,
  6137. 0x33,0x32,0x2d,0x62,0x79,0x74,0x65,0x20,
  6138. 0x6b,0x65,0x79,0x20,0x66,0x6f,0x72,0x20,
  6139. 0x50,0x6f,0x6c,0x79,0x31,0x33,0x30,0x35
  6140. };
  6141. WOLFSSL_SMALL_STACK_STATIC const byte key4[] = {
  6142. 0x7b,0xac,0x2b,0x25,0x2d,0xb4,0x47,0xaf,
  6143. 0x09,0xb6,0x7a,0x55,0xa4,0xe9,0x55,0x84,
  6144. 0x0a,0xe1,0xd6,0x73,0x10,0x75,0xd9,0xeb,
  6145. 0x2a,0x93,0x75,0x78,0x3e,0xd5,0x53,0xff
  6146. };
  6147. WOLFSSL_SMALL_STACK_STATIC const byte key5[] = {
  6148. 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6149. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6150. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6151. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6152. };
  6153. const byte* msgs[] = {NULL, msg1, msg2, msg3, msg5, msg6};
  6154. word32 szm[] = {0, sizeof(msg1), sizeof(msg2),
  6155. sizeof(msg3), sizeof(msg5), sizeof(msg6)};
  6156. const byte* keys[] = {key, key, key2, key2, key5, key};
  6157. const byte* tests[] = {correct0, correct1, correct2, correct3, correct5,
  6158. correct6};
  6159. for (i = 0; i < 6; i++) {
  6160. ret = wc_Poly1305SetKey(&enc, keys[i], 32);
  6161. if (ret != 0)
  6162. return WC_TEST_RET_ENC_I(i);
  6163. ret = wc_Poly1305Update(&enc, msgs[i], szm[i]);
  6164. if (ret != 0)
  6165. return WC_TEST_RET_ENC_I(i);
  6166. ret = wc_Poly1305Final(&enc, tag);
  6167. if (ret != 0)
  6168. return WC_TEST_RET_ENC_I(i);
  6169. if (XMEMCMP(tag, tests[i], sizeof(tag)))
  6170. return WC_TEST_RET_ENC_I(i);
  6171. }
  6172. /* Check TLS MAC function from 2.8.2 https://tools.ietf.org/html/rfc7539 */
  6173. XMEMSET(tag, 0, sizeof(tag));
  6174. ret = wc_Poly1305SetKey(&enc, key4, sizeof(key4));
  6175. if (ret != 0)
  6176. return WC_TEST_RET_ENC_EC(ret);
  6177. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  6178. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  6179. if (ret != 0)
  6180. return WC_TEST_RET_ENC_EC(ret);
  6181. if (XMEMCMP(tag, correct4, sizeof(tag)))
  6182. return WC_TEST_RET_ENC_NC;
  6183. /* Check fail of TLS MAC function if altering additional data */
  6184. XMEMSET(tag, 0, sizeof(tag));
  6185. additional[0]++;
  6186. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  6187. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  6188. if (ret != 0)
  6189. return WC_TEST_RET_ENC_EC(ret);
  6190. if (XMEMCMP(tag, correct4, sizeof(tag)) == 0)
  6191. return WC_TEST_RET_ENC_NC;
  6192. return 0;
  6193. }
  6194. #endif /* HAVE_POLY1305 */
  6195. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  6196. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t chacha20_poly1305_aead_test(void)
  6197. {
  6198. /* Test #1 from Section 2.8.2 of draft-irtf-cfrg-chacha20-poly1305-10 */
  6199. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  6200. WOLFSSL_SMALL_STACK_STATIC const byte key1[] = {
  6201. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  6202. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  6203. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  6204. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  6205. };
  6206. WOLFSSL_SMALL_STACK_STATIC const byte plaintext1[] = {
  6207. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  6208. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  6209. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  6210. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  6211. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  6212. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  6213. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  6214. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  6215. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  6216. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  6217. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  6218. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  6219. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  6220. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  6221. 0x74, 0x2e
  6222. };
  6223. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] = {
  6224. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  6225. 0x44, 0x45, 0x46, 0x47
  6226. };
  6227. WOLFSSL_SMALL_STACK_STATIC const byte aad1[] = { /* additional data */
  6228. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  6229. 0xc4, 0xc5, 0xc6, 0xc7
  6230. };
  6231. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] = { /* expected output from operation */
  6232. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  6233. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  6234. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  6235. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  6236. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  6237. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  6238. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  6239. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  6240. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  6241. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  6242. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  6243. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  6244. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  6245. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  6246. 0x61, 0x16
  6247. };
  6248. WOLFSSL_SMALL_STACK_STATIC const byte authTag1[] = { /* expected output from operation */
  6249. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  6250. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  6251. };
  6252. /* Test #2 from Appendix A.2 in draft-irtf-cfrg-chacha20-poly1305-10 */
  6253. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  6254. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  6255. 0x1c, 0x92, 0x40, 0xa5, 0xeb, 0x55, 0xd3, 0x8a,
  6256. 0xf3, 0x33, 0x88, 0x86, 0x04, 0xf6, 0xb5, 0xf0,
  6257. 0x47, 0x39, 0x17, 0xc1, 0x40, 0x2b, 0x80, 0x09,
  6258. 0x9d, 0xca, 0x5c, 0xbc, 0x20, 0x70, 0x75, 0xc0
  6259. };
  6260. WOLFSSL_SMALL_STACK_STATIC const byte plaintext2[] = {
  6261. 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74,
  6262. 0x2d, 0x44, 0x72, 0x61, 0x66, 0x74, 0x73, 0x20,
  6263. 0x61, 0x72, 0x65, 0x20, 0x64, 0x72, 0x61, 0x66,
  6264. 0x74, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  6265. 0x6e, 0x74, 0x73, 0x20, 0x76, 0x61, 0x6c, 0x69,
  6266. 0x64, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x61, 0x20,
  6267. 0x6d, 0x61, 0x78, 0x69, 0x6d, 0x75, 0x6d, 0x20,
  6268. 0x6f, 0x66, 0x20, 0x73, 0x69, 0x78, 0x20, 0x6d,
  6269. 0x6f, 0x6e, 0x74, 0x68, 0x73, 0x20, 0x61, 0x6e,
  6270. 0x64, 0x20, 0x6d, 0x61, 0x79, 0x20, 0x62, 0x65,
  6271. 0x20, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64,
  6272. 0x2c, 0x20, 0x72, 0x65, 0x70, 0x6c, 0x61, 0x63,
  6273. 0x65, 0x64, 0x2c, 0x20, 0x6f, 0x72, 0x20, 0x6f,
  6274. 0x62, 0x73, 0x6f, 0x6c, 0x65, 0x74, 0x65, 0x64,
  6275. 0x20, 0x62, 0x79, 0x20, 0x6f, 0x74, 0x68, 0x65,
  6276. 0x72, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  6277. 0x6e, 0x74, 0x73, 0x20, 0x61, 0x74, 0x20, 0x61,
  6278. 0x6e, 0x79, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x2e,
  6279. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x69,
  6280. 0x6e, 0x61, 0x70, 0x70, 0x72, 0x6f, 0x70, 0x72,
  6281. 0x69, 0x61, 0x74, 0x65, 0x20, 0x74, 0x6f, 0x20,
  6282. 0x75, 0x73, 0x65, 0x20, 0x49, 0x6e, 0x74, 0x65,
  6283. 0x72, 0x6e, 0x65, 0x74, 0x2d, 0x44, 0x72, 0x61,
  6284. 0x66, 0x74, 0x73, 0x20, 0x61, 0x73, 0x20, 0x72,
  6285. 0x65, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x65,
  6286. 0x20, 0x6d, 0x61, 0x74, 0x65, 0x72, 0x69, 0x61,
  6287. 0x6c, 0x20, 0x6f, 0x72, 0x20, 0x74, 0x6f, 0x20,
  6288. 0x63, 0x69, 0x74, 0x65, 0x20, 0x74, 0x68, 0x65,
  6289. 0x6d, 0x20, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20,
  6290. 0x74, 0x68, 0x61, 0x6e, 0x20, 0x61, 0x73, 0x20,
  6291. 0x2f, 0xe2, 0x80, 0x9c, 0x77, 0x6f, 0x72, 0x6b,
  6292. 0x20, 0x69, 0x6e, 0x20, 0x70, 0x72, 0x6f, 0x67,
  6293. 0x72, 0x65, 0x73, 0x73, 0x2e, 0x2f, 0xe2, 0x80,
  6294. 0x9d
  6295. };
  6296. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  6297. 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
  6298. 0x05, 0x06, 0x07, 0x08
  6299. };
  6300. WOLFSSL_SMALL_STACK_STATIC const byte aad2[] = { /* additional data */
  6301. 0xf3, 0x33, 0x88, 0x86, 0x00, 0x00, 0x00, 0x00,
  6302. 0x00, 0x00, 0x4e, 0x91
  6303. };
  6304. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] = { /* expected output from operation */
  6305. 0x64, 0xa0, 0x86, 0x15, 0x75, 0x86, 0x1a, 0xf4,
  6306. 0x60, 0xf0, 0x62, 0xc7, 0x9b, 0xe6, 0x43, 0xbd,
  6307. 0x5e, 0x80, 0x5c, 0xfd, 0x34, 0x5c, 0xf3, 0x89,
  6308. 0xf1, 0x08, 0x67, 0x0a, 0xc7, 0x6c, 0x8c, 0xb2,
  6309. 0x4c, 0x6c, 0xfc, 0x18, 0x75, 0x5d, 0x43, 0xee,
  6310. 0xa0, 0x9e, 0xe9, 0x4e, 0x38, 0x2d, 0x26, 0xb0,
  6311. 0xbd, 0xb7, 0xb7, 0x3c, 0x32, 0x1b, 0x01, 0x00,
  6312. 0xd4, 0xf0, 0x3b, 0x7f, 0x35, 0x58, 0x94, 0xcf,
  6313. 0x33, 0x2f, 0x83, 0x0e, 0x71, 0x0b, 0x97, 0xce,
  6314. 0x98, 0xc8, 0xa8, 0x4a, 0xbd, 0x0b, 0x94, 0x81,
  6315. 0x14, 0xad, 0x17, 0x6e, 0x00, 0x8d, 0x33, 0xbd,
  6316. 0x60, 0xf9, 0x82, 0xb1, 0xff, 0x37, 0xc8, 0x55,
  6317. 0x97, 0x97, 0xa0, 0x6e, 0xf4, 0xf0, 0xef, 0x61,
  6318. 0xc1, 0x86, 0x32, 0x4e, 0x2b, 0x35, 0x06, 0x38,
  6319. 0x36, 0x06, 0x90, 0x7b, 0x6a, 0x7c, 0x02, 0xb0,
  6320. 0xf9, 0xf6, 0x15, 0x7b, 0x53, 0xc8, 0x67, 0xe4,
  6321. 0xb9, 0x16, 0x6c, 0x76, 0x7b, 0x80, 0x4d, 0x46,
  6322. 0xa5, 0x9b, 0x52, 0x16, 0xcd, 0xe7, 0xa4, 0xe9,
  6323. 0x90, 0x40, 0xc5, 0xa4, 0x04, 0x33, 0x22, 0x5e,
  6324. 0xe2, 0x82, 0xa1, 0xb0, 0xa0, 0x6c, 0x52, 0x3e,
  6325. 0xaf, 0x45, 0x34, 0xd7, 0xf8, 0x3f, 0xa1, 0x15,
  6326. 0x5b, 0x00, 0x47, 0x71, 0x8c, 0xbc, 0x54, 0x6a,
  6327. 0x0d, 0x07, 0x2b, 0x04, 0xb3, 0x56, 0x4e, 0xea,
  6328. 0x1b, 0x42, 0x22, 0x73, 0xf5, 0x48, 0x27, 0x1a,
  6329. 0x0b, 0xb2, 0x31, 0x60, 0x53, 0xfa, 0x76, 0x99,
  6330. 0x19, 0x55, 0xeb, 0xd6, 0x31, 0x59, 0x43, 0x4e,
  6331. 0xce, 0xbb, 0x4e, 0x46, 0x6d, 0xae, 0x5a, 0x10,
  6332. 0x73, 0xa6, 0x72, 0x76, 0x27, 0x09, 0x7a, 0x10,
  6333. 0x49, 0xe6, 0x17, 0xd9, 0x1d, 0x36, 0x10, 0x94,
  6334. 0xfa, 0x68, 0xf0, 0xff, 0x77, 0x98, 0x71, 0x30,
  6335. 0x30, 0x5b, 0xea, 0xba, 0x2e, 0xda, 0x04, 0xdf,
  6336. 0x99, 0x7b, 0x71, 0x4d, 0x6c, 0x6f, 0x2c, 0x29,
  6337. 0xa6, 0xad, 0x5c, 0xb4, 0x02, 0x2b, 0x02, 0x70,
  6338. 0x9b
  6339. };
  6340. WOLFSSL_SMALL_STACK_STATIC const byte authTag2[] = { /* expected output from operation */
  6341. 0xee, 0xad, 0x9d, 0x67, 0x89, 0x0c, 0xbb, 0x22,
  6342. 0x39, 0x23, 0x36, 0xfe, 0xa1, 0x85, 0x1f, 0x38
  6343. };
  6344. byte generatedCiphertext[265]; /* max plaintext2/cipher2 */
  6345. byte generatedPlaintext[265]; /* max plaintext2/cipher2 */
  6346. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  6347. wc_test_ret_t err;
  6348. ChaChaPoly_Aead aead;
  6349. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  6350. #define TEST_SMALL_CHACHA_CHUNKS 32
  6351. #else
  6352. #define TEST_SMALL_CHACHA_CHUNKS 64
  6353. #endif
  6354. #ifdef TEST_SMALL_CHACHA_CHUNKS
  6355. word32 testLen;
  6356. #endif
  6357. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  6358. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  6359. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  6360. /* Parameter Validation testing */
  6361. /* Encrypt */
  6362. err = wc_ChaCha20Poly1305_Encrypt(NULL, iv1, aad1, sizeof(aad1), plaintext1,
  6363. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  6364. if (err != BAD_FUNC_ARG)
  6365. return WC_TEST_RET_ENC_EC(err);
  6366. err = wc_ChaCha20Poly1305_Encrypt(key1, NULL, aad1, sizeof(aad1),
  6367. plaintext1, sizeof(plaintext1), generatedCiphertext,
  6368. generatedAuthTag);
  6369. if (err != BAD_FUNC_ARG)
  6370. return WC_TEST_RET_ENC_EC(err);
  6371. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL,
  6372. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  6373. if (err != BAD_FUNC_ARG)
  6374. return WC_TEST_RET_ENC_EC(err);
  6375. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  6376. sizeof(plaintext1), NULL, generatedAuthTag);
  6377. if (err != BAD_FUNC_ARG)
  6378. return WC_TEST_RET_ENC_EC(err);
  6379. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  6380. sizeof(plaintext1), generatedCiphertext, NULL);
  6381. if (err != BAD_FUNC_ARG)
  6382. return WC_TEST_RET_ENC_EC(err);
  6383. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL,
  6384. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  6385. if (err != BAD_FUNC_ARG)
  6386. return WC_TEST_RET_ENC_EC(err);
  6387. /* Decrypt */
  6388. err = wc_ChaCha20Poly1305_Decrypt(NULL, iv2, aad2, sizeof(aad2), cipher2,
  6389. sizeof(cipher2), authTag2, generatedPlaintext);
  6390. if (err != BAD_FUNC_ARG)
  6391. return WC_TEST_RET_ENC_EC(err);
  6392. err = wc_ChaCha20Poly1305_Decrypt(key2, NULL, aad2, sizeof(aad2), cipher2,
  6393. sizeof(cipher2), authTag2, generatedPlaintext);
  6394. if (err != BAD_FUNC_ARG)
  6395. return WC_TEST_RET_ENC_EC(err);
  6396. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL,
  6397. sizeof(cipher2), authTag2, generatedPlaintext);
  6398. if (err != BAD_FUNC_ARG)
  6399. return WC_TEST_RET_ENC_EC(err);
  6400. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  6401. sizeof(cipher2), NULL, generatedPlaintext);
  6402. if (err != BAD_FUNC_ARG)
  6403. return WC_TEST_RET_ENC_EC(err);
  6404. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  6405. sizeof(cipher2), authTag2, NULL);
  6406. if (err != BAD_FUNC_ARG)
  6407. return WC_TEST_RET_ENC_EC(err);
  6408. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL,
  6409. sizeof(cipher2), authTag2, generatedPlaintext);
  6410. if (err != BAD_FUNC_ARG)
  6411. return WC_TEST_RET_ENC_EC(err);
  6412. /* Test #1 */
  6413. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1,
  6414. aad1, sizeof(aad1),
  6415. plaintext1, sizeof(plaintext1),
  6416. generatedCiphertext, generatedAuthTag);
  6417. if (err) {
  6418. return WC_TEST_RET_ENC_EC(err);
  6419. }
  6420. /* -- Check the ciphertext and authtag */
  6421. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  6422. return WC_TEST_RET_ENC_NC;
  6423. }
  6424. if (XMEMCMP(generatedAuthTag, authTag1, sizeof(authTag1))) {
  6425. return WC_TEST_RET_ENC_NC;
  6426. }
  6427. /* -- Verify decryption works */
  6428. err = wc_ChaCha20Poly1305_Decrypt(key1, iv1,
  6429. aad1, sizeof(aad1),
  6430. cipher1, sizeof(cipher1),
  6431. authTag1, generatedPlaintext);
  6432. if (err) {
  6433. return err;
  6434. }
  6435. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  6436. return WC_TEST_RET_ENC_NC;
  6437. }
  6438. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  6439. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  6440. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  6441. /* Test #2 */
  6442. err = wc_ChaCha20Poly1305_Encrypt(key2, iv2,
  6443. aad2, sizeof(aad2),
  6444. plaintext2, sizeof(plaintext2),
  6445. generatedCiphertext, generatedAuthTag);
  6446. if (err) {
  6447. return err;
  6448. }
  6449. /* -- Check the ciphertext and authtag */
  6450. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  6451. return WC_TEST_RET_ENC_NC;
  6452. }
  6453. if (XMEMCMP(generatedAuthTag, authTag2, sizeof(authTag2))) {
  6454. return WC_TEST_RET_ENC_NC;
  6455. }
  6456. /* -- Verify decryption works */
  6457. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2,
  6458. aad2, sizeof(aad2),
  6459. cipher2, sizeof(cipher2),
  6460. authTag2, generatedPlaintext);
  6461. if (err) {
  6462. return err;
  6463. }
  6464. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  6465. return WC_TEST_RET_ENC_NC;
  6466. }
  6467. /* AEAD init/update/final - bad argument tests */
  6468. err = wc_ChaCha20Poly1305_Init(NULL, key1, iv1,
  6469. CHACHA20_POLY1305_AEAD_DECRYPT);
  6470. if (err != BAD_FUNC_ARG)
  6471. return WC_TEST_RET_ENC_EC(err);
  6472. err = wc_ChaCha20Poly1305_Init(&aead, NULL, iv1,
  6473. CHACHA20_POLY1305_AEAD_DECRYPT);
  6474. if (err != BAD_FUNC_ARG)
  6475. return WC_TEST_RET_ENC_EC(err);
  6476. err = wc_ChaCha20Poly1305_Init(&aead, key1, NULL,
  6477. CHACHA20_POLY1305_AEAD_DECRYPT);
  6478. if (err != BAD_FUNC_ARG)
  6479. return WC_TEST_RET_ENC_EC(err);
  6480. err = wc_ChaCha20Poly1305_UpdateAad(NULL, aad1, sizeof(aad1));
  6481. if (err != BAD_FUNC_ARG)
  6482. return WC_TEST_RET_ENC_EC(err);
  6483. err = wc_ChaCha20Poly1305_UpdateAad(&aead, NULL, sizeof(aad1));
  6484. if (err != BAD_FUNC_ARG)
  6485. return WC_TEST_RET_ENC_EC(err);
  6486. err = wc_ChaCha20Poly1305_UpdateData(NULL, generatedPlaintext,
  6487. generatedPlaintext, sizeof(plaintext1));
  6488. if (err != BAD_FUNC_ARG)
  6489. return WC_TEST_RET_ENC_EC(err);
  6490. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext, NULL,
  6491. sizeof(plaintext1));
  6492. if (err != BAD_FUNC_ARG)
  6493. return WC_TEST_RET_ENC_EC(err);
  6494. err = wc_ChaCha20Poly1305_UpdateData(&aead, NULL, generatedPlaintext,
  6495. sizeof(plaintext1));
  6496. if (err != BAD_FUNC_ARG)
  6497. return WC_TEST_RET_ENC_EC(err);
  6498. err = wc_ChaCha20Poly1305_Final(NULL, generatedAuthTag);
  6499. if (err != BAD_FUNC_ARG)
  6500. return WC_TEST_RET_ENC_EC(err);
  6501. err = wc_ChaCha20Poly1305_Final(&aead, NULL);
  6502. if (err != BAD_FUNC_ARG)
  6503. return WC_TEST_RET_ENC_EC(err);
  6504. /* AEAD init/update/final - bad state tests */
  6505. /* clear struct - make valgrind happy to resolve
  6506. "Conditional jump or move depends on uninitialised value(s)".
  6507. The enum is "int" size and aead.state is "byte" */
  6508. /* The wc_ChaCha20Poly1305_Init function does this normally */
  6509. XMEMSET(&aead, 0, sizeof(aead));
  6510. aead.state = CHACHA20_POLY1305_STATE_INIT;
  6511. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  6512. if (err != BAD_STATE_E)
  6513. return WC_TEST_RET_ENC_EC(err);
  6514. aead.state = CHACHA20_POLY1305_STATE_DATA;
  6515. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  6516. if (err != BAD_STATE_E)
  6517. return WC_TEST_RET_ENC_EC(err);
  6518. aead.state = CHACHA20_POLY1305_STATE_INIT;
  6519. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext,
  6520. generatedPlaintext, sizeof(plaintext1));
  6521. if (err != BAD_STATE_E)
  6522. return WC_TEST_RET_ENC_EC(err);
  6523. aead.state = CHACHA20_POLY1305_STATE_INIT;
  6524. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  6525. if (err != BAD_STATE_E)
  6526. return WC_TEST_RET_ENC_EC(err);
  6527. aead.state = CHACHA20_POLY1305_STATE_READY;
  6528. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  6529. if (err != BAD_STATE_E)
  6530. return WC_TEST_RET_ENC_EC(err);
  6531. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  6532. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  6533. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  6534. /* Test 1 - Encrypt */
  6535. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  6536. CHACHA20_POLY1305_AEAD_ENCRYPT);
  6537. if (err != 0)
  6538. return WC_TEST_RET_ENC_EC(err);
  6539. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  6540. if (err != 0)
  6541. return WC_TEST_RET_ENC_EC(err);
  6542. #ifdef TEST_SMALL_CHACHA_CHUNKS
  6543. /* test doing data in smaller chunks */
  6544. for (testLen=0; testLen<sizeof(plaintext1); ) {
  6545. word32 dataLen = sizeof(plaintext1) - testLen;
  6546. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  6547. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  6548. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext1[testLen],
  6549. &generatedCiphertext[testLen], dataLen);
  6550. if (err != 0)
  6551. return WC_TEST_RET_ENC_EC(err);
  6552. testLen += dataLen;
  6553. }
  6554. #else
  6555. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext1,
  6556. generatedCiphertext, sizeof(plaintext1));
  6557. #endif
  6558. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  6559. if (err != 0)
  6560. return WC_TEST_RET_ENC_EC(err);
  6561. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  6562. if (err != 0)
  6563. return WC_TEST_RET_ENC_EC(err);
  6564. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  6565. return WC_TEST_RET_ENC_NC;
  6566. }
  6567. /* Test 1 - Decrypt */
  6568. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  6569. CHACHA20_POLY1305_AEAD_DECRYPT);
  6570. if (err != 0)
  6571. return WC_TEST_RET_ENC_EC(err);
  6572. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  6573. if (err != 0)
  6574. return WC_TEST_RET_ENC_EC(err);
  6575. #ifdef TEST_SMALL_CHACHA_CHUNKS
  6576. /* test doing data in smaller chunks */
  6577. for (testLen=0; testLen<sizeof(plaintext1); ) {
  6578. word32 dataLen = sizeof(plaintext1) - testLen;
  6579. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  6580. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  6581. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  6582. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  6583. dataLen);
  6584. if (err != 0)
  6585. return WC_TEST_RET_ENC_EC(err);
  6586. testLen += dataLen;
  6587. }
  6588. #else
  6589. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  6590. generatedPlaintext, sizeof(cipher1));
  6591. #endif
  6592. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  6593. if (err != 0)
  6594. return WC_TEST_RET_ENC_EC(err);
  6595. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  6596. if (err != 0)
  6597. return WC_TEST_RET_ENC_EC(err);
  6598. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  6599. return WC_TEST_RET_ENC_NC;
  6600. }
  6601. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  6602. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  6603. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  6604. /* Test 2 - Encrypt */
  6605. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  6606. CHACHA20_POLY1305_AEAD_ENCRYPT);
  6607. if (err != 0)
  6608. return WC_TEST_RET_ENC_EC(err);
  6609. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  6610. if (err != 0)
  6611. return WC_TEST_RET_ENC_EC(err);
  6612. #ifdef TEST_SMALL_CHACHA_CHUNKS
  6613. /* test doing data in smaller chunks */
  6614. for (testLen=0; testLen<sizeof(plaintext2); ) {
  6615. word32 dataLen = sizeof(plaintext2) - testLen;
  6616. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  6617. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  6618. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext2[testLen],
  6619. &generatedCiphertext[testLen], dataLen);
  6620. if (err != 0)
  6621. return WC_TEST_RET_ENC_EC(err);
  6622. testLen += dataLen;
  6623. }
  6624. #else
  6625. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext2, generatedCiphertext,
  6626. sizeof(plaintext2));
  6627. #endif
  6628. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  6629. if (err != 0)
  6630. return WC_TEST_RET_ENC_EC(err);
  6631. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  6632. if (err != 0)
  6633. return WC_TEST_RET_ENC_EC(err);
  6634. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  6635. return WC_TEST_RET_ENC_NC;
  6636. }
  6637. /* Test 2 - Decrypt */
  6638. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  6639. CHACHA20_POLY1305_AEAD_DECRYPT);
  6640. if (err != 0)
  6641. return WC_TEST_RET_ENC_EC(err);
  6642. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  6643. if (err != 0)
  6644. return WC_TEST_RET_ENC_EC(err);
  6645. #ifdef TEST_SMALL_CHACHA_CHUNKS
  6646. /* test doing data in smaller chunks */
  6647. for (testLen=0; testLen<sizeof(plaintext2); ) {
  6648. word32 dataLen = sizeof(plaintext2) - testLen;
  6649. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  6650. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  6651. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  6652. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  6653. dataLen);
  6654. if (err != 0)
  6655. return WC_TEST_RET_ENC_EC(err);
  6656. testLen += dataLen;
  6657. }
  6658. #else
  6659. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  6660. generatedPlaintext, sizeof(cipher2));
  6661. #endif
  6662. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  6663. if (err != 0)
  6664. return WC_TEST_RET_ENC_EC(err);
  6665. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  6666. if (err != 0)
  6667. return WC_TEST_RET_ENC_EC(err);
  6668. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  6669. return WC_TEST_RET_ENC_NC;
  6670. }
  6671. return err;
  6672. }
  6673. #endif /* HAVE_CHACHA && HAVE_POLY1305 */
  6674. #ifndef NO_DES3
  6675. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t des_test(void)
  6676. {
  6677. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  6678. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  6679. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  6680. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  6681. };
  6682. byte plain[24];
  6683. byte cipher[24];
  6684. Des enc;
  6685. Des dec;
  6686. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  6687. {
  6688. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  6689. };
  6690. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  6691. {
  6692. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  6693. };
  6694. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  6695. {
  6696. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  6697. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  6698. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  6699. };
  6700. wc_test_ret_t ret;
  6701. ret = wc_Des_SetKey(&enc, key, iv, DES_ENCRYPTION);
  6702. if (ret != 0)
  6703. return WC_TEST_RET_ENC_EC(ret);
  6704. ret = wc_Des_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  6705. if (ret != 0)
  6706. return WC_TEST_RET_ENC_EC(ret);
  6707. ret = wc_Des_SetKey(&dec, key, iv, DES_DECRYPTION);
  6708. if (ret != 0)
  6709. return WC_TEST_RET_ENC_EC(ret);
  6710. ret = wc_Des_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  6711. if (ret != 0)
  6712. return WC_TEST_RET_ENC_EC(ret);
  6713. if (XMEMCMP(plain, vector, sizeof(plain)))
  6714. return WC_TEST_RET_ENC_NC;
  6715. if (XMEMCMP(cipher, verify, sizeof(cipher)))
  6716. return WC_TEST_RET_ENC_NC;
  6717. ret = wc_Des_CbcEncryptWithKey(cipher, vector, sizeof(vector), key, iv);
  6718. if (ret != 0)
  6719. return WC_TEST_RET_ENC_EC(ret);
  6720. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  6721. {
  6722. EncryptedInfo info;
  6723. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  6724. XMEMCPY(info.iv, iv, sizeof(iv));
  6725. info.ivSz = sizeof(iv);
  6726. info.keySz = sizeof(key);
  6727. info.cipherType = WC_CIPHER_DES;
  6728. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  6729. sizeof(key), WC_HASH_TYPE_SHA);
  6730. if (ret != 0)
  6731. return WC_TEST_RET_ENC_EC(ret);
  6732. /* Test invalid info ptr */
  6733. ret = wc_BufferKeyEncrypt(NULL, cipher, sizeof(cipher), key,
  6734. sizeof(key), WC_HASH_TYPE_SHA);
  6735. if (ret != BAD_FUNC_ARG)
  6736. return WC_TEST_RET_ENC_EC(ret);
  6737. #ifndef NO_PWDBASED
  6738. /* Test invalid hash type - only applies to wc_PBKDF1 call */
  6739. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  6740. sizeof(key), WC_HASH_TYPE_NONE);
  6741. if (ret == 0)
  6742. return WC_TEST_RET_ENC_EC(ret);
  6743. #endif /* !NO_PWDBASED */
  6744. }
  6745. #endif
  6746. return 0;
  6747. }
  6748. #endif /* !NO_DES3 */
  6749. #ifndef NO_DES3
  6750. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t des3_test(void)
  6751. {
  6752. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  6753. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  6754. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  6755. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  6756. };
  6757. byte plain[24];
  6758. byte cipher[24];
  6759. Des3 enc;
  6760. Des3 dec;
  6761. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6762. {
  6763. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  6764. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  6765. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  6766. };
  6767. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  6768. {
  6769. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  6770. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  6771. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  6772. };
  6773. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] =
  6774. {
  6775. 0x43,0xa0,0x29,0x7e,0xd1,0x84,0xf8,0x0e,
  6776. 0x89,0x64,0x84,0x32,0x12,0xd5,0x08,0x98,
  6777. 0x18,0x94,0x15,0x74,0x87,0x12,0x7d,0xb0
  6778. };
  6779. wc_test_ret_t ret;
  6780. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  6781. size_t i;
  6782. #endif
  6783. ret = wc_Des3Init(&enc, HEAP_HINT, devId);
  6784. if (ret != 0)
  6785. return WC_TEST_RET_ENC_EC(ret);
  6786. ret = wc_Des3Init(&dec, HEAP_HINT, devId);
  6787. if (ret != 0)
  6788. return WC_TEST_RET_ENC_EC(ret);
  6789. ret = wc_Des3_SetKey(&enc, key3, iv3, DES_ENCRYPTION);
  6790. if (ret != 0)
  6791. return WC_TEST_RET_ENC_EC(ret);
  6792. ret = wc_Des3_SetKey(&dec, key3, iv3, DES_DECRYPTION);
  6793. if (ret != 0)
  6794. return WC_TEST_RET_ENC_EC(ret);
  6795. ret = wc_Des3_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  6796. #if defined(WOLFSSL_ASYNC_CRYPT)
  6797. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  6798. #endif
  6799. if (ret != 0)
  6800. return WC_TEST_RET_ENC_EC(ret);
  6801. ret = wc_Des3_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  6802. #if defined(WOLFSSL_ASYNC_CRYPT)
  6803. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  6804. #endif
  6805. if (ret != 0)
  6806. return WC_TEST_RET_ENC_EC(ret);
  6807. if (XMEMCMP(plain, vector, sizeof(plain)))
  6808. return WC_TEST_RET_ENC_NC;
  6809. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  6810. return WC_TEST_RET_ENC_NC;
  6811. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  6812. /* test the same vectors with using compatibility layer */
  6813. for (i = 0; i < sizeof(vector); i += DES_BLOCK_SIZE){
  6814. DES_key_schedule ks1;
  6815. DES_key_schedule ks2;
  6816. DES_key_schedule ks3;
  6817. DES_cblock iv4;
  6818. byte tmp[sizeof(vector)];
  6819. XMEMCPY(ks1, key3, sizeof(DES_key_schedule));
  6820. XMEMCPY(ks2, key3 + 8, sizeof(DES_key_schedule));
  6821. XMEMCPY(ks3, key3 + 16, sizeof(DES_key_schedule));
  6822. XMEMSET(plain, 0, sizeof(plain));
  6823. XMEMSET(cipher, 0, sizeof(cipher));
  6824. /* Test in-place encrypt/decrypt */
  6825. XMEMCPY(tmp, vector, sizeof(vector));
  6826. /* Use i as the splitter */
  6827. XMEMCPY(iv4, iv3, sizeof(DES_cblock));
  6828. DES_ede3_cbc_encrypt(tmp, tmp, (long)i, &ks1, &ks2, &ks3,
  6829. &iv4, DES_ENCRYPT);
  6830. DES_ede3_cbc_encrypt(tmp + i, tmp + i, (long)(sizeof(vector) - i),
  6831. &ks1, &ks2, &ks3, &iv4, DES_ENCRYPT);
  6832. XMEMCPY(cipher, tmp, sizeof(cipher));
  6833. XMEMCPY(iv4, iv3, sizeof(DES_cblock));
  6834. DES_ede3_cbc_encrypt(tmp, tmp, (long)i, &ks1, &ks2, &ks3,
  6835. &iv4, DES_DECRYPT);
  6836. DES_ede3_cbc_encrypt(tmp + i, tmp + i, (long)(sizeof(cipher) - i),
  6837. &ks1, &ks2, &ks3, &iv4, DES_DECRYPT);
  6838. XMEMCPY(plain, tmp, sizeof(plain));
  6839. if (XMEMCMP(plain, vector, sizeof(plain)))
  6840. return WC_TEST_RET_ENC_NC;
  6841. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  6842. return WC_TEST_RET_ENC_NC;
  6843. }
  6844. #endif /* OPENSSL_EXTRA */
  6845. wc_Des3Free(&enc);
  6846. wc_Des3Free(&dec);
  6847. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  6848. {
  6849. EncryptedInfo info;
  6850. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  6851. XMEMCPY(info.iv, iv3, sizeof(iv3));
  6852. info.ivSz = sizeof(iv3);
  6853. info.keySz = sizeof(key3);
  6854. info.cipherType = WC_CIPHER_DES3;
  6855. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key3,
  6856. sizeof(key3), WC_HASH_TYPE_SHA);
  6857. if (ret != 0)
  6858. return WC_TEST_RET_ENC_EC(ret);
  6859. }
  6860. #endif
  6861. return 0;
  6862. }
  6863. #endif /* NO_DES3 */
  6864. static const int fiducial1 = WC_TEST_RET_LN; /* source code reference point --
  6865. * see print_fiducials() below.
  6866. */
  6867. #ifndef NO_AES
  6868. #if defined(WOLFSSL_AES_OFB) || defined(WOLFSSL_AES_CFB) || \
  6869. defined(WOLFSSL_AES_XTS)
  6870. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6871. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6872. /* pass in the function, key, iv, plain text and expected and this function
  6873. * tests that the encryption and decryption is successful */
  6874. static wc_test_ret_t EVP_test(const WOLFSSL_EVP_CIPHER* type, const byte* key,
  6875. const byte* iv, const byte* plain, int plainSz,
  6876. const byte* expected, int expectedSz)
  6877. {
  6878. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6879. EVP_CIPHER_CTX *ctx = NULL;
  6880. #else
  6881. EVP_CIPHER_CTX ctx[1];
  6882. #endif
  6883. int idx, cipherSz;
  6884. wc_test_ret_t ret = 0;
  6885. byte* cipher;
  6886. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6887. if ((ctx = wolfSSL_EVP_CIPHER_CTX_new()) == NULL)
  6888. return MEMORY_E;
  6889. #endif
  6890. cipher = (byte*)XMALLOC(plainSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6891. if (cipher == NULL) {
  6892. ret = WC_TEST_RET_ENC_ERRNO;
  6893. goto EVP_TEST_END;
  6894. }
  6895. /* test encrypt */
  6896. EVP_CIPHER_CTX_init(ctx);
  6897. if (EVP_CipherInit(ctx, type, key, iv, 1) == 0) {
  6898. ret = WC_TEST_RET_ENC_NC;
  6899. goto EVP_TEST_END;
  6900. }
  6901. if (EVP_CipherUpdate(ctx, cipher, &idx, plain, expectedSz) == 0) {
  6902. ret = WC_TEST_RET_ENC_NC;
  6903. goto EVP_TEST_END;
  6904. }
  6905. cipherSz = idx;
  6906. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  6907. ret = WC_TEST_RET_ENC_NC;
  6908. goto EVP_TEST_END;
  6909. }
  6910. cipherSz += idx;
  6911. if (XMEMCMP(cipher, expected, plainSz)) {
  6912. ret = WC_TEST_RET_ENC_NC;
  6913. goto EVP_TEST_END;
  6914. }
  6915. /* test decrypt */
  6916. EVP_CIPHER_CTX_init(ctx);
  6917. if (EVP_CipherInit(ctx, type, key, iv, 0) == 0) {
  6918. ret = WC_TEST_RET_ENC_NC;
  6919. goto EVP_TEST_END;
  6920. }
  6921. if (EVP_CipherUpdate(ctx, cipher, &idx, cipher, expectedSz) == 0) {
  6922. ret = WC_TEST_RET_ENC_NC;
  6923. goto EVP_TEST_END;
  6924. }
  6925. cipherSz = idx;
  6926. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  6927. ret = WC_TEST_RET_ENC_NC;
  6928. goto EVP_TEST_END;
  6929. }
  6930. cipherSz += idx;
  6931. if ((expectedSz != cipherSz) || XMEMCMP(plain, cipher, plainSz)) {
  6932. ret = WC_TEST_RET_ENC_NC;
  6933. goto EVP_TEST_END;
  6934. }
  6935. EVP_TEST_END:
  6936. if (cipher)
  6937. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6938. (void)cipherSz;
  6939. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6940. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  6941. #endif
  6942. return ret;
  6943. }
  6944. #endif /* OPENSSL_EXTRA */
  6945. #endif /* WOLFSSL_AES_OFB || WOLFSSL_AES_CFB */
  6946. #ifdef WOLFSSL_AES_OFB
  6947. /* test vector from https://csrc.nist.gov/Projects/cryptographic-algorithm-validation-program/Block-Ciphers */
  6948. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesofb_test(void)
  6949. {
  6950. #ifdef WOLFSSL_AES_256
  6951. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6952. {
  6953. 0xc4,0xc7,0xfa,0xd6,0x53,0x5c,0xb8,0x71,
  6954. 0x4a,0x5c,0x40,0x77,0x9a,0x8b,0xa1,0xd2,
  6955. 0x53,0x3e,0x23,0xb4,0xb2,0x58,0x73,0x2a,
  6956. 0x5b,0x78,0x01,0xf4,0xe3,0x71,0xa7,0x94
  6957. };
  6958. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  6959. {
  6960. 0x5e,0xb9,0x33,0x13,0xb8,0x71,0xff,0x16,
  6961. 0xb9,0x8a,0x9b,0xcb,0x43,0x33,0x0d,0x6f
  6962. };
  6963. WOLFSSL_SMALL_STACK_STATIC const byte plain1[] =
  6964. {
  6965. 0x6d,0x0b,0xb0,0x79,0x63,0x84,0x71,0xe9,
  6966. 0x39,0xd4,0x53,0x14,0x86,0xc1,0x4c,0x25,
  6967. 0x9a,0xee,0xc6,0xf3,0xc0,0x0d,0xfd,0xd6,
  6968. 0xc0,0x50,0xa8,0xba,0xa8,0x20,0xdb,0x71,
  6969. 0xcc,0x12,0x2c,0x4e,0x0c,0x17,0x15,0xef,
  6970. 0x55,0xf3,0x99,0x5a,0x6b,0xf0,0x2a,0x4c
  6971. };
  6972. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  6973. {
  6974. 0x0f,0x54,0x61,0x71,0x59,0xd0,0x3f,0xfc,
  6975. 0x1b,0xfa,0xfb,0x60,0x29,0x30,0xd7,0x00,
  6976. 0xf4,0xa4,0xa8,0xe6,0xdd,0x93,0x94,0x46,
  6977. 0x64,0xd2,0x19,0xc4,0xc5,0x4d,0xde,0x1b,
  6978. 0x04,0x53,0xe1,0x73,0xf5,0x18,0x74,0xae,
  6979. 0xfd,0x64,0xa2,0xe1,0xe2,0x76,0x13,0xb0
  6980. };
  6981. #endif /* WOLFSSL_AES_256 */
  6982. #ifdef WOLFSSL_AES_128
  6983. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6984. {
  6985. 0x10,0xa5,0x88,0x69,0xd7,0x4b,0xe5,0xa3,
  6986. 0x74,0xcf,0x86,0x7c,0xfb,0x47,0x38,0x59
  6987. };
  6988. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  6989. {
  6990. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6991. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6992. };
  6993. WOLFSSL_SMALL_STACK_STATIC const byte plain2[] =
  6994. {
  6995. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6996. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6997. };
  6998. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  6999. {
  7000. 0x6d,0x25,0x1e,0x69,0x44,0xb0,0x51,0xe0,
  7001. 0x4e,0xaa,0x6f,0xb4,0xdb,0xf7,0x84,0x65
  7002. };
  7003. #endif /* WOLFSSL_AES_128 */
  7004. #ifdef WOLFSSL_AES_192
  7005. WOLFSSL_SMALL_STACK_STATIC const byte key3[] = {
  7006. 0xd0,0x77,0xa0,0x3b,0xd8,0xa3,0x89,0x73,
  7007. 0x92,0x8c,0xca,0xfe,0x4a,0x9d,0x2f,0x45,
  7008. 0x51,0x30,0xbd,0x0a,0xf5,0xae,0x46,0xa9
  7009. };
  7010. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  7011. {
  7012. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7013. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  7014. };
  7015. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  7016. {
  7017. 0xab,0xc7,0x86,0xfb,0x1e,0xdb,0x50,0x45,
  7018. 0x80,0xc4,0xd8,0x82,0xef,0x29,0xa0,0xc7
  7019. };
  7020. WOLFSSL_SMALL_STACK_STATIC const byte plain3[] =
  7021. {
  7022. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  7023. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  7024. };
  7025. #endif /* WOLFSSL_AES_192 */
  7026. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7027. Aes *enc = NULL;
  7028. #else
  7029. Aes enc[1];
  7030. #endif
  7031. byte cipher[AES_BLOCK_SIZE * 4];
  7032. #ifdef HAVE_AES_DECRYPT
  7033. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7034. Aes *dec = NULL;
  7035. #else
  7036. Aes dec[1];
  7037. #endif
  7038. byte plain [AES_BLOCK_SIZE * 4];
  7039. #endif
  7040. wc_test_ret_t ret = 0;
  7041. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7042. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7043. ERROR_OUT(-1, out);
  7044. #ifdef HAVE_AES_DECRYPT
  7045. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7046. ERROR_OUT(-1, out);
  7047. #endif
  7048. #endif
  7049. XMEMSET(enc, 0, sizeof *enc);
  7050. #ifdef HAVE_AES_DECRYPT
  7051. XMEMSET(dec, 0, sizeof *dec);
  7052. #endif
  7053. #ifdef WOLFSSL_AES_128
  7054. /* 128 key size test */
  7055. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7056. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7057. ret = EVP_test(EVP_aes_128_ofb(), key2, iv2, plain2, sizeof(plain2),
  7058. cipher2, sizeof(cipher2));
  7059. if (ret != 0) {
  7060. goto out;
  7061. }
  7062. #endif
  7063. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  7064. if (ret != 0)
  7065. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7066. #ifdef HAVE_AES_DECRYPT
  7067. /* decrypt uses AES_ENCRYPTION */
  7068. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  7069. if (ret != 0)
  7070. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7071. #endif
  7072. XMEMSET(cipher, 0, sizeof(cipher));
  7073. ret = wc_AesOfbEncrypt(enc, cipher, plain2, AES_BLOCK_SIZE);
  7074. if (ret != 0)
  7075. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7076. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE))
  7077. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7078. #ifdef HAVE_AES_DECRYPT
  7079. ret = wc_AesOfbDecrypt(dec, plain, cipher2, AES_BLOCK_SIZE);
  7080. if (ret != 0)
  7081. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7082. if (XMEMCMP(plain, plain2, AES_BLOCK_SIZE))
  7083. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7084. #endif /* HAVE_AES_DECRYPT */
  7085. #endif /* WOLFSSL_AES_128 */
  7086. #ifdef WOLFSSL_AES_192
  7087. /* 192 key size test */
  7088. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7089. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7090. ret = EVP_test(EVP_aes_192_ofb(), key3, iv3, plain3, sizeof(plain3),
  7091. cipher3, sizeof(cipher3));
  7092. if (ret != 0) {
  7093. goto out;
  7094. }
  7095. #endif
  7096. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  7097. if (ret != 0)
  7098. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7099. #ifdef HAVE_AES_DECRYPT
  7100. /* decrypt uses AES_ENCRYPTION */
  7101. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  7102. if (ret != 0)
  7103. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7104. #endif
  7105. XMEMSET(cipher, 0, sizeof(cipher));
  7106. ret = wc_AesOfbEncrypt(enc, cipher, plain3, AES_BLOCK_SIZE);
  7107. if (ret != 0)
  7108. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7109. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE))
  7110. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7111. #ifdef HAVE_AES_DECRYPT
  7112. ret = wc_AesOfbDecrypt(dec, plain, cipher3, AES_BLOCK_SIZE);
  7113. if (ret != 0)
  7114. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7115. if (XMEMCMP(plain, plain3, AES_BLOCK_SIZE))
  7116. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7117. #endif /* HAVE_AES_DECRYPT */
  7118. #endif /* WOLFSSL_AES_192 */
  7119. #ifdef WOLFSSL_AES_256
  7120. /* 256 key size test */
  7121. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7122. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7123. ret = EVP_test(EVP_aes_256_ofb(), key1, iv1, plain1, sizeof(plain1),
  7124. cipher1, sizeof(cipher1));
  7125. if (ret != 0) {
  7126. goto out;
  7127. }
  7128. #endif
  7129. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  7130. if (ret != 0)
  7131. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7132. #ifdef HAVE_AES_DECRYPT
  7133. /* decrypt uses AES_ENCRYPTION */
  7134. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  7135. if (ret != 0)
  7136. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7137. #endif
  7138. XMEMSET(cipher, 0, sizeof(cipher));
  7139. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE);
  7140. if (ret != 0)
  7141. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7142. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE))
  7143. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7144. ret = wc_AesOfbEncrypt(enc, cipher + AES_BLOCK_SIZE,
  7145. plain1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  7146. if (ret != 0)
  7147. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7148. if (XMEMCMP(cipher + AES_BLOCK_SIZE, cipher1 + AES_BLOCK_SIZE,
  7149. AES_BLOCK_SIZE))
  7150. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7151. #ifdef HAVE_AES_DECRYPT
  7152. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE);
  7153. if (ret != 0)
  7154. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7155. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE))
  7156. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7157. ret = wc_AesOfbDecrypt(dec, plain + AES_BLOCK_SIZE,
  7158. cipher1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  7159. if (ret != 0)
  7160. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7161. if (XMEMCMP(plain + AES_BLOCK_SIZE, plain1 + AES_BLOCK_SIZE,
  7162. AES_BLOCK_SIZE))
  7163. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7164. #endif /* HAVE_AES_DECRYPT */
  7165. /* multiple blocks at once */
  7166. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  7167. if (ret != 0)
  7168. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7169. #ifdef HAVE_AES_DECRYPT
  7170. /* decrypt uses AES_ENCRYPTION */
  7171. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  7172. if (ret != 0)
  7173. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7174. #endif
  7175. XMEMSET(cipher, 0, sizeof(cipher));
  7176. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE * 3);
  7177. if (ret != 0)
  7178. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7179. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 3))
  7180. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7181. #ifdef HAVE_AES_DECRYPT
  7182. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE * 3);
  7183. if (ret != 0)
  7184. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7185. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE * 3))
  7186. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7187. #endif /* HAVE_AES_DECRYPT */
  7188. /* inline decrypt/encrypt*/
  7189. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  7190. if (ret != 0)
  7191. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7192. #ifdef HAVE_AES_DECRYPT
  7193. /* decrypt uses AES_ENCRYPTION */
  7194. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  7195. if (ret != 0)
  7196. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7197. #endif
  7198. XMEMCPY(cipher, plain1, AES_BLOCK_SIZE * 2);
  7199. ret = wc_AesOfbEncrypt(enc, cipher, cipher, AES_BLOCK_SIZE * 2);
  7200. if (ret != 0)
  7201. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7202. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  7203. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7204. #ifdef HAVE_AES_DECRYPT
  7205. ret = wc_AesOfbDecrypt(dec, cipher, cipher, AES_BLOCK_SIZE * 2);
  7206. if (ret != 0)
  7207. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7208. if (XMEMCMP(cipher, plain1, AES_BLOCK_SIZE * 2))
  7209. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7210. #endif /* HAVE_AES_DECRYPT */
  7211. /* 256 key size test leftover support */
  7212. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  7213. if (ret != 0)
  7214. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7215. #ifdef HAVE_AES_DECRYPT
  7216. /* decrypt uses AES_ENCRYPTION */
  7217. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  7218. if (ret != 0)
  7219. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7220. #endif
  7221. XMEMSET(cipher, 0, sizeof(cipher));
  7222. ret = wc_AesOfbEncrypt(enc, cipher, plain1, 3);
  7223. if (ret != 0)
  7224. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7225. if (XMEMCMP(cipher, cipher1, 3))
  7226. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7227. ret = wc_AesOfbEncrypt(enc, cipher + 3, plain1 + 3, AES_BLOCK_SIZE);
  7228. if (ret != 0)
  7229. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7230. if (XMEMCMP(cipher + 3, cipher1 + 3, AES_BLOCK_SIZE))
  7231. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7232. #ifdef HAVE_AES_DECRYPT
  7233. ret = wc_AesOfbDecrypt(dec, plain, cipher1, 6);
  7234. if (ret != 0)
  7235. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7236. if (XMEMCMP(plain, plain1, 6))
  7237. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7238. ret = wc_AesOfbDecrypt(dec, plain + 6, cipher1 + 6, AES_BLOCK_SIZE);
  7239. if (ret != 0)
  7240. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7241. if (XMEMCMP(plain + 6, plain1 + 6, AES_BLOCK_SIZE))
  7242. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7243. #endif /* HAVE_AES_DECRYPT */
  7244. out:
  7245. wc_AesFree(enc);
  7246. wc_AesFree(dec);
  7247. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7248. if (enc)
  7249. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7250. #ifdef HAVE_AES_DECRYPT
  7251. if (dec)
  7252. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7253. #endif
  7254. #endif
  7255. #endif /* WOLFSSL_AES_256 */
  7256. return ret;
  7257. }
  7258. #endif /* WOLFSSL_AES_OFB */
  7259. #if defined(WOLFSSL_AES_CFB)
  7260. /* Test cases from NIST SP 800-38A, Recommendation for Block Cipher Modes of Operation Methods an*/
  7261. static wc_test_ret_t aescfb_test(void)
  7262. {
  7263. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7264. Aes *enc = NULL;
  7265. #else
  7266. Aes enc[1];
  7267. #endif
  7268. int enc_inited = 0;
  7269. byte cipher[AES_BLOCK_SIZE * 4];
  7270. #ifdef HAVE_AES_DECRYPT
  7271. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7272. Aes *dec = NULL;
  7273. #else
  7274. Aes dec[1];
  7275. #endif
  7276. int dec_inited = 0;
  7277. byte plain [AES_BLOCK_SIZE * 4];
  7278. #endif
  7279. wc_test_ret_t ret = 0;
  7280. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  7281. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  7282. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  7283. };
  7284. #ifdef WOLFSSL_AES_128
  7285. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  7286. {
  7287. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  7288. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  7289. };
  7290. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  7291. {
  7292. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  7293. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  7294. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  7295. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b,
  7296. 0x26,0x75,0x1f,0x67,0xa3,0xcb,0xb1,0x40,
  7297. 0xb1,0x80,0x8c,0xf1,0x87,0xa4,0xf4,0xdf
  7298. };
  7299. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  7300. {
  7301. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  7302. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  7303. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  7304. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  7305. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  7306. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef
  7307. };
  7308. #endif /* WOLFSSL_AES_128 */
  7309. #ifdef WOLFSSL_AES_192
  7310. /* 192 size key test */
  7311. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  7312. {
  7313. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  7314. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  7315. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  7316. };
  7317. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  7318. {
  7319. 0xcd,0xc8,0x0d,0x6f,0xdd,0xf1,0x8c,0xab,
  7320. 0x34,0xc2,0x59,0x09,0xc9,0x9a,0x41,0x74,
  7321. 0x67,0xce,0x7f,0x7f,0x81,0x17,0x36,0x21,
  7322. 0x96,0x1a,0x2b,0x70,0x17,0x1d,0x3d,0x7a,
  7323. 0x2e,0x1e,0x8a,0x1d,0xd5,0x9b,0x88,0xb1,
  7324. 0xc8,0xe6,0x0f,0xed,0x1e,0xfa,0xc4,0xc9,
  7325. 0xc0,0x5f,0x9f,0x9c,0xa9,0x83,0x4f,0xa0,
  7326. 0x42,0xae,0x8f,0xba,0x58,0x4b,0x09,0xff
  7327. };
  7328. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  7329. {
  7330. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  7331. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  7332. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  7333. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  7334. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  7335. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  7336. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  7337. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  7338. };
  7339. #endif /* WOLFSSL_AES_192 */
  7340. #ifdef WOLFSSL_AES_256
  7341. /* 256 size key simple test */
  7342. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  7343. {
  7344. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  7345. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  7346. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  7347. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  7348. };
  7349. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  7350. {
  7351. 0xdc,0x7e,0x84,0xbf,0xda,0x79,0x16,0x4b,
  7352. 0x7e,0xcd,0x84,0x86,0x98,0x5d,0x38,0x60,
  7353. 0x39,0xff,0xed,0x14,0x3b,0x28,0xb1,0xc8,
  7354. 0x32,0x11,0x3c,0x63,0x31,0xe5,0x40,0x7b,
  7355. 0xdf,0x10,0x13,0x24,0x15,0xe5,0x4b,0x92,
  7356. 0xa1,0x3e,0xd0,0xa8,0x26,0x7a,0xe2,0xf9,
  7357. 0x75,0xa3,0x85,0x74,0x1a,0xb9,0xce,0xf8,
  7358. 0x20,0x31,0x62,0x3d,0x55,0xb1,0xe4,0x71
  7359. };
  7360. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  7361. {
  7362. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  7363. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  7364. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  7365. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  7366. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  7367. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  7368. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  7369. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  7370. };
  7371. #endif /* WOLFSSL_AES_256 */
  7372. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7373. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7374. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  7375. #ifdef HAVE_AES_DECRYPT
  7376. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7377. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  7378. #endif
  7379. #endif
  7380. ret = wc_AesInit(enc, HEAP_HINT, devId);
  7381. if (ret != 0)
  7382. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7383. else
  7384. enc_inited = 1;
  7385. #ifdef HAVE_AES_DECRYPT
  7386. ret = wc_AesInit(dec, HEAP_HINT, devId);
  7387. if (ret != 0)
  7388. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7389. else
  7390. dec_inited = 1;
  7391. #endif
  7392. #ifdef WOLFSSL_AES_128
  7393. /* 128 key tests */
  7394. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7395. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7396. ret = EVP_test(EVP_aes_128_cfb128(), key1, iv, msg1, sizeof(msg1),
  7397. cipher1, sizeof(cipher1));
  7398. if (ret != 0) {
  7399. return ret;
  7400. }
  7401. #endif
  7402. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7403. if (ret != 0)
  7404. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7405. #ifdef HAVE_AES_DECRYPT
  7406. /* decrypt uses AES_ENCRYPTION */
  7407. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7408. if (ret != 0)
  7409. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7410. #endif
  7411. XMEMSET(cipher, 0, sizeof(cipher));
  7412. ret = wc_AesCfbEncrypt(enc, cipher, msg1, AES_BLOCK_SIZE * 2);
  7413. if (ret != 0)
  7414. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7415. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  7416. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7417. /* test restarting encryption process */
  7418. ret = wc_AesCfbEncrypt(enc, cipher + (AES_BLOCK_SIZE * 2),
  7419. msg1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE);
  7420. if (ret != 0)
  7421. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7422. if (XMEMCMP(cipher + (AES_BLOCK_SIZE * 2),
  7423. cipher1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE))
  7424. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7425. #ifdef HAVE_AES_DECRYPT
  7426. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 3);
  7427. if (ret != 0)
  7428. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7429. if (XMEMCMP(plain, msg1, AES_BLOCK_SIZE * 3))
  7430. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7431. #endif /* HAVE_AES_DECRYPT */
  7432. #endif /* WOLFSSL_AES_128 */
  7433. #ifdef WOLFSSL_AES_192
  7434. /* 192 key size test */
  7435. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7436. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7437. ret = EVP_test(EVP_aes_192_cfb128(), key2, iv, msg2, sizeof(msg2),
  7438. cipher2, sizeof(cipher2));
  7439. if (ret != 0) {
  7440. return ret;
  7441. }
  7442. #endif
  7443. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv, AES_ENCRYPTION);
  7444. if (ret != 0)
  7445. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7446. #ifdef HAVE_AES_DECRYPT
  7447. /* decrypt uses AES_ENCRYPTION */
  7448. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv, AES_ENCRYPTION);
  7449. if (ret != 0)
  7450. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7451. #endif
  7452. XMEMSET(cipher, 0, sizeof(cipher));
  7453. ret = wc_AesCfbEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE * 4);
  7454. if (ret != 0)
  7455. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7456. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE * 4))
  7457. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7458. #ifdef HAVE_AES_DECRYPT
  7459. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 4);
  7460. if (ret != 0)
  7461. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7462. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE * 4))
  7463. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7464. #endif /* HAVE_AES_DECRYPT */
  7465. #endif /* WOLFSSL_AES_192 */
  7466. #ifdef WOLFSSL_AES_256
  7467. /* 256 key size test */
  7468. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7469. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7470. ret = EVP_test(EVP_aes_256_cfb128(), key3, iv, msg3, sizeof(msg3),
  7471. cipher3, sizeof(cipher3));
  7472. if (ret != 0) {
  7473. return ret;
  7474. }
  7475. #endif
  7476. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv, AES_ENCRYPTION);
  7477. if (ret != 0)
  7478. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7479. #ifdef HAVE_AES_DECRYPT
  7480. /* decrypt uses AES_ENCRYPTION */
  7481. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv, AES_ENCRYPTION);
  7482. if (ret != 0)
  7483. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7484. #endif
  7485. /* test with data left overs, magic lengths are checking near edges */
  7486. XMEMSET(cipher, 0, sizeof(cipher));
  7487. ret = wc_AesCfbEncrypt(enc, cipher, msg3, 4);
  7488. if (ret != 0)
  7489. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7490. if (XMEMCMP(cipher, cipher3, 4))
  7491. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7492. ret = wc_AesCfbEncrypt(enc, cipher + 4, msg3 + 4, 27);
  7493. if (ret != 0)
  7494. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7495. if (XMEMCMP(cipher + 4, cipher3 + 4, 27))
  7496. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7497. ret = wc_AesCfbEncrypt(enc, cipher + 31, msg3 + 31,
  7498. (AES_BLOCK_SIZE * 4) - 31);
  7499. if (ret != 0)
  7500. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7501. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE * 4))
  7502. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7503. #ifdef HAVE_AES_DECRYPT
  7504. ret = wc_AesCfbDecrypt(dec, plain, cipher, 4);
  7505. if (ret != 0)
  7506. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7507. if (XMEMCMP(plain, msg3, 4))
  7508. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7509. ret = wc_AesCfbDecrypt(dec, plain + 4, cipher + 4, 4);
  7510. if (ret != 0)
  7511. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7512. ret = wc_AesCfbDecrypt(dec, plain + 8, cipher + 8, 23);
  7513. if (ret != 0)
  7514. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7515. if (XMEMCMP(plain + 4, msg3 + 4, 27))
  7516. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7517. ret = wc_AesCfbDecrypt(dec, plain + 31, cipher + 31,
  7518. (AES_BLOCK_SIZE * 4) - 31);
  7519. if (ret != 0)
  7520. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7521. if (XMEMCMP(plain, msg3, AES_BLOCK_SIZE * 4))
  7522. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7523. #endif /* HAVE_AES_DECRYPT */
  7524. #endif /* WOLFSSL_AES_256 */
  7525. out:
  7526. if (enc_inited)
  7527. wc_AesFree(enc);
  7528. if (dec_inited)
  7529. wc_AesFree(dec);
  7530. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7531. if (enc)
  7532. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7533. #ifdef HAVE_AES_DECRYPT
  7534. if (dec)
  7535. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7536. #endif
  7537. #endif
  7538. return ret;
  7539. }
  7540. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7541. static wc_test_ret_t aescfb1_test(void)
  7542. {
  7543. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7544. Aes *enc = NULL;
  7545. #else
  7546. Aes enc[1];
  7547. #endif
  7548. int enc_inited = 0;
  7549. byte cipher[AES_BLOCK_SIZE];
  7550. #ifdef HAVE_AES_DECRYPT
  7551. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7552. Aes *dec = NULL;
  7553. #else
  7554. Aes dec[1];
  7555. #endif
  7556. int dec_inited = 0;
  7557. byte plain [AES_BLOCK_SIZE];
  7558. #endif
  7559. wc_test_ret_t ret = 0;
  7560. #ifdef WOLFSSL_AES_128
  7561. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  7562. 0x4d,0xbb,0xdc,0xaa,0x59,0xf3,0x63,0xc9,
  7563. 0x2a,0x3b,0x98,0x43,0xad,0x20,0xe2,0xb7
  7564. };
  7565. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  7566. {
  7567. 0xcd,0xef,0x9d,0x06,0x61,0xba,0xe4,0x73,
  7568. 0x8d,0x1a,0x58,0xa2,0xa6,0x22,0x8b,0x66
  7569. };
  7570. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  7571. {
  7572. 0x00
  7573. };
  7574. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  7575. {
  7576. 0xC0
  7577. };
  7578. #endif /* WOLFSSL_AES_128 */
  7579. #ifdef WOLFSSL_AES_192
  7580. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  7581. 0x57,0xc6,0x89,0x7c,0x99,0x52,0x28,0x13,
  7582. 0xbf,0x67,0x9c,0xe1,0x13,0x70,0xaf,0x5e
  7583. };
  7584. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  7585. {
  7586. 0xba,0xa1,0x58,0xa1,0x6b,0x50,0x4a,0x10,
  7587. 0x8e,0xd4,0x33,0x2e,0xe7,0xf2,0x9b,0xf6,
  7588. 0xd1,0xac,0x46,0xa8,0xde,0x5a,0xfe,0x7a
  7589. };
  7590. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  7591. {
  7592. 0x30
  7593. };
  7594. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  7595. {
  7596. 0x80
  7597. };
  7598. #endif /* WOLFSSL_AES_192 */
  7599. #ifdef WOLFSSL_AES_256
  7600. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  7601. 0x63,0x2e,0x9f,0x83,0x1f,0xa3,0x80,0x5e,
  7602. 0x52,0x02,0xbc,0xe0,0x6d,0x04,0xf9,0xa0
  7603. };
  7604. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  7605. {
  7606. 0xf6,0xfa,0xe4,0xf1,0x5d,0x91,0xfc,0x50,
  7607. 0x88,0x78,0x4f,0x84,0xa5,0x37,0x12,0x7e,
  7608. 0x32,0x63,0x55,0x9c,0x62,0x73,0x88,0x20,
  7609. 0xc2,0xcf,0x3d,0xe1,0x1c,0x2a,0x30,0x40
  7610. };
  7611. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  7612. {
  7613. 0xF7, 0x00
  7614. };
  7615. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  7616. {
  7617. 0x41, 0xC0
  7618. };
  7619. #endif /* WOLFSSL_AES_256 */
  7620. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7621. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7622. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  7623. #ifdef HAVE_AES_DECRYPT
  7624. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7625. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  7626. #endif
  7627. #endif
  7628. ret = wc_AesInit(enc, HEAP_HINT, devId);
  7629. if (ret != 0)
  7630. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7631. else
  7632. enc_inited = 1;
  7633. #ifdef HAVE_AES_DECRYPT
  7634. ret = wc_AesInit(dec, HEAP_HINT, devId);
  7635. if (ret != 0)
  7636. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7637. else
  7638. dec_inited = 1;
  7639. #endif
  7640. #ifdef WOLFSSL_AES_128
  7641. /* 128 key tests */
  7642. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7643. if (ret != 0)
  7644. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7645. #ifdef HAVE_AES_DECRYPT
  7646. /* decrypt uses AES_ENCRYPTION */
  7647. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7648. if (ret != 0)
  7649. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7650. #endif
  7651. XMEMSET(cipher, 0, sizeof(cipher));
  7652. ret = wc_AesCfb1Encrypt(enc, cipher, msg1, 2);
  7653. if (ret != 0)
  7654. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7655. if (cipher[0] != cipher1[0])
  7656. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7657. #ifdef HAVE_AES_DECRYPT
  7658. ret = wc_AesCfb1Decrypt(dec, plain, cipher, 2);
  7659. if (ret != 0)
  7660. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7661. if (plain[0] != msg1[0])
  7662. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7663. #endif /* HAVE_AES_DECRYPT */
  7664. #ifdef OPENSSL_EXTRA
  7665. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, 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, msg1,
  7670. sizeof(msg1) * WOLFSSL_BIT_SIZE);
  7671. if (ret != 0)
  7672. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7673. #ifndef WOLFCRYPT_ONLY
  7674. ret = EVP_test(EVP_aes_128_cfb1(), key1, iv, msg1, sizeof(msg1),
  7675. cipher, sizeof(msg1));
  7676. if (ret != 0) {
  7677. goto out;
  7678. }
  7679. #endif
  7680. #endif
  7681. #endif /* WOLFSSL_AES_128 */
  7682. #ifdef WOLFSSL_AES_192
  7683. /* 192 key tests */
  7684. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  7685. if (ret != 0)
  7686. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7687. XMEMSET(cipher, 0, sizeof(cipher));
  7688. ret = wc_AesCfb1Encrypt(enc, cipher, msg2, 4);
  7689. if (ret != 0)
  7690. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7691. if (XMEMCMP(cipher, cipher2, sizeof(cipher2)) != 0)
  7692. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7693. #ifdef OPENSSL_EXTRA
  7694. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, 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, msg2,
  7699. sizeof(msg2) * WOLFSSL_BIT_SIZE);
  7700. if (ret != 0)
  7701. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7702. #ifndef WOLFCRYPT_ONLY
  7703. ret = EVP_test(EVP_aes_192_cfb1(), key2, iv2, msg2, sizeof(msg2),
  7704. cipher, sizeof(msg2));
  7705. if (ret != 0) {
  7706. goto out;
  7707. }
  7708. #endif
  7709. #endif
  7710. #endif /* WOLFSSL_AES_192 */
  7711. #ifdef WOLFSSL_AES_256
  7712. /* 256 key tests */
  7713. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  7714. if (ret != 0)
  7715. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7716. XMEMSET(cipher, 0, sizeof(cipher));
  7717. ret = wc_AesCfb1Encrypt(enc, cipher, msg3, 10);
  7718. if (ret != 0)
  7719. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7720. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  7721. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7722. #ifdef OPENSSL_EXTRA
  7723. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  7724. if (ret != 0)
  7725. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7726. XMEMSET(cipher, 0, sizeof(cipher));
  7727. ret = wc_AesCfb1Encrypt(enc, cipher, msg3,
  7728. sizeof(msg3) * WOLFSSL_BIT_SIZE);
  7729. if (ret != 0)
  7730. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7731. #ifndef WOLFCRYPT_ONLY
  7732. ret = EVP_test(EVP_aes_256_cfb1(), key3, iv3, msg3, sizeof(msg3),
  7733. cipher, sizeof(msg3));
  7734. if (ret != 0) {
  7735. goto out;
  7736. }
  7737. #endif
  7738. #endif
  7739. #endif /* WOLFSSL_AES_256 */
  7740. out:
  7741. if (enc_inited)
  7742. wc_AesFree(enc);
  7743. #ifdef HAVE_AES_DECRYPT
  7744. if (dec_inited)
  7745. wc_AesFree(dec);
  7746. #endif
  7747. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7748. if (enc)
  7749. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7750. #ifdef HAVE_AES_DECRYPT
  7751. if (dec)
  7752. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7753. #endif
  7754. #endif
  7755. return ret;
  7756. }
  7757. static wc_test_ret_t aescfb8_test(void)
  7758. {
  7759. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7760. Aes *enc = NULL;
  7761. #else
  7762. Aes enc[1];
  7763. #endif
  7764. int enc_inited = 0;
  7765. byte cipher[AES_BLOCK_SIZE];
  7766. #ifdef HAVE_AES_DECRYPT
  7767. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7768. Aes *dec = NULL;
  7769. #else
  7770. Aes dec[1];
  7771. #endif
  7772. int dec_inited = 0;
  7773. byte plain [AES_BLOCK_SIZE];
  7774. #endif
  7775. wc_test_ret_t ret = 0;
  7776. #ifdef WOLFSSL_AES_128
  7777. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  7778. 0xf4,0x75,0xc6,0x49,0x91,0xb2,0x0e,0xae,
  7779. 0xe1,0x83,0xa2,0x26,0x29,0xe2,0x1e,0x22
  7780. };
  7781. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  7782. {
  7783. 0xc8,0xfe,0x9b,0xf7,0x7b,0x93,0x0f,0x46,
  7784. 0xd2,0x07,0x8b,0x8c,0x0e,0x65,0x7c,0xd4
  7785. };
  7786. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  7787. {
  7788. 0xd2,0x76,0x91
  7789. };
  7790. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  7791. {
  7792. 0xc9,0x06,0x35
  7793. };
  7794. #endif /* WOLFSSL_AES_128 */
  7795. #ifdef WOLFSSL_AES_192
  7796. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  7797. 0x0a,0x02,0x84,0x6b,0x62,0xab,0xb6,0x93,
  7798. 0xef,0x31,0xd7,0x54,0x84,0x2e,0xed,0x29
  7799. };
  7800. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  7801. {
  7802. 0xba,0xf0,0x8b,0x76,0x31,0x7a,0x65,0xc5,
  7803. 0xf0,0x7a,0xe6,0xf5,0x7e,0xb0,0xe6,0x54,
  7804. 0x88,0x65,0x93,0x24,0xd2,0x97,0x09,0xe3
  7805. };
  7806. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  7807. {
  7808. 0x72,0x9c,0x0b,0x6d,0xeb,0x75,0xfa,0x6e,
  7809. 0xb5,0xe8
  7810. };
  7811. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  7812. {
  7813. 0x98,0x95,0x93,0x24,0x02,0x39,0x3d,0xc3,
  7814. 0x3a,0x60
  7815. };
  7816. #endif
  7817. #ifdef WOLFSSL_AES_256
  7818. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  7819. 0x33,0x8c,0x55,0x2f,0xf1,0xec,0xa1,0x44,
  7820. 0x08,0xe0,0x5d,0x8c,0xf9,0xf3,0xb3,0x1b
  7821. };
  7822. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  7823. {
  7824. 0x06,0x48,0x74,0x09,0x2f,0x7a,0x13,0xcc,
  7825. 0x44,0x62,0x24,0x7a,0xd4,0x23,0xd0,0xe9,
  7826. 0x6e,0xdf,0x42,0xe8,0xb6,0x7a,0x5a,0x23,
  7827. 0xb7,0xa0,0xa6,0x47,0x7b,0x09,0x8e,0x66
  7828. };
  7829. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  7830. {
  7831. 0x1c,0xff,0x95
  7832. };
  7833. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  7834. {
  7835. 0xb9,0x74,0xfa
  7836. };
  7837. #endif
  7838. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7839. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7840. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  7841. #ifdef HAVE_AES_DECRYPT
  7842. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7843. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  7844. #endif
  7845. #endif
  7846. ret = wc_AesInit(enc, HEAP_HINT, devId);
  7847. if (ret != 0)
  7848. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7849. else
  7850. enc_inited = 1;
  7851. #ifdef HAVE_AES_DECRYPT
  7852. ret = wc_AesInit(dec, HEAP_HINT, devId);
  7853. if (ret != 0)
  7854. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7855. else
  7856. dec_inited = 1;
  7857. #endif
  7858. #ifdef WOLFSSL_AES_128
  7859. /* 128 key tests */
  7860. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7861. ret = EVP_test(EVP_aes_128_cfb8(), key1, iv, msg1, sizeof(msg1),
  7862. cipher1, sizeof(cipher1));
  7863. if (ret != 0) {
  7864. return ret;
  7865. }
  7866. #endif
  7867. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7868. if (ret != 0)
  7869. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7870. #ifdef HAVE_AES_DECRYPT
  7871. /* decrypt uses AES_ENCRYPTION */
  7872. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7873. if (ret != 0)
  7874. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7875. #endif
  7876. XMEMSET(cipher, 0, sizeof(cipher));
  7877. ret = wc_AesCfb8Encrypt(enc, cipher, msg1, sizeof(msg1));
  7878. if (ret != 0)
  7879. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7880. if (XMEMCMP(cipher, cipher1, sizeof(cipher1)) != 0)
  7881. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7882. #ifdef HAVE_AES_DECRYPT
  7883. ret = wc_AesCfb8Decrypt(dec, plain, cipher, sizeof(msg1));
  7884. if (ret != 0)
  7885. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7886. if (XMEMCMP(plain, msg1, sizeof(msg1)) != 0)
  7887. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7888. #endif /* HAVE_AES_DECRYPT */
  7889. #endif /* WOLFSSL_AES_128 */
  7890. #ifdef WOLFSSL_AES_192
  7891. /* 192 key tests */
  7892. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, 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, msg2, sizeof(msg2));
  7897. if (ret != 0)
  7898. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7899. if (XMEMCMP(cipher, cipher2, sizeof(msg2)) != 0)
  7900. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7901. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7902. ret = EVP_test(EVP_aes_192_cfb8(), key2, iv2, msg2, sizeof(msg2),
  7903. cipher2, sizeof(msg2));
  7904. if (ret != 0) {
  7905. return ret;
  7906. }
  7907. #endif
  7908. #endif /* WOLFSSL_AES_192 */
  7909. #ifdef WOLFSSL_AES_256
  7910. /* 256 key tests */
  7911. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  7912. if (ret != 0)
  7913. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7914. XMEMSET(cipher, 0, sizeof(cipher));
  7915. ret = wc_AesCfb8Encrypt(enc, cipher, msg3, sizeof(msg3));
  7916. if (ret != 0)
  7917. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7918. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  7919. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  7920. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7921. ret = EVP_test(EVP_aes_256_cfb8(), key3, iv3, msg3, sizeof(msg3),
  7922. cipher3, sizeof(msg3));
  7923. if (ret != 0) {
  7924. goto out;
  7925. }
  7926. #endif
  7927. #endif /* WOLFSSL_AES_256 */
  7928. out:
  7929. if (enc_inited)
  7930. wc_AesFree(enc);
  7931. #ifdef HAVE_AES_DECRYPT
  7932. if (dec_inited)
  7933. wc_AesFree(dec);
  7934. #endif
  7935. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7936. if (enc)
  7937. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7938. #ifdef HAVE_AES_DECRYPT
  7939. if (dec)
  7940. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7941. #endif
  7942. #endif
  7943. return ret;
  7944. }
  7945. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  7946. #endif /* WOLFSSL_AES_CFB */
  7947. static wc_test_ret_t aes_key_size_test(void)
  7948. {
  7949. wc_test_ret_t ret;
  7950. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7951. Aes *aes;
  7952. #else
  7953. Aes aes[1];
  7954. #endif
  7955. byte key16[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7956. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  7957. #ifndef WOLFSSL_CRYPTOCELL
  7958. byte key24[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7959. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  7960. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37 };
  7961. #endif
  7962. byte key32[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7963. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  7964. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7965. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  7966. byte iv[] = "1234567890abcdef";
  7967. #ifndef HAVE_FIPS
  7968. word32 keySize;
  7969. #endif
  7970. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7971. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7972. return WC_TEST_RET_ENC_ERRNO;
  7973. #endif
  7974. #if !defined(HAVE_FIPS) || \
  7975. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  7976. /* w/ FIPS v1 (cert 2425) wc_AesInit just returns 0 always as it's not
  7977. * supported with that FIPS version */
  7978. ret = wc_AesInit(NULL, HEAP_HINT, devId);
  7979. if (ret != BAD_FUNC_ARG)
  7980. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7981. #endif
  7982. ret = wc_AesInit(aes, HEAP_HINT, devId);
  7983. /* 0 check OK for FIPSv1 */
  7984. if (ret != 0)
  7985. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7986. #ifndef HAVE_FIPS
  7987. /* Parameter Validation testing. */
  7988. ret = wc_AesGetKeySize(NULL, NULL);
  7989. if (ret != BAD_FUNC_ARG)
  7990. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7991. ret = wc_AesGetKeySize(aes, NULL);
  7992. if (ret != BAD_FUNC_ARG)
  7993. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7994. ret = wc_AesGetKeySize(NULL, &keySize);
  7995. if (ret != BAD_FUNC_ARG)
  7996. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  7997. /* Crashes in FIPS */
  7998. ret = wc_AesSetKey(NULL, key16, sizeof(key16), iv, AES_ENCRYPTION);
  7999. if (ret != BAD_FUNC_ARG)
  8000. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8001. #endif
  8002. /* NULL IV indicates to use all zeros IV. */
  8003. ret = wc_AesSetKey(aes, key16, sizeof(key16), NULL, AES_ENCRYPTION);
  8004. #ifdef WOLFSSL_AES_128
  8005. if (ret != 0)
  8006. #else
  8007. if (ret != BAD_FUNC_ARG)
  8008. #endif
  8009. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8010. ret = wc_AesSetKey(aes, key32, sizeof(key32) - 1, iv, AES_ENCRYPTION);
  8011. if (ret != BAD_FUNC_ARG)
  8012. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8013. /* CryptoCell handles rounds internally */
  8014. #if !defined(HAVE_FIPS) && !defined(WOLFSSL_CRYPTOCELL)
  8015. /* PSA don't use aes->rounds */
  8016. #if !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_AES)
  8017. /* Force invalid rounds */
  8018. aes->rounds = 16;
  8019. ret = wc_AesGetKeySize(aes, &keySize);
  8020. if (ret != BAD_FUNC_ARG)
  8021. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8022. #endif
  8023. #endif
  8024. ret = wc_AesSetKey(aes, key16, sizeof(key16), iv, AES_ENCRYPTION);
  8025. #ifdef WOLFSSL_AES_128
  8026. if (ret != 0)
  8027. #else
  8028. if (ret != BAD_FUNC_ARG)
  8029. #endif
  8030. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8031. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_128)
  8032. ret = wc_AesGetKeySize(aes, &keySize);
  8033. if (ret != 0 || keySize != sizeof(key16))
  8034. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8035. #endif
  8036. #ifndef WOLFSSL_CRYPTOCELL
  8037. /* Cryptocell only supports AES-128 key size */
  8038. ret = wc_AesSetKey(aes, key24, sizeof(key24), iv, AES_ENCRYPTION);
  8039. #ifdef WOLFSSL_AES_192
  8040. if (ret != 0)
  8041. #else
  8042. if (ret != BAD_FUNC_ARG)
  8043. #endif
  8044. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8045. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_192)
  8046. ret = wc_AesGetKeySize(aes, &keySize);
  8047. if (ret != 0 || keySize != sizeof(key24))
  8048. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8049. #endif
  8050. ret = wc_AesSetKey(aes, key32, sizeof(key32), iv, AES_ENCRYPTION);
  8051. #ifdef WOLFSSL_AES_256
  8052. if (ret != 0)
  8053. #else
  8054. if (ret != BAD_FUNC_ARG)
  8055. #endif
  8056. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8057. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_256)
  8058. ret = wc_AesGetKeySize(aes, &keySize);
  8059. if (ret != 0 || keySize != sizeof(key32))
  8060. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8061. #endif
  8062. #endif /* !WOLFSSL_CRYPTOCELL */
  8063. ret = 0; /* success */
  8064. out:
  8065. wc_AesFree(aes);
  8066. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8067. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  8068. #endif
  8069. return ret;
  8070. }
  8071. #if defined(WOLFSSL_AES_XTS)
  8072. /* test vectors from http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html */
  8073. #ifdef WOLFSSL_AES_128
  8074. static wc_test_ret_t aes_xts_128_test(void)
  8075. {
  8076. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8077. XtsAes *aes = NULL;
  8078. #else
  8079. XtsAes aes[1];
  8080. #endif
  8081. int aes_inited = 0;
  8082. wc_test_ret_t ret = 0;
  8083. unsigned char buf[AES_BLOCK_SIZE * 2 + 8];
  8084. unsigned char cipher[AES_BLOCK_SIZE * 2 + 8];
  8085. /* 128 key tests */
  8086. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  8087. 0xa1, 0xb9, 0x0c, 0xba, 0x3f, 0x06, 0xac, 0x35,
  8088. 0x3b, 0x2c, 0x34, 0x38, 0x76, 0x08, 0x17, 0x62,
  8089. 0x09, 0x09, 0x23, 0x02, 0x6e, 0x91, 0x77, 0x18,
  8090. 0x15, 0xf2, 0x9d, 0xab, 0x01, 0x93, 0x2f, 0x2f
  8091. };
  8092. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  8093. 0x4f, 0xae, 0xf7, 0x11, 0x7c, 0xda, 0x59, 0xc6,
  8094. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  8095. };
  8096. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  8097. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  8098. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c
  8099. };
  8100. /* plain text test of partial block is not from NIST test vector list */
  8101. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  8102. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  8103. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  8104. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  8105. };
  8106. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  8107. 0x77, 0x8a, 0xe8, 0xb4, 0x3c, 0xb9, 0x8d, 0x5a,
  8108. 0x82, 0x50, 0x81, 0xd5, 0xbe, 0x47, 0x1c, 0x63
  8109. };
  8110. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  8111. 0x39, 0x25, 0x79, 0x05, 0xdf, 0xcc, 0x77, 0x76,
  8112. 0x6c, 0x87, 0x0a, 0x80, 0x6a, 0x60, 0xe3, 0xc0,
  8113. 0x93, 0xd1, 0x2a, 0xcf, 0xcb, 0x51, 0x42, 0xfa,
  8114. 0x09, 0x69, 0x89, 0x62, 0x5b, 0x60, 0xdb, 0x16
  8115. };
  8116. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  8117. 0x5c, 0xf7, 0x9d, 0xb6, 0xc5, 0xcd, 0x99, 0x1a,
  8118. 0x1c, 0x78, 0x81, 0x42, 0x24, 0x95, 0x1e, 0x84
  8119. };
  8120. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  8121. 0xbd, 0xc5, 0x46, 0x8f, 0xbc, 0x8d, 0x50, 0xa1,
  8122. 0x0d, 0x1c, 0x85, 0x7f, 0x79, 0x1c, 0x5c, 0xba,
  8123. 0xb3, 0x81, 0x0d, 0x0d, 0x73, 0xcf, 0x8f, 0x20,
  8124. 0x46, 0xb1, 0xd1, 0x9e, 0x7d, 0x5d, 0x8a, 0x56
  8125. };
  8126. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  8127. 0xd6, 0xbe, 0x04, 0x6d, 0x41, 0xf2, 0x3b, 0x5e,
  8128. 0xd7, 0x0b, 0x6b, 0x3d, 0x5c, 0x8e, 0x66, 0x23,
  8129. 0x2b, 0xe6, 0xb8, 0x07, 0xd4, 0xdc, 0xc6, 0x0e,
  8130. 0xff, 0x8d, 0xbc, 0x1d, 0x9f, 0x7f, 0xc8, 0x22
  8131. };
  8132. #if !defined(HAVE_FIPS) || FIPS_VERSION_GE(5,3)
  8133. WOLFSSL_SMALL_STACK_STATIC unsigned char k3[] = {
  8134. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8135. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8136. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8137. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8138. };
  8139. WOLFSSL_SMALL_STACK_STATIC unsigned char i3[] = {
  8140. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8141. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8142. };
  8143. WOLFSSL_SMALL_STACK_STATIC unsigned char p3[] = {
  8144. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8145. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8146. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8147. 0x20, 0xff, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  8148. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20
  8149. };
  8150. WOLFSSL_SMALL_STACK_STATIC unsigned char c3[] = {
  8151. 0xA2, 0x07, 0x47, 0x76, 0x3F, 0xEC, 0x0C, 0x23,
  8152. 0x1B, 0xD0, 0xBD, 0x46, 0x9A, 0x27, 0x38, 0x12,
  8153. 0x95, 0x02, 0x3D, 0x5D, 0xC6, 0x94, 0x51, 0x36,
  8154. 0xA0, 0x85, 0xD2, 0x69, 0x6E, 0x87, 0x0A, 0xBF,
  8155. 0xB5, 0x5A, 0xDD, 0xCB, 0x80, 0xE0, 0xFC, 0xCD
  8156. };
  8157. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  8158. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8159. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8160. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8161. #endif
  8162. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  8163. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  8164. ret = EVP_test(EVP_aes_128_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  8165. if (ret != 0) {
  8166. printf("EVP_aes_128_xts failed!\n");
  8167. goto out;
  8168. }
  8169. #endif
  8170. XMEMSET(buf, 0, sizeof(buf));
  8171. ret = wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  8172. HEAP_HINT, devId);
  8173. if (ret != 0)
  8174. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8175. else
  8176. aes_inited = 1;
  8177. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  8178. #if defined(WOLFSSL_ASYNC_CRYPT)
  8179. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8180. #endif
  8181. if (ret != 0)
  8182. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8183. if (XMEMCMP(c2, buf, sizeof(c2)))
  8184. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8185. XMEMSET(buf, 0, sizeof(buf));
  8186. wc_AesXtsFree(aes);
  8187. ret = wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  8188. HEAP_HINT, devId);
  8189. if (ret != 0)
  8190. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8191. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  8192. #if defined(WOLFSSL_ASYNC_CRYPT)
  8193. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8194. #endif
  8195. if (ret != 0)
  8196. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8197. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  8198. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8199. /* partial block encryption test */
  8200. XMEMSET(cipher, 0, sizeof(cipher));
  8201. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  8202. #if defined(WOLFSSL_ASYNC_CRYPT)
  8203. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8204. #endif
  8205. if (ret != 0)
  8206. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8207. wc_AesXtsFree(aes);
  8208. /* partial block decrypt test */
  8209. XMEMSET(buf, 0, sizeof(buf));
  8210. ret = wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  8211. HEAP_HINT, devId);
  8212. if (ret != 0)
  8213. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8214. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  8215. #if defined(WOLFSSL_ASYNC_CRYPT)
  8216. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8217. #endif
  8218. if (ret != 0)
  8219. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8220. if (XMEMCMP(pp, buf, sizeof(pp)))
  8221. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8222. /* NIST decrypt test vector */
  8223. XMEMSET(buf, 0, sizeof(buf));
  8224. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  8225. #if defined(WOLFSSL_ASYNC_CRYPT)
  8226. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8227. #endif
  8228. if (ret != 0)
  8229. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8230. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  8231. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8232. /* fail case with decrypting using wrong key */
  8233. XMEMSET(buf, 0, sizeof(buf));
  8234. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  8235. #if defined(WOLFSSL_ASYNC_CRYPT)
  8236. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8237. #endif
  8238. if (ret != 0)
  8239. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8240. if (XMEMCMP(p2, buf, sizeof(p2)) == 0) /* fail case with wrong key */
  8241. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8242. wc_AesXtsFree(aes);
  8243. /* set correct key and retest */
  8244. XMEMSET(buf, 0, sizeof(buf));
  8245. ret = wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  8246. HEAP_HINT, devId);
  8247. if (ret != 0)
  8248. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8249. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  8250. #if defined(WOLFSSL_ASYNC_CRYPT)
  8251. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8252. #endif
  8253. if (ret != 0)
  8254. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8255. if (XMEMCMP(p2, buf, sizeof(p2)))
  8256. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8257. wc_AesXtsFree(aes);
  8258. #if !defined(HAVE_FIPS) || FIPS_VERSION_GE(5,3)
  8259. /* Test ciphertext stealing in-place. */
  8260. XMEMCPY(buf, p3, sizeof(p3));
  8261. ret = wc_AesXtsSetKey(aes, k3, sizeof(k3), AES_ENCRYPTION,
  8262. HEAP_HINT, devId);
  8263. if (ret != 0)
  8264. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8265. else
  8266. aes_inited = 1;
  8267. ret = wc_AesXtsEncrypt(aes, buf, buf, sizeof(p3), i3, sizeof(i3));
  8268. #if defined(WOLFSSL_ASYNC_CRYPT)
  8269. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8270. #endif
  8271. if (ret != 0)
  8272. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8273. if (XMEMCMP(c3, buf, sizeof(c3)))
  8274. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8275. wc_AesXtsFree(aes);
  8276. ret = wc_AesXtsSetKey(aes, k3, sizeof(k3), AES_DECRYPTION,
  8277. HEAP_HINT, devId);
  8278. if (ret != 0)
  8279. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8280. ret = wc_AesXtsDecrypt(aes, buf, buf, sizeof(c3), i3, sizeof(i3));
  8281. #if defined(WOLFSSL_ASYNC_CRYPT)
  8282. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8283. #endif
  8284. if (ret != 0)
  8285. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8286. if (XMEMCMP(p3, buf, sizeof(p3)))
  8287. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8288. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  8289. out:
  8290. if (aes_inited)
  8291. wc_AesXtsFree(aes);
  8292. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8293. if (aes)
  8294. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  8295. #endif
  8296. return ret;
  8297. }
  8298. #endif /* WOLFSSL_AES_128 */
  8299. #ifdef WOLFSSL_AES_256
  8300. static wc_test_ret_t aes_xts_256_test(void)
  8301. {
  8302. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8303. XtsAes *aes = NULL;
  8304. #else
  8305. XtsAes aes[1];
  8306. #endif
  8307. int aes_inited = 0;
  8308. wc_test_ret_t ret = 0;
  8309. unsigned char buf[AES_BLOCK_SIZE * 3];
  8310. unsigned char cipher[AES_BLOCK_SIZE * 3];
  8311. /* 256 key tests */
  8312. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  8313. 0x1e, 0xa6, 0x61, 0xc5, 0x8d, 0x94, 0x3a, 0x0e,
  8314. 0x48, 0x01, 0xe4, 0x2f, 0x4b, 0x09, 0x47, 0x14,
  8315. 0x9e, 0x7f, 0x9f, 0x8e, 0x3e, 0x68, 0xd0, 0xc7,
  8316. 0x50, 0x52, 0x10, 0xbd, 0x31, 0x1a, 0x0e, 0x7c,
  8317. 0xd6, 0xe1, 0x3f, 0xfd, 0xf2, 0x41, 0x8d, 0x8d,
  8318. 0x19, 0x11, 0xc0, 0x04, 0xcd, 0xa5, 0x8d, 0xa3,
  8319. 0xd6, 0x19, 0xb7, 0xe2, 0xb9, 0x14, 0x1e, 0x58,
  8320. 0x31, 0x8e, 0xea, 0x39, 0x2c, 0xf4, 0x1b, 0x08
  8321. };
  8322. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  8323. 0xad, 0xf8, 0xd9, 0x26, 0x27, 0x46, 0x4a, 0xd2,
  8324. 0xf0, 0x42, 0x8e, 0x84, 0xa9, 0xf8, 0x75, 0x64
  8325. };
  8326. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  8327. 0x2e, 0xed, 0xea, 0x52, 0xcd, 0x82, 0x15, 0xe1,
  8328. 0xac, 0xc6, 0x47, 0xe8, 0x10, 0xbb, 0xc3, 0x64,
  8329. 0x2e, 0x87, 0x28, 0x7f, 0x8d, 0x2e, 0x57, 0xe3,
  8330. 0x6c, 0x0a, 0x24, 0xfb, 0xc1, 0x2a, 0x20, 0x2e
  8331. };
  8332. /* plain text test of partial block is not from NIST test vector list */
  8333. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  8334. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  8335. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  8336. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  8337. };
  8338. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  8339. 0xcb, 0xaa, 0xd0, 0xe2, 0xf6, 0xce, 0xa3, 0xf5,
  8340. 0x0b, 0x37, 0xf9, 0x34, 0xd4, 0x6a, 0x9b, 0x13,
  8341. 0x0b, 0x9d, 0x54, 0xf0, 0x7e, 0x34, 0xf3, 0x6a,
  8342. 0xf7, 0x93, 0xe8, 0x6f, 0x73, 0xc6, 0xd7, 0xdb
  8343. };
  8344. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  8345. 0xad, 0x50, 0x4b, 0x85, 0xd7, 0x51, 0xbf, 0xba,
  8346. 0x69, 0x13, 0xb4, 0xcc, 0x79, 0xb6, 0x5a, 0x62,
  8347. 0xf7, 0xf3, 0x9d, 0x36, 0x0f, 0x35, 0xb5, 0xec,
  8348. 0x4a, 0x7e, 0x95, 0xbd, 0x9b, 0xa5, 0xf2, 0xec,
  8349. 0xc1, 0xd7, 0x7e, 0xa3, 0xc3, 0x74, 0xbd, 0x4b,
  8350. 0x13, 0x1b, 0x07, 0x83, 0x87, 0xdd, 0x55, 0x5a,
  8351. 0xb5, 0xb0, 0xc7, 0xe5, 0x2d, 0xb5, 0x06, 0x12,
  8352. 0xd2, 0xb5, 0x3a, 0xcb, 0x47, 0x8a, 0x53, 0xb4
  8353. };
  8354. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  8355. 0xe6, 0x42, 0x19, 0xed, 0xe0, 0xe1, 0xc2, 0xa0,
  8356. 0x0e, 0xf5, 0x58, 0x6a, 0xc4, 0x9b, 0xeb, 0x6f
  8357. };
  8358. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  8359. 0x24, 0xcb, 0x76, 0x22, 0x55, 0xb5, 0xa8, 0x00,
  8360. 0xf4, 0x6e, 0x80, 0x60, 0x56, 0x9e, 0x05, 0x53,
  8361. 0xbc, 0xfe, 0x86, 0x55, 0x3b, 0xca, 0xd5, 0x89,
  8362. 0xc7, 0x54, 0x1a, 0x73, 0xac, 0xc3, 0x9a, 0xbd,
  8363. 0x53, 0xc4, 0x07, 0x76, 0xd8, 0xe8, 0x22, 0x61,
  8364. 0x9e, 0xa9, 0xad, 0x77, 0xa0, 0x13, 0x4c, 0xfc
  8365. };
  8366. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  8367. 0xa3, 0xc6, 0xf3, 0xf3, 0x82, 0x79, 0x5b, 0x10,
  8368. 0x87, 0xd7, 0x02, 0x50, 0xdb, 0x2c, 0xd3, 0xb1,
  8369. 0xa1, 0x62, 0xa8, 0xb6, 0xdc, 0x12, 0x60, 0x61,
  8370. 0xc1, 0x0a, 0x84, 0xa5, 0x85, 0x3f, 0x3a, 0x89,
  8371. 0xe6, 0x6c, 0xdb, 0xb7, 0x9a, 0xb4, 0x28, 0x9b,
  8372. 0xc3, 0xea, 0xd8, 0x10, 0xe9, 0xc0, 0xaf, 0x92
  8373. };
  8374. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8375. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8376. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8377. #endif
  8378. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  8379. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  8380. ret = EVP_test(EVP_aes_256_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  8381. if (ret != 0) {
  8382. printf("EVP_aes_256_xts failed\n");
  8383. goto out;
  8384. }
  8385. #endif
  8386. XMEMSET(buf, 0, sizeof(buf));
  8387. ret = wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  8388. HEAP_HINT, devId);
  8389. if (ret != 0)
  8390. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8391. else
  8392. aes_inited = 1;
  8393. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  8394. #if defined(WOLFSSL_ASYNC_CRYPT)
  8395. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8396. #endif
  8397. if (ret != 0)
  8398. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8399. if (XMEMCMP(c2, buf, sizeof(c2)))
  8400. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8401. wc_AesXtsFree(aes);
  8402. XMEMSET(buf, 0, sizeof(buf));
  8403. ret = wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  8404. HEAP_HINT, devId);
  8405. if (ret != 0)
  8406. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8407. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  8408. #if defined(WOLFSSL_ASYNC_CRYPT)
  8409. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8410. #endif
  8411. if (ret != 0)
  8412. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8413. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  8414. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8415. /* partial block encryption test */
  8416. XMEMSET(cipher, 0, sizeof(cipher));
  8417. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  8418. #if defined(WOLFSSL_ASYNC_CRYPT)
  8419. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8420. #endif
  8421. if (ret != 0)
  8422. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8423. wc_AesXtsFree(aes);
  8424. /* partial block decrypt test */
  8425. XMEMSET(buf, 0, sizeof(buf));
  8426. ret = wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  8427. HEAP_HINT, devId);
  8428. if (ret != 0)
  8429. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8430. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  8431. #if defined(WOLFSSL_ASYNC_CRYPT)
  8432. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8433. #endif
  8434. if (ret != 0)
  8435. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8436. if (XMEMCMP(pp, buf, sizeof(pp)))
  8437. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8438. /* NIST decrypt test vector */
  8439. XMEMSET(buf, 0, sizeof(buf));
  8440. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  8441. #if defined(WOLFSSL_ASYNC_CRYPT)
  8442. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8443. #endif
  8444. if (ret != 0)
  8445. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8446. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  8447. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8448. wc_AesXtsFree(aes);
  8449. XMEMSET(buf, 0, sizeof(buf));
  8450. ret = wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  8451. HEAP_HINT, devId);
  8452. if (ret != 0)
  8453. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8454. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  8455. #if defined(WOLFSSL_ASYNC_CRYPT)
  8456. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8457. #endif
  8458. if (ret != 0)
  8459. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8460. if (XMEMCMP(p2, buf, sizeof(p2)))
  8461. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8462. out:
  8463. if (aes_inited)
  8464. wc_AesXtsFree(aes);
  8465. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8466. if (aes)
  8467. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  8468. #endif
  8469. return ret;
  8470. }
  8471. #endif /* WOLFSSL_AES_256 */
  8472. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  8473. /* both 128 and 256 bit key test */
  8474. static wc_test_ret_t aes_xts_sector_test(void)
  8475. {
  8476. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8477. XtsAes *aes = NULL;
  8478. #else
  8479. XtsAes aes[1];
  8480. #endif
  8481. int aes_inited = 0;
  8482. wc_test_ret_t ret = 0;
  8483. unsigned char buf[AES_BLOCK_SIZE * 2];
  8484. /* 128 key tests */
  8485. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  8486. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  8487. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  8488. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  8489. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  8490. };
  8491. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  8492. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  8493. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  8494. };
  8495. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  8496. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  8497. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  8498. };
  8499. word64 s1 = 141;
  8500. /* 256 key tests */
  8501. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  8502. 0xef, 0x01, 0x0c, 0xa1, 0xa3, 0x66, 0x3e, 0x32,
  8503. 0x53, 0x43, 0x49, 0xbc, 0x0b, 0xae, 0x62, 0x23,
  8504. 0x2a, 0x15, 0x73, 0x34, 0x85, 0x68, 0xfb, 0x9e,
  8505. 0xf4, 0x17, 0x68, 0xa7, 0x67, 0x4f, 0x50, 0x7a,
  8506. 0x72, 0x7f, 0x98, 0x75, 0x53, 0x97, 0xd0, 0xe0,
  8507. 0xaa, 0x32, 0xf8, 0x30, 0x33, 0x8c, 0xc7, 0xa9,
  8508. 0x26, 0xc7, 0x73, 0xf0, 0x9e, 0x57, 0xb3, 0x57,
  8509. 0xcd, 0x15, 0x6a, 0xfb, 0xca, 0x46, 0xe1, 0xa0
  8510. };
  8511. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  8512. 0xed, 0x98, 0xe0, 0x17, 0x70, 0xa8, 0x53, 0xb4,
  8513. 0x9d, 0xb9, 0xe6, 0xaa, 0xf8, 0x8f, 0x0a, 0x41,
  8514. 0xb9, 0xb5, 0x6e, 0x91, 0xa5, 0xa2, 0xb1, 0x1d,
  8515. 0x40, 0x52, 0x92, 0x54, 0xf5, 0x52, 0x3e, 0x75
  8516. };
  8517. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  8518. 0xca, 0x20, 0xc5, 0x5e, 0x8d, 0xc1, 0x49, 0x68,
  8519. 0x7d, 0x25, 0x41, 0xde, 0x39, 0xc3, 0xdf, 0x63,
  8520. 0x00, 0xbb, 0x5a, 0x16, 0x3c, 0x10, 0xce, 0xd3,
  8521. 0x66, 0x6b, 0x13, 0x57, 0xdb, 0x8b, 0xd3, 0x9d
  8522. };
  8523. word64 s2 = 187;
  8524. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8525. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8526. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8527. #endif
  8528. XMEMSET(buf, 0, sizeof(buf));
  8529. ret = wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  8530. HEAP_HINT, devId);
  8531. if (ret != 0)
  8532. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8533. else
  8534. aes_inited = 1;
  8535. ret = wc_AesXtsEncryptSector(aes, buf, p1, sizeof(p1), s1);
  8536. #if defined(WOLFSSL_ASYNC_CRYPT)
  8537. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8538. #endif
  8539. if (ret != 0)
  8540. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8541. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  8542. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8543. wc_AesXtsFree(aes);
  8544. /* decrypt test */
  8545. XMEMSET(buf, 0, sizeof(buf));
  8546. ret = wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  8547. HEAP_HINT, devId);
  8548. if (ret != 0)
  8549. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8550. ret = wc_AesXtsDecryptSector(aes, buf, c1, sizeof(c1), s1);
  8551. #if defined(WOLFSSL_ASYNC_CRYPT)
  8552. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8553. #endif
  8554. if (ret != 0)
  8555. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8556. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  8557. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8558. wc_AesXtsFree(aes);
  8559. /* 256 bit key tests */
  8560. XMEMSET(buf, 0, sizeof(buf));
  8561. ret = wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  8562. HEAP_HINT, devId);
  8563. if (ret != 0)
  8564. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8565. ret = wc_AesXtsEncryptSector(aes, buf, p2, sizeof(p2), s2);
  8566. #if defined(WOLFSSL_ASYNC_CRYPT)
  8567. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8568. #endif
  8569. if (ret != 0)
  8570. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8571. if (XMEMCMP(c2, buf, sizeof(c2)))
  8572. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8573. wc_AesXtsFree(aes);
  8574. /* decrypt test */
  8575. XMEMSET(buf, 0, sizeof(buf));
  8576. ret = wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  8577. HEAP_HINT, devId);
  8578. if (ret != 0)
  8579. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8580. ret = wc_AesXtsDecryptSector(aes, buf, c2, sizeof(c2), s2);
  8581. #if defined(WOLFSSL_ASYNC_CRYPT)
  8582. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8583. #endif
  8584. if (ret != 0)
  8585. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8586. if (XMEMCMP(p2, buf, sizeof(p2)))
  8587. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8588. out:
  8589. if (aes_inited)
  8590. wc_AesXtsFree(aes);
  8591. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8592. if (aes)
  8593. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  8594. #endif
  8595. return ret;
  8596. }
  8597. #endif /* WOLFSSL_AES_128 && WOLFSSL_AES_256 */
  8598. #ifdef WOLFSSL_AES_128
  8599. /* testing of bad arguments */
  8600. static wc_test_ret_t aes_xts_args_test(void)
  8601. {
  8602. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8603. XtsAes *aes = NULL;
  8604. #else
  8605. XtsAes aes[1];
  8606. #endif
  8607. int aes_inited = 0;
  8608. wc_test_ret_t ret;
  8609. unsigned char buf[AES_BLOCK_SIZE * 2];
  8610. /* 128 key tests */
  8611. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  8612. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  8613. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  8614. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  8615. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  8616. };
  8617. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  8618. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  8619. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  8620. };
  8621. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  8622. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  8623. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  8624. };
  8625. word64 s1 = 141;
  8626. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8627. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  8628. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  8629. #endif
  8630. if (wc_AesXtsSetKey(NULL, k1, sizeof(k1), AES_ENCRYPTION,
  8631. HEAP_HINT, devId) == 0)
  8632. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8633. if (wc_AesXtsSetKey(aes, NULL, sizeof(k1), AES_ENCRYPTION,
  8634. HEAP_HINT, devId) == 0)
  8635. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8636. /* encryption operations */
  8637. ret = wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  8638. HEAP_HINT, devId);
  8639. if (ret != 0)
  8640. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8641. else
  8642. aes_inited = 1;
  8643. ret = wc_AesXtsEncryptSector(NULL, buf, p1, sizeof(p1), s1);
  8644. #if defined(WOLFSSL_ASYNC_CRYPT)
  8645. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8646. #endif
  8647. if (ret == 0)
  8648. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8649. ret = wc_AesXtsEncryptSector(aes, NULL, p1, sizeof(p1), s1);
  8650. #if defined(WOLFSSL_ASYNC_CRYPT)
  8651. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8652. #endif
  8653. if (ret == 0)
  8654. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8655. wc_AesXtsFree(aes);
  8656. /* decryption operations */
  8657. ret = wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  8658. HEAP_HINT, devId);
  8659. if (ret != 0)
  8660. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8661. ret = wc_AesXtsDecryptSector(NULL, buf, c1, sizeof(c1), s1);
  8662. #if defined(WOLFSSL_ASYNC_CRYPT)
  8663. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8664. #endif
  8665. if (ret == 0)
  8666. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8667. ret = wc_AesXtsDecryptSector(aes, NULL, c1, sizeof(c1), s1);
  8668. #if defined(WOLFSSL_ASYNC_CRYPT)
  8669. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  8670. #endif
  8671. if (ret == 0)
  8672. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8673. ret = 0;
  8674. out:
  8675. if (aes_inited)
  8676. wc_AesXtsFree(aes);
  8677. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8678. if (aes)
  8679. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  8680. #endif
  8681. return ret;
  8682. }
  8683. #endif /* WOLFSSL_AES_128 */
  8684. #endif /* WOLFSSL_AES_XTS */
  8685. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  8686. static wc_test_ret_t aes_cbc_test(void)
  8687. {
  8688. byte cipher[AES_BLOCK_SIZE];
  8689. byte plain[AES_BLOCK_SIZE];
  8690. wc_test_ret_t ret;
  8691. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  8692. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  8693. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  8694. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  8695. };
  8696. byte key[] = "0123456789abcdef "; /* align */
  8697. byte iv[] = "1234567890abcdef "; /* align */
  8698. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  8699. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  8700. /* Parameter Validation testing. */
  8701. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key, 17, NULL);
  8702. if (ret != BAD_FUNC_ARG)
  8703. return WC_TEST_RET_ENC_EC(ret);
  8704. #ifdef HAVE_AES_DECRYPT
  8705. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key, 17, NULL);
  8706. if (ret != BAD_FUNC_ARG)
  8707. return WC_TEST_RET_ENC_EC(ret);
  8708. #endif
  8709. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key,
  8710. AES_BLOCK_SIZE, iv);
  8711. if (ret != 0)
  8712. return WC_TEST_RET_ENC_EC(ret);
  8713. #ifdef HAVE_AES_DECRYPT
  8714. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key,
  8715. AES_BLOCK_SIZE, iv);
  8716. if (ret != 0)
  8717. return WC_TEST_RET_ENC_EC(ret);
  8718. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE) != 0)
  8719. return WC_TEST_RET_ENC_NC;
  8720. #endif /* HAVE_AES_DECRYPT */
  8721. (void)plain;
  8722. return 0;
  8723. }
  8724. #endif
  8725. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  8726. static wc_test_ret_t aesecb_test(void)
  8727. {
  8728. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8729. Aes *enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8730. #else
  8731. Aes enc[1];
  8732. #endif
  8733. int enc_inited = 0;
  8734. byte cipher[AES_BLOCK_SIZE * 4];
  8735. #ifdef HAVE_AES_DECRYPT
  8736. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8737. Aes *dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8738. #else
  8739. Aes dec[1];
  8740. #endif
  8741. int dec_inited = 0;
  8742. byte plain [AES_BLOCK_SIZE * 4];
  8743. #endif /* HAVE_AES_DECRYPT */
  8744. wc_test_ret_t ret = 0;
  8745. #if defined(WOLFSSL_AES_256)
  8746. {
  8747. WOLFSSL_SMALL_STACK_STATIC const byte niPlain[] =
  8748. {
  8749. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8750. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  8751. };
  8752. WOLFSSL_SMALL_STACK_STATIC const byte niCipher[] =
  8753. {
  8754. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  8755. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  8756. };
  8757. WOLFSSL_SMALL_STACK_STATIC const byte niKey[] =
  8758. {
  8759. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  8760. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  8761. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  8762. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  8763. };
  8764. ret = wc_AesInit(enc, HEAP_HINT, devId);
  8765. if (ret != 0)
  8766. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8767. enc_inited = 1;
  8768. #if defined(HAVE_AES_DECRYPT)
  8769. ret = wc_AesInit(dec, HEAP_HINT, devId);
  8770. if (ret != 0)
  8771. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8772. dec_inited = 1;
  8773. #endif
  8774. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  8775. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  8776. if (ret != 0)
  8777. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8778. if (wc_AesEcbEncrypt(enc, cipher, niPlain, AES_BLOCK_SIZE) != 0)
  8779. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8780. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  8781. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8782. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  8783. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  8784. if (ret != 0)
  8785. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  8786. if (wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE) != 0)
  8787. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8788. wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE);
  8789. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  8790. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  8791. }
  8792. out:
  8793. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8794. if (enc) {
  8795. if (enc_inited)
  8796. wc_AesFree(enc);
  8797. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8798. }
  8799. #ifdef HAVE_AES_DECRYPT
  8800. if (dec) {
  8801. if (dec_inited)
  8802. wc_AesFree(dec);
  8803. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8804. }
  8805. #endif
  8806. #else
  8807. if (enc_inited)
  8808. wc_AesFree(enc);
  8809. #ifdef HAVE_AES_DECRYPT
  8810. if (dec_inited)
  8811. wc_AesFree(dec);
  8812. #endif
  8813. #endif
  8814. #endif /* WOLFSSL_AES_256 */
  8815. return ret;
  8816. }
  8817. #endif /* HAVE_AES_ECB */
  8818. #ifdef WOLFSSL_AES_COUNTER
  8819. static wc_test_ret_t aesctr_test(Aes* enc, Aes* dec, byte* cipher, byte* plain)
  8820. {
  8821. wc_test_ret_t ret = 0;
  8822. /* test vectors from "Recommendation for Block Cipher Modes of
  8823. * Operation" NIST Special Publication 800-38A */
  8824. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  8825. {
  8826. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  8827. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  8828. };
  8829. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  8830. {
  8831. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8832. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  8833. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  8834. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  8835. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  8836. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  8837. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  8838. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  8839. };
  8840. #ifdef WOLFSSL_ARMASM
  8841. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap32[] =
  8842. {
  8843. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,
  8844. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  8845. };
  8846. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap32_2[] =
  8847. {
  8848. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8849. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xfe
  8850. };
  8851. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap64[] =
  8852. {
  8853. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8854. 0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  8855. };
  8856. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap64_2[] =
  8857. {
  8858. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0,
  8859. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  8860. };
  8861. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap96[] =
  8862. {
  8863. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8864. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff
  8865. };
  8866. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap96_2[] =
  8867. {
  8868. 0xff,0xff,0xff,0xf0,0xff,0xff,0xff,0xff,
  8869. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  8870. };
  8871. #endif
  8872. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap128[] =
  8873. {
  8874. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8875. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  8876. };
  8877. #ifdef WOLFSSL_ARMASM
  8878. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap128_2[] =
  8879. {
  8880. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8881. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  8882. };
  8883. #endif
  8884. #ifdef WOLFSSL_AES_128
  8885. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  8886. {
  8887. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  8888. 0xc2
  8889. };
  8890. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Key[] =
  8891. {
  8892. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  8893. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  8894. };
  8895. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Cipher[] =
  8896. {
  8897. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  8898. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  8899. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  8900. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  8901. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  8902. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  8903. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  8904. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  8905. };
  8906. #ifdef WOLFSSL_ARMASM
  8907. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32Cipher[] =
  8908. {
  8909. 0xb3,0x8b,0x58,0xbc,0xce,0xf4,0x71,0x78,
  8910. 0xf6,0x7c,0xdb,0xb4,0x27,0x2b,0x0a,0xbf,
  8911. 0x7e,0xad,0xea,0x5c,0xd1
  8912. };
  8913. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32CipherLong[] =
  8914. {
  8915. 0xb3,0x8b,0x58,0xbc,0xce,0xf4,0x71,0x78,
  8916. 0xf6,0x7c,0xdb,0xb4,0x27,0x2b,0x0a,0xbf,
  8917. 0x7e,0xad,0xea,0x5c,0xd1,0xb7,0x98,0xf0,
  8918. 0x22,0x20,0xfe,0x67,0xb0,0x02,0x23,0x50
  8919. };
  8920. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32_2CipherLong[] =
  8921. {
  8922. 0x6e,0xa1,0x27,0x4d,0xea,0x20,0x5f,0x39,
  8923. 0x68,0xc8,0xb6,0x78,0xde,0xfc,0x53,0x5c,
  8924. 0x90,0xc8,0xf6,0xc6,0xfa,0xe0,0x7b,0x09,
  8925. 0x7c,0xf8,0x9c,0x6a,0x5a,0xa5,0x17,0x7f,
  8926. 0x03,0x92,0x5f,0x4e,0x85,0xea,0x26,0xc9,
  8927. 0x5a,0xc2,0x74,0xe2,0xbf,0xe4,0x1b,0xd4
  8928. };
  8929. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64Cipher[] =
  8930. {
  8931. 0xdd,0x17,0x10,0x7c,0x45,0x04,0xac,0x43,
  8932. 0xef,0xa8,0xcc,0x32,0x34,0x87,0x88,0xd7,
  8933. 0xae,0x74,0x94,0x72,0x8e
  8934. };
  8935. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64CipherLong[] =
  8936. {
  8937. 0xdd,0x17,0x10,0x7c,0x45,0x04,0xac,0x43,
  8938. 0xef,0xa8,0xcc,0x32,0x34,0x87,0x88,0xd7,
  8939. 0xae,0x74,0x94,0x72,0x8e,0xd0,0x71,0xc0,
  8940. 0x89,0x8a,0xa1,0xb0,0x29,0xa0,0x10,0x9e
  8941. };
  8942. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64_2CipherLong[] =
  8943. {
  8944. 0x3f,0xe7,0xd5,0xf3,0xfa,0x09,0xfe,0x40,
  8945. 0xa6,0xa1,0x32,0x8b,0x57,0x12,0xb9,0xfa,
  8946. 0xf2,0x2d,0xe4,0x3c,0x66,0x1d,0x0a,0x8e,
  8947. 0x46,0xf8,0x2e,0x33,0xce,0x8d,0x4e,0x3b,
  8948. 0x17,0x67,0x9e,0x9f,0x76,0x9e,0xc2,0x99,
  8949. 0xd5,0xd4,0x71,0xed,0xb4,0x33,0xb2,0xcd
  8950. };
  8951. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96Cipher[] =
  8952. {
  8953. 0x55,0x24,0xc2,0x73,0xca,0xa3,0x48,0x03,
  8954. 0x0b,0x72,0x8d,0xd7,0x6c,0x99,0x8e,0x04,
  8955. 0x9d,0x77,0xc9,0x5f,0x38
  8956. };
  8957. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96CipherLong[] =
  8958. {
  8959. 0x55,0x24,0xc2,0x73,0xca,0xa3,0x48,0x03,
  8960. 0x0b,0x72,0x8d,0xd7,0x6c,0x99,0x8e,0x04,
  8961. 0x9d,0x77,0xc9,0x5f,0x38,0xb5,0x6e,0x44,
  8962. 0x21,0x8e,0xda,0x57,0xe0,0x41,0xc7,0x6a
  8963. };
  8964. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96_2CipherLong[] =
  8965. {
  8966. 0xc8,0x81,0x1a,0xbe,0xc7,0x5b,0x93,0x6f,
  8967. 0xe6,0x52,0xe4,0xb1,0x2d,0x1c,0x39,0xbc,
  8968. 0xeb,0x82,0x27,0x0a,0x7e,0xa5,0x0e,0x2d,
  8969. 0x32,0xda,0xbe,0x10,0x7a,0x10,0xcc,0xd3,
  8970. 0x6f,0xc6,0x83,0x28,0x05,0x57,0x8a,0x24,
  8971. 0x44,0x76,0x17,0x81,0xb9,0x5c,0x94,0x81
  8972. };
  8973. #endif
  8974. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128Cipher[] =
  8975. {
  8976. 0xe1,0x33,0x38,0xe3,0x6c,0xb7,0x19,0x62,
  8977. 0xe0,0x0d,0x02,0x0b,0x4c,0xed,0xbd,0x86,
  8978. 0xd3,0xda,0xe1,0x5b,0x04
  8979. };
  8980. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128CipherLong[] =
  8981. {
  8982. 0xe1,0x33,0x38,0xe3,0x6c,0xb7,0x19,0x62,
  8983. 0xe0,0x0d,0x02,0x0b,0x4c,0xed,0xbd,0x86,
  8984. 0xd3,0xda,0xe1,0x5b,0x04,0xbb,0x35,0x2f,
  8985. 0xa0,0xf5,0x9f,0xeb,0xfc,0xb4,0xda,0x3e
  8986. };
  8987. #ifdef WOLFSSL_ARMASM
  8988. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128_2CipherLong[] =
  8989. {
  8990. 0xba,0x76,0xaa,0x54,0xd5,0xb5,0x60,0x67,
  8991. 0xc1,0xa7,0x90,0x3b,0x3f,0xdd,0xfa,0x89,
  8992. 0x24,0xdf,0x0c,0x56,0x5c,0xf4,0x2a,0x68,
  8993. 0x97,0x87,0x13,0xb6,0x7a,0xd1,0x24,0xfd,
  8994. 0x4d,0x3f,0x77,0x4a,0xb9,0xe4,0x7d,0xa2,
  8995. 0xdb,0xb9,0x31,0x5e,0xa3,0x11,0x06,0x80
  8996. };
  8997. #endif
  8998. #endif /* WOLFSSL_AES_128 */
  8999. #ifdef WOLFSSL_AES_192
  9000. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  9001. {
  9002. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  9003. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  9004. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  9005. };
  9006. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  9007. {
  9008. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  9009. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b,
  9010. 0x09,0x03,0x39,0xec,0x0a,0xa6,0xfa,0xef,
  9011. 0xd5,0xcc,0xc2,0xc6,0xf4,0xce,0x8e,0x94,
  9012. 0x1e,0x36,0xb2,0x6b,0xd1,0xeb,0xc6,0x70,
  9013. 0xd1,0xbd,0x1d,0x66,0x56,0x20,0xab,0xf7,
  9014. 0x4f,0x78,0xa7,0xf6,0xd2,0x98,0x09,0x58,
  9015. 0x5a,0x97,0xda,0xec,0x58,0xc6,0xb0,0x50
  9016. };
  9017. #ifdef WOLFSSL_ARMASM
  9018. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32Cipher[] =
  9019. {
  9020. 0x28,0xaa,0xfa,0x90,0x72,0x74,0x86,0xaf,
  9021. 0x72,0x73,0x35,0x17,0x70,0x4e,0x7d,0xca,
  9022. 0x0c,0x33,0x97,0x06,0xc0
  9023. };
  9024. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32CipherLong[] =
  9025. {
  9026. 0x28,0xaa,0xfa,0x90,0x72,0x74,0x86,0xaf,
  9027. 0x72,0x73,0x35,0x17,0x70,0x4e,0x7d,0xca,
  9028. 0x0c,0x33,0x97,0x06,0xc0,0xbe,0x83,0x87,
  9029. 0xdd,0xd3,0xff,0xd8,0xe4,0x6a,0x5b,0x84
  9030. };
  9031. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32_2CipherLong[] =
  9032. {
  9033. 0xf5,0x00,0xa2,0x91,0x54,0xa3,0x76,0xa2,
  9034. 0xdd,0xad,0x16,0x89,0xe5,0xf0,0x1d,0x40,
  9035. 0x84,0xcd,0x74,0x84,0xcb,0x8b,0x9e,0x29,
  9036. 0xa9,0x8a,0x12,0x65,0xa0,0x79,0x5e,0xce,
  9037. 0xd9,0x50,0x65,0x21,0x86,0xb0,0x85,0x0d,
  9038. 0x98,0x2d,0x9a,0x5a,0x11,0xbe,0xa5,0x7f
  9039. };
  9040. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64Cipher[] =
  9041. {
  9042. 0xfe,0x39,0x27,0x97,0xac,0xe5,0xb8,0x74,
  9043. 0xb9,0x8c,0xbf,0x58,0x71,0xa4,0x80,0x33,
  9044. 0x3d,0xf7,0xb4,0xfd,0x8c
  9045. };
  9046. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64CipherLong[] =
  9047. {
  9048. 0xfe,0x39,0x27,0x97,0xac,0xe5,0xb8,0x74,
  9049. 0xb9,0x8c,0xbf,0x58,0x71,0xa4,0x80,0x33,
  9050. 0x3d,0xf7,0xb4,0xfd,0x8c,0x55,0x47,0x10,
  9051. 0xd5,0x91,0x35,0xbe,0xd8,0x0d,0xa5,0x9e
  9052. };
  9053. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64_2CipherLong[] =
  9054. {
  9055. 0x59,0xf1,0xed,0x70,0x62,0x42,0xa8,0x06,
  9056. 0x07,0x36,0xe1,0xc5,0x04,0x79,0xc3,0x9b,
  9057. 0xd1,0x14,0x5c,0xcc,0x6f,0x81,0x5f,0x2f,
  9058. 0xa0,0xde,0xcf,0x61,0x55,0x18,0x7a,0xac,
  9059. 0xb0,0x59,0x37,0x90,0x53,0xb3,0x00,0x88,
  9060. 0xb4,0x49,0x90,0x7b,0x96,0xcd,0xcc,0xc3
  9061. };
  9062. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96Cipher[] =
  9063. {
  9064. 0x41,0x21,0x40,0x31,0xfb,0xc8,0xad,0x23,
  9065. 0xde,0x00,0x03,0xd8,0x96,0x45,0xc7,0xb5,
  9066. 0x47,0xb5,0xf3,0x30,0x3b
  9067. };
  9068. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96CipherLong[] =
  9069. {
  9070. 0x41,0x21,0x40,0x31,0xfb,0xc8,0xad,0x23,
  9071. 0xde,0x00,0x03,0xd8,0x96,0x45,0xc7,0xb5,
  9072. 0x47,0xb5,0xf3,0x30,0x3b,0xef,0xcd,0x80,
  9073. 0xe3,0x61,0x34,0xef,0x4e,0x1b,0x79,0xc1
  9074. };
  9075. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96_2CipherLong[] =
  9076. {
  9077. 0x3c,0xb2,0xff,0xc0,0x24,0xe1,0xf5,0xc4,
  9078. 0x0f,0xd1,0x0a,0x1b,0xbe,0x1f,0x23,0xa1,
  9079. 0x8e,0xbf,0x2b,0x96,0xb6,0x37,0xc8,0x25,
  9080. 0x06,0x90,0xe2,0xca,0x71,0x24,0x52,0x95,
  9081. 0xaa,0x8c,0x80,0xdf,0xb7,0xd7,0x30,0xb0,
  9082. 0xcc,0x06,0x4f,0x28,0xa2,0x74,0x27,0xf8
  9083. };
  9084. #endif
  9085. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128Cipher[] =
  9086. {
  9087. 0x5c,0xc3,0x8f,0xab,0x30,0xb6,0xac,0x67,
  9088. 0xdc,0xc2,0x1e,0x7b,0x01,0x2e,0xcf,0x98,
  9089. 0x8c,0x68,0xa7,0xd9,0x57
  9090. };
  9091. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128CipherLong[] =
  9092. {
  9093. 0x5c,0xc3,0x8f,0xab,0x30,0xb6,0xac,0x67,
  9094. 0xdc,0xc2,0x1e,0x7b,0x01,0x2e,0xcf,0x98,
  9095. 0x8c,0x68,0xa7,0xd9,0x57,0xab,0x09,0x0f,
  9096. 0x01,0xc4,0x4e,0x62,0xaf,0xc2,0xdf,0x1a
  9097. };
  9098. #ifdef WOLFSSL_ARMASM
  9099. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128_2CipherLong[] =
  9100. {
  9101. 0x88,0x0a,0x26,0x4e,0xa8,0x26,0x21,0xe0,
  9102. 0xfc,0xbc,0x63,0xdc,0xd9,0x60,0x52,0xb2,
  9103. 0x99,0x2f,0xbb,0x1e,0x00,0xf5,0x9f,0x6d,
  9104. 0xab,0x48,0x0f,0xc6,0x37,0x12,0x56,0xe3,
  9105. 0x12,0x8d,0x31,0xc8,0xea,0xf4,0x41,0x82,
  9106. 0x7a,0x88,0xe0,0xd7,0xf0,0x67,0x03,0xa4
  9107. };
  9108. #endif
  9109. #endif
  9110. #ifdef WOLFSSL_AES_256
  9111. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  9112. {
  9113. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  9114. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  9115. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  9116. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  9117. };
  9118. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  9119. {
  9120. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  9121. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28,
  9122. 0xf4,0x43,0xe3,0xca,0x4d,0x62,0xb5,0x9a,
  9123. 0xca,0x84,0xe9,0x90,0xca,0xca,0xf5,0xc5,
  9124. 0x2b,0x09,0x30,0xda,0xa2,0x3d,0xe9,0x4c,
  9125. 0xe8,0x70,0x17,0xba,0x2d,0x84,0x98,0x8d,
  9126. 0xdf,0xc9,0xc5,0x8d,0xb6,0x7a,0xad,0xa6,
  9127. 0x13,0xc2,0xdd,0x08,0x45,0x79,0x41,0xa6
  9128. };
  9129. #ifdef WOLFSSL_ARMASM
  9130. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32Cipher[] =
  9131. {
  9132. 0xb0,0xa8,0xc0,0x65,0x85,0x20,0x0d,0x5c,
  9133. 0x25,0xcf,0xe7,0x58,0x63,0xc8,0xd4,0xea,
  9134. 0xa2,0x13,0x47,0x74,0xda
  9135. };
  9136. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32CipherLong[] =
  9137. {
  9138. 0xb0,0xa8,0xc0,0x65,0x85,0x20,0x0d,0x5c,
  9139. 0x25,0xcf,0xe7,0x58,0x63,0xc8,0xd4,0xea,
  9140. 0xa2,0x13,0x47,0x74,0xda,0x89,0x77,0x40,
  9141. 0x28,0x9c,0xe8,0x19,0x26,0x32,0xd8,0x1f
  9142. };
  9143. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32_2CipherLong[] =
  9144. {
  9145. 0xf6,0xd9,0x22,0xc6,0x80,0x29,0xaf,0x14,
  9146. 0x54,0x6c,0x0a,0xce,0x42,0xea,0x3c,0xa1,
  9147. 0x7c,0xeb,0x36,0x0d,0x8e,0xd7,0x8c,0x59,
  9148. 0xa8,0x09,0x9f,0x9e,0xba,0x5b,0x95,0xfa,
  9149. 0x26,0x8c,0x37,0x59,0xf8,0xae,0x8e,0xaa,
  9150. 0x4d,0xe4,0x1c,0xfe,0x51,0xc7,0xb7,0xcc
  9151. };
  9152. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64Cipher[] =
  9153. {
  9154. 0x74,0x1a,0x52,0x41,0x76,0xb4,0x11,0x8f,
  9155. 0xfd,0x57,0x31,0xfd,0x3d,0x76,0x8f,0x07,
  9156. 0xd4,0x94,0x4c,0xcd,0x4d
  9157. };
  9158. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64CipherLong[] =
  9159. {
  9160. 0x74,0x1a,0x52,0x41,0x76,0xb4,0x11,0x8f,
  9161. 0xfd,0x57,0x31,0xfd,0x3d,0x76,0x8f,0x07,
  9162. 0xd4,0x94,0x4c,0xcd,0x4d,0x47,0x5a,0x92,
  9163. 0x26,0x49,0x81,0x7a,0xda,0x36,0x27,0x01
  9164. };
  9165. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64_2CipherLong[] =
  9166. {
  9167. 0xf7,0x9c,0xbf,0xf6,0xa2,0xaa,0x8a,0x0a,
  9168. 0x63,0x8a,0x20,0x2f,0x12,0xf1,0x8e,0x49,
  9169. 0x30,0xc0,0x8d,0x5c,0x5f,0x8b,0xbc,0x16,
  9170. 0xdd,0x71,0xee,0x13,0x14,0x7b,0xe1,0x25,
  9171. 0xcb,0x87,0x8a,0xc6,0xdc,0x1d,0x54,0x7a,
  9172. 0xe1,0xe4,0x6f,0x0d,0x95,0x1b,0xd1,0x8b
  9173. };
  9174. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96Cipher[] =
  9175. {
  9176. 0xb9,0x07,0x02,0xb8,0xbe,0x94,0xbf,0x53,
  9177. 0xdf,0x83,0x8e,0x23,0x8c,0x67,0x0c,0x81,
  9178. 0xb8,0x69,0xa1,0x48,0x45
  9179. };
  9180. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96CipherLong[] =
  9181. {
  9182. 0xb9,0x07,0x02,0xb8,0xbe,0x94,0xbf,0x53,
  9183. 0xdf,0x83,0x8e,0x23,0x8c,0x67,0x0c,0x81,
  9184. 0xb8,0x69,0xa1,0x48,0x45,0xf1,0xc6,0x27,
  9185. 0x36,0xa8,0xb2,0x4b,0x0e,0x62,0x6b,0x72
  9186. };
  9187. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96_2CipherLong[] =
  9188. {
  9189. 0xd5,0x56,0x73,0xaa,0xb8,0xe4,0x06,0xf6,
  9190. 0x83,0x45,0x3a,0xb4,0xb9,0x63,0xec,0xad,
  9191. 0x73,0xc5,0xab,0x78,0xb1,0x21,0xab,0xef,
  9192. 0x69,0x15,0xb7,0x0c,0xe9,0xb4,0x3a,0xe7,
  9193. 0xbc,0xc4,0x22,0xbd,0x93,0xba,0x52,0xe0,
  9194. 0x91,0x2f,0x5e,0x8d,0x6d,0x59,0xf7,0xc2
  9195. };
  9196. #endif
  9197. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128Cipher[] =
  9198. {
  9199. 0x50,0xfd,0x97,0xc3,0xe6,0x1a,0xbb,0x48,
  9200. 0x73,0xfb,0x78,0xdf,0x1e,0x8e,0x77,0xe6,
  9201. 0x4b,0x45,0x7c,0xd6,0x8a
  9202. };
  9203. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128CipherLong[] =
  9204. {
  9205. 0x50,0xfd,0x97,0xc3,0xe6,0x1a,0xbb,0x48,
  9206. 0x73,0xfb,0x78,0xdf,0x1e,0x8e,0x77,0xe6,
  9207. 0x4b,0x45,0x7c,0xd6,0x8a,0xcc,0xda,0x4a,
  9208. 0x89,0xfa,0x23,0x6c,0x06,0xbf,0x26,0x05
  9209. };
  9210. #ifdef WOLFSSL_ARMASM
  9211. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128_2CipherLong[] =
  9212. {
  9213. 0x24,0x5c,0x09,0xa0,0x3b,0x1a,0x5a,0x94,
  9214. 0x2b,0x93,0x56,0x13,0x48,0xa0,0x21,0xce,
  9215. 0x95,0x11,0xa3,0x76,0xd6,0x59,0x88,0x42,
  9216. 0x04,0x71,0x69,0x62,0x28,0xb2,0xee,0x9d,
  9217. 0xd5,0xa0,0xea,0xc7,0x37,0x93,0x92,0xc7,
  9218. 0xf2,0xb6,0x8d,0xd9,0x59,0x1a,0xfa,0xbb
  9219. };
  9220. #endif
  9221. #endif
  9222. int i;
  9223. struct {
  9224. const byte* key;
  9225. int keySz;
  9226. const byte* iv;
  9227. const byte* plain;
  9228. int len;
  9229. const byte* cipher;
  9230. } testVec[] = {
  9231. #ifdef WOLFSSL_AES_128
  9232. { ctr128Key, (int)sizeof(ctr128Key), ctrIv,
  9233. ctrPlain, (int)sizeof(ctrPlain), ctr128Cipher },
  9234. /* let's try with just 9 bytes, non block size test */
  9235. { ctr128Key, (int)sizeof(ctr128Key), ctrIv,
  9236. ctrPlain, (int)sizeof(oddCipher), ctr128Cipher },
  9237. /* and an additional 9 bytes to reuse tmp left buffer */
  9238. { NULL, 0, NULL, ctrPlain, (int)sizeof(oddCipher), oddCipher },
  9239. /* Counter wrapping */
  9240. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128,
  9241. ctrPlain, (int)sizeof(ctr128Wrap128Cipher), ctr128Wrap128Cipher },
  9242. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128,
  9243. ctrPlain, (int)sizeof(ctr128Wrap128CipherLong),
  9244. ctr128Wrap128CipherLong },
  9245. #ifdef WOLFSSL_ARMASM
  9246. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128_2,
  9247. ctrPlain, (int)sizeof(ctr128Wrap128_2CipherLong),
  9248. ctr128Wrap128_2CipherLong },
  9249. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96,
  9250. ctrPlain, (int)sizeof(ctr128Wrap96Cipher), ctr128Wrap96Cipher },
  9251. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96,
  9252. ctrPlain, (int)sizeof(ctr128Wrap96CipherLong),
  9253. ctr128Wrap96CipherLong },
  9254. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96_2,
  9255. ctrPlain, (int)sizeof(ctr128Wrap96_2CipherLong),
  9256. ctr128Wrap96_2CipherLong },
  9257. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64,
  9258. ctrPlain, (int)sizeof(ctr128Wrap64Cipher), ctr128Wrap64Cipher },
  9259. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64,
  9260. ctrPlain, (int)sizeof(ctr128Wrap64CipherLong),
  9261. ctr128Wrap64CipherLong },
  9262. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64_2,
  9263. ctrPlain, (int)sizeof(ctr128Wrap64_2CipherLong),
  9264. ctr128Wrap64_2CipherLong },
  9265. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32,
  9266. ctrPlain, (int)sizeof(ctr128Wrap32Cipher), ctr128Wrap32Cipher },
  9267. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32,
  9268. ctrPlain, (int)sizeof(ctr128Wrap32CipherLong),
  9269. ctr128Wrap32CipherLong },
  9270. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32_2,
  9271. ctrPlain, (int)sizeof(ctr128Wrap32_2CipherLong),
  9272. ctr128Wrap32_2CipherLong },
  9273. #endif
  9274. #endif
  9275. #ifdef WOLFSSL_AES_192
  9276. { ctr192Key, (int)sizeof(ctr192Key), ctrIv,
  9277. ctrPlain, (int)sizeof(ctrPlain), ctr192Cipher },
  9278. /* let's try with just 9 bytes, non block size test */
  9279. { ctr192Key, (int)sizeof(ctr192Key), ctrIv,
  9280. ctrPlain, (int)sizeof(oddCipher), ctr192Cipher },
  9281. /* Counter wrapping */
  9282. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128,
  9283. ctrPlain, (int)sizeof(ctr192Wrap128Cipher), ctr192Wrap128Cipher },
  9284. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128,
  9285. ctrPlain, (int)sizeof(ctr192Wrap128CipherLong),
  9286. ctr192Wrap128CipherLong },
  9287. #ifdef WOLFSSL_ARMASM
  9288. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128_2,
  9289. ctrPlain, (int)sizeof(ctr192Wrap128_2CipherLong),
  9290. ctr192Wrap128_2CipherLong },
  9291. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96,
  9292. ctrPlain, (int)sizeof(ctr192Wrap96Cipher), ctr192Wrap96Cipher },
  9293. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96,
  9294. ctrPlain, (int)sizeof(ctr192Wrap96CipherLong),
  9295. ctr192Wrap96CipherLong },
  9296. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96_2,
  9297. ctrPlain, (int)sizeof(ctr192Wrap96_2CipherLong),
  9298. ctr192Wrap96_2CipherLong },
  9299. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64,
  9300. ctrPlain, (int)sizeof(ctr192Wrap64Cipher), ctr192Wrap64Cipher },
  9301. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64,
  9302. ctrPlain, (int)sizeof(ctr192Wrap64CipherLong),
  9303. ctr192Wrap64CipherLong },
  9304. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64_2,
  9305. ctrPlain, (int)sizeof(ctr192Wrap64_2CipherLong),
  9306. ctr192Wrap64_2CipherLong },
  9307. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32,
  9308. ctrPlain, (int)sizeof(ctr192Wrap32Cipher), ctr192Wrap32Cipher },
  9309. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32,
  9310. ctrPlain, (int)sizeof(ctr192Wrap32CipherLong),
  9311. ctr192Wrap32CipherLong },
  9312. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32_2,
  9313. ctrPlain, (int)sizeof(ctr192Wrap32_2CipherLong),
  9314. ctr192Wrap32_2CipherLong },
  9315. #endif
  9316. #endif
  9317. #ifdef WOLFSSL_AES_256
  9318. { ctr256Key, (int)sizeof(ctr256Key), ctrIv,
  9319. ctrPlain, (int)sizeof(ctrPlain), ctr256Cipher },
  9320. /* let's try with just 9 bytes, non block size test */
  9321. { ctr256Key, (int)sizeof(ctr256Key), ctrIv,
  9322. ctrPlain, (int)sizeof(oddCipher), ctr256Cipher },
  9323. /* Counter wrapping */
  9324. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128,
  9325. ctrPlain, (int)sizeof(ctr256Wrap128Cipher), ctr256Wrap128Cipher },
  9326. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128,
  9327. ctrPlain, (int)sizeof(ctr256Wrap128CipherLong),
  9328. ctr256Wrap128CipherLong },
  9329. #ifdef WOLFSSL_ARMASM
  9330. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128_2,
  9331. ctrPlain, (int)sizeof(ctr256Wrap128_2CipherLong),
  9332. ctr256Wrap128_2CipherLong },
  9333. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96,
  9334. ctrPlain, (int)sizeof(ctr256Wrap96Cipher), ctr256Wrap96Cipher },
  9335. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96,
  9336. ctrPlain, (int)sizeof(ctr256Wrap96CipherLong),
  9337. ctr256Wrap96CipherLong },
  9338. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96_2,
  9339. ctrPlain, (int)sizeof(ctr256Wrap96_2CipherLong),
  9340. ctr256Wrap96_2CipherLong },
  9341. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64,
  9342. ctrPlain, (int)sizeof(ctr256Wrap64Cipher), ctr256Wrap64Cipher },
  9343. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64,
  9344. ctrPlain, (int)sizeof(ctr256Wrap64CipherLong),
  9345. ctr256Wrap64CipherLong },
  9346. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64_2,
  9347. ctrPlain, (int)sizeof(ctr256Wrap64_2CipherLong),
  9348. ctr256Wrap64_2CipherLong },
  9349. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32,
  9350. ctrPlain, (int)sizeof(ctr256Wrap32Cipher), ctr256Wrap32Cipher },
  9351. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32,
  9352. ctrPlain, (int)sizeof(ctr256Wrap32CipherLong),
  9353. ctr256Wrap32CipherLong },
  9354. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32_2,
  9355. ctrPlain, (int)sizeof(ctr256Wrap32_2CipherLong),
  9356. ctr256Wrap32_2CipherLong },
  9357. #endif
  9358. #endif
  9359. };
  9360. #define AES_CTR_TEST_LEN (int)(sizeof(testVec) / sizeof(*testVec))
  9361. for (i = 0; i < AES_CTR_TEST_LEN; i++) {
  9362. if (testVec[i].key != NULL) {
  9363. ret = wc_AesSetKeyDirect(enc, testVec[i].key, testVec[i].keySz,
  9364. testVec[i].iv, AES_ENCRYPTION);
  9365. if (ret != 0) {
  9366. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  9367. }
  9368. /* Ctr only uses encrypt, even on key setup */
  9369. ret = wc_AesSetKeyDirect(dec, testVec[i].key, testVec[i].keySz,
  9370. testVec[i].iv, AES_ENCRYPTION);
  9371. if (ret != 0) {
  9372. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  9373. }
  9374. }
  9375. ret = wc_AesCtrEncrypt(enc, cipher, testVec[i].plain, testVec[i].len);
  9376. if (ret != 0) {
  9377. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  9378. }
  9379. ret = wc_AesCtrEncrypt(dec, plain, cipher, testVec[i].len);
  9380. if (ret != 0) {
  9381. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  9382. }
  9383. if (XMEMCMP(plain, ctrPlain, testVec[i].len)) {
  9384. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  9385. }
  9386. #if !(FIPS_VERSION_EQ(2,0) && defined(WOLFSSL_ARMASM))
  9387. if (XMEMCMP(cipher, testVec[i].cipher, testVec[i].len)) {
  9388. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  9389. }
  9390. #endif
  9391. }
  9392. out:
  9393. return ret;
  9394. }
  9395. #endif /* WOLFSSL_AES_COUNTER */
  9396. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_test(void)
  9397. {
  9398. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  9399. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9400. Aes *enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9401. #else
  9402. Aes enc[1];
  9403. #endif
  9404. int enc_inited = 0;
  9405. byte cipher[AES_BLOCK_SIZE * 4];
  9406. #ifdef HAVE_AES_DECRYPT
  9407. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9408. Aes *dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9409. #else
  9410. Aes dec[1];
  9411. #endif
  9412. int dec_inited = 0;
  9413. byte plain [AES_BLOCK_SIZE * 4];
  9414. #endif /* HAVE_AES_DECRYPT */
  9415. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER || WOLFSSL_AES_DIRECT */
  9416. wc_test_ret_t ret = 0;
  9417. #ifdef HAVE_AES_CBC
  9418. #ifdef WOLFSSL_AES_128
  9419. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  9420. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  9421. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  9422. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  9423. };
  9424. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  9425. {
  9426. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  9427. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  9428. };
  9429. WOLFSSL_SMALL_STACK_STATIC const byte key[] = "0123456789abcdef "; /* align */
  9430. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = "1234567890abcdef "; /* align */
  9431. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9432. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  9433. if (enc == NULL)
  9434. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  9435. #endif
  9436. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  9437. if (dec == NULL)
  9438. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  9439. #endif
  9440. #endif
  9441. ret = wc_AesInit(enc, HEAP_HINT, devId);
  9442. if (ret != 0)
  9443. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9444. enc_inited = 1;
  9445. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  9446. ret = wc_AesInit(dec, HEAP_HINT, devId);
  9447. if (ret != 0)
  9448. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9449. dec_inited = 1;
  9450. #endif
  9451. ret = wc_AesSetKey(enc, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  9452. if (ret != 0)
  9453. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9454. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  9455. ret = wc_AesSetKey(dec, key, AES_BLOCK_SIZE, iv, AES_DECRYPTION);
  9456. if (ret != 0)
  9457. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9458. #endif
  9459. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 4);
  9460. ret = wc_AesCbcEncrypt(enc, cipher, msg, AES_BLOCK_SIZE);
  9461. #if defined(WOLFSSL_ASYNC_CRYPT)
  9462. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9463. #endif
  9464. if (ret != 0)
  9465. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9466. #ifdef HAVE_AES_DECRYPT
  9467. XMEMSET(plain, 0, AES_BLOCK_SIZE * 4);
  9468. ret = wc_AesCbcDecrypt(dec, plain, cipher, AES_BLOCK_SIZE);
  9469. #if defined(WOLFSSL_ASYNC_CRYPT)
  9470. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9471. #endif
  9472. if (ret != 0)
  9473. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9474. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  9475. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9476. #endif /* HAVE_AES_DECRYPT */
  9477. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  9478. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9479. #endif /* WOLFSSL_AES_128 */
  9480. #if defined(WOLFSSL_AESNI) && defined(HAVE_AES_DECRYPT)
  9481. {
  9482. WOLFSSL_SMALL_STACK_STATIC const byte bigMsg[] = {
  9483. /* "All work and no play makes Jack a dull boy. " */
  9484. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  9485. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  9486. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  9487. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  9488. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  9489. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  9490. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  9491. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  9492. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  9493. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  9494. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  9495. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  9496. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  9497. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  9498. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  9499. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  9500. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  9501. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  9502. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  9503. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  9504. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  9505. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  9506. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  9507. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  9508. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  9509. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  9510. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  9511. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  9512. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  9513. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  9514. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  9515. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  9516. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  9517. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  9518. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  9519. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  9520. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  9521. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  9522. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  9523. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  9524. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  9525. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  9526. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  9527. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  9528. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  9529. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  9530. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  9531. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20
  9532. };
  9533. WOLFSSL_SMALL_STACK_STATIC const byte bigKey[] = "0123456789abcdeffedcba9876543210";
  9534. word32 keySz, msgSz;
  9535. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9536. byte *bigCipher = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9537. byte *bigPlain = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9538. if ((bigCipher == NULL) ||
  9539. (bigPlain == NULL)) {
  9540. if (bigCipher != NULL)
  9541. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9542. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9543. }
  9544. #else
  9545. byte bigCipher[sizeof(bigMsg)];
  9546. byte bigPlain[sizeof(bigMsg)];
  9547. #endif
  9548. /* Iterate from one AES_BLOCK_SIZE of bigMsg through the whole
  9549. * message by AES_BLOCK_SIZE for each size of AES key. */
  9550. for (keySz = 16; keySz <= 32; keySz += 8) {
  9551. for (msgSz = AES_BLOCK_SIZE;
  9552. msgSz <= sizeof(bigMsg);
  9553. msgSz += AES_BLOCK_SIZE) {
  9554. XMEMSET(bigCipher, 0, sizeof(bigMsg));
  9555. XMEMSET(bigPlain, 0, sizeof(bigMsg));
  9556. ret = wc_AesSetKey(enc, bigKey, keySz, iv, AES_ENCRYPTION);
  9557. if (ret != 0) {
  9558. ret = WC_TEST_RET_ENC_EC(ret);
  9559. break;
  9560. }
  9561. ret = wc_AesSetKey(dec, bigKey, keySz, iv, AES_DECRYPTION);
  9562. if (ret != 0) {
  9563. ret = WC_TEST_RET_ENC_EC(ret);
  9564. break;
  9565. }
  9566. ret = wc_AesCbcEncrypt(enc, bigCipher, bigMsg, msgSz);
  9567. #if defined(WOLFSSL_ASYNC_CRYPT)
  9568. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9569. #endif
  9570. if (ret != 0) {
  9571. ret = WC_TEST_RET_ENC_EC(ret);
  9572. break;
  9573. }
  9574. ret = wc_AesCbcDecrypt(dec, bigPlain, bigCipher, msgSz);
  9575. #if defined(WOLFSSL_ASYNC_CRYPT)
  9576. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9577. #endif
  9578. if (ret != 0) {
  9579. ret = WC_TEST_RET_ENC_EC(ret);
  9580. break;
  9581. }
  9582. if (XMEMCMP(bigPlain, bigMsg, msgSz)) {
  9583. ret = WC_TEST_RET_ENC_NC;
  9584. break;
  9585. }
  9586. }
  9587. if (ret != 0)
  9588. break;
  9589. }
  9590. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9591. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9592. XFREE(bigPlain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9593. #endif
  9594. if (ret != 0)
  9595. goto out;
  9596. }
  9597. #endif /* WOLFSSL_AESNI && HAVE_AES_DECRYPT */
  9598. /* Test of AES IV state with encrypt/decrypt */
  9599. #ifdef WOLFSSL_AES_128
  9600. {
  9601. /* Test Vector from "NIST Special Publication 800-38A, 2001 Edition"
  9602. * https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38a.pdf
  9603. */
  9604. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  9605. {
  9606. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  9607. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  9608. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  9609. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51
  9610. };
  9611. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] =
  9612. {
  9613. 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46,
  9614. 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
  9615. 0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee,
  9616. 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2
  9617. };
  9618. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  9619. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  9620. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  9621. };
  9622. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  9623. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9624. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  9625. };
  9626. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  9627. if (ret != 0)
  9628. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9629. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 2);
  9630. ret = wc_AesCbcEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE);
  9631. #if defined(WOLFSSL_ASYNC_CRYPT)
  9632. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9633. #endif
  9634. if (ret != 0)
  9635. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9636. if (XMEMCMP(cipher, verify2, AES_BLOCK_SIZE))
  9637. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9638. ret = wc_AesCbcEncrypt(enc, cipher + AES_BLOCK_SIZE,
  9639. msg2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  9640. #if defined(WOLFSSL_ASYNC_CRYPT)
  9641. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9642. #endif
  9643. if (ret != 0)
  9644. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9645. if (XMEMCMP(cipher + AES_BLOCK_SIZE, verify2 + AES_BLOCK_SIZE,
  9646. AES_BLOCK_SIZE))
  9647. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9648. #if defined(HAVE_AES_DECRYPT)
  9649. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_DECRYPTION);
  9650. if (ret != 0)
  9651. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9652. XMEMSET(plain, 0, AES_BLOCK_SIZE * 2);
  9653. ret = wc_AesCbcDecrypt(dec, plain, verify2, AES_BLOCK_SIZE);
  9654. #if defined(WOLFSSL_ASYNC_CRYPT)
  9655. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9656. #endif
  9657. if (ret != 0)
  9658. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9659. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE))
  9660. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9661. ret = wc_AesCbcDecrypt(dec, plain + AES_BLOCK_SIZE,
  9662. verify2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  9663. #if defined(WOLFSSL_ASYNC_CRYPT)
  9664. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9665. #endif
  9666. if (ret != 0)
  9667. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9668. if (XMEMCMP(plain + AES_BLOCK_SIZE, msg2 + AES_BLOCK_SIZE,
  9669. AES_BLOCK_SIZE))
  9670. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9671. #endif /* HAVE_AES_DECRYPT */
  9672. }
  9673. #endif /* WOLFSSL_AES_128 */
  9674. #endif /* HAVE_AES_CBC */
  9675. #ifdef WOLFSSL_AES_COUNTER
  9676. ret = aesctr_test(enc, dec, cipher, plain);
  9677. if (ret != 0)
  9678. return ret;
  9679. #endif
  9680. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  9681. {
  9682. WOLFSSL_SMALL_STACK_STATIC const byte niPlain[] =
  9683. {
  9684. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  9685. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  9686. };
  9687. WOLFSSL_SMALL_STACK_STATIC const byte niCipher[] =
  9688. {
  9689. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  9690. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  9691. };
  9692. WOLFSSL_SMALL_STACK_STATIC const byte niKey[] =
  9693. {
  9694. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  9695. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  9696. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  9697. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  9698. };
  9699. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  9700. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  9701. if (ret != 0)
  9702. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9703. #if !defined(HAVE_SELFTEST) && \
  9704. (defined(WOLFSSL_LINUXKM) || \
  9705. !defined(HAVE_FIPS) || \
  9706. (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  9707. ret = wc_AesEncryptDirect(enc, cipher, niPlain);
  9708. if (ret != 0)
  9709. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9710. #else
  9711. wc_AesEncryptDirect(enc, cipher, niPlain);
  9712. #endif
  9713. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  9714. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9715. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  9716. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  9717. if (ret != 0)
  9718. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9719. #if !defined(HAVE_SELFTEST) && \
  9720. (defined(WOLFSSL_LINUXKM) || \
  9721. !defined(HAVE_FIPS) || \
  9722. (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  9723. ret = wc_AesDecryptDirect(dec, plain, niCipher);
  9724. if (ret != 0)
  9725. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9726. #else
  9727. wc_AesDecryptDirect(dec, plain, niCipher);
  9728. #endif
  9729. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  9730. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9731. }
  9732. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  9733. ret = aes_key_size_test();
  9734. if (ret != 0)
  9735. goto out;
  9736. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  9737. ret = aes_cbc_test();
  9738. if (ret != 0)
  9739. goto out;
  9740. #endif
  9741. #if defined(WOLFSSL_AES_XTS)
  9742. #ifdef WOLFSSL_AES_128
  9743. ret = aes_xts_128_test();
  9744. if (ret != 0)
  9745. goto out;
  9746. #endif
  9747. #ifdef WOLFSSL_AES_256
  9748. ret = aes_xts_256_test();
  9749. if (ret != 0)
  9750. goto out;
  9751. #endif
  9752. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  9753. ret = aes_xts_sector_test();
  9754. if (ret != 0)
  9755. goto out;
  9756. #endif
  9757. #ifdef WOLFSSL_AES_128
  9758. ret = aes_xts_args_test();
  9759. if (ret != 0)
  9760. goto out;
  9761. #endif
  9762. #endif
  9763. #if defined(WOLFSSL_AES_CFB)
  9764. ret = aescfb_test();
  9765. if (ret != 0)
  9766. goto out;
  9767. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  9768. ret = aescfb1_test();
  9769. if (ret != 0)
  9770. goto out;
  9771. ret = aescfb8_test();
  9772. if (ret != 0)
  9773. goto out;
  9774. #endif
  9775. #endif
  9776. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9777. ret = aesecb_test();
  9778. if (ret != 0)
  9779. goto out;
  9780. #endif
  9781. out:
  9782. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  9783. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9784. if (enc) {
  9785. if (enc_inited)
  9786. wc_AesFree(enc);
  9787. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9788. }
  9789. #else
  9790. if (enc_inited)
  9791. wc_AesFree(enc);
  9792. #endif
  9793. (void)cipher;
  9794. #ifdef HAVE_AES_DECRYPT
  9795. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9796. if (dec) {
  9797. if (dec_inited)
  9798. wc_AesFree(dec);
  9799. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9800. }
  9801. #else
  9802. if (dec_inited)
  9803. wc_AesFree(dec);
  9804. #endif
  9805. (void)plain;
  9806. #endif /* HAVE_AES_DECRYPT */
  9807. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER || WOLFSSL_AES_DIRECT */
  9808. return ret;
  9809. }
  9810. #ifdef WOLFSSL_AES_192
  9811. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes192_test(void)
  9812. {
  9813. #ifdef HAVE_AES_CBC
  9814. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9815. Aes *enc = NULL;
  9816. #else
  9817. Aes enc[1];
  9818. #endif
  9819. byte cipher[AES_BLOCK_SIZE];
  9820. #ifdef HAVE_AES_DECRYPT
  9821. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9822. Aes *dec = NULL;
  9823. #else
  9824. Aes dec[1];
  9825. #endif
  9826. byte plain[AES_BLOCK_SIZE];
  9827. #endif
  9828. #endif /* HAVE_AES_CBC */
  9829. wc_test_ret_t ret = 0;
  9830. #ifdef HAVE_AES_CBC
  9831. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  9832. * Appendix F.2.3 */
  9833. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  9834. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  9835. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  9836. };
  9837. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  9838. {
  9839. 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  9840. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8
  9841. };
  9842. WOLFSSL_SMALL_STACK_STATIC byte key[] = {
  9843. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  9844. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  9845. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  9846. };
  9847. WOLFSSL_SMALL_STACK_STATIC byte iv[] = {
  9848. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  9849. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  9850. };
  9851. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9852. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9853. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  9854. #ifdef HAVE_AES_DECRYPT
  9855. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9856. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  9857. #endif
  9858. #endif
  9859. ret = wc_AesInit(enc, HEAP_HINT, devId);
  9860. if (ret != 0)
  9861. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9862. #ifdef HAVE_AES_DECRYPT
  9863. ret = wc_AesInit(dec, HEAP_HINT, devId);
  9864. if (ret != 0)
  9865. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9866. #endif
  9867. ret = wc_AesSetKey(enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  9868. if (ret != 0)
  9869. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9870. #ifdef HAVE_AES_DECRYPT
  9871. ret = wc_AesSetKey(dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  9872. if (ret != 0)
  9873. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9874. #endif
  9875. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  9876. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  9877. #if defined(WOLFSSL_ASYNC_CRYPT)
  9878. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9879. #endif
  9880. if (ret != 0)
  9881. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9882. #ifdef HAVE_AES_DECRYPT
  9883. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  9884. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  9885. #if defined(WOLFSSL_ASYNC_CRYPT)
  9886. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9887. #endif
  9888. if (ret != 0)
  9889. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9890. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  9891. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9892. }
  9893. #endif
  9894. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  9895. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9896. wc_AesFree(enc);
  9897. #ifdef HAVE_AES_DECRYPT
  9898. wc_AesFree(dec);
  9899. #endif
  9900. out:
  9901. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9902. if (enc)
  9903. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9904. #ifdef HAVE_AES_DECRYPT
  9905. if (dec)
  9906. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9907. #endif
  9908. #endif
  9909. #endif /* HAVE_AES_CBC */
  9910. return ret;
  9911. }
  9912. #endif /* WOLFSSL_AES_192 */
  9913. #ifdef WOLFSSL_AES_256
  9914. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes256_test(void)
  9915. {
  9916. #ifdef HAVE_AES_CBC
  9917. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9918. Aes *enc = NULL;
  9919. #else
  9920. Aes enc[1];
  9921. #endif
  9922. byte cipher[AES_BLOCK_SIZE];
  9923. #ifdef HAVE_AES_DECRYPT
  9924. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9925. Aes *dec = NULL;
  9926. #else
  9927. Aes dec[1];
  9928. #endif
  9929. byte plain[AES_BLOCK_SIZE];
  9930. #endif
  9931. #endif /* HAVE_AES_CBC */
  9932. wc_test_ret_t ret = 0;
  9933. #ifdef HAVE_AES_CBC
  9934. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  9935. * Appendix F.2.5 */
  9936. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  9937. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  9938. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  9939. };
  9940. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  9941. {
  9942. 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  9943. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6
  9944. };
  9945. WOLFSSL_SMALL_STACK_STATIC byte key[] = {
  9946. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  9947. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  9948. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  9949. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  9950. };
  9951. WOLFSSL_SMALL_STACK_STATIC byte iv[] = {
  9952. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  9953. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  9954. };
  9955. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9956. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9957. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  9958. #ifdef HAVE_AES_DECRYPT
  9959. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9960. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  9961. #endif
  9962. #endif
  9963. ret = wc_AesInit(enc, HEAP_HINT, devId);
  9964. if (ret != 0)
  9965. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9966. #ifdef HAVE_AES_DECRYPT
  9967. ret = wc_AesInit(dec, HEAP_HINT, devId);
  9968. if (ret != 0)
  9969. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9970. #endif
  9971. ret = wc_AesSetKey(enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  9972. if (ret != 0)
  9973. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9974. #ifdef HAVE_AES_DECRYPT
  9975. ret = wc_AesSetKey(dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  9976. if (ret != 0)
  9977. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9978. #endif
  9979. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  9980. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  9981. #if defined(WOLFSSL_ASYNC_CRYPT)
  9982. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9983. #endif
  9984. if (ret != 0)
  9985. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9986. #ifdef HAVE_AES_DECRYPT
  9987. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  9988. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  9989. #if defined(WOLFSSL_ASYNC_CRYPT)
  9990. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9991. #endif
  9992. if (ret != 0)
  9993. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  9994. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  9995. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  9996. }
  9997. #endif
  9998. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  9999. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10000. wc_AesFree(enc);
  10001. #ifdef HAVE_AES_DECRYPT
  10002. wc_AesFree(dec);
  10003. #endif
  10004. out:
  10005. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10006. if (enc)
  10007. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  10008. #ifdef HAVE_AES_DECRYPT
  10009. if (dec)
  10010. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  10011. #endif
  10012. #endif
  10013. #endif /* HAVE_AES_CBC */
  10014. return ret;
  10015. }
  10016. #endif /* WOLFSSL_AES_256 */
  10017. #ifdef HAVE_AESGCM
  10018. #ifdef WOLFSSL_AES_128
  10019. static wc_test_ret_t aesgcm_default_test_helper(byte* key, int keySz, byte* iv, int ivSz,
  10020. byte* plain, int plainSz, byte* cipher, int cipherSz,
  10021. byte* aad, int aadSz, byte* tag, int tagSz)
  10022. {
  10023. wc_test_ret_t ret;
  10024. int enc_inited = 0, dec_inited = 0;
  10025. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10026. Aes *enc = NULL;
  10027. Aes *dec = NULL;
  10028. #else
  10029. Aes enc[1];
  10030. Aes dec[1];
  10031. #endif
  10032. byte resultT[AES_BLOCK_SIZE];
  10033. byte resultP[AES_BLOCK_SIZE * 3];
  10034. byte resultC[AES_BLOCK_SIZE * 3];
  10035. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10036. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10037. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  10038. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10039. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  10040. #endif
  10041. XMEMSET(resultT, 0, sizeof(resultT));
  10042. XMEMSET(resultC, 0, sizeof(resultC));
  10043. XMEMSET(resultP, 0, sizeof(resultP));
  10044. ret = wc_AesInit(enc, HEAP_HINT, devId);
  10045. if (ret != 0)
  10046. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10047. else
  10048. enc_inited = 1;
  10049. ret = wc_AesInit(dec, HEAP_HINT, devId);
  10050. if (ret != 0)
  10051. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10052. else
  10053. dec_inited = 1;
  10054. ret = wc_AesGcmSetKey(enc, key, keySz);
  10055. if (ret != 0)
  10056. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10057. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  10058. ret = wc_AesGcmEncrypt(enc, resultC, plain, plainSz, iv, ivSz,
  10059. resultT, tagSz, aad, aadSz);
  10060. #if defined(WOLFSSL_ASYNC_CRYPT)
  10061. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10062. #endif
  10063. if (ret != 0)
  10064. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10065. if (cipher != NULL) {
  10066. if (XMEMCMP(cipher, resultC, cipherSz))
  10067. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10068. }
  10069. if (XMEMCMP(tag, resultT, tagSz))
  10070. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10071. #ifdef HAVE_AES_DECRYPT
  10072. ret = wc_AesGcmSetKey(dec, key, keySz);
  10073. if (ret != 0)
  10074. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10075. ret = wc_AesGcmDecrypt(dec, resultP, resultC, cipherSz,
  10076. iv, ivSz, resultT, tagSz, aad, aadSz);
  10077. #if defined(WOLFSSL_ASYNC_CRYPT)
  10078. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  10079. #endif
  10080. if (ret != 0)
  10081. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10082. if (plain != NULL) {
  10083. if (XMEMCMP(plain, resultP, plainSz))
  10084. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10085. }
  10086. #endif /* HAVE_AES_DECRYPT */
  10087. ret = 0;
  10088. out:
  10089. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10090. if (enc) {
  10091. if (enc_inited)
  10092. wc_AesFree(enc);
  10093. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  10094. }
  10095. if (dec) {
  10096. if (dec_inited)
  10097. wc_AesFree(dec);
  10098. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  10099. }
  10100. #else
  10101. if (enc_inited)
  10102. wc_AesFree(enc);
  10103. if (dec_inited)
  10104. wc_AesFree(dec);
  10105. #endif
  10106. return ret;
  10107. }
  10108. #endif
  10109. /* tests that only use 12 byte IV and 16 or less byte AAD
  10110. * test vectors are from NIST SP 800-38D
  10111. * https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/CAVP-TESTING-BLOCK-CIPHER-MODES*/
  10112. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesgcm_default_test(void)
  10113. {
  10114. #ifdef WOLFSSL_AES_128
  10115. byte key1[] = {
  10116. 0x29, 0x8e, 0xfa, 0x1c, 0xcf, 0x29, 0xcf, 0x62,
  10117. 0xae, 0x68, 0x24, 0xbf, 0xc1, 0x95, 0x57, 0xfc
  10118. };
  10119. byte iv1[] = {
  10120. 0x6f, 0x58, 0xa9, 0x3f, 0xe1, 0xd2, 0x07, 0xfa,
  10121. 0xe4, 0xed, 0x2f, 0x6d
  10122. };
  10123. ALIGN64 byte plain1[] = {
  10124. 0xcc, 0x38, 0xbc, 0xcd, 0x6b, 0xc5, 0x36, 0xad,
  10125. 0x91, 0x9b, 0x13, 0x95, 0xf5, 0xd6, 0x38, 0x01,
  10126. 0xf9, 0x9f, 0x80, 0x68, 0xd6, 0x5c, 0xa5, 0xac,
  10127. 0x63, 0x87, 0x2d, 0xaf, 0x16, 0xb9, 0x39, 0x01
  10128. };
  10129. byte aad1[] = {
  10130. 0x02, 0x1f, 0xaf, 0xd2, 0x38, 0x46, 0x39, 0x73,
  10131. 0xff, 0xe8, 0x02, 0x56, 0xe5, 0xb1, 0xc6, 0xb1
  10132. };
  10133. ALIGN64 byte cipher1[] = {
  10134. 0xdf, 0xce, 0x4e, 0x9c, 0xd2, 0x91, 0x10, 0x3d,
  10135. 0x7f, 0xe4, 0xe6, 0x33, 0x51, 0xd9, 0xe7, 0x9d,
  10136. 0x3d, 0xfd, 0x39, 0x1e, 0x32, 0x67, 0x10, 0x46,
  10137. 0x58, 0x21, 0x2d, 0xa9, 0x65, 0x21, 0xb7, 0xdb
  10138. };
  10139. byte tag1[] = {
  10140. 0x54, 0x24, 0x65, 0xef, 0x59, 0x93, 0x16, 0xf7,
  10141. 0x3a, 0x7a, 0x56, 0x05, 0x09, 0xa2, 0xd9, 0xf2
  10142. };
  10143. byte key2[] = {
  10144. 0x01, 0x6d, 0xbb, 0x38, 0xda, 0xa7, 0x6d, 0xfe,
  10145. 0x7d, 0xa3, 0x84, 0xeb, 0xf1, 0x24, 0x03, 0x64
  10146. };
  10147. byte iv2[] = {
  10148. 0x07, 0x93, 0xef, 0x3a, 0xda, 0x78, 0x2f, 0x78,
  10149. 0xc9, 0x8a, 0xff, 0xe3
  10150. };
  10151. ALIGN64 byte plain2[] = {
  10152. 0x4b, 0x34, 0xa9, 0xec, 0x57, 0x63, 0x52, 0x4b,
  10153. 0x19, 0x1d, 0x56, 0x16, 0xc5, 0x47, 0xf6, 0xb7
  10154. };
  10155. ALIGN64 byte cipher2[] = {
  10156. 0x60, 0x9a, 0xa3, 0xf4, 0x54, 0x1b, 0xc0, 0xfe,
  10157. 0x99, 0x31, 0xda, 0xad, 0x2e, 0xe1, 0x5d, 0x0c
  10158. };
  10159. byte tag2[] = {
  10160. 0x33, 0xaf, 0xec, 0x59, 0xc4, 0x5b, 0xaf, 0x68,
  10161. 0x9a, 0x5e, 0x1b, 0x13, 0xae, 0x42, 0x36, 0x19
  10162. };
  10163. byte key3[] = {
  10164. 0xb0, 0x1e, 0x45, 0xcc, 0x30, 0x88, 0xaa, 0xba,
  10165. 0x9f, 0xa4, 0x3d, 0x81, 0xd4, 0x81, 0x82, 0x3f
  10166. };
  10167. byte iv3[] = {
  10168. 0x5a, 0x2c, 0x4a, 0x66, 0x46, 0x87, 0x13, 0x45,
  10169. 0x6a, 0x4b, 0xd5, 0xe1
  10170. };
  10171. byte tag3[] = {
  10172. 0x01, 0x42, 0x80, 0xf9, 0x44, 0xf5, 0x3c, 0x68,
  10173. 0x11, 0x64, 0xb2, 0xff
  10174. };
  10175. wc_test_ret_t ret;
  10176. ret = aesgcm_default_test_helper(key1, sizeof(key1), iv1, sizeof(iv1),
  10177. plain1, sizeof(plain1), cipher1, sizeof(cipher1),
  10178. aad1, sizeof(aad1), tag1, sizeof(tag1));
  10179. if (ret != 0) {
  10180. return ret;
  10181. }
  10182. ret = aesgcm_default_test_helper(key2, sizeof(key2), iv2, sizeof(iv2),
  10183. plain2, sizeof(plain2), cipher2, sizeof(cipher2),
  10184. NULL, 0, tag2, sizeof(tag2));
  10185. if (ret != 0) {
  10186. return ret;
  10187. }
  10188. ret = aesgcm_default_test_helper(key3, sizeof(key3), iv3, sizeof(iv3),
  10189. NULL, 0, NULL, 0,
  10190. NULL, 0, tag3, sizeof(tag3));
  10191. if (ret != 0) {
  10192. return ret;
  10193. }
  10194. #endif
  10195. return 0;
  10196. }
  10197. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesgcm_test(void)
  10198. {
  10199. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10200. Aes *enc = NULL;
  10201. Aes *dec = NULL;
  10202. #else
  10203. Aes enc[1];
  10204. Aes dec[1];
  10205. #endif
  10206. /*
  10207. * This is Test Case 16 from the document Galois/
  10208. * Counter Mode of Operation (GCM) by McGrew and
  10209. * Viega.
  10210. */
  10211. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  10212. {
  10213. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  10214. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  10215. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  10216. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  10217. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  10218. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  10219. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  10220. 0xba, 0x63, 0x7b, 0x39
  10221. };
  10222. #if defined(WOLFSSL_AES_256) || defined(WOLFSSL_AES_192)
  10223. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  10224. {
  10225. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  10226. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  10227. 0xab, 0xad, 0xda, 0xd2
  10228. };
  10229. #endif
  10230. #ifdef WOLFSSL_AES_256
  10231. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  10232. {
  10233. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  10234. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  10235. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  10236. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08
  10237. };
  10238. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  10239. {
  10240. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
  10241. 0xde, 0xca, 0xf8, 0x88
  10242. };
  10243. #endif /* WOLFSSL_AES_256 */
  10244. #if defined(WOLFSSL_AES_256) || defined(WOLFSSL_AES_192)
  10245. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  10246. {
  10247. 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
  10248. 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
  10249. 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  10250. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
  10251. 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
  10252. 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  10253. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
  10254. 0xbc, 0xc9, 0xf6, 0x62
  10255. };
  10256. #endif /* WOLFSSL_AES_256 || WOLFSSL_AES_192 */
  10257. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  10258. {
  10259. 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68,
  10260. 0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b
  10261. };
  10262. /* FIPS, QAT and PIC32MZ HW Crypto only support 12-byte IV */
  10263. #if !defined(HAVE_FIPS) && \
  10264. !defined(WOLFSSL_PIC32MZ_CRYPT) && \
  10265. !defined(FREESCALE_LTC) && !defined(FREESCALE_MMCAU) && \
  10266. !defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  10267. !defined(WOLFSSL_SILABS_SE_ACCEL) && !defined(WOLFSSL_KCAPI_AES) && \
  10268. !(defined(WOLF_CRYPTO_CB) && \
  10269. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  10270. #define ENABLE_NON_12BYTE_IV_TEST
  10271. #ifdef WOLFSSL_AES_192
  10272. /* Test Case 12, uses same plaintext and AAD data. */
  10273. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  10274. {
  10275. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  10276. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  10277. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c
  10278. };
  10279. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  10280. {
  10281. 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5,
  10282. 0x55, 0x90, 0x9c, 0x5a, 0xff, 0x52, 0x69, 0xaa,
  10283. 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1,
  10284. 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28,
  10285. 0xc3, 0xc0, 0xc9, 0x51, 0x56, 0x80, 0x95, 0x39,
  10286. 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54,
  10287. 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57,
  10288. 0xa6, 0x37, 0xb3, 0x9b
  10289. };
  10290. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  10291. {
  10292. 0xd2, 0x7e, 0x88, 0x68, 0x1c, 0xe3, 0x24, 0x3c,
  10293. 0x48, 0x30, 0x16, 0x5a, 0x8f, 0xdc, 0xf9, 0xff,
  10294. 0x1d, 0xe9, 0xa1, 0xd8, 0xe6, 0xb4, 0x47, 0xef,
  10295. 0x6e, 0xf7, 0xb7, 0x98, 0x28, 0x66, 0x6e, 0x45,
  10296. 0x81, 0xe7, 0x90, 0x12, 0xaf, 0x34, 0xdd, 0xd9,
  10297. 0xe2, 0xf0, 0x37, 0x58, 0x9b, 0x29, 0x2d, 0xb3,
  10298. 0xe6, 0x7c, 0x03, 0x67, 0x45, 0xfa, 0x22, 0xe7,
  10299. 0xe9, 0xb7, 0x37, 0x3b
  10300. };
  10301. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  10302. {
  10303. 0xdc, 0xf5, 0x66, 0xff, 0x29, 0x1c, 0x25, 0xbb,
  10304. 0xb8, 0x56, 0x8f, 0xc3, 0xd3, 0x76, 0xa6, 0xd9
  10305. };
  10306. #endif /* WOLFSSL_AES_192 */
  10307. #ifdef WOLFSSL_AES_128
  10308. /* The following is an interesting test case from the example
  10309. * FIPS test vectors for AES-GCM. IVlen = 1 byte */
  10310. WOLFSSL_SMALL_STACK_STATIC const byte p3[] =
  10311. {
  10312. 0x57, 0xce, 0x45, 0x1f, 0xa5, 0xe2, 0x35, 0xa5,
  10313. 0x8e, 0x1a, 0xa2, 0x3b, 0x77, 0xcb, 0xaf, 0xe2
  10314. };
  10315. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  10316. {
  10317. 0xbb, 0x01, 0xd7, 0x03, 0x81, 0x1c, 0x10, 0x1a,
  10318. 0x35, 0xe0, 0xff, 0xd2, 0x91, 0xba, 0xf2, 0x4b
  10319. };
  10320. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  10321. {
  10322. 0xca
  10323. };
  10324. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  10325. {
  10326. 0x6b, 0x5f, 0xb3, 0x9d, 0xc1, 0xc5, 0x7a, 0x4f,
  10327. 0xf3, 0x51, 0x4d, 0xc2, 0xd5, 0xf0, 0xd0, 0x07
  10328. };
  10329. WOLFSSL_SMALL_STACK_STATIC const byte a3[] =
  10330. {
  10331. 0x40, 0xfc, 0xdc, 0xd7, 0x4a, 0xd7, 0x8b, 0xf1,
  10332. 0x3e, 0x7c, 0x60, 0x55, 0x50, 0x51, 0xdd, 0x54
  10333. };
  10334. WOLFSSL_SMALL_STACK_STATIC const byte t3[] =
  10335. {
  10336. 0x06, 0x90, 0xed, 0x01, 0x34, 0xdd, 0xc6, 0x95,
  10337. 0x31, 0x2e, 0x2a, 0xf9, 0x57, 0x7a, 0x1e, 0xa6
  10338. };
  10339. #endif /* WOLFSSL_AES_128 */
  10340. #ifdef WOLFSSL_AES_256
  10341. int ivlen;
  10342. #endif
  10343. #endif
  10344. byte resultT[sizeof(t1) + AES_BLOCK_SIZE];
  10345. byte resultP[sizeof(p) + AES_BLOCK_SIZE];
  10346. byte resultC[sizeof(p) + AES_BLOCK_SIZE];
  10347. wc_test_ret_t ret = 0;
  10348. #ifdef WOLFSSL_AES_256
  10349. #if !(defined(WOLF_CRYPTO_CB) && defined(HAVE_INTEL_QA_SYNC))
  10350. int alen;
  10351. #endif
  10352. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  10353. int plen;
  10354. #endif
  10355. #endif
  10356. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  10357. byte buf[sizeof(p) + AES_BLOCK_SIZE];
  10358. byte bufA[sizeof(a) + 1];
  10359. byte *large_aad = (byte*)XMALLOC((size_t)1024 + 16, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10360. #endif
  10361. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM)
  10362. #if !defined(BENCH_AESGCM_LARGE)
  10363. #define BENCH_AESGCM_LARGE 1024
  10364. #endif
  10365. #ifndef WOLFSSL_NO_MALLOC
  10366. byte *large_input = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10367. byte *large_output = (byte *)XMALLOC(BENCH_AESGCM_LARGE + AES_BLOCK_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10368. byte *large_outdec = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10369. if ((! large_input) || (! large_output) || (! large_outdec))
  10370. ERROR_OUT(MEMORY_E, out);
  10371. #else
  10372. byte large_input[BENCH_AESGCM_LARGE];
  10373. byte large_output[BENCH_AESGCM_LARGE];
  10374. byte large_outdec[BENCH_AESGCM_LARGE];
  10375. #endif
  10376. XMEMSET(large_input, 0, BENCH_AESGCM_LARGE);
  10377. XMEMSET(large_output, 0, BENCH_AESGCM_LARGE + AES_BLOCK_SIZE);
  10378. XMEMSET(large_outdec, 0, BENCH_AESGCM_LARGE);
  10379. #endif
  10380. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10381. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10382. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  10383. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10384. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  10385. #endif
  10386. XMEMSET(resultT, 0, sizeof(resultT));
  10387. XMEMSET(resultC, 0, sizeof(resultC));
  10388. XMEMSET(resultP, 0, sizeof(resultP));
  10389. ret = wc_AesInit(enc, HEAP_HINT, devId);
  10390. if (ret != 0)
  10391. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10392. ret = wc_AesInit(dec, HEAP_HINT, devId);
  10393. if (ret != 0)
  10394. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10395. #ifdef WOLFSSL_AES_256
  10396. ret = wc_AesGcmSetKey(enc, k1, sizeof(k1));
  10397. if (ret != 0)
  10398. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10399. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  10400. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  10401. resultT, sizeof(t1), a, sizeof(a));
  10402. #if defined(WOLFSSL_ASYNC_CRYPT)
  10403. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10404. #endif
  10405. if (ret != 0)
  10406. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10407. if (XMEMCMP(c1, resultC, sizeof(c1)))
  10408. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10409. if (XMEMCMP(t1, resultT, sizeof(t1)))
  10410. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10411. #ifdef HAVE_AES_DECRYPT
  10412. ret = wc_AesGcmSetKey(dec, k1, sizeof(k1));
  10413. if (ret != 0)
  10414. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10415. ret = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1),
  10416. iv1, sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  10417. #if defined(WOLFSSL_ASYNC_CRYPT)
  10418. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  10419. #endif
  10420. if (ret != 0)
  10421. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10422. if (XMEMCMP(p, resultP, sizeof(p)))
  10423. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10424. #endif /* HAVE_AES_DECRYPT */
  10425. /* Large buffer test */
  10426. #ifdef BENCH_AESGCM_LARGE
  10427. /* setup test buffer */
  10428. for (alen=0; alen<BENCH_AESGCM_LARGE; alen++)
  10429. large_input[alen] = (byte)alen;
  10430. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  10431. ret = wc_AesGcmEncrypt(enc, large_output, large_input,
  10432. BENCH_AESGCM_LARGE, iv1, sizeof(iv1),
  10433. resultT, sizeof(t1), a, sizeof(a));
  10434. #if defined(WOLFSSL_ASYNC_CRYPT)
  10435. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10436. #endif
  10437. if (ret != 0)
  10438. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10439. #ifdef HAVE_AES_DECRYPT
  10440. ret = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  10441. BENCH_AESGCM_LARGE, iv1, sizeof(iv1), resultT,
  10442. sizeof(t1), a, sizeof(a));
  10443. #if defined(WOLFSSL_ASYNC_CRYPT)
  10444. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  10445. #endif
  10446. if (ret != 0)
  10447. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10448. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  10449. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10450. #endif /* HAVE_AES_DECRYPT */
  10451. #endif /* BENCH_AESGCM_LARGE */
  10452. #if defined(ENABLE_NON_12BYTE_IV_TEST) && defined(WOLFSSL_AES_256)
  10453. /* Variable IV length test */
  10454. for (ivlen=1; ivlen<(int)sizeof(k1); ivlen++) {
  10455. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  10456. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), k1,
  10457. (word32)ivlen, resultT, sizeof(t1), a, sizeof(a));
  10458. #if defined(WOLFSSL_ASYNC_CRYPT)
  10459. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10460. #endif
  10461. if (ret != 0)
  10462. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10463. #ifdef HAVE_AES_DECRYPT
  10464. ret = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), k1,
  10465. (word32)ivlen, resultT, sizeof(t1), a, sizeof(a));
  10466. #if defined(WOLFSSL_ASYNC_CRYPT)
  10467. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  10468. #endif
  10469. if (ret != 0)
  10470. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10471. #endif /* HAVE_AES_DECRYPT */
  10472. }
  10473. #endif
  10474. #if !(defined(WOLF_CRYPTO_CB) && defined(HAVE_INTEL_QA_SYNC))
  10475. /* Variable authenticated data length test */
  10476. for (alen=0; alen<(int)sizeof(p); alen++) {
  10477. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  10478. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1,
  10479. sizeof(iv1), resultT, sizeof(t1), p, (word32)alen);
  10480. #if defined(WOLFSSL_ASYNC_CRYPT)
  10481. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10482. #endif
  10483. if (ret != 0)
  10484. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10485. #ifdef HAVE_AES_DECRYPT
  10486. ret = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), iv1,
  10487. sizeof(iv1), resultT, sizeof(t1), p, (word32)alen);
  10488. #if defined(WOLFSSL_ASYNC_CRYPT)
  10489. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  10490. #endif
  10491. if (ret != 0)
  10492. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10493. #endif /* HAVE_AES_DECRYPT */
  10494. }
  10495. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  10496. if (! large_aad)
  10497. ERROR_OUT(MEMORY_E, out);
  10498. XMEMSET(large_aad, 0, 1024+16);
  10499. /* Variable authenticated data length test */
  10500. for (alen=0; alen<=1024; alen+=16) {
  10501. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  10502. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1,
  10503. sizeof(iv1), resultT, sizeof(t1), large_aad, (word32)alen);
  10504. if (ret != 0)
  10505. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10506. #ifdef HAVE_AES_DECRYPT
  10507. ret = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), iv1,
  10508. sizeof(iv1), resultT, sizeof(t1), large_aad, (word32)alen);
  10509. if (ret != 0)
  10510. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10511. #endif /* HAVE_AES_DECRYPT */
  10512. }
  10513. /* Test unaligned memory of all potential arguments */
  10514. ret = wc_AesGcmSetKey(enc, k1, sizeof(k1));
  10515. if (ret != 0)
  10516. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10517. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  10518. XMEMCPY(&buf[1], p, sizeof(p));
  10519. XMEMCPY(&bufA[1], a, sizeof(a));
  10520. ret = wc_AesGcmEncrypt(enc, &resultC[1], &buf[1], sizeof(p), iv1, sizeof(iv1),
  10521. &resultT[1], sizeof(t1), &bufA[1], sizeof(a));
  10522. if (ret != 0)
  10523. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10524. if (XMEMCMP(c1, &resultC[1], sizeof(c1)))
  10525. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10526. if (XMEMCMP(t1, &resultT[1], sizeof(t1)))
  10527. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10528. #ifdef HAVE_AES_DECRYPT
  10529. ret = wc_AesGcmSetKey(dec, k1, sizeof(k1));
  10530. if (ret != 0)
  10531. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10532. ret = wc_AesGcmDecrypt(dec, &resultP[1], &resultC[1], sizeof(c1),
  10533. iv1, sizeof(iv1), &resultT[1], sizeof(t1), &bufA[1], sizeof(a));
  10534. if (ret != 0)
  10535. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10536. if (XMEMCMP(p, &resultP[1], sizeof(p)))
  10537. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10538. #endif /* HAVE_AES_DECRYPT */
  10539. #endif /* Xilinx Versal */
  10540. #endif
  10541. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  10542. #ifdef BENCH_AESGCM_LARGE
  10543. /* Variable plain text length test */
  10544. for (plen=1; plen<BENCH_AESGCM_LARGE; plen++) {
  10545. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  10546. ret = wc_AesGcmEncrypt(enc, large_output, large_input,
  10547. plen, iv1, sizeof(iv1), resultT,
  10548. sizeof(t1), a, sizeof(a));
  10549. #if defined(WOLFSSL_ASYNC_CRYPT)
  10550. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10551. #endif
  10552. if (ret != 0)
  10553. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10554. #ifdef HAVE_AES_DECRYPT
  10555. ret = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  10556. plen, iv1, sizeof(iv1), resultT,
  10557. sizeof(t1), a, sizeof(a));
  10558. #if defined(WOLFSSL_ASYNC_CRYPT)
  10559. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  10560. #endif
  10561. if (ret != 0)
  10562. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10563. #endif /* HAVE_AES_DECRYPT */
  10564. }
  10565. #else /* BENCH_AESGCM_LARGE */
  10566. /* Variable plain text length test */
  10567. for (plen=1; plen<(int)sizeof(p); plen++) {
  10568. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  10569. ret = wc_AesGcmEncrypt(enc, resultC, p, (word32)plen, iv1,
  10570. sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  10571. #if defined(WOLFSSL_ASYNC_CRYPT)
  10572. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10573. #endif
  10574. if (ret != 0)
  10575. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10576. #ifdef HAVE_AES_DECRYPT
  10577. ret = wc_AesGcmDecrypt(dec, resultP, resultC, (word32)plen, iv1,
  10578. sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  10579. #if defined(WOLFSSL_ASYNC_CRYPT)
  10580. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  10581. #endif
  10582. if (ret != 0)
  10583. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10584. #endif /* HAVE_AES_DECRYPT */
  10585. }
  10586. #endif /* BENCH_AESGCM_LARGE */
  10587. #endif
  10588. #endif /* WOLFSSL_AES_256 */
  10589. /* test with IV != 12 bytes */
  10590. #ifdef ENABLE_NON_12BYTE_IV_TEST
  10591. XMEMSET(resultT, 0, sizeof(resultT));
  10592. XMEMSET(resultC, 0, sizeof(resultC));
  10593. XMEMSET(resultP, 0, sizeof(resultP));
  10594. #ifdef WOLFSSL_AES_192
  10595. wc_AesGcmSetKey(enc, k2, sizeof(k2));
  10596. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  10597. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv2, sizeof(iv2),
  10598. resultT, sizeof(t1), a, sizeof(a));
  10599. #if defined(WOLFSSL_ASYNC_CRYPT)
  10600. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10601. #endif
  10602. if (ret != 0)
  10603. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10604. if (XMEMCMP(c2, resultC, sizeof(c2)))
  10605. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10606. if (XMEMCMP(t2, resultT, sizeof(t1)))
  10607. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10608. #ifdef HAVE_AES_DECRYPT
  10609. ret = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c1),
  10610. iv2, sizeof(iv2), resultT, sizeof(t1), a, sizeof(a));
  10611. #if defined(WOLFSSL_ASYNC_CRYPT)
  10612. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10613. #endif
  10614. if (ret != 0)
  10615. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10616. if (XMEMCMP(p, resultP, sizeof(p)))
  10617. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10618. #endif /* HAVE_AES_DECRYPT */
  10619. XMEMSET(resultT, 0, sizeof(resultT));
  10620. XMEMSET(resultC, 0, sizeof(resultC));
  10621. XMEMSET(resultP, 0, sizeof(resultP));
  10622. #endif /* WOLFSSL_AES_192 */
  10623. #ifdef WOLFSSL_AES_128
  10624. wc_AesGcmSetKey(enc, k3, sizeof(k3));
  10625. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  10626. ret = wc_AesGcmEncrypt(enc, resultC, p3, sizeof(p3), iv3, sizeof(iv3),
  10627. resultT, sizeof(t3), a3, sizeof(a3));
  10628. #if defined(WOLFSSL_ASYNC_CRYPT)
  10629. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10630. #endif
  10631. if (ret != 0)
  10632. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10633. if (XMEMCMP(c3, resultC, sizeof(c3)))
  10634. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10635. if (XMEMCMP(t3, resultT, sizeof(t3)))
  10636. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10637. #ifdef HAVE_AES_DECRYPT
  10638. ret = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c3),
  10639. iv3, sizeof(iv3), resultT, sizeof(t3), a3, sizeof(a3));
  10640. #if defined(WOLFSSL_ASYNC_CRYPT)
  10641. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10642. #endif
  10643. if (ret != 0)
  10644. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10645. if (XMEMCMP(p3, resultP, sizeof(p3)))
  10646. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10647. #endif /* HAVE_AES_DECRYPT */
  10648. #endif /* WOLFSSL_AES_128 */
  10649. #endif /* ENABLE_NON_12BYTE_IV_TEST */
  10650. #if defined(WOLFSSL_AES_256) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  10651. !defined(WOLFSSL_XILINX_CRYPT) && \
  10652. !(defined(WOLF_CRYPTO_CB) && \
  10653. defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC))
  10654. XMEMSET(resultT, 0, sizeof(resultT));
  10655. XMEMSET(resultC, 0, sizeof(resultC));
  10656. XMEMSET(resultP, 0, sizeof(resultP));
  10657. wc_AesGcmSetKey(enc, k1, sizeof(k1));
  10658. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  10659. ret = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  10660. resultT + 1, sizeof(t1) - 1, a, sizeof(a));
  10661. #if defined(WOLFSSL_ASYNC_CRYPT)
  10662. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10663. #endif
  10664. if (ret != 0)
  10665. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10666. if (XMEMCMP(c1, resultC, sizeof(c1)))
  10667. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10668. if (XMEMCMP(t1, resultT + 1, sizeof(t1) - 1))
  10669. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10670. #ifdef HAVE_AES_DECRYPT
  10671. ret = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(p),
  10672. iv1, sizeof(iv1), resultT + 1, sizeof(t1) - 1, a, sizeof(a));
  10673. #if defined(WOLFSSL_ASYNC_CRYPT)
  10674. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10675. #endif
  10676. if (ret != 0)
  10677. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10678. if (XMEMCMP(p, resultP, sizeof(p)))
  10679. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10680. #endif /* HAVE_AES_DECRYPT */
  10681. #endif /* WOLFSSL_AES_256 */
  10682. #if !defined(HAVE_FIPS) || \
  10683. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  10684. /* Test encrypt with internally generated IV */
  10685. #if defined(WOLFSSL_AES_256) && !(defined(WC_NO_RNG) || defined(HAVE_SELFTEST)) \
  10686. && !(defined(WOLF_CRYPTO_CB) && defined(HAVE_CAVIUM_OCTEON_SYNC))
  10687. {
  10688. WC_RNG rng;
  10689. byte randIV[12];
  10690. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  10691. if (ret != 0)
  10692. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10693. XMEMSET(randIV, 0, sizeof(randIV));
  10694. XMEMSET(resultT, 0, sizeof(resultT));
  10695. XMEMSET(resultC, 0, sizeof(resultC));
  10696. XMEMSET(resultP, 0, sizeof(resultP));
  10697. wc_AesGcmSetKey(enc, k1, sizeof(k1));
  10698. ret = wc_AesGcmSetIV(enc, sizeof(randIV), NULL, 0, &rng);
  10699. if (ret != 0)
  10700. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10701. ret = wc_AesGcmEncrypt_ex(enc,
  10702. resultC, p, sizeof(p),
  10703. randIV, sizeof(randIV),
  10704. resultT, sizeof(t1),
  10705. a, sizeof(a));
  10706. #if defined(WOLFSSL_ASYNC_CRYPT)
  10707. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  10708. #endif
  10709. if (ret != 0)
  10710. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10711. /* Check the IV has been set. */
  10712. {
  10713. word32 i, ivSum = 0;
  10714. for (i = 0; i < sizeof(randIV); i++)
  10715. ivSum += randIV[i];
  10716. if (ivSum == 0)
  10717. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10718. }
  10719. #ifdef HAVE_AES_DECRYPT
  10720. wc_AesGcmSetKey(dec, k1, sizeof(k1));
  10721. ret = wc_AesGcmSetIV(dec, sizeof(randIV), NULL, 0, &rng);
  10722. if (ret != 0)
  10723. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10724. ret = wc_AesGcmDecrypt(dec,
  10725. resultP, resultC, sizeof(c1),
  10726. randIV, sizeof(randIV),
  10727. resultT, sizeof(t1),
  10728. a, sizeof(a));
  10729. #if defined(WOLFSSL_ASYNC_CRYPT)
  10730. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  10731. #endif
  10732. if (ret != 0)
  10733. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10734. if (XMEMCMP(p, resultP, sizeof(p)))
  10735. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10736. #endif /* HAVE_AES_DECRYPT */
  10737. wc_FreeRng(&rng);
  10738. }
  10739. #endif /* WOLFSSL_AES_256 && !(WC_NO_RNG || HAVE_SELFTEST) */
  10740. #endif /* HAVE_FIPS_VERSION >= 2 */
  10741. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  10742. #ifdef WOLFSSL_AES_256
  10743. #ifdef WOLFSSL_AESGCM_STREAM
  10744. ret = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  10745. if (ret != 0)
  10746. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10747. ret = wc_AesGcmEncryptUpdate(enc, resultC, p, sizeof(p), a, sizeof(a));
  10748. if (ret != 0)
  10749. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10750. ret = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  10751. if (ret != 0)
  10752. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10753. if (XMEMCMP(resultC, c1, sizeof(c1)) != 0)
  10754. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10755. if (XMEMCMP(resultT, t1, sizeof(t1)) != 0)
  10756. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10757. #ifdef HAVE_AES_DECRYPT
  10758. ret = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  10759. if (ret != 0)
  10760. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10761. ret = wc_AesGcmDecryptUpdate(enc, resultP, c1, sizeof(c1), a, sizeof(a));
  10762. if (ret != 0)
  10763. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10764. ret = wc_AesGcmDecryptFinal(enc, t1, sizeof(t1));
  10765. if (ret != 0)
  10766. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10767. if (XMEMCMP(resultP, p, sizeof(p)) != 0)
  10768. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10769. #endif
  10770. /* alen is the size to pass in with each update. */
  10771. for (alen = 1; alen < AES_BLOCK_SIZE + 1; alen++) {
  10772. ret = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  10773. if (ret != 0)
  10774. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10775. /* plen is the offset into AAD to update with. */
  10776. for (plen = 0; plen < (int)sizeof(a); plen += alen) {
  10777. int len = sizeof(a) - plen;
  10778. if (len > alen) len = alen;
  10779. ret = wc_AesGcmEncryptUpdate(enc, NULL, NULL, 0, a + plen, len);
  10780. if (ret != 0)
  10781. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10782. }
  10783. /* plen is the offset into plaintext to update with. */
  10784. for (plen = 0; plen < (int)sizeof(p); plen += alen) {
  10785. int len = sizeof(p) - plen;
  10786. if (len > alen) len = alen;
  10787. ret = wc_AesGcmEncryptUpdate(enc, resultC + plen, p + plen, len,
  10788. NULL, 0);
  10789. if (ret != 0)
  10790. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10791. }
  10792. ret = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  10793. if (ret != 0)
  10794. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10795. if (XMEMCMP(resultC, c1, sizeof(c1)) != 0)
  10796. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10797. if (XMEMCMP(resultT, t1, sizeof(t1)) != 0)
  10798. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10799. }
  10800. #ifdef HAVE_AES_DECRYPT
  10801. for (alen = 1; alen < AES_BLOCK_SIZE + 1; alen++) {
  10802. ret = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  10803. if (ret != 0)
  10804. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10805. /* plen is the offset into AAD to update with. */
  10806. for (plen = 0; plen < (int)sizeof(a); plen += alen) {
  10807. int len = sizeof(a) - plen;
  10808. if (len > alen) len = alen;
  10809. ret = wc_AesGcmDecryptUpdate(enc, NULL, NULL, 0, a + plen, len);
  10810. if (ret != 0)
  10811. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10812. }
  10813. /* plen is the offset into cipher text to update with. */
  10814. for (plen = 0; plen < (int)sizeof(c1); plen += alen) {
  10815. int len = sizeof(c1) - plen;
  10816. if (len > alen) len = alen;
  10817. ret = wc_AesGcmDecryptUpdate(enc, resultP + plen, c1 + plen, len,
  10818. NULL, 0);
  10819. if (ret != 0)
  10820. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10821. }
  10822. ret = wc_AesGcmDecryptFinal(enc, t1, sizeof(t1));
  10823. if (ret != 0)
  10824. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10825. if (XMEMCMP(resultP, p, sizeof(p)) != 0)
  10826. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10827. }
  10828. #endif /* HAVE_AES_DECRYPT */
  10829. #ifdef BENCH_AESGCM_LARGE
  10830. /* setup test buffer */
  10831. ret = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  10832. if (ret != 0)
  10833. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10834. ret = wc_AesGcmEncryptUpdate(enc, large_output, large_input,
  10835. BENCH_AESGCM_LARGE, a, sizeof(a));
  10836. if (ret != 0)
  10837. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10838. ret = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  10839. if (ret != 0)
  10840. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10841. #ifdef HAVE_AES_DECRYPT
  10842. ret = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  10843. if (ret != 0)
  10844. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10845. ret = wc_AesGcmDecryptUpdate(enc, large_outdec, large_output,
  10846. BENCH_AESGCM_LARGE, a, sizeof(a));
  10847. if (ret != 0)
  10848. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10849. ret = wc_AesGcmDecryptFinal(enc, resultT, sizeof(t1));
  10850. if (ret != 0)
  10851. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10852. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  10853. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10854. #endif /* HAVE_AES_DECRYPT */
  10855. #endif /* BENCH_AESGCM_LARGE */
  10856. #endif /* WOLFSSL_AESGCM_STREAM */
  10857. #endif /* WOLFSSL_AES_256 */
  10858. #endif /* !WOLFSSL_AFALG_XILINX_AES && !WOLFSSL_XILINX_CRYPT */
  10859. wc_AesFree(enc);
  10860. wc_AesFree(dec);
  10861. ret = 0;
  10862. out:
  10863. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  10864. !defined(WOLFSSL_NO_MALLOC)
  10865. if (large_input)
  10866. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10867. if (large_output)
  10868. XFREE(large_output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10869. if (large_outdec)
  10870. XFREE(large_outdec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10871. #endif
  10872. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10873. if (enc)
  10874. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  10875. if (dec)
  10876. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  10877. #endif
  10878. return ret;
  10879. }
  10880. #ifdef WOLFSSL_AES_128
  10881. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t gmac_test(void)
  10882. {
  10883. wc_test_ret_t ret;
  10884. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10885. Gmac *gmac;
  10886. #else
  10887. Gmac gmac[1];
  10888. #endif
  10889. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  10890. {
  10891. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  10892. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  10893. };
  10894. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  10895. {
  10896. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  10897. 0xe2, 0x8c, 0x8f, 0x16
  10898. };
  10899. WOLFSSL_SMALL_STACK_STATIC const byte a1[] =
  10900. {
  10901. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  10902. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  10903. };
  10904. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  10905. {
  10906. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  10907. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  10908. };
  10909. #if (!defined(HAVE_FIPS) || \
  10910. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
  10911. /* FIPS builds only allow 16-byte auth tags. */
  10912. /* This sample uses a 15-byte auth tag. */
  10913. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  10914. {
  10915. 0x40, 0xf7, 0xec, 0xb2, 0x52, 0x6d, 0xaa, 0xd4,
  10916. 0x74, 0x25, 0x1d, 0xf4, 0x88, 0x9e, 0xf6, 0x5b
  10917. };
  10918. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  10919. {
  10920. 0xee, 0x9c, 0x6e, 0x06, 0x15, 0x45, 0x45, 0x03,
  10921. 0x1a, 0x60, 0x24, 0xa7
  10922. };
  10923. WOLFSSL_SMALL_STACK_STATIC const byte a2[] =
  10924. {
  10925. 0x94, 0x81, 0x2c, 0x87, 0x07, 0x4e, 0x15, 0x18,
  10926. 0x34, 0xb8, 0x35, 0xaf, 0x1c, 0xa5, 0x7e, 0x56
  10927. };
  10928. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  10929. {
  10930. 0xc6, 0x81, 0x79, 0x8e, 0x3d, 0xda, 0xb0, 0x9f,
  10931. 0x8d, 0x83, 0xb0, 0xbb, 0x14, 0xb6, 0x91
  10932. };
  10933. #endif
  10934. byte tag[16];
  10935. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10936. if ((gmac = (Gmac *)XMALLOC(sizeof *gmac, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10937. return WC_TEST_RET_ENC_ERRNO;
  10938. #endif
  10939. XMEMSET(gmac, 0, sizeof *gmac); /* clear context */
  10940. (void)wc_AesInit(&gmac->aes, HEAP_HINT, INVALID_DEVID); /* Make sure devId updated */
  10941. XMEMSET(tag, 0, sizeof(tag));
  10942. wc_GmacSetKey(gmac, k1, sizeof(k1));
  10943. wc_GmacUpdate(gmac, iv1, sizeof(iv1), a1, sizeof(a1), tag, sizeof(t1));
  10944. if (XMEMCMP(t1, tag, sizeof(t1)) != 0)
  10945. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10946. #if (!defined(HAVE_FIPS) || \
  10947. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)) )
  10948. XMEMSET(tag, 0, sizeof(tag));
  10949. wc_GmacSetKey(gmac, k2, sizeof(k2));
  10950. wc_GmacUpdate(gmac, iv2, sizeof(iv2), a2, sizeof(a2), tag, sizeof(t2));
  10951. if (XMEMCMP(t2, tag, sizeof(t2)) != 0)
  10952. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  10953. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && !defined(NO_AES_DECRYPT)
  10954. {
  10955. WOLFSSL_SMALL_STACK_STATIC const byte badT[] =
  10956. {
  10957. 0xde, 0xad, 0xbe, 0xef, 0x17, 0x2e, 0xd0, 0x43,
  10958. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  10959. };
  10960. WC_RNG rng;
  10961. byte iv[12];
  10962. #ifndef HAVE_FIPS
  10963. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  10964. if (ret != 0)
  10965. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10966. #else
  10967. ret = wc_InitRng(&rng);
  10968. if (ret != 0)
  10969. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10970. #endif
  10971. ret = wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  10972. t1, sizeof(t1));
  10973. if (ret != 0)
  10974. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10975. ret = wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  10976. badT, sizeof(badT));
  10977. if (ret != AES_GCM_AUTH_E)
  10978. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10979. ret = wc_GmacVerify(k2, sizeof(k2), iv2, sizeof(iv2), a2, sizeof(a2),
  10980. t2, sizeof(t2));
  10981. if (ret != 0)
  10982. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10983. XMEMSET(tag, 0, sizeof(tag));
  10984. XMEMSET(iv, 0, sizeof(iv));
  10985. ret = wc_Gmac(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  10986. tag, sizeof(tag), &rng);
  10987. if (ret != 0)
  10988. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10989. ret = wc_GmacVerify(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  10990. tag, sizeof(tag));
  10991. if (ret != 0)
  10992. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  10993. wc_FreeRng(&rng);
  10994. }
  10995. #endif /* !WC_NO_RNG && !HAVE_SELFTEST && !NO_AES_DECRYPT */
  10996. #endif /* HAVE_FIPS */
  10997. ret = 0;
  10998. out:
  10999. wc_AesFree(&gmac->aes);
  11000. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11001. XFREE(gmac, HEAP_HINT, DYNAMIC_TYPE_AES);
  11002. #endif
  11003. return ret;
  11004. }
  11005. #endif /* WOLFSSL_AES_128 */
  11006. #endif /* HAVE_AESGCM */
  11007. #if defined(HAVE_AESCCM)
  11008. #if defined(WOLFSSL_AES_256)
  11009. static wc_test_ret_t aesccm_256_test(void)
  11010. {
  11011. wc_test_ret_t ret;
  11012. /* Test vectors from NIST AES CCM 256-bit CAST Example #1 */
  11013. WOLFSSL_SMALL_STACK_STATIC const byte in_key[32] = {
  11014. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  11015. 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
  11016. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  11017. 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F
  11018. };
  11019. WOLFSSL_SMALL_STACK_STATIC const byte in_nonce[7] = {
  11020. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
  11021. WOLFSSL_SMALL_STACK_STATIC const byte in_auth[8] = {
  11022. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  11023. WOLFSSL_SMALL_STACK_STATIC const byte in_plaintext[4] = {
  11024. 0x20, 0x21, 0x22, 0x23};
  11025. WOLFSSL_SMALL_STACK_STATIC const byte exp_ciphertext[4] = {
  11026. 0x8A, 0xB1, 0xA8, 0x74};
  11027. WOLFSSL_SMALL_STACK_STATIC const byte exp_tag[4] = {
  11028. 0x95, 0xFC, 0x08, 0x20};
  11029. byte output[sizeof(in_plaintext)];
  11030. byte atag[sizeof(exp_tag)];
  11031. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11032. Aes* aes = (Aes*)XMALLOC(sizeof(Aes), HEAP_HINT, DYNAMIC_TYPE_AES);
  11033. if (aes == NULL) {
  11034. return MEMORY_E;
  11035. }
  11036. #else
  11037. Aes aes[1];
  11038. #endif
  11039. ret = wc_AesInit(aes, HEAP_HINT, devId);
  11040. if (ret == 0) {
  11041. ret = wc_AesCcmSetKey(aes, in_key, sizeof(in_key));
  11042. }
  11043. if (ret == 0) {
  11044. ret = wc_AesCcmEncrypt(aes, output, in_plaintext, sizeof(in_plaintext),
  11045. in_nonce, sizeof(in_nonce),
  11046. atag, sizeof(atag),
  11047. in_auth, sizeof(in_auth));
  11048. }
  11049. /* Verify we produce the proper ciphertext and tag */
  11050. if (ret == 0 &&
  11051. (XMEMCMP(output, exp_ciphertext, sizeof(output)) ||
  11052. XMEMCMP(atag, exp_tag, sizeof(atag)))) {
  11053. ret = WC_TEST_RET_ENC_NC;
  11054. }
  11055. if (ret == 0) {
  11056. /* decrypt inline */
  11057. ret = wc_AesCcmDecrypt(aes, output, output, sizeof(output),
  11058. in_nonce, sizeof(in_nonce),
  11059. atag, sizeof(atag),
  11060. in_auth, sizeof(in_auth));
  11061. }
  11062. /* Verify decryption was successful */
  11063. if (ret == 0 &&
  11064. XMEMCMP(output, in_plaintext, sizeof(output))) {
  11065. ret = WC_TEST_RET_ENC_NC;
  11066. }
  11067. wc_AesFree(aes);
  11068. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11069. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  11070. #endif
  11071. return ret;
  11072. }
  11073. #endif /* WOLFSSL_AES_256 */
  11074. #if defined(WOLFSSL_AES_128)
  11075. static wc_test_ret_t aesccm_128_test(void)
  11076. {
  11077. wc_test_ret_t ret;
  11078. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11079. Aes *enc;
  11080. #else
  11081. Aes enc[1];
  11082. #endif
  11083. /* key */
  11084. WOLFSSL_SMALL_STACK_STATIC const byte k[] =
  11085. {
  11086. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  11087. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  11088. };
  11089. /* nonce */
  11090. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  11091. {
  11092. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  11093. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  11094. };
  11095. /* plaintext */
  11096. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  11097. {
  11098. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  11099. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  11100. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  11101. };
  11102. /* plaintext - long */
  11103. WOLFSSL_SMALL_STACK_STATIC const byte pl[] =
  11104. {
  11105. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  11106. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  11107. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  11108. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  11109. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  11110. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  11111. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  11112. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  11113. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  11114. 0x50
  11115. };
  11116. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  11117. {
  11118. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  11119. };
  11120. /* ciphertext */
  11121. WOLFSSL_SMALL_STACK_STATIC const byte c[] =
  11122. {
  11123. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  11124. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  11125. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  11126. };
  11127. /* tag - authentication */
  11128. WOLFSSL_SMALL_STACK_STATIC const byte t[] =
  11129. {
  11130. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  11131. };
  11132. /* ciphertext - long */
  11133. WOLFSSL_SMALL_STACK_STATIC const byte cl[] =
  11134. {
  11135. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  11136. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  11137. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84, 0xe0,
  11138. 0x44, 0x2d, 0xbe, 0x25, 0xfa, 0x48, 0x2b, 0xa8,
  11139. 0x36, 0x0b, 0xbf, 0x01, 0xc0, 0x12, 0x45, 0xa4,
  11140. 0x82, 0x9f, 0x20, 0x6c, 0xc3, 0xd6, 0xae, 0x5b,
  11141. 0x54, 0x8d, 0xd0, 0xb1, 0x69, 0x2c, 0xec, 0x5e,
  11142. 0x95, 0xa5, 0x6b, 0x48, 0xc3, 0xc6, 0xc8, 0x9e,
  11143. 0xc7, 0x92, 0x98, 0x9d, 0x26, 0x7d, 0x2a, 0x10,
  11144. 0x0b
  11145. };
  11146. /* tag - authentication - long */
  11147. WOLFSSL_SMALL_STACK_STATIC const byte tl[] =
  11148. {
  11149. 0x89, 0xd8, 0xd2, 0x02, 0xc5, 0xcf, 0xae, 0xf4
  11150. };
  11151. /* tag - authentication - empty plaintext */
  11152. WOLFSSL_SMALL_STACK_STATIC const byte t_empty[] =
  11153. {
  11154. 0xe4, 0x28, 0x8a, 0xc3, 0x78, 0x00, 0x0f, 0xf5
  11155. };
  11156. byte t2[sizeof(t)];
  11157. byte p2[sizeof(p)];
  11158. byte c2[sizeof(c)];
  11159. byte iv2[sizeof(iv)];
  11160. byte pl2[sizeof(pl)];
  11161. byte cl2[sizeof(cl)];
  11162. byte tl2[sizeof(tl)];
  11163. byte t_empty2[sizeof(t_empty)];
  11164. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11165. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  11166. return WC_TEST_RET_ENC_ERRNO;
  11167. #endif
  11168. XMEMSET(enc, 0, sizeof *enc); /* clear context */
  11169. XMEMSET(t2, 0, sizeof(t2));
  11170. XMEMSET(c2, 0, sizeof(c2));
  11171. XMEMSET(p2, 0, sizeof(p2));
  11172. ret = wc_AesInit(enc, HEAP_HINT, devId);
  11173. if (ret != 0)
  11174. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11175. ret = wc_AesCcmSetKey(enc, k, sizeof(k));
  11176. if (ret != 0)
  11177. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11178. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  11179. ret = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  11180. t2, sizeof(t2), a, sizeof(a));
  11181. if (ret != 0)
  11182. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11183. if (XMEMCMP(c, c2, sizeof(c2)))
  11184. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11185. if (XMEMCMP(t, t2, sizeof(t2)))
  11186. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11187. ret = wc_AesCcmDecrypt(enc, p2, c2, sizeof(p2), iv, sizeof(iv),
  11188. t2, sizeof(t2), a, sizeof(a));
  11189. if (ret != 0)
  11190. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11191. if (XMEMCMP(p, p2, sizeof(p2)))
  11192. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11193. /* Test the authentication failure */
  11194. t2[0]++; /* Corrupt the authentication tag. */
  11195. ret = wc_AesCcmDecrypt(enc, p2, c, sizeof(p2), iv, sizeof(iv),
  11196. t2, sizeof(t2), a, sizeof(a));
  11197. if (ret == 0)
  11198. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11199. /* Clear c2 to compare against p2. p2 should be set to zero in case of
  11200. * authentication fail. */
  11201. XMEMSET(c2, 0, sizeof(c2));
  11202. if (XMEMCMP(p2, c2, sizeof(p2)))
  11203. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11204. wc_AesFree(enc);
  11205. XMEMSET(enc, 0, sizeof(Aes)); /* clear context */
  11206. XMEMSET(t2, 0, sizeof(t2));
  11207. XMEMSET(c2, 0, sizeof(c2));
  11208. XMEMSET(p2, 0, sizeof(p2));
  11209. XMEMSET(iv2, 0, sizeof(iv2));
  11210. #ifndef HAVE_SELFTEST
  11211. /* selftest build does not have wc_AesCcmSetNonce() or
  11212. * wc_AesCcmEncrypt_ex() */
  11213. ret = wc_AesCcmSetKey(enc, k, sizeof(k));
  11214. if (ret != 0)
  11215. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11216. ret = wc_AesCcmSetNonce(enc, iv, sizeof(iv));
  11217. if (ret != 0)
  11218. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11219. ret = wc_AesCcmEncrypt_ex(enc, c2, p, sizeof(c2), iv2, sizeof(iv2),
  11220. t2, sizeof(t2), a, sizeof(a));
  11221. if (ret != 0)
  11222. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11223. if (XMEMCMP(iv, iv2, sizeof(iv2)))
  11224. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11225. if (XMEMCMP(c, c2, sizeof(c2)))
  11226. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11227. if (XMEMCMP(t, t2, sizeof(t2)))
  11228. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11229. #endif
  11230. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  11231. /* test fail on invalid IV sizes */
  11232. ret = wc_AesCcmSetKey(enc, k, sizeof(k));
  11233. if (ret != 0)
  11234. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11235. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  11236. ret = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  11237. t2, 1, a, sizeof(a));
  11238. if (ret == 0) {
  11239. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11240. }
  11241. #endif
  11242. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  11243. ret = wc_AesCcmEncrypt(enc, cl2, pl, sizeof(cl2), iv, sizeof(iv),
  11244. tl2, sizeof(tl2), a, sizeof(a));
  11245. if (ret != 0)
  11246. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11247. if (XMEMCMP(cl, cl2, sizeof(cl2)))
  11248. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11249. if (XMEMCMP(tl, tl2, sizeof(tl2)))
  11250. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11251. ret = wc_AesCcmDecrypt(enc, pl2, cl2, sizeof(pl2), iv, sizeof(iv),
  11252. tl2, sizeof(tl2), a, sizeof(a));
  11253. if (ret != 0)
  11254. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11255. if (XMEMCMP(pl, pl2, sizeof(pl2)))
  11256. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11257. /* test empty message as null input or output with nonzero inSz. */
  11258. ret = wc_AesCcmEncrypt(enc, pl2 /* out */, NULL /* in */, 1 /* inSz */,
  11259. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  11260. a, sizeof(a));
  11261. if (ret != BAD_FUNC_ARG)
  11262. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11263. ret = wc_AesCcmEncrypt(enc, NULL /* out */, (const byte *)"" /* in */, 1 /* inSz */,
  11264. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  11265. a, sizeof(a));
  11266. if (ret != BAD_FUNC_ARG)
  11267. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11268. ret = wc_AesCcmDecrypt(enc, pl2, NULL /* in */, 1 /* inSz */,
  11269. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  11270. sizeof(a));
  11271. if (ret != BAD_FUNC_ARG)
  11272. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11273. ret = wc_AesCcmDecrypt(enc, NULL /* out */, (const byte *)"" /* in */, 1 /* inSz */,
  11274. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  11275. sizeof(a));
  11276. if (ret != BAD_FUNC_ARG)
  11277. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11278. /* test empty message as null input and output with zero inSz --
  11279. * must either succeed, or fail early with BAD_FUNC_ARG.
  11280. */
  11281. ret = wc_AesCcmEncrypt(enc, NULL /* out */, NULL /* in */, 0 /* inSz */,
  11282. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  11283. a, sizeof(a));
  11284. if (ret != BAD_FUNC_ARG) {
  11285. if (ret != 0)
  11286. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11287. if (XMEMCMP(t_empty, t_empty2, sizeof(t_empty2)))
  11288. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11289. ret = wc_AesCcmDecrypt(enc, NULL /* out */, NULL /* in */,
  11290. 0 /* inSz */, iv, sizeof(iv), t_empty2,
  11291. sizeof(t_empty2), a, sizeof(a));
  11292. if (ret != 0)
  11293. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11294. }
  11295. /* test empty message as zero-length string -- must work. */
  11296. ret = wc_AesCcmEncrypt(enc, pl2, (const byte *)"", 0 /* inSz */, iv,
  11297. sizeof(iv), t_empty2, sizeof(t_empty2), a,
  11298. sizeof(a));
  11299. if (ret != 0)
  11300. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11301. if (XMEMCMP(t_empty, t_empty2, sizeof(t_empty2)))
  11302. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  11303. ret = wc_AesCcmDecrypt(enc, pl2, (const byte *)"", 0 /* inSz */,
  11304. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  11305. sizeof(a));
  11306. if (ret != 0)
  11307. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11308. wc_AesFree(enc);
  11309. ret = 0;
  11310. out:
  11311. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11312. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  11313. #endif
  11314. return ret;
  11315. }
  11316. #endif /* WOLFSSL_AES_128 */
  11317. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aesccm_test(void)
  11318. {
  11319. wc_test_ret_t ret = 0;
  11320. #ifdef WOLFSSL_AES_128
  11321. if (ret == 0)
  11322. ret = aesccm_128_test();
  11323. #endif
  11324. #ifdef WOLFSSL_AES_256
  11325. if (ret == 0)
  11326. ret = aesccm_256_test();
  11327. #endif
  11328. return ret;
  11329. }
  11330. #endif /* HAVE_AESCCM */
  11331. #ifdef HAVE_AES_KEYWRAP
  11332. #define MAX_KEYWRAP_TEST_OUTLEN 40
  11333. #define MAX_KEYWRAP_TEST_PLAINLEN 32
  11334. typedef struct keywrapVector {
  11335. const byte* kek;
  11336. const byte* data;
  11337. const byte* verify;
  11338. word32 kekLen;
  11339. word32 dataLen;
  11340. word32 verifyLen;
  11341. } keywrapVector;
  11342. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aeskeywrap_test(void)
  11343. {
  11344. int wrapSz, plainSz, testSz, i;
  11345. /* test vectors from RFC 3394 (kek, data, verify) */
  11346. #ifdef WOLFSSL_AES_128
  11347. /* Wrap 128 bits of Key Data with a 128-bit KEK */
  11348. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  11349. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  11350. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  11351. };
  11352. WOLFSSL_SMALL_STACK_STATIC const byte d1[] = {
  11353. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  11354. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  11355. };
  11356. WOLFSSL_SMALL_STACK_STATIC const byte v1[] = {
  11357. 0x1F, 0xA6, 0x8B, 0x0A, 0x81, 0x12, 0xB4, 0x47,
  11358. 0xAE, 0xF3, 0x4B, 0xD8, 0xFB, 0x5A, 0x7B, 0x82,
  11359. 0x9D, 0x3E, 0x86, 0x23, 0x71, 0xD2, 0xCF, 0xE5
  11360. };
  11361. #endif /* WOLFSSL_AES_128 */
  11362. #ifdef WOLFSSL_AES_192
  11363. /* Wrap 128 bits of Key Data with a 192-bit KEK */
  11364. WOLFSSL_SMALL_STACK_STATIC const byte k2[] = {
  11365. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  11366. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  11367. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  11368. };
  11369. WOLFSSL_SMALL_STACK_STATIC const byte d2[] = {
  11370. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  11371. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  11372. };
  11373. WOLFSSL_SMALL_STACK_STATIC const byte v2[] = {
  11374. 0x96, 0x77, 0x8B, 0x25, 0xAE, 0x6C, 0xA4, 0x35,
  11375. 0xF9, 0x2B, 0x5B, 0x97, 0xC0, 0x50, 0xAE, 0xD2,
  11376. 0x46, 0x8A, 0xB8, 0xA1, 0x7A, 0xD8, 0x4E, 0x5D
  11377. };
  11378. #endif
  11379. #ifdef WOLFSSL_AES_256
  11380. /* Wrap 128 bits of Key Data with a 256-bit KEK */
  11381. WOLFSSL_SMALL_STACK_STATIC const byte k3[] = {
  11382. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  11383. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  11384. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  11385. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  11386. };
  11387. WOLFSSL_SMALL_STACK_STATIC const byte d3[] = {
  11388. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  11389. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  11390. };
  11391. WOLFSSL_SMALL_STACK_STATIC const byte v3[] = {
  11392. 0x64, 0xE8, 0xC3, 0xF9, 0xCE, 0x0F, 0x5B, 0xA2,
  11393. 0x63, 0xE9, 0x77, 0x79, 0x05, 0x81, 0x8A, 0x2A,
  11394. 0x93, 0xC8, 0x19, 0x1E, 0x7D, 0x6E, 0x8A, 0xE7
  11395. };
  11396. #endif
  11397. #ifdef WOLFSSL_AES_192
  11398. /* Wrap 192 bits of Key Data with a 192-bit KEK */
  11399. WOLFSSL_SMALL_STACK_STATIC const byte k4[] = {
  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. };
  11404. WOLFSSL_SMALL_STACK_STATIC const byte d4[] = {
  11405. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  11406. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  11407. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  11408. };
  11409. WOLFSSL_SMALL_STACK_STATIC const byte v4[] = {
  11410. 0x03, 0x1D, 0x33, 0x26, 0x4E, 0x15, 0xD3, 0x32,
  11411. 0x68, 0xF2, 0x4E, 0xC2, 0x60, 0x74, 0x3E, 0xDC,
  11412. 0xE1, 0xC6, 0xC7, 0xDD, 0xEE, 0x72, 0x5A, 0x93,
  11413. 0x6B, 0xA8, 0x14, 0x91, 0x5C, 0x67, 0x62, 0xD2
  11414. };
  11415. #endif
  11416. #ifdef WOLFSSL_AES_256
  11417. /* Wrap 192 bits of Key Data with a 256-bit KEK */
  11418. WOLFSSL_SMALL_STACK_STATIC const byte k5[] = {
  11419. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  11420. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  11421. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  11422. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  11423. };
  11424. WOLFSSL_SMALL_STACK_STATIC const byte d5[] = {
  11425. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  11426. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  11427. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  11428. };
  11429. WOLFSSL_SMALL_STACK_STATIC const byte v5[] = {
  11430. 0xA8, 0xF9, 0xBC, 0x16, 0x12, 0xC6, 0x8B, 0x3F,
  11431. 0xF6, 0xE6, 0xF4, 0xFB, 0xE3, 0x0E, 0x71, 0xE4,
  11432. 0x76, 0x9C, 0x8B, 0x80, 0xA3, 0x2C, 0xB8, 0x95,
  11433. 0x8C, 0xD5, 0xD1, 0x7D, 0x6B, 0x25, 0x4D, 0xA1
  11434. };
  11435. /* Wrap 256 bits of Key Data with a 256-bit KEK */
  11436. WOLFSSL_SMALL_STACK_STATIC const byte k6[] = {
  11437. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  11438. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  11439. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  11440. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  11441. };
  11442. WOLFSSL_SMALL_STACK_STATIC const byte d6[] = {
  11443. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  11444. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  11445. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  11446. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  11447. };
  11448. WOLFSSL_SMALL_STACK_STATIC const byte v6[] = {
  11449. 0x28, 0xC9, 0xF4, 0x04, 0xC4, 0xB8, 0x10, 0xF4,
  11450. 0xCB, 0xCC, 0xB3, 0x5C, 0xFB, 0x87, 0xF8, 0x26,
  11451. 0x3F, 0x57, 0x86, 0xE2, 0xD8, 0x0E, 0xD3, 0x26,
  11452. 0xCB, 0xC7, 0xF0, 0xE7, 0x1A, 0x99, 0xF4, 0x3B,
  11453. 0xFB, 0x98, 0x8B, 0x9B, 0x7A, 0x02, 0xDD, 0x21
  11454. };
  11455. #endif /* WOLFSSL_AES_256 */
  11456. byte output[MAX_KEYWRAP_TEST_OUTLEN];
  11457. byte plain [MAX_KEYWRAP_TEST_PLAINLEN];
  11458. const keywrapVector test_wrap[] =
  11459. {
  11460. #ifdef WOLFSSL_AES_128
  11461. {k1, d1, v1, sizeof(k1), sizeof(d1), sizeof(v1)},
  11462. #endif
  11463. #ifdef WOLFSSL_AES_192
  11464. {k2, d2, v2, sizeof(k2), sizeof(d2), sizeof(v2)},
  11465. #endif
  11466. #ifdef WOLFSSL_AES_256
  11467. {k3, d3, v3, sizeof(k3), sizeof(d3), sizeof(v3)},
  11468. #endif
  11469. #ifdef WOLFSSL_AES_192
  11470. {k4, d4, v4, sizeof(k4), sizeof(d4), sizeof(v4)},
  11471. #endif
  11472. #ifdef WOLFSSL_AES_256
  11473. {k5, d5, v5, sizeof(k5), sizeof(d5), sizeof(v5)},
  11474. {k6, d6, v6, sizeof(k6), sizeof(d6), sizeof(v6)}
  11475. #endif
  11476. };
  11477. testSz = sizeof(test_wrap) / sizeof(keywrapVector);
  11478. XMEMSET(output, 0, sizeof(output));
  11479. XMEMSET(plain, 0, sizeof(plain));
  11480. for (i = 0; i < testSz; i++) {
  11481. wrapSz = wc_AesKeyWrap(test_wrap[i].kek, test_wrap[i].kekLen,
  11482. test_wrap[i].data, test_wrap[i].dataLen,
  11483. output, sizeof(output), NULL);
  11484. if ( (wrapSz < 0) || (wrapSz != (int)test_wrap[i].verifyLen) )
  11485. return WC_TEST_RET_ENC_NC;
  11486. if (XMEMCMP(output, test_wrap[i].verify, test_wrap[i].verifyLen) != 0)
  11487. return WC_TEST_RET_ENC_NC;
  11488. plainSz = wc_AesKeyUnWrap((byte*)test_wrap[i].kek, test_wrap[i].kekLen,
  11489. output, wrapSz,
  11490. plain, sizeof(plain), NULL);
  11491. if ( (plainSz < 0) || (plainSz != (int)test_wrap[i].dataLen) )
  11492. return WC_TEST_RET_ENC_NC;
  11493. if (XMEMCMP(plain, test_wrap[i].data, test_wrap[i].dataLen) != 0)
  11494. return WC_TEST_RET_ENC_I(i);
  11495. }
  11496. return 0;
  11497. }
  11498. #endif /* HAVE_AES_KEYWRAP */
  11499. #endif /* NO_AES */
  11500. #ifdef HAVE_ARIA
  11501. void printOutput(const char *strName, unsigned char *data, unsigned int dataSz)
  11502. {
  11503. #ifndef DEBUG_WOLFSSL
  11504. (void)strName;
  11505. (void)data;
  11506. (void)dataSz;
  11507. #else
  11508. WOLFSSL_MSG_EX("%s (%d):", strName,dataSz);
  11509. WOLFSSL_BUFFER(data,dataSz);
  11510. #endif
  11511. }
  11512. WOLFSSL_TEST_SUBROUTINE int ariagcm_test(MC_ALGID algo)
  11513. {
  11514. int ret = 0;
  11515. byte data[] = TEST_STRING;
  11516. word32 dataSz = TEST_STRING_SZ;
  11517. /* Arbitrarily random long key that we will truncate to the right size */
  11518. byte key[] = { 0x1E, 0xCC, 0x95, 0xCB, 0xD3, 0x74, 0x58, 0x4F,
  11519. 0x6F, 0x8A, 0x70, 0x26, 0xF7, 0x3C, 0x8D, 0xB6,
  11520. 0xDC, 0x32, 0x76, 0x20, 0xCF, 0x05, 0x4A, 0xCF,
  11521. 0x11, 0x86, 0xCD, 0x23, 0x5E, 0xC1, 0x6E, 0x2B };
  11522. byte cipher[2*TEST_STRING_SZ], plain[TEST_STRING_SZ], ad[256], authTag[AES_BLOCK_SIZE];
  11523. word32 keySz, adSz = 256, authTagSz = sizeof(authTag);
  11524. wc_Aria aria;
  11525. XMEMSET((void *)&aria, 0, sizeof(aria));
  11526. ret = wc_AriaInitCrypt(&aria, algo);
  11527. if (ret != 0) { ERROR_OUT(WC_TEST_RET_ENC_EC(ret),out); }
  11528. ret = wc_AriaSetKey(&aria, key);
  11529. if (ret != 0) { ERROR_OUT(WC_TEST_RET_ENC_EC(ret),out); }
  11530. MC_GetObjectValue(aria.hSession, aria.hKey, key, &keySz);
  11531. printOutput("Key", key, keySz);
  11532. WC_RNG rng;
  11533. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  11534. if (ret != 0)
  11535. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11536. ret = wc_AriaGcmSetIV(&aria, GCM_NONCE_MID_SZ, NULL, 0, &rng);
  11537. if (ret != 0)
  11538. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  11539. wc_FreeRng(&rng);
  11540. printOutput("Plaintext", data, sizeof(data));
  11541. XMEMSET(cipher, 0, sizeof(cipher));
  11542. ret = wc_AriaEncrypt(&aria, cipher, data, dataSz,
  11543. (byte *)aria.nonce, aria.nonceSz, ad, adSz,
  11544. authTag, authTagSz);
  11545. if (ret != 0) { ERROR_OUT(WC_TEST_RET_ENC_EC(ret),out); }
  11546. printOutput("Ciphertext", cipher, sizeof(cipher));
  11547. printOutput("AuthTag", authTag, sizeof(authTag));
  11548. XMEMSET(plain, 0, sizeof(plain));
  11549. ret = wc_AriaDecrypt(&aria, plain, cipher, dataSz,
  11550. (byte *)aria.nonce, aria.nonceSz, ad, adSz,
  11551. authTag, authTagSz);
  11552. if (ret != 0) { ERROR_OUT(WC_TEST_RET_ENC_EC(ret),out); }
  11553. printOutput("Plaintext", plain, sizeof(plain));
  11554. if (XMEMCMP(plain, data, dataSz) != 0)
  11555. ERROR_OUT(WC_TEST_RET_ENC_NC,out);
  11556. out:
  11557. if (ret != 0) { wc_AriaFreeCrypt(&aria); }
  11558. else { ret = wc_AriaFreeCrypt(&aria); }
  11559. return ret;
  11560. }
  11561. #endif /* HAVE_ARIA */
  11562. #ifdef HAVE_CAMELLIA
  11563. enum {
  11564. CAM_ECB_ENC, CAM_ECB_DEC, CAM_CBC_ENC, CAM_CBC_DEC
  11565. };
  11566. typedef struct {
  11567. int type;
  11568. const byte* plaintext;
  11569. const byte* iv;
  11570. const byte* ciphertext;
  11571. const byte* key;
  11572. word32 keySz;
  11573. int errorCode;
  11574. } test_vector_t;
  11575. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t camellia_test(void)
  11576. {
  11577. /* Camellia ECB Test Plaintext */
  11578. WOLFSSL_SMALL_STACK_STATIC const byte pte[] =
  11579. {
  11580. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  11581. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  11582. };
  11583. /* Camellia ECB Test Initialization Vector */
  11584. WOLFSSL_SMALL_STACK_STATIC const byte ive[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  11585. /* Test 1: Camellia ECB 128-bit key */
  11586. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  11587. {
  11588. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  11589. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  11590. };
  11591. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  11592. {
  11593. 0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73,
  11594. 0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43
  11595. };
  11596. /* Test 2: Camellia ECB 192-bit key */
  11597. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  11598. {
  11599. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  11600. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  11601. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  11602. };
  11603. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  11604. {
  11605. 0xb4, 0x99, 0x34, 0x01, 0xb3, 0xe9, 0x96, 0xf8,
  11606. 0x4e, 0xe5, 0xce, 0xe7, 0xd7, 0x9b, 0x09, 0xb9
  11607. };
  11608. /* Test 3: Camellia ECB 256-bit key */
  11609. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  11610. {
  11611. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  11612. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  11613. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  11614. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  11615. };
  11616. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  11617. {
  11618. 0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c,
  11619. 0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09
  11620. };
  11621. /* Camellia CBC Test Plaintext */
  11622. WOLFSSL_SMALL_STACK_STATIC const byte ptc[] =
  11623. {
  11624. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  11625. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  11626. };
  11627. /* Camellia CBC Test Initialization Vector */
  11628. WOLFSSL_SMALL_STACK_STATIC const byte ivc[] =
  11629. {
  11630. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  11631. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  11632. };
  11633. /* Test 4: Camellia-CBC 128-bit key */
  11634. WOLFSSL_SMALL_STACK_STATIC const byte k4[] =
  11635. {
  11636. 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
  11637. 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
  11638. };
  11639. WOLFSSL_SMALL_STACK_STATIC const byte c4[] =
  11640. {
  11641. 0x16, 0x07, 0xCF, 0x49, 0x4B, 0x36, 0xBB, 0xF0,
  11642. 0x0D, 0xAE, 0xB0, 0xB5, 0x03, 0xC8, 0x31, 0xAB
  11643. };
  11644. /* Test 5: Camellia-CBC 192-bit key */
  11645. WOLFSSL_SMALL_STACK_STATIC const byte k5[] =
  11646. {
  11647. 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52,
  11648. 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5,
  11649. 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B
  11650. };
  11651. WOLFSSL_SMALL_STACK_STATIC const byte c5[] =
  11652. {
  11653. 0x2A, 0x48, 0x30, 0xAB, 0x5A, 0xC4, 0xA1, 0xA2,
  11654. 0x40, 0x59, 0x55, 0xFD, 0x21, 0x95, 0xCF, 0x93
  11655. };
  11656. /* Test 6: CBC 256-bit key */
  11657. WOLFSSL_SMALL_STACK_STATIC const byte k6[] =
  11658. {
  11659. 0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE,
  11660. 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
  11661. 0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7,
  11662. 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4
  11663. };
  11664. WOLFSSL_SMALL_STACK_STATIC const byte c6[] =
  11665. {
  11666. 0xE6, 0xCF, 0xA3, 0x5F, 0xC0, 0x2B, 0x13, 0x4A,
  11667. 0x4D, 0x2C, 0x0B, 0x67, 0x37, 0xAC, 0x3E, 0xDA
  11668. };
  11669. byte out[CAMELLIA_BLOCK_SIZE];
  11670. Camellia cam;
  11671. int i, testsSz, ret;
  11672. WOLFSSL_SMALL_STACK_STATIC const test_vector_t testVectors[] =
  11673. {
  11674. {CAM_ECB_ENC, pte, ive, c1, k1, sizeof(k1), -114},
  11675. {CAM_ECB_ENC, pte, ive, c2, k2, sizeof(k2), -115},
  11676. {CAM_ECB_ENC, pte, ive, c3, k3, sizeof(k3), -116},
  11677. {CAM_ECB_DEC, pte, ive, c1, k1, sizeof(k1), -117},
  11678. {CAM_ECB_DEC, pte, ive, c2, k2, sizeof(k2), -118},
  11679. {CAM_ECB_DEC, pte, ive, c3, k3, sizeof(k3), -119},
  11680. {CAM_CBC_ENC, ptc, ivc, c4, k4, sizeof(k4), -120},
  11681. {CAM_CBC_ENC, ptc, ivc, c5, k5, sizeof(k5), -121},
  11682. {CAM_CBC_ENC, ptc, ivc, c6, k6, sizeof(k6), -122},
  11683. {CAM_CBC_DEC, ptc, ivc, c4, k4, sizeof(k4), -123},
  11684. {CAM_CBC_DEC, ptc, ivc, c5, k5, sizeof(k5), -124},
  11685. {CAM_CBC_DEC, ptc, ivc, c6, k6, sizeof(k6), -125}
  11686. };
  11687. testsSz = sizeof(testVectors)/sizeof(test_vector_t);
  11688. for (i = 0; i < testsSz; i++) {
  11689. if (wc_CamelliaSetKey(&cam, testVectors[i].key, testVectors[i].keySz,
  11690. testVectors[i].iv) != 0)
  11691. return testVectors[i].errorCode;
  11692. switch (testVectors[i].type) {
  11693. case CAM_ECB_ENC:
  11694. ret = wc_CamelliaEncryptDirect(&cam, out,
  11695. testVectors[i].plaintext);
  11696. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  11697. CAMELLIA_BLOCK_SIZE))
  11698. return testVectors[i].errorCode;
  11699. break;
  11700. case CAM_ECB_DEC:
  11701. ret = wc_CamelliaDecryptDirect(&cam, out,
  11702. testVectors[i].ciphertext);
  11703. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  11704. CAMELLIA_BLOCK_SIZE))
  11705. return testVectors[i].errorCode;
  11706. break;
  11707. case CAM_CBC_ENC:
  11708. ret = wc_CamelliaCbcEncrypt(&cam, out, testVectors[i].plaintext,
  11709. CAMELLIA_BLOCK_SIZE);
  11710. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  11711. CAMELLIA_BLOCK_SIZE))
  11712. return testVectors[i].errorCode;
  11713. break;
  11714. case CAM_CBC_DEC:
  11715. ret = wc_CamelliaCbcDecrypt(&cam, out,
  11716. testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE);
  11717. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  11718. CAMELLIA_BLOCK_SIZE))
  11719. return testVectors[i].errorCode;
  11720. break;
  11721. default:
  11722. break;
  11723. }
  11724. }
  11725. /* Setting the IV and checking it was actually set. */
  11726. ret = wc_CamelliaSetIV(&cam, ivc);
  11727. if (ret != 0)
  11728. return WC_TEST_RET_ENC_EC(ret);
  11729. if (XMEMCMP(cam.reg, ivc, CAMELLIA_BLOCK_SIZE) != 0)
  11730. return WC_TEST_RET_ENC_NC;
  11731. /* Setting the IV to NULL should be same as all zeros IV */
  11732. ret = wc_CamelliaSetIV(&cam, NULL);
  11733. if (ret != 0)
  11734. return WC_TEST_RET_ENC_EC(ret);
  11735. if (XMEMCMP(cam.reg, ive, CAMELLIA_BLOCK_SIZE) != 0)
  11736. return WC_TEST_RET_ENC_NC;
  11737. /* First parameter should never be null */
  11738. if (wc_CamelliaSetIV(NULL, NULL) == 0)
  11739. return WC_TEST_RET_ENC_NC;
  11740. /* First parameter should never be null, check it fails */
  11741. if (wc_CamelliaSetKey(NULL, k1, sizeof(k1), NULL) == 0)
  11742. return WC_TEST_RET_ENC_NC;
  11743. /* Key should have a size of 16, 24, or 32 */
  11744. if (wc_CamelliaSetKey(&cam, k1, 0, NULL) == 0)
  11745. return WC_TEST_RET_ENC_NC;
  11746. return 0;
  11747. }
  11748. #endif /* HAVE_CAMELLIA */
  11749. #ifdef WOLFSSL_SM4
  11750. #ifdef WOLFSSL_SM4_ECB
  11751. static int sm4_ecb_test(void)
  11752. {
  11753. /* draft-ribose-cfrg-sm4-10 A.2.1.1 */
  11754. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  11755. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  11756. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  11757. };
  11758. WOLFSSL_SMALL_STACK_STATIC const byte p1[] = {
  11759. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB,
  11760. 0xCC, 0xCC, 0xCC, 0xCC, 0xDD, 0xDD, 0xDD, 0xDD,
  11761. 0xEE, 0xEE, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF, 0xFF,
  11762. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB
  11763. };
  11764. WOLFSSL_SMALL_STACK_STATIC const byte c1_ecb[] = {
  11765. 0x5E, 0xC8, 0x14, 0x3D, 0xE5, 0x09, 0xCF, 0xF7,
  11766. 0xB5, 0x17, 0x9F, 0x8F, 0x47, 0x4B, 0x86, 0x19,
  11767. 0x2F, 0x1D, 0x30, 0x5A, 0x7F, 0xB1, 0x7D, 0xF9,
  11768. 0x85, 0xF8, 0x1C, 0x84, 0x82, 0x19, 0x23, 0x04
  11769. };
  11770. wc_Sm4 sm4;
  11771. byte enc[SM4_BLOCK_SIZE * 4];
  11772. byte dec[SM4_BLOCK_SIZE * 4];
  11773. int ret;
  11774. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  11775. if (ret != 0)
  11776. return WC_TEST_RET_ENC_EC(ret);
  11777. /* Encrypt and decrypt with ECB. */
  11778. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  11779. if (ret != 0)
  11780. return WC_TEST_RET_ENC_EC(ret);
  11781. ret = wc_Sm4EcbEncrypt(&sm4, enc, p1, sizeof(p1));
  11782. if (ret != 0)
  11783. return WC_TEST_RET_ENC_EC(ret);
  11784. if (XMEMCMP(enc, c1_ecb, sizeof(c1_ecb)) != 0)
  11785. return WC_TEST_RET_ENC_NC;
  11786. ret = wc_Sm4EcbDecrypt(&sm4, dec, enc, sizeof(c1_ecb));
  11787. if (ret != 0)
  11788. return WC_TEST_RET_ENC_EC(ret);
  11789. if (XMEMCMP(dec, p1, sizeof(p1)) != 0)
  11790. return WC_TEST_RET_ENC_NC;
  11791. wc_Sm4Free(&sm4);
  11792. return 0;
  11793. }
  11794. #endif
  11795. #ifdef WOLFSSL_SM4_CBC
  11796. static int sm4_cbc_test(void)
  11797. {
  11798. /* draft-ribose-cfrg-sm4-10 A.2.2.1 */
  11799. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  11800. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  11801. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  11802. };
  11803. WOLFSSL_SMALL_STACK_STATIC const byte p1[] = {
  11804. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB,
  11805. 0xCC, 0xCC, 0xCC, 0xCC, 0xDD, 0xDD, 0xDD, 0xDD,
  11806. 0xEE, 0xEE, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF, 0xFF,
  11807. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB
  11808. };
  11809. WOLFSSL_SMALL_STACK_STATIC const byte i1[] = {
  11810. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  11811. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  11812. };
  11813. WOLFSSL_SMALL_STACK_STATIC const byte c1_cbc[] = {
  11814. 0x78, 0xEB, 0xB1, 0x1C, 0xC4, 0x0B, 0x0A, 0x48,
  11815. 0x31, 0x2A, 0xAE, 0xB2, 0x04, 0x02, 0x44, 0xCB,
  11816. 0x4C, 0xB7, 0x01, 0x69, 0x51, 0x90, 0x92, 0x26,
  11817. 0x97, 0x9B, 0x0D, 0x15, 0xDC, 0x6A, 0x8F, 0x6D
  11818. };
  11819. wc_Sm4 sm4;
  11820. byte enc[SM4_BLOCK_SIZE * 4];
  11821. byte dec[SM4_BLOCK_SIZE * 4];
  11822. int ret;
  11823. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  11824. if (ret != 0)
  11825. return WC_TEST_RET_ENC_EC(ret);
  11826. /* Encrypt and decrypt with CBC. */
  11827. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  11828. if (ret != 0)
  11829. return WC_TEST_RET_ENC_EC(ret);
  11830. ret = wc_Sm4SetIV(&sm4, i1);
  11831. if (ret != 0)
  11832. return WC_TEST_RET_ENC_EC(ret);
  11833. ret = wc_Sm4CbcEncrypt(&sm4, enc, p1, sizeof(p1));
  11834. if (ret != 0)
  11835. return WC_TEST_RET_ENC_EC(ret);
  11836. if (XMEMCMP(enc, c1_cbc, sizeof(c1_cbc)) != 0)
  11837. return WC_TEST_RET_ENC_NC;
  11838. ret = wc_Sm4SetIV(&sm4, i1);
  11839. if (ret != 0)
  11840. return WC_TEST_RET_ENC_EC(ret);
  11841. ret = wc_Sm4CbcDecrypt(&sm4, dec, enc, sizeof(c1_cbc));
  11842. if (ret != 0)
  11843. return WC_TEST_RET_ENC_EC(ret);
  11844. if (XMEMCMP(dec, p1, sizeof(p1)) != 0)
  11845. return WC_TEST_RET_ENC_NC;
  11846. /* Encrypt and decrypt in-place with CBC. */
  11847. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  11848. if (ret != 0)
  11849. return WC_TEST_RET_ENC_EC(ret);
  11850. ret = wc_Sm4SetIV(&sm4, i1);
  11851. if (ret != 0)
  11852. return WC_TEST_RET_ENC_EC(ret);
  11853. XMEMCPY(enc, p1, sizeof(p1));
  11854. ret = wc_Sm4CbcEncrypt(&sm4, enc, enc, sizeof(p1));
  11855. if (ret != 0)
  11856. return WC_TEST_RET_ENC_EC(ret);
  11857. if (XMEMCMP(enc, c1_cbc, sizeof(c1_cbc)) != 0)
  11858. return WC_TEST_RET_ENC_NC;
  11859. ret = wc_Sm4SetIV(&sm4, i1);
  11860. if (ret != 0)
  11861. return WC_TEST_RET_ENC_EC(ret);
  11862. ret = wc_Sm4CbcDecrypt(&sm4, enc, enc, sizeof(c1_cbc));
  11863. if (ret != 0)
  11864. return WC_TEST_RET_ENC_EC(ret);
  11865. if (XMEMCMP(enc, p1, sizeof(p1)) != 0)
  11866. return WC_TEST_RET_ENC_NC;
  11867. wc_Sm4Free(&sm4);
  11868. return 0;
  11869. }
  11870. #endif
  11871. #ifdef WOLFSSL_SM4_CTR
  11872. static int sm4_ctr_test(void)
  11873. {
  11874. /* draft-ribose-cfrg-sm4-10 A.2.5.1 */
  11875. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  11876. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  11877. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  11878. };
  11879. WOLFSSL_SMALL_STACK_STATIC const byte i1[] = {
  11880. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  11881. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  11882. };
  11883. WOLFSSL_SMALL_STACK_STATIC const byte p2[] = {
  11884. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  11885. 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  11886. 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
  11887. 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
  11888. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  11889. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  11890. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  11891. 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB
  11892. };
  11893. WOLFSSL_SMALL_STACK_STATIC const byte c2_ctr[] = {
  11894. 0xAC, 0x32, 0x36, 0xCB, 0x97, 0x0C, 0xC2, 0x07,
  11895. 0x91, 0x36, 0x4C, 0x39, 0x5A, 0x13, 0x42, 0xD1,
  11896. 0xA3, 0xCB, 0xC1, 0x87, 0x8C, 0x6F, 0x30, 0xCD,
  11897. 0x07, 0x4C, 0xCE, 0x38, 0x5C, 0xDD, 0x70, 0xC7,
  11898. 0xF2, 0x34, 0xBC, 0x0E, 0x24, 0xC1, 0x19, 0x80,
  11899. 0xFD, 0x12, 0x86, 0x31, 0x0C, 0xE3, 0x7B, 0x92,
  11900. 0x6E, 0x02, 0xFC, 0xD0, 0xFA, 0xA0, 0xBA, 0xF3,
  11901. 0x8B, 0x29, 0x33, 0x85, 0x1D, 0x82, 0x45, 0x14
  11902. };
  11903. wc_Sm4 sm4;
  11904. byte enc[SM4_BLOCK_SIZE * 4];
  11905. byte dec[SM4_BLOCK_SIZE * 4];
  11906. int chunk;
  11907. int i;
  11908. int ret;
  11909. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  11910. if (ret != 0)
  11911. return WC_TEST_RET_ENC_EC(ret);
  11912. /* Encrypt and decrypt using encrypt with CTR. */
  11913. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  11914. if (ret != 0)
  11915. return WC_TEST_RET_ENC_EC(ret);
  11916. ret = wc_Sm4SetIV(&sm4, i1);
  11917. if (ret != 0)
  11918. return WC_TEST_RET_ENC_EC(ret);
  11919. ret = wc_Sm4CtrEncrypt(&sm4, enc, p2, sizeof(p2));
  11920. if (ret != 0)
  11921. return WC_TEST_RET_ENC_EC(ret);
  11922. if (XMEMCMP(enc, c2_ctr, sizeof(c2_ctr)) != 0)
  11923. return WC_TEST_RET_ENC_NC;
  11924. ret = wc_Sm4SetIV(&sm4, i1);
  11925. if (ret != 0)
  11926. return WC_TEST_RET_ENC_EC(ret);
  11927. ret = wc_Sm4CtrEncrypt(&sm4, dec, enc, sizeof(c2_ctr));
  11928. if (ret != 0)
  11929. return WC_TEST_RET_ENC_EC(ret);
  11930. if (XMEMCMP(dec, p2, sizeof(p2)) != 0)
  11931. return WC_TEST_RET_ENC_NC;
  11932. for (chunk = 1; chunk <= SM4_BLOCK_SIZE + 1; chunk++) {
  11933. ret = wc_Sm4SetIV(&sm4, i1);
  11934. if (ret != 0)
  11935. return WC_TEST_RET_ENC_I(chunk);
  11936. XMEMSET(enc, 0, sizeof(enc));
  11937. for (i = 0; i + chunk <= (int)sizeof(p2); i += chunk) {
  11938. ret = wc_Sm4CtrEncrypt(&sm4, enc + i, p2 + i, chunk);
  11939. if (ret != 0)
  11940. return WC_TEST_RET_ENC_I(i);
  11941. }
  11942. if (i < (int)sizeof(p2)) {
  11943. ret = wc_Sm4CtrEncrypt(&sm4, enc + i, p2 + i, sizeof(p2) - i);
  11944. if (ret != 0)
  11945. return WC_TEST_RET_ENC_I(chunk);
  11946. }
  11947. if (XMEMCMP(enc, c2_ctr, sizeof(c2_ctr)) != 0)
  11948. return WC_TEST_RET_ENC_I(chunk);
  11949. }
  11950. wc_Sm4Free(&sm4);
  11951. return 0;
  11952. }
  11953. #endif
  11954. #ifdef WOLFSSL_SM4_GCM
  11955. static int sm4_gcm_test(void)
  11956. {
  11957. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  11958. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  11959. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  11960. };
  11961. WOLFSSL_SMALL_STACK_STATIC const byte p1[] = {
  11962. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB,
  11963. 0xCC, 0xCC, 0xCC, 0xCC, 0xDD, 0xDD, 0xDD, 0xDD,
  11964. 0xEE, 0xEE, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF, 0xFF,
  11965. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB
  11966. };
  11967. WOLFSSL_SMALL_STACK_STATIC const byte i1[] = {
  11968. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  11969. 0x08, 0x09, 0x0A, 0x0B
  11970. };
  11971. WOLFSSL_SMALL_STACK_STATIC const byte a1[] = {
  11972. 0xFF, 0xEE, 0xDD
  11973. };
  11974. WOLFSSL_SMALL_STACK_STATIC const byte tag1[] = {
  11975. 0x83, 0xb2, 0x91, 0xcf, 0x22, 0xc9, 0x5f, 0x89,
  11976. 0xde, 0x3d, 0x52, 0x8d, 0xd7, 0x13, 0x50, 0x89
  11977. };
  11978. WOLFSSL_SMALL_STACK_STATIC const byte c1[] = {
  11979. 0xff, 0x8b, 0xb2, 0x3b, 0x0a, 0x0a, 0x12, 0xa4,
  11980. 0xa8, 0x4c, 0x4f, 0x67, 0x06, 0x81, 0xbb, 0x88,
  11981. 0x66, 0x17, 0xc7, 0x43, 0xbf, 0xae, 0x41, 0x40,
  11982. 0xec, 0x1e, 0x03, 0x85, 0x2b, 0x56, 0xa8, 0xc0
  11983. };
  11984. /* RFC8998 A.1. */
  11985. WOLFSSL_SMALL_STACK_STATIC const byte i2[] = {
  11986. 0x00, 0x00, 0x12, 0x34, 0x56, 0x78, 0x00, 0x00,
  11987. 0x00, 0x00, 0xAB, 0xCD
  11988. };
  11989. WOLFSSL_SMALL_STACK_STATIC const byte k2[] = {
  11990. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  11991. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  11992. };
  11993. WOLFSSL_SMALL_STACK_STATIC const byte p2[] = {
  11994. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  11995. 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  11996. 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
  11997. 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
  11998. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  11999. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  12000. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  12001. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA
  12002. };
  12003. WOLFSSL_SMALL_STACK_STATIC const byte a2[] = {
  12004. 0xFE, 0xED, 0xFA, 0xCE, 0xDE, 0xAD, 0xBE, 0xEF,
  12005. 0xFE, 0xED, 0xFA, 0xCE, 0xDE, 0xAD, 0xBE, 0xEF,
  12006. 0xAB, 0xAD, 0xDA, 0xD2
  12007. };
  12008. WOLFSSL_SMALL_STACK_STATIC const byte c2[] = {
  12009. 0x17, 0xF3, 0x99, 0xF0, 0x8C, 0x67, 0xD5, 0xEE,
  12010. 0x19, 0xD0, 0xDC, 0x99, 0x69, 0xC4, 0xBB, 0x7D,
  12011. 0x5F, 0xD4, 0x6F, 0xD3, 0x75, 0x64, 0x89, 0x06,
  12012. 0x91, 0x57, 0xB2, 0x82, 0xBB, 0x20, 0x07, 0x35,
  12013. 0xD8, 0x27, 0x10, 0xCA, 0x5C, 0x22, 0xF0, 0xCC,
  12014. 0xFA, 0x7C, 0xBF, 0x93, 0xD4, 0x96, 0xAC, 0x15,
  12015. 0xA5, 0x68, 0x34, 0xCB, 0xCF, 0x98, 0xC3, 0x97,
  12016. 0xB4, 0x02, 0x4A, 0x26, 0x91, 0x23, 0x3B, 0x8D
  12017. };
  12018. WOLFSSL_SMALL_STACK_STATIC const byte tag2[] = {
  12019. 0x83, 0xDE, 0x35, 0x41, 0xE4, 0xC2, 0xB5, 0x81,
  12020. 0x77, 0xE0, 0x65, 0xA9, 0xBF, 0x7B, 0x62, 0xEC
  12021. };
  12022. wc_Sm4 sm4;
  12023. byte enc[SM4_BLOCK_SIZE * 4];
  12024. byte dec[SM4_BLOCK_SIZE * 4];
  12025. byte tag[SM4_BLOCK_SIZE];
  12026. int ret;
  12027. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  12028. if (ret != 0)
  12029. return WC_TEST_RET_ENC_EC(ret);
  12030. /* Encrypt and decrypt using encrypt with GCM. */
  12031. ret = wc_Sm4GcmSetKey(&sm4, k1, sizeof(k1));
  12032. if (ret != 0)
  12033. return WC_TEST_RET_ENC_EC(ret);
  12034. ret = wc_Sm4GcmEncrypt(&sm4, enc, p1, sizeof(p1), i1, sizeof(i1), tag,
  12035. sizeof(tag), a1, sizeof(a1));
  12036. if (ret != 0)
  12037. return WC_TEST_RET_ENC_EC(ret);
  12038. if (XMEMCMP(enc, c1, sizeof(c1)) != 0)
  12039. return WC_TEST_RET_ENC_NC;
  12040. if (XMEMCMP(tag, tag1, sizeof(tag1)) != 0)
  12041. return WC_TEST_RET_ENC_NC;
  12042. ret = wc_Sm4GcmDecrypt(&sm4, dec, enc, sizeof(c1), i1, sizeof(i1), tag,
  12043. sizeof(tag), a1, sizeof(a1));
  12044. if (ret != 0)
  12045. return WC_TEST_RET_ENC_EC(ret);
  12046. if (XMEMCMP(dec, p1, sizeof(p1)) != 0)
  12047. return WC_TEST_RET_ENC_NC;
  12048. /* RFC8998 test vector. */
  12049. ret = wc_Sm4GcmSetKey(&sm4, k2, sizeof(k2));
  12050. if (ret != 0)
  12051. return WC_TEST_RET_ENC_EC(ret);
  12052. ret = wc_Sm4GcmEncrypt(&sm4, enc, p2, sizeof(p2), i2, sizeof(i2), tag,
  12053. sizeof(tag), a2, sizeof(a2));
  12054. if (ret != 0)
  12055. return WC_TEST_RET_ENC_EC(ret);
  12056. if (XMEMCMP(enc, c2, sizeof(c2)) != 0)
  12057. return WC_TEST_RET_ENC_NC;
  12058. if (XMEMCMP(tag, tag2, sizeof(tag2)) != 0)
  12059. return WC_TEST_RET_ENC_NC;
  12060. ret = wc_Sm4GcmDecrypt(&sm4, dec, enc, sizeof(c2), i2, sizeof(i2), tag,
  12061. sizeof(tag), a2, sizeof(a2));
  12062. if (ret != 0)
  12063. return WC_TEST_RET_ENC_EC(ret);
  12064. if (XMEMCMP(dec, p2, sizeof(p2)) != 0)
  12065. return WC_TEST_RET_ENC_NC;
  12066. wc_Sm4Free(&sm4);
  12067. return 0;
  12068. }
  12069. #endif
  12070. #ifdef WOLFSSL_SM4_CCM
  12071. static int sm4_ccm_test(void)
  12072. {
  12073. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  12074. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  12075. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  12076. };
  12077. WOLFSSL_SMALL_STACK_STATIC const byte p1[] = {
  12078. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB,
  12079. 0xCC, 0xCC, 0xCC, 0xCC, 0xDD, 0xDD, 0xDD, 0xDD,
  12080. 0xEE, 0xEE, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF, 0xFF,
  12081. 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB
  12082. };
  12083. WOLFSSL_SMALL_STACK_STATIC const byte i1[] = {
  12084. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  12085. 0x08, 0x09, 0x0A, 0x0B
  12086. };
  12087. WOLFSSL_SMALL_STACK_STATIC const byte a1[] = {
  12088. 0xFF, 0xEE, 0xDD
  12089. };
  12090. WOLFSSL_SMALL_STACK_STATIC const byte tag1[] = {
  12091. 0x9a, 0x98, 0x04, 0xb6, 0x0f, 0x19, 0x4a, 0x46,
  12092. 0xba, 0xed, 0xe6, 0x89, 0x69, 0x34, 0xad, 0x61
  12093. };
  12094. WOLFSSL_SMALL_STACK_STATIC const byte c1[] = {
  12095. 0xbd, 0xc0, 0x72, 0x60, 0xda, 0x2d, 0x11, 0xdc,
  12096. 0x66, 0x33, 0xcc, 0xec, 0xb2, 0xf4, 0x53, 0x59,
  12097. 0x9e, 0xb1, 0xb3, 0x6b, 0x1f, 0x1c, 0xfb, 0x29,
  12098. 0xf5, 0x37, 0xfc, 0x00, 0xf2, 0x4e, 0x70, 0x6f
  12099. };
  12100. /* RFC8998 A.1. */
  12101. WOLFSSL_SMALL_STACK_STATIC const byte i2[] = {
  12102. 0x00, 0x00, 0x12, 0x34, 0x56, 0x78, 0x00, 0x00,
  12103. 0x00, 0x00, 0xAB, 0xCD
  12104. };
  12105. WOLFSSL_SMALL_STACK_STATIC const byte k2[] = {
  12106. 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
  12107. 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10
  12108. };
  12109. WOLFSSL_SMALL_STACK_STATIC const byte p2[] = {
  12110. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  12111. 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  12112. 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
  12113. 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
  12114. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  12115. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  12116. 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE,
  12117. 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA
  12118. };
  12119. WOLFSSL_SMALL_STACK_STATIC const byte a2[] = {
  12120. 0xFE, 0xED, 0xFA, 0xCE, 0xDE, 0xAD, 0xBE, 0xEF,
  12121. 0xFE, 0xED, 0xFA, 0xCE, 0xDE, 0xAD, 0xBE, 0xEF,
  12122. 0xAB, 0xAD, 0xDA, 0xD2
  12123. };
  12124. WOLFSSL_SMALL_STACK_STATIC const byte c2[] = {
  12125. 0x48, 0xAF, 0x93, 0x50, 0x1F, 0xA6, 0x2A, 0xDB,
  12126. 0xCD, 0x41, 0x4C, 0xCE, 0x60, 0x34, 0xD8, 0x95,
  12127. 0xDD, 0xA1, 0xBF, 0x8F, 0x13, 0x2F, 0x04, 0x20,
  12128. 0x98, 0x66, 0x15, 0x72, 0xE7, 0x48, 0x30, 0x94,
  12129. 0xFD, 0x12, 0xE5, 0x18, 0xCE, 0x06, 0x2C, 0x98,
  12130. 0xAC, 0xEE, 0x28, 0xD9, 0x5D, 0xF4, 0x41, 0x6B,
  12131. 0xED, 0x31, 0xA2, 0xF0, 0x44, 0x76, 0xC1, 0x8B,
  12132. 0xB4, 0x0C, 0x84, 0xA7, 0x4B, 0x97, 0xDC, 0x5B
  12133. };
  12134. WOLFSSL_SMALL_STACK_STATIC const byte tag2[] = {
  12135. 0x16, 0x84, 0x2D, 0x4F, 0xA1, 0x86, 0xF5, 0x6A,
  12136. 0xB3, 0x32, 0x56, 0x97, 0x1F, 0xA1, 0x10, 0xF4
  12137. };
  12138. wc_Sm4 sm4;
  12139. byte enc[SM4_BLOCK_SIZE * 4];
  12140. byte dec[SM4_BLOCK_SIZE * 4];
  12141. byte tag[SM4_BLOCK_SIZE];
  12142. int ret;
  12143. ret = wc_Sm4Init(&sm4, NULL, INVALID_DEVID);
  12144. if (ret != 0)
  12145. return -6720;
  12146. /* Encrypt and decrypt using encrypt with CCM. */
  12147. ret = wc_Sm4SetKey(&sm4, k1, sizeof(k1));
  12148. if (ret != 0)
  12149. return WC_TEST_RET_ENC_EC(ret);
  12150. ret = wc_Sm4CcmEncrypt(&sm4, enc, p1, sizeof(p1), i1, sizeof(i1), tag,
  12151. sizeof(tag), a1, sizeof(a1));
  12152. if (ret != 0)
  12153. return WC_TEST_RET_ENC_EC(ret);
  12154. if (XMEMCMP(enc, c1, sizeof(c1)) != 0)
  12155. return WC_TEST_RET_ENC_NC;
  12156. if (XMEMCMP(tag, tag1, sizeof(tag1)) != 0)
  12157. return WC_TEST_RET_ENC_NC;
  12158. ret = wc_Sm4CcmDecrypt(&sm4, dec, enc, sizeof(c1), i1, sizeof(i1), tag,
  12159. sizeof(tag), a1, sizeof(a1));
  12160. if (ret != 0)
  12161. return WC_TEST_RET_ENC_EC(ret);
  12162. if (XMEMCMP(dec, p1, sizeof(p1)) != 0)
  12163. return WC_TEST_RET_ENC_NC;
  12164. /* RFC8998 test vector. */
  12165. ret = wc_Sm4SetKey(&sm4, k2, sizeof(k2));
  12166. if (ret != 0)
  12167. return WC_TEST_RET_ENC_EC(ret);
  12168. ret = wc_Sm4CcmEncrypt(&sm4, enc, p2, sizeof(p2), i2, sizeof(i2), tag,
  12169. sizeof(tag), a2, sizeof(a2));
  12170. if (ret != 0)
  12171. return WC_TEST_RET_ENC_EC(ret);
  12172. if (XMEMCMP(enc, c2, sizeof(c2)) != 0)
  12173. return WC_TEST_RET_ENC_NC;
  12174. if (XMEMCMP(tag, tag2, sizeof(tag2)) != 0)
  12175. return WC_TEST_RET_ENC_NC;
  12176. ret = wc_Sm4CcmDecrypt(&sm4, dec, enc, sizeof(c2), i2, sizeof(i2), tag,
  12177. sizeof(tag), a2, sizeof(a2));
  12178. if (ret != 0)
  12179. return WC_TEST_RET_ENC_EC(ret);
  12180. if (XMEMCMP(dec, p2, sizeof(p2)) != 0)
  12181. return WC_TEST_RET_ENC_NC;
  12182. wc_Sm4Free(&sm4);
  12183. return 0;
  12184. }
  12185. #endif
  12186. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sm4_test(void)
  12187. {
  12188. wc_test_ret_t ret;
  12189. #ifdef WOLFSSL_SM4_ECB
  12190. ret = sm4_ecb_test();
  12191. if (ret != 0)
  12192. return ret;
  12193. #endif
  12194. #ifdef WOLFSSL_SM4_CBC
  12195. ret = sm4_cbc_test();
  12196. if (ret != 0)
  12197. return ret;
  12198. #endif
  12199. #ifdef WOLFSSL_SM4_CTR
  12200. ret = sm4_ctr_test();
  12201. if (ret != 0)
  12202. return ret;
  12203. #endif
  12204. #ifdef WOLFSSL_SM4_GCM
  12205. ret = sm4_gcm_test();
  12206. if (ret != 0)
  12207. return ret;
  12208. #endif
  12209. #ifdef WOLFSSL_SM4_CCM
  12210. ret = sm4_ccm_test();
  12211. if (ret != 0)
  12212. return ret;
  12213. #endif
  12214. return 0;
  12215. }
  12216. #endif
  12217. #ifdef HAVE_XCHACHA
  12218. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t XChaCha_test(void) {
  12219. wc_test_ret_t ret;
  12220. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  12221. 0x54, 0x68, 0x65, 0x20, 0x64, 0x68, 0x6f, 0x6c, 0x65, 0x20, 0x28, 0x70, 0x72, 0x6f, 0x6e, 0x6f, /* The dhole (prono */
  12222. 0x75, 0x6e, 0x63, 0x65, 0x64, 0x20, 0x22, 0x64, 0x6f, 0x6c, 0x65, 0x22, 0x29, 0x20, 0x69, 0x73, /* unced "dole") is */
  12223. 0x20, 0x61, 0x6c, 0x73, 0x6f, 0x20, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x20, 0x61, 0x73, 0x20, 0x74, /* also known as t */
  12224. 0x68, 0x65, 0x20, 0x41, 0x73, 0x69, 0x61, 0x74, 0x69, 0x63, 0x20, 0x77, 0x69, 0x6c, 0x64, 0x20, /* he Asiatic wild */
  12225. 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x72, 0x65, 0x64, 0x20, 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x61, 0x6e, /* dog, red dog, an */
  12226. 0x64, 0x20, 0x77, 0x68, 0x69, 0x73, 0x74, 0x6c, 0x69, 0x6e, 0x67, 0x20, 0x64, 0x6f, 0x67, 0x2e, /* d whistling dog. */
  12227. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x61, 0x62, 0x6f, 0x75, 0x74, 0x20, 0x74, 0x68, 0x65, /* It is about the */
  12228. 0x20, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x6f, 0x66, 0x20, 0x61, 0x20, 0x47, 0x65, 0x72, 0x6d, 0x61, /* size of a Germa */
  12229. 0x6e, 0x20, 0x73, 0x68, 0x65, 0x70, 0x68, 0x65, 0x72, 0x64, 0x20, 0x62, 0x75, 0x74, 0x20, 0x6c, /* n shepherd but l */
  12230. 0x6f, 0x6f, 0x6b, 0x73, 0x20, 0x6d, 0x6f, 0x72, 0x65, 0x20, 0x6c, 0x69, 0x6b, 0x65, 0x20, 0x61, /* ooks more like a */
  12231. 0x20, 0x6c, 0x6f, 0x6e, 0x67, 0x2d, 0x6c, 0x65, 0x67, 0x67, 0x65, 0x64, 0x20, 0x66, 0x6f, 0x78, /* long-legged fox */
  12232. 0x2e, 0x20, 0x54, 0x68, 0x69, 0x73, 0x20, 0x68, 0x69, 0x67, 0x68, 0x6c, 0x79, 0x20, 0x65, 0x6c, /* . This highly el */
  12233. 0x75, 0x73, 0x69, 0x76, 0x65, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x6b, 0x69, 0x6c, 0x6c, 0x65, /* usive and skille */
  12234. 0x64, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x65, 0x72, 0x20, 0x69, 0x73, 0x20, 0x63, 0x6c, 0x61, 0x73, /* d jumper is clas */
  12235. 0x73, 0x69, 0x66, 0x69, 0x65, 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x77, 0x6f, 0x6c, 0x76, /* sified with wolv */
  12236. 0x65, 0x73, 0x2c, 0x20, 0x63, 0x6f, 0x79, 0x6f, 0x74, 0x65, 0x73, 0x2c, 0x20, 0x6a, 0x61, 0x63, /* es, coyotes, jac */
  12237. 0x6b, 0x61, 0x6c, 0x73, 0x2c, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x66, 0x6f, 0x78, 0x65, 0x73, 0x20, /* kals, and foxes */
  12238. 0x69, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74, 0x61, 0x78, 0x6f, 0x6e, 0x6f, 0x6d, 0x69, 0x63, /* in the taxonomic */
  12239. 0x20, 0x66, 0x61, 0x6d, 0x69, 0x6c, 0x79, 0x20, 0x43, 0x61, 0x6e, 0x69, 0x64, 0x61, 0x65, 0x2e /* family Canidae. */
  12240. };
  12241. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  12242. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  12243. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  12244. };
  12245. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  12246. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  12247. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x58 }; /* PQRSTUVW */
  12248. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  12249. 0x45, 0x59, 0xab, 0xba, 0x4e, 0x48, 0xc1, 0x61, 0x02, 0xe8, 0xbb, 0x2c, 0x05, 0xe6, 0x94, 0x7f,
  12250. 0x50, 0xa7, 0x86, 0xde, 0x16, 0x2f, 0x9b, 0x0b, 0x7e, 0x59, 0x2a, 0x9b, 0x53, 0xd0, 0xd4, 0xe9,
  12251. 0x8d, 0x8d, 0x64, 0x10, 0xd5, 0x40, 0xa1, 0xa6, 0x37, 0x5b, 0x26, 0xd8, 0x0d, 0xac, 0xe4, 0xfa,
  12252. 0xb5, 0x23, 0x84, 0xc7, 0x31, 0xac, 0xbf, 0x16, 0xa5, 0x92, 0x3c, 0x0c, 0x48, 0xd3, 0x57, 0x5d,
  12253. 0x4d, 0x0d, 0x2c, 0x67, 0x3b, 0x66, 0x6f, 0xaa, 0x73, 0x10, 0x61, 0x27, 0x77, 0x01, 0x09, 0x3a,
  12254. 0x6b, 0xf7, 0xa1, 0x58, 0xa8, 0x86, 0x42, 0x92, 0xa4, 0x1c, 0x48, 0xe3, 0xa9, 0xb4, 0xc0, 0xda,
  12255. 0xec, 0xe0, 0xf8, 0xd9, 0x8d, 0x0d, 0x7e, 0x05, 0xb3, 0x7a, 0x30, 0x7b, 0xbb, 0x66, 0x33, 0x31,
  12256. 0x64, 0xec, 0x9e, 0x1b, 0x24, 0xea, 0x0d, 0x6c, 0x3f, 0xfd, 0xdc, 0xec, 0x4f, 0x68, 0xe7, 0x44,
  12257. 0x30, 0x56, 0x19, 0x3a, 0x03, 0xc8, 0x10, 0xe1, 0x13, 0x44, 0xca, 0x06, 0xd8, 0xed, 0x8a, 0x2b,
  12258. 0xfb, 0x1e, 0x8d, 0x48, 0xcf, 0xa6, 0xbc, 0x0e, 0xb4, 0xe2, 0x46, 0x4b, 0x74, 0x81, 0x42, 0x40,
  12259. 0x7c, 0x9f, 0x43, 0x1a, 0xee, 0x76, 0x99, 0x60, 0xe1, 0x5b, 0xa8, 0xb9, 0x68, 0x90, 0x46, 0x6e,
  12260. 0xf2, 0x45, 0x75, 0x99, 0x85, 0x23, 0x85, 0xc6, 0x61, 0xf7, 0x52, 0xce, 0x20, 0xf9, 0xda, 0x0c,
  12261. 0x09, 0xab, 0x6b, 0x19, 0xdf, 0x74, 0xe7, 0x6a, 0x95, 0x96, 0x74, 0x46, 0xf8, 0xd0, 0xfd, 0x41,
  12262. 0x5e, 0x7b, 0xee, 0x2a, 0x12, 0xa1, 0x14, 0xc2, 0x0e, 0xb5, 0x29, 0x2a, 0xe7, 0xa3, 0x49, 0xae,
  12263. 0x57, 0x78, 0x20, 0xd5, 0x52, 0x0a, 0x1f, 0x3f, 0xb6, 0x2a, 0x17, 0xce, 0x6a, 0x7e, 0x68, 0xfa,
  12264. 0x7c, 0x79, 0x11, 0x1d, 0x88, 0x60, 0x92, 0x0b, 0xc0, 0x48, 0xef, 0x43, 0xfe, 0x84, 0x48, 0x6c,
  12265. 0xcb, 0x87, 0xc2, 0x5f, 0x0a, 0xe0, 0x45, 0xf0, 0xcc, 0xe1, 0xe7, 0x98, 0x9a, 0x9a, 0xa2, 0x20,
  12266. 0xa2, 0x8b, 0xdd, 0x48, 0x27, 0xe7, 0x51, 0xa2, 0x4a, 0x6d, 0x5c, 0x62, 0xd7, 0x90, 0xa6, 0x63,
  12267. 0x93, 0xb9, 0x31, 0x11, 0xc1, 0xa5, 0x5d, 0xd7, 0x42, 0x1a, 0x10, 0x18, 0x49, 0x74, 0xc7, 0xc5
  12268. };
  12269. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12270. struct ChaCha *chacha = (struct ChaCha *)XMALLOC(sizeof *chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  12271. byte *buf1 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12272. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12273. if ((chacha == NULL) || (buf1 == NULL) || (buf2 == NULL))
  12274. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12275. #else
  12276. struct ChaCha chacha[1];
  12277. byte buf1[sizeof Plaintext];
  12278. byte buf2[sizeof Plaintext];
  12279. #endif
  12280. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  12281. if (ret < 0)
  12282. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12283. ret = wc_Chacha_Process(chacha, buf1, Plaintext, sizeof Plaintext);
  12284. if (ret < 0)
  12285. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12286. if (XMEMCMP(buf1, Ciphertext, sizeof Plaintext))
  12287. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12288. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  12289. if (ret < 0)
  12290. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12291. ret = wc_Chacha_Process(chacha, buf2, buf1, sizeof Plaintext);
  12292. if (ret < 0)
  12293. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12294. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  12295. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12296. out:
  12297. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12298. if (chacha)
  12299. XFREE(chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  12300. if (buf1)
  12301. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12302. if (buf2)
  12303. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12304. #endif
  12305. return ret;
  12306. }
  12307. #endif /* HAVE_XCHACHA */
  12308. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  12309. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t XChaCha20Poly1305_test(void) {
  12310. wc_test_ret_t ret;
  12311. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  12312. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c, /* Ladies and Gentl */
  12313. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73, /* emen of the clas */
  12314. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39, 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63, /* s of '99: If I c */
  12315. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66, 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f, /* ould offer you o */
  12316. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20, 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20, /* nly one tip for */
  12317. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75, 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73, /* the future, suns */
  12318. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69, /* creen would be i */
  12319. 0x74, 0x2e }; /* t. */
  12320. WOLFSSL_SMALL_STACK_STATIC const byte AAD[] = { 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7 }; /* PQRS........ */
  12321. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  12322. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  12323. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  12324. };
  12325. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  12326. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  12327. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57 }; /* PQRSTUVW */
  12328. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  12329. 0xbd, 0x6d, 0x17, 0x9d, 0x3e, 0x83, 0xd4, 0x3b, 0x95, 0x76, 0x57, 0x94, 0x93, 0xc0, 0xe9, 0x39,
  12330. 0x57, 0x2a, 0x17, 0x00, 0x25, 0x2b, 0xfa, 0xcc, 0xbe, 0xd2, 0x90, 0x2c, 0x21, 0x39, 0x6c, 0xbb,
  12331. 0x73, 0x1c, 0x7f, 0x1b, 0x0b, 0x4a, 0xa6, 0x44, 0x0b, 0xf3, 0xa8, 0x2f, 0x4e, 0xda, 0x7e, 0x39,
  12332. 0xae, 0x64, 0xc6, 0x70, 0x8c, 0x54, 0xc2, 0x16, 0xcb, 0x96, 0xb7, 0x2e, 0x12, 0x13, 0xb4, 0x52,
  12333. 0x2f, 0x8c, 0x9b, 0xa4, 0x0d, 0xb5, 0xd9, 0x45, 0xb1, 0x1b, 0x69, 0xb9, 0x82, 0xc1, 0xbb, 0x9e,
  12334. 0x3f, 0x3f, 0xac, 0x2b, 0xc3, 0x69, 0x48, 0x8f, 0x76, 0xb2, 0x38, 0x35, 0x65, 0xd3, 0xff, 0xf9,
  12335. 0x21, 0xf9, 0x66, 0x4c, 0x97, 0x63, 0x7d, 0xa9, 0x76, 0x88, 0x12, 0xf6, 0x15, 0xc6, 0x8b, 0x13,
  12336. 0xb5, 0x2e };
  12337. WOLFSSL_SMALL_STACK_STATIC const byte Tag[] = {
  12338. 0xc0, 0x87, 0x59, 0x24, 0xc1, 0xc7, 0x98, 0x79, 0x47, 0xde, 0xaf, 0xd8, 0x78, 0x0a, 0xcf, 0x49
  12339. };
  12340. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12341. byte *buf1 = (byte *)XMALLOC(sizeof Ciphertext + sizeof Tag, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12342. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12343. if ((buf1 == NULL) || (buf2 == NULL))
  12344. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12345. #else
  12346. byte buf1[sizeof Ciphertext + sizeof Tag];
  12347. byte buf2[sizeof Plaintext];
  12348. #endif
  12349. ret = wc_XChaCha20Poly1305_Encrypt(buf1, sizeof Ciphertext + sizeof Tag,
  12350. Plaintext, sizeof Plaintext,
  12351. AAD, sizeof AAD,
  12352. IV, sizeof IV,
  12353. Key, sizeof Key);
  12354. if (ret < 0)
  12355. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12356. if (XMEMCMP(buf1, Ciphertext, sizeof Ciphertext))
  12357. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12358. if (XMEMCMP(buf1 + sizeof Ciphertext, Tag, CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE))
  12359. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12360. ret = wc_XChaCha20Poly1305_Decrypt(buf2, sizeof Plaintext,
  12361. buf1, sizeof Ciphertext + sizeof Tag,
  12362. AAD, sizeof AAD,
  12363. IV, sizeof IV,
  12364. Key, sizeof Key);
  12365. if (ret < 0)
  12366. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12367. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  12368. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12369. out:
  12370. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  12371. if (buf1 != NULL)
  12372. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12373. if (buf2 != NULL)
  12374. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12375. #endif
  12376. return ret;
  12377. }
  12378. #endif /* defined(HAVE_XCHACHA) && defined(HAVE_POLY1305) */
  12379. #ifndef WC_NO_RNG
  12380. static wc_test_ret_t _rng_test(WC_RNG* rng, int errorOffset)
  12381. {
  12382. byte block[32];
  12383. wc_test_ret_t ret;
  12384. int i;
  12385. XMEMSET(block, 0, sizeof(block));
  12386. ret = wc_RNG_GenerateBlock(rng, block, sizeof(block));
  12387. if (ret != 0) {
  12388. ret = 1;
  12389. goto exit;
  12390. }
  12391. /* Check for 0's */
  12392. for (i=0; i<(int)sizeof(block); i++) {
  12393. if (block[i] == 0) {
  12394. ret++;
  12395. }
  12396. }
  12397. /* All zeros count check */
  12398. if (ret >= (int)sizeof(block)) {
  12399. ret = 2;
  12400. goto exit;
  12401. }
  12402. ret = wc_RNG_GenerateByte(rng, block);
  12403. if (ret != 0) {
  12404. ret = 3;
  12405. goto exit;
  12406. }
  12407. /* Parameter validation testing. */
  12408. ret = wc_RNG_GenerateBlock(NULL, block, sizeof(block));
  12409. if (ret != BAD_FUNC_ARG) {
  12410. ret = 4;
  12411. goto exit;
  12412. }
  12413. ret = wc_RNG_GenerateBlock(rng, NULL, sizeof(block));
  12414. if (ret != BAD_FUNC_ARG) {
  12415. ret = 5;
  12416. goto exit;
  12417. }
  12418. ret = wc_RNG_GenerateByte(NULL, block);
  12419. if (ret != BAD_FUNC_ARG) {
  12420. ret = 6;
  12421. goto exit;
  12422. }
  12423. ret = wc_RNG_GenerateByte(rng, NULL);
  12424. if (ret != BAD_FUNC_ARG) {
  12425. ret = 7;
  12426. goto exit;
  12427. }
  12428. ret = 0;
  12429. exit:
  12430. if (ret != 0)
  12431. ret = errorOffset - (ret * 1000000);
  12432. return ret;
  12433. }
  12434. static wc_test_ret_t random_rng_test(void)
  12435. {
  12436. WC_RNG localRng;
  12437. WC_RNG* rng;
  12438. wc_test_ret_t ret;
  12439. rng = &localRng;
  12440. /* Test stack based RNG. */
  12441. #ifndef HAVE_FIPS
  12442. ret = wc_InitRng_ex(rng, HEAP_HINT, devId);
  12443. #else
  12444. ret = wc_InitRng(rng);
  12445. #endif
  12446. if (ret != 0)
  12447. return WC_TEST_RET_ENC_EC(ret);
  12448. ret = _rng_test(rng, WC_TEST_RET_ENC_NC);
  12449. /* Make sure and free RNG */
  12450. wc_FreeRng(rng);
  12451. if (ret != 0)
  12452. return ret;
  12453. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC)
  12454. {
  12455. byte nonce[8] = { 0 };
  12456. /* Test dynamic RNG. */
  12457. rng = wc_rng_new(nonce, (word32)sizeof(nonce), HEAP_HINT);
  12458. if (rng == NULL)
  12459. return WC_TEST_RET_ENC_ERRNO;
  12460. ret = _rng_test(rng, WC_TEST_RET_ENC_NC);
  12461. wc_rng_free(rng);
  12462. }
  12463. #endif
  12464. return ret;
  12465. }
  12466. #if defined(HAVE_HASHDRBG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  12467. #ifdef WC_RNG_SEED_CB
  12468. static int seed_cb(OS_Seed* os, byte* output, word32 sz)
  12469. {
  12470. word32 i;
  12471. (void)os;
  12472. /* Known answer test. Set the seed to the same value every time. */
  12473. for (i = 0; i < sz; i++)
  12474. output[i] = (byte)i;
  12475. return 0;
  12476. }
  12477. static wc_test_ret_t rng_seed_test(void)
  12478. {
  12479. #ifndef HAVE_FIPS
  12480. WOLFSSL_SMALL_STACK_STATIC const byte check[] =
  12481. {
  12482. 0x83, 0x46, 0x65, 0x2f, 0x5c, 0x44, 0x16, 0x5f,
  12483. 0xb3, 0x89, 0x26, 0xde, 0x0b, 0x6b, 0xa2, 0x06,
  12484. 0x7e, 0xa7, 0x9a, 0x55, 0x22, 0x01, 0xb0, 0x22,
  12485. 0xf4, 0x7e, 0xa2, 0x66, 0xc4, 0x08, 0x6f, 0xba
  12486. };
  12487. #else
  12488. /* FIPS uses a longer seed, so different check value. */
  12489. WOLFSSL_SMALL_STACK_STATIC const byte check[] =
  12490. {
  12491. 0xaf, 0x31, 0xcc, 0xef, 0xa9, 0x29, 0x4c, 0x24,
  12492. 0xbd, 0xa5, 0xa3, 0x52, 0x69, 0xf3, 0xb9, 0xb2,
  12493. 0x1e, 0xd4, 0x52, 0x3b, 0x9a, 0x96, 0x06, 0x20,
  12494. 0xc0, 0x5f, 0x44, 0x06, 0x1f, 0x80, 0xdf, 0xe0
  12495. };
  12496. #endif
  12497. byte output[WC_SHA256_DIGEST_SIZE];
  12498. WC_RNG rng;
  12499. wc_test_ret_t ret;
  12500. ret = wc_SetSeed_Cb(seed_cb);
  12501. if (ret != 0) {
  12502. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12503. }
  12504. ret = wc_InitRng(&rng);
  12505. if (ret != 0) {
  12506. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12507. }
  12508. ret = wc_RNG_GenerateBlock(&rng, output, sizeof(output));
  12509. if (ret != 0) {
  12510. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12511. }
  12512. ret = XMEMCMP(output, check, sizeof(output));
  12513. if (ret != 0) {
  12514. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  12515. }
  12516. ret = wc_FreeRng(&rng);
  12517. if (ret != 0) {
  12518. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12519. }
  12520. ret = wc_SetSeed_Cb(wc_GenerateSeed);
  12521. if (ret != 0) {
  12522. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  12523. }
  12524. out:
  12525. return ret;
  12526. }
  12527. #endif
  12528. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t random_test(void)
  12529. {
  12530. WOLFSSL_SMALL_STACK_STATIC const byte test1Entropy[] =
  12531. {
  12532. 0xa6, 0x5a, 0xd0, 0xf3, 0x45, 0xdb, 0x4e, 0x0e, 0xff, 0xe8, 0x75, 0xc3,
  12533. 0xa2, 0xe7, 0x1f, 0x42, 0xc7, 0x12, 0x9d, 0x62, 0x0f, 0xf5, 0xc1, 0x19,
  12534. 0xa9, 0xef, 0x55, 0xf0, 0x51, 0x85, 0xe0, 0xfb, 0x85, 0x81, 0xf9, 0x31,
  12535. 0x75, 0x17, 0x27, 0x6e, 0x06, 0xe9, 0x60, 0x7d, 0xdb, 0xcb, 0xcc, 0x2e
  12536. };
  12537. WOLFSSL_SMALL_STACK_STATIC const byte test1Output[] =
  12538. {
  12539. 0xd3, 0xe1, 0x60, 0xc3, 0x5b, 0x99, 0xf3, 0x40, 0xb2, 0x62, 0x82, 0x64,
  12540. 0xd1, 0x75, 0x10, 0x60, 0xe0, 0x04, 0x5d, 0xa3, 0x83, 0xff, 0x57, 0xa5,
  12541. 0x7d, 0x73, 0xa6, 0x73, 0xd2, 0xb8, 0xd8, 0x0d, 0xaa, 0xf6, 0xa6, 0xc3,
  12542. 0x5a, 0x91, 0xbb, 0x45, 0x79, 0xd7, 0x3f, 0xd0, 0xc8, 0xfe, 0xd1, 0x11,
  12543. 0xb0, 0x39, 0x13, 0x06, 0x82, 0x8a, 0xdf, 0xed, 0x52, 0x8f, 0x01, 0x81,
  12544. 0x21, 0xb3, 0xfe, 0xbd, 0xc3, 0x43, 0xe7, 0x97, 0xb8, 0x7d, 0xbb, 0x63,
  12545. 0xdb, 0x13, 0x33, 0xde, 0xd9, 0xd1, 0xec, 0xe1, 0x77, 0xcf, 0xa6, 0xb7,
  12546. 0x1f, 0xe8, 0xab, 0x1d, 0xa4, 0x66, 0x24, 0xed, 0x64, 0x15, 0xe5, 0x1c,
  12547. 0xcd, 0xe2, 0xc7, 0xca, 0x86, 0xe2, 0x83, 0x99, 0x0e, 0xea, 0xeb, 0x91,
  12548. 0x12, 0x04, 0x15, 0x52, 0x8b, 0x22, 0x95, 0x91, 0x02, 0x81, 0xb0, 0x2d,
  12549. 0xd4, 0x31, 0xf4, 0xc9, 0xf7, 0x04, 0x27, 0xdf
  12550. };
  12551. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyA[] =
  12552. {
  12553. 0x63, 0x36, 0x33, 0x77, 0xe4, 0x1e, 0x86, 0x46, 0x8d, 0xeb, 0x0a, 0xb4,
  12554. 0xa8, 0xed, 0x68, 0x3f, 0x6a, 0x13, 0x4e, 0x47, 0xe0, 0x14, 0xc7, 0x00,
  12555. 0x45, 0x4e, 0x81, 0xe9, 0x53, 0x58, 0xa5, 0x69, 0x80, 0x8a, 0xa3, 0x8f,
  12556. 0x2a, 0x72, 0xa6, 0x23, 0x59, 0x91, 0x5a, 0x9f, 0x8a, 0x04, 0xca, 0x68
  12557. };
  12558. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyB[] =
  12559. {
  12560. 0xe6, 0x2b, 0x8a, 0x8e, 0xe8, 0xf1, 0x41, 0xb6, 0x98, 0x05, 0x66, 0xe3,
  12561. 0xbf, 0xe3, 0xc0, 0x49, 0x03, 0xda, 0xd4, 0xac, 0x2c, 0xdf, 0x9f, 0x22,
  12562. 0x80, 0x01, 0x0a, 0x67, 0x39, 0xbc, 0x83, 0xd3
  12563. };
  12564. WOLFSSL_SMALL_STACK_STATIC const byte test2Output[] =
  12565. {
  12566. 0x04, 0xee, 0xc6, 0x3b, 0xb2, 0x31, 0xdf, 0x2c, 0x63, 0x0a, 0x1a, 0xfb,
  12567. 0xe7, 0x24, 0x94, 0x9d, 0x00, 0x5a, 0x58, 0x78, 0x51, 0xe1, 0xaa, 0x79,
  12568. 0x5e, 0x47, 0x73, 0x47, 0xc8, 0xb0, 0x56, 0x62, 0x1c, 0x18, 0xbd, 0xdc,
  12569. 0xdd, 0x8d, 0x99, 0xfc, 0x5f, 0xc2, 0xb9, 0x20, 0x53, 0xd8, 0xcf, 0xac,
  12570. 0xfb, 0x0b, 0xb8, 0x83, 0x12, 0x05, 0xfa, 0xd1, 0xdd, 0xd6, 0xc0, 0x71,
  12571. 0x31, 0x8a, 0x60, 0x18, 0xf0, 0x3b, 0x73, 0xf5, 0xed, 0xe4, 0xd4, 0xd0,
  12572. 0x71, 0xf9, 0xde, 0x03, 0xfd, 0x7a, 0xea, 0x10, 0x5d, 0x92, 0x99, 0xb8,
  12573. 0xaf, 0x99, 0xaa, 0x07, 0x5b, 0xdb, 0x4d, 0xb9, 0xaa, 0x28, 0xc1, 0x8d,
  12574. 0x17, 0x4b, 0x56, 0xee, 0x2a, 0x01, 0x4d, 0x09, 0x88, 0x96, 0xff, 0x22,
  12575. 0x82, 0xc9, 0x55, 0xa8, 0x19, 0x69, 0xe0, 0x69, 0xfa, 0x8c, 0xe0, 0x07,
  12576. 0xa1, 0x80, 0x18, 0x3a, 0x07, 0xdf, 0xae, 0x17
  12577. };
  12578. byte output[WC_SHA256_DIGEST_SIZE * 4];
  12579. wc_test_ret_t ret;
  12580. ret = wc_RNG_HealthTest(0, test1Entropy, sizeof(test1Entropy), NULL, 0,
  12581. output, sizeof(output));
  12582. if (ret != 0)
  12583. return WC_TEST_RET_ENC_EC(ret);
  12584. if (XMEMCMP(test1Output, output, sizeof(output)) != 0)
  12585. return WC_TEST_RET_ENC_NC;
  12586. ret = wc_RNG_HealthTest(1, test2EntropyA, sizeof(test2EntropyA),
  12587. test2EntropyB, sizeof(test2EntropyB),
  12588. output, sizeof(output));
  12589. if (ret != 0)
  12590. return WC_TEST_RET_ENC_EC(ret);
  12591. if (XMEMCMP(test2Output, output, sizeof(output)) != 0)
  12592. return WC_TEST_RET_ENC_NC;
  12593. /* Basic RNG generate block test */
  12594. if ((ret = random_rng_test()) != 0)
  12595. return ret;
  12596. /* Test the seed check function. */
  12597. #if !(defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) || \
  12598. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  12599. {
  12600. word32 i, outputSz;
  12601. /* Repeat the same byte over and over. Should fail. */
  12602. outputSz = sizeof(output);
  12603. XMEMSET(output, 1, outputSz);
  12604. ret = wc_RNG_TestSeed(output, outputSz);
  12605. if (ret == 0)
  12606. return WC_TEST_RET_ENC_NC;
  12607. /* Every byte of the entropy scratch is different,
  12608. * entropy is a single byte that shouldn't match. */
  12609. outputSz = (sizeof(output) / 2) + 1;
  12610. for (i = 0; i < outputSz; i++)
  12611. output[i] = (byte)i;
  12612. ret = wc_RNG_TestSeed(output, outputSz);
  12613. if (ret != 0)
  12614. return WC_TEST_RET_ENC_EC(ret);
  12615. outputSz = sizeof(output);
  12616. for (i = 0; i < outputSz; i++)
  12617. output[i] = (byte)i;
  12618. ret = wc_RNG_TestSeed(output, outputSz);
  12619. if (ret != 0)
  12620. return WC_TEST_RET_ENC_EC(ret);
  12621. }
  12622. #endif
  12623. /* Test the seed callback. */
  12624. #ifdef WC_RNG_SEED_CB
  12625. if ((ret = rng_seed_test()) != 0)
  12626. return ret;
  12627. #endif
  12628. return 0;
  12629. }
  12630. #else
  12631. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t random_test(void)
  12632. {
  12633. /* Basic RNG generate block test */
  12634. return random_rng_test();
  12635. }
  12636. #endif /* HAVE_HASHDRBG && !CUSTOM_RAND_GENERATE_BLOCK */
  12637. #endif /* WC_NO_RNG */
  12638. #ifndef MEM_TEST_SZ
  12639. #define MEM_TEST_SZ 1024
  12640. #endif
  12641. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  12642. static int simple_mem_test(int sz)
  12643. {
  12644. int ret = 0;
  12645. byte* b;
  12646. int i;
  12647. b = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12648. if (b == NULL) {
  12649. return WC_TEST_RET_ENC_NC;
  12650. }
  12651. /* utilize memory */
  12652. for (i = 0; i < sz; i++) {
  12653. b[i] = (byte)i;
  12654. }
  12655. /* read back and verify */
  12656. for (i = 0; i < sz; i++) {
  12657. if (b[i] != (byte)i) {
  12658. ret = WC_TEST_RET_ENC_NC;
  12659. break;
  12660. }
  12661. }
  12662. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12663. return ret;
  12664. }
  12665. #endif
  12666. /* If successful, returns the first letter of the byte array `in`.
  12667. **
  12668. ** This is a deceptively simple test of a read-only embedded Linux file system.
  12669. ** (e.g CFLAGS `-mfdpic` and `-mforce-l32`) for Xtensa Linux ESP32. When the
  12670. ** `-mforce-l32` is missing, access to `in` will fail with Illegal Instruction.
  12671. ** Const is on read-only memory-mapped file system, *not* loaded in app memory.
  12672. **
  12673. ** Edit with caution. See PR #6523. */
  12674. static wc_test_ret_t const_byte_ptr_test(const byte* in, word32 *outJ)
  12675. {
  12676. wc_test_ret_t ret = 0;
  12677. volatile word32 j = -1; /* must be volatile to properly detect error */
  12678. ret = (wc_test_ret_t)*in; /* accessed *in value. */
  12679. (void)ret;
  12680. j = *outJ; /* Found index to use in const array. */
  12681. if (j == 0) {
  12682. #ifdef WOLFSSL_DEBUG
  12683. printf("Testing const byte ptr reference...\n");
  12684. #endif
  12685. /* although j is zero, in[0] does not detect the Illegal instruction */
  12686. ret = in[j]; /* The big test: can we actually access the `in` data? */
  12687. }
  12688. else {
  12689. ret = -1;
  12690. }
  12691. return ret;
  12692. }
  12693. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memory_test(void)
  12694. {
  12695. wc_test_ret_t ret = 0;
  12696. word32 j = 0; /* used in embedded const pointer test */
  12697. #if defined(COMPLEX_MEM_TEST) || defined(WOLFSSL_STATIC_MEMORY)
  12698. int i;
  12699. #endif
  12700. #ifdef WOLFSSL_STATIC_MEMORY
  12701. word32 size[] = { WOLFMEM_BUCKETS };
  12702. word32 dist[] = { WOLFMEM_DIST };
  12703. byte buffer[30000]; /* make large enough to involve many bucket sizes */
  12704. int pad = -(int)((wc_ptr_t)buffer) & (WOLFSSL_STATIC_ALIGN - 1);
  12705. /* pad to account for if head of buffer is not at set memory
  12706. * alignment when tests are ran */
  12707. #endif
  12708. #ifdef WOLFSSL_STATIC_MEMORY
  12709. /* check macro settings */
  12710. if (sizeof(size)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
  12711. return WC_TEST_RET_ENC_NC;
  12712. }
  12713. if (sizeof(dist)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
  12714. return WC_TEST_RET_ENC_NC;
  12715. }
  12716. for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
  12717. if ((size[i] % WOLFSSL_STATIC_ALIGN) != 0) {
  12718. /* each element in array should be divisible by alignment size */
  12719. return WC_TEST_RET_ENC_NC;
  12720. }
  12721. }
  12722. for (i = 1; i < WOLFMEM_MAX_BUCKETS; i++) {
  12723. if (size[i - 1] >= size[i]) {
  12724. return WC_TEST_RET_ENC_NC; /* sizes should be in increasing order */
  12725. }
  12726. }
  12727. /* check that padding size returned is possible */
  12728. if (wolfSSL_MemoryPaddingSz() < WOLFSSL_STATIC_ALIGN) {
  12729. return WC_TEST_RET_ENC_NC; /* no room for wc_Memory struct */
  12730. }
  12731. ret = wolfSSL_MemoryPaddingSz();
  12732. if (ret < 0) {
  12733. return WC_TEST_RET_ENC_EC(ret);
  12734. }
  12735. if (wolfSSL_MemoryPaddingSz() % WOLFSSL_STATIC_ALIGN != 0) {
  12736. return WC_TEST_RET_ENC_NC; /* not aligned! */
  12737. }
  12738. /* check function to return optimum buffer size (rounded down) */
  12739. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_GENERAL);
  12740. if (ret < 0) {
  12741. return WC_TEST_RET_ENC_EC(ret);
  12742. }
  12743. if ((ret - pad) % WOLFSSL_STATIC_ALIGN != 0) {
  12744. return WC_TEST_RET_ENC_NC; /* not aligned! */
  12745. }
  12746. if ((unsigned int)ret > sizeof(buffer)) {
  12747. return WC_TEST_RET_ENC_NC; /* did not round down as expected */
  12748. }
  12749. if (ret != wolfSSL_StaticBufferSz(buffer, ret, WOLFMEM_GENERAL)) {
  12750. return WC_TEST_RET_ENC_NC; /* return value changed when using suggested
  12751. * value
  12752. */
  12753. }
  12754. ret = wolfSSL_MemoryPaddingSz();
  12755. ret += pad; /* add space that is going to be needed if buffer not aligned */
  12756. if (wolfSSL_StaticBufferSz(buffer, size[0] + ret + 1, WOLFMEM_GENERAL) !=
  12757. (ret + (int)size[0])) {
  12758. return WC_TEST_RET_ENC_NC; /* did not round down to nearest bucket
  12759. * value
  12760. */
  12761. }
  12762. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_IO_POOL);
  12763. if ((ret - pad) < 0) {
  12764. return WC_TEST_RET_ENC_NC;
  12765. }
  12766. if (((ret - pad) % (WOLFMEM_IO_SZ + wolfSSL_MemoryPaddingSz())) != 0) {
  12767. return WC_TEST_RET_ENC_NC; /* not even chunks of memory for IO size */
  12768. }
  12769. if (((ret - pad) % WOLFSSL_STATIC_ALIGN) != 0) {
  12770. return WC_TEST_RET_ENC_NC; /* memory not aligned */
  12771. }
  12772. /* check for passing bad or unknown arguments to functions */
  12773. if (wolfSSL_StaticBufferSz(NULL, 1, WOLFMEM_GENERAL) > 0) {
  12774. return WC_TEST_RET_ENC_NC;
  12775. }
  12776. if (wolfSSL_StaticBufferSz(buffer, 1, WOLFMEM_GENERAL) != 0) {
  12777. return WC_TEST_RET_ENC_NC; /* should round to 0
  12778. since struct + bucket will not fit */
  12779. }
  12780. (void)dist; /* avoid static analysis warning of variable not used */
  12781. #endif
  12782. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  12783. /* simple test */
  12784. ret = simple_mem_test(MEM_TEST_SZ);
  12785. if (ret != 0)
  12786. return ret;
  12787. #endif
  12788. #ifdef COMPLEX_MEM_TEST
  12789. /* test various size blocks */
  12790. for (i = 1; i < MEM_TEST_SZ; i*=2) {
  12791. ret = simple_mem_test(i);
  12792. if (ret != 0)
  12793. return ret;
  12794. }
  12795. #endif
  12796. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_NO_MALLOC) && defined(XREALLOC)
  12797. /* realloc test */
  12798. {
  12799. byte *b = (byte*)XMALLOC(MEM_TEST_SZ, HEAP_HINT,
  12800. DYNAMIC_TYPE_TMP_BUFFER);
  12801. #ifndef WOLFSSL_NO_REALLOC
  12802. byte *c = NULL;
  12803. if (b) {
  12804. c = (byte*)XREALLOC(b, MEM_TEST_SZ+sizeof(word32), HEAP_HINT,
  12805. DYNAMIC_TYPE_TMP_BUFFER);
  12806. if (c)
  12807. b = c;
  12808. }
  12809. #endif
  12810. if (b)
  12811. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12812. if ((b == NULL)
  12813. #ifndef WOLFSSL_NO_REALLOC
  12814. || (c == NULL)
  12815. #endif
  12816. ) {
  12817. return WC_TEST_RET_ENC_ERRNO;
  12818. }
  12819. }
  12820. #endif
  12821. if (ret == 0) {
  12822. /* This test is only interesting on embedded R/O Flash systems */
  12823. if (const_byte_ptr_test(const_byte_array, &j) != CBPTR_EXPECTED) {
  12824. ret = 1;
  12825. }
  12826. }
  12827. return ret;
  12828. }
  12829. #ifndef NO_FILESYSTEM
  12830. /* Cert Paths */
  12831. #ifdef FREESCALE_MQX
  12832. #define CERT_PREFIX "a:\\"
  12833. #define CERT_PATH_SEP "\\"
  12834. #elif defined(WOLFSSL_uTKERNEL2)
  12835. #define CERT_PREFIX "/uda/"
  12836. #define CERT_PATH_SEP "/"
  12837. #elif defined(_WIN32_WCE)
  12838. #define CERT_PREFIX "\\windows\\"
  12839. #define CERT_PATH_SEP "\\"
  12840. #endif
  12841. #ifndef CERT_PREFIX
  12842. #define CERT_PREFIX "./"
  12843. #endif
  12844. #ifndef CERT_PATH_SEP
  12845. #define CERT_PATH_SEP "/"
  12846. #endif
  12847. #ifndef CERT_WRITE_TEMP_DIR
  12848. #define CERT_WRITE_TEMP_DIR CERT_PREFIX
  12849. #endif
  12850. #define CERT_ROOT CERT_PREFIX "certs" CERT_PATH_SEP
  12851. /* Generated Test Certs */
  12852. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  12853. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  12854. #if !defined(NO_RSA) && !defined(NO_ASN)
  12855. static const char* clientKey = CERT_ROOT "client-key.der";
  12856. static const char* clientCert = CERT_ROOT "client-cert.der";
  12857. #ifdef WOLFSSL_CERT_EXT
  12858. static const char* clientKeyPub = CERT_ROOT "client-keyPub.der";
  12859. #endif
  12860. #endif /* !NO_RSA && !NO_ASN */
  12861. #endif
  12862. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  12863. #if !defined(NO_RSA) && !defined(NO_ASN)
  12864. #if defined(WOLFSSL_CERT_GEN) || defined(HAVE_PKCS7)
  12865. static const char* rsaCaKeyFile = CERT_ROOT "ca-key.der";
  12866. #ifdef WOLFSSL_CERT_GEN
  12867. static const char* rsaCaCertFile = CERT_ROOT "ca-cert.pem";
  12868. #endif
  12869. #if defined(WOLFSSL_ALT_NAMES) || defined(HAVE_PKCS7)
  12870. static const char* rsaCaCertDerFile = CERT_ROOT "ca-cert.der";
  12871. #endif
  12872. #ifdef HAVE_PKCS7
  12873. static const char* rsaServerCertDerFile =
  12874. CERT_ROOT "server-cert.der";
  12875. static const char* rsaServerKeyDerFile =
  12876. CERT_ROOT "server-key.der";
  12877. #endif
  12878. #endif
  12879. #endif /* !NO_RSA && !NO_ASN */
  12880. #endif /* !USE_CERT_BUFFER_* */
  12881. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  12882. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  12883. #if !defined(NO_ASN) && !defined(NO_DH)
  12884. static const char* dhParamsFile = CERT_ROOT "dh2048.der";
  12885. #endif
  12886. #endif
  12887. #if !defined(NO_ASN) && !defined(NO_DH)
  12888. #if defined(WOLFSSL_DH_EXTRA) && (!defined(HAVE_FIPS) || \
  12889. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  12890. #if !defined(USE_CERT_BUFFERS_2048)
  12891. static const char* dhKeyFile = CERT_ROOT "statickeys/dh-ffdhe2048.der";
  12892. static const char* dhKeyPubFile = CERT_ROOT "statickeys/dh-ffdhe2048-pub.der";
  12893. #endif
  12894. #endif
  12895. #endif
  12896. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  12897. #ifndef NO_DSA
  12898. static const char* dsaKey = CERT_ROOT "dsa2048.der";
  12899. #endif
  12900. #endif /* !USE_CERT_BUFFER_* */
  12901. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ECC256)
  12902. #ifdef HAVE_ECC
  12903. /* cert files to be used in rsa cert gen test, check if RSA enabled */
  12904. #ifdef HAVE_ECC_KEY_IMPORT
  12905. static const char* eccKeyDerFile = CERT_ROOT "ecc-key.der";
  12906. #endif
  12907. #endif
  12908. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ASN)
  12909. #if defined(HAVE_ECC) && defined(WOLFSSL_CERT_GEN) && \
  12910. !defined(NO_ECC_SECP)
  12911. #ifndef NO_RSA
  12912. static const char* eccKeyPubFileDer = CERT_ROOT "ecc-keyPub.der";
  12913. #endif
  12914. #ifndef NO_ASN_TIME
  12915. static const char* eccCaKeyFile = CERT_ROOT "ca-ecc-key.der";
  12916. static const char* eccCaCertFile = CERT_ROOT "ca-ecc-cert.pem";
  12917. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  12918. static const char* eccCaKey384File =
  12919. CERT_ROOT "ca-ecc384-key.der";
  12920. static const char* eccCaCert384File =
  12921. CERT_ROOT "ca-ecc384-cert.pem";
  12922. #endif
  12923. #endif
  12924. #endif
  12925. #if defined(HAVE_PKCS7) && defined(HAVE_ECC)
  12926. static const char* eccClientKey = CERT_ROOT "ecc-client-key.der";
  12927. static const char* eccClientCert = CERT_ROOT "client-ecc-cert.der";
  12928. #endif
  12929. #endif /* HAVE_ECC */
  12930. #ifdef HAVE_ED25519
  12931. #ifdef WOLFSSL_TEST_CERT
  12932. static const char* serverEd25519Cert =
  12933. CERT_ROOT "ed25519/server-ed25519.der";
  12934. static const char* caEd25519Cert =
  12935. CERT_ROOT "ed25519/ca-ed25519.der";
  12936. #endif
  12937. #endif
  12938. #ifdef HAVE_ED448
  12939. #ifdef WOLFSSL_TEST_CERT
  12940. static const char* serverEd448Cert =
  12941. CERT_ROOT "ed448/server-ed448.der";
  12942. static const char* caEd448Cert = CERT_ROOT "ed448/ca-ed448.der";
  12943. #endif
  12944. #endif
  12945. #endif /* !USE_CERT_BUFFER_* */
  12946. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  12947. !defined(NO_FILESYSTEM)
  12948. static const char* certExtNc =
  12949. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-nc.der";
  12950. static const char* certExtIa =
  12951. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-ia.der";
  12952. static const char* certExtNct =
  12953. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-nct.der";
  12954. #endif
  12955. #ifndef NO_WRITE_TEMP_FILES
  12956. #ifdef HAVE_ECC
  12957. #ifndef NO_ECC_SECP
  12958. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  12959. static const char* certEccPemFile = CERT_WRITE_TEMP_DIR "certecc.pem";
  12960. static const char* certEccDerFile = CERT_WRITE_TEMP_DIR "certecc.der";
  12961. #endif
  12962. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  12963. static const char* certEccRsaPemFile = CERT_WRITE_TEMP_DIR "certeccrsa.pem";
  12964. static const char* certEccRsaDerFile = CERT_WRITE_TEMP_DIR "certeccrsa.der";
  12965. #endif
  12966. #endif
  12967. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  12968. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ASN_CRYPT)
  12969. static const char* eccCaKeyPemFile = CERT_WRITE_TEMP_DIR "ecc-key.pem";
  12970. static const char* eccPubKeyDerFile = CERT_WRITE_TEMP_DIR "ecc-public-key.der";
  12971. static const char* eccCaKeyTempFile = CERT_WRITE_TEMP_DIR "ecc-key.der";
  12972. #if defined(HAVE_PKCS8) && !defined(WC_NO_RNG) && \
  12973. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  12974. static const char* eccPkcs8KeyDerFile = CERT_WRITE_TEMP_DIR "ecc-key-pkcs8.der";
  12975. #endif
  12976. #endif /* HAVE_ECC_KEY_EXPORT */
  12977. #endif /* HAVE_ECC */
  12978. #ifndef NO_RSA
  12979. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  12980. static const char* otherCertDerFile = CERT_WRITE_TEMP_DIR "othercert.der";
  12981. static const char* certDerFile = CERT_WRITE_TEMP_DIR "cert.der";
  12982. static const char* otherCertPemFile = CERT_WRITE_TEMP_DIR "othercert.pem";
  12983. static const char* certPemFile = CERT_WRITE_TEMP_DIR "cert.pem";
  12984. #if defined(WOLFSSL_CERT_REQ) && !defined(WOLFSSL_NO_MALLOC)
  12985. static const char* certReqDerFile = CERT_WRITE_TEMP_DIR "certreq.der";
  12986. static const char* certReqPemFile = CERT_WRITE_TEMP_DIR "certreq.pem";
  12987. #endif
  12988. #endif
  12989. #endif /* !NO_RSA */
  12990. #if !defined(NO_RSA) || !defined(NO_DSA)
  12991. #ifdef WOLFSSL_KEY_GEN
  12992. static const char* keyDerFile = CERT_WRITE_TEMP_DIR "key.der";
  12993. static const char* keyPemFile = CERT_WRITE_TEMP_DIR "key.pem";
  12994. #endif
  12995. #endif
  12996. #endif /* !NO_WRITE_TEMP_FILES */
  12997. #endif /* !NO_FILESYSTEM */
  12998. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  12999. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  13000. static CertName certDefaultName;
  13001. static void initDefaultName(void)
  13002. {
  13003. #if defined(WOLFSSL_MULTI_ATTRIB) && defined(WOLFSSL_TEST_CERT)
  13004. NameAttrib* n;
  13005. #endif
  13006. XMEMCPY(certDefaultName.country, "US", sizeof("US"));
  13007. certDefaultName.countryEnc = CTC_PRINTABLE;
  13008. XMEMCPY(certDefaultName.state, "Oregon", sizeof("Oregon"));
  13009. certDefaultName.stateEnc = CTC_UTF8;
  13010. XMEMCPY(certDefaultName.street, "Main St", sizeof("Main St"));
  13011. certDefaultName.streetEnc = CTC_UTF8;
  13012. XMEMCPY(certDefaultName.locality, "Portland", sizeof("Portland"));
  13013. certDefaultName.localityEnc = CTC_UTF8;
  13014. XMEMCPY(certDefaultName.sur, "Test", sizeof("Test"));
  13015. certDefaultName.surEnc = CTC_UTF8;
  13016. XMEMCPY(certDefaultName.org, "wolfSSL", sizeof("wolfSSL"));
  13017. certDefaultName.orgEnc = CTC_UTF8;
  13018. XMEMCPY(certDefaultName.unit, "Development", sizeof("Development"));
  13019. certDefaultName.unitEnc = CTC_UTF8;
  13020. XMEMCPY(certDefaultName.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  13021. certDefaultName.commonNameEnc = CTC_UTF8;
  13022. XMEMCPY(certDefaultName.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  13023. certDefaultName.serialDevEnc = CTC_PRINTABLE;
  13024. XMEMCPY(certDefaultName.postalCode, "12-456", sizeof("12-456"));
  13025. certDefaultName.postalCodeEnc = CTC_PRINTABLE;
  13026. #ifdef WOLFSSL_CERT_EXT
  13027. XMEMCPY(certDefaultName.busCat, "Private Organization", sizeof("Private Organization"));
  13028. certDefaultName.busCatEnc = CTC_UTF8;
  13029. XMEMCPY(certDefaultName.joiSt, "US", sizeof("US"));
  13030. certDefaultName.joiStEnc = CTC_PRINTABLE;
  13031. XMEMCPY(certDefaultName.joiC, "Oregon", sizeof("Oregon"));
  13032. certDefaultName.joiCEnc = CTC_PRINTABLE;
  13033. #endif
  13034. XMEMCPY(certDefaultName.email, "info@wolfssl.com", sizeof("info@wolfssl.com"));
  13035. XMEMCPY(certDefaultName.userId, "TestUserID", sizeof("TestUserID"));
  13036. certDefaultName.userIdEnc = CTC_PRINTABLE;
  13037. #if defined(WOLFSSL_MULTI_ATTRIB) && defined(WOLFSSL_TEST_CERT)
  13038. /* test having additional OUs and setting DC */
  13039. n = &certDefaultName.name[0];
  13040. n->id = ASN_ORGUNIT_NAME;
  13041. n->type = CTC_UTF8;
  13042. n->sz = XSTRLEN("Development-2");
  13043. XMEMCPY(n->value, "Development-2", sizeof("Development-2"));
  13044. #if CTC_MAX_ATTRIB > 3
  13045. n = &certDefaultName.name[1];
  13046. n->id = ASN_DOMAIN_COMPONENT;
  13047. n->type = CTC_UTF8;
  13048. n->sz = XSTRLEN("com");
  13049. XMEMCPY(n->value, "com", sizeof("com"));
  13050. n = &certDefaultName.name[2];
  13051. n->id = ASN_DOMAIN_COMPONENT;
  13052. n->type = CTC_UTF8;
  13053. n->sz = XSTRLEN("wolfssl");
  13054. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  13055. #endif
  13056. #endif /* WOLFSSL_MULTI_ATTRIB && WOLFSSL_TEST_CERT */
  13057. #ifdef WOLFSSL_CUSTOM_OID
  13058. /* TODO: Add test case for custom OID's */
  13059. #endif
  13060. }
  13061. #ifdef WOLFSSL_CERT_EXT
  13062. #if ((defined(HAVE_ED25519) || defined(HAVE_ED448)) && \
  13063. defined(WOLFSSL_TEST_CERT)) || defined(HAVE_ECC)
  13064. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage[] =
  13065. "digitalSignature,nonRepudiation";
  13066. #endif
  13067. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  13068. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage2[] =
  13069. "digitalSignature,nonRepudiation,keyEncipherment,keyAgreement";
  13070. #endif
  13071. #endif /* WOLFSSL_CERT_EXT */
  13072. #endif /* WOLFSSL_CERT_GEN */
  13073. #ifndef NO_RSA
  13074. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  13075. !defined(NO_FILESYSTEM)
  13076. static byte minSerial[] = { 0x02, 0x01, 0x01 };
  13077. static byte minName[] = { 0x30, 0x00 };
  13078. static byte nameBad[] = {
  13079. 0x30, 0x08,
  13080. 0x31, 0x06,
  13081. 0x30, 0x04,
  13082. 0x06, 0x02,
  13083. 0x55, 0x04,
  13084. };
  13085. static byte minDates[] = {
  13086. 0x30, 0x1e,
  13087. 0x17, 0x0d,
  13088. 0x31, 0x38, 0x30, 0x34, 0x31, 0x33, 0x31, 0x35,
  13089. 0x32, 0x33, 0x31, 0x30, 0x5a,
  13090. 0x17, 0x0d,
  13091. 0x32, 0x31, 0x30, 0x31, 0x30, 0x37, 0x31, 0x35,
  13092. 0x32, 0x33, 0x31, 0x30, 0x5a
  13093. };
  13094. static byte minPubKey[] = {
  13095. 0x30, 0x1c,
  13096. 0x30, 0x0d,
  13097. 0x06, 0x09,
  13098. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  13099. 0x01,
  13100. 0x05, 0x00,
  13101. 0x03, 0x0b,
  13102. 0x00, 0x30, 0x08,
  13103. 0x02, 0x01,
  13104. 0x03,
  13105. 0x02, 0x03,
  13106. 0x01, 0x00, 0x01
  13107. };
  13108. static byte minSigAlg[] = {
  13109. 0x30, 0x0d,
  13110. 0x06, 0x09,
  13111. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  13112. 0x0b,
  13113. 0x05, 0x00
  13114. };
  13115. static byte minSig[] = {
  13116. 0x03, 0x01,
  13117. 0x00
  13118. };
  13119. static int add_seq(byte* certData, int offset, byte* data, byte length)
  13120. {
  13121. XMEMMOVE(certData + offset + 2, data, length);
  13122. certData[offset++] = 0x30;
  13123. certData[offset++] = length;
  13124. return offset + length;
  13125. }
  13126. static int add_data(byte* certData, int offset, byte* data, byte length)
  13127. {
  13128. XMEMCPY(certData + offset, data, length);
  13129. return offset + length;
  13130. }
  13131. static wc_test_ret_t cert_asn1_test(void)
  13132. {
  13133. wc_test_ret_t ret;
  13134. int len[3];
  13135. DecodedCert cert;
  13136. byte certData[114];
  13137. byte* badCert = NULL;
  13138. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  13139. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  13140. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  13141. len[2] = add_data(certData, len[2], minDates, (byte)sizeof(minDates));
  13142. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  13143. len[2] = add_data(certData, len[2], minPubKey, (byte)sizeof(minPubKey));
  13144. len[1] = add_seq(certData, 0, certData, len[2]);
  13145. len[1] = add_data(certData, len[1], minSigAlg, (byte)sizeof(minSigAlg));
  13146. len[1] = add_data(certData, len[1], minSig, (byte)sizeof(minSig));
  13147. len[0] = add_seq(certData, 0, certData, len[1]);
  13148. /* Minimal good certificate */
  13149. InitDecodedCert(&cert, certData, len[0], 0);
  13150. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  13151. FreeDecodedCert(&cert);
  13152. if (ret != 0)
  13153. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  13154. /* Bad issuer name */
  13155. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  13156. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  13157. len[2] = add_data(certData, len[2], nameBad, (byte)sizeof(nameBad));
  13158. len[2] = add_data(certData, len[2], minDates, (byte)sizeof(minDates));
  13159. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  13160. len[2] = add_data(certData, len[2], minPubKey, (byte)sizeof(minPubKey));
  13161. len[1] = add_seq(certData, 0, certData, len[2]);
  13162. len[1] = add_data(certData, len[1], minSigAlg, (byte)sizeof(minSigAlg));
  13163. len[1] = add_data(certData, len[1], minSig, (byte)sizeof(minSig));
  13164. len[0] = add_seq(certData, 0, certData, len[1]);
  13165. /* Put data into allocated buffer to allow access error checking. */
  13166. badCert = (byte*)XMALLOC(len[0], HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13167. XMEMCPY(badCert, certData, len[0]);
  13168. InitDecodedCert(&cert, badCert, len[0], 0);
  13169. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  13170. FreeDecodedCert(&cert);
  13171. if (ret != ASN_PARSE_E) {
  13172. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  13173. }
  13174. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13175. badCert = NULL;
  13176. ret = 0;
  13177. done:
  13178. if (badCert != NULL)
  13179. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13180. return ret;
  13181. }
  13182. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cert_test(void)
  13183. {
  13184. #if !defined(NO_FILESYSTEM)
  13185. DecodedCert cert;
  13186. byte* tmp;
  13187. size_t bytes;
  13188. XFILE file;
  13189. wc_test_ret_t ret;
  13190. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13191. if (tmp == NULL)
  13192. return WC_TEST_RET_ENC_ERRNO;
  13193. /* Certificate with Name Constraints extension. */
  13194. file = XFOPEN(certExtNc, "rb");
  13195. if (!file) {
  13196. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  13197. }
  13198. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  13199. XFCLOSE(file);
  13200. if (bytes == 0)
  13201. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  13202. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  13203. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  13204. if (ret != 0)
  13205. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  13206. FreeDecodedCert(&cert);
  13207. /* Certificate with Inhibit Any Policy extension. */
  13208. file = XFOPEN(certExtIa, "rb");
  13209. if (!file) {
  13210. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  13211. }
  13212. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  13213. XFCLOSE(file);
  13214. if (bytes == 0)
  13215. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  13216. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  13217. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  13218. if (ret != 0)
  13219. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  13220. FreeDecodedCert(&cert);
  13221. /* Certificate with Netscape Certificate Type extension. */
  13222. file = XFOPEN(certExtNct, "rb");
  13223. if (!file) {
  13224. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  13225. }
  13226. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  13227. XFCLOSE(file);
  13228. if (bytes == 0)
  13229. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  13230. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  13231. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  13232. #ifndef IGNORE_NETSCAPE_CERT_TYPE
  13233. if (ret != 0)
  13234. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  13235. #else
  13236. if (ret != ASN_CRIT_EXT_E) {
  13237. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  13238. }
  13239. ret = 0;
  13240. #endif
  13241. done:
  13242. FreeDecodedCert(&cert);
  13243. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13244. #endif /* !NO_FILESYSTEM */
  13245. if (ret == 0)
  13246. ret = cert_asn1_test();
  13247. return ret;
  13248. }
  13249. #endif /* WOLFSSL_TEST_CERT */
  13250. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  13251. !defined(NO_FILESYSTEM) && defined(WOLFSSL_CERT_GEN)
  13252. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t certext_test(void)
  13253. {
  13254. DecodedCert cert;
  13255. byte* tmp;
  13256. size_t bytes;
  13257. XFILE file;
  13258. wc_test_ret_t ret;
  13259. /* created from rsa_test : othercert.der */
  13260. byte skid_rsa[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  13261. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  13262. /* created from rsa_test : othercert.der */
  13263. byte akid_rsa[] = "\x27\x8E\x67\x11\x74\xC3\x26\x1D\x3F\xED"
  13264. "\x33\x63\xB3\xA4\xD8\x1D\x30\xE5\xE8\xD5";
  13265. #ifdef HAVE_ECC
  13266. /* created from ecc_test_cert_gen : certecc.der */
  13267. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  13268. /* Authority key id from ./certs/ca-ecc384-cert.pem */
  13269. byte akid_ecc[] = "\xAB\xE0\xC3\x26\x4C\x18\xD4\x72\xBB\xD2"
  13270. "\x84\x8C\x9C\x0A\x05\x92\x80\x12\x53\x52";
  13271. #else
  13272. /* Authority key id from ./certs/ca-ecc-cert.pem */
  13273. byte akid_ecc[] = "\x56\x8E\x9A\xC3\xF0\x42\xDE\x18\xB9\x45"
  13274. "\x55\x6E\xF9\x93\xCF\xEA\xC3\xF3\xA5\x21";
  13275. #endif
  13276. #endif /* HAVE_ECC */
  13277. /* created from rsa_test : cert.der */
  13278. byte kid_ca[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  13279. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  13280. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13281. if (tmp == NULL)
  13282. return WC_TEST_RET_ENC_ERRNO;
  13283. /* load othercert.der (Cert signed by an authority) */
  13284. file = XFOPEN(otherCertDerFile, "rb");
  13285. if (!file) {
  13286. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13287. return WC_TEST_RET_ENC_ERRNO;
  13288. }
  13289. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  13290. XFCLOSE(file);
  13291. if (bytes == 0)
  13292. return WC_TEST_RET_ENC_ERRNO;
  13293. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  13294. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  13295. if (ret != 0)
  13296. return WC_TEST_RET_ENC_EC(ret);
  13297. /* check the SKID from a RSA certificate */
  13298. if (XMEMCMP(skid_rsa, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  13299. return WC_TEST_RET_ENC_NC;
  13300. /* check the AKID from an RSA certificate */
  13301. if (XMEMCMP(akid_rsa, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  13302. return WC_TEST_RET_ENC_NC;
  13303. /* check the Key Usage from an RSA certificate */
  13304. if (!cert.extKeyUsageSet)
  13305. return WC_TEST_RET_ENC_NC;
  13306. if (cert.extKeyUsage != (KEYUSE_KEY_ENCIPHER|KEYUSE_KEY_AGREE))
  13307. return WC_TEST_RET_ENC_NC;
  13308. /* check the CA Basic Constraints from an RSA certificate */
  13309. if (cert.isCA)
  13310. return WC_TEST_RET_ENC_NC;
  13311. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  13312. /* check the Certificate Policies Id */
  13313. if (cert.extCertPoliciesNb != 1)
  13314. return WC_TEST_RET_ENC_NC;
  13315. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  13316. return WC_TEST_RET_ENC_NC;
  13317. #endif
  13318. FreeDecodedCert(&cert);
  13319. #ifdef HAVE_ECC
  13320. /* load certecc.der (Cert signed by our ECC CA test in ecc_test_cert_gen) */
  13321. file = XFOPEN(certEccDerFile, "rb");
  13322. if (!file) {
  13323. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13324. return WC_TEST_RET_ENC_ERRNO;
  13325. }
  13326. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  13327. XFCLOSE(file);
  13328. if (bytes == 0)
  13329. return WC_TEST_RET_ENC_ERRNO;
  13330. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  13331. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  13332. if (ret != 0)
  13333. return WC_TEST_RET_ENC_EC(ret);
  13334. /* check the SKID from a ECC certificate - generated dynamically */
  13335. /* check the AKID from an ECC certificate */
  13336. if (XMEMCMP(akid_ecc, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  13337. return WC_TEST_RET_ENC_NC;
  13338. /* check the Key Usage from an ECC certificate */
  13339. if (!cert.extKeyUsageSet)
  13340. return WC_TEST_RET_ENC_NC;
  13341. if (cert.extKeyUsage != (KEYUSE_DIGITAL_SIG|KEYUSE_CONTENT_COMMIT))
  13342. return WC_TEST_RET_ENC_NC;
  13343. /* check the CA Basic Constraints from an ECC certificate */
  13344. if (cert.isCA)
  13345. return WC_TEST_RET_ENC_NC;
  13346. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  13347. /* check the Certificate Policies Id */
  13348. if (cert.extCertPoliciesNb != 2)
  13349. return WC_TEST_RET_ENC_NC;
  13350. if (strncmp(cert.extCertPolicies[0], "2.4.589440.587.101.2.1.9632587.1", 32))
  13351. return WC_TEST_RET_ENC_NC;
  13352. if (strncmp(cert.extCertPolicies[1], "1.2.13025.489.1.113549", 22))
  13353. return WC_TEST_RET_ENC_NC;
  13354. #endif
  13355. FreeDecodedCert(&cert);
  13356. #endif /* HAVE_ECC */
  13357. /* load cert.der (self signed certificate) */
  13358. file = XFOPEN(certDerFile, "rb");
  13359. if (!file) {
  13360. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13361. return WC_TEST_RET_ENC_ERRNO;
  13362. }
  13363. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  13364. XFCLOSE(file);
  13365. if (bytes == 0)
  13366. return WC_TEST_RET_ENC_ERRNO;
  13367. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  13368. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  13369. if (ret != 0)
  13370. return WC_TEST_RET_ENC_EC(ret);
  13371. /* check the SKID from a CA certificate */
  13372. if (XMEMCMP(kid_ca, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  13373. return WC_TEST_RET_ENC_NC;
  13374. /* check the AKID from an CA certificate */
  13375. if (XMEMCMP(kid_ca, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  13376. return WC_TEST_RET_ENC_NC;
  13377. /* check the Key Usage from CA certificate */
  13378. if (!cert.extKeyUsageSet)
  13379. return WC_TEST_RET_ENC_NC;
  13380. if (cert.extKeyUsage != (KEYUSE_KEY_CERT_SIGN|KEYUSE_CRL_SIGN))
  13381. return WC_TEST_RET_ENC_NC;
  13382. /* check the CA Basic Constraints CA certificate */
  13383. if (!cert.isCA)
  13384. return WC_TEST_RET_ENC_NC;
  13385. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  13386. /* check the Certificate Policies Id */
  13387. if (cert.extCertPoliciesNb != 2)
  13388. return WC_TEST_RET_ENC_NC;
  13389. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  13390. return WC_TEST_RET_ENC_NC;
  13391. if (strncmp(cert.extCertPolicies[1], "1.2.840.113549.1.9.16.6.5", 25))
  13392. return WC_TEST_RET_ENC_NC;
  13393. #endif
  13394. FreeDecodedCert(&cert);
  13395. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13396. return 0;
  13397. }
  13398. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_TEST_CERT &&
  13399. !NO_FILESYSTEM && WOLFSSL_CERT_GEN */
  13400. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  13401. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  13402. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t decodedCertCache_test(void)
  13403. {
  13404. wc_test_ret_t ret = 0;
  13405. Cert cert;
  13406. FILE* file;
  13407. byte* der;
  13408. word32 derSz;
  13409. derSz = FOURK_BUF;
  13410. der = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13411. if (der == NULL)
  13412. ret = WC_TEST_RET_ENC_NC;
  13413. if (ret == 0) {
  13414. /* load cert.der */
  13415. file = XFOPEN(certDerFile, "rb");
  13416. if (file != NULL) {
  13417. derSz = (word32)XFREAD(der, 1, FOURK_BUF, file);
  13418. XFCLOSE(file);
  13419. if (derSz == 0)
  13420. ret = WC_TEST_RET_ENC_ERRNO;
  13421. }
  13422. else
  13423. ret = WC_TEST_RET_ENC_ERRNO;
  13424. }
  13425. if (ret == 0) {
  13426. ret = wc_InitCert_ex(&cert, HEAP_HINT, devId);
  13427. if (ret != 0)
  13428. ret = WC_TEST_RET_ENC_EC(ret);
  13429. }
  13430. if (ret == 0) {
  13431. ret = wc_SetSubjectBuffer(&cert, der, derSz);
  13432. if (ret != 0)
  13433. ret = WC_TEST_RET_ENC_EC(ret);
  13434. }
  13435. if (ret == 0) {
  13436. ret = wc_SetSubjectBuffer(NULL, der, derSz);
  13437. if (ret == BAD_FUNC_ARG)
  13438. ret = 0;
  13439. else
  13440. ret = WC_TEST_RET_ENC_EC(ret);
  13441. }
  13442. if (ret == 0) {
  13443. ret = wc_SetSubjectRaw(&cert, der, derSz);
  13444. if (ret != 0)
  13445. ret = WC_TEST_RET_ENC_EC(ret);
  13446. }
  13447. if (ret == 0) {
  13448. ret = wc_SetSubjectRaw(NULL, der, derSz);
  13449. if (ret == BAD_FUNC_ARG)
  13450. ret = 0;
  13451. else
  13452. ret = WC_TEST_RET_ENC_EC(ret);
  13453. }
  13454. if (ret == 0) {
  13455. ret = wc_SetIssuerBuffer(&cert, der, derSz);
  13456. if (ret != 0)
  13457. ret = WC_TEST_RET_ENC_EC(ret);
  13458. }
  13459. if (ret == 0) {
  13460. ret = wc_SetIssuerBuffer(NULL, der, derSz);
  13461. if (ret == BAD_FUNC_ARG)
  13462. ret = 0;
  13463. else
  13464. ret = WC_TEST_RET_ENC_EC(ret);
  13465. }
  13466. if (ret == 0) {
  13467. ret = wc_SetIssuerRaw(&cert, der, derSz);
  13468. if (ret != 0)
  13469. ret = WC_TEST_RET_ENC_EC(ret);
  13470. }
  13471. if (ret == 0) {
  13472. ret = wc_SetIssuerRaw(NULL, der, derSz);
  13473. if (ret == BAD_FUNC_ARG)
  13474. ret = 0;
  13475. else
  13476. ret = WC_TEST_RET_ENC_EC(ret);
  13477. }
  13478. #ifdef WOLFSSL_ALT_NAMES
  13479. if (ret == 0) {
  13480. ret = wc_SetAltNamesBuffer(&cert, der, derSz);
  13481. if (ret != 0)
  13482. ret = WC_TEST_RET_ENC_EC(ret);
  13483. }
  13484. if (ret == 0) {
  13485. ret = wc_SetAltNamesBuffer(NULL, der, derSz);
  13486. if (ret == BAD_FUNC_ARG)
  13487. ret = 0;
  13488. else
  13489. ret = WC_TEST_RET_ENC_EC(ret);
  13490. }
  13491. if (ret == 0) {
  13492. ret = wc_SetDatesBuffer(&cert, der, derSz);
  13493. if (ret != 0)
  13494. ret = WC_TEST_RET_ENC_EC(ret);
  13495. }
  13496. if (ret == 0) {
  13497. ret = wc_SetDatesBuffer(NULL, der, derSz);
  13498. if (ret == BAD_FUNC_ARG)
  13499. ret = 0;
  13500. else
  13501. ret = WC_TEST_RET_ENC_EC(ret);
  13502. }
  13503. #endif
  13504. if (ret == 0) {
  13505. ret = wc_SetAuthKeyIdFromCert(&cert, der, derSz);
  13506. if (ret != 0)
  13507. ret = WC_TEST_RET_ENC_EC(ret);
  13508. }
  13509. if (ret == 0) {
  13510. ret = wc_SetAuthKeyIdFromCert(NULL, der, derSz);
  13511. if (ret == BAD_FUNC_ARG)
  13512. ret = 0;
  13513. else
  13514. ret = WC_TEST_RET_ENC_NC;
  13515. }
  13516. wc_SetCert_Free(&cert);
  13517. if (ret == 0) {
  13518. if(cert.decodedCert != NULL)
  13519. ret = WC_TEST_RET_ENC_NC;
  13520. }
  13521. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13522. return ret;
  13523. }
  13524. #endif /* defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) &&
  13525. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) */
  13526. #define RSA_TEST_BYTES 512 /* up to 4096-bit key */
  13527. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  13528. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  13529. static wc_test_ret_t rsa_flatten_test(RsaKey* key)
  13530. {
  13531. wc_test_ret_t ret;
  13532. byte e[RSA_TEST_BYTES];
  13533. byte n[RSA_TEST_BYTES];
  13534. word32 eSz = sizeof(e);
  13535. word32 nSz = sizeof(n);
  13536. /* Parameter Validation testing. */
  13537. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  13538. #ifdef HAVE_USER_RSA
  13539. /* Implementation using IPP Libraries returns:
  13540. * -101 = USER_CRYPTO_ERROR
  13541. */
  13542. if (ret == 0)
  13543. #else
  13544. if (ret != BAD_FUNC_ARG)
  13545. #endif
  13546. return WC_TEST_RET_ENC_EC(ret);
  13547. ret = wc_RsaFlattenPublicKey(key, NULL, &eSz, n, &nSz);
  13548. #ifdef HAVE_USER_RSA
  13549. /* Implementation using IPP Libraries returns:
  13550. * -101 = USER_CRYPTO_ERROR
  13551. */
  13552. if (ret == 0)
  13553. #else
  13554. if (ret != BAD_FUNC_ARG)
  13555. #endif
  13556. return WC_TEST_RET_ENC_EC(ret);
  13557. ret = wc_RsaFlattenPublicKey(key, e, NULL, n, &nSz);
  13558. #ifdef HAVE_USER_RSA
  13559. /* Implementation using IPP Libraries returns:
  13560. * -101 = USER_CRYPTO_ERROR
  13561. */
  13562. if (ret == 0)
  13563. #else
  13564. if (ret != BAD_FUNC_ARG)
  13565. #endif
  13566. return WC_TEST_RET_ENC_EC(ret);
  13567. ret = wc_RsaFlattenPublicKey(key, e, &eSz, NULL, &nSz);
  13568. #ifdef HAVE_USER_RSA
  13569. /* Implementation using IPP Libraries returns:
  13570. * -101 = USER_CRYPTO_ERROR
  13571. */
  13572. if (ret == 0)
  13573. #else
  13574. if (ret != BAD_FUNC_ARG)
  13575. #endif
  13576. return WC_TEST_RET_ENC_EC(ret);
  13577. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, NULL);
  13578. #ifdef HAVE_USER_RSA
  13579. /* Implementation using IPP Libraries returns:
  13580. * -101 = USER_CRYPTO_ERROR
  13581. */
  13582. if (ret == 0)
  13583. #else
  13584. if (ret != BAD_FUNC_ARG)
  13585. #endif
  13586. return WC_TEST_RET_ENC_EC(ret);
  13587. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  13588. if (ret != 0)
  13589. return WC_TEST_RET_ENC_EC(ret);
  13590. eSz = 0;
  13591. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  13592. #ifdef HAVE_USER_RSA
  13593. /* Implementation using IPP Libraries returns:
  13594. * -101 = USER_CRYPTO_ERROR
  13595. */
  13596. if (ret == 0)
  13597. #elif defined(HAVE_FIPS) && \
  13598. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
  13599. if (ret != 0)
  13600. #else
  13601. if (ret != RSA_BUFFER_E)
  13602. #endif
  13603. return WC_TEST_RET_ENC_EC(ret);
  13604. eSz = sizeof(e);
  13605. nSz = 0;
  13606. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  13607. #ifdef HAVE_USER_RSA
  13608. /* Implementation using IPP Libraries returns:
  13609. * -101 = USER_CRYPTO_ERROR
  13610. */
  13611. if (ret == 0)
  13612. #else
  13613. if (ret != RSA_BUFFER_E)
  13614. #endif
  13615. return WC_TEST_RET_ENC_EC(ret);
  13616. return 0;
  13617. }
  13618. #endif /* NO_ASN */
  13619. #if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(NO_ASN) \
  13620. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  13621. static wc_test_ret_t rsa_export_key_test(RsaKey* key)
  13622. {
  13623. wc_test_ret_t ret;
  13624. byte e[3];
  13625. word32 eSz = sizeof(e);
  13626. byte n[RSA_TEST_BYTES];
  13627. word32 nSz = sizeof(n);
  13628. byte d[RSA_TEST_BYTES];
  13629. word32 dSz = sizeof(d);
  13630. byte p[RSA_TEST_BYTES/2];
  13631. word32 pSz = sizeof(p);
  13632. byte q[RSA_TEST_BYTES/2];
  13633. word32 qSz = sizeof(q);
  13634. word32 zero = 0;
  13635. ret = wc_RsaExportKey(NULL, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  13636. if (ret != BAD_FUNC_ARG)
  13637. return WC_TEST_RET_ENC_EC(ret);
  13638. ret = wc_RsaExportKey(key, NULL, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  13639. if (ret != BAD_FUNC_ARG)
  13640. return WC_TEST_RET_ENC_EC(ret);
  13641. ret = wc_RsaExportKey(key, e, NULL, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  13642. if (ret != BAD_FUNC_ARG)
  13643. return WC_TEST_RET_ENC_EC(ret);
  13644. ret = wc_RsaExportKey(key, e, &eSz, NULL, &nSz, d, &dSz, p, &pSz, q, &qSz);
  13645. if (ret != BAD_FUNC_ARG)
  13646. return WC_TEST_RET_ENC_EC(ret);
  13647. ret = wc_RsaExportKey(key, e, &eSz, n, NULL, d, &dSz, p, &pSz, q, &qSz);
  13648. if (ret != BAD_FUNC_ARG)
  13649. return WC_TEST_RET_ENC_EC(ret);
  13650. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, NULL, &dSz, p, &pSz, q, &qSz);
  13651. if (ret != BAD_FUNC_ARG)
  13652. return WC_TEST_RET_ENC_EC(ret);
  13653. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, NULL, p, &pSz, q, &qSz);
  13654. if (ret != BAD_FUNC_ARG)
  13655. return WC_TEST_RET_ENC_EC(ret);
  13656. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, NULL, &pSz, q, &qSz);
  13657. if (ret != BAD_FUNC_ARG)
  13658. return WC_TEST_RET_ENC_EC(ret);
  13659. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, NULL, q, &qSz);
  13660. if (ret != BAD_FUNC_ARG)
  13661. return WC_TEST_RET_ENC_EC(ret);
  13662. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, NULL, &qSz);
  13663. if (ret != BAD_FUNC_ARG)
  13664. return WC_TEST_RET_ENC_EC(ret);
  13665. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, NULL);
  13666. if (ret != BAD_FUNC_ARG)
  13667. return WC_TEST_RET_ENC_EC(ret);
  13668. ret = wc_RsaExportKey(key, e, &zero, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  13669. if (ret != RSA_BUFFER_E)
  13670. return WC_TEST_RET_ENC_EC(ret);
  13671. ret = wc_RsaExportKey(key, e, &eSz, n, &zero, d, &dSz, p, &pSz, q, &qSz);
  13672. if (ret != RSA_BUFFER_E)
  13673. return WC_TEST_RET_ENC_EC(ret);
  13674. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13675. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &zero, p, &pSz, q, &qSz);
  13676. if (ret != RSA_BUFFER_E)
  13677. return WC_TEST_RET_ENC_EC(ret);
  13678. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &zero, q, &qSz);
  13679. if (ret != RSA_BUFFER_E)
  13680. return WC_TEST_RET_ENC_EC(ret);
  13681. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &zero);
  13682. if (ret != RSA_BUFFER_E)
  13683. return WC_TEST_RET_ENC_EC(ret);
  13684. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13685. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  13686. if (ret != 0)
  13687. return WC_TEST_RET_ENC_EC(ret);
  13688. return 0;
  13689. }
  13690. #endif /* !HAVE_FIPS && !USER_RSA && !NO_ASN */
  13691. #ifndef NO_SIG_WRAPPER
  13692. static wc_test_ret_t rsa_sig_test(RsaKey* key, word32 keyLen, int modLen, WC_RNG* rng)
  13693. {
  13694. wc_test_ret_t ret;
  13695. word32 sigSz;
  13696. WOLFSSL_SMALL_STACK_STATIC const byte in[] = TEST_STRING;
  13697. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  13698. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  13699. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  13700. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  13701. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  13702. };
  13703. WOLFSSL_SMALL_STACK_STATIC const byte hashEnc[] = {
  13704. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  13705. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  13706. 0x00, 0x04, 0x20,
  13707. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  13708. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  13709. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  13710. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  13711. };
  13712. word32 inLen = (word32)XSTRLEN((char*)in);
  13713. byte out[RSA_TEST_BYTES];
  13714. /* Parameter Validation testing. */
  13715. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_NONE, key, keyLen);
  13716. if (ret != BAD_FUNC_ARG)
  13717. return WC_TEST_RET_ENC_EC(ret);
  13718. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, 0);
  13719. if (ret != BAD_FUNC_ARG)
  13720. return WC_TEST_RET_ENC_EC(ret);
  13721. sigSz = (word32)modLen;
  13722. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  13723. inLen, out, &sigSz, key, keyLen, rng);
  13724. if (ret != BAD_FUNC_ARG)
  13725. return WC_TEST_RET_ENC_EC(ret);
  13726. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  13727. 0, out, &sigSz, key, keyLen, rng);
  13728. if (ret != BAD_FUNC_ARG)
  13729. return WC_TEST_RET_ENC_EC(ret);
  13730. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  13731. inLen, NULL, &sigSz, key, keyLen, rng);
  13732. if (ret != BAD_FUNC_ARG)
  13733. return WC_TEST_RET_ENC_EC(ret);
  13734. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  13735. inLen, out, NULL, key, keyLen, rng);
  13736. if (ret != BAD_FUNC_ARG)
  13737. return WC_TEST_RET_ENC_EC(ret);
  13738. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  13739. inLen, out, &sigSz, NULL, keyLen, rng);
  13740. if (ret != BAD_FUNC_ARG)
  13741. return WC_TEST_RET_ENC_EC(ret);
  13742. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  13743. inLen, out, &sigSz, key, 0, rng);
  13744. if (ret != BAD_FUNC_ARG)
  13745. return WC_TEST_RET_ENC_EC(ret);
  13746. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  13747. inLen, out, &sigSz, key, keyLen, NULL);
  13748. #ifdef HAVE_USER_RSA
  13749. /* Implementation using IPP Libraries returns:
  13750. * -101 = USER_CRYPTO_ERROR
  13751. */
  13752. if (ret == 0)
  13753. #elif defined(WOLFSSL_AFALG_XILINX_RSA) || defined(WOLFSSL_XILINX_CRYPT)
  13754. /* blinding / rng handled with hardware acceleration */
  13755. if (ret != 0)
  13756. #elif defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
  13757. /* async may not require RNG */
  13758. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  13759. if (ret != NO_VALID_DEVID)
  13760. #else
  13761. if (ret != 0 && ret != MISSING_RNG_E)
  13762. #endif
  13763. #elif defined(HAVE_FIPS) || !defined(WC_RSA_BLINDING)
  13764. /* FIPS140 implementation does not do blinding */
  13765. if (ret != 0)
  13766. #elif defined(WOLFSSL_RSA_PUBLIC_ONLY) || defined(WOLFSSL_RSA_VERIFY_ONLY)
  13767. if (ret != SIG_TYPE_E)
  13768. #elif defined(WOLFSSL_CRYPTOCELL) || defined(WOLFSSL_SE050)
  13769. /* RNG is handled by hardware */
  13770. if (ret != 0)
  13771. #else
  13772. if (ret != MISSING_RNG_E)
  13773. #endif
  13774. return WC_TEST_RET_ENC_EC(ret);
  13775. sigSz = 0;
  13776. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  13777. inLen, out, &sigSz, key, keyLen, rng);
  13778. if (ret != BAD_FUNC_ARG)
  13779. return WC_TEST_RET_ENC_EC(ret);
  13780. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  13781. inLen, out, (word32)modLen, key, keyLen);
  13782. if (ret != BAD_FUNC_ARG)
  13783. return WC_TEST_RET_ENC_EC(ret);
  13784. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  13785. 0, out, (word32)modLen, key, keyLen);
  13786. if (ret != BAD_FUNC_ARG)
  13787. return WC_TEST_RET_ENC_EC(ret);
  13788. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  13789. inLen, NULL, (word32)modLen, key, keyLen);
  13790. if (ret != BAD_FUNC_ARG)
  13791. return WC_TEST_RET_ENC_EC(ret);
  13792. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  13793. inLen, out, 0, key, keyLen);
  13794. if (ret != BAD_FUNC_ARG)
  13795. return WC_TEST_RET_ENC_EC(ret);
  13796. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  13797. inLen, out, (word32)modLen, NULL, keyLen);
  13798. if (ret != BAD_FUNC_ARG)
  13799. return WC_TEST_RET_ENC_EC(ret);
  13800. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  13801. inLen, out, (word32)modLen, key, 0);
  13802. if (ret != BAD_FUNC_ARG)
  13803. return WC_TEST_RET_ENC_EC(ret);
  13804. #ifndef HAVE_ECC
  13805. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, keyLen);
  13806. if (ret != SIG_TYPE_E)
  13807. return WC_TEST_RET_ENC_EC(ret);
  13808. #endif
  13809. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  13810. return 0;
  13811. #endif
  13812. /* Use APIs. */
  13813. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, keyLen);
  13814. if (ret != modLen)
  13815. return WC_TEST_RET_ENC_EC(ret);
  13816. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA_W_ENC, key, keyLen);
  13817. if (ret != modLen)
  13818. return WC_TEST_RET_ENC_EC(ret);
  13819. sigSz = (word32)ret;
  13820. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  13821. XMEMSET(out, 0, sizeof(out));
  13822. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  13823. inLen, out, &sigSz, key, keyLen, rng);
  13824. if (ret != 0)
  13825. return WC_TEST_RET_ENC_EC(ret);
  13826. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  13827. inLen, out, (word32)modLen, key, keyLen);
  13828. if (ret != 0)
  13829. return WC_TEST_RET_ENC_EC(ret);
  13830. sigSz = (word32)sizeof(out);
  13831. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  13832. in, inLen, out, &sigSz, key, keyLen, rng);
  13833. if (ret != 0)
  13834. return WC_TEST_RET_ENC_EC(ret);
  13835. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  13836. in, inLen, out, (word32)modLen, key, keyLen);
  13837. if (ret != 0)
  13838. return WC_TEST_RET_ENC_EC(ret);
  13839. /* Wrong signature type. */
  13840. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  13841. inLen, out, (word32)modLen, key, keyLen);
  13842. if (ret == 0)
  13843. return WC_TEST_RET_ENC_EC(ret);
  13844. /* check hash functions */
  13845. sigSz = (word32)sizeof(out);
  13846. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  13847. hash, (int)sizeof(hash), out, &sigSz, key, keyLen, rng);
  13848. if (ret != 0)
  13849. return WC_TEST_RET_ENC_EC(ret);
  13850. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  13851. hash, (int)sizeof(hash), out, (word32)modLen, key, keyLen);
  13852. if (ret != 0)
  13853. return WC_TEST_RET_ENC_EC(ret);
  13854. sigSz = (word32)sizeof(out);
  13855. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  13856. hashEnc, (int)sizeof(hashEnc), out, &sigSz, key, keyLen, rng);
  13857. if (ret != 0)
  13858. return WC_TEST_RET_ENC_EC(ret);
  13859. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  13860. hashEnc, (int)sizeof(hashEnc), out, (word32)modLen, key, keyLen);
  13861. if (ret != 0)
  13862. return WC_TEST_RET_ENC_EC(ret);
  13863. #else
  13864. (void)hash;
  13865. (void)hashEnc;
  13866. #endif /* !WOLFSSL_RSA_PUBLIC_ONLY && !WOLFSSL_RSA_VERIFY_ONLY */
  13867. return 0;
  13868. }
  13869. #endif /* !NO_SIG_WRAPPER */
  13870. #ifdef WC_RSA_NONBLOCK
  13871. static wc_test_ret_t rsa_nb_test(RsaKey* key, const byte* in, word32 inLen, byte* out,
  13872. word32 outSz, byte* plain, word32 plainSz, WC_RNG* rng)
  13873. {
  13874. wc_test_ret_t ret = 0;
  13875. int count;
  13876. int signSz = 0;
  13877. RsaNb nb;
  13878. byte* inlinePlain = NULL;
  13879. /* Enable non-blocking RSA mode - provide context */
  13880. ret = wc_RsaSetNonBlock(key, &nb);
  13881. if (ret != 0)
  13882. return ret;
  13883. #ifdef WC_RSA_NONBLOCK_TIME
  13884. /* Enable time based RSA blocking. 8 microseconds max (3.1GHz) */
  13885. ret = wc_RsaSetNonBlockTime(key, 8, 3100);
  13886. if (ret != 0)
  13887. return ret;
  13888. #endif
  13889. count = 0;
  13890. do {
  13891. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, rng);
  13892. count++; /* track number of would blocks */
  13893. if (ret == FP_WOULDBLOCK) {
  13894. /* do "other" work here */
  13895. }
  13896. } while (ret == FP_WOULDBLOCK);
  13897. if (ret < 0) {
  13898. return ret;
  13899. }
  13900. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  13901. printf("RSA non-block sign: %d times\n", count);
  13902. #endif
  13903. signSz = ret;
  13904. /* Test non-blocking verify */
  13905. XMEMSET(plain, 0, plainSz);
  13906. count = 0;
  13907. do {
  13908. ret = wc_RsaSSL_Verify(out, (word32)signSz, plain, plainSz, key);
  13909. count++; /* track number of would blocks */
  13910. if (ret == FP_WOULDBLOCK) {
  13911. /* do "other" work here */
  13912. }
  13913. } while (ret == FP_WOULDBLOCK);
  13914. if (ret < 0) {
  13915. return ret;
  13916. }
  13917. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  13918. printf("RSA non-block verify: %d times\n", count);
  13919. #endif
  13920. if (signSz == ret && XMEMCMP(plain, in, (size_t)ret)) {
  13921. return SIG_VERIFY_E;
  13922. }
  13923. /* Test inline non-blocking verify */
  13924. count = 0;
  13925. do {
  13926. ret = wc_RsaSSL_VerifyInline(out, (word32)signSz, &inlinePlain, key);
  13927. count++; /* track number of would blocks */
  13928. if (ret == FP_WOULDBLOCK) {
  13929. /* do "other" work here */
  13930. }
  13931. } while (ret == FP_WOULDBLOCK);
  13932. if (ret < 0) {
  13933. return ret;
  13934. }
  13935. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  13936. printf("RSA non-block inline verify: %d times\n", count);
  13937. #endif
  13938. if (signSz == ret && XMEMCMP(inlinePlain, in, (size_t)ret)) {
  13939. return SIG_VERIFY_E;
  13940. }
  13941. /* Disabling non-block RSA mode */
  13942. ret = wc_RsaSetNonBlock(key, NULL);
  13943. (void)count;
  13944. return 0;
  13945. }
  13946. #endif
  13947. #if !defined(HAVE_USER_RSA) && !defined(NO_ASN)
  13948. static wc_test_ret_t rsa_decode_test(RsaKey* keyPub)
  13949. {
  13950. wc_test_ret_t ret;
  13951. word32 inSz;
  13952. word32 inOutIdx;
  13953. WOLFSSL_SMALL_STACK_STATIC const byte n[2] = { 0x00, 0x23 };
  13954. WOLFSSL_SMALL_STACK_STATIC const byte e[2] = { 0x00, 0x03 };
  13955. WOLFSSL_SMALL_STACK_STATIC const byte good[] = { 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1,
  13956. 0x03 };
  13957. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgId[] = {
  13958. 0x30, 0x18, 0x30, 0x16,
  13959. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  13960. 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  13961. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgIdNull[] = {
  13962. 0x30, 0x1a, 0x30, 0x18,
  13963. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  13964. 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  13965. 0x02, 0x1, 0x03 };
  13966. WOLFSSL_SMALL_STACK_STATIC const byte badAlgIdNull[] = {
  13967. 0x30, 0x1b, 0x30, 0x19,
  13968. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  13969. 0x05, 0x01, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  13970. 0x02, 0x1, 0x03 };
  13971. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitString[] = {
  13972. 0x30, 0x18, 0x30, 0x16,
  13973. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  13974. 0x04, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  13975. WOLFSSL_SMALL_STACK_STATIC const byte badBitStringLen[] = {
  13976. 0x30, 0x18, 0x30, 0x16,
  13977. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  13978. 0x03, 0x0a, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  13979. WOLFSSL_SMALL_STACK_STATIC const byte badNoSeq[] = {
  13980. 0x30, 0x16, 0x30, 0x14,
  13981. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  13982. 0x07, 0x00, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  13983. WOLFSSL_SMALL_STACK_STATIC const byte badNoObj[] = {
  13984. 0x30, 0x0f, 0x30, 0x0d, 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06,
  13985. 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  13986. WOLFSSL_SMALL_STACK_STATIC const byte badIntN[] = {
  13987. 0x30, 0x06, 0x02, 0x05, 0x23, 0x02, 0x1, 0x03 };
  13988. WOLFSSL_SMALL_STACK_STATIC const byte badNotIntE[] = {
  13989. 0x30, 0x06, 0x02, 0x01, 0x23, 0x04, 0x1, 0x03 };
  13990. WOLFSSL_SMALL_STACK_STATIC const byte badLength[] = {
  13991. 0x30, 0x04, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  13992. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrNoZero[] = {
  13993. 0x30, 0x17, 0x30, 0x15,
  13994. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  13995. 0x03, 0x08, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  13996. ret = wc_InitRsaKey(keyPub, NULL);
  13997. if (ret != 0)
  13998. return WC_TEST_RET_ENC_EC(ret);
  13999. /* Parameter Validation testing. */
  14000. ret = wc_RsaPublicKeyDecodeRaw(NULL, sizeof(n), e, sizeof(e), keyPub);
  14001. if (ret != BAD_FUNC_ARG) {
  14002. ret = WC_TEST_RET_ENC_EC(ret);
  14003. goto done;
  14004. }
  14005. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), NULL, sizeof(e), keyPub);
  14006. if (ret != BAD_FUNC_ARG) {
  14007. ret = WC_TEST_RET_ENC_EC(ret);
  14008. goto done;
  14009. }
  14010. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), NULL);
  14011. if (ret != BAD_FUNC_ARG) {
  14012. ret = WC_TEST_RET_ENC_EC(ret);
  14013. goto done;
  14014. }
  14015. ret = wc_RsaPublicKeyDecodeRaw(n, (word32)-1, e, sizeof(e), keyPub);
  14016. #if !defined(WOLFSSL_SP_MATH) & !defined(WOLFSSL_SP_MATH_ALL)
  14017. if (ret != 0)
  14018. #else
  14019. if (ret != ASN_GETINT_E)
  14020. #endif
  14021. {
  14022. ret = WC_TEST_RET_ENC_EC(ret);
  14023. goto done;
  14024. }
  14025. wc_FreeRsaKey(keyPub);
  14026. ret = wc_InitRsaKey(keyPub, NULL);
  14027. if (ret != 0)
  14028. return WC_TEST_RET_ENC_EC(ret);
  14029. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, (word32)-1, keyPub);
  14030. #if !defined(WOLFSSL_SP_MATH) & !defined(WOLFSSL_SP_MATH_ALL)
  14031. if (ret != 0) {
  14032. #else
  14033. if (ret != ASN_GETINT_E) {
  14034. #endif
  14035. ret = WC_TEST_RET_ENC_EC(ret);
  14036. goto done;
  14037. }
  14038. wc_FreeRsaKey(keyPub);
  14039. ret = wc_InitRsaKey(keyPub, NULL);
  14040. if (ret != 0)
  14041. return WC_TEST_RET_ENC_EC(ret);
  14042. /* Use API. */
  14043. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), keyPub);
  14044. if (ret != 0) {
  14045. ret = WC_TEST_RET_ENC_EC(ret);
  14046. goto done;
  14047. }
  14048. wc_FreeRsaKey(keyPub);
  14049. ret = wc_InitRsaKey(keyPub, NULL);
  14050. if (ret != 0)
  14051. return WC_TEST_RET_ENC_EC(ret);
  14052. /* Parameter Validation testing. */
  14053. inSz = sizeof(good);
  14054. ret = wc_RsaPublicKeyDecode(NULL, &inOutIdx, keyPub, inSz);
  14055. if (ret != BAD_FUNC_ARG) {
  14056. ret = WC_TEST_RET_ENC_EC(ret);
  14057. goto done;
  14058. }
  14059. ret = wc_RsaPublicKeyDecode(good, NULL, keyPub, inSz);
  14060. if (ret != BAD_FUNC_ARG) {
  14061. ret = WC_TEST_RET_ENC_EC(ret);
  14062. goto done;
  14063. }
  14064. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  14065. if (ret != BAD_FUNC_ARG) {
  14066. ret = WC_TEST_RET_ENC_EC(ret);
  14067. goto done;
  14068. }
  14069. /* Use good data and offset to bad data. */
  14070. inOutIdx = 2;
  14071. inSz = sizeof(good) - inOutIdx;
  14072. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  14073. if (ret != ASN_PARSE_E) {
  14074. ret = WC_TEST_RET_ENC_EC(ret);
  14075. goto done;
  14076. }
  14077. inOutIdx = 2;
  14078. inSz = sizeof(goodAlgId) - inOutIdx;
  14079. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  14080. if (ret != ASN_PARSE_E) {
  14081. ret = WC_TEST_RET_ENC_EC(ret);
  14082. goto done;
  14083. }
  14084. inOutIdx = 2;
  14085. inSz = sizeof(goodAlgId);
  14086. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  14087. #ifndef WOLFSSL_NO_DECODE_EXTRA
  14088. if (ret != ASN_PARSE_E)
  14089. #else
  14090. if (ret != ASN_RSA_KEY_E)
  14091. #endif
  14092. {
  14093. ret = WC_TEST_RET_ENC_EC(ret);
  14094. goto done;
  14095. }
  14096. /* Try different bad data. */
  14097. inSz = sizeof(badAlgIdNull);
  14098. inOutIdx = 0;
  14099. ret = wc_RsaPublicKeyDecode(badAlgIdNull, &inOutIdx, keyPub, inSz);
  14100. if (ret != ASN_EXPECT_0_E) {
  14101. ret = WC_TEST_RET_ENC_EC(ret);
  14102. goto done;
  14103. }
  14104. inSz = sizeof(badNotBitString);
  14105. inOutIdx = 0;
  14106. ret = wc_RsaPublicKeyDecode(badNotBitString, &inOutIdx, keyPub, inSz);
  14107. if (ret != ASN_BITSTR_E) {
  14108. ret = WC_TEST_RET_ENC_EC(ret);
  14109. goto done;
  14110. }
  14111. inSz = sizeof(badBitStringLen);
  14112. inOutIdx = 0;
  14113. ret = wc_RsaPublicKeyDecode(badBitStringLen, &inOutIdx, keyPub, inSz);
  14114. if (ret != ASN_PARSE_E) {
  14115. ret = WC_TEST_RET_ENC_EC(ret);
  14116. goto done;
  14117. }
  14118. inSz = sizeof(badNoSeq);
  14119. inOutIdx = 0;
  14120. ret = wc_RsaPublicKeyDecode(badNoSeq, &inOutIdx, keyPub, inSz);
  14121. if (ret != ASN_PARSE_E) {
  14122. ret = WC_TEST_RET_ENC_EC(ret);
  14123. goto done;
  14124. }
  14125. inSz = sizeof(badNoObj);
  14126. inOutIdx = 0;
  14127. ret = wc_RsaPublicKeyDecode(badNoObj, &inOutIdx, keyPub, inSz);
  14128. if (ret != ASN_PARSE_E && ret != ASN_OBJECT_ID_E) {
  14129. ret = WC_TEST_RET_ENC_EC(ret);
  14130. goto done;
  14131. }
  14132. inSz = sizeof(badIntN);
  14133. inOutIdx = 0;
  14134. ret = wc_RsaPublicKeyDecode(badIntN, &inOutIdx, keyPub, inSz);
  14135. if (ret != ASN_RSA_KEY_E && ret != ASN_PARSE_E) {
  14136. ret = WC_TEST_RET_ENC_EC(ret);
  14137. goto done;
  14138. }
  14139. inSz = sizeof(badNotIntE);
  14140. inOutIdx = 0;
  14141. ret = wc_RsaPublicKeyDecode(badNotIntE, &inOutIdx, keyPub, inSz);
  14142. if (ret != ASN_RSA_KEY_E && ret != ASN_PARSE_E) {
  14143. ret = WC_TEST_RET_ENC_EC(ret);
  14144. goto done;
  14145. }
  14146. /* TODO: Shouldn't pass as the sequence length is too small. */
  14147. inSz = sizeof(badLength);
  14148. inOutIdx = 0;
  14149. ret = wc_RsaPublicKeyDecode(badLength, &inOutIdx, keyPub, inSz);
  14150. #ifndef WOLFSSL_ASN_TEMPLATE
  14151. if (ret != 0)
  14152. #else
  14153. if (ret != ASN_PARSE_E)
  14154. #endif
  14155. {
  14156. ret = WC_TEST_RET_ENC_EC(ret);
  14157. goto done;
  14158. }
  14159. /* TODO: Shouldn't ignore object id's data. */
  14160. wc_FreeRsaKey(keyPub);
  14161. ret = wc_InitRsaKey(keyPub, NULL);
  14162. if (ret != 0)
  14163. return WC_TEST_RET_ENC_EC(ret);
  14164. inSz = sizeof(badBitStrNoZero);
  14165. inOutIdx = 0;
  14166. ret = wc_RsaPublicKeyDecode(badBitStrNoZero, &inOutIdx, keyPub, inSz);
  14167. if (ret != ASN_EXPECT_0_E && ret != ASN_PARSE_E) {
  14168. ret = WC_TEST_RET_ENC_EC(ret);
  14169. goto done;
  14170. }
  14171. wc_FreeRsaKey(keyPub);
  14172. ret = wc_InitRsaKey(keyPub, NULL);
  14173. if (ret != 0)
  14174. return WC_TEST_RET_ENC_EC(ret);
  14175. /* Valid data cases. */
  14176. inSz = sizeof(good);
  14177. inOutIdx = 0;
  14178. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  14179. if (ret != 0) {
  14180. ret = WC_TEST_RET_ENC_EC(ret);
  14181. goto done;
  14182. }
  14183. if (inOutIdx != inSz) {
  14184. ret = WC_TEST_RET_ENC_NC;
  14185. goto done;
  14186. }
  14187. wc_FreeRsaKey(keyPub);
  14188. ret = wc_InitRsaKey(keyPub, NULL);
  14189. if (ret != 0)
  14190. return WC_TEST_RET_ENC_EC(ret);
  14191. inSz = sizeof(goodAlgId);
  14192. inOutIdx = 0;
  14193. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  14194. if (ret != 0) {
  14195. ret = WC_TEST_RET_ENC_EC(ret);
  14196. goto done;
  14197. }
  14198. if (inOutIdx != inSz) {
  14199. ret = WC_TEST_RET_ENC_NC;
  14200. goto done;
  14201. }
  14202. wc_FreeRsaKey(keyPub);
  14203. ret = wc_InitRsaKey(keyPub, NULL);
  14204. if (ret != 0)
  14205. return WC_TEST_RET_ENC_EC(ret);
  14206. inSz = sizeof(goodAlgIdNull);
  14207. inOutIdx = 0;
  14208. ret = wc_RsaPublicKeyDecode(goodAlgIdNull, &inOutIdx, keyPub, inSz);
  14209. if (ret != 0) {
  14210. ret = WC_TEST_RET_ENC_EC(ret);
  14211. goto done;
  14212. }
  14213. if (inOutIdx != inSz) {
  14214. ret = WC_TEST_RET_ENC_NC;
  14215. goto done;
  14216. }
  14217. done:
  14218. wc_FreeRsaKey(keyPub);
  14219. return ret;
  14220. }
  14221. #endif
  14222. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  14223. /* Need to create known good signatures to test with this. */
  14224. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  14225. !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14226. static wc_test_ret_t rsa_pss_test(WC_RNG* rng, RsaKey* key)
  14227. {
  14228. byte digest[WC_MAX_DIGEST_SIZE];
  14229. wc_test_ret_t ret = 0;
  14230. const char inStr[] = TEST_STRING;
  14231. word32 inLen = (word32)TEST_STRING_SZ;
  14232. word32 outSz;
  14233. word32 plainSz;
  14234. word32 digestSz;
  14235. int i, j;
  14236. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  14237. int k, l;
  14238. #endif
  14239. #ifndef WOLFSSL_SE050
  14240. int len;
  14241. #endif
  14242. byte* plain;
  14243. int mgf[] = {
  14244. #ifndef NO_SHA
  14245. WC_MGF1SHA1,
  14246. #endif
  14247. #ifdef WOLFSSL_SHA224
  14248. WC_MGF1SHA224,
  14249. #endif
  14250. WC_MGF1SHA256,
  14251. #ifdef WOLFSSL_SHA384
  14252. WC_MGF1SHA384,
  14253. #endif
  14254. #ifdef WOLFSSL_SHA512
  14255. WC_MGF1SHA512
  14256. #endif
  14257. };
  14258. enum wc_HashType hash[] = {
  14259. #ifndef NO_SHA
  14260. WC_HASH_TYPE_SHA,
  14261. #endif
  14262. #ifdef WOLFSSL_SHA224
  14263. WC_HASH_TYPE_SHA224,
  14264. #endif
  14265. WC_HASH_TYPE_SHA256,
  14266. #ifdef WOLFSSL_SHA384
  14267. WC_HASH_TYPE_SHA384,
  14268. #endif
  14269. #ifdef WOLFSSL_SHA512
  14270. WC_HASH_TYPE_SHA512,
  14271. #endif
  14272. };
  14273. WC_DECLARE_VAR(in, byte, RSA_TEST_BYTES, HEAP_HINT);
  14274. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  14275. WC_DECLARE_VAR(sig, byte, RSA_TEST_BYTES, HEAP_HINT);
  14276. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  14277. if (in == NULL || out == NULL || sig == NULL)
  14278. ERROR_OUT(MEMORY_E, exit_rsa_pss);
  14279. #endif
  14280. XMEMCPY(in, inStr, inLen);
  14281. /* Test all combinations of hash and MGF. */
  14282. for (j = 0; j < (int)(sizeof(hash)/sizeof(*hash)); j++) {
  14283. /* Calculate hash of message. */
  14284. ret = wc_Hash(hash[j], in, inLen, digest, sizeof(digest));
  14285. if (ret != 0)
  14286. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  14287. digestSz = wc_HashGetDigestSize(hash[j]);
  14288. #ifdef WOLFSSL_SE050
  14289. /* SE050 only supports MGF matched to same hash type */
  14290. i = j;
  14291. #else
  14292. for (i = 0; i < (int)(sizeof(mgf)/sizeof(*mgf)); i++) {
  14293. #endif
  14294. outSz = RSA_TEST_BYTES;
  14295. do {
  14296. #if defined(WOLFSSL_ASYNC_CRYPT)
  14297. ret = wc_AsyncWait(ret, &key->asyncDev,
  14298. WC_ASYNC_FLAG_CALL_AGAIN);
  14299. #endif
  14300. if (ret >= 0) {
  14301. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz,
  14302. hash[j], mgf[i], -1, key, rng);
  14303. }
  14304. } while (ret == WC_PENDING_E);
  14305. if (ret <= 0)
  14306. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  14307. outSz = (word32)ret;
  14308. XMEMCPY(sig, out, outSz);
  14309. plain = NULL;
  14310. TEST_SLEEP();
  14311. do {
  14312. #if defined(WOLFSSL_ASYNC_CRYPT)
  14313. ret = wc_AsyncWait(ret, &key->asyncDev,
  14314. WC_ASYNC_FLAG_CALL_AGAIN);
  14315. #endif
  14316. if (ret >= 0) {
  14317. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[j],
  14318. mgf[i], -1, key);
  14319. }
  14320. } while (ret == WC_PENDING_E);
  14321. if (ret <= 0)
  14322. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  14323. plainSz = (word32)ret;
  14324. TEST_SLEEP();
  14325. #if defined(HAVE_SELFTEST) && \
  14326. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  14327. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  14328. hash[j], -1);
  14329. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  14330. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  14331. hash[j], -1, 0);
  14332. #else
  14333. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz,
  14334. hash[j], -1, wc_RsaEncryptSize(key)*8, HEAP_HINT);
  14335. #endif
  14336. if (ret != 0)
  14337. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  14338. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  14339. for (k = 0; k < (int)(sizeof(mgf)/sizeof(*mgf)); k++) {
  14340. for (l = 0; l < (int)(sizeof(hash)/sizeof(*hash)); l++) {
  14341. if (i == k && j == l)
  14342. continue;
  14343. XMEMCPY(sig, out, outSz);
  14344. do {
  14345. #if defined(WOLFSSL_ASYNC_CRYPT)
  14346. ret = wc_AsyncWait(ret, &key->asyncDev,
  14347. WC_ASYNC_FLAG_CALL_AGAIN);
  14348. #endif
  14349. if (ret >= 0) {
  14350. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz,
  14351. (byte**)&plain, hash[l], mgf[k], -1, key);
  14352. }
  14353. } while (ret == WC_PENDING_E);
  14354. if (ret >= 0)
  14355. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  14356. }
  14357. }
  14358. #endif
  14359. #ifndef WOLFSSL_SE050
  14360. } /* end mgf for loop */
  14361. #endif
  14362. }
  14363. /* SE050 generates salts internally only of hash length */
  14364. #ifndef WOLFSSL_SE050
  14365. /* Test that a salt length of zero works. */
  14366. digestSz = wc_HashGetDigestSize(hash[0]);
  14367. outSz = RSA_TEST_BYTES;
  14368. do {
  14369. #if defined(WOLFSSL_ASYNC_CRYPT)
  14370. ret = wc_AsyncWait(ret, &key->asyncDev,
  14371. WC_ASYNC_FLAG_CALL_AGAIN);
  14372. #endif
  14373. if (ret >= 0) {
  14374. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  14375. mgf[0], 0, key, rng);
  14376. }
  14377. } while (ret == WC_PENDING_E);
  14378. if (ret <= 0)
  14379. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  14380. outSz = (word32)ret;
  14381. TEST_SLEEP();
  14382. do {
  14383. #if defined(WOLFSSL_ASYNC_CRYPT)
  14384. ret = wc_AsyncWait(ret, &key->asyncDev,
  14385. WC_ASYNC_FLAG_CALL_AGAIN);
  14386. #endif
  14387. if (ret >= 0) {
  14388. ret = wc_RsaPSS_Verify_ex(out, outSz, sig, outSz, hash[0], mgf[0],
  14389. 0, key);
  14390. }
  14391. } while (ret == WC_PENDING_E);
  14392. if (ret <= 0)
  14393. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  14394. plainSz = (word32)ret;
  14395. TEST_SLEEP();
  14396. do {
  14397. #if defined(WOLFSSL_ASYNC_CRYPT)
  14398. ret = wc_AsyncWait(ret, &key->asyncDev,
  14399. WC_ASYNC_FLAG_CALL_AGAIN);
  14400. #endif
  14401. if (ret >= 0) {
  14402. #if defined(HAVE_SELFTEST) && \
  14403. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  14404. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  14405. hash[0], 0);
  14406. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  14407. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  14408. hash[0], 0, 0);
  14409. #else
  14410. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, sig, plainSz,
  14411. hash[0], 0, 0, HEAP_HINT);
  14412. #endif
  14413. }
  14414. } while (ret == WC_PENDING_E);
  14415. if (ret != 0)
  14416. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  14417. XMEMCPY(sig, out, outSz);
  14418. plain = NULL;
  14419. do {
  14420. #if defined(WOLFSSL_ASYNC_CRYPT)
  14421. ret = wc_AsyncWait(ret, &key->asyncDev,
  14422. WC_ASYNC_FLAG_CALL_AGAIN);
  14423. #endif
  14424. if (ret >= 0) {
  14425. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  14426. 0, key);
  14427. }
  14428. } while (ret == WC_PENDING_E);
  14429. if (ret <= 0)
  14430. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  14431. plainSz = (word32)ret;
  14432. TEST_SLEEP();
  14433. #if defined(HAVE_SELFTEST) && \
  14434. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  14435. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  14436. hash[0], 0);
  14437. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  14438. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  14439. hash[0], 0, 0);
  14440. #else
  14441. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  14442. 0, 0, HEAP_HINT);
  14443. #endif
  14444. if (ret != 0)
  14445. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  14446. /* Test bad salt lengths in various APIs. */
  14447. digestSz = wc_HashGetDigestSize(hash[0]);
  14448. outSz = RSA_TEST_BYTES;
  14449. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  14450. len = -2;
  14451. #else
  14452. len = -3;
  14453. #endif
  14454. do {
  14455. #if defined(WOLFSSL_ASYNC_CRYPT)
  14456. ret = wc_AsyncWait(ret, &key->asyncDev,
  14457. WC_ASYNC_FLAG_CALL_AGAIN);
  14458. #endif
  14459. if (ret >= 0) {
  14460. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  14461. mgf[0], len, key, rng);
  14462. }
  14463. } while (ret == WC_PENDING_E);
  14464. if (ret != PSS_SALTLEN_E)
  14465. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  14466. do {
  14467. #if defined(WOLFSSL_ASYNC_CRYPT)
  14468. ret = wc_AsyncWait(ret, &key->asyncDev,
  14469. WC_ASYNC_FLAG_CALL_AGAIN);
  14470. #endif
  14471. if (ret >= 0) {
  14472. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  14473. mgf[0], digestSz + 1, key, rng);
  14474. }
  14475. } while (ret == WC_PENDING_E);
  14476. if (ret != PSS_SALTLEN_E)
  14477. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  14478. TEST_SLEEP();
  14479. do {
  14480. #if defined(WOLFSSL_ASYNC_CRYPT)
  14481. ret = wc_AsyncWait(ret, &key->asyncDev,
  14482. WC_ASYNC_FLAG_CALL_AGAIN);
  14483. #endif
  14484. if (ret >= 0) {
  14485. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0],
  14486. mgf[0], -2, key);
  14487. }
  14488. } while (ret == WC_PENDING_E);
  14489. if (ret != PSS_SALTLEN_E)
  14490. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  14491. TEST_SLEEP();
  14492. do {
  14493. #if defined(WOLFSSL_ASYNC_CRYPT)
  14494. ret = wc_AsyncWait(ret, &key->asyncDev,
  14495. WC_ASYNC_FLAG_CALL_AGAIN);
  14496. #endif
  14497. if (ret >= 0) {
  14498. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  14499. digestSz + 1, key);
  14500. }
  14501. } while (ret == WC_PENDING_E);
  14502. if (ret != PSS_SALTLEN_E)
  14503. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  14504. TEST_SLEEP();
  14505. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  14506. len = -2;
  14507. #else
  14508. len = -3;
  14509. #endif
  14510. #if defined(HAVE_SELFTEST) && \
  14511. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  14512. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  14513. hash[0], len);
  14514. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  14515. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  14516. hash[0], len, 0);
  14517. #else
  14518. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  14519. len, 0, HEAP_HINT);
  14520. #endif
  14521. if (ret != PSS_SALTLEN_E)
  14522. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  14523. #ifndef WOLFSSL_PSS_LONG_SALT
  14524. len = digestSz + 1;
  14525. #else
  14526. len = plainSz - digestSz - 1;
  14527. #endif
  14528. #if defined(HAVE_SELFTEST) && \
  14529. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  14530. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  14531. hash[0], len);
  14532. if (ret != PSS_SALTLEN_E)
  14533. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  14534. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  14535. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  14536. hash[0], len, 0);
  14537. if (ret != BAD_PADDING_E)
  14538. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  14539. #else
  14540. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  14541. len, 0, HEAP_HINT);
  14542. if (ret != PSS_SALTLEN_E)
  14543. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_pss);
  14544. #endif
  14545. ret = 0;
  14546. #endif /* WOLFSSL_SE050 */
  14547. exit_rsa_pss:
  14548. WC_FREE_VAR(sig, HEAP_HINT);
  14549. WC_FREE_VAR(in, HEAP_HINT);
  14550. WC_FREE_VAR(out, HEAP_HINT);
  14551. return ret;
  14552. }
  14553. #endif /* !WOLFSSL_RSA_VERIFY_ONLY && !WOLFSSL_RSA_PUBLIC_ONLY */
  14554. #endif
  14555. #ifdef WC_RSA_NO_PADDING
  14556. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_no_pad_test(void)
  14557. {
  14558. WC_RNG rng;
  14559. byte* tmp = NULL;
  14560. size_t bytes;
  14561. wc_test_ret_t ret;
  14562. word32 inLen = 0;
  14563. word32 idx = 0;
  14564. word32 outSz = RSA_TEST_BYTES;
  14565. word32 plainSz = RSA_TEST_BYTES;
  14566. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  14567. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  14568. !defined(NO_FILESYSTEM)
  14569. XFILE file;
  14570. #endif
  14571. WC_DECLARE_VAR(key, RsaKey, 1, HEAP_HINT);
  14572. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  14573. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  14574. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  14575. if (key == NULL || out == NULL || plain == NULL)
  14576. ERROR_OUT(MEMORY_E, exit_rsa_nopadding);
  14577. #endif
  14578. /* initialize stack structures */
  14579. XMEMSET(&rng, 0, sizeof(rng));
  14580. XMEMSET(key, 0, sizeof(RsaKey));
  14581. #ifdef USE_CERT_BUFFERS_1024
  14582. bytes = (size_t)sizeof_client_key_der_1024;
  14583. if (bytes < (size_t)sizeof_client_cert_der_1024)
  14584. bytes = (size_t)sizeof_client_cert_der_1024;
  14585. #elif defined(USE_CERT_BUFFERS_2048)
  14586. bytes = (size_t)sizeof_client_key_der_2048;
  14587. if (bytes < (size_t)sizeof_client_cert_der_2048)
  14588. bytes = (size_t)sizeof_client_cert_der_2048;
  14589. #else
  14590. bytes = FOURK_BUF;
  14591. #endif
  14592. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14593. if (tmp == NULL
  14594. #ifdef WOLFSSL_ASYNC_CRYPT
  14595. || out == NULL || plain == NULL
  14596. #endif
  14597. ) {
  14598. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  14599. }
  14600. #ifdef USE_CERT_BUFFERS_1024
  14601. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  14602. #elif defined(USE_CERT_BUFFERS_2048)
  14603. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  14604. #elif defined(USE_CERT_BUFFERS_3072)
  14605. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  14606. #elif defined(USE_CERT_BUFFERS_4096)
  14607. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  14608. #elif !defined(NO_FILESYSTEM)
  14609. file = XFOPEN(clientKey, "rb");
  14610. if (!file) {
  14611. err_sys("can't open clientKey, Please run from wolfSSL home dir",
  14612. WC_TEST_RET_ENC_ERRNO);
  14613. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa_nopadding);
  14614. }
  14615. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  14616. XFCLOSE(file);
  14617. if (bytes == 0)
  14618. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa_nopadding);
  14619. #else
  14620. /* No key to use. */
  14621. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  14622. #endif /* USE_CERT_BUFFERS */
  14623. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  14624. if (ret != 0) {
  14625. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  14626. }
  14627. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  14628. if (ret != 0) {
  14629. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  14630. }
  14631. /* after loading in key use tmp as the test buffer */
  14632. #ifndef HAVE_FIPS
  14633. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  14634. #else
  14635. ret = wc_InitRng(&rng);
  14636. #endif
  14637. if (ret != 0) {
  14638. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  14639. }
  14640. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  14641. inLen = wc_RsaEncryptSize(key);
  14642. outSz = inLen;
  14643. plainSz = inLen;
  14644. XMEMSET(tmp, 7, inLen);
  14645. do {
  14646. #if defined(WOLFSSL_ASYNC_CRYPT)
  14647. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14648. #endif
  14649. if (ret >= 0) {
  14650. ret = wc_RsaDirect(tmp, inLen, out, &outSz, key,
  14651. RSA_PRIVATE_ENCRYPT, &rng);
  14652. }
  14653. } while (ret == WC_PENDING_E);
  14654. if (ret <= 0) {
  14655. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  14656. }
  14657. /* encrypted result should not be the same as input */
  14658. if (XMEMCMP(out, tmp, inLen) == 0) {
  14659. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  14660. }
  14661. TEST_SLEEP();
  14662. /* decrypt with public key and compare result */
  14663. do {
  14664. #if defined(WOLFSSL_ASYNC_CRYPT)
  14665. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14666. #endif
  14667. if (ret >= 0) {
  14668. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key,
  14669. RSA_PUBLIC_DECRYPT, &rng);
  14670. }
  14671. } while (ret == WC_PENDING_E);
  14672. if (ret <= 0) {
  14673. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  14674. }
  14675. if (XMEMCMP(plain, tmp, inLen) != 0) {
  14676. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  14677. }
  14678. TEST_SLEEP();
  14679. #endif
  14680. #ifdef WC_RSA_BLINDING
  14681. ret = wc_RsaSetRNG(NULL, &rng);
  14682. if (ret != BAD_FUNC_ARG) {
  14683. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  14684. }
  14685. ret = wc_RsaSetRNG(key, &rng);
  14686. if (ret < 0) {
  14687. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  14688. }
  14689. #endif
  14690. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  14691. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  14692. do {
  14693. #if defined(WOLFSSL_ASYNC_CRYPT)
  14694. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14695. #endif
  14696. if (ret >= 0) {
  14697. ret = wc_RsaPublicEncrypt_ex(tmp, inLen, out, (int)outSz, key, &rng,
  14698. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  14699. }
  14700. } while (ret == WC_PENDING_E);
  14701. if (ret < 0) {
  14702. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  14703. }
  14704. TEST_SLEEP();
  14705. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  14706. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14707. do {
  14708. #if defined(WOLFSSL_ASYNC_CRYPT)
  14709. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14710. #endif
  14711. if (ret >= 0) {
  14712. ret = wc_RsaPrivateDecrypt_ex(out, outSz, plain, (int)plainSz, key,
  14713. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  14714. }
  14715. } while (ret == WC_PENDING_E);
  14716. if (ret < 0) {
  14717. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  14718. }
  14719. if (XMEMCMP(plain, tmp, inLen) != 0) {
  14720. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_nopadding);
  14721. }
  14722. TEST_SLEEP();
  14723. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14724. /* test some bad arguments */
  14725. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key, -1,
  14726. &rng);
  14727. if (ret != BAD_FUNC_ARG) {
  14728. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  14729. }
  14730. ret = wc_RsaDirect(out, outSz, plain, &plainSz, NULL, RSA_PUBLIC_DECRYPT,
  14731. &rng);
  14732. if (ret != BAD_FUNC_ARG) {
  14733. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  14734. }
  14735. ret = wc_RsaDirect(out, outSz, NULL, &plainSz, key, RSA_PUBLIC_DECRYPT,
  14736. &rng);
  14737. if (ret != LENGTH_ONLY_E || plainSz != inLen) {
  14738. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  14739. }
  14740. ret = wc_RsaDirect(out, outSz - 10, plain, &plainSz, key,
  14741. RSA_PUBLIC_DECRYPT, &rng);
  14742. if (ret != BAD_FUNC_ARG) {
  14743. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_nopadding);
  14744. }
  14745. /* if making it to this point of code without hitting an ERROR_OUT then
  14746. * all tests have passed */
  14747. ret = 0;
  14748. exit_rsa_nopadding:
  14749. wc_FreeRsaKey(key);
  14750. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14751. WC_FREE_VAR(key, HEAP_HINT);
  14752. WC_FREE_VAR(out, HEAP_HINT);
  14753. WC_FREE_VAR(plain, HEAP_HINT);
  14754. wc_FreeRng(&rng);
  14755. return ret;
  14756. }
  14757. #endif /* WC_RSA_NO_PADDING */
  14758. #if defined(WOLFSSL_HAVE_SP_RSA) && defined(USE_FAST_MATH)
  14759. static wc_test_ret_t rsa_even_mod_test(WC_RNG* rng, RsaKey* key)
  14760. {
  14761. byte* tmp = NULL;
  14762. size_t bytes;
  14763. wc_test_ret_t ret;
  14764. word32 inLen = 0;
  14765. #ifndef NO_ASN
  14766. word32 idx = 0;
  14767. #endif
  14768. word32 outSz = RSA_TEST_BYTES;
  14769. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14770. word32 plainSz = RSA_TEST_BYTES;
  14771. #endif
  14772. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_3072) && \
  14773. !defined(USE_CERT_BUFFERS_4096) && !defined(NO_FILESYSTEM)
  14774. XFILE file;
  14775. #endif
  14776. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  14777. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14778. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  14779. #endif
  14780. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  14781. if (out == NULL
  14782. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14783. || plain == NULL
  14784. #endif
  14785. ) {
  14786. ERROR_OUT(MEMORY_E, exit_rsa_even_mod);
  14787. }
  14788. #endif
  14789. #if defined(USE_CERT_BUFFERS_2048)
  14790. bytes = (size_t)sizeof_client_key_der_2048;
  14791. if (bytes < (size_t)sizeof_client_cert_der_2048)
  14792. bytes = (size_t)sizeof_client_cert_der_2048;
  14793. #else
  14794. bytes = FOURK_BUF;
  14795. #endif
  14796. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14797. if (tmp == NULL
  14798. #ifdef WOLFSSL_ASYNC_CRYPT
  14799. || out == NULL || plain == NULL
  14800. #endif
  14801. ) {
  14802. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_even_mod);
  14803. }
  14804. #if defined(USE_CERT_BUFFERS_2048)
  14805. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  14806. #elif defined(USE_CERT_BUFFERS_3072)
  14807. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  14808. #elif defined(USE_CERT_BUFFERS_4096)
  14809. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  14810. #elif !defined(NO_FILESYSTEM)
  14811. file = XFOPEN(clientKey, "rb");
  14812. if (!file) {
  14813. err_sys("can't open ./certs/client-key.der, "
  14814. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  14815. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa_even_mod);
  14816. }
  14817. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  14818. XFCLOSE(file);
  14819. if (bytes == 0)
  14820. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa_even_mod);
  14821. #else
  14822. /* No key to use. */
  14823. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa_even_mod);
  14824. #endif /* USE_CERT_BUFFERS */
  14825. #ifndef NO_ASN
  14826. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  14827. if (ret != 0) {
  14828. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  14829. }
  14830. #else
  14831. #ifdef USE_CERT_BUFFERS_2048
  14832. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  14833. if (ret != 0) {
  14834. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  14835. }
  14836. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  14837. if (ret != 0) {
  14838. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  14839. }
  14840. #ifndef NO_SIG_WRAPPER
  14841. modLen = 2048;
  14842. #endif
  14843. #else
  14844. #error Not supported yet!
  14845. #endif
  14846. #endif
  14847. key->n.dp[0] &= (mp_digit)-2;
  14848. if (ret != 0) {
  14849. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  14850. }
  14851. /* after loading in key use tmp as the test buffer */
  14852. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  14853. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM))) && \
  14854. !defined(WOLFSSL_XILINX_CRYPT)
  14855. /* The ARM64_ASM code that was FIPS validated did not return these expected
  14856. * failure codes. These tests cases were added after the assembly was
  14857. * in-lined in the module and validated, these tests will be available in
  14858. * the 140-3 module */
  14859. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  14860. inLen = 32;
  14861. outSz = wc_RsaEncryptSize(key);
  14862. XMEMSET(tmp, 7, plainSz);
  14863. ret = wc_RsaSSL_Sign(tmp, inLen, out, outSz, key, rng);
  14864. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  14865. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  14866. }
  14867. ret = wc_RsaSSL_Verify(out, outSz, tmp, inLen, key);
  14868. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  14869. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  14870. }
  14871. #endif
  14872. #ifdef WC_RSA_BLINDING
  14873. ret = wc_RsaSetRNG(key, rng);
  14874. if (ret < 0) {
  14875. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  14876. }
  14877. #endif
  14878. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  14879. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  14880. ret = wc_RsaPublicEncrypt(tmp, inLen, out, (int)outSz, key, rng);
  14881. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  14882. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  14883. }
  14884. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  14885. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14886. ret = wc_RsaPrivateDecrypt(out, outSz, plain, (int)plainSz, key);
  14887. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  14888. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa_even_mod);
  14889. }
  14890. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14891. #endif /* !(HAVE_FIPS_VERSION == 2 && WOLFSSL_SP_ARMxx_ASM) */
  14892. /* if making it to this point of code without hitting an ERROR_OUT then
  14893. * all tests have passed */
  14894. ret = 0;
  14895. exit_rsa_even_mod:
  14896. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14897. WC_FREE_VAR(out, HEAP_HINT);
  14898. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14899. WC_FREE_VAR(plain, HEAP_HINT);
  14900. #endif
  14901. (void)out;
  14902. (void)outSz;
  14903. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14904. (void)plain;
  14905. (void)plainSz;
  14906. #endif
  14907. (void)inLen;
  14908. (void)rng;
  14909. return ret;
  14910. }
  14911. #endif /* WOLFSSL_HAVE_SP_RSA */
  14912. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  14913. static wc_test_ret_t rsa_certgen_test(RsaKey* key, RsaKey* keypub, WC_RNG* rng, byte* tmp)
  14914. {
  14915. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14916. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14917. #ifdef WOLFSSL_TEST_CERT
  14918. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14919. #endif
  14920. #else
  14921. RsaKey caKey[1];
  14922. #ifdef WOLFSSL_TEST_CERT
  14923. DecodedCert decode[1];
  14924. #endif
  14925. #endif
  14926. byte* der = NULL;
  14927. wc_test_ret_t ret;
  14928. Cert* myCert = NULL;
  14929. int certSz;
  14930. size_t bytes3;
  14931. word32 idx3 = 0;
  14932. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  14933. XFILE file3;
  14934. #endif
  14935. #if defined(WOLFSSL_ALT_NAMES) && !defined(NO_ASN_TIME)
  14936. struct tm beforeTime;
  14937. struct tm afterTime;
  14938. #endif
  14939. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  14940. (void)keypub;
  14941. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14942. if (caKey == NULL)
  14943. ERROR_OUT(MEMORY_E, exit_rsa);
  14944. #ifdef WOLFSSL_TEST_CERT
  14945. if (decode == NULL)
  14946. ERROR_OUT(MEMORY_E, exit_rsa);
  14947. #endif
  14948. #endif
  14949. XMEMSET(caKey, 0, sizeof *caKey);
  14950. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14951. if (der == NULL) {
  14952. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  14953. }
  14954. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14955. if (myCert == NULL) {
  14956. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  14957. }
  14958. /* self signed */
  14959. ret = wc_InitCert_ex(myCert, HEAP_HINT, devId);
  14960. if (ret != 0) {
  14961. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  14962. }
  14963. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  14964. XMEMCPY(myCert->serial, mySerial, sizeof(mySerial));
  14965. myCert->serialSz = (int)sizeof(mySerial);
  14966. myCert->isCA = 1;
  14967. #ifndef NO_SHA256
  14968. myCert->sigType = CTC_SHA256wRSA;
  14969. #else
  14970. myCert->sigType = CTC_SHAwRSA;
  14971. #endif
  14972. #ifdef WOLFSSL_CERT_EXT
  14973. /* add Policies */
  14974. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  14975. CTC_MAX_CERTPOL_SZ);
  14976. XSTRNCPY(myCert->certPolicies[1], "1.2.840.113549.1.9.16.6.5",
  14977. CTC_MAX_CERTPOL_SZ);
  14978. myCert->certPoliciesNb = 2;
  14979. /* add SKID from the Public Key */
  14980. ret = wc_SetSubjectKeyIdFromPublicKey(myCert, keypub, NULL);
  14981. if (ret != 0) {
  14982. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  14983. }
  14984. /* add AKID from the Public Key */
  14985. ret = wc_SetAuthKeyIdFromPublicKey(myCert, keypub, NULL);
  14986. if (ret != 0) {
  14987. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  14988. }
  14989. /* add Key Usage */
  14990. ret = wc_SetKeyUsage(myCert,"cRLSign,keyCertSign");
  14991. if (ret != 0) {
  14992. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  14993. }
  14994. #ifdef WOLFSSL_EKU_OID
  14995. {
  14996. const char unique[] = "2.16.840.1.111111.100.1.10.1";
  14997. ret = wc_SetExtKeyUsageOID(myCert, unique, sizeof(unique), 0,
  14998. HEAP_HINT);
  14999. if (ret != 0) {
  15000. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15001. }
  15002. }
  15003. #endif /* WOLFSSL_EKU_OID */
  15004. #endif /* WOLFSSL_CERT_EXT */
  15005. do {
  15006. #if defined(WOLFSSL_ASYNC_CRYPT)
  15007. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15008. #endif
  15009. if (ret >= 0) {
  15010. ret = wc_MakeSelfCert(myCert, der, FOURK_BUF, key, rng);
  15011. }
  15012. } while (ret == WC_PENDING_E);
  15013. if (ret < 0) {
  15014. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15015. }
  15016. certSz = (word32)ret;
  15017. #ifdef WOLFSSL_TEST_CERT
  15018. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  15019. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  15020. if (ret != 0) {
  15021. FreeDecodedCert(decode);
  15022. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15023. }
  15024. FreeDecodedCert(decode);
  15025. #endif
  15026. ret = SaveDerAndPem(der, certSz, certDerFile, certPemFile,
  15027. CERT_TYPE);
  15028. if (ret != 0) {
  15029. goto exit_rsa;
  15030. }
  15031. /* Setup Certificate */
  15032. ret = wc_InitCert_ex(myCert, HEAP_HINT, devId);
  15033. if (ret < 0) {
  15034. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15035. }
  15036. #ifdef WOLFSSL_ALT_NAMES
  15037. /* Get CA Cert for testing */
  15038. #ifdef USE_CERT_BUFFERS_1024
  15039. XMEMCPY(tmp, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  15040. bytes3 = sizeof_ca_cert_der_1024;
  15041. #elif defined(USE_CERT_BUFFERS_2048)
  15042. XMEMCPY(tmp, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  15043. bytes3 = sizeof_ca_cert_der_2048;
  15044. #else
  15045. file3 = XFOPEN(rsaCaCertDerFile, "rb");
  15046. if (!file3) {
  15047. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  15048. }
  15049. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  15050. XFCLOSE(file3);
  15051. if (bytes3 == 0)
  15052. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  15053. #endif /* USE_CERT_BUFFERS */
  15054. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_1024) && \
  15055. !defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  15056. ret = wc_SetAltNames(myCert, rsaCaCertFile);
  15057. if (ret != 0)
  15058. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15059. #endif
  15060. /* get alt names from der */
  15061. ret = wc_SetAltNamesBuffer(myCert, tmp, (int)bytes3);
  15062. if (ret != 0)
  15063. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15064. /* get dates from der */
  15065. ret = wc_SetDatesBuffer(myCert, tmp, (int)bytes3);
  15066. if (ret != 0)
  15067. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15068. #ifndef NO_ASN_TIME
  15069. ret = wc_GetCertDates(myCert, &beforeTime, &afterTime);
  15070. if (ret < 0)
  15071. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15072. #endif
  15073. #endif /* WOLFSSL_ALT_NAMES */
  15074. /* Get CA Key */
  15075. #ifdef USE_CERT_BUFFERS_1024
  15076. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  15077. bytes3 = sizeof_ca_key_der_1024;
  15078. #elif defined(USE_CERT_BUFFERS_2048)
  15079. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  15080. bytes3 = sizeof_ca_key_der_2048;
  15081. #else
  15082. file3 = XFOPEN(rsaCaKeyFile, "rb");
  15083. if (!file3) {
  15084. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  15085. }
  15086. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  15087. XFCLOSE(file3);
  15088. if (bytes3 == 0)
  15089. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  15090. #endif /* USE_CERT_BUFFERS */
  15091. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  15092. if (ret != 0)
  15093. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15094. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  15095. if (ret != 0)
  15096. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15097. #ifndef NO_SHA256
  15098. myCert->sigType = CTC_SHA256wRSA;
  15099. #else
  15100. myCert->sigType = CTC_SHAwRSA;
  15101. #endif
  15102. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  15103. #ifdef WOLFSSL_CERT_EXT
  15104. /* add Policies */
  15105. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  15106. CTC_MAX_CERTPOL_SZ);
  15107. myCert->certPoliciesNb =1;
  15108. /* add SKID from the Public Key */
  15109. ret = wc_SetSubjectKeyIdFromPublicKey(myCert, key, NULL);
  15110. if (ret != 0)
  15111. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15112. /* add AKID from the CA certificate */
  15113. #if defined(USE_CERT_BUFFERS_2048)
  15114. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  15115. sizeof_ca_cert_der_2048);
  15116. #elif defined(USE_CERT_BUFFERS_1024)
  15117. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  15118. sizeof_ca_cert_der_1024);
  15119. #else
  15120. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  15121. #endif
  15122. if (ret != 0)
  15123. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15124. /* add Key Usage */
  15125. ret = wc_SetKeyUsage(myCert,"keyEncipherment,keyAgreement");
  15126. if (ret != 0)
  15127. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15128. #endif /* WOLFSSL_CERT_EXT */
  15129. #if defined(USE_CERT_BUFFERS_2048)
  15130. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  15131. sizeof_ca_cert_der_2048);
  15132. #elif defined(USE_CERT_BUFFERS_1024)
  15133. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  15134. sizeof_ca_cert_der_1024);
  15135. #else
  15136. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  15137. #endif
  15138. if (ret < 0)
  15139. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15140. certSz = wc_MakeCert(myCert, der, FOURK_BUF, key, NULL, rng);
  15141. if (certSz < 0) {
  15142. ERROR_OUT(WC_TEST_RET_ENC_EC(certSz), exit_rsa);
  15143. }
  15144. ret = 0;
  15145. do {
  15146. #if defined(WOLFSSL_ASYNC_CRYPT)
  15147. ret = wc_AsyncWait(ret, &caKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15148. #endif
  15149. if (ret >= 0) {
  15150. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der, FOURK_BUF,
  15151. caKey, NULL, rng);
  15152. }
  15153. } while (ret == WC_PENDING_E);
  15154. if (ret < 0)
  15155. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15156. certSz = (word32)ret;
  15157. #ifdef WOLFSSL_TEST_CERT
  15158. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  15159. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  15160. if (ret != 0) {
  15161. FreeDecodedCert(decode);
  15162. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15163. }
  15164. FreeDecodedCert(decode);
  15165. #endif
  15166. ret = SaveDerAndPem(der, certSz, otherCertDerFile, otherCertPemFile,
  15167. CERT_TYPE);
  15168. if (ret != 0) {
  15169. goto exit_rsa;
  15170. }
  15171. exit_rsa:
  15172. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15173. if (caKey != NULL) {
  15174. wc_FreeRsaKey(caKey);
  15175. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15176. }
  15177. #ifdef WOLFSSL_TEST_CERT
  15178. if (decode != NULL)
  15179. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15180. #endif
  15181. #else
  15182. wc_FreeRsaKey(caKey);
  15183. #endif
  15184. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15185. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15186. return ret;
  15187. }
  15188. #endif
  15189. #if !defined(NO_RSA) && defined(HAVE_ECC) && !defined(NO_ECC_SECP) && \
  15190. defined(WOLFSSL_CERT_GEN)
  15191. /* Make Cert / Sign example for ECC cert and RSA CA */
  15192. static wc_test_ret_t rsa_ecc_certgen_test(WC_RNG* rng, byte* tmp)
  15193. {
  15194. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15195. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15196. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15197. ecc_key *caEccKeyPub = (ecc_key *)XMALLOC(sizeof *caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15198. #ifdef WOLFSSL_TEST_CERT
  15199. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15200. #endif
  15201. #else
  15202. RsaKey caKey[1];
  15203. ecc_key caEccKey[1];
  15204. ecc_key caEccKeyPub[1];
  15205. #ifdef WOLFSSL_TEST_CERT
  15206. DecodedCert decode[1];
  15207. #endif
  15208. #endif
  15209. byte* der = NULL;
  15210. Cert* myCert = NULL;
  15211. int certSz;
  15212. size_t bytes3;
  15213. word32 idx3 = 0;
  15214. #if (!defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)) \
  15215. || !defined(USE_CERT_BUFFERS_256)
  15216. XFILE file3;
  15217. #endif
  15218. wc_test_ret_t ret;
  15219. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15220. if ((caKey == NULL) || (caEccKey == NULL) || (caEccKeyPub == NULL)
  15221. #ifdef WOLFSSL_TEST_CERT
  15222. || (decode == NULL)
  15223. #endif
  15224. )
  15225. ERROR_OUT(MEMORY_E, exit_rsa);
  15226. #endif
  15227. XMEMSET(caKey, 0, sizeof *caKey);
  15228. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  15229. XMEMSET(caEccKeyPub, 0, sizeof *caEccKeyPub);
  15230. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15231. if (der == NULL) {
  15232. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  15233. }
  15234. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15235. if (myCert == NULL) {
  15236. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  15237. }
  15238. /* Get CA Key */
  15239. #ifdef USE_CERT_BUFFERS_1024
  15240. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  15241. bytes3 = sizeof_ca_key_der_1024;
  15242. #elif defined(USE_CERT_BUFFERS_2048)
  15243. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  15244. bytes3 = sizeof_ca_key_der_2048;
  15245. #else
  15246. file3 = XFOPEN(rsaCaKeyFile, "rb");
  15247. if (!file3) {
  15248. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  15249. }
  15250. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  15251. XFCLOSE(file3);
  15252. if (bytes3 == 0)
  15253. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  15254. #endif /* USE_CERT_BUFFERS */
  15255. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  15256. if (ret != 0)
  15257. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15258. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  15259. if (ret != 0)
  15260. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15261. /* Get Cert Key */
  15262. #ifdef USE_CERT_BUFFERS_256
  15263. XMEMCPY(tmp, ecc_key_pub_der_256, sizeof_ecc_key_pub_der_256);
  15264. bytes3 = sizeof_ecc_key_pub_der_256;
  15265. #else
  15266. file3 = XFOPEN(eccKeyPubFileDer, "rb");
  15267. if (!file3) {
  15268. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  15269. }
  15270. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  15271. XFCLOSE(file3);
  15272. if (bytes3 == 0)
  15273. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  15274. #endif
  15275. ret = wc_ecc_init_ex(caEccKeyPub, HEAP_HINT, devId);
  15276. if (ret != 0)
  15277. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15278. idx3 = 0;
  15279. ret = wc_EccPublicKeyDecode(tmp, &idx3, caEccKeyPub, (word32)bytes3);
  15280. if (ret != 0)
  15281. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15282. /* Setup Certificate */
  15283. ret = wc_InitCert_ex(myCert, HEAP_HINT, devId);
  15284. if (ret != 0)
  15285. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15286. #ifndef NO_SHA256
  15287. myCert->sigType = CTC_SHA256wRSA;
  15288. #else
  15289. myCert->sigType = CTC_SHAwRSA;
  15290. #endif
  15291. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  15292. #ifdef WOLFSSL_CERT_EXT
  15293. /* add Policies */
  15294. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  15295. CTC_MAX_CERTPOL_SZ);
  15296. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  15297. CTC_MAX_CERTPOL_SZ);
  15298. myCert->certPoliciesNb = 2;
  15299. /* add SKID from the Public Key */
  15300. ret = wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, caEccKeyPub);
  15301. if (ret != 0)
  15302. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15303. /* add AKID from the CA certificate */
  15304. #if defined(USE_CERT_BUFFERS_2048)
  15305. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  15306. sizeof_ca_cert_der_2048);
  15307. #elif defined(USE_CERT_BUFFERS_1024)
  15308. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  15309. sizeof_ca_cert_der_1024);
  15310. #else
  15311. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  15312. #endif
  15313. if (ret != 0)
  15314. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15315. /* add Key Usage */
  15316. ret = wc_SetKeyUsage(myCert, certKeyUsage);
  15317. if (ret != 0)
  15318. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15319. #endif /* WOLFSSL_CERT_EXT */
  15320. #if defined(USE_CERT_BUFFERS_2048)
  15321. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  15322. sizeof_ca_cert_der_2048);
  15323. #elif defined(USE_CERT_BUFFERS_1024)
  15324. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  15325. sizeof_ca_cert_der_1024);
  15326. #else
  15327. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  15328. #endif
  15329. if (ret < 0)
  15330. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15331. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, caEccKeyPub, rng);
  15332. if (certSz < 0) {
  15333. ERROR_OUT(WC_TEST_RET_ENC_EC(certSz), exit_rsa);
  15334. }
  15335. ret = 0;
  15336. do {
  15337. #if defined(WOLFSSL_ASYNC_CRYPT)
  15338. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15339. #endif
  15340. if (ret >= 0) {
  15341. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  15342. FOURK_BUF, caKey, NULL, rng);
  15343. }
  15344. } while (ret == WC_PENDING_E);
  15345. if (ret < 0)
  15346. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15347. certSz = (word32)ret;
  15348. #ifdef WOLFSSL_TEST_CERT
  15349. InitDecodedCert(decode, der, certSz, 0);
  15350. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  15351. if (ret != 0) {
  15352. FreeDecodedCert(decode);
  15353. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15354. }
  15355. FreeDecodedCert(decode);
  15356. #endif
  15357. ret = SaveDerAndPem(der, certSz, certEccRsaDerFile, certEccRsaPemFile,
  15358. CERT_TYPE);
  15359. if (ret != 0) {
  15360. goto exit_rsa;
  15361. }
  15362. exit_rsa:
  15363. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15364. if (caKey != NULL) {
  15365. wc_FreeRsaKey(caKey);
  15366. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15367. }
  15368. if (caEccKey != NULL) {
  15369. wc_ecc_free(caEccKey);
  15370. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15371. }
  15372. if (caEccKeyPub != NULL) {
  15373. wc_ecc_free(caEccKeyPub);
  15374. XFREE(caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15375. }
  15376. #ifdef WOLFSSL_TEST_CERT
  15377. if (decode != NULL)
  15378. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15379. #endif
  15380. #else
  15381. wc_FreeRsaKey(caKey);
  15382. wc_ecc_free(caEccKey);
  15383. wc_ecc_free(caEccKeyPub);
  15384. #endif
  15385. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15386. myCert = NULL;
  15387. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15388. der = NULL;
  15389. if (ret >= 0)
  15390. ret = 0;
  15391. return ret;
  15392. }
  15393. #endif /* !NO_RSA && HAVE_ECC && WOLFSSL_CERT_GEN */
  15394. #ifdef WOLFSSL_KEY_GEN
  15395. static wc_test_ret_t rsa_keygen_test(WC_RNG* rng)
  15396. {
  15397. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15398. RsaKey *genKey = (RsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15399. #else
  15400. RsaKey genKey[1];
  15401. #endif
  15402. wc_test_ret_t ret;
  15403. byte* der = NULL;
  15404. #ifndef WOLFSSL_CRYPTOCELL
  15405. word32 idx = 0;
  15406. #endif
  15407. int derSz = 0;
  15408. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FIPS)
  15409. int keySz = 1024;
  15410. #else
  15411. int keySz = 2048;
  15412. #endif
  15413. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15414. if (! genKey)
  15415. ERROR_OUT(MEMORY_E, exit_rsa);
  15416. #endif
  15417. XMEMSET(genKey, 0, sizeof *genKey);
  15418. ret = wc_InitRsaKey_ex(genKey, HEAP_HINT, devId);
  15419. if (ret != 0)
  15420. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15421. #ifdef HAVE_FIPS
  15422. for (;;) {
  15423. #endif
  15424. ret = wc_MakeRsaKey(genKey, keySz, WC_RSA_EXPONENT, rng);
  15425. #if defined(WOLFSSL_ASYNC_CRYPT)
  15426. ret = wc_AsyncWait(ret, &genKey->asyncDev, WC_ASYNC_FLAG_NONE);
  15427. #endif
  15428. #ifdef HAVE_FIPS
  15429. if (ret == PRIME_GEN_E)
  15430. continue;
  15431. break;
  15432. }
  15433. #endif
  15434. if (ret != 0)
  15435. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15436. TEST_SLEEP();
  15437. #ifdef WOLFSSL_RSA_KEY_CHECK
  15438. ret = wc_CheckRsaKey(genKey);
  15439. if (ret != 0)
  15440. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15441. #endif
  15442. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15443. if (der == NULL) {
  15444. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  15445. }
  15446. derSz = wc_RsaKeyToDer(genKey, der, FOURK_BUF);
  15447. if (derSz < 0) {
  15448. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  15449. }
  15450. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  15451. PRIVATEKEY_TYPE);
  15452. if (ret != 0) {
  15453. goto exit_rsa;
  15454. }
  15455. wc_FreeRsaKey(genKey);
  15456. ret = wc_InitRsaKey(genKey, HEAP_HINT);
  15457. if (ret != 0)
  15458. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15459. #ifndef WOLFSSL_CRYPTOCELL
  15460. idx = 0;
  15461. /* The private key part of the key gen pairs from cryptocell can't be exported */
  15462. ret = wc_RsaPrivateKeyDecode(der, &idx, genKey, derSz);
  15463. if (ret != 0)
  15464. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15465. #endif /* WOLFSSL_CRYPTOCELL */
  15466. exit_rsa:
  15467. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15468. if (genKey) {
  15469. wc_FreeRsaKey(genKey);
  15470. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15471. }
  15472. #else
  15473. wc_FreeRsaKey(genKey);
  15474. #endif
  15475. if (der != NULL) {
  15476. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15477. der = NULL;
  15478. }
  15479. return ret;
  15480. }
  15481. #endif
  15482. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  15483. #if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG) && \
  15484. !defined(HAVE_FAST_RSA) && !defined(HAVE_USER_RSA) && \
  15485. (!defined(HAVE_FIPS) || \
  15486. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) \
  15487. && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  15488. static wc_test_ret_t rsa_oaep_padding_test(RsaKey* key, WC_RNG* rng)
  15489. {
  15490. wc_test_ret_t ret = 0;
  15491. word32 idx = 0;
  15492. const char inStr[] = TEST_STRING;
  15493. const word32 inLen = (word32)TEST_STRING_SZ;
  15494. const word32 outSz = RSA_TEST_BYTES;
  15495. const word32 plainSz = RSA_TEST_BYTES;
  15496. byte* res = NULL;
  15497. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  15498. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  15499. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  15500. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  15501. if (in == NULL || out == NULL || plain == NULL)
  15502. ERROR_OUT(MEMORY_E, exit_rsa);
  15503. #endif
  15504. XMEMCPY(in, inStr, inLen);
  15505. #ifndef NO_SHA
  15506. do {
  15507. #if defined(WOLFSSL_ASYNC_CRYPT)
  15508. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15509. #endif
  15510. if (ret >= 0) {
  15511. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  15512. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  15513. }
  15514. } while (ret == WC_PENDING_E);
  15515. if (ret < 0)
  15516. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15517. TEST_SLEEP();
  15518. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  15519. idx = (word32)ret;
  15520. do {
  15521. #if defined(WOLFSSL_ASYNC_CRYPT)
  15522. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15523. #endif
  15524. if (ret >= 0) {
  15525. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  15526. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  15527. }
  15528. } while (ret == WC_PENDING_E);
  15529. if (ret < 0)
  15530. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15531. if (XMEMCMP(plain, in, inLen)) {
  15532. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  15533. }
  15534. TEST_SLEEP();
  15535. #endif /* NO_SHA */
  15536. #endif
  15537. #ifndef NO_SHA256
  15538. XMEMSET(plain, 0, plainSz);
  15539. do {
  15540. #if defined(WOLFSSL_ASYNC_CRYPT)
  15541. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15542. #endif
  15543. if (ret >= 0) {
  15544. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  15545. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  15546. }
  15547. } while (ret == WC_PENDING_E);
  15548. if (ret < 0)
  15549. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15550. TEST_SLEEP();
  15551. idx = (word32)ret;
  15552. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  15553. do {
  15554. #if defined(WOLFSSL_ASYNC_CRYPT)
  15555. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15556. #endif
  15557. if (ret >= 0) {
  15558. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  15559. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  15560. }
  15561. } while (ret == WC_PENDING_E);
  15562. if (ret < 0)
  15563. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15564. if (XMEMCMP(plain, in, inLen)) {
  15565. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  15566. }
  15567. TEST_SLEEP();
  15568. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  15569. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  15570. do {
  15571. #if defined(WOLFSSL_ASYNC_CRYPT)
  15572. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15573. #endif
  15574. if (ret >= 0) {
  15575. ret = wc_RsaPrivateDecryptInline_ex(out, idx, &res, key,
  15576. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  15577. }
  15578. } while (ret == WC_PENDING_E);
  15579. if (ret < 0)
  15580. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15581. if (ret != (int)inLen) {
  15582. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  15583. }
  15584. if (XMEMCMP(res, in, inLen)) {
  15585. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  15586. }
  15587. TEST_SLEEP();
  15588. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  15589. /* check fails if not using the same optional label */
  15590. XMEMSET(plain, 0, plainSz);
  15591. do {
  15592. #if defined(WOLFSSL_ASYNC_CRYPT)
  15593. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15594. #endif
  15595. if (ret >= 0) {
  15596. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  15597. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  15598. }
  15599. } while (ret == WC_PENDING_E);
  15600. if (ret < 0)
  15601. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15602. TEST_SLEEP();
  15603. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  15604. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  15605. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SE050)
  15606. /* label is unused in cryptocell and SE050 so it won't detect decrypt error
  15607. * due to label */
  15608. idx = (word32)ret;
  15609. do {
  15610. #if defined(WOLFSSL_ASYNC_CRYPT)
  15611. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15612. #endif
  15613. if (ret >= 0) {
  15614. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  15615. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  15616. }
  15617. } while (ret == WC_PENDING_E);
  15618. if (ret > 0) { /* in this case decrypt should fail */
  15619. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  15620. }
  15621. ret = 0;
  15622. TEST_SLEEP();
  15623. #endif /* !HAVE_CAVIUM */
  15624. /* check using optional label with encrypt/decrypt */
  15625. XMEMSET(plain, 0, plainSz);
  15626. do {
  15627. #if defined(WOLFSSL_ASYNC_CRYPT)
  15628. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15629. #endif
  15630. if (ret >= 0) {
  15631. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  15632. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  15633. }
  15634. } while (ret == WC_PENDING_E);
  15635. if (ret < 0)
  15636. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15637. TEST_SLEEP();
  15638. idx = (word32)ret;
  15639. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  15640. do {
  15641. #if defined(WOLFSSL_ASYNC_CRYPT)
  15642. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15643. #endif
  15644. if (ret >= 0) {
  15645. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  15646. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  15647. }
  15648. } while (ret == WC_PENDING_E);
  15649. if (ret < 0)
  15650. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15651. if (XMEMCMP(plain, in, inLen)) {
  15652. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  15653. }
  15654. TEST_SLEEP();
  15655. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  15656. #ifndef NO_SHA
  15657. /* check fail using mismatch hash algorithms */
  15658. XMEMSET(plain, 0, plainSz);
  15659. do {
  15660. #if defined(WOLFSSL_ASYNC_CRYPT)
  15661. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15662. #endif
  15663. if (ret >= 0) {
  15664. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  15665. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, in, inLen);
  15666. }
  15667. } while (ret == WC_PENDING_E);
  15668. if (ret < 0)
  15669. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15670. TEST_SLEEP();
  15671. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  15672. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  15673. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SE050)
  15674. idx = (word32)ret;
  15675. do {
  15676. #if defined(WOLFSSL_ASYNC_CRYPT)
  15677. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15678. #endif
  15679. if (ret >= 0) {
  15680. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  15681. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256,
  15682. in, inLen);
  15683. }
  15684. } while (ret == WC_PENDING_E);
  15685. if (ret > 0) { /* should fail */
  15686. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  15687. }
  15688. ret = 0;
  15689. TEST_SLEEP();
  15690. #endif /* !HAVE_CAVIUM */
  15691. #endif /* NO_SHA */
  15692. #endif /* NO_SHA256 */
  15693. #ifdef WOLFSSL_SHA512
  15694. /* Check valid RSA key size is used while using hash length of SHA512
  15695. If key size is less than (hash length * 2) + 2 then is invalid use
  15696. and test, since OAEP padding requires this.
  15697. BAD_FUNC_ARG is returned when this case is not met */
  15698. if (wc_RsaEncryptSize(key) > ((int)WC_SHA512_DIGEST_SIZE * 2) + 2) {
  15699. XMEMSET(plain, 0, plainSz);
  15700. do {
  15701. #if defined(WOLFSSL_ASYNC_CRYPT)
  15702. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15703. #endif
  15704. if (ret >= 0) {
  15705. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  15706. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  15707. }
  15708. } while (ret == WC_PENDING_E);
  15709. if (ret < 0)
  15710. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15711. TEST_SLEEP();
  15712. idx = (word32)ret;
  15713. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  15714. do {
  15715. #if defined(WOLFSSL_ASYNC_CRYPT)
  15716. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15717. #endif
  15718. if (ret >= 0) {
  15719. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  15720. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  15721. }
  15722. } while (ret == WC_PENDING_E);
  15723. if (ret < 0)
  15724. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15725. if (XMEMCMP(plain, in, inLen)) {
  15726. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  15727. }
  15728. TEST_SLEEP();
  15729. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  15730. }
  15731. #endif /* WOLFSSL_SHA512 */
  15732. /* check using pkcsv15 padding with _ex API */
  15733. XMEMSET(plain, 0, plainSz);
  15734. do {
  15735. #if defined(WOLFSSL_ASYNC_CRYPT)
  15736. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15737. #endif
  15738. if (ret >= 0) {
  15739. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  15740. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  15741. }
  15742. } while (ret == WC_PENDING_E);
  15743. if (ret < 0)
  15744. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15745. TEST_SLEEP();
  15746. idx = (word32)ret;
  15747. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  15748. do {
  15749. #if defined(WOLFSSL_ASYNC_CRYPT)
  15750. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15751. #endif
  15752. if (ret >= 0) {
  15753. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  15754. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  15755. }
  15756. } while (ret == WC_PENDING_E);
  15757. if (ret < 0)
  15758. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15759. if (XMEMCMP(plain, in, inLen)) {
  15760. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  15761. }
  15762. TEST_SLEEP();
  15763. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  15764. exit_rsa:
  15765. WC_FREE_VAR(in, HEAP_HINT);
  15766. WC_FREE_VAR(out, HEAP_HINT);
  15767. WC_FREE_VAR(plain, HEAP_HINT);
  15768. (void)idx;
  15769. (void)inStr;
  15770. (void)res;
  15771. if (ret >= 0)
  15772. ret = 0;
  15773. return ret;
  15774. }
  15775. #endif
  15776. #endif
  15777. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t rsa_test(void)
  15778. {
  15779. wc_test_ret_t ret;
  15780. size_t bytes;
  15781. WC_RNG rng;
  15782. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15783. byte* tmp = NULL;
  15784. byte* der = NULL;
  15785. RsaKey *key = (RsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15786. #else
  15787. RsaKey key[1];
  15788. byte tmp[FOURK_BUF];
  15789. #endif
  15790. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  15791. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15792. RsaKey *keypub = (RsaKey *)XMALLOC(sizeof *keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15793. #else
  15794. RsaKey keypub[1];
  15795. #endif
  15796. #endif
  15797. word32 idx = 0;
  15798. const char inStr[] = TEST_STRING;
  15799. const word32 inLen = (word32)TEST_STRING_SZ;
  15800. const word32 outSz = RSA_TEST_BYTES;
  15801. const word32 plainSz = RSA_TEST_BYTES;
  15802. byte* res = NULL;
  15803. #ifndef NO_SIG_WRAPPER
  15804. int modLen;
  15805. #endif
  15806. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  15807. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  15808. !defined(NO_FILESYSTEM)
  15809. XFILE file;
  15810. #ifdef WOLFSSL_TEST_CERT
  15811. XFILE file2;
  15812. #endif
  15813. #endif
  15814. #ifdef WOLFSSL_TEST_CERT
  15815. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15816. DecodedCert *cert = (DecodedCert *)XMALLOC(sizeof *cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15817. #else
  15818. DecodedCert cert[1];
  15819. #endif
  15820. #ifndef NO_ASN_TIME
  15821. struct tm timearg;
  15822. const byte* date;
  15823. byte dateFormat;
  15824. int dateLength;
  15825. #endif
  15826. #endif
  15827. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  15828. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  15829. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  15830. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  15831. if (in == NULL || out == NULL || plain == NULL)
  15832. ERROR_OUT(MEMORY_E, exit_rsa);
  15833. #endif
  15834. XMEMCPY(in, inStr, inLen);
  15835. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15836. if (key == NULL)
  15837. ERROR_OUT(MEMORY_E, exit_rsa);
  15838. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  15839. if (keypub == NULL)
  15840. ERROR_OUT(MEMORY_E, exit_rsa);
  15841. #endif
  15842. #ifdef WOLFSSL_TEST_CERT
  15843. if (cert == NULL)
  15844. ERROR_OUT(MEMORY_E, exit_rsa);
  15845. #endif
  15846. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  15847. /* initialize stack structures */
  15848. XMEMSET(&rng, 0, sizeof(rng));
  15849. XMEMSET(key, 0, sizeof *key);
  15850. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  15851. XMEMSET(keypub, 0, sizeof *keypub);
  15852. #endif
  15853. #if !defined(HAVE_USER_RSA) && !defined(NO_ASN)
  15854. ret = rsa_decode_test(key);
  15855. if (ret != 0)
  15856. ERROR_OUT(ret, exit_rsa);
  15857. #endif
  15858. #ifdef USE_CERT_BUFFERS_1024
  15859. bytes = (size_t)sizeof_client_key_der_1024;
  15860. if (bytes < (size_t)sizeof_client_cert_der_1024)
  15861. bytes = (size_t)sizeof_client_cert_der_1024;
  15862. #elif defined(USE_CERT_BUFFERS_2048)
  15863. bytes = (size_t)sizeof_client_key_der_2048;
  15864. if (bytes < (size_t)sizeof_client_cert_der_2048)
  15865. bytes = (size_t)sizeof_client_cert_der_2048;
  15866. #elif defined(USE_CERT_BUFFERS_3072)
  15867. bytes = (size_t)sizeof_client_key_der_3072;
  15868. if (bytes < (size_t)sizeof_client_cert_der_3072)
  15869. bytes = (size_t)sizeof_client_cert_der_3072;
  15870. #elif defined(USE_CERT_BUFFERS_4096)
  15871. bytes = (size_t)sizeof_client_key_der_4096;
  15872. if (bytes < (size_t)sizeof_client_cert_der_4096)
  15873. bytes = (size_t)sizeof_client_cert_der_4096;
  15874. #else
  15875. bytes = FOURK_BUF;
  15876. #endif
  15877. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15878. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15879. if (tmp == NULL)
  15880. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  15881. #endif
  15882. #ifdef USE_CERT_BUFFERS_1024
  15883. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  15884. #elif defined(USE_CERT_BUFFERS_2048)
  15885. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  15886. #elif defined(USE_CERT_BUFFERS_3072)
  15887. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  15888. #elif defined(USE_CERT_BUFFERS_4096)
  15889. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  15890. #elif !defined(NO_FILESYSTEM)
  15891. file = XFOPEN(clientKey, "rb");
  15892. if (!file) {
  15893. err_sys("can't open ./certs/client-key.der, "
  15894. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  15895. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  15896. }
  15897. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  15898. XFCLOSE(file);
  15899. if (bytes == 0)
  15900. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  15901. #else
  15902. /* No key to use. */
  15903. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  15904. #endif /* USE_CERT_BUFFERS */
  15905. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  15906. if (ret != 0)
  15907. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15908. #ifndef NO_ASN
  15909. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  15910. if (ret != 0)
  15911. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15912. #ifndef NO_SIG_WRAPPER
  15913. modLen = wc_RsaEncryptSize(key);
  15914. #endif
  15915. #else
  15916. #ifdef USE_CERT_BUFFERS_2048
  15917. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  15918. if (ret != 0)
  15919. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15920. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  15921. if (ret != 0)
  15922. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15923. #ifndef NO_SIG_WRAPPER
  15924. modLen = 2048;
  15925. #endif
  15926. #else
  15927. #error Not supported yet!
  15928. #endif
  15929. #endif
  15930. #ifndef WC_NO_RNG
  15931. #ifndef HAVE_FIPS
  15932. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  15933. #else
  15934. ret = wc_InitRng(&rng);
  15935. #endif
  15936. if (ret != 0)
  15937. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15938. #endif
  15939. #ifndef NO_SIG_WRAPPER
  15940. ret = rsa_sig_test(key, sizeof *key, modLen, &rng);
  15941. if (ret != 0)
  15942. goto exit_rsa;
  15943. #endif
  15944. #ifdef WC_RSA_NONBLOCK
  15945. ret = rsa_nb_test(key, in, inLen, out, outSz, plain, plainSz, &rng);
  15946. if (ret != 0)
  15947. goto exit_rsa;
  15948. #endif
  15949. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  15950. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  15951. do {
  15952. #if defined(WOLFSSL_ASYNC_CRYPT)
  15953. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15954. #endif
  15955. if (ret >= 0) {
  15956. ret = wc_RsaPublicEncrypt(in, inLen, out, outSz, key, &rng);
  15957. }
  15958. } while (ret == WC_PENDING_E);
  15959. if (ret < 0)
  15960. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15961. TEST_SLEEP();
  15962. #ifdef WC_RSA_BLINDING
  15963. {
  15964. wc_test_ret_t tmpret = ret;
  15965. ret = wc_RsaSetRNG(key, &rng);
  15966. if (ret < 0)
  15967. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15968. ret = tmpret;
  15969. }
  15970. #endif
  15971. idx = (word32)ret; /* save off encrypted length */
  15972. do {
  15973. #if defined(WOLFSSL_ASYNC_CRYPT)
  15974. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15975. #endif
  15976. if (ret >= 0) {
  15977. ret = wc_RsaPrivateDecrypt(out, idx, plain, plainSz, key);
  15978. }
  15979. } while (ret == WC_PENDING_E);
  15980. if (ret < 0)
  15981. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15982. if (XMEMCMP(plain, in, inLen)) {
  15983. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  15984. }
  15985. TEST_SLEEP();
  15986. do {
  15987. #if defined(WOLFSSL_ASYNC_CRYPT)
  15988. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  15989. #endif
  15990. if (ret >= 0) {
  15991. ret = wc_RsaPrivateDecryptInline(out, idx, &res, key);
  15992. }
  15993. } while (ret == WC_PENDING_E);
  15994. if (ret < 0)
  15995. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  15996. if (ret != (int)inLen) {
  15997. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  15998. }
  15999. if (XMEMCMP(res, in, inLen)) {
  16000. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  16001. }
  16002. TEST_SLEEP();
  16003. do {
  16004. #if defined(WOLFSSL_ASYNC_CRYPT)
  16005. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16006. #endif
  16007. if (ret >= 0) {
  16008. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, &rng);
  16009. }
  16010. } while (ret == WC_PENDING_E);
  16011. if (ret < 0)
  16012. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16013. TEST_SLEEP();
  16014. #elif defined(WOLFSSL_PUBLIC_MP)
  16015. {
  16016. static byte signature_2048[] = {
  16017. 0x07, 0x6f, 0xc9, 0x85, 0x73, 0x9e, 0x21, 0x79,
  16018. 0x47, 0xf1, 0xa3, 0xd7, 0xf4, 0x27, 0x29, 0xbe,
  16019. 0x99, 0x5d, 0xac, 0xb2, 0x10, 0x3f, 0x95, 0xda,
  16020. 0x89, 0x23, 0xb8, 0x96, 0x13, 0x57, 0x72, 0x30,
  16021. 0xa1, 0xfe, 0x5a, 0x68, 0x9c, 0x99, 0x9d, 0x1e,
  16022. 0x05, 0xa4, 0x80, 0xb0, 0xbb, 0xd9, 0xd9, 0xa1,
  16023. 0x69, 0x97, 0x74, 0xb3, 0x41, 0x21, 0x3b, 0x47,
  16024. 0xf5, 0x51, 0xb1, 0xfb, 0xc7, 0xaa, 0xcc, 0xdc,
  16025. 0xcd, 0x76, 0xa0, 0x28, 0x4d, 0x27, 0x14, 0xa4,
  16026. 0xb9, 0x41, 0x68, 0x7c, 0xb3, 0x66, 0xe6, 0x6f,
  16027. 0x40, 0x76, 0xe4, 0x12, 0xfd, 0xae, 0x29, 0xb5,
  16028. 0x63, 0x60, 0x87, 0xce, 0x49, 0x6b, 0xf3, 0x05,
  16029. 0x9a, 0x14, 0xb5, 0xcc, 0xcd, 0xf7, 0x30, 0x95,
  16030. 0xd2, 0x72, 0x52, 0x1d, 0x5b, 0x7e, 0xef, 0x4a,
  16031. 0x02, 0x96, 0x21, 0x6c, 0x55, 0xa5, 0x15, 0xb1,
  16032. 0x57, 0x63, 0x2c, 0xa3, 0x8e, 0x9d, 0x3d, 0x45,
  16033. 0xcc, 0xb8, 0xe6, 0xa1, 0xc8, 0x59, 0xcd, 0xf5,
  16034. 0xdc, 0x0a, 0x51, 0xb6, 0x9d, 0xfb, 0xf4, 0x6b,
  16035. 0xfd, 0x32, 0x71, 0x6e, 0xcf, 0xcb, 0xb3, 0xd9,
  16036. 0xe0, 0x4a, 0x77, 0x34, 0xd6, 0x61, 0xf5, 0x7c,
  16037. 0xf9, 0xa9, 0xa4, 0xb0, 0x8e, 0x3b, 0xd6, 0x04,
  16038. 0xe0, 0xde, 0x2b, 0x5b, 0x5a, 0xbf, 0xd9, 0xef,
  16039. 0x8d, 0xa3, 0xf5, 0xb1, 0x67, 0xf3, 0xb9, 0x72,
  16040. 0x0a, 0x37, 0x12, 0x35, 0x6c, 0x8e, 0x10, 0x8b,
  16041. 0x38, 0x06, 0x16, 0x4b, 0x20, 0x20, 0x13, 0x00,
  16042. 0x2e, 0x6d, 0xc2, 0x59, 0x23, 0x67, 0x4a, 0x6d,
  16043. 0xa1, 0x46, 0x8b, 0xee, 0xcf, 0x44, 0xb4, 0x3e,
  16044. 0x56, 0x75, 0x00, 0x68, 0xb5, 0x7d, 0x0f, 0x20,
  16045. 0x79, 0x5d, 0x7f, 0x12, 0x15, 0x32, 0x89, 0x61,
  16046. 0x6b, 0x29, 0xb7, 0x52, 0xf5, 0x25, 0xd8, 0x98,
  16047. 0xe8, 0x6f, 0xf9, 0x22, 0xb4, 0xbb, 0xe5, 0xff,
  16048. 0xd0, 0x92, 0x86, 0x9a, 0x88, 0xa2, 0xaf, 0x6b
  16049. };
  16050. ret = sizeof(signature_2048);
  16051. XMEMCPY(out, signature_2048, ret);
  16052. }
  16053. #endif
  16054. #if !defined(WC_NO_RNG) && !defined(WC_NO_RSA_OAEP) && \
  16055. ((!defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || \
  16056. defined(WOLFSSL_PUBLIC_MP)) && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  16057. idx = (word32)ret;
  16058. XMEMSET(plain, 0, plainSz);
  16059. do {
  16060. #if defined(WOLFSSL_ASYNC_CRYPT)
  16061. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16062. #endif
  16063. if (ret >= 0) {
  16064. #ifndef WOLFSSL_RSA_VERIFY_INLINE
  16065. #if defined(WOLFSSL_CRYPTOCELL)
  16066. /*
  16067. Cryptocell requires the input data and signature byte array to verify.
  16068. first argument must be the input data
  16069. second argument must be the length of input data
  16070. third argument must be the signature byte array or the output from
  16071. wc_RsaSSL_Sign()
  16072. fourth argument must be the length of the signature byte array
  16073. */
  16074. ret = wc_RsaSSL_Verify(in, inLen, out, outSz, key);
  16075. #else
  16076. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, key);
  16077. #endif /* WOLFSSL_CRYPTOCELL */
  16078. #else
  16079. byte* dec = NULL;
  16080. ret = wc_RsaSSL_VerifyInline(out, idx, &dec, key);
  16081. if (ret > 0) {
  16082. XMEMCPY(plain, dec, ret);
  16083. }
  16084. #endif
  16085. }
  16086. } while (ret == WC_PENDING_E);
  16087. if (ret < 0)
  16088. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16089. if (XMEMCMP(plain, in, (size_t)ret)) {
  16090. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  16091. }
  16092. TEST_SLEEP();
  16093. #endif
  16094. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  16095. #if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG)
  16096. #if !defined(HAVE_FAST_RSA) && !defined(HAVE_USER_RSA) && \
  16097. (!defined(HAVE_FIPS) || \
  16098. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) \
  16099. && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  16100. ret = rsa_oaep_padding_test(key, &rng);
  16101. if (ret != 0)
  16102. return ret;
  16103. #endif /* !HAVE_FAST_RSA && !HAVE_FIPS */
  16104. #endif /* WC_NO_RSA_OAEP && !WC_NO_RNG */
  16105. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  16106. #if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(NO_ASN) \
  16107. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  16108. ret = rsa_export_key_test(key);
  16109. if (ret != 0)
  16110. return ret;
  16111. #endif
  16112. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  16113. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  16114. ret = rsa_flatten_test(key);
  16115. if (ret != 0)
  16116. return ret;
  16117. #endif
  16118. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_ASN) && \
  16119. !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  16120. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  16121. (void)clientCert;
  16122. #endif
  16123. #ifdef WOLFSSL_TEST_CERT
  16124. #if defined(WOLFSSL_MDK_ARM)
  16125. #define sizeof(s) XSTRLEN((char *)(s))
  16126. #endif
  16127. #ifdef USE_CERT_BUFFERS_1024
  16128. XMEMCPY(tmp, client_cert_der_1024, (size_t)sizeof_client_cert_der_1024);
  16129. bytes = (size_t)sizeof_client_cert_der_1024;
  16130. #elif defined(USE_CERT_BUFFERS_2048)
  16131. XMEMCPY(tmp, client_cert_der_2048, (size_t)sizeof_client_cert_der_2048);
  16132. bytes = (size_t)sizeof_client_cert_der_2048;
  16133. #elif defined(USE_CERT_BUFFERS_3072)
  16134. XMEMCPY(tmp, client_cert_der_3072, (size_t)sizeof_client_cert_der_3072);
  16135. bytes = (size_t)sizeof_client_cert_der_3072;
  16136. #elif defined(USE_CERT_BUFFERS_4096)
  16137. XMEMCPY(tmp, client_cert_der_4096, (size_t)sizeof_client_cert_der_4096);
  16138. bytes = (size_t)sizeof_client_cert_der_4096;
  16139. #elif !defined(NO_FILESYSTEM)
  16140. file2 = XFOPEN(clientCert, "rb");
  16141. if (!file2) {
  16142. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16143. }
  16144. bytes = XFREAD(tmp, 1, FOURK_BUF, file2);
  16145. XFCLOSE(file2);
  16146. if (bytes == 0)
  16147. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16148. #else
  16149. /* No certificate to use. */
  16150. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_rsa);
  16151. #endif
  16152. #ifdef sizeof
  16153. #undef sizeof
  16154. #endif
  16155. InitDecodedCert(cert, tmp, (word32)bytes, NULL);
  16156. ret = ParseCert(cert, CERT_TYPE, NO_VERIFY, NULL);
  16157. if (ret != 0) {
  16158. FreeDecodedCert(cert);
  16159. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16160. }
  16161. #ifndef NO_ASN_TIME
  16162. ret = wc_GetDateInfo(cert->afterDate, cert->afterDateLen, &date,
  16163. &dateFormat, &dateLength);
  16164. if (ret != 0) {
  16165. FreeDecodedCert(cert);
  16166. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16167. }
  16168. ret = wc_GetDateAsCalendarTime(date, dateLength, dateFormat, &timearg);
  16169. if (ret != 0) {
  16170. FreeDecodedCert(cert);
  16171. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16172. }
  16173. #endif
  16174. FreeDecodedCert(cert);
  16175. #endif /* WOLFSSL_TEST_CERT */
  16176. #ifdef WOLFSSL_CERT_EXT
  16177. #ifdef USE_CERT_BUFFERS_1024
  16178. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  16179. bytes = sizeof_client_keypub_der_1024;
  16180. #elif defined(USE_CERT_BUFFERS_2048)
  16181. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  16182. bytes = sizeof_client_keypub_der_2048;
  16183. #elif defined(USE_CERT_BUFFERS_3072)
  16184. XMEMCPY(tmp, client_keypub_der_3072, sizeof_client_keypub_der_3072);
  16185. bytes = sizeof_client_keypub_der_3072;
  16186. #elif defined(USE_CERT_BUFFERS_4096)
  16187. XMEMCPY(tmp, client_keypub_der_4096, sizeof_client_keypub_der_4096);
  16188. bytes = sizeof_client_keypub_der_4096;
  16189. #else
  16190. file = XFOPEN(clientKeyPub, "rb");
  16191. if (!file) {
  16192. err_sys("can't open ./certs/client-keyPub.der, "
  16193. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  16194. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16195. }
  16196. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  16197. XFCLOSE(file);
  16198. if (bytes == 0)
  16199. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16200. #endif /* USE_CERT_BUFFERS */
  16201. ret = wc_InitRsaKey(keypub, HEAP_HINT);
  16202. if (ret != 0)
  16203. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16204. idx = 0;
  16205. ret = wc_RsaPublicKeyDecode(tmp, &idx, keypub, (word32)bytes);
  16206. if (ret != 0)
  16207. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16208. #endif /* WOLFSSL_CERT_EXT */
  16209. #ifdef WOLFSSL_KEY_GEN
  16210. ret = rsa_keygen_test(&rng);
  16211. if (ret != 0)
  16212. goto exit_rsa;
  16213. #endif
  16214. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  16215. /* Make Cert / Sign example for RSA cert and RSA CA */
  16216. ret = rsa_certgen_test(key, keypub, &rng, tmp);
  16217. if (ret != 0)
  16218. goto exit_rsa;
  16219. #if !defined(NO_RSA) && defined(HAVE_ECC) && !defined(NO_ECC_SECP)
  16220. ret = rsa_ecc_certgen_test(&rng, tmp);
  16221. if (ret != 0)
  16222. goto exit_rsa;
  16223. #endif
  16224. #if defined(WOLFSSL_CERT_REQ) && !defined(WOLFSSL_NO_MALLOC)
  16225. {
  16226. Cert *req;
  16227. int derSz;
  16228. #ifndef WOLFSSL_SMALL_STACK
  16229. byte* der = NULL;
  16230. #endif
  16231. req = (Cert *)XMALLOC(sizeof *req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16232. if (! req)
  16233. ERROR_OUT(MEMORY_E, exit_rsa);
  16234. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,DYNAMIC_TYPE_TMP_BUFFER);
  16235. if (der == NULL) {
  16236. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_rsa);
  16237. }
  16238. ret = wc_InitCert_ex(req, HEAP_HINT, devId);
  16239. if (ret != 0)
  16240. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16241. req->version = 0;
  16242. req->isCA = 1;
  16243. XSTRNCPY(req->challengePw, "wolf123", CTC_NAME_SIZE);
  16244. XMEMCPY(&req->subject, &certDefaultName, sizeof(CertName));
  16245. #ifndef NO_SHA256
  16246. req->sigType = CTC_SHA256wRSA;
  16247. #else
  16248. req->sigType = CTC_SHAwRSA;
  16249. #endif
  16250. #ifdef WOLFSSL_CERT_EXT
  16251. /* add SKID from the Public Key */
  16252. ret = wc_SetSubjectKeyIdFromPublicKey(req, keypub, NULL);
  16253. if (ret != 0)
  16254. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16255. /* add Key Usage */
  16256. ret = wc_SetKeyUsage(req, certKeyUsage2);
  16257. if (ret != 0)
  16258. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16259. /* add Extended Key Usage */
  16260. ret = wc_SetExtKeyUsage(req,
  16261. "serverAuth,clientAuth,codeSigning,"
  16262. "emailProtection,timeStamping,OCSPSigning");
  16263. if (ret != 0)
  16264. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16265. #ifdef WOLFSSL_EKU_OID
  16266. {
  16267. WOLFSSL_SMALL_STACK_STATIC const char unique[] =
  16268. "2.16.840.1.111111.100.1.10.1";
  16269. ret = wc_SetExtKeyUsageOID(req, unique, sizeof(unique), 0,
  16270. HEAP_HINT);
  16271. if (ret != 0)
  16272. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16273. }
  16274. #endif /* WOLFSSL_EKU_OID */
  16275. #endif /* WOLFSSL_CERT_EXT */
  16276. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  16277. if (derSz < 0) {
  16278. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  16279. }
  16280. #ifdef WOLFSSL_CERT_EXT
  16281. /* Try again with "any" flag set, will override all others */
  16282. ret = wc_SetExtKeyUsage(req, "any");
  16283. if (ret != 0)
  16284. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16285. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  16286. if (derSz < 0) {
  16287. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  16288. }
  16289. #endif /* WOLFSSL_CERT_EXT */
  16290. ret = 0;
  16291. do {
  16292. #if defined(WOLFSSL_ASYNC_CRYPT)
  16293. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  16294. #endif
  16295. if (ret >= 0) {
  16296. ret = wc_SignCert(req->bodySz, req->sigType, der, FOURK_BUF,
  16297. key, NULL, &rng);
  16298. }
  16299. } while (ret == WC_PENDING_E);
  16300. if (ret < 0)
  16301. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_rsa);
  16302. derSz = (word32)ret;
  16303. ret = SaveDerAndPem(der, derSz, certReqDerFile, certReqPemFile,
  16304. CERTREQ_TYPE);
  16305. if (ret != 0) {
  16306. goto exit_rsa;
  16307. }
  16308. derSz = wc_MakeCertReq_ex(req, der, FOURK_BUF, RSA_TYPE, key);
  16309. if (derSz < 0) {
  16310. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  16311. }
  16312. /* Test getting the size of the buffer without providing the buffer.
  16313. * derSz is set to the "largest buffer" we are willing to allocate. */
  16314. derSz = wc_MakeCertReq(req, NULL, 10000, key, NULL);
  16315. if (derSz < 0) {
  16316. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), exit_rsa);
  16317. }
  16318. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16319. XFREE(req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16320. der = NULL;
  16321. }
  16322. #endif /* WOLFSSL_CERT_REQ */
  16323. #endif /* WOLFSSL_CERT_GEN */
  16324. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  16325. /* Need to create known good signatures to test with this. */
  16326. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  16327. !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  16328. ret = rsa_pss_test(&rng, key);
  16329. if (ret != 0)
  16330. goto exit_rsa;
  16331. #endif
  16332. #endif
  16333. #if defined(WOLFSSL_HAVE_SP_RSA) && defined(USE_FAST_MATH)
  16334. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16335. /* New key to be loaded in rsa_even_mod_test(). */
  16336. if (key != NULL)
  16337. #endif
  16338. wc_FreeRsaKey(key);
  16339. /* New key to be loaded in rsa_even_mod_test(). */
  16340. ret = rsa_even_mod_test(&rng, key);
  16341. #endif
  16342. exit_rsa:
  16343. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16344. if (key != NULL) {
  16345. wc_FreeRsaKey(key);
  16346. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16347. }
  16348. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  16349. if (keypub != NULL) {
  16350. wc_FreeRsaKey(keypub);
  16351. XFREE(keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16352. }
  16353. #endif
  16354. #ifdef WOLFSSL_TEST_CERT
  16355. if (cert != NULL)
  16356. XFREE(cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16357. #endif
  16358. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16359. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16360. #else
  16361. wc_FreeRsaKey(key);
  16362. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  16363. wc_FreeRsaKey(keypub);
  16364. #endif
  16365. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  16366. wc_FreeRng(&rng);
  16367. WC_FREE_VAR(in, HEAP_HINT);
  16368. WC_FREE_VAR(out, HEAP_HINT);
  16369. WC_FREE_VAR(plain, HEAP_HINT);
  16370. (void)res;
  16371. (void)bytes;
  16372. (void)idx;
  16373. (void)in;
  16374. (void)out;
  16375. (void)plain;
  16376. (void)idx;
  16377. (void)inStr;
  16378. (void)inLen;
  16379. (void)outSz;
  16380. (void)plainSz;
  16381. /* ret can be greater then 0 with certgen but all negative values should
  16382. * be returned and treated as an error */
  16383. if (ret >= 0) {
  16384. return 0;
  16385. }
  16386. else {
  16387. return ret;
  16388. }
  16389. }
  16390. #endif /* !NO_RSA */
  16391. #ifndef NO_DH
  16392. static wc_test_ret_t dh_fips_generate_test(WC_RNG *rng)
  16393. {
  16394. wc_test_ret_t ret = 0;
  16395. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16396. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16397. #else
  16398. DhKey key[1];
  16399. #endif
  16400. WOLFSSL_SMALL_STACK_STATIC const byte p[] = {
  16401. 0xc5, 0x7c, 0xa2, 0x4f, 0x4b, 0xd6, 0x8c, 0x3c,
  16402. 0xda, 0xc7, 0xba, 0xaa, 0xea, 0x2e, 0x5c, 0x1e,
  16403. 0x18, 0xb2, 0x7b, 0x8c, 0x55, 0x65, 0x9f, 0xea,
  16404. 0xe0, 0xa1, 0x36, 0x53, 0x2b, 0x36, 0xe0, 0x4e,
  16405. 0x3e, 0x64, 0xa9, 0xe4, 0xfc, 0x8f, 0x32, 0x62,
  16406. 0x97, 0xe4, 0xbe, 0xf7, 0xc1, 0xde, 0x07, 0x5a,
  16407. 0x89, 0x28, 0xf3, 0xfe, 0x4f, 0xfe, 0x68, 0xbc,
  16408. 0xfb, 0x0a, 0x7c, 0xa4, 0xb3, 0x14, 0x48, 0x89,
  16409. 0x9f, 0xaf, 0xb8, 0x43, 0xe2, 0xa0, 0x62, 0x5c,
  16410. 0xb4, 0x88, 0x3f, 0x06, 0x50, 0x11, 0xfe, 0x65,
  16411. 0x8d, 0x49, 0xd2, 0xf5, 0x4b, 0x74, 0x79, 0xdb,
  16412. 0x06, 0x62, 0x92, 0x89, 0xed, 0xda, 0xcb, 0x87,
  16413. 0x37, 0x16, 0xd2, 0xa1, 0x7a, 0xe8, 0xde, 0x92,
  16414. 0xee, 0x3e, 0x41, 0x4a, 0x91, 0x5e, 0xed, 0xf3,
  16415. 0x6c, 0x6b, 0x7e, 0xfd, 0x15, 0x92, 0x18, 0xfc,
  16416. 0xa7, 0xac, 0x42, 0x85, 0x57, 0xe9, 0xdc, 0xda,
  16417. 0x55, 0xc9, 0x8b, 0x28, 0x9e, 0xc1, 0xc4, 0x46,
  16418. 0x4d, 0x88, 0xed, 0x62, 0x8e, 0xdb, 0x3f, 0xb9,
  16419. 0xd7, 0xc8, 0xe3, 0xcf, 0xb8, 0x34, 0x2c, 0xd2,
  16420. 0x6f, 0x28, 0x06, 0x41, 0xe3, 0x66, 0x8c, 0xfc,
  16421. 0x72, 0xff, 0x26, 0x3b, 0x6b, 0x6c, 0x6f, 0x73,
  16422. 0xde, 0xf2, 0x90, 0x29, 0xe0, 0x61, 0x32, 0xc4,
  16423. 0x12, 0x74, 0x09, 0x52, 0xec, 0xf3, 0x1b, 0xa6,
  16424. 0x45, 0x98, 0xac, 0xf9, 0x1c, 0x65, 0x8e, 0x3a,
  16425. 0x91, 0x84, 0x4b, 0x23, 0x8a, 0xb2, 0x3c, 0xc9,
  16426. 0xfa, 0xea, 0xf1, 0x38, 0xce, 0xd8, 0x05, 0xe0,
  16427. 0xfa, 0x44, 0x68, 0x1f, 0xeb, 0xd9, 0x57, 0xb8,
  16428. 0x4a, 0x97, 0x5b, 0x88, 0xc5, 0xf1, 0xbb, 0xb0,
  16429. 0x49, 0xc3, 0x91, 0x7c, 0xd3, 0x13, 0xb9, 0x47,
  16430. 0xbb, 0x91, 0x8f, 0xe5, 0x26, 0x07, 0xab, 0xa9,
  16431. 0xc5, 0xd0, 0x3d, 0x95, 0x41, 0x26, 0x92, 0x9d,
  16432. 0x13, 0x67, 0xf2, 0x7e, 0x11, 0x88, 0xdc, 0x2d
  16433. };
  16434. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  16435. 0x4a, 0x1a, 0xf3, 0xa4, 0x92, 0xe9, 0xee, 0x74,
  16436. 0x6e, 0x57, 0xd5, 0x8c, 0x2c, 0x5b, 0x41, 0x41,
  16437. 0x5e, 0xd4, 0x55, 0x19, 0xdc, 0xd9, 0x32, 0x91,
  16438. 0xf7, 0xfd, 0xc2, 0x57, 0xff, 0x03, 0x14, 0xdb,
  16439. 0xf1, 0xb7, 0x60, 0x0c, 0x43, 0x59, 0x3f, 0xff,
  16440. 0xac, 0xf1, 0x80, 0x9a, 0x15, 0x6f, 0xd8, 0x6e,
  16441. 0xb7, 0x85, 0x18, 0xc8, 0xec, 0x4e, 0x59, 0x4a,
  16442. 0xe2, 0x91, 0x43, 0x4c, 0xeb, 0x95, 0xb6, 0x2e,
  16443. 0x9a, 0xea, 0x53, 0x68, 0x80, 0x64, 0x69, 0x40,
  16444. 0xf9, 0xec, 0xbd, 0x85, 0x89, 0x26, 0x97, 0x67,
  16445. 0xaf, 0xb0, 0xad, 0x00, 0x1b, 0xd4, 0xfd, 0x94,
  16446. 0xd3, 0xe9, 0x92, 0xb1, 0xb4, 0xbc, 0x5a, 0xaa,
  16447. 0x92, 0x80, 0x89, 0x3b, 0x39, 0x05, 0x6c, 0x22,
  16448. 0x26, 0xfe, 0x5a, 0x28, 0x6c, 0x37, 0x50, 0x5a,
  16449. 0x38, 0x99, 0xcf, 0xf3, 0xc1, 0x96, 0x45, 0xdc,
  16450. 0x01, 0xcb, 0x20, 0x87, 0xa5, 0x00, 0x8c, 0xf5,
  16451. 0x4d, 0xc2, 0xef, 0xb8, 0x9b, 0xd1, 0x87, 0xbe,
  16452. 0xed, 0xd5, 0x0a, 0x29, 0x15, 0x34, 0x59, 0x4c,
  16453. 0x3a, 0x05, 0x22, 0x05, 0x44, 0x4f, 0x9f, 0xc8,
  16454. 0x47, 0x12, 0x24, 0x8e, 0xa8, 0x79, 0xe4, 0x67,
  16455. 0xba, 0x4d, 0x5b, 0x75, 0x56, 0x95, 0xeb, 0xe8,
  16456. 0x8a, 0xfa, 0x8e, 0x01, 0x8c, 0x1b, 0x74, 0x63,
  16457. 0xd9, 0x2f, 0xf7, 0xd3, 0x44, 0x8f, 0xa8, 0xf5,
  16458. 0xaf, 0x6c, 0x4f, 0xdb, 0xe7, 0xc9, 0x6c, 0x71,
  16459. 0x22, 0xa3, 0x1d, 0xf1, 0x40, 0xb2, 0xe0, 0x9a,
  16460. 0xb6, 0x72, 0xc9, 0xc0, 0x13, 0x16, 0xa2, 0x4a,
  16461. 0xe1, 0x92, 0xc7, 0x54, 0x23, 0xab, 0x9d, 0xa1,
  16462. 0xa1, 0xe5, 0x0b, 0xed, 0xba, 0xe8, 0x84, 0x37,
  16463. 0xb2, 0xe7, 0xfe, 0x32, 0x8d, 0xfa, 0x1c, 0x53,
  16464. 0x77, 0x97, 0xc7, 0xf3, 0x48, 0xc9, 0xdb, 0x2d,
  16465. 0x75, 0x52, 0x9d, 0x42, 0x51, 0x78, 0x62, 0x68,
  16466. 0x05, 0x45, 0x15, 0xf8, 0xa2, 0x4e, 0xf3, 0x0b
  16467. };
  16468. WOLFSSL_SMALL_STACK_STATIC const byte q[] = {
  16469. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  16470. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  16471. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  16472. 0x40, 0x52, 0xed, 0x41
  16473. };
  16474. WOLFSSL_SMALL_STACK_STATIC const byte q0[] = {
  16475. 0x00,
  16476. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  16477. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  16478. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  16479. 0x40, 0x52, 0xed, 0x41
  16480. };
  16481. byte priv[256];
  16482. byte pub[256];
  16483. word32 privSz = sizeof(priv);
  16484. word32 pubSz = sizeof(pub);
  16485. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16486. if (key == NULL)
  16487. ERROR_OUT(MEMORY_E, exit_gen_test);
  16488. #endif
  16489. /* Parameter Validation testing. */
  16490. ret = wc_DhGenerateKeyPair(NULL, rng, priv, &privSz, pub, &pubSz);
  16491. if (ret != BAD_FUNC_ARG)
  16492. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16493. ret = wc_DhGenerateKeyPair(key, NULL, priv, &privSz, pub, &pubSz);
  16494. if (ret != BAD_FUNC_ARG)
  16495. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16496. ret = wc_DhGenerateKeyPair(key, rng, NULL, &privSz, pub, &pubSz);
  16497. if (ret != BAD_FUNC_ARG)
  16498. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16499. ret = wc_DhGenerateKeyPair(key, rng, priv, NULL, pub, &pubSz);
  16500. if (ret != BAD_FUNC_ARG)
  16501. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16502. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, NULL, &pubSz);
  16503. if (ret != BAD_FUNC_ARG)
  16504. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16505. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, NULL);
  16506. if (ret != BAD_FUNC_ARG)
  16507. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16508. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  16509. if (ret != 0)
  16510. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16511. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q0, sizeof(q0));
  16512. if (ret != 0)
  16513. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16514. wc_FreeDhKey(key);
  16515. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  16516. if (ret != 0)
  16517. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16518. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q, sizeof(q));
  16519. if (ret != 0)
  16520. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16521. /* Use API. */
  16522. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  16523. #if defined(WOLFSSL_ASYNC_CRYPT)
  16524. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  16525. #endif
  16526. if (ret != 0)
  16527. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16528. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q0, sizeof(q0));
  16529. if (ret != 0)
  16530. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16531. wc_FreeDhKey(key);
  16532. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  16533. if (ret != 0)
  16534. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16535. ret = wc_DhSetKey(key, p, sizeof(p), g, sizeof(g));
  16536. if (ret != 0)
  16537. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16538. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q, sizeof(q));
  16539. if (ret != 0)
  16540. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16541. #ifndef HAVE_SELFTEST
  16542. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  16543. if (ret != 0)
  16544. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16545. /* Taint the public key so the check fails. */
  16546. pub[0]++;
  16547. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  16548. if (ret != MP_CMP_E) {
  16549. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16550. }
  16551. #ifdef WOLFSSL_KEY_GEN
  16552. wc_FreeDhKey(key);
  16553. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  16554. if (ret != 0)
  16555. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16556. ret = wc_DhGenerateParams(rng, 2048, key);
  16557. if (ret != 0)
  16558. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16559. privSz = sizeof(priv);
  16560. pubSz = sizeof(pub);
  16561. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  16562. #if defined(WOLFSSL_ASYNC_CRYPT)
  16563. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  16564. #endif
  16565. if (ret != 0)
  16566. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16567. #endif /* WOLFSSL_KEY_GEN */
  16568. #endif /* HAVE_SELFTEST */
  16569. ret = 0;
  16570. exit_gen_test:
  16571. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16572. if (key) {
  16573. wc_FreeDhKey(key);
  16574. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16575. }
  16576. #else
  16577. wc_FreeDhKey(key);
  16578. #endif
  16579. return ret;
  16580. }
  16581. static wc_test_ret_t dh_generate_test(WC_RNG *rng)
  16582. {
  16583. wc_test_ret_t ret = 0;
  16584. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16585. DhKey *smallKey = NULL;
  16586. #else
  16587. DhKey smallKey[1];
  16588. #endif
  16589. byte p[2] = { 1, 7 }; /* 263 in decimal */
  16590. byte g[2] = { 0, 2 };
  16591. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FFDHE)
  16592. #ifdef WOLFSSL_DH_CONST
  16593. /* the table for constant DH lookup will round to the lowest byte size 21 */
  16594. byte priv[21];
  16595. byte pub[21];
  16596. #else
  16597. byte priv[2];
  16598. byte pub[2];
  16599. #endif
  16600. word32 privSz = sizeof(priv);
  16601. word32 pubSz = sizeof(pub);
  16602. #endif
  16603. int smallKey_inited = 0;
  16604. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16605. if ((smallKey = (DhKey *)XMALLOC(sizeof(*smallKey), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  16606. return WC_TEST_RET_ENC_ERRNO;
  16607. #endif
  16608. ret = wc_InitDhKey_ex(smallKey, HEAP_HINT, devId);
  16609. if (ret != 0)
  16610. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16611. smallKey_inited = 1;
  16612. /* Parameter Validation testing. */
  16613. ret = wc_InitDhKey_ex(NULL, HEAP_HINT, devId);
  16614. if (ret != BAD_FUNC_ARG)
  16615. return WC_TEST_RET_ENC_EC(ret);
  16616. wc_FreeDhKey(NULL);
  16617. ret = wc_DhSetKey(NULL, p, sizeof(p), g, sizeof(g));
  16618. if (ret != BAD_FUNC_ARG) {
  16619. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16620. }
  16621. ret = wc_DhSetKey(smallKey, NULL, sizeof(p), g, sizeof(g));
  16622. if (ret != BAD_FUNC_ARG) {
  16623. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16624. }
  16625. ret = wc_DhSetKey(smallKey, p, 0, g, sizeof(g));
  16626. if (ret != BAD_FUNC_ARG) {
  16627. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16628. }
  16629. ret = wc_DhSetKey(smallKey, p, sizeof(p), NULL, sizeof(g));
  16630. if (ret != BAD_FUNC_ARG) {
  16631. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16632. }
  16633. ret = wc_DhSetKey(smallKey, p, sizeof(p), g, 0);
  16634. if (ret != BAD_FUNC_ARG) {
  16635. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16636. }
  16637. ret = wc_DhSetKey(smallKey, p, sizeof(p), g, sizeof(g));
  16638. if (ret != 0)
  16639. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16640. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FFDHE)
  16641. /* Use API. */
  16642. ret = wc_DhGenerateKeyPair(smallKey, rng, priv, &privSz, pub, &pubSz);
  16643. #if defined(WOLFSSL_ASYNC_CRYPT)
  16644. ret = wc_AsyncWait(ret, &smallKey->asyncDev, WC_ASYNC_FLAG_NONE);
  16645. #endif
  16646. if (ret != 0) {
  16647. ret = WC_TEST_RET_ENC_EC(ret);
  16648. }
  16649. #else
  16650. (void)rng;
  16651. #if defined(HAVE_FIPS) || !defined(WOLFSSL_NO_DH186)
  16652. ret = 0;
  16653. #endif
  16654. #endif
  16655. #if !defined(HAVE_FIPS) && defined(WOLFSSL_NO_DH186)
  16656. {
  16657. byte priv[260];
  16658. byte pub[260];
  16659. word32 privSz = sizeof(priv);
  16660. word32 pubSz = sizeof(pub);
  16661. /* test odd ball param generation with DH */
  16662. wc_FreeDhKey(smallKey);
  16663. ret = wc_InitDhKey_ex(smallKey, HEAP_HINT, devId);
  16664. if (ret != 0)
  16665. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16666. ret = wc_DhGenerateParams(rng, 2056, smallKey);
  16667. if (ret != 0)
  16668. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16669. privSz = sizeof(priv);
  16670. pubSz = sizeof(pub);
  16671. ret = wc_DhGenerateKeyPair(smallKey, rng, priv, &privSz, pub, &pubSz);
  16672. #if defined(WOLFSSL_ASYNC_CRYPT)
  16673. ret = wc_AsyncWait(ret, &smallKey->asyncDev, WC_ASYNC_FLAG_NONE);
  16674. #endif
  16675. if (ret != 0)
  16676. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_gen_test);
  16677. }
  16678. #endif /* !HAVE_FIPS and WOLFSSL_NO_DH186 */
  16679. exit_gen_test:
  16680. if (smallKey_inited)
  16681. wc_FreeDhKey(smallKey);
  16682. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16683. if (smallKey != NULL)
  16684. XFREE(smallKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16685. #endif
  16686. return ret;
  16687. }
  16688. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  16689. typedef struct dh_pubvalue_test {
  16690. const byte* data;
  16691. word32 len;
  16692. } dh_pubvalue_test;
  16693. static wc_test_ret_t dh_test_check_pubvalue(void)
  16694. {
  16695. wc_test_ret_t ret;
  16696. word32 i;
  16697. WOLFSSL_SMALL_STACK_STATIC const byte prime[] = {0x01, 0x00, 0x01};
  16698. WOLFSSL_SMALL_STACK_STATIC const byte pubValZero[] = { 0x00 };
  16699. WOLFSSL_SMALL_STACK_STATIC const byte pubValZeroLong[] = { 0x00, 0x00, 0x00 };
  16700. WOLFSSL_SMALL_STACK_STATIC const byte pubValOne[] = { 0x01 };
  16701. WOLFSSL_SMALL_STACK_STATIC const byte pubValOneLong[] = { 0x00, 0x00, 0x01 };
  16702. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeMinusOne[] = { 0x01, 0x00, 0x00 };
  16703. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeLong[] = {0x00, 0x01, 0x00, 0x01};
  16704. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimePlusOne[] = { 0x01, 0x00, 0x02 };
  16705. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig0[] = { 0x02, 0x00, 0x01 };
  16706. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig1[] = { 0x01, 0x01, 0x01 };
  16707. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooLong[] = { 0x01, 0x00, 0x00, 0x01 };
  16708. const dh_pubvalue_test dh_pubval_fail[] = {
  16709. { prime, sizeof(prime) },
  16710. { pubValZero, sizeof(pubValZero) },
  16711. { pubValZeroLong, sizeof(pubValZeroLong) },
  16712. { pubValOne, sizeof(pubValOne) },
  16713. { pubValOneLong, sizeof(pubValOneLong) },
  16714. { pubValPrimeMinusOne, sizeof(pubValPrimeMinusOne) },
  16715. { pubValPrimeLong, sizeof(pubValPrimeLong) },
  16716. { pubValPrimePlusOne, sizeof(pubValPrimePlusOne) },
  16717. { pubValTooBig0, sizeof(pubValTooBig0) },
  16718. { pubValTooBig1, sizeof(pubValTooBig1) },
  16719. { pubValTooLong, sizeof(pubValTooLong) },
  16720. };
  16721. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwo[] = { 0x02 };
  16722. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwoLong[] = { 0x00, 0x00, 0x02 };
  16723. WOLFSSL_SMALL_STACK_STATIC const byte pubValGood[] = { 0x12, 0x34 };
  16724. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLen[] = { 0x00, 0x12, 0x34 };
  16725. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLong[] = { 0x00, 0x00, 0x12, 0x34 };
  16726. const dh_pubvalue_test dh_pubval_pass[] = {
  16727. { pubValTwo, sizeof(pubValTwo) },
  16728. { pubValTwoLong, sizeof(pubValTwoLong) },
  16729. { pubValGood, sizeof(pubValGood) },
  16730. { pubValGoodLen, sizeof(pubValGoodLen) },
  16731. { pubValGoodLong, sizeof(pubValGoodLong) },
  16732. };
  16733. for (i = 0; i < sizeof(dh_pubval_fail) / sizeof(*dh_pubval_fail); i++) {
  16734. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_fail[i].data,
  16735. dh_pubval_fail[i].len);
  16736. if (ret != MP_VAL)
  16737. return WC_TEST_RET_ENC_I(i);
  16738. }
  16739. for (i = 0; i < sizeof(dh_pubval_pass) / sizeof(*dh_pubval_pass); i++) {
  16740. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_pass[i].data,
  16741. dh_pubval_pass[i].len);
  16742. if (ret != 0)
  16743. return WC_TEST_RET_ENC_I(i);
  16744. }
  16745. return 0;
  16746. }
  16747. #endif
  16748. #if defined(HAVE_FFDHE)
  16749. #if defined(HAVE_FFDHE_4096)
  16750. #define MAX_DH_PRIV_SZ 39
  16751. #define MAX_DH_KEY_SZ 512
  16752. #elif defined(HAVE_FFDHE_3072)
  16753. #define MAX_DH_PRIV_SZ 34
  16754. #define MAX_DH_KEY_SZ 384
  16755. #else
  16756. #define MAX_DH_PRIV_SZ 29
  16757. #define MAX_DH_KEY_SZ 256
  16758. #endif
  16759. #ifndef WC_NO_RNG
  16760. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  16761. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM)))
  16762. #ifdef HAVE_PUBLIC_FFDHE
  16763. static wc_test_ret_t dh_ffdhe_test(WC_RNG *rng, const DhParams* params)
  16764. #else
  16765. static wc_test_ret_t dh_ffdhe_test(WC_RNG *rng, int name)
  16766. #endif
  16767. {
  16768. wc_test_ret_t ret;
  16769. word32 privSz, pubSz, privSz2, pubSz2;
  16770. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16771. byte *priv = (byte*)XMALLOC(MAX_DH_PRIV_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16772. byte *pub = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16773. byte *priv2 = (byte*)XMALLOC(MAX_DH_PRIV_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16774. byte *pub2 = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16775. byte *agree = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16776. byte *agree2 = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16777. DhKey *key = (DhKey*)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16778. DhKey *key2 = (DhKey*)XMALLOC(sizeof(*key2), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16779. #else
  16780. byte priv[MAX_DH_PRIV_SZ];
  16781. byte pub[MAX_DH_KEY_SZ];
  16782. byte priv2[MAX_DH_PRIV_SZ];
  16783. byte pub2[MAX_DH_KEY_SZ];
  16784. byte agree[MAX_DH_KEY_SZ];
  16785. byte agree2[MAX_DH_KEY_SZ];
  16786. DhKey key[1];
  16787. DhKey key2[1];
  16788. #endif
  16789. word32 agreeSz = MAX_DH_KEY_SZ;
  16790. word32 agreeSz2 = MAX_DH_KEY_SZ;
  16791. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16792. if ((priv == NULL) ||
  16793. (pub == NULL) ||
  16794. (priv2 == NULL) ||
  16795. (pub2 == NULL) ||
  16796. (agree == NULL) ||
  16797. (agree2 == NULL) ||
  16798. (key == NULL) ||
  16799. (key2 == NULL))
  16800. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  16801. #endif
  16802. pubSz = MAX_DH_KEY_SZ;
  16803. pubSz2 = MAX_DH_KEY_SZ;
  16804. #ifdef HAVE_PUBLIC_FFDHE
  16805. privSz = MAX_DH_PRIV_SZ;
  16806. privSz2 = MAX_DH_PRIV_SZ;
  16807. #else
  16808. privSz = wc_DhGetNamedKeyMinSize(name);
  16809. privSz2 = privSz;
  16810. #endif
  16811. XMEMSET(key, 0, sizeof(*key));
  16812. XMEMSET(key2, 0, sizeof(*key2));
  16813. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  16814. if (ret != 0)
  16815. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16816. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  16817. if (ret != 0)
  16818. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16819. #ifdef HAVE_PUBLIC_FFDHE
  16820. ret = wc_DhSetKey(key, params->p, params->p_len, params->g, params->g_len);
  16821. #else
  16822. ret = wc_DhSetNamedKey(key, name);
  16823. #endif
  16824. if (ret != 0)
  16825. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16826. #ifdef HAVE_PUBLIC_FFDHE
  16827. ret = wc_DhSetKey(key2, params->p, params->p_len, params->g,
  16828. params->g_len);
  16829. #else
  16830. ret = wc_DhSetNamedKey(key2, name);
  16831. #endif
  16832. if (ret != 0)
  16833. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16834. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  16835. #if defined(WOLFSSL_ASYNC_CRYPT)
  16836. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  16837. #endif
  16838. if (ret != 0)
  16839. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16840. ret = wc_DhGenerateKeyPair(key2, rng, priv2, &privSz2, pub2, &pubSz2);
  16841. #if defined(WOLFSSL_ASYNC_CRYPT)
  16842. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  16843. #endif
  16844. if (ret != 0)
  16845. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16846. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  16847. #if defined(WOLFSSL_ASYNC_CRYPT)
  16848. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  16849. #endif
  16850. if (ret != 0)
  16851. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16852. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  16853. #if defined(WOLFSSL_ASYNC_CRYPT)
  16854. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  16855. #endif
  16856. if (ret != 0)
  16857. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16858. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  16859. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  16860. }
  16861. #if defined(WOLFSSL_HAVE_SP_DH) || defined(USE_FAST_MATH)
  16862. /* Make p even */
  16863. key->p.dp[0] &= (mp_digit)-2;
  16864. if (ret != 0)
  16865. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16866. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  16867. #if defined(WOLFSSL_ASYNC_CRYPT)
  16868. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  16869. #endif
  16870. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  16871. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16872. }
  16873. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  16874. #if defined(WOLFSSL_ASYNC_CRYPT)
  16875. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  16876. #endif
  16877. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != ASYNC_OP_E) {
  16878. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16879. }
  16880. #ifndef HAVE_SELFTEST
  16881. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  16882. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_CMP_E &&
  16883. ret != ASYNC_OP_E) {
  16884. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  16885. }
  16886. #endif
  16887. /* Getting here means success - set ret to 0. */
  16888. ret = 0;
  16889. #endif
  16890. done:
  16891. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) && \
  16892. !defined(WC_NO_RNG)
  16893. if (priv)
  16894. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16895. if (pub)
  16896. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16897. if (priv2)
  16898. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16899. if (pub2)
  16900. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16901. if (agree)
  16902. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16903. if (agree2)
  16904. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16905. if (key) {
  16906. wc_FreeDhKey(key);
  16907. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16908. }
  16909. if (key2) {
  16910. wc_FreeDhKey(key2);
  16911. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16912. }
  16913. #else
  16914. wc_FreeDhKey(key);
  16915. wc_FreeDhKey(key2);
  16916. #endif
  16917. return ret;
  16918. }
  16919. #endif /* !(HAVE_FIPS_VERSION == 2 && WOLFSSL_SP_ARMxx_ASM) */
  16920. #endif /* !WC_NO_RNG */
  16921. #endif /* HAVE_FFDHE */
  16922. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t dh_test(void)
  16923. {
  16924. wc_test_ret_t ret;
  16925. word32 bytes;
  16926. word32 idx = 0, privSz, pubSz, privSz2, pubSz2;
  16927. #ifndef WC_NO_RNG
  16928. WC_RNG rng;
  16929. int rngInit = 0;
  16930. #endif
  16931. int keyInit = 0, key2Init = 0;
  16932. #define DH_TEST_TMP_SIZE 1024
  16933. #if !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  16934. #define DH_TEST_BUF_SIZE 256
  16935. #else
  16936. #define DH_TEST_BUF_SIZE 512
  16937. #endif
  16938. #ifndef WC_NO_RNG
  16939. word32 agreeSz = DH_TEST_BUF_SIZE;
  16940. word32 agreeSz2 = DH_TEST_BUF_SIZE;
  16941. #endif
  16942. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16943. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16944. DhKey *key2 = (DhKey *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16945. byte *tmp = (byte *)XMALLOC(DH_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16946. #else
  16947. DhKey key[1];
  16948. DhKey key2[1];
  16949. byte tmp[DH_TEST_TMP_SIZE];
  16950. #endif
  16951. #ifndef WC_NO_RNG
  16952. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16953. byte *priv = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16954. byte *pub = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16955. byte *priv2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16956. byte *pub2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16957. byte *agree = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16958. byte *agree2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16959. if (priv == NULL || pub == NULL || priv2 == NULL || pub2 == NULL ||
  16960. agree == NULL || agree2 == NULL) {
  16961. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16962. }
  16963. #else
  16964. byte priv[DH_TEST_BUF_SIZE];
  16965. byte pub[DH_TEST_BUF_SIZE];
  16966. byte priv2[DH_TEST_BUF_SIZE];
  16967. byte pub2[DH_TEST_BUF_SIZE];
  16968. byte agree[DH_TEST_BUF_SIZE];
  16969. byte agree2[DH_TEST_BUF_SIZE];
  16970. #endif
  16971. #endif /* !WC_NO_RNG */
  16972. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16973. if (key == NULL || key2 == NULL || tmp == NULL) {
  16974. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16975. }
  16976. #endif
  16977. #ifdef USE_CERT_BUFFERS_1024
  16978. XMEMCPY(tmp, dh_key_der_1024, (size_t)sizeof_dh_key_der_1024);
  16979. bytes = (size_t)sizeof_dh_key_der_1024;
  16980. #elif defined(USE_CERT_BUFFERS_2048)
  16981. XMEMCPY(tmp, dh_key_der_2048, (size_t)sizeof_dh_key_der_2048);
  16982. bytes = (size_t)sizeof_dh_key_der_2048;
  16983. #elif defined(USE_CERT_BUFFERS_3072)
  16984. XMEMCPY(tmp, dh_key_der_3072, (size_t)sizeof_dh_key_der_3072);
  16985. bytes = (size_t)sizeof_dh_key_der_3072;
  16986. #elif defined(USE_CERT_BUFFERS_4096)
  16987. XMEMCPY(tmp, dh_key_der_4096, (size_t)sizeof_dh_key_der_4096);
  16988. bytes = (size_t)sizeof_dh_key_der_4096;
  16989. #elif defined(NO_ASN)
  16990. /* don't use file, no DER parsing */
  16991. #elif !defined(NO_FILESYSTEM)
  16992. {
  16993. XFILE file = XFOPEN(dhParamsFile, "rb");
  16994. if (! file)
  16995. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  16996. bytes = (word32) XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  16997. XFCLOSE(file);
  16998. if (bytes == 0)
  16999. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  17000. }
  17001. #else
  17002. /* No DH key to use. */
  17003. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  17004. #endif /* USE_CERT_BUFFERS */
  17005. (void)idx;
  17006. (void)tmp;
  17007. (void)bytes;
  17008. pubSz = DH_TEST_BUF_SIZE;
  17009. pubSz2 = DH_TEST_BUF_SIZE;
  17010. privSz = DH_TEST_BUF_SIZE;
  17011. privSz2 = DH_TEST_BUF_SIZE;
  17012. #ifndef WC_NO_RNG
  17013. XMEMSET(&rng, 0, sizeof(rng));
  17014. #endif
  17015. /* Use API for coverage. */
  17016. ret = wc_InitDhKey(key);
  17017. if (ret != 0)
  17018. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17019. wc_FreeDhKey(key);
  17020. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  17021. if (ret != 0)
  17022. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17023. keyInit = 1;
  17024. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  17025. if (ret != 0)
  17026. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17027. key2Init = 1;
  17028. #ifdef NO_ASN
  17029. #ifndef WOLFSSL_SP_MATH
  17030. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  17031. if (ret != 0)
  17032. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17033. ret = wc_DhSetKey(key2, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  17034. if (ret != 0)
  17035. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17036. #else
  17037. ret = wc_DhSetKey(key, dh2048_p, sizeof(dh2048_p), dh2048_g,
  17038. sizeof(dh2048_g));
  17039. if (ret != 0)
  17040. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17041. ret = wc_DhSetKey(key2, dh2048_p, sizeof(dh2048_p), dh2048_g,
  17042. sizeof(dh2048_g));
  17043. if (ret != 0)
  17044. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17045. #endif
  17046. #else
  17047. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  17048. if (ret != 0)
  17049. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17050. idx = 0;
  17051. ret = wc_DhKeyDecode(tmp, &idx, key2, bytes);
  17052. if (ret != 0)
  17053. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17054. #endif
  17055. #ifndef WC_NO_RNG
  17056. #ifndef HAVE_FIPS
  17057. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  17058. #else
  17059. ret = wc_InitRng(&rng);
  17060. #endif
  17061. if (ret != 0)
  17062. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17063. rngInit = 1;
  17064. ret = wc_DhGenerateKeyPair(key, &rng, priv, &privSz, pub, &pubSz);
  17065. #if defined(WOLFSSL_ASYNC_CRYPT)
  17066. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  17067. #endif
  17068. if (ret != 0)
  17069. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17070. ret = wc_DhGenerateKeyPair(key2, &rng, priv2, &privSz2, pub2, &pubSz2);
  17071. #if defined(WOLFSSL_ASYNC_CRYPT)
  17072. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  17073. #endif
  17074. if (ret != 0)
  17075. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17076. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  17077. #if defined(WOLFSSL_ASYNC_CRYPT)
  17078. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  17079. #endif
  17080. if (ret != 0)
  17081. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17082. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  17083. #if defined(WOLFSSL_ASYNC_CRYPT)
  17084. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  17085. #endif
  17086. if (ret != 0)
  17087. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17088. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  17089. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  17090. }
  17091. #endif /* !WC_NO_RNG */
  17092. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  17093. ret = wc_DhCheckPrivKey(NULL, NULL, 0);
  17094. if (ret != BAD_FUNC_ARG)
  17095. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17096. ret = wc_DhCheckPrivKey(key, priv, privSz);
  17097. if (ret != 0)
  17098. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17099. ret = wc_DhExportParamsRaw(NULL, NULL, NULL, NULL, NULL, NULL, NULL);
  17100. if (ret != BAD_FUNC_ARG)
  17101. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17102. {
  17103. word32 pSz, qSz, gSz;
  17104. ret = wc_DhExportParamsRaw(key, NULL, &pSz, NULL, &qSz, NULL, &gSz);
  17105. if (ret != LENGTH_ONLY_E)
  17106. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17107. }
  17108. #endif
  17109. /* Test DH key import / export */
  17110. #if defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM) && \
  17111. (!defined(HAVE_FIPS) || \
  17112. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  17113. wc_FreeDhKey(key);
  17114. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  17115. if (ret != 0)
  17116. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17117. #ifndef NO_ASN
  17118. {
  17119. /* DH Private - Key Export / Import */
  17120. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17121. byte *tmp2;
  17122. #else
  17123. byte tmp2[DH_TEST_TMP_SIZE];
  17124. #endif
  17125. #if defined(USE_CERT_BUFFERS_2048)
  17126. XMEMCPY(tmp, dh_ffdhe_statickey_der_2048, sizeof_dh_ffdhe_statickey_der_2048);
  17127. bytes = sizeof_dh_ffdhe_statickey_der_2048;
  17128. #else
  17129. XFILE file = XFOPEN(dhKeyFile, "rb");
  17130. if (!file)
  17131. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  17132. bytes = (word32)XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  17133. XFCLOSE(file);
  17134. if (bytes == 0)
  17135. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  17136. #endif
  17137. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17138. tmp2 = (byte*)XMALLOC(DH_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17139. if (tmp2 == NULL)
  17140. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  17141. #endif
  17142. idx = 0;
  17143. XMEMSET(tmp2, 0, DH_TEST_TMP_SIZE);
  17144. /* Import DH Private key as DER */
  17145. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  17146. if (ret == 0) {
  17147. /* Export as DER */
  17148. idx = DH_TEST_TMP_SIZE;
  17149. ret = wc_DhPrivKeyToDer(key, tmp2, &idx);
  17150. }
  17151. /* Verify export matches original */
  17152. if (ret <= 0 || bytes != idx || XMEMCMP(tmp, tmp2, bytes) != 0) {
  17153. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  17154. }
  17155. /* DH Public Key - Export / Import */
  17156. #if defined(USE_CERT_BUFFERS_2048)
  17157. XMEMCPY(tmp, dh_ffdhe_pub_statickey_der_2048, sizeof_dh_ffdhe_pub_statickey_der_2048);
  17158. bytes = sizeof_dh_ffdhe_pub_statickey_der_2048;
  17159. #else
  17160. file = XFOPEN(dhKeyPubFile, "rb");
  17161. if (!file)
  17162. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  17163. bytes = (word32)XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  17164. XFCLOSE(file);
  17165. if (bytes == 0)
  17166. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  17167. #endif
  17168. /* for HAVE_WOLF_BIGINT prevent leak */
  17169. wc_FreeDhKey(key);
  17170. (void)wc_InitDhKey_ex(key, HEAP_HINT, devId);
  17171. idx = 0;
  17172. XMEMSET(tmp2, 0, DH_TEST_TMP_SIZE);
  17173. /* Import DH Public key as DER */
  17174. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  17175. if (ret == 0) {
  17176. /* Export as DER */
  17177. idx = DH_TEST_TMP_SIZE;
  17178. ret = wc_DhPubKeyToDer(key, tmp2, &idx);
  17179. }
  17180. /* Verify export matches original */
  17181. if (ret <= 0 || bytes != idx || XMEMCMP(tmp, tmp2, bytes) != 0) {
  17182. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  17183. }
  17184. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17185. XFREE(tmp2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17186. #endif
  17187. }
  17188. #else
  17189. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  17190. if (ret != 0)
  17191. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17192. #endif /* !NO_ASN */
  17193. privSz = DH_TEST_BUF_SIZE;
  17194. pubSz = DH_TEST_BUF_SIZE;
  17195. ret = wc_DhExportKeyPair(key, priv, &privSz, pub, &pubSz);
  17196. if (ret != 0)
  17197. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17198. ret = wc_DhImportKeyPair(key2, priv, privSz, pub, pubSz);
  17199. if (ret != 0)
  17200. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17201. #endif /* WOLFSSL_DH_EXTRA && !NO_FILESYSTEM && !FIPS <= 2 */
  17202. #ifndef WC_NO_RNG
  17203. ret = dh_generate_test(&rng);
  17204. if (ret != 0)
  17205. ERROR_OUT(ret, done);
  17206. ret = dh_fips_generate_test(&rng);
  17207. if (ret != 0)
  17208. ERROR_OUT(ret, done);
  17209. #endif /* !WC_NO_RNG */
  17210. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  17211. ret = dh_test_check_pubvalue();
  17212. if (ret != 0)
  17213. ERROR_OUT(ret, done);
  17214. #endif
  17215. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  17216. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM)))
  17217. /* RNG with DH and SP_ASM code not supported in the in-lined FIPS ASM code,
  17218. * this will be available for testing in the 140-3 module */
  17219. #ifndef WC_NO_RNG
  17220. /* Specialized code for key gen when using FFDHE-2048, FFDHE-3072 and FFDHE-4096 */
  17221. #ifdef HAVE_FFDHE_2048
  17222. #ifdef HAVE_PUBLIC_FFDHE
  17223. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe2048_Get());
  17224. #else
  17225. ret = dh_ffdhe_test(&rng, WC_FFDHE_2048);
  17226. #endif
  17227. if (ret != 0)
  17228. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17229. #endif
  17230. #ifdef HAVE_FFDHE_3072
  17231. #ifdef HAVE_PUBLIC_FFDHE
  17232. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe3072_Get());
  17233. #else
  17234. ret = dh_ffdhe_test(&rng, WC_FFDHE_3072);
  17235. #endif
  17236. if (ret != 0)
  17237. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17238. #endif
  17239. #ifdef HAVE_FFDHE_4096
  17240. #ifdef HAVE_PUBLIC_FFDHE
  17241. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe4096_Get());
  17242. #else
  17243. ret = dh_ffdhe_test(&rng, WC_FFDHE_4096);
  17244. #endif
  17245. if (ret != 0)
  17246. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17247. #endif
  17248. #endif /* !WC_NO_RNG */
  17249. #endif /* HAVE_FIPS_VERSION == 2 && !WOLFSSL_SP_ARM64_ASM */
  17250. wc_FreeDhKey(key);
  17251. keyInit = 0;
  17252. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  17253. !defined(WOLFSSL_OLD_PRIME_CHECK) && !defined(WC_NO_RNG)
  17254. /* Test Check Key */
  17255. ret = wc_DhSetCheckKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g),
  17256. NULL, 0, 0, &rng);
  17257. if (ret != 0)
  17258. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  17259. keyInit = 1; /* DhSetCheckKey also initializes the key, free it */
  17260. #endif
  17261. done:
  17262. #ifndef WC_NO_RNG
  17263. if (rngInit)
  17264. wc_FreeRng(&rng);
  17265. #endif
  17266. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17267. if (key) {
  17268. if (keyInit)
  17269. wc_FreeDhKey(key);
  17270. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17271. }
  17272. if (key2) {
  17273. if (key2Init)
  17274. wc_FreeDhKey(key2);
  17275. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17276. }
  17277. if (tmp)
  17278. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17279. if (priv)
  17280. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17281. if (pub)
  17282. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17283. if (priv2)
  17284. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17285. if (pub2)
  17286. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17287. if (agree)
  17288. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17289. if (agree2)
  17290. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17291. #else
  17292. if (keyInit)
  17293. wc_FreeDhKey(key);
  17294. if (key2Init)
  17295. wc_FreeDhKey(key2);
  17296. #endif
  17297. (void)privSz;
  17298. (void)pubSz;
  17299. (void)pubSz2;
  17300. (void)privSz2;
  17301. return ret;
  17302. #undef DH_TEST_BUF_SIZE
  17303. #undef DH_TEST_TMP_SIZE
  17304. }
  17305. #endif /* NO_DH */
  17306. #ifndef NO_DSA
  17307. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t dsa_test(void)
  17308. {
  17309. wc_test_ret_t ret = 0;
  17310. int answer;
  17311. word32 bytes;
  17312. word32 idx = 0;
  17313. WC_RNG rng;
  17314. wc_Sha sha;
  17315. byte hash[WC_SHA_DIGEST_SIZE];
  17316. byte signature[40];
  17317. #ifdef WOLFSSL_KEY_GEN
  17318. byte* der = 0;
  17319. #endif
  17320. #define DSA_TEST_TMP_SIZE 1024
  17321. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17322. byte *tmp = (byte *)XMALLOC(DSA_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17323. DsaKey *key = (DsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17324. #ifdef WOLFSSL_KEY_GEN
  17325. DsaKey *derIn = (DsaKey *)XMALLOC(sizeof *derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17326. DsaKey *genKey = (DsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17327. #endif
  17328. if ((tmp == NULL) ||
  17329. (key == NULL)
  17330. #ifdef WOLFSSL_KEY_GEN
  17331. || (derIn == NULL)
  17332. || (genKey == NULL)
  17333. #endif
  17334. ) {
  17335. ret = WC_TEST_RET_ENC_NC;
  17336. goto out;
  17337. }
  17338. #else
  17339. byte tmp[1024];
  17340. DsaKey key[1];
  17341. #ifdef WOLFSSL_KEY_GEN
  17342. DsaKey derIn[1];
  17343. DsaKey genKey[1];
  17344. #endif
  17345. #endif
  17346. #ifdef USE_CERT_BUFFERS_1024
  17347. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  17348. bytes = sizeof_dsa_key_der_1024;
  17349. #elif defined(USE_CERT_BUFFERS_2048)
  17350. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  17351. bytes = sizeof_dsa_key_der_2048;
  17352. #else
  17353. {
  17354. XFILE file = XFOPEN(dsaKey, "rb");
  17355. if (!file)
  17356. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  17357. bytes = (word32) XFREAD(tmp, 1, DSA_TEST_TMP_SIZE, file);
  17358. XFCLOSE(file);
  17359. if (bytes == 0)
  17360. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  17361. }
  17362. #endif /* USE_CERT_BUFFERS */
  17363. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  17364. if (ret != 0)
  17365. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  17366. wc_ShaUpdate(&sha, tmp, bytes);
  17367. wc_ShaFinal(&sha, hash);
  17368. wc_ShaFree(&sha);
  17369. ret = wc_InitDsaKey(key);
  17370. if (ret != 0)
  17371. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  17372. ret = wc_DsaPrivateKeyDecode(tmp, &idx, key, bytes);
  17373. if (ret != 0)
  17374. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  17375. #ifndef HAVE_FIPS
  17376. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  17377. #else
  17378. ret = wc_InitRng(&rng);
  17379. #endif
  17380. if (ret != 0)
  17381. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  17382. ret = wc_DsaSign(hash, signature, key, &rng);
  17383. if (ret != 0)
  17384. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  17385. ret = wc_DsaVerify(hash, signature, key, &answer);
  17386. if (ret != 0)
  17387. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  17388. if (answer != 1)
  17389. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  17390. wc_FreeDsaKey(key);
  17391. #ifdef WOLFSSL_KEY_GEN
  17392. {
  17393. int derSz = 0;
  17394. ret = wc_InitDsaKey(genKey);
  17395. if (ret != 0)
  17396. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  17397. ret = wc_MakeDsaParameters(&rng, 1024, genKey);
  17398. if (ret != 0) {
  17399. wc_FreeDsaKey(genKey);
  17400. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  17401. }
  17402. ret = wc_MakeDsaKey(&rng, genKey);
  17403. if (ret != 0) {
  17404. wc_FreeDsaKey(genKey);
  17405. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  17406. }
  17407. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17408. if (der == NULL) {
  17409. wc_FreeDsaKey(genKey);
  17410. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  17411. }
  17412. derSz = wc_DsaKeyToDer(genKey, der, FOURK_BUF);
  17413. if (derSz < 0) {
  17414. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17415. ERROR_OUT(WC_TEST_RET_ENC_EC(derSz), out);
  17416. }
  17417. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  17418. DSA_PRIVATEKEY_TYPE);
  17419. if (ret != 0) {
  17420. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17421. wc_FreeDsaKey(genKey);
  17422. goto out;
  17423. }
  17424. ret = wc_InitDsaKey(derIn);
  17425. if (ret != 0) {
  17426. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17427. wc_FreeDsaKey(genKey);
  17428. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  17429. }
  17430. idx = 0;
  17431. ret = wc_DsaPrivateKeyDecode(der, &idx, derIn, derSz);
  17432. if (ret != 0) {
  17433. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17434. wc_FreeDsaKey(derIn);
  17435. wc_FreeDsaKey(genKey);
  17436. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  17437. }
  17438. }
  17439. #endif /* WOLFSSL_KEY_GEN */
  17440. out:
  17441. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17442. if (key) {
  17443. #endif
  17444. ret = wc_InitDsaKey_h(key, NULL);
  17445. if (ret != 0)
  17446. ret = WC_TEST_RET_ENC_EC(ret);
  17447. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17448. }
  17449. #endif
  17450. #ifdef WOLFSSL_KEY_GEN
  17451. if (der)
  17452. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17453. #endif
  17454. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17455. if (tmp)
  17456. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17457. if (key)
  17458. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17459. #ifdef WOLFSSL_KEY_GEN
  17460. if (derIn) {
  17461. wc_FreeDsaKey(derIn);
  17462. XFREE(derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17463. }
  17464. if (genKey) {
  17465. wc_FreeDsaKey(genKey);
  17466. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17467. }
  17468. #endif
  17469. #else /* !WOLFSSL_SMALL_STACK || WOLFSSL_NO_MALLOC */
  17470. #ifdef WOLFSSL_KEY_GEN
  17471. wc_FreeDsaKey(derIn);
  17472. wc_FreeDsaKey(genKey);
  17473. #endif
  17474. #endif
  17475. wc_FreeRng(&rng);
  17476. return ret;
  17477. }
  17478. #endif /* NO_DSA */
  17479. #ifdef WOLFCRYPT_HAVE_SRP
  17480. static wc_test_ret_t generate_random_salt(byte *buf, word32 size)
  17481. {
  17482. wc_test_ret_t ret = WC_TEST_RET_ENC_NC;
  17483. WC_RNG rng;
  17484. if(NULL == buf || !size)
  17485. return WC_TEST_RET_ENC_NC;
  17486. if (buf && size && wc_InitRng_ex(&rng, HEAP_HINT, devId) == 0) {
  17487. ret = wc_RNG_GenerateBlock(&rng, (byte *)buf, size);
  17488. wc_FreeRng(&rng);
  17489. }
  17490. return ret;
  17491. }
  17492. static wc_test_ret_t srp_test_digest(SrpType dgstType)
  17493. {
  17494. wc_test_ret_t r;
  17495. byte clientPubKey[192]; /* A */
  17496. byte serverPubKey[192]; /* B */
  17497. word32 clientPubKeySz = 192;
  17498. word32 serverPubKeySz = 192;
  17499. byte username[] = "user";
  17500. word32 usernameSz = 4;
  17501. byte password[] = "password";
  17502. word32 passwordSz = 8;
  17503. WOLFSSL_SMALL_STACK_STATIC const byte N[] = {
  17504. 0xfc, 0x58, 0x7a, 0x8a, 0x70, 0xfb, 0x5a, 0x9a,
  17505. 0x5d, 0x39, 0x48, 0xbf, 0x1c, 0x46, 0xd8, 0x3b,
  17506. 0x7a, 0xe9, 0x1f, 0x85, 0x36, 0x18, 0xc4, 0x35,
  17507. 0x3f, 0xf8, 0x8a, 0x8f, 0x8c, 0x10, 0x2e, 0x01,
  17508. 0x58, 0x1d, 0x41, 0xcb, 0xc4, 0x47, 0xa8, 0xaf,
  17509. 0x9a, 0x6f, 0x58, 0x14, 0xa4, 0x68, 0xf0, 0x9c,
  17510. 0xa6, 0xe7, 0xbf, 0x0d, 0xe9, 0x62, 0x0b, 0xd7,
  17511. 0x26, 0x46, 0x5b, 0x27, 0xcb, 0x4c, 0xf9, 0x7e,
  17512. 0x1e, 0x8b, 0xe6, 0xdd, 0x29, 0xb7, 0xb7, 0x15,
  17513. 0x2e, 0xcf, 0x23, 0xa6, 0x4b, 0x97, 0x9f, 0x89,
  17514. 0xd4, 0x86, 0xc4, 0x90, 0x63, 0x92, 0xf4, 0x30,
  17515. 0x26, 0x69, 0x48, 0x9d, 0x7a, 0x4f, 0xad, 0xb5,
  17516. 0x6a, 0x51, 0xad, 0xeb, 0xf9, 0x90, 0x31, 0x77,
  17517. 0x53, 0x30, 0x2a, 0x85, 0xf7, 0x11, 0x21, 0x0c,
  17518. 0xb8, 0x4b, 0x56, 0x03, 0x5e, 0xbb, 0x25, 0x33,
  17519. 0x7c, 0xd9, 0x5a, 0xd1, 0x5c, 0xb2, 0xd4, 0x53,
  17520. 0xc5, 0x16, 0x68, 0xf0, 0xdf, 0x48, 0x55, 0x3e,
  17521. 0xd4, 0x59, 0x87, 0x64, 0x59, 0xaa, 0x39, 0x01,
  17522. 0x45, 0x89, 0x9c, 0x72, 0xff, 0xdd, 0x8f, 0x6d,
  17523. 0xa0, 0x42, 0xbc, 0x6f, 0x6e, 0x62, 0x18, 0x2d,
  17524. 0x50, 0xe8, 0x18, 0x97, 0x87, 0xfc, 0xef, 0x1f,
  17525. 0xf5, 0x53, 0x68, 0xe8, 0x49, 0xd1, 0xa2, 0xe8,
  17526. 0xb9, 0x26, 0x03, 0xba, 0xb5, 0x58, 0x6f, 0x6c,
  17527. 0x8b, 0x08, 0xa1, 0x7b, 0x6f, 0x42, 0xc9, 0x53
  17528. };
  17529. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  17530. 0x02
  17531. };
  17532. byte salt[10];
  17533. byte verifier[192];
  17534. word32 v_size = sizeof(verifier);
  17535. word32 clientProofSz = SRP_MAX_DIGEST_SIZE;
  17536. word32 serverProofSz = SRP_MAX_DIGEST_SIZE;
  17537. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17538. Srp *cli = (Srp *)XMALLOC(sizeof *cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17539. Srp *srv = (Srp *)XMALLOC(sizeof *srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17540. byte *clientProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT,
  17541. DYNAMIC_TYPE_TMP_BUFFER); /* M1 */
  17542. byte *serverProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT,
  17543. DYNAMIC_TYPE_TMP_BUFFER); /* M2 */
  17544. if ((cli == NULL) ||
  17545. (srv == NULL) ||
  17546. (clientProof == NULL) ||
  17547. (serverProof == NULL)) {
  17548. r = WC_TEST_RET_ENC_NC;
  17549. goto out;
  17550. }
  17551. #else
  17552. Srp cli[1], srv[1];
  17553. byte clientProof[SRP_MAX_DIGEST_SIZE]; /* M1 */
  17554. byte serverProof[SRP_MAX_DIGEST_SIZE]; /* M2 */
  17555. #endif
  17556. /* set as 0's so if second init on srv not called SrpTerm is not on
  17557. * garbage values */
  17558. XMEMSET(srv, 0, sizeof *srv);
  17559. XMEMSET(cli, 0, sizeof *cli);
  17560. /* generating random salt */
  17561. r = generate_random_salt(salt, sizeof(salt));
  17562. /* client knows username and password. */
  17563. /* server knows N, g, salt and verifier. */
  17564. if (!r) r = wc_SrpInit_ex(cli, dgstType, SRP_CLIENT_SIDE, HEAP_HINT, devId);
  17565. if (!r) r = wc_SrpSetUsername(cli, username, usernameSz);
  17566. /* loading N, g and salt in advance to generate the verifier. */
  17567. if (!r) r = wc_SrpSetParams(cli, N, sizeof(N),
  17568. g, sizeof(g),
  17569. salt, sizeof(salt));
  17570. if (!r) r = wc_SrpSetPassword(cli, password, passwordSz);
  17571. if (!r) r = wc_SrpGetVerifier(cli, verifier, &v_size);
  17572. /* client sends username to server */
  17573. if (!r) r = wc_SrpInit_ex(srv, dgstType, SRP_SERVER_SIDE, HEAP_HINT, devId);
  17574. if (!r) r = wc_SrpSetUsername(srv, username, usernameSz);
  17575. if (!r) r = wc_SrpSetParams(srv, N, sizeof(N),
  17576. g, sizeof(g),
  17577. salt, sizeof(salt));
  17578. if (!r) r = wc_SrpSetVerifier(srv, verifier, v_size);
  17579. if (!r) r = wc_SrpGetPublic(srv, serverPubKey, &serverPubKeySz);
  17580. /* server sends N, g, salt and B to client */
  17581. if (!r) r = wc_SrpGetPublic(cli, clientPubKey, &clientPubKeySz);
  17582. if (!r) r = wc_SrpComputeKey(cli, clientPubKey, clientPubKeySz,
  17583. serverPubKey, serverPubKeySz);
  17584. if (!r) r = wc_SrpGetProof(cli, clientProof, &clientProofSz);
  17585. /* client sends A and M1 to server */
  17586. if (!r) r = wc_SrpComputeKey(srv, clientPubKey, clientPubKeySz,
  17587. serverPubKey, serverPubKeySz);
  17588. if (!r) r = wc_SrpVerifyPeersProof(srv, clientProof, clientProofSz);
  17589. if (!r) r = wc_SrpGetProof(srv, serverProof, &serverProofSz);
  17590. /* server sends M2 to client */
  17591. if (!r) r = wc_SrpVerifyPeersProof(cli, serverProof, serverProofSz);
  17592. wc_SrpTerm(cli);
  17593. wc_SrpTerm(srv);
  17594. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17595. out:
  17596. if (cli)
  17597. XFREE(cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17598. if (srv)
  17599. XFREE(srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17600. if (clientProof)
  17601. XFREE(clientProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17602. if (serverProof)
  17603. XFREE(serverProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17604. #endif
  17605. return r;
  17606. }
  17607. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t srp_test(void)
  17608. {
  17609. wc_test_ret_t ret;
  17610. #ifndef NO_SHA
  17611. ret = srp_test_digest(SRP_TYPE_SHA);
  17612. if (ret != 0)
  17613. return ret;
  17614. #endif
  17615. #ifndef NO_SHA256
  17616. ret = srp_test_digest(SRP_TYPE_SHA256);
  17617. if (ret != 0)
  17618. return ret;
  17619. #endif
  17620. #ifdef WOLFSSL_SHA384
  17621. ret = srp_test_digest(SRP_TYPE_SHA384);
  17622. if (ret != 0)
  17623. return ret;
  17624. #endif
  17625. #ifdef WOLFSSL_SHA512
  17626. ret = srp_test_digest(SRP_TYPE_SHA512);
  17627. if (ret != 0)
  17628. return ret;
  17629. #endif
  17630. return ret;
  17631. }
  17632. #endif /* WOLFCRYPT_HAVE_SRP */
  17633. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  17634. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  17635. static wc_test_ret_t openssl_aes_test(void)
  17636. {
  17637. #ifdef HAVE_AES_CBC
  17638. #ifdef WOLFSSL_AES_128
  17639. {
  17640. /* EVP_CipherUpdate test */
  17641. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  17642. {
  17643. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17644. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  17645. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  17646. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  17647. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  17648. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  17649. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  17650. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  17651. };
  17652. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  17653. "0123456789abcdef "; /* align */
  17654. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  17655. "1234567890abcdef "; /* align */
  17656. byte cipher[AES_BLOCK_SIZE * 4];
  17657. byte plain [AES_BLOCK_SIZE * 4];
  17658. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17659. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  17660. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  17661. #else
  17662. EVP_CIPHER_CTX en[1];
  17663. EVP_CIPHER_CTX de[1];
  17664. #endif
  17665. int outlen ;
  17666. int total = 0;
  17667. int i;
  17668. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17669. if ((en == NULL) || (de == NULL))
  17670. return MEMORY_E;
  17671. #endif
  17672. EVP_CIPHER_CTX_init(en);
  17673. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  17674. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  17675. return WC_TEST_RET_ENC_NC;
  17676. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  17677. (byte*)cbcPlain, 9) == 0)
  17678. return WC_TEST_RET_ENC_NC;
  17679. if (outlen != 0)
  17680. return WC_TEST_RET_ENC_NC;
  17681. total += outlen;
  17682. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  17683. (byte*)&cbcPlain[9] , 9) == 0)
  17684. return WC_TEST_RET_ENC_NC;
  17685. if (outlen != 16)
  17686. return WC_TEST_RET_ENC_NC;
  17687. total += outlen;
  17688. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  17689. return WC_TEST_RET_ENC_NC;
  17690. if (outlen != 16)
  17691. return WC_TEST_RET_ENC_NC;
  17692. total += outlen;
  17693. if (total != 32)
  17694. return 3408;
  17695. total = 0;
  17696. EVP_CIPHER_CTX_init(de);
  17697. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  17698. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  17699. return WC_TEST_RET_ENC_NC;
  17700. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  17701. return WC_TEST_RET_ENC_NC;
  17702. if (outlen != 0)
  17703. return WC_TEST_RET_ENC_NC;
  17704. total += outlen;
  17705. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  17706. (byte*)&cipher[6], 12) == 0)
  17707. return WC_TEST_RET_ENC_NC;
  17708. if (outlen != 0)
  17709. total += outlen;
  17710. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  17711. (byte*)&cipher[6+12], 14) == 0)
  17712. return WC_TEST_RET_ENC_NC;
  17713. if (outlen != 16)
  17714. return WC_TEST_RET_ENC_NC;
  17715. total += outlen;
  17716. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  17717. return WC_TEST_RET_ENC_NC;
  17718. if (outlen != 2)
  17719. return WC_TEST_RET_ENC_NC;
  17720. total += outlen;
  17721. if (total != 18)
  17722. return 3427;
  17723. if (XMEMCMP(plain, cbcPlain, 18))
  17724. return WC_TEST_RET_ENC_NC;
  17725. /* test with encrypting/decrypting more than 16 bytes at once */
  17726. total = 0;
  17727. EVP_CIPHER_CTX_init(en);
  17728. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  17729. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  17730. return WC_TEST_RET_ENC_NC;
  17731. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  17732. (byte*)cbcPlain, 17) == 0)
  17733. return WC_TEST_RET_ENC_NC;
  17734. if (outlen != 16)
  17735. return WC_TEST_RET_ENC_NC;
  17736. total += outlen;
  17737. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  17738. (byte*)&cbcPlain[17] , 1) == 0)
  17739. return WC_TEST_RET_ENC_NC;
  17740. if (outlen != 0)
  17741. return WC_TEST_RET_ENC_NC;
  17742. total += outlen;
  17743. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  17744. return WC_TEST_RET_ENC_NC;
  17745. if (outlen != 16)
  17746. return WC_TEST_RET_ENC_NC;
  17747. total += outlen;
  17748. if (total != 32)
  17749. return WC_TEST_RET_ENC_NC;
  17750. total = 0;
  17751. EVP_CIPHER_CTX_init(de);
  17752. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  17753. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  17754. return WC_TEST_RET_ENC_NC;
  17755. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 17) == 0)
  17756. return WC_TEST_RET_ENC_NC;
  17757. if (outlen != 16)
  17758. return WC_TEST_RET_ENC_NC;
  17759. total += outlen;
  17760. /* final call on non block size should fail */
  17761. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  17762. return WC_TEST_RET_ENC_NC;
  17763. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  17764. (byte*)&cipher[17], 1) == 0)
  17765. return WC_TEST_RET_ENC_NC;
  17766. if (outlen != 0)
  17767. total += outlen;
  17768. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  17769. (byte*)&cipher[17+1], 14) == 0)
  17770. return WC_TEST_RET_ENC_NC;
  17771. if (outlen != 0)
  17772. return WC_TEST_RET_ENC_NC;
  17773. total += outlen;
  17774. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  17775. return WC_TEST_RET_ENC_NC;
  17776. if (outlen != 2)
  17777. return WC_TEST_RET_ENC_NC;
  17778. total += outlen;
  17779. if (total != 18)
  17780. return WC_TEST_RET_ENC_NC;
  17781. if (XMEMCMP(plain, cbcPlain, 18))
  17782. return WC_TEST_RET_ENC_NC;
  17783. /* test byte by byte decrypt */
  17784. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  17785. plain[i] = i;
  17786. }
  17787. total = 0;
  17788. EVP_CIPHER_CTX_init(en);
  17789. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  17790. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  17791. return WC_TEST_RET_ENC_NC;
  17792. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  17793. (byte*)plain, AES_BLOCK_SIZE * 3) == 0)
  17794. return WC_TEST_RET_ENC_NC;
  17795. if (outlen != AES_BLOCK_SIZE * 3)
  17796. return WC_TEST_RET_ENC_NC;
  17797. total += outlen;
  17798. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  17799. return WC_TEST_RET_ENC_NC;
  17800. if (outlen != AES_BLOCK_SIZE)
  17801. return WC_TEST_RET_ENC_NC;
  17802. total += outlen;
  17803. if (total != sizeof(plain))
  17804. return WC_TEST_RET_ENC_NC;
  17805. total = 0;
  17806. EVP_CIPHER_CTX_init(de);
  17807. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  17808. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  17809. return WC_TEST_RET_ENC_NC;
  17810. for (i = 0; i < AES_BLOCK_SIZE * 4; i++) {
  17811. if (EVP_CipherUpdate(de, (byte*)plain + total, &outlen,
  17812. (byte*)cipher + i, 1) == 0)
  17813. return WC_TEST_RET_ENC_NC;
  17814. if (outlen > 0) {
  17815. int j;
  17816. total += outlen;
  17817. for (j = 0; j < total; j++) {
  17818. if (plain[j] != j) {
  17819. return WC_TEST_RET_ENC_NC;
  17820. }
  17821. }
  17822. }
  17823. }
  17824. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  17825. return WC_TEST_RET_ENC_NC;
  17826. total += outlen;
  17827. if (total != AES_BLOCK_SIZE * 3) {
  17828. return WC_TEST_RET_ENC_NC;
  17829. }
  17830. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  17831. if (plain[i] != i) {
  17832. return WC_TEST_RET_ENC_NC;
  17833. }
  17834. }
  17835. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17836. wolfSSL_EVP_CIPHER_CTX_free(en);
  17837. wolfSSL_EVP_CIPHER_CTX_free(de);
  17838. #endif
  17839. }
  17840. /* set buffers to be exact size to catch potential over read/write */
  17841. {
  17842. /* EVP_CipherUpdate test */
  17843. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  17844. {
  17845. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17846. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  17847. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  17848. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  17849. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  17850. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  17851. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  17852. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  17853. };
  17854. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  17855. "0123456789abcdef "; /* align */
  17856. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  17857. "1234567890abcdef "; /* align */
  17858. #define EVP_TEST_BUF_SZ 18
  17859. #define EVP_TEST_BUF_PAD 32
  17860. byte cipher[EVP_TEST_BUF_SZ];
  17861. byte plain [EVP_TEST_BUF_SZ];
  17862. byte padded[EVP_TEST_BUF_PAD];
  17863. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17864. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  17865. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  17866. #else
  17867. EVP_CIPHER_CTX en[1];
  17868. EVP_CIPHER_CTX de[1];
  17869. #endif
  17870. int outlen ;
  17871. int total = 0;
  17872. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17873. if ((en == NULL) || (de == NULL))
  17874. return MEMORY_E;
  17875. #endif
  17876. EVP_CIPHER_CTX_init(en);
  17877. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  17878. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  17879. return WC_TEST_RET_ENC_NC;
  17880. if (EVP_CIPHER_CTX_set_padding(en, 0) != 1)
  17881. return WC_TEST_RET_ENC_NC;
  17882. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  17883. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  17884. return WC_TEST_RET_ENC_NC;
  17885. if (outlen != 16)
  17886. return WC_TEST_RET_ENC_NC;
  17887. total += outlen;
  17888. /* should fail here */
  17889. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) != 0)
  17890. return WC_TEST_RET_ENC_NC;
  17891. /* turn padding back on and do successful encrypt */
  17892. total = 0;
  17893. EVP_CIPHER_CTX_init(en);
  17894. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  17895. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  17896. return WC_TEST_RET_ENC_NC;
  17897. if (EVP_CIPHER_CTX_set_padding(en, 1) != 1)
  17898. return WC_TEST_RET_ENC_NC;
  17899. if (EVP_CipherUpdate(en, (byte*)padded, &outlen,
  17900. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  17901. return WC_TEST_RET_ENC_NC;
  17902. if (outlen != 16)
  17903. return WC_TEST_RET_ENC_NC;
  17904. total += outlen;
  17905. if (EVP_CipherFinal(en, (byte*)&padded[total], &outlen) == 0)
  17906. return WC_TEST_RET_ENC_NC;
  17907. total += outlen;
  17908. if (total != 32)
  17909. return WC_TEST_RET_ENC_NC;
  17910. XMEMCPY(cipher, padded, EVP_TEST_BUF_SZ);
  17911. /* test out of bounds read on buffers w/o padding during decryption */
  17912. total = 0;
  17913. EVP_CIPHER_CTX_init(de);
  17914. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  17915. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  17916. return WC_TEST_RET_ENC_NC;
  17917. if (EVP_CIPHER_CTX_set_padding(de, 0) != 1)
  17918. return WC_TEST_RET_ENC_NC;
  17919. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher,
  17920. EVP_TEST_BUF_SZ) == 0)
  17921. return WC_TEST_RET_ENC_NC;
  17922. if (outlen != 16)
  17923. return WC_TEST_RET_ENC_NC;
  17924. total += outlen;
  17925. /* should fail since not using padding */
  17926. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  17927. return WC_TEST_RET_ENC_NC;
  17928. total = 0;
  17929. EVP_CIPHER_CTX_init(de);
  17930. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  17931. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  17932. return WC_TEST_RET_ENC_NC;
  17933. if (EVP_CIPHER_CTX_set_padding(de, 1) != 1)
  17934. return WC_TEST_RET_ENC_NC;
  17935. if (EVP_CipherUpdate(de, (byte*)padded, &outlen, (byte*)padded,
  17936. EVP_TEST_BUF_PAD) == 0)
  17937. return WC_TEST_RET_ENC_NC;
  17938. if (outlen != 16)
  17939. return WC_TEST_RET_ENC_NC;
  17940. total += outlen;
  17941. if (EVP_CipherFinal(de, (byte*)&padded[total], &outlen) == 0)
  17942. return WC_TEST_RET_ENC_NC;
  17943. if (XMEMCMP(padded, cbcPlain, EVP_TEST_BUF_SZ))
  17944. return WC_TEST_RET_ENC_NC;
  17945. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17946. wolfSSL_EVP_CIPHER_CTX_free(en);
  17947. wolfSSL_EVP_CIPHER_CTX_free(de);
  17948. #endif
  17949. }
  17950. { /* evp_cipher test: EVP_aes_128_cbc */
  17951. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17952. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  17953. #else
  17954. EVP_CIPHER_CTX ctx[1];
  17955. #endif
  17956. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  17957. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  17958. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  17959. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  17960. };
  17961. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  17962. {
  17963. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  17964. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  17965. };
  17966. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  17967. "0123456789abcdef "; /* align */
  17968. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  17969. "1234567890abcdef "; /* align */
  17970. byte cipher[AES_BLOCK_SIZE * 4];
  17971. byte plain [AES_BLOCK_SIZE * 4];
  17972. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17973. if (ctx == NULL)
  17974. return MEMORY_E;
  17975. #endif
  17976. EVP_CIPHER_CTX_init(ctx);
  17977. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1) == 0)
  17978. return WC_TEST_RET_ENC_NC;
  17979. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  17980. return WC_TEST_RET_ENC_NC;
  17981. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  17982. return WC_TEST_RET_ENC_NC;
  17983. EVP_CIPHER_CTX_init(ctx);
  17984. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0) == 0)
  17985. return WC_TEST_RET_ENC_NC;
  17986. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  17987. return WC_TEST_RET_ENC_NC;
  17988. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  17989. return WC_TEST_RET_ENC_NC;
  17990. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17991. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  17992. #endif
  17993. } /* end evp_cipher test: EVP_aes_128_cbc*/
  17994. #endif /* WOLFSSL_AES_128 */
  17995. #endif /* HAVE_AES_CBC */
  17996. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  17997. { /* evp_cipher test: EVP_aes_256_ecb*/
  17998. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17999. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  18000. #else
  18001. EVP_CIPHER_CTX ctx[1];
  18002. #endif
  18003. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  18004. {
  18005. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  18006. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  18007. };
  18008. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  18009. {
  18010. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  18011. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  18012. };
  18013. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  18014. {
  18015. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  18016. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  18017. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  18018. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  18019. };
  18020. byte cipher[AES_BLOCK_SIZE * 4];
  18021. byte plain [AES_BLOCK_SIZE * 4];
  18022. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18023. if (ctx == NULL)
  18024. return MEMORY_E;
  18025. #endif
  18026. EVP_CIPHER_CTX_init(ctx);
  18027. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1) == 0)
  18028. return WC_TEST_RET_ENC_NC;
  18029. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  18030. return WC_TEST_RET_ENC_NC;
  18031. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  18032. return WC_TEST_RET_ENC_NC;
  18033. EVP_CIPHER_CTX_init(ctx);
  18034. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0) == 0)
  18035. return WC_TEST_RET_ENC_NC;
  18036. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  18037. return WC_TEST_RET_ENC_NC;
  18038. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  18039. return WC_TEST_RET_ENC_NC;
  18040. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18041. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  18042. #endif
  18043. } /* end evp_cipher test */
  18044. #endif /* HAVE_AES_ECB && WOLFSSL_AES_256 */
  18045. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  18046. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  18047. {
  18048. /* Test: AES_encrypt/decrypt/set Key */
  18049. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18050. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  18051. #ifdef HAVE_AES_DECRYPT
  18052. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  18053. #endif
  18054. #else
  18055. AES_KEY enc[1];
  18056. #ifdef HAVE_AES_DECRYPT
  18057. AES_KEY dec[1];
  18058. #endif
  18059. #endif
  18060. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  18061. {
  18062. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  18063. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  18064. };
  18065. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  18066. {
  18067. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  18068. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  18069. };
  18070. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  18071. {
  18072. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  18073. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  18074. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  18075. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  18076. };
  18077. byte plain[sizeof(msg)];
  18078. byte cipher[sizeof(msg)];
  18079. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18080. if (enc == NULL)
  18081. return MEMORY_E;
  18082. #ifdef HAVE_AES_DECRYPT
  18083. if (dec == NULL)
  18084. return MEMORY_E;
  18085. #endif
  18086. #endif
  18087. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  18088. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  18089. AES_encrypt(msg, cipher, enc);
  18090. #ifdef HAVE_AES_DECRYPT
  18091. AES_decrypt(cipher, plain, dec);
  18092. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  18093. return WC_TEST_RET_ENC_NC;
  18094. #endif /* HAVE_AES_DECRYPT */
  18095. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  18096. return WC_TEST_RET_ENC_NC;
  18097. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18098. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  18099. #ifdef HAVE_AES_DECRYPT
  18100. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  18101. #endif
  18102. #endif
  18103. }
  18104. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  18105. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  18106. #ifdef WOLFSSL_AES_COUNTER
  18107. {
  18108. byte plainBuff [64];
  18109. byte cipherBuff[64];
  18110. #ifdef WOLFSSL_AES_128
  18111. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  18112. {
  18113. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  18114. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  18115. };
  18116. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  18117. {
  18118. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  18119. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  18120. };
  18121. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  18122. {
  18123. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  18124. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  18125. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  18126. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  18127. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  18128. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  18129. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  18130. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  18131. };
  18132. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  18133. {
  18134. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  18135. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  18136. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  18137. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  18138. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  18139. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  18140. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  18141. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  18142. };
  18143. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  18144. {
  18145. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  18146. 0xc2
  18147. };
  18148. #endif
  18149. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  18150. * NIST Special Publication 800-38A */
  18151. #ifdef WOLFSSL_AES_192
  18152. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  18153. {
  18154. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  18155. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  18156. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  18157. };
  18158. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  18159. {
  18160. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  18161. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  18162. };
  18163. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  18164. {
  18165. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  18166. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  18167. };
  18168. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  18169. {
  18170. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  18171. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  18172. };
  18173. #endif /* WOLFSSL_AES_192 */
  18174. #ifdef WOLFSSL_AES_256
  18175. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  18176. * NIST Special Publication 800-38A */
  18177. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  18178. {
  18179. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  18180. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  18181. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  18182. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  18183. };
  18184. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  18185. {
  18186. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  18187. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  18188. };
  18189. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  18190. {
  18191. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  18192. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  18193. };
  18194. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  18195. {
  18196. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  18197. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  18198. };
  18199. #endif /* WOLFSSL_AES_256 */
  18200. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18201. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  18202. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  18203. #else
  18204. EVP_CIPHER_CTX en[1];
  18205. EVP_CIPHER_CTX de[1];
  18206. #endif
  18207. #ifdef WOLFSSL_AES_128
  18208. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18209. EVP_CIPHER_CTX *p_en;
  18210. EVP_CIPHER_CTX *p_de;
  18211. #endif
  18212. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18213. if ((en == NULL) || (de == NULL))
  18214. return MEMORY_E;
  18215. #endif
  18216. EVP_CIPHER_CTX_init(en);
  18217. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  18218. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  18219. return WC_TEST_RET_ENC_NC;
  18220. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  18221. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  18222. return WC_TEST_RET_ENC_NC;
  18223. EVP_CIPHER_CTX_init(de);
  18224. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  18225. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  18226. return WC_TEST_RET_ENC_NC;
  18227. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  18228. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  18229. return WC_TEST_RET_ENC_NC;
  18230. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  18231. return WC_TEST_RET_ENC_NC;
  18232. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  18233. return WC_TEST_RET_ENC_NC;
  18234. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18235. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  18236. if (p_en == NULL)
  18237. return WC_TEST_RET_ENC_ERRNO;
  18238. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  18239. if (p_de == NULL)
  18240. return WC_TEST_RET_ENC_ERRNO;
  18241. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  18242. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  18243. return WC_TEST_RET_ENC_NC;
  18244. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  18245. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  18246. return WC_TEST_RET_ENC_NC;
  18247. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  18248. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  18249. return WC_TEST_RET_ENC_NC;
  18250. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  18251. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  18252. return WC_TEST_RET_ENC_NC;
  18253. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  18254. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  18255. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  18256. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  18257. return WC_TEST_RET_ENC_NC;
  18258. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  18259. return WC_TEST_RET_ENC_NC;
  18260. EVP_CIPHER_CTX_init(en);
  18261. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  18262. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  18263. return WC_TEST_RET_ENC_NC;
  18264. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  18265. return WC_TEST_RET_ENC_NC;
  18266. EVP_CIPHER_CTX_init(de);
  18267. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  18268. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  18269. return WC_TEST_RET_ENC_NC;
  18270. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  18271. return WC_TEST_RET_ENC_NC;
  18272. if (XMEMCMP(plainBuff, ctrPlain, 9))
  18273. return WC_TEST_RET_ENC_NC;
  18274. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  18275. return WC_TEST_RET_ENC_NC;
  18276. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  18277. return WC_TEST_RET_ENC_NC;
  18278. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  18279. return WC_TEST_RET_ENC_NC;
  18280. if (XMEMCMP(plainBuff, ctrPlain, 9))
  18281. return WC_TEST_RET_ENC_NC;
  18282. if (XMEMCMP(cipherBuff, oddCipher, 9))
  18283. return WC_TEST_RET_ENC_NC;
  18284. #endif /* WOLFSSL_AES_128 */
  18285. #ifdef WOLFSSL_AES_192
  18286. EVP_CIPHER_CTX_init(en);
  18287. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  18288. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  18289. return WC_TEST_RET_ENC_NC;
  18290. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  18291. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  18292. return WC_TEST_RET_ENC_NC;
  18293. EVP_CIPHER_CTX_init(de);
  18294. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  18295. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  18296. return WC_TEST_RET_ENC_NC;
  18297. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  18298. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  18299. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  18300. return WC_TEST_RET_ENC_NC;
  18301. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  18302. return WC_TEST_RET_ENC_NC;
  18303. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  18304. return WC_TEST_RET_ENC_NC;
  18305. #endif /* WOLFSSL_AES_192 */
  18306. #ifdef WOLFSSL_AES_256
  18307. EVP_CIPHER_CTX_init(en);
  18308. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  18309. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  18310. return WC_TEST_RET_ENC_NC;
  18311. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  18312. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  18313. return WC_TEST_RET_ENC_NC;
  18314. EVP_CIPHER_CTX_init(de);
  18315. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  18316. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  18317. return WC_TEST_RET_ENC_NC;
  18318. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  18319. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  18320. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  18321. return WC_TEST_RET_ENC_NC;
  18322. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  18323. return WC_TEST_RET_ENC_NC;
  18324. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  18325. return WC_TEST_RET_ENC_NC;
  18326. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18327. wolfSSL_EVP_CIPHER_CTX_free(en);
  18328. wolfSSL_EVP_CIPHER_CTX_free(de);
  18329. #endif
  18330. #endif /* WOLFSSL_AES_256 */
  18331. }
  18332. #endif /* HAVE_AES_COUNTER */
  18333. #if defined(WOLFSSL_AES_CFB) && defined(WOLFSSL_AES_128)
  18334. {
  18335. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18336. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  18337. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  18338. #else
  18339. AES_KEY enc[1];
  18340. AES_KEY dec[1];
  18341. #endif
  18342. WOLFSSL_SMALL_STACK_STATIC const byte setIv[] = {
  18343. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  18344. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  18345. };
  18346. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  18347. {
  18348. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  18349. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  18350. };
  18351. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  18352. {
  18353. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  18354. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  18355. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  18356. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b
  18357. };
  18358. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  18359. {
  18360. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  18361. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  18362. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  18363. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51
  18364. };
  18365. byte cipher[AES_BLOCK_SIZE * 2];
  18366. byte iv[AES_BLOCK_SIZE]; /* iv buffer is updeated by API */
  18367. int num = 0;
  18368. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18369. if ((enc == NULL) || (dec == NULL))
  18370. return MEMORY_E;
  18371. #endif
  18372. XMEMCPY(iv, setIv, sizeof(setIv));
  18373. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, enc);
  18374. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, dec);
  18375. wolfSSL_AES_cfb128_encrypt(msg, cipher, AES_BLOCK_SIZE - 1, enc, iv,
  18376. &num, AES_ENCRYPT);
  18377. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE - 1))
  18378. return WC_TEST_RET_ENC_NC;
  18379. if (num != 15) /* should have used 15 of the 16 bytes */
  18380. return WC_TEST_RET_ENC_NC;
  18381. wolfSSL_AES_cfb128_encrypt(msg + AES_BLOCK_SIZE - 1,
  18382. cipher + AES_BLOCK_SIZE - 1, AES_BLOCK_SIZE + 1, enc, iv,
  18383. &num, AES_ENCRYPT);
  18384. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  18385. return WC_TEST_RET_ENC_NC;
  18386. if (num != 0)
  18387. return WC_TEST_RET_ENC_NC;
  18388. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18389. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  18390. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  18391. #endif
  18392. }
  18393. #endif /* WOLFSSL_AES_CFB && WOLFSSL_AES_128 */
  18394. return 0;
  18395. }
  18396. #endif /* !defined(NO_AES) && !defined(WOLFCRYPT_ONLY) */
  18397. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_test(void)
  18398. {
  18399. wc_test_ret_t ret;
  18400. EVP_MD_CTX md_ctx;
  18401. testVector a, b, c, d, e, f;
  18402. byte hash[WC_SHA256_DIGEST_SIZE*2]; /* max size */
  18403. a.inLen = 0;
  18404. b.inLen = c.inLen = d.inLen = e.inLen = f.inLen = a.inLen;
  18405. (void)a;
  18406. (void)b;
  18407. (void)c;
  18408. (void)d;
  18409. (void)e;
  18410. (void)f;
  18411. /* test malloc / free , 10 is an arbitrary amount of memory chosen */
  18412. {
  18413. byte* p;
  18414. p = (byte*)CRYPTO_malloc(10, "", 0);
  18415. if (p == NULL) {
  18416. return WC_TEST_RET_ENC_NC;
  18417. }
  18418. XMEMSET(p, 0, 10);
  18419. CRYPTO_free(p, "", 0);
  18420. }
  18421. #ifndef NO_MD5
  18422. a.input = "1234567890123456789012345678901234567890123456789012345678"
  18423. "9012345678901234567890";
  18424. a.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  18425. "\x7a";
  18426. a.inLen = XSTRLEN(a.input);
  18427. a.outLen = WC_MD5_DIGEST_SIZE;
  18428. EVP_MD_CTX_init(&md_ctx);
  18429. ret = EVP_DigestInit(&md_ctx, EVP_md5());
  18430. if (ret == WOLFSSL_SUCCESS) {
  18431. ret = EVP_DigestUpdate(&md_ctx, a.input, (unsigned long)a.inLen);
  18432. }
  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)
  18438. return WC_TEST_RET_ENC_NC;
  18439. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0)
  18440. return WC_TEST_RET_ENC_NC;
  18441. #endif /* NO_MD5 */
  18442. #ifndef NO_SHA
  18443. b.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  18444. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  18445. "aaaaaaaaaa";
  18446. b.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  18447. "\x53\x99\x5E\x26\xA0";
  18448. b.inLen = XSTRLEN(b.input);
  18449. b.outLen = WC_SHA_DIGEST_SIZE;
  18450. EVP_MD_CTX_init(&md_ctx);
  18451. ret = EVP_DigestInit(&md_ctx, EVP_sha1());
  18452. if (ret == WOLFSSL_SUCCESS) {
  18453. ret = EVP_DigestUpdate(&md_ctx, b.input, (unsigned long)b.inLen);
  18454. if (ret == WOLFSSL_SUCCESS)
  18455. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  18456. }
  18457. EVP_MD_CTX_cleanup(&md_ctx);
  18458. if (ret != WOLFSSL_SUCCESS)
  18459. return WC_TEST_RET_ENC_NC;
  18460. if (XMEMCMP(hash, b.output, b.outLen) != 0)
  18461. return WC_TEST_RET_ENC_NC;
  18462. #endif /* NO_SHA */
  18463. #ifdef WOLFSSL_SHA224
  18464. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  18465. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  18466. e.output = "\xc9\x7c\xa9\xa5\x59\x85\x0c\xe9\x7a\x04\xa9\x6d\xef\x6d\x99"
  18467. "\xa9\xe0\xe0\xe2\xab\x14\xe6\xb8\xdf\x26\x5f\xc0\xb3";
  18468. e.inLen = XSTRLEN(e.input);
  18469. e.outLen = WC_SHA224_DIGEST_SIZE;
  18470. EVP_MD_CTX_init(&md_ctx);
  18471. ret = EVP_DigestInit(&md_ctx, EVP_sha224());
  18472. if (ret == WOLFSSL_SUCCESS) {
  18473. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.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, e.output, e.outLen) != 0) {
  18479. return WC_TEST_RET_ENC_NC;
  18480. }
  18481. #endif /* WOLFSSL_SHA224 */
  18482. #ifndef NO_SHA256
  18483. d.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  18484. d.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  18485. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  18486. "\x06\xC1";
  18487. d.inLen = XSTRLEN(d.input);
  18488. d.outLen = WC_SHA256_DIGEST_SIZE;
  18489. EVP_MD_CTX_init(&md_ctx);
  18490. ret = EVP_DigestInit(&md_ctx, EVP_sha256());
  18491. if (ret == WOLFSSL_SUCCESS) {
  18492. ret = EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen);
  18493. if (ret == WOLFSSL_SUCCESS)
  18494. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  18495. }
  18496. EVP_MD_CTX_cleanup(&md_ctx);
  18497. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, d.output, d.outLen) != 0) {
  18498. return WC_TEST_RET_ENC_NC;
  18499. }
  18500. #endif /* !NO_SHA256 */
  18501. #ifdef WOLFSSL_SHA384
  18502. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  18503. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  18504. e.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  18505. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  18506. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  18507. "\x74\x60\x39";
  18508. e.inLen = XSTRLEN(e.input);
  18509. e.outLen = WC_SHA384_DIGEST_SIZE;
  18510. EVP_MD_CTX_init(&md_ctx);
  18511. ret = EVP_DigestInit(&md_ctx, EVP_sha384());
  18512. if (ret == WOLFSSL_SUCCESS) {
  18513. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  18514. if (ret == WOLFSSL_SUCCESS)
  18515. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  18516. }
  18517. EVP_MD_CTX_cleanup(&md_ctx);
  18518. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, e.output, e.outLen) != 0) {
  18519. return WC_TEST_RET_ENC_NC;
  18520. }
  18521. #endif /* WOLFSSL_SHA384 */
  18522. #ifdef WOLFSSL_SHA512
  18523. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  18524. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  18525. f.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  18526. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  18527. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  18528. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  18529. "\x87\x4b\xe9\x09";
  18530. f.inLen = XSTRLEN(f.input);
  18531. f.outLen = WC_SHA512_DIGEST_SIZE;
  18532. EVP_MD_CTX_init(&md_ctx);
  18533. ret = EVP_DigestInit(&md_ctx, EVP_sha512());
  18534. if (ret == WOLFSSL_SUCCESS) {
  18535. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.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, f.output, f.outLen) != 0) {
  18541. return WC_TEST_RET_ENC_NC;
  18542. }
  18543. #if !defined(WOLFSSL_NOSHA512_224) && \
  18544. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  18545. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  18546. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  18547. f.output = "\x23\xfe\xc5\xbb\x94\xd6\x0b\x23\x30\x81\x92\x64\x0b\x0c\x45"
  18548. "\x33\x35\xd6\x64\x73\x4f\xe4\x0e\x72\x68\x67\x4a\xf9";
  18549. f.inLen = XSTRLEN(f.input);
  18550. f.outLen = WC_SHA512_224_DIGEST_SIZE;
  18551. EVP_MD_CTX_init(&md_ctx);
  18552. ret = EVP_DigestInit(&md_ctx, EVP_sha512_224());
  18553. if (ret == WOLFSSL_SUCCESS) {
  18554. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  18555. if (ret == WOLFSSL_SUCCESS)
  18556. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  18557. }
  18558. EVP_MD_CTX_cleanup(&md_ctx);
  18559. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, f.output, f.outLen) != 0) {
  18560. return WC_TEST_RET_ENC_NC;
  18561. }
  18562. #endif /* !WOLFSSL_NOSHA512_224 && !FIPS ... */
  18563. #if !defined(WOLFSSL_NOSHA512_256) && \
  18564. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  18565. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  18566. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  18567. f.output = "\x39\x28\xe1\x84\xfb\x86\x90\xf8\x40\xda\x39\x88\x12\x1d\x31"
  18568. "\xbe\x65\xcb\x9d\x3e\xf8\x3e\xe6\x14\x6f\xea\xc8\x61\xe1\x9b"
  18569. "\x56\x3a";
  18570. f.inLen = XSTRLEN(f.input);
  18571. f.outLen = WC_SHA512_256_DIGEST_SIZE;
  18572. EVP_MD_CTX_init(&md_ctx);
  18573. ret = EVP_DigestInit(&md_ctx, EVP_sha512_256());
  18574. if (ret == WOLFSSL_SUCCESS) {
  18575. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  18576. if (ret == WOLFSSL_SUCCESS)
  18577. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  18578. }
  18579. EVP_MD_CTX_cleanup(&md_ctx);
  18580. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, f.output, f.outLen) != 0) {
  18581. return WC_TEST_RET_ENC_NC;
  18582. }
  18583. #endif /* !WOLFSSL_NOSHA512_224 && !FIPS ... */
  18584. #endif /* WOLFSSL_SHA512 */
  18585. #ifdef WOLFSSL_SHA3
  18586. #ifndef WOLFSSL_NOSHA3_224
  18587. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  18588. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  18589. e.output = "\x54\x3e\x68\x68\xe1\x66\x6c\x1a\x64\x36\x30\xdf\x77\x36\x7a"
  18590. "\xe5\xa6\x2a\x85\x07\x0a\x51\xc1\x4c\xbf\x66\x5c\xbc";
  18591. e.inLen = XSTRLEN(e.input);
  18592. e.outLen = WC_SHA3_224_DIGEST_SIZE;
  18593. EVP_MD_CTX_init(&md_ctx);
  18594. ret = EVP_DigestInit(&md_ctx, EVP_sha3_224());
  18595. if (ret == WOLFSSL_SUCCESS) {
  18596. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  18597. if (ret == WOLFSSL_SUCCESS)
  18598. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  18599. }
  18600. EVP_MD_CTX_cleanup(&md_ctx);
  18601. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, e.output, e.outLen) != 0) {
  18602. return WC_TEST_RET_ENC_NC;
  18603. }
  18604. #endif /* WOLFSSL_NOSHA3_224 */
  18605. #ifndef WOLFSSL_NOSHA3_256
  18606. d.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  18607. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  18608. d.output = "\x91\x6f\x60\x61\xfe\x87\x97\x41\xca\x64\x69\xb4\x39\x71\xdf"
  18609. "\xdb\x28\xb1\xa3\x2d\xc3\x6c\xb3\x25\x4e\x81\x2b\xe2\x7a\xad"
  18610. "\x1d\x18";
  18611. d.inLen = XSTRLEN(d.input);
  18612. d.outLen = WC_SHA3_256_DIGEST_SIZE;
  18613. EVP_MD_CTX_init(&md_ctx);
  18614. ret = EVP_DigestInit(&md_ctx, EVP_sha3_256());
  18615. if (ret == WOLFSSL_SUCCESS) {
  18616. ret = EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen);
  18617. if (ret == WOLFSSL_SUCCESS)
  18618. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  18619. }
  18620. EVP_MD_CTX_cleanup(&md_ctx);
  18621. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, d.output, d.outLen) != 0) {
  18622. return WC_TEST_RET_ENC_NC;
  18623. }
  18624. #endif /* WOLFSSL_NOSHA3_256 */
  18625. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  18626. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  18627. e.output = "\x79\x40\x7d\x3b\x59\x16\xb5\x9c\x3e\x30\xb0\x98\x22\x97\x47"
  18628. "\x91\xc3\x13\xfb\x9e\xcc\x84\x9e\x40\x6f\x23\x59\x2d\x04\xf6"
  18629. "\x25\xdc\x8c\x70\x9b\x98\xb4\x3b\x38\x52\xb3\x37\x21\x61\x79"
  18630. "\xaa\x7f\xc7";
  18631. e.inLen = XSTRLEN(e.input);
  18632. e.outLen = WC_SHA3_384_DIGEST_SIZE;
  18633. EVP_MD_CTX_init(&md_ctx);
  18634. ret = EVP_DigestInit(&md_ctx, EVP_sha3_384());
  18635. if (ret == WOLFSSL_SUCCESS) {
  18636. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  18637. if (ret == WOLFSSL_SUCCESS)
  18638. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  18639. }
  18640. EVP_MD_CTX_cleanup(&md_ctx);
  18641. if (ret != WOLFSSL_SUCCESS || XMEMCMP(hash, e.output, e.outLen) != 0) {
  18642. return WC_TEST_RET_ENC_NC;
  18643. }
  18644. #ifndef WOLFSSL_NOSHA3_512
  18645. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  18646. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  18647. f.output = "\xaf\xeb\xb2\xef\x54\x2e\x65\x79\xc5\x0c\xad\x06\xd2\xe5\x78"
  18648. "\xf9\xf8\xdd\x68\x81\xd7\xdc\x82\x4d\x26\x36\x0f\xee\xbf\x18"
  18649. "\xa4\xfa\x73\xe3\x26\x11\x22\x94\x8e\xfc\xfd\x49\x2e\x74\xe8"
  18650. "\x2e\x21\x89\xed\x0f\xb4\x40\xd1\x87\xf3\x82\x27\x0c\xb4\x55"
  18651. "\xf2\x1d\xd1\x85";
  18652. f.inLen = XSTRLEN(f.input);
  18653. f.outLen = WC_SHA3_512_DIGEST_SIZE;
  18654. EVP_MD_CTX_init(&md_ctx);
  18655. ret = EVP_DigestInit(&md_ctx, EVP_sha3_512());
  18656. if (ret == WOLFSSL_SUCCESS) {
  18657. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  18658. if (ret == WOLFSSL_SUCCESS)
  18659. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  18660. }
  18661. EVP_MD_CTX_cleanup(&md_ctx);
  18662. if (ret != WOLFSSL_SUCCESS ||
  18663. XMEMCMP(hash, f.output, f.outLen) != 0) {
  18664. return WC_TEST_RET_ENC_NC;
  18665. }
  18666. #endif /* WOLFSSL_NOSHA3_512 */
  18667. #endif /* WOLFSSL_SHA3 */
  18668. #ifndef WC_NO_RNG
  18669. if (RAND_bytes(hash, sizeof(hash)) != WOLFSSL_SUCCESS)
  18670. return WC_TEST_RET_ENC_NC;
  18671. #endif
  18672. #ifndef NO_MD5
  18673. c.input = "what do ya want for nothing?";
  18674. c.output = "\x55\x78\xe8\x48\x4b\xcc\x93\x80\x93\xec\x53\xaf\x22\xd6\x14"
  18675. "\x76";
  18676. c.inLen = XSTRLEN(c.input);
  18677. c.outLen = WC_MD5_DIGEST_SIZE;
  18678. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)
  18679. /* Expect failure with MD5 + HMAC when using FIPS 140-3. */
  18680. if (HMAC(EVP_md5(), "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen,
  18681. hash, 0) != NULL)
  18682. #else
  18683. if (HMAC(EVP_md5(), "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen,
  18684. hash, 0) == NULL ||
  18685. XMEMCMP(hash, c.output, c.outLen) != 0)
  18686. #endif
  18687. {
  18688. return WC_TEST_RET_ENC_NC;
  18689. }
  18690. #endif /* NO_MD5 */
  18691. #ifndef NO_DES3
  18692. { /* des test */
  18693. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  18694. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  18695. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  18696. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  18697. };
  18698. byte plain[24];
  18699. byte cipher[24];
  18700. const_DES_cblock key = {
  18701. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  18702. };
  18703. DES_cblock iv = {
  18704. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  18705. };
  18706. DES_key_schedule sched;
  18707. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  18708. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  18709. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  18710. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  18711. };
  18712. DES_key_sched(&key, &sched);
  18713. DES_cbc_encrypt(vector, cipher, sizeof(vector), &sched, &iv, DES_ENCRYPT);
  18714. DES_cbc_encrypt(cipher, plain, sizeof(vector), &sched, &iv, DES_DECRYPT);
  18715. if (XMEMCMP(plain, vector, sizeof(vector)) != 0)
  18716. return WC_TEST_RET_ENC_NC;
  18717. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  18718. return WC_TEST_RET_ENC_NC;
  18719. /* test changing iv */
  18720. DES_ncbc_encrypt(vector, cipher, 8, &sched, &iv, DES_ENCRYPT);
  18721. DES_ncbc_encrypt(vector + 8, cipher + 8, 16, &sched, &iv, DES_ENCRYPT);
  18722. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  18723. return WC_TEST_RET_ENC_NC;
  18724. } /* end des test */
  18725. #endif /* NO_DES3 */
  18726. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  18727. if ((ret = openssl_aes_test()) != 0) {
  18728. return ret;
  18729. }
  18730. #if defined(WOLFSSL_AES_128) && defined(HAVE_AES_CBC)
  18731. { /* evp_cipher test: EVP_aes_128_cbc */
  18732. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18733. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  18734. #else
  18735. EVP_CIPHER_CTX ctx[1];
  18736. #endif
  18737. int idx, cipherSz, plainSz;
  18738. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  18739. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  18740. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  18741. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  18742. };
  18743. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  18744. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  18745. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  18746. 0x3b,0x5d,0x41,0x97,0x94,0x25,0xa4,0xb4,
  18747. 0xae,0x7b,0x34,0xd0,0x3f,0x0c,0xbc,0x06
  18748. };
  18749. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  18750. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  18751. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  18752. 0x7d,0x37,0x7b,0x0b,0x44,0xaa,0xb5,0xf0,
  18753. 0x5f,0x34,0xb4,0xde,0xb5,0xbd,0x2a,0xbb
  18754. };
  18755. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  18756. "0123456789abcdef "; /* align */
  18757. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  18758. "1234567890abcdef "; /* align */
  18759. byte cipher[AES_BLOCK_SIZE * 4];
  18760. byte plain [AES_BLOCK_SIZE * 4];
  18761. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18762. if (ctx == NULL)
  18763. return MEMORY_E;
  18764. #endif
  18765. cipherSz = 0;
  18766. EVP_CIPHER_CTX_init(ctx);
  18767. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  18768. if (ret == WOLFSSL_SUCCESS) {
  18769. ret = EVP_CipherUpdate(ctx, cipher, &idx, (byte*)msg, sizeof(msg));
  18770. if (ret == WOLFSSL_SUCCESS)
  18771. cipherSz += idx;
  18772. }
  18773. if (ret == WOLFSSL_SUCCESS) {
  18774. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  18775. if (ret == WOLFSSL_SUCCESS)
  18776. cipherSz += idx;
  18777. }
  18778. EVP_CIPHER_CTX_cleanup(ctx);
  18779. if (ret != WOLFSSL_SUCCESS)
  18780. return WC_TEST_RET_ENC_NC;
  18781. if (cipherSz != (int)sizeof(verify) || XMEMCMP(cipher, verify, cipherSz))
  18782. return WC_TEST_RET_ENC_NC;
  18783. /* check partial decrypt (not enough padding for full block) */
  18784. plainSz = 0;
  18785. EVP_CIPHER_CTX_init(ctx);
  18786. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  18787. if (ret == WOLFSSL_SUCCESS) {
  18788. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, 1);
  18789. if (ret == WOLFSSL_SUCCESS)
  18790. plainSz += idx;
  18791. }
  18792. if (ret == WOLFSSL_SUCCESS) {
  18793. /* this test should fail... not enough padding for full block */
  18794. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  18795. if (plainSz == 0 && ret != WOLFSSL_SUCCESS)
  18796. ret = WOLFSSL_SUCCESS;
  18797. else
  18798. ret = WC_TEST_RET_ENC_NC;
  18799. }
  18800. else
  18801. ret = WC_TEST_RET_ENC_NC;
  18802. EVP_CIPHER_CTX_cleanup(ctx);
  18803. if (ret != WOLFSSL_SUCCESS)
  18804. return ret;
  18805. plainSz = 0;
  18806. EVP_CIPHER_CTX_init(ctx);
  18807. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  18808. if (ret == WOLFSSL_SUCCESS) {
  18809. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, cipherSz);
  18810. if (ret == WOLFSSL_SUCCESS)
  18811. plainSz += idx;
  18812. }
  18813. if (ret == WOLFSSL_SUCCESS) {
  18814. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  18815. if (ret == WOLFSSL_SUCCESS)
  18816. plainSz += idx;
  18817. }
  18818. EVP_CIPHER_CTX_cleanup(ctx);
  18819. if (ret != WOLFSSL_SUCCESS)
  18820. return WC_TEST_RET_ENC_NC;
  18821. if (plainSz != (int)sizeof(msg) || XMEMCMP(plain, msg, sizeof(msg)))
  18822. return WC_TEST_RET_ENC_NC;
  18823. cipherSz = 0;
  18824. EVP_CIPHER_CTX_init(ctx);
  18825. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  18826. if (ret == WOLFSSL_SUCCESS) {
  18827. ret = EVP_CipherUpdate(ctx, cipher, &idx, msg, AES_BLOCK_SIZE);
  18828. if (ret == WOLFSSL_SUCCESS)
  18829. cipherSz += idx;
  18830. }
  18831. if (ret == WOLFSSL_SUCCESS) {
  18832. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  18833. if (ret == WOLFSSL_SUCCESS)
  18834. cipherSz += idx;
  18835. }
  18836. EVP_CIPHER_CTX_cleanup(ctx);
  18837. if (ret != WOLFSSL_SUCCESS)
  18838. return WC_TEST_RET_ENC_NC;
  18839. if (cipherSz != (int)sizeof(verify2) || XMEMCMP(cipher, verify2, cipherSz))
  18840. return WC_TEST_RET_ENC_NC;
  18841. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18842. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  18843. #endif
  18844. } /* end evp_cipher test: EVP_aes_128_cbc*/
  18845. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  18846. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  18847. { /* evp_cipher test: EVP_aes_256_ecb*/
  18848. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18849. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  18850. #else
  18851. EVP_CIPHER_CTX ctx[1];
  18852. #endif
  18853. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  18854. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  18855. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  18856. };
  18857. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  18858. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  18859. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  18860. };
  18861. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  18862. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  18863. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  18864. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  18865. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  18866. };
  18867. byte cipher[AES_BLOCK_SIZE * 4];
  18868. byte plain [AES_BLOCK_SIZE * 4];
  18869. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18870. if (ctx == NULL)
  18871. return MEMORY_E;
  18872. #endif
  18873. EVP_CIPHER_CTX_init(ctx);
  18874. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1);
  18875. if (ret == WOLFSSL_SUCCESS)
  18876. ret = EVP_Cipher(ctx, cipher, (byte*)msg, 16);
  18877. EVP_CIPHER_CTX_cleanup(ctx);
  18878. if (ret != 16)
  18879. return WC_TEST_RET_ENC_NC;
  18880. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  18881. return WC_TEST_RET_ENC_NC;
  18882. EVP_CIPHER_CTX_init(ctx);
  18883. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0);
  18884. if (ret == WOLFSSL_SUCCESS)
  18885. ret = EVP_Cipher(ctx, plain, cipher, 16);
  18886. EVP_CIPHER_CTX_cleanup(ctx);
  18887. if (ret != 16)
  18888. return WC_TEST_RET_ENC_NC;
  18889. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  18890. return WC_TEST_RET_ENC_NC;
  18891. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18892. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  18893. #endif
  18894. } /* end evp_cipher test */
  18895. #endif /* HAVE_AES_ECB && WOLFSSL_AES_128 */
  18896. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  18897. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  18898. {
  18899. /* Test: AES_encrypt/decrypt/set Key */
  18900. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18901. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  18902. #ifdef HAVE_AES_DECRYPT
  18903. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  18904. #endif
  18905. #else
  18906. AES_KEY enc[1];
  18907. #ifdef HAVE_AES_DECRYPT
  18908. AES_KEY dec[1];
  18909. #endif
  18910. #endif
  18911. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  18912. {
  18913. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  18914. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  18915. };
  18916. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  18917. {
  18918. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  18919. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  18920. };
  18921. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  18922. {
  18923. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  18924. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  18925. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  18926. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  18927. };
  18928. byte plain[sizeof(msg)];
  18929. byte cipher[sizeof(msg)];
  18930. printf("openSSL extra test\n") ;
  18931. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18932. if (enc == NULL)
  18933. return MEMORY_E;
  18934. #ifdef HAVE_AES_DECRYPT
  18935. if (dec == NULL)
  18936. return MEMORY_E;
  18937. #endif
  18938. #endif
  18939. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  18940. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  18941. AES_encrypt(msg, cipher, enc);
  18942. #ifdef HAVE_AES_DECRYPT
  18943. AES_decrypt(cipher, plain, dec);
  18944. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  18945. return WC_TEST_RET_ENC_NC;
  18946. #endif /* HAVE_AES_DECRYPT */
  18947. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  18948. return WC_TEST_RET_ENC_NC;
  18949. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18950. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  18951. #ifdef HAVE_AES_DECRYPT
  18952. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  18953. #endif
  18954. #endif
  18955. }
  18956. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  18957. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  18958. #ifdef WOLFSSL_AES_COUNTER
  18959. {
  18960. byte plainBuff [64];
  18961. byte cipherBuff[64];
  18962. #ifdef WOLFSSL_AES_128
  18963. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  18964. {
  18965. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  18966. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  18967. };
  18968. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  18969. {
  18970. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  18971. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  18972. };
  18973. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  18974. {
  18975. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  18976. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  18977. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  18978. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  18979. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  18980. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  18981. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  18982. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  18983. };
  18984. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  18985. {
  18986. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  18987. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  18988. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  18989. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  18990. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  18991. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  18992. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  18993. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  18994. };
  18995. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  18996. {
  18997. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  18998. 0xc2
  18999. };
  19000. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19001. EVP_CIPHER_CTX *p_en;
  19002. EVP_CIPHER_CTX *p_de;
  19003. #endif
  19004. #endif /* WOLFSSL_AES_128 */
  19005. #ifdef WOLFSSL_AES_192
  19006. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  19007. * NIST Special Publication 800-38A */
  19008. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  19009. {
  19010. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  19011. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  19012. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  19013. };
  19014. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  19015. {
  19016. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  19017. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  19018. };
  19019. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  19020. {
  19021. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  19022. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  19023. };
  19024. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  19025. {
  19026. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  19027. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  19028. };
  19029. #endif /* WOLFSSL_AES_192 */
  19030. #ifdef WOLFSSL_AES_256
  19031. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  19032. * NIST Special Publication 800-38A */
  19033. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  19034. {
  19035. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  19036. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  19037. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  19038. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  19039. };
  19040. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  19041. {
  19042. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  19043. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  19044. };
  19045. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  19046. {
  19047. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  19048. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  19049. };
  19050. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  19051. {
  19052. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  19053. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  19054. };
  19055. #endif /* WOLFSSL_AES_256 */
  19056. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19057. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  19058. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  19059. if ((en == NULL) || (de == NULL))
  19060. return MEMORY_E;
  19061. #else
  19062. EVP_CIPHER_CTX en[1];
  19063. EVP_CIPHER_CTX de[1];
  19064. #endif
  19065. #ifdef WOLFSSL_AES_128
  19066. EVP_CIPHER_CTX_init(en);
  19067. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  19068. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  19069. return WC_TEST_RET_ENC_NC;
  19070. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  19071. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  19072. return WC_TEST_RET_ENC_NC;
  19073. EVP_CIPHER_CTX_init(de);
  19074. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  19075. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  19076. return WC_TEST_RET_ENC_NC;
  19077. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  19078. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  19079. return WC_TEST_RET_ENC_NC;
  19080. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  19081. return WC_TEST_RET_ENC_NC;
  19082. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  19083. return WC_TEST_RET_ENC_NC;
  19084. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19085. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  19086. if (p_en == NULL)
  19087. return WC_TEST_RET_ENC_ERRNO;
  19088. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  19089. if (p_de == NULL)
  19090. return WC_TEST_RET_ENC_ERRNO;
  19091. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  19092. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  19093. return WC_TEST_RET_ENC_NC;
  19094. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  19095. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  19096. return WC_TEST_RET_ENC_NC;
  19097. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  19098. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  19099. return WC_TEST_RET_ENC_NC;
  19100. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  19101. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  19102. return WC_TEST_RET_ENC_NC;
  19103. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  19104. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  19105. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  19106. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  19107. return WC_TEST_RET_ENC_NC;
  19108. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  19109. return WC_TEST_RET_ENC_NC;
  19110. EVP_CIPHER_CTX_init(en);
  19111. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  19112. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  19113. return WC_TEST_RET_ENC_NC;
  19114. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  19115. return WC_TEST_RET_ENC_NC;
  19116. EVP_CIPHER_CTX_init(de);
  19117. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  19118. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  19119. return WC_TEST_RET_ENC_NC;
  19120. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  19121. return WC_TEST_RET_ENC_NC;
  19122. if (XMEMCMP(plainBuff, ctrPlain, 9))
  19123. return WC_TEST_RET_ENC_NC;
  19124. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  19125. return WC_TEST_RET_ENC_NC;
  19126. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  19127. return WC_TEST_RET_ENC_NC;
  19128. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  19129. return WC_TEST_RET_ENC_NC;
  19130. if (XMEMCMP(plainBuff, ctrPlain, 9))
  19131. return WC_TEST_RET_ENC_NC;
  19132. if (XMEMCMP(cipherBuff, oddCipher, 9))
  19133. return WC_TEST_RET_ENC_NC;
  19134. #endif /* WOLFSSL_AES_128 */
  19135. #ifdef WOLFSSL_AES_192
  19136. EVP_CIPHER_CTX_init(en);
  19137. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  19138. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  19139. return WC_TEST_RET_ENC_NC;
  19140. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  19141. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  19142. return WC_TEST_RET_ENC_NC;
  19143. EVP_CIPHER_CTX_init(de);
  19144. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  19145. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  19146. return WC_TEST_RET_ENC_NC;
  19147. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  19148. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  19149. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  19150. return WC_TEST_RET_ENC_NC;
  19151. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  19152. return WC_TEST_RET_ENC_NC;
  19153. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  19154. return WC_TEST_RET_ENC_NC;
  19155. #endif /* WOLFSSL_AES_192 */
  19156. #ifdef WOLFSSL_AES_256
  19157. EVP_CIPHER_CTX_init(en);
  19158. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  19159. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  19160. return WC_TEST_RET_ENC_NC;
  19161. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  19162. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  19163. return WC_TEST_RET_ENC_NC;
  19164. EVP_CIPHER_CTX_init(de);
  19165. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  19166. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  19167. return WC_TEST_RET_ENC_NC;
  19168. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  19169. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  19170. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  19171. return WC_TEST_RET_ENC_NC;
  19172. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  19173. return WC_TEST_RET_ENC_NC;
  19174. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  19175. return WC_TEST_RET_ENC_NC;
  19176. #endif /* WOLFSSL_AES_256 */
  19177. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19178. wolfSSL_EVP_CIPHER_CTX_free(en);
  19179. wolfSSL_EVP_CIPHER_CTX_free(de);
  19180. #endif
  19181. }
  19182. #endif /* HAVE_AES_COUNTER */
  19183. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  19184. {
  19185. /* EVP_CipherUpdate test */
  19186. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  19187. {
  19188. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  19189. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  19190. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  19191. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  19192. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  19193. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  19194. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  19195. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  19196. };
  19197. byte key[] = "0123456789abcdef "; /* align */
  19198. byte iv[] = "1234567890abcdef "; /* align */
  19199. byte cipher[AES_BLOCK_SIZE * 4];
  19200. byte plain [AES_BLOCK_SIZE * 4];
  19201. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19202. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  19203. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  19204. #else
  19205. EVP_CIPHER_CTX en[1];
  19206. EVP_CIPHER_CTX de[1];
  19207. #endif
  19208. int outlen ;
  19209. int total = 0;
  19210. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19211. if ((en == NULL) || (de == NULL))
  19212. return MEMORY_E;
  19213. #endif
  19214. EVP_CIPHER_CTX_init(en);
  19215. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  19216. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  19217. return WC_TEST_RET_ENC_NC;
  19218. /* openSSL compatibility, if(inlen == 0)return 1; */
  19219. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  19220. (byte*)cbcPlain, 0) != 1)
  19221. return WC_TEST_RET_ENC_NC;
  19222. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  19223. return WC_TEST_RET_ENC_NC;
  19224. EVP_CIPHER_CTX_init(en);
  19225. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  19226. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  19227. return WC_TEST_RET_ENC_NC;
  19228. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  19229. (byte*)cbcPlain, 9) == 0)
  19230. return WC_TEST_RET_ENC_NC;
  19231. if(outlen != 0)
  19232. return WC_TEST_RET_ENC_NC;
  19233. total += outlen;
  19234. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  19235. (byte*)&cbcPlain[9] , 9) == 0)
  19236. return WC_TEST_RET_ENC_NC;
  19237. if(outlen != 16)
  19238. return WC_TEST_RET_ENC_NC;
  19239. total += outlen;
  19240. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  19241. return WC_TEST_RET_ENC_NC;
  19242. if(outlen != 16)
  19243. return WC_TEST_RET_ENC_NC;
  19244. total += outlen;
  19245. if(total != 32)
  19246. return WC_TEST_RET_ENC_NC;
  19247. total = 0;
  19248. EVP_CIPHER_CTX_init(de);
  19249. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  19250. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  19251. return WC_TEST_RET_ENC_NC;
  19252. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  19253. return WC_TEST_RET_ENC_NC;
  19254. if(outlen != 0)
  19255. return WC_TEST_RET_ENC_NC;
  19256. total += outlen;
  19257. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  19258. (byte*)&cipher[6], 12) == 0)
  19259. return WC_TEST_RET_ENC_NC;
  19260. if(outlen != 0)
  19261. total += outlen;
  19262. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  19263. (byte*)&cipher[6+12], 14) == 0)
  19264. return WC_TEST_RET_ENC_NC;
  19265. if(outlen != 16)
  19266. return WC_TEST_RET_ENC_NC;
  19267. total += outlen;
  19268. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  19269. return WC_TEST_RET_ENC_NC;
  19270. if(outlen != 2)
  19271. return WC_TEST_RET_ENC_NC;
  19272. total += outlen;
  19273. if(total != 18)
  19274. return WC_TEST_RET_ENC_NC;
  19275. if (XMEMCMP(plain, cbcPlain, 18))
  19276. return WC_TEST_RET_ENC_NC;
  19277. total = 0;
  19278. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  19279. return WC_TEST_RET_ENC_NC;
  19280. EVP_CIPHER_CTX_init(en);
  19281. if (EVP_EncryptInit(en, EVP_aes_128_cbc(),
  19282. (unsigned char*)key, (unsigned char*)iv) == 0)
  19283. return WC_TEST_RET_ENC_NC;
  19284. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen, (byte*)cbcPlain, 9) == 0)
  19285. return WC_TEST_RET_ENC_NC;
  19286. if(outlen != 0)
  19287. return WC_TEST_RET_ENC_NC;
  19288. total += outlen;
  19289. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen, (byte*)&cbcPlain[9] , 9) == 0)
  19290. return WC_TEST_RET_ENC_NC;
  19291. if(outlen != 16)
  19292. return WC_TEST_RET_ENC_NC;
  19293. total += outlen;
  19294. if (EVP_EncryptFinal(en, (byte*)&cipher[total], &outlen) == 0)
  19295. return WC_TEST_RET_ENC_NC;
  19296. if(outlen != 16)
  19297. return WC_TEST_RET_ENC_NC;
  19298. total += outlen;
  19299. if(total != 32)
  19300. return 3438;
  19301. total = 0;
  19302. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  19303. return WC_TEST_RET_ENC_NC;
  19304. EVP_CIPHER_CTX_init(de);
  19305. if (EVP_DecryptInit(de, EVP_aes_128_cbc(),
  19306. (unsigned char*)key, (unsigned char*)iv) == 0)
  19307. return WC_TEST_RET_ENC_NC;
  19308. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  19309. return WC_TEST_RET_ENC_NC;
  19310. if(outlen != 0)
  19311. return WC_TEST_RET_ENC_NC;
  19312. total += outlen;
  19313. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6], 12) == 0)
  19314. return WC_TEST_RET_ENC_NC;
  19315. if(outlen != 0)
  19316. total += outlen;
  19317. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6+12], 14) == 0)
  19318. return WC_TEST_RET_ENC_NC;
  19319. if(outlen != 16)
  19320. return WC_TEST_RET_ENC_NC;
  19321. total += outlen;
  19322. if (EVP_DecryptFinal(de, (byte*)&plain[total], &outlen) == 0)
  19323. return WC_TEST_RET_ENC_NC;
  19324. if(outlen != 2)
  19325. return WC_TEST_RET_ENC_NC;
  19326. total += outlen;
  19327. if(total != 18)
  19328. return 3447;
  19329. if (XMEMCMP(plain, cbcPlain, 18))
  19330. return WC_TEST_RET_ENC_NC;
  19331. if (EVP_CIPHER_key_length(NULL) != 0)
  19332. return WC_TEST_RET_ENC_NC;
  19333. if (EVP_CIPHER_key_length(EVP_aes_128_cbc()) != 16)
  19334. return WC_TEST_RET_ENC_NC;
  19335. if (EVP_CIPHER_CTX_mode(NULL) != 0)
  19336. return WC_TEST_RET_ENC_NC;
  19337. if (EVP_CIPHER_CTX_mode(en) != (en->flags & WOLFSSL_EVP_CIPH_MODE))
  19338. return WC_TEST_RET_ENC_NC;
  19339. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  19340. return WC_TEST_RET_ENC_NC;
  19341. EVP_CIPHER_CTX_init(en);
  19342. if (EVP_CipherInit_ex(en, EVP_aes_128_cbc(), NULL,
  19343. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  19344. return WC_TEST_RET_ENC_NC;
  19345. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  19346. return WC_TEST_RET_ENC_NC;
  19347. EVP_CIPHER_CTX_init(en);
  19348. if (EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  19349. (unsigned char*)key, (unsigned char*)iv) == 0)
  19350. return WC_TEST_RET_ENC_NC;
  19351. if (wolfSSL_EVP_EncryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  19352. return WC_TEST_RET_ENC_NC;
  19353. if (wolfSSL_EVP_EncryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  19354. return WC_TEST_RET_ENC_NC;
  19355. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  19356. return WC_TEST_RET_ENC_NC;
  19357. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  19358. return WC_TEST_RET_ENC_NC;
  19359. EVP_CIPHER_CTX_init(de);
  19360. if (EVP_DecryptInit_ex(de, EVP_aes_128_cbc(), NULL,
  19361. (unsigned char*)key, (unsigned char*)iv) == 0)
  19362. return WC_TEST_RET_ENC_NC;
  19363. if (wolfSSL_EVP_DecryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  19364. return WC_TEST_RET_ENC_NC;
  19365. if (wolfSSL_EVP_DecryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  19366. return WC_TEST_RET_ENC_NC;
  19367. if (EVP_CIPHER_CTX_block_size(NULL) != BAD_FUNC_ARG)
  19368. return WC_TEST_RET_ENC_NC;
  19369. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  19370. return WC_TEST_RET_ENC_NC;
  19371. EVP_CIPHER_CTX_init(en);
  19372. EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  19373. (unsigned char*)key, (unsigned char*)iv);
  19374. if (EVP_CIPHER_CTX_block_size(en) != en->block_size)
  19375. return WC_TEST_RET_ENC_NC;
  19376. if (EVP_CIPHER_block_size(NULL) != BAD_FUNC_ARG)
  19377. return WC_TEST_RET_ENC_NC;
  19378. if (EVP_CIPHER_block_size(EVP_aes_128_cbc()) != AES_BLOCK_SIZE)
  19379. return WC_TEST_RET_ENC_NC;
  19380. if (WOLFSSL_EVP_CIPHER_mode(NULL) != 0)
  19381. return WC_TEST_RET_ENC_NC;
  19382. if (EVP_CIPHER_flags(EVP_aes_128_cbc()) != WOLFSSL_EVP_CIPH_CBC_MODE)
  19383. return WC_TEST_RET_ENC_NC;
  19384. EVP_CIPHER_CTX_clear_flags(en, 0xFFFFFFFF);
  19385. EVP_CIPHER_CTX_set_flags(en, 42);
  19386. if (en->flags != 42)
  19387. return WC_TEST_RET_ENC_NC;
  19388. if (EVP_CIPHER_CTX_set_padding(NULL, 0) != BAD_FUNC_ARG)
  19389. return WC_TEST_RET_ENC_NC;
  19390. if (EVP_CIPHER_CTX_set_padding(en, 0) != WOLFSSL_SUCCESS)
  19391. return WC_TEST_RET_ENC_NC;
  19392. if (EVP_CIPHER_CTX_set_padding(en, 1) != WOLFSSL_SUCCESS)
  19393. return WC_TEST_RET_ENC_NC;
  19394. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  19395. return WC_TEST_RET_ENC_NC;
  19396. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  19397. return WC_TEST_RET_ENC_NC;
  19398. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19399. wolfSSL_EVP_CIPHER_CTX_free(en);
  19400. wolfSSL_EVP_CIPHER_CTX_free(de);
  19401. #endif
  19402. }
  19403. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  19404. #endif /* ifndef NO_AES */
  19405. return 0;
  19406. }
  19407. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openSSL_evpMD_test(void)
  19408. {
  19409. wc_test_ret_t ret = 0;
  19410. #if !defined(NO_SHA256) && !defined(NO_SHA)
  19411. WOLFSSL_EVP_MD_CTX* ctx;
  19412. WOLFSSL_EVP_MD_CTX* ctx2;
  19413. ctx = EVP_MD_CTX_create();
  19414. ctx2 = EVP_MD_CTX_create();
  19415. ret = EVP_DigestInit(ctx, EVP_sha256());
  19416. if (ret != SSL_SUCCESS) {
  19417. ret = WC_TEST_RET_ENC_NC;
  19418. goto openSSL_evpMD_test_done;
  19419. }
  19420. ret = EVP_MD_CTX_copy(ctx2, ctx);
  19421. if (ret != SSL_SUCCESS) {
  19422. ret = WC_TEST_RET_ENC_NC;
  19423. goto openSSL_evpMD_test_done;
  19424. }
  19425. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  19426. ret = WC_TEST_RET_ENC_NC;
  19427. goto openSSL_evpMD_test_done;
  19428. }
  19429. ret = EVP_DigestInit(ctx, EVP_sha1());
  19430. if (ret != SSL_SUCCESS) {
  19431. ret = WC_TEST_RET_ENC_NC;
  19432. goto openSSL_evpMD_test_done;
  19433. }
  19434. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  19435. ret = WC_TEST_RET_ENC_NC;
  19436. goto openSSL_evpMD_test_done;
  19437. }
  19438. ret = EVP_MD_CTX_copy_ex(ctx2, ctx);
  19439. if (ret != SSL_SUCCESS) {
  19440. ret = WC_TEST_RET_ENC_NC;
  19441. goto openSSL_evpMD_test_done;
  19442. }
  19443. if (EVP_MD_type(EVP_sha256()) == EVP_MD_CTX_type(ctx2)) {
  19444. ret = WC_TEST_RET_ENC_NC;
  19445. goto openSSL_evpMD_test_done;
  19446. }
  19447. if (EVP_MD_type(EVP_sha1()) != EVP_MD_CTX_type(ctx2)) {
  19448. ret = WC_TEST_RET_ENC_NC;
  19449. goto openSSL_evpMD_test_done;
  19450. }
  19451. if (EVP_DigestInit_ex(ctx, EVP_sha1(), NULL) != SSL_SUCCESS) {
  19452. ret = WC_TEST_RET_ENC_NC;
  19453. goto openSSL_evpMD_test_done;
  19454. }
  19455. if (EVP_add_digest(NULL) != 0) {
  19456. ret = WC_TEST_RET_ENC_NC;
  19457. goto openSSL_evpMD_test_done;
  19458. }
  19459. if (wolfSSL_EVP_add_cipher(NULL) != 0) {
  19460. ret = WC_TEST_RET_ENC_NC;
  19461. goto openSSL_evpMD_test_done;
  19462. }
  19463. ret = 0; /* got to success state without jumping to end with a fail */
  19464. openSSL_evpMD_test_done:
  19465. EVP_MD_CTX_destroy(ctx);
  19466. EVP_MD_CTX_destroy(ctx2);
  19467. #endif /* NO_SHA256 */
  19468. return ret;
  19469. }
  19470. #ifdef DEBUG_SIGN
  19471. static void show(const char *title, const char *p, unsigned int s) {
  19472. char* i;
  19473. printf("%s: ", title);
  19474. for (i = p;
  19475. i < p + s;
  19476. printf("%c", *i), i++);
  19477. printf("\n");
  19478. }
  19479. #else
  19480. #define show(a,b,c) WC_DO_NOTHING
  19481. #endif
  19482. #define FOURK_BUFF 4096
  19483. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_pkey0_test(void)
  19484. {
  19485. wc_test_ret_t ret = 0;
  19486. #if !defined(NO_RSA) && !defined(HAVE_USER_RSA) && !defined(NO_SHA)
  19487. byte* prvTmp;
  19488. byte* pubTmp;
  19489. int prvBytes;
  19490. int pubBytes;
  19491. RSA *prvRsa = NULL;
  19492. RSA *pubRsa = NULL;
  19493. EVP_PKEY *prvPkey = NULL;
  19494. EVP_PKEY *pubPkey = NULL;
  19495. EVP_PKEY_CTX *enc = NULL;
  19496. EVP_PKEY_CTX *dec = NULL;
  19497. byte in[] = TEST_STRING;
  19498. byte out[256];
  19499. size_t outlen;
  19500. size_t keySz;
  19501. byte plain[256];
  19502. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  19503. XFILE keyFile;
  19504. XFILE keypubFile;
  19505. char cliKey[] = "./certs/client-key.der";
  19506. char cliKeypub[] = "./certs/client-keyPub.der";
  19507. #endif
  19508. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19509. if (prvTmp == NULL)
  19510. return WC_TEST_RET_ENC_ERRNO;
  19511. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19512. if (pubTmp == NULL) {
  19513. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19514. return WC_TEST_RET_ENC_NC;
  19515. }
  19516. #ifdef USE_CERT_BUFFERS_1024
  19517. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  19518. prvBytes = sizeof_client_key_der_1024;
  19519. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  19520. pubBytes = sizeof_client_keypub_der_1024;
  19521. #elif defined(USE_CERT_BUFFERS_2048)
  19522. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  19523. prvBytes = sizeof_client_key_der_2048;
  19524. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  19525. pubBytes = sizeof_client_keypub_der_2048;
  19526. #else
  19527. keyFile = XFOPEN(cliKey, "rb");
  19528. if (!keyFile) {
  19529. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19530. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19531. err_sys("can't open ./certs/client-key.der, "
  19532. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  19533. return WC_TEST_RET_ENC_ERRNO;
  19534. }
  19535. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  19536. XFCLOSE(keyFile);
  19537. if (prvBytes == 0) {
  19538. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19539. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19540. return WC_TEST_RET_ENC_ERRNO;
  19541. }
  19542. keypubFile = XFOPEN(cliKeypub, "rb");
  19543. if (!keypubFile) {
  19544. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19545. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19546. err_sys("can't open ./certs/client-cert.der, "
  19547. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  19548. return WC_TEST_RET_ENC_ERRNO;
  19549. }
  19550. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  19551. XFCLOSE(keypubFile);
  19552. if (pubBytes == 0) {
  19553. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19554. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19555. return WC_TEST_RET_ENC_ERRNO;
  19556. }
  19557. #endif /* USE_CERT_BUFFERS */
  19558. prvRsa = wolfSSL_RSA_new();
  19559. pubRsa = wolfSSL_RSA_new();
  19560. if((prvRsa == NULL) || (pubRsa == NULL)){
  19561. printf("error with RSA_new\n");
  19562. ret = WC_TEST_RET_ENC_ERRNO;
  19563. goto openssl_pkey0_test_done;
  19564. }
  19565. ret = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  19566. if(ret != SSL_SUCCESS){
  19567. printf("error with RSA_LoadDer_ex\n");
  19568. ret = WC_TEST_RET_ENC_EC(ret);
  19569. goto openssl_pkey0_test_done;
  19570. }
  19571. ret = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  19572. if(ret != SSL_SUCCESS){
  19573. printf("error with RSA_LoadDer_ex\n");
  19574. ret = WC_TEST_RET_ENC_EC(ret);
  19575. goto openssl_pkey0_test_done;
  19576. }
  19577. keySz = (size_t)RSA_size(pubRsa);
  19578. prvPkey = wolfSSL_EVP_PKEY_new();
  19579. pubPkey = wolfSSL_EVP_PKEY_new();
  19580. if((prvPkey == NULL) || (pubPkey == NULL)){
  19581. printf("error with PKEY_new\n");
  19582. ret = WC_TEST_RET_ENC_NC;
  19583. goto openssl_pkey0_test_done;
  19584. }
  19585. ret = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  19586. ret += wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  19587. if(ret != 2){
  19588. printf("error with PKEY_set1_RSA\n");
  19589. ret = WC_TEST_RET_ENC_I(ret);
  19590. goto openssl_pkey0_test_done;
  19591. }
  19592. dec = EVP_PKEY_CTX_new(prvPkey, NULL);
  19593. enc = EVP_PKEY_CTX_new(pubPkey, NULL);
  19594. if((dec == NULL)||(enc==NULL)){
  19595. printf("error with EVP_PKEY_CTX_new\n");
  19596. ret = WC_TEST_RET_ENC_NC;
  19597. goto openssl_pkey0_test_done;
  19598. }
  19599. ret = EVP_PKEY_decrypt_init(dec);
  19600. if (ret != 1) {
  19601. printf("error with decrypt init\n");
  19602. ret = WC_TEST_RET_ENC_NC;
  19603. goto openssl_pkey0_test_done;
  19604. }
  19605. ret = EVP_PKEY_encrypt_init(enc);
  19606. if (ret != 1) {
  19607. printf("error with encrypt init\n");
  19608. ret = WC_TEST_RET_ENC_NC;
  19609. goto openssl_pkey0_test_done;
  19610. }
  19611. XMEMSET(out, 0, sizeof(out));
  19612. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  19613. if (ret != 1) {
  19614. printf("error encrypting msg\n");
  19615. ret = WC_TEST_RET_ENC_NC;
  19616. goto openssl_pkey0_test_done;
  19617. }
  19618. show("encrypted msg", out, outlen);
  19619. XMEMSET(plain, 0, sizeof(plain));
  19620. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  19621. if (ret != 1) {
  19622. printf("error decrypting msg\n");
  19623. ret = WC_TEST_RET_ENC_NC;
  19624. goto openssl_pkey0_test_done;
  19625. }
  19626. show("decrypted msg", plain, outlen);
  19627. /* RSA_PKCS1_OAEP_PADDING test */
  19628. ret = EVP_PKEY_decrypt_init(dec);
  19629. if (ret != 1) {
  19630. printf("error with decrypt init\n");
  19631. ret = WC_TEST_RET_ENC_NC;
  19632. goto openssl_pkey0_test_done;
  19633. }
  19634. ret = EVP_PKEY_encrypt_init(enc);
  19635. if (ret != 1) {
  19636. printf("error with encrypt init\n");
  19637. ret = WC_TEST_RET_ENC_NC;
  19638. goto openssl_pkey0_test_done;
  19639. }
  19640. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  19641. printf("first set rsa padding error\n");
  19642. ret = WC_TEST_RET_ENC_EC(ret);
  19643. goto openssl_pkey0_test_done;
  19644. }
  19645. #ifndef HAVE_FIPS
  19646. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  19647. printf("second set rsa padding error\n");
  19648. ret = WC_TEST_RET_ENC_EC(ret);
  19649. goto openssl_pkey0_test_done;
  19650. }
  19651. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  19652. printf("third set rsa padding error\n");
  19653. ret = WC_TEST_RET_ENC_EC(ret);
  19654. goto openssl_pkey0_test_done;
  19655. }
  19656. #endif
  19657. XMEMSET(out, 0, sizeof(out));
  19658. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  19659. if (ret != 1) {
  19660. printf("error encrypting msg\n");
  19661. ret = WC_TEST_RET_ENC_NC;
  19662. goto openssl_pkey0_test_done;
  19663. }
  19664. show("encrypted msg", out, outlen);
  19665. XMEMSET(plain, 0, sizeof(plain));
  19666. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  19667. if (ret != 1) {
  19668. printf("error decrypting msg\n");
  19669. ret = WC_TEST_RET_ENC_NC;
  19670. goto openssl_pkey0_test_done;
  19671. }
  19672. show("decrypted msg", plain, outlen);
  19673. ret = 0; /* made it to this point without error then set success */
  19674. openssl_pkey0_test_done:
  19675. wolfSSL_RSA_free(prvRsa);
  19676. wolfSSL_RSA_free(pubRsa);
  19677. EVP_PKEY_free(pubPkey);
  19678. EVP_PKEY_free(prvPkey);
  19679. EVP_PKEY_CTX_free(dec);
  19680. EVP_PKEY_CTX_free(enc);
  19681. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19682. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19683. #endif /* NO_RSA */
  19684. return ret;
  19685. }
  19686. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_pkey1_test(void)
  19687. {
  19688. wc_test_ret_t ret = 0;
  19689. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  19690. !defined(NO_SHA)
  19691. EVP_PKEY_CTX* dec = NULL;
  19692. EVP_PKEY_CTX* enc = NULL;
  19693. EVP_PKEY* pubKey = NULL;
  19694. EVP_PKEY* prvKey = NULL;
  19695. X509* x509 = NULL;
  19696. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sugar slapped";
  19697. const unsigned char* clikey;
  19698. long cliKeySz;
  19699. size_t outlen;
  19700. int keyLenBits = 2048;
  19701. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19702. unsigned char *tmp = (unsigned char *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19703. unsigned char *cipher = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19704. unsigned char *plain = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19705. if ((tmp == NULL) ||
  19706. (cipher == NULL) ||
  19707. (plain == NULL)) {
  19708. ret = WC_TEST_RET_ENC_NC;
  19709. goto openssl_pkey1_test_done;
  19710. }
  19711. #else
  19712. unsigned char tmp[FOURK_BUF];
  19713. unsigned char cipher[RSA_TEST_BYTES];
  19714. unsigned char plain[RSA_TEST_BYTES];
  19715. #endif
  19716. #if defined(USE_CERT_BUFFERS_1024)
  19717. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  19718. cliKeySz = (long)sizeof_client_key_der_1024;
  19719. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_1024,
  19720. sizeof_client_cert_der_1024, SSL_FILETYPE_ASN1);
  19721. keyLenBits = 1024;
  19722. #elif defined(USE_CERT_BUFFERS_2048)
  19723. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  19724. cliKeySz = (long)sizeof_client_key_der_2048;
  19725. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_2048,
  19726. sizeof_client_cert_der_2048, SSL_FILETYPE_ASN1);
  19727. #elif defined(USE_CERT_BUFFERS_3072)
  19728. XMEMCPY(tmp, client_key_der_3072, sizeof_client_key_der_3072);
  19729. cliKeySz = (long)sizeof_client_key_der_3072;
  19730. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_3072,
  19731. sizeof_client_cert_der_3072, SSL_FILETYPE_ASN1);
  19732. keyLenBits = 3072;
  19733. #elif defined(USE_CERT_BUFFERS_4096)
  19734. XMEMCPY(tmp, client_key_der_4096, sizeof_client_key_der_4096);
  19735. cliKeySz = (long)sizeof_client_key_der_4096;
  19736. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_4096,
  19737. sizeof_client_cert_der_4096, SSL_FILETYPE_ASN1);
  19738. keyLenBits = 4096;
  19739. #else
  19740. {
  19741. XFILE f;
  19742. f = XFOPEN(clientKey, "rb");
  19743. if (!f) {
  19744. err_sys("can't open ./certs/client-key.der, "
  19745. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  19746. ret = WC_TEST_RET_ENC_ERRNO;
  19747. goto openssl_pkey1_test_done;
  19748. }
  19749. cliKeySz = (long)XFREAD(tmp, 1, FOURK_BUF, f);
  19750. XFCLOSE(f);
  19751. if (cliKeySz == 0)
  19752. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, openssl_pkey1_test_done);
  19753. }
  19754. /* using existing wolfSSL api to get public and private key */
  19755. x509 = wolfSSL_X509_load_certificate_file(clientCert, SSL_FILETYPE_ASN1);
  19756. #endif /* USE_CERT_BUFFERS */
  19757. clikey = tmp;
  19758. if ((prvKey = EVP_PKEY_new()) == NULL) {
  19759. ret = WC_TEST_RET_ENC_ERRNO;
  19760. goto openssl_pkey1_test_done;
  19761. }
  19762. EVP_PKEY_free(prvKey);
  19763. prvKey = NULL;
  19764. if (x509 == NULL) {
  19765. ret = WC_TEST_RET_ENC_NC;
  19766. goto openssl_pkey1_test_done;
  19767. }
  19768. pubKey = X509_get_pubkey(x509);
  19769. if (pubKey == NULL) {
  19770. ret = WC_TEST_RET_ENC_NC;
  19771. goto openssl_pkey1_test_done;
  19772. }
  19773. prvKey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &clikey, cliKeySz);
  19774. if (prvKey == NULL) {
  19775. ret = WC_TEST_RET_ENC_NC;
  19776. goto openssl_pkey1_test_done;
  19777. }
  19778. /* phase 2 API to create EVP_PKEY_CTX and encrypt/decrypt */
  19779. if (EVP_PKEY_bits(prvKey) != keyLenBits) {
  19780. ret = WC_TEST_RET_ENC_NC;
  19781. goto openssl_pkey1_test_done;
  19782. }
  19783. if (EVP_PKEY_size(prvKey) != keyLenBits/8) {
  19784. ret = WC_TEST_RET_ENC_NC;
  19785. goto openssl_pkey1_test_done;
  19786. }
  19787. dec = EVP_PKEY_CTX_new(prvKey, NULL);
  19788. enc = EVP_PKEY_CTX_new(pubKey, NULL);
  19789. if (dec == NULL || enc == NULL) {
  19790. ret = WC_TEST_RET_ENC_ERRNO;
  19791. goto openssl_pkey1_test_done;
  19792. }
  19793. if (EVP_PKEY_decrypt_init(dec) != 1) {
  19794. ret = WC_TEST_RET_ENC_NC;
  19795. goto openssl_pkey1_test_done;
  19796. }
  19797. if (EVP_PKEY_encrypt_init(enc) != 1) {
  19798. ret = WC_TEST_RET_ENC_NC;
  19799. goto openssl_pkey1_test_done;
  19800. }
  19801. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  19802. ret = WC_TEST_RET_ENC_NC;
  19803. goto openssl_pkey1_test_done;
  19804. }
  19805. #ifndef HAVE_FIPS
  19806. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  19807. ret = WC_TEST_RET_ENC_EC(ret);
  19808. goto openssl_pkey1_test_done;
  19809. }
  19810. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  19811. ret = WC_TEST_RET_ENC_EC(ret);
  19812. goto openssl_pkey1_test_done;
  19813. }
  19814. #endif
  19815. XMEMSET(cipher, 0, RSA_TEST_BYTES);
  19816. outlen = keyLenBits/8;
  19817. if (EVP_PKEY_encrypt(enc, cipher, &outlen, msg, sizeof(msg)) < 0) {
  19818. ret = WC_TEST_RET_ENC_EC(ret);
  19819. goto openssl_pkey1_test_done;
  19820. }
  19821. XMEMSET(plain, 0, RSA_TEST_BYTES);
  19822. if (EVP_PKEY_decrypt(dec, plain, &outlen, cipher, outlen) != 1) {
  19823. ret = WC_TEST_RET_ENC_NC;
  19824. goto openssl_pkey1_test_done;
  19825. }
  19826. openssl_pkey1_test_done:
  19827. if (pubKey != NULL) {
  19828. EVP_PKEY_free(pubKey);
  19829. }
  19830. if (prvKey != NULL) {
  19831. EVP_PKEY_free(prvKey);
  19832. }
  19833. if (dec != NULL) {
  19834. EVP_PKEY_CTX_free(dec);
  19835. }
  19836. if (enc != NULL) {
  19837. EVP_PKEY_CTX_free(enc);
  19838. }
  19839. if (x509 != NULL) {
  19840. X509_free(x509);
  19841. }
  19842. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19843. if (tmp != NULL)
  19844. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19845. if (cipher != NULL)
  19846. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19847. if (plain != NULL)
  19848. XFREE(plain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19849. #endif
  19850. #endif
  19851. return ret;
  19852. }
  19853. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_evpSig_test(void)
  19854. {
  19855. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(HAVE_USER_RSA)
  19856. byte* prvTmp;
  19857. byte* pubTmp;
  19858. int prvBytes;
  19859. int pubBytes;
  19860. RSA *prvRsa;
  19861. RSA *pubRsa;
  19862. EVP_PKEY *prvPkey;
  19863. EVP_PKEY *pubPkey;
  19864. EVP_MD_CTX* sign;
  19865. EVP_MD_CTX* verf;
  19866. char msg[] = "see spot run";
  19867. unsigned char sig[256];
  19868. unsigned int sigSz;
  19869. const void* pt;
  19870. unsigned int count;
  19871. wc_test_ret_t ret, ret1, ret2;
  19872. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  19873. XFILE keyFile;
  19874. XFILE keypubFile;
  19875. char cliKey[] = "./certs/client-key.der";
  19876. char cliKeypub[] = "./certs/client-keyPub.der";
  19877. #endif
  19878. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19879. if (prvTmp == NULL)
  19880. return WC_TEST_RET_ENC_ERRNO;
  19881. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19882. if (pubTmp == NULL) {
  19883. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19884. return WC_TEST_RET_ENC_NC;
  19885. }
  19886. #ifdef USE_CERT_BUFFERS_1024
  19887. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  19888. prvBytes = sizeof_client_key_der_1024;
  19889. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  19890. pubBytes = sizeof_client_keypub_der_1024;
  19891. #elif defined(USE_CERT_BUFFERS_2048)
  19892. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  19893. prvBytes = sizeof_client_key_der_2048;
  19894. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  19895. pubBytes = sizeof_client_keypub_der_2048;
  19896. #else
  19897. keyFile = XFOPEN(cliKey, "rb");
  19898. if (!keyFile) {
  19899. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19900. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19901. err_sys("can't open ./certs/client-key.der, "
  19902. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  19903. return WC_TEST_RET_ENC_ERRNO;
  19904. }
  19905. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  19906. XFCLOSE(keyFile);
  19907. if (prvBytes == 0) {
  19908. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19909. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19910. return WC_TEST_RET_ENC_ERRNO;
  19911. }
  19912. keypubFile = XFOPEN(cliKeypub, "rb");
  19913. if (!keypubFile) {
  19914. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19915. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19916. err_sys("can't open ./certs/client-cert.der, "
  19917. "Please run from wolfSSL home dir", WC_TEST_RET_ENC_ERRNO);
  19918. return WC_TEST_RET_ENC_ERRNO;
  19919. }
  19920. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  19921. XFCLOSE(keypubFile);
  19922. if (pubBytes == 0) {
  19923. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19924. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19925. return WC_TEST_RET_ENC_ERRNO;
  19926. }
  19927. #endif /* USE_CERT_BUFFERS */
  19928. prvRsa = wolfSSL_RSA_new();
  19929. pubRsa = wolfSSL_RSA_new();
  19930. if((prvRsa == NULL) || (pubRsa == NULL)){
  19931. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19932. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19933. err_sys("ERROR with RSA_new", WC_TEST_RET_ENC_NC);
  19934. return WC_TEST_RET_ENC_NC;
  19935. }
  19936. ret1 = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  19937. ret2 = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  19938. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  19939. printf("error with RSA_LoadDer_ex\n");
  19940. return WC_TEST_RET_ENC_NC;
  19941. }
  19942. prvPkey = wolfSSL_EVP_PKEY_new();
  19943. pubPkey = wolfSSL_EVP_PKEY_new();
  19944. if((prvPkey == NULL) || (pubPkey == NULL)){
  19945. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19946. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19947. printf("error with KEY_new\n");
  19948. return WC_TEST_RET_ENC_NC;
  19949. }
  19950. ret1 = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  19951. ret2 = wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  19952. if((ret1 != 1) || (ret2 != 1)){
  19953. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19954. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19955. printf("error with EVP_PKEY_set1_RSA\n");
  19956. return WC_TEST_RET_ENC_NC;
  19957. }
  19958. /****************** sign and verify *******************/
  19959. sign = EVP_MD_CTX_create();
  19960. verf = EVP_MD_CTX_create();
  19961. if((sign == NULL)||(verf == NULL)){
  19962. printf("error with EVP_MD_CTX_create\n");
  19963. EVP_MD_CTX_destroy(sign);
  19964. EVP_MD_CTX_destroy(verf);
  19965. return WC_TEST_RET_ENC_NC;
  19966. }
  19967. ret = EVP_SignInit(sign, EVP_sha1());
  19968. if (ret != SSL_SUCCESS){
  19969. printf("error with EVP_SignInit\n");
  19970. EVP_MD_CTX_destroy(sign);
  19971. EVP_MD_CTX_destroy(verf);
  19972. return WC_TEST_RET_ENC_NC;
  19973. }
  19974. count = sizeof(msg);
  19975. show("message = ", (char *)msg, count);
  19976. /* sign */
  19977. XMEMSET(sig, 0, sizeof(sig));
  19978. pt = (const void*)msg;
  19979. ret1 = EVP_SignUpdate(sign, pt, count);
  19980. ret2 = EVP_SignFinal(sign, sig, &sigSz, prvPkey);
  19981. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  19982. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19983. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19984. EVP_MD_CTX_destroy(sign);
  19985. EVP_MD_CTX_destroy(verf);
  19986. printf("error with EVP_MD_CTX_create\n");
  19987. return WC_TEST_RET_ENC_NC;
  19988. }
  19989. show("signature = ", (char *)sig, sigSz);
  19990. /* verify */
  19991. pt = (const void*)msg;
  19992. ret1 = EVP_VerifyInit(verf, EVP_sha1());
  19993. ret2 = EVP_VerifyUpdate(verf, pt, count);
  19994. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  19995. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19996. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19997. EVP_MD_CTX_destroy(sign);
  19998. EVP_MD_CTX_destroy(verf);
  19999. printf("error with EVP_Verify\n");
  20000. return WC_TEST_RET_ENC_NC;
  20001. }
  20002. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) != 1) {
  20003. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20004. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20005. EVP_MD_CTX_destroy(sign);
  20006. EVP_MD_CTX_destroy(verf);
  20007. printf("error with EVP_VerifyFinal\n");
  20008. return WC_TEST_RET_ENC_NC;
  20009. }
  20010. /* expect fail without update */
  20011. EVP_VerifyInit(verf, EVP_sha1());
  20012. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) == 1) {
  20013. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20014. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20015. EVP_MD_CTX_destroy(sign);
  20016. EVP_MD_CTX_destroy(verf);
  20017. printf("EVP_VerifyInit without update not detected\n");
  20018. return WC_TEST_RET_ENC_NC;
  20019. }
  20020. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20021. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20022. EVP_MD_CTX_destroy(sign);
  20023. EVP_MD_CTX_destroy(verf);
  20024. wolfSSL_RSA_free(prvRsa);
  20025. wolfSSL_RSA_free(pubRsa);
  20026. EVP_PKEY_free(pubPkey);
  20027. EVP_PKEY_free(prvPkey);
  20028. #endif /* NO_RSA */
  20029. return 0;
  20030. }
  20031. #endif /* OPENSSL_EXTRA */
  20032. #ifndef NO_PWDBASED
  20033. #ifdef HAVE_SCRYPT
  20034. /* Test vectors taken from RFC 7914: scrypt PBKDF - Section 12. */
  20035. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t scrypt_test(void)
  20036. {
  20037. #ifdef HAVE_FIPS
  20038. /* RFC 7914 test vector keys are too short for FIPS. */
  20039. #else
  20040. wc_test_ret_t ret;
  20041. byte derived[64];
  20042. WOLFSSL_SMALL_STACK_STATIC const byte verify1[] = {
  20043. 0x77, 0xd6, 0x57, 0x62, 0x38, 0x65, 0x7b, 0x20,
  20044. 0x3b, 0x19, 0xca, 0x42, 0xc1, 0x8a, 0x04, 0x97,
  20045. 0xf1, 0x6b, 0x48, 0x44, 0xe3, 0x07, 0x4a, 0xe8,
  20046. 0xdf, 0xdf, 0xfa, 0x3f, 0xed, 0xe2, 0x14, 0x42,
  20047. 0xfc, 0xd0, 0x06, 0x9d, 0xed, 0x09, 0x48, 0xf8,
  20048. 0x32, 0x6a, 0x75, 0x3a, 0x0f, 0xc8, 0x1f, 0x17,
  20049. 0xe8, 0xd3, 0xe0, 0xfb, 0x2e, 0x0d, 0x36, 0x28,
  20050. 0xcf, 0x35, 0xe2, 0x0c, 0x38, 0xd1, 0x89, 0x06
  20051. };
  20052. #if !defined(BENCH_EMBEDDED)
  20053. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  20054. 0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00,
  20055. 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe,
  20056. 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30,
  20057. 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62,
  20058. 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88,
  20059. 0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda,
  20060. 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d,
  20061. 0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40
  20062. };
  20063. #endif
  20064. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  20065. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  20066. 0x70, 0x23, 0xbd, 0xcb, 0x3a, 0xfd, 0x73, 0x48,
  20067. 0x46, 0x1c, 0x06, 0xcd, 0x81, 0xfd, 0x38, 0xeb,
  20068. 0xfd, 0xa8, 0xfb, 0xba, 0x90, 0x4f, 0x8e, 0x3e,
  20069. 0xa9, 0xb5, 0x43, 0xf6, 0x54, 0x5d, 0xa1, 0xf2,
  20070. 0xd5, 0x43, 0x29, 0x55, 0x61, 0x3f, 0x0f, 0xcf,
  20071. 0x62, 0xd4, 0x97, 0x05, 0x24, 0x2a, 0x9a, 0xf9,
  20072. 0xe6, 0x1e, 0x85, 0xdc, 0x0d, 0x65, 0x1e, 0x40,
  20073. 0xdf, 0xcf, 0x01, 0x7b, 0x45, 0x57, 0x58, 0x87
  20074. };
  20075. #endif
  20076. #ifdef SCRYPT_TEST_ALL
  20077. /* Test case is very slow.
  20078. * Use for confirmation after code change or new platform.
  20079. */
  20080. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  20081. 0x21, 0x01, 0xcb, 0x9b, 0x6a, 0x51, 0x1a, 0xae,
  20082. 0xad, 0xdb, 0xbe, 0x09, 0xcf, 0x70, 0xf8, 0x81,
  20083. 0xec, 0x56, 0x8d, 0x57, 0x4a, 0x2f, 0xfd, 0x4d,
  20084. 0xab, 0xe5, 0xee, 0x98, 0x20, 0xad, 0xaa, 0x47,
  20085. 0x8e, 0x56, 0xfd, 0x8f, 0x4b, 0xa5, 0xd0, 0x9f,
  20086. 0xfa, 0x1c, 0x6d, 0x92, 0x7c, 0x40, 0xf4, 0xc3,
  20087. 0x37, 0x30, 0x40, 0x49, 0xe8, 0xa9, 0x52, 0xfb,
  20088. 0xcb, 0xf4, 0x5c, 0x6f, 0xa7, 0x7a, 0x41, 0xa4
  20089. };
  20090. #endif
  20091. ret = wc_scrypt(derived, NULL, 0, NULL, 0, 4, 1, 1, sizeof(verify1));
  20092. if (ret != 0)
  20093. return WC_TEST_RET_ENC_EC(ret);
  20094. if (XMEMCMP(derived, verify1, sizeof(verify1)) != 0)
  20095. return WC_TEST_RET_ENC_NC;
  20096. #if !defined(BENCH_EMBEDDED)
  20097. ret = wc_scrypt(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 10, 8, 16,
  20098. sizeof(verify2));
  20099. if (ret != 0)
  20100. return WC_TEST_RET_ENC_EC(ret);
  20101. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  20102. return WC_TEST_RET_ENC_NC;
  20103. #endif
  20104. /* Test case with parallel overflowing */
  20105. ret = wc_scrypt(derived, (byte*)"password", 16, (byte*)"NaCl", 16, 2, 4, 8388608,
  20106. sizeof(verify1));
  20107. if (ret != BAD_FUNC_ARG)
  20108. return WC_TEST_RET_ENC_EC(ret);
  20109. /* Don't run these test on embedded, since they use large mallocs */
  20110. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  20111. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  20112. (byte*)"SodiumChloride", 14, 14, 8, 1, sizeof(verify3));
  20113. if (ret != 0)
  20114. return WC_TEST_RET_ENC_EC(ret);
  20115. if (XMEMCMP(derived, verify3, sizeof(verify3)) != 0)
  20116. return WC_TEST_RET_ENC_NC;
  20117. #ifdef SCRYPT_TEST_ALL
  20118. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  20119. (byte*)"SodiumChloride", 14, 20, 8, 1, sizeof(verify4));
  20120. if (ret != 0)
  20121. return WC_TEST_RET_ENC_EC(ret);
  20122. if (XMEMCMP(derived, verify4, sizeof(verify4)) != 0)
  20123. return WC_TEST_RET_ENC_NC;
  20124. #endif
  20125. #endif /* !BENCH_EMBEDDED && !defined(WOLFSSL_LINUXKM) && !HAVE_INTEL_QA */
  20126. #if !defined(BENCH_EMBEDDED)
  20127. ret = wc_scrypt_ex(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 1<<10,
  20128. 8, 16, sizeof(verify2));
  20129. if (ret != 0)
  20130. return WC_TEST_RET_ENC_EC(ret);
  20131. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  20132. return WC_TEST_RET_ENC_NC;
  20133. #endif
  20134. #endif /* !HAVE_FIPS */
  20135. return 0;
  20136. }
  20137. #endif
  20138. #ifdef HAVE_PKCS12
  20139. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_test(void)
  20140. {
  20141. WOLFSSL_SMALL_STACK_STATIC const byte passwd[] = { 0x00, 0x73, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x67,
  20142. 0x00, 0x00 };
  20143. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x0a, 0x58, 0xCF, 0x64, 0x53, 0x0d, 0x82, 0x3f };
  20144. WOLFSSL_SMALL_STACK_STATIC const byte passwd2[] = { 0x00, 0x71, 0x00, 0x75, 0x00, 0x65, 0x00, 0x65,
  20145. 0x00, 0x67, 0x00, 0x00 };
  20146. WOLFSSL_SMALL_STACK_STATIC const byte salt2[] = { 0x16, 0x82, 0xC0, 0xfC, 0x5b, 0x3f, 0x7e, 0xc5 };
  20147. byte derived[64];
  20148. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  20149. 0x27, 0xE9, 0x0D, 0x7E, 0xD5, 0xA1, 0xC4, 0x11,
  20150. 0xBA, 0x87, 0x8B, 0xC0, 0x90, 0xF5, 0xCE, 0xBE,
  20151. 0x5E, 0x9D, 0x5F, 0xE3, 0xD6, 0x2B, 0x73, 0xAA
  20152. };
  20153. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  20154. 0x90, 0x1B, 0x49, 0x70, 0xF0, 0x94, 0xF0, 0xF8,
  20155. 0x45, 0xC0, 0xF3, 0xF3, 0x13, 0x59, 0x18, 0x6A,
  20156. 0x35, 0xE3, 0x67, 0xFE, 0xD3, 0x21, 0xFD, 0x7C
  20157. };
  20158. int id = 1;
  20159. int kLen = 24;
  20160. int iterations = 1;
  20161. wc_test_ret_t ret = wc_PKCS12_PBKDF(derived, passwd, sizeof(passwd), salt, 8,
  20162. iterations, kLen, WC_SHA256, id);
  20163. if (ret < 0)
  20164. return WC_TEST_RET_ENC_EC(ret);
  20165. if (XMEMCMP(derived, verify, kLen) != 0)
  20166. return WC_TEST_RET_ENC_NC;
  20167. iterations = 1000;
  20168. ret = wc_PKCS12_PBKDF(derived, passwd2, sizeof(passwd2), salt2, 8,
  20169. iterations, kLen, WC_SHA256, id);
  20170. if (ret < 0)
  20171. return WC_TEST_RET_ENC_EC(ret);
  20172. ret = wc_PKCS12_PBKDF_ex(derived, passwd2, sizeof(passwd2), salt2, 8,
  20173. iterations, kLen, WC_SHA256, id, HEAP_HINT);
  20174. if (ret < 0)
  20175. return WC_TEST_RET_ENC_EC(ret);
  20176. if (XMEMCMP(derived, verify2, 24) != 0)
  20177. return WC_TEST_RET_ENC_NC;
  20178. return 0;
  20179. }
  20180. #endif /* HAVE_PKCS12 */
  20181. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  20182. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf2_test(void)
  20183. {
  20184. char passwd[] = "passwordpassword";
  20185. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  20186. int iterations = 2048;
  20187. int kLen = 24;
  20188. byte derived[64];
  20189. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  20190. 0x43, 0x6d, 0xb5, 0xe8, 0xd0, 0xfb, 0x3f, 0x35, 0x42, 0x48, 0x39, 0xbc,
  20191. 0x2d, 0xd4, 0xf9, 0x37, 0xd4, 0x95, 0x16, 0xa7, 0x2a, 0x9a, 0x21, 0xd1
  20192. };
  20193. wc_test_ret_t ret = wc_PBKDF2_ex(derived, (byte*)passwd, (int)XSTRLEN(passwd), salt,
  20194. (int)sizeof(salt), iterations, kLen, WC_SHA256, HEAP_HINT, devId);
  20195. if (ret != 0)
  20196. return ret;
  20197. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  20198. return WC_TEST_RET_ENC_NC;
  20199. return 0;
  20200. }
  20201. #endif /* HAVE_PBKDF2 && !NO_SHA256 && !NO_HMAC */
  20202. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  20203. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf1_test(void)
  20204. {
  20205. char passwd[] = "password";
  20206. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  20207. int iterations = 1000;
  20208. int kLen = 16;
  20209. byte derived[16];
  20210. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  20211. 0xDC, 0x19, 0x84, 0x7E, 0x05, 0xC6, 0x4D, 0x2F,
  20212. 0xAF, 0x10, 0xEB, 0xFB, 0x4A, 0x3D, 0x2A, 0x20
  20213. };
  20214. wc_test_ret_t ret = wc_PBKDF1_ex(derived, kLen, NULL, 0, (byte*)passwd,
  20215. (int)XSTRLEN(passwd), salt, (int)sizeof(salt), iterations, WC_SHA,
  20216. HEAP_HINT);
  20217. if (ret != 0)
  20218. return ret;
  20219. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  20220. return WC_TEST_RET_ENC_NC;
  20221. return 0;
  20222. }
  20223. #endif /* HAVE_PBKDF2 && !NO_SHA */
  20224. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pwdbased_test(void)
  20225. {
  20226. wc_test_ret_t ret = 0;
  20227. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  20228. ret = pbkdf1_test();
  20229. if (ret != 0)
  20230. return ret;
  20231. #endif
  20232. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  20233. ret = pbkdf2_test();
  20234. if (ret != 0)
  20235. return ret;
  20236. #endif
  20237. #ifdef HAVE_PKCS12
  20238. ret = pkcs12_test();
  20239. if (ret != 0)
  20240. return ret;
  20241. #endif
  20242. #ifdef HAVE_SCRYPT
  20243. ret = scrypt_test();
  20244. if (ret != 0)
  20245. return ret;
  20246. #endif
  20247. return ret;
  20248. }
  20249. #endif /* NO_PWDBASED */
  20250. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  20251. #if defined(WOLFSSL_AFALG_XILINX) || defined(WOLFSSL_AFALG_XILINX_AES) || \
  20252. defined(WOLFSSL_AFALG_XILINX_SHA3) || defined(WOLFSSL_AFALG_HASH_KEEP) || \
  20253. defined(WOLFSSL_AFALG_XILINX_RSA)
  20254. /* hkdf_test has issue with WOLFSSL_TEST_SUBROUTINE set on Xilinx with afalg */
  20255. static wc_test_ret_t hkdf_test(void)
  20256. #else
  20257. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hkdf_test(void)
  20258. #endif
  20259. {
  20260. wc_test_ret_t ret = 0;
  20261. #if !defined(NO_SHA) || !defined(NO_SHA256)
  20262. int L;
  20263. byte okm1[42];
  20264. byte ikm1[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  20265. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  20266. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  20267. #ifndef HAVE_FIPS
  20268. byte salt1[13] ={ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  20269. 0x08, 0x09, 0x0a, 0x0b, 0x0c };
  20270. byte info1[10] ={ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  20271. 0xf8, 0xf9 };
  20272. #endif
  20273. #ifndef NO_SHA
  20274. byte res1[42] = { 0x0a, 0xc1, 0xaf, 0x70, 0x02, 0xb3, 0xd7, 0x61,
  20275. 0xd1, 0xe5, 0x52, 0x98, 0xda, 0x9d, 0x05, 0x06,
  20276. 0xb9, 0xae, 0x52, 0x05, 0x72, 0x20, 0xa3, 0x06,
  20277. 0xe0, 0x7b, 0x6b, 0x87, 0xe8, 0xdf, 0x21, 0xd0,
  20278. 0xea, 0x00, 0x03, 0x3d, 0xe0, 0x39, 0x84, 0xd3,
  20279. 0x49, 0x18 };
  20280. #ifndef HAVE_FIPS
  20281. byte res2[42] = { 0x08, 0x5a, 0x01, 0xea, 0x1b, 0x10, 0xf3, 0x69,
  20282. 0x33, 0x06, 0x8b, 0x56, 0xef, 0xa5, 0xad, 0x81,
  20283. 0xa4, 0xf1, 0x4b, 0x82, 0x2f, 0x5b, 0x09, 0x15,
  20284. 0x68, 0xa9, 0xcd, 0xd4, 0xf1, 0x55, 0xfd, 0xa2,
  20285. 0xc2, 0x2e, 0x42, 0x24, 0x78, 0xd3, 0x05, 0xf3,
  20286. 0xf8, 0x96 };
  20287. #endif
  20288. #endif /* !NO_SHA */
  20289. #ifndef NO_SHA256
  20290. byte res3[42] = { 0x8d, 0xa4, 0xe7, 0x75, 0xa5, 0x63, 0xc1, 0x8f,
  20291. 0x71, 0x5f, 0x80, 0x2a, 0x06, 0x3c, 0x5a, 0x31,
  20292. 0xb8, 0xa1, 0x1f, 0x5c, 0x5e, 0xe1, 0x87, 0x9e,
  20293. 0xc3, 0x45, 0x4e, 0x5f, 0x3c, 0x73, 0x8d, 0x2d,
  20294. 0x9d, 0x20, 0x13, 0x95, 0xfa, 0xa4, 0xb6, 0x1a,
  20295. 0x96, 0xc8 };
  20296. #ifndef HAVE_FIPS
  20297. byte res4[42] = { 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a,
  20298. 0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a,
  20299. 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c,
  20300. 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf,
  20301. 0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18,
  20302. 0x58, 0x65 };
  20303. #endif
  20304. #endif /* !NO_SHA256 */
  20305. XMEMSET(okm1, 0, sizeof(okm1));
  20306. L = (int)sizeof(okm1);
  20307. #ifndef NO_SHA
  20308. ret = wc_HKDF(WC_SHA, ikm1, (word32)sizeof(ikm1), NULL, 0, NULL, 0,
  20309. okm1, L);
  20310. if (ret != 0)
  20311. return WC_TEST_RET_ENC_EC(ret);
  20312. if (XMEMCMP(okm1, res1, L) != 0)
  20313. return WC_TEST_RET_ENC_NC;
  20314. #ifndef HAVE_FIPS
  20315. /* fips can't have key size under 14 bytes, salt is key too */
  20316. L = (int)sizeof(okm1);
  20317. ret = wc_HKDF(WC_SHA, ikm1, 11, salt1, (word32)sizeof(salt1),
  20318. info1, (word32)sizeof(info1), okm1, L);
  20319. if (ret != 0)
  20320. return WC_TEST_RET_ENC_EC(ret);
  20321. if (XMEMCMP(okm1, res2, L) != 0)
  20322. return WC_TEST_RET_ENC_NC;
  20323. #endif /* HAVE_FIPS */
  20324. #endif /* !NO_SHA */
  20325. #ifndef NO_SHA256
  20326. ret = wc_HKDF(WC_SHA256, ikm1, (word32)sizeof(ikm1), NULL, 0, NULL, 0,
  20327. okm1, L);
  20328. if (ret != 0)
  20329. return WC_TEST_RET_ENC_EC(ret);
  20330. if (XMEMCMP(okm1, res3, L) != 0)
  20331. return WC_TEST_RET_ENC_NC;
  20332. #ifndef HAVE_FIPS
  20333. /* fips can't have key size under 14 bytes, salt is key too */
  20334. ret = wc_HKDF(WC_SHA256, ikm1, (word32)sizeof(ikm1),
  20335. salt1, (word32)sizeof(salt1), info1, (word32)sizeof(info1), okm1, L);
  20336. if (ret != 0)
  20337. return WC_TEST_RET_ENC_EC(ret);
  20338. if (XMEMCMP(okm1, res4, L) != 0)
  20339. return WC_TEST_RET_ENC_NC;
  20340. #endif /* HAVE_FIPS */
  20341. #endif /* !NO_SHA256 */
  20342. #endif /* !NO_SHA || !NO_SHA256 */
  20343. return ret;
  20344. }
  20345. #endif /* HAVE_HKDF */
  20346. #ifdef WOLFSSL_WOLFSSH
  20347. typedef struct {
  20348. byte hashId;
  20349. byte keyId;
  20350. const byte* k;
  20351. word32 kSz;
  20352. const byte* h;
  20353. word32 hSz;
  20354. const byte* sessionId;
  20355. word32 sessionIdSz;
  20356. const byte* expectedKey;
  20357. word32 expectedKeySz;
  20358. } SshKdfTestVector;
  20359. /** Test Vector Set #3: SHA-256 **/
  20360. static const byte sshKdfTvSet3k[] = {
  20361. 0x6A, 0xC3, 0x82, 0xEA, 0xAC, 0xA0, 0x93, 0xE1,
  20362. 0x25, 0xE2, 0x5C, 0x24, 0xBE, 0xBC, 0x84, 0x64,
  20363. 0x0C, 0x11, 0x98, 0x75, 0x07, 0x34, 0x4B, 0x5C,
  20364. 0x73, 0x9C, 0xEB, 0x84, 0xA9, 0xE0, 0xB2, 0x22,
  20365. 0xB9, 0xA8, 0xB5, 0x1C, 0x83, 0x9E, 0x5E, 0xBE,
  20366. 0x49, 0xCF, 0xAD, 0xBF, 0xB3, 0x95, 0x99, 0x76,
  20367. 0x4E, 0xD5, 0x22, 0x09, 0x9D, 0xC9, 0x12, 0x75,
  20368. 0x19, 0x50, 0xDC, 0x7D, 0xC9, 0x7F, 0xBD, 0xC0,
  20369. 0x63, 0x28, 0xB6, 0x8F, 0x22, 0x78, 0x1F, 0xD3,
  20370. 0x15, 0xAF, 0x56, 0x80, 0x09, 0xA5, 0x50, 0x9E,
  20371. 0x5B, 0x87, 0xA1, 0x1B, 0xF5, 0x27, 0xC0, 0x56,
  20372. 0xDA, 0xFF, 0xD8, 0x2A, 0xB6, 0xCB, 0xC2, 0x5C,
  20373. 0xCA, 0x37, 0x14, 0x34, 0x59, 0xE7, 0xBC, 0x63,
  20374. 0xBC, 0xDE, 0x52, 0x75, 0x7A, 0xDE, 0xB7, 0xDF,
  20375. 0x01, 0xCF, 0x12, 0x17, 0x3F, 0x1F, 0xEF, 0x81,
  20376. 0x02, 0xEC, 0x5A, 0xB1, 0x42, 0xC2, 0x13, 0xDD,
  20377. 0x9D, 0x30, 0x69, 0x62, 0x78, 0xA8, 0xD8, 0xBC,
  20378. 0x32, 0xDD, 0xE9, 0x59, 0x2D, 0x28, 0xC0, 0x78,
  20379. 0xC6, 0xD9, 0x2B, 0x94, 0x7D, 0x82, 0x5A, 0xCA,
  20380. 0xAB, 0x64, 0x94, 0x84, 0x6A, 0x49, 0xDE, 0x24,
  20381. 0xB9, 0x62, 0x3F, 0x48, 0x89, 0xE8, 0xAD, 0xC3,
  20382. 0x8E, 0x8C, 0x66, 0x9E, 0xFF, 0xEF, 0x17, 0x60,
  20383. 0x40, 0xAD, 0x94, 0x5E, 0x90, 0xA7, 0xD3, 0xEE,
  20384. 0xC1, 0x5E, 0xFE, 0xEE, 0x78, 0xAE, 0x71, 0x04,
  20385. 0x3C, 0x96, 0x51, 0x11, 0x03, 0xA1, 0x6B, 0xA7,
  20386. 0xCA, 0xF0, 0xAC, 0xD0, 0x64, 0x2E, 0xFD, 0xBE,
  20387. 0x80, 0x99, 0x34, 0xFA, 0xA1, 0xA5, 0xF1, 0xBD,
  20388. 0x11, 0x04, 0x36, 0x49, 0xB2, 0x5C, 0xCD, 0x1F,
  20389. 0xEE, 0x2E, 0x38, 0x81, 0x5D, 0x4D, 0x5F, 0x5F,
  20390. 0xC6, 0xB4, 0x10, 0x29, 0x69, 0xF2, 0x1C, 0x22,
  20391. 0xAE, 0x1B, 0x0E, 0x7D, 0x36, 0x03, 0xA5, 0x56,
  20392. 0xA1, 0x32, 0x62, 0xFF, 0x62, 0x8D, 0xE2, 0x22
  20393. };
  20394. static const byte sshKdfTvSet3h[] = {
  20395. 0x7B, 0x70, 0x01, 0x18, 0x5E, 0x25, 0x6D, 0x44,
  20396. 0x93, 0x44, 0x5F, 0x39, 0xA5, 0x5F, 0xB9, 0x05,
  20397. 0xE6, 0x32, 0x1F, 0x4B, 0x5D, 0xD8, 0xBB, 0xF3,
  20398. 0x10, 0x0D, 0x51, 0xBA, 0x0B, 0xDA, 0x3D, 0x2D
  20399. };
  20400. static const byte sshKdfTvSet3sid[] = {
  20401. 0x7B, 0x70, 0x01, 0x18, 0x5E, 0x25, 0x6D, 0x44,
  20402. 0x93, 0x44, 0x5F, 0x39, 0xA5, 0x5F, 0xB9, 0x05,
  20403. 0xE6, 0x32, 0x1F, 0x4B, 0x5D, 0xD8, 0xBB, 0xF3,
  20404. 0x10, 0x0D, 0x51, 0xBA, 0x0B, 0xDA, 0x3D, 0x2D
  20405. };
  20406. static const byte sshKdfTvSet3a[] = {
  20407. 0x81, 0xF0, 0x33, 0x0E, 0xF6, 0xF0, 0x53, 0x61,
  20408. 0xB3, 0x82, 0x3B, 0xFD, 0xED, 0x6E, 0x1D, 0xE9
  20409. };
  20410. static const byte sshKdfTvSet3b[] = {
  20411. 0x3F, 0x6F, 0xD2, 0x06, 0x5E, 0xEB, 0x2B, 0x0B,
  20412. 0x1D, 0x93, 0x19, 0x5A, 0x1F, 0xED, 0x48, 0xA5
  20413. };
  20414. static const byte sshKdfTvSet3c[] = {
  20415. 0xC3, 0x54, 0x71, 0x03, 0x4E, 0x6F, 0xD6, 0x54,
  20416. 0x76, 0x13, 0x17, 0x8E, 0x23, 0x43, 0x5F, 0x21
  20417. };
  20418. static const byte sshKdfTvSet3d[] = {
  20419. 0x7E, 0x9D, 0x79, 0x03, 0x20, 0x90, 0xD9, 0x9F,
  20420. 0x98, 0xB0, 0x15, 0x63, 0x4D, 0xD9, 0xF4, 0x62
  20421. };
  20422. static const byte sshKdfTvSet3e[] = {
  20423. 0x24, 0xEE, 0x55, 0x9A, 0xD7, 0xCE, 0x71, 0x2B,
  20424. 0x68, 0x5D, 0x0B, 0x22, 0x71, 0xE4, 0x43, 0xC1,
  20425. 0x7A, 0xB1, 0xD1, 0xDC, 0xEB, 0x5A, 0x36, 0x05,
  20426. 0x69, 0xD2, 0x5D, 0x5D, 0xC2, 0x43, 0x00, 0x2F
  20427. };
  20428. static const byte sshKdfTvSet3f[] = {
  20429. 0xC3, 0x41, 0x9C, 0x2B, 0x96, 0x62, 0x35, 0x86,
  20430. 0x9D, 0x71, 0x4B, 0xA5, 0xAC, 0x48, 0xDD, 0xB7,
  20431. 0xD9, 0xE3, 0x5C, 0x8C, 0x19, 0xAA, 0xC7, 0x34,
  20432. 0x22, 0x33, 0x7A, 0x37, 0x34, 0x53, 0x60, 0x7E
  20433. };
  20434. static const SshKdfTestVector sshKdfTestVectors[] = {
  20435. {WC_HASH_TYPE_SHA256, 'A',
  20436. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  20437. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  20438. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  20439. sshKdfTvSet3a, sizeof(sshKdfTvSet3a)},
  20440. {WC_HASH_TYPE_SHA256, 'B',
  20441. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  20442. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  20443. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  20444. sshKdfTvSet3b, sizeof(sshKdfTvSet3b)},
  20445. {WC_HASH_TYPE_SHA256, 'C',
  20446. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  20447. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  20448. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  20449. sshKdfTvSet3c, sizeof(sshKdfTvSet3c)},
  20450. {WC_HASH_TYPE_SHA256, 'D',
  20451. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  20452. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  20453. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  20454. sshKdfTvSet3d, sizeof(sshKdfTvSet3d)},
  20455. {WC_HASH_TYPE_SHA256, 'E',
  20456. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  20457. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  20458. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  20459. sshKdfTvSet3e, sizeof(sshKdfTvSet3e)},
  20460. {WC_HASH_TYPE_SHA256, 'F',
  20461. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  20462. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  20463. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  20464. sshKdfTvSet3f, sizeof(sshKdfTvSet3f)},
  20465. };
  20466. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sshkdf_test(void)
  20467. {
  20468. wc_test_ret_t result = 0;
  20469. word32 i;
  20470. word32 tc = sizeof(sshKdfTestVectors)/sizeof(SshKdfTestVector);
  20471. const SshKdfTestVector* tv = NULL;
  20472. byte cKey[32]; /* Greater of SHA256_DIGEST_SIZE and AES_BLOCK_SIZE */
  20473. /* sId - Session ID, eKey - Expected Key, cKey - Calculated Key */
  20474. for (i = 0, tv = sshKdfTestVectors; i < tc; i++, tv++) {
  20475. result = wc_SSH_KDF(tv->hashId, tv->keyId,
  20476. cKey, tv->expectedKeySz,
  20477. tv->k, tv->kSz, tv->h, tv->hSz,
  20478. tv->sessionId, tv->sessionIdSz);
  20479. if (result != 0) {
  20480. printf("KDF: Could not derive key.\n");
  20481. result = WC_TEST_RET_ENC_EC(result);
  20482. }
  20483. else {
  20484. if (XMEMCMP(cKey, tv->expectedKey, tv->expectedKeySz) != 0) {
  20485. printf("KDF: Calculated Key does not match Expected Key.\n");
  20486. result = WC_TEST_RET_ENC_EC(result);
  20487. }
  20488. }
  20489. if (result != 0) break;
  20490. }
  20491. return result;
  20492. }
  20493. #endif /* WOLFSSL_WOLFSSH */
  20494. #ifdef WOLFSSL_TLS13
  20495. #define TLSV13_PSK_DHE_SZ 40
  20496. typedef struct {
  20497. enum wc_HashType hashAlg;
  20498. word32 pskSz;
  20499. word32 dheSz;
  20500. byte psk[TLSV13_PSK_DHE_SZ];
  20501. byte dhe[TLSV13_PSK_DHE_SZ];
  20502. byte hashHello1[WC_MAX_DIGEST_SIZE];
  20503. byte hashHello2[WC_MAX_DIGEST_SIZE];
  20504. byte hashFinished1[WC_MAX_DIGEST_SIZE];
  20505. byte hashFinished2[WC_MAX_DIGEST_SIZE];
  20506. /* Expected */
  20507. byte clientEarlyTrafficSecret[WC_MAX_DIGEST_SIZE];
  20508. byte earlyExporterMasterSecret[WC_MAX_DIGEST_SIZE];
  20509. byte clientHandshakeTrafficSecret[WC_MAX_DIGEST_SIZE];
  20510. byte serverHandshakeTrafficSecret[WC_MAX_DIGEST_SIZE];
  20511. byte clientApplicationTrafficSecret[WC_MAX_DIGEST_SIZE];
  20512. byte serverApplicationTrafficSecret[WC_MAX_DIGEST_SIZE];
  20513. byte exporterMasterSecret[WC_MAX_DIGEST_SIZE];
  20514. byte resumptionMasterSecret[WC_MAX_DIGEST_SIZE];
  20515. } Tls13KdfTestVector;
  20516. /* The following tests come from the CAVP test vectors we used for
  20517. * our FIPS validation. The hash values used are the components from
  20518. * the test hashed together. hashHello1 is the hash of the
  20519. * clientHelloRandom value of the test vector. hashHello2 is the hash
  20520. * of the clientHelloRandom and serverHelloRandom values from the test
  20521. * vector. hashFinished1 is clientHelloRandom, serverHelloRandom, and
  20522. * serverFinishedRandom. hashFinished2 is clientHelloRandom,
  20523. * serverHelloRandom, serverFinishedRandom, and clietnFinishedRandom
  20524. * hashed together. */
  20525. static const Tls13KdfTestVector tls13KdfTestVectors[] = {
  20526. { /* 1 */
  20527. WC_HASH_TYPE_SHA256, 35, 35,
  20528. { /* PSK */
  20529. 0x7b, 0xf1, 0x05, 0x31, 0x36, 0xfa, 0x03, 0xdc,
  20530. 0x31, 0x97, 0x88, 0x04, 0x9c, 0xbc, 0xee, 0xf7,
  20531. 0x8d, 0x84, 0x95, 0x26, 0xaf, 0x1d, 0x68, 0xb0,
  20532. 0x60, 0x7a, 0xcc, 0x4f, 0xc1, 0xd3, 0xa1, 0x68,
  20533. 0x7f, 0x6d, 0xbe
  20534. },
  20535. { /* DHE */
  20536. 0x6e, 0xa1, 0x77, 0xab, 0x2f, 0x43, 0xd2, 0x4b,
  20537. 0xe5, 0xa1, 0x09, 0xe0, 0x7a, 0xd0, 0x01, 0x35,
  20538. 0x8d, 0xf8, 0xf2, 0x5c, 0x91, 0x02, 0xb0, 0x6c,
  20539. 0x3f, 0xeb, 0xee, 0xa4, 0x42, 0x19, 0xce, 0xdc,
  20540. 0x81, 0x26, 0x40
  20541. },
  20542. { /* Hello 1 */
  20543. 0xd9, 0x4b, 0xe4, 0x17, 0xef, 0x58, 0x73, 0x7d,
  20544. 0x28, 0x3d, 0xf0, 0xcc, 0x05, 0x03, 0xaf, 0xac,
  20545. 0x3d, 0x92, 0x79, 0x48, 0xe8, 0x8c, 0xdb, 0xce,
  20546. 0x95, 0x82, 0x21, 0x31, 0x7b, 0x61, 0xd7, 0xc6
  20547. },
  20548. { /* Hello 2 */
  20549. 0xb7, 0x7f, 0x29, 0x91, 0xa4, 0x8b, 0x34, 0xdb,
  20550. 0xbd, 0xc7, 0x54, 0x1c, 0x3b, 0x86, 0xa3, 0x69,
  20551. 0xfe, 0x26, 0xe4, 0x7b, 0xac, 0x57, 0x71, 0xb3,
  20552. 0x32, 0x97, 0xed, 0xd2, 0x0e, 0x95, 0xb8, 0x63
  20553. },
  20554. { /* Finished 1 */
  20555. 0x65, 0xdb, 0x6d, 0x71, 0x71, 0xd0, 0xd8, 0x49,
  20556. 0xd0, 0x3c, 0x8e, 0x2b, 0x24, 0xdf, 0xc2, 0xe9,
  20557. 0xd6, 0xfd, 0xea, 0x04, 0x95, 0x7c, 0xf0, 0x7e,
  20558. 0x57, 0x74, 0x7c, 0xdd, 0xa3, 0x0b, 0x2b, 0x36
  20559. },
  20560. { /* Finished 2 */
  20561. 0x28, 0xf2, 0xf2, 0x79, 0xcf, 0x20, 0x52, 0x90,
  20562. 0x1d, 0x91, 0x05, 0xad, 0x44, 0x26, 0x23, 0x96,
  20563. 0x32, 0xce, 0xec, 0x61, 0xd1, 0xbf, 0x00, 0x48,
  20564. 0x4a, 0xa5, 0x60, 0xcc, 0x28, 0xb5, 0x8d, 0x98
  20565. },
  20566. { /* Client Early Traffic Secret */
  20567. 0x07, 0x14, 0x6a, 0x26, 0x5b, 0x6c, 0x7f, 0x4d, 0x6b, 0x47, 0x3f, 0xd5,
  20568. 0x03, 0x1d, 0xd2, 0x23, 0x3d, 0x89, 0x3e, 0xc6, 0x51, 0xd1, 0xac, 0xf8,
  20569. 0x28, 0xae, 0x4b, 0x76, 0xc8, 0x10, 0x7e, 0xdd
  20570. },
  20571. { /* Early Exporter Master Secret */
  20572. 0xb8, 0xd3, 0x25, 0x7e, 0x2d, 0x41, 0x7b, 0xcb, 0x5e, 0x82, 0x49, 0xf5,
  20573. 0x51, 0x3d, 0xb7, 0x59, 0x32, 0xb3, 0xdf, 0x99, 0x4e, 0x04, 0x69, 0xc6,
  20574. 0x96, 0x8e, 0xe6, 0x3d, 0x91, 0xe4, 0x81, 0x11
  20575. },
  20576. { /* Client Handshake Traffic Secret */
  20577. 0xd9, 0x3b, 0x54, 0xe2, 0xb0, 0xd1, 0x85, 0xf0, 0xfd, 0xf3, 0x48, 0x4a,
  20578. 0xf8, 0x0b, 0xa5, 0xdc, 0x4c, 0x37, 0xcb, 0xd4, 0x20, 0xaf, 0x60, 0xc7,
  20579. 0xd5, 0x50, 0x5d, 0x0c, 0x77, 0x3b, 0x6f, 0xd2
  20580. },
  20581. { /* Server Handshake Traffic Secret */
  20582. 0x4d, 0x40, 0x2b, 0xd2, 0x8c, 0x33, 0x90, 0x39, 0x67, 0x67, 0x05, 0xf7,
  20583. 0x5d, 0x37, 0x1e, 0xdc, 0x4a, 0x70, 0x6b, 0x9e, 0xf8, 0x06, 0x61, 0x89,
  20584. 0x70, 0xe1, 0x3d, 0x36, 0xad, 0x88, 0x7e, 0x5b
  20585. },
  20586. { /* Client Application Traffic Secret */
  20587. 0x74, 0x6e, 0xa0, 0x13, 0x18, 0x34, 0x48, 0x4d, 0x23, 0x31, 0xf1, 0xf9,
  20588. 0xee, 0x44, 0x6d, 0xad, 0xc1, 0xad, 0x92, 0x73, 0xca, 0x27, 0x16, 0x91,
  20589. 0xa2, 0x50, 0x9a, 0xfc, 0xec, 0xf0, 0x6b, 0x24
  20590. },
  20591. { /* Server Application Traffic Secret */
  20592. 0x89, 0x18, 0x7e, 0x34, 0x8d, 0xfc, 0x14, 0xb1, 0x4f, 0x21, 0xd8, 0x29,
  20593. 0xdb, 0x9b, 0xfb, 0x55, 0xcf, 0xa1, 0x4f, 0x95, 0xf8, 0xe0, 0xb0, 0x83,
  20594. 0xd5, 0x34, 0x9e, 0x0b, 0x83, 0x37, 0x42, 0x93
  20595. },
  20596. { /* Exporter Master Secret */
  20597. 0x7d, 0xc8, 0x88, 0x46, 0xd5, 0x57, 0x15, 0xb6, 0x24, 0x25, 0x92, 0x61,
  20598. 0xb1, 0x18, 0x86, 0x2a, 0x6d, 0xa5, 0x84, 0xeb, 0x59, 0xdf, 0x13, 0xbd,
  20599. 0x73, 0xaa, 0x5d, 0x65, 0xab, 0xd9, 0xb4, 0x56
  20600. },
  20601. { /* Resumption Master Secret */
  20602. 0x20, 0xb7, 0xd0, 0xe3, 0x82, 0x01, 0xa1, 0x04, 0xb8, 0x13, 0x29, 0xed,
  20603. 0x35, 0xe4, 0x2f, 0xbf, 0x58, 0x23, 0x7f, 0x21, 0xdb, 0x9f, 0xf8, 0xe0,
  20604. 0xe8, 0xe4, 0xab, 0xc4, 0xa1, 0x61, 0xb9, 0xbb
  20605. }
  20606. },
  20607. { /* 6 */
  20608. WC_HASH_TYPE_SHA256, 0, 33,
  20609. { 0 }, /* PSK */
  20610. { /* DHE */
  20611. 0x7a, 0x46, 0x8c, 0x5a, 0xd1, 0x8e, 0x95, 0xba,
  20612. 0x61, 0xe6, 0x6f, 0xe6, 0x76, 0x0c, 0x20, 0x43,
  20613. 0x16, 0x82, 0x15, 0xfe, 0x54, 0xa3, 0xc7, 0xfd,
  20614. 0x3b, 0x2c, 0x88, 0xb4, 0xd3, 0x42, 0x70, 0x12,
  20615. 0x18
  20616. },
  20617. { /* Hello 1 */
  20618. 0x63, 0x83, 0x58, 0xab, 0x36, 0xcd, 0x0c, 0xf3,
  20619. 0x26, 0x07, 0xb5, 0x5f, 0x0b, 0x8b, 0x45, 0xd6,
  20620. 0x7d, 0x5b, 0x42, 0xdc, 0xa8, 0xaa, 0x06, 0xfb,
  20621. 0x20, 0xa5, 0xbb, 0x85, 0xdb, 0x54, 0xd8, 0x8b
  20622. },
  20623. { /* Hello 2 */
  20624. 0xea, 0xfe, 0x9e, 0x8e, 0xff, 0x1f, 0x6f, 0x43,
  20625. 0xf9, 0x5d, 0xfd, 0xbf, 0xe2, 0x5f, 0x02, 0x2f,
  20626. 0x6d, 0x47, 0x60, 0x9a, 0x48, 0x9a, 0x75, 0xfb,
  20627. 0xb5, 0x4a, 0xbf, 0x9c, 0x4e, 0xff, 0xbf, 0x0b
  20628. },
  20629. { /* Finished 1 */
  20630. 0xca, 0x25, 0xb3, 0x53, 0x8e, 0x6d, 0xc3, 0x36,
  20631. 0x17, 0x30, 0x07, 0xdf, 0x0d, 0xd7, 0x79, 0xb0,
  20632. 0x7f, 0xcb, 0xbe, 0x7a, 0xbc, 0x2d, 0x9f, 0x2d,
  20633. 0x94, 0x44, 0x94, 0xe6, 0xa4, 0xf3, 0xe8, 0x53
  20634. },
  20635. { /* Finished 2 */
  20636. 0x2e, 0xa6, 0x5a, 0xaf, 0xb5, 0xba, 0x9f, 0x2f,
  20637. 0x74, 0x83, 0x5d, 0xbf, 0x86, 0xa4, 0xa6, 0xf6,
  20638. 0xb9, 0x89, 0xdf, 0x17, 0xe1, 0xa8, 0x14, 0xc0,
  20639. 0xe1, 0x50, 0xfa, 0xec, 0xfa, 0xae, 0x8b, 0x7b
  20640. },
  20641. {
  20642. 0x20, 0x18, 0x72, 0x7c, 0xde, 0x3a, 0x85, 0x17, 0x72, 0xdc, 0xd7, 0x72,
  20643. 0xb0, 0xfc, 0x45, 0xd0, 0x62, 0xb9, 0xbb, 0x38, 0x69, 0x05, 0x7b, 0xb4,
  20644. 0x5e, 0x58, 0x5d, 0xed, 0xcd, 0x0b, 0x96, 0xd3
  20645. },
  20646. {
  20647. 0x68, 0x10, 0x20, 0xd1, 0x5e, 0xfc, 0x0c, 0x53, 0x85, 0xbb, 0xdb, 0x18,
  20648. 0xa8, 0x78, 0xf1, 0x2b, 0x13, 0xba, 0x64, 0x1d, 0xe7, 0x09, 0xbe, 0x13,
  20649. 0x49, 0x26, 0xf9, 0x98, 0x56, 0xf1, 0x43, 0xfb
  20650. },
  20651. {
  20652. 0x24, 0x35, 0x3e, 0x10, 0x6f, 0x39, 0x50, 0xd6, 0xa2, 0x12, 0x99, 0xf2,
  20653. 0xd5, 0xf5, 0x19, 0xf5, 0x84, 0xed, 0xee, 0x78, 0x2a, 0xa6, 0xfa, 0x3d,
  20654. 0x06, 0xa8, 0xa7, 0x5d, 0x97, 0x78, 0xd6, 0x58
  20655. },
  20656. {
  20657. 0xf4, 0x57, 0xac, 0x24, 0x7a, 0xfb, 0x7c, 0x3b, 0xb6, 0x39, 0x17, 0x14,
  20658. 0xd9, 0xd4, 0x58, 0x4d, 0x46, 0xd5, 0x1b, 0xde, 0xf7, 0x9d, 0x06, 0xee,
  20659. 0x8d, 0x1a, 0x2c, 0x25, 0x6d, 0x64, 0xde, 0x89
  20660. },
  20661. {
  20662. 0xb6, 0x00, 0xce, 0x63, 0xed, 0x65, 0x8b, 0x66, 0x66, 0x42, 0xc6, 0xbd,
  20663. 0x89, 0xc4, 0x71, 0x6f, 0xce, 0x28, 0xb2, 0xac, 0x97, 0x07, 0x5b, 0xea,
  20664. 0xb8, 0x1d, 0x4c, 0xeb, 0x9e, 0x71, 0x07, 0x8f
  20665. },
  20666. {
  20667. 0xf8, 0x92, 0xc8, 0xba, 0xe7, 0x83, 0xfe, 0x68, 0xe4, 0xd6, 0x5e, 0xcb,
  20668. 0xb3, 0xef, 0x49, 0xd0, 0xe7, 0xb1, 0xac, 0xcb, 0x39, 0x19, 0xfd, 0xa7,
  20669. 0xf7, 0xca, 0xab, 0x1e, 0x42, 0x14, 0xd8, 0xe7
  20670. },
  20671. {
  20672. 0x32, 0x4a, 0x1a, 0xad, 0xe2, 0xbb, 0x55, 0x8a, 0xdd, 0xe9, 0xa5, 0x2a,
  20673. 0x46, 0x5e, 0x6c, 0x83, 0x66, 0x27, 0x27, 0x94, 0xdd, 0x68, 0x59, 0xa0,
  20674. 0xbb, 0xe8, 0x31, 0x7c, 0x39, 0xd7, 0xfd, 0x6d
  20675. },
  20676. {
  20677. 0x58, 0xbc, 0x6c, 0x5b, 0x24, 0xad, 0x82, 0xb3, 0xcc, 0xc7, 0xd1, 0xa1,
  20678. 0xaa, 0x2b, 0x98, 0x9f, 0x2f, 0x7e, 0xa9, 0x63, 0xc2, 0x8e, 0xb6, 0x06,
  20679. 0xc2, 0x2b, 0x74, 0x4b, 0x79, 0x19, 0x7e, 0x2e
  20680. }
  20681. },
  20682. { /* 11 */
  20683. WC_HASH_TYPE_SHA256, 33, 0,
  20684. { /* PSK */
  20685. 0x3d, 0x39, 0x49, 0x36, 0x98, 0xc5, 0xfd, 0xcd,
  20686. 0xa0, 0x17, 0xbd, 0x65, 0x0a, 0xdb, 0xd4, 0x07,
  20687. 0x56, 0xa2, 0x7b, 0xb8, 0x2a, 0x7e, 0xfb, 0x26,
  20688. 0x74, 0xe1, 0xbc, 0x08, 0x4b, 0xf0, 0x30, 0x14,
  20689. 0x12
  20690. },
  20691. { 0 }, /* DHE */
  20692. { /* Hello 1 */
  20693. 0xb7, 0x44, 0x74, 0x6c, 0x57, 0x1f, 0xf3, 0x84,
  20694. 0x8f, 0x63, 0xfb, 0x8c, 0x94, 0x6c, 0x16, 0x68,
  20695. 0x4b, 0xe1, 0xb5, 0xb5, 0x2a, 0x4e, 0x5f, 0xdf,
  20696. 0x4b, 0x53, 0xb2, 0x35, 0xfc, 0x30, 0xf1, 0x36
  20697. },
  20698. { /* Hello 2 */
  20699. 0xe6, 0x4f, 0x3a, 0x4f, 0xd7, 0xe0, 0x64, 0xd4,
  20700. 0x69, 0x50, 0xe4, 0x8b, 0xba, 0xbc, 0x47, 0x74,
  20701. 0xa7, 0x9b, 0x40, 0x91, 0x8f, 0xa8, 0x72, 0x22,
  20702. 0x97, 0xad, 0x43, 0xa7, 0x11, 0x86, 0xb5, 0x72
  20703. },
  20704. { /* Finished 1 */
  20705. 0x5f, 0xa6, 0x10, 0xe2, 0xa3, 0x99, 0x0b, 0x5e,
  20706. 0x57, 0xee, 0xc3, 0x3a, 0x8e, 0x04, 0xf3, 0x0e,
  20707. 0x58, 0x02, 0x09, 0xb2, 0x7e, 0x2d, 0xc6, 0xd2,
  20708. 0x08, 0xae, 0x68, 0x0a, 0x55, 0xa5, 0xda, 0x51
  20709. },
  20710. { /* Finished 2 */
  20711. 0xfc, 0x5b, 0xc0, 0x7e, 0x1b, 0xaa, 0xc0, 0xb4,
  20712. 0x34, 0x85, 0x49, 0x8e, 0x16, 0x31, 0x98, 0xdf,
  20713. 0x10, 0x54, 0x22, 0xda, 0x1e, 0x6b, 0x51, 0xf6,
  20714. 0x97, 0x57, 0xa0, 0x7a, 0x92, 0xe7, 0x47, 0x52
  20715. },
  20716. {
  20717. 0x80, 0xfa, 0x36, 0x30, 0xb8, 0x65, 0xb3, 0x2a, 0x1d, 0x68, 0x91, 0x06,
  20718. 0x98, 0xa0, 0x17, 0x8f, 0xee, 0xb7, 0x9e, 0x3d, 0xd8, 0x84, 0x99, 0x30,
  20719. 0xb9, 0xd6, 0x09, 0x25, 0x5e, 0xfb, 0x8f, 0xd3 },
  20720. {
  20721. 0xa9, 0x89, 0x29, 0x70, 0xe4, 0x55, 0xec, 0x97, 0xfb, 0x24, 0x5b, 0xf9,
  20722. 0xf1, 0xa3, 0x19, 0x3d, 0xf1, 0x31, 0x14, 0xcd, 0x2a, 0xed, 0x21, 0xc8,
  20723. 0xb1, 0x53, 0xad, 0x11, 0x0b, 0x9e, 0x5a, 0xee },
  20724. {
  20725. 0x72, 0xad, 0x8d, 0x7f, 0xfc, 0xb7, 0x68, 0xda, 0x27, 0x60, 0x37, 0xa3,
  20726. 0x4a, 0x63, 0xe8, 0xa5, 0xc8, 0xcd, 0x36, 0x6a, 0x77, 0x99, 0x0d, 0xa9,
  20727. 0xb1, 0x5b, 0x2f, 0x47, 0x2e, 0x22, 0xa7, 0x5e },
  20728. {
  20729. 0x95, 0x6e, 0x85, 0x09, 0xe5, 0x04, 0x88, 0x14, 0x28, 0x8d, 0xdf, 0xe6,
  20730. 0x0d, 0x0f, 0x0d, 0x6b, 0x4e, 0x66, 0x1c, 0x03, 0xb9, 0xaa, 0x2d, 0x45,
  20731. 0x56, 0x67, 0x5c, 0x55, 0x29, 0xd6, 0x89, 0xd0 },
  20732. {
  20733. 0xe8, 0xf2, 0x14, 0xf9, 0x9b, 0x2b, 0x9f, 0x24, 0x2b, 0x37, 0xbe, 0x86,
  20734. 0xdb, 0x23, 0x4b, 0xbe, 0x39, 0x57, 0xe8, 0xa9, 0xa5, 0xee, 0x08, 0xf2,
  20735. 0x75, 0x58, 0xdb, 0xd9, 0x51, 0xc1, 0x46, 0x02 },
  20736. {
  20737. 0x3d, 0x19, 0xaf, 0xa3, 0x0b, 0x21, 0xf7, 0x3d, 0xe7, 0x37, 0x6e, 0x32,
  20738. 0x13, 0x48, 0x9d, 0xea, 0xe0, 0x90, 0xbf, 0x64, 0x48, 0xf7, 0x1e, 0xcc,
  20739. 0xf0, 0xbc, 0x92, 0xd7, 0x8a, 0x4a, 0xa8, 0xc1 },
  20740. {
  20741. 0x16, 0x35, 0xb1, 0x66, 0x28, 0xa3, 0x3e, 0x19, 0xf5, 0x2d, 0x92, 0x22,
  20742. 0x95, 0x48, 0xe8, 0x34, 0x7b, 0x30, 0x50, 0xa2, 0xa0, 0xd9, 0xc2, 0x59,
  20743. 0x39, 0xf9, 0x8c, 0x69, 0xf2, 0x2a, 0xb9, 0xff },
  20744. {
  20745. 0x32, 0x71, 0xa6, 0x87, 0x0c, 0x97, 0x42, 0x07, 0xdd, 0x5f, 0xc9, 0x44,
  20746. 0xa5, 0x7c, 0x50, 0x14, 0xfd, 0xe7, 0x5f, 0x8b, 0xd3, 0x2f, 0xdc, 0x9b,
  20747. 0xa9, 0x93, 0x22, 0x19, 0xe6, 0xf2, 0x0c, 0xd8 }
  20748. },
  20749. #ifdef WOLFSSL_SHA384
  20750. { /* 26 */
  20751. WC_HASH_TYPE_SHA384, 35, 35,
  20752. { /* PSK */
  20753. 0x62, 0x83, 0x25, 0xc7, 0xcc, 0x08, 0x5e, 0x63,
  20754. 0x64, 0x56, 0xf0, 0xc6, 0x88, 0x27, 0x5a, 0x5b,
  20755. 0x68, 0x59, 0x0b, 0x14, 0x55, 0x13, 0x2e, 0xfd,
  20756. 0x8f, 0x28, 0x5b, 0x3d, 0xe3, 0xad, 0x67, 0xe4,
  20757. 0x68, 0xba, 0xf9
  20758. },
  20759. { /* DHE */
  20760. 0xa8, 0xb1, 0xab, 0xd8, 0xc8, 0x5b, 0x52, 0xdf,
  20761. 0x7f, 0x49, 0x10, 0xf4, 0xa1, 0x31, 0xd1, 0x91,
  20762. 0x36, 0xc1, 0x87, 0x5d, 0x42, 0x2a, 0xe7, 0x1d,
  20763. 0x2c, 0x29, 0x3d, 0x40, 0x64, 0x61, 0x63, 0x76,
  20764. 0xd8, 0x66, 0xac
  20765. },
  20766. { /* Hello 1 */
  20767. 0x6f, 0xc6, 0x4c, 0xe1, 0xc6, 0x68, 0x34, 0x8c,
  20768. 0x0a, 0xe1, 0xf8, 0xb8, 0x3e, 0xd4, 0xf8, 0x0b,
  20769. 0x54, 0x50, 0xe4, 0xc5, 0x4a, 0x33, 0x7d, 0xbd,
  20770. 0x90, 0xd2, 0xa2, 0xb9, 0xb7, 0x92, 0xed, 0xab,
  20771. 0x14, 0xf1, 0xe4, 0x86, 0x22, 0x67, 0xd7, 0x44,
  20772. 0x03, 0x21, 0xdc, 0x51, 0x52, 0x7f, 0x35, 0x80
  20773. },
  20774. { /* Hello 2 */
  20775. 0x3e, 0xcf, 0x2f, 0xc3, 0x87, 0xba, 0xc5, 0xbd,
  20776. 0x7c, 0xe8, 0x35, 0x5b, 0x95, 0x51, 0x30, 0x3b,
  20777. 0x08, 0xcc, 0x2a, 0x7d, 0xb5, 0x74, 0x7c, 0x16,
  20778. 0xb3, 0x0b, 0xe7, 0x61, 0xa3, 0x7c, 0x6c, 0xbd,
  20779. 0x39, 0x74, 0xfd, 0x1e, 0x4c, 0xff, 0xc8, 0xcc,
  20780. 0xa0, 0xef, 0x29, 0x4d, 0x94, 0xaa, 0x55, 0x6f,
  20781. },
  20782. { /* Finished 1 */
  20783. 0x06, 0xc1, 0x47, 0x78, 0x66, 0x53, 0x6f, 0x24,
  20784. 0x94, 0x61, 0x69, 0xec, 0xd8, 0x60, 0x31, 0x2f,
  20785. 0xbf, 0xd6, 0x8a, 0x29, 0x17, 0xff, 0xa3, 0x88,
  20786. 0x13, 0x09, 0x8c, 0x9d, 0x6c, 0x64, 0x84, 0x48,
  20787. 0x44, 0xdd, 0x2d, 0x29, 0x4d, 0xe6, 0x98, 0x2b,
  20788. 0x45, 0x3b, 0x84, 0x33, 0x79, 0xb2, 0x75, 0x68
  20789. },
  20790. { /* Finished 2 */
  20791. 0x28, 0x1e, 0x18, 0xf7, 0x9c, 0x32, 0xa9, 0xbf,
  20792. 0x0c, 0x24, 0x58, 0x21, 0xce, 0xbc, 0xf2, 0x44,
  20793. 0xb1, 0x18, 0xaf, 0x9d, 0xd9, 0x20, 0xf9, 0xf4,
  20794. 0xed, 0xcc, 0x53, 0x82, 0x66, 0x5c, 0x46, 0x94,
  20795. 0x8c, 0x36, 0x5e, 0xca, 0x9f, 0xd8, 0x9a, 0xd3,
  20796. 0xf0, 0xe1, 0x53, 0x71, 0xdd, 0x19, 0x1e, 0x59
  20797. },
  20798. {
  20799. 0xd0, 0xef, 0xa8, 0xcb, 0x5b, 0x14, 0x0f, 0x0a, 0x62, 0xba, 0x5a, 0xb1,
  20800. 0xc5, 0xb5, 0x3f, 0x11, 0xda, 0xa1, 0x0c, 0x9c, 0xb4, 0x32, 0x48, 0x4e,
  20801. 0xfa, 0x84, 0x4f, 0xe4, 0xe7, 0x91, 0x8f, 0x42, 0x3f, 0xc7, 0x4e, 0xd3,
  20802. 0x83, 0x3d, 0x7f, 0x70, 0x12, 0xee, 0x9a, 0x37, 0x01, 0xbb, 0x14, 0xd3
  20803. },
  20804. {
  20805. 0x48, 0x6f, 0x77, 0x1d, 0x39, 0x1b, 0xa5, 0x9a, 0x76, 0xd9, 0x1d, 0x7d,
  20806. 0xb3, 0xd9, 0xb9, 0x78, 0x35, 0x0f, 0xd0, 0xe1, 0x07, 0x1f, 0x8d, 0xe5,
  20807. 0x75, 0x00, 0xda, 0xc0, 0x19, 0x01, 0xfb, 0x08, 0x35, 0xe7, 0x18, 0x8f,
  20808. 0xf0, 0x19, 0xfb, 0x46, 0xf6, 0xa5, 0x77, 0x0e, 0x90, 0x38, 0x8b, 0x15
  20809. },
  20810. {
  20811. 0x80, 0x8c, 0xa7, 0x24, 0x97, 0xf9, 0xd3, 0x52, 0xb0, 0x69, 0x9d, 0x4b,
  20812. 0xa4, 0x19, 0x4a, 0xb1, 0x46, 0x53, 0x3a, 0xc8, 0xe4, 0x02, 0x69, 0xf2,
  20813. 0xe7, 0xb6, 0x1d, 0x33, 0x51, 0xcc, 0x14, 0x40, 0x4a, 0xb0, 0xe7, 0x58,
  20814. 0x84, 0xba, 0xc2, 0x14, 0x58, 0x6b, 0xb9, 0xdc, 0x50, 0x98, 0x67, 0x01
  20815. },
  20816. {
  20817. 0xb1, 0xa8, 0xc0, 0x06, 0xb3, 0x2e, 0xa7, 0x8a, 0x6a, 0x12, 0x88, 0x00,
  20818. 0x65, 0x88, 0x9c, 0x5d, 0x35, 0xee, 0xe5, 0x51, 0x0b, 0x62, 0xf8, 0x67,
  20819. 0xe5, 0xef, 0x15, 0x1f, 0x23, 0x02, 0x74, 0x08, 0x9c, 0xc8, 0xba, 0x27,
  20820. 0x5d, 0x32, 0x19, 0x6f, 0x6d, 0x5d, 0x72, 0x5e, 0x15, 0xde, 0x30, 0xc3
  20821. },
  20822. {
  20823. 0xfd, 0xce, 0xf5, 0x65, 0x45, 0x84, 0xfb, 0x8c, 0x79, 0xa4, 0x6c, 0x1b,
  20824. 0x0e, 0x1b, 0xfd, 0x26, 0xa2, 0x53, 0xf4, 0x4e, 0x00, 0x4d, 0x4b, 0x0b,
  20825. 0x24, 0x6d, 0x35, 0x35, 0xd9, 0x97, 0x70, 0xc5, 0xf4, 0xee, 0xe3, 0xba,
  20826. 0x31, 0x1e, 0x2a, 0x42, 0xcb, 0xdf, 0x40, 0xb1, 0x14, 0xb8, 0x53, 0xce
  20827. },
  20828. {
  20829. 0xbb, 0xb3, 0x26, 0x7c, 0x22, 0x21, 0x9b, 0x72, 0x32, 0xa1, 0x97, 0xfb,
  20830. 0x78, 0x8c, 0xbe, 0x3d, 0x71, 0x45, 0xb8, 0xf5, 0x24, 0x8f, 0x0f, 0xac,
  20831. 0x42, 0x5b, 0x81, 0xe8, 0xd0, 0x71, 0x4a, 0xcb, 0x32, 0x3f, 0x03, 0xfb,
  20832. 0xec, 0x6a, 0x1f, 0x76, 0x80, 0x65, 0x01, 0x7a, 0x3d, 0xce, 0xc4, 0xdf
  20833. },
  20834. {
  20835. 0x3f, 0xcf, 0x2f, 0x63, 0x94, 0x94, 0x99, 0xfd, 0x04, 0x3a, 0x89, 0x83,
  20836. 0xcf, 0x06, 0x05, 0xec, 0x20, 0x3e, 0x5f, 0x51, 0x9d, 0x6e, 0x4a, 0xc6,
  20837. 0xf1, 0x2b, 0x37, 0x17, 0x34, 0x72, 0x6e, 0x1d, 0x2a, 0xfd, 0xc7, 0x73,
  20838. 0xb5, 0x07, 0x22, 0x81, 0x32, 0x2e, 0x21, 0x85, 0xaf, 0x10, 0xb2, 0x73
  20839. },
  20840. {
  20841. 0x52, 0x0c, 0x3d, 0x2e, 0x2d, 0x4a, 0x11, 0xae, 0x96, 0x78, 0xe9, 0x5b,
  20842. 0xd8, 0x0f, 0x6c, 0xf4, 0xbd, 0x96, 0x13, 0x55, 0x88, 0xdd, 0xa3, 0x67,
  20843. 0x36, 0x86, 0x1e, 0x0b, 0x36, 0x41, 0xec, 0xf6, 0x04, 0xb2, 0xc4, 0x16,
  20844. 0xbc, 0x2c, 0xdb, 0x30, 0x02, 0x94, 0xd4, 0x42, 0xbf, 0x38, 0xee, 0x9d
  20845. }
  20846. },
  20847. { /* 36 */
  20848. WC_HASH_TYPE_SHA384, 0, 33,
  20849. { 0 }, /* PSK */
  20850. { /* DHE */
  20851. 0xd3, 0x00, 0x72, 0x9a, 0xa8, 0xc5, 0xf3, 0xc4,
  20852. 0xf1, 0xa0, 0x26, 0x89, 0x65, 0x70, 0xc7, 0x0b,
  20853. 0x77, 0xbb, 0xe1, 0x4b, 0x2b, 0xa8, 0x4f, 0xa6,
  20854. 0x09, 0x4b, 0xba, 0x45, 0x36, 0x15, 0xee, 0x68,
  20855. 0xfd
  20856. },
  20857. { /* Hello 1 */
  20858. 0x10, 0x9d, 0x8b, 0xa2, 0x93, 0xe7, 0xd3, 0xb9,
  20859. 0xb4, 0x0f, 0xeb, 0x6a, 0xb9, 0x69, 0xcb, 0x39,
  20860. 0x16, 0x29, 0xcc, 0xd3, 0xcc, 0x1a, 0x4c, 0x1b,
  20861. 0x53, 0x7c, 0x33, 0x88, 0x06, 0xbc, 0x0a, 0x02,
  20862. 0xa0, 0xbe, 0x62, 0xc0, 0xe6, 0x5e, 0x97, 0x5b,
  20863. 0x6a, 0xa1, 0x98, 0xf3, 0xd2, 0x1e, 0xcd, 0xc5
  20864. },
  20865. { /* Hello 2 */
  20866. 0x74, 0xc0, 0x07, 0x2c, 0xc1, 0x63, 0xcc, 0x11,
  20867. 0xad, 0x1a, 0x55, 0x63, 0xbc, 0x20, 0x77, 0x96,
  20868. 0x30, 0x1c, 0x68, 0x45, 0x1e, 0x9b, 0xa7, 0xb4,
  20869. 0xf3, 0x04, 0x45, 0x16, 0x76, 0x55, 0xf9, 0xdf,
  20870. 0x4b, 0x2f, 0x1a, 0xdf, 0x5a, 0xb0, 0x93, 0xc9,
  20871. 0xab, 0xf5, 0x32, 0x47, 0x79, 0x9c, 0x01, 0xeb
  20872. },
  20873. { /* Finished 1 */
  20874. 0x27, 0x08, 0x8e, 0xa5, 0xf1, 0x30, 0xe1, 0xd6,
  20875. 0x4f, 0xa2, 0x9e, 0x3b, 0x03, 0x2d, 0x2e, 0xa3,
  20876. 0x84, 0x75, 0x51, 0x3a, 0xc3, 0xf6, 0xee, 0x2e,
  20877. 0x37, 0x0c, 0xe3, 0x28, 0x46, 0xa5, 0x2d, 0xc7,
  20878. 0xf0, 0x64, 0x78, 0x53, 0x66, 0x43, 0x02, 0xa4,
  20879. 0x7a, 0x43, 0x66, 0x4b, 0xa7, 0xcb, 0x97, 0x16
  20880. },
  20881. { /* Finished 2 */
  20882. 0x1d, 0x0d, 0xf8, 0xe1, 0x81, 0xa5, 0xbd, 0xa8,
  20883. 0x6f, 0x9d, 0x01, 0xa4, 0x9a, 0x92, 0xe2, 0xef,
  20884. 0x08, 0xab, 0xef, 0x3e, 0x2d, 0xd4, 0x82, 0xac,
  20885. 0x68, 0x9d, 0xe0, 0x54, 0x17, 0xde, 0x1a, 0xed,
  20886. 0x57, 0xcb, 0xd9, 0x2d, 0xc8, 0xbc, 0x93, 0xe6,
  20887. 0xa3, 0xec, 0xde, 0xee, 0xa1, 0x1c, 0x41, 0x85
  20888. },
  20889. {
  20890. 0x7f, 0x1f, 0xe6, 0x7b, 0xd8, 0xf5, 0x2b, 0x37, 0xbe, 0xb7, 0xd0, 0x37,
  20891. 0xce, 0x46, 0xad, 0x04, 0x2f, 0xc7, 0xdb, 0xc9, 0x9a, 0xb6, 0x00, 0x3f,
  20892. 0xc1, 0x97, 0xe9, 0x5c, 0x5e, 0x14, 0xd1, 0x38, 0x4d, 0x55, 0xe1, 0x07,
  20893. 0xb5, 0x85, 0x6d, 0xfa, 0xa7, 0x66, 0xad, 0xfa, 0xb6, 0xad, 0x29, 0x44
  20894. },
  20895. {
  20896. 0x4e, 0x6b, 0x20, 0x99, 0x55, 0x1b, 0x21, 0x89, 0xb6, 0x70, 0xdb, 0xe8,
  20897. 0xa7, 0x16, 0x55, 0xf2, 0x93, 0x13, 0x90, 0x7d, 0xfa, 0x62, 0x65, 0x53,
  20898. 0xa0, 0x97, 0xe9, 0xb4, 0xc0, 0xf1, 0xc9, 0x1a, 0x67, 0xdd, 0xca, 0x57,
  20899. 0xbc, 0xca, 0x39, 0xe6, 0x39, 0x6b, 0x63, 0x47, 0x25, 0x08, 0x3a, 0xd7
  20900. },
  20901. {
  20902. 0x35, 0x0d, 0xac, 0xd8, 0x10, 0x6a, 0x46, 0x50, 0x66, 0xae, 0x02, 0xc9,
  20903. 0xde, 0x13, 0x48, 0xce, 0x53, 0xd4, 0x92, 0x62, 0xc5, 0x65, 0x10, 0x08,
  20904. 0xc2, 0xc2, 0x82, 0xed, 0x9d, 0xc9, 0x6f, 0xa8, 0xc3, 0xc1, 0x0b, 0x7c,
  20905. 0xe1, 0x97, 0x85, 0xd6, 0x46, 0x29, 0x0e, 0x42, 0x51, 0xc1, 0x35, 0xcf
  20906. },
  20907. {
  20908. 0x3d, 0x5d, 0x84, 0xbd, 0x16, 0x46, 0x34, 0xb3, 0xf6, 0x31, 0x49, 0x3e,
  20909. 0x8d, 0xdc, 0xcb, 0x8c, 0x6a, 0x42, 0xf4, 0x88, 0xfc, 0x19, 0xfa, 0xa2,
  20910. 0x25, 0xc7, 0xa0, 0xa4, 0xca, 0xf0, 0xea, 0x2d, 0xe8, 0xc4, 0x02, 0x14,
  20911. 0x63, 0xfb, 0xd3, 0x7b, 0x51, 0x1c, 0xce, 0xca, 0xa3, 0xc3, 0xe4, 0xa5
  20912. },
  20913. {
  20914. 0x7c, 0x3a, 0x55, 0x92, 0x2e, 0xdd, 0x75, 0xdd, 0x76, 0x54, 0x4a, 0x9f,
  20915. 0xd0, 0xa2, 0x88, 0x83, 0xe9, 0x27, 0xda, 0x30, 0xe9, 0x96, 0x58, 0xc5,
  20916. 0xb7, 0x56, 0xfc, 0x4b, 0xb8, 0x5d, 0xee, 0x46, 0x70, 0x4e, 0x1b, 0x06,
  20917. 0x86, 0xaf, 0x48, 0x5c, 0x17, 0x35, 0xfa, 0x69, 0xc2, 0x4d, 0xfb, 0x09
  20918. },
  20919. {
  20920. 0x00, 0x0e, 0x28, 0x51, 0xc1, 0x7f, 0x41, 0x89, 0x6f, 0x9a, 0xca, 0x15,
  20921. 0xee, 0xed, 0x43, 0xca, 0x6d, 0x65, 0x6f, 0x51, 0x18, 0x6c, 0x08, 0x4b,
  20922. 0x77, 0xca, 0x75, 0xc4, 0xc3, 0xde, 0x29, 0x41, 0x8b, 0xaf, 0xa7, 0x1c,
  20923. 0x28, 0x37, 0xa0, 0xa0, 0x74, 0x8e, 0x09, 0x42, 0x7a, 0x1b, 0x68, 0xdb
  20924. },
  20925. {
  20926. 0x14, 0x8f, 0xab, 0x28, 0x64, 0xea, 0x45, 0x88, 0xdb, 0xc1, 0xc6, 0xa0,
  20927. 0x48, 0xdf, 0x15, 0xd0, 0x28, 0x07, 0x2d, 0x6c, 0xb8, 0x42, 0xbb, 0x60,
  20928. 0x02, 0x08, 0x9e, 0x29, 0x9b, 0x8d, 0xd6, 0x1c, 0xaf, 0xf2, 0x1a, 0xdc,
  20929. 0xf0, 0x78, 0x0b, 0x4d, 0x90, 0xa1, 0x0c, 0xb3, 0x13, 0xde, 0xca, 0x5a
  20930. },
  20931. {
  20932. 0x4d, 0x80, 0x7d, 0x0b, 0xb9, 0x00, 0x6f, 0x65, 0x51, 0x65, 0x23, 0xde,
  20933. 0x72, 0xdc, 0x4f, 0x04, 0xa5, 0xa2, 0x90, 0x45, 0x51, 0x9e, 0xd0, 0x3a,
  20934. 0xe4, 0xd7, 0x78, 0xa3, 0x0f, 0x2d, 0x65, 0x12, 0xad, 0xc8, 0x92, 0x30,
  20935. 0x79, 0x9d, 0x9d, 0x08, 0x7a, 0x9c, 0x9f, 0x83, 0xb1, 0xca, 0x59, 0x56
  20936. }
  20937. },
  20938. { /* 41 */
  20939. WC_HASH_TYPE_SHA384, 33, 0,
  20940. { /* PSK */
  20941. 0xa4, 0x8b, 0x1b, 0x5f, 0xd0, 0xea, 0x75, 0x62,
  20942. 0x06, 0x4d, 0x68, 0x40, 0x85, 0x20, 0x45, 0x95,
  20943. 0x4a, 0x00, 0xca, 0x05, 0xeb, 0xd4, 0x1d, 0x48,
  20944. 0x81, 0x89, 0xe8, 0x86, 0x43, 0xfa, 0x28, 0x17,
  20945. 0x12
  20946. },
  20947. { 0 }, /* DHE */
  20948. { /* Hello 1 */
  20949. 0x03, 0x7c, 0x33, 0x75, 0xdc, 0xc5, 0x46, 0x3a,
  20950. 0x0d, 0x56, 0xc6, 0xfb, 0xab, 0x1e, 0x1d, 0xda,
  20951. 0x59, 0xc2, 0xb2, 0xb1, 0x7c, 0x48, 0x9b, 0x06,
  20952. 0x0a, 0x5a, 0xbb, 0xf8, 0x98, 0x53, 0x78, 0x2d,
  20953. 0xd2, 0xcc, 0x87, 0x68, 0x25, 0xdd, 0x88, 0x22,
  20954. 0xcd, 0xb7, 0x74, 0x55, 0x21, 0xf9, 0x34, 0x98
  20955. },
  20956. { /* Hello 2 */
  20957. 0x03, 0xb4, 0xfb, 0xcc, 0x28, 0x2c, 0xc1, 0x70,
  20958. 0x42, 0x73, 0x57, 0xac, 0xdb, 0x47, 0x71, 0xf6,
  20959. 0x2e, 0x11, 0x8a, 0x5b, 0x47, 0x2f, 0x02, 0x54,
  20960. 0x95, 0x34, 0xed, 0x5f, 0x19, 0xc1, 0x75, 0xe0,
  20961. 0x76, 0xad, 0xb0, 0x90, 0x57, 0xcd, 0xfd, 0xd7,
  20962. 0x58, 0x1f, 0x0d, 0x6b, 0x9e, 0x51, 0x3c, 0x08
  20963. },
  20964. { /* Finished 1 */
  20965. 0x2b, 0x50, 0xd9, 0xa7, 0x43, 0x24, 0xda, 0x2c,
  20966. 0x7a, 0xaa, 0x0e, 0x37, 0xd7, 0x6b, 0x2c, 0xab,
  20967. 0x8e, 0xb2, 0xfe, 0x31, 0x1b, 0xa8, 0x12, 0x59,
  20968. 0x5b, 0x7b, 0xdc, 0x3e, 0xa7, 0x86, 0xa5, 0x48,
  20969. 0xe4, 0x46, 0x2b, 0x4c, 0xc1, 0x66, 0x4b, 0xf3,
  20970. 0x2a, 0x99, 0x93, 0x08, 0xbc, 0x3d, 0x08, 0x76
  20971. },
  20972. { /* Finished 2 */
  20973. 0x7c, 0x34, 0xc8, 0x56, 0x17, 0xf1, 0x62, 0x1c,
  20974. 0x9f, 0x0b, 0xeb, 0xfd, 0x69, 0x72, 0x51, 0xc5,
  20975. 0xfa, 0x74, 0x87, 0xc9, 0xbd, 0x50, 0xe9, 0x48,
  20976. 0xa7, 0x3c, 0x94, 0x3e, 0x06, 0x7d, 0xe8, 0x8e,
  20977. 0xc1, 0xd1, 0x08, 0x1f, 0x5d, 0x48, 0x8a, 0x25,
  20978. 0xfc, 0xea, 0xe7, 0xd9, 0xd4, 0xd0, 0xf9, 0xad
  20979. },
  20980. {
  20981. 0x4b, 0x0b, 0xed, 0xb9, 0xc8, 0xb8, 0xa8, 0x1e, 0xb0, 0x81, 0x76, 0xd5,
  20982. 0x33, 0x22, 0x71, 0x33, 0x3a, 0x85, 0x19, 0x67, 0x7e, 0x91, 0x37, 0xf2,
  20983. 0xa6, 0x11, 0x22, 0xdf, 0x41, 0x04, 0x3d, 0xa9, 0x13, 0xb9, 0xb2, 0xb1,
  20984. 0xbb, 0xd8, 0xef, 0x23, 0x7c, 0xc2, 0xab, 0x70, 0x1b, 0x51, 0x9f, 0xc9
  20985. },
  20986. {
  20987. 0xeb, 0x96, 0x10, 0x8c, 0x7d, 0x92, 0xea, 0x80, 0x86, 0xb2, 0xf8, 0x27,
  20988. 0xf2, 0x9a, 0x09, 0xc1, 0x7c, 0x09, 0x43, 0xbc, 0xfe, 0xc8, 0x75, 0xe0,
  20989. 0x97, 0xe7, 0x6d, 0xd5, 0xb2, 0x3c, 0xed, 0x12, 0xb7, 0x74, 0x0e, 0xe3,
  20990. 0xb6, 0xe0, 0xba, 0xe1, 0x8d, 0x89, 0xcf, 0x4f, 0x57, 0xf6, 0x6d, 0x90
  20991. },
  20992. {
  20993. 0x22, 0xb0, 0x39, 0x34, 0xb6, 0x6c, 0x2d, 0x7a, 0x97, 0x1c, 0x5d, 0xcc,
  20994. 0x78, 0x84, 0x71, 0xbb, 0xc6, 0x7b, 0xb6, 0xbc, 0xcc, 0x0b, 0xf8, 0xac,
  20995. 0x8e, 0xd7, 0x20, 0xbd, 0xbe, 0x32, 0xf0, 0xd6, 0xe9, 0x69, 0x13, 0xf2,
  20996. 0x9a, 0xce, 0xfe, 0x86, 0xd3, 0xee, 0xba, 0x69, 0x51, 0xb6, 0x77, 0x56
  20997. },
  20998. {
  20999. 0x16, 0xfd, 0xda, 0xf3, 0x5e, 0xb9, 0xa6, 0x17, 0x24, 0xb2, 0x16, 0x9f,
  21000. 0xb6, 0x59, 0x13, 0x0f, 0x25, 0x5a, 0xf1, 0x5b, 0x5f, 0xe4, 0x54, 0x2a,
  21001. 0xa7, 0xbf, 0x29, 0xaf, 0x5a, 0x77, 0xf4, 0x4f, 0x25, 0xba, 0x94, 0xad,
  21002. 0x6b, 0x91, 0x3b, 0xe7, 0xd5, 0x73, 0x0d, 0xff, 0xaa, 0xe3, 0x72, 0x2c
  21003. },
  21004. {
  21005. 0x22, 0xb4, 0x94, 0xc0, 0x53, 0xd7, 0x82, 0x06, 0x38, 0x9d, 0x4a, 0xa0,
  21006. 0x3f, 0xf1, 0x5f, 0x6e, 0x23, 0x8d, 0x09, 0x62, 0xbf, 0x6f, 0x7c, 0x84,
  21007. 0xc6, 0x3e, 0x15, 0xad, 0x18, 0x37, 0x76, 0x29, 0xc7, 0xd6, 0x68, 0x0c,
  21008. 0x1e, 0xc6, 0x93, 0x31, 0xef, 0x85, 0x69, 0x30, 0x68, 0xf0, 0x1e, 0x37
  21009. },
  21010. {
  21011. 0x6d, 0x4d, 0x20, 0xaf, 0x47, 0xe8, 0x1b, 0xfa, 0xd0, 0xb6, 0xc8, 0x97,
  21012. 0xd1, 0x03, 0xfc, 0x9d, 0x59, 0xa0, 0x68, 0x9d, 0xe9, 0x17, 0x8b, 0xce,
  21013. 0x48, 0x2c, 0x77, 0x8a, 0x22, 0x4b, 0x5c, 0x54, 0x22, 0xa1, 0x15, 0x12,
  21014. 0xe1, 0x07, 0x8e, 0x15, 0xd8, 0x7b, 0x16, 0x65, 0x99, 0x6b, 0xcb, 0x71
  21015. },
  21016. {
  21017. 0x79, 0x64, 0x79, 0xdd, 0x75, 0x5c, 0x6f, 0x98, 0xac, 0x03, 0xe0, 0xcd,
  21018. 0x92, 0xba, 0x0e, 0x2d, 0xb4, 0xd1, 0x8b, 0x97, 0xd0, 0x85, 0xbb, 0x2e,
  21019. 0x4f, 0x26, 0x93, 0xf5, 0x1d, 0xf3, 0xd2, 0x43, 0x4f, 0xd2, 0x47, 0xaa,
  21020. 0x91, 0x1e, 0xf3, 0x67, 0x10, 0x18, 0x2c, 0xb9, 0x01, 0xba, 0x10, 0x9f
  21021. },
  21022. {
  21023. 0x79, 0xb6, 0x9c, 0xbe, 0xf1, 0x6a, 0xb0, 0x92, 0xa0, 0x29, 0x52, 0x61,
  21024. 0xf1, 0xcd, 0x3a, 0x67, 0xe1, 0x6b, 0xb8, 0x9d, 0x0d, 0x95, 0xb6, 0x03,
  21025. 0x80, 0x1f, 0xd5, 0x75, 0xb6, 0x1d, 0x79, 0x02, 0x93, 0x43, 0x77, 0xa7,
  21026. 0x9d, 0x2f, 0xc3, 0x84, 0xc6, 0x83, 0x76, 0x16, 0x06, 0x98, 0x7b, 0x79
  21027. }
  21028. },
  21029. #endif /* WOLFSSL_SHA384 */
  21030. };
  21031. static const char protocolLabel[] = "tls13 ";
  21032. static const char ceTrafficLabel[] = "c e traffic";
  21033. static const char eExpMasterLabel[] = "e exp master";
  21034. static const char cHsTrafficLabel[] = "c hs traffic";
  21035. static const char sHsTrafficLabel[] = "s hs traffic";
  21036. static const char cAppTrafficLabel[] = "c ap traffic";
  21037. static const char sAppTrafficLabel[] = "s ap traffic";
  21038. static const char expMasterLabel[] = "exp master";
  21039. static const char resMasterLabel[] = "res master";
  21040. static const char derivedLabel[] = "derived";
  21041. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t tls13_kdf_test(void)
  21042. {
  21043. wc_test_ret_t ret = 0;
  21044. word32 i;
  21045. word32 tc = sizeof(tls13KdfTestVectors)/sizeof(Tls13KdfTestVector);
  21046. const Tls13KdfTestVector* tv = NULL;
  21047. for (i = 0, tv = tls13KdfTestVectors; i < tc; i++, tv++) {
  21048. byte output[WC_MAX_DIGEST_SIZE];
  21049. byte secret[WC_MAX_DIGEST_SIZE];
  21050. byte salt[WC_MAX_DIGEST_SIZE];
  21051. byte zeroes[WC_MAX_DIGEST_SIZE];
  21052. byte hashZero[WC_MAX_DIGEST_SIZE];
  21053. int hashAlgSz;
  21054. XMEMSET(zeroes, 0, sizeof zeroes);
  21055. hashAlgSz = wc_HashGetDigestSize(tv->hashAlg);
  21056. if (hashAlgSz == BAD_FUNC_ARG) break;
  21057. ret = wc_Hash(tv->hashAlg, NULL, 0, hashZero, hashAlgSz);
  21058. if (ret != 0) break;
  21059. ret = wc_Tls13_HKDF_Extract(secret, NULL, 0,
  21060. (tv->pskSz == 0) ? zeroes : (byte*)tv->psk,
  21061. tv->pskSz, tv->hashAlg);
  21062. if (ret != 0) break;
  21063. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  21064. secret, hashAlgSz,
  21065. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  21066. (byte*)ceTrafficLabel, (word32)XSTRLEN(ceTrafficLabel),
  21067. tv->hashHello1, hashAlgSz, tv->hashAlg);
  21068. if (ret != 0) break;
  21069. ret = XMEMCMP(tv->clientEarlyTrafficSecret, output, hashAlgSz);
  21070. if (ret != 0) break;
  21071. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  21072. secret, hashAlgSz,
  21073. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  21074. (byte*)eExpMasterLabel, (word32)XSTRLEN(eExpMasterLabel),
  21075. tv->hashHello1, hashAlgSz, tv->hashAlg);
  21076. if (ret != 0) break;
  21077. ret = XMEMCMP(tv->earlyExporterMasterSecret, output, hashAlgSz);
  21078. if (ret != 0) break;
  21079. ret = wc_Tls13_HKDF_Expand_Label(salt, hashAlgSz,
  21080. secret, hashAlgSz,
  21081. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  21082. (byte*)derivedLabel, (word32)XSTRLEN(derivedLabel),
  21083. hashZero, hashAlgSz, tv->hashAlg);
  21084. if (ret != 0) break;
  21085. ret = wc_Tls13_HKDF_Extract(secret, salt, hashAlgSz,
  21086. (tv->dheSz == 0) ? zeroes : (byte*)tv->dhe,
  21087. tv->dheSz, tv->hashAlg);
  21088. if (ret != 0) break;
  21089. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  21090. secret, hashAlgSz,
  21091. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  21092. (byte*)cHsTrafficLabel, (word32)XSTRLEN(cHsTrafficLabel),
  21093. tv->hashHello2, hashAlgSz, tv->hashAlg);
  21094. if (ret != 0) break;
  21095. ret = XMEMCMP(tv->clientHandshakeTrafficSecret,
  21096. output, hashAlgSz);
  21097. if (ret != 0) break;
  21098. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  21099. secret, hashAlgSz,
  21100. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  21101. (byte*)sHsTrafficLabel, (word32)XSTRLEN(sHsTrafficLabel),
  21102. tv->hashHello2, hashAlgSz, tv->hashAlg);
  21103. if (ret != 0) break;
  21104. ret = XMEMCMP(tv->serverHandshakeTrafficSecret, output, hashAlgSz);
  21105. if (ret != 0) break;
  21106. ret = wc_Tls13_HKDF_Expand_Label(salt, hashAlgSz,
  21107. secret, hashAlgSz,
  21108. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  21109. (byte*)derivedLabel, (word32)XSTRLEN(derivedLabel),
  21110. hashZero, hashAlgSz, tv->hashAlg);
  21111. if (ret != 0) break;
  21112. ret = wc_Tls13_HKDF_Extract(secret, salt, hashAlgSz,
  21113. zeroes, hashAlgSz, tv->hashAlg);
  21114. if (ret != 0) break;
  21115. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  21116. secret, hashAlgSz,
  21117. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  21118. (byte*)cAppTrafficLabel, (word32)XSTRLEN(cAppTrafficLabel),
  21119. tv->hashFinished1, hashAlgSz, tv->hashAlg);
  21120. if (ret != 0) break;
  21121. ret = XMEMCMP(tv->clientApplicationTrafficSecret, output, hashAlgSz);
  21122. if (ret != 0) break;
  21123. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  21124. secret, hashAlgSz,
  21125. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  21126. (byte*)sAppTrafficLabel, (word32)XSTRLEN(sAppTrafficLabel),
  21127. tv->hashFinished1, hashAlgSz, tv->hashAlg);
  21128. if (ret != 0) break;
  21129. ret = XMEMCMP(tv->serverApplicationTrafficSecret, output, hashAlgSz);
  21130. if (ret != 0) break;
  21131. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  21132. secret, hashAlgSz,
  21133. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  21134. (byte*)expMasterLabel, (word32)XSTRLEN(expMasterLabel),
  21135. tv->hashFinished1, hashAlgSz, tv->hashAlg);
  21136. if (ret != 0) break;
  21137. ret = XMEMCMP(tv->exporterMasterSecret, output, hashAlgSz);
  21138. if (ret != 0) break;
  21139. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  21140. secret, hashAlgSz,
  21141. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  21142. (byte*)resMasterLabel, (word32)XSTRLEN(resMasterLabel),
  21143. tv->hashFinished2, hashAlgSz, tv->hashAlg);
  21144. if (ret != 0) break;
  21145. ret = XMEMCMP(tv->resumptionMasterSecret, output, hashAlgSz);
  21146. if (ret != 0) break;
  21147. }
  21148. return ret;
  21149. }
  21150. #endif /* WOLFSSL_TLS13 */
  21151. static const int fiducial2 = WC_TEST_RET_LN; /* source code reference point --
  21152. * see print_fiducials() below.
  21153. */
  21154. #if defined(HAVE_ECC) && defined(HAVE_X963_KDF)
  21155. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t x963kdf_test(void)
  21156. {
  21157. wc_test_ret_t ret;
  21158. byte kek[128];
  21159. #ifndef NO_SHA
  21160. /* SHA-1, COUNT = 0
  21161. * shared secret length: 192
  21162. * SharedInfo length: 0
  21163. * key data length: 128
  21164. */
  21165. WOLFSSL_SMALL_STACK_STATIC const byte Z[] = {
  21166. 0x1c, 0x7d, 0x7b, 0x5f, 0x05, 0x97, 0xb0, 0x3d,
  21167. 0x06, 0xa0, 0x18, 0x46, 0x6e, 0xd1, 0xa9, 0x3e,
  21168. 0x30, 0xed, 0x4b, 0x04, 0xdc, 0x64, 0xcc, 0xdd
  21169. };
  21170. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  21171. 0xbf, 0x71, 0xdf, 0xfd, 0x8f, 0x4d, 0x99, 0x22,
  21172. 0x39, 0x36, 0xbe, 0xb4, 0x6f, 0xee, 0x8c, 0xcc
  21173. };
  21174. #endif
  21175. #ifndef NO_SHA256
  21176. /* SHA-256, COUNT = 3
  21177. * shared secret length: 192
  21178. * SharedInfo length: 0
  21179. * key data length: 128
  21180. */
  21181. WOLFSSL_SMALL_STACK_STATIC const byte Z2[] = {
  21182. 0xd3, 0x8b, 0xdb, 0xe5, 0xc4, 0xfc, 0x16, 0x4c,
  21183. 0xdd, 0x96, 0x7f, 0x63, 0xc0, 0x4f, 0xe0, 0x7b,
  21184. 0x60, 0xcd, 0xe8, 0x81, 0xc2, 0x46, 0x43, 0x8c
  21185. };
  21186. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  21187. 0x5e, 0x67, 0x4d, 0xb9, 0x71, 0xba, 0xc2, 0x0a,
  21188. 0x80, 0xba, 0xd0, 0xd4, 0x51, 0x4d, 0xc4, 0x84
  21189. };
  21190. #endif
  21191. #ifdef WOLFSSL_SHA512
  21192. /* SHA-512, COUNT = 0
  21193. * shared secret length: 192
  21194. * SharedInfo length: 0
  21195. * key data length: 128
  21196. */
  21197. WOLFSSL_SMALL_STACK_STATIC const byte Z3[] = {
  21198. 0x87, 0xfc, 0x0d, 0x8c, 0x44, 0x77, 0x48, 0x5b,
  21199. 0xb5, 0x74, 0xf5, 0xfc, 0xea, 0x26, 0x4b, 0x30,
  21200. 0x88, 0x5d, 0xc8, 0xd9, 0x0a, 0xd8, 0x27, 0x82
  21201. };
  21202. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  21203. 0x94, 0x76, 0x65, 0xfb, 0xb9, 0x15, 0x21, 0x53,
  21204. 0xef, 0x46, 0x02, 0x38, 0x50, 0x6a, 0x02, 0x45
  21205. };
  21206. /* SHA-512, COUNT = 0
  21207. * shared secret length: 521
  21208. * SharedInfo length: 128
  21209. * key data length: 1024
  21210. */
  21211. WOLFSSL_SMALL_STACK_STATIC const byte Z4[] = {
  21212. 0x00, 0xaa, 0x5b, 0xb7, 0x9b, 0x33, 0xe3, 0x89,
  21213. 0xfa, 0x58, 0xce, 0xad, 0xc0, 0x47, 0x19, 0x7f,
  21214. 0x14, 0xe7, 0x37, 0x12, 0xf4, 0x52, 0xca, 0xa9,
  21215. 0xfc, 0x4c, 0x9a, 0xdb, 0x36, 0x93, 0x48, 0xb8,
  21216. 0x15, 0x07, 0x39, 0x2f, 0x1a, 0x86, 0xdd, 0xfd,
  21217. 0xb7, 0xc4, 0xff, 0x82, 0x31, 0xc4, 0xbd, 0x0f,
  21218. 0x44, 0xe4, 0x4a, 0x1b, 0x55, 0xb1, 0x40, 0x47,
  21219. 0x47, 0xa9, 0xe2, 0xe7, 0x53, 0xf5, 0x5e, 0xf0,
  21220. 0x5a, 0x2d
  21221. };
  21222. WOLFSSL_SMALL_STACK_STATIC const byte info4[] = {
  21223. 0xe3, 0xb5, 0xb4, 0xc1, 0xb0, 0xd5, 0xcf, 0x1d,
  21224. 0x2b, 0x3a, 0x2f, 0x99, 0x37, 0x89, 0x5d, 0x31
  21225. };
  21226. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  21227. 0x44, 0x63, 0xf8, 0x69, 0xf3, 0xcc, 0x18, 0x76,
  21228. 0x9b, 0x52, 0x26, 0x4b, 0x01, 0x12, 0xb5, 0x85,
  21229. 0x8f, 0x7a, 0xd3, 0x2a, 0x5a, 0x2d, 0x96, 0xd8,
  21230. 0xcf, 0xfa, 0xbf, 0x7f, 0xa7, 0x33, 0x63, 0x3d,
  21231. 0x6e, 0x4d, 0xd2, 0xa5, 0x99, 0xac, 0xce, 0xb3,
  21232. 0xea, 0x54, 0xa6, 0x21, 0x7c, 0xe0, 0xb5, 0x0e,
  21233. 0xef, 0x4f, 0x6b, 0x40, 0xa5, 0xc3, 0x02, 0x50,
  21234. 0xa5, 0xa8, 0xee, 0xee, 0x20, 0x80, 0x02, 0x26,
  21235. 0x70, 0x89, 0xdb, 0xf3, 0x51, 0xf3, 0xf5, 0x02,
  21236. 0x2a, 0xa9, 0x63, 0x8b, 0xf1, 0xee, 0x41, 0x9d,
  21237. 0xea, 0x9c, 0x4f, 0xf7, 0x45, 0xa2, 0x5a, 0xc2,
  21238. 0x7b, 0xda, 0x33, 0xca, 0x08, 0xbd, 0x56, 0xdd,
  21239. 0x1a, 0x59, 0xb4, 0x10, 0x6c, 0xf2, 0xdb, 0xbc,
  21240. 0x0a, 0xb2, 0xaa, 0x8e, 0x2e, 0xfa, 0x7b, 0x17,
  21241. 0x90, 0x2d, 0x34, 0x27, 0x69, 0x51, 0xce, 0xcc,
  21242. 0xab, 0x87, 0xf9, 0x66, 0x1c, 0x3e, 0x88, 0x16
  21243. };
  21244. #endif
  21245. #ifndef NO_SHA
  21246. ret = wc_X963_KDF(WC_HASH_TYPE_SHA, Z, sizeof(Z), NULL, 0,
  21247. kek, sizeof(verify));
  21248. if (ret != 0)
  21249. return WC_TEST_RET_ENC_EC(ret);
  21250. if (XMEMCMP(verify, kek, sizeof(verify)) != 0)
  21251. return WC_TEST_RET_ENC_NC;
  21252. #endif
  21253. #ifndef NO_SHA256
  21254. ret = wc_X963_KDF(WC_HASH_TYPE_SHA256, Z2, sizeof(Z2), NULL, 0,
  21255. kek, sizeof(verify2));
  21256. if (ret != 0)
  21257. return WC_TEST_RET_ENC_EC(ret);
  21258. if (XMEMCMP(verify2, kek, sizeof(verify2)) != 0)
  21259. return WC_TEST_RET_ENC_NC;
  21260. #endif
  21261. #ifdef WOLFSSL_SHA512
  21262. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z3, sizeof(Z3), NULL, 0,
  21263. kek, sizeof(verify3));
  21264. if (ret != 0)
  21265. return WC_TEST_RET_ENC_EC(ret);
  21266. if (XMEMCMP(verify3, kek, sizeof(verify3)) != 0)
  21267. return WC_TEST_RET_ENC_NC;
  21268. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z4, sizeof(Z4), info4,
  21269. sizeof(info4), kek, sizeof(verify4));
  21270. if (ret != 0)
  21271. return WC_TEST_RET_ENC_EC(ret);
  21272. if (XMEMCMP(verify4, kek, sizeof(verify4)) != 0)
  21273. return WC_TEST_RET_ENC_NC;
  21274. #endif
  21275. return 0;
  21276. }
  21277. #endif /* HAVE_X963_KDF */
  21278. #if defined(HAVE_HPKE) && (defined(HAVE_ECC) || defined(HAVE_CURVE25519)) && \
  21279. defined(HAVE_AESGCM)
  21280. static wc_test_ret_t hpke_test_single(Hpke* hpke)
  21281. {
  21282. wc_test_ret_t ret = 0;
  21283. int rngRet = 0;
  21284. WC_RNG rng[1];
  21285. const char* start_text = "this is a test";
  21286. const char* info_text = "info";
  21287. const char* aad_text = "aad";
  21288. byte ciphertext[MAX_HPKE_LABEL_SZ];
  21289. byte plaintext[MAX_HPKE_LABEL_SZ];
  21290. void* receiverKey = NULL;
  21291. void* ephemeralKey = NULL;
  21292. #ifdef WOLFSSL_SMALL_STACK
  21293. byte *pubKey = NULL; /* public key */
  21294. word16 pubKeySz = (word16)HPKE_Npk_MAX;
  21295. #else
  21296. byte pubKey[HPKE_Npk_MAX]; /* public key */
  21297. word16 pubKeySz = (word16)sizeof(pubKey);
  21298. #endif
  21299. rngRet = ret = wc_InitRng(rng);
  21300. if (ret != 0)
  21301. return ret;
  21302. #ifdef WOLFSSL_SMALL_STACK
  21303. if (ret == 0) {
  21304. pubKey = (byte *)XMALLOC(pubKeySz, HEAP_HINT,
  21305. DYNAMIC_TYPE_TMP_BUFFER);
  21306. if (pubKey == NULL)
  21307. ret = MEMORY_E;
  21308. }
  21309. #endif
  21310. /* generate the keys */
  21311. if (ret == 0)
  21312. ret = wc_HpkeGenerateKeyPair(hpke, &ephemeralKey, rng);
  21313. if (ret == 0)
  21314. ret = wc_HpkeGenerateKeyPair(hpke, &receiverKey, rng);
  21315. /* seal */
  21316. if (ret == 0) {
  21317. ret = wc_HpkeSealBase(hpke, ephemeralKey, receiverKey,
  21318. (byte*)info_text, (word32)XSTRLEN(info_text),
  21319. (byte*)aad_text, (word32)XSTRLEN(aad_text),
  21320. (byte*)start_text, (word32)XSTRLEN(start_text),
  21321. ciphertext);
  21322. }
  21323. /* export ephemeral key */
  21324. if (ret == 0)
  21325. ret = wc_HpkeSerializePublicKey(hpke, ephemeralKey, pubKey, &pubKeySz);
  21326. /* open with exported ephemeral key */
  21327. if (ret == 0) {
  21328. ret = wc_HpkeOpenBase(hpke, receiverKey, pubKey, pubKeySz,
  21329. (byte*)info_text, (word32)XSTRLEN(info_text),
  21330. (byte*)aad_text, (word32)XSTRLEN(aad_text),
  21331. ciphertext, (word32)XSTRLEN(start_text),
  21332. plaintext);
  21333. }
  21334. if (ret == 0)
  21335. ret = XMEMCMP(plaintext, start_text, XSTRLEN(start_text));
  21336. if (ephemeralKey != NULL)
  21337. wc_HpkeFreeKey(hpke, hpke->kem, ephemeralKey, hpke->heap);
  21338. if (receiverKey != NULL)
  21339. wc_HpkeFreeKey(hpke, hpke->kem, receiverKey, hpke->heap);
  21340. #ifdef WOLFSSL_SMALL_STACK
  21341. if (pubKey != NULL)
  21342. XFREE(pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21343. #endif
  21344. if (rngRet == 0)
  21345. wc_FreeRng(rng);
  21346. return ret;
  21347. }
  21348. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t hpke_test(void)
  21349. {
  21350. wc_test_ret_t ret = 0;
  21351. Hpke hpke[1];
  21352. #if defined(HAVE_ECC)
  21353. #if defined(WOLFSSL_SHA224) || !defined(NO_SHA256)
  21354. /* p256 */
  21355. ret = wc_HpkeInit(hpke, DHKEM_P256_HKDF_SHA256, HKDF_SHA256,
  21356. HPKE_AES_128_GCM, NULL);
  21357. if (ret != 0)
  21358. return ret;
  21359. ret = hpke_test_single(hpke);
  21360. if (ret != 0)
  21361. return ret;
  21362. #endif
  21363. #if defined(WOLFSSL_SHA384) && \
  21364. (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES))
  21365. /* p384 */
  21366. ret = wc_HpkeInit(hpke, DHKEM_P384_HKDF_SHA384, HKDF_SHA384,
  21367. HPKE_AES_128_GCM, NULL);
  21368. if (ret != 0)
  21369. return ret;
  21370. ret = hpke_test_single(hpke);
  21371. if (ret != 0)
  21372. return ret;
  21373. #endif
  21374. #if (defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)) && \
  21375. (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES))
  21376. /* p521 */
  21377. ret = wc_HpkeInit(hpke, DHKEM_P521_HKDF_SHA512, HKDF_SHA512,
  21378. HPKE_AES_128_GCM, NULL);
  21379. if (ret != 0)
  21380. return ret;
  21381. ret = hpke_test_single(hpke);
  21382. if (ret != 0)
  21383. return ret;
  21384. #endif
  21385. #endif
  21386. #if defined(HAVE_CURVE25519)
  21387. /* test with curve25519 and aes256 */
  21388. ret = wc_HpkeInit(hpke, DHKEM_X25519_HKDF_SHA256, HKDF_SHA256,
  21389. HPKE_AES_256_GCM, NULL);
  21390. if (ret != 0)
  21391. return ret;
  21392. ret = hpke_test_single(hpke);
  21393. if (ret != 0)
  21394. return ret;
  21395. #endif
  21396. return ret;
  21397. /* x448 and chacha20 are unimplemented */
  21398. }
  21399. #endif /* HAVE_HPKE && HAVE_ECC && HAVE_AESGCM */
  21400. #ifdef HAVE_ECC
  21401. /* size to use for ECC key gen tests */
  21402. #ifndef ECC_KEYGEN_SIZE
  21403. #ifndef NO_ECC256
  21404. #define ECC_KEYGEN_SIZE 32
  21405. #elif defined(HAVE_ECC384)
  21406. #define ECC_KEYGEN_SIZE 48
  21407. #elif defined(HAVE_ECC224)
  21408. #define ECC_KEYGEN_SIZE 28
  21409. #elif defined(HAVE_ECC521)
  21410. #define ECC_KEYGEN_SIZE 66
  21411. #else
  21412. #error No ECC keygen size defined for test
  21413. #endif
  21414. #endif
  21415. #ifdef BENCH_EMBEDDED
  21416. #define ECC_SHARED_SIZE 128
  21417. #else
  21418. #define ECC_SHARED_SIZE MAX_ECC_BYTES
  21419. #endif
  21420. #if defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  21421. #define HAVE_ECC_DETERMINISTIC_K
  21422. #define ECC_DIGEST_SIZE WC_SHA256_DIGEST_SIZE
  21423. #else
  21424. #define ECC_DIGEST_SIZE MAX_ECC_BYTES
  21425. #endif
  21426. #define ECC_SIG_SIZE ECC_MAX_SIG_SIZE
  21427. #ifdef NO_ECC_SECP
  21428. #define NO_ECC_VECTOR_TEST
  21429. #endif
  21430. #ifndef NO_ECC_VECTOR_TEST
  21431. #if (defined(HAVE_ECC192) || defined(HAVE_ECC224) ||\
  21432. !defined(NO_ECC256) || defined(HAVE_ECC384) ||\
  21433. defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES))
  21434. #define HAVE_ECC_VECTOR_TEST
  21435. #endif
  21436. #endif
  21437. #ifdef HAVE_ECC_VECTOR_TEST
  21438. typedef struct eccVector {
  21439. const char* msg; /* SHA-1 Encoded Message */
  21440. const char* Qx;
  21441. const char* Qy;
  21442. const char* d; /* Private Key */
  21443. const char* R;
  21444. const char* S;
  21445. const char* curveName;
  21446. word32 msgLen;
  21447. word32 keySize;
  21448. #ifndef NO_ASN
  21449. const byte* r;
  21450. word32 rSz;
  21451. const byte* s;
  21452. word32 sSz;
  21453. #endif
  21454. } eccVector;
  21455. #if !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21456. static wc_test_ret_t ecc_test_vector_item(const eccVector* vector)
  21457. {
  21458. wc_test_ret_t ret = 0;
  21459. int verify = 0;
  21460. word32 sigSz;
  21461. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21462. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21463. #else
  21464. ecc_key userA[1];
  21465. #endif
  21466. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  21467. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  21468. word32 sigRawSz, rSz = MAX_ECC_BYTES, sSz = MAX_ECC_BYTES;
  21469. WC_DECLARE_VAR(sigRaw, byte, ECC_SIG_SIZE, HEAP_HINT);
  21470. WC_DECLARE_VAR(r, byte, MAX_ECC_BYTES, HEAP_HINT);
  21471. WC_DECLARE_VAR(s, byte, MAX_ECC_BYTES, HEAP_HINT);
  21472. #endif
  21473. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  21474. if (sig == NULL)
  21475. ERROR_OUT(MEMORY_E, done);
  21476. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  21477. if (sigRaw == NULL || r == NULL || s == NULL)
  21478. ERROR_OUT(MEMORY_E, done);
  21479. #endif
  21480. #endif
  21481. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21482. if (userA == NULL)
  21483. ERROR_OUT(MEMORY_E, done);
  21484. #endif
  21485. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  21486. if (ret != 0)
  21487. goto done;
  21488. ret = wc_ecc_import_raw(userA, vector->Qx, vector->Qy,
  21489. vector->d, vector->curveName);
  21490. if (ret != 0)
  21491. goto done;
  21492. XMEMSET(sig, 0, ECC_SIG_SIZE);
  21493. sigSz = ECC_SIG_SIZE;
  21494. ret = wc_ecc_rs_to_sig(vector->R, vector->S, sig, &sigSz);
  21495. if (ret != 0)
  21496. goto done;
  21497. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  21498. XMEMSET(sigRaw, 0, ECC_SIG_SIZE);
  21499. sigRawSz = ECC_SIG_SIZE;
  21500. ret = wc_ecc_rs_raw_to_sig(vector->r, vector->rSz, vector->s, vector->sSz,
  21501. sigRaw, &sigRawSz);
  21502. if (ret != 0)
  21503. goto done;
  21504. if (sigSz != sigRawSz || XMEMCMP(sig, sigRaw, sigSz) != 0) {
  21505. ret = WC_TEST_RET_ENC_NC;
  21506. goto done;
  21507. }
  21508. ret = wc_ecc_sig_to_rs(sig, sigSz, r, &rSz, s, &sSz);
  21509. if (ret != 0)
  21510. goto done;
  21511. if (rSz != vector->rSz || XMEMCMP(r, vector->r, rSz) != 0 ||
  21512. sSz != vector->sSz || XMEMCMP(s, vector->s, sSz) != 0) {
  21513. ret = WC_TEST_RET_ENC_NC;
  21514. goto done;
  21515. }
  21516. #endif
  21517. #ifdef HAVE_ECC_VERIFY
  21518. do {
  21519. #if defined(WOLFSSL_ASYNC_CRYPT)
  21520. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21521. #endif
  21522. if (ret == 0)
  21523. ret = wc_ecc_verify_hash(sig, sigSz, (byte*)vector->msg,
  21524. vector->msgLen, &verify, userA);
  21525. } while (ret == WC_PENDING_E);
  21526. if (ret != 0)
  21527. goto done;
  21528. TEST_SLEEP();
  21529. if (verify != 1)
  21530. ret = WC_TEST_RET_ENC_NC;
  21531. #endif
  21532. done:
  21533. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21534. if (userA != NULL) {
  21535. wc_ecc_free(userA);
  21536. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21537. }
  21538. #else
  21539. wc_ecc_free(userA);
  21540. #endif
  21541. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  21542. WC_FREE_VAR(sigRaw, HEAP_HINT);
  21543. WC_FREE_VAR(r, HEAP_HINT);
  21544. WC_FREE_VAR(s, HEAP_HINT);
  21545. #endif
  21546. WC_FREE_VAR(sig, HEAP_HINT);
  21547. return ret;
  21548. }
  21549. static wc_test_ret_t ecc_test_vector(int keySize)
  21550. {
  21551. wc_test_ret_t ret;
  21552. eccVector vec;
  21553. XMEMSET(&vec, 0, sizeof(vec));
  21554. vec.keySize = (word32)keySize;
  21555. switch(keySize) {
  21556. #if defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)
  21557. case 14:
  21558. return 0;
  21559. #endif /* HAVE_ECC112 */
  21560. #if defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)
  21561. case 16:
  21562. return 0;
  21563. #endif /* HAVE_ECC128 */
  21564. #if defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)
  21565. case 20:
  21566. return 0;
  21567. #endif /* HAVE_ECC160 */
  21568. #if defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)
  21569. case 24:
  21570. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  21571. #if 1
  21572. vec.msg = "\x60\x80\x79\x42\x3f\x12\x42\x1d\xe6\x16\xb7\x49\x3e\xbe\x55\x1c\xf4\xd6\x5b\x92";
  21573. vec.msgLen = 20;
  21574. #else
  21575. /* This is the raw message prior to SHA-1 */
  21576. vec.msg =
  21577. "\xeb\xf7\x48\xd7\x48\xeb\xbc\xa7\xd2\x9f\xb4\x73\x69\x8a\x6e\x6b"
  21578. "\x4f\xb1\x0c\x86\x5d\x4a\xf0\x24\xcc\x39\xae\x3d\xf3\x46\x4b\xa4"
  21579. "\xf1\xd6\xd4\x0f\x32\xbf\x96\x18\xa9\x1b\xb5\x98\x6f\xa1\xa2\xaf"
  21580. "\x04\x8a\x0e\x14\xdc\x51\xe5\x26\x7e\xb0\x5e\x12\x7d\x68\x9d\x0a"
  21581. "\xc6\xf1\xa7\xf1\x56\xce\x06\x63\x16\xb9\x71\xcc\x7a\x11\xd0\xfd"
  21582. "\x7a\x20\x93\xe2\x7c\xf2\xd0\x87\x27\xa4\xe6\x74\x8c\xc3\x2f\xd5"
  21583. "\x9c\x78\x10\xc5\xb9\x01\x9d\xf2\x1c\xdc\xc0\xbc\xa4\x32\xc0\xa3"
  21584. "\xee\xd0\x78\x53\x87\x50\x88\x77\x11\x43\x59\xce\xe4\xa0\x71\xcf";
  21585. vec.msgLen = 128;
  21586. #endif
  21587. vec.Qx = "07008ea40b08dbe76432096e80a2494c94982d2d5bcf98e6";
  21588. vec.Qy = "76fab681d00b414ea636ba215de26d98c41bd7f2e4d65477";
  21589. vec.d = "e14f37b3d1374ff8b03f41b9b3fdd2f0ebccf275d660d7f3";
  21590. vec.R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  21591. vec.S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  21592. vec.curveName = "SECP192R1";
  21593. #ifndef NO_ASN
  21594. vec.r = (byte*)"\x69\x94\xd9\x62\xbd\xd0\xd7\x93\xff\xdd\xf8\x55"
  21595. "\xec\x5b\xf2\xf9\x1a\x96\x98\xb4\x62\x58\xa6\x3e";
  21596. vec.rSz = 24;
  21597. vec.s = (byte*)"\x02\xba\x64\x65\xa2\x34\x90\x37\x44\xab\x02\xbc"
  21598. "\x85\x21\x40\x5b\x73\xcf\x5f\xc0\x0e\x1a\x9f\x41";
  21599. vec.sSz = 24;
  21600. #endif
  21601. break;
  21602. #endif /* HAVE_ECC192 */
  21603. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  21604. case 28:
  21605. /* first [P-224,SHA-1] vector from FIPS 186-3 NIST vectors */
  21606. #if 1
  21607. vec.msg = "\xb9\xa3\xb8\x6d\xb0\xba\x99\xfd\xc6\xd2\x94\x6b\xfe\xbe\x9c\xe8\x3f\x10\x74\xfc";
  21608. vec.msgLen = 20;
  21609. #else
  21610. /* This is the raw message prior to SHA-1 */
  21611. vec.msg =
  21612. "\x36\xc8\xb2\x29\x86\x48\x7f\x67\x7c\x18\xd0\x97\x2a\x9e\x20\x47"
  21613. "\xb3\xaf\xa5\x9e\xc1\x62\x76\x4e\xc3\x0b\x5b\x69\xe0\x63\x0f\x99"
  21614. "\x0d\x4e\x05\xc2\x73\xb0\xe5\xa9\xd4\x28\x27\xb6\x95\xfc\x2d\x64"
  21615. "\xd9\x13\x8b\x1c\xf4\xc1\x21\x55\x89\x4c\x42\x13\x21\xa7\xbb\x97"
  21616. "\x0b\xdc\xe0\xfb\xf0\xd2\xae\x85\x61\xaa\xd8\x71\x7f\x2e\x46\xdf"
  21617. "\xe3\xff\x8d\xea\xb4\xd7\x93\x23\x56\x03\x2c\x15\x13\x0d\x59\x9e"
  21618. "\x26\xc1\x0f\x2f\xec\x96\x30\x31\xac\x69\x38\xa1\x8d\x66\x45\x38"
  21619. "\xb9\x4d\xac\x55\x34\xef\x7b\x59\x94\x24\xd6\x9b\xe1\xf7\x1c\x20";
  21620. vec.msgLen = 128;
  21621. #endif
  21622. vec.Qx = "8a4dca35136c4b70e588e23554637ae251077d1365a6ba5db9585de7";
  21623. vec.Qy = "ad3dee06de0be8279d4af435d7245f14f3b4f82eb578e519ee0057b1";
  21624. vec.d = "97c4b796e1639dd1035b708fc00dc7ba1682cec44a1002a1a820619f";
  21625. vec.R = "147b33758321e722a0360a4719738af848449e2c1d08defebc1671a7";
  21626. vec.S = "24fc7ed7f1352ca3872aa0916191289e2e04d454935d50fe6af3ad5b";
  21627. vec.curveName = "SECP224R1";
  21628. #ifndef NO_ASN
  21629. vec.r = (byte*)"\x14\x7b\x33\x75\x83\x21\xe7\x22\xa0\x36\x0a\x47"
  21630. "\x19\x73\x8a\xf8\x48\x44\x9e\x2c\x1d\x08\xde\xfe"
  21631. "\xbc\x16\x71\xa7";
  21632. vec.rSz = 28;
  21633. vec.s = (byte*)"\x24\xfc\x7e\xd7\xf1\x35\x2c\xa3\x87\x2a\xa0\x91"
  21634. "\x61\x91\x28\x9e\x2e\x04\xd4\x54\x93\x5d\x50\xfe"
  21635. "\x6a\xf3\xad\x5b";
  21636. vec.sSz = 28;
  21637. #endif
  21638. break;
  21639. #endif /* HAVE_ECC224 */
  21640. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  21641. case 30:
  21642. return 0;
  21643. #endif /* HAVE_ECC239 */
  21644. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  21645. case 32:
  21646. /* first [P-256,SHA-1] vector from FIPS 186-3 NIST vectors */
  21647. #if 1
  21648. vec.msg = "\xa3\xf9\x1a\xe2\x1b\xa6\xb3\x03\x98\x64\x47\x2f\x18\x41\x44\xc6\xaf\x62\xcd\x0e";
  21649. vec.msgLen = 20;
  21650. #else
  21651. /* This is the raw message prior to SHA-1 */
  21652. vec.msg =
  21653. "\xa2\x4b\x21\x76\x2e\x6e\xdb\x15\x3c\xc1\x14\x38\xdb\x0e\x92\xcd"
  21654. "\xf5\x2b\x86\xb0\x6c\xa9\x70\x16\x06\x27\x59\xc7\x0d\x36\xd1\x56"
  21655. "\x2c\xc9\x63\x0d\x7f\xc7\xc7\x74\xb2\x8b\x54\xe3\x1e\xf5\x58\x72"
  21656. "\xb2\xa6\x5d\xf1\xd7\xec\x26\xde\xbb\x33\xe7\xd9\x27\xef\xcc\xf4"
  21657. "\x6b\x63\xde\x52\xa4\xf4\x31\xea\xca\x59\xb0\x5d\x2e\xde\xc4\x84"
  21658. "\x5f\xff\xc0\xee\x15\x03\x94\xd6\x1f\x3d\xfe\xcb\xcd\xbf\x6f\x5a"
  21659. "\x73\x38\xd0\xbe\x3f\x2a\x77\x34\x51\x98\x3e\xba\xeb\x48\xf6\x73"
  21660. "\x8f\xc8\x95\xdf\x35\x7e\x1a\x48\xa6\x53\xbb\x35\x5a\x31\xa1\xb4"
  21661. vec.msgLen = 128;
  21662. #endif
  21663. vec.Qx = "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  21664. vec.Qy = "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  21665. vec.d = "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  21666. vec.R = "2b826f5d44e2d0b6de531ad96b51e8f0c56fdfead3c236892e4d84eacfc3b75c";
  21667. vec.S = "a2248b62c03db35a7cd63e8a120a3521a89d3d2f61ff99035a2148ae32e3a248";
  21668. #ifndef NO_ASN
  21669. vec.r = (byte*)"\x2b\x82\x6f\x5d\x44\xe2\xd0\xb6\xde\x53\x1a\xd9"
  21670. "\x6b\x51\xe8\xf0\xc5\x6f\xdf\xea\xd3\xc2\x36\x89"
  21671. "\x2e\x4d\x84\xea\xcf\xc3\xb7\x5c";
  21672. vec.rSz = 32;
  21673. vec.s = (byte*)"\xa2\x24\x8b\x62\xc0\x3d\xb3\x5a\x7c\xd6\x3e\x8a"
  21674. "\x12\x0a\x35\x21\xa8\x9d\x3d\x2f\x61\xff\x99\x03"
  21675. "\x5a\x21\x48\xae\x32\xe3\xa2\x48";
  21676. vec.sSz = 32;
  21677. #endif
  21678. vec.curveName = "SECP256R1";
  21679. break;
  21680. #endif /* !NO_ECC256 */
  21681. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  21682. case 40:
  21683. return 0;
  21684. #endif /* HAVE_ECC320 */
  21685. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  21686. case 48:
  21687. /* first [P-384,SHA-1] vector from FIPS 186-3 NIST vectors */
  21688. #if 1
  21689. vec.msg = "\x9b\x9f\x8c\x95\x35\xa5\xca\x26\x60\x5d\xb7\xf2\xfa\x57\x3b\xdf\xc3\x2e\xab\x8b";
  21690. vec.msgLen = 20;
  21691. #else
  21692. /* This is the raw message prior to SHA-1 */
  21693. vec.msg =
  21694. "\xab\xe1\x0a\xce\x13\xe7\xe1\xd9\x18\x6c\x48\xf7\x88\x9d\x51\x47"
  21695. "\x3d\x3a\x09\x61\x98\x4b\xc8\x72\xdf\x70\x8e\xcc\x3e\xd3\xb8\x16"
  21696. "\x9d\x01\xe3\xd9\x6f\xc4\xf1\xd5\xea\x00\xa0\x36\x92\xbc\xc5\xcf"
  21697. "\xfd\x53\x78\x7c\x88\xb9\x34\xaf\x40\x4c\x03\x9d\x32\x89\xb5\xba"
  21698. "\xc5\xae\x7d\xb1\x49\x68\x75\xb5\xdc\x73\xc3\x09\xf9\x25\xc1\x3d"
  21699. "\x1c\x01\xab\xda\xaf\xeb\xcd\xac\x2c\xee\x43\x39\x39\xce\x8d\x4a"
  21700. "\x0a\x5d\x57\xbb\x70\x5f\x3b\xf6\xec\x08\x47\x95\x11\xd4\xb4\xa3"
  21701. "\x21\x1f\x61\x64\x9a\xd6\x27\x43\x14\xbf\x0d\x43\x8a\x81\xe0\x60"
  21702. vec.msgLen = 128;
  21703. #endif
  21704. vec.Qx = "e55fee6c49d8d523f5ce7bf9c0425ce4ff650708b7de5cfb095901523979a7f042602db30854735369813b5c3f5ef868";
  21705. vec.Qy = "28f59cc5dc509892a988d38a8e2519de3d0c4fd0fbdb0993e38f18506c17606c5e24249246f1ce94983a5361c5be983e";
  21706. vec.d = "a492ce8fa90084c227e1a32f7974d39e9ff67a7e8705ec3419b35fb607582bebd461e0b1520ac76ec2dd4e9b63ebae71";
  21707. vec.R = "6820b8585204648aed63bdff47f6d9acebdea62944774a7d14f0e14aa0b9a5b99545b2daee6b3c74ebf606667a3f39b7";
  21708. vec.S = "491af1d0cccd56ddd520b233775d0bc6b40a6255cc55207d8e9356741f23c96c14714221078dbd5c17f4fdd89b32a907";
  21709. vec.curveName = "SECP384R1";
  21710. #ifndef NO_ASN
  21711. vec.r = (byte*)"\x68\x20\xb8\x58\x52\x04\x64\x8a\xed\x63\xbd\xff"
  21712. "\x47\xf6\xd9\xac\xeb\xde\xa6\x29\x44\x77\x4a\x7d"
  21713. "\x14\xf0\xe1\x4a\xa0\xb9\xa5\xb9\x95\x45\xb2\xda"
  21714. "\xee\x6b\x3c\x74\xeb\xf6\x06\x66\x7a\x3f\x39\xb7";
  21715. vec.rSz = 48;
  21716. vec.s = (byte*)"\x49\x1a\xf1\xd0\xcc\xcd\x56\xdd\xd5\x20\xb2\x33"
  21717. "\x77\x5d\x0b\xc6\xb4\x0a\x62\x55\xcc\x55\x20\x7d"
  21718. "\x8e\x93\x56\x74\x1f\x23\xc9\x6c\x14\x71\x42\x21"
  21719. "\x07\x8d\xbd\x5c\x17\xf4\xfd\xd8\x9b\x32\xa9\x07";
  21720. vec.sSz = 48;
  21721. #endif
  21722. break;
  21723. #endif /* HAVE_ECC384 */
  21724. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  21725. case 64:
  21726. return 0;
  21727. #endif /* HAVE_ECC512 */
  21728. #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
  21729. case 66:
  21730. /* first [P-521,SHA-1] vector from FIPS 186-3 NIST vectors */
  21731. #if 1
  21732. vec.msg = "\x1b\xf7\x03\x9c\xca\x23\x94\x27\x3f\x11\xa1\xd4\x8d\xcc\xb4\x46\x6f\x31\x61\xdf";
  21733. vec.msgLen = 20;
  21734. #else
  21735. /* This is the raw message prior to SHA-1 */
  21736. vec.msg =
  21737. "\x50\x3f\x79\x39\x34\x0a\xc7\x23\xcd\x4a\x2f\x4e\x6c\xcc\x27\x33"
  21738. "\x38\x3a\xca\x2f\xba\x90\x02\x19\x9d\x9e\x1f\x94\x8b\xe0\x41\x21"
  21739. "\x07\xa3\xfd\xd5\x14\xd9\x0c\xd4\xf3\x7c\xc3\xac\x62\xef\x00\x3a"
  21740. "\x2d\xb1\xd9\x65\x7a\xb7\x7f\xe7\x55\xbf\x71\xfa\x59\xe4\xd9\x6e"
  21741. "\xa7\x2a\xe7\xbf\x9d\xe8\x7d\x79\x34\x3b\xc1\xa4\xbb\x14\x4d\x16"
  21742. "\x28\xd1\xe9\xe9\xc8\xed\x80\x8b\x96\x2c\x54\xe5\xf9\x6d\x53\xda"
  21743. "\x14\x7a\x96\x38\xf9\x4a\x91\x75\xd8\xed\x61\x05\x5f\x0b\xa5\x73"
  21744. "\xa8\x2b\xb7\xe0\x18\xee\xda\xc4\xea\x7b\x36\x2e\xc8\x9c\x38\x2b"
  21745. vec.msgLen = 128;
  21746. #endif
  21747. vec.Qx = "12fbcaeffa6a51f3ee4d3d2b51c5dec6d7c726ca353fc014ea2bf7cfbb9b910d32cbfa6a00fe39b6cdb8946f22775398b2e233c0cf144d78c8a7742b5c7a3bb5d23";
  21748. vec.Qy = "09cdef823dd7bf9a79e8cceacd2e4527c231d0ae5967af0958e931d7ddccf2805a3e618dc3039fec9febbd33052fe4c0fee98f033106064982d88f4e03549d4a64d";
  21749. vec.d = "1bd56bd106118eda246155bd43b42b8e13f0a6e25dd3bb376026fab4dc92b6157bc6dfec2d15dd3d0cf2a39aa68494042af48ba9601118da82c6f2108a3a203ad74";
  21750. vec.R = "0bd117b4807710898f9dd7778056485777668f0e78e6ddf5b000356121eb7a220e9493c7f9a57c077947f89ac45d5acb6661bbcd17abb3faea149ba0aa3bb1521be";
  21751. vec.S = "019cd2c5c3f9870ecdeb9b323abdf3a98cd5e231d85c6ddc5b71ab190739f7f226e6b134ba1d5889ddeb2751dabd97911dff90c34684cdbe7bb669b6c3d22f2480c";
  21752. vec.curveName = "SECP521R1";
  21753. #ifndef NO_ASN
  21754. vec.r = (byte*)"\xbd\x11\x7b\x48\x07\x71\x08\x98\xf9\xdd\x77\x78"
  21755. "\x05\x64\x85\x77\x76\x68\xf0\xe7\x8e\x6d\xdf\x5b"
  21756. "\x00\x03\x56\x12\x1e\xb7\xa2\x20\xe9\x49\x3c\x7f"
  21757. "\x9a\x57\xc0\x77\x94\x7f\x89\xac\x45\xd5\xac\xb6"
  21758. "\x66\x1b\xbc\xd1\x7a\xbb\x3f\xae\xa1\x49\xba\x0a"
  21759. "\xa3\xbb\x15\x21\xbe";
  21760. vec.rSz = 65;
  21761. vec.s = (byte*)"\x19\xcd\x2c\x5c\x3f\x98\x70\xec\xde\xb9\xb3\x23"
  21762. "\xab\xdf\x3a\x98\xcd\x5e\x23\x1d\x85\xc6\xdd\xc5"
  21763. "\xb7\x1a\xb1\x90\x73\x9f\x7f\x22\x6e\x6b\x13\x4b"
  21764. "\xa1\xd5\x88\x9d\xde\xb2\x75\x1d\xab\xd9\x79\x11"
  21765. "\xdf\xf9\x0c\x34\x68\x4c\xdb\xe7\xbb\x66\x9b\x6c"
  21766. "\x3d\x22\xf2\x48\x0c";
  21767. vec.sSz = 65;
  21768. #endif
  21769. break;
  21770. #endif /* HAVE_ECC521 */
  21771. default:
  21772. return NOT_COMPILED_IN; /* Invalid key size / Not supported */
  21773. }; /* Switch */
  21774. ret = ecc_test_vector_item(&vec);
  21775. if (ret < 0) {
  21776. return ret;
  21777. }
  21778. return 0;
  21779. }
  21780. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  21781. #if defined(HAVE_ECC_SIGN) && (defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  21782. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)) \
  21783. && (!defined(FIPS_VERSION_GE) || FIPS_VERSION_GE(5,3))
  21784. #if defined(HAVE_ECC256)
  21785. static wc_test_ret_t ecc_test_deterministic_k(WC_RNG* rng)
  21786. {
  21787. wc_test_ret_t ret;
  21788. #ifdef WOLFSSL_SMALL_STACK
  21789. ecc_key *key = NULL;
  21790. #else
  21791. ecc_key key[1];
  21792. #endif
  21793. int key_inited = 0;
  21794. byte sig[72];
  21795. word32 sigSz;
  21796. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  21797. unsigned char hash[32];
  21798. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  21799. "C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721";
  21800. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  21801. "60FED4BA255A9D31C961EB74C6356D68C049B8923B61FA6CE669622E60F29FB6";
  21802. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  21803. "7903FE1008B8BC99A41AE9E95628BC64F2F1B20C2D7E9F5177A3C294D4462299";
  21804. WOLFSSL_SMALL_STACK_STATIC const byte expSig[] = {
  21805. 0x30, 0x46, 0x02, 0x21, 0x00, 0xEF, 0xD4, 0x8B,
  21806. 0x2A, 0xAC, 0xB6, 0xA8, 0xFD, 0x11, 0x40, 0xDD,
  21807. 0x9C, 0xD4, 0x5E, 0x81, 0xD6, 0x9D, 0x2C, 0x87,
  21808. 0x7B, 0x56, 0xAA, 0xF9, 0x91, 0xC3, 0x4D, 0x0E,
  21809. 0xA8, 0x4E, 0xAF, 0x37, 0x16, 0x02, 0x21, 0x00,
  21810. 0xF7, 0xCB, 0x1C, 0x94, 0x2D, 0x65, 0x7C, 0x41,
  21811. 0xD4, 0x36, 0xC7, 0xA1, 0xB6, 0xE2, 0x9F, 0x65,
  21812. 0xF3, 0xE9, 0x00, 0xDB, 0xB9, 0xAF, 0xF4, 0x06,
  21813. 0x4D, 0xC4, 0xAB, 0x2F, 0x84, 0x3A, 0xCD, 0xA8
  21814. };
  21815. #ifdef WOLFSSL_SMALL_STACK
  21816. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21817. if (key == NULL)
  21818. return MEMORY_E;
  21819. #endif
  21820. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  21821. if (ret != 0) {
  21822. goto done;
  21823. }
  21824. key_inited = 1;
  21825. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP256R1");
  21826. if (ret != 0) {
  21827. goto done;
  21828. }
  21829. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  21830. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  21831. if (ret != 0) {
  21832. goto done;
  21833. }
  21834. ret = wc_ecc_set_deterministic(key, 1);
  21835. if (ret != 0) {
  21836. goto done;
  21837. }
  21838. sigSz = sizeof(sig);
  21839. do {
  21840. #if defined(WOLFSSL_ASYNC_CRYPT)
  21841. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21842. #endif
  21843. if (ret == 0)
  21844. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  21845. } while (ret == WC_PENDING_E);
  21846. if (ret != 0) {
  21847. goto done;
  21848. }
  21849. TEST_SLEEP();
  21850. if (sigSz != sizeof(expSig)) {
  21851. ret = WC_TEST_RET_ENC_NC;
  21852. goto done;
  21853. }
  21854. if (XMEMCMP(sig, expSig, sigSz) != 0) {
  21855. ret = WC_TEST_RET_ENC_NC;
  21856. goto done;
  21857. }
  21858. sigSz = sizeof(sig);
  21859. do {
  21860. #if defined(WOLFSSL_ASYNC_CRYPT)
  21861. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21862. #endif
  21863. if (ret == 0)
  21864. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  21865. } while (ret == WC_PENDING_E);
  21866. if (ret != 0) {
  21867. goto done;
  21868. }
  21869. TEST_SLEEP();
  21870. done:
  21871. if (key_inited)
  21872. wc_ecc_free(key);
  21873. #ifdef WOLFSSL_SMALL_STACK
  21874. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21875. #endif
  21876. return ret;
  21877. }
  21878. #endif
  21879. #ifdef WOLFSSL_PUBLIC_MP
  21880. #if defined(HAVE_ECC384)
  21881. /* KAT from RFC6979 */
  21882. static wc_test_ret_t ecc384_test_deterministic_k(WC_RNG* rng)
  21883. {
  21884. wc_test_ret_t ret;
  21885. #ifdef WOLFSSL_SMALL_STACK
  21886. ecc_key *key;
  21887. mp_int *r, *s, *expR, *expS;
  21888. #else
  21889. ecc_key key[1];
  21890. mp_int r[1], s[1], expR[1], expS[1];
  21891. #endif
  21892. int key_inited = 0;
  21893. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  21894. unsigned char hash[32];
  21895. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  21896. "6B9D3DAD2E1B8C1C05B19875B6659F4DE23C3B667BF297BA9AA47740787137D8"
  21897. "96D5724E4C70A825F872C9EA60D2EDF5";
  21898. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  21899. "EC3A4E415B4E19A4568618029F427FA5DA9A8BC4AE92E02E06AAE5286B300C64"
  21900. "DEF8F0EA9055866064A254515480BC13";
  21901. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  21902. "8015D9B72D7D57244EA8EF9AC0C621896708A59367F9DFB9F54CA84B3F1C9DB1"
  21903. "288B231C3AE0D4FE7344FD2533264720";
  21904. WOLFSSL_SMALL_STACK_STATIC const char* expRstr =
  21905. "21B13D1E013C7FA1392D03C5F99AF8B30C570C6F98D4EA8E354B63A21D3DAA33"
  21906. "BDE1E888E63355D92FA2B3C36D8FB2CD";
  21907. WOLFSSL_SMALL_STACK_STATIC const char* expSstr =
  21908. "F3AA443FB107745BF4BD77CB3891674632068A10CA67E3D45DB2266FA7D1FEEB"
  21909. "EFDC63ECCD1AC42EC0CB8668A4FA0AB0";
  21910. #ifdef WOLFSSL_SMALL_STACK
  21911. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21912. r = (mp_int *)XMALLOC(sizeof(*r), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21913. s = (mp_int *)XMALLOC(sizeof(*s), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21914. expR = (mp_int *)XMALLOC(sizeof(*expR), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21915. expS = (mp_int *)XMALLOC(sizeof(*expS), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21916. if ((key == NULL) ||
  21917. (r == NULL) ||
  21918. (s == NULL) ||
  21919. (expR == NULL) ||
  21920. (expS == NULL))
  21921. {
  21922. ret = MEMORY_E;
  21923. goto done;
  21924. }
  21925. #endif
  21926. ret = mp_init_multi(r, s, expR, expS, NULL, NULL);
  21927. if (ret != MP_OKAY) {
  21928. goto done;
  21929. }
  21930. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  21931. if (ret != 0) {
  21932. goto done;
  21933. }
  21934. key_inited = 1;
  21935. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP384R1");
  21936. if (ret != 0) {
  21937. goto done;
  21938. }
  21939. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  21940. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  21941. if (ret != 0) {
  21942. goto done;
  21943. }
  21944. ret = wc_ecc_set_deterministic(key, 1);
  21945. if (ret != 0) {
  21946. goto done;
  21947. }
  21948. do {
  21949. #if defined(WOLFSSL_ASYNC_CRYPT)
  21950. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21951. #endif
  21952. if (ret == 0)
  21953. ret = wc_ecc_sign_hash_ex(hash, sizeof(hash), rng, key, r, s);
  21954. } while (ret == WC_PENDING_E);
  21955. if (ret != 0) {
  21956. goto done;
  21957. }
  21958. TEST_SLEEP();
  21959. mp_read_radix(expR, expRstr, MP_RADIX_HEX);
  21960. mp_read_radix(expS, expSstr, MP_RADIX_HEX);
  21961. if (mp_cmp(r, expR) != MP_EQ) {
  21962. ret = WC_TEST_RET_ENC_NC;
  21963. }
  21964. done:
  21965. if (key_inited)
  21966. wc_ecc_free(key);
  21967. #ifdef WOLFSSL_SMALL_STACK
  21968. if (key != NULL)
  21969. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21970. if (r != NULL)
  21971. XFREE(r, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21972. if (s != NULL)
  21973. XFREE(s, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21974. if (expR != NULL)
  21975. XFREE(expR, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21976. if (expS != NULL)
  21977. XFREE(expS, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21978. #endif
  21979. return ret;
  21980. }
  21981. #endif /* HAVE_ECC384 */
  21982. #if defined(HAVE_ECC521)
  21983. /* KAT from RFC6979 */
  21984. static wc_test_ret_t ecc521_test_deterministic_k(WC_RNG* rng)
  21985. {
  21986. wc_test_ret_t ret;
  21987. #ifdef WOLFSSL_SMALL_STACK
  21988. ecc_key *key;
  21989. mp_int *r, *s, *expR, *expS;
  21990. #else
  21991. ecc_key key[1];
  21992. mp_int r[1], s[1], expR[1], expS[1];
  21993. #endif
  21994. int key_inited = 0;
  21995. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  21996. unsigned char hash[32];
  21997. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  21998. "0FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75C"
  21999. "AA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83"
  22000. "538";
  22001. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  22002. "1894550D0785932E00EAA23B694F213F8C3121F86DC97A04E5A7167DB4E5BCD3"
  22003. "71123D46E45DB6B5D5370A7F20FB633155D38FFA16D2BD761DCAC474B9A2F502"
  22004. "3A4";
  22005. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  22006. "0493101C962CD4D2FDDF782285E64584139C2F91B47F87FF82354D6630F746A2"
  22007. "8A0DB25741B5B34A828008B22ACC23F924FAAFBD4D33F81EA66956DFEAA2BFDF"
  22008. "CF5";
  22009. WOLFSSL_SMALL_STACK_STATIC const char* expRstr =
  22010. "1511BB4D675114FE266FC4372B87682BAECC01D3CC62CF2303C92B3526012659"
  22011. "D16876E25C7C1E57648F23B73564D67F61C6F14D527D54972810421E7D87589E"
  22012. "1A7";
  22013. WOLFSSL_SMALL_STACK_STATIC const char* expSstr =
  22014. "04A171143A83163D6DF460AAF61522695F207A58B95C0644D87E52AA1A347916"
  22015. "E4F7A72930B1BC06DBE22CE3F58264AFD23704CBB63B29B931F7DE6C9D949A7E"
  22016. "CFC";
  22017. #ifdef WOLFSSL_SMALL_STACK
  22018. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22019. r = (mp_int *)XMALLOC(sizeof(*r), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22020. s = (mp_int *)XMALLOC(sizeof(*s), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22021. expR = (mp_int *)XMALLOC(sizeof(*expR), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22022. expS = (mp_int *)XMALLOC(sizeof(*expS), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22023. if ((key == NULL) ||
  22024. (r == NULL) ||
  22025. (s == NULL) ||
  22026. (expR == NULL) ||
  22027. (expS == NULL))
  22028. {
  22029. ret = MEMORY_E;
  22030. goto done;
  22031. }
  22032. #endif
  22033. ret = mp_init_multi(r, s, expR, expS, NULL, NULL);
  22034. if (ret != MP_OKAY) {
  22035. goto done;
  22036. }
  22037. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  22038. if (ret != 0) {
  22039. return ret;
  22040. }
  22041. key_inited = 1;
  22042. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP521R1");
  22043. if (ret != 0) {
  22044. goto done;
  22045. }
  22046. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  22047. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  22048. if (ret != 0) {
  22049. goto done;
  22050. }
  22051. ret = wc_ecc_set_deterministic(key, 1);
  22052. if (ret != 0) {
  22053. goto done;
  22054. }
  22055. do {
  22056. #if defined(WOLFSSL_ASYNC_CRYPT)
  22057. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22058. #endif
  22059. if (ret == 0)
  22060. ret = wc_ecc_sign_hash_ex(hash, sizeof(hash), rng, key, r, s);
  22061. } while (ret == WC_PENDING_E);
  22062. if (ret != 0) {
  22063. goto done;
  22064. }
  22065. TEST_SLEEP();
  22066. mp_read_radix(expR, expRstr, MP_RADIX_HEX);
  22067. mp_read_radix(expS, expSstr, MP_RADIX_HEX);
  22068. if (mp_cmp(r, expR) != MP_EQ) {
  22069. ret = WC_TEST_RET_ENC_NC;
  22070. }
  22071. done:
  22072. if (key_inited)
  22073. wc_ecc_free(key);
  22074. #ifdef WOLFSSL_SMALL_STACK
  22075. if (key != NULL)
  22076. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22077. if (r != NULL)
  22078. XFREE(r, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22079. if (s != NULL)
  22080. XFREE(s, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22081. if (expR != NULL)
  22082. XFREE(expR, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22083. if (expS != NULL)
  22084. XFREE(expS, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22085. #endif
  22086. return ret;
  22087. }
  22088. #endif /* HAVE_ECC521 */
  22089. #endif /* WOLFSSL_PUBLIC_MP */
  22090. #endif /* HAVE_ECC_SIGN && (WOLFSSL_ECDSA_DETERMINISTIC_K ||
  22091. WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  22092. && (!FIPS_VERSION_GE || FIPS_VERSION_GE(5,3)) */
  22093. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K) && \
  22094. !defined(WOLFSSL_KCAPI_ECC)
  22095. static wc_test_ret_t ecc_test_sign_vectors(WC_RNG* rng)
  22096. {
  22097. wc_test_ret_t ret;
  22098. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22099. ecc_key *key = NULL;
  22100. #else
  22101. ecc_key key[1];
  22102. #endif
  22103. int key_inited = 0;
  22104. byte sig[72];
  22105. word32 sigSz;
  22106. WOLFSSL_SMALL_STACK_STATIC const unsigned char hash[32] = "test wolfSSL deterministic sign";
  22107. WOLFSSL_SMALL_STACK_STATIC const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  22108. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  22109. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  22110. WOLFSSL_SMALL_STACK_STATIC const byte k[1] = { 0x02 };
  22111. WOLFSSL_SMALL_STACK_STATIC const byte expSig[71] = {
  22112. 0x30, 0x45, 0x02, 0x20, 0x7c, 0xf2, 0x7b, 0x18,
  22113. 0x8d, 0x03, 0x4f, 0x7e, 0x8a, 0x52, 0x38, 0x03,
  22114. 0x04, 0xb5, 0x1a, 0xc3, 0xc0, 0x89, 0x69, 0xe2,
  22115. 0x77, 0xf2, 0x1b, 0x35, 0xa6, 0x0b, 0x48, 0xfc,
  22116. 0x47, 0x66, 0x99, 0x78, 0x02, 0x21, 0x00, 0xa8,
  22117. 0x43, 0xa0, 0xce, 0x6c, 0x5e, 0x17, 0x8a, 0x53,
  22118. 0x4d, 0xaf, 0xd2, 0x95, 0x78, 0x9f, 0x84, 0x4f,
  22119. 0x94, 0xb8, 0x75, 0xa3, 0x19, 0xa5, 0xd4, 0xdf,
  22120. 0xe1, 0xd4, 0x5e, 0x9d, 0x97, 0xfe, 0x81
  22121. };
  22122. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22123. if ((key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  22124. return MEMORY_E;
  22125. #endif
  22126. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  22127. if (ret != 0) {
  22128. goto done;
  22129. }
  22130. key_inited = 1;
  22131. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP256R1");
  22132. if (ret != 0) {
  22133. goto done;
  22134. }
  22135. #if (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) \
  22136. && (HAVE_FIPS_VERSION > 2)))
  22137. wc_ecc_set_flags(key, WC_ECC_FLAG_DEC_SIGN);
  22138. #endif
  22139. ret = wc_ecc_sign_set_k(k, sizeof(k), key);
  22140. if (ret != 0) {
  22141. goto done;
  22142. }
  22143. sigSz = sizeof(sig);
  22144. do {
  22145. #if defined(WOLFSSL_ASYNC_CRYPT)
  22146. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22147. #endif
  22148. if (ret == 0)
  22149. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  22150. } while (ret == WC_PENDING_E);
  22151. if (ret != 0) {
  22152. goto done;
  22153. }
  22154. TEST_SLEEP();
  22155. if (sigSz != sizeof(expSig)) {
  22156. ret = WC_TEST_RET_ENC_NC;
  22157. goto done;
  22158. }
  22159. if (XMEMCMP(sig, expSig, sigSz) != 0) {
  22160. ret = WC_TEST_RET_ENC_NC;
  22161. goto done;
  22162. }
  22163. sigSz = sizeof(sig);
  22164. do {
  22165. #if defined(WOLFSSL_ASYNC_CRYPT)
  22166. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22167. #endif
  22168. if (ret == 0)
  22169. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  22170. } while (ret == WC_PENDING_E);
  22171. if (ret != 0) {
  22172. goto done;
  22173. }
  22174. TEST_SLEEP();
  22175. done:
  22176. if (key_inited)
  22177. wc_ecc_free(key);
  22178. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22179. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22180. #endif
  22181. return ret;
  22182. }
  22183. #endif
  22184. #if defined(HAVE_ECC_CDH) && defined(HAVE_ECC_DHE)
  22185. static wc_test_ret_t ecc_test_cdh_vectors(WC_RNG* rng)
  22186. {
  22187. wc_test_ret_t ret;
  22188. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22189. ecc_key *pub_key = (ecc_key *)XMALLOC(sizeof *pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22190. ecc_key *priv_key = (ecc_key *)XMALLOC(sizeof *priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22191. #else
  22192. ecc_key pub_key[1], priv_key[1];
  22193. #endif
  22194. byte sharedA[32] = {0}, sharedB[32] = {0};
  22195. word32 x, z;
  22196. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSx = "700c48f77f56584c5cc632ca65640db91b6bacce3a4df6b42ce7cc838833d287";
  22197. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSy = "db71e509e3fd9b060ddb20ba5c51dcc5948d46fbf640dfe0441782cab85fa4ac";
  22198. WOLFSSL_SMALL_STACK_STATIC const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  22199. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  22200. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  22201. WOLFSSL_SMALL_STACK_STATIC const char* ZIUT = "46fc62106420ff012e54a434fbdd2d25ccc5852060561e68040dd7778997bd7b";
  22202. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22203. if ((pub_key == NULL) ||
  22204. (priv_key == NULL)) {
  22205. ret = MEMORY_E;
  22206. goto done;
  22207. }
  22208. #endif
  22209. XMEMSET(pub_key, 0, sizeof *pub_key);
  22210. XMEMSET(priv_key, 0, sizeof *priv_key);
  22211. /* setup private and public keys */
  22212. ret = wc_ecc_init_ex(pub_key, HEAP_HINT, devId);
  22213. if (ret != 0)
  22214. goto done;
  22215. ret = wc_ecc_init_ex(priv_key, HEAP_HINT, devId);
  22216. if (ret != 0)
  22217. goto done;
  22218. wc_ecc_set_flags(pub_key, WC_ECC_FLAG_COFACTOR);
  22219. wc_ecc_set_flags(priv_key, WC_ECC_FLAG_COFACTOR);
  22220. ret = wc_ecc_import_raw(pub_key, QCAVSx, QCAVSy, NULL, "SECP256R1");
  22221. if (ret != 0)
  22222. goto done;
  22223. ret = wc_ecc_import_raw(priv_key, QIUTx, QIUTy, dIUT, "SECP256R1");
  22224. if (ret != 0)
  22225. goto done;
  22226. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22227. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22228. !defined(HAVE_SELFTEST)
  22229. ret = wc_ecc_set_rng(priv_key, rng);
  22230. if (ret != 0)
  22231. goto done;
  22232. #else
  22233. (void)rng;
  22234. #endif
  22235. /* compute ECC Cofactor shared secret */
  22236. x = sizeof(sharedA);
  22237. do {
  22238. #if defined(WOLFSSL_ASYNC_CRYPT)
  22239. ret = wc_AsyncWait(ret, &priv_key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22240. #endif
  22241. if (ret == 0)
  22242. ret = wc_ecc_shared_secret(priv_key, pub_key, sharedA, &x);
  22243. } while (ret == WC_PENDING_E);
  22244. if (ret != 0) {
  22245. goto done;
  22246. }
  22247. TEST_SLEEP();
  22248. /* read in expected Z */
  22249. z = sizeof(sharedB);
  22250. ret = Base16_Decode((const byte*)ZIUT, (word32)XSTRLEN(ZIUT), sharedB, &z);
  22251. if (ret != 0)
  22252. goto done;
  22253. /* compare results */
  22254. if (x != z || XMEMCMP(sharedA, sharedB, x)) {
  22255. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22256. }
  22257. done:
  22258. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22259. if (priv_key) {
  22260. wc_ecc_free(priv_key);
  22261. XFREE(priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22262. }
  22263. if (pub_key) {
  22264. wc_ecc_free(pub_key);
  22265. XFREE(pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22266. }
  22267. #else
  22268. wc_ecc_free(priv_key);
  22269. wc_ecc_free(pub_key);
  22270. #endif
  22271. return ret;
  22272. }
  22273. #endif /* HAVE_ECC_CDH && HAVE_ECC_DHE */
  22274. #endif /* HAVE_ECC_VECTOR_TEST */
  22275. #ifdef HAVE_ECC_KEY_IMPORT
  22276. /* returns 0 on success */
  22277. static wc_test_ret_t ecc_test_make_pub(WC_RNG* rng)
  22278. {
  22279. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22280. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22281. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  22282. ecc_key *pub = (ecc_key *)XMALLOC(sizeof *pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22283. #endif
  22284. byte *exportBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22285. byte *tmp = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22286. #else
  22287. ecc_key key[1];
  22288. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  22289. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  22290. ecc_key pub[1];
  22291. #endif
  22292. byte exportBuf[ECC_BUFSIZE];
  22293. byte tmp[ECC_BUFSIZE];
  22294. #endif
  22295. const byte* msg = (const byte*)"test wolfSSL ECC public gen";
  22296. word32 x;
  22297. word32 tmpSz;
  22298. wc_test_ret_t ret = 0;
  22299. ecc_point* pubPoint = NULL;
  22300. #ifdef HAVE_ECC_VERIFY
  22301. int verify = 0;
  22302. #endif
  22303. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22304. if ((key == NULL) ||
  22305. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  22306. (pub == NULL) ||
  22307. #endif
  22308. (exportBuf == NULL) ||
  22309. (tmp == NULL))
  22310. ERROR_OUT(MEMORY_E, done);
  22311. #endif
  22312. (void)msg;
  22313. (void)verify;
  22314. (void)exportBuf;
  22315. (void)rng;
  22316. wc_ecc_init_ex(key, HEAP_HINT, devId);
  22317. #ifndef NO_ECC256
  22318. #ifdef USE_CERT_BUFFERS_256
  22319. XMEMCPY(tmp, ecc_key_der_256, (size_t)sizeof_ecc_key_der_256);
  22320. tmpSz = (size_t)sizeof_ecc_key_der_256;
  22321. #else
  22322. {
  22323. XFILE file = XFOPEN(eccKeyDerFile, "rb");
  22324. if (!file) {
  22325. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  22326. }
  22327. tmpSz = (word32)XFREAD(tmp, 1, ECC_BUFSIZE, file);
  22328. XFCLOSE(file);
  22329. if (tmpSz == 0)
  22330. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  22331. }
  22332. #endif /* USE_CERT_BUFFERS_256 */
  22333. /* import private only then test with */
  22334. ret = wc_ecc_import_private_key(tmp, tmpSz, NULL, 0, NULL);
  22335. if (ret == 0) {
  22336. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22337. }
  22338. ret = wc_ecc_import_private_key(NULL, tmpSz, NULL, 0, key);
  22339. if (ret == 0) {
  22340. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22341. }
  22342. x = 0;
  22343. ret = wc_EccPrivateKeyDecode(tmp, &x, key, tmpSz);
  22344. if (ret != 0)
  22345. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22346. #ifdef HAVE_ECC_KEY_EXPORT
  22347. x = ECC_BUFSIZE;
  22348. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  22349. if (ret != 0)
  22350. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22351. /* make private only key */
  22352. wc_ecc_free(key);
  22353. wc_ecc_init_ex(key, HEAP_HINT, devId);
  22354. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  22355. if (ret != 0)
  22356. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22357. x = ECC_BUFSIZE;
  22358. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  22359. if (ret == 0) {
  22360. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22361. }
  22362. #endif /* HAVE_ECC_KEY_EXPORT */
  22363. ret = wc_ecc_make_pub(NULL, NULL);
  22364. if (ret == 0) {
  22365. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22366. }
  22367. TEST_SLEEP();
  22368. #ifndef WOLFSSL_NO_MALLOC
  22369. pubPoint = wc_ecc_new_point_h(HEAP_HINT);
  22370. if (pubPoint == NULL) {
  22371. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  22372. }
  22373. #if !defined(WOLFSSL_CRYPTOCELL)
  22374. ret = wc_ecc_make_pub(key, pubPoint);
  22375. #if defined(WOLFSSL_ASYNC_CRYPT)
  22376. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  22377. #endif
  22378. if (ret != 0)
  22379. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22380. #endif
  22381. TEST_SLEEP();
  22382. #ifdef HAVE_ECC_KEY_EXPORT
  22383. /* export should still fail, is private only key */
  22384. x = ECC_BUFSIZE;
  22385. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  22386. if (ret == 0) {
  22387. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22388. }
  22389. #endif /* HAVE_ECC_KEY_EXPORT */
  22390. #endif /* !WOLFSSL_NO_MALLOC */
  22391. #endif /* !NO_ECC256 */
  22392. /* create a new key since above test for loading key is not supported */
  22393. #if defined(WOLFSSL_CRYPTOCELL) || defined(NO_ECC256) || \
  22394. defined(WOLFSSL_QNX_CAAM) || defined(WOLFSSL_SE050) || \
  22395. defined(WOLFSSL_SECO_CAAM) || defined(WOLFSSL_IMXRT1170_CAAM)
  22396. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  22397. if (ret != 0)
  22398. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22399. #endif
  22400. #if defined(HAVE_ECC_SIGN) && (!defined(ECC_TIMING_RESISTANT) || \
  22401. (defined(ECC_TIMING_RESISTANT) && !defined(WC_NO_RNG))) && \
  22402. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(HAVE_ECC_DETERMINISTIC_K)
  22403. tmpSz = ECC_BUFSIZE;
  22404. ret = 0;
  22405. do {
  22406. #if defined(WOLFSSL_ASYNC_CRYPT)
  22407. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22408. #endif
  22409. if (ret == 0) {
  22410. ret = wc_ecc_sign_hash(msg, (word32)XSTRLEN((const char* )msg), tmp,
  22411. &tmpSz, rng, key);
  22412. }
  22413. } while (ret == WC_PENDING_E);
  22414. if (ret != 0)
  22415. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22416. TEST_SLEEP();
  22417. #ifdef HAVE_ECC_VERIFY
  22418. /* try verify with private only key */
  22419. ret = 0;
  22420. do {
  22421. #if defined(WOLFSSL_ASYNC_CRYPT)
  22422. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22423. #endif
  22424. if (ret == 0) {
  22425. ret = wc_ecc_verify_hash(tmp, tmpSz, msg,
  22426. (word32)XSTRLEN((const char*)msg), &verify, key);
  22427. }
  22428. } while (ret == WC_PENDING_E);
  22429. if (ret != 0)
  22430. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22431. if (verify != 1) {
  22432. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22433. }
  22434. TEST_SLEEP();
  22435. #ifdef HAVE_ECC_KEY_EXPORT
  22436. /* exporting the public part should now work */
  22437. x = ECC_BUFSIZE;
  22438. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  22439. if (ret != 0)
  22440. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22441. #endif /* HAVE_ECC_KEY_EXPORT */
  22442. #endif /* HAVE_ECC_VERIFY */
  22443. #endif /* HAVE_ECC_SIGN */
  22444. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22445. /* now test private only key with creating a shared secret */
  22446. x = ECC_BUFSIZE;
  22447. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  22448. if (ret != 0)
  22449. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22450. #if !defined(WOLFSSL_QNX_CAAM) && !defined(WOLFSSL_SE050)
  22451. /* make private only key */
  22452. wc_ecc_free(key);
  22453. wc_ecc_init_ex(key, HEAP_HINT, devId);
  22454. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  22455. if (ret != 0)
  22456. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22457. /* check that public export fails with private only key */
  22458. x = ECC_BUFSIZE;
  22459. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  22460. if (ret == 0) {
  22461. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22462. }
  22463. #endif /* WOLFSSL_QNX_CAAM */
  22464. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  22465. /* make public key for shared secret */
  22466. wc_ecc_init_ex(pub, HEAP_HINT, devId);
  22467. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, pub);
  22468. #ifdef HAVE_ECC_CDH
  22469. wc_ecc_set_flags(key, WC_ECC_FLAG_COFACTOR);
  22470. #endif
  22471. #if defined(WOLFSSL_ASYNC_CRYPT)
  22472. ret = wc_AsyncWait(ret, &pub->asyncDev, WC_ASYNC_FLAG_NONE);
  22473. #endif
  22474. if (ret != 0)
  22475. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22476. TEST_SLEEP();
  22477. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22478. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22479. !defined(HAVE_SELFTEST)
  22480. ret = wc_ecc_set_rng(key, rng);
  22481. if (ret != 0)
  22482. goto done;
  22483. #endif
  22484. x = ECC_BUFSIZE;
  22485. do {
  22486. #if defined(WOLFSSL_ASYNC_CRYPT)
  22487. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22488. #endif
  22489. if (ret == 0) {
  22490. ret = wc_ecc_shared_secret(key, pub, exportBuf, &x);
  22491. }
  22492. } while (ret == WC_PENDING_E);
  22493. wc_ecc_free(pub);
  22494. if (ret != 0)
  22495. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22496. TEST_SLEEP();
  22497. #endif /* HAVE_ECC_DHE && HAVE_ECC_KEY_EXPORT && !WC_NO_RNG */
  22498. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  22499. ret = 0;
  22500. done:
  22501. wc_ecc_del_point_h(pubPoint, HEAP_HINT);
  22502. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22503. if (key != NULL) {
  22504. wc_ecc_free(key);
  22505. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22506. }
  22507. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  22508. if (pub != NULL)
  22509. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22510. #endif
  22511. if (exportBuf != NULL)
  22512. XFREE(exportBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22513. if (tmp != NULL)
  22514. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22515. #else
  22516. wc_ecc_free(key);
  22517. #endif
  22518. return ret;
  22519. }
  22520. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && \
  22521. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  22522. static wc_test_ret_t ecc_test_key_decode(WC_RNG* rng, int keySize)
  22523. {
  22524. wc_test_ret_t ret;
  22525. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22526. ecc_key *eccKey = (ecc_key *)XMALLOC(sizeof *eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22527. byte *tmpBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22528. #else
  22529. ecc_key eccKey[1];
  22530. byte tmpBuf[ECC_BUFSIZE];
  22531. #endif
  22532. word32 tmpSz;
  22533. word32 idx;
  22534. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22535. if ((eccKey == NULL) || (tmpBuf == NULL))
  22536. ERROR_OUT(MEMORY_E, done);
  22537. #endif
  22538. ret = wc_ecc_init(eccKey);
  22539. if (ret != 0) {
  22540. goto done;
  22541. }
  22542. ret = wc_ecc_make_key(rng, keySize, eccKey);
  22543. #if defined(WOLFSSL_ASYNC_CRYPT)
  22544. ret = wc_AsyncWait(ret, &eccKey->asyncDev, WC_ASYNC_FLAG_NONE);
  22545. #endif
  22546. if (ret != 0) {
  22547. goto done;
  22548. }
  22549. tmpSz = ECC_BUFSIZE;
  22550. ret = wc_EccKeyToDer(eccKey, tmpBuf, tmpSz);
  22551. wc_ecc_free(eccKey);
  22552. if (ret < 0) {
  22553. goto done;
  22554. }
  22555. tmpSz = (word32)ret;
  22556. ret = wc_ecc_init(eccKey);
  22557. if (ret != 0) {
  22558. goto done;
  22559. }
  22560. idx = 0;
  22561. ret = wc_EccPrivateKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  22562. if (ret != 0) {
  22563. goto done;
  22564. }
  22565. wc_ecc_free(eccKey);
  22566. ret = wc_ecc_init(eccKey);
  22567. if (ret != 0) {
  22568. goto done;
  22569. }
  22570. idx = 0;
  22571. ret = wc_EccPublicKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  22572. if (ret != 0) {
  22573. goto done;
  22574. }
  22575. ret = 0;
  22576. done:
  22577. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22578. if (eccKey != NULL) {
  22579. wc_ecc_free(eccKey);
  22580. XFREE(eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22581. }
  22582. if (tmpBuf != NULL)
  22583. XFREE(tmpBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22584. #else
  22585. wc_ecc_free(eccKey);
  22586. #endif
  22587. return ret;
  22588. }
  22589. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  22590. #endif /* HAVE_ECC_KEY_IMPORT */
  22591. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && \
  22592. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  22593. static wc_test_ret_t ecc_test_key_gen(WC_RNG* rng, int keySize)
  22594. {
  22595. wc_test_ret_t ret = 0;
  22596. int derSz;
  22597. #ifdef HAVE_PKCS8
  22598. word32 pkcs8Sz;
  22599. #endif
  22600. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22601. byte *der = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22602. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22603. #else
  22604. byte der[ECC_BUFSIZE];
  22605. ecc_key userA[1];
  22606. #endif
  22607. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22608. if ((der == NULL) || (userA == NULL))
  22609. ERROR_OUT(MEMORY_E, done);
  22610. #endif
  22611. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  22612. if (ret != 0)
  22613. goto done;
  22614. ret = wc_ecc_make_key(rng, keySize, userA);
  22615. #if defined(WOLFSSL_ASYNC_CRYPT)
  22616. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  22617. #endif
  22618. if (ret != 0)
  22619. goto done;
  22620. TEST_SLEEP();
  22621. ret = wc_ecc_check_key(userA);
  22622. if (ret != 0)
  22623. goto done;
  22624. TEST_SLEEP();
  22625. derSz = wc_EccKeyToDer(userA, der, ECC_BUFSIZE);
  22626. if (derSz < 0) {
  22627. ERROR_OUT(derSz, done);
  22628. }
  22629. ret = SaveDerAndPem(der, derSz, eccCaKeyTempFile, eccCaKeyPemFile,
  22630. ECC_PRIVATEKEY_TYPE);
  22631. if (ret != 0) {
  22632. goto done;
  22633. }
  22634. /* test export of public key */
  22635. derSz = wc_EccPublicKeyToDer(userA, der, ECC_BUFSIZE, 1);
  22636. if (derSz < 0) {
  22637. ERROR_OUT(derSz, done);
  22638. }
  22639. if (derSz == 0) {
  22640. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22641. }
  22642. #ifdef HAVE_COMP_KEY
  22643. /* test export of compressed public key */
  22644. derSz = wc_EccPublicKeyToDer_ex(userA, der, ECC_BUFSIZE, 1, 1);
  22645. if (derSz < 0) {
  22646. ERROR_OUT(derSz, done);
  22647. }
  22648. if (derSz == 0) {
  22649. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22650. }
  22651. #endif
  22652. ret = SaveDerAndPem(der, derSz, eccPubKeyDerFile, NULL, 0);
  22653. if (ret != 0) {
  22654. goto done;
  22655. }
  22656. #ifdef HAVE_PKCS8
  22657. /* test export of PKCS#8 unencrypted private key */
  22658. pkcs8Sz = FOURK_BUF;
  22659. derSz = wc_EccPrivateKeyToPKCS8(userA, der, &pkcs8Sz);
  22660. if (derSz < 0) {
  22661. ERROR_OUT(derSz, done);
  22662. }
  22663. if (derSz == 0) {
  22664. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22665. }
  22666. ret = SaveDerAndPem(der, derSz, eccPkcs8KeyDerFile, NULL, 0);
  22667. if (ret != 0) {
  22668. goto done;
  22669. }
  22670. #endif /* HAVE_PKCS8 */
  22671. done:
  22672. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22673. if (der != NULL)
  22674. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22675. if (userA != NULL) {
  22676. wc_ecc_free(userA);
  22677. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22678. }
  22679. #else
  22680. wc_ecc_free(userA);
  22681. #endif
  22682. return ret;
  22683. }
  22684. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  22685. static wc_test_ret_t ecc_test_curve_size(WC_RNG* rng, int keySize, int testVerifyCount,
  22686. int curve_id, const ecc_set_type* dp)
  22687. {
  22688. #if defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG) && \
  22689. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  22690. WC_DECLARE_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  22691. WC_DECLARE_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  22692. word32 y;
  22693. #endif
  22694. #ifdef HAVE_ECC_KEY_EXPORT
  22695. #define ECC_KEY_EXPORT_BUF_SIZE (MAX_ECC_BYTES * 2 + 32)
  22696. WC_DECLARE_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  22697. #endif
  22698. word32 x = 0;
  22699. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  22700. !defined(WC_NO_RNG) && !defined(WOLFSSL_KCAPI_ECC)) && \
  22701. defined(HAVE_ECC_SIGN)
  22702. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  22703. WC_DECLARE_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  22704. int i;
  22705. #ifdef HAVE_ECC_VERIFY
  22706. int verify;
  22707. #endif /* HAVE_ECC_VERIFY */
  22708. #endif /* HAVE_ECC_SIGN */
  22709. wc_test_ret_t ret;
  22710. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22711. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22712. ecc_key *userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22713. ecc_key *pubKey = (ecc_key *)XMALLOC(sizeof *pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22714. #else
  22715. ecc_key userA[1];
  22716. ecc_key userB[1];
  22717. ecc_key pubKey[1];
  22718. #endif
  22719. #ifndef WC_NO_RNG
  22720. int curveSize;
  22721. #endif
  22722. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  22723. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG) && \
  22724. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  22725. if (sharedA == NULL || sharedB == NULL)
  22726. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  22727. #endif
  22728. #ifdef HAVE_ECC_KEY_EXPORT
  22729. if (exportBuf == NULL)
  22730. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  22731. #endif
  22732. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  22733. !defined(WC_NO_RNG) && !defined(WOLFSSL_KCAPI_ECC)) && \
  22734. defined(HAVE_ECC_SIGN)
  22735. if (sig == NULL || digest == NULL)
  22736. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  22737. #endif
  22738. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  22739. (void)testVerifyCount;
  22740. (void)dp;
  22741. (void)x;
  22742. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22743. if ((userA == NULL) ||
  22744. (userB == NULL) ||
  22745. (pubKey == NULL))
  22746. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22747. #endif
  22748. XMEMSET(userA, 0, sizeof *userA);
  22749. XMEMSET(userB, 0, sizeof *userB);
  22750. XMEMSET(pubKey, 0, sizeof *pubKey);
  22751. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  22752. if (ret != 0)
  22753. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22754. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  22755. if (ret != 0)
  22756. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22757. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  22758. if (ret != 0)
  22759. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22760. #ifdef WOLFSSL_CUSTOM_CURVES
  22761. if (dp != NULL) {
  22762. ret = wc_ecc_set_custom_curve(userA, dp);
  22763. if (ret != 0)
  22764. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22765. ret = wc_ecc_set_custom_curve(userB, dp);
  22766. if (ret != 0)
  22767. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22768. }
  22769. #endif
  22770. #ifndef WC_NO_RNG
  22771. ret = wc_ecc_make_key_ex(rng, keySize, userA, curve_id);
  22772. #if defined(WOLFSSL_ASYNC_CRYPT)
  22773. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  22774. #endif
  22775. #ifdef WOLF_CRYPTO_CB_ONLY_ECC
  22776. if (ret == NO_VALID_DEVID) {
  22777. ret = 0;
  22778. goto done; /* no software case */
  22779. }
  22780. #endif
  22781. if (ret == ECC_CURVE_OID_E)
  22782. goto done; /* catch case, where curve is not supported */
  22783. if (ret != 0)
  22784. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22785. TEST_SLEEP();
  22786. if (wc_ecc_get_curve_idx(curve_id) != -1) {
  22787. curveSize = wc_ecc_get_curve_size_from_id(userA->dp->id);
  22788. if (curveSize != userA->dp->size)
  22789. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22790. }
  22791. ret = wc_ecc_check_key(userA);
  22792. if (ret != 0)
  22793. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22794. TEST_SLEEP();
  22795. /* ATECC508/608 configuration may not support more than one ECDH key */
  22796. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  22797. ret = wc_ecc_make_key_ex(rng, keySize, userB, curve_id);
  22798. #if defined(WOLFSSL_ASYNC_CRYPT)
  22799. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  22800. #endif
  22801. if (ret != 0)
  22802. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22803. TEST_SLEEP();
  22804. /* only perform the below tests if the key size matches */
  22805. if (dp == NULL && keySize > 0 && wc_ecc_size(userA) != keySize)
  22806. ERROR_OUT(ECC_CURVE_OID_E, done);
  22807. #ifdef HAVE_ECC_DHE
  22808. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22809. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22810. !defined(HAVE_SELFTEST)
  22811. ret = wc_ecc_set_rng(userA, rng);
  22812. if (ret != 0)
  22813. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22814. ret = wc_ecc_set_rng(userB, rng);
  22815. if (ret != 0)
  22816. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22817. #endif
  22818. x = ECC_SHARED_SIZE;
  22819. do {
  22820. #if defined(WOLFSSL_ASYNC_CRYPT)
  22821. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22822. #endif
  22823. if (ret == 0)
  22824. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  22825. } while (ret == WC_PENDING_E);
  22826. if (ret != 0)
  22827. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22828. TEST_SLEEP();
  22829. y = ECC_SHARED_SIZE;
  22830. do {
  22831. #if defined(WOLFSSL_ASYNC_CRYPT)
  22832. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22833. #endif
  22834. if (ret == 0)
  22835. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  22836. } while (ret == WC_PENDING_E);
  22837. if (ret != 0)
  22838. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22839. if (y != x)
  22840. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22841. if (XMEMCMP(sharedA, sharedB, x))
  22842. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22843. TEST_SLEEP();
  22844. #ifdef HAVE_ECC_CDH
  22845. /* add cofactor flag */
  22846. wc_ecc_set_flags(userA, WC_ECC_FLAG_COFACTOR);
  22847. wc_ecc_set_flags(userB, WC_ECC_FLAG_COFACTOR);
  22848. x = ECC_SHARED_SIZE;
  22849. do {
  22850. #if defined(WOLFSSL_ASYNC_CRYPT)
  22851. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22852. #endif
  22853. if (ret == 0)
  22854. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  22855. } while (ret == WC_PENDING_E);
  22856. if (ret != 0)
  22857. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22858. TEST_SLEEP();
  22859. y = ECC_SHARED_SIZE;
  22860. do {
  22861. #if defined(WOLFSSL_ASYNC_CRYPT)
  22862. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22863. #endif
  22864. if (ret == 0)
  22865. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  22866. } while (ret == WC_PENDING_E);
  22867. if (ret != 0)
  22868. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22869. if (y != x)
  22870. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22871. if (XMEMCMP(sharedA, sharedB, x))
  22872. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22873. TEST_SLEEP();
  22874. /* remove cofactor flag */
  22875. wc_ecc_set_flags(userA, 0);
  22876. wc_ecc_set_flags(userB, 0);
  22877. #endif /* HAVE_ECC_CDH */
  22878. #endif /* HAVE_ECC_DHE */
  22879. #endif /* !WOLFSSL_ATECC508A && WOLFSSL_ATECC608A */
  22880. #ifdef HAVE_ECC_KEY_EXPORT
  22881. x = ECC_KEY_EXPORT_BUF_SIZE;
  22882. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 0);
  22883. if (ret != 0)
  22884. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22885. #ifdef HAVE_ECC_KEY_IMPORT
  22886. #ifdef WOLFSSL_CUSTOM_CURVES
  22887. if (dp != NULL) {
  22888. ret = wc_ecc_set_custom_curve(pubKey, dp);
  22889. if (ret != 0)
  22890. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22891. }
  22892. #endif
  22893. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  22894. if (ret != 0)
  22895. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22896. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  22897. #ifdef HAVE_ECC_DHE
  22898. y = ECC_SHARED_SIZE;
  22899. do {
  22900. #if defined(WOLFSSL_ASYNC_CRYPT)
  22901. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22902. #endif
  22903. if (ret == 0)
  22904. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  22905. } while (ret == WC_PENDING_E);
  22906. if (ret != 0)
  22907. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22908. if (XMEMCMP(sharedA, sharedB, y))
  22909. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22910. TEST_SLEEP();
  22911. #endif /* HAVE_ECC_DHE */
  22912. #ifdef HAVE_COMP_KEY
  22913. /* try compressed export / import too */
  22914. x = ECC_KEY_EXPORT_BUF_SIZE;
  22915. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 1);
  22916. if (ret != 0)
  22917. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22918. wc_ecc_free(pubKey);
  22919. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  22920. if (ret != 0)
  22921. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22922. #ifdef WOLFSSL_CUSTOM_CURVES
  22923. if (dp != NULL) {
  22924. ret = wc_ecc_set_custom_curve(pubKey, dp);
  22925. if (ret != 0)
  22926. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22927. }
  22928. #endif
  22929. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  22930. if (ret != 0)
  22931. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22932. #ifdef HAVE_ECC_DHE
  22933. y = ECC_SHARED_SIZE;
  22934. do {
  22935. #if defined(WOLFSSL_ASYNC_CRYPT)
  22936. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22937. #endif
  22938. if (ret == 0)
  22939. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  22940. } while (ret == WC_PENDING_E);
  22941. if (ret != 0)
  22942. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22943. if (XMEMCMP(sharedA, sharedB, y))
  22944. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22945. TEST_SLEEP();
  22946. #endif /* HAVE_ECC_DHE */
  22947. #endif /* HAVE_COMP_KEY */
  22948. #endif /* !WOLFSSL_ATECC508A && !WOLFSSL_ATECC608A */
  22949. #endif /* !WC_NO_RNG */
  22950. #endif /* HAVE_ECC_KEY_IMPORT */
  22951. #endif /* HAVE_ECC_KEY_EXPORT */
  22952. /* For KCAPI cannot sign using generated ECDH key */
  22953. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  22954. !defined(WC_NO_RNG) && !defined(WOLFSSL_KCAPI_ECC))
  22955. #ifdef HAVE_ECC_SIGN
  22956. /* ECC w/out Shamir has issue with all 0 digest */
  22957. /* WC_BIGINT doesn't have 0 len well on hardware */
  22958. /* Cryptocell has issues with all 0 digest */
  22959. #if defined(ECC_SHAMIR) && !defined(WOLFSSL_ASYNC_CRYPT) && \
  22960. !defined(WOLFSSL_CRYPTOCELL)
  22961. /* test DSA sign hash with zeros */
  22962. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  22963. digest[i] = 0;
  22964. }
  22965. x = ECC_SIG_SIZE;
  22966. do {
  22967. #if defined(WOLFSSL_ASYNC_CRYPT)
  22968. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22969. #endif
  22970. if (ret == 0)
  22971. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng,
  22972. userA);
  22973. } while (ret == WC_PENDING_E);
  22974. if (ret != 0)
  22975. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22976. TEST_SLEEP();
  22977. #ifdef HAVE_ECC_VERIFY
  22978. for (i=0; i<testVerifyCount; i++) {
  22979. verify = 0;
  22980. do {
  22981. #if defined(WOLFSSL_ASYNC_CRYPT)
  22982. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22983. #endif
  22984. if (ret == 0)
  22985. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE,
  22986. &verify, userA);
  22987. } while (ret == WC_PENDING_E);
  22988. if (ret != 0)
  22989. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  22990. if (verify != 1)
  22991. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  22992. TEST_SLEEP();
  22993. }
  22994. #endif /* HAVE_ECC_VERIFY */
  22995. #endif /* ECC_SHAMIR && !WOLFSSL_ASYNC_CRYPT && !WOLFSSL_CRYPTOCELL */
  22996. /* test DSA sign hash with sequence (0,1,2,3,4,...) */
  22997. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  22998. digest[i] = (byte)i;
  22999. }
  23000. x = ECC_SIG_SIZE;
  23001. do {
  23002. #if defined(WOLFSSL_ASYNC_CRYPT)
  23003. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  23004. #endif
  23005. if (ret == 0)
  23006. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng, userA);
  23007. } while (ret == WC_PENDING_E);
  23008. if (ret != 0)
  23009. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  23010. TEST_SLEEP();
  23011. #ifdef HAVE_ECC_VERIFY
  23012. for (i=0; i<testVerifyCount; i++) {
  23013. verify = 0;
  23014. do {
  23015. #if defined(WOLFSSL_ASYNC_CRYPT)
  23016. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  23017. #endif
  23018. if (ret == 0)
  23019. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE, &verify, userA);
  23020. } while (ret == WC_PENDING_E);
  23021. if (ret != 0)
  23022. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  23023. if (verify != 1)
  23024. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  23025. TEST_SLEEP();
  23026. }
  23027. #endif /* HAVE_ECC_VERIFY */
  23028. #endif /* HAVE_ECC_SIGN */
  23029. #endif /* !ECC_TIMING_RESISTANT || (ECC_TIMING_RESISTANT &&
  23030. * !WC_NO_RNG && !WOLFSSL_KCAPI_ECC) */
  23031. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  23032. !defined(WOLFSSL_ATECC508) && !defined(WOLFSSL_ATECC608A) && \
  23033. !defined(WOLFSSL_KCAPI_ECC)
  23034. x = ECC_KEY_EXPORT_BUF_SIZE;
  23035. ret = wc_ecc_export_private_only(userA, exportBuf, &x);
  23036. if (ret != 0)
  23037. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  23038. #elif defined(HAVE_ECC_KEY_EXPORT)
  23039. (void)exportBuf;
  23040. #endif /* HAVE_ECC_KEY_EXPORT */
  23041. done:
  23042. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23043. if (userA != NULL) {
  23044. wc_ecc_free(userA);
  23045. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23046. }
  23047. if (userB != NULL) {
  23048. wc_ecc_free(userB);
  23049. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23050. }
  23051. if (pubKey != NULL) {
  23052. wc_ecc_free(pubKey);
  23053. XFREE(pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23054. }
  23055. #else
  23056. wc_ecc_free(pubKey);
  23057. wc_ecc_free(userB);
  23058. wc_ecc_free(userA);
  23059. #endif
  23060. #if defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)
  23061. WC_FREE_VAR(sharedA, HEAP_HINT);
  23062. WC_FREE_VAR(sharedB, HEAP_HINT);
  23063. #endif
  23064. #ifdef HAVE_ECC_KEY_EXPORT
  23065. WC_FREE_VAR(exportBuf, HEAP_HINT);
  23066. #endif
  23067. #ifdef HAVE_ECC_SIGN
  23068. WC_FREE_VAR(sig, HEAP_HINT);
  23069. WC_FREE_VAR(digest, HEAP_HINT);
  23070. #endif
  23071. (void)keySize;
  23072. (void)curve_id;
  23073. (void)rng;
  23074. return ret;
  23075. }
  23076. #undef ECC_TEST_VERIFY_COUNT
  23077. #define ECC_TEST_VERIFY_COUNT 2
  23078. static wc_test_ret_t ecc_test_curve(WC_RNG* rng, int keySize, int curve_id)
  23079. {
  23080. wc_test_ret_t ret;
  23081. ret = ecc_test_curve_size(rng, keySize, ECC_TEST_VERIFY_COUNT, curve_id,
  23082. NULL);
  23083. if (ret < 0) {
  23084. if (ret == ECC_CURVE_OID_E) {
  23085. /* ignore error for curves not found */
  23086. /* some curve sizes are only available with:
  23087. HAVE_ECC_SECPR2, HAVE_ECC_SECPR3, HAVE_ECC_BRAINPOOL
  23088. and HAVE_ECC_KOBLITZ */
  23089. }
  23090. else {
  23091. printf("ecc_test_curve_size %d failed!\n", keySize);
  23092. return ret;
  23093. }
  23094. }
  23095. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  23096. #ifdef HAVE_ECC_VECTOR_TEST
  23097. ret = ecc_test_vector(keySize);
  23098. if (ret < 0) {
  23099. printf("ecc_test_vector %d failed!\n", keySize);
  23100. return ret;
  23101. }
  23102. #endif
  23103. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  23104. !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  23105. ret = ecc_test_key_decode(rng, keySize);
  23106. if (ret < 0) {
  23107. if (ret == ECC_CURVE_OID_E) {
  23108. /* ignore error for curves not found */
  23109. }
  23110. else {
  23111. printf("ecc_test_key_decode %d failed!\n", keySize);
  23112. return ret;
  23113. }
  23114. }
  23115. #endif
  23116. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  23117. ret = ecc_test_key_gen(rng, keySize);
  23118. if (ret < 0) {
  23119. if (ret == ECC_CURVE_OID_E) {
  23120. /* ignore error for curves not found */
  23121. }
  23122. else {
  23123. printf("ecc_test_key_gen %d failed!\n", keySize);
  23124. return ret;
  23125. }
  23126. }
  23127. #endif
  23128. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  23129. return 0;
  23130. }
  23131. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  23132. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  23133. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  23134. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23135. static wc_test_ret_t ecc_point_test(void)
  23136. {
  23137. wc_test_ret_t ret;
  23138. ecc_point* point;
  23139. ecc_point* point2;
  23140. #ifdef HAVE_COMP_KEY
  23141. ecc_point* point3;
  23142. ecc_point* point4;
  23143. #endif
  23144. word32 outLen;
  23145. byte out[65];
  23146. byte der[] = { 0x04, /* = Uncompressed */
  23147. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  23148. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  23149. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  23150. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  23151. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  23152. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  23153. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  23154. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  23155. #if defined(HAVE_COMP_KEY) && (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  23156. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  23157. byte derComp0[] = { 0x02, /* = Compressed, y even */
  23158. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  23159. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  23160. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  23161. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  23162. byte derComp1[] = { 0x03, /* = Compressed, y odd */
  23163. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  23164. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  23165. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  23166. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  23167. #endif
  23168. byte altDer[] = { 0x04, /* = Uncompressed */
  23169. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  23170. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  23171. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  23172. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  23173. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  23174. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  23175. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  23176. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
  23177. int curve_idx = wc_ecc_get_curve_idx(ECC_SECP256R1);
  23178. /* if curve P256 is not enabled then test should not fail */
  23179. if (curve_idx == ECC_CURVE_INVALID)
  23180. return 0;
  23181. outLen = sizeof(out);
  23182. point = wc_ecc_new_point();
  23183. if (point == NULL)
  23184. return WC_TEST_RET_ENC_ERRNO;
  23185. point2 = wc_ecc_new_point();
  23186. if (point2 == NULL) {
  23187. wc_ecc_del_point(point);
  23188. return WC_TEST_RET_ENC_NC;
  23189. }
  23190. #ifdef HAVE_COMP_KEY
  23191. point3 = wc_ecc_new_point();
  23192. if (point3 == NULL) {
  23193. wc_ecc_del_point(point2);
  23194. wc_ecc_del_point(point);
  23195. return WC_TEST_RET_ENC_NC;
  23196. }
  23197. point4 = wc_ecc_new_point();
  23198. if (point4 == NULL) {
  23199. wc_ecc_del_point(point3);
  23200. wc_ecc_del_point(point2);
  23201. wc_ecc_del_point(point);
  23202. return WC_TEST_RET_ENC_NC;
  23203. }
  23204. #endif
  23205. /* Parameter Validation testing. */
  23206. wc_ecc_del_point(NULL);
  23207. ret = wc_ecc_import_point_der(NULL, sizeof(der), curve_idx, point);
  23208. if (ret != ECC_BAD_ARG_E) {
  23209. ret = WC_TEST_RET_ENC_EC(ret);
  23210. goto done;
  23211. }
  23212. ret = wc_ecc_import_point_der(der, sizeof(der), ECC_CURVE_INVALID, point);
  23213. if (ret != ECC_BAD_ARG_E) {
  23214. ret = WC_TEST_RET_ENC_EC(ret);
  23215. goto done;
  23216. }
  23217. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, NULL);
  23218. if (ret != ECC_BAD_ARG_E) {
  23219. ret = WC_TEST_RET_ENC_EC(ret);
  23220. goto done;
  23221. }
  23222. ret = wc_ecc_export_point_der(-1, point, out, &outLen);
  23223. if (ret != ECC_BAD_ARG_E) {
  23224. ret = WC_TEST_RET_ENC_EC(ret);
  23225. goto done;
  23226. }
  23227. ret = wc_ecc_export_point_der(curve_idx, NULL, out, &outLen);
  23228. if (ret != ECC_BAD_ARG_E) {
  23229. ret = WC_TEST_RET_ENC_EC(ret);
  23230. goto done;
  23231. }
  23232. ret = wc_ecc_export_point_der(curve_idx, point, NULL, &outLen);
  23233. if (ret != LENGTH_ONLY_E || outLen != sizeof(out)) {
  23234. ret = WC_TEST_RET_ENC_EC(ret);
  23235. goto done;
  23236. }
  23237. ret = wc_ecc_export_point_der(curve_idx, point, out, NULL);
  23238. if (ret != ECC_BAD_ARG_E) {
  23239. ret = WC_TEST_RET_ENC_EC(ret);
  23240. goto done;
  23241. }
  23242. outLen = 0;
  23243. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  23244. if (ret != BUFFER_E) {
  23245. ret = WC_TEST_RET_ENC_EC(ret);
  23246. goto done;
  23247. }
  23248. ret = wc_ecc_copy_point(NULL, NULL);
  23249. if (ret != ECC_BAD_ARG_E) {
  23250. ret = WC_TEST_RET_ENC_EC(ret);
  23251. goto done;
  23252. }
  23253. ret = wc_ecc_copy_point(NULL, point2);
  23254. if (ret != ECC_BAD_ARG_E) {
  23255. ret = WC_TEST_RET_ENC_EC(ret);
  23256. goto done;
  23257. }
  23258. ret = wc_ecc_copy_point(point, NULL);
  23259. if (ret != ECC_BAD_ARG_E) {
  23260. ret = WC_TEST_RET_ENC_EC(ret);
  23261. goto done;
  23262. }
  23263. ret = wc_ecc_cmp_point(NULL, NULL);
  23264. if (ret != BAD_FUNC_ARG) {
  23265. ret = WC_TEST_RET_ENC_EC(ret);
  23266. goto done;
  23267. }
  23268. ret = wc_ecc_cmp_point(NULL, point2);
  23269. if (ret != BAD_FUNC_ARG) {
  23270. ret = WC_TEST_RET_ENC_EC(ret);
  23271. goto done;
  23272. }
  23273. ret = wc_ecc_cmp_point(point, NULL);
  23274. if (ret != BAD_FUNC_ARG) {
  23275. ret = WC_TEST_RET_ENC_EC(ret);
  23276. goto done;
  23277. }
  23278. /* Use API. */
  23279. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, point);
  23280. if (ret != 0) {
  23281. ret = WC_TEST_RET_ENC_EC(ret);
  23282. goto done;
  23283. }
  23284. outLen = sizeof(out);
  23285. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  23286. if (ret != 0) {
  23287. ret = WC_TEST_RET_ENC_EC(ret);
  23288. goto done;
  23289. }
  23290. if (outLen != sizeof(der)) {
  23291. ret = WC_TEST_RET_ENC_NC;
  23292. goto done;
  23293. }
  23294. if (XMEMCMP(out, der, outLen) != 0) {
  23295. ret = WC_TEST_RET_ENC_NC;
  23296. goto done;
  23297. }
  23298. ret = wc_ecc_copy_point(point2, point);
  23299. if (ret != MP_OKAY) {
  23300. ret = WC_TEST_RET_ENC_EC(ret);
  23301. goto done;
  23302. }
  23303. ret = wc_ecc_cmp_point(point2, point);
  23304. if (ret != MP_EQ) {
  23305. ret = WC_TEST_RET_ENC_EC(ret);
  23306. goto done;
  23307. }
  23308. ret = wc_ecc_import_point_der(altDer, sizeof(altDer), curve_idx, point2);
  23309. if (ret != 0) {
  23310. ret = WC_TEST_RET_ENC_EC(ret);
  23311. goto done;
  23312. }
  23313. ret = wc_ecc_cmp_point(point2, point);
  23314. if (ret != MP_GT) {
  23315. ret = WC_TEST_RET_ENC_EC(ret);
  23316. goto done;
  23317. }
  23318. #if defined(HAVE_COMP_KEY) && (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  23319. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  23320. ret = wc_ecc_import_point_der(derComp0, sizeof(derComp0)*2-1, curve_idx, point3);
  23321. if (ret != 0) {
  23322. ret = WC_TEST_RET_ENC_EC(ret);
  23323. goto done;
  23324. }
  23325. ret = wc_ecc_import_point_der_ex(derComp0, sizeof(derComp0), curve_idx, point4, 0);
  23326. if (ret != 0) {
  23327. ret = WC_TEST_RET_ENC_EC(ret);
  23328. goto done;
  23329. }
  23330. ret = wc_ecc_cmp_point(point3, point4);
  23331. if (ret != MP_EQ) {
  23332. ret = WC_TEST_RET_ENC_EC(ret);
  23333. goto done;
  23334. }
  23335. ret = wc_ecc_import_point_der(derComp1, sizeof(derComp1)*2-1, curve_idx, point3);
  23336. if (ret != 0) {
  23337. ret = WC_TEST_RET_ENC_EC(ret);
  23338. goto done;
  23339. }
  23340. ret = wc_ecc_import_point_der_ex(derComp1, sizeof(derComp1), curve_idx, point4, 0);
  23341. if (ret != 0) {
  23342. ret = WC_TEST_RET_ENC_EC(ret);
  23343. goto done;
  23344. }
  23345. ret = wc_ecc_cmp_point(point3, point4);
  23346. if (ret != MP_EQ) {
  23347. ret = WC_TEST_RET_ENC_EC(ret);
  23348. goto done;
  23349. }
  23350. #endif
  23351. done:
  23352. #ifdef HAVE_COMP_KEY
  23353. wc_ecc_del_point(point4);
  23354. wc_ecc_del_point(point3);
  23355. #endif
  23356. wc_ecc_del_point(point2);
  23357. wc_ecc_del_point(point);
  23358. return ret;
  23359. }
  23360. #endif /* !WOLFSSL_ATECC508A && HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  23361. #if !defined(NO_SIG_WRAPPER) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23362. static wc_test_ret_t ecc_sig_test(WC_RNG* rng, ecc_key* key)
  23363. {
  23364. wc_test_ret_t ret;
  23365. word32 sigSz;
  23366. int size;
  23367. byte out[ECC_MAX_SIG_SIZE];
  23368. byte in[] = TEST_STRING;
  23369. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  23370. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  23371. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  23372. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  23373. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  23374. };
  23375. word32 inLen = (word32)XSTRLEN((char*)in);
  23376. size = wc_ecc_sig_size(key);
  23377. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, sizeof(*key));
  23378. if (ret != size)
  23379. return WC_TEST_RET_ENC_NC;
  23380. sigSz = (word32)ret;
  23381. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  23382. inLen, out, &sigSz, key, sizeof(*key), rng);
  23383. if (ret != 0)
  23384. return WC_TEST_RET_ENC_EC(ret);
  23385. TEST_SLEEP();
  23386. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  23387. inLen, out, sigSz, key, sizeof(*key));
  23388. if (ret != 0)
  23389. return WC_TEST_RET_ENC_EC(ret);
  23390. TEST_SLEEP();
  23391. sigSz = (word32)sizeof(out);
  23392. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  23393. hash, (int)sizeof(hash), out, &sigSz, key, sizeof(*key), rng);
  23394. if (ret != 0)
  23395. return WC_TEST_RET_ENC_EC(ret);
  23396. TEST_SLEEP();
  23397. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  23398. hash, (int)sizeof(hash), out, sigSz, key, sizeof(*key));
  23399. if (ret != 0)
  23400. return WC_TEST_RET_ENC_EC(ret);
  23401. TEST_SLEEP();
  23402. return 0;
  23403. }
  23404. #endif
  23405. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  23406. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23407. static wc_test_ret_t ecc_exp_imp_test(ecc_key* key)
  23408. {
  23409. wc_test_ret_t ret;
  23410. int curve_id;
  23411. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23412. ecc_key *keyImp = (ecc_key *)XMALLOC(sizeof *keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23413. #else
  23414. ecc_key keyImp[1];
  23415. #endif
  23416. byte priv[32];
  23417. word32 privLen;
  23418. byte pub[65*2];
  23419. word32 pubLen, pubLenX, pubLenY;
  23420. const char qx[] = "7a4e287890a1a47ad3457e52f2f76a83"
  23421. "ce46cbc947616d0cbaa82323818a793d";
  23422. const char qy[] = "eec4084f5b29ebf29c44cce3b3059610"
  23423. "922f8b30ea6e8811742ac7238fe87308";
  23424. const char d[] = "8c14b793cb19137e323a6d2e2a870bca"
  23425. "2e7a493ec1153b3a95feb8a4873f8d08";
  23426. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23427. if (keyImp == NULL)
  23428. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  23429. #endif
  23430. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  23431. privLen = sizeof(priv);
  23432. ret = wc_ecc_export_private_only(key, priv, &privLen);
  23433. if (ret != 0) {
  23434. ret = WC_TEST_RET_ENC_EC(ret);
  23435. goto done;
  23436. }
  23437. pubLen = sizeof(pub);
  23438. ret = wc_ecc_export_point_der(key->idx, &key->pubkey, pub, &pubLen);
  23439. if (ret != 0) {
  23440. ret = WC_TEST_RET_ENC_EC(ret);
  23441. goto done;
  23442. }
  23443. ret = wc_ecc_import_private_key(priv, privLen, pub, pubLen, keyImp);
  23444. if (ret != 0) {
  23445. ret = WC_TEST_RET_ENC_EC(ret);
  23446. goto done;
  23447. }
  23448. wc_ecc_free(keyImp);
  23449. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  23450. ret = wc_ecc_import_raw_ex(keyImp, qx, qy, d, ECC_SECP256R1);
  23451. if (ret != 0) {
  23452. ret = WC_TEST_RET_ENC_EC(ret);
  23453. goto done;
  23454. }
  23455. wc_ecc_free(keyImp);
  23456. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  23457. curve_id = wc_ecc_get_curve_id(key->idx);
  23458. if (curve_id < 0) {
  23459. ret = WC_TEST_RET_ENC_EC(curve_id);
  23460. goto done;
  23461. }
  23462. /* test import private only */
  23463. ret = wc_ecc_import_private_key_ex(priv, privLen, NULL, 0, keyImp,
  23464. curve_id);
  23465. if (ret != 0) {
  23466. ret = WC_TEST_RET_ENC_EC(ret);
  23467. goto done;
  23468. }
  23469. wc_ecc_free(keyImp);
  23470. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  23471. /* test export public raw */
  23472. pubLenX = pubLenY = 32;
  23473. ret = wc_ecc_export_public_raw(key, pub, &pubLenX, &pub[32], &pubLenY);
  23474. if (ret != 0) {
  23475. ret = WC_TEST_RET_ENC_EC(ret);
  23476. goto done;
  23477. }
  23478. #ifndef HAVE_SELFTEST
  23479. /* test import of public */
  23480. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], NULL, ECC_SECP256R1);
  23481. if (ret != 0) {
  23482. ret = WC_TEST_RET_ENC_EC(ret);
  23483. goto done;
  23484. }
  23485. #endif
  23486. wc_ecc_free(keyImp);
  23487. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  23488. /* test export private and public raw */
  23489. pubLenX = pubLenY = privLen = 32;
  23490. ret = wc_ecc_export_private_raw(key, pub, &pubLenX, &pub[32], &pubLenY,
  23491. priv, &privLen);
  23492. if (ret != 0) {
  23493. ret = WC_TEST_RET_ENC_EC(ret);
  23494. goto done;
  23495. }
  23496. #ifndef HAVE_SELFTEST
  23497. /* test import of private and public */
  23498. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], priv, ECC_SECP256R1);
  23499. if (ret != 0) {
  23500. ret = WC_TEST_RET_ENC_EC(ret);
  23501. goto done;
  23502. }
  23503. #endif
  23504. done:
  23505. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23506. if (keyImp != NULL) {
  23507. wc_ecc_free(keyImp);
  23508. XFREE(keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23509. }
  23510. #else
  23511. wc_ecc_free(keyImp);
  23512. #endif
  23513. return ret;
  23514. }
  23515. #endif /* HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  23516. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT) && \
  23517. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23518. static wc_test_ret_t ecc_mulmod_test(ecc_key* key1)
  23519. {
  23520. wc_test_ret_t ret;
  23521. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23522. ecc_key *key2 = (ecc_key *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23523. ecc_key *key3 = (ecc_key *)XMALLOC(sizeof *key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23524. #else
  23525. ecc_key key2[1];
  23526. ecc_key key3[1];
  23527. #endif
  23528. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23529. if ((key2 == NULL) || (key3 == NULL))
  23530. ERROR_OUT(MEMORY_E, done);
  23531. #endif
  23532. wc_ecc_init_ex(key2, HEAP_HINT, devId);
  23533. wc_ecc_init_ex(key3, HEAP_HINT, devId);
  23534. /* TODO: Use test data, test with WOLFSSL_VALIDATE_ECC_IMPORT. */
  23535. /* Need base point (Gx,Gy) and parameter A - load them as the public and
  23536. * private key in key2.
  23537. */
  23538. ret = wc_ecc_import_raw_ex(key2, key1->dp->Gx, key1->dp->Gy, key1->dp->Af,
  23539. ECC_SECP256R1);
  23540. if (ret != 0)
  23541. goto done;
  23542. /* Need a point (Gx,Gy) and prime - load them as the public and private key
  23543. * in key3.
  23544. */
  23545. ret = wc_ecc_import_raw_ex(key3, key1->dp->Gx, key1->dp->Gy,
  23546. key1->dp->prime, ECC_SECP256R1);
  23547. if (ret != 0)
  23548. goto done;
  23549. ret = wc_ecc_mulmod(wc_ecc_key_get_priv(key1), &key2->pubkey, &key3->pubkey,
  23550. wc_ecc_key_get_priv(key2), wc_ecc_key_get_priv(key3),
  23551. 1);
  23552. if (ret != 0) {
  23553. ret = WC_TEST_RET_ENC_EC(ret);
  23554. goto done;
  23555. }
  23556. done:
  23557. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23558. if (key2 != NULL) {
  23559. wc_ecc_free(key2);
  23560. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23561. }
  23562. if (key3 != NULL) {
  23563. wc_ecc_free(key3);
  23564. XFREE(key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23565. }
  23566. #else
  23567. wc_ecc_free(key3);
  23568. wc_ecc_free(key2);
  23569. #endif
  23570. return ret;
  23571. }
  23572. #endif
  23573. #if defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG) && \
  23574. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23575. static wc_test_ret_t ecc_ssh_test(ecc_key* key, WC_RNG* rng)
  23576. {
  23577. wc_test_ret_t ret;
  23578. byte out[128];
  23579. word32 outLen = sizeof(out);
  23580. /* Parameter Validation testing. */
  23581. ret = wc_ecc_shared_secret_ssh(NULL, &key->pubkey, out, &outLen);
  23582. if (ret != BAD_FUNC_ARG)
  23583. return WC_TEST_RET_ENC_EC(ret);
  23584. ret = wc_ecc_shared_secret_ssh(key, NULL, out, &outLen);
  23585. if (ret != BAD_FUNC_ARG)
  23586. return WC_TEST_RET_ENC_EC(ret);
  23587. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, NULL, &outLen);
  23588. if (ret != BAD_FUNC_ARG)
  23589. return WC_TEST_RET_ENC_EC(ret);
  23590. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, NULL);
  23591. if (ret != BAD_FUNC_ARG)
  23592. return WC_TEST_RET_ENC_EC(ret);
  23593. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  23594. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  23595. !defined(HAVE_SELFTEST)
  23596. ret = wc_ecc_set_rng(key, rng);
  23597. if (ret != 0)
  23598. return WC_TEST_RET_ENC_EC(ret);
  23599. #else
  23600. (void)rng;
  23601. #endif
  23602. /* Use API. */
  23603. ret = 0;
  23604. do {
  23605. #if defined(WOLFSSL_ASYNC_CRYPT)
  23606. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  23607. #endif
  23608. if (ret == 0)
  23609. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, &outLen);
  23610. } while (ret == WC_PENDING_E);
  23611. if (ret != 0)
  23612. return WC_TEST_RET_ENC_EC(ret);
  23613. TEST_SLEEP();
  23614. return 0;
  23615. }
  23616. #endif /* HAVE_ECC_DHE && !WC_NO_RNG */
  23617. static wc_test_ret_t ecc_def_curve_test(WC_RNG *rng)
  23618. {
  23619. wc_test_ret_t ret;
  23620. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23621. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23622. #else
  23623. ecc_key key[1];
  23624. #endif
  23625. #if (defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)) || \
  23626. (defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  23627. word32 idx = 0;
  23628. #endif
  23629. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23630. if (key == NULL)
  23631. ERROR_OUT(MEMORY_E, done);
  23632. #endif
  23633. wc_ecc_init_ex(key, HEAP_HINT, devId);
  23634. /* Use API */
  23635. ret = wc_ecc_set_flags(NULL, 0);
  23636. if (ret != BAD_FUNC_ARG) {
  23637. ret = WC_TEST_RET_ENC_EC(ret);
  23638. goto done;
  23639. }
  23640. ret = wc_ecc_set_flags(key, 0);
  23641. if (ret != 0) {
  23642. ret = WC_TEST_RET_ENC_EC(ret);
  23643. goto done;
  23644. }
  23645. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  23646. #ifndef WC_NO_RNG
  23647. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  23648. #if defined(WOLFSSL_ASYNC_CRYPT)
  23649. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  23650. #endif
  23651. if (ret != 0) {
  23652. goto done;
  23653. }
  23654. #ifndef NO_SIG_WRAPPER
  23655. ret = ecc_sig_test(rng, key);
  23656. if (ret < 0)
  23657. goto done;
  23658. #endif
  23659. TEST_SLEEP();
  23660. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_CRYPTOCELL) && \
  23661. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23662. ret = ecc_ssh_test(key, rng);
  23663. if (ret < 0)
  23664. goto done;
  23665. #endif
  23666. wc_ecc_free(key);
  23667. #else
  23668. (void)rng;
  23669. #endif /* !WC_NO_RNG */
  23670. #if (defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)) || \
  23671. (defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  23672. /* Use test ECC key - ensure real private "d" exists */
  23673. #ifdef USE_CERT_BUFFERS_256
  23674. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, key,
  23675. sizeof_ecc_key_der_256);
  23676. #else
  23677. {
  23678. XFILE file = XFOPEN(eccKeyDerFile, "rb");
  23679. byte der[128];
  23680. word32 derSz;
  23681. if (!file) {
  23682. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  23683. }
  23684. derSz = (word32)XFREAD(der, 1, sizeof(der), file);
  23685. XFCLOSE(file);
  23686. if (derSz == 0)
  23687. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  23688. ret = wc_EccPrivateKeyDecode(der, &idx, key, derSz);
  23689. }
  23690. #endif
  23691. if (ret != 0) {
  23692. goto done;
  23693. }
  23694. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)
  23695. ret = ecc_exp_imp_test(key);
  23696. if (ret < 0)
  23697. goto done;
  23698. #endif
  23699. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT) && \
  23700. !defined(WOLFSSL_CRYPTOCELL)
  23701. ret = ecc_mulmod_test(key);
  23702. if (ret < 0)
  23703. goto done;
  23704. #endif
  23705. #endif
  23706. #else
  23707. (void)rng;
  23708. (void)idx;
  23709. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  23710. done:
  23711. wc_ecc_free(key);
  23712. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23713. if (key != NULL) {
  23714. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23715. }
  23716. #endif
  23717. return ret;
  23718. }
  23719. #endif /* !NO_ECC256 || HAVE_ALL_CURVES */
  23720. #if defined(WOLFSSL_CERT_EXT) && \
  23721. (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  23722. static wc_test_ret_t ecc_decode_test(void)
  23723. {
  23724. wc_test_ret_t ret;
  23725. word32 inSz;
  23726. word32 inOutIdx;
  23727. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23728. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23729. #else
  23730. ecc_key key[1];
  23731. #endif
  23732. /* SECP256R1 OID: 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07 */
  23733. /* This is ecc_clikeypub_der_256. */
  23734. WOLFSSL_SMALL_STACK_STATIC const byte good[] = {
  23735. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce,
  23736. 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
  23737. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xbf, 0xf4,
  23738. 0x0f, 0x44, 0x50, 0x9a, 0x3d, 0xce, 0x9b, 0xb7, 0xf0, 0xc5,
  23739. 0x4d, 0xf5, 0x70, 0x7b, 0xd4, 0xec, 0x24, 0x8e, 0x19, 0x80,
  23740. 0xec, 0x5a, 0x4c, 0xa2, 0x24, 0x03, 0x62, 0x2c, 0x9b, 0xda,
  23741. 0xef, 0xa2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xc6, 0x56,
  23742. 0x95, 0x06, 0xcc, 0x01, 0xa9, 0xbd, 0xf6, 0x75, 0x1a, 0x42,
  23743. 0xf7, 0xbd, 0xa9, 0xb2, 0x36, 0x22, 0x5f, 0xc7, 0x5d, 0x7f,
  23744. 0xb4 };
  23745. WOLFSSL_SMALL_STACK_STATIC const byte badNoObjId[] = { 0x30, 0x08, 0x30, 0x06, 0x03, 0x04,
  23746. 0x00, 0x04, 0x01, 0x01 };
  23747. WOLFSSL_SMALL_STACK_STATIC const byte badOneObjId[] = { 0x30, 0x0a, 0x30, 0x08, 0x06, 0x00,
  23748. 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  23749. WOLFSSL_SMALL_STACK_STATIC const byte badObjId1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x09,
  23750. 0x06, 0x00, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  23751. WOLFSSL_SMALL_STACK_STATIC const byte badObj2d1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x00,
  23752. 0x06, 0x07, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  23753. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitStr[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  23754. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  23755. 0x04, 0x04, 0x00, 0x04, 0x01, 0x01 };
  23756. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrLen[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  23757. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  23758. 0x03, 0x05, 0x00, 0x04, 0x01, 0x01 };
  23759. WOLFSSL_SMALL_STACK_STATIC const byte badNoBitStrZero[] = { 0x30, 0x13, 0x30, 0x0a, 0x06, 0x00,
  23760. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  23761. 0x03, 0x03, 0x04, 0x01, 0x01 };
  23762. WOLFSSL_SMALL_STACK_STATIC const byte badPoint[] = { 0x30, 0x12, 0x30, 0x09, 0x06, 0x00,
  23763. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  23764. 0x03, 0x03, 0x00, 0x04, 0x01 };
  23765. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23766. if (key == NULL)
  23767. ERROR_OUT(MEMORY_E, done);
  23768. #endif
  23769. XMEMSET(key, 0, sizeof *key);
  23770. wc_ecc_init_ex(key, HEAP_HINT, devId);
  23771. inSz = sizeof(good);
  23772. ret = wc_EccPublicKeyDecode(NULL, &inOutIdx, key, inSz);
  23773. if (ret != BAD_FUNC_ARG) {
  23774. ret = WC_TEST_RET_ENC_EC(ret);
  23775. goto done;
  23776. }
  23777. ret = wc_EccPublicKeyDecode(good, NULL, key, inSz);
  23778. if (ret != BAD_FUNC_ARG) {
  23779. ret = WC_TEST_RET_ENC_EC(ret);
  23780. goto done;
  23781. }
  23782. ret = wc_EccPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  23783. if (ret != BAD_FUNC_ARG) {
  23784. ret = WC_TEST_RET_ENC_EC(ret);
  23785. goto done;
  23786. }
  23787. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, 0);
  23788. if (ret != BAD_FUNC_ARG) {
  23789. ret = WC_TEST_RET_ENC_EC(ret);
  23790. goto done;
  23791. }
  23792. /* Change offset to produce bad input data. */
  23793. inOutIdx = 2;
  23794. inSz = sizeof(good) - inOutIdx;
  23795. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  23796. if (ret != ASN_PARSE_E) {
  23797. ret = WC_TEST_RET_ENC_EC(ret);
  23798. goto done;
  23799. }
  23800. inOutIdx = 4;
  23801. inSz = sizeof(good) - inOutIdx;
  23802. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  23803. if (ret != ASN_PARSE_E) {
  23804. ret = WC_TEST_RET_ENC_EC(ret);
  23805. goto done;
  23806. }
  23807. /* Bad data. */
  23808. inSz = sizeof(badNoObjId);
  23809. inOutIdx = 0;
  23810. ret = wc_EccPublicKeyDecode(badNoObjId, &inOutIdx, key, inSz);
  23811. if (ret != ASN_OBJECT_ID_E && ret != ASN_PARSE_E) {
  23812. ret = WC_TEST_RET_ENC_EC(ret);
  23813. goto done;
  23814. }
  23815. inSz = sizeof(badOneObjId);
  23816. inOutIdx = 0;
  23817. ret = wc_EccPublicKeyDecode(badOneObjId, &inOutIdx, key, inSz);
  23818. if (ret != ASN_OBJECT_ID_E && ret != ASN_PARSE_E) {
  23819. ret = WC_TEST_RET_ENC_EC(ret);
  23820. goto done;
  23821. }
  23822. inSz = sizeof(badObjId1Len);
  23823. inOutIdx = 0;
  23824. ret = wc_EccPublicKeyDecode(badObjId1Len, &inOutIdx, key, inSz);
  23825. if (ret != ASN_PARSE_E) {
  23826. ret = WC_TEST_RET_ENC_EC(ret);
  23827. goto done;
  23828. }
  23829. inSz = sizeof(badObj2d1Len);
  23830. inOutIdx = 0;
  23831. ret = wc_EccPublicKeyDecode(badObj2d1Len, &inOutIdx, key, inSz);
  23832. if (ret != ASN_PARSE_E) {
  23833. ret = WC_TEST_RET_ENC_EC(ret);
  23834. goto done;
  23835. }
  23836. inSz = sizeof(badNotBitStr);
  23837. inOutIdx = 0;
  23838. ret = wc_EccPublicKeyDecode(badNotBitStr, &inOutIdx, key, inSz);
  23839. if (ret != ASN_BITSTR_E && ret != ASN_PARSE_E) {
  23840. ret = WC_TEST_RET_ENC_EC(ret);
  23841. goto done;
  23842. }
  23843. inSz = sizeof(badBitStrLen);
  23844. inOutIdx = 0;
  23845. ret = wc_EccPublicKeyDecode(badBitStrLen, &inOutIdx, key, inSz);
  23846. if (ret != ASN_PARSE_E) {
  23847. ret = WC_TEST_RET_ENC_EC(ret);
  23848. goto done;
  23849. }
  23850. inSz = sizeof(badNoBitStrZero);
  23851. inOutIdx = 0;
  23852. ret = wc_EccPublicKeyDecode(badNoBitStrZero, &inOutIdx, key, inSz);
  23853. if (ret != ASN_EXPECT_0_E && ret != ASN_PARSE_E) {
  23854. ret = WC_TEST_RET_ENC_EC(ret);
  23855. goto done;
  23856. }
  23857. inSz = sizeof(badPoint);
  23858. inOutIdx = 0;
  23859. ret = wc_EccPublicKeyDecode(badPoint, &inOutIdx, key, inSz);
  23860. if (ret != ASN_ECC_KEY_E && ret != ASN_PARSE_E) {
  23861. ret = WC_TEST_RET_ENC_EC(ret);
  23862. goto done;
  23863. }
  23864. inSz = sizeof(good);
  23865. inOutIdx = 0;
  23866. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  23867. if (ret != 0) {
  23868. ret = WC_TEST_RET_ENC_EC(ret);
  23869. goto done;
  23870. }
  23871. done:
  23872. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23873. if (key != NULL) {
  23874. wc_ecc_free(key);
  23875. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23876. }
  23877. #else
  23878. wc_ecc_free(key);
  23879. #endif
  23880. return ret;
  23881. }
  23882. #endif /* WOLFSSL_CERT_EXT */
  23883. #ifdef WOLFSSL_CUSTOM_CURVES
  23884. static const byte eccKeyExplicitCurve[] = {
  23885. 0x30, 0x81, 0xf5, 0x30, 0x81, 0xae, 0x06, 0x07,
  23886. 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x30,
  23887. 0x81, 0xa2, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06,
  23888. 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01,
  23889. 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
  23890. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  23891. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  23892. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff,
  23893. 0xff, 0xfc, 0x2f, 0x30, 0x06, 0x04, 0x01, 0x00,
  23894. 0x04, 0x01, 0x07, 0x04, 0x41, 0x04, 0x79, 0xbe,
  23895. 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0,
  23896. 0x62, 0x95, 0xce, 0x87, 0x0b, 0x07, 0x02, 0x9b,
  23897. 0xfc, 0xdb, 0x2d, 0xce, 0x28, 0xd9, 0x59, 0xf2,
  23898. 0x81, 0x5b, 0x16, 0xf8, 0x17, 0x98, 0x48, 0x3a,
  23899. 0xda, 0x77, 0x26, 0xa3, 0xc4, 0x65, 0x5d, 0xa4,
  23900. 0xfb, 0xfc, 0x0e, 0x11, 0x08, 0xa8, 0xfd, 0x17,
  23901. 0xb4, 0x48, 0xa6, 0x85, 0x54, 0x19, 0x9c, 0x47,
  23902. 0xd0, 0x8f, 0xfb, 0x10, 0xd4, 0xb8, 0x02, 0x21,
  23903. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  23904. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  23905. 0xfe, 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0,
  23906. 0x3b, 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41,
  23907. 0x41, 0x02, 0x01, 0x01, 0x03, 0x42, 0x00, 0x04,
  23908. 0x3c, 0x4c, 0xc9, 0x5e, 0x2e, 0xa2, 0x3d, 0x49,
  23909. 0xcc, 0x5b, 0xff, 0x4f, 0xc9, 0x2e, 0x1d, 0x4a,
  23910. 0xc6, 0x21, 0xf6, 0xf3, 0xe6, 0x0b, 0x4f, 0xa9,
  23911. 0x9d, 0x74, 0x99, 0xdd, 0x97, 0xc7, 0x6e, 0xbe,
  23912. 0x14, 0x2b, 0x39, 0x9d, 0x63, 0xc7, 0x97, 0x0d,
  23913. 0x45, 0x25, 0x40, 0x30, 0x77, 0x05, 0x76, 0x88,
  23914. 0x38, 0x96, 0x29, 0x7d, 0x9c, 0xe1, 0x50, 0xbe,
  23915. 0xac, 0xf0, 0x1d, 0x86, 0xf4, 0x2f, 0x65, 0x0b
  23916. };
  23917. static wc_test_ret_t ecc_test_custom_curves(WC_RNG* rng)
  23918. {
  23919. wc_test_ret_t ret;
  23920. word32 inOutIdx;
  23921. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23922. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23923. #else
  23924. ecc_key key[1];
  23925. #endif
  23926. /* test use of custom curve - using BRAINPOOLP256R1 for test */
  23927. #if defined(HAVE_ECC_BRAINPOOL) && !defined(HAVE_INTEL_QA)
  23928. #ifndef WOLFSSL_ECC_CURVE_STATIC
  23929. WOLFSSL_SMALL_STACK_STATIC const ecc_oid_t ecc_oid_brainpoolp256r1[] = {
  23930. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07
  23931. };
  23932. #define ecc_oid_brainpoolp256r1_sz \
  23933. (sizeof(ecc_oid_brainpoolp256r1) / sizeof(ecc_oid_t))
  23934. #else
  23935. #define ecc_oid_brainpoolp256r1 { \
  23936. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07 \
  23937. }
  23938. #define ecc_oid_brainpoolp256r1_sz 9
  23939. #endif
  23940. #define ecc_oid_brainpoolp256r1_sum 104
  23941. WOLFSSL_SMALL_STACK_STATIC const ecc_set_type ecc_dp_brainpool256r1 = {
  23942. 32, /* size/bytes */
  23943. ECC_CURVE_CUSTOM, /* ID */
  23944. "BRAINPOOLP256R1", /* curve name */
  23945. "A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377", /* prime */
  23946. "7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9", /* A */
  23947. "26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6", /* B */
  23948. "A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7", /* order */
  23949. "8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262", /* Gx */
  23950. "547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997", /* Gy */
  23951. ecc_oid_brainpoolp256r1, /* oid/oidSz */
  23952. ecc_oid_brainpoolp256r1_sz,
  23953. ecc_oid_brainpoolp256r1_sum, /* oid sum */
  23954. 1, /* cofactor */
  23955. };
  23956. #endif /* HAVE_ECC_BRAINPOOL */
  23957. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23958. if (! key) {
  23959. ret = MEMORY_E;
  23960. goto done;
  23961. }
  23962. #endif
  23963. XMEMSET(key, 0, sizeof *key);
  23964. #if defined(HAVE_ECC_BRAINPOOL) && !defined(HAVE_INTEL_QA)
  23965. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, ECC_CURVE_DEF,
  23966. &ecc_dp_brainpool256r1);
  23967. if (ret != 0) {
  23968. printf("ECC test for custom curve failed!\n");
  23969. goto done;
  23970. }
  23971. #endif
  23972. #if defined(HAVE_ECC_BRAINPOOL) || defined(HAVE_ECC_KOBLITZ)
  23973. {
  23974. int curve_id;
  23975. #ifdef HAVE_ECC_BRAINPOOL
  23976. curve_id = ECC_BRAINPOOLP256R1;
  23977. #else
  23978. curve_id = ECC_SECP256K1;
  23979. #endif
  23980. /* Test and demonstrate use of non-SECP curve */
  23981. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, curve_id, NULL);
  23982. if (ret < 0) {
  23983. printf("ECC test for curve_id %d failed!\n", curve_id);
  23984. goto done;
  23985. }
  23986. }
  23987. #endif
  23988. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  23989. if (ret != 0) {
  23990. ret = WC_TEST_RET_ENC_EC(ret);
  23991. goto done;
  23992. }
  23993. inOutIdx = 0;
  23994. ret = wc_EccPublicKeyDecode(eccKeyExplicitCurve, &inOutIdx, key,
  23995. sizeof(eccKeyExplicitCurve));
  23996. if (ret != 0)
  23997. ret = WC_TEST_RET_ENC_EC(ret);
  23998. done:
  23999. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24000. if (key) {
  24001. wc_ecc_free(key);
  24002. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24003. }
  24004. #else
  24005. wc_ecc_free(key);
  24006. #endif
  24007. (void)rng;
  24008. return ret;
  24009. }
  24010. #endif /* WOLFSSL_CUSTOM_CURVES */
  24011. #ifdef WOLFSSL_SM2
  24012. #ifdef HAVE_ECC_VERIFY
  24013. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_CUSTOM_CURVES)
  24014. #ifdef WOLFSSL_SM2
  24015. #ifdef HAVE_OID_ENCODING
  24016. #define CODED_SM2P256V1 {1,2,156,10197,1,301}
  24017. #define CODED_SM2P256V1_SZ 6
  24018. #else
  24019. #define CODED_SM2P256V1 {0x06,0x08,0x2A,0x81,0x1C,0xCF,0x55,0x01,0x82,0x2D}
  24020. #define CODED_SM2P256V1_SZ 10
  24021. #endif
  24022. #ifndef WOLFSSL_ECC_CURVE_STATIC
  24023. static const ecc_oid_t ecc_oid_sm2p256v1[] = CODED_SM2P256V1;
  24024. #else
  24025. #define ecc_oid_sm2p256v1 CODED_SM2P256V1
  24026. #endif
  24027. #define ecc_oid_sm2p256v1_sz CODED_SM2P256V1_SZ
  24028. #endif /* WOLFSSL_SM2 */
  24029. #define ECC_SM2P256V1_TEST 102
  24030. static int test_sm2_verify_caseA2(void)
  24031. {
  24032. ecc_key key;
  24033. int ret, res;
  24034. mp_int r,s;
  24035. /* test key values */
  24036. const char qx[] = "0AE4C7798AA0F119471BEE11825BE46202BB79E2A5844495E97C04FF4DF2548A";
  24037. const char qy[] = "7C0240F88F1CD4E16352A73C17B7F16F07353E53A176D684A9FE0C6BB798E857";
  24038. const char d[] = "128B2FA8BD433C6C068C8D803DFF79792A519A55171B1B650C23661D15897263";
  24039. const ecc_set_type ecc_sm2_A2 = {
  24040. 32, /* size/bytes */
  24041. ECC_SM2P256V1_TEST, /* ID */
  24042. "SM2P256V1_TEST", /* curve name */
  24043. /* from test case A.2 in draft-shen-sm2-ecdsa-02 */
  24044. "8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3", /* prime */
  24045. "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498", /* A */
  24046. "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A", /* B */
  24047. "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7", /* order n */
  24048. "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D", /* Gx */
  24049. "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2", /* Gy */
  24050. ecc_oid_sm2p256v1, /* oid/oidSz */
  24051. ecc_oid_sm2p256v1_sz,
  24052. ECC_SM2P256V1_OID, /* oid sum */
  24053. 1, /* cofactor */
  24054. };
  24055. /* use canned hash value hash = H(ZA||M) */
  24056. const byte hash[] = {
  24057. 0xB5,0x24,0xF5,0x52,0xCD,0x82,0xB8,0xB0,
  24058. 0x28,0x47,0x6E,0x00,0x5C,0x37,0x7F,0xB1,
  24059. 0x9A,0x87,0xE6,0xFC,0x68,0x2D,0x48,0xBB,
  24060. 0x5D,0x42,0xE3,0xD9,0xB9,0xEF,0xFE,0x76
  24061. };
  24062. /* canned r and s */
  24063. const byte rCan[] = {
  24064. 0x40,0xF1,0xEC,0x59,0xF7,0x93,0xD9,0xF4,
  24065. 0x9E,0x09,0xDC,0xEF,0x49,0x13,0x0D,0x41,
  24066. 0x94,0xF7,0x9F,0xB1,0xEE,0xD2,0xCA,0xA5,
  24067. 0x5B,0xAC,0xDB,0x49,0xC4,0xE7,0x55,0xD1
  24068. };
  24069. const byte sCan[] = {
  24070. 0x6F,0xC6,0xDA,0xC3,0x2C,0x5D,0x5C,0xF1,
  24071. 0x0C,0x77,0xDF,0xB2,0x0F,0x7C,0x2E,0xB6,
  24072. 0x67,0xA4,0x57,0x87,0x2F,0xB0,0x9E,0xC5,
  24073. 0x63,0x27,0xA6,0x7E,0xC7,0xDE,0xEB,0xE7
  24074. };
  24075. mp_init(&r);
  24076. mp_init(&s);
  24077. ret = wc_ecc_init_ex(&key, HEAP_HINT, devId);
  24078. if (ret != 0)
  24079. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24080. ret = wc_ecc_set_custom_curve(&key, &ecc_sm2_A2);
  24081. if (ret != 0)
  24082. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24083. ret = wc_ecc_import_raw_ex(&key, qx, qy, d, ECC_SM2P256V1_TEST);
  24084. if (ret != 0)
  24085. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24086. mp_read_unsigned_bin(&r, rCan, sizeof(rCan));
  24087. mp_read_unsigned_bin(&s, sCan, sizeof(sCan));
  24088. ret = wc_ecc_sm2_verify_hash_ex(&r, &s, hash, sizeof(hash), &res, &key);
  24089. if (ret != 0)
  24090. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24091. if (res != 1)
  24092. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24093. done:
  24094. mp_free(&r);
  24095. mp_free(&s);
  24096. wc_ecc_free(&key);
  24097. return ret;
  24098. }
  24099. #endif /* WOLFSSL_PUBLIC_MP && WOLFSSL_CUSTOM_CURVES */
  24100. static int test_sm2_verify_case(void)
  24101. {
  24102. ecc_key key;
  24103. int ret, res;
  24104. /* test key values */
  24105. const char qx[] = "637F1B135036C933DC3F7A8EBB1B7B2FD1DFBD268D4F894B5AD47DBDBECD558F";
  24106. const char qy[] = "E88101D08048E36CCBF61CA38DDF7ABA542B4486E99E49F3A7470A857A096433";
  24107. /* use canned hash value hash = H(ZA||M) */
  24108. const byte hash[] = {
  24109. 0x3B,0xFA,0x5F,0xFB,0xC4,0x27,0x8C,0x9D,
  24110. 0x02,0x3A,0x19,0xCB,0x1E,0xAA,0xD2,0xF1,
  24111. 0x50,0x69,0x5B,0x20
  24112. };
  24113. const byte sig[] = {
  24114. 0x30,0x45,0x02,0x21,0x00,0xD2,0xFC,0xA3,
  24115. 0x88,0xE3,0xDF,0xA3,0x00,0x73,0x9B,0x3C,
  24116. 0x2A,0x0D,0xAD,0x44,0xA2,0xFC,0x62,0xD5,
  24117. 0x6B,0x84,0x54,0xD8,0x40,0x22,0x62,0x3D,
  24118. 0x5C,0xA6,0x61,0x9B,0xE7,0x02,0x20,0x1D,
  24119. 0xB5,0xB5,0xD9,0xD8,0xF1,0x20,0xDD,0x97,
  24120. 0x92,0xBF,0x7E,0x9B,0x3F,0xE6,0x3C,0x4B,
  24121. 0x03,0xD8,0x80,0xBD,0xB7,0x27,0x7E,0x6A,
  24122. 0x84,0x23,0xDE,0x61,0x7C,0x8D,0xDC
  24123. };
  24124. const byte badSig[] = {
  24125. 0x30,0x45,0x02,0x21,0x00,0xD2,0xFC,0xA3,
  24126. 0x88,0xE3,0xDF,0xA3,0x00,0x73,0x9B,0x3C,
  24127. 0x2A,0x0D,0xAD,0x44,0xA2,0xFC,0x62,0xD5,
  24128. 0x6B,0x84,0x54,0xD8,0x40,0x22,0x62,0x3D,
  24129. 0x5C,0xA6,0x61,0x9B,0xE7,0x02,0x20,0x1D,
  24130. 0xB5,0xB5,0xE9,0xD8,0xF1,0x20,0xDD,0x97,
  24131. 0x92,0xBF,0x7E,0x9B,0x3F,0xE6,0x3C,0x4B,
  24132. 0x03,0xD8,0x80,0xBD,0xB7,0x27,0x7E,0x6A,
  24133. 0x84,0x23,0xDE,0x61,0x7C,0x8D,0xDC
  24134. };
  24135. ret = wc_ecc_init_ex(&key, HEAP_HINT, devId);
  24136. if (ret != 0)
  24137. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24138. ret = wc_ecc_import_raw(&key, qx, qy, NULL, "SM2P256V1");
  24139. if (ret != 0)
  24140. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24141. ret = wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash), &res,
  24142. &key);
  24143. if (ret != 0)
  24144. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24145. if (res != 1)
  24146. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24147. /* now test a case that should fail */
  24148. ret = wc_ecc_sm2_verify_hash(badSig, sizeof(badSig), hash, sizeof(hash),
  24149. &res, &key);
  24150. if (ret != 0)
  24151. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24152. if (res == 1)
  24153. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24154. done:
  24155. wc_ecc_free(&key);
  24156. return ret;
  24157. }
  24158. static int ecc_sm2_test_curve(WC_RNG* rng, int testVerifyCount)
  24159. {
  24160. const ecc_set_type* dp = wc_ecc_get_curve_params(
  24161. wc_ecc_get_curve_idx(ECC_SM2P256V1));
  24162. int keySize = 32;
  24163. int curve_id = ECC_SM2P256V1;
  24164. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG)
  24165. WC_DECLARE_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  24166. WC_DECLARE_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  24167. #endif
  24168. #ifdef HAVE_ECC_KEY_EXPORT
  24169. #define ECC_KEY_EXPORT_BUF_SIZE (MAX_ECC_BYTES * 2 + 32)
  24170. WC_DECLARE_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  24171. #endif
  24172. word32 x = 0;
  24173. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG)
  24174. word32 y;
  24175. #endif
  24176. #ifdef HAVE_ECC_SIGN
  24177. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  24178. WC_DECLARE_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  24179. int i;
  24180. #ifdef HAVE_ECC_VERIFY
  24181. int verify;
  24182. #endif /* HAVE_ECC_VERIFY */
  24183. #endif /* HAVE_ECC_SIGN */
  24184. int ret;
  24185. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24186. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24187. ecc_key *userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24188. ecc_key *pubKey = (ecc_key *)XMALLOC(sizeof *pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24189. #else
  24190. ecc_key userA[1];
  24191. ecc_key userB[1];
  24192. ecc_key pubKey[1];
  24193. #endif
  24194. #ifndef WC_NO_RNG
  24195. int curveSize;
  24196. #endif
  24197. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  24198. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG)
  24199. if (sharedA == NULL || sharedB == NULL)
  24200. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24201. #endif
  24202. #ifdef HAVE_ECC_KEY_EXPORT
  24203. if (exportBuf == NULL)
  24204. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24205. #endif
  24206. #ifdef HAVE_ECC_SIGN
  24207. if (sig == NULL || digest == NULL)
  24208. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24209. #endif
  24210. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  24211. (void)testVerifyCount;
  24212. (void)dp;
  24213. (void)x;
  24214. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24215. if ((userA == NULL) ||
  24216. (userB == NULL) ||
  24217. (pubKey == NULL))
  24218. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24219. #endif
  24220. XMEMSET(userA, 0, sizeof *userA);
  24221. XMEMSET(userB, 0, sizeof *userB);
  24222. XMEMSET(pubKey, 0, sizeof *pubKey);
  24223. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  24224. if (ret != 0)
  24225. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24226. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  24227. if (ret != 0)
  24228. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24229. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  24230. if (ret != 0)
  24231. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24232. #ifndef WC_NO_RNG
  24233. ret = wc_ecc_sm2_make_key(rng, userA, WC_ECC_FLAG_NONE);
  24234. if (ret == ECC_CURVE_OID_E)
  24235. goto done; /* catch case, where curve is not supported */
  24236. if (ret != 0)
  24237. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24238. TEST_SLEEP();
  24239. if (wc_ecc_get_curve_idx(curve_id) != -1) {
  24240. curveSize = wc_ecc_get_curve_size_from_id(userA->dp->id);
  24241. if (curveSize != userA->dp->size) {
  24242. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24243. }
  24244. }
  24245. ret = wc_ecc_check_key(userA);
  24246. if (ret != 0)
  24247. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24248. TEST_SLEEP();
  24249. ret = wc_ecc_sm2_make_key(rng, userB, WC_ECC_FLAG_NONE);
  24250. if (ret != 0)
  24251. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24252. /* only perform the below tests if the key size matches */
  24253. if (dp == NULL && keySize > 0 && wc_ecc_size(userA) != keySize)
  24254. if (ret != 0) {
  24255. ret = ECC_CURVE_OID_E;
  24256. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24257. }
  24258. #ifdef HAVE_ECC_DHE
  24259. #if defined(ECC_TIMING_RESISTANT)
  24260. ret = wc_ecc_set_rng(userA, rng);
  24261. if (ret != 0)
  24262. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24263. ret = wc_ecc_set_rng(userB, rng);
  24264. if (ret != 0)
  24265. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24266. #endif
  24267. x = ECC_SHARED_SIZE;
  24268. ret = wc_ecc_sm2_shared_secret(userA, userB, sharedA, &x);
  24269. if (ret != 0)
  24270. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24271. y = ECC_SHARED_SIZE;
  24272. ret = wc_ecc_sm2_shared_secret(userB, userA, sharedB, &y);
  24273. if (ret != 0)
  24274. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24275. if (y != x)
  24276. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24277. if (XMEMCMP(sharedA, sharedB, x))
  24278. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24279. #endif /* HAVE_ECC_DHE */
  24280. #ifdef HAVE_ECC_KEY_EXPORT
  24281. x = ECC_KEY_EXPORT_BUF_SIZE;
  24282. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 0);
  24283. if (ret != 0)
  24284. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24285. #ifdef HAVE_ECC_KEY_IMPORT
  24286. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  24287. if (ret != 0)
  24288. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24289. #ifdef HAVE_ECC_DHE
  24290. y = ECC_SHARED_SIZE;
  24291. ret = wc_ecc_sm2_shared_secret(userB, pubKey, sharedB, &y);
  24292. if (ret != 0)
  24293. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24294. if (XMEMCMP(sharedA, sharedB, y))
  24295. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24296. #endif /* HAVE_ECC_DHE */
  24297. #ifdef HAVE_COMP_KEY
  24298. /* try compressed export / import too */
  24299. x = ECC_KEY_EXPORT_BUF_SIZE;
  24300. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 1);
  24301. if (ret != 0)
  24302. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24303. wc_ecc_free(pubKey);
  24304. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  24305. if (ret != 0)
  24306. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24307. #endif
  24308. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  24309. if (ret != 0)
  24310. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24311. #ifdef HAVE_ECC_DHE
  24312. y = ECC_SHARED_SIZE;
  24313. ret = wc_ecc_sm2_shared_secret(userB, pubKey, sharedB, &y);
  24314. if (ret != 0)
  24315. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24316. if (XMEMCMP(sharedA, sharedB, y))
  24317. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24318. #endif /* HAVE_ECC_DHE */
  24319. #endif /* HAVE_ECC_KEY_IMPORT */
  24320. #endif /* HAVE_ECC_KEY_EXPORT */
  24321. #endif /* !WC_NO_RNG */
  24322. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  24323. !defined(WC_NO_RNG))
  24324. #ifdef HAVE_ECC_SIGN
  24325. /* ECC w/out Shamir has issue with all 0 digest */
  24326. /* WC_BIGINT doesn't have 0 len well on hardware */
  24327. /* Cryptocell has issues with all 0 digest */
  24328. #if defined(ECC_SHAMIR)
  24329. /* test DSA sign hash with zeros */
  24330. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  24331. digest[i] = 0;
  24332. }
  24333. x = ECC_SIG_SIZE;
  24334. ret = wc_ecc_sm2_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng, userA);
  24335. if (ret != 0)
  24336. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24337. #ifdef HAVE_ECC_VERIFY
  24338. for (i = 0; i < testVerifyCount; i++) {
  24339. verify = 0;
  24340. ret = wc_ecc_sm2_verify_hash(sig, x, digest, ECC_DIGEST_SIZE, &verify,
  24341. userA);
  24342. if (ret != 0)
  24343. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24344. if (verify != 1)
  24345. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24346. }
  24347. #endif /* HAVE_ECC_VERIFY */
  24348. #endif /* ECC_SHAMIR */
  24349. /* test DSA sign hash with sequence (0,1,2,3,4,...) */
  24350. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  24351. digest[i] = (byte)i;
  24352. }
  24353. x = ECC_SIG_SIZE;
  24354. ret = wc_ecc_sm2_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng, userA);
  24355. if (ret != 0)
  24356. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24357. #ifdef HAVE_ECC_VERIFY
  24358. for (i = 0; i < testVerifyCount; i++) {
  24359. verify = 0;
  24360. ret = wc_ecc_sm2_verify_hash(sig, x, digest, ECC_DIGEST_SIZE, &verify,
  24361. userA);
  24362. if (ret != 0)
  24363. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24364. if (verify != 1)
  24365. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24366. }
  24367. #endif /* HAVE_ECC_VERIFY */
  24368. #endif /* HAVE_ECC_SIGN */
  24369. #endif /* !ECC_TIMING_RESISTANT || (ECC_TIMING_RESISTANT && !WC_NO_RNG) */
  24370. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  24371. x = ECC_KEY_EXPORT_BUF_SIZE;
  24372. ret = wc_ecc_export_private_only(userA, exportBuf, &x);
  24373. if (ret != 0)
  24374. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24375. #elif defined(HAVE_ECC_KEY_EXPORT)
  24376. (void)exportBuf;
  24377. #endif /* HAVE_ECC_KEY_EXPORT */
  24378. done:
  24379. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24380. if (userA != NULL) {
  24381. wc_ecc_free(userA);
  24382. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24383. }
  24384. if (userB != NULL) {
  24385. wc_ecc_free(userB);
  24386. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24387. }
  24388. if (pubKey != NULL) {
  24389. wc_ecc_free(pubKey);
  24390. XFREE(pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24391. }
  24392. #else
  24393. wc_ecc_free(pubKey);
  24394. wc_ecc_free(userB);
  24395. wc_ecc_free(userA);
  24396. #endif
  24397. #if defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)
  24398. WC_FREE_VAR(sharedA, HEAP_HINT);
  24399. WC_FREE_VAR(sharedB, HEAP_HINT);
  24400. #endif
  24401. #ifdef HAVE_ECC_KEY_EXPORT
  24402. WC_FREE_VAR(exportBuf, HEAP_HINT);
  24403. #endif
  24404. #ifdef HAVE_ECC_SIGN
  24405. WC_FREE_VAR(sig, HEAP_HINT);
  24406. WC_FREE_VAR(digest, HEAP_HINT);
  24407. #endif
  24408. (void)keySize;
  24409. (void)curve_id;
  24410. (void)rng;
  24411. return ret;
  24412. }
  24413. #endif /* HAVE_ECC_VERIFY */
  24414. static int test_sm2_create_digest(void)
  24415. {
  24416. const byte msg[] = "message to sign";
  24417. const byte id[] = "0123456789";
  24418. const byte badId[] = "0123556789";
  24419. byte expected[] = {
  24420. 0xdd, 0x4d, 0x65, 0x49, 0xa3, 0x64, 0x76, 0xc0,
  24421. 0x73, 0x05, 0xdc, 0x05, 0x16, 0xb5, 0xee, 0x9f,
  24422. 0x82, 0xf9, 0xe9, 0x7d, 0x01, 0x1a, 0xdc, 0x88,
  24423. 0x5a, 0x59, 0x9c, 0x44, 0xcc, 0x47, 0xa4, 0x78
  24424. };
  24425. ecc_key key;
  24426. int ret;
  24427. /* test key values */
  24428. const char qx[] =
  24429. "af178b7b8740cc9d5b493fbd22049c12621bc27dcc5802e75ff4d045a4158baf";
  24430. const char qy[] =
  24431. "89933faf7a4798f48c5b9b4cd3a7693d54c9e05449946eb489c0dd50a5294805";
  24432. const char d[] =
  24433. "b3e66c2dbfb50c6ff6830c1fac4b51293a2562f9e667052b03df2d4b43c1f34a";
  24434. byte digest[WC_SHA256_DIGEST_SIZE];
  24435. ret = wc_ecc_init_ex(&key, HEAP_HINT, devId);
  24436. if (ret != 0)
  24437. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24438. ret = wc_ecc_import_raw(&key, qx, qy, d, "SM2P256V1");
  24439. if (ret != 0)
  24440. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24441. ret = wc_ecc_sm2_create_digest(id, (int)XSTRLEN((const char*)id),
  24442. msg, (int)XSTRLEN((const char*)msg), WC_HASH_TYPE_SHA256, digest,
  24443. WC_SHA256_DIGEST_SIZE, &key);
  24444. if (ret != 0)
  24445. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  24446. if (XMEMCMP(digest, expected, WC_SHA256_DIGEST_SIZE) != 0)
  24447. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24448. ret = wc_ecc_sm2_create_digest(badId, (int)XSTRLEN((const char*)badId),
  24449. msg, (int)XSTRLEN((const char*)msg), WC_HASH_TYPE_SHA256, digest,
  24450. WC_SHA256_DIGEST_SIZE, &key);
  24451. if (ret != 0)
  24452. goto done;
  24453. /* should be different than the previous ID used */
  24454. if (XMEMCMP(digest, expected, WC_SHA256_DIGEST_SIZE) == 0)
  24455. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  24456. done:
  24457. wc_ecc_free(&key);
  24458. return ret;
  24459. }
  24460. static int test_sm2_verify(void)
  24461. {
  24462. int ret = 0;
  24463. #ifdef HAVE_ECC_VERIFY
  24464. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_CUSTOM_CURVES)
  24465. ret = test_sm2_verify_caseA2();
  24466. if (ret != 0)
  24467. return ret;
  24468. #endif
  24469. ret = test_sm2_verify_case();
  24470. if (ret != 0)
  24471. return ret;
  24472. #endif /* HAVE_ECC_VERIFY */
  24473. ret = test_sm2_create_digest();
  24474. return ret;
  24475. }
  24476. #endif /* WOLFSSL_SM2 */
  24477. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ECC_SECP) && !defined(NO_ASN_TIME)
  24478. /* Make Cert / Sign example for ECC cert and ECC CA */
  24479. static wc_test_ret_t ecc_test_cert_gen(WC_RNG* rng)
  24480. {
  24481. wc_test_ret_t ret;
  24482. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24483. Cert *myCert = (Cert *)XMALLOC(sizeof *myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24484. #ifdef WOLFSSL_TEST_CERT
  24485. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24486. #endif
  24487. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24488. ecc_key *certPubKey = (ecc_key *)XMALLOC(sizeof *certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24489. #else
  24490. Cert myCert[1];
  24491. #ifdef WOLFSSL_TEST_CERT
  24492. DecodedCert decode[1];
  24493. #endif
  24494. ecc_key caEccKey[1];
  24495. ecc_key certPubKey[1];
  24496. #endif
  24497. int certSz;
  24498. size_t bytes;
  24499. word32 idx = 0;
  24500. #ifndef USE_CERT_BUFFERS_256
  24501. XFILE file;
  24502. #endif
  24503. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24504. byte* der = NULL;
  24505. #else
  24506. byte der[FOURK_BUF];
  24507. #endif
  24508. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24509. if ((myCert == NULL)
  24510. #ifdef WOLFSSL_TEST_CERT
  24511. || (decode == NULL)
  24512. #endif
  24513. || (caEccKey == NULL) || (certPubKey == NULL))
  24514. ERROR_OUT(MEMORY_E, exit);
  24515. #endif
  24516. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  24517. XMEMSET(certPubKey, 0, sizeof *certPubKey);
  24518. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24519. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24520. if (der == NULL) {
  24521. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  24522. }
  24523. #endif
  24524. /* Get cert private key */
  24525. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  24526. /* Get Cert Key 384 */
  24527. #ifdef USE_CERT_BUFFERS_256
  24528. XMEMCPY(der, ca_ecc_key_der_384, sizeof_ca_ecc_key_der_384);
  24529. bytes = sizeof_ca_ecc_key_der_384;
  24530. #else
  24531. file = XFOPEN(eccCaKey384File, "rb");
  24532. if (!file) {
  24533. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  24534. }
  24535. bytes = XFREAD(der, 1, FOURK_BUF, file);
  24536. XFCLOSE(file);
  24537. if (bytes == 0)
  24538. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  24539. (void)eccCaKeyFile;
  24540. #endif /* USE_CERT_BUFFERS_256 */
  24541. #else
  24542. #ifdef USE_CERT_BUFFERS_256
  24543. XMEMCPY(der, ca_ecc_key_der_256, sizeof_ca_ecc_key_der_256);
  24544. bytes = sizeof_ca_ecc_key_der_256;
  24545. #else
  24546. file = XFOPEN(eccCaKeyFile, "rb");
  24547. if (!file) {
  24548. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  24549. }
  24550. bytes = XFREAD(der, 1, FOURK_BUF, file);
  24551. XFCLOSE(file);
  24552. if (bytes == 0)
  24553. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  24554. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  24555. (void)eccCaKey384File;
  24556. #endif
  24557. #endif /* USE_CERT_BUFFERS_256 */
  24558. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  24559. /* Get CA Key */
  24560. ret = wc_ecc_init_ex(caEccKey, HEAP_HINT, devId);
  24561. if (ret != 0)
  24562. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  24563. ret = wc_EccPrivateKeyDecode(der, &idx, caEccKey, (word32)bytes);
  24564. if (ret != 0)
  24565. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  24566. /* Make a public key */
  24567. ret = wc_ecc_init_ex(certPubKey, HEAP_HINT, devId);
  24568. if (ret != 0)
  24569. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  24570. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, certPubKey);
  24571. #if defined(WOLFSSL_ASYNC_CRYPT)
  24572. ret = wc_AsyncWait(ret, &certPubKey->asyncDev, WC_ASYNC_FLAG_NONE);
  24573. #endif
  24574. if (ret != 0)
  24575. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  24576. TEST_SLEEP();
  24577. /* Setup Certificate */
  24578. ret = wc_InitCert_ex(myCert, HEAP_HINT, devId);
  24579. if (ret != 0)
  24580. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  24581. #ifndef NO_SHA256
  24582. myCert->sigType = CTC_SHA256wECDSA;
  24583. #else
  24584. myCert->sigType = CTC_SHAwECDSA;
  24585. #endif
  24586. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  24587. #ifdef WOLFSSL_CERT_EXT
  24588. /* add Policies */
  24589. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  24590. CTC_MAX_CERTPOL_SZ);
  24591. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  24592. CTC_MAX_CERTPOL_SZ);
  24593. myCert->certPoliciesNb = 2;
  24594. /* add SKID from the Public Key */
  24595. ret = wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, certPubKey);
  24596. if (ret != 0)
  24597. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  24598. /* add AKID from the Public Key */
  24599. ret = wc_SetAuthKeyIdFromPublicKey(myCert, NULL, caEccKey);
  24600. if (ret != 0)
  24601. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  24602. /* add Key Usage */
  24603. ret = wc_SetKeyUsage(myCert, certKeyUsage);
  24604. if (ret != 0)
  24605. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  24606. #endif /* WOLFSSL_CERT_EXT */
  24607. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  24608. #if defined(USE_CERT_BUFFERS_256)
  24609. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_384,
  24610. sizeof_ca_ecc_cert_der_384);
  24611. #else
  24612. ret = wc_SetIssuer(myCert, eccCaCert384File);
  24613. (void)eccCaCertFile;
  24614. #endif
  24615. #else
  24616. #if defined(USE_CERT_BUFFERS_256)
  24617. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_256,
  24618. sizeof_ca_ecc_cert_der_256);
  24619. #else
  24620. ret = wc_SetIssuer(myCert, eccCaCertFile);
  24621. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  24622. (void)eccCaCert384File;
  24623. #endif
  24624. #endif
  24625. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  24626. if (ret < 0)
  24627. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  24628. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, certPubKey, rng);
  24629. if (certSz < 0) {
  24630. ERROR_OUT(WC_TEST_RET_ENC_EC(certSz), exit);
  24631. }
  24632. ret = 0;
  24633. do {
  24634. #if defined(WOLFSSL_ASYNC_CRYPT)
  24635. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  24636. #endif
  24637. if (ret >= 0) {
  24638. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  24639. FOURK_BUF, NULL, caEccKey, rng);
  24640. }
  24641. } while (ret == WC_PENDING_E);
  24642. if (ret < 0)
  24643. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  24644. certSz = (word32)ret;
  24645. TEST_SLEEP();
  24646. #ifdef WOLFSSL_TEST_CERT
  24647. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  24648. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  24649. if (ret != 0) {
  24650. FreeDecodedCert(decode);
  24651. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  24652. }
  24653. FreeDecodedCert(decode);
  24654. #endif
  24655. ret = SaveDerAndPem(der, certSz, certEccDerFile, certEccPemFile,
  24656. CERT_TYPE);
  24657. if (ret != 0) {
  24658. goto exit;
  24659. }
  24660. exit:
  24661. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24662. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24663. #endif
  24664. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24665. if (myCert != NULL)
  24666. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24667. #ifdef WOLFSSL_TEST_CERT
  24668. if (decode != NULL)
  24669. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24670. #endif
  24671. if (caEccKey != NULL) {
  24672. wc_ecc_free(caEccKey);
  24673. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24674. }
  24675. if (certPubKey != NULL) {
  24676. wc_ecc_free(certPubKey);
  24677. XFREE(certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24678. }
  24679. #else
  24680. wc_ecc_free(certPubKey);
  24681. wc_ecc_free(caEccKey);
  24682. #endif
  24683. return ret;
  24684. }
  24685. #endif /* WOLFSSL_CERT_GEN */
  24686. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  24687. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC) && \
  24688. (!defined(NO_ECC_SECP) || defined(WOLFSSL_CUSTOM_CURVES))
  24689. /* Test for the wc_ecc_key_new() and wc_ecc_key_free() functions. */
  24690. static wc_test_ret_t ecc_test_allocator(WC_RNG* rng)
  24691. {
  24692. wc_test_ret_t ret = 0;
  24693. ecc_key* key;
  24694. #ifdef WC_NO_RNG
  24695. word32 idx = 0;
  24696. #endif
  24697. key = wc_ecc_key_new(HEAP_HINT);
  24698. if (key == NULL) {
  24699. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  24700. }
  24701. #ifndef WC_NO_RNG
  24702. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  24703. #if defined(WOLFSSL_ASYNC_CRYPT)
  24704. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  24705. #endif
  24706. if (ret != 0)
  24707. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  24708. #else
  24709. /* use test ECC key */
  24710. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, key,
  24711. (word32)sizeof_ecc_key_der_256);
  24712. (void)rng;
  24713. #endif
  24714. exit:
  24715. wc_ecc_key_free(key);
  24716. return ret;
  24717. }
  24718. #endif
  24719. /* ECC Non-blocking tests for Sign and Verify */
  24720. /* Requires SP math and supports P384 or P256 */
  24721. /* ./configure --enable-ecc=nonblock --enable-sp=yes,nonblock CFLAGS="-DWOLFSSL_PUBLIC_MP" */
  24722. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_HAVE_SP_ECC) && \
  24723. defined(WOLFSSL_PUBLIC_MP)
  24724. /* ECC Private Key "d" */
  24725. static const byte p256PrivKey[] = {
  24726. /* SECP256R1 */
  24727. /* d */
  24728. 0x1e, 0xe7, 0x70, 0x07, 0xd3, 0x30, 0x94, 0x39,
  24729. 0x28, 0x90, 0xdf, 0x23, 0x88, 0x2c, 0x4a, 0x34,
  24730. 0x15, 0xdb, 0x4c, 0x43, 0xcd, 0xfa, 0xe5, 0x1f,
  24731. 0x3d, 0x4c, 0x37, 0xfe, 0x59, 0x3b, 0x96, 0xd8
  24732. };
  24733. #ifdef HAVE_ECC384
  24734. static const byte p384PrivKey[] = {
  24735. /* SECP384R1 */
  24736. /* d */
  24737. 0xa4, 0xe5, 0x06, 0xe8, 0x06, 0x16, 0x3e, 0xab,
  24738. 0x89, 0xf8, 0x60, 0x43, 0xc0, 0x60, 0x25, 0xdb,
  24739. 0xba, 0x7b, 0xfe, 0x19, 0x35, 0x08, 0x55, 0x65,
  24740. 0x76, 0xe2, 0xdc, 0xe0, 0x01, 0x8b, 0x6b, 0x68,
  24741. 0xdf, 0xcf, 0x6f, 0x80, 0x12, 0xce, 0x79, 0x37,
  24742. 0xeb, 0x2b, 0x9c, 0x7b, 0xc4, 0x68, 0x1c, 0x74
  24743. };
  24744. #endif /* HAVE_ECC384 */
  24745. #ifdef HAVE_ECC521
  24746. static const byte p521PrivKey[] = {
  24747. /* SECP521R1 */
  24748. /* d */
  24749. 0x01, 0x68, 0x91, 0x33, 0x53, 0xe2, 0x90, 0x68,
  24750. 0x11, 0x8f, 0xaa, 0xa8, 0x76, 0x0c, 0xf7, 0x2a,
  24751. 0x07, 0x1b, 0x92, 0x2a, 0xa7, 0x82, 0x3d, 0xfa,
  24752. 0x83, 0xce, 0x70, 0xc8, 0xc2, 0x60, 0x82, 0xfe,
  24753. 0x18, 0x88, 0x68, 0xda, 0x6a, 0x83, 0x46, 0x78,
  24754. 0xe4, 0xe9, 0xe9, 0xcc, 0x51, 0x7f, 0xed, 0x81,
  24755. 0x02, 0x32, 0xee, 0x26, 0x87, 0xcc, 0xed, 0x63,
  24756. 0x3f, 0x39, 0x27, 0xf0, 0xd7, 0x17, 0x77, 0xa1,
  24757. 0xa4, 0x36
  24758. };
  24759. #endif /* HAVE_ECC521 */
  24760. /* ECC public key Qx/Qy */
  24761. static const byte p256PubKey[] = {
  24762. /* SECP256R1 */
  24763. /* Qx */
  24764. 0x96, 0x93, 0x1c, 0x53, 0x0b, 0x43, 0x6c, 0x42,
  24765. 0x0c, 0x52, 0x90, 0xe4, 0xa7, 0xec, 0x98, 0xb1,
  24766. 0xaf, 0xd4, 0x14, 0x49, 0xd8, 0xc1, 0x42, 0x82,
  24767. 0x04, 0x78, 0xd1, 0x90, 0xae, 0xa0, 0x6c, 0x07,
  24768. /* Qy */
  24769. 0xf2, 0x3a, 0xb5, 0x10, 0x32, 0x8d, 0xce, 0x9e,
  24770. 0x76, 0xa0, 0xd2, 0x8c, 0xf3, 0xfc, 0xa9, 0x94,
  24771. 0x43, 0x24, 0xe6, 0x82, 0x00, 0x40, 0xc6, 0xdb,
  24772. 0x1c, 0x2f, 0xcd, 0x38, 0x4b, 0x60, 0xdd, 0x61
  24773. };
  24774. #ifdef HAVE_ECC384
  24775. static const byte p384PubKey[] = {
  24776. /* SECP384R1 */
  24777. /* Qx */
  24778. 0xea, 0xcf, 0x93, 0x4f, 0x2c, 0x09, 0xbb, 0x39,
  24779. 0x14, 0x0f, 0x56, 0x64, 0xc3, 0x40, 0xb4, 0xdf,
  24780. 0x0e, 0x63, 0xae, 0xe5, 0x71, 0x4b, 0x00, 0xcc,
  24781. 0x04, 0x97, 0xff, 0xe1, 0xe9, 0x38, 0x96, 0xbb,
  24782. 0x5f, 0x91, 0xb2, 0x6a, 0xcc, 0xb5, 0x39, 0x5f,
  24783. 0x8f, 0x70, 0x59, 0xf1, 0x01, 0xf6, 0x5a, 0x2b,
  24784. /* Qy */
  24785. 0x01, 0x6c, 0x68, 0x0b, 0xcf, 0x55, 0x25, 0xaf,
  24786. 0x6d, 0x98, 0x48, 0x0a, 0xa8, 0x74, 0xc9, 0xa9,
  24787. 0x17, 0xa0, 0x0c, 0xc3, 0xfb, 0xd3, 0x23, 0x68,
  24788. 0xfe, 0x04, 0x3c, 0x63, 0x50, 0x88, 0x3b, 0xb9,
  24789. 0x4f, 0x7c, 0x67, 0x34, 0xf7, 0x3b, 0xa9, 0x73,
  24790. 0xe7, 0x1b, 0xc3, 0x51, 0x5e, 0x22, 0x18, 0xec
  24791. };
  24792. #endif
  24793. #ifdef HAVE_ECC521
  24794. static const byte p521PubKey[] = {
  24795. /* SECP521R1 */
  24796. /* Qx */
  24797. 0x01, 0x62, 0x6e, 0xf1, 0x00, 0xec, 0xd8, 0x99,
  24798. 0x58, 0x9b, 0x80, 0x6b, 0xfe, 0x2c, 0xf1, 0xb2,
  24799. 0xf0, 0xc8, 0x48, 0xdf, 0xac, 0xd2, 0x3b, 0x71,
  24800. 0x29, 0xab, 0xf0, 0x66, 0x63, 0xd8, 0x8e, 0xb5,
  24801. 0xc8, 0xc2, 0xfc, 0x99, 0x44, 0xe2, 0x45, 0xb1,
  24802. 0x5a, 0x7b, 0xb9, 0x73, 0x01, 0xda, 0x79, 0xec,
  24803. 0x9c, 0x26, 0x27, 0x34, 0x45, 0x26, 0xd5, 0x89,
  24804. 0x4b, 0x44, 0xfe, 0x69, 0x4e, 0x72, 0x14, 0xe3,
  24805. 0x8b, 0xbc,
  24806. /* Qy */
  24807. 0x00, 0x0f, 0x09, 0xa2, 0x03, 0xc3, 0x5a, 0xdc,
  24808. 0x95, 0x82, 0xf6, 0xf9, 0xf6, 0x9c, 0xff, 0xb5,
  24809. 0x6b, 0x75, 0x95, 0x4b, 0xa4, 0x28, 0x5d, 0x9e,
  24810. 0x90, 0x04, 0xd1, 0xc0, 0x1e, 0xd5, 0xfd, 0x43,
  24811. 0x9e, 0x1e, 0x83, 0xc0, 0x11, 0x2b, 0x2b, 0x07,
  24812. 0x6d, 0xa9, 0x7a, 0x10, 0xd7, 0x67, 0xe7, 0x51,
  24813. 0x37, 0x24, 0xd8, 0xbf, 0x03, 0x0d, 0x8b, 0xb5,
  24814. 0x40, 0x5c, 0x4f, 0xd6, 0x13, 0x73, 0x42, 0xbc,
  24815. 0x91, 0xd9
  24816. };
  24817. /* perform verify of signature and hash using public key */
  24818. /* key is public Qx + public Qy */
  24819. /* sig is r + s */
  24820. static wc_test_ret_t crypto_ecc_verify(const byte *key, uint32_t keySz,
  24821. const byte *hash, uint32_t hashSz, const byte *sig, uint32_t sigSz,
  24822. uint32_t curveSz, int curveId)
  24823. {
  24824. wc_test_ret_t ret;
  24825. int verify_res = 0, count = 0;
  24826. mp_int r, s;
  24827. ecc_key ecc;
  24828. ecc_nb_ctx_t nb_ctx;
  24829. /* validate arguments */
  24830. if (key == NULL || hash == NULL || sig == NULL || curveSz == 0 ||
  24831. hashSz == 0 || keySz < (curveSz*2) || sigSz < (curveSz*2))
  24832. {
  24833. return WC_TEST_RET_ENC_NC;
  24834. }
  24835. /* Setup the ECC key */
  24836. ret = wc_ecc_init(&ecc);
  24837. if (ret < 0) {
  24838. return WC_TEST_RET_ENC_EC(ret);
  24839. }
  24840. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  24841. if (ret != MP_OKAY) {
  24842. wc_ecc_free(&ecc);
  24843. return WC_TEST_RET_ENC_EC(ret);
  24844. }
  24845. /* Setup the signature r/s variables */
  24846. ret = mp_init(&r);
  24847. if (ret != MP_OKAY) {
  24848. wc_ecc_free(&ecc);
  24849. return WC_TEST_RET_ENC_EC(ret);
  24850. }
  24851. ret = mp_init(&s);
  24852. if (ret != MP_OKAY) {
  24853. mp_clear(&r);
  24854. wc_ecc_free(&ecc);
  24855. return WC_TEST_RET_ENC_EC(ret);
  24856. }
  24857. /* Import public key x/y */
  24858. ret = wc_ecc_import_unsigned(
  24859. &ecc,
  24860. (byte*)key, /* Public "x" Coordinate */
  24861. (byte*)(key + curveSz), /* Public "y" Coordinate */
  24862. NULL, /* Private "d" (optional) */
  24863. curveId /* ECC Curve Id */
  24864. );
  24865. /* Make sure it was a public key imported */
  24866. if (ret == 0 && ecc.type != ECC_PUBLICKEY) {
  24867. ret = WC_TEST_RET_ENC_NC; /* ECC_BAD_ARG_E */
  24868. }
  24869. /* Import signature r/s */
  24870. if (ret == 0) {
  24871. ret = mp_read_unsigned_bin(&r, sig, curveSz);
  24872. if (ret < 0)
  24873. ret = WC_TEST_RET_ENC_EC(ret);
  24874. }
  24875. if (ret == 0) {
  24876. ret = mp_read_unsigned_bin(&s, sig + curveSz, curveSz);
  24877. if (ret < 0)
  24878. ret = WC_TEST_RET_ENC_EC(ret);
  24879. }
  24880. /* Verify ECC Signature */
  24881. if (ret == 0) {
  24882. do {
  24883. ret = wc_ecc_verify_hash_ex(
  24884. &r, &s, /* r/s as mp_int */
  24885. hash, hashSz, /* computed hash digest */
  24886. &verify_res, /* verification result 1=success */
  24887. &ecc
  24888. );
  24889. count++;
  24890. /* This is where real-time work could be called */
  24891. } while (ret == FP_WOULDBLOCK);
  24892. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  24893. printf("ECC non-block verify: %d times\n", count);
  24894. #endif
  24895. if (ret < 0)
  24896. ret = WC_TEST_RET_ENC_EC(ret);
  24897. }
  24898. /* check verify result */
  24899. if (ret == 0 && verify_res == 0) {
  24900. ret = WC_TEST_RET_ENC_NC /* SIG_VERIFY_E */;
  24901. }
  24902. mp_clear(&r);
  24903. mp_clear(&s);
  24904. wc_ecc_free(&ecc);
  24905. (void)count;
  24906. return ret;
  24907. }
  24908. /* perform signature operation against hash using private key */
  24909. static wc_test_ret_t crypto_ecc_sign(const byte *key, uint32_t keySz,
  24910. const byte *hash, uint32_t hashSz, byte *sig, uint32_t* sigSz,
  24911. uint32_t curveSz, int curveId, WC_RNG* rng)
  24912. {
  24913. wc_test_ret_t ret;
  24914. int count = 0;
  24915. mp_int r, s;
  24916. ecc_key ecc;
  24917. ecc_nb_ctx_t nb_ctx;
  24918. /* validate arguments */
  24919. if (key == NULL || hash == NULL || sig == NULL || sigSz == NULL ||
  24920. curveSz == 0 || hashSz == 0 || keySz < curveSz || *sigSz < (curveSz*2))
  24921. {
  24922. return WC_TEST_RET_ENC_NC /* BAD_FUNC_ARG */;
  24923. }
  24924. /* Initialize signature result */
  24925. memset(sig, 0, curveSz*2);
  24926. /* Setup the ECC key */
  24927. ret = wc_ecc_init(&ecc);
  24928. if (ret < 0) {
  24929. return WC_TEST_RET_ENC_EC(ret);
  24930. }
  24931. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  24932. if (ret != MP_OKAY) {
  24933. wc_ecc_free(&ecc);
  24934. return WC_TEST_RET_ENC_EC(ret);
  24935. }
  24936. /* Setup the signature r/s variables */
  24937. ret = mp_init(&r);
  24938. if (ret != MP_OKAY) {
  24939. wc_ecc_free(&ecc);
  24940. return WC_TEST_RET_ENC_EC(ret);
  24941. }
  24942. ret = mp_init(&s);
  24943. if (ret != MP_OKAY) {
  24944. mp_clear(&r);
  24945. wc_ecc_free(&ecc);
  24946. return WC_TEST_RET_ENC_EC(ret);
  24947. }
  24948. /* Import private key "k" */
  24949. ret = wc_ecc_import_private_key_ex(
  24950. key, keySz, /* private key "d" */
  24951. NULL, 0, /* public (optional) */
  24952. &ecc,
  24953. curveId /* ECC Curve Id */
  24954. );
  24955. if (ret < 0)
  24956. ret = WC_TEST_RET_ENC_EC(ret);
  24957. if (ret == 0) {
  24958. do {
  24959. /* Verify ECC Signature */
  24960. ret = wc_ecc_sign_hash_ex(
  24961. hash, hashSz, /* computed hash digest */
  24962. rng, &ecc, /* random and key context */
  24963. &r, &s /* r/s as mp_int */
  24964. );
  24965. count++;
  24966. /* This is where real-time work could be called */
  24967. } while (ret == FP_WOULDBLOCK);
  24968. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  24969. printf("ECC non-block sign: %d times\n", count);
  24970. #endif
  24971. if (ret < 0)
  24972. ret = WC_TEST_RET_ENC_EC(ret);
  24973. }
  24974. if (ret == 0) {
  24975. /* export r/s */
  24976. mp_to_unsigned_bin_len(&r, sig, curveSz);
  24977. mp_to_unsigned_bin_len(&s, sig + curveSz, curveSz);
  24978. }
  24979. mp_clear(&r);
  24980. mp_clear(&s);
  24981. wc_ecc_free(&ecc);
  24982. (void)count;
  24983. return ret;
  24984. }
  24985. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  24986. /*
  24987. * This test doesn't work with WOLFSSL_VALIDATE_ECC_KEYGEN defined because we
  24988. * don't have non-blocking versions of the key checking functions, yet.
  24989. */
  24990. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_VALIDATE_ECC_KEYGEN)
  24991. static wc_test_ret_t ecc_test_nonblock_dhe(int curveId, word32 curveSz,
  24992. const byte* privKey, const byte* pubKey, WC_RNG* rng)
  24993. {
  24994. wc_test_ret_t ret;
  24995. ecc_key keyA;
  24996. ecc_key keyB;
  24997. ecc_nb_ctx_t nbCtxA;
  24998. ecc_nb_ctx_t nbCtxB;
  24999. byte secretA[ECC_SHARED_SIZE];
  25000. byte secretB[ECC_SHARED_SIZE];
  25001. word32 secretSzA = ECC_SHARED_SIZE;
  25002. word32 secretSzB = ECC_SHARED_SIZE;
  25003. int count = 0;
  25004. ret = wc_ecc_init(&keyA);
  25005. if (ret == 0) {
  25006. ret = wc_ecc_init(&keyB);
  25007. if (ret < 0)
  25008. ret = WC_TEST_RET_ENC_EC(ret);
  25009. }
  25010. if (ret == 0) {
  25011. ret = wc_ecc_set_nonblock(&keyA, &nbCtxA);
  25012. if (ret < 0)
  25013. ret = WC_TEST_RET_ENC_EC(ret);
  25014. }
  25015. if (ret == 0) {
  25016. ret = wc_ecc_set_nonblock(&keyB, &nbCtxB);
  25017. if (ret < 0)
  25018. ret = WC_TEST_RET_ENC_EC(ret);
  25019. }
  25020. if (ret == 0) {
  25021. do {
  25022. ret = wc_ecc_make_key_ex(rng, curveSz, &keyA, curveId);
  25023. count++;
  25024. } while (ret == FP_WOULDBLOCK);
  25025. if (ret < 0)
  25026. ret = WC_TEST_RET_ENC_EC(ret);
  25027. }
  25028. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  25029. fprintf(stderr, "ECC non-block key gen: %d times\n", count);
  25030. #endif
  25031. if (ret == 0) {
  25032. ret = wc_ecc_check_key(&keyA);
  25033. if (ret < 0)
  25034. ret = WC_TEST_RET_ENC_EC(ret);
  25035. }
  25036. if (ret == 0) {
  25037. ret = wc_ecc_import_unsigned(&keyB, pubKey, pubKey + curveSz,
  25038. privKey, curveId);
  25039. if (ret < 0)
  25040. ret = WC_TEST_RET_ENC_EC(ret);
  25041. }
  25042. count = 0;
  25043. if (ret == 0) {
  25044. do {
  25045. ret = wc_ecc_shared_secret(&keyA, &keyB, secretA, &secretSzA);
  25046. count++;
  25047. } while (ret == FP_WOULDBLOCK);
  25048. if (ret < 0)
  25049. ret = WC_TEST_RET_ENC_EC(ret);
  25050. }
  25051. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  25052. fprintf(stderr, "ECC non-block shared secret: %d times\n", count);
  25053. #endif
  25054. if (ret == 0) {
  25055. do {
  25056. ret = wc_ecc_shared_secret(&keyB, &keyA, secretB, &secretSzB);
  25057. } while (ret == FP_WOULDBLOCK);
  25058. if (ret < 0)
  25059. ret = WC_TEST_RET_ENC_EC(ret);
  25060. }
  25061. if (ret == 0) {
  25062. if (secretSzA != secretSzB ||
  25063. XMEMCMP(secretA, secretB, secretSzA) != 0) {
  25064. ret = WC_TEST_RET_ENC_NC;
  25065. }
  25066. }
  25067. wc_ecc_free(&keyA);
  25068. wc_ecc_free(&keyB);
  25069. return ret;
  25070. }
  25071. #endif /* HAVE_ECC_DHE && !WOLFSSL_VALIDATE_ECC_KEYGEN */
  25072. #if defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  25073. static wc_test_ret_t ecc_test_nonblock_ecdsa(int curveId, word32 curveSz,
  25074. const byte* privKey, word32 privKeySz, const byte* pubKey, word32 pubKeySz,
  25075. WC_RNG* rng)
  25076. {
  25077. wc_test_ret_t ret = 0;
  25078. byte* sig = NULL;
  25079. word32 sigSz = curveSz * 2;
  25080. static const byte hash[] = {
  25081. 0x8d, 0x28, 0xa3, 0x8b, 0x0b, 0xa9, 0xfe, 0xd4, 0x0e, 0x54, 0xc4, 0x17,
  25082. 0x3d, 0x54, 0x66, 0x34, 0xbf, 0x5d, 0x6f, 0x46, 0xc2, 0x20, 0xcb, 0xc3,
  25083. 0x22, 0xe9, 0xb0, 0xdf, 0xe7, 0x64, 0x3f, 0xd9
  25084. };
  25085. sig = (byte*)XMALLOC(sigSz, HEAP_HINT, DYNAMIC_TYPE_SIGNATURE);
  25086. if (sig == NULL) {
  25087. ret = WC_TEST_RET_ENC_ERRNO;
  25088. }
  25089. if (ret == 0) {
  25090. /* Sign hash using private key */
  25091. /* Note: result of an ECC sign varies for each call even with same
  25092. private key and hash. This is because a new random public key is
  25093. used for each operation. */
  25094. ret = crypto_ecc_sign(privKey, privKeySz, hash, sizeof(hash), sig,
  25095. &sigSz, curveSz, curveId, rng);
  25096. }
  25097. if (ret == 0) {
  25098. /* Verify generated signature is valid */
  25099. ret = crypto_ecc_verify(pubKey, pubKeySz, hash, sizeof(hash), sig,
  25100. sigSz, curveSz, curveId);
  25101. }
  25102. if (sig != NULL) {
  25103. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_SIGNATURE);
  25104. }
  25105. return ret;
  25106. }
  25107. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  25108. static wc_test_ret_t ecc_test_nonblock(WC_RNG* rng)
  25109. {
  25110. wc_test_ret_t ret = 0;
  25111. word32 i;
  25112. int curveIds[3] = {0, 0, 0};
  25113. word32 curveSzs[3] = {0, 0, 0};
  25114. const byte* privKeys[3] = {NULL, NULL, NULL};
  25115. word32 privKeySzs[3] = {0, 0, 0};
  25116. const byte* pubKeys[3] = {NULL, NULL, NULL};
  25117. word32 pubKeySzs[3] = {0, 0, 0};
  25118. curveIds[0] = ECC_SECP256R1;
  25119. curveSzs[0] = 32;
  25120. privKeys[0] = p256PrivKey;
  25121. privKeySzs[0] = sizeof(p256PrivKey);
  25122. pubKeys[0] = p256PubKey;
  25123. pubKeySzs[0] = sizeof(p256PubKey);
  25124. #ifdef HAVE_ECC384
  25125. curveIds[1] = ECC_SECP384R1;
  25126. curveSzs[1] = 48;
  25127. privKeys[1] = p384PrivKey;
  25128. privKeySzs[1] = sizeof(p384PrivKey);
  25129. pubKeys[1] = p384PubKey;
  25130. pubKeySzs[1] = sizeof(p384PubKey);
  25131. #endif
  25132. #ifdef HAVE_ECC521
  25133. curveIds[2] = ECC_SECP521R1;
  25134. curveSzs[2] = 66;
  25135. privKeys[2] = p521PrivKey;
  25136. privKeySzs[2] = sizeof(p521PrivKey);
  25137. pubKeys[2] = p521PubKey;
  25138. pubKeySzs[2] = sizeof(p521PubKey);
  25139. #endif
  25140. for (i = 0; ret == 0 && i < sizeof(curveIds) / sizeof(curveIds[0]); ++i) {
  25141. if (curveIds[i] == 0) {
  25142. continue;
  25143. }
  25144. #if defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  25145. ret = ecc_test_nonblock_ecdsa(curveIds[i], curveSzs[i], privKeys[i],
  25146. privKeySzs[i], pubKeys[i], pubKeySzs[i], rng);
  25147. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  25148. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_VALIDATE_ECC_KEYGEN)
  25149. if (ret == 0) {
  25150. ret = ecc_test_nonblock_dhe(curveIds[i], curveSzs[i], privKeys[i],
  25151. pubKeys[i], rng);
  25152. }
  25153. #endif /* HAVE_ECC_DHE && !WOLFSSL_VALIDATE_ECC_KEYGEN */
  25154. }
  25155. return ret;
  25156. }
  25157. #endif /* WC_ECC_NONBLOCK && WOLFSSL_HAVE_SP_ECC && WOLFSSL_PUBLIC_MP */
  25158. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_test(void)
  25159. {
  25160. wc_test_ret_t ret;
  25161. WC_RNG rng;
  25162. #if defined(WOLFSSL_CERT_EXT) && \
  25163. (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  25164. ret = ecc_decode_test();
  25165. if (ret < 0)
  25166. return ret;
  25167. #endif
  25168. #ifndef HAVE_FIPS
  25169. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  25170. #else
  25171. ret = wc_InitRng(&rng);
  25172. #endif
  25173. #ifndef WC_NO_RNG
  25174. if (ret != 0)
  25175. return WC_TEST_RET_ENC_EC(ret);
  25176. #else
  25177. (void)ret;
  25178. #endif
  25179. #if (defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 112
  25180. ret = ecc_test_curve(&rng, 14, ECC_CURVE_DEF);
  25181. if (ret < 0) {
  25182. printf("keySize=14, Default\n");
  25183. goto done;
  25184. }
  25185. #endif /* HAVE_ECC112 */
  25186. #if (defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 128
  25187. ret = ecc_test_curve(&rng, 16, ECC_CURVE_DEF);
  25188. if (ret < 0) {
  25189. printf("keySize=16, Default\n");
  25190. goto done;
  25191. }
  25192. #endif /* HAVE_ECC128 */
  25193. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  25194. ret = ecc_test_curve(&rng, 20, ECC_CURVE_DEF);
  25195. if (ret < 0) {
  25196. printf("keySize=20, Default\n");
  25197. goto done;
  25198. }
  25199. #endif /* HAVE_ECC160 */
  25200. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  25201. ret = ecc_test_curve(&rng, 24, ECC_CURVE_DEF);
  25202. printf("keySize=24, Default\n");
  25203. if (ret < 0) {
  25204. goto done;
  25205. }
  25206. #endif /* HAVE_ECC192 */
  25207. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  25208. ret = ecc_test_curve(&rng, 28, ECC_CURVE_DEF);
  25209. if (ret < 0) {
  25210. printf("keySize=28, Default\n");
  25211. goto done;
  25212. }
  25213. #endif /* HAVE_ECC224 */
  25214. #if (defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 239
  25215. ret = ecc_test_curve(&rng, 30, ECC_CURVE_DEF);
  25216. if (ret < 0) {
  25217. printf("keySize=30, Default\n");
  25218. goto done;
  25219. }
  25220. #endif /* HAVE_ECC239 */
  25221. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  25222. ret = ecc_test_curve(&rng, 32, ECC_CURVE_DEF);
  25223. if (ret < 0) {
  25224. printf("keySize=32, Default\n");
  25225. goto done;
  25226. }
  25227. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  25228. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  25229. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  25230. ret = ecc_point_test();
  25231. if (ret < 0) {
  25232. goto done;
  25233. }
  25234. #endif
  25235. #if !defined(NO_ECC_SECP) || defined(WOLFSSL_CUSTOM_CURVES)
  25236. ret = ecc_def_curve_test(&rng);
  25237. if (ret < 0) {
  25238. printf("Default\n");
  25239. goto done;
  25240. }
  25241. #endif
  25242. #endif /* !NO_ECC256 */
  25243. #if (defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 320
  25244. ret = ecc_test_curve(&rng, 40, ECC_CURVE_DEF);
  25245. if (ret < 0) {
  25246. printf("keySize=40, Default\n");
  25247. goto done;
  25248. }
  25249. #endif /* HAVE_ECC320 */
  25250. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  25251. ret = ecc_test_curve(&rng, 48, ECC_CURVE_DEF);
  25252. if (ret < 0) {
  25253. printf("keySize=48, Default\n");
  25254. goto done;
  25255. }
  25256. #endif /* HAVE_ECC384 */
  25257. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  25258. ret = ecc_test_curve(&rng, 64, ECC_CURVE_DEF);
  25259. if (ret < 0) {
  25260. printf("keySize=64, Default\n");
  25261. goto done;
  25262. }
  25263. #endif /* HAVE_ECC512 */
  25264. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  25265. ret = ecc_test_curve(&rng, 66, ECC_CURVE_DEF);
  25266. if (ret < 0) {
  25267. printf("keySize=68, Default\n");
  25268. goto done;
  25269. }
  25270. #endif /* HAVE_ECC521 */
  25271. #ifdef WOLFSSL_SM2
  25272. ret = ecc_test_curve(&rng, 32, ECC_SM2P256V1);
  25273. if (ret < 0) {
  25274. printf("SM2\n");
  25275. goto done;
  25276. }
  25277. #endif
  25278. #if defined(WOLFSSL_CUSTOM_CURVES)
  25279. ret = ecc_test_custom_curves(&rng);
  25280. if (ret != 0) {
  25281. printf("Custom\n");
  25282. goto done;
  25283. }
  25284. #endif
  25285. #if defined(WOLFSSL_SM2)
  25286. ret = test_sm2_verify();
  25287. if (ret != 0) {
  25288. printf("SM2 Verify\n");
  25289. goto done;
  25290. }
  25291. ret = ecc_sm2_test_curve(&rng, ECC_TEST_VERIFY_COUNT);
  25292. if (ret != 0) {
  25293. printf("SM2 test\n");
  25294. goto done;
  25295. }
  25296. #endif
  25297. #if defined(HAVE_ECC_SIGN) && (defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  25298. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)) \
  25299. && (!defined(FIPS_VERSION_GE) || FIPS_VERSION_GE(5,3))
  25300. #ifdef HAVE_ECC256
  25301. ret = ecc_test_deterministic_k(&rng);
  25302. if (ret != 0) {
  25303. printf("ecc_test_deterministic_k failed!\n");
  25304. goto done;
  25305. }
  25306. #endif
  25307. #ifdef WOLFSSL_PUBLIC_MP
  25308. #if defined(HAVE_ECC384)
  25309. ret = ecc384_test_deterministic_k(&rng);
  25310. if (ret != 0) {
  25311. printf("ecc384_test_deterministic_k failed!\n");
  25312. goto done;
  25313. }
  25314. #endif
  25315. #if defined(HAVE_ECC521)
  25316. ret = ecc521_test_deterministic_k(&rng);
  25317. if (ret != 0) {
  25318. printf("ecc512_test_deterministic_k failed!\n");
  25319. goto done;
  25320. }
  25321. #endif
  25322. #endif
  25323. #endif
  25324. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K) && \
  25325. !defined(WOLFSSL_KCAPI_ECC)
  25326. ret = ecc_test_sign_vectors(&rng);
  25327. if (ret != 0) {
  25328. printf("ecc_test_sign_vectors failed!\n");
  25329. goto done;
  25330. }
  25331. #endif
  25332. #if defined(HAVE_ECC_VECTOR_TEST) && defined(HAVE_ECC_CDH) && \
  25333. defined(HAVE_ECC_DHE)
  25334. ret = ecc_test_cdh_vectors(&rng);
  25335. if (ret != 0) {
  25336. printf("ecc_test_cdh_vectors failed!\n");
  25337. goto done;
  25338. }
  25339. #endif
  25340. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  25341. !defined(WOLFSSL_STM32_PKA) && !defined(WOLFSSL_SILABS_SE_ACCEL) && \
  25342. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ECC_SECP)
  25343. ret = ecc_test_make_pub(&rng);
  25344. if (ret != 0) {
  25345. printf("ecc_test_make_pub failed!\n");
  25346. goto done;
  25347. }
  25348. #elif defined(HAVE_ECC_KEY_IMPORT)
  25349. (void)ecc_test_make_pub; /* for compiler warning */
  25350. #endif
  25351. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ECC_SECP) && !defined(NO_ASN_TIME)
  25352. ret = ecc_test_cert_gen(&rng);
  25353. if (ret != 0) {
  25354. printf("ecc_test_cert_gen failed!\n");
  25355. goto done;
  25356. }
  25357. #endif
  25358. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC) && \
  25359. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && (!defined(NO_ECC_SECP) || \
  25360. defined(WOLFSSL_CUSTOM_CURVES))
  25361. ret = ecc_test_allocator(&rng);
  25362. if (ret != 0) {
  25363. printf("ecc_test_allocator failed!\n");
  25364. goto done;
  25365. }
  25366. #endif
  25367. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_PUBLIC_MP) && \
  25368. defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  25369. ret = ecc_test_nonblock(&rng);
  25370. if (ret != 0) {
  25371. printf("ecc_test_nonblock failed!\n");
  25372. goto done;
  25373. }
  25374. #endif
  25375. done:
  25376. wc_FreeRng(&rng);
  25377. return ret;
  25378. }
  25379. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  25380. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  25381. #if ((! defined(HAVE_FIPS)) || FIPS_VERSION_GE(5,3))
  25382. static wc_test_ret_t ecc_ctx_kdf_salt_test(WC_RNG* rng, ecc_key* a, ecc_key* b)
  25383. {
  25384. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25385. byte* plaintext;
  25386. byte* encrypted;
  25387. byte* decrypted;
  25388. #else
  25389. byte plaintext[128];
  25390. byte encrypted[128];
  25391. byte decrypted[128];
  25392. #endif
  25393. ecEncCtx* aCtx = NULL;
  25394. ecEncCtx* bCtx = NULL;
  25395. static const byte salt[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
  25396. 14, 15};
  25397. wc_test_ret_t ret = 0;
  25398. static const char message[] = "Hello wolfSSL!";
  25399. word32 plaintextLen;
  25400. word32 encryptLen = 128;
  25401. word32 decryptLen = 128;
  25402. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25403. plaintext = XMALLOC(128, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25404. encrypted = XMALLOC(128, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25405. decrypted = XMALLOC(128, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25406. #endif
  25407. wc_ecc_free(a);
  25408. wc_ecc_free(b);
  25409. ret = wc_ecc_init(a);
  25410. if (ret != 0)
  25411. ret = WC_TEST_RET_ENC_EC(ret);
  25412. if (ret == 0) {
  25413. ret = wc_ecc_init(b);
  25414. if (ret != 0)
  25415. ret = WC_TEST_RET_ENC_EC(ret);
  25416. }
  25417. if (ret == 0)
  25418. ret = wc_ecc_make_key(rng, 32, a);
  25419. if (ret == 0)
  25420. ret = wc_ecc_make_key(rng, 32, b);
  25421. /* create context */
  25422. if (ret == 0) {
  25423. aCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  25424. if (aCtx == NULL)
  25425. ret = WC_TEST_RET_ENC_NC;
  25426. }
  25427. if (ret == 0) {
  25428. bCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  25429. if (bCtx == NULL)
  25430. ret = WC_TEST_RET_ENC_NC;
  25431. }
  25432. /* set salt */
  25433. if (ret == 0) {
  25434. ret = wc_ecc_ctx_set_kdf_salt(aCtx, salt, sizeof(salt));
  25435. if (ret != 0)
  25436. ret = 10472;
  25437. }
  25438. if (ret == 0) {
  25439. ret = wc_ecc_ctx_set_kdf_salt(bCtx, salt, sizeof(salt));
  25440. if (ret != 0)
  25441. ret = 10473;
  25442. }
  25443. XMEMSET(plaintext, 0, 128);
  25444. XSTRLCPY((char *)plaintext, message, sizeof plaintext);
  25445. plaintextLen = (((word32)XSTRLEN(message) + AES_BLOCK_SIZE - 1) /
  25446. AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
  25447. /* encrypt */
  25448. if (ret == 0) {
  25449. ret = wc_ecc_encrypt(a, b, plaintext, plaintextLen, encrypted,
  25450. &encryptLen, aCtx);
  25451. if (ret != 0)
  25452. ret = WC_TEST_RET_ENC_EC(ret);
  25453. }
  25454. /* decrypt */
  25455. if (ret == 0) {
  25456. ret = wc_ecc_decrypt(b, a, encrypted, encryptLen, decrypted,
  25457. &decryptLen, bCtx);
  25458. if (ret != 0)
  25459. ret = WC_TEST_RET_ENC_EC(ret);
  25460. }
  25461. /* compare */
  25462. if (ret == 0 && XMEMCMP(decrypted, plaintext, plaintextLen) != 0)
  25463. ret = WC_TEST_RET_ENC_NC;
  25464. wc_ecc_free(a);
  25465. wc_ecc_free(b);
  25466. wc_ecc_ctx_free(aCtx);
  25467. wc_ecc_ctx_free(bCtx);
  25468. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25469. XFREE(plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25470. XFREE(encrypted, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25471. XFREE(decrypted, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25472. #endif
  25473. return ret;
  25474. }
  25475. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  25476. /* ecc_encrypt_e2e_test() uses wc_ecc_ctx_set_algo(), which was added in
  25477. * wolfFIPS 5.3.
  25478. * ecc_encrypt_kat() is used only by ecc_encrypt_e2e_test().
  25479. */
  25480. #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))
  25481. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  25482. ECC_MIN_KEY_SZ <= 256 && defined(WOLFSSL_AES_128)
  25483. static wc_test_ret_t ecc_encrypt_kat(WC_RNG *rng)
  25484. {
  25485. wc_test_ret_t ret = 0;
  25486. #ifdef WOLFSSL_ECIES_OLD
  25487. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25488. ecc_key* userA = NULL;
  25489. #else
  25490. ecc_key userA[1];
  25491. #endif
  25492. int userAInit = 0;
  25493. #endif
  25494. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25495. ecc_key* userB = NULL;
  25496. #else
  25497. ecc_key userB[1];
  25498. #endif
  25499. int userBInit = 0;
  25500. ecc_key* tmpKey;
  25501. byte plain[48];
  25502. word32 plainSz = sizeof(plain);
  25503. WOLFSSL_SMALL_STACK_STATIC const byte privKey[] = {
  25504. 0x04, 0x80, 0xef, 0x1d, 0xbe, 0x02, 0x0c, 0x20,
  25505. 0x5b, 0xab, 0x80, 0x35, 0x5b, 0x2a, 0x0f, 0x6d,
  25506. 0xd3, 0xb0, 0x7f, 0x7e, 0x7f, 0x86, 0x8a, 0x49,
  25507. 0xee, 0xb4, 0xaa, 0x09, 0x2d, 0x1e, 0x1d, 0x02
  25508. };
  25509. #if defined(WOLFSSL_ECIES_OLD) || defined(WOLFSSL_QNX_CAAM)
  25510. WOLFSSL_SMALL_STACK_STATIC const byte pubKey[] = {
  25511. 0x04,
  25512. /* X */
  25513. 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0, 0x5a,
  25514. 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c, 0x3a,
  25515. 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3, 0xc1,
  25516. 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63, 0xa0,
  25517. /* X */
  25518. 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7, 0xcd,
  25519. 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75, 0xaa,
  25520. 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe, 0xe8,
  25521. 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3, 0x28
  25522. };
  25523. #endif
  25524. WOLFSSL_SMALL_STACK_STATIC const byte enc_msg[] = {
  25525. #ifdef WOLFSSL_ECIES_OLD
  25526. 0x42, 0x70, 0xbf, 0xf9, 0xf4, 0x7e, 0x4b, 0x9b,
  25527. 0xb5, 0x4c, 0xcc, 0xc5, 0x94, 0xa7, 0xef, 0xaa,
  25528. 0xc3, 0x7c, 0x85, 0xa6, 0x51, 0x6e, 0xd3, 0xfa,
  25529. 0x56, 0xc9, 0x10, 0x4d, 0x14, 0x32, 0x61, 0xb8,
  25530. 0xbb, 0x66, 0x7a, 0xb5, 0xbc, 0x95, 0xf8, 0xca,
  25531. 0xd1, 0x2a, 0x19, 0x51, 0x44, 0xd8, 0x0e, 0x57,
  25532. 0x34, 0xed, 0x45, 0x89, 0x2e, 0x57, 0xbe, 0xd5,
  25533. 0x06, 0x22, 0xd7, 0x13, 0x0a, 0x0e, 0x40, 0x36,
  25534. 0x0d, 0x05, 0x0d, 0xb6, 0xae, 0x61, 0x37, 0x18,
  25535. 0x83, 0x90, 0x0a, 0x27, 0x95, 0x41, 0x8c, 0x45
  25536. #elif defined(WOLFSSL_ECIES_ISO18033)
  25537. 0x04, 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0,
  25538. 0x5a, 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c,
  25539. 0x3a, 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3,
  25540. 0xc1, 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63,
  25541. 0xa0, 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7,
  25542. 0xcd, 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75,
  25543. 0xaa, 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe,
  25544. 0xe8, 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3,
  25545. 0x28, 0xbb, 0x9f, 0xa8, 0x2d, 0xe1, 0xf1, 0x67,
  25546. 0x45, 0x02, 0x19, 0xdc, 0xc8, 0x24, 0x8b, 0x20,
  25547. 0x02, 0xa0, 0x8f, 0x95, 0x12, 0x55, 0x51, 0xf8,
  25548. 0x03, 0xc4, 0x54, 0x13, 0x98, 0x2d, 0xf0, 0x31,
  25549. 0x51, 0x80, 0x45, 0x24, 0xcb, 0x8b, 0x48, 0xa6,
  25550. 0x8b, 0x8e, 0x97, 0x9c, 0x56, 0x4d, 0x70, 0x00,
  25551. 0x53, 0xd3, 0x47, 0x00, 0x5a, 0x23, 0x8c, 0xf9,
  25552. 0xfd, 0xd2, 0x33, 0x2c, 0x43, 0x6e, 0x9e, 0xb2,
  25553. 0xf4, 0x95, 0xd4, 0xcf, 0x30, 0xd6, 0xa2, 0xc5,
  25554. 0x35, 0x96, 0x6a, 0xd4, 0x36, 0x15, 0xa9, 0xbd,
  25555. 0x7f
  25556. #elif defined(WOLFSSL_ECIES_GEN_IV)
  25557. /* EC P-256 point */
  25558. 0x04,
  25559. /* X */
  25560. 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0, 0x5a,
  25561. 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c, 0x3a,
  25562. 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3, 0xc1,
  25563. 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63, 0xa0,
  25564. /* Y */
  25565. 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7, 0xcd,
  25566. 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75, 0xaa,
  25567. 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe, 0xe8,
  25568. 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3, 0x28,
  25569. /* IV */
  25570. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  25571. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  25572. /* Encrypted Msg */
  25573. 0xe5, 0x17, 0xaf, 0x0d, 0x65, 0x4d, 0x3d, 0x50,
  25574. 0x96, 0x05, 0xc9, 0x63, 0x2c, 0xef, 0x1c, 0x1f,
  25575. 0x78, 0xc9, 0x90, 0x7a, 0x14, 0x00, 0xfc, 0x44,
  25576. 0x71, 0x6d, 0x57, 0x8c, 0xdf, 0x23, 0xca, 0x65,
  25577. 0xcf, 0x93, 0x06, 0xb6, 0x9a, 0xf4, 0x61, 0xbd,
  25578. 0x44, 0x1a, 0xeb, 0x52, 0x68, 0x0f, 0xd1, 0xde,
  25579. /* HMAC */
  25580. 0x5a, 0x22, 0xc1, 0x5d, 0x99, 0x66, 0x3f, 0x24,
  25581. 0x35, 0x96, 0xac, 0xf7, 0xf6, 0x28, 0x45, 0x16,
  25582. 0x52, 0x19, 0x0d, 0xe4, 0xb2, 0xca, 0x5b, 0x28,
  25583. 0x4e, 0xbb, 0xf3, 0x98, 0x57, 0xd7, 0x3b, 0xe2
  25584. #else
  25585. 0x04, 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0,
  25586. 0x5a, 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c,
  25587. 0x3a, 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3,
  25588. 0xc1, 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63,
  25589. 0xa0, 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7,
  25590. 0xcd, 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75,
  25591. 0xaa, 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe,
  25592. 0xe8, 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3,
  25593. 0x28, 0xe5, 0x17, 0xaf, 0x0d, 0x65, 0x4d, 0x3d,
  25594. 0x50, 0x96, 0x05, 0xc9, 0x63, 0x2c, 0xef, 0x1c,
  25595. 0x1f, 0x78, 0xc9, 0x90, 0x7a, 0x14, 0x00, 0xfc,
  25596. 0x44, 0x71, 0x6d, 0x57, 0x8c, 0xdf, 0x23, 0xca,
  25597. 0x65, 0xcf, 0x93, 0x06, 0xb6, 0x9a, 0xf4, 0x61,
  25598. 0xbd, 0x44, 0x1a, 0xeb, 0x52, 0x68, 0x0f, 0xd1,
  25599. 0xde, 0xc7, 0x3f, 0x6f, 0xce, 0xbe, 0x49, 0x61,
  25600. 0x48, 0x01, 0x77, 0x41, 0xd0, 0xd8, 0x5b, 0x48,
  25601. 0xca, 0x4e, 0x47, 0x3e, 0x47, 0xbf, 0x1d, 0x28,
  25602. 0x4c, 0x18, 0x1a, 0xfb, 0x96, 0x95, 0xda, 0xde,
  25603. 0x55
  25604. #endif
  25605. };
  25606. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  25607. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  25608. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  25609. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  25610. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  25611. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  25612. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  25613. };
  25614. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25615. userB = (ecc_key *)XMALLOC(sizeof(*userB), HEAP_HINT,
  25616. DYNAMIC_TYPE_TMP_BUFFER);
  25617. if (userB == NULL) {
  25618. ret = WC_TEST_RET_ENC_ERRNO;
  25619. }
  25620. #ifdef WOLFSSL_ECIES_OLD
  25621. if (ret == 0) {
  25622. userA = (ecc_key *)XMALLOC(sizeof(*userA), HEAP_HINT,
  25623. DYNAMIC_TYPE_TMP_BUFFER);
  25624. if (userA == NULL) {
  25625. ret = WC_TEST_RET_ENC_ERRNO;
  25626. }
  25627. }
  25628. #endif
  25629. #endif
  25630. if (ret == 0) {
  25631. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  25632. if (ret != 0)
  25633. ret = WC_TEST_RET_ENC_EC(ret);
  25634. }
  25635. if (ret == 0) {
  25636. userBInit = 1;
  25637. #ifdef WOLFSSL_ECIES_OLD
  25638. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  25639. if (ret != 0)
  25640. ret = WC_TEST_RET_ENC_EC(ret);
  25641. }
  25642. if (ret == 0) {
  25643. userAInit = 1;
  25644. tmpKey = userA;
  25645. #else
  25646. tmpKey = NULL;
  25647. #endif
  25648. }
  25649. if (ret == 0) {
  25650. #ifdef WOLFSSL_QNX_CAAM
  25651. ret = wc_ecc_import_private_key_ex(privKey, sizeof(privKey), pubKey,
  25652. sizeof(pubKey), userB, ECC_SECP256R1);
  25653. #else
  25654. ret = wc_ecc_import_private_key_ex(privKey, sizeof(privKey), NULL, 0,
  25655. userB, ECC_SECP256R1);
  25656. #endif
  25657. if (ret != 0)
  25658. ret = WC_TEST_RET_ENC_EC(ret);
  25659. }
  25660. #ifdef WOLFSSL_ECIES_OLD
  25661. if (ret == 0) {
  25662. ret = wc_ecc_import_x963_ex(pubKey, sizeof(pubKey), userA,
  25663. ECC_SECP256R1);
  25664. if (ret != 0)
  25665. ret = WC_TEST_RET_ENC_EC(ret);
  25666. }
  25667. #endif
  25668. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  25669. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  25670. !defined(HAVE_SELFTEST)
  25671. if (ret == 0) {
  25672. ret = wc_ecc_set_rng(userB, rng);
  25673. if (ret != 0) {
  25674. ret = WC_TEST_RET_ENC_EC(ret);
  25675. }
  25676. }
  25677. #else
  25678. (void)rng;
  25679. #endif
  25680. if (ret == 0) {
  25681. ret = wc_ecc_decrypt(userB, tmpKey, enc_msg, sizeof(enc_msg), plain,
  25682. &plainSz, NULL);
  25683. if (ret != 0)
  25684. ret = WC_TEST_RET_ENC_EC(ret);
  25685. }
  25686. if (ret == 0) {
  25687. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  25688. ret = WC_TEST_RET_ENC_NC;
  25689. }
  25690. }
  25691. if (userBInit)
  25692. wc_ecc_free(userB);
  25693. #ifdef WOLFSSL_ECIES_OLD
  25694. if (userAInit)
  25695. wc_ecc_free(userA);
  25696. #endif
  25697. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25698. if (userB != NULL) {
  25699. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25700. }
  25701. #ifdef WOLFSSL_ECIES_OLD
  25702. if (userA != NULL) {
  25703. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25704. }
  25705. #endif
  25706. #endif
  25707. return ret;
  25708. }
  25709. #endif
  25710. static wc_test_ret_t ecc_encrypt_e2e_test(WC_RNG* rng, ecc_key* userA, ecc_key* userB,
  25711. byte encAlgo, byte kdfAlgo, byte macAlgo)
  25712. {
  25713. wc_test_ret_t ret = 0;
  25714. byte msg[48];
  25715. byte plain[48];
  25716. #ifdef WOLFSSL_ECIES_OLD
  25717. byte out[80];
  25718. #elif defined(WOLFSSL_ECIES_GEN_IV)
  25719. byte out[1 + ECC_KEYGEN_SIZE * 2 + 16 + 80];
  25720. #else
  25721. byte out[1 + ECC_KEYGEN_SIZE * 2 + 80];
  25722. #endif
  25723. word32 outSz = sizeof(out);
  25724. word32 plainSz = sizeof(plain);
  25725. int i;
  25726. ecEncCtx* cliCtx = NULL;
  25727. ecEncCtx* srvCtx = NULL;
  25728. byte cliSalt[EXCHANGE_SALT_SZ];
  25729. byte srvSalt[EXCHANGE_SALT_SZ];
  25730. const byte* tmpSalt;
  25731. byte msg2[48];
  25732. byte plain2[48];
  25733. #ifdef WOLFSSL_ECIES_OLD
  25734. byte out2[80];
  25735. #elif defined(WOLFSSL_ECIES_GEN_IV)
  25736. byte out2[1 + ECC_KEYGEN_SIZE * 2 + 16 + 80];
  25737. #else
  25738. byte out2[1 + ECC_KEYGEN_SIZE * 2 + 80];
  25739. #endif
  25740. word32 outSz2 = sizeof(out2);
  25741. word32 plainSz2 = sizeof(plain2);
  25742. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25743. ecc_key *tmpKey = (ecc_key *)XMALLOC(sizeof(ecc_key), HEAP_HINT,
  25744. DYNAMIC_TYPE_TMP_BUFFER);
  25745. #else
  25746. ecc_key tmpKey[1];
  25747. #endif
  25748. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25749. if (tmpKey == NULL) {
  25750. ERROR_OUT(MEMORY_E, done);
  25751. }
  25752. #endif
  25753. ret = wc_ecc_init_ex(tmpKey, HEAP_HINT, devId);
  25754. if (ret != 0)
  25755. goto done;
  25756. /* set message to incrementing 0,1,2,etc... */
  25757. for (i = 0; i < (int)sizeof(msg); i++)
  25758. msg[i] = i;
  25759. /* encrypt msg to B */
  25760. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz, NULL);
  25761. if (ret != 0) {
  25762. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  25763. }
  25764. #ifdef WOLFSSL_ECIES_OLD
  25765. tmpKey->dp = userA->dp;
  25766. ret = wc_ecc_copy_point(&userA->pubkey, &tmpKey->pubkey);
  25767. if (ret != 0) {
  25768. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  25769. }
  25770. #endif
  25771. /* decrypt msg from A */
  25772. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, NULL);
  25773. if (ret != 0) {
  25774. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  25775. }
  25776. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  25777. ret = WC_TEST_RET_ENC_NC; goto done;
  25778. }
  25779. #ifndef WOLFSSL_ECIES_OLD
  25780. /* A decrypts msg (response) from B */
  25781. ret = wc_ecc_decrypt(userB, NULL, out, outSz, plain2, &plainSz2, NULL);
  25782. if (ret != 0)
  25783. goto done;
  25784. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  25785. ret = WC_TEST_RET_ENC_NC; goto done;
  25786. }
  25787. #endif
  25788. /* let's verify message exchange works, A is client, B is server */
  25789. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  25790. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  25791. if (cliCtx == NULL || srvCtx == NULL) {
  25792. ret = WC_TEST_RET_ENC_ERRNO; goto done;
  25793. }
  25794. ret = wc_ecc_ctx_set_algo(cliCtx, encAlgo, kdfAlgo, macAlgo);
  25795. if (ret != 0)
  25796. goto done;
  25797. ret = wc_ecc_ctx_set_algo(srvCtx, encAlgo, kdfAlgo, macAlgo);
  25798. if (ret != 0)
  25799. goto done;
  25800. /* get salt to send to peer */
  25801. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  25802. if (tmpSalt == NULL) {
  25803. ret = WC_TEST_RET_ENC_NC; goto done;
  25804. }
  25805. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  25806. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  25807. if (tmpSalt == NULL) {
  25808. ret = WC_TEST_RET_ENC_NC; goto done;
  25809. }
  25810. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  25811. /* in actual use, we'd get the peer's salt over the transport */
  25812. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  25813. if (ret != 0)
  25814. goto done;
  25815. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  25816. if (ret != 0)
  25817. goto done;
  25818. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 11);
  25819. if (ret != 0)
  25820. goto done;
  25821. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 11);
  25822. if (ret != 0)
  25823. goto done;
  25824. /* get encrypted msg (request) to send to B */
  25825. outSz = sizeof(out);
  25826. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz,cliCtx);
  25827. if (ret != 0)
  25828. goto done;
  25829. #ifndef WOLFSSL_ECIES_OLD
  25830. wc_ecc_free(tmpKey);
  25831. #endif
  25832. /* B decrypts msg (request) from A */
  25833. plainSz = sizeof(plain);
  25834. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, srvCtx);
  25835. if (ret != 0)
  25836. goto done;
  25837. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  25838. ret = WC_TEST_RET_ENC_NC; goto done;
  25839. }
  25840. /* msg2 (response) from B to A */
  25841. for (i = 0; i < (int)sizeof(msg2); i++)
  25842. msg2[i] = i + sizeof(msg2);
  25843. /* get encrypted msg (response) to send to B */
  25844. ret = wc_ecc_encrypt(userB, userA, msg2, sizeof(msg2), out2,
  25845. &outSz2, srvCtx);
  25846. if (ret != 0)
  25847. goto done;
  25848. #ifdef WOLFSSL_ECIES_OLD
  25849. tmpKey->dp = userB->dp;
  25850. ret = wc_ecc_copy_point(&userB->pubkey, &tmpKey->pubkey);
  25851. if (ret != 0) {
  25852. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  25853. }
  25854. #else
  25855. wc_ecc_free(tmpKey);
  25856. #endif
  25857. /* A decrypts msg (response) from B */
  25858. ret = wc_ecc_decrypt(userA, tmpKey, out2, outSz2, plain2, &plainSz2,
  25859. cliCtx);
  25860. if (ret != 0)
  25861. goto done;
  25862. if (XMEMCMP(plain2, msg2, sizeof(msg2)) != 0) {
  25863. ret = WC_TEST_RET_ENC_NC; goto done;
  25864. }
  25865. #if defined(HAVE_COMP_KEY) && \
  25866. (! defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  25867. /* Create new client and server contexts. */
  25868. wc_ecc_ctx_free(srvCtx);
  25869. wc_ecc_ctx_free(cliCtx);
  25870. /* let's verify message exchange works, A is client, B is server */
  25871. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  25872. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  25873. if (cliCtx == NULL || srvCtx == NULL) {
  25874. ret = WC_TEST_RET_ENC_ERRNO; goto done;
  25875. }
  25876. ret = wc_ecc_ctx_set_algo(cliCtx, encAlgo, kdfAlgo, macAlgo);
  25877. if (ret != 0)
  25878. goto done;
  25879. ret = wc_ecc_ctx_set_algo(srvCtx, encAlgo, kdfAlgo, macAlgo);
  25880. if (ret != 0)
  25881. goto done;
  25882. /* get salt to send to peer */
  25883. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  25884. if (tmpSalt == NULL) {
  25885. ret = WC_TEST_RET_ENC_NC; goto done;
  25886. }
  25887. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  25888. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  25889. if (tmpSalt == NULL) {
  25890. ret = WC_TEST_RET_ENC_NC; goto done;
  25891. }
  25892. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  25893. /* in actual use, we'd get the peer's salt over the transport */
  25894. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  25895. if (ret != 0)
  25896. goto done;
  25897. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  25898. if (ret != 0)
  25899. goto done;
  25900. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 12);
  25901. if (ret != 0)
  25902. goto done;
  25903. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 12);
  25904. if (ret != 0)
  25905. goto done;
  25906. /* get encrypted msg (request) to send to B - compressed public key */
  25907. outSz = sizeof(out);
  25908. ret = wc_ecc_encrypt_ex(userA, userB, msg, sizeof(msg), out, &outSz, cliCtx,
  25909. 1);
  25910. if (ret != 0)
  25911. goto done;
  25912. #ifndef WOLFSSL_ECIES_OLD
  25913. wc_ecc_free(tmpKey);
  25914. #endif
  25915. /* B decrypts msg (request) from A - out has a compressed public key */
  25916. plainSz = sizeof(plain);
  25917. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, srvCtx);
  25918. if (ret != 0)
  25919. goto done;
  25920. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  25921. ret = WC_TEST_RET_ENC_NC; goto done;
  25922. }
  25923. #endif /* HAVE_COMP_KEY && (!FIPS || FIPS>=5.3) */
  25924. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  25925. (ECC_MIN_KEY_SZ <= 256) && defined(WOLFSSL_AES_128)
  25926. ret = ecc_encrypt_kat(rng);
  25927. #endif
  25928. done:
  25929. /* cleanup */
  25930. wc_ecc_ctx_free(srvCtx);
  25931. wc_ecc_ctx_free(cliCtx);
  25932. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25933. if (tmpKey != NULL) {
  25934. wc_ecc_free(tmpKey);
  25935. XFREE(tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25936. }
  25937. #else
  25938. wc_ecc_free(tmpKey);
  25939. #endif
  25940. return ret;
  25941. }
  25942. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  25943. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_encrypt_test(void)
  25944. {
  25945. WC_RNG rng;
  25946. wc_test_ret_t ret;
  25947. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25948. ecc_key *userA;
  25949. ecc_key *userB;
  25950. #else
  25951. ecc_key userA[1];
  25952. ecc_key userB[1];
  25953. #endif
  25954. #ifndef HAVE_FIPS
  25955. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  25956. #else
  25957. ret = wc_InitRng(&rng);
  25958. #endif
  25959. if (ret != 0)
  25960. return WC_TEST_RET_ENC_EC(ret);
  25961. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25962. userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT,
  25963. DYNAMIC_TYPE_TMP_BUFFER);
  25964. userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT,
  25965. DYNAMIC_TYPE_TMP_BUFFER);
  25966. if ((userA == NULL) || (userB == NULL)) {
  25967. ERROR_OUT(MEMORY_E, done);
  25968. }
  25969. #endif
  25970. XMEMSET(userA, 0, sizeof *userA);
  25971. XMEMSET(userB, 0, sizeof *userB);
  25972. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  25973. if (ret != 0)
  25974. goto done;
  25975. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  25976. if (ret != 0)
  25977. goto done;
  25978. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userA);
  25979. #if defined(WOLFSSL_ASYNC_CRYPT)
  25980. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  25981. #endif
  25982. if (ret != 0){
  25983. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  25984. }
  25985. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userB);
  25986. #if defined(WOLFSSL_ASYNC_CRYPT)
  25987. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  25988. #endif
  25989. if (ret != 0){
  25990. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  25991. }
  25992. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  25993. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  25994. !defined(HAVE_SELFTEST)
  25995. ret = wc_ecc_set_rng(userA, &rng);
  25996. if (ret != 0) {
  25997. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  25998. }
  25999. ret = wc_ecc_set_rng(userB, &rng);
  26000. if (ret != 0) {
  26001. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  26002. }
  26003. #endif
  26004. #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))
  26005. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  26006. #ifdef WOLFSSL_AES_128
  26007. if (ret == 0) {
  26008. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CBC,
  26009. ecHKDF_SHA256, ecHMAC_SHA256);
  26010. if (ret != 0) {
  26011. printf("ECIES: AES_128_CBC, HKDF_SHA256, HMAC_SHA256\n");
  26012. }
  26013. }
  26014. #endif
  26015. #ifdef WOLFSSL_AES_256
  26016. if (ret == 0) {
  26017. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_256_CBC,
  26018. ecHKDF_SHA256, ecHMAC_SHA256);
  26019. if (ret != 0) {
  26020. printf("ECIES: AES_256_CBC, HKDF_SHA256, HMAC_SHA256\n");
  26021. }
  26022. }
  26023. #endif
  26024. #endif
  26025. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER)
  26026. #ifdef WOLFSSL_AES_128
  26027. if (ret == 0) {
  26028. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CTR,
  26029. ecHKDF_SHA256, ecHMAC_SHA256);
  26030. if (ret != 0) {
  26031. printf("ECIES: AES_128_CTR, HKDF_SHA256, HMAC_SHA256\n");
  26032. }
  26033. }
  26034. #endif
  26035. #ifdef WOLFSSL_AES_256
  26036. if (ret == 0) {
  26037. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_256_CTR,
  26038. ecHKDF_SHA256, ecHMAC_SHA256);
  26039. if (ret != 0) {
  26040. printf("ECIES: AES_256_CTR, HKDF_SHA256, HMAC_SHA256\n");
  26041. }
  26042. }
  26043. #endif
  26044. #endif /* !NO_AES && WOLFSSL_AES_COUNTER */
  26045. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  26046. if (ret == 0) {
  26047. ret = ecc_ctx_kdf_salt_test(&rng, userA, userB);
  26048. }
  26049. #endif
  26050. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  26051. done:
  26052. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26053. if (userA != NULL) {
  26054. wc_ecc_free(userA);
  26055. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26056. }
  26057. if (userB != NULL) {
  26058. wc_ecc_free(userB);
  26059. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26060. }
  26061. #else
  26062. wc_ecc_free(userB);
  26063. wc_ecc_free(userA);
  26064. #endif
  26065. wc_FreeRng(&rng);
  26066. return ret;
  26067. }
  26068. #endif /* HAVE_ECC_ENCRYPT && HAVE_AES_CBC && WOLFSSL_AES_128 */
  26069. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  26070. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  26071. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  26072. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ECC_SECP)
  26073. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ecc_test_buffers(void)
  26074. {
  26075. size_t bytes;
  26076. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26077. ecc_key *cliKey = (ecc_key *)XMALLOC(sizeof *cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26078. ecc_key *servKey = (ecc_key *)XMALLOC(sizeof *servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26079. ecc_key *tmpKey = (ecc_key *)XMALLOC(sizeof *tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26080. #else
  26081. ecc_key cliKey[1];
  26082. ecc_key servKey[1];
  26083. ecc_key tmpKey[1];
  26084. #endif
  26085. WC_RNG rng;
  26086. word32 idx = 0;
  26087. wc_test_ret_t ret;
  26088. /* pad our test message to 32 bytes so evenly divisible by AES_BLOCK_SZ */
  26089. byte in[] = "Everyone gets Friday off. ecc p";
  26090. word32 inLen = (word32)XSTRLEN((char*)in);
  26091. byte out[256];
  26092. byte plain[256];
  26093. int verify = 0;
  26094. word32 x;
  26095. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26096. if ((cliKey == NULL) || (servKey == NULL) || (tmpKey == NULL))
  26097. ERROR_OUT(MEMORY_E, done);
  26098. #endif
  26099. ret = wc_ecc_init_ex(cliKey, HEAP_HINT, devId);
  26100. if (ret != 0)
  26101. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26102. ret = wc_ecc_init_ex(servKey, HEAP_HINT, devId);
  26103. if (ret != 0)
  26104. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26105. ret = wc_ecc_init_ex(tmpKey, HEAP_HINT, devId);
  26106. if (ret != 0)
  26107. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26108. bytes = (size_t)sizeof_ecc_clikey_der_256;
  26109. /* place client key into ecc_key struct cliKey */
  26110. ret = wc_EccPrivateKeyDecode(ecc_clikey_der_256, &idx, cliKey,
  26111. (word32)bytes);
  26112. if (ret != 0)
  26113. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26114. idx = 0;
  26115. bytes = (size_t)sizeof_ecc_key_der_256;
  26116. /* place server key into ecc_key struct servKey */
  26117. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, servKey,
  26118. (word32)bytes);
  26119. if (ret != 0)
  26120. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26121. #ifndef WC_NO_RNG
  26122. #ifndef HAVE_FIPS
  26123. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  26124. #else
  26125. ret = wc_InitRng(&rng);
  26126. #endif
  26127. if (ret != 0)
  26128. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26129. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  26130. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  26131. !defined(HAVE_SELFTEST)
  26132. ret = wc_ecc_set_rng(cliKey, &rng);
  26133. if (ret != 0)
  26134. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26135. ret = wc_ecc_set_rng(servKey, &rng);
  26136. if (ret != 0)
  26137. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26138. #endif
  26139. #endif /* !WC_NO_RNG */
  26140. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_HKDF) && \
  26141. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  26142. {
  26143. word32 y;
  26144. /* test encrypt and decrypt if they're available */
  26145. x = sizeof(out);
  26146. ret = wc_ecc_encrypt(cliKey, servKey, in, sizeof(in), out, &x, NULL);
  26147. if (ret < 0)
  26148. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26149. #ifdef WOLFSSL_ECIES_OLD
  26150. tmpKey->dp = cliKey->dp;
  26151. ret = wc_ecc_copy_point(&cliKey->pubkey, &tmpKey->pubkey);
  26152. if (ret != 0) {
  26153. ret = WC_TEST_RET_ENC_EC(ret); goto done;
  26154. }
  26155. #endif
  26156. y = sizeof(plain);
  26157. ret = wc_ecc_decrypt(servKey, tmpKey, out, x, plain, &y, NULL);
  26158. if (ret < 0)
  26159. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26160. if (XMEMCMP(plain, in, inLen))
  26161. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26162. }
  26163. #endif
  26164. x = sizeof(out);
  26165. do {
  26166. #if defined(WOLFSSL_ASYNC_CRYPT)
  26167. ret = wc_AsyncWait(ret, &cliKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26168. #endif
  26169. if (ret == 0)
  26170. ret = wc_ecc_sign_hash(in, inLen, out, &x, &rng, cliKey);
  26171. } while (ret == WC_PENDING_E);
  26172. if (ret < 0)
  26173. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26174. TEST_SLEEP();
  26175. XMEMSET(plain, 0, sizeof(plain));
  26176. do {
  26177. #if defined(WOLFSSL_ASYNC_CRYPT)
  26178. ret = wc_AsyncWait(ret, &cliKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  26179. #endif
  26180. if (ret == 0)
  26181. ret = wc_ecc_verify_hash(out, x, in, inLen, &verify,
  26182. cliKey);
  26183. } while (ret == WC_PENDING_E);
  26184. if (ret < 0)
  26185. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26186. if (verify != 1)
  26187. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26188. TEST_SLEEP();
  26189. #ifdef WOLFSSL_CERT_EXT
  26190. idx = 0;
  26191. bytes = sizeof_ecc_clikeypub_der_256;
  26192. ret = wc_EccPublicKeyDecode(ecc_clikeypub_der_256, &idx, cliKey,
  26193. (word32) bytes);
  26194. if (ret != 0)
  26195. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26196. #endif
  26197. ret = 0;
  26198. done:
  26199. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26200. if (cliKey != NULL) {
  26201. wc_ecc_free(cliKey);
  26202. XFREE(cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26203. }
  26204. if (servKey != NULL) {
  26205. wc_ecc_free(servKey);
  26206. XFREE(servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26207. }
  26208. if (tmpKey != NULL) {
  26209. wc_ecc_free(tmpKey);
  26210. XFREE(tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26211. }
  26212. #else
  26213. wc_ecc_free(cliKey);
  26214. wc_ecc_free(servKey);
  26215. wc_ecc_free(tmpKey);
  26216. #endif
  26217. wc_FreeRng(&rng);
  26218. return ret;
  26219. }
  26220. #endif /* USE_CERT_BUFFERS_256 && !WOLFSSL_ATECCX08A && !NO_ECC256 */
  26221. #endif /* HAVE_ECC */
  26222. #ifdef HAVE_CURVE25519
  26223. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  26224. defined(HAVE_CURVE25519_KEY_IMPORT)
  26225. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  26226. #define X25519_TEST_CNT 5
  26227. #else
  26228. #define X25519_TEST_CNT 1
  26229. #endif
  26230. static wc_test_ret_t curve25519_overflow_test(void)
  26231. {
  26232. /* secret key for party a */
  26233. byte sa[X25519_TEST_CNT][32] = {
  26234. {
  26235. 0x8d,0xaf,0x6e,0x7a,0xc1,0xeb,0x8d,0x30,
  26236. 0x99,0x86,0xd3,0x90,0x47,0x96,0x21,0x3c,
  26237. 0x3a,0x75,0xc0,0x7b,0x75,0x01,0x75,0xa3,
  26238. 0x81,0x4b,0xff,0x5a,0xbc,0x96,0x87,0x28
  26239. },
  26240. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  26241. {
  26242. 0x9d,0x63,0x5f,0xce,0xe2,0xe8,0xd7,0xfb,
  26243. 0x68,0x77,0x0e,0x44,0xd1,0xad,0x87,0x2b,
  26244. 0xf4,0x65,0x06,0xb7,0xbb,0xdb,0xbe,0x6e,
  26245. 0x02,0x43,0x24,0xc7,0x3d,0x7b,0x88,0x60
  26246. },
  26247. {
  26248. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  26249. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  26250. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  26251. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  26252. },
  26253. {
  26254. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  26255. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  26256. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  26257. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  26258. },
  26259. {
  26260. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  26261. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  26262. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  26263. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  26264. }
  26265. #endif
  26266. };
  26267. /* public key for party b */
  26268. byte pb[X25519_TEST_CNT][32] = {
  26269. {
  26270. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  26271. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  26272. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  26273. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  26274. },
  26275. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  26276. {
  26277. /* 0xff first byte in original - invalid! */
  26278. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  26279. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  26280. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  26281. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  26282. },
  26283. {
  26284. 0x36,0x1a,0x74,0x87,0x28,0x59,0xe0,0xb6,
  26285. 0xe4,0x2b,0x17,0x9b,0x16,0xb0,0x3b,0xf8,
  26286. 0xb8,0x9f,0x2a,0x8f,0xc5,0x33,0x68,0x4f,
  26287. 0xde,0x4d,0xd8,0x80,0x63,0xe7,0xb4,0x0a
  26288. },
  26289. {
  26290. 0x00,0x80,0x38,0x59,0x19,0x3a,0x66,0x12,
  26291. 0xfd,0xa1,0xec,0x1c,0x40,0x84,0x40,0xbd,
  26292. 0x64,0x10,0x8b,0x53,0x81,0x21,0x03,0x2d,
  26293. 0x7d,0x33,0xb4,0x01,0x57,0x0d,0xe1,0x89
  26294. },
  26295. {
  26296. 0x1d,0xf8,0xf8,0x33,0x89,0x6c,0xb7,0xba,
  26297. 0x94,0x73,0xfa,0xc2,0x36,0xac,0xbe,0x49,
  26298. 0xaf,0x85,0x3e,0x93,0x5f,0xae,0xb2,0xc0,
  26299. 0xc8,0x80,0x8f,0x4a,0xaa,0xd3,0x55,0x2b
  26300. }
  26301. #endif
  26302. };
  26303. /* expected shared key */
  26304. byte ss[X25519_TEST_CNT][32] = {
  26305. {
  26306. 0x5c,0x4c,0x85,0x5f,0xfb,0x20,0x38,0xcc,
  26307. 0x55,0x16,0x5b,0x8a,0xa7,0xed,0x57,0x6e,
  26308. 0x35,0xaa,0x71,0x67,0x85,0x1f,0xb6,0x28,
  26309. 0x17,0x07,0x7b,0xda,0x76,0xdd,0xe0,0xb4
  26310. },
  26311. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  26312. {
  26313. 0x33,0xf6,0xc1,0x34,0x62,0x92,0x06,0x02,
  26314. 0x95,0xdb,0x91,0x4c,0x5d,0x52,0x54,0xc7,
  26315. 0xd2,0x5b,0x24,0xb5,0x4f,0x33,0x59,0x79,
  26316. 0x9f,0x6d,0x7e,0x4a,0x4c,0x30,0xd6,0x38
  26317. },
  26318. {
  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,0x00,
  26322. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02
  26323. },
  26324. {
  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,0x00,
  26328. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09
  26329. },
  26330. {
  26331. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26332. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26333. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26334. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10
  26335. }
  26336. #endif
  26337. };
  26338. wc_test_ret_t ret = 0;
  26339. int i;
  26340. word32 y;
  26341. byte shared[32];
  26342. curve25519_key userA;
  26343. wc_curve25519_init_ex(&userA, HEAP_HINT, devId);
  26344. for (i = 0; i < X25519_TEST_CNT; i++) {
  26345. if (wc_curve25519_import_private_raw(sa[i], sizeof(sa[i]), pb[i],
  26346. sizeof(pb[i]), &userA) != 0) {
  26347. ret = WC_TEST_RET_ENC_I(i); break;
  26348. }
  26349. /* test against known test vector */
  26350. XMEMSET(shared, 0, sizeof(shared));
  26351. y = sizeof(shared);
  26352. if (wc_curve25519_shared_secret(&userA, &userA, shared, &y) != 0) {
  26353. ret = WC_TEST_RET_ENC_I(i); break;
  26354. }
  26355. if (XMEMCMP(ss[i], shared, y)) {
  26356. ret = WC_TEST_RET_ENC_I(i); break;
  26357. }
  26358. }
  26359. wc_curve25519_free(&userA);
  26360. return ret;
  26361. }
  26362. /* Test the wc_curve25519_check_public API.
  26363. *
  26364. * returns 0 on success and -ve on failure.
  26365. */
  26366. static wc_test_ret_t curve25519_check_public_test(void)
  26367. {
  26368. wc_test_ret_t ret;
  26369. /* Little-endian values that will fail */
  26370. byte fail_le[][CURVE25519_KEYSIZE] = {
  26371. {
  26372. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26373. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26374. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26375. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  26376. },
  26377. {
  26378. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26379. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26380. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26381. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  26382. },
  26383. {
  26384. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26385. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26386. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26387. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x81
  26388. },
  26389. };
  26390. /* Big-endian values that will fail */
  26391. byte fail_be[][CURVE25519_KEYSIZE] = {
  26392. {
  26393. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26394. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26395. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26396. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  26397. },
  26398. {
  26399. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26400. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26401. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26402. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  26403. },
  26404. {
  26405. 0x81,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26406. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26407. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26408. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  26409. },
  26410. };
  26411. /* Good or valid public value */
  26412. byte good[CURVE25519_KEYSIZE] = {
  26413. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26414. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26415. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  26416. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  26417. };
  26418. int i;
  26419. /* Parameter checks */
  26420. /* NULL pointer */
  26421. ret = wc_curve25519_check_public(NULL, 0, EC25519_LITTLE_ENDIAN);
  26422. if (ret != BAD_FUNC_ARG) {
  26423. return WC_TEST_RET_ENC_EC(ret);
  26424. }
  26425. ret = wc_curve25519_check_public(NULL, 0, EC25519_BIG_ENDIAN);
  26426. if (ret != BAD_FUNC_ARG) {
  26427. return WC_TEST_RET_ENC_EC(ret);
  26428. }
  26429. /* Length of 0 treated differently to other invalid lengths for TLS */
  26430. ret = wc_curve25519_check_public(good, 0, EC25519_LITTLE_ENDIAN);
  26431. if (ret != BUFFER_E)
  26432. return WC_TEST_RET_ENC_EC(ret);
  26433. ret = wc_curve25519_check_public(good, 0, EC25519_BIG_ENDIAN);
  26434. if (ret != BUFFER_E)
  26435. return WC_TEST_RET_ENC_EC(ret);
  26436. /* Length not CURVE25519_KEYSIZE */
  26437. for (i = 1; i < CURVE25519_KEYSIZE + 2; i++) {
  26438. if (i == CURVE25519_KEYSIZE)
  26439. continue;
  26440. if (wc_curve25519_check_public(good, i, EC25519_LITTLE_ENDIAN) !=
  26441. ECC_BAD_ARG_E) {
  26442. return WC_TEST_RET_ENC_I(i);
  26443. }
  26444. if (wc_curve25519_check_public(good, i, EC25519_BIG_ENDIAN) !=
  26445. ECC_BAD_ARG_E) {
  26446. return WC_TEST_RET_ENC_I(i);
  26447. }
  26448. }
  26449. /* Little-endian fail cases */
  26450. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  26451. if (wc_curve25519_check_public(fail_le[i], CURVE25519_KEYSIZE,
  26452. EC25519_LITTLE_ENDIAN) == 0) {
  26453. return WC_TEST_RET_ENC_I(i);
  26454. }
  26455. }
  26456. /* Big-endian fail cases */
  26457. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  26458. if (wc_curve25519_check_public(fail_be[i], CURVE25519_KEYSIZE,
  26459. EC25519_BIG_ENDIAN) == 0) {
  26460. return WC_TEST_RET_ENC_I(i);
  26461. }
  26462. }
  26463. /* Check a valid public value works! */
  26464. ret = wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  26465. EC25519_LITTLE_ENDIAN);
  26466. if (ret != 0) {
  26467. return WC_TEST_RET_ENC_EC(ret);
  26468. }
  26469. ret = wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  26470. EC25519_BIG_ENDIAN);
  26471. if (ret != 0) {
  26472. return WC_TEST_RET_ENC_EC(ret);
  26473. }
  26474. return 0;
  26475. }
  26476. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  26477. #if !defined(NO_ASN) && defined(HAVE_CURVE25519_KEY_EXPORT) && \
  26478. defined(HAVE_CURVE25519_KEY_IMPORT)
  26479. static wc_test_ret_t curve255519_der_test(void)
  26480. {
  26481. wc_test_ret_t ret = 0;
  26482. /* certs/statickeys/x25519.der */
  26483. const byte kCurve25519PrivDer[] = {
  26484. 0x30, 0x2E, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x6E,
  26485. 0x04, 0x22, 0x04, 0x20, 0x78, 0x8E, 0x31, 0x5C, 0x33, 0xA9, 0x19, 0xC0,
  26486. 0x5E, 0x36, 0x70, 0x1B, 0xA4, 0xE8, 0xEF, 0xC1, 0x89, 0x8C, 0xB3, 0x15,
  26487. 0xC6, 0x79, 0xD3, 0xAC, 0x22, 0x00, 0xAE, 0xFA, 0xB3, 0xB7, 0x0F, 0x78
  26488. };
  26489. /* certs/statickeys/x25519-pub.der */
  26490. const byte kCurve25519PubDer[] = {
  26491. 0x30, 0x2A, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x6E, 0x03, 0x21, 0x00,
  26492. 0x09, 0xBC, 0x8C, 0xC7, 0x45, 0x0D, 0xC1, 0xC2, 0x02, 0x57, 0x9A, 0x68,
  26493. 0x3A, 0xFD, 0x7A, 0xA8, 0xA5, 0x2F, 0xF0, 0x99, 0x39, 0x98, 0xEA, 0x26,
  26494. 0xA2, 0x5B, 0x38, 0xFD, 0x96, 0xDB, 0x2A, 0x26
  26495. };
  26496. curve25519_key key;
  26497. byte output[128];
  26498. word32 outputSz = 128;
  26499. word32 idx;
  26500. ret = wc_curve25519_init_ex(&key, HEAP_HINT, devId);
  26501. if (ret != 0)
  26502. return WC_TEST_RET_ENC_EC(ret);
  26503. /* Test decode / encode of Curve25519 private key only */
  26504. if (ret == 0) {
  26505. idx = 0;
  26506. ret = wc_Curve25519PrivateKeyDecode(kCurve25519PrivDer, &idx, &key,
  26507. (word32)sizeof(kCurve25519PrivDer));
  26508. if (ret < 0)
  26509. ret = WC_TEST_RET_ENC_EC(ret);
  26510. }
  26511. if (ret == 0) {
  26512. outputSz = (word32)sizeof(output);
  26513. ret = wc_Curve25519PrivateKeyToDer(&key, output, outputSz);
  26514. if (ret >= 0) {
  26515. outputSz = (word32)ret;
  26516. ret = 0;
  26517. }
  26518. else {
  26519. ret = WC_TEST_RET_ENC_EC(ret);
  26520. }
  26521. }
  26522. if (ret == 0 && (outputSz != (word32)sizeof(kCurve25519PrivDer) ||
  26523. XMEMCMP(output, kCurve25519PrivDer, outputSz) != 0)) {
  26524. ret = WC_TEST_RET_ENC_NC;
  26525. }
  26526. /* Test decode / encode of Curve25519 public key only */
  26527. if (ret == 0) {
  26528. idx = 0;
  26529. ret = wc_Curve25519PublicKeyDecode(kCurve25519PubDer, &idx, &key,
  26530. (word32)sizeof(kCurve25519PubDer));
  26531. if (ret < 0)
  26532. ret = WC_TEST_RET_ENC_EC(ret);
  26533. }
  26534. if (ret == 0) {
  26535. outputSz = (word32)sizeof(output);
  26536. ret = wc_Curve25519PublicKeyToDer(&key, output, outputSz, 1);
  26537. if (ret >= 0) {
  26538. outputSz = (word32)ret;
  26539. ret = 0;
  26540. }
  26541. else {
  26542. ret = WC_TEST_RET_ENC_EC(ret);
  26543. }
  26544. }
  26545. if (ret == 0 && (outputSz != (word32)sizeof(kCurve25519PubDer) ||
  26546. XMEMCMP(output, kCurve25519PubDer, outputSz) != 0)) {
  26547. ret = WC_TEST_RET_ENC_NC;
  26548. }
  26549. wc_curve25519_free(&key);
  26550. return ret;
  26551. }
  26552. #endif /* !NO_ASN && HAVE_CURVE25519_KEY_EXPORT && HAVE_CURVE25519_KEY_IMPORT */
  26553. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t curve25519_test(void)
  26554. {
  26555. WC_RNG rng;
  26556. wc_test_ret_t ret;
  26557. #ifdef HAVE_CURVE25519_SHARED_SECRET
  26558. byte sharedA[32];
  26559. byte sharedB[32];
  26560. word32 y;
  26561. #endif
  26562. #ifdef HAVE_CURVE25519_KEY_EXPORT
  26563. byte exportBuf[32];
  26564. #endif
  26565. word32 x = 0;
  26566. curve25519_key userA, userB, pubKey;
  26567. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  26568. defined(HAVE_CURVE25519_KEY_IMPORT)
  26569. /* test vectors from
  26570. https://tools.ietf.org/html/draft-josefsson-tls-curve25519-03
  26571. */
  26572. /* secret key for party a */
  26573. byte sa[] = {
  26574. 0x5A,0xC9,0x9F,0x33,0x63,0x2E,0x5A,0x76,
  26575. 0x8D,0xE7,0xE8,0x1B,0xF8,0x54,0xC2,0x7C,
  26576. 0x46,0xE3,0xFB,0xF2,0xAB,0xBA,0xCD,0x29,
  26577. 0xEC,0x4A,0xFF,0x51,0x73,0x69,0xC6,0x60
  26578. };
  26579. /* public key for party a */
  26580. byte pa[] = {
  26581. 0x05,0x7E,0x23,0xEA,0x9F,0x1C,0xBE,0x8A,
  26582. 0x27,0x16,0x8F,0x6E,0x69,0x6A,0x79,0x1D,
  26583. 0xE6,0x1D,0xD3,0xAF,0x7A,0xCD,0x4E,0xEA,
  26584. 0xCC,0x6E,0x7B,0xA5,0x14,0xFD,0xA8,0x63
  26585. };
  26586. /* secret key for party b */
  26587. byte sb[] = {
  26588. 0x47,0xDC,0x3D,0x21,0x41,0x74,0x82,0x0E,
  26589. 0x11,0x54,0xB4,0x9B,0xC6,0xCD,0xB2,0xAB,
  26590. 0xD4,0x5E,0xE9,0x58,0x17,0x05,0x5D,0x25,
  26591. 0x5A,0xA3,0x58,0x31,0xB7,0x0D,0x32,0x60
  26592. };
  26593. /* public key for party b */
  26594. byte pb[] = {
  26595. 0x6E,0xB8,0x9D,0xA9,0x19,0x89,0xAE,0x37,
  26596. 0xC7,0xEA,0xC7,0x61,0x8D,0x9E,0x5C,0x49,
  26597. 0x51,0xDB,0xA1,0xD7,0x3C,0x28,0x5A,0xE1,
  26598. 0xCD,0x26,0xA8,0x55,0x02,0x0E,0xEF,0x04
  26599. };
  26600. /* expected shared key */
  26601. byte ss[] = {
  26602. 0x61,0x45,0x0C,0xD9,0x8E,0x36,0x01,0x6B,
  26603. 0x58,0x77,0x6A,0x89,0x7A,0x9F,0x0A,0xEF,
  26604. 0x73,0x8B,0x99,0xF0,0x94,0x68,0xB8,0xD6,
  26605. 0xB8,0x51,0x11,0x84,0xD5,0x34,0x94,0xAB
  26606. };
  26607. #endif /* HAVE_CURVE25519_SHARED_SECRET */
  26608. (void)x;
  26609. #ifndef HAVE_FIPS
  26610. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  26611. #else
  26612. ret = wc_InitRng(&rng);
  26613. #endif
  26614. if (ret != 0)
  26615. return WC_TEST_RET_ENC_EC(ret);
  26616. wc_curve25519_init_ex(&userA, HEAP_HINT, devId);
  26617. wc_curve25519_init_ex(&userB, HEAP_HINT, devId);
  26618. wc_curve25519_init_ex(&pubKey, HEAP_HINT, devId);
  26619. /* make curve25519 keys */
  26620. ret = wc_curve25519_make_key(&rng, 32, &userA);
  26621. if (ret != 0)
  26622. return WC_TEST_RET_ENC_EC(ret);
  26623. ret = wc_curve25519_make_key(&rng, 32, &userB);
  26624. if (ret != 0)
  26625. return WC_TEST_RET_ENC_EC(ret);
  26626. #ifdef HAVE_CURVE25519_SHARED_SECRET
  26627. /* find shared secret key */
  26628. x = sizeof(sharedA);
  26629. if ((ret = wc_curve25519_shared_secret(&userA, &userB, sharedA, &x)) != 0) {
  26630. printf("wc_curve25519_shared_secret 1 failed\n");
  26631. return WC_TEST_RET_ENC_EC(ret);
  26632. }
  26633. y = sizeof(sharedB);
  26634. if ((ret = wc_curve25519_shared_secret(&userB, &userA, sharedB, &y)) != 0) {
  26635. printf("wc_curve25519_shared_secret 2 failed\n");
  26636. return WC_TEST_RET_ENC_EC(ret);
  26637. }
  26638. /* compare shared secret keys to test they are the same */
  26639. if (y != x)
  26640. return WC_TEST_RET_ENC_NC;
  26641. if (XMEMCMP(sharedA, sharedB, x))
  26642. return WC_TEST_RET_ENC_NC;
  26643. #endif
  26644. #ifdef HAVE_CURVE25519_KEY_EXPORT
  26645. /* export a public key and import it for another user */
  26646. x = sizeof(exportBuf);
  26647. ret = wc_curve25519_export_public(&userA, exportBuf, &x);
  26648. if (ret != 0)
  26649. return WC_TEST_RET_ENC_EC(ret);
  26650. #ifdef HAVE_CURVE25519_KEY_IMPORT
  26651. ret = wc_curve25519_import_public(exportBuf, x, &pubKey);
  26652. if (ret != 0)
  26653. return WC_TEST_RET_ENC_EC(ret);
  26654. #endif
  26655. #endif
  26656. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  26657. defined(HAVE_CURVE25519_KEY_IMPORT)
  26658. /* test shared key after importing a public key */
  26659. XMEMSET(sharedB, 0, sizeof(sharedB));
  26660. y = sizeof(sharedB);
  26661. if (wc_curve25519_shared_secret(&userB, &pubKey, sharedB, &y) != 0) {
  26662. return WC_TEST_RET_ENC_NC;
  26663. }
  26664. if (XMEMCMP(sharedA, sharedB, y))
  26665. return WC_TEST_RET_ENC_NC;
  26666. /* import RFC test vectors and compare shared key */
  26667. ret = wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa),
  26668. &userA);
  26669. if (ret != 0)
  26670. return WC_TEST_RET_ENC_EC(ret);
  26671. ret = wc_curve25519_import_private_raw(sb, sizeof(sb), pb, sizeof(pb),
  26672. &userB);
  26673. if (ret != 0)
  26674. return WC_TEST_RET_ENC_EC(ret);
  26675. /* test against known test vector */
  26676. XMEMSET(sharedB, 0, sizeof(sharedB));
  26677. y = sizeof(sharedB);
  26678. ret = wc_curve25519_shared_secret(&userA, &userB, sharedB, &y);
  26679. if (ret != 0)
  26680. return WC_TEST_RET_ENC_EC(ret);
  26681. if (XMEMCMP(ss, sharedB, y))
  26682. return WC_TEST_RET_ENC_NC;
  26683. /* test swapping roles of keys and generating same shared key */
  26684. XMEMSET(sharedB, 0, sizeof(sharedB));
  26685. y = sizeof(sharedB);
  26686. ret = wc_curve25519_shared_secret(&userB, &userA, sharedB, &y);
  26687. if (ret != 0)
  26688. return WC_TEST_RET_ENC_EC(ret);
  26689. if (XMEMCMP(ss, sharedB, y))
  26690. return WC_TEST_RET_ENC_NC;
  26691. /* test with 1 generated key and 1 from known test vector */
  26692. ret = wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa),
  26693. &userA);
  26694. if (ret != 0)
  26695. return WC_TEST_RET_ENC_EC(ret);
  26696. wc_curve25519_free(&userB);
  26697. wc_curve25519_init_ex(&userB, HEAP_HINT, devId);
  26698. ret = wc_curve25519_make_key(&rng, 32, &userB);
  26699. if (ret != 0)
  26700. return WC_TEST_RET_ENC_EC(ret);
  26701. x = sizeof(sharedA);
  26702. ret = wc_curve25519_shared_secret(&userA, &userB, sharedA, &x);
  26703. if (ret != 0)
  26704. return WC_TEST_RET_ENC_EC(ret);
  26705. y = sizeof(sharedB);
  26706. ret = wc_curve25519_shared_secret(&userB, &userA, sharedB, &y);
  26707. if (ret != 0)
  26708. return WC_TEST_RET_ENC_EC(ret);
  26709. /* compare shared secret keys to test they are the same */
  26710. if (y != x)
  26711. return WC_TEST_RET_ENC_NC;
  26712. if (XMEMCMP(sharedA, sharedB, x))
  26713. return WC_TEST_RET_ENC_NC;
  26714. ret = curve25519_overflow_test();
  26715. if (ret != 0)
  26716. return ret;
  26717. ret = curve25519_check_public_test();
  26718. if (ret != 0)
  26719. return ret;
  26720. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  26721. #if !defined(NO_ASN) && defined(HAVE_CURVE25519_KEY_EXPORT) && \
  26722. defined(HAVE_CURVE25519_KEY_IMPORT)
  26723. ret = curve255519_der_test();
  26724. if (ret != 0)
  26725. return ret;
  26726. #endif
  26727. /* clean up keys when done */
  26728. wc_curve25519_free(&pubKey);
  26729. wc_curve25519_free(&userB);
  26730. wc_curve25519_free(&userA);
  26731. wc_FreeRng(&rng);
  26732. return 0;
  26733. }
  26734. #endif /* HAVE_CURVE25519 */
  26735. #ifdef HAVE_ED25519
  26736. #ifdef WOLFSSL_TEST_CERT
  26737. static wc_test_ret_t ed25519_test_cert(void)
  26738. {
  26739. DecodedCert cert[2];
  26740. DecodedCert* serverCert = NULL;
  26741. DecodedCert* caCert = NULL;
  26742. #ifdef HAVE_ED25519_VERIFY
  26743. ed25519_key key;
  26744. ed25519_key* pubKey = NULL;
  26745. int verify;
  26746. #endif /* HAVE_ED25519_VERIFY */
  26747. wc_test_ret_t ret;
  26748. byte* tmp;
  26749. size_t bytes;
  26750. XFILE file;
  26751. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26752. if (tmp == NULL) {
  26753. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  26754. }
  26755. #ifdef USE_CERT_BUFFERS_256
  26756. XMEMCPY(tmp, ca_ed25519_cert, sizeof_ca_ed25519_cert);
  26757. bytes = sizeof_ca_ed25519_cert;
  26758. #elif !defined(NO_FILESYSTEM)
  26759. file = XFOPEN(caEd25519Cert, "rb");
  26760. if (file == NULL) {
  26761. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  26762. }
  26763. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  26764. XFCLOSE(file);
  26765. if (bytes == 0)
  26766. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  26767. #else
  26768. /* No certificate to use. */
  26769. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26770. #endif
  26771. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  26772. caCert = &cert[0];
  26773. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  26774. if (ret != 0)
  26775. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26776. #ifdef USE_CERT_BUFFERS_256
  26777. XMEMCPY(tmp, server_ed25519_cert, sizeof_server_ed25519_cert);
  26778. bytes = sizeof_server_ed25519_cert;
  26779. #elif !defined(NO_FILESYSTEM)
  26780. file = XFOPEN(serverEd25519Cert, "rb");
  26781. if (file == NULL) {
  26782. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  26783. }
  26784. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  26785. XFCLOSE(file);
  26786. if (bytes == 0)
  26787. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  26788. #else
  26789. /* No certificate to use. */
  26790. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  26791. #endif
  26792. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  26793. serverCert = &cert[1];
  26794. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  26795. if (ret != 0)
  26796. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26797. #ifdef HAVE_ED25519_VERIFY
  26798. ret = wc_ed25519_init(&key);
  26799. if (ret < 0)
  26800. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26801. pubKey = &key;
  26802. ret = wc_ed25519_import_public(caCert->publicKey, caCert->pubKeySize,
  26803. pubKey);
  26804. if (ret < 0)
  26805. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26806. ret = wc_ed25519_verify_msg(serverCert->signature, serverCert->sigLength,
  26807. serverCert->source + serverCert->certBegin,
  26808. serverCert->sigIndex - serverCert->certBegin,
  26809. &verify, pubKey);
  26810. if (ret < 0 || verify != 1)
  26811. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26812. #endif /* HAVE_ED25519_VERIFY */
  26813. done:
  26814. if (tmp != NULL)
  26815. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26816. #ifdef HAVE_ED25519_VERIFY
  26817. wc_ed25519_free(pubKey);
  26818. #endif /* HAVE_ED25519_VERIFY */
  26819. if (caCert != NULL)
  26820. FreeDecodedCert(caCert);
  26821. if (serverCert != NULL)
  26822. FreeDecodedCert(serverCert);
  26823. return ret;
  26824. }
  26825. static wc_test_ret_t ed25519_test_make_cert(void)
  26826. {
  26827. WC_RNG rng;
  26828. Cert cert;
  26829. DecodedCert decode;
  26830. ed25519_key key;
  26831. ed25519_key* privKey = NULL;
  26832. wc_test_ret_t ret = 0;
  26833. byte* tmp = NULL;
  26834. wc_InitCert_ex(&cert, HEAP_HINT, devId);
  26835. #ifndef HAVE_FIPS
  26836. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  26837. #else
  26838. ret = wc_InitRng(&rng);
  26839. #endif
  26840. if (ret != 0)
  26841. return WC_TEST_RET_ENC_EC(ret);
  26842. wc_ed25519_init(&key);
  26843. privKey = &key;
  26844. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, privKey);
  26845. cert.daysValid = 365 * 2;
  26846. cert.selfSigned = 1;
  26847. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  26848. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  26849. cert.isCA = 0;
  26850. #ifdef WOLFSSL_CERT_EXT
  26851. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  26852. if (ret < 0)
  26853. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26854. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  26855. if (ret < 0)
  26856. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26857. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  26858. if (ret < 0)
  26859. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26860. #endif
  26861. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26862. if (tmp == NULL) {
  26863. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  26864. }
  26865. cert.sigType = CTC_ED25519;
  26866. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED25519_TYPE, privKey, &rng);
  26867. if (ret < 0)
  26868. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26869. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF,
  26870. ED25519_TYPE, privKey, &rng);
  26871. if (ret < 0)
  26872. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26873. InitDecodedCert(&decode, tmp, (word32)ret, HEAP_HINT);
  26874. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  26875. FreeDecodedCert(&decode);
  26876. if (ret != 0)
  26877. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  26878. done:
  26879. if (tmp != NULL)
  26880. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26881. wc_ed25519_free(privKey);
  26882. wc_FreeRng(&rng);
  26883. return ret;
  26884. }
  26885. #endif /* WOLFSSL_TEST_CERT */
  26886. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  26887. defined(HAVE_ED25519_KEY_IMPORT)
  26888. static wc_test_ret_t ed25519ctx_test(void)
  26889. {
  26890. wc_test_ret_t ret;
  26891. byte out[ED25519_SIG_SIZE];
  26892. word32 outlen;
  26893. #ifdef HAVE_ED25519_VERIFY
  26894. int verify = 0;
  26895. #endif /* HAVE_ED25519_VERIFY */
  26896. ed25519_key key;
  26897. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  26898. 0x03,0x05,0x33,0x4e,0x38,0x1a,0xf7,0x8f,
  26899. 0x14,0x1c,0xb6,0x66,0xf6,0x19,0x9f,0x57,
  26900. 0xbc,0x34,0x95,0x33,0x5a,0x25,0x6a,0x95,
  26901. 0xbd,0x2a,0x55,0xbf,0x54,0x66,0x63,0xf6
  26902. };
  26903. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  26904. 0xdf,0xc9,0x42,0x5e,0x4f,0x96,0x8f,0x7f,
  26905. 0x0c,0x29,0xf0,0x25,0x9c,0xf5,0xf9,0xae,
  26906. 0xd6,0x85,0x1c,0x2b,0xb4,0xad,0x8b,0xfb,
  26907. 0x86,0x0c,0xfe,0xe0,0xab,0x24,0x82,0x92
  26908. };
  26909. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx1[] = {
  26910. 0x55,0xa4,0xcc,0x2f,0x70,0xa5,0x4e,0x04,
  26911. 0x28,0x8c,0x5f,0x4c,0xd1,0xe4,0x5a,0x7b,
  26912. 0xb5,0x20,0xb3,0x62,0x92,0x91,0x18,0x76,
  26913. 0xca,0xda,0x73,0x23,0x19,0x8d,0xd8,0x7a,
  26914. 0x8b,0x36,0x95,0x0b,0x95,0x13,0x00,0x22,
  26915. 0x90,0x7a,0x7f,0xb7,0xc4,0xe9,0xb2,0xd5,
  26916. 0xf6,0xcc,0xa6,0x85,0xa5,0x87,0xb4,0xb2,
  26917. 0x1f,0x4b,0x88,0x8e,0x4e,0x7e,0xdb,0x0d
  26918. };
  26919. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx2[] = {
  26920. 0xcc,0x5e,0x63,0xa2,0x7e,0x94,0xaf,0xd3,
  26921. 0x41,0x83,0x38,0xd2,0x48,0x6f,0xa9,0x2a,
  26922. 0xf9,0x91,0x7c,0x2d,0x98,0x9e,0x06,0xe5,
  26923. 0x02,0x77,0x72,0x1c,0x34,0x38,0x18,0xb4,
  26924. 0x21,0x96,0xbc,0x29,0x2e,0x68,0xf3,0x4d,
  26925. 0x85,0x9b,0xbe,0xad,0x17,0x9f,0x54,0x54,
  26926. 0x2d,0x4b,0x04,0xdc,0xfb,0xfa,0x4a,0x68,
  26927. 0x4e,0x39,0x50,0xfb,0x1c,0xcd,0x8d,0x0d
  26928. };
  26929. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  26930. 0xf7,0x26,0x93,0x6d,0x19,0xc8,0x00,0x49,
  26931. 0x4e,0x3f,0xda,0xff,0x20,0xb2,0x76,0xa8
  26932. };
  26933. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  26934. 0x66,0x6f,0x6f
  26935. };
  26936. outlen = sizeof(out);
  26937. XMEMSET(out, 0, sizeof(out));
  26938. ret = wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  26939. if (ret != 0)
  26940. return 10800;
  26941. ret = wc_ed25519_import_private_key(sKeyCtx, ED25519_KEY_SIZE, pKeyCtx,
  26942. sizeof(pKeyCtx), &key);
  26943. if (ret == 0)
  26944. ret = wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  26945. contextCtx, sizeof(contextCtx));
  26946. if (ret == 0 && XMEMCMP(out, sigCtx1, 64) != 0)
  26947. ret = WC_TEST_RET_ENC_NC;
  26948. #if defined(HAVE_ED25519_VERIFY)
  26949. /* test verify on good msg */
  26950. if (ret == 0)
  26951. ret = wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx),
  26952. &verify, &key, contextCtx, sizeof(contextCtx));
  26953. if (ret == 0 && verify != 1)
  26954. ret = WC_TEST_RET_ENC_NC;
  26955. #endif
  26956. if (ret == 0)
  26957. ret = wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  26958. NULL, 0);
  26959. if (ret == 0 && XMEMCMP(out, sigCtx2, 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_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx),
  26965. &verify, &key, NULL, 0);
  26966. if (ret == 0 && verify != 1)
  26967. ret = WC_TEST_RET_ENC_NC;
  26968. #endif
  26969. wc_ed25519_free(&key);
  26970. return ret;
  26971. }
  26972. static wc_test_ret_t ed25519ph_test(void)
  26973. {
  26974. wc_test_ret_t ret = 0;
  26975. byte out[ED25519_SIG_SIZE];
  26976. word32 outlen;
  26977. #ifdef HAVE_ED25519_VERIFY
  26978. int verify = 0;
  26979. #endif /* HAVE_ED25519_VERIFY */
  26980. ed25519_key key;
  26981. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  26982. 0x83,0x3f,0xe6,0x24,0x09,0x23,0x7b,0x9d,
  26983. 0x62,0xec,0x77,0x58,0x75,0x20,0x91,0x1e,
  26984. 0x9a,0x75,0x9c,0xec,0x1d,0x19,0x75,0x5b,
  26985. 0x7d,0xa9,0x01,0xb9,0x6d,0xca,0x3d,0x42
  26986. };
  26987. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  26988. 0xec,0x17,0x2b,0x93,0xad,0x5e,0x56,0x3b,
  26989. 0xf4,0x93,0x2c,0x70,0xe1,0x24,0x50,0x34,
  26990. 0xc3,0x54,0x67,0xef,0x2e,0xfd,0x4d,0x64,
  26991. 0xeb,0xf8,0x19,0x68,0x34,0x67,0xe2,0xbf
  26992. };
  26993. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  26994. 0x98,0xa7,0x02,0x22,0xf0,0xb8,0x12,0x1a,
  26995. 0xa9,0xd3,0x0f,0x81,0x3d,0x68,0x3f,0x80,
  26996. 0x9e,0x46,0x2b,0x46,0x9c,0x7f,0xf8,0x76,
  26997. 0x39,0x49,0x9b,0xb9,0x4e,0x6d,0xae,0x41,
  26998. 0x31,0xf8,0x50,0x42,0x46,0x3c,0x2a,0x35,
  26999. 0x5a,0x20,0x03,0xd0,0x62,0xad,0xf5,0xaa,
  27000. 0xa1,0x0b,0x8c,0x61,0xe6,0x36,0x06,0x2a,
  27001. 0xaa,0xd1,0x1c,0x2a,0x26,0x08,0x34,0x06
  27002. };
  27003. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  27004. 0xe0,0x39,0x70,0x2b,0x4c,0x25,0x95,0xa6,
  27005. 0xa5,0x41,0xac,0x85,0x09,0x23,0x6e,0x29,
  27006. 0x90,0x47,0x47,0x95,0x33,0x0c,0x9b,0x34,
  27007. 0xa7,0x5f,0x58,0xa6,0x60,0x12,0x9e,0x08,
  27008. 0xfd,0x73,0x69,0x43,0xfb,0x19,0x43,0xa5,
  27009. 0x57,0x20,0xb9,0xe0,0x95,0x7b,0x1e,0xd6,
  27010. 0x73,0x48,0x16,0x61,0x9f,0x13,0x88,0xf4,
  27011. 0x3f,0x73,0xe6,0xe3,0xba,0xa8,0x1c,0x0e
  27012. };
  27013. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  27014. 0x61,0x62,0x63
  27015. };
  27016. /* SHA-512 hash of msgPh */
  27017. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  27018. 0xdd,0xaf,0x35,0xa1,0x93,0x61,0x7a,0xba,
  27019. 0xcc,0x41,0x73,0x49,0xae,0x20,0x41,0x31,
  27020. 0x12,0xe6,0xfa,0x4e,0x89,0xa9,0x7e,0xa2,
  27021. 0x0a,0x9e,0xee,0xe6,0x4b,0x55,0xd3,0x9a,
  27022. 0x21,0x92,0x99,0x2a,0x27,0x4f,0xc1,0xa8,
  27023. 0x36,0xba,0x3c,0x23,0xa3,0xfe,0xeb,0xbd,
  27024. 0x45,0x4d,0x44,0x23,0x64,0x3c,0xe8,0x0e,
  27025. 0x2a,0x9a,0xc9,0x4f,0xa5,0x4c,0xa4,0x9f
  27026. };
  27027. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  27028. 0x66,0x6f,0x6f
  27029. };
  27030. outlen = sizeof(out);
  27031. XMEMSET(out, 0, sizeof(out));
  27032. ret = wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  27033. if (ret != 0)
  27034. return WC_TEST_RET_ENC_EC(ret);
  27035. ret = wc_ed25519_import_private_key(sKeyPh, ED25519_KEY_SIZE, pKeyPh,
  27036. sizeof(pKeyPh), &key);
  27037. if (ret == 0)
  27038. ret = wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  27039. NULL, 0);
  27040. if (ret == 0 && XMEMCMP(out, sigPh1, 64) != 0)
  27041. ret = WC_TEST_RET_ENC_NC;
  27042. #if defined(HAVE_ED25519_VERIFY)
  27043. /* test verify on good msg */
  27044. if (ret == 0)
  27045. ret = wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh),
  27046. &verify, &key, NULL, 0);
  27047. if (ret == 0 && verify != 1)
  27048. ret = WC_TEST_RET_ENC_NC;
  27049. #endif
  27050. if (ret == 0)
  27051. ret = wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  27052. contextPh2, sizeof(contextPh2));
  27053. if (ret == 0 && XMEMCMP(out, sigPh2, 64) != 0)
  27054. ret = WC_TEST_RET_ENC_NC;
  27055. #if defined(HAVE_ED25519_VERIFY)
  27056. /* test verify on good msg */
  27057. if (ret == 0)
  27058. ret = wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify,
  27059. &key, contextPh2, sizeof(contextPh2));
  27060. if (ret == 0 && verify != 1)
  27061. ret = WC_TEST_RET_ENC_NC;
  27062. #endif
  27063. if (ret == 0)
  27064. ret = wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  27065. NULL, 0);
  27066. if (ret == 0 && XMEMCMP(out, sigPh1, 64) != 0)
  27067. ret = WC_TEST_RET_ENC_NC;
  27068. #if defined(HAVE_ED25519_VERIFY)
  27069. if (ret == 0)
  27070. ret = wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh),
  27071. &verify, &key, NULL, 0);
  27072. if (ret == 0 && verify != 1)
  27073. ret = WC_TEST_RET_ENC_NC;
  27074. #endif
  27075. if (ret == 0)
  27076. ret = wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  27077. contextPh2, sizeof(contextPh2));
  27078. if (ret == 0 && XMEMCMP(out, sigPh2, 64) != 0)
  27079. ret = WC_TEST_RET_ENC_NC;
  27080. #if defined(HAVE_ED25519_VERIFY)
  27081. if (ret == 0)
  27082. ret = wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  27083. &key, contextPh2, sizeof(contextPh2));
  27084. if (ret == 0 && verify != 1)
  27085. ret = WC_TEST_RET_ENC_NC;
  27086. #endif
  27087. wc_ed25519_free(&key);
  27088. return ret;
  27089. }
  27090. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  27091. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ed25519_test(void)
  27092. {
  27093. wc_test_ret_t ret;
  27094. WC_RNG rng;
  27095. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  27096. defined(HAVE_ED25519_KEY_IMPORT)
  27097. byte out[ED25519_SIG_SIZE];
  27098. byte exportPKey[ED25519_KEY_SIZE];
  27099. byte exportSKey[ED25519_KEY_SIZE];
  27100. word32 exportPSz;
  27101. word32 exportSSz;
  27102. int i;
  27103. word32 outlen;
  27104. #ifdef HAVE_ED25519_VERIFY
  27105. #ifdef WOLFSSL_ED25519_STREAMING_VERIFY
  27106. int j;
  27107. #endif
  27108. int verify;
  27109. #endif /* HAVE_ED25519_VERIFY */
  27110. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  27111. word32 keySz, sigSz;
  27112. ed25519_key key;
  27113. ed25519_key key2;
  27114. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  27115. defined(HAVE_ED25519_KEY_IMPORT)
  27116. /* test vectors from
  27117. https://tools.ietf.org/html/draft-josefsson-eddsa-ed25519-02
  27118. */
  27119. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  27120. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  27121. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  27122. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  27123. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  27124. };
  27125. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  27126. 0x4c,0xcd,0x08,0x9b,0x28,0xff,0x96,0xda,
  27127. 0x9d,0xb6,0xc3,0x46,0xec,0x11,0x4e,0x0f,
  27128. 0x5b,0x8a,0x31,0x9f,0x35,0xab,0xa6,0x24,
  27129. 0xda,0x8c,0xf6,0xed,0x4f,0xb8,0xa6,0xfb
  27130. };
  27131. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  27132. 0xc5,0xaa,0x8d,0xf4,0x3f,0x9f,0x83,0x7b,
  27133. 0xed,0xb7,0x44,0x2f,0x31,0xdc,0xb7,0xb1,
  27134. 0x66,0xd3,0x85,0x35,0x07,0x6f,0x09,0x4b,
  27135. 0x85,0xce,0x3a,0x2e,0x0b,0x44,0x58,0xf7
  27136. };
  27137. /* uncompressed test */
  27138. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  27139. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  27140. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  27141. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  27142. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  27143. };
  27144. /* compressed prefix test */
  27145. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  27146. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  27147. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  27148. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  27149. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  27150. };
  27151. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  27152. 0xf5,0xe5,0x76,0x7c,0xf1,0x53,0x31,0x95,
  27153. 0x17,0x63,0x0f,0x22,0x68,0x76,0xb8,0x6c,
  27154. 0x81,0x60,0xcc,0x58,0x3b,0xc0,0x13,0x74,
  27155. 0x4c,0x6b,0xf2,0x55,0xf5,0xcc,0x0e,0xe5
  27156. };
  27157. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  27158. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  27159. 0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  27160. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  27161. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  27162. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  27163. };
  27164. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  27165. 0x3d,0x40,0x17,0xc3,0xe8,0x43,0x89,0x5a,
  27166. 0x92,0xb7,0x0a,0xa7,0x4d,0x1b,0x7e,0xbc,
  27167. 0x9c,0x98,0x2c,0xcf,0x2e,0xc4,0x96,0x8c,
  27168. 0xc0,0xcd,0x55,0xf1,0x2a,0xf4,0x66,0x0c
  27169. };
  27170. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  27171. 0xfc,0x51,0xcd,0x8e,0x62,0x18,0xa1,0xa3,
  27172. 0x8d,0xa4,0x7e,0xd0,0x02,0x30,0xf0,0x58,
  27173. 0x08,0x16,0xed,0x13,0xba,0x33,0x03,0xac,
  27174. 0x5d,0xeb,0x91,0x15,0x48,0x90,0x80,0x25
  27175. };
  27176. /* uncompressed test */
  27177. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  27178. 0x04,0x55,0xd0,0xe0,0x9a,0x2b,0x9d,0x34,
  27179. 0x29,0x22,0x97,0xe0,0x8d,0x60,0xd0,0xf6,
  27180. 0x20,0xc5,0x13,0xd4,0x72,0x53,0x18,0x7c,
  27181. 0x24,0xb1,0x27,0x86,0xbd,0x77,0x76,0x45,
  27182. 0xce,0x1a,0x51,0x07,0xf7,0x68,0x1a,0x02,
  27183. 0xaf,0x25,0x23,0xa6,0xda,0xf3,0x72,0xe1,
  27184. 0x0e,0x3a,0x07,0x64,0xc9,0xd3,0xfe,0x4b,
  27185. 0xd5,0xb7,0x0a,0xb1,0x82,0x01,0x98,0x5a,
  27186. 0xd7
  27187. };
  27188. /* compressed prefix */
  27189. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  27190. 0x40,0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  27191. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  27192. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  27193. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  27194. };
  27195. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  27196. 0x27,0x81,0x17,0xfc,0x14,0x4c,0x72,0x34,
  27197. 0x0f,0x67,0xd0,0xf2,0x31,0x6e,0x83,0x86,
  27198. 0xce,0xff,0xbf,0x2b,0x24,0x28,0xc9,0xc5,
  27199. 0x1f,0xef,0x7c,0x59,0x7f,0x1d,0x42,0x6e
  27200. };
  27201. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  27202. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  27203. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  27204. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  27205. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  27206. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  27207. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  27208. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  27209. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  27210. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  27211. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  27212. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  27213. };
  27214. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  27215. 0x92,0xa0,0x09,0xa9,0xf0,0xd4,0xca,0xb8,
  27216. 0x72,0x0e,0x82,0x0b,0x5f,0x64,0x25,0x40,
  27217. 0xa2,0xb2,0x7b,0x54,0x16,0x50,0x3f,0x8f,
  27218. 0xb3,0x76,0x22,0x23,0xeb,0xdb,0x69,0xda,
  27219. 0x08,0x5a,0xc1,0xe4,0x3e,0x15,0x99,0x6e,
  27220. 0x45,0x8f,0x36,0x13,0xd0,0xf1,0x1d,0x8c,
  27221. 0x38,0x7b,0x2e,0xae,0xb4,0x30,0x2a,0xee,
  27222. 0xb0,0x0d,0x29,0x16,0x12,0xbb,0x0c,0x00
  27223. };
  27224. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  27225. 0x62,0x91,0xd6,0x57,0xde,0xec,0x24,0x02,
  27226. 0x48,0x27,0xe6,0x9c,0x3a,0xbe,0x01,0xa3,
  27227. 0x0c,0xe5,0x48,0xa2,0x84,0x74,0x3a,0x44,
  27228. 0x5e,0x36,0x80,0xd7,0xdb,0x5a,0xc3,0xac,
  27229. 0x18,0xff,0x9b,0x53,0x8d,0x16,0xf2,0x90,
  27230. 0xae,0x67,0xf7,0x60,0x98,0x4d,0xc6,0x59,
  27231. 0x4a,0x7c,0x15,0xe9,0x71,0x6e,0xd2,0x8d,
  27232. 0xc0,0x27,0xbe,0xce,0xea,0x1e,0xc4,0x0a
  27233. };
  27234. /* uncompressed test */
  27235. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  27236. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  27237. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  27238. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  27239. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  27240. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  27241. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  27242. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  27243. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  27244. };
  27245. /* compressed prefix */
  27246. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  27247. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  27248. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  27249. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  27250. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  27251. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  27252. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  27253. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  27254. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  27255. };
  27256. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  27257. 0x0a,0xab,0x4c,0x90,0x05,0x01,0xb3,0xe2,
  27258. 0x4d,0x7c,0xdf,0x46,0x63,0x32,0x6a,0x3a,
  27259. 0x87,0xdf,0x5e,0x48,0x43,0xb2,0xcb,0xdb,
  27260. 0x67,0xcb,0xf6,0xe4,0x60,0xfe,0xc3,0x50,
  27261. 0xaa,0x53,0x71,0xb1,0x50,0x8f,0x9f,0x45,
  27262. 0x28,0xec,0xea,0x23,0xc4,0x36,0xd9,0x4b,
  27263. 0x5e,0x8f,0xcd,0x4f,0x68,0x1e,0x30,0xa6,
  27264. 0xac,0x00,0xa9,0x70,0x4a,0x18,0x8a,0x03
  27265. };
  27266. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  27267. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = {0x0 };
  27268. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = {0x72};
  27269. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = {0xAF,0x82};
  27270. /* test of a 1024 byte long message */
  27271. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  27272. 0x08,0xb8,0xb2,0xb7,0x33,0x42,0x42,0x43,
  27273. 0x76,0x0f,0xe4,0x26,0xa4,0xb5,0x49,0x08,
  27274. 0x63,0x21,0x10,0xa6,0x6c,0x2f,0x65,0x91,
  27275. 0xea,0xbd,0x33,0x45,0xe3,0xe4,0xeb,0x98,
  27276. 0xfa,0x6e,0x26,0x4b,0xf0,0x9e,0xfe,0x12,
  27277. 0xee,0x50,0xf8,0xf5,0x4e,0x9f,0x77,0xb1,
  27278. 0xe3,0x55,0xf6,0xc5,0x05,0x44,0xe2,0x3f,
  27279. 0xb1,0x43,0x3d,0xdf,0x73,0xbe,0x84,0xd8,
  27280. 0x79,0xde,0x7c,0x00,0x46,0xdc,0x49,0x96,
  27281. 0xd9,0xe7,0x73,0xf4,0xbc,0x9e,0xfe,0x57,
  27282. 0x38,0x82,0x9a,0xdb,0x26,0xc8,0x1b,0x37,
  27283. 0xc9,0x3a,0x1b,0x27,0x0b,0x20,0x32,0x9d,
  27284. 0x65,0x86,0x75,0xfc,0x6e,0xa5,0x34,0xe0,
  27285. 0x81,0x0a,0x44,0x32,0x82,0x6b,0xf5,0x8c,
  27286. 0x94,0x1e,0xfb,0x65,0xd5,0x7a,0x33,0x8b,
  27287. 0xbd,0x2e,0x26,0x64,0x0f,0x89,0xff,0xbc,
  27288. 0x1a,0x85,0x8e,0xfc,0xb8,0x55,0x0e,0xe3,
  27289. 0xa5,0xe1,0x99,0x8b,0xd1,0x77,0xe9,0x3a,
  27290. 0x73,0x63,0xc3,0x44,0xfe,0x6b,0x19,0x9e,
  27291. 0xe5,0xd0,0x2e,0x82,0xd5,0x22,0xc4,0xfe,
  27292. 0xba,0x15,0x45,0x2f,0x80,0x28,0x8a,0x82,
  27293. 0x1a,0x57,0x91,0x16,0xec,0x6d,0xad,0x2b,
  27294. 0x3b,0x31,0x0d,0xa9,0x03,0x40,0x1a,0xa6,
  27295. 0x21,0x00,0xab,0x5d,0x1a,0x36,0x55,0x3e,
  27296. 0x06,0x20,0x3b,0x33,0x89,0x0c,0xc9,0xb8,
  27297. 0x32,0xf7,0x9e,0xf8,0x05,0x60,0xcc,0xb9,
  27298. 0xa3,0x9c,0xe7,0x67,0x96,0x7e,0xd6,0x28,
  27299. 0xc6,0xad,0x57,0x3c,0xb1,0x16,0xdb,0xef,
  27300. 0xef,0xd7,0x54,0x99,0xda,0x96,0xbd,0x68,
  27301. 0xa8,0xa9,0x7b,0x92,0x8a,0x8b,0xbc,0x10,
  27302. 0x3b,0x66,0x21,0xfc,0xde,0x2b,0xec,0xa1,
  27303. 0x23,0x1d,0x20,0x6b,0xe6,0xcd,0x9e,0xc7,
  27304. 0xaf,0xf6,0xf6,0xc9,0x4f,0xcd,0x72,0x04,
  27305. 0xed,0x34,0x55,0xc6,0x8c,0x83,0xf4,0xa4,
  27306. 0x1d,0xa4,0xaf,0x2b,0x74,0xef,0x5c,0x53,
  27307. 0xf1,0xd8,0xac,0x70,0xbd,0xcb,0x7e,0xd1,
  27308. 0x85,0xce,0x81,0xbd,0x84,0x35,0x9d,0x44,
  27309. 0x25,0x4d,0x95,0x62,0x9e,0x98,0x55,0xa9,
  27310. 0x4a,0x7c,0x19,0x58,0xd1,0xf8,0xad,0xa5,
  27311. 0xd0,0x53,0x2e,0xd8,0xa5,0xaa,0x3f,0xb2,
  27312. 0xd1,0x7b,0xa7,0x0e,0xb6,0x24,0x8e,0x59,
  27313. 0x4e,0x1a,0x22,0x97,0xac,0xbb,0xb3,0x9d,
  27314. 0x50,0x2f,0x1a,0x8c,0x6e,0xb6,0xf1,0xce,
  27315. 0x22,0xb3,0xde,0x1a,0x1f,0x40,0xcc,0x24,
  27316. 0x55,0x41,0x19,0xa8,0x31,0xa9,0xaa,0xd6,
  27317. 0x07,0x9c,0xad,0x88,0x42,0x5d,0xe6,0xbd,
  27318. 0xe1,0xa9,0x18,0x7e,0xbb,0x60,0x92,0xcf,
  27319. 0x67,0xbf,0x2b,0x13,0xfd,0x65,0xf2,0x70,
  27320. 0x88,0xd7,0x8b,0x7e,0x88,0x3c,0x87,0x59,
  27321. 0xd2,0xc4,0xf5,0xc6,0x5a,0xdb,0x75,0x53,
  27322. 0x87,0x8a,0xd5,0x75,0xf9,0xfa,0xd8,0x78,
  27323. 0xe8,0x0a,0x0c,0x9b,0xa6,0x3b,0xcb,0xcc,
  27324. 0x27,0x32,0xe6,0x94,0x85,0xbb,0xc9,0xc9,
  27325. 0x0b,0xfb,0xd6,0x24,0x81,0xd9,0x08,0x9b,
  27326. 0xec,0xcf,0x80,0xcf,0xe2,0xdf,0x16,0xa2,
  27327. 0xcf,0x65,0xbd,0x92,0xdd,0x59,0x7b,0x07,
  27328. 0x07,0xe0,0x91,0x7a,0xf4,0x8b,0xbb,0x75,
  27329. 0xfe,0xd4,0x13,0xd2,0x38,0xf5,0x55,0x5a,
  27330. 0x7a,0x56,0x9d,0x80,0xc3,0x41,0x4a,0x8d,
  27331. 0x08,0x59,0xdc,0x65,0xa4,0x61,0x28,0xba,
  27332. 0xb2,0x7a,0xf8,0x7a,0x71,0x31,0x4f,0x31,
  27333. 0x8c,0x78,0x2b,0x23,0xeb,0xfe,0x80,0x8b,
  27334. 0x82,0xb0,0xce,0x26,0x40,0x1d,0x2e,0x22,
  27335. 0xf0,0x4d,0x83,0xd1,0x25,0x5d,0xc5,0x1a,
  27336. 0xdd,0xd3,0xb7,0x5a,0x2b,0x1a,0xe0,0x78,
  27337. 0x45,0x04,0xdf,0x54,0x3a,0xf8,0x96,0x9b,
  27338. 0xe3,0xea,0x70,0x82,0xff,0x7f,0xc9,0x88,
  27339. 0x8c,0x14,0x4d,0xa2,0xaf,0x58,0x42,0x9e,
  27340. 0xc9,0x60,0x31,0xdb,0xca,0xd3,0xda,0xd9,
  27341. 0xaf,0x0d,0xcb,0xaa,0xaf,0x26,0x8c,0xb8,
  27342. 0xfc,0xff,0xea,0xd9,0x4f,0x3c,0x7c,0xa4,
  27343. 0x95,0xe0,0x56,0xa9,0xb4,0x7a,0xcd,0xb7,
  27344. 0x51,0xfb,0x73,0xe6,0x66,0xc6,0xc6,0x55,
  27345. 0xad,0xe8,0x29,0x72,0x97,0xd0,0x7a,0xd1,
  27346. 0xba,0x5e,0x43,0xf1,0xbc,0xa3,0x23,0x01,
  27347. 0x65,0x13,0x39,0xe2,0x29,0x04,0xcc,0x8c,
  27348. 0x42,0xf5,0x8c,0x30,0xc0,0x4a,0xaf,0xdb,
  27349. 0x03,0x8d,0xda,0x08,0x47,0xdd,0x98,0x8d,
  27350. 0xcd,0xa6,0xf3,0xbf,0xd1,0x5c,0x4b,0x4c,
  27351. 0x45,0x25,0x00,0x4a,0xa0,0x6e,0xef,0xf8,
  27352. 0xca,0x61,0x78,0x3a,0xac,0xec,0x57,0xfb,
  27353. 0x3d,0x1f,0x92,0xb0,0xfe,0x2f,0xd1,0xa8,
  27354. 0x5f,0x67,0x24,0x51,0x7b,0x65,0xe6,0x14,
  27355. 0xad,0x68,0x08,0xd6,0xf6,0xee,0x34,0xdf,
  27356. 0xf7,0x31,0x0f,0xdc,0x82,0xae,0xbf,0xd9,
  27357. 0x04,0xb0,0x1e,0x1d,0xc5,0x4b,0x29,0x27,
  27358. 0x09,0x4b,0x2d,0xb6,0x8d,0x6f,0x90,0x3b,
  27359. 0x68,0x40,0x1a,0xde,0xbf,0x5a,0x7e,0x08,
  27360. 0xd7,0x8f,0xf4,0xef,0x5d,0x63,0x65,0x3a,
  27361. 0x65,0x04,0x0c,0xf9,0xbf,0xd4,0xac,0xa7,
  27362. 0x98,0x4a,0x74,0xd3,0x71,0x45,0x98,0x67,
  27363. 0x80,0xfc,0x0b,0x16,0xac,0x45,0x16,0x49,
  27364. 0xde,0x61,0x88,0xa7,0xdb,0xdf,0x19,0x1f,
  27365. 0x64,0xb5,0xfc,0x5e,0x2a,0xb4,0x7b,0x57,
  27366. 0xf7,0xf7,0x27,0x6c,0xd4,0x19,0xc1,0x7a,
  27367. 0x3c,0xa8,0xe1,0xb9,0x39,0xae,0x49,0xe4,
  27368. 0x88,0xac,0xba,0x6b,0x96,0x56,0x10,0xb5,
  27369. 0x48,0x01,0x09,0xc8,0xb1,0x7b,0x80,0xe1,
  27370. 0xb7,0xb7,0x50,0xdf,0xc7,0x59,0x8d,0x5d,
  27371. 0x50,0x11,0xfd,0x2d,0xcc,0x56,0x00,0xa3,
  27372. 0x2e,0xf5,0xb5,0x2a,0x1e,0xcc,0x82,0x0e,
  27373. 0x30,0x8a,0xa3,0x42,0x72,0x1a,0xac,0x09,
  27374. 0x43,0xbf,0x66,0x86,0xb6,0x4b,0x25,0x79,
  27375. 0x37,0x65,0x04,0xcc,0xc4,0x93,0xd9,0x7e,
  27376. 0x6a,0xed,0x3f,0xb0,0xf9,0xcd,0x71,0xa4,
  27377. 0x3d,0xd4,0x97,0xf0,0x1f,0x17,0xc0,0xe2,
  27378. 0xcb,0x37,0x97,0xaa,0x2a,0x2f,0x25,0x66,
  27379. 0x56,0x16,0x8e,0x6c,0x49,0x6a,0xfc,0x5f,
  27380. 0xb9,0x32,0x46,0xf6,0xb1,0x11,0x63,0x98,
  27381. 0xa3,0x46,0xf1,0xa6,0x41,0xf3,0xb0,0x41,
  27382. 0xe9,0x89,0xf7,0x91,0x4f,0x90,0xcc,0x2c,
  27383. 0x7f,0xff,0x35,0x78,0x76,0xe5,0x06,0xb5,
  27384. 0x0d,0x33,0x4b,0xa7,0x7c,0x22,0x5b,0xc3,
  27385. 0x07,0xba,0x53,0x71,0x52,0xf3,0xf1,0x61,
  27386. 0x0e,0x4e,0xaf,0xe5,0x95,0xf6,0xd9,0xd9,
  27387. 0x0d,0x11,0xfa,0xa9,0x33,0xa1,0x5e,0xf1,
  27388. 0x36,0x95,0x46,0x86,0x8a,0x7f,0x3a,0x45,
  27389. 0xa9,0x67,0x68,0xd4,0x0f,0xd9,0xd0,0x34,
  27390. 0x12,0xc0,0x91,0xc6,0x31,0x5c,0xf4,0xfd,
  27391. 0xe7,0xcb,0x68,0x60,0x69,0x37,0x38,0x0d,
  27392. 0xb2,0xea,0xaa,0x70,0x7b,0x4c,0x41,0x85,
  27393. 0xc3,0x2e,0xdd,0xcd,0xd3,0x06,0x70,0x5e,
  27394. 0x4d,0xc1,0xff,0xc8,0x72,0xee,0xee,0x47,
  27395. 0x5a,0x64,0xdf,0xac,0x86,0xab,0xa4,0x1c,
  27396. 0x06,0x18,0x98,0x3f,0x87,0x41,0xc5,0xef,
  27397. 0x68,0xd3,0xa1,0x01,0xe8,0xa3,0xb8,0xca,
  27398. 0xc6,0x0c,0x90,0x5c,0x15,0xfc,0x91,0x08,
  27399. 0x40,0xb9,0x4c,0x00,0xa0,0xb9,0xd0
  27400. };
  27401. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  27402. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  27403. sizeof(msg2),
  27404. sizeof(msg3),
  27405. 0 /*sizeof(msg1)*/,
  27406. 0 /*sizeof(msg1)*/,
  27407. sizeof(msg4)
  27408. };
  27409. #ifndef NO_ASN
  27410. static byte privateEd25519[] = {
  27411. 0x30,0x2e,0x02,0x01,0x00,0x30,0x05,0x06,
  27412. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  27413. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  27414. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  27415. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  27416. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  27417. };
  27418. static byte badPrivateEd25519[] = {
  27419. 0x30,0x52,0x02,0x01,0x00,0x30,0x05,0x06,
  27420. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  27421. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  27422. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  27423. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  27424. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60,
  27425. 0xa1,0x22,0x04,0x21,0xd7,0x5a,0x98,0x01, /* octet len 0x20 -> 0x21 */
  27426. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  27427. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  27428. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  27429. 0xf7,0x07,0x51,0x1a,
  27430. 0x00 /* add additional bytes to make the pubkey bigger */
  27431. };
  27432. static byte publicEd25519[] = {
  27433. 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,
  27434. 0x70,0x03,0x21,0x00,0xd7,0x5a,0x98,0x01,
  27435. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  27436. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  27437. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  27438. 0xf7,0x07,0x51,0x1a
  27439. };
  27440. /* size has been altered to catch if sanity check is done */
  27441. static byte badPublicEd25519[] = {
  27442. 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,
  27443. 0x70,0x03,0x21,0x00,0xd7,0x5a,0x98,0x01,
  27444. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  27445. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  27446. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  27447. 0xf7,0x07,0x51,0x1a,
  27448. 0x00 /* add an additional byte to make the pubkey appear bigger */
  27449. };
  27450. static byte privPubEd25519[] = {
  27451. 0x30,0x50,0x02,0x01,0x00,0x30,0x05,0x06,
  27452. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  27453. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  27454. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  27455. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  27456. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60,
  27457. 0x81,0x20,0xd7,0x5a,0x98,0x01,0x82,0xb1,
  27458. 0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,0xc9,0x64,
  27459. 0x07,0x3a,0x0e,0xe1,0x72,0xf3,0xda,0xa6,
  27460. 0x23,0x25,0xaf,0x02,0x1a,0x68,0xf7,0x07,
  27461. 0x51,0x1a
  27462. };
  27463. word32 idx;
  27464. #endif /* NO_ASN */
  27465. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  27466. #if !defined(NO_ASN) && defined(HAVE_ED25519_SIGN)
  27467. ed25519_key key3;
  27468. #endif
  27469. /* create ed25519 keys */
  27470. #ifndef HAVE_FIPS
  27471. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  27472. #else
  27473. ret = wc_InitRng(&rng);
  27474. #endif
  27475. if (ret != 0)
  27476. return WC_TEST_RET_ENC_EC(ret);
  27477. wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  27478. wc_ed25519_init_ex(&key2, HEAP_HINT, devId);
  27479. #if !defined(NO_ASN) && defined(HAVE_ED25519_SIGN)
  27480. wc_ed25519_init_ex(&key3, HEAP_HINT, devId);
  27481. #endif
  27482. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  27483. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key2);
  27484. /* helper functions for signature and key size */
  27485. keySz = wc_ed25519_size(&key);
  27486. sigSz = wc_ed25519_sig_size(&key);
  27487. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  27488. defined(HAVE_ED25519_KEY_IMPORT)
  27489. for (i = 0; i < 6; i++) {
  27490. outlen = sizeof(out);
  27491. XMEMSET(out, 0, sizeof(out));
  27492. if (wc_ed25519_import_private_key(sKeys[i], ED25519_KEY_SIZE, pKeys[i],
  27493. pKeySz[i], &key) != 0)
  27494. return WC_TEST_RET_ENC_I(i);
  27495. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key) != 0)
  27496. return WC_TEST_RET_ENC_I(i);
  27497. if (XMEMCMP(out, sigs[i], 64))
  27498. return WC_TEST_RET_ENC_I(i);
  27499. #if defined(HAVE_ED25519_VERIFY)
  27500. /* test verify on good msg */
  27501. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  27502. &key) != 0 || verify != 1)
  27503. return WC_TEST_RET_ENC_I(i);
  27504. #ifdef WOLFSSL_ED25519_STREAMING_VERIFY
  27505. /* test verify on good msg using streaming interface directly */
  27506. if (wc_ed25519_verify_msg_init(out, outlen,
  27507. &key, (byte)Ed25519, NULL, 0) != 0)
  27508. return WC_TEST_RET_ENC_I(i);
  27509. for (j = 0; j < msgSz[i]; j += i) {
  27510. if (wc_ed25519_verify_msg_update(msgs[i] + j, MIN(i, msgSz[i] - j), &key) != 0)
  27511. return WC_TEST_RET_ENC_I(i);
  27512. }
  27513. if (wc_ed25519_verify_msg_final(out, outlen, &verify,
  27514. &key) != 0 || verify != 1)
  27515. return WC_TEST_RET_ENC_I(i);
  27516. #endif /* WOLFSSL_ED25519_STREAMING_VERIFY */
  27517. /* test verify on bad msg */
  27518. out[outlen-1] = out[outlen-1] + 1;
  27519. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  27520. &key) == 0 || verify == 1)
  27521. return WC_TEST_RET_ENC_I(i);
  27522. #endif /* HAVE_ED25519_VERIFY */
  27523. /* test api for import/exporting keys */
  27524. exportPSz = sizeof(exportPKey);
  27525. exportSSz = sizeof(exportSKey);
  27526. if (wc_ed25519_export_public(&key, exportPKey, &exportPSz) != 0)
  27527. return WC_TEST_RET_ENC_I(i);
  27528. if (wc_ed25519_import_public_ex(exportPKey, exportPSz, &key2, 1) != 0)
  27529. return WC_TEST_RET_ENC_I(i);
  27530. if (wc_ed25519_export_private_only(&key, exportSKey, &exportSSz) != 0)
  27531. return WC_TEST_RET_ENC_I(i);
  27532. if (wc_ed25519_import_private_key(exportSKey, exportSSz,
  27533. exportPKey, exportPSz, &key2) != 0)
  27534. return WC_TEST_RET_ENC_I(i);
  27535. /* clear "out" buffer and test sign with imported keys */
  27536. outlen = sizeof(out);
  27537. XMEMSET(out, 0, sizeof(out));
  27538. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key2) != 0)
  27539. return WC_TEST_RET_ENC_I(i);
  27540. #if defined(HAVE_ED25519_VERIFY)
  27541. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  27542. &key2) != 0 || verify != 1)
  27543. return WC_TEST_RET_ENC_I(i);
  27544. if (XMEMCMP(out, sigs[i], 64))
  27545. return WC_TEST_RET_ENC_I(i);
  27546. #endif /* HAVE_ED25519_VERIFY */
  27547. }
  27548. ret = ed25519ctx_test();
  27549. if (ret != 0)
  27550. return ret;
  27551. ret = ed25519ph_test();
  27552. if (ret != 0)
  27553. return ret;
  27554. #ifndef NO_ASN
  27555. /* Try ASN.1 encoded private-only key and public key. */
  27556. idx = 0;
  27557. ret = wc_Ed25519PrivateKeyDecode(privateEd25519, &idx, &key3,
  27558. sizeof(privateEd25519));
  27559. if (ret != 0)
  27560. return WC_TEST_RET_ENC_EC(ret);
  27561. idx = 0;
  27562. if (wc_Ed25519PrivateKeyDecode(badPrivateEd25519, &idx, &key3,
  27563. sizeof(badPrivateEd25519)) == 0)
  27564. return WC_TEST_RET_ENC_NC;
  27565. ret = wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3);
  27566. if (ret != BAD_FUNC_ARG)
  27567. return WC_TEST_RET_ENC_EC(ret);
  27568. /* try with a buffer size that is too large */
  27569. idx = 0;
  27570. if (wc_Ed25519PublicKeyDecode(badPublicEd25519, &idx, &key3,
  27571. sizeof(badPublicEd25519)) == 0)
  27572. return WC_TEST_RET_ENC_NC;
  27573. idx = 0;
  27574. ret = wc_Ed25519PublicKeyDecode(publicEd25519, &idx, &key3,
  27575. sizeof(publicEd25519));
  27576. if (ret != 0)
  27577. return WC_TEST_RET_ENC_EC(ret);
  27578. ret = wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3);
  27579. if (ret != 0)
  27580. return WC_TEST_RET_ENC_EC(ret);
  27581. if (XMEMCMP(out, sigs[0], 64))
  27582. return WC_TEST_RET_ENC_NC;
  27583. #if defined(HAVE_ED25519_VERIFY)
  27584. /* test verify on good msg */
  27585. ret = wc_ed25519_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, &key3);
  27586. if (ret != 0 || verify != 1)
  27587. return WC_TEST_RET_ENC_EC(ret);
  27588. #endif /* HAVE_ED25519_VERIFY */
  27589. wc_ed25519_free(&key3);
  27590. wc_ed25519_init(&key3);
  27591. idx = 0;
  27592. ret = wc_Ed25519PrivateKeyDecode(privPubEd25519, &idx, &key3,
  27593. sizeof(privPubEd25519));
  27594. if (ret != 0)
  27595. return WC_TEST_RET_ENC_EC(ret);
  27596. ret = wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3);
  27597. if (ret != 0)
  27598. return WC_TEST_RET_ENC_EC(ret);
  27599. if (XMEMCMP(out, sigs[0], 64))
  27600. return WC_TEST_RET_ENC_NC;
  27601. wc_ed25519_free(&key3);
  27602. #endif /* NO_ASN */
  27603. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  27604. /* clean up keys when done */
  27605. wc_ed25519_free(&key);
  27606. wc_ed25519_free(&key2);
  27607. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  27608. wc_FreeRng(&rng);
  27609. #endif
  27610. /* hush warnings of unused keySz and sigSz */
  27611. (void)keySz;
  27612. (void)sigSz;
  27613. #ifdef WOLFSSL_TEST_CERT
  27614. ret = ed25519_test_cert();
  27615. if (ret < 0)
  27616. return ret;
  27617. #ifdef WOLFSSL_CERT_GEN
  27618. ret = ed25519_test_make_cert();
  27619. if (ret < 0)
  27620. return ret;
  27621. #endif /* WOLFSSL_CERT_GEN */
  27622. #endif /* WOLFSSL_TEST_CERT */
  27623. return 0;
  27624. }
  27625. #endif /* HAVE_ED25519 */
  27626. #ifdef HAVE_CURVE448
  27627. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  27628. defined(HAVE_CURVE448_KEY_IMPORT)
  27629. /* Test the wc_curve448_check_public API.
  27630. *
  27631. * returns 0 on success and -ve on failure.
  27632. */
  27633. static wc_test_ret_t curve448_check_public_test(void)
  27634. {
  27635. /* Little-endian values that will fail */
  27636. byte fail_le[][CURVE448_KEY_SIZE] = {
  27637. {
  27638. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27639. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27640. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27641. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27642. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27643. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27644. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  27645. },
  27646. {
  27647. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27648. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27649. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27650. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27651. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27652. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27653. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  27654. },
  27655. };
  27656. /* Big-endian values that will fail */
  27657. byte fail_be[][CURVE448_KEY_SIZE] = {
  27658. {
  27659. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27660. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27661. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27662. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27663. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27664. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27665. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  27666. },
  27667. {
  27668. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27669. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27670. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27671. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27672. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27673. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27674. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  27675. },
  27676. };
  27677. /* Good or valid public value */
  27678. byte good[CURVE448_KEY_SIZE] = {
  27679. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27680. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27681. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27682. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27683. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27684. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  27685. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  27686. };
  27687. int i;
  27688. wc_test_ret_t ret;
  27689. /* Parameter checks */
  27690. /* NULL pointer */
  27691. ret = wc_curve448_check_public(NULL, 0, EC448_LITTLE_ENDIAN);
  27692. if (ret != BAD_FUNC_ARG)
  27693. return WC_TEST_RET_ENC_EC(ret);
  27694. ret = wc_curve448_check_public(NULL, 0, EC448_BIG_ENDIAN);
  27695. if (ret != BAD_FUNC_ARG)
  27696. return WC_TEST_RET_ENC_EC(ret);
  27697. /* Length of 0 treated differently to other invalid lengths for TLS */
  27698. ret = wc_curve448_check_public(good, 0, EC448_LITTLE_ENDIAN);
  27699. if (ret != BUFFER_E)
  27700. return WC_TEST_RET_ENC_EC(ret);
  27701. ret = wc_curve448_check_public(good, 0, EC448_BIG_ENDIAN);
  27702. if (ret != BUFFER_E)
  27703. return WC_TEST_RET_ENC_EC(ret);
  27704. /* Length not CURVE448_KEY_SIZE */
  27705. for (i = 1; i < CURVE448_KEY_SIZE + 2; i++) {
  27706. if (i == CURVE448_KEY_SIZE)
  27707. continue;
  27708. if (wc_curve448_check_public(good, i, EC448_LITTLE_ENDIAN) !=
  27709. ECC_BAD_ARG_E) {
  27710. return WC_TEST_RET_ENC_I(i);
  27711. }
  27712. if (wc_curve448_check_public(good, i, EC448_BIG_ENDIAN) !=
  27713. ECC_BAD_ARG_E) {
  27714. return WC_TEST_RET_ENC_I(i);
  27715. }
  27716. }
  27717. /* Little-endian fail cases */
  27718. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  27719. if (wc_curve448_check_public(fail_le[i], CURVE448_KEY_SIZE,
  27720. EC448_LITTLE_ENDIAN) == 0) {
  27721. return WC_TEST_RET_ENC_I(i);
  27722. }
  27723. }
  27724. /* Big-endian fail cases */
  27725. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  27726. if (wc_curve448_check_public(fail_be[i], CURVE448_KEY_SIZE,
  27727. EC448_BIG_ENDIAN) == 0) {
  27728. return WC_TEST_RET_ENC_I(i);
  27729. }
  27730. }
  27731. /* Check a valid public value works! */
  27732. ret = wc_curve448_check_public(good, CURVE448_KEY_SIZE,
  27733. EC448_LITTLE_ENDIAN);
  27734. if (ret != 0)
  27735. return WC_TEST_RET_ENC_EC(ret);
  27736. ret = wc_curve448_check_public(good, CURVE448_KEY_SIZE, EC448_BIG_ENDIAN);
  27737. if (ret != 0)
  27738. return WC_TEST_RET_ENC_EC(ret);
  27739. return 0;
  27740. }
  27741. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  27742. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t curve448_test(void)
  27743. {
  27744. WC_RNG rng;
  27745. wc_test_ret_t ret;
  27746. #ifdef HAVE_CURVE448_SHARED_SECRET
  27747. byte sharedA[CURVE448_KEY_SIZE];
  27748. byte sharedB[CURVE448_KEY_SIZE];
  27749. word32 y;
  27750. #endif
  27751. #ifdef HAVE_CURVE448_KEY_EXPORT
  27752. byte exportBuf[CURVE448_KEY_SIZE];
  27753. #endif
  27754. word32 x;
  27755. curve448_key userA, userB, pubKey;
  27756. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  27757. defined(HAVE_CURVE448_KEY_IMPORT)
  27758. /* test vectors from
  27759. https://www.rfc-editor.org/rfc/rfc7748.html
  27760. */
  27761. /* secret key for party a */
  27762. byte sa[] = {
  27763. 0x6b, 0x72, 0x98, 0xa5, 0xc0, 0xd8, 0xc2, 0x9a,
  27764. 0x1d, 0xab, 0x27, 0xf1, 0xa6, 0x82, 0x63, 0x00,
  27765. 0x91, 0x73, 0x89, 0x44, 0x97, 0x41, 0xa9, 0x74,
  27766. 0xf5, 0xba, 0xc9, 0xd9, 0x8d, 0xc2, 0x98, 0xd4,
  27767. 0x65, 0x55, 0xbc, 0xe8, 0xba, 0xe8, 0x9e, 0xee,
  27768. 0xd4, 0x00, 0x58, 0x4b, 0xb0, 0x46, 0xcf, 0x75,
  27769. 0x57, 0x9f, 0x51, 0xd1, 0x25, 0x49, 0x8f, 0x9a,
  27770. };
  27771. /* public key for party a */
  27772. byte pa[] = {
  27773. 0xa0, 0x1f, 0xc4, 0x32, 0xe5, 0x80, 0x7f, 0x17,
  27774. 0x53, 0x0d, 0x12, 0x88, 0xda, 0x12, 0x5b, 0x0c,
  27775. 0xd4, 0x53, 0xd9, 0x41, 0x72, 0x64, 0x36, 0xc8,
  27776. 0xbb, 0xd9, 0xc5, 0x22, 0x2c, 0x3d, 0xa7, 0xfa,
  27777. 0x63, 0x9c, 0xe0, 0x3d, 0xb8, 0xd2, 0x3b, 0x27,
  27778. 0x4a, 0x07, 0x21, 0xa1, 0xae, 0xd5, 0x22, 0x7d,
  27779. 0xe6, 0xe3, 0xb7, 0x31, 0xcc, 0xf7, 0x08, 0x9b,
  27780. };
  27781. /* secret key for party b */
  27782. byte sb[] = {
  27783. 0x2d, 0x99, 0x73, 0x51, 0xb6, 0x10, 0x6f, 0x36,
  27784. 0xb0, 0xd1, 0x09, 0x1b, 0x92, 0x9c, 0x4c, 0x37,
  27785. 0x21, 0x3e, 0x0d, 0x2b, 0x97, 0xe8, 0x5e, 0xbb,
  27786. 0x20, 0xc1, 0x27, 0x69, 0x1d, 0x0d, 0xad, 0x8f,
  27787. 0x1d, 0x81, 0x75, 0xb0, 0x72, 0x37, 0x45, 0xe6,
  27788. 0x39, 0xa3, 0xcb, 0x70, 0x44, 0x29, 0x0b, 0x99,
  27789. 0xe0, 0xe2, 0xa0, 0xc2, 0x7a, 0x6a, 0x30, 0x1c,
  27790. };
  27791. /* public key for party b */
  27792. byte pb[] = {
  27793. 0x09, 0x36, 0xf3, 0x7b, 0xc6, 0xc1, 0xbd, 0x07,
  27794. 0xae, 0x3d, 0xec, 0x7a, 0xb5, 0xdc, 0x06, 0xa7,
  27795. 0x3c, 0xa1, 0x32, 0x42, 0xfb, 0x34, 0x3e, 0xfc,
  27796. 0x72, 0xb9, 0xd8, 0x27, 0x30, 0xb4, 0x45, 0xf3,
  27797. 0xd4, 0xb0, 0xbd, 0x07, 0x71, 0x62, 0xa4, 0x6d,
  27798. 0xcf, 0xec, 0x6f, 0x9b, 0x59, 0x0b, 0xfc, 0xbc,
  27799. 0xf5, 0x20, 0xcd, 0xb0, 0x29, 0xa8, 0xb7, 0x3e,
  27800. };
  27801. /* expected shared key */
  27802. byte ss[] = {
  27803. 0x9d, 0x87, 0x4a, 0x51, 0x37, 0x50, 0x9a, 0x44,
  27804. 0x9a, 0xd5, 0x85, 0x30, 0x40, 0x24, 0x1c, 0x52,
  27805. 0x36, 0x39, 0x54, 0x35, 0xc3, 0x64, 0x24, 0xfd,
  27806. 0x56, 0x0b, 0x0c, 0xb6, 0x2b, 0x28, 0x1d, 0x28,
  27807. 0x52, 0x75, 0xa7, 0x40, 0xce, 0x32, 0xa2, 0x2d,
  27808. 0xd1, 0x74, 0x0f, 0x4a, 0xa9, 0x16, 0x1c, 0xec,
  27809. 0x95, 0xcc, 0xc6, 0x1a, 0x18, 0xf4, 0xff, 0x07,
  27810. };
  27811. #endif /* HAVE_CURVE448_SHARED_SECRET */
  27812. (void)x;
  27813. #ifndef HAVE_FIPS
  27814. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  27815. #else
  27816. ret = wc_InitRng(&rng);
  27817. #endif
  27818. if (ret != 0)
  27819. return WC_TEST_RET_ENC_EC(ret);
  27820. wc_curve448_init(&userA);
  27821. wc_curve448_init(&userB);
  27822. wc_curve448_init(&pubKey);
  27823. /* make curve448 keys */
  27824. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userA);
  27825. if (ret != 0)
  27826. return WC_TEST_RET_ENC_EC(ret);
  27827. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userB);
  27828. if (ret != 0)
  27829. return WC_TEST_RET_ENC_EC(ret);
  27830. #ifdef HAVE_CURVE448_SHARED_SECRET
  27831. /* find shared secret key */
  27832. x = sizeof(sharedA);
  27833. ret = wc_curve448_shared_secret(&userA, &userB, sharedA, &x);
  27834. if (ret != 0)
  27835. return WC_TEST_RET_ENC_EC(ret);
  27836. y = sizeof(sharedB);
  27837. ret = wc_curve448_shared_secret(&userB, &userA, sharedB, &y);
  27838. if (ret != 0)
  27839. return WC_TEST_RET_ENC_EC(ret);
  27840. /* compare shared secret keys to test they are the same */
  27841. if (y != x)
  27842. return WC_TEST_RET_ENC_NC;
  27843. if (XMEMCMP(sharedA, sharedB, x))
  27844. return WC_TEST_RET_ENC_NC;
  27845. #endif
  27846. #ifdef HAVE_CURVE448_KEY_EXPORT
  27847. /* export a public key and import it for another user */
  27848. x = sizeof(exportBuf);
  27849. ret = wc_curve448_export_public(&userA, exportBuf, &x);
  27850. if (ret != 0)
  27851. return WC_TEST_RET_ENC_EC(ret);
  27852. #ifdef HAVE_CURVE448_KEY_IMPORT
  27853. ret = wc_curve448_import_public(exportBuf, x, &pubKey);
  27854. if (ret != 0)
  27855. return WC_TEST_RET_ENC_EC(ret);
  27856. #endif
  27857. #endif
  27858. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  27859. defined(HAVE_CURVE448_KEY_IMPORT)
  27860. /* test shared key after importing a public key */
  27861. XMEMSET(sharedB, 0, sizeof(sharedB));
  27862. y = sizeof(sharedB);
  27863. ret = wc_curve448_shared_secret(&userB, &pubKey, sharedB, &y);
  27864. if (ret != 0)
  27865. return WC_TEST_RET_ENC_EC(ret);
  27866. if (XMEMCMP(sharedA, sharedB, y))
  27867. return WC_TEST_RET_ENC_NC;
  27868. /* import RFC test vectors and compare shared key */
  27869. ret = wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA);
  27870. if (ret != 0)
  27871. return WC_TEST_RET_ENC_EC(ret);
  27872. ret = wc_curve448_import_private_raw(sb, sizeof(sb), pb, sizeof(pb), &userB);
  27873. if (ret != 0)
  27874. return WC_TEST_RET_ENC_EC(ret);
  27875. /* test against known test vector */
  27876. XMEMSET(sharedB, 0, sizeof(sharedB));
  27877. y = sizeof(sharedB);
  27878. ret = wc_curve448_shared_secret(&userA, &userB, sharedB, &y);
  27879. if (ret != 0)
  27880. return WC_TEST_RET_ENC_EC(ret);
  27881. if (XMEMCMP(ss, sharedB, y))
  27882. return WC_TEST_RET_ENC_NC;
  27883. /* test swapping roles of keys and generating same shared key */
  27884. XMEMSET(sharedB, 0, sizeof(sharedB));
  27885. y = sizeof(sharedB);
  27886. ret = wc_curve448_shared_secret(&userB, &userA, sharedB, &y);
  27887. if (ret != 0)
  27888. return WC_TEST_RET_ENC_EC(ret);
  27889. if (XMEMCMP(ss, sharedB, y))
  27890. return WC_TEST_RET_ENC_NC;
  27891. /* test with 1 generated key and 1 from known test vector */
  27892. ret = wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA);
  27893. if (ret != 0)
  27894. return WC_TEST_RET_ENC_EC(ret);
  27895. ret = wc_curve448_make_key(&rng, 56, &userB);
  27896. if (ret != 0)
  27897. return WC_TEST_RET_ENC_EC(ret);
  27898. x = sizeof(sharedA);
  27899. ret = wc_curve448_shared_secret(&userA, &userB, sharedA, &x);
  27900. if (ret != 0)
  27901. return WC_TEST_RET_ENC_EC(ret);
  27902. y = sizeof(sharedB);
  27903. ret = wc_curve448_shared_secret(&userB, &userA, sharedB, &y);
  27904. if (ret != 0)
  27905. return WC_TEST_RET_ENC_EC(ret);
  27906. /* compare shared secret keys to test they are the same */
  27907. if (y != x)
  27908. return WC_TEST_RET_ENC_NC;
  27909. if (XMEMCMP(sharedA, sharedB, x))
  27910. return WC_TEST_RET_ENC_NC;
  27911. ret = curve448_check_public_test();
  27912. if (ret != 0)
  27913. return ret;
  27914. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  27915. /* clean up keys when done */
  27916. wc_curve448_free(&pubKey);
  27917. wc_curve448_free(&userB);
  27918. wc_curve448_free(&userA);
  27919. wc_FreeRng(&rng);
  27920. return 0;
  27921. }
  27922. #endif /* HAVE_CURVE448 */
  27923. #ifdef HAVE_ED448
  27924. #ifdef WOLFSSL_TEST_CERT
  27925. static wc_test_ret_t ed448_test_cert(void)
  27926. {
  27927. DecodedCert cert[2];
  27928. DecodedCert* serverCert = NULL;
  27929. DecodedCert* caCert = NULL;
  27930. #ifdef HAVE_ED448_VERIFY
  27931. ed448_key key;
  27932. ed448_key* pubKey = NULL;
  27933. int verify;
  27934. #endif /* HAVE_ED448_VERIFY */
  27935. wc_test_ret_t ret;
  27936. byte* tmp;
  27937. size_t bytes;
  27938. XFILE file;
  27939. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27940. if (tmp == NULL) {
  27941. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  27942. }
  27943. #ifdef USE_CERT_BUFFERS_256
  27944. XMEMCPY(tmp, ca_ed448_cert, sizeof_ca_ed448_cert);
  27945. bytes = sizeof_ca_ed448_cert;
  27946. #elif !defined(NO_FILESYSTEM)
  27947. file = XFOPEN(caEd448Cert, "rb");
  27948. if (file == NULL) {
  27949. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  27950. }
  27951. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  27952. XFCLOSE(file);
  27953. if (bytes == 0)
  27954. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  27955. #else
  27956. /* No certificate to use. */
  27957. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  27958. #endif
  27959. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  27960. caCert = &cert[0];
  27961. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  27962. if (ret != 0)
  27963. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27964. #ifdef USE_CERT_BUFFERS_256
  27965. XMEMCPY(tmp, server_ed448_cert, sizeof_server_ed448_cert);
  27966. bytes = sizeof_server_ed448_cert;
  27967. #elif !defined(NO_FILESYSTEM)
  27968. file = XFOPEN(serverEd448Cert, "rb");
  27969. if (file == NULL) {
  27970. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  27971. }
  27972. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  27973. XFCLOSE(file);
  27974. if (bytes == 0)
  27975. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  27976. #else
  27977. /* No certificate to use. */
  27978. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  27979. #endif
  27980. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  27981. serverCert = &cert[1];
  27982. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  27983. if (ret != 0)
  27984. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27985. #ifdef HAVE_ED448_VERIFY
  27986. ret = wc_ed448_init(&key);
  27987. if (ret < 0)
  27988. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27989. pubKey = &key;
  27990. ret = wc_ed448_import_public(caCert->publicKey, caCert->pubKeySize, pubKey);
  27991. if (ret < 0)
  27992. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27993. ret = wc_ed448_verify_msg(serverCert->signature, serverCert->sigLength,
  27994. serverCert->source + serverCert->certBegin,
  27995. serverCert->sigIndex - serverCert->certBegin,
  27996. &verify, pubKey, NULL, 0);
  27997. if (ret < 0 || verify != 1) {
  27998. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  27999. }
  28000. #endif /* HAVE_ED448_VERIFY */
  28001. done:
  28002. if (tmp != NULL)
  28003. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28004. #ifdef HAVE_ED448_VERIFY
  28005. wc_ed448_free(pubKey);
  28006. #endif /* HAVE_ED448_VERIFY */
  28007. if (caCert != NULL)
  28008. FreeDecodedCert(caCert);
  28009. if (serverCert != NULL)
  28010. FreeDecodedCert(serverCert);
  28011. return ret;
  28012. }
  28013. static wc_test_ret_t ed448_test_make_cert(void)
  28014. {
  28015. WC_RNG rng;
  28016. Cert cert;
  28017. DecodedCert decode;
  28018. ed448_key key;
  28019. ed448_key* privKey = NULL;
  28020. wc_test_ret_t ret = 0;
  28021. byte* tmp = NULL;
  28022. wc_InitCert_ex(&cert, HEAP_HINT, devId);
  28023. #ifndef HAVE_FIPS
  28024. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  28025. #else
  28026. ret = wc_InitRng(&rng);
  28027. #endif
  28028. if (ret != 0)
  28029. return WC_TEST_RET_ENC_EC(ret);
  28030. wc_ed448_init(&key);
  28031. privKey = &key;
  28032. wc_ed448_make_key(&rng, ED448_KEY_SIZE, privKey);
  28033. cert.daysValid = 365 * 2;
  28034. cert.selfSigned = 1;
  28035. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  28036. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  28037. cert.isCA = 0;
  28038. #ifdef WOLFSSL_CERT_EXT
  28039. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  28040. if (ret < 0)
  28041. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28042. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  28043. if (ret < 0)
  28044. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28045. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  28046. if (ret < 0)
  28047. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28048. #endif
  28049. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28050. if (tmp == NULL) {
  28051. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  28052. }
  28053. cert.sigType = CTC_ED448;
  28054. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED448_TYPE, privKey, &rng);
  28055. if (ret < 0)
  28056. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28057. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF, ED448_TYPE,
  28058. privKey, &rng);
  28059. if (ret < 0)
  28060. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28061. InitDecodedCert(&decode, tmp, (word32)ret, HEAP_HINT);
  28062. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  28063. FreeDecodedCert(&decode);
  28064. if (ret != 0)
  28065. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  28066. done:
  28067. if (tmp != NULL)
  28068. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28069. wc_ed448_free(privKey);
  28070. wc_FreeRng(&rng);
  28071. return ret;
  28072. }
  28073. #endif /* WOLFSSL_TEST_CERT */
  28074. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  28075. defined(HAVE_ED448_KEY_IMPORT)
  28076. static wc_test_ret_t ed448_ctx_test(void)
  28077. {
  28078. wc_test_ret_t ret;
  28079. byte out[ED448_SIG_SIZE];
  28080. word32 outlen;
  28081. #ifdef HAVE_ED448_VERIFY
  28082. int verify;
  28083. #endif /* HAVE_ED448_VERIFY */
  28084. ed448_key key;
  28085. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  28086. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  28087. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  28088. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  28089. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  28090. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  28091. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  28092. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  28093. 0x4e
  28094. };
  28095. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  28096. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  28097. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  28098. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  28099. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  28100. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  28101. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  28102. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  28103. 0x80
  28104. };
  28105. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx[] = {
  28106. 0xd4, 0xf8, 0xf6, 0x13, 0x17, 0x70, 0xdd, 0x46,
  28107. 0xf4, 0x08, 0x67, 0xd6, 0xfd, 0x5d, 0x50, 0x55,
  28108. 0xde, 0x43, 0x54, 0x1f, 0x8c, 0x5e, 0x35, 0xab,
  28109. 0xbc, 0xd0, 0x01, 0xb3, 0x2a, 0x89, 0xf7, 0xd2,
  28110. 0x15, 0x1f, 0x76, 0x47, 0xf1, 0x1d, 0x8c, 0xa2,
  28111. 0xae, 0x27, 0x9f, 0xb8, 0x42, 0xd6, 0x07, 0x21,
  28112. 0x7f, 0xce, 0x6e, 0x04, 0x2f, 0x68, 0x15, 0xea,
  28113. 0x00, 0x0c, 0x85, 0x74, 0x1d, 0xe5, 0xc8, 0xda,
  28114. 0x11, 0x44, 0xa6, 0xa1, 0xab, 0xa7, 0xf9, 0x6d,
  28115. 0xe4, 0x25, 0x05, 0xd7, 0xa7, 0x29, 0x85, 0x24,
  28116. 0xfd, 0xa5, 0x38, 0xfc, 0xcb, 0xbb, 0x75, 0x4f,
  28117. 0x57, 0x8c, 0x1c, 0xad, 0x10, 0xd5, 0x4d, 0x0d,
  28118. 0x54, 0x28, 0x40, 0x7e, 0x85, 0xdc, 0xbc, 0x98,
  28119. 0xa4, 0x91, 0x55, 0xc1, 0x37, 0x64, 0xe6, 0x6c,
  28120. 0x3c, 0x00
  28121. };
  28122. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  28123. 0x03
  28124. };
  28125. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  28126. 0x66,0x6f,0x6f
  28127. };
  28128. outlen = sizeof(out);
  28129. XMEMSET(out, 0, sizeof(out));
  28130. ret = wc_ed448_init_ex(&key, HEAP_HINT, devId);
  28131. if (ret != 0)
  28132. return WC_TEST_RET_ENC_EC(ret);
  28133. ret = wc_ed448_import_private_key(sKeyCtx, ED448_KEY_SIZE, pKeyCtx,
  28134. sizeof(pKeyCtx), &key);
  28135. if (ret != 0)
  28136. return WC_TEST_RET_ENC_EC(ret);
  28137. ret = wc_ed448_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  28138. contextCtx, sizeof(contextCtx));
  28139. if (ret != 0)
  28140. return WC_TEST_RET_ENC_EC(ret);
  28141. if (XMEMCMP(out, sigCtx, sizeof(sigCtx)))
  28142. return WC_TEST_RET_ENC_NC;
  28143. #if defined(HAVE_ED448_VERIFY)
  28144. /* test verify on good msg */
  28145. ret = wc_ed448_verify_msg(out, outlen, msgCtx, sizeof(msgCtx), &verify, &key,
  28146. contextCtx, sizeof(contextCtx));
  28147. if (ret != 0 || verify != 1)
  28148. return WC_TEST_RET_ENC_EC(ret);
  28149. #endif
  28150. wc_ed448_free(&key);
  28151. return 0;
  28152. }
  28153. static wc_test_ret_t ed448ph_test(void)
  28154. {
  28155. wc_test_ret_t ret;
  28156. byte out[ED448_SIG_SIZE];
  28157. word32 outlen;
  28158. #ifdef HAVE_ED448_VERIFY
  28159. int verify;
  28160. #endif /* HAVE_ED448_VERIFY */
  28161. ed448_key key;
  28162. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  28163. 0x83, 0x3f, 0xe6, 0x24, 0x09, 0x23, 0x7b, 0x9d,
  28164. 0x62, 0xec, 0x77, 0x58, 0x75, 0x20, 0x91, 0x1e,
  28165. 0x9a, 0x75, 0x9c, 0xec, 0x1d, 0x19, 0x75, 0x5b,
  28166. 0x7d, 0xa9, 0x01, 0xb9, 0x6d, 0xca, 0x3d, 0x42,
  28167. 0xef, 0x78, 0x22, 0xe0, 0xd5, 0x10, 0x41, 0x27,
  28168. 0xdc, 0x05, 0xd6, 0xdb, 0xef, 0xde, 0x69, 0xe3,
  28169. 0xab, 0x2c, 0xec, 0x7c, 0x86, 0x7c, 0x6e, 0x2c,
  28170. 0x49
  28171. };
  28172. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  28173. 0x25, 0x9b, 0x71, 0xc1, 0x9f, 0x83, 0xef, 0x77,
  28174. 0xa7, 0xab, 0xd2, 0x65, 0x24, 0xcb, 0xdb, 0x31,
  28175. 0x61, 0xb5, 0x90, 0xa4, 0x8f, 0x7d, 0x17, 0xde,
  28176. 0x3e, 0xe0, 0xba, 0x9c, 0x52, 0xbe, 0xb7, 0x43,
  28177. 0xc0, 0x94, 0x28, 0xa1, 0x31, 0xd6, 0xb1, 0xb5,
  28178. 0x73, 0x03, 0xd9, 0x0d, 0x81, 0x32, 0xc2, 0x76,
  28179. 0xd5, 0xed, 0x3d, 0x5d, 0x01, 0xc0, 0xf5, 0x38,
  28180. 0x80
  28181. };
  28182. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  28183. 0x82, 0x2f, 0x69, 0x01, 0xf7, 0x48, 0x0f, 0x3d,
  28184. 0x5f, 0x56, 0x2c, 0x59, 0x29, 0x94, 0xd9, 0x69,
  28185. 0x36, 0x02, 0x87, 0x56, 0x14, 0x48, 0x32, 0x56,
  28186. 0x50, 0x56, 0x00, 0xbb, 0xc2, 0x81, 0xae, 0x38,
  28187. 0x1f, 0x54, 0xd6, 0xbc, 0xe2, 0xea, 0x91, 0x15,
  28188. 0x74, 0x93, 0x2f, 0x52, 0xa4, 0xe6, 0xca, 0xdd,
  28189. 0x78, 0x76, 0x93, 0x75, 0xec, 0x3f, 0xfd, 0x1b,
  28190. 0x80, 0x1a, 0x0d, 0x9b, 0x3f, 0x40, 0x30, 0xcd,
  28191. 0x43, 0x39, 0x64, 0xb6, 0x45, 0x7e, 0xa3, 0x94,
  28192. 0x76, 0x51, 0x12, 0x14, 0xf9, 0x74, 0x69, 0xb5,
  28193. 0x7d, 0xd3, 0x2d, 0xbc, 0x56, 0x0a, 0x9a, 0x94,
  28194. 0xd0, 0x0b, 0xff, 0x07, 0x62, 0x04, 0x64, 0xa3,
  28195. 0xad, 0x20, 0x3d, 0xf7, 0xdc, 0x7c, 0xe3, 0x60,
  28196. 0xc3, 0xcd, 0x36, 0x96, 0xd9, 0xd9, 0xfa, 0xb9,
  28197. 0x0f, 0x00
  28198. };
  28199. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  28200. 0xc3, 0x22, 0x99, 0xd4, 0x6e, 0xc8, 0xff, 0x02,
  28201. 0xb5, 0x45, 0x40, 0x98, 0x28, 0x14, 0xdc, 0xe9,
  28202. 0xa0, 0x58, 0x12, 0xf8, 0x19, 0x62, 0xb6, 0x49,
  28203. 0xd5, 0x28, 0x09, 0x59, 0x16, 0xa2, 0xaa, 0x48,
  28204. 0x10, 0x65, 0xb1, 0x58, 0x04, 0x23, 0xef, 0x92,
  28205. 0x7e, 0xcf, 0x0a, 0xf5, 0x88, 0x8f, 0x90, 0xda,
  28206. 0x0f, 0x6a, 0x9a, 0x85, 0xad, 0x5d, 0xc3, 0xf2,
  28207. 0x80, 0xd9, 0x12, 0x24, 0xba, 0x99, 0x11, 0xa3,
  28208. 0x65, 0x3d, 0x00, 0xe4, 0x84, 0xe2, 0xce, 0x23,
  28209. 0x25, 0x21, 0x48, 0x1c, 0x86, 0x58, 0xdf, 0x30,
  28210. 0x4b, 0xb7, 0x74, 0x5a, 0x73, 0x51, 0x4c, 0xdb,
  28211. 0x9b, 0xf3, 0xe1, 0x57, 0x84, 0xab, 0x71, 0x28,
  28212. 0x4f, 0x8d, 0x07, 0x04, 0xa6, 0x08, 0xc5, 0x4a,
  28213. 0x6b, 0x62, 0xd9, 0x7b, 0xeb, 0x51, 0x1d, 0x13,
  28214. 0x21, 0x00
  28215. };
  28216. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  28217. 0x61,0x62,0x63
  28218. };
  28219. /* SHA-512 hash of msgPh */
  28220. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  28221. 0x48, 0x33, 0x66, 0x60, 0x13, 0x60, 0xa8, 0x77,
  28222. 0x1c, 0x68, 0x63, 0x08, 0x0c, 0xc4, 0x11, 0x4d,
  28223. 0x8d, 0xb4, 0x45, 0x30, 0xf8, 0xf1, 0xe1, 0xee,
  28224. 0x4f, 0x94, 0xea, 0x37, 0xe7, 0x8b, 0x57, 0x39,
  28225. 0xd5, 0xa1, 0x5b, 0xef, 0x18, 0x6a, 0x53, 0x86,
  28226. 0xc7, 0x57, 0x44, 0xc0, 0x52, 0x7e, 0x1f, 0xaa,
  28227. 0x9f, 0x87, 0x26, 0xe4, 0x62, 0xa1, 0x2a, 0x4f,
  28228. 0xeb, 0x06, 0xbd, 0x88, 0x01, 0xe7, 0x51, 0xe4
  28229. };
  28230. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  28231. 0x66,0x6f,0x6f
  28232. };
  28233. outlen = sizeof(out);
  28234. XMEMSET(out, 0, sizeof(out));
  28235. ret = wc_ed448_init_ex(&key, HEAP_HINT, devId);
  28236. if (ret != 0)
  28237. return WC_TEST_RET_ENC_EC(ret);
  28238. ret = wc_ed448_import_private_key(sKeyPh, ED448_KEY_SIZE, pKeyPh,
  28239. sizeof(pKeyPh), &key);
  28240. if (ret != 0)
  28241. return WC_TEST_RET_ENC_EC(ret);
  28242. ret = wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key, NULL,
  28243. 0);
  28244. if (ret != 0)
  28245. return WC_TEST_RET_ENC_EC(ret);
  28246. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  28247. return WC_TEST_RET_ENC_NC;
  28248. #if defined(HAVE_ED448_VERIFY)
  28249. /* test verify on good msg */
  28250. ret = wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  28251. NULL, 0);
  28252. if (ret != 0 || verify != 1) {
  28253. return WC_TEST_RET_ENC_EC(ret);
  28254. }
  28255. #endif
  28256. ret = wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  28257. contextPh2, sizeof(contextPh2));
  28258. if (ret != 0)
  28259. return WC_TEST_RET_ENC_EC(ret);
  28260. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  28261. return WC_TEST_RET_ENC_NC;
  28262. #if defined(HAVE_ED448_VERIFY)
  28263. /* test verify on good msg */
  28264. ret = wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  28265. contextPh2, sizeof(contextPh2));
  28266. if (ret != 0 || verify != 1) {
  28267. return WC_TEST_RET_ENC_EC(ret);
  28268. }
  28269. #endif
  28270. ret = wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key, NULL,
  28271. 0);
  28272. if (ret != 0)
  28273. return WC_TEST_RET_ENC_EC(ret);
  28274. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  28275. return WC_TEST_RET_ENC_NC;
  28276. #if defined(HAVE_ED448_VERIFY)
  28277. ret = wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  28278. &key, NULL, 0);
  28279. if (ret != 0 || verify != 1) {
  28280. return WC_TEST_RET_ENC_EC(ret);
  28281. }
  28282. #endif
  28283. ret = wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  28284. contextPh2, sizeof(contextPh2));
  28285. if (ret != 0)
  28286. return WC_TEST_RET_ENC_EC(ret);
  28287. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  28288. return WC_TEST_RET_ENC_NC;
  28289. #if defined(HAVE_ED448_VERIFY)
  28290. ret = wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  28291. &key, contextPh2, sizeof(contextPh2));
  28292. if (ret != 0 || verify != 1) {
  28293. return WC_TEST_RET_ENC_EC(ret);
  28294. }
  28295. #endif
  28296. wc_ed448_free(&key);
  28297. return 0;
  28298. }
  28299. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  28300. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ed448_test(void)
  28301. {
  28302. wc_test_ret_t ret;
  28303. WC_RNG rng;
  28304. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  28305. defined(HAVE_ED448_KEY_IMPORT)
  28306. byte out[ED448_SIG_SIZE];
  28307. int i;
  28308. word32 outlen;
  28309. #ifdef HAVE_ED448_VERIFY
  28310. #ifdef WOLFSSL_ED448_STREAMING_VERIFY
  28311. int j;
  28312. #endif /* WOLFSSL_ED448_STREAMING_VERIFY */
  28313. int verify;
  28314. #endif /* HAVE_ED448_VERIFY */
  28315. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  28316. word32 keySz, sigSz;
  28317. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28318. ed448_key *key = NULL;
  28319. ed448_key *key2 = NULL;
  28320. #else
  28321. ed448_key key[1];
  28322. ed448_key key2[1];
  28323. #endif
  28324. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  28325. defined(HAVE_ED448_KEY_IMPORT)
  28326. /* test vectors from
  28327. https://tools.ietf.org/html/rfc8032
  28328. */
  28329. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  28330. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  28331. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  28332. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  28333. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  28334. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  28335. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  28336. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  28337. 0x5b
  28338. };
  28339. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  28340. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  28341. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  28342. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  28343. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  28344. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  28345. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  28346. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  28347. 0x4e
  28348. };
  28349. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  28350. 0x25, 0x8c, 0xdd, 0x4a, 0xda, 0x32, 0xed, 0x9c,
  28351. 0x9f, 0xf5, 0x4e, 0x63, 0x75, 0x6a, 0xe5, 0x82,
  28352. 0xfb, 0x8f, 0xab, 0x2a, 0xc7, 0x21, 0xf2, 0xc8,
  28353. 0xe6, 0x76, 0xa7, 0x27, 0x68, 0x51, 0x3d, 0x93,
  28354. 0x9f, 0x63, 0xdd, 0xdb, 0x55, 0x60, 0x91, 0x33,
  28355. 0xf2, 0x9a, 0xdf, 0x86, 0xec, 0x99, 0x29, 0xdc,
  28356. 0xcb, 0x52, 0xc1, 0xc5, 0xfd, 0x2f, 0xf7, 0xe2,
  28357. 0x1b
  28358. };
  28359. /* uncompressed test */
  28360. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  28361. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  28362. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  28363. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  28364. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  28365. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  28366. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  28367. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  28368. 0x5b
  28369. };
  28370. /* compressed prefix test */
  28371. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  28372. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  28373. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  28374. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  28375. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  28376. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  28377. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  28378. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  28379. 0x5b
  28380. };
  28381. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  28382. 0x87, 0x2d, 0x09, 0x37, 0x80, 0xf5, 0xd3, 0x73,
  28383. 0x0d, 0xf7, 0xc2, 0x12, 0x66, 0x4b, 0x37, 0xb8,
  28384. 0xa0, 0xf2, 0x4f, 0x56, 0x81, 0x0d, 0xaa, 0x83,
  28385. 0x82, 0xcd, 0x4f, 0xa3, 0xf7, 0x76, 0x34, 0xec,
  28386. 0x44, 0xdc, 0x54, 0xf1, 0xc2, 0xed, 0x9b, 0xea,
  28387. 0x86, 0xfa, 0xfb, 0x76, 0x32, 0xd8, 0xbe, 0x19,
  28388. 0x9e, 0xa1, 0x65, 0xf5, 0xad, 0x55, 0xdd, 0x9c,
  28389. 0xe8
  28390. };
  28391. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  28392. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  28393. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  28394. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  28395. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  28396. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  28397. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  28398. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  28399. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  28400. 0x80
  28401. };
  28402. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  28403. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  28404. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  28405. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  28406. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  28407. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  28408. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  28409. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  28410. 0x80
  28411. };
  28412. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  28413. 0x3b, 0xa1, 0x6d, 0xa0, 0xc6, 0xf2, 0xcc, 0x1f,
  28414. 0x30, 0x18, 0x77, 0x40, 0x75, 0x6f, 0x5e, 0x79,
  28415. 0x8d, 0x6b, 0xc5, 0xfc, 0x01, 0x5d, 0x7c, 0x63,
  28416. 0xcc, 0x95, 0x10, 0xee, 0x3f, 0xd4, 0x4a, 0xdc,
  28417. 0x24, 0xd8, 0xe9, 0x68, 0xb6, 0xe4, 0x6e, 0x6f,
  28418. 0x94, 0xd1, 0x9b, 0x94, 0x53, 0x61, 0x72, 0x6b,
  28419. 0xd7, 0x5e, 0x14, 0x9e, 0xf0, 0x98, 0x17, 0xf5,
  28420. 0x80
  28421. };
  28422. /* uncompressed test */
  28423. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  28424. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  28425. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  28426. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  28427. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  28428. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  28429. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  28430. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  28431. 0x80
  28432. };
  28433. /* compressed prefix */
  28434. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  28435. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  28436. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  28437. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  28438. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  28439. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  28440. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  28441. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  28442. 0x80
  28443. };
  28444. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  28445. 0xa8, 0x1b, 0x2e, 0x8a, 0x70, 0xa5, 0xac, 0x94,
  28446. 0xff, 0xdb, 0xcc, 0x9b, 0xad, 0xfc, 0x3f, 0xeb,
  28447. 0x08, 0x01, 0xf2, 0x58, 0x57, 0x8b, 0xb1, 0x14,
  28448. 0xad, 0x44, 0xec, 0xe1, 0xec, 0x0e, 0x79, 0x9d,
  28449. 0xa0, 0x8e, 0xff, 0xb8, 0x1c, 0x5d, 0x68, 0x5c,
  28450. 0x0c, 0x56, 0xf6, 0x4e, 0xec, 0xae, 0xf8, 0xcd,
  28451. 0xf1, 0x1c, 0xc3, 0x87, 0x37, 0x83, 0x8c, 0xf4,
  28452. 0x00
  28453. };
  28454. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  28455. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  28456. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  28457. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  28458. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  28459. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  28460. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  28461. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  28462. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  28463. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  28464. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  28465. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  28466. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  28467. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  28468. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  28469. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  28470. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  28471. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  28472. 0x26, 0x00
  28473. };
  28474. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  28475. 0x26, 0xb8, 0xf9, 0x17, 0x27, 0xbd, 0x62, 0x89,
  28476. 0x7a, 0xf1, 0x5e, 0x41, 0xeb, 0x43, 0xc3, 0x77,
  28477. 0xef, 0xb9, 0xc6, 0x10, 0xd4, 0x8f, 0x23, 0x35,
  28478. 0xcb, 0x0b, 0xd0, 0x08, 0x78, 0x10, 0xf4, 0x35,
  28479. 0x25, 0x41, 0xb1, 0x43, 0xc4, 0xb9, 0x81, 0xb7,
  28480. 0xe1, 0x8f, 0x62, 0xde, 0x8c, 0xcd, 0xf6, 0x33,
  28481. 0xfc, 0x1b, 0xf0, 0x37, 0xab, 0x7c, 0xd7, 0x79,
  28482. 0x80, 0x5e, 0x0d, 0xbc, 0xc0, 0xaa, 0xe1, 0xcb,
  28483. 0xce, 0xe1, 0xaf, 0xb2, 0xe0, 0x27, 0xdf, 0x36,
  28484. 0xbc, 0x04, 0xdc, 0xec, 0xbf, 0x15, 0x43, 0x36,
  28485. 0xc1, 0x9f, 0x0a, 0xf7, 0xe0, 0xa6, 0x47, 0x29,
  28486. 0x05, 0xe7, 0x99, 0xf1, 0x95, 0x3d, 0x2a, 0x0f,
  28487. 0xf3, 0x34, 0x8a, 0xb2, 0x1a, 0xa4, 0xad, 0xaf,
  28488. 0xd1, 0xd2, 0x34, 0x44, 0x1c, 0xf8, 0x07, 0xc0,
  28489. 0x3a, 0x00
  28490. };
  28491. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  28492. 0x7e, 0xee, 0xab, 0x7c, 0x4e, 0x50, 0xfb, 0x79,
  28493. 0x9b, 0x41, 0x8e, 0xe5, 0xe3, 0x19, 0x7f, 0xf6,
  28494. 0xbf, 0x15, 0xd4, 0x3a, 0x14, 0xc3, 0x43, 0x89,
  28495. 0xb5, 0x9d, 0xd1, 0xa7, 0xb1, 0xb8, 0x5b, 0x4a,
  28496. 0xe9, 0x04, 0x38, 0xac, 0xa6, 0x34, 0xbe, 0xa4,
  28497. 0x5e, 0x3a, 0x26, 0x95, 0xf1, 0x27, 0x0f, 0x07,
  28498. 0xfd, 0xcd, 0xf7, 0xc6, 0x2b, 0x8e, 0xfe, 0xaf,
  28499. 0x00, 0xb4, 0x5c, 0x2c, 0x96, 0xba, 0x45, 0x7e,
  28500. 0xb1, 0xa8, 0xbf, 0x07, 0x5a, 0x3d, 0xb2, 0x8e,
  28501. 0x5c, 0x24, 0xf6, 0xb9, 0x23, 0xed, 0x4a, 0xd7,
  28502. 0x47, 0xc3, 0xc9, 0xe0, 0x3c, 0x70, 0x79, 0xef,
  28503. 0xb8, 0x7c, 0xb1, 0x10, 0xd3, 0xa9, 0x98, 0x61,
  28504. 0xe7, 0x20, 0x03, 0xcb, 0xae, 0x6d, 0x6b, 0x8b,
  28505. 0x82, 0x7e, 0x4e, 0x6c, 0x14, 0x30, 0x64, 0xff,
  28506. 0x3c, 0x00
  28507. };
  28508. /* uncompressed test */
  28509. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  28510. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  28511. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  28512. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  28513. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  28514. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  28515. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  28516. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  28517. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  28518. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  28519. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  28520. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  28521. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  28522. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  28523. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  28524. 0x26, 0x00
  28525. };
  28526. /* compressed prefix */
  28527. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  28528. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  28529. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  28530. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  28531. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  28532. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  28533. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  28534. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  28535. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  28536. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  28537. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  28538. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  28539. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  28540. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  28541. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  28542. 0x26, 0x00
  28543. };
  28544. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  28545. 0xe3, 0x01, 0x34, 0x5a, 0x41, 0xa3, 0x9a, 0x4d,
  28546. 0x72, 0xff, 0xf8, 0xdf, 0x69, 0xc9, 0x80, 0x75,
  28547. 0xa0, 0xcc, 0x08, 0x2b, 0x80, 0x2f, 0xc9, 0xb2,
  28548. 0xb6, 0xbc, 0x50, 0x3f, 0x92, 0x6b, 0x65, 0xbd,
  28549. 0xdf, 0x7f, 0x4c, 0x8f, 0x1c, 0xb4, 0x9f, 0x63,
  28550. 0x96, 0xaf, 0xc8, 0xa7, 0x0a, 0xbe, 0x6d, 0x8a,
  28551. 0xef, 0x0d, 0xb4, 0x78, 0xd4, 0xc6, 0xb2, 0x97,
  28552. 0x00, 0x76, 0xc6, 0xa0, 0x48, 0x4f, 0xe7, 0x6d,
  28553. 0x76, 0xb3, 0xa9, 0x76, 0x25, 0xd7, 0x9f, 0x1c,
  28554. 0xe2, 0x40, 0xe7, 0xc5, 0x76, 0x75, 0x0d, 0x29,
  28555. 0x55, 0x28, 0x28, 0x6f, 0x71, 0x9b, 0x41, 0x3d,
  28556. 0xe9, 0xad, 0xa3, 0xe8, 0xeb, 0x78, 0xed, 0x57,
  28557. 0x36, 0x03, 0xce, 0x30, 0xd8, 0xbb, 0x76, 0x17,
  28558. 0x85, 0xdc, 0x30, 0xdb, 0xc3, 0x20, 0x86, 0x9e,
  28559. 0x1a, 0x00
  28560. };
  28561. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  28562. #define SIGSZ sizeof(sig1)
  28563. PEDANTIC_EXTENSION WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = { };
  28564. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = { 0x03 };
  28565. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = { 0x64, 0xa6, 0x5f, 0x3c, 0xde, 0xdc, 0xdd,
  28566. 0x66, 0x81, 0x1e, 0x29, 0x15 };
  28567. /* test of a 1023 byte long message */
  28568. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  28569. 0x6d, 0xdf, 0x80, 0x2e, 0x1a, 0xae, 0x49, 0x86,
  28570. 0x93, 0x5f, 0x7f, 0x98, 0x1b, 0xa3, 0xf0, 0x35,
  28571. 0x1d, 0x62, 0x73, 0xc0, 0xa0, 0xc2, 0x2c, 0x9c,
  28572. 0x0e, 0x83, 0x39, 0x16, 0x8e, 0x67, 0x54, 0x12,
  28573. 0xa3, 0xde, 0xbf, 0xaf, 0x43, 0x5e, 0xd6, 0x51,
  28574. 0x55, 0x80, 0x07, 0xdb, 0x43, 0x84, 0xb6, 0x50,
  28575. 0xfc, 0xc0, 0x7e, 0x3b, 0x58, 0x6a, 0x27, 0xa4,
  28576. 0xf7, 0xa0, 0x0a, 0xc8, 0xa6, 0xfe, 0xc2, 0xcd,
  28577. 0x86, 0xae, 0x4b, 0xf1, 0x57, 0x0c, 0x41, 0xe6,
  28578. 0xa4, 0x0c, 0x93, 0x1d, 0xb2, 0x7b, 0x2f, 0xaa,
  28579. 0x15, 0xa8, 0xce, 0xdd, 0x52, 0xcf, 0xf7, 0x36,
  28580. 0x2c, 0x4e, 0x6e, 0x23, 0xda, 0xec, 0x0f, 0xbc,
  28581. 0x3a, 0x79, 0xb6, 0x80, 0x6e, 0x31, 0x6e, 0xfc,
  28582. 0xc7, 0xb6, 0x81, 0x19, 0xbf, 0x46, 0xbc, 0x76,
  28583. 0xa2, 0x60, 0x67, 0xa5, 0x3f, 0x29, 0x6d, 0xaf,
  28584. 0xdb, 0xdc, 0x11, 0xc7, 0x7f, 0x77, 0x77, 0xe9,
  28585. 0x72, 0x66, 0x0c, 0xf4, 0xb6, 0xa9, 0xb3, 0x69,
  28586. 0xa6, 0x66, 0x5f, 0x02, 0xe0, 0xcc, 0x9b, 0x6e,
  28587. 0xdf, 0xad, 0x13, 0x6b, 0x4f, 0xab, 0xe7, 0x23,
  28588. 0xd2, 0x81, 0x3d, 0xb3, 0x13, 0x6c, 0xfd, 0xe9,
  28589. 0xb6, 0xd0, 0x44, 0x32, 0x2f, 0xee, 0x29, 0x47,
  28590. 0x95, 0x2e, 0x03, 0x1b, 0x73, 0xab, 0x5c, 0x60,
  28591. 0x33, 0x49, 0xb3, 0x07, 0xbd, 0xc2, 0x7b, 0xc6,
  28592. 0xcb, 0x8b, 0x8b, 0xbd, 0x7b, 0xd3, 0x23, 0x21,
  28593. 0x9b, 0x80, 0x33, 0xa5, 0x81, 0xb5, 0x9e, 0xad,
  28594. 0xeb, 0xb0, 0x9b, 0x3c, 0x4f, 0x3d, 0x22, 0x77,
  28595. 0xd4, 0xf0, 0x34, 0x36, 0x24, 0xac, 0xc8, 0x17,
  28596. 0x80, 0x47, 0x28, 0xb2, 0x5a, 0xb7, 0x97, 0x17,
  28597. 0x2b, 0x4c, 0x5c, 0x21, 0xa2, 0x2f, 0x9c, 0x78,
  28598. 0x39, 0xd6, 0x43, 0x00, 0x23, 0x2e, 0xb6, 0x6e,
  28599. 0x53, 0xf3, 0x1c, 0x72, 0x3f, 0xa3, 0x7f, 0xe3,
  28600. 0x87, 0xc7, 0xd3, 0xe5, 0x0b, 0xdf, 0x98, 0x13,
  28601. 0xa3, 0x0e, 0x5b, 0xb1, 0x2c, 0xf4, 0xcd, 0x93,
  28602. 0x0c, 0x40, 0xcf, 0xb4, 0xe1, 0xfc, 0x62, 0x25,
  28603. 0x92, 0xa4, 0x95, 0x88, 0x79, 0x44, 0x94, 0xd5,
  28604. 0x6d, 0x24, 0xea, 0x4b, 0x40, 0xc8, 0x9f, 0xc0,
  28605. 0x59, 0x6c, 0xc9, 0xeb, 0xb9, 0x61, 0xc8, 0xcb,
  28606. 0x10, 0xad, 0xde, 0x97, 0x6a, 0x5d, 0x60, 0x2b,
  28607. 0x1c, 0x3f, 0x85, 0xb9, 0xb9, 0xa0, 0x01, 0xed,
  28608. 0x3c, 0x6a, 0x4d, 0x3b, 0x14, 0x37, 0xf5, 0x20,
  28609. 0x96, 0xcd, 0x19, 0x56, 0xd0, 0x42, 0xa5, 0x97,
  28610. 0xd5, 0x61, 0xa5, 0x96, 0xec, 0xd3, 0xd1, 0x73,
  28611. 0x5a, 0x8d, 0x57, 0x0e, 0xa0, 0xec, 0x27, 0x22,
  28612. 0x5a, 0x2c, 0x4a, 0xaf, 0xf2, 0x63, 0x06, 0xd1,
  28613. 0x52, 0x6c, 0x1a, 0xf3, 0xca, 0x6d, 0x9c, 0xf5,
  28614. 0xa2, 0xc9, 0x8f, 0x47, 0xe1, 0xc4, 0x6d, 0xb9,
  28615. 0xa3, 0x32, 0x34, 0xcf, 0xd4, 0xd8, 0x1f, 0x2c,
  28616. 0x98, 0x53, 0x8a, 0x09, 0xeb, 0xe7, 0x69, 0x98,
  28617. 0xd0, 0xd8, 0xfd, 0x25, 0x99, 0x7c, 0x7d, 0x25,
  28618. 0x5c, 0x6d, 0x66, 0xec, 0xe6, 0xfa, 0x56, 0xf1,
  28619. 0x11, 0x44, 0x95, 0x0f, 0x02, 0x77, 0x95, 0xe6,
  28620. 0x53, 0x00, 0x8f, 0x4b, 0xd7, 0xca, 0x2d, 0xee,
  28621. 0x85, 0xd8, 0xe9, 0x0f, 0x3d, 0xc3, 0x15, 0x13,
  28622. 0x0c, 0xe2, 0xa0, 0x03, 0x75, 0xa3, 0x18, 0xc7,
  28623. 0xc3, 0xd9, 0x7b, 0xe2, 0xc8, 0xce, 0x5b, 0x6d,
  28624. 0xb4, 0x1a, 0x62, 0x54, 0xff, 0x26, 0x4f, 0xa6,
  28625. 0x15, 0x5b, 0xae, 0xe3, 0xb0, 0x77, 0x3c, 0x0f,
  28626. 0x49, 0x7c, 0x57, 0x3f, 0x19, 0xbb, 0x4f, 0x42,
  28627. 0x40, 0x28, 0x1f, 0x0b, 0x1f, 0x4f, 0x7b, 0xe8,
  28628. 0x57, 0xa4, 0xe5, 0x9d, 0x41, 0x6c, 0x06, 0xb4,
  28629. 0xc5, 0x0f, 0xa0, 0x9e, 0x18, 0x10, 0xdd, 0xc6,
  28630. 0xb1, 0x46, 0x7b, 0xae, 0xac, 0x5a, 0x36, 0x68,
  28631. 0xd1, 0x1b, 0x6e, 0xca, 0xa9, 0x01, 0x44, 0x00,
  28632. 0x16, 0xf3, 0x89, 0xf8, 0x0a, 0xcc, 0x4d, 0xb9,
  28633. 0x77, 0x02, 0x5e, 0x7f, 0x59, 0x24, 0x38, 0x8c,
  28634. 0x7e, 0x34, 0x0a, 0x73, 0x2e, 0x55, 0x44, 0x40,
  28635. 0xe7, 0x65, 0x70, 0xf8, 0xdd, 0x71, 0xb7, 0xd6,
  28636. 0x40, 0xb3, 0x45, 0x0d, 0x1f, 0xd5, 0xf0, 0x41,
  28637. 0x0a, 0x18, 0xf9, 0xa3, 0x49, 0x4f, 0x70, 0x7c,
  28638. 0x71, 0x7b, 0x79, 0xb4, 0xbf, 0x75, 0xc9, 0x84,
  28639. 0x00, 0xb0, 0x96, 0xb2, 0x16, 0x53, 0xb5, 0xd2,
  28640. 0x17, 0xcf, 0x35, 0x65, 0xc9, 0x59, 0x74, 0x56,
  28641. 0xf7, 0x07, 0x03, 0x49, 0x7a, 0x07, 0x87, 0x63,
  28642. 0x82, 0x9b, 0xc0, 0x1b, 0xb1, 0xcb, 0xc8, 0xfa,
  28643. 0x04, 0xea, 0xdc, 0x9a, 0x6e, 0x3f, 0x66, 0x99,
  28644. 0x58, 0x7a, 0x9e, 0x75, 0xc9, 0x4e, 0x5b, 0xab,
  28645. 0x00, 0x36, 0xe0, 0xb2, 0xe7, 0x11, 0x39, 0x2c,
  28646. 0xff, 0x00, 0x47, 0xd0, 0xd6, 0xb0, 0x5b, 0xd2,
  28647. 0xa5, 0x88, 0xbc, 0x10, 0x97, 0x18, 0x95, 0x42,
  28648. 0x59, 0xf1, 0xd8, 0x66, 0x78, 0xa5, 0x79, 0xa3,
  28649. 0x12, 0x0f, 0x19, 0xcf, 0xb2, 0x96, 0x3f, 0x17,
  28650. 0x7a, 0xeb, 0x70, 0xf2, 0xd4, 0x84, 0x48, 0x26,
  28651. 0x26, 0x2e, 0x51, 0xb8, 0x02, 0x71, 0x27, 0x20,
  28652. 0x68, 0xef, 0x5b, 0x38, 0x56, 0xfa, 0x85, 0x35,
  28653. 0xaa, 0x2a, 0x88, 0xb2, 0xd4, 0x1f, 0x2a, 0x0e,
  28654. 0x2f, 0xda, 0x76, 0x24, 0xc2, 0x85, 0x02, 0x72,
  28655. 0xac, 0x4a, 0x2f, 0x56, 0x1f, 0x8f, 0x2f, 0x7a,
  28656. 0x31, 0x8b, 0xfd, 0x5c, 0xaf, 0x96, 0x96, 0x14,
  28657. 0x9e, 0x4a, 0xc8, 0x24, 0xad, 0x34, 0x60, 0x53,
  28658. 0x8f, 0xdc, 0x25, 0x42, 0x1b, 0xee, 0xc2, 0xcc,
  28659. 0x68, 0x18, 0x16, 0x2d, 0x06, 0xbb, 0xed, 0x0c,
  28660. 0x40, 0xa3, 0x87, 0x19, 0x23, 0x49, 0xdb, 0x67,
  28661. 0xa1, 0x18, 0xba, 0xda, 0x6c, 0xd5, 0xab, 0x01,
  28662. 0x40, 0xee, 0x27, 0x32, 0x04, 0xf6, 0x28, 0xaa,
  28663. 0xd1, 0xc1, 0x35, 0xf7, 0x70, 0x27, 0x9a, 0x65,
  28664. 0x1e, 0x24, 0xd8, 0xc1, 0x4d, 0x75, 0xa6, 0x05,
  28665. 0x9d, 0x76, 0xb9, 0x6a, 0x6f, 0xd8, 0x57, 0xde,
  28666. 0xf5, 0xe0, 0xb3, 0x54, 0xb2, 0x7a, 0xb9, 0x37,
  28667. 0xa5, 0x81, 0x5d, 0x16, 0xb5, 0xfa, 0xe4, 0x07,
  28668. 0xff, 0x18, 0x22, 0x2c, 0x6d, 0x1e, 0xd2, 0x63,
  28669. 0xbe, 0x68, 0xc9, 0x5f, 0x32, 0xd9, 0x08, 0xbd,
  28670. 0x89, 0x5c, 0xd7, 0x62, 0x07, 0xae, 0x72, 0x64,
  28671. 0x87, 0x56, 0x7f, 0x9a, 0x67, 0xda, 0xd7, 0x9a,
  28672. 0xbe, 0xc3, 0x16, 0xf6, 0x83, 0xb1, 0x7f, 0x2d,
  28673. 0x02, 0xbf, 0x07, 0xe0, 0xac, 0x8b, 0x5b, 0xc6,
  28674. 0x16, 0x2c, 0xf9, 0x46, 0x97, 0xb3, 0xc2, 0x7c,
  28675. 0xd1, 0xfe, 0xa4, 0x9b, 0x27, 0xf2, 0x3b, 0xa2,
  28676. 0x90, 0x18, 0x71, 0x96, 0x25, 0x06, 0x52, 0x0c,
  28677. 0x39, 0x2d, 0xa8, 0xb6, 0xad, 0x0d, 0x99, 0xf7,
  28678. 0x01, 0x3f, 0xbc, 0x06, 0xc2, 0xc1, 0x7a, 0x56,
  28679. 0x95, 0x00, 0xc8, 0xa7, 0x69, 0x64, 0x81, 0xc1,
  28680. 0xcd, 0x33, 0xe9, 0xb1, 0x4e, 0x40, 0xb8, 0x2e,
  28681. 0x79, 0xa5, 0xf5, 0xdb, 0x82, 0x57, 0x1b, 0xa9,
  28682. 0x7b, 0xae, 0x3a, 0xd3, 0xe0, 0x47, 0x95, 0x15,
  28683. 0xbb, 0x0e, 0x2b, 0x0f, 0x3b, 0xfc, 0xd1, 0xfd,
  28684. 0x33, 0x03, 0x4e, 0xfc, 0x62, 0x45, 0xed, 0xdd,
  28685. 0x7e, 0xe2, 0x08, 0x6d, 0xda, 0xe2, 0x60, 0x0d,
  28686. 0x8c, 0xa7, 0x3e, 0x21, 0x4e, 0x8c, 0x2b, 0x0b,
  28687. 0xdb, 0x2b, 0x04, 0x7c, 0x6a, 0x46, 0x4a, 0x56,
  28688. 0x2e, 0xd7, 0x7b, 0x73, 0xd2, 0xd8, 0x41, 0xc4,
  28689. 0xb3, 0x49, 0x73, 0x55, 0x12, 0x57, 0x71, 0x3b,
  28690. 0x75, 0x36, 0x32, 0xef, 0xba, 0x34, 0x81, 0x69,
  28691. 0xab, 0xc9, 0x0a, 0x68, 0xf4, 0x26, 0x11, 0xa4,
  28692. 0x01, 0x26, 0xd7, 0xcb, 0x21, 0xb5, 0x86, 0x95,
  28693. 0x56, 0x81, 0x86, 0xf7, 0xe5, 0x69, 0xd2, 0xff,
  28694. 0x0f, 0x9e, 0x74, 0x5d, 0x04, 0x87, 0xdd, 0x2e,
  28695. 0xb9, 0x97, 0xca, 0xfc, 0x5a, 0xbf, 0x9d, 0xd1,
  28696. 0x02, 0xe6, 0x2f, 0xf6, 0x6c, 0xba, 0x87
  28697. };
  28698. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  28699. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  28700. sizeof(msg2),
  28701. sizeof(msg3),
  28702. 0 /*sizeof(msg1)*/,
  28703. 0 /*sizeof(msg1)*/,
  28704. sizeof(msg4)
  28705. };
  28706. #ifndef NO_ASN
  28707. static const byte privateEd448[] = {
  28708. 0x30, 0x47, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06,
  28709. 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04, 0x39,
  28710. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  28711. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  28712. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  28713. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  28714. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  28715. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  28716. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  28717. 0x5b
  28718. };
  28719. static const byte publicEd448[] = {
  28720. 0x30, 0x43, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65,
  28721. 0x71, 0x03, 0x3a, 0x00, 0x5f, 0xd7, 0x44, 0x9b,
  28722. 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec,
  28723. 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  28724. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d,
  28725. 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76,
  28726. 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d,
  28727. 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe,
  28728. 0xaf, 0xe8, 0x25, 0x61, 0x80
  28729. };
  28730. static const byte privPubEd448[] = {
  28731. 0x30, 0x81, 0x82, 0x02, 0x01, 0x00, 0x30, 0x05,
  28732. 0x06, 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04,
  28733. 0x39, 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d,
  28734. 0x10, 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e,
  28735. 0xbf, 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c,
  28736. 0x9f, 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48,
  28737. 0xa3, 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04,
  28738. 0x4e, 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f,
  28739. 0x8f, 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98,
  28740. 0xf9, 0x5b, 0x81, 0x39, 0x5f, 0xd7, 0x44, 0x9b,
  28741. 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec,
  28742. 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  28743. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d,
  28744. 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76,
  28745. 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d,
  28746. 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe,
  28747. 0xaf, 0xe8, 0x25, 0x61, 0x80
  28748. };
  28749. word32 idx;
  28750. #endif /* NO_ASN */
  28751. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  28752. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  28753. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28754. ed448_key *key3 = NULL;
  28755. #else
  28756. ed448_key key3[1];
  28757. #endif
  28758. #endif
  28759. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28760. key = (ed448_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28761. key2 = (ed448_key *)XMALLOC(sizeof(*key2), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28762. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  28763. key3 = (ed448_key *)XMALLOC(sizeof(*key3), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28764. #endif
  28765. #endif
  28766. /* create ed448 keys */
  28767. #ifndef HAVE_FIPS
  28768. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  28769. #else
  28770. ret = wc_InitRng(&rng);
  28771. #endif
  28772. if (ret != 0) {
  28773. XMEMSET(&rng, 0, sizeof(rng));
  28774. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  28775. }
  28776. ret = wc_ed448_init(key);
  28777. if (ret < 0)
  28778. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  28779. ret = wc_ed448_init(key2);
  28780. if (ret < 0)
  28781. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  28782. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  28783. ret = wc_ed448_init(key3);
  28784. if (ret < 0)
  28785. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  28786. #endif
  28787. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, key);
  28788. if (ret < 0)
  28789. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  28790. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, key2);
  28791. if (ret < 0)
  28792. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  28793. /* helper functions for signature and key size */
  28794. keySz = wc_ed448_size(key);
  28795. sigSz = wc_ed448_sig_size(key);
  28796. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  28797. defined(HAVE_ED448_KEY_IMPORT)
  28798. for (i = 0; i < 6; i++) {
  28799. outlen = sizeof(out);
  28800. XMEMSET(out, 0, sizeof(out));
  28801. if (wc_ed448_import_private_key(sKeys[i], ED448_KEY_SIZE, pKeys[i],
  28802. pKeySz[i], key) != 0)
  28803. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  28804. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, key, NULL,
  28805. 0) != 0)
  28806. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  28807. if (XMEMCMP(out, sigs[i], 114))
  28808. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  28809. #if defined(HAVE_ED448_VERIFY)
  28810. /* test verify on good msg */
  28811. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key,
  28812. NULL, 0) != 0 || verify != 1)
  28813. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  28814. #ifdef WOLFSSL_ED448_STREAMING_VERIFY
  28815. /* test verify on good msg using streaming interface directly */
  28816. if (wc_ed448_verify_msg_init(out, outlen,
  28817. key, (byte)Ed448, NULL, 0) != 0)
  28818. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  28819. for (j = 0; j < msgSz[i]; j += i) {
  28820. if (wc_ed448_verify_msg_update(msgs[i] + j, MIN(i, msgSz[i] - j), key) != 0)
  28821. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  28822. }
  28823. if (wc_ed448_verify_msg_final(out, outlen, &verify,
  28824. key) != 0 || verify != 1)
  28825. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  28826. #endif /* WOLFSSL_ED448_STREAMING_VERIFY */
  28827. /* test verify on bad msg */
  28828. out[outlen-2] = out[outlen-2] + 1;
  28829. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key,
  28830. NULL, 0) == 0 || verify == 1)
  28831. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  28832. #endif /* HAVE_ED448_VERIFY */
  28833. /* test api for import/exporting keys */
  28834. {
  28835. byte *exportPKey = NULL;
  28836. byte *exportSKey = NULL;
  28837. word32 exportPSz = ED448_KEY_SIZE;
  28838. word32 exportSSz = ED448_KEY_SIZE;
  28839. exportPKey = (byte *)XMALLOC(exportPSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28840. exportSKey = (byte *)XMALLOC(exportSSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28841. if ((exportPKey == NULL) || (exportSKey == NULL))
  28842. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  28843. ret = 0;
  28844. do {
  28845. if (wc_ed448_export_public(key, exportPKey, &exportPSz) != 0) {
  28846. ret = WC_TEST_RET_ENC_I(i);
  28847. break;
  28848. }
  28849. if (wc_ed448_import_public_ex(exportPKey, exportPSz, key2, 1) != 0) {
  28850. ret = WC_TEST_RET_ENC_I(i);
  28851. break;
  28852. }
  28853. if (wc_ed448_export_private_only(key, exportSKey, &exportSSz) != 0) {
  28854. ret = WC_TEST_RET_ENC_I(i);
  28855. break;
  28856. }
  28857. if (wc_ed448_import_private_key(exportSKey, exportSSz,
  28858. exportPKey, exportPSz, key2) != 0) {
  28859. ret = WC_TEST_RET_ENC_I(i);
  28860. break;
  28861. }
  28862. /* clear "out" buffer and test sign with imported keys */
  28863. outlen = sizeof(out);
  28864. XMEMSET(out, 0, sizeof(out));
  28865. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, key2, NULL,
  28866. 0) != 0) {
  28867. ret = WC_TEST_RET_ENC_I(i);
  28868. break;
  28869. }
  28870. } while(0);
  28871. XFREE(exportPKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28872. XFREE(exportSKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28873. if (ret != 0)
  28874. goto out;
  28875. }
  28876. #if defined(HAVE_ED448_VERIFY)
  28877. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key2,
  28878. NULL, 0) != 0 || verify != 1)
  28879. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  28880. if (XMEMCMP(out, sigs[i], SIGSZ))
  28881. ERROR_OUT(WC_TEST_RET_ENC_I(i), out);
  28882. #endif /* HAVE_ED448_VERIFY */
  28883. }
  28884. ret = ed448_ctx_test();
  28885. if (ret != 0)
  28886. goto out;
  28887. ret = ed448ph_test();
  28888. if (ret != 0)
  28889. goto out;
  28890. #ifndef NO_ASN
  28891. /* Try ASN.1 encoded private-only key and public key. */
  28892. idx = 0;
  28893. ret = wc_Ed448PrivateKeyDecode(privateEd448, &idx, key3,
  28894. sizeof(privateEd448));
  28895. if (ret != 0)
  28896. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  28897. ret = wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0);
  28898. if (ret != BAD_FUNC_ARG)
  28899. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  28900. idx = 0;
  28901. ret = wc_Ed448PublicKeyDecode(publicEd448, &idx, key3, sizeof(publicEd448));
  28902. if (ret != 0)
  28903. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  28904. ret = wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0);
  28905. if (ret != 0)
  28906. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  28907. if (XMEMCMP(out, sigs[0], SIGSZ))
  28908. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  28909. #if defined(HAVE_ED448_VERIFY)
  28910. /* test verify on good msg */
  28911. ret = wc_ed448_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, key3,
  28912. NULL, 0);
  28913. if (ret != 0 || verify != 1)
  28914. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  28915. #endif /* HAVE_ED448_VERIFY */
  28916. wc_ed448_free(key3);
  28917. ret = wc_ed448_init(key3);
  28918. if (ret < 0)
  28919. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  28920. idx = 0;
  28921. ret = wc_Ed448PrivateKeyDecode(privPubEd448, &idx, key3,
  28922. sizeof(privPubEd448));
  28923. if (ret != 0)
  28924. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  28925. ret = wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0);
  28926. if (ret != 0)
  28927. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  28928. if (XMEMCMP(out, sigs[0], SIGSZ))
  28929. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  28930. #endif /* NO_ASN */
  28931. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  28932. ret = 0;
  28933. out:
  28934. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  28935. if (key) {
  28936. wc_ed448_free(key);
  28937. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28938. }
  28939. if (key2) {
  28940. wc_ed448_free(key2);
  28941. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28942. }
  28943. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  28944. if (key3) {
  28945. wc_ed448_free(key3);
  28946. XFREE(key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28947. }
  28948. #endif
  28949. #else
  28950. wc_ed448_free(key);
  28951. wc_ed448_free(key2);
  28952. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  28953. wc_ed448_free(key3);
  28954. #endif
  28955. #endif
  28956. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  28957. wc_FreeRng(&rng);
  28958. #endif
  28959. if (ret < 0)
  28960. return ret;
  28961. /* hush warnings of unused keySz and sigSz */
  28962. (void)keySz;
  28963. (void)sigSz;
  28964. #ifdef WOLFSSL_TEST_CERT
  28965. ret = ed448_test_cert();
  28966. if (ret < 0)
  28967. return ret;
  28968. #ifdef WOLFSSL_CERT_GEN
  28969. ret = ed448_test_make_cert();
  28970. if (ret < 0)
  28971. return ret;
  28972. #endif /* WOLFSSL_CERT_GEN */
  28973. #endif /* WOLFSSL_TEST_CERT */
  28974. return 0;
  28975. }
  28976. #endif /* HAVE_ED448 */
  28977. #ifdef WOLFSSL_HAVE_KYBER
  28978. #ifdef WOLFSSL_WC_KYBER /* OQS and PQM4 do not support KATs */
  28979. #ifdef WOLFSSL_KYBER512
  28980. static wc_test_ret_t kyber512_kat(void)
  28981. {
  28982. KyberKey key;
  28983. wc_test_ret_t ret;
  28984. byte priv[KYBER512_PRIVATE_KEY_SIZE];
  28985. byte pub[KYBER512_PUBLIC_KEY_SIZE];
  28986. byte ct[KYBER512_CIPHER_TEXT_SIZE];
  28987. byte ss[KYBER_SS_SZ];
  28988. byte ss_dec[KYBER_SS_SZ];
  28989. const byte kyber512_rand[] = {
  28990. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  28991. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  28992. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  28993. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  28994. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  28995. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  28996. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  28997. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  28998. };
  28999. const byte kyber512enc_rand[] = {
  29000. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  29001. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  29002. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  29003. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  29004. };
  29005. const byte kyber512_pk[] = {
  29006. 0x11, 0x5A, 0xCE, 0x0E, 0x64, 0x67, 0x7C, 0xBB,
  29007. 0x7D, 0xCF, 0xC9, 0x3C, 0x16, 0xD3, 0xA3, 0x05,
  29008. 0xF6, 0x76, 0x15, 0xA4, 0x88, 0xD7, 0x11, 0xAA,
  29009. 0x56, 0x69, 0x8C, 0x56, 0x63, 0xAB, 0x7A, 0xC9,
  29010. 0xCE, 0x66, 0xD5, 0x47, 0xC0, 0x59, 0x5F, 0x98,
  29011. 0xA4, 0x3F, 0x46, 0x50, 0xBB, 0xE0, 0x8C, 0x36,
  29012. 0x4D, 0x97, 0x67, 0x89, 0x11, 0x7D, 0x34, 0xF6,
  29013. 0xAE, 0x51, 0xAC, 0x06, 0x3C, 0xB5, 0x5C, 0x6C,
  29014. 0xA3, 0x25, 0x58, 0x22, 0x7D, 0xFE, 0xF8, 0x07,
  29015. 0xD1, 0x9C, 0x30, 0xDE, 0x41, 0x44, 0x24, 0x09,
  29016. 0x7F, 0x6A, 0xA2, 0x36, 0xA1, 0x05, 0x3B, 0x4A,
  29017. 0x07, 0xA7, 0x6B, 0xE3, 0x72, 0xA5, 0xC6, 0xB6,
  29018. 0x00, 0x27, 0x91, 0xEB, 0xE0, 0xAF, 0xDA, 0xF5,
  29019. 0x4E, 0x1C, 0xA2, 0x37, 0xFF, 0x54, 0x5B, 0xA6,
  29020. 0x83, 0x43, 0xE7, 0x45, 0xC0, 0x4A, 0xD1, 0x63,
  29021. 0x9D, 0xBC, 0x59, 0x03, 0x46, 0xB6, 0xB9, 0x56,
  29022. 0x9B, 0x56, 0xDB, 0xBF, 0xE5, 0x31, 0x51, 0x91,
  29023. 0x30, 0x66, 0xE5, 0xC8, 0x55, 0x27, 0xDC, 0x94,
  29024. 0x68, 0x11, 0x0A, 0x13, 0x6A, 0x41, 0x14, 0x97,
  29025. 0xC2, 0x27, 0xDC, 0xB8, 0xC9, 0xB2, 0x55, 0x70,
  29026. 0xB7, 0xA0, 0xE4, 0x2A, 0xAD, 0xA6, 0x70, 0x9F,
  29027. 0x23, 0x20, 0x8F, 0x5D, 0x49, 0x6E, 0xBA, 0xB7,
  29028. 0x84, 0x3F, 0x64, 0x83, 0xBF, 0x0C, 0x0C, 0x73,
  29029. 0xA4, 0x02, 0x96, 0xEC, 0x2C, 0x64, 0x40, 0x00,
  29030. 0x13, 0x94, 0xC9, 0x9C, 0xA1, 0x73, 0xD5, 0xC7,
  29031. 0x75, 0xB7, 0xF4, 0x15, 0xD0, 0x2A, 0x5A, 0x26,
  29032. 0xA0, 0x74, 0x07, 0x91, 0x85, 0x87, 0xC4, 0x11,
  29033. 0x69, 0xF2, 0xB7, 0x17, 0x87, 0x55, 0xAC, 0xC2,
  29034. 0x7F, 0xC8, 0xB1, 0x9C, 0x4C, 0x4B, 0x3F, 0xCD,
  29035. 0x41, 0x05, 0x3F, 0x2C, 0x74, 0xC8, 0xA1, 0x0A,
  29036. 0x83, 0x21, 0x24, 0x1B, 0x28, 0x02, 0x43, 0x28,
  29037. 0x75, 0xAE, 0x80, 0x8B, 0x9E, 0xF1, 0x36, 0x5C,
  29038. 0x7B, 0x8A, 0x52, 0x90, 0x2F, 0x13, 0x17, 0xBA,
  29039. 0x2F, 0xB0, 0x26, 0x9F, 0x47, 0x93, 0x06, 0x72,
  29040. 0x10, 0x7B, 0x47, 0x26, 0xFE, 0xF6, 0x45, 0x47,
  29041. 0x39, 0x4D, 0x33, 0x20, 0xC8, 0xF1, 0x20, 0xB3,
  29042. 0xC2, 0xF4, 0x72, 0x5B, 0x03, 0x05, 0xFA, 0xB8,
  29043. 0x8C, 0xC7, 0x98, 0x1F, 0xCB, 0x09, 0xA7, 0x6A,
  29044. 0x1C, 0xBF, 0x7F, 0x17, 0x9F, 0x43, 0xBB, 0x0A,
  29045. 0x4C, 0x8B, 0x05, 0x90, 0x85, 0x7F, 0x1E, 0x69,
  29046. 0x70, 0x84, 0x66, 0xC7, 0xF8, 0x60, 0x73, 0x91,
  29047. 0xE7, 0xBC, 0x52, 0x68, 0xBF, 0xD3, 0xD7, 0xA1,
  29048. 0xDF, 0xFC, 0xB4, 0xEC, 0xA2, 0xA1, 0xC9, 0xB5,
  29049. 0x97, 0x59, 0x30, 0x13, 0xD5, 0xFC, 0x42, 0x02,
  29050. 0xEC, 0x2B, 0x74, 0xE5, 0x7A, 0xB7, 0x6B, 0xBC,
  29051. 0xF3, 0x63, 0x2B, 0xBA, 0xF9, 0x7C, 0xDC, 0x41,
  29052. 0x8A, 0x6F, 0x16, 0x39, 0x28, 0x38, 0xCA, 0x9B,
  29053. 0xF4, 0x5D, 0xDF, 0x02, 0x37, 0x77, 0xB7, 0x56,
  29054. 0x18, 0x33, 0xC1, 0x05, 0x19, 0x0F, 0x94, 0xF3,
  29055. 0x02, 0xC5, 0x9B, 0x53, 0x19, 0x00, 0xBB, 0xC8,
  29056. 0x16, 0x36, 0x1F, 0xAA, 0x5B, 0x33, 0x80, 0xCA,
  29057. 0x3A, 0x89, 0x31, 0x04, 0xCA, 0x73, 0x88, 0xB1,
  29058. 0x85, 0x67, 0x1B, 0x3E, 0x5F, 0xE3, 0x79, 0x0E,
  29059. 0x9A, 0x62, 0x6E, 0xC4, 0x6D, 0x9B, 0x0B, 0x33,
  29060. 0xC7, 0xA4, 0x19, 0xAF, 0x7B, 0x32, 0xB6, 0x85,
  29061. 0x98, 0x94, 0xF5, 0x75, 0xD8, 0x2A, 0xC5, 0x45,
  29062. 0x6B, 0x54, 0x90, 0xA7, 0xAF, 0x8F, 0xE6, 0x10,
  29063. 0x46, 0x36, 0x05, 0x89, 0xEC, 0xBA, 0x72, 0x44,
  29064. 0x23, 0x6F, 0x41, 0x23, 0x11, 0x6B, 0x61, 0x74,
  29065. 0xAA, 0x17, 0x92, 0x49, 0xA4, 0x91, 0x95, 0xB3,
  29066. 0x56, 0xC7, 0x2F, 0xC6, 0x64, 0x1F, 0x02, 0x51,
  29067. 0x81, 0x2E, 0xAA, 0x98, 0x57, 0x0B, 0x04, 0x66,
  29068. 0x99, 0x07, 0x0E, 0x08, 0x19, 0xDC, 0x27, 0x13,
  29069. 0xF4, 0x69, 0x13, 0x7D, 0xFC, 0x6A, 0x3D, 0x7B,
  29070. 0x92, 0xB2, 0x98, 0x99, 0x5E, 0xE7, 0x80, 0x36,
  29071. 0x91, 0x53, 0xAC, 0x36, 0x6B, 0x06, 0xD7, 0x24,
  29072. 0x9C, 0xD0, 0x9E, 0x1B, 0x33, 0x78, 0xFB, 0x04,
  29073. 0x39, 0x9C, 0xEC, 0xB8, 0x65, 0x05, 0x81, 0xD6,
  29074. 0x37, 0xC7, 0x9A, 0xE6, 0x7D, 0x6F, 0x2C, 0xAF,
  29075. 0x6A, 0xBA, 0xCF, 0x59, 0x81, 0x59, 0xA7, 0x79,
  29076. 0x2C, 0xB3, 0xC9, 0x71, 0xD1, 0x49, 0x9D, 0x23,
  29077. 0x73, 0xAD, 0x20, 0xF6, 0x3F, 0x03, 0xBB, 0x59,
  29078. 0xED, 0x13, 0x73, 0x84, 0xAC, 0x61, 0xA7, 0x15,
  29079. 0x51, 0x43, 0xB8, 0xCA, 0x49, 0x32, 0x61, 0x2E,
  29080. 0xC9, 0x15, 0xE4, 0xCA, 0x34, 0x6A, 0x9B, 0xCE,
  29081. 0x5D, 0xD6, 0x04, 0x17, 0xC6, 0xB2, 0xA8, 0x9B,
  29082. 0x1C, 0xC4, 0x35, 0x64, 0x3F, 0x87, 0x5B, 0xDC,
  29083. 0x5A, 0x7E, 0x5B, 0x34, 0x81, 0xCF, 0x91, 0x9E,
  29084. 0xA0, 0x91, 0x72, 0xFE, 0xBC, 0x46, 0xD4, 0xFC,
  29085. 0x3F, 0xB0, 0xCB, 0x95, 0x91, 0x70, 0x4E, 0xE2,
  29086. 0xDB, 0xB6, 0x18, 0x44, 0xB2, 0xF3, 0x31, 0x4A,
  29087. 0x06, 0xBB, 0x6C, 0x6D, 0x34, 0x00, 0x5E, 0x48,
  29088. 0x5C, 0xE6, 0x67, 0xBD, 0xC7, 0xD0, 0x98, 0x58,
  29089. 0x69, 0x28, 0xD2, 0xD9, 0x13, 0x40, 0xF0, 0x04,
  29090. 0x19, 0xEA, 0x40, 0x13, 0x51, 0xA2, 0x40, 0xA0,
  29091. 0xB0, 0x41, 0x05, 0x8B, 0xEF, 0xB0, 0xC2, 0xFD,
  29092. 0x32, 0x64, 0x5B, 0x7A, 0x2D, 0xF8, 0xF5, 0xCB,
  29093. 0xFD, 0x87, 0x33, 0x27, 0xC9, 0x78, 0xD7, 0xB3,
  29094. 0x51, 0xA2, 0x80, 0x88, 0x43, 0x88, 0x37, 0x02,
  29095. 0x4C, 0x52, 0xB9, 0xC2, 0x95, 0xCD, 0x71, 0x36,
  29096. 0x46, 0xFB, 0x5D, 0x6C, 0x0C, 0xCF, 0xB4, 0x70,
  29097. 0x73, 0x4A, 0xC2, 0xB2, 0xBC, 0x81, 0x23, 0xC2,
  29098. 0xC1, 0x3D, 0xF6, 0x93, 0x8E, 0x92, 0x45, 0x5A,
  29099. 0x86, 0x26, 0x39, 0xFE, 0xB8, 0xA6, 0x4B, 0x85,
  29100. 0x16, 0x3E, 0x32, 0x70, 0x7E, 0x03, 0x7B, 0x38,
  29101. 0xD8, 0xAC, 0x39, 0x22, 0xB4, 0x51, 0x87, 0xBB,
  29102. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  29103. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  29104. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  29105. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  29106. };
  29107. const byte kyber512_sk[] = {
  29108. 0x6C, 0x89, 0x2B, 0x02, 0x97, 0xA9, 0xC7, 0x64,
  29109. 0x14, 0x93, 0xF8, 0x7D, 0xAF, 0x35, 0x33, 0xEE,
  29110. 0xD6, 0x1F, 0x07, 0xF4, 0x65, 0x20, 0x66, 0x33,
  29111. 0x7E, 0xD7, 0x40, 0x46, 0xDC, 0xC7, 0x1B, 0xA0,
  29112. 0x3F, 0x30, 0x96, 0x01, 0x03, 0x16, 0x1F, 0x7D,
  29113. 0xEB, 0x53, 0xA7, 0x1B, 0x11, 0x61, 0x72, 0x63,
  29114. 0xFE, 0x2A, 0x80, 0x97, 0x69, 0xCE, 0x6D, 0x70,
  29115. 0xA8, 0x5F, 0xE6, 0x00, 0xEC, 0xE2, 0x9D, 0x7F,
  29116. 0x36, 0xA1, 0x6D, 0x33, 0x1B, 0x8B, 0x2A, 0x9E,
  29117. 0x1D, 0xB8, 0xC0, 0x90, 0x74, 0x2D, 0xF0, 0x73,
  29118. 0x9F, 0xF0, 0x60, 0xCE, 0xB4, 0xEC, 0xC5, 0xAB,
  29119. 0x1C, 0x5E, 0x55, 0xAC, 0x97, 0xBB, 0x66, 0xA7,
  29120. 0xF8, 0x95, 0x10, 0x5D, 0x57, 0x78, 0x2B, 0x22,
  29121. 0x95, 0x38, 0xE3, 0x42, 0x15, 0x44, 0xA3, 0x42,
  29122. 0x14, 0x08, 0xDB, 0xF4, 0x49, 0x10, 0x93, 0x4C,
  29123. 0xC4, 0x23, 0x77, 0x4F, 0x16, 0x76, 0xFF, 0x1C,
  29124. 0x30, 0x6F, 0x97, 0x55, 0x5F, 0x57, 0xB4, 0xAE,
  29125. 0xD7, 0xA6, 0xBA, 0xB9, 0x50, 0xA8, 0x16, 0x3C,
  29126. 0x8D, 0x31, 0x8D, 0xEA, 0x62, 0x75, 0x1B, 0xD6,
  29127. 0xAB, 0xC5, 0x06, 0x9C, 0x06, 0xC8, 0x8F, 0x33,
  29128. 0x00, 0x26, 0xA1, 0x98, 0x06, 0xA0, 0x3B, 0x97,
  29129. 0xA7, 0x69, 0x6B, 0x56, 0xDA, 0x21, 0x82, 0x7B,
  29130. 0xB4, 0xE8, 0xDC, 0x03, 0x11, 0x52, 0xB4, 0x1B,
  29131. 0x89, 0x2A, 0x9E, 0x99, 0xAD, 0xF6, 0xE1, 0x96,
  29132. 0x3E, 0x96, 0x57, 0x88, 0x28, 0x15, 0x4F, 0x46,
  29133. 0x70, 0x33, 0x84, 0x69, 0x20, 0xFB, 0xB4, 0xB8,
  29134. 0x05, 0x44, 0xE7, 0xE8, 0xA8, 0x1A, 0xE9, 0x63,
  29135. 0xCF, 0x36, 0x8C, 0x9B, 0xA0, 0x37, 0xA8, 0xC2,
  29136. 0xAD, 0x62, 0xE3, 0x2B, 0x6E, 0x61, 0xC9, 0x1D,
  29137. 0x75, 0xCE, 0x00, 0x5A, 0xB3, 0x0F, 0x80, 0x99,
  29138. 0xA1, 0xF2, 0x9D, 0x7B, 0x63, 0x05, 0xB4, 0xDC,
  29139. 0x06, 0xE2, 0x56, 0x80, 0xBB, 0x00, 0x99, 0x2F,
  29140. 0x71, 0x7F, 0xE6, 0xC1, 0x15, 0xA8, 0x08, 0x42,
  29141. 0x31, 0xCC, 0x79, 0xDD, 0x70, 0x0E, 0xA6, 0x91,
  29142. 0x2A, 0xC7, 0xFA, 0x0D, 0x93, 0x7B, 0xB6, 0xA7,
  29143. 0x56, 0x66, 0x22, 0x30, 0x47, 0x0C, 0x18, 0x9B,
  29144. 0x5A, 0xA1, 0x65, 0x3D, 0xEB, 0x93, 0x7D, 0x5A,
  29145. 0x9C, 0x25, 0xA2, 0x1D, 0x93, 0xB1, 0x90, 0x74,
  29146. 0xFC, 0x23, 0x9D, 0x81, 0x53, 0x53, 0x97, 0x97,
  29147. 0xC7, 0xD4, 0xAB, 0x62, 0x64, 0x9D, 0x76, 0xAA,
  29148. 0x55, 0x37, 0x36, 0xA9, 0x49, 0x02, 0x2C, 0x22,
  29149. 0xC5, 0x2B, 0xAE, 0xEC, 0x60, 0x5B, 0x32, 0xCE,
  29150. 0x9E, 0x5B, 0x93, 0x84, 0x90, 0x35, 0x58, 0xCA,
  29151. 0x9D, 0x6A, 0x3A, 0xBA, 0x90, 0x42, 0x3E, 0xED,
  29152. 0xA0, 0x1C, 0x94, 0x19, 0x8B, 0x19, 0x2A, 0x8B,
  29153. 0xA9, 0x06, 0x34, 0x97, 0xA0, 0xC5, 0x01, 0x33,
  29154. 0x07, 0xDD, 0xD8, 0x63, 0x52, 0x64, 0x71, 0xA4,
  29155. 0xD9, 0x95, 0x23, 0xEB, 0x41, 0x7F, 0x29, 0x1A,
  29156. 0xAC, 0x0C, 0x3A, 0x58, 0x1B, 0x6D, 0xA0, 0x07,
  29157. 0x32, 0xE5, 0xE8, 0x1B, 0x1F, 0x7C, 0x87, 0x9B,
  29158. 0x16, 0x93, 0xC1, 0x3B, 0x6F, 0x9F, 0x79, 0x31,
  29159. 0x62, 0x24, 0x29, 0xE5, 0x42, 0xAF, 0x40, 0x69,
  29160. 0x22, 0x2F, 0x04, 0x55, 0x44, 0xE0, 0xCC, 0x4F,
  29161. 0xB2, 0x4D, 0x44, 0x48, 0xCF, 0x2C, 0x65, 0x96,
  29162. 0xF5, 0xCB, 0x08, 0x62, 0x4B, 0x11, 0x85, 0x01,
  29163. 0x3B, 0x6B, 0x02, 0x08, 0x92, 0xF9, 0x6B, 0xDF,
  29164. 0xD4, 0xAD, 0xA9, 0x17, 0x9D, 0xE7, 0x27, 0xB8,
  29165. 0xD9, 0x42, 0x6E, 0x09, 0x96, 0xB5, 0xD3, 0x49,
  29166. 0x48, 0xCE, 0x02, 0xD0, 0xC3, 0x69, 0xB3, 0x7C,
  29167. 0xBB, 0x54, 0xD3, 0x47, 0x9E, 0xD8, 0xB5, 0x82,
  29168. 0xE9, 0xE7, 0x28, 0x92, 0x9B, 0x4C, 0x71, 0xC9,
  29169. 0xBE, 0x11, 0xD4, 0x5B, 0x20, 0xC4, 0xBD, 0xC3,
  29170. 0xC7, 0x43, 0x13, 0x22, 0x3F, 0x58, 0x27, 0x4E,
  29171. 0x8B, 0xA5, 0x24, 0x44, 0x47, 0xC4, 0x95, 0x95,
  29172. 0x0B, 0x84, 0xCB, 0x0C, 0x3C, 0x27, 0x36, 0x40,
  29173. 0x10, 0x8A, 0x33, 0x97, 0x94, 0x45, 0x73, 0x27,
  29174. 0x93, 0x28, 0x99, 0x6C, 0xDC, 0x0C, 0x91, 0x3C,
  29175. 0x95, 0x8A, 0xD6, 0x20, 0xBA, 0x8B, 0x5E, 0x5E,
  29176. 0xCB, 0xBB, 0x7E, 0x13, 0xCB, 0x9C, 0x70, 0xBD,
  29177. 0x5A, 0xB3, 0x0E, 0xB7, 0x48, 0x8C, 0x97, 0x00,
  29178. 0x1C, 0x20, 0x49, 0x8F, 0x1D, 0x7C, 0xC0, 0x6D,
  29179. 0xA7, 0x6B, 0xF5, 0x20, 0xC6, 0x58, 0xCC, 0xAD,
  29180. 0xFA, 0x29, 0x56, 0x42, 0x45, 0x57, 0xAB, 0xEA,
  29181. 0x8A, 0xB8, 0x92, 0x39, 0xC1, 0x78, 0x33, 0xDC,
  29182. 0x3A, 0x49, 0xB3, 0x6A, 0x9A, 0xE9, 0xA4, 0x86,
  29183. 0x94, 0x05, 0x40, 0xEB, 0x44, 0x4F, 0x97, 0x15,
  29184. 0x23, 0x57, 0xE0, 0x20, 0x35, 0x93, 0x9D, 0x75,
  29185. 0xA3, 0xC0, 0x25, 0xF4, 0x1A, 0x40, 0x08, 0x23,
  29186. 0x82, 0xA0, 0x73, 0x3C, 0x39, 0xB0, 0x62, 0x2B,
  29187. 0x74, 0x0E, 0x40, 0x75, 0x92, 0xC6, 0x2E, 0xCA,
  29188. 0xEB, 0x14, 0x32, 0xC4, 0x45, 0xB3, 0x70, 0x3A,
  29189. 0x86, 0xF6, 0x98, 0x1A, 0x27, 0x81, 0x57, 0xEA,
  29190. 0x95, 0xA6, 0xE9, 0x2D, 0x55, 0xE4, 0xB9, 0x72,
  29191. 0xF9, 0x36, 0xC2, 0xF0, 0xA6, 0x58, 0x28, 0x0E,
  29192. 0xA2, 0xB0, 0x7A, 0x48, 0x99, 0x2D, 0xF8, 0x93,
  29193. 0x7E, 0x0A, 0x2A, 0xC1, 0xDC, 0xC9, 0x74, 0xFE,
  29194. 0x00, 0xAA, 0xE1, 0xF5, 0x61, 0xFA, 0x25, 0x8E,
  29195. 0x2D, 0x25, 0x9C, 0x3E, 0x86, 0x1D, 0xCE, 0x23,
  29196. 0x60, 0x39, 0x12, 0x76, 0x06, 0xFC, 0x1C, 0xE0,
  29197. 0x09, 0x00, 0x3A, 0x7B, 0xAC, 0x94, 0x21, 0x01,
  29198. 0xDC, 0xB8, 0x22, 0xB1, 0xF3, 0xC1, 0x2B, 0xF7,
  29199. 0x32, 0x38, 0xF5, 0x46, 0xE0, 0x1C, 0x36, 0xB5,
  29200. 0xA6, 0x93, 0x61, 0x92, 0x99, 0x5C, 0xC6, 0x9C,
  29201. 0x63, 0x23, 0x74, 0x09, 0xCB, 0x53, 0xC2, 0xE3,
  29202. 0x5D, 0x74, 0x89, 0x0D, 0x18, 0x88, 0x53, 0x76,
  29203. 0xFA, 0x55, 0x03, 0xB1, 0x07, 0xA2, 0xA3, 0x92,
  29204. 0x11, 0x5A, 0xCE, 0x0E, 0x64, 0x67, 0x7C, 0xBB,
  29205. 0x7D, 0xCF, 0xC9, 0x3C, 0x16, 0xD3, 0xA3, 0x05,
  29206. 0xF6, 0x76, 0x15, 0xA4, 0x88, 0xD7, 0x11, 0xAA,
  29207. 0x56, 0x69, 0x8C, 0x56, 0x63, 0xAB, 0x7A, 0xC9,
  29208. 0xCE, 0x66, 0xD5, 0x47, 0xC0, 0x59, 0x5F, 0x98,
  29209. 0xA4, 0x3F, 0x46, 0x50, 0xBB, 0xE0, 0x8C, 0x36,
  29210. 0x4D, 0x97, 0x67, 0x89, 0x11, 0x7D, 0x34, 0xF6,
  29211. 0xAE, 0x51, 0xAC, 0x06, 0x3C, 0xB5, 0x5C, 0x6C,
  29212. 0xA3, 0x25, 0x58, 0x22, 0x7D, 0xFE, 0xF8, 0x07,
  29213. 0xD1, 0x9C, 0x30, 0xDE, 0x41, 0x44, 0x24, 0x09,
  29214. 0x7F, 0x6A, 0xA2, 0x36, 0xA1, 0x05, 0x3B, 0x4A,
  29215. 0x07, 0xA7, 0x6B, 0xE3, 0x72, 0xA5, 0xC6, 0xB6,
  29216. 0x00, 0x27, 0x91, 0xEB, 0xE0, 0xAF, 0xDA, 0xF5,
  29217. 0x4E, 0x1C, 0xA2, 0x37, 0xFF, 0x54, 0x5B, 0xA6,
  29218. 0x83, 0x43, 0xE7, 0x45, 0xC0, 0x4A, 0xD1, 0x63,
  29219. 0x9D, 0xBC, 0x59, 0x03, 0x46, 0xB6, 0xB9, 0x56,
  29220. 0x9B, 0x56, 0xDB, 0xBF, 0xE5, 0x31, 0x51, 0x91,
  29221. 0x30, 0x66, 0xE5, 0xC8, 0x55, 0x27, 0xDC, 0x94,
  29222. 0x68, 0x11, 0x0A, 0x13, 0x6A, 0x41, 0x14, 0x97,
  29223. 0xC2, 0x27, 0xDC, 0xB8, 0xC9, 0xB2, 0x55, 0x70,
  29224. 0xB7, 0xA0, 0xE4, 0x2A, 0xAD, 0xA6, 0x70, 0x9F,
  29225. 0x23, 0x20, 0x8F, 0x5D, 0x49, 0x6E, 0xBA, 0xB7,
  29226. 0x84, 0x3F, 0x64, 0x83, 0xBF, 0x0C, 0x0C, 0x73,
  29227. 0xA4, 0x02, 0x96, 0xEC, 0x2C, 0x64, 0x40, 0x00,
  29228. 0x13, 0x94, 0xC9, 0x9C, 0xA1, 0x73, 0xD5, 0xC7,
  29229. 0x75, 0xB7, 0xF4, 0x15, 0xD0, 0x2A, 0x5A, 0x26,
  29230. 0xA0, 0x74, 0x07, 0x91, 0x85, 0x87, 0xC4, 0x11,
  29231. 0x69, 0xF2, 0xB7, 0x17, 0x87, 0x55, 0xAC, 0xC2,
  29232. 0x7F, 0xC8, 0xB1, 0x9C, 0x4C, 0x4B, 0x3F, 0xCD,
  29233. 0x41, 0x05, 0x3F, 0x2C, 0x74, 0xC8, 0xA1, 0x0A,
  29234. 0x83, 0x21, 0x24, 0x1B, 0x28, 0x02, 0x43, 0x28,
  29235. 0x75, 0xAE, 0x80, 0x8B, 0x9E, 0xF1, 0x36, 0x5C,
  29236. 0x7B, 0x8A, 0x52, 0x90, 0x2F, 0x13, 0x17, 0xBA,
  29237. 0x2F, 0xB0, 0x26, 0x9F, 0x47, 0x93, 0x06, 0x72,
  29238. 0x10, 0x7B, 0x47, 0x26, 0xFE, 0xF6, 0x45, 0x47,
  29239. 0x39, 0x4D, 0x33, 0x20, 0xC8, 0xF1, 0x20, 0xB3,
  29240. 0xC2, 0xF4, 0x72, 0x5B, 0x03, 0x05, 0xFA, 0xB8,
  29241. 0x8C, 0xC7, 0x98, 0x1F, 0xCB, 0x09, 0xA7, 0x6A,
  29242. 0x1C, 0xBF, 0x7F, 0x17, 0x9F, 0x43, 0xBB, 0x0A,
  29243. 0x4C, 0x8B, 0x05, 0x90, 0x85, 0x7F, 0x1E, 0x69,
  29244. 0x70, 0x84, 0x66, 0xC7, 0xF8, 0x60, 0x73, 0x91,
  29245. 0xE7, 0xBC, 0x52, 0x68, 0xBF, 0xD3, 0xD7, 0xA1,
  29246. 0xDF, 0xFC, 0xB4, 0xEC, 0xA2, 0xA1, 0xC9, 0xB5,
  29247. 0x97, 0x59, 0x30, 0x13, 0xD5, 0xFC, 0x42, 0x02,
  29248. 0xEC, 0x2B, 0x74, 0xE5, 0x7A, 0xB7, 0x6B, 0xBC,
  29249. 0xF3, 0x63, 0x2B, 0xBA, 0xF9, 0x7C, 0xDC, 0x41,
  29250. 0x8A, 0x6F, 0x16, 0x39, 0x28, 0x38, 0xCA, 0x9B,
  29251. 0xF4, 0x5D, 0xDF, 0x02, 0x37, 0x77, 0xB7, 0x56,
  29252. 0x18, 0x33, 0xC1, 0x05, 0x19, 0x0F, 0x94, 0xF3,
  29253. 0x02, 0xC5, 0x9B, 0x53, 0x19, 0x00, 0xBB, 0xC8,
  29254. 0x16, 0x36, 0x1F, 0xAA, 0x5B, 0x33, 0x80, 0xCA,
  29255. 0x3A, 0x89, 0x31, 0x04, 0xCA, 0x73, 0x88, 0xB1,
  29256. 0x85, 0x67, 0x1B, 0x3E, 0x5F, 0xE3, 0x79, 0x0E,
  29257. 0x9A, 0x62, 0x6E, 0xC4, 0x6D, 0x9B, 0x0B, 0x33,
  29258. 0xC7, 0xA4, 0x19, 0xAF, 0x7B, 0x32, 0xB6, 0x85,
  29259. 0x98, 0x94, 0xF5, 0x75, 0xD8, 0x2A, 0xC5, 0x45,
  29260. 0x6B, 0x54, 0x90, 0xA7, 0xAF, 0x8F, 0xE6, 0x10,
  29261. 0x46, 0x36, 0x05, 0x89, 0xEC, 0xBA, 0x72, 0x44,
  29262. 0x23, 0x6F, 0x41, 0x23, 0x11, 0x6B, 0x61, 0x74,
  29263. 0xAA, 0x17, 0x92, 0x49, 0xA4, 0x91, 0x95, 0xB3,
  29264. 0x56, 0xC7, 0x2F, 0xC6, 0x64, 0x1F, 0x02, 0x51,
  29265. 0x81, 0x2E, 0xAA, 0x98, 0x57, 0x0B, 0x04, 0x66,
  29266. 0x99, 0x07, 0x0E, 0x08, 0x19, 0xDC, 0x27, 0x13,
  29267. 0xF4, 0x69, 0x13, 0x7D, 0xFC, 0x6A, 0x3D, 0x7B,
  29268. 0x92, 0xB2, 0x98, 0x99, 0x5E, 0xE7, 0x80, 0x36,
  29269. 0x91, 0x53, 0xAC, 0x36, 0x6B, 0x06, 0xD7, 0x24,
  29270. 0x9C, 0xD0, 0x9E, 0x1B, 0x33, 0x78, 0xFB, 0x04,
  29271. 0x39, 0x9C, 0xEC, 0xB8, 0x65, 0x05, 0x81, 0xD6,
  29272. 0x37, 0xC7, 0x9A, 0xE6, 0x7D, 0x6F, 0x2C, 0xAF,
  29273. 0x6A, 0xBA, 0xCF, 0x59, 0x81, 0x59, 0xA7, 0x79,
  29274. 0x2C, 0xB3, 0xC9, 0x71, 0xD1, 0x49, 0x9D, 0x23,
  29275. 0x73, 0xAD, 0x20, 0xF6, 0x3F, 0x03, 0xBB, 0x59,
  29276. 0xED, 0x13, 0x73, 0x84, 0xAC, 0x61, 0xA7, 0x15,
  29277. 0x51, 0x43, 0xB8, 0xCA, 0x49, 0x32, 0x61, 0x2E,
  29278. 0xC9, 0x15, 0xE4, 0xCA, 0x34, 0x6A, 0x9B, 0xCE,
  29279. 0x5D, 0xD6, 0x04, 0x17, 0xC6, 0xB2, 0xA8, 0x9B,
  29280. 0x1C, 0xC4, 0x35, 0x64, 0x3F, 0x87, 0x5B, 0xDC,
  29281. 0x5A, 0x7E, 0x5B, 0x34, 0x81, 0xCF, 0x91, 0x9E,
  29282. 0xA0, 0x91, 0x72, 0xFE, 0xBC, 0x46, 0xD4, 0xFC,
  29283. 0x3F, 0xB0, 0xCB, 0x95, 0x91, 0x70, 0x4E, 0xE2,
  29284. 0xDB, 0xB6, 0x18, 0x44, 0xB2, 0xF3, 0x31, 0x4A,
  29285. 0x06, 0xBB, 0x6C, 0x6D, 0x34, 0x00, 0x5E, 0x48,
  29286. 0x5C, 0xE6, 0x67, 0xBD, 0xC7, 0xD0, 0x98, 0x58,
  29287. 0x69, 0x28, 0xD2, 0xD9, 0x13, 0x40, 0xF0, 0x04,
  29288. 0x19, 0xEA, 0x40, 0x13, 0x51, 0xA2, 0x40, 0xA0,
  29289. 0xB0, 0x41, 0x05, 0x8B, 0xEF, 0xB0, 0xC2, 0xFD,
  29290. 0x32, 0x64, 0x5B, 0x7A, 0x2D, 0xF8, 0xF5, 0xCB,
  29291. 0xFD, 0x87, 0x33, 0x27, 0xC9, 0x78, 0xD7, 0xB3,
  29292. 0x51, 0xA2, 0x80, 0x88, 0x43, 0x88, 0x37, 0x02,
  29293. 0x4C, 0x52, 0xB9, 0xC2, 0x95, 0xCD, 0x71, 0x36,
  29294. 0x46, 0xFB, 0x5D, 0x6C, 0x0C, 0xCF, 0xB4, 0x70,
  29295. 0x73, 0x4A, 0xC2, 0xB2, 0xBC, 0x81, 0x23, 0xC2,
  29296. 0xC1, 0x3D, 0xF6, 0x93, 0x8E, 0x92, 0x45, 0x5A,
  29297. 0x86, 0x26, 0x39, 0xFE, 0xB8, 0xA6, 0x4B, 0x85,
  29298. 0x16, 0x3E, 0x32, 0x70, 0x7E, 0x03, 0x7B, 0x38,
  29299. 0xD8, 0xAC, 0x39, 0x22, 0xB4, 0x51, 0x87, 0xBB,
  29300. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  29301. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  29302. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  29303. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  29304. 0x7F, 0xFA, 0xD1, 0xBC, 0x8A, 0xF7, 0x3B, 0x7E,
  29305. 0x87, 0x49, 0x56, 0xB8, 0x1C, 0x2A, 0x2E, 0xF0,
  29306. 0xBF, 0xAB, 0xE8, 0xDC, 0x93, 0xD7, 0x7B, 0x2F,
  29307. 0xBC, 0x9E, 0x0C, 0x64, 0xEF, 0xA0, 0x1E, 0x84,
  29308. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  29309. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  29310. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  29311. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  29312. };
  29313. const byte kyber512_ct[] = {
  29314. 0xED, 0xF2, 0x41, 0x45, 0xE4, 0x3B, 0x4F, 0x6D,
  29315. 0xC6, 0xBF, 0x83, 0x32, 0xF5, 0x4E, 0x02, 0xCA,
  29316. 0xB0, 0x2D, 0xBF, 0x3B, 0x56, 0x05, 0xDD, 0xC9,
  29317. 0x0A, 0x15, 0xC8, 0x86, 0xAD, 0x3E, 0xD4, 0x89,
  29318. 0x46, 0x26, 0x99, 0xE4, 0xAB, 0xED, 0x44, 0x35,
  29319. 0x0B, 0xC3, 0x75, 0x7E, 0x26, 0x96, 0xFB, 0xFB,
  29320. 0x25, 0x34, 0x41, 0x2E, 0x8D, 0xD2, 0x01, 0xF1,
  29321. 0xE4, 0x54, 0x0A, 0x39, 0x70, 0xB0, 0x55, 0xFE,
  29322. 0x3B, 0x0B, 0xEC, 0x3A, 0x71, 0xF9, 0xE1, 0x15,
  29323. 0xB3, 0xF9, 0xF3, 0x91, 0x02, 0x06, 0x5B, 0x1C,
  29324. 0xCA, 0x83, 0x14, 0xDC, 0xC7, 0x95, 0xE3, 0xC0,
  29325. 0xE8, 0xFA, 0x98, 0xEE, 0x83, 0xCA, 0x66, 0x28,
  29326. 0x45, 0x70, 0x28, 0xA4, 0xD0, 0x9E, 0x83, 0x9E,
  29327. 0x55, 0x48, 0x62, 0xCF, 0x0B, 0x7B, 0xF5, 0x6C,
  29328. 0x5C, 0x0A, 0x82, 0x9E, 0x86, 0x57, 0x94, 0x79,
  29329. 0x45, 0xFE, 0x9C, 0x22, 0x56, 0x4F, 0xBA, 0xEB,
  29330. 0xC1, 0xB3, 0xAF, 0x35, 0x0D, 0x79, 0x55, 0x50,
  29331. 0x8A, 0x26, 0xD8, 0xA8, 0xEB, 0x54, 0x7B, 0x8B,
  29332. 0x1A, 0x2C, 0xF0, 0x3C, 0xCA, 0x1A, 0xAB, 0xCE,
  29333. 0x6C, 0x34, 0x97, 0x78, 0x3B, 0x64, 0x65, 0xBA,
  29334. 0x0B, 0x6E, 0x7A, 0xCB, 0xA8, 0x21, 0x19, 0x51,
  29335. 0x24, 0xAE, 0xF0, 0x9E, 0x62, 0x83, 0x82, 0xA1,
  29336. 0xF9, 0x14, 0x04, 0x3B, 0xE7, 0x09, 0x6E, 0x95,
  29337. 0x2C, 0xBC, 0x4F, 0xB4, 0xAF, 0xED, 0x13, 0x60,
  29338. 0x90, 0x46, 0x11, 0x7C, 0x01, 0x1F, 0xD7, 0x41,
  29339. 0xEE, 0x28, 0x6C, 0x83, 0x77, 0x16, 0x90, 0xF0,
  29340. 0xAE, 0xB5, 0x0D, 0xA0, 0xD7, 0x12, 0x85, 0xA1,
  29341. 0x79, 0xB2, 0x15, 0xC6, 0x03, 0x6D, 0xEB, 0x78,
  29342. 0x0F, 0x4D, 0x16, 0x76, 0x9F, 0x72, 0xDE, 0x16,
  29343. 0xFD, 0xAD, 0xAC, 0x73, 0xBE, 0xFA, 0x5B, 0xEF,
  29344. 0x89, 0x43, 0x19, 0x7F, 0x44, 0xC5, 0x95, 0x89,
  29345. 0xDC, 0x9F, 0x49, 0x73, 0xDE, 0x14, 0x50, 0xBA,
  29346. 0x1D, 0x0C, 0x32, 0x90, 0xD6, 0xB1, 0xD6, 0x83,
  29347. 0xF2, 0x94, 0xE7, 0x59, 0xC9, 0x54, 0xAB, 0xE8,
  29348. 0xA7, 0xDA, 0x5B, 0x10, 0x54, 0xFD, 0x6D, 0x21,
  29349. 0x32, 0x9B, 0x8E, 0x73, 0xD3, 0x75, 0x6A, 0xFD,
  29350. 0xA0, 0xDC, 0xB1, 0xFC, 0x8B, 0x15, 0x82, 0xD1,
  29351. 0xF9, 0x0C, 0xF2, 0x75, 0xA1, 0x02, 0xAB, 0xC6,
  29352. 0xAC, 0x69, 0x9D, 0xF0, 0xC5, 0x87, 0x0E, 0x50,
  29353. 0xA1, 0xF9, 0x89, 0xE4, 0xE6, 0x24, 0x1B, 0x60,
  29354. 0xAA, 0xA2, 0xEC, 0xF9, 0xE8, 0xE3, 0x3E, 0x0F,
  29355. 0xFC, 0xF4, 0x0F, 0xE8, 0x31, 0xE8, 0xFD, 0xC2,
  29356. 0xE8, 0x3B, 0x52, 0xCA, 0x7A, 0xB6, 0xD9, 0x3F,
  29357. 0x14, 0x6D, 0x29, 0xDC, 0xA5, 0x3C, 0x7D, 0xA1,
  29358. 0xDB, 0x4A, 0xC4, 0xF2, 0xDB, 0x39, 0xEA, 0x12,
  29359. 0x0D, 0x90, 0xFA, 0x60, 0xF4, 0xD4, 0x37, 0xC6,
  29360. 0xD0, 0x0E, 0xF4, 0x83, 0xBC, 0x94, 0xA3, 0x17,
  29361. 0x5C, 0xDA, 0x16, 0x3F, 0xC1, 0xC2, 0x82, 0x8B,
  29362. 0xE4, 0xDB, 0xD6, 0x43, 0x05, 0x07, 0xB5, 0x84,
  29363. 0xBB, 0x51, 0x77, 0xE1, 0x71, 0xB8, 0xDD, 0xA9,
  29364. 0xA4, 0x29, 0x3C, 0x32, 0x00, 0x29, 0x5C, 0x80,
  29365. 0x3A, 0x86, 0x5D, 0x6D, 0x21, 0x66, 0xF6, 0x6B,
  29366. 0xA5, 0x40, 0x1F, 0xB7, 0xA0, 0xE8, 0x53, 0x16,
  29367. 0x86, 0x00, 0xA2, 0x94, 0x84, 0x37, 0xE0, 0x36,
  29368. 0xE3, 0xBF, 0x19, 0xE1, 0x2F, 0xD3, 0xF2, 0xA2,
  29369. 0xB8, 0xB3, 0x43, 0xF7, 0x84, 0x24, 0x8E, 0x8D,
  29370. 0x68, 0x5E, 0xB0, 0xAF, 0xDE, 0x63, 0x15, 0x33,
  29371. 0x87, 0x30, 0xE7, 0xA1, 0x00, 0x1C, 0x27, 0xD8,
  29372. 0xD2, 0xA7, 0x6F, 0xA6, 0x9D, 0x15, 0x7B, 0xA1,
  29373. 0xAC, 0x7A, 0xD5, 0x6D, 0xA5, 0xA8, 0xC7, 0x0F,
  29374. 0xE4, 0xB5, 0xB8, 0xD7, 0x86, 0xDC, 0x6F, 0xC0,
  29375. 0x56, 0x6B, 0xA8, 0xE1, 0xB8, 0x81, 0x63, 0x34,
  29376. 0xD3, 0x2A, 0x3F, 0xB1, 0xCE, 0x7D, 0x4D, 0x5E,
  29377. 0x4C, 0x33, 0x2A, 0xF7, 0xB0, 0x03, 0xD0, 0x91,
  29378. 0x74, 0x1A, 0x3D, 0x5C, 0x96, 0x52, 0x92, 0x25,
  29379. 0x5D, 0xFF, 0x8E, 0xD2, 0xBB, 0xF1, 0xF9, 0x11,
  29380. 0x6B, 0xE5, 0x0C, 0x17, 0xB8, 0xE5, 0x48, 0x74,
  29381. 0x8A, 0xD4, 0xB2, 0xE9, 0x57, 0xBB, 0xD1, 0x95,
  29382. 0x34, 0x82, 0xA2, 0xE1, 0x71, 0x8C, 0xEC, 0x66,
  29383. 0xCD, 0x2C, 0x81, 0xF5, 0x72, 0xD5, 0x52, 0xB7,
  29384. 0x18, 0x78, 0x85, 0xE6, 0xB8, 0x94, 0x3D, 0x64,
  29385. 0x31, 0x41, 0x3C, 0x59, 0xEB, 0xB7, 0xE0, 0x36,
  29386. 0x04, 0x84, 0x90, 0xBE, 0x52, 0x89, 0xE9, 0x5B,
  29387. 0x20, 0xA8, 0x9E, 0x8B, 0x15, 0x9F, 0x61, 0xA9,
  29388. 0xA9, 0x88, 0x6E, 0x14, 0x75, 0x68, 0xF4, 0xC9,
  29389. 0x02, 0x1F, 0x36, 0x2F, 0x02, 0x68, 0x8A, 0x1C,
  29390. 0x8C, 0x3B, 0xB0, 0xD2, 0x40, 0x86, 0x88, 0x0E,
  29391. 0x55, 0xB6, 0xED, 0xB4, 0x3F, 0x37, 0x45, 0xD2,
  29392. 0xC1, 0x66, 0xDC, 0x1C, 0xB7, 0x43, 0xC7, 0x6F,
  29393. 0xE6, 0xBE, 0x52, 0x3A, 0x89, 0x3C, 0xC7, 0x64,
  29394. 0xD1, 0x64, 0x35, 0xC3, 0x78, 0x51, 0x25, 0x2A,
  29395. 0x81, 0xE2, 0xFF, 0xBA, 0x0F, 0x18, 0x97, 0x1A,
  29396. 0x3D, 0xEE, 0x37, 0xD4, 0x87, 0x7C, 0xB9, 0x28,
  29397. 0xE3, 0x6E, 0x52, 0x35, 0x03, 0x7A, 0x6B, 0x20,
  29398. 0x57, 0x89, 0x7D, 0x51, 0x8A, 0x5F, 0x0E, 0x34,
  29399. 0x8E, 0x3A, 0xB6, 0xD5, 0xB5, 0x2D, 0xFC, 0x60,
  29400. 0x75, 0x7F, 0x3B, 0x41, 0xA4, 0xFE, 0xC7, 0x82,
  29401. 0x8F, 0x1D, 0xEE, 0xAF, 0x45, 0x87, 0xCC, 0xC8,
  29402. 0xEA, 0xDF, 0x64, 0x7F, 0x4D, 0x20, 0x3B, 0x2F,
  29403. 0xAA, 0x05, 0xA6, 0x49, 0xB5, 0x82, 0x34, 0x0C,
  29404. 0xB4, 0xCA, 0xCE, 0x57, 0xA3, 0x07, 0x11, 0xBE,
  29405. 0x75, 0x2F, 0xAC, 0xF0, 0x22, 0x7D, 0x0A, 0x80,
  29406. 0xC4, 0x12, 0x84, 0x42, 0xDD, 0xC5, 0x44, 0xBE,
  29407. 0x80, 0x5B, 0x9C, 0xFE, 0x8F, 0xE9, 0xB1, 0x23,
  29408. 0x7C, 0x80, 0xF9, 0x67, 0x87, 0xCD, 0x92, 0x81,
  29409. 0xCC, 0xF2, 0x70, 0xC1, 0xAF, 0xC0, 0x67, 0x0D
  29410. };
  29411. const byte kyber512_ss[] = {
  29412. 0x0A, 0x69, 0x25, 0x67, 0x6F, 0x24, 0xB2, 0x2C,
  29413. 0x28, 0x6F, 0x4C, 0x81, 0xA4, 0x22, 0x4C, 0xEC,
  29414. 0x50, 0x6C, 0x9B, 0x25, 0x7D, 0x48, 0x0E, 0x02,
  29415. 0xE3, 0xB4, 0x9F, 0x44, 0xCA, 0xA3, 0x23, 0x7F
  29416. };
  29417. ret = wc_KyberKey_Init(KYBER512, &key, HEAP_HINT, INVALID_DEVID);
  29418. if (ret != 0)
  29419. return WC_TEST_RET_ENC_EC(ret);
  29420. ret = wc_KyberKey_MakeKeyWithRandom(&key, kyber512_rand,
  29421. sizeof(kyber512_rand));
  29422. if (ret != 0)
  29423. return WC_TEST_RET_ENC_EC(ret);
  29424. ret = wc_KyberKey_EncodePublicKey(&key, pub, sizeof(pub));
  29425. if (ret != 0)
  29426. return WC_TEST_RET_ENC_EC(ret);
  29427. ret = wc_KyberKey_EncodePrivateKey(&key, priv, sizeof(priv));
  29428. if (ret != 0)
  29429. return WC_TEST_RET_ENC_EC(ret);
  29430. if (XMEMCMP(pub, kyber512_pk, sizeof(kyber512_pk)) != 0)
  29431. return WC_TEST_RET_ENC_NC;
  29432. if (XMEMCMP(priv, kyber512_sk, sizeof(kyber512_sk)) != 0)
  29433. return WC_TEST_RET_ENC_NC;
  29434. ret = wc_KyberKey_EncapsulateWithRandom(&key, ct, ss, kyber512enc_rand,
  29435. sizeof(kyber512enc_rand));
  29436. if (ret != 0)
  29437. return WC_TEST_RET_ENC_EC(ret);
  29438. if (XMEMCMP(ct, kyber512_ct, sizeof(kyber512_ct)) != 0)
  29439. return WC_TEST_RET_ENC_NC;
  29440. if (XMEMCMP(ss, kyber512_ss, sizeof(kyber512_ss)) != 0)
  29441. return WC_TEST_RET_ENC_NC;
  29442. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, sizeof(kyber512_ct));
  29443. if (ret != 0)
  29444. return WC_TEST_RET_ENC_EC(ret);
  29445. if (XMEMCMP(ss_dec, kyber512_ss, sizeof(kyber512_ss)) != 0)
  29446. return WC_TEST_RET_ENC_NC;
  29447. wc_KyberKey_Free(&key);
  29448. return 0;
  29449. }
  29450. #endif /* WOLFSSL_KYBER512 */
  29451. #ifdef WOLFSSL_KYBER768
  29452. static wc_test_ret_t kyber768_kat(void)
  29453. {
  29454. KyberKey key;
  29455. wc_test_ret_t ret;
  29456. byte priv[KYBER768_PRIVATE_KEY_SIZE];
  29457. byte pub[KYBER768_PUBLIC_KEY_SIZE];
  29458. byte ct[KYBER768_CIPHER_TEXT_SIZE];
  29459. byte ss[KYBER_SS_SZ];
  29460. byte ss_dec[KYBER_SS_SZ];
  29461. const byte kyber768_rand[] = {
  29462. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  29463. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  29464. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  29465. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  29466. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  29467. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  29468. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  29469. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  29470. };
  29471. const byte kyber768enc_rand[] = {
  29472. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  29473. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  29474. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  29475. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  29476. };
  29477. const byte kyber768_pk[] = {
  29478. 0xA7, 0x2C, 0x2D, 0x9C, 0x84, 0x3E, 0xE9, 0xF8,
  29479. 0x31, 0x3E, 0xCC, 0x7F, 0x86, 0xD6, 0x29, 0x4D,
  29480. 0x59, 0x15, 0x9D, 0x9A, 0x87, 0x9A, 0x54, 0x2E,
  29481. 0x26, 0x09, 0x22, 0xAD, 0xF9, 0x99, 0x05, 0x1C,
  29482. 0xC4, 0x52, 0x00, 0xC9, 0xFF, 0xDB, 0x60, 0x44,
  29483. 0x9C, 0x49, 0x46, 0x59, 0x79, 0x27, 0x23, 0x67,
  29484. 0xC0, 0x83, 0xA7, 0xD6, 0x26, 0x7A, 0x3E, 0xD7,
  29485. 0xA7, 0xFD, 0x47, 0x95, 0x7C, 0x21, 0x93, 0x27,
  29486. 0xF7, 0xCA, 0x73, 0xA4, 0x00, 0x7E, 0x16, 0x27,
  29487. 0xF0, 0x0B, 0x11, 0xCC, 0x80, 0x57, 0x3C, 0x15,
  29488. 0xAE, 0xE6, 0x64, 0x0F, 0xB8, 0x56, 0x2D, 0xFA,
  29489. 0x6B, 0x24, 0x0C, 0xA0, 0xAD, 0x35, 0x1A, 0xC4,
  29490. 0xAC, 0x15, 0x5B, 0x96, 0xC1, 0x4C, 0x8A, 0xB1,
  29491. 0x3D, 0xD2, 0x62, 0xCD, 0xFD, 0x51, 0xC4, 0xBB,
  29492. 0x55, 0x72, 0xFD, 0x61, 0x65, 0x53, 0xD1, 0x7B,
  29493. 0xDD, 0x43, 0x0A, 0xCB, 0xEA, 0x3E, 0x95, 0xF0,
  29494. 0xB6, 0x98, 0xD6, 0x69, 0x90, 0xAB, 0x51, 0xE5,
  29495. 0xD0, 0x37, 0x83, 0xA8, 0xB3, 0xD2, 0x78, 0xA5,
  29496. 0x72, 0x04, 0x54, 0xCF, 0x96, 0x95, 0xCF, 0xDC,
  29497. 0xA0, 0x84, 0x85, 0xBA, 0x09, 0x9C, 0x51, 0xCD,
  29498. 0x92, 0xA7, 0xEA, 0x75, 0x87, 0xC1, 0xD1, 0x5C,
  29499. 0x28, 0xE6, 0x09, 0xA8, 0x18, 0x52, 0x60, 0x1B,
  29500. 0x06, 0x04, 0x01, 0x06, 0x79, 0xAA, 0x48, 0x2D,
  29501. 0x51, 0x26, 0x1E, 0xC3, 0x6E, 0x36, 0xB8, 0x71,
  29502. 0x96, 0x76, 0x21, 0x7F, 0xD7, 0x4C, 0x54, 0x78,
  29503. 0x64, 0x88, 0xF4, 0xB4, 0x96, 0x9C, 0x05, 0xA8,
  29504. 0xBA, 0x27, 0xCA, 0x3A, 0x77, 0xCC, 0xE7, 0x3B,
  29505. 0x96, 0x59, 0x23, 0xCA, 0x55, 0x4E, 0x42, 0x2B,
  29506. 0x9B, 0x61, 0xF4, 0x75, 0x46, 0x41, 0x60, 0x8A,
  29507. 0xC1, 0x6C, 0x9B, 0x85, 0x87, 0xA3, 0x2C, 0x1C,
  29508. 0x5D, 0xD7, 0x88, 0xF8, 0x8B, 0x36, 0xB7, 0x17,
  29509. 0xA4, 0x69, 0x65, 0x63, 0x5D, 0xEB, 0x67, 0xF4,
  29510. 0x5B, 0x12, 0x9B, 0x99, 0x07, 0x09, 0x09, 0xC9,
  29511. 0x3E, 0xB8, 0x0B, 0x42, 0xC2, 0xB3, 0xF3, 0xF7,
  29512. 0x03, 0x43, 0xA7, 0xCF, 0x37, 0xE8, 0x52, 0x0E,
  29513. 0x7B, 0xCF, 0xC4, 0x16, 0xAC, 0xA4, 0xF1, 0x8C,
  29514. 0x79, 0x81, 0x26, 0x2B, 0xA2, 0xBF, 0xC7, 0x56,
  29515. 0xAE, 0x03, 0x27, 0x8F, 0x0E, 0xC6, 0x6D, 0xC2,
  29516. 0x05, 0x76, 0x96, 0x82, 0x4B, 0xA6, 0x76, 0x98,
  29517. 0x65, 0xA6, 0x01, 0xD7, 0x14, 0x8E, 0xF6, 0xF5,
  29518. 0x4E, 0x5A, 0xF5, 0x68, 0x6A, 0xA2, 0x90, 0x6F,
  29519. 0x99, 0x4C, 0xE3, 0x8A, 0x5E, 0x0B, 0x93, 0x8F,
  29520. 0x23, 0x90, 0x07, 0x00, 0x30, 0x22, 0xC0, 0x33,
  29521. 0x92, 0xDF, 0x34, 0x01, 0xB1, 0xE4, 0xA3, 0xA7,
  29522. 0xEB, 0xC6, 0x16, 0x14, 0x49, 0xF7, 0x33, 0x74,
  29523. 0xC8, 0xB0, 0x14, 0x03, 0x69, 0x34, 0x3D, 0x92,
  29524. 0x95, 0xFD, 0xF5, 0x11, 0x84, 0x5C, 0x4A, 0x46,
  29525. 0xEB, 0xAA, 0xB6, 0xCA, 0x54, 0x92, 0xF6, 0x80,
  29526. 0x0B, 0x98, 0xC0, 0xCC, 0x80, 0x36, 0x53, 0xA4,
  29527. 0xB1, 0xD6, 0xE6, 0xAA, 0xED, 0x19, 0x32, 0xBA,
  29528. 0xCC, 0x5F, 0xEF, 0xAA, 0x81, 0x8B, 0xA5, 0x02,
  29529. 0x85, 0x9B, 0xA5, 0x49, 0x4C, 0x5F, 0x54, 0x02,
  29530. 0xC8, 0x53, 0x6A, 0x9C, 0x4C, 0x18, 0x88, 0x15,
  29531. 0x06, 0x17, 0xF8, 0x00, 0x98, 0xF6, 0xB2, 0xA9,
  29532. 0x9C, 0x39, 0xBC, 0x5D, 0xC7, 0xCF, 0x3B, 0x59,
  29533. 0x00, 0xA2, 0x13, 0x29, 0xAB, 0x59, 0x05, 0x3A,
  29534. 0xBA, 0xA6, 0x4E, 0xD1, 0x63, 0xE8, 0x59, 0xA8,
  29535. 0xB3, 0xB3, 0xCA, 0x33, 0x59, 0xB7, 0x50, 0xCC,
  29536. 0xC3, 0xE7, 0x10, 0xC7, 0xAC, 0x43, 0xC8, 0x19,
  29537. 0x1C, 0xB5, 0xD6, 0x88, 0x70, 0xC0, 0x63, 0x91,
  29538. 0xC0, 0xCB, 0x8A, 0xEC, 0x72, 0xB8, 0x97, 0xAC,
  29539. 0x6B, 0xE7, 0xFB, 0xAA, 0xCC, 0x67, 0x6E, 0xD6,
  29540. 0x63, 0x14, 0xC8, 0x36, 0x30, 0xE8, 0x94, 0x48,
  29541. 0xC8, 0x8A, 0x1D, 0xF0, 0x4A, 0xCE, 0xB2, 0x3A,
  29542. 0xBF, 0x2E, 0x40, 0x9E, 0xF3, 0x33, 0xC6, 0x22,
  29543. 0x28, 0x9C, 0x18, 0xA2, 0x13, 0x4E, 0x65, 0x0C,
  29544. 0x45, 0x25, 0x7E, 0x47, 0x47, 0x5F, 0xA3, 0x3A,
  29545. 0xA5, 0x37, 0xA5, 0xA8, 0xF7, 0x68, 0x02, 0x14,
  29546. 0x71, 0x6C, 0x50, 0xD4, 0x70, 0xE3, 0x28, 0x49,
  29547. 0x63, 0xCA, 0x64, 0xF5, 0x46, 0x77, 0xAE, 0xC5,
  29548. 0x4B, 0x52, 0x72, 0x16, 0x2B, 0xF5, 0x2B, 0xC8,
  29549. 0x14, 0x2E, 0x1D, 0x41, 0x83, 0xFC, 0x01, 0x74,
  29550. 0x54, 0xA6, 0xB5, 0xA4, 0x96, 0x83, 0x17, 0x59,
  29551. 0x06, 0x40, 0x24, 0x74, 0x59, 0x78, 0xCB, 0xD5,
  29552. 0x1A, 0x6C, 0xED, 0xC8, 0x95, 0x5D, 0xE4, 0xCC,
  29553. 0x6D, 0x36, 0x36, 0x70, 0xA4, 0x74, 0x66, 0xE8,
  29554. 0x2B, 0xE5, 0xC2, 0x36, 0x03, 0xA1, 0x7B, 0xF2,
  29555. 0x2A, 0xCD, 0xB7, 0xCC, 0x98, 0x4A, 0xF0, 0x8C,
  29556. 0x87, 0xE1, 0x4E, 0x27, 0x75, 0x3C, 0xF5, 0x87,
  29557. 0xA8, 0xEC, 0x34, 0x47, 0xE6, 0x2C, 0x64, 0x9E,
  29558. 0x88, 0x7A, 0x67, 0xC3, 0x6C, 0x9C, 0xE9, 0x87,
  29559. 0x21, 0xB6, 0x97, 0x21, 0x32, 0x75, 0x64, 0x6B,
  29560. 0x19, 0x4F, 0x36, 0x75, 0x86, 0x73, 0xA8, 0xED,
  29561. 0x11, 0x28, 0x44, 0x55, 0xAF, 0xC7, 0xA8, 0x52,
  29562. 0x9F, 0x69, 0xC9, 0x7A, 0x3C, 0x2D, 0x7B, 0x8C,
  29563. 0x63, 0x6C, 0x0B, 0xA5, 0x56, 0x14, 0xB7, 0x68,
  29564. 0xE6, 0x24, 0xE7, 0x12, 0x93, 0x0F, 0x77, 0x61,
  29565. 0x69, 0xB0, 0x17, 0x15, 0x72, 0x53, 0x51, 0xBC,
  29566. 0x74, 0xB4, 0x73, 0x95, 0xED, 0x52, 0xB2, 0x5A,
  29567. 0x13, 0x13, 0xC9, 0x51, 0x64, 0x81, 0x4C, 0x34,
  29568. 0xC9, 0x79, 0xCB, 0xDF, 0xAB, 0x85, 0x95, 0x46,
  29569. 0x62, 0xCA, 0xB4, 0x85, 0xE7, 0x50, 0x87, 0xA9,
  29570. 0x8C, 0xC7, 0x4B, 0xB8, 0x2C, 0xA2, 0xD1, 0xB5,
  29571. 0xBF, 0x28, 0x03, 0x23, 0x84, 0x80, 0x63, 0x8C,
  29572. 0x40, 0xE9, 0x0B, 0x43, 0xC7, 0x46, 0x0E, 0x7A,
  29573. 0xA9, 0x17, 0xF0, 0x10, 0x15, 0x1F, 0xAB, 0x11,
  29574. 0x69, 0x98, 0x7B, 0x37, 0x2A, 0xBB, 0x59, 0x27,
  29575. 0x1F, 0x70, 0x06, 0xC2, 0x4E, 0x60, 0x23, 0x6B,
  29576. 0x84, 0xB9, 0xDD, 0xD6, 0x00, 0x62, 0x37, 0x04,
  29577. 0x25, 0x46, 0x17, 0xFB, 0x49, 0x8D, 0x89, 0xE5,
  29578. 0x8B, 0x03, 0x68, 0xBC, 0xB2, 0x10, 0x3E, 0x79,
  29579. 0x35, 0x3E, 0xB5, 0x87, 0x86, 0x0C, 0x14, 0x22,
  29580. 0xE4, 0x76, 0x16, 0x2E, 0x42, 0x5B, 0xC2, 0x38,
  29581. 0x1D, 0xB8, 0x2C, 0x65, 0x92, 0x73, 0x7E, 0x1D,
  29582. 0xD6, 0x02, 0x86, 0x4B, 0x01, 0x67, 0xA7, 0x1E,
  29583. 0xC1, 0xF2, 0x23, 0x30, 0x5C, 0x02, 0xFE, 0x25,
  29584. 0x05, 0x2A, 0xF2, 0xB3, 0xB5, 0xA5, 0x5A, 0x0D,
  29585. 0x7A, 0x20, 0x22, 0xD9, 0xA7, 0x98, 0xDC, 0x0C,
  29586. 0x58, 0x74, 0xA9, 0x87, 0x02, 0xAA, 0xF4, 0x05,
  29587. 0x4C, 0x5D, 0x80, 0x33, 0x8A, 0x52, 0x48, 0xB5,
  29588. 0xB7, 0xBD, 0x09, 0xC5, 0x3B, 0x5E, 0x2A, 0x08,
  29589. 0x4B, 0x04, 0x7D, 0x27, 0x7A, 0x86, 0x1B, 0x1A,
  29590. 0x73, 0xBB, 0x51, 0x48, 0x8D, 0xE0, 0x4E, 0xF5,
  29591. 0x73, 0xC8, 0x52, 0x30, 0xA0, 0x47, 0x0B, 0x73,
  29592. 0x17, 0x5C, 0x9F, 0xA5, 0x05, 0x94, 0xF6, 0x6A,
  29593. 0x5F, 0x50, 0xB4, 0x15, 0x00, 0x54, 0xC9, 0x3B,
  29594. 0x68, 0x18, 0x6F, 0x8B, 0x5C, 0xBC, 0x49, 0x31,
  29595. 0x6C, 0x85, 0x48, 0xA6, 0x42, 0xB2, 0xB3, 0x6A,
  29596. 0x1D, 0x45, 0x4C, 0x74, 0x89, 0xAC, 0x33, 0xB2,
  29597. 0xD2, 0xCE, 0x66, 0x68, 0x09, 0x67, 0x82, 0xA2,
  29598. 0xC1, 0xE0, 0x86, 0x6D, 0x21, 0xA6, 0x5E, 0x16,
  29599. 0xB5, 0x85, 0xE7, 0xAF, 0x86, 0x18, 0xBD, 0xF3,
  29600. 0x18, 0x4C, 0x19, 0x86, 0x87, 0x85, 0x08, 0x91,
  29601. 0x72, 0x77, 0xB9, 0x3E, 0x10, 0x70, 0x6B, 0x16,
  29602. 0x14, 0x97, 0x2B, 0x2A, 0x94, 0xC7, 0x31, 0x0F,
  29603. 0xE9, 0xC7, 0x08, 0xC2, 0x31, 0xA1, 0xA8, 0xAC,
  29604. 0x8D, 0x93, 0x14, 0xA5, 0x29, 0xA9, 0x7F, 0x46,
  29605. 0x9B, 0xF6, 0x49, 0x62, 0xD8, 0x20, 0x64, 0x84,
  29606. 0x43, 0x09, 0x9A, 0x07, 0x6D, 0x55, 0xD4, 0xCE,
  29607. 0xA8, 0x24, 0xA5, 0x83, 0x04, 0x84, 0x4F, 0x99,
  29608. 0x49, 0x7C, 0x10, 0xA2, 0x51, 0x48, 0x61, 0x8A,
  29609. 0x31, 0x5D, 0x72, 0xCA, 0x85, 0x7D, 0x1B, 0x04,
  29610. 0xD5, 0x75, 0xB9, 0x4F, 0x85, 0xC0, 0x1D, 0x19,
  29611. 0xBE, 0xF2, 0x11, 0xBF, 0x0A, 0xA3, 0x36, 0x2E,
  29612. 0x70, 0x41, 0xFD, 0x16, 0x59, 0x6D, 0x80, 0x8E,
  29613. 0x86, 0x7B, 0x44, 0xC4, 0xC0, 0x0D, 0x1C, 0xDA,
  29614. 0x34, 0x18, 0x96, 0x77, 0x17, 0xF1, 0x47, 0xD0,
  29615. 0xEB, 0x21, 0xB4, 0x2A, 0xAE, 0xE7, 0x4A, 0xC3,
  29616. 0x5D, 0x0B, 0x92, 0x41, 0x4B, 0x95, 0x85, 0x31,
  29617. 0xAA, 0xDF, 0x46, 0x3E, 0xC6, 0x30, 0x5A, 0xE5,
  29618. 0xEC, 0xAF, 0x79, 0x17, 0x40, 0x02, 0xF2, 0x6D,
  29619. 0xDE, 0xCC, 0x81, 0x3B, 0xF3, 0x26, 0x72, 0xE8,
  29620. 0x52, 0x9D, 0x95, 0xA4, 0xE7, 0x30, 0xA7, 0xAB,
  29621. 0x4A, 0x3E, 0x8F, 0x8A, 0x8A, 0xF9, 0x79, 0xA6,
  29622. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  29623. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  29624. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  29625. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  29626. };
  29627. const byte kyber768_sk[] = {
  29628. 0x07, 0x63, 0x8F, 0xB6, 0x98, 0x68, 0xF3, 0xD3,
  29629. 0x20, 0xE5, 0x86, 0x2B, 0xD9, 0x69, 0x33, 0xFE,
  29630. 0xB3, 0x11, 0xB3, 0x62, 0x09, 0x3C, 0x9B, 0x5D,
  29631. 0x50, 0x17, 0x0B, 0xCE, 0xD4, 0x3F, 0x1B, 0x53,
  29632. 0x6D, 0x9A, 0x20, 0x4B, 0xB1, 0xF2, 0x26, 0x95,
  29633. 0x95, 0x0B, 0xA1, 0xF2, 0xA9, 0xE8, 0xEB, 0x82,
  29634. 0x8B, 0x28, 0x44, 0x88, 0x76, 0x0B, 0x3F, 0xC8,
  29635. 0x4F, 0xAB, 0xA0, 0x42, 0x75, 0xD5, 0x62, 0x8E,
  29636. 0x39, 0xC5, 0xB2, 0x47, 0x13, 0x74, 0x28, 0x3C,
  29637. 0x50, 0x32, 0x99, 0xC0, 0xAB, 0x49, 0xB6, 0x6B,
  29638. 0x8B, 0xBB, 0x56, 0xA4, 0x18, 0x66, 0x24, 0xF9,
  29639. 0x19, 0xA2, 0xBA, 0x59, 0xBB, 0x08, 0xD8, 0x55,
  29640. 0x18, 0x80, 0xC2, 0xBE, 0xFC, 0x4F, 0x87, 0xF2,
  29641. 0x5F, 0x59, 0xAB, 0x58, 0x7A, 0x79, 0xC3, 0x27,
  29642. 0xD7, 0x92, 0xD5, 0x4C, 0x97, 0x4A, 0x69, 0x26,
  29643. 0x2F, 0xF8, 0xA7, 0x89, 0x38, 0x28, 0x9E, 0x9A,
  29644. 0x87, 0xB6, 0x88, 0xB0, 0x83, 0xE0, 0x59, 0x5F,
  29645. 0xE2, 0x18, 0xB6, 0xBB, 0x15, 0x05, 0x94, 0x1C,
  29646. 0xE2, 0xE8, 0x1A, 0x5A, 0x64, 0xC5, 0xAA, 0xC6,
  29647. 0x04, 0x17, 0x25, 0x69, 0x85, 0x34, 0x9E, 0xE4,
  29648. 0x7A, 0x52, 0x42, 0x0A, 0x5F, 0x97, 0x47, 0x7B,
  29649. 0x72, 0x36, 0xAC, 0x76, 0xBC, 0x70, 0xE8, 0x28,
  29650. 0x87, 0x29, 0x28, 0x7E, 0xE3, 0xE3, 0x4A, 0x3D,
  29651. 0xBC, 0x36, 0x83, 0xC0, 0xB7, 0xB1, 0x00, 0x29,
  29652. 0xFC, 0x20, 0x34, 0x18, 0x53, 0x7E, 0x74, 0x66,
  29653. 0xBA, 0x63, 0x85, 0xA8, 0xFF, 0x30, 0x1E, 0xE1,
  29654. 0x27, 0x08, 0xF8, 0x2A, 0xAA, 0x1E, 0x38, 0x0F,
  29655. 0xC7, 0xA8, 0x8F, 0x8F, 0x20, 0x5A, 0xB7, 0xE8,
  29656. 0x8D, 0x7E, 0x95, 0x95, 0x2A, 0x55, 0xBA, 0x20,
  29657. 0xD0, 0x9B, 0x79, 0xA4, 0x71, 0x41, 0xD6, 0x2B,
  29658. 0xF6, 0xEB, 0x7D, 0xD3, 0x07, 0xB0, 0x8E, 0xCA,
  29659. 0x13, 0xA5, 0xBC, 0x5F, 0x6B, 0x68, 0x58, 0x1C,
  29660. 0x68, 0x65, 0xB2, 0x7B, 0xBC, 0xDD, 0xAB, 0x14,
  29661. 0x2F, 0x4B, 0x2C, 0xBF, 0xF4, 0x88, 0xC8, 0xA2,
  29662. 0x27, 0x05, 0xFA, 0xA9, 0x8A, 0x2B, 0x9E, 0xEA,
  29663. 0x35, 0x30, 0xC7, 0x66, 0x62, 0x33, 0x5C, 0xC7,
  29664. 0xEA, 0x3A, 0x00, 0x77, 0x77, 0x25, 0xEB, 0xCC,
  29665. 0xCD, 0x2A, 0x46, 0x36, 0xB2, 0xD9, 0x12, 0x2F,
  29666. 0xF3, 0xAB, 0x77, 0x12, 0x3C, 0xE0, 0x88, 0x3C,
  29667. 0x19, 0x11, 0x11, 0x5E, 0x50, 0xC9, 0xE8, 0xA9,
  29668. 0x41, 0x94, 0xE4, 0x8D, 0xD0, 0xD0, 0x9C, 0xFF,
  29669. 0xB3, 0xAD, 0xCD, 0x2C, 0x1E, 0x92, 0x43, 0x09,
  29670. 0x03, 0xD0, 0x7A, 0xDB, 0xF0, 0x05, 0x32, 0x03,
  29671. 0x15, 0x75, 0xAA, 0x7F, 0x9E, 0x7B, 0x5A, 0x1F,
  29672. 0x33, 0x62, 0xDE, 0xC9, 0x36, 0xD4, 0x04, 0x3C,
  29673. 0x05, 0xF2, 0x47, 0x6C, 0x07, 0x57, 0x8B, 0xC9,
  29674. 0xCB, 0xAF, 0x2A, 0xB4, 0xE3, 0x82, 0x72, 0x7A,
  29675. 0xD4, 0x16, 0x86, 0xA9, 0x6B, 0x25, 0x48, 0x82,
  29676. 0x0B, 0xB0, 0x3B, 0x32, 0xF1, 0x1B, 0x28, 0x11,
  29677. 0xAD, 0x62, 0xF4, 0x89, 0xE9, 0x51, 0x63, 0x2A,
  29678. 0xBA, 0x0D, 0x1D, 0xF8, 0x96, 0x80, 0xCC, 0x8A,
  29679. 0x8B, 0x53, 0xB4, 0x81, 0xD9, 0x2A, 0x68, 0xD7,
  29680. 0x0B, 0x4E, 0xA1, 0xC3, 0xA6, 0xA5, 0x61, 0xC0,
  29681. 0x69, 0x28, 0x82, 0xB5, 0xCA, 0x8C, 0xC9, 0x42,
  29682. 0xA8, 0xD4, 0x95, 0xAF, 0xCB, 0x06, 0xDE, 0x89,
  29683. 0x49, 0x8F, 0xB9, 0x35, 0xB7, 0x75, 0x90, 0x8F,
  29684. 0xE7, 0xA0, 0x3E, 0x32, 0x4D, 0x54, 0xCC, 0x19,
  29685. 0xD4, 0xE1, 0xAA, 0xBD, 0x35, 0x93, 0xB3, 0x8B,
  29686. 0x19, 0xEE, 0x13, 0x88, 0xFE, 0x49, 0x2B, 0x43,
  29687. 0x12, 0x7E, 0x5A, 0x50, 0x42, 0x53, 0x78, 0x6A,
  29688. 0x0D, 0x69, 0xAD, 0x32, 0x60, 0x1C, 0x28, 0xE2,
  29689. 0xC8, 0x85, 0x04, 0xA5, 0xBA, 0x59, 0x97, 0x06,
  29690. 0x02, 0x3A, 0x61, 0x36, 0x3E, 0x17, 0xC6, 0xB9,
  29691. 0xBB, 0x59, 0xBD, 0xC6, 0x97, 0x45, 0x2C, 0xD0,
  29692. 0x59, 0x45, 0x19, 0x83, 0xD7, 0x38, 0xCA, 0x3F,
  29693. 0xD0, 0x34, 0xE3, 0xF5, 0x98, 0x88, 0x54, 0xCA,
  29694. 0x05, 0x03, 0x1D, 0xB0, 0x96, 0x11, 0x49, 0x89,
  29695. 0x88, 0x19, 0x7C, 0x6B, 0x30, 0xD2, 0x58, 0xDF,
  29696. 0xE2, 0x62, 0x65, 0x54, 0x1C, 0x89, 0xA4, 0xB3,
  29697. 0x1D, 0x68, 0x64, 0xE9, 0x38, 0x9B, 0x03, 0xCB,
  29698. 0x74, 0xF7, 0xEC, 0x43, 0x23, 0xFB, 0x94, 0x21,
  29699. 0xA4, 0xB9, 0x79, 0x0A, 0x26, 0xD1, 0x7B, 0x03,
  29700. 0x98, 0xA2, 0x67, 0x67, 0x35, 0x09, 0x09, 0xF8,
  29701. 0x4D, 0x57, 0xB6, 0x69, 0x4D, 0xF8, 0x30, 0x66,
  29702. 0x4C, 0xA8, 0xB3, 0xC3, 0xC0, 0x3E, 0xD2, 0xAE,
  29703. 0x67, 0xB8, 0x90, 0x06, 0x86, 0x8A, 0x68, 0x52,
  29704. 0x7C, 0xCD, 0x66, 0x64, 0x59, 0xAB, 0x7F, 0x05,
  29705. 0x66, 0x71, 0x00, 0x0C, 0x61, 0x64, 0xD3, 0xA7,
  29706. 0xF2, 0x66, 0xA1, 0x4D, 0x97, 0xCB, 0xD7, 0x00,
  29707. 0x4D, 0x6C, 0x92, 0xCA, 0xCA, 0x77, 0x0B, 0x84,
  29708. 0x4A, 0x4F, 0xA9, 0xB1, 0x82, 0xE7, 0xB1, 0x8C,
  29709. 0xA8, 0x85, 0x08, 0x2A, 0xC5, 0x64, 0x6F, 0xCB,
  29710. 0x4A, 0x14, 0xE1, 0x68, 0x5F, 0xEB, 0x0C, 0x9C,
  29711. 0xE3, 0x37, 0x2A, 0xB9, 0x53, 0x65, 0xC0, 0x4F,
  29712. 0xD8, 0x30, 0x84, 0xF8, 0x0A, 0x23, 0xFF, 0x10,
  29713. 0xA0, 0x5B, 0xF1, 0x5F, 0x7F, 0xA5, 0xAC, 0xC6,
  29714. 0xC0, 0xCB, 0x46, 0x2C, 0x33, 0xCA, 0x52, 0x4F,
  29715. 0xA6, 0xB8, 0xBB, 0x35, 0x90, 0x43, 0xBA, 0x68,
  29716. 0x60, 0x9E, 0xAA, 0x25, 0x36, 0xE8, 0x1D, 0x08,
  29717. 0x46, 0x3B, 0x19, 0x65, 0x3B, 0x54, 0x35, 0xBA,
  29718. 0x94, 0x6C, 0x9A, 0xDD, 0xEB, 0x20, 0x2B, 0x04,
  29719. 0xB0, 0x31, 0xCC, 0x96, 0x0D, 0xCC, 0x12, 0xE4,
  29720. 0x51, 0x8D, 0x42, 0x8B, 0x32, 0xB2, 0x57, 0xA4,
  29721. 0xFC, 0x73, 0x13, 0xD3, 0xA7, 0x98, 0x0D, 0x80,
  29722. 0x08, 0x2E, 0x93, 0x4F, 0x9D, 0x95, 0xC3, 0x2B,
  29723. 0x0A, 0x01, 0x91, 0xA2, 0x36, 0x04, 0x38, 0x4D,
  29724. 0xD9, 0xE0, 0x79, 0xBB, 0xBA, 0xA2, 0x66, 0xD1,
  29725. 0x4C, 0x3F, 0x75, 0x6B, 0x9F, 0x21, 0x33, 0x10,
  29726. 0x74, 0x33, 0xA4, 0xE8, 0x3F, 0xA7, 0x18, 0x72,
  29727. 0x82, 0xA8, 0x09, 0x20, 0x3A, 0x4F, 0xAF, 0x84,
  29728. 0x18, 0x51, 0x83, 0x3D, 0x12, 0x1A, 0xC3, 0x83,
  29729. 0x84, 0x3A, 0x5E, 0x55, 0xBC, 0x23, 0x81, 0x42,
  29730. 0x5E, 0x16, 0xC7, 0xDB, 0x4C, 0xC9, 0xAB, 0x5C,
  29731. 0x1B, 0x0D, 0x91, 0xA4, 0x7E, 0x2B, 0x8D, 0xE0,
  29732. 0xE5, 0x82, 0xC8, 0x6B, 0x6B, 0x0D, 0x90, 0x7B,
  29733. 0xB3, 0x60, 0xB9, 0x7F, 0x40, 0xAB, 0x5D, 0x03,
  29734. 0x8F, 0x6B, 0x75, 0xC8, 0x14, 0xB2, 0x7D, 0x9B,
  29735. 0x96, 0x8D, 0x41, 0x98, 0x32, 0xBC, 0x8C, 0x2B,
  29736. 0xEE, 0x60, 0x5E, 0xF6, 0xE5, 0x05, 0x9D, 0x33,
  29737. 0x10, 0x0D, 0x90, 0x48, 0x5D, 0x37, 0x84, 0x50,
  29738. 0x01, 0x42, 0x21, 0x73, 0x6C, 0x07, 0x40, 0x7C,
  29739. 0xAC, 0x26, 0x04, 0x08, 0xAA, 0x64, 0x92, 0x66,
  29740. 0x19, 0x78, 0x8B, 0x86, 0x01, 0xC2, 0xA7, 0x52,
  29741. 0xD1, 0xA6, 0xCB, 0xF8, 0x20, 0xD7, 0xC7, 0xA0,
  29742. 0x47, 0x16, 0x20, 0x32, 0x25, 0xB3, 0x89, 0x5B,
  29743. 0x93, 0x42, 0xD1, 0x47, 0xA8, 0x18, 0x5C, 0xFC,
  29744. 0x1B, 0xB6, 0x5B, 0xA0, 0x6B, 0x41, 0x42, 0x33,
  29745. 0x99, 0x03, 0xC0, 0xAC, 0x46, 0x51, 0x38, 0x5B,
  29746. 0x45, 0xD9, 0x8A, 0x8B, 0x19, 0xD2, 0x8C, 0xD6,
  29747. 0xBA, 0xB0, 0x88, 0x78, 0x7F, 0x7E, 0xE1, 0xB1,
  29748. 0x24, 0x61, 0x76, 0x6B, 0x43, 0xCB, 0xCC, 0xB9,
  29749. 0x64, 0x34, 0x42, 0x7D, 0x93, 0xC0, 0x65, 0x55,
  29750. 0x06, 0x88, 0xF6, 0x94, 0x8E, 0xD1, 0xB5, 0x47,
  29751. 0x5A, 0x42, 0x5F, 0x1B, 0x85, 0x20, 0x9D, 0x06,
  29752. 0x1C, 0x08, 0xB5, 0x6C, 0x1C, 0xC0, 0x69, 0xF6,
  29753. 0xC0, 0xA7, 0xC6, 0xF2, 0x93, 0x58, 0xCA, 0xB9,
  29754. 0x11, 0x08, 0x77, 0x32, 0xA6, 0x49, 0xD2, 0x7C,
  29755. 0x9B, 0x98, 0xF9, 0xA4, 0x88, 0x79, 0x38, 0x7D,
  29756. 0x9B, 0x00, 0xC2, 0x59, 0x59, 0xA7, 0x16, 0x54,
  29757. 0xD6, 0xF6, 0xA9, 0x46, 0x16, 0x45, 0x13, 0xE4,
  29758. 0x7A, 0x75, 0xD0, 0x05, 0x98, 0x6C, 0x23, 0x63,
  29759. 0xC0, 0x9F, 0x6B, 0x53, 0x7E, 0xCA, 0x78, 0xB9,
  29760. 0x30, 0x3A, 0x5F, 0xA4, 0x57, 0x60, 0x8A, 0x58,
  29761. 0x6A, 0x65, 0x3A, 0x34, 0x7D, 0xB0, 0x4D, 0xFC,
  29762. 0xC1, 0x91, 0x75, 0xB3, 0xA3, 0x01, 0x17, 0x25,
  29763. 0x36, 0x06, 0x2A, 0x65, 0x8A, 0x95, 0x27, 0x75,
  29764. 0x70, 0xC8, 0x85, 0x2C, 0xA8, 0x97, 0x3F, 0x4A,
  29765. 0xE1, 0x23, 0xA3, 0x34, 0x04, 0x7D, 0xD7, 0x11,
  29766. 0xC8, 0x92, 0x7A, 0x63, 0x4A, 0x03, 0x38, 0x8A,
  29767. 0x52, 0x7B, 0x03, 0x4B, 0xF7, 0xA8, 0x17, 0x0F,
  29768. 0xA7, 0x02, 0xC1, 0xF7, 0xC2, 0x3E, 0xC3, 0x2D,
  29769. 0x18, 0xA2, 0x37, 0x48, 0x90, 0xBE, 0x9C, 0x78,
  29770. 0x7A, 0x94, 0x09, 0xC8, 0x2D, 0x19, 0x2C, 0x4B,
  29771. 0xB7, 0x05, 0xA2, 0xF9, 0x96, 0xCE, 0x40, 0x5D,
  29772. 0xA7, 0x2C, 0x2D, 0x9C, 0x84, 0x3E, 0xE9, 0xF8,
  29773. 0x31, 0x3E, 0xCC, 0x7F, 0x86, 0xD6, 0x29, 0x4D,
  29774. 0x59, 0x15, 0x9D, 0x9A, 0x87, 0x9A, 0x54, 0x2E,
  29775. 0x26, 0x09, 0x22, 0xAD, 0xF9, 0x99, 0x05, 0x1C,
  29776. 0xC4, 0x52, 0x00, 0xC9, 0xFF, 0xDB, 0x60, 0x44,
  29777. 0x9C, 0x49, 0x46, 0x59, 0x79, 0x27, 0x23, 0x67,
  29778. 0xC0, 0x83, 0xA7, 0xD6, 0x26, 0x7A, 0x3E, 0xD7,
  29779. 0xA7, 0xFD, 0x47, 0x95, 0x7C, 0x21, 0x93, 0x27,
  29780. 0xF7, 0xCA, 0x73, 0xA4, 0x00, 0x7E, 0x16, 0x27,
  29781. 0xF0, 0x0B, 0x11, 0xCC, 0x80, 0x57, 0x3C, 0x15,
  29782. 0xAE, 0xE6, 0x64, 0x0F, 0xB8, 0x56, 0x2D, 0xFA,
  29783. 0x6B, 0x24, 0x0C, 0xA0, 0xAD, 0x35, 0x1A, 0xC4,
  29784. 0xAC, 0x15, 0x5B, 0x96, 0xC1, 0x4C, 0x8A, 0xB1,
  29785. 0x3D, 0xD2, 0x62, 0xCD, 0xFD, 0x51, 0xC4, 0xBB,
  29786. 0x55, 0x72, 0xFD, 0x61, 0x65, 0x53, 0xD1, 0x7B,
  29787. 0xDD, 0x43, 0x0A, 0xCB, 0xEA, 0x3E, 0x95, 0xF0,
  29788. 0xB6, 0x98, 0xD6, 0x69, 0x90, 0xAB, 0x51, 0xE5,
  29789. 0xD0, 0x37, 0x83, 0xA8, 0xB3, 0xD2, 0x78, 0xA5,
  29790. 0x72, 0x04, 0x54, 0xCF, 0x96, 0x95, 0xCF, 0xDC,
  29791. 0xA0, 0x84, 0x85, 0xBA, 0x09, 0x9C, 0x51, 0xCD,
  29792. 0x92, 0xA7, 0xEA, 0x75, 0x87, 0xC1, 0xD1, 0x5C,
  29793. 0x28, 0xE6, 0x09, 0xA8, 0x18, 0x52, 0x60, 0x1B,
  29794. 0x06, 0x04, 0x01, 0x06, 0x79, 0xAA, 0x48, 0x2D,
  29795. 0x51, 0x26, 0x1E, 0xC3, 0x6E, 0x36, 0xB8, 0x71,
  29796. 0x96, 0x76, 0x21, 0x7F, 0xD7, 0x4C, 0x54, 0x78,
  29797. 0x64, 0x88, 0xF4, 0xB4, 0x96, 0x9C, 0x05, 0xA8,
  29798. 0xBA, 0x27, 0xCA, 0x3A, 0x77, 0xCC, 0xE7, 0x3B,
  29799. 0x96, 0x59, 0x23, 0xCA, 0x55, 0x4E, 0x42, 0x2B,
  29800. 0x9B, 0x61, 0xF4, 0x75, 0x46, 0x41, 0x60, 0x8A,
  29801. 0xC1, 0x6C, 0x9B, 0x85, 0x87, 0xA3, 0x2C, 0x1C,
  29802. 0x5D, 0xD7, 0x88, 0xF8, 0x8B, 0x36, 0xB7, 0x17,
  29803. 0xA4, 0x69, 0x65, 0x63, 0x5D, 0xEB, 0x67, 0xF4,
  29804. 0x5B, 0x12, 0x9B, 0x99, 0x07, 0x09, 0x09, 0xC9,
  29805. 0x3E, 0xB8, 0x0B, 0x42, 0xC2, 0xB3, 0xF3, 0xF7,
  29806. 0x03, 0x43, 0xA7, 0xCF, 0x37, 0xE8, 0x52, 0x0E,
  29807. 0x7B, 0xCF, 0xC4, 0x16, 0xAC, 0xA4, 0xF1, 0x8C,
  29808. 0x79, 0x81, 0x26, 0x2B, 0xA2, 0xBF, 0xC7, 0x56,
  29809. 0xAE, 0x03, 0x27, 0x8F, 0x0E, 0xC6, 0x6D, 0xC2,
  29810. 0x05, 0x76, 0x96, 0x82, 0x4B, 0xA6, 0x76, 0x98,
  29811. 0x65, 0xA6, 0x01, 0xD7, 0x14, 0x8E, 0xF6, 0xF5,
  29812. 0x4E, 0x5A, 0xF5, 0x68, 0x6A, 0xA2, 0x90, 0x6F,
  29813. 0x99, 0x4C, 0xE3, 0x8A, 0x5E, 0x0B, 0x93, 0x8F,
  29814. 0x23, 0x90, 0x07, 0x00, 0x30, 0x22, 0xC0, 0x33,
  29815. 0x92, 0xDF, 0x34, 0x01, 0xB1, 0xE4, 0xA3, 0xA7,
  29816. 0xEB, 0xC6, 0x16, 0x14, 0x49, 0xF7, 0x33, 0x74,
  29817. 0xC8, 0xB0, 0x14, 0x03, 0x69, 0x34, 0x3D, 0x92,
  29818. 0x95, 0xFD, 0xF5, 0x11, 0x84, 0x5C, 0x4A, 0x46,
  29819. 0xEB, 0xAA, 0xB6, 0xCA, 0x54, 0x92, 0xF6, 0x80,
  29820. 0x0B, 0x98, 0xC0, 0xCC, 0x80, 0x36, 0x53, 0xA4,
  29821. 0xB1, 0xD6, 0xE6, 0xAA, 0xED, 0x19, 0x32, 0xBA,
  29822. 0xCC, 0x5F, 0xEF, 0xAA, 0x81, 0x8B, 0xA5, 0x02,
  29823. 0x85, 0x9B, 0xA5, 0x49, 0x4C, 0x5F, 0x54, 0x02,
  29824. 0xC8, 0x53, 0x6A, 0x9C, 0x4C, 0x18, 0x88, 0x15,
  29825. 0x06, 0x17, 0xF8, 0x00, 0x98, 0xF6, 0xB2, 0xA9,
  29826. 0x9C, 0x39, 0xBC, 0x5D, 0xC7, 0xCF, 0x3B, 0x59,
  29827. 0x00, 0xA2, 0x13, 0x29, 0xAB, 0x59, 0x05, 0x3A,
  29828. 0xBA, 0xA6, 0x4E, 0xD1, 0x63, 0xE8, 0x59, 0xA8,
  29829. 0xB3, 0xB3, 0xCA, 0x33, 0x59, 0xB7, 0x50, 0xCC,
  29830. 0xC3, 0xE7, 0x10, 0xC7, 0xAC, 0x43, 0xC8, 0x19,
  29831. 0x1C, 0xB5, 0xD6, 0x88, 0x70, 0xC0, 0x63, 0x91,
  29832. 0xC0, 0xCB, 0x8A, 0xEC, 0x72, 0xB8, 0x97, 0xAC,
  29833. 0x6B, 0xE7, 0xFB, 0xAA, 0xCC, 0x67, 0x6E, 0xD6,
  29834. 0x63, 0x14, 0xC8, 0x36, 0x30, 0xE8, 0x94, 0x48,
  29835. 0xC8, 0x8A, 0x1D, 0xF0, 0x4A, 0xCE, 0xB2, 0x3A,
  29836. 0xBF, 0x2E, 0x40, 0x9E, 0xF3, 0x33, 0xC6, 0x22,
  29837. 0x28, 0x9C, 0x18, 0xA2, 0x13, 0x4E, 0x65, 0x0C,
  29838. 0x45, 0x25, 0x7E, 0x47, 0x47, 0x5F, 0xA3, 0x3A,
  29839. 0xA5, 0x37, 0xA5, 0xA8, 0xF7, 0x68, 0x02, 0x14,
  29840. 0x71, 0x6C, 0x50, 0xD4, 0x70, 0xE3, 0x28, 0x49,
  29841. 0x63, 0xCA, 0x64, 0xF5, 0x46, 0x77, 0xAE, 0xC5,
  29842. 0x4B, 0x52, 0x72, 0x16, 0x2B, 0xF5, 0x2B, 0xC8,
  29843. 0x14, 0x2E, 0x1D, 0x41, 0x83, 0xFC, 0x01, 0x74,
  29844. 0x54, 0xA6, 0xB5, 0xA4, 0x96, 0x83, 0x17, 0x59,
  29845. 0x06, 0x40, 0x24, 0x74, 0x59, 0x78, 0xCB, 0xD5,
  29846. 0x1A, 0x6C, 0xED, 0xC8, 0x95, 0x5D, 0xE4, 0xCC,
  29847. 0x6D, 0x36, 0x36, 0x70, 0xA4, 0x74, 0x66, 0xE8,
  29848. 0x2B, 0xE5, 0xC2, 0x36, 0x03, 0xA1, 0x7B, 0xF2,
  29849. 0x2A, 0xCD, 0xB7, 0xCC, 0x98, 0x4A, 0xF0, 0x8C,
  29850. 0x87, 0xE1, 0x4E, 0x27, 0x75, 0x3C, 0xF5, 0x87,
  29851. 0xA8, 0xEC, 0x34, 0x47, 0xE6, 0x2C, 0x64, 0x9E,
  29852. 0x88, 0x7A, 0x67, 0xC3, 0x6C, 0x9C, 0xE9, 0x87,
  29853. 0x21, 0xB6, 0x97, 0x21, 0x32, 0x75, 0x64, 0x6B,
  29854. 0x19, 0x4F, 0x36, 0x75, 0x86, 0x73, 0xA8, 0xED,
  29855. 0x11, 0x28, 0x44, 0x55, 0xAF, 0xC7, 0xA8, 0x52,
  29856. 0x9F, 0x69, 0xC9, 0x7A, 0x3C, 0x2D, 0x7B, 0x8C,
  29857. 0x63, 0x6C, 0x0B, 0xA5, 0x56, 0x14, 0xB7, 0x68,
  29858. 0xE6, 0x24, 0xE7, 0x12, 0x93, 0x0F, 0x77, 0x61,
  29859. 0x69, 0xB0, 0x17, 0x15, 0x72, 0x53, 0x51, 0xBC,
  29860. 0x74, 0xB4, 0x73, 0x95, 0xED, 0x52, 0xB2, 0x5A,
  29861. 0x13, 0x13, 0xC9, 0x51, 0x64, 0x81, 0x4C, 0x34,
  29862. 0xC9, 0x79, 0xCB, 0xDF, 0xAB, 0x85, 0x95, 0x46,
  29863. 0x62, 0xCA, 0xB4, 0x85, 0xE7, 0x50, 0x87, 0xA9,
  29864. 0x8C, 0xC7, 0x4B, 0xB8, 0x2C, 0xA2, 0xD1, 0xB5,
  29865. 0xBF, 0x28, 0x03, 0x23, 0x84, 0x80, 0x63, 0x8C,
  29866. 0x40, 0xE9, 0x0B, 0x43, 0xC7, 0x46, 0x0E, 0x7A,
  29867. 0xA9, 0x17, 0xF0, 0x10, 0x15, 0x1F, 0xAB, 0x11,
  29868. 0x69, 0x98, 0x7B, 0x37, 0x2A, 0xBB, 0x59, 0x27,
  29869. 0x1F, 0x70, 0x06, 0xC2, 0x4E, 0x60, 0x23, 0x6B,
  29870. 0x84, 0xB9, 0xDD, 0xD6, 0x00, 0x62, 0x37, 0x04,
  29871. 0x25, 0x46, 0x17, 0xFB, 0x49, 0x8D, 0x89, 0xE5,
  29872. 0x8B, 0x03, 0x68, 0xBC, 0xB2, 0x10, 0x3E, 0x79,
  29873. 0x35, 0x3E, 0xB5, 0x87, 0x86, 0x0C, 0x14, 0x22,
  29874. 0xE4, 0x76, 0x16, 0x2E, 0x42, 0x5B, 0xC2, 0x38,
  29875. 0x1D, 0xB8, 0x2C, 0x65, 0x92, 0x73, 0x7E, 0x1D,
  29876. 0xD6, 0x02, 0x86, 0x4B, 0x01, 0x67, 0xA7, 0x1E,
  29877. 0xC1, 0xF2, 0x23, 0x30, 0x5C, 0x02, 0xFE, 0x25,
  29878. 0x05, 0x2A, 0xF2, 0xB3, 0xB5, 0xA5, 0x5A, 0x0D,
  29879. 0x7A, 0x20, 0x22, 0xD9, 0xA7, 0x98, 0xDC, 0x0C,
  29880. 0x58, 0x74, 0xA9, 0x87, 0x02, 0xAA, 0xF4, 0x05,
  29881. 0x4C, 0x5D, 0x80, 0x33, 0x8A, 0x52, 0x48, 0xB5,
  29882. 0xB7, 0xBD, 0x09, 0xC5, 0x3B, 0x5E, 0x2A, 0x08,
  29883. 0x4B, 0x04, 0x7D, 0x27, 0x7A, 0x86, 0x1B, 0x1A,
  29884. 0x73, 0xBB, 0x51, 0x48, 0x8D, 0xE0, 0x4E, 0xF5,
  29885. 0x73, 0xC8, 0x52, 0x30, 0xA0, 0x47, 0x0B, 0x73,
  29886. 0x17, 0x5C, 0x9F, 0xA5, 0x05, 0x94, 0xF6, 0x6A,
  29887. 0x5F, 0x50, 0xB4, 0x15, 0x00, 0x54, 0xC9, 0x3B,
  29888. 0x68, 0x18, 0x6F, 0x8B, 0x5C, 0xBC, 0x49, 0x31,
  29889. 0x6C, 0x85, 0x48, 0xA6, 0x42, 0xB2, 0xB3, 0x6A,
  29890. 0x1D, 0x45, 0x4C, 0x74, 0x89, 0xAC, 0x33, 0xB2,
  29891. 0xD2, 0xCE, 0x66, 0x68, 0x09, 0x67, 0x82, 0xA2,
  29892. 0xC1, 0xE0, 0x86, 0x6D, 0x21, 0xA6, 0x5E, 0x16,
  29893. 0xB5, 0x85, 0xE7, 0xAF, 0x86, 0x18, 0xBD, 0xF3,
  29894. 0x18, 0x4C, 0x19, 0x86, 0x87, 0x85, 0x08, 0x91,
  29895. 0x72, 0x77, 0xB9, 0x3E, 0x10, 0x70, 0x6B, 0x16,
  29896. 0x14, 0x97, 0x2B, 0x2A, 0x94, 0xC7, 0x31, 0x0F,
  29897. 0xE9, 0xC7, 0x08, 0xC2, 0x31, 0xA1, 0xA8, 0xAC,
  29898. 0x8D, 0x93, 0x14, 0xA5, 0x29, 0xA9, 0x7F, 0x46,
  29899. 0x9B, 0xF6, 0x49, 0x62, 0xD8, 0x20, 0x64, 0x84,
  29900. 0x43, 0x09, 0x9A, 0x07, 0x6D, 0x55, 0xD4, 0xCE,
  29901. 0xA8, 0x24, 0xA5, 0x83, 0x04, 0x84, 0x4F, 0x99,
  29902. 0x49, 0x7C, 0x10, 0xA2, 0x51, 0x48, 0x61, 0x8A,
  29903. 0x31, 0x5D, 0x72, 0xCA, 0x85, 0x7D, 0x1B, 0x04,
  29904. 0xD5, 0x75, 0xB9, 0x4F, 0x85, 0xC0, 0x1D, 0x19,
  29905. 0xBE, 0xF2, 0x11, 0xBF, 0x0A, 0xA3, 0x36, 0x2E,
  29906. 0x70, 0x41, 0xFD, 0x16, 0x59, 0x6D, 0x80, 0x8E,
  29907. 0x86, 0x7B, 0x44, 0xC4, 0xC0, 0x0D, 0x1C, 0xDA,
  29908. 0x34, 0x18, 0x96, 0x77, 0x17, 0xF1, 0x47, 0xD0,
  29909. 0xEB, 0x21, 0xB4, 0x2A, 0xAE, 0xE7, 0x4A, 0xC3,
  29910. 0x5D, 0x0B, 0x92, 0x41, 0x4B, 0x95, 0x85, 0x31,
  29911. 0xAA, 0xDF, 0x46, 0x3E, 0xC6, 0x30, 0x5A, 0xE5,
  29912. 0xEC, 0xAF, 0x79, 0x17, 0x40, 0x02, 0xF2, 0x6D,
  29913. 0xDE, 0xCC, 0x81, 0x3B, 0xF3, 0x26, 0x72, 0xE8,
  29914. 0x52, 0x9D, 0x95, 0xA4, 0xE7, 0x30, 0xA7, 0xAB,
  29915. 0x4A, 0x3E, 0x8F, 0x8A, 0x8A, 0xF9, 0x79, 0xA6,
  29916. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  29917. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  29918. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  29919. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  29920. 0xD4, 0xEC, 0x14, 0x3B, 0x50, 0xF0, 0x14, 0x23,
  29921. 0xB1, 0x77, 0x89, 0x5E, 0xDE, 0xE2, 0x2B, 0xB7,
  29922. 0x39, 0xF6, 0x47, 0xEC, 0xF8, 0x5F, 0x50, 0xBC,
  29923. 0x25, 0xEF, 0x7B, 0x5A, 0x72, 0x5D, 0xEE, 0x86,
  29924. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  29925. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  29926. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  29927. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  29928. };
  29929. const byte kyber768_ct[] = {
  29930. 0xB5, 0x2C, 0x56, 0xB9, 0x2A, 0x4B, 0x7C, 0xE9,
  29931. 0xE4, 0xCB, 0x7C, 0x5B, 0x1B, 0x16, 0x31, 0x67,
  29932. 0xA8, 0xA1, 0x67, 0x5B, 0x2F, 0xDE, 0xF8, 0x4A,
  29933. 0x5B, 0x67, 0xCA, 0x15, 0xDB, 0x69, 0x4C, 0x9F,
  29934. 0x11, 0xBD, 0x02, 0x7C, 0x30, 0xAE, 0x22, 0xEC,
  29935. 0x92, 0x1A, 0x1D, 0x91, 0x15, 0x99, 0xAF, 0x05,
  29936. 0x85, 0xE4, 0x8D, 0x20, 0xDA, 0x70, 0xDF, 0x9F,
  29937. 0x39, 0xE3, 0x2E, 0xF9, 0x5D, 0x4C, 0x8F, 0x44,
  29938. 0xBF, 0xEF, 0xDA, 0xA5, 0xDA, 0x64, 0xF1, 0x05,
  29939. 0x46, 0x31, 0xD0, 0x4D, 0x6D, 0x3C, 0xFD, 0x0A,
  29940. 0x54, 0x0D, 0xD7, 0xBA, 0x38, 0x86, 0xE4, 0xB5,
  29941. 0xF1, 0x3E, 0x87, 0x87, 0x88, 0x60, 0x4C, 0x95,
  29942. 0xC0, 0x96, 0xEA, 0xB3, 0x91, 0x9F, 0x42, 0x75,
  29943. 0x21, 0x41, 0x9A, 0x94, 0x6C, 0x26, 0xCC, 0x04,
  29944. 0x14, 0x75, 0xD7, 0x12, 0x4C, 0xDC, 0x01, 0xD0,
  29945. 0x37, 0x3E, 0x5B, 0x09, 0xC7, 0xA7, 0x06, 0x03,
  29946. 0xCF, 0xDB, 0x4F, 0xB3, 0x40, 0x50, 0x23, 0xF2,
  29947. 0x26, 0x4D, 0xC3, 0xF9, 0x83, 0xC4, 0xFC, 0x02,
  29948. 0xA2, 0xD1, 0xB2, 0x68, 0xF2, 0x20, 0x8A, 0x1F,
  29949. 0x6E, 0x2A, 0x62, 0x09, 0xBF, 0xF1, 0x2F, 0x6F,
  29950. 0x46, 0x5F, 0x0B, 0x06, 0x9C, 0x3A, 0x7F, 0x84,
  29951. 0xF6, 0x06, 0xD8, 0xA9, 0x40, 0x64, 0x00, 0x3D,
  29952. 0x6E, 0xC1, 0x14, 0xC8, 0xE8, 0x08, 0xD3, 0x05,
  29953. 0x38, 0x84, 0xC1, 0xD5, 0xA1, 0x42, 0xFB, 0xF2,
  29954. 0x01, 0x12, 0xEB, 0x36, 0x0F, 0xDA, 0x3F, 0x0F,
  29955. 0x28, 0xB1, 0x72, 0xAE, 0x50, 0xF5, 0xE7, 0xD8,
  29956. 0x38, 0x01, 0xFB, 0x3F, 0x00, 0x64, 0xB6, 0x87,
  29957. 0x18, 0x70, 0x74, 0xBD, 0x7F, 0xE3, 0x0E, 0xDD,
  29958. 0xAA, 0x33, 0x4C, 0xF8, 0xFC, 0x04, 0xFA, 0x8C,
  29959. 0xED, 0x89, 0x9C, 0xEA, 0xDE, 0x4B, 0x4F, 0x28,
  29960. 0xB6, 0x83, 0x72, 0xBA, 0xF9, 0x8F, 0xF4, 0x82,
  29961. 0xA4, 0x15, 0xB7, 0x31, 0x15, 0x5B, 0x75, 0xCE,
  29962. 0xB9, 0x76, 0xBE, 0x0E, 0xA0, 0x28, 0x5B, 0xA0,
  29963. 0x1A, 0x27, 0xF1, 0x85, 0x7A, 0x8F, 0xB3, 0x77,
  29964. 0xA3, 0xAE, 0x0C, 0x23, 0xB2, 0xAA, 0x9A, 0x07,
  29965. 0x9B, 0xFA, 0xBF, 0xF0, 0xD5, 0xB2, 0xF1, 0xCD,
  29966. 0x9B, 0x71, 0x8B, 0xEA, 0x03, 0xC4, 0x2F, 0x34,
  29967. 0x3A, 0x39, 0xB4, 0xF1, 0x42, 0xD0, 0x1A, 0xD8,
  29968. 0xAC, 0xBB, 0x50, 0xE3, 0x88, 0x53, 0xCF, 0x9A,
  29969. 0x50, 0xC8, 0xB4, 0x4C, 0x3C, 0xF6, 0x71, 0xA4,
  29970. 0xA9, 0x04, 0x3B, 0x26, 0xDD, 0xBB, 0x24, 0x95,
  29971. 0x9A, 0xD6, 0x71, 0x5C, 0x08, 0x52, 0x18, 0x55,
  29972. 0xC7, 0x9A, 0x23, 0xB9, 0xC3, 0xD6, 0x47, 0x17,
  29973. 0x49, 0xC4, 0x07, 0x25, 0xBD, 0xD5, 0xC2, 0x77,
  29974. 0x6D, 0x43, 0xAE, 0xD2, 0x02, 0x04, 0xBA, 0xA1,
  29975. 0x41, 0xEF, 0xB3, 0x30, 0x49, 0x17, 0x47, 0x4B,
  29976. 0x7F, 0x9F, 0x7A, 0x4B, 0x08, 0xB1, 0xA9, 0x3D,
  29977. 0xAE, 0xD9, 0x8C, 0x67, 0x49, 0x53, 0x59, 0xD3,
  29978. 0x7D, 0x67, 0xF7, 0x43, 0x8B, 0xEE, 0x5E, 0x43,
  29979. 0x58, 0x56, 0x34, 0xB2, 0x6C, 0x6B, 0x38, 0x10,
  29980. 0xD7, 0xCD, 0xCB, 0xC0, 0xF6, 0xEB, 0x87, 0x7A,
  29981. 0x60, 0x87, 0xE6, 0x8A, 0xCB, 0x84, 0x80, 0xD3,
  29982. 0xA8, 0xCF, 0x69, 0x00, 0x44, 0x7E, 0x49, 0xB4,
  29983. 0x17, 0xF1, 0x5A, 0x53, 0xB6, 0x07, 0xA0, 0xE2,
  29984. 0x16, 0xB8, 0x55, 0x97, 0x0D, 0x37, 0x40, 0x68,
  29985. 0x70, 0xB4, 0x56, 0x87, 0x22, 0xDA, 0x77, 0xA4,
  29986. 0x08, 0x47, 0x03, 0x81, 0x67, 0x84, 0xE2, 0xF1,
  29987. 0x6B, 0xED, 0x18, 0x99, 0x65, 0x32, 0xC5, 0xD8,
  29988. 0xB7, 0xF5, 0xD2, 0x14, 0x46, 0x4E, 0x5F, 0x3F,
  29989. 0x6E, 0x90, 0x58, 0x67, 0xB0, 0xCE, 0x11, 0x9E,
  29990. 0x25, 0x2A, 0x66, 0x71, 0x32, 0x53, 0x54, 0x46,
  29991. 0x85, 0xD2, 0x08, 0xE1, 0x72, 0x39, 0x08, 0xA0,
  29992. 0xCE, 0x97, 0x83, 0x46, 0x52, 0xE0, 0x8A, 0xE7,
  29993. 0xBD, 0xC8, 0x81, 0xA1, 0x31, 0xB7, 0x3C, 0x71,
  29994. 0xE8, 0x4D, 0x20, 0xD6, 0x8F, 0xDE, 0xFF, 0x4F,
  29995. 0x5D, 0x70, 0xCD, 0x1A, 0xF5, 0x7B, 0x78, 0xE3,
  29996. 0x49, 0x1A, 0x98, 0x65, 0x94, 0x23, 0x21, 0x80,
  29997. 0x0A, 0x20, 0x3C, 0x05, 0xED, 0x1F, 0xEE, 0xB5,
  29998. 0xA2, 0x8E, 0x58, 0x4E, 0x19, 0xF6, 0x53, 0x5E,
  29999. 0x7F, 0x84, 0xE4, 0xA2, 0x4F, 0x84, 0xA7, 0x2D,
  30000. 0xCA, 0xF5, 0x64, 0x8B, 0x4A, 0x42, 0x35, 0xDD,
  30001. 0x66, 0x44, 0x64, 0x48, 0x2F, 0x03, 0x17, 0x6E,
  30002. 0x88, 0x8C, 0x28, 0xBF, 0xC6, 0xC1, 0xCB, 0x23,
  30003. 0x8C, 0xFF, 0xA3, 0x5A, 0x32, 0x1E, 0x71, 0x79,
  30004. 0x1D, 0x9E, 0xA8, 0xED, 0x08, 0x78, 0xC6, 0x11,
  30005. 0x21, 0xBF, 0x8D, 0x2A, 0x4A, 0xB2, 0xC1, 0xA5,
  30006. 0xE1, 0x20, 0xBC, 0x40, 0xAB, 0xB1, 0x89, 0x2D,
  30007. 0x17, 0x15, 0x09, 0x0A, 0x0E, 0xE4, 0x82, 0x52,
  30008. 0xCA, 0x29, 0x7A, 0x99, 0xAA, 0x0E, 0x51, 0x0C,
  30009. 0xF2, 0x6B, 0x1A, 0xDD, 0x06, 0xCA, 0x54, 0x3E,
  30010. 0x1C, 0x5D, 0x6B, 0xDC, 0xD3, 0xB9, 0xC5, 0x85,
  30011. 0xC8, 0x53, 0x80, 0x45, 0xDB, 0x5C, 0x25, 0x2E,
  30012. 0xC3, 0xC8, 0xC3, 0xC9, 0x54, 0xD9, 0xBE, 0x59,
  30013. 0x07, 0x09, 0x4A, 0x89, 0x4E, 0x60, 0xEA, 0xB4,
  30014. 0x35, 0x38, 0xCF, 0xEE, 0x82, 0xE8, 0xFF, 0xC0,
  30015. 0x79, 0x1B, 0x0D, 0x0F, 0x43, 0xAC, 0x16, 0x27,
  30016. 0x83, 0x0A, 0x61, 0xD5, 0x6D, 0xAD, 0x96, 0xC6,
  30017. 0x29, 0x58, 0xB0, 0xDE, 0x78, 0x0B, 0x78, 0xBD,
  30018. 0x47, 0xA6, 0x04, 0x55, 0x0D, 0xAB, 0x83, 0xFF,
  30019. 0xF2, 0x27, 0xC3, 0x24, 0x04, 0x94, 0x71, 0xF3,
  30020. 0x52, 0x48, 0xCF, 0xB8, 0x49, 0xB2, 0x57, 0x24,
  30021. 0xFF, 0x70, 0x4D, 0x52, 0x77, 0xAA, 0x35, 0x2D,
  30022. 0x55, 0x09, 0x58, 0xBE, 0x3B, 0x23, 0x7D, 0xFF,
  30023. 0x47, 0x3E, 0xC2, 0xAD, 0xBA, 0xEA, 0x48, 0xCA,
  30024. 0x26, 0x58, 0xAE, 0xFC, 0xC7, 0x7B, 0xBD, 0x42,
  30025. 0x64, 0xAB, 0x37, 0x4D, 0x70, 0xEA, 0xE5, 0xB9,
  30026. 0x64, 0x41, 0x6C, 0xE8, 0x22, 0x6A, 0x7E, 0x32,
  30027. 0x55, 0xA0, 0xF8, 0xD7, 0xE2, 0xAD, 0xCA, 0x06,
  30028. 0x2B, 0xCD, 0x6D, 0x78, 0xD6, 0x0D, 0x1B, 0x32,
  30029. 0xE1, 0x14, 0x05, 0xBE, 0x54, 0xB6, 0x6E, 0xF0,
  30030. 0xFD, 0xDD, 0x56, 0x77, 0x02, 0xA3, 0xBC, 0xCF,
  30031. 0xED, 0xE3, 0xC5, 0x84, 0x70, 0x12, 0x69, 0xED,
  30032. 0x14, 0x80, 0x9F, 0x06, 0xF8, 0x96, 0x83, 0x56,
  30033. 0xBB, 0x92, 0x67, 0xFE, 0x86, 0xE5, 0x14, 0x25,
  30034. 0x2E, 0x88, 0xBB, 0x5C, 0x30, 0xA7, 0xEC, 0xB3,
  30035. 0xD0, 0xE6, 0x21, 0x02, 0x1E, 0xE0, 0xFB, 0xF7,
  30036. 0x87, 0x1B, 0x09, 0x34, 0x2B, 0xF8, 0x4F, 0x55,
  30037. 0xC9, 0x7E, 0xAF, 0x86, 0xC4, 0x81, 0x89, 0xC7,
  30038. 0xFF, 0x4D, 0xF3, 0x89, 0xF0, 0x77, 0xE2, 0x80,
  30039. 0x6E, 0x5F, 0xA7, 0x3B, 0x3E, 0x94, 0x58, 0xA1,
  30040. 0x6C, 0x7E, 0x27, 0x5F, 0x4F, 0x60, 0x22, 0x75,
  30041. 0x58, 0x0E, 0xB7, 0xB7, 0x13, 0x5F, 0xB5, 0x37,
  30042. 0xFA, 0x0C, 0xD9, 0x5D, 0x6E, 0xA5, 0x8C, 0x10,
  30043. 0x8C, 0xD8, 0x94, 0x3D, 0x70, 0xC1, 0x64, 0x31,
  30044. 0x11, 0xF4, 0xF0, 0x1C, 0xA8, 0xA8, 0x27, 0x6A,
  30045. 0x90, 0x26, 0x66, 0xED, 0x81, 0xB7, 0x8D, 0x16,
  30046. 0x8B, 0x00, 0x6F, 0x16, 0xAA, 0xA3, 0xD8, 0xE4,
  30047. 0xCE, 0x4F, 0x4D, 0x0F, 0xB0, 0x99, 0x7E, 0x41,
  30048. 0xAE, 0xFF, 0xB5, 0xB3, 0xDA, 0xA8, 0x38, 0x73,
  30049. 0x2F, 0x35, 0x73, 0x49, 0x44, 0x7F, 0x38, 0x77,
  30050. 0x76, 0xC7, 0x93, 0xC0, 0x47, 0x9D, 0xE9, 0xE9,
  30051. 0x94, 0x98, 0xCC, 0x35, 0x6F, 0xDB, 0x00, 0x75,
  30052. 0xA7, 0x03, 0xF2, 0x3C, 0x55, 0xD4, 0x7B, 0x55,
  30053. 0x0E, 0xC8, 0x9B, 0x02, 0xAD, 0xE8, 0x93, 0x29,
  30054. 0x08, 0x6A, 0x50, 0x84, 0x34, 0x56, 0xFE, 0xDC,
  30055. 0x37, 0x88, 0xAC, 0x8D, 0x97, 0x23, 0x3C, 0x54,
  30056. 0x56, 0x04, 0x67, 0xEE, 0x1D, 0x0F, 0x02, 0x4B,
  30057. 0x18, 0x42, 0x8F, 0x0D, 0x73, 0xB3, 0x0E, 0x19,
  30058. 0xF5, 0xC6, 0x3B, 0x9A, 0xBF, 0x11, 0x41, 0x5B,
  30059. 0xEA, 0x4D, 0x01, 0x70, 0x13, 0x0B, 0xAA, 0xBD,
  30060. 0x33, 0xC0, 0x5E, 0x65, 0x24, 0xE5, 0xFB, 0x55,
  30061. 0x81, 0xB2, 0x2B, 0x04, 0x33, 0x34, 0x22, 0x48,
  30062. 0x26, 0x6D, 0x0F, 0x10, 0x53, 0xB2, 0x45, 0xCC,
  30063. 0x24, 0x62, 0xDC, 0x44, 0xD3, 0x49, 0x65, 0x10,
  30064. 0x24, 0x82, 0xA8, 0xED, 0x9E, 0x4E, 0x96, 0x4D,
  30065. 0x56, 0x83, 0xE5, 0xD4, 0x5D, 0x0C, 0x82, 0x69
  30066. };
  30067. const byte kyber768_ss[] = {
  30068. 0x91, 0x4C, 0xB6, 0x7F, 0xE5, 0xC3, 0x8E, 0x73,
  30069. 0xBF, 0x74, 0x18, 0x1C, 0x0A, 0xC5, 0x04, 0x28,
  30070. 0xDE, 0xDF, 0x77, 0x50, 0xA9, 0x80, 0x58, 0xF7,
  30071. 0xD5, 0x36, 0x70, 0x87, 0x74, 0x53, 0x5B, 0x29
  30072. };
  30073. ret = wc_KyberKey_Init(KYBER768, &key, HEAP_HINT, INVALID_DEVID);
  30074. if (ret != 0)
  30075. return WC_TEST_RET_ENC_EC(ret);
  30076. ret = wc_KyberKey_MakeKeyWithRandom(&key, kyber768_rand,
  30077. sizeof(kyber768_rand));
  30078. if (ret != 0)
  30079. return WC_TEST_RET_ENC_EC(ret);
  30080. ret = wc_KyberKey_EncodePublicKey(&key, pub, sizeof(pub));
  30081. if (ret != 0)
  30082. return WC_TEST_RET_ENC_EC(ret);
  30083. ret = wc_KyberKey_EncodePrivateKey(&key, priv, sizeof(priv));
  30084. if (ret != 0)
  30085. return WC_TEST_RET_ENC_EC(ret);
  30086. if (XMEMCMP(pub, kyber768_pk, sizeof(kyber768_pk)) != 0)
  30087. return WC_TEST_RET_ENC_NC;
  30088. if (XMEMCMP(priv, kyber768_sk, sizeof(kyber768_sk)) != 0)
  30089. return WC_TEST_RET_ENC_NC;
  30090. ret = wc_KyberKey_EncapsulateWithRandom(&key, ct, ss, kyber768enc_rand,
  30091. sizeof(kyber768enc_rand));
  30092. if (ret != 0)
  30093. return WC_TEST_RET_ENC_EC(ret);
  30094. if (XMEMCMP(ct, kyber768_ct, sizeof(kyber768_ct)) != 0)
  30095. return WC_TEST_RET_ENC_NC;
  30096. if (XMEMCMP(ss, kyber768_ss, sizeof(kyber768_ss)) != 0)
  30097. return WC_TEST_RET_ENC_NC;
  30098. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, sizeof(kyber768_ct));
  30099. if (ret != 0)
  30100. return WC_TEST_RET_ENC_EC(ret);
  30101. if (XMEMCMP(ss_dec, kyber768_ss, sizeof(kyber768_ss)) != 0)
  30102. return WC_TEST_RET_ENC_NC;
  30103. wc_KyberKey_Free(&key);
  30104. return 0;
  30105. }
  30106. #endif /* WOLFSSL_KYBER768 */
  30107. #ifdef WOLFSSL_KYBER1024
  30108. static wc_test_ret_t kyber1024_kat(void)
  30109. {
  30110. KyberKey key;
  30111. wc_test_ret_t ret;
  30112. byte priv[KYBER1024_PRIVATE_KEY_SIZE];
  30113. byte pub[KYBER1024_PUBLIC_KEY_SIZE];
  30114. byte ct[KYBER1024_CIPHER_TEXT_SIZE];
  30115. byte ss[KYBER_SS_SZ];
  30116. byte ss_dec[KYBER_SS_SZ];
  30117. const byte kyber1024_rand[] = {
  30118. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  30119. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  30120. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  30121. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  30122. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  30123. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  30124. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  30125. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  30126. };
  30127. const byte kyber1024enc_rand[] = {
  30128. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  30129. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  30130. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  30131. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  30132. };
  30133. const byte kyber1024_pk[] = {
  30134. 0xD2, 0x23, 0x02, 0xCB, 0xD3, 0x39, 0x9F, 0xAC,
  30135. 0xC6, 0x30, 0x99, 0x1F, 0xC8, 0xF2, 0x8B, 0xDB,
  30136. 0x43, 0x54, 0x76, 0x25, 0x41, 0x52, 0x76, 0x78,
  30137. 0xBC, 0xF6, 0x1F, 0x65, 0xC2, 0x41, 0x14, 0x6C,
  30138. 0x42, 0x6D, 0x23, 0xB9, 0xBF, 0xAA, 0x6B, 0x7D,
  30139. 0xF1, 0x8C, 0x97, 0xF2, 0x0C, 0x1B, 0x61, 0x25,
  30140. 0xBF, 0x87, 0x4B, 0x1D, 0x89, 0x47, 0x58, 0x52,
  30141. 0xC4, 0x48, 0x21, 0x5D, 0xB0, 0xEB, 0x77, 0x37,
  30142. 0xF9, 0x14, 0x80, 0xE8, 0xCE, 0xBD, 0x9A, 0x08,
  30143. 0x71, 0x57, 0x4F, 0x5A, 0xB6, 0x2D, 0x90, 0x20,
  30144. 0x17, 0x5E, 0xC6, 0x92, 0x7C, 0xA0, 0xB5, 0x4C,
  30145. 0x09, 0x81, 0x8E, 0x42, 0xCF, 0x92, 0xA3, 0x83,
  30146. 0x17, 0x24, 0x22, 0xC7, 0xDC, 0x18, 0x31, 0xD6,
  30147. 0x3B, 0x0C, 0x29, 0x5D, 0xE7, 0x51, 0x59, 0xDB,
  30148. 0x80, 0x34, 0xE9, 0xE0, 0x7F, 0x7B, 0x0B, 0x91,
  30149. 0x0C, 0x3C, 0x1E, 0x5F, 0xB6, 0x6B, 0x3D, 0xC5,
  30150. 0x23, 0xF1, 0xFA, 0x6E, 0xB4, 0x91, 0x0C, 0xB8,
  30151. 0x9A, 0x6C, 0x17, 0x56, 0x2C, 0x83, 0xAB, 0x4C,
  30152. 0x18, 0xD0, 0xCD, 0x7E, 0x07, 0x96, 0x59, 0x2A,
  30153. 0x37, 0x2A, 0xA4, 0x09, 0xB1, 0xC5, 0x57, 0x34,
  30154. 0x7C, 0xCA, 0xCD, 0xC4, 0x64, 0x4A, 0x11, 0x90,
  30155. 0x64, 0xD0, 0x6D, 0xD4, 0x74, 0x92, 0x9D, 0x1C,
  30156. 0x6F, 0xB4, 0xD6, 0x86, 0xE5, 0x49, 0x1C, 0xE4,
  30157. 0xBC, 0x89, 0xA3, 0x0B, 0xB4, 0xB8, 0xC4, 0x1B,
  30158. 0xCE, 0x51, 0x57, 0xDF, 0xC1, 0x36, 0x08, 0x23,
  30159. 0xB1, 0xAB, 0x61, 0x8C, 0x14, 0xB1, 0x0F, 0x98,
  30160. 0xC2, 0x50, 0x67, 0x39, 0x8E, 0xA7, 0x01, 0x8C,
  30161. 0x27, 0x8A, 0x4B, 0x3D, 0xF3, 0x13, 0x34, 0xD6,
  30162. 0x03, 0xB2, 0x04, 0x4E, 0xF1, 0x87, 0xCD, 0x9B,
  30163. 0xC6, 0xCE, 0x42, 0x72, 0x5B, 0xD9, 0x62, 0xC2,
  30164. 0x64, 0x98, 0x3E, 0x9E, 0x18, 0x15, 0x5A, 0x8B,
  30165. 0x9C, 0x47, 0x14, 0x3D, 0x70, 0x46, 0x0A, 0x26,
  30166. 0xA5, 0x6F, 0xE7, 0x65, 0x8C, 0x1F, 0x15, 0x03,
  30167. 0x48, 0xC6, 0x08, 0x7E, 0xF7, 0x58, 0xAD, 0x16,
  30168. 0x78, 0x87, 0x86, 0x0A, 0x00, 0x7A, 0x5F, 0xC3,
  30169. 0x73, 0x58, 0xD4, 0x3B, 0x5E, 0xBE, 0xE8, 0x20,
  30170. 0xAC, 0xEA, 0x47, 0x4F, 0x0A, 0xC0, 0x7B, 0x76,
  30171. 0x80, 0x28, 0x66, 0x19, 0x9C, 0x61, 0x23, 0x1D,
  30172. 0x5C, 0x74, 0x7C, 0x93, 0x77, 0x4D, 0x2C, 0x1E,
  30173. 0x0C, 0x1C, 0x67, 0xE6, 0xC8, 0x1B, 0x82, 0x75,
  30174. 0x21, 0x73, 0xE1, 0x25, 0xBA, 0xF3, 0x9B, 0x4F,
  30175. 0xD1, 0x9A, 0x4F, 0x45, 0x3D, 0xC5, 0x79, 0x76,
  30176. 0xB1, 0xD9, 0x7F, 0xE6, 0x99, 0x69, 0x92, 0xBB,
  30177. 0xB6, 0x5B, 0x7C, 0xB2, 0x5D, 0x07, 0x7B, 0xBA,
  30178. 0xA6, 0xA1, 0x33, 0x22, 0x89, 0x9A, 0xF6, 0x59,
  30179. 0xCF, 0x1B, 0x35, 0x58, 0xC1, 0xB5, 0x00, 0x11,
  30180. 0x54, 0xB6, 0x25, 0x80, 0x9E, 0xD8, 0x9A, 0xEE,
  30181. 0xBB, 0x89, 0xE6, 0xEA, 0x7D, 0x67, 0xF7, 0x23,
  30182. 0xD0, 0x45, 0xAB, 0x05, 0x71, 0x5C, 0x42, 0x35,
  30183. 0x5D, 0xA6, 0xA5, 0xC8, 0xDD, 0x39, 0xC8, 0xAB,
  30184. 0xE3, 0x03, 0x77, 0x51, 0xA0, 0x1E, 0xD1, 0xC7,
  30185. 0x37, 0x49, 0x19, 0xF3, 0x12, 0x1B, 0x5A, 0x52,
  30186. 0xC5, 0x3D, 0x14, 0x87, 0x31, 0x67, 0x69, 0xF8,
  30187. 0x07, 0x21, 0xDE, 0xEA, 0xAA, 0xD3, 0xC9, 0x0F,
  30188. 0x76, 0xE7, 0xAE, 0x9E, 0x12, 0xBA, 0x92, 0xB3,
  30189. 0x2B, 0x5F, 0xD4, 0x57, 0xE3, 0xC7, 0x52, 0xC2,
  30190. 0x65, 0x0D, 0xFB, 0x88, 0x57, 0x71, 0xCB, 0x77,
  30191. 0xAC, 0x3C, 0x78, 0x5A, 0x8C, 0x56, 0x2E, 0x6A,
  30192. 0x1C, 0x63, 0xC2, 0xA5, 0x5E, 0xA4, 0x7C, 0xF8,
  30193. 0xB9, 0x0E, 0xB8, 0x22, 0x5C, 0x12, 0x3C, 0x34,
  30194. 0x64, 0x52, 0x56, 0x62, 0x35, 0xB2, 0xF3, 0x18,
  30195. 0x23, 0xA3, 0x35, 0x21, 0xE0, 0x87, 0x93, 0x7A,
  30196. 0x34, 0x5D, 0x8D, 0x66, 0x3E, 0xEA, 0xA0, 0x56,
  30197. 0x58, 0x91, 0x7B, 0xBA, 0xA0, 0x08, 0xC2, 0xE3,
  30198. 0x35, 0xF8, 0x85, 0x0A, 0x90, 0xA3, 0x26, 0xD0,
  30199. 0xE6, 0x64, 0x32, 0xF4, 0x4C, 0xEB, 0x82, 0x89,
  30200. 0xE4, 0xEC, 0xB2, 0xD1, 0x29, 0x58, 0xE9, 0x84,
  30201. 0x07, 0x2E, 0xCA, 0xCB, 0x88, 0xE1, 0x34, 0x8F,
  30202. 0xF0, 0xB5, 0x56, 0x54, 0xAC, 0xBA, 0x5B, 0x54,
  30203. 0x97, 0x1C, 0xBA, 0xEB, 0xA8, 0x8E, 0xC4, 0xB9,
  30204. 0x1A, 0x94, 0xC3, 0x71, 0x92, 0xFA, 0x98, 0x2B,
  30205. 0xEC, 0xB9, 0xF3, 0xDA, 0x42, 0x16, 0x03, 0xB6,
  30206. 0x1A, 0x51, 0xBC, 0x8E, 0x36, 0xCB, 0xD0, 0x53,
  30207. 0x85, 0x1C, 0x77, 0xB1, 0xB9, 0x26, 0xB1, 0x7A,
  30208. 0x27, 0x2A, 0xA9, 0x02, 0x32, 0x46, 0xB0, 0x2B,
  30209. 0x3E, 0xD4, 0x7F, 0x66, 0xA0, 0x0B, 0xD5, 0x68,
  30210. 0x48, 0x23, 0x63, 0x4E, 0x7C, 0xE5, 0x8C, 0xF8,
  30211. 0xF3, 0x06, 0xE3, 0x5B, 0x1E, 0x53, 0x22, 0x82,
  30212. 0x4D, 0x90, 0x48, 0x01, 0xF0, 0xA2, 0xFA, 0x7C,
  30213. 0x2B, 0xC9, 0xC2, 0x52, 0xB0, 0xA5, 0x6B, 0x7B,
  30214. 0xA2, 0xAB, 0x0F, 0x63, 0x60, 0x21, 0x74, 0x5A,
  30215. 0x70, 0xA9, 0xA4, 0x3E, 0x2B, 0x0A, 0x8D, 0x61,
  30216. 0x59, 0x70, 0xB6, 0x53, 0x09, 0x62, 0x4B, 0x51,
  30217. 0x84, 0xBC, 0xC3, 0x0B, 0x91, 0x16, 0x79, 0xAE,
  30218. 0xDD, 0x76, 0x02, 0x5F, 0xE3, 0x90, 0x8F, 0xD6,
  30219. 0x78, 0x97, 0xB0, 0xCF, 0x4B, 0xE5, 0xA6, 0xF5,
  30220. 0x41, 0x3D, 0x7D, 0xD9, 0x85, 0x64, 0xB2, 0x3E,
  30221. 0x42, 0xA9, 0x3E, 0x4A, 0xA8, 0x82, 0x1C, 0xD4,
  30222. 0x50, 0x54, 0xC6, 0x43, 0xED, 0xC1, 0x15, 0x8D,
  30223. 0xB6, 0xB3, 0xDE, 0xB1, 0x3F, 0xB5, 0xA5, 0x1E,
  30224. 0xBD, 0x1A, 0x8A, 0x78, 0xB8, 0x72, 0x25, 0xA7,
  30225. 0x33, 0x8E, 0x10, 0x11, 0x04, 0xC4, 0xA2, 0x20,
  30226. 0xD9, 0xBD, 0xED, 0xD4, 0x8C, 0x85, 0xA1, 0xC2,
  30227. 0xDA, 0xE7, 0x81, 0xA8, 0x0C, 0x40, 0xE1, 0x3B,
  30228. 0x87, 0xEA, 0xC7, 0x3A, 0x76, 0x42, 0x01, 0xC9,
  30229. 0xB7, 0x60, 0xCC, 0xFB, 0x1A, 0xE3, 0x92, 0x69,
  30230. 0x9C, 0x70, 0x39, 0xD2, 0x7C, 0x39, 0x36, 0x2B,
  30231. 0x27, 0xB8, 0xFC, 0x6F, 0x07, 0xA8, 0xA3, 0xD4,
  30232. 0x41, 0x0F, 0x15, 0x47, 0xC4, 0x8A, 0x99, 0x97,
  30233. 0xF6, 0x2C, 0x61, 0x07, 0x44, 0x52, 0xEF, 0x15,
  30234. 0x15, 0xF8, 0xA6, 0x49, 0xEB, 0xCA, 0x94, 0x37,
  30235. 0x20, 0x5A, 0x4E, 0x8A, 0x61, 0x60, 0x6B, 0x41,
  30236. 0xDA, 0xF6, 0x83, 0x4D, 0x67, 0x1F, 0x4D, 0x85,
  30237. 0x2C, 0x0C, 0x9C, 0x40, 0x96, 0x61, 0x16, 0x48,
  30238. 0xC6, 0xA3, 0x17, 0x06, 0x78, 0xB1, 0x53, 0x7C,
  30239. 0xC1, 0x82, 0x8D, 0x93, 0x58, 0x0C, 0x9E, 0x58,
  30240. 0x49, 0xA9, 0x65, 0x31, 0x75, 0xAC, 0xB7, 0x53,
  30241. 0xF2, 0xBE, 0x74, 0x37, 0xBE, 0x45, 0xF6, 0xC6,
  30242. 0x03, 0xE4, 0x85, 0xF2, 0xEC, 0x30, 0x1B, 0xB4,
  30243. 0x2B, 0x6C, 0x37, 0xC2, 0x25, 0xD7, 0x49, 0x5A,
  30244. 0x58, 0x4A, 0xE2, 0x31, 0x89, 0x0A, 0xB5, 0xC8,
  30245. 0xC3, 0x5C, 0x26, 0x8C, 0xF4, 0xBB, 0xB0, 0x21,
  30246. 0x3C, 0x09, 0x60, 0x19, 0x31, 0x95, 0x61, 0xA8,
  30247. 0xA6, 0x94, 0x76, 0x37, 0xAA, 0x40, 0xD0, 0x06,
  30248. 0xB4, 0x15, 0xBB, 0x2C, 0xFA, 0x22, 0x37, 0xE0,
  30249. 0x89, 0x0B, 0x6A, 0x3B, 0xC1, 0x34, 0xAB, 0xF8,
  30250. 0xF6, 0x58, 0x5E, 0x10, 0x8D, 0x15, 0x94, 0x0F,
  30251. 0x91, 0xF4, 0xBF, 0x5B, 0x0C, 0x81, 0x80, 0x55,
  30252. 0xB2, 0x1D, 0xEA, 0x6E, 0x63, 0xB5, 0x53, 0x98,
  30253. 0x8C, 0x47, 0xF4, 0xB9, 0x4E, 0x7C, 0xF8, 0x00,
  30254. 0xA4, 0x93, 0xB4, 0x73, 0x47, 0x05, 0xED, 0xC5,
  30255. 0x6A, 0x4B, 0x60, 0x21, 0xC6, 0x29, 0x50, 0x06,
  30256. 0x75, 0x87, 0x68, 0x04, 0xCF, 0x0B, 0x95, 0x1F,
  30257. 0x03, 0x8A, 0x5C, 0x7F, 0xE5, 0x8E, 0x89, 0x77,
  30258. 0x4E, 0xF2, 0x99, 0x2F, 0xD7, 0xC6, 0x30, 0x99,
  30259. 0xD3, 0x52, 0xA7, 0xD2, 0x15, 0x60, 0xB7, 0x88,
  30260. 0xB4, 0x05, 0x70, 0x98, 0x61, 0x81, 0x7E, 0x59,
  30261. 0xA9, 0x6B, 0x3A, 0x3A, 0x83, 0xCB, 0xA8, 0x03,
  30262. 0xB1, 0x69, 0x34, 0x33, 0x10, 0x71, 0x90, 0x5B,
  30263. 0xBE, 0xC6, 0x53, 0x29, 0x00, 0x15, 0x5D, 0x8A,
  30264. 0xC8, 0x8C, 0xB3, 0x2E, 0x4E, 0x21, 0xA3, 0xBD,
  30265. 0x3A, 0x03, 0xFD, 0xEC, 0x32, 0x5A, 0x51, 0xCD,
  30266. 0x27, 0x73, 0x96, 0x4E, 0x67, 0x84, 0xFC, 0xF1,
  30267. 0x85, 0x37, 0x37, 0xAA, 0x64, 0xEB, 0x67, 0x56,
  30268. 0x47, 0x27, 0x27, 0x26, 0x61, 0xAB, 0xF8, 0x43,
  30269. 0x13, 0xA5, 0x7A, 0x44, 0xB1, 0x23, 0xC6, 0x55,
  30270. 0x09, 0xCF, 0xB7, 0xA6, 0xF6, 0x64, 0x1C, 0xDC,
  30271. 0xC3, 0xB5, 0x7F, 0xE6, 0x28, 0xC7, 0xB8, 0x19,
  30272. 0x2D, 0xB4, 0x4F, 0xFB, 0xF5, 0x79, 0x6A, 0x86,
  30273. 0x13, 0xB1, 0xFA, 0x12, 0x6F, 0x60, 0x76, 0x88,
  30274. 0x3C, 0x78, 0x3D, 0xC2, 0x4E, 0x2A, 0x44, 0x64,
  30275. 0xC4, 0x0B, 0x3A, 0x41, 0xCA, 0x70, 0xAE, 0x87,
  30276. 0x62, 0x08, 0x66, 0xCF, 0x4F, 0xCB, 0x2B, 0xD2,
  30277. 0x04, 0xBF, 0x5C, 0x28, 0x38, 0x12, 0xBA, 0x05,
  30278. 0x6A, 0xC0, 0xC3, 0x45, 0xE3, 0x79, 0xC4, 0xBA,
  30279. 0x24, 0xD7, 0x50, 0x90, 0x12, 0x79, 0xBB, 0x2F,
  30280. 0x3A, 0x16, 0xF6, 0x12, 0xBF, 0xAD, 0xB3, 0x57,
  30281. 0x03, 0x33, 0x2C, 0x7C, 0x13, 0x6F, 0x68, 0xEA,
  30282. 0xB6, 0x75, 0x5C, 0x66, 0xB6, 0xA4, 0xAD, 0x1A,
  30283. 0xAB, 0xA7, 0xB7, 0x68, 0xA5, 0x8A, 0xCA, 0xAC,
  30284. 0xC1, 0x0A, 0x45, 0x9A, 0x1C, 0xC8, 0xEF, 0x29,
  30285. 0x37, 0x7B, 0xC2, 0x00, 0xE4, 0xD3, 0x15, 0xA3,
  30286. 0x0A, 0x6B, 0xCC, 0x32, 0x56, 0xF9, 0x73, 0x4D,
  30287. 0x06, 0xE9, 0x77, 0x9C, 0xAA, 0x54, 0x42, 0xA9,
  30288. 0xA1, 0x60, 0x69, 0x08, 0x13, 0x77, 0xC7, 0x6E,
  30289. 0x75, 0x15, 0x43, 0x68, 0x07, 0x2D, 0xC4, 0x46,
  30290. 0xED, 0x6C, 0x8B, 0x8E, 0x62, 0x2A, 0x21, 0xE3,
  30291. 0x83, 0xCF, 0x9B, 0xA1, 0xFB, 0x43, 0x4E, 0x2E,
  30292. 0xCC, 0x81, 0xE7, 0xB7, 0x8C, 0xEE, 0x98, 0x6B,
  30293. 0x8F, 0xF7, 0x98, 0xAB, 0x18, 0xCF, 0x96, 0x34,
  30294. 0x54, 0x35, 0x46, 0x28, 0x4E, 0xDA, 0x2A, 0x26,
  30295. 0xB4, 0x7F, 0x05, 0xB7, 0x35, 0xBC, 0xDB, 0x12,
  30296. 0x02, 0x22, 0x00, 0x76, 0xDC, 0x8B, 0x4E, 0x4B,
  30297. 0x9F, 0x85, 0x35, 0x33, 0xC8, 0xF6, 0xC7, 0xFF,
  30298. 0x38, 0x81, 0x7B, 0xA4, 0x97, 0x12, 0x83, 0x57,
  30299. 0x85, 0xF1, 0x7F, 0x14, 0xCA, 0x01, 0xD0, 0xC1,
  30300. 0xC1, 0xE9, 0x88, 0x10, 0xFE, 0x0B, 0x36, 0xE5,
  30301. 0xB4, 0x27, 0x15, 0x7B, 0x94, 0x18, 0x44, 0x9C,
  30302. 0xED, 0xD6, 0x41, 0xA4, 0x29, 0x3C, 0x85, 0xC3,
  30303. 0x27, 0x00, 0x10, 0x2A, 0xCE, 0xC2, 0x2E, 0xBA,
  30304. 0xD9, 0x8E, 0xD1, 0x60, 0xA5, 0xF0, 0x27, 0xBD,
  30305. 0x4C, 0xDA, 0x57, 0xF1, 0xF3, 0x72, 0x0A, 0x12,
  30306. 0xC1, 0x34, 0x65, 0x4D, 0xD5, 0xE7, 0x3F, 0x82,
  30307. 0x96, 0x76, 0x49, 0x53, 0x90, 0xD0, 0xE7, 0x92,
  30308. 0x9D, 0x60, 0x34, 0xE9, 0xC5, 0x5F, 0x7D, 0x55,
  30309. 0xBA, 0x65, 0x8B, 0xC5, 0x87, 0x98, 0x8E, 0x8A,
  30310. 0xF9, 0x49, 0x60, 0xF6, 0xCF, 0xB8, 0xD5, 0xAF,
  30311. 0x7A, 0x00, 0x21, 0x53, 0x5A, 0x6E, 0x25, 0xE4,
  30312. 0x37, 0xD4, 0x9A, 0x78, 0x06, 0x98, 0xBE, 0x22,
  30313. 0xAC, 0x99, 0x53, 0x94, 0x9F, 0x57, 0x1B, 0x85,
  30314. 0xA6, 0x85, 0x72, 0x5F, 0x82, 0x07, 0xA2, 0xB0,
  30315. 0xAE, 0x84, 0x9B, 0x60, 0x1A, 0xB9, 0x1B, 0x15,
  30316. 0x9B, 0x3D, 0xF4, 0xA1, 0x54, 0xC2, 0x04, 0x1E,
  30317. 0x77, 0x60, 0x70, 0xAF, 0xC4, 0x29, 0x69, 0x32,
  30318. 0x23, 0x80, 0x91, 0x7C, 0x97, 0x51, 0x07, 0x99,
  30319. 0xF3, 0x14, 0x91, 0x31, 0x47, 0x7E, 0x16, 0x66,
  30320. 0x3D, 0x31, 0x74, 0xC7, 0xC1, 0xCA, 0xEA, 0x78,
  30321. 0x85, 0x35, 0xC6, 0xC0, 0x05, 0xA6, 0x4F, 0x28,
  30322. 0x68, 0x63, 0x1B, 0x31, 0xB6, 0x6E, 0x20, 0x5F,
  30323. 0xD3, 0x8C, 0x1D, 0x84, 0x54, 0x2D, 0x0F, 0x1B,
  30324. 0x57, 0x8F, 0x58, 0xC9, 0xBF, 0x5A, 0x0F, 0xAE,
  30325. 0xAB, 0x6A, 0xB6, 0x49, 0x48, 0x93, 0x05, 0x31,
  30326. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  30327. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  30328. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  30329. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  30330. };
  30331. const byte kyber1024_sk[] = {
  30332. 0x07, 0x63, 0x8F, 0xB6, 0x98, 0x68, 0xF3, 0xD3,
  30333. 0x20, 0xE5, 0x86, 0x2B, 0xD9, 0x69, 0x33, 0xFE,
  30334. 0xB3, 0x11, 0xB3, 0x62, 0x09, 0x3C, 0x9B, 0x5D,
  30335. 0x50, 0x17, 0x0B, 0xCE, 0xD4, 0x3F, 0x1B, 0x53,
  30336. 0x6D, 0x9A, 0x20, 0x4B, 0xB1, 0xF2, 0x26, 0x95,
  30337. 0x95, 0x0B, 0xA1, 0xF2, 0xA9, 0xE8, 0xEB, 0x82,
  30338. 0x8B, 0x28, 0x44, 0x88, 0x76, 0x0B, 0x3F, 0xC8,
  30339. 0x4F, 0xAB, 0xA0, 0x42, 0x75, 0xD5, 0x62, 0x8E,
  30340. 0x39, 0xC5, 0xB2, 0x47, 0x13, 0x74, 0x28, 0x3C,
  30341. 0x50, 0x32, 0x99, 0xC0, 0xAB, 0x49, 0xB6, 0x6B,
  30342. 0x8B, 0xBB, 0x56, 0xA4, 0x18, 0x66, 0x24, 0xF9,
  30343. 0x19, 0xA2, 0xBA, 0x59, 0xBB, 0x08, 0xD8, 0x55,
  30344. 0x18, 0x80, 0xC2, 0xBE, 0xFC, 0x4F, 0x87, 0xF2,
  30345. 0x5F, 0x59, 0xAB, 0x58, 0x7A, 0x79, 0xC3, 0x27,
  30346. 0xD7, 0x92, 0xD5, 0x4C, 0x97, 0x4A, 0x69, 0x26,
  30347. 0x2F, 0xF8, 0xA7, 0x89, 0x38, 0x28, 0x9E, 0x9A,
  30348. 0x87, 0xB6, 0x88, 0xB0, 0x83, 0xE0, 0x59, 0x5F,
  30349. 0xE2, 0x18, 0xB6, 0xBB, 0x15, 0x05, 0x94, 0x1C,
  30350. 0xE2, 0xE8, 0x1A, 0x5A, 0x64, 0xC5, 0xAA, 0xC6,
  30351. 0x04, 0x17, 0x25, 0x69, 0x85, 0x34, 0x9E, 0xE4,
  30352. 0x7A, 0x52, 0x42, 0x0A, 0x5F, 0x97, 0x47, 0x7B,
  30353. 0x72, 0x36, 0xAC, 0x76, 0xBC, 0x70, 0xE8, 0x28,
  30354. 0x87, 0x29, 0x28, 0x7E, 0xE3, 0xE3, 0x4A, 0x3D,
  30355. 0xBC, 0x36, 0x83, 0xC0, 0xB7, 0xB1, 0x00, 0x29,
  30356. 0xFC, 0x20, 0x34, 0x18, 0x53, 0x7E, 0x74, 0x66,
  30357. 0xBA, 0x63, 0x85, 0xA8, 0xFF, 0x30, 0x1E, 0xE1,
  30358. 0x27, 0x08, 0xF8, 0x2A, 0xAA, 0x1E, 0x38, 0x0F,
  30359. 0xC7, 0xA8, 0x8F, 0x8F, 0x20, 0x5A, 0xB7, 0xE8,
  30360. 0x8D, 0x7E, 0x95, 0x95, 0x2A, 0x55, 0xBA, 0x20,
  30361. 0xD0, 0x9B, 0x79, 0xA4, 0x71, 0x41, 0xD6, 0x2B,
  30362. 0xF6, 0xEB, 0x7D, 0xD3, 0x07, 0xB0, 0x8E, 0xCA,
  30363. 0x13, 0xA5, 0xBC, 0x5F, 0x6B, 0x68, 0x58, 0x1C,
  30364. 0x68, 0x65, 0xB2, 0x7B, 0xBC, 0xDD, 0xAB, 0x14,
  30365. 0x2F, 0x4B, 0x2C, 0xBF, 0xF4, 0x88, 0xC8, 0xA2,
  30366. 0x27, 0x05, 0xFA, 0xA9, 0x8A, 0x2B, 0x9E, 0xEA,
  30367. 0x35, 0x30, 0xC7, 0x66, 0x62, 0x33, 0x5C, 0xC7,
  30368. 0xEA, 0x3A, 0x00, 0x77, 0x77, 0x25, 0xEB, 0xCC,
  30369. 0xCD, 0x2A, 0x46, 0x36, 0xB2, 0xD9, 0x12, 0x2F,
  30370. 0xF3, 0xAB, 0x77, 0x12, 0x3C, 0xE0, 0x88, 0x3C,
  30371. 0x19, 0x11, 0x11, 0x5E, 0x50, 0xC9, 0xE8, 0xA9,
  30372. 0x41, 0x94, 0xE4, 0x8D, 0xD0, 0xD0, 0x9C, 0xFF,
  30373. 0xB3, 0xAD, 0xCD, 0x2C, 0x1E, 0x92, 0x43, 0x09,
  30374. 0x03, 0xD0, 0x7A, 0xDB, 0xF0, 0x05, 0x32, 0x03,
  30375. 0x15, 0x75, 0xAA, 0x7F, 0x9E, 0x7B, 0x5A, 0x1F,
  30376. 0x33, 0x62, 0xDE, 0xC9, 0x36, 0xD4, 0x04, 0x3C,
  30377. 0x05, 0xF2, 0x47, 0x6C, 0x07, 0x57, 0x8B, 0xC9,
  30378. 0xCB, 0xAF, 0x2A, 0xB4, 0xE3, 0x82, 0x72, 0x7A,
  30379. 0xD4, 0x16, 0x86, 0xA9, 0x6B, 0x25, 0x48, 0x82,
  30380. 0x0B, 0xB0, 0x3B, 0x32, 0xF1, 0x1B, 0x28, 0x11,
  30381. 0xAD, 0x62, 0xF4, 0x89, 0xE9, 0x51, 0x63, 0x2A,
  30382. 0xBA, 0x0D, 0x1D, 0xF8, 0x96, 0x80, 0xCC, 0x8A,
  30383. 0x8B, 0x53, 0xB4, 0x81, 0xD9, 0x2A, 0x68, 0xD7,
  30384. 0x0B, 0x4E, 0xA1, 0xC3, 0xA6, 0xA5, 0x61, 0xC0,
  30385. 0x69, 0x28, 0x82, 0xB5, 0xCA, 0x8C, 0xC9, 0x42,
  30386. 0xA8, 0xD4, 0x95, 0xAF, 0xCB, 0x06, 0xDE, 0x89,
  30387. 0x49, 0x8F, 0xB9, 0x35, 0xB7, 0x75, 0x90, 0x8F,
  30388. 0xE7, 0xA0, 0x3E, 0x32, 0x4D, 0x54, 0xCC, 0x19,
  30389. 0xD4, 0xE1, 0xAA, 0xBD, 0x35, 0x93, 0xB3, 0x8B,
  30390. 0x19, 0xEE, 0x13, 0x88, 0xFE, 0x49, 0x2B, 0x43,
  30391. 0x12, 0x7E, 0x5A, 0x50, 0x42, 0x53, 0x78, 0x6A,
  30392. 0x0D, 0x69, 0xAD, 0x32, 0x60, 0x1C, 0x28, 0xE2,
  30393. 0xC8, 0x85, 0x04, 0xA5, 0xBA, 0x59, 0x97, 0x06,
  30394. 0x02, 0x3A, 0x61, 0x36, 0x3E, 0x17, 0xC6, 0xB9,
  30395. 0xBB, 0x59, 0xBD, 0xC6, 0x97, 0x45, 0x2C, 0xD0,
  30396. 0x59, 0x45, 0x19, 0x83, 0xD7, 0x38, 0xCA, 0x3F,
  30397. 0xD0, 0x34, 0xE3, 0xF5, 0x98, 0x88, 0x54, 0xCA,
  30398. 0x05, 0x03, 0x1D, 0xB0, 0x96, 0x11, 0x49, 0x89,
  30399. 0x88, 0x19, 0x7C, 0x6B, 0x30, 0xD2, 0x58, 0xDF,
  30400. 0xE2, 0x62, 0x65, 0x54, 0x1C, 0x89, 0xA4, 0xB3,
  30401. 0x1D, 0x68, 0x64, 0xE9, 0x38, 0x9B, 0x03, 0xCB,
  30402. 0x74, 0xF7, 0xEC, 0x43, 0x23, 0xFB, 0x94, 0x21,
  30403. 0xA4, 0xB9, 0x79, 0x0A, 0x26, 0xD1, 0x7B, 0x03,
  30404. 0x98, 0xA2, 0x67, 0x67, 0x35, 0x09, 0x09, 0xF8,
  30405. 0x4D, 0x57, 0xB6, 0x69, 0x4D, 0xF8, 0x30, 0x66,
  30406. 0x4C, 0xA8, 0xB3, 0xC3, 0xC0, 0x3E, 0xD2, 0xAE,
  30407. 0x67, 0xB8, 0x90, 0x06, 0x86, 0x8A, 0x68, 0x52,
  30408. 0x7C, 0xCD, 0x66, 0x64, 0x59, 0xAB, 0x7F, 0x05,
  30409. 0x66, 0x71, 0x00, 0x0C, 0x61, 0x64, 0xD3, 0xA7,
  30410. 0xF2, 0x66, 0xA1, 0x4D, 0x97, 0xCB, 0xD7, 0x00,
  30411. 0x4D, 0x6C, 0x92, 0xCA, 0xCA, 0x77, 0x0B, 0x84,
  30412. 0x4A, 0x4F, 0xA9, 0xB1, 0x82, 0xE7, 0xB1, 0x8C,
  30413. 0xA8, 0x85, 0x08, 0x2A, 0xC5, 0x64, 0x6F, 0xCB,
  30414. 0x4A, 0x14, 0xE1, 0x68, 0x5F, 0xEB, 0x0C, 0x9C,
  30415. 0xE3, 0x37, 0x2A, 0xB9, 0x53, 0x65, 0xC0, 0x4F,
  30416. 0xD8, 0x30, 0x84, 0xF8, 0x0A, 0x23, 0xFF, 0x10,
  30417. 0xA0, 0x5B, 0xF1, 0x5F, 0x7F, 0xA5, 0xAC, 0xC6,
  30418. 0xC0, 0xCB, 0x46, 0x2C, 0x33, 0xCA, 0x52, 0x4F,
  30419. 0xA6, 0xB8, 0xBB, 0x35, 0x90, 0x43, 0xBA, 0x68,
  30420. 0x60, 0x9E, 0xAA, 0x25, 0x36, 0xE8, 0x1D, 0x08,
  30421. 0x46, 0x3B, 0x19, 0x65, 0x3B, 0x54, 0x35, 0xBA,
  30422. 0x94, 0x6C, 0x9A, 0xDD, 0xEB, 0x20, 0x2B, 0x04,
  30423. 0xB0, 0x31, 0xCC, 0x96, 0x0D, 0xCC, 0x12, 0xE4,
  30424. 0x51, 0x8D, 0x42, 0x8B, 0x32, 0xB2, 0x57, 0xA4,
  30425. 0xFC, 0x73, 0x13, 0xD3, 0xA7, 0x98, 0x0D, 0x80,
  30426. 0x08, 0x2E, 0x93, 0x4F, 0x9D, 0x95, 0xC3, 0x2B,
  30427. 0x0A, 0x01, 0x91, 0xA2, 0x36, 0x04, 0x38, 0x4D,
  30428. 0xD9, 0xE0, 0x79, 0xBB, 0xBA, 0xA2, 0x66, 0xD1,
  30429. 0x4C, 0x3F, 0x75, 0x6B, 0x9F, 0x21, 0x33, 0x10,
  30430. 0x74, 0x33, 0xA4, 0xE8, 0x3F, 0xA7, 0x18, 0x72,
  30431. 0x82, 0xA8, 0x09, 0x20, 0x3A, 0x4F, 0xAF, 0x84,
  30432. 0x18, 0x51, 0x83, 0x3D, 0x12, 0x1A, 0xC3, 0x83,
  30433. 0x84, 0x3A, 0x5E, 0x55, 0xBC, 0x23, 0x81, 0x42,
  30434. 0x5E, 0x16, 0xC7, 0xDB, 0x4C, 0xC9, 0xAB, 0x5C,
  30435. 0x1B, 0x0D, 0x91, 0xA4, 0x7E, 0x2B, 0x8D, 0xE0,
  30436. 0xE5, 0x82, 0xC8, 0x6B, 0x6B, 0x0D, 0x90, 0x7B,
  30437. 0xB3, 0x60, 0xB9, 0x7F, 0x40, 0xAB, 0x5D, 0x03,
  30438. 0x8F, 0x6B, 0x75, 0xC8, 0x14, 0xB2, 0x7D, 0x9B,
  30439. 0x96, 0x8D, 0x41, 0x98, 0x32, 0xBC, 0x8C, 0x2B,
  30440. 0xEE, 0x60, 0x5E, 0xF6, 0xE5, 0x05, 0x9D, 0x33,
  30441. 0x10, 0x0D, 0x90, 0x48, 0x5D, 0x37, 0x84, 0x50,
  30442. 0x01, 0x42, 0x21, 0x73, 0x6C, 0x07, 0x40, 0x7C,
  30443. 0xAC, 0x26, 0x04, 0x08, 0xAA, 0x64, 0x92, 0x66,
  30444. 0x19, 0x78, 0x8B, 0x86, 0x01, 0xC2, 0xA7, 0x52,
  30445. 0xD1, 0xA6, 0xCB, 0xF8, 0x20, 0xD7, 0xC7, 0xA0,
  30446. 0x47, 0x16, 0x20, 0x32, 0x25, 0xB3, 0x89, 0x5B,
  30447. 0x93, 0x42, 0xD1, 0x47, 0xA8, 0x18, 0x5C, 0xFC,
  30448. 0x1B, 0xB6, 0x5B, 0xA0, 0x6B, 0x41, 0x42, 0x33,
  30449. 0x99, 0x03, 0xC0, 0xAC, 0x46, 0x51, 0x38, 0x5B,
  30450. 0x45, 0xD9, 0x8A, 0x8B, 0x19, 0xD2, 0x8C, 0xD6,
  30451. 0xBA, 0xB0, 0x88, 0x78, 0x7F, 0x7E, 0xE1, 0xB1,
  30452. 0x24, 0x61, 0x76, 0x6B, 0x43, 0xCB, 0xCC, 0xB9,
  30453. 0x64, 0x34, 0x42, 0x7D, 0x93, 0xC0, 0x65, 0x55,
  30454. 0x06, 0x88, 0xF6, 0x94, 0x8E, 0xD1, 0xB5, 0x47,
  30455. 0x5A, 0x42, 0x5F, 0x1B, 0x85, 0x20, 0x9D, 0x06,
  30456. 0x1C, 0x08, 0xB5, 0x6C, 0x1C, 0xC0, 0x69, 0xF6,
  30457. 0xC0, 0xA7, 0xC6, 0xF2, 0x93, 0x58, 0xCA, 0xB9,
  30458. 0x11, 0x08, 0x77, 0x32, 0xA6, 0x49, 0xD2, 0x7C,
  30459. 0x9B, 0x98, 0xF9, 0xA4, 0x88, 0x79, 0x38, 0x7D,
  30460. 0x9B, 0x00, 0xC2, 0x59, 0x59, 0xA7, 0x16, 0x54,
  30461. 0xD6, 0xF6, 0xA9, 0x46, 0x16, 0x45, 0x13, 0xE4,
  30462. 0x7A, 0x75, 0xD0, 0x05, 0x98, 0x6C, 0x23, 0x63,
  30463. 0xC0, 0x9F, 0x6B, 0x53, 0x7E, 0xCA, 0x78, 0xB9,
  30464. 0x30, 0x3A, 0x5F, 0xA4, 0x57, 0x60, 0x8A, 0x58,
  30465. 0x6A, 0x65, 0x3A, 0x34, 0x7D, 0xB0, 0x4D, 0xFC,
  30466. 0xC1, 0x91, 0x75, 0xB3, 0xA3, 0x01, 0x17, 0x25,
  30467. 0x36, 0x06, 0x2A, 0x65, 0x8A, 0x95, 0x27, 0x75,
  30468. 0x70, 0xC8, 0x85, 0x2C, 0xA8, 0x97, 0x3F, 0x4A,
  30469. 0xE1, 0x23, 0xA3, 0x34, 0x04, 0x7D, 0xD7, 0x11,
  30470. 0xC8, 0x92, 0x7A, 0x63, 0x4A, 0x03, 0x38, 0x8A,
  30471. 0x52, 0x7B, 0x03, 0x4B, 0xF7, 0xA8, 0x17, 0x0F,
  30472. 0xA7, 0x02, 0xC1, 0xF7, 0xC2, 0x3E, 0xC3, 0x2D,
  30473. 0x18, 0xA2, 0x37, 0x48, 0x90, 0xBE, 0x9C, 0x78,
  30474. 0x7A, 0x94, 0x09, 0xC8, 0x2D, 0x19, 0x2C, 0x4B,
  30475. 0xB7, 0x05, 0xA2, 0xF9, 0x96, 0xCE, 0x40, 0x5D,
  30476. 0x85, 0xA4, 0xC1, 0xA1, 0xAB, 0x9B, 0x6A, 0xEB,
  30477. 0x49, 0xCC, 0xE1, 0xC2, 0xF8, 0xA9, 0x7C, 0x35,
  30478. 0x16, 0xC7, 0x2A, 0x00, 0xA4, 0x62, 0x63, 0xBA,
  30479. 0xA6, 0x96, 0xBF, 0x25, 0x72, 0x77, 0x19, 0xC3,
  30480. 0x21, 0x64, 0x23, 0x61, 0x8F, 0xF3, 0x33, 0x80,
  30481. 0x93, 0x4A, 0x6C, 0x10, 0x54, 0x5C, 0x4C, 0x5C,
  30482. 0x51, 0x55, 0xB1, 0x24, 0x86, 0x18, 0x1F, 0xC7,
  30483. 0xA2, 0x31, 0x98, 0x73, 0x97, 0x8B, 0x6A, 0x2A,
  30484. 0x67, 0x49, 0x0F, 0x82, 0x56, 0xBD, 0x21, 0x96,
  30485. 0xFE, 0x17, 0x92, 0xA4, 0xC0, 0x00, 0x77, 0xB8,
  30486. 0x12, 0xEA, 0xE8, 0xBE, 0xD3, 0x57, 0x24, 0x99,
  30487. 0x68, 0x4A, 0xB3, 0x37, 0x18, 0x76, 0x76, 0x1E,
  30488. 0x45, 0x0C, 0x9F, 0x9D, 0x27, 0x68, 0xA3, 0x68,
  30489. 0x06, 0xD7, 0xAB, 0x20, 0x46, 0xC9, 0x1F, 0x17,
  30490. 0x59, 0x9E, 0x9A, 0xC5, 0x92, 0x99, 0x08, 0x08,
  30491. 0xDC, 0xD7, 0xB4, 0xD0, 0x91, 0x90, 0x72, 0xF1,
  30492. 0x4E, 0xC3, 0x61, 0x77, 0x3B, 0x72, 0x52, 0x44,
  30493. 0x4C, 0x32, 0x3C, 0x30, 0x83, 0x26, 0xF4, 0xA3,
  30494. 0x0F, 0x86, 0x80, 0xD2, 0xF7, 0x48, 0xF5, 0x6A,
  30495. 0x13, 0x2B, 0x82, 0x67, 0x4E, 0xD0, 0x18, 0x46,
  30496. 0x20, 0xB8, 0x2A, 0xD2, 0xCB, 0x18, 0x2C, 0x97,
  30497. 0xB4, 0x81, 0x62, 0x66, 0x47, 0x49, 0x12, 0x90,
  30498. 0xA0, 0x11, 0xCC, 0x73, 0x82, 0x86, 0x85, 0xA8,
  30499. 0xC3, 0x67, 0xA5, 0xB9, 0xCF, 0x8D, 0x62, 0x1B,
  30500. 0x0D, 0x5C, 0x1E, 0xFF, 0x03, 0x17, 0x27, 0x58,
  30501. 0xBD, 0x00, 0x49, 0x78, 0xC2, 0x51, 0xCD, 0x51,
  30502. 0x34, 0x22, 0x28, 0x98, 0x9C, 0xAE, 0x63, 0x32,
  30503. 0xAC, 0x48, 0x64, 0x37, 0xCB, 0x5C, 0x57, 0xD4,
  30504. 0x30, 0x74, 0x62, 0x86, 0x52, 0x53, 0xBE, 0x21,
  30505. 0x7B, 0x35, 0x15, 0xC7, 0x3D, 0xF4, 0x05, 0xB7,
  30506. 0xF2, 0x82, 0x17, 0xAD, 0x0B, 0x8C, 0xF6, 0x0C,
  30507. 0x2F, 0xFF, 0xAA, 0x0A, 0x00, 0x48, 0xB1, 0xFB,
  30508. 0x4A, 0xCD, 0xCD, 0xC3, 0x8B, 0x52, 0x50, 0xCF,
  30509. 0xEC, 0x35, 0x6A, 0x6D, 0xE2, 0x6C, 0xFA, 0x7A,
  30510. 0x58, 0x8F, 0xDC, 0x86, 0xF9, 0x8C, 0x85, 0x4A,
  30511. 0xC6, 0x4C, 0x7B, 0xFA, 0xA9, 0x6F, 0x5A, 0x32,
  30512. 0xCC, 0x06, 0x10, 0x93, 0x4B, 0xAA, 0x6A, 0x58,
  30513. 0x6B, 0x9A, 0x20, 0x54, 0xF1, 0x3B, 0xA2, 0x74,
  30514. 0x17, 0x4A, 0xA0, 0xD2, 0xB3, 0xA8, 0x1B, 0x96,
  30515. 0xA9, 0x40, 0x66, 0x6F, 0x78, 0x9B, 0x5A, 0x6B,
  30516. 0xCD, 0xC0, 0xA6, 0xA0, 0x17, 0x8A, 0x0C, 0x9A,
  30517. 0x02, 0x57, 0x8A, 0x49, 0x3F, 0x6E, 0xEA, 0x0D,
  30518. 0x2E, 0x6C, 0x13, 0x95, 0x1C, 0x9F, 0x24, 0x9A,
  30519. 0x5E, 0x8D, 0xD7, 0x1D, 0xD4, 0x9A, 0x74, 0x2D,
  30520. 0x45, 0x1F, 0x1A, 0xBB, 0xA1, 0x9A, 0xF8, 0xC5,
  30521. 0x47, 0x85, 0x5E, 0x0A, 0xFC, 0x72, 0x8E, 0x90,
  30522. 0xAB, 0xB4, 0x99, 0xC9, 0xBE, 0xEB, 0x76, 0x6F,
  30523. 0x47, 0x29, 0xCD, 0xA2, 0x22, 0x63, 0xE3, 0x24,
  30524. 0xD2, 0x23, 0x02, 0xCB, 0xD3, 0x39, 0x9F, 0xAC,
  30525. 0xC6, 0x30, 0x99, 0x1F, 0xC8, 0xF2, 0x8B, 0xDB,
  30526. 0x43, 0x54, 0x76, 0x25, 0x41, 0x52, 0x76, 0x78,
  30527. 0xBC, 0xF6, 0x1F, 0x65, 0xC2, 0x41, 0x14, 0x6C,
  30528. 0x42, 0x6D, 0x23, 0xB9, 0xBF, 0xAA, 0x6B, 0x7D,
  30529. 0xF1, 0x8C, 0x97, 0xF2, 0x0C, 0x1B, 0x61, 0x25,
  30530. 0xBF, 0x87, 0x4B, 0x1D, 0x89, 0x47, 0x58, 0x52,
  30531. 0xC4, 0x48, 0x21, 0x5D, 0xB0, 0xEB, 0x77, 0x37,
  30532. 0xF9, 0x14, 0x80, 0xE8, 0xCE, 0xBD, 0x9A, 0x08,
  30533. 0x71, 0x57, 0x4F, 0x5A, 0xB6, 0x2D, 0x90, 0x20,
  30534. 0x17, 0x5E, 0xC6, 0x92, 0x7C, 0xA0, 0xB5, 0x4C,
  30535. 0x09, 0x81, 0x8E, 0x42, 0xCF, 0x92, 0xA3, 0x83,
  30536. 0x17, 0x24, 0x22, 0xC7, 0xDC, 0x18, 0x31, 0xD6,
  30537. 0x3B, 0x0C, 0x29, 0x5D, 0xE7, 0x51, 0x59, 0xDB,
  30538. 0x80, 0x34, 0xE9, 0xE0, 0x7F, 0x7B, 0x0B, 0x91,
  30539. 0x0C, 0x3C, 0x1E, 0x5F, 0xB6, 0x6B, 0x3D, 0xC5,
  30540. 0x23, 0xF1, 0xFA, 0x6E, 0xB4, 0x91, 0x0C, 0xB8,
  30541. 0x9A, 0x6C, 0x17, 0x56, 0x2C, 0x83, 0xAB, 0x4C,
  30542. 0x18, 0xD0, 0xCD, 0x7E, 0x07, 0x96, 0x59, 0x2A,
  30543. 0x37, 0x2A, 0xA4, 0x09, 0xB1, 0xC5, 0x57, 0x34,
  30544. 0x7C, 0xCA, 0xCD, 0xC4, 0x64, 0x4A, 0x11, 0x90,
  30545. 0x64, 0xD0, 0x6D, 0xD4, 0x74, 0x92, 0x9D, 0x1C,
  30546. 0x6F, 0xB4, 0xD6, 0x86, 0xE5, 0x49, 0x1C, 0xE4,
  30547. 0xBC, 0x89, 0xA3, 0x0B, 0xB4, 0xB8, 0xC4, 0x1B,
  30548. 0xCE, 0x51, 0x57, 0xDF, 0xC1, 0x36, 0x08, 0x23,
  30549. 0xB1, 0xAB, 0x61, 0x8C, 0x14, 0xB1, 0x0F, 0x98,
  30550. 0xC2, 0x50, 0x67, 0x39, 0x8E, 0xA7, 0x01, 0x8C,
  30551. 0x27, 0x8A, 0x4B, 0x3D, 0xF3, 0x13, 0x34, 0xD6,
  30552. 0x03, 0xB2, 0x04, 0x4E, 0xF1, 0x87, 0xCD, 0x9B,
  30553. 0xC6, 0xCE, 0x42, 0x72, 0x5B, 0xD9, 0x62, 0xC2,
  30554. 0x64, 0x98, 0x3E, 0x9E, 0x18, 0x15, 0x5A, 0x8B,
  30555. 0x9C, 0x47, 0x14, 0x3D, 0x70, 0x46, 0x0A, 0x26,
  30556. 0xA5, 0x6F, 0xE7, 0x65, 0x8C, 0x1F, 0x15, 0x03,
  30557. 0x48, 0xC6, 0x08, 0x7E, 0xF7, 0x58, 0xAD, 0x16,
  30558. 0x78, 0x87, 0x86, 0x0A, 0x00, 0x7A, 0x5F, 0xC3,
  30559. 0x73, 0x58, 0xD4, 0x3B, 0x5E, 0xBE, 0xE8, 0x20,
  30560. 0xAC, 0xEA, 0x47, 0x4F, 0x0A, 0xC0, 0x7B, 0x76,
  30561. 0x80, 0x28, 0x66, 0x19, 0x9C, 0x61, 0x23, 0x1D,
  30562. 0x5C, 0x74, 0x7C, 0x93, 0x77, 0x4D, 0x2C, 0x1E,
  30563. 0x0C, 0x1C, 0x67, 0xE6, 0xC8, 0x1B, 0x82, 0x75,
  30564. 0x21, 0x73, 0xE1, 0x25, 0xBA, 0xF3, 0x9B, 0x4F,
  30565. 0xD1, 0x9A, 0x4F, 0x45, 0x3D, 0xC5, 0x79, 0x76,
  30566. 0xB1, 0xD9, 0x7F, 0xE6, 0x99, 0x69, 0x92, 0xBB,
  30567. 0xB6, 0x5B, 0x7C, 0xB2, 0x5D, 0x07, 0x7B, 0xBA,
  30568. 0xA6, 0xA1, 0x33, 0x22, 0x89, 0x9A, 0xF6, 0x59,
  30569. 0xCF, 0x1B, 0x35, 0x58, 0xC1, 0xB5, 0x00, 0x11,
  30570. 0x54, 0xB6, 0x25, 0x80, 0x9E, 0xD8, 0x9A, 0xEE,
  30571. 0xBB, 0x89, 0xE6, 0xEA, 0x7D, 0x67, 0xF7, 0x23,
  30572. 0xD0, 0x45, 0xAB, 0x05, 0x71, 0x5C, 0x42, 0x35,
  30573. 0x5D, 0xA6, 0xA5, 0xC8, 0xDD, 0x39, 0xC8, 0xAB,
  30574. 0xE3, 0x03, 0x77, 0x51, 0xA0, 0x1E, 0xD1, 0xC7,
  30575. 0x37, 0x49, 0x19, 0xF3, 0x12, 0x1B, 0x5A, 0x52,
  30576. 0xC5, 0x3D, 0x14, 0x87, 0x31, 0x67, 0x69, 0xF8,
  30577. 0x07, 0x21, 0xDE, 0xEA, 0xAA, 0xD3, 0xC9, 0x0F,
  30578. 0x76, 0xE7, 0xAE, 0x9E, 0x12, 0xBA, 0x92, 0xB3,
  30579. 0x2B, 0x5F, 0xD4, 0x57, 0xE3, 0xC7, 0x52, 0xC2,
  30580. 0x65, 0x0D, 0xFB, 0x88, 0x57, 0x71, 0xCB, 0x77,
  30581. 0xAC, 0x3C, 0x78, 0x5A, 0x8C, 0x56, 0x2E, 0x6A,
  30582. 0x1C, 0x63, 0xC2, 0xA5, 0x5E, 0xA4, 0x7C, 0xF8,
  30583. 0xB9, 0x0E, 0xB8, 0x22, 0x5C, 0x12, 0x3C, 0x34,
  30584. 0x64, 0x52, 0x56, 0x62, 0x35, 0xB2, 0xF3, 0x18,
  30585. 0x23, 0xA3, 0x35, 0x21, 0xE0, 0x87, 0x93, 0x7A,
  30586. 0x34, 0x5D, 0x8D, 0x66, 0x3E, 0xEA, 0xA0, 0x56,
  30587. 0x58, 0x91, 0x7B, 0xBA, 0xA0, 0x08, 0xC2, 0xE3,
  30588. 0x35, 0xF8, 0x85, 0x0A, 0x90, 0xA3, 0x26, 0xD0,
  30589. 0xE6, 0x64, 0x32, 0xF4, 0x4C, 0xEB, 0x82, 0x89,
  30590. 0xE4, 0xEC, 0xB2, 0xD1, 0x29, 0x58, 0xE9, 0x84,
  30591. 0x07, 0x2E, 0xCA, 0xCB, 0x88, 0xE1, 0x34, 0x8F,
  30592. 0xF0, 0xB5, 0x56, 0x54, 0xAC, 0xBA, 0x5B, 0x54,
  30593. 0x97, 0x1C, 0xBA, 0xEB, 0xA8, 0x8E, 0xC4, 0xB9,
  30594. 0x1A, 0x94, 0xC3, 0x71, 0x92, 0xFA, 0x98, 0x2B,
  30595. 0xEC, 0xB9, 0xF3, 0xDA, 0x42, 0x16, 0x03, 0xB6,
  30596. 0x1A, 0x51, 0xBC, 0x8E, 0x36, 0xCB, 0xD0, 0x53,
  30597. 0x85, 0x1C, 0x77, 0xB1, 0xB9, 0x26, 0xB1, 0x7A,
  30598. 0x27, 0x2A, 0xA9, 0x02, 0x32, 0x46, 0xB0, 0x2B,
  30599. 0x3E, 0xD4, 0x7F, 0x66, 0xA0, 0x0B, 0xD5, 0x68,
  30600. 0x48, 0x23, 0x63, 0x4E, 0x7C, 0xE5, 0x8C, 0xF8,
  30601. 0xF3, 0x06, 0xE3, 0x5B, 0x1E, 0x53, 0x22, 0x82,
  30602. 0x4D, 0x90, 0x48, 0x01, 0xF0, 0xA2, 0xFA, 0x7C,
  30603. 0x2B, 0xC9, 0xC2, 0x52, 0xB0, 0xA5, 0x6B, 0x7B,
  30604. 0xA2, 0xAB, 0x0F, 0x63, 0x60, 0x21, 0x74, 0x5A,
  30605. 0x70, 0xA9, 0xA4, 0x3E, 0x2B, 0x0A, 0x8D, 0x61,
  30606. 0x59, 0x70, 0xB6, 0x53, 0x09, 0x62, 0x4B, 0x51,
  30607. 0x84, 0xBC, 0xC3, 0x0B, 0x91, 0x16, 0x79, 0xAE,
  30608. 0xDD, 0x76, 0x02, 0x5F, 0xE3, 0x90, 0x8F, 0xD6,
  30609. 0x78, 0x97, 0xB0, 0xCF, 0x4B, 0xE5, 0xA6, 0xF5,
  30610. 0x41, 0x3D, 0x7D, 0xD9, 0x85, 0x64, 0xB2, 0x3E,
  30611. 0x42, 0xA9, 0x3E, 0x4A, 0xA8, 0x82, 0x1C, 0xD4,
  30612. 0x50, 0x54, 0xC6, 0x43, 0xED, 0xC1, 0x15, 0x8D,
  30613. 0xB6, 0xB3, 0xDE, 0xB1, 0x3F, 0xB5, 0xA5, 0x1E,
  30614. 0xBD, 0x1A, 0x8A, 0x78, 0xB8, 0x72, 0x25, 0xA7,
  30615. 0x33, 0x8E, 0x10, 0x11, 0x04, 0xC4, 0xA2, 0x20,
  30616. 0xD9, 0xBD, 0xED, 0xD4, 0x8C, 0x85, 0xA1, 0xC2,
  30617. 0xDA, 0xE7, 0x81, 0xA8, 0x0C, 0x40, 0xE1, 0x3B,
  30618. 0x87, 0xEA, 0xC7, 0x3A, 0x76, 0x42, 0x01, 0xC9,
  30619. 0xB7, 0x60, 0xCC, 0xFB, 0x1A, 0xE3, 0x92, 0x69,
  30620. 0x9C, 0x70, 0x39, 0xD2, 0x7C, 0x39, 0x36, 0x2B,
  30621. 0x27, 0xB8, 0xFC, 0x6F, 0x07, 0xA8, 0xA3, 0xD4,
  30622. 0x41, 0x0F, 0x15, 0x47, 0xC4, 0x8A, 0x99, 0x97,
  30623. 0xF6, 0x2C, 0x61, 0x07, 0x44, 0x52, 0xEF, 0x15,
  30624. 0x15, 0xF8, 0xA6, 0x49, 0xEB, 0xCA, 0x94, 0x37,
  30625. 0x20, 0x5A, 0x4E, 0x8A, 0x61, 0x60, 0x6B, 0x41,
  30626. 0xDA, 0xF6, 0x83, 0x4D, 0x67, 0x1F, 0x4D, 0x85,
  30627. 0x2C, 0x0C, 0x9C, 0x40, 0x96, 0x61, 0x16, 0x48,
  30628. 0xC6, 0xA3, 0x17, 0x06, 0x78, 0xB1, 0x53, 0x7C,
  30629. 0xC1, 0x82, 0x8D, 0x93, 0x58, 0x0C, 0x9E, 0x58,
  30630. 0x49, 0xA9, 0x65, 0x31, 0x75, 0xAC, 0xB7, 0x53,
  30631. 0xF2, 0xBE, 0x74, 0x37, 0xBE, 0x45, 0xF6, 0xC6,
  30632. 0x03, 0xE4, 0x85, 0xF2, 0xEC, 0x30, 0x1B, 0xB4,
  30633. 0x2B, 0x6C, 0x37, 0xC2, 0x25, 0xD7, 0x49, 0x5A,
  30634. 0x58, 0x4A, 0xE2, 0x31, 0x89, 0x0A, 0xB5, 0xC8,
  30635. 0xC3, 0x5C, 0x26, 0x8C, 0xF4, 0xBB, 0xB0, 0x21,
  30636. 0x3C, 0x09, 0x60, 0x19, 0x31, 0x95, 0x61, 0xA8,
  30637. 0xA6, 0x94, 0x76, 0x37, 0xAA, 0x40, 0xD0, 0x06,
  30638. 0xB4, 0x15, 0xBB, 0x2C, 0xFA, 0x22, 0x37, 0xE0,
  30639. 0x89, 0x0B, 0x6A, 0x3B, 0xC1, 0x34, 0xAB, 0xF8,
  30640. 0xF6, 0x58, 0x5E, 0x10, 0x8D, 0x15, 0x94, 0x0F,
  30641. 0x91, 0xF4, 0xBF, 0x5B, 0x0C, 0x81, 0x80, 0x55,
  30642. 0xB2, 0x1D, 0xEA, 0x6E, 0x63, 0xB5, 0x53, 0x98,
  30643. 0x8C, 0x47, 0xF4, 0xB9, 0x4E, 0x7C, 0xF8, 0x00,
  30644. 0xA4, 0x93, 0xB4, 0x73, 0x47, 0x05, 0xED, 0xC5,
  30645. 0x6A, 0x4B, 0x60, 0x21, 0xC6, 0x29, 0x50, 0x06,
  30646. 0x75, 0x87, 0x68, 0x04, 0xCF, 0x0B, 0x95, 0x1F,
  30647. 0x03, 0x8A, 0x5C, 0x7F, 0xE5, 0x8E, 0x89, 0x77,
  30648. 0x4E, 0xF2, 0x99, 0x2F, 0xD7, 0xC6, 0x30, 0x99,
  30649. 0xD3, 0x52, 0xA7, 0xD2, 0x15, 0x60, 0xB7, 0x88,
  30650. 0xB4, 0x05, 0x70, 0x98, 0x61, 0x81, 0x7E, 0x59,
  30651. 0xA9, 0x6B, 0x3A, 0x3A, 0x83, 0xCB, 0xA8, 0x03,
  30652. 0xB1, 0x69, 0x34, 0x33, 0x10, 0x71, 0x90, 0x5B,
  30653. 0xBE, 0xC6, 0x53, 0x29, 0x00, 0x15, 0x5D, 0x8A,
  30654. 0xC8, 0x8C, 0xB3, 0x2E, 0x4E, 0x21, 0xA3, 0xBD,
  30655. 0x3A, 0x03, 0xFD, 0xEC, 0x32, 0x5A, 0x51, 0xCD,
  30656. 0x27, 0x73, 0x96, 0x4E, 0x67, 0x84, 0xFC, 0xF1,
  30657. 0x85, 0x37, 0x37, 0xAA, 0x64, 0xEB, 0x67, 0x56,
  30658. 0x47, 0x27, 0x27, 0x26, 0x61, 0xAB, 0xF8, 0x43,
  30659. 0x13, 0xA5, 0x7A, 0x44, 0xB1, 0x23, 0xC6, 0x55,
  30660. 0x09, 0xCF, 0xB7, 0xA6, 0xF6, 0x64, 0x1C, 0xDC,
  30661. 0xC3, 0xB5, 0x7F, 0xE6, 0x28, 0xC7, 0xB8, 0x19,
  30662. 0x2D, 0xB4, 0x4F, 0xFB, 0xF5, 0x79, 0x6A, 0x86,
  30663. 0x13, 0xB1, 0xFA, 0x12, 0x6F, 0x60, 0x76, 0x88,
  30664. 0x3C, 0x78, 0x3D, 0xC2, 0x4E, 0x2A, 0x44, 0x64,
  30665. 0xC4, 0x0B, 0x3A, 0x41, 0xCA, 0x70, 0xAE, 0x87,
  30666. 0x62, 0x08, 0x66, 0xCF, 0x4F, 0xCB, 0x2B, 0xD2,
  30667. 0x04, 0xBF, 0x5C, 0x28, 0x38, 0x12, 0xBA, 0x05,
  30668. 0x6A, 0xC0, 0xC3, 0x45, 0xE3, 0x79, 0xC4, 0xBA,
  30669. 0x24, 0xD7, 0x50, 0x90, 0x12, 0x79, 0xBB, 0x2F,
  30670. 0x3A, 0x16, 0xF6, 0x12, 0xBF, 0xAD, 0xB3, 0x57,
  30671. 0x03, 0x33, 0x2C, 0x7C, 0x13, 0x6F, 0x68, 0xEA,
  30672. 0xB6, 0x75, 0x5C, 0x66, 0xB6, 0xA4, 0xAD, 0x1A,
  30673. 0xAB, 0xA7, 0xB7, 0x68, 0xA5, 0x8A, 0xCA, 0xAC,
  30674. 0xC1, 0x0A, 0x45, 0x9A, 0x1C, 0xC8, 0xEF, 0x29,
  30675. 0x37, 0x7B, 0xC2, 0x00, 0xE4, 0xD3, 0x15, 0xA3,
  30676. 0x0A, 0x6B, 0xCC, 0x32, 0x56, 0xF9, 0x73, 0x4D,
  30677. 0x06, 0xE9, 0x77, 0x9C, 0xAA, 0x54, 0x42, 0xA9,
  30678. 0xA1, 0x60, 0x69, 0x08, 0x13, 0x77, 0xC7, 0x6E,
  30679. 0x75, 0x15, 0x43, 0x68, 0x07, 0x2D, 0xC4, 0x46,
  30680. 0xED, 0x6C, 0x8B, 0x8E, 0x62, 0x2A, 0x21, 0xE3,
  30681. 0x83, 0xCF, 0x9B, 0xA1, 0xFB, 0x43, 0x4E, 0x2E,
  30682. 0xCC, 0x81, 0xE7, 0xB7, 0x8C, 0xEE, 0x98, 0x6B,
  30683. 0x8F, 0xF7, 0x98, 0xAB, 0x18, 0xCF, 0x96, 0x34,
  30684. 0x54, 0x35, 0x46, 0x28, 0x4E, 0xDA, 0x2A, 0x26,
  30685. 0xB4, 0x7F, 0x05, 0xB7, 0x35, 0xBC, 0xDB, 0x12,
  30686. 0x02, 0x22, 0x00, 0x76, 0xDC, 0x8B, 0x4E, 0x4B,
  30687. 0x9F, 0x85, 0x35, 0x33, 0xC8, 0xF6, 0xC7, 0xFF,
  30688. 0x38, 0x81, 0x7B, 0xA4, 0x97, 0x12, 0x83, 0x57,
  30689. 0x85, 0xF1, 0x7F, 0x14, 0xCA, 0x01, 0xD0, 0xC1,
  30690. 0xC1, 0xE9, 0x88, 0x10, 0xFE, 0x0B, 0x36, 0xE5,
  30691. 0xB4, 0x27, 0x15, 0x7B, 0x94, 0x18, 0x44, 0x9C,
  30692. 0xED, 0xD6, 0x41, 0xA4, 0x29, 0x3C, 0x85, 0xC3,
  30693. 0x27, 0x00, 0x10, 0x2A, 0xCE, 0xC2, 0x2E, 0xBA,
  30694. 0xD9, 0x8E, 0xD1, 0x60, 0xA5, 0xF0, 0x27, 0xBD,
  30695. 0x4C, 0xDA, 0x57, 0xF1, 0xF3, 0x72, 0x0A, 0x12,
  30696. 0xC1, 0x34, 0x65, 0x4D, 0xD5, 0xE7, 0x3F, 0x82,
  30697. 0x96, 0x76, 0x49, 0x53, 0x90, 0xD0, 0xE7, 0x92,
  30698. 0x9D, 0x60, 0x34, 0xE9, 0xC5, 0x5F, 0x7D, 0x55,
  30699. 0xBA, 0x65, 0x8B, 0xC5, 0x87, 0x98, 0x8E, 0x8A,
  30700. 0xF9, 0x49, 0x60, 0xF6, 0xCF, 0xB8, 0xD5, 0xAF,
  30701. 0x7A, 0x00, 0x21, 0x53, 0x5A, 0x6E, 0x25, 0xE4,
  30702. 0x37, 0xD4, 0x9A, 0x78, 0x06, 0x98, 0xBE, 0x22,
  30703. 0xAC, 0x99, 0x53, 0x94, 0x9F, 0x57, 0x1B, 0x85,
  30704. 0xA6, 0x85, 0x72, 0x5F, 0x82, 0x07, 0xA2, 0xB0,
  30705. 0xAE, 0x84, 0x9B, 0x60, 0x1A, 0xB9, 0x1B, 0x15,
  30706. 0x9B, 0x3D, 0xF4, 0xA1, 0x54, 0xC2, 0x04, 0x1E,
  30707. 0x77, 0x60, 0x70, 0xAF, 0xC4, 0x29, 0x69, 0x32,
  30708. 0x23, 0x80, 0x91, 0x7C, 0x97, 0x51, 0x07, 0x99,
  30709. 0xF3, 0x14, 0x91, 0x31, 0x47, 0x7E, 0x16, 0x66,
  30710. 0x3D, 0x31, 0x74, 0xC7, 0xC1, 0xCA, 0xEA, 0x78,
  30711. 0x85, 0x35, 0xC6, 0xC0, 0x05, 0xA6, 0x4F, 0x28,
  30712. 0x68, 0x63, 0x1B, 0x31, 0xB6, 0x6E, 0x20, 0x5F,
  30713. 0xD3, 0x8C, 0x1D, 0x84, 0x54, 0x2D, 0x0F, 0x1B,
  30714. 0x57, 0x8F, 0x58, 0xC9, 0xBF, 0x5A, 0x0F, 0xAE,
  30715. 0xAB, 0x6A, 0xB6, 0x49, 0x48, 0x93, 0x05, 0x31,
  30716. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  30717. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  30718. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  30719. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  30720. 0x8A, 0x39, 0xE8, 0x7D, 0x53, 0x1F, 0x35, 0x27,
  30721. 0xC2, 0x07, 0xED, 0xCC, 0x1D, 0xB7, 0xFA, 0xDD,
  30722. 0xCF, 0x96, 0x28, 0x39, 0x18, 0x79, 0xB3, 0x35,
  30723. 0xC7, 0x07, 0x83, 0x9A, 0x0D, 0xB0, 0x51, 0xA8,
  30724. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  30725. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  30726. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  30727. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  30728. };
  30729. const byte kyber1024_ct[] = {
  30730. 0xA6, 0xAF, 0x29, 0xD5, 0xF5, 0xB8, 0x0B, 0xD1,
  30731. 0x30, 0xF5, 0x18, 0xBA, 0xDD, 0xD6, 0xC8, 0xF1,
  30732. 0x75, 0x45, 0x41, 0x3D, 0x86, 0x0F, 0xB3, 0xDE,
  30733. 0x45, 0x19, 0x79, 0xEB, 0xFA, 0x5E, 0x4E, 0x31,
  30734. 0x12, 0xC7, 0xC0, 0xAD, 0xF9, 0x98, 0x24, 0xBB,
  30735. 0x52, 0x6F, 0x2C, 0x35, 0x50, 0x74, 0x8E, 0xD0,
  30736. 0xE1, 0x34, 0xF0, 0x45, 0x7A, 0x7C, 0x61, 0xF9,
  30737. 0xF5, 0x26, 0xF0, 0x02, 0xBA, 0xAD, 0xC0, 0x3F,
  30738. 0xC1, 0x3E, 0x38, 0x13, 0x12, 0x19, 0x51, 0x3C,
  30739. 0x3E, 0xDE, 0x06, 0x16, 0x61, 0xE7, 0x4F, 0x60,
  30740. 0x3C, 0x4F, 0xCF, 0x79, 0x51, 0xC8, 0xE5, 0x2C,
  30741. 0x9C, 0x21, 0x3B, 0x0D, 0x22, 0xD9, 0x29, 0x36,
  30742. 0x63, 0xD6, 0x69, 0xA6, 0xB5, 0x8E, 0xD8, 0xFC,
  30743. 0xEF, 0xCF, 0x82, 0x49, 0xD7, 0xBB, 0x52, 0x98,
  30744. 0xF5, 0x57, 0x61, 0x44, 0x5B, 0x2B, 0x83, 0xCE,
  30745. 0x7F, 0x00, 0x5C, 0xB0, 0x42, 0x48, 0xAE, 0xC8,
  30746. 0xBD, 0xA2, 0x2F, 0xD2, 0xD4, 0x2A, 0xA7, 0x66,
  30747. 0x32, 0x20, 0x14, 0xEA, 0x03, 0x8C, 0xC3, 0x2C,
  30748. 0x55, 0xC8, 0xE4, 0xB9, 0xE2, 0x8E, 0xC9, 0x11,
  30749. 0x9F, 0x52, 0x73, 0x41, 0xE4, 0xF6, 0x6A, 0x03,
  30750. 0x51, 0x21, 0x07, 0x3B, 0x85, 0xDE, 0x67, 0x06,
  30751. 0xDA, 0x19, 0xE0, 0x83, 0x8A, 0x9F, 0x33, 0xB7,
  30752. 0x19, 0xA6, 0x8F, 0x03, 0x9B, 0x66, 0x4D, 0xC0,
  30753. 0x02, 0x65, 0x9E, 0xAB, 0xFC, 0x39, 0x86, 0x79,
  30754. 0xAA, 0x70, 0x09, 0xCE, 0x0C, 0xD0, 0x1C, 0xDA,
  30755. 0xFB, 0x6C, 0xD2, 0xA2, 0x6F, 0xE4, 0x10, 0x16,
  30756. 0x72, 0xC9, 0x8F, 0xF5, 0x8F, 0x7C, 0x47, 0xD5,
  30757. 0xBD, 0xA2, 0x90, 0x66, 0x53, 0xB3, 0xA6, 0xF9,
  30758. 0x65, 0x1F, 0x7A, 0x12, 0x1E, 0xA7, 0x7E, 0xA7,
  30759. 0x47, 0x23, 0xFA, 0xE5, 0xB8, 0x73, 0xF9, 0xBB,
  30760. 0x7B, 0x66, 0x4F, 0x0C, 0x8A, 0x93, 0x83, 0x1E,
  30761. 0xF9, 0xD5, 0x1C, 0x7C, 0xC1, 0xEF, 0x44, 0xAC,
  30762. 0x0E, 0x55, 0xA5, 0x5C, 0xA7, 0x6D, 0x13, 0x7F,
  30763. 0xE9, 0xB7, 0x5F, 0x40, 0x50, 0x9C, 0xEF, 0x15,
  30764. 0x6E, 0x5A, 0xD1, 0x8F, 0x9F, 0xB9, 0x99, 0x68,
  30765. 0x00, 0x08, 0xE5, 0x47, 0xD5, 0x5E, 0xEC, 0xD5,
  30766. 0xB4, 0xD1, 0xCB, 0x1D, 0x9F, 0x07, 0x6C, 0xEC,
  30767. 0x21, 0x50, 0x1C, 0x74, 0x02, 0x50, 0x9E, 0xCB,
  30768. 0x77, 0xAF, 0xB2, 0xCB, 0x9A, 0x61, 0x34, 0x0A,
  30769. 0x8B, 0xD1, 0x51, 0x4C, 0x6E, 0x71, 0xB4, 0xAA,
  30770. 0x45, 0xE4, 0x7E, 0xC3, 0x75, 0x12, 0x27, 0x1B,
  30771. 0x91, 0x1F, 0x8F, 0xB4, 0x6C, 0x90, 0x82, 0xC9,
  30772. 0xDF, 0x07, 0x20, 0x4A, 0xBB, 0x5A, 0x50, 0xE6,
  30773. 0xE3, 0x64, 0x7A, 0x8A, 0xD4, 0xD8, 0xD5, 0xD7,
  30774. 0xBF, 0xF1, 0x9C, 0x8A, 0x50, 0x93, 0x08, 0xBC,
  30775. 0xFB, 0x89, 0x55, 0x36, 0xD0, 0x45, 0xCA, 0x2B,
  30776. 0x97, 0xCB, 0x16, 0xA2, 0x9B, 0xB7, 0x18, 0x1C,
  30777. 0xAD, 0x05, 0x09, 0xDD, 0xB9, 0x17, 0x35, 0x02,
  30778. 0x8E, 0xBA, 0x8C, 0x31, 0xD7, 0x4B, 0xD2, 0x75,
  30779. 0xEA, 0xA6, 0x5B, 0x53, 0x40, 0xB3, 0xA4, 0x3F,
  30780. 0xBF, 0xE0, 0xB3, 0x06, 0x1D, 0x6B, 0xAE, 0x7E,
  30781. 0x75, 0xB7, 0x09, 0x8C, 0xDA, 0xBE, 0x91, 0xD4,
  30782. 0xB3, 0x1E, 0x36, 0xC9, 0xAA, 0x7A, 0x82, 0x98,
  30783. 0x86, 0x2A, 0xD6, 0x3C, 0x8F, 0xD2, 0x82, 0xE0,
  30784. 0x3B, 0x46, 0x0B, 0x3A, 0xB4, 0x64, 0xCE, 0x0F,
  30785. 0x27, 0xB1, 0xC3, 0xD1, 0x11, 0x55, 0xAC, 0xAA,
  30786. 0x01, 0x1E, 0xB9, 0xE2, 0xAE, 0x3E, 0x6D, 0xDA,
  30787. 0x07, 0xD6, 0xF4, 0x91, 0x73, 0x7C, 0xBC, 0xE9,
  30788. 0xB0, 0x5F, 0x9B, 0xC5, 0x6B, 0xE2, 0x0E, 0x8D,
  30789. 0x32, 0x6B, 0xA1, 0x32, 0xC5, 0x7F, 0xB2, 0x35,
  30790. 0x16, 0x11, 0x44, 0x51, 0x9C, 0xDF, 0x40, 0x56,
  30791. 0x0F, 0xBE, 0x27, 0x9B, 0xDE, 0x41, 0x1E, 0x11,
  30792. 0x25, 0x31, 0xF8, 0x26, 0xD6, 0xAB, 0x10, 0xD4,
  30793. 0x54, 0x73, 0x50, 0xAD, 0xD2, 0xA9, 0xDE, 0x8D,
  30794. 0x62, 0xC2, 0xAC, 0x82, 0xCA, 0xBE, 0x68, 0x15,
  30795. 0x64, 0x6F, 0x4D, 0xC9, 0x74, 0x2B, 0xB0, 0xC2,
  30796. 0xA3, 0xF7, 0x7E, 0xC7, 0xB4, 0x6C, 0x6B, 0x53,
  30797. 0x76, 0x05, 0xFA, 0x31, 0x79, 0x8C, 0xD8, 0x92,
  30798. 0x81, 0x22, 0x1A, 0x33, 0xDF, 0xB9, 0x79, 0x6E,
  30799. 0x64, 0x43, 0x05, 0x63, 0x03, 0x32, 0xC2, 0xCB,
  30800. 0x93, 0x14, 0x08, 0xAB, 0x48, 0x1A, 0x16, 0xD9,
  30801. 0x53, 0xF6, 0xBE, 0xAE, 0x38, 0x91, 0xD6, 0xD9,
  30802. 0xAC, 0x1F, 0xAB, 0x38, 0x22, 0x2D, 0x92, 0x71,
  30803. 0x87, 0x2D, 0x9D, 0x0C, 0xAD, 0xB9, 0x1A, 0xBE,
  30804. 0x9B, 0x4E, 0x26, 0x5F, 0x75, 0xC6, 0xE5, 0xE8,
  30805. 0x29, 0xE1, 0x46, 0xC3, 0xD8, 0xCE, 0x1E, 0x9D,
  30806. 0x12, 0xE0, 0xD1, 0x29, 0x80, 0x19, 0x57, 0xF4,
  30807. 0x6B, 0x0D, 0x2D, 0xBE, 0x1F, 0x74, 0x9B, 0x1D,
  30808. 0x08, 0xE2, 0x34, 0x5F, 0x62, 0x39, 0xA7, 0x31,
  30809. 0x34, 0x2E, 0xB7, 0x5B, 0x0C, 0xF1, 0xBF, 0x41,
  30810. 0x17, 0x49, 0xBC, 0x2C, 0xAF, 0x28, 0x10, 0xB7,
  30811. 0x88, 0xC6, 0xB7, 0x23, 0x8B, 0x4D, 0x3D, 0xA2,
  30812. 0xD6, 0x31, 0x5C, 0xE9, 0x54, 0x2E, 0x24, 0x40,
  30813. 0x4F, 0x14, 0x57, 0x55, 0xA3, 0x0A, 0xB8, 0x51,
  30814. 0xE4, 0x44, 0x58, 0x41, 0xBD, 0x33, 0xF7, 0x16,
  30815. 0xA5, 0x86, 0x88, 0x48, 0x88, 0xEC, 0xC6, 0xBC,
  30816. 0x64, 0x98, 0xAA, 0x32, 0x91, 0x9A, 0xE8, 0x1D,
  30817. 0x20, 0xC2, 0x69, 0x73, 0xC2, 0xBD, 0x54, 0x58,
  30818. 0x2A, 0x0F, 0x6A, 0xD9, 0x8A, 0xBF, 0xD2, 0x62,
  30819. 0x7E, 0x15, 0x69, 0x0A, 0x72, 0x7E, 0x69, 0xF5,
  30820. 0x81, 0xDD, 0x2A, 0x71, 0x27, 0x98, 0x2A, 0x90,
  30821. 0xE3, 0x3E, 0x2D, 0x4A, 0x03, 0xFE, 0x33, 0x91,
  30822. 0x42, 0xC7, 0xE4, 0x4C, 0x32, 0x6A, 0xC4, 0x6E,
  30823. 0xD3, 0x95, 0xA2, 0x25, 0xD3, 0x03, 0x33, 0x89,
  30824. 0x91, 0x73, 0x28, 0xB4, 0x53, 0x16, 0xB1, 0x58,
  30825. 0x5A, 0x01, 0xB2, 0xC3, 0x04, 0xB2, 0x94, 0x4E,
  30826. 0x90, 0x3A, 0xBB, 0xB3, 0xEC, 0x56, 0x19, 0x44,
  30827. 0x1C, 0xFC, 0x89, 0x65, 0xA4, 0x46, 0xDF, 0x75,
  30828. 0xDE, 0xFA, 0x80, 0xC6, 0xE1, 0x5A, 0xDB, 0xD5,
  30829. 0x06, 0xB7, 0xAB, 0x2D, 0xE1, 0x2D, 0xDA, 0x9B,
  30830. 0xC8, 0x14, 0x41, 0xCF, 0xC8, 0x90, 0x52, 0xE2,
  30831. 0xE5, 0x80, 0x8F, 0x71, 0x26, 0xC6, 0xFD, 0x3A,
  30832. 0xC6, 0xAC, 0x80, 0x81, 0x25, 0x8A, 0x84, 0xA0,
  30833. 0x9A, 0xE5, 0x0F, 0x6C, 0xD7, 0xCC, 0x0F, 0x4A,
  30834. 0xF3, 0x36, 0xFD, 0x1D, 0x64, 0x3E, 0x99, 0x07,
  30835. 0x99, 0x96, 0x26, 0x8C, 0x2D, 0x32, 0xD9, 0x09,
  30836. 0xF2, 0x2E, 0x35, 0x04, 0xF0, 0x7F, 0xBB, 0x56,
  30837. 0x31, 0x96, 0xD4, 0x31, 0x2F, 0xDD, 0xB9, 0x33,
  30838. 0x5D, 0x5C, 0x1D, 0x36, 0xE8, 0xC5, 0xEE, 0xA2,
  30839. 0x27, 0x8D, 0xBA, 0x23, 0xB9, 0x4D, 0x19, 0x3C,
  30840. 0x94, 0x7C, 0xC4, 0x1C, 0xA9, 0x93, 0xDC, 0x7D,
  30841. 0xB1, 0x39, 0x63, 0x40, 0xAD, 0x9C, 0x4F, 0xE6,
  30842. 0x87, 0xDD, 0x7B, 0x8D, 0x0C, 0x7A, 0x51, 0x20,
  30843. 0xAE, 0x02, 0x04, 0xF2, 0xC6, 0x65, 0xBD, 0x5F,
  30844. 0x47, 0x3D, 0x64, 0x4C, 0x7F, 0xF2, 0x6B, 0xFF,
  30845. 0xBA, 0x7A, 0x36, 0x98, 0x08, 0x30, 0x70, 0x21,
  30846. 0x28, 0xA7, 0xE6, 0x61, 0xD6, 0x77, 0xA0, 0x92,
  30847. 0xA3, 0x6E, 0x74, 0x28, 0xA4, 0x13, 0x9F, 0xB2,
  30848. 0x9B, 0x00, 0x95, 0xCC, 0x11, 0x08, 0x6F, 0x44,
  30849. 0x7D, 0x2A, 0x9E, 0xF6, 0xC9, 0xB1, 0x61, 0xF1,
  30850. 0x89, 0xC6, 0x29, 0x9E, 0x08, 0x4C, 0xB7, 0xAA,
  30851. 0x00, 0xFA, 0xF7, 0x87, 0x79, 0x7B, 0xFB, 0x06,
  30852. 0x9F, 0xBC, 0x08, 0x7F, 0xDE, 0x26, 0x25, 0x2A,
  30853. 0x16, 0x64, 0xF1, 0x9C, 0x5A, 0x8A, 0x22, 0xEC,
  30854. 0x5E, 0xE1, 0xAE, 0xB0, 0x76, 0x35, 0x7B, 0x7D,
  30855. 0xC3, 0x7E, 0x6B, 0x0F, 0x15, 0x20, 0xF9, 0x58,
  30856. 0xF7, 0x85, 0x1B, 0xAC, 0xB9, 0x2C, 0x89, 0xFD,
  30857. 0x11, 0x4A, 0x72, 0xFE, 0xAC, 0x54, 0x65, 0x2D,
  30858. 0x45, 0xB0, 0x9E, 0x1A, 0xE7, 0x65, 0x1A, 0xBD,
  30859. 0x16, 0x4B, 0xCD, 0x53, 0x7D, 0x58, 0xFA, 0x39,
  30860. 0xD3, 0xEC, 0x8A, 0xCD, 0xCD, 0xF9, 0x84, 0x25,
  30861. 0x00, 0x58, 0x62, 0xFA, 0x59, 0x69, 0x2D, 0xE1,
  30862. 0x62, 0xB7, 0x7E, 0x62, 0x97, 0xC6, 0x62, 0x33,
  30863. 0x34, 0x84, 0x08, 0xA8, 0xAB, 0x69, 0x5C, 0xE2,
  30864. 0xF2, 0x72, 0x8D, 0xB9, 0xFB, 0xE2, 0x7E, 0x95,
  30865. 0x89, 0x67, 0xEC, 0x59, 0x74, 0x76, 0x7C, 0x5A,
  30866. 0x66, 0x02, 0x30, 0x74, 0xB4, 0xA7, 0x1A, 0xFD,
  30867. 0x26, 0x4A, 0xD2, 0x89, 0x0E, 0x97, 0x0A, 0x1F,
  30868. 0x31, 0xD6, 0xE3, 0x31, 0x1B, 0x73, 0x6F, 0x9F,
  30869. 0x94, 0x88, 0x79, 0x3D, 0xDC, 0x88, 0xF2, 0x34,
  30870. 0x58, 0x06, 0x42, 0x54, 0xC8, 0x2A, 0x1D, 0x9E,
  30871. 0x59, 0xEA, 0xD2, 0xFC, 0xEC, 0x40, 0xB4, 0x30,
  30872. 0x68, 0x7C, 0x4B, 0x7E, 0x28, 0x96, 0x09, 0x26,
  30873. 0xAF, 0xCA, 0xCC, 0x9B, 0xD7, 0x56, 0xA7, 0x10,
  30874. 0x88, 0xC7, 0x84, 0x50, 0xE2, 0x0A, 0x2E, 0x98,
  30875. 0x0A, 0xED, 0xE9, 0xEB, 0xED, 0xFE, 0x7F, 0xAB,
  30876. 0xD6, 0xAB, 0xFE, 0x96, 0xF9, 0x34, 0xC4, 0xB0,
  30877. 0x2C, 0x01, 0xCA, 0x19, 0x4D, 0x01, 0xB7, 0x3C,
  30878. 0x25, 0xD5, 0x99, 0x70, 0x39, 0xD3, 0xFC, 0xD0,
  30879. 0xF0, 0x99, 0x52, 0x1F, 0x70, 0xCA, 0xEE, 0x69,
  30880. 0x11, 0x0A, 0xC1, 0xFC, 0x5A, 0x99, 0x91, 0x7A,
  30881. 0xD7, 0x52, 0xFC, 0x96, 0xAD, 0xFA, 0xD7, 0x18,
  30882. 0x6D, 0x0A, 0x7C, 0x9C, 0xFE, 0x56, 0x01, 0xC0,
  30883. 0x75, 0x14, 0xEA, 0x64, 0x48, 0xD6, 0x61, 0xC5,
  30884. 0x7A, 0xA2, 0x02, 0x42, 0x10, 0x3C, 0x42, 0x76,
  30885. 0xA0, 0x70, 0xA4, 0x89, 0xA4, 0xCB, 0x6B, 0xCA,
  30886. 0x0F, 0x9E, 0xCC, 0x43, 0x79, 0xFB, 0x22, 0x02,
  30887. 0x15, 0xFD, 0x91, 0xF8, 0x10, 0x19, 0xD5, 0xB0,
  30888. 0xAE, 0x61, 0x93, 0x58, 0xB5, 0x24, 0x68, 0xF2,
  30889. 0x72, 0xC1, 0x78, 0xE3, 0xA7, 0x4C, 0xF6, 0x77,
  30890. 0x5A, 0xA9, 0x24, 0xFE, 0x32, 0x9C, 0x31, 0x75,
  30891. 0xD9, 0xE4, 0xC3, 0xE2, 0x1A, 0xB9, 0xEC, 0x83,
  30892. 0x6E, 0xDC, 0x3A, 0xCA, 0xB2, 0xE3, 0x89, 0x1E,
  30893. 0xE8, 0xDE, 0xDA, 0x51, 0x5D, 0x39, 0xAF, 0x9B,
  30894. 0x8D, 0xDD, 0x0E, 0xE7, 0xB0, 0x16, 0x4F, 0x80,
  30895. 0x5C, 0x38, 0x35, 0xF6, 0xD2, 0xBA, 0xBD, 0xB3,
  30896. 0x0E, 0xAB, 0x47, 0x56, 0xE7, 0xEC, 0x7F, 0x82,
  30897. 0x9E, 0xCE, 0x01, 0xE8, 0xEA, 0xDF, 0xBB, 0xED,
  30898. 0x12, 0xFC, 0x28, 0x3B, 0x3D, 0x4C, 0x69, 0xF5,
  30899. 0x75, 0xE7, 0xF8, 0x04, 0x17, 0x68, 0x9F, 0xDF,
  30900. 0xCF, 0xC7, 0xBE, 0x27, 0xEE, 0x3B, 0x8C, 0xDF,
  30901. 0x57, 0xAA, 0xEB, 0xEC, 0x4A, 0x95, 0xB7, 0xE5,
  30902. 0xBB, 0x58, 0x5B, 0x85, 0x22, 0x7F, 0x7C, 0x32,
  30903. 0xBE, 0x30, 0xDB, 0x3E, 0x65, 0xE4, 0x2E, 0x30,
  30904. 0xDC, 0xF5, 0xA5, 0xFA, 0x07, 0x3D, 0xBA, 0x39,
  30905. 0x9D, 0x94, 0x2F, 0x22, 0x22, 0xAD, 0xB9, 0xB9,
  30906. 0x89, 0x81, 0x02, 0xAF, 0xE5, 0x43, 0x2E, 0xDC,
  30907. 0x7F, 0x04, 0xAE, 0x34, 0xA8, 0xFE, 0xC2, 0xD8,
  30908. 0x1C, 0xB4, 0x9A, 0x9A, 0x9B, 0x43, 0x81, 0x4C,
  30909. 0xE7, 0x1D, 0x97, 0xF7, 0x26, 0xE2, 0xB1, 0xE8,
  30910. 0xF6, 0x4B, 0x50, 0xE6, 0x5D, 0xFB, 0x48, 0x16,
  30911. 0xE1, 0x2E, 0x82, 0xA3, 0x19, 0x74, 0x84, 0xA4,
  30912. 0xE9, 0xBB, 0xA4, 0xD2, 0xD6, 0x9E, 0x3F, 0x19,
  30913. 0xD0, 0xB7, 0x5C, 0x21, 0xE2, 0xBF, 0xFE, 0x9F,
  30914. 0xC0, 0xC9, 0x8C, 0xF4, 0x8A, 0x3A, 0xAF, 0x08,
  30915. 0xD4, 0x67, 0xF7, 0x26, 0x87, 0xDF, 0x01, 0x78,
  30916. 0x17, 0x4B, 0x78, 0x97, 0xF7, 0x34, 0x34, 0x9B,
  30917. 0x18, 0x1E, 0xCA, 0x86, 0xA5, 0x98, 0xA0, 0xC5,
  30918. 0xE8, 0xC2, 0x59, 0x46, 0xF2, 0x4D, 0xC5, 0x57,
  30919. 0x2B, 0xD3, 0x24, 0xA4, 0x04, 0x58, 0xA7, 0x88,
  30920. 0xE5, 0x13, 0x7F, 0x3C, 0x7A, 0x7C, 0x97, 0xFC,
  30921. 0x9F, 0x12, 0xA3, 0xC4, 0x63, 0xA8, 0xFE, 0x94,
  30922. 0x49, 0x10, 0x1C, 0xCE, 0x96, 0x6D, 0x7C, 0x00,
  30923. 0x93, 0x23, 0x93, 0x29, 0x98, 0xD5, 0x6E, 0xF4,
  30924. 0x30, 0xC7, 0x3B, 0xC2, 0x4F, 0x5D, 0x95, 0xF7,
  30925. 0x37, 0x85, 0x8D, 0xDC, 0x4F, 0x32, 0xC0, 0x13
  30926. };
  30927. const byte kyber1024_ss[] = {
  30928. 0xB1, 0x0F, 0x73, 0x94, 0x92, 0x6A, 0xD3, 0xB4,
  30929. 0x9C, 0x5D, 0x62, 0xD5, 0xAE, 0xB5, 0x31, 0xD5,
  30930. 0x75, 0x75, 0x38, 0xBC, 0xC0, 0xDA, 0x9E, 0x55,
  30931. 0x0D, 0x43, 0x8F, 0x1B, 0x61, 0xBD, 0x74, 0x19
  30932. };
  30933. ret = wc_KyberKey_Init(KYBER1024, &key, HEAP_HINT, INVALID_DEVID);
  30934. if (ret != 0)
  30935. return WC_TEST_RET_ENC_EC(ret);
  30936. ret = wc_KyberKey_MakeKeyWithRandom(&key, kyber1024_rand,
  30937. sizeof(kyber1024_rand));
  30938. if (ret != 0)
  30939. return WC_TEST_RET_ENC_EC(ret);
  30940. ret = wc_KyberKey_EncodePublicKey(&key, pub, sizeof(pub));
  30941. if (ret != 0)
  30942. return WC_TEST_RET_ENC_EC(ret);
  30943. ret = wc_KyberKey_EncodePrivateKey(&key, priv, sizeof(priv));
  30944. if (ret != 0)
  30945. return WC_TEST_RET_ENC_EC(ret);
  30946. if (XMEMCMP(pub, kyber1024_pk, sizeof(kyber1024_pk)) != 0)
  30947. return WC_TEST_RET_ENC_NC;
  30948. if (XMEMCMP(priv, kyber1024_sk, sizeof(kyber1024_sk)) != 0)
  30949. return WC_TEST_RET_ENC_NC;
  30950. ret = wc_KyberKey_EncapsulateWithRandom(&key, ct, ss, kyber1024enc_rand,
  30951. sizeof(kyber1024enc_rand));
  30952. if (ret != 0)
  30953. return WC_TEST_RET_ENC_EC(ret);
  30954. if (XMEMCMP(ct, kyber1024_ct, sizeof(kyber1024_ct)) != 0)
  30955. return WC_TEST_RET_ENC_NC;
  30956. if (XMEMCMP(ss, kyber1024_ss, sizeof(kyber1024_ss)) != 0)
  30957. return WC_TEST_RET_ENC_NC;
  30958. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, sizeof(kyber1024_ct));
  30959. if (ret != 0)
  30960. return WC_TEST_RET_ENC_EC(ret);
  30961. if (XMEMCMP(ss_dec, kyber1024_ss, sizeof(kyber1024_ss)) != 0)
  30962. return WC_TEST_RET_ENC_NC;
  30963. wc_KyberKey_Free(&key);
  30964. return 0;
  30965. }
  30966. #endif /* WOLFSSL_KYBER1024 */
  30967. #endif /* WOLFSSL_WC_KYBER */
  30968. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t kyber_test(void)
  30969. {
  30970. wc_test_ret_t ret;
  30971. KyberKey key;
  30972. WC_RNG rng;
  30973. int i;
  30974. byte priv[KYBER_MAX_PRIVATE_KEY_SIZE];
  30975. byte pub[KYBER_MAX_PUBLIC_KEY_SIZE];
  30976. byte priv2[KYBER_MAX_PRIVATE_KEY_SIZE];
  30977. byte pub2[KYBER_MAX_PUBLIC_KEY_SIZE];
  30978. byte ct[KYBER_MAX_CIPHER_TEXT_SIZE];
  30979. byte ss[KYBER_SS_SZ];
  30980. byte ss_dec[KYBER_SS_SZ];
  30981. int testData[][4] = {
  30982. #ifdef WOLFSSL_KYBER512
  30983. { KYBER512, KYBER512_PRIVATE_KEY_SIZE, KYBER512_PUBLIC_KEY_SIZE,
  30984. KYBER512_CIPHER_TEXT_SIZE },
  30985. #endif
  30986. #ifdef WOLFSSL_KYBER768
  30987. { KYBER768, KYBER768_PRIVATE_KEY_SIZE, KYBER768_PUBLIC_KEY_SIZE,
  30988. KYBER768_CIPHER_TEXT_SIZE },
  30989. #endif
  30990. #ifdef WOLFSSL_KYBER1024
  30991. { KYBER1024, KYBER1024_PRIVATE_KEY_SIZE, KYBER1024_PUBLIC_KEY_SIZE,
  30992. KYBER1024_CIPHER_TEXT_SIZE },
  30993. #endif
  30994. };
  30995. #ifndef HAVE_FIPS
  30996. ret = wc_InitRng_ex(&rng, HEAP_HINT, INVALID_DEVID);
  30997. #else
  30998. ret = wc_InitRng(&rng);
  30999. #endif
  31000. if (ret != 0)
  31001. return WC_TEST_RET_ENC_EC(ret);
  31002. for (i = 0; i < (int)(sizeof(testData) / sizeof(*testData)); i++) {
  31003. ret = wc_KyberKey_Init(testData[i][0], &key, HEAP_HINT, INVALID_DEVID);
  31004. if (ret != 0)
  31005. return WC_TEST_RET_ENC_I(i);
  31006. ret = wc_KyberKey_MakeKey(&key, &rng);
  31007. if (ret != 0)
  31008. return WC_TEST_RET_ENC_I(i);
  31009. ret = wc_KyberKey_EncodePublicKey(&key, pub, testData[i][2]);
  31010. if (ret != 0)
  31011. return WC_TEST_RET_ENC_I(i);
  31012. ret = wc_KyberKey_EncodePrivateKey(&key, priv, testData[i][1]);
  31013. if (ret != 0)
  31014. return WC_TEST_RET_ENC_I(i);
  31015. ret = wc_KyberKey_Init(testData[i][0], &key, HEAP_HINT, INVALID_DEVID);
  31016. if (ret != 0)
  31017. return WC_TEST_RET_ENC_I(i);
  31018. ret = wc_KyberKey_DecodePublicKey(&key, pub, testData[i][2]);
  31019. if (ret != 0)
  31020. return WC_TEST_RET_ENC_I(i);
  31021. ret = wc_KyberKey_Encapsulate(&key, ct, ss, &rng);
  31022. if (ret != 0)
  31023. return WC_TEST_RET_ENC_I(i);
  31024. ret = wc_KyberKey_EncodePublicKey(&key, pub2, testData[i][2]);
  31025. if (ret != 0)
  31026. return WC_TEST_RET_ENC_I(i);
  31027. if (XMEMCMP(pub, pub2, testData[i][2]) != 0)
  31028. return WC_TEST_RET_ENC_I(i);
  31029. ret = wc_KyberKey_Init(testData[i][0], &key, HEAP_HINT, INVALID_DEVID);
  31030. if (ret != 0)
  31031. return WC_TEST_RET_ENC_I(i);
  31032. ret = wc_KyberKey_DecodePrivateKey(&key, priv, testData[i][1]);
  31033. if (ret != 0)
  31034. return WC_TEST_RET_ENC_I(i);
  31035. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, testData[i][3]);
  31036. if (ret != 0)
  31037. return WC_TEST_RET_ENC_I(i);
  31038. if (XMEMCMP(ss, ss_dec, sizeof(ss)) != 0)
  31039. return WC_TEST_RET_ENC_I(i);
  31040. ret = wc_KyberKey_EncodePrivateKey(&key, priv2, testData[i][1]);
  31041. if (ret != 0)
  31042. return WC_TEST_RET_ENC_I(i);
  31043. if (XMEMCMP(priv, priv2, testData[i][2]) != 0)
  31044. return WC_TEST_RET_ENC_I(i);
  31045. wc_KyberKey_Free(&key);
  31046. }
  31047. wc_FreeRng(&rng);
  31048. #ifdef WOLFSSL_WC_KYBER
  31049. #ifdef WOLFSSL_KYBER512
  31050. ret = kyber512_kat();
  31051. if (ret != 0)
  31052. return ret;
  31053. #endif
  31054. #ifdef WOLFSSL_KYBER768
  31055. ret = kyber768_kat();
  31056. if (ret != 0)
  31057. return ret;
  31058. #endif
  31059. #ifdef WOLFSSL_KYBER1024
  31060. ret = kyber1024_kat();
  31061. if (ret != 0)
  31062. return ret;
  31063. #endif
  31064. #endif /* WOLFSSL_WC_KYBER */
  31065. return 0;
  31066. }
  31067. #endif /* WOLFSSL_HAVE_KYBER */
  31068. static const int fiducial3 = WC_TEST_RET_LN; /* source code reference point --
  31069. * see print_fiducials() below.
  31070. */
  31071. #ifdef WOLFCRYPT_HAVE_ECCSI
  31072. static wc_test_ret_t eccsi_api_test(WC_RNG* rng, EccsiKey* key, mp_int* ssk,
  31073. ecc_point* pvt)
  31074. {
  31075. wc_test_ret_t ret;
  31076. byte id[1] = { 0x00 };
  31077. int valid;
  31078. word32 sz;
  31079. byte data[256];
  31080. byte hash[WC_MAX_DIGEST_SIZE];
  31081. byte hashSz;
  31082. byte sig[257];
  31083. word32 sigSz;
  31084. ret = wc_InitEccsiKey_ex(NULL, 32, ECC_SECP256R1, HEAP_HINT, INVALID_DEVID);
  31085. if (ret != BAD_FUNC_ARG)
  31086. return WC_TEST_RET_ENC_EC(ret);
  31087. ret = wc_InitEccsiKey_ex(NULL, 32, ECC_SECP256R1, HEAP_HINT, INVALID_DEVID);
  31088. if (ret != BAD_FUNC_ARG)
  31089. return WC_TEST_RET_ENC_EC(ret);
  31090. ret = wc_InitEccsiKey(NULL, NULL, INVALID_DEVID);
  31091. if (ret != BAD_FUNC_ARG)
  31092. return WC_TEST_RET_ENC_EC(ret);
  31093. ret = wc_InitEccsiKey(NULL, HEAP_HINT, INVALID_DEVID);
  31094. if (ret != BAD_FUNC_ARG)
  31095. return WC_TEST_RET_ENC_EC(ret);
  31096. wc_FreeEccsiKey(NULL);
  31097. /* Create a valid key. */
  31098. ret = wc_InitEccsiKey(key, NULL, INVALID_DEVID);
  31099. if (ret != 0)
  31100. return WC_TEST_RET_ENC_EC(ret);
  31101. ret = wc_MakeEccsiKey(NULL, NULL);
  31102. if (ret != BAD_FUNC_ARG)
  31103. return WC_TEST_RET_ENC_EC(ret);
  31104. ret = wc_MakeEccsiKey(key, NULL);
  31105. if (ret != BAD_FUNC_ARG)
  31106. return WC_TEST_RET_ENC_EC(ret);
  31107. ret = wc_MakeEccsiKey(NULL, rng);
  31108. if (ret != BAD_FUNC_ARG)
  31109. return WC_TEST_RET_ENC_EC(ret);
  31110. ret = wc_MakeEccsiPair(NULL, NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL,
  31111. NULL);
  31112. if (ret != BAD_FUNC_ARG)
  31113. return WC_TEST_RET_ENC_EC(ret);
  31114. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, NULL);
  31115. if (ret != BAD_FUNC_ARG)
  31116. return WC_TEST_RET_ENC_EC(ret);
  31117. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, NULL, pvt);
  31118. if (ret != BAD_FUNC_ARG)
  31119. return WC_TEST_RET_ENC_EC(ret);
  31120. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, NULL, 1, ssk, pvt);
  31121. if (ret != BAD_FUNC_ARG)
  31122. return WC_TEST_RET_ENC_EC(ret);
  31123. ret = wc_MakeEccsiPair(key, NULL, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  31124. if (ret != BAD_FUNC_ARG)
  31125. return WC_TEST_RET_ENC_EC(ret);
  31126. ret = wc_MakeEccsiPair(NULL, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  31127. if (ret != BAD_FUNC_ARG)
  31128. return WC_TEST_RET_ENC_EC(ret);
  31129. /* No key set */
  31130. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  31131. if (ret != BAD_STATE_E)
  31132. return WC_TEST_RET_ENC_EC(ret);
  31133. ret = wc_ValidateEccsiPair(NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL, NULL,
  31134. NULL);
  31135. if (ret != BAD_FUNC_ARG)
  31136. return WC_TEST_RET_ENC_EC(ret);
  31137. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  31138. NULL);
  31139. if (ret != BAD_FUNC_ARG)
  31140. return WC_TEST_RET_ENC_EC(ret);
  31141. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, NULL,
  31142. &valid);
  31143. if (ret != BAD_FUNC_ARG)
  31144. return WC_TEST_RET_ENC_EC(ret);
  31145. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, NULL, pvt,
  31146. &valid);
  31147. if (ret != BAD_FUNC_ARG)
  31148. return WC_TEST_RET_ENC_EC(ret);
  31149. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, NULL, 1, ssk, pvt,
  31150. &valid);
  31151. if (ret != BAD_FUNC_ARG)
  31152. return WC_TEST_RET_ENC_EC(ret);
  31153. ret = wc_ValidateEccsiPair(NULL, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  31154. &valid);
  31155. if (ret != BAD_FUNC_ARG)
  31156. return WC_TEST_RET_ENC_EC(ret);
  31157. /* No key set */
  31158. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  31159. &valid);
  31160. if (ret != BAD_STATE_E)
  31161. return WC_TEST_RET_ENC_EC(ret);
  31162. ret = wc_ValidateEccsiPvt(NULL, NULL, NULL);
  31163. if (ret != BAD_FUNC_ARG)
  31164. return WC_TEST_RET_ENC_EC(ret);
  31165. ret = wc_ValidateEccsiPvt(key, NULL, NULL);
  31166. if (ret != BAD_FUNC_ARG)
  31167. return WC_TEST_RET_ENC_EC(ret);
  31168. ret = wc_ValidateEccsiPvt(NULL, pvt, NULL);
  31169. if (ret != BAD_FUNC_ARG)
  31170. return WC_TEST_RET_ENC_EC(ret);
  31171. ret = wc_ValidateEccsiPvt(NULL, NULL, &valid);
  31172. if (ret != BAD_FUNC_ARG)
  31173. return WC_TEST_RET_ENC_EC(ret);
  31174. ret = wc_ValidateEccsiPvt(key, pvt, NULL);
  31175. if (ret != BAD_FUNC_ARG)
  31176. return WC_TEST_RET_ENC_EC(ret);
  31177. ret = wc_ValidateEccsiPvt(key, NULL, &valid);
  31178. if (ret != BAD_FUNC_ARG)
  31179. return WC_TEST_RET_ENC_EC(ret);
  31180. ret = wc_ValidateEccsiPvt(NULL, pvt, &valid);
  31181. if (ret != BAD_FUNC_ARG)
  31182. return WC_TEST_RET_ENC_EC(ret);
  31183. ret = wc_EncodeEccsiPair(NULL, NULL, NULL, data, NULL);
  31184. if (ret != BAD_FUNC_ARG)
  31185. return WC_TEST_RET_ENC_EC(ret);
  31186. ret = wc_EncodeEccsiPair(key, ssk, pvt, data, NULL);
  31187. if (ret != BAD_FUNC_ARG)
  31188. return WC_TEST_RET_ENC_EC(ret);
  31189. ret = wc_EncodeEccsiPair(key, ssk, NULL, data, &sz);
  31190. if (ret != BAD_FUNC_ARG)
  31191. return WC_TEST_RET_ENC_EC(ret);
  31192. ret = wc_EncodeEccsiPair(key, NULL, pvt, data, &sz);
  31193. if (ret != BAD_FUNC_ARG)
  31194. return WC_TEST_RET_ENC_EC(ret);
  31195. ret = wc_EncodeEccsiPair(NULL, ssk, pvt, data, &sz);
  31196. if (ret != BAD_FUNC_ARG)
  31197. return WC_TEST_RET_ENC_EC(ret);
  31198. /* No key created so no curve information. */
  31199. ret = wc_EncodeEccsiPair(key, ssk, pvt, NULL, &sz);
  31200. if (ret != LENGTH_ONLY_E)
  31201. return WC_TEST_RET_ENC_EC(ret);
  31202. ret = wc_EncodeEccsiSsk(NULL, NULL, data, NULL);
  31203. if (ret != BAD_FUNC_ARG)
  31204. return WC_TEST_RET_ENC_EC(ret);
  31205. ret = wc_EncodeEccsiSsk(key, ssk, data, NULL);
  31206. if (ret != BAD_FUNC_ARG)
  31207. return WC_TEST_RET_ENC_EC(ret);
  31208. ret = wc_EncodeEccsiSsk(key, NULL, data, &sz);
  31209. if (ret != BAD_FUNC_ARG)
  31210. return WC_TEST_RET_ENC_EC(ret);
  31211. ret = wc_EncodeEccsiSsk(NULL, ssk, data, &sz);
  31212. if (ret != BAD_FUNC_ARG)
  31213. return WC_TEST_RET_ENC_EC(ret);
  31214. ret = wc_EncodeEccsiPvt(NULL, NULL, data, NULL, 1);
  31215. if (ret != BAD_FUNC_ARG)
  31216. return WC_TEST_RET_ENC_EC(ret);
  31217. ret = wc_EncodeEccsiPvt(key, pvt, data, NULL, 1);
  31218. if (ret != BAD_FUNC_ARG)
  31219. return WC_TEST_RET_ENC_EC(ret);
  31220. ret = wc_EncodeEccsiPvt(key, NULL, data, &sz, 1);
  31221. if (ret != BAD_FUNC_ARG)
  31222. return WC_TEST_RET_ENC_EC(ret);
  31223. ret = wc_EncodeEccsiPvt(NULL, pvt, data, &sz, 1);
  31224. if (ret != BAD_FUNC_ARG)
  31225. return WC_TEST_RET_ENC_EC(ret);
  31226. ret = wc_DecodeEccsiPair(NULL, NULL, 0, NULL, NULL);
  31227. if (ret != BAD_FUNC_ARG)
  31228. return WC_TEST_RET_ENC_EC(ret);
  31229. ret = wc_DecodeEccsiPair(key, data, 0, ssk, NULL);
  31230. if (ret != BAD_FUNC_ARG)
  31231. return WC_TEST_RET_ENC_EC(ret);
  31232. ret = wc_DecodeEccsiPair(key, data, 0, NULL, pvt);
  31233. if (ret != BAD_FUNC_ARG)
  31234. return WC_TEST_RET_ENC_EC(ret);
  31235. ret = wc_DecodeEccsiPair(key, NULL, 0, ssk, pvt);
  31236. if (ret != BAD_FUNC_ARG)
  31237. return WC_TEST_RET_ENC_EC(ret);
  31238. ret = wc_DecodeEccsiPair(NULL, data, 0, ssk, pvt);
  31239. if (ret != BAD_FUNC_ARG)
  31240. return WC_TEST_RET_ENC_EC(ret);
  31241. ret = wc_DecodeEccsiSsk(NULL, NULL, 0, NULL);
  31242. if (ret != BAD_FUNC_ARG)
  31243. return WC_TEST_RET_ENC_EC(ret);
  31244. ret = wc_DecodeEccsiSsk(key, data, 0, NULL);
  31245. if (ret != BAD_FUNC_ARG)
  31246. return WC_TEST_RET_ENC_EC(ret);
  31247. ret = wc_DecodeEccsiSsk(key, NULL, 0, ssk);
  31248. if (ret != BAD_FUNC_ARG)
  31249. return WC_TEST_RET_ENC_EC(ret);
  31250. ret = wc_DecodeEccsiSsk(NULL, data, 0, ssk);
  31251. if (ret != BAD_FUNC_ARG)
  31252. return WC_TEST_RET_ENC_EC(ret);
  31253. ret = wc_DecodeEccsiPvt(NULL, NULL, 0, NULL);
  31254. if (ret != BAD_FUNC_ARG)
  31255. return WC_TEST_RET_ENC_EC(ret);
  31256. ret = wc_DecodeEccsiPvt(key, data, 0, NULL);
  31257. if (ret != BAD_FUNC_ARG)
  31258. return WC_TEST_RET_ENC_EC(ret);
  31259. ret = wc_DecodeEccsiPvt(key, NULL, 0, pvt);
  31260. if (ret != BAD_FUNC_ARG)
  31261. return WC_TEST_RET_ENC_EC(ret);
  31262. ret = wc_DecodeEccsiPvt(NULL, data, 0, pvt);
  31263. if (ret != BAD_FUNC_ARG)
  31264. return WC_TEST_RET_ENC_EC(ret);
  31265. ret = wc_DecodeEccsiPvtFromSig(NULL, NULL, 0, NULL);
  31266. if (ret != BAD_FUNC_ARG)
  31267. return WC_TEST_RET_ENC_EC(ret);
  31268. ret = wc_DecodeEccsiPvtFromSig(key, data, 0, NULL);
  31269. if (ret != BAD_FUNC_ARG)
  31270. return WC_TEST_RET_ENC_EC(ret);
  31271. ret = wc_DecodeEccsiPvtFromSig(key, NULL, 0, pvt);
  31272. if (ret != BAD_FUNC_ARG)
  31273. return WC_TEST_RET_ENC_EC(ret);
  31274. ret = wc_DecodeEccsiPvtFromSig(NULL, data, 0, pvt);
  31275. if (ret != BAD_FUNC_ARG)
  31276. return WC_TEST_RET_ENC_EC(ret);
  31277. ret = wc_ExportEccsiKey(NULL, data, NULL);
  31278. if (ret != BAD_FUNC_ARG)
  31279. return WC_TEST_RET_ENC_EC(ret);
  31280. ret = wc_ExportEccsiKey(key, data, NULL);
  31281. if (ret != BAD_FUNC_ARG)
  31282. return WC_TEST_RET_ENC_EC(ret);
  31283. ret = wc_ExportEccsiKey(NULL, data, &sz);
  31284. if (ret != BAD_FUNC_ARG)
  31285. return WC_TEST_RET_ENC_EC(ret);
  31286. /* No key to export */
  31287. ret = wc_ExportEccsiKey(key, NULL, &sz);
  31288. if (ret != BAD_STATE_E)
  31289. return WC_TEST_RET_ENC_EC(ret);
  31290. ret = wc_ImportEccsiKey(NULL, NULL, 0);
  31291. if (ret != BAD_FUNC_ARG)
  31292. return WC_TEST_RET_ENC_EC(ret);
  31293. ret = wc_ImportEccsiKey(key, NULL, 0);
  31294. if (ret != BAD_FUNC_ARG)
  31295. return WC_TEST_RET_ENC_EC(ret);
  31296. ret = wc_ImportEccsiKey(NULL, data, 0);
  31297. if (ret != BAD_FUNC_ARG)
  31298. return WC_TEST_RET_ENC_EC(ret);
  31299. ret = wc_ExportEccsiPrivateKey(NULL, data, NULL);
  31300. if (ret != BAD_FUNC_ARG)
  31301. return WC_TEST_RET_ENC_EC(ret);
  31302. ret = wc_ExportEccsiPrivateKey(key, data, NULL);
  31303. if (ret != BAD_FUNC_ARG)
  31304. return WC_TEST_RET_ENC_EC(ret);
  31305. ret = wc_ExportEccsiPrivateKey(NULL, data, &sz);
  31306. if (ret != BAD_FUNC_ARG)
  31307. return WC_TEST_RET_ENC_EC(ret);
  31308. /* No key to export */
  31309. ret = wc_ExportEccsiPrivateKey(key, NULL, &sz);
  31310. if (ret != BAD_STATE_E)
  31311. return WC_TEST_RET_ENC_EC(ret);
  31312. ret = wc_ImportEccsiPrivateKey(NULL, NULL, 0);
  31313. if (ret != BAD_FUNC_ARG)
  31314. return WC_TEST_RET_ENC_EC(ret);
  31315. ret = wc_ImportEccsiPrivateKey(key, NULL, 0);
  31316. if (ret != BAD_FUNC_ARG)
  31317. return WC_TEST_RET_ENC_EC(ret);
  31318. ret = wc_ImportEccsiPrivateKey(NULL, data, 0);
  31319. if (ret != BAD_FUNC_ARG)
  31320. return WC_TEST_RET_ENC_EC(ret);
  31321. ret = wc_ExportEccsiPublicKey(NULL, data, NULL, 1);
  31322. if (ret != BAD_FUNC_ARG)
  31323. return WC_TEST_RET_ENC_EC(ret);
  31324. ret = wc_ExportEccsiPublicKey(key, data, NULL, 1);
  31325. if (ret != BAD_FUNC_ARG)
  31326. return WC_TEST_RET_ENC_EC(ret);
  31327. ret = wc_ExportEccsiPublicKey(NULL, data, &sz, 1);
  31328. if (ret != BAD_FUNC_ARG)
  31329. return WC_TEST_RET_ENC_EC(ret);
  31330. /* No key to export */
  31331. ret = wc_ExportEccsiPublicKey(key, data, &sz, 1);
  31332. if (ret != BAD_STATE_E)
  31333. return WC_TEST_RET_ENC_EC(ret);
  31334. ret = wc_ImportEccsiPublicKey(NULL, NULL, 0, 1);
  31335. if (ret != BAD_FUNC_ARG)
  31336. return WC_TEST_RET_ENC_EC(ret);
  31337. ret = wc_ImportEccsiPublicKey(key, NULL, 0, 1);
  31338. if (ret != BAD_FUNC_ARG)
  31339. return WC_TEST_RET_ENC_EC(ret);
  31340. ret = wc_ImportEccsiPublicKey(NULL, data, 0, 1);
  31341. if (ret != BAD_FUNC_ARG)
  31342. return WC_TEST_RET_ENC_EC(ret);
  31343. ret = wc_HashEccsiId(NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL, NULL, NULL);
  31344. if (ret != BAD_FUNC_ARG)
  31345. return WC_TEST_RET_ENC_EC(ret);
  31346. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, NULL);
  31347. if (ret != BAD_FUNC_ARG)
  31348. return WC_TEST_RET_ENC_EC(ret);
  31349. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, NULL, &hashSz);
  31350. if (ret != BAD_FUNC_ARG)
  31351. return WC_TEST_RET_ENC_EC(ret);
  31352. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, NULL, hash, &hashSz);
  31353. if (ret != BAD_FUNC_ARG)
  31354. return WC_TEST_RET_ENC_EC(ret);
  31355. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, NULL, 1, pvt, hash,
  31356. &hashSz);
  31357. if (ret != BAD_FUNC_ARG)
  31358. return WC_TEST_RET_ENC_EC(ret);
  31359. ret = wc_HashEccsiId(NULL, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, &hashSz);
  31360. if (ret != BAD_FUNC_ARG)
  31361. return WC_TEST_RET_ENC_EC(ret);
  31362. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, &hashSz);
  31363. if (ret != BAD_STATE_E)
  31364. return WC_TEST_RET_ENC_EC(ret);
  31365. ret = wc_SetEccsiHash(NULL, NULL, 1);
  31366. if (ret != BAD_FUNC_ARG)
  31367. return WC_TEST_RET_ENC_EC(ret);
  31368. ret = wc_SetEccsiHash(key, NULL, 1);
  31369. if (ret != BAD_FUNC_ARG)
  31370. return WC_TEST_RET_ENC_EC(ret);
  31371. ret = wc_SetEccsiHash(NULL, hash, 1);
  31372. if (ret != BAD_FUNC_ARG)
  31373. return WC_TEST_RET_ENC_EC(ret);
  31374. ret = wc_SetEccsiPair(NULL, NULL, NULL);
  31375. if (ret != BAD_FUNC_ARG)
  31376. return WC_TEST_RET_ENC_EC(ret);
  31377. ret = wc_SetEccsiPair(key, NULL, NULL);
  31378. if (ret != BAD_FUNC_ARG)
  31379. return WC_TEST_RET_ENC_EC(ret);
  31380. ret = wc_SetEccsiPair(NULL, ssk, NULL);
  31381. if (ret != BAD_FUNC_ARG)
  31382. return WC_TEST_RET_ENC_EC(ret);
  31383. ret = wc_SetEccsiPair(NULL, NULL, pvt);
  31384. if (ret != BAD_FUNC_ARG)
  31385. return WC_TEST_RET_ENC_EC(ret);
  31386. ret = wc_SetEccsiPair(key, ssk, NULL);
  31387. if (ret != BAD_FUNC_ARG)
  31388. return WC_TEST_RET_ENC_EC(ret);
  31389. ret = wc_SetEccsiPair(key, NULL, pvt);
  31390. if (ret != BAD_FUNC_ARG)
  31391. return WC_TEST_RET_ENC_EC(ret);
  31392. ret = wc_SetEccsiPair(NULL, ssk, pvt);
  31393. if (ret != BAD_FUNC_ARG)
  31394. return WC_TEST_RET_ENC_EC(ret);
  31395. ret = wc_SignEccsiHash(NULL, NULL, WC_HASH_TYPE_SHA256, NULL, 0, sig, NULL);
  31396. if (ret != BAD_FUNC_ARG)
  31397. return WC_TEST_RET_ENC_EC(ret);
  31398. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, sig, NULL);
  31399. if (ret != BAD_FUNC_ARG)
  31400. return WC_TEST_RET_ENC_EC(ret);
  31401. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, NULL, 0, sig,
  31402. &sigSz);
  31403. if (ret != BAD_FUNC_ARG)
  31404. return WC_TEST_RET_ENC_EC(ret);
  31405. ret = wc_SignEccsiHash(key, NULL, WC_HASH_TYPE_SHA256, data, 0, sig,
  31406. &sigSz);
  31407. if (ret != BAD_FUNC_ARG)
  31408. return WC_TEST_RET_ENC_EC(ret);
  31409. ret = wc_SignEccsiHash(NULL, rng, WC_HASH_TYPE_SHA256, data, 0, sig,
  31410. &sigSz);
  31411. if (ret != BAD_FUNC_ARG)
  31412. return WC_TEST_RET_ENC_EC(ret);
  31413. /* Key not set. */
  31414. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, NULL,
  31415. &sigSz);
  31416. if (ret != BAD_STATE_E)
  31417. return WC_TEST_RET_ENC_EC(ret);
  31418. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0, NULL);
  31419. if (ret != BAD_FUNC_ARG)
  31420. return WC_TEST_RET_ENC_EC(ret);
  31421. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0, NULL);
  31422. if (ret != BAD_FUNC_ARG)
  31423. return WC_TEST_RET_ENC_EC(ret);
  31424. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, data, 0, NULL, 0, NULL);
  31425. if (ret != BAD_FUNC_ARG)
  31426. return WC_TEST_RET_ENC_EC(ret);
  31427. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, sig, 0, NULL);
  31428. if (ret != BAD_FUNC_ARG)
  31429. return WC_TEST_RET_ENC_EC(ret);
  31430. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0,
  31431. &valid);
  31432. if (ret != BAD_FUNC_ARG)
  31433. return WC_TEST_RET_ENC_EC(ret);
  31434. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, sig, 0, NULL);
  31435. if (ret != BAD_FUNC_ARG)
  31436. return WC_TEST_RET_ENC_EC(ret);
  31437. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, NULL, 0,
  31438. &valid);
  31439. if (ret != BAD_FUNC_ARG)
  31440. return WC_TEST_RET_ENC_EC(ret);
  31441. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, NULL, 0, sig, 0,
  31442. &valid);
  31443. if (ret != BAD_FUNC_ARG)
  31444. return WC_TEST_RET_ENC_EC(ret);
  31445. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, data, 0, sig, 0,
  31446. &valid);
  31447. if (ret != BAD_FUNC_ARG)
  31448. return WC_TEST_RET_ENC_EC(ret);
  31449. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, sig, 0,
  31450. &valid);
  31451. if (ret != BAD_STATE_E)
  31452. return WC_TEST_RET_ENC_EC(ret);
  31453. ret = wc_SetEccsiPair(key, ssk, pvt);
  31454. if (ret != 0)
  31455. return WC_TEST_RET_ENC_EC(ret);
  31456. /* Identity hash not set. */
  31457. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, NULL,
  31458. &sigSz);
  31459. if (ret != BAD_STATE_E)
  31460. return WC_TEST_RET_ENC_EC(ret);
  31461. wc_FreeEccsiKey(key);
  31462. return 0;
  31463. }
  31464. /* RFC 6507: Appendix A */
  31465. static wc_test_ret_t eccsi_kat_verify_test(EccsiKey* key, ecc_point* pvt)
  31466. {
  31467. wc_test_ret_t ret;
  31468. int verified;
  31469. const byte msg[] = { 0x6D, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x00 };
  31470. word32 msgSz = sizeof(msg);
  31471. byte hash[WC_SHA256_DIGEST_SIZE];
  31472. byte hashSz = WC_SHA256_DIGEST_SIZE;
  31473. static const byte id[] = {
  31474. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  31475. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  31476. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  31477. 0x33, 0x00
  31478. };
  31479. word32 idSz = sizeof(id);
  31480. static const byte sig[] = {
  31481. 0x26, 0x9D, 0x4C, 0x8F, 0xDE, 0xB6, 0x6A, 0x74,
  31482. 0xE4, 0xEF, 0x8C, 0x0D, 0x5D, 0xCC, 0x59, 0x7D,
  31483. 0xDF, 0xE6, 0x02, 0x9C, 0x2A, 0xFF, 0xC4, 0x93,
  31484. 0x60, 0x08, 0xCD, 0x2C, 0xC1, 0x04, 0x5D, 0x81,
  31485. 0xE0, 0x9B, 0x52, 0x8D, 0x0E, 0xF8, 0xD6, 0xDF,
  31486. 0x1A, 0xA3, 0xEC, 0xBF, 0x80, 0x11, 0x0C, 0xFC,
  31487. 0xEC, 0x9F, 0xC6, 0x82, 0x52, 0xCE, 0xBB, 0x67,
  31488. 0x9F, 0x41, 0x34, 0x84, 0x69, 0x40, 0xCC, 0xFD,
  31489. 0x04,
  31490. 0x75, 0x8A, 0x14, 0x27, 0x79, 0xBE, 0x89, 0xE8,
  31491. 0x29, 0xE7, 0x19, 0x84, 0xCB, 0x40, 0xEF, 0x75,
  31492. 0x8C, 0xC4, 0xAD, 0x77, 0x5F, 0xC5, 0xB9, 0xA3,
  31493. 0xE1, 0xC8, 0xED, 0x52, 0xF6, 0xFA, 0x36, 0xD9,
  31494. 0xA7, 0x9D, 0x24, 0x76, 0x92, 0xF4, 0xED, 0xA3,
  31495. 0xA6, 0xBD, 0xAB, 0x77, 0xD6, 0xAA, 0x64, 0x74,
  31496. 0xA4, 0x64, 0xAE, 0x49, 0x34, 0x66, 0x3C, 0x52,
  31497. 0x65, 0xBA, 0x70, 0x18, 0xBA, 0x09, 0x1F, 0x79
  31498. };
  31499. word32 sigSz = sizeof(sig);
  31500. static const byte pubData[] = {
  31501. 0x50, 0xD4, 0x67, 0x0B, 0xDE, 0x75, 0x24, 0x4F,
  31502. 0x28, 0xD2, 0x83, 0x8A, 0x0D, 0x25, 0x55, 0x8A,
  31503. 0x7A, 0x72, 0x68, 0x6D, 0x45, 0x22, 0xD4, 0xC8,
  31504. 0x27, 0x3F, 0xB6, 0x44, 0x2A, 0xEB, 0xFA, 0x93,
  31505. 0xDB, 0xDD, 0x37, 0x55, 0x1A, 0xFD, 0x26, 0x3B,
  31506. 0x5D, 0xFD, 0x61, 0x7F, 0x39, 0x60, 0xC6, 0x5A,
  31507. 0x8C, 0x29, 0x88, 0x50, 0xFF, 0x99, 0xF2, 0x03,
  31508. 0x66, 0xDC, 0xE7, 0xD4, 0x36, 0x72, 0x17, 0xF4
  31509. };
  31510. static const byte expHash[] = {
  31511. 0x49, 0x0f, 0x3f, 0xeb, 0xbc, 0x1c, 0x90, 0x2f,
  31512. 0x62, 0x89, 0x72, 0x3d, 0x7f, 0x8c, 0xbf, 0x79,
  31513. 0xdb, 0x88, 0x93, 0x08, 0x49, 0xd1, 0x9f, 0x38,
  31514. 0xf0, 0x29, 0x5b, 0x5c, 0x27, 0x6c, 0x14, 0xd1
  31515. };
  31516. ret = wc_ImportEccsiPublicKey(key, pubData, sizeof(pubData), 0);
  31517. if (ret != 0)
  31518. return WC_TEST_RET_ENC_EC(ret);
  31519. ret = wc_DecodeEccsiPvtFromSig(key, sig, sigSz, pvt);
  31520. if (ret != 0)
  31521. return WC_TEST_RET_ENC_EC(ret);
  31522. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, idSz, pvt, hash,
  31523. &hashSz);
  31524. if (ret != 0)
  31525. return WC_TEST_RET_ENC_EC(ret);
  31526. if (hashSz != sizeof(expHash))
  31527. return WC_TEST_RET_ENC_NC;
  31528. if (XMEMCMP(hash, expHash, hashSz) != 0)
  31529. return WC_TEST_RET_ENC_NC;
  31530. ret = wc_SetEccsiHash(key, hash, hashSz);
  31531. if (ret != 0)
  31532. return WC_TEST_RET_ENC_EC(ret);
  31533. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31534. &verified);
  31535. if (ret != 0)
  31536. return WC_TEST_RET_ENC_EC(ret);
  31537. if (!verified)
  31538. return WC_TEST_RET_ENC_NC;
  31539. return 0;
  31540. }
  31541. static wc_test_ret_t eccsi_enc_dec_pair_test(EccsiKey* priv, mp_int* ssk, ecc_point* pvt)
  31542. {
  31543. wc_test_ret_t ret;
  31544. byte data[32 * 3];
  31545. word32 sz;
  31546. ecc_point* decPvt = NULL;
  31547. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  31548. mp_int *decSsk = (mp_int *)XMALLOC(sizeof(*decSsk), HEAP_HINT,
  31549. DYNAMIC_TYPE_TMP_BUFFER);
  31550. if (decSsk == NULL)
  31551. return WC_TEST_RET_ENC_ERRNO;
  31552. #else
  31553. mp_int decSsk[1];
  31554. #endif
  31555. ret = mp_init(decSsk);
  31556. if (ret != 0)
  31557. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  31558. decPvt = wc_ecc_new_point();
  31559. if (decPvt == NULL)
  31560. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  31561. ret = wc_EncodeEccsiPair(priv, ssk, pvt, NULL, &sz);
  31562. if (ret != LENGTH_ONLY_E)
  31563. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  31564. if (sz != 32 * 3)
  31565. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  31566. ret = wc_EncodeEccsiPair(priv, ssk, pvt, data, &sz);
  31567. if (ret != 0)
  31568. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  31569. if (sz != 32* 3)
  31570. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  31571. ret = wc_DecodeEccsiPair(priv, data, sz, decSsk, decPvt);
  31572. if (ret != 0)
  31573. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  31574. ret = mp_cmp(ssk, decSsk);
  31575. if (ret != MP_EQ)
  31576. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  31577. ret = wc_ecc_cmp_point(pvt, decPvt);
  31578. if (ret != MP_EQ)
  31579. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  31580. ret = wc_EncodeEccsiSsk(priv, ssk, NULL, &sz);
  31581. if (ret != LENGTH_ONLY_E)
  31582. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  31583. if (sz != 32)
  31584. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  31585. ret = wc_EncodeEccsiSsk(priv, ssk, data, &sz);
  31586. if (ret != 0)
  31587. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  31588. if (sz != 32)
  31589. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  31590. ret = wc_DecodeEccsiSsk(priv, data, sz, decSsk);
  31591. if (ret != 0)
  31592. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  31593. ret = mp_cmp(ssk, decSsk);
  31594. if (ret != MP_EQ)
  31595. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  31596. ret = wc_EncodeEccsiPvt(priv, pvt, NULL, &sz, 1);
  31597. if (ret != LENGTH_ONLY_E)
  31598. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  31599. if (sz != 32 * 2)
  31600. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  31601. ret = wc_EncodeEccsiPvt(priv, pvt, data, &sz, 1);
  31602. if (ret != 0)
  31603. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  31604. if (sz != 32 * 2)
  31605. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  31606. ret = wc_DecodeEccsiPvt(priv, data, sz, decPvt);
  31607. if (ret != 0)
  31608. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  31609. ret = wc_ecc_cmp_point(pvt, decPvt);
  31610. if (ret != MP_EQ)
  31611. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  31612. sz = sizeof(data);
  31613. ret = wc_EncodeEccsiPvt(priv, pvt, data, &sz, 0);
  31614. if (ret != 0)
  31615. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  31616. if (sz != 32 * 2 + 1)
  31617. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  31618. ret = wc_DecodeEccsiPvt(priv, data, sz, decPvt);
  31619. if (ret != 0)
  31620. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  31621. ret = wc_ecc_cmp_point(pvt, decPvt);
  31622. if (ret != MP_EQ)
  31623. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  31624. wc_ecc_del_point(decPvt);
  31625. out:
  31626. mp_free(decSsk);
  31627. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  31628. XFREE(decSsk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31629. #endif
  31630. return ret;
  31631. }
  31632. static wc_test_ret_t eccsi_imp_exp_key_test(EccsiKey* priv)
  31633. {
  31634. wc_test_ret_t ret;
  31635. byte data[32 * 3];
  31636. byte out[32 * 3];
  31637. word32 sz;
  31638. ret = wc_ExportEccsiKey(priv, NULL, &sz);
  31639. if (ret != LENGTH_ONLY_E)
  31640. return WC_TEST_RET_ENC_EC(ret);
  31641. if (sz != 32 * 3)
  31642. return WC_TEST_RET_ENC_NC;
  31643. ret = wc_ExportEccsiKey(priv, data, &sz);
  31644. if (ret != 0)
  31645. return WC_TEST_RET_ENC_EC(ret);
  31646. ret = wc_ImportEccsiKey(priv, data, sz);
  31647. if (ret != 0)
  31648. return WC_TEST_RET_ENC_EC(ret);
  31649. ret = wc_ExportEccsiKey(priv, NULL, &sz);
  31650. if (ret != LENGTH_ONLY_E)
  31651. return WC_TEST_RET_ENC_EC(ret);
  31652. if (sz != 32 * 3)
  31653. return WC_TEST_RET_ENC_NC;
  31654. ret = wc_ExportEccsiKey(priv, out, &sz);
  31655. if (ret != 0)
  31656. return WC_TEST_RET_ENC_EC(ret);
  31657. if (sz != 32 * 3)
  31658. return WC_TEST_RET_ENC_NC;
  31659. if (XMEMCMP(data, out, sz) != 0)
  31660. return WC_TEST_RET_ENC_NC;
  31661. ret = wc_ExportEccsiPrivateKey(priv, NULL, &sz);
  31662. if (ret != LENGTH_ONLY_E)
  31663. return WC_TEST_RET_ENC_EC(ret);
  31664. if (sz != 32)
  31665. return WC_TEST_RET_ENC_NC;
  31666. ret = wc_ExportEccsiPrivateKey(priv, data, &sz);
  31667. if (ret != 0)
  31668. return WC_TEST_RET_ENC_EC(ret);
  31669. ret = wc_ImportEccsiPrivateKey(priv, data, sz);
  31670. if (ret != 0)
  31671. return WC_TEST_RET_ENC_EC(ret);
  31672. ret = wc_ExportEccsiPrivateKey(priv, NULL, &sz);
  31673. if (ret != LENGTH_ONLY_E)
  31674. return WC_TEST_RET_ENC_EC(ret);
  31675. if (sz != 32)
  31676. return WC_TEST_RET_ENC_NC;
  31677. ret = wc_ExportEccsiPrivateKey(priv, out, &sz);
  31678. if (ret != 0)
  31679. return WC_TEST_RET_ENC_EC(ret);
  31680. if (sz != 32)
  31681. return WC_TEST_RET_ENC_NC;
  31682. if (XMEMCMP(data, out, sz) != 0)
  31683. return WC_TEST_RET_ENC_NC;
  31684. return 0;
  31685. }
  31686. static wc_test_ret_t eccsi_imp_exp_pubkey_test(EccsiKey* key1, EccsiKey* key2)
  31687. {
  31688. wc_test_ret_t ret;
  31689. byte data[32 * 2 + 1];
  31690. byte pubData[32 * 2 + 1];
  31691. word32 sz;
  31692. ret = wc_ExportEccsiPublicKey(key1, NULL, &sz, 1);
  31693. if (ret != LENGTH_ONLY_E)
  31694. return WC_TEST_RET_ENC_EC(ret);
  31695. if (sz != 32 * 2)
  31696. return WC_TEST_RET_ENC_NC;
  31697. ret = wc_ExportEccsiPublicKey(key1, data, &sz, 1);
  31698. if (ret != 0)
  31699. return WC_TEST_RET_ENC_EC(ret);
  31700. ret = wc_ImportEccsiPublicKey(key2, data, sz, 1);
  31701. if (ret != 0)
  31702. return WC_TEST_RET_ENC_EC(ret);
  31703. sz = sizeof(pubData);
  31704. ret = wc_ExportEccsiPublicKey(key2, pubData, &sz, 1);
  31705. if (ret != 0)
  31706. return WC_TEST_RET_ENC_EC(ret);
  31707. if (sz != 32 * 2)
  31708. return WC_TEST_RET_ENC_NC;
  31709. if (XMEMCMP(data, pubData, sz) != 0)
  31710. return WC_TEST_RET_ENC_NC;
  31711. sz = sizeof(pubData);
  31712. ret = wc_ExportEccsiPublicKey(key2, pubData, &sz, 0);
  31713. if (ret != 0)
  31714. return WC_TEST_RET_ENC_EC(ret);
  31715. if (sz != 32 * 2 + 1)
  31716. return WC_TEST_RET_ENC_NC;
  31717. if (pubData[0] != 0x04)
  31718. return WC_TEST_RET_ENC_NC;
  31719. if (XMEMCMP(pubData + 1, data, sz - 1) != 0)
  31720. return WC_TEST_RET_ENC_NC;
  31721. ret = wc_ImportEccsiPublicKey(key2, pubData, sz, 0);
  31722. if (ret != 0)
  31723. return WC_TEST_RET_ENC_EC(ret);
  31724. return 0;
  31725. }
  31726. static wc_test_ret_t eccsi_make_key_test(EccsiKey* priv, EccsiKey* pub, WC_RNG* rng,
  31727. mp_int* ssk, ecc_point* pvt)
  31728. {
  31729. wc_test_ret_t ret;
  31730. char mail[] = "test@wolfssl.com";
  31731. byte* id = (byte*)mail;
  31732. word32 idSz = (word32) XSTRLEN(mail);
  31733. int valid;
  31734. ret = wc_MakeEccsiKey(priv, rng);
  31735. if (ret != 0)
  31736. return WC_TEST_RET_ENC_EC(ret);
  31737. ret = eccsi_imp_exp_key_test(priv);
  31738. if (ret < 0)
  31739. return ret;
  31740. ret = eccsi_imp_exp_pubkey_test(priv, pub);
  31741. if (ret < 0)
  31742. return ret;
  31743. ret = wc_MakeEccsiPair(priv, rng, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt);
  31744. if (ret != 0)
  31745. return WC_TEST_RET_ENC_EC(ret);
  31746. ret = wc_ValidateEccsiPair(pub, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt,
  31747. &valid);
  31748. if (ret != 0)
  31749. return WC_TEST_RET_ENC_EC(ret);
  31750. if (!valid)
  31751. return WC_TEST_RET_ENC_NC;
  31752. ret = eccsi_enc_dec_pair_test(priv, ssk, pvt);
  31753. if (ret != 0)
  31754. return ret;
  31755. return 0;
  31756. }
  31757. static wc_test_ret_t eccsi_sign_verify_test(EccsiKey* priv, EccsiKey* pub, WC_RNG* rng,
  31758. mp_int* ssk, ecc_point* pvt)
  31759. {
  31760. wc_test_ret_t ret;
  31761. byte hashPriv[WC_MAX_DIGEST_SIZE];
  31762. byte hashPub[WC_MAX_DIGEST_SIZE];
  31763. byte hashSz;
  31764. byte sig[144];
  31765. word32 sigSz;
  31766. int verified, valid;
  31767. char mail[] = "test@wolfssl.com";
  31768. byte* id = (byte*)mail;
  31769. word32 idSz = (word32) XSTRLEN(mail);
  31770. byte msg[] = { 0x00 };
  31771. word32 msgSz = sizeof(msg);
  31772. #ifdef WOLFSSL_SHA384
  31773. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA384, id, idSz, pvt, hashPriv,
  31774. &hashSz);
  31775. if (ret != BAD_FUNC_ARG)
  31776. return WC_TEST_RET_ENC_EC(ret);
  31777. #endif
  31778. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA256, id, idSz, pvt, hashPriv,
  31779. &hashSz);
  31780. if (ret != 0)
  31781. return WC_TEST_RET_ENC_EC(ret);
  31782. if (hashSz != 32)
  31783. return WC_TEST_RET_ENC_NC;
  31784. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA256, id, idSz, pvt, hashPub,
  31785. &hashSz);
  31786. if (ret != 0)
  31787. return WC_TEST_RET_ENC_EC(ret);
  31788. if (hashSz != 32)
  31789. return WC_TEST_RET_ENC_NC;
  31790. if (XMEMCMP(hashPriv, hashPub, hashSz) != 0)
  31791. return WC_TEST_RET_ENC_NC;
  31792. ret = wc_SetEccsiHash(priv, hashPriv, hashSz);
  31793. if (ret != 0)
  31794. return WC_TEST_RET_ENC_EC(ret);
  31795. ret = wc_SetEccsiPair(priv, ssk, pvt);
  31796. if (ret != 0)
  31797. return WC_TEST_RET_ENC_EC(ret);
  31798. ret = wc_SignEccsiHash(priv, rng, WC_HASH_TYPE_SHA256, msg, msgSz, NULL,
  31799. &sigSz);
  31800. if (ret != LENGTH_ONLY_E)
  31801. return WC_TEST_RET_ENC_EC(ret);
  31802. if (sigSz != 129)
  31803. return WC_TEST_RET_ENC_NC;
  31804. ret = wc_SignEccsiHash(priv, rng, WC_HASH_TYPE_SHA256, msg, msgSz, sig,
  31805. &sigSz);
  31806. if (ret != 0)
  31807. return WC_TEST_RET_ENC_EC(ret);
  31808. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  31809. if (ret != 0)
  31810. return WC_TEST_RET_ENC_EC(ret);
  31811. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31812. &verified);
  31813. if (ret != 0)
  31814. return WC_TEST_RET_ENC_EC(ret);
  31815. if (!verified)
  31816. return WC_TEST_RET_ENC_NC;
  31817. /* Check that changing HS results in verification failure. */
  31818. hashPub[0] ^= 0x80;
  31819. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  31820. if (ret != 0)
  31821. return WC_TEST_RET_ENC_EC(ret);
  31822. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31823. &verified);
  31824. if (ret != 0)
  31825. return WC_TEST_RET_ENC_EC(ret);
  31826. if (verified)
  31827. return WC_TEST_RET_ENC_NC;
  31828. hashPub[0] ^= 0x80;
  31829. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  31830. if (ret != 0)
  31831. return WC_TEST_RET_ENC_EC(ret);
  31832. /* Check that changing msg results in verification failure. */
  31833. msg[0] ^= 0x80;
  31834. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31835. &verified);
  31836. if (ret != 0)
  31837. return WC_TEST_RET_ENC_EC(ret);
  31838. if (verified)
  31839. return WC_TEST_RET_ENC_NC;
  31840. msg[0] ^= 0x80;
  31841. /* Check that changing signature results in verification failure. */
  31842. sig[0] ^= 0x80;
  31843. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31844. &verified);
  31845. if (ret != 0)
  31846. return WC_TEST_RET_ENC_EC(ret);
  31847. if (verified)
  31848. return WC_TEST_RET_ENC_NC;
  31849. sig[0] ^= 0x80;
  31850. /* Check that key state hasn't been invalidated. */
  31851. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31852. &verified);
  31853. if (ret != 0)
  31854. return WC_TEST_RET_ENC_EC(ret);
  31855. if (!verified)
  31856. return WC_TEST_RET_ENC_NC;
  31857. /* Check that verifying with the private key works. */
  31858. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31859. &verified);
  31860. if (ret != 0)
  31861. return WC_TEST_RET_ENC_EC(ret);
  31862. if (!verified)
  31863. return WC_TEST_RET_ENC_NC;
  31864. /* Check that the KPAK is converted from montgomery form. */
  31865. ret = eccsi_imp_exp_key_test(priv);
  31866. if (ret != 0)
  31867. return ret;
  31868. /* Check that KPAK can converted to Montgomery form again. */
  31869. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31870. &verified);
  31871. if (ret != 0)
  31872. return WC_TEST_RET_ENC_EC(ret);
  31873. if (!verified)
  31874. return WC_TEST_RET_ENC_NC;
  31875. /* Check that the KPAK is converted from montgomery form. */
  31876. ret = wc_ValidateEccsiPair(pub, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt,
  31877. &valid);
  31878. if (ret != 0)
  31879. return WC_TEST_RET_ENC_EC(ret);
  31880. if (!valid)
  31881. return WC_TEST_RET_ENC_NC;
  31882. /* Check that KPAK can converted to Montgomery form again. */
  31883. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31884. &verified);
  31885. if (ret != 0)
  31886. return WC_TEST_RET_ENC_EC(ret);
  31887. if (!verified)
  31888. return WC_TEST_RET_ENC_NC;
  31889. /* Check that the KPAK is converted from montgomery form. */
  31890. ret = eccsi_imp_exp_pubkey_test(priv, pub);
  31891. if (ret != 0)
  31892. return ret;
  31893. return 0;
  31894. }
  31895. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t eccsi_test(void)
  31896. {
  31897. wc_test_ret_t ret = 0;
  31898. WC_RNG rng;
  31899. int rng_inited = 0;
  31900. EccsiKey* priv = NULL;
  31901. EccsiKey* pub = NULL;
  31902. mp_int* ssk = NULL;
  31903. ecc_point* pvt = NULL;
  31904. priv = (EccsiKey*)XMALLOC(sizeof(EccsiKey), HEAP_HINT,
  31905. DYNAMIC_TYPE_TMP_BUFFER);
  31906. if (priv == NULL)
  31907. ret = WC_TEST_RET_ENC_NC;
  31908. else
  31909. XMEMSET(priv, 0, sizeof(*priv));
  31910. if (ret == 0) {
  31911. pub = (EccsiKey*)XMALLOC(sizeof(EccsiKey), HEAP_HINT,
  31912. DYNAMIC_TYPE_TMP_BUFFER);
  31913. if (pub == NULL)
  31914. ret = WC_TEST_RET_ENC_NC;
  31915. else
  31916. XMEMSET(pub, 0, sizeof(*pub));
  31917. }
  31918. if (ret == 0) {
  31919. ssk = (mp_int*)XMALLOC(sizeof(mp_int), HEAP_HINT,
  31920. DYNAMIC_TYPE_TMP_BUFFER);
  31921. if (ssk == NULL)
  31922. ret = WC_TEST_RET_ENC_NC;
  31923. else
  31924. XMEMSET(ssk, 0, sizeof(*ssk));
  31925. }
  31926. if (ret == 0) {
  31927. #ifndef HAVE_FIPS
  31928. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  31929. #else
  31930. ret = wc_InitRng(&rng);
  31931. #endif
  31932. if (ret != 0)
  31933. ret = WC_TEST_RET_ENC_EC(ret);
  31934. else
  31935. rng_inited = 1;
  31936. }
  31937. if (ret == 0) {
  31938. pvt = wc_ecc_new_point();
  31939. if (pvt == NULL)
  31940. ret = WC_TEST_RET_ENC_NC;
  31941. }
  31942. if (ret == 0) {
  31943. ret = mp_init(ssk);
  31944. if (ret != 0)
  31945. ret = WC_TEST_RET_ENC_EC(ret);
  31946. }
  31947. if (ret == 0) {
  31948. ret = eccsi_api_test(&rng, priv, ssk, pvt);
  31949. }
  31950. if (ret == 0) {
  31951. ret = wc_InitEccsiKey(pub, HEAP_HINT, INVALID_DEVID);
  31952. if (ret != 0)
  31953. ret = WC_TEST_RET_ENC_EC(ret);
  31954. }
  31955. if (ret == 0) {
  31956. ret = wc_InitEccsiKey(priv, HEAP_HINT, INVALID_DEVID);
  31957. if (ret != 0)
  31958. ret = WC_TEST_RET_ENC_EC(ret);
  31959. }
  31960. if (ret == 0) {
  31961. ret = eccsi_kat_verify_test(pub, pvt);
  31962. }
  31963. if (ret == 0) {
  31964. ret = eccsi_make_key_test(priv, pub, &rng, ssk, pvt);
  31965. }
  31966. if (ret == 0) {
  31967. ret = eccsi_sign_verify_test(priv, pub, &rng, ssk, pvt);
  31968. }
  31969. if (pvt != NULL)
  31970. wc_ecc_del_point(pvt);
  31971. if (rng_inited)
  31972. wc_FreeRng(&rng);
  31973. if (ssk != NULL) {
  31974. mp_free(ssk);
  31975. XFREE(ssk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31976. }
  31977. if (pub != NULL) {
  31978. wc_FreeEccsiKey(pub);
  31979. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31980. }
  31981. if (priv != NULL) {
  31982. wc_FreeEccsiKey(priv);
  31983. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31984. }
  31985. return ret;
  31986. }
  31987. #endif /* WOLFCRYPT_HAVE_ECCSI */
  31988. #ifdef WOLFCRYPT_HAVE_SAKKE
  31989. static wc_test_ret_t sakke_api_test(WC_RNG* rng, SakkeKey* key, ecc_point* rsk)
  31990. {
  31991. wc_test_ret_t ret;
  31992. byte id[1] = { 0x00 };
  31993. int valid;
  31994. byte data[256];
  31995. word32 sz;
  31996. byte auth[257];
  31997. word16 authSz;
  31998. byte ssv[256];
  31999. word16 ssvSz;
  32000. word32 len;
  32001. ret = wc_InitSakkeKey_ex(NULL, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  32002. if (ret != BAD_FUNC_ARG)
  32003. return WC_TEST_RET_ENC_EC(ret);
  32004. ret = wc_InitSakkeKey_ex(NULL, 128, ECC_SAKKE_1, HEAP_HINT, INVALID_DEVID);
  32005. if (ret != BAD_FUNC_ARG)
  32006. return WC_TEST_RET_ENC_EC(ret);
  32007. wc_FreeSakkeKey(NULL);
  32008. XMEMSET(key, 0, sizeof(*key));
  32009. wc_FreeSakkeKey(key);
  32010. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, HEAP_HINT, INVALID_DEVID);
  32011. if (ret != 0)
  32012. return WC_TEST_RET_ENC_EC(ret);
  32013. ret = wc_MakeSakkeKey(NULL, NULL);
  32014. if (ret != BAD_FUNC_ARG)
  32015. return WC_TEST_RET_ENC_EC(ret);
  32016. ret = wc_MakeSakkeKey(key, NULL);
  32017. if (ret != BAD_FUNC_ARG)
  32018. return WC_TEST_RET_ENC_EC(ret);
  32019. ret = wc_MakeSakkeKey(NULL, rng);
  32020. if (ret != BAD_FUNC_ARG)
  32021. return WC_TEST_RET_ENC_EC(ret);
  32022. ret = wc_MakeSakkePublicKey(NULL, NULL);
  32023. if (ret != BAD_FUNC_ARG)
  32024. return WC_TEST_RET_ENC_EC(ret);
  32025. ret = wc_MakeSakkePublicKey(key, NULL);
  32026. if (ret != BAD_FUNC_ARG)
  32027. return WC_TEST_RET_ENC_EC(ret);
  32028. ret = wc_MakeSakkePublicKey(NULL, rsk);
  32029. if (ret != BAD_FUNC_ARG)
  32030. return WC_TEST_RET_ENC_EC(ret);
  32031. ret = wc_MakeSakkeRsk(NULL, NULL, 1, NULL);
  32032. if (ret != BAD_FUNC_ARG)
  32033. return WC_TEST_RET_ENC_EC(ret);
  32034. ret = wc_MakeSakkeRsk(key, id, 1, NULL);
  32035. if (ret != BAD_FUNC_ARG)
  32036. return WC_TEST_RET_ENC_EC(ret);
  32037. ret = wc_MakeSakkeRsk(key, NULL, 1, rsk);
  32038. if (ret != BAD_FUNC_ARG)
  32039. return WC_TEST_RET_ENC_EC(ret);
  32040. ret = wc_MakeSakkeRsk(NULL, id, 1, rsk);
  32041. if (ret != BAD_FUNC_ARG)
  32042. return WC_TEST_RET_ENC_EC(ret);
  32043. ret = wc_ValidateSakkeRsk(NULL, NULL, 1, NULL, NULL);
  32044. if (ret != BAD_FUNC_ARG)
  32045. return WC_TEST_RET_ENC_EC(ret);
  32046. ret = wc_ValidateSakkeRsk(key, id, 1, rsk, NULL);
  32047. if (ret != BAD_FUNC_ARG)
  32048. return WC_TEST_RET_ENC_EC(ret);
  32049. ret = wc_ValidateSakkeRsk(NULL, id, 1, rsk, &valid);
  32050. if (ret != BAD_FUNC_ARG)
  32051. return WC_TEST_RET_ENC_EC(ret);
  32052. ret = wc_ExportSakkeKey(NULL, NULL, NULL);
  32053. if (ret != BAD_FUNC_ARG)
  32054. return WC_TEST_RET_ENC_EC(ret);
  32055. ret = wc_ExportSakkeKey(key, data, NULL);
  32056. if (ret != BAD_FUNC_ARG)
  32057. return WC_TEST_RET_ENC_EC(ret);
  32058. ret = wc_ExportSakkeKey(NULL, data, &sz);
  32059. if (ret != BAD_FUNC_ARG)
  32060. return WC_TEST_RET_ENC_EC(ret);
  32061. ret = wc_ImportSakkeKey(NULL, NULL, 1);
  32062. if (ret != BAD_FUNC_ARG)
  32063. return WC_TEST_RET_ENC_EC(ret);
  32064. ret = wc_ImportSakkeKey(key, NULL, 1);
  32065. if (ret != BAD_FUNC_ARG)
  32066. return WC_TEST_RET_ENC_EC(ret);
  32067. ret = wc_ImportSakkeKey(NULL, data, 1);
  32068. if (ret != BAD_FUNC_ARG)
  32069. return WC_TEST_RET_ENC_EC(ret);
  32070. ret = wc_ExportSakkePrivateKey(NULL, NULL, NULL);
  32071. if (ret != BAD_FUNC_ARG)
  32072. return WC_TEST_RET_ENC_EC(ret);
  32073. ret = wc_ExportSakkePrivateKey(key, data, NULL);
  32074. if (ret != BAD_FUNC_ARG)
  32075. return WC_TEST_RET_ENC_EC(ret);
  32076. ret = wc_ExportSakkePrivateKey(NULL, data, &sz);
  32077. if (ret != BAD_FUNC_ARG)
  32078. return WC_TEST_RET_ENC_EC(ret);
  32079. ret = wc_ImportSakkePrivateKey(NULL, NULL, 1);
  32080. if (ret != BAD_FUNC_ARG)
  32081. return WC_TEST_RET_ENC_EC(ret);
  32082. ret = wc_ImportSakkePrivateKey(key, NULL, 1);
  32083. if (ret != BAD_FUNC_ARG)
  32084. return WC_TEST_RET_ENC_EC(ret);
  32085. ret = wc_ImportSakkePrivateKey(NULL, data, 1);
  32086. if (ret != BAD_FUNC_ARG)
  32087. return WC_TEST_RET_ENC_EC(ret);
  32088. sz = sizeof(data);
  32089. ret = wc_EncodeSakkeRsk(NULL, NULL, data, NULL, 1);
  32090. if (ret != BAD_FUNC_ARG)
  32091. return WC_TEST_RET_ENC_EC(ret);
  32092. ret = wc_EncodeSakkeRsk(key, rsk, data, NULL, 1);
  32093. if (ret != BAD_FUNC_ARG)
  32094. return WC_TEST_RET_ENC_EC(ret);
  32095. ret = wc_EncodeSakkeRsk(key, NULL, data, &sz, 1);
  32096. if (ret != BAD_FUNC_ARG)
  32097. return WC_TEST_RET_ENC_EC(ret);
  32098. ret = wc_EncodeSakkeRsk(NULL, rsk, data, &sz, 1);
  32099. if (ret != BAD_FUNC_ARG)
  32100. return WC_TEST_RET_ENC_EC(ret);
  32101. ret = wc_DecodeSakkeRsk(NULL, NULL, sz, NULL);
  32102. if (ret != BAD_FUNC_ARG)
  32103. return WC_TEST_RET_ENC_EC(ret);
  32104. ret = wc_DecodeSakkeRsk(key, data, sz, NULL);
  32105. if (ret != BAD_FUNC_ARG)
  32106. return WC_TEST_RET_ENC_EC(ret);
  32107. ret = wc_DecodeSakkeRsk(key, NULL, sz, rsk);
  32108. if (ret != BAD_FUNC_ARG)
  32109. return WC_TEST_RET_ENC_EC(ret);
  32110. ret = wc_DecodeSakkeRsk(NULL, data, sz, rsk);
  32111. if (ret != BAD_FUNC_ARG)
  32112. return WC_TEST_RET_ENC_EC(ret);
  32113. ret = wc_ImportSakkeRsk(NULL, NULL, sz);
  32114. if (ret != BAD_FUNC_ARG)
  32115. return WC_TEST_RET_ENC_EC(ret);
  32116. ret = wc_ImportSakkeRsk(key, NULL, sz);
  32117. if (ret != BAD_FUNC_ARG)
  32118. return WC_TEST_RET_ENC_EC(ret);
  32119. ret = wc_ImportSakkeRsk(NULL, data, sz);
  32120. if (ret != BAD_FUNC_ARG)
  32121. return WC_TEST_RET_ENC_EC(ret);
  32122. ret = wc_ImportSakkeRsk(key, data, 1);
  32123. if (ret != BUFFER_E)
  32124. return WC_TEST_RET_ENC_EC(ret);
  32125. ret = wc_GenerateSakkeRskTable(NULL, NULL, data, NULL);
  32126. if (ret != BAD_FUNC_ARG)
  32127. return WC_TEST_RET_ENC_EC(ret);
  32128. ret = wc_GenerateSakkeRskTable(key, NULL, data, NULL);
  32129. if (ret != BAD_FUNC_ARG)
  32130. return WC_TEST_RET_ENC_EC(ret);
  32131. ret = wc_GenerateSakkeRskTable(NULL, rsk, data, NULL);
  32132. if (ret != BAD_FUNC_ARG)
  32133. return WC_TEST_RET_ENC_EC(ret);
  32134. ret = wc_GenerateSakkeRskTable(NULL, NULL, data, &len);
  32135. if (ret != BAD_FUNC_ARG)
  32136. return WC_TEST_RET_ENC_EC(ret);
  32137. ret = wc_GenerateSakkeRskTable(key, rsk, data, NULL);
  32138. if (ret != BAD_FUNC_ARG)
  32139. return WC_TEST_RET_ENC_EC(ret);
  32140. ret = wc_GenerateSakkeRskTable(key, NULL, data, &len);
  32141. if (ret != BAD_FUNC_ARG)
  32142. return WC_TEST_RET_ENC_EC(ret);
  32143. ret = wc_GenerateSakkeRskTable(NULL, rsk, data, &len);
  32144. if (ret != BAD_FUNC_ARG)
  32145. return WC_TEST_RET_ENC_EC(ret);
  32146. ret = wc_GenerateSakkeRskTable(key, rsk, NULL, &len);
  32147. if (ret != LENGTH_ONLY_E)
  32148. return WC_TEST_RET_ENC_EC(ret);
  32149. len--;
  32150. ret = wc_GenerateSakkeRskTable(key, rsk, data, &len);
  32151. if (ret != BUFFER_E)
  32152. return WC_TEST_RET_ENC_EC(ret);
  32153. ret = wc_ExportSakkePublicKey(NULL, data, NULL, 1);
  32154. if (ret != BAD_FUNC_ARG)
  32155. return WC_TEST_RET_ENC_EC(ret);
  32156. ret = wc_ExportSakkePublicKey(key, data, NULL, 1);
  32157. if (ret != BAD_FUNC_ARG)
  32158. return WC_TEST_RET_ENC_EC(ret);
  32159. ret = wc_ExportSakkePublicKey(NULL, data, &sz, 1);
  32160. if (ret != BAD_FUNC_ARG)
  32161. return WC_TEST_RET_ENC_EC(ret);
  32162. ret = wc_ImportSakkePublicKey(NULL, NULL, sz, 1);
  32163. if (ret != BAD_FUNC_ARG)
  32164. return WC_TEST_RET_ENC_EC(ret);
  32165. ret = wc_ImportSakkePublicKey(key, NULL, sz, 1);
  32166. if (ret != BAD_FUNC_ARG)
  32167. return WC_TEST_RET_ENC_EC(ret);
  32168. ret = wc_ImportSakkePublicKey(NULL, data, sz, 1);
  32169. if (ret != BAD_FUNC_ARG)
  32170. return WC_TEST_RET_ENC_EC(ret);
  32171. ret = wc_GetSakkeAuthSize(NULL, NULL);
  32172. if (ret != BAD_FUNC_ARG)
  32173. return WC_TEST_RET_ENC_EC(ret);
  32174. ret = wc_GetSakkeAuthSize(key, NULL);
  32175. if (ret != BAD_FUNC_ARG)
  32176. return WC_TEST_RET_ENC_EC(ret);
  32177. ret = wc_GetSakkeAuthSize(NULL, &authSz);
  32178. if (ret != BAD_FUNC_ARG)
  32179. return WC_TEST_RET_ENC_EC(ret);
  32180. ret = wc_MakeSakkePointI(NULL, NULL, SAKKE_ID_MAX_SIZE + 1);
  32181. if (ret != BAD_FUNC_ARG)
  32182. return WC_TEST_RET_ENC_EC(ret);
  32183. ret = wc_MakeSakkePointI(key, NULL, SAKKE_ID_MAX_SIZE + 1);
  32184. if (ret != BAD_FUNC_ARG)
  32185. return WC_TEST_RET_ENC_EC(ret);
  32186. ret = wc_MakeSakkePointI(NULL, id, 1);
  32187. if (ret != BAD_FUNC_ARG)
  32188. return WC_TEST_RET_ENC_EC(ret);
  32189. ret = wc_MakeSakkePointI(NULL, NULL, 1);
  32190. if (ret != BAD_FUNC_ARG)
  32191. return WC_TEST_RET_ENC_EC(ret);
  32192. ret = wc_MakeSakkePointI(key, id, SAKKE_ID_MAX_SIZE + 1);
  32193. if (ret != BAD_FUNC_ARG)
  32194. return WC_TEST_RET_ENC_EC(ret);
  32195. ret = wc_MakeSakkePointI(key, NULL, 1);
  32196. if (ret != BAD_FUNC_ARG)
  32197. return WC_TEST_RET_ENC_EC(ret);
  32198. ret = wc_MakeSakkePointI(NULL, id, 1);
  32199. if (ret != BAD_FUNC_ARG)
  32200. return WC_TEST_RET_ENC_EC(ret);
  32201. ret = wc_GenerateSakkePointITable(NULL, data, NULL);
  32202. if (ret != BAD_FUNC_ARG)
  32203. return WC_TEST_RET_ENC_EC(ret);
  32204. ret = wc_GenerateSakkePointITable(key, data, NULL);
  32205. if (ret != BAD_FUNC_ARG)
  32206. return WC_TEST_RET_ENC_EC(ret);
  32207. ret = wc_GenerateSakkePointITable(NULL, data, &len);
  32208. if (ret != BAD_FUNC_ARG)
  32209. return WC_TEST_RET_ENC_EC(ret);
  32210. ret = wc_GenerateSakkePointITable(key, NULL, &len);
  32211. if (ret != LENGTH_ONLY_E)
  32212. return WC_TEST_RET_ENC_EC(ret);
  32213. len--;
  32214. ret = wc_GenerateSakkePointITable(key, data, &len);
  32215. if (ret != BUFFER_E)
  32216. return WC_TEST_RET_ENC_EC(ret);
  32217. ret = wc_SetSakkePointITable(NULL, NULL, 1);
  32218. if (ret != BAD_FUNC_ARG)
  32219. return WC_TEST_RET_ENC_EC(ret);
  32220. ret = wc_SetSakkePointITable(key, NULL, 1);
  32221. if (ret != BAD_FUNC_ARG)
  32222. return WC_TEST_RET_ENC_EC(ret);
  32223. ret = wc_SetSakkePointITable(NULL, data, 1);
  32224. if (ret != BAD_FUNC_ARG)
  32225. return WC_TEST_RET_ENC_EC(ret);
  32226. ret = wc_SetSakkePointITable(key, data, 1);
  32227. if (ret != BUFFER_E)
  32228. return WC_TEST_RET_ENC_EC(ret);
  32229. ret = wc_ClearSakkePointITable(NULL);
  32230. if (ret != BAD_FUNC_ARG)
  32231. return WC_TEST_RET_ENC_EC(ret);
  32232. ret = wc_GetSakkePointI(NULL, data, NULL);
  32233. if (ret != BAD_FUNC_ARG)
  32234. return WC_TEST_RET_ENC_EC(ret);
  32235. ret = wc_GetSakkePointI(key, data, NULL);
  32236. if (ret != BAD_FUNC_ARG)
  32237. return WC_TEST_RET_ENC_EC(ret);
  32238. ret = wc_GetSakkePointI(NULL, data, &sz);
  32239. if (ret != BAD_FUNC_ARG)
  32240. return WC_TEST_RET_ENC_EC(ret);
  32241. sz = 1;
  32242. ret = wc_GetSakkePointI(key, data, &sz);
  32243. if (ret != BUFFER_E)
  32244. return WC_TEST_RET_ENC_EC(ret);
  32245. sz = 256;
  32246. ret = wc_SetSakkePointI(NULL, NULL, 1, NULL, sz);
  32247. if (ret != BAD_FUNC_ARG)
  32248. return WC_TEST_RET_ENC_EC(ret);
  32249. ret = wc_SetSakkePointI(key, NULL, 1, NULL, sz);
  32250. if (ret != BAD_FUNC_ARG)
  32251. return WC_TEST_RET_ENC_EC(ret);
  32252. ret = wc_SetSakkePointI(NULL, id, 1, NULL, sz);
  32253. if (ret != BAD_FUNC_ARG)
  32254. return WC_TEST_RET_ENC_EC(ret);
  32255. ret = wc_SetSakkePointI(NULL, NULL, 1, data, sz);
  32256. if (ret != BAD_FUNC_ARG)
  32257. return WC_TEST_RET_ENC_EC(ret);
  32258. ret = wc_SetSakkePointI(key, id, 1, NULL, sz);
  32259. if (ret != BAD_FUNC_ARG)
  32260. return WC_TEST_RET_ENC_EC(ret);
  32261. ret = wc_SetSakkePointI(key, NULL, 1, data, sz);
  32262. if (ret != BAD_FUNC_ARG)
  32263. return WC_TEST_RET_ENC_EC(ret);
  32264. ret = wc_SetSakkePointI(NULL, id, 1, data, sz);
  32265. if (ret != BAD_FUNC_ARG)
  32266. return WC_TEST_RET_ENC_EC(ret);
  32267. ret = wc_SetSakkePointI(key, id, SAKKE_ID_MAX_SIZE + 1, data, sz);
  32268. if (ret != BUFFER_E)
  32269. return WC_TEST_RET_ENC_EC(ret);
  32270. ret = wc_SetSakkePointI(key, id, 1, data, sz - 1);
  32271. if (ret != BUFFER_E)
  32272. return WC_TEST_RET_ENC_EC(ret);
  32273. ret = wc_SetSakkeIdentity(NULL, NULL, 1);
  32274. if (ret != BAD_FUNC_ARG)
  32275. return WC_TEST_RET_ENC_EC(ret);
  32276. ret = wc_SetSakkeIdentity(key, NULL, 1);
  32277. if (ret != BAD_FUNC_ARG)
  32278. return WC_TEST_RET_ENC_EC(ret);
  32279. ret = wc_SetSakkeIdentity(NULL, id, 1);
  32280. if (ret != BAD_FUNC_ARG)
  32281. return WC_TEST_RET_ENC_EC(ret);
  32282. ssvSz = sizeof(ssv);
  32283. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  32284. auth, NULL);
  32285. if (ret != BAD_FUNC_ARG)
  32286. return WC_TEST_RET_ENC_EC(ret);
  32287. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  32288. auth, NULL);
  32289. if (ret != BAD_FUNC_ARG)
  32290. return WC_TEST_RET_ENC_EC(ret);
  32291. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  32292. auth, NULL);
  32293. if (ret != BAD_FUNC_ARG)
  32294. return WC_TEST_RET_ENC_EC(ret);
  32295. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  32296. auth, &authSz);
  32297. if (ret != BAD_FUNC_ARG)
  32298. return WC_TEST_RET_ENC_EC(ret);
  32299. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  32300. auth, NULL);
  32301. if (ret != BAD_FUNC_ARG)
  32302. return WC_TEST_RET_ENC_EC(ret);
  32303. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  32304. auth, &authSz);
  32305. if (ret != BAD_FUNC_ARG)
  32306. return WC_TEST_RET_ENC_EC(ret);
  32307. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  32308. auth, &authSz);
  32309. if (ret != BAD_FUNC_ARG)
  32310. return WC_TEST_RET_ENC_EC(ret);
  32311. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  32312. auth, &authSz);
  32313. if (ret != BAD_STATE_E)
  32314. return WC_TEST_RET_ENC_EC(ret);
  32315. ret = wc_GenerateSakkeSSV(NULL, NULL, data, NULL);
  32316. if (ret != BAD_FUNC_ARG)
  32317. return WC_TEST_RET_ENC_EC(ret);
  32318. ret = wc_GenerateSakkeSSV(key, rng, data, NULL);
  32319. if (ret != BAD_FUNC_ARG)
  32320. return WC_TEST_RET_ENC_EC(ret);
  32321. ret = wc_GenerateSakkeSSV(key, NULL, data, &ssvSz);
  32322. if (ret != BAD_FUNC_ARG)
  32323. return WC_TEST_RET_ENC_EC(ret);
  32324. ret = wc_GenerateSakkeSSV(NULL, rng, data, &ssvSz);
  32325. if (ret != BAD_FUNC_ARG)
  32326. return WC_TEST_RET_ENC_EC(ret);
  32327. ret = wc_SetSakkeRsk(NULL, NULL, data, 1);
  32328. if (ret != BAD_FUNC_ARG)
  32329. return WC_TEST_RET_ENC_EC(ret);
  32330. ret = wc_SetSakkeRsk(key, NULL, data, 1);
  32331. if (ret != BAD_FUNC_ARG)
  32332. return WC_TEST_RET_ENC_EC(ret);
  32333. ret = wc_SetSakkeRsk(NULL, rsk, data, 1);
  32334. if (ret != BAD_FUNC_ARG)
  32335. return WC_TEST_RET_ENC_EC(ret);
  32336. ssvSz = sizeof(ssv);
  32337. authSz = sizeof(auth);
  32338. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz, NULL,
  32339. authSz);
  32340. if (ret != BAD_FUNC_ARG)
  32341. return WC_TEST_RET_ENC_EC(ret);
  32342. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz, NULL,
  32343. authSz);
  32344. if (ret != BAD_FUNC_ARG)
  32345. return WC_TEST_RET_ENC_EC(ret);
  32346. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz, NULL,
  32347. authSz);
  32348. if (ret != BAD_FUNC_ARG)
  32349. return WC_TEST_RET_ENC_EC(ret);
  32350. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz, auth,
  32351. authSz);
  32352. if (ret != BAD_FUNC_ARG)
  32353. return WC_TEST_RET_ENC_EC(ret);
  32354. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, NULL,
  32355. authSz);
  32356. if (ret != BAD_FUNC_ARG)
  32357. return WC_TEST_RET_ENC_EC(ret);
  32358. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz, auth,
  32359. authSz);
  32360. if (ret != BAD_FUNC_ARG)
  32361. return WC_TEST_RET_ENC_EC(ret);
  32362. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  32363. authSz);
  32364. if (ret != BAD_FUNC_ARG)
  32365. return WC_TEST_RET_ENC_EC(ret);
  32366. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  32367. authSz);
  32368. if (ret != BAD_STATE_E)
  32369. return WC_TEST_RET_ENC_EC(ret);
  32370. ret = wc_SetSakkeIdentity(key, id, 1);
  32371. if (ret != 0)
  32372. return WC_TEST_RET_ENC_EC(ret);
  32373. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  32374. authSz);
  32375. if (ret != BAD_STATE_E)
  32376. return WC_TEST_RET_ENC_EC(ret);
  32377. ret = wc_SetSakkeIdentity(key, id, 0);
  32378. if (ret != 0)
  32379. return WC_TEST_RET_ENC_EC(ret);
  32380. ret = wc_SetSakkeRsk(key, rsk, data, 1);
  32381. if (ret != 0)
  32382. return WC_TEST_RET_ENC_EC(ret);
  32383. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  32384. authSz);
  32385. if (ret != BAD_STATE_E)
  32386. return WC_TEST_RET_ENC_EC(ret);
  32387. wc_FreeSakkeKey(key);
  32388. return 0;
  32389. }
  32390. static wc_test_ret_t sakke_kat_derive_test(SakkeKey* key, ecc_point* rsk)
  32391. {
  32392. WOLFSSL_SMALL_STACK_STATIC const byte pubData[] = {
  32393. 0x59, 0x58, 0xEF, 0x1B, 0x16, 0x79, 0xBF, 0x09,
  32394. 0x9B, 0x3A, 0x03, 0x0D, 0xF2, 0x55, 0xAA, 0x6A,
  32395. 0x23, 0xC1, 0xD8, 0xF1, 0x43, 0xD4, 0xD2, 0x3F,
  32396. 0x75, 0x3E, 0x69, 0xBD, 0x27, 0xA8, 0x32, 0xF3,
  32397. 0x8C, 0xB4, 0xAD, 0x53, 0xDD, 0xEF, 0x42, 0x60,
  32398. 0xB0, 0xFE, 0x8B, 0xB4, 0x5C, 0x4C, 0x1F, 0xF5,
  32399. 0x10, 0xEF, 0xFE, 0x30, 0x03, 0x67, 0xA3, 0x7B,
  32400. 0x61, 0xF7, 0x01, 0xD9, 0x14, 0xAE, 0xF0, 0x97,
  32401. 0x24, 0x82, 0x5F, 0xA0, 0x70, 0x7D, 0x61, 0xA6,
  32402. 0xDF, 0xF4, 0xFB, 0xD7, 0x27, 0x35, 0x66, 0xCD,
  32403. 0xDE, 0x35, 0x2A, 0x0B, 0x04, 0xB7, 0xC1, 0x6A,
  32404. 0x78, 0x30, 0x9B, 0xE6, 0x40, 0x69, 0x7D, 0xE7,
  32405. 0x47, 0x61, 0x3A, 0x5F, 0xC1, 0x95, 0xE8, 0xB9,
  32406. 0xF3, 0x28, 0x85, 0x2A, 0x57, 0x9D, 0xB8, 0xF9,
  32407. 0x9B, 0x1D, 0x00, 0x34, 0x47, 0x9E, 0xA9, 0xC5,
  32408. 0x59, 0x5F, 0x47, 0xC4, 0xB2, 0xF5, 0x4F, 0xF2,
  32409. 0x15, 0x08, 0xD3, 0x75, 0x14, 0xDC, 0xF7, 0xA8,
  32410. 0xE1, 0x43, 0xA6, 0x05, 0x8C, 0x09, 0xA6, 0xBF,
  32411. 0x2C, 0x98, 0x58, 0xCA, 0x37, 0xC2, 0x58, 0x06,
  32412. 0x5A, 0xE6, 0xBF, 0x75, 0x32, 0xBC, 0x8B, 0x5B,
  32413. 0x63, 0x38, 0x38, 0x66, 0xE0, 0x75, 0x3C, 0x5A,
  32414. 0xC0, 0xE7, 0x27, 0x09, 0xF8, 0x44, 0x5F, 0x2E,
  32415. 0x61, 0x78, 0xE0, 0x65, 0x85, 0x7E, 0x0E, 0xDA,
  32416. 0x10, 0xF6, 0x82, 0x06, 0xB6, 0x35, 0x05, 0xED,
  32417. 0x87, 0xE5, 0x34, 0xFB, 0x28, 0x31, 0xFF, 0x95,
  32418. 0x7F, 0xB7, 0xDC, 0x61, 0x9D, 0xAE, 0x61, 0x30,
  32419. 0x1E, 0xEA, 0xCC, 0x2F, 0xDA, 0x36, 0x80, 0xEA,
  32420. 0x49, 0x99, 0x25, 0x8A, 0x83, 0x3C, 0xEA, 0x8F,
  32421. 0xC6, 0x7C, 0x6D, 0x19, 0x48, 0x7F, 0xB4, 0x49,
  32422. 0x05, 0x9F, 0x26, 0xCC, 0x8A, 0xAB, 0x65, 0x5A,
  32423. 0xB5, 0x8B, 0x7C, 0xC7, 0x96, 0xE2, 0x4E, 0x9A,
  32424. 0x39, 0x40, 0x95, 0x75, 0x4F, 0x5F, 0x8B, 0xAE
  32425. };
  32426. WOLFSSL_SMALL_STACK_STATIC const byte rskData[] = {
  32427. 0x93, 0xAF, 0x67, 0xE5, 0x00, 0x7B, 0xA6, 0xE6,
  32428. 0xA8, 0x0D, 0xA7, 0x93, 0xDA, 0x30, 0x0F, 0xA4,
  32429. 0xB5, 0x2D, 0x0A, 0x74, 0xE2, 0x5E, 0x6E, 0x7B,
  32430. 0x2B, 0x3D, 0x6E, 0xE9, 0xD1, 0x8A, 0x9B, 0x5C,
  32431. 0x50, 0x23, 0x59, 0x7B, 0xD8, 0x2D, 0x80, 0x62,
  32432. 0xD3, 0x40, 0x19, 0x56, 0x3B, 0xA1, 0xD2, 0x5C,
  32433. 0x0D, 0xC5, 0x6B, 0x7B, 0x97, 0x9D, 0x74, 0xAA,
  32434. 0x50, 0xF2, 0x9F, 0xBF, 0x11, 0xCC, 0x2C, 0x93,
  32435. 0xF5, 0xDF, 0xCA, 0x61, 0x5E, 0x60, 0x92, 0x79,
  32436. 0xF6, 0x17, 0x5C, 0xEA, 0xDB, 0x00, 0xB5, 0x8C,
  32437. 0x6B, 0xEE, 0x1E, 0x7A, 0x2A, 0x47, 0xC4, 0xF0,
  32438. 0xC4, 0x56, 0xF0, 0x52, 0x59, 0xA6, 0xFA, 0x94,
  32439. 0xA6, 0x34, 0xA4, 0x0D, 0xAE, 0x1D, 0xF5, 0x93,
  32440. 0xD4, 0xFE, 0xCF, 0x68, 0x8D, 0x5F, 0xC6, 0x78,
  32441. 0xBE, 0x7E, 0xFC, 0x6D, 0xF3, 0xD6, 0x83, 0x53,
  32442. 0x25, 0xB8, 0x3B, 0x2C, 0x6E, 0x69, 0x03, 0x6B,
  32443. 0x15, 0x5F, 0x0A, 0x27, 0x24, 0x10, 0x94, 0xB0,
  32444. 0x4B, 0xFB, 0x0B, 0xDF, 0xAC, 0x6C, 0x67, 0x0A,
  32445. 0x65, 0xC3, 0x25, 0xD3, 0x9A, 0x06, 0x9F, 0x03,
  32446. 0x65, 0x9D, 0x44, 0xCA, 0x27, 0xD3, 0xBE, 0x8D,
  32447. 0xF3, 0x11, 0x17, 0x2B, 0x55, 0x41, 0x60, 0x18,
  32448. 0x1C, 0xBE, 0x94, 0xA2, 0xA7, 0x83, 0x32, 0x0C,
  32449. 0xED, 0x59, 0x0B, 0xC4, 0x26, 0x44, 0x70, 0x2C,
  32450. 0xF3, 0x71, 0x27, 0x1E, 0x49, 0x6B, 0xF2, 0x0F,
  32451. 0x58, 0x8B, 0x78, 0xA1, 0xBC, 0x01, 0xEC, 0xBB,
  32452. 0x65, 0x59, 0x93, 0x4B, 0xDD, 0x2F, 0xB6, 0x5D,
  32453. 0x28, 0x84, 0x31, 0x8A, 0x33, 0xD1, 0xA4, 0x2A,
  32454. 0xDF, 0x5E, 0x33, 0xCC, 0x58, 0x00, 0x28, 0x0B,
  32455. 0x28, 0x35, 0x64, 0x97, 0xF8, 0x71, 0x35, 0xBA,
  32456. 0xB9, 0x61, 0x2A, 0x17, 0x26, 0x04, 0x24, 0x40,
  32457. 0x9A, 0xC1, 0x5F, 0xEE, 0x99, 0x6B, 0x74, 0x4C,
  32458. 0x33, 0x21, 0x51, 0x23, 0x5D, 0xEC, 0xB0, 0xF5
  32459. };
  32460. WOLFSSL_SMALL_STACK_STATIC const byte id[] = {
  32461. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  32462. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  32463. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  32464. 0x33, 0x00
  32465. };
  32466. WOLFSSL_SMALL_STACK_STATIC const byte ssv[] = {
  32467. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
  32468. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0
  32469. };
  32470. WOLFSSL_SMALL_STACK_STATIC const byte auth[] = {
  32471. 0x04,
  32472. 0x44, 0xE8, 0xAD, 0x44, 0xAB, 0x85, 0x92, 0xA6,
  32473. 0xA5, 0xA3, 0xDD, 0xCA, 0x5C, 0xF8, 0x96, 0xC7,
  32474. 0x18, 0x04, 0x36, 0x06, 0xA0, 0x1D, 0x65, 0x0D,
  32475. 0xEF, 0x37, 0xA0, 0x1F, 0x37, 0xC2, 0x28, 0xC3,
  32476. 0x32, 0xFC, 0x31, 0x73, 0x54, 0xE2, 0xC2, 0x74,
  32477. 0xD4, 0xDA, 0xF8, 0xAD, 0x00, 0x10, 0x54, 0xC7,
  32478. 0x6C, 0xE5, 0x79, 0x71, 0xC6, 0xF4, 0x48, 0x6D,
  32479. 0x57, 0x23, 0x04, 0x32, 0x61, 0xC5, 0x06, 0xEB,
  32480. 0xF5, 0xBE, 0x43, 0x8F, 0x53, 0xDE, 0x04, 0xF0,
  32481. 0x67, 0xC7, 0x76, 0xE0, 0xDD, 0x3B, 0x71, 0xA6,
  32482. 0x29, 0x01, 0x33, 0x28, 0x37, 0x25, 0xA5, 0x32,
  32483. 0xF2, 0x1A, 0xF1, 0x45, 0x12, 0x6D, 0xC1, 0xD7,
  32484. 0x77, 0xEC, 0xC2, 0x7B, 0xE5, 0x08, 0x35, 0xBD,
  32485. 0x28, 0x09, 0x8B, 0x8A, 0x73, 0xD9, 0xF8, 0x01,
  32486. 0xD8, 0x93, 0x79, 0x3A, 0x41, 0xFF, 0x5C, 0x49,
  32487. 0xB8, 0x7E, 0x79, 0xF2, 0xBE, 0x4D, 0x56, 0xCE,
  32488. 0x55, 0x7E, 0x13, 0x4A, 0xD8, 0x5B, 0xB1, 0xD4,
  32489. 0xB9, 0xCE, 0x4F, 0x8B, 0xE4, 0xB0, 0x8A, 0x12,
  32490. 0xBA, 0xBF, 0x55, 0xB1, 0xD6, 0xF1, 0xD7, 0xA6,
  32491. 0x38, 0x01, 0x9E, 0xA2, 0x8E, 0x15, 0xAB, 0x1C,
  32492. 0x9F, 0x76, 0x37, 0x5F, 0xDD, 0x12, 0x10, 0xD4,
  32493. 0xF4, 0x35, 0x1B, 0x9A, 0x00, 0x94, 0x86, 0xB7,
  32494. 0xF3, 0xED, 0x46, 0xC9, 0x65, 0xDE, 0xD2, 0xD8,
  32495. 0x0D, 0xAD, 0xE4, 0xF3, 0x8C, 0x67, 0x21, 0xD5,
  32496. 0x2C, 0x3A, 0xD1, 0x03, 0xA1, 0x0E, 0xBD, 0x29,
  32497. 0x59, 0x24, 0x8B, 0x4E, 0xF0, 0x06, 0x83, 0x6B,
  32498. 0xF0, 0x97, 0x44, 0x8E, 0x61, 0x07, 0xC9, 0xED,
  32499. 0xEE, 0x9F, 0xB7, 0x04, 0x82, 0x3D, 0xF1, 0x99,
  32500. 0xF8, 0x32, 0xC9, 0x05, 0xAE, 0x45, 0xF8, 0xA2,
  32501. 0x47, 0xA0, 0x72, 0xD8, 0xEF, 0x72, 0x9E, 0xAB,
  32502. 0xC5, 0xE2, 0x75, 0x74, 0xB0, 0x77, 0x39, 0xB3,
  32503. 0x4B, 0xE7, 0x4A, 0x53, 0x2F, 0x74, 0x7B, 0x86
  32504. };
  32505. WOLFSSL_SMALL_STACK_STATIC const byte encSsv[] = {
  32506. 0x89, 0xE0, 0xBC, 0x66, 0x1A, 0xA1, 0xE9, 0x16,
  32507. 0x38, 0xE6, 0xAC, 0xC8, 0x4E, 0x49, 0x65, 0x07
  32508. };
  32509. wc_test_ret_t ret;
  32510. int valid;
  32511. byte pubKey[sizeof(pubData) + 1];
  32512. word32 sz = sizeof(pubKey);
  32513. byte tmpSsv[sizeof(encSsv)];
  32514. byte* iTable = NULL;
  32515. word32 iTableLen;
  32516. byte* table = NULL;
  32517. word32 len;
  32518. ret = wc_ImportSakkePublicKey(key, pubData, sizeof(pubData), 0);
  32519. if (ret != 0)
  32520. return WC_TEST_RET_ENC_EC(ret);
  32521. ret = wc_DecodeSakkeRsk(key, rskData, sizeof(rskData), rsk);
  32522. if (ret != 0)
  32523. return WC_TEST_RET_ENC_EC(ret);
  32524. ret = wc_ValidateSakkeRsk(key, id, sizeof(id), rsk, &valid);
  32525. if (ret != 0)
  32526. return WC_TEST_RET_ENC_EC(ret);
  32527. if (valid != 1)
  32528. return WC_TEST_RET_ENC_NC;
  32529. ret = wc_SetSakkeRsk(key, rsk, NULL, 0);
  32530. if (ret != 0)
  32531. return WC_TEST_RET_ENC_EC(ret);
  32532. ret = wc_SetSakkeIdentity(key, id, sizeof(id));
  32533. if (ret != 0)
  32534. return WC_TEST_RET_ENC_EC(ret);
  32535. XMEMCPY(tmpSsv, encSsv, sizeof(encSsv));
  32536. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, tmpSsv, sizeof(tmpSsv),
  32537. auth, sizeof(auth));
  32538. if (ret != 0)
  32539. return WC_TEST_RET_ENC_EC(ret);
  32540. if (XMEMCMP(tmpSsv, ssv, sizeof(ssv)) != 0)
  32541. return WC_TEST_RET_ENC_NC;
  32542. ret = wc_MakeSakkePointI(key, id, sizeof(id));
  32543. if (ret != 0)
  32544. return WC_TEST_RET_ENC_EC(ret);
  32545. iTableLen = 0;
  32546. ret = wc_GenerateSakkePointITable(key, NULL, &iTableLen);
  32547. if (ret != LENGTH_ONLY_E)
  32548. return WC_TEST_RET_ENC_EC(ret);
  32549. if (iTableLen != 0) {
  32550. iTable = (byte*)XMALLOC(iTableLen, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32551. if (iTable == NULL)
  32552. return WC_TEST_RET_ENC_ERRNO;
  32553. ret = wc_GenerateSakkePointITable(key, iTable, &iTableLen);
  32554. if (ret != 0)
  32555. return WC_TEST_RET_ENC_EC(ret);
  32556. }
  32557. len = 0;
  32558. ret = wc_GenerateSakkeRskTable(key, rsk, NULL, &len);
  32559. if (ret != LENGTH_ONLY_E)
  32560. return WC_TEST_RET_ENC_EC(ret);
  32561. if (len > 0) {
  32562. table = (byte*)XMALLOC(len, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32563. if (table == NULL)
  32564. return WC_TEST_RET_ENC_ERRNO;
  32565. ret = wc_GenerateSakkeRskTable(key, rsk, table, &len);
  32566. if (ret != 0)
  32567. return WC_TEST_RET_ENC_EC(ret);
  32568. }
  32569. ret = wc_SetSakkeRsk(key, rsk, table, len);
  32570. if (ret != 0)
  32571. return WC_TEST_RET_ENC_EC(ret);
  32572. XMEMCPY(tmpSsv, encSsv, sizeof(encSsv));
  32573. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, tmpSsv, sizeof(tmpSsv),
  32574. auth, sizeof(auth));
  32575. if (ret != 0)
  32576. return WC_TEST_RET_ENC_EC(ret);
  32577. if (XMEMCMP(tmpSsv, ssv, sizeof(ssv)) != 0)
  32578. return WC_TEST_RET_ENC_NC;
  32579. /* Don't reference table that is about to be freed. */
  32580. ret = wc_ClearSakkePointITable(key);
  32581. if (ret != 0)
  32582. return WC_TEST_RET_ENC_EC(ret);
  32583. /* Dispose of tables */
  32584. if (iTable != NULL)
  32585. XFREE(iTable, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32586. if (table != NULL)
  32587. XFREE(table, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32588. /* Make sure the key public key is exportable - convert to Montgomery form
  32589. * in Validation.
  32590. */
  32591. ret = wc_ExportSakkePublicKey(key, pubKey, &sz, 1);
  32592. if (ret != 0)
  32593. return WC_TEST_RET_ENC_EC(ret);
  32594. if (sz != sizeof(pubData))
  32595. return WC_TEST_RET_ENC_NC;
  32596. if (XMEMCMP(pubKey, pubData, sizeof(pubData)) != 0)
  32597. return WC_TEST_RET_ENC_NC;
  32598. sz = sizeof(pubData) + 1;
  32599. ret = wc_ExportSakkePublicKey(key, pubKey, &sz, 0);
  32600. if (ret != 0)
  32601. return WC_TEST_RET_ENC_EC(ret);
  32602. if (sz != sizeof(pubData) + 1)
  32603. return WC_TEST_RET_ENC_NC;
  32604. if (pubKey[0] != 0x04)
  32605. return WC_TEST_RET_ENC_NC;
  32606. if (XMEMCMP(pubKey + 1, pubData, sizeof(pubData)) != 0)
  32607. return WC_TEST_RET_ENC_NC;
  32608. return 0;
  32609. }
  32610. static wc_test_ret_t sakke_kat_encapsulate_test(SakkeKey* key)
  32611. {
  32612. static const byte pubData[] = {
  32613. 0x59, 0x58, 0xEF, 0x1B, 0x16, 0x79, 0xBF, 0x09,
  32614. 0x9B, 0x3A, 0x03, 0x0D, 0xF2, 0x55, 0xAA, 0x6A,
  32615. 0x23, 0xC1, 0xD8, 0xF1, 0x43, 0xD4, 0xD2, 0x3F,
  32616. 0x75, 0x3E, 0x69, 0xBD, 0x27, 0xA8, 0x32, 0xF3,
  32617. 0x8C, 0xB4, 0xAD, 0x53, 0xDD, 0xEF, 0x42, 0x60,
  32618. 0xB0, 0xFE, 0x8B, 0xB4, 0x5C, 0x4C, 0x1F, 0xF5,
  32619. 0x10, 0xEF, 0xFE, 0x30, 0x03, 0x67, 0xA3, 0x7B,
  32620. 0x61, 0xF7, 0x01, 0xD9, 0x14, 0xAE, 0xF0, 0x97,
  32621. 0x24, 0x82, 0x5F, 0xA0, 0x70, 0x7D, 0x61, 0xA6,
  32622. 0xDF, 0xF4, 0xFB, 0xD7, 0x27, 0x35, 0x66, 0xCD,
  32623. 0xDE, 0x35, 0x2A, 0x0B, 0x04, 0xB7, 0xC1, 0x6A,
  32624. 0x78, 0x30, 0x9B, 0xE6, 0x40, 0x69, 0x7D, 0xE7,
  32625. 0x47, 0x61, 0x3A, 0x5F, 0xC1, 0x95, 0xE8, 0xB9,
  32626. 0xF3, 0x28, 0x85, 0x2A, 0x57, 0x9D, 0xB8, 0xF9,
  32627. 0x9B, 0x1D, 0x00, 0x34, 0x47, 0x9E, 0xA9, 0xC5,
  32628. 0x59, 0x5F, 0x47, 0xC4, 0xB2, 0xF5, 0x4F, 0xF2,
  32629. 0x15, 0x08, 0xD3, 0x75, 0x14, 0xDC, 0xF7, 0xA8,
  32630. 0xE1, 0x43, 0xA6, 0x05, 0x8C, 0x09, 0xA6, 0xBF,
  32631. 0x2C, 0x98, 0x58, 0xCA, 0x37, 0xC2, 0x58, 0x06,
  32632. 0x5A, 0xE6, 0xBF, 0x75, 0x32, 0xBC, 0x8B, 0x5B,
  32633. 0x63, 0x38, 0x38, 0x66, 0xE0, 0x75, 0x3C, 0x5A,
  32634. 0xC0, 0xE7, 0x27, 0x09, 0xF8, 0x44, 0x5F, 0x2E,
  32635. 0x61, 0x78, 0xE0, 0x65, 0x85, 0x7E, 0x0E, 0xDA,
  32636. 0x10, 0xF6, 0x82, 0x06, 0xB6, 0x35, 0x05, 0xED,
  32637. 0x87, 0xE5, 0x34, 0xFB, 0x28, 0x31, 0xFF, 0x95,
  32638. 0x7F, 0xB7, 0xDC, 0x61, 0x9D, 0xAE, 0x61, 0x30,
  32639. 0x1E, 0xEA, 0xCC, 0x2F, 0xDA, 0x36, 0x80, 0xEA,
  32640. 0x49, 0x99, 0x25, 0x8A, 0x83, 0x3C, 0xEA, 0x8F,
  32641. 0xC6, 0x7C, 0x6D, 0x19, 0x48, 0x7F, 0xB4, 0x49,
  32642. 0x05, 0x9F, 0x26, 0xCC, 0x8A, 0xAB, 0x65, 0x5A,
  32643. 0xB5, 0x8B, 0x7C, 0xC7, 0x96, 0xE2, 0x4E, 0x9A,
  32644. 0x39, 0x40, 0x95, 0x75, 0x4F, 0x5F, 0x8B, 0xAE
  32645. };
  32646. static const byte id[] = {
  32647. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  32648. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  32649. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  32650. 0x33, 0x00
  32651. };
  32652. static word32 idSz = sizeof(id);
  32653. byte ssv[] = {
  32654. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
  32655. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0
  32656. };
  32657. static word16 ssvSz = sizeof(ssv);
  32658. static const byte expAuth[] = {
  32659. 0x04,
  32660. 0x44, 0xE8, 0xAD, 0x44, 0xAB, 0x85, 0x92, 0xA6,
  32661. 0xA5, 0xA3, 0xDD, 0xCA, 0x5C, 0xF8, 0x96, 0xC7,
  32662. 0x18, 0x04, 0x36, 0x06, 0xA0, 0x1D, 0x65, 0x0D,
  32663. 0xEF, 0x37, 0xA0, 0x1F, 0x37, 0xC2, 0x28, 0xC3,
  32664. 0x32, 0xFC, 0x31, 0x73, 0x54, 0xE2, 0xC2, 0x74,
  32665. 0xD4, 0xDA, 0xF8, 0xAD, 0x00, 0x10, 0x54, 0xC7,
  32666. 0x6C, 0xE5, 0x79, 0x71, 0xC6, 0xF4, 0x48, 0x6D,
  32667. 0x57, 0x23, 0x04, 0x32, 0x61, 0xC5, 0x06, 0xEB,
  32668. 0xF5, 0xBE, 0x43, 0x8F, 0x53, 0xDE, 0x04, 0xF0,
  32669. 0x67, 0xC7, 0x76, 0xE0, 0xDD, 0x3B, 0x71, 0xA6,
  32670. 0x29, 0x01, 0x33, 0x28, 0x37, 0x25, 0xA5, 0x32,
  32671. 0xF2, 0x1A, 0xF1, 0x45, 0x12, 0x6D, 0xC1, 0xD7,
  32672. 0x77, 0xEC, 0xC2, 0x7B, 0xE5, 0x08, 0x35, 0xBD,
  32673. 0x28, 0x09, 0x8B, 0x8A, 0x73, 0xD9, 0xF8, 0x01,
  32674. 0xD8, 0x93, 0x79, 0x3A, 0x41, 0xFF, 0x5C, 0x49,
  32675. 0xB8, 0x7E, 0x79, 0xF2, 0xBE, 0x4D, 0x56, 0xCE,
  32676. 0x55, 0x7E, 0x13, 0x4A, 0xD8, 0x5B, 0xB1, 0xD4,
  32677. 0xB9, 0xCE, 0x4F, 0x8B, 0xE4, 0xB0, 0x8A, 0x12,
  32678. 0xBA, 0xBF, 0x55, 0xB1, 0xD6, 0xF1, 0xD7, 0xA6,
  32679. 0x38, 0x01, 0x9E, 0xA2, 0x8E, 0x15, 0xAB, 0x1C,
  32680. 0x9F, 0x76, 0x37, 0x5F, 0xDD, 0x12, 0x10, 0xD4,
  32681. 0xF4, 0x35, 0x1B, 0x9A, 0x00, 0x94, 0x86, 0xB7,
  32682. 0xF3, 0xED, 0x46, 0xC9, 0x65, 0xDE, 0xD2, 0xD8,
  32683. 0x0D, 0xAD, 0xE4, 0xF3, 0x8C, 0x67, 0x21, 0xD5,
  32684. 0x2C, 0x3A, 0xD1, 0x03, 0xA1, 0x0E, 0xBD, 0x29,
  32685. 0x59, 0x24, 0x8B, 0x4E, 0xF0, 0x06, 0x83, 0x6B,
  32686. 0xF0, 0x97, 0x44, 0x8E, 0x61, 0x07, 0xC9, 0xED,
  32687. 0xEE, 0x9F, 0xB7, 0x04, 0x82, 0x3D, 0xF1, 0x99,
  32688. 0xF8, 0x32, 0xC9, 0x05, 0xAE, 0x45, 0xF8, 0xA2,
  32689. 0x47, 0xA0, 0x72, 0xD8, 0xEF, 0x72, 0x9E, 0xAB,
  32690. 0xC5, 0xE2, 0x75, 0x74, 0xB0, 0x77, 0x39, 0xB3,
  32691. 0x4B, 0xE7, 0x4A, 0x53, 0x2F, 0x74, 0x7B, 0x86
  32692. };
  32693. static const byte encSsv[] = {
  32694. 0x89, 0xE0, 0xBC, 0x66, 0x1A, 0xA1, 0xE9, 0x16,
  32695. 0x38, 0xE6, 0xAC, 0xC8, 0x4E, 0x49, 0x65, 0x07
  32696. };
  32697. wc_test_ret_t ret;
  32698. byte auth[257];
  32699. word16 authSz = sizeof(auth);
  32700. ret = wc_ImportSakkePublicKey(key, pubData, sizeof(pubData), 0);
  32701. if (ret != 0)
  32702. return WC_TEST_RET_ENC_EC(ret);
  32703. ret = wc_SetSakkeIdentity(key, id, idSz);
  32704. if (ret != 0)
  32705. return WC_TEST_RET_ENC_EC(ret);
  32706. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  32707. auth, &authSz);
  32708. if (ret != 0)
  32709. return WC_TEST_RET_ENC_EC(ret);
  32710. if (authSz != 257)
  32711. return WC_TEST_RET_ENC_NC;
  32712. if (XMEMCMP(ssv, encSsv, ssvSz) != 0)
  32713. return WC_TEST_RET_ENC_NC;
  32714. if (XMEMCMP(auth, expAuth, authSz) != 0)
  32715. return WC_TEST_RET_ENC_NC;
  32716. return 0;
  32717. }
  32718. static wc_test_ret_t sakke_make_key_test(SakkeKey* priv, SakkeKey* pub, SakkeKey* key,
  32719. WC_RNG* rng, ecc_point* rsk)
  32720. {
  32721. wc_test_ret_t ret;
  32722. byte data[440];
  32723. byte pubData[257];
  32724. word32 sz;
  32725. char mail[] = "test@wolfssl.com";
  32726. byte* id = (byte*)mail;
  32727. word32 idSz = (word32)XSTRLEN(mail);
  32728. int valid;
  32729. ecc_point* pubKey = rsk;
  32730. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  32731. if (ret != 0)
  32732. return WC_TEST_RET_ENC_EC(ret);
  32733. ret = wc_MakeSakkeKey(priv, rng);
  32734. if (ret != 0)
  32735. return WC_TEST_RET_ENC_EC(ret);
  32736. ret = wc_ExportSakkeKey(priv, NULL, &sz);
  32737. if (ret != LENGTH_ONLY_E)
  32738. return WC_TEST_RET_ENC_EC(ret);
  32739. if (sz != 384)
  32740. return WC_TEST_RET_ENC_NC;
  32741. sz--;
  32742. ret = wc_ExportSakkeKey(priv, data, &sz);
  32743. if (ret == 0)
  32744. return WC_TEST_RET_ENC_NC;
  32745. sz++;
  32746. ret = wc_ExportSakkeKey(priv, data, &sz);
  32747. if (ret != 0)
  32748. return WC_TEST_RET_ENC_EC(ret);
  32749. if (sz != 384)
  32750. return WC_TEST_RET_ENC_NC;
  32751. ret = wc_ImportSakkeKey(key, data, sz - 1);
  32752. if (ret == 0)
  32753. return WC_TEST_RET_ENC_NC;
  32754. ret = wc_ImportSakkeKey(key, data, sz);
  32755. if (ret != 0)
  32756. return WC_TEST_RET_ENC_EC(ret);
  32757. wc_FreeSakkeKey(key);
  32758. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  32759. if (ret != 0)
  32760. return WC_TEST_RET_ENC_EC(ret);
  32761. ret = wc_ExportSakkePrivateKey(priv, NULL, &sz);
  32762. if (ret != LENGTH_ONLY_E)
  32763. return WC_TEST_RET_ENC_EC(ret);
  32764. if (sz != 128)
  32765. return WC_TEST_RET_ENC_NC;
  32766. sz--;
  32767. ret = wc_ExportSakkePrivateKey(priv, data, &sz);
  32768. if (ret == 0)
  32769. return WC_TEST_RET_ENC_NC;
  32770. sz++;
  32771. ret = wc_ExportSakkePrivateKey(priv, data, &sz);
  32772. if (ret != 0)
  32773. return WC_TEST_RET_ENC_EC(ret);
  32774. if (sz != 128)
  32775. return WC_TEST_RET_ENC_NC;
  32776. ret = wc_ImportSakkePrivateKey(key, data, sz - 1);
  32777. if (ret == 0)
  32778. return WC_TEST_RET_ENC_NC;
  32779. ret = wc_ImportSakkePrivateKey(key, data, sz);
  32780. if (ret != 0)
  32781. return WC_TEST_RET_ENC_EC(ret);
  32782. ret = wc_MakeSakkePublicKey(key, pubKey);
  32783. if (ret != 0)
  32784. return WC_TEST_RET_ENC_EC(ret);
  32785. ret = wc_ExportSakkePublicKey(priv, NULL, &sz, 1);
  32786. if (ret != LENGTH_ONLY_E)
  32787. return WC_TEST_RET_ENC_EC(ret);
  32788. if (sz != 256)
  32789. return WC_TEST_RET_ENC_NC;
  32790. sz--;
  32791. ret = wc_ExportSakkePublicKey(priv, data, &sz, 1);
  32792. if (ret == 0)
  32793. return WC_TEST_RET_ENC_NC;
  32794. sz++;
  32795. ret = wc_ExportSakkePublicKey(priv, data, &sz, 1);
  32796. if (ret != 0)
  32797. return WC_TEST_RET_ENC_EC(ret);
  32798. if (sz != 256)
  32799. return WC_TEST_RET_ENC_NC;
  32800. ret = wc_ImportSakkePublicKey(pub, data, sz - 1, 1);
  32801. if (ret == 0)
  32802. return WC_TEST_RET_ENC_NC;
  32803. ret = wc_ImportSakkePublicKey(pub, data, sz, 1);
  32804. if (ret != 0)
  32805. return WC_TEST_RET_ENC_EC(ret);
  32806. ret = wc_ExportSakkePublicKey(pub, pubData, &sz, 1);
  32807. if (ret != 0)
  32808. return WC_TEST_RET_ENC_EC(ret);
  32809. if (sz != 256)
  32810. return WC_TEST_RET_ENC_NC;
  32811. if (XMEMCMP(data, pubData, sz) != 0)
  32812. return WC_TEST_RET_ENC_NC;
  32813. ret = wc_MakeSakkeRsk(priv, id, idSz, rsk);
  32814. if (ret != 0)
  32815. return WC_TEST_RET_ENC_EC(ret);
  32816. ret = wc_ValidateSakkeRsk(priv, id, idSz, rsk, &valid);
  32817. if (ret != 0)
  32818. return WC_TEST_RET_ENC_EC(ret);
  32819. if (valid != 1)
  32820. return WC_TEST_RET_ENC_NC;
  32821. ret = wc_ValidateSakkeRsk(pub, id, idSz, rsk, &valid);
  32822. if (ret != 0)
  32823. return WC_TEST_RET_ENC_EC(ret);
  32824. if (valid != 1)
  32825. return WC_TEST_RET_ENC_NC;
  32826. sz = sizeof(data);
  32827. ret = wc_EncodeSakkeRsk(priv, rsk, data, &sz, 1);
  32828. if (ret != 0)
  32829. return WC_TEST_RET_ENC_EC(ret);
  32830. if (sz != 256)
  32831. return WC_TEST_RET_ENC_NC;
  32832. ret = wc_DecodeSakkeRsk(priv, data, sz, rsk);
  32833. if (ret != 0)
  32834. return WC_TEST_RET_ENC_EC(ret);
  32835. sz = sizeof(pubData);
  32836. ret = wc_EncodeSakkeRsk(priv, rsk, pubData, &sz, 0);
  32837. if (ret != 0)
  32838. return WC_TEST_RET_ENC_EC(ret);
  32839. if (sz != sizeof(pubData))
  32840. return WC_TEST_RET_ENC_NC;
  32841. ret = wc_DecodeSakkeRsk(priv, pubData, sz, rsk);
  32842. if (ret != 0)
  32843. return WC_TEST_RET_ENC_EC(ret);
  32844. wc_FreeSakkeKey(key);
  32845. return 0;
  32846. }
  32847. static wc_test_ret_t sakke_op_test(SakkeKey* priv, SakkeKey* pub, WC_RNG* rng,
  32848. ecc_point* rsk)
  32849. {
  32850. wc_test_ret_t ret;
  32851. byte ssv[16];
  32852. word16 ssvSz;
  32853. byte auth[257];
  32854. word16 authSz;
  32855. char mail[] = "test@wolfssl.com";
  32856. byte* id = (byte*)mail;
  32857. word32 idSz = (word32)XSTRLEN(mail);
  32858. byte pointI[256];
  32859. word32 sz;
  32860. ret = wc_GenerateSakkeSSV(pub, rng, NULL, &ssvSz);
  32861. if (ret != LENGTH_ONLY_E)
  32862. return WC_TEST_RET_ENC_EC(ret);
  32863. if (ssvSz != 16)
  32864. return WC_TEST_RET_ENC_NC;
  32865. ssvSz += 128;
  32866. ret = wc_GenerateSakkeSSV(pub, rng, ssv, &ssvSz);
  32867. if (ret == 0)
  32868. return WC_TEST_RET_ENC_NC;
  32869. ssvSz -= 128;
  32870. ret = wc_GenerateSakkeSSV(pub, rng, ssv, &ssvSz);
  32871. if (ret != 0)
  32872. return WC_TEST_RET_ENC_EC(ret);
  32873. if (ssvSz != 16)
  32874. return WC_TEST_RET_ENC_NC;
  32875. ret = wc_GetSakkeAuthSize(pub, &authSz);
  32876. if (ret != 0)
  32877. return WC_TEST_RET_ENC_EC(ret);
  32878. ret = wc_SetSakkeIdentity(pub, id, idSz);
  32879. if (ret != 0)
  32880. return WC_TEST_RET_ENC_EC(ret);
  32881. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  32882. NULL, &authSz);
  32883. if (ret != LENGTH_ONLY_E)
  32884. return WC_TEST_RET_ENC_EC(ret);
  32885. if (authSz != 257)
  32886. return WC_TEST_RET_ENC_NC;
  32887. authSz--;
  32888. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  32889. auth, &authSz);
  32890. if (ret == 0)
  32891. return WC_TEST_RET_ENC_NC;
  32892. authSz++;
  32893. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  32894. auth, &authSz);
  32895. if (ret != 0)
  32896. return WC_TEST_RET_ENC_EC(ret);
  32897. if (authSz != 257)
  32898. return WC_TEST_RET_ENC_NC;
  32899. ret = wc_GetSakkePointI(pub, NULL, &sz);
  32900. if (ret != LENGTH_ONLY_E)
  32901. return WC_TEST_RET_ENC_EC(ret);
  32902. if (sz != 256)
  32903. return WC_TEST_RET_ENC_NC;
  32904. ret = wc_GetSakkePointI(pub, pointI, &sz);
  32905. if (ret != 0)
  32906. return WC_TEST_RET_ENC_EC(ret);
  32907. if (sz != 256)
  32908. return WC_TEST_RET_ENC_NC;
  32909. /* Bogus identity - make it check and regenerate I. */
  32910. ret = wc_MakeSakkePointI(pub, ssv, ssvSz);
  32911. if (ret != 0)
  32912. return WC_TEST_RET_ENC_EC(ret);
  32913. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  32914. auth, &authSz);
  32915. if (ret != 0)
  32916. return WC_TEST_RET_ENC_EC(ret);
  32917. if (authSz != 257)
  32918. return WC_TEST_RET_ENC_NC;
  32919. ret = wc_SetSakkeRsk(priv, rsk, NULL, 0);
  32920. if (ret != 0)
  32921. return WC_TEST_RET_ENC_EC(ret);
  32922. ret = wc_SetSakkeIdentity(priv, id, idSz);
  32923. if (ret != 0)
  32924. return WC_TEST_RET_ENC_EC(ret);
  32925. authSz--;
  32926. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  32927. authSz);
  32928. if (ret == 0)
  32929. return WC_TEST_RET_ENC_NC;
  32930. authSz++;
  32931. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  32932. authSz);
  32933. if (ret != 0)
  32934. return WC_TEST_RET_ENC_EC(ret);
  32935. ssv[0] ^= 0x80;
  32936. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  32937. authSz);
  32938. if (ret != SAKKE_VERIFY_FAIL_E)
  32939. return WC_TEST_RET_ENC_EC(ret);
  32940. ssv[0] ^= 0x80;
  32941. /* Bogus identity - make it check and regenerate I. */
  32942. ret = wc_MakeSakkePointI(pub, ssv, idSz);
  32943. if (ret != 0)
  32944. return WC_TEST_RET_ENC_EC(ret);
  32945. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  32946. authSz);
  32947. if (ret != 0)
  32948. return WC_TEST_RET_ENC_EC(ret);
  32949. return 0;
  32950. }
  32951. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t sakke_test(void)
  32952. {
  32953. wc_test_ret_t ret = 0;
  32954. WC_RNG rng;
  32955. int rng_inited = 0;
  32956. SakkeKey* priv = NULL;
  32957. SakkeKey* pub = NULL;
  32958. SakkeKey* key = NULL;
  32959. ecc_point* rsk = NULL;
  32960. priv = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  32961. DYNAMIC_TYPE_TMP_BUFFER);
  32962. if (priv == NULL)
  32963. ret = WC_TEST_RET_ENC_NC;
  32964. else
  32965. XMEMSET(priv, 0, sizeof(*priv));
  32966. if (ret == 0) {
  32967. pub = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  32968. DYNAMIC_TYPE_TMP_BUFFER);
  32969. if (pub == NULL)
  32970. ret = WC_TEST_RET_ENC_NC;
  32971. else
  32972. XMEMSET(pub, 0, sizeof(*pub));
  32973. }
  32974. if (ret == 0) {
  32975. key = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  32976. DYNAMIC_TYPE_TMP_BUFFER);
  32977. if (key == NULL)
  32978. ret = WC_TEST_RET_ENC_NC;
  32979. else
  32980. XMEMSET(key, 0, sizeof(*key));
  32981. }
  32982. if (ret == 0) {
  32983. #ifndef HAVE_FIPS
  32984. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  32985. #else
  32986. ret = wc_InitRng(&rng);
  32987. #endif
  32988. if (ret == 0)
  32989. rng_inited = 1;
  32990. else
  32991. ret = WC_TEST_RET_ENC_EC(ret);
  32992. }
  32993. if (ret == 0) {
  32994. rsk = wc_ecc_new_point();
  32995. if (rsk == NULL)
  32996. ret = WC_TEST_RET_ENC_NC;
  32997. }
  32998. if (ret == 0) {
  32999. ret = wc_InitSakkeKey(pub, HEAP_HINT, INVALID_DEVID);
  33000. if (ret != 0)
  33001. ret = WC_TEST_RET_ENC_EC(ret);
  33002. }
  33003. if (ret == 0) {
  33004. ret = wc_InitSakkeKey(priv, HEAP_HINT, INVALID_DEVID);
  33005. if (ret != 0)
  33006. ret = WC_TEST_RET_ENC_EC(ret);
  33007. }
  33008. if (ret == 0) {
  33009. ret = sakke_api_test(&rng, key, rsk);
  33010. }
  33011. if (ret == 0) {
  33012. ret = sakke_kat_derive_test(pub, rsk);
  33013. }
  33014. if (ret == 0) {
  33015. ret = sakke_kat_encapsulate_test(pub);
  33016. }
  33017. if (ret == 0) {
  33018. ret = sakke_make_key_test(priv, pub, key, &rng, rsk);
  33019. }
  33020. if (ret == 0) {
  33021. ret = sakke_op_test(priv, pub, &rng, rsk);
  33022. }
  33023. if (rsk != NULL) {
  33024. wc_ecc_forcezero_point(rsk);
  33025. wc_ecc_del_point(rsk);
  33026. }
  33027. if (rng_inited)
  33028. wc_FreeRng(&rng);
  33029. if (key != NULL)
  33030. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33031. if (pub != NULL) {
  33032. wc_FreeSakkeKey(pub);
  33033. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33034. }
  33035. if (priv != NULL) {
  33036. wc_FreeSakkeKey(priv);
  33037. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33038. }
  33039. return ret;
  33040. }
  33041. #endif /* WOLFCRYPT_HAVE_SAKKE */
  33042. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  33043. typedef struct CMAC_Test_Case {
  33044. int type;
  33045. int partial;
  33046. const byte* m;
  33047. word32 mSz;
  33048. const byte* k;
  33049. word32 kSz;
  33050. const byte* t;
  33051. word32 tSz;
  33052. } CMAC_Test_Case;
  33053. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cmac_test(void)
  33054. {
  33055. #ifdef WOLFSSL_AES_128
  33056. WOLFSSL_SMALL_STACK_STATIC const byte k128[] =
  33057. {
  33058. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  33059. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  33060. };
  33061. #define KLEN_128 (sizeof(k128))
  33062. #endif
  33063. #ifdef WOLFSSL_AES_192
  33064. WOLFSSL_SMALL_STACK_STATIC const byte k192[] =
  33065. {
  33066. 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52,
  33067. 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
  33068. 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b
  33069. };
  33070. #define KLEN_192 (sizeof(k192))
  33071. #endif
  33072. #ifdef WOLFSSL_AES_256
  33073. WOLFSSL_SMALL_STACK_STATIC const byte k256[] =
  33074. {
  33075. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  33076. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  33077. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  33078. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  33079. };
  33080. #define KLEN_256 (sizeof(k256))
  33081. #endif
  33082. WOLFSSL_SMALL_STACK_STATIC const byte m[] =
  33083. {
  33084. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  33085. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  33086. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  33087. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  33088. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
  33089. 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  33090. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
  33091. 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
  33092. };
  33093. #define MLEN_0 (0)
  33094. #define MLEN_128 (128/8)
  33095. #define MLEN_320 (320/8)
  33096. #define MLEN_319 (MLEN_320 - 1)
  33097. #define MLEN_512 (512/8)
  33098. #ifdef WOLFSSL_AES_128
  33099. WOLFSSL_SMALL_STACK_STATIC const byte t128_0[] =
  33100. {
  33101. 0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28,
  33102. 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46
  33103. };
  33104. WOLFSSL_SMALL_STACK_STATIC const byte t128_128[] =
  33105. {
  33106. 0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
  33107. 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c
  33108. };
  33109. WOLFSSL_SMALL_STACK_STATIC const byte t128_319[] =
  33110. {
  33111. 0x2c, 0x17, 0x84, 0x4c, 0x93, 0x1c, 0x07, 0x95,
  33112. 0x15, 0x92, 0x73, 0x0a, 0x34, 0xd0, 0xd9, 0xd2
  33113. };
  33114. WOLFSSL_SMALL_STACK_STATIC const byte t128_320[] =
  33115. {
  33116. 0xdf, 0xa6, 0x67, 0x47, 0xde, 0x9a, 0xe6, 0x30,
  33117. 0x30, 0xca, 0x32, 0x61, 0x14, 0x97, 0xc8, 0x27
  33118. };
  33119. WOLFSSL_SMALL_STACK_STATIC const byte t128_512[] =
  33120. {
  33121. 0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92,
  33122. 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe
  33123. };
  33124. #endif
  33125. #ifdef WOLFSSL_AES_192
  33126. WOLFSSL_SMALL_STACK_STATIC const byte t192_0[] =
  33127. {
  33128. 0xd1, 0x7d, 0xdf, 0x46, 0xad, 0xaa, 0xcd, 0xe5,
  33129. 0x31, 0xca, 0xc4, 0x83, 0xde, 0x7a, 0x93, 0x67
  33130. };
  33131. WOLFSSL_SMALL_STACK_STATIC const byte t192_128[] =
  33132. {
  33133. 0x9e, 0x99, 0xa7, 0xbf, 0x31, 0xe7, 0x10, 0x90,
  33134. 0x06, 0x62, 0xf6, 0x5e, 0x61, 0x7c, 0x51, 0x84
  33135. };
  33136. WOLFSSL_SMALL_STACK_STATIC const byte t192_320[] =
  33137. {
  33138. 0x8a, 0x1d, 0xe5, 0xbe, 0x2e, 0xb3, 0x1a, 0xad,
  33139. 0x08, 0x9a, 0x82, 0xe6, 0xee, 0x90, 0x8b, 0x0e
  33140. };
  33141. WOLFSSL_SMALL_STACK_STATIC const byte t192_512[] =
  33142. {
  33143. 0xa1, 0xd5, 0xdf, 0x0e, 0xed, 0x79, 0x0f, 0x79,
  33144. 0x4d, 0x77, 0x58, 0x96, 0x59, 0xf3, 0x9a, 0x11
  33145. };
  33146. #endif
  33147. #ifdef WOLFSSL_AES_256
  33148. WOLFSSL_SMALL_STACK_STATIC const byte t256_0[] =
  33149. {
  33150. 0x02, 0x89, 0x62, 0xf6, 0x1b, 0x7b, 0xf8, 0x9e,
  33151. 0xfc, 0x6b, 0x55, 0x1f, 0x46, 0x67, 0xd9, 0x83
  33152. };
  33153. WOLFSSL_SMALL_STACK_STATIC const byte t256_128[] =
  33154. {
  33155. 0x28, 0xa7, 0x02, 0x3f, 0x45, 0x2e, 0x8f, 0x82,
  33156. 0xbd, 0x4b, 0xf2, 0x8d, 0x8c, 0x37, 0xc3, 0x5c
  33157. };
  33158. WOLFSSL_SMALL_STACK_STATIC const byte t256_320[] =
  33159. {
  33160. 0xaa, 0xf3, 0xd8, 0xf1, 0xde, 0x56, 0x40, 0xc2,
  33161. 0x32, 0xf5, 0xb1, 0x69, 0xb9, 0xc9, 0x11, 0xe6
  33162. };
  33163. WOLFSSL_SMALL_STACK_STATIC const byte t256_512[] =
  33164. {
  33165. 0xe1, 0x99, 0x21, 0x90, 0x54, 0x9f, 0x6e, 0xd5,
  33166. 0x69, 0x6a, 0x2c, 0x05, 0x6c, 0x31, 0x54, 0x10
  33167. };
  33168. #endif
  33169. const CMAC_Test_Case testCases[] =
  33170. {
  33171. #ifdef WOLFSSL_AES_128
  33172. {WC_CMAC_AES, 0, m, MLEN_0, k128, KLEN_128, t128_0, AES_BLOCK_SIZE},
  33173. {WC_CMAC_AES, 0, m, MLEN_128, k128, KLEN_128, t128_128, AES_BLOCK_SIZE},
  33174. {WC_CMAC_AES, 0, m, MLEN_320, k128, KLEN_128, t128_320, AES_BLOCK_SIZE},
  33175. {WC_CMAC_AES, 0, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  33176. {WC_CMAC_AES, 5, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  33177. #endif
  33178. #ifdef WOLFSSL_AES_192
  33179. {WC_CMAC_AES, 0, m, MLEN_0, k192, KLEN_192, t192_0, AES_BLOCK_SIZE},
  33180. {WC_CMAC_AES, 0, m, MLEN_128, k192, KLEN_192, t192_128, AES_BLOCK_SIZE},
  33181. {WC_CMAC_AES, 0, m, MLEN_320, k192, KLEN_192, t192_320, AES_BLOCK_SIZE},
  33182. {WC_CMAC_AES, 0, m, MLEN_512, k192, KLEN_192, t192_512, AES_BLOCK_SIZE},
  33183. #endif
  33184. #ifdef WOLFSSL_AES_256
  33185. {WC_CMAC_AES, 0, m, MLEN_0, k256, KLEN_256, t256_0, AES_BLOCK_SIZE},
  33186. {WC_CMAC_AES, 0, m, MLEN_128, k256, KLEN_256, t256_128, AES_BLOCK_SIZE},
  33187. {WC_CMAC_AES, 0, m, MLEN_320, k256, KLEN_256, t256_320, AES_BLOCK_SIZE},
  33188. {WC_CMAC_AES, 0, m, MLEN_512, k256, KLEN_256, t256_512, AES_BLOCK_SIZE},
  33189. #endif
  33190. #ifdef WOLFSSL_AES_128
  33191. {WC_CMAC_AES, 0, m, MLEN_319, k128, KLEN_128, t128_319, AES_BLOCK_SIZE}
  33192. #endif
  33193. };
  33194. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33195. Cmac *cmac;
  33196. #else
  33197. Cmac cmac[1];
  33198. #endif
  33199. byte tag[AES_BLOCK_SIZE];
  33200. const CMAC_Test_Case* tc;
  33201. word32 i, tagSz;
  33202. wc_test_ret_t ret;
  33203. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33204. if ((cmac = (Cmac *)XMALLOC(sizeof *cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC)) == NULL)
  33205. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  33206. #endif
  33207. for (i = 0, tc = testCases;
  33208. i < sizeof(testCases)/sizeof(CMAC_Test_Case);
  33209. i++, tc++) {
  33210. XMEMSET(tag, 0, sizeof(tag));
  33211. tagSz = AES_BLOCK_SIZE;
  33212. #if !defined(HAVE_FIPS) || \
  33213. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)
  33214. ret = wc_InitCmac_ex(cmac, tc->k, tc->kSz, tc->type, NULL, HEAP_HINT, devId);
  33215. #else
  33216. ret = wc_InitCmac(cmac, tc->k, tc->kSz, tc->type, NULL);
  33217. #endif
  33218. if (ret != 0)
  33219. {
  33220. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33221. }
  33222. if (tc->partial) {
  33223. ret = wc_CmacUpdate(cmac, tc->m,
  33224. tc->mSz/2 - tc->partial);
  33225. if (ret != 0)
  33226. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33227. ret = wc_CmacUpdate(cmac, tc->m + tc->mSz/2 - tc->partial,
  33228. tc->mSz/2 + tc->partial);
  33229. if (ret != 0)
  33230. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33231. }
  33232. else {
  33233. ret = wc_CmacUpdate(cmac, tc->m, tc->mSz);
  33234. if (ret != 0)
  33235. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33236. }
  33237. ret = wc_CmacFinal(cmac, tag, &tagSz);
  33238. if (ret != 0)
  33239. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33240. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  33241. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  33242. XMEMSET(tag, 0, sizeof(tag));
  33243. tagSz = sizeof(tag);
  33244. ret = wc_AesCmacGenerate(tag, &tagSz, tc->m, tc->mSz,
  33245. tc->k, tc->kSz);
  33246. if (ret != 0)
  33247. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33248. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  33249. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  33250. ret = wc_AesCmacVerify(tc->t, tc->tSz, tc->m, tc->mSz,
  33251. tc->k, tc->kSz);
  33252. if (ret != 0)
  33253. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  33254. }
  33255. ret = 0;
  33256. out:
  33257. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33258. if (cmac)
  33259. XFREE(cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC);
  33260. #endif
  33261. return ret;
  33262. }
  33263. #endif /* NO_AES && WOLFSSL_CMAC */
  33264. #if defined(WOLFSSL_SIPHASH)
  33265. #if WOLFSSL_SIPHASH_CROUNDS == 2 && WOLFSSL_SIPHASH_DROUNDS == 4
  33266. /* Test vectors from:
  33267. * https://github.com/veorq/SipHash/blob/master/vectors.h
  33268. */
  33269. static const unsigned char siphash_key[SIPHASH_KEY_SIZE] = {
  33270. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  33271. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  33272. };
  33273. static const unsigned char siphash_msg[64] = {
  33274. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  33275. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  33276. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  33277. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  33278. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  33279. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  33280. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  33281. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f
  33282. };
  33283. static const unsigned char siphash_r8[64][SIPHASH_MAC_SIZE_8] = {
  33284. { 0x31, 0x0e, 0x0e, 0xdd, 0x47, 0xdb, 0x6f, 0x72, },
  33285. { 0xfd, 0x67, 0xdc, 0x93, 0xc5, 0x39, 0xf8, 0x74, },
  33286. { 0x5a, 0x4f, 0xa9, 0xd9, 0x09, 0x80, 0x6c, 0x0d, },
  33287. { 0x2d, 0x7e, 0xfb, 0xd7, 0x96, 0x66, 0x67, 0x85, },
  33288. { 0xb7, 0x87, 0x71, 0x27, 0xe0, 0x94, 0x27, 0xcf, },
  33289. { 0x8d, 0xa6, 0x99, 0xcd, 0x64, 0x55, 0x76, 0x18, },
  33290. { 0xce, 0xe3, 0xfe, 0x58, 0x6e, 0x46, 0xc9, 0xcb, },
  33291. { 0x37, 0xd1, 0x01, 0x8b, 0xf5, 0x00, 0x02, 0xab, },
  33292. { 0x62, 0x24, 0x93, 0x9a, 0x79, 0xf5, 0xf5, 0x93, },
  33293. { 0xb0, 0xe4, 0xa9, 0x0b, 0xdf, 0x82, 0x00, 0x9e, },
  33294. { 0xf3, 0xb9, 0xdd, 0x94, 0xc5, 0xbb, 0x5d, 0x7a, },
  33295. { 0xa7, 0xad, 0x6b, 0x22, 0x46, 0x2f, 0xb3, 0xf4, },
  33296. { 0xfb, 0xe5, 0x0e, 0x86, 0xbc, 0x8f, 0x1e, 0x75, },
  33297. { 0x90, 0x3d, 0x84, 0xc0, 0x27, 0x56, 0xea, 0x14, },
  33298. { 0xee, 0xf2, 0x7a, 0x8e, 0x90, 0xca, 0x23, 0xf7, },
  33299. { 0xe5, 0x45, 0xbe, 0x49, 0x61, 0xca, 0x29, 0xa1, },
  33300. { 0xdb, 0x9b, 0xc2, 0x57, 0x7f, 0xcc, 0x2a, 0x3f, },
  33301. { 0x94, 0x47, 0xbe, 0x2c, 0xf5, 0xe9, 0x9a, 0x69, },
  33302. { 0x9c, 0xd3, 0x8d, 0x96, 0xf0, 0xb3, 0xc1, 0x4b, },
  33303. { 0xbd, 0x61, 0x79, 0xa7, 0x1d, 0xc9, 0x6d, 0xbb, },
  33304. { 0x98, 0xee, 0xa2, 0x1a, 0xf2, 0x5c, 0xd6, 0xbe, },
  33305. { 0xc7, 0x67, 0x3b, 0x2e, 0xb0, 0xcb, 0xf2, 0xd0, },
  33306. { 0x88, 0x3e, 0xa3, 0xe3, 0x95, 0x67, 0x53, 0x93, },
  33307. { 0xc8, 0xce, 0x5c, 0xcd, 0x8c, 0x03, 0x0c, 0xa8, },
  33308. { 0x94, 0xaf, 0x49, 0xf6, 0xc6, 0x50, 0xad, 0xb8, },
  33309. { 0xea, 0xb8, 0x85, 0x8a, 0xde, 0x92, 0xe1, 0xbc, },
  33310. { 0xf3, 0x15, 0xbb, 0x5b, 0xb8, 0x35, 0xd8, 0x17, },
  33311. { 0xad, 0xcf, 0x6b, 0x07, 0x63, 0x61, 0x2e, 0x2f, },
  33312. { 0xa5, 0xc9, 0x1d, 0xa7, 0xac, 0xaa, 0x4d, 0xde, },
  33313. { 0x71, 0x65, 0x95, 0x87, 0x66, 0x50, 0xa2, 0xa6, },
  33314. { 0x28, 0xef, 0x49, 0x5c, 0x53, 0xa3, 0x87, 0xad, },
  33315. { 0x42, 0xc3, 0x41, 0xd8, 0xfa, 0x92, 0xd8, 0x32, },
  33316. { 0xce, 0x7c, 0xf2, 0x72, 0x2f, 0x51, 0x27, 0x71, },
  33317. { 0xe3, 0x78, 0x59, 0xf9, 0x46, 0x23, 0xf3, 0xa7, },
  33318. { 0x38, 0x12, 0x05, 0xbb, 0x1a, 0xb0, 0xe0, 0x12, },
  33319. { 0xae, 0x97, 0xa1, 0x0f, 0xd4, 0x34, 0xe0, 0x15, },
  33320. { 0xb4, 0xa3, 0x15, 0x08, 0xbe, 0xff, 0x4d, 0x31, },
  33321. { 0x81, 0x39, 0x62, 0x29, 0xf0, 0x90, 0x79, 0x02, },
  33322. { 0x4d, 0x0c, 0xf4, 0x9e, 0xe5, 0xd4, 0xdc, 0xca, },
  33323. { 0x5c, 0x73, 0x33, 0x6a, 0x76, 0xd8, 0xbf, 0x9a, },
  33324. { 0xd0, 0xa7, 0x04, 0x53, 0x6b, 0xa9, 0x3e, 0x0e, },
  33325. { 0x92, 0x59, 0x58, 0xfc, 0xd6, 0x42, 0x0c, 0xad, },
  33326. { 0xa9, 0x15, 0xc2, 0x9b, 0xc8, 0x06, 0x73, 0x18, },
  33327. { 0x95, 0x2b, 0x79, 0xf3, 0xbc, 0x0a, 0xa6, 0xd4, },
  33328. { 0xf2, 0x1d, 0xf2, 0xe4, 0x1d, 0x45, 0x35, 0xf9, },
  33329. { 0x87, 0x57, 0x75, 0x19, 0x04, 0x8f, 0x53, 0xa9, },
  33330. { 0x10, 0xa5, 0x6c, 0xf5, 0xdf, 0xcd, 0x9a, 0xdb, },
  33331. { 0xeb, 0x75, 0x09, 0x5c, 0xcd, 0x98, 0x6c, 0xd0, },
  33332. { 0x51, 0xa9, 0xcb, 0x9e, 0xcb, 0xa3, 0x12, 0xe6, },
  33333. { 0x96, 0xaf, 0xad, 0xfc, 0x2c, 0xe6, 0x66, 0xc7, },
  33334. { 0x72, 0xfe, 0x52, 0x97, 0x5a, 0x43, 0x64, 0xee, },
  33335. { 0x5a, 0x16, 0x45, 0xb2, 0x76, 0xd5, 0x92, 0xa1, },
  33336. { 0xb2, 0x74, 0xcb, 0x8e, 0xbf, 0x87, 0x87, 0x0a, },
  33337. { 0x6f, 0x9b, 0xb4, 0x20, 0x3d, 0xe7, 0xb3, 0x81, },
  33338. { 0xea, 0xec, 0xb2, 0xa3, 0x0b, 0x22, 0xa8, 0x7f, },
  33339. { 0x99, 0x24, 0xa4, 0x3c, 0xc1, 0x31, 0x57, 0x24, },
  33340. { 0xbd, 0x83, 0x8d, 0x3a, 0xaf, 0xbf, 0x8d, 0xb7, },
  33341. { 0x0b, 0x1a, 0x2a, 0x32, 0x65, 0xd5, 0x1a, 0xea, },
  33342. { 0x13, 0x50, 0x79, 0xa3, 0x23, 0x1c, 0xe6, 0x60, },
  33343. { 0x93, 0x2b, 0x28, 0x46, 0xe4, 0xd7, 0x06, 0x66, },
  33344. { 0xe1, 0x91, 0x5f, 0x5c, 0xb1, 0xec, 0xa4, 0x6c, },
  33345. { 0xf3, 0x25, 0x96, 0x5c, 0xa1, 0x6d, 0x62, 0x9f, },
  33346. { 0x57, 0x5f, 0xf2, 0x8e, 0x60, 0x38, 0x1b, 0xe5, },
  33347. { 0x72, 0x45, 0x06, 0xeb, 0x4c, 0x32, 0x8a, 0x95, },
  33348. };
  33349. static const unsigned char siphash_r16[64][SIPHASH_MAC_SIZE_16] = {
  33350. { 0xa3, 0x81, 0x7f, 0x04, 0xba, 0x25, 0xa8, 0xe6,
  33351. 0x6d, 0xf6, 0x72, 0x14, 0xc7, 0x55, 0x02, 0x93, },
  33352. { 0xda, 0x87, 0xc1, 0xd8, 0x6b, 0x99, 0xaf, 0x44,
  33353. 0x34, 0x76, 0x59, 0x11, 0x9b, 0x22, 0xfc, 0x45, },
  33354. { 0x81, 0x77, 0x22, 0x8d, 0xa4, 0xa4, 0x5d, 0xc7,
  33355. 0xfc, 0xa3, 0x8b, 0xde, 0xf6, 0x0a, 0xff, 0xe4, },
  33356. { 0x9c, 0x70, 0xb6, 0x0c, 0x52, 0x67, 0xa9, 0x4e,
  33357. 0x5f, 0x33, 0xb6, 0xb0, 0x29, 0x85, 0xed, 0x51, },
  33358. { 0xf8, 0x81, 0x64, 0xc1, 0x2d, 0x9c, 0x8f, 0xaf,
  33359. 0x7d, 0x0f, 0x6e, 0x7c, 0x7b, 0xcd, 0x55, 0x79, },
  33360. { 0x13, 0x68, 0x87, 0x59, 0x80, 0x77, 0x6f, 0x88,
  33361. 0x54, 0x52, 0x7a, 0x07, 0x69, 0x0e, 0x96, 0x27, },
  33362. { 0x14, 0xee, 0xca, 0x33, 0x8b, 0x20, 0x86, 0x13,
  33363. 0x48, 0x5e, 0xa0, 0x30, 0x8f, 0xd7, 0xa1, 0x5e, },
  33364. { 0xa1, 0xf1, 0xeb, 0xbe, 0xd8, 0xdb, 0xc1, 0x53,
  33365. 0xc0, 0xb8, 0x4a, 0xa6, 0x1f, 0xf0, 0x82, 0x39, },
  33366. { 0x3b, 0x62, 0xa9, 0xba, 0x62, 0x58, 0xf5, 0x61,
  33367. 0x0f, 0x83, 0xe2, 0x64, 0xf3, 0x14, 0x97, 0xb4, },
  33368. { 0x26, 0x44, 0x99, 0x06, 0x0a, 0xd9, 0xba, 0xab,
  33369. 0xc4, 0x7f, 0x8b, 0x02, 0xbb, 0x6d, 0x71, 0xed, },
  33370. { 0x00, 0x11, 0x0d, 0xc3, 0x78, 0x14, 0x69, 0x56,
  33371. 0xc9, 0x54, 0x47, 0xd3, 0xf3, 0xd0, 0xfb, 0xba, },
  33372. { 0x01, 0x51, 0xc5, 0x68, 0x38, 0x6b, 0x66, 0x77,
  33373. 0xa2, 0xb4, 0xdc, 0x6f, 0x81, 0xe5, 0xdc, 0x18, },
  33374. { 0xd6, 0x26, 0xb2, 0x66, 0x90, 0x5e, 0xf3, 0x58,
  33375. 0x82, 0x63, 0x4d, 0xf6, 0x85, 0x32, 0xc1, 0x25, },
  33376. { 0x98, 0x69, 0xe2, 0x47, 0xe9, 0xc0, 0x8b, 0x10,
  33377. 0xd0, 0x29, 0x93, 0x4f, 0xc4, 0xb9, 0x52, 0xf7, },
  33378. { 0x31, 0xfc, 0xef, 0xac, 0x66, 0xd7, 0xde, 0x9c,
  33379. 0x7e, 0xc7, 0x48, 0x5f, 0xe4, 0x49, 0x49, 0x02, },
  33380. { 0x54, 0x93, 0xe9, 0x99, 0x33, 0xb0, 0xa8, 0x11,
  33381. 0x7e, 0x08, 0xec, 0x0f, 0x97, 0xcf, 0xc3, 0xd9, },
  33382. { 0x6e, 0xe2, 0xa4, 0xca, 0x67, 0xb0, 0x54, 0xbb,
  33383. 0xfd, 0x33, 0x15, 0xbf, 0x85, 0x23, 0x05, 0x77, },
  33384. { 0x47, 0x3d, 0x06, 0xe8, 0x73, 0x8d, 0xb8, 0x98,
  33385. 0x54, 0xc0, 0x66, 0xc4, 0x7a, 0xe4, 0x77, 0x40, },
  33386. { 0xa4, 0x26, 0xe5, 0xe4, 0x23, 0xbf, 0x48, 0x85,
  33387. 0x29, 0x4d, 0xa4, 0x81, 0xfe, 0xae, 0xf7, 0x23, },
  33388. { 0x78, 0x01, 0x77, 0x31, 0xcf, 0x65, 0xfa, 0xb0,
  33389. 0x74, 0xd5, 0x20, 0x89, 0x52, 0x51, 0x2e, 0xb1, },
  33390. { 0x9e, 0x25, 0xfc, 0x83, 0x3f, 0x22, 0x90, 0x73,
  33391. 0x3e, 0x93, 0x44, 0xa5, 0xe8, 0x38, 0x39, 0xeb, },
  33392. { 0x56, 0x8e, 0x49, 0x5a, 0xbe, 0x52, 0x5a, 0x21,
  33393. 0x8a, 0x22, 0x14, 0xcd, 0x3e, 0x07, 0x1d, 0x12, },
  33394. { 0x4a, 0x29, 0xb5, 0x45, 0x52, 0xd1, 0x6b, 0x9a,
  33395. 0x46, 0x9c, 0x10, 0x52, 0x8e, 0xff, 0x0a, 0xae, },
  33396. { 0xc9, 0xd1, 0x84, 0xdd, 0xd5, 0xa9, 0xf5, 0xe0,
  33397. 0xcf, 0x8c, 0xe2, 0x9a, 0x9a, 0xbf, 0x69, 0x1c, },
  33398. { 0x2d, 0xb4, 0x79, 0xae, 0x78, 0xbd, 0x50, 0xd8,
  33399. 0x88, 0x2a, 0x8a, 0x17, 0x8a, 0x61, 0x32, 0xad, },
  33400. { 0x8e, 0xce, 0x5f, 0x04, 0x2d, 0x5e, 0x44, 0x7b,
  33401. 0x50, 0x51, 0xb9, 0xea, 0xcb, 0x8d, 0x8f, 0x6f, },
  33402. { 0x9c, 0x0b, 0x53, 0xb4, 0xb3, 0xc3, 0x07, 0xe8,
  33403. 0x7e, 0xae, 0xe0, 0x86, 0x78, 0x14, 0x1f, 0x66, },
  33404. { 0xab, 0xf2, 0x48, 0xaf, 0x69, 0xa6, 0xea, 0xe4,
  33405. 0xbf, 0xd3, 0xeb, 0x2f, 0x12, 0x9e, 0xeb, 0x94, },
  33406. { 0x06, 0x64, 0xda, 0x16, 0x68, 0x57, 0x4b, 0x88,
  33407. 0xb9, 0x35, 0xf3, 0x02, 0x73, 0x58, 0xae, 0xf4, },
  33408. { 0xaa, 0x4b, 0x9d, 0xc4, 0xbf, 0x33, 0x7d, 0xe9,
  33409. 0x0c, 0xd4, 0xfd, 0x3c, 0x46, 0x7c, 0x6a, 0xb7, },
  33410. { 0xea, 0x5c, 0x7f, 0x47, 0x1f, 0xaf, 0x6b, 0xde,
  33411. 0x2b, 0x1a, 0xd7, 0xd4, 0x68, 0x6d, 0x22, 0x87, },
  33412. { 0x29, 0x39, 0xb0, 0x18, 0x32, 0x23, 0xfa, 0xfc,
  33413. 0x17, 0x23, 0xde, 0x4f, 0x52, 0xc4, 0x3d, 0x35, },
  33414. { 0x7c, 0x39, 0x56, 0xca, 0x5e, 0xea, 0xfc, 0x3e,
  33415. 0x36, 0x3e, 0x9d, 0x55, 0x65, 0x46, 0xeb, 0x68, },
  33416. { 0x77, 0xc6, 0x07, 0x71, 0x46, 0xf0, 0x1c, 0x32,
  33417. 0xb6, 0xb6, 0x9d, 0x5f, 0x4e, 0xa9, 0xff, 0xcf, },
  33418. { 0x37, 0xa6, 0x98, 0x6c, 0xb8, 0x84, 0x7e, 0xdf,
  33419. 0x09, 0x25, 0xf0, 0xf1, 0x30, 0x9b, 0x54, 0xde, },
  33420. { 0xa7, 0x05, 0xf0, 0xe6, 0x9d, 0xa9, 0xa8, 0xf9,
  33421. 0x07, 0x24, 0x1a, 0x2e, 0x92, 0x3c, 0x8c, 0xc8, },
  33422. { 0x3d, 0xc4, 0x7d, 0x1f, 0x29, 0xc4, 0x48, 0x46,
  33423. 0x1e, 0x9e, 0x76, 0xed, 0x90, 0x4f, 0x67, 0x11, },
  33424. { 0x0d, 0x62, 0xbf, 0x01, 0xe6, 0xfc, 0x0e, 0x1a,
  33425. 0x0d, 0x3c, 0x47, 0x51, 0xc5, 0xd3, 0x69, 0x2b, },
  33426. { 0x8c, 0x03, 0x46, 0x8b, 0xca, 0x7c, 0x66, 0x9e,
  33427. 0xe4, 0xfd, 0x5e, 0x08, 0x4b, 0xbe, 0xe7, 0xb5, },
  33428. { 0x52, 0x8a, 0x5b, 0xb9, 0x3b, 0xaf, 0x2c, 0x9c,
  33429. 0x44, 0x73, 0xcc, 0xe5, 0xd0, 0xd2, 0x2b, 0xd9, },
  33430. { 0xdf, 0x6a, 0x30, 0x1e, 0x95, 0xc9, 0x5d, 0xad,
  33431. 0x97, 0xae, 0x0c, 0xc8, 0xc6, 0x91, 0x3b, 0xd8, },
  33432. { 0x80, 0x11, 0x89, 0x90, 0x2c, 0x85, 0x7f, 0x39,
  33433. 0xe7, 0x35, 0x91, 0x28, 0x5e, 0x70, 0xb6, 0xdb, },
  33434. { 0xe6, 0x17, 0x34, 0x6a, 0xc9, 0xc2, 0x31, 0xbb,
  33435. 0x36, 0x50, 0xae, 0x34, 0xcc, 0xca, 0x0c, 0x5b, },
  33436. { 0x27, 0xd9, 0x34, 0x37, 0xef, 0xb7, 0x21, 0xaa,
  33437. 0x40, 0x18, 0x21, 0xdc, 0xec, 0x5a, 0xdf, 0x89, },
  33438. { 0x89, 0x23, 0x7d, 0x9d, 0xed, 0x9c, 0x5e, 0x78,
  33439. 0xd8, 0xb1, 0xc9, 0xb1, 0x66, 0xcc, 0x73, 0x42, },
  33440. { 0x4a, 0x6d, 0x80, 0x91, 0xbf, 0x5e, 0x7d, 0x65,
  33441. 0x11, 0x89, 0xfa, 0x94, 0xa2, 0x50, 0xb1, 0x4c, },
  33442. { 0x0e, 0x33, 0xf9, 0x60, 0x55, 0xe7, 0xae, 0x89,
  33443. 0x3f, 0xfc, 0x0e, 0x3d, 0xcf, 0x49, 0x29, 0x02, },
  33444. { 0xe6, 0x1c, 0x43, 0x2b, 0x72, 0x0b, 0x19, 0xd1,
  33445. 0x8e, 0xc8, 0xd8, 0x4b, 0xdc, 0x63, 0x15, 0x1b, },
  33446. { 0xf7, 0xe5, 0xae, 0xf5, 0x49, 0xf7, 0x82, 0xcf,
  33447. 0x37, 0x90, 0x55, 0xa6, 0x08, 0x26, 0x9b, 0x16, },
  33448. { 0x43, 0x8d, 0x03, 0x0f, 0xd0, 0xb7, 0xa5, 0x4f,
  33449. 0xa8, 0x37, 0xf2, 0xad, 0x20, 0x1a, 0x64, 0x03, },
  33450. { 0xa5, 0x90, 0xd3, 0xee, 0x4f, 0xbf, 0x04, 0xe3,
  33451. 0x24, 0x7e, 0x0d, 0x27, 0xf2, 0x86, 0x42, 0x3f, },
  33452. { 0x5f, 0xe2, 0xc1, 0xa1, 0x72, 0xfe, 0x93, 0xc4,
  33453. 0xb1, 0x5c, 0xd3, 0x7c, 0xae, 0xf9, 0xf5, 0x38, },
  33454. { 0x2c, 0x97, 0x32, 0x5c, 0xbd, 0x06, 0xb3, 0x6e,
  33455. 0xb2, 0x13, 0x3d, 0xd0, 0x8b, 0x3a, 0x01, 0x7c, },
  33456. { 0x92, 0xc8, 0x14, 0x22, 0x7a, 0x6b, 0xca, 0x94,
  33457. 0x9f, 0xf0, 0x65, 0x9f, 0x00, 0x2a, 0xd3, 0x9e, },
  33458. { 0xdc, 0xe8, 0x50, 0x11, 0x0b, 0xd8, 0x32, 0x8c,
  33459. 0xfb, 0xd5, 0x08, 0x41, 0xd6, 0x91, 0x1d, 0x87, },
  33460. { 0x67, 0xf1, 0x49, 0x84, 0xc7, 0xda, 0x79, 0x12,
  33461. 0x48, 0xe3, 0x2b, 0xb5, 0x92, 0x25, 0x83, 0xda, },
  33462. { 0x19, 0x38, 0xf2, 0xcf, 0x72, 0xd5, 0x4e, 0xe9,
  33463. 0x7e, 0x94, 0x16, 0x6f, 0xa9, 0x1d, 0x2a, 0x36, },
  33464. { 0x74, 0x48, 0x1e, 0x96, 0x46, 0xed, 0x49, 0xfe,
  33465. 0x0f, 0x62, 0x24, 0x30, 0x16, 0x04, 0x69, 0x8e, },
  33466. { 0x57, 0xfc, 0xa5, 0xde, 0x98, 0xa9, 0xd6, 0xd8,
  33467. 0x00, 0x64, 0x38, 0xd0, 0x58, 0x3d, 0x8a, 0x1d, },
  33468. { 0x9f, 0xec, 0xde, 0x1c, 0xef, 0xdc, 0x1c, 0xbe,
  33469. 0xd4, 0x76, 0x36, 0x74, 0xd9, 0x57, 0x53, 0x59, },
  33470. { 0xe3, 0x04, 0x0c, 0x00, 0xeb, 0x28, 0xf1, 0x53,
  33471. 0x66, 0xca, 0x73, 0xcb, 0xd8, 0x72, 0xe7, 0x40, },
  33472. { 0x76, 0x97, 0x00, 0x9a, 0x6a, 0x83, 0x1d, 0xfe,
  33473. 0xcc, 0xa9, 0x1c, 0x59, 0x93, 0x67, 0x0f, 0x7a, },
  33474. { 0x58, 0x53, 0x54, 0x23, 0x21, 0xf5, 0x67, 0xa0,
  33475. 0x05, 0xd5, 0x47, 0xa4, 0xf0, 0x47, 0x59, 0xbd, },
  33476. { 0x51, 0x50, 0xd1, 0x77, 0x2f, 0x50, 0x83, 0x4a,
  33477. 0x50, 0x3e, 0x06, 0x9a, 0x97, 0x3f, 0xbd, 0x7c, },
  33478. };
  33479. #endif
  33480. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t siphash_test(void)
  33481. {
  33482. wc_test_ret_t ret = 0;
  33483. int i;
  33484. #if WOLFSSL_SIPHASH_CROUNDS == 2 && WOLFSSL_SIPHASH_DROUNDS == 4
  33485. unsigned char res[SIPHASH_MAC_SIZE_16];
  33486. unsigned char tmp[SIPHASH_MAC_SIZE_8];
  33487. SipHash siphash;
  33488. for (i = 0; i < 64; i++) {
  33489. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  33490. if (ret != 0)
  33491. return WC_TEST_RET_ENC_I(i);
  33492. ret = wc_SipHashUpdate(&siphash, siphash_msg, i);
  33493. if (ret != 0)
  33494. return WC_TEST_RET_ENC_I(i);
  33495. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_8);
  33496. if (ret != 0)
  33497. return WC_TEST_RET_ENC_I(i);
  33498. if (XMEMCMP(res, siphash_r8[i], SIPHASH_MAC_SIZE_8) != 0)
  33499. return WC_TEST_RET_ENC_I(i);
  33500. ret = wc_SipHash(siphash_key, siphash_msg, i, res, SIPHASH_MAC_SIZE_8);
  33501. if (ret != 0)
  33502. return WC_TEST_RET_ENC_I(i);
  33503. if (XMEMCMP(res, siphash_r8[i], SIPHASH_MAC_SIZE_8) != 0)
  33504. return WC_TEST_RET_ENC_I(i);
  33505. }
  33506. for (i = 0; i < 64; i++) {
  33507. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_16);
  33508. if (ret != 0)
  33509. return WC_TEST_RET_ENC_I(i);
  33510. ret = wc_SipHashUpdate(&siphash, siphash_msg, i);
  33511. if (ret != 0)
  33512. return WC_TEST_RET_ENC_I(i);
  33513. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_16);
  33514. if (ret != 0)
  33515. return WC_TEST_RET_ENC_I(i);
  33516. if (XMEMCMP(res, siphash_r16[i], SIPHASH_MAC_SIZE_16) != 0)
  33517. return WC_TEST_RET_ENC_I(i);
  33518. ret = wc_SipHash(siphash_key, siphash_msg, i, res, SIPHASH_MAC_SIZE_16);
  33519. if (ret != 0)
  33520. return WC_TEST_RET_ENC_I(i);
  33521. if (XMEMCMP(res, siphash_r16[i], SIPHASH_MAC_SIZE_16) != 0)
  33522. return WC_TEST_RET_ENC_I(i);
  33523. }
  33524. #endif
  33525. /* Testing bad parameters. */
  33526. ret = wc_InitSipHash(NULL, NULL, SIPHASH_MAC_SIZE_8);
  33527. if (ret != BAD_FUNC_ARG)
  33528. return WC_TEST_RET_ENC_EC(ret);
  33529. ret = wc_InitSipHash(NULL, siphash_key, SIPHASH_MAC_SIZE_8);
  33530. if (ret != BAD_FUNC_ARG)
  33531. return WC_TEST_RET_ENC_EC(ret);
  33532. ret = wc_InitSipHash(&siphash, NULL, SIPHASH_MAC_SIZE_8);
  33533. if (ret != BAD_FUNC_ARG)
  33534. return WC_TEST_RET_ENC_EC(ret);
  33535. ret = wc_InitSipHash(&siphash, siphash_key, 7);
  33536. if (ret != BAD_FUNC_ARG)
  33537. return WC_TEST_RET_ENC_EC(ret);
  33538. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  33539. if (ret != 0)
  33540. return WC_TEST_RET_ENC_EC(ret);
  33541. ret = wc_SipHashUpdate(NULL, NULL, 0);
  33542. if (ret != BAD_FUNC_ARG)
  33543. return WC_TEST_RET_ENC_EC(ret);
  33544. ret = wc_SipHashUpdate(&siphash, NULL, 1);
  33545. if (ret != BAD_FUNC_ARG)
  33546. return WC_TEST_RET_ENC_EC(ret);
  33547. ret = wc_SipHashFinal(NULL, NULL, SIPHASH_MAC_SIZE_8);
  33548. if (ret != BAD_FUNC_ARG)
  33549. return WC_TEST_RET_ENC_EC(ret);
  33550. ret = wc_SipHashFinal(&siphash, NULL, SIPHASH_MAC_SIZE_8);
  33551. if (ret != BAD_FUNC_ARG)
  33552. return WC_TEST_RET_ENC_EC(ret);
  33553. ret = wc_SipHashFinal(NULL, res, SIPHASH_MAC_SIZE_8);
  33554. if (ret != BAD_FUNC_ARG)
  33555. return WC_TEST_RET_ENC_EC(ret);
  33556. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_16);
  33557. if (ret != BAD_FUNC_ARG)
  33558. return WC_TEST_RET_ENC_EC(ret);
  33559. ret = wc_SipHash(NULL, NULL, 0, NULL, SIPHASH_MAC_SIZE_16);
  33560. if (ret != BAD_FUNC_ARG)
  33561. return WC_TEST_RET_ENC_EC(ret);
  33562. ret = wc_SipHash(siphash_key, NULL, 0, NULL, SIPHASH_MAC_SIZE_16);
  33563. if (ret != BAD_FUNC_ARG)
  33564. return WC_TEST_RET_ENC_EC(ret);
  33565. ret = wc_SipHash(NULL, NULL, 0, res, SIPHASH_MAC_SIZE_16);
  33566. if (ret != BAD_FUNC_ARG)
  33567. return WC_TEST_RET_ENC_EC(ret);
  33568. ret = wc_SipHash(siphash_key, NULL, 0, res, 15);
  33569. if (ret != BAD_FUNC_ARG)
  33570. return WC_TEST_RET_ENC_EC(ret);
  33571. ret = wc_SipHash(siphash_key, NULL, 1, res, SIPHASH_MAC_SIZE_16);
  33572. if (ret != BAD_FUNC_ARG)
  33573. return WC_TEST_RET_ENC_EC(ret);
  33574. /* Test cache with multiple non blocksize bytes */
  33575. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  33576. if (ret != 0)
  33577. return WC_TEST_RET_ENC_EC(ret);
  33578. ret = wc_SipHashUpdate(&siphash, siphash_msg, 5);
  33579. if (ret != 0)
  33580. return WC_TEST_RET_ENC_EC(ret);
  33581. ret = wc_SipHashUpdate(&siphash, siphash_msg + 5, 4);
  33582. if (ret != 0)
  33583. return WC_TEST_RET_ENC_EC(ret);
  33584. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_8);
  33585. if (ret != 0)
  33586. return WC_TEST_RET_ENC_EC(ret);
  33587. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  33588. if (ret != 0)
  33589. return WC_TEST_RET_ENC_EC(ret);
  33590. ret = wc_SipHashUpdate(&siphash, siphash_msg, 9);
  33591. if (ret != 0)
  33592. return WC_TEST_RET_ENC_EC(ret);
  33593. ret = wc_SipHashFinal(&siphash, tmp, SIPHASH_MAC_SIZE_8);
  33594. if (ret != 0)
  33595. return WC_TEST_RET_ENC_EC(ret);
  33596. if (XMEMCMP(res, tmp, SIPHASH_MAC_SIZE_8) != 0)
  33597. return WC_TEST_RET_ENC_NC;
  33598. return 0;
  33599. }
  33600. #endif /* WOLFSSL_SIPHASH */
  33601. #ifdef HAVE_LIBZ
  33602. static const byte sample_text[] =
  33603. "Biodiesel cupidatat marfa, cliche aute put a bird on it incididunt elit\n"
  33604. "polaroid. Sunt tattooed bespoke reprehenderit. Sint twee organic id\n"
  33605. "marfa. Commodo veniam ad esse gastropub. 3 wolf moon sartorial vero,\n"
  33606. "plaid delectus biodiesel squid +1 vice. Post-ironic keffiyeh leggings\n"
  33607. "selfies cray fap hoodie, forage anim. Carles cupidatat shoreditch, VHS\n"
  33608. "small batch meggings kogi dolore food truck bespoke gastropub.\n"
  33609. "\n"
  33610. "Terry richardson adipisicing actually typewriter tumblr, twee whatever\n"
  33611. "four loko you probably haven't heard of them high life. Messenger bag\n"
  33612. "whatever tattooed deep v mlkshk. Brooklyn pinterest assumenda chillwave\n"
  33613. "et, banksy ullamco messenger bag umami pariatur direct trade forage.\n"
  33614. "Typewriter culpa try-hard, pariatur sint brooklyn meggings. Gentrify\n"
  33615. "food truck next level, tousled irony non semiotics PBR ethical anim cred\n"
  33616. "readymade. Mumblecore brunch lomo odd future, portland organic terry\n"
  33617. "richardson elit leggings adipisicing ennui raw denim banjo hella. Godard\n"
  33618. "mixtape polaroid, pork belly readymade organic cray typewriter helvetica\n"
  33619. "four loko whatever street art yr farm-to-table.\n"
  33620. "\n"
  33621. "Vinyl keytar vice tofu. Locavore you probably haven't heard of them pug\n"
  33622. "pickled, hella tonx labore truffaut DIY mlkshk elit cosby sweater sint\n"
  33623. "et mumblecore. Elit swag semiotics, reprehenderit DIY sartorial nisi ugh\n"
  33624. "nesciunt pug pork belly wayfarers selfies delectus. Ethical hoodie\n"
  33625. "seitan fingerstache kale chips. Terry richardson artisan williamsburg,\n"
  33626. "eiusmod fanny pack irony tonx ennui lo-fi incididunt tofu YOLO\n"
  33627. "readymade. 8-bit sed ethnic beard officia. Pour-over iphone DIY butcher,\n"
  33628. "ethnic art party qui letterpress nisi proident jean shorts mlkshk\n"
  33629. "locavore.\n"
  33630. "\n"
  33631. "Narwhal flexitarian letterpress, do gluten-free voluptate next level\n"
  33632. "banh mi tonx incididunt carles DIY. Odd future nulla 8-bit beard ut\n"
  33633. "cillum pickled velit, YOLO officia you probably haven't heard of them\n"
  33634. "trust fund gastropub. Nisi adipisicing tattooed, Austin mlkshk 90's\n"
  33635. "small batch american apparel. Put a bird on it cosby sweater before they\n"
  33636. "sold out pork belly kogi hella. Street art mollit sustainable polaroid,\n"
  33637. "DIY ethnic ea pug beard dreamcatcher cosby sweater magna scenester nisi.\n"
  33638. "Sed pork belly skateboard mollit, labore proident eiusmod. Sriracha\n"
  33639. "excepteur cosby sweater, anim deserunt laborum eu aliquip ethical et\n"
  33640. "neutra PBR selvage.\n"
  33641. "\n"
  33642. "Raw denim pork belly truffaut, irony plaid sustainable put a bird on it\n"
  33643. "next level jean shorts exercitation. Hashtag keytar whatever, nihil\n"
  33644. "authentic aliquip disrupt laborum. Tattooed selfies deserunt trust fund\n"
  33645. "wayfarers. 3 wolf moon synth church-key sartorial, gastropub leggings\n"
  33646. "tattooed. Labore high life commodo, meggings raw denim fingerstache pug\n"
  33647. "trust fund leggings seitan forage. Nostrud ullamco duis, reprehenderit\n"
  33648. "incididunt flannel sustainable helvetica pork belly pug banksy you\n"
  33649. "probably haven't heard of them nesciunt farm-to-table. Disrupt nostrud\n"
  33650. "mollit magna, sriracha sartorial helvetica.\n"
  33651. "\n"
  33652. "Nulla kogi reprehenderit, skateboard sustainable duis adipisicing viral\n"
  33653. "ad fanny pack salvia. Fanny pack trust fund you probably haven't heard\n"
  33654. "of them YOLO vice nihil. Keffiyeh cray lo-fi pinterest cardigan aliqua,\n"
  33655. "reprehenderit aute. Culpa tousled williamsburg, marfa lomo actually anim\n"
  33656. "skateboard. Iphone aliqua ugh, semiotics pariatur vero readymade\n"
  33657. "organic. Marfa squid nulla, in laborum disrupt laboris irure gastropub.\n"
  33658. "Veniam sunt food truck leggings, sint vinyl fap.\n"
  33659. "\n"
  33660. "Hella dolore pork belly, truffaut carles you probably haven't heard of\n"
  33661. "them PBR helvetica in sapiente. Fashion axe ugh bushwick american\n"
  33662. "apparel. Fingerstache sed iphone, jean shorts blue bottle nisi bushwick\n"
  33663. "flexitarian officia veniam plaid bespoke fap YOLO lo-fi. Blog\n"
  33664. "letterpress mumblecore, food truck id cray brooklyn cillum ad sed.\n"
  33665. "Assumenda chambray wayfarers vinyl mixtape sustainable. VHS vinyl\n"
  33666. "delectus, culpa williamsburg polaroid cliche swag church-key synth kogi\n"
  33667. "magna pop-up literally. Swag thundercats ennui shoreditch vegan\n"
  33668. "pitchfork neutra truffaut etsy, sed single-origin coffee craft beer.\n"
  33669. "\n"
  33670. "Odio letterpress brooklyn elit. Nulla single-origin coffee in occaecat\n"
  33671. "meggings. Irony meggings 8-bit, chillwave lo-fi adipisicing cred\n"
  33672. "dreamcatcher veniam. Put a bird on it irony umami, trust fund bushwick\n"
  33673. "locavore kale chips. Sriracha swag thundercats, chillwave disrupt\n"
  33674. "tousled beard mollit mustache leggings portland next level. Nihil esse\n"
  33675. "est, skateboard art party etsy thundercats sed dreamcatcher ut iphone\n"
  33676. "swag consectetur et. Irure skateboard banjo, nulla deserunt messenger\n"
  33677. "bag dolor terry richardson sapiente.\n";
  33678. static const byte sample_text_gz[] = {
  33679. 0x1F, 0x8B, 0x08, 0x08, 0xC5, 0x49, 0xB5, 0x5B, 0x00, 0x03, 0x63, 0x69, 0x70,
  33680. 0x68, 0x65, 0x72, 0x74, 0x65, 0x78, 0x74, 0x2E, 0x74, 0x78, 0x74, 0x00, 0x8D,
  33681. 0x58, 0xCB, 0x92, 0xE4, 0xB6, 0x11, 0xBC, 0xE3, 0x2B, 0xEA, 0xA6, 0x83, 0xD9,
  33682. 0x1D, 0x72, 0xF8, 0x22, 0x1F, 0xB5, 0x96, 0xA5, 0xDD, 0x90, 0xBC, 0xAB, 0xD0,
  33683. 0x28, 0x36, 0x42, 0x47, 0x90, 0x2C, 0x36, 0xA1, 0x06, 0x09, 0x0A, 0x8F, 0xEE,
  33684. 0xE1, 0xDF, 0x3B, 0x0B, 0xE0, 0x73, 0x2C, 0x4B, 0xBA, 0xCD, 0xCE, 0x80, 0x78,
  33685. 0x64, 0x65, 0x65, 0x66, 0xED, 0x3B, 0xE3, 0x5A, 0xC3, 0x81, 0x2D, 0x35, 0x69,
  33686. 0x32, 0xAD, 0x8E, 0x3A, 0xD2, 0xA0, 0x7D, 0xA7, 0x2B, 0x6A, 0xAC, 0x69, 0x7A,
  33687. 0x26, 0x9D, 0x22, 0xD3, 0x94, 0x22, 0x69, 0xAA, 0x8D, 0x6F, 0xC9, 0x8D, 0x64,
  33688. 0x22, 0x99, 0xB1, 0x31, 0xAD, 0x69, 0xD3, 0x18, 0x89, 0xAD, 0x89, 0x6A, 0x72,
  33689. 0x56, 0x7B, 0x67, 0xDA, 0x2B, 0xBD, 0xC8, 0xEF, 0xB0, 0x4D, 0x74, 0x8E, 0x5B,
  33690. 0xAA, 0x39, 0x4C, 0xEE, 0xCE, 0xE4, 0x79, 0xF2, 0xDC, 0xF3, 0xD8, 0xB2, 0x37,
  33691. 0x11, 0x8B, 0x8C, 0x2C, 0x7A, 0x32, 0x93, 0xF3, 0x37, 0x3D, 0x9A, 0x86, 0x4C,
  33692. 0xAB, 0xF2, 0xB9, 0x57, 0xFA, 0x97, 0x1B, 0x06, 0xD7, 0x3A, 0x7A, 0xF0, 0x68,
  33693. 0xF4, 0x40, 0xBA, 0x25, 0x0E, 0x81, 0xE9, 0xA6, 0x43, 0xF4, 0x6E, 0x4A, 0xF5,
  33694. 0x95, 0xFE, 0x41, 0x4F, 0x67, 0x3B, 0x1A, 0x1C, 0xEE, 0x12, 0xB4, 0x8F, 0xCE,
  33695. 0x1B, 0x6D, 0xB1, 0xDE, 0xBB, 0x4A, 0x4D, 0x56, 0x9B, 0x96, 0x5A, 0xB6, 0xDC,
  33696. 0xC4, 0x14, 0x70, 0xE5, 0xF5, 0x7D, 0xE1, 0xB7, 0x84, 0x3F, 0xFC, 0xED, 0xEF,
  33697. 0xF4, 0x30, 0x0D, 0x5F, 0xE9, 0x47, 0x17, 0xE2, 0xC5, 0x78, 0x27, 0x67, 0xDF,
  33698. 0xB9, 0xEB, 0xCC, 0xCC, 0x3D, 0x59, 0xBE, 0xDD, 0xCC, 0x78, 0x0B, 0x0A, 0x1F,
  33699. 0x74, 0xF8, 0x8C, 0x1A, 0xAF, 0x67, 0xEA, 0xF4, 0x44, 0xBD, 0x93, 0x7D, 0x2A,
  33700. 0xEA, 0x9C, 0xD7, 0x37, 0x80, 0x32, 0x9A, 0x01, 0x37, 0xD5, 0xDE, 0xCA, 0xA2,
  33701. 0x0D, 0xB9, 0xD0, 0x3B, 0xCF, 0xAD, 0x89, 0x4D, 0x5F, 0xD1, 0xE7, 0xF7, 0x2F,
  33702. 0x2A, 0x0C, 0xDA, 0x5A, 0xAA, 0x35, 0x7E, 0x41, 0xC3, 0xB2, 0x37, 0xDD, 0xDD,
  33703. 0xCD, 0x50, 0xEB, 0x2C, 0x96, 0x62, 0x3B, 0xD7, 0x52, 0xF4, 0xA9, 0xB9, 0x6F,
  33704. 0x48, 0xED, 0xEF, 0x54, 0xEA, 0x67, 0xF6, 0x7E, 0x26, 0x8F, 0x3A, 0x68, 0xDF,
  33705. 0x06, 0xBC, 0x56, 0xB7, 0x66, 0x32, 0xC1, 0x34, 0xD8, 0x88, 0x34, 0x1E, 0x88,
  33706. 0xED, 0x67, 0x8A, 0xF3, 0xC4, 0x4F, 0xC0, 0xCA, 0x9E, 0x62, 0x1A, 0x6A, 0xEB,
  33707. 0xAB, 0x02, 0xED, 0xB3, 0xD7, 0x91, 0x81, 0x8A, 0xEA, 0x5C, 0xF2, 0x64, 0xDD,
  33708. 0xDD, 0xD1, 0xEC, 0x12, 0x4D, 0xDE, 0xD5, 0xBA, 0xC6, 0x77, 0xBD, 0x06, 0xC4,
  33709. 0x5F, 0x44, 0xEA, 0x59, 0x4B, 0x5D, 0x3B, 0x8A, 0x3D, 0x0F, 0xD4, 0x9B, 0x1B,
  33710. 0x80, 0x30, 0x1D, 0x30, 0xFA, 0x8F, 0x00, 0x3F, 0xDE, 0xB0, 0x6F, 0xAD, 0x6F,
  33711. 0x6A, 0xDD, 0x6E, 0x2F, 0x6E, 0xCB, 0x3C, 0xD1, 0x83, 0x06, 0x7B, 0x0F, 0xFD,
  33712. 0xFD, 0x4A, 0xEF, 0xBC, 0x73, 0x77, 0x3B, 0x8F, 0x34, 0xA1, 0xBA, 0xEC, 0x39,
  33713. 0x80, 0x33, 0x21, 0xA4, 0x01, 0x55, 0xD7, 0xD4, 0xF4, 0xC6, 0xDA, 0x27, 0x4E,
  33714. 0x54, 0x1C, 0x2B, 0xEC, 0x37, 0xDE, 0xC3, 0x4C, 0xC9, 0x5A, 0x3D, 0x34, 0x0E,
  33715. 0xD8, 0x1C, 0x0E, 0xA2, 0x34, 0xE8, 0xC1, 0xD0, 0xA4, 0x51, 0xD5, 0x88, 0x8B,
  33716. 0xB7, 0xC6, 0xA3, 0x96, 0x40, 0x49, 0xB7, 0xBC, 0xE0, 0x7F, 0x55, 0x3F, 0xEF,
  33717. 0x6F, 0x6E, 0x92, 0x9D, 0x34, 0xFE, 0x3C, 0x5F, 0x04, 0xA5, 0x6A, 0xFF, 0x30,
  33718. 0x08, 0xC9, 0xEA, 0xF5, 0x52, 0x2B, 0xFE, 0x57, 0xFA, 0x8E, 0xC7, 0xE8, 0x4D,
  33719. 0x37, 0xAB, 0x03, 0xFA, 0x23, 0xBF, 0x46, 0x94, 0xFF, 0xC1, 0x16, 0xE0, 0xB9,
  33720. 0x14, 0x2C, 0x9E, 0x27, 0xEC, 0x98, 0x69, 0x14, 0x92, 0xF1, 0x60, 0x5C, 0x34,
  33721. 0x4D, 0xA0, 0x1F, 0xDF, 0xFD, 0x44, 0x1C, 0x7B, 0xD3, 0x80, 0x70, 0x42, 0x02,
  33722. 0x30, 0x84, 0x5B, 0xE5, 0x59, 0xB7, 0xF3, 0x80, 0xFB, 0x01, 0x33, 0xA9, 0x00,
  33723. 0x37, 0x52, 0xDC, 0xDA, 0xA7, 0x11, 0x85, 0xB7, 0x6E, 0x70, 0xE4, 0xDA, 0x96,
  33724. 0xBA, 0x84, 0x5B, 0x81, 0x43, 0x93, 0xF3, 0xD1, 0xEA, 0xB1, 0xDD, 0xB8, 0x1F,
  33725. 0xA5, 0xCC, 0xEA, 0x50, 0x66, 0x69, 0xA9, 0x8D, 0x8C, 0xA7, 0xA2, 0xF3, 0x38,
  33726. 0x26, 0x43, 0x5E, 0x3F, 0x01, 0xBE, 0x1C, 0x0F, 0x20, 0x7F, 0x75, 0xA8, 0x20,
  33727. 0x80, 0xC4, 0xC3, 0x5C, 0x8B, 0x0D, 0xD4, 0x60, 0x5E, 0xA3, 0x9E, 0xD0, 0xB4,
  33728. 0x4B, 0x4F, 0xE6, 0x13, 0x85, 0x60, 0x42, 0x96, 0xED, 0xAA, 0xDB, 0xE9, 0x99,
  33729. 0xE3, 0x07, 0x0E, 0x61, 0xB3, 0x07, 0xE3, 0xB1, 0xFA, 0xC0, 0x9B, 0xAD, 0xF6,
  33730. 0xE0, 0x26, 0x33, 0xEA, 0xEA, 0x23, 0xCD, 0x1E, 0x9D, 0xE1, 0x87, 0x4B, 0x74,
  33731. 0x97, 0x08, 0x3E, 0xA1, 0x28, 0xEA, 0xB3, 0x19, 0x67, 0x8B, 0x76, 0x9A, 0xA3,
  33732. 0xF6, 0xB9, 0xCF, 0x80, 0x65, 0x97, 0xAE, 0xF4, 0x83, 0x6B, 0xF4, 0x43, 0x20,
  33733. 0xF9, 0x0B, 0xFC, 0x9B, 0xD2, 0x4D, 0x4D, 0xA6, 0xB9, 0xA3, 0x02, 0x55, 0x79,
  33734. 0x18, 0x36, 0x19, 0x5F, 0xC9, 0xEA, 0x5A, 0x76, 0x40, 0xB9, 0xBA, 0x0E, 0x9A,
  33735. 0x44, 0xDF, 0x7C, 0xF8, 0x65, 0x61, 0x5E, 0x81, 0xAB, 0x71, 0xA1, 0x9E, 0x29,
  33736. 0x3C, 0x59, 0xCB, 0x23, 0xA4, 0xF6, 0x60, 0x1A, 0x0D, 0x5B, 0x39, 0xAE, 0xF4,
  33737. 0x6F, 0x59, 0x16, 0x9E, 0x60, 0xD8, 0x56, 0xCF, 0xEA, 0x2C, 0x4C, 0x79, 0xD3,
  33738. 0x5D, 0x51, 0x46, 0xA0, 0x4E, 0xE9, 0xD6, 0xAB, 0x91, 0x43, 0x63, 0x44, 0xD7,
  33739. 0x70, 0xB9, 0x23, 0x98, 0x4F, 0x3D, 0x03, 0x02, 0xF6, 0x81, 0x56, 0xC1, 0x58,
  33740. 0x85, 0x07, 0xA7, 0x2D, 0x2C, 0x29, 0xCA, 0x01, 0x45, 0x31, 0x51, 0x8F, 0xD4,
  33741. 0x19, 0xA1, 0x79, 0x88, 0x5A, 0xA4, 0xF5, 0xAE, 0x2D, 0x4B, 0x63, 0x4C, 0x58,
  33742. 0xFE, 0xBF, 0xAD, 0xEE, 0xA3, 0x09, 0xF8, 0xE2, 0x89, 0xBE, 0x81, 0x0E, 0x86,
  33743. 0x3A, 0xF9, 0x5B, 0xA5, 0xD8, 0xA4, 0x00, 0x75, 0x04, 0xF2, 0x23, 0xB8, 0x39,
  33744. 0x69, 0x50, 0xB7, 0xD0, 0x34, 0x63, 0x54, 0xD8, 0x61, 0xDD, 0xA5, 0x33, 0x47,
  33745. 0x85, 0x96, 0x22, 0xD0, 0x2F, 0x9F, 0x7E, 0xF8, 0x74, 0x24, 0xEA, 0x57, 0x97,
  33746. 0x5A, 0xE0, 0x00, 0xCF, 0xC1, 0x67, 0xE1, 0x41, 0xBD, 0x94, 0xA1, 0x03, 0xD3,
  33747. 0xB4, 0x08, 0x64, 0xF2, 0x17, 0x27, 0x35, 0x37, 0x53, 0xEF, 0x46, 0xCE, 0xD8,
  33748. 0xD4, 0x09, 0x52, 0xC6, 0x1E, 0xF7, 0x28, 0xDF, 0x08, 0x0F, 0xD0, 0x6F, 0x71,
  33749. 0xA6, 0xDF, 0xE4, 0x60, 0x8E, 0xC0, 0x1E, 0x78, 0x86, 0x50, 0xB0, 0x9B, 0x84,
  33750. 0x7E, 0xE8, 0x36, 0xFA, 0x95, 0xF1, 0x12, 0x51, 0xC7, 0x18, 0x96, 0xA2, 0x29,
  33751. 0xBB, 0x70, 0x02, 0xB4, 0xF9, 0xA8, 0x3D, 0x08, 0x66, 0xA9, 0xB3, 0xFC, 0x0A,
  33752. 0x94, 0x80, 0xFD, 0x78, 0xDC, 0xAB, 0x82, 0x5A, 0xD2, 0xCD, 0xC2, 0x87, 0xC6,
  33753. 0x4B, 0x07, 0xFA, 0xD1, 0xC3, 0xD9, 0x34, 0x41, 0x85, 0xF8, 0xD0, 0xB6, 0x0A,
  33754. 0x9D, 0x00, 0x91, 0x35, 0x05, 0x88, 0xC3, 0xE3, 0x9B, 0x22, 0xD2, 0xB8, 0xFD,
  33755. 0x95, 0x3E, 0x6D, 0x5D, 0x48, 0xA3, 0x68, 0xCF, 0x02, 0x42, 0x79, 0x79, 0x8A,
  33756. 0xAA, 0x01, 0xD6, 0x09, 0x14, 0x2C, 0xF4, 0x83, 0xA3, 0x80, 0x31, 0x55, 0x46,
  33757. 0x6E, 0xC5, 0xE5, 0x2F, 0x30, 0x58, 0x81, 0xA2, 0x90, 0xBE, 0x2E, 0xA1, 0xC3,
  33758. 0x0F, 0xA6, 0xF5, 0x51, 0x00, 0x39, 0xB6, 0xF2, 0x2A, 0xA3, 0x15, 0x7D, 0x8D,
  33759. 0xF5, 0x66, 0x5C, 0xD9, 0xFC, 0xCF, 0x2F, 0xBF, 0x08, 0x27, 0xE7, 0xD0, 0x03,
  33760. 0xB8, 0xD9, 0x00, 0x13, 0x3D, 0x01, 0x6B, 0xB6, 0xA8, 0xCD, 0x5B, 0x3B, 0x3E,
  33761. 0x93, 0xBF, 0xE6, 0x2E, 0xB7, 0x4A, 0xCF, 0xB3, 0x0A, 0xCE, 0x62, 0x11, 0xD6,
  33762. 0x1F, 0x68, 0x9B, 0x1D, 0x68, 0xD1, 0x8C, 0x97, 0xBD, 0xA1, 0x07, 0x67, 0x73,
  33763. 0x87, 0xE0, 0x36, 0xDA, 0x8C, 0xD2, 0xD2, 0xBB, 0x84, 0x28, 0xA9, 0xFE, 0x52,
  33764. 0x74, 0xD6, 0xB9, 0x0F, 0x0A, 0x6A, 0x2D, 0x28, 0x35, 0x34, 0x3A, 0xD3, 0xE2,
  33765. 0xCD, 0x35, 0x06, 0x7D, 0x1B, 0x35, 0x85, 0x86, 0xD1, 0x3E, 0xF2, 0x6F, 0xA1,
  33766. 0xC4, 0x55, 0xBD, 0x00, 0xD8, 0xC3, 0x5D, 0xC2, 0x1D, 0x6B, 0x6B, 0x27, 0x5B,
  33767. 0x95, 0xF3, 0xAB, 0xB5, 0xD3, 0x37, 0xF2, 0x2C, 0x9C, 0xC7, 0x5D, 0xBD, 0xF1,
  33768. 0x68, 0x1C, 0xAD, 0xF8, 0xB5, 0xE1, 0x29, 0x72, 0x7A, 0x73, 0x62, 0x55, 0x24,
  33769. 0xB9, 0x85, 0xDF, 0x7B, 0x29, 0x7D, 0xDE, 0x08, 0xF5, 0xE4, 0x44, 0xDA, 0x1A,
  33770. 0x30, 0x74, 0xDA, 0xB4, 0x9B, 0x23, 0x9A, 0x3A, 0xC1, 0x53, 0xB2, 0xA2, 0xA3,
  33771. 0x7B, 0x1F, 0xD9, 0x56, 0xD4, 0x4F, 0x9B, 0xB2, 0x1E, 0xEE, 0xB8, 0x6A, 0x4E,
  33772. 0xB5, 0xF4, 0x5A, 0xC9, 0x18, 0x27, 0x9C, 0xDE, 0x14, 0x44, 0xED, 0xC4, 0x3C,
  33773. 0x71, 0x9F, 0x5F, 0xD9, 0x37, 0xA0, 0x78, 0x34, 0x6E, 0xBC, 0xD2, 0x7B, 0x1D,
  33774. 0xFA, 0x08, 0x39, 0x5A, 0x04, 0x73, 0x15, 0xD9, 0x0A, 0x48, 0xC1, 0x2D, 0x15,
  33775. 0x4E, 0x84, 0x30, 0x45, 0x69, 0xB3, 0xE5, 0xF6, 0xAD, 0x09, 0x1E, 0xCC, 0x5F,
  33776. 0x1F, 0x06, 0xD5, 0x58, 0xAD, 0x78, 0xD7, 0x9F, 0xE5, 0xED, 0x3B, 0x09, 0xD5,
  33777. 0xA6, 0x52, 0x6F, 0x92, 0xD3, 0x3C, 0xC6, 0x1E, 0xF2, 0x93, 0x7C, 0xD3, 0x5F,
  33778. 0x70, 0x85, 0x5D, 0xF8, 0xAA, 0x9D, 0xB7, 0x7B, 0x24, 0x5A, 0xE9, 0x0A, 0x35,
  33779. 0x2F, 0xF5, 0xD9, 0x82, 0x02, 0x8A, 0x90, 0x13, 0x5B, 0xB5, 0x67, 0x9C, 0xDD,
  33780. 0xA0, 0x4E, 0x82, 0x27, 0xDA, 0x7E, 0xE8, 0x8E, 0xCD, 0xE1, 0x56, 0x71, 0x2C,
  33781. 0xE6, 0x4E, 0x1F, 0x91, 0xCD, 0x7C, 0x6A, 0xB7, 0x78, 0xD0, 0x26, 0xF3, 0x56,
  33782. 0xA9, 0xD5, 0xA1, 0xC3, 0x3B, 0x98, 0xE9, 0x28, 0x09, 0xEF, 0x50, 0x90, 0xCD,
  33783. 0xC4, 0x8E, 0x75, 0xCC, 0xAC, 0x2D, 0xC9, 0x03, 0x6D, 0xAC, 0xFE, 0xC4, 0x88,
  33784. 0x36, 0xD1, 0x3F, 0xBB, 0x1C, 0x7D, 0xB3, 0x14, 0x61, 0x2C, 0xB7, 0x54, 0x4B,
  33785. 0xDB, 0x64, 0xB6, 0x57, 0x14, 0x16, 0x8E, 0x1E, 0x6C, 0x64, 0xBB, 0x8B, 0x48,
  33786. 0x5D, 0x96, 0x9D, 0xDC, 0x80, 0xA7, 0xF7, 0x54, 0xC7, 0x46, 0x38, 0x3E, 0x44,
  33787. 0xDE, 0x7E, 0x92, 0x8D, 0x07, 0xF6, 0x07, 0x37, 0x4E, 0x16, 0x10, 0xB4, 0x7D,
  33788. 0x88, 0x66, 0x7F, 0xBB, 0xFF, 0xEA, 0x00, 0xF3, 0xFF, 0x97, 0x2C, 0xB5, 0xBE,
  33789. 0x35, 0x4B, 0x5C, 0x36, 0xEC, 0x4C, 0xBD, 0x2B, 0x7D, 0xBF, 0x46, 0xE2, 0x9C,
  33790. 0x0E, 0x8A, 0xA3, 0xEC, 0xB1, 0x0E, 0x9A, 0xDA, 0x9A, 0x9B, 0x28, 0x92, 0x10,
  33791. 0x53, 0x57, 0xEA, 0xEC, 0xA2, 0x32, 0x32, 0x20, 0x1D, 0x97, 0x5C, 0xB6, 0x84,
  33792. 0xA9, 0x93, 0x8D, 0x95, 0x11, 0xA3, 0x24, 0xA3, 0x2D, 0xC6, 0x4A, 0xEF, 0xAA,
  33793. 0x1D, 0x85, 0x2B, 0x7D, 0x28, 0xBE, 0x53, 0xCE, 0x10, 0x1F, 0xAE, 0x0E, 0x41,
  33794. 0x6C, 0x4B, 0x79, 0x12, 0xFB, 0xF7, 0x54, 0xA3, 0x96, 0x54, 0x83, 0x20, 0x96,
  33795. 0x8F, 0x28, 0xA9, 0x3F, 0x8B, 0x3D, 0xBA, 0x77, 0xDC, 0x24, 0xE1, 0xD4, 0x49,
  33796. 0x40, 0xD8, 0x78, 0x31, 0x85, 0x43, 0xF6, 0xFE, 0x5C, 0xA6, 0x8F, 0x90, 0x09,
  33797. 0xB0, 0xE7, 0xC4, 0x95, 0xB2, 0x55, 0x49, 0x97, 0x8F, 0x1C, 0x78, 0x30, 0x20,
  33798. 0xA0, 0xB4, 0xEF, 0x73, 0x56, 0x59, 0x82, 0xFD, 0xCE, 0xBA, 0x6A, 0x8F, 0x2C,
  33799. 0x8B, 0x15, 0xFD, 0xA1, 0x85, 0xA8, 0x5C, 0x0F, 0x11, 0xA5, 0x9D, 0xC2, 0x46,
  33800. 0xC6, 0x9C, 0xC9, 0x40, 0x0B, 0x58, 0x6A, 0x1C, 0x7A, 0x23, 0xF9, 0xE0, 0x95,
  33801. 0x05, 0x13, 0x58, 0x72, 0xE8, 0x9F, 0x30, 0xAC, 0xCD, 0x26, 0xD4, 0x66, 0x13,
  33802. 0xDF, 0x1E, 0x7B, 0x4F, 0x9C, 0xBE, 0x38, 0x79, 0x75, 0x92, 0xA4, 0xDA, 0x26,
  33803. 0x44, 0x55, 0x17, 0xA3, 0xE5, 0x62, 0xDA, 0xEB, 0x86, 0xEA, 0x68, 0xC7, 0xAB,
  33804. 0xFD, 0x2D, 0x43, 0x59, 0x51, 0xC0, 0x75, 0x64, 0x91, 0x01, 0x29, 0x33, 0x28,
  33805. 0xF3, 0x04, 0x83, 0x80, 0x75, 0x37, 0x75, 0x0C, 0x03, 0x7B, 0x0A, 0xAB, 0x8E,
  33806. 0x60, 0x62, 0x8B, 0x4C, 0xAF, 0x2D, 0xA3, 0x2F, 0xFE, 0xAB, 0x45, 0xCF, 0xDA,
  33807. 0xAB, 0xFA, 0xFA, 0x30, 0x3D, 0xE8, 0xA1, 0x96, 0xA5, 0x7B, 0xE2, 0x2A, 0xD0,
  33808. 0xAF, 0x59, 0xF7, 0xD0, 0x32, 0x57, 0x19, 0xBD, 0xCA, 0x9F, 0xD5, 0x1A, 0xC7,
  33809. 0xAA, 0x65, 0x4A, 0x38, 0xB2, 0x70, 0x33, 0xB7, 0x75, 0xD2, 0xCD, 0xD1, 0xF0,
  33810. 0xA8, 0x87, 0x59, 0x20, 0xA5, 0x57, 0x55, 0xB1, 0xB2, 0xC9, 0x4D, 0x97, 0x34,
  33811. 0x41, 0xF3, 0xF0, 0x30, 0xA1, 0x2C, 0x1C, 0x49, 0x3E, 0x89, 0x7D, 0x12, 0xE2,
  33812. 0xC3, 0x04, 0xC3, 0x92, 0xC0, 0xF6, 0x39, 0x10, 0x80, 0x81, 0x8F, 0x08, 0xB4,
  33813. 0xF8, 0xB9, 0x13, 0x4E, 0x2C, 0xAE, 0xB3, 0x71, 0x82, 0x63, 0x98, 0xAB, 0x5C,
  33814. 0x1C, 0x10, 0xEA, 0x66, 0xF9, 0x02, 0x3A, 0x82, 0x61, 0xD0, 0xD4, 0xAE, 0x43,
  33815. 0xD4, 0x01, 0x3E, 0x9D, 0x04, 0x14, 0xF6, 0x60, 0xD8, 0xA7, 0xD6, 0xB8, 0x53,
  33816. 0xC8, 0xDA, 0x80, 0x93, 0xA0, 0x02, 0xDD, 0xCC, 0xE2, 0xF2, 0xBB, 0xFB, 0xE0,
  33817. 0x27, 0xD7, 0x34, 0x9A, 0x71, 0x49, 0xB5, 0x4F, 0x42, 0x1F, 0xB2, 0x9D, 0x6D,
  33818. 0xAA, 0x9D, 0xD3, 0x50, 0xB5, 0x8F, 0x6A, 0x4B, 0xDF, 0x1F, 0xD5, 0x27, 0x8F,
  33819. 0x3B, 0x27, 0xCF, 0x2F, 0x8C, 0xF8, 0x9D, 0x4C, 0x52, 0xBC, 0x32, 0x0F, 0x73,
  33820. 0xD5, 0x51, 0x8E, 0x36, 0x7E, 0xAD, 0x09, 0xF0, 0x94, 0x83, 0x5F, 0x36, 0xFD,
  33821. 0x7C, 0x03, 0xED, 0xF1, 0x5E, 0x4B, 0xF7, 0xAA, 0x55, 0x5C, 0x4A, 0x14, 0x59,
  33822. 0x85, 0x38, 0x2D, 0x8C, 0xDF, 0xEC, 0x65, 0x1B, 0xB8, 0x76, 0x57, 0x96, 0x3C,
  33823. 0x86, 0xED, 0xF2, 0x7F, 0x2D, 0x28, 0x48, 0xDA, 0x49, 0x7F, 0xF7, 0x54, 0x2B,
  33824. 0xD5, 0x39, 0xD5, 0x57, 0x0A, 0x75, 0x7A, 0x3E, 0x5E, 0x5D, 0xBA, 0x4A, 0x15,
  33825. 0xFA, 0xB8, 0x31, 0x80, 0x71, 0x2C, 0xCA, 0xC4, 0x51, 0x10, 0x16, 0x5D, 0x39,
  33826. 0xEC, 0x9D, 0x07, 0xB6, 0x6A, 0x89, 0x9F, 0x9B, 0x5B, 0x6F, 0x03, 0xB0, 0x92,
  33827. 0x01, 0x38, 0x6B, 0x48, 0x99, 0x0A, 0x8F, 0x13, 0xC1, 0xA6, 0x01, 0xEA, 0xBF,
  33828. 0x6F, 0x86, 0x43, 0x51, 0xB6, 0x11, 0x00, 0x00
  33829. };
  33830. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t compress_test(void)
  33831. {
  33832. wc_test_ret_t ret = 0;
  33833. word32 dSz = sizeof(sample_text);
  33834. word32 cSz = (dSz + (word32)(dSz * 0.001) + 12);
  33835. byte *c;
  33836. byte *d;
  33837. c = (byte *)XMALLOC(cSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33838. d = (byte *)XMALLOC(dSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33839. if (c == NULL || d == NULL) {
  33840. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit);
  33841. }
  33842. /* follow calloc and initialize to 0 */
  33843. XMEMSET(c, 0, cSz);
  33844. XMEMSET(d, 0, dSz);
  33845. if ((ret = wc_Compress(c, cSz, sample_text, dSz, 0)) < 0) {
  33846. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  33847. }
  33848. cSz = (word32)ret;
  33849. if ((ret = wc_DeCompress(d, dSz, c, cSz)) != (int)dSz) {
  33850. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  33851. }
  33852. dSz = (word32)ret;
  33853. if (XMEMCMP(d, sample_text, dSz) != 0) {
  33854. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  33855. }
  33856. /* GZIP tests */
  33857. cSz = (dSz + (word32)(dSz * 0.001) + 12); /* reset cSz */
  33858. XMEMSET(c, 0, cSz);
  33859. XMEMSET(d, 0, dSz);
  33860. ret = wc_Compress_ex(c, cSz, sample_text, dSz, 0, LIBZ_WINBITS_GZIP);
  33861. if (ret < 0)
  33862. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  33863. cSz = (word32)ret;
  33864. ret = wc_DeCompress_ex(d, dSz, c, cSz, LIBZ_WINBITS_GZIP);
  33865. if (ret < 0)
  33866. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  33867. if (XMEMCMP(d, sample_text, dSz) != 0) {
  33868. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  33869. }
  33870. /* Try with gzip generated output */
  33871. XMEMSET(d, 0, dSz);
  33872. ret = wc_DeCompress_ex(d, dSz, sample_text_gz, sizeof(sample_text_gz),
  33873. LIBZ_WINBITS_GZIP);
  33874. if (ret < 0)
  33875. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit);
  33876. dSz = (word32)ret;
  33877. if (XMEMCMP(d, sample_text, dSz) != 0) {
  33878. ERROR_OUT(WC_TEST_RET_ENC_NC, exit);
  33879. }
  33880. ret = 0; /* success */
  33881. exit:
  33882. if (c) XFREE(c, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33883. if (d) XFREE(d, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33884. return ret;
  33885. }
  33886. #endif /* HAVE_LIBZ */
  33887. #ifdef HAVE_PKCS7
  33888. /* External Debugging/Testing Note:
  33889. *
  33890. * PKCS#7 test functions can output generated PKCS#7/CMS bundles for
  33891. * additional testing. To dump bundles to files DER encoded files, please
  33892. * define:
  33893. *
  33894. * #define PKCS7_OUTPUT_TEST_BUNDLES
  33895. */
  33896. /* Loads certs and keys for use with PKCS7 tests, from either files
  33897. * or buffers.
  33898. *
  33899. * rsaClientCertBuf - output buffer for RSA client cert
  33900. * rsaClientCertBufSz - IN/OUT size of output buffer, size of RSA client cert
  33901. * rsaClientPrivKeyBuf - output buffer for RSA client private key
  33902. * rsaClientPrivKeyBufSz - IN/OUT size of output buffer, size of RSA client key
  33903. *
  33904. * rsaServerCertBuf - output buffer for RSA server cert
  33905. * rsaServerCertBufSz - IN/OUT size of output buffer, size of RSA server cert
  33906. * rsaServerPrivKeyBuf - output buffer for RSA server private key
  33907. * rsaServerPrivKeyBufSz - IN/OUT size of output buffer, size of RSA server key
  33908. *
  33909. * rsaCaCertBuf - output buffer for RSA CA cert
  33910. * rsaCaCertBufSz - IN/OUT size of output buffer, size of RSA ca cert
  33911. * rsaCaPrivKeyBuf - output buffer for RSA CA private key
  33912. * rsaCaPrivKeyBufSz - IN/OUT size of output buffer, size of RSA CA key
  33913. *
  33914. * eccClientCertBuf - output buffer for ECC cert
  33915. * eccClientCertBufSz - IN/OUT size of output buffer, size of ECC cert
  33916. * eccClientPrivKeyBuf - output buffer for ECC private key
  33917. * eccClientPrivKeyBufSz - IN/OUT size of output buffer, size of ECC private key
  33918. *
  33919. * Returns 0 on success, negative on error
  33920. */
  33921. static wc_test_ret_t pkcs7_load_certs_keys(
  33922. byte* rsaClientCertBuf, word32* rsaClientCertBufSz,
  33923. byte* rsaClientPrivKeyBuf, word32* rsaClientPrivKeyBufSz,
  33924. byte* rsaServerCertBuf, word32* rsaServerCertBufSz,
  33925. byte* rsaServerPrivKeyBuf, word32* rsaServerPrivKeyBufSz,
  33926. byte* rsaCaCertBuf, word32* rsaCaCertBufSz,
  33927. byte* rsaCaPrivKeyBuf, word32* rsaCaPrivKeyBufSz,
  33928. byte* eccClientCertBuf, word32* eccClientCertBufSz,
  33929. byte* eccClientPrivKeyBuf, word32* eccClientPrivKeyBufSz)
  33930. {
  33931. #ifndef NO_FILESYSTEM
  33932. XFILE certFile;
  33933. XFILE keyFile;
  33934. (void)certFile;
  33935. (void)keyFile;
  33936. #endif
  33937. #ifndef NO_RSA
  33938. if (rsaClientCertBuf == NULL || rsaClientCertBufSz == NULL ||
  33939. rsaClientPrivKeyBuf == NULL || rsaClientPrivKeyBufSz == NULL)
  33940. return BAD_FUNC_ARG;
  33941. #endif
  33942. #ifdef HAVE_ECC
  33943. if (eccClientCertBuf == NULL || eccClientCertBufSz == NULL ||
  33944. eccClientPrivKeyBuf == NULL || eccClientPrivKeyBufSz == NULL)
  33945. return BAD_FUNC_ARG;
  33946. #endif
  33947. /* RSA */
  33948. #ifndef NO_RSA
  33949. #ifdef USE_CERT_BUFFERS_1024
  33950. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_1024)
  33951. return WC_TEST_RET_ENC_NC;
  33952. XMEMCPY(rsaClientCertBuf, client_cert_der_1024,
  33953. sizeof_client_cert_der_1024);
  33954. *rsaClientCertBufSz = sizeof_client_cert_der_1024;
  33955. if (rsaServerCertBuf != NULL) {
  33956. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_1024)
  33957. return WC_TEST_RET_ENC_NC;
  33958. XMEMCPY(rsaServerCertBuf, server_cert_der_1024,
  33959. sizeof_server_cert_der_1024);
  33960. *rsaServerCertBufSz = sizeof_server_cert_der_1024;
  33961. }
  33962. if (rsaCaCertBuf != NULL) {
  33963. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_1024)
  33964. return WC_TEST_RET_ENC_NC;
  33965. XMEMCPY(rsaCaCertBuf, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  33966. *rsaCaCertBufSz = sizeof_ca_cert_der_1024;
  33967. }
  33968. #elif defined(USE_CERT_BUFFERS_2048)
  33969. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_2048)
  33970. return WC_TEST_RET_ENC_NC;
  33971. XMEMCPY(rsaClientCertBuf, client_cert_der_2048,
  33972. sizeof_client_cert_der_2048);
  33973. *rsaClientCertBufSz = sizeof_client_cert_der_2048;
  33974. if (rsaServerCertBuf != NULL) {
  33975. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_2048)
  33976. return WC_TEST_RET_ENC_NC;
  33977. XMEMCPY(rsaServerCertBuf, server_cert_der_2048,
  33978. sizeof_server_cert_der_2048);
  33979. *rsaServerCertBufSz = sizeof_server_cert_der_2048;
  33980. }
  33981. if (rsaCaCertBuf != NULL) {
  33982. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_2048)
  33983. return WC_TEST_RET_ENC_NC;
  33984. XMEMCPY(rsaCaCertBuf, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  33985. *rsaCaCertBufSz = sizeof_ca_cert_der_2048;
  33986. }
  33987. #else
  33988. certFile = XFOPEN(clientCert, "rb");
  33989. if (!certFile)
  33990. return WC_TEST_RET_ENC_ERRNO;
  33991. *rsaClientCertBufSz = (word32)XFREAD(rsaClientCertBuf, 1,
  33992. *rsaClientCertBufSz, certFile);
  33993. XFCLOSE(certFile);
  33994. if (*rsaClientCertBufSz == 0)
  33995. return WC_TEST_RET_ENC_ERRNO;
  33996. if (rsaServerCertBuf != NULL) {
  33997. certFile = XFOPEN(rsaServerCertDerFile, "rb");
  33998. if (!certFile)
  33999. return WC_TEST_RET_ENC_ERRNO;
  34000. *rsaServerCertBufSz = (word32)XFREAD(rsaServerCertBuf, 1,
  34001. *rsaServerCertBufSz, certFile);
  34002. XFCLOSE(certFile);
  34003. if (*rsaServerCertBufSz == 0)
  34004. return WC_TEST_RET_ENC_ERRNO;
  34005. }
  34006. if (rsaCaCertBuf != NULL) {
  34007. certFile = XFOPEN(rsaCaCertDerFile, "rb");
  34008. if (!certFile)
  34009. return WC_TEST_RET_ENC_ERRNO;
  34010. *rsaCaCertBufSz = (word32)XFREAD(rsaCaCertBuf, 1, *rsaCaCertBufSz,
  34011. certFile);
  34012. XFCLOSE(certFile);
  34013. if (*rsaCaCertBufSz == 0)
  34014. return WC_TEST_RET_ENC_ERRNO;
  34015. }
  34016. #endif
  34017. #ifdef USE_CERT_BUFFERS_1024
  34018. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_1024)
  34019. return WC_TEST_RET_ENC_NC;
  34020. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_1024,
  34021. sizeof_client_key_der_1024);
  34022. *rsaClientPrivKeyBufSz = sizeof_client_key_der_1024;
  34023. if (rsaServerPrivKeyBuf != NULL) {
  34024. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_1024)
  34025. return WC_TEST_RET_ENC_NC;
  34026. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_1024,
  34027. sizeof_server_key_der_1024);
  34028. *rsaServerPrivKeyBufSz = sizeof_server_key_der_1024;
  34029. }
  34030. if (rsaCaPrivKeyBuf != NULL) {
  34031. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_1024)
  34032. return WC_TEST_RET_ENC_NC;
  34033. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_1024, sizeof_ca_key_der_1024);
  34034. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_1024;
  34035. }
  34036. #elif defined(USE_CERT_BUFFERS_2048)
  34037. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_2048)
  34038. return WC_TEST_RET_ENC_NC;
  34039. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_2048,
  34040. sizeof_client_key_der_2048);
  34041. *rsaClientPrivKeyBufSz = sizeof_client_key_der_2048;
  34042. if (rsaServerPrivKeyBuf != NULL) {
  34043. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_2048)
  34044. return WC_TEST_RET_ENC_NC;
  34045. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_2048,
  34046. sizeof_server_key_der_2048);
  34047. *rsaServerPrivKeyBufSz = sizeof_server_key_der_2048;
  34048. }
  34049. if (rsaCaPrivKeyBuf != NULL) {
  34050. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_2048)
  34051. return WC_TEST_RET_ENC_NC;
  34052. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_2048, sizeof_ca_key_der_2048);
  34053. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_2048;
  34054. }
  34055. #else
  34056. keyFile = XFOPEN(clientKey, "rb");
  34057. if (!keyFile)
  34058. return WC_TEST_RET_ENC_ERRNO;
  34059. *rsaClientPrivKeyBufSz = (word32)XFREAD(rsaClientPrivKeyBuf, 1,
  34060. *rsaClientPrivKeyBufSz, keyFile);
  34061. XFCLOSE(keyFile);
  34062. if (*rsaClientPrivKeyBufSz == 0)
  34063. return WC_TEST_RET_ENC_ERRNO;
  34064. if (rsaServerPrivKeyBuf != NULL) {
  34065. keyFile = XFOPEN(rsaServerKeyDerFile, "rb");
  34066. if (!keyFile)
  34067. return WC_TEST_RET_ENC_ERRNO;
  34068. *rsaServerPrivKeyBufSz = (word32)XFREAD(rsaServerPrivKeyBuf, 1,
  34069. *rsaServerPrivKeyBufSz, keyFile);
  34070. XFCLOSE(keyFile);
  34071. if (*rsaServerPrivKeyBufSz == 0)
  34072. return WC_TEST_RET_ENC_ERRNO;
  34073. }
  34074. if (rsaCaPrivKeyBuf != NULL) {
  34075. keyFile = XFOPEN(rsaCaKeyFile, "rb");
  34076. if (!keyFile)
  34077. return WC_TEST_RET_ENC_ERRNO;
  34078. *rsaCaPrivKeyBufSz = (word32)XFREAD(rsaCaPrivKeyBuf, 1,
  34079. *rsaCaPrivKeyBufSz, keyFile);
  34080. XFCLOSE(keyFile);
  34081. if (*rsaCaPrivKeyBufSz == 0)
  34082. return WC_TEST_RET_ENC_ERRNO;
  34083. }
  34084. #endif /* USE_CERT_BUFFERS */
  34085. #endif /* NO_RSA */
  34086. /* ECC */
  34087. #ifdef HAVE_ECC
  34088. #ifdef USE_CERT_BUFFERS_256
  34089. if (*eccClientCertBufSz < (word32)sizeof_cliecc_cert_der_256)
  34090. return WC_TEST_RET_ENC_NC;
  34091. XMEMCPY(eccClientCertBuf, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  34092. *eccClientCertBufSz = sizeof_cliecc_cert_der_256;
  34093. #else
  34094. certFile = XFOPEN(eccClientCert, "rb");
  34095. if (!certFile)
  34096. return WC_TEST_RET_ENC_ERRNO;
  34097. *eccClientCertBufSz = (word32)XFREAD(eccClientCertBuf, 1,
  34098. *eccClientCertBufSz, certFile);
  34099. XFCLOSE(certFile);
  34100. if (*eccClientCertBufSz == 0)
  34101. return WC_TEST_RET_ENC_ERRNO;
  34102. #endif /* USE_CERT_BUFFERS_256 */
  34103. #ifdef USE_CERT_BUFFERS_256
  34104. if (*eccClientPrivKeyBufSz < (word32)sizeof_ecc_clikey_der_256)
  34105. return WC_TEST_RET_ENC_NC;
  34106. XMEMCPY(eccClientPrivKeyBuf, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  34107. *eccClientPrivKeyBufSz = sizeof_ecc_clikey_der_256;
  34108. #else
  34109. keyFile = XFOPEN(eccClientKey, "rb");
  34110. if (!keyFile)
  34111. return WC_TEST_RET_ENC_ERRNO;
  34112. *eccClientPrivKeyBufSz = (word32)XFREAD(eccClientPrivKeyBuf, 1,
  34113. *eccClientPrivKeyBufSz, keyFile);
  34114. XFCLOSE(keyFile);
  34115. if (*eccClientPrivKeyBufSz == 0)
  34116. return WC_TEST_RET_ENC_ERRNO;
  34117. #endif /* USE_CERT_BUFFERS_256 */
  34118. #endif /* HAVE_ECC */
  34119. #ifdef NO_RSA
  34120. (void)rsaClientCertBuf;
  34121. (void)rsaClientCertBufSz;
  34122. (void)rsaClientPrivKeyBuf;
  34123. (void)rsaClientPrivKeyBufSz;
  34124. (void)rsaServerCertBuf;
  34125. (void)rsaServerCertBufSz;
  34126. (void)rsaServerPrivKeyBuf;
  34127. (void)rsaServerPrivKeyBufSz;
  34128. (void)rsaCaCertBuf;
  34129. (void)rsaCaCertBufSz;
  34130. (void)rsaCaPrivKeyBuf;
  34131. (void)rsaCaPrivKeyBufSz;
  34132. #endif
  34133. #ifndef HAVE_ECC
  34134. (void)eccClientCertBuf;
  34135. (void)eccClientCertBufSz;
  34136. (void)eccClientPrivKeyBuf;
  34137. (void)eccClientPrivKeyBufSz;
  34138. #endif
  34139. #ifndef NO_FILESYSTEM
  34140. (void)certFile;
  34141. (void)keyFile;
  34142. #endif
  34143. return 0;
  34144. }
  34145. typedef struct {
  34146. const byte* content;
  34147. word32 contentSz;
  34148. int contentOID;
  34149. int encryptOID;
  34150. int keyWrapOID;
  34151. int keyAgreeOID;
  34152. byte* cert;
  34153. size_t certSz;
  34154. byte* privateKey;
  34155. word32 privateKeySz;
  34156. byte* optionalUkm;
  34157. word32 optionalUkmSz;
  34158. int ktriOptions; /* KTRI options flags */
  34159. int kariOptions; /* KARI options flags */
  34160. /* KEKRI specific */
  34161. const byte* secretKey; /* key, only for kekri RecipientInfo types */
  34162. word32 secretKeySz; /* size of secretKey, bytes */
  34163. const byte* secretKeyId; /* key identifier */
  34164. word32 secretKeyIdSz; /* size of key identifier, bytes */
  34165. void* timePtr; /* time_t pointer */
  34166. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  34167. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  34168. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  34169. word32 otherAttrSz; /* size of otherAttr, bytes */
  34170. int kekriOptions; /* KEKRI options flags */
  34171. /* PWRI specific */
  34172. const char* password;
  34173. word32 passwordSz;
  34174. const byte* salt;
  34175. word32 saltSz;
  34176. int kdfOID;
  34177. int hashOID;
  34178. int kdfIterations;
  34179. int pwriOptions; /* PWRI options flags */
  34180. /* ORI specific */
  34181. int isOri;
  34182. int oriOptions; /* ORI options flags */
  34183. const char* outFileName;
  34184. } pkcs7EnvelopedVector;
  34185. static const byte asnDataOid[] = {
  34186. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01
  34187. };
  34188. /* ORI encrypt callback, responsible for encrypting content-encryption key (CEK)
  34189. * and giving wolfCrypt the value for oriOID and oriValue to place in
  34190. * OtherRecipientInfo.
  34191. *
  34192. * Returns 0 on success, negative upon error. */
  34193. static int myOriEncryptCb(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* oriType,
  34194. word32* oriTypeSz, byte* oriValue, word32* oriValueSz,
  34195. void* ctx)
  34196. {
  34197. int i;
  34198. /* make sure buffers are large enough */
  34199. if ((*oriValueSz < (2 + cekSz)) || (*oriTypeSz < sizeof(oriType)))
  34200. return WC_TEST_RET_ENC_NC;
  34201. /* our simple encryption algorithm will be take the bitwise complement */
  34202. oriValue[0] = 0x04; /*ASN OCTET STRING */
  34203. oriValue[1] = (byte)cekSz; /* length */
  34204. for (i = 0; i < (int)cekSz; i++) {
  34205. oriValue[2 + i] = ~cek[i];
  34206. }
  34207. *oriValueSz = 2 + cekSz;
  34208. /* set oriType to ASN.1 encoded data OID */
  34209. XMEMCPY(oriType, asnDataOid, sizeof(asnDataOid));
  34210. *oriTypeSz = sizeof(asnDataOid);
  34211. (void)pkcs7;
  34212. (void)ctx;
  34213. return 0;
  34214. }
  34215. /* ORI decrypt callback, responsible for providing a decrypted content
  34216. * encryption key (CEK) placed into decryptedKey and size placed into
  34217. * decryptedKeySz. oriOID and oriValue are given to the callback to help
  34218. * in decrypting the encrypted CEK.
  34219. *
  34220. * Returns 0 on success, negative upon error. */
  34221. static int myOriDecryptCb(PKCS7* pkcs7, byte* oriType, word32 oriTypeSz,
  34222. byte* oriValue, word32 oriValueSz, byte* decryptedKey,
  34223. word32* decryptedKeySz, void* ctx)
  34224. {
  34225. int i;
  34226. /* make sure oriType matches what we expect */
  34227. if (oriTypeSz != sizeof(asnDataOid))
  34228. return WC_TEST_RET_ENC_NC;
  34229. if (XMEMCMP(oriType, asnDataOid, sizeof(asnDataOid)) != 0)
  34230. return WC_TEST_RET_ENC_NC;
  34231. /* make sure decrypted buffer is large enough */
  34232. if (*decryptedKeySz < oriValueSz)
  34233. return WC_TEST_RET_ENC_NC;
  34234. /* decrypt encrypted CEK using simple bitwise complement,
  34235. only for example */
  34236. for (i = 0; i < (int)oriValueSz - 2; i++) {
  34237. decryptedKey[i] = ~oriValue[2 + i];
  34238. }
  34239. *decryptedKeySz = oriValueSz - 2;
  34240. (void)pkcs7;
  34241. (void)ctx;
  34242. return 0;
  34243. }
  34244. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  34245. /* returns 0 on success */
  34246. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  34247. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  34248. byte* in, int inSz, byte* out, void* usrCtx)
  34249. {
  34250. wc_test_ret_t ret;
  34251. int keyId = -1, keySz;
  34252. word32 keyIdSz = 8;
  34253. const byte* key;
  34254. byte keyIdRaw[8];
  34255. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  34256. Aes *aes;
  34257. #else
  34258. Aes aes[1];
  34259. #endif
  34260. /* looking for KEY ID
  34261. * fwDecryptKeyID OID "1.2.840.113549.1.9.16.2.37
  34262. */
  34263. WOLFSSL_SMALL_STACK_STATIC const unsigned char OID[] = {
  34264. /* 0x06, 0x0B do not pass in tag and length */
  34265. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  34266. 0x01, 0x09, 0x10, 0x02, 0x25
  34267. };
  34268. WOLFSSL_SMALL_STACK_STATIC const byte defKey[] = {
  34269. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  34270. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  34271. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  34272. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  34273. };
  34274. WOLFSSL_SMALL_STACK_STATIC const byte altKey[] = {
  34275. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  34276. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  34277. };
  34278. /* test user context passed in */
  34279. if (usrCtx == NULL || *(int*)usrCtx != 1) {
  34280. return WC_TEST_RET_ENC_NC;
  34281. }
  34282. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  34283. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  34284. return WC_TEST_RET_ENC_ERRNO;
  34285. #endif
  34286. /* if needing to find keyIdSz can call with NULL */
  34287. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), NULL,
  34288. &keyIdSz);
  34289. if (ret != LENGTH_ONLY_E) {
  34290. printf("Unexpected error %d when getting keyIdSz\n", ret);
  34291. printf("Possibly no KEY ID attribute set\n");
  34292. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34293. }
  34294. else {
  34295. XMEMSET(keyIdRaw, 0, sizeof(keyIdRaw));
  34296. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), keyIdRaw,
  34297. &keyIdSz);
  34298. if (ret < 0) {
  34299. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34300. }
  34301. if (keyIdSz < 3) {
  34302. printf("keyIdSz is smaller than expected\n");
  34303. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34304. }
  34305. if (keyIdSz > 2 + sizeof(int)) {
  34306. printf("example case was only expecting a keyId of int size\n");
  34307. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34308. }
  34309. /* keyIdRaw[0] OCTET TAG */
  34310. /* keyIdRaw[1] Length */
  34311. #ifdef BIG_ENDIAN_ORDER
  34312. if (keyIdRaw[1] == 0x01) {
  34313. keyId = 1;
  34314. }
  34315. #else
  34316. XMEMCPY(&keyId, keyIdRaw + 2, sizeof(keyId));
  34317. #endif
  34318. }
  34319. /* Use keyID here if found to select key and decrypt in HSM or in this
  34320. * example just select key and do software decryption */
  34321. if (keyId == 1) {
  34322. key = altKey;
  34323. keySz = sizeof(altKey);
  34324. }
  34325. else {
  34326. key = defKey;
  34327. keySz = sizeof(defKey);
  34328. }
  34329. switch (encryptOID) {
  34330. #ifdef WOLFSSL_AES_256
  34331. case AES256CBCb:
  34332. if ((keySz != 32 ) || (ivSz != AES_BLOCK_SIZE))
  34333. ERROR_OUT(BAD_FUNC_ARG, out);
  34334. break;
  34335. #endif
  34336. #ifdef WOLFSSL_AES_128
  34337. case AES128CBCb:
  34338. if ((keySz != 16 ) || (ivSz != AES_BLOCK_SIZE))
  34339. ERROR_OUT(BAD_FUNC_ARG, out);
  34340. break;
  34341. #endif
  34342. default:
  34343. printf("Unsupported content cipher type for example");
  34344. ERROR_OUT(ALGO_ID_E, out);
  34345. };
  34346. ret = wc_AesInit(aes, HEAP_HINT, INVALID_DEVID);
  34347. if (ret == 0) {
  34348. ret = wc_AesSetKey(aes, key, keySz, iv, AES_DECRYPTION);
  34349. if (ret == 0)
  34350. ret = wc_AesCbcDecrypt(aes, out, in, inSz);
  34351. wc_AesFree(aes);
  34352. }
  34353. out:
  34354. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  34355. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34356. #endif
  34357. (void)aad;
  34358. (void)aadSz;
  34359. (void)authTag;
  34360. (void)authTagSz;
  34361. return (int)ret;
  34362. }
  34363. #endif /* !NO_AES && HAVE_AES_CBC */
  34364. #define PKCS7_BUF_SIZE 2048
  34365. static wc_test_ret_t pkcs7enveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  34366. byte* rsaPrivKey, word32 rsaPrivKeySz,
  34367. byte* eccCert, word32 eccCertSz,
  34368. byte* eccPrivKey, word32 eccPrivKeySz)
  34369. {
  34370. wc_test_ret_t ret = 0;
  34371. int testSz = 0, i;
  34372. int envelopedSz, decodedSz;
  34373. byte *enveloped = NULL;
  34374. byte *decoded = NULL;
  34375. PKCS7* pkcs7 = NULL;
  34376. #ifdef ECC_TIMING_RESISTANT
  34377. WC_RNG rng;
  34378. #endif
  34379. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  34380. XFILE pkcs7File;
  34381. #endif
  34382. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  34383. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  34384. 0x72,0x6c,0x64
  34385. };
  34386. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256) && \
  34387. defined(HAVE_ECC) && defined(WOLFSSL_SHA512)
  34388. byte optionalUkm[] = {
  34389. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  34390. };
  34391. #endif /* NO_AES */
  34392. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128) && \
  34393. !defined(NO_SHA)
  34394. /* encryption key for kekri recipient types */
  34395. WOLFSSL_SMALL_STACK_STATIC const byte secretKey[] = {
  34396. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  34397. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  34398. };
  34399. /* encryption key identifier */
  34400. WOLFSSL_SMALL_STACK_STATIC const byte secretKeyId[] = {
  34401. 0x02,0x02,0x03,0x04
  34402. };
  34403. #endif
  34404. #if !defined(NO_PWDBASED) && !defined(NO_SHA) && \
  34405. !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  34406. #ifndef HAVE_FIPS
  34407. WOLFSSL_SMALL_STACK_STATIC const char password[] = "password"; /* NOTE: Password is too short for FIPS */
  34408. #else
  34409. WOLFSSL_SMALL_STACK_STATIC const char password[] = "passwordFIPS_MODE";
  34410. #endif
  34411. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = {
  34412. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  34413. };
  34414. #endif
  34415. #define MAX_TESTVECTORS_LEN 13
  34416. #define ADD_PKCS7ENVELOPEDVECTOR(...) { \
  34417. pkcs7EnvelopedVector _this_vector = { __VA_ARGS__ }; \
  34418. if (testSz == MAX_TESTVECTORS_LEN) { \
  34419. ret = WC_TEST_RET_ENC_NC; \
  34420. goto out; \
  34421. } \
  34422. XMEMCPY(&testVectors[testSz++], &_this_vector, sizeof _this_vector);\
  34423. }
  34424. pkcs7EnvelopedVector *testVectors = NULL;
  34425. #ifdef ECC_TIMING_RESISTANT
  34426. XMEMSET(&rng, 0, sizeof(rng));
  34427. #endif
  34428. testVectors = (pkcs7EnvelopedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  34429. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34430. if (testVectors == NULL) {
  34431. ret = WC_TEST_RET_ENC_ERRNO;
  34432. goto out;
  34433. }
  34434. {
  34435. /* key transport key encryption technique */
  34436. #ifndef NO_RSA
  34437. #ifndef NO_DES3
  34438. ADD_PKCS7ENVELOPEDVECTOR(
  34439. data, (word32)sizeof(data), DATA, DES3b, 0, 0, rsaCert, rsaCertSz,
  34440. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  34441. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  34442. "pkcs7envelopedDataDES3.der");
  34443. #endif
  34444. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  34445. #ifdef WOLFSSL_AES_128
  34446. ADD_PKCS7ENVELOPEDVECTOR(
  34447. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, rsaCert, rsaCertSz,
  34448. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  34449. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  34450. "pkcs7envelopedDataAES128CBC.der");
  34451. #endif
  34452. #ifdef WOLFSSL_AES_192
  34453. ADD_PKCS7ENVELOPEDVECTOR(
  34454. data, (word32)sizeof(data), DATA, AES192CBCb, 0, 0, rsaCert, rsaCertSz,
  34455. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  34456. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  34457. "pkcs7envelopedDataAES192CBC.der");
  34458. #endif
  34459. #ifdef WOLFSSL_AES_256
  34460. ADD_PKCS7ENVELOPEDVECTOR(
  34461. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  34462. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  34463. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  34464. "pkcs7envelopedDataAES256CBC.der");
  34465. /* explicitly using SKID for SubjectKeyIdentifier */
  34466. ADD_PKCS7ENVELOPEDVECTOR(
  34467. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  34468. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_SKID, 0, NULL, 0, NULL, 0, NULL,
  34469. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  34470. "pkcs7envelopedDataAES256CBC_SKID.der");
  34471. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  34472. ADD_PKCS7ENVELOPEDVECTOR(
  34473. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  34474. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_ISSUER_AND_SERIAL_NUMBER, 0,
  34475. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  34476. 0, 0, 0, 0, "pkcs7envelopedDataAES256CBC_IANDS.der");
  34477. #endif
  34478. #endif /* !NO_AES && HAVE_AES_CBC */
  34479. #endif
  34480. /* key agreement key encryption technique*/
  34481. #ifdef HAVE_ECC
  34482. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  34483. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  34484. ADD_PKCS7ENVELOPEDVECTOR(
  34485. data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP,
  34486. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34487. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  34488. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  34489. "pkcs7envelopedDataAES128CBC_ECDH_SHA1KDF.der");
  34490. #endif
  34491. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  34492. ADD_PKCS7ENVELOPEDVECTOR(
  34493. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  34494. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34495. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  34496. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  34497. "pkcs7envelopedDataAES256CBC_ECDH_SHA256KDF.der");
  34498. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  34499. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  34500. ADD_PKCS7ENVELOPEDVECTOR(
  34501. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  34502. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34503. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  34504. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  34505. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF.der");
  34506. /* with optional user keying material (ukm) */
  34507. ADD_PKCS7ENVELOPEDVECTOR(
  34508. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  34509. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34510. eccPrivKeySz, optionalUkm, sizeof(optionalUkm), 0, 0, NULL, 0,
  34511. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  34512. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF_ukm.der");
  34513. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  34514. #endif /* !NO_AES && HAVE_AES_CBC */
  34515. #endif
  34516. /* kekri (KEKRecipientInfo) recipient types */
  34517. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  34518. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  34519. ADD_PKCS7ENVELOPEDVECTOR(
  34520. data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP, 0,
  34521. NULL, 0, NULL, 0, NULL, 0, 0, 0, secretKey, sizeof(secretKey),
  34522. secretKeyId, sizeof(secretKeyId), NULL, NULL, 0, NULL, 0,
  34523. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  34524. "pkcs7envelopedDataAES128CBC_KEKRI.der");
  34525. #endif
  34526. #endif /* !NO_AES && HAVE_AES_CBC */
  34527. /* pwri (PasswordRecipientInfo) recipient types */
  34528. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  34529. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  34530. ADD_PKCS7ENVELOPEDVECTOR(
  34531. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0,
  34532. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  34533. NULL, 0, NULL, NULL, 0, NULL, 0, 0, password,
  34534. (word32)XSTRLEN(password), salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  34535. 0, 0, 0, "pkcs7envelopedDataAES128CBC_PWRI.der");
  34536. #endif
  34537. #endif
  34538. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_128)
  34539. /* ori (OtherRecipientInfo) recipient types */
  34540. ADD_PKCS7ENVELOPEDVECTOR(
  34541. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, NULL, 0, NULL, 0,
  34542. NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0,
  34543. NULL, 0, 0, 0, 0, 0, 1, 0, "pkcs7envelopedDataAES128CBC_ORI.der");
  34544. #endif
  34545. };
  34546. #undef MAX_TESTVECTORS_LEN
  34547. #undef ADD_PKCS7ENVELOPEDVECTOR
  34548. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34549. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34550. if ((! enveloped) || (! decoded)) {
  34551. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34552. }
  34553. #ifdef ECC_TIMING_RESISTANT
  34554. #ifndef HAVE_FIPS
  34555. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  34556. #else
  34557. ret = wc_InitRng(&rng);
  34558. #endif
  34559. if (ret != 0)
  34560. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34561. #endif
  34562. for (i = 0; i < testSz; i++) {
  34563. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  34564. #ifdef WOLFSSL_ASYNC_CRYPT
  34565. INVALID_DEVID /* async PKCS7 is not supported */
  34566. #else
  34567. devId
  34568. #endif
  34569. );
  34570. if (pkcs7 == NULL) {
  34571. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  34572. }
  34573. if (testVectors[i].secretKey != NULL) {
  34574. /* KEKRI recipient type */
  34575. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  34576. if (ret != 0)
  34577. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34578. pkcs7->content = (byte*)testVectors[i].content;
  34579. pkcs7->contentSz = testVectors[i].contentSz;
  34580. pkcs7->contentOID = testVectors[i].contentOID;
  34581. pkcs7->encryptOID = testVectors[i].encryptOID;
  34582. pkcs7->ukm = testVectors[i].optionalUkm;
  34583. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  34584. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  34585. (byte *)testVectors[i].secretKey, testVectors[i].secretKeySz,
  34586. (byte *)testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  34587. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  34588. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  34589. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  34590. if (ret < 0) {
  34591. wc_PKCS7_Free(pkcs7);
  34592. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34593. }
  34594. /* set key, for decryption */
  34595. ret = wc_PKCS7_SetKey(pkcs7, (byte *)testVectors[i].secretKey,
  34596. testVectors[i].secretKeySz);
  34597. if (ret != 0) {
  34598. wc_PKCS7_Free(pkcs7);
  34599. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34600. }
  34601. } else if (testVectors[i].password != NULL) {
  34602. #if !defined(NO_PWDBASED) && !defined(NO_SHA)
  34603. /* PWRI recipient type */
  34604. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  34605. if (ret != 0)
  34606. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34607. pkcs7->content = (byte*)testVectors[i].content;
  34608. pkcs7->contentSz = testVectors[i].contentSz;
  34609. pkcs7->contentOID = testVectors[i].contentOID;
  34610. pkcs7->encryptOID = testVectors[i].encryptOID;
  34611. pkcs7->ukm = testVectors[i].optionalUkm;
  34612. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  34613. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  34614. (byte *)testVectors[i].password, testVectors[i].passwordSz,
  34615. (byte *)testVectors[i].salt, testVectors[i].saltSz,
  34616. testVectors[i].kdfOID,
  34617. testVectors[i].hashOID, testVectors[i].kdfIterations,
  34618. testVectors[i].encryptOID, testVectors[i].pwriOptions);
  34619. if (ret < 0) {
  34620. wc_PKCS7_Free(pkcs7);
  34621. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34622. }
  34623. /* set password, for decryption */
  34624. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  34625. testVectors[i].passwordSz);
  34626. if (ret < 0) {
  34627. wc_PKCS7_Free(pkcs7);
  34628. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34629. }
  34630. #endif /* ! NO_PWDBASED && ! NO_SHA */
  34631. } else if (testVectors[i].isOri == 1) {
  34632. /* ORI recipient type */
  34633. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  34634. if (ret != 0)
  34635. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34636. pkcs7->content = (byte*)testVectors[i].content;
  34637. pkcs7->contentSz = testVectors[i].contentSz;
  34638. pkcs7->contentOID = testVectors[i].contentOID;
  34639. pkcs7->encryptOID = testVectors[i].encryptOID;
  34640. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  34641. testVectors[i].oriOptions);
  34642. if (ret < 0) {
  34643. wc_PKCS7_Free(pkcs7);
  34644. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34645. }
  34646. /* set decrypt callback for decryption */
  34647. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  34648. if (ret < 0) {
  34649. wc_PKCS7_Free(pkcs7);
  34650. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34651. }
  34652. } else {
  34653. /* KTRI or KARI recipient types */
  34654. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  34655. if (ret != 0)
  34656. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34657. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  34658. (word32)testVectors[i].certSz);
  34659. if (ret != 0) {
  34660. wc_PKCS7_Free(pkcs7);
  34661. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34662. }
  34663. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  34664. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  34665. pkcs7->privateKey = testVectors[i].privateKey;
  34666. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  34667. pkcs7->content = (byte*)testVectors[i].content;
  34668. pkcs7->contentSz = testVectors[i].contentSz;
  34669. pkcs7->contentOID = testVectors[i].contentOID;
  34670. pkcs7->encryptOID = testVectors[i].encryptOID;
  34671. pkcs7->ukm = testVectors[i].optionalUkm;
  34672. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  34673. /* set SubjectIdentifier type for KTRI types */
  34674. if (testVectors[i].ktriOptions & CMS_SKID) {
  34675. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  34676. if (ret != 0) {
  34677. wc_PKCS7_Free(pkcs7);
  34678. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34679. }
  34680. } else if (testVectors[i].ktriOptions &
  34681. CMS_ISSUER_AND_SERIAL_NUMBER) {
  34682. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  34683. CMS_ISSUER_AND_SERIAL_NUMBER);
  34684. if (ret != 0) {
  34685. wc_PKCS7_Free(pkcs7);
  34686. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  34687. }
  34688. }
  34689. }
  34690. #ifdef ECC_TIMING_RESISTANT
  34691. pkcs7->rng = &rng;
  34692. #endif
  34693. /* encode envelopedData */
  34694. envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, enveloped,
  34695. PKCS7_BUF_SIZE);
  34696. if (envelopedSz <= 0) {
  34697. wc_PKCS7_Free(pkcs7);
  34698. ERROR_OUT(WC_TEST_RET_ENC_EC(envelopedSz), out);
  34699. }
  34700. /* decode envelopedData */
  34701. pkcs7->contentOID = 0;
  34702. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped, envelopedSz,
  34703. decoded, PKCS7_BUF_SIZE);
  34704. if (pkcs7->contentOID != testVectors[i].contentOID ||
  34705. decodedSz <= 0) {
  34706. wc_PKCS7_Free(pkcs7);
  34707. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  34708. }
  34709. /* test decode result */
  34710. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  34711. wc_PKCS7_Free(pkcs7);
  34712. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34713. }
  34714. #ifndef NO_PKCS7_STREAM
  34715. { /* test reading byte by byte */
  34716. int z;
  34717. for (z = 0; z < envelopedSz; z++) {
  34718. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped + z, 1,
  34719. decoded, PKCS7_BUF_SIZE);
  34720. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  34721. printf("unexpected error %d\n", decodedSz);
  34722. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  34723. }
  34724. }
  34725. /* test decode result */
  34726. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  34727. printf("stream read compare failed\n");
  34728. wc_PKCS7_Free(pkcs7);
  34729. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  34730. }
  34731. }
  34732. #endif
  34733. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  34734. /* output pkcs7 envelopedData for external testing */
  34735. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  34736. if (!pkcs7File) {
  34737. wc_PKCS7_Free(pkcs7);
  34738. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  34739. }
  34740. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  34741. XFCLOSE(pkcs7File);
  34742. if (ret != envelopedSz) {
  34743. wc_PKCS7_Free(pkcs7);
  34744. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  34745. } else {
  34746. /* reset ret to 0 for success */
  34747. ret = 0;
  34748. }
  34749. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  34750. wc_PKCS7_Free(pkcs7);
  34751. pkcs7 = NULL;
  34752. }
  34753. #ifdef ECC_TIMING_RESISTANT
  34754. wc_FreeRng(&rng);
  34755. #endif
  34756. (void)eccCert;
  34757. (void)eccCertSz;
  34758. (void)eccPrivKey;
  34759. (void)eccPrivKeySz;
  34760. (void)rsaCert;
  34761. (void)rsaCertSz;
  34762. (void)rsaPrivKey;
  34763. (void)rsaPrivKeySz;
  34764. out:
  34765. if (testVectors)
  34766. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34767. if (enveloped)
  34768. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34769. if (decoded)
  34770. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34771. return ret;
  34772. }
  34773. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7enveloped_test(void)
  34774. {
  34775. wc_test_ret_t ret = 0;
  34776. byte* rsaCert = NULL;
  34777. byte* rsaPrivKey = NULL;
  34778. word32 rsaCertSz = 0;
  34779. word32 rsaPrivKeySz = 0;
  34780. byte* eccCert = NULL;
  34781. byte* eccPrivKey = NULL;
  34782. word32 eccCertSz = 0;
  34783. word32 eccPrivKeySz = 0;
  34784. #ifndef NO_RSA
  34785. /* read client RSA cert and key in DER format */
  34786. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34787. if (rsaCert == NULL)
  34788. return WC_TEST_RET_ENC_ERRNO;
  34789. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34790. if (rsaPrivKey == NULL) {
  34791. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34792. return WC_TEST_RET_ENC_NC;
  34793. }
  34794. rsaCertSz = FOURK_BUF;
  34795. rsaPrivKeySz = FOURK_BUF;
  34796. #endif /* NO_RSA */
  34797. #ifdef HAVE_ECC
  34798. /* read client ECC cert and key in DER format */
  34799. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34800. if (eccCert == NULL) {
  34801. #ifndef NO_RSA
  34802. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34803. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34804. #endif
  34805. return WC_TEST_RET_ENC_NC;
  34806. }
  34807. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34808. if (eccPrivKey == NULL) {
  34809. #ifndef NO_RSA
  34810. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34811. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34812. #endif
  34813. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34814. return WC_TEST_RET_ENC_NC;
  34815. }
  34816. eccCertSz = FOURK_BUF;
  34817. eccPrivKeySz = FOURK_BUF;
  34818. #endif /* HAVE_ECC */
  34819. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  34820. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  34821. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  34822. eccPrivKey, &eccPrivKeySz);
  34823. if (ret < 0) {
  34824. #ifndef NO_RSA
  34825. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34826. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34827. #endif
  34828. #ifdef HAVE_ECC
  34829. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34830. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34831. #endif
  34832. return WC_TEST_RET_ENC_EC(ret);
  34833. }
  34834. ret = pkcs7enveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  34835. rsaPrivKey, (word32)rsaPrivKeySz,
  34836. eccCert, (word32)eccCertSz,
  34837. eccPrivKey, (word32)eccPrivKeySz);
  34838. #ifndef NO_RSA
  34839. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34840. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34841. #endif
  34842. #ifdef HAVE_ECC
  34843. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34844. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34845. #endif
  34846. return ret;
  34847. }
  34848. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  34849. typedef struct {
  34850. const byte* content;
  34851. word32 contentSz;
  34852. int contentOID;
  34853. int encryptOID;
  34854. int keyWrapOID;
  34855. int keyAgreeOID;
  34856. byte* cert;
  34857. size_t certSz;
  34858. byte* privateKey;
  34859. word32 privateKeySz;
  34860. PKCS7Attrib* authAttribs;
  34861. word32 authAttribsSz;
  34862. PKCS7Attrib* unauthAttribs;
  34863. word32 unauthAttribsSz;
  34864. /* KARI / KTRI specific */
  34865. byte* optionalUkm;
  34866. word32 optionalUkmSz;
  34867. int ktriOptions; /* KTRI options flags */
  34868. int kariOptions; /* KARI options flags */
  34869. /* KEKRI specific */
  34870. byte* secretKey; /* key, only for kekri RecipientInfo types */
  34871. word32 secretKeySz; /* size of secretKey, bytes */
  34872. byte* secretKeyId; /* key identifier */
  34873. word32 secretKeyIdSz; /* size of key identifier, bytes */
  34874. void* timePtr; /* time_t pointer */
  34875. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  34876. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  34877. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  34878. word32 otherAttrSz; /* size of otherAttr, bytes */
  34879. int kekriOptions; /* KEKRI options flags */
  34880. /* PWRI specific */
  34881. char* password; /* password */
  34882. word32 passwordSz; /* password size, bytes */
  34883. byte* salt; /* KDF salt */
  34884. word32 saltSz; /* KDF salt size, bytes */
  34885. int kdfOID; /* KDF OID */
  34886. int hashOID; /* KDF hash algorithm OID */
  34887. int kdfIterations; /* KDF iterations */
  34888. int kekEncryptOID; /* KEK encryption algorithm OID */
  34889. int pwriOptions; /* PWRI options flags */
  34890. /* ORI specific */
  34891. int isOri;
  34892. int oriOptions; /* ORI options flags */
  34893. const char* outFileName;
  34894. } pkcs7AuthEnvelopedVector;
  34895. static wc_test_ret_t pkcs7authenveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  34896. byte* rsaPrivKey, word32 rsaPrivKeySz,
  34897. byte* eccCert, word32 eccCertSz,
  34898. byte* eccPrivKey, word32 eccPrivKeySz)
  34899. {
  34900. wc_test_ret_t ret = 0;
  34901. int testSz = 0, i;
  34902. int envelopedSz, decodedSz;
  34903. byte *enveloped = NULL;
  34904. byte *decoded = NULL;
  34905. WC_RNG rng;
  34906. PKCS7* pkcs7;
  34907. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  34908. XFILE pkcs7File;
  34909. #endif
  34910. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  34911. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  34912. 0x72,0x6c,0x64
  34913. };
  34914. byte senderNonce[PKCS7_NONCE_SZ + 2];
  34915. #ifdef HAVE_ECC
  34916. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  34917. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  34918. WOLFSSL_SMALL_STACK_STATIC const byte senderNonceOid[] =
  34919. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  34920. 0x09, 0x05 };
  34921. PKCS7Attrib attribs[] =
  34922. {
  34923. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  34924. sizeof(senderNonce) }
  34925. };
  34926. #endif
  34927. #endif
  34928. #endif
  34929. #if !defined(NO_AES) && defined(WOLFSSL_AES_256) && defined(HAVE_ECC) && \
  34930. defined(WOLFSSL_SHA512)
  34931. WOLFSSL_SMALL_STACK_STATIC const byte optionalUkm[] = {
  34932. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  34933. };
  34934. #endif /* NO_AES */
  34935. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  34936. /* encryption key for kekri recipient types */
  34937. WOLFSSL_SMALL_STACK_STATIC const byte secretKey[] = {
  34938. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  34939. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  34940. };
  34941. /* encryption key identifier */
  34942. WOLFSSL_SMALL_STACK_STATIC const byte secretKeyId[] = {
  34943. 0x02,0x02,0x03,0x04
  34944. };
  34945. #endif
  34946. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  34947. !defined(NO_SHA) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  34948. #ifndef HAVE_FIPS
  34949. WOLFSSL_SMALL_STACK_STATIC const char password[] = "password";
  34950. #else
  34951. WOLFSSL_SMALL_STACK_STATIC const char password[] = "passwordFIPS_MODE";
  34952. #endif
  34953. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = {
  34954. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  34955. };
  34956. #endif
  34957. #define MAX_TESTVECTORS_LEN 20
  34958. #define ADD_PKCS7AUTHENVELOPEDVECTOR(...) { \
  34959. pkcs7AuthEnvelopedVector _this_vector = { __VA_ARGS__ }; \
  34960. if (testSz == MAX_TESTVECTORS_LEN) { \
  34961. ret = WC_TEST_RET_ENC_NC; \
  34962. goto out; \
  34963. } \
  34964. XMEMCPY(&testVectors[testSz++], &_this_vector, \
  34965. sizeof _this_vector); \
  34966. }
  34967. pkcs7AuthEnvelopedVector *testVectors = NULL;
  34968. XMEMSET(&rng, 0, sizeof(rng));
  34969. testVectors = (pkcs7AuthEnvelopedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  34970. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34971. if (testVectors == NULL) {
  34972. ret = WC_TEST_RET_ENC_ERRNO;
  34973. goto out;
  34974. }
  34975. {
  34976. /* key transport key encryption technique */
  34977. #ifndef NO_RSA
  34978. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  34979. #ifdef WOLFSSL_AES_128
  34980. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34981. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, rsaCert, rsaCertSz,
  34982. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  34983. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  34984. 0, 0, "pkcs7authEnvelopedDataAES128GCM.der");
  34985. #endif
  34986. #ifdef WOLFSSL_AES_192
  34987. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34988. data, (word32)sizeof(data), DATA, AES192GCMb, 0, 0, rsaCert, rsaCertSz,
  34989. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  34990. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  34991. 0, 0, "pkcs7authEnvelopedDataAES192GCM.der");
  34992. #endif
  34993. #ifdef WOLFSSL_AES_256
  34994. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34995. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  34996. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  34997. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  34998. 0, 0, "pkcs7authEnvelopedDataAES256GCM.der");
  34999. /* test with contentType set to FirmwarePkgData */
  35000. ADD_PKCS7AUTHENVELOPEDVECTOR(
  35001. data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, 0, 0,
  35002. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL,
  35003. 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL,
  35004. 0, 0, 0, 0, 0, 0, 0, 0,
  35005. "pkcs7authEnvelopedDataAES256GCM_firmwarePkgData.der");
  35006. /* explicitly using SKID for SubjectKeyIdentifier */
  35007. ADD_PKCS7AUTHENVELOPEDVECTOR(
  35008. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  35009. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, CMS_SKID, 0,
  35010. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  35011. 0, 0, 0, 0, 0, "pkcs7authEnvelopedDataAES256GCM_SKID.der");
  35012. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  35013. ADD_PKCS7AUTHENVELOPEDVECTOR(
  35014. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  35015. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0,
  35016. CMS_ISSUER_AND_SERIAL_NUMBER, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  35017. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  35018. "pkcs7authEnvelopedDataAES256GCM_IANDS.der");
  35019. #endif
  35020. #endif /* NO_AES */
  35021. #endif
  35022. /* key agreement key encryption technique*/
  35023. #ifdef HAVE_ECC
  35024. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  35025. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  35026. ADD_PKCS7AUTHENVELOPEDVECTOR(
  35027. data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP,
  35028. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  35029. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  35030. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  35031. "pkcs7authEnvelopedDataAES128GCM_ECDH_SHA1KDF.der");
  35032. #endif
  35033. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  35034. ADD_PKCS7AUTHENVELOPEDVECTOR(
  35035. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  35036. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  35037. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  35038. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  35039. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF.der");
  35040. /* with authenticated attributes */
  35041. ADD_PKCS7AUTHENVELOPEDVECTOR(
  35042. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  35043. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  35044. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  35045. NULL, 0, NULL, 0, 0, 0, NULL, 0,
  35046. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  35047. 0, 0, 0,
  35048. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_authAttribs.der");
  35049. /* with unauthenticated attributes */
  35050. ADD_PKCS7AUTHENVELOPEDVECTOR(
  35051. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  35052. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  35053. eccPrivKeySz, NULL, 0, attribs,
  35054. (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0, NULL, 0,
  35055. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  35056. 0, 0, 0,
  35057. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_unauthAttribs.der");
  35058. /* with authenticated AND unauthenticated attributes */
  35059. ADD_PKCS7AUTHENVELOPEDVECTOR(
  35060. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  35061. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  35062. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  35063. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  35064. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  35065. 0, 0, 0, 0, 0, 0,
  35066. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_bothAttribs.der");
  35067. /* with authenticated AND unauthenticated attributes AND
  35068. * contentType of FirmwarePkgData */
  35069. ADD_PKCS7AUTHENVELOPEDVECTOR(
  35070. data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, AES256_WRAP,
  35071. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  35072. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  35073. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  35074. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  35075. 0, 0, 0, 0, 0, 0,
  35076. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_fw_bothAttribs.der");
  35077. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  35078. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  35079. ADD_PKCS7AUTHENVELOPEDVECTOR(
  35080. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  35081. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  35082. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL,
  35083. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  35084. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF.der");
  35085. /* with optional user keying material (ukm) */
  35086. ADD_PKCS7AUTHENVELOPEDVECTOR(
  35087. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  35088. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  35089. eccPrivKeySz, NULL, 0, NULL, 0, (byte *)optionalUkm, sizeof(optionalUkm), 0,
  35090. 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  35091. 0, 0, 0, 0, 0, 0,
  35092. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF_ukm.der");
  35093. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  35094. #endif /* NO_AES */
  35095. #endif
  35096. /* kekri (KEKRecipientInfo) recipient types */
  35097. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  35098. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  35099. ADD_PKCS7AUTHENVELOPEDVECTOR(
  35100. data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP, 0,
  35101. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0,
  35102. (byte *)secretKey, sizeof(secretKey), (byte *)secretKeyId, sizeof(secretKeyId),
  35103. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  35104. "pkcs7authEnvelopedDataAES128GCM_KEKRI.der");
  35105. #endif
  35106. #endif
  35107. /* pwri (PasswordRecipientInfo) recipient types */
  35108. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM)
  35109. #if !defined(NO_SHA) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  35110. ADD_PKCS7AUTHENVELOPEDVECTOR(
  35111. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0,
  35112. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  35113. NULL, 0, NULL, NULL, 0, NULL, 0, 0, (char *)password,
  35114. (word32)XSTRLEN(password), (byte *)salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  35115. AES128CBCb, 0, 0, 0, "pkcs7authEnvelopedDataAES128GCM_PWRI.der");
  35116. #endif
  35117. #endif
  35118. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  35119. #ifdef WOLFSSL_AES_128
  35120. /* ori (OtherRecipientInfo) recipient types */
  35121. ADD_PKCS7AUTHENVELOPEDVECTOR(
  35122. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, NULL, 0, NULL, 0,
  35123. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  35124. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 1, 0,
  35125. "pkcs7authEnvelopedDataAES128GCM_ORI.der");
  35126. #endif
  35127. #endif
  35128. }
  35129. #undef MAX_TESTVECTORS_LEN
  35130. #undef ADD_PKCS7AUTHENVELOPEDVECTOR
  35131. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35132. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35133. if ((! enveloped) || (! decoded)) {
  35134. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35135. }
  35136. /* generate senderNonce */
  35137. {
  35138. #ifndef HAVE_FIPS
  35139. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  35140. #else
  35141. ret = wc_InitRng(&rng);
  35142. #endif
  35143. if (ret != 0)
  35144. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35145. senderNonce[0] = 0x04;
  35146. senderNonce[1] = PKCS7_NONCE_SZ;
  35147. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  35148. if (ret != 0) {
  35149. wc_FreeRng(&rng);
  35150. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35151. }
  35152. }
  35153. for (i = 0; i < testSz; i++) {
  35154. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  35155. #ifdef WOLFSSL_ASYNC_CRYPT
  35156. INVALID_DEVID /* async PKCS7 is not supported */
  35157. #else
  35158. devId
  35159. #endif
  35160. );
  35161. if (pkcs7 == NULL) {
  35162. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  35163. }
  35164. if (testVectors[i].secretKey != NULL) {
  35165. /* KEKRI recipient type */
  35166. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  35167. if (ret != 0)
  35168. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35169. pkcs7->content = (byte*)testVectors[i].content;
  35170. pkcs7->contentSz = testVectors[i].contentSz;
  35171. pkcs7->contentOID = testVectors[i].contentOID;
  35172. pkcs7->encryptOID = testVectors[i].encryptOID;
  35173. pkcs7->ukm = testVectors[i].optionalUkm;
  35174. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  35175. pkcs7->authAttribs = testVectors[i].authAttribs;
  35176. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  35177. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  35178. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  35179. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  35180. testVectors[i].secretKey, testVectors[i].secretKeySz,
  35181. testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  35182. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  35183. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  35184. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  35185. if (ret < 0) {
  35186. wc_PKCS7_Free(pkcs7);
  35187. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35188. }
  35189. /* set key, for decryption */
  35190. ret = wc_PKCS7_SetKey(pkcs7, testVectors[i].secretKey,
  35191. testVectors[i].secretKeySz);
  35192. if (ret != 0) {
  35193. wc_PKCS7_Free(pkcs7);
  35194. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35195. }
  35196. } else if (testVectors[i].password != NULL) {
  35197. #if !defined(NO_PWDBASED) && !defined(NO_SHA)
  35198. /* PWRI recipient type */
  35199. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  35200. if (ret != 0)
  35201. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35202. pkcs7->content = (byte*)testVectors[i].content;
  35203. pkcs7->contentSz = testVectors[i].contentSz;
  35204. pkcs7->contentOID = testVectors[i].contentOID;
  35205. pkcs7->encryptOID = testVectors[i].encryptOID;
  35206. pkcs7->ukm = testVectors[i].optionalUkm;
  35207. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  35208. pkcs7->authAttribs = testVectors[i].authAttribs;
  35209. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  35210. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  35211. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  35212. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  35213. (byte*)testVectors[i].password,
  35214. testVectors[i].passwordSz, testVectors[i].salt,
  35215. testVectors[i].saltSz, testVectors[i].kdfOID,
  35216. testVectors[i].hashOID, testVectors[i].kdfIterations,
  35217. testVectors[i].kekEncryptOID, testVectors[i].pwriOptions);
  35218. if (ret < 0) {
  35219. wc_PKCS7_Free(pkcs7);
  35220. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35221. }
  35222. /* set password, for decryption */
  35223. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  35224. testVectors[i].passwordSz);
  35225. if (ret < 0) {
  35226. wc_PKCS7_Free(pkcs7);
  35227. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35228. }
  35229. #endif /* ! NO_PWDBASED && ! NO_SHA */
  35230. } else if (testVectors[i].isOri == 1) {
  35231. /* ORI recipient type */
  35232. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  35233. if (ret != 0)
  35234. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35235. pkcs7->content = (byte*)testVectors[i].content;
  35236. pkcs7->contentSz = testVectors[i].contentSz;
  35237. pkcs7->contentOID = testVectors[i].contentOID;
  35238. pkcs7->encryptOID = testVectors[i].encryptOID;
  35239. pkcs7->authAttribs = testVectors[i].authAttribs;
  35240. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  35241. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  35242. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  35243. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  35244. testVectors[i].oriOptions);
  35245. if (ret < 0) {
  35246. wc_PKCS7_Free(pkcs7);
  35247. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35248. }
  35249. /* set decrypt callback for decryption */
  35250. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  35251. if (ret < 0) {
  35252. wc_PKCS7_Free(pkcs7);
  35253. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35254. }
  35255. } else {
  35256. /* KTRI or KARI recipient types */
  35257. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  35258. (word32)testVectors[i].certSz);
  35259. if (ret != 0) {
  35260. wc_PKCS7_Free(pkcs7);
  35261. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35262. }
  35263. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  35264. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  35265. pkcs7->privateKey = testVectors[i].privateKey;
  35266. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  35267. pkcs7->content = (byte*)testVectors[i].content;
  35268. pkcs7->contentSz = testVectors[i].contentSz;
  35269. pkcs7->contentOID = testVectors[i].contentOID;
  35270. pkcs7->encryptOID = testVectors[i].encryptOID;
  35271. pkcs7->ukm = testVectors[i].optionalUkm;
  35272. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  35273. pkcs7->authAttribs = testVectors[i].authAttribs;
  35274. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  35275. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  35276. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  35277. /* set SubjectIdentifier type for KTRI types */
  35278. if (testVectors[i].ktriOptions & CMS_SKID) {
  35279. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  35280. if (ret != 0) {
  35281. wc_PKCS7_Free(pkcs7);
  35282. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35283. }
  35284. } else if (testVectors[i].ktriOptions &
  35285. CMS_ISSUER_AND_SERIAL_NUMBER) {
  35286. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  35287. CMS_ISSUER_AND_SERIAL_NUMBER);
  35288. if (ret != 0) {
  35289. wc_PKCS7_Free(pkcs7);
  35290. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35291. }
  35292. }
  35293. }
  35294. #ifdef ECC_TIMING_RESISTANT
  35295. pkcs7->rng = &rng;
  35296. #endif
  35297. /* encode envelopedData */
  35298. envelopedSz = wc_PKCS7_EncodeAuthEnvelopedData(pkcs7, enveloped,
  35299. PKCS7_BUF_SIZE);
  35300. if (envelopedSz <= 0) {
  35301. wc_PKCS7_Free(pkcs7);
  35302. ERROR_OUT(WC_TEST_RET_ENC_EC(envelopedSz), out);
  35303. }
  35304. #ifndef NO_PKCS7_STREAM
  35305. { /* test reading byte by byte */
  35306. int z;
  35307. for (z = 0; z < envelopedSz; z++) {
  35308. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7,
  35309. enveloped + z, 1, decoded, PKCS7_BUF_SIZE);
  35310. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  35311. printf("unexpected error %d\n", decodedSz);
  35312. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  35313. }
  35314. }
  35315. /* test decode result */
  35316. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  35317. printf("stream read compare failed\n");
  35318. wc_PKCS7_Free(pkcs7);
  35319. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35320. }
  35321. }
  35322. #endif
  35323. /* decode envelopedData */
  35324. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7, enveloped,
  35325. envelopedSz, decoded,
  35326. PKCS7_BUF_SIZE);
  35327. if (decodedSz <= 0) {
  35328. wc_PKCS7_Free(pkcs7);
  35329. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  35330. }
  35331. /* test decode result */
  35332. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  35333. wc_PKCS7_Free(pkcs7);
  35334. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35335. }
  35336. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  35337. /* output pkcs7 envelopedData for external testing */
  35338. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  35339. if (!pkcs7File) {
  35340. wc_PKCS7_Free(pkcs7);
  35341. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  35342. }
  35343. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  35344. XFCLOSE(pkcs7File);
  35345. if (ret != envelopedSz) {
  35346. wc_PKCS7_Free(pkcs7);
  35347. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  35348. } else {
  35349. /* reset ret to 0 for success */
  35350. ret = 0;
  35351. }
  35352. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  35353. wc_PKCS7_Free(pkcs7);
  35354. pkcs7 = NULL;
  35355. }
  35356. wc_FreeRng(&rng);
  35357. (void)eccCert;
  35358. (void)eccCertSz;
  35359. (void)eccPrivKey;
  35360. (void)eccPrivKeySz;
  35361. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  35362. (void)secretKey;
  35363. (void)secretKeyId;
  35364. #endif
  35365. #ifdef NO_RSA
  35366. (void)rsaCert;
  35367. (void)rsaCertSz;
  35368. (void)rsaPrivKey;
  35369. (void)rsaPrivKeySz;
  35370. #endif
  35371. out:
  35372. if (testVectors)
  35373. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35374. if (enveloped)
  35375. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35376. if (decoded)
  35377. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35378. return ret;
  35379. }
  35380. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7authenveloped_test(void)
  35381. {
  35382. wc_test_ret_t ret = 0;
  35383. byte* rsaCert = NULL;
  35384. byte* rsaPrivKey = NULL;
  35385. word32 rsaCertSz = 0;
  35386. word32 rsaPrivKeySz = 0;
  35387. byte* eccCert = NULL;
  35388. byte* eccPrivKey = NULL;
  35389. word32 eccCertSz = 0;
  35390. word32 eccPrivKeySz = 0;
  35391. #ifndef NO_RSA
  35392. /* read client RSA cert and key in DER format */
  35393. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35394. if (rsaCert == NULL)
  35395. return WC_TEST_RET_ENC_ERRNO;
  35396. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35397. if (rsaPrivKey == NULL) {
  35398. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35399. return WC_TEST_RET_ENC_NC;
  35400. }
  35401. rsaCertSz = FOURK_BUF;
  35402. rsaPrivKeySz = FOURK_BUF;
  35403. #endif /* NO_RSA */
  35404. #ifdef HAVE_ECC
  35405. /* read client ECC cert and key in DER format */
  35406. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35407. if (eccCert == NULL) {
  35408. #ifndef NO_RSA
  35409. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35410. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35411. #endif
  35412. return WC_TEST_RET_ENC_NC;
  35413. }
  35414. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35415. if (eccPrivKey == NULL) {
  35416. #ifndef NO_RSA
  35417. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35418. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35419. #endif
  35420. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35421. return WC_TEST_RET_ENC_NC;
  35422. }
  35423. eccCertSz = FOURK_BUF;
  35424. eccPrivKeySz = FOURK_BUF;
  35425. #endif /* HAVE_ECC */
  35426. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  35427. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  35428. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  35429. eccPrivKey, &eccPrivKeySz);
  35430. if (ret < 0) {
  35431. #ifndef NO_RSA
  35432. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35433. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35434. #endif
  35435. #ifdef HAVE_ECC
  35436. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35437. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35438. #endif
  35439. return WC_TEST_RET_ENC_EC(ret);
  35440. }
  35441. ret = pkcs7authenveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  35442. rsaPrivKey, (word32)rsaPrivKeySz,
  35443. eccCert, (word32)eccCertSz,
  35444. eccPrivKey, (word32)eccPrivKeySz);
  35445. #ifndef NO_RSA
  35446. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35447. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35448. #endif
  35449. #ifdef HAVE_ECC
  35450. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35451. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35452. #endif
  35453. return ret;
  35454. }
  35455. #endif /* HAVE_AESGCM || HAVE_AESCCM */
  35456. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  35457. static const byte p7DefKey[] = {
  35458. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  35459. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  35460. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  35461. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  35462. };
  35463. static const byte p7AltKey[] = {
  35464. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  35465. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  35466. };
  35467. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  35468. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  35469. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  35470. {
  35471. wc_test_ret_t ret;
  35472. if (cek == NULL || out == NULL)
  35473. return BAD_FUNC_ARG;
  35474. /* test case sanity checks */
  35475. if (keyIdSz != 1) {
  35476. return WC_TEST_RET_ENC_NC;
  35477. }
  35478. if (keyId[0] != 0x00) {
  35479. return WC_TEST_RET_ENC_NC;
  35480. }
  35481. if (type != (int)PKCS7_KEKRI) {
  35482. return WC_TEST_RET_ENC_NC;
  35483. }
  35484. switch (keyWrapAlgo) {
  35485. case AES256_WRAP:
  35486. ret = wc_AesKeyUnWrap(p7DefKey, sizeof(p7DefKey), cek, cekSz,
  35487. out, outSz, NULL);
  35488. if (ret <= 0)
  35489. return (int)ret;
  35490. break;
  35491. default:
  35492. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  35493. return BAD_KEYWRAP_ALG_E;
  35494. };
  35495. (void)pkcs7;
  35496. (void)direction;
  35497. (void)orginKey; /* used with KAKRI */
  35498. (void)orginKeySz;
  35499. return (int)ret;
  35500. }
  35501. /* returns key size on success */
  35502. static wc_test_ret_t getFirmwareKey(PKCS7* pkcs7, byte* key, word32 keySz)
  35503. {
  35504. wc_test_ret_t ret;
  35505. word32 atrSz;
  35506. byte atr[256];
  35507. /* Additionally can look for fwWrappedFirmwareKey
  35508. * 1.2.840.113529.1.9.16.1.16 */
  35509. const unsigned char fwWrappedFirmwareKey[] = {
  35510. /* 0x06, 0x0B */
  35511. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  35512. 0x01, 0x09, 0x10, 0x02, 0x27
  35513. };
  35514. /* find keyID in fwWrappedFirmwareKey */
  35515. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  35516. sizeof(fwWrappedFirmwareKey), NULL, &atrSz);
  35517. if (ret == LENGTH_ONLY_E) {
  35518. XMEMSET(atr, 0, sizeof(atr));
  35519. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  35520. sizeof(fwWrappedFirmwareKey), atr, &atrSz);
  35521. /* keyIdRaw[0] OCTET TAG */
  35522. /* keyIdRaw[1] Length */
  35523. if (ret > 0) {
  35524. PKCS7* envPkcs7;
  35525. envPkcs7 = wc_PKCS7_New(NULL, 0);
  35526. if (envPkcs7 == NULL) {
  35527. return MEMORY_E;
  35528. }
  35529. wc_PKCS7_Init(envPkcs7, NULL, 0);
  35530. ret = wc_PKCS7_SetWrapCEKCb(envPkcs7, myCEKwrapFunc);
  35531. if (ret == 0) {
  35532. /* expecting FIRMWARE_PKG_DATA content */
  35533. envPkcs7->contentOID = FIRMWARE_PKG_DATA;
  35534. ret = wc_PKCS7_DecodeEnvelopedData(envPkcs7, atr, atrSz,
  35535. key, keySz);
  35536. if (envPkcs7->contentOID != FIRMWARE_PKG_DATA) {
  35537. /* the contentOID should have been set to the inner
  35538. * FIRMWARE_PKG_DATA content */
  35539. ret = BAD_STATE_E;
  35540. }
  35541. }
  35542. wc_PKCS7_Free(envPkcs7);
  35543. }
  35544. }
  35545. return ret;
  35546. }
  35547. /* create a KEKRI enveloped data
  35548. * return size on success */
  35549. static wc_test_ret_t envelopedData_encrypt(byte* in, word32 inSz, byte* out,
  35550. word32 outSz)
  35551. {
  35552. wc_test_ret_t ret;
  35553. PKCS7* pkcs7;
  35554. WOLFSSL_SMALL_STACK_STATIC const byte keyId[] = { 0x00 };
  35555. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  35556. if (pkcs7 == NULL)
  35557. return WC_TEST_RET_ENC_ERRNO;
  35558. pkcs7->content = in;
  35559. pkcs7->contentSz = inSz;
  35560. pkcs7->contentOID = FIRMWARE_PKG_DATA;
  35561. pkcs7->encryptOID = AES256CBCb;
  35562. pkcs7->ukm = NULL;
  35563. pkcs7->ukmSz = 0;
  35564. /* add recipient (KEKRI type) */
  35565. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP, (byte*)p7DefKey,
  35566. sizeof(p7DefKey), (byte*)keyId,
  35567. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0);
  35568. if (ret < 0) {
  35569. printf("wc_PKCS7_AddRecipient_KEKRI() failed\n");
  35570. wc_PKCS7_Free(pkcs7);
  35571. return WC_TEST_RET_ENC_EC(ret);
  35572. }
  35573. /* encode envelopedData, returns size */
  35574. ret = wc_PKCS7_EncodeEnvelopedData(pkcs7, out, outSz);
  35575. if (ret <= 0) {
  35576. printf("wc_PKCS7_EncodeEnvelopedData() failed\n");
  35577. wc_PKCS7_Free(pkcs7);
  35578. return WC_TEST_RET_ENC_EC(ret);
  35579. }
  35580. wc_PKCS7_Free(pkcs7);
  35581. return ret;
  35582. }
  35583. /*
  35584. * keyHint is the KeyID to be set in the fwDecryptKeyID attribute
  35585. * returns size of buffer output on success
  35586. */
  35587. static wc_test_ret_t generateBundle(byte* out, word32 *outSz, const byte* encryptKey,
  35588. word32 encryptKeySz, byte keyHint, byte* cert, word32 certSz,
  35589. byte* key, word32 keySz)
  35590. {
  35591. wc_test_ret_t ret;
  35592. int attribNum = 1;
  35593. PKCS7* pkcs7;
  35594. /* KEY ID
  35595. * fwDecryptKeyID OID 1.2.840.113549.1.9.16.2.37
  35596. */
  35597. const unsigned char fwDecryptKeyID[] = {
  35598. 0x06, 0x0B,
  35599. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  35600. 0x01, 0x09, 0x10, 0x02, 0x25
  35601. };
  35602. /* fwWrappedFirmwareKey 1.2.840.113529.1.9.16.1.16 */
  35603. const unsigned char fwWrappedFirmwareKey[] = {
  35604. 0x06, 0x0B, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  35605. 0x01, 0x09, 0x10, 0x02, 0x27
  35606. };
  35607. byte keyID[] = { 0x04, 0x01, 0x00 };
  35608. byte env[256];
  35609. char data[] = "Test of wolfSSL PKCS7 decrypt callback";
  35610. PKCS7Attrib attribs[] =
  35611. {
  35612. { fwDecryptKeyID, sizeof(fwDecryptKeyID), keyID, sizeof(keyID) },
  35613. { fwWrappedFirmwareKey, sizeof(fwWrappedFirmwareKey), env, 0 }
  35614. };
  35615. keyID[2] = keyHint;
  35616. /* If using keyHint 0 then create a bundle with fwWrappedFirmwareKey */
  35617. if (keyHint == 0) {
  35618. ret = envelopedData_encrypt((byte*)p7DefKey, sizeof(p7DefKey), env,
  35619. sizeof(env));
  35620. if (ret <= 0) {
  35621. return ret;
  35622. }
  35623. attribs[1].valueSz = (int)ret;
  35624. attribNum++;
  35625. }
  35626. /* init PKCS7 */
  35627. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  35628. if (pkcs7 == NULL)
  35629. return WC_TEST_RET_ENC_ERRNO;
  35630. ret = wc_PKCS7_InitWithCert(pkcs7, cert, certSz);
  35631. if (ret != 0) {
  35632. printf("ERROR: wc_PKCS7_InitWithCert() failed, ret = %d\n", ret);
  35633. wc_PKCS7_Free(pkcs7);
  35634. return WC_TEST_RET_ENC_EC(ret);
  35635. }
  35636. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  35637. if (ret != 0) {
  35638. wc_PKCS7_Free(pkcs7);
  35639. return WC_TEST_RET_ENC_EC(ret);
  35640. }
  35641. /* encode Signed Encrypted FirmwarePkgData */
  35642. if (encryptKeySz == 16) {
  35643. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  35644. encryptKeySz, key, keySz, AES128CBCb, RSAk, SHA256h,
  35645. (byte*)data, sizeof(data), NULL, 0,
  35646. attribs, attribNum, out, *outSz);
  35647. }
  35648. else {
  35649. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  35650. encryptKeySz, key, keySz, AES256CBCb, RSAk, SHA256h,
  35651. (byte*)data, sizeof(data), NULL, 0,
  35652. attribs, attribNum, out, *outSz);
  35653. }
  35654. if (ret <= 0) {
  35655. printf("ERROR: wc_PKCS7_EncodeSignedEncryptedFPD() failed, "
  35656. "ret = %d\n", ret);
  35657. wc_PKCS7_Free(pkcs7);
  35658. return WC_TEST_RET_ENC_EC(ret);
  35659. } else {
  35660. *outSz = (int)ret;
  35661. }
  35662. wc_PKCS7_Free(pkcs7);
  35663. return ret;
  35664. }
  35665. /* test verification and decryption of PKCS7 bundle
  35666. * return 0 on success
  35667. */
  35668. static wc_test_ret_t verifyBundle(byte* derBuf, word32 derSz, int keyHint)
  35669. {
  35670. wc_test_ret_t ret = 0;
  35671. int usrCtx = 1; /* test value to pass as user context to callback */
  35672. PKCS7* pkcs7 = NULL;
  35673. byte* sid = NULL;
  35674. word32 sidSz;
  35675. byte key[256];
  35676. word32 keySz = sizeof(key);
  35677. byte *decoded = NULL;
  35678. int decodedSz = FOURK_BUF/2;
  35679. WOLFSSL_SMALL_STACK_STATIC const byte expectedSid[] = {
  35680. #ifdef NO_SHA
  35681. #ifdef USE_CERT_BUFFERS_1024
  35682. 0x70, 0xe7, 0x79, 0x60, 0x8f, 0x41, 0xdc, 0xe9,
  35683. 0xad, 0x8b, 0x3d, 0x0c, 0x20, 0xf4, 0xc3, 0xf2,
  35684. 0x8e, 0x05, 0xe8, 0xa1, 0xb6, 0x68, 0x74, 0x06,
  35685. 0xbc, 0xe7, 0xc5, 0x3c, 0x13, 0x99, 0x79, 0xb9
  35686. #else
  35687. 0xce, 0x06, 0x07, 0xbe, 0xf1, 0xa6, 0x1e, 0x36,
  35688. 0xef, 0xfa, 0xbc, 0x89, 0x71, 0xf3, 0x23, 0x9e,
  35689. 0x34, 0x6d, 0xae, 0x86, 0xae, 0x2b, 0xdc, 0xf4,
  35690. 0x4a, 0x27, 0xd5, 0x63, 0x59, 0x4f, 0x4a, 0x71
  35691. #endif
  35692. #else /* !NO_SHA */
  35693. #ifdef USE_CERT_BUFFERS_1024
  35694. 0x81, 0x69, 0x0f, 0xf8, 0xdf, 0xdd, 0xcf, 0x34,
  35695. 0x29, 0xd5, 0x67, 0x75, 0x71, 0x85, 0xc7, 0x75,
  35696. 0x10, 0x69, 0x59, 0xec,
  35697. #else
  35698. 0x33, 0xD8, 0x45, 0x66, 0xD7, 0x68, 0x87, 0x18,
  35699. 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26,
  35700. 0xD7, 0x85, 0x65, 0xC0
  35701. #endif
  35702. #endif /* !NO_SHA */
  35703. };
  35704. decoded = (byte *)XMALLOC(decodedSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35705. if (decoded == NULL) {
  35706. ret = MEMORY_E;
  35707. goto out;
  35708. }
  35709. pkcs7 = wc_PKCS7_New(HEAP_HINT, INVALID_DEVID);
  35710. if (pkcs7 == NULL) {
  35711. ret = MEMORY_E;
  35712. goto out;
  35713. }
  35714. /* Test verify */
  35715. ret = wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID);
  35716. if (ret != 0)
  35717. goto out;
  35718. ret = wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  35719. if (ret != 0)
  35720. goto out;
  35721. ret = wc_PKCS7_VerifySignedData(pkcs7, derBuf, derSz);
  35722. if (ret != 0)
  35723. goto out;
  35724. /* Get size of SID and print it out */
  35725. ret = wc_PKCS7_GetSignerSID(pkcs7, NULL, &sidSz);
  35726. if (ret != LENGTH_ONLY_E)
  35727. goto out;
  35728. sid = (byte*)XMALLOC(sidSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35729. if (sid == NULL) {
  35730. ret = MEMORY_E;
  35731. goto out;
  35732. }
  35733. ret = wc_PKCS7_GetSignerSID(pkcs7, sid, &sidSz);
  35734. if (ret != 0)
  35735. goto out;
  35736. ret = XMEMCMP(sid, expectedSid, sidSz);
  35737. if (ret != 0) {
  35738. ret = PKCS7_NO_SIGNER_E; /* close enough */
  35739. goto out;
  35740. }
  35741. /* get expected fwWrappedFirmwareKey */
  35742. if (keyHint == 0) {
  35743. ret = getFirmwareKey(pkcs7, key, keySz);
  35744. if (ret < 0)
  35745. goto out;
  35746. pkcs7->encryptionKey = key;
  35747. pkcs7->encryptionKeySz = (int)ret;
  35748. }
  35749. else {
  35750. decodedSz = PKCS7_BUF_SIZE;
  35751. ret = wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc);
  35752. if (ret != 0)
  35753. goto out;
  35754. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)&usrCtx);
  35755. if (ret != 0)
  35756. goto out;
  35757. }
  35758. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  35759. pkcs7->contentSz, decoded, decodedSz);
  35760. if (decodedSz < 0) {
  35761. ret = decodedSz;
  35762. goto out;
  35763. }
  35764. ret = 0;
  35765. out:
  35766. if (decoded)
  35767. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35768. if (pkcs7)
  35769. wc_PKCS7_Free(pkcs7);
  35770. if (sid)
  35771. XFREE(sid, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35772. return ret;
  35773. }
  35774. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7callback_test(byte* cert, word32 certSz, byte* key, word32 keySz)
  35775. {
  35776. wc_test_ret_t ret = 0;
  35777. word32 derSz;
  35778. byte *derBuf = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35779. if (! derBuf)
  35780. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35781. /* Doing default generation and verify */
  35782. derSz = FOURK_BUF;
  35783. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 0, cert,
  35784. certSz, key, keySz);
  35785. if (ret <= 0) {
  35786. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  35787. }
  35788. ret = verifyBundle(derBuf, derSz, 0);
  35789. if (ret != 0)
  35790. ERROR_OUT(ret, out);
  35791. /* test choosing other key with keyID */
  35792. derSz = FOURK_BUF;
  35793. ret = generateBundle(derBuf, &derSz, p7AltKey, sizeof(p7AltKey), 1,
  35794. cert, certSz, key, keySz);
  35795. if (ret <= 0) {
  35796. ERROR_OUT(ret, out);
  35797. }
  35798. ret = verifyBundle(derBuf, derSz, 1);
  35799. if (ret != 0)
  35800. ERROR_OUT(ret, out);
  35801. /* test fail case with wrong keyID */
  35802. derSz = FOURK_BUF;
  35803. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 1,
  35804. cert, certSz, key, keySz);
  35805. if (ret <= 0) {
  35806. ERROR_OUT(ret, out);
  35807. }
  35808. ret = verifyBundle(derBuf, derSz, 1);
  35809. if (ret == 0) {
  35810. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35811. }
  35812. ret = 0;
  35813. out:
  35814. if (derBuf)
  35815. XFREE(derBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35816. return ret;
  35817. }
  35818. #endif /* !NO_AES && HAVE_AES_CBC */
  35819. #ifndef NO_PKCS7_ENCRYPTED_DATA
  35820. typedef struct {
  35821. const byte* content;
  35822. word32 contentSz;
  35823. int contentOID;
  35824. int encryptOID;
  35825. byte* encryptionKey;
  35826. word32 encryptionKeySz;
  35827. PKCS7Attrib* attribs;
  35828. word32 attribsSz;
  35829. const char* outFileName;
  35830. } pkcs7EncryptedVector;
  35831. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7encrypted_test(void)
  35832. {
  35833. wc_test_ret_t ret = 0;
  35834. int i, testSz;
  35835. int encryptedSz, decodedSz, attribIdx;
  35836. PKCS7* pkcs7;
  35837. byte *encrypted;
  35838. byte *decoded;
  35839. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  35840. XFILE pkcs7File;
  35841. #endif
  35842. PKCS7Attrib* expectedAttrib;
  35843. PKCS7DecodedAttrib* decodedAttrib;
  35844. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  35845. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  35846. 0x72,0x6c,0x64
  35847. };
  35848. #ifndef NO_DES3
  35849. byte desKey[] = {
  35850. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  35851. };
  35852. byte des3Key[] = {
  35853. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  35854. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  35855. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  35856. };
  35857. #endif
  35858. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  35859. #ifdef WOLFSSL_AES_128
  35860. byte aes128Key[] = {
  35861. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  35862. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  35863. };
  35864. #endif
  35865. #ifdef WOLFSSL_AES_192
  35866. byte aes192Key[] = {
  35867. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  35868. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  35869. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  35870. };
  35871. #endif
  35872. #ifdef WOLFSSL_AES_256
  35873. byte aes256Key[] = {
  35874. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  35875. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  35876. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  35877. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  35878. };
  35879. #endif
  35880. #ifdef WOLFSSL_AES_256
  35881. /* Attribute example from RFC 4134, Section 7.2
  35882. * OID = 1.2.5555
  35883. * OCTET STRING = 'This is a test General ASN Attribute, number 1.' */
  35884. static byte genAttrOid[] = { 0x06, 0x03, 0x2a, 0xab, 0x33 };
  35885. static byte genAttr[] = { 0x04, 47,
  35886. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  35887. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  35888. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  35889. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  35890. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  35891. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x31, 0x2e };
  35892. static byte genAttrOid2[] = { 0x06, 0x03, 0x2a, 0xab, 0x34 };
  35893. static byte genAttr2[] = { 0x04, 47,
  35894. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  35895. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  35896. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  35897. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  35898. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  35899. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x32, 0x2e };
  35900. PKCS7Attrib attribs[] =
  35901. {
  35902. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) }
  35903. };
  35904. PKCS7Attrib multiAttribs[] =
  35905. {
  35906. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) },
  35907. { genAttrOid2, sizeof(genAttrOid2), genAttr2, sizeof(genAttr2) }
  35908. };
  35909. #endif
  35910. #endif /* NO_AES */
  35911. const pkcs7EncryptedVector testVectors[] =
  35912. {
  35913. #ifndef NO_DES3
  35914. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key),
  35915. NULL, 0, "pkcs7encryptedDataDES3.der"},
  35916. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey),
  35917. NULL, 0, "pkcs7encryptedDataDES.der"},
  35918. #endif /* NO_DES3 */
  35919. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  35920. #ifdef WOLFSSL_AES_128
  35921. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  35922. sizeof(aes128Key), NULL, 0, "pkcs7encryptedDataAES128CBC.der"},
  35923. #endif
  35924. #ifdef WOLFSSL_AES_192
  35925. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  35926. sizeof(aes192Key), NULL, 0, "pkcs7encryptedDataAES192CBC.der"},
  35927. #endif
  35928. #ifdef WOLFSSL_AES_256
  35929. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  35930. sizeof(aes256Key), NULL, 0, "pkcs7encryptedDataAES256CBC.der"},
  35931. /* test with optional unprotected attributes */
  35932. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  35933. sizeof(aes256Key), attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35934. "pkcs7encryptedDataAES256CBC_attribs.der"},
  35935. /* test with multiple optional unprotected attributes */
  35936. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  35937. sizeof(aes256Key), multiAttribs,
  35938. (sizeof(multiAttribs)/sizeof(PKCS7Attrib)),
  35939. "pkcs7encryptedDataAES256CBC_multi_attribs.der"},
  35940. /* test with contentType set to FirmwarePkgData */
  35941. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256CBCb, aes256Key,
  35942. sizeof(aes256Key), NULL, 0,
  35943. "pkcs7encryptedDataAES256CBC_firmwarePkgData.der"},
  35944. #endif
  35945. #endif /* !NO_AES && HAVE_AES_CBC */
  35946. };
  35947. encrypted = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35948. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35949. if ((! encrypted) || (! decoded)) {
  35950. ERROR_OUT(MEMORY_E, out);
  35951. }
  35952. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  35953. for (i = 0; i < testSz; i++) {
  35954. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  35955. if (pkcs7 == NULL) {
  35956. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  35957. }
  35958. pkcs7->content = (byte*)testVectors[i].content;
  35959. pkcs7->contentSz = testVectors[i].contentSz;
  35960. pkcs7->contentOID = testVectors[i].contentOID;
  35961. pkcs7->encryptOID = testVectors[i].encryptOID;
  35962. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  35963. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  35964. pkcs7->unprotectedAttribs = testVectors[i].attribs;
  35965. pkcs7->unprotectedAttribsSz = testVectors[i].attribsSz;
  35966. /* encode encryptedData */
  35967. encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  35968. PKCS7_BUF_SIZE);
  35969. if (encryptedSz <= 0) {
  35970. wc_PKCS7_Free(pkcs7);
  35971. ERROR_OUT(WC_TEST_RET_ENC_EC(encryptedSz), out);
  35972. }
  35973. /* decode encryptedData */
  35974. #ifndef NO_PKCS7_STREAM
  35975. { /* test reading byte by byte */
  35976. int z;
  35977. for (z = 0; z < encryptedSz; z++) {
  35978. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted + z, 1,
  35979. decoded, PKCS7_BUF_SIZE);
  35980. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  35981. printf("unexpected error %d\n", decodedSz);
  35982. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  35983. }
  35984. }
  35985. /* test decode result */
  35986. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  35987. printf("stream read failed\n");
  35988. wc_PKCS7_Free(pkcs7);
  35989. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  35990. }
  35991. }
  35992. #endif
  35993. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  35994. decoded, PKCS7_BUF_SIZE);
  35995. if (decodedSz <= 0){
  35996. wc_PKCS7_Free(pkcs7);
  35997. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  35998. }
  35999. /* test decode result */
  36000. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  36001. wc_PKCS7_Free(pkcs7);
  36002. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  36003. }
  36004. /* verify decoded unprotected attributes */
  36005. if (pkcs7->decodedAttrib != NULL) {
  36006. decodedAttrib = pkcs7->decodedAttrib;
  36007. attribIdx = 1;
  36008. while (decodedAttrib != NULL) {
  36009. /* expected attribute, stored list is reversed */
  36010. expectedAttrib = &(pkcs7->unprotectedAttribs
  36011. [pkcs7->unprotectedAttribsSz - attribIdx]);
  36012. /* verify oid */
  36013. if (XMEMCMP(decodedAttrib->oid, expectedAttrib->oid,
  36014. decodedAttrib->oidSz) != 0) {
  36015. wc_PKCS7_Free(pkcs7);
  36016. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  36017. }
  36018. /* verify value */
  36019. if (XMEMCMP(decodedAttrib->value, expectedAttrib->value,
  36020. decodedAttrib->valueSz) != 0) {
  36021. wc_PKCS7_Free(pkcs7);
  36022. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  36023. }
  36024. decodedAttrib = decodedAttrib->next;
  36025. attribIdx++;
  36026. }
  36027. }
  36028. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  36029. /* output pkcs7 envelopedData for external testing */
  36030. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  36031. if (!pkcs7File) {
  36032. wc_PKCS7_Free(pkcs7);
  36033. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  36034. }
  36035. ret = (int)XFWRITE(encrypted, encryptedSz, 1, pkcs7File);
  36036. if (ret < 0)
  36037. ret = WC_TEST_RET_ENC_ERRNO;
  36038. else
  36039. ret = 0;
  36040. XFCLOSE(pkcs7File);
  36041. #endif
  36042. wc_PKCS7_Free(pkcs7);
  36043. }
  36044. out:
  36045. if (encrypted)
  36046. XFREE(encrypted, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36047. if (decoded)
  36048. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36049. return ret;
  36050. }
  36051. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  36052. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  36053. typedef struct {
  36054. const byte* content;
  36055. word32 contentSz;
  36056. int contentOID;
  36057. const char* outFileName;
  36058. } pkcs7CompressedVector;
  36059. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7compressed_test(void)
  36060. {
  36061. wc_test_ret_t ret = 0;
  36062. int i, testSz;
  36063. int compressedSz, decodedSz;
  36064. PKCS7* pkcs7;
  36065. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  36066. byte *compressed;
  36067. byte *decoded;
  36068. #else
  36069. byte compressed[PKCS7_BUF_SIZE];
  36070. byte decoded[PKCS7_BUF_SIZE];
  36071. #endif
  36072. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  36073. XFILE pkcs7File;
  36074. #endif
  36075. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  36076. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  36077. 0x72,0x6c,0x64
  36078. };
  36079. const pkcs7CompressedVector testVectors[] =
  36080. {
  36081. {data, (word32)sizeof(data), DATA,
  36082. "pkcs7compressedData_data_zlib.der"},
  36083. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA,
  36084. "pkcs7compressedData_firmwarePkgData_zlib.der"},
  36085. };
  36086. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  36087. compressed = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36088. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36089. if ((! compressed) || (! decoded)) {
  36090. ERROR_OUT(MEMORY_E, out);
  36091. }
  36092. #endif
  36093. testSz = sizeof(testVectors) / sizeof(pkcs7CompressedVector);
  36094. for (i = 0; i < testSz; i++) {
  36095. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  36096. if (pkcs7 == NULL) {
  36097. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  36098. }
  36099. pkcs7->content = (byte*)testVectors[i].content;
  36100. pkcs7->contentSz = testVectors[i].contentSz;
  36101. pkcs7->contentOID = testVectors[i].contentOID;
  36102. /* encode compressedData */
  36103. compressedSz = wc_PKCS7_EncodeCompressedData(pkcs7, compressed,
  36104. PKCS7_BUF_SIZE);
  36105. if (compressedSz <= 0) {
  36106. wc_PKCS7_Free(pkcs7);
  36107. ERROR_OUT(WC_TEST_RET_ENC_EC(compressedSz), out);
  36108. }
  36109. /* decode compressedData */
  36110. decodedSz = wc_PKCS7_DecodeCompressedData(pkcs7, compressed,
  36111. compressedSz, decoded,
  36112. PKCS7_BUF_SIZE);
  36113. if (decodedSz <= 0){
  36114. wc_PKCS7_Free(pkcs7);
  36115. ERROR_OUT(WC_TEST_RET_ENC_EC(decodedSz), out);
  36116. }
  36117. /* test decode result */
  36118. if (XMEMCMP(decoded, testVectors[i].content,
  36119. testVectors[i].contentSz) != 0) {
  36120. wc_PKCS7_Free(pkcs7);
  36121. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  36122. }
  36123. /* make sure content type is the same */
  36124. if (testVectors[i].contentOID != pkcs7->contentOID) {
  36125. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  36126. }
  36127. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  36128. /* output pkcs7 compressedData for external testing */
  36129. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  36130. if (!pkcs7File) {
  36131. wc_PKCS7_Free(pkcs7);
  36132. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  36133. }
  36134. ret = (int)XFWRITE(compressed, compressedSz, 1, pkcs7File);
  36135. if (ret < 0)
  36136. ret = WC_TEST_RET_ENC_ERRNO;
  36137. else
  36138. ret = 0;
  36139. XFCLOSE(pkcs7File);
  36140. #endif
  36141. wc_PKCS7_Free(pkcs7);
  36142. }
  36143. out:
  36144. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  36145. if (compressed)
  36146. XFREE(compressed, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36147. if (decoded)
  36148. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36149. #endif
  36150. return ret;
  36151. } /* pkcs7compressed_test() */
  36152. #undef PKCS7_BUF_SIZE
  36153. #endif /* HAVE_LIBZ */
  36154. typedef struct {
  36155. const byte* content;
  36156. word32 contentSz;
  36157. int hashOID;
  36158. int signOID;
  36159. byte* privateKey;
  36160. word32 privateKeySz;
  36161. byte* cert;
  36162. size_t certSz;
  36163. byte* caCert;
  36164. size_t caCertSz;
  36165. PKCS7Attrib* signedAttribs;
  36166. word32 signedAttribsSz;
  36167. const char* outFileName;
  36168. int contentOID;
  36169. byte* contentType;
  36170. word32 contentTypeSz;
  36171. int sidType;
  36172. int encryptOID; /* for single-shot encrypt alg OID */
  36173. int encCompFlag; /* for single-shot. 1 = enc, 2 = comp, 3 = both*/
  36174. byte* encryptKey; /* for single-shot, encryptedData */
  36175. word32 encryptKeySz; /* for single-shot, encryptedData */
  36176. PKCS7Attrib* unprotectedAttribs; /* for single-shot, encryptedData */
  36177. word32 unprotectedAttribsSz; /* for single-shot, encryptedData */
  36178. word16 detachedSignature; /* generate detached signature (0:1) */
  36179. } pkcs7SignedVector;
  36180. static wc_test_ret_t pkcs7signed_run_vectors(
  36181. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  36182. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  36183. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  36184. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  36185. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  36186. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  36187. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  36188. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  36189. {
  36190. wc_test_ret_t ret;
  36191. int testSz = 0, i;
  36192. int encodedSz;
  36193. byte* out = NULL;
  36194. word32 outSz;
  36195. WC_RNG rng;
  36196. PKCS7* pkcs7 = NULL;
  36197. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  36198. XFILE file;
  36199. #endif
  36200. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  36201. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  36202. 0x72,0x6c,0x64
  36203. };
  36204. static byte transIdOid[] =
  36205. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  36206. 0x09, 0x07 };
  36207. static byte messageTypeOid[] =
  36208. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  36209. 0x09, 0x02 };
  36210. static byte senderNonceOid[] =
  36211. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  36212. 0x09, 0x05 };
  36213. #ifndef NO_SHA
  36214. static byte transId[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  36215. #else
  36216. static byte transId[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  36217. #endif
  36218. static byte messageType[] = { 0x13, 2, '1', '9' };
  36219. static byte senderNonce[PKCS7_NONCE_SZ + 2];
  36220. static PKCS7Attrib attribs[] =
  36221. {
  36222. { transIdOid, sizeof(transIdOid), transId,
  36223. sizeof(transId) - 1 }, /* take off the null */
  36224. { messageTypeOid, sizeof(messageTypeOid), messageType,
  36225. sizeof(messageType) },
  36226. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  36227. sizeof(senderNonce) }
  36228. };
  36229. /* for testing custom contentType, FirmwarePkgData */
  36230. static byte customContentType[] = { 0x06, 0x0B, 0x2A, 0x86,
  36231. 0x48, 0x86, 0xF7, 0x0D,
  36232. 0x01, 0x09, 0x10, 0x01, 0x10 };
  36233. #define MAX_TESTVECTORS_LEN 20
  36234. #define ADD_PKCS7SIGNEDVECTOR(...) { \
  36235. pkcs7SignedVector _this_vector = { __VA_ARGS__ }; \
  36236. if (testSz == MAX_TESTVECTORS_LEN) { \
  36237. ret = WC_TEST_RET_ENC_NC; \
  36238. goto out; \
  36239. } \
  36240. XMEMCPY(&testVectors[testSz++], &_this_vector, \
  36241. sizeof _this_vector); \
  36242. }
  36243. pkcs7SignedVector *testVectors = NULL;
  36244. XMEMSET(&rng, 0, sizeof(rng));
  36245. testVectors = (pkcs7SignedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  36246. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36247. if (testVectors == NULL) {
  36248. ret = WC_TEST_RET_ENC_ERRNO;
  36249. goto out;
  36250. }
  36251. {
  36252. #ifndef NO_RSA
  36253. #ifndef NO_SHA
  36254. /* RSA with SHA */
  36255. ADD_PKCS7SIGNEDVECTOR(
  36256. data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  36257. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36258. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36259. "pkcs7signedData_RSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0, NULL,
  36260. 0, 0);
  36261. /* RSA with SHA, no signed attributes */
  36262. ADD_PKCS7SIGNEDVECTOR(
  36263. data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  36264. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz,
  36265. NULL, 0, NULL, 0,
  36266. "pkcs7signedData_RSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  36267. NULL, 0, 0);
  36268. #endif
  36269. #ifdef WOLFSSL_SHA224
  36270. /* RSA with SHA224 */
  36271. ADD_PKCS7SIGNEDVECTOR(
  36272. data, (word32)sizeof(data), SHA224h, RSAk, rsaClientPrivKeyBuf,
  36273. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36274. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36275. "pkcs7signedData_RSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  36276. NULL, 0, 0);
  36277. #endif
  36278. #ifndef NO_SHA256
  36279. /* RSA with SHA256 */
  36280. ADD_PKCS7SIGNEDVECTOR(
  36281. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36282. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36283. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36284. "pkcs7signedData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  36285. NULL, 0, 0);
  36286. /* RSA with SHA256, detached signature */
  36287. ADD_PKCS7SIGNEDVECTOR(
  36288. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36289. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36290. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36291. "pkcs7signedData_RSA_SHA256_detachedSig.der", 0, NULL, 0, 0, 0, 0,
  36292. NULL, 0, NULL, 0, 1);
  36293. /* RSA with SHA256 and SubjectKeyIdentifier in SignerIdentifier */
  36294. ADD_PKCS7SIGNEDVECTOR(
  36295. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36296. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36297. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36298. "pkcs7signedData_RSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  36299. NULL, 0, NULL, 0, 0);
  36300. /* RSA with SHA256 and custom contentType */
  36301. ADD_PKCS7SIGNEDVECTOR(
  36302. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36303. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36304. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36305. "pkcs7signedData_RSA_SHA256_custom_contentType.der", 0,
  36306. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  36307. NULL, 0, 0);
  36308. /* RSA with SHA256 and FirmwarePkgData contentType */
  36309. ADD_PKCS7SIGNEDVECTOR(
  36310. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36311. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36312. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36313. "pkcs7signedData_RSA_SHA256_firmwarePkgData.der",
  36314. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  36315. /* RSA with SHA256 using server cert and ca cert */
  36316. ADD_PKCS7SIGNEDVECTOR(
  36317. data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  36318. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  36319. rsaCaCertBuf, rsaCaCertBufSz,
  36320. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36321. "pkcs7signedData_RSA_SHA256_with_ca_cert.der", 0, NULL, 0, 0, 0, 0,
  36322. NULL, 0, NULL, 0, 0);
  36323. #endif
  36324. #if defined(WOLFSSL_SHA384)
  36325. /* RSA with SHA384 */
  36326. ADD_PKCS7SIGNEDVECTOR(
  36327. data, (word32)sizeof(data), SHA384h, RSAk, rsaClientPrivKeyBuf,
  36328. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36329. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36330. "pkcs7signedData_RSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  36331. NULL, 0, 0);
  36332. #endif
  36333. #if defined(WOLFSSL_SHA512)
  36334. /* RSA with SHA512 */
  36335. ADD_PKCS7SIGNEDVECTOR(
  36336. data, (word32)sizeof(data), SHA512h, RSAk, rsaClientPrivKeyBuf,
  36337. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36338. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36339. "pkcs7signedData_RSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  36340. NULL, 0, 0);
  36341. #endif
  36342. #endif /* NO_RSA */
  36343. #ifdef HAVE_ECC
  36344. #ifndef NO_SHA
  36345. /* ECDSA with SHA */
  36346. ADD_PKCS7SIGNEDVECTOR(
  36347. data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  36348. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36349. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36350. "pkcs7signedData_ECDSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  36351. NULL, 0, 0);
  36352. /* ECDSA with SHA, no signed attributes */
  36353. ADD_PKCS7SIGNEDVECTOR(
  36354. data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  36355. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz,
  36356. NULL, 0, NULL, 0,
  36357. "pkcs7signedData_ECDSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  36358. NULL, 0, 0);
  36359. #endif
  36360. #ifdef WOLFSSL_SHA224
  36361. /* ECDSA with SHA224 */
  36362. ADD_PKCS7SIGNEDVECTOR(
  36363. data, (word32)sizeof(data), SHA224h, ECDSAk, eccClientPrivKeyBuf,
  36364. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36365. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36366. "pkcs7signedData_ECDSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  36367. NULL, 0, 0);
  36368. #endif
  36369. #ifndef NO_SHA256
  36370. /* ECDSA with SHA256 */
  36371. ADD_PKCS7SIGNEDVECTOR(
  36372. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36373. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36374. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36375. "pkcs7signedData_ECDSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  36376. NULL, 0, 0);
  36377. /* ECDSA with SHA256 and SubjectKeyIdentifier in SigherIdentifier */
  36378. ADD_PKCS7SIGNEDVECTOR(
  36379. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36380. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36381. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36382. "pkcs7signedData_ECDSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  36383. NULL, 0, NULL, 0, 0);
  36384. /* ECDSA with SHA256 and custom contentType */
  36385. ADD_PKCS7SIGNEDVECTOR(
  36386. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36387. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36388. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36389. "pkcs7signedData_ECDSA_SHA256_custom_contentType.der", 0,
  36390. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  36391. NULL, 0, 0);
  36392. /* ECDSA with SHA256 and FirmwarePkgData contentType */
  36393. ADD_PKCS7SIGNEDVECTOR(
  36394. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36395. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36396. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36397. "pkcs7signedData_ECDSA_SHA256_firmwarePkgData.der",
  36398. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  36399. #endif
  36400. #ifdef WOLFSSL_SHA384
  36401. /* ECDSA with SHA384 */
  36402. ADD_PKCS7SIGNEDVECTOR(
  36403. data, (word32)sizeof(data), SHA384h, ECDSAk, eccClientPrivKeyBuf,
  36404. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36405. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36406. "pkcs7signedData_ECDSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  36407. NULL, 0, 0);
  36408. #endif
  36409. #ifdef WOLFSSL_SHA512
  36410. /* ECDSA with SHA512 */
  36411. ADD_PKCS7SIGNEDVECTOR(
  36412. data, (word32)sizeof(data), SHA512h, ECDSAk, eccClientPrivKeyBuf,
  36413. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36414. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36415. "pkcs7signedData_ECDSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  36416. NULL, 0, 0);
  36417. #endif
  36418. #endif /* HAVE_ECC */
  36419. };
  36420. #undef MAX_TESTVECTORS_LEN
  36421. #undef ADD_PKCS7SIGNEDVECTOR
  36422. outSz = FOURK_BUF;
  36423. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36424. if (out == NULL)
  36425. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  36426. XMEMSET(out, 0, outSz);
  36427. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  36428. if (ret < 0)
  36429. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36430. #ifndef HAVE_FIPS
  36431. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  36432. #else
  36433. ret = wc_InitRng(&rng);
  36434. #endif
  36435. if (ret != 0)
  36436. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36437. for (i = 0; i < testSz; i++) {
  36438. if (pkcs7)
  36439. wc_PKCS7_Free(pkcs7);
  36440. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  36441. if (pkcs7 == NULL)
  36442. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  36443. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  36444. (word32)testVectors[i].certSz);
  36445. if (ret != 0)
  36446. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36447. /* load CA certificate, if present */
  36448. if (testVectors[i].caCert != NULL) {
  36449. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  36450. (word32)testVectors[i].caCertSz);
  36451. if (ret != 0)
  36452. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36453. }
  36454. pkcs7->rng = &rng;
  36455. pkcs7->content = (byte*)testVectors[i].content;
  36456. pkcs7->contentSz = testVectors[i].contentSz;
  36457. pkcs7->contentOID = testVectors[i].contentOID;
  36458. pkcs7->hashOID = testVectors[i].hashOID;
  36459. pkcs7->encryptOID = testVectors[i].signOID;
  36460. pkcs7->privateKey = testVectors[i].privateKey;
  36461. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  36462. pkcs7->signedAttribs = testVectors[i].signedAttribs;
  36463. pkcs7->signedAttribsSz = testVectors[i].signedAttribsSz;
  36464. /* optional custom contentType, default is DATA,
  36465. overrides contentOID if set */
  36466. if (testVectors[i].contentType != NULL) {
  36467. ret = wc_PKCS7_SetContentType(pkcs7, testVectors[i].contentType,
  36468. testVectors[i].contentTypeSz);
  36469. if (ret != 0)
  36470. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36471. }
  36472. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  36473. default is IssuerAndSerialNumber */
  36474. if (testVectors[i].sidType == CMS_SKID) {
  36475. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  36476. if (ret != 0)
  36477. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36478. }
  36479. /* generate senderNonce */
  36480. {
  36481. senderNonce[0] = 0x04;
  36482. senderNonce[1] = PKCS7_NONCE_SZ;
  36483. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  36484. if (ret != 0)
  36485. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36486. }
  36487. /* generate transactionID (used with SCEP) */
  36488. {
  36489. #ifndef NO_SHA
  36490. wc_Sha sha;
  36491. byte digest[WC_SHA_DIGEST_SIZE];
  36492. #else
  36493. wc_Sha256 sha;
  36494. byte digest[WC_SHA256_DIGEST_SIZE];
  36495. #endif
  36496. int j,k;
  36497. transId[0] = 0x13;
  36498. transId[1] = sizeof(digest) * 2;
  36499. #ifndef NO_SHA
  36500. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  36501. if (ret != 0)
  36502. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36503. wc_ShaUpdate(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  36504. wc_ShaFinal(&sha, digest);
  36505. wc_ShaFree(&sha);
  36506. #else
  36507. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  36508. if (ret != 0)
  36509. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36510. wc_Sha256Update(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  36511. wc_Sha256Final(&sha, digest);
  36512. wc_Sha256Free(&sha);
  36513. #endif
  36514. for (j = 0, k = 2; j < (int)sizeof(digest); j++, k += 2) {
  36515. #if defined(WOLF_C89)
  36516. XSPRINTF((char*)&transId[k], "%02x", digest[j]);
  36517. #else
  36518. (void)XSNPRINTF((char*)&transId[k], 3, "%02x", digest[j]);
  36519. #endif
  36520. }
  36521. }
  36522. /* enable detached signature generation, if set */
  36523. if (testVectors[i].detachedSignature == 1) {
  36524. ret = wc_PKCS7_SetDetached(pkcs7, 1);
  36525. if (ret != 0)
  36526. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36527. }
  36528. encodedSz = wc_PKCS7_EncodeSignedData(pkcs7, out, outSz);
  36529. if (encodedSz < 0)
  36530. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  36531. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  36532. /* write PKCS#7 to output file for more testing */
  36533. file = XFOPEN(testVectors[i].outFileName, "wb");
  36534. if (!file) {
  36535. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  36536. }
  36537. ret = (int)XFWRITE(out, 1, encodedSz, file);
  36538. XFCLOSE(file);
  36539. if (ret != (int)encodedSz)
  36540. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  36541. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  36542. wc_PKCS7_Free(pkcs7);
  36543. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  36544. if (pkcs7 == NULL)
  36545. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  36546. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  36547. if (testVectors[i].detachedSignature == 1) {
  36548. /* set content for verifying detached signatures */
  36549. pkcs7->content = (byte*)testVectors[i].content;
  36550. pkcs7->contentSz = testVectors[i].contentSz;
  36551. }
  36552. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  36553. if (ret < 0)
  36554. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36555. /* verify contentType extracted successfully for custom content types */
  36556. if (testVectors[i].contentTypeSz > 0) {
  36557. if (pkcs7->contentTypeSz != testVectors[i].contentTypeSz) {
  36558. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  36559. } else if (XMEMCMP(pkcs7->contentType, testVectors[i].contentType,
  36560. pkcs7->contentTypeSz) != 0) {
  36561. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  36562. }
  36563. }
  36564. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0)
  36565. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  36566. {
  36567. /* check getting signed attributes */
  36568. #ifndef NO_SHA
  36569. byte buf[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  36570. #else
  36571. byte buf[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  36572. #endif
  36573. byte* oidPt = transIdOid + 2; /* skip object id tag and size */
  36574. int oidSz = (int)sizeof(transIdOid) - 2;
  36575. int bufSz = 0;
  36576. if (testVectors[i].signedAttribs != NULL) {
  36577. ret = wc_PKCS7_GetAttributeValue(pkcs7, oidPt, oidSz,
  36578. NULL, (word32*)&bufSz);
  36579. if (ret != LENGTH_ONLY_E)
  36580. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36581. ret = 0;
  36582. }
  36583. if (bufSz > (int)sizeof(buf))
  36584. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  36585. bufSz = wc_PKCS7_GetAttributeValue(pkcs7, oidPt, oidSz,
  36586. buf, (word32*)&bufSz);
  36587. if ((testVectors[i].signedAttribs != NULL && bufSz < 0) ||
  36588. (testVectors[i].signedAttribs == NULL && bufSz > 0))
  36589. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  36590. }
  36591. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  36592. file = XFOPEN("./pkcs7cert.der", "wb");
  36593. if (!file)
  36594. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  36595. ret = (int)XFWRITE(pkcs7->singleCert, 1, pkcs7->singleCertSz, file);
  36596. if (ret < 0)
  36597. ret = WC_TEST_RET_ENC_ERRNO;
  36598. else
  36599. ret = 0;
  36600. XFCLOSE(file);
  36601. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  36602. }
  36603. out:
  36604. if (pkcs7 != NULL)
  36605. wc_PKCS7_Free(pkcs7);
  36606. if (out != NULL)
  36607. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36608. if (testVectors != NULL)
  36609. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36610. wc_FreeRng(&rng);
  36611. if (ret > 0)
  36612. return 0;
  36613. (void)rsaClientCertBuf;
  36614. (void)rsaClientCertBufSz;
  36615. (void)rsaClientPrivKeyBuf;
  36616. (void)rsaClientPrivKeyBufSz;
  36617. (void)rsaServerCertBuf;
  36618. (void)rsaServerCertBufSz;
  36619. (void)rsaServerPrivKeyBuf;
  36620. (void)rsaServerPrivKeyBufSz;
  36621. (void)rsaCaCertBuf;
  36622. (void)rsaCaCertBufSz;
  36623. (void)rsaCaPrivKeyBuf;
  36624. (void)rsaCaPrivKeyBufSz;
  36625. (void)eccClientCertBuf;
  36626. (void)eccClientCertBufSz;
  36627. (void)eccClientPrivKeyBuf;
  36628. (void)eccClientPrivKeyBufSz;
  36629. return ret;
  36630. }
  36631. static wc_test_ret_t pkcs7signed_run_SingleShotVectors(
  36632. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  36633. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  36634. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  36635. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  36636. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  36637. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  36638. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  36639. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  36640. {
  36641. wc_test_ret_t ret;
  36642. int testSz = 0, i;
  36643. int encodedSz;
  36644. byte* out = NULL;
  36645. word32 outSz;
  36646. WC_RNG rng;
  36647. PKCS7* pkcs7 = NULL;
  36648. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  36649. XFILE file;
  36650. #endif
  36651. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA) && \
  36652. !defined(NO_PKCS7_ENCRYPTED_DATA)
  36653. byte* encryptedTmp = NULL;
  36654. int encryptedTmpSz;
  36655. #endif
  36656. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  36657. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  36658. 0x72,0x6c,0x64
  36659. };
  36660. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  36661. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  36662. static byte aes256Key[] = {
  36663. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  36664. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  36665. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  36666. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  36667. };
  36668. #endif
  36669. static byte messageTypeOid[] =
  36670. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  36671. 0x09, 0x02 };
  36672. static byte messageType[] = { 0x13, 2, '1', '9' };
  36673. PKCS7Attrib attribs[] =
  36674. {
  36675. { messageTypeOid, sizeof(messageTypeOid), messageType,
  36676. sizeof(messageType) },
  36677. };
  36678. #define MAX_TESTVECTORS_LEN 19
  36679. #define ADD_PKCS7SIGNEDVECTOR(...) { \
  36680. pkcs7SignedVector _this_vector = { __VA_ARGS__ }; \
  36681. if (testSz == MAX_TESTVECTORS_LEN) { \
  36682. ret = WC_TEST_RET_ENC_NC; \
  36683. goto out; \
  36684. } \
  36685. XMEMCPY(&testVectors[testSz++], &_this_vector, \
  36686. sizeof _this_vector); \
  36687. }
  36688. pkcs7SignedVector *testVectors = NULL;
  36689. XMEMSET(&rng, 0, sizeof(rng));
  36690. testVectors = (pkcs7SignedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  36691. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36692. if (testVectors == NULL) {
  36693. ret = WC_TEST_RET_ENC_ERRNO;
  36694. goto out;
  36695. }
  36696. {
  36697. #ifndef NO_RSA
  36698. #ifndef NO_SHA256
  36699. /* Signed FirmwarePkgData, RSA, SHA256, no attribs */
  36700. ADD_PKCS7SIGNEDVECTOR(
  36701. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36702. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36703. NULL, 0,
  36704. "pkcs7signedFirmwarePkgData_RSA_SHA256_noattr.der", 0, NULL, 0, 0,
  36705. 0, 0, NULL, 0, NULL, 0, 0);
  36706. /* Signed FirmwarePkgData, RSA, SHA256, attrs */
  36707. ADD_PKCS7SIGNEDVECTOR(
  36708. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36709. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36710. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36711. "pkcs7signedFirmwarePkgData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0,
  36712. NULL, 0, NULL, 0, 0);
  36713. /* Signed FirmwarePkgData, RSA, SHA256, SubjectKeyIdentifier, attrs */
  36714. ADD_PKCS7SIGNEDVECTOR(
  36715. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36716. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36717. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36718. "pkcs7signedFirmwarePkgData_RSA_SHA256_SKID.der", 0, NULL,
  36719. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0);
  36720. /* Signed FirmwraePkgData, RSA, SHA256, server cert and ca cert, attr */
  36721. ADD_PKCS7SIGNEDVECTOR(
  36722. data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  36723. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  36724. rsaCaCertBuf, rsaCaCertBufSz,
  36725. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36726. "pkcs7signedFirmwarePkgData_RSA_SHA256_with_ca_cert.der", 0, NULL,
  36727. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  36728. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  36729. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  36730. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, no attribs */
  36731. ADD_PKCS7SIGNEDVECTOR(
  36732. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36733. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36734. NULL, 0,
  36735. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  36736. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0);
  36737. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, attribs */
  36738. ADD_PKCS7SIGNEDVECTOR(
  36739. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36740. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36741. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36742. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256.der", 0,
  36743. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  36744. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  36745. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  36746. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  36747. /* Signed Compressed FirmwarePkgData, RSA, SHA256, no attribs */
  36748. ADD_PKCS7SIGNEDVECTOR(
  36749. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36750. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36751. NULL, 0,
  36752. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  36753. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  36754. /* Signed Compressed FirmwarePkgData, RSA, SHA256, attribs */
  36755. ADD_PKCS7SIGNEDVECTOR(
  36756. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36757. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36758. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36759. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256.der", 0,
  36760. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  36761. #ifndef NO_PKCS7_ENCRYPTED_DATA
  36762. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  36763. no attribs */
  36764. ADD_PKCS7SIGNEDVECTOR(
  36765. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36766. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36767. NULL, 0,
  36768. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256_noattr.der",
  36769. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  36770. 0, 0);
  36771. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  36772. attribs */
  36773. ADD_PKCS7SIGNEDVECTOR(
  36774. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36775. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36776. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36777. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256.der",
  36778. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  36779. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  36780. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  36781. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  36782. #endif /* NO_SHA256 */
  36783. #endif /* NO_RSA */
  36784. #ifdef HAVE_ECC
  36785. #ifndef NO_SHA256
  36786. /* Signed FirmwarePkgData, ECDSA, SHA256, no attribs */
  36787. ADD_PKCS7SIGNEDVECTOR(
  36788. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36789. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36790. NULL, 0,
  36791. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  36792. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  36793. /* Signed FirmwarePkgData, ECDSA, SHA256, attribs */
  36794. ADD_PKCS7SIGNEDVECTOR(
  36795. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36796. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36797. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36798. "pkcs7signedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  36799. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  36800. /* Signed FirmwarePkgData, ECDSA, SHA256, SubjectKeyIdentifier, attr */
  36801. ADD_PKCS7SIGNEDVECTOR(
  36802. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36803. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36804. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36805. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_SKID.der", 0, NULL,
  36806. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0);
  36807. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  36808. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  36809. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, no attribs */
  36810. ADD_PKCS7SIGNEDVECTOR(
  36811. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36812. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36813. NULL, 0,
  36814. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  36815. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0);
  36816. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, attribs */
  36817. ADD_PKCS7SIGNEDVECTOR(
  36818. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36819. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36820. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36821. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  36822. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  36823. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  36824. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  36825. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  36826. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, no attribs */
  36827. ADD_PKCS7SIGNEDVECTOR(
  36828. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36829. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36830. NULL, 0,
  36831. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  36832. 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  36833. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, attrib */
  36834. ADD_PKCS7SIGNEDVECTOR(
  36835. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36836. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36837. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36838. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  36839. 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  36840. #ifndef NO_PKCS7_ENCRYPTED_DATA
  36841. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  36842. no attribs */
  36843. ADD_PKCS7SIGNEDVECTOR(
  36844. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36845. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36846. NULL, 0,
  36847. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der",
  36848. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  36849. 0, 0);
  36850. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  36851. attribs */
  36852. ADD_PKCS7SIGNEDVECTOR(
  36853. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36854. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36855. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36856. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256.der",
  36857. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  36858. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  36859. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  36860. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  36861. #endif /* NO_SHA256 */
  36862. #endif /* HAVE_ECC */
  36863. };
  36864. #undef MAX_TESTVECTORS_LEN
  36865. #undef ADD_PKCS7SIGNEDVECTOR
  36866. outSz = FOURK_BUF;
  36867. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36868. if (out == NULL)
  36869. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  36870. XMEMSET(out, 0, outSz);
  36871. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  36872. if (ret < 0)
  36873. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36874. #ifndef HAVE_FIPS
  36875. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  36876. #else
  36877. ret = wc_InitRng(&rng);
  36878. #endif
  36879. if (ret != 0)
  36880. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36881. for (i = 0; i < testSz; i++) {
  36882. if (pkcs7)
  36883. wc_PKCS7_Free(pkcs7);
  36884. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  36885. if (pkcs7 == NULL)
  36886. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  36887. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  36888. (word32)testVectors[i].certSz);
  36889. if (ret != 0)
  36890. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36891. /* load CA certificate, if present */
  36892. if (testVectors[i].caCert != NULL) {
  36893. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  36894. (word32)testVectors[i].caCertSz);
  36895. if (ret != 0)
  36896. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36897. }
  36898. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  36899. default is IssuerAndSerialNumber */
  36900. if (testVectors[i].sidType == CMS_SKID) {
  36901. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  36902. if (ret != 0)
  36903. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36904. }
  36905. if (testVectors[i].encCompFlag == 0) {
  36906. /* encode Signed FirmwarePkgData */
  36907. encodedSz = wc_PKCS7_EncodeSignedFPD(pkcs7,
  36908. testVectors[i].privateKey, testVectors[i].privateKeySz,
  36909. testVectors[i].signOID, testVectors[i].hashOID,
  36910. (byte*)testVectors[i].content, testVectors[i].contentSz,
  36911. testVectors[i].signedAttribs,
  36912. testVectors[i].signedAttribsSz, out, outSz);
  36913. if (encodedSz < 0)
  36914. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  36915. #ifndef NO_PKCS7_ENCRYPTED_DATA
  36916. } else if (testVectors[i].encCompFlag == 1) {
  36917. /* encode Signed Encrypted FirmwarePkgData */
  36918. encodedSz = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7,
  36919. testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  36920. testVectors[i].privateKey, testVectors[i].privateKeySz,
  36921. testVectors[i].encryptOID, testVectors[i].signOID,
  36922. testVectors[i].hashOID, (byte*)testVectors[i].content,
  36923. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  36924. testVectors[i].unprotectedAttribsSz,
  36925. testVectors[i].signedAttribs,
  36926. testVectors[i].signedAttribsSz, out, outSz);
  36927. if (encodedSz <= 0)
  36928. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  36929. #endif
  36930. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  36931. } else if (testVectors[i].encCompFlag == 2) {
  36932. /* encode Signed Compressed FirmwarePkgData */
  36933. encodedSz = wc_PKCS7_EncodeSignedCompressedFPD(pkcs7,
  36934. testVectors[i].privateKey, testVectors[i].privateKeySz,
  36935. testVectors[i].signOID, testVectors[i].hashOID,
  36936. (byte*)testVectors[i].content, testVectors[i].contentSz,
  36937. testVectors[i].signedAttribs,
  36938. testVectors[i].signedAttribsSz, out, outSz);
  36939. if (encodedSz <= 0)
  36940. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  36941. #ifndef NO_PKCS7_ENCRYPTED_DATA
  36942. } else if (testVectors[i].encCompFlag == 3) {
  36943. /* encode Signed Encrypted Compressed FirmwarePkgData */
  36944. encodedSz = wc_PKCS7_EncodeSignedEncryptedCompressedFPD(pkcs7,
  36945. testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  36946. testVectors[i].privateKey, testVectors[i].privateKeySz,
  36947. testVectors[i].encryptOID, testVectors[i].signOID,
  36948. testVectors[i].hashOID, (byte*)testVectors[i].content,
  36949. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  36950. testVectors[i].unprotectedAttribsSz,
  36951. testVectors[i].signedAttribs,
  36952. testVectors[i].signedAttribsSz, out, outSz);
  36953. if (encodedSz <= 0)
  36954. ERROR_OUT(WC_TEST_RET_ENC_EC(encodedSz), out);
  36955. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  36956. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  36957. } else {
  36958. /* unsupported SignedData single-shot combination */
  36959. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  36960. }
  36961. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  36962. /* write PKCS#7 to output file for more testing */
  36963. file = XFOPEN(testVectors[i].outFileName, "wb");
  36964. if (!file)
  36965. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  36966. ret = (int)XFWRITE(out, 1, encodedSz, file);
  36967. XFCLOSE(file);
  36968. file = NULL;
  36969. if (ret != (int)encodedSz)
  36970. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  36971. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  36972. wc_PKCS7_Free(pkcs7);
  36973. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  36974. if (pkcs7 == NULL)
  36975. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  36976. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  36977. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  36978. if (ret < 0)
  36979. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36980. #ifndef NO_PKCS7_STREAM
  36981. {
  36982. word32 z;
  36983. for (z = 0; z < outSz && ret != 0; z++) {
  36984. ret = wc_PKCS7_VerifySignedData(pkcs7, out + z, 1);
  36985. if (ret < 0 && ret != WC_PKCS7_WANT_READ_E) {
  36986. printf("unexpected error %d\n", ret);
  36987. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  36988. }
  36989. }
  36990. }
  36991. #endif
  36992. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0)
  36993. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  36994. if (testVectors[i].encCompFlag == 0) {
  36995. /* verify decoded content matches expected */
  36996. if ((pkcs7->contentSz != testVectors[i].contentSz) ||
  36997. XMEMCMP(pkcs7->content, testVectors[i].content,
  36998. pkcs7->contentSz)) {
  36999. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  37000. }
  37001. }
  37002. #ifndef NO_PKCS7_ENCRYPTED_DATA
  37003. else if (testVectors[i].encCompFlag == 1) {
  37004. /* decrypt inner encryptedData */
  37005. pkcs7->encryptionKey = testVectors[i].encryptKey;
  37006. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  37007. ret = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  37008. pkcs7->contentSz, out, outSz);
  37009. if (ret < 0)
  37010. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37011. /* compare decrypted to expected */
  37012. if (((word32)ret != testVectors[i].contentSz) ||
  37013. XMEMCMP(out, testVectors[i].content, ret))
  37014. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  37015. }
  37016. #endif
  37017. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  37018. else if (testVectors[i].encCompFlag == 2) {
  37019. /* decompress inner compressedData */
  37020. ret = wc_PKCS7_DecodeCompressedData(pkcs7, pkcs7->content,
  37021. pkcs7->contentSz, out, outSz);
  37022. if (ret < 0)
  37023. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37024. /* compare decompressed to expected */
  37025. if (((word32)ret != testVectors[i].contentSz) ||
  37026. XMEMCMP(out, testVectors[i].content, ret))
  37027. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  37028. }
  37029. #ifndef NO_PKCS7_ENCRYPTED_DATA
  37030. else if (testVectors[i].encCompFlag == 3) {
  37031. encryptedTmpSz = FOURK_BUF;
  37032. encryptedTmp = (byte*)XMALLOC(encryptedTmpSz, HEAP_HINT,
  37033. DYNAMIC_TYPE_TMP_BUFFER);
  37034. if (encryptedTmp == NULL)
  37035. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, out);
  37036. XMEMSET(encryptedTmp, 0, encryptedTmpSz);
  37037. /* decrypt inner encryptedData */
  37038. pkcs7->encryptionKey = testVectors[i].encryptKey;
  37039. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  37040. encryptedTmpSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  37041. pkcs7->contentSz, encryptedTmp,
  37042. encryptedTmpSz);
  37043. if (encryptedTmpSz < 0 || pkcs7->contentOID != COMPRESSED_DATA)
  37044. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  37045. /* decompress inner compressedData */
  37046. ret = wc_PKCS7_DecodeCompressedData(pkcs7, encryptedTmp,
  37047. encryptedTmpSz, out, outSz);
  37048. if (ret < 0)
  37049. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  37050. /* compare decompressed to expected */
  37051. if (((word32)ret != testVectors[i].contentSz) ||
  37052. XMEMCMP(out, testVectors[i].content, ret))
  37053. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  37054. }
  37055. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  37056. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  37057. }
  37058. out:
  37059. if (pkcs7 != NULL)
  37060. wc_PKCS7_Free(pkcs7);
  37061. if (out != NULL)
  37062. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37063. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA) && \
  37064. !defined(NO_PKCS7_ENCRYPTED_DATA)
  37065. if (encryptedTmp != NULL)
  37066. XFREE(encryptedTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37067. #endif
  37068. if (testVectors != NULL)
  37069. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37070. wc_FreeRng(&rng);
  37071. if (ret > 0)
  37072. return 0;
  37073. (void)eccClientCertBuf;
  37074. (void)eccClientCertBufSz;
  37075. (void)eccClientPrivKeyBuf;
  37076. (void)eccClientPrivKeyBufSz;
  37077. (void)rsaClientCertBuf;
  37078. (void)rsaClientCertBufSz;
  37079. (void)rsaClientPrivKeyBuf;
  37080. (void)rsaClientPrivKeyBufSz;
  37081. (void)rsaServerCertBuf;
  37082. (void)rsaServerCertBufSz;
  37083. (void)rsaServerPrivKeyBuf;
  37084. (void)rsaServerPrivKeyBufSz;
  37085. (void)rsaCaCertBuf;
  37086. (void)rsaCaCertBufSz;
  37087. (void)rsaCaPrivKeyBuf;
  37088. (void)rsaCaPrivKeyBufSz;
  37089. return ret;
  37090. }
  37091. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs7signed_test(void)
  37092. {
  37093. wc_test_ret_t ret = 0;
  37094. byte* rsaClientCertBuf = NULL;
  37095. byte* rsaServerCertBuf = NULL;
  37096. byte* rsaCaCertBuf = NULL;
  37097. byte* eccClientCertBuf = NULL;
  37098. byte* rsaClientPrivKeyBuf = NULL;
  37099. byte* rsaServerPrivKeyBuf = NULL;
  37100. byte* rsaCaPrivKeyBuf = NULL;
  37101. byte* eccClientPrivKeyBuf = NULL;
  37102. word32 rsaClientCertBufSz = 0;
  37103. word32 rsaServerCertBufSz = 0;
  37104. word32 rsaCaCertBufSz = 0;
  37105. word32 eccClientCertBufSz = 0;
  37106. word32 rsaClientPrivKeyBufSz = 0;
  37107. word32 rsaServerPrivKeyBufSz = 0;
  37108. word32 rsaCaPrivKeyBufSz = 0;
  37109. word32 eccClientPrivKeyBufSz = 0;
  37110. #ifndef NO_RSA
  37111. /* read client RSA cert and key in DER format */
  37112. rsaClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  37113. DYNAMIC_TYPE_TMP_BUFFER);
  37114. if (rsaClientCertBuf == NULL)
  37115. ret = WC_TEST_RET_ENC_NC;
  37116. rsaClientPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  37117. DYNAMIC_TYPE_TMP_BUFFER);
  37118. if (ret == 0 && rsaClientPrivKeyBuf == NULL) {
  37119. ret = WC_TEST_RET_ENC_ERRNO;
  37120. }
  37121. rsaClientCertBufSz = FOURK_BUF;
  37122. rsaClientPrivKeyBufSz = FOURK_BUF;
  37123. /* read server RSA cert and key in DER format */
  37124. rsaServerCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  37125. DYNAMIC_TYPE_TMP_BUFFER);
  37126. if (ret == 0 && rsaServerCertBuf == NULL)
  37127. ret = WC_TEST_RET_ENC_NC;
  37128. rsaServerPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  37129. DYNAMIC_TYPE_TMP_BUFFER);
  37130. if (ret == 0 && rsaServerPrivKeyBuf == NULL) {
  37131. ret = WC_TEST_RET_ENC_ERRNO;
  37132. }
  37133. rsaServerCertBufSz = FOURK_BUF;
  37134. rsaServerPrivKeyBufSz = FOURK_BUF;
  37135. /* read CA RSA cert and key in DER format, for use with server cert */
  37136. rsaCaCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  37137. DYNAMIC_TYPE_TMP_BUFFER);
  37138. if (ret == 0 && rsaCaCertBuf == NULL)
  37139. ret = WC_TEST_RET_ENC_NC;
  37140. rsaCaPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  37141. DYNAMIC_TYPE_TMP_BUFFER);
  37142. if (ret == 0 && rsaCaPrivKeyBuf == NULL) {
  37143. ret = WC_TEST_RET_ENC_ERRNO;
  37144. }
  37145. rsaCaCertBufSz = FOURK_BUF;
  37146. rsaCaPrivKeyBufSz = FOURK_BUF;
  37147. #endif /* NO_RSA */
  37148. #ifdef HAVE_ECC
  37149. /* read client ECC cert and key in DER format */
  37150. eccClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  37151. DYNAMIC_TYPE_TMP_BUFFER);
  37152. if (ret == 0 && eccClientCertBuf == NULL) {
  37153. ret = WC_TEST_RET_ENC_ERRNO;
  37154. }
  37155. eccClientPrivKeyBuf =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  37156. DYNAMIC_TYPE_TMP_BUFFER);
  37157. if (ret == 0 && eccClientPrivKeyBuf == NULL) {
  37158. ret = WC_TEST_RET_ENC_ERRNO;
  37159. }
  37160. eccClientCertBufSz = FOURK_BUF;
  37161. eccClientPrivKeyBufSz = FOURK_BUF;
  37162. #endif /* HAVE_ECC */
  37163. if (ret >= 0)
  37164. ret = pkcs7_load_certs_keys(rsaClientCertBuf, &rsaClientCertBufSz,
  37165. rsaClientPrivKeyBuf, &rsaClientPrivKeyBufSz,
  37166. rsaServerCertBuf, &rsaServerCertBufSz,
  37167. rsaServerPrivKeyBuf, &rsaServerPrivKeyBufSz,
  37168. rsaCaCertBuf, &rsaCaCertBufSz,
  37169. rsaCaPrivKeyBuf, &rsaCaPrivKeyBufSz,
  37170. eccClientCertBuf, &eccClientCertBufSz,
  37171. eccClientPrivKeyBuf, &eccClientPrivKeyBufSz);
  37172. if (ret < 0) {
  37173. ret = WC_TEST_RET_ENC_EC(ret);
  37174. }
  37175. if (ret >= 0)
  37176. ret = pkcs7signed_run_vectors(rsaClientCertBuf, (word32)rsaClientCertBufSz,
  37177. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  37178. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  37179. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  37180. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  37181. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  37182. eccClientCertBuf, (word32)eccClientCertBufSz,
  37183. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  37184. if (ret >= 0)
  37185. ret = pkcs7signed_run_SingleShotVectors(
  37186. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  37187. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  37188. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  37189. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  37190. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  37191. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  37192. eccClientCertBuf, (word32)eccClientCertBufSz,
  37193. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  37194. #if !defined(NO_RSA) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  37195. if (ret >= 0)
  37196. ret = pkcs7callback_test(
  37197. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  37198. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz);
  37199. #endif
  37200. XFREE(rsaClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37201. XFREE(rsaClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37202. XFREE(rsaServerCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37203. XFREE(rsaServerPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37204. XFREE(rsaCaCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37205. XFREE(rsaCaPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37206. XFREE(eccClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37207. XFREE(eccClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37208. return ret;
  37209. }
  37210. #endif /* HAVE_PKCS7 */
  37211. #if defined(WOLFSSL_PUBLIC_MP) && \
  37212. ((defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  37213. defined(USE_FAST_MATH))
  37214. /* Maximum number of bytes in a number to test. */
  37215. #define MP_MAX_TEST_BYTE_LEN 32
  37216. static wc_test_ret_t randNum(mp_int* n, int len, WC_RNG* rng, void* heap)
  37217. {
  37218. byte d[MP_MAX_TEST_BYTE_LEN];
  37219. wc_test_ret_t ret;
  37220. (void)heap;
  37221. do {
  37222. ret = wc_RNG_GenerateBlock(rng, d, len);
  37223. if (ret != 0)
  37224. return ret;
  37225. ret = mp_read_unsigned_bin(n, d, len);
  37226. if (ret != 0)
  37227. return ret;
  37228. } while (mp_iszero(n));
  37229. return 0;
  37230. }
  37231. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  37232. static wc_test_ret_t mp_test_div_3(mp_int* a, mp_int* r, WC_RNG* rng)
  37233. {
  37234. int i, j;
  37235. mp_digit rem;
  37236. mp_digit rem2;
  37237. wc_test_ret_t ret;
  37238. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  37239. defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  37240. for (i = 0; i < 10; i++) {
  37241. for (j = 1; j < 10; j++) {
  37242. ret = randNum(a, j, rng, NULL);
  37243. if (ret != 0)
  37244. return WC_TEST_RET_ENC_EC(ret);
  37245. ret = mp_div_3(a, r, &rem);
  37246. if (ret != 0)
  37247. return WC_TEST_RET_ENC_EC(ret);
  37248. ret = mp_mul_d(r, 3, r);
  37249. if (ret != 0)
  37250. return WC_TEST_RET_ENC_EC(ret);
  37251. ret = mp_add_d(r, rem, r);
  37252. if (ret != 0)
  37253. return WC_TEST_RET_ENC_EC(ret);
  37254. ret = mp_cmp(r, a);
  37255. if (ret != MP_EQ)
  37256. return WC_TEST_RET_ENC_NC;
  37257. }
  37258. }
  37259. ret = mp_div_3(a, r, &rem);
  37260. if (ret != 0)
  37261. return WC_TEST_RET_ENC_NC;
  37262. ret = mp_div_3(a, a, NULL);
  37263. if (ret != 0)
  37264. return WC_TEST_RET_ENC_NC;
  37265. ret = mp_cmp(r, a);
  37266. if (ret != MP_EQ)
  37267. return WC_TEST_RET_ENC_NC;
  37268. #endif
  37269. #if defined(WOLFSSL_SP_MATH_ALL)
  37270. ret = mp_div_d(a, 10, r, &rem);
  37271. if (ret != 0)
  37272. return WC_TEST_RET_ENC_EC(ret);
  37273. ret = mp_div_d(a, 10, a, NULL);
  37274. if (ret != 0)
  37275. return WC_TEST_RET_ENC_EC(ret);
  37276. ret = mp_cmp(r, a);
  37277. if (ret != MP_EQ)
  37278. return WC_TEST_RET_ENC_NC;
  37279. ret = mp_div_d(a, 12, r, &rem);
  37280. if (ret != 0)
  37281. return WC_TEST_RET_ENC_EC(ret);
  37282. ret = mp_div_d(a, 12, a, NULL);
  37283. if (ret != 0)
  37284. return WC_TEST_RET_ENC_EC(ret);
  37285. ret = mp_cmp(r, a);
  37286. if (ret != MP_EQ)
  37287. return WC_TEST_RET_ENC_NC;
  37288. ret = mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), r, &rem);
  37289. if (ret != 0)
  37290. return WC_TEST_RET_ENC_EC(ret);
  37291. ret = mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), NULL, &rem2);
  37292. if (ret != 0)
  37293. return WC_TEST_RET_ENC_EC(ret);
  37294. ret = mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), a, NULL);
  37295. if (ret != 0)
  37296. return WC_TEST_RET_ENC_EC(ret);
  37297. ret = mp_cmp(r, a);
  37298. if (ret != MP_EQ)
  37299. return WC_TEST_RET_ENC_NC;
  37300. if (rem != rem2)
  37301. return WC_TEST_RET_ENC_NC;
  37302. #endif
  37303. (void)a;
  37304. (void)r;
  37305. (void)rng;
  37306. (void)i;
  37307. (void)j;
  37308. (void)rem;
  37309. (void)rem2;
  37310. (void)ret;
  37311. return 0;
  37312. }
  37313. #endif /* WOLFSSL_SP_MATH || !USE_FAST_MATH */
  37314. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  37315. !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  37316. (!defined WOLFSSL_SP_MATH && !defined(WOLFSSL_SP_MATH_ALL) && \
  37317. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  37318. static wc_test_ret_t mp_test_radix_10(mp_int* a, mp_int* r, WC_RNG* rng)
  37319. {
  37320. wc_test_ret_t ret;
  37321. int i, j;
  37322. int size;
  37323. char str[30];
  37324. WOLFSSL_SMALL_STACK_STATIC const char* badStr1 = "A";
  37325. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "a";
  37326. WOLFSSL_SMALL_STACK_STATIC const char* badStr3 = " ";
  37327. WOLFSSL_SMALL_STACK_STATIC const char* zeros = "000";
  37328. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  37329. for (i = 0; i < 10; i++) {
  37330. for (j = 2; j < 12; j++) {
  37331. ret = randNum(a, j, rng, NULL);
  37332. if (ret != 0)
  37333. return WC_TEST_RET_ENC_EC(ret);
  37334. ret = mp_radix_size(a, MP_RADIX_DEC, &size);
  37335. if (ret != MP_OKAY)
  37336. return WC_TEST_RET_ENC_EC(ret);
  37337. ret = mp_toradix(a, str, MP_RADIX_DEC);
  37338. if (ret != MP_OKAY)
  37339. return WC_TEST_RET_ENC_EC(ret);
  37340. if ((int)XSTRLEN(str) != size - 1)
  37341. return WC_TEST_RET_ENC_NC;
  37342. ret = mp_read_radix(r, str, MP_RADIX_DEC);
  37343. if (ret != MP_OKAY)
  37344. return WC_TEST_RET_ENC_EC(ret);
  37345. ret = mp_cmp(a, r);
  37346. if (ret != MP_EQ)
  37347. return WC_TEST_RET_ENC_NC;
  37348. }
  37349. }
  37350. ret = mp_read_radix(r, badStr1, MP_RADIX_DEC);
  37351. if (ret != MP_VAL)
  37352. return WC_TEST_RET_ENC_EC(ret);
  37353. ret = mp_read_radix(r, badStr2, MP_RADIX_DEC);
  37354. if (ret != MP_VAL)
  37355. return WC_TEST_RET_ENC_EC(ret);
  37356. ret = mp_read_radix(r, badStr3, MP_RADIX_DEC);
  37357. if (ret != MP_VAL)
  37358. return WC_TEST_RET_ENC_EC(ret);
  37359. ret = mp_read_radix(r, zeros, MP_RADIX_DEC);
  37360. if (ret != MP_OKAY)
  37361. return WC_TEST_RET_ENC_EC(ret);
  37362. if (!mp_iszero(r))
  37363. return WC_TEST_RET_ENC_NC;
  37364. mp_set(r, 1);
  37365. ret = mp_read_radix(r, empty, MP_RADIX_DEC);
  37366. if (ret != MP_OKAY)
  37367. return WC_TEST_RET_ENC_EC(ret);
  37368. if (!mp_iszero(r))
  37369. return WC_TEST_RET_ENC_NC;
  37370. mp_zero(a);
  37371. ret = mp_radix_size(a, MP_RADIX_DEC, &size);
  37372. if (ret != 0)
  37373. return WC_TEST_RET_ENC_EC(ret);
  37374. if (size != 2)
  37375. return WC_TEST_RET_ENC_NC;
  37376. ret = mp_toradix(a, str, MP_RADIX_DEC);
  37377. if (ret != 0)
  37378. return WC_TEST_RET_ENC_EC(ret);
  37379. if ((int)XSTRLEN(str) != size - 1)
  37380. return WC_TEST_RET_ENC_NC;
  37381. ret = mp_read_radix(r, str, MP_RADIX_DEC);
  37382. if (ret != 0)
  37383. return WC_TEST_RET_ENC_EC(ret);
  37384. if (!mp_iszero(r))
  37385. return WC_TEST_RET_ENC_NC;
  37386. return 0;
  37387. }
  37388. #endif
  37389. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  37390. defined(HAVE_ECC))
  37391. static wc_test_ret_t mp_test_radix_16(mp_int* a, mp_int* r, WC_RNG* rng)
  37392. {
  37393. wc_test_ret_t ret;
  37394. int i, j;
  37395. int size;
  37396. char str[30];
  37397. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  37398. static char longStr[2 * sizeof(a->dp) + 2];
  37399. #endif
  37400. WOLFSSL_SMALL_STACK_STATIC const char* badStr1 = " ";
  37401. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "}";
  37402. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  37403. for (i = 0; i < 10; i++) {
  37404. for (j = 2; j < 12; j++) {
  37405. ret = randNum(a, j, rng, NULL);
  37406. if (ret != 0)
  37407. return WC_TEST_RET_ENC_EC(ret);
  37408. mp_radix_size(a, MP_RADIX_HEX, &size);
  37409. mp_toradix(a, str, MP_RADIX_HEX);
  37410. if ((int)XSTRLEN(str) != size - 1)
  37411. return WC_TEST_RET_ENC_NC;
  37412. mp_read_radix(r, str, MP_RADIX_HEX);
  37413. ret = mp_cmp(a, r);
  37414. if (ret != MP_EQ)
  37415. return WC_TEST_RET_ENC_NC;
  37416. }
  37417. }
  37418. ret = mp_read_radix(r, badStr1, MP_RADIX_HEX);
  37419. if (ret != MP_VAL)
  37420. return WC_TEST_RET_ENC_EC(ret);
  37421. ret = mp_read_radix(r, badStr2, MP_RADIX_HEX);
  37422. if (ret != MP_VAL)
  37423. return WC_TEST_RET_ENC_EC(ret);
  37424. mp_set(r, 1);
  37425. ret = mp_read_radix(r, empty, MP_RADIX_HEX);
  37426. if (ret != MP_OKAY)
  37427. return WC_TEST_RET_ENC_EC(ret);
  37428. if (!mp_iszero(r))
  37429. return WC_TEST_RET_ENC_NC;
  37430. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  37431. /* Fixed MP data size - string can be too long. */
  37432. longStr[0] = '8';
  37433. XMEMSET(longStr+1, '0', sizeof(longStr) - 2);
  37434. longStr[sizeof(longStr)-1] = '\0';
  37435. ret = mp_read_radix(r, longStr, MP_RADIX_HEX);
  37436. if (ret != MP_VAL)
  37437. return WC_TEST_RET_ENC_EC(ret);
  37438. #endif
  37439. mp_zero(a);
  37440. ret = mp_radix_size(a, MP_RADIX_HEX, &size);
  37441. if (ret != 0)
  37442. return WC_TEST_RET_ENC_EC(ret);
  37443. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  37444. if (size != 3)
  37445. #else
  37446. if (size != 2)
  37447. #endif
  37448. return WC_TEST_RET_ENC_NC;
  37449. ret = mp_toradix(a, str, MP_RADIX_HEX);
  37450. if (ret != 0)
  37451. return WC_TEST_RET_ENC_EC(ret);
  37452. if ((int)XSTRLEN(str) != size - 1)
  37453. return WC_TEST_RET_ENC_NC;
  37454. ret = mp_read_radix(r, str, MP_RADIX_HEX);
  37455. if (ret != 0)
  37456. return WC_TEST_RET_ENC_EC(ret);
  37457. if (!mp_iszero(r))
  37458. return WC_TEST_RET_ENC_NC;
  37459. #ifdef WOLFSSL_SP_MATH
  37460. ret = mp_toradix(a, str, 8);
  37461. if (ret != MP_VAL)
  37462. return WC_TEST_RET_ENC_EC(ret);
  37463. ret = mp_radix_size(a, 8, &size);
  37464. if (ret != MP_VAL)
  37465. return WC_TEST_RET_ENC_EC(ret);
  37466. #endif
  37467. return 0;
  37468. }
  37469. #endif
  37470. static wc_test_ret_t mp_test_shift(mp_int* a, mp_int* r1, WC_RNG* rng)
  37471. {
  37472. int i;
  37473. wc_test_ret_t ret;
  37474. ret = randNum(a, 4, rng, NULL);
  37475. if (ret != 0)
  37476. return WC_TEST_RET_ENC_EC(ret);
  37477. for (i = 0; i < 4; i++) {
  37478. mp_copy(r1, a);
  37479. ret = mp_lshd(r1, i);
  37480. if (ret != MP_OKAY)
  37481. return WC_TEST_RET_ENC_EC(ret);
  37482. #ifndef WOLFSSL_SP_MATH
  37483. mp_rshd(r1, i);
  37484. #else
  37485. mp_rshb(r1, i * SP_WORD_SIZE);
  37486. #endif
  37487. ret = mp_cmp(a, r1);
  37488. if (ret != MP_EQ)
  37489. return WC_TEST_RET_ENC_NC;
  37490. }
  37491. #ifndef WOLFSSL_SP_MATH
  37492. for (i = 0; i < DIGIT_BIT+1; i++) {
  37493. ret = mp_mul_2d(a, i, r1);
  37494. if (ret != MP_OKAY)
  37495. return WC_TEST_RET_ENC_EC(ret);
  37496. mp_rshb(r1, i);
  37497. ret = mp_cmp(a, r1);
  37498. if (ret != MP_EQ)
  37499. return WC_TEST_RET_ENC_NC;
  37500. }
  37501. #endif
  37502. return 0;
  37503. }
  37504. static wc_test_ret_t mp_test_add_sub_d(mp_int* a, mp_int* r1)
  37505. {
  37506. int i, j;
  37507. wc_test_ret_t ret;
  37508. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  37509. mp_zero(a);
  37510. mp_set_bit(a, i);
  37511. if ((int)a->used != (i + DIGIT_BIT) / DIGIT_BIT)
  37512. return WC_TEST_RET_ENC_NC;
  37513. for (j = 0; j < i && j < DIGIT_BIT; j++) {
  37514. mp_zero(r1);
  37515. mp_set_bit(r1, i);
  37516. ret = mp_sub_d(r1, (mp_digit)1 << j, r1);
  37517. if (ret != MP_OKAY)
  37518. return WC_TEST_RET_ENC_EC(ret);
  37519. ret = mp_add_d(r1, (mp_digit)1 << j, r1);
  37520. if (ret != MP_OKAY)
  37521. return WC_TEST_RET_ENC_EC(ret);
  37522. ret = mp_cmp(a, r1);
  37523. if (ret != MP_EQ)
  37524. return WC_TEST_RET_ENC_NC;
  37525. }
  37526. }
  37527. mp_zero(r1);
  37528. ret = mp_add_d(r1, 1, r1);
  37529. if (ret != MP_OKAY)
  37530. return WC_TEST_RET_ENC_EC(ret);
  37531. if (r1->used != 1)
  37532. return WC_TEST_RET_ENC_NC;
  37533. ret = mp_sub_d(r1, 1, r1);
  37534. if (ret != MP_OKAY)
  37535. return WC_TEST_RET_ENC_EC(ret);
  37536. if (r1->used != 0)
  37537. return WC_TEST_RET_ENC_NC;
  37538. return 0;
  37539. }
  37540. static wc_test_ret_t mp_test_read_to_bin(mp_int* a)
  37541. {
  37542. WOLFSSL_SMALL_STACK_STATIC const byte in[16] = {
  37543. 0x91, 0xa2, 0xb3, 0xc4, 0xd5, 0xe6, 0xf7, 0x08,
  37544. 0x93, 0xa4, 0xb4, 0xc5, 0xd6, 0xe7, 0xf8, 0x09
  37545. };
  37546. byte out[24];
  37547. int i, j, k;
  37548. const byte* p;
  37549. wc_test_ret_t ret;
  37550. for (i = 0; i < (int)sizeof(in); i++) {
  37551. p = in + sizeof(in) - i;
  37552. ret = mp_read_unsigned_bin(a, p, i);
  37553. if (ret != 0)
  37554. return WC_TEST_RET_ENC_EC(ret);
  37555. for (j = i; j < (int)sizeof(out); j++) {
  37556. XMEMSET(out, 0xff, sizeof(out));
  37557. ret = mp_to_unsigned_bin_len(a, out, j);
  37558. if (ret != 0)
  37559. return WC_TEST_RET_ENC_EC(ret);
  37560. for (k = 0; k < j - i; k++) {
  37561. if (out[k] != 0)
  37562. return WC_TEST_RET_ENC_NC;
  37563. }
  37564. for (; k < j; k++) {
  37565. if (out[k] != p[k - (j - i)])
  37566. return WC_TEST_RET_ENC_NC;
  37567. }
  37568. }
  37569. }
  37570. /* Length too small. */
  37571. ret = mp_to_unsigned_bin_len(a, out, 1);
  37572. if (ret != MP_VAL)
  37573. return WC_TEST_RET_ENC_EC(ret);
  37574. ret = mp_read_unsigned_bin(a, NULL, 0);
  37575. if (ret != 0)
  37576. return WC_TEST_RET_ENC_EC(ret);
  37577. if (!mp_iszero(a))
  37578. return WC_TEST_RET_ENC_NC;
  37579. return 0;
  37580. }
  37581. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  37582. static wc_test_ret_t mp_test_set_int(mp_int* a)
  37583. {
  37584. #if SP_ULONG_BITS == 64
  37585. unsigned long n = 0xfedcba9876543210UL;
  37586. byte exp[8] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
  37587. byte out[8] = { 0 };
  37588. #elif SP_ULONG_BITS == 32
  37589. unsigned long n = 0xfedcba98UL;
  37590. byte exp[4] = { 0xfe, 0xdc, 0xba, 0x98 };
  37591. byte out[4] = { 0 };
  37592. #elif SP_ULONG_BITS == 16
  37593. unsigned long n = 0xfedc;
  37594. byte exp[2] = { 0xfe, 0xdc };
  37595. byte out[2] = { 0 };
  37596. #elif SP_ULONG_BITS == 8
  37597. unsigned long n = 0xfe;
  37598. byte exp[1] = { 0xfe };
  37599. byte out[1] = { 0 };
  37600. #endif
  37601. wc_test_ret_t ret;
  37602. ret = mp_set_int(a, n);
  37603. if (ret != 0)
  37604. return WC_TEST_RET_ENC_EC(ret);
  37605. ret = mp_unsigned_bin_size(a);
  37606. if (ret != sizeof(exp))
  37607. return WC_TEST_RET_ENC_NC;
  37608. ret = mp_to_unsigned_bin(a, out);
  37609. if (ret != 0)
  37610. return WC_TEST_RET_ENC_EC(ret);
  37611. if (XMEMCMP(exp, out, sizeof(exp)) != 0)
  37612. return WC_TEST_RET_ENC_NC;
  37613. return 0;
  37614. }
  37615. #endif
  37616. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  37617. static wc_test_ret_t mp_test_param(mp_int* a, mp_int* b, mp_int* r, WC_RNG* rng)
  37618. {
  37619. byte buffer[16];
  37620. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  37621. char hexStr[] = "abcdef0123456789";
  37622. #ifndef WOLFSSL_SP_INT_NEGATIVE
  37623. char negStr[] = "-1234";
  37624. #endif
  37625. #endif
  37626. #if !defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_KEY_GEN) || \
  37627. defined(HAVE_COMP_KEY)
  37628. char decStr[] = "0987654321";
  37629. #endif
  37630. wc_test_ret_t ret;
  37631. #ifdef WOLFSSL_SP_MATH_ALL
  37632. mp_digit rho;
  37633. int size;
  37634. #endif
  37635. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  37636. int result;
  37637. #endif
  37638. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  37639. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  37640. mp_digit rd;
  37641. #endif
  37642. (void)rng;
  37643. (void)r;
  37644. ret = mp_init(NULL);
  37645. if (ret != MP_VAL)
  37646. return WC_TEST_RET_ENC_EC(ret);
  37647. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  37648. ret = mp_init_multi(NULL, NULL, NULL, NULL, NULL, NULL);
  37649. if (ret != MP_OKAY)
  37650. return WC_TEST_RET_ENC_EC(ret);
  37651. #endif
  37652. mp_free(NULL);
  37653. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || !defined(NO_DH) || defined(HAVE_ECC)
  37654. ret = mp_grow(NULL, 1);
  37655. if (ret != MP_VAL)
  37656. return WC_TEST_RET_ENC_EC(ret);
  37657. #ifdef WOLFSSL_SP_MATH
  37658. ret = mp_grow(a, SP_INT_DIGITS + 1);
  37659. if (ret != MP_MEM)
  37660. return WC_TEST_RET_ENC_EC(ret);
  37661. #endif
  37662. #endif
  37663. mp_clear(NULL);
  37664. ret = mp_abs(NULL, NULL);
  37665. if (ret != MP_VAL)
  37666. return WC_TEST_RET_ENC_EC(ret);
  37667. ret = mp_abs(a, NULL);
  37668. if (ret != MP_VAL)
  37669. return WC_TEST_RET_ENC_EC(ret);
  37670. ret = mp_abs(NULL, b);
  37671. if (ret != MP_VAL)
  37672. return WC_TEST_RET_ENC_EC(ret);
  37673. ret = mp_unsigned_bin_size(NULL);
  37674. if (ret != 0)
  37675. return WC_TEST_RET_ENC_EC(ret);
  37676. ret = mp_read_unsigned_bin(NULL, NULL, sizeof(buffer));
  37677. if (ret != MP_VAL)
  37678. return WC_TEST_RET_ENC_EC(ret);
  37679. ret = mp_read_unsigned_bin(NULL, buffer, sizeof(buffer));
  37680. if (ret != MP_VAL)
  37681. return WC_TEST_RET_ENC_EC(ret);
  37682. ret = mp_read_unsigned_bin(a, NULL, sizeof(buffer));
  37683. if (ret != MP_VAL)
  37684. return WC_TEST_RET_ENC_EC(ret);
  37685. ret = mp_read_unsigned_bin(a, buffer, SP_INT_DIGITS * SP_WORD_SIZEOF + 1);
  37686. if (ret != MP_VAL)
  37687. return WC_TEST_RET_ENC_EC(ret);
  37688. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  37689. ret = mp_read_radix(NULL, NULL, 16);
  37690. if (ret != MP_VAL)
  37691. return WC_TEST_RET_ENC_EC(ret);
  37692. ret = mp_read_radix(a, NULL, 16);
  37693. if (ret != MP_VAL)
  37694. return WC_TEST_RET_ENC_EC(ret);
  37695. ret = mp_read_radix(NULL, hexStr, 16);
  37696. if (ret != MP_VAL)
  37697. return WC_TEST_RET_ENC_EC(ret);
  37698. #ifndef WOLFSSL_SP_INT_NEGATIVE
  37699. ret = mp_read_radix(a, negStr, 16);
  37700. if (ret != MP_VAL)
  37701. return WC_TEST_RET_ENC_EC(ret);
  37702. #ifdef WOLFSSL_SP_MATH_ALL
  37703. ret = mp_read_radix(a, negStr, 10);
  37704. if (ret != MP_VAL)
  37705. return WC_TEST_RET_ENC_EC(ret);
  37706. #endif /* WOLFSSL_SP_MATH_ALL */
  37707. #endif /* WOLFSSL_SP_INT_NEGATIVE */
  37708. #endif
  37709. #ifndef WOLFSSL_SP_MATH_ALL
  37710. /* Radix 10 only supported with ALL. */
  37711. ret = mp_read_radix(a, decStr, 10);
  37712. if (ret != MP_VAL)
  37713. return WC_TEST_RET_ENC_EC(ret);
  37714. #endif
  37715. /* Radix 8 not supported SP_INT. */
  37716. ret = mp_read_radix(a, "0123", 8);
  37717. if (ret != MP_VAL)
  37718. return WC_TEST_RET_ENC_EC(ret);
  37719. ret = mp_count_bits(NULL);
  37720. if (ret != 0)
  37721. return WC_TEST_RET_ENC_EC(ret);
  37722. ret = mp_is_bit_set(NULL, 0);
  37723. if (ret != 0)
  37724. return WC_TEST_RET_ENC_EC(ret);
  37725. ret = mp_leading_bit(NULL);
  37726. if (ret != 0)
  37727. return WC_TEST_RET_ENC_EC(ret);
  37728. mp_zero(a);
  37729. ret = mp_leading_bit(a);
  37730. if (ret != 0)
  37731. return WC_TEST_RET_ENC_EC(ret);
  37732. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  37733. defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || \
  37734. !defined(NO_RSA)
  37735. ret = mp_set_bit(NULL, 1);
  37736. if (ret != MP_VAL)
  37737. return WC_TEST_RET_ENC_EC(ret);
  37738. #endif
  37739. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  37740. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37741. ret = mp_to_unsigned_bin(NULL, NULL);
  37742. if (ret != MP_VAL)
  37743. return WC_TEST_RET_ENC_EC(ret);
  37744. ret = mp_to_unsigned_bin(a, NULL);
  37745. if (ret != MP_VAL)
  37746. return WC_TEST_RET_ENC_EC(ret);
  37747. ret = mp_to_unsigned_bin(NULL, buffer);
  37748. if (ret != MP_VAL)
  37749. return WC_TEST_RET_ENC_EC(ret);
  37750. #endif
  37751. ret = mp_to_unsigned_bin_len(NULL, NULL, 1);
  37752. if (ret != MP_VAL)
  37753. return WC_TEST_RET_ENC_EC(ret);
  37754. ret = mp_to_unsigned_bin_len(a, NULL, 1);
  37755. if (ret != MP_VAL)
  37756. return WC_TEST_RET_ENC_EC(ret);
  37757. ret = mp_to_unsigned_bin_len(NULL, buffer, 1);
  37758. if (ret != MP_VAL)
  37759. return WC_TEST_RET_ENC_EC(ret);
  37760. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  37761. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37762. ret = mp_to_unsigned_bin_at_pos(0, NULL, NULL);
  37763. if (ret != MP_VAL)
  37764. return WC_TEST_RET_ENC_EC(ret);
  37765. ret = mp_to_unsigned_bin_at_pos(0, a, NULL);
  37766. if (ret != MP_VAL)
  37767. return WC_TEST_RET_ENC_EC(ret);
  37768. ret = mp_to_unsigned_bin_at_pos(0, NULL, buffer);
  37769. if (ret != MP_VAL)
  37770. return WC_TEST_RET_ENC_EC(ret);
  37771. ret = mp_to_unsigned_bin_at_pos(0, a, buffer);
  37772. if (ret != MP_OKAY)
  37773. return WC_TEST_RET_ENC_EC(ret);
  37774. #endif
  37775. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  37776. ret = mp_copy(NULL, NULL);
  37777. if (ret != MP_VAL)
  37778. return WC_TEST_RET_ENC_EC(ret);
  37779. ret = mp_copy(a, NULL);
  37780. if (ret != MP_VAL)
  37781. return WC_TEST_RET_ENC_EC(ret);
  37782. ret = mp_copy(NULL, b);
  37783. if (ret != MP_VAL)
  37784. return WC_TEST_RET_ENC_EC(ret);
  37785. #endif
  37786. #if defined(WOLFSSL_KEY_GEN) || !defined(NO_DH)
  37787. ret = sp_2expt(NULL, 1);
  37788. if (ret != MP_VAL)
  37789. return WC_TEST_RET_ENC_EC(ret);
  37790. #endif
  37791. ret = mp_set(NULL, 0);
  37792. if (ret != MP_VAL)
  37793. return WC_TEST_RET_ENC_EC(ret);
  37794. ret = mp_cmp_d(NULL, 0);
  37795. if (ret != MP_LT)
  37796. return WC_TEST_RET_ENC_EC(ret);
  37797. ret = mp_cmp(NULL, NULL);
  37798. if (ret != MP_EQ)
  37799. return WC_TEST_RET_ENC_NC;
  37800. ret = mp_cmp(a, NULL);
  37801. if (ret != MP_GT)
  37802. return WC_TEST_RET_ENC_NC;
  37803. ret = mp_cmp(NULL, b);
  37804. if (ret != MP_LT)
  37805. return WC_TEST_RET_ENC_NC;
  37806. #ifdef WOLFSSL_SP_MATH_ALL
  37807. mp_rshd(NULL, 1);
  37808. #endif
  37809. mp_zero(NULL);
  37810. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  37811. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37812. ret = mp_lshd(NULL, 0);
  37813. if (ret != MP_VAL)
  37814. return WC_TEST_RET_ENC_EC(ret);
  37815. ret = mp_lshd(a, SP_INT_DIGITS + 1);
  37816. if (ret != MP_VAL)
  37817. return WC_TEST_RET_ENC_EC(ret);
  37818. #endif
  37819. #if defined(WOLFSSL_SP_MATH_ALL)
  37820. ret = mp_div(NULL, NULL, a, b);
  37821. if (ret != MP_VAL)
  37822. return WC_TEST_RET_ENC_EC(ret);
  37823. ret = mp_div(a, NULL, a, b);
  37824. if (ret != MP_VAL)
  37825. return WC_TEST_RET_ENC_EC(ret);
  37826. ret = mp_div(NULL, b, a, b);
  37827. if (ret != MP_VAL)
  37828. return WC_TEST_RET_ENC_EC(ret);
  37829. ret = mp_div(a, b, NULL, NULL);
  37830. if (ret != MP_VAL)
  37831. return WC_TEST_RET_ENC_EC(ret);
  37832. #endif
  37833. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  37834. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  37835. ret = mp_mod(NULL, NULL, NULL);
  37836. if (ret != MP_VAL)
  37837. return WC_TEST_RET_ENC_EC(ret);
  37838. ret = mp_mod(a, NULL, NULL);
  37839. if (ret != MP_VAL)
  37840. return WC_TEST_RET_ENC_EC(ret);
  37841. ret = mp_mod(NULL, b, NULL);
  37842. if (ret != MP_VAL)
  37843. return WC_TEST_RET_ENC_EC(ret);
  37844. ret = mp_mod(NULL, NULL, r);
  37845. if (ret != MP_VAL)
  37846. return WC_TEST_RET_ENC_EC(ret);
  37847. ret = mp_mod(a, b, NULL);
  37848. if (ret != MP_VAL)
  37849. return WC_TEST_RET_ENC_EC(ret);
  37850. ret = mp_mod(a, NULL, r);
  37851. if (ret != MP_VAL)
  37852. return WC_TEST_RET_ENC_EC(ret);
  37853. ret = mp_mod(NULL, b, r);
  37854. if (ret != MP_VAL)
  37855. return WC_TEST_RET_ENC_EC(ret);
  37856. #endif
  37857. #if !defined(NO_RSA) || defined(WOLFSSL_SP_MATH_ALL)
  37858. ret = mp_set_int(NULL, 0);
  37859. if (ret != MP_VAL)
  37860. return WC_TEST_RET_ENC_EC(ret);
  37861. #endif
  37862. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  37863. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  37864. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  37865. if (ret != MP_VAL)
  37866. return WC_TEST_RET_ENC_EC(ret);
  37867. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  37868. if (ret != MP_VAL)
  37869. return WC_TEST_RET_ENC_EC(ret);
  37870. ret = mp_exptmod_ex(NULL, a, 1, NULL, NULL);
  37871. if (ret != MP_VAL)
  37872. return WC_TEST_RET_ENC_EC(ret);
  37873. ret = mp_exptmod_ex(NULL, NULL, 1, a, NULL);
  37874. if (ret != MP_VAL)
  37875. return WC_TEST_RET_ENC_EC(ret);
  37876. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  37877. if (ret != MP_VAL)
  37878. return WC_TEST_RET_ENC_EC(ret);
  37879. ret = mp_exptmod_ex(a, a, 1, a, NULL);
  37880. if (ret != MP_VAL)
  37881. return WC_TEST_RET_ENC_EC(ret);
  37882. ret = mp_exptmod_ex(a, a, 1, NULL, a);
  37883. if (ret != MP_VAL)
  37884. return WC_TEST_RET_ENC_EC(ret);
  37885. ret = mp_exptmod_ex(a, NULL, 1, a, a);
  37886. if (ret != MP_VAL)
  37887. return WC_TEST_RET_ENC_EC(ret);
  37888. ret = mp_exptmod_ex(NULL, a, 1, a, a);
  37889. if (ret != MP_VAL)
  37890. return WC_TEST_RET_ENC_EC(ret);
  37891. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  37892. if (ret != MP_VAL)
  37893. return WC_TEST_RET_ENC_EC(ret);
  37894. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  37895. if (ret != MP_VAL)
  37896. return WC_TEST_RET_ENC_EC(ret);
  37897. ret = mp_exptmod_nct(NULL, a, NULL, NULL);
  37898. if (ret != MP_VAL)
  37899. return WC_TEST_RET_ENC_EC(ret);
  37900. ret = mp_exptmod_nct(NULL, NULL, a, NULL);
  37901. if (ret != MP_VAL)
  37902. return WC_TEST_RET_ENC_EC(ret);
  37903. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  37904. if (ret != MP_VAL)
  37905. return WC_TEST_RET_ENC_EC(ret);
  37906. ret = mp_exptmod_nct(a, a, a, NULL);
  37907. if (ret != MP_VAL)
  37908. return WC_TEST_RET_ENC_EC(ret);
  37909. ret = mp_exptmod_nct(a, a, NULL, a);
  37910. if (ret != MP_VAL)
  37911. return WC_TEST_RET_ENC_EC(ret);
  37912. ret = mp_exptmod_nct(a, NULL, a, a);
  37913. if (ret != MP_VAL)
  37914. return WC_TEST_RET_ENC_EC(ret);
  37915. ret = mp_exptmod_nct(NULL, a, a, a);
  37916. if (ret != MP_VAL)
  37917. return WC_TEST_RET_ENC_EC(ret);
  37918. #endif
  37919. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  37920. !defined(WC_NO_RNG)
  37921. ret = mp_rand_prime(NULL, 32, NULL, NULL);
  37922. if (ret != MP_VAL)
  37923. return WC_TEST_RET_ENC_EC(ret);
  37924. ret = mp_rand_prime(a, 32, NULL, NULL);
  37925. if (ret != MP_VAL)
  37926. return WC_TEST_RET_ENC_EC(ret);
  37927. ret = mp_rand_prime(NULL, 32, rng, NULL);
  37928. if (ret != MP_VAL)
  37929. return WC_TEST_RET_ENC_EC(ret);
  37930. ret = mp_rand_prime(a, 0, rng, NULL);
  37931. if (ret != MP_VAL)
  37932. return WC_TEST_RET_ENC_EC(ret);
  37933. #endif
  37934. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37935. ret = mp_mul(NULL, NULL, NULL);
  37936. if (ret != MP_VAL)
  37937. return WC_TEST_RET_ENC_EC(ret);
  37938. ret = mp_mul(a, NULL, NULL);
  37939. if (ret != MP_VAL)
  37940. return WC_TEST_RET_ENC_EC(ret);
  37941. ret = mp_mul(NULL, b, NULL);
  37942. if (ret != MP_VAL)
  37943. return WC_TEST_RET_ENC_EC(ret);
  37944. ret = mp_mul(NULL, NULL, r);
  37945. if (ret != MP_VAL)
  37946. return WC_TEST_RET_ENC_EC(ret);
  37947. ret = mp_mul(a, b, NULL);
  37948. if (ret != MP_VAL)
  37949. return WC_TEST_RET_ENC_EC(ret);
  37950. ret = mp_mul(a, NULL, r);
  37951. if (ret != MP_VAL)
  37952. return WC_TEST_RET_ENC_EC(ret);
  37953. ret = mp_mul(NULL, b, r);
  37954. if (ret != MP_VAL)
  37955. return WC_TEST_RET_ENC_EC(ret);
  37956. #endif
  37957. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  37958. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  37959. ret = mp_sqr(NULL, NULL);
  37960. if (ret != MP_VAL)
  37961. return WC_TEST_RET_ENC_EC(ret);
  37962. ret = mp_sqr(a, NULL);
  37963. if (ret != MP_VAL)
  37964. return WC_TEST_RET_ENC_EC(ret);
  37965. ret = mp_sqr(NULL, r);
  37966. if (ret != MP_VAL)
  37967. return WC_TEST_RET_ENC_EC(ret);
  37968. #endif
  37969. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37970. ret = mp_sqrmod(NULL, NULL, NULL);
  37971. if (ret != MP_VAL)
  37972. return WC_TEST_RET_ENC_EC(ret);
  37973. ret = mp_sqrmod(a, NULL, NULL);
  37974. if (ret != MP_VAL)
  37975. return WC_TEST_RET_ENC_EC(ret);
  37976. ret = mp_sqrmod(NULL, a, NULL);
  37977. if (ret != MP_VAL)
  37978. return WC_TEST_RET_ENC_EC(ret);
  37979. ret = mp_sqrmod(NULL, NULL, a);
  37980. if (ret != MP_VAL)
  37981. return WC_TEST_RET_ENC_EC(ret);
  37982. ret = mp_sqrmod(a, b, NULL);
  37983. if (ret != MP_VAL)
  37984. return WC_TEST_RET_ENC_EC(ret);
  37985. ret = mp_sqrmod(a, NULL, b);
  37986. if (ret != MP_VAL)
  37987. return WC_TEST_RET_ENC_EC(ret);
  37988. ret = mp_sqrmod(NULL, a, b);
  37989. if (ret != MP_VAL)
  37990. return WC_TEST_RET_ENC_EC(ret);
  37991. ret = mp_mulmod(NULL, NULL, NULL, NULL);
  37992. if (ret != MP_VAL)
  37993. return WC_TEST_RET_ENC_EC(ret);
  37994. ret = mp_mulmod(a, NULL, NULL, NULL);
  37995. if (ret != MP_VAL)
  37996. return WC_TEST_RET_ENC_EC(ret);
  37997. ret = mp_mulmod(NULL, a, NULL, NULL);
  37998. if (ret != MP_VAL)
  37999. return WC_TEST_RET_ENC_EC(ret);
  38000. ret = mp_mulmod(NULL, NULL, a, NULL);
  38001. if (ret != MP_VAL)
  38002. return WC_TEST_RET_ENC_EC(ret);
  38003. ret = mp_mulmod(NULL, NULL, NULL, a);
  38004. if (ret != MP_VAL)
  38005. return WC_TEST_RET_ENC_EC(ret);
  38006. ret = mp_mulmod(a, b, b, NULL);
  38007. if (ret != MP_VAL)
  38008. return WC_TEST_RET_ENC_EC(ret);
  38009. ret = mp_mulmod(a, b, NULL, a);
  38010. if (ret != MP_VAL)
  38011. return WC_TEST_RET_ENC_EC(ret);
  38012. ret = mp_mulmod(a, NULL, b, a);
  38013. if (ret != MP_VAL)
  38014. return WC_TEST_RET_ENC_EC(ret);
  38015. ret = mp_mulmod(NULL, b, b, a);
  38016. if (ret != MP_VAL)
  38017. return WC_TEST_RET_ENC_EC(ret);
  38018. #endif
  38019. #if !defined(NO_PWDBASED) || defined(WOLFSSL_KEY_GEN) || !defined(NO_DH) || \
  38020. !defined(NO_RSA) || !defined(NO_DSA)
  38021. ret = mp_add_d(NULL, 1, NULL);
  38022. if (ret != MP_VAL)
  38023. return WC_TEST_RET_ENC_EC(ret);
  38024. ret = mp_add_d(a, 1, NULL);
  38025. if (ret != MP_VAL)
  38026. return WC_TEST_RET_ENC_EC(ret);
  38027. ret = mp_add_d(NULL, 1, b);
  38028. if (ret != MP_VAL)
  38029. return WC_TEST_RET_ENC_EC(ret);
  38030. #endif
  38031. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  38032. !defined(NO_DH) || defined(HAVE_ECC) || !defined(NO_DSA)
  38033. ret = mp_sub_d(NULL, 1, NULL);
  38034. if (ret != MP_VAL)
  38035. return WC_TEST_RET_ENC_EC(ret);
  38036. ret = mp_sub_d(a, 1, NULL);
  38037. if (ret != MP_VAL)
  38038. return WC_TEST_RET_ENC_EC(ret);
  38039. ret = mp_sub_d(NULL, 1, b);
  38040. if (ret != MP_VAL)
  38041. return WC_TEST_RET_ENC_EC(ret);
  38042. #endif
  38043. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  38044. defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  38045. ret = mp_div_d(NULL, 0, NULL, NULL);
  38046. if (ret != MP_VAL)
  38047. return WC_TEST_RET_ENC_EC(ret);
  38048. ret = mp_div_d(a, 0, NULL, NULL);
  38049. if (ret != MP_VAL)
  38050. return WC_TEST_RET_ENC_EC(ret);
  38051. ret = mp_div_d(NULL, 1, NULL, NULL);
  38052. if (ret != MP_VAL)
  38053. return WC_TEST_RET_ENC_EC(ret);
  38054. #endif
  38055. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  38056. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  38057. ret = mp_mod_d(NULL, 0, NULL);
  38058. if (ret != MP_VAL)
  38059. return WC_TEST_RET_ENC_EC(ret);
  38060. ret = mp_mod_d(a, 0, NULL);
  38061. if (ret != MP_VAL)
  38062. return WC_TEST_RET_ENC_EC(ret);
  38063. ret = mp_mod_d(NULL, 0, &rd);
  38064. if (ret != MP_VAL)
  38065. return WC_TEST_RET_ENC_EC(ret);
  38066. #endif
  38067. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  38068. ret = mp_gcd(NULL, NULL, NULL);
  38069. if (ret != MP_VAL)
  38070. return WC_TEST_RET_ENC_EC(ret);
  38071. ret = mp_gcd(a, NULL, NULL);
  38072. if (ret != MP_VAL)
  38073. return WC_TEST_RET_ENC_EC(ret);
  38074. ret = mp_gcd(NULL, a, NULL);
  38075. if (ret != MP_VAL)
  38076. return WC_TEST_RET_ENC_EC(ret);
  38077. ret = mp_gcd(NULL, NULL, a);
  38078. if (ret != MP_VAL)
  38079. return WC_TEST_RET_ENC_EC(ret);
  38080. ret = mp_gcd(a, b, NULL);
  38081. if (ret != MP_VAL)
  38082. return WC_TEST_RET_ENC_EC(ret);
  38083. ret = mp_gcd(a, NULL, b);
  38084. if (ret != MP_VAL)
  38085. return WC_TEST_RET_ENC_EC(ret);
  38086. ret = mp_gcd(NULL, a, b);
  38087. if (ret != MP_VAL)
  38088. return WC_TEST_RET_ENC_EC(ret);
  38089. #endif
  38090. #if !defined(WOLFSSL_SP_MATH) && defined(HAVE_ECC)
  38091. ret = mp_div_2_mod_ct(NULL, NULL, NULL);
  38092. if (ret != MP_VAL)
  38093. return WC_TEST_RET_ENC_EC(ret);
  38094. ret = mp_div_2_mod_ct(a, NULL, NULL);
  38095. if (ret != MP_VAL)
  38096. return WC_TEST_RET_ENC_EC(ret);
  38097. ret = mp_div_2_mod_ct(NULL, b, NULL);
  38098. if (ret != MP_VAL)
  38099. return WC_TEST_RET_ENC_EC(ret);
  38100. ret = mp_div_2_mod_ct(NULL, NULL, a);
  38101. if (ret != MP_VAL)
  38102. return WC_TEST_RET_ENC_EC(ret);
  38103. ret = mp_div_2_mod_ct(a, b, NULL);
  38104. if (ret != MP_VAL)
  38105. return WC_TEST_RET_ENC_EC(ret);
  38106. ret = mp_div_2_mod_ct(a, b, NULL);
  38107. if (ret != MP_VAL)
  38108. return WC_TEST_RET_ENC_EC(ret);
  38109. ret = mp_div_2_mod_ct(NULL, b, a);
  38110. if (ret != MP_VAL)
  38111. return WC_TEST_RET_ENC_EC(ret);
  38112. ret = mp_div_2(NULL, NULL);
  38113. if (ret != MP_VAL)
  38114. return WC_TEST_RET_ENC_EC(ret);
  38115. ret = mp_div_2(a, NULL);
  38116. if (ret != MP_VAL)
  38117. return WC_TEST_RET_ENC_EC(ret);
  38118. ret = mp_div_2(NULL, a);
  38119. if (ret != MP_VAL)
  38120. return WC_TEST_RET_ENC_EC(ret);
  38121. #endif
  38122. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  38123. defined(HAVE_ECC) || !defined(NO_DSA) || defined(OPENSSL_EXTRA)
  38124. ret = mp_invmod(NULL, NULL, NULL);
  38125. if (ret != MP_VAL)
  38126. return WC_TEST_RET_ENC_EC(ret);
  38127. ret = mp_invmod(a, NULL, NULL);
  38128. if (ret != MP_VAL)
  38129. return WC_TEST_RET_ENC_EC(ret);
  38130. ret = mp_invmod(NULL, b, NULL);
  38131. if (ret != MP_VAL)
  38132. return WC_TEST_RET_ENC_EC(ret);
  38133. ret = mp_invmod(NULL, NULL, a);
  38134. if (ret != MP_VAL)
  38135. return WC_TEST_RET_ENC_EC(ret);
  38136. ret = mp_invmod(a, b, NULL);
  38137. if (ret != MP_VAL)
  38138. return WC_TEST_RET_ENC_EC(ret);
  38139. ret = mp_invmod(a, NULL, a);
  38140. if (ret != MP_VAL)
  38141. return WC_TEST_RET_ENC_EC(ret);
  38142. ret = mp_invmod(NULL, b, a);
  38143. if (ret != MP_VAL)
  38144. return WC_TEST_RET_ENC_EC(ret);
  38145. #endif
  38146. #if !defined(WOLFSSL_SP_MATH) && defined(HAVE_ECC)
  38147. ret = mp_invmod_mont_ct(NULL, NULL, NULL, 1);
  38148. if (ret != MP_VAL)
  38149. return WC_TEST_RET_ENC_EC(ret);
  38150. ret = mp_invmod_mont_ct(a, NULL, NULL, 1);
  38151. if (ret != MP_VAL)
  38152. return WC_TEST_RET_ENC_EC(ret);
  38153. ret = mp_invmod_mont_ct(NULL, b, NULL, 1);
  38154. if (ret != MP_VAL)
  38155. return WC_TEST_RET_ENC_EC(ret);
  38156. ret = mp_invmod_mont_ct(NULL, NULL, a, 1);
  38157. if (ret != MP_VAL)
  38158. return WC_TEST_RET_ENC_EC(ret);
  38159. ret = mp_invmod_mont_ct(a, b, NULL, 1);
  38160. if (ret != MP_VAL)
  38161. return WC_TEST_RET_ENC_EC(ret);
  38162. ret = mp_invmod_mont_ct(a, NULL, a, 1);
  38163. if (ret != MP_VAL)
  38164. return WC_TEST_RET_ENC_EC(ret);
  38165. ret = mp_invmod_mont_ct(NULL, b, a, 1);
  38166. if (ret != MP_VAL)
  38167. return WC_TEST_RET_ENC_EC(ret);
  38168. #endif
  38169. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  38170. ret = mp_lcm(NULL, NULL, NULL);
  38171. if (ret != MP_VAL)
  38172. return WC_TEST_RET_ENC_EC(ret);
  38173. ret = mp_lcm(a, NULL, NULL);
  38174. if (ret != MP_VAL)
  38175. return WC_TEST_RET_ENC_EC(ret);
  38176. ret = mp_lcm(NULL, b, NULL);
  38177. if (ret != MP_VAL)
  38178. return WC_TEST_RET_ENC_EC(ret);
  38179. ret = mp_lcm(NULL, NULL, a);
  38180. if (ret != MP_VAL)
  38181. return WC_TEST_RET_ENC_EC(ret);
  38182. ret = mp_lcm(a, b, NULL);
  38183. if (ret != MP_VAL)
  38184. return WC_TEST_RET_ENC_EC(ret);
  38185. ret = mp_lcm(a, NULL, a);
  38186. if (ret != MP_VAL)
  38187. return WC_TEST_RET_ENC_EC(ret);
  38188. ret = mp_lcm(NULL, b, a);
  38189. if (ret != MP_VAL)
  38190. return WC_TEST_RET_ENC_EC(ret);
  38191. #endif
  38192. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  38193. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  38194. if (ret != MP_VAL)
  38195. return WC_TEST_RET_ENC_EC(ret);
  38196. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  38197. if (ret != MP_VAL)
  38198. return WC_TEST_RET_ENC_EC(ret);
  38199. ret = mp_exptmod_ex(NULL, b, 1, NULL, NULL);
  38200. if (ret != MP_VAL)
  38201. return WC_TEST_RET_ENC_EC(ret);
  38202. ret = mp_exptmod_ex(NULL, NULL, 1, b, NULL);
  38203. if (ret != MP_VAL)
  38204. return WC_TEST_RET_ENC_EC(ret);
  38205. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  38206. if (ret != MP_VAL)
  38207. return WC_TEST_RET_ENC_EC(ret);
  38208. ret = mp_exptmod_ex(a, b, 1, b, NULL);
  38209. if (ret != MP_VAL)
  38210. return WC_TEST_RET_ENC_EC(ret);
  38211. ret = mp_exptmod_ex(a, b, 1, NULL, a);
  38212. if (ret != MP_VAL)
  38213. return WC_TEST_RET_ENC_EC(ret);
  38214. ret = mp_exptmod_ex(a, NULL, 1, b, a);
  38215. if (ret != MP_VAL)
  38216. return WC_TEST_RET_ENC_EC(ret);
  38217. ret = mp_exptmod_ex(NULL, b, 1, b, a);
  38218. if (ret != MP_VAL)
  38219. return WC_TEST_RET_ENC_EC(ret);
  38220. ret = mp_exptmod(NULL, NULL, NULL, NULL);
  38221. if (ret != MP_VAL)
  38222. return WC_TEST_RET_ENC_EC(ret);
  38223. ret = mp_exptmod(a, NULL, NULL, NULL);
  38224. if (ret != MP_VAL)
  38225. return WC_TEST_RET_ENC_EC(ret);
  38226. ret = mp_exptmod(NULL, b, NULL, NULL);
  38227. if (ret != MP_VAL)
  38228. return WC_TEST_RET_ENC_EC(ret);
  38229. ret = mp_exptmod(NULL, NULL, b, NULL);
  38230. if (ret != MP_VAL)
  38231. return WC_TEST_RET_ENC_EC(ret);
  38232. ret = mp_exptmod(NULL, NULL, NULL, a);
  38233. if (ret != MP_VAL)
  38234. return WC_TEST_RET_ENC_EC(ret);
  38235. ret = mp_exptmod(a, b, b, NULL);
  38236. if (ret != MP_VAL)
  38237. return WC_TEST_RET_ENC_EC(ret);
  38238. ret = mp_exptmod(a, b, NULL, a);
  38239. if (ret != MP_VAL)
  38240. return WC_TEST_RET_ENC_EC(ret);
  38241. ret = mp_exptmod(a, NULL, b, a);
  38242. if (ret != MP_VAL)
  38243. return WC_TEST_RET_ENC_EC(ret);
  38244. ret = mp_exptmod(NULL, b, b, a);
  38245. if (ret != MP_VAL)
  38246. return WC_TEST_RET_ENC_EC(ret);
  38247. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  38248. if (ret != MP_VAL)
  38249. return WC_TEST_RET_ENC_EC(ret);
  38250. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  38251. if (ret != MP_VAL)
  38252. return WC_TEST_RET_ENC_EC(ret);
  38253. ret = mp_exptmod_nct(NULL, b, NULL, NULL);
  38254. if (ret != MP_VAL)
  38255. return WC_TEST_RET_ENC_EC(ret);
  38256. ret = mp_exptmod_nct(NULL, NULL, b, NULL);
  38257. if (ret != MP_VAL)
  38258. return WC_TEST_RET_ENC_EC(ret);
  38259. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  38260. if (ret != MP_VAL)
  38261. return WC_TEST_RET_ENC_EC(ret);
  38262. ret = mp_exptmod_nct(a, b, b, NULL);
  38263. if (ret != MP_VAL)
  38264. return WC_TEST_RET_ENC_EC(ret);
  38265. ret = mp_exptmod_nct(a, b, NULL, a);
  38266. if (ret != MP_VAL)
  38267. return WC_TEST_RET_ENC_EC(ret);
  38268. ret = mp_exptmod_nct(a, NULL, b, a);
  38269. if (ret != MP_VAL)
  38270. return WC_TEST_RET_ENC_EC(ret);
  38271. ret = mp_exptmod_nct(NULL, b, b, a);
  38272. if (ret != MP_VAL)
  38273. return WC_TEST_RET_ENC_EC(ret);
  38274. #endif
  38275. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  38276. ret = mp_cnt_lsb(NULL);
  38277. if (ret != 0)
  38278. return WC_TEST_RET_ENC_EC(ret);
  38279. #endif
  38280. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  38281. ret = mp_prime_is_prime(NULL, 1, NULL);
  38282. if (ret != MP_VAL)
  38283. return WC_TEST_RET_ENC_EC(ret);
  38284. ret = mp_prime_is_prime(a, 1, NULL);
  38285. if (ret != MP_VAL)
  38286. return WC_TEST_RET_ENC_EC(ret);
  38287. ret = mp_prime_is_prime(NULL, 1, &result);
  38288. if (ret != MP_VAL)
  38289. return WC_TEST_RET_ENC_EC(ret);
  38290. ret = mp_prime_is_prime(a, 0, &result);
  38291. if (ret != MP_VAL)
  38292. return WC_TEST_RET_ENC_EC(ret);
  38293. ret = mp_prime_is_prime(a, 1024, &result);
  38294. if (ret != MP_VAL)
  38295. return WC_TEST_RET_ENC_EC(ret);
  38296. ret = mp_prime_is_prime_ex(NULL, 1, NULL, NULL);
  38297. if (ret != MP_VAL)
  38298. return WC_TEST_RET_ENC_EC(ret);
  38299. ret = mp_prime_is_prime_ex(a, 1, NULL, NULL);
  38300. if (ret != MP_VAL)
  38301. return WC_TEST_RET_ENC_EC(ret);
  38302. ret = mp_prime_is_prime_ex(NULL, 1, &result, NULL);
  38303. if (ret != MP_VAL)
  38304. return WC_TEST_RET_ENC_EC(ret);
  38305. ret = mp_prime_is_prime_ex(NULL, 1, NULL, rng);
  38306. if (ret != MP_VAL)
  38307. return WC_TEST_RET_ENC_EC(ret);
  38308. ret = mp_prime_is_prime_ex(a, 1, &result, NULL);
  38309. if (ret != MP_VAL)
  38310. return WC_TEST_RET_ENC_EC(ret);
  38311. ret = mp_prime_is_prime_ex(a, 1, NULL, rng);
  38312. if (ret != MP_VAL)
  38313. return WC_TEST_RET_ENC_EC(ret);
  38314. ret = mp_prime_is_prime_ex(NULL, 1, &result, rng);
  38315. if (ret != MP_VAL)
  38316. return WC_TEST_RET_ENC_EC(ret);
  38317. #endif
  38318. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || !defined(NO_DSA)
  38319. ret = mp_exch(NULL, NULL);
  38320. if (ret != MP_VAL)
  38321. return WC_TEST_RET_ENC_EC(ret);
  38322. ret = mp_exch(a, NULL);
  38323. if (ret != MP_VAL)
  38324. return WC_TEST_RET_ENC_EC(ret);
  38325. ret = mp_exch(NULL, b);
  38326. if (ret != MP_VAL)
  38327. return WC_TEST_RET_ENC_EC(ret);
  38328. #endif
  38329. #if (defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)) || \
  38330. defined(WOLFSSL_SP_MATH_ALL)
  38331. ret = mp_mul_d(NULL, 1, NULL);
  38332. if (ret != MP_VAL)
  38333. return WC_TEST_RET_ENC_EC(ret);
  38334. ret = mp_mul_d(a, 1, NULL);
  38335. if (ret != MP_VAL)
  38336. return WC_TEST_RET_ENC_EC(ret);
  38337. ret = mp_mul_d(NULL, 1, b);
  38338. if (ret != MP_VAL)
  38339. return WC_TEST_RET_ENC_EC(ret);
  38340. #endif
  38341. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  38342. ret = mp_add(NULL, NULL, NULL);
  38343. if (ret != MP_VAL)
  38344. return WC_TEST_RET_ENC_EC(ret);
  38345. ret = mp_add(a, NULL, NULL);
  38346. if (ret != MP_VAL)
  38347. return WC_TEST_RET_ENC_EC(ret);
  38348. ret = mp_add(NULL, b, NULL);
  38349. if (ret != MP_VAL)
  38350. return WC_TEST_RET_ENC_EC(ret);
  38351. ret = mp_add(NULL, NULL, r);
  38352. if (ret != MP_VAL)
  38353. return WC_TEST_RET_ENC_EC(ret);
  38354. ret = mp_add(a, b, NULL);
  38355. if (ret != MP_VAL)
  38356. return WC_TEST_RET_ENC_EC(ret);
  38357. ret = mp_add(a, NULL, r);
  38358. if (ret != MP_VAL)
  38359. return WC_TEST_RET_ENC_EC(ret);
  38360. ret = mp_add(NULL, b, r);
  38361. if (ret != MP_VAL)
  38362. return WC_TEST_RET_ENC_EC(ret);
  38363. #endif
  38364. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  38365. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  38366. ret = mp_sub(NULL, NULL, NULL);
  38367. if (ret != MP_VAL)
  38368. return WC_TEST_RET_ENC_EC(ret);
  38369. ret = mp_sub(a, NULL, NULL);
  38370. if (ret != MP_VAL)
  38371. return WC_TEST_RET_ENC_EC(ret);
  38372. ret = mp_sub(NULL, b, NULL);
  38373. if (ret != MP_VAL)
  38374. return WC_TEST_RET_ENC_EC(ret);
  38375. ret = mp_sub(NULL, NULL, r);
  38376. if (ret != MP_VAL)
  38377. return WC_TEST_RET_ENC_EC(ret);
  38378. ret = mp_sub(a, b, NULL);
  38379. if (ret != MP_VAL)
  38380. return WC_TEST_RET_ENC_EC(ret);
  38381. ret = mp_sub(a, NULL, r);
  38382. if (ret != MP_VAL)
  38383. return WC_TEST_RET_ENC_EC(ret);
  38384. ret = mp_sub(NULL, b, r);
  38385. if (ret != MP_VAL)
  38386. return WC_TEST_RET_ENC_EC(ret);
  38387. #endif
  38388. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined(WOLFSSL_SP_MATH) && \
  38389. defined(WOLFSSL_CUSTOM_CURVES))
  38390. ret = mp_addmod(NULL, NULL, NULL, NULL);
  38391. if (ret != MP_VAL)
  38392. return WC_TEST_RET_ENC_EC(ret);
  38393. ret = mp_addmod(a, NULL, NULL, NULL);
  38394. if (ret != MP_VAL)
  38395. return WC_TEST_RET_ENC_EC(ret);
  38396. ret = mp_addmod(NULL, b, NULL, NULL);
  38397. if (ret != MP_VAL)
  38398. return WC_TEST_RET_ENC_EC(ret);
  38399. ret = mp_addmod(NULL, NULL, b, NULL);
  38400. if (ret != MP_VAL)
  38401. return WC_TEST_RET_ENC_EC(ret);
  38402. ret = mp_addmod(NULL, NULL, NULL, a);
  38403. if (ret != MP_VAL)
  38404. return WC_TEST_RET_ENC_EC(ret);
  38405. ret = mp_addmod(a, b, b, NULL);
  38406. if (ret != MP_VAL)
  38407. return WC_TEST_RET_ENC_EC(ret);
  38408. ret = mp_addmod(a, b, NULL, a);
  38409. if (ret != MP_VAL)
  38410. return WC_TEST_RET_ENC_EC(ret);
  38411. ret = mp_addmod(a, NULL, b, a);
  38412. if (ret != MP_VAL)
  38413. return WC_TEST_RET_ENC_EC(ret);
  38414. ret = mp_addmod(NULL, b, b, a);
  38415. if (ret != MP_VAL)
  38416. return WC_TEST_RET_ENC_EC(ret);
  38417. #endif
  38418. #ifdef WOLFSSL_SP_MATH_ALL
  38419. ret = mp_submod(NULL, NULL, NULL, NULL);
  38420. if (ret != MP_VAL)
  38421. return WC_TEST_RET_ENC_EC(ret);
  38422. ret = mp_submod(a, NULL, NULL, NULL);
  38423. if (ret != MP_VAL)
  38424. return WC_TEST_RET_ENC_EC(ret);
  38425. ret = mp_submod(NULL, b, NULL, NULL);
  38426. if (ret != MP_VAL)
  38427. return WC_TEST_RET_ENC_EC(ret);
  38428. ret = mp_submod(NULL, NULL, b, NULL);
  38429. if (ret != MP_VAL)
  38430. return WC_TEST_RET_ENC_EC(ret);
  38431. ret = mp_submod(NULL, NULL, NULL, a);
  38432. if (ret != MP_VAL)
  38433. return WC_TEST_RET_ENC_EC(ret);
  38434. ret = mp_submod(a, b, b, NULL);
  38435. if (ret != MP_VAL)
  38436. return WC_TEST_RET_ENC_EC(ret);
  38437. ret = mp_submod(a, b, NULL, a);
  38438. if (ret != MP_VAL)
  38439. return WC_TEST_RET_ENC_EC(ret);
  38440. ret = mp_submod(a, NULL, b, a);
  38441. if (ret != MP_VAL)
  38442. return WC_TEST_RET_ENC_EC(ret);
  38443. ret = mp_submod(NULL, b, b, a);
  38444. if (ret != MP_VAL)
  38445. return WC_TEST_RET_ENC_EC(ret);
  38446. #endif
  38447. #ifdef WOLFSSL_SP_MATH_ALL
  38448. ret = mp_div_2d(NULL, 1, a, b);
  38449. if (ret != MP_VAL)
  38450. return WC_TEST_RET_ENC_EC(ret);
  38451. ret = mp_mod_2d(NULL, 1, NULL);
  38452. if (ret != MP_VAL)
  38453. return WC_TEST_RET_ENC_EC(ret);
  38454. ret = mp_mod_2d(a, 1, NULL);
  38455. if (ret != MP_VAL)
  38456. return WC_TEST_RET_ENC_EC(ret);
  38457. ret = mp_mod_2d(NULL, 1, b);
  38458. if (ret != MP_VAL)
  38459. return WC_TEST_RET_ENC_EC(ret);
  38460. ret = mp_mul_2d(NULL, 1, NULL);
  38461. if (ret != MP_VAL)
  38462. return WC_TEST_RET_ENC_EC(ret);
  38463. ret = mp_mul_2d(a, 1, NULL);
  38464. if (ret != MP_VAL)
  38465. return WC_TEST_RET_ENC_EC(ret);
  38466. ret = mp_mul_2d(NULL, 1, b);
  38467. if (ret != MP_VAL)
  38468. return WC_TEST_RET_ENC_EC(ret);
  38469. #endif
  38470. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  38471. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  38472. ret = mp_montgomery_reduce(NULL, NULL, 1);
  38473. if (ret != MP_VAL)
  38474. return WC_TEST_RET_ENC_EC(ret);
  38475. ret = mp_montgomery_reduce(a, NULL, 1);
  38476. if (ret != MP_VAL)
  38477. return WC_TEST_RET_ENC_EC(ret);
  38478. ret = mp_montgomery_reduce(NULL, b, 1);
  38479. if (ret != MP_VAL)
  38480. return WC_TEST_RET_ENC_EC(ret);
  38481. mp_zero(b);
  38482. ret = mp_montgomery_reduce(a, b, 1);
  38483. if (ret != MP_VAL)
  38484. return WC_TEST_RET_ENC_EC(ret);
  38485. #endif
  38486. #ifdef WOLFSSL_SP_MATH_ALL
  38487. ret = mp_montgomery_setup(NULL, NULL);
  38488. if (ret != MP_VAL)
  38489. return WC_TEST_RET_ENC_EC(ret);
  38490. ret = mp_montgomery_setup(a, NULL);
  38491. if (ret != MP_VAL)
  38492. return WC_TEST_RET_ENC_EC(ret);
  38493. ret = mp_montgomery_setup(NULL, &rho);
  38494. if (ret != MP_VAL)
  38495. return WC_TEST_RET_ENC_EC(ret);
  38496. ret = mp_montgomery_calc_normalization(NULL, NULL);
  38497. if (ret != MP_VAL)
  38498. return WC_TEST_RET_ENC_EC(ret);
  38499. ret = mp_montgomery_calc_normalization(a, NULL);
  38500. if (ret != MP_VAL)
  38501. return WC_TEST_RET_ENC_EC(ret);
  38502. ret = mp_montgomery_calc_normalization(NULL, b);
  38503. if (ret != MP_VAL)
  38504. return WC_TEST_RET_ENC_EC(ret);
  38505. #endif
  38506. ret = mp_unsigned_bin_size(NULL);
  38507. if (ret != 0)
  38508. return WC_TEST_RET_ENC_EC(ret);
  38509. #if defined(WC_MP_TO_RADIX) || defined(WOLFSSL_SP_MATH_ALL)
  38510. ret = mp_tohex(NULL, NULL);
  38511. if (ret != MP_VAL)
  38512. return WC_TEST_RET_ENC_EC(ret);
  38513. ret = mp_tohex(a, NULL);
  38514. if (ret != MP_VAL)
  38515. return WC_TEST_RET_ENC_EC(ret);
  38516. ret = mp_tohex(NULL, hexStr);
  38517. if (ret != MP_VAL)
  38518. return WC_TEST_RET_ENC_EC(ret);
  38519. #endif
  38520. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  38521. ret = mp_todecimal(NULL, NULL);
  38522. if (ret != MP_VAL)
  38523. return WC_TEST_RET_ENC_EC(ret);
  38524. ret = mp_todecimal(a, NULL);
  38525. if (ret != MP_VAL)
  38526. return WC_TEST_RET_ENC_EC(ret);
  38527. ret = mp_todecimal(NULL, decStr);
  38528. if (ret != MP_VAL)
  38529. return WC_TEST_RET_ENC_EC(ret);
  38530. #endif
  38531. #ifdef WOLFSSL_SP_MATH_ALL
  38532. ret = mp_toradix(NULL, NULL, MP_RADIX_HEX);
  38533. if (ret != MP_VAL)
  38534. return WC_TEST_RET_ENC_EC(ret);
  38535. ret = mp_toradix(a, NULL, MP_RADIX_HEX);
  38536. if (ret != MP_VAL)
  38537. return WC_TEST_RET_ENC_EC(ret);
  38538. ret = mp_toradix(NULL, hexStr, MP_RADIX_HEX);
  38539. if (ret != MP_VAL)
  38540. return WC_TEST_RET_ENC_EC(ret);
  38541. ret = mp_toradix(a, hexStr, 3);
  38542. if (ret != MP_VAL)
  38543. return WC_TEST_RET_ENC_EC(ret);
  38544. ret = mp_radix_size(NULL, MP_RADIX_HEX, NULL);
  38545. if (ret != MP_VAL)
  38546. return WC_TEST_RET_ENC_EC(ret);
  38547. ret = mp_radix_size(a, MP_RADIX_HEX, NULL);
  38548. if (ret != MP_VAL)
  38549. return WC_TEST_RET_ENC_EC(ret);
  38550. ret = mp_radix_size(NULL, MP_RADIX_HEX, &size);
  38551. if (ret != MP_VAL)
  38552. return WC_TEST_RET_ENC_EC(ret);
  38553. ret = mp_radix_size(a, 3, &size);
  38554. if (ret != MP_VAL)
  38555. return WC_TEST_RET_ENC_EC(ret);
  38556. #endif
  38557. return 0;
  38558. }
  38559. #endif
  38560. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  38561. static wc_test_ret_t mp_test_set_is_bit(mp_int* a)
  38562. {
  38563. int i, j;
  38564. wc_test_ret_t ret;
  38565. mp_zero(a);
  38566. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  38567. if (mp_is_bit_set(a, i))
  38568. return WC_TEST_RET_ENC_NC;
  38569. for (j = 0; j < i; j++) {
  38570. if (!mp_is_bit_set(a, j))
  38571. return WC_TEST_RET_ENC_NC;
  38572. }
  38573. if (mp_set_bit(a, i) != 0)
  38574. return WC_TEST_RET_ENC_NC;
  38575. if (!mp_is_bit_set(a, i))
  38576. return WC_TEST_RET_ENC_NC;
  38577. }
  38578. mp_zero(a);
  38579. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  38580. if (mp_is_bit_set(a, i))
  38581. return WC_TEST_RET_ENC_NC;
  38582. }
  38583. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  38584. mp_zero(a);
  38585. if (mp_set_bit(a, i) != 0)
  38586. return WC_TEST_RET_ENC_NC;
  38587. for (j = 0; j < i; j++) {
  38588. if (mp_is_bit_set(a, j))
  38589. return WC_TEST_RET_ENC_NC;
  38590. }
  38591. if (!mp_is_bit_set(a, i))
  38592. return WC_TEST_RET_ENC_NC;
  38593. }
  38594. #ifdef WOLFSSL_KEY_GEN
  38595. for (i = 0; i < DIGIT_BIT * 2; i++) {
  38596. mp_set(a, 1);
  38597. ret = mp_2expt(a, i);
  38598. if (ret != 0)
  38599. return WC_TEST_RET_ENC_EC(ret);
  38600. for (j = 0; j < i; j++) {
  38601. if (mp_is_bit_set(a, j))
  38602. return WC_TEST_RET_ENC_NC;
  38603. }
  38604. if (!mp_is_bit_set(a, i))
  38605. return WC_TEST_RET_ENC_NC;
  38606. }
  38607. #endif
  38608. #ifdef WOLFSSL_SP_MATH
  38609. mp_zero(a);
  38610. for (j = 1; j <= 3; j++) {
  38611. i = SP_INT_MAX_BITS - j;
  38612. if (mp_is_bit_set(a, i))
  38613. return WC_TEST_RET_ENC_NC;
  38614. if (mp_set_bit(a, i) != 0)
  38615. return WC_TEST_RET_ENC_NC;
  38616. if (!mp_is_bit_set(a, i))
  38617. return WC_TEST_RET_ENC_NC;
  38618. #ifdef WOLFSSL_KEY_GEN
  38619. ret = mp_2expt(a, i);
  38620. if (ret != 0)
  38621. return WC_TEST_RET_ENC_EC(ret);
  38622. if (!mp_is_bit_set(a, i))
  38623. return WC_TEST_RET_ENC_NC;
  38624. #endif
  38625. }
  38626. mp_zero(a);
  38627. for (j = 0; j <= 3; j++) {
  38628. i = SP_INT_MAX_BITS + j;
  38629. if (mp_is_bit_set(a, i))
  38630. return WC_TEST_RET_ENC_NC;
  38631. if (mp_set_bit(a, i) != MP_VAL)
  38632. return WC_TEST_RET_ENC_NC;
  38633. #ifdef WOLFSSL_KEY_GEN
  38634. ret = mp_2expt(a, i);
  38635. if (ret != MP_VAL)
  38636. return WC_TEST_RET_ENC_EC(ret);
  38637. #endif
  38638. }
  38639. #endif
  38640. (void)ret;
  38641. return 0;
  38642. }
  38643. #endif /* !WOLFSSL_SP_MATH || WOLFSSL_SP_MATH_ALL */
  38644. static wc_test_ret_t mp_test_cmp(mp_int* a, mp_int* b)
  38645. {
  38646. wc_test_ret_t ret;
  38647. mp_zero(a);
  38648. mp_zero(b);
  38649. ret = mp_cmp_d(a, 0);
  38650. if (ret != MP_EQ)
  38651. return WC_TEST_RET_ENC_EC(ret);
  38652. ret = mp_cmp_d(a, 1);
  38653. if (ret != MP_LT)
  38654. return WC_TEST_RET_ENC_EC(ret);
  38655. ret = mp_cmp(a, b);
  38656. if (ret != MP_EQ)
  38657. return WC_TEST_RET_ENC_NC;
  38658. mp_set(a, 1);
  38659. ret = mp_cmp_d(a, 0);
  38660. if (ret != MP_GT)
  38661. return WC_TEST_RET_ENC_EC(ret);
  38662. ret = mp_cmp_d(a, 1);
  38663. if (ret != MP_EQ)
  38664. return WC_TEST_RET_ENC_EC(ret);
  38665. ret = mp_cmp_d(a, 2);
  38666. if (ret != MP_LT)
  38667. return WC_TEST_RET_ENC_EC(ret);
  38668. ret = mp_cmp(a, b);
  38669. if (ret != MP_GT)
  38670. return WC_TEST_RET_ENC_NC;
  38671. mp_read_radix(b, "1234567890123456789", MP_RADIX_HEX);
  38672. ret = mp_cmp_d(b, -1);
  38673. if (ret != MP_GT)
  38674. return WC_TEST_RET_ENC_EC(ret);
  38675. ret = mp_cmp(a, b);
  38676. if (ret != MP_LT)
  38677. return WC_TEST_RET_ENC_NC;
  38678. ret = mp_cmp(b, a);
  38679. if (ret != MP_GT)
  38680. return WC_TEST_RET_ENC_NC;
  38681. ret = mp_cmp(b, b);
  38682. if (ret != MP_EQ)
  38683. return WC_TEST_RET_ENC_NC;
  38684. #if (!defined(WOLFSSL_SP_MATH) && !defined(WOLFSSL_SP_MATH_ALL)) || \
  38685. defined(WOLFSSL_SP_INT_NEGATIVE)
  38686. mp_read_radix(a, "-1", MP_RADIX_HEX);
  38687. mp_read_radix(a, "1", MP_RADIX_HEX);
  38688. ret = mp_cmp(a, b);
  38689. if (ret != MP_LT)
  38690. return WC_TEST_RET_ENC_NC;
  38691. ret = mp_cmp(b, a);
  38692. if (ret != MP_GT)
  38693. return WC_TEST_RET_ENC_NC;
  38694. mp_read_radix(b, "-2", MP_RADIX_HEX);
  38695. ret = mp_cmp(a, b);
  38696. if (ret != MP_GT)
  38697. return WC_TEST_RET_ENC_NC;
  38698. ret = mp_cmp(b, a);
  38699. if (ret != MP_LT)
  38700. return WC_TEST_RET_ENC_NC;
  38701. mp_read_radix(a, "-2", MP_RADIX_HEX);
  38702. ret = mp_cmp(a, b);
  38703. if (ret != MP_EQ)
  38704. return WC_TEST_RET_ENC_NC;
  38705. #endif
  38706. return 0;
  38707. }
  38708. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  38709. static wc_test_ret_t mp_test_shbd(mp_int* a, mp_int* b, WC_RNG* rng)
  38710. {
  38711. wc_test_ret_t ret;
  38712. int i, j, k;
  38713. #ifndef WOLFSSL_SP_MATH
  38714. for (i = 0; i < 10; i++) {
  38715. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  38716. ret = randNum(a, j, rng, NULL);
  38717. if (ret != MP_OKAY)
  38718. return WC_TEST_RET_ENC_EC(ret);
  38719. mp_copy(a, b);
  38720. for (k = 0; k <= DIGIT_BIT * 2; k++) {
  38721. ret = mp_mul_2d(a, k, a);
  38722. if (ret != MP_OKAY)
  38723. return WC_TEST_RET_ENC_EC(ret);
  38724. mp_rshb(a, k);
  38725. ret = mp_cmp(a, b);
  38726. if (ret != MP_EQ)
  38727. return WC_TEST_RET_ENC_NC;
  38728. }
  38729. }
  38730. }
  38731. #endif
  38732. for (i = 0; i < 10; i++) {
  38733. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  38734. ret = randNum(a, j, rng, NULL);
  38735. if (ret != MP_OKAY)
  38736. return WC_TEST_RET_ENC_EC(ret);
  38737. mp_copy(a, b);
  38738. for (k = 0; k < 10; k++) {
  38739. ret = mp_lshd(a, k);
  38740. if (ret != MP_OKAY)
  38741. return WC_TEST_RET_ENC_EC(ret);
  38742. #ifndef WOLFSSL_SP_MATH
  38743. mp_rshd(a, k);
  38744. #else
  38745. mp_rshb(a, k * SP_WORD_SIZE);
  38746. #endif
  38747. ret = mp_cmp(a, b);
  38748. if (ret != MP_EQ)
  38749. return WC_TEST_RET_ENC_NC;
  38750. }
  38751. }
  38752. }
  38753. #ifndef WOLFSSL_SP_MATH
  38754. mp_zero(a);
  38755. mp_rshd(a, 1);
  38756. if (!mp_iszero(a))
  38757. return WC_TEST_RET_ENC_NC;
  38758. mp_set(a, 1);
  38759. mp_rshd(a, 1);
  38760. if (!mp_iszero(a))
  38761. return WC_TEST_RET_ENC_NC;
  38762. mp_set(a, 1);
  38763. mp_rshd(a, 2);
  38764. if (!mp_iszero(a))
  38765. return WC_TEST_RET_ENC_NC;
  38766. #endif
  38767. return 0;
  38768. }
  38769. #endif
  38770. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  38771. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY) && \
  38772. !defined(WOLFSSL_RSA_PUBLIC_ONLY))
  38773. static wc_test_ret_t mp_test_div(mp_int* a, mp_int* d, mp_int* r, mp_int* rem,
  38774. WC_RNG* rng)
  38775. {
  38776. wc_test_ret_t ret;
  38777. int i, j, k;
  38778. mp_zero(a);
  38779. mp_zero(d);
  38780. ret = mp_div(a, d, r, rem);
  38781. if (ret != MP_VAL)
  38782. return WC_TEST_RET_ENC_EC(ret);
  38783. mp_set(d, 1);
  38784. ret = mp_div(a, d, r, rem);
  38785. if (ret != MP_OKAY)
  38786. return WC_TEST_RET_ENC_EC(ret);
  38787. if (!mp_iszero(r))
  38788. return WC_TEST_RET_ENC_EC(ret);
  38789. if (!mp_iszero(rem))
  38790. return WC_TEST_RET_ENC_EC(ret);
  38791. mp_set(a, 1);
  38792. ret = mp_div(a, d, r, rem);
  38793. if (ret != MP_OKAY)
  38794. return WC_TEST_RET_ENC_EC(ret);
  38795. if (!mp_isone(r))
  38796. return WC_TEST_RET_ENC_EC(ret);
  38797. if (!mp_iszero(rem))
  38798. return WC_TEST_RET_ENC_EC(ret);
  38799. for (i = 0; i < 100; i++) {
  38800. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 2; j++) {
  38801. ret = randNum(d, j, rng, NULL);
  38802. if (ret != MP_OKAY)
  38803. return WC_TEST_RET_ENC_EC(ret);
  38804. for (k = 1; k < (DIGIT_BIT + 7) / 8 * 2 + 1; k++) {
  38805. ret = randNum(a, k, rng, NULL);
  38806. if (ret != MP_OKAY)
  38807. return WC_TEST_RET_ENC_EC(ret);
  38808. ret = mp_div(a, d, NULL, rem);
  38809. if (ret != MP_OKAY)
  38810. return WC_TEST_RET_ENC_EC(ret);
  38811. ret = mp_div(a, d, r, NULL);
  38812. if (ret != MP_OKAY)
  38813. return WC_TEST_RET_ENC_EC(ret);
  38814. ret = mp_div(a, d, r, rem);
  38815. if (ret != MP_OKAY)
  38816. return WC_TEST_RET_ENC_EC(ret);
  38817. mp_mul(r, d, r);
  38818. mp_add(r, rem, r);
  38819. if (mp_cmp(r, a) != MP_EQ)
  38820. return WC_TEST_RET_ENC_NC;
  38821. }
  38822. }
  38823. }
  38824. ret = randNum(d, (DIGIT_BIT + 7) / 8 * 2, rng, NULL);
  38825. if (ret != MP_OKAY)
  38826. return WC_TEST_RET_ENC_EC(ret);
  38827. mp_add(d, d, a);
  38828. mp_set(rem, 1);
  38829. mp_div(a, d, NULL, rem);
  38830. if (ret != MP_OKAY)
  38831. return WC_TEST_RET_ENC_EC(ret);
  38832. if (!mp_iszero(rem))
  38833. return WC_TEST_RET_ENC_EC(ret);
  38834. mp_set(r, 1);
  38835. mp_div(a, d, r, NULL);
  38836. if (ret != MP_OKAY)
  38837. return WC_TEST_RET_ENC_EC(ret);
  38838. if (mp_cmp_d(r, 2) != MP_EQ)
  38839. return WC_TEST_RET_ENC_EC(ret);
  38840. mp_set(r, 1);
  38841. mp_set(rem, 1);
  38842. mp_div(a, d, r, rem);
  38843. if (ret != MP_OKAY)
  38844. return WC_TEST_RET_ENC_EC(ret);
  38845. if (mp_cmp_d(r, 2) != MP_EQ)
  38846. return WC_TEST_RET_ENC_EC(ret);
  38847. if (!mp_iszero(rem))
  38848. return WC_TEST_RET_ENC_EC(ret);
  38849. mp_set(a, 0xfe);
  38850. mp_lshd(a, 3);
  38851. mp_add_d(a, 0xff, a);
  38852. mp_set(d, 0xfe);
  38853. mp_lshd(d, 2);
  38854. ret = mp_div(a, d, r, rem);
  38855. if (ret != MP_OKAY)
  38856. return WC_TEST_RET_ENC_EC(ret);
  38857. mp_mul(r, d, d);
  38858. mp_add(rem, d, d);
  38859. if (mp_cmp(a, d) != MP_EQ)
  38860. return WC_TEST_RET_ENC_NC;
  38861. /* Force (hi | lo) / d to be (d | 0) / d which will would not fit in
  38862. * a digit. So mp_div must detect and handle.
  38863. * For example: 0x800000 / 0x8001, DIGIT_BIT = 8
  38864. */
  38865. mp_zero(a);
  38866. mp_set_bit(a, DIGIT_BIT * 3 - 1);
  38867. mp_zero(d);
  38868. mp_set_bit(d, DIGIT_BIT * 2 - 1);
  38869. mp_add_d(d, 1, d);
  38870. ret = mp_div(a, d, r, rem);
  38871. if (ret != MP_OKAY)
  38872. return WC_TEST_RET_ENC_EC(ret);
  38873. /* Make sure [d | d] / d is handled. */
  38874. mp_zero(a);
  38875. mp_set_bit(a, DIGIT_BIT * 2 - 1);
  38876. mp_set_bit(a, DIGIT_BIT * 1 - 1);
  38877. mp_zero(d);
  38878. mp_set_bit(d, DIGIT_BIT - 1);
  38879. ret = mp_div(a, d, r, rem);
  38880. if (ret != MP_OKAY)
  38881. return WC_TEST_RET_ENC_EC(ret);
  38882. mp_zero(a);
  38883. mp_set_bit(a, DIGIT_BIT);
  38884. mp_set_bit(a, 0);
  38885. mp_zero(d);
  38886. if (mp_cmp(r, a) != MP_EQ)
  38887. return WC_TEST_RET_ENC_NC;
  38888. if (mp_cmp(rem, d) != MP_EQ)
  38889. return WC_TEST_RET_ENC_NC;
  38890. return 0;
  38891. }
  38892. #endif
  38893. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  38894. !defined(WC_NO_RNG)
  38895. static wc_test_ret_t mp_test_prime(mp_int* a, WC_RNG* rng)
  38896. {
  38897. wc_test_ret_t ret;
  38898. int res;
  38899. ret = mp_rand_prime(a, 1, rng, NULL);
  38900. #if defined(WOLFSSL_SP_MATH_ALL)
  38901. if (ret != 0)
  38902. return WC_TEST_RET_ENC_EC(ret);
  38903. #else
  38904. if (ret != MP_VAL)
  38905. return WC_TEST_RET_ENC_NC;
  38906. #endif
  38907. #ifndef WOLFSSL_SP_MATH
  38908. ret = mp_rand_prime(a, -5, rng, NULL);
  38909. if (ret != 0 || (a->dp[0] & 3) != 3)
  38910. return WC_TEST_RET_ENC_NC;
  38911. #endif
  38912. ret = mp_prime_is_prime(a, 1, &res);
  38913. if (ret != MP_OKAY)
  38914. return WC_TEST_RET_ENC_EC(ret);
  38915. #ifndef WOLFSSL_SP_MATH
  38916. if (res != MP_YES)
  38917. return WC_TEST_RET_ENC_EC(res);
  38918. #else
  38919. if (res != MP_NO)
  38920. return WC_TEST_RET_ENC_EC(res);
  38921. #endif
  38922. ret = mp_prime_is_prime(a, 0, &res);
  38923. if (ret != MP_VAL)
  38924. return WC_TEST_RET_ENC_EC(ret);
  38925. ret = mp_prime_is_prime(a, -1, &res);
  38926. if (ret != MP_VAL)
  38927. return WC_TEST_RET_ENC_EC(ret);
  38928. ret = mp_prime_is_prime(a, 257, &res);
  38929. if (ret != MP_VAL)
  38930. return WC_TEST_RET_ENC_EC(ret);
  38931. mp_set(a, 1);
  38932. ret = mp_prime_is_prime(a, 1, &res);
  38933. if (ret != MP_OKAY)
  38934. return WC_TEST_RET_ENC_EC(ret);
  38935. if (res != MP_NO)
  38936. return WC_TEST_RET_ENC_EC(res);
  38937. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  38938. if (ret != MP_OKAY)
  38939. return WC_TEST_RET_ENC_EC(ret);
  38940. if (res != MP_NO)
  38941. return WC_TEST_RET_ENC_EC(res);
  38942. mp_set(a, 2);
  38943. ret = mp_prime_is_prime(a, 1, &res);
  38944. if (ret != MP_OKAY)
  38945. return WC_TEST_RET_ENC_EC(ret);
  38946. if (res != MP_YES)
  38947. return WC_TEST_RET_ENC_EC(res);
  38948. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  38949. if (ret != MP_OKAY)
  38950. return WC_TEST_RET_ENC_EC(ret);
  38951. if (res != MP_YES)
  38952. return WC_TEST_RET_ENC_EC(res);
  38953. mp_set(a, 0xfb);
  38954. ret = mp_prime_is_prime(a, 1, &res);
  38955. if (ret != MP_OKAY)
  38956. return WC_TEST_RET_ENC_EC(ret);
  38957. if (res != MP_YES)
  38958. return WC_TEST_RET_ENC_EC(res);
  38959. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  38960. if (ret != MP_OKAY)
  38961. return WC_TEST_RET_ENC_EC(ret);
  38962. if (res != MP_YES)
  38963. return WC_TEST_RET_ENC_EC(res);
  38964. mp_set(a, 0x6);
  38965. ret = mp_prime_is_prime(a, 1, &res);
  38966. if (ret != MP_OKAY)
  38967. return WC_TEST_RET_ENC_EC(ret);
  38968. if (res != MP_NO)
  38969. return WC_TEST_RET_ENC_EC(res);
  38970. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  38971. if (ret != MP_OKAY)
  38972. return WC_TEST_RET_ENC_EC(ret);
  38973. if (res != MP_NO)
  38974. return WC_TEST_RET_ENC_EC(res);
  38975. mp_set_int(a, 0x655 * 0x65b);
  38976. ret = mp_prime_is_prime(a, 10, &res);
  38977. if (ret != MP_OKAY)
  38978. return WC_TEST_RET_ENC_EC(ret);
  38979. if (res != MP_NO)
  38980. return WC_TEST_RET_ENC_EC(res);
  38981. ret = mp_prime_is_prime_ex(a, 10, &res, rng);
  38982. if (ret != MP_OKAY)
  38983. return WC_TEST_RET_ENC_EC(ret);
  38984. if (res != MP_NO)
  38985. return WC_TEST_RET_ENC_EC(res);
  38986. return 0;
  38987. }
  38988. #endif
  38989. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  38990. static wc_test_ret_t mp_test_lcm_gcd(mp_int* a, mp_int* b, mp_int* r, mp_int* exp,
  38991. WC_RNG* rng)
  38992. {
  38993. wc_test_ret_t ret;
  38994. int i;
  38995. WOLFSSL_SMALL_STACK_STATIC const int kat[][3] = {
  38996. { 1, 1, 1 }, { 2, 1, 2 }, { 1, 2, 2 }, { 2, 4, 4 }, { 4, 2, 4 },
  38997. { 12, 56, 168 }, { 56, 12, 168 }
  38998. };
  38999. (void)exp;
  39000. mp_set(a, 0);
  39001. mp_set(b, 1);
  39002. ret = mp_lcm(a, a, r);
  39003. if (ret != MP_VAL)
  39004. return WC_TEST_RET_ENC_EC(ret);
  39005. ret = mp_lcm(a, b, r);
  39006. if (ret != MP_VAL)
  39007. return WC_TEST_RET_ENC_EC(ret);
  39008. ret = mp_lcm(b, a, r);
  39009. if (ret != MP_VAL)
  39010. return WC_TEST_RET_ENC_EC(ret);
  39011. for (i = 0; i < (int)(sizeof(kat) / sizeof(*kat)); i++) {
  39012. mp_set(a, kat[i][0]);
  39013. mp_set(b, kat[i][1]);
  39014. ret = mp_lcm(a, b, r);
  39015. if (ret != MP_OKAY)
  39016. return WC_TEST_RET_ENC_EC(ret);
  39017. mp_set(exp, kat[i][2]);
  39018. ret = mp_cmp(r, exp);
  39019. if (ret != MP_EQ)
  39020. return WC_TEST_RET_ENC_NC;
  39021. }
  39022. (void)rng;
  39023. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  39024. !defined(WC_NO_RNG)
  39025. ret = mp_rand_prime(a, 20, rng, NULL);
  39026. if (ret != MP_OKAY)
  39027. return WC_TEST_RET_ENC_EC(ret);
  39028. ret = mp_rand_prime(b, 20, rng, NULL);
  39029. if (ret != MP_OKAY)
  39030. return WC_TEST_RET_ENC_EC(ret);
  39031. ret = mp_mul(a, b, exp);
  39032. if (ret != MP_OKAY)
  39033. return WC_TEST_RET_ENC_EC(ret);
  39034. ret = mp_lcm(a, b, r);
  39035. if (ret != MP_OKAY)
  39036. return WC_TEST_RET_ENC_EC(ret);
  39037. ret = mp_cmp(r, exp);
  39038. if (ret != MP_EQ)
  39039. return WC_TEST_RET_ENC_NC;
  39040. ret = mp_lcm(b, a, r);
  39041. if (ret != MP_OKAY)
  39042. return WC_TEST_RET_ENC_EC(ret);
  39043. ret = mp_cmp(r, exp);
  39044. if (ret != MP_EQ)
  39045. return WC_TEST_RET_ENC_NC;
  39046. #endif
  39047. mp_set(a, 11);
  39048. mp_zero(b);
  39049. ret = mp_gcd(a, b, r);
  39050. if (ret != MP_OKAY)
  39051. return WC_TEST_RET_ENC_EC(ret);
  39052. ret = mp_cmp_d(r, 11);
  39053. if (ret != MP_EQ)
  39054. return WC_TEST_RET_ENC_EC(ret);
  39055. ret = mp_gcd(b, a, r);
  39056. if (ret != MP_OKAY)
  39057. return WC_TEST_RET_ENC_EC(ret);
  39058. ret = mp_cmp_d(r, 11);
  39059. if (ret != MP_EQ)
  39060. return WC_TEST_RET_ENC_EC(ret);
  39061. ret = mp_gcd(b, b, r);
  39062. if (ret != MP_VAL)
  39063. return WC_TEST_RET_ENC_EC(ret);
  39064. return 0;
  39065. }
  39066. #endif
  39067. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  39068. defined(WOLFSSL_SP_MATH_ALL)
  39069. static wc_test_ret_t mp_test_mod_2d(mp_int* a, mp_int* r, mp_int* t, WC_RNG* rng)
  39070. {
  39071. wc_test_ret_t ret;
  39072. int i;
  39073. int j;
  39074. mp_set(a, 10);
  39075. ret = mp_mod_2d(a, 0, r);
  39076. if (ret != MP_OKAY)
  39077. return WC_TEST_RET_ENC_EC(ret);
  39078. if (!mp_iszero(r))
  39079. return WC_TEST_RET_ENC_NC;
  39080. ret = mp_mod_2d(a, 1, r);
  39081. if (ret != MP_OKAY)
  39082. return WC_TEST_RET_ENC_EC(ret);
  39083. if (!mp_iszero(r))
  39084. return WC_TEST_RET_ENC_NC;
  39085. ret = mp_mod_2d(a, 2, r);
  39086. if (ret != MP_OKAY)
  39087. return WC_TEST_RET_ENC_EC(ret);
  39088. ret = mp_cmp_d(r, 2);
  39089. if (ret != 0)
  39090. return WC_TEST_RET_ENC_EC(ret);
  39091. for (i = 2; i < 20; i++) {
  39092. ret = randNum(a, i, rng, NULL);
  39093. if (ret != 0)
  39094. return WC_TEST_RET_ENC_EC(ret);
  39095. for (j = 1; j <= mp_count_bits(a); j++) {
  39096. /* Get top part */
  39097. ret = mp_div_2d(a, j, t, NULL);
  39098. if (ret != 0)
  39099. return WC_TEST_RET_ENC_EC(ret);
  39100. ret = mp_mul_2d(t, j, t);
  39101. if (ret != 0)
  39102. return WC_TEST_RET_ENC_EC(ret);
  39103. /* Get bottom part */
  39104. ret = mp_mod_2d(a, j, r);
  39105. if (ret != 0)
  39106. return WC_TEST_RET_ENC_EC(ret);
  39107. /* Reassemble */
  39108. ret = mp_add(t, r, r);
  39109. if (ret != 0)
  39110. return WC_TEST_RET_ENC_EC(ret);
  39111. ret = mp_cmp(a, r);
  39112. if (ret != MP_EQ)
  39113. return WC_TEST_RET_ENC_NC;
  39114. }
  39115. }
  39116. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_SP_INT_NEGATIVE)
  39117. /* Test negative value being moded. */
  39118. for (j = 0; j < 20; j++) {
  39119. ret = randNum(a, 2, rng, NULL);
  39120. if (ret != 0)
  39121. return WC_TEST_RET_ENC_EC(ret);
  39122. a->sign = MP_NEG;
  39123. for (i = 1; i < DIGIT_BIT * 3 + 1; i++) {
  39124. ret = mp_mod_2d(a, i, r);
  39125. if (ret != 0)
  39126. return WC_TEST_RET_ENC_EC(ret);
  39127. mp_zero(t);
  39128. ret = mp_set_bit(t, i);
  39129. if (ret != 0)
  39130. return WC_TEST_RET_ENC_EC(ret);
  39131. ret = mp_mod(a, t, t);
  39132. if (ret != 0)
  39133. return WC_TEST_RET_ENC_EC(ret);
  39134. ret = mp_cmp(r, t);
  39135. if (ret != MP_EQ)
  39136. return WC_TEST_RET_ENC_NC;
  39137. }
  39138. }
  39139. #endif
  39140. return 0;
  39141. }
  39142. #endif
  39143. #if defined(WOLFSSL_SP_MATH_ALL) || defined(OPENSSL_EXTRA) || \
  39144. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY))
  39145. static wc_test_ret_t mp_test_mod_d(mp_int* a, WC_RNG* rng)
  39146. {
  39147. wc_test_ret_t ret;
  39148. mp_digit r;
  39149. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_SP_MATH)
  39150. mp_digit rem;
  39151. int i;
  39152. #endif
  39153. (void)rng;
  39154. ret = mp_set(a, 1);
  39155. if (ret != MP_OKAY)
  39156. return WC_TEST_RET_ENC_EC(ret);
  39157. ret = mp_mod_d(a, 0, &r);
  39158. if (ret != MP_VAL)
  39159. return WC_TEST_RET_ENC_EC(ret);
  39160. mp_zero(a);
  39161. ret = mp_mod_d(a, 1, &r);
  39162. if (ret != MP_OKAY)
  39163. return WC_TEST_RET_ENC_EC(ret);
  39164. ret = mp_mod_d(a, 3, &r);
  39165. if (ret != MP_OKAY)
  39166. return WC_TEST_RET_ENC_EC(ret);
  39167. ret = mp_mod_d(a, 5, &r);
  39168. if (ret != MP_OKAY)
  39169. return WC_TEST_RET_ENC_EC(ret);
  39170. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_SP_MATH)
  39171. for (i = MP_MAX_TEST_BYTE_LEN - 16; i <= MP_MAX_TEST_BYTE_LEN; i++) {
  39172. ret = randNum(a, i, rng, NULL);
  39173. if (ret != MP_OKAY)
  39174. return WC_TEST_RET_ENC_EC(ret);
  39175. ret = mp_mod_d(a, 3, &r);
  39176. if (ret != MP_OKAY)
  39177. return WC_TEST_RET_ENC_EC(ret);
  39178. ret = mp_div_d(a, 3, a, &rem);
  39179. if (ret != MP_OKAY)
  39180. return WC_TEST_RET_ENC_EC(ret);
  39181. if (r != rem)
  39182. return WC_TEST_RET_ENC_NC;
  39183. }
  39184. #endif
  39185. return 0;
  39186. }
  39187. #endif
  39188. static wc_test_ret_t mp_test_mul_sqr(mp_int* a, mp_int* b, mp_int* r1, mp_int* r2,
  39189. WC_RNG* rng)
  39190. {
  39191. wc_test_ret_t ret;
  39192. int i;
  39193. for (i = 1; i < 16; i++) {
  39194. ret = randNum(a, i, rng, NULL);
  39195. if (ret != 0)
  39196. return WC_TEST_RET_ENC_EC(ret);
  39197. ret = mp_mul(a, a, r1);
  39198. if (ret != 0)
  39199. return WC_TEST_RET_ENC_EC(ret);
  39200. ret = mp_sqr(a, r2);
  39201. if (ret != 0)
  39202. return WC_TEST_RET_ENC_EC(ret);
  39203. ret = mp_cmp(r1, r2);
  39204. if (ret != MP_EQ)
  39205. return WC_TEST_RET_ENC_NC;
  39206. }
  39207. ret = mp_set(b, 0);
  39208. if (ret != MP_OKAY)
  39209. return WC_TEST_RET_ENC_EC(ret);
  39210. ret = mp_mul(a, b, r1);
  39211. if (ret != MP_OKAY)
  39212. return WC_TEST_RET_ENC_EC(ret);
  39213. if (!mp_iszero(r1))
  39214. return WC_TEST_RET_ENC_EC(ret);
  39215. ret = mp_sqr(b, r1);
  39216. if (ret != MP_OKAY)
  39217. return WC_TEST_RET_ENC_EC(ret);
  39218. if (!mp_iszero(r1))
  39219. return WC_TEST_RET_ENC_NC;
  39220. #ifdef WOLFSSL_SP_MATH_ALL
  39221. ret = mp_set(a, 1);
  39222. if (ret != MP_OKAY)
  39223. return WC_TEST_RET_ENC_EC(ret);
  39224. i = (SP_INT_DIGITS / 2) + 1;
  39225. ret = mp_mul_2d(a, i * SP_WORD_SIZE - 1, a);
  39226. if (ret != MP_OKAY)
  39227. return WC_TEST_RET_ENC_EC(ret);
  39228. ret = mp_set(b, 1);
  39229. if (ret != MP_OKAY)
  39230. return WC_TEST_RET_ENC_EC(ret);
  39231. ret = mp_mul_2d(b, (SP_INT_DIGITS - 1 - i) * SP_WORD_SIZE - 1, b);
  39232. if (ret != MP_OKAY)
  39233. return WC_TEST_RET_ENC_EC(ret);
  39234. ret = mp_mul(a, b, r1);
  39235. if (ret != MP_OKAY)
  39236. return WC_TEST_RET_ENC_EC(ret);
  39237. ret = mp_mul(a, a, r1);
  39238. if (ret == MP_OKAY)
  39239. return WC_TEST_RET_ENC_NC;
  39240. ret = mp_sqr(a, r1);
  39241. if (ret == MP_OKAY)
  39242. return WC_TEST_RET_ENC_NC;
  39243. ret = mp_sqr(b, r1);
  39244. if (ret != MP_OKAY)
  39245. return WC_TEST_RET_ENC_EC(ret);
  39246. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  39247. (defined(HAVE_ECC) && defined(FP_ECC))
  39248. ret = mp_mulmod(a, b, b, r1);
  39249. if (ret != MP_OKAY)
  39250. return WC_TEST_RET_ENC_EC(ret);
  39251. ret = mp_mulmod(a, a, b, r1);
  39252. if (ret == MP_OKAY)
  39253. return WC_TEST_RET_ENC_NC;
  39254. #if defined(HAVE_ECC) && (defined(ECC_SHAMIR) || defined(FP_ECC))
  39255. ret = mp_sqrmod(a, b, r1);
  39256. if (ret == MP_OKAY)
  39257. return WC_TEST_RET_ENC_NC;
  39258. ret = mp_sqrmod(b, a, r1);
  39259. if (ret != MP_OKAY)
  39260. return WC_TEST_RET_ENC_EC(ret);
  39261. #endif /* HAVE_ECC && (ECC_SHAMIR || FP_ECC) */
  39262. #endif /* WOLFSSL_SP_MATH_ALL || WOLFSSL_HAVE_SP_DH || (HAVE_ECC && FP_ECC) */
  39263. #endif /* WOLFSSL_SP_MATH_ALL */
  39264. return 0;
  39265. }
  39266. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  39267. defined(OPENSSL_EXTRA)
  39268. static wc_test_ret_t mp_test_invmod(mp_int* a, mp_int* m, mp_int* r)
  39269. {
  39270. wc_test_ret_t ret;
  39271. mp_set(a, 0);
  39272. mp_set(m, 1);
  39273. ret = mp_invmod(a, m, r);
  39274. if (ret != MP_VAL)
  39275. return WC_TEST_RET_ENC_EC(ret);
  39276. ret = mp_invmod(m, a, r);
  39277. if (ret != MP_VAL)
  39278. return WC_TEST_RET_ENC_EC(ret);
  39279. mp_set(a, 2);
  39280. mp_set(m, 4);
  39281. ret = mp_invmod(a, m, r);
  39282. if (ret != MP_VAL)
  39283. return WC_TEST_RET_ENC_EC(ret);
  39284. mp_set(a, 3);
  39285. mp_set(m, 6);
  39286. ret = mp_invmod(a, m, r);
  39287. if (ret != MP_VAL)
  39288. return WC_TEST_RET_ENC_EC(ret);
  39289. mp_set(a, 5*9);
  39290. mp_set(m, 6*9);
  39291. ret = mp_invmod(a, m, r);
  39292. if (ret != MP_VAL)
  39293. return WC_TEST_RET_ENC_EC(ret);
  39294. mp_set(a, 1);
  39295. mp_set(m, 4);
  39296. ret = mp_invmod(a, m, r);
  39297. if (ret != MP_OKAY)
  39298. return WC_TEST_RET_ENC_EC(ret);
  39299. if (!mp_isone(r))
  39300. return WC_TEST_RET_ENC_NC;
  39301. mp_set(a, 3);
  39302. mp_set(m, 4);
  39303. ret = mp_invmod(a, m, r);
  39304. if (ret != MP_OKAY)
  39305. return WC_TEST_RET_ENC_EC(ret);
  39306. ret = mp_cmp_d(r, 3);
  39307. if (ret != 0)
  39308. return WC_TEST_RET_ENC_EC(ret);
  39309. mp_set(a, 3);
  39310. mp_set(m, 5);
  39311. ret = mp_invmod(a, m, r);
  39312. if (ret != MP_OKAY)
  39313. return WC_TEST_RET_ENC_EC(ret);
  39314. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  39315. /* Maximum 'a' */
  39316. mp_set(a, 0);
  39317. mp_set_bit(a, (r->size / 2)* SP_WORD_SIZE - 1);
  39318. mp_sub_d(a, 1, a);
  39319. /* Modulus too big. */
  39320. mp_set(m, 0);
  39321. mp_set_bit(m, (r->size / 2) * SP_WORD_SIZE);
  39322. ret = mp_invmod(a, m, r);
  39323. if (ret != MP_VAL)
  39324. return WC_TEST_RET_ENC_EC(ret);
  39325. /* Maximum modulus - even. */
  39326. mp_set(m, 0);
  39327. mp_set_bit(m, (r->size / 2) * SP_WORD_SIZE - 1);
  39328. ret = mp_invmod(a, m, r);
  39329. if (ret != MP_OKAY)
  39330. return WC_TEST_RET_ENC_EC(ret);
  39331. #endif
  39332. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_INT_NEGATIVE)
  39333. mp_read_radix(a, "-3", 16);
  39334. ret = mp_invmod(a, m, r);
  39335. if (ret != MP_OKAY)
  39336. return WC_TEST_RET_ENC_EC(ret);
  39337. #endif
  39338. #if defined(WOLFSSL_SP_MATH_ALL) && defined(HAVE_ECC)
  39339. mp_set(a, 0);
  39340. mp_set(m, 3);
  39341. ret = mp_invmod_mont_ct(a, m, r, 1);
  39342. if (ret != MP_VAL)
  39343. return WC_TEST_RET_ENC_EC(ret);
  39344. mp_set(a, 1);
  39345. mp_set(m, 0);
  39346. ret = mp_invmod_mont_ct(a, m, r, 1);
  39347. if (ret != MP_VAL)
  39348. return WC_TEST_RET_ENC_EC(ret);
  39349. mp_set(a, 1);
  39350. mp_set(m, 1);
  39351. ret = mp_invmod_mont_ct(a, m, r, 1);
  39352. if (ret != MP_VAL)
  39353. return WC_TEST_RET_ENC_EC(ret);
  39354. mp_set(a, 1);
  39355. mp_set(m, 2);
  39356. ret = mp_invmod_mont_ct(a, m, r, 1);
  39357. if (ret != MP_VAL)
  39358. return WC_TEST_RET_ENC_EC(ret);
  39359. mp_set(a, 1);
  39360. mp_set(m, 3);
  39361. ret = mp_invmod_mont_ct(a, m, r, 1);
  39362. if (ret != MP_OKAY)
  39363. return WC_TEST_RET_ENC_EC(ret);
  39364. #endif
  39365. return 0;
  39366. }
  39367. #endif /* !NO_RSA || HAVE_ECC || !NO_DSA || OPENSSL_EXTRA */
  39368. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  39369. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  39370. static wc_test_ret_t mp_test_exptmod(mp_int* b, mp_int* e, mp_int* m, mp_int* r)
  39371. {
  39372. wc_test_ret_t ret;
  39373. mp_set(b, 0x2);
  39374. mp_set(e, 0x3);
  39375. mp_set(m, 0x0);
  39376. ret = mp_exptmod_ex(b, e, 1, m, r);
  39377. if (ret != MP_VAL)
  39378. return WC_TEST_RET_ENC_EC(ret);
  39379. ret = mp_exptmod_nct(b, e, m, r);
  39380. if (ret != MP_VAL)
  39381. return WC_TEST_RET_ENC_EC(ret);
  39382. mp_set(b, 0x2);
  39383. mp_set(e, 0x3);
  39384. mp_set(m, 0x1);
  39385. ret = mp_exptmod_ex(b, e, 1, m, r);
  39386. if (ret != MP_OKAY)
  39387. return WC_TEST_RET_ENC_EC(ret);
  39388. if (!mp_iszero(r))
  39389. return WC_TEST_RET_ENC_NC;
  39390. ret = mp_exptmod_nct(b, e, m, r);
  39391. if (ret != MP_OKAY)
  39392. return WC_TEST_RET_ENC_EC(ret);
  39393. if (!mp_iszero(r))
  39394. return WC_TEST_RET_ENC_NC;
  39395. mp_set(b, 0x2);
  39396. mp_set(e, 0x0);
  39397. mp_set(m, 0x7);
  39398. ret = mp_exptmod_ex(b, e, 1, m, r);
  39399. if (ret != MP_OKAY)
  39400. return WC_TEST_RET_ENC_EC(ret);
  39401. if (!mp_isone(r))
  39402. return WC_TEST_RET_ENC_NC;
  39403. ret = mp_exptmod_nct(b, e, m, r);
  39404. if (ret != MP_OKAY)
  39405. return WC_TEST_RET_ENC_EC(ret);
  39406. if (!mp_isone(r))
  39407. return WC_TEST_RET_ENC_NC;
  39408. mp_set(b, 0x0);
  39409. mp_set(e, 0x3);
  39410. mp_set(m, 0x7);
  39411. ret = mp_exptmod_ex(b, e, 1, m, r);
  39412. if (ret != MP_OKAY)
  39413. return WC_TEST_RET_ENC_EC(ret);
  39414. if (!mp_iszero(r))
  39415. return WC_TEST_RET_ENC_NC;
  39416. ret = mp_exptmod_nct(b, e, m, r);
  39417. if (ret != MP_OKAY)
  39418. return WC_TEST_RET_ENC_EC(ret);
  39419. if (!mp_iszero(r))
  39420. return WC_TEST_RET_ENC_NC;
  39421. mp_set(b, 0x10);
  39422. mp_set(e, 0x3);
  39423. mp_set(m, 0x7);
  39424. ret = mp_exptmod_ex(b, e, 1, m, r);
  39425. if (ret != MP_OKAY)
  39426. return WC_TEST_RET_ENC_EC(ret);
  39427. ret = mp_exptmod_nct(b, e, m, r);
  39428. if (ret != MP_OKAY)
  39429. return WC_TEST_RET_ENC_EC(ret);
  39430. mp_set(b, 0x7);
  39431. mp_set(e, 0x3);
  39432. mp_set(m, 0x7);
  39433. ret = mp_exptmod_ex(b, e, 1, m, r);
  39434. if (ret != MP_OKAY)
  39435. return WC_TEST_RET_ENC_EC(ret);
  39436. if (!mp_iszero(r))
  39437. return WC_TEST_RET_ENC_NC;
  39438. ret = mp_exptmod_nct(b, e, m, r);
  39439. if (ret != MP_OKAY)
  39440. return WC_TEST_RET_ENC_EC(ret);
  39441. if (!mp_iszero(r))
  39442. return WC_TEST_RET_ENC_NC;
  39443. #ifndef WOLFSSL_SP_MATH
  39444. mp_set(b, 0x01);
  39445. mp_mul_2d(b, DIGIT_BIT, b);
  39446. mp_add_d(b, 1, b);
  39447. mp_set(e, 0x3);
  39448. mp_copy(b, m);
  39449. ret = mp_exptmod_ex(b, e, 1, m, r);
  39450. if (ret != MP_OKAY)
  39451. return WC_TEST_RET_ENC_EC(ret);
  39452. if (!mp_iszero(r))
  39453. return WC_TEST_RET_ENC_NC;
  39454. ret = mp_exptmod_nct(b, e, m, r);
  39455. if (ret != MP_OKAY)
  39456. return WC_TEST_RET_ENC_EC(ret);
  39457. if (!mp_iszero(r))
  39458. return WC_TEST_RET_ENC_NC;
  39459. #endif
  39460. mp_set(b, 0x2);
  39461. mp_set(e, 0x3);
  39462. mp_set(m, 0x7);
  39463. ret = mp_exptmod_ex(b, e, 1, m, r);
  39464. if (ret != MP_OKAY)
  39465. return WC_TEST_RET_ENC_EC(ret);
  39466. ret = mp_exptmod_nct(b, e, m, r);
  39467. if (ret != MP_OKAY)
  39468. return WC_TEST_RET_ENC_EC(ret);
  39469. #ifdef WOLFSSL_SP_MATH_ALL
  39470. mp_set(b, 0x2);
  39471. mp_set(e, 0x3);
  39472. mp_set(m, 0x01);
  39473. mp_mul_2d(m, SP_WORD_SIZE * SP_INT_DIGITS / 2, m);
  39474. mp_add_d(m, 0x01, m);
  39475. ret = mp_exptmod_ex(b, e, 1, m, r);
  39476. if (ret != MP_VAL)
  39477. return WC_TEST_RET_ENC_EC(ret);
  39478. ret = mp_exptmod_nct(b, e, m, r);
  39479. if (ret != MP_VAL)
  39480. return WC_TEST_RET_ENC_EC(ret);
  39481. #endif
  39482. return 0;
  39483. }
  39484. #endif /* !NO_RSA || !NO_DSA || !NO_DH || (HAVE_ECC && HAVE_COMP_KEY) ||
  39485. * OPENSSL_EXTRA */
  39486. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  39487. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  39488. static wc_test_ret_t mp_test_mont(mp_int* a, mp_int* m, mp_int* n, mp_int* r, WC_RNG* rng)
  39489. {
  39490. wc_test_ret_t ret;
  39491. mp_digit mp;
  39492. static int exp[] = { 7, 8, 16, 27, 32, 64,
  39493. 127, 128, 255, 256,
  39494. #if defined(SP_WORD_SIZE) && SP_WORD_SIZE > 8
  39495. 383, 384, 2033, 2048
  39496. #endif
  39497. };
  39498. static mp_digit sub[] = { 0x01, 0x05, 0x0f, 0x27, 0x05, 0x3b,
  39499. 0x01, 0x9f, 0x13, 0xbd,
  39500. #if defined(SP_WORD_SIZE) && SP_WORD_SIZE > 8
  39501. 0x1f, 0x13d, 0x45, 0x615
  39502. #endif
  39503. };
  39504. int bits[] = { 256, 384,
  39505. #if defined(SP_INT_MAX_BITS) && SP_INT_MAX_BITS > 4096
  39506. 2048,
  39507. #endif
  39508. #if defined(SP_INT_MAX_BITS) && SP_INT_MAX_BITS > 6144
  39509. 3072
  39510. #endif
  39511. };
  39512. int i;
  39513. int j;
  39514. for (i = 0; i < (int)(sizeof(exp) / sizeof(*exp)); i++) {
  39515. if (exp[i] >= DIGIT_BIT)
  39516. continue;
  39517. mp_zero(m);
  39518. ret = mp_set_bit(m, exp[i]);
  39519. if (ret != MP_OKAY)
  39520. return WC_TEST_RET_ENC_EC(ret);
  39521. ret = mp_sub_d(m, sub[i], m);
  39522. if (ret != MP_OKAY)
  39523. return WC_TEST_RET_ENC_EC(ret);
  39524. ret = mp_montgomery_setup(m, &mp);
  39525. if (ret != MP_OKAY)
  39526. return WC_TEST_RET_ENC_EC(ret);
  39527. ret = mp_montgomery_calc_normalization(n, m);
  39528. if (ret != MP_OKAY)
  39529. return WC_TEST_RET_ENC_EC(ret);
  39530. for (j = 0; j < 10; j++) {
  39531. ret = randNum(a, (exp[i] + DIGIT_BIT - 1) / DIGIT_BIT, rng, NULL);
  39532. if (ret != 0)
  39533. return WC_TEST_RET_ENC_EC(ret);
  39534. ret = mp_mod(a, m, a);
  39535. if (ret != 0)
  39536. return WC_TEST_RET_ENC_EC(ret);
  39537. /* r = a * a */
  39538. ret = mp_sqrmod(a, m, r);
  39539. if (ret != MP_OKAY)
  39540. return WC_TEST_RET_ENC_EC(ret);
  39541. /* Convert to Montgomery form = a*n */
  39542. ret = mp_mulmod(a, n, m, a);
  39543. if (ret != MP_OKAY)
  39544. return WC_TEST_RET_ENC_EC(ret);
  39545. /* a*a mod m == ((a*n) * (a*n)) / n / n */
  39546. ret = mp_sqr(a, a);
  39547. if (ret != MP_OKAY)
  39548. return WC_TEST_RET_ENC_EC(ret);
  39549. ret = mp_montgomery_reduce(a, m, mp);
  39550. if (ret != MP_OKAY)
  39551. return WC_TEST_RET_ENC_EC(ret);
  39552. ret = mp_montgomery_reduce(a, m, mp);
  39553. if (ret != MP_OKAY)
  39554. return WC_TEST_RET_ENC_EC(ret);
  39555. if (mp_cmp(a, r) != MP_EQ)
  39556. return WC_TEST_RET_ENC_NC;
  39557. }
  39558. }
  39559. /* Force carries. */
  39560. for (i = 0; i < (int)(sizeof(bits) / sizeof(*bits)); i++) {
  39561. /* a = 2^(bits*2) - 1 */
  39562. mp_zero(a);
  39563. mp_set_bit(a, bits[i] * 2);
  39564. mp_sub_d(a, 1, a);
  39565. /* m = 2^(bits) - 1 */
  39566. mp_zero(m);
  39567. mp_set_bit(m, bits[i]);
  39568. mp_sub_d(m, 1, m);
  39569. mp = 1;
  39570. /* result = r = 2^(bits) - 1 */
  39571. mp_zero(r);
  39572. mp_set_bit(r, bits[i]);
  39573. mp_sub_d(r, 1, r);
  39574. ret = mp_montgomery_reduce(a, m, mp);
  39575. if (ret != MP_OKAY)
  39576. return WC_TEST_RET_ENC_EC(ret);
  39577. /* Result is m or 0 if reduced to range of modulus. */
  39578. if (mp_cmp(a, r) != MP_EQ && mp_iszero(a) != MP_YES)
  39579. return WC_TEST_RET_ENC_NC;
  39580. }
  39581. return 0;
  39582. }
  39583. #endif
  39584. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mp_test(void)
  39585. {
  39586. WC_RNG rng;
  39587. int rng_inited = 0;
  39588. wc_test_ret_t ret;
  39589. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  39590. int i, j;
  39591. #ifndef WOLFSSL_SP_MATH
  39592. int k;
  39593. #endif
  39594. mp_digit d = 0;
  39595. #endif
  39596. #ifdef WOLFSSL_SMALL_STACK
  39597. mp_int *a = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  39598. DYNAMIC_TYPE_TMP_BUFFER),
  39599. *b = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  39600. DYNAMIC_TYPE_TMP_BUFFER),
  39601. *r1 = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  39602. DYNAMIC_TYPE_TMP_BUFFER),
  39603. *r2 = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  39604. DYNAMIC_TYPE_TMP_BUFFER),
  39605. *p = (mp_int *)XMALLOC(sizeof(mp_int), HEAP_HINT,
  39606. DYNAMIC_TYPE_TMP_BUFFER);
  39607. if ((a == NULL) ||
  39608. (b == NULL) ||
  39609. (r1 == NULL) ||
  39610. (r2 == NULL) ||
  39611. (p == NULL))
  39612. {
  39613. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, done);
  39614. }
  39615. #else
  39616. mp_int a[1], b[1], r1[1], r2[1], p[1];
  39617. #endif
  39618. ret = mp_init_multi(a, b, r1, r2, NULL, NULL);
  39619. if (ret != 0)
  39620. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  39621. #ifdef WOLFSSL_SP_MATH_ALL
  39622. mp_init_copy(p, a);
  39623. #else
  39624. ret = mp_init(p);
  39625. if (ret != 0)
  39626. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  39627. #endif
  39628. #ifndef HAVE_FIPS
  39629. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  39630. #else
  39631. ret = wc_InitRng(&rng);
  39632. #endif
  39633. if (ret != 0)
  39634. goto done;
  39635. rng_inited = 1;
  39636. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  39637. mp_set_int(a, 0);
  39638. if (a->used != 0 || a->dp[0] != 0)
  39639. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  39640. for (j = 1; j <= MP_MAX_TEST_BYTE_LEN; j++) {
  39641. for (i = 0; i < 4 * j; i++) {
  39642. /* New values to use. */
  39643. ret = randNum(p, j, &rng, NULL);
  39644. if (ret != 0)
  39645. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  39646. ret = randNum(a, j, &rng, NULL);
  39647. if (ret != 0)
  39648. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  39649. ret = randNum(b, j, &rng, NULL);
  39650. if (ret != 0)
  39651. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  39652. ret = wc_RNG_GenerateBlock(&rng, (byte*)&d, sizeof(d));
  39653. if (ret != 0)
  39654. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  39655. d &= MP_MASK;
  39656. #if !defined(WOLFSSL_SP_MATH) || (defined(HAVE_ECC) && \
  39657. (defined(ECC_SHAMIR) || defined(FP_ECC)))
  39658. /* Ensure sqrmod produce same result as mulmod. */
  39659. ret = mp_sqrmod(a, p, r1);
  39660. if (ret != 0)
  39661. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  39662. ret = mp_mulmod(a, a, p, r2);
  39663. if (ret != 0)
  39664. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  39665. if (mp_cmp(r1, r2) != 0)
  39666. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  39667. #endif
  39668. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  39669. #if defined(WOLFSSL_SP_MATH) || (defined(WOLFSSL_SP_MATH_ALL) && \
  39670. !defined(WOLFSSL_SP_INT_NEGATIVE))
  39671. ret = mp_addmod(a, b, p, r1);
  39672. if (ret != 0)
  39673. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  39674. ret = mp_submod(r1, b, p, r2);
  39675. if (ret != 0)
  39676. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  39677. ret = mp_mod(a, p, r1);
  39678. if (ret != 0)
  39679. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  39680. if (mp_cmp(r1, r2) != MP_EQ)
  39681. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  39682. #else
  39683. /* Ensure add with mod produce same result as sub with mod. */
  39684. ret = mp_addmod(a, b, p, r1);
  39685. if (ret != 0)
  39686. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  39687. b->sign ^= 1;
  39688. ret = mp_submod(a, b, p, r2);
  39689. if (ret != 0)
  39690. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  39691. if (mp_cmp(r1, r2) != 0)
  39692. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  39693. #endif
  39694. #endif
  39695. /* Ensure add digit produce same result as sub digit. */
  39696. ret = mp_add_d(a, d, r1);
  39697. if (ret != 0)
  39698. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  39699. ret = mp_sub_d(r1, d, r2);
  39700. if (ret != 0)
  39701. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  39702. if (mp_cmp(a, r2) != 0)
  39703. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  39704. /* Invert - if p is even it will use the slow impl.
  39705. * - if p and a are even it will fail.
  39706. */
  39707. ret = mp_invmod(a, p, r1);
  39708. if (ret != 0 && ret != MP_VAL)
  39709. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  39710. #ifndef WOLFSSL_SP_MATH
  39711. /* Shift up and down number all bits in a digit. */
  39712. for (k = 0; k < DIGIT_BIT; k++) {
  39713. mp_mul_2d(a, k, r1);
  39714. mp_div_2d(r1, k, r2, p);
  39715. if (mp_cmp(a, r2) != 0)
  39716. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  39717. if (!mp_iszero(p))
  39718. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  39719. mp_rshb(r1, k);
  39720. if (mp_cmp(a, r1) != 0)
  39721. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  39722. }
  39723. #endif
  39724. }
  39725. }
  39726. /* Test adding and subtracting zero from zero. */
  39727. mp_zero(a);
  39728. ret = mp_add_d(a, 0, r1);
  39729. if (ret != 0)
  39730. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  39731. if (!mp_iszero(r1)) {
  39732. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  39733. }
  39734. ret = mp_sub_d(a, 0, r2);
  39735. if (ret != 0)
  39736. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done);
  39737. if (!mp_iszero(r2)) {
  39738. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  39739. }
  39740. #if DIGIT_BIT >= 32
  39741. /* Check that setting a 32-bit digit works. */
  39742. d &= 0xffffffffU;
  39743. mp_set_int(a, d);
  39744. if (a->used != 1 || a->dp[0] != d)
  39745. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  39746. #endif
  39747. /* Check setting a bit and testing a bit works. */
  39748. for (i = 0; i < MP_MAX_TEST_BYTE_LEN * 8; i++) {
  39749. mp_zero(a);
  39750. mp_set_bit(a, i);
  39751. if (!mp_is_bit_set(a, i))
  39752. ERROR_OUT(WC_TEST_RET_ENC_NC, done);
  39753. }
  39754. #endif
  39755. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  39756. mp_zero(a);
  39757. i = mp_cnt_lsb(a);
  39758. if (i != 0)
  39759. ERROR_OUT(WC_TEST_RET_ENC_I(i), done);
  39760. mp_set(a, 1);
  39761. i = mp_cnt_lsb(a);
  39762. if (i != 0)
  39763. ERROR_OUT(WC_TEST_RET_ENC_I(i), done);
  39764. mp_set(a, 32);
  39765. i = mp_cnt_lsb(a);
  39766. if (i != 5)
  39767. ERROR_OUT(WC_TEST_RET_ENC_I(i), done);
  39768. mp_zero(a);
  39769. mp_set_bit(a, 129);
  39770. i = mp_cnt_lsb(a);
  39771. if (i != 129)
  39772. ERROR_OUT(WC_TEST_RET_ENC_I(i), done);
  39773. #endif
  39774. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  39775. if ((ret = mp_test_param(a, b, r1, &rng)) != 0)
  39776. goto done;
  39777. #endif
  39778. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  39779. if ((ret = mp_test_div_3(a, r1, &rng)) != 0)
  39780. goto done;
  39781. #endif
  39782. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  39783. !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  39784. (!defined WOLFSSL_SP_MATH && !defined(WOLFSSL_SP_MATH_ALL) && \
  39785. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  39786. if ((ret = mp_test_radix_10(a, r1, &rng)) != 0)
  39787. goto done;
  39788. #endif
  39789. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  39790. defined(HAVE_ECC))
  39791. if ((ret = mp_test_radix_16(a, r1, &rng)) != 0)
  39792. goto done;
  39793. #endif
  39794. if ((ret = mp_test_shift(a, r1, &rng)) != 0)
  39795. goto done;
  39796. if ((ret = mp_test_add_sub_d(a, r1)) != 0)
  39797. goto done;
  39798. if ((ret = mp_test_read_to_bin(a)) != 0)
  39799. goto done;
  39800. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  39801. if ((ret = mp_test_set_int(a)) != 0)
  39802. goto done;
  39803. #endif
  39804. if ((ret = mp_test_cmp(a, r1)) != 0)
  39805. goto done;
  39806. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  39807. if ((ret = mp_test_shbd(a, b, &rng)) != 0)
  39808. goto done;
  39809. #endif
  39810. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  39811. if ((ret = mp_test_set_is_bit(a)) != 0)
  39812. goto done;
  39813. #endif
  39814. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  39815. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY) && \
  39816. !defined(WOLFSSL_RSA_PUBLIC_ONLY))
  39817. if ((ret = mp_test_div(a, b, r1, r2, &rng)) != 0)
  39818. goto done;
  39819. #endif
  39820. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  39821. !defined(WC_NO_RNG)
  39822. if ((ret = mp_test_prime(a, &rng)) != 0)
  39823. goto done;
  39824. #endif
  39825. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  39826. if ((ret = mp_test_lcm_gcd(a, b, r1, r2, &rng)) != 0)
  39827. goto done;
  39828. #endif
  39829. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  39830. defined(WOLFSSL_SP_MATH_ALL)
  39831. if ((ret = mp_test_mod_2d(a, r1, p, &rng)) != 0)
  39832. goto done;
  39833. #endif
  39834. #if defined(WOLFSSL_SP_MATH_ALL) || defined(OPENSSL_EXTRA) || \
  39835. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY))
  39836. if ((ret = mp_test_mod_d(a, &rng)) != 0)
  39837. goto done;
  39838. #endif
  39839. if ((ret = mp_test_mul_sqr(a, b, r1, r2, &rng)) != 0)
  39840. goto done;
  39841. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  39842. defined(OPENSSL_EXTRA)
  39843. if ((ret = mp_test_invmod(a, b, r1)) != 0)
  39844. goto done;
  39845. #endif
  39846. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  39847. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  39848. if ((ret = mp_test_exptmod(a, b, r1, r2)) != 0)
  39849. goto done;
  39850. #endif
  39851. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  39852. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  39853. if ((ret = mp_test_mont(a, b, r1, r2, &rng)) != 0)
  39854. goto done;
  39855. #endif
  39856. done:
  39857. #ifdef WOLFSSL_SMALL_STACK
  39858. if (p) {
  39859. mp_clear(p);
  39860. XFREE(p, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39861. }
  39862. if (r2) {
  39863. mp_clear(r2);
  39864. XFREE(r2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39865. }
  39866. if (r1) {
  39867. mp_clear(r1);
  39868. XFREE(r1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39869. }
  39870. if (b) {
  39871. mp_clear(b);
  39872. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39873. }
  39874. if (a) {
  39875. mp_clear(a);
  39876. XFREE(a, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39877. }
  39878. #else
  39879. mp_clear(p);
  39880. mp_clear(r2);
  39881. mp_clear(r1);
  39882. mp_clear(b);
  39883. mp_clear(a);
  39884. #endif
  39885. if (rng_inited)
  39886. wc_FreeRng(&rng);
  39887. return ret;
  39888. }
  39889. #endif /* WOLFSSL_PUBLIC_MP && ((WOLFSSL_SP_MATH_ALL &&
  39890. * !WOLFSSL_RSA_VERIFY_ONLY) || USE_FAST_MATH) */
  39891. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  39892. typedef struct pairs_t {
  39893. const unsigned char* coeff;
  39894. int coeffSz;
  39895. int exp;
  39896. } pairs_t;
  39897. /*
  39898. n =p1p2p3, where pi = ki(p1-1)+1 with (k2,k3) = (173,293)
  39899. p1 = 2^192 * 0x000000000000e24fd4f6d6363200bf2323ec46285cac1d3a
  39900. + 2^0 * 0x0b2488b0c29d96c5e67f8bec15b54b189ae5636efe89b45b
  39901. */
  39902. static const unsigned char c192a[] =
  39903. {
  39904. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe2, 0x4f,
  39905. 0xd4, 0xf6, 0xd6, 0x36, 0x32, 0x00, 0xbf, 0x23,
  39906. 0x23, 0xec, 0x46, 0x28, 0x5c, 0xac, 0x1d, 0x3a
  39907. };
  39908. static const unsigned char c0a[] =
  39909. {
  39910. 0x0b, 0x24, 0x88, 0xb0, 0xc2, 0x9d, 0x96, 0xc5,
  39911. 0xe6, 0x7f, 0x8b, 0xec, 0x15, 0xb5, 0x4b, 0x18,
  39912. 0x9a, 0xe5, 0x63, 0x6e, 0xfe, 0x89, 0xb4, 0x5b
  39913. };
  39914. static const pairs_t ecPairsA[] =
  39915. {
  39916. {c192a, sizeof(c192a), 192},
  39917. {c0a, sizeof(c0a), 0}
  39918. };
  39919. static const int kA[] = {173, 293};
  39920. static const unsigned char controlPrime[] = {
  39921. 0xe1, 0x76, 0x45, 0x80, 0x59, 0xb6, 0xd3, 0x49,
  39922. 0xdf, 0x0a, 0xef, 0x12, 0xd6, 0x0f, 0xf0, 0xb7,
  39923. 0xcb, 0x2a, 0x37, 0xbf, 0xa7, 0xf8, 0xb5, 0x4d,
  39924. 0xf5, 0x31, 0x35, 0xad, 0xe4, 0xa3, 0x94, 0xa1,
  39925. 0xdb, 0xf1, 0x96, 0xad, 0xb5, 0x05, 0x64, 0x85,
  39926. 0x83, 0xfc, 0x1b, 0x5b, 0x29, 0xaa, 0xbe, 0xf8,
  39927. 0x26, 0x3f, 0x76, 0x7e, 0xad, 0x1c, 0xf0, 0xcb,
  39928. 0xd7, 0x26, 0xb4, 0x1b, 0x05, 0x8e, 0x56, 0x86,
  39929. 0x7e, 0x08, 0x62, 0x21, 0xc1, 0x86, 0xd6, 0x47,
  39930. 0x79, 0x3e, 0xb7, 0x5d, 0xa4, 0xc6, 0x3a, 0xd7,
  39931. 0xb1, 0x74, 0x20, 0xf6, 0x50, 0x97, 0x41, 0x04,
  39932. 0x53, 0xed, 0x3f, 0x26, 0xd6, 0x6f, 0x91, 0xfa,
  39933. 0x68, 0x26, 0xec, 0x2a, 0xdc, 0x9a, 0xf1, 0xe7,
  39934. 0xdc, 0xfb, 0x73, 0xf0, 0x79, 0x43, 0x1b, 0x21,
  39935. 0xa3, 0x59, 0x04, 0x63, 0x52, 0x07, 0xc9, 0xd7,
  39936. 0xe6, 0xd1, 0x1b, 0x5d, 0x5e, 0x96, 0xfa, 0x53
  39937. };
  39938. static const unsigned char testOne[] = { 1 };
  39939. static wc_test_ret_t GenerateNextP(mp_int* p1, mp_int* p2, int k)
  39940. {
  39941. wc_test_ret_t ret;
  39942. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39943. mp_int *ki = (mp_int *)XMALLOC(sizeof(*ki), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39944. if (ki == NULL)
  39945. return MEMORY_E;
  39946. #else
  39947. mp_int ki[1];
  39948. #endif
  39949. ret = mp_init(ki);
  39950. if (ret != 0)
  39951. ret = WC_TEST_RET_ENC_EC(ret);
  39952. if (ret == 0) {
  39953. ret = mp_set(ki, k);
  39954. if (ret != 0)
  39955. ret = WC_TEST_RET_ENC_EC(ret);
  39956. }
  39957. if (ret == 0) {
  39958. ret = mp_sub_d(p1, 1, p2);
  39959. if (ret != 0)
  39960. ret = WC_TEST_RET_ENC_EC(ret);
  39961. }
  39962. if (ret == 0) {
  39963. ret = mp_mul(p2, ki, p2);
  39964. if (ret != 0)
  39965. ret = WC_TEST_RET_ENC_EC(ret);
  39966. }
  39967. if (ret == 0) {
  39968. ret = mp_add_d(p2, 1, p2);
  39969. if (ret != 0)
  39970. ret = WC_TEST_RET_ENC_EC(ret);
  39971. }
  39972. mp_clear(ki);
  39973. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39974. XFREE(ki, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39975. #endif
  39976. return ret;
  39977. }
  39978. static wc_test_ret_t GenerateP(mp_int* p1, mp_int* p2, mp_int* p3,
  39979. const pairs_t* ecPairs, int ecPairsSz,
  39980. const int* k)
  39981. {
  39982. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39983. mp_int *x = NULL, *y = NULL;
  39984. #else
  39985. mp_int x[1], y[1];
  39986. #endif
  39987. wc_test_ret_t ret;
  39988. int i;
  39989. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39990. if (((x = (mp_int *)XMALLOC(sizeof(*x), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL) ||
  39991. ((y = (mp_int *)XMALLOC(sizeof(*x), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)) {
  39992. ret = MEMORY_E;
  39993. goto out;
  39994. }
  39995. #endif
  39996. ret = mp_init_multi(x, y, NULL, NULL, NULL, NULL);
  39997. if (ret != 0) {
  39998. ret = WC_TEST_RET_ENC_EC(ret);
  39999. goto out;
  40000. }
  40001. for (i = 0; ret == 0 && i < ecPairsSz; i++) {
  40002. ret = mp_read_unsigned_bin(x, ecPairs[i].coeff, ecPairs[i].coeffSz);
  40003. if (ret != 0) {
  40004. ret = WC_TEST_RET_ENC_EC(ret);
  40005. break;
  40006. }
  40007. /* p1 = 2^exp */
  40008. ret = mp_2expt(y, ecPairs[i].exp);
  40009. if (ret != 0) {
  40010. ret = WC_TEST_RET_ENC_EC(ret);
  40011. break;
  40012. }
  40013. /* p1 = p1 * m */
  40014. ret = mp_mul(x, y, x);
  40015. if (ret != 0) {
  40016. ret = WC_TEST_RET_ENC_EC(ret);
  40017. break;
  40018. }
  40019. /* p1 += */
  40020. ret = mp_add(p1, x, p1);
  40021. if (ret != 0) {
  40022. ret = WC_TEST_RET_ENC_EC(ret);
  40023. break;
  40024. }
  40025. mp_zero(x);
  40026. mp_zero(y);
  40027. }
  40028. if (ret == 0)
  40029. ret = GenerateNextP(p1, p2, k[0]);
  40030. if (ret == 0)
  40031. ret = GenerateNextP(p1, p3, k[1]);
  40032. out:
  40033. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  40034. if (x != NULL) {
  40035. mp_clear(x);
  40036. XFREE(x, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40037. }
  40038. if (y != NULL) {
  40039. mp_clear(y);
  40040. XFREE(y, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40041. }
  40042. #else
  40043. mp_clear(x);
  40044. mp_clear(y);
  40045. #endif
  40046. return ret;
  40047. }
  40048. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t prime_test(void)
  40049. {
  40050. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  40051. mp_int *n = (mp_int *)XMALLOC(sizeof *n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  40052. *p1 = (mp_int *)XMALLOC(sizeof *p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  40053. *p2 = (mp_int *)XMALLOC(sizeof *p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  40054. *p3 = (mp_int *)XMALLOC(sizeof *p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40055. #else
  40056. mp_int n[1],
  40057. p1[1],
  40058. p2[1],
  40059. p3[1];
  40060. #endif
  40061. wc_test_ret_t ret;
  40062. int isPrime = 0;
  40063. WC_RNG rng;
  40064. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  40065. if ((n == NULL) ||
  40066. (p1 == NULL) ||
  40067. (p2 == NULL) ||
  40068. (p3 == NULL))
  40069. ERROR_OUT(MEMORY_E, out);
  40070. #endif
  40071. ret = wc_InitRng(&rng);
  40072. if (ret != 0)
  40073. ret = WC_TEST_RET_ENC_EC(ret);
  40074. if (ret == 0) {
  40075. ret = mp_init_multi(n, p1, p2, p3, NULL, NULL);
  40076. if (ret != 0)
  40077. ret = WC_TEST_RET_ENC_EC(ret);
  40078. }
  40079. if (ret == 0)
  40080. ret = GenerateP(p1, p2, p3,
  40081. ecPairsA, sizeof(ecPairsA) / sizeof(ecPairsA[0]), kA);
  40082. if (ret == 0) {
  40083. ret = mp_mul(p1, p2, n);
  40084. if (ret != 0)
  40085. ret = WC_TEST_RET_ENC_EC(ret);
  40086. }
  40087. if (ret == 0) {
  40088. ret = mp_mul(n, p3, n);
  40089. if (ret != 0)
  40090. ret = WC_TEST_RET_ENC_EC(ret);
  40091. }
  40092. if (ret != 0)
  40093. ERROR_OUT(ret, out);
  40094. /* Check the old prime test using the number that false positives.
  40095. * This test result should indicate as not prime. */
  40096. ret = mp_prime_is_prime(n, 40, &isPrime);
  40097. if (ret != 0)
  40098. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40099. if (isPrime)
  40100. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  40101. /* This test result should fail. It should indicate the value as prime. */
  40102. ret = mp_prime_is_prime(n, 8, &isPrime);
  40103. if (ret != 0)
  40104. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40105. if (!isPrime)
  40106. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  40107. /* This test result should indicate the value as not prime. */
  40108. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  40109. if (ret != 0)
  40110. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40111. if (isPrime)
  40112. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  40113. ret = mp_read_unsigned_bin(n, controlPrime, sizeof(controlPrime));
  40114. if (ret != 0)
  40115. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40116. /* This test result should indicate the value as prime. */
  40117. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  40118. if (ret != 0)
  40119. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40120. if (!isPrime)
  40121. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  40122. /* This test result should indicate the value as prime. */
  40123. isPrime = -1;
  40124. ret = mp_prime_is_prime(n, 8, &isPrime);
  40125. if (ret != 0)
  40126. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40127. if (!isPrime)
  40128. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  40129. ret = mp_read_unsigned_bin(n, testOne, sizeof(testOne));
  40130. if (ret != 0)
  40131. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40132. /* This test result should indicate the value as not prime. */
  40133. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  40134. if (ret != 0)
  40135. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40136. if (isPrime)
  40137. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  40138. ret = mp_prime_is_prime(n, 8, &isPrime);
  40139. if (ret != 0)
  40140. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
  40141. if (isPrime)
  40142. ERROR_OUT(WC_TEST_RET_ENC_NC, out);
  40143. ret = 0;
  40144. out:
  40145. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  40146. if (n != NULL) {
  40147. mp_clear(n);
  40148. XFREE(n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40149. }
  40150. if (p1 != NULL) {
  40151. mp_clear(p1);
  40152. XFREE(p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40153. }
  40154. if (p2 != NULL) {
  40155. mp_clear(p2);
  40156. XFREE(p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40157. }
  40158. if (p3 != NULL) {
  40159. mp_clear(p3);
  40160. XFREE(p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40161. }
  40162. #else
  40163. mp_clear(p3);
  40164. mp_clear(p2);
  40165. mp_clear(p1);
  40166. mp_clear(n);
  40167. #endif
  40168. wc_FreeRng(&rng);
  40169. return ret;
  40170. }
  40171. #endif /* WOLFSSL_PUBLIC_MP */
  40172. #if defined(ASN_BER_TO_DER) && \
  40173. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  40174. defined(OPENSSL_EXTRA_X509_SMALL))
  40175. /* wc_BerToDer is only public facing in the case of test cert or opensslextra */
  40176. typedef struct berDerTestData {
  40177. const byte *in;
  40178. word32 inSz;
  40179. const byte *out;
  40180. word32 outSz;
  40181. } berDerTestData;
  40182. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t berder_test(void)
  40183. {
  40184. wc_test_ret_t ret;
  40185. int i;
  40186. word32 len = 0, l;
  40187. byte out[32];
  40188. WOLFSSL_SMALL_STACK_STATIC const byte good1_in[] = { 0x30, 0x80, 0x00, 0x00 };
  40189. WOLFSSL_SMALL_STACK_STATIC const byte good1_out[] = { 0x30, 0x00 };
  40190. WOLFSSL_SMALL_STACK_STATIC const byte good2_in[] = { 0x30, 0x80, 0x02, 0x01, 0x01, 0x00, 0x00 };
  40191. WOLFSSL_SMALL_STACK_STATIC const byte good2_out[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  40192. WOLFSSL_SMALL_STACK_STATIC const byte good3_in[] = {
  40193. 0x24, 0x80, 0x04, 0x01, 0x01, 0x00, 0x00
  40194. };
  40195. WOLFSSL_SMALL_STACK_STATIC const byte good3_out[] = { 0x04, 0x1, 0x01 };
  40196. WOLFSSL_SMALL_STACK_STATIC const byte good4_in[] = {
  40197. 0x30, 0x80,
  40198. 0x02, 0x01, 0x01,
  40199. 0x30, 0x80,
  40200. 0x24, 0x80,
  40201. 0x04, 0x01, 0x01,
  40202. 0x04, 0x02, 0x02, 0x03,
  40203. 0x00, 0x00,
  40204. 0x06, 0x01, 0x01,
  40205. 0x00, 0x00,
  40206. 0x31, 0x80,
  40207. 0x06, 0x01, 0x01,
  40208. 0x00, 0x00,
  40209. 0x00, 0x00,
  40210. };
  40211. WOLFSSL_SMALL_STACK_STATIC const byte good4_out[] = {
  40212. 0x30, 0x12,
  40213. 0x02, 0x01, 0x01,
  40214. 0x30, 0x08,
  40215. 0x04, 0x03, 0x01, 0x02, 0x03,
  40216. 0x06, 0x01, 0x01,
  40217. 0x31, 0x03,
  40218. 0x06, 0x01, 0x01
  40219. };
  40220. WOLFSSL_SMALL_STACK_STATIC const byte good5_in[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  40221. berDerTestData testData[] = {
  40222. { good1_in, sizeof(good1_in), good1_out, sizeof(good1_out) },
  40223. { good2_in, sizeof(good2_in), good2_out, sizeof(good2_out) },
  40224. { good3_in, sizeof(good3_in), good3_out, sizeof(good3_out) },
  40225. { good4_in, sizeof(good4_in), good4_out, sizeof(good4_out) },
  40226. { good5_in, sizeof(good5_in), good5_in , sizeof(good5_in ) },
  40227. };
  40228. for (i = 0; i < (int)(sizeof(testData) / sizeof(*testData)); i++) {
  40229. ret = wc_BerToDer(testData[i].in, testData[i].inSz, NULL, &len);
  40230. if (ret != LENGTH_ONLY_E)
  40231. return WC_TEST_RET_ENC_I(i);
  40232. if (len != testData[i].outSz)
  40233. return WC_TEST_RET_ENC_I(i);
  40234. len = testData[i].outSz;
  40235. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &len);
  40236. if (ret != 0)
  40237. return WC_TEST_RET_ENC_I(i);
  40238. if (XMEMCMP(out, testData[i].out, len) != 0)
  40239. return WC_TEST_RET_ENC_I(i);
  40240. for (l = 1; l < testData[i].inSz; l++) {
  40241. ret = wc_BerToDer(testData[i].in, l, NULL, &len);
  40242. if (ret != ASN_PARSE_E)
  40243. return WC_TEST_RET_ENC_EC(ret);
  40244. len = testData[i].outSz;
  40245. ret = wc_BerToDer(testData[i].in, l, out, &len);
  40246. if (ret != ASN_PARSE_E)
  40247. return WC_TEST_RET_ENC_EC(ret);
  40248. }
  40249. for (l = 0; l < testData[i].outSz-1; l++) {
  40250. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &l);
  40251. if (ret != BUFFER_E)
  40252. return WC_TEST_RET_ENC_EC(ret);
  40253. }
  40254. }
  40255. ret = wc_BerToDer(NULL, 4, NULL, NULL);
  40256. if (ret != BAD_FUNC_ARG)
  40257. return WC_TEST_RET_ENC_EC(ret);
  40258. ret = wc_BerToDer(out, 4, NULL, NULL);
  40259. if (ret != BAD_FUNC_ARG)
  40260. return WC_TEST_RET_ENC_EC(ret);
  40261. ret = wc_BerToDer(NULL, 4, NULL, &len);
  40262. if (ret != BAD_FUNC_ARG)
  40263. return WC_TEST_RET_ENC_EC(ret);
  40264. ret = wc_BerToDer(NULL, 4, out, NULL);
  40265. if (ret != BAD_FUNC_ARG)
  40266. return WC_TEST_RET_ENC_EC(ret);
  40267. ret = wc_BerToDer(out, 4, out, NULL);
  40268. if (ret != BAD_FUNC_ARG)
  40269. return WC_TEST_RET_ENC_EC(ret);
  40270. ret = wc_BerToDer(NULL, 4, out, &len);
  40271. if (ret != BAD_FUNC_ARG)
  40272. return WC_TEST_RET_ENC_EC(ret);
  40273. for (l = 1; l < sizeof(good4_out); l++) {
  40274. len = l;
  40275. ret = wc_BerToDer(good4_in, sizeof(good4_in), out, &len);
  40276. if (ret != BUFFER_E)
  40277. return WC_TEST_RET_ENC_EC(ret);
  40278. }
  40279. return 0;
  40280. }
  40281. #endif
  40282. #ifdef DEBUG_WOLFSSL
  40283. static THREAD_LS_T int log_cnt = 0;
  40284. static void my_Logging_cb(const int logLevel, const char *const logMessage)
  40285. {
  40286. (void)logLevel;
  40287. (void)logMessage;
  40288. log_cnt++;
  40289. }
  40290. #endif /* DEBUG_WOLFSSL */
  40291. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t logging_test(void)
  40292. {
  40293. wc_test_ret_t ret;
  40294. #ifdef DEBUG_WOLFSSL
  40295. const char* msg = "Testing, testing. 1, 2, 3, 4 ...";
  40296. byte a[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  40297. byte b[256];
  40298. int i;
  40299. for (i = 0; i < (int)sizeof(b); i++)
  40300. b[i] = i;
  40301. ret = wolfSSL_Debugging_ON();
  40302. if (ret != 0)
  40303. return WC_TEST_RET_ENC_EC(ret);
  40304. ret = wolfSSL_SetLoggingCb(my_Logging_cb);
  40305. if (ret != 0)
  40306. return WC_TEST_RET_ENC_EC(ret);
  40307. WOLFSSL_MSG(msg);
  40308. WOLFSSL_BUFFER(a, sizeof(a));
  40309. WOLFSSL_BUFFER(b, sizeof(b));
  40310. WOLFSSL_BUFFER(NULL, 0);
  40311. WOLFSSL_ERROR(MEMORY_E);
  40312. WOLFSSL_ERROR_MSG(msg);
  40313. /* turn off logs */
  40314. wolfSSL_Debugging_OFF();
  40315. /* capture log count */
  40316. i = log_cnt;
  40317. /* validate no logs are output when disabled */
  40318. WOLFSSL_MSG(msg);
  40319. WOLFSSL_BUFFER(a, sizeof(a));
  40320. WOLFSSL_BUFFER(b, sizeof(b));
  40321. WOLFSSL_BUFFER(NULL, 0);
  40322. WOLFSSL_ERROR(MEMORY_E);
  40323. WOLFSSL_ERROR_MSG(msg);
  40324. /* check the logs were disabled */
  40325. if (i != log_cnt)
  40326. return WC_TEST_RET_ENC_NC;
  40327. /* restore callback and leave logging enabled */
  40328. wolfSSL_SetLoggingCb(NULL);
  40329. wolfSSL_Debugging_ON();
  40330. /* suppress unused args */
  40331. (void)a;
  40332. (void)b;
  40333. #else
  40334. ret = wolfSSL_Debugging_ON();
  40335. if (ret != NOT_COMPILED_IN)
  40336. return WC_TEST_RET_ENC_EC(ret);
  40337. wolfSSL_Debugging_OFF();
  40338. ret = wolfSSL_SetLoggingCb(NULL);
  40339. if (ret != NOT_COMPILED_IN)
  40340. return WC_TEST_RET_ENC_EC(ret);
  40341. #endif /* DEBUG_WOLFSSL */
  40342. return 0;
  40343. }
  40344. #if defined(__INCLUDE_NUTTX_CONFIG_H)
  40345. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t wolfcrypt_mutex_test(void)
  40346. #else
  40347. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t mutex_test(void)
  40348. #endif
  40349. {
  40350. #ifdef WOLFSSL_PTHREADS
  40351. wolfSSL_Mutex m;
  40352. #endif
  40353. #if defined(WOLFSSL_PTHREADS) || (!defined(WOLFSSL_NO_MALLOC) && \
  40354. !defined(WOLFSSL_USER_MUTEX) && defined(WOLFSSL_STATIC_MEMORY))
  40355. wc_test_ret_t ret;
  40356. #endif
  40357. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_USER_MUTEX)
  40358. #ifndef WOLFSSL_STATIC_MEMORY
  40359. wolfSSL_Mutex *mm = wc_InitAndAllocMutex();
  40360. #else
  40361. wolfSSL_Mutex *mm = (wolfSSL_Mutex*) XMALLOC(sizeof(wolfSSL_Mutex),
  40362. HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  40363. if (mm != NULL) {
  40364. ret = wc_InitMutex(mm);
  40365. if (ret != 0) {
  40366. WOLFSSL_MSG("Init Mutex failed");
  40367. XFREE(mm, HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  40368. return WC_TEST_RET_ENC_EC(ret);
  40369. }
  40370. }
  40371. #endif
  40372. if (mm == NULL)
  40373. return WC_TEST_RET_ENC_ERRNO;
  40374. wc_FreeMutex(mm);
  40375. XFREE(mm, HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  40376. #endif
  40377. /* Can optionally enable advanced pthread tests using "ENABLE_PTHREAD_LOCKFREE_TESTS" */
  40378. #ifdef WOLFSSL_PTHREADS
  40379. ret = wc_InitMutex(&m);
  40380. if (ret != 0)
  40381. return WC_TEST_RET_ENC_EC(ret);
  40382. ret = wc_LockMutex(&m);
  40383. if (ret != 0)
  40384. return WC_TEST_RET_ENC_EC(ret);
  40385. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  40386. /* trying to free a locked mutex is not portable behavior with pthread */
  40387. /* Attempting to destroy a locked mutex results in undefined behavior */
  40388. ret = wc_FreeMutex(&m);
  40389. if (ret != BAD_MUTEX_E)
  40390. return WC_TEST_RET_ENC_EC(ret);
  40391. #endif
  40392. ret = wc_UnLockMutex(&m);
  40393. if (ret != 0)
  40394. return WC_TEST_RET_ENC_EC(ret);
  40395. ret = wc_FreeMutex(&m);
  40396. if (ret != 0)
  40397. return WC_TEST_RET_ENC_EC(ret);
  40398. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  40399. /* Trying to use a pthread after free'ing is not portable behavior */
  40400. ret = wc_LockMutex(&m);
  40401. if (ret != BAD_MUTEX_E)
  40402. return WC_TEST_RET_ENC_EC(ret);
  40403. ret = wc_UnLockMutex(&m);
  40404. if (ret != BAD_MUTEX_E)
  40405. return WC_TEST_RET_ENC_EC(ret);
  40406. #endif
  40407. #endif
  40408. return 0;
  40409. }
  40410. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  40411. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  40412. !defined(WOLFSSL_STATIC_MEMORY)
  40413. static wc_test_ret_t malloc_cnt = 0;
  40414. static wc_test_ret_t realloc_cnt = 0;
  40415. static wc_test_ret_t free_cnt = 0;
  40416. #ifdef WOLFSSL_DEBUG_MEMORY
  40417. static void *my_Malloc_cb(size_t size, const char* func, unsigned int line)
  40418. {
  40419. (void) func;
  40420. (void) line;
  40421. #else
  40422. static void *my_Malloc_cb(size_t size)
  40423. {
  40424. #endif
  40425. malloc_cnt++;
  40426. #ifndef WOLFSSL_NO_MALLOC
  40427. return malloc(size);
  40428. #else
  40429. WOLFSSL_MSG("No malloc available");
  40430. (void)size;
  40431. return NULL;
  40432. #endif
  40433. }
  40434. #ifdef WOLFSSL_DEBUG_MEMORY
  40435. static void my_Free_cb(void *ptr, const char* func, unsigned int line)
  40436. {
  40437. (void) func;
  40438. (void) line;
  40439. #else
  40440. static void my_Free_cb(void *ptr)
  40441. {
  40442. #endif
  40443. free_cnt++;
  40444. #ifndef WOLFSSL_NO_MALLOC
  40445. free(ptr);
  40446. #else
  40447. WOLFSSL_MSG("No free available");
  40448. (void)ptr;
  40449. #endif
  40450. }
  40451. #ifdef WOLFSSL_DEBUG_MEMORY
  40452. static void *my_Realloc_cb(void *ptr, size_t size, const char* func, unsigned int line)
  40453. {
  40454. (void) func;
  40455. (void) line;
  40456. #else
  40457. static void *my_Realloc_cb(void *ptr, size_t size)
  40458. {
  40459. #endif
  40460. realloc_cnt++;
  40461. #ifndef WOLFSSL_NO_MALLOC
  40462. return realloc(ptr, size);
  40463. #else
  40464. WOLFSSL_MSG("No realloc available");
  40465. (void)ptr;
  40466. (void)size;
  40467. return NULL;
  40468. #endif
  40469. }
  40470. #endif /* !WOLFSSL_NO_MALLOC */
  40471. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t memcb_test(void)
  40472. {
  40473. wc_test_ret_t ret = 0;
  40474. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  40475. !defined(WOLFSSL_STATIC_MEMORY)
  40476. byte* b = NULL;
  40477. #endif
  40478. wolfSSL_Malloc_cb mc;
  40479. wolfSSL_Free_cb fc;
  40480. wolfSSL_Realloc_cb rc;
  40481. /* Save existing memory callbacks */
  40482. ret = wolfSSL_GetAllocators(&mc, &fc, &rc);
  40483. if (ret != 0)
  40484. return WC_TEST_RET_ENC_EC(ret);
  40485. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  40486. !defined(WOLFSSL_STATIC_MEMORY)
  40487. /* test realloc */
  40488. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40489. if (b == NULL) {
  40490. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_memcb);
  40491. }
  40492. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40493. b = NULL;
  40494. /* Use API. */
  40495. ret = wolfSSL_SetAllocators((wolfSSL_Malloc_cb)my_Malloc_cb,
  40496. (wolfSSL_Free_cb)my_Free_cb,
  40497. (wolfSSL_Realloc_cb)my_Realloc_cb);
  40498. if (ret != 0) {
  40499. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_memcb);
  40500. }
  40501. b = (byte*)XMALLOC(1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40502. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40503. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40504. #ifndef WOLFSSL_STATIC_MEMORY
  40505. #ifndef WOLFSSL_CHECK_MEM_ZERO
  40506. if (malloc_cnt != 1 || free_cnt != 1 || realloc_cnt != 1)
  40507. #else
  40508. /* Checking zeroized memory means realloc is a malloc and free. */
  40509. if (malloc_cnt != 2 || free_cnt != 2 || realloc_cnt != 0)
  40510. #endif
  40511. #else
  40512. if (malloc_cnt != 0 || free_cnt != 0 || realloc_cnt != 0)
  40513. #endif
  40514. ret = WC_TEST_RET_ENC_NC;
  40515. #endif /* !WOLFSSL_NO_MALLOC */
  40516. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  40517. !defined(WOLFSSL_STATIC_MEMORY)
  40518. exit_memcb:
  40519. /* reset malloc/free/realloc counts */
  40520. malloc_cnt = 0;
  40521. free_cnt = 0;
  40522. realloc_cnt = 0;
  40523. #endif
  40524. /* restore memory callbacks */
  40525. wolfSSL_SetAllocators(mc, fc, rc);
  40526. return ret;
  40527. }
  40528. #endif /* USE_WOLFSSL_MEMORY && !WOLFSSL_NO_MALLOC */
  40529. #if defined(WOLFSSL_CAAM_BLOB)
  40530. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t blob_test(void)
  40531. {
  40532. wc_test_ret_t ret = 0;
  40533. byte out[112];
  40534. byte blob[112];
  40535. word32 outSz;
  40536. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  40537. {
  40538. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  40539. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  40540. };
  40541. WOLFSSL_SMALL_STACK_STATIC const byte text[] =
  40542. {
  40543. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  40544. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  40545. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  40546. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  40547. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  40548. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  40549. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  40550. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  40551. };
  40552. XMEMSET(blob, 0, sizeof(blob));
  40553. XMEMSET(out, 0, sizeof(out));
  40554. outSz = sizeof(blob);
  40555. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  40556. if (ret != 0)
  40557. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  40558. blob[outSz - 2] += 1;
  40559. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  40560. if (ret == 0) { /* should fail with altered blob */
  40561. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_blob);
  40562. }
  40563. XMEMSET(blob, 0, sizeof(blob));
  40564. outSz = sizeof(blob);
  40565. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  40566. if (ret != 0)
  40567. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  40568. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  40569. if (ret != 0)
  40570. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  40571. if (XMEMCMP(out, iv, sizeof(iv))) {
  40572. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_blob);
  40573. }
  40574. XMEMSET(blob, 0, sizeof(blob));
  40575. outSz = sizeof(blob);
  40576. ret = wc_caamCreateBlob((byte*)text, sizeof(text), blob, &outSz);
  40577. if (ret != 0)
  40578. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  40579. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  40580. if (ret != 0)
  40581. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_blob);
  40582. if (XMEMCMP(out, text, sizeof(text))) {
  40583. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_blob);
  40584. }
  40585. exit_blob:
  40586. return ret;
  40587. }
  40588. #endif /* WOLFSSL_CAAM_BLOB */
  40589. #ifdef WOLF_CRYPTO_CB
  40590. /* Example custom context for crypto callback */
  40591. typedef struct {
  40592. int exampleVar; /* flag for testing if only crypt is enabled. */
  40593. } myCryptoDevCtx;
  40594. #ifdef WOLF_CRYPTO_CB_ONLY_RSA
  40595. /* Testing rsa cb when CB_ONLY_RSA is enabled
  40596. * When CB_ONLY_RSA is enabled, software imple. is not available.
  40597. *
  40598. * ctx callback ctx
  40599. * returen 0 on success, otherwise return negative
  40600. */
  40601. static wc_test_ret_t rsa_onlycb_test(myCryptoDevCtx *ctx)
  40602. {
  40603. wc_test_ret_t ret = 0;
  40604. #if !defined(NO_RSA)
  40605. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  40606. RsaKey *key = (RsaKey *)XMALLOC(sizeof *key,
  40607. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40608. byte* tmp = NULL;
  40609. #else
  40610. RsaKey key[1];
  40611. byte tmp[FOURK_BUF];
  40612. #endif
  40613. size_t bytes;
  40614. const word32 inLen = (word32)TEST_STRING_SZ;
  40615. word32 idx = 0;
  40616. word32 sigSz;
  40617. WOLFSSL_SMALL_STACK_STATIC const byte in[] = TEST_STRING;
  40618. byte out[RSA_TEST_BYTES];
  40619. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  40620. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  40621. !defined(NO_FILESYSTEM)
  40622. XFILE file;
  40623. #endif
  40624. #ifdef WOLFSSL_KEY_GEN
  40625. WC_RNG rng;
  40626. #endif
  40627. #ifdef USE_CERT_BUFFERS_1024
  40628. bytes = (size_t)sizeof_client_key_der_1024;
  40629. if (bytes < (size_t)sizeof_client_cert_der_1024)
  40630. bytes = (size_t)sizeof_client_cert_der_1024;
  40631. #elif defined(USE_CERT_BUFFERS_2048)
  40632. bytes = (size_t)sizeof_client_key_der_2048;
  40633. if (bytes < (size_t)sizeof_client_cert_der_2048)
  40634. bytes = (size_t)sizeof_client_cert_der_2048;
  40635. #elif defined(USE_CERT_BUFFERS_3072)
  40636. bytes = (size_t)sizeof_client_key_der_3072;
  40637. if (bytes < (size_t)sizeof_client_cert_der_3072)
  40638. bytes = (size_t)sizeof_client_cert_der_3072;
  40639. #elif defined(USE_CERT_BUFFERS_4096)
  40640. bytes = (size_t)sizeof_client_key_der_4096;
  40641. if (bytes < (size_t)sizeof_client_cert_der_4096)
  40642. bytes = (size_t)sizeof_client_cert_der_4096;
  40643. #else
  40644. bytes = FOURK_BUF;
  40645. #endif
  40646. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  40647. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40648. if (tmp == NULL)
  40649. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_onlycb);
  40650. #endif
  40651. #ifdef USE_CERT_BUFFERS_1024
  40652. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  40653. #elif defined(USE_CERT_BUFFERS_2048)
  40654. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  40655. #elif defined(USE_CERT_BUFFERS_3072)
  40656. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  40657. #elif defined(USE_CERT_BUFFERS_4096)
  40658. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  40659. #elif !defined(NO_FILESYSTEM)
  40660. file = XFOPEN(clientKey, "rb");
  40661. if (!file) {
  40662. ret = WC_TEST_RET_ENC_ERRNO;
  40663. err_sys("can't open ./certs/client-key.der, "
  40664. "Please run from wolfSSL home dir", ret);
  40665. ERROR_OUT(ret, exit_onlycb);
  40666. }
  40667. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  40668. XFCLOSE(file);
  40669. if (bytes == 0)
  40670. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_onlycb);
  40671. #endif
  40672. #ifdef WOLFSSL_KEY_GEN
  40673. /* wc_CryptoCb_MakeRsaKey cb test, no actual making key
  40674. * wc_MakeRsaKey() -> rsa cb ->
  40675. * myCryptoDevCb -> wc_MakeRsaKey(CBONLY_TEST_DEVID)
  40676. * wc_MakeRsaKey(CBONLY_TEST_DEVID) expects to return 0(success)
  40677. */
  40678. ctx->exampleVar = 99;
  40679. ret = wc_MakeRsaKey(key, keySz, WC_RSA_EXPONENT, rng);
  40680. if (ret != 0)
  40681. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40682. /* wc_MakeRsaKey() -> rsa cb ->
  40683. * myCryptoDevCb -> wc_MakeRsaKey(INVALID_DEVID)
  40684. * wc_MakeRsaKey(CBONLY_TEST_DEVID) expects to return NO_VALID_DEVID(failure)
  40685. */
  40686. ctx->exampleVar = 1;
  40687. ret = wc_MakeRsaKey(key, keySz, WC_RSA_EXPONENT, rng);
  40688. if (ret != NO_VALID_DEVID) {
  40689. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40690. } else
  40691. /* reset return code */
  40692. ret = 0;
  40693. #endif
  40694. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  40695. if (ret != 0)
  40696. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40697. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  40698. if (ret != 0)
  40699. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40700. sigSz = (word32)wc_RsaEncryptSize(key);
  40701. /* wc_CryptoCb_Rsa cb test, no actual rsa operation */
  40702. if (ret == 0) {
  40703. /* wc_SignatureGenerate() -> rsa cb ->
  40704. * myCryptoDevCb -> wc_RsaFunction(CBONLY_TEST_DEVID)
  40705. * wc_RsaFunction(CBONLY_TEST_DEVID) expects to return 0(success)
  40706. */
  40707. ctx->exampleVar = 99;
  40708. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  40709. in, inLen, out, &sigSz, key, sizeof(*key), NULL);
  40710. if (ret != 0)
  40711. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40712. }
  40713. if (ret == 0) {
  40714. /* wc_SignatureGenerate() -> rsa cb ->
  40715. * myCryptoDevCb -> wc_RsaFunction(INVALID_DEVID)
  40716. * wc_SignatureGenerate(INVALID_DEVID) expects to
  40717. * return NO_VALID_DEVID(failure)
  40718. */
  40719. ctx->exampleVar = 1;
  40720. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  40721. in, inLen, out, &sigSz, key, sizeof(*key), NULL);
  40722. if (ret != NO_VALID_DEVID) {
  40723. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40724. } else
  40725. /* reset return code */
  40726. ret = 0;
  40727. }
  40728. exit_onlycb:
  40729. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  40730. if (key != NULL) {
  40731. wc_FreeRsaKey(key);
  40732. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40733. }
  40734. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40735. #else
  40736. wc_FreeRsaKey(key);
  40737. #endif
  40738. #endif
  40739. return ret;
  40740. }
  40741. #endif
  40742. #ifdef WOLF_CRYPTO_CB_ONLY_ECC
  40743. /* Testing rsa cb when CB_ONLY_ECC is enabled
  40744. * When CB_ONLY_ECC is enabled, software imple. is not available.
  40745. *
  40746. * ctx callback ctx
  40747. * returen 0 on success, otherwise return negative
  40748. */
  40749. static wc_test_ret_t ecc_onlycb_test(myCryptoDevCtx *ctx)
  40750. {
  40751. wc_test_ret_t ret = 0;
  40752. #if defined(HAVE_ECC)
  40753. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  40754. ecc_key* key = (ecc_key *)XMALLOC(sizeof *key,
  40755. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40756. ecc_key* pub = (ecc_key *)XMALLOC(sizeof *pub,
  40757. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40758. byte* out = (byte*)XMALLOC(sizeof(byte),
  40759. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40760. #ifdef OPENSSL_EXTRA
  40761. byte* check = (byte*)XMALLOC(sizeof(byte)*(256), HEAP_HINT,
  40762. DYNAMIC_TYPE_TMP_BUFFER);
  40763. #endif
  40764. #else
  40765. ecc_key key[1];
  40766. ecc_key pub[1];
  40767. byte out[256];
  40768. #ifdef OPENSSL_EXTRA
  40769. unsigned char check[256];
  40770. #endif
  40771. #endif
  40772. #ifdef OPENSSL_EXTRA
  40773. EVP_PKEY* privKey = NULL;
  40774. EVP_PKEY* pubKey = NULL;
  40775. #ifdef USE_CERT_BUFFERS_256
  40776. ecc_key* pkey;
  40777. const unsigned char* cp;
  40778. #endif
  40779. EVP_MD_CTX mdCtx;
  40780. const char testData[] = "Hi There";
  40781. size_t checkSz = -1;
  40782. const unsigned char* p;
  40783. const unsigned char check_v[256] = {
  40784. 0x30,0x45,0x02,0x20,0x1b,0x5c,0x2a,0xf0,0x18,0x09,
  40785. 0x74,0x65,0xa1,0x04,0x76,0x3a,0xce,0xcc,0xe5,0x34,
  40786. 0x5e,0x89,0xed,0x40,0x1e,0x5a,0xb1,0x53,0xb4,0xff,
  40787. 0xc7,0x18,0xfe,0x0f,0xc7,0xa6,0x02,0x21,0x00,0xe5,
  40788. 0x70,0x21,0xfc,0xf9,0x63,0x36,0xfd,0x16,0x18,0x08,
  40789. 0x9a,0x63,0x61,0x0f,0xe7,0x7c,0xa3,0xc9,0x14,0xa3,
  40790. 0x30,0x87,0xf7,0xf5,0x70,0x19,0xaf,0x56,0x96,0x9b,
  40791. 0xd8,0x64,0xcd,0xd9,0xff,0x7b,0x2a,0x55,0x52,0xca,
  40792. 0x41,0xb2,0xa6,0xa4,0x8a,0x3b,0x02,0x20,0x8c,0xc5,
  40793. 0xf9,0xc1,0x7d,0x2a,0x65,0x6c,0xe6,0x5a,0xe3,0x76,
  40794. 0x9b,0xab,0x0b,0x9f,0xaf,0x62,0x5d,0xb2,0x60,0xd7,
  40795. 0xeb,0xb4,0x1b,0x73,0xdc,0x01,0x7d,0x7b,0xab,0xc1,
  40796. 0x0c,0x74,0x96,0x41,0xe6,0x3f,0xc5,0x86,0xe6,0x7d,
  40797. 0x2b,0x9d,0x54,0x6b,0xcd,0x31,0x35,0x1f,0xdb,0x49,
  40798. 0x1f,0x32,0x34,0xf8,0x57,0x12,0x86,0x5c,0x0e,0x80,
  40799. 0x55,0x8d,0xff,0xd8,0xbd,0xdf,0x32,0x26,0x62,0x42,
  40800. 0x09,0xda,0xf7,0x74,0xf2,0x3f,0xe6,0xf1,0x77,0x82,
  40801. 0xce,0xe4,0xbb,0x61,0xa6,0xc0,0x17,0x0c,0x6c,0x47,
  40802. 0x2a,0x40,0x1c,0x2b,0xe0,0x98,0x3b,0xbf,0xc6,0xf8,
  40803. 0x6d,0xfd,0xd0,0xfa,0xc1,0x02,0xfb,0x5f,0xfb,0xb0,
  40804. 0xcb,0xd9,0xa3,0x59,0x94,0xe9,0x0f,0x74,0xbb,0x3f,
  40805. 0x64,0xa3,0x83,0xc4,0x2b,0xf7,0xd2,0x97,0xbf,0x3b,
  40806. 0xcf,0xbb,0x60,0x81,0x33,0x94,0xfa,0x0d,0x35,0xd2,
  40807. 0x3d,0xb9,0x99,0xe3,0x12,0xf8,0xf4,0xa3,0x74,0xf4,
  40808. 0x94,0x1d,0x7a,0x66,0xf8,0xd1,0x1d,0xcf,0xb0,0x48,
  40809. 0xef,0x8c,0x94,0x6f,0xdd,0x62,
  40810. };
  40811. #endif
  40812. WC_RNG rng;
  40813. EncryptedInfo encInfo;
  40814. int keyFormat = 0;
  40815. word32 keyIdx = 0;
  40816. byte in[] = "Everyone gets Friday off. ecc p";
  40817. word32 inLen = (word32)XSTRLEN((char*)in);
  40818. word32 outLen;
  40819. int verify;
  40820. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  40821. if (key == NULL || pub == NULL) {
  40822. ERROR_OUT(WC_TEST_RET_ENC_ERRNO, exit_onlycb);
  40823. }
  40824. #endif
  40825. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  40826. if (ret != 0)
  40827. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40828. /* wc_CryptoCb_MakeEccKey cb test, , no actual testing */
  40829. ctx->exampleVar = 99;
  40830. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, key);
  40831. if (ret != 0)
  40832. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40833. ctx->exampleVar = 1;
  40834. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, key);
  40835. if (ret != NO_VALID_DEVID) {
  40836. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40837. } else
  40838. /* reset return code */
  40839. ret = 0;
  40840. #ifdef USE_CERT_BUFFERS_256
  40841. if (ret == 0) {
  40842. /* load ECC private key and perform private transform */
  40843. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &keyIdx,
  40844. key, sizeof_ecc_key_der_256);
  40845. }
  40846. if (ret != 0)
  40847. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40848. /* wc_CryptoCb_EccSign cb test, no actual testing */
  40849. ctx->exampleVar = 99;
  40850. if (ret == 0) {
  40851. ret = wc_ecc_sign_hash(in, inLen, out, &outLen, &rng, key);
  40852. }
  40853. if (ret != 0)
  40854. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40855. ctx->exampleVar = 1;
  40856. if (ret == 0) {
  40857. ret = wc_ecc_sign_hash(in, inLen, out, &outLen, &rng, key);
  40858. }
  40859. if (ret != NO_VALID_DEVID) {
  40860. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40861. }
  40862. else
  40863. ret = 0;
  40864. /* wc_CryptoCb_EccVerify cb test, no actual testing */
  40865. ctx->exampleVar = 99;
  40866. if (ret == 0) {
  40867. ret = wc_ecc_verify_hash(in, inLen, out, outLen, &verify, key);
  40868. }
  40869. if (ret != 0)
  40870. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40871. ctx->exampleVar = 1;
  40872. if (ret == 0) {
  40873. ret = wc_ecc_verify_hash(in, inLen, out, outLen, &verify, key);
  40874. }
  40875. if (ret != NO_VALID_DEVID) {
  40876. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40877. }
  40878. else
  40879. ret = 0;
  40880. /* wc_CryptoCb_Ecdh cb test, no actual testing */
  40881. /* make public key for shared secret */
  40882. wc_ecc_init_ex(pub, HEAP_HINT, devId);
  40883. ctx->exampleVar = 99;
  40884. if (ret == 0) {
  40885. ret = wc_ecc_shared_secret(key, pub, out, &outLen);
  40886. }
  40887. if (ret != 0)
  40888. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40889. ctx->exampleVar = 1;
  40890. if (ret == 0) {
  40891. ret = wc_ecc_shared_secret(key, pub, out, &outLen);
  40892. }
  40893. if (ret != NO_VALID_DEVID) {
  40894. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40895. }
  40896. else
  40897. ret = 0;
  40898. #ifdef OPENSSL_EXTRA
  40899. (void)pkey;
  40900. cp = ecc_clikey_der_256;
  40901. privKey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  40902. sizeof_ecc_clikey_der_256);
  40903. if (privKey == NULL) {
  40904. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  40905. }
  40906. pkey = (ecc_key*)privKey->ecc->internal;
  40907. pkey->devId = devId;
  40908. p = ecc_clikeypub_der_256;
  40909. pubKey = d2i_PUBKEY(NULL, &p, sizeof_ecc_clikeypub_der_256);
  40910. if (pubKey == NULL) {
  40911. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  40912. }
  40913. pkey = (ecc_key*)pubKey->ecc->internal;
  40914. pkey->devId = devId;
  40915. /* sign */
  40916. EVP_MD_CTX_init(&mdCtx);
  40917. ret = EVP_DigestSignInit(&mdCtx, NULL, EVP_sha256(), NULL, privKey);
  40918. if (ret != WOLFSSL_SUCCESS) {
  40919. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  40920. }
  40921. ret = EVP_DigestSignUpdate(&mdCtx, testData,
  40922. (unsigned int)XSTRLEN(testData));
  40923. if (ret != WOLFSSL_SUCCESS) {
  40924. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  40925. }
  40926. ret = EVP_DigestSignFinal(&mdCtx, NULL, &checkSz);
  40927. if (ret != WOLFSSL_SUCCESS) {
  40928. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  40929. }
  40930. ctx->exampleVar = 99;
  40931. ret = EVP_DigestSignFinal(&mdCtx, check, &checkSz);
  40932. /* just called crypt callback as dummy
  40933. * EVP_DigestSignFinal returns 0 internally.
  40934. */
  40935. if (ret != 0)
  40936. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40937. ctx->exampleVar = 1;
  40938. ret = EVP_DigestSignFinal(&mdCtx, check, &checkSz);
  40939. /* just called crypt callback as dummy
  40940. * EVP_DigestSignFinal returns 0 internally.
  40941. */
  40942. if (ret != 0)
  40943. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40944. /* restore checkSz for verify */
  40945. checkSz = 71;
  40946. ret = EVP_MD_CTX_cleanup(&mdCtx);
  40947. if (ret != SSL_SUCCESS) {
  40948. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  40949. }
  40950. /* verify */
  40951. EVP_MD_CTX_init(&mdCtx);
  40952. if (ret == SSL_SUCCESS) {
  40953. ret = EVP_DigestVerifyInit(&mdCtx, NULL, EVP_sha256(), NULL, pubKey);
  40954. }
  40955. if (ret != WOLFSSL_SUCCESS) {
  40956. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  40957. }
  40958. if (ret == WOLFSSL_SUCCESS) {
  40959. ret = EVP_DigestVerifyUpdate(&mdCtx, testData,
  40960. (unsigned int)XSTRLEN(testData));
  40961. }
  40962. if (ret != WOLFSSL_SUCCESS) {
  40963. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  40964. }
  40965. ctx->exampleVar = 99;
  40966. ret = EVP_DigestVerifyFinal(&mdCtx, check_v, checkSz);
  40967. /* just called crypt callback as dummy
  40968. * EVP_DigestSignFinal returns 0 internally.
  40969. */
  40970. if (ret != 0)
  40971. ERROR_OUT(WC_TEST_RET_ENC_EC(ret), exit_onlycb);
  40972. ctx->exampleVar = 1;
  40973. ret = EVP_DigestVerifyFinal(&mdCtx, check_v, checkSz);
  40974. /* just called crypt callback as dummy
  40975. * EVP_DigestVerifyFinal returns -1 internally rather than NO_VALID_DEVID.
  40976. */
  40977. if (ret != -1) {
  40978. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  40979. }
  40980. ret = EVP_MD_CTX_cleanup(&mdCtx);
  40981. if (ret != SSL_SUCCESS) {
  40982. ERROR_OUT(WC_TEST_RET_ENC_NC, exit_onlycb);
  40983. } else
  40984. ret = 0;
  40985. #endif
  40986. #else
  40987. (void)verify;
  40988. (void)outLen;
  40989. (void)inLen;
  40990. (void)out;
  40991. (void)pub;
  40992. #ifdef OPENSSL_EXTRA
  40993. (void)privKey;
  40994. (void)pubKey;
  40995. (void)mdCtx;
  40996. (void)check;
  40997. (void)checkSz;
  40998. (void)p;
  40999. #endif
  41000. #endif
  41001. (void)keyFormat;
  41002. (void)encInfo;
  41003. exit_onlycb:
  41004. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  41005. if (key != NULL) {
  41006. wc_ecc_free(key);
  41007. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41008. }
  41009. if (pub != NULL) {
  41010. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41011. }
  41012. if (out != NULL) {
  41013. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41014. }
  41015. #ifdef OPENSSL_EXTRA
  41016. if (check) {
  41017. FREE(check, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41018. }
  41019. #endif
  41020. #else
  41021. wc_ecc_free(key);
  41022. #ifdef OPENSSL_EXTRA
  41023. if (privKey)
  41024. EVP_PKEY_free(privKey);
  41025. if (pubKey)
  41026. EVP_PKEY_free(pubKey);
  41027. #endif
  41028. #endif
  41029. #endif /* HAVE_ECC */
  41030. return ret;
  41031. }
  41032. #endif
  41033. /* Example crypto dev callback function that calls software version */
  41034. static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
  41035. {
  41036. int ret = NOT_COMPILED_IN; /* return this to bypass HW and use SW */
  41037. myCryptoDevCtx* myCtx = (myCryptoDevCtx*)ctx;
  41038. if (info == NULL)
  41039. return BAD_FUNC_ARG;
  41040. #ifdef DEBUG_WOLFSSL
  41041. printf("CryptoDevCb: Algo Type %d\n", info->algo_type);
  41042. #endif
  41043. if (info->algo_type == WC_ALGO_TYPE_RNG) {
  41044. #if defined(WOLF_CRYPTO_CB) && !defined(HAVE_HASHDRBG) && \
  41045. !defined(WC_NO_RNG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  41046. /* if RNG only supports crypto callback, just use seed */
  41047. ret = wc_GenerateSeed(&info->rng.rng->seed,
  41048. info->rng.out, info->rng.sz);
  41049. #elif !defined(WC_NO_RNG)
  41050. /* set devId to invalid, so software is used */
  41051. info->rng.rng->devId = INVALID_DEVID;
  41052. ret = wc_RNG_GenerateBlock(info->rng.rng,
  41053. info->rng.out, info->rng.sz);
  41054. /* reset devId */
  41055. info->rng.rng->devId = devIdArg;
  41056. #endif
  41057. }
  41058. else if (info->algo_type == WC_ALGO_TYPE_SEED) {
  41059. #ifndef WC_NO_RNG
  41060. ALIGN32 static byte seed[sizeof(word32)] = { 0x00, 0x00, 0x00, 0x01 };
  41061. word32* seedWord32 = (word32*)seed;
  41062. word32 len;
  41063. /* wc_GenerateSeed is a local symbol so we need to fake the entropy. */
  41064. while (info->seed.sz > 0) {
  41065. len = (word32)sizeof(seed);
  41066. if (info->seed.sz < len)
  41067. len = info->seed.sz;
  41068. XMEMCPY(info->seed.seed, seed, sizeof(seed));
  41069. info->seed.seed += len;
  41070. info->seed.sz -= len;
  41071. (*seedWord32)++;
  41072. }
  41073. ret = 0;
  41074. #endif
  41075. }
  41076. else if (info->algo_type == WC_ALGO_TYPE_PK) {
  41077. #ifdef DEBUG_WOLFSSL
  41078. printf("CryptoDevCb: Pk Type %d\n", info->pk.type);
  41079. #endif
  41080. #ifndef NO_RSA
  41081. if (info->pk.type == WC_PK_TYPE_RSA) {
  41082. /* set devId to invalid, so software is used */
  41083. info->pk.rsa.key->devId = INVALID_DEVID;
  41084. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  41085. #ifdef DEBUG_WOLFSSL
  41086. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  41087. #endif
  41088. if (myCtx->exampleVar == 99) {
  41089. info->pk.rsa.key->devId = devIdArg;
  41090. return 0;
  41091. }
  41092. #endif
  41093. switch (info->pk.rsa.type) {
  41094. case RSA_PUBLIC_ENCRYPT:
  41095. case RSA_PUBLIC_DECRYPT:
  41096. /* perform software based RSA public op */
  41097. ret = wc_RsaFunction(
  41098. info->pk.rsa.in, info->pk.rsa.inLen,
  41099. info->pk.rsa.out, info->pk.rsa.outLen,
  41100. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  41101. break;
  41102. case RSA_PRIVATE_ENCRYPT:
  41103. case RSA_PRIVATE_DECRYPT:
  41104. /* perform software based RSA private op */
  41105. ret = wc_RsaFunction(
  41106. info->pk.rsa.in, info->pk.rsa.inLen,
  41107. info->pk.rsa.out, info->pk.rsa.outLen,
  41108. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  41109. break;
  41110. }
  41111. /* reset devId */
  41112. info->pk.rsa.key->devId = devIdArg;
  41113. }
  41114. #ifdef WOLFSSL_KEY_GEN
  41115. else if (info->pk.type == WC_PK_TYPE_RSA_KEYGEN) {
  41116. info->pk.rsakg.key->devId = INVALID_DEVID;
  41117. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  41118. #ifdef DEBUG_WOLFSSL
  41119. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  41120. #endif
  41121. if (myCtx->exampleVar == 99) {
  41122. info->pk.rsakg.key->devId = devIdArg;
  41123. return 0;
  41124. }
  41125. #endif
  41126. #ifdef HAVE_FIPS
  41127. for (;;) {
  41128. #endif
  41129. ret = wc_MakeRsaKey(info->pk.rsakg.key, info->pk.rsakg.size,
  41130. info->pk.rsakg.e, info->pk.rsakg.rng);
  41131. #ifdef HAVE_FIPS
  41132. if (ret == PRIME_GEN_E)
  41133. continue;
  41134. break;
  41135. }
  41136. #endif
  41137. /* reset devId */
  41138. info->pk.rsakg.key->devId = devIdArg;
  41139. }
  41140. #endif
  41141. #endif /* !NO_RSA */
  41142. #ifdef HAVE_ECC
  41143. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  41144. /* set devId to invalid, so software is used */
  41145. info->pk.eckg.key->devId = INVALID_DEVID;
  41146. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  41147. #ifdef DEBUG_WOLFSSL
  41148. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  41149. #endif
  41150. if (myCtx->exampleVar == 99) {
  41151. info->pk.eckg.key->devId = devIdArg;
  41152. return 0;
  41153. }
  41154. #endif
  41155. ret = wc_ecc_make_key_ex(info->pk.eckg.rng, info->pk.eckg.size,
  41156. info->pk.eckg.key, info->pk.eckg.curveId);
  41157. /* reset devId */
  41158. info->pk.eckg.key->devId = devIdArg;
  41159. }
  41160. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  41161. /* set devId to invalid, so software is used */
  41162. info->pk.eccsign.key->devId = INVALID_DEVID;
  41163. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  41164. #ifdef DEBUG_WOLFSSL
  41165. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  41166. #endif
  41167. if (myCtx->exampleVar == 99) {
  41168. info->pk.eccsign.key->devId = devIdArg;
  41169. return 0;
  41170. }
  41171. #endif
  41172. ret = wc_ecc_sign_hash(
  41173. info->pk.eccsign.in, info->pk.eccsign.inlen,
  41174. info->pk.eccsign.out, info->pk.eccsign.outlen,
  41175. info->pk.eccsign.rng, info->pk.eccsign.key);
  41176. /* reset devId */
  41177. info->pk.eccsign.key->devId = devIdArg;
  41178. }
  41179. else if (info->pk.type == WC_PK_TYPE_ECDSA_VERIFY) {
  41180. /* set devId to invalid, so software is used */
  41181. info->pk.eccverify.key->devId = INVALID_DEVID;
  41182. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  41183. #ifdef DEBUG_WOLFSSL
  41184. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  41185. #endif
  41186. if (myCtx->exampleVar == 99) {
  41187. info->pk.eccverify.key->devId = devIdArg;
  41188. return 0;
  41189. }
  41190. #endif
  41191. ret = wc_ecc_verify_hash(
  41192. info->pk.eccverify.sig, info->pk.eccverify.siglen,
  41193. info->pk.eccverify.hash, info->pk.eccverify.hashlen,
  41194. info->pk.eccverify.res, info->pk.eccverify.key);
  41195. /* reset devId */
  41196. info->pk.eccverify.key->devId = devIdArg;
  41197. }
  41198. else if (info->pk.type == WC_PK_TYPE_ECDH) {
  41199. /* set devId to invalid, so software is used */
  41200. info->pk.ecdh.private_key->devId = INVALID_DEVID;
  41201. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  41202. #ifdef DEBUG_WOLFSSL
  41203. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  41204. #endif
  41205. if (myCtx->exampleVar == 99) {
  41206. info->pk.ecdh.private_key->devId = devIdArg;
  41207. return 0;
  41208. }
  41209. #endif
  41210. ret = wc_ecc_shared_secret(
  41211. info->pk.ecdh.private_key, info->pk.ecdh.public_key,
  41212. info->pk.ecdh.out, info->pk.ecdh.outlen);
  41213. /* reset devId */
  41214. info->pk.ecdh.private_key->devId = devIdArg;
  41215. }
  41216. #endif /* HAVE_ECC */
  41217. #ifdef HAVE_CURVE25519
  41218. if (info->pk.type == WC_PK_TYPE_CURVE25519_KEYGEN) {
  41219. /* set devId to invalid, so software is used */
  41220. info->pk.curve25519kg.key->devId = INVALID_DEVID;
  41221. ret = wc_curve25519_make_key(info->pk.curve25519kg.rng,
  41222. info->pk.curve25519kg.size, info->pk.curve25519kg.key);
  41223. /* reset devId */
  41224. info->pk.curve25519kg.key->devId = devIdArg;
  41225. }
  41226. else if (info->pk.type == WC_PK_TYPE_CURVE25519) {
  41227. /* set devId to invalid, so software is used */
  41228. info->pk.curve25519.private_key->devId = INVALID_DEVID;
  41229. ret = wc_curve25519_shared_secret_ex(
  41230. info->pk.curve25519.private_key, info->pk.curve25519.public_key,
  41231. info->pk.curve25519.out, info->pk.curve25519.outlen,
  41232. info->pk.curve25519.endian);
  41233. /* reset devId */
  41234. info->pk.curve25519.private_key->devId = devIdArg;
  41235. }
  41236. #endif /* HAVE_CURVE25519 */
  41237. #ifdef HAVE_ED25519
  41238. if (info->pk.type == WC_PK_TYPE_ED25519_KEYGEN) {
  41239. /* set devId to invalid, so software is used */
  41240. info->pk.ed25519kg.key->devId = INVALID_DEVID;
  41241. ret = wc_ed25519_make_key(info->pk.ed25519kg.rng,
  41242. info->pk.ed25519kg.size, info->pk.ed25519kg.key);
  41243. /* reset devId */
  41244. info->pk.ed25519kg.key->devId = devIdArg;
  41245. }
  41246. #ifdef HAVE_ED25519_SIGN
  41247. else if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  41248. /* set devId to invalid, so software is used */
  41249. info->pk.ed25519sign.key->devId = INVALID_DEVID;
  41250. ret = wc_ed25519_sign_msg_ex(
  41251. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  41252. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  41253. info->pk.ed25519sign.key, info->pk.ed25519sign.type,
  41254. info->pk.ed25519sign.context, info->pk.ed25519sign.contextLen);
  41255. /* reset devId */
  41256. info->pk.ed25519sign.key->devId = devIdArg;
  41257. }
  41258. #endif
  41259. #ifdef HAVE_ED25519_VERIFY
  41260. else if (info->pk.type == WC_PK_TYPE_ED25519_VERIFY) {
  41261. /* set devId to invalid, so software is used */
  41262. info->pk.ed25519verify.key->devId = INVALID_DEVID;
  41263. ret = wc_ed25519_verify_msg_ex(
  41264. info->pk.ed25519verify.sig, info->pk.ed25519verify.sigLen,
  41265. info->pk.ed25519verify.msg, info->pk.ed25519verify.msgLen,
  41266. info->pk.ed25519verify.res, info->pk.ed25519verify.key,
  41267. info->pk.ed25519verify.type, info->pk.ed25519verify.context,
  41268. info->pk.ed25519verify.contextLen);
  41269. /* reset devId */
  41270. info->pk.ed25519verify.key->devId = devIdArg;
  41271. }
  41272. #endif
  41273. #endif /* HAVE_ED25519 */
  41274. }
  41275. else if (info->algo_type == WC_ALGO_TYPE_CIPHER) {
  41276. #if !defined(NO_AES) || !defined(NO_DES3)
  41277. #ifdef HAVE_AESGCM
  41278. if (info->cipher.type == WC_CIPHER_AES_GCM) {
  41279. if (info->cipher.enc) {
  41280. /* set devId to invalid, so software is used */
  41281. info->cipher.aesgcm_enc.aes->devId = INVALID_DEVID;
  41282. ret = wc_AesGcmEncrypt(
  41283. info->cipher.aesgcm_enc.aes,
  41284. info->cipher.aesgcm_enc.out,
  41285. info->cipher.aesgcm_enc.in,
  41286. info->cipher.aesgcm_enc.sz,
  41287. info->cipher.aesgcm_enc.iv,
  41288. info->cipher.aesgcm_enc.ivSz,
  41289. info->cipher.aesgcm_enc.authTag,
  41290. info->cipher.aesgcm_enc.authTagSz,
  41291. info->cipher.aesgcm_enc.authIn,
  41292. info->cipher.aesgcm_enc.authInSz);
  41293. /* reset devId */
  41294. info->cipher.aesgcm_enc.aes->devId = devIdArg;
  41295. }
  41296. else {
  41297. /* set devId to invalid, so software is used */
  41298. info->cipher.aesgcm_dec.aes->devId = INVALID_DEVID;
  41299. ret = wc_AesGcmDecrypt(
  41300. info->cipher.aesgcm_dec.aes,
  41301. info->cipher.aesgcm_dec.out,
  41302. info->cipher.aesgcm_dec.in,
  41303. info->cipher.aesgcm_dec.sz,
  41304. info->cipher.aesgcm_dec.iv,
  41305. info->cipher.aesgcm_dec.ivSz,
  41306. info->cipher.aesgcm_dec.authTag,
  41307. info->cipher.aesgcm_dec.authTagSz,
  41308. info->cipher.aesgcm_dec.authIn,
  41309. info->cipher.aesgcm_dec.authInSz);
  41310. /* reset devId */
  41311. info->cipher.aesgcm_dec.aes->devId = devIdArg;
  41312. }
  41313. }
  41314. #endif /* HAVE_AESGCM */
  41315. #ifdef HAVE_AES_CBC
  41316. if (info->cipher.type == WC_CIPHER_AES_CBC) {
  41317. if (info->cipher.enc) {
  41318. /* set devId to invalid, so software is used */
  41319. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  41320. ret = wc_AesCbcEncrypt(
  41321. info->cipher.aescbc.aes,
  41322. info->cipher.aescbc.out,
  41323. info->cipher.aescbc.in,
  41324. info->cipher.aescbc.sz);
  41325. /* reset devId */
  41326. info->cipher.aescbc.aes->devId = devIdArg;
  41327. }
  41328. else {
  41329. /* set devId to invalid, so software is used */
  41330. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  41331. ret = wc_AesCbcDecrypt(
  41332. info->cipher.aescbc.aes,
  41333. info->cipher.aescbc.out,
  41334. info->cipher.aescbc.in,
  41335. info->cipher.aescbc.sz);
  41336. /* reset devId */
  41337. info->cipher.aescbc.aes->devId = devIdArg;
  41338. }
  41339. }
  41340. #endif /* HAVE_AES_CBC */
  41341. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  41342. if (info->cipher.type == WC_CIPHER_AES_ECB) {
  41343. if (info->cipher.enc) {
  41344. /* set devId to invalid, so software is used */
  41345. info->cipher.aesecb.aes->devId = INVALID_DEVID;
  41346. ret = wc_AesEcbEncrypt(
  41347. info->cipher.aesecb.aes,
  41348. info->cipher.aesecb.out,
  41349. info->cipher.aesecb.in,
  41350. info->cipher.aesecb.sz);
  41351. /* reset devId */
  41352. info->cipher.aesecb.aes->devId = devIdArg;
  41353. }
  41354. else {
  41355. /* set devId to invalid, so software is used */
  41356. info->cipher.aesecb.aes->devId = INVALID_DEVID;
  41357. ret = wc_AesEcbDecrypt(
  41358. info->cipher.aesecb.aes,
  41359. info->cipher.aesecb.out,
  41360. info->cipher.aesecb.in,
  41361. info->cipher.aesecb.sz);
  41362. /* reset devId */
  41363. info->cipher.aesecb.aes->devId = devIdArg;
  41364. }
  41365. }
  41366. #endif /* HAVE_AES_ECB */
  41367. #if defined(WOLFSSL_AES_COUNTER) && !defined(HAVE_FIPS) && \
  41368. !defined(HAVE_SELFTEST)
  41369. if (info->cipher.type == WC_CIPHER_AES_CTR) {
  41370. /* set devId to invalid, so software is used */
  41371. info->cipher.aesctr.aes->devId = INVALID_DEVID;
  41372. ret = wc_AesCtrEncrypt(
  41373. info->cipher.aesctr.aes,
  41374. info->cipher.aesctr.out,
  41375. info->cipher.aesctr.in,
  41376. info->cipher.aesctr.sz);
  41377. /* reset devId */
  41378. info->cipher.aesctr.aes->devId = devIdArg;
  41379. }
  41380. #endif /* WOLFSSL_AES_COUNTER */
  41381. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  41382. if (info->cipher.type == WC_CIPHER_AES_CCM) {
  41383. if (info->cipher.enc) {
  41384. /* set devId to invalid, so software is used */
  41385. info->cipher.aesccm_enc.aes->devId = INVALID_DEVID;
  41386. ret = wc_AesCcmEncrypt(
  41387. info->cipher.aesccm_enc.aes,
  41388. info->cipher.aesccm_enc.out,
  41389. info->cipher.aesccm_enc.in,
  41390. info->cipher.aesccm_enc.sz,
  41391. info->cipher.aesccm_enc.nonce,
  41392. info->cipher.aesccm_enc.nonceSz,
  41393. info->cipher.aesccm_enc.authTag,
  41394. info->cipher.aesccm_enc.authTagSz,
  41395. info->cipher.aesccm_enc.authIn,
  41396. info->cipher.aesccm_enc.authInSz);
  41397. /* reset devId */
  41398. info->cipher.aesccm_enc.aes->devId = devIdArg;
  41399. }
  41400. else {
  41401. /* set devId to invalid, so software is used */
  41402. info->cipher.aesccm_dec.aes->devId = INVALID_DEVID;
  41403. ret = wc_AesCcmDecrypt(
  41404. info->cipher.aesccm_dec.aes,
  41405. info->cipher.aesccm_dec.out,
  41406. info->cipher.aesccm_dec.in,
  41407. info->cipher.aesccm_dec.sz,
  41408. info->cipher.aesccm_dec.nonce,
  41409. info->cipher.aesccm_dec.nonceSz,
  41410. info->cipher.aesccm_dec.authTag,
  41411. info->cipher.aesccm_dec.authTagSz,
  41412. info->cipher.aesccm_dec.authIn,
  41413. info->cipher.aesccm_dec.authInSz);
  41414. /* reset devId */
  41415. info->cipher.aesccm_dec.aes->devId = devIdArg;
  41416. }
  41417. }
  41418. #endif
  41419. #ifndef NO_DES3
  41420. if (info->cipher.type == WC_CIPHER_DES3) {
  41421. if (info->cipher.enc) {
  41422. /* set devId to invalid, so software is used */
  41423. info->cipher.des3.des->devId = INVALID_DEVID;
  41424. ret = wc_Des3_CbcEncrypt(
  41425. info->cipher.des3.des,
  41426. info->cipher.des3.out,
  41427. info->cipher.des3.in,
  41428. info->cipher.des3.sz);
  41429. /* reset devId */
  41430. info->cipher.des3.des->devId = devIdArg;
  41431. }
  41432. else {
  41433. /* set devId to invalid, so software is used */
  41434. info->cipher.des3.des->devId = INVALID_DEVID;
  41435. ret = wc_Des3_CbcDecrypt(
  41436. info->cipher.des3.des,
  41437. info->cipher.des3.out,
  41438. info->cipher.des3.in,
  41439. info->cipher.des3.sz);
  41440. /* reset devId */
  41441. info->cipher.des3.des->devId = devIdArg;
  41442. }
  41443. }
  41444. #endif /* !NO_DES3 */
  41445. #endif /* !NO_AES || !NO_DES3 */
  41446. }
  41447. #if !defined(NO_SHA) || !defined(NO_SHA256) || \
  41448. defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)
  41449. else if (info->algo_type == WC_ALGO_TYPE_HASH) {
  41450. #if !defined(NO_SHA)
  41451. if (info->hash.type == WC_HASH_TYPE_SHA) {
  41452. if (info->hash.sha1 == NULL)
  41453. return NOT_COMPILED_IN;
  41454. /* set devId to invalid, so software is used */
  41455. info->hash.sha1->devId = INVALID_DEVID;
  41456. if (info->hash.in != NULL) {
  41457. ret = wc_ShaUpdate(
  41458. info->hash.sha1,
  41459. info->hash.in,
  41460. info->hash.inSz);
  41461. }
  41462. if (info->hash.digest != NULL) {
  41463. ret = wc_ShaFinal(
  41464. info->hash.sha1,
  41465. info->hash.digest);
  41466. }
  41467. /* reset devId */
  41468. info->hash.sha1->devId = devIdArg;
  41469. }
  41470. else
  41471. #endif
  41472. #if !defined(NO_SHA256)
  41473. if (info->hash.type == WC_HASH_TYPE_SHA256) {
  41474. if (info->hash.sha256 == NULL)
  41475. return NOT_COMPILED_IN;
  41476. /* set devId to invalid, so software is used */
  41477. info->hash.sha256->devId = INVALID_DEVID;
  41478. if (info->hash.in != NULL) {
  41479. ret = wc_Sha256Update(
  41480. info->hash.sha256,
  41481. info->hash.in,
  41482. info->hash.inSz);
  41483. }
  41484. if (info->hash.digest != NULL) {
  41485. ret = wc_Sha256Final(
  41486. info->hash.sha256,
  41487. info->hash.digest);
  41488. }
  41489. /* reset devId */
  41490. info->hash.sha256->devId = devIdArg;
  41491. }
  41492. else
  41493. #endif
  41494. #ifdef WOLFSSL_SHA384
  41495. if (info->hash.type == WC_HASH_TYPE_SHA384) {
  41496. if (info->hash.sha384 == NULL)
  41497. return NOT_COMPILED_IN;
  41498. #ifndef NO_SHA2_CRYPTO_CB
  41499. /* set devId to invalid, so software is used */
  41500. info->hash.sha384->devId = INVALID_DEVID;
  41501. #endif
  41502. if (info->hash.in != NULL) {
  41503. ret = wc_Sha384Update(
  41504. info->hash.sha384,
  41505. info->hash.in,
  41506. info->hash.inSz);
  41507. }
  41508. if (info->hash.digest != NULL) {
  41509. ret = wc_Sha384Final(
  41510. info->hash.sha384,
  41511. info->hash.digest);
  41512. }
  41513. #ifndef NO_SHA2_CRYPTO_CB
  41514. /* reset devId */
  41515. info->hash.sha384->devId = devIdArg;
  41516. #endif
  41517. }
  41518. else
  41519. #endif
  41520. #ifdef WOLFSSL_SHA512
  41521. if (info->hash.type == WC_HASH_TYPE_SHA512) {
  41522. if (info->hash.sha512 == NULL)
  41523. return NOT_COMPILED_IN;
  41524. #ifndef NO_SHA2_CRYPTO_CB
  41525. /* set devId to invalid, so software is used */
  41526. info->hash.sha512->devId = INVALID_DEVID;
  41527. #endif
  41528. if (info->hash.in != NULL) {
  41529. ret = wc_Sha512Update(
  41530. info->hash.sha512,
  41531. info->hash.in,
  41532. info->hash.inSz);
  41533. }
  41534. if (info->hash.digest != NULL) {
  41535. ret = wc_Sha512Final(
  41536. info->hash.sha512,
  41537. info->hash.digest);
  41538. }
  41539. #ifndef NO_SHA2_CRYPTO_CB
  41540. /* reset devId */
  41541. info->hash.sha512->devId = devIdArg;
  41542. #endif
  41543. }
  41544. else
  41545. #endif
  41546. {
  41547. }
  41548. }
  41549. #endif /* !NO_SHA || !NO_SHA256 */
  41550. #ifndef NO_HMAC
  41551. else if (info->algo_type == WC_ALGO_TYPE_HMAC) {
  41552. if (info->hmac.hmac == NULL)
  41553. return NOT_COMPILED_IN;
  41554. /* set devId to invalid, so software is used */
  41555. info->hmac.hmac->devId = INVALID_DEVID;
  41556. if (info->hash.in != NULL) {
  41557. ret = wc_HmacUpdate(
  41558. info->hmac.hmac,
  41559. info->hmac.in,
  41560. info->hmac.inSz);
  41561. }
  41562. else if (info->hash.digest != NULL) {
  41563. ret = wc_HmacFinal(
  41564. info->hmac.hmac,
  41565. info->hmac.digest);
  41566. }
  41567. /* reset devId */
  41568. info->hmac.hmac->devId = devIdArg;
  41569. }
  41570. #endif
  41571. (void)devIdArg;
  41572. (void)myCtx;
  41573. return ret;
  41574. }
  41575. #ifdef WOLF_CRYPTO_CB_FIND
  41576. static int myCryptoCbFind(int currentId, int algoType)
  41577. {
  41578. /* can have algo specific overrides here
  41579. switch (algoType) {
  41580. i.e.
  41581. WC_ALGO_TYPE_CMAC
  41582. WC_ALGO_TYPE_SEED
  41583. WC_ALGO_TYPE_HMAC
  41584. WC_ALGO_TYPE_HASH
  41585. WC_ALGO_TYPE_CIPHER
  41586. WC_ALGO_TYPE_PK
  41587. }
  41588. */
  41589. (void)algoType;
  41590. if (currentId == INVALID_DEVID) {
  41591. /* can override invalid devid found with 1 */
  41592. }
  41593. return currentId;
  41594. }
  41595. #endif /* WOLF_CRYPTO_CB_FIND */
  41596. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t cryptocb_test(void)
  41597. {
  41598. wc_test_ret_t ret = 0;
  41599. int origDevId = devId;
  41600. myCryptoDevCtx myCtx;
  41601. /* example data for callback */
  41602. myCtx.exampleVar = 1;
  41603. /* set devId to something other than INVALID_DEVID */
  41604. devId = 1;
  41605. ret = wc_CryptoCb_RegisterDevice(devId, myCryptoDevCb, &myCtx);
  41606. #ifdef WOLF_CRYPTO_CB_FIND
  41607. wc_CryptoCb_SetDeviceFindCb(myCryptoCbFind);
  41608. #endif /* WOLF_CRYPTO_CB_FIND */
  41609. #ifndef WC_NO_RNG
  41610. if (ret == 0)
  41611. ret = random_test();
  41612. #endif /* WC_NO_RNG */
  41613. #if !defined(NO_RSA)
  41614. PRIVATE_KEY_UNLOCK();
  41615. if (ret == 0)
  41616. ret = rsa_test();
  41617. PRIVATE_KEY_LOCK();
  41618. #endif
  41619. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  41620. PRIVATE_KEY_UNLOCK();
  41621. if (ret == 0)
  41622. ret = rsa_onlycb_test(&myCtx);
  41623. PRIVATE_KEY_LOCK();
  41624. #endif
  41625. #if defined(HAVE_ECC)
  41626. PRIVATE_KEY_UNLOCK();
  41627. if (ret == 0)
  41628. ret = ecc_test();
  41629. PRIVATE_KEY_LOCK();
  41630. #endif
  41631. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  41632. PRIVATE_KEY_UNLOCK();
  41633. if (ret == 0)
  41634. ret = ecc_onlycb_test(&myCtx);
  41635. PRIVATE_KEY_LOCK();
  41636. #endif
  41637. #ifdef HAVE_ED25519
  41638. if (ret == 0)
  41639. ret = ed25519_test();
  41640. #endif
  41641. #ifdef HAVE_CURVE25519
  41642. if (ret == 0)
  41643. ret = curve25519_test();
  41644. #endif
  41645. #ifndef NO_AES
  41646. #ifdef HAVE_AESGCM
  41647. if (ret == 0)
  41648. ret = aesgcm_test();
  41649. #endif
  41650. #ifdef HAVE_AES_CBC
  41651. if (ret == 0)
  41652. ret = aes_test();
  41653. #endif
  41654. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  41655. if (ret == 0)
  41656. ret = aesccm_test();
  41657. #endif
  41658. #endif /* !NO_AES */
  41659. #ifndef NO_DES3
  41660. if (ret == 0)
  41661. ret = des3_test();
  41662. #endif /* !NO_DES3 */
  41663. #ifndef NO_SHA
  41664. if (ret == 0)
  41665. ret = sha_test();
  41666. #endif
  41667. #ifndef NO_SHA256
  41668. if (ret == 0)
  41669. ret = sha256_test();
  41670. #endif
  41671. #ifdef WOLFSSL_SHA384
  41672. if (ret == 0)
  41673. ret = sha384_test();
  41674. #endif
  41675. #ifdef WOLFSSL_SHA512
  41676. if (ret == 0)
  41677. ret = sha512_test();
  41678. #endif
  41679. #ifndef NO_HMAC
  41680. #ifndef NO_SHA
  41681. if (ret == 0)
  41682. ret = hmac_sha_test();
  41683. #endif
  41684. #ifndef NO_SHA256
  41685. if (ret == 0)
  41686. ret = hmac_sha256_test();
  41687. #endif
  41688. #endif
  41689. #ifndef NO_PWDBASED
  41690. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256)
  41691. if (ret == 0)
  41692. ret = pbkdf2_test();
  41693. #endif
  41694. #endif
  41695. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  41696. if (ret == 0)
  41697. ret = cmac_test();
  41698. #endif
  41699. /* restore devId */
  41700. devId = origDevId;
  41701. return ret;
  41702. }
  41703. #endif /* WOLF_CRYPTO_CB */
  41704. #ifdef WOLFSSL_CERT_PIV
  41705. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t certpiv_test(void)
  41706. {
  41707. wc_test_ret_t ret;
  41708. wc_CertPIV piv;
  41709. /* Template for Identiv PIV cert, nonce and signature */
  41710. WOLFSSL_SMALL_STACK_STATIC const byte pivCertIdentiv[] = {
  41711. 0x0A, 0x0B,
  41712. 0x53, 0x09, /* NIST PIV Cert */
  41713. 0x70, 0x02, /* Certificate */
  41714. 0x30, 0x00,
  41715. 0x71, 0x01, 0x05, /* Cert Info */
  41716. 0xFE, 0x00, /* Error Detection */
  41717. 0x0B, 0x01, 0x00, /* Nonce */
  41718. 0x0C, 0x01, 0x00, /* Signed Nonce */
  41719. };
  41720. /* PIV certificate data including certificate, info and error detection. */
  41721. WOLFSSL_SMALL_STACK_STATIC const byte pivCert[] = {
  41722. 0x53, 0x09, /* NIST PIV Cert */
  41723. 0x70, 0x02, /* Certificate */
  41724. 0x30, 0x00,
  41725. 0x71, 0x01, 0x04, /* Cert Info */
  41726. 0xFE, 0x00, /* Error Detection */
  41727. };
  41728. XMEMSET(&piv, 0, sizeof(piv));
  41729. /* Test with Identiv 0x0A, 0x0B and 0x0C markers */
  41730. ret = wc_ParseCertPIV(&piv, pivCertIdentiv, sizeof(pivCertIdentiv));
  41731. if (ret != 0) {
  41732. return WC_TEST_RET_ENC_EC(ret);
  41733. }
  41734. if (!piv.isIdentiv) {
  41735. return WC_TEST_RET_ENC_NC;
  41736. }
  41737. if ((piv.cert == NULL) || (piv.certSz != 2)) {
  41738. return WC_TEST_RET_ENC_NC;
  41739. }
  41740. if ((piv.certErrDet == NULL) || (piv.certErrDetSz != 0)) {
  41741. return WC_TEST_RET_ENC_NC;
  41742. }
  41743. if ((piv.compression != ASN_PIV_CERT_INFO_GZIP)) {
  41744. return WC_TEST_RET_ENC_NC;
  41745. }
  41746. if (!piv.isX509) {
  41747. return WC_TEST_RET_ENC_NC;
  41748. }
  41749. if ((piv.nonce == NULL) || (piv.nonceSz != 1)) {
  41750. return WC_TEST_RET_ENC_NC;
  41751. }
  41752. if ((piv.signedNonce == NULL) || (piv.signedNonceSz != 1)) {
  41753. return WC_TEST_RET_ENC_NC;
  41754. }
  41755. XMEMSET(&piv, 0, sizeof(piv));
  41756. /* Test with NIST PIV format */
  41757. ret = wc_ParseCertPIV(&piv, pivCert, sizeof(pivCert));
  41758. if (ret != 0) {
  41759. return WC_TEST_RET_ENC_EC(ret);
  41760. }
  41761. if (piv.isIdentiv) {
  41762. return WC_TEST_RET_ENC_NC;
  41763. }
  41764. if ((piv.cert == NULL) || (piv.certSz != 2)) {
  41765. return WC_TEST_RET_ENC_NC;
  41766. }
  41767. if ((piv.certErrDet == NULL) || (piv.certErrDetSz != 0)) {
  41768. return WC_TEST_RET_ENC_NC;
  41769. }
  41770. if ((piv.compression != 0)) {
  41771. return WC_TEST_RET_ENC_NC;
  41772. }
  41773. if (!piv.isX509) {
  41774. return WC_TEST_RET_ENC_NC;
  41775. }
  41776. return ret;
  41777. }
  41778. #endif /* WOLFSSL_CERT_PIV */
  41779. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  41780. static time_t time_cb(time_t* t)
  41781. {
  41782. if (t != NULL) {
  41783. *t = 99;
  41784. }
  41785. return 99;
  41786. }
  41787. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t time_test(void)
  41788. {
  41789. time_t t;
  41790. wc_test_ret_t ret;
  41791. ret = wc_SetTimeCb(time_cb);
  41792. if (ret != 0)
  41793. return WC_TEST_RET_ENC_EC(ret);
  41794. t = wc_Time(NULL);
  41795. if (t != 99)
  41796. return WC_TEST_RET_ENC_NC;
  41797. ret = wc_GetTime(&t, sizeof(time_t));
  41798. if (ret != 0)
  41799. return WC_TEST_RET_ENC_EC(ret);
  41800. if (t != 99)
  41801. return WC_TEST_RET_ENC_NC;
  41802. ret = wc_SetTimeCb(NULL);
  41803. if (ret != 0)
  41804. return WC_TEST_RET_ENC_EC(ret);
  41805. return 0;
  41806. }
  41807. #endif
  41808. #ifdef WOLFSSL_AES_SIV
  41809. typedef struct {
  41810. const byte key[33];
  41811. word32 keySz;
  41812. const byte nonce[49];
  41813. word32 nonceSz;
  41814. const byte assoc[81];
  41815. word32 assocSz;
  41816. const byte plaintext[83];
  41817. word32 plaintextSz;
  41818. const byte siv[AES_BLOCK_SIZE+1];
  41819. const byte ciphertext[82];
  41820. word32 ciphertextSz;
  41821. } AesSivTestVector;
  41822. #define AES_SIV_TEST_VECTORS 7
  41823. WOLFSSL_TEST_SUBROUTINE wc_test_ret_t aes_siv_test(void)
  41824. {
  41825. /* These test vectors come from chrony 4.1's SIV unit tests. */
  41826. WOLFSSL_SMALL_STACK_STATIC const AesSivTestVector testVectors[AES_SIV_TEST_VECTORS] = {
  41827. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  41828. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  41829. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  41830. "", 0,
  41831. "", 0,
  41832. "\x22\x3e\xb5\x94\xe0\xe0\x25\x4b\x00\x25\x8e\x21\x9a\x1c\xa4\x21",
  41833. "", 0
  41834. },
  41835. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  41836. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  41837. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  41838. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  41839. "", 0,
  41840. "\xd7\x20\x19\x89\xc6\xdb\xc6\xd6\x61\xfc\x62\xbc\x86\x5e\xee\xef",
  41841. "", 0
  41842. },
  41843. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  41844. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  41845. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  41846. "", 0,
  41847. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  41848. "\xb6\xc1\x60\xe9\xc2\xfd\x2a\xe8\xde\xc5\x36\x8b\x2a\x33\xed\xe1",
  41849. "\x14\xff\xb3\x97\x34\x5c\xcb\xe4\x4a\xa4\xde\xac\xd9\x36\x90\x46", 16
  41850. },
  41851. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  41852. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  41853. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e", 15,
  41854. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c", 15,
  41855. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4", 15,
  41856. "\x03\x8c\x41\x51\xba\x7a\x8f\x77\x6e\x56\x31\x99\x42\x0b\xc7\x03",
  41857. "\xe7\x6c\x67\xc9\xda\xb7\x0d\x5b\x44\x06\x26\x5a\xd0\xd2\x3b", 15
  41858. },
  41859. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  41860. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  41861. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  41862. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  41863. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4\xa7", 16,
  41864. "\x5c\x05\x23\x65\xf4\x57\x0a\xa0\xfb\x38\x3e\xce\x9b\x75\x85\xeb",
  41865. "\x68\x85\x19\x36\x0c\x7c\x48\x11\x40\xcb\x9b\x57\x9a\x0e\x65\x32", 16
  41866. },
  41867. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  41868. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  41869. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
  41870. "\xd5", 17,
  41871. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b"
  41872. "\xa0", 17,
  41873. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4\xa7"
  41874. "\x08", 17,
  41875. "\xaf\x58\x4b\xe7\x82\x1e\x96\x19\x29\x91\x25\xe0\xdd\x80\x3b\x49",
  41876. "\xa5\x11\xcd\xb6\x08\xf3\x76\xa0\xb6\xfa\x15\x82\xf3\x95\xe1\xeb"
  41877. "\xbd", 17
  41878. },
  41879. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  41880. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  41881. "\xb0\x5a\x1b\xc7\x56\xe7\xb6\x2c\xb4\x85\xe5\x56\xa5\x28\xc0\x6c"
  41882. "\x2f\x3b\x0b\x9d\x1a\x0c\xdf\x69\x47\xe0\xcc\xc0\x87\xaa\x5c\x09"
  41883. "\x98\x48\x8d\x6a\x8e\x1e\x05\xd7\x8b\x68\x74\x83\xb5\x1d\xf1\x2c", 48,
  41884. "\xe5\x8b\xd2\x6a\x30\xc5\xc5\x61\xcc\xbd\x7c\x27\xbf\xfe\xf9\x06"
  41885. "\x00\x5b\xd7\xfc\x11\x0b\xcf\x16\x61\xef\xac\x05\xa7\xaf\xec\x27"
  41886. "\x41\xc8\x5e\x9e\x0d\xf9\x2f\xaf\x20\x79\x17\xe5\x17\x91\x2a\x27"
  41887. "\x34\x1c\xbc\xaf\xeb\xef\x7f\x52\xe7\x1e\x4c\x2a\xca\xbd\x2b\xbe"
  41888. "\x34\xd6\xfb\x69\xd3\x3e\x49\x59\x60\xb4\x26\xc9\xb8\xce\xba", 79,
  41889. "\x6c\xe7\xcf\x7e\xab\x7b\xa0\xe1\xa7\x22\xcb\x88\xde\x5e\x42\xd2"
  41890. "\xec\x79\xe0\xa2\xcf\x5f\x0f\x6f\x6b\x89\x57\xcd\xae\x17\xd4\xc2"
  41891. "\xf3\x1b\xa2\xa8\x13\x78\x23\x2f\x83\xa8\xd4\x0c\xc0\xd2\xf3\x99"
  41892. "\xae\x81\xa1\xca\x5b\x5f\x45\xa6\x6f\x0c\x8a\xf3\xd4\x67\x40\x81"
  41893. "\x26\xe2\x01\x86\xe8\x5a\xd5\xf8\x58\x80\x9f\x56\xaa\x76\x96\xbf"
  41894. "\x31", 81,
  41895. "\x9a\x06\x33\xe0\xee\x00\x6a\x9b\xc8\x20\xd5\xe2\xc2\xed\xb5\x75",
  41896. "\xfa\x9e\x42\x2a\x31\x6b\xda\xca\xaa\x7d\x31\x8b\x84\x7a\xb8\xd7"
  41897. "\x8a\x81\x25\x64\xed\x41\x9b\xa9\x77\x10\xbd\x05\x0c\x4e\xc5\x31"
  41898. "\x0c\xa2\x86\xec\x8a\x94\xc8\x24\x23\x3c\x13\xee\xa5\x51\xc9\xdf"
  41899. "\x48\xc9\x55\xc5\x2f\x40\x73\x3f\x98\xbb\x8d\x69\x78\x46\x64\x17"
  41900. "\x8d\x49\x2f\x14\x62\xa4\x7c\x2a\x57\x38\x87\xce\xc6\x72\xd3\x5c"
  41901. "\xa1", 81
  41902. }};
  41903. int i;
  41904. byte computedCiphertext[82];
  41905. byte computedPlaintext[82];
  41906. byte siv[AES_BLOCK_SIZE];
  41907. wc_test_ret_t ret = 0;
  41908. for (i = 0; i < AES_SIV_TEST_VECTORS; ++i) {
  41909. ret = wc_AesSivEncrypt(testVectors[i].key, testVectors[i].keySz,
  41910. testVectors[i].assoc, testVectors[i].assocSz,
  41911. testVectors[i].nonce, testVectors[i].nonceSz,
  41912. testVectors[i].plaintext,
  41913. testVectors[i].plaintextSz, siv,
  41914. computedCiphertext);
  41915. if (ret != 0) {
  41916. return WC_TEST_RET_ENC_EC(ret);
  41917. }
  41918. ret = XMEMCMP(siv, testVectors[i].siv, AES_BLOCK_SIZE);
  41919. if (ret != 0) {
  41920. return WC_TEST_RET_ENC_NC;
  41921. }
  41922. ret = XMEMCMP(computedCiphertext, testVectors[i].ciphertext,
  41923. testVectors[i].ciphertextSz);
  41924. if (ret != 0) {
  41925. return WC_TEST_RET_ENC_NC;
  41926. }
  41927. ret = wc_AesSivDecrypt(testVectors[i].key, testVectors[i].keySz,
  41928. testVectors[i].assoc, testVectors[i].assocSz,
  41929. testVectors[i].nonce, testVectors[i].nonceSz,
  41930. computedCiphertext, testVectors[i].plaintextSz,
  41931. siv, computedPlaintext);
  41932. if (ret != 0) {
  41933. return WC_TEST_RET_ENC_EC(ret);
  41934. }
  41935. ret = XMEMCMP(computedPlaintext, testVectors[i].plaintext,
  41936. testVectors[i].plaintextSz);
  41937. if (ret != 0) {
  41938. return WC_TEST_RET_ENC_NC;
  41939. }
  41940. }
  41941. return 0;
  41942. }
  41943. #endif
  41944. #undef ERROR_OUT
  41945. static const int fiducial4 = WC_TEST_RET_LN;
  41946. /* print the fiducial line numbers assigned above, allowing confirmation of
  41947. * source code version match when in doubt.
  41948. */
  41949. static void print_fiducials(void) {
  41950. printf(" [fiducial line numbers: %d %d %d %d]\n",
  41951. fiducial1, fiducial2, fiducial3, fiducial4);
  41952. }
  41953. #else
  41954. #ifndef NO_MAIN_DRIVER
  41955. int main(void) { return 0; }
  41956. #endif
  41957. #endif /* NO_CRYPT_TEST */