test.c 1.4 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931349323493334934349353493634937349383493934940349413494234943349443494534946349473494834949349503495134952349533495434955349563495734958349593496034961349623496334964349653496634967349683496934970349713497234973349743497534976349773497834979349803498134982349833498434985349863498734988349893499034991349923499334994349953499634997349983499935000350013500235003350043500535006350073500835009350103501135012350133501435015350163501735018350193502035021350223502335024350253502635027350283502935030350313503235033350343503535036350373503835039350403504135042350433504435045350463504735048350493505035051350523505335054350553505635057350583505935060350613506235063350643506535066350673506835069350703507135072350733507435075350763507735078350793508035081350823508335084350853508635087350883508935090350913509235093350943509535096350973509835099351003510135102351033510435105351063510735108351093511035111351123511335114351153511635117351183511935120351213512235123351243512535126351273512835129351303513135132351333513435135351363513735138351393514035141351423514335144351453514635147351483514935150351513515235153351543515535156351573515835159351603516135162351633516435165351663516735168351693517035171351723517335174351753517635177351783517935180351813518235183351843518535186351873518835189351903519135192351933519435195351963519735198351993520035201352023520335204352053520635207352083520935210352113521235213352143521535216352173521835219352203522135222352233522435225352263522735228352293523035231352323523335234352353523635237352383523935240352413524235243352443524535246352473524835249352503525135252352533525435255352563525735258352593526035261352623526335264352653526635267352683526935270352713527235273352743527535276352773527835279352803528135282352833528435285352863528735288352893529035291352923529335294352953529635297352983529935300353013530235303353043530535306353073530835309353103531135312353133531435315353163531735318353193532035321353223532335324353253532635327353283532935330353313533235333353343533535336353373533835339353403534135342353433534435345353463534735348353493535035351353523535335354353553535635357353583535935360353613536235363353643536535366353673536835369353703537135372353733537435375353763537735378353793538035381353823538335384353853538635387353883538935390353913539235393353943539535396353973539835399354003540135402354033540435405354063540735408354093541035411354123541335414354153541635417354183541935420354213542235423354243542535426354273542835429354303543135432354333543435435354363543735438354393544035441354423544335444354453544635447354483544935450354513545235453354543545535456354573545835459354603546135462354633546435465354663546735468354693547035471354723547335474354753547635477354783547935480354813548235483354843548535486354873548835489354903549135492354933549435495354963549735498354993550035501355023550335504355053550635507355083550935510355113551235513355143551535516355173551835519355203552135522355233552435525355263552735528355293553035531355323553335534355353553635537355383553935540355413554235543355443554535546355473554835549355503555135552355533555435555355563555735558355593556035561355623556335564355653556635567355683556935570355713557235573355743557535576355773557835579355803558135582355833558435585355863558735588355893559035591355923559335594355953559635597355983559935600356013560235603356043560535606356073560835609356103561135612356133561435615356163561735618356193562035621356223562335624356253562635627356283562935630356313563235633356343563535636356373563835639356403564135642356433564435645356463564735648356493565035651356523565335654356553565635657356583565935660356613566235663356643566535666356673566835669356703567135672356733567435675356763567735678356793568035681356823568335684356853568635687356883568935690356913569235693356943569535696356973569835699357003570135702357033570435705357063570735708357093571035711357123571335714357153571635717357183571935720357213572235723357243572535726357273572835729357303573135732357333573435735357363573735738357393574035741357423574335744357453574635747357483574935750357513575235753357543575535756357573575835759357603576135762357633576435765357663576735768357693577035771357723577335774357753577635777357783577935780357813578235783357843578535786357873578835789357903579135792357933579435795357963579735798357993580035801358023580335804358053580635807358083580935810358113581235813358143581535816358173581835819358203582135822358233582435825358263582735828358293583035831358323583335834358353583635837358383583935840358413584235843358443584535846358473584835849358503585135852358533585435855358563585735858358593586035861358623586335864358653586635867358683586935870358713587235873358743587535876358773587835879358803588135882358833588435885358863588735888358893589035891358923589335894358953589635897358983589935900359013590235903359043590535906359073590835909359103591135912359133591435915359163591735918359193592035921359223592335924359253592635927359283592935930359313593235933359343593535936359373593835939359403594135942359433594435945359463594735948359493595035951359523595335954359553595635957359583595935960359613596235963359643596535966359673596835969359703597135972359733597435975359763597735978359793598035981359823598335984359853598635987359883598935990359913599235993359943599535996359973599835999360003600136002360033600436005360063600736008360093601036011360123601336014360153601636017360183601936020360213602236023360243602536026360273602836029360303603136032360333603436035360363603736038360393604036041360423604336044360453604636047360483604936050360513605236053360543605536056360573605836059360603606136062360633606436065360663606736068360693607036071360723607336074360753607636077360783607936080360813608236083360843608536086360873608836089360903609136092360933609436095360963609736098360993610036101361023610336104361053610636107361083610936110361113611236113361143611536116361173611836119361203612136122361233612436125361263612736128361293613036131361323613336134361353613636137361383613936140361413614236143361443614536146361473614836149361503615136152361533615436155361563615736158361593616036161361623616336164361653616636167361683616936170361713617236173361743617536176361773617836179361803618136182361833618436185361863618736188361893619036191361923619336194361953619636197361983619936200362013620236203362043620536206362073620836209362103621136212362133621436215362163621736218362193622036221362223622336224362253622636227362283622936230362313623236233362343623536236362373623836239362403624136242362433624436245362463624736248362493625036251362523625336254362553625636257362583625936260362613626236263362643626536266362673626836269362703627136272362733627436275362763627736278362793628036281362823628336284362853628636287362883628936290362913629236293362943629536296362973629836299363003630136302363033630436305363063630736308363093631036311363123631336314363153631636317363183631936320363213632236323363243632536326363273632836329363303633136332363333633436335363363633736338363393634036341363423634336344363453634636347363483634936350363513635236353363543635536356363573635836359363603636136362363633636436365363663636736368363693637036371363723637336374363753637636377363783637936380363813638236383363843638536386363873638836389363903639136392363933639436395363963639736398363993640036401364023640336404364053640636407364083640936410364113641236413364143641536416364173641836419364203642136422364233642436425364263642736428364293643036431364323643336434364353643636437364383643936440364413644236443364443644536446364473644836449364503645136452364533645436455364563645736458364593646036461364623646336464364653646636467364683646936470364713647236473364743647536476364773647836479364803648136482364833648436485364863648736488364893649036491364923649336494364953649636497364983649936500365013650236503365043650536506365073650836509365103651136512365133651436515365163651736518365193652036521365223652336524365253652636527365283652936530365313653236533365343653536536365373653836539365403654136542365433654436545365463654736548365493655036551365523655336554365553655636557365583655936560365613656236563365643656536566365673656836569365703657136572365733657436575365763657736578365793658036581365823658336584365853658636587365883658936590365913659236593365943659536596365973659836599366003660136602366033660436605366063660736608366093661036611366123661336614366153661636617366183661936620366213662236623366243662536626366273662836629366303663136632366333663436635366363663736638366393664036641366423664336644366453664636647366483664936650366513665236653366543665536656366573665836659366603666136662366633666436665366663666736668366693667036671366723667336674366753667636677366783667936680366813668236683366843668536686366873668836689366903669136692366933669436695366963669736698366993670036701367023670336704367053670636707367083670936710367113671236713367143671536716367173671836719367203672136722367233672436725367263672736728367293673036731367323673336734367353673636737367383673936740367413674236743367443674536746367473674836749367503675136752367533675436755367563675736758367593676036761367623676336764367653676636767367683676936770367713677236773367743677536776367773677836779367803678136782367833678436785367863678736788367893679036791367923679336794367953679636797367983679936800368013680236803368043680536806368073680836809368103681136812368133681436815368163681736818368193682036821368223682336824368253682636827368283682936830368313683236833368343683536836368373683836839368403684136842368433684436845368463684736848368493685036851368523685336854368553685636857368583685936860368613686236863368643686536866368673686836869368703687136872368733687436875368763687736878368793688036881368823688336884368853688636887368883688936890368913689236893368943689536896368973689836899369003690136902369033690436905369063690736908369093691036911369123691336914369153691636917369183691936920369213692236923369243692536926369273692836929369303693136932369333693436935369363693736938369393694036941369423694336944369453694636947369483694936950369513695236953369543695536956369573695836959369603696136962369633696436965369663696736968369693697036971369723697336974369753697636977369783697936980369813698236983369843698536986369873698836989369903699136992369933699436995369963699736998369993700037001370023700337004370053700637007370083700937010370113701237013370143701537016370173701837019370203702137022370233702437025370263702737028370293703037031370323703337034370353703637037370383703937040370413704237043370443704537046370473704837049370503705137052370533705437055370563705737058370593706037061370623706337064370653706637067370683706937070370713707237073370743707537076370773707837079370803708137082370833708437085370863708737088370893709037091370923709337094370953709637097370983709937100371013710237103371043710537106371073710837109371103711137112371133711437115371163711737118371193712037121371223712337124371253712637127371283712937130371313713237133371343713537136371373713837139371403714137142371433714437145371463714737148371493715037151371523715337154371553715637157371583715937160371613716237163371643716537166371673716837169371703717137172371733717437175371763717737178371793718037181371823718337184371853718637187371883718937190371913719237193371943719537196371973719837199372003720137202372033720437205372063720737208372093721037211372123721337214372153721637217372183721937220372213722237223372243722537226372273722837229372303723137232372333723437235372363723737238372393724037241372423724337244372453724637247372483724937250372513725237253372543725537256372573725837259372603726137262372633726437265372663726737268372693727037271372723727337274372753727637277372783727937280372813728237283372843728537286372873728837289372903729137292372933729437295372963729737298372993730037301373023730337304373053730637307373083730937310373113731237313373143731537316373173731837319373203732137322373233732437325373263732737328373293733037331373323733337334373353733637337373383733937340373413734237343373443734537346373473734837349373503735137352373533735437355373563735737358373593736037361373623736337364373653736637367373683736937370373713737237373373743737537376373773737837379373803738137382373833738437385373863738737388373893739037391373923739337394373953739637397373983739937400374013740237403374043740537406374073740837409374103741137412374133741437415374163741737418374193742037421374223742337424374253742637427374283742937430374313743237433374343743537436374373743837439374403744137442374433744437445374463744737448374493745037451374523745337454374553745637457374583745937460374613746237463374643746537466374673746837469374703747137472374733747437475374763747737478374793748037481374823748337484374853748637487374883748937490374913749237493374943749537496374973749837499375003750137502375033750437505375063750737508375093751037511375123751337514375153751637517375183751937520375213752237523375243752537526375273752837529375303753137532375333753437535375363753737538375393754037541375423754337544375453754637547375483754937550375513755237553375543755537556375573755837559375603756137562375633756437565375663756737568375693757037571375723757337574375753757637577375783757937580375813758237583375843758537586375873758837589375903759137592375933759437595375963759737598375993760037601376023760337604376053760637607376083760937610376113761237613376143761537616376173761837619376203762137622376233762437625376263762737628376293763037631376323763337634376353763637637376383763937640376413764237643376443764537646376473764837649376503765137652376533765437655376563765737658376593766037661376623766337664376653766637667376683766937670376713767237673376743767537676376773767837679376803768137682376833768437685376863768737688376893769037691376923769337694376953769637697376983769937700377013770237703377043770537706377073770837709377103771137712377133771437715377163771737718377193772037721377223772337724377253772637727377283772937730377313773237733377343773537736377373773837739377403774137742377433774437745377463774737748377493775037751377523775337754377553775637757377583775937760377613776237763377643776537766377673776837769377703777137772377733777437775377763777737778377793778037781377823778337784377853778637787377883778937790377913779237793377943779537796377973779837799378003780137802378033780437805378063780737808378093781037811378123781337814378153781637817378183781937820378213782237823378243782537826378273782837829378303783137832378333783437835378363783737838378393784037841378423784337844378453784637847378483784937850378513785237853378543785537856378573785837859378603786137862378633786437865378663786737868378693787037871378723787337874378753787637877378783787937880378813788237883378843788537886378873788837889378903789137892378933789437895378963789737898378993790037901379023790337904379053790637907379083790937910379113791237913379143791537916379173791837919379203792137922379233792437925379263792737928379293793037931379323793337934379353793637937379383793937940379413794237943379443794537946379473794837949379503795137952379533795437955379563795737958379593796037961379623796337964379653796637967379683796937970379713797237973379743797537976379773797837979379803798137982379833798437985379863798737988379893799037991379923799337994379953799637997379983799938000380013800238003380043800538006380073800838009380103801138012380133801438015380163801738018380193802038021380223802338024380253802638027380283802938030380313803238033380343803538036380373803838039380403804138042380433804438045380463804738048380493805038051380523805338054380553805638057380583805938060380613806238063380643806538066380673806838069380703807138072380733807438075380763807738078380793808038081380823808338084380853808638087380883808938090380913809238093380943809538096380973809838099381003810138102381033810438105381063810738108381093811038111381123811338114381153811638117381183811938120381213812238123381243812538126381273812838129381303813138132381333813438135381363813738138381393814038141381423814338144381453814638147381483814938150381513815238153381543815538156381573815838159381603816138162381633816438165381663816738168381693817038171381723817338174381753817638177381783817938180381813818238183381843818538186381873818838189381903819138192381933819438195381963819738198381993820038201382023820338204382053820638207382083820938210382113821238213382143821538216382173821838219382203822138222382233822438225382263822738228382293823038231382323823338234382353823638237382383823938240382413824238243382443824538246382473824838249382503825138252382533825438255382563825738258382593826038261382623826338264382653826638267382683826938270382713827238273382743827538276382773827838279382803828138282382833828438285382863828738288382893829038291382923829338294382953829638297382983829938300383013830238303383043830538306383073830838309383103831138312383133831438315383163831738318383193832038321383223832338324383253832638327383283832938330383313833238333383343833538336383373833838339383403834138342383433834438345383463834738348383493835038351383523835338354383553835638357383583835938360383613836238363383643836538366383673836838369383703837138372383733837438375383763837738378383793838038381383823838338384383853838638387383883838938390383913839238393383943839538396383973839838399384003840138402384033840438405384063840738408384093841038411384123841338414384153841638417384183841938420384213842238423384243842538426384273842838429384303843138432384333843438435384363843738438384393844038441384423844338444384453844638447384483844938450384513845238453384543845538456384573845838459384603846138462384633846438465384663846738468384693847038471384723847338474384753847638477384783847938480384813848238483384843848538486384873848838489384903849138492384933849438495384963849738498384993850038501385023850338504385053850638507385083850938510385113851238513385143851538516385173851838519385203852138522385233852438525385263852738528385293853038531385323853338534385353853638537385383853938540385413854238543385443854538546385473854838549385503855138552385533855438555385563855738558385593856038561385623856338564385653856638567385683856938570385713857238573385743857538576385773857838579385803858138582385833858438585385863858738588385893859038591385923859338594385953859638597385983859938600386013860238603386043860538606386073860838609386103861138612386133861438615386163861738618386193862038621386223862338624386253862638627386283862938630386313863238633386343863538636386373863838639386403864138642386433864438645386463864738648386493865038651386523865338654386553865638657386583865938660386613866238663386643866538666386673866838669386703867138672386733867438675386763867738678386793868038681386823868338684386853868638687386883868938690386913869238693386943869538696386973869838699387003870138702387033870438705387063870738708387093871038711387123871338714387153871638717387183871938720387213872238723387243872538726387273872838729387303873138732387333873438735387363873738738387393874038741387423874338744387453874638747387483874938750387513875238753387543875538756387573875838759387603876138762387633876438765387663876738768387693877038771387723877338774387753877638777387783877938780387813878238783387843878538786387873878838789387903879138792387933879438795387963879738798387993880038801388023880338804388053880638807388083880938810388113881238813388143881538816388173881838819388203882138822388233882438825388263882738828388293883038831388323883338834388353883638837388383883938840388413884238843388443884538846388473884838849388503885138852388533885438855388563885738858388593886038861388623886338864388653886638867388683886938870388713887238873388743887538876388773887838879388803888138882388833888438885388863888738888388893889038891388923889338894388953889638897388983889938900389013890238903389043890538906389073890838909389103891138912389133891438915389163891738918389193892038921389223892338924389253892638927389283892938930389313893238933389343893538936389373893838939389403894138942389433894438945389463894738948389493895038951389523895338954389553895638957389583895938960389613896238963389643896538966389673896838969389703897138972389733897438975389763897738978389793898038981389823898338984389853898638987389883898938990389913899238993389943899538996389973899838999390003900139002390033900439005390063900739008390093901039011390123901339014390153901639017390183901939020390213902239023390243902539026390273902839029390303903139032390333903439035390363903739038390393904039041390423904339044390453904639047390483904939050390513905239053390543905539056390573905839059390603906139062390633906439065390663906739068390693907039071390723907339074390753907639077390783907939080390813908239083390843908539086390873908839089390903909139092390933909439095390963909739098390993910039101391023910339104391053910639107391083910939110391113911239113391143911539116391173911839119391203912139122391233912439125391263912739128391293913039131391323913339134391353913639137391383913939140391413914239143391443914539146391473914839149391503915139152391533915439155391563915739158391593916039161391623916339164391653916639167391683916939170391713917239173391743917539176391773917839179391803918139182391833918439185391863918739188391893919039191391923919339194391953919639197391983919939200392013920239203392043920539206392073920839209392103921139212392133921439215392163921739218392193922039221392223922339224392253922639227392283922939230392313923239233392343923539236392373923839239392403924139242392433924439245392463924739248392493925039251392523925339254392553925639257392583925939260392613926239263392643926539266392673926839269392703927139272392733927439275392763927739278392793928039281392823928339284392853928639287392883928939290392913929239293392943929539296392973929839299393003930139302393033930439305393063930739308393093931039311393123931339314393153931639317393183931939320393213932239323393243932539326393273932839329393303933139332393333933439335393363933739338393393934039341393423934339344393453934639347393483934939350393513935239353393543935539356393573935839359393603936139362393633936439365393663936739368393693937039371393723937339374393753937639377393783937939380393813938239383393843938539386393873938839389393903939139392393933939439395393963939739398393993940039401394023940339404394053940639407394083940939410394113941239413394143941539416394173941839419394203942139422394233942439425394263942739428394293943039431394323943339434394353943639437394383943939440394413944239443394443944539446394473944839449394503945139452394533945439455394563945739458394593946039461394623946339464394653946639467394683946939470394713947239473394743947539476394773947839479394803948139482394833948439485394863948739488394893949039491394923949339494394953949639497394983949939500395013950239503395043950539506395073950839509395103951139512395133951439515395163951739518395193952039521395223952339524395253952639527395283952939530395313953239533395343953539536395373953839539395403954139542395433954439545395463954739548395493955039551395523955339554395553955639557395583955939560395613956239563395643956539566395673956839569395703957139572395733957439575395763957739578395793958039581395823958339584395853958639587395883958939590395913959239593395943959539596395973959839599396003960139602396033960439605396063960739608396093961039611396123961339614396153961639617396183961939620396213962239623396243962539626396273962839629396303963139632396333963439635396363963739638396393964039641396423964339644396453964639647396483964939650396513965239653396543965539656396573965839659396603966139662396633966439665396663966739668396693967039671396723967339674396753967639677396783967939680396813968239683396843968539686396873968839689396903969139692396933969439695396963969739698396993970039701397023970339704397053970639707397083970939710397113971239713397143971539716397173971839719397203972139722397233972439725397263972739728397293973039731397323973339734397353973639737397383973939740397413974239743397443974539746397473974839749397503975139752397533975439755397563975739758397593976039761397623976339764397653976639767397683976939770397713977239773397743977539776397773977839779397803978139782397833978439785397863978739788397893979039791397923979339794397953979639797397983979939800398013980239803398043980539806398073980839809398103981139812398133981439815398163981739818398193982039821398223982339824398253982639827398283982939830398313983239833398343983539836398373983839839398403984139842398433984439845398463984739848398493985039851398523985339854398553985639857398583985939860398613986239863398643986539866398673986839869398703987139872398733987439875398763987739878398793988039881398823988339884398853988639887398883988939890398913989239893398943989539896398973989839899399003990139902399033990439905399063990739908399093991039911399123991339914399153991639917399183991939920399213992239923399243992539926399273992839929399303993139932399333993439935399363993739938399393994039941399423994339944399453994639947399483994939950399513995239953399543995539956399573995839959399603996139962399633996439965399663996739968399693997039971399723997339974399753997639977399783997939980399813998239983399843998539986399873998839989399903999139992399933999439995399963999739998399994000040001400024000340004400054000640007400084000940010400114001240013400144001540016400174001840019400204002140022400234002440025400264002740028400294003040031400324003340034400354003640037400384003940040400414004240043400444004540046400474004840049400504005140052400534005440055400564005740058400594006040061400624006340064400654006640067400684006940070400714007240073400744007540076400774007840079400804008140082400834008440085400864008740088400894009040091400924009340094400954009640097400984009940100401014010240103401044010540106401074010840109401104011140112401134011440115401164011740118401194012040121401224012340124401254012640127401284012940130401314013240133401344013540136401374013840139401404014140142401434014440145401464014740148401494015040151401524015340154401554015640157401584015940160401614016240163401644016540166401674016840169401704017140172401734017440175401764017740178401794018040181401824018340184401854018640187401884018940190401914019240193401944019540196401974019840199402004020140202402034020440205402064020740208402094021040211402124021340214402154021640217402184021940220402214022240223402244022540226402274022840229402304023140232402334023440235402364023740238402394024040241402424024340244402454024640247402484024940250402514025240253402544025540256402574025840259402604026140262402634026440265402664026740268402694027040271402724027340274402754027640277402784027940280402814028240283402844028540286402874028840289402904029140292402934029440295402964029740298402994030040301403024030340304403054030640307403084030940310403114031240313403144031540316403174031840319403204032140322403234032440325403264032740328403294033040331403324033340334403354033640337403384033940340403414034240343403444034540346403474034840349403504035140352403534035440355403564035740358403594036040361403624036340364403654036640367403684036940370403714037240373403744037540376403774037840379403804038140382403834038440385403864038740388403894039040391403924039340394403954039640397403984039940400404014040240403404044040540406404074040840409404104041140412404134041440415404164041740418404194042040421404224042340424404254042640427404284042940430404314043240433404344043540436404374043840439404404044140442404434044440445404464044740448404494045040451404524045340454404554045640457404584045940460404614046240463404644046540466404674046840469404704047140472404734047440475404764047740478404794048040481404824048340484404854048640487404884048940490404914049240493404944049540496404974049840499405004050140502405034050440505405064050740508405094051040511405124051340514405154051640517405184051940520405214052240523405244052540526405274052840529405304053140532405334053440535405364053740538405394054040541405424054340544405454054640547405484054940550405514055240553405544055540556405574055840559405604056140562405634056440565405664056740568405694057040571405724057340574405754057640577405784057940580405814058240583405844058540586405874058840589405904059140592405934059440595405964059740598405994060040601406024060340604406054060640607406084060940610406114061240613406144061540616406174061840619406204062140622406234062440625406264062740628406294063040631406324063340634406354063640637406384063940640406414064240643406444064540646406474064840649406504065140652406534065440655406564065740658406594066040661406624066340664406654066640667406684066940670406714067240673406744067540676406774067840679406804068140682406834068440685406864068740688406894069040691406924069340694406954069640697406984069940700407014070240703407044070540706407074070840709407104071140712407134071440715407164071740718407194072040721407224072340724407254072640727407284072940730407314073240733407344073540736407374073840739407404074140742407434074440745407464074740748407494075040751407524075340754407554075640757407584075940760407614076240763407644076540766407674076840769407704077140772407734077440775407764077740778407794078040781407824078340784407854078640787407884078940790407914079240793407944079540796407974079840799408004080140802408034080440805408064080740808408094081040811408124081340814408154081640817408184081940820408214082240823408244082540826408274082840829408304083140832408334083440835408364083740838408394084040841408424084340844408454084640847408484084940850408514085240853408544085540856408574085840859408604086140862408634086440865408664086740868408694087040871408724087340874408754087640877408784087940880408814088240883408844088540886408874088840889408904089140892408934089440895408964089740898408994090040901409024090340904409054090640907409084090940910409114091240913409144091540916409174091840919409204092140922409234092440925409264092740928409294093040931409324093340934409354093640937409384093940940409414094240943409444094540946409474094840949409504095140952409534095440955409564095740958409594096040961409624096340964409654096640967409684096940970409714097240973409744097540976409774097840979409804098140982409834098440985409864098740988409894099040991409924099340994409954099640997409984099941000410014100241003410044100541006410074100841009410104101141012410134101441015410164101741018410194102041021410224102341024410254102641027410284102941030410314103241033410344103541036410374103841039410404104141042410434104441045410464104741048410494105041051410524105341054410554105641057410584105941060410614106241063410644106541066410674106841069410704107141072410734107441075410764107741078410794108041081410824108341084410854108641087410884108941090410914109241093410944109541096410974109841099411004110141102411034110441105411064110741108411094111041111411124111341114411154111641117411184111941120411214112241123411244112541126411274112841129411304113141132411334113441135411364113741138411394114041141411424114341144411454114641147411484114941150411514115241153411544115541156411574115841159411604116141162411634116441165411664116741168411694117041171411724117341174411754117641177411784117941180411814118241183411844118541186411874118841189411904119141192411934119441195411964119741198411994120041201412024120341204412054120641207412084120941210412114121241213412144121541216412174121841219412204122141222412234122441225412264122741228412294123041231412324123341234412354123641237412384123941240412414124241243412444124541246412474124841249412504125141252412534125441255412564125741258412594126041261412624126341264412654126641267412684126941270412714127241273412744127541276412774127841279412804128141282412834128441285412864128741288412894129041291412924129341294412954129641297412984129941300413014130241303413044130541306413074130841309413104131141312413134131441315413164131741318413194132041321413224132341324413254132641327413284132941330413314133241333413344133541336413374133841339413404134141342413434134441345413464134741348413494135041351413524135341354413554135641357413584135941360413614136241363413644136541366413674136841369413704137141372413734137441375413764137741378413794138041381413824138341384413854138641387413884138941390413914139241393413944139541396413974139841399414004140141402414034140441405414064140741408414094141041411414124141341414414154141641417414184141941420414214142241423414244142541426414274142841429414304143141432414334143441435414364143741438414394144041441414424144341444414454144641447414484144941450414514145241453414544145541456414574145841459414604146141462414634146441465414664146741468414694147041471414724147341474414754147641477414784147941480414814148241483414844148541486414874148841489414904149141492414934149441495414964149741498414994150041501415024150341504415054150641507415084150941510415114151241513415144151541516415174151841519415204152141522415234152441525415264152741528415294153041531415324153341534415354153641537415384153941540415414154241543415444154541546415474154841549415504155141552415534155441555415564155741558415594156041561415624156341564415654156641567415684156941570415714157241573415744157541576415774157841579415804158141582415834158441585415864158741588415894159041591415924159341594415954159641597415984159941600416014160241603416044160541606416074160841609416104161141612416134161441615416164161741618416194162041621416224162341624416254162641627416284162941630416314163241633416344163541636416374163841639416404164141642416434164441645416464164741648416494165041651416524165341654416554165641657416584165941660416614166241663416644166541666416674166841669416704167141672416734167441675416764167741678416794168041681416824168341684416854168641687416884168941690416914169241693416944169541696416974169841699417004170141702417034170441705417064170741708417094171041711417124171341714417154171641717417184171941720417214172241723417244172541726417274172841729417304173141732417334173441735417364173741738417394174041741417424174341744417454174641747417484174941750417514175241753417544175541756417574175841759417604176141762417634176441765417664176741768417694177041771417724177341774417754177641777417784177941780417814178241783417844178541786417874178841789417904179141792417934179441795417964179741798417994180041801418024180341804418054180641807418084180941810418114181241813418144181541816418174181841819418204182141822418234182441825418264182741828418294183041831418324183341834418354183641837418384183941840418414184241843418444184541846418474184841849418504185141852418534185441855418564185741858418594186041861418624186341864418654186641867418684186941870418714187241873418744187541876418774187841879418804188141882418834188441885418864188741888418894189041891418924189341894418954189641897418984189941900419014190241903419044190541906419074190841909419104191141912419134191441915419164191741918419194192041921419224192341924419254192641927419284192941930419314193241933419344193541936419374193841939419404194141942419434194441945419464194741948419494195041951419524195341954419554195641957419584195941960419614196241963419644196541966419674196841969419704197141972419734197441975419764197741978419794198041981419824198341984419854198641987419884198941990419914199241993419944199541996419974199841999420004200142002420034200442005420064200742008420094201042011420124201342014420154201642017420184201942020420214202242023420244202542026420274202842029420304203142032420334203442035420364203742038420394204042041420424204342044420454204642047420484204942050420514205242053420544205542056420574205842059420604206142062420634206442065420664206742068420694207042071420724207342074420754207642077420784207942080420814208242083420844208542086420874208842089420904209142092420934209442095420964209742098420994210042101421024210342104421054210642107421084210942110421114211242113421144211542116421174211842119421204212142122421234212442125421264212742128421294213042131421324213342134421354213642137421384213942140421414214242143421444214542146421474214842149421504215142152421534215442155421564215742158421594216042161421624216342164421654216642167421684216942170421714217242173421744217542176421774217842179421804218142182421834218442185421864218742188421894219042191421924219342194421954219642197421984219942200422014220242203422044220542206422074220842209422104221142212422134221442215422164221742218422194222042221422224222342224422254222642227422284222942230422314223242233422344223542236422374223842239422404224142242422434224442245422464224742248422494225042251422524225342254422554225642257422584225942260422614226242263422644226542266422674226842269422704227142272422734227442275422764227742278422794228042281422824228342284422854228642287422884228942290422914229242293422944229542296422974229842299423004230142302423034230442305423064230742308423094231042311423124231342314423154231642317423184231942320423214232242323423244232542326423274232842329423304233142332423334233442335423364233742338423394234042341423424234342344423454234642347423484234942350423514235242353423544235542356423574235842359423604236142362423634236442365423664236742368423694237042371423724237342374423754237642377423784237942380423814238242383423844238542386423874238842389423904239142392423934239442395423964239742398423994240042401424024240342404424054240642407424084240942410424114241242413424144241542416424174241842419424204242142422424234242442425424264242742428424294243042431424324243342434424354243642437424384243942440424414244242443424444244542446424474244842449424504245142452424534245442455424564245742458424594246042461424624246342464424654246642467424684246942470424714247242473424744247542476424774247842479424804248142482424834248442485424864248742488424894249042491424924249342494424954249642497424984249942500425014250242503425044250542506425074250842509425104251142512425134251442515425164251742518425194252042521425224252342524425254252642527425284252942530425314253242533425344253542536425374253842539425404254142542425434254442545425464254742548425494255042551425524255342554425554255642557425584255942560425614256242563425644256542566425674256842569425704257142572425734257442575425764257742578425794258042581425824258342584425854258642587425884258942590425914259242593425944259542596425974259842599426004260142602426034260442605426064260742608426094261042611426124261342614426154261642617426184261942620426214262242623426244262542626426274262842629426304263142632426334263442635426364263742638426394264042641426424264342644426454264642647426484264942650426514265242653426544265542656426574265842659426604266142662426634266442665426664266742668426694267042671426724267342674426754267642677426784267942680426814268242683426844268542686426874268842689426904269142692426934269442695426964269742698426994270042701427024270342704427054270642707427084270942710427114271242713427144271542716427174271842719427204272142722427234272442725427264272742728427294273042731427324273342734427354273642737427384273942740427414274242743427444274542746427474274842749427504275142752427534275442755427564275742758427594276042761427624276342764427654276642767427684276942770427714277242773427744277542776427774277842779427804278142782427834278442785427864278742788427894279042791427924279342794427954279642797427984279942800428014280242803428044280542806428074280842809428104281142812428134281442815428164281742818428194282042821428224282342824428254282642827428284282942830428314283242833428344283542836428374283842839428404284142842428434284442845428464284742848428494285042851428524285342854428554285642857428584285942860428614286242863428644286542866428674286842869428704287142872428734287442875428764287742878428794288042881428824288342884428854288642887428884288942890428914289242893428944289542896428974289842899429004290142902429034290442905429064290742908429094291042911429124291342914429154291642917429184291942920429214292242923429244292542926429274292842929429304293142932429334293442935429364293742938429394294042941429424294342944429454294642947429484294942950429514295242953429544295542956429574295842959429604296142962429634296442965429664296742968429694297042971429724297342974429754297642977429784297942980429814298242983429844298542986429874298842989429904299142992429934299442995429964299742998429994300043001430024300343004430054300643007430084300943010430114301243013430144301543016430174301843019430204302143022430234302443025430264302743028430294303043031430324303343034430354303643037430384303943040430414304243043430444304543046430474304843049430504305143052430534305443055430564305743058430594306043061430624306343064430654306643067430684306943070430714307243073430744307543076430774307843079430804308143082430834308443085430864308743088430894309043091430924309343094430954309643097430984309943100431014310243103431044310543106431074310843109431104311143112431134311443115431164311743118431194312043121431224312343124431254312643127431284312943130431314313243133431344313543136431374313843139431404314143142431434314443145431464314743148431494315043151431524315343154431554315643157431584315943160431614316243163431644316543166431674316843169431704317143172431734317443175431764317743178431794318043181431824318343184431854318643187431884318943190431914319243193431944319543196431974319843199432004320143202432034320443205432064320743208432094321043211432124321343214432154321643217432184321943220432214322243223432244322543226432274322843229432304323143232432334323443235432364323743238432394324043241432424324343244432454324643247432484324943250432514325243253432544325543256432574325843259432604326143262432634326443265432664326743268432694327043271432724327343274432754327643277432784327943280432814328243283432844328543286432874328843289432904329143292432934329443295432964329743298432994330043301433024330343304433054330643307433084330943310433114331243313433144331543316433174331843319433204332143322433234332443325433264332743328433294333043331433324333343334433354333643337433384333943340433414334243343433444334543346433474334843349433504335143352433534335443355433564335743358433594336043361433624336343364433654336643367433684336943370433714337243373433744337543376433774337843379433804338143382433834338443385433864338743388433894339043391433924339343394433954339643397433984339943400434014340243403434044340543406434074340843409434104341143412434134341443415434164341743418434194342043421434224342343424434254342643427434284342943430434314343243433434344343543436434374343843439434404344143442434434344443445434464344743448434494345043451434524345343454434554345643457434584345943460434614346243463434644346543466434674346843469434704347143472434734347443475434764347743478434794348043481434824348343484434854348643487434884348943490434914349243493434944349543496434974349843499435004350143502435034350443505435064350743508435094351043511435124351343514435154351643517435184351943520435214352243523435244352543526435274352843529435304353143532435334353443535435364353743538435394354043541435424354343544435454354643547435484354943550435514355243553435544355543556435574355843559435604356143562435634356443565435664356743568435694357043571435724357343574435754357643577435784357943580435814358243583435844358543586435874358843589435904359143592435934359443595435964359743598435994360043601436024360343604436054360643607436084360943610436114361243613436144361543616436174361843619436204362143622436234362443625436264362743628436294363043631436324363343634436354363643637436384363943640436414364243643436444364543646436474364843649436504365143652436534365443655436564365743658436594366043661436624366343664436654366643667436684366943670436714367243673436744367543676436774367843679436804368143682436834368443685436864368743688436894369043691436924369343694436954369643697436984369943700437014370243703437044370543706437074370843709437104371143712437134371443715437164371743718437194372043721437224372343724437254372643727437284372943730437314373243733437344373543736437374373843739437404374143742437434374443745437464374743748437494375043751437524375343754437554375643757437584375943760437614376243763437644376543766437674376843769437704377143772437734377443775437764377743778437794378043781437824378343784437854378643787437884378943790437914379243793437944379543796437974379843799438004380143802438034380443805438064380743808438094381043811438124381343814438154381643817438184381943820438214382243823438244382543826438274382843829438304383143832438334383443835438364383743838438394384043841438424384343844438454384643847438484384943850438514385243853438544385543856438574385843859438604386143862438634386443865438664386743868438694387043871438724387343874438754387643877438784387943880438814388243883438844388543886438874388843889438904389143892438934389443895438964389743898438994390043901439024390343904439054390643907439084390943910439114391243913439144391543916439174391843919439204392143922439234392443925439264392743928439294393043931439324393343934439354393643937439384393943940439414394243943439444394543946439474394843949439504395143952439534395443955439564395743958439594396043961439624396343964439654396643967439684396943970439714397243973439744397543976439774397843979439804398143982439834398443985439864398743988439894399043991439924399343994439954399643997439984399944000440014400244003440044400544006440074400844009440104401144012440134401444015440164401744018440194402044021440224402344024440254402644027440284402944030440314403244033440344403544036440374403844039440404404144042440434404444045440464404744048440494405044051440524405344054440554405644057440584405944060440614406244063440644406544066440674406844069440704407144072440734407444075440764407744078440794408044081440824408344084440854408644087440884408944090440914409244093440944409544096440974409844099441004410144102441034410444105441064410744108441094411044111441124411344114441154411644117441184411944120441214412244123441244412544126441274412844129441304413144132441334413444135441364413744138441394414044141441424414344144441454414644147441484414944150441514415244153441544415544156441574415844159441604416144162441634416444165441664416744168441694417044171441724417344174441754417644177441784417944180441814418244183441844418544186441874418844189441904419144192441934419444195441964419744198441994420044201442024420344204442054420644207442084420944210442114421244213442144421544216442174421844219442204422144222442234422444225442264422744228442294423044231442324423344234442354423644237442384423944240442414424244243442444424544246442474424844249442504425144252442534425444255442564425744258442594426044261442624426344264442654426644267
  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. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  51. #ifdef WOLFSSL_TEST_MAX_RELATIVE_HEAP_ALLOCS
  52. static ssize_t max_relative_heap_allocs = WOLFSSL_TEST_MAX_RELATIVE_HEAP_ALLOCS;
  53. #else
  54. static ssize_t max_relative_heap_allocs = -1;
  55. #endif
  56. #ifdef WOLFSSL_TEST_MAX_RELATIVE_HEAP_BYTES
  57. static ssize_t max_relative_heap_bytes = WOLFSSL_TEST_MAX_RELATIVE_HEAP_BYTES;
  58. #else
  59. static ssize_t max_relative_heap_bytes = -1;
  60. #endif
  61. #define PRINT_HEAP_CHECKPOINT() { \
  62. const ssize_t _rha = wolfCrypt_heap_peakAllocs_checkpoint() - heap_baselineAllocs; \
  63. const ssize_t _rhb = wolfCrypt_heap_peakBytes_checkpoint() - heap_baselineBytes; \
  64. printf(" relative heap peak usage: %ld alloc%s, %ld bytes\n", \
  65. (long int)_rha, \
  66. _rha == 1 ? "" : "s", \
  67. (long int)_rhb); \
  68. if ((max_relative_heap_allocs > 0) && (_rha > max_relative_heap_allocs)) \
  69. return err_sys("heap allocs exceed designated max.", -1); \
  70. if ((max_relative_heap_bytes > 0) && (_rhb > max_relative_heap_bytes)) \
  71. return err_sys("heap bytes exceed designated max.", -1); \
  72. heap_baselineAllocs = wolfCrypt_heap_peakAllocs_checkpoint(); \
  73. heap_baselineBytes = wolfCrypt_heap_peakBytes_checkpoint(); \
  74. }
  75. #else
  76. #define PRINT_HEAP_CHECKPOINT()
  77. #endif
  78. #ifdef USE_FLAT_TEST_H
  79. #ifdef HAVE_CONFIG_H
  80. #include "test_paths.h"
  81. #endif
  82. #include "test.h"
  83. #else
  84. #ifdef HAVE_CONFIG_H
  85. #include "wolfcrypt/test/test_paths.h"
  86. #endif
  87. #include "wolfcrypt/test/test.h"
  88. #endif
  89. /* printf mappings */
  90. #ifndef WOLFSSL_LOG_PRINTF
  91. #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
  92. #include <mqx.h>
  93. #include <stdlib.h>
  94. /* see wc_port.h for fio.h and nio.h includes */
  95. #elif defined(FREESCALE_KSDK_BM)
  96. #include "fsl_debug_console.h"
  97. #undef printf
  98. #define printf PRINTF
  99. #elif defined(WOLFSSL_APACHE_MYNEWT)
  100. #include <assert.h>
  101. #include <string.h>
  102. #include "sysinit/sysinit.h"
  103. #include "os/os.h"
  104. #ifdef ARCH_sim
  105. #include "mcu/mcu_sim.h"
  106. #endif
  107. #include "os/os_time.h"
  108. #elif defined(WOLFSSL_ESPIDF)
  109. #include <time.h>
  110. #include <sys/time.h>
  111. #include <esp_log.h>
  112. #elif defined(WOLFSSL_ZEPHYR)
  113. #include <stdio.h>
  114. #define printf printk
  115. #elif defined(MICRIUM)
  116. #include <os.h>
  117. #if (OS_VERSION < 50000)
  118. #include <bsp_ser.h>
  119. void BSP_Ser_Printf (CPU_CHAR* format, ...);
  120. #undef printf
  121. #define printf BSP_Ser_Printf
  122. #else
  123. #include <stdio.h>
  124. #endif
  125. #elif defined(WOLFSSL_PB)
  126. #include <stdarg.h>
  127. int wolfssl_pb_print(const char*, ...);
  128. #undef printf
  129. #define printf wolfssl_pb_print
  130. #elif defined(WOLFSSL_TELIT_M2MB)
  131. #include "wolfssl/wolfcrypt/wc_port.h" /* for m2mb headers */
  132. #include "m2m_log.h" /* for M2M_LOG_INFO - not standard API */
  133. /* remap printf */
  134. #undef printf
  135. #define printf M2M_LOG_INFO
  136. /* OS requires occasional sleep() */
  137. #ifndef TEST_SLEEP_MS
  138. #define TEST_SLEEP_MS 50
  139. #endif
  140. #define TEST_SLEEP() m2mb_os_taskSleep(M2MB_OS_MS2TICKS(TEST_SLEEP_MS))
  141. /* don't use file system for these tests, since ./certs dir isn't loaded */
  142. #undef NO_FILESYSTEM
  143. #define NO_FILESYSTEM
  144. #elif defined(THREADX) && !defined(WOLFSSL_WICED) && \
  145. !defined(THREADX_NO_DC_PRINTF)
  146. #ifndef NETOS
  147. /* since just testing, use THREADX log printf instead (NETOS prototypes
  148. * this elsewhere) */
  149. int dc_log_printf(char*, ...);
  150. #endif
  151. #undef printf
  152. #define printf dc_log_printf
  153. #elif defined(ANDROID)
  154. #ifdef XMALLOC_USER
  155. #include <stdlib.h> /* we're using malloc / free direct here */
  156. #endif
  157. #ifndef STRING_USER
  158. #include <stdio.h>
  159. #endif
  160. #include <android/log.h>
  161. #ifdef ANDROID_V454 /* See fips/android/wolfCrypt_v454_android */
  162. #ifndef NO_FILESYSTEM
  163. #define NO_FILESYSTEM /* Turn off tests that want to call SaveDerAndPem() */
  164. #endif
  165. #else
  166. #define printf(...) \
  167. __android_log_print(ANDROID_LOG_DEBUG, "[WOLFCRYPT]", __VA_ARGS__)
  168. #define fprintf(fp, ...) \
  169. __android_log_print(ANDROID_LOG_DEBUG, "[WOLFCRYPT]", __VA_ARGS__)
  170. #endif
  171. #elif defined(WOLFSSL_DEOS)
  172. #include <printx.h>
  173. #undef printf
  174. #define printf printx
  175. #else
  176. #ifdef XMALLOC_USER
  177. #include <stdlib.h> /* we're using malloc / free direct here */
  178. #endif
  179. #if !defined(STRING_USER) && !defined(WOLFSSL_LINUXKM)
  180. #include <stdio.h>
  181. #endif
  182. #if defined(WOLFSSL_LINUXKM) && !defined(WOLFSSL_LINUXKM_VERBOSE_DEBUG)
  183. #undef printf
  184. #define printf(...) ({})
  185. #endif
  186. /* enable way for customer to override test/bench printf */
  187. #ifdef XPRINTF
  188. #undef printf
  189. #define printf XPRINTF
  190. #elif !defined(printf)
  191. /* arrange for printf() to flush after every message -- this assures
  192. * redirected output (to a log file) records progress right up to the
  193. * moment of a crash/abort(); otherwise anything queued in stdout would
  194. * be lost.
  195. */
  196. #define printf(...) ( printf(__VA_ARGS__), fflush(stdout) )
  197. #endif
  198. #endif
  199. #endif /* !WOLFSSL_LOG_PRINTF */
  200. #include <wolfssl/wolfcrypt/memory.h>
  201. #include <wolfssl/wolfcrypt/wc_port.h>
  202. #include <wolfssl/wolfcrypt/logging.h>
  203. #include <wolfssl/wolfcrypt/types.h>
  204. #include <wolfssl/wolfcrypt/asn.h>
  205. #include <wolfssl/wolfcrypt/md2.h>
  206. #include <wolfssl/wolfcrypt/md5.h>
  207. #include <wolfssl/wolfcrypt/md4.h>
  208. #include <wolfssl/wolfcrypt/sha.h>
  209. #include <wolfssl/wolfcrypt/sha256.h>
  210. #include <wolfssl/wolfcrypt/sha512.h>
  211. #include <wolfssl/wolfcrypt/rc2.h>
  212. #include <wolfssl/wolfcrypt/arc4.h>
  213. #if defined(WC_NO_RNG)
  214. #include <wolfssl/wolfcrypt/integer.h>
  215. #else
  216. #include <wolfssl/wolfcrypt/random.h>
  217. #endif
  218. #include <wolfssl/wolfcrypt/coding.h>
  219. #include <wolfssl/wolfcrypt/signature.h>
  220. #include <wolfssl/wolfcrypt/rsa.h>
  221. #include <wolfssl/wolfcrypt/des3.h>
  222. #include <wolfssl/wolfcrypt/aes.h>
  223. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  224. #include <wolfssl/wolfcrypt/cmac.h>
  225. #include <wolfssl/wolfcrypt/siphash.h>
  226. #include <wolfssl/wolfcrypt/poly1305.h>
  227. #include <wolfssl/wolfcrypt/camellia.h>
  228. #include <wolfssl/wolfcrypt/hmac.h>
  229. #include <wolfssl/wolfcrypt/kdf.h>
  230. #include <wolfssl/wolfcrypt/dh.h>
  231. #include <wolfssl/wolfcrypt/dsa.h>
  232. #include <wolfssl/wolfcrypt/srp.h>
  233. #include <wolfssl/wolfcrypt/chacha.h>
  234. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  235. #include <wolfssl/wolfcrypt/pwdbased.h>
  236. #include <wolfssl/wolfcrypt/ripemd.h>
  237. #include <wolfssl/wolfcrypt/error-crypt.h>
  238. #ifdef HAVE_ECC
  239. #include <wolfssl/wolfcrypt/ecc.h>
  240. #endif
  241. #ifdef HAVE_CURVE25519
  242. #include <wolfssl/wolfcrypt/curve25519.h>
  243. #endif
  244. #ifdef HAVE_ED25519
  245. #include <wolfssl/wolfcrypt/ed25519.h>
  246. #endif
  247. #ifdef HAVE_CURVE448
  248. #include <wolfssl/wolfcrypt/curve448.h>
  249. #endif
  250. #ifdef HAVE_ED448
  251. #include <wolfssl/wolfcrypt/ed448.h>
  252. #endif
  253. #ifdef WOLFSSL_HAVE_KYBER
  254. #include <wolfssl/wolfcrypt/kyber.h>
  255. #ifdef WOLFSSL_WC_KYBER
  256. #include <wolfssl/wolfcrypt/wc_kyber.h>
  257. #endif
  258. #if defined(HAVE_LIBOQS) || defined(HAVE_PQM4)
  259. #include <wolfssl/wolfcrypt/ext_kyber.h>
  260. #endif
  261. #endif
  262. #ifdef WOLFCRYPT_HAVE_ECCSI
  263. #include <wolfssl/wolfcrypt/eccsi.h>
  264. #endif
  265. #ifdef WOLFCRYPT_HAVE_SAKKE
  266. #include <wolfssl/wolfcrypt/sakke.h>
  267. #endif
  268. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  269. #include <wolfssl/wolfcrypt/blake2.h>
  270. #endif
  271. #ifdef WOLFSSL_SHA3
  272. #include <wolfssl/wolfcrypt/sha3.h>
  273. #endif
  274. #ifdef HAVE_LIBZ
  275. #include <wolfssl/wolfcrypt/compress.h>
  276. #endif
  277. #ifdef HAVE_PKCS7
  278. #include <wolfssl/wolfcrypt/pkcs7.h>
  279. #endif
  280. #ifdef HAVE_FIPS
  281. #include <wolfssl/wolfcrypt/fips_test.h>
  282. #endif
  283. #ifdef HAVE_SELFTEST
  284. #include <wolfssl/wolfcrypt/selftest.h>
  285. #endif
  286. #ifdef WOLFSSL_ASYNC_CRYPT
  287. #include <wolfssl/wolfcrypt/async.h>
  288. #endif
  289. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  290. #include <wolfssl/wolfcrypt/logging.h>
  291. #endif
  292. #ifdef WOLFSSL_CAAM
  293. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  294. #endif
  295. #ifdef WOLF_CRYPTO_CB
  296. #include <wolfssl/wolfcrypt/cryptocb.h>
  297. #ifdef HAVE_INTEL_QA_SYNC
  298. #include <wolfssl/wolfcrypt/port/intel/quickassist_sync.h>
  299. #endif
  300. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  301. #include <wolfssl/wolfcrypt/port/cavium/cavium_octeon_sync.h>
  302. #endif
  303. #endif
  304. #ifdef _MSC_VER
  305. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  306. #pragma warning(disable: 4996)
  307. #endif
  308. #ifdef OPENSSL_EXTRA
  309. #ifndef WOLFCRYPT_ONLY
  310. #include <wolfssl/openssl/evp.h>
  311. #include <wolfssl/openssl/hmac.h>
  312. #endif
  313. #include <wolfssl/openssl/rand.h>
  314. #include <wolfssl/openssl/aes.h>
  315. #include <wolfssl/openssl/des.h>
  316. #endif
  317. #if defined(NO_FILESYSTEM) || defined(WC_NO_RNG)
  318. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  319. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  320. #define USE_CERT_BUFFERS_2048
  321. #endif
  322. #if !defined(USE_CERT_BUFFERS_256)
  323. #define USE_CERT_BUFFERS_256
  324. #endif
  325. #endif
  326. #if defined(WOLFSSL_CERT_GEN) && (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES))
  327. #define ENABLE_ECC384_CERT_GEN_TEST
  328. #endif
  329. #include <wolfssl/certs_test.h>
  330. #ifdef DEVKITPRO
  331. #include <wiiuse/wpad.h>
  332. #endif
  333. #ifdef WOLFSSL_STATIC_MEMORY
  334. static WOLFSSL_HEAP_HINT* HEAP_HINT;
  335. #else
  336. #define HEAP_HINT NULL
  337. #endif /* WOLFSSL_STATIC_MEMORY */
  338. /* these cases do not have intermediate hashing support */
  339. #if (defined(WOLFSSL_AFALG_XILINX_SHA3) && !defined(WOLFSSL_AFALG_HASH_KEEP)) \
  340. && !defined(WOLFSSL_XILINX_CRYPT) || defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  341. #define NO_INTM_HASH_TEST
  342. #endif
  343. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  344. defined(WOLFSSL_SECO_CAAM)
  345. #define HASH_SIZE_LIMIT
  346. #endif
  347. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  348. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  349. static void initDefaultName(void);
  350. #endif
  351. /* for async devices */
  352. #ifdef WOLFSSL_CAAM_DEVID
  353. static int devId = WOLFSSL_CAAM_DEVID;
  354. #else
  355. static int devId = INVALID_DEVID;
  356. #endif
  357. #ifdef HAVE_WNR
  358. const char* wnrConfigFile = "wnr-example.conf";
  359. #endif
  360. #define TEST_STRING "Everyone gets Friday off."
  361. #define TEST_STRING_SZ 25
  362. typedef struct testVector {
  363. const char* input;
  364. const char* output;
  365. size_t inLen;
  366. size_t outLen;
  367. } testVector;
  368. #ifndef WOLFSSL_TEST_SUBROUTINE
  369. #define WOLFSSL_TEST_SUBROUTINE
  370. #endif
  371. PRAGMA_GCC("GCC diagnostic ignored \"-Wunused-function\"")
  372. PRAGMA_CLANG("clang diagnostic ignored \"-Wunused-function\"")
  373. WOLFSSL_TEST_SUBROUTINE int error_test(void);
  374. WOLFSSL_TEST_SUBROUTINE int base64_test(void);
  375. WOLFSSL_TEST_SUBROUTINE int base16_test(void);
  376. WOLFSSL_TEST_SUBROUTINE int asn_test(void);
  377. WOLFSSL_TEST_SUBROUTINE int md2_test(void);
  378. WOLFSSL_TEST_SUBROUTINE int md5_test(void);
  379. WOLFSSL_TEST_SUBROUTINE int md4_test(void);
  380. WOLFSSL_TEST_SUBROUTINE int sha_test(void);
  381. WOLFSSL_TEST_SUBROUTINE int sha224_test(void);
  382. WOLFSSL_TEST_SUBROUTINE int sha256_test(void);
  383. WOLFSSL_TEST_SUBROUTINE int sha512_test(void);
  384. WOLFSSL_TEST_SUBROUTINE int sha384_test(void);
  385. WOLFSSL_TEST_SUBROUTINE int sha3_test(void);
  386. WOLFSSL_TEST_SUBROUTINE int shake128_test(void);
  387. WOLFSSL_TEST_SUBROUTINE int shake256_test(void);
  388. WOLFSSL_TEST_SUBROUTINE int hash_test(void);
  389. WOLFSSL_TEST_SUBROUTINE int hmac_md5_test(void);
  390. WOLFSSL_TEST_SUBROUTINE int hmac_sha_test(void);
  391. WOLFSSL_TEST_SUBROUTINE int hmac_sha224_test(void);
  392. WOLFSSL_TEST_SUBROUTINE int hmac_sha256_test(void);
  393. WOLFSSL_TEST_SUBROUTINE int hmac_sha384_test(void);
  394. WOLFSSL_TEST_SUBROUTINE int hmac_sha512_test(void);
  395. WOLFSSL_TEST_SUBROUTINE int hmac_sha3_test(void);
  396. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  397. /* hkdf_test has issue with WOLFSSL_TEST_SUBROUTINE set on Xilinx with afalg */
  398. static int hkdf_test(void);
  399. #endif
  400. WOLFSSL_TEST_SUBROUTINE int sshkdf_test(void);
  401. #ifdef WOLFSSL_TLS13
  402. WOLFSSL_TEST_SUBROUTINE int tls13_kdf_test(void);
  403. #endif
  404. WOLFSSL_TEST_SUBROUTINE int x963kdf_test(void);
  405. WOLFSSL_TEST_SUBROUTINE int arc4_test(void);
  406. #ifdef WC_RC2
  407. WOLFSSL_TEST_SUBROUTINE int rc2_test(void);
  408. #endif
  409. WOLFSSL_TEST_SUBROUTINE int chacha_test(void);
  410. WOLFSSL_TEST_SUBROUTINE int XChaCha_test(void);
  411. WOLFSSL_TEST_SUBROUTINE int chacha20_poly1305_aead_test(void);
  412. WOLFSSL_TEST_SUBROUTINE int XChaCha20Poly1305_test(void);
  413. WOLFSSL_TEST_SUBROUTINE int des_test(void);
  414. WOLFSSL_TEST_SUBROUTINE int des3_test(void);
  415. WOLFSSL_TEST_SUBROUTINE int aes_test(void);
  416. WOLFSSL_TEST_SUBROUTINE int aes192_test(void);
  417. WOLFSSL_TEST_SUBROUTINE int aes256_test(void);
  418. WOLFSSL_TEST_SUBROUTINE int aesofb_test(void);
  419. WOLFSSL_TEST_SUBROUTINE int cmac_test(void);
  420. #if defined(WOLFSSL_SIPHASH)
  421. WOLFSSL_TEST_SUBROUTINE int siphash_test(void);
  422. #endif
  423. WOLFSSL_TEST_SUBROUTINE int poly1305_test(void);
  424. WOLFSSL_TEST_SUBROUTINE int aesgcm_test(void);
  425. WOLFSSL_TEST_SUBROUTINE int aesgcm_default_test(void);
  426. WOLFSSL_TEST_SUBROUTINE int gmac_test(void);
  427. WOLFSSL_TEST_SUBROUTINE int aesccm_test(void);
  428. WOLFSSL_TEST_SUBROUTINE int aeskeywrap_test(void);
  429. WOLFSSL_TEST_SUBROUTINE int camellia_test(void);
  430. #ifdef WC_RSA_NO_PADDING
  431. WOLFSSL_TEST_SUBROUTINE int rsa_no_pad_test(void);
  432. #endif
  433. WOLFSSL_TEST_SUBROUTINE int rsa_test(void);
  434. WOLFSSL_TEST_SUBROUTINE int dh_test(void);
  435. WOLFSSL_TEST_SUBROUTINE int dsa_test(void);
  436. WOLFSSL_TEST_SUBROUTINE int srp_test(void);
  437. #ifndef WC_NO_RNG
  438. WOLFSSL_TEST_SUBROUTINE int random_test(void);
  439. #endif /* WC_NO_RNG */
  440. WOLFSSL_TEST_SUBROUTINE int pwdbased_test(void);
  441. WOLFSSL_TEST_SUBROUTINE int ripemd_test(void);
  442. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  443. WOLFSSL_TEST_SUBROUTINE int openssl_test(void); /* test mini api */
  444. WOLFSSL_TEST_SUBROUTINE int openssl_pkey0_test(void);
  445. WOLFSSL_TEST_SUBROUTINE int openssl_pkey1_test(void);
  446. WOLFSSL_TEST_SUBROUTINE int openSSL_evpMD_test(void);
  447. WOLFSSL_TEST_SUBROUTINE int openssl_evpSig_test(void);
  448. #endif
  449. WOLFSSL_TEST_SUBROUTINE int pbkdf1_test(void);
  450. WOLFSSL_TEST_SUBROUTINE int pkcs12_test(void);
  451. WOLFSSL_TEST_SUBROUTINE int pbkdf2_test(void);
  452. WOLFSSL_TEST_SUBROUTINE int scrypt_test(void);
  453. #ifdef HAVE_ECC
  454. WOLFSSL_TEST_SUBROUTINE int ecc_test(void);
  455. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  456. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  457. WOLFSSL_TEST_SUBROUTINE int ecc_encrypt_test(void);
  458. #endif
  459. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  460. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  461. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  462. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  463. /* skip for ATECC508/608A, cannot import private key buffers */
  464. WOLFSSL_TEST_SUBROUTINE int ecc_test_buffers(void);
  465. #endif
  466. #endif
  467. #ifdef HAVE_CURVE25519
  468. WOLFSSL_TEST_SUBROUTINE int curve25519_test(void);
  469. #endif
  470. #ifdef HAVE_ED25519
  471. WOLFSSL_TEST_SUBROUTINE int ed25519_test(void);
  472. #endif
  473. #ifdef HAVE_CURVE448
  474. WOLFSSL_TEST_SUBROUTINE int curve448_test(void);
  475. #endif
  476. #ifdef HAVE_ED448
  477. WOLFSSL_TEST_SUBROUTINE int ed448_test(void);
  478. #endif
  479. #ifdef WOLFSSL_HAVE_KYBER
  480. WOLFSSL_TEST_SUBROUTINE int kyber_test(void);
  481. #endif
  482. #ifdef WOLFCRYPT_HAVE_ECCSI
  483. WOLFSSL_TEST_SUBROUTINE int eccsi_test(void);
  484. #endif
  485. #ifdef WOLFCRYPT_HAVE_SAKKE
  486. WOLFSSL_TEST_SUBROUTINE int sakke_test(void);
  487. #endif
  488. #ifdef HAVE_BLAKE2
  489. WOLFSSL_TEST_SUBROUTINE int blake2b_test(void);
  490. #endif
  491. #ifdef HAVE_BLAKE2S
  492. WOLFSSL_TEST_SUBROUTINE int blake2s_test(void);
  493. #endif
  494. #ifdef HAVE_LIBZ
  495. WOLFSSL_TEST_SUBROUTINE int compress_test(void);
  496. #endif
  497. #ifdef HAVE_PKCS7
  498. #ifndef NO_PKCS7_ENCRYPTED_DATA
  499. WOLFSSL_TEST_SUBROUTINE int pkcs7encrypted_test(void);
  500. #endif
  501. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  502. WOLFSSL_TEST_SUBROUTINE int pkcs7compressed_test(void);
  503. #endif
  504. WOLFSSL_TEST_SUBROUTINE int pkcs7signed_test(void);
  505. WOLFSSL_TEST_SUBROUTINE int pkcs7enveloped_test(void);
  506. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  507. WOLFSSL_TEST_SUBROUTINE int pkcs7authenveloped_test(void);
  508. #endif
  509. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  510. WOLFSSL_TEST_SUBROUTINE int pkcs7callback_test(byte* cert, word32 certSz, byte* key,
  511. word32 keySz);
  512. #endif
  513. #endif
  514. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  515. !defined(NO_FILESYSTEM)
  516. WOLFSSL_TEST_SUBROUTINE int cert_test(void);
  517. #endif
  518. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  519. !defined(NO_FILESYSTEM) && defined(WOLFSSL_CERT_GEN)
  520. WOLFSSL_TEST_SUBROUTINE int certext_test(void);
  521. #endif
  522. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  523. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  524. WOLFSSL_TEST_SUBROUTINE int decodedCertCache_test(void);
  525. #endif
  526. WOLFSSL_TEST_SUBROUTINE int memory_test(void);
  527. #ifdef HAVE_VALGRIND
  528. WOLFSSL_TEST_SUBROUTINE int mp_test(void);
  529. #endif
  530. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  531. WOLFSSL_TEST_SUBROUTINE int prime_test(void);
  532. #endif
  533. #if defined(ASN_BER_TO_DER) && \
  534. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  535. defined(OPENSSL_EXTRA_X509_SMALL))
  536. WOLFSSL_TEST_SUBROUTINE int berder_test(void);
  537. #endif
  538. WOLFSSL_TEST_SUBROUTINE int logging_test(void);
  539. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  540. WOLFSSL_TEST_SUBROUTINE int time_test(void);
  541. #endif
  542. WOLFSSL_TEST_SUBROUTINE int mutex_test(void);
  543. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  544. WOLFSSL_TEST_SUBROUTINE int memcb_test(void);
  545. #endif
  546. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  547. WOLFSSL_TEST_SUBROUTINE int blob_test(void);
  548. #endif
  549. #ifdef WOLF_CRYPTO_CB
  550. WOLFSSL_TEST_SUBROUTINE int cryptocb_test(void);
  551. #endif
  552. #ifdef WOLFSSL_CERT_PIV
  553. WOLFSSL_TEST_SUBROUTINE int certpiv_test(void);
  554. #endif
  555. #ifdef WOLFSSL_AES_SIV
  556. WOLFSSL_TEST_SUBROUTINE int aes_siv_test(void);
  557. #endif
  558. /* General big buffer size for many tests. */
  559. #define FOURK_BUF 4096
  560. #define ERROR_OUT(err, eLabel) do { ret = (err); goto eLabel; } while (0)
  561. #ifdef HAVE_STACK_SIZE
  562. static THREAD_RETURN err_sys(const char* msg, int es)
  563. #else
  564. static int err_sys(const char* msg, int es)
  565. #endif
  566. {
  567. (void)msg;
  568. (void)es;
  569. #ifdef WOLFSSL_LINUXKM
  570. lkm_printf("%s error = %d\n", msg, es);
  571. EXIT_TEST(es);
  572. #else
  573. printf("%s error = %d\n", msg, es);
  574. EXIT_TEST(-1);
  575. #endif
  576. }
  577. #ifndef HAVE_WOLFCRYPT_TEST_OPTIONS
  578. /* func_args from test.h, so don't have to pull in other stuff */
  579. typedef struct func_args {
  580. int argc;
  581. char** argv;
  582. int return_code;
  583. } func_args;
  584. #endif /* !HAVE_WOLFCRYPT_TEST_OPTIONS */
  585. #if defined(HAVE_FIPS) && !defined(WOLFSSL_LINUXKM)
  586. static void myFipsCb(int ok, int err, const char* hash)
  587. {
  588. printf("in my Fips callback, ok = %d, err = %d\n", ok, err);
  589. printf("message = %s\n", wc_GetErrorString(err));
  590. printf("hash = %s\n", hash);
  591. if (err == IN_CORE_FIPS_E) {
  592. printf("In core integrity hash check failure, copy above hash\n");
  593. printf("into verifyCore[] in fips_test.c and rebuild\n");
  594. }
  595. }
  596. #endif /* HAVE_FIPS && !WOLFSSL_LINUXKM */
  597. #ifdef WOLFSSL_STATIC_MEMORY
  598. #if defined(WOLFSSL_STATIC_MEMORY_TEST_SZ)
  599. static byte gTestMemory[WOLFSSL_STATIC_MEMORY_TEST_SZ];
  600. #elif defined(BENCH_EMBEDDED)
  601. static byte gTestMemory[14000];
  602. #elif defined(WOLFSSL_CERT_EXT)
  603. static byte gTestMemory[140000];
  604. #elif (defined(WOLFSSL_SP_MATH_ALL) || defined(USE_FAST_MATH)) && \
  605. !defined(ALT_ECC_SIZE)
  606. static byte gTestMemory[160000];
  607. #else
  608. static byte gTestMemory[80000];
  609. #endif
  610. #endif
  611. #ifdef WOLFSSL_PB
  612. static int wolfssl_pb_print(const char* msg, ...)
  613. {
  614. int ret;
  615. va_list args;
  616. char tmpBuf[80];
  617. va_start(args, msg);
  618. ret = vsprint(tmpBuf, msg, args);
  619. va_end(args);
  620. fnDumpStringToSystemLog(tmpBuf);
  621. return ret;
  622. }
  623. #endif /* WOLFSSL_PB */
  624. /* optional macro to add sleep between tests */
  625. #ifndef TEST_SLEEP
  626. #define TEST_SLEEP()
  627. #else
  628. #define TEST_PASS test_pass
  629. #include <stdarg.h> /* for var args */
  630. static WC_INLINE void test_pass(const char* fmt, ...)
  631. {
  632. va_list args;
  633. va_start(args, fmt);
  634. STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK(max_relative_stack, vprintf(fmt, args));
  635. va_end(args);
  636. PRINT_HEAP_CHECKPOINT();
  637. TEST_SLEEP();
  638. ASSERT_RESTORED_VECTOR_REGISTERS(exit(1););
  639. }
  640. #endif
  641. /* set test pass output to printf if not overriden */
  642. #ifndef TEST_PASS
  643. /* redirect to printf */
  644. #define TEST_PASS(...) { \
  645. if (STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK \
  646. (max_relative_stack, printf(__VA_ARGS__)) < 0) { \
  647. return err_sys("post-test check failed", -1); \
  648. } \
  649. PRINT_HEAP_CHECKPOINT(); \
  650. ASSERT_RESTORED_VECTOR_REGISTERS(exit(1);); \
  651. }
  652. #endif
  653. #ifdef HAVE_STACK_SIZE
  654. THREAD_RETURN WOLFSSL_THREAD wolfcrypt_test(void* args)
  655. #else
  656. int wolfcrypt_test(void* args)
  657. #endif
  658. {
  659. int ret;
  660. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  661. long heap_baselineAllocs, heap_baselineBytes;
  662. #endif
  663. STACK_SIZE_INIT();
  664. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  665. (void)wolfCrypt_heap_peakAllocs_checkpoint();
  666. heap_baselineAllocs = wolfCrypt_heap_peakAllocs_checkpoint();
  667. (void)wolfCrypt_heap_peakBytes_checkpoint();
  668. heap_baselineBytes = wolfCrypt_heap_peakBytes_checkpoint();
  669. #endif
  670. printf("------------------------------------------------------------------------------\n");
  671. printf(" wolfSSL version %s\n", LIBWOLFSSL_VERSION_STRING);
  672. printf("------------------------------------------------------------------------------\n");
  673. if (args) {
  674. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  675. int ch;
  676. #endif
  677. ((func_args*)args)->return_code = -1; /* error state */
  678. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  679. while ((ch = mygetopt(((func_args*)args)->argc, ((func_args*)args)->argv, "s:m:a:h")) != -1) {
  680. switch(ch) {
  681. case 's':
  682. #ifdef HAVE_STACK_SIZE_VERBOSE
  683. max_relative_stack = (ssize_t)atoi(myoptarg);
  684. break;
  685. #else
  686. return err_sys("-s (max relative stack bytes) requires HAVE_STACK_SIZE_VERBOSE (--enable-stacksize=verbose).", -1);
  687. #endif
  688. case 'm':
  689. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  690. max_relative_heap_bytes = (ssize_t)atoi(myoptarg);
  691. break;
  692. #else
  693. return err_sys("-m (max relative heap memory bytes) requires WOLFSSL_TRACK_MEMORY_VERBOSE (--enable-trackmemory=verbose).", -1);
  694. #endif
  695. case 'a':
  696. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  697. max_relative_heap_allocs = (ssize_t)atoi(myoptarg);
  698. break;
  699. #else
  700. return err_sys("-a (max relative heap allocs) requires WOLFSSL_TRACK_MEMORY_VERBOSE (--enable-trackmemory=verbose).", -1);
  701. #endif
  702. case 'h':
  703. return err_sys("\
  704. options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\
  705. [-a max_relative_heap_allocs] [-h]\n", 0);
  706. default:
  707. return err_sys("unknown test option. try -h.", -1);
  708. }
  709. }
  710. #endif
  711. }
  712. #ifdef WOLFSSL_STATIC_MEMORY
  713. if (wc_LoadStaticMemory(&HEAP_HINT, gTestMemory, sizeof(gTestMemory),
  714. WOLFMEM_GENERAL, 1) != 0) {
  715. printf("unable to load static memory.\n");
  716. return(EXIT_FAILURE);
  717. }
  718. #endif
  719. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
  720. wolfSSL_Debugging_ON();
  721. #endif
  722. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  723. wc_SetLoggingHeap(HEAP_HINT);
  724. #endif
  725. #if defined(HAVE_FIPS) && !defined(WOLFSSL_LINUXKM)
  726. wolfCrypt_SetCb_fips(myFipsCb);
  727. #endif
  728. #if !defined(NO_BIG_INT)
  729. if (CheckCtcSettings() != 1) {
  730. printf("Sizeof mismatch (build) %x != (run) %lx\n",
  731. CTC_SETTINGS, (unsigned long)CheckRunTimeSettings());
  732. return err_sys("Build vs runtime math mismatch\n", -1000);
  733. }
  734. #if defined(USE_FAST_MATH) && \
  735. (!defined(NO_RSA) || !defined(NO_DH) || defined(HAVE_ECC))
  736. if (CheckFastMathSettings() != 1)
  737. return err_sys("Build vs runtime fastmath FP_MAX_BITS mismatch\n",
  738. -1001);
  739. #endif /* USE_FAST_MATH */
  740. #endif /* !NO_BIG_INT */
  741. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  742. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  743. initDefaultName();
  744. #endif
  745. #ifdef WOLFSSL_ASYNC_CRYPT
  746. ret = wolfAsync_DevOpen(&devId);
  747. if (ret < 0) {
  748. printf("Async device open failed\nRunning without async\n");
  749. }
  750. #else
  751. (void)devId;
  752. #endif /* WOLFSSL_ASYNC_CRYPT */
  753. #ifdef WOLF_CRYPTO_CB
  754. #ifdef HAVE_INTEL_QA_SYNC
  755. devId = wc_CryptoCb_InitIntelQa();
  756. if (INVALID_DEVID == devId) {
  757. printf("Couldn't init the Intel QA\n");
  758. }
  759. #endif
  760. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  761. devId = wc_CryptoCb_InitOcteon();
  762. if (INVALID_DEVID == devId) {
  763. printf("Couldn't init the Cavium Octeon\n");
  764. }
  765. #endif
  766. #endif
  767. #ifdef HAVE_SELFTEST
  768. if ( (ret = wolfCrypt_SelfTest()) != 0)
  769. return err_sys("CAVP selftest failed!\n", ret);
  770. else
  771. TEST_PASS("CAVP selftest passed!\n");
  772. #endif
  773. if ( (ret = error_test()) != 0)
  774. return err_sys("error test failed!\n", ret);
  775. else
  776. TEST_PASS("error test passed!\n");
  777. if ( (ret = memory_test()) != 0)
  778. return err_sys("MEMORY test failed!\n", ret);
  779. else
  780. TEST_PASS("MEMORY test passed!\n");
  781. #ifndef NO_CODING
  782. if ( (ret = base64_test()) != 0)
  783. return err_sys("base64 test failed!\n", ret);
  784. else
  785. TEST_PASS("base64 test passed!\n");
  786. #ifdef WOLFSSL_BASE16
  787. if ( (ret = base16_test()) != 0)
  788. return err_sys("base16 test failed!\n", ret);
  789. else
  790. TEST_PASS("base16 test passed!\n");
  791. #endif
  792. #endif /* !NO_CODING */
  793. #ifndef NO_ASN
  794. if ( (ret = asn_test()) != 0)
  795. return err_sys("asn test failed!\n", ret);
  796. else
  797. TEST_PASS("asn test passed!\n");
  798. #endif
  799. #ifndef WC_NO_RNG
  800. if ( (ret = random_test()) != 0)
  801. return err_sys("RANDOM test failed!\n", ret);
  802. else
  803. TEST_PASS("RANDOM test passed!\n");
  804. #endif /* WC_NO_RNG */
  805. #ifndef NO_MD5
  806. if ( (ret = md5_test()) != 0)
  807. return err_sys("MD5 test failed!\n", ret);
  808. else
  809. TEST_PASS("MD5 test passed!\n");
  810. #endif
  811. #ifdef WOLFSSL_MD2
  812. if ( (ret = md2_test()) != 0)
  813. return err_sys("MD2 test failed!\n", ret);
  814. else
  815. TEST_PASS("MD2 test passed!\n");
  816. #endif
  817. #ifndef NO_MD4
  818. if ( (ret = md4_test()) != 0)
  819. return err_sys("MD4 test failed!\n", ret);
  820. else
  821. TEST_PASS("MD4 test passed!\n");
  822. #endif
  823. #ifndef NO_SHA
  824. if ( (ret = sha_test()) != 0)
  825. return err_sys("SHA test failed!\n", ret);
  826. else
  827. TEST_PASS("SHA test passed!\n");
  828. #endif
  829. #ifdef WOLFSSL_SHA224
  830. if ( (ret = sha224_test()) != 0)
  831. return err_sys("SHA-224 test failed!\n", ret);
  832. else
  833. TEST_PASS("SHA-224 test passed!\n");
  834. #endif
  835. #ifndef NO_SHA256
  836. if ( (ret = sha256_test()) != 0)
  837. return err_sys("SHA-256 test failed!\n", ret);
  838. else
  839. TEST_PASS("SHA-256 test passed!\n");
  840. #endif
  841. #ifdef WOLFSSL_SHA384
  842. if ( (ret = sha384_test()) != 0)
  843. return err_sys("SHA-384 test failed!\n", ret);
  844. else
  845. TEST_PASS("SHA-384 test passed!\n");
  846. #endif
  847. #ifdef WOLFSSL_SHA512
  848. if ( (ret = sha512_test()) != 0)
  849. return err_sys("SHA-512 test failed!\n", ret);
  850. else
  851. TEST_PASS("SHA-512 test passed!\n");
  852. #endif
  853. #ifdef WOLFSSL_SHA3
  854. if ( (ret = sha3_test()) != 0)
  855. return err_sys("SHA-3 test failed!\n", ret);
  856. else
  857. TEST_PASS("SHA-3 test passed!\n");
  858. #endif
  859. #ifdef WOLFSSL_SHAKE128
  860. if ( (ret = shake128_test()) != 0)
  861. return err_sys("SHAKE128 test failed!\n", ret);
  862. else
  863. TEST_PASS("SHAKE128 test passed!\n");
  864. #endif
  865. #ifdef WOLFSSL_SHAKE256
  866. if ( (ret = shake256_test()) != 0)
  867. return err_sys("SHAKE256 test failed!\n", ret);
  868. else
  869. TEST_PASS("SHAKE256 test passed!\n");
  870. #endif
  871. #ifndef NO_HASH_WRAPPER
  872. if ( (ret = hash_test()) != 0)
  873. return err_sys("Hash test failed!\n", ret);
  874. else
  875. TEST_PASS("Hash test passed!\n");
  876. #endif
  877. #ifdef WOLFSSL_RIPEMD
  878. if ( (ret = ripemd_test()) != 0)
  879. return err_sys("RIPEMD test failed!\n", ret);
  880. else
  881. TEST_PASS("RIPEMD test passed!\n");
  882. #endif
  883. #ifdef HAVE_BLAKE2
  884. if ( (ret = blake2b_test()) != 0)
  885. return err_sys("BLAKE2b test failed!\n", ret);
  886. else
  887. TEST_PASS("BLAKE2b test passed!\n");
  888. #endif
  889. #ifdef HAVE_BLAKE2S
  890. if ( (ret = blake2s_test()) != 0)
  891. return err_sys("BLAKE2s test failed!\n", ret);
  892. else
  893. TEST_PASS("BLAKE2s test passed!\n");
  894. #endif
  895. #ifndef NO_HMAC
  896. #if !defined(NO_MD5) && !(defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) \
  897. && (HAVE_FIPS_VERSION >= 5))
  898. if ( (ret = hmac_md5_test()) != 0)
  899. return err_sys("HMAC-MD5 test failed!\n", ret);
  900. else
  901. TEST_PASS("HMAC-MD5 test passed!\n");
  902. #endif
  903. #ifndef NO_SHA
  904. if ( (ret = hmac_sha_test()) != 0)
  905. return err_sys("HMAC-SHA test failed!\n", ret);
  906. else
  907. TEST_PASS("HMAC-SHA test passed!\n");
  908. #endif
  909. #ifdef WOLFSSL_SHA224
  910. if ( (ret = hmac_sha224_test()) != 0)
  911. return err_sys("HMAC-SHA224 test failed!\n", ret);
  912. else
  913. TEST_PASS("HMAC-SHA224 test passed!\n");
  914. #endif
  915. #ifndef NO_SHA256
  916. if ( (ret = hmac_sha256_test()) != 0)
  917. return err_sys("HMAC-SHA256 test failed!\n", ret);
  918. else
  919. TEST_PASS("HMAC-SHA256 test passed!\n");
  920. #endif
  921. #ifdef WOLFSSL_SHA384
  922. if ( (ret = hmac_sha384_test()) != 0)
  923. return err_sys("HMAC-SHA384 test failed!\n", ret);
  924. else
  925. TEST_PASS("HMAC-SHA384 test passed!\n");
  926. #endif
  927. #ifdef WOLFSSL_SHA512
  928. if ( (ret = hmac_sha512_test()) != 0)
  929. return err_sys("HMAC-SHA512 test failed!\n", ret);
  930. else
  931. TEST_PASS("HMAC-SHA512 test passed!\n");
  932. #endif
  933. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  934. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  935. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  936. if ( (ret = hmac_sha3_test()) != 0)
  937. return err_sys("HMAC-SHA3 test failed!\n", ret);
  938. else
  939. TEST_PASS("HMAC-SHA3 test passed!\n");
  940. #endif
  941. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  942. PRIVATE_KEY_UNLOCK();
  943. if ( (ret = hkdf_test()) != 0)
  944. return err_sys("HMAC-KDF test failed!\n", ret);
  945. else
  946. TEST_PASS("HMAC-KDF test passed!\n");
  947. PRIVATE_KEY_LOCK();
  948. #endif
  949. #endif /* !NO_HMAC */
  950. #ifdef WOLFSSL_WOLFSSH
  951. PRIVATE_KEY_UNLOCK();
  952. if ( (ret = sshkdf_test()) != 0)
  953. return err_sys("SSH-KDF test failed!\n", ret);
  954. else
  955. TEST_PASS("SSH-KDF test passed!\n");
  956. PRIVATE_KEY_LOCK();
  957. #endif /* WOLFSSL_WOLFSSH */
  958. #ifdef WOLFSSL_TLS13
  959. PRIVATE_KEY_UNLOCK();
  960. if ( (ret = tls13_kdf_test()) != 0)
  961. return err_sys("TLSv1.3 KDF test failed!\n", ret);
  962. else
  963. TEST_PASS("TLSv1.3 KDF test passed!\n");
  964. PRIVATE_KEY_LOCK();
  965. #endif /* WOLFSSL_TLS13 */
  966. #if defined(HAVE_X963_KDF) && defined(HAVE_ECC)
  967. if ( (ret = x963kdf_test()) != 0)
  968. return err_sys("X963-KDF test failed!\n", ret);
  969. else
  970. TEST_PASS("X963-KDF test passed!\n");
  971. #endif
  972. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128) && \
  973. !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  974. if ( (ret = gmac_test()) != 0)
  975. return err_sys("GMAC test failed!\n", ret);
  976. else
  977. TEST_PASS("GMAC test passed!\n");
  978. #endif
  979. #ifdef WC_RC2
  980. if ( (ret = rc2_test()) != 0)
  981. return err_sys("RC2 test failed!\n", ret);
  982. else
  983. TEST_PASS("RC2 test passed!\n");
  984. #endif
  985. #ifndef NO_RC4
  986. if ( (ret = arc4_test()) != 0)
  987. return err_sys("ARC4 test failed!\n", ret);
  988. else
  989. TEST_PASS("ARC4 test passed!\n");
  990. #endif
  991. #ifdef HAVE_CHACHA
  992. if ( (ret = chacha_test()) != 0)
  993. return err_sys("Chacha test failed!\n", ret);
  994. else
  995. TEST_PASS("Chacha test passed!\n");
  996. #endif
  997. #ifdef HAVE_XCHACHA
  998. if ( (ret = XChaCha_test()) != 0)
  999. return err_sys("XChacha test failed!\n", ret);
  1000. else
  1001. TEST_PASS("XChacha test passed!\n");
  1002. #endif
  1003. #ifdef HAVE_POLY1305
  1004. if ( (ret = poly1305_test()) != 0)
  1005. return err_sys("POLY1305 test failed!\n", ret);
  1006. else
  1007. TEST_PASS("POLY1305 test passed!\n");
  1008. #endif
  1009. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  1010. if ( (ret = chacha20_poly1305_aead_test()) != 0)
  1011. return err_sys("ChaCha20-Poly1305 AEAD test failed!\n", ret);
  1012. else
  1013. TEST_PASS("ChaCha20-Poly1305 AEAD test passed!\n");
  1014. #endif
  1015. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  1016. if ( (ret = XChaCha20Poly1305_test()) != 0)
  1017. return err_sys("XChaCha20-Poly1305 AEAD test failed!\n", ret);
  1018. else
  1019. TEST_PASS("XChaCha20-Poly1305 AEAD test passed!\n");
  1020. #endif
  1021. #ifndef NO_DES3
  1022. if ( (ret = des_test()) != 0)
  1023. return err_sys("DES test failed!\n", ret);
  1024. else
  1025. TEST_PASS("DES test passed!\n");
  1026. #endif
  1027. #ifndef NO_DES3
  1028. if ( (ret = des3_test()) != 0)
  1029. return err_sys("DES3 test failed!\n", ret);
  1030. else
  1031. TEST_PASS("DES3 test passed!\n");
  1032. #endif
  1033. #ifndef NO_AES
  1034. if ( (ret = aes_test()) != 0)
  1035. return err_sys("AES test failed!\n", ret);
  1036. else
  1037. TEST_PASS("AES test passed!\n");
  1038. #ifdef WOLFSSL_AES_192
  1039. if ( (ret = aes192_test()) != 0)
  1040. return err_sys("AES192 test failed!\n", ret);
  1041. else
  1042. TEST_PASS("AES192 test passed!\n");
  1043. #endif
  1044. #ifdef WOLFSSL_AES_256
  1045. if ( (ret = aes256_test()) != 0)
  1046. return err_sys("AES256 test failed!\n", ret);
  1047. else
  1048. TEST_PASS("AES256 test passed!\n");
  1049. #endif
  1050. #ifdef WOLFSSL_AES_OFB
  1051. if ( (ret = aesofb_test()) != 0)
  1052. return err_sys("AES-OFB test failed!\n", ret);
  1053. else
  1054. TEST_PASS("AESOFB test passed!\n");
  1055. #endif
  1056. #ifdef HAVE_AESGCM
  1057. #if !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO)
  1058. if ( (ret = aesgcm_test()) != 0)
  1059. return err_sys("AES-GCM test failed!\n", ret);
  1060. #endif
  1061. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT) && \
  1062. !defined(WOLFSSL_KCAPI_AES) && !(defined(WOLF_CRYPTO_CB) && \
  1063. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  1064. if ((ret = aesgcm_default_test()) != 0) {
  1065. return err_sys("AES-GCM test failed!\n", ret);
  1066. }
  1067. #endif
  1068. if (ret == 0) {
  1069. TEST_PASS("AES-GCM test passed!\n");
  1070. }
  1071. #endif
  1072. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  1073. if ( (ret = aesccm_test()) != 0)
  1074. return err_sys("AES-CCM test failed!\n", ret);
  1075. else
  1076. TEST_PASS("AES-CCM test passed!\n");
  1077. #endif
  1078. #ifdef HAVE_AES_KEYWRAP
  1079. if ( (ret = aeskeywrap_test()) != 0)
  1080. return err_sys("AES Key Wrap test failed!\n", ret);
  1081. else
  1082. TEST_PASS("AES Key Wrap test passed!\n");
  1083. #endif
  1084. #ifdef WOLFSSL_AES_SIV
  1085. if ( (ret = aes_siv_test()) != 0)
  1086. return err_sys("AES-SIV test failed!\n", ret);
  1087. else
  1088. TEST_PASS("AES-SIV test passed!\n");
  1089. #endif
  1090. #endif
  1091. #ifdef HAVE_CAMELLIA
  1092. if ( (ret = camellia_test()) != 0)
  1093. return err_sys("CAMELLIA test failed!\n", ret);
  1094. else
  1095. TEST_PASS("CAMELLIA test passed!\n");
  1096. #endif
  1097. #if !defined(NO_RSA)
  1098. #ifdef WC_RSA_NO_PADDING
  1099. if ( (ret = rsa_no_pad_test()) != 0)
  1100. return err_sys("RSA NOPAD test failed!\n", ret);
  1101. else
  1102. TEST_PASS("RSA NOPAD test passed!\n");
  1103. #endif
  1104. if ( (ret = rsa_test()) != 0)
  1105. return err_sys("RSA test failed!\n", ret);
  1106. else
  1107. TEST_PASS("RSA test passed!\n");
  1108. #endif
  1109. #ifndef NO_DH
  1110. PRIVATE_KEY_UNLOCK();
  1111. if ( (ret = dh_test()) != 0)
  1112. return err_sys("DH test failed!\n", ret);
  1113. else
  1114. TEST_PASS("DH test passed!\n");
  1115. PRIVATE_KEY_LOCK();
  1116. #endif
  1117. #ifndef NO_DSA
  1118. if ( (ret = dsa_test()) != 0)
  1119. return err_sys("DSA test failed!\n", ret);
  1120. else
  1121. TEST_PASS("DSA test passed!\n");
  1122. #endif
  1123. #ifdef WOLFCRYPT_HAVE_SRP
  1124. if ( (ret = srp_test()) != 0)
  1125. return err_sys("SRP test failed!\n", ret);
  1126. else
  1127. TEST_PASS("SRP test passed!\n");
  1128. #endif
  1129. #ifndef NO_PWDBASED
  1130. if ( (ret = pwdbased_test()) != 0)
  1131. return err_sys("PWDBASED test failed!\n", ret);
  1132. else
  1133. TEST_PASS("PWDBASED test passed!\n");
  1134. #endif
  1135. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  1136. if ( (ret = openssl_test()) != 0)
  1137. return err_sys("OPENSSL test failed!\n", ret);
  1138. else
  1139. TEST_PASS("OPENSSL test passed!\n");
  1140. if ( (ret = openSSL_evpMD_test()) != 0)
  1141. return err_sys("OPENSSL (EVP MD) test failed!\n", ret);
  1142. else
  1143. TEST_PASS("OPENSSL (EVP MD) passed!\n");
  1144. if ( (ret = openssl_pkey0_test()) != 0)
  1145. return err_sys("OPENSSL (PKEY0) test failed!\n", ret);
  1146. else
  1147. TEST_PASS("OPENSSL (PKEY0) passed!\n");
  1148. if ( (ret = openssl_pkey1_test()) != 0)
  1149. return err_sys("OPENSSL (PKEY1) test failed!\n", ret);
  1150. else
  1151. TEST_PASS("OPENSSL (PKEY1) passed!\n");
  1152. #if !defined(WOLF_CRYPTO_CB_ONLY_RSA) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  1153. if ( (ret = openssl_evpSig_test()) != 0)
  1154. return err_sys("OPENSSL (EVP Sign/Verify) test failed!\n", ret);
  1155. else
  1156. TEST_PASS("OPENSSL (EVP Sign/Verify) passed!\n");
  1157. #endif
  1158. #endif
  1159. #if defined(HAVE_ECC)
  1160. PRIVATE_KEY_UNLOCK();
  1161. if ( (ret = ecc_test()) != 0)
  1162. return err_sys("ECC test failed!\n", ret);
  1163. else
  1164. TEST_PASS("ECC test passed!\n");
  1165. PRIVATE_KEY_LOCK();
  1166. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  1167. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  1168. if ( (ret = ecc_encrypt_test()) != 0)
  1169. return err_sys("ECC Enc test failed!\n", ret);
  1170. else
  1171. TEST_PASS("ECC Enc test passed!\n");
  1172. #endif
  1173. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  1174. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  1175. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  1176. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  1177. /* skip for ATECC508/608A, cannot import private key buffers */
  1178. if ( (ret = ecc_test_buffers()) != 0)
  1179. return err_sys("ECC buffer test failed!\n", ret);
  1180. else
  1181. TEST_PASS("ECC buffer test passed!\n");
  1182. #endif
  1183. #endif
  1184. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  1185. !defined(NO_FILESYSTEM)
  1186. if ( (ret = cert_test()) != 0)
  1187. return err_sys("CERT test failed!\n", ret);
  1188. else
  1189. TEST_PASS("CERT test passed!\n");
  1190. #endif
  1191. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  1192. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(WOLFSSL_GEN_CERT)
  1193. if ( (ret = certext_test()) != 0)
  1194. return err_sys("CERT EXT test failed!\n", ret);
  1195. else
  1196. TEST_PASS("CERT EXT test passed!\n");
  1197. #endif
  1198. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  1199. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  1200. if ( (ret = decodedCertCache_test()) != 0)
  1201. return err_sys("DECODED CERT CACHE test failed!\n", ret);
  1202. else
  1203. TEST_PASS("DECODED CERT CACHE test passed!\n");
  1204. #endif
  1205. #ifdef HAVE_CURVE25519
  1206. if ( (ret = curve25519_test()) != 0)
  1207. return err_sys("CURVE25519 test failed!\n", ret);
  1208. else
  1209. TEST_PASS("CURVE25519 test passed!\n");
  1210. #endif
  1211. #ifdef HAVE_ED25519
  1212. if ( (ret = ed25519_test()) != 0)
  1213. return err_sys("ED25519 test failed!\n", ret);
  1214. else
  1215. TEST_PASS("ED25519 test passed!\n");
  1216. #endif
  1217. #ifdef HAVE_CURVE448
  1218. if ( (ret = curve448_test()) != 0)
  1219. return err_sys("CURVE448 test failed!\n", ret);
  1220. else
  1221. TEST_PASS("CURVE448 test passed!\n");
  1222. #endif
  1223. #ifdef HAVE_ED448
  1224. if ( (ret = ed448_test()) != 0)
  1225. return err_sys("ED448 test failed!\n", ret);
  1226. else
  1227. TEST_PASS("ED448 test passed!\n");
  1228. #endif
  1229. #ifdef WOLFSSL_HAVE_KYBER
  1230. if ( (ret = kyber_test()) != 0)
  1231. return err_sys("KYBER test failed!\n", ret);
  1232. else
  1233. TEST_PASS("KYBER test passed!\n");
  1234. #endif
  1235. #ifdef WOLFCRYPT_HAVE_ECCSI
  1236. if ( (ret = eccsi_test()) != 0)
  1237. return err_sys("ECCSI test failed!\n", ret);
  1238. else
  1239. TEST_PASS("ECCSI test passed!\n");
  1240. #endif
  1241. #ifdef WOLFCRYPT_HAVE_SAKKE
  1242. if ( (ret = sakke_test()) != 0)
  1243. return err_sys("SAKKE test failed!\n", ret);
  1244. else
  1245. TEST_PASS("SAKKE test passed!\n");
  1246. #endif
  1247. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  1248. if ( (ret = cmac_test()) != 0)
  1249. return err_sys("CMAC test failed!\n", ret);
  1250. else
  1251. TEST_PASS("CMAC test passed!\n");
  1252. #endif
  1253. #if defined(WOLFSSL_SIPHASH)
  1254. if ( (ret = siphash_test()) != 0)
  1255. return err_sys("SipHash test failed!\n", ret);
  1256. else
  1257. TEST_PASS("SipHash test passed!\n");
  1258. #endif
  1259. #ifdef HAVE_LIBZ
  1260. if ( (ret = compress_test()) != 0)
  1261. return err_sys("COMPRESS test failed!\n", ret);
  1262. else
  1263. TEST_PASS("COMPRESS test passed!\n");
  1264. #endif
  1265. #ifdef HAVE_PKCS7
  1266. #ifndef NO_PKCS7_ENCRYPTED_DATA
  1267. if ( (ret = pkcs7encrypted_test()) != 0)
  1268. return err_sys("PKCS7encrypted test failed!\n", ret);
  1269. else
  1270. TEST_PASS("PKCS7encrypted test passed!\n");
  1271. #endif
  1272. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  1273. if ( (ret = pkcs7compressed_test()) != 0)
  1274. return err_sys("PKCS7compressed test failed!\n", ret);
  1275. else
  1276. TEST_PASS("PKCS7compressed test passed!\n");
  1277. #endif
  1278. if ( (ret = pkcs7signed_test()) != 0)
  1279. return err_sys("PKCS7signed test failed!\n", ret);
  1280. else
  1281. TEST_PASS("PKCS7signed test passed!\n");
  1282. if ( (ret = pkcs7enveloped_test()) != 0)
  1283. return err_sys("PKCS7enveloped test failed!\n", ret);
  1284. else
  1285. TEST_PASS("PKCS7enveloped test passed!\n");
  1286. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  1287. if ( (ret = pkcs7authenveloped_test()) != 0)
  1288. return err_sys("PKCS7authenveloped test failed!\n", ret);
  1289. else
  1290. TEST_PASS("PKCS7authenveloped test passed!\n");
  1291. #endif
  1292. #endif
  1293. #ifdef HAVE_VALGRIND
  1294. if ( (ret = mp_test()) != 0)
  1295. return err_sys("mp test failed!\n", ret);
  1296. else
  1297. TEST_PASS("mp test passed!\n");
  1298. #endif
  1299. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  1300. if ( (ret = prime_test()) != 0)
  1301. return err_sys("prime test failed!\n", ret);
  1302. else
  1303. TEST_PASS("prime test passed!\n");
  1304. #endif
  1305. #if defined(ASN_BER_TO_DER) && \
  1306. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  1307. defined(OPENSSL_EXTRA_X509_SMALL))
  1308. if ( (ret = berder_test()) != 0)
  1309. return err_sys("ber-der test failed!\n", ret);
  1310. else
  1311. TEST_PASS("ber-der test passed!\n");
  1312. #endif
  1313. if ( (ret = logging_test()) != 0)
  1314. return err_sys("logging test failed!\n", ret);
  1315. else
  1316. TEST_PASS("logging test passed!\n");
  1317. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  1318. if ( (ret = time_test()) != 0)
  1319. return err_sys("time test failed!\n", ret);
  1320. else
  1321. TEST_PASS("time test passed!\n");
  1322. #endif
  1323. if ( (ret = mutex_test()) != 0)
  1324. return err_sys("mutex test failed!\n", ret);
  1325. else
  1326. TEST_PASS("mutex test passed!\n");
  1327. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  1328. if ( (ret = memcb_test()) != 0)
  1329. return err_sys("memcb test failed!\n", ret);
  1330. else
  1331. TEST_PASS("memcb test passed!\n");
  1332. #endif
  1333. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  1334. if ( (ret = blob_test()) != 0)
  1335. return err_sys("blob test failed!\n", ret);
  1336. else
  1337. TEST_PASS("blob test passed!\n");
  1338. #endif
  1339. #if defined(WOLF_CRYPTO_CB) && \
  1340. !(defined(HAVE_INTEL_QAT_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC) || \
  1341. defined(WOLFSSL_QNX_CAAM))
  1342. if ( (ret = cryptocb_test()) != 0)
  1343. return err_sys("crypto callback test failed!\n", ret);
  1344. else
  1345. TEST_PASS("crypto callback test passed!\n");
  1346. #endif
  1347. #ifdef WOLFSSL_CERT_PIV
  1348. if ( (ret = certpiv_test()) != 0)
  1349. return err_sys("cert piv test failed!\n", ret);
  1350. else
  1351. TEST_PASS("cert piv test passed!\n");
  1352. #endif
  1353. #ifdef WOLF_CRYPTO_CB
  1354. #ifdef HAVE_INTEL_QA_SYNC
  1355. wc_CryptoCb_CleanupIntelQa(&devId);
  1356. #endif
  1357. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  1358. wc_CryptoCb_CleanupOcteon(&devId);
  1359. #endif
  1360. #endif
  1361. #ifdef WOLFSSL_ASYNC_CRYPT
  1362. wolfAsync_DevClose(&devId);
  1363. #endif
  1364. /* cleanup the thread if fixed point cache is enabled and have thread local */
  1365. #if defined(HAVE_THREAD_LS) && defined(HAVE_ECC) && defined(FP_ECC)
  1366. wc_ecc_fp_free();
  1367. #endif
  1368. if (args)
  1369. ((func_args*)args)->return_code = ret;
  1370. TEST_PASS("Test complete\n");
  1371. EXIT_TEST(ret);
  1372. }
  1373. #ifndef NO_MAIN_DRIVER
  1374. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  1375. int myoptind = 0;
  1376. char* myoptarg = NULL;
  1377. #endif
  1378. /* so overall tests can pull in test function */
  1379. #if defined(WOLFSSL_ESPIDF) || defined(_WIN32_WCE)
  1380. int wolf_test_task(void)
  1381. #else
  1382. #ifndef NO_MAIN_FUNCTION
  1383. int main(int argc, char** argv)
  1384. {
  1385. return wolfcrypt_test_main(argc, argv);
  1386. }
  1387. #endif
  1388. int wolfcrypt_test_main(int argc, char** argv)
  1389. #endif
  1390. {
  1391. int ret;
  1392. func_args args;
  1393. #if defined(WOLFSSL_ESPIDF) || defined(WOLFSSL_SE050)
  1394. /* set dummy wallclock time. */
  1395. struct timeval utctime;
  1396. struct timezone tz;
  1397. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1398. utctime.tv_usec = 0;
  1399. tz.tz_minuteswest = 0;
  1400. tz.tz_dsttime = 0;
  1401. settimeofday(&utctime, &tz);
  1402. #endif
  1403. #ifdef WOLFSSL_APACHE_MYNEWT
  1404. #ifdef ARCH_sim
  1405. mcu_sim_parse_args(argc, argv);
  1406. #endif
  1407. sysinit();
  1408. /* set dummy wallclock time. */
  1409. struct os_timeval utctime;
  1410. struct os_timezone tz;
  1411. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1412. utctime.tv_usec = 0;
  1413. tz.tz_minuteswest = 0;
  1414. tz.tz_dsttime = 0;
  1415. os_settimeofday(&utctime, &tz);
  1416. #endif
  1417. #ifdef WOLFSSL_ZEPHYR
  1418. /* set dummy wallclock time. */
  1419. struct timespec utctime;
  1420. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1421. utctime.tv_nsec = 0;
  1422. clock_settime(CLOCK_REALTIME, &utctime);
  1423. #endif
  1424. #ifdef DEVKITPRO
  1425. void *framebuffer;
  1426. GXRModeObj *rmode = NULL;
  1427. VIDEO_Init();
  1428. WPAD_Init();
  1429. rmode = VIDEO_GetPreferredMode(NULL);
  1430. #pragma GCC diagnostic ignored "-Wbad-function-cast"
  1431. framebuffer = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
  1432. #pragma GCC diagnostic pop
  1433. console_init(framebuffer,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
  1434. VIDEO_Configure(rmode);
  1435. VIDEO_SetNextFramebuffer(framebuffer);
  1436. VIDEO_SetBlack(FALSE);
  1437. VIDEO_Flush();
  1438. VIDEO_WaitVSync();
  1439. if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
  1440. #endif
  1441. #ifdef HAVE_WNR
  1442. if (wc_InitNetRandom(wnrConfigFile, NULL, 5000) != 0) {
  1443. err_sys("Whitewood netRandom global config failed", -1001);
  1444. return -1002;
  1445. }
  1446. #endif
  1447. #ifndef WOLFSSL_ESPIDF
  1448. args.argc = argc;
  1449. args.argv = argv;
  1450. #endif
  1451. if ((ret = wolfCrypt_Init()) != 0) {
  1452. printf("wolfCrypt_Init failed %d\n", ret);
  1453. err_sys("Error with wolfCrypt_Init!\n", -1003);
  1454. }
  1455. #ifdef WC_RNG_SEED_CB
  1456. wc_SetSeed_Cb(wc_GenerateSeed);
  1457. #endif
  1458. #ifdef HAVE_STACK_SIZE
  1459. StackSizeCheck(&args, wolfcrypt_test);
  1460. #else
  1461. wolfcrypt_test(&args);
  1462. #endif
  1463. if ((ret = wolfCrypt_Cleanup()) != 0) {
  1464. printf("wolfCrypt_Cleanup failed %d\n", ret);
  1465. err_sys("Error with wolfCrypt_Cleanup!\n", -1004);
  1466. }
  1467. #ifdef HAVE_WNR
  1468. if (wc_FreeNetRandom() < 0)
  1469. err_sys("Failed to free netRandom context", -1005);
  1470. #endif /* HAVE_WNR */
  1471. #ifdef DOLPHIN_EMULATOR
  1472. /* Returning from main panics the emulator. Just hang
  1473. * and let the user force quit the emulator window. */
  1474. printf("args.return_code: %d\n", args.return_code);
  1475. printf("Testing complete. You may close the window now\n");
  1476. while (1);
  1477. #endif
  1478. #ifdef WOLFSSL_ESPIDF
  1479. /* ESP_LOGI to print takes up a lot less memory than printf */
  1480. ESP_LOGI("wolfcrypt_test", "Exiting main with return code: % d\n", args.return_code);
  1481. #endif
  1482. /* everything else will use printf */
  1483. #if !defined(WOLFSSL_ESPIDF)
  1484. /* gate this for target platforms wishing to avoid printf reference */
  1485. printf("Exiting main with return code: %d\n", args.return_code);
  1486. #endif
  1487. return args.return_code;
  1488. } /* wolfcrypt_test_main or wolf_test_task */
  1489. #endif /* NO_MAIN_DRIVER */
  1490. /* helper to save DER, convert to PEM and save PEM */
  1491. #if !defined(NO_ASN) && (defined(HAVE_ECC) || !defined(NO_DSA) || \
  1492. (!defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)))) \
  1493. && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  1494. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1495. #define SaveDerAndPem(d, dSz, fD, fP, pT, eB) _SaveDerAndPem(d, dSz, fD, fP, pT, eB)
  1496. #else
  1497. #define SaveDerAndPem(d, dSz, fD, fP, pT, eB) _SaveDerAndPem(d, dSz, NULL, NULL, pT, eB)
  1498. #endif
  1499. static int _SaveDerAndPem(const byte* der, int derSz,
  1500. const char* fileDer, const char* filePem, int pemType, int errBase)
  1501. {
  1502. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1503. int ret;
  1504. XFILE derFile;
  1505. derFile = XFOPEN(fileDer, "wb");
  1506. if (!derFile) {
  1507. return errBase + 0;
  1508. }
  1509. ret = (int)XFWRITE(der, 1, derSz, derFile);
  1510. XFCLOSE(derFile);
  1511. if (ret != derSz) {
  1512. return errBase + 1;
  1513. }
  1514. #endif
  1515. #ifdef WOLFSSL_DER_TO_PEM
  1516. if (filePem) {
  1517. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1518. XFILE pemFile;
  1519. #endif
  1520. byte* pem;
  1521. int pemSz;
  1522. /* calculate PEM size */
  1523. pemSz = wc_DerToPem(der, derSz, NULL, 0, pemType);
  1524. if (pemSz < 0) {
  1525. return pemSz;
  1526. }
  1527. pem = (byte*)XMALLOC(pemSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1528. if (pem == NULL) {
  1529. return MEMORY_E;
  1530. }
  1531. /* Convert to PEM */
  1532. pemSz = wc_DerToPem(der, derSz, pem, pemSz, pemType);
  1533. if (pemSz < 0) {
  1534. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1535. return errBase + 2;
  1536. }
  1537. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1538. pemFile = XFOPEN(filePem, "wb");
  1539. if (!pemFile) {
  1540. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1541. return errBase + 3;
  1542. }
  1543. ret = (int)XFWRITE(pem, 1, pemSz, pemFile);
  1544. XFCLOSE(pemFile);
  1545. if (ret != pemSz) {
  1546. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1547. return errBase + 4;
  1548. }
  1549. #endif
  1550. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1551. }
  1552. #endif /* WOLFSSL_DER_TO_PEM */
  1553. /* suppress unused variable warnings */
  1554. (void)der;
  1555. (void)derSz;
  1556. (void)filePem;
  1557. (void)fileDer;
  1558. (void)pemType;
  1559. (void)errBase;
  1560. return 0;
  1561. }
  1562. #endif /* WOLFSSL_KEY_GEN || WOLFSSL_CERT_GEN */
  1563. WOLFSSL_TEST_SUBROUTINE int error_test(void)
  1564. {
  1565. const char* errStr;
  1566. char out[WOLFSSL_MAX_ERROR_SZ];
  1567. const char* unknownStr = wc_GetErrorString(0);
  1568. #ifdef NO_ERROR_STRINGS
  1569. /* Ensure a valid error code's string matches an invalid code's.
  1570. * The string is that error strings are not available.
  1571. */
  1572. errStr = wc_GetErrorString(OPEN_RAN_E);
  1573. wc_ErrorString(OPEN_RAN_E, out);
  1574. if (XSTRCMP(errStr, unknownStr) != 0)
  1575. return -1100;
  1576. if (XSTRCMP(out, unknownStr) != 0)
  1577. return -1101;
  1578. #else
  1579. int i;
  1580. int j = 0;
  1581. /* Values that are not or no longer error codes. */
  1582. int missing[] = { -122, -123, -124, -127, -128, -129, -159,
  1583. -163, -164, -165, -166, -167, -168, -169, -233,
  1584. 0 };
  1585. /* Check that all errors have a string and it's the same through the two
  1586. * APIs. Check that the values that are not errors map to the unknown
  1587. * string.
  1588. */
  1589. for (i = MAX_CODE_E-1; i >= WC_LAST_E; i--) {
  1590. errStr = wc_GetErrorString(i);
  1591. wc_ErrorString(i, out);
  1592. if (i != missing[j]) {
  1593. if (XSTRCMP(errStr, unknownStr) == 0)
  1594. return -1102;
  1595. if (XSTRCMP(out, unknownStr) == 0)
  1596. return -1103;
  1597. if (XSTRCMP(errStr, out) != 0)
  1598. return -1104;
  1599. if (XSTRLEN(errStr) >= WOLFSSL_MAX_ERROR_SZ)
  1600. return -1105;
  1601. }
  1602. else {
  1603. j++;
  1604. if (XSTRCMP(errStr, unknownStr) != 0)
  1605. return -1106;
  1606. if (XSTRCMP(out, unknownStr) != 0)
  1607. return -1107;
  1608. }
  1609. }
  1610. /* Check if the next possible value has been given a string. */
  1611. errStr = wc_GetErrorString(i);
  1612. wc_ErrorString(i, out);
  1613. if (XSTRCMP(errStr, unknownStr) != 0)
  1614. return -1108;
  1615. if (XSTRCMP(out, unknownStr) != 0)
  1616. return -1109;
  1617. #endif
  1618. return 0;
  1619. }
  1620. #ifndef NO_CODING
  1621. WOLFSSL_TEST_SUBROUTINE int base64_test(void)
  1622. {
  1623. int ret;
  1624. WOLFSSL_SMALL_STACK_STATIC const byte good[] = "A+Gd\0\0\0";
  1625. WOLFSSL_SMALL_STACK_STATIC const byte goodEnd[] = "A+Gd \r\n";
  1626. WOLFSSL_SMALL_STACK_STATIC const byte good_spaces[] = " A + G d \0";
  1627. byte out[128];
  1628. word32 outLen;
  1629. #ifdef WOLFSSL_BASE64_ENCODE
  1630. byte data[3];
  1631. word32 dataLen;
  1632. byte longData[79] = { 0 };
  1633. WOLFSSL_SMALL_STACK_STATIC const byte symbols[] = "+/A=";
  1634. #endif
  1635. WOLFSSL_SMALL_STACK_STATIC const byte badSmall[] = "AAA!Gdj=";
  1636. WOLFSSL_SMALL_STACK_STATIC const byte badLarge[] = "AAA~Gdj=";
  1637. WOLFSSL_SMALL_STACK_STATIC const byte badEOL[] = "A+Gd!AA";
  1638. WOLFSSL_SMALL_STACK_STATIC const byte badPadding[] = "AA=A";
  1639. WOLFSSL_SMALL_STACK_STATIC const byte badChar[] = ",-.:;<=>?@[\\]^_`";
  1640. byte goodChar[] =
  1641. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1642. "abcdefghijklmnopqrstuvwxyz"
  1643. "0123456789+/;";
  1644. byte charTest[] = "A+Gd\0\0\0";
  1645. int i;
  1646. /* Good Base64 encodings. */
  1647. outLen = sizeof(out);
  1648. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  1649. if (ret != 0)
  1650. return -1200;
  1651. outLen = sizeof(out);
  1652. ret = Base64_Decode(goodEnd, sizeof(goodEnd), out, &outLen);
  1653. if (ret != 0)
  1654. return -1201;
  1655. outLen = sizeof(goodChar);
  1656. ret = Base64_Decode(goodChar, sizeof(goodChar), goodChar, &outLen);
  1657. if (ret != 0)
  1658. return -1235;
  1659. if (outLen != 64 / 4 * 3)
  1660. return -1236;
  1661. outLen = sizeof(out);
  1662. ret = Base64_Decode(good_spaces, sizeof(good_spaces), out, &outLen);
  1663. if (ret != 0)
  1664. return -1201;
  1665. /* Bad parameters. */
  1666. outLen = 1;
  1667. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  1668. if (ret != BAD_FUNC_ARG)
  1669. return -1202;
  1670. outLen = sizeof(out);
  1671. ret = Base64_Decode(badEOL, sizeof(badEOL), out, &outLen);
  1672. if (ret != ASN_INPUT_E)
  1673. return -1203;
  1674. outLen = sizeof(out);
  1675. ret = Base64_Decode(badPadding, sizeof(badPadding), out, &outLen);
  1676. if (ret != ASN_INPUT_E)
  1677. return -1203;
  1678. /* Bad character at each offset 0-3. */
  1679. for (i = 0; i < 4; i++) {
  1680. outLen = sizeof(out);
  1681. ret = Base64_Decode(badSmall + i, 4, out, &outLen);
  1682. if (ret != ASN_INPUT_E)
  1683. return -1204 - i;
  1684. ret = Base64_Decode(badLarge + i, 4, out, &outLen);
  1685. if (ret != ASN_INPUT_E)
  1686. return -1214 - i;
  1687. }
  1688. /* Invalid character less than 0x2b */
  1689. for (i = 1; i < 0x2b; i++) {
  1690. outLen = sizeof(out);
  1691. charTest[0] = (byte)i;
  1692. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1693. if (ret != ASN_INPUT_E)
  1694. return -1240 - i;
  1695. }
  1696. /* Bad characters in range 0x2b - 0x7a. */
  1697. for (i = 0; i < (int)sizeof(badChar) - 1; i++) {
  1698. outLen = sizeof(out);
  1699. charTest[0] = badChar[i];
  1700. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1701. if (ret != ASN_INPUT_E)
  1702. return -1270 - i;
  1703. }
  1704. /* Invalid character greater than 0x7a */
  1705. for (i = 0x7b; i < 0x100; i++) {
  1706. outLen = sizeof(out);
  1707. charTest[0] = (byte)i;
  1708. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1709. if (ret != ASN_INPUT_E)
  1710. return -1290 - i;
  1711. }
  1712. #ifdef WOLFSSL_BASE64_ENCODE
  1713. /* Decode and encode all symbols - non-alphanumeric. */
  1714. dataLen = sizeof(data);
  1715. ret = Base64_Decode(symbols, sizeof(symbols), data, &dataLen);
  1716. if (ret != 0)
  1717. return -1224;
  1718. outLen = sizeof(out);
  1719. ret = Base64_Encode(data, dataLen, NULL, &outLen);
  1720. if (ret != LENGTH_ONLY_E)
  1721. return -1225;
  1722. outLen = sizeof(out);
  1723. ret = Base64_Encode(data, dataLen, out, &outLen);
  1724. if (ret != 0)
  1725. return -1226;
  1726. outLen = 7;
  1727. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  1728. if (ret != BUFFER_E)
  1729. return -1227;
  1730. outLen = sizeof(out);
  1731. ret = Base64_EncodeEsc(data, dataLen, NULL, &outLen);
  1732. if (ret != LENGTH_ONLY_E)
  1733. return -1228;
  1734. outLen = sizeof(out);
  1735. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  1736. if (ret != 0)
  1737. return -1229;
  1738. outLen = sizeof(out);
  1739. ret = Base64_Encode_NoNl(data, dataLen, out, &outLen);
  1740. if (ret != 0)
  1741. return -1230;
  1742. /* Data that results in an encoding longer than one line. */
  1743. outLen = sizeof(out);
  1744. dataLen = sizeof(longData);
  1745. ret = Base64_Encode(longData, dataLen, out, &outLen);
  1746. if (ret != 0)
  1747. return -1231;
  1748. outLen = sizeof(out);
  1749. ret = Base64_EncodeEsc(longData, dataLen, out, &outLen);
  1750. if (ret != 0)
  1751. return -1232;
  1752. outLen = sizeof(out);
  1753. ret = Base64_Encode_NoNl(longData, dataLen, out, &outLen);
  1754. if (ret != 0)
  1755. return -1233;
  1756. #endif
  1757. return 0;
  1758. }
  1759. #ifdef WOLFSSL_BASE16
  1760. WOLFSSL_TEST_SUBROUTINE int base16_test(void)
  1761. {
  1762. int ret;
  1763. WOLFSSL_SMALL_STACK_STATIC const byte testData[] = "SomeDataToEncode\n";
  1764. WOLFSSL_SMALL_STACK_STATIC const byte encodedTestData[] = "536F6D6544617461546F456E636F64650A00";
  1765. byte encoded[40];
  1766. word32 encodedLen;
  1767. byte plain[40];
  1768. word32 len;
  1769. /* length returned includes null termination */
  1770. encodedLen = sizeof(encoded);
  1771. ret = Base16_Encode(testData, sizeof(testData), encoded, &encodedLen);
  1772. if (ret != 0)
  1773. return -1300;
  1774. len = (word32)XSTRLEN((char*)encoded);
  1775. if (len != encodedLen - 1)
  1776. return -1301;
  1777. len = sizeof(plain);
  1778. ret = Base16_Decode(encoded, encodedLen - 1, plain, &len);
  1779. if (ret != 0)
  1780. return -1302;
  1781. if (len != sizeof(testData) || XMEMCMP(testData, plain, len) != 0)
  1782. return -1303;
  1783. if (encodedLen != sizeof(encodedTestData) ||
  1784. XMEMCMP(encoded, encodedTestData, encodedLen) != 0) {
  1785. return -1304;
  1786. }
  1787. return 0;
  1788. }
  1789. #endif /* WOLFSSL_BASE16 */
  1790. #endif /* !NO_CODING */
  1791. #ifndef NO_ASN
  1792. WOLFSSL_TEST_SUBROUTINE int asn_test(void)
  1793. {
  1794. int ret;
  1795. /* ASN1 encoded date buffer */
  1796. WOLFSSL_SMALL_STACK_STATIC const byte dateBuf[] = {0x17, 0x0d, 0x31, 0x36, 0x30, 0x38, 0x31, 0x31,
  1797. 0x32, 0x30, 0x30, 0x37, 0x33, 0x37, 0x5a};
  1798. byte format;
  1799. int length;
  1800. const byte* datePart;
  1801. #ifndef NO_ASN_TIME
  1802. struct tm timearg;
  1803. time_t now;
  1804. #endif
  1805. ret = wc_GetDateInfo(dateBuf, (int)sizeof(dateBuf), &datePart, &format,
  1806. &length);
  1807. if (ret != 0)
  1808. return -1400;
  1809. #ifndef NO_ASN_TIME
  1810. /* Parameter Validation tests. */
  1811. if (wc_GetTime(NULL, sizeof(now)) != BAD_FUNC_ARG)
  1812. return -1401;
  1813. if (wc_GetTime(&now, 0) != BUFFER_E)
  1814. return -1402;
  1815. now = 0;
  1816. if (wc_GetTime(&now, sizeof(now)) != 0) {
  1817. return -1403;
  1818. }
  1819. if (now == 0) {
  1820. printf("RTC/Time not set!\n");
  1821. return -1404;
  1822. }
  1823. ret = wc_GetDateAsCalendarTime(datePart, length, format, &timearg);
  1824. if (ret != 0)
  1825. return -1405;
  1826. #endif /* !NO_ASN_TIME */
  1827. return 0;
  1828. }
  1829. #endif /* !NO_ASN */
  1830. #ifdef WOLFSSL_MD2
  1831. WOLFSSL_TEST_SUBROUTINE int md2_test(void)
  1832. {
  1833. int ret = 0;
  1834. Md2 md2;
  1835. byte hash[MD2_DIGEST_SIZE];
  1836. testVector a, b, c, d, e, f, g;
  1837. testVector test_md2[7];
  1838. int times = sizeof(test_md2) / sizeof(testVector), i;
  1839. a.input = "";
  1840. a.output = "\x83\x50\xe5\xa3\xe2\x4c\x15\x3d\xf2\x27\x5c\x9f\x80\x69"
  1841. "\x27\x73";
  1842. a.inLen = XSTRLEN(a.input);
  1843. a.outLen = MD2_DIGEST_SIZE;
  1844. b.input = "a";
  1845. b.output = "\x32\xec\x01\xec\x4a\x6d\xac\x72\xc0\xab\x96\xfb\x34\xc0"
  1846. "\xb5\xd1";
  1847. b.inLen = XSTRLEN(b.input);
  1848. b.outLen = MD2_DIGEST_SIZE;
  1849. c.input = "abc";
  1850. c.output = "\xda\x85\x3b\x0d\x3f\x88\xd9\x9b\x30\x28\x3a\x69\xe6\xde"
  1851. "\xd6\xbb";
  1852. c.inLen = XSTRLEN(c.input);
  1853. c.outLen = MD2_DIGEST_SIZE;
  1854. d.input = "message digest";
  1855. d.output = "\xab\x4f\x49\x6b\xfb\x2a\x53\x0b\x21\x9f\xf3\x30\x31\xfe"
  1856. "\x06\xb0";
  1857. d.inLen = XSTRLEN(d.input);
  1858. d.outLen = MD2_DIGEST_SIZE;
  1859. e.input = "abcdefghijklmnopqrstuvwxyz";
  1860. e.output = "\x4e\x8d\xdf\xf3\x65\x02\x92\xab\x5a\x41\x08\xc3\xaa\x47"
  1861. "\x94\x0b";
  1862. e.inLen = XSTRLEN(e.input);
  1863. e.outLen = MD2_DIGEST_SIZE;
  1864. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  1865. "6789";
  1866. f.output = "\xda\x33\xde\xf2\xa4\x2d\xf1\x39\x75\x35\x28\x46\xc3\x03"
  1867. "\x38\xcd";
  1868. f.inLen = XSTRLEN(f.input);
  1869. f.outLen = MD2_DIGEST_SIZE;
  1870. g.input = "1234567890123456789012345678901234567890123456789012345678"
  1871. "9012345678901234567890";
  1872. g.output = "\xd5\x97\x6f\x79\xd8\x3d\x3a\x0d\xc9\x80\x6c\x3c\x66\xf3"
  1873. "\xef\xd8";
  1874. g.inLen = XSTRLEN(g.input);
  1875. g.outLen = MD2_DIGEST_SIZE;
  1876. test_md2[0] = a;
  1877. test_md2[1] = b;
  1878. test_md2[2] = c;
  1879. test_md2[3] = d;
  1880. test_md2[4] = e;
  1881. test_md2[5] = f;
  1882. test_md2[6] = g;
  1883. wc_InitMd2(&md2);
  1884. for (i = 0; i < times; ++i) {
  1885. wc_Md2Update(&md2, (byte*)test_md2[i].input, (word32)test_md2[i].inLen);
  1886. wc_Md2Final(&md2, hash);
  1887. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0)
  1888. return -1500 - i;
  1889. }
  1890. for (i = 0; i < times; ++i) {
  1891. ret = wc_Md2Hash((byte*)test_md2[i].input, (word32)test_md2[i].inLen, hash);
  1892. if (ret != 0) {
  1893. return -1507 - i;
  1894. }
  1895. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0) {
  1896. return -1507 - i;
  1897. }
  1898. }
  1899. return 0;
  1900. }
  1901. #endif
  1902. #ifndef NO_MD5
  1903. WOLFSSL_TEST_SUBROUTINE int md5_test(void)
  1904. {
  1905. int ret = 0;
  1906. wc_Md5 md5, md5Copy;
  1907. byte hash[WC_MD5_DIGEST_SIZE];
  1908. byte hashcopy[WC_MD5_DIGEST_SIZE];
  1909. testVector a, b, c, d, e, f;
  1910. testVector test_md5[6];
  1911. int times = sizeof(test_md5) / sizeof(testVector), i;
  1912. a.input = "";
  1913. a.output = "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42"
  1914. "\x7e";
  1915. a.inLen = XSTRLEN(a.input);
  1916. a.outLen = WC_MD5_DIGEST_SIZE;
  1917. b.input = "abc";
  1918. b.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  1919. "\x72";
  1920. b.inLen = XSTRLEN(b.input);
  1921. b.outLen = WC_MD5_DIGEST_SIZE;
  1922. c.input = "message digest";
  1923. c.output = "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
  1924. "\xd0";
  1925. c.inLen = XSTRLEN(c.input);
  1926. c.outLen = WC_MD5_DIGEST_SIZE;
  1927. d.input = "abcdefghijklmnopqrstuvwxyz";
  1928. d.output = "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
  1929. "\x3b";
  1930. d.inLen = XSTRLEN(d.input);
  1931. d.outLen = WC_MD5_DIGEST_SIZE;
  1932. e.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  1933. "6789";
  1934. e.output = "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
  1935. "\x9f";
  1936. e.inLen = XSTRLEN(e.input);
  1937. e.outLen = WC_MD5_DIGEST_SIZE;
  1938. f.input = "1234567890123456789012345678901234567890123456789012345678"
  1939. "9012345678901234567890";
  1940. f.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  1941. "\x7a";
  1942. f.inLen = XSTRLEN(f.input);
  1943. f.outLen = WC_MD5_DIGEST_SIZE;
  1944. test_md5[0] = a;
  1945. test_md5[1] = b;
  1946. test_md5[2] = c;
  1947. test_md5[3] = d;
  1948. test_md5[4] = e;
  1949. test_md5[5] = f;
  1950. ret = wc_InitMd5_ex(&md5, HEAP_HINT, devId);
  1951. if (ret != 0)
  1952. return -1600;
  1953. ret = wc_InitMd5_ex(&md5Copy, HEAP_HINT, devId);
  1954. if (ret != 0) {
  1955. wc_Md5Free(&md5);
  1956. return -1601;
  1957. }
  1958. for (i = 0; i < times; ++i) {
  1959. ret = wc_Md5Update(&md5, (byte*)test_md5[i].input,
  1960. (word32)test_md5[i].inLen);
  1961. if (ret != 0)
  1962. ERROR_OUT(-1602 - i, exit);
  1963. ret = wc_Md5GetHash(&md5, hashcopy);
  1964. if (ret != 0)
  1965. ERROR_OUT(-1603 - i, exit);
  1966. ret = wc_Md5Copy(&md5, &md5Copy);
  1967. if (ret != 0)
  1968. ERROR_OUT(-1604 - i, exit);
  1969. ret = wc_Md5Final(&md5, hash);
  1970. if (ret != 0)
  1971. ERROR_OUT(-1605 - i, exit);
  1972. wc_Md5Free(&md5Copy);
  1973. if (XMEMCMP(hash, test_md5[i].output, WC_MD5_DIGEST_SIZE) != 0)
  1974. ERROR_OUT(-1606 - i, exit);
  1975. if (XMEMCMP(hash, hashcopy, WC_MD5_DIGEST_SIZE) != 0)
  1976. ERROR_OUT(-1607 - i, exit);
  1977. }
  1978. #ifndef NO_LARGE_HASH_TEST
  1979. /* BEGIN LARGE HASH TEST */ {
  1980. byte large_input[1024];
  1981. const char* large_digest =
  1982. "\x44\xd0\x88\xce\xf1\x36\xd1\x78\xe9\xc8\xba\x84\xc3\xfd\xf6\xca";
  1983. for (i = 0; i < (int)sizeof(large_input); i++) {
  1984. large_input[i] = (byte)(i & 0xFF);
  1985. }
  1986. times = 100;
  1987. #ifdef WOLFSSL_PIC32MZ_HASH
  1988. wc_Md5SizeSet(&md5, times * sizeof(large_input));
  1989. #endif
  1990. for (i = 0; i < times; ++i) {
  1991. ret = wc_Md5Update(&md5, (byte*)large_input,
  1992. (word32)sizeof(large_input));
  1993. if (ret != 0)
  1994. ERROR_OUT(-1608, exit);
  1995. }
  1996. ret = wc_Md5Final(&md5, hash);
  1997. if (ret != 0)
  1998. ERROR_OUT(-1609, exit);
  1999. if (XMEMCMP(hash, large_digest, WC_MD5_DIGEST_SIZE) != 0)
  2000. ERROR_OUT(-1610, exit);
  2001. } /* END LARGE HASH TEST */
  2002. #endif /* NO_LARGE_HASH_TEST */
  2003. exit:
  2004. wc_Md5Free(&md5);
  2005. wc_Md5Free(&md5Copy);
  2006. return ret;
  2007. }
  2008. #endif /* NO_MD5 */
  2009. #ifndef NO_MD4
  2010. WOLFSSL_TEST_SUBROUTINE int md4_test(void)
  2011. {
  2012. Md4 md4;
  2013. byte hash[MD4_DIGEST_SIZE];
  2014. testVector a, b, c, d, e, f, g;
  2015. testVector test_md4[7];
  2016. int times = sizeof(test_md4) / sizeof(testVector), i;
  2017. a.input = "";
  2018. a.output = "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"
  2019. "\xc0";
  2020. a.inLen = XSTRLEN(a.input);
  2021. a.outLen = MD4_DIGEST_SIZE;
  2022. b.input = "a";
  2023. b.output = "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"
  2024. "\x24";
  2025. b.inLen = XSTRLEN(b.input);
  2026. b.outLen = MD4_DIGEST_SIZE;
  2027. c.input = "abc";
  2028. c.output = "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"
  2029. "\x9d";
  2030. c.inLen = XSTRLEN(c.input);
  2031. c.outLen = MD4_DIGEST_SIZE;
  2032. d.input = "message digest";
  2033. d.output = "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"
  2034. "\x4b";
  2035. d.inLen = XSTRLEN(d.input);
  2036. d.outLen = MD4_DIGEST_SIZE;
  2037. e.input = "abcdefghijklmnopqrstuvwxyz";
  2038. e.output = "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"
  2039. "\xa9";
  2040. e.inLen = XSTRLEN(e.input);
  2041. e.outLen = MD4_DIGEST_SIZE;
  2042. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  2043. "6789";
  2044. f.output = "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"
  2045. "\xe4";
  2046. f.inLen = XSTRLEN(f.input);
  2047. f.outLen = MD4_DIGEST_SIZE;
  2048. g.input = "1234567890123456789012345678901234567890123456789012345678"
  2049. "9012345678901234567890";
  2050. g.output = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"
  2051. "\x36";
  2052. g.inLen = XSTRLEN(g.input);
  2053. g.outLen = MD4_DIGEST_SIZE;
  2054. test_md4[0] = a;
  2055. test_md4[1] = b;
  2056. test_md4[2] = c;
  2057. test_md4[3] = d;
  2058. test_md4[4] = e;
  2059. test_md4[5] = f;
  2060. test_md4[6] = g;
  2061. wc_InitMd4(&md4);
  2062. for (i = 0; i < times; ++i) {
  2063. wc_Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen);
  2064. wc_Md4Final(&md4, hash);
  2065. if (XMEMCMP(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0)
  2066. return -1700 - i;
  2067. }
  2068. return 0;
  2069. }
  2070. #endif /* NO_MD4 */
  2071. #ifndef NO_SHA
  2072. WOLFSSL_TEST_SUBROUTINE int sha_test(void)
  2073. {
  2074. int ret = 0;
  2075. wc_Sha sha, shaCopy;
  2076. byte hash[WC_SHA_DIGEST_SIZE];
  2077. byte hashcopy[WC_SHA_DIGEST_SIZE];
  2078. testVector a, b, c, d, e;
  2079. testVector test_sha[5];
  2080. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2081. a.input = "";
  2082. a.output = "\xda\x39\xa3\xee\x5e\x6b\x4b\x0d\x32\x55\xbf\xef\x95\x60\x18"
  2083. "\x90\xaf\xd8\x07\x09";
  2084. a.inLen = XSTRLEN(a.input);
  2085. a.outLen = WC_SHA_DIGEST_SIZE;
  2086. b.input = "abc";
  2087. b.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  2088. "\x6C\x9C\xD0\xD8\x9D";
  2089. b.inLen = XSTRLEN(b.input);
  2090. b.outLen = WC_SHA_DIGEST_SIZE;
  2091. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2092. c.output = "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
  2093. "\xE5\xE5\x46\x70\xF1";
  2094. c.inLen = XSTRLEN(c.input);
  2095. c.outLen = WC_SHA_DIGEST_SIZE;
  2096. d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2097. "aaaaaa";
  2098. d.output = "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
  2099. "\x2A\x25\xEC\x64\x4D";
  2100. d.inLen = XSTRLEN(d.input);
  2101. d.outLen = WC_SHA_DIGEST_SIZE;
  2102. e.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2103. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2104. "aaaaaaaaaa";
  2105. e.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  2106. "\x53\x99\x5E\x26\xA0";
  2107. e.inLen = XSTRLEN(e.input);
  2108. e.outLen = WC_SHA_DIGEST_SIZE;
  2109. test_sha[0] = a;
  2110. test_sha[1] = b;
  2111. test_sha[2] = c;
  2112. test_sha[3] = d;
  2113. test_sha[4] = e;
  2114. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  2115. if (ret != 0)
  2116. return -1800;
  2117. ret = wc_InitSha_ex(&shaCopy, HEAP_HINT, devId);
  2118. if (ret != 0) {
  2119. wc_ShaFree(&sha);
  2120. return -1801;
  2121. }
  2122. for (i = 0; i < times; ++i) {
  2123. ret = wc_ShaUpdate(&sha, (byte*)test_sha[i].input,
  2124. (word32)test_sha[i].inLen);
  2125. if (ret != 0)
  2126. ERROR_OUT(-1802 - i, exit);
  2127. ret = wc_ShaGetHash(&sha, hashcopy);
  2128. if (ret != 0)
  2129. ERROR_OUT(-1803 - i, exit);
  2130. ret = wc_ShaCopy(&sha, &shaCopy);
  2131. if (ret != 0)
  2132. ERROR_OUT(-1804 - i, exit);
  2133. ret = wc_ShaFinal(&sha, hash);
  2134. if (ret != 0)
  2135. ERROR_OUT(-1805 - i, exit);
  2136. wc_ShaFree(&shaCopy);
  2137. if (XMEMCMP(hash, test_sha[i].output, WC_SHA_DIGEST_SIZE) != 0)
  2138. ERROR_OUT(-1806 - i, exit);
  2139. if (XMEMCMP(hash, hashcopy, WC_SHA_DIGEST_SIZE) != 0)
  2140. ERROR_OUT(-1807 - i, exit);
  2141. }
  2142. #ifndef NO_LARGE_HASH_TEST
  2143. /* BEGIN LARGE HASH TEST */ {
  2144. byte large_input[1024];
  2145. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  2146. defined(HASH_SIZE_LIMIT)
  2147. const char* large_digest =
  2148. "\x1d\x6a\x5a\xf6\xe5\x7c\x86\xce\x7f\x7c\xaf\xd5\xdb\x08\xcd\x59"
  2149. "\x15\x8c\x6d\xb6";
  2150. #else
  2151. const char* large_digest =
  2152. "\x8b\x77\x02\x48\x39\xe8\xdb\xd3\x9a\xf4\x05\x24\x66\x12\x2d\x9e"
  2153. "\xc5\xd9\x0a\xac";
  2154. #endif
  2155. for (i = 0; i < (int)sizeof(large_input); i++) {
  2156. large_input[i] = (byte)(i & 0xFF);
  2157. }
  2158. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  2159. defined(HASH_SIZE_LIMIT)
  2160. times = 20;
  2161. #else
  2162. times = 100;
  2163. #endif
  2164. #ifdef WOLFSSL_PIC32MZ_HASH
  2165. wc_ShaSizeSet(&sha, times * sizeof(large_input));
  2166. #endif
  2167. for (i = 0; i < times; ++i) {
  2168. ret = wc_ShaUpdate(&sha, (byte*)large_input,
  2169. (word32)sizeof(large_input));
  2170. if (ret != 0)
  2171. ERROR_OUT(-1808, exit);
  2172. }
  2173. ret = wc_ShaFinal(&sha, hash);
  2174. if (ret != 0)
  2175. ERROR_OUT(-1809, exit);
  2176. if (XMEMCMP(hash, large_digest, WC_SHA_DIGEST_SIZE) != 0)
  2177. ERROR_OUT(-1810, exit);
  2178. } /* END LARGE HASH TEST */
  2179. #endif /* NO_LARGE_HASH_TEST */
  2180. exit:
  2181. wc_ShaFree(&sha);
  2182. wc_ShaFree(&shaCopy);
  2183. return ret;
  2184. }
  2185. #endif /* NO_SHA */
  2186. #ifdef WOLFSSL_RIPEMD
  2187. WOLFSSL_TEST_SUBROUTINE int ripemd_test(void)
  2188. {
  2189. RipeMd ripemd;
  2190. int ret;
  2191. byte hash[RIPEMD_DIGEST_SIZE];
  2192. testVector a, b, c, d;
  2193. testVector test_ripemd[4];
  2194. int times = sizeof(test_ripemd) / sizeof(struct testVector), i;
  2195. a.input = "abc";
  2196. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  2197. "\xb0\x87\xf1\x5a\x0b\xfc";
  2198. a.inLen = XSTRLEN(a.input);
  2199. a.outLen = RIPEMD_DIGEST_SIZE;
  2200. b.input = "message digest";
  2201. b.output = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
  2202. "\x5f\xfa\x21\x59\x5f\x36";
  2203. b.inLen = XSTRLEN(b.input);
  2204. b.outLen = RIPEMD_DIGEST_SIZE;
  2205. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2206. c.output = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
  2207. "\xf4\x9a\xda\x62\xeb\x2b";
  2208. c.inLen = XSTRLEN(c.input);
  2209. c.outLen = RIPEMD_DIGEST_SIZE;
  2210. d.input = "12345678901234567890123456789012345678901234567890123456"
  2211. "789012345678901234567890";
  2212. d.output = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
  2213. "\x82\xbf\x63\x32\x6b\xfb";
  2214. d.inLen = XSTRLEN(d.input);
  2215. d.outLen = RIPEMD_DIGEST_SIZE;
  2216. test_ripemd[0] = a;
  2217. test_ripemd[1] = b;
  2218. test_ripemd[2] = c;
  2219. test_ripemd[3] = d;
  2220. ret = wc_InitRipeMd(&ripemd);
  2221. if (ret != 0) {
  2222. return -1900;
  2223. }
  2224. for (i = 0; i < times; ++i) {
  2225. ret = wc_RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input,
  2226. (word32)test_ripemd[i].inLen);
  2227. if (ret != 0) {
  2228. return -1901 - i;
  2229. }
  2230. ret = wc_RipeMdFinal(&ripemd, hash);
  2231. if (ret != 0) {
  2232. return -1911 - i;
  2233. }
  2234. if (XMEMCMP(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0)
  2235. return -1921 - i;
  2236. }
  2237. return 0;
  2238. }
  2239. #endif /* WOLFSSL_RIPEMD */
  2240. #ifdef HAVE_BLAKE2
  2241. #define BLAKE2B_TESTS 3
  2242. static const byte blake2b_vec[BLAKE2B_TESTS][BLAKE2B_OUTBYTES] =
  2243. {
  2244. {
  2245. 0x78, 0x6A, 0x02, 0xF7, 0x42, 0x01, 0x59, 0x03,
  2246. 0xC6, 0xC6, 0xFD, 0x85, 0x25, 0x52, 0xD2, 0x72,
  2247. 0x91, 0x2F, 0x47, 0x40, 0xE1, 0x58, 0x47, 0x61,
  2248. 0x8A, 0x86, 0xE2, 0x17, 0xF7, 0x1F, 0x54, 0x19,
  2249. 0xD2, 0x5E, 0x10, 0x31, 0xAF, 0xEE, 0x58, 0x53,
  2250. 0x13, 0x89, 0x64, 0x44, 0x93, 0x4E, 0xB0, 0x4B,
  2251. 0x90, 0x3A, 0x68, 0x5B, 0x14, 0x48, 0xB7, 0x55,
  2252. 0xD5, 0x6F, 0x70, 0x1A, 0xFE, 0x9B, 0xE2, 0xCE
  2253. },
  2254. {
  2255. 0x2F, 0xA3, 0xF6, 0x86, 0xDF, 0x87, 0x69, 0x95,
  2256. 0x16, 0x7E, 0x7C, 0x2E, 0x5D, 0x74, 0xC4, 0xC7,
  2257. 0xB6, 0xE4, 0x8F, 0x80, 0x68, 0xFE, 0x0E, 0x44,
  2258. 0x20, 0x83, 0x44, 0xD4, 0x80, 0xF7, 0x90, 0x4C,
  2259. 0x36, 0x96, 0x3E, 0x44, 0x11, 0x5F, 0xE3, 0xEB,
  2260. 0x2A, 0x3A, 0xC8, 0x69, 0x4C, 0x28, 0xBC, 0xB4,
  2261. 0xF5, 0xA0, 0xF3, 0x27, 0x6F, 0x2E, 0x79, 0x48,
  2262. 0x7D, 0x82, 0x19, 0x05, 0x7A, 0x50, 0x6E, 0x4B
  2263. },
  2264. {
  2265. 0x1C, 0x08, 0x79, 0x8D, 0xC6, 0x41, 0xAB, 0xA9,
  2266. 0xDE, 0xE4, 0x35, 0xE2, 0x25, 0x19, 0xA4, 0x72,
  2267. 0x9A, 0x09, 0xB2, 0xBF, 0xE0, 0xFF, 0x00, 0xEF,
  2268. 0x2D, 0xCD, 0x8E, 0xD6, 0xF8, 0xA0, 0x7D, 0x15,
  2269. 0xEA, 0xF4, 0xAE, 0xE5, 0x2B, 0xBF, 0x18, 0xAB,
  2270. 0x56, 0x08, 0xA6, 0x19, 0x0F, 0x70, 0xB9, 0x04,
  2271. 0x86, 0xC8, 0xA7, 0xD4, 0x87, 0x37, 0x10, 0xB1,
  2272. 0x11, 0x5D, 0x3D, 0xEB, 0xBB, 0x43, 0x27, 0xB5
  2273. }
  2274. };
  2275. WOLFSSL_TEST_SUBROUTINE int blake2b_test(void)
  2276. {
  2277. Blake2b b2b;
  2278. byte digest[64];
  2279. byte input[64];
  2280. int i, ret;
  2281. for (i = 0; i < (int)sizeof(input); i++)
  2282. input[i] = (byte)i;
  2283. for (i = 0; i < BLAKE2B_TESTS; i++) {
  2284. ret = wc_InitBlake2b(&b2b, 64);
  2285. if (ret != 0)
  2286. return -2000 - i;
  2287. ret = wc_Blake2bUpdate(&b2b, input, i);
  2288. if (ret != 0)
  2289. return -2010 - 1;
  2290. ret = wc_Blake2bFinal(&b2b, digest, 64);
  2291. if (ret != 0)
  2292. return -2020 - i;
  2293. if (XMEMCMP(digest, blake2b_vec[i], 64) != 0) {
  2294. return -2030 - i;
  2295. }
  2296. }
  2297. return 0;
  2298. }
  2299. #endif /* HAVE_BLAKE2 */
  2300. #ifdef HAVE_BLAKE2S
  2301. #define BLAKE2S_TESTS 3
  2302. static const byte blake2s_vec[BLAKE2S_TESTS][BLAKE2S_OUTBYTES] =
  2303. {
  2304. {
  2305. 0x69, 0x21, 0x7a, 0x30, 0x79, 0x90, 0x80, 0x94,
  2306. 0xe1, 0x11, 0x21, 0xd0, 0x42, 0x35, 0x4a, 0x7c,
  2307. 0x1f, 0x55, 0xb6, 0x48, 0x2c, 0xa1, 0xa5, 0x1e,
  2308. 0x1b, 0x25, 0x0d, 0xfd, 0x1e, 0xd0, 0xee, 0xf9,
  2309. },
  2310. {
  2311. 0xe3, 0x4d, 0x74, 0xdb, 0xaf, 0x4f, 0xf4, 0xc6,
  2312. 0xab, 0xd8, 0x71, 0xcc, 0x22, 0x04, 0x51, 0xd2,
  2313. 0xea, 0x26, 0x48, 0x84, 0x6c, 0x77, 0x57, 0xfb,
  2314. 0xaa, 0xc8, 0x2f, 0xe5, 0x1a, 0xd6, 0x4b, 0xea,
  2315. },
  2316. {
  2317. 0xdd, 0xad, 0x9a, 0xb1, 0x5d, 0xac, 0x45, 0x49,
  2318. 0xba, 0x42, 0xf4, 0x9d, 0x26, 0x24, 0x96, 0xbe,
  2319. 0xf6, 0xc0, 0xba, 0xe1, 0xdd, 0x34, 0x2a, 0x88,
  2320. 0x08, 0xf8, 0xea, 0x26, 0x7c, 0x6e, 0x21, 0x0c,
  2321. }
  2322. };
  2323. WOLFSSL_TEST_SUBROUTINE int blake2s_test(void)
  2324. {
  2325. Blake2s b2s;
  2326. byte digest[32];
  2327. byte input[64];
  2328. int i, ret;
  2329. for (i = 0; i < (int)sizeof(input); i++)
  2330. input[i] = (byte)i;
  2331. for (i = 0; i < BLAKE2S_TESTS; i++) {
  2332. ret = wc_InitBlake2s(&b2s, 32);
  2333. if (ret != 0)
  2334. return -2100 - i;
  2335. ret = wc_Blake2sUpdate(&b2s, input, i);
  2336. if (ret != 0)
  2337. return -2110 - 1;
  2338. ret = wc_Blake2sFinal(&b2s, digest, 32);
  2339. if (ret != 0)
  2340. return -2120 - i;
  2341. if (XMEMCMP(digest, blake2s_vec[i], 32) != 0) {
  2342. return -2130 - i;
  2343. }
  2344. }
  2345. return 0;
  2346. }
  2347. #endif /* HAVE_BLAKE2S */
  2348. #ifdef WOLFSSL_SHA224
  2349. WOLFSSL_TEST_SUBROUTINE int sha224_test(void)
  2350. {
  2351. wc_Sha224 sha, shaCopy;
  2352. byte hash[WC_SHA224_DIGEST_SIZE];
  2353. byte hashcopy[WC_SHA224_DIGEST_SIZE];
  2354. int ret = 0;
  2355. testVector a, b, c;
  2356. testVector test_sha[3];
  2357. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2358. a.input = "";
  2359. a.output = "\xd1\x4a\x02\x8c\x2a\x3a\x2b\xc9\x47\x61\x02\xbb\x28\x82\x34"
  2360. "\xc4\x15\xa2\xb0\x1f\x82\x8e\xa6\x2a\xc5\xb3\xe4\x2f";
  2361. a.inLen = XSTRLEN(a.input);
  2362. a.outLen = WC_SHA224_DIGEST_SIZE;
  2363. b.input = "abc";
  2364. b.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2\x55"
  2365. "\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  2366. b.inLen = XSTRLEN(b.input);
  2367. b.outLen = WC_SHA224_DIGEST_SIZE;
  2368. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2369. c.output = "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  2370. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  2371. c.inLen = XSTRLEN(c.input);
  2372. c.outLen = WC_SHA224_DIGEST_SIZE;
  2373. test_sha[0] = a;
  2374. test_sha[1] = b;
  2375. test_sha[2] = c;
  2376. ret = wc_InitSha224_ex(&sha, HEAP_HINT, devId);
  2377. if (ret != 0)
  2378. return -2200;
  2379. ret = wc_InitSha224_ex(&shaCopy, HEAP_HINT, devId);
  2380. if (ret != 0) {
  2381. wc_Sha224Free(&sha);
  2382. return -2201;
  2383. }
  2384. for (i = 0; i < times; ++i) {
  2385. ret = wc_Sha224Update(&sha, (byte*)test_sha[i].input,
  2386. (word32)test_sha[i].inLen);
  2387. if (ret != 0)
  2388. ERROR_OUT(-2202 - i, exit);
  2389. ret = wc_Sha224GetHash(&sha, hashcopy);
  2390. if (ret != 0)
  2391. ERROR_OUT(-2203 - i, exit);
  2392. ret = wc_Sha224Copy(&sha, &shaCopy);
  2393. if (ret != 0)
  2394. ERROR_OUT(-2204 - i, exit);
  2395. ret = wc_Sha224Final(&sha, hash);
  2396. if (ret != 0)
  2397. ERROR_OUT(-2205 - i, exit);
  2398. wc_Sha224Free(&shaCopy);
  2399. if (XMEMCMP(hash, test_sha[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  2400. ERROR_OUT(-2206 - i, exit);
  2401. if (XMEMCMP(hash, hashcopy, WC_SHA224_DIGEST_SIZE) != 0)
  2402. ERROR_OUT(-2207 - i, exit);
  2403. }
  2404. exit:
  2405. wc_Sha224Free(&sha);
  2406. wc_Sha224Free(&shaCopy);
  2407. return ret;
  2408. }
  2409. #endif
  2410. #ifndef NO_SHA256
  2411. WOLFSSL_TEST_SUBROUTINE int sha256_test(void)
  2412. {
  2413. wc_Sha256 sha, shaCopy;
  2414. byte hash[WC_SHA256_DIGEST_SIZE];
  2415. byte hashcopy[WC_SHA256_DIGEST_SIZE];
  2416. int ret = 0;
  2417. testVector a, b, c;
  2418. testVector test_sha[3];
  2419. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2420. a.input = "";
  2421. a.output = "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9"
  2422. "\x24\x27\xae\x41\xe4\x64\x9b\x93\x4c\xa4\x95\x99\x1b\x78\x52"
  2423. "\xb8\x55";
  2424. a.inLen = XSTRLEN(a.input);
  2425. a.outLen = WC_SHA256_DIGEST_SIZE;
  2426. b.input = "abc";
  2427. b.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  2428. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  2429. "\x15\xAD";
  2430. b.inLen = XSTRLEN(b.input);
  2431. b.outLen = WC_SHA256_DIGEST_SIZE;
  2432. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2433. c.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  2434. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  2435. "\x06\xC1";
  2436. c.inLen = XSTRLEN(c.input);
  2437. c.outLen = WC_SHA256_DIGEST_SIZE;
  2438. test_sha[0] = a;
  2439. test_sha[1] = b;
  2440. test_sha[2] = c;
  2441. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  2442. if (ret != 0)
  2443. return -2300;
  2444. ret = wc_InitSha256_ex(&shaCopy, HEAP_HINT, devId);
  2445. if (ret != 0) {
  2446. wc_Sha256Free(&sha);
  2447. return -2301;
  2448. }
  2449. for (i = 0; i < times; ++i) {
  2450. ret = wc_Sha256Update(&sha, (byte*)test_sha[i].input,
  2451. (word32)test_sha[i].inLen);
  2452. if (ret != 0) {
  2453. ERROR_OUT(-2302 - i, exit);
  2454. }
  2455. ret = wc_Sha256GetHash(&sha, hashcopy);
  2456. if (ret != 0)
  2457. ERROR_OUT(-2303 - i, exit);
  2458. ret = wc_Sha256Copy(&sha, &shaCopy);
  2459. if (ret != 0)
  2460. ERROR_OUT(-2304 - i, exit);
  2461. ret = wc_Sha256Final(&sha, hash);
  2462. if (ret != 0)
  2463. ERROR_OUT(-2305 - i, exit);
  2464. wc_Sha256Free(&shaCopy);
  2465. if (XMEMCMP(hash, test_sha[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  2466. ERROR_OUT(-2306 - i, exit);
  2467. if (XMEMCMP(hash, hashcopy, WC_SHA256_DIGEST_SIZE) != 0)
  2468. ERROR_OUT(-2307 - i, exit);
  2469. }
  2470. #ifndef NO_LARGE_HASH_TEST
  2471. /* BEGIN LARGE HASH TEST */ {
  2472. byte large_input[1024];
  2473. #ifdef HASH_SIZE_LIMIT
  2474. const char* large_digest =
  2475. "\xa4\x75\x9e\x7a\xa2\x03\x38\x32\x88\x66\xa2\xea\x17\xea\xf8\xc7"
  2476. "\xfe\x4e\xc6\xbb\xe3\xbb\x71\xce\xe7\xdf\x7c\x04\x61\xb3\xc2\x2f";
  2477. #else
  2478. const char* large_digest =
  2479. "\x27\x78\x3e\x87\x96\x3a\x4e\xfb\x68\x29\xb5\x31\xc9\xba\x57\xb4"
  2480. "\x4f\x45\x79\x7f\x67\x70\xbd\x63\x7f\xbf\x0d\x80\x7c\xbd\xba\xe0";
  2481. #endif
  2482. for (i = 0; i < (int)sizeof(large_input); i++) {
  2483. large_input[i] = (byte)(i & 0xFF);
  2484. }
  2485. #ifdef HASH_SIZE_LIMIT
  2486. times = 20;
  2487. #else
  2488. times = 100;
  2489. #endif
  2490. #ifdef WOLFSSL_PIC32MZ_HASH
  2491. wc_Sha256SizeSet(&sha, times * sizeof(large_input));
  2492. #endif
  2493. for (i = 0; i < times; ++i) {
  2494. ret = wc_Sha256Update(&sha, (byte*)large_input,
  2495. (word32)sizeof(large_input));
  2496. if (ret != 0)
  2497. ERROR_OUT(-2308, exit);
  2498. }
  2499. ret = wc_Sha256Final(&sha, hash);
  2500. if (ret != 0)
  2501. ERROR_OUT(-2309, exit);
  2502. if (XMEMCMP(hash, large_digest, WC_SHA256_DIGEST_SIZE) != 0)
  2503. ERROR_OUT(-2310, exit);
  2504. } /* END LARGE HASH TEST */
  2505. #endif /* NO_LARGE_HASH_TEST */
  2506. exit:
  2507. wc_Sha256Free(&sha);
  2508. wc_Sha256Free(&shaCopy);
  2509. return ret;
  2510. }
  2511. #endif
  2512. #ifdef WOLFSSL_SHA512
  2513. WOLFSSL_TEST_SUBROUTINE int sha512_test(void)
  2514. {
  2515. wc_Sha512 sha, shaCopy;
  2516. byte hash[WC_SHA512_DIGEST_SIZE];
  2517. byte hashcopy[WC_SHA512_DIGEST_SIZE];
  2518. int ret = 0;
  2519. testVector a, b, c;
  2520. testVector test_sha[3];
  2521. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2522. a.input = "";
  2523. a.output = "\xcf\x83\xe1\x35\x7e\xef\xb8\xbd\xf1\x54\x28\x50\xd6\x6d\x80"
  2524. "\x07\xd6\x20\xe4\x05\x0b\x57\x15\xdc\x83\xf4\xa9\x21\xd3\x6c"
  2525. "\xe9\xce\x47\xd0\xd1\x3c\x5d\x85\xf2\xb0\xff\x83\x18\xd2\x87"
  2526. "\x7e\xec\x2f\x63\xb9\x31\xbd\x47\x41\x7a\x81\xa5\x38\x32\x7a"
  2527. "\xf9\x27\xda\x3e";
  2528. a.inLen = XSTRLEN(a.input);
  2529. a.outLen = WC_SHA512_DIGEST_SIZE;
  2530. b.input = "abc";
  2531. b.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  2532. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  2533. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  2534. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  2535. "\xa5\x4c\xa4\x9f";
  2536. b.inLen = XSTRLEN(b.input);
  2537. b.outLen = WC_SHA512_DIGEST_SIZE;
  2538. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  2539. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  2540. c.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  2541. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  2542. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  2543. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  2544. "\x87\x4b\xe9\x09";
  2545. c.inLen = XSTRLEN(c.input);
  2546. c.outLen = WC_SHA512_DIGEST_SIZE;
  2547. test_sha[0] = a;
  2548. test_sha[1] = b;
  2549. test_sha[2] = c;
  2550. ret = wc_InitSha512_ex(&sha, HEAP_HINT, devId);
  2551. if (ret != 0)
  2552. return -2400;
  2553. ret = wc_InitSha512_ex(&shaCopy, HEAP_HINT, devId);
  2554. if (ret != 0) {
  2555. wc_Sha512Free(&sha);
  2556. return -2401;
  2557. }
  2558. for (i = 0; i < times; ++i) {
  2559. ret = wc_Sha512Update(&sha, (byte*)test_sha[i].input,
  2560. (word32)test_sha[i].inLen);
  2561. if (ret != 0)
  2562. ERROR_OUT(-2402 - i, exit);
  2563. ret = wc_Sha512GetHash(&sha, hashcopy);
  2564. if (ret != 0)
  2565. ERROR_OUT(-2403 - i, exit);
  2566. ret = wc_Sha512Copy(&sha, &shaCopy);
  2567. if (ret != 0)
  2568. ERROR_OUT(-2404 - i, exit);
  2569. ret = wc_Sha512Final(&sha, hash);
  2570. if (ret != 0)
  2571. ERROR_OUT(-2405 - i, exit);
  2572. wc_Sha512Free(&shaCopy);
  2573. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  2574. ERROR_OUT(-2406 - i, exit);
  2575. if (XMEMCMP(hash, hashcopy, WC_SHA512_DIGEST_SIZE) != 0)
  2576. ERROR_OUT(-2407 - i, exit);
  2577. }
  2578. #ifndef NO_LARGE_HASH_TEST
  2579. /* BEGIN LARGE HASH TEST */ {
  2580. byte large_input[1024];
  2581. #ifdef HASH_SIZE_LIMIT
  2582. const char* large_digest =
  2583. "\x30\x9B\x96\xA6\xE9\x43\x78\x30\xA3\x71\x51\x61\xC1\xEB\xE1\xBE"
  2584. "\xC8\xA5\xF9\x13\x5A\xD6\x6D\x9E\x46\x31\x31\x67\x8D\xE2\xC0\x0B"
  2585. "\x2A\x1A\x03\xE1\xF3\x48\xA7\x33\xBD\x49\xF8\xFF\xF1\xC2\xC2\x95"
  2586. "\xCB\xF0\xAF\x87\x61\x85\x58\x63\x6A\xCA\x70\x9C\x8B\x83\x3F\x5D";
  2587. #else
  2588. const char* large_digest =
  2589. "\x5a\x1f\x73\x90\xbd\x8c\xe4\x63\x54\xce\xa0\x9b\xef\x32\x78\x2d"
  2590. "\x2e\xe7\x0d\x5e\x2f\x9d\x15\x1b\xdd\x2d\xde\x65\x0c\x7b\xfa\x83"
  2591. "\x5e\x80\x02\x13\x84\xb8\x3f\xff\x71\x62\xb5\x09\x89\x63\xe1\xdc"
  2592. "\xa5\xdc\xfc\xfa\x9d\x1a\x4d\xc0\xfa\x3a\x14\xf6\x01\x51\x90\xa4";
  2593. #endif
  2594. for (i = 0; i < (int)sizeof(large_input); i++) {
  2595. large_input[i] = (byte)(i & 0xFF);
  2596. }
  2597. #ifdef HASH_SIZE_LIMIT
  2598. times = 20;
  2599. #else
  2600. times = 100;
  2601. #endif
  2602. for (i = 0; i < times; ++i) {
  2603. ret = wc_Sha512Update(&sha, (byte*)large_input,
  2604. (word32)sizeof(large_input));
  2605. if (ret != 0)
  2606. ERROR_OUT(-2408, exit);
  2607. }
  2608. ret = wc_Sha512Final(&sha, hash);
  2609. if (ret != 0)
  2610. ERROR_OUT(-2409, exit);
  2611. if (XMEMCMP(hash, large_digest, WC_SHA512_DIGEST_SIZE) != 0)
  2612. ERROR_OUT(-2410, exit);
  2613. #ifndef NO_UNALIGNED_MEMORY_TEST
  2614. /* Unaligned memory access test */
  2615. for (i = 1; i < 16; i++) {
  2616. ret = wc_Sha512Update(&sha, (byte*)large_input + i,
  2617. (word32)sizeof(large_input) - i);
  2618. if (ret != 0)
  2619. ERROR_OUT(-2411, exit);
  2620. ret = wc_Sha512Final(&sha, hash);
  2621. }
  2622. #endif
  2623. } /* END LARGE HASH TEST */
  2624. #endif /* NO_LARGE_HASH_TEST */
  2625. exit:
  2626. wc_Sha512Free(&sha);
  2627. wc_Sha512Free(&shaCopy);
  2628. return ret;
  2629. }
  2630. #endif
  2631. #ifdef WOLFSSL_SHA384
  2632. WOLFSSL_TEST_SUBROUTINE int sha384_test(void)
  2633. {
  2634. wc_Sha384 sha, shaCopy;
  2635. byte hash[WC_SHA384_DIGEST_SIZE];
  2636. byte hashcopy[WC_SHA384_DIGEST_SIZE];
  2637. int ret = 0;
  2638. testVector a, b, c;
  2639. testVector test_sha[3];
  2640. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2641. a.input = "";
  2642. a.output = "\x38\xb0\x60\xa7\x51\xac\x96\x38\x4c\xd9\x32\x7e\xb1\xb1\xe3"
  2643. "\x6a\x21\xfd\xb7\x11\x14\xbe\x07\x43\x4c\x0c\xc7\xbf\x63\xf6"
  2644. "\xe1\xda\x27\x4e\xde\xbf\xe7\x6f\x65\xfb\xd5\x1a\xd2\xf1\x48"
  2645. "\x98\xb9\x5b";
  2646. a.inLen = XSTRLEN(a.input);
  2647. a.outLen = WC_SHA384_DIGEST_SIZE;
  2648. b.input = "abc";
  2649. b.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  2650. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  2651. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  2652. "\xc8\x25\xa7";
  2653. b.inLen = XSTRLEN(b.input);
  2654. b.outLen = WC_SHA384_DIGEST_SIZE;
  2655. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  2656. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  2657. c.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  2658. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  2659. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  2660. "\x74\x60\x39";
  2661. c.inLen = XSTRLEN(c.input);
  2662. c.outLen = WC_SHA384_DIGEST_SIZE;
  2663. test_sha[0] = a;
  2664. test_sha[1] = b;
  2665. test_sha[2] = c;
  2666. ret = wc_InitSha384_ex(&sha, HEAP_HINT, devId);
  2667. if (ret != 0)
  2668. return -2500;
  2669. ret = wc_InitSha384_ex(&shaCopy, HEAP_HINT, devId);
  2670. if (ret != 0) {
  2671. wc_Sha384Free(&sha);
  2672. return -2501;
  2673. }
  2674. for (i = 0; i < times; ++i) {
  2675. ret = wc_Sha384Update(&sha, (byte*)test_sha[i].input,
  2676. (word32)test_sha[i].inLen);
  2677. if (ret != 0)
  2678. ERROR_OUT(-2502 - i, exit);
  2679. ret = wc_Sha384GetHash(&sha, hashcopy);
  2680. if (ret != 0)
  2681. ERROR_OUT(-2503 - i, exit);
  2682. ret = wc_Sha384Copy(&sha, &shaCopy);
  2683. if (ret != 0)
  2684. ERROR_OUT(-2504 - i, exit);
  2685. ret = wc_Sha384Final(&sha, hash);
  2686. if (ret != 0)
  2687. ERROR_OUT(-2505 - i, exit);
  2688. wc_Sha384Free(&shaCopy);
  2689. if (XMEMCMP(hash, test_sha[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  2690. ERROR_OUT(-2506 - i, exit);
  2691. if (XMEMCMP(hash, hashcopy, WC_SHA384_DIGEST_SIZE) != 0)
  2692. ERROR_OUT(-2507 - i, exit);
  2693. }
  2694. #ifndef NO_LARGE_HASH_TEST
  2695. /* BEGIN LARGE HASH TEST */ {
  2696. byte large_input[1024];
  2697. #ifdef HASH_SIZE_LIMIT
  2698. const char* large_digest =
  2699. "\xB5\xAD\x66\x6F\xD9\x58\x5E\x68\xDD\x5E\x30\xD3\x95\x72\x33\xA4"
  2700. "\xE9\x4B\x99\x3A\xEF\xF8\xE1\xBF\x1F\x05\x32\xAA\x16\x00\x82\xEC"
  2701. "\x15\xDA\xF2\x75\xEE\xE9\x06\xAF\x52\x8A\x5C\xEF\x72\x81\x80\xD6";
  2702. #else
  2703. const char* large_digest =
  2704. "\x37\x01\xdb\xff\x1e\x40\x4f\xe1\xe2\xea\x0b\x40\xbb\x3b\x39\x9a"
  2705. "\xcc\xe8\x44\x8e\x7e\xe5\x64\xb5\x6b\x7f\x56\x64\xa7\x2b\x84\xe3"
  2706. "\xc5\xd7\x79\x03\x25\x90\xf7\xa4\x58\xcb\x97\xa8\x8b\xb1\xa4\x81";
  2707. #endif
  2708. for (i = 0; i < (int)sizeof(large_input); i++) {
  2709. large_input[i] = (byte)(i & 0xFF);
  2710. }
  2711. #ifdef HASH_SIZE_LIMIT
  2712. times = 20;
  2713. #else
  2714. times = 100;
  2715. #endif
  2716. for (i = 0; i < times; ++i) {
  2717. ret = wc_Sha384Update(&sha, (byte*)large_input,
  2718. (word32)sizeof(large_input));
  2719. if (ret != 0)
  2720. ERROR_OUT(-2508, exit);
  2721. }
  2722. ret = wc_Sha384Final(&sha, hash);
  2723. if (ret != 0)
  2724. ERROR_OUT(-2509, exit);
  2725. if (XMEMCMP(hash, large_digest, WC_SHA384_DIGEST_SIZE) != 0)
  2726. ERROR_OUT(-2510, exit);
  2727. } /* END LARGE HASH TEST */
  2728. #endif /* NO_LARGE_HASH_TEST */
  2729. exit:
  2730. wc_Sha384Free(&sha);
  2731. wc_Sha384Free(&shaCopy);
  2732. return ret;
  2733. }
  2734. #endif /* WOLFSSL_SHA384 */
  2735. #ifdef WOLFSSL_SHA3
  2736. #ifndef WOLFSSL_NOSHA3_224
  2737. static int sha3_224_test(void)
  2738. {
  2739. wc_Sha3 sha;
  2740. byte hash[WC_SHA3_224_DIGEST_SIZE];
  2741. byte hashcopy[WC_SHA3_224_DIGEST_SIZE];
  2742. testVector a, b, c;
  2743. testVector test_sha[3];
  2744. int ret = 0;
  2745. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2746. a.input = "";
  2747. a.output = "\x6b\x4e\x03\x42\x36\x67\xdb\xb7\x3b\x6e\x15\x45\x4f\x0e\xb1"
  2748. "\xab\xd4\x59\x7f\x9a\x1b\x07\x8e\x3f\x5b\x5a\x6b\xc7";
  2749. a.inLen = XSTRLEN(a.input);
  2750. a.outLen = WC_SHA3_224_DIGEST_SIZE;
  2751. b.input = "abc";
  2752. b.output = "\xe6\x42\x82\x4c\x3f\x8c\xf2\x4a\xd0\x92\x34\xee\x7d\x3c\x76"
  2753. "\x6f\xc9\xa3\xa5\x16\x8d\x0c\x94\xad\x73\xb4\x6f\xdf";
  2754. b.inLen = XSTRLEN(b.input);
  2755. b.outLen = WC_SHA3_224_DIGEST_SIZE;
  2756. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2757. c.output = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55\x74\x49\x44\x79"
  2758. "\xba\x5c\x7e\x7a\xb7\x6e\xf2\x64\xea\xd0\xfc\xce\x33";
  2759. c.inLen = XSTRLEN(c.input);
  2760. c.outLen = WC_SHA3_224_DIGEST_SIZE;
  2761. test_sha[0] = a;
  2762. test_sha[1] = b;
  2763. test_sha[2] = c;
  2764. ret = wc_InitSha3_224(&sha, HEAP_HINT, devId);
  2765. if (ret != 0)
  2766. return -2600;
  2767. for (i = 0; i < times; ++i) {
  2768. ret = wc_Sha3_224_Update(&sha, (byte*)test_sha[i].input,
  2769. (word32)test_sha[i].inLen);
  2770. if (ret != 0)
  2771. ERROR_OUT(-2601 - i, exit);
  2772. ret = wc_Sha3_224_GetHash(&sha, hashcopy);
  2773. if (ret != 0)
  2774. ERROR_OUT(-2602 - i, exit);
  2775. ret = wc_Sha3_224_Final(&sha, hash);
  2776. if (ret != 0)
  2777. ERROR_OUT(-2603 - i, exit);
  2778. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_224_DIGEST_SIZE) != 0)
  2779. ERROR_OUT(-2604 - i, exit);
  2780. if (XMEMCMP(hash, hashcopy, WC_SHA3_224_DIGEST_SIZE) != 0)
  2781. ERROR_OUT(-2605 - i, exit);
  2782. }
  2783. #ifndef NO_LARGE_HASH_TEST
  2784. /* BEGIN LARGE HASH TEST */ {
  2785. byte large_input[1024];
  2786. const char* large_digest =
  2787. "\x13\xe5\xd3\x98\x7b\x94\xda\x41\x12\xc7\x1e\x92\x3a\x19"
  2788. "\x21\x20\x86\x6f\x24\xbf\x0a\x31\xbc\xfd\xd6\x70\x36\xf3";
  2789. for (i = 0; i < (int)sizeof(large_input); i++) {
  2790. large_input[i] = (byte)(i & 0xFF);
  2791. }
  2792. times = 100;
  2793. for (i = 0; i < times; ++i) {
  2794. ret = wc_Sha3_224_Update(&sha, (byte*)large_input,
  2795. (word32)sizeof(large_input));
  2796. if (ret != 0)
  2797. ERROR_OUT(-2606, exit);
  2798. }
  2799. ret = wc_Sha3_224_Final(&sha, hash);
  2800. if (ret != 0)
  2801. ERROR_OUT(-2607, exit);
  2802. if (XMEMCMP(hash, large_digest, WC_SHA3_224_DIGEST_SIZE) != 0)
  2803. ERROR_OUT(-2608, exit);
  2804. } /* END LARGE HASH TEST */
  2805. #endif /* NO_LARGE_HASH_TEST */
  2806. exit:
  2807. wc_Sha3_224_Free(&sha);
  2808. return ret;
  2809. }
  2810. #endif /* WOLFSSL_NOSHA3_224 */
  2811. #ifndef WOLFSSL_NOSHA3_256
  2812. static int sha3_256_test(void)
  2813. {
  2814. wc_Sha3 sha;
  2815. byte hash[WC_SHA3_256_DIGEST_SIZE];
  2816. byte hashcopy[WC_SHA3_256_DIGEST_SIZE];
  2817. testVector a, b, c;
  2818. testVector test_sha[3];
  2819. int ret = 0;
  2820. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2821. byte large_input[1024];
  2822. const char* large_digest =
  2823. "\xdc\x90\xc0\xb1\x25\xdb\x2c\x34\x81\xa3\xff\xbc\x1e\x2e\x87\xeb"
  2824. "\x6d\x70\x85\x61\xe0\xe9\x63\x61\xff\xe5\x84\x4b\x1f\x68\x05\x15";
  2825. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  2826. /* test vector with hash of empty string */
  2827. const char* Keccak256EmptyOut =
  2828. "\xc5\xd2\x46\x01\x86\xf7\x23\x3c\x92\x7e\x7d\xb2\xdc\xc7\x03\xc0"
  2829. "\xe5\x00\xb6\x53\xca\x82\x27\x3b\x7b\xfa\xd8\x04\x5d\x85\xa4\x70";
  2830. #endif
  2831. a.input = "";
  2832. a.output = "\xa7\xff\xc6\xf8\xbf\x1e\xd7\x66\x51\xc1\x47\x56\xa0\x61\xd6"
  2833. "\x62\xf5\x80\xff\x4d\xe4\x3b\x49\xfa\x82\xd8\x0a\x4b\x80\xf8"
  2834. "\x43\x4a";
  2835. a.inLen = XSTRLEN(a.input);
  2836. a.outLen = WC_SHA3_256_DIGEST_SIZE;
  2837. b.input = "abc";
  2838. b.output = "\x3a\x98\x5d\xa7\x4f\xe2\x25\xb2\x04\x5c\x17\x2d\x6b\xd3\x90"
  2839. "\xbd\x85\x5f\x08\x6e\x3e\x9d\x52\x5b\x46\xbf\xe2\x45\x11\x43"
  2840. "\x15\x32";
  2841. b.inLen = XSTRLEN(b.input);
  2842. b.outLen = WC_SHA3_256_DIGEST_SIZE;
  2843. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2844. c.output = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8\x23\x5e"
  2845. "\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32\xdd\x97\x49\x6d"
  2846. "\x33\x76";
  2847. c.inLen = XSTRLEN(c.input);
  2848. c.outLen = WC_SHA3_256_DIGEST_SIZE;
  2849. test_sha[0] = a;
  2850. test_sha[1] = b;
  2851. test_sha[2] = c;
  2852. ret = wc_InitSha3_256(&sha, HEAP_HINT, devId);
  2853. if (ret != 0)
  2854. return -2700;
  2855. for (i = 0; i < times; ++i) {
  2856. ret = wc_Sha3_256_Update(&sha, (byte*)test_sha[i].input,
  2857. (word32)test_sha[i].inLen);
  2858. if (ret != 0)
  2859. ERROR_OUT(-2701 - i, exit);
  2860. ret = wc_Sha3_256_GetHash(&sha, hashcopy);
  2861. if (ret != 0)
  2862. ERROR_OUT(-2702 - i, exit);
  2863. ret = wc_Sha3_256_Final(&sha, hash);
  2864. if (ret != 0)
  2865. ERROR_OUT(-2703 - i, exit);
  2866. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_256_DIGEST_SIZE) != 0)
  2867. ERROR_OUT(-2704 - i, exit);
  2868. if (XMEMCMP(hash, hashcopy, WC_SHA3_256_DIGEST_SIZE) != 0)
  2869. ERROR_OUT(-2705 - i, exit);
  2870. }
  2871. #ifndef NO_LARGE_HASH_TEST
  2872. /* BEGIN LARGE HASH TEST */ {
  2873. for (i = 0; i < (int)sizeof(large_input); i++) {
  2874. large_input[i] = (byte)(i & 0xFF);
  2875. }
  2876. times = 100;
  2877. for (i = 0; i < times; ++i) {
  2878. ret = wc_Sha3_256_Update(&sha, (byte*)large_input,
  2879. (word32)sizeof(large_input));
  2880. if (ret != 0)
  2881. ERROR_OUT(-2706, exit);
  2882. }
  2883. ret = wc_Sha3_256_Final(&sha, hash);
  2884. if (ret != 0)
  2885. ERROR_OUT(-2707, exit);
  2886. if (XMEMCMP(hash, large_digest, WC_SHA3_256_DIGEST_SIZE) != 0)
  2887. ERROR_OUT(-2708, exit);
  2888. } /* END LARGE HASH TEST */
  2889. #endif /* NO_LARGE_HASH_TEST */
  2890. /* this is a software only variant of SHA3 not supported by external hardware devices */
  2891. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  2892. /* Test for Keccak256 */
  2893. ret = wc_Sha3_SetFlags(&sha, WC_HASH_SHA3_KECCAK256);
  2894. if (ret != 0) {
  2895. ERROR_OUT(-2709, exit);
  2896. }
  2897. ret = wc_Sha3_256_Update(&sha, (byte*)"", 0);
  2898. if (ret != 0) {
  2899. ERROR_OUT(-2710, exit);
  2900. }
  2901. ret = wc_Sha3_256_Final(&sha, hash);
  2902. if (ret != 0) {
  2903. ERROR_OUT(-2711, exit);
  2904. }
  2905. if (XMEMCMP(hash, Keccak256EmptyOut, WC_SHA3_256_DIGEST_SIZE) != 0) {
  2906. ERROR_OUT(-2712, exit);
  2907. }
  2908. #endif /* WOLFSSL_HASH_FLAGS && !WOLFSSL_ASYNC_CRYPT */
  2909. exit:
  2910. wc_Sha3_256_Free(&sha);
  2911. return ret;
  2912. }
  2913. #endif /* WOLFSSL_NOSHA3_256 */
  2914. #ifndef WOLFSSL_NOSHA3_384
  2915. static int sha3_384_test(void)
  2916. {
  2917. wc_Sha3 sha;
  2918. byte hash[WC_SHA3_384_DIGEST_SIZE];
  2919. byte buf[64];
  2920. #ifndef NO_INTM_HASH_TEST
  2921. byte hashcopy[WC_SHA3_384_DIGEST_SIZE];
  2922. #endif
  2923. testVector a, b, c;
  2924. testVector test_sha[3];
  2925. int ret;
  2926. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2927. a.input = "";
  2928. a.output = "\x0c\x63\xa7\x5b\x84\x5e\x4f\x7d\x01\x10\x7d\x85\x2e\x4c\x24"
  2929. "\x85\xc5\x1a\x50\xaa\xaa\x94\xfc\x61\x99\x5e\x71\xbb\xee\x98"
  2930. "\x3a\x2a\xc3\x71\x38\x31\x26\x4a\xdb\x47\xfb\x6b\xd1\xe0\x58"
  2931. "\xd5\xf0\x04";
  2932. a.inLen = XSTRLEN(a.input);
  2933. a.outLen = WC_SHA3_384_DIGEST_SIZE;
  2934. #if defined(WOLFSSL_AFALG_XILINX_SHA3) || defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  2935. /* NIST test vector with a length that is a multiple of 4 */
  2936. b.input = "\x7d\x80\xb1\x60\xc4\xb5\x36\xa3\xbe\xb7\x99\x80\x59\x93\x44"
  2937. "\x04\x7c\x5f\x82\xa1\xdf\xc3\xee\xd4";
  2938. b.output = "\x04\x1c\xc5\x86\x1b\xa3\x34\x56\x3c\x61\xd4\xef\x97\x10\xd4"
  2939. "\x89\x6c\x31\x1c\x92\xed\xbe\x0d\x7c\xd5\x3e\x80\x3b\xf2\xf4"
  2940. "\xeb\x60\x57\x23\x55\x70\x77\x0c\xe8\x7c\x55\x20\xd7\xec\x14"
  2941. "\x19\x87\x22";
  2942. b.inLen = XSTRLEN(b.input);
  2943. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  2944. #else
  2945. b.input = "abc";
  2946. b.output = "\xec\x01\x49\x82\x88\x51\x6f\xc9\x26\x45\x9f\x58\xe2\xc6\xad"
  2947. "\x8d\xf9\xb4\x73\xcb\x0f\xc0\x8c\x25\x96\xda\x7c\xf0\xe4\x9b"
  2948. "\xe4\xb2\x98\xd8\x8c\xea\x92\x7a\xc7\xf5\x39\xf1\xed\xf2\x28"
  2949. "\x37\x6d\x25";
  2950. b.inLen = XSTRLEN(b.input);
  2951. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  2952. #endif
  2953. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2954. c.output = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7\x8a\x49"
  2955. "\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd\xbc\x32\xb9\xd4"
  2956. "\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe\xa1\x9e\xef\x51\xac\xd0"
  2957. "\x65\x7c\x22";
  2958. c.inLen = XSTRLEN(c.input);
  2959. c.outLen = WC_SHA3_384_DIGEST_SIZE;
  2960. #if defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  2961. test_sha[0] = b; /* hardware acc. pre-Versal can not handle "" string */
  2962. #else
  2963. test_sha[0] = a;
  2964. #endif
  2965. test_sha[1] = b;
  2966. test_sha[2] = c;
  2967. ret = wc_InitSha3_384(&sha, HEAP_HINT, devId);
  2968. if (ret != 0)
  2969. return -2800;
  2970. for (i = 0; i < times; ++i) {
  2971. XMEMCPY(buf, test_sha[i].input, test_sha[i].inLen);
  2972. ret = wc_Sha3_384_Update(&sha, buf,
  2973. (word32)test_sha[i].inLen);
  2974. if (ret != 0)
  2975. ERROR_OUT(-2801 - (i * 10), exit);
  2976. #ifndef NO_INTM_HASH_TEST
  2977. ret = wc_Sha3_384_GetHash(&sha, hashcopy);
  2978. if (ret != 0)
  2979. ERROR_OUT(-2802 - (i * 10), exit);
  2980. #endif
  2981. ret = wc_Sha3_384_Final(&sha, hash);
  2982. if (ret != 0)
  2983. ERROR_OUT(-2803 - (i * 10), exit);
  2984. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_384_DIGEST_SIZE) != 0)
  2985. ERROR_OUT(-2804 - (i * 10), exit);
  2986. #ifndef NO_INTM_HASH_TEST
  2987. if (XMEMCMP(hash, hashcopy, WC_SHA3_384_DIGEST_SIZE) != 0)
  2988. ERROR_OUT(-2805 - (i * 10), exit);
  2989. #endif
  2990. }
  2991. #ifndef NO_LARGE_HASH_TEST
  2992. /* BEGIN LARGE HASH TEST */ {
  2993. byte large_input[1024];
  2994. const char* large_digest =
  2995. "\x30\x44\xec\x17\xef\x47\x9f\x55\x36\x11\xd6\x3f\x8a\x31\x5a\x71"
  2996. "\x8a\x71\xa7\x1d\x8e\x84\xe8\x6c\x24\x02\x2f\x7a\x08\x4e\xea\xd7"
  2997. "\x42\x36\x5d\xa8\xc2\xb7\x42\xad\xec\x19\xfb\xca\xc6\x64\xb3\xa4";
  2998. for (i = 0; i < (int)sizeof(large_input); i++) {
  2999. large_input[i] = (byte)(i & 0xFF);
  3000. }
  3001. times = 100;
  3002. for (i = 0; i < times; ++i) {
  3003. ret = wc_Sha3_384_Update(&sha, (byte*)large_input,
  3004. (word32)sizeof(large_input));
  3005. if (ret != 0)
  3006. ERROR_OUT(-2806, exit);
  3007. }
  3008. ret = wc_Sha3_384_Final(&sha, hash);
  3009. if (ret != 0)
  3010. ERROR_OUT(-2807, exit);
  3011. if (XMEMCMP(hash, large_digest, WC_SHA3_384_DIGEST_SIZE) != 0)
  3012. ERROR_OUT(-2808, exit);
  3013. } /* END LARGE HASH TEST */
  3014. #endif /* NO_LARGE_HASH_TEST */
  3015. exit:
  3016. wc_Sha3_384_Free(&sha);
  3017. return ret;
  3018. }
  3019. #endif /* WOLFSSL_NOSHA3_384 */
  3020. #ifndef WOLFSSL_NOSHA3_512
  3021. static int sha3_512_test(void)
  3022. {
  3023. wc_Sha3 sha;
  3024. byte hash[WC_SHA3_512_DIGEST_SIZE];
  3025. byte hashcopy[WC_SHA3_512_DIGEST_SIZE];
  3026. testVector a, b, c;
  3027. testVector test_sha[3];
  3028. int ret;
  3029. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3030. a.input = "";
  3031. a.output = "\xa6\x9f\x73\xcc\xa2\x3a\x9a\xc5\xc8\xb5\x67\xdc\x18\x5a\x75"
  3032. "\x6e\x97\xc9\x82\x16\x4f\xe2\x58\x59\xe0\xd1\xdc\xc1\x47\x5c"
  3033. "\x80\xa6\x15\xb2\x12\x3a\xf1\xf5\xf9\x4c\x11\xe3\xe9\x40\x2c"
  3034. "\x3a\xc5\x58\xf5\x00\x19\x9d\x95\xb6\xd3\xe3\x01\x75\x85\x86"
  3035. "\x28\x1d\xcd\x26";
  3036. a.inLen = XSTRLEN(a.input);
  3037. a.outLen = WC_SHA3_512_DIGEST_SIZE;
  3038. b.input = "abc";
  3039. b.output = "\xb7\x51\x85\x0b\x1a\x57\x16\x8a\x56\x93\xcd\x92\x4b\x6b\x09"
  3040. "\x6e\x08\xf6\x21\x82\x74\x44\xf7\x0d\x88\x4f\x5d\x02\x40\xd2"
  3041. "\x71\x2e\x10\xe1\x16\xe9\x19\x2a\xf3\xc9\x1a\x7e\xc5\x76\x47"
  3042. "\xe3\x93\x40\x57\x34\x0b\x4c\xf4\x08\xd5\xa5\x65\x92\xf8\x27"
  3043. "\x4e\xec\x53\xf0";
  3044. b.inLen = XSTRLEN(b.input);
  3045. b.outLen = WC_SHA3_512_DIGEST_SIZE;
  3046. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3047. c.output = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10\xfc\xa8"
  3048. "\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7\xec\x2f\x1e\x91"
  3049. "\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53\x02\xba\x1b\x0d\x8d\xc7"
  3050. "\x8c\x08\x63\x46\xb5\x33\xb4\x9c\x03\x0d\x99\xa2\x7d\xaf\x11"
  3051. "\x39\xd6\xe7\x5e";
  3052. c.inLen = XSTRLEN(c.input);
  3053. c.outLen = WC_SHA3_512_DIGEST_SIZE;
  3054. test_sha[0] = a;
  3055. test_sha[1] = b;
  3056. test_sha[2] = c;
  3057. ret = wc_InitSha3_512(&sha, HEAP_HINT, devId);
  3058. if (ret != 0)
  3059. return -2900;
  3060. for (i = 0; i < times; ++i) {
  3061. ret = wc_Sha3_512_Update(&sha, (byte*)test_sha[i].input,
  3062. (word32)test_sha[i].inLen);
  3063. if (ret != 0)
  3064. ERROR_OUT(-2901 - i, exit);
  3065. ret = wc_Sha3_512_GetHash(&sha, hashcopy);
  3066. if (ret != 0)
  3067. ERROR_OUT(-2902 - i, exit);
  3068. ret = wc_Sha3_512_Final(&sha, hash);
  3069. if (ret != 0)
  3070. ERROR_OUT(-2903 - i, exit);
  3071. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_512_DIGEST_SIZE) != 0)
  3072. ERROR_OUT(-2904 - i, exit);
  3073. if (XMEMCMP(hash, hashcopy, WC_SHA3_512_DIGEST_SIZE) != 0)
  3074. ERROR_OUT(-2905 - i, exit);
  3075. }
  3076. #ifndef NO_LARGE_HASH_TEST
  3077. /* BEGIN LARGE HASH TEST */ {
  3078. byte large_input[1024];
  3079. const char* large_digest =
  3080. "\x9c\x13\x26\xb6\x26\xb2\x94\x31\xbc\xf4\x34\xe9\x6f\xf2\xd6\x29"
  3081. "\x9a\xd0\x9b\x32\x63\x2f\x18\xa7\x5f\x23\xc9\x60\xc2\x32\x0c\xbc"
  3082. "\x57\x77\x33\xf1\x83\x81\x8a\xd3\x15\x7c\x93\xdc\x80\x9f\xed\x61"
  3083. "\x41\xa7\x5b\xfd\x32\x0e\x38\x15\xb0\x46\x3b\x7a\x4f\xfd\x44\x88";
  3084. for (i = 0; i < (int)sizeof(large_input); i++) {
  3085. large_input[i] = (byte)(i & 0xFF);
  3086. }
  3087. times = 100;
  3088. for (i = 0; i < times; ++i) {
  3089. ret = wc_Sha3_512_Update(&sha, (byte*)large_input,
  3090. (word32)sizeof(large_input));
  3091. if (ret != 0)
  3092. ERROR_OUT(-2906, exit);
  3093. }
  3094. ret = wc_Sha3_512_Final(&sha, hash);
  3095. if (ret != 0)
  3096. ERROR_OUT(-2907, exit);
  3097. if (XMEMCMP(hash, large_digest, WC_SHA3_512_DIGEST_SIZE) != 0)
  3098. ERROR_OUT(-2908, exit);
  3099. } /* END LARGE HASH TEST */
  3100. #endif /* NO_LARGE_HASH_TEST */
  3101. exit:
  3102. wc_Sha3_512_Free(&sha);
  3103. return ret;
  3104. }
  3105. #endif /* WOLFSSL_NOSHA3_512 */
  3106. WOLFSSL_TEST_SUBROUTINE int sha3_test(void)
  3107. {
  3108. int ret;
  3109. (void)ret;
  3110. #ifndef WOLFSSL_NOSHA3_224
  3111. if ((ret = sha3_224_test()) != 0)
  3112. return ret;
  3113. #endif
  3114. #ifndef WOLFSSL_NOSHA3_256
  3115. if ((ret = sha3_256_test()) != 0)
  3116. return ret;
  3117. #endif
  3118. #ifndef WOLFSSL_NOSHA3_384
  3119. if ((ret = sha3_384_test()) != 0)
  3120. return ret;
  3121. #endif
  3122. #ifndef WOLFSSL_NOSHA3_512
  3123. if ((ret = sha3_512_test()) != 0)
  3124. return ret;
  3125. #endif
  3126. return 0;
  3127. }
  3128. #endif /* WOLFSSL_SHA3 */
  3129. #ifdef WOLFSSL_SHAKE128
  3130. static int shake128_absorb_test(wc_Shake* sha)
  3131. {
  3132. byte hash[WC_SHA3_128_BLOCK_SIZE*2];
  3133. testVector a, b, c, d, e;
  3134. testVector test_sha[5];
  3135. int ret = 0;
  3136. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3137. byte large_input[1024];
  3138. const char* large_digest =
  3139. "\x2b\xd1\x69\x9f\xb3\x75\x40\x74\xb8\xb2\xd2\x0b\x92\x47\x9b\xfe"
  3140. "\xc9\x91\x48\xbe\xda\xa4\x09\xd7\x61\x35\x18\x05\x07\x71\xa5\x61"
  3141. "\x4d\xc4\x94\xad\xbe\x04\x7d\xad\x95\x2f\xeb\x2c\xc0\x10\x67\x43"
  3142. "\x40\xf1\x4a\x58\x1c\x54\xfa\x24\x1c\x1a\x4e\x8d\x9b\xbc\xea\xa7"
  3143. "\x32\xf2\x4c\xc7\x86\x05\x36\xdc\xb4\x42\xd8\x35\xd1\xb4\xa2\x79"
  3144. "\xa2\xe6\xee\x67\x4f\xbf\x2a\x93\x41\x88\x25\x56\x29\x90\x1a\x06"
  3145. "\xba\xfe\x9f\xa6\x1a\x74\xe8\x7e\x85\x4a\xc8\x58\x60\xb1\x7b\x18"
  3146. "\xdf\x77\x59\x46\x04\xc1\xff\x4b\x9b\xcb\xad\xfe\x91\x28\xf0\x01"
  3147. "\xc1\x33\xd0\x99\x99\x2e\x0c\x86\x84\x67\x4d\x37\xa4\x42\x45\x10"
  3148. "\xdc\x8f\xdb\x6f\xa6\x9b\xee\x8a\x60\xa5\x1f\x95\x3f\x8f\xf5\x31"
  3149. "\x4b\x1d\x48\x1e\x45\xff\x79\x5c\xbe\x72\xfc\x56\xed\x6d\x1a\x99"
  3150. "\x7f\x23\x7c\xd1\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b"
  3151. "\xa3\x60\x51\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41"
  3152. "\x77\x2c\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59"
  3153. "\x0d\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  3154. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4\xcb\x87\xd8\xb7\x9d\xa8"
  3155. "\xbf\xc5\x2e\x5e\xfc\xd3\x6c\x45\xd4\x5d\x72\x0f\x66\xeb\x67\x86"
  3156. "\xfa\x6c\xd6\x80\xa4\x23\xcb\x5d\xed\x3c\xde\xdc\x5b\x3d\xca\x95"
  3157. "\x43\x4b\xdc\xe8\x49\xd3\xe1\x01\xd4\xf1\xe4\x47\xcf\x56\xba\x71"
  3158. "\xb4\x69\xed\xe7\xdb\x0f\x89\xd6\xbb\xcd\x1a\xff\xb4\xbe\x72\x26"
  3159. "\xdc\x76\x79\xb3\x1a\x4b\xe6\x8d\x9b\x8e\xd9\xe9\xe6\xf9\xff\xa5";
  3160. a.input = "";
  3161. a.output = "\x7f\x9c\x2b\xa4\xe8\x8f\x82\x7d\x61\x60\x45\x50\x76\x05\x85"
  3162. "\x3e\xd7\x3b\x80\x93\xf6\xef\xbc\x88\xeb\x1a\x6e\xac\xfa\x66"
  3163. "\xef\x26\x3c\xb1\xee\xa9\x88\x00\x4b\x93\x10\x3c\xfb\x0a\xee"
  3164. "\xfd\x2a\x68\x6e\x01\xfa\x4a\x58\xe8\xa3\x63\x9c\xa8\xa1\xe3"
  3165. "\xf9\xae\x57\xe2\x35\xb8\xcc\x87\x3c\x23\xdc\x62\xb8\xd2\x60"
  3166. "\x16\x9a\xfa\x2f\x75\xab\x91\x6a\x58\xd9\x74\x91\x88\x35\xd2"
  3167. "\x5e\x6a\x43\x50\x85\xb2\xba\xdf\xd6\xdf\xaa\xc3\x59\xa5\xef"
  3168. "\xbb\x7b\xcc\x4b\x59\xd5\x38\xdf\x9a\x04\x30\x2e\x10\xc8\xbc"
  3169. "\x1c\xbf\x1a\x0b\x3a\x51\x20\xea\x17\xcd\xa7\xcf\xad\x76\x5f"
  3170. "\x56\x23\x47\x4d\x36\x8c\xcc\xa8\xaf\x00\x07\xcd\x9f\x5e\x4c"
  3171. "\x84\x9f\x16\x7a\x58\x0b\x14\xaa\xbd\xef\xae\xe7\xee\xf4\x7c"
  3172. "\xb0\xfc\xa9";
  3173. a.inLen = XSTRLEN(a.input);
  3174. a.outLen = WC_SHA3_128_BLOCK_SIZE;
  3175. b.input = "abc";
  3176. b.output = "\x58\x81\x09\x2d\xd8\x18\xbf\x5c\xf8\xa3\xdd\xb7\x93\xfb\xcb"
  3177. "\xa7\x40\x97\xd5\xc5\x26\xa6\xd3\x5f\x97\xb8\x33\x51\x94\x0f"
  3178. "\x2c\xc8\x44\xc5\x0a\xf3\x2a\xcd\x3f\x2c\xdd\x06\x65\x68\x70"
  3179. "\x6f\x50\x9b\xc1\xbd\xde\x58\x29\x5d\xae\x3f\x89\x1a\x9a\x0f"
  3180. "\xca\x57\x83\x78\x9a\x41\xf8\x61\x12\x14\xce\x61\x23\x94\xdf"
  3181. "\x28\x6a\x62\xd1\xa2\x25\x2a\xa9\x4d\xb9\xc5\x38\x95\x6c\x71"
  3182. "\x7d\xc2\xbe\xd4\xf2\x32\xa0\x29\x4c\x85\x7c\x73\x0a\xa1\x60"
  3183. "\x67\xac\x10\x62\xf1\x20\x1f\xb0\xd3\x77\xcf\xb9\xcd\xe4\xc6"
  3184. "\x35\x99\xb2\x7f\x34\x62\xbb\xa4\xa0\xed\x29\x6c\x80\x1f\x9f"
  3185. "\xf7\xf5\x73\x02\xbb\x30\x76\xee\x14\x5f\x97\xa3\x2a\xe6\x8e"
  3186. "\x76\xab\x66\xc4\x8d\x51\x67\x5b\xd4\x9a\xcc\x29\x08\x2f\x56"
  3187. "\x47\x58\x4e";
  3188. b.inLen = XSTRLEN(b.input);
  3189. b.outLen = WC_SHA3_128_BLOCK_SIZE;
  3190. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3191. c.output = "\x1a\x96\x18\x2b\x50\xfb\x8c\x7e\x74\xe0\xa7\x07\x78\x8f\x55"
  3192. "\xe9\x82\x09\xb8\xd9\x1f\xad\xe8\xf3\x2f\x8d\xd5\xcf\xf7\xbf"
  3193. "\x21\xf5\x4e\xe5\xf1\x95\x50\x82\x5a\x6e\x07\x00\x30\x51\x9e"
  3194. "\x94\x42\x63\xac\x1c\x67\x65\x28\x70\x65\x62\x1f\x9f\xcb\x32"
  3195. "\x01\x72\x3e\x32\x23\xb6\x3a\x46\xc2\x93\x8a\xa9\x53\xba\x84"
  3196. "\x01\xd0\xea\x77\xb8\xd2\x64\x90\x77\x55\x66\x40\x7b\x95\x67"
  3197. "\x3c\x0f\x4c\xc1\xce\x9f\xd9\x66\x14\x8d\x7e\xfd\xff\x26\xbb"
  3198. "\xf9\xf4\x8a\x21\xc6\xda\x35\xbf\xaa\x54\x56\x54\xf7\x0a\xe5"
  3199. "\x86\xff\x10\x13\x14\x20\x77\x14\x83\xec\x92\xed\xab\x40\x8c"
  3200. "\x76\x7b\xf4\xc5\xb4\xff\xfa\xa8\x0c\x8c\xa2\x14\xd8\x4c\x4d"
  3201. "\xc7\x00\xd0\xc5\x06\x30\xb2\xff\xc3\x79\x3e\xa4\xd8\x72\x58"
  3202. "\xb4\xc9\x54";
  3203. c.inLen = XSTRLEN(c.input);
  3204. c.outLen = WC_SHA3_128_BLOCK_SIZE;
  3205. /* Taken from NIST CAVP test vectors - full rate output. */
  3206. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  3207. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  3208. "\x85\xe0";
  3209. d.output = "\x93\x68\xf0\x15\x10\x92\x44\xeb\x02\x47\xfa\x3a\x0e\x57\xf5"
  3210. "\x2e\xa7\xd9\xeb\xa2\x3d\xae\x7a\x19\x7f\x0a\x29\xe9\x22\x55"
  3211. "\x06\x05\x98\x16\xb7\x84\x48\xb6\x49\x7a\x76\xeb\x96\x2d\xb3"
  3212. "\xf8\x4d\x37\x60\xf1\xfe\xb4\xbd\xc1\xfd\x4a\xc9\x4e\x91\x7a"
  3213. "\xc2\xea\x5e\x4f\x38\x37\x4a\xa5\x6e\x4f\x47\x67\xb8\xd7\x83"
  3214. "\x1b\x2d\x51\x49\x5a\xb8\xea\xb7\xc9\x82\x20\xaf\x13\x41\x5a"
  3215. "\x59\xbb\x7c\x17\x7a\xcd\x62\x8e\xf0\xff\xe3\x6c\xeb\x18\x59"
  3216. "\x5d\x14\x4c\xbf\x25\xef\xc0\x6c\xd9\x56\xa5\x78\x20\x6e\xa8"
  3217. "\xf9\x14\x5e\xf9\xce\x19\x50\x6a\x9d\x04\x4e\xc7\x00\x79\x9f"
  3218. "\xa1\x41\x9b\xaf\x60\x52\xc0\xc1\xb4\x45\xf8\x35\x17\x57\xb0"
  3219. "\xd0\x22\x87\x21\x89\xe2\xc0\x27\x3f\x82\xd9\x69\x69\x66\x3e"
  3220. "\x55\x4d\x09";
  3221. d.inLen = 32;
  3222. d.outLen = WC_SHA3_128_BLOCK_SIZE;
  3223. /* Taken from NIST CAVP test vectors - more than one output block. */
  3224. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  3225. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  3226. "\x47\xe4";
  3227. e.output = "\xe1\x7e\xab\x0d\xa4\x04\xf9\xb6\xac\xc0\x84\x97\x2f\xc5\x79"
  3228. "\xe8\x6d\xaa\x76\x10\xa5\xe1\x7c\x23\x2f\x79\x19\x83\x96\xfd"
  3229. "\x01\xc2\x4c\x34\xbb\x54\xf4\xb0\x1e\xf7\x40\xb4\x25\x33\x4a"
  3230. "\x55\xdd\x24\x81\x3d\xc8\xea\x86\xf5\x6e\xf7\x27\x67\x26\x2b"
  3231. "\xf2\x25\x74\x8c\xcc\x3d\x9f\x48\x6f\xfb\x72\x8f\x4e\xad\x29"
  3232. "\x60\xc9\x6c\x3e\x44\x63\x86\xea\xce\x21\x9c\x84\x28\x16\x11"
  3233. "\x63\x58\xb0\xf4\x2d\x7d\xff\xf7\xdd\x24\x11\xfa\x2a\x56\x79"
  3234. "\xfd\x7a\x94\x77\x45\x75\xba\xf9\xfc\xad\x68\xa1\x9e\x30\xd1"
  3235. "\x49\xb0\x59\xb5\x9c\x44\x6c\x4e\xdc\xa5\x9b\xc5\xa4\x79\x9d"
  3236. "\xc4\x65\xaa\x9e\x78\x2c\xed\x9f\x21\xc5\x5d\xe2\x42\xdd\x25"
  3237. "\xd0\xd9\xde\x60\xd0\x9f\xf8\x6a\xba\xf3\xa0\x3a\x76\x71\xb3"
  3238. "\x05\x42\xdf\xbe\x72\xfc\x56\xed\x6d\x1a\x99\x7f\x23\x7c\xd1"
  3239. "\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b\xa3\x60\x51"
  3240. "\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41\x77\x2c"
  3241. "\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59\x0d"
  3242. "\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  3243. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4\xcb\x87\xd8\xb7\x9d"
  3244. "\xa8\xbf\xc5\x2e\x5e\xfc\xd3\x6c\x45\xd4\x5d\x72\x0f\x66\xeb"
  3245. "\x67\x86\xfa\x6c\xd6\x80\xa4\x23\xcb\x5d\xed\x3c\xde\xdc\x5b"
  3246. "\x3d\xca\x95\x43\x4b\xdc\xe8\x49\xd3\xe1\x01\xd4\xf1\xe4\x47"
  3247. "\xcf\x56\xba\x71\xb4\x69\xed\xe7\xdb\x0f\x89\xd6\xbb\xcd\x1a"
  3248. "\xff\xb4\xbe\x72\x26\xdc\x76\x79\xb3\x1a\x4b\xe6\x8d\x9b\x8e"
  3249. "\xd9\xe9\xe6\xf9\xff\xa5";
  3250. e.inLen = 32;
  3251. e.outLen = 2 * WC_SHA3_128_BLOCK_SIZE;
  3252. test_sha[0] = a;
  3253. test_sha[1] = b;
  3254. test_sha[2] = c;
  3255. test_sha[3] = d;
  3256. test_sha[4] = e;
  3257. for (i = 0; i < times; ++i) {
  3258. ret = wc_InitShake128(sha, HEAP_HINT, devId);
  3259. if (ret != 0)
  3260. ERROR_OUT(-3100 - i, exit);
  3261. ret = wc_Shake128_Absorb(sha, (byte*)test_sha[i].input,
  3262. (word32)test_sha[i].inLen);
  3263. if (ret != 0)
  3264. ERROR_OUT(-3101 - i, exit);
  3265. ret = wc_Shake128_SqueezeBlocks(sha, hash,
  3266. (word32)test_sha[i].outLen / WC_SHA3_128_BLOCK_SIZE);
  3267. if (ret != 0)
  3268. ERROR_OUT(-3102 - i, exit);
  3269. if (XMEMCMP(hash, test_sha[i].output, (word32)test_sha[i].outLen) != 0)
  3270. ERROR_OUT(-3103 - i, exit);
  3271. }
  3272. #ifndef NO_LARGE_HASH_TEST
  3273. /* BEGIN LARGE HASH TEST */ {
  3274. for (i = 0; i < (int)sizeof(large_input); i++) {
  3275. large_input[i] = (byte)(i & 0xFF);
  3276. }
  3277. ret = wc_InitShake128(sha, HEAP_HINT, devId);
  3278. if (ret != 0)
  3279. ERROR_OUT(-3104, exit);
  3280. /* Absorb is non-incremental. */
  3281. ret = wc_Shake128_Absorb(sha, (byte*)large_input,
  3282. (word32)sizeof(large_input));
  3283. if (ret != 0)
  3284. ERROR_OUT(-3105, exit);
  3285. /* Able to squeeze out blocks incrementally. */
  3286. ret = wc_Shake128_SqueezeBlocks(sha, hash, 1);
  3287. if (ret != 0)
  3288. ERROR_OUT(-3106, exit);
  3289. ret = wc_Shake128_SqueezeBlocks(sha, hash,
  3290. ((word32)sizeof(hash) / WC_SHA3_128_BLOCK_SIZE) - 1);
  3291. if (ret != 0)
  3292. ERROR_OUT(-3106, exit);
  3293. if (XMEMCMP(hash, large_digest, sizeof(hash)) != 0)
  3294. ERROR_OUT(-3107, exit);
  3295. } /* END LARGE HASH TEST */
  3296. #endif /* NO_LARGE_HASH_TEST */
  3297. exit:
  3298. return ret;
  3299. }
  3300. WOLFSSL_TEST_SUBROUTINE int shake128_test(void)
  3301. {
  3302. wc_Shake sha;
  3303. byte hash[250];
  3304. testVector a, b, c, d, e;
  3305. testVector test_sha[5];
  3306. int ret = 0;
  3307. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3308. byte large_input[1024];
  3309. const char* large_digest =
  3310. "\x88\xd7\x0e\x86\x46\x72\x6b\x3d\x7d\x22\xe1\xa9\x2d\x02\xdb\x35"
  3311. "\x92\x4f\x1b\x03\x90\xee\xa3\xce\xd1\x3a\x08\x3a\xd7\x4e\x10\xdf"
  3312. "\x09\x67\x33\x35\x4f\xdd\x38\x50\x5b\xcb\x75\xc7\xba\x65\xe5\xe8"
  3313. "\xb8\x76\xde\xc5\xee\xd7\xf1\x65\x93\x4e\x5e\xc4\xb1\xd7\x6b\xee"
  3314. "\x4b\x57\x48\xf5\x38\x49\x9e\x45\xa0\xf7\x32\xe9\x05\x26\x6a\x10"
  3315. "\x70\xd4\x7c\x19\x01\x1f\x6d\x37\xba\x7b\x74\xc2\xbc\xb6\xbc\x74"
  3316. "\xa3\x66\x6c\x9b\x11\x84\x9d\x4a\x36\xbc\x8a\x0d\x4c\xe3\x39\xfa"
  3317. "\xfa\x1b";
  3318. a.input = "";
  3319. a.output = "\x7f\x9c\x2b\xa4\xe8\x8f\x82\x7d\x61\x60\x45\x50\x76\x05\x85"
  3320. "\x3e\xd7\x3b\x80\x93\xf6\xef\xbc\x88\xeb\x1a\x6e\xac\xfa\x66"
  3321. "\xef\x26\x3c\xb1\xee\xa9\x88\x00\x4b\x93\x10\x3c\xfb\x0a\xee"
  3322. "\xfd\x2a\x68\x6e\x01\xfa\x4a\x58\xe8\xa3\x63\x9c\xa8\xa1\xe3"
  3323. "\xf9\xae\x57\xe2\x35\xb8\xcc\x87\x3c\x23\xdc\x62\xb8\xd2\x60"
  3324. "\x16\x9a\xfa\x2f\x75\xab\x91\x6a\x58\xd9\x74\x91\x88\x35\xd2"
  3325. "\x5e\x6a\x43\x50\x85\xb2\xba\xdf\xd6\xdf\xaa\xc3\x59\xa5\xef"
  3326. "\xbb\x7b\xcc\x4b\x59\xd5\x38\xdf\x9a";
  3327. a.inLen = XSTRLEN(a.input);
  3328. a.outLen = 114;
  3329. b.input = "abc";
  3330. b.output = "\x58\x81\x09\x2d\xd8\x18\xbf\x5c\xf8\xa3\xdd\xb7\x93\xfb\xcb"
  3331. "\xa7\x40\x97\xd5\xc5\x26\xa6\xd3\x5f\x97\xb8\x33\x51\x94\x0f"
  3332. "\x2c\xc8\x44\xc5\x0a\xf3\x2a\xcd\x3f\x2c\xdd\x06\x65\x68\x70"
  3333. "\x6f\x50\x9b\xc1\xbd\xde\x58\x29\x5d\xae\x3f\x89\x1a\x9a\x0f"
  3334. "\xca\x57\x83\x78\x9a\x41\xf8\x61\x12\x14\xce\x61\x23\x94\xdf"
  3335. "\x28\x6a\x62\xd1\xa2\x25\x2a\xa9\x4d\xb9\xc5\x38\x95\x6c\x71"
  3336. "\x7d\xc2\xbe\xd4\xf2\x32\xa0\x29\x4c\x85\x7c\x73\x0a\xa1\x60"
  3337. "\x67\xac\x10\x62\xf1\x20\x1f\xb0\xd3";
  3338. b.inLen = XSTRLEN(b.input);
  3339. b.outLen = 114;
  3340. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3341. c.output = "\x1a\x96\x18\x2b\x50\xfb\x8c\x7e\x74\xe0\xa7\x07\x78\x8f\x55"
  3342. "\xe9\x82\x09\xb8\xd9\x1f\xad\xe8\xf3\x2f\x8d\xd5\xcf\xf7\xbf"
  3343. "\x21\xf5\x4e\xe5\xf1\x95\x50\x82\x5a\x6e\x07\x00\x30\x51\x9e"
  3344. "\x94\x42\x63\xac\x1c\x67\x65\x28\x70\x65\x62\x1f\x9f\xcb\x32"
  3345. "\x01\x72\x3e\x32\x23\xb6\x3a\x46\xc2\x93\x8a\xa9\x53\xba\x84"
  3346. "\x01\xd0\xea\x77\xb8\xd2\x64\x90\x77\x55\x66\x40\x7b\x95\x67"
  3347. "\x3c\x0f\x4c\xc1\xce\x9f\xd9\x66\x14\x8d\x7e\xfd\xff\x26\xbb"
  3348. "\xf9\xf4\x8a\x21\xc6\xda\x35\xbf\xaa";
  3349. c.inLen = XSTRLEN(c.input);
  3350. c.outLen = 114;
  3351. /* Taken from NIST CAVP test vectors - full rate output. */
  3352. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  3353. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  3354. "\x85\xe0";
  3355. d.output = "\x93\x68\xf0\x15\x10\x92\x44\xeb\x02\x47\xfa\x3a\x0e\x57\xf5"
  3356. "\x2e\xa7\xd9\xeb\xa2\x3d\xae\x7a\x19\x7f\x0a\x29\xe9\x22\x55"
  3357. "\x06\x05\x98\x16\xb7\x84\x48\xb6\x49\x7a\x76\xeb\x96\x2d\xb3"
  3358. "\xf8\x4d\x37\x60\xf1\xfe\xb4\xbd\xc1\xfd\x4a\xc9\x4e\x91\x7a"
  3359. "\xc2\xea\x5e\x4f\x38\x37\x4a\xa5\x6e\x4f\x47\x67\xb8\xd7\x83"
  3360. "\x1b\x2d\x51\x49\x5a\xb8\xea\xb7\xc9\x82\x20\xaf\x13\x41\x5a"
  3361. "\x59\xbb\x7c\x17\x7a\xcd\x62\x8e\xf0\xff\xe3\x6c\xeb\x18\x59"
  3362. "\x5d\x14\x4c\xbf\x25\xef\xc0\x6c\xd9\x56\xa5\x78\x20\x6e\xa8"
  3363. "\xf9\x14\x5e\xf9\xce\x19\x50\x6a\x9d\x04\x4e\xc7\x00\x79\x9f"
  3364. "\xa1";
  3365. d.inLen = 32;
  3366. d.outLen = 136;
  3367. /* Taken from NIST CAVP test vectors - more than one output block. */
  3368. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  3369. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  3370. "\x47\xe4";
  3371. e.output = "\xe1\x7e\xab\x0d\xa4\x04\xf9\xb6\xac\xc0\x84\x97\x2f\xc5\x79"
  3372. "\xe8\x6d\xaa\x76\x10\xa5\xe1\x7c\x23\x2f\x79\x19\x83\x96\xfd"
  3373. "\x01\xc2\x4c\x34\xbb\x54\xf4\xb0\x1e\xf7\x40\xb4\x25\x33\x4a"
  3374. "\x55\xdd\x24\x81\x3d\xc8\xea\x86\xf5\x6e\xf7\x27\x67\x26\x2b"
  3375. "\xf2\x25\x74\x8c\xcc\x3d\x9f\x48\x6f\xfb\x72\x8f\x4e\xad\x29"
  3376. "\x60\xc9\x6c\x3e\x44\x63\x86\xea\xce\x21\x9c\x84\x28\x16\x11"
  3377. "\x63\x58\xb0\xf4\x2d\x7d\xff\xf7\xdd\x24\x11\xfa\x2a\x56\x79"
  3378. "\xfd\x7a\x94\x77\x45\x75\xba\xf9\xfc\xad\x68\xa1\x9e\x30\xd1"
  3379. "\x49\xb0\x59\xb5\x9c\x44\x6c\x4e\xdc\xa5\x9b\xc5\xa4\x79\x9d"
  3380. "\xc4\x65\xaa\x9e\x78\x2c\xed\x9f\x21\xc5\x5d\xe2\x42\xdd\x25"
  3381. "\xd0\xd9\xde\x60\xd0\x9f\xf8\x6a\xba\xf3\xa0\x3a\x76\x71\xb3"
  3382. "\x05\x42\xdf\xbe\x72\xfc\x56\xed\x6d\x1a\x99\x7f\x23\x7c\xd1"
  3383. "\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b\xa3\x60\x51"
  3384. "\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41\x77\x2c"
  3385. "\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59\x0d"
  3386. "\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  3387. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4";
  3388. e.inLen = 32;
  3389. e.outLen = 250;
  3390. test_sha[0] = a;
  3391. test_sha[1] = b;
  3392. test_sha[2] = c;
  3393. test_sha[3] = d;
  3394. test_sha[4] = e;
  3395. ret = wc_InitShake128(&sha, HEAP_HINT, devId);
  3396. if (ret != 0)
  3397. return -3100;
  3398. for (i = 0; i < times; ++i) {
  3399. ret = wc_Shake128_Update(&sha, (byte*)test_sha[i].input,
  3400. (word32)test_sha[i].inLen);
  3401. if (ret != 0)
  3402. ERROR_OUT(-3101 - i, exit);
  3403. ret = wc_Shake128_Final(&sha, hash, (word32)test_sha[i].outLen);
  3404. if (ret != 0)
  3405. ERROR_OUT(-3102 - i, exit);
  3406. if (XMEMCMP(hash, test_sha[i].output, test_sha[i].outLen) != 0)
  3407. ERROR_OUT(-3103 - i, exit);
  3408. }
  3409. #ifndef NO_LARGE_HASH_TEST
  3410. /* BEGIN LARGE HASH TEST */ {
  3411. for (i = 0; i < (int)sizeof(large_input); i++) {
  3412. large_input[i] = (byte)(i & 0xFF);
  3413. }
  3414. times = 100;
  3415. for (i = 0; i < times; ++i) {
  3416. ret = wc_Shake128_Update(&sha, (byte*)large_input,
  3417. (word32)sizeof(large_input));
  3418. if (ret != 0)
  3419. ERROR_OUT(-3104, exit);
  3420. }
  3421. ret = wc_Shake128_Final(&sha, hash, (word32)sizeof(hash));
  3422. if (ret != 0)
  3423. ERROR_OUT(-3105, exit);
  3424. if (XMEMCMP(hash, large_digest, 114) != 0)
  3425. ERROR_OUT(-3106, exit);
  3426. } /* END LARGE HASH TEST */
  3427. #endif /* NO_LARGE_HASH_TEST */
  3428. ret = shake128_absorb_test(&sha);
  3429. exit:
  3430. wc_Shake128_Free(&sha);
  3431. return ret;
  3432. }
  3433. #endif
  3434. #ifdef WOLFSSL_SHAKE256
  3435. static int shake256_absorb_test(wc_Shake* sha, byte *large_input_buf,
  3436. size_t large_input_buf_size)
  3437. {
  3438. byte hash[WC_SHA3_256_BLOCK_SIZE*2];
  3439. testVector a, b, c, d, e;
  3440. testVector test_sha[5];
  3441. int ret = 0;
  3442. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3443. const char* large_digest =
  3444. "\x21\x25\x8e\xae\x6e\x4f\xa7\xe1\xb9\x6d\xa7\xc9\x7d\x46\x03\x69"
  3445. "\x29\x0d\x81\x49\xba\x5d\xaf\x37\xfd\xeb\x25\x52\x1d\xd9\xbd\x65"
  3446. "\xfa\x99\xb9\xd1\x70\x6b\xeb\xd4\xc1\x2c\xea\x24\x20\x27\xa7\xcd"
  3447. "\xfa\xe1\x81\xd9\xd5\xc1\x1c\xc7\xe9\x70\xc3\xc7\x21\x6f\x32\x22"
  3448. "\xe3\x27\xdb\x58\x5e\xea\x18\x2d\x63\x4d\x14\x6c\x94\xcf\x2b\x7e"
  3449. "\x6e\x2a\x74\xf3\xe0\xac\xb3\xb2\xcc\xef\x38\xe9\xe7\x35\xb3\xc5"
  3450. "\x77\x9d\xff\xe3\x08\x8e\xf8\x2c\x89\xbb\x45\x22\x16\x99\x91\xc0"
  3451. "\xe7\x71\x57\x75\xc5\xb1\xc6\xaf\x27\xcb\x64\x8c\xc4\xee\x3d\x5f"
  3452. "\x4c\x35\xfb\x1c\xf3\xf8\x0e\xfd\x5e\xfc\x07\xd8\x4d\x55\x32\x49"
  3453. "\x45\x0d\xab\x4a\x49\xc4\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93"
  3454. "\x7a\xe6\x6b\xb4\x36\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43"
  3455. "\x2f\x3b\xfc\x09\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48"
  3456. "\x3d\x0e\xda\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08"
  3457. "\xd9\xdc\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a"
  3458. "\xb7\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  3459. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c\x68\x1b\x30\xdd\xc4\xe6"
  3460. "\x83\x8b\x0f\x23\x58\x7e\x06\x5f\x4a\x2b\xed\xc9\x6c\x97\x68\x44";
  3461. a.input = "";
  3462. a.output = "\x46\xb9\xdd\x2b\x0b\xa8\x8d\x13\x23\x3b\x3f\xeb\x74\x3e\xeb"
  3463. "\x24\x3f\xcd\x52\xea\x62\xb8\x1b\x82\xb5\x0c\x27\x64\x6e\xd5"
  3464. "\x76\x2f\xd7\x5d\xc4\xdd\xd8\xc0\xf2\x00\xcb\x05\x01\x9d\x67"
  3465. "\xb5\x92\xf6\xfc\x82\x1c\x49\x47\x9a\xb4\x86\x40\x29\x2e\xac"
  3466. "\xb3\xb7\xc4\xbe\x14\x1e\x96\x61\x6f\xb1\x39\x57\x69\x2c\xc7"
  3467. "\xed\xd0\xb4\x5a\xe3\xdc\x07\x22\x3c\x8e\x92\x93\x7b\xef\x84"
  3468. "\xbc\x0e\xab\x86\x28\x53\x34\x9e\xc7\x55\x46\xf5\x8f\xb7\xc2"
  3469. "\x77\x5c\x38\x46\x2c\x50\x10\xd8\x46\xc1\x85\xc1\x51\x11\xe5"
  3470. "\x95\x52\x2a\x6b\xcd\x16\xcf\x86\xf3\xd1\x22\x10\x9e\x3b\x1f"
  3471. "\xdd";
  3472. a.inLen = XSTRLEN(a.input);
  3473. a.outLen = WC_SHA3_256_BLOCK_SIZE;
  3474. b.input = "abc";
  3475. b.output = "\x48\x33\x66\x60\x13\x60\xa8\x77\x1c\x68\x63\x08\x0c\xc4\x11"
  3476. "\x4d\x8d\xb4\x45\x30\xf8\xf1\xe1\xee\x4f\x94\xea\x37\xe7\x8b"
  3477. "\x57\x39\xd5\xa1\x5b\xef\x18\x6a\x53\x86\xc7\x57\x44\xc0\x52"
  3478. "\x7e\x1f\xaa\x9f\x87\x26\xe4\x62\xa1\x2a\x4f\xeb\x06\xbd\x88"
  3479. "\x01\xe7\x51\xe4\x13\x85\x14\x12\x04\xf3\x29\x97\x9f\xd3\x04"
  3480. "\x7a\x13\xc5\x65\x77\x24\xad\xa6\x4d\x24\x70\x15\x7b\x3c\xdc"
  3481. "\x28\x86\x20\x94\x4d\x78\xdb\xcd\xdb\xd9\x12\x99\x3f\x09\x13"
  3482. "\xf1\x64\xfb\x2c\xe9\x51\x31\xa2\xd0\x9a\x3e\x6d\x51\xcb\xfc"
  3483. "\x62\x27\x20\xd7\xa7\x5c\x63\x34\xe8\xa2\xd7\xec\x71\xa7\xcc"
  3484. "\x29";
  3485. b.inLen = XSTRLEN(b.input);
  3486. b.outLen = WC_SHA3_256_BLOCK_SIZE;
  3487. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3488. c.output = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f\x6f\x87"
  3489. "\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b\xe5\xd4\xfd\x2e"
  3490. "\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59\xaa\x80\x60\xf1\xf9\xbc"
  3491. "\x99\x6c\x05\xac\xa3\xc6\x96\xa8\xb6\x62\x79\xdc\x67\x2c\x74"
  3492. "\x0b\xb2\x24\xec\x37\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55"
  3493. "\xf5\x1d\x97\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a"
  3494. "\xf2\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67\x60"
  3495. "\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4\x9d\xd3\xef\x7e\x18\x2b"
  3496. "\x15\x24\xdf\x82\xea\x1c\xef\xe1\xc6\xc3\x96\x61\x75\xf0\x22"
  3497. "\x8d";
  3498. c.inLen = XSTRLEN(c.input);
  3499. c.outLen = WC_SHA3_256_BLOCK_SIZE;
  3500. /* Taken from NIST CAVP test vectors - full rate output. */
  3501. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  3502. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  3503. "\x85\xe0";
  3504. d.output = "\x00\x64\x8a\xfb\xc5\xe6\x51\x64\x9d\xb1\xfd\x82\x93\x6b\x00"
  3505. "\xdb\xbc\x12\x2f\xb4\xc8\x77\x86\x0d\x38\x5c\x49\x50\xd5\x6d"
  3506. "\xe7\xe0\x96\xd6\x13\xd7\xa3\xf2\x7e\xd8\xf2\x63\x34\xb0\xcc"
  3507. "\xc1\x40\x7b\x41\xdc\xcb\x23\xdf\xaa\x52\x98\x18\xd1\x12\x5c"
  3508. "\xd5\x34\x80\x92\x52\x43\x66\xb8\x5f\xab\xb9\x7c\x6c\xd1\xe6"
  3509. "\x06\x6f\x45\x9b\xcc\x56\x6d\xa8\x7e\xc9\xb7\xba\x36\x79\x2d"
  3510. "\x11\x8a\xc3\x9a\x4c\xce\xf6\x19\x2b\xbf\x3a\x54\xaf\x18\xe5"
  3511. "\x7b\x0c\x14\x61\x01\xf6\xae\xaa\x82\x2b\xc4\xb4\xc9\x70\x8b"
  3512. "\x09\xf0\xb3\xba\xb4\x1b\xcc\xe9\x64\xd9\x99\xd1\x10\x7b\xd7"
  3513. "\xc2";
  3514. d.inLen = 32;
  3515. d.outLen = WC_SHA3_256_BLOCK_SIZE;
  3516. /* Taken from NIST CAVP test vectors - more than one output block. */
  3517. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  3518. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  3519. "\x47\xe4";
  3520. e.output = "\x2e\x97\x5f\x6a\x8a\x14\xf0\x70\x4d\x51\xb1\x36\x67\xd8\x19"
  3521. "\x5c\x21\x9f\x71\xe6\x34\x56\x96\xc4\x9f\xa4\xb9\xd0\x8e\x92"
  3522. "\x25\xd3\xd3\x93\x93\x42\x51\x52\xc9\x7e\x71\xdd\x24\x60\x1c"
  3523. "\x11\xab\xcf\xa0\xf1\x2f\x53\xc6\x80\xbd\x3a\xe7\x57\xb8\x13"
  3524. "\x4a\x9c\x10\xd4\x29\x61\x58\x69\x21\x7f\xdd\x58\x85\xc4\xdb"
  3525. "\x17\x49\x85\x70\x3a\x6d\x6d\xe9\x4a\x66\x7e\xac\x30\x23\x44"
  3526. "\x3a\x83\x37\xae\x1b\xc6\x01\xb7\x6d\x7d\x38\xec\x3c\x34\x46"
  3527. "\x31\x05\xf0\xd3\x94\x9d\x78\xe5\x62\xa0\x39\xe4\x46\x95\x48"
  3528. "\xb6\x09\x39\x5d\xe5\xa4\xfd\x43\xc4\x6c\xa9\xfd\x6e\xe2\x9a"
  3529. "\xda\x5e\xfc\x07\xd8\x4d\x55\x32\x49\x45\x0d\xab\x4a\x49\xc4"
  3530. "\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93\x7a\xe6\x6b\xb4\x36"
  3531. "\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43\x2f\x3b\xfc\x09"
  3532. "\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48\x3d\x0e\xda"
  3533. "\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08\xd9\xdc"
  3534. "\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a\xb7"
  3535. "\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  3536. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c\x68\x1b\x30\xdd\xc4"
  3537. "\xe6\x83\x8b\x0f\x23\x58\x7e\x06\x5f\x4a\x2b\xed\xc9\x6c\x97"
  3538. "\x68\x44";
  3539. e.inLen = 32;
  3540. e.outLen = 2 * WC_SHA3_256_BLOCK_SIZE;
  3541. test_sha[0] = a;
  3542. test_sha[1] = b;
  3543. test_sha[2] = c;
  3544. test_sha[3] = d;
  3545. test_sha[4] = e;
  3546. for (i = 0; i < times; ++i) {
  3547. ret = wc_InitShake256(sha, HEAP_HINT, devId);
  3548. if (ret != 0)
  3549. ERROR_OUT(-3100 - i, exit);
  3550. ret = wc_Shake256_Absorb(sha, (byte*)test_sha[i].input,
  3551. (word32)test_sha[i].inLen);
  3552. if (ret != 0)
  3553. ERROR_OUT(-3101 - i, exit);
  3554. ret = wc_Shake256_SqueezeBlocks(sha, hash,
  3555. (word32)test_sha[i].outLen / WC_SHA3_256_BLOCK_SIZE);
  3556. if (ret != 0)
  3557. ERROR_OUT(-3102 - i, exit);
  3558. if (XMEMCMP(hash, test_sha[i].output, (word32)test_sha[i].outLen) != 0)
  3559. ERROR_OUT(-3103 - i, exit);
  3560. }
  3561. #ifndef NO_LARGE_HASH_TEST
  3562. /* BEGIN LARGE HASH TEST */ {
  3563. for (i = 0; i < (int)large_input_buf_size; i++) {
  3564. large_input_buf[i] = (byte)(i & 0xFF);
  3565. }
  3566. ret = wc_InitShake256(sha, HEAP_HINT, devId);
  3567. if (ret != 0)
  3568. ERROR_OUT(-3104, exit);
  3569. /* Absorb is non-incremental. */
  3570. ret = wc_Shake256_Absorb(sha, large_input_buf,
  3571. (word32)large_input_buf_size);
  3572. if (ret != 0)
  3573. ERROR_OUT(-3105, exit);
  3574. /* Able to squeeze out blocks incrementally. */
  3575. ret = wc_Shake256_SqueezeBlocks(sha, hash, 1);
  3576. if (ret != 0)
  3577. ERROR_OUT(-3106, exit);
  3578. ret = wc_Shake256_SqueezeBlocks(sha, hash,
  3579. ((word32)sizeof(hash) / WC_SHA3_256_BLOCK_SIZE) - 1);
  3580. if (ret != 0)
  3581. ERROR_OUT(-3106, exit);
  3582. if (XMEMCMP(hash, large_digest, sizeof(hash)) != 0)
  3583. ERROR_OUT(-3107, exit);
  3584. } /* END LARGE HASH TEST */
  3585. #endif /* NO_LARGE_HASH_TEST */
  3586. exit:
  3587. return ret;
  3588. }
  3589. WOLFSSL_TEST_SUBROUTINE int shake256_test(void)
  3590. {
  3591. wc_Shake sha;
  3592. byte hash[250];
  3593. testVector a, b, c, d, e;
  3594. testVector test_sha[5];
  3595. int ret = 0;
  3596. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3597. #define SHAKE256_LARGE_INPUT_BUFSIZ 1024
  3598. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  3599. byte *large_input = NULL;
  3600. #else
  3601. byte large_input[SHAKE256_LARGE_INPUT_BUFSIZ];
  3602. #endif
  3603. const char* large_digest =
  3604. "\x90\x32\x4a\xcc\xd1\xdf\xb8\x0b\x79\x1f\xb8\xc8\x5b\x54\xc8\xe7"
  3605. "\x45\xf5\x60\x6b\x38\x26\xb2\x0a\xee\x38\x01\xf3\xd9\xfa\x96\x9f"
  3606. "\x6a\xd7\x15\xdf\xb6\xc2\xf4\x20\x33\x44\x55\xe8\x2a\x09\x2b\x68"
  3607. "\x2e\x18\x65\x5e\x65\x93\x28\xbc\xb1\x9e\xe2\xb1\x92\xea\x98\xac"
  3608. "\x21\xef\x4c\xe1\xb4\xb7\xbe\x81\x5c\x1d\xd3\xb7\x17\xe5\xbb\xc5"
  3609. "\x8c\x68\xb7\xfb\xac\x55\x8a\x9b\x4d\x91\xe4\x9f\x72\xbb\x6e\x38"
  3610. "\xaf\x21\x7d\x21\xaa\x98\x4e\x75\xc4\xb4\x1c\x7c\x50\x45\x54\xf9"
  3611. "\xea\x26";
  3612. a.input = "";
  3613. a.output = "\x46\xb9\xdd\x2b\x0b\xa8\x8d\x13\x23\x3b\x3f\xeb\x74\x3e\xeb"
  3614. "\x24\x3f\xcd\x52\xea\x62\xb8\x1b\x82\xb5\x0c\x27\x64\x6e\xd5"
  3615. "\x76\x2f\xd7\x5d\xc4\xdd\xd8\xc0\xf2\x00\xcb\x05\x01\x9d\x67"
  3616. "\xb5\x92\xf6\xfc\x82\x1c\x49\x47\x9a\xb4\x86\x40\x29\x2e\xac"
  3617. "\xb3\xb7\xc4\xbe\x14\x1e\x96\x61\x6f\xb1\x39\x57\x69\x2c\xc7"
  3618. "\xed\xd0\xb4\x5a\xe3\xdc\x07\x22\x3c\x8e\x92\x93\x7b\xef\x84"
  3619. "\xbc\x0e\xab\x86\x28\x53\x34\x9e\xc7\x55\x46\xf5\x8f\xb7\xc2"
  3620. "\x77\x5c\x38\x46\x2c\x50\x10\xd8\x46";
  3621. a.inLen = XSTRLEN(a.input);
  3622. a.outLen = 114;
  3623. b.input = "abc";
  3624. b.output = "\x48\x33\x66\x60\x13\x60\xa8\x77\x1c\x68\x63\x08\x0c\xc4\x11"
  3625. "\x4d\x8d\xb4\x45\x30\xf8\xf1\xe1\xee\x4f\x94\xea\x37\xe7\x8b"
  3626. "\x57\x39\xd5\xa1\x5b\xef\x18\x6a\x53\x86\xc7\x57\x44\xc0\x52"
  3627. "\x7e\x1f\xaa\x9f\x87\x26\xe4\x62\xa1\x2a\x4f\xeb\x06\xbd\x88"
  3628. "\x01\xe7\x51\xe4\x13\x85\x14\x12\x04\xf3\x29\x97\x9f\xd3\x04"
  3629. "\x7a\x13\xc5\x65\x77\x24\xad\xa6\x4d\x24\x70\x15\x7b\x3c\xdc"
  3630. "\x28\x86\x20\x94\x4d\x78\xdb\xcd\xdb\xd9\x12\x99\x3f\x09\x13"
  3631. "\xf1\x64\xfb\x2c\xe9\x51\x31\xa2\xd0";
  3632. b.inLen = XSTRLEN(b.input);
  3633. b.outLen = 114;
  3634. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3635. c.output = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f\x6f\x87"
  3636. "\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b\xe5\xd4\xfd\x2e"
  3637. "\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59\xaa\x80\x60\xf1\xf9\xbc"
  3638. "\x99\x6c\x05\xac\xa3\xc6\x96\xa8\xb6\x62\x79\xdc\x67\x2c\x74"
  3639. "\x0b\xb2\x24\xec\x37\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55"
  3640. "\xf5\x1d\x97\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a"
  3641. "\xf2\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67\x60"
  3642. "\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  3643. c.inLen = XSTRLEN(c.input);
  3644. c.outLen = 114;
  3645. /* Taken from NIST CAVP test vectors - full rate output. */
  3646. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  3647. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  3648. "\x85\xe0";
  3649. d.output = "\x00\x64\x8a\xfb\xc5\xe6\x51\x64\x9d\xb1\xfd\x82\x93\x6b\x00"
  3650. "\xdb\xbc\x12\x2f\xb4\xc8\x77\x86\x0d\x38\x5c\x49\x50\xd5\x6d"
  3651. "\xe7\xe0\x96\xd6\x13\xd7\xa3\xf2\x7e\xd8\xf2\x63\x34\xb0\xcc"
  3652. "\xc1\x40\x7b\x41\xdc\xcb\x23\xdf\xaa\x52\x98\x18\xd1\x12\x5c"
  3653. "\xd5\x34\x80\x92\x52\x43\x66\xb8\x5f\xab\xb9\x7c\x6c\xd1\xe6"
  3654. "\x06\x6f\x45\x9b\xcc\x56\x6d\xa8\x7e\xc9\xb7\xba\x36\x79\x2d"
  3655. "\x11\x8a\xc3\x9a\x4c\xce\xf6\x19\x2b\xbf\x3a\x54\xaf\x18\xe5"
  3656. "\x7b\x0c\x14\x61\x01\xf6\xae\xaa\x82\x2b\xc4\xb4\xc9\x70\x8b"
  3657. "\x09\xf0\xb3\xba\xb4\x1b\xcc\xe9\x64\xd9\x99\xd1\x10\x7b\xd7"
  3658. "\xc2";
  3659. d.inLen = 32;
  3660. d.outLen = 136;
  3661. /* Taken from NIST CAVP test vectors - more than one output block. */
  3662. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  3663. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  3664. "\x47\xe4";
  3665. e.output = "\x2e\x97\x5f\x6a\x8a\x14\xf0\x70\x4d\x51\xb1\x36\x67\xd8\x19"
  3666. "\x5c\x21\x9f\x71\xe6\x34\x56\x96\xc4\x9f\xa4\xb9\xd0\x8e\x92"
  3667. "\x25\xd3\xd3\x93\x93\x42\x51\x52\xc9\x7e\x71\xdd\x24\x60\x1c"
  3668. "\x11\xab\xcf\xa0\xf1\x2f\x53\xc6\x80\xbd\x3a\xe7\x57\xb8\x13"
  3669. "\x4a\x9c\x10\xd4\x29\x61\x58\x69\x21\x7f\xdd\x58\x85\xc4\xdb"
  3670. "\x17\x49\x85\x70\x3a\x6d\x6d\xe9\x4a\x66\x7e\xac\x30\x23\x44"
  3671. "\x3a\x83\x37\xae\x1b\xc6\x01\xb7\x6d\x7d\x38\xec\x3c\x34\x46"
  3672. "\x31\x05\xf0\xd3\x94\x9d\x78\xe5\x62\xa0\x39\xe4\x46\x95\x48"
  3673. "\xb6\x09\x39\x5d\xe5\xa4\xfd\x43\xc4\x6c\xa9\xfd\x6e\xe2\x9a"
  3674. "\xda\x5e\xfc\x07\xd8\x4d\x55\x32\x49\x45\x0d\xab\x4a\x49\xc4"
  3675. "\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93\x7a\xe6\x6b\xb4\x36"
  3676. "\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43\x2f\x3b\xfc\x09"
  3677. "\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48\x3d\x0e\xda"
  3678. "\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08\xd9\xdc"
  3679. "\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a\xb7"
  3680. "\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  3681. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c";
  3682. e.inLen = 32;
  3683. e.outLen = 250;
  3684. test_sha[0] = a;
  3685. test_sha[1] = b;
  3686. test_sha[2] = c;
  3687. test_sha[3] = d;
  3688. test_sha[4] = e;
  3689. ret = wc_InitShake256(&sha, HEAP_HINT, devId);
  3690. if (ret != 0)
  3691. return -3100;
  3692. for (i = 0; i < times; ++i) {
  3693. ret = wc_Shake256_Update(&sha, (byte*)test_sha[i].input,
  3694. (word32)test_sha[i].inLen);
  3695. if (ret != 0)
  3696. ERROR_OUT(-3101 - i, exit);
  3697. ret = wc_Shake256_Final(&sha, hash, (word32)test_sha[i].outLen);
  3698. if (ret != 0)
  3699. ERROR_OUT(-3102 - i, exit);
  3700. if (XMEMCMP(hash, test_sha[i].output, test_sha[i].outLen) != 0)
  3701. ERROR_OUT(-3103 - i, exit);
  3702. }
  3703. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  3704. large_input = (byte *)XMALLOC(SHAKE256_LARGE_INPUT_BUFSIZ, NULL,
  3705. DYNAMIC_TYPE_TMP_BUFFER);
  3706. if (large_input == NULL)
  3707. ERROR_OUT(-3107, exit);
  3708. #endif
  3709. #ifndef NO_LARGE_HASH_TEST
  3710. /* BEGIN LARGE HASH TEST */ {
  3711. for (i = 0; i < SHAKE256_LARGE_INPUT_BUFSIZ; i++) {
  3712. large_input[i] = (byte)(i & 0xFF);
  3713. }
  3714. times = 100;
  3715. for (i = 0; i < times; ++i) {
  3716. ret = wc_Shake256_Update(&sha, (byte*)large_input,
  3717. SHAKE256_LARGE_INPUT_BUFSIZ);
  3718. if (ret != 0)
  3719. ERROR_OUT(-3104, exit);
  3720. }
  3721. ret = wc_Shake256_Final(&sha, hash, (word32)sizeof(hash));
  3722. if (ret != 0)
  3723. ERROR_OUT(-3105, exit);
  3724. if (XMEMCMP(hash, large_digest, 114) != 0)
  3725. ERROR_OUT(-3106, exit);
  3726. } /* END LARGE HASH TEST */
  3727. #endif /* NO_LARGE_HASH_TEST */
  3728. ret = shake256_absorb_test(&sha, large_input, SHAKE256_LARGE_INPUT_BUFSIZ);
  3729. exit:
  3730. wc_Shake256_Free(&sha);
  3731. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  3732. if (large_input != NULL)
  3733. XFREE(large_input, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3734. #endif
  3735. return ret;
  3736. }
  3737. #endif
  3738. #ifndef NO_HASH_WRAPPER
  3739. WOLFSSL_TEST_SUBROUTINE int hash_test(void)
  3740. {
  3741. wc_HashAlg hash;
  3742. int ret, exp_ret;
  3743. int i, j;
  3744. int digestSz;
  3745. byte data[] = "0123456789abcdef0123456789abcdef0123456";
  3746. byte out[WC_MAX_DIGEST_SIZE];
  3747. byte hashOut[WC_MAX_DIGEST_SIZE];
  3748. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  3749. enum wc_HashType hashType;
  3750. #endif
  3751. enum wc_HashType typesGood[] = { WC_HASH_TYPE_MD5, WC_HASH_TYPE_SHA,
  3752. WC_HASH_TYPE_SHA224, WC_HASH_TYPE_SHA256,
  3753. WC_HASH_TYPE_SHA384, WC_HASH_TYPE_SHA512,
  3754. WC_HASH_TYPE_SHA3_224,
  3755. WC_HASH_TYPE_SHA3_256,
  3756. WC_HASH_TYPE_SHA3_384,
  3757. WC_HASH_TYPE_SHA3_512 };
  3758. enum wc_HashType typesNoImpl[] = {
  3759. #ifdef NO_MD5
  3760. WC_HASH_TYPE_MD5,
  3761. #endif
  3762. #ifdef NO_SHA
  3763. WC_HASH_TYPE_SHA,
  3764. #endif
  3765. #ifndef WOLFSSL_SHA224
  3766. WC_HASH_TYPE_SHA224,
  3767. #endif
  3768. #ifdef NO_SHA256
  3769. WC_HASH_TYPE_SHA256,
  3770. #endif
  3771. #ifndef WOLFSSL_SHA384
  3772. WC_HASH_TYPE_SHA384,
  3773. #endif
  3774. #ifndef WOLFSSL_SHA512
  3775. WC_HASH_TYPE_SHA512,
  3776. #endif
  3777. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_224)
  3778. WC_HASH_TYPE_SHA3_224,
  3779. #endif
  3780. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_256)
  3781. WC_HASH_TYPE_SHA3_256,
  3782. #endif
  3783. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_384)
  3784. WC_HASH_TYPE_SHA3_384,
  3785. #endif
  3786. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_512)
  3787. WC_HASH_TYPE_SHA3_512,
  3788. #endif
  3789. WC_HASH_TYPE_NONE
  3790. };
  3791. enum wc_HashType typesBad[] = { WC_HASH_TYPE_NONE, WC_HASH_TYPE_MD5_SHA,
  3792. WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4 };
  3793. enum wc_HashType typesHashBad[] = { WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4,
  3794. WC_HASH_TYPE_BLAKE2B,
  3795. WC_HASH_TYPE_NONE };
  3796. /* Parameter Validation testing. */
  3797. ret = wc_HashInit(NULL, WC_HASH_TYPE_SHA256);
  3798. if (ret != BAD_FUNC_ARG)
  3799. return -3200;
  3800. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  3801. if (ret != BAD_FUNC_ARG)
  3802. return -3201;
  3803. ret = wc_HashUpdate(&hash, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  3804. if (ret != BAD_FUNC_ARG)
  3805. return -3202;
  3806. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, data, sizeof(data));
  3807. if (ret != BAD_FUNC_ARG)
  3808. return -3203;
  3809. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, NULL);
  3810. if (ret != BAD_FUNC_ARG)
  3811. return -3204;
  3812. ret = wc_HashFinal(&hash, WC_HASH_TYPE_SHA256, NULL);
  3813. if (ret != BAD_FUNC_ARG)
  3814. return -3205;
  3815. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, out);
  3816. if (ret != BAD_FUNC_ARG)
  3817. return -3206;
  3818. /* Try invalid hash algorithms. */
  3819. for (i = 0; i < (int)(sizeof(typesBad)/sizeof(*typesBad)); i++) {
  3820. ret = wc_HashInit(&hash, typesBad[i]);
  3821. if (ret != BAD_FUNC_ARG)
  3822. return -3207 - i;
  3823. ret = wc_HashUpdate(&hash, typesBad[i], data, sizeof(data));
  3824. if (ret != BAD_FUNC_ARG)
  3825. return -3217 - i;
  3826. ret = wc_HashFinal(&hash, typesBad[i], out);
  3827. if (ret != BAD_FUNC_ARG)
  3828. return -3227 - i;
  3829. wc_HashFree(&hash, typesBad[i]);
  3830. }
  3831. /* Try valid hash algorithms. */
  3832. for (i = 0, j = 0; i < (int)(sizeof(typesGood)/sizeof(*typesGood)); i++) {
  3833. exp_ret = 0;
  3834. if (typesGood[i] == typesNoImpl[j]) {
  3835. /* Recognized but no implementation compiled in. */
  3836. exp_ret = HASH_TYPE_E;
  3837. j++;
  3838. }
  3839. ret = wc_HashInit(&hash, typesGood[i]);
  3840. if (ret != exp_ret)
  3841. return -3237 - i;
  3842. ret = wc_HashUpdate(&hash, typesGood[i], data, sizeof(data));
  3843. if (ret != exp_ret)
  3844. return -3247 - i;
  3845. ret = wc_HashFinal(&hash, typesGood[i], out);
  3846. if (ret != exp_ret)
  3847. return -3257 - i;
  3848. wc_HashFree(&hash, typesGood[i]);
  3849. digestSz = wc_HashGetDigestSize(typesGood[i]);
  3850. if (exp_ret < 0 && digestSz != exp_ret)
  3851. return -3267 - i;
  3852. if (exp_ret == 0 && digestSz < 0)
  3853. return -3277 - i;
  3854. if (exp_ret == 0) {
  3855. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut,
  3856. digestSz - 1);
  3857. if (ret != BUFFER_E)
  3858. return -3287 - i;
  3859. }
  3860. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut, digestSz);
  3861. if (ret != exp_ret)
  3862. return -3297 - i;
  3863. if (exp_ret == 0 && XMEMCMP(out, hashOut, digestSz) != 0)
  3864. return -3307 -i;
  3865. ret = wc_HashGetBlockSize(typesGood[i]);
  3866. if (exp_ret < 0 && ret != exp_ret)
  3867. return -3308 - i;
  3868. if (exp_ret == 0 && ret < 0)
  3869. return -3318 - i;
  3870. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  3871. ret = wc_HashGetOID(typesGood[i]);
  3872. if (ret == BAD_FUNC_ARG ||
  3873. (exp_ret == 0 && ret == HASH_TYPE_E) ||
  3874. (exp_ret != 0 && ret != HASH_TYPE_E)) {
  3875. return -3328 - i;
  3876. }
  3877. hashType = wc_OidGetHash(ret);
  3878. if (exp_ret == 0 && hashType != typesGood[i])
  3879. return -3338 - i;
  3880. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  3881. }
  3882. for (i = 0; i < (int)(sizeof(typesHashBad)/sizeof(*typesHashBad)); i++) {
  3883. ret = wc_Hash(typesHashBad[i], data, sizeof(data), out, sizeof(out));
  3884. if (ret != BAD_FUNC_ARG && ret != BUFFER_E)
  3885. return -3348 - i;
  3886. }
  3887. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  3888. ret = wc_HashGetOID(WC_HASH_TYPE_MD2);
  3889. #ifdef WOLFSSL_MD2
  3890. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3891. return -3358;
  3892. #else
  3893. if (ret != HASH_TYPE_E)
  3894. return -3359;
  3895. #endif
  3896. hashType = wc_OidGetHash(646); /* Md2h */
  3897. #ifdef WOLFSSL_MD2
  3898. if (hashType != WC_HASH_TYPE_MD2)
  3899. return -3360;
  3900. #else
  3901. if (hashType != WC_HASH_TYPE_NONE)
  3902. return -3361;
  3903. #endif
  3904. ret = wc_HashGetOID(WC_HASH_TYPE_MD5_SHA);
  3905. #ifndef NO_MD5
  3906. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3907. return -3362;
  3908. #else
  3909. if (ret != HASH_TYPE_E)
  3910. return -3363;
  3911. #endif
  3912. ret = wc_HashGetOID(WC_HASH_TYPE_MD4);
  3913. if (ret != BAD_FUNC_ARG)
  3914. return -3364;
  3915. ret = wc_HashGetOID(WC_HASH_TYPE_NONE);
  3916. if (ret != BAD_FUNC_ARG)
  3917. return -3365;
  3918. hashType = wc_OidGetHash(0);
  3919. if (hashType != WC_HASH_TYPE_NONE)
  3920. return -3366;
  3921. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  3922. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD2);
  3923. #ifdef WOLFSSL_MD2
  3924. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3925. return -3367;
  3926. #else
  3927. if (ret != HASH_TYPE_E)
  3928. return -3368;
  3929. #endif
  3930. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD2);
  3931. #ifdef WOLFSSL_MD2
  3932. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3933. return -3369;
  3934. #else
  3935. if (ret != HASH_TYPE_E)
  3936. return -3370;
  3937. #endif
  3938. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD4);
  3939. #ifndef NO_MD4
  3940. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3941. return -3371;
  3942. #else
  3943. if (ret != HASH_TYPE_E)
  3944. return -3372;
  3945. #endif
  3946. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD4);
  3947. #ifndef NO_MD4
  3948. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3949. return -3373;
  3950. #else
  3951. if (ret != HASH_TYPE_E)
  3952. return -3374;
  3953. #endif
  3954. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD5_SHA);
  3955. #if !defined(NO_MD5) && !defined(NO_SHA)
  3956. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3957. return -3375;
  3958. #else
  3959. if (ret != HASH_TYPE_E)
  3960. return -3376;
  3961. #endif
  3962. ret = wc_HashGetBlockSize(WC_HASH_TYPE_BLAKE2B);
  3963. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  3964. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3965. return -3377;
  3966. #else
  3967. if (ret != HASH_TYPE_E)
  3968. return -3378;
  3969. #endif
  3970. ret = wc_HashGetDigestSize(WC_HASH_TYPE_BLAKE2B);
  3971. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  3972. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3973. return -3379;
  3974. #else
  3975. if (ret != HASH_TYPE_E)
  3976. return -3380;
  3977. #endif
  3978. ret = wc_HashGetBlockSize(WC_HASH_TYPE_NONE);
  3979. if (ret != BAD_FUNC_ARG)
  3980. return -3381;
  3981. ret = wc_HashGetDigestSize(WC_HASH_TYPE_NONE);
  3982. if (ret != BAD_FUNC_ARG)
  3983. return -3382;
  3984. #if !defined(NO_CERTS) && !defined(NO_ASN)
  3985. #if defined(WOLFSSL_MD2) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  3986. ret = wc_GetCTC_HashOID(MD2);
  3987. if (ret == 0)
  3988. return -3383;
  3989. #endif
  3990. #ifndef NO_MD5
  3991. ret = wc_GetCTC_HashOID(WC_MD5);
  3992. if (ret == 0)
  3993. return -3384;
  3994. #endif
  3995. #ifndef NO_SHA
  3996. ret = wc_GetCTC_HashOID(WC_SHA);
  3997. if (ret == 0)
  3998. return -3385;
  3999. #endif
  4000. #ifdef WOLFSSL_SHA224
  4001. ret = wc_GetCTC_HashOID(WC_SHA224);
  4002. if (ret == 0)
  4003. return -3386;
  4004. #endif
  4005. #ifndef NO_SHA256
  4006. ret = wc_GetCTC_HashOID(WC_SHA256);
  4007. if (ret == 0)
  4008. return -3387;
  4009. #endif
  4010. #ifdef WOLFSSL_SHA384
  4011. ret = wc_GetCTC_HashOID(WC_SHA384);
  4012. if (ret == 0)
  4013. return -3388;
  4014. #endif
  4015. #ifdef WOLFSSL_SHA512
  4016. ret = wc_GetCTC_HashOID(WC_SHA512);
  4017. if (ret == 0)
  4018. return -3389;
  4019. #endif
  4020. ret = wc_GetCTC_HashOID(-1);
  4021. if (ret != 0)
  4022. return -3390;
  4023. #endif
  4024. return 0;
  4025. }
  4026. #endif /* !NO_HASH_WRAPPER */
  4027. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS) && \
  4028. defined(HAVE_FIPS_VERSION) && \
  4029. (HAVE_FIPS_VERSION >= 5))
  4030. WOLFSSL_TEST_SUBROUTINE int hmac_md5_test(void)
  4031. {
  4032. Hmac hmac;
  4033. byte hash[WC_MD5_DIGEST_SIZE];
  4034. const char* keys[]=
  4035. {
  4036. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  4037. "Jefe",
  4038. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4039. };
  4040. testVector a, b, c;
  4041. testVector test_hmac[3];
  4042. int ret;
  4043. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4044. a.input = "Hi There";
  4045. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  4046. "\x9d";
  4047. a.inLen = XSTRLEN(a.input);
  4048. a.outLen = WC_MD5_DIGEST_SIZE;
  4049. b.input = "what do ya want for nothing?";
  4050. b.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
  4051. "\x38";
  4052. b.inLen = XSTRLEN(b.input);
  4053. b.outLen = WC_MD5_DIGEST_SIZE;
  4054. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4055. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4056. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4057. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4058. c.output = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
  4059. "\xf6";
  4060. c.inLen = XSTRLEN(c.input);
  4061. c.outLen = WC_MD5_DIGEST_SIZE;
  4062. test_hmac[0] = a;
  4063. test_hmac[1] = b;
  4064. test_hmac[2] = c;
  4065. for (i = 0; i < times; ++i) {
  4066. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4067. if (i == 1) {
  4068. continue; /* cavium can't handle short keys, fips not allowed */
  4069. }
  4070. #endif
  4071. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0) {
  4072. return -3400;
  4073. }
  4074. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[i],
  4075. (word32)XSTRLEN(keys[i]));
  4076. if (ret != 0)
  4077. return -3401;
  4078. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4079. (word32)test_hmac[i].inLen);
  4080. if (ret != 0)
  4081. return -3402;
  4082. ret = wc_HmacFinal(&hmac, hash);
  4083. if (ret != 0)
  4084. return -3403;
  4085. if (XMEMCMP(hash, test_hmac[i].output, WC_MD5_DIGEST_SIZE) != 0)
  4086. return -3404 - i;
  4087. wc_HmacFree(&hmac);
  4088. }
  4089. #ifndef HAVE_FIPS
  4090. if (wc_HmacSizeByType(WC_MD5) != WC_MD5_DIGEST_SIZE)
  4091. return -3414;
  4092. #endif
  4093. return 0;
  4094. }
  4095. #endif /* !NO_HMAC && !NO_MD5 && (!HAVE_FIPS || (HAVE_FIPS_VERSION < 5)) */
  4096. #if !defined(NO_HMAC) && !defined(NO_SHA)
  4097. WOLFSSL_TEST_SUBROUTINE int hmac_sha_test(void)
  4098. {
  4099. Hmac hmac;
  4100. byte hash[WC_SHA_DIGEST_SIZE];
  4101. const char* keys[]=
  4102. {
  4103. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4104. "\x0b\x0b\x0b",
  4105. "Jefe",
  4106. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4107. "\xAA\xAA\xAA"
  4108. };
  4109. testVector a, b, c;
  4110. testVector test_hmac[3];
  4111. int ret;
  4112. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4113. a.input = "Hi There";
  4114. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  4115. "\x8e\xf1\x46\xbe\x00";
  4116. a.inLen = XSTRLEN(a.input);
  4117. a.outLen = WC_SHA_DIGEST_SIZE;
  4118. b.input = "what do ya want for nothing?";
  4119. b.output = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf"
  4120. "\x9c\x25\x9a\x7c\x79";
  4121. b.inLen = XSTRLEN(b.input);
  4122. b.outLen = WC_SHA_DIGEST_SIZE;
  4123. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4124. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4125. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4126. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4127. c.output = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b"
  4128. "\x4f\x63\xf1\x75\xd3";
  4129. c.inLen = XSTRLEN(c.input);
  4130. c.outLen = WC_SHA_DIGEST_SIZE;
  4131. test_hmac[0] = a;
  4132. test_hmac[1] = b;
  4133. test_hmac[2] = c;
  4134. for (i = 0; i < times; ++i) {
  4135. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4136. if (i == 1)
  4137. continue; /* cavium can't handle short keys, fips not allowed */
  4138. #endif
  4139. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4140. return -3500;
  4141. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[i],
  4142. (word32)XSTRLEN(keys[i]));
  4143. if (ret != 0)
  4144. return -3501;
  4145. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4146. (word32)test_hmac[i].inLen);
  4147. if (ret != 0)
  4148. return -3502;
  4149. ret = wc_HmacFinal(&hmac, hash);
  4150. if (ret != 0)
  4151. return -3503;
  4152. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA_DIGEST_SIZE) != 0)
  4153. return -3504 - i;
  4154. wc_HmacFree(&hmac);
  4155. }
  4156. #ifndef HAVE_FIPS
  4157. if (wc_HmacSizeByType(WC_SHA) != WC_SHA_DIGEST_SIZE)
  4158. return -3514;
  4159. #endif
  4160. return 0;
  4161. }
  4162. #endif
  4163. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  4164. WOLFSSL_TEST_SUBROUTINE int hmac_sha224_test(void)
  4165. {
  4166. Hmac hmac;
  4167. byte hash[WC_SHA224_DIGEST_SIZE];
  4168. const char* keys[]=
  4169. {
  4170. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4171. "\x0b\x0b\x0b",
  4172. "Jefe",
  4173. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4174. "\xAA\xAA\xAA",
  4175. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4176. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4177. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4178. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4179. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4180. };
  4181. testVector a, b, c, d;
  4182. testVector test_hmac[4];
  4183. int ret;
  4184. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4185. a.input = "Hi There";
  4186. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  4187. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  4188. a.inLen = XSTRLEN(a.input);
  4189. a.outLen = WC_SHA224_DIGEST_SIZE;
  4190. b.input = "what do ya want for nothing?";
  4191. b.output = "\xa3\x0e\x01\x09\x8b\xc6\xdb\xbf\x45\x69\x0f\x3a\x7e\x9e\x6d"
  4192. "\x0f\x8b\xbe\xa2\xa3\x9e\x61\x48\x00\x8f\xd0\x5e\x44";
  4193. b.inLen = XSTRLEN(b.input);
  4194. b.outLen = WC_SHA224_DIGEST_SIZE;
  4195. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4196. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4197. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4198. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4199. c.output = "\x7f\xb3\xcb\x35\x88\xc6\xc1\xf6\xff\xa9\x69\x4d\x7d\x6a\xd2"
  4200. "\x64\x93\x65\xb0\xc1\xf6\x5d\x69\xd1\xec\x83\x33\xea";
  4201. c.inLen = XSTRLEN(c.input);
  4202. c.outLen = WC_SHA224_DIGEST_SIZE;
  4203. d.input = "Big Key Input";
  4204. d.output = "\xe7\x4e\x2b\x8a\xa9\xf0\x37\x2f\xed\xae\x70\x0c\x49\x47\xf1"
  4205. "\x46\x54\xa7\x32\x6b\x55\x01\x87\xd2\xc8\x02\x0e\x3a";
  4206. d.inLen = XSTRLEN(d.input);
  4207. d.outLen = WC_SHA224_DIGEST_SIZE;
  4208. test_hmac[0] = a;
  4209. test_hmac[1] = b;
  4210. test_hmac[2] = c;
  4211. test_hmac[3] = d;
  4212. for (i = 0; i < times; ++i) {
  4213. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4214. if (i == 1)
  4215. continue; /* cavium can't handle short keys, fips not allowed */
  4216. #endif
  4217. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4218. return -3600;
  4219. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[i],
  4220. (word32)XSTRLEN(keys[i]));
  4221. if (ret != 0)
  4222. return -3601;
  4223. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4224. (word32)test_hmac[i].inLen);
  4225. if (ret != 0)
  4226. return -3602;
  4227. ret = wc_HmacFinal(&hmac, hash);
  4228. if (ret != 0)
  4229. return -3603;
  4230. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  4231. return -3604 - i;
  4232. wc_HmacFree(&hmac);
  4233. }
  4234. #ifndef HAVE_FIPS
  4235. if (wc_HmacSizeByType(WC_SHA224) != WC_SHA224_DIGEST_SIZE)
  4236. return -3614;
  4237. #endif
  4238. return 0;
  4239. }
  4240. #endif
  4241. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  4242. WOLFSSL_TEST_SUBROUTINE int hmac_sha256_test(void)
  4243. {
  4244. Hmac hmac;
  4245. byte hash[WC_SHA256_DIGEST_SIZE];
  4246. const char* keys[]=
  4247. {
  4248. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4249. "\x0b\x0b\x0b",
  4250. "Jefe",
  4251. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4252. "\xAA\xAA\xAA",
  4253. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4254. "\xAA\xAA\xAA",
  4255. };
  4256. testVector a, b, c, d;
  4257. testVector test_hmac[4];
  4258. int ret;
  4259. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4260. a.input = "Hi There";
  4261. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  4262. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  4263. "\xcf\xf7";
  4264. a.inLen = XSTRLEN(a.input);
  4265. a.outLen = WC_SHA256_DIGEST_SIZE;
  4266. b.input = "what do ya want for nothing?";
  4267. b.output = "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75"
  4268. "\xc7\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec"
  4269. "\x38\x43";
  4270. b.inLen = XSTRLEN(b.input);
  4271. b.outLen = WC_SHA256_DIGEST_SIZE;
  4272. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4273. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4274. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4275. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4276. c.output = "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81"
  4277. "\xa7\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5"
  4278. "\x65\xfe";
  4279. c.inLen = XSTRLEN(c.input);
  4280. c.outLen = WC_SHA256_DIGEST_SIZE;
  4281. d.input = 0;
  4282. d.output = "\x86\xe5\x4f\xd4\x48\x72\x5d\x7e\x5d\xcf\xe2\x23\x53\xc8\x28"
  4283. "\xaf\x48\x78\x1e\xb4\x8c\xae\x81\x06\xa7\xe1\xd4\x98\x94\x9f"
  4284. "\x3e\x46";
  4285. d.inLen = 0;
  4286. d.outLen = WC_SHA256_DIGEST_SIZE;
  4287. test_hmac[0] = a;
  4288. test_hmac[1] = b;
  4289. test_hmac[2] = c;
  4290. test_hmac[3] = d;
  4291. for (i = 0; i < times; ++i) {
  4292. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4293. if (i == 1)
  4294. continue; /* cavium can't handle short keys, fips not allowed */
  4295. #endif
  4296. #if defined(HAVE_INTEL_QA) || defined(HAVE_CAVIUM)
  4297. if (i == 3)
  4298. continue; /* QuickAssist can't handle empty HMAC */
  4299. #endif
  4300. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4301. return -3700 - i;
  4302. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[i],
  4303. (word32)XSTRLEN(keys[i]));
  4304. if (ret != 0)
  4305. return -3710 - i;
  4306. if (test_hmac[i].input != NULL) {
  4307. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4308. (word32)test_hmac[i].inLen);
  4309. if (ret != 0)
  4310. return -3720 - i;
  4311. }
  4312. ret = wc_HmacFinal(&hmac, hash);
  4313. if (ret != 0)
  4314. return -3730 - i;
  4315. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  4316. return -3740 - i;
  4317. wc_HmacFree(&hmac);
  4318. }
  4319. #ifndef HAVE_FIPS
  4320. if (wc_HmacSizeByType(WC_SHA256) != WC_SHA256_DIGEST_SIZE)
  4321. return -3750;
  4322. if (wc_HmacSizeByType(20) != BAD_FUNC_ARG)
  4323. return -3751;
  4324. #endif
  4325. if (wolfSSL_GetHmacMaxSize() != WC_MAX_DIGEST_SIZE)
  4326. return -3752;
  4327. return 0;
  4328. }
  4329. #endif
  4330. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  4331. WOLFSSL_TEST_SUBROUTINE int hmac_sha384_test(void)
  4332. {
  4333. Hmac hmac;
  4334. byte hash[WC_SHA384_DIGEST_SIZE];
  4335. const char* keys[]=
  4336. {
  4337. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4338. "\x0b\x0b\x0b",
  4339. "Jefe",
  4340. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4341. "\xAA\xAA\xAA",
  4342. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4343. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4344. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4345. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4346. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4347. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4348. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4349. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4350. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4351. };
  4352. testVector a, b, c, d;
  4353. testVector test_hmac[4];
  4354. int ret;
  4355. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4356. a.input = "Hi There";
  4357. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  4358. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  4359. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  4360. "\xfa\x9c\xb6";
  4361. a.inLen = XSTRLEN(a.input);
  4362. a.outLen = WC_SHA384_DIGEST_SIZE;
  4363. b.input = "what do ya want for nothing?";
  4364. b.output = "\xaf\x45\xd2\xe3\x76\x48\x40\x31\x61\x7f\x78\xd2\xb5\x8a\x6b"
  4365. "\x1b\x9c\x7e\xf4\x64\xf5\xa0\x1b\x47\xe4\x2e\xc3\x73\x63\x22"
  4366. "\x44\x5e\x8e\x22\x40\xca\x5e\x69\xe2\xc7\x8b\x32\x39\xec\xfa"
  4367. "\xb2\x16\x49";
  4368. b.inLen = XSTRLEN(b.input);
  4369. b.outLen = WC_SHA384_DIGEST_SIZE;
  4370. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4371. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4372. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4373. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4374. c.output = "\x88\x06\x26\x08\xd3\xe6\xad\x8a\x0a\xa2\xac\xe0\x14\xc8\xa8"
  4375. "\x6f\x0a\xa6\x35\xd9\x47\xac\x9f\xeb\xe8\x3e\xf4\xe5\x59\x66"
  4376. "\x14\x4b\x2a\x5a\xb3\x9d\xc1\x38\x14\xb9\x4e\x3a\xb6\xe1\x01"
  4377. "\xa3\x4f\x27";
  4378. c.inLen = XSTRLEN(c.input);
  4379. c.outLen = WC_SHA384_DIGEST_SIZE;
  4380. d.input = "Big Key Input";
  4381. d.output = "\xd2\x3d\x29\x6e\xf5\x1e\x23\x23\x49\x18\xb3\xbf\x4c\x38\x7b"
  4382. "\x31\x21\x17\xbb\x09\x73\x27\xf8\x12\x9d\xe9\xc6\x5d\xf9\x54"
  4383. "\xd6\x38\x5a\x68\x53\x14\xee\xe0\xa6\x4f\x36\x7e\xb2\xf3\x1a"
  4384. "\x57\x41\x69";
  4385. d.inLen = XSTRLEN(d.input);
  4386. d.outLen = WC_SHA384_DIGEST_SIZE;
  4387. test_hmac[0] = a;
  4388. test_hmac[1] = b;
  4389. test_hmac[2] = c;
  4390. test_hmac[3] = d;
  4391. for (i = 0; i < times; ++i) {
  4392. #if defined(HAVE_FIPS)
  4393. if (i == 1)
  4394. continue; /* fips not allowed */
  4395. #endif
  4396. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4397. return -3800;
  4398. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[i],
  4399. (word32)XSTRLEN(keys[i]));
  4400. if (ret != 0)
  4401. return -3801;
  4402. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4403. (word32)test_hmac[i].inLen);
  4404. if (ret != 0)
  4405. return -3802;
  4406. ret = wc_HmacFinal(&hmac, hash);
  4407. if (ret != 0)
  4408. return -3803;
  4409. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  4410. return -3804 - i;
  4411. wc_HmacFree(&hmac);
  4412. }
  4413. #ifndef HAVE_FIPS
  4414. if (wc_HmacSizeByType(WC_SHA384) != WC_SHA384_DIGEST_SIZE)
  4415. return -3814;
  4416. #endif
  4417. return 0;
  4418. }
  4419. #endif
  4420. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA512)
  4421. WOLFSSL_TEST_SUBROUTINE int hmac_sha512_test(void)
  4422. {
  4423. Hmac hmac;
  4424. byte hash[WC_SHA512_DIGEST_SIZE];
  4425. const char* keys[]=
  4426. {
  4427. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4428. "\x0b\x0b\x0b",
  4429. "Jefe",
  4430. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4431. "\xAA\xAA\xAA",
  4432. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4433. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4434. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4435. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4436. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4437. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4438. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4439. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4440. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4441. };
  4442. testVector a, b, c, d;
  4443. testVector test_hmac[4];
  4444. int ret;
  4445. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4446. a.input = "Hi There";
  4447. a.output = "\x87\xaa\x7c\xde\xa5\xef\x61\x9d\x4f\xf0\xb4\x24\x1a\x1d\x6c"
  4448. "\xb0\x23\x79\xf4\xe2\xce\x4e\xc2\x78\x7a\xd0\xb3\x05\x45\xe1"
  4449. "\x7c\xde\xda\xa8\x33\xb7\xd6\xb8\xa7\x02\x03\x8b\x27\x4e\xae"
  4450. "\xa3\xf4\xe4\xbe\x9d\x91\x4e\xeb\x61\xf1\x70\x2e\x69\x6c\x20"
  4451. "\x3a\x12\x68\x54";
  4452. a.inLen = XSTRLEN(a.input);
  4453. a.outLen = WC_SHA512_DIGEST_SIZE;
  4454. b.input = "what do ya want for nothing?";
  4455. b.output = "\x16\x4b\x7a\x7b\xfc\xf8\x19\xe2\xe3\x95\xfb\xe7\x3b\x56\xe0"
  4456. "\xa3\x87\xbd\x64\x22\x2e\x83\x1f\xd6\x10\x27\x0c\xd7\xea\x25"
  4457. "\x05\x54\x97\x58\xbf\x75\xc0\x5a\x99\x4a\x6d\x03\x4f\x65\xf8"
  4458. "\xf0\xe6\xfd\xca\xea\xb1\xa3\x4d\x4a\x6b\x4b\x63\x6e\x07\x0a"
  4459. "\x38\xbc\xe7\x37";
  4460. b.inLen = XSTRLEN(b.input);
  4461. b.outLen = WC_SHA512_DIGEST_SIZE;
  4462. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4463. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4464. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4465. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4466. c.output = "\xfa\x73\xb0\x08\x9d\x56\xa2\x84\xef\xb0\xf0\x75\x6c\x89\x0b"
  4467. "\xe9\xb1\xb5\xdb\xdd\x8e\xe8\x1a\x36\x55\xf8\x3e\x33\xb2\x27"
  4468. "\x9d\x39\xbf\x3e\x84\x82\x79\xa7\x22\xc8\x06\xb4\x85\xa4\x7e"
  4469. "\x67\xc8\x07\xb9\x46\xa3\x37\xbe\xe8\x94\x26\x74\x27\x88\x59"
  4470. "\xe1\x32\x92\xfb";
  4471. c.inLen = XSTRLEN(c.input);
  4472. c.outLen = WC_SHA512_DIGEST_SIZE;
  4473. d.input = "Big Key Input";
  4474. d.output = "\x3f\xa9\xc9\xe1\xbd\xbb\x04\x55\x1f\xef\xcc\x92\x33\x08\xeb"
  4475. "\xcf\xc1\x9a\x5b\x5b\xc0\x7c\x86\x84\xae\x8c\x40\xaf\xb1\x27"
  4476. "\x87\x38\x92\x04\xa8\xed\xd7\xd7\x07\xa9\x85\xa0\xc2\xcd\x30"
  4477. "\xc0\x56\x14\x49\xbc\x2f\x69\x15\x6a\x97\xd8\x79\x2f\xb3\x3b"
  4478. "\x1e\x18\xfe\xfa";
  4479. d.inLen = XSTRLEN(d.input);
  4480. d.outLen = WC_SHA512_DIGEST_SIZE;
  4481. test_hmac[0] = a;
  4482. test_hmac[1] = b;
  4483. test_hmac[2] = c;
  4484. test_hmac[3] = d;
  4485. for (i = 0; i < times; ++i) {
  4486. #if defined(HAVE_FIPS)
  4487. if (i == 1)
  4488. continue; /* fips not allowed */
  4489. #endif
  4490. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4491. return -3900;
  4492. ret = wc_HmacSetKey(&hmac, WC_SHA512, (byte*)keys[i],
  4493. (word32)XSTRLEN(keys[i]));
  4494. if (ret != 0)
  4495. return -3901;
  4496. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4497. (word32)test_hmac[i].inLen);
  4498. if (ret != 0)
  4499. return -3902;
  4500. ret = wc_HmacFinal(&hmac, hash);
  4501. if (ret != 0)
  4502. return -3903;
  4503. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  4504. return -3904 - i;
  4505. wc_HmacFree(&hmac);
  4506. }
  4507. #ifndef HAVE_FIPS
  4508. if (wc_HmacSizeByType(WC_SHA512) != WC_SHA512_DIGEST_SIZE)
  4509. return -3914;
  4510. #endif
  4511. return 0;
  4512. }
  4513. #endif
  4514. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  4515. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  4516. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  4517. WOLFSSL_TEST_SUBROUTINE int hmac_sha3_test(void)
  4518. {
  4519. Hmac hmac;
  4520. byte hash[WC_SHA3_512_DIGEST_SIZE];
  4521. const char* key[4] =
  4522. {
  4523. "Jefe",
  4524. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4525. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  4526. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  4527. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
  4528. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4529. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4530. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4531. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4532. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4533. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4534. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4535. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4536. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4537. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4538. };
  4539. const char* input[4] =
  4540. {
  4541. "what do ya want for nothing?",
  4542. "Hi There",
  4543. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  4544. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  4545. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  4546. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  4547. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd",
  4548. "Big Key Input"
  4549. };
  4550. const int hashType[4] =
  4551. {
  4552. WC_SHA3_224, WC_SHA3_256, WC_SHA3_384, WC_SHA3_512
  4553. };
  4554. const int hashSz[4] =
  4555. {
  4556. WC_SHA3_224_DIGEST_SIZE, WC_SHA3_256_DIGEST_SIZE,
  4557. WC_SHA3_384_DIGEST_SIZE, WC_SHA3_512_DIGEST_SIZE
  4558. };
  4559. const char* output[16] =
  4560. {
  4561. /* key = jefe, input = what do ya want for nothing? */
  4562. /* HMAC-SHA3-224 */
  4563. "\x7f\xdb\x8d\xd8\x8b\xd2\xf6\x0d\x1b\x79\x86\x34\xad\x38\x68\x11"
  4564. "\xc2\xcf\xc8\x5b\xfa\xf5\xd5\x2b\xba\xce\x5e\x66",
  4565. /* HMAC-SHA3-256 */
  4566. "\xc7\xd4\x07\x2e\x78\x88\x77\xae\x35\x96\xbb\xb0\xda\x73\xb8\x87"
  4567. "\xc9\x17\x1f\x93\x09\x5b\x29\x4a\xe8\x57\xfb\xe2\x64\x5e\x1b\xa5",
  4568. /* HMAC-SHA3-384 */
  4569. "\xf1\x10\x1f\x8c\xbf\x97\x66\xfd\x67\x64\xd2\xed\x61\x90\x3f\x21"
  4570. "\xca\x9b\x18\xf5\x7c\xf3\xe1\xa2\x3c\xa1\x35\x08\xa9\x32\x43\xce"
  4571. "\x48\xc0\x45\xdc\x00\x7f\x26\xa2\x1b\x3f\x5e\x0e\x9d\xf4\xc2\x0a",
  4572. /* HMAC-SHA3-512 */
  4573. "\x5a\x4b\xfe\xab\x61\x66\x42\x7c\x7a\x36\x47\xb7\x47\x29\x2b\x83"
  4574. "\x84\x53\x7c\xdb\x89\xaf\xb3\xbf\x56\x65\xe4\xc5\xe7\x09\x35\x0b"
  4575. "\x28\x7b\xae\xc9\x21\xfd\x7c\xa0\xee\x7a\x0c\x31\xd0\x22\xa9\x5e"
  4576. "\x1f\xc9\x2b\xa9\xd7\x7d\xf8\x83\x96\x02\x75\xbe\xb4\xe6\x20\x24",
  4577. /* key = 0b..., input = Hi There */
  4578. /* HMAC-SHA3-224 */
  4579. "\x3b\x16\x54\x6b\xbc\x7b\xe2\x70\x6a\x03\x1d\xca\xfd\x56\x37\x3d"
  4580. "\x98\x84\x36\x76\x41\xd8\xc5\x9a\xf3\xc8\x60\xf7",
  4581. /* HMAC-SHA3-256 */
  4582. "\xba\x85\x19\x23\x10\xdf\xfa\x96\xe2\xa3\xa4\x0e\x69\x77\x43\x51"
  4583. "\x14\x0b\xb7\x18\x5e\x12\x02\xcd\xcc\x91\x75\x89\xf9\x5e\x16\xbb",
  4584. /* HMAC-SHA3-384 */
  4585. "\x68\xd2\xdc\xf7\xfd\x4d\xdd\x0a\x22\x40\xc8\xa4\x37\x30\x5f\x61"
  4586. "\xfb\x73\x34\xcf\xb5\xd0\x22\x6e\x1b\xc2\x7d\xc1\x0a\x2e\x72\x3a"
  4587. "\x20\xd3\x70\xb4\x77\x43\x13\x0e\x26\xac\x7e\x3d\x53\x28\x86\xbd",
  4588. /* HMAC-SHA3-512 */
  4589. "\xeb\x3f\xbd\x4b\x2e\xaa\xb8\xf5\xc5\x04\xbd\x3a\x41\x46\x5a\xac"
  4590. "\xec\x15\x77\x0a\x7c\xab\xac\x53\x1e\x48\x2f\x86\x0b\x5e\xc7\xba"
  4591. "\x47\xcc\xb2\xc6\xf2\xaf\xce\x8f\x88\xd2\x2b\x6d\xc6\x13\x80\xf2"
  4592. "\x3a\x66\x8f\xd3\x88\x8b\xb8\x05\x37\xc0\xa0\xb8\x64\x07\x68\x9e",
  4593. /* key = aa..., output = dd... */
  4594. /* HMAC-SHA3-224 */
  4595. "\x67\x6c\xfc\x7d\x16\x15\x36\x38\x78\x03\x90\x69\x2b\xe1\x42\xd2"
  4596. "\xdf\x7c\xe9\x24\xb9\x09\xc0\xc0\x8d\xbf\xdc\x1a",
  4597. /* HMAC-SHA3-256 */
  4598. "\x84\xec\x79\x12\x4a\x27\x10\x78\x65\xce\xdd\x8b\xd8\x2d\xa9\x96"
  4599. "\x5e\x5e\xd8\xc3\x7b\x0a\xc9\x80\x05\xa7\xf3\x9e\xd5\x8a\x42\x07",
  4600. /* HMAC-SHA3-384 */
  4601. "\x27\x5c\xd0\xe6\x61\xbb\x8b\x15\x1c\x64\xd2\x88\xf1\xf7\x82\xfb"
  4602. "\x91\xa8\xab\xd5\x68\x58\xd7\x2b\xab\xb2\xd4\x76\xf0\x45\x83\x73"
  4603. "\xb4\x1b\x6a\xb5\xbf\x17\x4b\xec\x42\x2e\x53\xfc\x31\x35\xac\x6e",
  4604. /* HMAC-SHA3-512 */
  4605. "\x30\x9e\x99\xf9\xec\x07\x5e\xc6\xc6\xd4\x75\xed\xa1\x18\x06\x87"
  4606. "\xfc\xf1\x53\x11\x95\x80\x2a\x99\xb5\x67\x74\x49\xa8\x62\x51\x82"
  4607. "\x85\x1c\xb3\x32\xaf\xb6\xa8\x9c\x41\x13\x25\xfb\xcb\xcd\x42\xaf"
  4608. "\xcb\x7b\x6e\x5a\xab\x7e\xa4\x2c\x66\x0f\x97\xfd\x85\x84\xbf\x03",
  4609. /* key = big key, input = Big Key Input */
  4610. /* HMAC-SHA3-224 */
  4611. "\x29\xe0\x5e\x46\xc4\xa4\x5e\x46\x74\xbf\xd7\x2d\x1a\xd8\x66\xdb"
  4612. "\x2d\x0d\x10\x4e\x2b\xfa\xad\x53\x7d\x15\x69\x8b",
  4613. /* HMAC-SHA3-256 */
  4614. "\xb5\x5b\x8d\x64\xb6\x9c\x21\xd0\xbf\x20\x5c\xa2\xf7\xb9\xb1\x4e"
  4615. "\x88\x21\x61\x2c\x66\xc3\x91\xae\x6c\x95\x16\x85\x83\xe6\xf4\x9b",
  4616. /* HMAC-SHA3-384 */
  4617. "\xaa\x91\xb3\xa6\x2f\x56\xa1\xbe\x8c\x3e\x74\x38\xdb\x58\xd9\xd3"
  4618. "\x34\xde\xa0\x60\x6d\x8d\x46\xe0\xec\xa9\xf6\x06\x35\x14\xe6\xed"
  4619. "\x83\xe6\x7c\x77\x24\x6c\x11\xb5\x90\x82\xb5\x75\xda\x7b\x83\x2d",
  4620. /* HMAC-SHA3-512 */
  4621. "\x1c\xc3\xa9\x24\x4a\x4a\x3f\xbd\xc7\x20\x00\x16\x9b\x79\x47\x03"
  4622. "\x78\x75\x2c\xb5\xf1\x2e\x62\x7c\xbe\xef\x4e\x8f\x0b\x11\x2b\x32"
  4623. "\xa0\xee\xc9\xd0\x4d\x64\x64\x0b\x37\xf4\xdd\x66\xf7\x8b\xb3\xad"
  4624. "\x52\x52\x6b\x65\x12\xde\x0d\x7c\xc0\x8b\x60\x01\x6c\x37\xd7\xa8"
  4625. };
  4626. int i = 0, iMax = sizeof(input) / sizeof(input[0]),
  4627. j, jMax = sizeof(hashType) / sizeof(hashType[0]),
  4628. ret;
  4629. #ifdef HAVE_FIPS
  4630. /* FIPS requires a minimum length for HMAC keys, and "Jefe" is too
  4631. * short. Skip it in FIPS builds. */
  4632. i = 1;
  4633. #endif
  4634. for (; i < iMax; i++) {
  4635. for (j = 0; j < jMax; j++) {
  4636. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4637. return -4000;
  4638. ret = wc_HmacSetKey(&hmac, hashType[j], (byte*)key[i],
  4639. (word32)XSTRLEN(key[i]));
  4640. if (ret != 0)
  4641. return -4001;
  4642. ret = wc_HmacUpdate(&hmac, (byte*)input[i],
  4643. (word32)XSTRLEN(input[i]));
  4644. if (ret != 0)
  4645. return -4002;
  4646. ret = wc_HmacFinal(&hmac, hash);
  4647. if (ret != 0)
  4648. return -4003;
  4649. if (XMEMCMP(hash, output[(i*jMax) + j], hashSz[j]) != 0)
  4650. return -4004;
  4651. wc_HmacFree(&hmac);
  4652. if (i > 0)
  4653. continue;
  4654. #ifndef HAVE_FIPS
  4655. ret = wc_HmacSizeByType(hashType[j]);
  4656. if (ret != hashSz[j])
  4657. return -4005;
  4658. #endif
  4659. }
  4660. }
  4661. return 0;
  4662. }
  4663. #endif
  4664. #ifdef WC_RC2
  4665. typedef struct rc2TestVector {
  4666. const char* input;
  4667. const char* output;
  4668. const char* key; /* Key, variable up to 128 bytes */
  4669. const char* iv; /* IV, 8-bytes */
  4670. int inLen;
  4671. int outLen;
  4672. int keyLen;
  4673. int effectiveKeyBits; /* Up to 1024 bits supported */
  4674. } rc2TestVector;
  4675. static int rc2_ecb_test(void)
  4676. {
  4677. int ret = 0;
  4678. byte cipher[RC2_BLOCK_SIZE];
  4679. byte plain[RC2_BLOCK_SIZE];
  4680. rc2TestVector a, b, c, d, e, f, g, h;
  4681. rc2TestVector test_rc2[8];
  4682. int times = sizeof(test_rc2) / sizeof(rc2TestVector), i;
  4683. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4684. a.output = "\xeb\xb7\x73\xf9\x93\x27\x8e\xff";
  4685. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4686. a.inLen = RC2_BLOCK_SIZE;
  4687. a.outLen = RC2_BLOCK_SIZE;
  4688. a.keyLen = 8;
  4689. a.effectiveKeyBits = 63;
  4690. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4691. b.output = "\x27\x8b\x27\xe4\x2e\x2f\x0d\x49";
  4692. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4693. b.inLen = RC2_BLOCK_SIZE;
  4694. b.outLen = RC2_BLOCK_SIZE;
  4695. b.keyLen = 8;
  4696. b.effectiveKeyBits = 64;
  4697. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01";
  4698. c.output = "\x30\x64\x9e\xdf\x9b\xe7\xd2\xc2";
  4699. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  4700. c.inLen = RC2_BLOCK_SIZE;
  4701. c.outLen = RC2_BLOCK_SIZE;
  4702. c.keyLen = 8;
  4703. c.effectiveKeyBits = 64;
  4704. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4705. d.output = "\x61\xa8\xa2\x44\xad\xac\xcc\xf0";
  4706. d.key = "\x88";
  4707. d.inLen = RC2_BLOCK_SIZE;
  4708. d.outLen = RC2_BLOCK_SIZE;
  4709. d.keyLen = 1;
  4710. d.effectiveKeyBits = 64;
  4711. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4712. e.output = "\x6c\xcf\x43\x08\x97\x4c\x26\x7f";
  4713. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  4714. e.inLen = RC2_BLOCK_SIZE;
  4715. e.outLen = RC2_BLOCK_SIZE;
  4716. e.keyLen = 7;
  4717. e.effectiveKeyBits = 64;
  4718. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4719. f.output = "\x1a\x80\x7d\x27\x2b\xbe\x5d\xb1";
  4720. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4721. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4722. f.inLen = RC2_BLOCK_SIZE;
  4723. f.outLen = RC2_BLOCK_SIZE;
  4724. f.keyLen = 16;
  4725. f.effectiveKeyBits = 64;
  4726. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4727. g.output = "\x22\x69\x55\x2a\xb0\xf8\x5c\xa6";
  4728. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4729. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4730. g.inLen = RC2_BLOCK_SIZE;
  4731. g.outLen = RC2_BLOCK_SIZE;
  4732. g.keyLen = 16;
  4733. g.effectiveKeyBits = 128;
  4734. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4735. h.output = "\x5b\x78\xd3\xa4\x3d\xff\xf1\xf1";
  4736. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4737. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  4738. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  4739. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  4740. "\x1e";
  4741. h.inLen = RC2_BLOCK_SIZE;
  4742. h.outLen = RC2_BLOCK_SIZE;
  4743. h.keyLen = 33;
  4744. h.effectiveKeyBits = 129;
  4745. a.iv = b.iv = c.iv = d.iv = e.iv = f.iv = g.iv = h.iv = NULL;
  4746. test_rc2[0] = a;
  4747. test_rc2[1] = b;
  4748. test_rc2[2] = c;
  4749. test_rc2[3] = d;
  4750. test_rc2[4] = e;
  4751. test_rc2[5] = f;
  4752. test_rc2[6] = g;
  4753. test_rc2[7] = h;
  4754. for (i = 0; i < times; ++i) {
  4755. Rc2 enc;
  4756. XMEMSET(cipher, 0, RC2_BLOCK_SIZE);
  4757. XMEMSET(plain, 0, RC2_BLOCK_SIZE);
  4758. ret = wc_Rc2SetKey(&enc, (byte*)test_rc2[i].key, test_rc2[i].keyLen,
  4759. NULL, test_rc2[i].effectiveKeyBits);
  4760. if (ret != 0) {
  4761. return -4100;
  4762. }
  4763. /* ECB encrypt */
  4764. ret = wc_Rc2EcbEncrypt(&enc, cipher, (byte*)test_rc2[i].input,
  4765. (word32)test_rc2[i].outLen);
  4766. if (ret != 0) {
  4767. return -4101;
  4768. }
  4769. if (XMEMCMP(cipher, test_rc2[i].output, test_rc2[i].outLen)) {
  4770. return -4102;
  4771. }
  4772. /* ECB decrypt */
  4773. ret = wc_Rc2EcbDecrypt(&enc, plain, cipher, RC2_BLOCK_SIZE);
  4774. if (ret != 0) {
  4775. return -4103;
  4776. }
  4777. if (XMEMCMP(plain, test_rc2[i].input, RC2_BLOCK_SIZE)) {
  4778. return -4104;
  4779. }
  4780. }
  4781. return 0;
  4782. }
  4783. static int rc2_cbc_test(void)
  4784. {
  4785. int ret = 0;
  4786. byte cipher[128];
  4787. byte plain[128];
  4788. rc2TestVector a, b, c, d, e, f, g, h, i;
  4789. rc2TestVector test_rc2[9];
  4790. int times = sizeof(test_rc2) / sizeof(rc2TestVector), j;
  4791. /* key length = 7, effective key bits = 63 */
  4792. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4793. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4794. a.output = "\xEB\xB7\x73\xF9\x93\x27\x8E\xFF"
  4795. "\xF0\x51\x77\x8B\x65\xDB\x13\x57";
  4796. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4797. a.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4798. a.inLen = RC2_BLOCK_SIZE*2;
  4799. a.outLen = RC2_BLOCK_SIZE*2;
  4800. a.keyLen = 8;
  4801. a.effectiveKeyBits = 63;
  4802. /* key length = 8, effective key bits = 64, all 0xFF */
  4803. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff"
  4804. "\xff\xff\xff\xff\xff\xff\xff\xff";
  4805. b.output = "\xA3\xA1\x12\x65\x4F\x81\xC5\xCD"
  4806. "\xB6\x94\x3E\xEA\x3E\x8B\x9D\x1F";
  4807. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4808. b.iv = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4809. b.inLen = RC2_BLOCK_SIZE*2;
  4810. b.outLen = RC2_BLOCK_SIZE*2;
  4811. b.keyLen = 8;
  4812. b.effectiveKeyBits = 64;
  4813. /* key length = 8, effective key bits = 64 */
  4814. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01"
  4815. "\x10\x00\x00\x00\x00\x00\x00\x01";
  4816. c.output = "\xB5\x70\x14\xA2\x5F\x40\xE3\x6D"
  4817. "\x81\x99\x8D\xE0\xB5\xD5\x3A\x05";
  4818. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  4819. c.iv = "\x30\x00\x00\x00\x00\x00\x00\x00";
  4820. c.inLen = RC2_BLOCK_SIZE*2;
  4821. c.outLen = RC2_BLOCK_SIZE*2;
  4822. c.keyLen = 8;
  4823. c.effectiveKeyBits = 64;
  4824. /* key length = 1, effective key bits = 64 */
  4825. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4826. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4827. d.output = "\x61\xA8\xA2\x44\xAD\xAC\xCC\xF0"
  4828. "\x6D\x19\xE8\xF1\xFC\xE7\x38\x87";
  4829. d.key = "\x88";
  4830. d.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4831. d.inLen = RC2_BLOCK_SIZE*2;
  4832. d.outLen = RC2_BLOCK_SIZE*2;
  4833. d.keyLen = 1;
  4834. d.effectiveKeyBits = 64;
  4835. /* key length = 7, effective key bits = 64 */
  4836. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4837. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4838. e.output = "\x6C\xCF\x43\x08\x97\x4C\x26\x7F"
  4839. "\xCC\x3C\x53\x57\x7C\xA1\xA4\x4B";
  4840. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  4841. e.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4842. e.inLen = RC2_BLOCK_SIZE*2;
  4843. e.outLen = RC2_BLOCK_SIZE*2;
  4844. e.keyLen = 7;
  4845. e.effectiveKeyBits = 64;
  4846. /* key length = 16, effective key bits = 64 */
  4847. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4848. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4849. f.output = "\x1A\x80\x7D\x27\x2B\xBE\x5D\xB1"
  4850. "\x64\xEF\xE1\xC3\xB8\xAD\xFB\xBA";
  4851. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4852. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4853. f.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4854. f.inLen = RC2_BLOCK_SIZE*2;
  4855. f.outLen = RC2_BLOCK_SIZE*2;
  4856. f.keyLen = 16;
  4857. f.effectiveKeyBits = 64;
  4858. /* key length = 16, effective bits = 128 */
  4859. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4860. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4861. g.output = "\x22\x69\x55\x2A\xB0\xF8\x5C\xA6"
  4862. "\x53\x6E\xFD\x2D\x89\xE1\x2A\x73";
  4863. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4864. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4865. g.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4866. g.inLen = RC2_BLOCK_SIZE*2;
  4867. g.outLen = RC2_BLOCK_SIZE*2;
  4868. g.keyLen = 16;
  4869. g.effectiveKeyBits = 128;
  4870. /* key length = 33, effective bits = 129 */
  4871. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4872. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4873. h.output = "\x5B\x78\xD3\xA4\x3D\xFF\xF1\xF1"
  4874. "\x45\x30\xA8\xD5\xC7\x7C\x46\x19";
  4875. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4876. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  4877. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  4878. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  4879. "\x1e";
  4880. h.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4881. h.inLen = RC2_BLOCK_SIZE*2;
  4882. h.outLen = RC2_BLOCK_SIZE*2;
  4883. h.keyLen = 33;
  4884. h.effectiveKeyBits = 129;
  4885. /* key length = 10, effective bits = 40 */
  4886. i.input = "\x11\x22\x33\x44\x55\x66\x77\x88"
  4887. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00"
  4888. "\x11\x22\x33\x44\x55\x66\x77\x88"
  4889. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00";
  4890. i.output = "\x71\x2D\x11\x99\xC9\xA0\x78\x4F"
  4891. "\xCD\xF1\x1E\x3D\xFD\x21\x7E\xDB"
  4892. "\xB2\x6E\x0D\xA4\x72\xBC\x31\x51"
  4893. "\x48\xEF\x4E\x68\x3B\xDC\xCD\x7D";
  4894. i.key = "\x26\x1E\x57\x8E\xC9\x62\xBF\xB8"
  4895. "\x3E\x96";
  4896. i.iv = "\x01\x02\x03\x04\x05\x06\x07\x08";
  4897. i.inLen = RC2_BLOCK_SIZE*4;
  4898. i.outLen = RC2_BLOCK_SIZE*4;
  4899. i.keyLen = 10;
  4900. i.effectiveKeyBits = 40;
  4901. test_rc2[0] = a;
  4902. test_rc2[1] = b;
  4903. test_rc2[2] = c;
  4904. test_rc2[3] = d;
  4905. test_rc2[4] = e;
  4906. test_rc2[5] = f;
  4907. test_rc2[6] = g;
  4908. test_rc2[7] = h;
  4909. test_rc2[8] = i;
  4910. for (j = 0; j < times; ++j) {
  4911. Rc2 rc2;
  4912. XMEMSET(cipher, 0, sizeof(cipher));
  4913. XMEMSET(plain, 0, sizeof(plain));
  4914. ret = wc_Rc2SetKey(&rc2, (byte*)test_rc2[j].key, test_rc2[j].keyLen,
  4915. (byte*)test_rc2[j].iv, test_rc2[j].effectiveKeyBits);
  4916. if (ret != 0) {
  4917. return -4200;
  4918. }
  4919. ret = wc_Rc2CbcEncrypt(&rc2, cipher, (byte*)test_rc2[j].input,
  4920. test_rc2[j].inLen);
  4921. if (ret != 0) {
  4922. return -4201;
  4923. }
  4924. if (XMEMCMP(cipher, (byte*)test_rc2[j].output, test_rc2[j].outLen)) {
  4925. return -4202;
  4926. }
  4927. /* reset IV for decrypt, since overriden by encrypt operation */
  4928. ret = wc_Rc2SetIV(&rc2, (byte*)test_rc2[j].iv);
  4929. if (ret != 0) {
  4930. return -4203;
  4931. }
  4932. ret = wc_Rc2CbcDecrypt(&rc2, plain, cipher, test_rc2[j].outLen);
  4933. if (ret != 0) {
  4934. return -4204;
  4935. }
  4936. if (XMEMCMP(plain, (byte*)test_rc2[j].input, test_rc2[j].inLen)) {
  4937. return -4205;
  4938. }
  4939. }
  4940. return 0;
  4941. }
  4942. WOLFSSL_TEST_SUBROUTINE int rc2_test(void)
  4943. {
  4944. int ret = 0;
  4945. ret = rc2_ecb_test();
  4946. if (ret != 0) {
  4947. return ret;
  4948. }
  4949. return rc2_cbc_test();
  4950. }
  4951. #endif
  4952. #ifndef NO_RC4
  4953. WOLFSSL_TEST_SUBROUTINE int arc4_test(void)
  4954. {
  4955. byte cipher[16];
  4956. byte plain[16];
  4957. const char* keys[] =
  4958. {
  4959. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  4960. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  4961. "\x00\x00\x00\x00\x00\x00\x00\x00",
  4962. "\xef\x01\x23\x45"
  4963. };
  4964. testVector a, b, c, d;
  4965. testVector test_arc4[4];
  4966. int times = sizeof(test_arc4) / sizeof(testVector), i;
  4967. a.input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  4968. a.output = "\x75\xb7\x87\x80\x99\xe0\xc5\x96";
  4969. a.inLen = 8;
  4970. a.outLen = 8;
  4971. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4972. b.output = "\x74\x94\xc2\xe7\x10\x4b\x08\x79";
  4973. b.inLen = 8;
  4974. b.outLen = 8;
  4975. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4976. c.output = "\xde\x18\x89\x41\xa3\x37\x5d\x3a";
  4977. c.inLen = 8;
  4978. c.outLen = 8;
  4979. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  4980. d.output = "\xd6\xa1\x41\xa7\xec\x3c\x38\xdf\xbd\x61";
  4981. d.inLen = 10;
  4982. d.outLen = 10;
  4983. test_arc4[0] = a;
  4984. test_arc4[1] = b;
  4985. test_arc4[2] = c;
  4986. test_arc4[3] = d;
  4987. for (i = 0; i < times; ++i) {
  4988. Arc4 enc;
  4989. Arc4 dec;
  4990. int keylen = 8; /* XSTRLEN with key 0x00 not good */
  4991. if (i == 3)
  4992. keylen = 4;
  4993. if (wc_Arc4Init(&enc, HEAP_HINT, devId) != 0)
  4994. return -4400;
  4995. if (wc_Arc4Init(&dec, HEAP_HINT, devId) != 0)
  4996. return -4401;
  4997. wc_Arc4SetKey(&enc, (byte*)keys[i], keylen);
  4998. wc_Arc4SetKey(&dec, (byte*)keys[i], keylen);
  4999. wc_Arc4Process(&enc, cipher, (byte*)test_arc4[i].input,
  5000. (word32)test_arc4[i].outLen);
  5001. wc_Arc4Process(&dec, plain, cipher, (word32)test_arc4[i].outLen);
  5002. if (XMEMCMP(plain, test_arc4[i].input, test_arc4[i].outLen))
  5003. return -4402 - i;
  5004. if (XMEMCMP(cipher, test_arc4[i].output, test_arc4[i].outLen))
  5005. return -4412 - i;
  5006. wc_Arc4Free(&enc);
  5007. wc_Arc4Free(&dec);
  5008. }
  5009. return 0;
  5010. }
  5011. #endif
  5012. #ifdef HAVE_CHACHA
  5013. WOLFSSL_TEST_SUBROUTINE int chacha_test(void)
  5014. {
  5015. ChaCha enc;
  5016. ChaCha dec;
  5017. byte cipher[128];
  5018. byte plain[128];
  5019. byte sliver[64];
  5020. byte input[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  5021. word32 keySz = 32;
  5022. int ret = 0;
  5023. int i;
  5024. int times = 4;
  5025. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  5026. {
  5027. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5028. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5029. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5030. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5031. };
  5032. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  5033. {
  5034. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5035. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5036. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5037. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  5038. };
  5039. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  5040. {
  5041. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5042. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5043. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5044. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5045. };
  5046. /* 128 bit key */
  5047. WOLFSSL_SMALL_STACK_STATIC const byte key4[] =
  5048. {
  5049. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5050. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5051. };
  5052. const byte* keys[] = {key1, key2, key3, key4};
  5053. WOLFSSL_SMALL_STACK_STATIC const byte ivs1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  5054. WOLFSSL_SMALL_STACK_STATIC const byte ivs2[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  5055. WOLFSSL_SMALL_STACK_STATIC const byte ivs3[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00};
  5056. WOLFSSL_SMALL_STACK_STATIC const byte ivs4[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  5057. const byte* ivs[] = {ivs1, ivs2, ivs3, ivs4};
  5058. #ifndef BENCH_EMBEDDED
  5059. WOLFSSL_SMALL_STACK_STATIC const byte cipher_big_result[] = {
  5060. 0x06, 0xa6, 0x5d, 0x31, 0x21, 0x6c, 0xdb, 0x37, 0x48, 0x7c, 0x01, 0x9d,
  5061. 0x72, 0xdf, 0x0a, 0x5b, 0x64, 0x74, 0x20, 0xba, 0x9e, 0xe0, 0x26, 0x7a,
  5062. 0xbf, 0xdf, 0x83, 0x34, 0x3b, 0x4f, 0x94, 0x3f, 0x37, 0x89, 0xaf, 0x00,
  5063. 0xdf, 0x0f, 0x2e, 0x75, 0x16, 0x41, 0xf6, 0x7a, 0x86, 0x94, 0x9d, 0x32,
  5064. 0x56, 0xf0, 0x79, 0x71, 0x68, 0x6f, 0xa6, 0x6b, 0xc6, 0x59, 0x49, 0xf6,
  5065. 0x10, 0x34, 0x03, 0x03, 0x16, 0x53, 0x9a, 0x98, 0x2a, 0x46, 0xde, 0x17,
  5066. 0x06, 0x65, 0x70, 0xca, 0x0a, 0x1f, 0xab, 0x80, 0x26, 0x96, 0x3f, 0x3e,
  5067. 0x7a, 0x3c, 0xa8, 0x87, 0xbb, 0x65, 0xdd, 0x5e, 0x07, 0x7b, 0x34, 0xe0,
  5068. 0x56, 0xda, 0x32, 0x13, 0x30, 0xc9, 0x0c, 0xd7, 0xba, 0xe4, 0x1f, 0xa6,
  5069. 0x91, 0x4f, 0x72, 0x9f, 0xd9, 0x5c, 0x62, 0x7d, 0xa6, 0xc2, 0xbc, 0x87,
  5070. 0xae, 0x64, 0x11, 0x94, 0x3b, 0xbc, 0x6c, 0x23, 0xbd, 0x7d, 0x00, 0xb4,
  5071. 0x99, 0xf2, 0x68, 0xb5, 0x59, 0x70, 0x93, 0xad, 0x69, 0xd0, 0xb1, 0x28,
  5072. 0x70, 0x92, 0xeb, 0xec, 0x39, 0x80, 0x82, 0xde, 0x44, 0xe2, 0x8a, 0x26,
  5073. 0xb3, 0xe9, 0x45, 0xcf, 0x83, 0x76, 0x9f, 0x6a, 0xa0, 0x46, 0x4a, 0x3d,
  5074. 0x26, 0x56, 0xaf, 0x49, 0x41, 0x26, 0x1b, 0x6a, 0x41, 0x37, 0x65, 0x91,
  5075. 0x72, 0xc4, 0xe7, 0x3c, 0x17, 0x31, 0xae, 0x2e, 0x2b, 0x31, 0x45, 0xe4,
  5076. 0x93, 0xd3, 0x10, 0xaa, 0xc5, 0x62, 0xd5, 0x11, 0x4b, 0x57, 0x1d, 0xad,
  5077. 0x48, 0x06, 0xd0, 0x0d, 0x98, 0xa5, 0xc6, 0x5b, 0xd0, 0x9e, 0x22, 0xc0,
  5078. 0x00, 0x32, 0x5a, 0xf5, 0x1c, 0x89, 0x6d, 0x54, 0x97, 0x55, 0x6b, 0x46,
  5079. 0xc5, 0xc7, 0xc4, 0x48, 0x9c, 0xbf, 0x47, 0xdc, 0x03, 0xc4, 0x1b, 0xcb,
  5080. 0x65, 0xa6, 0x91, 0x9d, 0x6d, 0xf1, 0xb0, 0x7a, 0x4d, 0x3b, 0x03, 0x95,
  5081. 0xf4, 0x8b, 0x0b, 0xae, 0x39, 0xff, 0x3f, 0xf6, 0xc0, 0x14, 0x18, 0x8a,
  5082. 0xe5, 0x19, 0xbd, 0xc1, 0xb4, 0x05, 0x4e, 0x29, 0x2f, 0x0b, 0x33, 0x76,
  5083. 0x28, 0x16, 0xa4, 0xa6, 0x93, 0x04, 0xb5, 0x55, 0x6b, 0x89, 0x3d, 0xa5,
  5084. 0x0f, 0xd3, 0xad, 0xfa, 0xd9, 0xfd, 0x05, 0x5d, 0x48, 0x94, 0x25, 0x5a,
  5085. 0x2c, 0x9a, 0x94, 0x80, 0xb0, 0xe7, 0xcb, 0x4d, 0x77, 0xbf, 0xca, 0xd8,
  5086. 0x55, 0x48, 0xbd, 0x66, 0xb1, 0x85, 0x81, 0xb1, 0x37, 0x79, 0xab, 0x52,
  5087. 0x08, 0x14, 0x12, 0xac, 0xcd, 0x45, 0x4d, 0x53, 0x6b, 0xca, 0x96, 0xc7,
  5088. 0x3b, 0x2f, 0x73, 0xb1, 0x5a, 0x23, 0xbd, 0x65, 0xd5, 0xea, 0x17, 0xb3,
  5089. 0xdc, 0xa1, 0x17, 0x1b, 0x2d, 0xb3, 0x9c, 0xd0, 0xdb, 0x41, 0x77, 0xef,
  5090. 0x93, 0x20, 0x52, 0x3e, 0x9d, 0xf5, 0xbf, 0x33, 0xf7, 0x52, 0xc1, 0x90,
  5091. 0xa0, 0x15, 0x17, 0xce, 0xf7, 0xf7, 0xd0, 0x3a, 0x3b, 0xd1, 0x72, 0x56,
  5092. 0x31, 0x81, 0xae, 0x60, 0xab, 0x40, 0xc1, 0xd1, 0x28, 0x77, 0x53, 0xac,
  5093. 0x9f, 0x11, 0x0a, 0x88, 0x36, 0x4b, 0xda, 0x57, 0xa7, 0x28, 0x5c, 0x85,
  5094. 0xd3, 0x85, 0x9b, 0x79, 0xad, 0x05, 0x1c, 0x37, 0x14, 0x5e, 0x0d, 0xd0,
  5095. 0x23, 0x03, 0x42, 0x1d, 0x48, 0x5d, 0xc5, 0x3c, 0x5a, 0x08, 0xa9, 0x0d,
  5096. 0x6e, 0x82, 0x7c, 0x2e, 0x3c, 0x41, 0xcc, 0x96, 0x8e, 0xad, 0xee, 0x2a,
  5097. 0x61, 0x0b, 0x16, 0x0f, 0xa9, 0x24, 0x40, 0x85, 0xbc, 0x9f, 0x28, 0x8d,
  5098. 0xe6, 0x68, 0x4d, 0x8f, 0x30, 0x48, 0xd9, 0x73, 0x73, 0x6c, 0x9a, 0x7f,
  5099. 0x67, 0xf7, 0xde, 0x4c, 0x0a, 0x8b, 0xe4, 0xb3, 0x08, 0x2a, 0x52, 0xda,
  5100. 0x54, 0xee, 0xcd, 0xb5, 0x62, 0x4a, 0x26, 0x20, 0xfb, 0x40, 0xbb, 0x39,
  5101. 0x3a, 0x0f, 0x09, 0xe8, 0x00, 0xd1, 0x24, 0x97, 0x60, 0xe9, 0x83, 0x83,
  5102. 0xfe, 0x9f, 0x9c, 0x15, 0xcf, 0x69, 0x03, 0x9f, 0x03, 0xe1, 0xe8, 0x6e,
  5103. 0xbd, 0x87, 0x58, 0x68, 0xee, 0xec, 0xd8, 0x29, 0x46, 0x23, 0x49, 0x92,
  5104. 0x72, 0x95, 0x5b, 0x49, 0xca, 0xe0, 0x45, 0x59, 0xb2, 0xca, 0xf4, 0xfc,
  5105. 0xb7, 0x59, 0x37, 0x49, 0x28, 0xbc, 0xf3, 0xd7, 0x61, 0xbc, 0x4b, 0xf3,
  5106. 0xa9, 0x4b, 0x2f, 0x05, 0xa8, 0x01, 0xa5, 0xdc, 0x00, 0x6e, 0x01, 0xb6,
  5107. 0x45, 0x3c, 0xd5, 0x49, 0x7d, 0x5c, 0x25, 0xe8, 0x31, 0x87, 0xb2, 0xb9,
  5108. 0xbf, 0xb3, 0x01, 0x62, 0x0c, 0xd0, 0x48, 0x77, 0xa2, 0x34, 0x0f, 0x16,
  5109. 0x22, 0x28, 0xee, 0x54, 0x08, 0x93, 0x3b, 0xe4, 0xde, 0x7e, 0x63, 0xf7,
  5110. 0x97, 0x16, 0x5d, 0x71, 0x58, 0xc2, 0x2e, 0xf2, 0x36, 0xa6, 0x12, 0x65,
  5111. 0x94, 0x17, 0xac, 0x66, 0x23, 0x7e, 0xc6, 0x72, 0x79, 0x24, 0xce, 0x8f,
  5112. 0x55, 0x19, 0x97, 0x44, 0xfc, 0x55, 0xec, 0x85, 0x26, 0x27, 0xdb, 0x38,
  5113. 0xb1, 0x42, 0x0a, 0xdd, 0x05, 0x99, 0x28, 0xeb, 0x03, 0x6c, 0x9a, 0xe9,
  5114. 0x17, 0xf6, 0x2c, 0xb0, 0xfe, 0xe7, 0xa4, 0xa7, 0x31, 0xda, 0x4d, 0xb0,
  5115. 0x29, 0xdb, 0xdd, 0x8d, 0x12, 0x13, 0x9c, 0xb4, 0xcc, 0x83, 0x97, 0xfb,
  5116. 0x1a, 0xdc, 0x08, 0xd6, 0x30, 0x62, 0xe8, 0xeb, 0x8b, 0x61, 0xcb, 0x1d,
  5117. 0x06, 0xe3, 0xa5, 0x4d, 0x35, 0xdb, 0x59, 0xa8, 0x2d, 0x87, 0x27, 0x44,
  5118. 0x6f, 0xc0, 0x38, 0x97, 0xe4, 0x85, 0x00, 0x02, 0x09, 0xf6, 0x69, 0x3a,
  5119. 0xcf, 0x08, 0x1b, 0x21, 0xbb, 0x79, 0xb1, 0xa1, 0x34, 0x09, 0xe0, 0x80,
  5120. 0xca, 0xb0, 0x78, 0x8a, 0x11, 0x97, 0xd4, 0x07, 0xbe, 0x1b, 0x6a, 0x5d,
  5121. 0xdb, 0xd6, 0x1f, 0x76, 0x6b, 0x16, 0xf0, 0x58, 0x84, 0x5f, 0x59, 0xce,
  5122. 0x62, 0x34, 0xc3, 0xdf, 0x94, 0xb8, 0x2f, 0x84, 0x68, 0xf0, 0xb8, 0x51,
  5123. 0xd9, 0x6d, 0x8e, 0x4a, 0x1d, 0xe6, 0x5c, 0xd8, 0x86, 0x25, 0xe3, 0x24,
  5124. 0xfd, 0x21, 0x61, 0x13, 0x48, 0x3e, 0xf6, 0x7d, 0xa6, 0x71, 0x9b, 0xd2,
  5125. 0x6e, 0xe6, 0xd2, 0x08, 0x94, 0x62, 0x6c, 0x98, 0xfe, 0x2f, 0x9c, 0x88,
  5126. 0x7e, 0x78, 0x15, 0x02, 0x00, 0xf0, 0xba, 0x24, 0x91, 0xf2, 0xdc, 0x47,
  5127. 0x51, 0x4d, 0x15, 0x5e, 0x91, 0x5f, 0x57, 0x5b, 0x1d, 0x35, 0x24, 0x45,
  5128. 0x75, 0x9b, 0x88, 0x75, 0xf1, 0x2f, 0x85, 0xe7, 0x89, 0xd1, 0x01, 0xb4,
  5129. 0xc8, 0x18, 0xb7, 0x97, 0xef, 0x4b, 0x90, 0xf4, 0xbf, 0x10, 0x27, 0x3c,
  5130. 0x60, 0xff, 0xc4, 0x94, 0x20, 0x2f, 0x93, 0x4b, 0x4d, 0xe3, 0x80, 0xf7,
  5131. 0x2c, 0x71, 0xd9, 0xe3, 0x68, 0xb4, 0x77, 0x2b, 0xc7, 0x0d, 0x39, 0x92,
  5132. 0xef, 0x91, 0x0d, 0xb2, 0x11, 0x50, 0x0e, 0xe8, 0xad, 0x3b, 0xf6, 0xb5,
  5133. 0xc6, 0x14, 0x4d, 0x33, 0x53, 0xa7, 0x60, 0x15, 0xc7, 0x27, 0x51, 0xdc,
  5134. 0x54, 0x29, 0xa7, 0x0d, 0x6a, 0x7b, 0x72, 0x13, 0xad, 0x7d, 0x41, 0x19,
  5135. 0x4e, 0x42, 0x49, 0xcc, 0x42, 0xe4, 0xbd, 0x99, 0x13, 0xd9, 0x7f, 0xf3,
  5136. 0x38, 0xa4, 0xb6, 0x33, 0xed, 0x07, 0x48, 0x7e, 0x8e, 0x82, 0xfe, 0x3a,
  5137. 0x9d, 0x75, 0x93, 0xba, 0x25, 0x4e, 0x37, 0x3c, 0x0c, 0xd5, 0x69, 0xa9,
  5138. 0x2d, 0x9e, 0xfd, 0xe8, 0xbb, 0xf5, 0x0c, 0xe2, 0x86, 0xb9, 0x5e, 0x6f,
  5139. 0x28, 0xe4, 0x19, 0xb3, 0x0b, 0xa4, 0x86, 0xd7, 0x24, 0xd0, 0xb8, 0x89,
  5140. 0x7b, 0x76, 0xec, 0x05, 0x10, 0x5b, 0x68, 0xe9, 0x58, 0x66, 0xa3, 0xc5,
  5141. 0xb6, 0x63, 0x20, 0x0e, 0x0e, 0xea, 0x3d, 0x61, 0x5e, 0xda, 0x3d, 0x3c,
  5142. 0xf9, 0xfd, 0xed, 0xa9, 0xdb, 0x52, 0x94, 0x8a, 0x00, 0xca, 0x3c, 0x8d,
  5143. 0x66, 0x8f, 0xb0, 0xf0, 0x5a, 0xca, 0x3f, 0x63, 0x71, 0xbf, 0xca, 0x99,
  5144. 0x37, 0x9b, 0x75, 0x97, 0x89, 0x10, 0x6e, 0xcf, 0xf2, 0xf5, 0xe3, 0xd5,
  5145. 0x45, 0x9b, 0xad, 0x10, 0x71, 0x6c, 0x5f, 0x6f, 0x7f, 0x22, 0x77, 0x18,
  5146. 0x2f, 0xf9, 0x99, 0xc5, 0x69, 0x58, 0x03, 0x12, 0x86, 0x82, 0x3e, 0xbf,
  5147. 0xc2, 0x12, 0x35, 0x43, 0xa3, 0xd9, 0x18, 0x4f, 0x41, 0x11, 0x6b, 0xf3,
  5148. 0x67, 0xaf, 0x3d, 0x78, 0xe4, 0x22, 0x2d, 0xb3, 0x48, 0x43, 0x31, 0x1d,
  5149. 0xef, 0xa8, 0xba, 0x49, 0x8e, 0xa9, 0xa7, 0xb6, 0x18, 0x77, 0x84, 0xca,
  5150. 0xbd, 0xa2, 0x02, 0x1b, 0x6a, 0xf8, 0x5f, 0xda, 0xff, 0xcf, 0x01, 0x6a,
  5151. 0x86, 0x69, 0xa9, 0xe9, 0xcb, 0x60, 0x1e, 0x15, 0xdc, 0x8f, 0x5d, 0x39,
  5152. 0xb5, 0xce, 0x55, 0x5f, 0x47, 0x97, 0xb1, 0x19, 0x6e, 0x21, 0xd6, 0x13,
  5153. 0x39, 0xb2, 0x24, 0xe0, 0x62, 0x82, 0x9f, 0xed, 0x12, 0x81, 0xed, 0xee,
  5154. 0xab, 0xd0, 0x2f, 0x19, 0x89, 0x3f, 0x57, 0x2e, 0xc2, 0xe2, 0x67, 0xe8,
  5155. 0xae, 0x03, 0x56, 0xba, 0xd4, 0xd0, 0xa4, 0x89, 0x03, 0x06, 0x5b, 0xcc,
  5156. 0xf2, 0x22, 0xb8, 0x0e, 0x76, 0x79, 0x4a, 0x42, 0x1d, 0x37, 0x51, 0x5a,
  5157. 0xaa, 0x46, 0x6c, 0x2a, 0xdd, 0x66, 0xfe, 0xc6, 0x68, 0xc3, 0x38, 0xa2,
  5158. 0xae, 0x5b, 0x98, 0x24, 0x5d, 0x43, 0x05, 0x82, 0x38, 0x12, 0xd3, 0xd1,
  5159. 0x75, 0x2d, 0x4f, 0x61, 0xbd, 0xb9, 0x10, 0x87, 0x44, 0x2a, 0x78, 0x07,
  5160. 0xff, 0xf4, 0x0f, 0xa1, 0xf3, 0x68, 0x9f, 0xbe, 0xae, 0xa2, 0x91, 0xf0,
  5161. 0xc7, 0x55, 0x7a, 0x52, 0xd5, 0xa3, 0x8d, 0x6f, 0xe4, 0x90, 0x5c, 0xf3,
  5162. 0x5f, 0xce, 0x3d, 0x23, 0xf9, 0x8e, 0xae, 0x14, 0xfb, 0x82, 0x9a, 0xa3,
  5163. 0x04, 0x5f, 0xbf, 0xad, 0x3e, 0xf2, 0x97, 0x0a, 0x60, 0x40, 0x70, 0x19,
  5164. 0x72, 0xad, 0x66, 0xfb, 0x78, 0x1b, 0x84, 0x6c, 0x98, 0xbc, 0x8c, 0xf8,
  5165. 0x4f, 0xcb, 0xb5, 0xf6, 0xaf, 0x7a, 0xb7, 0x93, 0xef, 0x67, 0x48, 0x02,
  5166. 0x2c, 0xcb, 0xe6, 0x77, 0x0f, 0x7b, 0xc1, 0xee, 0xc5, 0xb6, 0x2d, 0x7e,
  5167. 0x62, 0xa0, 0xc0, 0xa7, 0xa5, 0x80, 0x31, 0x92, 0x50, 0xa1, 0x28, 0x22,
  5168. 0x95, 0x03, 0x17, 0xd1, 0x0f, 0xf6, 0x08, 0xe5, 0xec
  5169. };
  5170. #define CHACHA_BIG_TEST_SIZE 1305
  5171. #if !defined(WOLFSSL_SMALL_STACK) || defined(WOLFSSL_NO_MALLOC)
  5172. byte cipher_big[CHACHA_BIG_TEST_SIZE] = {0};
  5173. byte plain_big[CHACHA_BIG_TEST_SIZE] = {0};
  5174. byte input_big[CHACHA_BIG_TEST_SIZE] = {0};
  5175. #else
  5176. byte* cipher_big;
  5177. byte* plain_big;
  5178. byte* input_big;
  5179. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  5180. int block_size;
  5181. #endif /* BENCH_EMBEDDED */
  5182. byte a[] = {0x76,0xb8,0xe0,0xad,0xa0,0xf1,0x3d,0x90};
  5183. byte b[] = {0x45,0x40,0xf0,0x5a,0x9f,0x1f,0xb2,0x96};
  5184. byte c[] = {0xde,0x9c,0xba,0x7b,0xf3,0xd6,0x9e,0xf5};
  5185. byte d[] = {0x89,0x67,0x09,0x52,0x60,0x83,0x64,0xfd};
  5186. byte* test_chacha[4];
  5187. test_chacha[0] = a;
  5188. test_chacha[1] = b;
  5189. test_chacha[2] = c;
  5190. test_chacha[3] = d;
  5191. #ifndef BENCH_EMBEDDED
  5192. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  5193. cipher_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  5194. DYNAMIC_TYPE_TMP_BUFFER);
  5195. if (cipher_big == NULL) {
  5196. return MEMORY_E;
  5197. }
  5198. plain_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  5199. DYNAMIC_TYPE_TMP_BUFFER);
  5200. if (plain_big == NULL) {
  5201. return MEMORY_E;
  5202. }
  5203. input_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  5204. DYNAMIC_TYPE_TMP_BUFFER);
  5205. if (input_big == NULL) {
  5206. return MEMORY_E;
  5207. }
  5208. XMEMSET(cipher_big, 0, CHACHA_BIG_TEST_SIZE);
  5209. XMEMSET(plain_big, 0, CHACHA_BIG_TEST_SIZE);
  5210. XMEMSET(input_big, 0, CHACHA_BIG_TEST_SIZE);
  5211. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  5212. #endif /* BENCH_EMBEDDED */
  5213. for (i = 0; i < times; ++i) {
  5214. if (i < 3) {
  5215. keySz = 32;
  5216. }
  5217. else {
  5218. keySz = 16;
  5219. }
  5220. XMEMCPY(plain, keys[i], keySz);
  5221. XMEMSET(cipher, 0, 32);
  5222. XMEMCPY(cipher + 4, ivs[i], 8);
  5223. ret |= wc_Chacha_SetKey(&enc, keys[i], keySz);
  5224. ret |= wc_Chacha_SetKey(&dec, keys[i], keySz);
  5225. if (ret != 0)
  5226. return ret;
  5227. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  5228. ret |= wc_Chacha_SetIV(&dec, cipher, 0);
  5229. if (ret != 0)
  5230. return ret;
  5231. XMEMCPY(plain, input, 8);
  5232. ret |= wc_Chacha_Process(&enc, cipher, plain, (word32)8);
  5233. ret |= wc_Chacha_Process(&dec, plain, cipher, (word32)8);
  5234. if (ret != 0)
  5235. return ret;
  5236. if (XMEMCMP(test_chacha[i], cipher, 8))
  5237. return -4700 - i;
  5238. if (XMEMCMP(plain, input, 8))
  5239. return -4710 - i;
  5240. }
  5241. /* test of starting at a different counter
  5242. encrypts all of the information and decrypts starting at 2nd chunk */
  5243. XMEMSET(plain, 0, sizeof(plain));
  5244. XMEMSET(sliver, 1, sizeof(sliver)); /* set as 1's to not match plain */
  5245. XMEMSET(cipher, 0, sizeof(cipher));
  5246. XMEMCPY(cipher + 4, ivs[0], 8);
  5247. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  5248. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  5249. if (ret != 0)
  5250. return ret;
  5251. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  5252. ret |= wc_Chacha_SetIV(&dec, cipher, 1);
  5253. if (ret != 0)
  5254. return ret;
  5255. ret |= wc_Chacha_Process(&enc, cipher, plain, sizeof(plain));
  5256. ret |= wc_Chacha_Process(&dec, sliver, cipher + 64, sizeof(sliver));
  5257. if (ret != 0)
  5258. return ret;
  5259. if (XMEMCMP(plain + 64, sliver, 64))
  5260. return -4720;
  5261. #ifndef BENCH_EMBEDDED
  5262. /* test of encrypting more data */
  5263. keySz = 32;
  5264. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  5265. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  5266. if (ret != 0)
  5267. return ret;
  5268. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  5269. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  5270. if (ret != 0)
  5271. return ret;
  5272. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big, CHACHA_BIG_TEST_SIZE);
  5273. ret |= wc_Chacha_Process(&dec, plain_big, cipher_big,
  5274. CHACHA_BIG_TEST_SIZE);
  5275. if (ret != 0)
  5276. return ret;
  5277. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  5278. return -4721;
  5279. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  5280. return -4722;
  5281. for (i = 0; i < 18; ++i) {
  5282. /* this will test all paths
  5283. * block sizes: 1 3 7 15 31 63 127 255 511 (i = 0- 8)
  5284. * 2 4 8 16 32 64 128 256 512 (i = 9-17)
  5285. */
  5286. block_size = (2 << (i%9)) - (i<9?1:0);
  5287. keySz = 32;
  5288. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  5289. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  5290. if (ret != 0)
  5291. return ret;
  5292. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  5293. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  5294. if (ret != 0)
  5295. return ret;
  5296. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big , block_size);
  5297. ret |= wc_Chacha_Process(&dec, plain_big , cipher_big, block_size);
  5298. if (ret != 0)
  5299. return ret;
  5300. if (XMEMCMP(plain_big, input_big, block_size))
  5301. return -4740-i*2;
  5302. if (XMEMCMP(cipher_big, cipher_big_result, block_size))
  5303. return -4741-i*2;
  5304. }
  5305. /* Streaming test */
  5306. for (i = 1; i <= (int)CHACHA_CHUNK_BYTES + 1; i++) {
  5307. int j, rem;
  5308. ret = wc_Chacha_SetKey(&enc, keys[0], keySz);
  5309. if (ret != 0)
  5310. return -4725;
  5311. ret = wc_Chacha_SetKey(&dec, keys[0], keySz);
  5312. if (ret != 0)
  5313. return -4726;
  5314. ret = wc_Chacha_SetIV(&enc, ivs[2], 0);
  5315. if (ret != 0)
  5316. return -4727;
  5317. ret = wc_Chacha_SetIV(&dec, ivs[2], 0);
  5318. if (ret != 0)
  5319. return -4728;
  5320. for (j = 0; j < CHACHA_BIG_TEST_SIZE - i; j+= i) {
  5321. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, i);
  5322. if (ret != 0)
  5323. return -4729;
  5324. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, i);
  5325. if (ret != 0)
  5326. return -4730;
  5327. }
  5328. rem = CHACHA_BIG_TEST_SIZE - j;
  5329. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, rem);
  5330. if (ret != 0)
  5331. return -4731;
  5332. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, rem);
  5333. if (ret != 0)
  5334. return -4732;
  5335. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  5336. return -4733;
  5337. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  5338. return -4734;
  5339. }
  5340. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  5341. XFREE(cipher_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5342. XFREE(plain_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5343. XFREE(input_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5344. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  5345. #endif /* BENCH_EMBEDDED */
  5346. return 0;
  5347. }
  5348. #endif /* HAVE_CHACHA */
  5349. #ifdef HAVE_POLY1305
  5350. WOLFSSL_TEST_SUBROUTINE int poly1305_test(void)
  5351. {
  5352. int ret = 0;
  5353. int i;
  5354. byte tag[16];
  5355. Poly1305 enc;
  5356. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  5357. {
  5358. 0x43,0x72,0x79,0x70,0x74,0x6f,0x67,0x72,
  5359. 0x61,0x70,0x68,0x69,0x63,0x20,0x46,0x6f,
  5360. 0x72,0x75,0x6d,0x20,0x52,0x65,0x73,0x65,
  5361. 0x61,0x72,0x63,0x68,0x20,0x47,0x72,0x6f,
  5362. 0x75,0x70
  5363. };
  5364. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  5365. {
  5366. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x77,0x6f,0x72,
  5367. 0x6c,0x64,0x21
  5368. };
  5369. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  5370. {
  5371. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5372. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5373. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5374. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5375. };
  5376. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] =
  5377. {
  5378. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  5379. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  5380. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  5381. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  5382. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  5383. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  5384. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  5385. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  5386. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  5387. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  5388. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  5389. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  5390. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  5391. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  5392. 0x61,0x16
  5393. };
  5394. WOLFSSL_SMALL_STACK_STATIC const byte msg5[] =
  5395. {
  5396. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  5397. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  5398. };
  5399. WOLFSSL_SMALL_STACK_STATIC const byte msg6[] =
  5400. {
  5401. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  5402. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  5403. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  5404. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  5405. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  5406. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  5407. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  5408. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  5409. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  5410. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  5411. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  5412. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  5413. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  5414. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  5415. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  5416. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  5417. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  5418. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  5419. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  5420. 0x61,0x16
  5421. };
  5422. byte additional[] =
  5423. {
  5424. 0x50,0x51,0x52,0x53,0xc0,0xc1,0xc2,0xc3,
  5425. 0xc4,0xc5,0xc6,0xc7
  5426. };
  5427. WOLFSSL_SMALL_STACK_STATIC const byte correct0[] =
  5428. {
  5429. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  5430. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  5431. };
  5432. WOLFSSL_SMALL_STACK_STATIC const byte correct1[] =
  5433. {
  5434. 0xa8,0x06,0x1d,0xc1,0x30,0x51,0x36,0xc6,
  5435. 0xc2,0x2b,0x8b,0xaf,0x0c,0x01,0x27,0xa9
  5436. };
  5437. WOLFSSL_SMALL_STACK_STATIC const byte correct2[] =
  5438. {
  5439. 0xa6,0xf7,0x45,0x00,0x8f,0x81,0xc9,0x16,
  5440. 0xa2,0x0d,0xcc,0x74,0xee,0xf2,0xb2,0xf0
  5441. };
  5442. WOLFSSL_SMALL_STACK_STATIC const byte correct3[] =
  5443. {
  5444. 0x49,0xec,0x78,0x09,0x0e,0x48,0x1e,0xc6,
  5445. 0xc2,0x6b,0x33,0xb9,0x1c,0xcc,0x03,0x07
  5446. };
  5447. WOLFSSL_SMALL_STACK_STATIC const byte correct4[] =
  5448. {
  5449. 0x1a,0xe1,0x0b,0x59,0x4f,0x09,0xe2,0x6a,
  5450. 0x7e,0x90,0x2e,0xcb,0xd0,0x60,0x06,0x91
  5451. };
  5452. WOLFSSL_SMALL_STACK_STATIC const byte correct5[] =
  5453. {
  5454. 0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5455. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5456. };
  5457. WOLFSSL_SMALL_STACK_STATIC const byte correct6[] =
  5458. {
  5459. 0xea,0x11,0x5c,0x4f,0xd0,0xc0,0x10,0xae,
  5460. 0xf7,0xdf,0xda,0x77,0xa2,0xe9,0xaf,0xca
  5461. };
  5462. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  5463. 0x85,0xd6,0xbe,0x78,0x57,0x55,0x6d,0x33,
  5464. 0x7f,0x44,0x52,0xfe,0x42,0xd5,0x06,0xa8,
  5465. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  5466. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  5467. };
  5468. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  5469. 0x74,0x68,0x69,0x73,0x20,0x69,0x73,0x20,
  5470. 0x33,0x32,0x2d,0x62,0x79,0x74,0x65,0x20,
  5471. 0x6b,0x65,0x79,0x20,0x66,0x6f,0x72,0x20,
  5472. 0x50,0x6f,0x6c,0x79,0x31,0x33,0x30,0x35
  5473. };
  5474. WOLFSSL_SMALL_STACK_STATIC const byte key4[] = {
  5475. 0x7b,0xac,0x2b,0x25,0x2d,0xb4,0x47,0xaf,
  5476. 0x09,0xb6,0x7a,0x55,0xa4,0xe9,0x55,0x84,
  5477. 0x0a,0xe1,0xd6,0x73,0x10,0x75,0xd9,0xeb,
  5478. 0x2a,0x93,0x75,0x78,0x3e,0xd5,0x53,0xff
  5479. };
  5480. WOLFSSL_SMALL_STACK_STATIC const byte key5[] = {
  5481. 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5482. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5483. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5484. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5485. };
  5486. const byte* msgs[] = {NULL, msg1, msg2, msg3, msg5, msg6};
  5487. word32 szm[] = {0, sizeof(msg1), sizeof(msg2),
  5488. sizeof(msg3), sizeof(msg5), sizeof(msg6)};
  5489. const byte* keys[] = {key, key, key2, key2, key5, key};
  5490. const byte* tests[] = {correct0, correct1, correct2, correct3, correct5,
  5491. correct6};
  5492. for (i = 0; i < 6; i++) {
  5493. ret = wc_Poly1305SetKey(&enc, keys[i], 32);
  5494. if (ret != 0)
  5495. return -4800 - i;
  5496. ret = wc_Poly1305Update(&enc, msgs[i], szm[i]);
  5497. if (ret != 0)
  5498. return -4810 - i;
  5499. ret = wc_Poly1305Final(&enc, tag);
  5500. if (ret != 0)
  5501. return -4820 - i;
  5502. if (XMEMCMP(tag, tests[i], sizeof(tag)))
  5503. return -4830 - i;
  5504. }
  5505. /* Check TLS MAC function from 2.8.2 https://tools.ietf.org/html/rfc7539 */
  5506. XMEMSET(tag, 0, sizeof(tag));
  5507. ret = wc_Poly1305SetKey(&enc, key4, sizeof(key4));
  5508. if (ret != 0)
  5509. return -4840;
  5510. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  5511. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  5512. if (ret != 0)
  5513. return -4841;
  5514. if (XMEMCMP(tag, correct4, sizeof(tag)))
  5515. return -4842;
  5516. /* Check fail of TLS MAC function if altering additional data */
  5517. XMEMSET(tag, 0, sizeof(tag));
  5518. additional[0]++;
  5519. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  5520. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  5521. if (ret != 0)
  5522. return -4843;
  5523. if (XMEMCMP(tag, correct4, sizeof(tag)) == 0)
  5524. return -4844;
  5525. return 0;
  5526. }
  5527. #endif /* HAVE_POLY1305 */
  5528. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  5529. WOLFSSL_TEST_SUBROUTINE int chacha20_poly1305_aead_test(void)
  5530. {
  5531. /* Test #1 from Section 2.8.2 of draft-irtf-cfrg-chacha20-poly1305-10 */
  5532. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  5533. WOLFSSL_SMALL_STACK_STATIC const byte key1[] = {
  5534. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  5535. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  5536. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  5537. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  5538. };
  5539. WOLFSSL_SMALL_STACK_STATIC const byte plaintext1[] = {
  5540. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  5541. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  5542. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  5543. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  5544. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  5545. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  5546. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  5547. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  5548. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  5549. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  5550. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  5551. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  5552. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  5553. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  5554. 0x74, 0x2e
  5555. };
  5556. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] = {
  5557. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  5558. 0x44, 0x45, 0x46, 0x47
  5559. };
  5560. WOLFSSL_SMALL_STACK_STATIC const byte aad1[] = { /* additional data */
  5561. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  5562. 0xc4, 0xc5, 0xc6, 0xc7
  5563. };
  5564. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] = { /* expected output from operation */
  5565. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  5566. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  5567. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  5568. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  5569. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  5570. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  5571. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  5572. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  5573. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  5574. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  5575. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  5576. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  5577. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  5578. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  5579. 0x61, 0x16
  5580. };
  5581. WOLFSSL_SMALL_STACK_STATIC const byte authTag1[] = { /* expected output from operation */
  5582. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  5583. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  5584. };
  5585. /* Test #2 from Appendix A.2 in draft-irtf-cfrg-chacha20-poly1305-10 */
  5586. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  5587. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  5588. 0x1c, 0x92, 0x40, 0xa5, 0xeb, 0x55, 0xd3, 0x8a,
  5589. 0xf3, 0x33, 0x88, 0x86, 0x04, 0xf6, 0xb5, 0xf0,
  5590. 0x47, 0x39, 0x17, 0xc1, 0x40, 0x2b, 0x80, 0x09,
  5591. 0x9d, 0xca, 0x5c, 0xbc, 0x20, 0x70, 0x75, 0xc0
  5592. };
  5593. WOLFSSL_SMALL_STACK_STATIC const byte plaintext2[] = {
  5594. 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74,
  5595. 0x2d, 0x44, 0x72, 0x61, 0x66, 0x74, 0x73, 0x20,
  5596. 0x61, 0x72, 0x65, 0x20, 0x64, 0x72, 0x61, 0x66,
  5597. 0x74, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  5598. 0x6e, 0x74, 0x73, 0x20, 0x76, 0x61, 0x6c, 0x69,
  5599. 0x64, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x61, 0x20,
  5600. 0x6d, 0x61, 0x78, 0x69, 0x6d, 0x75, 0x6d, 0x20,
  5601. 0x6f, 0x66, 0x20, 0x73, 0x69, 0x78, 0x20, 0x6d,
  5602. 0x6f, 0x6e, 0x74, 0x68, 0x73, 0x20, 0x61, 0x6e,
  5603. 0x64, 0x20, 0x6d, 0x61, 0x79, 0x20, 0x62, 0x65,
  5604. 0x20, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64,
  5605. 0x2c, 0x20, 0x72, 0x65, 0x70, 0x6c, 0x61, 0x63,
  5606. 0x65, 0x64, 0x2c, 0x20, 0x6f, 0x72, 0x20, 0x6f,
  5607. 0x62, 0x73, 0x6f, 0x6c, 0x65, 0x74, 0x65, 0x64,
  5608. 0x20, 0x62, 0x79, 0x20, 0x6f, 0x74, 0x68, 0x65,
  5609. 0x72, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  5610. 0x6e, 0x74, 0x73, 0x20, 0x61, 0x74, 0x20, 0x61,
  5611. 0x6e, 0x79, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x2e,
  5612. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x69,
  5613. 0x6e, 0x61, 0x70, 0x70, 0x72, 0x6f, 0x70, 0x72,
  5614. 0x69, 0x61, 0x74, 0x65, 0x20, 0x74, 0x6f, 0x20,
  5615. 0x75, 0x73, 0x65, 0x20, 0x49, 0x6e, 0x74, 0x65,
  5616. 0x72, 0x6e, 0x65, 0x74, 0x2d, 0x44, 0x72, 0x61,
  5617. 0x66, 0x74, 0x73, 0x20, 0x61, 0x73, 0x20, 0x72,
  5618. 0x65, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x65,
  5619. 0x20, 0x6d, 0x61, 0x74, 0x65, 0x72, 0x69, 0x61,
  5620. 0x6c, 0x20, 0x6f, 0x72, 0x20, 0x74, 0x6f, 0x20,
  5621. 0x63, 0x69, 0x74, 0x65, 0x20, 0x74, 0x68, 0x65,
  5622. 0x6d, 0x20, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20,
  5623. 0x74, 0x68, 0x61, 0x6e, 0x20, 0x61, 0x73, 0x20,
  5624. 0x2f, 0xe2, 0x80, 0x9c, 0x77, 0x6f, 0x72, 0x6b,
  5625. 0x20, 0x69, 0x6e, 0x20, 0x70, 0x72, 0x6f, 0x67,
  5626. 0x72, 0x65, 0x73, 0x73, 0x2e, 0x2f, 0xe2, 0x80,
  5627. 0x9d
  5628. };
  5629. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  5630. 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
  5631. 0x05, 0x06, 0x07, 0x08
  5632. };
  5633. WOLFSSL_SMALL_STACK_STATIC const byte aad2[] = { /* additional data */
  5634. 0xf3, 0x33, 0x88, 0x86, 0x00, 0x00, 0x00, 0x00,
  5635. 0x00, 0x00, 0x4e, 0x91
  5636. };
  5637. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] = { /* expected output from operation */
  5638. 0x64, 0xa0, 0x86, 0x15, 0x75, 0x86, 0x1a, 0xf4,
  5639. 0x60, 0xf0, 0x62, 0xc7, 0x9b, 0xe6, 0x43, 0xbd,
  5640. 0x5e, 0x80, 0x5c, 0xfd, 0x34, 0x5c, 0xf3, 0x89,
  5641. 0xf1, 0x08, 0x67, 0x0a, 0xc7, 0x6c, 0x8c, 0xb2,
  5642. 0x4c, 0x6c, 0xfc, 0x18, 0x75, 0x5d, 0x43, 0xee,
  5643. 0xa0, 0x9e, 0xe9, 0x4e, 0x38, 0x2d, 0x26, 0xb0,
  5644. 0xbd, 0xb7, 0xb7, 0x3c, 0x32, 0x1b, 0x01, 0x00,
  5645. 0xd4, 0xf0, 0x3b, 0x7f, 0x35, 0x58, 0x94, 0xcf,
  5646. 0x33, 0x2f, 0x83, 0x0e, 0x71, 0x0b, 0x97, 0xce,
  5647. 0x98, 0xc8, 0xa8, 0x4a, 0xbd, 0x0b, 0x94, 0x81,
  5648. 0x14, 0xad, 0x17, 0x6e, 0x00, 0x8d, 0x33, 0xbd,
  5649. 0x60, 0xf9, 0x82, 0xb1, 0xff, 0x37, 0xc8, 0x55,
  5650. 0x97, 0x97, 0xa0, 0x6e, 0xf4, 0xf0, 0xef, 0x61,
  5651. 0xc1, 0x86, 0x32, 0x4e, 0x2b, 0x35, 0x06, 0x38,
  5652. 0x36, 0x06, 0x90, 0x7b, 0x6a, 0x7c, 0x02, 0xb0,
  5653. 0xf9, 0xf6, 0x15, 0x7b, 0x53, 0xc8, 0x67, 0xe4,
  5654. 0xb9, 0x16, 0x6c, 0x76, 0x7b, 0x80, 0x4d, 0x46,
  5655. 0xa5, 0x9b, 0x52, 0x16, 0xcd, 0xe7, 0xa4, 0xe9,
  5656. 0x90, 0x40, 0xc5, 0xa4, 0x04, 0x33, 0x22, 0x5e,
  5657. 0xe2, 0x82, 0xa1, 0xb0, 0xa0, 0x6c, 0x52, 0x3e,
  5658. 0xaf, 0x45, 0x34, 0xd7, 0xf8, 0x3f, 0xa1, 0x15,
  5659. 0x5b, 0x00, 0x47, 0x71, 0x8c, 0xbc, 0x54, 0x6a,
  5660. 0x0d, 0x07, 0x2b, 0x04, 0xb3, 0x56, 0x4e, 0xea,
  5661. 0x1b, 0x42, 0x22, 0x73, 0xf5, 0x48, 0x27, 0x1a,
  5662. 0x0b, 0xb2, 0x31, 0x60, 0x53, 0xfa, 0x76, 0x99,
  5663. 0x19, 0x55, 0xeb, 0xd6, 0x31, 0x59, 0x43, 0x4e,
  5664. 0xce, 0xbb, 0x4e, 0x46, 0x6d, 0xae, 0x5a, 0x10,
  5665. 0x73, 0xa6, 0x72, 0x76, 0x27, 0x09, 0x7a, 0x10,
  5666. 0x49, 0xe6, 0x17, 0xd9, 0x1d, 0x36, 0x10, 0x94,
  5667. 0xfa, 0x68, 0xf0, 0xff, 0x77, 0x98, 0x71, 0x30,
  5668. 0x30, 0x5b, 0xea, 0xba, 0x2e, 0xda, 0x04, 0xdf,
  5669. 0x99, 0x7b, 0x71, 0x4d, 0x6c, 0x6f, 0x2c, 0x29,
  5670. 0xa6, 0xad, 0x5c, 0xb4, 0x02, 0x2b, 0x02, 0x70,
  5671. 0x9b
  5672. };
  5673. WOLFSSL_SMALL_STACK_STATIC const byte authTag2[] = { /* expected output from operation */
  5674. 0xee, 0xad, 0x9d, 0x67, 0x89, 0x0c, 0xbb, 0x22,
  5675. 0x39, 0x23, 0x36, 0xfe, 0xa1, 0x85, 0x1f, 0x38
  5676. };
  5677. byte generatedCiphertext[265]; /* max plaintext2/cipher2 */
  5678. byte generatedPlaintext[265]; /* max plaintext2/cipher2 */
  5679. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  5680. int err;
  5681. ChaChaPoly_Aead aead;
  5682. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  5683. #define TEST_SMALL_CHACHA_CHUNKS 32
  5684. #else
  5685. #define TEST_SMALL_CHACHA_CHUNKS 64
  5686. #endif
  5687. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5688. word32 testLen;
  5689. #endif
  5690. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5691. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5692. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5693. /* Parameter Validation testing */
  5694. /* Encrypt */
  5695. err = wc_ChaCha20Poly1305_Encrypt(NULL, iv1, aad1, sizeof(aad1), plaintext1,
  5696. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  5697. if (err != BAD_FUNC_ARG)
  5698. return -4900;
  5699. err = wc_ChaCha20Poly1305_Encrypt(key1, NULL, aad1, sizeof(aad1),
  5700. plaintext1, sizeof(plaintext1), generatedCiphertext,
  5701. generatedAuthTag);
  5702. if (err != BAD_FUNC_ARG)
  5703. return -4901;
  5704. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL,
  5705. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  5706. if (err != BAD_FUNC_ARG)
  5707. return -4902;
  5708. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  5709. sizeof(plaintext1), NULL, generatedAuthTag);
  5710. if (err != BAD_FUNC_ARG)
  5711. return -4903;
  5712. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  5713. sizeof(plaintext1), generatedCiphertext, NULL);
  5714. if (err != BAD_FUNC_ARG)
  5715. return -4904;
  5716. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL,
  5717. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  5718. if (err != BAD_FUNC_ARG)
  5719. return -4905;
  5720. /* Decrypt */
  5721. err = wc_ChaCha20Poly1305_Decrypt(NULL, iv2, aad2, sizeof(aad2), cipher2,
  5722. sizeof(cipher2), authTag2, generatedPlaintext);
  5723. if (err != BAD_FUNC_ARG)
  5724. return -4906;
  5725. err = wc_ChaCha20Poly1305_Decrypt(key2, NULL, aad2, sizeof(aad2), cipher2,
  5726. sizeof(cipher2), authTag2, generatedPlaintext);
  5727. if (err != BAD_FUNC_ARG)
  5728. return -4907;
  5729. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL,
  5730. sizeof(cipher2), authTag2, generatedPlaintext);
  5731. if (err != BAD_FUNC_ARG)
  5732. return -4908;
  5733. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  5734. sizeof(cipher2), NULL, generatedPlaintext);
  5735. if (err != BAD_FUNC_ARG)
  5736. return -4909;
  5737. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  5738. sizeof(cipher2), authTag2, NULL);
  5739. if (err != BAD_FUNC_ARG)
  5740. return -4910;
  5741. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL,
  5742. sizeof(cipher2), authTag2, generatedPlaintext);
  5743. if (err != BAD_FUNC_ARG)
  5744. return -4911;
  5745. /* Test #1 */
  5746. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1,
  5747. aad1, sizeof(aad1),
  5748. plaintext1, sizeof(plaintext1),
  5749. generatedCiphertext, generatedAuthTag);
  5750. if (err) {
  5751. return err;
  5752. }
  5753. /* -- Check the ciphertext and authtag */
  5754. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  5755. return -4912;
  5756. }
  5757. if (XMEMCMP(generatedAuthTag, authTag1, sizeof(authTag1))) {
  5758. return -4913;
  5759. }
  5760. /* -- Verify decryption works */
  5761. err = wc_ChaCha20Poly1305_Decrypt(key1, iv1,
  5762. aad1, sizeof(aad1),
  5763. cipher1, sizeof(cipher1),
  5764. authTag1, generatedPlaintext);
  5765. if (err) {
  5766. return err;
  5767. }
  5768. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  5769. return -4914;
  5770. }
  5771. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5772. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5773. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5774. /* Test #2 */
  5775. err = wc_ChaCha20Poly1305_Encrypt(key2, iv2,
  5776. aad2, sizeof(aad2),
  5777. plaintext2, sizeof(plaintext2),
  5778. generatedCiphertext, generatedAuthTag);
  5779. if (err) {
  5780. return err;
  5781. }
  5782. /* -- Check the ciphertext and authtag */
  5783. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  5784. return -4915;
  5785. }
  5786. if (XMEMCMP(generatedAuthTag, authTag2, sizeof(authTag2))) {
  5787. return -4916;
  5788. }
  5789. /* -- Verify decryption works */
  5790. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2,
  5791. aad2, sizeof(aad2),
  5792. cipher2, sizeof(cipher2),
  5793. authTag2, generatedPlaintext);
  5794. if (err) {
  5795. return err;
  5796. }
  5797. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  5798. return -4917;
  5799. }
  5800. /* AEAD init/update/final - bad argument tests */
  5801. err = wc_ChaCha20Poly1305_Init(NULL, key1, iv1,
  5802. CHACHA20_POLY1305_AEAD_DECRYPT);
  5803. if (err != BAD_FUNC_ARG)
  5804. return -4918;
  5805. err = wc_ChaCha20Poly1305_Init(&aead, NULL, iv1,
  5806. CHACHA20_POLY1305_AEAD_DECRYPT);
  5807. if (err != BAD_FUNC_ARG)
  5808. return -4919;
  5809. err = wc_ChaCha20Poly1305_Init(&aead, key1, NULL,
  5810. CHACHA20_POLY1305_AEAD_DECRYPT);
  5811. if (err != BAD_FUNC_ARG)
  5812. return -4920;
  5813. err = wc_ChaCha20Poly1305_UpdateAad(NULL, aad1, sizeof(aad1));
  5814. if (err != BAD_FUNC_ARG)
  5815. return -4921;
  5816. err = wc_ChaCha20Poly1305_UpdateAad(&aead, NULL, sizeof(aad1));
  5817. if (err != BAD_FUNC_ARG)
  5818. return -4922;
  5819. err = wc_ChaCha20Poly1305_UpdateData(NULL, generatedPlaintext,
  5820. generatedPlaintext, sizeof(plaintext1));
  5821. if (err != BAD_FUNC_ARG)
  5822. return -4923;
  5823. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext, NULL,
  5824. sizeof(plaintext1));
  5825. if (err != BAD_FUNC_ARG)
  5826. return -4924;
  5827. err = wc_ChaCha20Poly1305_UpdateData(&aead, NULL, generatedPlaintext,
  5828. sizeof(plaintext1));
  5829. if (err != BAD_FUNC_ARG)
  5830. return -4925;
  5831. err = wc_ChaCha20Poly1305_Final(NULL, generatedAuthTag);
  5832. if (err != BAD_FUNC_ARG)
  5833. return -4926;
  5834. err = wc_ChaCha20Poly1305_Final(&aead, NULL);
  5835. if (err != BAD_FUNC_ARG)
  5836. return -4927;
  5837. /* AEAD init/update/final - bad state tests */
  5838. /* clear struct - make valgrind happy to resolve
  5839. "Conditional jump or move depends on uninitialised value(s)".
  5840. The enum is "int" size and aead.state is "byte" */
  5841. /* The wc_ChaCha20Poly1305_Init function does this normally */
  5842. XMEMSET(&aead, 0, sizeof(aead));
  5843. aead.state = CHACHA20_POLY1305_STATE_INIT;
  5844. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5845. if (err != BAD_STATE_E)
  5846. return -4928;
  5847. aead.state = CHACHA20_POLY1305_STATE_DATA;
  5848. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5849. if (err != BAD_STATE_E)
  5850. return -4929;
  5851. aead.state = CHACHA20_POLY1305_STATE_INIT;
  5852. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext,
  5853. generatedPlaintext, sizeof(plaintext1));
  5854. if (err != BAD_STATE_E)
  5855. return -4930;
  5856. aead.state = CHACHA20_POLY1305_STATE_INIT;
  5857. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5858. if (err != BAD_STATE_E)
  5859. return -4931;
  5860. aead.state = CHACHA20_POLY1305_STATE_READY;
  5861. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5862. if (err != BAD_STATE_E)
  5863. return -4932;
  5864. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5865. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5866. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5867. /* Test 1 - Encrypt */
  5868. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  5869. CHACHA20_POLY1305_AEAD_ENCRYPT);
  5870. if (err != 0)
  5871. return -4933;
  5872. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5873. if (err != 0)
  5874. return -4934;
  5875. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5876. /* test doing data in smaller chunks */
  5877. for (testLen=0; testLen<sizeof(plaintext1); ) {
  5878. word32 dataLen = sizeof(plaintext1) - testLen;
  5879. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5880. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5881. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext1[testLen],
  5882. &generatedCiphertext[testLen], dataLen);
  5883. if (err != 0)
  5884. return -4935;
  5885. testLen += dataLen;
  5886. }
  5887. #else
  5888. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext1,
  5889. generatedCiphertext, sizeof(plaintext1));
  5890. #endif
  5891. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5892. if (err != 0)
  5893. return -4936;
  5894. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  5895. if (err != 0)
  5896. return -4937;
  5897. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  5898. return -4938;
  5899. }
  5900. /* Test 1 - Decrypt */
  5901. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  5902. CHACHA20_POLY1305_AEAD_DECRYPT);
  5903. if (err != 0)
  5904. return -4939;
  5905. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5906. if (err != 0)
  5907. return -4940;
  5908. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5909. /* test doing data in smaller chunks */
  5910. for (testLen=0; testLen<sizeof(plaintext1); ) {
  5911. word32 dataLen = sizeof(plaintext1) - testLen;
  5912. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5913. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5914. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  5915. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  5916. dataLen);
  5917. if (err != 0)
  5918. return -4941;
  5919. testLen += dataLen;
  5920. }
  5921. #else
  5922. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  5923. generatedPlaintext, sizeof(cipher1));
  5924. #endif
  5925. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5926. if (err != 0)
  5927. return -4942;
  5928. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  5929. if (err != 0)
  5930. return -4943;
  5931. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  5932. return -4944;
  5933. }
  5934. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5935. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5936. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5937. /* Test 2 - Encrypt */
  5938. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  5939. CHACHA20_POLY1305_AEAD_ENCRYPT);
  5940. if (err != 0)
  5941. return -4945;
  5942. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  5943. if (err != 0)
  5944. return -4946;
  5945. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5946. /* test doing data in smaller chunks */
  5947. for (testLen=0; testLen<sizeof(plaintext2); ) {
  5948. word32 dataLen = sizeof(plaintext2) - testLen;
  5949. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5950. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5951. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext2[testLen],
  5952. &generatedCiphertext[testLen], dataLen);
  5953. if (err != 0)
  5954. return -4947;
  5955. testLen += dataLen;
  5956. }
  5957. #else
  5958. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext2, generatedCiphertext,
  5959. sizeof(plaintext2));
  5960. #endif
  5961. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5962. if (err != 0)
  5963. return -4948;
  5964. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  5965. if (err != 0)
  5966. return -4949;
  5967. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  5968. return -4950;
  5969. }
  5970. /* Test 2 - Decrypt */
  5971. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  5972. CHACHA20_POLY1305_AEAD_DECRYPT);
  5973. if (err != 0)
  5974. return -4951;
  5975. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  5976. if (err != 0)
  5977. return -4952;
  5978. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5979. /* test doing data in smaller chunks */
  5980. for (testLen=0; testLen<sizeof(plaintext2); ) {
  5981. word32 dataLen = sizeof(plaintext2) - testLen;
  5982. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5983. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5984. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  5985. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  5986. dataLen);
  5987. if (err != 0)
  5988. return -4953;
  5989. testLen += dataLen;
  5990. }
  5991. #else
  5992. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  5993. generatedPlaintext, sizeof(cipher2));
  5994. #endif
  5995. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5996. if (err != 0)
  5997. return -4954;
  5998. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  5999. if (err != 0)
  6000. return -4955;
  6001. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  6002. return -4956;
  6003. }
  6004. return err;
  6005. }
  6006. #endif /* HAVE_CHACHA && HAVE_POLY1305 */
  6007. #ifndef NO_DES3
  6008. WOLFSSL_TEST_SUBROUTINE int des_test(void)
  6009. {
  6010. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  6011. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  6012. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  6013. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  6014. };
  6015. byte plain[24];
  6016. byte cipher[24];
  6017. Des enc;
  6018. Des dec;
  6019. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  6020. {
  6021. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  6022. };
  6023. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  6024. {
  6025. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  6026. };
  6027. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  6028. {
  6029. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  6030. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  6031. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  6032. };
  6033. int ret;
  6034. ret = wc_Des_SetKey(&enc, key, iv, DES_ENCRYPTION);
  6035. if (ret != 0)
  6036. return -5000;
  6037. ret = wc_Des_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  6038. if (ret != 0)
  6039. return -5001;
  6040. ret = wc_Des_SetKey(&dec, key, iv, DES_DECRYPTION);
  6041. if (ret != 0)
  6042. return -5002;
  6043. ret = wc_Des_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  6044. if (ret != 0)
  6045. return -5003;
  6046. if (XMEMCMP(plain, vector, sizeof(plain)))
  6047. return -5004;
  6048. if (XMEMCMP(cipher, verify, sizeof(cipher)))
  6049. return -5005;
  6050. ret = wc_Des_CbcEncryptWithKey(cipher, vector, sizeof(vector), key, iv);
  6051. if (ret != 0)
  6052. return -5006;
  6053. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  6054. {
  6055. EncryptedInfo info;
  6056. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  6057. XMEMCPY(info.iv, iv, sizeof(iv));
  6058. info.ivSz = sizeof(iv);
  6059. info.keySz = sizeof(key);
  6060. info.cipherType = WC_CIPHER_DES;
  6061. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  6062. sizeof(key), WC_HASH_TYPE_SHA);
  6063. if (ret != 0)
  6064. return -5007;
  6065. /* Test invalid info ptr */
  6066. ret = wc_BufferKeyEncrypt(NULL, cipher, sizeof(cipher), key,
  6067. sizeof(key), WC_HASH_TYPE_SHA);
  6068. if (ret != BAD_FUNC_ARG)
  6069. return -5008;
  6070. #ifndef NO_PWDBASED
  6071. /* Test invalid hash type - only applies to wc_PBKDF1 call */
  6072. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  6073. sizeof(key), WC_HASH_TYPE_NONE);
  6074. if (ret == 0)
  6075. return -5009;
  6076. #endif /* !NO_PWDBASED */
  6077. }
  6078. #endif
  6079. return 0;
  6080. }
  6081. #endif /* !NO_DES3 */
  6082. #ifndef NO_DES3
  6083. WOLFSSL_TEST_SUBROUTINE int des3_test(void)
  6084. {
  6085. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  6086. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  6087. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  6088. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  6089. };
  6090. byte plain[24];
  6091. byte cipher[24];
  6092. Des3 enc;
  6093. Des3 dec;
  6094. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6095. {
  6096. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  6097. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  6098. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  6099. };
  6100. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  6101. {
  6102. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  6103. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  6104. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  6105. };
  6106. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] =
  6107. {
  6108. 0x43,0xa0,0x29,0x7e,0xd1,0x84,0xf8,0x0e,
  6109. 0x89,0x64,0x84,0x32,0x12,0xd5,0x08,0x98,
  6110. 0x18,0x94,0x15,0x74,0x87,0x12,0x7d,0xb0
  6111. };
  6112. int ret;
  6113. if (wc_Des3Init(&enc, HEAP_HINT, devId) != 0)
  6114. return -5100;
  6115. if (wc_Des3Init(&dec, HEAP_HINT, devId) != 0)
  6116. return -5101;
  6117. ret = wc_Des3_SetKey(&enc, key3, iv3, DES_ENCRYPTION);
  6118. if (ret != 0)
  6119. return -5102;
  6120. ret = wc_Des3_SetKey(&dec, key3, iv3, DES_DECRYPTION);
  6121. if (ret != 0)
  6122. return -5103;
  6123. ret = wc_Des3_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  6124. #if defined(WOLFSSL_ASYNC_CRYPT)
  6125. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  6126. #endif
  6127. if (ret != 0)
  6128. return -5104;
  6129. ret = wc_Des3_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  6130. #if defined(WOLFSSL_ASYNC_CRYPT)
  6131. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  6132. #endif
  6133. if (ret != 0)
  6134. return -5105;
  6135. if (XMEMCMP(plain, vector, sizeof(plain)))
  6136. return -5106;
  6137. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  6138. return -5107;
  6139. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  6140. /* test the same vectors with using compatibility layer */
  6141. {
  6142. DES_key_schedule ks1;
  6143. DES_key_schedule ks2;
  6144. DES_key_schedule ks3;
  6145. DES_cblock iv4;
  6146. XMEMCPY(ks1, key3, sizeof(DES_key_schedule));
  6147. XMEMCPY(ks2, key3 + 8, sizeof(DES_key_schedule));
  6148. XMEMCPY(ks3, key3 + 16, sizeof(DES_key_schedule));
  6149. XMEMCPY(iv4, iv3, sizeof(DES_cblock));
  6150. XMEMSET(plain, 0, sizeof(plain));
  6151. XMEMSET(cipher, 0, sizeof(cipher));
  6152. DES_ede3_cbc_encrypt(vector, cipher, sizeof(vector), &ks1, &ks2, &ks3,
  6153. &iv4, DES_ENCRYPT);
  6154. DES_ede3_cbc_encrypt(cipher, plain, sizeof(cipher), &ks1, &ks2, &ks3,
  6155. &iv4, DES_DECRYPT);
  6156. if (XMEMCMP(plain, vector, sizeof(plain)))
  6157. return -5108;
  6158. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  6159. return -5109;
  6160. }
  6161. #endif /* OPENSSL_EXTRA */
  6162. wc_Des3Free(&enc);
  6163. wc_Des3Free(&dec);
  6164. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  6165. {
  6166. EncryptedInfo info;
  6167. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  6168. XMEMCPY(info.iv, iv3, sizeof(iv3));
  6169. info.ivSz = sizeof(iv3);
  6170. info.keySz = sizeof(key3);
  6171. info.cipherType = WC_CIPHER_DES3;
  6172. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key3,
  6173. sizeof(key3), WC_HASH_TYPE_SHA);
  6174. if (ret != 0)
  6175. return -5110;
  6176. }
  6177. #endif
  6178. return 0;
  6179. }
  6180. #endif /* NO_DES3 */
  6181. #ifndef NO_AES
  6182. #if defined(WOLFSSL_AES_OFB) || defined(WOLFSSL_AES_CFB) || \
  6183. defined(WOLFSSL_AES_XTS)
  6184. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6185. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6186. /* pass in the function, key, iv, plain text and expected and this function
  6187. * tests that the encryption and decryption is successful */
  6188. static int EVP_test(const WOLFSSL_EVP_CIPHER* type, const byte* key,
  6189. const byte* iv, const byte* plain, int plainSz,
  6190. const byte* expected, int expectedSz)
  6191. {
  6192. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6193. EVP_CIPHER_CTX *ctx = NULL;
  6194. #else
  6195. EVP_CIPHER_CTX ctx[1];
  6196. #endif
  6197. int idx, ret = 0, cipherSz;
  6198. byte* cipher;
  6199. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6200. if ((ctx = wolfSSL_EVP_CIPHER_CTX_new()) == NULL)
  6201. return MEMORY_E;
  6202. #endif
  6203. cipher = (byte*)XMALLOC(plainSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6204. if (cipher == NULL) {
  6205. ret = -5120;
  6206. goto EVP_TEST_END;
  6207. }
  6208. /* test encrypt */
  6209. EVP_CIPHER_CTX_init(ctx);
  6210. if (EVP_CipherInit(ctx, type, key, iv, 1) == 0) {
  6211. ret = -5121;
  6212. goto EVP_TEST_END;
  6213. }
  6214. if (EVP_CipherUpdate(ctx, cipher, &idx, plain, expectedSz) == 0) {
  6215. ret = -5122;
  6216. goto EVP_TEST_END;
  6217. }
  6218. cipherSz = idx;
  6219. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  6220. ret = -5123;
  6221. goto EVP_TEST_END;
  6222. }
  6223. cipherSz += idx;
  6224. if (XMEMCMP(cipher, expected, plainSz)) {
  6225. ret = -5124;
  6226. goto EVP_TEST_END;
  6227. }
  6228. /* test decrypt */
  6229. EVP_CIPHER_CTX_init(ctx);
  6230. if (EVP_CipherInit(ctx, type, key, iv, 0) == 0) {
  6231. ret = -5125;
  6232. goto EVP_TEST_END;
  6233. }
  6234. if (EVP_CipherUpdate(ctx, cipher, &idx, cipher, expectedSz) == 0) {
  6235. ret = -5126;
  6236. goto EVP_TEST_END;
  6237. }
  6238. cipherSz = idx;
  6239. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  6240. ret = -5127;
  6241. goto EVP_TEST_END;
  6242. }
  6243. cipherSz += idx;
  6244. if ((expectedSz != cipherSz) || XMEMCMP(plain, cipher, plainSz)) {
  6245. ret = -5128;
  6246. goto EVP_TEST_END;
  6247. }
  6248. EVP_TEST_END:
  6249. if (cipher)
  6250. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6251. (void)cipherSz;
  6252. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6253. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  6254. #endif
  6255. return ret;
  6256. }
  6257. #endif /* OPENSSL_EXTRA */
  6258. #endif /* WOLFSSL_AES_OFB || WOLFSSL_AES_CFB */
  6259. #ifdef WOLFSSL_AES_OFB
  6260. /* test vector from https://csrc.nist.gov/Projects/cryptographic-algorithm-validation-program/Block-Ciphers */
  6261. WOLFSSL_TEST_SUBROUTINE int aesofb_test(void)
  6262. {
  6263. #ifdef WOLFSSL_AES_256
  6264. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6265. {
  6266. 0xc4,0xc7,0xfa,0xd6,0x53,0x5c,0xb8,0x71,
  6267. 0x4a,0x5c,0x40,0x77,0x9a,0x8b,0xa1,0xd2,
  6268. 0x53,0x3e,0x23,0xb4,0xb2,0x58,0x73,0x2a,
  6269. 0x5b,0x78,0x01,0xf4,0xe3,0x71,0xa7,0x94
  6270. };
  6271. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  6272. {
  6273. 0x5e,0xb9,0x33,0x13,0xb8,0x71,0xff,0x16,
  6274. 0xb9,0x8a,0x9b,0xcb,0x43,0x33,0x0d,0x6f
  6275. };
  6276. WOLFSSL_SMALL_STACK_STATIC const byte plain1[] =
  6277. {
  6278. 0x6d,0x0b,0xb0,0x79,0x63,0x84,0x71,0xe9,
  6279. 0x39,0xd4,0x53,0x14,0x86,0xc1,0x4c,0x25,
  6280. 0x9a,0xee,0xc6,0xf3,0xc0,0x0d,0xfd,0xd6,
  6281. 0xc0,0x50,0xa8,0xba,0xa8,0x20,0xdb,0x71,
  6282. 0xcc,0x12,0x2c,0x4e,0x0c,0x17,0x15,0xef,
  6283. 0x55,0xf3,0x99,0x5a,0x6b,0xf0,0x2a,0x4c
  6284. };
  6285. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  6286. {
  6287. 0x0f,0x54,0x61,0x71,0x59,0xd0,0x3f,0xfc,
  6288. 0x1b,0xfa,0xfb,0x60,0x29,0x30,0xd7,0x00,
  6289. 0xf4,0xa4,0xa8,0xe6,0xdd,0x93,0x94,0x46,
  6290. 0x64,0xd2,0x19,0xc4,0xc5,0x4d,0xde,0x1b,
  6291. 0x04,0x53,0xe1,0x73,0xf5,0x18,0x74,0xae,
  6292. 0xfd,0x64,0xa2,0xe1,0xe2,0x76,0x13,0xb0
  6293. };
  6294. #endif /* WOLFSSL_AES_256 */
  6295. #ifdef WOLFSSL_AES_128
  6296. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6297. {
  6298. 0x10,0xa5,0x88,0x69,0xd7,0x4b,0xe5,0xa3,
  6299. 0x74,0xcf,0x86,0x7c,0xfb,0x47,0x38,0x59
  6300. };
  6301. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  6302. {
  6303. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6304. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6305. };
  6306. WOLFSSL_SMALL_STACK_STATIC const byte plain2[] =
  6307. {
  6308. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6309. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6310. };
  6311. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  6312. {
  6313. 0x6d,0x25,0x1e,0x69,0x44,0xb0,0x51,0xe0,
  6314. 0x4e,0xaa,0x6f,0xb4,0xdb,0xf7,0x84,0x65
  6315. };
  6316. #endif /* WOLFSSL_AES_128 */
  6317. #ifdef WOLFSSL_AES_192
  6318. WOLFSSL_SMALL_STACK_STATIC const byte key3[] = {
  6319. 0xd0,0x77,0xa0,0x3b,0xd8,0xa3,0x89,0x73,
  6320. 0x92,0x8c,0xca,0xfe,0x4a,0x9d,0x2f,0x45,
  6321. 0x51,0x30,0xbd,0x0a,0xf5,0xae,0x46,0xa9
  6322. };
  6323. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  6324. {
  6325. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6326. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6327. };
  6328. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  6329. {
  6330. 0xab,0xc7,0x86,0xfb,0x1e,0xdb,0x50,0x45,
  6331. 0x80,0xc4,0xd8,0x82,0xef,0x29,0xa0,0xc7
  6332. };
  6333. WOLFSSL_SMALL_STACK_STATIC const byte plain3[] =
  6334. {
  6335. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6336. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6337. };
  6338. #endif /* WOLFSSL_AES_192 */
  6339. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6340. Aes *enc = NULL;
  6341. #else
  6342. Aes enc[1];
  6343. #endif
  6344. byte cipher[AES_BLOCK_SIZE * 4];
  6345. #ifdef HAVE_AES_DECRYPT
  6346. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6347. Aes *dec = NULL;
  6348. #else
  6349. Aes dec[1];
  6350. #endif
  6351. byte plain [AES_BLOCK_SIZE * 4];
  6352. #endif
  6353. int ret = 0;
  6354. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6355. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6356. ERROR_OUT(-1, out);
  6357. #ifdef HAVE_AES_DECRYPT
  6358. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6359. ERROR_OUT(-1, out);
  6360. #endif
  6361. #endif
  6362. XMEMSET(enc, 0, sizeof *enc);
  6363. #ifdef HAVE_AES_DECRYPT
  6364. XMEMSET(dec, 0, sizeof *dec);
  6365. #endif
  6366. #ifdef WOLFSSL_AES_128
  6367. /* 128 key size test */
  6368. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6369. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6370. ret = EVP_test(EVP_aes_128_ofb(), key2, iv2, plain2, sizeof(plain2),
  6371. cipher2, sizeof(cipher2));
  6372. if (ret != 0) {
  6373. goto out;
  6374. }
  6375. #endif
  6376. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  6377. if (ret != 0)
  6378. ERROR_OUT(-5129, out);
  6379. #ifdef HAVE_AES_DECRYPT
  6380. /* decrypt uses AES_ENCRYPTION */
  6381. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  6382. if (ret != 0)
  6383. ERROR_OUT(-5130, out);
  6384. #endif
  6385. XMEMSET(cipher, 0, sizeof(cipher));
  6386. ret = wc_AesOfbEncrypt(enc, cipher, plain2, AES_BLOCK_SIZE);
  6387. if (ret != 0)
  6388. ERROR_OUT(-5131, out);
  6389. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE))
  6390. ERROR_OUT(-5132, out);
  6391. #ifdef HAVE_AES_DECRYPT
  6392. ret = wc_AesOfbDecrypt(dec, plain, cipher2, AES_BLOCK_SIZE);
  6393. if (ret != 0)
  6394. ERROR_OUT(-5133, out);
  6395. if (XMEMCMP(plain, plain2, AES_BLOCK_SIZE))
  6396. ERROR_OUT(-5134, out);
  6397. #endif /* HAVE_AES_DECRYPT */
  6398. #endif /* WOLFSSL_AES_128 */
  6399. #ifdef WOLFSSL_AES_192
  6400. /* 192 key size test */
  6401. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6402. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6403. ret = EVP_test(EVP_aes_192_ofb(), key3, iv3, plain3, sizeof(plain3),
  6404. cipher3, sizeof(cipher3));
  6405. if (ret != 0) {
  6406. goto out;
  6407. }
  6408. #endif
  6409. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  6410. if (ret != 0)
  6411. ERROR_OUT(-5135, out);
  6412. #ifdef HAVE_AES_DECRYPT
  6413. /* decrypt uses AES_ENCRYPTION */
  6414. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  6415. if (ret != 0)
  6416. ERROR_OUT(-5136, out);
  6417. #endif
  6418. XMEMSET(cipher, 0, sizeof(cipher));
  6419. ret = wc_AesOfbEncrypt(enc, cipher, plain3, AES_BLOCK_SIZE);
  6420. if (ret != 0)
  6421. ERROR_OUT(-5137, out);
  6422. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE))
  6423. ERROR_OUT(-5138, out);
  6424. #ifdef HAVE_AES_DECRYPT
  6425. ret = wc_AesOfbDecrypt(dec, plain, cipher3, AES_BLOCK_SIZE);
  6426. if (ret != 0)
  6427. ERROR_OUT(-5139, out);
  6428. if (XMEMCMP(plain, plain3, AES_BLOCK_SIZE))
  6429. ERROR_OUT(-5140, out);
  6430. #endif /* HAVE_AES_DECRYPT */
  6431. #endif /* WOLFSSL_AES_192 */
  6432. #ifdef WOLFSSL_AES_256
  6433. /* 256 key size test */
  6434. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6435. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6436. ret = EVP_test(EVP_aes_256_ofb(), key1, iv1, plain1, sizeof(plain1),
  6437. cipher1, sizeof(cipher1));
  6438. if (ret != 0) {
  6439. goto out;
  6440. }
  6441. #endif
  6442. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6443. if (ret != 0)
  6444. ERROR_OUT(-5141, out);
  6445. #ifdef HAVE_AES_DECRYPT
  6446. /* decrypt uses AES_ENCRYPTION */
  6447. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6448. if (ret != 0)
  6449. ERROR_OUT(-5142, out);
  6450. #endif
  6451. XMEMSET(cipher, 0, sizeof(cipher));
  6452. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE);
  6453. if (ret != 0)
  6454. ERROR_OUT(-5143, out);
  6455. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE))
  6456. ERROR_OUT(-5144, out);
  6457. ret = wc_AesOfbEncrypt(enc, cipher + AES_BLOCK_SIZE,
  6458. plain1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  6459. if (ret != 0)
  6460. ERROR_OUT(-5145, out);
  6461. if (XMEMCMP(cipher + AES_BLOCK_SIZE, cipher1 + AES_BLOCK_SIZE,
  6462. AES_BLOCK_SIZE))
  6463. ERROR_OUT(-5146, out);
  6464. #ifdef HAVE_AES_DECRYPT
  6465. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE);
  6466. if (ret != 0)
  6467. ERROR_OUT(-5147, out);
  6468. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE))
  6469. ERROR_OUT(-5148, out);
  6470. ret = wc_AesOfbDecrypt(dec, plain + AES_BLOCK_SIZE,
  6471. cipher1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  6472. if (ret != 0)
  6473. ERROR_OUT(-5149, out);
  6474. if (XMEMCMP(plain + AES_BLOCK_SIZE, plain1 + AES_BLOCK_SIZE,
  6475. AES_BLOCK_SIZE))
  6476. ERROR_OUT(-5150, out);
  6477. #endif /* HAVE_AES_DECRYPT */
  6478. /* multiple blocks at once */
  6479. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6480. if (ret != 0)
  6481. ERROR_OUT(-5151, out);
  6482. #ifdef HAVE_AES_DECRYPT
  6483. /* decrypt uses AES_ENCRYPTION */
  6484. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6485. if (ret != 0)
  6486. ERROR_OUT(-5152, out);
  6487. #endif
  6488. XMEMSET(cipher, 0, sizeof(cipher));
  6489. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE * 3);
  6490. if (ret != 0)
  6491. ERROR_OUT(-5153, out);
  6492. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 3))
  6493. ERROR_OUT(-5154, out);
  6494. #ifdef HAVE_AES_DECRYPT
  6495. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE * 3);
  6496. if (ret != 0)
  6497. ERROR_OUT(-5155, out);
  6498. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE * 3))
  6499. ERROR_OUT(-5156, out);
  6500. #endif /* HAVE_AES_DECRYPT */
  6501. /* inline decrypt/encrypt*/
  6502. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6503. if (ret != 0)
  6504. ERROR_OUT(-5157, out);
  6505. #ifdef HAVE_AES_DECRYPT
  6506. /* decrypt uses AES_ENCRYPTION */
  6507. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6508. if (ret != 0)
  6509. ERROR_OUT(-5158, out);
  6510. #endif
  6511. XMEMCPY(cipher, plain1, AES_BLOCK_SIZE * 2);
  6512. ret = wc_AesOfbEncrypt(enc, cipher, cipher, AES_BLOCK_SIZE * 2);
  6513. if (ret != 0)
  6514. ERROR_OUT(-5159, out);
  6515. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  6516. ERROR_OUT(-5160, out);
  6517. #ifdef HAVE_AES_DECRYPT
  6518. ret = wc_AesOfbDecrypt(dec, cipher, cipher, AES_BLOCK_SIZE * 2);
  6519. if (ret != 0)
  6520. ERROR_OUT(-5161, out);
  6521. if (XMEMCMP(cipher, plain1, AES_BLOCK_SIZE * 2))
  6522. ERROR_OUT(-5162, out);
  6523. #endif /* HAVE_AES_DECRYPT */
  6524. /* 256 key size test leftover support */
  6525. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6526. if (ret != 0)
  6527. ERROR_OUT(-5163, out);
  6528. #ifdef HAVE_AES_DECRYPT
  6529. /* decrypt uses AES_ENCRYPTION */
  6530. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6531. if (ret != 0)
  6532. ERROR_OUT(-5164, out);
  6533. #endif
  6534. XMEMSET(cipher, 0, sizeof(cipher));
  6535. ret = wc_AesOfbEncrypt(enc, cipher, plain1, 3);
  6536. if (ret != 0)
  6537. ERROR_OUT(-5165, out);
  6538. if (XMEMCMP(cipher, cipher1, 3))
  6539. ERROR_OUT(-5166, out);
  6540. ret = wc_AesOfbEncrypt(enc, cipher + 3, plain1 + 3, AES_BLOCK_SIZE);
  6541. if (ret != 0)
  6542. ERROR_OUT(-5167, out);
  6543. if (XMEMCMP(cipher + 3, cipher1 + 3, AES_BLOCK_SIZE))
  6544. ERROR_OUT(-5168, out);
  6545. #ifdef HAVE_AES_DECRYPT
  6546. ret = wc_AesOfbDecrypt(dec, plain, cipher1, 6);
  6547. if (ret != 0)
  6548. ERROR_OUT(-5169, out);
  6549. if (XMEMCMP(plain, plain1, 6))
  6550. ERROR_OUT(-5170, out);
  6551. ret = wc_AesOfbDecrypt(dec, plain + 6, cipher1 + 6, AES_BLOCK_SIZE);
  6552. if (ret != 0)
  6553. ERROR_OUT(-5171, out);
  6554. if (XMEMCMP(plain + 6, plain1 + 6, AES_BLOCK_SIZE))
  6555. ERROR_OUT(-5172, out);
  6556. #endif /* HAVE_AES_DECRYPT */
  6557. out:
  6558. wc_AesFree(enc);
  6559. wc_AesFree(dec);
  6560. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6561. if (enc)
  6562. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  6563. #ifdef HAVE_AES_DECRYPT
  6564. if (dec)
  6565. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  6566. #endif
  6567. #endif
  6568. #endif /* WOLFSSL_AES_256 */
  6569. return ret;
  6570. }
  6571. #endif /* WOLFSSL_AES_OFB */
  6572. #if defined(WOLFSSL_AES_CFB)
  6573. /* Test cases from NIST SP 800-38A, Recommendation for Block Cipher Modes of Operation Methods an*/
  6574. static int aescfb_test(void)
  6575. {
  6576. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6577. Aes *enc = NULL;
  6578. #else
  6579. Aes enc[1];
  6580. #endif
  6581. int enc_inited = 0;
  6582. byte cipher[AES_BLOCK_SIZE * 4];
  6583. #ifdef HAVE_AES_DECRYPT
  6584. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6585. Aes *dec = NULL;
  6586. #else
  6587. Aes dec[1];
  6588. #endif
  6589. int dec_inited = 0;
  6590. byte plain [AES_BLOCK_SIZE * 4];
  6591. #endif
  6592. int ret = 0;
  6593. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  6594. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  6595. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  6596. };
  6597. #ifdef WOLFSSL_AES_128
  6598. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6599. {
  6600. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  6601. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  6602. };
  6603. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  6604. {
  6605. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  6606. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  6607. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  6608. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b,
  6609. 0x26,0x75,0x1f,0x67,0xa3,0xcb,0xb1,0x40,
  6610. 0xb1,0x80,0x8c,0xf1,0x87,0xa4,0xf4,0xdf
  6611. };
  6612. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  6613. {
  6614. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  6615. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  6616. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  6617. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  6618. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  6619. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef
  6620. };
  6621. #endif /* WOLFSSL_AES_128 */
  6622. #ifdef WOLFSSL_AES_192
  6623. /* 192 size key test */
  6624. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6625. {
  6626. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  6627. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  6628. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  6629. };
  6630. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  6631. {
  6632. 0xcd,0xc8,0x0d,0x6f,0xdd,0xf1,0x8c,0xab,
  6633. 0x34,0xc2,0x59,0x09,0xc9,0x9a,0x41,0x74,
  6634. 0x67,0xce,0x7f,0x7f,0x81,0x17,0x36,0x21,
  6635. 0x96,0x1a,0x2b,0x70,0x17,0x1d,0x3d,0x7a,
  6636. 0x2e,0x1e,0x8a,0x1d,0xd5,0x9b,0x88,0xb1,
  6637. 0xc8,0xe6,0x0f,0xed,0x1e,0xfa,0xc4,0xc9,
  6638. 0xc0,0x5f,0x9f,0x9c,0xa9,0x83,0x4f,0xa0,
  6639. 0x42,0xae,0x8f,0xba,0x58,0x4b,0x09,0xff
  6640. };
  6641. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  6642. {
  6643. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  6644. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  6645. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  6646. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  6647. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  6648. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  6649. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  6650. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  6651. };
  6652. #endif /* WOLFSSL_AES_192 */
  6653. #ifdef WOLFSSL_AES_256
  6654. /* 256 size key simple test */
  6655. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6656. {
  6657. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  6658. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  6659. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  6660. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  6661. };
  6662. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  6663. {
  6664. 0xdc,0x7e,0x84,0xbf,0xda,0x79,0x16,0x4b,
  6665. 0x7e,0xcd,0x84,0x86,0x98,0x5d,0x38,0x60,
  6666. 0x39,0xff,0xed,0x14,0x3b,0x28,0xb1,0xc8,
  6667. 0x32,0x11,0x3c,0x63,0x31,0xe5,0x40,0x7b,
  6668. 0xdf,0x10,0x13,0x24,0x15,0xe5,0x4b,0x92,
  6669. 0xa1,0x3e,0xd0,0xa8,0x26,0x7a,0xe2,0xf9,
  6670. 0x75,0xa3,0x85,0x74,0x1a,0xb9,0xce,0xf8,
  6671. 0x20,0x31,0x62,0x3d,0x55,0xb1,0xe4,0x71
  6672. };
  6673. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  6674. {
  6675. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  6676. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  6677. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  6678. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  6679. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  6680. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  6681. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  6682. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  6683. };
  6684. #endif /* WOLFSSL_AES_256 */
  6685. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6686. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6687. ERROR_OUT(-1, out);
  6688. #ifdef HAVE_AES_DECRYPT
  6689. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6690. ERROR_OUT(-1, out);
  6691. #endif
  6692. #endif
  6693. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  6694. ERROR_OUT(-5173, out);
  6695. else
  6696. enc_inited = 1;
  6697. #ifdef HAVE_AES_DECRYPT
  6698. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  6699. ERROR_OUT(-5174, out);
  6700. else
  6701. dec_inited = 1;
  6702. #endif
  6703. #ifdef WOLFSSL_AES_128
  6704. /* 128 key tests */
  6705. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6706. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6707. ret = EVP_test(EVP_aes_128_cfb128(), key1, iv, msg1, sizeof(msg1),
  6708. cipher1, sizeof(cipher1));
  6709. if (ret != 0) {
  6710. return ret;
  6711. }
  6712. #endif
  6713. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6714. if (ret != 0)
  6715. ERROR_OUT(-5175, out);
  6716. #ifdef HAVE_AES_DECRYPT
  6717. /* decrypt uses AES_ENCRYPTION */
  6718. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6719. if (ret != 0)
  6720. ERROR_OUT(-5176, out);
  6721. #endif
  6722. XMEMSET(cipher, 0, sizeof(cipher));
  6723. ret = wc_AesCfbEncrypt(enc, cipher, msg1, AES_BLOCK_SIZE * 2);
  6724. if (ret != 0)
  6725. ERROR_OUT(-5177, out);
  6726. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  6727. ERROR_OUT(-5178, out);
  6728. /* test restarting encryption process */
  6729. ret = wc_AesCfbEncrypt(enc, cipher + (AES_BLOCK_SIZE * 2),
  6730. msg1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE);
  6731. if (ret != 0)
  6732. ERROR_OUT(-5179, out);
  6733. if (XMEMCMP(cipher + (AES_BLOCK_SIZE * 2),
  6734. cipher1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE))
  6735. ERROR_OUT(-5180, out);
  6736. #ifdef HAVE_AES_DECRYPT
  6737. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 3);
  6738. if (ret != 0)
  6739. ERROR_OUT(-5181, out);
  6740. if (XMEMCMP(plain, msg1, AES_BLOCK_SIZE * 3))
  6741. ERROR_OUT(-5182, out);
  6742. #endif /* HAVE_AES_DECRYPT */
  6743. #endif /* WOLFSSL_AES_128 */
  6744. #ifdef WOLFSSL_AES_192
  6745. /* 192 key size test */
  6746. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6747. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6748. ret = EVP_test(EVP_aes_192_cfb128(), key2, iv, msg2, sizeof(msg2),
  6749. cipher2, sizeof(cipher2));
  6750. if (ret != 0) {
  6751. return ret;
  6752. }
  6753. #endif
  6754. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv, AES_ENCRYPTION);
  6755. if (ret != 0)
  6756. ERROR_OUT(-5183, out);
  6757. #ifdef HAVE_AES_DECRYPT
  6758. /* decrypt uses AES_ENCRYPTION */
  6759. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv, AES_ENCRYPTION);
  6760. if (ret != 0)
  6761. ERROR_OUT(-5184, out);
  6762. #endif
  6763. XMEMSET(cipher, 0, sizeof(cipher));
  6764. ret = wc_AesCfbEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE * 4);
  6765. if (ret != 0)
  6766. ERROR_OUT(-5185, out);
  6767. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE * 4))
  6768. ERROR_OUT(-5186, out);
  6769. #ifdef HAVE_AES_DECRYPT
  6770. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 4);
  6771. if (ret != 0)
  6772. ERROR_OUT(-5187, out);
  6773. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE * 4))
  6774. ERROR_OUT(-5188, out);
  6775. #endif /* HAVE_AES_DECRYPT */
  6776. #endif /* WOLFSSL_AES_192 */
  6777. #ifdef WOLFSSL_AES_256
  6778. /* 256 key size test */
  6779. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6780. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6781. ret = EVP_test(EVP_aes_256_cfb128(), key3, iv, msg3, sizeof(msg3),
  6782. cipher3, sizeof(cipher3));
  6783. if (ret != 0) {
  6784. return ret;
  6785. }
  6786. #endif
  6787. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv, AES_ENCRYPTION);
  6788. if (ret != 0)
  6789. ERROR_OUT(-5189, out);
  6790. #ifdef HAVE_AES_DECRYPT
  6791. /* decrypt uses AES_ENCRYPTION */
  6792. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv, AES_ENCRYPTION);
  6793. if (ret != 0)
  6794. ERROR_OUT(-5190, out);
  6795. #endif
  6796. /* test with data left overs, magic lengths are checking near edges */
  6797. XMEMSET(cipher, 0, sizeof(cipher));
  6798. ret = wc_AesCfbEncrypt(enc, cipher, msg3, 4);
  6799. if (ret != 0)
  6800. ERROR_OUT(-5191, out);
  6801. if (XMEMCMP(cipher, cipher3, 4))
  6802. ERROR_OUT(-5192, out);
  6803. ret = wc_AesCfbEncrypt(enc, cipher + 4, msg3 + 4, 27);
  6804. if (ret != 0)
  6805. ERROR_OUT(-5193, out);
  6806. if (XMEMCMP(cipher + 4, cipher3 + 4, 27))
  6807. ERROR_OUT(-5194, out);
  6808. ret = wc_AesCfbEncrypt(enc, cipher + 31, msg3 + 31,
  6809. (AES_BLOCK_SIZE * 4) - 31);
  6810. if (ret != 0)
  6811. ERROR_OUT(-5195, out);
  6812. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE * 4))
  6813. ERROR_OUT(-5196, out);
  6814. #ifdef HAVE_AES_DECRYPT
  6815. ret = wc_AesCfbDecrypt(dec, plain, cipher, 4);
  6816. if (ret != 0)
  6817. ERROR_OUT(-5197, out);
  6818. if (XMEMCMP(plain, msg3, 4))
  6819. ERROR_OUT(-5198, out);
  6820. ret = wc_AesCfbDecrypt(dec, plain + 4, cipher + 4, 4);
  6821. if (ret != 0)
  6822. ERROR_OUT(-5199, out);
  6823. ret = wc_AesCfbDecrypt(dec, plain + 8, cipher + 8, 23);
  6824. if (ret != 0)
  6825. ERROR_OUT(-5200, out);
  6826. if (XMEMCMP(plain + 4, msg3 + 4, 27))
  6827. ERROR_OUT(-5201, out);
  6828. ret = wc_AesCfbDecrypt(dec, plain + 31, cipher + 31,
  6829. (AES_BLOCK_SIZE * 4) - 31);
  6830. if (ret != 0)
  6831. ERROR_OUT(-5202, out);
  6832. if (XMEMCMP(plain, msg3, AES_BLOCK_SIZE * 4))
  6833. ERROR_OUT(-5203, out);
  6834. #endif /* HAVE_AES_DECRYPT */
  6835. #endif /* WOLFSSL_AES_256 */
  6836. out:
  6837. if (enc_inited)
  6838. wc_AesFree(enc);
  6839. if (dec_inited)
  6840. wc_AesFree(dec);
  6841. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6842. if (enc)
  6843. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  6844. #ifdef HAVE_AES_DECRYPT
  6845. if (dec)
  6846. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  6847. #endif
  6848. #endif
  6849. return ret;
  6850. }
  6851. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6852. static int aescfb1_test(void)
  6853. {
  6854. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6855. Aes *enc = NULL;
  6856. #else
  6857. Aes enc[1];
  6858. #endif
  6859. int enc_inited = 0;
  6860. byte cipher[AES_BLOCK_SIZE];
  6861. #ifdef HAVE_AES_DECRYPT
  6862. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6863. Aes *dec = NULL;
  6864. #else
  6865. Aes dec[1];
  6866. #endif
  6867. int dec_inited = 0;
  6868. byte plain [AES_BLOCK_SIZE];
  6869. #endif
  6870. int ret = 0;
  6871. #ifdef WOLFSSL_AES_128
  6872. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  6873. 0x4d,0xbb,0xdc,0xaa,0x59,0xf3,0x63,0xc9,
  6874. 0x2a,0x3b,0x98,0x43,0xad,0x20,0xe2,0xb7
  6875. };
  6876. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6877. {
  6878. 0xcd,0xef,0x9d,0x06,0x61,0xba,0xe4,0x73,
  6879. 0x8d,0x1a,0x58,0xa2,0xa6,0x22,0x8b,0x66
  6880. };
  6881. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  6882. {
  6883. 0x00
  6884. };
  6885. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  6886. {
  6887. 0xC0
  6888. };
  6889. #endif /* WOLFSSL_AES_128 */
  6890. #ifdef WOLFSSL_AES_192
  6891. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  6892. 0x57,0xc6,0x89,0x7c,0x99,0x52,0x28,0x13,
  6893. 0xbf,0x67,0x9c,0xe1,0x13,0x70,0xaf,0x5e
  6894. };
  6895. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6896. {
  6897. 0xba,0xa1,0x58,0xa1,0x6b,0x50,0x4a,0x10,
  6898. 0x8e,0xd4,0x33,0x2e,0xe7,0xf2,0x9b,0xf6,
  6899. 0xd1,0xac,0x46,0xa8,0xde,0x5a,0xfe,0x7a
  6900. };
  6901. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  6902. {
  6903. 0x30
  6904. };
  6905. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  6906. {
  6907. 0x80
  6908. };
  6909. #endif /* WOLFSSL_AES_192 */
  6910. #ifdef WOLFSSL_AES_256
  6911. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  6912. 0x63,0x2e,0x9f,0x83,0x1f,0xa3,0x80,0x5e,
  6913. 0x52,0x02,0xbc,0xe0,0x6d,0x04,0xf9,0xa0
  6914. };
  6915. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6916. {
  6917. 0xf6,0xfa,0xe4,0xf1,0x5d,0x91,0xfc,0x50,
  6918. 0x88,0x78,0x4f,0x84,0xa5,0x37,0x12,0x7e,
  6919. 0x32,0x63,0x55,0x9c,0x62,0x73,0x88,0x20,
  6920. 0xc2,0xcf,0x3d,0xe1,0x1c,0x2a,0x30,0x40
  6921. };
  6922. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  6923. {
  6924. 0xF7, 0x00
  6925. };
  6926. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  6927. {
  6928. 0x41, 0xC0
  6929. };
  6930. #endif /* WOLFSSL_AES_256 */
  6931. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6932. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6933. ERROR_OUT(-1, out);
  6934. #ifdef HAVE_AES_DECRYPT
  6935. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6936. ERROR_OUT(-1, out);
  6937. #endif
  6938. #endif
  6939. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  6940. ERROR_OUT(-5204, out);
  6941. else
  6942. enc_inited = 1;
  6943. #ifdef HAVE_AES_DECRYPT
  6944. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  6945. ERROR_OUT(-5205, out);
  6946. else
  6947. dec_inited = 1;
  6948. #endif
  6949. #ifdef WOLFSSL_AES_128
  6950. /* 128 key tests */
  6951. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6952. if (ret != 0)
  6953. ERROR_OUT(-5206, out);
  6954. #ifdef HAVE_AES_DECRYPT
  6955. /* decrypt uses AES_ENCRYPTION */
  6956. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6957. if (ret != 0)
  6958. ERROR_OUT(-5207, out);
  6959. #endif
  6960. XMEMSET(cipher, 0, sizeof(cipher));
  6961. ret = wc_AesCfb1Encrypt(enc, cipher, msg1, 2);
  6962. if (ret != 0)
  6963. ERROR_OUT(-5208, out);
  6964. if (cipher[0] != cipher1[0])
  6965. ERROR_OUT(-5209, out);
  6966. #ifdef HAVE_AES_DECRYPT
  6967. ret = wc_AesCfb1Decrypt(dec, plain, cipher, 2);
  6968. if (ret != 0)
  6969. ERROR_OUT(-5210, out);
  6970. if (plain[0] != msg1[0])
  6971. ERROR_OUT(-5211, out);
  6972. #endif /* HAVE_AES_DECRYPT */
  6973. #ifdef OPENSSL_EXTRA
  6974. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6975. if (ret != 0)
  6976. ERROR_OUT(-5212, out);
  6977. XMEMSET(cipher, 0, sizeof(cipher));
  6978. ret = wc_AesCfb1Encrypt(enc, cipher, msg1,
  6979. sizeof(msg1) * WOLFSSL_BIT_SIZE);
  6980. if (ret != 0)
  6981. ERROR_OUT(-5213, out);
  6982. #ifndef WOLFCRYPT_ONLY
  6983. ret = EVP_test(EVP_aes_128_cfb1(), key1, iv, msg1, sizeof(msg1),
  6984. cipher, sizeof(msg1));
  6985. if (ret != 0) {
  6986. goto out;
  6987. }
  6988. #endif
  6989. #endif
  6990. #endif /* WOLFSSL_AES_128 */
  6991. #ifdef WOLFSSL_AES_192
  6992. /* 192 key tests */
  6993. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  6994. if (ret != 0)
  6995. ERROR_OUT(-5214, out);
  6996. XMEMSET(cipher, 0, sizeof(cipher));
  6997. ret = wc_AesCfb1Encrypt(enc, cipher, msg2, 4);
  6998. if (ret != 0)
  6999. ERROR_OUT(-5215, out);
  7000. if (XMEMCMP(cipher, cipher2, sizeof(cipher2)) != 0)
  7001. ERROR_OUT(-5216, out);
  7002. #ifdef OPENSSL_EXTRA
  7003. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  7004. if (ret != 0)
  7005. ERROR_OUT(-5217, out);
  7006. XMEMSET(cipher, 0, sizeof(cipher));
  7007. ret = wc_AesCfb1Encrypt(enc, cipher, msg2,
  7008. sizeof(msg2) * WOLFSSL_BIT_SIZE);
  7009. if (ret != 0)
  7010. ERROR_OUT(-5218, out);
  7011. #ifndef WOLFCRYPT_ONLY
  7012. ret = EVP_test(EVP_aes_192_cfb1(), key2, iv2, msg2, sizeof(msg2),
  7013. cipher, sizeof(msg2));
  7014. if (ret != 0) {
  7015. goto out;
  7016. }
  7017. #endif
  7018. #endif
  7019. #endif /* WOLFSSL_AES_192 */
  7020. #ifdef WOLFSSL_AES_256
  7021. /* 256 key tests */
  7022. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  7023. if (ret != 0)
  7024. ERROR_OUT(-5219, out);
  7025. XMEMSET(cipher, 0, sizeof(cipher));
  7026. ret = wc_AesCfb1Encrypt(enc, cipher, msg3, 10);
  7027. if (ret != 0)
  7028. ERROR_OUT(-5220, out);
  7029. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  7030. ERROR_OUT(-5221, out);
  7031. #ifdef OPENSSL_EXTRA
  7032. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  7033. if (ret != 0)
  7034. ERROR_OUT(-5222, out);
  7035. XMEMSET(cipher, 0, sizeof(cipher));
  7036. ret = wc_AesCfb1Encrypt(enc, cipher, msg3,
  7037. sizeof(msg3) * WOLFSSL_BIT_SIZE);
  7038. if (ret != 0)
  7039. ERROR_OUT(-5223, out);
  7040. #ifndef WOLFCRYPT_ONLY
  7041. ret = EVP_test(EVP_aes_256_cfb1(), key3, iv3, msg3, sizeof(msg3),
  7042. cipher, sizeof(msg3));
  7043. if (ret != 0) {
  7044. goto out;
  7045. }
  7046. #endif
  7047. #endif
  7048. out:
  7049. if (enc_inited)
  7050. wc_AesFree(enc);
  7051. #ifdef HAVE_AES_DECRYPT
  7052. if (dec_inited)
  7053. wc_AesFree(dec);
  7054. #endif
  7055. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7056. if (enc)
  7057. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7058. #ifdef HAVE_AES_DECRYPT
  7059. if (dec)
  7060. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7061. #endif
  7062. #endif
  7063. #endif /* WOLFSSL_AES_256 */
  7064. return ret;
  7065. }
  7066. static int aescfb8_test(void)
  7067. {
  7068. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7069. Aes *enc = NULL;
  7070. #else
  7071. Aes enc[1];
  7072. #endif
  7073. int enc_inited = 0;
  7074. byte cipher[AES_BLOCK_SIZE];
  7075. #ifdef HAVE_AES_DECRYPT
  7076. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7077. Aes *dec = NULL;
  7078. #else
  7079. Aes dec[1];
  7080. #endif
  7081. int dec_inited = 0;
  7082. byte plain [AES_BLOCK_SIZE];
  7083. #endif
  7084. int ret = 0;
  7085. #ifdef WOLFSSL_AES_128
  7086. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  7087. 0xf4,0x75,0xc6,0x49,0x91,0xb2,0x0e,0xae,
  7088. 0xe1,0x83,0xa2,0x26,0x29,0xe2,0x1e,0x22
  7089. };
  7090. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  7091. {
  7092. 0xc8,0xfe,0x9b,0xf7,0x7b,0x93,0x0f,0x46,
  7093. 0xd2,0x07,0x8b,0x8c,0x0e,0x65,0x7c,0xd4
  7094. };
  7095. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  7096. {
  7097. 0xd2,0x76,0x91
  7098. };
  7099. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  7100. {
  7101. 0xc9,0x06,0x35
  7102. };
  7103. #endif /* WOLFSSL_AES_128 */
  7104. #ifdef WOLFSSL_AES_192
  7105. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  7106. 0x0a,0x02,0x84,0x6b,0x62,0xab,0xb6,0x93,
  7107. 0xef,0x31,0xd7,0x54,0x84,0x2e,0xed,0x29
  7108. };
  7109. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  7110. {
  7111. 0xba,0xf0,0x8b,0x76,0x31,0x7a,0x65,0xc5,
  7112. 0xf0,0x7a,0xe6,0xf5,0x7e,0xb0,0xe6,0x54,
  7113. 0x88,0x65,0x93,0x24,0xd2,0x97,0x09,0xe3
  7114. };
  7115. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  7116. {
  7117. 0x72,0x9c,0x0b,0x6d,0xeb,0x75,0xfa,0x6e,
  7118. 0xb5,0xe8
  7119. };
  7120. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  7121. {
  7122. 0x98,0x95,0x93,0x24,0x02,0x39,0x3d,0xc3,
  7123. 0x3a,0x60
  7124. };
  7125. #endif
  7126. #ifdef WOLFSSL_AES_256
  7127. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  7128. 0x33,0x8c,0x55,0x2f,0xf1,0xec,0xa1,0x44,
  7129. 0x08,0xe0,0x5d,0x8c,0xf9,0xf3,0xb3,0x1b
  7130. };
  7131. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  7132. {
  7133. 0x06,0x48,0x74,0x09,0x2f,0x7a,0x13,0xcc,
  7134. 0x44,0x62,0x24,0x7a,0xd4,0x23,0xd0,0xe9,
  7135. 0x6e,0xdf,0x42,0xe8,0xb6,0x7a,0x5a,0x23,
  7136. 0xb7,0xa0,0xa6,0x47,0x7b,0x09,0x8e,0x66
  7137. };
  7138. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  7139. {
  7140. 0x1c,0xff,0x95
  7141. };
  7142. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  7143. {
  7144. 0xb9,0x74,0xfa
  7145. };
  7146. #endif
  7147. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7148. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7149. ERROR_OUT(-5238, out);
  7150. #ifdef HAVE_AES_DECRYPT
  7151. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7152. ERROR_OUT(-5239, out);
  7153. #endif
  7154. #endif
  7155. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  7156. ERROR_OUT(-5224, out);
  7157. else
  7158. enc_inited = 1;
  7159. #ifdef HAVE_AES_DECRYPT
  7160. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  7161. ERROR_OUT(-5225, out);
  7162. else
  7163. dec_inited = 1;
  7164. #endif
  7165. #ifdef WOLFSSL_AES_128
  7166. /* 128 key tests */
  7167. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7168. ret = EVP_test(EVP_aes_128_cfb8(), key1, iv, msg1, sizeof(msg1),
  7169. cipher1, sizeof(cipher1));
  7170. if (ret != 0) {
  7171. return ret;
  7172. }
  7173. #endif
  7174. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7175. if (ret != 0)
  7176. ERROR_OUT(-5226, out);
  7177. #ifdef HAVE_AES_DECRYPT
  7178. /* decrypt uses AES_ENCRYPTION */
  7179. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7180. if (ret != 0)
  7181. ERROR_OUT(-5227, out);
  7182. #endif
  7183. XMEMSET(cipher, 0, sizeof(cipher));
  7184. ret = wc_AesCfb8Encrypt(enc, cipher, msg1, sizeof(msg1));
  7185. if (ret != 0)
  7186. ERROR_OUT(-5228, out);
  7187. if (XMEMCMP(cipher, cipher1, sizeof(cipher1)) != 0)
  7188. ERROR_OUT(-5229, out);
  7189. #ifdef HAVE_AES_DECRYPT
  7190. ret = wc_AesCfb8Decrypt(dec, plain, cipher, sizeof(msg1));
  7191. if (ret != 0)
  7192. ERROR_OUT(-5230, out);
  7193. if (XMEMCMP(plain, msg1, sizeof(msg1)) != 0)
  7194. ERROR_OUT(-5231, out);
  7195. #endif /* HAVE_AES_DECRYPT */
  7196. #endif /* WOLFSSL_AES_128 */
  7197. #ifdef WOLFSSL_AES_192
  7198. /* 192 key tests */
  7199. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  7200. if (ret != 0)
  7201. ERROR_OUT(-5232, out);
  7202. XMEMSET(cipher, 0, sizeof(cipher));
  7203. ret = wc_AesCfb8Encrypt(enc, cipher, msg2, sizeof(msg2));
  7204. if (ret != 0)
  7205. ERROR_OUT(-5233, out);
  7206. if (XMEMCMP(cipher, cipher2, sizeof(msg2)) != 0)
  7207. ERROR_OUT(-5234, out);
  7208. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7209. ret = EVP_test(EVP_aes_192_cfb8(), key2, iv2, msg2, sizeof(msg2),
  7210. cipher2, sizeof(msg2));
  7211. if (ret != 0) {
  7212. return ret;
  7213. }
  7214. #endif
  7215. #endif /* WOLFSSL_AES_192 */
  7216. #ifdef WOLFSSL_AES_256
  7217. /* 256 key tests */
  7218. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  7219. if (ret != 0)
  7220. ERROR_OUT(-5235, out);
  7221. XMEMSET(cipher, 0, sizeof(cipher));
  7222. ret = wc_AesCfb8Encrypt(enc, cipher, msg3, sizeof(msg3));
  7223. if (ret != 0)
  7224. ERROR_OUT(-5236, out);
  7225. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  7226. ERROR_OUT(-5237, out);
  7227. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7228. ret = EVP_test(EVP_aes_256_cfb8(), key3, iv3, msg3, sizeof(msg3),
  7229. cipher3, sizeof(msg3));
  7230. if (ret != 0) {
  7231. goto out;
  7232. }
  7233. #endif
  7234. out:
  7235. if (enc_inited)
  7236. wc_AesFree(enc);
  7237. #ifdef HAVE_AES_DECRYPT
  7238. if (dec_inited)
  7239. wc_AesFree(dec);
  7240. #endif
  7241. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7242. if (enc)
  7243. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7244. #ifdef HAVE_AES_DECRYPT
  7245. if (dec)
  7246. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7247. #endif
  7248. #endif
  7249. #endif /* WOLFSSL_AES_256 */
  7250. return ret;
  7251. }
  7252. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  7253. #endif /* WOLFSSL_AES_CFB */
  7254. static int aes_key_size_test(void)
  7255. {
  7256. int ret;
  7257. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7258. Aes *aes;
  7259. #else
  7260. Aes aes[1];
  7261. #endif
  7262. byte key16[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7263. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  7264. #ifndef WOLFSSL_CRYPTOCELL
  7265. byte key24[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7266. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  7267. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37 };
  7268. #endif
  7269. byte key32[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7270. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  7271. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7272. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  7273. byte iv[] = "1234567890abcdef";
  7274. #ifndef HAVE_FIPS
  7275. word32 keySize;
  7276. #endif
  7277. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7278. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7279. return -5315;
  7280. #endif
  7281. #if !defined(HAVE_FIPS) || \
  7282. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  7283. /* w/ FIPS v1 (cert 2425) wc_AesInit just returns 0 always as it's not
  7284. * supported with that FIPS version */
  7285. ret = wc_AesInit(NULL, HEAP_HINT, devId);
  7286. if (ret != BAD_FUNC_ARG)
  7287. ERROR_OUT(-5300, out);
  7288. #endif
  7289. ret = wc_AesInit(aes, HEAP_HINT, devId);
  7290. /* 0 check OK for FIPSv1 */
  7291. if (ret != 0)
  7292. ERROR_OUT(-5301, out);
  7293. #ifndef HAVE_FIPS
  7294. /* Parameter Validation testing. */
  7295. ret = wc_AesGetKeySize(NULL, NULL);
  7296. if (ret != BAD_FUNC_ARG)
  7297. ERROR_OUT(-5302, out);
  7298. ret = wc_AesGetKeySize(aes, NULL);
  7299. if (ret != BAD_FUNC_ARG)
  7300. ERROR_OUT(-5303, out);
  7301. ret = wc_AesGetKeySize(NULL, &keySize);
  7302. if (ret != BAD_FUNC_ARG)
  7303. ERROR_OUT(-5304, out);
  7304. /* Crashes in FIPS */
  7305. ret = wc_AesSetKey(NULL, key16, sizeof(key16), iv, AES_ENCRYPTION);
  7306. if (ret != BAD_FUNC_ARG)
  7307. ERROR_OUT(-5305, out);
  7308. #endif
  7309. /* NULL IV indicates to use all zeros IV. */
  7310. ret = wc_AesSetKey(aes, key16, sizeof(key16), NULL, AES_ENCRYPTION);
  7311. #ifdef WOLFSSL_AES_128
  7312. if (ret != 0)
  7313. #else
  7314. if (ret != BAD_FUNC_ARG)
  7315. #endif
  7316. ERROR_OUT(-5306, out);
  7317. ret = wc_AesSetKey(aes, key32, sizeof(key32) - 1, iv, AES_ENCRYPTION);
  7318. if (ret != BAD_FUNC_ARG)
  7319. ERROR_OUT(-5307, out);
  7320. /* CryptoCell handles rounds internally */
  7321. #if !defined(HAVE_FIPS) && !defined(WOLFSSL_CRYPTOCELL)
  7322. /* PSA don't use aes->rounds */
  7323. #if !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_AES)
  7324. /* Force invalid rounds */
  7325. aes->rounds = 16;
  7326. ret = wc_AesGetKeySize(aes, &keySize);
  7327. if (ret != BAD_FUNC_ARG)
  7328. ERROR_OUT(-5308, out);
  7329. #endif
  7330. #endif
  7331. ret = wc_AesSetKey(aes, key16, sizeof(key16), iv, AES_ENCRYPTION);
  7332. #ifdef WOLFSSL_AES_128
  7333. if (ret != 0)
  7334. #else
  7335. if (ret != BAD_FUNC_ARG)
  7336. #endif
  7337. ERROR_OUT(-5309, out);
  7338. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_128)
  7339. ret = wc_AesGetKeySize(aes, &keySize);
  7340. if (ret != 0 || keySize != sizeof(key16))
  7341. ERROR_OUT(-5310, out);
  7342. #endif
  7343. #ifndef WOLFSSL_CRYPTOCELL
  7344. /* Cryptocell only supports AES-128 key size */
  7345. ret = wc_AesSetKey(aes, key24, sizeof(key24), iv, AES_ENCRYPTION);
  7346. #ifdef WOLFSSL_AES_192
  7347. if (ret != 0)
  7348. #else
  7349. if (ret != BAD_FUNC_ARG)
  7350. #endif
  7351. ERROR_OUT(-5311, out);
  7352. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_192)
  7353. ret = wc_AesGetKeySize(aes, &keySize);
  7354. if (ret != 0 || keySize != sizeof(key24))
  7355. ERROR_OUT(-5312, out);
  7356. #endif
  7357. ret = wc_AesSetKey(aes, key32, sizeof(key32), iv, AES_ENCRYPTION);
  7358. #ifdef WOLFSSL_AES_256
  7359. if (ret != 0)
  7360. #else
  7361. if (ret != BAD_FUNC_ARG)
  7362. #endif
  7363. ERROR_OUT(-5313, out);
  7364. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_256)
  7365. ret = wc_AesGetKeySize(aes, &keySize);
  7366. if (ret != 0 || keySize != sizeof(key32))
  7367. ERROR_OUT(-5314, out);
  7368. #endif
  7369. #endif /* !WOLFSSL_CRYPTOCELL */
  7370. ret = 0; /* success */
  7371. out:
  7372. wc_AesFree(aes);
  7373. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7374. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7375. #endif
  7376. return ret;
  7377. }
  7378. #if defined(WOLFSSL_AES_XTS)
  7379. /* test vectors from http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html */
  7380. #ifdef WOLFSSL_AES_128
  7381. static int aes_xts_128_test(void)
  7382. {
  7383. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7384. XtsAes *aes = NULL;
  7385. #else
  7386. XtsAes aes[1];
  7387. #endif
  7388. int aes_inited = 0;
  7389. int ret = 0;
  7390. unsigned char buf[AES_BLOCK_SIZE * 2];
  7391. unsigned char cipher[AES_BLOCK_SIZE * 2];
  7392. /* 128 key tests */
  7393. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7394. 0xa1, 0xb9, 0x0c, 0xba, 0x3f, 0x06, 0xac, 0x35,
  7395. 0x3b, 0x2c, 0x34, 0x38, 0x76, 0x08, 0x17, 0x62,
  7396. 0x09, 0x09, 0x23, 0x02, 0x6e, 0x91, 0x77, 0x18,
  7397. 0x15, 0xf2, 0x9d, 0xab, 0x01, 0x93, 0x2f, 0x2f
  7398. };
  7399. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  7400. 0x4f, 0xae, 0xf7, 0x11, 0x7c, 0xda, 0x59, 0xc6,
  7401. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  7402. };
  7403. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7404. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  7405. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c
  7406. };
  7407. /* plain text test of partial block is not from NIST test vector list */
  7408. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  7409. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  7410. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  7411. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  7412. };
  7413. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7414. 0x77, 0x8a, 0xe8, 0xb4, 0x3c, 0xb9, 0x8d, 0x5a,
  7415. 0x82, 0x50, 0x81, 0xd5, 0xbe, 0x47, 0x1c, 0x63
  7416. };
  7417. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  7418. 0x39, 0x25, 0x79, 0x05, 0xdf, 0xcc, 0x77, 0x76,
  7419. 0x6c, 0x87, 0x0a, 0x80, 0x6a, 0x60, 0xe3, 0xc0,
  7420. 0x93, 0xd1, 0x2a, 0xcf, 0xcb, 0x51, 0x42, 0xfa,
  7421. 0x09, 0x69, 0x89, 0x62, 0x5b, 0x60, 0xdb, 0x16
  7422. };
  7423. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  7424. 0x5c, 0xf7, 0x9d, 0xb6, 0xc5, 0xcd, 0x99, 0x1a,
  7425. 0x1c, 0x78, 0x81, 0x42, 0x24, 0x95, 0x1e, 0x84
  7426. };
  7427. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  7428. 0xbd, 0xc5, 0x46, 0x8f, 0xbc, 0x8d, 0x50, 0xa1,
  7429. 0x0d, 0x1c, 0x85, 0x7f, 0x79, 0x1c, 0x5c, 0xba,
  7430. 0xb3, 0x81, 0x0d, 0x0d, 0x73, 0xcf, 0x8f, 0x20,
  7431. 0x46, 0xb1, 0xd1, 0x9e, 0x7d, 0x5d, 0x8a, 0x56
  7432. };
  7433. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  7434. 0xd6, 0xbe, 0x04, 0x6d, 0x41, 0xf2, 0x3b, 0x5e,
  7435. 0xd7, 0x0b, 0x6b, 0x3d, 0x5c, 0x8e, 0x66, 0x23,
  7436. 0x2b, 0xe6, 0xb8, 0x07, 0xd4, 0xdc, 0xc6, 0x0e,
  7437. 0xff, 0x8d, 0xbc, 0x1d, 0x9f, 0x7f, 0xc8, 0x22
  7438. };
  7439. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7440. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7441. ERROR_OUT(-5417, out);
  7442. #endif
  7443. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7444. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7445. ret = EVP_test(EVP_aes_128_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  7446. if (ret != 0) {
  7447. printf("EVP_aes_128_xts failed!\n");
  7448. goto out;
  7449. }
  7450. #endif
  7451. XMEMSET(buf, 0, sizeof(buf));
  7452. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  7453. HEAP_HINT, devId) != 0)
  7454. ERROR_OUT(-5400, out);
  7455. else
  7456. aes_inited = 1;
  7457. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  7458. #if defined(WOLFSSL_ASYNC_CRYPT)
  7459. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7460. #endif
  7461. if (ret != 0)
  7462. ERROR_OUT(-5401, out);
  7463. if (XMEMCMP(c2, buf, sizeof(c2)))
  7464. ERROR_OUT(-5402, out);
  7465. XMEMSET(buf, 0, sizeof(buf));
  7466. wc_AesXtsFree(aes);
  7467. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7468. HEAP_HINT, devId) != 0)
  7469. ERROR_OUT(-5403, out);
  7470. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  7471. #if defined(WOLFSSL_ASYNC_CRYPT)
  7472. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7473. #endif
  7474. if (ret != 0)
  7475. ERROR_OUT(-5404, out);
  7476. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  7477. ERROR_OUT(-5405, out);
  7478. /* partial block encryption test */
  7479. XMEMSET(cipher, 0, sizeof(cipher));
  7480. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  7481. #if defined(WOLFSSL_ASYNC_CRYPT)
  7482. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7483. #endif
  7484. if (ret != 0)
  7485. ERROR_OUT(-5406, out);
  7486. wc_AesXtsFree(aes);
  7487. /* partial block decrypt test */
  7488. XMEMSET(buf, 0, sizeof(buf));
  7489. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7490. HEAP_HINT, devId) != 0)
  7491. ERROR_OUT(-5407, out);
  7492. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  7493. #if defined(WOLFSSL_ASYNC_CRYPT)
  7494. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7495. #endif
  7496. if (ret != 0)
  7497. ERROR_OUT(-5408, out);
  7498. if (XMEMCMP(pp, buf, sizeof(pp)))
  7499. ERROR_OUT(-5409, out);
  7500. /* NIST decrypt test vector */
  7501. XMEMSET(buf, 0, sizeof(buf));
  7502. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  7503. #if defined(WOLFSSL_ASYNC_CRYPT)
  7504. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7505. #endif
  7506. if (ret != 0)
  7507. ERROR_OUT(-5410, out);
  7508. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  7509. ERROR_OUT(-5411, out);
  7510. /* fail case with decrypting using wrong key */
  7511. XMEMSET(buf, 0, sizeof(buf));
  7512. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  7513. #if defined(WOLFSSL_ASYNC_CRYPT)
  7514. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7515. #endif
  7516. if (ret != 0)
  7517. ERROR_OUT(-5412, out);
  7518. if (XMEMCMP(p2, buf, sizeof(p2)) == 0) /* fail case with wrong key */
  7519. ERROR_OUT(-5413, out);
  7520. wc_AesXtsFree(aes);
  7521. /* set correct key and retest */
  7522. XMEMSET(buf, 0, sizeof(buf));
  7523. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  7524. HEAP_HINT, devId) != 0)
  7525. ERROR_OUT(-5414, out);
  7526. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  7527. #if defined(WOLFSSL_ASYNC_CRYPT)
  7528. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7529. #endif
  7530. if (ret != 0)
  7531. ERROR_OUT(-5415, out);
  7532. if (XMEMCMP(p2, buf, sizeof(p2)))
  7533. ERROR_OUT(-5416, out);
  7534. out:
  7535. if (aes_inited)
  7536. wc_AesXtsFree(aes);
  7537. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7538. if (aes)
  7539. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7540. #endif
  7541. return ret;
  7542. }
  7543. #endif /* WOLFSSL_AES_128 */
  7544. #ifdef WOLFSSL_AES_256
  7545. static int aes_xts_256_test(void)
  7546. {
  7547. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7548. XtsAes *aes = NULL;
  7549. #else
  7550. XtsAes aes[1];
  7551. #endif
  7552. int aes_inited = 0;
  7553. int ret = 0;
  7554. unsigned char buf[AES_BLOCK_SIZE * 3];
  7555. unsigned char cipher[AES_BLOCK_SIZE * 3];
  7556. /* 256 key tests */
  7557. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7558. 0x1e, 0xa6, 0x61, 0xc5, 0x8d, 0x94, 0x3a, 0x0e,
  7559. 0x48, 0x01, 0xe4, 0x2f, 0x4b, 0x09, 0x47, 0x14,
  7560. 0x9e, 0x7f, 0x9f, 0x8e, 0x3e, 0x68, 0xd0, 0xc7,
  7561. 0x50, 0x52, 0x10, 0xbd, 0x31, 0x1a, 0x0e, 0x7c,
  7562. 0xd6, 0xe1, 0x3f, 0xfd, 0xf2, 0x41, 0x8d, 0x8d,
  7563. 0x19, 0x11, 0xc0, 0x04, 0xcd, 0xa5, 0x8d, 0xa3,
  7564. 0xd6, 0x19, 0xb7, 0xe2, 0xb9, 0x14, 0x1e, 0x58,
  7565. 0x31, 0x8e, 0xea, 0x39, 0x2c, 0xf4, 0x1b, 0x08
  7566. };
  7567. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  7568. 0xad, 0xf8, 0xd9, 0x26, 0x27, 0x46, 0x4a, 0xd2,
  7569. 0xf0, 0x42, 0x8e, 0x84, 0xa9, 0xf8, 0x75, 0x64
  7570. };
  7571. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7572. 0x2e, 0xed, 0xea, 0x52, 0xcd, 0x82, 0x15, 0xe1,
  7573. 0xac, 0xc6, 0x47, 0xe8, 0x10, 0xbb, 0xc3, 0x64,
  7574. 0x2e, 0x87, 0x28, 0x7f, 0x8d, 0x2e, 0x57, 0xe3,
  7575. 0x6c, 0x0a, 0x24, 0xfb, 0xc1, 0x2a, 0x20, 0x2e
  7576. };
  7577. /* plain text test of partial block is not from NIST test vector list */
  7578. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  7579. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  7580. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  7581. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  7582. };
  7583. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7584. 0xcb, 0xaa, 0xd0, 0xe2, 0xf6, 0xce, 0xa3, 0xf5,
  7585. 0x0b, 0x37, 0xf9, 0x34, 0xd4, 0x6a, 0x9b, 0x13,
  7586. 0x0b, 0x9d, 0x54, 0xf0, 0x7e, 0x34, 0xf3, 0x6a,
  7587. 0xf7, 0x93, 0xe8, 0x6f, 0x73, 0xc6, 0xd7, 0xdb
  7588. };
  7589. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  7590. 0xad, 0x50, 0x4b, 0x85, 0xd7, 0x51, 0xbf, 0xba,
  7591. 0x69, 0x13, 0xb4, 0xcc, 0x79, 0xb6, 0x5a, 0x62,
  7592. 0xf7, 0xf3, 0x9d, 0x36, 0x0f, 0x35, 0xb5, 0xec,
  7593. 0x4a, 0x7e, 0x95, 0xbd, 0x9b, 0xa5, 0xf2, 0xec,
  7594. 0xc1, 0xd7, 0x7e, 0xa3, 0xc3, 0x74, 0xbd, 0x4b,
  7595. 0x13, 0x1b, 0x07, 0x83, 0x87, 0xdd, 0x55, 0x5a,
  7596. 0xb5, 0xb0, 0xc7, 0xe5, 0x2d, 0xb5, 0x06, 0x12,
  7597. 0xd2, 0xb5, 0x3a, 0xcb, 0x47, 0x8a, 0x53, 0xb4
  7598. };
  7599. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  7600. 0xe6, 0x42, 0x19, 0xed, 0xe0, 0xe1, 0xc2, 0xa0,
  7601. 0x0e, 0xf5, 0x58, 0x6a, 0xc4, 0x9b, 0xeb, 0x6f
  7602. };
  7603. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  7604. 0x24, 0xcb, 0x76, 0x22, 0x55, 0xb5, 0xa8, 0x00,
  7605. 0xf4, 0x6e, 0x80, 0x60, 0x56, 0x9e, 0x05, 0x53,
  7606. 0xbc, 0xfe, 0x86, 0x55, 0x3b, 0xca, 0xd5, 0x89,
  7607. 0xc7, 0x54, 0x1a, 0x73, 0xac, 0xc3, 0x9a, 0xbd,
  7608. 0x53, 0xc4, 0x07, 0x76, 0xd8, 0xe8, 0x22, 0x61,
  7609. 0x9e, 0xa9, 0xad, 0x77, 0xa0, 0x13, 0x4c, 0xfc
  7610. };
  7611. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  7612. 0xa3, 0xc6, 0xf3, 0xf3, 0x82, 0x79, 0x5b, 0x10,
  7613. 0x87, 0xd7, 0x02, 0x50, 0xdb, 0x2c, 0xd3, 0xb1,
  7614. 0xa1, 0x62, 0xa8, 0xb6, 0xdc, 0x12, 0x60, 0x61,
  7615. 0xc1, 0x0a, 0x84, 0xa5, 0x85, 0x3f, 0x3a, 0x89,
  7616. 0xe6, 0x6c, 0xdb, 0xb7, 0x9a, 0xb4, 0x28, 0x9b,
  7617. 0xc3, 0xea, 0xd8, 0x10, 0xe9, 0xc0, 0xaf, 0x92
  7618. };
  7619. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7620. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7621. ERROR_OUT(-5515, out);
  7622. #endif
  7623. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7624. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7625. ret = EVP_test(EVP_aes_256_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  7626. if (ret != 0) {
  7627. printf("EVP_aes_256_xts failed\n");
  7628. goto out;
  7629. }
  7630. #endif
  7631. XMEMSET(buf, 0, sizeof(buf));
  7632. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  7633. HEAP_HINT, devId) != 0)
  7634. ERROR_OUT(-5500, out);
  7635. else
  7636. aes_inited = 1;
  7637. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  7638. #if defined(WOLFSSL_ASYNC_CRYPT)
  7639. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7640. #endif
  7641. if (ret != 0)
  7642. ERROR_OUT(-5501, out);
  7643. if (XMEMCMP(c2, buf, sizeof(c2)))
  7644. ERROR_OUT(-5502, out);
  7645. wc_AesXtsFree(aes);
  7646. XMEMSET(buf, 0, sizeof(buf));
  7647. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7648. HEAP_HINT, devId) != 0)
  7649. ERROR_OUT(-5503, out);
  7650. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  7651. #if defined(WOLFSSL_ASYNC_CRYPT)
  7652. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7653. #endif
  7654. if (ret != 0)
  7655. ERROR_OUT(-5504, out);
  7656. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  7657. ERROR_OUT(-5505, out);
  7658. /* partial block encryption test */
  7659. XMEMSET(cipher, 0, sizeof(cipher));
  7660. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  7661. #if defined(WOLFSSL_ASYNC_CRYPT)
  7662. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7663. #endif
  7664. if (ret != 0)
  7665. ERROR_OUT(-5506, out);
  7666. wc_AesXtsFree(aes);
  7667. /* partial block decrypt test */
  7668. XMEMSET(buf, 0, sizeof(buf));
  7669. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7670. HEAP_HINT, devId) != 0)
  7671. ERROR_OUT(-5507, out);
  7672. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  7673. #if defined(WOLFSSL_ASYNC_CRYPT)
  7674. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7675. #endif
  7676. if (ret != 0)
  7677. ERROR_OUT(-5508, out);
  7678. if (XMEMCMP(pp, buf, sizeof(pp)))
  7679. ERROR_OUT(-5509, out);
  7680. /* NIST decrypt test vector */
  7681. XMEMSET(buf, 0, sizeof(buf));
  7682. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  7683. #if defined(WOLFSSL_ASYNC_CRYPT)
  7684. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7685. #endif
  7686. if (ret != 0)
  7687. ERROR_OUT(-5510, out);
  7688. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  7689. ERROR_OUT(-5511, out);
  7690. wc_AesXtsFree(aes);
  7691. XMEMSET(buf, 0, sizeof(buf));
  7692. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  7693. HEAP_HINT, devId) != 0)
  7694. ERROR_OUT(-5512, out);
  7695. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  7696. #if defined(WOLFSSL_ASYNC_CRYPT)
  7697. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7698. #endif
  7699. if (ret != 0)
  7700. ERROR_OUT(-5513, out);
  7701. if (XMEMCMP(p2, buf, sizeof(p2)))
  7702. ERROR_OUT(-5514, out);
  7703. out:
  7704. if (aes_inited)
  7705. wc_AesXtsFree(aes);
  7706. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7707. if (aes)
  7708. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7709. #endif
  7710. return ret;
  7711. }
  7712. #endif /* WOLFSSL_AES_256 */
  7713. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  7714. /* both 128 and 256 bit key test */
  7715. static int aes_xts_sector_test(void)
  7716. {
  7717. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7718. XtsAes *aes = NULL;
  7719. #else
  7720. XtsAes aes[1];
  7721. #endif
  7722. int aes_inited = 0;
  7723. int ret = 0;
  7724. unsigned char buf[AES_BLOCK_SIZE * 2];
  7725. /* 128 key tests */
  7726. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7727. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  7728. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  7729. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  7730. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  7731. };
  7732. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7733. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  7734. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  7735. };
  7736. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7737. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  7738. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  7739. };
  7740. word64 s1 = 141;
  7741. /* 256 key tests */
  7742. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  7743. 0xef, 0x01, 0x0c, 0xa1, 0xa3, 0x66, 0x3e, 0x32,
  7744. 0x53, 0x43, 0x49, 0xbc, 0x0b, 0xae, 0x62, 0x23,
  7745. 0x2a, 0x15, 0x73, 0x34, 0x85, 0x68, 0xfb, 0x9e,
  7746. 0xf4, 0x17, 0x68, 0xa7, 0x67, 0x4f, 0x50, 0x7a,
  7747. 0x72, 0x7f, 0x98, 0x75, 0x53, 0x97, 0xd0, 0xe0,
  7748. 0xaa, 0x32, 0xf8, 0x30, 0x33, 0x8c, 0xc7, 0xa9,
  7749. 0x26, 0xc7, 0x73, 0xf0, 0x9e, 0x57, 0xb3, 0x57,
  7750. 0xcd, 0x15, 0x6a, 0xfb, 0xca, 0x46, 0xe1, 0xa0
  7751. };
  7752. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  7753. 0xed, 0x98, 0xe0, 0x17, 0x70, 0xa8, 0x53, 0xb4,
  7754. 0x9d, 0xb9, 0xe6, 0xaa, 0xf8, 0x8f, 0x0a, 0x41,
  7755. 0xb9, 0xb5, 0x6e, 0x91, 0xa5, 0xa2, 0xb1, 0x1d,
  7756. 0x40, 0x52, 0x92, 0x54, 0xf5, 0x52, 0x3e, 0x75
  7757. };
  7758. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  7759. 0xca, 0x20, 0xc5, 0x5e, 0x8d, 0xc1, 0x49, 0x68,
  7760. 0x7d, 0x25, 0x41, 0xde, 0x39, 0xc3, 0xdf, 0x63,
  7761. 0x00, 0xbb, 0x5a, 0x16, 0x3c, 0x10, 0xce, 0xd3,
  7762. 0x66, 0x6b, 0x13, 0x57, 0xdb, 0x8b, 0xd3, 0x9d
  7763. };
  7764. word64 s2 = 187;
  7765. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7766. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7767. ERROR_OUT(-5612, out);
  7768. #endif
  7769. XMEMSET(buf, 0, sizeof(buf));
  7770. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7771. HEAP_HINT, devId) != 0)
  7772. ERROR_OUT(-5600, out);
  7773. else
  7774. aes_inited = 1;
  7775. ret = wc_AesXtsEncryptSector(aes, buf, p1, sizeof(p1), s1);
  7776. #if defined(WOLFSSL_ASYNC_CRYPT)
  7777. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7778. #endif
  7779. if (ret != 0)
  7780. ERROR_OUT(-5601, out);
  7781. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  7782. ERROR_OUT(-5602, out);
  7783. wc_AesXtsFree(aes);
  7784. /* decrypt test */
  7785. XMEMSET(buf, 0, sizeof(buf));
  7786. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7787. HEAP_HINT, devId) != 0)
  7788. ERROR_OUT(-5603, out);
  7789. ret = wc_AesXtsDecryptSector(aes, buf, c1, sizeof(c1), s1);
  7790. #if defined(WOLFSSL_ASYNC_CRYPT)
  7791. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7792. #endif
  7793. if (ret != 0)
  7794. ERROR_OUT(-5604, out);
  7795. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  7796. ERROR_OUT(-5605, out);
  7797. wc_AesXtsFree(aes);
  7798. /* 256 bit key tests */
  7799. XMEMSET(buf, 0, sizeof(buf));
  7800. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  7801. HEAP_HINT, devId) != 0)
  7802. ERROR_OUT(-5606, out);
  7803. ret = wc_AesXtsEncryptSector(aes, buf, p2, sizeof(p2), s2);
  7804. #if defined(WOLFSSL_ASYNC_CRYPT)
  7805. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7806. #endif
  7807. if (ret != 0)
  7808. ERROR_OUT(-5607, out);
  7809. if (XMEMCMP(c2, buf, sizeof(c2)))
  7810. ERROR_OUT(-5608, out);
  7811. wc_AesXtsFree(aes);
  7812. /* decrypt test */
  7813. XMEMSET(buf, 0, sizeof(buf));
  7814. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  7815. HEAP_HINT, devId) != 0)
  7816. ERROR_OUT(-5609, out);
  7817. ret = wc_AesXtsDecryptSector(aes, buf, c2, sizeof(c2), s2);
  7818. #if defined(WOLFSSL_ASYNC_CRYPT)
  7819. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7820. #endif
  7821. if (ret != 0)
  7822. ERROR_OUT(-5610, out);
  7823. if (XMEMCMP(p2, buf, sizeof(p2)))
  7824. ERROR_OUT(-5611, out);
  7825. out:
  7826. if (aes_inited)
  7827. wc_AesXtsFree(aes);
  7828. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7829. if (aes)
  7830. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7831. #endif
  7832. return ret;
  7833. }
  7834. #endif /* WOLFSSL_AES_128 && WOLFSSL_AES_256 */
  7835. #ifdef WOLFSSL_AES_128
  7836. /* testing of bad arguments */
  7837. static int aes_xts_args_test(void)
  7838. {
  7839. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7840. XtsAes *aes = NULL;
  7841. #else
  7842. XtsAes aes[1];
  7843. #endif
  7844. int aes_inited = 0;
  7845. int ret;
  7846. unsigned char buf[AES_BLOCK_SIZE * 2];
  7847. /* 128 key tests */
  7848. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7849. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  7850. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  7851. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  7852. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  7853. };
  7854. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7855. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  7856. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  7857. };
  7858. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7859. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  7860. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  7861. };
  7862. word64 s1 = 141;
  7863. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7864. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7865. ERROR_OUT(-5708, out);
  7866. #endif
  7867. if (wc_AesXtsSetKey(NULL, k1, sizeof(k1), AES_ENCRYPTION,
  7868. HEAP_HINT, devId) == 0)
  7869. ERROR_OUT(-5700, out);
  7870. if (wc_AesXtsSetKey(aes, NULL, sizeof(k1), AES_ENCRYPTION,
  7871. HEAP_HINT, devId) == 0)
  7872. ERROR_OUT(-5701, out);
  7873. /* encryption operations */
  7874. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7875. HEAP_HINT, devId) != 0)
  7876. ERROR_OUT(-5702, out);
  7877. else
  7878. aes_inited = 1;
  7879. ret = wc_AesXtsEncryptSector(NULL, buf, p1, sizeof(p1), s1);
  7880. #if defined(WOLFSSL_ASYNC_CRYPT)
  7881. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7882. #endif
  7883. if (ret == 0)
  7884. ERROR_OUT(-5703, out);
  7885. ret = wc_AesXtsEncryptSector(aes, NULL, p1, sizeof(p1), s1);
  7886. #if defined(WOLFSSL_ASYNC_CRYPT)
  7887. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7888. #endif
  7889. if (ret == 0)
  7890. ERROR_OUT(-5704, out);
  7891. wc_AesXtsFree(aes);
  7892. /* decryption operations */
  7893. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7894. HEAP_HINT, devId) != 0)
  7895. ERROR_OUT(-5705, out);
  7896. ret = wc_AesXtsDecryptSector(NULL, buf, c1, sizeof(c1), s1);
  7897. #if defined(WOLFSSL_ASYNC_CRYPT)
  7898. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7899. #endif
  7900. if (ret == 0)
  7901. ERROR_OUT(-5706, out);
  7902. ret = wc_AesXtsDecryptSector(aes, NULL, c1, sizeof(c1), s1);
  7903. #if defined(WOLFSSL_ASYNC_CRYPT)
  7904. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7905. #endif
  7906. if (ret == 0)
  7907. ERROR_OUT(-5707, out);
  7908. ret = 0;
  7909. out:
  7910. if (aes_inited)
  7911. wc_AesXtsFree(aes);
  7912. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7913. if (aes)
  7914. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7915. #endif
  7916. return ret;
  7917. }
  7918. #endif /* WOLFSSL_AES_128 */
  7919. #endif /* WOLFSSL_AES_XTS */
  7920. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  7921. static int aes_cbc_test(void)
  7922. {
  7923. byte cipher[AES_BLOCK_SIZE];
  7924. byte plain[AES_BLOCK_SIZE];
  7925. int ret;
  7926. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  7927. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  7928. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  7929. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  7930. };
  7931. byte key[] = "0123456789abcdef "; /* align */
  7932. byte iv[] = "1234567890abcdef "; /* align */
  7933. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  7934. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  7935. /* Parameter Validation testing. */
  7936. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key, 17, NULL);
  7937. if (ret != BAD_FUNC_ARG)
  7938. return -5800;
  7939. #ifdef HAVE_AES_DECRYPT
  7940. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key, 17, NULL);
  7941. if (ret != BAD_FUNC_ARG)
  7942. return -5801;
  7943. #endif
  7944. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key,
  7945. AES_BLOCK_SIZE, iv);
  7946. if (ret != 0)
  7947. return -5802;
  7948. #ifdef HAVE_AES_DECRYPT
  7949. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key,
  7950. AES_BLOCK_SIZE, iv);
  7951. if (ret != 0)
  7952. return -5803;
  7953. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE) != 0)
  7954. return -5804;
  7955. #endif /* HAVE_AES_DECRYPT */
  7956. (void)plain;
  7957. return 0;
  7958. }
  7959. #endif
  7960. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  7961. static int aesecb_test(void)
  7962. {
  7963. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7964. Aes *enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7965. #else
  7966. Aes enc[1];
  7967. #endif
  7968. byte cipher[AES_BLOCK_SIZE * 4];
  7969. #ifdef HAVE_AES_DECRYPT
  7970. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7971. Aes *dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7972. #else
  7973. Aes dec[1];
  7974. #endif
  7975. byte plain [AES_BLOCK_SIZE * 4];
  7976. #endif /* HAVE_AES_DECRYPT */
  7977. int ret = 0;
  7978. #if defined(WOLFSSL_AES_256)
  7979. {
  7980. WOLFSSL_SMALL_STACK_STATIC const byte niPlain[] =
  7981. {
  7982. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  7983. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  7984. };
  7985. WOLFSSL_SMALL_STACK_STATIC const byte niCipher[] =
  7986. {
  7987. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  7988. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  7989. };
  7990. WOLFSSL_SMALL_STACK_STATIC const byte niKey[] =
  7991. {
  7992. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  7993. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  7994. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  7995. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  7996. };
  7997. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  7998. ERROR_OUT(-5900, out);
  7999. #if defined(HAVE_AES_DECRYPT)
  8000. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  8001. ERROR_OUT(-5901, out);
  8002. #endif
  8003. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  8004. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  8005. if (ret != 0)
  8006. ERROR_OUT(-5923, out);
  8007. if (wc_AesEcbEncrypt(enc, cipher, niPlain, AES_BLOCK_SIZE) != 0)
  8008. ERROR_OUT(-5924, out);
  8009. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  8010. ERROR_OUT(-5925, out);
  8011. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  8012. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  8013. if (ret != 0)
  8014. ERROR_OUT(-5926, out);
  8015. if (wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE) != 0)
  8016. ERROR_OUT(-5927, out);
  8017. wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE);
  8018. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  8019. ERROR_OUT(-5928, out);
  8020. }
  8021. wc_AesFree(enc);
  8022. #ifdef HAVE_AES_DECRYPT
  8023. wc_AesFree(dec);
  8024. #endif
  8025. out:
  8026. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8027. if (enc)
  8028. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8029. #ifdef HAVE_AES_DECRYPT
  8030. if (dec)
  8031. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8032. #endif
  8033. #endif
  8034. #endif /* WOLFSSL_AES_256 */
  8035. return ret;
  8036. }
  8037. #endif /* HAVE_AES_ECB */
  8038. #ifdef WOLFSSL_AES_COUNTER
  8039. static int aesctr_test(Aes* enc, Aes* dec, byte* cipher, byte* plain)
  8040. {
  8041. int ret;
  8042. /* test vectors from "Recommendation for Block Cipher Modes of
  8043. * Operation" NIST Special Publication 800-38A */
  8044. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  8045. {
  8046. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  8047. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  8048. };
  8049. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  8050. {
  8051. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8052. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  8053. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  8054. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  8055. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  8056. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  8057. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  8058. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  8059. };
  8060. #ifdef WOLFSSL_ARMASM
  8061. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap32[] =
  8062. {
  8063. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,
  8064. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  8065. };
  8066. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap32_2[] =
  8067. {
  8068. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8069. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xfe
  8070. };
  8071. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap64[] =
  8072. {
  8073. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8074. 0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  8075. };
  8076. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap64_2[] =
  8077. {
  8078. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0,
  8079. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  8080. };
  8081. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap96[] =
  8082. {
  8083. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8084. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff
  8085. };
  8086. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap96_2[] =
  8087. {
  8088. 0xff,0xff,0xff,0xf0,0xff,0xff,0xff,0xff,
  8089. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  8090. };
  8091. #endif
  8092. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap128[] =
  8093. {
  8094. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8095. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  8096. };
  8097. #ifdef WOLFSSL_ARMASM
  8098. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap128_2[] =
  8099. {
  8100. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8101. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  8102. };
  8103. #endif
  8104. #ifdef WOLFSSL_AES_128
  8105. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  8106. {
  8107. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  8108. 0xc2
  8109. };
  8110. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Key[] =
  8111. {
  8112. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  8113. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  8114. };
  8115. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Cipher[] =
  8116. {
  8117. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  8118. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  8119. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  8120. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  8121. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  8122. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  8123. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  8124. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  8125. };
  8126. #ifdef WOLFSSL_ARMASM
  8127. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32Cipher[] =
  8128. {
  8129. 0xb3,0x8b,0x58,0xbc,0xce,0xf4,0x71,0x78,
  8130. 0xf6,0x7c,0xdb,0xb4,0x27,0x2b,0x0a,0xbf,
  8131. 0x7e,0xad,0xea,0x5c,0xd1
  8132. };
  8133. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32CipherLong[] =
  8134. {
  8135. 0xb3,0x8b,0x58,0xbc,0xce,0xf4,0x71,0x78,
  8136. 0xf6,0x7c,0xdb,0xb4,0x27,0x2b,0x0a,0xbf,
  8137. 0x7e,0xad,0xea,0x5c,0xd1,0xb7,0x98,0xf0,
  8138. 0x22,0x20,0xfe,0x67,0xb0,0x02,0x23,0x50
  8139. };
  8140. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32_2CipherLong[] =
  8141. {
  8142. 0x6e,0xa1,0x27,0x4d,0xea,0x20,0x5f,0x39,
  8143. 0x68,0xc8,0xb6,0x78,0xde,0xfc,0x53,0x5c,
  8144. 0x90,0xc8,0xf6,0xc6,0xfa,0xe0,0x7b,0x09,
  8145. 0x7c,0xf8,0x9c,0x6a,0x5a,0xa5,0x17,0x7f,
  8146. 0x03,0x92,0x5f,0x4e,0x85,0xea,0x26,0xc9,
  8147. 0x5a,0xc2,0x74,0xe2,0xbf,0xe4,0x1b,0xd4
  8148. };
  8149. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64Cipher[] =
  8150. {
  8151. 0xdd,0x17,0x10,0x7c,0x45,0x04,0xac,0x43,
  8152. 0xef,0xa8,0xcc,0x32,0x34,0x87,0x88,0xd7,
  8153. 0xae,0x74,0x94,0x72,0x8e
  8154. };
  8155. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64CipherLong[] =
  8156. {
  8157. 0xdd,0x17,0x10,0x7c,0x45,0x04,0xac,0x43,
  8158. 0xef,0xa8,0xcc,0x32,0x34,0x87,0x88,0xd7,
  8159. 0xae,0x74,0x94,0x72,0x8e,0xd0,0x71,0xc0,
  8160. 0x89,0x8a,0xa1,0xb0,0x29,0xa0,0x10,0x9e
  8161. };
  8162. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64_2CipherLong[] =
  8163. {
  8164. 0x3f,0xe7,0xd5,0xf3,0xfa,0x09,0xfe,0x40,
  8165. 0xa6,0xa1,0x32,0x8b,0x57,0x12,0xb9,0xfa,
  8166. 0xf2,0x2d,0xe4,0x3c,0x66,0x1d,0x0a,0x8e,
  8167. 0x46,0xf8,0x2e,0x33,0xce,0x8d,0x4e,0x3b,
  8168. 0x17,0x67,0x9e,0x9f,0x76,0x9e,0xc2,0x99,
  8169. 0xd5,0xd4,0x71,0xed,0xb4,0x33,0xb2,0xcd
  8170. };
  8171. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96Cipher[] =
  8172. {
  8173. 0x55,0x24,0xc2,0x73,0xca,0xa3,0x48,0x03,
  8174. 0x0b,0x72,0x8d,0xd7,0x6c,0x99,0x8e,0x04,
  8175. 0x9d,0x77,0xc9,0x5f,0x38
  8176. };
  8177. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96CipherLong[] =
  8178. {
  8179. 0x55,0x24,0xc2,0x73,0xca,0xa3,0x48,0x03,
  8180. 0x0b,0x72,0x8d,0xd7,0x6c,0x99,0x8e,0x04,
  8181. 0x9d,0x77,0xc9,0x5f,0x38,0xb5,0x6e,0x44,
  8182. 0x21,0x8e,0xda,0x57,0xe0,0x41,0xc7,0x6a
  8183. };
  8184. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96_2CipherLong[] =
  8185. {
  8186. 0xc8,0x81,0x1a,0xbe,0xc7,0x5b,0x93,0x6f,
  8187. 0xe6,0x52,0xe4,0xb1,0x2d,0x1c,0x39,0xbc,
  8188. 0xeb,0x82,0x27,0x0a,0x7e,0xa5,0x0e,0x2d,
  8189. 0x32,0xda,0xbe,0x10,0x7a,0x10,0xcc,0xd3,
  8190. 0x6f,0xc6,0x83,0x28,0x05,0x57,0x8a,0x24,
  8191. 0x44,0x76,0x17,0x81,0xb9,0x5c,0x94,0x81
  8192. };
  8193. #endif
  8194. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128Cipher[] =
  8195. {
  8196. 0xe1,0x33,0x38,0xe3,0x6c,0xb7,0x19,0x62,
  8197. 0xe0,0x0d,0x02,0x0b,0x4c,0xed,0xbd,0x86,
  8198. 0xd3,0xda,0xe1,0x5b,0x04
  8199. };
  8200. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128CipherLong[] =
  8201. {
  8202. 0xe1,0x33,0x38,0xe3,0x6c,0xb7,0x19,0x62,
  8203. 0xe0,0x0d,0x02,0x0b,0x4c,0xed,0xbd,0x86,
  8204. 0xd3,0xda,0xe1,0x5b,0x04,0xbb,0x35,0x2f,
  8205. 0xa0,0xf5,0x9f,0xeb,0xfc,0xb4,0xda,0x3e
  8206. };
  8207. #ifdef WOLFSSL_ARMASM
  8208. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128_2CipherLong[] =
  8209. {
  8210. 0xba,0x76,0xaa,0x54,0xd5,0xb5,0x60,0x67,
  8211. 0xc1,0xa7,0x90,0x3b,0x3f,0xdd,0xfa,0x89,
  8212. 0x24,0xdf,0x0c,0x56,0x5c,0xf4,0x2a,0x68,
  8213. 0x97,0x87,0x13,0xb6,0x7a,0xd1,0x24,0xfd,
  8214. 0x4d,0x3f,0x77,0x4a,0xb9,0xe4,0x7d,0xa2,
  8215. 0xdb,0xb9,0x31,0x5e,0xa3,0x11,0x06,0x80
  8216. };
  8217. #endif
  8218. #endif /* WOLFSSL_AES_128 */
  8219. #ifdef WOLFSSL_AES_192
  8220. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  8221. {
  8222. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  8223. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  8224. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  8225. };
  8226. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  8227. {
  8228. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  8229. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b,
  8230. 0x09,0x03,0x39,0xec,0x0a,0xa6,0xfa,0xef,
  8231. 0xd5,0xcc,0xc2,0xc6,0xf4,0xce,0x8e,0x94,
  8232. 0x1e,0x36,0xb2,0x6b,0xd1,0xeb,0xc6,0x70,
  8233. 0xd1,0xbd,0x1d,0x66,0x56,0x20,0xab,0xf7,
  8234. 0x4f,0x78,0xa7,0xf6,0xd2,0x98,0x09,0x58,
  8235. 0x5a,0x97,0xda,0xec,0x58,0xc6,0xb0,0x50
  8236. };
  8237. #ifdef WOLFSSL_ARMASM
  8238. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32Cipher[] =
  8239. {
  8240. 0x28,0xaa,0xfa,0x90,0x72,0x74,0x86,0xaf,
  8241. 0x72,0x73,0x35,0x17,0x70,0x4e,0x7d,0xca,
  8242. 0x0c,0x33,0x97,0x06,0xc0
  8243. };
  8244. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32CipherLong[] =
  8245. {
  8246. 0x28,0xaa,0xfa,0x90,0x72,0x74,0x86,0xaf,
  8247. 0x72,0x73,0x35,0x17,0x70,0x4e,0x7d,0xca,
  8248. 0x0c,0x33,0x97,0x06,0xc0,0xbe,0x83,0x87,
  8249. 0xdd,0xd3,0xff,0xd8,0xe4,0x6a,0x5b,0x84
  8250. };
  8251. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32_2CipherLong[] =
  8252. {
  8253. 0xf5,0x00,0xa2,0x91,0x54,0xa3,0x76,0xa2,
  8254. 0xdd,0xad,0x16,0x89,0xe5,0xf0,0x1d,0x40,
  8255. 0x84,0xcd,0x74,0x84,0xcb,0x8b,0x9e,0x29,
  8256. 0xa9,0x8a,0x12,0x65,0xa0,0x79,0x5e,0xce,
  8257. 0xd9,0x50,0x65,0x21,0x86,0xb0,0x85,0x0d,
  8258. 0x98,0x2d,0x9a,0x5a,0x11,0xbe,0xa5,0x7f
  8259. };
  8260. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64Cipher[] =
  8261. {
  8262. 0xfe,0x39,0x27,0x97,0xac,0xe5,0xb8,0x74,
  8263. 0xb9,0x8c,0xbf,0x58,0x71,0xa4,0x80,0x33,
  8264. 0x3d,0xf7,0xb4,0xfd,0x8c
  8265. };
  8266. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64CipherLong[] =
  8267. {
  8268. 0xfe,0x39,0x27,0x97,0xac,0xe5,0xb8,0x74,
  8269. 0xb9,0x8c,0xbf,0x58,0x71,0xa4,0x80,0x33,
  8270. 0x3d,0xf7,0xb4,0xfd,0x8c,0x55,0x47,0x10,
  8271. 0xd5,0x91,0x35,0xbe,0xd8,0x0d,0xa5,0x9e
  8272. };
  8273. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64_2CipherLong[] =
  8274. {
  8275. 0x59,0xf1,0xed,0x70,0x62,0x42,0xa8,0x06,
  8276. 0x07,0x36,0xe1,0xc5,0x04,0x79,0xc3,0x9b,
  8277. 0xd1,0x14,0x5c,0xcc,0x6f,0x81,0x5f,0x2f,
  8278. 0xa0,0xde,0xcf,0x61,0x55,0x18,0x7a,0xac,
  8279. 0xb0,0x59,0x37,0x90,0x53,0xb3,0x00,0x88,
  8280. 0xb4,0x49,0x90,0x7b,0x96,0xcd,0xcc,0xc3
  8281. };
  8282. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96Cipher[] =
  8283. {
  8284. 0x41,0x21,0x40,0x31,0xfb,0xc8,0xad,0x23,
  8285. 0xde,0x00,0x03,0xd8,0x96,0x45,0xc7,0xb5,
  8286. 0x47,0xb5,0xf3,0x30,0x3b
  8287. };
  8288. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96CipherLong[] =
  8289. {
  8290. 0x41,0x21,0x40,0x31,0xfb,0xc8,0xad,0x23,
  8291. 0xde,0x00,0x03,0xd8,0x96,0x45,0xc7,0xb5,
  8292. 0x47,0xb5,0xf3,0x30,0x3b,0xef,0xcd,0x80,
  8293. 0xe3,0x61,0x34,0xef,0x4e,0x1b,0x79,0xc1
  8294. };
  8295. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96_2CipherLong[] =
  8296. {
  8297. 0x3c,0xb2,0xff,0xc0,0x24,0xe1,0xf5,0xc4,
  8298. 0x0f,0xd1,0x0a,0x1b,0xbe,0x1f,0x23,0xa1,
  8299. 0x8e,0xbf,0x2b,0x96,0xb6,0x37,0xc8,0x25,
  8300. 0x06,0x90,0xe2,0xca,0x71,0x24,0x52,0x95,
  8301. 0xaa,0x8c,0x80,0xdf,0xb7,0xd7,0x30,0xb0,
  8302. 0xcc,0x06,0x4f,0x28,0xa2,0x74,0x27,0xf8
  8303. };
  8304. #endif
  8305. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128Cipher[] =
  8306. {
  8307. 0x5c,0xc3,0x8f,0xab,0x30,0xb6,0xac,0x67,
  8308. 0xdc,0xc2,0x1e,0x7b,0x01,0x2e,0xcf,0x98,
  8309. 0x8c,0x68,0xa7,0xd9,0x57
  8310. };
  8311. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128CipherLong[] =
  8312. {
  8313. 0x5c,0xc3,0x8f,0xab,0x30,0xb6,0xac,0x67,
  8314. 0xdc,0xc2,0x1e,0x7b,0x01,0x2e,0xcf,0x98,
  8315. 0x8c,0x68,0xa7,0xd9,0x57,0xab,0x09,0x0f,
  8316. 0x01,0xc4,0x4e,0x62,0xaf,0xc2,0xdf,0x1a
  8317. };
  8318. #ifdef WOLFSSL_ARMASM
  8319. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128_2CipherLong[] =
  8320. {
  8321. 0x88,0x0a,0x26,0x4e,0xa8,0x26,0x21,0xe0,
  8322. 0xfc,0xbc,0x63,0xdc,0xd9,0x60,0x52,0xb2,
  8323. 0x99,0x2f,0xbb,0x1e,0x00,0xf5,0x9f,0x6d,
  8324. 0xab,0x48,0x0f,0xc6,0x37,0x12,0x56,0xe3,
  8325. 0x12,0x8d,0x31,0xc8,0xea,0xf4,0x41,0x82,
  8326. 0x7a,0x88,0xe0,0xd7,0xf0,0x67,0x03,0xa4
  8327. };
  8328. #endif
  8329. #endif
  8330. #ifdef WOLFSSL_AES_256
  8331. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  8332. {
  8333. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  8334. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  8335. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  8336. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  8337. };
  8338. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  8339. {
  8340. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  8341. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28,
  8342. 0xf4,0x43,0xe3,0xca,0x4d,0x62,0xb5,0x9a,
  8343. 0xca,0x84,0xe9,0x90,0xca,0xca,0xf5,0xc5,
  8344. 0x2b,0x09,0x30,0xda,0xa2,0x3d,0xe9,0x4c,
  8345. 0xe8,0x70,0x17,0xba,0x2d,0x84,0x98,0x8d,
  8346. 0xdf,0xc9,0xc5,0x8d,0xb6,0x7a,0xad,0xa6,
  8347. 0x13,0xc2,0xdd,0x08,0x45,0x79,0x41,0xa6
  8348. };
  8349. #ifdef WOLFSSL_ARMASM
  8350. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32Cipher[] =
  8351. {
  8352. 0xb0,0xa8,0xc0,0x65,0x85,0x20,0x0d,0x5c,
  8353. 0x25,0xcf,0xe7,0x58,0x63,0xc8,0xd4,0xea,
  8354. 0xa2,0x13,0x47,0x74,0xda
  8355. };
  8356. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32CipherLong[] =
  8357. {
  8358. 0xb0,0xa8,0xc0,0x65,0x85,0x20,0x0d,0x5c,
  8359. 0x25,0xcf,0xe7,0x58,0x63,0xc8,0xd4,0xea,
  8360. 0xa2,0x13,0x47,0x74,0xda,0x89,0x77,0x40,
  8361. 0x28,0x9c,0xe8,0x19,0x26,0x32,0xd8,0x1f
  8362. };
  8363. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32_2CipherLong[] =
  8364. {
  8365. 0xf6,0xd9,0x22,0xc6,0x80,0x29,0xaf,0x14,
  8366. 0x54,0x6c,0x0a,0xce,0x42,0xea,0x3c,0xa1,
  8367. 0x7c,0xeb,0x36,0x0d,0x8e,0xd7,0x8c,0x59,
  8368. 0xa8,0x09,0x9f,0x9e,0xba,0x5b,0x95,0xfa,
  8369. 0x26,0x8c,0x37,0x59,0xf8,0xae,0x8e,0xaa,
  8370. 0x4d,0xe4,0x1c,0xfe,0x51,0xc7,0xb7,0xcc
  8371. };
  8372. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64Cipher[] =
  8373. {
  8374. 0x74,0x1a,0x52,0x41,0x76,0xb4,0x11,0x8f,
  8375. 0xfd,0x57,0x31,0xfd,0x3d,0x76,0x8f,0x07,
  8376. 0xd4,0x94,0x4c,0xcd,0x4d
  8377. };
  8378. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64CipherLong[] =
  8379. {
  8380. 0x74,0x1a,0x52,0x41,0x76,0xb4,0x11,0x8f,
  8381. 0xfd,0x57,0x31,0xfd,0x3d,0x76,0x8f,0x07,
  8382. 0xd4,0x94,0x4c,0xcd,0x4d,0x47,0x5a,0x92,
  8383. 0x26,0x49,0x81,0x7a,0xda,0x36,0x27,0x01
  8384. };
  8385. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64_2CipherLong[] =
  8386. {
  8387. 0xf7,0x9c,0xbf,0xf6,0xa2,0xaa,0x8a,0x0a,
  8388. 0x63,0x8a,0x20,0x2f,0x12,0xf1,0x8e,0x49,
  8389. 0x30,0xc0,0x8d,0x5c,0x5f,0x8b,0xbc,0x16,
  8390. 0xdd,0x71,0xee,0x13,0x14,0x7b,0xe1,0x25,
  8391. 0xcb,0x87,0x8a,0xc6,0xdc,0x1d,0x54,0x7a,
  8392. 0xe1,0xe4,0x6f,0x0d,0x95,0x1b,0xd1,0x8b
  8393. };
  8394. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96Cipher[] =
  8395. {
  8396. 0xb9,0x07,0x02,0xb8,0xbe,0x94,0xbf,0x53,
  8397. 0xdf,0x83,0x8e,0x23,0x8c,0x67,0x0c,0x81,
  8398. 0xb8,0x69,0xa1,0x48,0x45
  8399. };
  8400. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96CipherLong[] =
  8401. {
  8402. 0xb9,0x07,0x02,0xb8,0xbe,0x94,0xbf,0x53,
  8403. 0xdf,0x83,0x8e,0x23,0x8c,0x67,0x0c,0x81,
  8404. 0xb8,0x69,0xa1,0x48,0x45,0xf1,0xc6,0x27,
  8405. 0x36,0xa8,0xb2,0x4b,0x0e,0x62,0x6b,0x72
  8406. };
  8407. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96_2CipherLong[] =
  8408. {
  8409. 0xd5,0x56,0x73,0xaa,0xb8,0xe4,0x06,0xf6,
  8410. 0x83,0x45,0x3a,0xb4,0xb9,0x63,0xec,0xad,
  8411. 0x73,0xc5,0xab,0x78,0xb1,0x21,0xab,0xef,
  8412. 0x69,0x15,0xb7,0x0c,0xe9,0xb4,0x3a,0xe7,
  8413. 0xbc,0xc4,0x22,0xbd,0x93,0xba,0x52,0xe0,
  8414. 0x91,0x2f,0x5e,0x8d,0x6d,0x59,0xf7,0xc2
  8415. };
  8416. #endif
  8417. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128Cipher[] =
  8418. {
  8419. 0x50,0xfd,0x97,0xc3,0xe6,0x1a,0xbb,0x48,
  8420. 0x73,0xfb,0x78,0xdf,0x1e,0x8e,0x77,0xe6,
  8421. 0x4b,0x45,0x7c,0xd6,0x8a
  8422. };
  8423. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128CipherLong[] =
  8424. {
  8425. 0x50,0xfd,0x97,0xc3,0xe6,0x1a,0xbb,0x48,
  8426. 0x73,0xfb,0x78,0xdf,0x1e,0x8e,0x77,0xe6,
  8427. 0x4b,0x45,0x7c,0xd6,0x8a,0xcc,0xda,0x4a,
  8428. 0x89,0xfa,0x23,0x6c,0x06,0xbf,0x26,0x05
  8429. };
  8430. #ifdef WOLFSSL_ARMASM
  8431. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128_2CipherLong[] =
  8432. {
  8433. 0x24,0x5c,0x09,0xa0,0x3b,0x1a,0x5a,0x94,
  8434. 0x2b,0x93,0x56,0x13,0x48,0xa0,0x21,0xce,
  8435. 0x95,0x11,0xa3,0x76,0xd6,0x59,0x88,0x42,
  8436. 0x04,0x71,0x69,0x62,0x28,0xb2,0xee,0x9d,
  8437. 0xd5,0xa0,0xea,0xc7,0x37,0x93,0x92,0xc7,
  8438. 0xf2,0xb6,0x8d,0xd9,0x59,0x1a,0xfa,0xbb
  8439. };
  8440. #endif
  8441. #endif
  8442. int i;
  8443. struct {
  8444. const byte* key;
  8445. int keySz;
  8446. const byte* iv;
  8447. const byte* plain;
  8448. int len;
  8449. const byte* cipher;
  8450. } testVec[] = {
  8451. #ifdef WOLFSSL_AES_128
  8452. { ctr128Key, (int)sizeof(ctr128Key), ctrIv,
  8453. ctrPlain, (int)sizeof(ctrPlain), ctr128Cipher },
  8454. /* let's try with just 9 bytes, non block size test */
  8455. { ctr128Key, (int)sizeof(ctr128Key), ctrIv,
  8456. ctrPlain, (int)sizeof(oddCipher), ctr128Cipher },
  8457. /* and an additional 9 bytes to reuse tmp left buffer */
  8458. { NULL, 0, NULL, ctrPlain, (int)sizeof(oddCipher), oddCipher },
  8459. /* Counter wrapping */
  8460. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128,
  8461. ctrPlain, (int)sizeof(ctr128Wrap128Cipher), ctr128Wrap128Cipher },
  8462. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128,
  8463. ctrPlain, (int)sizeof(ctr128Wrap128CipherLong),
  8464. ctr128Wrap128CipherLong },
  8465. #ifdef WOLFSSL_ARMASM
  8466. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128_2,
  8467. ctrPlain, (int)sizeof(ctr128Wrap128_2CipherLong),
  8468. ctr128Wrap128_2CipherLong },
  8469. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96,
  8470. ctrPlain, (int)sizeof(ctr128Wrap96Cipher), ctr128Wrap96Cipher },
  8471. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96,
  8472. ctrPlain, (int)sizeof(ctr128Wrap96CipherLong),
  8473. ctr128Wrap96CipherLong },
  8474. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96_2,
  8475. ctrPlain, (int)sizeof(ctr128Wrap96_2CipherLong),
  8476. ctr128Wrap96_2CipherLong },
  8477. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64,
  8478. ctrPlain, (int)sizeof(ctr128Wrap64Cipher), ctr128Wrap64Cipher },
  8479. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64,
  8480. ctrPlain, (int)sizeof(ctr128Wrap64CipherLong),
  8481. ctr128Wrap64CipherLong },
  8482. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64_2,
  8483. ctrPlain, (int)sizeof(ctr128Wrap64_2CipherLong),
  8484. ctr128Wrap64_2CipherLong },
  8485. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32,
  8486. ctrPlain, (int)sizeof(ctr128Wrap32Cipher), ctr128Wrap32Cipher },
  8487. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32,
  8488. ctrPlain, (int)sizeof(ctr128Wrap32CipherLong),
  8489. ctr128Wrap32CipherLong },
  8490. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32_2,
  8491. ctrPlain, (int)sizeof(ctr128Wrap32_2CipherLong),
  8492. ctr128Wrap32_2CipherLong },
  8493. #endif
  8494. #endif
  8495. #ifdef WOLFSSL_AES_192
  8496. { ctr192Key, (int)sizeof(ctr192Key), ctrIv,
  8497. ctrPlain, (int)sizeof(ctrPlain), ctr192Cipher },
  8498. /* let's try with just 9 bytes, non block size test */
  8499. { ctr192Key, (int)sizeof(ctr192Key), ctrIv,
  8500. ctrPlain, (int)sizeof(oddCipher), ctr192Cipher },
  8501. /* Counter wrapping */
  8502. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128,
  8503. ctrPlain, (int)sizeof(ctr192Wrap128Cipher), ctr192Wrap128Cipher },
  8504. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128,
  8505. ctrPlain, (int)sizeof(ctr192Wrap128CipherLong),
  8506. ctr192Wrap128CipherLong },
  8507. #ifdef WOLFSSL_ARMASM
  8508. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128_2,
  8509. ctrPlain, (int)sizeof(ctr192Wrap128_2CipherLong),
  8510. ctr192Wrap128_2CipherLong },
  8511. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96,
  8512. ctrPlain, (int)sizeof(ctr192Wrap96Cipher), ctr192Wrap96Cipher },
  8513. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96,
  8514. ctrPlain, (int)sizeof(ctr192Wrap96CipherLong),
  8515. ctr192Wrap96CipherLong },
  8516. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96_2,
  8517. ctrPlain, (int)sizeof(ctr192Wrap96_2CipherLong),
  8518. ctr192Wrap96_2CipherLong },
  8519. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64,
  8520. ctrPlain, (int)sizeof(ctr192Wrap64Cipher), ctr192Wrap64Cipher },
  8521. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64,
  8522. ctrPlain, (int)sizeof(ctr192Wrap64CipherLong),
  8523. ctr192Wrap64CipherLong },
  8524. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64_2,
  8525. ctrPlain, (int)sizeof(ctr192Wrap64_2CipherLong),
  8526. ctr192Wrap64_2CipherLong },
  8527. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32,
  8528. ctrPlain, (int)sizeof(ctr192Wrap32Cipher), ctr192Wrap32Cipher },
  8529. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32,
  8530. ctrPlain, (int)sizeof(ctr192Wrap32CipherLong),
  8531. ctr192Wrap32CipherLong },
  8532. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32_2,
  8533. ctrPlain, (int)sizeof(ctr192Wrap32_2CipherLong),
  8534. ctr192Wrap32_2CipherLong },
  8535. #endif
  8536. #endif
  8537. #ifdef WOLFSSL_AES_256
  8538. { ctr256Key, (int)sizeof(ctr256Key), ctrIv,
  8539. ctrPlain, (int)sizeof(ctrPlain), ctr256Cipher },
  8540. /* let's try with just 9 bytes, non block size test */
  8541. { ctr256Key, (int)sizeof(ctr256Key), ctrIv,
  8542. ctrPlain, (int)sizeof(oddCipher), ctr256Cipher },
  8543. /* Counter wrapping */
  8544. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128,
  8545. ctrPlain, (int)sizeof(ctr256Wrap128Cipher), ctr256Wrap128Cipher },
  8546. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128,
  8547. ctrPlain, (int)sizeof(ctr256Wrap128CipherLong),
  8548. ctr256Wrap128CipherLong },
  8549. #ifdef WOLFSSL_ARMASM
  8550. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128_2,
  8551. ctrPlain, (int)sizeof(ctr256Wrap128_2CipherLong),
  8552. ctr256Wrap128_2CipherLong },
  8553. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96,
  8554. ctrPlain, (int)sizeof(ctr256Wrap96Cipher), ctr256Wrap96Cipher },
  8555. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96,
  8556. ctrPlain, (int)sizeof(ctr256Wrap96CipherLong),
  8557. ctr256Wrap96CipherLong },
  8558. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96_2,
  8559. ctrPlain, (int)sizeof(ctr256Wrap96_2CipherLong),
  8560. ctr256Wrap96_2CipherLong },
  8561. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64,
  8562. ctrPlain, (int)sizeof(ctr256Wrap64Cipher), ctr256Wrap64Cipher },
  8563. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64,
  8564. ctrPlain, (int)sizeof(ctr256Wrap64CipherLong),
  8565. ctr256Wrap64CipherLong },
  8566. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64_2,
  8567. ctrPlain, (int)sizeof(ctr256Wrap64_2CipherLong),
  8568. ctr256Wrap64_2CipherLong },
  8569. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32,
  8570. ctrPlain, (int)sizeof(ctr256Wrap32Cipher), ctr256Wrap32Cipher },
  8571. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32,
  8572. ctrPlain, (int)sizeof(ctr256Wrap32CipherLong),
  8573. ctr256Wrap32CipherLong },
  8574. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32_2,
  8575. ctrPlain, (int)sizeof(ctr256Wrap32_2CipherLong),
  8576. ctr256Wrap32_2CipherLong },
  8577. #endif
  8578. #endif
  8579. };
  8580. #define AES_CTR_TEST_LEN (int)(sizeof(testVec) / sizeof(*testVec))
  8581. for (i = 0; i < AES_CTR_TEST_LEN; i++) {
  8582. if (testVec[i].key != NULL) {
  8583. ret = wc_AesSetKeyDirect(enc, testVec[i].key, testVec[i].keySz,
  8584. testVec[i].iv, AES_ENCRYPTION);
  8585. if (ret != 0) {
  8586. ERROR_OUT(-5930 - i * 10, out);
  8587. }
  8588. /* Ctr only uses encrypt, even on key setup */
  8589. ret = wc_AesSetKeyDirect(dec, testVec[i].key, testVec[i].keySz,
  8590. testVec[i].iv, AES_ENCRYPTION);
  8591. if (ret != 0) {
  8592. ERROR_OUT(-5931 - i * 10, out);
  8593. }
  8594. }
  8595. ret = wc_AesCtrEncrypt(enc, cipher, testVec[i].plain, testVec[i].len);
  8596. if (ret != 0) {
  8597. ERROR_OUT(-5932 - i * 10, out);
  8598. }
  8599. ret = wc_AesCtrEncrypt(dec, plain, cipher, testVec[i].len);
  8600. if (ret != 0) {
  8601. ERROR_OUT(-5933 - i * 10, out);
  8602. }
  8603. if (XMEMCMP(plain, ctrPlain, testVec[i].len)) {
  8604. ERROR_OUT(-5934 - i * 10, out);
  8605. }
  8606. #if !(FIPS_VERSION_EQ(2,0) && defined(WOLFSSL_ARMASM))
  8607. if (XMEMCMP(cipher, testVec[i].cipher, testVec[i].len)) {
  8608. ERROR_OUT(-5935 - i * 10, out);
  8609. }
  8610. #endif
  8611. }
  8612. out:
  8613. return ret;
  8614. }
  8615. #endif /* WOLFSSL_AES_COUNTER */
  8616. WOLFSSL_TEST_SUBROUTINE int aes_test(void)
  8617. {
  8618. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  8619. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8620. Aes *enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8621. #else
  8622. Aes enc[1];
  8623. #endif
  8624. byte cipher[AES_BLOCK_SIZE * 4];
  8625. #ifdef HAVE_AES_DECRYPT
  8626. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8627. Aes *dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8628. #else
  8629. Aes dec[1];
  8630. #endif
  8631. byte plain [AES_BLOCK_SIZE * 4];
  8632. #endif /* HAVE_AES_DECRYPT */
  8633. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER || WOLFSSL_AES_DIRECT */
  8634. int ret = 0;
  8635. #ifdef HAVE_AES_CBC
  8636. #ifdef WOLFSSL_AES_128
  8637. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  8638. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  8639. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  8640. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  8641. };
  8642. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  8643. {
  8644. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  8645. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  8646. };
  8647. WOLFSSL_SMALL_STACK_STATIC const byte key[] = "0123456789abcdef "; /* align */
  8648. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = "1234567890abcdef "; /* align */
  8649. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8650. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  8651. if (enc == NULL)
  8652. ERROR_OUT(-5990, out);
  8653. #endif
  8654. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  8655. if (dec == NULL)
  8656. ERROR_OUT(-5991, out);
  8657. #endif
  8658. #endif
  8659. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  8660. ERROR_OUT(-5900, out); /* note this error code is used programmatically in cleanup. */
  8661. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  8662. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  8663. ERROR_OUT(-5901, out); /* note this error code is used programmatically in cleanup. */
  8664. #endif
  8665. ret = wc_AesSetKey(enc, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8666. if (ret != 0)
  8667. ERROR_OUT(-5902, out);
  8668. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  8669. ret = wc_AesSetKey(dec, key, AES_BLOCK_SIZE, iv, AES_DECRYPTION);
  8670. if (ret != 0)
  8671. ERROR_OUT(-5903, out);
  8672. #endif
  8673. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 4);
  8674. ret = wc_AesCbcEncrypt(enc, cipher, msg, AES_BLOCK_SIZE);
  8675. #if defined(WOLFSSL_ASYNC_CRYPT)
  8676. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8677. #endif
  8678. if (ret != 0)
  8679. ERROR_OUT(-5904, out);
  8680. #ifdef HAVE_AES_DECRYPT
  8681. XMEMSET(plain, 0, AES_BLOCK_SIZE * 4);
  8682. ret = wc_AesCbcDecrypt(dec, plain, cipher, AES_BLOCK_SIZE);
  8683. #if defined(WOLFSSL_ASYNC_CRYPT)
  8684. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8685. #endif
  8686. if (ret != 0)
  8687. ERROR_OUT(-5905, out);
  8688. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  8689. ERROR_OUT(-5906, out);
  8690. #endif /* HAVE_AES_DECRYPT */
  8691. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  8692. ERROR_OUT(-5907, out);
  8693. #endif /* WOLFSSL_AES_128 */
  8694. #if defined(WOLFSSL_AESNI) && defined(HAVE_AES_DECRYPT)
  8695. {
  8696. WOLFSSL_SMALL_STACK_STATIC const byte bigMsg[] = {
  8697. /* "All work and no play makes Jack a dull boy. " */
  8698. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  8699. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  8700. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  8701. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  8702. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  8703. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  8704. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  8705. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  8706. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  8707. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  8708. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  8709. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  8710. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  8711. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  8712. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  8713. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  8714. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  8715. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  8716. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  8717. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  8718. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  8719. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  8720. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  8721. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  8722. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  8723. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  8724. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  8725. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  8726. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  8727. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  8728. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  8729. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  8730. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  8731. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  8732. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  8733. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  8734. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  8735. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  8736. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  8737. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  8738. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  8739. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  8740. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  8741. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  8742. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  8743. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  8744. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  8745. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20
  8746. };
  8747. WOLFSSL_SMALL_STACK_STATIC const byte bigKey[] = "0123456789abcdeffedcba9876543210";
  8748. word32 keySz, msgSz;
  8749. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8750. byte *bigCipher = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8751. byte *bigPlain = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8752. if ((bigCipher == NULL) ||
  8753. (bigPlain == NULL)) {
  8754. if (bigCipher != NULL)
  8755. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8756. ERROR_OUT(-5992, out);
  8757. }
  8758. #else
  8759. byte bigCipher[sizeof(bigMsg)];
  8760. byte bigPlain[sizeof(bigMsg)];
  8761. #endif
  8762. /* Iterate from one AES_BLOCK_SIZE of bigMsg through the whole
  8763. * message by AES_BLOCK_SIZE for each size of AES key. */
  8764. for (keySz = 16; keySz <= 32; keySz += 8) {
  8765. for (msgSz = AES_BLOCK_SIZE;
  8766. msgSz <= sizeof(bigMsg);
  8767. msgSz += AES_BLOCK_SIZE) {
  8768. XMEMSET(bigCipher, 0, sizeof(bigMsg));
  8769. XMEMSET(bigPlain, 0, sizeof(bigMsg));
  8770. ret = wc_AesSetKey(enc, bigKey, keySz, iv, AES_ENCRYPTION);
  8771. if (ret != 0) {
  8772. ret = -5908;
  8773. break;
  8774. }
  8775. ret = wc_AesSetKey(dec, bigKey, keySz, iv, AES_DECRYPTION);
  8776. if (ret != 0) {
  8777. ret = -5909;
  8778. break;
  8779. }
  8780. ret = wc_AesCbcEncrypt(enc, bigCipher, bigMsg, msgSz);
  8781. #if defined(WOLFSSL_ASYNC_CRYPT)
  8782. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8783. #endif
  8784. if (ret != 0) {
  8785. ret = -5910;
  8786. break;
  8787. }
  8788. ret = wc_AesCbcDecrypt(dec, bigPlain, bigCipher, msgSz);
  8789. #if defined(WOLFSSL_ASYNC_CRYPT)
  8790. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8791. #endif
  8792. if (ret != 0) {
  8793. ret = -5911;
  8794. break;
  8795. }
  8796. if (XMEMCMP(bigPlain, bigMsg, msgSz)) {
  8797. ret = -5912;
  8798. break;
  8799. }
  8800. }
  8801. if (ret != 0)
  8802. break;
  8803. }
  8804. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8805. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8806. XFREE(bigPlain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8807. #endif
  8808. if (ret != 0)
  8809. goto out;
  8810. }
  8811. #endif /* WOLFSSL_AESNI && HAVE_AES_DECRYPT */
  8812. /* Test of AES IV state with encrypt/decrypt */
  8813. #ifdef WOLFSSL_AES_128
  8814. {
  8815. /* Test Vector from "NIST Special Publication 800-38A, 2001 Edition"
  8816. * https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38a.pdf
  8817. */
  8818. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  8819. {
  8820. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  8821. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  8822. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  8823. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51
  8824. };
  8825. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] =
  8826. {
  8827. 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46,
  8828. 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
  8829. 0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee,
  8830. 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2
  8831. };
  8832. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  8833. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  8834. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  8835. };
  8836. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  8837. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  8838. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  8839. };
  8840. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  8841. if (ret != 0)
  8842. ERROR_OUT(-5913, out);
  8843. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 2);
  8844. ret = wc_AesCbcEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE);
  8845. #if defined(WOLFSSL_ASYNC_CRYPT)
  8846. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8847. #endif
  8848. if (ret != 0)
  8849. ERROR_OUT(-5914, out);
  8850. if (XMEMCMP(cipher, verify2, AES_BLOCK_SIZE))
  8851. ERROR_OUT(-5915, out);
  8852. ret = wc_AesCbcEncrypt(enc, cipher + AES_BLOCK_SIZE,
  8853. msg2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  8854. #if defined(WOLFSSL_ASYNC_CRYPT)
  8855. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8856. #endif
  8857. if (ret != 0)
  8858. ERROR_OUT(-5916, out);
  8859. if (XMEMCMP(cipher + AES_BLOCK_SIZE, verify2 + AES_BLOCK_SIZE,
  8860. AES_BLOCK_SIZE))
  8861. ERROR_OUT(-5917, out);
  8862. #if defined(HAVE_AES_DECRYPT)
  8863. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_DECRYPTION);
  8864. if (ret != 0)
  8865. ERROR_OUT(-5918, out);
  8866. XMEMSET(plain, 0, AES_BLOCK_SIZE * 2);
  8867. ret = wc_AesCbcDecrypt(dec, plain, verify2, AES_BLOCK_SIZE);
  8868. #if defined(WOLFSSL_ASYNC_CRYPT)
  8869. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8870. #endif
  8871. if (ret != 0)
  8872. ERROR_OUT(-5919, out);
  8873. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE))
  8874. ERROR_OUT(-5920, out);
  8875. ret = wc_AesCbcDecrypt(dec, plain + AES_BLOCK_SIZE,
  8876. verify2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  8877. #if defined(WOLFSSL_ASYNC_CRYPT)
  8878. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8879. #endif
  8880. if (ret != 0)
  8881. ERROR_OUT(-5921, out);
  8882. if (XMEMCMP(plain + AES_BLOCK_SIZE, msg2 + AES_BLOCK_SIZE,
  8883. AES_BLOCK_SIZE))
  8884. ERROR_OUT(-5922, out);
  8885. #endif /* HAVE_AES_DECRYPT */
  8886. }
  8887. #endif /* WOLFSSL_AES_128 */
  8888. #endif /* HAVE_AES_CBC */
  8889. #ifdef WOLFSSL_AES_COUNTER
  8890. ret = aesctr_test(enc, dec, cipher, plain);
  8891. if (ret != 0)
  8892. return ret;
  8893. #endif
  8894. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  8895. {
  8896. WOLFSSL_SMALL_STACK_STATIC const byte niPlain[] =
  8897. {
  8898. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8899. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  8900. };
  8901. WOLFSSL_SMALL_STACK_STATIC const byte niCipher[] =
  8902. {
  8903. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  8904. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  8905. };
  8906. WOLFSSL_SMALL_STACK_STATIC const byte niKey[] =
  8907. {
  8908. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  8909. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  8910. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  8911. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  8912. };
  8913. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  8914. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  8915. if (ret != 0)
  8916. ERROR_OUT(-5994, out);
  8917. #if !defined(HAVE_SELFTEST) && \
  8918. (defined(WOLFSSL_LINUXKM) || \
  8919. !defined(HAVE_FIPS) || \
  8920. (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  8921. if (wc_AesEncryptDirect(enc, cipher, niPlain) != 0)
  8922. ERROR_OUT(-5995, out);
  8923. #else
  8924. wc_AesEncryptDirect(enc, cipher, niPlain);
  8925. #endif
  8926. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  8927. ERROR_OUT(-5996, out);
  8928. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  8929. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  8930. if (ret != 0)
  8931. ERROR_OUT(-5997, out);
  8932. #if !defined(HAVE_SELFTEST) && \
  8933. (defined(WOLFSSL_LINUXKM) || \
  8934. !defined(HAVE_FIPS) || \
  8935. (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  8936. if (wc_AesDecryptDirect(dec, plain, niCipher) != 0)
  8937. ERROR_OUT(-5998, out);
  8938. #else
  8939. wc_AesDecryptDirect(dec, plain, niCipher);
  8940. #endif
  8941. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  8942. ERROR_OUT(-5999, out);
  8943. }
  8944. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  8945. ret = aes_key_size_test();
  8946. if (ret != 0)
  8947. goto out;
  8948. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  8949. ret = aes_cbc_test();
  8950. if (ret != 0)
  8951. goto out;
  8952. #endif
  8953. #if defined(WOLFSSL_AES_XTS)
  8954. #ifdef WOLFSSL_AES_128
  8955. ret = aes_xts_128_test();
  8956. if (ret != 0)
  8957. goto out;
  8958. #endif
  8959. #ifdef WOLFSSL_AES_256
  8960. ret = aes_xts_256_test();
  8961. if (ret != 0)
  8962. goto out;
  8963. #endif
  8964. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  8965. ret = aes_xts_sector_test();
  8966. if (ret != 0)
  8967. goto out;
  8968. #endif
  8969. #ifdef WOLFSSL_AES_128
  8970. ret = aes_xts_args_test();
  8971. if (ret != 0)
  8972. goto out;
  8973. #endif
  8974. #endif
  8975. #if defined(WOLFSSL_AES_CFB)
  8976. ret = aescfb_test();
  8977. if (ret != 0)
  8978. goto out;
  8979. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  8980. ret = aescfb1_test();
  8981. if (ret != 0)
  8982. goto out;
  8983. ret = aescfb8_test();
  8984. if (ret != 0)
  8985. goto out;
  8986. #endif
  8987. #endif
  8988. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  8989. ret = aesecb_test();
  8990. if (ret != 0)
  8991. goto out;
  8992. #endif
  8993. out:
  8994. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  8995. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8996. if (enc) {
  8997. if (ret != -5900) /* note this must match ERRROR_OUT() code
  8998. * for wc_AesInit(enc, ...) failure above.
  8999. */
  9000. wc_AesFree(enc);
  9001. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9002. }
  9003. #else
  9004. if (ret != -5900)
  9005. wc_AesFree(enc);
  9006. #endif
  9007. (void)cipher;
  9008. #ifdef HAVE_AES_DECRYPT
  9009. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9010. if (dec) {
  9011. if ((ret != -5900) && (ret != -5901))
  9012. /* note these codes must match the ERRROR_OUT() codes for
  9013. * wc_AesInit() failures above.
  9014. */
  9015. wc_AesFree(dec);
  9016. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9017. }
  9018. #else
  9019. if ((ret != -5900) && (ret != -5901))
  9020. wc_AesFree(dec);
  9021. #endif
  9022. (void)plain;
  9023. #endif /* HAVE_AES_DECRYPT */
  9024. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER || WOLFSSL_AES_DIRECT */
  9025. return ret;
  9026. }
  9027. #ifdef WOLFSSL_AES_192
  9028. WOLFSSL_TEST_SUBROUTINE int aes192_test(void)
  9029. {
  9030. #ifdef HAVE_AES_CBC
  9031. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9032. Aes *enc = NULL;
  9033. #else
  9034. Aes enc[1];
  9035. #endif
  9036. byte cipher[AES_BLOCK_SIZE];
  9037. #ifdef HAVE_AES_DECRYPT
  9038. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9039. Aes *dec = NULL;
  9040. #else
  9041. Aes dec[1];
  9042. #endif
  9043. byte plain[AES_BLOCK_SIZE];
  9044. #endif
  9045. #endif /* HAVE_AES_CBC */
  9046. int ret = 0;
  9047. #ifdef HAVE_AES_CBC
  9048. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  9049. * Appendix F.2.3 */
  9050. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  9051. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  9052. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  9053. };
  9054. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  9055. {
  9056. 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  9057. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8
  9058. };
  9059. WOLFSSL_SMALL_STACK_STATIC byte key[] = {
  9060. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  9061. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  9062. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  9063. };
  9064. WOLFSSL_SMALL_STACK_STATIC byte iv[] = {
  9065. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  9066. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  9067. };
  9068. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9069. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9070. ERROR_OUT(-6008, out);
  9071. #ifdef HAVE_AES_DECRYPT
  9072. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9073. ERROR_OUT(-6009, out);
  9074. #endif
  9075. #endif
  9076. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  9077. ERROR_OUT(-6000, out);
  9078. #ifdef HAVE_AES_DECRYPT
  9079. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  9080. ERROR_OUT(-6001, out);
  9081. #endif
  9082. ret = wc_AesSetKey(enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  9083. if (ret != 0)
  9084. ERROR_OUT(-6002, out);
  9085. #ifdef HAVE_AES_DECRYPT
  9086. ret = wc_AesSetKey(dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  9087. if (ret != 0)
  9088. ERROR_OUT(-6003, out);
  9089. #endif
  9090. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  9091. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  9092. #if defined(WOLFSSL_ASYNC_CRYPT)
  9093. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9094. #endif
  9095. if (ret != 0)
  9096. ERROR_OUT(-6004, out);
  9097. #ifdef HAVE_AES_DECRYPT
  9098. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  9099. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  9100. #if defined(WOLFSSL_ASYNC_CRYPT)
  9101. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9102. #endif
  9103. if (ret != 0)
  9104. ERROR_OUT(-6005, out);
  9105. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  9106. ERROR_OUT(-6006, out);
  9107. }
  9108. #endif
  9109. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  9110. ERROR_OUT(-6007, out);
  9111. wc_AesFree(enc);
  9112. #ifdef HAVE_AES_DECRYPT
  9113. wc_AesFree(dec);
  9114. #endif
  9115. out:
  9116. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9117. if (enc)
  9118. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9119. #ifdef HAVE_AES_DECRYPT
  9120. if (dec)
  9121. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9122. #endif
  9123. #endif
  9124. #endif /* HAVE_AES_CBC */
  9125. return ret;
  9126. }
  9127. #endif /* WOLFSSL_AES_192 */
  9128. #ifdef WOLFSSL_AES_256
  9129. WOLFSSL_TEST_SUBROUTINE int aes256_test(void)
  9130. {
  9131. #ifdef HAVE_AES_CBC
  9132. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9133. Aes *enc = NULL;
  9134. #else
  9135. Aes enc[1];
  9136. #endif
  9137. byte cipher[AES_BLOCK_SIZE];
  9138. #ifdef HAVE_AES_DECRYPT
  9139. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9140. Aes *dec = NULL;
  9141. #else
  9142. Aes dec[1];
  9143. #endif
  9144. byte plain[AES_BLOCK_SIZE];
  9145. #endif
  9146. #endif /* HAVE_AES_CBC */
  9147. int ret = 0;
  9148. #ifdef HAVE_AES_CBC
  9149. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  9150. * Appendix F.2.5 */
  9151. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  9152. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  9153. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  9154. };
  9155. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  9156. {
  9157. 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  9158. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6
  9159. };
  9160. WOLFSSL_SMALL_STACK_STATIC byte key[] = {
  9161. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  9162. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  9163. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  9164. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  9165. };
  9166. WOLFSSL_SMALL_STACK_STATIC byte iv[] = {
  9167. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  9168. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  9169. };
  9170. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9171. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9172. ERROR_OUT(-6108, out);
  9173. #ifdef HAVE_AES_DECRYPT
  9174. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9175. ERROR_OUT(-6109, out);
  9176. #endif
  9177. #endif
  9178. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  9179. ERROR_OUT(-6100, out);
  9180. #ifdef HAVE_AES_DECRYPT
  9181. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  9182. ERROR_OUT(-6101, out);
  9183. #endif
  9184. ret = wc_AesSetKey(enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  9185. if (ret != 0)
  9186. ERROR_OUT(-6102, out);
  9187. #ifdef HAVE_AES_DECRYPT
  9188. ret = wc_AesSetKey(dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  9189. if (ret != 0)
  9190. ERROR_OUT(-6103, out);
  9191. #endif
  9192. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  9193. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  9194. #if defined(WOLFSSL_ASYNC_CRYPT)
  9195. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9196. #endif
  9197. if (ret != 0)
  9198. ERROR_OUT(-6104, out);
  9199. #ifdef HAVE_AES_DECRYPT
  9200. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  9201. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  9202. #if defined(WOLFSSL_ASYNC_CRYPT)
  9203. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9204. #endif
  9205. if (ret != 0)
  9206. ERROR_OUT(-6105, out);
  9207. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  9208. ERROR_OUT(-6106, out);
  9209. }
  9210. #endif
  9211. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  9212. ERROR_OUT(-6107, out);
  9213. wc_AesFree(enc);
  9214. #ifdef HAVE_AES_DECRYPT
  9215. wc_AesFree(dec);
  9216. #endif
  9217. out:
  9218. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9219. if (enc)
  9220. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9221. #ifdef HAVE_AES_DECRYPT
  9222. if (dec)
  9223. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9224. #endif
  9225. #endif
  9226. #endif /* HAVE_AES_CBC */
  9227. return ret;
  9228. }
  9229. #endif /* WOLFSSL_AES_256 */
  9230. #ifdef HAVE_AESGCM
  9231. #ifdef WOLFSSL_AES_128
  9232. static int aesgcm_default_test_helper(byte* key, int keySz, byte* iv, int ivSz,
  9233. byte* plain, int plainSz, byte* cipher, int cipherSz,
  9234. byte* aad, int aadSz, byte* tag, int tagSz)
  9235. {
  9236. int ret, enc_inited = 0, dec_inited = 0;
  9237. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9238. Aes *enc = NULL;
  9239. Aes *dec = NULL;
  9240. #else
  9241. Aes enc[1];
  9242. Aes dec[1];
  9243. #endif
  9244. byte resultT[AES_BLOCK_SIZE];
  9245. byte resultP[AES_BLOCK_SIZE * 3];
  9246. byte resultC[AES_BLOCK_SIZE * 3];
  9247. int result;
  9248. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9249. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9250. ERROR_OUT(-6118, out);
  9251. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9252. ERROR_OUT(-6119, out);
  9253. #endif
  9254. XMEMSET(resultT, 0, sizeof(resultT));
  9255. XMEMSET(resultC, 0, sizeof(resultC));
  9256. XMEMSET(resultP, 0, sizeof(resultP));
  9257. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  9258. ERROR_OUT(-6110, out);
  9259. else
  9260. enc_inited = 1;
  9261. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  9262. ERROR_OUT(-6111, out);
  9263. else
  9264. dec_inited = 1;
  9265. result = wc_AesGcmSetKey(enc, key, keySz);
  9266. if (result != 0)
  9267. ERROR_OUT(-6112, out);
  9268. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9269. result = wc_AesGcmEncrypt(enc, resultC, plain, plainSz, iv, ivSz,
  9270. resultT, tagSz, aad, aadSz);
  9271. #if defined(WOLFSSL_ASYNC_CRYPT)
  9272. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9273. #endif
  9274. if (result != 0)
  9275. ERROR_OUT(-6113, out);
  9276. if (cipher != NULL) {
  9277. if (XMEMCMP(cipher, resultC, cipherSz))
  9278. ERROR_OUT(-6114, out);
  9279. }
  9280. if (XMEMCMP(tag, resultT, tagSz))
  9281. ERROR_OUT(-6115, out);
  9282. #ifdef HAVE_AES_DECRYPT
  9283. result = wc_AesGcmSetKey(dec, key, keySz);
  9284. if (result != 0)
  9285. ERROR_OUT(-6116, out);
  9286. result = wc_AesGcmDecrypt(dec, resultP, resultC, cipherSz,
  9287. iv, ivSz, resultT, tagSz, aad, aadSz);
  9288. #if defined(WOLFSSL_ASYNC_CRYPT)
  9289. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9290. #endif
  9291. if (result != 0)
  9292. ERROR_OUT(-6117, out);
  9293. if (plain != NULL) {
  9294. if (XMEMCMP(plain, resultP, plainSz))
  9295. ERROR_OUT(-6118, out);
  9296. }
  9297. #endif /* HAVE_AES_DECRYPT */
  9298. ret = 0;
  9299. out:
  9300. if (enc_inited)
  9301. wc_AesFree(enc);
  9302. if (dec_inited)
  9303. wc_AesFree(dec);
  9304. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9305. if (enc)
  9306. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9307. if (dec)
  9308. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9309. #endif
  9310. return ret;
  9311. }
  9312. #endif
  9313. /* tests that only use 12 byte IV and 16 or less byte AAD
  9314. * test vectors are from NIST SP 800-38D
  9315. * https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/CAVP-TESTING-BLOCK-CIPHER-MODES*/
  9316. WOLFSSL_TEST_SUBROUTINE int aesgcm_default_test(void)
  9317. {
  9318. #ifdef WOLFSSL_AES_128
  9319. byte key1[] = {
  9320. 0x29, 0x8e, 0xfa, 0x1c, 0xcf, 0x29, 0xcf, 0x62,
  9321. 0xae, 0x68, 0x24, 0xbf, 0xc1, 0x95, 0x57, 0xfc
  9322. };
  9323. byte iv1[] = {
  9324. 0x6f, 0x58, 0xa9, 0x3f, 0xe1, 0xd2, 0x07, 0xfa,
  9325. 0xe4, 0xed, 0x2f, 0x6d
  9326. };
  9327. ALIGN64 byte plain1[] = {
  9328. 0xcc, 0x38, 0xbc, 0xcd, 0x6b, 0xc5, 0x36, 0xad,
  9329. 0x91, 0x9b, 0x13, 0x95, 0xf5, 0xd6, 0x38, 0x01,
  9330. 0xf9, 0x9f, 0x80, 0x68, 0xd6, 0x5c, 0xa5, 0xac,
  9331. 0x63, 0x87, 0x2d, 0xaf, 0x16, 0xb9, 0x39, 0x01
  9332. };
  9333. byte aad1[] = {
  9334. 0x02, 0x1f, 0xaf, 0xd2, 0x38, 0x46, 0x39, 0x73,
  9335. 0xff, 0xe8, 0x02, 0x56, 0xe5, 0xb1, 0xc6, 0xb1
  9336. };
  9337. ALIGN64 byte cipher1[] = {
  9338. 0xdf, 0xce, 0x4e, 0x9c, 0xd2, 0x91, 0x10, 0x3d,
  9339. 0x7f, 0xe4, 0xe6, 0x33, 0x51, 0xd9, 0xe7, 0x9d,
  9340. 0x3d, 0xfd, 0x39, 0x1e, 0x32, 0x67, 0x10, 0x46,
  9341. 0x58, 0x21, 0x2d, 0xa9, 0x65, 0x21, 0xb7, 0xdb
  9342. };
  9343. byte tag1[] = {
  9344. 0x54, 0x24, 0x65, 0xef, 0x59, 0x93, 0x16, 0xf7,
  9345. 0x3a, 0x7a, 0x56, 0x05, 0x09, 0xa2, 0xd9, 0xf2
  9346. };
  9347. byte key2[] = {
  9348. 0x01, 0x6d, 0xbb, 0x38, 0xda, 0xa7, 0x6d, 0xfe,
  9349. 0x7d, 0xa3, 0x84, 0xeb, 0xf1, 0x24, 0x03, 0x64
  9350. };
  9351. byte iv2[] = {
  9352. 0x07, 0x93, 0xef, 0x3a, 0xda, 0x78, 0x2f, 0x78,
  9353. 0xc9, 0x8a, 0xff, 0xe3
  9354. };
  9355. ALIGN64 byte plain2[] = {
  9356. 0x4b, 0x34, 0xa9, 0xec, 0x57, 0x63, 0x52, 0x4b,
  9357. 0x19, 0x1d, 0x56, 0x16, 0xc5, 0x47, 0xf6, 0xb7
  9358. };
  9359. ALIGN64 byte cipher2[] = {
  9360. 0x60, 0x9a, 0xa3, 0xf4, 0x54, 0x1b, 0xc0, 0xfe,
  9361. 0x99, 0x31, 0xda, 0xad, 0x2e, 0xe1, 0x5d, 0x0c
  9362. };
  9363. byte tag2[] = {
  9364. 0x33, 0xaf, 0xec, 0x59, 0xc4, 0x5b, 0xaf, 0x68,
  9365. 0x9a, 0x5e, 0x1b, 0x13, 0xae, 0x42, 0x36, 0x19
  9366. };
  9367. byte key3[] = {
  9368. 0xb0, 0x1e, 0x45, 0xcc, 0x30, 0x88, 0xaa, 0xba,
  9369. 0x9f, 0xa4, 0x3d, 0x81, 0xd4, 0x81, 0x82, 0x3f
  9370. };
  9371. byte iv3[] = {
  9372. 0x5a, 0x2c, 0x4a, 0x66, 0x46, 0x87, 0x13, 0x45,
  9373. 0x6a, 0x4b, 0xd5, 0xe1
  9374. };
  9375. byte tag3[] = {
  9376. 0x01, 0x42, 0x80, 0xf9, 0x44, 0xf5, 0x3c, 0x68,
  9377. 0x11, 0x64, 0xb2, 0xff
  9378. };
  9379. int ret;
  9380. ret = aesgcm_default_test_helper(key1, sizeof(key1), iv1, sizeof(iv1),
  9381. plain1, sizeof(plain1), cipher1, sizeof(cipher1),
  9382. aad1, sizeof(aad1), tag1, sizeof(tag1));
  9383. if (ret != 0) {
  9384. return ret;
  9385. }
  9386. ret = aesgcm_default_test_helper(key2, sizeof(key2), iv2, sizeof(iv2),
  9387. plain2, sizeof(plain2), cipher2, sizeof(cipher2),
  9388. NULL, 0, tag2, sizeof(tag2));
  9389. if (ret != 0) {
  9390. return ret;
  9391. }
  9392. ret = aesgcm_default_test_helper(key3, sizeof(key3), iv3, sizeof(iv3),
  9393. NULL, 0, NULL, 0,
  9394. NULL, 0, tag3, sizeof(tag3));
  9395. if (ret != 0) {
  9396. return ret;
  9397. }
  9398. #endif
  9399. return 0;
  9400. }
  9401. WOLFSSL_TEST_SUBROUTINE int aesgcm_test(void)
  9402. {
  9403. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9404. Aes *enc = NULL;
  9405. Aes *dec = NULL;
  9406. #else
  9407. Aes enc[1];
  9408. Aes dec[1];
  9409. #endif
  9410. /*
  9411. * This is Test Case 16 from the document Galois/
  9412. * Counter Mode of Operation (GCM) by McGrew and
  9413. * Viega.
  9414. */
  9415. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  9416. {
  9417. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  9418. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  9419. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  9420. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  9421. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  9422. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  9423. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  9424. 0xba, 0x63, 0x7b, 0x39
  9425. };
  9426. #if defined(WOLFSSL_AES_256) || defined(WOLFSSL_AES_192)
  9427. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  9428. {
  9429. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  9430. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  9431. 0xab, 0xad, 0xda, 0xd2
  9432. };
  9433. #endif
  9434. #ifdef WOLFSSL_AES_256
  9435. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  9436. {
  9437. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  9438. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  9439. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  9440. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08
  9441. };
  9442. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  9443. {
  9444. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
  9445. 0xde, 0xca, 0xf8, 0x88
  9446. };
  9447. #endif /* WOLFSSL_AES_256 */
  9448. #if defined(WOLFSSL_AES_256) || defined(WOLFSSL_AES_192)
  9449. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  9450. {
  9451. 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
  9452. 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
  9453. 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  9454. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
  9455. 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
  9456. 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  9457. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
  9458. 0xbc, 0xc9, 0xf6, 0x62
  9459. };
  9460. #endif /* WOLFSSL_AES_256 || WOLFSSL_AES_192 */
  9461. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  9462. {
  9463. 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68,
  9464. 0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b
  9465. };
  9466. /* FIPS, QAT and PIC32MZ HW Crypto only support 12-byte IV */
  9467. #if !defined(HAVE_FIPS) && \
  9468. !defined(WOLFSSL_PIC32MZ_CRYPT) && \
  9469. !defined(FREESCALE_LTC) && !defined(FREESCALE_MMCAU) && \
  9470. !defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  9471. !defined(WOLFSSL_SILABS_SE_ACCEL) && !defined(WOLFSSL_KCAPI_AES) && \
  9472. !(defined(WOLF_CRYPTO_CB) && \
  9473. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  9474. #define ENABLE_NON_12BYTE_IV_TEST
  9475. #ifdef WOLFSSL_AES_192
  9476. /* Test Case 12, uses same plaintext and AAD data. */
  9477. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  9478. {
  9479. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  9480. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  9481. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c
  9482. };
  9483. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  9484. {
  9485. 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5,
  9486. 0x55, 0x90, 0x9c, 0x5a, 0xff, 0x52, 0x69, 0xaa,
  9487. 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1,
  9488. 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28,
  9489. 0xc3, 0xc0, 0xc9, 0x51, 0x56, 0x80, 0x95, 0x39,
  9490. 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54,
  9491. 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57,
  9492. 0xa6, 0x37, 0xb3, 0x9b
  9493. };
  9494. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  9495. {
  9496. 0xd2, 0x7e, 0x88, 0x68, 0x1c, 0xe3, 0x24, 0x3c,
  9497. 0x48, 0x30, 0x16, 0x5a, 0x8f, 0xdc, 0xf9, 0xff,
  9498. 0x1d, 0xe9, 0xa1, 0xd8, 0xe6, 0xb4, 0x47, 0xef,
  9499. 0x6e, 0xf7, 0xb7, 0x98, 0x28, 0x66, 0x6e, 0x45,
  9500. 0x81, 0xe7, 0x90, 0x12, 0xaf, 0x34, 0xdd, 0xd9,
  9501. 0xe2, 0xf0, 0x37, 0x58, 0x9b, 0x29, 0x2d, 0xb3,
  9502. 0xe6, 0x7c, 0x03, 0x67, 0x45, 0xfa, 0x22, 0xe7,
  9503. 0xe9, 0xb7, 0x37, 0x3b
  9504. };
  9505. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  9506. {
  9507. 0xdc, 0xf5, 0x66, 0xff, 0x29, 0x1c, 0x25, 0xbb,
  9508. 0xb8, 0x56, 0x8f, 0xc3, 0xd3, 0x76, 0xa6, 0xd9
  9509. };
  9510. #endif /* WOLFSSL_AES_192 */
  9511. #ifdef WOLFSSL_AES_128
  9512. /* The following is an interesting test case from the example
  9513. * FIPS test vectors for AES-GCM. IVlen = 1 byte */
  9514. WOLFSSL_SMALL_STACK_STATIC const byte p3[] =
  9515. {
  9516. 0x57, 0xce, 0x45, 0x1f, 0xa5, 0xe2, 0x35, 0xa5,
  9517. 0x8e, 0x1a, 0xa2, 0x3b, 0x77, 0xcb, 0xaf, 0xe2
  9518. };
  9519. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  9520. {
  9521. 0xbb, 0x01, 0xd7, 0x03, 0x81, 0x1c, 0x10, 0x1a,
  9522. 0x35, 0xe0, 0xff, 0xd2, 0x91, 0xba, 0xf2, 0x4b
  9523. };
  9524. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  9525. {
  9526. 0xca
  9527. };
  9528. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  9529. {
  9530. 0x6b, 0x5f, 0xb3, 0x9d, 0xc1, 0xc5, 0x7a, 0x4f,
  9531. 0xf3, 0x51, 0x4d, 0xc2, 0xd5, 0xf0, 0xd0, 0x07
  9532. };
  9533. WOLFSSL_SMALL_STACK_STATIC const byte a3[] =
  9534. {
  9535. 0x40, 0xfc, 0xdc, 0xd7, 0x4a, 0xd7, 0x8b, 0xf1,
  9536. 0x3e, 0x7c, 0x60, 0x55, 0x50, 0x51, 0xdd, 0x54
  9537. };
  9538. WOLFSSL_SMALL_STACK_STATIC const byte t3[] =
  9539. {
  9540. 0x06, 0x90, 0xed, 0x01, 0x34, 0xdd, 0xc6, 0x95,
  9541. 0x31, 0x2e, 0x2a, 0xf9, 0x57, 0x7a, 0x1e, 0xa6
  9542. };
  9543. #endif /* WOLFSSL_AES_128 */
  9544. #ifdef WOLFSSL_AES_256
  9545. int ivlen;
  9546. #endif
  9547. #endif
  9548. byte resultT[sizeof(t1) + AES_BLOCK_SIZE];
  9549. byte resultP[sizeof(p) + AES_BLOCK_SIZE];
  9550. byte resultC[sizeof(p) + AES_BLOCK_SIZE];
  9551. int result = 0;
  9552. int ret;
  9553. #ifdef WOLFSSL_AES_256
  9554. #if !(defined(WOLF_CRYPTO_CB) && defined(HAVE_INTEL_QA_SYNC))
  9555. int alen;
  9556. #endif
  9557. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  9558. int plen;
  9559. #endif
  9560. #endif
  9561. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  9562. byte buf[sizeof(p) + AES_BLOCK_SIZE];
  9563. byte bufA[sizeof(a) + 1];
  9564. byte *large_aad = (byte*)XMALLOC((size_t)1024 + 16, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9565. #endif
  9566. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM)
  9567. #if !defined(BENCH_AESGCM_LARGE)
  9568. #define BENCH_AESGCM_LARGE 1024
  9569. #endif
  9570. #ifndef WOLFSSL_NO_MALLOC
  9571. byte *large_input = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9572. byte *large_output = (byte *)XMALLOC(BENCH_AESGCM_LARGE + AES_BLOCK_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9573. byte *large_outdec = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9574. if ((! large_input) || (! large_output) || (! large_outdec))
  9575. ERROR_OUT(MEMORY_E, out);
  9576. #else
  9577. byte large_input[BENCH_AESGCM_LARGE];
  9578. byte large_output[BENCH_AESGCM_LARGE];
  9579. byte large_outdec[BENCH_AESGCM_LARGE];
  9580. #endif
  9581. XMEMSET(large_input, 0, BENCH_AESGCM_LARGE);
  9582. XMEMSET(large_output, 0, BENCH_AESGCM_LARGE + AES_BLOCK_SIZE);
  9583. XMEMSET(large_outdec, 0, BENCH_AESGCM_LARGE);
  9584. #endif
  9585. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9586. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9587. ERROR_OUT(-6342, out);
  9588. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9589. ERROR_OUT(-6343, out);
  9590. #endif
  9591. (void)result;
  9592. XMEMSET(resultT, 0, sizeof(resultT));
  9593. XMEMSET(resultC, 0, sizeof(resultC));
  9594. XMEMSET(resultP, 0, sizeof(resultP));
  9595. if (wc_AesInit(enc, HEAP_HINT, devId) != 0) {
  9596. ERROR_OUT(-6300, out);
  9597. }
  9598. if (wc_AesInit(dec, HEAP_HINT, devId) != 0) {
  9599. ERROR_OUT(-6301, out);
  9600. }
  9601. #ifdef WOLFSSL_AES_256
  9602. result = wc_AesGcmSetKey(enc, k1, sizeof(k1));
  9603. if (result != 0)
  9604. ERROR_OUT(-6302, out);
  9605. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9606. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  9607. resultT, sizeof(t1), a, sizeof(a));
  9608. #if defined(WOLFSSL_ASYNC_CRYPT)
  9609. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9610. #endif
  9611. if (result != 0)
  9612. ERROR_OUT(-6303, out);
  9613. if (XMEMCMP(c1, resultC, sizeof(c1)))
  9614. ERROR_OUT(-6304, out);
  9615. if (XMEMCMP(t1, resultT, sizeof(t1)))
  9616. ERROR_OUT(-6305, out);
  9617. #ifdef HAVE_AES_DECRYPT
  9618. result = wc_AesGcmSetKey(dec, k1, sizeof(k1));
  9619. if (result != 0)
  9620. ERROR_OUT(-6306, out);
  9621. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1),
  9622. iv1, sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  9623. #if defined(WOLFSSL_ASYNC_CRYPT)
  9624. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9625. #endif
  9626. if (result != 0)
  9627. ERROR_OUT(-6307, out);
  9628. if (XMEMCMP(p, resultP, sizeof(p)))
  9629. ERROR_OUT(-6308, out);
  9630. #endif /* HAVE_AES_DECRYPT */
  9631. /* Large buffer test */
  9632. #ifdef BENCH_AESGCM_LARGE
  9633. /* setup test buffer */
  9634. for (alen=0; alen<BENCH_AESGCM_LARGE; alen++)
  9635. large_input[alen] = (byte)alen;
  9636. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9637. result = wc_AesGcmEncrypt(enc, large_output, large_input,
  9638. BENCH_AESGCM_LARGE, iv1, sizeof(iv1),
  9639. resultT, sizeof(t1), a, sizeof(a));
  9640. #if defined(WOLFSSL_ASYNC_CRYPT)
  9641. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9642. #endif
  9643. if (result != 0)
  9644. ERROR_OUT(-6309, out);
  9645. #ifdef HAVE_AES_DECRYPT
  9646. result = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  9647. BENCH_AESGCM_LARGE, iv1, sizeof(iv1), resultT,
  9648. sizeof(t1), a, sizeof(a));
  9649. #if defined(WOLFSSL_ASYNC_CRYPT)
  9650. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9651. #endif
  9652. if (result != 0)
  9653. ERROR_OUT(-6310, out);
  9654. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  9655. ERROR_OUT(-6311, out);
  9656. #endif /* HAVE_AES_DECRYPT */
  9657. #endif /* BENCH_AESGCM_LARGE */
  9658. #if defined(ENABLE_NON_12BYTE_IV_TEST) && defined(WOLFSSL_AES_256)
  9659. /* Variable IV length test */
  9660. for (ivlen=1; ivlen<(int)sizeof(k1); ivlen++) {
  9661. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9662. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), k1,
  9663. (word32)ivlen, resultT, sizeof(t1), a, sizeof(a));
  9664. #if defined(WOLFSSL_ASYNC_CRYPT)
  9665. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9666. #endif
  9667. if (result != 0)
  9668. ERROR_OUT(-6312, out);
  9669. #ifdef HAVE_AES_DECRYPT
  9670. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), k1,
  9671. (word32)ivlen, resultT, sizeof(t1), a, sizeof(a));
  9672. #if defined(WOLFSSL_ASYNC_CRYPT)
  9673. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9674. #endif
  9675. if (result != 0)
  9676. ERROR_OUT(-6313, out);
  9677. #endif /* HAVE_AES_DECRYPT */
  9678. }
  9679. #endif
  9680. #if !(defined(WOLF_CRYPTO_CB) && defined(HAVE_INTEL_QA_SYNC))
  9681. /* Variable authenticated data length test */
  9682. for (alen=0; alen<(int)sizeof(p); alen++) {
  9683. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9684. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1,
  9685. sizeof(iv1), resultT, sizeof(t1), p, (word32)alen);
  9686. #if defined(WOLFSSL_ASYNC_CRYPT)
  9687. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9688. #endif
  9689. if (result != 0)
  9690. ERROR_OUT(-6314, out);
  9691. #ifdef HAVE_AES_DECRYPT
  9692. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), iv1,
  9693. sizeof(iv1), resultT, sizeof(t1), p, (word32)alen);
  9694. #if defined(WOLFSSL_ASYNC_CRYPT)
  9695. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9696. #endif
  9697. if (result != 0)
  9698. ERROR_OUT(-6315, out);
  9699. #endif /* HAVE_AES_DECRYPT */
  9700. }
  9701. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  9702. if (! large_aad)
  9703. ERROR_OUT(MEMORY_E, out);
  9704. XMEMSET(large_aad, 0, 1024+16);
  9705. /* Variable authenticated data length test */
  9706. for (alen=0; alen<=1024; alen+=16) {
  9707. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9708. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1,
  9709. sizeof(iv1), resultT, sizeof(t1), large_aad, (word32)alen);
  9710. if (result != 0)
  9711. ERROR_OUT(-6316, out);
  9712. #ifdef HAVE_AES_DECRYPT
  9713. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), iv1,
  9714. sizeof(iv1), resultT, sizeof(t1), large_aad, (word32)alen);
  9715. if (result != 0)
  9716. ERROR_OUT(-6317, out);
  9717. #endif /* HAVE_AES_DECRYPT */
  9718. }
  9719. /* Test unaligned memory of all potential arguments */
  9720. result = wc_AesGcmSetKey(enc, k1, sizeof(k1));
  9721. if (result != 0)
  9722. ERROR_OUT(-6318, out);
  9723. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9724. XMEMCPY(&buf[1], p, sizeof(p));
  9725. XMEMCPY(&bufA[1], a, sizeof(a));
  9726. result = wc_AesGcmEncrypt(enc, &resultC[1], &buf[1], sizeof(p), iv1, sizeof(iv1),
  9727. &resultT[1], sizeof(t1), &bufA[1], sizeof(a));
  9728. if (result != 0)
  9729. ERROR_OUT(-6319, out);
  9730. if (XMEMCMP(c1, &resultC[1], sizeof(c1)))
  9731. ERROR_OUT(-6320, out);
  9732. if (XMEMCMP(t1, &resultT[1], sizeof(t1)))
  9733. ERROR_OUT(-6321, out);
  9734. #ifdef HAVE_AES_DECRYPT
  9735. result = wc_AesGcmSetKey(dec, k1, sizeof(k1));
  9736. if (result != 0)
  9737. ERROR_OUT(-6322, out);
  9738. result = wc_AesGcmDecrypt(dec, &resultP[1], &resultC[1], sizeof(c1),
  9739. iv1, sizeof(iv1), &resultT[1], sizeof(t1), &bufA[1], sizeof(a));
  9740. if (result != 0)
  9741. ERROR_OUT(-6323, out);
  9742. if (XMEMCMP(p, &resultP[1], sizeof(p)))
  9743. ERROR_OUT(-6324, out);
  9744. #endif /* HAVE_AES_DECRYPT */
  9745. #endif /* Xilinx Versal */
  9746. #endif
  9747. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  9748. #ifdef BENCH_AESGCM_LARGE
  9749. /* Variable plain text length test */
  9750. for (plen=1; plen<BENCH_AESGCM_LARGE; plen++) {
  9751. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9752. result = wc_AesGcmEncrypt(enc, large_output, large_input,
  9753. plen, iv1, sizeof(iv1), resultT,
  9754. sizeof(t1), a, sizeof(a));
  9755. #if defined(WOLFSSL_ASYNC_CRYPT)
  9756. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9757. #endif
  9758. if (result != 0)
  9759. ERROR_OUT(-6316, out);
  9760. #ifdef HAVE_AES_DECRYPT
  9761. result = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  9762. plen, iv1, sizeof(iv1), resultT,
  9763. sizeof(t1), a, sizeof(a));
  9764. #if defined(WOLFSSL_ASYNC_CRYPT)
  9765. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9766. #endif
  9767. if (result != 0)
  9768. ERROR_OUT(-6317, out);
  9769. #endif /* HAVE_AES_DECRYPT */
  9770. }
  9771. #else /* BENCH_AESGCM_LARGE */
  9772. /* Variable plain text length test */
  9773. for (plen=1; plen<(int)sizeof(p); plen++) {
  9774. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9775. result = wc_AesGcmEncrypt(enc, resultC, p, (word32)plen, iv1,
  9776. sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  9777. #if defined(WOLFSSL_ASYNC_CRYPT)
  9778. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9779. #endif
  9780. if (result != 0)
  9781. ERROR_OUT(-6318, out);
  9782. #ifdef HAVE_AES_DECRYPT
  9783. result = wc_AesGcmDecrypt(dec, resultP, resultC, (word32)plen, iv1,
  9784. sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  9785. #if defined(WOLFSSL_ASYNC_CRYPT)
  9786. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9787. #endif
  9788. if (result != 0)
  9789. ERROR_OUT(-6319, out);
  9790. #endif /* HAVE_AES_DECRYPT */
  9791. }
  9792. #endif /* BENCH_AESGCM_LARGE */
  9793. #endif
  9794. #endif /* WOLFSSL_AES_256 */
  9795. /* test with IV != 12 bytes */
  9796. #ifdef ENABLE_NON_12BYTE_IV_TEST
  9797. XMEMSET(resultT, 0, sizeof(resultT));
  9798. XMEMSET(resultC, 0, sizeof(resultC));
  9799. XMEMSET(resultP, 0, sizeof(resultP));
  9800. #ifdef WOLFSSL_AES_192
  9801. wc_AesGcmSetKey(enc, k2, sizeof(k2));
  9802. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9803. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv2, sizeof(iv2),
  9804. resultT, sizeof(t1), a, sizeof(a));
  9805. #if defined(WOLFSSL_ASYNC_CRYPT)
  9806. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9807. #endif
  9808. if (result != 0)
  9809. ERROR_OUT(-6320, out);
  9810. if (XMEMCMP(c2, resultC, sizeof(c2)))
  9811. ERROR_OUT(-6321, out);
  9812. if (XMEMCMP(t2, resultT, sizeof(t1)))
  9813. ERROR_OUT(-6322, out);
  9814. #ifdef HAVE_AES_DECRYPT
  9815. result = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c1),
  9816. iv2, sizeof(iv2), resultT, sizeof(t1), a, sizeof(a));
  9817. #if defined(WOLFSSL_ASYNC_CRYPT)
  9818. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9819. #endif
  9820. if (result != 0)
  9821. ERROR_OUT(-6323, out);
  9822. if (XMEMCMP(p, resultP, sizeof(p)))
  9823. ERROR_OUT(-6324, out);
  9824. #endif /* HAVE_AES_DECRYPT */
  9825. XMEMSET(resultT, 0, sizeof(resultT));
  9826. XMEMSET(resultC, 0, sizeof(resultC));
  9827. XMEMSET(resultP, 0, sizeof(resultP));
  9828. #endif /* WOLFSSL_AES_192 */
  9829. #ifdef WOLFSSL_AES_128
  9830. wc_AesGcmSetKey(enc, k3, sizeof(k3));
  9831. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9832. result = wc_AesGcmEncrypt(enc, resultC, p3, sizeof(p3), iv3, sizeof(iv3),
  9833. resultT, sizeof(t3), a3, sizeof(a3));
  9834. #if defined(WOLFSSL_ASYNC_CRYPT)
  9835. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9836. #endif
  9837. if (result != 0)
  9838. ERROR_OUT(-6325, out);
  9839. if (XMEMCMP(c3, resultC, sizeof(c3)))
  9840. ERROR_OUT(-6326, out);
  9841. if (XMEMCMP(t3, resultT, sizeof(t3)))
  9842. ERROR_OUT(-6327, out);
  9843. #ifdef HAVE_AES_DECRYPT
  9844. result = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c3),
  9845. iv3, sizeof(iv3), resultT, sizeof(t3), a3, sizeof(a3));
  9846. #if defined(WOLFSSL_ASYNC_CRYPT)
  9847. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9848. #endif
  9849. if (result != 0)
  9850. ERROR_OUT(-6328, out);
  9851. if (XMEMCMP(p3, resultP, sizeof(p3)))
  9852. ERROR_OUT(-6329, out);
  9853. #endif /* HAVE_AES_DECRYPT */
  9854. #endif /* WOLFSSL_AES_128 */
  9855. #endif /* ENABLE_NON_12BYTE_IV_TEST */
  9856. #if defined(WOLFSSL_AES_256) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  9857. !defined(WOLFSSL_XILINX_CRYPT) && \
  9858. !(defined(WOLF_CRYPTO_CB) && \
  9859. defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC))
  9860. XMEMSET(resultT, 0, sizeof(resultT));
  9861. XMEMSET(resultC, 0, sizeof(resultC));
  9862. XMEMSET(resultP, 0, sizeof(resultP));
  9863. wc_AesGcmSetKey(enc, k1, sizeof(k1));
  9864. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9865. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  9866. resultT + 1, sizeof(t1) - 1, a, sizeof(a));
  9867. #if defined(WOLFSSL_ASYNC_CRYPT)
  9868. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9869. #endif
  9870. if (result != 0)
  9871. ERROR_OUT(-6330, out);
  9872. if (XMEMCMP(c1, resultC, sizeof(c1)))
  9873. ERROR_OUT(-6331, out);
  9874. if (XMEMCMP(t1, resultT + 1, sizeof(t1) - 1))
  9875. ERROR_OUT(-6332, out);
  9876. #ifdef HAVE_AES_DECRYPT
  9877. result = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(p),
  9878. iv1, sizeof(iv1), resultT + 1, sizeof(t1) - 1, a, sizeof(a));
  9879. #if defined(WOLFSSL_ASYNC_CRYPT)
  9880. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9881. #endif
  9882. if (result != 0)
  9883. ERROR_OUT(-6333, out);
  9884. if (XMEMCMP(p, resultP, sizeof(p)))
  9885. ERROR_OUT(-6334, out);
  9886. #endif /* HAVE_AES_DECRYPT */
  9887. #endif /* WOLFSSL_AES_256 */
  9888. #if !defined(HAVE_FIPS) || \
  9889. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  9890. /* Test encrypt with internally generated IV */
  9891. #if defined(WOLFSSL_AES_256) && !(defined(WC_NO_RNG) || defined(HAVE_SELFTEST)) \
  9892. && !(defined(WOLF_CRYPTO_CB) && defined(HAVE_CAVIUM_OCTEON_SYNC))
  9893. {
  9894. WC_RNG rng;
  9895. byte randIV[12];
  9896. result = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  9897. if (result != 0)
  9898. ERROR_OUT(-6335, out);
  9899. XMEMSET(randIV, 0, sizeof(randIV));
  9900. XMEMSET(resultT, 0, sizeof(resultT));
  9901. XMEMSET(resultC, 0, sizeof(resultC));
  9902. XMEMSET(resultP, 0, sizeof(resultP));
  9903. wc_AesGcmSetKey(enc, k1, sizeof(k1));
  9904. result = wc_AesGcmSetIV(enc, sizeof(randIV), NULL, 0, &rng);
  9905. if (result != 0)
  9906. ERROR_OUT(-6336, out);
  9907. result = wc_AesGcmEncrypt_ex(enc,
  9908. resultC, p, sizeof(p),
  9909. randIV, sizeof(randIV),
  9910. resultT, sizeof(t1),
  9911. a, sizeof(a));
  9912. #if defined(WOLFSSL_ASYNC_CRYPT)
  9913. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9914. #endif
  9915. if (result != 0)
  9916. ERROR_OUT(-6337, out);
  9917. /* Check the IV has been set. */
  9918. {
  9919. word32 i, ivSum = 0;
  9920. for (i = 0; i < sizeof(randIV); i++)
  9921. ivSum += randIV[i];
  9922. if (ivSum == 0)
  9923. ERROR_OUT(-6338, out);
  9924. }
  9925. #ifdef HAVE_AES_DECRYPT
  9926. wc_AesGcmSetKey(dec, k1, sizeof(k1));
  9927. result = wc_AesGcmSetIV(dec, sizeof(randIV), NULL, 0, &rng);
  9928. if (result != 0)
  9929. ERROR_OUT(-6339, out);
  9930. result = wc_AesGcmDecrypt(dec,
  9931. resultP, resultC, sizeof(c1),
  9932. randIV, sizeof(randIV),
  9933. resultT, sizeof(t1),
  9934. a, sizeof(a));
  9935. #if defined(WOLFSSL_ASYNC_CRYPT)
  9936. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9937. #endif
  9938. if (result != 0)
  9939. ERROR_OUT(-6340, out);
  9940. if (XMEMCMP(p, resultP, sizeof(p)))
  9941. ERROR_OUT(-6341, out);
  9942. #endif /* HAVE_AES_DECRYPT */
  9943. wc_FreeRng(&rng);
  9944. }
  9945. #endif /* WOLFSSL_AES_256 && !(WC_NO_RNG || HAVE_SELFTEST) */
  9946. #endif /* HAVE_FIPS_VERSION >= 2 */
  9947. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  9948. #ifdef WOLFSSL_AES_256
  9949. #ifdef WOLFSSL_AESGCM_STREAM
  9950. result = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  9951. if (result != 0)
  9952. ERROR_OUT(-6360, out);
  9953. result = wc_AesGcmEncryptUpdate(enc, resultC, p, sizeof(p), a, sizeof(a));
  9954. if (result != 0)
  9955. ERROR_OUT(-6361, out);
  9956. result = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  9957. if (result != 0)
  9958. ERROR_OUT(-6362, out);
  9959. if (XMEMCMP(resultC, c1, sizeof(c1)) != 0)
  9960. ERROR_OUT(-6363, out);
  9961. if (XMEMCMP(resultT, t1, sizeof(t1)) != 0)
  9962. ERROR_OUT(-6364, out);
  9963. #ifdef HAVE_AES_DECRYPT
  9964. result = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  9965. if (result != 0)
  9966. ERROR_OUT(-6370, out);
  9967. result = wc_AesGcmDecryptUpdate(enc, resultP, c1, sizeof(c1), a, sizeof(a));
  9968. if (result != 0)
  9969. ERROR_OUT(-6371, out);
  9970. result = wc_AesGcmDecryptFinal(enc, t1, sizeof(t1));
  9971. if (result != 0)
  9972. ERROR_OUT(-6372, out);
  9973. if (XMEMCMP(resultP, p, sizeof(p)) != 0)
  9974. ERROR_OUT(-6373, out);
  9975. #endif
  9976. /* alen is the size to pass in with each update. */
  9977. for (alen = 1; alen < AES_BLOCK_SIZE + 1; alen++) {
  9978. result = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  9979. if (result != 0)
  9980. ERROR_OUT(-6380, out);
  9981. /* plen is the offset into AAD to update with. */
  9982. for (plen = 0; plen < (int)sizeof(a); plen += alen) {
  9983. int len = sizeof(a) - plen;
  9984. if (len > alen) len = alen;
  9985. result = wc_AesGcmEncryptUpdate(enc, NULL, NULL, 0, a + plen, len);
  9986. if (result != 0)
  9987. ERROR_OUT(-6381, out);
  9988. }
  9989. /* plen is the offset into plaintext to update with. */
  9990. for (plen = 0; plen < (int)sizeof(p); plen += alen) {
  9991. int len = sizeof(p) - plen;
  9992. if (len > alen) len = alen;
  9993. result = wc_AesGcmEncryptUpdate(enc, resultC + plen, p + plen, len,
  9994. NULL, 0);
  9995. if (result != 0)
  9996. ERROR_OUT(-6382, out);
  9997. }
  9998. result = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  9999. if (result != 0)
  10000. ERROR_OUT(-6383, out);
  10001. if (XMEMCMP(resultC, c1, sizeof(c1)) != 0)
  10002. ERROR_OUT(-6384, out);
  10003. if (XMEMCMP(resultT, t1, sizeof(t1)) != 0)
  10004. ERROR_OUT(-6385, out);
  10005. }
  10006. #ifdef HAVE_AES_DECRYPT
  10007. for (alen = 1; alen < AES_BLOCK_SIZE + 1; alen++) {
  10008. result = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  10009. if (result != 0)
  10010. ERROR_OUT(-6390, out);
  10011. /* plen is the offset into AAD to update with. */
  10012. for (plen = 0; plen < (int)sizeof(a); plen += alen) {
  10013. int len = sizeof(a) - plen;
  10014. if (len > alen) len = alen;
  10015. result = wc_AesGcmDecryptUpdate(enc, NULL, NULL, 0, a + plen, len);
  10016. if (result != 0)
  10017. ERROR_OUT(-6391, out);
  10018. }
  10019. /* plen is the offset into cipher text to update with. */
  10020. for (plen = 0; plen < (int)sizeof(c1); plen += alen) {
  10021. int len = sizeof(c1) - plen;
  10022. if (len > alen) len = alen;
  10023. result = wc_AesGcmDecryptUpdate(enc, resultP + plen, c1 + plen, len,
  10024. NULL, 0);
  10025. if (result != 0)
  10026. ERROR_OUT(-6392, out);
  10027. }
  10028. result = wc_AesGcmDecryptFinal(enc, t1, sizeof(t1));
  10029. if (result != 0)
  10030. ERROR_OUT(-6393, out);
  10031. if (XMEMCMP(resultP, p, sizeof(p)) != 0)
  10032. ERROR_OUT(-6394, out);
  10033. }
  10034. #endif /* HAVE_AES_DECRYPT */
  10035. #ifdef BENCH_AESGCM_LARGE
  10036. /* setup test buffer */
  10037. result = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  10038. if (result != 0)
  10039. ERROR_OUT(-6360, out);
  10040. result = wc_AesGcmEncryptUpdate(enc, large_output, large_input,
  10041. BENCH_AESGCM_LARGE, a, sizeof(a));
  10042. if (result != 0)
  10043. ERROR_OUT(-6361, out);
  10044. result = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  10045. if (result != 0)
  10046. ERROR_OUT(-6362, out);
  10047. #ifdef HAVE_AES_DECRYPT
  10048. result = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  10049. if (result != 0)
  10050. ERROR_OUT(-6363, out);
  10051. result = wc_AesGcmDecryptUpdate(enc, large_outdec, large_output,
  10052. BENCH_AESGCM_LARGE, a, sizeof(a));
  10053. if (result != 0)
  10054. ERROR_OUT(-6364, out);
  10055. result = wc_AesGcmDecryptFinal(enc, resultT, sizeof(t1));
  10056. if (result != 0)
  10057. ERROR_OUT(-6365, out);
  10058. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  10059. ERROR_OUT(-6366, out);
  10060. #endif /* HAVE_AES_DECRYPT */
  10061. #endif /* BENCH_AESGCM_LARGE */
  10062. #endif /* WOLFSSL_AESGCM_STREAM */
  10063. #endif /* WOLFSSL_AES_256 */
  10064. #endif /* !WOLFSSL_AFALG_XILINX_AES && !WOLFSSL_XILINX_CRYPT */
  10065. wc_AesFree(enc);
  10066. wc_AesFree(dec);
  10067. ret = 0;
  10068. out:
  10069. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  10070. !defined(WOLFSSL_NO_MALLOC)
  10071. if (large_input)
  10072. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10073. if (large_output)
  10074. XFREE(large_output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10075. if (large_outdec)
  10076. XFREE(large_outdec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10077. #endif
  10078. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10079. if (enc)
  10080. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  10081. if (dec)
  10082. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  10083. #endif
  10084. return ret;
  10085. }
  10086. #ifdef WOLFSSL_AES_128
  10087. WOLFSSL_TEST_SUBROUTINE int gmac_test(void)
  10088. {
  10089. int ret;
  10090. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10091. Gmac *gmac;
  10092. #else
  10093. Gmac gmac[1];
  10094. #endif
  10095. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  10096. {
  10097. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  10098. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  10099. };
  10100. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  10101. {
  10102. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  10103. 0xe2, 0x8c, 0x8f, 0x16
  10104. };
  10105. WOLFSSL_SMALL_STACK_STATIC const byte a1[] =
  10106. {
  10107. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  10108. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  10109. };
  10110. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  10111. {
  10112. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  10113. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  10114. };
  10115. #if (!defined(HAVE_FIPS) || \
  10116. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
  10117. /* FIPS builds only allow 16-byte auth tags. */
  10118. /* This sample uses a 15-byte auth tag. */
  10119. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  10120. {
  10121. 0x40, 0xf7, 0xec, 0xb2, 0x52, 0x6d, 0xaa, 0xd4,
  10122. 0x74, 0x25, 0x1d, 0xf4, 0x88, 0x9e, 0xf6, 0x5b
  10123. };
  10124. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  10125. {
  10126. 0xee, 0x9c, 0x6e, 0x06, 0x15, 0x45, 0x45, 0x03,
  10127. 0x1a, 0x60, 0x24, 0xa7
  10128. };
  10129. WOLFSSL_SMALL_STACK_STATIC const byte a2[] =
  10130. {
  10131. 0x94, 0x81, 0x2c, 0x87, 0x07, 0x4e, 0x15, 0x18,
  10132. 0x34, 0xb8, 0x35, 0xaf, 0x1c, 0xa5, 0x7e, 0x56
  10133. };
  10134. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  10135. {
  10136. 0xc6, 0x81, 0x79, 0x8e, 0x3d, 0xda, 0xb0, 0x9f,
  10137. 0x8d, 0x83, 0xb0, 0xbb, 0x14, 0xb6, 0x91
  10138. };
  10139. #endif
  10140. byte tag[16];
  10141. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10142. if ((gmac = (Gmac *)XMALLOC(sizeof *gmac, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10143. return -6409;
  10144. #endif
  10145. XMEMSET(gmac, 0, sizeof *gmac); /* clear context */
  10146. (void)wc_AesInit(&gmac->aes, HEAP_HINT, INVALID_DEVID); /* Make sure devId updated */
  10147. XMEMSET(tag, 0, sizeof(tag));
  10148. wc_GmacSetKey(gmac, k1, sizeof(k1));
  10149. wc_GmacUpdate(gmac, iv1, sizeof(iv1), a1, sizeof(a1), tag, sizeof(t1));
  10150. if (XMEMCMP(t1, tag, sizeof(t1)) != 0)
  10151. ERROR_OUT(-6400, out);
  10152. #if (!defined(HAVE_FIPS) || \
  10153. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)) )
  10154. XMEMSET(tag, 0, sizeof(tag));
  10155. wc_GmacSetKey(gmac, k2, sizeof(k2));
  10156. wc_GmacUpdate(gmac, iv2, sizeof(iv2), a2, sizeof(a2), tag, sizeof(t2));
  10157. if (XMEMCMP(t2, tag, sizeof(t2)) != 0)
  10158. ERROR_OUT(-6401, out);
  10159. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && !defined(NO_AES_DECRYPT)
  10160. {
  10161. WOLFSSL_SMALL_STACK_STATIC const byte badT[] =
  10162. {
  10163. 0xde, 0xad, 0xbe, 0xef, 0x17, 0x2e, 0xd0, 0x43,
  10164. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  10165. };
  10166. WC_RNG rng;
  10167. byte iv[12];
  10168. #ifndef HAVE_FIPS
  10169. if (wc_InitRng_ex(&rng, HEAP_HINT, devId) != 0)
  10170. ERROR_OUT(-6402, out);
  10171. #else
  10172. if (wc_InitRng(&rng) != 0)
  10173. ERROR_OUT(-6403, out);
  10174. #endif
  10175. if (wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  10176. t1, sizeof(t1)) != 0)
  10177. ERROR_OUT(-6404, out);
  10178. if (wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  10179. badT, sizeof(badT)) != AES_GCM_AUTH_E)
  10180. ERROR_OUT(-6405, out);
  10181. if (wc_GmacVerify(k2, sizeof(k2), iv2, sizeof(iv2), a2, sizeof(a2),
  10182. t2, sizeof(t2)) != 0)
  10183. ERROR_OUT(-6406, out);
  10184. XMEMSET(tag, 0, sizeof(tag));
  10185. XMEMSET(iv, 0, sizeof(iv));
  10186. if (wc_Gmac(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  10187. tag, sizeof(tag), &rng) != 0)
  10188. ERROR_OUT(-6407, out);
  10189. if (wc_GmacVerify(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  10190. tag, sizeof(tag)) != 0)
  10191. ERROR_OUT(-6408, out);
  10192. wc_FreeRng(&rng);
  10193. }
  10194. #endif /* !WC_NO_RNG && !HAVE_SELFTEST && !NO_AES_DECRYPT */
  10195. #endif /* HAVE_FIPS */
  10196. ret = 0;
  10197. out:
  10198. wc_AesFree(&gmac->aes);
  10199. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10200. XFREE(gmac, HEAP_HINT, DYNAMIC_TYPE_AES);
  10201. #endif
  10202. return ret;
  10203. }
  10204. #endif /* WOLFSSL_AES_128 */
  10205. #endif /* HAVE_AESGCM */
  10206. #if defined(HAVE_AESCCM)
  10207. #if defined(WOLFSSL_AES_256)
  10208. static int aesccm_256_test(void)
  10209. {
  10210. int ret;
  10211. /* Test vectors from NIST AES CCM 256-bit CAST Example #1 */
  10212. WOLFSSL_SMALL_STACK_STATIC const byte in_key[32] = {
  10213. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  10214. 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
  10215. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  10216. 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F
  10217. };
  10218. WOLFSSL_SMALL_STACK_STATIC const byte in_nonce[7] = {
  10219. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
  10220. WOLFSSL_SMALL_STACK_STATIC const byte in_auth[8] = {
  10221. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  10222. WOLFSSL_SMALL_STACK_STATIC const byte in_plaintext[4] = {
  10223. 0x20, 0x21, 0x22, 0x23};
  10224. WOLFSSL_SMALL_STACK_STATIC const byte exp_ciphertext[4] = {
  10225. 0x8A, 0xB1, 0xA8, 0x74};
  10226. WOLFSSL_SMALL_STACK_STATIC const byte exp_tag[4] = {
  10227. 0x95, 0xFC, 0x08, 0x20};
  10228. byte output[sizeof(in_plaintext)];
  10229. byte atag[sizeof(exp_tag)];
  10230. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10231. Aes* aes = (Aes*)XMALLOC(sizeof(Aes), HEAP_HINT, DYNAMIC_TYPE_AES);
  10232. if (aes == NULL) {
  10233. return MEMORY_E;
  10234. }
  10235. #else
  10236. Aes aes[1];
  10237. #endif
  10238. ret = wc_AesInit(aes, HEAP_HINT, devId);
  10239. if (ret == 0) {
  10240. ret = wc_AesCcmSetKey(aes, in_key, sizeof(in_key));
  10241. }
  10242. if (ret == 0) {
  10243. ret = wc_AesCcmEncrypt(aes, output, in_plaintext, sizeof(in_plaintext),
  10244. in_nonce, sizeof(in_nonce),
  10245. atag, sizeof(atag),
  10246. in_auth, sizeof(in_auth));
  10247. }
  10248. /* Verify we produce the proper ciphertext and tag */
  10249. if (ret == 0 &&
  10250. (XMEMCMP(output, exp_ciphertext, sizeof(output)) ||
  10251. XMEMCMP(atag, exp_tag, sizeof(atag)))) {
  10252. ret = -1;
  10253. }
  10254. if (ret == 0) {
  10255. /* decrypt inline */
  10256. ret = wc_AesCcmDecrypt(aes, output, output, sizeof(output),
  10257. in_nonce, sizeof(in_nonce),
  10258. atag, sizeof(atag),
  10259. in_auth, sizeof(in_auth));
  10260. }
  10261. /* Verify decryption was successful */
  10262. if (ret == 0 &&
  10263. XMEMCMP(output, in_plaintext, sizeof(output))) {
  10264. ret = -1;
  10265. }
  10266. wc_AesFree(aes);
  10267. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10268. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  10269. #endif
  10270. return ret;
  10271. }
  10272. #endif /* WOLFSSL_AES_256 */
  10273. #if defined(WOLFSSL_AES_128)
  10274. static int aesccm_128_test(void)
  10275. {
  10276. int ret;
  10277. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10278. Aes *enc;
  10279. #else
  10280. Aes enc[1];
  10281. #endif
  10282. /* key */
  10283. WOLFSSL_SMALL_STACK_STATIC const byte k[] =
  10284. {
  10285. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  10286. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  10287. };
  10288. /* nonce */
  10289. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  10290. {
  10291. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  10292. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  10293. };
  10294. /* plaintext */
  10295. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  10296. {
  10297. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  10298. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10299. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  10300. };
  10301. /* plaintext - long */
  10302. WOLFSSL_SMALL_STACK_STATIC const byte pl[] =
  10303. {
  10304. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  10305. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10306. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  10307. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  10308. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  10309. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10310. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  10311. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  10312. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  10313. 0x50
  10314. };
  10315. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  10316. {
  10317. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  10318. };
  10319. /* ciphertext */
  10320. WOLFSSL_SMALL_STACK_STATIC const byte c[] =
  10321. {
  10322. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  10323. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  10324. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  10325. };
  10326. /* tag - authentication */
  10327. WOLFSSL_SMALL_STACK_STATIC const byte t[] =
  10328. {
  10329. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  10330. };
  10331. /* ciphertext - long */
  10332. WOLFSSL_SMALL_STACK_STATIC const byte cl[] =
  10333. {
  10334. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  10335. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  10336. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84, 0xe0,
  10337. 0x44, 0x2d, 0xbe, 0x25, 0xfa, 0x48, 0x2b, 0xa8,
  10338. 0x36, 0x0b, 0xbf, 0x01, 0xc0, 0x12, 0x45, 0xa4,
  10339. 0x82, 0x9f, 0x20, 0x6c, 0xc3, 0xd6, 0xae, 0x5b,
  10340. 0x54, 0x8d, 0xd0, 0xb1, 0x69, 0x2c, 0xec, 0x5e,
  10341. 0x95, 0xa5, 0x6b, 0x48, 0xc3, 0xc6, 0xc8, 0x9e,
  10342. 0xc7, 0x92, 0x98, 0x9d, 0x26, 0x7d, 0x2a, 0x10,
  10343. 0x0b
  10344. };
  10345. /* tag - authentication - long */
  10346. WOLFSSL_SMALL_STACK_STATIC const byte tl[] =
  10347. {
  10348. 0x89, 0xd8, 0xd2, 0x02, 0xc5, 0xcf, 0xae, 0xf4
  10349. };
  10350. /* tag - authentication - empty plaintext */
  10351. WOLFSSL_SMALL_STACK_STATIC const byte t_empty[] =
  10352. {
  10353. 0xe4, 0x28, 0x8a, 0xc3, 0x78, 0x00, 0x0f, 0xf5
  10354. };
  10355. byte t2[sizeof(t)];
  10356. byte p2[sizeof(p)];
  10357. byte c2[sizeof(c)];
  10358. byte iv2[sizeof(iv)];
  10359. byte pl2[sizeof(pl)];
  10360. byte cl2[sizeof(cl)];
  10361. byte tl2[sizeof(tl)];
  10362. byte t_empty2[sizeof(t_empty)];
  10363. int result;
  10364. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10365. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10366. return -6521;
  10367. #endif
  10368. XMEMSET(enc, 0, sizeof *enc); /* clear context */
  10369. XMEMSET(t2, 0, sizeof(t2));
  10370. XMEMSET(c2, 0, sizeof(c2));
  10371. XMEMSET(p2, 0, sizeof(p2));
  10372. result = wc_AesInit(enc, HEAP_HINT, devId);
  10373. if (result != 0)
  10374. ERROR_OUT(-6499, out);
  10375. result = wc_AesCcmSetKey(enc, k, sizeof(k));
  10376. if (result != 0)
  10377. ERROR_OUT(-6500, out);
  10378. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  10379. result = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  10380. t2, sizeof(t2), a, sizeof(a));
  10381. if (result != 0)
  10382. ERROR_OUT(-6501, out);
  10383. if (XMEMCMP(c, c2, sizeof(c2)))
  10384. ERROR_OUT(-6502, out);
  10385. if (XMEMCMP(t, t2, sizeof(t2)))
  10386. ERROR_OUT(-6503, out);
  10387. result = wc_AesCcmDecrypt(enc, p2, c2, sizeof(p2), iv, sizeof(iv),
  10388. t2, sizeof(t2), a, sizeof(a));
  10389. if (result != 0)
  10390. ERROR_OUT(-6504, out);
  10391. if (XMEMCMP(p, p2, sizeof(p2)))
  10392. ERROR_OUT(-6505, out);
  10393. /* Test the authentication failure */
  10394. t2[0]++; /* Corrupt the authentication tag. */
  10395. result = wc_AesCcmDecrypt(enc, p2, c, sizeof(p2), iv, sizeof(iv),
  10396. t2, sizeof(t2), a, sizeof(a));
  10397. if (result == 0)
  10398. ERROR_OUT(-6506, out);
  10399. /* Clear c2 to compare against p2. p2 should be set to zero in case of
  10400. * authentication fail. */
  10401. XMEMSET(c2, 0, sizeof(c2));
  10402. if (XMEMCMP(p2, c2, sizeof(p2)))
  10403. ERROR_OUT(-6507, out);
  10404. wc_AesFree(enc);
  10405. XMEMSET(enc, 0, sizeof(Aes)); /* clear context */
  10406. XMEMSET(t2, 0, sizeof(t2));
  10407. XMEMSET(c2, 0, sizeof(c2));
  10408. XMEMSET(p2, 0, sizeof(p2));
  10409. XMEMSET(iv2, 0, sizeof(iv2));
  10410. #ifndef HAVE_SELFTEST
  10411. /* selftest build does not have wc_AesCcmSetNonce() or
  10412. * wc_AesCcmEncrypt_ex() */
  10413. if (wc_AesCcmSetKey(enc, k, sizeof(k)) != 0)
  10414. ERROR_OUT(-6508, out);
  10415. if (wc_AesCcmSetNonce(enc, iv, sizeof(iv)) != 0)
  10416. ERROR_OUT(-6509, out);
  10417. if (wc_AesCcmEncrypt_ex(enc, c2, p, sizeof(c2), iv2, sizeof(iv2),
  10418. t2, sizeof(t2), a, sizeof(a)) != 0)
  10419. ERROR_OUT(-6510, out);
  10420. if (XMEMCMP(iv, iv2, sizeof(iv2)))
  10421. ERROR_OUT(-6511, out);
  10422. if (XMEMCMP(c, c2, sizeof(c2)))
  10423. ERROR_OUT(-6512, out);
  10424. if (XMEMCMP(t, t2, sizeof(t2)))
  10425. ERROR_OUT(-6513, out);
  10426. #endif
  10427. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10428. /* test fail on invalid IV sizes */
  10429. result = wc_AesCcmSetKey(enc, k, sizeof(k));
  10430. if (result != 0)
  10431. ERROR_OUT(-6514, out);
  10432. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  10433. result = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  10434. t2, 1, a, sizeof(a));
  10435. if (result == 0) {
  10436. ERROR_OUT(-6515, out);
  10437. }
  10438. #endif
  10439. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  10440. result = wc_AesCcmEncrypt(enc, cl2, pl, sizeof(cl2), iv, sizeof(iv),
  10441. tl2, sizeof(tl2), a, sizeof(a));
  10442. if (result != 0)
  10443. ERROR_OUT(-6516, out);
  10444. if (XMEMCMP(cl, cl2, sizeof(cl2)))
  10445. ERROR_OUT(-6517, out);
  10446. if (XMEMCMP(tl, tl2, sizeof(tl2)))
  10447. ERROR_OUT(-6518, out);
  10448. result = wc_AesCcmDecrypt(enc, pl2, cl2, sizeof(pl2), iv, sizeof(iv),
  10449. tl2, sizeof(tl2), a, sizeof(a));
  10450. if (result != 0)
  10451. ERROR_OUT(-6519, out);
  10452. if (XMEMCMP(pl, pl2, sizeof(pl2)))
  10453. ERROR_OUT(-6520, out);
  10454. /* test empty message as null input or output with nonzero inSz. */
  10455. result = wc_AesCcmEncrypt(enc, pl2 /* out */, NULL /* in */, 1 /* inSz */,
  10456. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  10457. a, sizeof(a));
  10458. if (result != BAD_FUNC_ARG)
  10459. ERROR_OUT(-6527, out);
  10460. result = wc_AesCcmEncrypt(enc, NULL /* out */, (const byte *)"" /* in */, 1 /* inSz */,
  10461. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  10462. a, sizeof(a));
  10463. if (result != BAD_FUNC_ARG)
  10464. ERROR_OUT(-6528, out);
  10465. result = wc_AesCcmDecrypt(enc, pl2, NULL /* in */, 1 /* inSz */,
  10466. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  10467. sizeof(a));
  10468. if (result != BAD_FUNC_ARG)
  10469. ERROR_OUT(-6529, out);
  10470. result = wc_AesCcmDecrypt(enc, NULL /* out */, (const byte *)"" /* in */, 1 /* inSz */,
  10471. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  10472. sizeof(a));
  10473. if (result != BAD_FUNC_ARG)
  10474. ERROR_OUT(-6530, out);
  10475. /* test empty message as null input and output with zero inSz --
  10476. * must either succeed, or fail early with BAD_FUNC_ARG.
  10477. */
  10478. result = wc_AesCcmEncrypt(enc, NULL /* out */, NULL /* in */, 0 /* inSz */,
  10479. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  10480. a, sizeof(a));
  10481. if (result != BAD_FUNC_ARG) {
  10482. if (result != 0)
  10483. ERROR_OUT(-6521, out);
  10484. if (XMEMCMP(t_empty, t_empty2, sizeof(t_empty2)))
  10485. ERROR_OUT(-6522, out);
  10486. result = wc_AesCcmDecrypt(enc, NULL /* out */, NULL /* in */,
  10487. 0 /* inSz */, iv, sizeof(iv), t_empty2,
  10488. sizeof(t_empty2), a, sizeof(a));
  10489. if (result != 0)
  10490. ERROR_OUT(-6523, out);
  10491. }
  10492. /* test empty message as zero-length string -- must work. */
  10493. result = wc_AesCcmEncrypt(enc, pl2, (const byte *)"", 0 /* inSz */, iv,
  10494. sizeof(iv), t_empty2, sizeof(t_empty2), a,
  10495. sizeof(a));
  10496. if (result != 0)
  10497. ERROR_OUT(-6524, out);
  10498. if (XMEMCMP(t_empty, t_empty2, sizeof(t_empty2)))
  10499. ERROR_OUT(-6525, out);
  10500. result = wc_AesCcmDecrypt(enc, pl2, (const byte *)"", 0 /* inSz */,
  10501. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  10502. sizeof(a));
  10503. if (result != 0)
  10504. ERROR_OUT(-6526, out);
  10505. wc_AesFree(enc);
  10506. ret = 0;
  10507. out:
  10508. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10509. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  10510. #endif
  10511. return ret;
  10512. }
  10513. #endif /* WOLFSSL_AES_128 */
  10514. WOLFSSL_TEST_SUBROUTINE int aesccm_test(void)
  10515. {
  10516. int ret = 0;
  10517. #ifdef WOLFSSL_AES_128
  10518. if (ret == 0)
  10519. ret = aesccm_128_test();
  10520. #endif
  10521. #ifdef WOLFSSL_AES_256
  10522. if (ret == 0)
  10523. ret = aesccm_256_test();
  10524. #endif
  10525. return ret;
  10526. }
  10527. #endif /* HAVE_AESCCM */
  10528. #ifdef HAVE_AES_KEYWRAP
  10529. #define MAX_KEYWRAP_TEST_OUTLEN 40
  10530. #define MAX_KEYWRAP_TEST_PLAINLEN 32
  10531. typedef struct keywrapVector {
  10532. const byte* kek;
  10533. const byte* data;
  10534. const byte* verify;
  10535. word32 kekLen;
  10536. word32 dataLen;
  10537. word32 verifyLen;
  10538. } keywrapVector;
  10539. WOLFSSL_TEST_SUBROUTINE int aeskeywrap_test(void)
  10540. {
  10541. int wrapSz, plainSz, testSz, i;
  10542. /* test vectors from RFC 3394 (kek, data, verify) */
  10543. #ifdef WOLFSSL_AES_128
  10544. /* Wrap 128 bits of Key Data with a 128-bit KEK */
  10545. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  10546. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10547. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  10548. };
  10549. WOLFSSL_SMALL_STACK_STATIC const byte d1[] = {
  10550. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10551. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  10552. };
  10553. WOLFSSL_SMALL_STACK_STATIC const byte v1[] = {
  10554. 0x1F, 0xA6, 0x8B, 0x0A, 0x81, 0x12, 0xB4, 0x47,
  10555. 0xAE, 0xF3, 0x4B, 0xD8, 0xFB, 0x5A, 0x7B, 0x82,
  10556. 0x9D, 0x3E, 0x86, 0x23, 0x71, 0xD2, 0xCF, 0xE5
  10557. };
  10558. #endif /* WOLFSSL_AES_128 */
  10559. #ifdef WOLFSSL_AES_192
  10560. /* Wrap 128 bits of Key Data with a 192-bit KEK */
  10561. WOLFSSL_SMALL_STACK_STATIC const byte k2[] = {
  10562. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10563. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  10564. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  10565. };
  10566. WOLFSSL_SMALL_STACK_STATIC const byte d2[] = {
  10567. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10568. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  10569. };
  10570. WOLFSSL_SMALL_STACK_STATIC const byte v2[] = {
  10571. 0x96, 0x77, 0x8B, 0x25, 0xAE, 0x6C, 0xA4, 0x35,
  10572. 0xF9, 0x2B, 0x5B, 0x97, 0xC0, 0x50, 0xAE, 0xD2,
  10573. 0x46, 0x8A, 0xB8, 0xA1, 0x7A, 0xD8, 0x4E, 0x5D
  10574. };
  10575. #endif
  10576. #ifdef WOLFSSL_AES_256
  10577. /* Wrap 128 bits of Key Data with a 256-bit KEK */
  10578. WOLFSSL_SMALL_STACK_STATIC const byte k3[] = {
  10579. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10580. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  10581. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10582. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  10583. };
  10584. WOLFSSL_SMALL_STACK_STATIC const byte d3[] = {
  10585. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10586. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  10587. };
  10588. WOLFSSL_SMALL_STACK_STATIC const byte v3[] = {
  10589. 0x64, 0xE8, 0xC3, 0xF9, 0xCE, 0x0F, 0x5B, 0xA2,
  10590. 0x63, 0xE9, 0x77, 0x79, 0x05, 0x81, 0x8A, 0x2A,
  10591. 0x93, 0xC8, 0x19, 0x1E, 0x7D, 0x6E, 0x8A, 0xE7
  10592. };
  10593. #endif
  10594. #ifdef WOLFSSL_AES_192
  10595. /* Wrap 192 bits of Key Data with a 192-bit KEK */
  10596. WOLFSSL_SMALL_STACK_STATIC const byte k4[] = {
  10597. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10598. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  10599. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  10600. };
  10601. WOLFSSL_SMALL_STACK_STATIC const byte d4[] = {
  10602. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10603. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  10604. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  10605. };
  10606. WOLFSSL_SMALL_STACK_STATIC const byte v4[] = {
  10607. 0x03, 0x1D, 0x33, 0x26, 0x4E, 0x15, 0xD3, 0x32,
  10608. 0x68, 0xF2, 0x4E, 0xC2, 0x60, 0x74, 0x3E, 0xDC,
  10609. 0xE1, 0xC6, 0xC7, 0xDD, 0xEE, 0x72, 0x5A, 0x93,
  10610. 0x6B, 0xA8, 0x14, 0x91, 0x5C, 0x67, 0x62, 0xD2
  10611. };
  10612. #endif
  10613. #ifdef WOLFSSL_AES_256
  10614. /* Wrap 192 bits of Key Data with a 256-bit KEK */
  10615. WOLFSSL_SMALL_STACK_STATIC const byte k5[] = {
  10616. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10617. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  10618. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10619. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  10620. };
  10621. WOLFSSL_SMALL_STACK_STATIC const byte d5[] = {
  10622. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10623. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  10624. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  10625. };
  10626. WOLFSSL_SMALL_STACK_STATIC const byte v5[] = {
  10627. 0xA8, 0xF9, 0xBC, 0x16, 0x12, 0xC6, 0x8B, 0x3F,
  10628. 0xF6, 0xE6, 0xF4, 0xFB, 0xE3, 0x0E, 0x71, 0xE4,
  10629. 0x76, 0x9C, 0x8B, 0x80, 0xA3, 0x2C, 0xB8, 0x95,
  10630. 0x8C, 0xD5, 0xD1, 0x7D, 0x6B, 0x25, 0x4D, 0xA1
  10631. };
  10632. /* Wrap 256 bits of Key Data with a 256-bit KEK */
  10633. WOLFSSL_SMALL_STACK_STATIC const byte k6[] = {
  10634. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10635. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  10636. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10637. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  10638. };
  10639. WOLFSSL_SMALL_STACK_STATIC const byte d6[] = {
  10640. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10641. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  10642. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10643. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  10644. };
  10645. WOLFSSL_SMALL_STACK_STATIC const byte v6[] = {
  10646. 0x28, 0xC9, 0xF4, 0x04, 0xC4, 0xB8, 0x10, 0xF4,
  10647. 0xCB, 0xCC, 0xB3, 0x5C, 0xFB, 0x87, 0xF8, 0x26,
  10648. 0x3F, 0x57, 0x86, 0xE2, 0xD8, 0x0E, 0xD3, 0x26,
  10649. 0xCB, 0xC7, 0xF0, 0xE7, 0x1A, 0x99, 0xF4, 0x3B,
  10650. 0xFB, 0x98, 0x8B, 0x9B, 0x7A, 0x02, 0xDD, 0x21
  10651. };
  10652. #endif /* WOLFSSL_AES_256 */
  10653. byte output[MAX_KEYWRAP_TEST_OUTLEN];
  10654. byte plain [MAX_KEYWRAP_TEST_PLAINLEN];
  10655. const keywrapVector test_wrap[] =
  10656. {
  10657. #ifdef WOLFSSL_AES_128
  10658. {k1, d1, v1, sizeof(k1), sizeof(d1), sizeof(v1)},
  10659. #endif
  10660. #ifdef WOLFSSL_AES_192
  10661. {k2, d2, v2, sizeof(k2), sizeof(d2), sizeof(v2)},
  10662. #endif
  10663. #ifdef WOLFSSL_AES_256
  10664. {k3, d3, v3, sizeof(k3), sizeof(d3), sizeof(v3)},
  10665. #endif
  10666. #ifdef WOLFSSL_AES_192
  10667. {k4, d4, v4, sizeof(k4), sizeof(d4), sizeof(v4)},
  10668. #endif
  10669. #ifdef WOLFSSL_AES_256
  10670. {k5, d5, v5, sizeof(k5), sizeof(d5), sizeof(v5)},
  10671. {k6, d6, v6, sizeof(k6), sizeof(d6), sizeof(v6)}
  10672. #endif
  10673. };
  10674. testSz = sizeof(test_wrap) / sizeof(keywrapVector);
  10675. XMEMSET(output, 0, sizeof(output));
  10676. XMEMSET(plain, 0, sizeof(plain));
  10677. for (i = 0; i < testSz; i++) {
  10678. wrapSz = wc_AesKeyWrap(test_wrap[i].kek, test_wrap[i].kekLen,
  10679. test_wrap[i].data, test_wrap[i].dataLen,
  10680. output, sizeof(output), NULL);
  10681. if ( (wrapSz < 0) || (wrapSz != (int)test_wrap[i].verifyLen) )
  10682. return -6600;
  10683. if (XMEMCMP(output, test_wrap[i].verify, test_wrap[i].verifyLen) != 0)
  10684. return -6601;
  10685. plainSz = wc_AesKeyUnWrap((byte*)test_wrap[i].kek, test_wrap[i].kekLen,
  10686. output, wrapSz,
  10687. plain, sizeof(plain), NULL);
  10688. if ( (plainSz < 0) || (plainSz != (int)test_wrap[i].dataLen) )
  10689. return -6602;
  10690. if (XMEMCMP(plain, test_wrap[i].data, test_wrap[i].dataLen) != 0)
  10691. return -6603 - i;
  10692. }
  10693. return 0;
  10694. }
  10695. #endif /* HAVE_AES_KEYWRAP */
  10696. #endif /* NO_AES */
  10697. #ifdef HAVE_CAMELLIA
  10698. enum {
  10699. CAM_ECB_ENC, CAM_ECB_DEC, CAM_CBC_ENC, CAM_CBC_DEC
  10700. };
  10701. typedef struct {
  10702. int type;
  10703. const byte* plaintext;
  10704. const byte* iv;
  10705. const byte* ciphertext;
  10706. const byte* key;
  10707. word32 keySz;
  10708. int errorCode;
  10709. } test_vector_t;
  10710. WOLFSSL_TEST_SUBROUTINE int camellia_test(void)
  10711. {
  10712. /* Camellia ECB Test Plaintext */
  10713. WOLFSSL_SMALL_STACK_STATIC const byte pte[] =
  10714. {
  10715. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10716. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  10717. };
  10718. /* Camellia ECB Test Initialization Vector */
  10719. WOLFSSL_SMALL_STACK_STATIC const byte ive[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  10720. /* Test 1: Camellia ECB 128-bit key */
  10721. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  10722. {
  10723. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10724. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  10725. };
  10726. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  10727. {
  10728. 0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73,
  10729. 0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43
  10730. };
  10731. /* Test 2: Camellia ECB 192-bit key */
  10732. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  10733. {
  10734. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10735. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  10736. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  10737. };
  10738. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  10739. {
  10740. 0xb4, 0x99, 0x34, 0x01, 0xb3, 0xe9, 0x96, 0xf8,
  10741. 0x4e, 0xe5, 0xce, 0xe7, 0xd7, 0x9b, 0x09, 0xb9
  10742. };
  10743. /* Test 3: Camellia ECB 256-bit key */
  10744. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  10745. {
  10746. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10747. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  10748. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10749. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  10750. };
  10751. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  10752. {
  10753. 0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c,
  10754. 0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09
  10755. };
  10756. /* Camellia CBC Test Plaintext */
  10757. WOLFSSL_SMALL_STACK_STATIC const byte ptc[] =
  10758. {
  10759. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  10760. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  10761. };
  10762. /* Camellia CBC Test Initialization Vector */
  10763. WOLFSSL_SMALL_STACK_STATIC const byte ivc[] =
  10764. {
  10765. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10766. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  10767. };
  10768. /* Test 4: Camellia-CBC 128-bit key */
  10769. WOLFSSL_SMALL_STACK_STATIC const byte k4[] =
  10770. {
  10771. 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
  10772. 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
  10773. };
  10774. WOLFSSL_SMALL_STACK_STATIC const byte c4[] =
  10775. {
  10776. 0x16, 0x07, 0xCF, 0x49, 0x4B, 0x36, 0xBB, 0xF0,
  10777. 0x0D, 0xAE, 0xB0, 0xB5, 0x03, 0xC8, 0x31, 0xAB
  10778. };
  10779. /* Test 5: Camellia-CBC 192-bit key */
  10780. WOLFSSL_SMALL_STACK_STATIC const byte k5[] =
  10781. {
  10782. 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52,
  10783. 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5,
  10784. 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B
  10785. };
  10786. WOLFSSL_SMALL_STACK_STATIC const byte c5[] =
  10787. {
  10788. 0x2A, 0x48, 0x30, 0xAB, 0x5A, 0xC4, 0xA1, 0xA2,
  10789. 0x40, 0x59, 0x55, 0xFD, 0x21, 0x95, 0xCF, 0x93
  10790. };
  10791. /* Test 6: CBC 256-bit key */
  10792. WOLFSSL_SMALL_STACK_STATIC const byte k6[] =
  10793. {
  10794. 0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE,
  10795. 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
  10796. 0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7,
  10797. 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4
  10798. };
  10799. WOLFSSL_SMALL_STACK_STATIC const byte c6[] =
  10800. {
  10801. 0xE6, 0xCF, 0xA3, 0x5F, 0xC0, 0x2B, 0x13, 0x4A,
  10802. 0x4D, 0x2C, 0x0B, 0x67, 0x37, 0xAC, 0x3E, 0xDA
  10803. };
  10804. byte out[CAMELLIA_BLOCK_SIZE];
  10805. Camellia cam;
  10806. int i, testsSz, ret;
  10807. WOLFSSL_SMALL_STACK_STATIC const test_vector_t testVectors[] =
  10808. {
  10809. {CAM_ECB_ENC, pte, ive, c1, k1, sizeof(k1), -114},
  10810. {CAM_ECB_ENC, pte, ive, c2, k2, sizeof(k2), -115},
  10811. {CAM_ECB_ENC, pte, ive, c3, k3, sizeof(k3), -116},
  10812. {CAM_ECB_DEC, pte, ive, c1, k1, sizeof(k1), -117},
  10813. {CAM_ECB_DEC, pte, ive, c2, k2, sizeof(k2), -118},
  10814. {CAM_ECB_DEC, pte, ive, c3, k3, sizeof(k3), -119},
  10815. {CAM_CBC_ENC, ptc, ivc, c4, k4, sizeof(k4), -120},
  10816. {CAM_CBC_ENC, ptc, ivc, c5, k5, sizeof(k5), -121},
  10817. {CAM_CBC_ENC, ptc, ivc, c6, k6, sizeof(k6), -122},
  10818. {CAM_CBC_DEC, ptc, ivc, c4, k4, sizeof(k4), -123},
  10819. {CAM_CBC_DEC, ptc, ivc, c5, k5, sizeof(k5), -124},
  10820. {CAM_CBC_DEC, ptc, ivc, c6, k6, sizeof(k6), -125}
  10821. };
  10822. testsSz = sizeof(testVectors)/sizeof(test_vector_t);
  10823. for (i = 0; i < testsSz; i++) {
  10824. if (wc_CamelliaSetKey(&cam, testVectors[i].key, testVectors[i].keySz,
  10825. testVectors[i].iv) != 0)
  10826. return testVectors[i].errorCode;
  10827. switch (testVectors[i].type) {
  10828. case CAM_ECB_ENC:
  10829. ret = wc_CamelliaEncryptDirect(&cam, out,
  10830. testVectors[i].plaintext);
  10831. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  10832. CAMELLIA_BLOCK_SIZE))
  10833. return testVectors[i].errorCode;
  10834. break;
  10835. case CAM_ECB_DEC:
  10836. ret = wc_CamelliaDecryptDirect(&cam, out,
  10837. testVectors[i].ciphertext);
  10838. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  10839. CAMELLIA_BLOCK_SIZE))
  10840. return testVectors[i].errorCode;
  10841. break;
  10842. case CAM_CBC_ENC:
  10843. ret = wc_CamelliaCbcEncrypt(&cam, out, testVectors[i].plaintext,
  10844. CAMELLIA_BLOCK_SIZE);
  10845. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  10846. CAMELLIA_BLOCK_SIZE))
  10847. return testVectors[i].errorCode;
  10848. break;
  10849. case CAM_CBC_DEC:
  10850. ret = wc_CamelliaCbcDecrypt(&cam, out,
  10851. testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE);
  10852. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  10853. CAMELLIA_BLOCK_SIZE))
  10854. return testVectors[i].errorCode;
  10855. break;
  10856. default:
  10857. break;
  10858. }
  10859. }
  10860. /* Setting the IV and checking it was actually set. */
  10861. ret = wc_CamelliaSetIV(&cam, ivc);
  10862. if (ret != 0 || XMEMCMP(cam.reg, ivc, CAMELLIA_BLOCK_SIZE))
  10863. return -6700;
  10864. /* Setting the IV to NULL should be same as all zeros IV */
  10865. if (wc_CamelliaSetIV(&cam, NULL) != 0 ||
  10866. XMEMCMP(cam.reg, ive, CAMELLIA_BLOCK_SIZE))
  10867. return -6701;
  10868. /* First parameter should never be null */
  10869. if (wc_CamelliaSetIV(NULL, NULL) == 0)
  10870. return -6702;
  10871. /* First parameter should never be null, check it fails */
  10872. if (wc_CamelliaSetKey(NULL, k1, sizeof(k1), NULL) == 0)
  10873. return -6703;
  10874. /* Key should have a size of 16, 24, or 32 */
  10875. if (wc_CamelliaSetKey(&cam, k1, 0, NULL) == 0)
  10876. return -6704;
  10877. return 0;
  10878. }
  10879. #endif /* HAVE_CAMELLIA */
  10880. #ifdef HAVE_XCHACHA
  10881. WOLFSSL_TEST_SUBROUTINE int XChaCha_test(void) {
  10882. int ret = -6830;
  10883. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  10884. 0x54, 0x68, 0x65, 0x20, 0x64, 0x68, 0x6f, 0x6c, 0x65, 0x20, 0x28, 0x70, 0x72, 0x6f, 0x6e, 0x6f, /* The dhole (prono */
  10885. 0x75, 0x6e, 0x63, 0x65, 0x64, 0x20, 0x22, 0x64, 0x6f, 0x6c, 0x65, 0x22, 0x29, 0x20, 0x69, 0x73, /* unced "dole") is */
  10886. 0x20, 0x61, 0x6c, 0x73, 0x6f, 0x20, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x20, 0x61, 0x73, 0x20, 0x74, /* also known as t */
  10887. 0x68, 0x65, 0x20, 0x41, 0x73, 0x69, 0x61, 0x74, 0x69, 0x63, 0x20, 0x77, 0x69, 0x6c, 0x64, 0x20, /* he Asiatic wild */
  10888. 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x72, 0x65, 0x64, 0x20, 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x61, 0x6e, /* dog, red dog, an */
  10889. 0x64, 0x20, 0x77, 0x68, 0x69, 0x73, 0x74, 0x6c, 0x69, 0x6e, 0x67, 0x20, 0x64, 0x6f, 0x67, 0x2e, /* d whistling dog. */
  10890. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x61, 0x62, 0x6f, 0x75, 0x74, 0x20, 0x74, 0x68, 0x65, /* It is about the */
  10891. 0x20, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x6f, 0x66, 0x20, 0x61, 0x20, 0x47, 0x65, 0x72, 0x6d, 0x61, /* size of a Germa */
  10892. 0x6e, 0x20, 0x73, 0x68, 0x65, 0x70, 0x68, 0x65, 0x72, 0x64, 0x20, 0x62, 0x75, 0x74, 0x20, 0x6c, /* n shepherd but l */
  10893. 0x6f, 0x6f, 0x6b, 0x73, 0x20, 0x6d, 0x6f, 0x72, 0x65, 0x20, 0x6c, 0x69, 0x6b, 0x65, 0x20, 0x61, /* ooks more like a */
  10894. 0x20, 0x6c, 0x6f, 0x6e, 0x67, 0x2d, 0x6c, 0x65, 0x67, 0x67, 0x65, 0x64, 0x20, 0x66, 0x6f, 0x78, /* long-legged fox */
  10895. 0x2e, 0x20, 0x54, 0x68, 0x69, 0x73, 0x20, 0x68, 0x69, 0x67, 0x68, 0x6c, 0x79, 0x20, 0x65, 0x6c, /* . This highly el */
  10896. 0x75, 0x73, 0x69, 0x76, 0x65, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x6b, 0x69, 0x6c, 0x6c, 0x65, /* usive and skille */
  10897. 0x64, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x65, 0x72, 0x20, 0x69, 0x73, 0x20, 0x63, 0x6c, 0x61, 0x73, /* d jumper is clas */
  10898. 0x73, 0x69, 0x66, 0x69, 0x65, 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x77, 0x6f, 0x6c, 0x76, /* sified with wolv */
  10899. 0x65, 0x73, 0x2c, 0x20, 0x63, 0x6f, 0x79, 0x6f, 0x74, 0x65, 0x73, 0x2c, 0x20, 0x6a, 0x61, 0x63, /* es, coyotes, jac */
  10900. 0x6b, 0x61, 0x6c, 0x73, 0x2c, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x66, 0x6f, 0x78, 0x65, 0x73, 0x20, /* kals, and foxes */
  10901. 0x69, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74, 0x61, 0x78, 0x6f, 0x6e, 0x6f, 0x6d, 0x69, 0x63, /* in the taxonomic */
  10902. 0x20, 0x66, 0x61, 0x6d, 0x69, 0x6c, 0x79, 0x20, 0x43, 0x61, 0x6e, 0x69, 0x64, 0x61, 0x65, 0x2e /* family Canidae. */
  10903. };
  10904. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  10905. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  10906. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  10907. };
  10908. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  10909. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  10910. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x58 }; /* PQRSTUVW */
  10911. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  10912. 0x45, 0x59, 0xab, 0xba, 0x4e, 0x48, 0xc1, 0x61, 0x02, 0xe8, 0xbb, 0x2c, 0x05, 0xe6, 0x94, 0x7f,
  10913. 0x50, 0xa7, 0x86, 0xde, 0x16, 0x2f, 0x9b, 0x0b, 0x7e, 0x59, 0x2a, 0x9b, 0x53, 0xd0, 0xd4, 0xe9,
  10914. 0x8d, 0x8d, 0x64, 0x10, 0xd5, 0x40, 0xa1, 0xa6, 0x37, 0x5b, 0x26, 0xd8, 0x0d, 0xac, 0xe4, 0xfa,
  10915. 0xb5, 0x23, 0x84, 0xc7, 0x31, 0xac, 0xbf, 0x16, 0xa5, 0x92, 0x3c, 0x0c, 0x48, 0xd3, 0x57, 0x5d,
  10916. 0x4d, 0x0d, 0x2c, 0x67, 0x3b, 0x66, 0x6f, 0xaa, 0x73, 0x10, 0x61, 0x27, 0x77, 0x01, 0x09, 0x3a,
  10917. 0x6b, 0xf7, 0xa1, 0x58, 0xa8, 0x86, 0x42, 0x92, 0xa4, 0x1c, 0x48, 0xe3, 0xa9, 0xb4, 0xc0, 0xda,
  10918. 0xec, 0xe0, 0xf8, 0xd9, 0x8d, 0x0d, 0x7e, 0x05, 0xb3, 0x7a, 0x30, 0x7b, 0xbb, 0x66, 0x33, 0x31,
  10919. 0x64, 0xec, 0x9e, 0x1b, 0x24, 0xea, 0x0d, 0x6c, 0x3f, 0xfd, 0xdc, 0xec, 0x4f, 0x68, 0xe7, 0x44,
  10920. 0x30, 0x56, 0x19, 0x3a, 0x03, 0xc8, 0x10, 0xe1, 0x13, 0x44, 0xca, 0x06, 0xd8, 0xed, 0x8a, 0x2b,
  10921. 0xfb, 0x1e, 0x8d, 0x48, 0xcf, 0xa6, 0xbc, 0x0e, 0xb4, 0xe2, 0x46, 0x4b, 0x74, 0x81, 0x42, 0x40,
  10922. 0x7c, 0x9f, 0x43, 0x1a, 0xee, 0x76, 0x99, 0x60, 0xe1, 0x5b, 0xa8, 0xb9, 0x68, 0x90, 0x46, 0x6e,
  10923. 0xf2, 0x45, 0x75, 0x99, 0x85, 0x23, 0x85, 0xc6, 0x61, 0xf7, 0x52, 0xce, 0x20, 0xf9, 0xda, 0x0c,
  10924. 0x09, 0xab, 0x6b, 0x19, 0xdf, 0x74, 0xe7, 0x6a, 0x95, 0x96, 0x74, 0x46, 0xf8, 0xd0, 0xfd, 0x41,
  10925. 0x5e, 0x7b, 0xee, 0x2a, 0x12, 0xa1, 0x14, 0xc2, 0x0e, 0xb5, 0x29, 0x2a, 0xe7, 0xa3, 0x49, 0xae,
  10926. 0x57, 0x78, 0x20, 0xd5, 0x52, 0x0a, 0x1f, 0x3f, 0xb6, 0x2a, 0x17, 0xce, 0x6a, 0x7e, 0x68, 0xfa,
  10927. 0x7c, 0x79, 0x11, 0x1d, 0x88, 0x60, 0x92, 0x0b, 0xc0, 0x48, 0xef, 0x43, 0xfe, 0x84, 0x48, 0x6c,
  10928. 0xcb, 0x87, 0xc2, 0x5f, 0x0a, 0xe0, 0x45, 0xf0, 0xcc, 0xe1, 0xe7, 0x98, 0x9a, 0x9a, 0xa2, 0x20,
  10929. 0xa2, 0x8b, 0xdd, 0x48, 0x27, 0xe7, 0x51, 0xa2, 0x4a, 0x6d, 0x5c, 0x62, 0xd7, 0x90, 0xa6, 0x63,
  10930. 0x93, 0xb9, 0x31, 0x11, 0xc1, 0xa5, 0x5d, 0xd7, 0x42, 0x1a, 0x10, 0x18, 0x49, 0x74, 0xc7, 0xc5
  10931. };
  10932. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10933. struct ChaCha *chacha = (struct ChaCha *)XMALLOC(sizeof *chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  10934. byte *buf1 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10935. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10936. if ((chacha == NULL) || (buf1 == NULL) || (buf2 == NULL))
  10937. ERROR_OUT(MEMORY_E, out);
  10938. #else
  10939. struct ChaCha chacha[1];
  10940. byte buf1[sizeof Plaintext];
  10941. byte buf2[sizeof Plaintext];
  10942. #endif
  10943. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  10944. if (ret < 0)
  10945. ERROR_OUT(-6831, out);
  10946. ret = wc_Chacha_Process(chacha, buf1, Plaintext, sizeof Plaintext);
  10947. if (ret < 0)
  10948. ERROR_OUT(-6832, out);
  10949. if (XMEMCMP(buf1, Ciphertext, sizeof Plaintext))
  10950. ERROR_OUT(-6833, out);
  10951. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  10952. if (ret < 0)
  10953. ERROR_OUT(-6834, out);
  10954. ret = wc_Chacha_Process(chacha, buf2, buf1, sizeof Plaintext);
  10955. if (ret < 0)
  10956. ERROR_OUT(-6835, out);
  10957. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  10958. ERROR_OUT(-6836, out);
  10959. out:
  10960. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10961. if (chacha)
  10962. XFREE(chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  10963. if (buf1)
  10964. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10965. if (buf2)
  10966. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10967. #endif
  10968. return ret;
  10969. }
  10970. #endif /* HAVE_XCHACHA */
  10971. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  10972. WOLFSSL_TEST_SUBROUTINE int XChaCha20Poly1305_test(void) {
  10973. int ret;
  10974. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  10975. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c, /* Ladies and Gentl */
  10976. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73, /* emen of the clas */
  10977. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39, 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63, /* s of '99: If I c */
  10978. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66, 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f, /* ould offer you o */
  10979. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20, 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20, /* nly one tip for */
  10980. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75, 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73, /* the future, suns */
  10981. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69, /* creen would be i */
  10982. 0x74, 0x2e }; /* t. */
  10983. WOLFSSL_SMALL_STACK_STATIC const byte AAD[] = { 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7 }; /* PQRS........ */
  10984. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  10985. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  10986. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  10987. };
  10988. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  10989. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  10990. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57 }; /* PQRSTUVW */
  10991. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  10992. 0xbd, 0x6d, 0x17, 0x9d, 0x3e, 0x83, 0xd4, 0x3b, 0x95, 0x76, 0x57, 0x94, 0x93, 0xc0, 0xe9, 0x39,
  10993. 0x57, 0x2a, 0x17, 0x00, 0x25, 0x2b, 0xfa, 0xcc, 0xbe, 0xd2, 0x90, 0x2c, 0x21, 0x39, 0x6c, 0xbb,
  10994. 0x73, 0x1c, 0x7f, 0x1b, 0x0b, 0x4a, 0xa6, 0x44, 0x0b, 0xf3, 0xa8, 0x2f, 0x4e, 0xda, 0x7e, 0x39,
  10995. 0xae, 0x64, 0xc6, 0x70, 0x8c, 0x54, 0xc2, 0x16, 0xcb, 0x96, 0xb7, 0x2e, 0x12, 0x13, 0xb4, 0x52,
  10996. 0x2f, 0x8c, 0x9b, 0xa4, 0x0d, 0xb5, 0xd9, 0x45, 0xb1, 0x1b, 0x69, 0xb9, 0x82, 0xc1, 0xbb, 0x9e,
  10997. 0x3f, 0x3f, 0xac, 0x2b, 0xc3, 0x69, 0x48, 0x8f, 0x76, 0xb2, 0x38, 0x35, 0x65, 0xd3, 0xff, 0xf9,
  10998. 0x21, 0xf9, 0x66, 0x4c, 0x97, 0x63, 0x7d, 0xa9, 0x76, 0x88, 0x12, 0xf6, 0x15, 0xc6, 0x8b, 0x13,
  10999. 0xb5, 0x2e };
  11000. WOLFSSL_SMALL_STACK_STATIC const byte Tag[] = {
  11001. 0xc0, 0x87, 0x59, 0x24, 0xc1, 0xc7, 0x98, 0x79, 0x47, 0xde, 0xaf, 0xd8, 0x78, 0x0a, 0xcf, 0x49
  11002. };
  11003. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11004. byte *buf1 = (byte *)XMALLOC(sizeof Ciphertext + sizeof Tag, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11005. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11006. if ((buf1 == NULL) || (buf2 == NULL))
  11007. ERROR_OUT(-6480, out);
  11008. #else
  11009. byte buf1[sizeof Ciphertext + sizeof Tag];
  11010. byte buf2[sizeof Plaintext];
  11011. #endif
  11012. ret = wc_XChaCha20Poly1305_Encrypt(buf1, sizeof Ciphertext + sizeof Tag,
  11013. Plaintext, sizeof Plaintext,
  11014. AAD, sizeof AAD,
  11015. IV, sizeof IV,
  11016. Key, sizeof Key);
  11017. if (ret < 0)
  11018. ERROR_OUT(-6841, out);
  11019. if (XMEMCMP(buf1, Ciphertext, sizeof Ciphertext))
  11020. ERROR_OUT(-6842, out);
  11021. if (XMEMCMP(buf1 + sizeof Ciphertext, Tag, CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE))
  11022. ERROR_OUT(-6843, out);
  11023. ret = wc_XChaCha20Poly1305_Decrypt(buf2, sizeof Plaintext,
  11024. buf1, sizeof Ciphertext + sizeof Tag,
  11025. AAD, sizeof AAD,
  11026. IV, sizeof IV,
  11027. Key, sizeof Key);
  11028. if (ret < 0)
  11029. ERROR_OUT(-6844, out);
  11030. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  11031. ERROR_OUT(-6845, out);
  11032. out:
  11033. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11034. if (buf1 != NULL)
  11035. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11036. if (buf2 != NULL)
  11037. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11038. #endif
  11039. return ret;
  11040. }
  11041. #endif /* defined(HAVE_XCHACHA) && defined(HAVE_POLY1305) */
  11042. #ifndef WC_NO_RNG
  11043. static int _rng_test(WC_RNG* rng, int errorOffset)
  11044. {
  11045. byte block[32];
  11046. int ret, i;
  11047. XMEMSET(block, 0, sizeof(block));
  11048. ret = wc_RNG_GenerateBlock(rng, block, sizeof(block));
  11049. if (ret != 0) {
  11050. ret = -6850;
  11051. goto exit;
  11052. }
  11053. /* Check for 0's */
  11054. for (i=0; i<(int)sizeof(block); i++) {
  11055. if (block[i] == 0) {
  11056. ret++;
  11057. }
  11058. }
  11059. /* All zeros count check */
  11060. if (ret >= (int)sizeof(block)) {
  11061. ret = -6851;
  11062. goto exit;
  11063. }
  11064. ret = wc_RNG_GenerateByte(rng, block);
  11065. if (ret != 0) {
  11066. ret = -6852;
  11067. goto exit;
  11068. }
  11069. /* Parameter validation testing. */
  11070. ret = wc_RNG_GenerateBlock(NULL, block, sizeof(block));
  11071. if (ret != BAD_FUNC_ARG) {
  11072. ret = -6853;
  11073. goto exit;
  11074. }
  11075. ret = wc_RNG_GenerateBlock(rng, NULL, sizeof(block));
  11076. if (ret != BAD_FUNC_ARG) {
  11077. ret = -6854;
  11078. goto exit;
  11079. }
  11080. ret = wc_RNG_GenerateByte(NULL, block);
  11081. if (ret != BAD_FUNC_ARG) {
  11082. ret = -6855;
  11083. goto exit;
  11084. }
  11085. ret = wc_RNG_GenerateByte(rng, NULL);
  11086. if (ret != BAD_FUNC_ARG) {
  11087. ret = -6856;
  11088. goto exit;
  11089. }
  11090. ret = 0;
  11091. exit:
  11092. if (ret != 0)
  11093. ret += errorOffset;
  11094. return ret;
  11095. }
  11096. static int random_rng_test(void)
  11097. {
  11098. WC_RNG localRng;
  11099. WC_RNG* rng;
  11100. int ret;
  11101. rng = &localRng;
  11102. /* Test stack based RNG. */
  11103. #ifndef HAVE_FIPS
  11104. ret = wc_InitRng_ex(rng, HEAP_HINT, devId);
  11105. #else
  11106. ret = wc_InitRng(rng);
  11107. #endif
  11108. if (ret != 0) return -6900;
  11109. ret = _rng_test(rng, -6300);
  11110. /* Make sure and free RNG */
  11111. wc_FreeRng(rng);
  11112. if (ret != 0) return ret;
  11113. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC)
  11114. {
  11115. byte nonce[8] = { 0 };
  11116. /* Test dynamic RNG. */
  11117. rng = wc_rng_new(nonce, (word32)sizeof(nonce), HEAP_HINT);
  11118. if (rng == NULL) return -6901;
  11119. ret = _rng_test(rng, -6310);
  11120. wc_rng_free(rng);
  11121. }
  11122. #endif
  11123. return ret;
  11124. }
  11125. #if defined(HAVE_HASHDRBG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  11126. #ifdef WC_RNG_SEED_CB
  11127. static int seed_cb(OS_Seed* os, byte* output, word32 sz)
  11128. {
  11129. word32 i;
  11130. (void)os;
  11131. /* Known answer test. Set the seed to the same value every time. */
  11132. for (i = 0; i < sz; i++)
  11133. output[i] = (byte)i;
  11134. return 0;
  11135. }
  11136. static int rng_seed_test(void)
  11137. {
  11138. #ifndef HAVE_FIPS
  11139. WOLFSSL_SMALL_STACK_STATIC const byte check[] =
  11140. {
  11141. 0x83, 0x46, 0x65, 0x2f, 0x5c, 0x44, 0x16, 0x5f,
  11142. 0xb3, 0x89, 0x26, 0xde, 0x0b, 0x6b, 0xa2, 0x06,
  11143. 0x7e, 0xa7, 0x9a, 0x55, 0x22, 0x01, 0xb0, 0x22,
  11144. 0xf4, 0x7e, 0xa2, 0x66, 0xc4, 0x08, 0x6f, 0xba
  11145. };
  11146. #else
  11147. /* FIPS uses a longer seed, so different check value. */
  11148. WOLFSSL_SMALL_STACK_STATIC const byte check[] =
  11149. {
  11150. 0xaf, 0x31, 0xcc, 0xef, 0xa9, 0x29, 0x4c, 0x24,
  11151. 0xbd, 0xa5, 0xa3, 0x52, 0x69, 0xf3, 0xb9, 0xb2,
  11152. 0x1e, 0xd4, 0x52, 0x3b, 0x9a, 0x96, 0x06, 0x20,
  11153. 0xc0, 0x5f, 0x44, 0x06, 0x1f, 0x80, 0xdf, 0xe0
  11154. };
  11155. #endif
  11156. byte output[WC_SHA256_DIGEST_SIZE];
  11157. WC_RNG rng;
  11158. int ret;
  11159. ret = wc_SetSeed_Cb(seed_cb);
  11160. if (ret != 0) {
  11161. ret = -7007;
  11162. goto exit;
  11163. }
  11164. ret = wc_InitRng(&rng);
  11165. if (ret != 0) {
  11166. ret = -7008;
  11167. goto exit;
  11168. }
  11169. ret = wc_RNG_GenerateBlock(&rng, output, sizeof(output));
  11170. if (ret != 0) {
  11171. ret = -7009;
  11172. goto exit;
  11173. }
  11174. ret = XMEMCMP(output, check, sizeof(output));
  11175. if (ret != 0) {
  11176. ret = -7010;
  11177. goto exit;
  11178. }
  11179. ret = wc_FreeRng(&rng);
  11180. if (ret != 0) {
  11181. ret = -7011;
  11182. goto exit;
  11183. }
  11184. ret = wc_SetSeed_Cb(wc_GenerateSeed);
  11185. if (ret != 0) {
  11186. ret = -7012;
  11187. }
  11188. exit:
  11189. return ret;
  11190. }
  11191. #endif
  11192. WOLFSSL_TEST_SUBROUTINE int random_test(void)
  11193. {
  11194. WOLFSSL_SMALL_STACK_STATIC const byte test1Entropy[] =
  11195. {
  11196. 0xa6, 0x5a, 0xd0, 0xf3, 0x45, 0xdb, 0x4e, 0x0e, 0xff, 0xe8, 0x75, 0xc3,
  11197. 0xa2, 0xe7, 0x1f, 0x42, 0xc7, 0x12, 0x9d, 0x62, 0x0f, 0xf5, 0xc1, 0x19,
  11198. 0xa9, 0xef, 0x55, 0xf0, 0x51, 0x85, 0xe0, 0xfb, 0x85, 0x81, 0xf9, 0x31,
  11199. 0x75, 0x17, 0x27, 0x6e, 0x06, 0xe9, 0x60, 0x7d, 0xdb, 0xcb, 0xcc, 0x2e
  11200. };
  11201. WOLFSSL_SMALL_STACK_STATIC const byte test1Output[] =
  11202. {
  11203. 0xd3, 0xe1, 0x60, 0xc3, 0x5b, 0x99, 0xf3, 0x40, 0xb2, 0x62, 0x82, 0x64,
  11204. 0xd1, 0x75, 0x10, 0x60, 0xe0, 0x04, 0x5d, 0xa3, 0x83, 0xff, 0x57, 0xa5,
  11205. 0x7d, 0x73, 0xa6, 0x73, 0xd2, 0xb8, 0xd8, 0x0d, 0xaa, 0xf6, 0xa6, 0xc3,
  11206. 0x5a, 0x91, 0xbb, 0x45, 0x79, 0xd7, 0x3f, 0xd0, 0xc8, 0xfe, 0xd1, 0x11,
  11207. 0xb0, 0x39, 0x13, 0x06, 0x82, 0x8a, 0xdf, 0xed, 0x52, 0x8f, 0x01, 0x81,
  11208. 0x21, 0xb3, 0xfe, 0xbd, 0xc3, 0x43, 0xe7, 0x97, 0xb8, 0x7d, 0xbb, 0x63,
  11209. 0xdb, 0x13, 0x33, 0xde, 0xd9, 0xd1, 0xec, 0xe1, 0x77, 0xcf, 0xa6, 0xb7,
  11210. 0x1f, 0xe8, 0xab, 0x1d, 0xa4, 0x66, 0x24, 0xed, 0x64, 0x15, 0xe5, 0x1c,
  11211. 0xcd, 0xe2, 0xc7, 0xca, 0x86, 0xe2, 0x83, 0x99, 0x0e, 0xea, 0xeb, 0x91,
  11212. 0x12, 0x04, 0x15, 0x52, 0x8b, 0x22, 0x95, 0x91, 0x02, 0x81, 0xb0, 0x2d,
  11213. 0xd4, 0x31, 0xf4, 0xc9, 0xf7, 0x04, 0x27, 0xdf
  11214. };
  11215. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyA[] =
  11216. {
  11217. 0x63, 0x36, 0x33, 0x77, 0xe4, 0x1e, 0x86, 0x46, 0x8d, 0xeb, 0x0a, 0xb4,
  11218. 0xa8, 0xed, 0x68, 0x3f, 0x6a, 0x13, 0x4e, 0x47, 0xe0, 0x14, 0xc7, 0x00,
  11219. 0x45, 0x4e, 0x81, 0xe9, 0x53, 0x58, 0xa5, 0x69, 0x80, 0x8a, 0xa3, 0x8f,
  11220. 0x2a, 0x72, 0xa6, 0x23, 0x59, 0x91, 0x5a, 0x9f, 0x8a, 0x04, 0xca, 0x68
  11221. };
  11222. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyB[] =
  11223. {
  11224. 0xe6, 0x2b, 0x8a, 0x8e, 0xe8, 0xf1, 0x41, 0xb6, 0x98, 0x05, 0x66, 0xe3,
  11225. 0xbf, 0xe3, 0xc0, 0x49, 0x03, 0xda, 0xd4, 0xac, 0x2c, 0xdf, 0x9f, 0x22,
  11226. 0x80, 0x01, 0x0a, 0x67, 0x39, 0xbc, 0x83, 0xd3
  11227. };
  11228. WOLFSSL_SMALL_STACK_STATIC const byte test2Output[] =
  11229. {
  11230. 0x04, 0xee, 0xc6, 0x3b, 0xb2, 0x31, 0xdf, 0x2c, 0x63, 0x0a, 0x1a, 0xfb,
  11231. 0xe7, 0x24, 0x94, 0x9d, 0x00, 0x5a, 0x58, 0x78, 0x51, 0xe1, 0xaa, 0x79,
  11232. 0x5e, 0x47, 0x73, 0x47, 0xc8, 0xb0, 0x56, 0x62, 0x1c, 0x18, 0xbd, 0xdc,
  11233. 0xdd, 0x8d, 0x99, 0xfc, 0x5f, 0xc2, 0xb9, 0x20, 0x53, 0xd8, 0xcf, 0xac,
  11234. 0xfb, 0x0b, 0xb8, 0x83, 0x12, 0x05, 0xfa, 0xd1, 0xdd, 0xd6, 0xc0, 0x71,
  11235. 0x31, 0x8a, 0x60, 0x18, 0xf0, 0x3b, 0x73, 0xf5, 0xed, 0xe4, 0xd4, 0xd0,
  11236. 0x71, 0xf9, 0xde, 0x03, 0xfd, 0x7a, 0xea, 0x10, 0x5d, 0x92, 0x99, 0xb8,
  11237. 0xaf, 0x99, 0xaa, 0x07, 0x5b, 0xdb, 0x4d, 0xb9, 0xaa, 0x28, 0xc1, 0x8d,
  11238. 0x17, 0x4b, 0x56, 0xee, 0x2a, 0x01, 0x4d, 0x09, 0x88, 0x96, 0xff, 0x22,
  11239. 0x82, 0xc9, 0x55, 0xa8, 0x19, 0x69, 0xe0, 0x69, 0xfa, 0x8c, 0xe0, 0x07,
  11240. 0xa1, 0x80, 0x18, 0x3a, 0x07, 0xdf, 0xae, 0x17
  11241. };
  11242. byte output[WC_SHA256_DIGEST_SIZE * 4];
  11243. int ret;
  11244. ret = wc_RNG_HealthTest(0, test1Entropy, sizeof(test1Entropy), NULL, 0,
  11245. output, sizeof(output));
  11246. if (ret != 0)
  11247. return -7000;
  11248. if (XMEMCMP(test1Output, output, sizeof(output)) != 0)
  11249. return -7001;
  11250. ret = wc_RNG_HealthTest(1, test2EntropyA, sizeof(test2EntropyA),
  11251. test2EntropyB, sizeof(test2EntropyB),
  11252. output, sizeof(output));
  11253. if (ret != 0)
  11254. return -7002;
  11255. if (XMEMCMP(test2Output, output, sizeof(output)) != 0)
  11256. return -7003;
  11257. /* Basic RNG generate block test */
  11258. if ((ret = random_rng_test()) != 0)
  11259. return ret;
  11260. /* Test the seed check function. */
  11261. #if !(defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) || \
  11262. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  11263. {
  11264. word32 i, outputSz;
  11265. /* Repeat the same byte over and over. Should fail. */
  11266. outputSz = sizeof(output);
  11267. XMEMSET(output, 1, outputSz);
  11268. ret = wc_RNG_TestSeed(output, outputSz);
  11269. if (ret == 0)
  11270. return -7004;
  11271. /* Every byte of the entropy scratch is different,
  11272. * entropy is a single byte that shouldn't match. */
  11273. outputSz = (sizeof(output) / 2) + 1;
  11274. for (i = 0; i < outputSz; i++)
  11275. output[i] = (byte)i;
  11276. ret = wc_RNG_TestSeed(output, outputSz);
  11277. if (ret != 0)
  11278. return -7005;
  11279. outputSz = sizeof(output);
  11280. for (i = 0; i < outputSz; i++)
  11281. output[i] = (byte)i;
  11282. ret = wc_RNG_TestSeed(output, outputSz);
  11283. if (ret != 0)
  11284. return -7006;
  11285. }
  11286. #endif
  11287. /* Test the seed callback. */
  11288. #ifdef WC_RNG_SEED_CB
  11289. if ((ret = rng_seed_test()) != 0)
  11290. return ret;
  11291. #endif
  11292. return 0;
  11293. }
  11294. #else
  11295. WOLFSSL_TEST_SUBROUTINE int random_test(void)
  11296. {
  11297. /* Basic RNG generate block test */
  11298. return random_rng_test();
  11299. }
  11300. #endif /* HAVE_HASHDRBG && !CUSTOM_RAND_GENERATE_BLOCK */
  11301. #endif /* WC_NO_RNG */
  11302. #ifndef MEM_TEST_SZ
  11303. #define MEM_TEST_SZ 1024
  11304. #endif
  11305. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  11306. static int simple_mem_test(int sz)
  11307. {
  11308. int ret = 0;
  11309. byte* b;
  11310. int i;
  11311. b = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11312. if (b == NULL) {
  11313. return -7110;
  11314. }
  11315. /* utilize memory */
  11316. for (i = 0; i < sz; i++) {
  11317. b[i] = (byte)i;
  11318. }
  11319. /* read back and verify */
  11320. for (i = 0; i < sz; i++) {
  11321. if (b[i] != (byte)i) {
  11322. ret = -7111;
  11323. break;
  11324. }
  11325. }
  11326. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11327. return ret;
  11328. }
  11329. #endif
  11330. WOLFSSL_TEST_SUBROUTINE int memory_test(void)
  11331. {
  11332. int ret = 0;
  11333. #if defined(COMPLEX_MEM_TEST) || defined(WOLFSSL_STATIC_MEMORY)
  11334. int i;
  11335. #endif
  11336. #ifdef WOLFSSL_STATIC_MEMORY
  11337. word32 size[] = { WOLFMEM_BUCKETS };
  11338. word32 dist[] = { WOLFMEM_DIST };
  11339. byte buffer[30000]; /* make large enough to involve many bucket sizes */
  11340. int pad = -(int)((wc_ptr_t)buffer) & (WOLFSSL_STATIC_ALIGN - 1);
  11341. /* pad to account for if head of buffer is not at set memory
  11342. * alignment when tests are ran */
  11343. #endif
  11344. #ifdef WOLFSSL_STATIC_MEMORY
  11345. /* check macro settings */
  11346. if (sizeof(size)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
  11347. return -7200;
  11348. }
  11349. if (sizeof(dist)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
  11350. return -7201;
  11351. }
  11352. for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
  11353. if ((size[i] % WOLFSSL_STATIC_ALIGN) != 0) {
  11354. /* each element in array should be divisible by alignment size */
  11355. return -7202;
  11356. }
  11357. }
  11358. for (i = 1; i < WOLFMEM_MAX_BUCKETS; i++) {
  11359. if (size[i - 1] >= size[i]) {
  11360. return -7203; /* sizes should be in increasing order */
  11361. }
  11362. }
  11363. /* check that padding size returned is possible */
  11364. if (wolfSSL_MemoryPaddingSz() < WOLFSSL_STATIC_ALIGN) {
  11365. return -7204; /* no room for wc_Memory struct */
  11366. }
  11367. if (wolfSSL_MemoryPaddingSz() < 0) {
  11368. return -7205;
  11369. }
  11370. if (wolfSSL_MemoryPaddingSz() % WOLFSSL_STATIC_ALIGN != 0) {
  11371. return -7206; /* not aligned! */
  11372. }
  11373. /* check function to return optimum buffer size (rounded down) */
  11374. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_GENERAL);
  11375. if ((ret - pad) % WOLFSSL_STATIC_ALIGN != 0) {
  11376. return -7207; /* not aligned! */
  11377. }
  11378. if (ret < 0) {
  11379. return -7208;
  11380. }
  11381. if ((unsigned int)ret > sizeof(buffer)) {
  11382. return -7209; /* did not round down as expected */
  11383. }
  11384. if (ret != wolfSSL_StaticBufferSz(buffer, ret, WOLFMEM_GENERAL)) {
  11385. return -7210; /* return value changed when using suggested value */
  11386. }
  11387. ret = wolfSSL_MemoryPaddingSz();
  11388. ret += pad; /* add space that is going to be needed if buffer not aligned */
  11389. if (wolfSSL_StaticBufferSz(buffer, size[0] + ret + 1, WOLFMEM_GENERAL) !=
  11390. (ret + (int)size[0])) {
  11391. return -7211; /* did not round down to nearest bucket value */
  11392. }
  11393. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_IO_POOL);
  11394. if ((ret - pad) < 0) {
  11395. return -7212;
  11396. }
  11397. if (((ret - pad) % (WOLFMEM_IO_SZ + wolfSSL_MemoryPaddingSz())) != 0) {
  11398. return -7213; /* not even chunks of memory for IO size */
  11399. }
  11400. if (((ret - pad) % WOLFSSL_STATIC_ALIGN) != 0) {
  11401. return -7214; /* memory not aligned */
  11402. }
  11403. /* check for passing bad or unknown arguments to functions */
  11404. if (wolfSSL_StaticBufferSz(NULL, 1, WOLFMEM_GENERAL) > 0) {
  11405. return -7215;
  11406. }
  11407. if (wolfSSL_StaticBufferSz(buffer, 1, WOLFMEM_GENERAL) != 0) {
  11408. return -7216; /* should round to 0 since struct + bucket will not fit */
  11409. }
  11410. (void)dist; /* avoid static analysis warning of variable not used */
  11411. #endif
  11412. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  11413. /* simple test */
  11414. ret = simple_mem_test(MEM_TEST_SZ);
  11415. if (ret != 0)
  11416. return ret;
  11417. #endif
  11418. #ifdef COMPLEX_MEM_TEST
  11419. /* test various size blocks */
  11420. for (i = 1; i < MEM_TEST_SZ; i*=2) {
  11421. ret = simple_mem_test(i);
  11422. if (ret != 0)
  11423. return ret;
  11424. }
  11425. #endif
  11426. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_NO_MALLOC)
  11427. /* realloc test */
  11428. {
  11429. byte *c = NULL;
  11430. byte *b = (byte*)XMALLOC(MEM_TEST_SZ, HEAP_HINT,
  11431. DYNAMIC_TYPE_TMP_BUFFER);
  11432. if (b) {
  11433. c = (byte*)XREALLOC(b, MEM_TEST_SZ+sizeof(word32), HEAP_HINT,
  11434. DYNAMIC_TYPE_TMP_BUFFER);
  11435. if (c)
  11436. b = c;
  11437. }
  11438. if (b)
  11439. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11440. if ((b == NULL) || (c == NULL)) {
  11441. return -7217;
  11442. }
  11443. }
  11444. #endif
  11445. return ret;
  11446. }
  11447. #ifndef NO_FILESYSTEM
  11448. /* Cert Paths */
  11449. #ifdef FREESCALE_MQX
  11450. #define CERT_PREFIX "a:\\"
  11451. #define CERT_PATH_SEP "\\"
  11452. #elif defined(WOLFSSL_uTKERNEL2)
  11453. #define CERT_PREFIX "/uda/"
  11454. #define CERT_PATH_SEP "/"
  11455. #elif defined(_WIN32_WCE)
  11456. #define CERT_PREFIX "\\windows\\"
  11457. #define CERT_PATH_SEP "\\"
  11458. #endif
  11459. #ifndef CERT_PREFIX
  11460. #define CERT_PREFIX "./"
  11461. #endif
  11462. #ifndef CERT_PATH_SEP
  11463. #define CERT_PATH_SEP "/"
  11464. #endif
  11465. #ifndef CERT_WRITE_TEMP_DIR
  11466. #define CERT_WRITE_TEMP_DIR CERT_PREFIX
  11467. #endif
  11468. #define CERT_ROOT CERT_PREFIX "certs" CERT_PATH_SEP
  11469. /* Generated Test Certs */
  11470. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  11471. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  11472. #if !defined(NO_RSA) && !defined(NO_ASN)
  11473. static const char* clientKey = CERT_ROOT "client-key.der";
  11474. static const char* clientCert = CERT_ROOT "client-cert.der";
  11475. #ifdef WOLFSSL_CERT_EXT
  11476. static const char* clientKeyPub = CERT_ROOT "client-keyPub.der";
  11477. #endif
  11478. #endif /* !NO_RSA && !NO_ASN */
  11479. #endif
  11480. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  11481. #if !defined(NO_RSA) && !defined(NO_ASN)
  11482. #if defined(WOLFSSL_CERT_GEN) || defined(HAVE_PKCS7)
  11483. static const char* rsaCaKeyFile = CERT_ROOT "ca-key.der";
  11484. #ifdef WOLFSSL_CERT_GEN
  11485. static const char* rsaCaCertFile = CERT_ROOT "ca-cert.pem";
  11486. #endif
  11487. #if defined(WOLFSSL_ALT_NAMES) || defined(HAVE_PKCS7)
  11488. static const char* rsaCaCertDerFile = CERT_ROOT "ca-cert.der";
  11489. #endif
  11490. #ifdef HAVE_PKCS7
  11491. static const char* rsaServerCertDerFile =
  11492. CERT_ROOT "server-cert.der";
  11493. static const char* rsaServerKeyDerFile =
  11494. CERT_ROOT "server-key.der";
  11495. #endif
  11496. #endif
  11497. #endif /* !NO_RSA && !NO_ASN */
  11498. #endif /* !USE_CERT_BUFFER_* */
  11499. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  11500. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  11501. #if !defined(NO_ASN) && !defined(NO_DH)
  11502. static const char* dhParamsFile = CERT_ROOT "dh2048.der";
  11503. #endif
  11504. #endif
  11505. #if !defined(NO_ASN) && !defined(NO_DH)
  11506. #if defined(WOLFSSL_DH_EXTRA) && (!defined(HAVE_FIPS) || \
  11507. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  11508. static const char* dhKeyFile = CERT_ROOT "statickeys/dh-ffdhe2048.der";
  11509. static const char* dhKeyPubFile = CERT_ROOT "statickeys/dh-ffdhe2048-pub.der";
  11510. #endif
  11511. #endif
  11512. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  11513. #ifndef NO_DSA
  11514. static const char* dsaKey = CERT_ROOT "dsa2048.der";
  11515. #endif
  11516. #endif /* !USE_CERT_BUFFER_* */
  11517. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ECC256)
  11518. #ifdef HAVE_ECC
  11519. /* cert files to be used in rsa cert gen test, check if RSA enabled */
  11520. #ifdef HAVE_ECC_KEY_IMPORT
  11521. static const char* eccKeyDerFile = CERT_ROOT "ecc-key.der";
  11522. #endif
  11523. #endif
  11524. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ASN)
  11525. #if defined(HAVE_ECC) && defined(WOLFSSL_CERT_GEN)
  11526. #ifndef NO_RSA
  11527. static const char* eccKeyPubFileDer = CERT_ROOT "ecc-keyPub.der";
  11528. #endif
  11529. static const char* eccCaKeyFile = CERT_ROOT "ca-ecc-key.der";
  11530. static const char* eccCaCertFile = CERT_ROOT "ca-ecc-cert.pem";
  11531. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  11532. static const char* eccCaKey384File =
  11533. CERT_ROOT "ca-ecc384-key.der";
  11534. static const char* eccCaCert384File =
  11535. CERT_ROOT "ca-ecc384-cert.pem";
  11536. #endif
  11537. #endif
  11538. #if defined(HAVE_PKCS7) && defined(HAVE_ECC)
  11539. static const char* eccClientKey = CERT_ROOT "ecc-client-key.der";
  11540. static const char* eccClientCert = CERT_ROOT "client-ecc-cert.der";
  11541. #endif
  11542. #endif /* HAVE_ECC */
  11543. #ifdef HAVE_ED25519
  11544. #ifdef WOLFSSL_TEST_CERT
  11545. static const char* serverEd25519Cert =
  11546. CERT_ROOT "ed25519/server-ed25519.der";
  11547. static const char* caEd25519Cert =
  11548. CERT_ROOT "ed25519/ca-ed25519.der";
  11549. #endif
  11550. #endif
  11551. #ifdef HAVE_ED448
  11552. #ifdef WOLFSSL_TEST_CERT
  11553. static const char* serverEd448Cert =
  11554. CERT_ROOT "ed448/server-ed448.der";
  11555. static const char* caEd448Cert = CERT_ROOT "ed448/ca-ed448.der";
  11556. #endif
  11557. #endif
  11558. #endif /* !USE_CERT_BUFFER_* */
  11559. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  11560. !defined(NO_FILESYSTEM)
  11561. static const char* certExtNc =
  11562. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-nc.der";
  11563. static const char* certExtIa =
  11564. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-ia.der";
  11565. static const char* certExtNct =
  11566. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-nct.der";
  11567. #endif
  11568. #ifndef NO_WRITE_TEMP_FILES
  11569. #ifdef HAVE_ECC
  11570. #ifdef WOLFSSL_CERT_GEN
  11571. static const char* certEccPemFile = CERT_WRITE_TEMP_DIR "certecc.pem";
  11572. static const char* certEccDerFile = CERT_WRITE_TEMP_DIR "certecc.der";
  11573. #endif
  11574. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  11575. static const char* certEccRsaPemFile = CERT_WRITE_TEMP_DIR "certeccrsa.pem";
  11576. static const char* certEccRsaDerFile = CERT_WRITE_TEMP_DIR "certeccrsa.der";
  11577. #endif
  11578. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  11579. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  11580. static const char* eccCaKeyPemFile = CERT_WRITE_TEMP_DIR "ecc-key.pem";
  11581. static const char* eccPubKeyDerFile = CERT_WRITE_TEMP_DIR "ecc-public-key.der";
  11582. static const char* eccCaKeyTempFile = CERT_WRITE_TEMP_DIR "ecc-key.der";
  11583. #if defined(HAVE_PKCS8) && !defined(WC_NO_RNG) && \
  11584. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  11585. static const char* eccPkcs8KeyDerFile = CERT_WRITE_TEMP_DIR "ecc-key-pkcs8.der";
  11586. #endif
  11587. #endif /* HAVE_ECC_KEY_EXPORT */
  11588. #endif /* HAVE_ECC */
  11589. #ifndef NO_RSA
  11590. #ifdef WOLFSSL_CERT_GEN
  11591. static const char* otherCertDerFile = CERT_WRITE_TEMP_DIR "othercert.der";
  11592. static const char* certDerFile = CERT_WRITE_TEMP_DIR "cert.der";
  11593. static const char* otherCertPemFile = CERT_WRITE_TEMP_DIR "othercert.pem";
  11594. static const char* certPemFile = CERT_WRITE_TEMP_DIR "cert.pem";
  11595. #if defined(WOLFSSL_CERT_REQ) && !defined(WOLFSSL_NO_MALLOC)
  11596. static const char* certReqDerFile = CERT_WRITE_TEMP_DIR "certreq.der";
  11597. static const char* certReqPemFile = CERT_WRITE_TEMP_DIR "certreq.pem";
  11598. #endif
  11599. #endif
  11600. #endif /* !NO_RSA */
  11601. #if !defined(NO_RSA) || !defined(NO_DSA)
  11602. #ifdef WOLFSSL_KEY_GEN
  11603. static const char* keyDerFile = CERT_WRITE_TEMP_DIR "key.der";
  11604. static const char* keyPemFile = CERT_WRITE_TEMP_DIR "key.pem";
  11605. #endif
  11606. #endif
  11607. #endif /* !NO_WRITE_TEMP_FILES */
  11608. #endif /* !NO_FILESYSTEM */
  11609. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  11610. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  11611. static CertName certDefaultName;
  11612. static void initDefaultName(void)
  11613. {
  11614. #if defined(WOLFSSL_MULTI_ATTRIB) && defined(WOLFSSL_TEST_CERT)
  11615. NameAttrib* n;
  11616. #endif
  11617. XMEMCPY(certDefaultName.country, "US", sizeof("US"));
  11618. certDefaultName.countryEnc = CTC_PRINTABLE;
  11619. XMEMCPY(certDefaultName.state, "Oregon", sizeof("Oregon"));
  11620. certDefaultName.stateEnc = CTC_UTF8;
  11621. XMEMCPY(certDefaultName.street, "Main St", sizeof("Main St"));
  11622. certDefaultName.streetEnc = CTC_UTF8;
  11623. XMEMCPY(certDefaultName.locality, "Portland", sizeof("Portland"));
  11624. certDefaultName.localityEnc = CTC_UTF8;
  11625. XMEMCPY(certDefaultName.sur, "Test", sizeof("Test"));
  11626. certDefaultName.surEnc = CTC_UTF8;
  11627. XMEMCPY(certDefaultName.org, "wolfSSL", sizeof("wolfSSL"));
  11628. certDefaultName.orgEnc = CTC_UTF8;
  11629. XMEMCPY(certDefaultName.unit, "Development", sizeof("Development"));
  11630. certDefaultName.unitEnc = CTC_UTF8;
  11631. XMEMCPY(certDefaultName.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  11632. certDefaultName.commonNameEnc = CTC_UTF8;
  11633. XMEMCPY(certDefaultName.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  11634. certDefaultName.serialDevEnc = CTC_PRINTABLE;
  11635. XMEMCPY(certDefaultName.postalCode, "12-456", sizeof("12-456"));
  11636. certDefaultName.postalCodeEnc = CTC_PRINTABLE;
  11637. #ifdef WOLFSSL_CERT_EXT
  11638. XMEMCPY(certDefaultName.busCat, "Private Organization", sizeof("Private Organization"));
  11639. certDefaultName.busCatEnc = CTC_UTF8;
  11640. XMEMCPY(certDefaultName.joiSt, "US", sizeof("US"));
  11641. certDefaultName.joiStEnc = CTC_PRINTABLE;
  11642. XMEMCPY(certDefaultName.joiC, "Oregon", sizeof("Oregon"));
  11643. certDefaultName.joiCEnc = CTC_PRINTABLE;
  11644. #endif
  11645. XMEMCPY(certDefaultName.email, "info@wolfssl.com", sizeof("info@wolfssl.com"));
  11646. XMEMCPY(certDefaultName.userId, "TestUserID", sizeof("TestUserID"));
  11647. certDefaultName.userIdEnc = CTC_PRINTABLE;
  11648. #if defined(WOLFSSL_MULTI_ATTRIB) && defined(WOLFSSL_TEST_CERT)
  11649. /* test having additional OUs and setting DC */
  11650. n = &certDefaultName.name[0];
  11651. n->id = ASN_ORGUNIT_NAME;
  11652. n->type = CTC_UTF8;
  11653. n->sz = sizeof("Development-2");
  11654. XMEMCPY(n->value, "Development-2", sizeof("Development-2"));
  11655. #if CTC_MAX_ATTRIB > 3
  11656. n = &certDefaultName.name[1];
  11657. n->id = ASN_DOMAIN_COMPONENT;
  11658. n->type = CTC_UTF8;
  11659. n->sz = sizeof("com");
  11660. XMEMCPY(n->value, "com", sizeof("com"));
  11661. n = &certDefaultName.name[2];
  11662. n->id = ASN_DOMAIN_COMPONENT;
  11663. n->type = CTC_UTF8;
  11664. n->sz = sizeof("wolfssl");
  11665. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  11666. #endif
  11667. #endif /* WOLFSSL_MULTI_ATTRIB && WOLFSSL_TEST_CERT */
  11668. #ifdef WOLFSSL_CUSTOM_OID
  11669. /* TODO: Add test case for custom OID's */
  11670. #endif
  11671. }
  11672. #ifdef WOLFSSL_CERT_EXT
  11673. #if ((defined(HAVE_ED25519) || defined(HAVE_ED448)) && \
  11674. defined(WOLFSSL_TEST_CERT)) || defined(HAVE_ECC)
  11675. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage[] =
  11676. "digitalSignature,nonRepudiation";
  11677. #endif
  11678. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  11679. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage2[] =
  11680. "digitalSignature,nonRepudiation,keyEncipherment,keyAgreement";
  11681. #endif
  11682. #endif /* WOLFSSL_CERT_EXT */
  11683. #endif /* WOLFSSL_CERT_GEN */
  11684. #ifndef NO_RSA
  11685. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  11686. !defined(NO_FILESYSTEM)
  11687. static byte minSerial[] = { 0x02, 0x01, 0x01 };
  11688. static byte minName[] = { 0x30, 0x00 };
  11689. static byte nameBad[] = {
  11690. 0x30, 0x08,
  11691. 0x31, 0x06,
  11692. 0x30, 0x04,
  11693. 0x06, 0x02,
  11694. 0x55, 0x04,
  11695. };
  11696. static byte minDates[] = {
  11697. 0x30, 0x1e,
  11698. 0x17, 0x0d,
  11699. 0x31, 0x38, 0x30, 0x34, 0x31, 0x33, 0x31, 0x35,
  11700. 0x32, 0x33, 0x31, 0x30, 0x5a,
  11701. 0x17, 0x0d,
  11702. 0x32, 0x31, 0x30, 0x31, 0x30, 0x37, 0x31, 0x35,
  11703. 0x32, 0x33, 0x31, 0x30, 0x5a
  11704. };
  11705. static byte minPubKey[] = {
  11706. 0x30, 0x1c,
  11707. 0x30, 0x0d,
  11708. 0x06, 0x09,
  11709. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  11710. 0x01,
  11711. 0x05, 0x00,
  11712. 0x03, 0x0b,
  11713. 0x00, 0x30, 0x08,
  11714. 0x02, 0x01,
  11715. 0x03,
  11716. 0x02, 0x03,
  11717. 0x01, 0x00, 0x01
  11718. };
  11719. static byte minSigAlg[] = {
  11720. 0x30, 0x0d,
  11721. 0x06, 0x09,
  11722. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  11723. 0x0b,
  11724. 0x05, 0x00
  11725. };
  11726. static byte minSig[] = {
  11727. 0x03, 0x01,
  11728. 0x00
  11729. };
  11730. static int add_seq(byte* certData, int offset, byte* data, byte length)
  11731. {
  11732. XMEMMOVE(certData + offset + 2, data, length);
  11733. certData[offset++] = 0x30;
  11734. certData[offset++] = length;
  11735. return offset + length;
  11736. }
  11737. static int add_data(byte* certData, int offset, byte* data, byte length)
  11738. {
  11739. XMEMCPY(certData + offset, data, length);
  11740. return offset + length;
  11741. }
  11742. static int cert_asn1_test(void)
  11743. {
  11744. int ret;
  11745. int len[3];
  11746. DecodedCert cert;
  11747. byte certData[114];
  11748. byte* badCert = NULL;
  11749. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  11750. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  11751. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  11752. len[2] = add_data(certData, len[2], minDates, (byte)sizeof(minDates));
  11753. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  11754. len[2] = add_data(certData, len[2], minPubKey, (byte)sizeof(minPubKey));
  11755. len[1] = add_seq(certData, 0, certData, len[2]);
  11756. len[1] = add_data(certData, len[1], minSigAlg, (byte)sizeof(minSigAlg));
  11757. len[1] = add_data(certData, len[1], minSig, (byte)sizeof(minSig));
  11758. len[0] = add_seq(certData, 0, certData, len[1]);
  11759. /* Minimal good certificate */
  11760. InitDecodedCert(&cert, certData, len[0], 0);
  11761. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  11762. FreeDecodedCert(&cert);
  11763. if (ret != 0) {
  11764. ERROR_OUT(-7300, done);
  11765. }
  11766. /* Bad issuer name */
  11767. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  11768. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  11769. len[2] = add_data(certData, len[2], nameBad, (byte)sizeof(nameBad));
  11770. len[2] = add_data(certData, len[2], minDates, (byte)sizeof(minDates));
  11771. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  11772. len[2] = add_data(certData, len[2], minPubKey, (byte)sizeof(minPubKey));
  11773. len[1] = add_seq(certData, 0, certData, len[2]);
  11774. len[1] = add_data(certData, len[1], minSigAlg, (byte)sizeof(minSigAlg));
  11775. len[1] = add_data(certData, len[1], minSig, (byte)sizeof(minSig));
  11776. len[0] = add_seq(certData, 0, certData, len[1]);
  11777. /* Put data into allocated buffer to allow access error checking. */
  11778. badCert = (byte*)XMALLOC(len[0], HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11779. XMEMCPY(badCert, certData, len[0]);
  11780. InitDecodedCert(&cert, badCert, len[0], 0);
  11781. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  11782. FreeDecodedCert(&cert);
  11783. if (ret != ASN_PARSE_E) {
  11784. ERROR_OUT(-7301, done);
  11785. }
  11786. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11787. badCert = NULL;
  11788. ret = 0;
  11789. done:
  11790. if (badCert != NULL)
  11791. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11792. return ret;
  11793. }
  11794. WOLFSSL_TEST_SUBROUTINE int cert_test(void)
  11795. {
  11796. #if !defined(NO_FILESYSTEM)
  11797. DecodedCert cert;
  11798. byte* tmp;
  11799. size_t bytes;
  11800. XFILE file;
  11801. int ret;
  11802. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11803. if (tmp == NULL)
  11804. return -7400;
  11805. /* Certificate with Name Constraints extension. */
  11806. file = XFOPEN(certExtNc, "rb");
  11807. if (!file) {
  11808. ERROR_OUT(-7401, done);
  11809. }
  11810. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11811. XFCLOSE(file);
  11812. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11813. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  11814. if (ret != 0) {
  11815. ERROR_OUT(-7402, done);
  11816. }
  11817. FreeDecodedCert(&cert);
  11818. /* Certificate with Inhibit Any Policy extension. */
  11819. file = XFOPEN(certExtIa, "rb");
  11820. if (!file) {
  11821. ERROR_OUT(-7403, done);
  11822. }
  11823. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11824. XFCLOSE(file);
  11825. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11826. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  11827. if (ret != 0) {
  11828. ERROR_OUT(-7404, done);
  11829. }
  11830. FreeDecodedCert(&cert);
  11831. /* Certificate with Netscape Certificate Type extension. */
  11832. file = XFOPEN(certExtNct, "rb");
  11833. if (!file) {
  11834. ERROR_OUT(-7405, done);
  11835. }
  11836. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11837. XFCLOSE(file);
  11838. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11839. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  11840. #ifndef IGNORE_NETSCAPE_CERT_TYPE
  11841. if (ret != 0) {
  11842. ERROR_OUT(-7406, done);
  11843. }
  11844. #else
  11845. if (ret != ASN_CRIT_EXT_E) {
  11846. ERROR_OUT(-7407, done);
  11847. }
  11848. ret = 0;
  11849. #endif
  11850. done:
  11851. FreeDecodedCert(&cert);
  11852. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11853. #endif /* !NO_FILESYSTEM */
  11854. if (ret == 0)
  11855. ret = cert_asn1_test();
  11856. return ret;
  11857. }
  11858. #endif /* WOLFSSL_TEST_CERT */
  11859. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  11860. !defined(NO_FILESYSTEM) && defined(WOLFSSL_CERT_GEN)
  11861. WOLFSSL_TEST_SUBROUTINE int certext_test(void)
  11862. {
  11863. DecodedCert cert;
  11864. byte* tmp;
  11865. size_t bytes;
  11866. XFILE file;
  11867. int ret;
  11868. /* created from rsa_test : othercert.der */
  11869. byte skid_rsa[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  11870. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  11871. /* created from rsa_test : othercert.der */
  11872. byte akid_rsa[] = "\x27\x8E\x67\x11\x74\xC3\x26\x1D\x3F\xED"
  11873. "\x33\x63\xB3\xA4\xD8\x1D\x30\xE5\xE8\xD5";
  11874. #ifdef HAVE_ECC
  11875. /* created from ecc_test_cert_gen : certecc.der */
  11876. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  11877. /* Authority key id from ./certs/ca-ecc384-cert.pem */
  11878. byte akid_ecc[] = "\xAB\xE0\xC3\x26\x4C\x18\xD4\x72\xBB\xD2"
  11879. "\x84\x8C\x9C\x0A\x05\x92\x80\x12\x53\x52";
  11880. #else
  11881. /* Authority key id from ./certs/ca-ecc-cert.pem */
  11882. byte akid_ecc[] = "\x56\x8E\x9A\xC3\xF0\x42\xDE\x18\xB9\x45"
  11883. "\x55\x6E\xF9\x93\xCF\xEA\xC3\xF3\xA5\x21";
  11884. #endif
  11885. #endif /* HAVE_ECC */
  11886. /* created from rsa_test : cert.der */
  11887. byte kid_ca[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  11888. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  11889. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11890. if (tmp == NULL)
  11891. return -7500;
  11892. /* load othercert.der (Cert signed by an authority) */
  11893. file = XFOPEN(otherCertDerFile, "rb");
  11894. if (!file) {
  11895. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  11896. return -7501;
  11897. }
  11898. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11899. XFCLOSE(file);
  11900. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11901. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  11902. if (ret != 0)
  11903. return -7502;
  11904. /* check the SKID from a RSA certificate */
  11905. if (XMEMCMP(skid_rsa, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  11906. return -7503;
  11907. /* check the AKID from an RSA certificate */
  11908. if (XMEMCMP(akid_rsa, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  11909. return -7504;
  11910. /* check the Key Usage from an RSA certificate */
  11911. if (!cert.extKeyUsageSet)
  11912. return -7505;
  11913. if (cert.extKeyUsage != (KEYUSE_KEY_ENCIPHER|KEYUSE_KEY_AGREE))
  11914. return -7506;
  11915. /* check the CA Basic Constraints from an RSA certificate */
  11916. if (cert.isCA)
  11917. return -7507;
  11918. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  11919. /* check the Certificate Policies Id */
  11920. if (cert.extCertPoliciesNb != 1)
  11921. return -7508;
  11922. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  11923. return -7509;
  11924. #endif
  11925. FreeDecodedCert(&cert);
  11926. #ifdef HAVE_ECC
  11927. /* load certecc.der (Cert signed by our ECC CA test in ecc_test_cert_gen) */
  11928. file = XFOPEN(certEccDerFile, "rb");
  11929. if (!file) {
  11930. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  11931. return -7510;
  11932. }
  11933. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11934. XFCLOSE(file);
  11935. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11936. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  11937. if (ret != 0)
  11938. return -7511;
  11939. /* check the SKID from a ECC certificate - generated dynamically */
  11940. /* check the AKID from an ECC certificate */
  11941. if (XMEMCMP(akid_ecc, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  11942. return -7512;
  11943. /* check the Key Usage from an ECC certificate */
  11944. if (!cert.extKeyUsageSet)
  11945. return -7513;
  11946. if (cert.extKeyUsage != (KEYUSE_DIGITAL_SIG|KEYUSE_CONTENT_COMMIT))
  11947. return -7514;
  11948. /* check the CA Basic Constraints from an ECC certificate */
  11949. if (cert.isCA)
  11950. return -7515;
  11951. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  11952. /* check the Certificate Policies Id */
  11953. if (cert.extCertPoliciesNb != 2)
  11954. return -7516;
  11955. if (strncmp(cert.extCertPolicies[0], "2.4.589440.587.101.2.1.9632587.1", 32))
  11956. return -7517;
  11957. if (strncmp(cert.extCertPolicies[1], "1.2.13025.489.1.113549", 22))
  11958. return -7518;
  11959. #endif
  11960. FreeDecodedCert(&cert);
  11961. #endif /* HAVE_ECC */
  11962. /* load cert.der (self signed certificate) */
  11963. file = XFOPEN(certDerFile, "rb");
  11964. if (!file) {
  11965. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  11966. return -7519;
  11967. }
  11968. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11969. XFCLOSE(file);
  11970. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11971. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  11972. if (ret != 0)
  11973. return -7520;
  11974. /* check the SKID from a CA certificate */
  11975. if (XMEMCMP(kid_ca, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  11976. return -7521;
  11977. /* check the AKID from an CA certificate */
  11978. if (XMEMCMP(kid_ca, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  11979. return -7522;
  11980. /* check the Key Usage from CA certificate */
  11981. if (!cert.extKeyUsageSet)
  11982. return -7523;
  11983. if (cert.extKeyUsage != (KEYUSE_KEY_CERT_SIGN|KEYUSE_CRL_SIGN))
  11984. return -7524;
  11985. /* check the CA Basic Constraints CA certificate */
  11986. if (!cert.isCA)
  11987. return -7525;
  11988. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  11989. /* check the Certificate Policies Id */
  11990. if (cert.extCertPoliciesNb != 2)
  11991. return -7526;
  11992. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  11993. return -7527;
  11994. if (strncmp(cert.extCertPolicies[1], "1.2.840.113549.1.9.16.6.5", 25))
  11995. return -7528;
  11996. #endif
  11997. FreeDecodedCert(&cert);
  11998. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  11999. return 0;
  12000. }
  12001. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_TEST_CERT &&
  12002. !NO_FILESYSTEM && WOLFSSL_CERT_GEN */
  12003. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  12004. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  12005. WOLFSSL_TEST_SUBROUTINE int decodedCertCache_test(void)
  12006. {
  12007. int ret = 0;
  12008. Cert cert;
  12009. FILE* file;
  12010. byte* der;
  12011. word32 derSz;
  12012. derSz = FOURK_BUF;
  12013. der = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12014. if (der == NULL)
  12015. ret = -7600;
  12016. if (ret == 0) {
  12017. /* load cert.der */
  12018. file = XFOPEN(certDerFile, "rb");
  12019. if (file != NULL) {
  12020. derSz = (word32)XFREAD(der, 1, FOURK_BUF, file);
  12021. XFCLOSE(file);
  12022. }
  12023. else
  12024. ret = -7601;
  12025. }
  12026. if (ret == 0) {
  12027. if (wc_InitCert_ex(&cert, HEAP_HINT, devId)) {
  12028. ret = -7602;
  12029. }
  12030. }
  12031. if (ret == 0) {
  12032. ret = wc_SetSubjectBuffer(&cert, der, derSz);
  12033. }
  12034. if (ret == 0) {
  12035. if(wc_SetSubjectBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  12036. ret = -7603;
  12037. }
  12038. if (ret == 0) {
  12039. if (wc_SetSubjectRaw(&cert, der, derSz) != 0)
  12040. ret = -7604;
  12041. }
  12042. if (ret == 0) {
  12043. if(wc_SetSubjectRaw(NULL, der, derSz) != BAD_FUNC_ARG)
  12044. ret = -7605;
  12045. }
  12046. if (ret == 0) {
  12047. if(wc_SetIssuerBuffer(&cert, der, derSz) != 0)
  12048. ret = -7606;
  12049. }
  12050. if (ret == 0) {
  12051. if(wc_SetIssuerBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  12052. ret = -7607;
  12053. }
  12054. if (ret == 0) {
  12055. if(wc_SetIssuerRaw(&cert, der, derSz) != 0)
  12056. ret = -7608;
  12057. }
  12058. if (ret == 0) {
  12059. if(wc_SetIssuerRaw(NULL, der, derSz) != BAD_FUNC_ARG)
  12060. ret = -7609;
  12061. }
  12062. #ifdef WOLFSSL_ALT_NAMES
  12063. if (ret == 0) {
  12064. if(wc_SetAltNamesBuffer(&cert, der, derSz) != 0)
  12065. ret = -7610;
  12066. }
  12067. if (ret == 0) {
  12068. if(wc_SetAltNamesBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  12069. ret = -7611;
  12070. }
  12071. if (ret == 0) {
  12072. if(wc_SetDatesBuffer(&cert, der, derSz) != 0)
  12073. ret = -7612;
  12074. }
  12075. if (ret == 0) {
  12076. if(wc_SetDatesBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  12077. ret = -7613;
  12078. }
  12079. #endif
  12080. if (ret == 0) {
  12081. if(wc_SetAuthKeyIdFromCert(&cert, der, derSz) != 0)
  12082. ret = -7614;
  12083. }
  12084. if (ret == 0) {
  12085. if(wc_SetAuthKeyIdFromCert(NULL, der, derSz) != BAD_FUNC_ARG)
  12086. ret = -7615;
  12087. }
  12088. wc_SetCert_Free(&cert);
  12089. if (ret == 0) {
  12090. if(cert.decodedCert != NULL)
  12091. ret = -7616;
  12092. }
  12093. XFREE(der, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  12094. return ret;
  12095. }
  12096. #endif /* defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) &&
  12097. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) */
  12098. #define RSA_TEST_BYTES 512 /* up to 4096-bit key */
  12099. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  12100. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  12101. static int rsa_flatten_test(RsaKey* key)
  12102. {
  12103. int ret;
  12104. byte e[RSA_TEST_BYTES];
  12105. byte n[RSA_TEST_BYTES];
  12106. word32 eSz = sizeof(e);
  12107. word32 nSz = sizeof(n);
  12108. /* Parameter Validation testing. */
  12109. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  12110. #ifdef HAVE_USER_RSA
  12111. /* Implementation using IPP Libraries returns:
  12112. * -101 = USER_CRYPTO_ERROR
  12113. */
  12114. if (ret == 0)
  12115. #else
  12116. if (ret != BAD_FUNC_ARG)
  12117. #endif
  12118. return -7620;
  12119. ret = wc_RsaFlattenPublicKey(key, NULL, &eSz, n, &nSz);
  12120. #ifdef HAVE_USER_RSA
  12121. /* Implementation using IPP Libraries returns:
  12122. * -101 = USER_CRYPTO_ERROR
  12123. */
  12124. if (ret == 0)
  12125. #else
  12126. if (ret != BAD_FUNC_ARG)
  12127. #endif
  12128. return -7621;
  12129. ret = wc_RsaFlattenPublicKey(key, e, NULL, n, &nSz);
  12130. #ifdef HAVE_USER_RSA
  12131. /* Implementation using IPP Libraries returns:
  12132. * -101 = USER_CRYPTO_ERROR
  12133. */
  12134. if (ret == 0)
  12135. #else
  12136. if (ret != BAD_FUNC_ARG)
  12137. #endif
  12138. return -7622;
  12139. ret = wc_RsaFlattenPublicKey(key, e, &eSz, NULL, &nSz);
  12140. #ifdef HAVE_USER_RSA
  12141. /* Implementation using IPP Libraries returns:
  12142. * -101 = USER_CRYPTO_ERROR
  12143. */
  12144. if (ret == 0)
  12145. #else
  12146. if (ret != BAD_FUNC_ARG)
  12147. #endif
  12148. return -7623;
  12149. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, NULL);
  12150. #ifdef HAVE_USER_RSA
  12151. /* Implementation using IPP Libraries returns:
  12152. * -101 = USER_CRYPTO_ERROR
  12153. */
  12154. if (ret == 0)
  12155. #else
  12156. if (ret != BAD_FUNC_ARG)
  12157. #endif
  12158. return -7624;
  12159. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  12160. if (ret != 0)
  12161. return -7625;
  12162. eSz = 0;
  12163. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  12164. #ifdef HAVE_USER_RSA
  12165. /* Implementation using IPP Libraries returns:
  12166. * -101 = USER_CRYPTO_ERROR
  12167. */
  12168. if (ret == 0)
  12169. #elif defined(HAVE_FIPS) && \
  12170. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
  12171. if (ret != 0)
  12172. #else
  12173. if (ret != RSA_BUFFER_E)
  12174. #endif
  12175. return -7626;
  12176. eSz = sizeof(e);
  12177. nSz = 0;
  12178. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  12179. #ifdef HAVE_USER_RSA
  12180. /* Implementation using IPP Libraries returns:
  12181. * -101 = USER_CRYPTO_ERROR
  12182. */
  12183. if (ret == 0)
  12184. #else
  12185. if (ret != RSA_BUFFER_E)
  12186. #endif
  12187. return -7627;
  12188. return 0;
  12189. }
  12190. #endif /* NO_ASN */
  12191. #if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(NO_ASN) \
  12192. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  12193. static int rsa_export_key_test(RsaKey* key)
  12194. {
  12195. int ret;
  12196. byte e[3];
  12197. word32 eSz = sizeof(e);
  12198. byte n[RSA_TEST_BYTES];
  12199. word32 nSz = sizeof(n);
  12200. byte d[RSA_TEST_BYTES];
  12201. word32 dSz = sizeof(d);
  12202. byte p[RSA_TEST_BYTES/2];
  12203. word32 pSz = sizeof(p);
  12204. byte q[RSA_TEST_BYTES/2];
  12205. word32 qSz = sizeof(q);
  12206. word32 zero = 0;
  12207. ret = wc_RsaExportKey(NULL, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12208. if (ret != BAD_FUNC_ARG)
  12209. return -7630;
  12210. ret = wc_RsaExportKey(key, NULL, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12211. if (ret != BAD_FUNC_ARG)
  12212. return -7631;
  12213. ret = wc_RsaExportKey(key, e, NULL, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12214. if (ret != BAD_FUNC_ARG)
  12215. return -7632;
  12216. ret = wc_RsaExportKey(key, e, &eSz, NULL, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12217. if (ret != BAD_FUNC_ARG)
  12218. return -7633;
  12219. ret = wc_RsaExportKey(key, e, &eSz, n, NULL, d, &dSz, p, &pSz, q, &qSz);
  12220. if (ret != BAD_FUNC_ARG)
  12221. return -7634;
  12222. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, NULL, &dSz, p, &pSz, q, &qSz);
  12223. if (ret != BAD_FUNC_ARG)
  12224. return -7635;
  12225. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, NULL, p, &pSz, q, &qSz);
  12226. if (ret != BAD_FUNC_ARG)
  12227. return -7636;
  12228. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, NULL, &pSz, q, &qSz);
  12229. if (ret != BAD_FUNC_ARG)
  12230. return -7637;
  12231. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, NULL, q, &qSz);
  12232. if (ret != BAD_FUNC_ARG)
  12233. return -7638;
  12234. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, NULL, &qSz);
  12235. if (ret != BAD_FUNC_ARG)
  12236. return -7639;
  12237. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, NULL);
  12238. if (ret != BAD_FUNC_ARG)
  12239. return -7640;
  12240. ret = wc_RsaExportKey(key, e, &zero, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12241. if (ret != RSA_BUFFER_E)
  12242. return -7641;
  12243. ret = wc_RsaExportKey(key, e, &eSz, n, &zero, d, &dSz, p, &pSz, q, &qSz);
  12244. if (ret != RSA_BUFFER_E)
  12245. return -7642;
  12246. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  12247. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &zero, p, &pSz, q, &qSz);
  12248. if (ret != RSA_BUFFER_E)
  12249. return -7643;
  12250. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &zero, q, &qSz);
  12251. if (ret != RSA_BUFFER_E)
  12252. return -7644;
  12253. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &zero);
  12254. if (ret != RSA_BUFFER_E)
  12255. return -7645;
  12256. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  12257. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12258. if (ret != 0)
  12259. return -7646;
  12260. return 0;
  12261. }
  12262. #endif /* !HAVE_FIPS && !USER_RSA && !NO_ASN */
  12263. #ifndef NO_SIG_WRAPPER
  12264. static int rsa_sig_test(RsaKey* key, word32 keyLen, int modLen, WC_RNG* rng)
  12265. {
  12266. int ret;
  12267. word32 sigSz;
  12268. WOLFSSL_SMALL_STACK_STATIC const byte in[] = TEST_STRING;
  12269. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  12270. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  12271. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  12272. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  12273. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  12274. };
  12275. WOLFSSL_SMALL_STACK_STATIC const byte hashEnc[] = {
  12276. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  12277. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  12278. 0x00, 0x04, 0x20,
  12279. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  12280. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  12281. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  12282. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  12283. };
  12284. word32 inLen = (word32)XSTRLEN((char*)in);
  12285. byte out[RSA_TEST_BYTES];
  12286. /* Parameter Validation testing. */
  12287. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_NONE, key, keyLen);
  12288. if (ret != BAD_FUNC_ARG)
  12289. return -7650;
  12290. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, 0);
  12291. if (ret != BAD_FUNC_ARG)
  12292. return -7651;
  12293. sigSz = (word32)modLen;
  12294. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  12295. inLen, out, &sigSz, key, keyLen, rng);
  12296. if (ret != BAD_FUNC_ARG)
  12297. return -7652;
  12298. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12299. 0, out, &sigSz, key, keyLen, rng);
  12300. if (ret != BAD_FUNC_ARG)
  12301. return -7653;
  12302. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12303. inLen, NULL, &sigSz, key, keyLen, rng);
  12304. if (ret != BAD_FUNC_ARG)
  12305. return -7654;
  12306. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12307. inLen, out, NULL, key, keyLen, rng);
  12308. if (ret != BAD_FUNC_ARG)
  12309. return -7655;
  12310. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12311. inLen, out, &sigSz, NULL, keyLen, rng);
  12312. if (ret != BAD_FUNC_ARG)
  12313. return -7656;
  12314. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12315. inLen, out, &sigSz, key, 0, rng);
  12316. if (ret != BAD_FUNC_ARG)
  12317. return -7657;
  12318. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12319. inLen, out, &sigSz, key, keyLen, NULL);
  12320. #ifdef HAVE_USER_RSA
  12321. /* Implementation using IPP Libraries returns:
  12322. * -101 = USER_CRYPTO_ERROR
  12323. */
  12324. if (ret == 0)
  12325. #elif defined(WOLFSSL_AFALG_XILINX_RSA) || defined(WOLFSSL_XILINX_CRYPT)
  12326. /* blinding / rng handled with hardware acceleration */
  12327. if (ret != 0)
  12328. #elif defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
  12329. /* async may not require RNG */
  12330. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  12331. if (ret != NO_VALID_DEVID)
  12332. #else
  12333. if (ret != 0 && ret != MISSING_RNG_E)
  12334. #endif
  12335. #elif defined(HAVE_FIPS) || !defined(WC_RSA_BLINDING)
  12336. /* FIPS140 implementation does not do blinding */
  12337. if (ret != 0)
  12338. #elif defined(WOLFSSL_RSA_PUBLIC_ONLY) || defined(WOLFSSL_RSA_VERIFY_ONLY)
  12339. if (ret != SIG_TYPE_E)
  12340. #elif defined(WOLFSSL_CRYPTOCELL) || defined(WOLFSSL_SE050)
  12341. /* RNG is handled by hardware */
  12342. if (ret != 0)
  12343. #else
  12344. if (ret != MISSING_RNG_E)
  12345. #endif
  12346. return -7658;
  12347. sigSz = 0;
  12348. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12349. inLen, out, &sigSz, key, keyLen, rng);
  12350. if (ret != BAD_FUNC_ARG)
  12351. return -7659;
  12352. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  12353. inLen, out, (word32)modLen, key, keyLen);
  12354. if (ret != BAD_FUNC_ARG)
  12355. return -7660;
  12356. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12357. 0, out, (word32)modLen, key, keyLen);
  12358. if (ret != BAD_FUNC_ARG)
  12359. return -7661;
  12360. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12361. inLen, NULL, (word32)modLen, key, keyLen);
  12362. if (ret != BAD_FUNC_ARG)
  12363. return -7662;
  12364. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12365. inLen, out, 0, key, keyLen);
  12366. if (ret != BAD_FUNC_ARG)
  12367. return -7663;
  12368. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12369. inLen, out, (word32)modLen, NULL, keyLen);
  12370. if (ret != BAD_FUNC_ARG)
  12371. return -7664;
  12372. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12373. inLen, out, (word32)modLen, key, 0);
  12374. if (ret != BAD_FUNC_ARG)
  12375. return -7665;
  12376. #ifndef HAVE_ECC
  12377. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, keyLen);
  12378. if (ret != SIG_TYPE_E)
  12379. return -7666;
  12380. #endif
  12381. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  12382. return 0;
  12383. #endif
  12384. /* Use APIs. */
  12385. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, keyLen);
  12386. if (ret != modLen)
  12387. return -7667;
  12388. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA_W_ENC, key, keyLen);
  12389. if (ret != modLen)
  12390. return -7668;
  12391. sigSz = (word32)ret;
  12392. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  12393. XMEMSET(out, 0, sizeof(out));
  12394. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12395. inLen, out, &sigSz, key, keyLen, rng);
  12396. if (ret != 0)
  12397. return -7669;
  12398. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12399. inLen, out, (word32)modLen, key, keyLen);
  12400. if (ret != 0)
  12401. return -7670;
  12402. sigSz = (word32)sizeof(out);
  12403. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  12404. in, inLen, out, &sigSz, key, keyLen, rng);
  12405. if (ret != 0)
  12406. return -7671;
  12407. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  12408. in, inLen, out, (word32)modLen, key, keyLen);
  12409. if (ret != 0)
  12410. return -7672;
  12411. /* Wrong signature type. */
  12412. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12413. inLen, out, (word32)modLen, key, keyLen);
  12414. if (ret == 0)
  12415. return -7673;
  12416. /* check hash functions */
  12417. sigSz = (word32)sizeof(out);
  12418. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  12419. hash, (int)sizeof(hash), out, &sigSz, key, keyLen, rng);
  12420. if (ret != 0)
  12421. return -7674;
  12422. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  12423. hash, (int)sizeof(hash), out, (word32)modLen, key, keyLen);
  12424. if (ret != 0)
  12425. return -7675;
  12426. sigSz = (word32)sizeof(out);
  12427. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  12428. hashEnc, (int)sizeof(hashEnc), out, &sigSz, key, keyLen, rng);
  12429. if (ret != 0)
  12430. return -7676;
  12431. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  12432. hashEnc, (int)sizeof(hashEnc), out, (word32)modLen, key, keyLen);
  12433. if (ret != 0)
  12434. return -7677;
  12435. #else
  12436. (void)hash;
  12437. (void)hashEnc;
  12438. #endif /* !WOLFSSL_RSA_PUBLIC_ONLY && !WOLFSSL_RSA_VERIFY_ONLY */
  12439. return 0;
  12440. }
  12441. #endif /* !NO_SIG_WRAPPER */
  12442. #ifdef WC_RSA_NONBLOCK
  12443. static int rsa_nb_test(RsaKey* key, const byte* in, word32 inLen, byte* out,
  12444. word32 outSz, byte* plain, word32 plainSz, WC_RNG* rng)
  12445. {
  12446. int ret = 0, count;
  12447. int signSz = 0;
  12448. RsaNb nb;
  12449. byte* inlinePlain = NULL;
  12450. /* Enable non-blocking RSA mode - provide context */
  12451. ret = wc_RsaSetNonBlock(key, &nb);
  12452. if (ret != 0)
  12453. return ret;
  12454. #ifdef WC_RSA_NONBLOCK_TIME
  12455. /* Enable time based RSA blocking. 8 microseconds max (3.1GHz) */
  12456. ret = wc_RsaSetNonBlockTime(key, 8, 3100);
  12457. if (ret != 0)
  12458. return ret;
  12459. #endif
  12460. count = 0;
  12461. do {
  12462. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, rng);
  12463. count++; /* track number of would blocks */
  12464. if (ret == FP_WOULDBLOCK) {
  12465. /* do "other" work here */
  12466. }
  12467. } while (ret == FP_WOULDBLOCK);
  12468. if (ret < 0) {
  12469. return ret;
  12470. }
  12471. #ifdef DEBUG_WOLFSSL
  12472. printf("RSA non-block sign: %d times\n", count);
  12473. #endif
  12474. signSz = ret;
  12475. /* Test non-blocking verify */
  12476. XMEMSET(plain, 0, plainSz);
  12477. count = 0;
  12478. do {
  12479. ret = wc_RsaSSL_Verify(out, (word32)signSz, plain, plainSz, key);
  12480. count++; /* track number of would blocks */
  12481. if (ret == FP_WOULDBLOCK) {
  12482. /* do "other" work here */
  12483. }
  12484. } while (ret == FP_WOULDBLOCK);
  12485. if (ret < 0) {
  12486. return ret;
  12487. }
  12488. #ifdef DEBUG_WOLFSSL
  12489. printf("RSA non-block verify: %d times\n", count);
  12490. #endif
  12491. if (signSz == ret && XMEMCMP(plain, in, (size_t)ret)) {
  12492. return SIG_VERIFY_E;
  12493. }
  12494. /* Test inline non-blocking verify */
  12495. count = 0;
  12496. do {
  12497. ret = wc_RsaSSL_VerifyInline(out, (word32)signSz, &inlinePlain, key);
  12498. count++; /* track number of would blocks */
  12499. if (ret == FP_WOULDBLOCK) {
  12500. /* do "other" work here */
  12501. }
  12502. } while (ret == FP_WOULDBLOCK);
  12503. if (ret < 0) {
  12504. return ret;
  12505. }
  12506. #ifdef DEBUG_WOLFSSL
  12507. printf("RSA non-block inline verify: %d times\n", count);
  12508. #endif
  12509. if (signSz == ret && XMEMCMP(inlinePlain, in, (size_t)ret)) {
  12510. return SIG_VERIFY_E;
  12511. }
  12512. /* Disabling non-block RSA mode */
  12513. ret = wc_RsaSetNonBlock(key, NULL);
  12514. (void)count;
  12515. return 0;
  12516. }
  12517. #endif
  12518. #if !defined(HAVE_USER_RSA) && !defined(NO_ASN)
  12519. static int rsa_decode_test(RsaKey* keyPub)
  12520. {
  12521. int ret;
  12522. word32 inSz;
  12523. word32 inOutIdx;
  12524. WOLFSSL_SMALL_STACK_STATIC const byte n[2] = { 0x00, 0x23 };
  12525. WOLFSSL_SMALL_STACK_STATIC const byte e[2] = { 0x00, 0x03 };
  12526. WOLFSSL_SMALL_STACK_STATIC const byte good[] = { 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1,
  12527. 0x03 };
  12528. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgId[] = {
  12529. 0x30, 0x18, 0x30, 0x16,
  12530. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12531. 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12532. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgIdNull[] = {
  12533. 0x30, 0x1a, 0x30, 0x18,
  12534. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12535. 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  12536. 0x02, 0x1, 0x03 };
  12537. WOLFSSL_SMALL_STACK_STATIC const byte badAlgIdNull[] = {
  12538. 0x30, 0x1b, 0x30, 0x19,
  12539. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12540. 0x05, 0x01, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  12541. 0x02, 0x1, 0x03 };
  12542. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitString[] = {
  12543. 0x30, 0x18, 0x30, 0x16,
  12544. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12545. 0x04, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12546. WOLFSSL_SMALL_STACK_STATIC const byte badBitStringLen[] = {
  12547. 0x30, 0x18, 0x30, 0x16,
  12548. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12549. 0x03, 0x0a, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12550. WOLFSSL_SMALL_STACK_STATIC const byte badNoSeq[] = {
  12551. 0x30, 0x16, 0x30, 0x14,
  12552. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12553. 0x07, 0x00, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12554. WOLFSSL_SMALL_STACK_STATIC const byte badNoObj[] = {
  12555. 0x30, 0x0f, 0x30, 0x0d, 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06,
  12556. 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12557. WOLFSSL_SMALL_STACK_STATIC const byte badIntN[] = {
  12558. 0x30, 0x06, 0x02, 0x05, 0x23, 0x02, 0x1, 0x03 };
  12559. WOLFSSL_SMALL_STACK_STATIC const byte badNotIntE[] = {
  12560. 0x30, 0x06, 0x02, 0x01, 0x23, 0x04, 0x1, 0x03 };
  12561. WOLFSSL_SMALL_STACK_STATIC const byte badLength[] = {
  12562. 0x30, 0x04, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12563. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrNoZero[] = {
  12564. 0x30, 0x17, 0x30, 0x15,
  12565. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12566. 0x03, 0x08, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12567. ret = wc_InitRsaKey(keyPub, NULL);
  12568. if (ret != 0)
  12569. return -7690;
  12570. /* Parameter Validation testing. */
  12571. ret = wc_RsaPublicKeyDecodeRaw(NULL, sizeof(n), e, sizeof(e), keyPub);
  12572. if (ret != BAD_FUNC_ARG) {
  12573. ret = -7691;
  12574. goto done;
  12575. }
  12576. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), NULL, sizeof(e), keyPub);
  12577. if (ret != BAD_FUNC_ARG) {
  12578. ret = -7692;
  12579. goto done;
  12580. }
  12581. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), NULL);
  12582. if (ret != BAD_FUNC_ARG) {
  12583. ret = -7693;
  12584. goto done;
  12585. }
  12586. ret = wc_RsaPublicKeyDecodeRaw(n, (word32)-1, e, sizeof(e), keyPub);
  12587. #if !defined(WOLFSSL_SP_MATH) & !defined(WOLFSSL_SP_MATH_ALL)
  12588. if (ret != 0) {
  12589. #else
  12590. if (ret != ASN_GETINT_E) {
  12591. #endif
  12592. ret = -7694;
  12593. goto done;
  12594. }
  12595. wc_FreeRsaKey(keyPub);
  12596. ret = wc_InitRsaKey(keyPub, NULL);
  12597. if (ret != 0)
  12598. return -7695;
  12599. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, (word32)-1, keyPub);
  12600. #if !defined(WOLFSSL_SP_MATH) & !defined(WOLFSSL_SP_MATH_ALL)
  12601. if (ret != 0) {
  12602. #else
  12603. if (ret != ASN_GETINT_E) {
  12604. #endif
  12605. ret = -7696;
  12606. goto done;
  12607. }
  12608. wc_FreeRsaKey(keyPub);
  12609. ret = wc_InitRsaKey(keyPub, NULL);
  12610. if (ret != 0)
  12611. return -7697;
  12612. /* Use API. */
  12613. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), keyPub);
  12614. if (ret != 0) {
  12615. ret = -7698;
  12616. goto done;
  12617. }
  12618. wc_FreeRsaKey(keyPub);
  12619. ret = wc_InitRsaKey(keyPub, NULL);
  12620. if (ret != 0)
  12621. return -7699;
  12622. /* Parameter Validation testing. */
  12623. inSz = sizeof(good);
  12624. ret = wc_RsaPublicKeyDecode(NULL, &inOutIdx, keyPub, inSz);
  12625. if (ret != BAD_FUNC_ARG) {
  12626. ret = -7700;
  12627. goto done;
  12628. }
  12629. ret = wc_RsaPublicKeyDecode(good, NULL, keyPub, inSz);
  12630. if (ret != BAD_FUNC_ARG) {
  12631. ret = -7701;
  12632. goto done;
  12633. }
  12634. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  12635. if (ret != BAD_FUNC_ARG) {
  12636. ret = -7702;
  12637. goto done;
  12638. }
  12639. /* Use good data and offset to bad data. */
  12640. inOutIdx = 2;
  12641. inSz = sizeof(good) - inOutIdx;
  12642. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  12643. if (ret != ASN_PARSE_E) {
  12644. ret = -7703;
  12645. goto done;
  12646. }
  12647. inOutIdx = 2;
  12648. inSz = sizeof(goodAlgId) - inOutIdx;
  12649. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  12650. if (ret != ASN_PARSE_E) {
  12651. ret = -7704;
  12652. goto done;
  12653. }
  12654. inOutIdx = 2;
  12655. inSz = sizeof(goodAlgId);
  12656. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  12657. #ifndef WOLFSSL_NO_DECODE_EXTRA
  12658. if (ret != ASN_PARSE_E)
  12659. #else
  12660. if (ret != ASN_RSA_KEY_E)
  12661. #endif
  12662. {
  12663. ret = -7705;
  12664. goto done;
  12665. }
  12666. /* Try different bad data. */
  12667. inSz = sizeof(badAlgIdNull);
  12668. inOutIdx = 0;
  12669. ret = wc_RsaPublicKeyDecode(badAlgIdNull, &inOutIdx, keyPub, inSz);
  12670. if (ret != ASN_EXPECT_0_E) {
  12671. ret = -7706;
  12672. goto done;
  12673. }
  12674. inSz = sizeof(badNotBitString);
  12675. inOutIdx = 0;
  12676. ret = wc_RsaPublicKeyDecode(badNotBitString, &inOutIdx, keyPub, inSz);
  12677. if (ret != ASN_BITSTR_E) {
  12678. ret = -7707;
  12679. goto done;
  12680. }
  12681. inSz = sizeof(badBitStringLen);
  12682. inOutIdx = 0;
  12683. ret = wc_RsaPublicKeyDecode(badBitStringLen, &inOutIdx, keyPub, inSz);
  12684. if (ret != ASN_PARSE_E) {
  12685. ret = -7708;
  12686. goto done;
  12687. }
  12688. inSz = sizeof(badNoSeq);
  12689. inOutIdx = 0;
  12690. ret = wc_RsaPublicKeyDecode(badNoSeq, &inOutIdx, keyPub, inSz);
  12691. if (ret != ASN_PARSE_E) {
  12692. ret = -7709;
  12693. goto done;
  12694. }
  12695. inSz = sizeof(badNoObj);
  12696. inOutIdx = 0;
  12697. ret = wc_RsaPublicKeyDecode(badNoObj, &inOutIdx, keyPub, inSz);
  12698. if (ret != ASN_PARSE_E && ret != ASN_OBJECT_ID_E) {
  12699. ret = -7710;
  12700. goto done;
  12701. }
  12702. inSz = sizeof(badIntN);
  12703. inOutIdx = 0;
  12704. ret = wc_RsaPublicKeyDecode(badIntN, &inOutIdx, keyPub, inSz);
  12705. if (ret != ASN_RSA_KEY_E && ret != ASN_PARSE_E) {
  12706. ret = -7711;
  12707. goto done;
  12708. }
  12709. inSz = sizeof(badNotIntE);
  12710. inOutIdx = 0;
  12711. ret = wc_RsaPublicKeyDecode(badNotIntE, &inOutIdx, keyPub, inSz);
  12712. if (ret != ASN_RSA_KEY_E && ret != ASN_PARSE_E) {
  12713. ret = -7712;
  12714. goto done;
  12715. }
  12716. /* TODO: Shouldn't pass as the sequence length is too small. */
  12717. inSz = sizeof(badLength);
  12718. inOutIdx = 0;
  12719. ret = wc_RsaPublicKeyDecode(badLength, &inOutIdx, keyPub, inSz);
  12720. #ifndef WOLFSSL_ASN_TEMPLATE
  12721. if (ret != 0)
  12722. #else
  12723. if (ret != ASN_PARSE_E)
  12724. #endif
  12725. {
  12726. ret = -7713;
  12727. goto done;
  12728. }
  12729. /* TODO: Shouldn't ignore object id's data. */
  12730. wc_FreeRsaKey(keyPub);
  12731. ret = wc_InitRsaKey(keyPub, NULL);
  12732. if (ret != 0)
  12733. return -7714;
  12734. inSz = sizeof(badBitStrNoZero);
  12735. inOutIdx = 0;
  12736. ret = wc_RsaPublicKeyDecode(badBitStrNoZero, &inOutIdx, keyPub, inSz);
  12737. if (ret != ASN_EXPECT_0_E && ret != ASN_PARSE_E) {
  12738. ret = -7715;
  12739. goto done;
  12740. }
  12741. wc_FreeRsaKey(keyPub);
  12742. ret = wc_InitRsaKey(keyPub, NULL);
  12743. if (ret != 0)
  12744. return -7716;
  12745. /* Valid data cases. */
  12746. inSz = sizeof(good);
  12747. inOutIdx = 0;
  12748. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  12749. if (ret != 0) {
  12750. ret = -7717;
  12751. goto done;
  12752. }
  12753. if (inOutIdx != inSz) {
  12754. ret = -7718;
  12755. goto done;
  12756. }
  12757. wc_FreeRsaKey(keyPub);
  12758. ret = wc_InitRsaKey(keyPub, NULL);
  12759. if (ret != 0)
  12760. return -7719;
  12761. inSz = sizeof(goodAlgId);
  12762. inOutIdx = 0;
  12763. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  12764. if (ret != 0) {
  12765. ret = -7720;
  12766. goto done;
  12767. }
  12768. if (inOutIdx != inSz) {
  12769. ret = -7721;
  12770. goto done;
  12771. }
  12772. wc_FreeRsaKey(keyPub);
  12773. ret = wc_InitRsaKey(keyPub, NULL);
  12774. if (ret != 0)
  12775. return -7722;
  12776. inSz = sizeof(goodAlgIdNull);
  12777. inOutIdx = 0;
  12778. ret = wc_RsaPublicKeyDecode(goodAlgIdNull, &inOutIdx, keyPub, inSz);
  12779. if (ret != 0) {
  12780. ret = -7723;
  12781. goto done;
  12782. }
  12783. if (inOutIdx != inSz) {
  12784. ret = -7724;
  12785. goto done;
  12786. }
  12787. done:
  12788. wc_FreeRsaKey(keyPub);
  12789. return ret;
  12790. }
  12791. #endif
  12792. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  12793. /* Need to create known good signatures to test with this. */
  12794. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  12795. !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  12796. static int rsa_pss_test(WC_RNG* rng, RsaKey* key)
  12797. {
  12798. byte digest[WC_MAX_DIGEST_SIZE];
  12799. int ret = 0;
  12800. const char inStr[] = TEST_STRING;
  12801. word32 inLen = (word32)TEST_STRING_SZ;
  12802. word32 outSz;
  12803. word32 plainSz;
  12804. word32 digestSz;
  12805. int i, j;
  12806. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  12807. int k, l;
  12808. #endif
  12809. #ifndef WOLFSSL_SE050
  12810. int len;
  12811. #endif
  12812. byte* plain;
  12813. int mgf[] = {
  12814. #ifndef NO_SHA
  12815. WC_MGF1SHA1,
  12816. #endif
  12817. #ifdef WOLFSSL_SHA224
  12818. WC_MGF1SHA224,
  12819. #endif
  12820. WC_MGF1SHA256,
  12821. #ifdef WOLFSSL_SHA384
  12822. WC_MGF1SHA384,
  12823. #endif
  12824. #ifdef WOLFSSL_SHA512
  12825. WC_MGF1SHA512
  12826. #endif
  12827. };
  12828. enum wc_HashType hash[] = {
  12829. #ifndef NO_SHA
  12830. WC_HASH_TYPE_SHA,
  12831. #endif
  12832. #ifdef WOLFSSL_SHA224
  12833. WC_HASH_TYPE_SHA224,
  12834. #endif
  12835. WC_HASH_TYPE_SHA256,
  12836. #ifdef WOLFSSL_SHA384
  12837. WC_HASH_TYPE_SHA384,
  12838. #endif
  12839. #ifdef WOLFSSL_SHA512
  12840. WC_HASH_TYPE_SHA512,
  12841. #endif
  12842. };
  12843. WC_DECLARE_VAR(in, byte, RSA_TEST_BYTES, HEAP_HINT);
  12844. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  12845. WC_DECLARE_VAR(sig, byte, RSA_TEST_BYTES, HEAP_HINT);
  12846. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  12847. if (in == NULL || out == NULL || sig == NULL)
  12848. ERROR_OUT(MEMORY_E, exit_rsa_pss);
  12849. #endif
  12850. XMEMCPY(in, inStr, inLen);
  12851. /* Test all combinations of hash and MGF. */
  12852. for (j = 0; j < (int)(sizeof(hash)/sizeof(*hash)); j++) {
  12853. /* Calculate hash of message. */
  12854. ret = wc_Hash(hash[j], in, inLen, digest, sizeof(digest));
  12855. if (ret != 0)
  12856. ERROR_OUT(-7730, exit_rsa_pss);
  12857. digestSz = wc_HashGetDigestSize(hash[j]);
  12858. #ifdef WOLFSSL_SE050
  12859. /* SE050 only supports MGF matched to same hash type */
  12860. i = j;
  12861. #else
  12862. for (i = 0; i < (int)(sizeof(mgf)/sizeof(*mgf)); i++) {
  12863. #endif
  12864. outSz = RSA_TEST_BYTES;
  12865. do {
  12866. #if defined(WOLFSSL_ASYNC_CRYPT)
  12867. ret = wc_AsyncWait(ret, &key->asyncDev,
  12868. WC_ASYNC_FLAG_CALL_AGAIN);
  12869. #endif
  12870. if (ret >= 0) {
  12871. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz,
  12872. hash[j], mgf[i], -1, key, rng);
  12873. }
  12874. } while (ret == WC_PENDING_E);
  12875. if (ret <= 0)
  12876. ERROR_OUT(-7731, exit_rsa_pss);
  12877. outSz = ret;
  12878. XMEMCPY(sig, out, outSz);
  12879. plain = NULL;
  12880. TEST_SLEEP();
  12881. do {
  12882. #if defined(WOLFSSL_ASYNC_CRYPT)
  12883. ret = wc_AsyncWait(ret, &key->asyncDev,
  12884. WC_ASYNC_FLAG_CALL_AGAIN);
  12885. #endif
  12886. if (ret >= 0) {
  12887. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[j],
  12888. mgf[i], -1, key);
  12889. }
  12890. } while (ret == WC_PENDING_E);
  12891. if (ret <= 0)
  12892. ERROR_OUT(-7732, exit_rsa_pss);
  12893. plainSz = ret;
  12894. TEST_SLEEP();
  12895. #if defined(HAVE_SELFTEST) && \
  12896. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  12897. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  12898. hash[j], -1);
  12899. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  12900. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  12901. hash[j], -1, 0);
  12902. #else
  12903. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz,
  12904. hash[j], -1, wc_RsaEncryptSize(key)*8, HEAP_HINT);
  12905. #endif
  12906. if (ret != 0)
  12907. ERROR_OUT(-7733, exit_rsa_pss);
  12908. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  12909. for (k = 0; k < (int)(sizeof(mgf)/sizeof(*mgf)); k++) {
  12910. for (l = 0; l < (int)(sizeof(hash)/sizeof(*hash)); l++) {
  12911. if (i == k && j == l)
  12912. continue;
  12913. XMEMCPY(sig, out, outSz);
  12914. do {
  12915. #if defined(WOLFSSL_ASYNC_CRYPT)
  12916. ret = wc_AsyncWait(ret, &key->asyncDev,
  12917. WC_ASYNC_FLAG_CALL_AGAIN);
  12918. #endif
  12919. if (ret >= 0) {
  12920. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz,
  12921. (byte**)&plain, hash[l], mgf[k], -1, key);
  12922. }
  12923. } while (ret == WC_PENDING_E);
  12924. if (ret >= 0)
  12925. ERROR_OUT(-7734, exit_rsa_pss);
  12926. }
  12927. }
  12928. #endif
  12929. #ifndef WOLFSSL_SE050
  12930. } /* end mgf for loop */
  12931. #endif
  12932. }
  12933. /* SE050 generates salts internally only of hash length */
  12934. #ifndef WOLFSSL_SE050
  12935. /* Test that a salt length of zero works. */
  12936. digestSz = wc_HashGetDigestSize(hash[0]);
  12937. outSz = RSA_TEST_BYTES;
  12938. do {
  12939. #if defined(WOLFSSL_ASYNC_CRYPT)
  12940. ret = wc_AsyncWait(ret, &key->asyncDev,
  12941. WC_ASYNC_FLAG_CALL_AGAIN);
  12942. #endif
  12943. if (ret >= 0) {
  12944. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  12945. mgf[0], 0, key, rng);
  12946. }
  12947. } while (ret == WC_PENDING_E);
  12948. if (ret <= 0)
  12949. ERROR_OUT(-7735, exit_rsa_pss);
  12950. outSz = ret;
  12951. TEST_SLEEP();
  12952. do {
  12953. #if defined(WOLFSSL_ASYNC_CRYPT)
  12954. ret = wc_AsyncWait(ret, &key->asyncDev,
  12955. WC_ASYNC_FLAG_CALL_AGAIN);
  12956. #endif
  12957. if (ret >= 0) {
  12958. ret = wc_RsaPSS_Verify_ex(out, outSz, sig, outSz, hash[0], mgf[0],
  12959. 0, key);
  12960. }
  12961. } while (ret == WC_PENDING_E);
  12962. if (ret <= 0)
  12963. ERROR_OUT(-7736, exit_rsa_pss);
  12964. plainSz = ret;
  12965. TEST_SLEEP();
  12966. do {
  12967. #if defined(WOLFSSL_ASYNC_CRYPT)
  12968. ret = wc_AsyncWait(ret, &key->asyncDev,
  12969. WC_ASYNC_FLAG_CALL_AGAIN);
  12970. #endif
  12971. if (ret >= 0) {
  12972. #if defined(HAVE_SELFTEST) && \
  12973. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  12974. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  12975. hash[0], 0);
  12976. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  12977. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  12978. hash[0], 0, 0);
  12979. #else
  12980. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, sig, plainSz,
  12981. hash[0], 0, 0, HEAP_HINT);
  12982. #endif
  12983. }
  12984. } while (ret == WC_PENDING_E);
  12985. if (ret != 0)
  12986. ERROR_OUT(-7737, exit_rsa_pss);
  12987. XMEMCPY(sig, out, outSz);
  12988. plain = NULL;
  12989. do {
  12990. #if defined(WOLFSSL_ASYNC_CRYPT)
  12991. ret = wc_AsyncWait(ret, &key->asyncDev,
  12992. WC_ASYNC_FLAG_CALL_AGAIN);
  12993. #endif
  12994. if (ret >= 0) {
  12995. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  12996. 0, key);
  12997. }
  12998. } while (ret == WC_PENDING_E);
  12999. if (ret <= 0)
  13000. ERROR_OUT(-7738, exit_rsa_pss);
  13001. plainSz = ret;
  13002. TEST_SLEEP();
  13003. #if defined(HAVE_SELFTEST) && \
  13004. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  13005. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  13006. hash[0], 0);
  13007. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  13008. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  13009. hash[0], 0, 0);
  13010. #else
  13011. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  13012. 0, 0, HEAP_HINT);
  13013. #endif
  13014. if (ret != 0)
  13015. ERROR_OUT(-7739, exit_rsa_pss);
  13016. /* Test bad salt lengths in various APIs. */
  13017. digestSz = wc_HashGetDigestSize(hash[0]);
  13018. outSz = RSA_TEST_BYTES;
  13019. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  13020. len = -2;
  13021. #else
  13022. len = -3;
  13023. #endif
  13024. do {
  13025. #if defined(WOLFSSL_ASYNC_CRYPT)
  13026. ret = wc_AsyncWait(ret, &key->asyncDev,
  13027. WC_ASYNC_FLAG_CALL_AGAIN);
  13028. #endif
  13029. if (ret >= 0) {
  13030. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  13031. mgf[0], len, key, rng);
  13032. }
  13033. } while (ret == WC_PENDING_E);
  13034. if (ret != PSS_SALTLEN_E)
  13035. ERROR_OUT(-7740, exit_rsa_pss);
  13036. do {
  13037. #if defined(WOLFSSL_ASYNC_CRYPT)
  13038. ret = wc_AsyncWait(ret, &key->asyncDev,
  13039. WC_ASYNC_FLAG_CALL_AGAIN);
  13040. #endif
  13041. if (ret >= 0) {
  13042. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  13043. mgf[0], digestSz + 1, key, rng);
  13044. }
  13045. } while (ret == WC_PENDING_E);
  13046. if (ret != PSS_SALTLEN_E)
  13047. ERROR_OUT(-7741, exit_rsa_pss);
  13048. TEST_SLEEP();
  13049. do {
  13050. #if defined(WOLFSSL_ASYNC_CRYPT)
  13051. ret = wc_AsyncWait(ret, &key->asyncDev,
  13052. WC_ASYNC_FLAG_CALL_AGAIN);
  13053. #endif
  13054. if (ret >= 0) {
  13055. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0],
  13056. mgf[0], -2, key);
  13057. }
  13058. } while (ret == WC_PENDING_E);
  13059. if (ret != PSS_SALTLEN_E)
  13060. ERROR_OUT(-7742, exit_rsa_pss);
  13061. TEST_SLEEP();
  13062. do {
  13063. #if defined(WOLFSSL_ASYNC_CRYPT)
  13064. ret = wc_AsyncWait(ret, &key->asyncDev,
  13065. WC_ASYNC_FLAG_CALL_AGAIN);
  13066. #endif
  13067. if (ret >= 0) {
  13068. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  13069. digestSz + 1, key);
  13070. }
  13071. } while (ret == WC_PENDING_E);
  13072. if (ret != PSS_SALTLEN_E)
  13073. ERROR_OUT(-7743, exit_rsa_pss);
  13074. TEST_SLEEP();
  13075. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  13076. len = -2;
  13077. #else
  13078. len = -3;
  13079. #endif
  13080. #if defined(HAVE_SELFTEST) && \
  13081. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  13082. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  13083. hash[0], len);
  13084. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  13085. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  13086. hash[0], len, 0);
  13087. #else
  13088. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  13089. len, 0, HEAP_HINT);
  13090. #endif
  13091. if (ret != PSS_SALTLEN_E)
  13092. ERROR_OUT(-7744, exit_rsa_pss);
  13093. #ifndef WOLFSSL_PSS_LONG_SALT
  13094. len = digestSz + 1;
  13095. #else
  13096. len = plainSz - digestSz - 1;
  13097. #endif
  13098. #if defined(HAVE_SELFTEST) && \
  13099. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  13100. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  13101. hash[0], len);
  13102. if (ret != PSS_SALTLEN_E)
  13103. ERROR_OUT(-7745, exit_rsa_pss);
  13104. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  13105. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  13106. hash[0], len, 0);
  13107. if (ret != BAD_PADDING_E)
  13108. ERROR_OUT(-7745, exit_rsa_pss);
  13109. #else
  13110. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  13111. len, 0, HEAP_HINT);
  13112. if (ret != PSS_SALTLEN_E)
  13113. ERROR_OUT(-7745, exit_rsa_pss);
  13114. #endif
  13115. ret = 0;
  13116. #endif /* WOLFSSL_SE050 */
  13117. exit_rsa_pss:
  13118. WC_FREE_VAR(sig, HEAP_HINT);
  13119. WC_FREE_VAR(in, HEAP_HINT);
  13120. WC_FREE_VAR(out, HEAP_HINT);
  13121. return ret;
  13122. }
  13123. #endif /* !WOLFSSL_RSA_VERIFY_ONLY && !WOLFSSL_RSA_PUBLIC_ONLY */
  13124. #endif
  13125. #ifdef WC_RSA_NO_PADDING
  13126. WOLFSSL_TEST_SUBROUTINE int rsa_no_pad_test(void)
  13127. {
  13128. WC_RNG rng;
  13129. byte* tmp = NULL;
  13130. size_t bytes;
  13131. int ret;
  13132. word32 inLen = 0;
  13133. word32 idx = 0;
  13134. word32 outSz = RSA_TEST_BYTES;
  13135. word32 plainSz = RSA_TEST_BYTES;
  13136. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  13137. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  13138. !defined(NO_FILESYSTEM)
  13139. XFILE file;
  13140. #endif
  13141. WC_DECLARE_VAR(key, RsaKey, 1, HEAP_HINT);
  13142. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  13143. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  13144. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  13145. if (key == NULL || out == NULL || plain == NULL)
  13146. ERROR_OUT(MEMORY_E, exit_rsa_nopadding);
  13147. #endif
  13148. /* initialize stack structures */
  13149. XMEMSET(&rng, 0, sizeof(rng));
  13150. XMEMSET(key, 0, sizeof(RsaKey));
  13151. #ifdef USE_CERT_BUFFERS_1024
  13152. bytes = (size_t)sizeof_client_key_der_1024;
  13153. if (bytes < (size_t)sizeof_client_cert_der_1024)
  13154. bytes = (size_t)sizeof_client_cert_der_1024;
  13155. #elif defined(USE_CERT_BUFFERS_2048)
  13156. bytes = (size_t)sizeof_client_key_der_2048;
  13157. if (bytes < (size_t)sizeof_client_cert_der_2048)
  13158. bytes = (size_t)sizeof_client_cert_der_2048;
  13159. #else
  13160. bytes = FOURK_BUF;
  13161. #endif
  13162. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13163. if (tmp == NULL
  13164. #ifdef WOLFSSL_ASYNC_CRYPT
  13165. || out == NULL || plain == NULL
  13166. #endif
  13167. ) {
  13168. ERROR_OUT(-7800, exit_rsa_nopadding);
  13169. }
  13170. #ifdef USE_CERT_BUFFERS_1024
  13171. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  13172. #elif defined(USE_CERT_BUFFERS_2048)
  13173. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  13174. #elif defined(USE_CERT_BUFFERS_3072)
  13175. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  13176. #elif defined(USE_CERT_BUFFERS_4096)
  13177. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  13178. #elif !defined(NO_FILESYSTEM)
  13179. file = XFOPEN(clientKey, "rb");
  13180. if (!file) {
  13181. err_sys("can't open clientKey, Please run from wolfSSL home dir", -40);
  13182. ERROR_OUT(-7801, exit_rsa_nopadding);
  13183. }
  13184. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  13185. XFCLOSE(file);
  13186. #else
  13187. /* No key to use. */
  13188. ERROR_OUT(-7802, exit_rsa_nopadding);
  13189. #endif /* USE_CERT_BUFFERS */
  13190. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  13191. if (ret != 0) {
  13192. ERROR_OUT(-7803, exit_rsa_nopadding);
  13193. }
  13194. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  13195. if (ret != 0) {
  13196. ERROR_OUT(-7804, exit_rsa_nopadding);
  13197. }
  13198. /* after loading in key use tmp as the test buffer */
  13199. #ifndef HAVE_FIPS
  13200. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  13201. #else
  13202. ret = wc_InitRng(&rng);
  13203. #endif
  13204. if (ret != 0) {
  13205. ERROR_OUT(-7805, exit_rsa_nopadding);
  13206. }
  13207. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  13208. inLen = wc_RsaEncryptSize(key);
  13209. outSz = inLen;
  13210. plainSz = inLen;
  13211. XMEMSET(tmp, 7, inLen);
  13212. do {
  13213. #if defined(WOLFSSL_ASYNC_CRYPT)
  13214. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13215. #endif
  13216. if (ret >= 0) {
  13217. ret = wc_RsaDirect(tmp, inLen, out, &outSz, key,
  13218. RSA_PRIVATE_ENCRYPT, &rng);
  13219. }
  13220. } while (ret == WC_PENDING_E);
  13221. if (ret <= 0) {
  13222. ERROR_OUT(-7806, exit_rsa_nopadding);
  13223. }
  13224. /* encrypted result should not be the same as input */
  13225. if (XMEMCMP(out, tmp, inLen) == 0) {
  13226. ERROR_OUT(-7807, exit_rsa_nopadding);
  13227. }
  13228. TEST_SLEEP();
  13229. /* decrypt with public key and compare result */
  13230. do {
  13231. #if defined(WOLFSSL_ASYNC_CRYPT)
  13232. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13233. #endif
  13234. if (ret >= 0) {
  13235. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key,
  13236. RSA_PUBLIC_DECRYPT, &rng);
  13237. }
  13238. } while (ret == WC_PENDING_E);
  13239. if (ret <= 0) {
  13240. ERROR_OUT(-7808, exit_rsa_nopadding);
  13241. }
  13242. if (XMEMCMP(plain, tmp, inLen) != 0) {
  13243. ERROR_OUT(-7809, exit_rsa_nopadding);
  13244. }
  13245. TEST_SLEEP();
  13246. #endif
  13247. #ifdef WC_RSA_BLINDING
  13248. ret = wc_RsaSetRNG(NULL, &rng);
  13249. if (ret != BAD_FUNC_ARG) {
  13250. ERROR_OUT(-7810, exit_rsa_nopadding);
  13251. }
  13252. ret = wc_RsaSetRNG(key, &rng);
  13253. if (ret < 0) {
  13254. ERROR_OUT(-7811, exit_rsa_nopadding);
  13255. }
  13256. #endif
  13257. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  13258. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  13259. do {
  13260. #if defined(WOLFSSL_ASYNC_CRYPT)
  13261. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13262. #endif
  13263. if (ret >= 0) {
  13264. ret = wc_RsaPublicEncrypt_ex(tmp, inLen, out, (int)outSz, key, &rng,
  13265. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  13266. }
  13267. } while (ret == WC_PENDING_E);
  13268. if (ret < 0) {
  13269. ERROR_OUT(-7812, exit_rsa_nopadding);
  13270. }
  13271. TEST_SLEEP();
  13272. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  13273. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13274. do {
  13275. #if defined(WOLFSSL_ASYNC_CRYPT)
  13276. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13277. #endif
  13278. if (ret >= 0) {
  13279. ret = wc_RsaPrivateDecrypt_ex(out, outSz, plain, (int)plainSz, key,
  13280. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  13281. }
  13282. } while (ret == WC_PENDING_E);
  13283. if (ret < 0) {
  13284. ERROR_OUT(-7813, exit_rsa_nopadding);
  13285. }
  13286. if (XMEMCMP(plain, tmp, inLen) != 0) {
  13287. ERROR_OUT(-7814, exit_rsa_nopadding);
  13288. }
  13289. TEST_SLEEP();
  13290. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13291. /* test some bad arguments */
  13292. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key, -1,
  13293. &rng);
  13294. if (ret != BAD_FUNC_ARG) {
  13295. ERROR_OUT(-7815, exit_rsa_nopadding);
  13296. }
  13297. ret = wc_RsaDirect(out, outSz, plain, &plainSz, NULL, RSA_PUBLIC_DECRYPT,
  13298. &rng);
  13299. if (ret != BAD_FUNC_ARG) {
  13300. ERROR_OUT(-7816, exit_rsa_nopadding);
  13301. }
  13302. ret = wc_RsaDirect(out, outSz, NULL, &plainSz, key, RSA_PUBLIC_DECRYPT,
  13303. &rng);
  13304. if (ret != LENGTH_ONLY_E || plainSz != inLen) {
  13305. ERROR_OUT(-7817, exit_rsa_nopadding);
  13306. }
  13307. ret = wc_RsaDirect(out, outSz - 10, plain, &plainSz, key,
  13308. RSA_PUBLIC_DECRYPT, &rng);
  13309. if (ret != BAD_FUNC_ARG) {
  13310. ERROR_OUT(-7818, exit_rsa_nopadding);
  13311. }
  13312. /* if making it to this point of code without hitting an ERROR_OUT then
  13313. * all tests have passed */
  13314. ret = 0;
  13315. exit_rsa_nopadding:
  13316. wc_FreeRsaKey(key);
  13317. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13318. WC_FREE_VAR(key, HEAP_HINT);
  13319. WC_FREE_VAR(out, HEAP_HINT);
  13320. WC_FREE_VAR(plain, HEAP_HINT);
  13321. wc_FreeRng(&rng);
  13322. return ret;
  13323. }
  13324. #endif /* WC_RSA_NO_PADDING */
  13325. #if defined(WOLFSSL_HAVE_SP_RSA) && defined(USE_FAST_MATH)
  13326. static int rsa_even_mod_test(WC_RNG* rng, RsaKey* key)
  13327. {
  13328. byte* tmp = NULL;
  13329. size_t bytes;
  13330. int ret;
  13331. word32 inLen = 0;
  13332. #ifndef NO_ASN
  13333. word32 idx = 0;
  13334. #endif
  13335. word32 outSz = RSA_TEST_BYTES;
  13336. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13337. word32 plainSz = RSA_TEST_BYTES;
  13338. #endif
  13339. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_3072) && \
  13340. !defined(USE_CERT_BUFFERS_4096) && !defined(NO_FILESYSTEM)
  13341. XFILE file;
  13342. #endif
  13343. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  13344. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13345. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  13346. #endif
  13347. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  13348. if (out == NULL
  13349. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13350. || plain == NULL
  13351. #endif
  13352. ) {
  13353. ERROR_OUT(MEMORY_E, exit_rsa_even_mod);
  13354. }
  13355. #endif
  13356. #if defined(USE_CERT_BUFFERS_2048)
  13357. bytes = (size_t)sizeof_client_key_der_2048;
  13358. if (bytes < (size_t)sizeof_client_cert_der_2048)
  13359. bytes = (size_t)sizeof_client_cert_der_2048;
  13360. #else
  13361. bytes = FOURK_BUF;
  13362. #endif
  13363. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13364. if (tmp == NULL
  13365. #ifdef WOLFSSL_ASYNC_CRYPT
  13366. || out == NULL || plain == NULL
  13367. #endif
  13368. ) {
  13369. ERROR_OUT(-7800, exit_rsa_even_mod);
  13370. }
  13371. #if defined(USE_CERT_BUFFERS_2048)
  13372. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  13373. #elif defined(USE_CERT_BUFFERS_3072)
  13374. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  13375. #elif defined(USE_CERT_BUFFERS_4096)
  13376. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  13377. #elif !defined(NO_FILESYSTEM)
  13378. file = XFOPEN(clientKey, "rb");
  13379. if (!file) {
  13380. err_sys("can't open ./certs/client-key.der, "
  13381. "Please run from wolfSSL home dir", -40);
  13382. ERROR_OUT(-7801, exit_rsa_even_mod);
  13383. }
  13384. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  13385. XFCLOSE(file);
  13386. #else
  13387. /* No key to use. */
  13388. ERROR_OUT(-7802, exit_rsa_even_mod);
  13389. #endif /* USE_CERT_BUFFERS */
  13390. #ifndef NO_ASN
  13391. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  13392. if (ret != 0) {
  13393. ERROR_OUT(-7804, exit_rsa_even_mod);
  13394. }
  13395. #else
  13396. #ifdef USE_CERT_BUFFERS_2048
  13397. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  13398. if (ret != 0) {
  13399. ERROR_OUT(-7804, exit_rsa_even_mod);
  13400. }
  13401. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  13402. if (ret != 0) {
  13403. ERROR_OUT(-7804, exit_rsa_even_mod);
  13404. }
  13405. #ifndef NO_SIG_WRAPPER
  13406. modLen = 2048;
  13407. #endif
  13408. #else
  13409. #error Not supported yet!
  13410. #endif
  13411. #endif
  13412. key->n.dp[0] &= (mp_digit)-2;
  13413. if (ret != 0) {
  13414. ERROR_OUT(-7804, exit_rsa_even_mod);
  13415. }
  13416. /* after loading in key use tmp as the test buffer */
  13417. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  13418. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM))) && \
  13419. !defined(WOLFSSL_XILINX_CRYPT)
  13420. /* The ARM64_ASM code that was FIPS validated did not return these expected
  13421. * failure codes. These tests cases were added after the assembly was
  13422. * in-lined in the module and validated, these tests will be available in
  13423. * the 140-3 module */
  13424. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  13425. inLen = 32;
  13426. outSz = wc_RsaEncryptSize(key);
  13427. XMEMSET(tmp, 7, plainSz);
  13428. ret = wc_RsaSSL_Sign(tmp, inLen, out, outSz, key, rng);
  13429. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  13430. ERROR_OUT(-7806, exit_rsa_even_mod);
  13431. }
  13432. ret = wc_RsaSSL_Verify(out, outSz, tmp, inLen, key);
  13433. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  13434. ERROR_OUT(-7808, exit_rsa_even_mod);
  13435. }
  13436. #endif
  13437. #ifdef WC_RSA_BLINDING
  13438. ret = wc_RsaSetRNG(key, rng);
  13439. if (ret < 0) {
  13440. ERROR_OUT(-7811, exit_rsa_even_mod);
  13441. }
  13442. #endif
  13443. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  13444. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  13445. ret = wc_RsaPublicEncrypt(tmp, inLen, out, (int)outSz, key, rng);
  13446. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  13447. ERROR_OUT(-7812, exit_rsa_even_mod);
  13448. }
  13449. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  13450. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13451. ret = wc_RsaPrivateDecrypt(out, outSz, plain, (int)plainSz, key);
  13452. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  13453. ERROR_OUT(-7813, exit_rsa_even_mod);
  13454. }
  13455. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13456. #endif /* !(HAVE_FIPS_VERSION == 2 && WOLFSSL_SP_ARMxx_ASM) */
  13457. /* if making it to this point of code without hitting an ERROR_OUT then
  13458. * all tests have passed */
  13459. ret = 0;
  13460. exit_rsa_even_mod:
  13461. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13462. WC_FREE_VAR(out, HEAP_HINT);
  13463. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13464. WC_FREE_VAR(plain, HEAP_HINT);
  13465. #endif
  13466. (void)out;
  13467. (void)outSz;
  13468. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13469. (void)plain;
  13470. (void)plainSz;
  13471. #endif
  13472. (void)inLen;
  13473. (void)rng;
  13474. return ret;
  13475. }
  13476. #endif /* WOLFSSL_HAVE_SP_RSA */
  13477. #ifdef WOLFSSL_CERT_GEN
  13478. static int rsa_certgen_test(RsaKey* key, RsaKey* keypub, WC_RNG* rng, byte* tmp)
  13479. {
  13480. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13481. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13482. #ifdef WOLFSSL_TEST_CERT
  13483. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13484. #endif
  13485. #else
  13486. RsaKey caKey[1];
  13487. #ifdef WOLFSSL_TEST_CERT
  13488. DecodedCert decode[1];
  13489. #endif
  13490. #endif
  13491. byte* der = NULL;
  13492. int ret;
  13493. Cert* myCert = NULL;
  13494. int certSz;
  13495. size_t bytes3;
  13496. word32 idx3 = 0;
  13497. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  13498. XFILE file3;
  13499. #endif
  13500. #if defined(WOLFSSL_ALT_NAMES) && !defined(NO_ASN_TIME)
  13501. struct tm beforeTime;
  13502. struct tm afterTime;
  13503. #endif
  13504. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  13505. (void)keypub;
  13506. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13507. if (caKey == NULL)
  13508. ERROR_OUT(MEMORY_E, exit_rsa);
  13509. #ifdef WOLFSSL_TEST_CERT
  13510. if (decode == NULL)
  13511. ERROR_OUT(MEMORY_E, exit_rsa);
  13512. #endif
  13513. #endif
  13514. XMEMSET(caKey, 0, sizeof *caKey);
  13515. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13516. if (der == NULL) {
  13517. ERROR_OUT(-7820, exit_rsa);
  13518. }
  13519. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13520. if (myCert == NULL) {
  13521. ERROR_OUT(-7821, exit_rsa);
  13522. }
  13523. /* self signed */
  13524. if (wc_InitCert_ex(myCert, HEAP_HINT, devId)) {
  13525. ERROR_OUT(-7822, exit_rsa);
  13526. }
  13527. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  13528. XMEMCPY(myCert->serial, mySerial, sizeof(mySerial));
  13529. myCert->serialSz = (int)sizeof(mySerial);
  13530. myCert->isCA = 1;
  13531. #ifndef NO_SHA256
  13532. myCert->sigType = CTC_SHA256wRSA;
  13533. #else
  13534. myCert->sigType = CTC_SHAwRSA;
  13535. #endif
  13536. #ifdef WOLFSSL_CERT_EXT
  13537. /* add Policies */
  13538. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  13539. CTC_MAX_CERTPOL_SZ);
  13540. XSTRNCPY(myCert->certPolicies[1], "1.2.840.113549.1.9.16.6.5",
  13541. CTC_MAX_CERTPOL_SZ);
  13542. myCert->certPoliciesNb = 2;
  13543. /* add SKID from the Public Key */
  13544. if (wc_SetSubjectKeyIdFromPublicKey(myCert, keypub, NULL) != 0) {
  13545. ERROR_OUT(-7823, exit_rsa);
  13546. }
  13547. /* add AKID from the Public Key */
  13548. if (wc_SetAuthKeyIdFromPublicKey(myCert, keypub, NULL) != 0) {
  13549. ERROR_OUT(-7824, exit_rsa);
  13550. }
  13551. /* add Key Usage */
  13552. if (wc_SetKeyUsage(myCert,"cRLSign,keyCertSign") != 0) {
  13553. ERROR_OUT(-7825, exit_rsa);
  13554. }
  13555. #ifdef WOLFSSL_EKU_OID
  13556. {
  13557. const char unique[] = "2.16.840.1.111111.100.1.10.1";
  13558. if (wc_SetExtKeyUsageOID(myCert, unique, sizeof(unique), 0,
  13559. HEAP_HINT) != 0) {
  13560. ERROR_OUT(-7826, exit_rsa);
  13561. }
  13562. }
  13563. #endif /* WOLFSSL_EKU_OID */
  13564. #endif /* WOLFSSL_CERT_EXT */
  13565. ret = 0;
  13566. do {
  13567. #if defined(WOLFSSL_ASYNC_CRYPT)
  13568. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13569. #endif
  13570. if (ret >= 0) {
  13571. ret = wc_MakeSelfCert(myCert, der, FOURK_BUF, key, rng);
  13572. }
  13573. } while (ret == WC_PENDING_E);
  13574. if (ret < 0) {
  13575. ERROR_OUT(-7827, exit_rsa);
  13576. }
  13577. certSz = ret;
  13578. #ifdef WOLFSSL_TEST_CERT
  13579. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  13580. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  13581. if (ret != 0) {
  13582. FreeDecodedCert(decode);
  13583. ERROR_OUT(-7828, exit_rsa);
  13584. }
  13585. FreeDecodedCert(decode);
  13586. #endif
  13587. ret = SaveDerAndPem(der, certSz, certDerFile, certPemFile,
  13588. CERT_TYPE, -5578);
  13589. if (ret != 0) {
  13590. goto exit_rsa;
  13591. }
  13592. /* Setup Certificate */
  13593. if (wc_InitCert_ex(myCert, HEAP_HINT, devId)) {
  13594. ERROR_OUT(-7829, exit_rsa);
  13595. }
  13596. #ifdef WOLFSSL_ALT_NAMES
  13597. /* Get CA Cert for testing */
  13598. #ifdef USE_CERT_BUFFERS_1024
  13599. XMEMCPY(tmp, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  13600. bytes3 = sizeof_ca_cert_der_1024;
  13601. #elif defined(USE_CERT_BUFFERS_2048)
  13602. XMEMCPY(tmp, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  13603. bytes3 = sizeof_ca_cert_der_2048;
  13604. #else
  13605. file3 = XFOPEN(rsaCaCertDerFile, "rb");
  13606. if (!file3) {
  13607. ERROR_OUT(-7830, exit_rsa);
  13608. }
  13609. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  13610. XFCLOSE(file3);
  13611. #endif /* USE_CERT_BUFFERS */
  13612. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_1024) && \
  13613. !defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  13614. ret = wc_SetAltNames(myCert, rsaCaCertFile);
  13615. if (ret != 0) {
  13616. ERROR_OUT(-7831, exit_rsa);
  13617. }
  13618. #endif
  13619. /* get alt names from der */
  13620. ret = wc_SetAltNamesBuffer(myCert, tmp, (int)bytes3);
  13621. if (ret != 0) {
  13622. ERROR_OUT(-7832, exit_rsa);
  13623. }
  13624. /* get dates from der */
  13625. ret = wc_SetDatesBuffer(myCert, tmp, (int)bytes3);
  13626. if (ret != 0) {
  13627. ERROR_OUT(-7833, exit_rsa);
  13628. }
  13629. #ifndef NO_ASN_TIME
  13630. ret = wc_GetCertDates(myCert, &beforeTime, &afterTime);
  13631. if (ret < 0) {
  13632. ERROR_OUT(-7834, exit_rsa);
  13633. }
  13634. #endif
  13635. #endif /* WOLFSSL_ALT_NAMES */
  13636. /* Get CA Key */
  13637. #ifdef USE_CERT_BUFFERS_1024
  13638. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  13639. bytes3 = sizeof_ca_key_der_1024;
  13640. #elif defined(USE_CERT_BUFFERS_2048)
  13641. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  13642. bytes3 = sizeof_ca_key_der_2048;
  13643. #else
  13644. file3 = XFOPEN(rsaCaKeyFile, "rb");
  13645. if (!file3) {
  13646. ERROR_OUT(-7835, exit_rsa);
  13647. }
  13648. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  13649. XFCLOSE(file3);
  13650. #endif /* USE_CERT_BUFFERS */
  13651. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  13652. if (ret != 0) {
  13653. ERROR_OUT(-7836, exit_rsa);
  13654. }
  13655. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  13656. if (ret != 0) {
  13657. ERROR_OUT(-7837, exit_rsa);
  13658. }
  13659. #ifndef NO_SHA256
  13660. myCert->sigType = CTC_SHA256wRSA;
  13661. #else
  13662. myCert->sigType = CTC_SHAwRSA;
  13663. #endif
  13664. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  13665. #ifdef WOLFSSL_CERT_EXT
  13666. /* add Policies */
  13667. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  13668. CTC_MAX_CERTPOL_SZ);
  13669. myCert->certPoliciesNb =1;
  13670. /* add SKID from the Public Key */
  13671. if (wc_SetSubjectKeyIdFromPublicKey(myCert, key, NULL) != 0) {
  13672. ERROR_OUT(-7838, exit_rsa);
  13673. }
  13674. /* add AKID from the CA certificate */
  13675. #if defined(USE_CERT_BUFFERS_2048)
  13676. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  13677. sizeof_ca_cert_der_2048);
  13678. #elif defined(USE_CERT_BUFFERS_1024)
  13679. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  13680. sizeof_ca_cert_der_1024);
  13681. #else
  13682. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  13683. #endif
  13684. if (ret != 0) {
  13685. ERROR_OUT(-7839, exit_rsa);
  13686. }
  13687. /* add Key Usage */
  13688. if (wc_SetKeyUsage(myCert,"keyEncipherment,keyAgreement") != 0) {
  13689. ERROR_OUT(-7840, exit_rsa);
  13690. }
  13691. #endif /* WOLFSSL_CERT_EXT */
  13692. #if defined(USE_CERT_BUFFERS_2048)
  13693. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  13694. sizeof_ca_cert_der_2048);
  13695. #elif defined(USE_CERT_BUFFERS_1024)
  13696. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  13697. sizeof_ca_cert_der_1024);
  13698. #else
  13699. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  13700. #endif
  13701. if (ret < 0) {
  13702. ERROR_OUT(-7841, exit_rsa);
  13703. }
  13704. certSz = wc_MakeCert(myCert, der, FOURK_BUF, key, NULL, rng);
  13705. if (certSz < 0) {
  13706. ERROR_OUT(-7842, exit_rsa);
  13707. }
  13708. ret = 0;
  13709. do {
  13710. #if defined(WOLFSSL_ASYNC_CRYPT)
  13711. ret = wc_AsyncWait(ret, &caKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13712. #endif
  13713. if (ret >= 0) {
  13714. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der, FOURK_BUF,
  13715. caKey, NULL, rng);
  13716. }
  13717. } while (ret == WC_PENDING_E);
  13718. if (ret < 0) {
  13719. ERROR_OUT(-7843, exit_rsa);
  13720. }
  13721. certSz = ret;
  13722. #ifdef WOLFSSL_TEST_CERT
  13723. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  13724. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  13725. if (ret != 0) {
  13726. FreeDecodedCert(decode);
  13727. ERROR_OUT(-7844, exit_rsa);
  13728. }
  13729. FreeDecodedCert(decode);
  13730. #endif
  13731. ret = SaveDerAndPem(der, certSz, otherCertDerFile, otherCertPemFile,
  13732. CERT_TYPE, -5598);
  13733. if (ret != 0) {
  13734. goto exit_rsa;
  13735. }
  13736. exit_rsa:
  13737. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13738. if (caKey != NULL) {
  13739. wc_FreeRsaKey(caKey);
  13740. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13741. }
  13742. #ifdef WOLFSSL_TEST_CERT
  13743. if (decode != NULL)
  13744. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13745. #endif
  13746. #else
  13747. wc_FreeRsaKey(caKey);
  13748. #endif
  13749. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13750. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13751. return ret;
  13752. }
  13753. #endif
  13754. #if !defined(NO_RSA) && defined(HAVE_ECC) && defined(WOLFSSL_CERT_GEN)
  13755. /* Make Cert / Sign example for ECC cert and RSA CA */
  13756. static int rsa_ecc_certgen_test(WC_RNG* rng, byte* tmp)
  13757. {
  13758. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13759. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13760. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13761. ecc_key *caEccKeyPub = (ecc_key *)XMALLOC(sizeof *caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13762. #ifdef WOLFSSL_TEST_CERT
  13763. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13764. #endif
  13765. #else
  13766. RsaKey caKey[1];
  13767. ecc_key caEccKey[1];
  13768. ecc_key caEccKeyPub[1];
  13769. #ifdef WOLFSSL_TEST_CERT
  13770. DecodedCert decode[1];
  13771. #endif
  13772. #endif
  13773. byte* der = NULL;
  13774. Cert* myCert = NULL;
  13775. int certSz;
  13776. size_t bytes3;
  13777. word32 idx3 = 0;
  13778. #if (!defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)) \
  13779. || !defined(USE_CERT_BUFFERS_256)
  13780. XFILE file3;
  13781. #endif
  13782. int ret;
  13783. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13784. if ((caKey == NULL) || (caEccKey == NULL) || (caEccKeyPub == NULL)
  13785. #ifdef WOLFSSL_TEST_CERT
  13786. || (decode == NULL)
  13787. #endif
  13788. )
  13789. ERROR_OUT(MEMORY_E, exit_rsa);
  13790. #endif
  13791. XMEMSET(caKey, 0, sizeof *caKey);
  13792. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  13793. XMEMSET(caEccKeyPub, 0, sizeof *caEccKeyPub);
  13794. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13795. if (der == NULL) {
  13796. ERROR_OUT(-7850, exit_rsa);
  13797. }
  13798. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13799. if (myCert == NULL) {
  13800. ERROR_OUT(-7851, exit_rsa);
  13801. }
  13802. /* Get CA Key */
  13803. #ifdef USE_CERT_BUFFERS_1024
  13804. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  13805. bytes3 = sizeof_ca_key_der_1024;
  13806. #elif defined(USE_CERT_BUFFERS_2048)
  13807. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  13808. bytes3 = sizeof_ca_key_der_2048;
  13809. #else
  13810. file3 = XFOPEN(rsaCaKeyFile, "rb");
  13811. if (!file3) {
  13812. ERROR_OUT(-7852, exit_rsa);
  13813. }
  13814. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  13815. XFCLOSE(file3);
  13816. #endif /* USE_CERT_BUFFERS */
  13817. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  13818. if (ret != 0) {
  13819. ERROR_OUT(-7853, exit_rsa);
  13820. }
  13821. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  13822. if (ret != 0) {
  13823. ERROR_OUT(-7854, exit_rsa);
  13824. }
  13825. /* Get Cert Key */
  13826. #ifdef USE_CERT_BUFFERS_256
  13827. XMEMCPY(tmp, ecc_key_pub_der_256, sizeof_ecc_key_pub_der_256);
  13828. bytes3 = sizeof_ecc_key_pub_der_256;
  13829. #else
  13830. file3 = XFOPEN(eccKeyPubFileDer, "rb");
  13831. if (!file3) {
  13832. ERROR_OUT(-7855, exit_rsa);
  13833. }
  13834. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  13835. XFCLOSE(file3);
  13836. #endif
  13837. ret = wc_ecc_init_ex(caEccKeyPub, HEAP_HINT, devId);
  13838. if (ret != 0) {
  13839. ERROR_OUT(-7856, exit_rsa);
  13840. }
  13841. idx3 = 0;
  13842. ret = wc_EccPublicKeyDecode(tmp, &idx3, caEccKeyPub, (word32)bytes3);
  13843. if (ret != 0) {
  13844. ERROR_OUT(-7857, exit_rsa);
  13845. }
  13846. /* Setup Certificate */
  13847. if (wc_InitCert_ex(myCert, HEAP_HINT, devId)) {
  13848. ERROR_OUT(-7858, exit_rsa);
  13849. }
  13850. #ifndef NO_SHA256
  13851. myCert->sigType = CTC_SHA256wRSA;
  13852. #else
  13853. myCert->sigType = CTC_SHAwRSA;
  13854. #endif
  13855. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  13856. #ifdef WOLFSSL_CERT_EXT
  13857. /* add Policies */
  13858. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  13859. CTC_MAX_CERTPOL_SZ);
  13860. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  13861. CTC_MAX_CERTPOL_SZ);
  13862. myCert->certPoliciesNb = 2;
  13863. /* add SKID from the Public Key */
  13864. if (wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, caEccKeyPub) != 0) {
  13865. ERROR_OUT(-7859, exit_rsa);
  13866. }
  13867. /* add AKID from the CA certificate */
  13868. #if defined(USE_CERT_BUFFERS_2048)
  13869. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  13870. sizeof_ca_cert_der_2048);
  13871. #elif defined(USE_CERT_BUFFERS_1024)
  13872. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  13873. sizeof_ca_cert_der_1024);
  13874. #else
  13875. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  13876. #endif
  13877. if (ret != 0) {
  13878. ERROR_OUT(-7860, exit_rsa);
  13879. }
  13880. /* add Key Usage */
  13881. if (wc_SetKeyUsage(myCert, certKeyUsage) != 0) {
  13882. ERROR_OUT(-7861, exit_rsa);
  13883. }
  13884. #endif /* WOLFSSL_CERT_EXT */
  13885. #if defined(USE_CERT_BUFFERS_2048)
  13886. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  13887. sizeof_ca_cert_der_2048);
  13888. #elif defined(USE_CERT_BUFFERS_1024)
  13889. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  13890. sizeof_ca_cert_der_1024);
  13891. #else
  13892. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  13893. #endif
  13894. if (ret < 0) {
  13895. ERROR_OUT(-7862, exit_rsa);
  13896. }
  13897. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, caEccKeyPub, rng);
  13898. if (certSz < 0) {
  13899. ERROR_OUT(-7863, exit_rsa);
  13900. }
  13901. ret = 0;
  13902. do {
  13903. #if defined(WOLFSSL_ASYNC_CRYPT)
  13904. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13905. #endif
  13906. if (ret >= 0) {
  13907. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  13908. FOURK_BUF, caKey, NULL, rng);
  13909. }
  13910. } while (ret == WC_PENDING_E);
  13911. if (ret < 0) {
  13912. ERROR_OUT(-7864, exit_rsa);
  13913. }
  13914. certSz = ret;
  13915. #ifdef WOLFSSL_TEST_CERT
  13916. InitDecodedCert(decode, der, certSz, 0);
  13917. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  13918. if (ret != 0) {
  13919. FreeDecodedCert(decode);
  13920. ERROR_OUT(-7865, exit_rsa);
  13921. }
  13922. FreeDecodedCert(decode);
  13923. #endif
  13924. ret = SaveDerAndPem(der, certSz, certEccRsaDerFile, certEccRsaPemFile,
  13925. CERT_TYPE, -5616);
  13926. if (ret != 0) {
  13927. goto exit_rsa;
  13928. }
  13929. exit_rsa:
  13930. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13931. if (caKey != NULL) {
  13932. wc_FreeRsaKey(caKey);
  13933. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13934. }
  13935. if (caEccKey != NULL) {
  13936. wc_ecc_free(caEccKey);
  13937. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13938. }
  13939. if (caEccKeyPub != NULL) {
  13940. wc_ecc_free(caEccKeyPub);
  13941. XFREE(caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13942. }
  13943. #ifdef WOLFSSL_TEST_CERT
  13944. if (decode != NULL)
  13945. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13946. #endif
  13947. #else
  13948. wc_FreeRsaKey(caKey);
  13949. wc_ecc_free(caEccKey);
  13950. wc_ecc_free(caEccKeyPub);
  13951. #endif
  13952. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13953. myCert = NULL;
  13954. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13955. der = NULL;
  13956. if (ret >= 0)
  13957. ret = 0;
  13958. return ret;
  13959. }
  13960. #endif /* !NO_RSA && HAVE_ECC && WOLFSSL_CERT_GEN */
  13961. #ifdef WOLFSSL_KEY_GEN
  13962. static int rsa_keygen_test(WC_RNG* rng)
  13963. {
  13964. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13965. RsaKey *genKey = (RsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13966. #else
  13967. RsaKey genKey[1];
  13968. #endif
  13969. int ret;
  13970. byte* der = NULL;
  13971. #ifndef WOLFSSL_CRYPTOCELL
  13972. word32 idx = 0;
  13973. #endif
  13974. int derSz = 0;
  13975. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FIPS)
  13976. int keySz = 1024;
  13977. #else
  13978. int keySz = 2048;
  13979. #endif
  13980. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13981. if (! genKey)
  13982. ERROR_OUT(MEMORY_E, exit_rsa);
  13983. #endif
  13984. XMEMSET(genKey, 0, sizeof *genKey);
  13985. ret = wc_InitRsaKey_ex(genKey, HEAP_HINT, devId);
  13986. if (ret != 0) {
  13987. ERROR_OUT(-7870, exit_rsa);
  13988. }
  13989. #ifdef HAVE_FIPS
  13990. for (;;) {
  13991. #endif
  13992. ret = wc_MakeRsaKey(genKey, keySz, WC_RSA_EXPONENT, rng);
  13993. #if defined(WOLFSSL_ASYNC_CRYPT)
  13994. ret = wc_AsyncWait(ret, &genKey->asyncDev, WC_ASYNC_FLAG_NONE);
  13995. #endif
  13996. #ifdef HAVE_FIPS
  13997. if (ret == PRIME_GEN_E)
  13998. continue;
  13999. break;
  14000. }
  14001. #endif
  14002. if (ret != 0) {
  14003. ERROR_OUT(-7871, exit_rsa);
  14004. }
  14005. TEST_SLEEP();
  14006. #ifdef WOLFSSL_RSA_KEY_CHECK
  14007. ret = wc_CheckRsaKey(genKey);
  14008. if (ret != 0) {
  14009. ERROR_OUT(-7872, exit_rsa);
  14010. }
  14011. #endif
  14012. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14013. if (der == NULL) {
  14014. ERROR_OUT(-7873, exit_rsa);
  14015. }
  14016. derSz = wc_RsaKeyToDer(genKey, der, FOURK_BUF);
  14017. if (derSz < 0) {
  14018. ERROR_OUT(-7874, exit_rsa);
  14019. }
  14020. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  14021. PRIVATEKEY_TYPE, -5555);
  14022. if (ret != 0) {
  14023. goto exit_rsa;
  14024. }
  14025. wc_FreeRsaKey(genKey);
  14026. ret = wc_InitRsaKey(genKey, HEAP_HINT);
  14027. if (ret != 0) {
  14028. ERROR_OUT(-7875, exit_rsa);
  14029. }
  14030. #ifndef WOLFSSL_CRYPTOCELL
  14031. idx = 0;
  14032. /* The private key part of the key gen pairs from cryptocell can't be exported */
  14033. ret = wc_RsaPrivateKeyDecode(der, &idx, genKey, derSz);
  14034. if (ret != 0) {
  14035. ERROR_OUT(-7876, exit_rsa);
  14036. }
  14037. #endif /* WOLFSSL_CRYPTOCELL */
  14038. exit_rsa:
  14039. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14040. if (genKey) {
  14041. wc_FreeRsaKey(genKey);
  14042. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14043. }
  14044. #else
  14045. wc_FreeRsaKey(genKey);
  14046. #endif
  14047. if (der != NULL) {
  14048. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14049. der = NULL;
  14050. }
  14051. return ret;
  14052. }
  14053. #endif
  14054. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  14055. #if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG) && \
  14056. !defined(HAVE_FAST_RSA) && !defined(HAVE_USER_RSA) && \
  14057. (!defined(HAVE_FIPS) || \
  14058. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) \
  14059. && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14060. static int rsa_oaep_padding_test(RsaKey* key, WC_RNG* rng)
  14061. {
  14062. int ret = 0;
  14063. word32 idx = 0;
  14064. const char inStr[] = TEST_STRING;
  14065. const word32 inLen = (word32)TEST_STRING_SZ;
  14066. const word32 outSz = RSA_TEST_BYTES;
  14067. const word32 plainSz = RSA_TEST_BYTES;
  14068. byte* res = NULL;
  14069. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  14070. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  14071. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  14072. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  14073. if (in == NULL || out == NULL || plain == NULL)
  14074. ERROR_OUT(MEMORY_E, exit_rsa);
  14075. #endif
  14076. XMEMCPY(in, inStr, inLen);
  14077. #ifndef NO_SHA
  14078. do {
  14079. #if defined(WOLFSSL_ASYNC_CRYPT)
  14080. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14081. #endif
  14082. if (ret >= 0) {
  14083. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14084. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  14085. }
  14086. } while (ret == WC_PENDING_E);
  14087. if (ret < 0) {
  14088. ERROR_OUT(-7918, exit_rsa);
  14089. }
  14090. TEST_SLEEP();
  14091. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14092. idx = (word32)ret;
  14093. do {
  14094. #if defined(WOLFSSL_ASYNC_CRYPT)
  14095. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14096. #endif
  14097. if (ret >= 0) {
  14098. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14099. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  14100. }
  14101. } while (ret == WC_PENDING_E);
  14102. if (ret < 0) {
  14103. ERROR_OUT(-7919, exit_rsa);
  14104. }
  14105. if (XMEMCMP(plain, in, inLen)) {
  14106. ERROR_OUT(-7920, exit_rsa);
  14107. }
  14108. TEST_SLEEP();
  14109. #endif /* NO_SHA */
  14110. #endif
  14111. #ifndef NO_SHA256
  14112. XMEMSET(plain, 0, plainSz);
  14113. do {
  14114. #if defined(WOLFSSL_ASYNC_CRYPT)
  14115. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14116. #endif
  14117. if (ret >= 0) {
  14118. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14119. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  14120. }
  14121. } while (ret == WC_PENDING_E);
  14122. if (ret < 0) {
  14123. ERROR_OUT(-7921, exit_rsa);
  14124. }
  14125. TEST_SLEEP();
  14126. idx = (word32)ret;
  14127. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14128. do {
  14129. #if defined(WOLFSSL_ASYNC_CRYPT)
  14130. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14131. #endif
  14132. if (ret >= 0) {
  14133. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14134. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  14135. }
  14136. } while (ret == WC_PENDING_E);
  14137. if (ret < 0) {
  14138. ERROR_OUT(-7922, exit_rsa);
  14139. }
  14140. if (XMEMCMP(plain, in, inLen)) {
  14141. ERROR_OUT(-7923, exit_rsa);
  14142. }
  14143. TEST_SLEEP();
  14144. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14145. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14146. do {
  14147. #if defined(WOLFSSL_ASYNC_CRYPT)
  14148. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14149. #endif
  14150. if (ret >= 0) {
  14151. ret = wc_RsaPrivateDecryptInline_ex(out, idx, &res, key,
  14152. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  14153. }
  14154. } while (ret == WC_PENDING_E);
  14155. if (ret < 0) {
  14156. ERROR_OUT(-7924, exit_rsa);
  14157. }
  14158. if (ret != (int)inLen) {
  14159. ERROR_OUT(-7925, exit_rsa);
  14160. }
  14161. if (XMEMCMP(res, in, inLen)) {
  14162. ERROR_OUT(-7926, exit_rsa);
  14163. }
  14164. TEST_SLEEP();
  14165. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14166. /* check fails if not using the same optional label */
  14167. XMEMSET(plain, 0, plainSz);
  14168. do {
  14169. #if defined(WOLFSSL_ASYNC_CRYPT)
  14170. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14171. #endif
  14172. if (ret >= 0) {
  14173. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14174. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  14175. }
  14176. } while (ret == WC_PENDING_E);
  14177. if (ret < 0) {
  14178. ERROR_OUT(-7927, exit_rsa);
  14179. }
  14180. TEST_SLEEP();
  14181. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  14182. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  14183. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SE050)
  14184. /* label is unused in cryptocell and SE050 so it won't detect decrypt error
  14185. * due to label */
  14186. idx = (word32)ret;
  14187. do {
  14188. #if defined(WOLFSSL_ASYNC_CRYPT)
  14189. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14190. #endif
  14191. if (ret >= 0) {
  14192. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14193. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  14194. }
  14195. } while (ret == WC_PENDING_E);
  14196. if (ret > 0) { /* in this case decrypt should fail */
  14197. ERROR_OUT(-7928, exit_rsa);
  14198. }
  14199. ret = 0;
  14200. TEST_SLEEP();
  14201. #endif /* !HAVE_CAVIUM */
  14202. /* check using optional label with encrypt/decrypt */
  14203. XMEMSET(plain, 0, plainSz);
  14204. do {
  14205. #if defined(WOLFSSL_ASYNC_CRYPT)
  14206. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14207. #endif
  14208. if (ret >= 0) {
  14209. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14210. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  14211. }
  14212. } while (ret == WC_PENDING_E);
  14213. if (ret < 0) {
  14214. ERROR_OUT(-7929, exit_rsa);
  14215. }
  14216. TEST_SLEEP();
  14217. idx = (word32)ret;
  14218. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14219. do {
  14220. #if defined(WOLFSSL_ASYNC_CRYPT)
  14221. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14222. #endif
  14223. if (ret >= 0) {
  14224. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14225. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  14226. }
  14227. } while (ret == WC_PENDING_E);
  14228. if (ret < 0) {
  14229. ERROR_OUT(-7930, exit_rsa);
  14230. }
  14231. if (XMEMCMP(plain, in, inLen)) {
  14232. ERROR_OUT(-7931, exit_rsa);
  14233. }
  14234. TEST_SLEEP();
  14235. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14236. #ifndef NO_SHA
  14237. /* check fail using mismatch hash algorithms */
  14238. XMEMSET(plain, 0, plainSz);
  14239. do {
  14240. #if defined(WOLFSSL_ASYNC_CRYPT)
  14241. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14242. #endif
  14243. if (ret >= 0) {
  14244. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14245. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, in, inLen);
  14246. }
  14247. } while (ret == WC_PENDING_E);
  14248. if (ret < 0) {
  14249. ERROR_OUT(-7932, exit_rsa);
  14250. }
  14251. TEST_SLEEP();
  14252. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  14253. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  14254. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SE050)
  14255. idx = (word32)ret;
  14256. do {
  14257. #if defined(WOLFSSL_ASYNC_CRYPT)
  14258. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14259. #endif
  14260. if (ret >= 0) {
  14261. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14262. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256,
  14263. in, inLen);
  14264. }
  14265. } while (ret == WC_PENDING_E);
  14266. if (ret > 0) { /* should fail */
  14267. ERROR_OUT(-7933, exit_rsa);
  14268. }
  14269. ret = 0;
  14270. TEST_SLEEP();
  14271. #endif /* !HAVE_CAVIUM */
  14272. #endif /* NO_SHA */
  14273. #endif /* NO_SHA256 */
  14274. #ifdef WOLFSSL_SHA512
  14275. /* Check valid RSA key size is used while using hash length of SHA512
  14276. If key size is less than (hash length * 2) + 2 then is invalid use
  14277. and test, since OAEP padding requires this.
  14278. BAD_FUNC_ARG is returned when this case is not met */
  14279. if (wc_RsaEncryptSize(key) > ((int)WC_SHA512_DIGEST_SIZE * 2) + 2) {
  14280. XMEMSET(plain, 0, plainSz);
  14281. do {
  14282. #if defined(WOLFSSL_ASYNC_CRYPT)
  14283. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14284. #endif
  14285. if (ret >= 0) {
  14286. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14287. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  14288. }
  14289. } while (ret == WC_PENDING_E);
  14290. if (ret < 0) {
  14291. ERROR_OUT(-7934, exit_rsa);
  14292. }
  14293. TEST_SLEEP();
  14294. idx = ret;
  14295. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14296. do {
  14297. #if defined(WOLFSSL_ASYNC_CRYPT)
  14298. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14299. #endif
  14300. if (ret >= 0) {
  14301. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14302. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  14303. }
  14304. } while (ret == WC_PENDING_E);
  14305. if (ret < 0) {
  14306. ERROR_OUT(-7935, exit_rsa);
  14307. }
  14308. if (XMEMCMP(plain, in, inLen)) {
  14309. ERROR_OUT(-7936, exit_rsa);
  14310. }
  14311. TEST_SLEEP();
  14312. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14313. }
  14314. #endif /* WOLFSSL_SHA512 */
  14315. /* check using pkcsv15 padding with _ex API */
  14316. XMEMSET(plain, 0, plainSz);
  14317. do {
  14318. #if defined(WOLFSSL_ASYNC_CRYPT)
  14319. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14320. #endif
  14321. if (ret >= 0) {
  14322. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14323. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  14324. }
  14325. } while (ret == WC_PENDING_E);
  14326. if (ret < 0) {
  14327. ERROR_OUT(-7937, exit_rsa);
  14328. }
  14329. TEST_SLEEP();
  14330. idx = (word32)ret;
  14331. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14332. do {
  14333. #if defined(WOLFSSL_ASYNC_CRYPT)
  14334. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14335. #endif
  14336. if (ret >= 0) {
  14337. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14338. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  14339. }
  14340. } while (ret == WC_PENDING_E);
  14341. if (ret < 0) {
  14342. ERROR_OUT(-7938, exit_rsa);
  14343. }
  14344. if (XMEMCMP(plain, in, inLen)) {
  14345. ERROR_OUT(-7939, exit_rsa);
  14346. }
  14347. TEST_SLEEP();
  14348. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14349. exit_rsa:
  14350. WC_FREE_VAR(in, HEAP_HINT);
  14351. WC_FREE_VAR(out, HEAP_HINT);
  14352. WC_FREE_VAR(plain, HEAP_HINT);
  14353. (void)idx;
  14354. (void)inStr;
  14355. (void)res;
  14356. if (ret >= 0)
  14357. ret = 0;
  14358. return ret;
  14359. }
  14360. #endif
  14361. #endif
  14362. WOLFSSL_TEST_SUBROUTINE int rsa_test(void)
  14363. {
  14364. int ret;
  14365. size_t bytes;
  14366. WC_RNG rng;
  14367. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14368. byte* tmp = NULL;
  14369. byte* der = NULL;
  14370. RsaKey *key = (RsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14371. #else
  14372. RsaKey key[1];
  14373. byte tmp[FOURK_BUF];
  14374. #endif
  14375. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  14376. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14377. RsaKey *keypub = (RsaKey *)XMALLOC(sizeof *keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14378. #else
  14379. RsaKey keypub[1];
  14380. #endif
  14381. #endif
  14382. word32 idx = 0;
  14383. const char inStr[] = TEST_STRING;
  14384. const word32 inLen = (word32)TEST_STRING_SZ;
  14385. const word32 outSz = RSA_TEST_BYTES;
  14386. const word32 plainSz = RSA_TEST_BYTES;
  14387. byte* res = NULL;
  14388. #ifndef NO_SIG_WRAPPER
  14389. int modLen;
  14390. #endif
  14391. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  14392. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  14393. !defined(NO_FILESYSTEM)
  14394. XFILE file;
  14395. #ifdef WOLFSSL_TEST_CERT
  14396. XFILE file2;
  14397. #endif
  14398. #endif
  14399. #ifdef WOLFSSL_TEST_CERT
  14400. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14401. DecodedCert *cert = (DecodedCert *)XMALLOC(sizeof *cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14402. #else
  14403. DecodedCert cert[1];
  14404. #endif
  14405. #ifndef NO_ASN_TIME
  14406. struct tm timearg;
  14407. const byte* date;
  14408. byte dateFormat;
  14409. int dateLength;
  14410. #endif
  14411. #endif
  14412. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  14413. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  14414. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  14415. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  14416. if (in == NULL || out == NULL || plain == NULL)
  14417. ERROR_OUT(MEMORY_E, exit_rsa);
  14418. #endif
  14419. XMEMCPY(in, inStr, inLen);
  14420. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14421. if (key == NULL)
  14422. ERROR_OUT(MEMORY_E, exit_rsa);
  14423. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  14424. if (keypub == NULL)
  14425. ERROR_OUT(MEMORY_E, exit_rsa);
  14426. #endif
  14427. #ifdef WOLFSSL_TEST_CERT
  14428. if (cert == NULL)
  14429. ERROR_OUT(MEMORY_E, exit_rsa);
  14430. #endif
  14431. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  14432. /* initialize stack structures */
  14433. XMEMSET(&rng, 0, sizeof(rng));
  14434. XMEMSET(key, 0, sizeof *key);
  14435. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  14436. XMEMSET(keypub, 0, sizeof *keypub);
  14437. #endif
  14438. #if !defined(HAVE_USER_RSA) && !defined(NO_ASN)
  14439. ret = rsa_decode_test(key);
  14440. if (ret != 0)
  14441. ERROR_OUT(ret, exit_rsa);
  14442. #endif
  14443. #ifdef USE_CERT_BUFFERS_1024
  14444. bytes = (size_t)sizeof_client_key_der_1024;
  14445. if (bytes < (size_t)sizeof_client_cert_der_1024)
  14446. bytes = (size_t)sizeof_client_cert_der_1024;
  14447. #elif defined(USE_CERT_BUFFERS_2048)
  14448. bytes = (size_t)sizeof_client_key_der_2048;
  14449. if (bytes < (size_t)sizeof_client_cert_der_2048)
  14450. bytes = (size_t)sizeof_client_cert_der_2048;
  14451. #elif defined(USE_CERT_BUFFERS_3072)
  14452. bytes = (size_t)sizeof_client_key_der_3072;
  14453. if (bytes < (size_t)sizeof_client_cert_der_3072)
  14454. bytes = (size_t)sizeof_client_cert_der_3072;
  14455. #elif defined(USE_CERT_BUFFERS_4096)
  14456. bytes = (size_t)sizeof_client_key_der_4096;
  14457. if (bytes < (size_t)sizeof_client_cert_der_4096)
  14458. bytes = (size_t)sizeof_client_cert_der_4096;
  14459. #else
  14460. bytes = FOURK_BUF;
  14461. #endif
  14462. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14463. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14464. if (tmp == NULL)
  14465. ERROR_OUT(-7900, exit_rsa);
  14466. #endif
  14467. #ifdef USE_CERT_BUFFERS_1024
  14468. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  14469. #elif defined(USE_CERT_BUFFERS_2048)
  14470. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  14471. #elif defined(USE_CERT_BUFFERS_3072)
  14472. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  14473. #elif defined(USE_CERT_BUFFERS_4096)
  14474. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  14475. #elif !defined(NO_FILESYSTEM)
  14476. file = XFOPEN(clientKey, "rb");
  14477. if (!file) {
  14478. err_sys("can't open ./certs/client-key.der, "
  14479. "Please run from wolfSSL home dir", -40);
  14480. ERROR_OUT(-7901, exit_rsa);
  14481. }
  14482. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  14483. XFCLOSE(file);
  14484. #else
  14485. /* No key to use. */
  14486. ERROR_OUT(-7902, exit_rsa);
  14487. #endif /* USE_CERT_BUFFERS */
  14488. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  14489. if (ret != 0) {
  14490. ERROR_OUT(-7903, exit_rsa);
  14491. }
  14492. #ifndef NO_ASN
  14493. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  14494. if (ret != 0) {
  14495. ERROR_OUT(-7904, exit_rsa);
  14496. }
  14497. #ifndef NO_SIG_WRAPPER
  14498. modLen = wc_RsaEncryptSize(key);
  14499. #endif
  14500. #else
  14501. #ifdef USE_CERT_BUFFERS_2048
  14502. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  14503. if (ret != 0) {
  14504. ERROR_OUT(-7905, exit_rsa);
  14505. }
  14506. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  14507. if (ret != 0) {
  14508. ERROR_OUT(-7906, exit_rsa);
  14509. }
  14510. #ifndef NO_SIG_WRAPPER
  14511. modLen = 2048;
  14512. #endif
  14513. #else
  14514. #error Not supported yet!
  14515. #endif
  14516. #endif
  14517. #ifndef WC_NO_RNG
  14518. #ifndef HAVE_FIPS
  14519. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  14520. #else
  14521. ret = wc_InitRng(&rng);
  14522. #endif
  14523. if (ret != 0) {
  14524. ERROR_OUT(-7907, exit_rsa);
  14525. }
  14526. #endif
  14527. #ifndef NO_SIG_WRAPPER
  14528. ret = rsa_sig_test(key, sizeof *key, modLen, &rng);
  14529. if (ret != 0)
  14530. goto exit_rsa;
  14531. #endif
  14532. #ifdef WC_RSA_NONBLOCK
  14533. ret = rsa_nb_test(key, in, inLen, out, outSz, plain, plainSz, &rng);
  14534. if (ret != 0)
  14535. goto exit_rsa;
  14536. #endif
  14537. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  14538. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14539. do {
  14540. #if defined(WOLFSSL_ASYNC_CRYPT)
  14541. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14542. #endif
  14543. if (ret >= 0) {
  14544. ret = wc_RsaPublicEncrypt(in, inLen, out, outSz, key, &rng);
  14545. }
  14546. } while (ret == WC_PENDING_E);
  14547. if (ret < 0) {
  14548. ERROR_OUT(-7908, exit_rsa);
  14549. }
  14550. TEST_SLEEP();
  14551. #ifdef WC_RSA_BLINDING
  14552. {
  14553. int tmpret = ret;
  14554. ret = wc_RsaSetRNG(key, &rng);
  14555. if (ret < 0) {
  14556. ERROR_OUT(-7909, exit_rsa);
  14557. }
  14558. ret = tmpret;
  14559. }
  14560. #endif
  14561. idx = (word32)ret; /* save off encrypted length */
  14562. do {
  14563. #if defined(WOLFSSL_ASYNC_CRYPT)
  14564. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14565. #endif
  14566. if (ret >= 0) {
  14567. ret = wc_RsaPrivateDecrypt(out, idx, plain, plainSz, key);
  14568. }
  14569. } while (ret == WC_PENDING_E);
  14570. if (ret < 0) {
  14571. ERROR_OUT(-7910, exit_rsa);
  14572. }
  14573. if (XMEMCMP(plain, in, inLen)) {
  14574. ERROR_OUT(-7911, exit_rsa);
  14575. }
  14576. TEST_SLEEP();
  14577. do {
  14578. #if defined(WOLFSSL_ASYNC_CRYPT)
  14579. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14580. #endif
  14581. if (ret >= 0) {
  14582. ret = wc_RsaPrivateDecryptInline(out, idx, &res, key);
  14583. }
  14584. } while (ret == WC_PENDING_E);
  14585. if (ret < 0) {
  14586. ERROR_OUT(-7912, exit_rsa);
  14587. }
  14588. if (ret != (int)inLen) {
  14589. ERROR_OUT(-7913, exit_rsa);
  14590. }
  14591. if (XMEMCMP(res, in, inLen)) {
  14592. ERROR_OUT(-7914, exit_rsa);
  14593. }
  14594. TEST_SLEEP();
  14595. do {
  14596. #if defined(WOLFSSL_ASYNC_CRYPT)
  14597. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14598. #endif
  14599. if (ret >= 0) {
  14600. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, &rng);
  14601. }
  14602. } while (ret == WC_PENDING_E);
  14603. if (ret < 0) {
  14604. ERROR_OUT(-7915, exit_rsa);
  14605. }
  14606. TEST_SLEEP();
  14607. #elif defined(WOLFSSL_PUBLIC_MP)
  14608. {
  14609. static byte signature_2048[] = {
  14610. 0x07, 0x6f, 0xc9, 0x85, 0x73, 0x9e, 0x21, 0x79,
  14611. 0x47, 0xf1, 0xa3, 0xd7, 0xf4, 0x27, 0x29, 0xbe,
  14612. 0x99, 0x5d, 0xac, 0xb2, 0x10, 0x3f, 0x95, 0xda,
  14613. 0x89, 0x23, 0xb8, 0x96, 0x13, 0x57, 0x72, 0x30,
  14614. 0xa1, 0xfe, 0x5a, 0x68, 0x9c, 0x99, 0x9d, 0x1e,
  14615. 0x05, 0xa4, 0x80, 0xb0, 0xbb, 0xd9, 0xd9, 0xa1,
  14616. 0x69, 0x97, 0x74, 0xb3, 0x41, 0x21, 0x3b, 0x47,
  14617. 0xf5, 0x51, 0xb1, 0xfb, 0xc7, 0xaa, 0xcc, 0xdc,
  14618. 0xcd, 0x76, 0xa0, 0x28, 0x4d, 0x27, 0x14, 0xa4,
  14619. 0xb9, 0x41, 0x68, 0x7c, 0xb3, 0x66, 0xe6, 0x6f,
  14620. 0x40, 0x76, 0xe4, 0x12, 0xfd, 0xae, 0x29, 0xb5,
  14621. 0x63, 0x60, 0x87, 0xce, 0x49, 0x6b, 0xf3, 0x05,
  14622. 0x9a, 0x14, 0xb5, 0xcc, 0xcd, 0xf7, 0x30, 0x95,
  14623. 0xd2, 0x72, 0x52, 0x1d, 0x5b, 0x7e, 0xef, 0x4a,
  14624. 0x02, 0x96, 0x21, 0x6c, 0x55, 0xa5, 0x15, 0xb1,
  14625. 0x57, 0x63, 0x2c, 0xa3, 0x8e, 0x9d, 0x3d, 0x45,
  14626. 0xcc, 0xb8, 0xe6, 0xa1, 0xc8, 0x59, 0xcd, 0xf5,
  14627. 0xdc, 0x0a, 0x51, 0xb6, 0x9d, 0xfb, 0xf4, 0x6b,
  14628. 0xfd, 0x32, 0x71, 0x6e, 0xcf, 0xcb, 0xb3, 0xd9,
  14629. 0xe0, 0x4a, 0x77, 0x34, 0xd6, 0x61, 0xf5, 0x7c,
  14630. 0xf9, 0xa9, 0xa4, 0xb0, 0x8e, 0x3b, 0xd6, 0x04,
  14631. 0xe0, 0xde, 0x2b, 0x5b, 0x5a, 0xbf, 0xd9, 0xef,
  14632. 0x8d, 0xa3, 0xf5, 0xb1, 0x67, 0xf3, 0xb9, 0x72,
  14633. 0x0a, 0x37, 0x12, 0x35, 0x6c, 0x8e, 0x10, 0x8b,
  14634. 0x38, 0x06, 0x16, 0x4b, 0x20, 0x20, 0x13, 0x00,
  14635. 0x2e, 0x6d, 0xc2, 0x59, 0x23, 0x67, 0x4a, 0x6d,
  14636. 0xa1, 0x46, 0x8b, 0xee, 0xcf, 0x44, 0xb4, 0x3e,
  14637. 0x56, 0x75, 0x00, 0x68, 0xb5, 0x7d, 0x0f, 0x20,
  14638. 0x79, 0x5d, 0x7f, 0x12, 0x15, 0x32, 0x89, 0x61,
  14639. 0x6b, 0x29, 0xb7, 0x52, 0xf5, 0x25, 0xd8, 0x98,
  14640. 0xe8, 0x6f, 0xf9, 0x22, 0xb4, 0xbb, 0xe5, 0xff,
  14641. 0xd0, 0x92, 0x86, 0x9a, 0x88, 0xa2, 0xaf, 0x6b
  14642. };
  14643. ret = sizeof(signature_2048);
  14644. XMEMCPY(out, signature_2048, ret);
  14645. }
  14646. #endif
  14647. #if !defined(WC_NO_RNG) && !defined(WC_NO_RSA_OAEP) && \
  14648. ((!defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || \
  14649. defined(WOLFSSL_PUBLIC_MP)) && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14650. idx = (word32)ret;
  14651. XMEMSET(plain, 0, plainSz);
  14652. do {
  14653. #if defined(WOLFSSL_ASYNC_CRYPT)
  14654. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14655. #endif
  14656. if (ret >= 0) {
  14657. #ifndef WOLFSSL_RSA_VERIFY_INLINE
  14658. #if defined(WOLFSSL_CRYPTOCELL)
  14659. /*
  14660. Cryptocell requires the input data and signature byte array to verify.
  14661. first argument must be the input data
  14662. second argument must be the length of input data
  14663. third argument must be the signature byte array or the output from
  14664. wc_RsaSSL_Sign()
  14665. fourth argument must be the length of the signature byte array
  14666. */
  14667. ret = wc_RsaSSL_Verify(in, inLen, out, outSz, key);
  14668. #else
  14669. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, key);
  14670. #endif /* WOLFSSL_CRYPTOCELL */
  14671. #else
  14672. byte* dec = NULL;
  14673. ret = wc_RsaSSL_VerifyInline(out, idx, &dec, key);
  14674. if (ret > 0) {
  14675. XMEMCPY(plain, dec, ret);
  14676. }
  14677. #endif
  14678. }
  14679. } while (ret == WC_PENDING_E);
  14680. if (ret < 0) {
  14681. ERROR_OUT(-7916, exit_rsa);
  14682. }
  14683. if (XMEMCMP(plain, in, (size_t)ret)) {
  14684. ERROR_OUT(-7917, exit_rsa);
  14685. }
  14686. TEST_SLEEP();
  14687. #endif
  14688. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  14689. #if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG)
  14690. #if !defined(HAVE_FAST_RSA) && !defined(HAVE_USER_RSA) && \
  14691. (!defined(HAVE_FIPS) || \
  14692. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) \
  14693. && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14694. ret = rsa_oaep_padding_test(key, &rng);
  14695. if (ret != 0)
  14696. return ret;
  14697. #endif /* !HAVE_FAST_RSA && !HAVE_FIPS */
  14698. #endif /* WC_NO_RSA_OAEP && !WC_NO_RNG */
  14699. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  14700. #if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(NO_ASN) \
  14701. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  14702. ret = rsa_export_key_test(key);
  14703. if (ret != 0)
  14704. return ret;
  14705. #endif
  14706. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  14707. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  14708. ret = rsa_flatten_test(key);
  14709. if (ret != 0)
  14710. return ret;
  14711. #endif
  14712. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_ASN) && \
  14713. !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  14714. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  14715. (void)clientCert;
  14716. #endif
  14717. #ifdef WOLFSSL_TEST_CERT
  14718. #if defined(WOLFSSL_MDK_ARM)
  14719. #define sizeof(s) XSTRLEN((char *)(s))
  14720. #endif
  14721. #ifdef USE_CERT_BUFFERS_1024
  14722. XMEMCPY(tmp, client_cert_der_1024, (size_t)sizeof_client_cert_der_1024);
  14723. bytes = (size_t)sizeof_client_cert_der_1024;
  14724. #elif defined(USE_CERT_BUFFERS_2048)
  14725. XMEMCPY(tmp, client_cert_der_2048, (size_t)sizeof_client_cert_der_2048);
  14726. bytes = (size_t)sizeof_client_cert_der_2048;
  14727. #elif defined(USE_CERT_BUFFERS_3072)
  14728. XMEMCPY(tmp, client_cert_der_3072, (size_t)sizeof_client_cert_der_3072);
  14729. bytes = (size_t)sizeof_client_cert_der_3072;
  14730. #elif defined(USE_CERT_BUFFERS_4096)
  14731. XMEMCPY(tmp, client_cert_der_4096, (size_t)sizeof_client_cert_der_4096);
  14732. bytes = (size_t)sizeof_client_cert_der_4096;
  14733. #elif !defined(NO_FILESYSTEM)
  14734. file2 = XFOPEN(clientCert, "rb");
  14735. if (!file2) {
  14736. ERROR_OUT(-7940, exit_rsa);
  14737. }
  14738. bytes = XFREAD(tmp, 1, FOURK_BUF, file2);
  14739. XFCLOSE(file2);
  14740. #else
  14741. /* No certificate to use. */
  14742. ERROR_OUT(-7941, exit_rsa);
  14743. #endif
  14744. #ifdef sizeof
  14745. #undef sizeof
  14746. #endif
  14747. InitDecodedCert(cert, tmp, (word32)bytes, NULL);
  14748. ret = ParseCert(cert, CERT_TYPE, NO_VERIFY, NULL);
  14749. if (ret != 0) {
  14750. FreeDecodedCert(cert);
  14751. ERROR_OUT(-7942, exit_rsa);
  14752. }
  14753. #ifndef NO_ASN_TIME
  14754. ret = wc_GetDateInfo(cert->afterDate, cert->afterDateLen, &date,
  14755. &dateFormat, &dateLength);
  14756. if (ret != 0) {
  14757. FreeDecodedCert(cert);
  14758. ERROR_OUT(-7943, exit_rsa);
  14759. }
  14760. ret = wc_GetDateAsCalendarTime(date, dateLength, dateFormat, &timearg);
  14761. if (ret != 0) {
  14762. FreeDecodedCert(cert);
  14763. ERROR_OUT(-7944, exit_rsa);
  14764. }
  14765. #endif
  14766. FreeDecodedCert(cert);
  14767. #endif /* WOLFSSL_TEST_CERT */
  14768. #ifdef WOLFSSL_CERT_EXT
  14769. #ifdef USE_CERT_BUFFERS_1024
  14770. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  14771. bytes = sizeof_client_keypub_der_1024;
  14772. #elif defined(USE_CERT_BUFFERS_2048)
  14773. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  14774. bytes = sizeof_client_keypub_der_2048;
  14775. #elif defined(USE_CERT_BUFFERS_3072)
  14776. XMEMCPY(tmp, client_keypub_der_3072, sizeof_client_keypub_der_3072);
  14777. bytes = sizeof_client_keypub_der_3072;
  14778. #elif defined(USE_CERT_BUFFERS_4096)
  14779. XMEMCPY(tmp, client_keypub_der_4096, sizeof_client_keypub_der_4096);
  14780. bytes = sizeof_client_keypub_der_4096;
  14781. #else
  14782. file = XFOPEN(clientKeyPub, "rb");
  14783. if (!file) {
  14784. err_sys("can't open ./certs/client-keyPub.der, "
  14785. "Please run from wolfSSL home dir", -40);
  14786. ERROR_OUT(-7945, exit_rsa);
  14787. }
  14788. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  14789. XFCLOSE(file);
  14790. #endif /* USE_CERT_BUFFERS */
  14791. ret = wc_InitRsaKey(keypub, HEAP_HINT);
  14792. if (ret != 0) {
  14793. ERROR_OUT(-7946, exit_rsa);
  14794. }
  14795. idx = 0;
  14796. ret = wc_RsaPublicKeyDecode(tmp, &idx, keypub, (word32)bytes);
  14797. if (ret != 0) {
  14798. ERROR_OUT(-7947, exit_rsa);
  14799. }
  14800. #endif /* WOLFSSL_CERT_EXT */
  14801. #ifdef WOLFSSL_KEY_GEN
  14802. ret = rsa_keygen_test(&rng);
  14803. if (ret != 0)
  14804. goto exit_rsa;
  14805. #endif
  14806. #ifdef WOLFSSL_CERT_GEN
  14807. /* Make Cert / Sign example for RSA cert and RSA CA */
  14808. ret = rsa_certgen_test(key, keypub, &rng, tmp);
  14809. if (ret != 0)
  14810. goto exit_rsa;
  14811. #if !defined(NO_RSA) && defined(HAVE_ECC)
  14812. ret = rsa_ecc_certgen_test(&rng, tmp);
  14813. if (ret != 0)
  14814. goto exit_rsa;
  14815. #endif
  14816. #if defined(WOLFSSL_CERT_REQ) && !defined(WOLFSSL_NO_MALLOC)
  14817. {
  14818. Cert *req;
  14819. int derSz;
  14820. #ifndef WOLFSSL_SMALL_STACK
  14821. byte* der = NULL;
  14822. #endif
  14823. req = (Cert *)XMALLOC(sizeof *req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14824. if (! req)
  14825. ERROR_OUT(MEMORY_E, exit_rsa);
  14826. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,DYNAMIC_TYPE_TMP_BUFFER);
  14827. if (der == NULL) {
  14828. ERROR_OUT(-7964, exit_rsa);
  14829. }
  14830. if (wc_InitCert_ex(req, HEAP_HINT, devId)) {
  14831. ERROR_OUT(-7965, exit_rsa);
  14832. }
  14833. req->version = 0;
  14834. req->isCA = 1;
  14835. XSTRNCPY(req->challengePw, "wolf123", CTC_NAME_SIZE);
  14836. XMEMCPY(&req->subject, &certDefaultName, sizeof(CertName));
  14837. #ifndef NO_SHA256
  14838. req->sigType = CTC_SHA256wRSA;
  14839. #else
  14840. req->sigType = CTC_SHAwRSA;
  14841. #endif
  14842. #ifdef WOLFSSL_CERT_EXT
  14843. /* add SKID from the Public Key */
  14844. if (wc_SetSubjectKeyIdFromPublicKey(req, keypub, NULL) != 0) {
  14845. ERROR_OUT(-7966, exit_rsa);
  14846. }
  14847. /* add Key Usage */
  14848. if (wc_SetKeyUsage(req, certKeyUsage2) != 0) {
  14849. ERROR_OUT(-7967, exit_rsa);
  14850. }
  14851. /* add Extended Key Usage */
  14852. if (wc_SetExtKeyUsage(req, "serverAuth,clientAuth,codeSigning,"
  14853. "emailProtection,timeStamping,OCSPSigning") != 0) {
  14854. ERROR_OUT(-7968, exit_rsa);
  14855. }
  14856. #ifdef WOLFSSL_EKU_OID
  14857. {
  14858. WOLFSSL_SMALL_STACK_STATIC const char unique[] = "2.16.840.1.111111.100.1.10.1";
  14859. if (wc_SetExtKeyUsageOID(req, unique, sizeof(unique), 0,
  14860. HEAP_HINT) != 0) {
  14861. ERROR_OUT(-7969, exit_rsa);
  14862. }
  14863. }
  14864. #endif /* WOLFSSL_EKU_OID */
  14865. #endif /* WOLFSSL_CERT_EXT */
  14866. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  14867. if (derSz < 0) {
  14868. ERROR_OUT(-7970, exit_rsa);
  14869. }
  14870. #ifdef WOLFSSL_CERT_EXT
  14871. /* Try again with "any" flag set, will override all others */
  14872. if (wc_SetExtKeyUsage(req, "any") != 0) {
  14873. ERROR_OUT(-7971, exit_rsa);
  14874. }
  14875. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  14876. if (derSz < 0) {
  14877. ERROR_OUT(-7972, exit_rsa);
  14878. }
  14879. #endif /* WOLFSSL_CERT_EXT */
  14880. ret = 0;
  14881. do {
  14882. #if defined(WOLFSSL_ASYNC_CRYPT)
  14883. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14884. #endif
  14885. if (ret >= 0) {
  14886. ret = wc_SignCert(req->bodySz, req->sigType, der, FOURK_BUF,
  14887. key, NULL, &rng);
  14888. }
  14889. } while (ret == WC_PENDING_E);
  14890. if (ret < 0) {
  14891. ERROR_OUT(-7973, exit_rsa);
  14892. }
  14893. derSz = ret;
  14894. ret = SaveDerAndPem(der, derSz, certReqDerFile, certReqPemFile,
  14895. CERTREQ_TYPE, -5650);
  14896. if (ret != 0) {
  14897. goto exit_rsa;
  14898. }
  14899. derSz = wc_MakeCertReq_ex(req, der, FOURK_BUF, RSA_TYPE, key);
  14900. if (derSz < 0) {
  14901. ERROR_OUT(-7974, exit_rsa);
  14902. }
  14903. /* Test getting the size of the buffer without providing the buffer.
  14904. * derSz is set to the "largest buffer" we are willing to allocate. */
  14905. derSz = wc_MakeCertReq(req, NULL, 10000, key, NULL);
  14906. if (derSz < 0) {
  14907. ERROR_OUT(-7975, exit_rsa);
  14908. }
  14909. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14910. XFREE(req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14911. der = NULL;
  14912. }
  14913. #endif /* WOLFSSL_CERT_REQ */
  14914. #endif /* WOLFSSL_CERT_GEN */
  14915. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  14916. /* Need to create known good signatures to test with this. */
  14917. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  14918. !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14919. ret = rsa_pss_test(&rng, key);
  14920. if (ret != 0)
  14921. goto exit_rsa;
  14922. #endif
  14923. #endif
  14924. #if defined(WOLFSSL_HAVE_SP_RSA) && defined(USE_FAST_MATH)
  14925. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14926. /* New key to be loaded in rsa_even_mod_test(). */
  14927. if (key != NULL)
  14928. #endif
  14929. wc_FreeRsaKey(key);
  14930. /* New key to be loaded in rsa_even_mod_test(). */
  14931. ret = rsa_even_mod_test(&rng, key);
  14932. #endif
  14933. exit_rsa:
  14934. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14935. if (key != NULL) {
  14936. wc_FreeRsaKey(key);
  14937. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14938. }
  14939. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  14940. if (keypub != NULL) {
  14941. wc_FreeRsaKey(keypub);
  14942. XFREE(keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14943. }
  14944. #endif
  14945. #ifdef WOLFSSL_TEST_CERT
  14946. if (cert != NULL)
  14947. XFREE(cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14948. #endif
  14949. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14950. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14951. #else
  14952. wc_FreeRsaKey(key);
  14953. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  14954. wc_FreeRsaKey(keypub);
  14955. #endif
  14956. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  14957. wc_FreeRng(&rng);
  14958. WC_FREE_VAR(in, HEAP_HINT);
  14959. WC_FREE_VAR(out, HEAP_HINT);
  14960. WC_FREE_VAR(plain, HEAP_HINT);
  14961. (void)res;
  14962. (void)bytes;
  14963. (void)idx;
  14964. (void)in;
  14965. (void)out;
  14966. (void)plain;
  14967. (void)idx;
  14968. (void)inStr;
  14969. (void)inLen;
  14970. (void)outSz;
  14971. (void)plainSz;
  14972. /* ret can be greater then 0 with certgen but all negative values should
  14973. * be returned and treated as an error */
  14974. if (ret >= 0) {
  14975. return 0;
  14976. }
  14977. else {
  14978. return ret;
  14979. }
  14980. }
  14981. #endif /* !NO_RSA */
  14982. #ifndef NO_DH
  14983. static int dh_fips_generate_test(WC_RNG *rng)
  14984. {
  14985. int ret = 0;
  14986. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14987. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14988. #else
  14989. DhKey key[1];
  14990. #endif
  14991. WOLFSSL_SMALL_STACK_STATIC const byte p[] = {
  14992. 0xc5, 0x7c, 0xa2, 0x4f, 0x4b, 0xd6, 0x8c, 0x3c,
  14993. 0xda, 0xc7, 0xba, 0xaa, 0xea, 0x2e, 0x5c, 0x1e,
  14994. 0x18, 0xb2, 0x7b, 0x8c, 0x55, 0x65, 0x9f, 0xea,
  14995. 0xe0, 0xa1, 0x36, 0x53, 0x2b, 0x36, 0xe0, 0x4e,
  14996. 0x3e, 0x64, 0xa9, 0xe4, 0xfc, 0x8f, 0x32, 0x62,
  14997. 0x97, 0xe4, 0xbe, 0xf7, 0xc1, 0xde, 0x07, 0x5a,
  14998. 0x89, 0x28, 0xf3, 0xfe, 0x4f, 0xfe, 0x68, 0xbc,
  14999. 0xfb, 0x0a, 0x7c, 0xa4, 0xb3, 0x14, 0x48, 0x89,
  15000. 0x9f, 0xaf, 0xb8, 0x43, 0xe2, 0xa0, 0x62, 0x5c,
  15001. 0xb4, 0x88, 0x3f, 0x06, 0x50, 0x11, 0xfe, 0x65,
  15002. 0x8d, 0x49, 0xd2, 0xf5, 0x4b, 0x74, 0x79, 0xdb,
  15003. 0x06, 0x62, 0x92, 0x89, 0xed, 0xda, 0xcb, 0x87,
  15004. 0x37, 0x16, 0xd2, 0xa1, 0x7a, 0xe8, 0xde, 0x92,
  15005. 0xee, 0x3e, 0x41, 0x4a, 0x91, 0x5e, 0xed, 0xf3,
  15006. 0x6c, 0x6b, 0x7e, 0xfd, 0x15, 0x92, 0x18, 0xfc,
  15007. 0xa7, 0xac, 0x42, 0x85, 0x57, 0xe9, 0xdc, 0xda,
  15008. 0x55, 0xc9, 0x8b, 0x28, 0x9e, 0xc1, 0xc4, 0x46,
  15009. 0x4d, 0x88, 0xed, 0x62, 0x8e, 0xdb, 0x3f, 0xb9,
  15010. 0xd7, 0xc8, 0xe3, 0xcf, 0xb8, 0x34, 0x2c, 0xd2,
  15011. 0x6f, 0x28, 0x06, 0x41, 0xe3, 0x66, 0x8c, 0xfc,
  15012. 0x72, 0xff, 0x26, 0x3b, 0x6b, 0x6c, 0x6f, 0x73,
  15013. 0xde, 0xf2, 0x90, 0x29, 0xe0, 0x61, 0x32, 0xc4,
  15014. 0x12, 0x74, 0x09, 0x52, 0xec, 0xf3, 0x1b, 0xa6,
  15015. 0x45, 0x98, 0xac, 0xf9, 0x1c, 0x65, 0x8e, 0x3a,
  15016. 0x91, 0x84, 0x4b, 0x23, 0x8a, 0xb2, 0x3c, 0xc9,
  15017. 0xfa, 0xea, 0xf1, 0x38, 0xce, 0xd8, 0x05, 0xe0,
  15018. 0xfa, 0x44, 0x68, 0x1f, 0xeb, 0xd9, 0x57, 0xb8,
  15019. 0x4a, 0x97, 0x5b, 0x88, 0xc5, 0xf1, 0xbb, 0xb0,
  15020. 0x49, 0xc3, 0x91, 0x7c, 0xd3, 0x13, 0xb9, 0x47,
  15021. 0xbb, 0x91, 0x8f, 0xe5, 0x26, 0x07, 0xab, 0xa9,
  15022. 0xc5, 0xd0, 0x3d, 0x95, 0x41, 0x26, 0x92, 0x9d,
  15023. 0x13, 0x67, 0xf2, 0x7e, 0x11, 0x88, 0xdc, 0x2d
  15024. };
  15025. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  15026. 0x4a, 0x1a, 0xf3, 0xa4, 0x92, 0xe9, 0xee, 0x74,
  15027. 0x6e, 0x57, 0xd5, 0x8c, 0x2c, 0x5b, 0x41, 0x41,
  15028. 0x5e, 0xd4, 0x55, 0x19, 0xdc, 0xd9, 0x32, 0x91,
  15029. 0xf7, 0xfd, 0xc2, 0x57, 0xff, 0x03, 0x14, 0xdb,
  15030. 0xf1, 0xb7, 0x60, 0x0c, 0x43, 0x59, 0x3f, 0xff,
  15031. 0xac, 0xf1, 0x80, 0x9a, 0x15, 0x6f, 0xd8, 0x6e,
  15032. 0xb7, 0x85, 0x18, 0xc8, 0xec, 0x4e, 0x59, 0x4a,
  15033. 0xe2, 0x91, 0x43, 0x4c, 0xeb, 0x95, 0xb6, 0x2e,
  15034. 0x9a, 0xea, 0x53, 0x68, 0x80, 0x64, 0x69, 0x40,
  15035. 0xf9, 0xec, 0xbd, 0x85, 0x89, 0x26, 0x97, 0x67,
  15036. 0xaf, 0xb0, 0xad, 0x00, 0x1b, 0xd4, 0xfd, 0x94,
  15037. 0xd3, 0xe9, 0x92, 0xb1, 0xb4, 0xbc, 0x5a, 0xaa,
  15038. 0x92, 0x80, 0x89, 0x3b, 0x39, 0x05, 0x6c, 0x22,
  15039. 0x26, 0xfe, 0x5a, 0x28, 0x6c, 0x37, 0x50, 0x5a,
  15040. 0x38, 0x99, 0xcf, 0xf3, 0xc1, 0x96, 0x45, 0xdc,
  15041. 0x01, 0xcb, 0x20, 0x87, 0xa5, 0x00, 0x8c, 0xf5,
  15042. 0x4d, 0xc2, 0xef, 0xb8, 0x9b, 0xd1, 0x87, 0xbe,
  15043. 0xed, 0xd5, 0x0a, 0x29, 0x15, 0x34, 0x59, 0x4c,
  15044. 0x3a, 0x05, 0x22, 0x05, 0x44, 0x4f, 0x9f, 0xc8,
  15045. 0x47, 0x12, 0x24, 0x8e, 0xa8, 0x79, 0xe4, 0x67,
  15046. 0xba, 0x4d, 0x5b, 0x75, 0x56, 0x95, 0xeb, 0xe8,
  15047. 0x8a, 0xfa, 0x8e, 0x01, 0x8c, 0x1b, 0x74, 0x63,
  15048. 0xd9, 0x2f, 0xf7, 0xd3, 0x44, 0x8f, 0xa8, 0xf5,
  15049. 0xaf, 0x6c, 0x4f, 0xdb, 0xe7, 0xc9, 0x6c, 0x71,
  15050. 0x22, 0xa3, 0x1d, 0xf1, 0x40, 0xb2, 0xe0, 0x9a,
  15051. 0xb6, 0x72, 0xc9, 0xc0, 0x13, 0x16, 0xa2, 0x4a,
  15052. 0xe1, 0x92, 0xc7, 0x54, 0x23, 0xab, 0x9d, 0xa1,
  15053. 0xa1, 0xe5, 0x0b, 0xed, 0xba, 0xe8, 0x84, 0x37,
  15054. 0xb2, 0xe7, 0xfe, 0x32, 0x8d, 0xfa, 0x1c, 0x53,
  15055. 0x77, 0x97, 0xc7, 0xf3, 0x48, 0xc9, 0xdb, 0x2d,
  15056. 0x75, 0x52, 0x9d, 0x42, 0x51, 0x78, 0x62, 0x68,
  15057. 0x05, 0x45, 0x15, 0xf8, 0xa2, 0x4e, 0xf3, 0x0b
  15058. };
  15059. WOLFSSL_SMALL_STACK_STATIC const byte q[] = {
  15060. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  15061. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  15062. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  15063. 0x40, 0x52, 0xed, 0x41
  15064. };
  15065. WOLFSSL_SMALL_STACK_STATIC const byte q0[] = {
  15066. 0x00,
  15067. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  15068. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  15069. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  15070. 0x40, 0x52, 0xed, 0x41
  15071. };
  15072. byte priv[256];
  15073. byte pub[256];
  15074. word32 privSz = sizeof(priv);
  15075. word32 pubSz = sizeof(pub);
  15076. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15077. if (key == NULL)
  15078. ERROR_OUT(MEMORY_E, exit_gen_test);
  15079. #endif
  15080. /* Parameter Validation testing. */
  15081. ret = wc_DhGenerateKeyPair(NULL, rng, priv, &privSz, pub, &pubSz);
  15082. if (ret != BAD_FUNC_ARG)
  15083. ERROR_OUT(-7980, exit_gen_test);
  15084. ret = wc_DhGenerateKeyPair(key, NULL, priv, &privSz, pub, &pubSz);
  15085. if (ret != BAD_FUNC_ARG)
  15086. ERROR_OUT(-7981, exit_gen_test);
  15087. ret = wc_DhGenerateKeyPair(key, rng, NULL, &privSz, pub, &pubSz);
  15088. if (ret != BAD_FUNC_ARG)
  15089. ERROR_OUT(-7982, exit_gen_test);
  15090. ret = wc_DhGenerateKeyPair(key, rng, priv, NULL, pub, &pubSz);
  15091. if (ret != BAD_FUNC_ARG)
  15092. ERROR_OUT(-7983, exit_gen_test);
  15093. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, NULL, &pubSz);
  15094. if (ret != BAD_FUNC_ARG)
  15095. ERROR_OUT(-7984, exit_gen_test);
  15096. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, NULL);
  15097. if (ret != BAD_FUNC_ARG)
  15098. ERROR_OUT(-7985, exit_gen_test);
  15099. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15100. if (ret != 0)
  15101. ERROR_OUT(-7986, exit_gen_test);
  15102. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q0, sizeof(q0));
  15103. if (ret != 0) {
  15104. ERROR_OUT(-7987, exit_gen_test);
  15105. }
  15106. wc_FreeDhKey(key);
  15107. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15108. if (ret != 0)
  15109. ERROR_OUT(-7988, exit_gen_test);
  15110. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q, sizeof(q));
  15111. if (ret != 0) {
  15112. ERROR_OUT(-7989, exit_gen_test);
  15113. }
  15114. /* Use API. */
  15115. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  15116. #if defined(WOLFSSL_ASYNC_CRYPT)
  15117. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15118. #endif
  15119. if (ret != 0) {
  15120. ERROR_OUT(-7990, exit_gen_test);
  15121. }
  15122. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q0, sizeof(q0));
  15123. if (ret != 0) {
  15124. ERROR_OUT(-7991, exit_gen_test);
  15125. }
  15126. wc_FreeDhKey(key);
  15127. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15128. if (ret != 0)
  15129. ERROR_OUT(-7992, exit_gen_test);
  15130. ret = wc_DhSetKey(key, p, sizeof(p), g, sizeof(g));
  15131. if (ret != 0) {
  15132. ERROR_OUT(-7993, exit_gen_test);
  15133. }
  15134. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q, sizeof(q));
  15135. if (ret != 0) {
  15136. ERROR_OUT(-7994, exit_gen_test);
  15137. }
  15138. #ifndef HAVE_SELFTEST
  15139. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  15140. if (ret != 0) {
  15141. ERROR_OUT(-7995, exit_gen_test);
  15142. }
  15143. /* Taint the public key so the check fails. */
  15144. pub[0]++;
  15145. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  15146. if (ret != MP_CMP_E) {
  15147. ERROR_OUT(-7996, exit_gen_test);
  15148. }
  15149. #ifdef WOLFSSL_KEY_GEN
  15150. wc_FreeDhKey(key);
  15151. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15152. if (ret != 0)
  15153. ERROR_OUT(-7997, exit_gen_test);
  15154. ret = wc_DhGenerateParams(rng, 2048, key);
  15155. if (ret != 0) {
  15156. ERROR_OUT(-7998, exit_gen_test);
  15157. }
  15158. privSz = sizeof(priv);
  15159. pubSz = sizeof(pub);
  15160. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  15161. #if defined(WOLFSSL_ASYNC_CRYPT)
  15162. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15163. #endif
  15164. if (ret != 0) {
  15165. ERROR_OUT(-7999, exit_gen_test);
  15166. }
  15167. #endif /* WOLFSSL_KEY_GEN */
  15168. #endif /* HAVE_SELFTEST */
  15169. ret = 0;
  15170. exit_gen_test:
  15171. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15172. if (key) {
  15173. wc_FreeDhKey(key);
  15174. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15175. }
  15176. #else
  15177. wc_FreeDhKey(key);
  15178. #endif
  15179. return ret;
  15180. }
  15181. static int dh_generate_test(WC_RNG *rng)
  15182. {
  15183. int ret = 0;
  15184. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15185. DhKey *smallKey = NULL;
  15186. #else
  15187. DhKey smallKey[1];
  15188. #endif
  15189. byte p[2] = { 1, 7 }; /* 263 in decimal */
  15190. byte g[2] = { 0, 2 };
  15191. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FFDHE)
  15192. #ifdef WOLFSSL_DH_CONST
  15193. /* the table for constant DH lookup will round to the lowest byte size 21 */
  15194. byte priv[21];
  15195. byte pub[21];
  15196. #else
  15197. byte priv[2];
  15198. byte pub[2];
  15199. #endif
  15200. word32 privSz = sizeof(priv);
  15201. word32 pubSz = sizeof(pub);
  15202. #endif
  15203. int smallKey_inited = 0;
  15204. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15205. if ((smallKey = (DhKey *)XMALLOC(sizeof(*smallKey), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  15206. return -8019;
  15207. #endif
  15208. ret = wc_InitDhKey_ex(smallKey, HEAP_HINT, devId);
  15209. if (ret != 0)
  15210. ERROR_OUT(-8010, exit_gen_test);
  15211. smallKey_inited = 1;
  15212. /* Parameter Validation testing. */
  15213. ret = wc_InitDhKey_ex(NULL, HEAP_HINT, devId);
  15214. if (ret != BAD_FUNC_ARG)
  15215. return -8011;
  15216. wc_FreeDhKey(NULL);
  15217. ret = wc_DhSetKey(NULL, p, sizeof(p), g, sizeof(g));
  15218. if (ret != BAD_FUNC_ARG) {
  15219. ERROR_OUT(-8012, exit_gen_test);
  15220. }
  15221. ret = wc_DhSetKey(smallKey, NULL, sizeof(p), g, sizeof(g));
  15222. if (ret != BAD_FUNC_ARG) {
  15223. ERROR_OUT(-8013, exit_gen_test);
  15224. }
  15225. ret = wc_DhSetKey(smallKey, p, 0, g, sizeof(g));
  15226. if (ret != BAD_FUNC_ARG) {
  15227. ERROR_OUT(-8014, exit_gen_test);
  15228. }
  15229. ret = wc_DhSetKey(smallKey, p, sizeof(p), NULL, sizeof(g));
  15230. if (ret != BAD_FUNC_ARG) {
  15231. ERROR_OUT(-8015, exit_gen_test);
  15232. }
  15233. ret = wc_DhSetKey(smallKey, p, sizeof(p), g, 0);
  15234. if (ret != BAD_FUNC_ARG) {
  15235. ERROR_OUT(-8016, exit_gen_test);
  15236. }
  15237. ret = wc_DhSetKey(smallKey, p, sizeof(p), g, sizeof(g));
  15238. if (ret != 0) {
  15239. ERROR_OUT(-8017, exit_gen_test);
  15240. }
  15241. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FFDHE)
  15242. /* Use API. */
  15243. ret = wc_DhGenerateKeyPair(smallKey, rng, priv, &privSz, pub, &pubSz);
  15244. #if defined(WOLFSSL_ASYNC_CRYPT)
  15245. ret = wc_AsyncWait(ret, &smallKey->asyncDev, WC_ASYNC_FLAG_NONE);
  15246. #endif
  15247. if (ret != 0) {
  15248. ret = -8018;
  15249. }
  15250. #else
  15251. (void)rng;
  15252. #if defined(HAVE_FIPS) || !defined(WOLFSSL_NO_DH186)
  15253. ret = 0;
  15254. #endif
  15255. #endif
  15256. #if !defined(HAVE_FIPS) && defined(WOLFSSL_NO_DH186)
  15257. {
  15258. byte priv[260];
  15259. byte pub[260];
  15260. word32 privSz = sizeof(priv);
  15261. word32 pubSz = sizeof(pub);
  15262. /* test odd ball param generation with DH */
  15263. wc_FreeDhKey(smallKey);
  15264. ret = wc_InitDhKey_ex(smallKey, HEAP_HINT, devId);
  15265. if (ret != 0)
  15266. ERROR_OUT(-8019, exit_gen_test);
  15267. ret = wc_DhGenerateParams(rng, 2056, smallKey);
  15268. if (ret != 0) {
  15269. ERROR_OUT(-8020, exit_gen_test);
  15270. }
  15271. privSz = sizeof(priv);
  15272. pubSz = sizeof(pub);
  15273. ret = wc_DhGenerateKeyPair(smallKey, rng, priv, &privSz, pub, &pubSz);
  15274. #if defined(WOLFSSL_ASYNC_CRYPT)
  15275. ret = wc_AsyncWait(ret, &smallKey->asyncDev, WC_ASYNC_FLAG_NONE);
  15276. #endif
  15277. if (ret != 0) {
  15278. ERROR_OUT(-8021, exit_gen_test);
  15279. }
  15280. }
  15281. #endif /* !HAVE_FIPS and WOLFSSL_NO_DH186 */
  15282. exit_gen_test:
  15283. if (smallKey_inited)
  15284. wc_FreeDhKey(smallKey);
  15285. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15286. if (smallKey != NULL)
  15287. XFREE(smallKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15288. #endif
  15289. return ret;
  15290. }
  15291. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  15292. typedef struct dh_pubvalue_test {
  15293. const byte* data;
  15294. word32 len;
  15295. } dh_pubvalue_test;
  15296. static int dh_test_check_pubvalue(void)
  15297. {
  15298. int ret;
  15299. word32 i;
  15300. WOLFSSL_SMALL_STACK_STATIC const byte prime[] = {0x01, 0x00, 0x01};
  15301. WOLFSSL_SMALL_STACK_STATIC const byte pubValZero[] = { 0x00 };
  15302. WOLFSSL_SMALL_STACK_STATIC const byte pubValZeroLong[] = { 0x00, 0x00, 0x00 };
  15303. WOLFSSL_SMALL_STACK_STATIC const byte pubValOne[] = { 0x01 };
  15304. WOLFSSL_SMALL_STACK_STATIC const byte pubValOneLong[] = { 0x00, 0x00, 0x01 };
  15305. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeMinusOne[] = { 0x01, 0x00, 0x00 };
  15306. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeLong[] = {0x00, 0x01, 0x00, 0x01};
  15307. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimePlusOne[] = { 0x01, 0x00, 0x02 };
  15308. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig0[] = { 0x02, 0x00, 0x01 };
  15309. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig1[] = { 0x01, 0x01, 0x01 };
  15310. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooLong[] = { 0x01, 0x00, 0x00, 0x01 };
  15311. const dh_pubvalue_test dh_pubval_fail[] = {
  15312. { prime, sizeof(prime) },
  15313. { pubValZero, sizeof(pubValZero) },
  15314. { pubValZeroLong, sizeof(pubValZeroLong) },
  15315. { pubValOne, sizeof(pubValOne) },
  15316. { pubValOneLong, sizeof(pubValOneLong) },
  15317. { pubValPrimeMinusOne, sizeof(pubValPrimeMinusOne) },
  15318. { pubValPrimeLong, sizeof(pubValPrimeLong) },
  15319. { pubValPrimePlusOne, sizeof(pubValPrimePlusOne) },
  15320. { pubValTooBig0, sizeof(pubValTooBig0) },
  15321. { pubValTooBig1, sizeof(pubValTooBig1) },
  15322. { pubValTooLong, sizeof(pubValTooLong) },
  15323. };
  15324. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwo[] = { 0x02 };
  15325. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwoLong[] = { 0x00, 0x00, 0x02 };
  15326. WOLFSSL_SMALL_STACK_STATIC const byte pubValGood[] = { 0x12, 0x34 };
  15327. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLen[] = { 0x00, 0x12, 0x34 };
  15328. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLong[] = { 0x00, 0x00, 0x12, 0x34 };
  15329. const dh_pubvalue_test dh_pubval_pass[] = {
  15330. { pubValTwo, sizeof(pubValTwo) },
  15331. { pubValTwoLong, sizeof(pubValTwoLong) },
  15332. { pubValGood, sizeof(pubValGood) },
  15333. { pubValGoodLen, sizeof(pubValGoodLen) },
  15334. { pubValGoodLong, sizeof(pubValGoodLong) },
  15335. };
  15336. for (i = 0; i < sizeof(dh_pubval_fail) / sizeof(*dh_pubval_fail); i++) {
  15337. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_fail[i].data,
  15338. dh_pubval_fail[i].len);
  15339. if (ret != MP_VAL)
  15340. return -8020 - (int)i;
  15341. }
  15342. for (i = 0; i < sizeof(dh_pubval_pass) / sizeof(*dh_pubval_pass); i++) {
  15343. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_pass[i].data,
  15344. dh_pubval_pass[i].len);
  15345. if (ret != 0)
  15346. return -8030 - (int)i;
  15347. }
  15348. return 0;
  15349. }
  15350. #endif
  15351. #if defined(HAVE_FFDHE)
  15352. #if defined(HAVE_FFDHE_4096)
  15353. #define MAX_DH_PRIV_SZ 39
  15354. #define MAX_DH_KEY_SZ 512
  15355. #elif defined(HAVE_FFDHE_3072)
  15356. #define MAX_DH_PRIV_SZ 34
  15357. #define MAX_DH_KEY_SZ 384
  15358. #else
  15359. #define MAX_DH_PRIV_SZ 29
  15360. #define MAX_DH_KEY_SZ 256
  15361. #endif
  15362. #ifndef WC_NO_RNG
  15363. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  15364. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM)))
  15365. #ifdef HAVE_PUBLIC_FFDHE
  15366. static int dh_ffdhe_test(WC_RNG *rng, const DhParams* params)
  15367. #else
  15368. static int dh_ffdhe_test(WC_RNG *rng, int name)
  15369. #endif
  15370. {
  15371. int ret;
  15372. word32 privSz, pubSz, privSz2, pubSz2;
  15373. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15374. byte *priv = (byte*)XMALLOC(MAX_DH_PRIV_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15375. byte *pub = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15376. byte *priv2 = (byte*)XMALLOC(MAX_DH_PRIV_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15377. byte *pub2 = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15378. byte *agree = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15379. byte *agree2 = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15380. DhKey *key = (DhKey*)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15381. DhKey *key2 = (DhKey*)XMALLOC(sizeof(*key2), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15382. #else
  15383. byte priv[MAX_DH_PRIV_SZ];
  15384. byte pub[MAX_DH_KEY_SZ];
  15385. byte priv2[MAX_DH_PRIV_SZ];
  15386. byte pub2[MAX_DH_KEY_SZ];
  15387. byte agree[MAX_DH_KEY_SZ];
  15388. byte agree2[MAX_DH_KEY_SZ];
  15389. DhKey key[1];
  15390. DhKey key2[1];
  15391. #endif
  15392. word32 agreeSz = MAX_DH_KEY_SZ;
  15393. word32 agreeSz2 = MAX_DH_KEY_SZ;
  15394. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15395. if ((priv == NULL) ||
  15396. (pub == NULL) ||
  15397. (priv2 == NULL) ||
  15398. (pub2 == NULL) ||
  15399. (agree == NULL) ||
  15400. (agree2 == NULL) ||
  15401. (key == NULL) ||
  15402. (key2 == NULL))
  15403. ERROR_OUT(-8050, done);
  15404. #endif
  15405. pubSz = MAX_DH_KEY_SZ;
  15406. pubSz2 = MAX_DH_KEY_SZ;
  15407. #ifdef HAVE_PUBLIC_FFDHE
  15408. privSz = MAX_DH_PRIV_SZ;
  15409. privSz2 = MAX_DH_PRIV_SZ;
  15410. #else
  15411. privSz = wc_DhGetNamedKeyMinSize(name);
  15412. privSz2 = privSz;
  15413. #endif
  15414. XMEMSET(key, 0, sizeof(*key));
  15415. XMEMSET(key2, 0, sizeof(*key2));
  15416. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15417. if (ret != 0) {
  15418. ERROR_OUT(-8051, done);
  15419. }
  15420. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  15421. if (ret != 0) {
  15422. ERROR_OUT(-8052, done);
  15423. }
  15424. #ifdef HAVE_PUBLIC_FFDHE
  15425. ret = wc_DhSetKey(key, params->p, params->p_len, params->g, params->g_len);
  15426. #else
  15427. ret = wc_DhSetNamedKey(key, name);
  15428. #endif
  15429. if (ret != 0) {
  15430. ERROR_OUT(-8053, done);
  15431. }
  15432. #ifdef HAVE_PUBLIC_FFDHE
  15433. ret = wc_DhSetKey(key2, params->p, params->p_len, params->g,
  15434. params->g_len);
  15435. #else
  15436. ret = wc_DhSetNamedKey(key2, name);
  15437. #endif
  15438. if (ret != 0) {
  15439. ERROR_OUT(-8054, done);
  15440. }
  15441. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  15442. #if defined(WOLFSSL_ASYNC_CRYPT)
  15443. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15444. #endif
  15445. if (ret != 0) {
  15446. ERROR_OUT(-8055, done);
  15447. }
  15448. ret = wc_DhGenerateKeyPair(key2, rng, priv2, &privSz2, pub2, &pubSz2);
  15449. #if defined(WOLFSSL_ASYNC_CRYPT)
  15450. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  15451. #endif
  15452. if (ret != 0) {
  15453. ERROR_OUT(-8056, done);
  15454. }
  15455. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  15456. #if defined(WOLFSSL_ASYNC_CRYPT)
  15457. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15458. #endif
  15459. if (ret != 0) {
  15460. ERROR_OUT(-8057, done);
  15461. }
  15462. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  15463. #if defined(WOLFSSL_ASYNC_CRYPT)
  15464. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  15465. #endif
  15466. if (ret != 0) {
  15467. ERROR_OUT(-8058, done);
  15468. }
  15469. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  15470. ERROR_OUT(-8059, done);
  15471. }
  15472. #if defined(WOLFSSL_HAVE_SP_DH) || defined(USE_FAST_MATH)
  15473. /* Make p even */
  15474. key->p.dp[0] &= (mp_digit)-2;
  15475. if (ret != 0) {
  15476. ERROR_OUT(-8058, done);
  15477. }
  15478. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  15479. #if defined(WOLFSSL_ASYNC_CRYPT)
  15480. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15481. #endif
  15482. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  15483. ERROR_OUT(-8058, done);
  15484. }
  15485. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  15486. #if defined(WOLFSSL_ASYNC_CRYPT)
  15487. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15488. #endif
  15489. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != ASYNC_OP_E) {
  15490. ERROR_OUT(-8057, done);
  15491. }
  15492. #ifndef HAVE_SELFTEST
  15493. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  15494. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_CMP_E &&
  15495. ret != ASYNC_OP_E) {
  15496. ERROR_OUT(-8057, done);
  15497. }
  15498. #endif
  15499. /* Getting here means success - set ret to 0. */
  15500. ret = 0;
  15501. #endif
  15502. done:
  15503. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) && \
  15504. !defined(WC_NO_RNG)
  15505. if (priv)
  15506. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15507. if (pub)
  15508. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15509. if (priv2)
  15510. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15511. if (pub2)
  15512. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15513. if (agree)
  15514. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15515. if (agree2)
  15516. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15517. if (key) {
  15518. wc_FreeDhKey(key);
  15519. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15520. }
  15521. if (key2) {
  15522. wc_FreeDhKey(key2);
  15523. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15524. }
  15525. #else
  15526. wc_FreeDhKey(key);
  15527. wc_FreeDhKey(key2);
  15528. #endif
  15529. return ret;
  15530. }
  15531. #endif /* !(HAVE_FIPS_VERSION == 2 && WOLFSSL_SP_ARMxx_ASM) */
  15532. #endif /* !WC_NO_RNG */
  15533. #endif /* HAVE_FFDHE */
  15534. WOLFSSL_TEST_SUBROUTINE int dh_test(void)
  15535. {
  15536. int ret;
  15537. word32 bytes;
  15538. word32 idx = 0, privSz, pubSz, privSz2, pubSz2;
  15539. #ifndef WC_NO_RNG
  15540. WC_RNG rng;
  15541. int rngInit = 0;
  15542. #endif
  15543. int keyInit = 0, key2Init = 0;
  15544. #define DH_TEST_TMP_SIZE 1024
  15545. #if !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  15546. #define DH_TEST_BUF_SIZE 256
  15547. #else
  15548. #define DH_TEST_BUF_SIZE 512
  15549. #endif
  15550. #ifndef WC_NO_RNG
  15551. word32 agreeSz = DH_TEST_BUF_SIZE;
  15552. word32 agreeSz2 = DH_TEST_BUF_SIZE;
  15553. #endif
  15554. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15555. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15556. DhKey *key2 = (DhKey *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15557. byte *tmp = (byte *)XMALLOC(DH_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15558. #else
  15559. DhKey key[1];
  15560. DhKey key2[1];
  15561. byte tmp[DH_TEST_TMP_SIZE];
  15562. #endif
  15563. #ifndef WC_NO_RNG
  15564. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15565. byte *priv = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15566. byte *pub = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15567. byte *priv2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15568. byte *pub2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15569. byte *agree = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15570. byte *agree2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15571. if (priv == NULL || pub == NULL || priv2 == NULL || pub2 == NULL ||
  15572. agree == NULL || agree2 == NULL) {
  15573. ERROR_OUT(-8100, done);
  15574. }
  15575. #else
  15576. byte priv[DH_TEST_BUF_SIZE];
  15577. byte pub[DH_TEST_BUF_SIZE];
  15578. byte priv2[DH_TEST_BUF_SIZE];
  15579. byte pub2[DH_TEST_BUF_SIZE];
  15580. byte agree[DH_TEST_BUF_SIZE];
  15581. byte agree2[DH_TEST_BUF_SIZE];
  15582. #endif
  15583. #endif /* !WC_NO_RNG */
  15584. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15585. if (key == NULL || key2 == NULL || tmp == NULL) {
  15586. ERROR_OUT(-8100, done);
  15587. }
  15588. #endif
  15589. #ifdef USE_CERT_BUFFERS_1024
  15590. XMEMCPY(tmp, dh_key_der_1024, (size_t)sizeof_dh_key_der_1024);
  15591. bytes = (size_t)sizeof_dh_key_der_1024;
  15592. #elif defined(USE_CERT_BUFFERS_2048)
  15593. XMEMCPY(tmp, dh_key_der_2048, (size_t)sizeof_dh_key_der_2048);
  15594. bytes = (size_t)sizeof_dh_key_der_2048;
  15595. #elif defined(USE_CERT_BUFFERS_3072)
  15596. XMEMCPY(tmp, dh_key_der_3072, (size_t)sizeof_dh_key_der_3072);
  15597. bytes = (size_t)sizeof_dh_key_der_3072;
  15598. #elif defined(USE_CERT_BUFFERS_4096)
  15599. XMEMCPY(tmp, dh_key_der_4096, (size_t)sizeof_dh_key_der_4096);
  15600. bytes = (size_t)sizeof_dh_key_der_4096;
  15601. #elif defined(NO_ASN)
  15602. /* don't use file, no DER parsing */
  15603. #elif !defined(NO_FILESYSTEM)
  15604. {
  15605. XFILE file = XFOPEN(dhParamsFile, "rb");
  15606. if (! file)
  15607. ERROR_OUT(-8101, done);
  15608. bytes = (word32) XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  15609. XFCLOSE(file);
  15610. }
  15611. #else
  15612. /* No DH key to use. */
  15613. ERROR_OUT(-8102, done);
  15614. #endif /* USE_CERT_BUFFERS */
  15615. (void)idx;
  15616. (void)tmp;
  15617. (void)bytes;
  15618. pubSz = DH_TEST_BUF_SIZE;
  15619. pubSz2 = DH_TEST_BUF_SIZE;
  15620. privSz = DH_TEST_BUF_SIZE;
  15621. privSz2 = DH_TEST_BUF_SIZE;
  15622. #ifndef WC_NO_RNG
  15623. XMEMSET(&rng, 0, sizeof(rng));
  15624. #endif
  15625. /* Use API for coverage. */
  15626. ret = wc_InitDhKey(key);
  15627. if (ret != 0) {
  15628. ERROR_OUT(-8103, done);
  15629. }
  15630. wc_FreeDhKey(key);
  15631. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15632. if (ret != 0) {
  15633. ERROR_OUT(-8104, done);
  15634. }
  15635. keyInit = 1;
  15636. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  15637. if (ret != 0) {
  15638. ERROR_OUT(-8105, done);
  15639. }
  15640. key2Init = 1;
  15641. #ifdef NO_ASN
  15642. #ifndef WOLFSSL_SP_MATH
  15643. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  15644. if (ret != 0) {
  15645. ERROR_OUT(-8106, done);
  15646. }
  15647. ret = wc_DhSetKey(key2, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  15648. if (ret != 0) {
  15649. ERROR_OUT(-8107, done);
  15650. }
  15651. #else
  15652. ret = wc_DhSetKey(key, dh2048_p, sizeof(dh2048_p), dh2048_g,
  15653. sizeof(dh2048_g));
  15654. if (ret != 0) {
  15655. ERROR_OUT(-8106, done);
  15656. }
  15657. ret = wc_DhSetKey(key2, dh2048_p, sizeof(dh2048_p), dh2048_g,
  15658. sizeof(dh2048_g));
  15659. if (ret != 0) {
  15660. ERROR_OUT(-8107, done);
  15661. }
  15662. #endif
  15663. #else
  15664. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  15665. if (ret != 0) {
  15666. ERROR_OUT(-8108, done);
  15667. }
  15668. idx = 0;
  15669. ret = wc_DhKeyDecode(tmp, &idx, key2, bytes);
  15670. if (ret != 0) {
  15671. ERROR_OUT(-8109, done);
  15672. }
  15673. #endif
  15674. #ifndef WC_NO_RNG
  15675. #ifndef HAVE_FIPS
  15676. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  15677. #else
  15678. ret = wc_InitRng(&rng);
  15679. #endif
  15680. if (ret != 0) {
  15681. ERROR_OUT(-8110, done);
  15682. }
  15683. rngInit = 1;
  15684. ret = wc_DhGenerateKeyPair(key, &rng, priv, &privSz, pub, &pubSz);
  15685. #if defined(WOLFSSL_ASYNC_CRYPT)
  15686. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15687. #endif
  15688. if (ret != 0) {
  15689. ERROR_OUT(-8111, done);
  15690. }
  15691. ret = wc_DhGenerateKeyPair(key2, &rng, priv2, &privSz2, pub2, &pubSz2);
  15692. #if defined(WOLFSSL_ASYNC_CRYPT)
  15693. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  15694. #endif
  15695. if (ret != 0) {
  15696. ERROR_OUT(-8112, done);
  15697. }
  15698. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  15699. #if defined(WOLFSSL_ASYNC_CRYPT)
  15700. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15701. #endif
  15702. if (ret != 0) {
  15703. ERROR_OUT(-8113, done);
  15704. }
  15705. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  15706. #if defined(WOLFSSL_ASYNC_CRYPT)
  15707. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  15708. #endif
  15709. if (ret != 0) {
  15710. ERROR_OUT(-8114, done);
  15711. }
  15712. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  15713. ERROR_OUT(-8115, done);
  15714. }
  15715. #endif /* !WC_NO_RNG */
  15716. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  15717. if (wc_DhCheckPrivKey(NULL, NULL, 0) != BAD_FUNC_ARG)
  15718. ERROR_OUT(-8116, done);
  15719. if (wc_DhCheckPrivKey(key, priv, privSz) != 0)
  15720. ERROR_OUT(-8117, done);
  15721. if (wc_DhExportParamsRaw(NULL, NULL, NULL, NULL, NULL, NULL, NULL) != BAD_FUNC_ARG)
  15722. ERROR_OUT(-8118, done);
  15723. {
  15724. word32 pSz, qSz, gSz;
  15725. if (wc_DhExportParamsRaw(key, NULL, &pSz, NULL, &qSz, NULL, &gSz) != LENGTH_ONLY_E)
  15726. ERROR_OUT(-8119, done);
  15727. }
  15728. #endif
  15729. /* Test DH key import / export */
  15730. #if defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM) && \
  15731. (!defined(HAVE_FIPS) || \
  15732. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  15733. wc_FreeDhKey(key);
  15734. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15735. if (ret != 0) {
  15736. ERROR_OUT(-8120, done);
  15737. }
  15738. #ifndef NO_ASN
  15739. {
  15740. /* DH Private - Key Export / Import */
  15741. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15742. byte *tmp2;
  15743. #else
  15744. byte tmp2[DH_TEST_TMP_SIZE];
  15745. #endif
  15746. XFILE file = XFOPEN(dhKeyFile, "rb");
  15747. if (!file)
  15748. ERROR_OUT(-8130, done);
  15749. bytes = (word32)XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  15750. XFCLOSE(file);
  15751. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15752. tmp2 = (byte*)XMALLOC(DH_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15753. if (tmp2 == NULL)
  15754. ERROR_OUT(-8131, done);
  15755. #endif
  15756. idx = 0;
  15757. XMEMSET(tmp2, 0, DH_TEST_TMP_SIZE);
  15758. /* Import DH Private key as DER */
  15759. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  15760. if (ret == 0) {
  15761. /* Export as DER */
  15762. idx = DH_TEST_TMP_SIZE;
  15763. ret = wc_DhPrivKeyToDer(key, tmp2, &idx);
  15764. }
  15765. /* Verify export matches original */
  15766. if (ret <= 0 || bytes != idx || XMEMCMP(tmp, tmp2, bytes) != 0) {
  15767. ERROR_OUT(-8132, done);
  15768. }
  15769. /* DH Public Key - Export / Import */
  15770. file = XFOPEN(dhKeyPubFile, "rb");
  15771. if (!file)
  15772. ERROR_OUT(-8133, done);
  15773. bytes = (word32)XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  15774. XFCLOSE(file);
  15775. /* for HAVE_WOLF_BIGINT prevent leak */
  15776. wc_FreeDhKey(key);
  15777. (void)wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15778. idx = 0;
  15779. XMEMSET(tmp2, 0, DH_TEST_TMP_SIZE);
  15780. /* Import DH Public key as DER */
  15781. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  15782. if (ret == 0) {
  15783. /* Export as DER */
  15784. idx = DH_TEST_TMP_SIZE;
  15785. ret = wc_DhPubKeyToDer(key, tmp2, &idx);
  15786. }
  15787. /* Verify export matches original */
  15788. if (ret <= 0 || bytes != idx || XMEMCMP(tmp, tmp2, bytes) != 0) {
  15789. ERROR_OUT(-8134, done);
  15790. }
  15791. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15792. XFREE(tmp2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15793. #endif
  15794. }
  15795. #else
  15796. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  15797. if (ret != 0) {
  15798. ERROR_OUT(-8121, done);
  15799. }
  15800. #endif /* !NO_ASN */
  15801. privSz = DH_TEST_BUF_SIZE;
  15802. pubSz = DH_TEST_BUF_SIZE;
  15803. ret = wc_DhExportKeyPair(key, priv, &privSz, pub, &pubSz);
  15804. if (ret != 0) {
  15805. ERROR_OUT(-8122, done);
  15806. }
  15807. ret = wc_DhImportKeyPair(key2, priv, privSz, pub, pubSz);
  15808. if (ret != 0) {
  15809. ERROR_OUT(-8125, done);
  15810. }
  15811. #endif /* WOLFSSL_DH_EXTRA && !NO_FILESYSTEM && !FIPS <= 2 */
  15812. #ifndef WC_NO_RNG
  15813. ret = dh_generate_test(&rng);
  15814. if (ret != 0)
  15815. ERROR_OUT(-8123, done);
  15816. ret = dh_fips_generate_test(&rng);
  15817. if (ret != 0)
  15818. ERROR_OUT(-8124, done);
  15819. #endif /* !WC_NO_RNG */
  15820. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  15821. ret = dh_test_check_pubvalue();
  15822. if (ret != 0)
  15823. ERROR_OUT(-8125, done);
  15824. #endif
  15825. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  15826. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM)))
  15827. /* RNG with DH and SP_ASM code not supported in the in-lined FIPS ASM code,
  15828. * this will be available for testing in the 140-3 module */
  15829. #ifndef WC_NO_RNG
  15830. /* Specialized code for key gen when using FFDHE-2048, FFDHE-3072 and FFDHE-4096 */
  15831. #ifdef HAVE_FFDHE_2048
  15832. #ifdef HAVE_PUBLIC_FFDHE
  15833. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe2048_Get());
  15834. #else
  15835. ret = dh_ffdhe_test(&rng, WC_FFDHE_2048);
  15836. #endif
  15837. if (ret != 0)
  15838. ERROR_OUT(-8126, done);
  15839. #endif
  15840. #ifdef HAVE_FFDHE_3072
  15841. #ifdef HAVE_PUBLIC_FFDHE
  15842. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe3072_Get());
  15843. #else
  15844. ret = dh_ffdhe_test(&rng, WC_FFDHE_3072);
  15845. #endif
  15846. if (ret != 0)
  15847. ERROR_OUT(-8127, done);
  15848. #endif
  15849. #ifdef HAVE_FFDHE_4096
  15850. #ifdef HAVE_PUBLIC_FFDHE
  15851. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe4096_Get());
  15852. #else
  15853. ret = dh_ffdhe_test(&rng, WC_FFDHE_4096);
  15854. #endif
  15855. if (ret != 0)
  15856. ERROR_OUT(-8128, done);
  15857. #endif
  15858. #endif /* !WC_NO_RNG */
  15859. #endif /* HAVE_FIPS_VERSION == 2 && !WOLFSSL_SP_ARM64_ASM */
  15860. wc_FreeDhKey(key);
  15861. keyInit = 0;
  15862. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  15863. !defined(WOLFSSL_OLD_PRIME_CHECK) && !defined(WC_NO_RNG)
  15864. /* Test Check Key */
  15865. ret = wc_DhSetCheckKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g),
  15866. NULL, 0, 0, &rng);
  15867. if (ret != 0)
  15868. ERROR_OUT(-8129, done);
  15869. keyInit = 1; /* DhSetCheckKey also initializes the key, free it */
  15870. #endif
  15871. done:
  15872. #ifndef WC_NO_RNG
  15873. if (rngInit)
  15874. wc_FreeRng(&rng);
  15875. #endif
  15876. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15877. if (key) {
  15878. if (keyInit)
  15879. wc_FreeDhKey(key);
  15880. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15881. }
  15882. if (key2) {
  15883. if (key2Init)
  15884. wc_FreeDhKey(key2);
  15885. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15886. }
  15887. if (tmp)
  15888. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15889. if (priv)
  15890. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15891. if (pub)
  15892. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15893. if (priv2)
  15894. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15895. if (pub2)
  15896. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15897. if (agree)
  15898. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15899. if (agree2)
  15900. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15901. #else
  15902. if (keyInit)
  15903. wc_FreeDhKey(key);
  15904. if (key2Init)
  15905. wc_FreeDhKey(key2);
  15906. #endif
  15907. (void)privSz;
  15908. (void)pubSz;
  15909. (void)pubSz2;
  15910. (void)privSz2;
  15911. return ret;
  15912. #undef DH_TEST_BUF_SIZE
  15913. #undef DH_TEST_TMP_SIZE
  15914. }
  15915. #endif /* NO_DH */
  15916. #ifndef NO_DSA
  15917. WOLFSSL_TEST_SUBROUTINE int dsa_test(void)
  15918. {
  15919. int ret = 0, answer;
  15920. word32 bytes;
  15921. word32 idx = 0;
  15922. WC_RNG rng;
  15923. wc_Sha sha;
  15924. byte hash[WC_SHA_DIGEST_SIZE];
  15925. byte signature[40];
  15926. #ifdef WOLFSSL_KEY_GEN
  15927. byte* der = 0;
  15928. #endif
  15929. #define DSA_TEST_TMP_SIZE 1024
  15930. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15931. byte *tmp = (byte *)XMALLOC(DSA_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15932. DsaKey *key = (DsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15933. #ifdef WOLFSSL_KEY_GEN
  15934. DsaKey *derIn = (DsaKey *)XMALLOC(sizeof *derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15935. DsaKey *genKey = (DsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15936. #endif
  15937. if ((tmp == NULL) ||
  15938. (key == NULL)
  15939. #ifdef WOLFSSL_KEY_GEN
  15940. || (derIn == NULL)
  15941. || (genKey == NULL)
  15942. #endif
  15943. ) {
  15944. ret = -8216;
  15945. goto out;
  15946. }
  15947. #else
  15948. byte tmp[1024];
  15949. DsaKey key[1];
  15950. #ifdef WOLFSSL_KEY_GEN
  15951. DsaKey derIn[1];
  15952. DsaKey genKey[1];
  15953. #endif
  15954. #endif
  15955. #ifdef USE_CERT_BUFFERS_1024
  15956. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  15957. bytes = sizeof_dsa_key_der_1024;
  15958. #elif defined(USE_CERT_BUFFERS_2048)
  15959. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  15960. bytes = sizeof_dsa_key_der_2048;
  15961. #else
  15962. {
  15963. XFILE file = XFOPEN(dsaKey, "rb");
  15964. if (!file)
  15965. ERROR_OUT(-8200, out);
  15966. bytes = (word32) XFREAD(tmp, 1, DSA_TEST_TMP_SIZE, file);
  15967. XFCLOSE(file);
  15968. }
  15969. #endif /* USE_CERT_BUFFERS */
  15970. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  15971. if (ret != 0)
  15972. ERROR_OUT(-8201, out);
  15973. wc_ShaUpdate(&sha, tmp, bytes);
  15974. wc_ShaFinal(&sha, hash);
  15975. wc_ShaFree(&sha);
  15976. ret = wc_InitDsaKey(key);
  15977. if (ret != 0)
  15978. ERROR_OUT(-8202, out);
  15979. ret = wc_DsaPrivateKeyDecode(tmp, &idx, key, bytes);
  15980. if (ret != 0)
  15981. ERROR_OUT(-8203, out);
  15982. #ifndef HAVE_FIPS
  15983. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  15984. #else
  15985. ret = wc_InitRng(&rng);
  15986. #endif
  15987. if (ret != 0)
  15988. ERROR_OUT(-8204, out);
  15989. ret = wc_DsaSign(hash, signature, key, &rng);
  15990. if (ret != 0)
  15991. ERROR_OUT(-8205, out);
  15992. ret = wc_DsaVerify(hash, signature, key, &answer);
  15993. if (ret != 0)
  15994. ERROR_OUT(-8206, out);
  15995. if (answer != 1)
  15996. ERROR_OUT(-8207, out);
  15997. wc_FreeDsaKey(key);
  15998. #ifdef WOLFSSL_KEY_GEN
  15999. {
  16000. int derSz = 0;
  16001. ret = wc_InitDsaKey(genKey);
  16002. if (ret != 0)
  16003. ERROR_OUT(-8208, out);
  16004. ret = wc_MakeDsaParameters(&rng, 1024, genKey);
  16005. if (ret != 0) {
  16006. wc_FreeDsaKey(genKey);
  16007. ERROR_OUT(-8209, out);
  16008. }
  16009. ret = wc_MakeDsaKey(&rng, genKey);
  16010. if (ret != 0) {
  16011. wc_FreeDsaKey(genKey);
  16012. ERROR_OUT(-8210, out);
  16013. }
  16014. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16015. if (der == NULL) {
  16016. wc_FreeDsaKey(genKey);
  16017. ERROR_OUT(-8211, out);
  16018. }
  16019. derSz = wc_DsaKeyToDer(genKey, der, FOURK_BUF);
  16020. if (derSz < 0) {
  16021. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16022. ERROR_OUT(-8212, out);
  16023. }
  16024. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  16025. DSA_PRIVATEKEY_TYPE, -5814);
  16026. if (ret != 0) {
  16027. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16028. wc_FreeDsaKey(genKey);
  16029. goto out;
  16030. }
  16031. ret = wc_InitDsaKey(derIn);
  16032. if (ret != 0) {
  16033. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16034. wc_FreeDsaKey(genKey);
  16035. ERROR_OUT(-8213, out);
  16036. }
  16037. idx = 0;
  16038. ret = wc_DsaPrivateKeyDecode(der, &idx, derIn, derSz);
  16039. if (ret != 0) {
  16040. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16041. wc_FreeDsaKey(derIn);
  16042. wc_FreeDsaKey(genKey);
  16043. ERROR_OUT(-8214, out);
  16044. }
  16045. }
  16046. #endif /* WOLFSSL_KEY_GEN */
  16047. out:
  16048. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16049. if (key) {
  16050. #endif
  16051. if (wc_InitDsaKey_h(key, NULL) != 0)
  16052. ret = -8215;
  16053. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16054. }
  16055. #endif
  16056. #ifdef WOLFSSL_KEY_GEN
  16057. if (der)
  16058. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16059. #endif
  16060. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16061. if (tmp)
  16062. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16063. if (key)
  16064. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16065. #ifdef WOLFSSL_KEY_GEN
  16066. if (derIn) {
  16067. wc_FreeDsaKey(derIn);
  16068. XFREE(derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16069. }
  16070. if (genKey) {
  16071. wc_FreeDsaKey(genKey);
  16072. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16073. }
  16074. #endif
  16075. #else /* !WOLFSSL_SMALL_STACK || WOLFSSL_NO_MALLOC */
  16076. #ifdef WOLFSSL_KEY_GEN
  16077. wc_FreeDsaKey(derIn);
  16078. wc_FreeDsaKey(genKey);
  16079. #endif
  16080. #endif
  16081. wc_FreeRng(&rng);
  16082. return ret;
  16083. }
  16084. #endif /* NO_DSA */
  16085. #ifdef WOLFCRYPT_HAVE_SRP
  16086. static int generate_random_salt(byte *buf, word32 size)
  16087. {
  16088. int ret = -8220;
  16089. WC_RNG rng;
  16090. if(NULL == buf || !size)
  16091. return -8221;
  16092. if (buf && size && wc_InitRng_ex(&rng, HEAP_HINT, devId) == 0) {
  16093. ret = wc_RNG_GenerateBlock(&rng, (byte *)buf, size);
  16094. wc_FreeRng(&rng);
  16095. }
  16096. return ret;
  16097. }
  16098. static int srp_test_digest(SrpType dgstType)
  16099. {
  16100. int r;
  16101. byte clientPubKey[192]; /* A */
  16102. byte serverPubKey[192]; /* B */
  16103. word32 clientPubKeySz = 192;
  16104. word32 serverPubKeySz = 192;
  16105. byte username[] = "user";
  16106. word32 usernameSz = 4;
  16107. byte password[] = "password";
  16108. word32 passwordSz = 8;
  16109. WOLFSSL_SMALL_STACK_STATIC const byte N[] = {
  16110. 0xfc, 0x58, 0x7a, 0x8a, 0x70, 0xfb, 0x5a, 0x9a,
  16111. 0x5d, 0x39, 0x48, 0xbf, 0x1c, 0x46, 0xd8, 0x3b,
  16112. 0x7a, 0xe9, 0x1f, 0x85, 0x36, 0x18, 0xc4, 0x35,
  16113. 0x3f, 0xf8, 0x8a, 0x8f, 0x8c, 0x10, 0x2e, 0x01,
  16114. 0x58, 0x1d, 0x41, 0xcb, 0xc4, 0x47, 0xa8, 0xaf,
  16115. 0x9a, 0x6f, 0x58, 0x14, 0xa4, 0x68, 0xf0, 0x9c,
  16116. 0xa6, 0xe7, 0xbf, 0x0d, 0xe9, 0x62, 0x0b, 0xd7,
  16117. 0x26, 0x46, 0x5b, 0x27, 0xcb, 0x4c, 0xf9, 0x7e,
  16118. 0x1e, 0x8b, 0xe6, 0xdd, 0x29, 0xb7, 0xb7, 0x15,
  16119. 0x2e, 0xcf, 0x23, 0xa6, 0x4b, 0x97, 0x9f, 0x89,
  16120. 0xd4, 0x86, 0xc4, 0x90, 0x63, 0x92, 0xf4, 0x30,
  16121. 0x26, 0x69, 0x48, 0x9d, 0x7a, 0x4f, 0xad, 0xb5,
  16122. 0x6a, 0x51, 0xad, 0xeb, 0xf9, 0x90, 0x31, 0x77,
  16123. 0x53, 0x30, 0x2a, 0x85, 0xf7, 0x11, 0x21, 0x0c,
  16124. 0xb8, 0x4b, 0x56, 0x03, 0x5e, 0xbb, 0x25, 0x33,
  16125. 0x7c, 0xd9, 0x5a, 0xd1, 0x5c, 0xb2, 0xd4, 0x53,
  16126. 0xc5, 0x16, 0x68, 0xf0, 0xdf, 0x48, 0x55, 0x3e,
  16127. 0xd4, 0x59, 0x87, 0x64, 0x59, 0xaa, 0x39, 0x01,
  16128. 0x45, 0x89, 0x9c, 0x72, 0xff, 0xdd, 0x8f, 0x6d,
  16129. 0xa0, 0x42, 0xbc, 0x6f, 0x6e, 0x62, 0x18, 0x2d,
  16130. 0x50, 0xe8, 0x18, 0x97, 0x87, 0xfc, 0xef, 0x1f,
  16131. 0xf5, 0x53, 0x68, 0xe8, 0x49, 0xd1, 0xa2, 0xe8,
  16132. 0xb9, 0x26, 0x03, 0xba, 0xb5, 0x58, 0x6f, 0x6c,
  16133. 0x8b, 0x08, 0xa1, 0x7b, 0x6f, 0x42, 0xc9, 0x53
  16134. };
  16135. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  16136. 0x02
  16137. };
  16138. byte salt[10];
  16139. byte verifier[192];
  16140. word32 v_size = sizeof(verifier);
  16141. word32 clientProofSz = SRP_MAX_DIGEST_SIZE;
  16142. word32 serverProofSz = SRP_MAX_DIGEST_SIZE;
  16143. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16144. Srp *cli = (Srp *)XMALLOC(sizeof *cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16145. Srp *srv = (Srp *)XMALLOC(sizeof *srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16146. byte *clientProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT,
  16147. DYNAMIC_TYPE_TMP_BUFFER); /* M1 */
  16148. byte *serverProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT,
  16149. DYNAMIC_TYPE_TMP_BUFFER); /* M2 */
  16150. if ((cli == NULL) ||
  16151. (srv == NULL) ||
  16152. (clientProof == NULL) ||
  16153. (serverProof == NULL)) {
  16154. r = -8222;
  16155. goto out;
  16156. }
  16157. #else
  16158. Srp cli[1], srv[1];
  16159. byte clientProof[SRP_MAX_DIGEST_SIZE]; /* M1 */
  16160. byte serverProof[SRP_MAX_DIGEST_SIZE]; /* M2 */
  16161. #endif
  16162. /* set as 0's so if second init on srv not called SrpTerm is not on
  16163. * garbage values */
  16164. XMEMSET(srv, 0, sizeof *srv);
  16165. XMEMSET(cli, 0, sizeof *cli);
  16166. /* generating random salt */
  16167. r = generate_random_salt(salt, sizeof(salt));
  16168. /* client knows username and password. */
  16169. /* server knows N, g, salt and verifier. */
  16170. if (!r) r = wc_SrpInit_ex(cli, dgstType, SRP_CLIENT_SIDE, HEAP_HINT, devId);
  16171. if (!r) r = wc_SrpSetUsername(cli, username, usernameSz);
  16172. /* loading N, g and salt in advance to generate the verifier. */
  16173. if (!r) r = wc_SrpSetParams(cli, N, sizeof(N),
  16174. g, sizeof(g),
  16175. salt, sizeof(salt));
  16176. if (!r) r = wc_SrpSetPassword(cli, password, passwordSz);
  16177. if (!r) r = wc_SrpGetVerifier(cli, verifier, &v_size);
  16178. /* client sends username to server */
  16179. if (!r) r = wc_SrpInit_ex(srv, dgstType, SRP_SERVER_SIDE, HEAP_HINT, devId);
  16180. if (!r) r = wc_SrpSetUsername(srv, username, usernameSz);
  16181. if (!r) r = wc_SrpSetParams(srv, N, sizeof(N),
  16182. g, sizeof(g),
  16183. salt, sizeof(salt));
  16184. if (!r) r = wc_SrpSetVerifier(srv, verifier, v_size);
  16185. if (!r) r = wc_SrpGetPublic(srv, serverPubKey, &serverPubKeySz);
  16186. /* server sends N, g, salt and B to client */
  16187. if (!r) r = wc_SrpGetPublic(cli, clientPubKey, &clientPubKeySz);
  16188. if (!r) r = wc_SrpComputeKey(cli, clientPubKey, clientPubKeySz,
  16189. serverPubKey, serverPubKeySz);
  16190. if (!r) r = wc_SrpGetProof(cli, clientProof, &clientProofSz);
  16191. /* client sends A and M1 to server */
  16192. if (!r) r = wc_SrpComputeKey(srv, clientPubKey, clientPubKeySz,
  16193. serverPubKey, serverPubKeySz);
  16194. if (!r) r = wc_SrpVerifyPeersProof(srv, clientProof, clientProofSz);
  16195. if (!r) r = wc_SrpGetProof(srv, serverProof, &serverProofSz);
  16196. /* server sends M2 to client */
  16197. if (!r) r = wc_SrpVerifyPeersProof(cli, serverProof, serverProofSz);
  16198. wc_SrpTerm(cli);
  16199. wc_SrpTerm(srv);
  16200. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16201. out:
  16202. if (cli)
  16203. XFREE(cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16204. if (srv)
  16205. XFREE(srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16206. if (clientProof)
  16207. XFREE(clientProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16208. if (serverProof)
  16209. XFREE(serverProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16210. #endif
  16211. return r;
  16212. }
  16213. WOLFSSL_TEST_SUBROUTINE int srp_test(void)
  16214. {
  16215. int ret;
  16216. #ifndef NO_SHA
  16217. ret = srp_test_digest(SRP_TYPE_SHA);
  16218. if (ret != 0)
  16219. return ret;
  16220. #endif
  16221. #ifndef NO_SHA256
  16222. ret = srp_test_digest(SRP_TYPE_SHA256);
  16223. if (ret != 0)
  16224. return ret;
  16225. #endif
  16226. #ifdef WOLFSSL_SHA384
  16227. ret = srp_test_digest(SRP_TYPE_SHA384);
  16228. if (ret != 0)
  16229. return ret;
  16230. #endif
  16231. #ifdef WOLFSSL_SHA512
  16232. ret = srp_test_digest(SRP_TYPE_SHA512);
  16233. if (ret != 0)
  16234. return ret;
  16235. #endif
  16236. return ret;
  16237. }
  16238. #endif /* WOLFCRYPT_HAVE_SRP */
  16239. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  16240. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  16241. static int openssl_aes_test(void)
  16242. {
  16243. #ifdef HAVE_AES_CBC
  16244. #ifdef WOLFSSL_AES_128
  16245. {
  16246. /* EVP_CipherUpdate test */
  16247. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  16248. {
  16249. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16250. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  16251. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  16252. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  16253. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  16254. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  16255. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  16256. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  16257. };
  16258. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16259. "0123456789abcdef "; /* align */
  16260. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  16261. "1234567890abcdef "; /* align */
  16262. byte cipher[AES_BLOCK_SIZE * 4];
  16263. byte plain [AES_BLOCK_SIZE * 4];
  16264. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16265. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  16266. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  16267. #else
  16268. EVP_CIPHER_CTX en[1];
  16269. EVP_CIPHER_CTX de[1];
  16270. #endif
  16271. int outlen ;
  16272. int total = 0;
  16273. int i;
  16274. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16275. if ((en == NULL) || (de == NULL))
  16276. return MEMORY_E;
  16277. #endif
  16278. EVP_CIPHER_CTX_init(en);
  16279. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16280. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16281. return -8400;
  16282. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  16283. (byte*)cbcPlain, 9) == 0)
  16284. return -8401;
  16285. if (outlen != 0)
  16286. return -8402;
  16287. total += outlen;
  16288. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  16289. (byte*)&cbcPlain[9] , 9) == 0)
  16290. return -8403;
  16291. if (outlen != 16)
  16292. return -8404;
  16293. total += outlen;
  16294. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  16295. return -8405;
  16296. if (outlen != 16)
  16297. return -8406;
  16298. total += outlen;
  16299. if (total != 32)
  16300. return 3408;
  16301. total = 0;
  16302. EVP_CIPHER_CTX_init(de);
  16303. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16304. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16305. return -8407;
  16306. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  16307. return -8408;
  16308. if (outlen != 0)
  16309. return -8409;
  16310. total += outlen;
  16311. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  16312. (byte*)&cipher[6], 12) == 0)
  16313. return -8410;
  16314. if (outlen != 0)
  16315. total += outlen;
  16316. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  16317. (byte*)&cipher[6+12], 14) == 0)
  16318. return -8411;
  16319. if (outlen != 16)
  16320. return -8412;
  16321. total += outlen;
  16322. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  16323. return -8413;
  16324. if (outlen != 2)
  16325. return -8414;
  16326. total += outlen;
  16327. if (total != 18)
  16328. return 3427;
  16329. if (XMEMCMP(plain, cbcPlain, 18))
  16330. return -8415;
  16331. /* test with encrypting/decrypting more than 16 bytes at once */
  16332. total = 0;
  16333. EVP_CIPHER_CTX_init(en);
  16334. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16335. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16336. return -8416;
  16337. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  16338. (byte*)cbcPlain, 17) == 0)
  16339. return -8417;
  16340. if (outlen != 16)
  16341. return -8418;
  16342. total += outlen;
  16343. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  16344. (byte*)&cbcPlain[17] , 1) == 0)
  16345. return -8419;
  16346. if (outlen != 0)
  16347. return -8420;
  16348. total += outlen;
  16349. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  16350. return -8421;
  16351. if (outlen != 16)
  16352. return -8422;
  16353. total += outlen;
  16354. if (total != 32)
  16355. return -8423;
  16356. total = 0;
  16357. EVP_CIPHER_CTX_init(de);
  16358. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16359. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16360. return -8424;
  16361. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 17) == 0)
  16362. return -8425;
  16363. if (outlen != 16)
  16364. return -8426;
  16365. total += outlen;
  16366. /* final call on non block size should fail */
  16367. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  16368. return -8427;
  16369. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  16370. (byte*)&cipher[17], 1) == 0)
  16371. return -8428;
  16372. if (outlen != 0)
  16373. total += outlen;
  16374. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  16375. (byte*)&cipher[17+1], 14) == 0)
  16376. return -8429;
  16377. if (outlen != 0)
  16378. return -8430;
  16379. total += outlen;
  16380. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  16381. return -8431;
  16382. if (outlen != 2)
  16383. return -8432;
  16384. total += outlen;
  16385. if (total != 18)
  16386. return -8433;
  16387. if (XMEMCMP(plain, cbcPlain, 18))
  16388. return -8434;
  16389. /* test byte by byte decrypt */
  16390. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  16391. plain[i] = i;
  16392. }
  16393. total = 0;
  16394. EVP_CIPHER_CTX_init(en);
  16395. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16396. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16397. return -8435;
  16398. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  16399. (byte*)plain, AES_BLOCK_SIZE * 3) == 0)
  16400. return -8436;
  16401. if (outlen != AES_BLOCK_SIZE * 3)
  16402. return -8437;
  16403. total += outlen;
  16404. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  16405. return -8438;
  16406. if (outlen != AES_BLOCK_SIZE)
  16407. return -8439;
  16408. total += outlen;
  16409. if (total != sizeof(plain))
  16410. return -8440;
  16411. total = 0;
  16412. EVP_CIPHER_CTX_init(de);
  16413. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16414. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16415. return -8441;
  16416. for (i = 0; i < AES_BLOCK_SIZE * 4; i++) {
  16417. if (EVP_CipherUpdate(de, (byte*)plain + total, &outlen,
  16418. (byte*)cipher + i, 1) == 0)
  16419. return -8442;
  16420. if (outlen > 0) {
  16421. int j;
  16422. total += outlen;
  16423. for (j = 0; j < total; j++) {
  16424. if (plain[j] != j) {
  16425. return -8443;
  16426. }
  16427. }
  16428. }
  16429. }
  16430. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  16431. return -8444;
  16432. total += outlen;
  16433. if (total != AES_BLOCK_SIZE * 3) {
  16434. return -8445;
  16435. }
  16436. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  16437. if (plain[i] != i) {
  16438. return -8446;
  16439. }
  16440. }
  16441. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16442. wolfSSL_EVP_CIPHER_CTX_free(en);
  16443. wolfSSL_EVP_CIPHER_CTX_free(de);
  16444. #endif
  16445. }
  16446. /* set buffers to be exact size to catch potential over read/write */
  16447. {
  16448. /* EVP_CipherUpdate test */
  16449. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  16450. {
  16451. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16452. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  16453. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  16454. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  16455. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  16456. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  16457. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  16458. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  16459. };
  16460. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16461. "0123456789abcdef "; /* align */
  16462. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  16463. "1234567890abcdef "; /* align */
  16464. #define EVP_TEST_BUF_SZ 18
  16465. #define EVP_TEST_BUF_PAD 32
  16466. byte cipher[EVP_TEST_BUF_SZ];
  16467. byte plain [EVP_TEST_BUF_SZ];
  16468. byte padded[EVP_TEST_BUF_PAD];
  16469. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16470. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  16471. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  16472. #else
  16473. EVP_CIPHER_CTX en[1];
  16474. EVP_CIPHER_CTX de[1];
  16475. #endif
  16476. int outlen ;
  16477. int total = 0;
  16478. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16479. if ((en == NULL) || (de == NULL))
  16480. return MEMORY_E;
  16481. #endif
  16482. EVP_CIPHER_CTX_init(en);
  16483. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16484. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16485. return -8447;
  16486. if (EVP_CIPHER_CTX_set_padding(en, 0) != 1)
  16487. return -8448;
  16488. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  16489. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  16490. return -8449;
  16491. if (outlen != 16)
  16492. return -8450;
  16493. total += outlen;
  16494. /* should fail here */
  16495. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) != 0)
  16496. return -8451;
  16497. /* turn padding back on and do successful encrypt */
  16498. total = 0;
  16499. EVP_CIPHER_CTX_init(en);
  16500. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16501. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16502. return -8452;
  16503. if (EVP_CIPHER_CTX_set_padding(en, 1) != 1)
  16504. return -8453;
  16505. if (EVP_CipherUpdate(en, (byte*)padded, &outlen,
  16506. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  16507. return -8454;
  16508. if (outlen != 16)
  16509. return -8455;
  16510. total += outlen;
  16511. if (EVP_CipherFinal(en, (byte*)&padded[total], &outlen) == 0)
  16512. return -8456;
  16513. total += outlen;
  16514. if (total != 32)
  16515. return -8457;
  16516. XMEMCPY(cipher, padded, EVP_TEST_BUF_SZ);
  16517. /* test out of bounds read on buffers w/o padding during decryption */
  16518. total = 0;
  16519. EVP_CIPHER_CTX_init(de);
  16520. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16521. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16522. return -8458;
  16523. if (EVP_CIPHER_CTX_set_padding(de, 0) != 1)
  16524. return -8459;
  16525. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher,
  16526. EVP_TEST_BUF_SZ) == 0)
  16527. return -8460;
  16528. if (outlen != 16)
  16529. return -8461;
  16530. total += outlen;
  16531. /* should fail since not using padding */
  16532. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  16533. return -8462;
  16534. total = 0;
  16535. EVP_CIPHER_CTX_init(de);
  16536. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16537. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16538. return -8463;
  16539. if (EVP_CIPHER_CTX_set_padding(de, 1) != 1)
  16540. return -8464;
  16541. if (EVP_CipherUpdate(de, (byte*)padded, &outlen, (byte*)padded,
  16542. EVP_TEST_BUF_PAD) == 0)
  16543. return -8465;
  16544. if (outlen != 16)
  16545. return -8466;
  16546. total += outlen;
  16547. if (EVP_CipherFinal(de, (byte*)&padded[total], &outlen) == 0)
  16548. return -8467;
  16549. if (XMEMCMP(padded, cbcPlain, EVP_TEST_BUF_SZ))
  16550. return -8468;
  16551. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16552. wolfSSL_EVP_CIPHER_CTX_free(en);
  16553. wolfSSL_EVP_CIPHER_CTX_free(de);
  16554. #endif
  16555. }
  16556. { /* evp_cipher test: EVP_aes_128_cbc */
  16557. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16558. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  16559. #else
  16560. EVP_CIPHER_CTX ctx[1];
  16561. #endif
  16562. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  16563. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16564. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16565. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16566. };
  16567. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  16568. {
  16569. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  16570. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  16571. };
  16572. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16573. "0123456789abcdef "; /* align */
  16574. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  16575. "1234567890abcdef "; /* align */
  16576. byte cipher[AES_BLOCK_SIZE * 4];
  16577. byte plain [AES_BLOCK_SIZE * 4];
  16578. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16579. if (ctx == NULL)
  16580. return MEMORY_E;
  16581. #endif
  16582. EVP_CIPHER_CTX_init(ctx);
  16583. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1) == 0)
  16584. return -8469;
  16585. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  16586. return -8470;
  16587. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  16588. return -8471;
  16589. EVP_CIPHER_CTX_init(ctx);
  16590. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0) == 0)
  16591. return -8472;
  16592. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  16593. return -8473;
  16594. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  16595. return -8474;
  16596. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16597. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  16598. #endif
  16599. } /* end evp_cipher test: EVP_aes_128_cbc*/
  16600. #endif /* WOLFSSL_AES_128 */
  16601. #endif /* HAVE_AES_CBC */
  16602. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  16603. { /* evp_cipher test: EVP_aes_256_ecb*/
  16604. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16605. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  16606. #else
  16607. EVP_CIPHER_CTX ctx[1];
  16608. #endif
  16609. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  16610. {
  16611. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16612. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16613. };
  16614. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  16615. {
  16616. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  16617. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  16618. };
  16619. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16620. {
  16621. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  16622. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  16623. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  16624. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  16625. };
  16626. byte cipher[AES_BLOCK_SIZE * 4];
  16627. byte plain [AES_BLOCK_SIZE * 4];
  16628. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16629. if (ctx == NULL)
  16630. return MEMORY_E;
  16631. #endif
  16632. EVP_CIPHER_CTX_init(ctx);
  16633. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1) == 0)
  16634. return -8475;
  16635. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  16636. return -8476;
  16637. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  16638. return -8477;
  16639. EVP_CIPHER_CTX_init(ctx);
  16640. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0) == 0)
  16641. return -8478;
  16642. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  16643. return -8479;
  16644. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  16645. return -8480;
  16646. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16647. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  16648. #endif
  16649. } /* end evp_cipher test */
  16650. #endif /* HAVE_AES_ECB && WOLFSSL_AES_256 */
  16651. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  16652. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  16653. {
  16654. /* Test: AES_encrypt/decrypt/set Key */
  16655. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16656. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  16657. #ifdef HAVE_AES_DECRYPT
  16658. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  16659. #endif
  16660. #else
  16661. AES_KEY enc[1];
  16662. #ifdef HAVE_AES_DECRYPT
  16663. AES_KEY dec[1];
  16664. #endif
  16665. #endif
  16666. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  16667. {
  16668. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16669. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16670. };
  16671. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  16672. {
  16673. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  16674. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  16675. };
  16676. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16677. {
  16678. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  16679. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  16680. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  16681. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  16682. };
  16683. byte plain[sizeof(msg)];
  16684. byte cipher[sizeof(msg)];
  16685. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16686. if (enc == NULL)
  16687. return MEMORY_E;
  16688. #ifdef HAVE_AES_DECRYPT
  16689. if (dec == NULL)
  16690. return MEMORY_E;
  16691. #endif
  16692. #endif
  16693. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  16694. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  16695. AES_encrypt(msg, cipher, enc);
  16696. #ifdef HAVE_AES_DECRYPT
  16697. AES_decrypt(cipher, plain, dec);
  16698. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  16699. return -8481;
  16700. #endif /* HAVE_AES_DECRYPT */
  16701. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  16702. return -8482;
  16703. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16704. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  16705. #ifdef HAVE_AES_DECRYPT
  16706. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  16707. #endif
  16708. #endif
  16709. }
  16710. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  16711. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  16712. #ifdef WOLFSSL_AES_COUNTER
  16713. {
  16714. byte plainBuff [64];
  16715. byte cipherBuff[64];
  16716. #ifdef WOLFSSL_AES_128
  16717. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  16718. {
  16719. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  16720. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  16721. };
  16722. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  16723. {
  16724. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  16725. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  16726. };
  16727. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  16728. {
  16729. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16730. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  16731. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  16732. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  16733. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  16734. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  16735. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  16736. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  16737. };
  16738. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  16739. {
  16740. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  16741. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  16742. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  16743. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  16744. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  16745. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  16746. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  16747. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  16748. };
  16749. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  16750. {
  16751. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  16752. 0xc2
  16753. };
  16754. #endif
  16755. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  16756. * NIST Special Publication 800-38A */
  16757. #ifdef WOLFSSL_AES_192
  16758. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  16759. {
  16760. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  16761. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  16762. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  16763. };
  16764. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  16765. {
  16766. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  16767. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  16768. };
  16769. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  16770. {
  16771. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16772. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16773. };
  16774. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  16775. {
  16776. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  16777. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  16778. };
  16779. #endif /* WOLFSSL_AES_192 */
  16780. #ifdef WOLFSSL_AES_256
  16781. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  16782. * NIST Special Publication 800-38A */
  16783. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  16784. {
  16785. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  16786. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  16787. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  16788. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  16789. };
  16790. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  16791. {
  16792. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  16793. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  16794. };
  16795. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  16796. {
  16797. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16798. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16799. };
  16800. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  16801. {
  16802. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  16803. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  16804. };
  16805. #endif /* WOLFSSL_AES_256 */
  16806. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16807. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  16808. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  16809. #else
  16810. EVP_CIPHER_CTX en[1];
  16811. EVP_CIPHER_CTX de[1];
  16812. #endif
  16813. #ifdef WOLFSSL_AES_128
  16814. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16815. EVP_CIPHER_CTX *p_en;
  16816. EVP_CIPHER_CTX *p_de;
  16817. #endif
  16818. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16819. if ((en == NULL) || (de == NULL))
  16820. return MEMORY_E;
  16821. #endif
  16822. EVP_CIPHER_CTX_init(en);
  16823. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  16824. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16825. return -8483;
  16826. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  16827. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16828. return -8484;
  16829. EVP_CIPHER_CTX_init(de);
  16830. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  16831. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16832. return -8485;
  16833. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  16834. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16835. return -8486;
  16836. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  16837. return -8487;
  16838. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  16839. return -8488;
  16840. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16841. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  16842. if (p_en == NULL)
  16843. return -8489;
  16844. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  16845. if (p_de == NULL)
  16846. return -8490;
  16847. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  16848. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16849. return -8491;
  16850. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  16851. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16852. return -8492;
  16853. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  16854. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16855. return -8493;
  16856. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  16857. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16858. return -8494;
  16859. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  16860. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  16861. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  16862. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  16863. return -8495;
  16864. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  16865. return -8496;
  16866. EVP_CIPHER_CTX_init(en);
  16867. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  16868. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16869. return -8497;
  16870. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  16871. return -8498;
  16872. EVP_CIPHER_CTX_init(de);
  16873. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  16874. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16875. return -8499;
  16876. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  16877. return -8500;
  16878. if (XMEMCMP(plainBuff, ctrPlain, 9))
  16879. return -8501;
  16880. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  16881. return -8502;
  16882. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  16883. return -8503;
  16884. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  16885. return -8504;
  16886. if (XMEMCMP(plainBuff, ctrPlain, 9))
  16887. return -8505;
  16888. if (XMEMCMP(cipherBuff, oddCipher, 9))
  16889. return -8506;
  16890. #endif /* WOLFSSL_AES_128 */
  16891. #ifdef WOLFSSL_AES_192
  16892. EVP_CIPHER_CTX_init(en);
  16893. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  16894. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  16895. return -8507;
  16896. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  16897. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16898. return -8508;
  16899. EVP_CIPHER_CTX_init(de);
  16900. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  16901. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  16902. return -8509;
  16903. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  16904. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  16905. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16906. return -8510;
  16907. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  16908. return -8511;
  16909. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  16910. return -8512;
  16911. #endif /* WOLFSSL_AES_192 */
  16912. #ifdef WOLFSSL_AES_256
  16913. EVP_CIPHER_CTX_init(en);
  16914. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  16915. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  16916. return -8513;
  16917. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  16918. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16919. return -8514;
  16920. EVP_CIPHER_CTX_init(de);
  16921. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  16922. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  16923. return -8515;
  16924. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  16925. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  16926. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16927. return -8516;
  16928. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  16929. return -8517;
  16930. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  16931. return -8518;
  16932. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16933. wolfSSL_EVP_CIPHER_CTX_free(en);
  16934. wolfSSL_EVP_CIPHER_CTX_free(de);
  16935. #endif
  16936. #endif /* WOLFSSL_AES_256 */
  16937. }
  16938. #endif /* HAVE_AES_COUNTER */
  16939. #if defined(WOLFSSL_AES_CFB) && defined(WOLFSSL_AES_128)
  16940. {
  16941. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16942. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  16943. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  16944. #else
  16945. AES_KEY enc[1];
  16946. AES_KEY dec[1];
  16947. #endif
  16948. WOLFSSL_SMALL_STACK_STATIC const byte setIv[] = {
  16949. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  16950. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  16951. };
  16952. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16953. {
  16954. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  16955. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  16956. };
  16957. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  16958. {
  16959. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  16960. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  16961. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  16962. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b
  16963. };
  16964. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  16965. {
  16966. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16967. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  16968. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  16969. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51
  16970. };
  16971. byte cipher[AES_BLOCK_SIZE * 2];
  16972. byte iv[AES_BLOCK_SIZE]; /* iv buffer is updeated by API */
  16973. int num = 0;
  16974. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16975. if ((enc == NULL) || (dec == NULL))
  16976. return MEMORY_E;
  16977. #endif
  16978. XMEMCPY(iv, setIv, sizeof(setIv));
  16979. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, enc);
  16980. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, dec);
  16981. wolfSSL_AES_cfb128_encrypt(msg, cipher, AES_BLOCK_SIZE - 1, enc, iv,
  16982. &num, AES_ENCRYPT);
  16983. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE - 1))
  16984. return -8519;
  16985. if (num != 15) /* should have used 15 of the 16 bytes */
  16986. return -8520;
  16987. wolfSSL_AES_cfb128_encrypt(msg + AES_BLOCK_SIZE - 1,
  16988. cipher + AES_BLOCK_SIZE - 1, AES_BLOCK_SIZE + 1, enc, iv,
  16989. &num, AES_ENCRYPT);
  16990. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  16991. return -8521;
  16992. if (num != 0)
  16993. return -8522;
  16994. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16995. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  16996. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  16997. #endif
  16998. }
  16999. #endif /* WOLFSSL_AES_CFB && WOLFSSL_AES_128 */
  17000. return 0;
  17001. }
  17002. #endif /* !defined(NO_AES) && !defined(WOLFCRYPT_ONLY) */
  17003. WOLFSSL_TEST_SUBROUTINE int openssl_test(void)
  17004. {
  17005. int ret;
  17006. EVP_MD_CTX md_ctx;
  17007. testVector a, b, c, d, e, f;
  17008. byte hash[WC_SHA256_DIGEST_SIZE*2]; /* max size */
  17009. a.inLen = 0;
  17010. b.inLen = c.inLen = d.inLen = e.inLen = f.inLen = a.inLen;
  17011. (void)a;
  17012. (void)b;
  17013. (void)c;
  17014. (void)d;
  17015. (void)e;
  17016. (void)f;
  17017. /* test malloc / free , 10 is an arbitrary amount of memory chosen */
  17018. {
  17019. byte* p;
  17020. p = (byte*)CRYPTO_malloc(10, "", 0);
  17021. if (p == NULL) {
  17022. return -8600;
  17023. }
  17024. XMEMSET(p, 0, 10);
  17025. CRYPTO_free(p, "", 0);
  17026. }
  17027. #ifndef NO_MD5
  17028. a.input = "1234567890123456789012345678901234567890123456789012345678"
  17029. "9012345678901234567890";
  17030. a.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  17031. "\x7a";
  17032. a.inLen = XSTRLEN(a.input);
  17033. a.outLen = WC_MD5_DIGEST_SIZE;
  17034. EVP_MD_CTX_init(&md_ctx);
  17035. ret = EVP_DigestInit(&md_ctx, EVP_md5());
  17036. if (ret == WOLFSSL_SUCCESS) {
  17037. ret = EVP_DigestUpdate(&md_ctx, a.input, (unsigned long)a.inLen);
  17038. }
  17039. if (ret == WOLFSSL_SUCCESS) {
  17040. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17041. }
  17042. EVP_MD_CTX_cleanup(&md_ctx);
  17043. if (ret != WOLFSSL_SUCCESS)
  17044. return -18601;
  17045. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0)
  17046. return -8601;
  17047. #endif /* NO_MD5 */
  17048. #ifndef NO_SHA
  17049. b.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  17050. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  17051. "aaaaaaaaaa";
  17052. b.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  17053. "\x53\x99\x5E\x26\xA0";
  17054. b.inLen = XSTRLEN(b.input);
  17055. b.outLen = WC_SHA_DIGEST_SIZE;
  17056. EVP_MD_CTX_init(&md_ctx);
  17057. ret = EVP_DigestInit(&md_ctx, EVP_sha1());
  17058. if (ret == WOLFSSL_SUCCESS) {
  17059. ret = EVP_DigestUpdate(&md_ctx, b.input, (unsigned long)b.inLen);
  17060. if (ret == WOLFSSL_SUCCESS)
  17061. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17062. }
  17063. EVP_MD_CTX_cleanup(&md_ctx);
  17064. if (ret != WOLFSSL_SUCCESS)
  17065. return -18602;
  17066. if (XMEMCMP(hash, b.output, WC_SHA_DIGEST_SIZE) != 0)
  17067. return -8602;
  17068. #endif /* NO_SHA */
  17069. #ifdef WOLFSSL_SHA224
  17070. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17071. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17072. e.output = "\xc9\x7c\xa9\xa5\x59\x85\x0c\xe9\x7a\x04\xa9\x6d\xef\x6d\x99"
  17073. "\xa9\xe0\xe0\xe2\xab\x14\xe6\xb8\xdf\x26\x5f\xc0\xb3";
  17074. e.inLen = XSTRLEN(e.input);
  17075. e.outLen = WC_SHA224_DIGEST_SIZE;
  17076. EVP_MD_CTX_init(&md_ctx);
  17077. ret = EVP_DigestInit(&md_ctx, EVP_sha224());
  17078. if (ret == WOLFSSL_SUCCESS) {
  17079. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  17080. if (ret == WOLFSSL_SUCCESS)
  17081. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17082. }
  17083. EVP_MD_CTX_cleanup(&md_ctx);
  17084. if (ret != WOLFSSL_SUCCESS ||
  17085. XMEMCMP(hash, e.output, WC_SHA224_DIGEST_SIZE) != 0) {
  17086. return -8603;
  17087. }
  17088. #endif /* WOLFSSL_SHA224 */
  17089. #ifndef NO_SHA256
  17090. d.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  17091. d.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  17092. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  17093. "\x06\xC1";
  17094. d.inLen = XSTRLEN(d.input);
  17095. d.outLen = WC_SHA256_DIGEST_SIZE;
  17096. EVP_MD_CTX_init(&md_ctx);
  17097. ret = EVP_DigestInit(&md_ctx, EVP_sha256());
  17098. if (ret == WOLFSSL_SUCCESS) {
  17099. ret = EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen);
  17100. if (ret == WOLFSSL_SUCCESS)
  17101. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17102. }
  17103. EVP_MD_CTX_cleanup(&md_ctx);
  17104. if (ret != WOLFSSL_SUCCESS ||
  17105. XMEMCMP(hash, d.output, WC_SHA256_DIGEST_SIZE) != 0) {
  17106. return -8604;
  17107. }
  17108. #endif /* !NO_SHA256 */
  17109. #ifdef WOLFSSL_SHA384
  17110. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17111. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17112. e.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  17113. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  17114. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  17115. "\x74\x60\x39";
  17116. e.inLen = XSTRLEN(e.input);
  17117. e.outLen = WC_SHA384_DIGEST_SIZE;
  17118. EVP_MD_CTX_init(&md_ctx);
  17119. ret = EVP_DigestInit(&md_ctx, EVP_sha384());
  17120. if (ret == WOLFSSL_SUCCESS) {
  17121. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  17122. if (ret == WOLFSSL_SUCCESS)
  17123. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17124. }
  17125. EVP_MD_CTX_cleanup(&md_ctx);
  17126. if (ret != WOLFSSL_SUCCESS ||
  17127. XMEMCMP(hash, e.output, WC_SHA384_DIGEST_SIZE) != 0) {
  17128. return -8605;
  17129. }
  17130. #endif /* WOLFSSL_SHA384 */
  17131. #ifdef WOLFSSL_SHA512
  17132. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17133. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17134. f.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  17135. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  17136. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  17137. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  17138. "\x87\x4b\xe9\x09";
  17139. f.inLen = XSTRLEN(f.input);
  17140. f.outLen = WC_SHA512_DIGEST_SIZE;
  17141. EVP_MD_CTX_init(&md_ctx);
  17142. ret = EVP_DigestInit(&md_ctx, EVP_sha512());
  17143. if (ret == WOLFSSL_SUCCESS) {
  17144. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  17145. if (ret == WOLFSSL_SUCCESS)
  17146. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17147. }
  17148. EVP_MD_CTX_cleanup(&md_ctx);
  17149. if (ret != WOLFSSL_SUCCESS ||
  17150. XMEMCMP(hash, f.output, WC_SHA512_DIGEST_SIZE) != 0) {
  17151. return -8606;
  17152. }
  17153. #endif /* WOLFSSL_SHA512 */
  17154. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  17155. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  17156. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17157. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17158. f.output = "\x23\xfe\xc5\xbb\x94\xd6\x0b\x23\x30\x81\x92\x64\x0b\x0c\x45"
  17159. "\x33\x35\xd6\x64\x73\x4f\xe4\x0e\x72\x68\x67\x4a\xf9";
  17160. f.inLen = XSTRLEN(f.input);
  17161. f.outLen = WC_SHA512_224_DIGEST_SIZE;
  17162. EVP_MD_CTX_init(&md_ctx);
  17163. ret = EVP_DigestInit(&md_ctx, EVP_sha512_224());
  17164. if (ret == WOLFSSL_SUCCESS) {
  17165. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  17166. if (ret == WOLFSSL_SUCCESS)
  17167. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17168. }
  17169. EVP_MD_CTX_cleanup(&md_ctx);
  17170. if (ret != WOLFSSL_SUCCESS ||
  17171. XMEMCMP(hash, f.output, WC_SHA512_224_DIGEST_SIZE) != 0) {
  17172. return -8722;
  17173. }
  17174. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  17175. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  17176. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  17177. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  17178. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17179. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17180. f.output = "\x39\x28\xe1\x84\xfb\x86\x90\xf8\x40\xda\x39\x88\x12\x1d\x31"
  17181. "\xbe\x65\xcb\x9d\x3e\xf8\x3e\xe6\x14\x6f\xea\xc8\x61\xe1\x9b"
  17182. "\x56\x3a";
  17183. f.inLen = XSTRLEN(f.input);
  17184. f.outLen = WC_SHA512_256_DIGEST_SIZE;
  17185. EVP_MD_CTX_init(&md_ctx);
  17186. ret = EVP_DigestInit(&md_ctx, EVP_sha512_256());
  17187. if (ret == WOLFSSL_SUCCESS) {
  17188. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  17189. if (ret == WOLFSSL_SUCCESS)
  17190. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17191. }
  17192. EVP_MD_CTX_cleanup(&md_ctx);
  17193. if (ret != WOLFSSL_SUCCESS ||
  17194. XMEMCMP(hash, f.output, WC_SHA512_256_DIGEST_SIZE) != 0) {
  17195. return -8723;
  17196. }
  17197. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  17198. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  17199. #ifdef WOLFSSL_SHA3
  17200. #ifndef WOLFSSL_NOSHA3_224
  17201. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17202. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17203. e.output = "\x54\x3e\x68\x68\xe1\x66\x6c\x1a\x64\x36\x30\xdf\x77\x36\x7a"
  17204. "\xe5\xa6\x2a\x85\x07\x0a\x51\xc1\x4c\xbf\x66\x5c\xbc";
  17205. e.inLen = XSTRLEN(e.input);
  17206. e.outLen = WC_SHA3_224_DIGEST_SIZE;
  17207. EVP_MD_CTX_init(&md_ctx);
  17208. ret = EVP_DigestInit(&md_ctx, EVP_sha3_224());
  17209. if (ret == WOLFSSL_SUCCESS) {
  17210. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  17211. if (ret == WOLFSSL_SUCCESS)
  17212. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17213. }
  17214. EVP_MD_CTX_cleanup(&md_ctx);
  17215. if (ret != WOLFSSL_SUCCESS ||
  17216. XMEMCMP(hash, e.output, WC_SHA3_224_DIGEST_SIZE) != 0) {
  17217. return -8607;
  17218. }
  17219. #endif /* WOLFSSL_NOSHA3_224 */
  17220. #ifndef WOLFSSL_NOSHA3_256
  17221. d.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17222. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17223. d.output = "\x91\x6f\x60\x61\xfe\x87\x97\x41\xca\x64\x69\xb4\x39\x71\xdf"
  17224. "\xdb\x28\xb1\xa3\x2d\xc3\x6c\xb3\x25\x4e\x81\x2b\xe2\x7a\xad"
  17225. "\x1d\x18";
  17226. d.inLen = XSTRLEN(d.input);
  17227. d.outLen = WC_SHA3_256_DIGEST_SIZE;
  17228. EVP_MD_CTX_init(&md_ctx);
  17229. ret = EVP_DigestInit(&md_ctx, EVP_sha3_256());
  17230. if (ret == WOLFSSL_SUCCESS) {
  17231. ret = EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen);
  17232. if (ret == WOLFSSL_SUCCESS)
  17233. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17234. }
  17235. EVP_MD_CTX_cleanup(&md_ctx);
  17236. if (ret != WOLFSSL_SUCCESS ||
  17237. XMEMCMP(hash, d.output, WC_SHA3_256_DIGEST_SIZE) != 0) {
  17238. return -8608;
  17239. }
  17240. #endif /* WOLFSSL_NOSHA3_256 */
  17241. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17242. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17243. e.output = "\x79\x40\x7d\x3b\x59\x16\xb5\x9c\x3e\x30\xb0\x98\x22\x97\x47"
  17244. "\x91\xc3\x13\xfb\x9e\xcc\x84\x9e\x40\x6f\x23\x59\x2d\x04\xf6"
  17245. "\x25\xdc\x8c\x70\x9b\x98\xb4\x3b\x38\x52\xb3\x37\x21\x61\x79"
  17246. "\xaa\x7f\xc7";
  17247. e.inLen = XSTRLEN(e.input);
  17248. e.outLen = WC_SHA3_384_DIGEST_SIZE;
  17249. EVP_MD_CTX_init(&md_ctx);
  17250. ret = EVP_DigestInit(&md_ctx, EVP_sha3_384());
  17251. if (ret == WOLFSSL_SUCCESS) {
  17252. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  17253. if (ret == WOLFSSL_SUCCESS)
  17254. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17255. }
  17256. EVP_MD_CTX_cleanup(&md_ctx);
  17257. if (ret != WOLFSSL_SUCCESS ||
  17258. XMEMCMP(hash, e.output, WC_SHA3_384_DIGEST_SIZE) != 0) {
  17259. return -8609;
  17260. }
  17261. #ifndef WOLFSSL_NOSHA3_512
  17262. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17263. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17264. f.output = "\xaf\xeb\xb2\xef\x54\x2e\x65\x79\xc5\x0c\xad\x06\xd2\xe5\x78"
  17265. "\xf9\xf8\xdd\x68\x81\xd7\xdc\x82\x4d\x26\x36\x0f\xee\xbf\x18"
  17266. "\xa4\xfa\x73\xe3\x26\x11\x22\x94\x8e\xfc\xfd\x49\x2e\x74\xe8"
  17267. "\x2e\x21\x89\xed\x0f\xb4\x40\xd1\x87\xf3\x82\x27\x0c\xb4\x55"
  17268. "\xf2\x1d\xd1\x85";
  17269. f.inLen = XSTRLEN(f.input);
  17270. f.outLen = WC_SHA3_512_DIGEST_SIZE;
  17271. EVP_MD_CTX_init(&md_ctx);
  17272. ret = EVP_DigestInit(&md_ctx, EVP_sha3_512());
  17273. if (ret == WOLFSSL_SUCCESS) {
  17274. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  17275. if (ret == WOLFSSL_SUCCESS)
  17276. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17277. }
  17278. EVP_MD_CTX_cleanup(&md_ctx);
  17279. if (ret != WOLFSSL_SUCCESS ||
  17280. XMEMCMP(hash, f.output, WC_SHA3_512_DIGEST_SIZE) != 0) {
  17281. return -8610;
  17282. }
  17283. #endif /* WOLFSSL_NOSHA3_512 */
  17284. #endif /* WOLFSSL_SHA3 */
  17285. #ifndef WC_NO_RNG
  17286. if (RAND_bytes(hash, sizeof(hash)) != WOLFSSL_SUCCESS)
  17287. return -8611;
  17288. #endif
  17289. #ifndef NO_MD5
  17290. c.input = "what do ya want for nothing?";
  17291. c.output = "\x55\x78\xe8\x48\x4b\xcc\x93\x80\x93\xec\x53\xaf\x22\xd6\x14"
  17292. "\x76";
  17293. c.inLen = XSTRLEN(c.input);
  17294. c.outLen = WC_MD5_DIGEST_SIZE;
  17295. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)
  17296. /* Expect failure with MD5 + HMAC when using FIPS 140-3. */
  17297. if (HMAC(EVP_md5(), "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen,
  17298. hash, 0) != NULL)
  17299. #else
  17300. if (HMAC(EVP_md5(), "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen,
  17301. hash, 0) == NULL ||
  17302. XMEMCMP(hash, c.output, WC_MD5_DIGEST_SIZE) != 0)
  17303. #endif
  17304. {
  17305. return -8612;
  17306. }
  17307. #endif /* NO_MD5 */
  17308. #ifndef NO_DES3
  17309. { /* des test */
  17310. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  17311. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  17312. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  17313. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  17314. };
  17315. byte plain[24];
  17316. byte cipher[24];
  17317. const_DES_cblock key = {
  17318. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  17319. };
  17320. DES_cblock iv = {
  17321. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  17322. };
  17323. DES_key_schedule sched;
  17324. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17325. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  17326. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  17327. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  17328. };
  17329. DES_key_sched(&key, &sched);
  17330. DES_cbc_encrypt(vector, cipher, sizeof(vector), &sched, &iv, DES_ENCRYPT);
  17331. DES_cbc_encrypt(cipher, plain, sizeof(vector), &sched, &iv, DES_DECRYPT);
  17332. if (XMEMCMP(plain, vector, sizeof(vector)) != 0)
  17333. return -8613;
  17334. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  17335. return -8614;
  17336. /* test changing iv */
  17337. DES_ncbc_encrypt(vector, cipher, 8, &sched, &iv, DES_ENCRYPT);
  17338. DES_ncbc_encrypt(vector + 8, cipher + 8, 16, &sched, &iv, DES_ENCRYPT);
  17339. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  17340. return -8615;
  17341. } /* end des test */
  17342. #endif /* NO_DES3 */
  17343. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  17344. if ((ret = openssl_aes_test()) != 0) {
  17345. return ret;
  17346. }
  17347. #if defined(WOLFSSL_AES_128) && defined(HAVE_AES_CBC)
  17348. { /* evp_cipher test: EVP_aes_128_cbc */
  17349. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17350. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  17351. #else
  17352. EVP_CIPHER_CTX ctx[1];
  17353. #endif
  17354. int idx, cipherSz, plainSz;
  17355. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  17356. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  17357. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  17358. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  17359. };
  17360. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17361. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  17362. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  17363. 0x3b,0x5d,0x41,0x97,0x94,0x25,0xa4,0xb4,
  17364. 0xae,0x7b,0x34,0xd0,0x3f,0x0c,0xbc,0x06
  17365. };
  17366. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  17367. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  17368. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  17369. 0x7d,0x37,0x7b,0x0b,0x44,0xaa,0xb5,0xf0,
  17370. 0x5f,0x34,0xb4,0xde,0xb5,0xbd,0x2a,0xbb
  17371. };
  17372. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  17373. "0123456789abcdef "; /* align */
  17374. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  17375. "1234567890abcdef "; /* align */
  17376. byte cipher[AES_BLOCK_SIZE * 4];
  17377. byte plain [AES_BLOCK_SIZE * 4];
  17378. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17379. if (ctx == NULL)
  17380. return MEMORY_E;
  17381. #endif
  17382. cipherSz = 0;
  17383. EVP_CIPHER_CTX_init(ctx);
  17384. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  17385. if (ret == WOLFSSL_SUCCESS) {
  17386. ret = EVP_CipherUpdate(ctx, cipher, &idx, (byte*)msg, sizeof(msg));
  17387. if (ret == WOLFSSL_SUCCESS)
  17388. cipherSz += idx;
  17389. }
  17390. if (ret == WOLFSSL_SUCCESS) {
  17391. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  17392. if (ret == WOLFSSL_SUCCESS)
  17393. cipherSz += idx;
  17394. }
  17395. EVP_CIPHER_CTX_cleanup(ctx);
  17396. if (ret != WOLFSSL_SUCCESS)
  17397. return -8617;
  17398. if (cipherSz != (int)sizeof(verify) || XMEMCMP(cipher, verify, cipherSz))
  17399. return -8618;
  17400. /* check partial decrypt (not enough padding for full block) */
  17401. plainSz = 0;
  17402. EVP_CIPHER_CTX_init(ctx);
  17403. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  17404. if (ret == WOLFSSL_SUCCESS) {
  17405. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, 1);
  17406. if (ret == WOLFSSL_SUCCESS)
  17407. plainSz += idx;
  17408. }
  17409. if (ret == WOLFSSL_SUCCESS) {
  17410. /* this test should fail... not enough padding for full block */
  17411. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  17412. if (plainSz == 0 && ret != WOLFSSL_SUCCESS)
  17413. ret = WOLFSSL_SUCCESS;
  17414. else
  17415. ret = -8619;
  17416. }
  17417. else
  17418. ret = -8620;
  17419. EVP_CIPHER_CTX_cleanup(ctx);
  17420. if (ret != WOLFSSL_SUCCESS)
  17421. return ret;
  17422. plainSz = 0;
  17423. EVP_CIPHER_CTX_init(ctx);
  17424. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  17425. if (ret == WOLFSSL_SUCCESS) {
  17426. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, cipherSz);
  17427. if (ret == WOLFSSL_SUCCESS)
  17428. plainSz += idx;
  17429. }
  17430. if (ret == WOLFSSL_SUCCESS) {
  17431. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  17432. if (ret == WOLFSSL_SUCCESS)
  17433. plainSz += idx;
  17434. }
  17435. EVP_CIPHER_CTX_cleanup(ctx);
  17436. if (ret != WOLFSSL_SUCCESS)
  17437. return -8621;
  17438. if (plainSz != (int)sizeof(msg) || XMEMCMP(plain, msg, sizeof(msg)))
  17439. return -8622;
  17440. cipherSz = 0;
  17441. EVP_CIPHER_CTX_init(ctx);
  17442. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  17443. if (ret == WOLFSSL_SUCCESS) {
  17444. ret = EVP_CipherUpdate(ctx, cipher, &idx, msg, AES_BLOCK_SIZE);
  17445. if (ret == WOLFSSL_SUCCESS)
  17446. cipherSz += idx;
  17447. }
  17448. if (ret == WOLFSSL_SUCCESS) {
  17449. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  17450. if (ret == WOLFSSL_SUCCESS)
  17451. cipherSz += idx;
  17452. }
  17453. EVP_CIPHER_CTX_cleanup(ctx);
  17454. if (ret != WOLFSSL_SUCCESS)
  17455. return -8623;
  17456. if (cipherSz != (int)sizeof(verify2) || XMEMCMP(cipher, verify2, cipherSz))
  17457. return -8624;
  17458. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17459. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  17460. #endif
  17461. } /* end evp_cipher test: EVP_aes_128_cbc*/
  17462. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  17463. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  17464. { /* evp_cipher test: EVP_aes_256_ecb*/
  17465. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17466. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  17467. #else
  17468. EVP_CIPHER_CTX ctx[1];
  17469. #endif
  17470. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  17471. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17472. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  17473. };
  17474. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17475. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  17476. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  17477. };
  17478. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  17479. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  17480. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  17481. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  17482. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  17483. };
  17484. byte cipher[AES_BLOCK_SIZE * 4];
  17485. byte plain [AES_BLOCK_SIZE * 4];
  17486. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17487. if (ctx == NULL)
  17488. return MEMORY_E;
  17489. #endif
  17490. EVP_CIPHER_CTX_init(ctx);
  17491. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1);
  17492. if (ret == WOLFSSL_SUCCESS)
  17493. ret = EVP_Cipher(ctx, cipher, (byte*)msg, 16);
  17494. EVP_CIPHER_CTX_cleanup(ctx);
  17495. if (ret != 16)
  17496. return -8625;
  17497. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  17498. return -8626;
  17499. EVP_CIPHER_CTX_init(ctx);
  17500. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0);
  17501. if (ret == WOLFSSL_SUCCESS)
  17502. ret = EVP_Cipher(ctx, plain, cipher, 16);
  17503. EVP_CIPHER_CTX_cleanup(ctx);
  17504. if (ret != 16)
  17505. return -8627;
  17506. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  17507. return -8628;
  17508. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17509. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  17510. #endif
  17511. } /* end evp_cipher test */
  17512. #endif /* HAVE_AES_ECB && WOLFSSL_AES_128 */
  17513. #define OPENSSL_TEST_ERROR (-10000)
  17514. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  17515. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  17516. {
  17517. /* Test: AES_encrypt/decrypt/set Key */
  17518. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17519. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  17520. #ifdef HAVE_AES_DECRYPT
  17521. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  17522. #endif
  17523. #else
  17524. AES_KEY enc[1];
  17525. #ifdef HAVE_AES_DECRYPT
  17526. AES_KEY dec[1];
  17527. #endif
  17528. #endif
  17529. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  17530. {
  17531. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17532. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  17533. };
  17534. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  17535. {
  17536. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  17537. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  17538. };
  17539. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  17540. {
  17541. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  17542. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  17543. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  17544. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  17545. };
  17546. byte plain[sizeof(msg)];
  17547. byte cipher[sizeof(msg)];
  17548. printf("openSSL extra test\n") ;
  17549. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17550. if (enc == NULL)
  17551. return MEMORY_E;
  17552. #ifdef HAVE_AES_DECRYPT
  17553. if (dec == NULL)
  17554. return MEMORY_E;
  17555. #endif
  17556. #endif
  17557. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  17558. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  17559. AES_encrypt(msg, cipher, enc);
  17560. #ifdef HAVE_AES_DECRYPT
  17561. AES_decrypt(cipher, plain, dec);
  17562. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  17563. return OPENSSL_TEST_ERROR-60;
  17564. #endif /* HAVE_AES_DECRYPT */
  17565. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  17566. return OPENSSL_TEST_ERROR-61;
  17567. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17568. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  17569. #ifdef HAVE_AES_DECRYPT
  17570. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  17571. #endif
  17572. #endif
  17573. }
  17574. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  17575. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  17576. #ifdef WOLFSSL_AES_COUNTER
  17577. {
  17578. byte plainBuff [64];
  17579. byte cipherBuff[64];
  17580. #ifdef WOLFSSL_AES_128
  17581. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  17582. {
  17583. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  17584. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  17585. };
  17586. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  17587. {
  17588. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  17589. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  17590. };
  17591. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  17592. {
  17593. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17594. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  17595. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  17596. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  17597. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  17598. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  17599. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  17600. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  17601. };
  17602. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  17603. {
  17604. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  17605. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  17606. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  17607. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  17608. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  17609. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  17610. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  17611. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  17612. };
  17613. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  17614. {
  17615. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  17616. 0xc2
  17617. };
  17618. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17619. EVP_CIPHER_CTX *p_en;
  17620. EVP_CIPHER_CTX *p_de;
  17621. #endif
  17622. #endif /* WOLFSSL_AES_128 */
  17623. #ifdef WOLFSSL_AES_192
  17624. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  17625. * NIST Special Publication 800-38A */
  17626. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  17627. {
  17628. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  17629. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  17630. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  17631. };
  17632. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  17633. {
  17634. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  17635. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  17636. };
  17637. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  17638. {
  17639. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17640. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  17641. };
  17642. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  17643. {
  17644. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  17645. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  17646. };
  17647. #endif /* WOLFSSL_AES_192 */
  17648. #ifdef WOLFSSL_AES_256
  17649. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  17650. * NIST Special Publication 800-38A */
  17651. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  17652. {
  17653. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  17654. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  17655. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  17656. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  17657. };
  17658. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  17659. {
  17660. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  17661. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  17662. };
  17663. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  17664. {
  17665. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17666. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  17667. };
  17668. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  17669. {
  17670. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  17671. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  17672. };
  17673. #endif /* WOLFSSL_AES_256 */
  17674. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17675. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  17676. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  17677. if ((en == NULL) || (de == NULL))
  17678. return MEMORY_E;
  17679. #else
  17680. EVP_CIPHER_CTX en[1];
  17681. EVP_CIPHER_CTX de[1];
  17682. #endif
  17683. #ifdef WOLFSSL_AES_128
  17684. EVP_CIPHER_CTX_init(en);
  17685. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  17686. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17687. return -8629;
  17688. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  17689. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  17690. return -8630;
  17691. EVP_CIPHER_CTX_init(de);
  17692. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  17693. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17694. return -8631;
  17695. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  17696. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  17697. return -8632;
  17698. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  17699. return -8633;
  17700. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  17701. return -8634;
  17702. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17703. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  17704. if (p_en == NULL)
  17705. return -8635;
  17706. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  17707. if (p_de == NULL)
  17708. return -8636;
  17709. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  17710. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17711. return -8637;
  17712. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  17713. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  17714. return -8638;
  17715. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  17716. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17717. return -8639;
  17718. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  17719. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  17720. return -8640;
  17721. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  17722. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  17723. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  17724. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  17725. return -8641;
  17726. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  17727. return -8642;
  17728. EVP_CIPHER_CTX_init(en);
  17729. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  17730. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17731. return -8643;
  17732. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  17733. return -8644;
  17734. EVP_CIPHER_CTX_init(de);
  17735. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  17736. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17737. return -8645;
  17738. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  17739. return -8646;
  17740. if (XMEMCMP(plainBuff, ctrPlain, 9))
  17741. return -8647;
  17742. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  17743. return -8648;
  17744. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  17745. return -8649;
  17746. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  17747. return -8650;
  17748. if (XMEMCMP(plainBuff, ctrPlain, 9))
  17749. return -8651;
  17750. if (XMEMCMP(cipherBuff, oddCipher, 9))
  17751. return -8652;
  17752. #endif /* WOLFSSL_AES_128 */
  17753. #ifdef WOLFSSL_AES_192
  17754. EVP_CIPHER_CTX_init(en);
  17755. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  17756. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  17757. return -8653;
  17758. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  17759. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  17760. return -8654;
  17761. EVP_CIPHER_CTX_init(de);
  17762. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  17763. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  17764. return -8655;
  17765. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  17766. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  17767. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  17768. return -8656;
  17769. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  17770. return -8657;
  17771. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  17772. return -8658;
  17773. #endif /* WOLFSSL_AES_192 */
  17774. #ifdef WOLFSSL_AES_256
  17775. EVP_CIPHER_CTX_init(en);
  17776. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  17777. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  17778. return -8659;
  17779. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  17780. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  17781. return -8660;
  17782. EVP_CIPHER_CTX_init(de);
  17783. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  17784. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  17785. return -8661;
  17786. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  17787. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  17788. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  17789. return -8662;
  17790. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  17791. return -8663;
  17792. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  17793. return -8664;
  17794. #endif /* WOLFSSL_AES_256 */
  17795. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17796. wolfSSL_EVP_CIPHER_CTX_free(en);
  17797. wolfSSL_EVP_CIPHER_CTX_free(de);
  17798. #endif
  17799. }
  17800. #endif /* HAVE_AES_COUNTER */
  17801. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  17802. {
  17803. /* EVP_CipherUpdate test */
  17804. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  17805. {
  17806. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17807. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  17808. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  17809. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  17810. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  17811. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  17812. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  17813. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  17814. };
  17815. byte key[] = "0123456789abcdef "; /* align */
  17816. byte iv[] = "1234567890abcdef "; /* align */
  17817. byte cipher[AES_BLOCK_SIZE * 4];
  17818. byte plain [AES_BLOCK_SIZE * 4];
  17819. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17820. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  17821. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  17822. #else
  17823. EVP_CIPHER_CTX en[1];
  17824. EVP_CIPHER_CTX de[1];
  17825. #endif
  17826. int outlen ;
  17827. int total = 0;
  17828. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17829. if ((en == NULL) || (de == NULL))
  17830. return MEMORY_E;
  17831. #endif
  17832. EVP_CIPHER_CTX_init(en);
  17833. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  17834. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  17835. return -8665;
  17836. /* openSSL compatibility, if(inlen == 0)return 1; */
  17837. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  17838. (byte*)cbcPlain, 0) != 1)
  17839. return -8666;
  17840. EVP_CIPHER_CTX_init(en);
  17841. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  17842. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  17843. return -8667;
  17844. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  17845. (byte*)cbcPlain, 9) == 0)
  17846. return -8668;
  17847. if(outlen != 0)
  17848. return -8669;
  17849. total += outlen;
  17850. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  17851. (byte*)&cbcPlain[9] , 9) == 0)
  17852. return -8670;
  17853. if(outlen != 16)
  17854. return -8671;
  17855. total += outlen;
  17856. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  17857. return -8672;
  17858. if(outlen != 16)
  17859. return -8673;
  17860. total += outlen;
  17861. if(total != 32)
  17862. return -8674;
  17863. total = 0;
  17864. EVP_CIPHER_CTX_init(de);
  17865. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  17866. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  17867. return -8675;
  17868. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  17869. return -8676;
  17870. if(outlen != 0)
  17871. return -8677;
  17872. total += outlen;
  17873. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  17874. (byte*)&cipher[6], 12) == 0)
  17875. return -8678;
  17876. if(outlen != 0)
  17877. total += outlen;
  17878. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  17879. (byte*)&cipher[6+12], 14) == 0)
  17880. return -8679;
  17881. if(outlen != 16)
  17882. return -8680;
  17883. total += outlen;
  17884. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  17885. return -8681;
  17886. if(outlen != 2)
  17887. return -8682;
  17888. total += outlen;
  17889. if(total != 18)
  17890. return -8683;
  17891. if (XMEMCMP(plain, cbcPlain, 18))
  17892. return -8684;
  17893. total = 0;
  17894. EVP_CIPHER_CTX_init(en);
  17895. if (EVP_EncryptInit(en, EVP_aes_128_cbc(),
  17896. (unsigned char*)key, (unsigned char*)iv) == 0)
  17897. return -8685;
  17898. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen, (byte*)cbcPlain, 9) == 0)
  17899. return -8686;
  17900. if(outlen != 0)
  17901. return -8687;
  17902. total += outlen;
  17903. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen, (byte*)&cbcPlain[9] , 9) == 0)
  17904. return -8688;
  17905. if(outlen != 16)
  17906. return -8689;
  17907. total += outlen;
  17908. if (EVP_EncryptFinal(en, (byte*)&cipher[total], &outlen) == 0)
  17909. return -8690;
  17910. if(outlen != 16)
  17911. return -8691;
  17912. total += outlen;
  17913. if(total != 32)
  17914. return 3438;
  17915. total = 0;
  17916. EVP_CIPHER_CTX_init(de);
  17917. if (EVP_DecryptInit(de, EVP_aes_128_cbc(),
  17918. (unsigned char*)key, (unsigned char*)iv) == 0)
  17919. return -8692;
  17920. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  17921. return -8693;
  17922. if(outlen != 0)
  17923. return -8694;
  17924. total += outlen;
  17925. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6], 12) == 0)
  17926. return -8695;
  17927. if(outlen != 0)
  17928. total += outlen;
  17929. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6+12], 14) == 0)
  17930. return -8696;
  17931. if(outlen != 16)
  17932. return -8697;
  17933. total += outlen;
  17934. if (EVP_DecryptFinal(de, (byte*)&plain[total], &outlen) == 0)
  17935. return -8698;
  17936. if(outlen != 2)
  17937. return -8699;
  17938. total += outlen;
  17939. if(total != 18)
  17940. return 3447;
  17941. if (XMEMCMP(plain, cbcPlain, 18))
  17942. return -8700;
  17943. if (EVP_CIPHER_key_length(NULL) != 0)
  17944. return -8701;
  17945. if (EVP_CIPHER_key_length(EVP_aes_128_cbc()) != 16)
  17946. return -8702;
  17947. if (EVP_CIPHER_CTX_mode(NULL) != 0)
  17948. return -8703;
  17949. if (EVP_CIPHER_CTX_mode(en) != (en->flags & WOLFSSL_EVP_CIPH_MODE))
  17950. return -8704;
  17951. EVP_CIPHER_CTX_init(en);
  17952. if (EVP_CipherInit_ex(en, EVP_aes_128_cbc(), NULL,
  17953. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  17954. return -8705;
  17955. EVP_CIPHER_CTX_init(en);
  17956. if (EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  17957. (unsigned char*)key, (unsigned char*)iv) == 0)
  17958. return -8706;
  17959. if (wolfSSL_EVP_EncryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  17960. return -8707;
  17961. if (wolfSSL_EVP_EncryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  17962. return -8708;
  17963. EVP_CIPHER_CTX_init(de);
  17964. if (EVP_DecryptInit_ex(de, EVP_aes_128_cbc(), NULL,
  17965. (unsigned char*)key, (unsigned char*)iv) == 0)
  17966. return -8709;
  17967. if (wolfSSL_EVP_DecryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  17968. return -8710;
  17969. if (wolfSSL_EVP_DecryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  17970. return -8711;
  17971. if (EVP_CIPHER_CTX_block_size(NULL) != BAD_FUNC_ARG)
  17972. return -8712;
  17973. EVP_CIPHER_CTX_init(en);
  17974. EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  17975. (unsigned char*)key, (unsigned char*)iv);
  17976. if (EVP_CIPHER_CTX_block_size(en) != en->block_size)
  17977. return -8713;
  17978. if (EVP_CIPHER_block_size(NULL) != BAD_FUNC_ARG)
  17979. return -8714;
  17980. if (EVP_CIPHER_block_size(EVP_aes_128_cbc()) != AES_BLOCK_SIZE)
  17981. return -8715;
  17982. if (WOLFSSL_EVP_CIPHER_mode(NULL) != 0)
  17983. return -8716;
  17984. if (EVP_CIPHER_flags(EVP_aes_128_cbc()) != WOLFSSL_EVP_CIPH_CBC_MODE)
  17985. return -8717;
  17986. EVP_CIPHER_CTX_clear_flags(en, 0xFFFFFFFF);
  17987. EVP_CIPHER_CTX_set_flags(en, 42);
  17988. if (en->flags != 42)
  17989. return -8718;
  17990. if (EVP_CIPHER_CTX_set_padding(NULL, 0) != BAD_FUNC_ARG)
  17991. return -8719;
  17992. if (EVP_CIPHER_CTX_set_padding(en, 0) != WOLFSSL_SUCCESS)
  17993. return -8720;
  17994. if (EVP_CIPHER_CTX_set_padding(en, 1) != WOLFSSL_SUCCESS)
  17995. return -8721;
  17996. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17997. wolfSSL_EVP_CIPHER_CTX_free(en);
  17998. wolfSSL_EVP_CIPHER_CTX_free(de);
  17999. #endif
  18000. }
  18001. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  18002. #endif /* ifndef NO_AES */
  18003. return 0;
  18004. }
  18005. WOLFSSL_TEST_SUBROUTINE int openSSL_evpMD_test(void)
  18006. {
  18007. int ret = 0;
  18008. #if !defined(NO_SHA256) && !defined(NO_SHA)
  18009. WOLFSSL_EVP_MD_CTX* ctx;
  18010. WOLFSSL_EVP_MD_CTX* ctx2;
  18011. ctx = EVP_MD_CTX_create();
  18012. ctx2 = EVP_MD_CTX_create();
  18013. ret = EVP_DigestInit(ctx, EVP_sha256());
  18014. if (ret != SSL_SUCCESS) {
  18015. ret = -8800;
  18016. goto openSSL_evpMD_test_done;
  18017. }
  18018. ret = EVP_MD_CTX_copy(ctx2, ctx);
  18019. if (ret != SSL_SUCCESS) {
  18020. ret = -8801;
  18021. goto openSSL_evpMD_test_done;
  18022. }
  18023. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  18024. ret = -8802;
  18025. goto openSSL_evpMD_test_done;
  18026. }
  18027. ret = EVP_DigestInit(ctx, EVP_sha1());
  18028. if (ret != SSL_SUCCESS) {
  18029. ret = -8803;
  18030. goto openSSL_evpMD_test_done;
  18031. }
  18032. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  18033. ret = -8804;
  18034. goto openSSL_evpMD_test_done;
  18035. }
  18036. ret = EVP_MD_CTX_copy_ex(ctx2, ctx);
  18037. if (ret != SSL_SUCCESS) {
  18038. ret = -8805;
  18039. goto openSSL_evpMD_test_done;
  18040. }
  18041. if (EVP_MD_type(EVP_sha256()) == EVP_MD_CTX_type(ctx2)) {
  18042. ret = -8806;
  18043. goto openSSL_evpMD_test_done;
  18044. }
  18045. if (EVP_MD_type(EVP_sha1()) != EVP_MD_CTX_type(ctx2)) {
  18046. ret = -8807;
  18047. goto openSSL_evpMD_test_done;
  18048. }
  18049. if (EVP_DigestInit_ex(ctx, EVP_sha1(), NULL) != SSL_SUCCESS) {
  18050. ret = -8808;
  18051. goto openSSL_evpMD_test_done;
  18052. }
  18053. if (EVP_add_digest(NULL) != 0) {
  18054. ret = -8809;
  18055. goto openSSL_evpMD_test_done;
  18056. }
  18057. if (wolfSSL_EVP_add_cipher(NULL) != 0) {
  18058. ret = -8810;
  18059. goto openSSL_evpMD_test_done;
  18060. }
  18061. ret = 0; /* got to success state without jumping to end with a fail */
  18062. openSSL_evpMD_test_done:
  18063. EVP_MD_CTX_destroy(ctx);
  18064. EVP_MD_CTX_destroy(ctx2);
  18065. #endif /* NO_SHA256 */
  18066. return ret;
  18067. }
  18068. #ifdef DEBUG_SIGN
  18069. static void show(const char *title, const char *p, unsigned int s) {
  18070. char* i;
  18071. printf("%s: ", title);
  18072. for (i = p;
  18073. i < p + s;
  18074. printf("%c", *i), i++);
  18075. printf("\n");
  18076. }
  18077. #else
  18078. #define show(a,b,c)
  18079. #endif
  18080. #define FOURK_BUFF 4096
  18081. #define ERR_BASE_PKEY (-5000)
  18082. WOLFSSL_TEST_SUBROUTINE int openssl_pkey0_test(void)
  18083. {
  18084. int ret = 0;
  18085. #if !defined(NO_RSA) && !defined(HAVE_USER_RSA) && !defined(NO_SHA)
  18086. byte* prvTmp;
  18087. byte* pubTmp;
  18088. int prvBytes;
  18089. int pubBytes;
  18090. RSA *prvRsa = NULL;
  18091. RSA *pubRsa = NULL;
  18092. EVP_PKEY *prvPkey = NULL;
  18093. EVP_PKEY *pubPkey = NULL;
  18094. EVP_PKEY_CTX *enc = NULL;
  18095. EVP_PKEY_CTX *dec = NULL;
  18096. byte in[] = TEST_STRING;
  18097. byte out[256];
  18098. size_t outlen;
  18099. size_t keySz;
  18100. byte plain[256];
  18101. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  18102. XFILE keyFile;
  18103. XFILE keypubFile;
  18104. char cliKey[] = "./certs/client-key.der";
  18105. char cliKeypub[] = "./certs/client-keyPub.der";
  18106. #endif
  18107. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18108. if (prvTmp == NULL)
  18109. return ERR_BASE_PKEY-1;
  18110. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18111. if (pubTmp == NULL) {
  18112. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18113. return ERR_BASE_PKEY-2;
  18114. }
  18115. #ifdef USE_CERT_BUFFERS_1024
  18116. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  18117. prvBytes = sizeof_client_key_der_1024;
  18118. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  18119. pubBytes = sizeof_client_keypub_der_1024;
  18120. #elif defined(USE_CERT_BUFFERS_2048)
  18121. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  18122. prvBytes = sizeof_client_key_der_2048;
  18123. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  18124. pubBytes = sizeof_client_keypub_der_2048;
  18125. #else
  18126. keyFile = XFOPEN(cliKey, "rb");
  18127. if (!keyFile) {
  18128. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18129. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18130. err_sys("can't open ./certs/client-key.der, "
  18131. "Please run from wolfSSL home dir", ERR_BASE_PKEY-3);
  18132. return ERR_BASE_PKEY-3;
  18133. }
  18134. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  18135. XFCLOSE(keyFile);
  18136. keypubFile = XFOPEN(cliKeypub, "rb");
  18137. if (!keypubFile) {
  18138. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18139. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18140. err_sys("can't open ./certs/client-cert.der, "
  18141. "Please run from wolfSSL home dir", -4);
  18142. return ERR_BASE_PKEY-4;
  18143. }
  18144. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  18145. XFCLOSE(keypubFile);
  18146. #endif /* USE_CERT_BUFFERS */
  18147. prvRsa = wolfSSL_RSA_new();
  18148. pubRsa = wolfSSL_RSA_new();
  18149. if((prvRsa == NULL) || (pubRsa == NULL)){
  18150. printf("error with RSA_new\n");
  18151. ret = ERR_BASE_PKEY-10;
  18152. goto openssl_pkey0_test_done;
  18153. }
  18154. ret = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  18155. if(ret != SSL_SUCCESS){
  18156. printf("error with RSA_LoadDer_ex\n");
  18157. ret = ERR_BASE_PKEY-11;
  18158. goto openssl_pkey0_test_done;
  18159. }
  18160. ret = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  18161. if(ret != SSL_SUCCESS){
  18162. printf("error with RSA_LoadDer_ex\n");
  18163. ret = ERR_BASE_PKEY-12;
  18164. goto openssl_pkey0_test_done;
  18165. }
  18166. keySz = (size_t)RSA_size(pubRsa);
  18167. prvPkey = wolfSSL_EVP_PKEY_new();
  18168. pubPkey = wolfSSL_EVP_PKEY_new();
  18169. if((prvPkey == NULL) || (pubPkey == NULL)){
  18170. printf("error with PKEY_new\n");
  18171. ret = ERR_BASE_PKEY-13;
  18172. goto openssl_pkey0_test_done;
  18173. }
  18174. ret = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  18175. ret += wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  18176. if(ret != 2){
  18177. printf("error with PKEY_set1_RSA\n");
  18178. ret = ERR_BASE_PKEY-14;
  18179. goto openssl_pkey0_test_done;
  18180. }
  18181. dec = EVP_PKEY_CTX_new(prvPkey, NULL);
  18182. enc = EVP_PKEY_CTX_new(pubPkey, NULL);
  18183. if((dec == NULL)||(enc==NULL)){
  18184. printf("error with EVP_PKEY_CTX_new\n");
  18185. ret = ERR_BASE_PKEY-15;
  18186. goto openssl_pkey0_test_done;
  18187. }
  18188. ret = EVP_PKEY_decrypt_init(dec);
  18189. if (ret != 1) {
  18190. printf("error with decrypt init\n");
  18191. ret = ERR_BASE_PKEY-16;
  18192. goto openssl_pkey0_test_done;
  18193. }
  18194. ret = EVP_PKEY_encrypt_init(enc);
  18195. if (ret != 1) {
  18196. printf("error with encrypt init\n");
  18197. ret = ERR_BASE_PKEY-17;
  18198. goto openssl_pkey0_test_done;
  18199. }
  18200. XMEMSET(out, 0, sizeof(out));
  18201. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  18202. if (ret != 1) {
  18203. printf("error encrypting msg\n");
  18204. ret = ERR_BASE_PKEY-18;
  18205. goto openssl_pkey0_test_done;
  18206. }
  18207. show("encrypted msg", out, outlen);
  18208. XMEMSET(plain, 0, sizeof(plain));
  18209. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  18210. if (ret != 1) {
  18211. printf("error decrypting msg\n");
  18212. ret = ERR_BASE_PKEY-19;
  18213. goto openssl_pkey0_test_done;
  18214. }
  18215. show("decrypted msg", plain, outlen);
  18216. /* RSA_PKCS1_OAEP_PADDING test */
  18217. ret = EVP_PKEY_decrypt_init(dec);
  18218. if (ret != 1) {
  18219. printf("error with decrypt init\n");
  18220. ret = ERR_BASE_PKEY-30;
  18221. goto openssl_pkey0_test_done;
  18222. }
  18223. ret = EVP_PKEY_encrypt_init(enc);
  18224. if (ret != 1) {
  18225. printf("error with encrypt init\n");
  18226. ret = ERR_BASE_PKEY-31;
  18227. goto openssl_pkey0_test_done;
  18228. }
  18229. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  18230. printf("first set rsa padding error\n");
  18231. ret = ERR_BASE_PKEY-32;
  18232. goto openssl_pkey0_test_done;
  18233. }
  18234. #ifndef HAVE_FIPS
  18235. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  18236. printf("second set rsa padding error\n");
  18237. ret = ERR_BASE_PKEY-33;
  18238. goto openssl_pkey0_test_done;
  18239. }
  18240. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  18241. printf("third set rsa padding error\n");
  18242. ret = ERR_BASE_PKEY-34;
  18243. goto openssl_pkey0_test_done;
  18244. }
  18245. #endif
  18246. XMEMSET(out, 0, sizeof(out));
  18247. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  18248. if (ret != 1) {
  18249. printf("error encrypting msg\n");
  18250. ret = ERR_BASE_PKEY-35;
  18251. goto openssl_pkey0_test_done;
  18252. }
  18253. show("encrypted msg", out, outlen);
  18254. XMEMSET(plain, 0, sizeof(plain));
  18255. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  18256. if (ret != 1) {
  18257. printf("error decrypting msg\n");
  18258. ret = ERR_BASE_PKEY-36;
  18259. goto openssl_pkey0_test_done;
  18260. }
  18261. show("decrypted msg", plain, outlen);
  18262. ret = 0; /* made it to this point without error then set success */
  18263. openssl_pkey0_test_done:
  18264. wolfSSL_RSA_free(prvRsa);
  18265. wolfSSL_RSA_free(pubRsa);
  18266. EVP_PKEY_free(pubPkey);
  18267. EVP_PKEY_free(prvPkey);
  18268. EVP_PKEY_CTX_free(dec);
  18269. EVP_PKEY_CTX_free(enc);
  18270. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18271. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18272. #endif /* NO_RSA */
  18273. return ret;
  18274. }
  18275. WOLFSSL_TEST_SUBROUTINE int openssl_pkey1_test(void)
  18276. {
  18277. int ret = 0;
  18278. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  18279. !defined(NO_SHA)
  18280. EVP_PKEY_CTX* dec = NULL;
  18281. EVP_PKEY_CTX* enc = NULL;
  18282. EVP_PKEY* pubKey = NULL;
  18283. EVP_PKEY* prvKey = NULL;
  18284. X509* x509 = NULL;
  18285. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sugar slapped";
  18286. const unsigned char* clikey;
  18287. long cliKeySz;
  18288. size_t outlen;
  18289. int keyLenBits = 2048;
  18290. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18291. unsigned char *tmp = (unsigned char *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18292. unsigned char *cipher = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18293. unsigned char *plain = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18294. if ((tmp == NULL) ||
  18295. (cipher == NULL) ||
  18296. (plain == NULL)) {
  18297. ret = -9015;
  18298. goto openssl_pkey1_test_done;
  18299. }
  18300. #else
  18301. unsigned char tmp[FOURK_BUF];
  18302. unsigned char cipher[RSA_TEST_BYTES];
  18303. unsigned char plain[RSA_TEST_BYTES];
  18304. #endif
  18305. #if defined(USE_CERT_BUFFERS_1024)
  18306. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  18307. cliKeySz = (long)sizeof_client_key_der_1024;
  18308. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_1024,
  18309. sizeof_client_cert_der_1024, SSL_FILETYPE_ASN1);
  18310. keyLenBits = 1024;
  18311. #elif defined(USE_CERT_BUFFERS_2048)
  18312. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  18313. cliKeySz = (long)sizeof_client_key_der_2048;
  18314. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_2048,
  18315. sizeof_client_cert_der_2048, SSL_FILETYPE_ASN1);
  18316. #elif defined(USE_CERT_BUFFERS_3072)
  18317. XMEMCPY(tmp, client_key_der_3072, sizeof_client_key_der_3072);
  18318. cliKeySz = (long)sizeof_client_key_der_3072;
  18319. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_3072,
  18320. sizeof_client_cert_der_3072, SSL_FILETYPE_ASN1);
  18321. keyLenBits = 3072;
  18322. #elif defined(USE_CERT_BUFFERS_4096)
  18323. XMEMCPY(tmp, client_key_der_4096, sizeof_client_key_der_4096);
  18324. cliKeySz = (long)sizeof_client_key_der_4096;
  18325. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_4096,
  18326. sizeof_client_cert_der_4096, SSL_FILETYPE_ASN1);
  18327. keyLenBits = 4096;
  18328. #else
  18329. {
  18330. XFILE f;
  18331. f = XFOPEN(clientKey, "rb");
  18332. if (!f) {
  18333. err_sys("can't open ./certs/client-key.der, "
  18334. "Please run from wolfSSL home dir", -41);
  18335. ret = -9000;
  18336. goto openssl_pkey1_test_done;
  18337. }
  18338. cliKeySz = (long)XFREAD(tmp, 1, FOURK_BUF, f);
  18339. XFCLOSE(f);
  18340. }
  18341. /* using existing wolfSSL api to get public and private key */
  18342. x509 = wolfSSL_X509_load_certificate_file(clientCert, SSL_FILETYPE_ASN1);
  18343. #endif /* USE_CERT_BUFFERS */
  18344. clikey = tmp;
  18345. if ((prvKey = EVP_PKEY_new()) == NULL) {
  18346. ret = -9001;
  18347. goto openssl_pkey1_test_done;
  18348. }
  18349. EVP_PKEY_free(prvKey);
  18350. prvKey = NULL;
  18351. if (x509 == NULL) {
  18352. ret = -9002;
  18353. goto openssl_pkey1_test_done;
  18354. }
  18355. pubKey = X509_get_pubkey(x509);
  18356. if (pubKey == NULL) {
  18357. ret = -9003;
  18358. goto openssl_pkey1_test_done;
  18359. }
  18360. prvKey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &clikey, cliKeySz);
  18361. if (prvKey == NULL) {
  18362. ret = -9004;
  18363. goto openssl_pkey1_test_done;
  18364. }
  18365. /* phase 2 API to create EVP_PKEY_CTX and encrypt/decrypt */
  18366. if (EVP_PKEY_bits(prvKey) != keyLenBits) {
  18367. ret = -9005;
  18368. goto openssl_pkey1_test_done;
  18369. }
  18370. if (EVP_PKEY_size(prvKey) != keyLenBits/8) {
  18371. ret = -9006;
  18372. goto openssl_pkey1_test_done;
  18373. }
  18374. dec = EVP_PKEY_CTX_new(prvKey, NULL);
  18375. enc = EVP_PKEY_CTX_new(pubKey, NULL);
  18376. if (dec == NULL || enc == NULL) {
  18377. ret = -9007;
  18378. goto openssl_pkey1_test_done;
  18379. }
  18380. if (EVP_PKEY_decrypt_init(dec) != 1) {
  18381. ret = -9008;
  18382. goto openssl_pkey1_test_done;
  18383. }
  18384. if (EVP_PKEY_encrypt_init(enc) != 1) {
  18385. ret = -9009;
  18386. goto openssl_pkey1_test_done;
  18387. }
  18388. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  18389. ret = -9010;
  18390. goto openssl_pkey1_test_done;
  18391. }
  18392. #ifndef HAVE_FIPS
  18393. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  18394. ret = -9011;
  18395. goto openssl_pkey1_test_done;
  18396. }
  18397. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  18398. ret = -9012;
  18399. goto openssl_pkey1_test_done;
  18400. }
  18401. #endif
  18402. XMEMSET(cipher, 0, RSA_TEST_BYTES);
  18403. outlen = keyLenBits/8;
  18404. if (EVP_PKEY_encrypt(enc, cipher, &outlen, msg, sizeof(msg)) < 0) {
  18405. ret = -9013;
  18406. goto openssl_pkey1_test_done;
  18407. }
  18408. XMEMSET(plain, 0, RSA_TEST_BYTES);
  18409. if (EVP_PKEY_decrypt(dec, plain, &outlen, cipher, outlen) != 1) {
  18410. ret = -9014;
  18411. goto openssl_pkey1_test_done;
  18412. }
  18413. openssl_pkey1_test_done:
  18414. if (pubKey != NULL) {
  18415. EVP_PKEY_free(pubKey);
  18416. }
  18417. if (prvKey != NULL) {
  18418. EVP_PKEY_free(prvKey);
  18419. }
  18420. if (dec != NULL) {
  18421. EVP_PKEY_CTX_free(dec);
  18422. }
  18423. if (enc != NULL) {
  18424. EVP_PKEY_CTX_free(enc);
  18425. }
  18426. if (x509 != NULL) {
  18427. X509_free(x509);
  18428. }
  18429. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18430. if (tmp != NULL)
  18431. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18432. if (cipher != NULL)
  18433. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18434. if (plain != NULL)
  18435. XFREE(plain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18436. #endif
  18437. #endif
  18438. return ret;
  18439. }
  18440. #define ERR_BASE_EVPSIG (-5100)
  18441. WOLFSSL_TEST_SUBROUTINE int openssl_evpSig_test(void)
  18442. {
  18443. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(HAVE_USER_RSA)
  18444. byte* prvTmp;
  18445. byte* pubTmp;
  18446. int prvBytes;
  18447. int pubBytes;
  18448. RSA *prvRsa;
  18449. RSA *pubRsa;
  18450. EVP_PKEY *prvPkey;
  18451. EVP_PKEY *pubPkey;
  18452. EVP_MD_CTX* sign;
  18453. EVP_MD_CTX* verf;
  18454. char msg[] = "see spot run";
  18455. unsigned char sig[256];
  18456. unsigned int sigSz;
  18457. const void* pt;
  18458. unsigned int count;
  18459. int ret, ret1, ret2;
  18460. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  18461. XFILE keyFile;
  18462. XFILE keypubFile;
  18463. char cliKey[] = "./certs/client-key.der";
  18464. char cliKeypub[] = "./certs/client-keyPub.der";
  18465. #endif
  18466. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18467. if (prvTmp == NULL)
  18468. return ERR_BASE_EVPSIG-1;
  18469. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18470. if (pubTmp == NULL) {
  18471. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18472. return ERR_BASE_EVPSIG-2;
  18473. }
  18474. #ifdef USE_CERT_BUFFERS_1024
  18475. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  18476. prvBytes = sizeof_client_key_der_1024;
  18477. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  18478. pubBytes = sizeof_client_keypub_der_1024;
  18479. #elif defined(USE_CERT_BUFFERS_2048)
  18480. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  18481. prvBytes = sizeof_client_key_der_2048;
  18482. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  18483. pubBytes = sizeof_client_keypub_der_2048;
  18484. #else
  18485. keyFile = XFOPEN(cliKey, "rb");
  18486. if (!keyFile) {
  18487. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18488. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18489. err_sys("can't open ./certs/client-key.der, "
  18490. "Please run from wolfSSL home dir", -40);
  18491. return ERR_BASE_EVPSIG-3;
  18492. }
  18493. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  18494. XFCLOSE(keyFile);
  18495. keypubFile = XFOPEN(cliKeypub, "rb");
  18496. if (!keypubFile) {
  18497. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18498. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18499. err_sys("can't open ./certs/client-cert.der, "
  18500. "Please run from wolfSSL home dir", -41);
  18501. return ERR_BASE_EVPSIG-4;
  18502. }
  18503. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  18504. XFCLOSE(keypubFile);
  18505. #endif /* USE_CERT_BUFFERS */
  18506. prvRsa = wolfSSL_RSA_new();
  18507. pubRsa = wolfSSL_RSA_new();
  18508. if((prvRsa == NULL) || (pubRsa == NULL)){
  18509. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18510. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18511. err_sys("ERROR with RSA_new", -9100);
  18512. return ERR_BASE_EVPSIG-5;
  18513. }
  18514. ret1 = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  18515. ret2 = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  18516. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  18517. printf("error with RSA_LoadDer_ex\n");
  18518. return ERR_BASE_EVPSIG-6;
  18519. }
  18520. prvPkey = wolfSSL_EVP_PKEY_new();
  18521. pubPkey = wolfSSL_EVP_PKEY_new();
  18522. if((prvPkey == NULL) || (pubPkey == NULL)){
  18523. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18524. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18525. printf("error with KEY_new\n");
  18526. return ERR_BASE_EVPSIG-7;
  18527. }
  18528. ret1 = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  18529. ret2 = wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  18530. if((ret1 != 1) || (ret2 != 1)){
  18531. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18532. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18533. printf("error with EVP_PKEY_set1_RSA\n");
  18534. return ERR_BASE_EVPSIG-8;
  18535. }
  18536. /****************** sign and verify *******************/
  18537. sign = EVP_MD_CTX_create();
  18538. verf = EVP_MD_CTX_create();
  18539. if((sign == NULL)||(verf == NULL)){
  18540. printf("error with EVP_MD_CTX_create\n");
  18541. EVP_MD_CTX_destroy(sign);
  18542. EVP_MD_CTX_destroy(verf);
  18543. return ERR_BASE_EVPSIG-10;
  18544. }
  18545. ret = EVP_SignInit(sign, EVP_sha1());
  18546. if (ret != SSL_SUCCESS){
  18547. printf("error with EVP_SignInit\n");
  18548. EVP_MD_CTX_destroy(sign);
  18549. EVP_MD_CTX_destroy(verf);
  18550. return ERR_BASE_EVPSIG-11;
  18551. }
  18552. count = sizeof(msg);
  18553. show("message = ", (char *)msg, count);
  18554. /* sign */
  18555. XMEMSET(sig, 0, sizeof(sig));
  18556. pt = (const void*)msg;
  18557. ret1 = EVP_SignUpdate(sign, pt, count);
  18558. ret2 = EVP_SignFinal(sign, sig, &sigSz, prvPkey);
  18559. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  18560. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18561. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18562. EVP_MD_CTX_destroy(sign);
  18563. EVP_MD_CTX_destroy(verf);
  18564. printf("error with EVP_MD_CTX_create\n");
  18565. return ERR_BASE_EVPSIG-12;
  18566. }
  18567. show("signature = ", (char *)sig, sigSz);
  18568. /* verify */
  18569. pt = (const void*)msg;
  18570. ret1 = EVP_VerifyInit(verf, EVP_sha1());
  18571. ret2 = EVP_VerifyUpdate(verf, pt, count);
  18572. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  18573. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18574. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18575. EVP_MD_CTX_destroy(sign);
  18576. EVP_MD_CTX_destroy(verf);
  18577. printf("error with EVP_Verify\n");
  18578. return ERR_BASE_EVPSIG-13;
  18579. }
  18580. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) != 1) {
  18581. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18582. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18583. EVP_MD_CTX_destroy(sign);
  18584. EVP_MD_CTX_destroy(verf);
  18585. printf("error with EVP_VerifyFinal\n");
  18586. return ERR_BASE_EVPSIG-14;
  18587. }
  18588. /* expect fail without update */
  18589. EVP_VerifyInit(verf, EVP_sha1());
  18590. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) == 1) {
  18591. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18592. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18593. EVP_MD_CTX_destroy(sign);
  18594. EVP_MD_CTX_destroy(verf);
  18595. printf("EVP_VerifyInit without update not detected\n");
  18596. return ERR_BASE_EVPSIG-15;
  18597. }
  18598. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18599. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18600. EVP_MD_CTX_destroy(sign);
  18601. EVP_MD_CTX_destroy(verf);
  18602. wolfSSL_RSA_free(prvRsa);
  18603. wolfSSL_RSA_free(pubRsa);
  18604. EVP_PKEY_free(pubPkey);
  18605. EVP_PKEY_free(prvPkey);
  18606. #endif /* NO_RSA */
  18607. return 0;
  18608. }
  18609. #endif /* OPENSSL_EXTRA */
  18610. #ifndef NO_PWDBASED
  18611. #ifdef HAVE_SCRYPT
  18612. /* Test vectors taken from RFC 7914: scrypt PBKDF - Section 12. */
  18613. WOLFSSL_TEST_SUBROUTINE int scrypt_test(void)
  18614. {
  18615. #ifdef HAVE_FIPS
  18616. /* RFC 7914 test vector keys are too short for FIPS. */
  18617. #else
  18618. int ret;
  18619. byte derived[64];
  18620. WOLFSSL_SMALL_STACK_STATIC const byte verify1[] = {
  18621. 0x77, 0xd6, 0x57, 0x62, 0x38, 0x65, 0x7b, 0x20,
  18622. 0x3b, 0x19, 0xca, 0x42, 0xc1, 0x8a, 0x04, 0x97,
  18623. 0xf1, 0x6b, 0x48, 0x44, 0xe3, 0x07, 0x4a, 0xe8,
  18624. 0xdf, 0xdf, 0xfa, 0x3f, 0xed, 0xe2, 0x14, 0x42,
  18625. 0xfc, 0xd0, 0x06, 0x9d, 0xed, 0x09, 0x48, 0xf8,
  18626. 0x32, 0x6a, 0x75, 0x3a, 0x0f, 0xc8, 0x1f, 0x17,
  18627. 0xe8, 0xd3, 0xe0, 0xfb, 0x2e, 0x0d, 0x36, 0x28,
  18628. 0xcf, 0x35, 0xe2, 0x0c, 0x38, 0xd1, 0x89, 0x06
  18629. };
  18630. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  18631. 0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00,
  18632. 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe,
  18633. 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30,
  18634. 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62,
  18635. 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88,
  18636. 0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda,
  18637. 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d,
  18638. 0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40
  18639. };
  18640. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  18641. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  18642. 0x70, 0x23, 0xbd, 0xcb, 0x3a, 0xfd, 0x73, 0x48,
  18643. 0x46, 0x1c, 0x06, 0xcd, 0x81, 0xfd, 0x38, 0xeb,
  18644. 0xfd, 0xa8, 0xfb, 0xba, 0x90, 0x4f, 0x8e, 0x3e,
  18645. 0xa9, 0xb5, 0x43, 0xf6, 0x54, 0x5d, 0xa1, 0xf2,
  18646. 0xd5, 0x43, 0x29, 0x55, 0x61, 0x3f, 0x0f, 0xcf,
  18647. 0x62, 0xd4, 0x97, 0x05, 0x24, 0x2a, 0x9a, 0xf9,
  18648. 0xe6, 0x1e, 0x85, 0xdc, 0x0d, 0x65, 0x1e, 0x40,
  18649. 0xdf, 0xcf, 0x01, 0x7b, 0x45, 0x57, 0x58, 0x87
  18650. };
  18651. #endif
  18652. #ifdef SCRYPT_TEST_ALL
  18653. /* Test case is very slow.
  18654. * Use for confirmation after code change or new platform.
  18655. */
  18656. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  18657. 0x21, 0x01, 0xcb, 0x9b, 0x6a, 0x51, 0x1a, 0xae,
  18658. 0xad, 0xdb, 0xbe, 0x09, 0xcf, 0x70, 0xf8, 0x81,
  18659. 0xec, 0x56, 0x8d, 0x57, 0x4a, 0x2f, 0xfd, 0x4d,
  18660. 0xab, 0xe5, 0xee, 0x98, 0x20, 0xad, 0xaa, 0x47,
  18661. 0x8e, 0x56, 0xfd, 0x8f, 0x4b, 0xa5, 0xd0, 0x9f,
  18662. 0xfa, 0x1c, 0x6d, 0x92, 0x7c, 0x40, 0xf4, 0xc3,
  18663. 0x37, 0x30, 0x40, 0x49, 0xe8, 0xa9, 0x52, 0xfb,
  18664. 0xcb, 0xf4, 0x5c, 0x6f, 0xa7, 0x7a, 0x41, 0xa4
  18665. };
  18666. #endif
  18667. ret = wc_scrypt(derived, NULL, 0, NULL, 0, 4, 1, 1, sizeof(verify1));
  18668. if (ret != 0)
  18669. return -9200;
  18670. if (XMEMCMP(derived, verify1, sizeof(verify1)) != 0)
  18671. return -9201;
  18672. ret = wc_scrypt(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 10, 8, 16,
  18673. sizeof(verify2));
  18674. if (ret != 0)
  18675. return -9202;
  18676. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  18677. return -9203;
  18678. /* Test case with parallel overflowing */
  18679. ret = wc_scrypt(derived, (byte*)"password", 16, (byte*)"NaCl", 16, 2, 4, 8388608,
  18680. sizeof(verify2));
  18681. if (ret != BAD_FUNC_ARG)
  18682. return -9210;
  18683. /* Don't run these test on embedded, since they use large mallocs */
  18684. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  18685. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  18686. (byte*)"SodiumChloride", 14, 14, 8, 1, sizeof(verify3));
  18687. if (ret != 0)
  18688. return -9204;
  18689. if (XMEMCMP(derived, verify3, sizeof(verify3)) != 0)
  18690. return -9205;
  18691. #ifdef SCRYPT_TEST_ALL
  18692. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  18693. (byte*)"SodiumChloride", 14, 20, 8, 1, sizeof(verify4));
  18694. if (ret != 0)
  18695. return -9206;
  18696. if (XMEMCMP(derived, verify4, sizeof(verify4)) != 0)
  18697. return -9207;
  18698. #endif
  18699. #endif /* !BENCH_EMBEDDED && !defined(WOLFSSL_LINUXKM) && !HAVE_INTEL_QA */
  18700. ret = wc_scrypt_ex(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 1<<10,
  18701. 8, 16, sizeof(verify2));
  18702. if (ret != 0)
  18703. return -9208;
  18704. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  18705. return -9209;
  18706. #endif /* !HAVE_FIPS */
  18707. return 0;
  18708. }
  18709. #endif
  18710. #ifdef HAVE_PKCS12
  18711. WOLFSSL_TEST_SUBROUTINE int pkcs12_test(void)
  18712. {
  18713. WOLFSSL_SMALL_STACK_STATIC const byte passwd[] = { 0x00, 0x73, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x67,
  18714. 0x00, 0x00 };
  18715. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x0a, 0x58, 0xCF, 0x64, 0x53, 0x0d, 0x82, 0x3f };
  18716. WOLFSSL_SMALL_STACK_STATIC const byte passwd2[] = { 0x00, 0x71, 0x00, 0x75, 0x00, 0x65, 0x00, 0x65,
  18717. 0x00, 0x67, 0x00, 0x00 };
  18718. WOLFSSL_SMALL_STACK_STATIC const byte salt2[] = { 0x16, 0x82, 0xC0, 0xfC, 0x5b, 0x3f, 0x7e, 0xc5 };
  18719. byte derived[64];
  18720. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  18721. 0x27, 0xE9, 0x0D, 0x7E, 0xD5, 0xA1, 0xC4, 0x11,
  18722. 0xBA, 0x87, 0x8B, 0xC0, 0x90, 0xF5, 0xCE, 0xBE,
  18723. 0x5E, 0x9D, 0x5F, 0xE3, 0xD6, 0x2B, 0x73, 0xAA
  18724. };
  18725. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  18726. 0x90, 0x1B, 0x49, 0x70, 0xF0, 0x94, 0xF0, 0xF8,
  18727. 0x45, 0xC0, 0xF3, 0xF3, 0x13, 0x59, 0x18, 0x6A,
  18728. 0x35, 0xE3, 0x67, 0xFE, 0xD3, 0x21, 0xFD, 0x7C
  18729. };
  18730. int id = 1;
  18731. int kLen = 24;
  18732. int iterations = 1;
  18733. int ret = wc_PKCS12_PBKDF(derived, passwd, sizeof(passwd), salt, 8,
  18734. iterations, kLen, WC_SHA256, id);
  18735. if (ret < 0)
  18736. return -9300;
  18737. if (XMEMCMP(derived, verify, kLen) != 0)
  18738. return -9301;
  18739. iterations = 1000;
  18740. ret = wc_PKCS12_PBKDF(derived, passwd2, sizeof(passwd2), salt2, 8,
  18741. iterations, kLen, WC_SHA256, id);
  18742. if (ret < 0)
  18743. return -9302;
  18744. ret = wc_PKCS12_PBKDF_ex(derived, passwd2, sizeof(passwd2), salt2, 8,
  18745. iterations, kLen, WC_SHA256, id, HEAP_HINT);
  18746. if (ret < 0)
  18747. return -9303;
  18748. if (XMEMCMP(derived, verify2, 24) != 0)
  18749. return -9304;
  18750. return 0;
  18751. }
  18752. #endif /* HAVE_PKCS12 */
  18753. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  18754. WOLFSSL_TEST_SUBROUTINE int pbkdf2_test(void)
  18755. {
  18756. char passwd[] = "passwordpassword";
  18757. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  18758. int iterations = 2048;
  18759. int kLen = 24;
  18760. byte derived[64];
  18761. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  18762. 0x43, 0x6d, 0xb5, 0xe8, 0xd0, 0xfb, 0x3f, 0x35, 0x42, 0x48, 0x39, 0xbc,
  18763. 0x2d, 0xd4, 0xf9, 0x37, 0xd4, 0x95, 0x16, 0xa7, 0x2a, 0x9a, 0x21, 0xd1
  18764. };
  18765. int ret = wc_PBKDF2_ex(derived, (byte*)passwd, (int)XSTRLEN(passwd), salt,
  18766. (int)sizeof(salt), iterations, kLen, WC_SHA256, HEAP_HINT, devId);
  18767. if (ret != 0)
  18768. return ret;
  18769. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  18770. return -9400;
  18771. return 0;
  18772. }
  18773. #endif /* HAVE_PBKDF2 && !NO_SHA256 && !NO_HMAC */
  18774. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  18775. WOLFSSL_TEST_SUBROUTINE int pbkdf1_test(void)
  18776. {
  18777. char passwd[] = "password";
  18778. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  18779. int iterations = 1000;
  18780. int kLen = 16;
  18781. byte derived[16];
  18782. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  18783. 0xDC, 0x19, 0x84, 0x7E, 0x05, 0xC6, 0x4D, 0x2F,
  18784. 0xAF, 0x10, 0xEB, 0xFB, 0x4A, 0x3D, 0x2A, 0x20
  18785. };
  18786. int ret = wc_PBKDF1_ex(derived, kLen, NULL, 0, (byte*)passwd,
  18787. (int)XSTRLEN(passwd), salt, (int)sizeof(salt), iterations, WC_SHA,
  18788. HEAP_HINT);
  18789. if (ret != 0)
  18790. return ret;
  18791. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  18792. return -9500;
  18793. return 0;
  18794. }
  18795. #endif /* HAVE_PBKDF2 && !NO_SHA */
  18796. WOLFSSL_TEST_SUBROUTINE int pwdbased_test(void)
  18797. {
  18798. int ret = 0;
  18799. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  18800. ret = pbkdf1_test();
  18801. if (ret != 0)
  18802. return ret;
  18803. #endif
  18804. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  18805. ret = pbkdf2_test();
  18806. if (ret != 0)
  18807. return ret;
  18808. #endif
  18809. #ifdef HAVE_PKCS12
  18810. ret = pkcs12_test();
  18811. if (ret != 0)
  18812. return ret;
  18813. #endif
  18814. #ifdef HAVE_SCRYPT
  18815. ret = scrypt_test();
  18816. if (ret != 0)
  18817. return ret;
  18818. #endif
  18819. return ret;
  18820. }
  18821. #endif /* NO_PWDBASED */
  18822. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  18823. /* hkdf_test has issue with WOLFSSL_TEST_SUBROUTINE set on Xilinx with afalg */
  18824. static int hkdf_test(void)
  18825. {
  18826. int ret = 0;
  18827. #if !defined(NO_SHA) || !defined(NO_SHA256)
  18828. int L;
  18829. byte okm1[42];
  18830. byte ikm1[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  18831. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  18832. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  18833. #ifndef HAVE_FIPS
  18834. byte salt1[13] ={ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18835. 0x08, 0x09, 0x0a, 0x0b, 0x0c };
  18836. byte info1[10] ={ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  18837. 0xf8, 0xf9 };
  18838. #endif
  18839. #ifndef NO_SHA
  18840. byte res1[42] = { 0x0a, 0xc1, 0xaf, 0x70, 0x02, 0xb3, 0xd7, 0x61,
  18841. 0xd1, 0xe5, 0x52, 0x98, 0xda, 0x9d, 0x05, 0x06,
  18842. 0xb9, 0xae, 0x52, 0x05, 0x72, 0x20, 0xa3, 0x06,
  18843. 0xe0, 0x7b, 0x6b, 0x87, 0xe8, 0xdf, 0x21, 0xd0,
  18844. 0xea, 0x00, 0x03, 0x3d, 0xe0, 0x39, 0x84, 0xd3,
  18845. 0x49, 0x18 };
  18846. #ifndef HAVE_FIPS
  18847. byte res2[42] = { 0x08, 0x5a, 0x01, 0xea, 0x1b, 0x10, 0xf3, 0x69,
  18848. 0x33, 0x06, 0x8b, 0x56, 0xef, 0xa5, 0xad, 0x81,
  18849. 0xa4, 0xf1, 0x4b, 0x82, 0x2f, 0x5b, 0x09, 0x15,
  18850. 0x68, 0xa9, 0xcd, 0xd4, 0xf1, 0x55, 0xfd, 0xa2,
  18851. 0xc2, 0x2e, 0x42, 0x24, 0x78, 0xd3, 0x05, 0xf3,
  18852. 0xf8, 0x96 };
  18853. #endif
  18854. #endif /* !NO_SHA */
  18855. #ifndef NO_SHA256
  18856. byte res3[42] = { 0x8d, 0xa4, 0xe7, 0x75, 0xa5, 0x63, 0xc1, 0x8f,
  18857. 0x71, 0x5f, 0x80, 0x2a, 0x06, 0x3c, 0x5a, 0x31,
  18858. 0xb8, 0xa1, 0x1f, 0x5c, 0x5e, 0xe1, 0x87, 0x9e,
  18859. 0xc3, 0x45, 0x4e, 0x5f, 0x3c, 0x73, 0x8d, 0x2d,
  18860. 0x9d, 0x20, 0x13, 0x95, 0xfa, 0xa4, 0xb6, 0x1a,
  18861. 0x96, 0xc8 };
  18862. #ifndef HAVE_FIPS
  18863. byte res4[42] = { 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a,
  18864. 0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a,
  18865. 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c,
  18866. 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf,
  18867. 0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18,
  18868. 0x58, 0x65 };
  18869. #endif
  18870. #endif /* !NO_SHA256 */
  18871. XMEMSET(okm1, 0, sizeof(okm1));
  18872. L = (int)sizeof(okm1);
  18873. #ifndef NO_SHA
  18874. ret = wc_HKDF(WC_SHA, ikm1, (word32)sizeof(ikm1), NULL, 0, NULL, 0,
  18875. okm1, L);
  18876. if (ret != 0)
  18877. return -9700;
  18878. if (XMEMCMP(okm1, res1, L) != 0)
  18879. return -9701;
  18880. #ifndef HAVE_FIPS
  18881. /* fips can't have key size under 14 bytes, salt is key too */
  18882. L = (int)sizeof(okm1);
  18883. ret = wc_HKDF(WC_SHA, ikm1, 11, salt1, (word32)sizeof(salt1),
  18884. info1, (word32)sizeof(info1), okm1, L);
  18885. if (ret != 0)
  18886. return -9702;
  18887. if (XMEMCMP(okm1, res2, L) != 0)
  18888. return -9703;
  18889. #endif /* HAVE_FIPS */
  18890. #endif /* !NO_SHA */
  18891. #ifndef NO_SHA256
  18892. ret = wc_HKDF(WC_SHA256, ikm1, (word32)sizeof(ikm1), NULL, 0, NULL, 0,
  18893. okm1, L);
  18894. if (ret != 0)
  18895. return -9704;
  18896. if (XMEMCMP(okm1, res3, L) != 0)
  18897. return -9705;
  18898. #ifndef HAVE_FIPS
  18899. /* fips can't have key size under 14 bytes, salt is key too */
  18900. ret = wc_HKDF(WC_SHA256, ikm1, (word32)sizeof(ikm1),
  18901. salt1, (word32)sizeof(salt1), info1, (word32)sizeof(info1), okm1, L);
  18902. if (ret != 0)
  18903. return -9706;
  18904. if (XMEMCMP(okm1, res4, L) != 0)
  18905. return -9707;
  18906. #endif /* HAVE_FIPS */
  18907. #endif /* !NO_SHA256 */
  18908. #endif /* !NO_SHA || !NO_SHA256 */
  18909. return ret;
  18910. }
  18911. #endif /* HAVE_HKDF */
  18912. #ifdef WOLFSSL_WOLFSSH
  18913. typedef struct {
  18914. byte hashId;
  18915. byte keyId;
  18916. const byte* k;
  18917. word32 kSz;
  18918. const byte* h;
  18919. word32 hSz;
  18920. const byte* sessionId;
  18921. word32 sessionIdSz;
  18922. const byte* expectedKey;
  18923. word32 expectedKeySz;
  18924. } SshKdfTestVector;
  18925. /** Test Vector Set #3: SHA-256 **/
  18926. static const byte sshKdfTvSet3k[] = {
  18927. 0x6A, 0xC3, 0x82, 0xEA, 0xAC, 0xA0, 0x93, 0xE1,
  18928. 0x25, 0xE2, 0x5C, 0x24, 0xBE, 0xBC, 0x84, 0x64,
  18929. 0x0C, 0x11, 0x98, 0x75, 0x07, 0x34, 0x4B, 0x5C,
  18930. 0x73, 0x9C, 0xEB, 0x84, 0xA9, 0xE0, 0xB2, 0x22,
  18931. 0xB9, 0xA8, 0xB5, 0x1C, 0x83, 0x9E, 0x5E, 0xBE,
  18932. 0x49, 0xCF, 0xAD, 0xBF, 0xB3, 0x95, 0x99, 0x76,
  18933. 0x4E, 0xD5, 0x22, 0x09, 0x9D, 0xC9, 0x12, 0x75,
  18934. 0x19, 0x50, 0xDC, 0x7D, 0xC9, 0x7F, 0xBD, 0xC0,
  18935. 0x63, 0x28, 0xB6, 0x8F, 0x22, 0x78, 0x1F, 0xD3,
  18936. 0x15, 0xAF, 0x56, 0x80, 0x09, 0xA5, 0x50, 0x9E,
  18937. 0x5B, 0x87, 0xA1, 0x1B, 0xF5, 0x27, 0xC0, 0x56,
  18938. 0xDA, 0xFF, 0xD8, 0x2A, 0xB6, 0xCB, 0xC2, 0x5C,
  18939. 0xCA, 0x37, 0x14, 0x34, 0x59, 0xE7, 0xBC, 0x63,
  18940. 0xBC, 0xDE, 0x52, 0x75, 0x7A, 0xDE, 0xB7, 0xDF,
  18941. 0x01, 0xCF, 0x12, 0x17, 0x3F, 0x1F, 0xEF, 0x81,
  18942. 0x02, 0xEC, 0x5A, 0xB1, 0x42, 0xC2, 0x13, 0xDD,
  18943. 0x9D, 0x30, 0x69, 0x62, 0x78, 0xA8, 0xD8, 0xBC,
  18944. 0x32, 0xDD, 0xE9, 0x59, 0x2D, 0x28, 0xC0, 0x78,
  18945. 0xC6, 0xD9, 0x2B, 0x94, 0x7D, 0x82, 0x5A, 0xCA,
  18946. 0xAB, 0x64, 0x94, 0x84, 0x6A, 0x49, 0xDE, 0x24,
  18947. 0xB9, 0x62, 0x3F, 0x48, 0x89, 0xE8, 0xAD, 0xC3,
  18948. 0x8E, 0x8C, 0x66, 0x9E, 0xFF, 0xEF, 0x17, 0x60,
  18949. 0x40, 0xAD, 0x94, 0x5E, 0x90, 0xA7, 0xD3, 0xEE,
  18950. 0xC1, 0x5E, 0xFE, 0xEE, 0x78, 0xAE, 0x71, 0x04,
  18951. 0x3C, 0x96, 0x51, 0x11, 0x03, 0xA1, 0x6B, 0xA7,
  18952. 0xCA, 0xF0, 0xAC, 0xD0, 0x64, 0x2E, 0xFD, 0xBE,
  18953. 0x80, 0x99, 0x34, 0xFA, 0xA1, 0xA5, 0xF1, 0xBD,
  18954. 0x11, 0x04, 0x36, 0x49, 0xB2, 0x5C, 0xCD, 0x1F,
  18955. 0xEE, 0x2E, 0x38, 0x81, 0x5D, 0x4D, 0x5F, 0x5F,
  18956. 0xC6, 0xB4, 0x10, 0x29, 0x69, 0xF2, 0x1C, 0x22,
  18957. 0xAE, 0x1B, 0x0E, 0x7D, 0x36, 0x03, 0xA5, 0x56,
  18958. 0xA1, 0x32, 0x62, 0xFF, 0x62, 0x8D, 0xE2, 0x22
  18959. };
  18960. static const byte sshKdfTvSet3h[] = {
  18961. 0x7B, 0x70, 0x01, 0x18, 0x5E, 0x25, 0x6D, 0x44,
  18962. 0x93, 0x44, 0x5F, 0x39, 0xA5, 0x5F, 0xB9, 0x05,
  18963. 0xE6, 0x32, 0x1F, 0x4B, 0x5D, 0xD8, 0xBB, 0xF3,
  18964. 0x10, 0x0D, 0x51, 0xBA, 0x0B, 0xDA, 0x3D, 0x2D
  18965. };
  18966. static const byte sshKdfTvSet3sid[] = {
  18967. 0x7B, 0x70, 0x01, 0x18, 0x5E, 0x25, 0x6D, 0x44,
  18968. 0x93, 0x44, 0x5F, 0x39, 0xA5, 0x5F, 0xB9, 0x05,
  18969. 0xE6, 0x32, 0x1F, 0x4B, 0x5D, 0xD8, 0xBB, 0xF3,
  18970. 0x10, 0x0D, 0x51, 0xBA, 0x0B, 0xDA, 0x3D, 0x2D
  18971. };
  18972. static const byte sshKdfTvSet3a[] = {
  18973. 0x81, 0xF0, 0x33, 0x0E, 0xF6, 0xF0, 0x53, 0x61,
  18974. 0xB3, 0x82, 0x3B, 0xFD, 0xED, 0x6E, 0x1D, 0xE9
  18975. };
  18976. static const byte sshKdfTvSet3b[] = {
  18977. 0x3F, 0x6F, 0xD2, 0x06, 0x5E, 0xEB, 0x2B, 0x0B,
  18978. 0x1D, 0x93, 0x19, 0x5A, 0x1F, 0xED, 0x48, 0xA5
  18979. };
  18980. static const byte sshKdfTvSet3c[] = {
  18981. 0xC3, 0x54, 0x71, 0x03, 0x4E, 0x6F, 0xD6, 0x54,
  18982. 0x76, 0x13, 0x17, 0x8E, 0x23, 0x43, 0x5F, 0x21
  18983. };
  18984. static const byte sshKdfTvSet3d[] = {
  18985. 0x7E, 0x9D, 0x79, 0x03, 0x20, 0x90, 0xD9, 0x9F,
  18986. 0x98, 0xB0, 0x15, 0x63, 0x4D, 0xD9, 0xF4, 0x62
  18987. };
  18988. static const byte sshKdfTvSet3e[] = {
  18989. 0x24, 0xEE, 0x55, 0x9A, 0xD7, 0xCE, 0x71, 0x2B,
  18990. 0x68, 0x5D, 0x0B, 0x22, 0x71, 0xE4, 0x43, 0xC1,
  18991. 0x7A, 0xB1, 0xD1, 0xDC, 0xEB, 0x5A, 0x36, 0x05,
  18992. 0x69, 0xD2, 0x5D, 0x5D, 0xC2, 0x43, 0x00, 0x2F
  18993. };
  18994. static const byte sshKdfTvSet3f[] = {
  18995. 0xC3, 0x41, 0x9C, 0x2B, 0x96, 0x62, 0x35, 0x86,
  18996. 0x9D, 0x71, 0x4B, 0xA5, 0xAC, 0x48, 0xDD, 0xB7,
  18997. 0xD9, 0xE3, 0x5C, 0x8C, 0x19, 0xAA, 0xC7, 0x34,
  18998. 0x22, 0x33, 0x7A, 0x37, 0x34, 0x53, 0x60, 0x7E
  18999. };
  19000. static const SshKdfTestVector sshKdfTestVectors[] = {
  19001. {WC_HASH_TYPE_SHA256, 'A',
  19002. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  19003. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  19004. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  19005. sshKdfTvSet3a, sizeof(sshKdfTvSet3a)},
  19006. {WC_HASH_TYPE_SHA256, 'B',
  19007. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  19008. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  19009. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  19010. sshKdfTvSet3b, sizeof(sshKdfTvSet3b)},
  19011. {WC_HASH_TYPE_SHA256, 'C',
  19012. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  19013. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  19014. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  19015. sshKdfTvSet3c, sizeof(sshKdfTvSet3c)},
  19016. {WC_HASH_TYPE_SHA256, 'D',
  19017. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  19018. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  19019. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  19020. sshKdfTvSet3d, sizeof(sshKdfTvSet3d)},
  19021. {WC_HASH_TYPE_SHA256, 'E',
  19022. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  19023. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  19024. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  19025. sshKdfTvSet3e, sizeof(sshKdfTvSet3e)},
  19026. {WC_HASH_TYPE_SHA256, 'F',
  19027. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  19028. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  19029. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  19030. sshKdfTvSet3f, sizeof(sshKdfTvSet3f)},
  19031. };
  19032. int sshkdf_test(void)
  19033. {
  19034. int result = 0;
  19035. word32 i;
  19036. word32 tc = sizeof(sshKdfTestVectors)/sizeof(SshKdfTestVector);
  19037. const SshKdfTestVector* tv = NULL;
  19038. byte cKey[32]; /* Greater of SHA256_DIGEST_SIZE and AES_BLOCK_SIZE */
  19039. /* sId - Session ID, eKey - Expected Key, cKey - Calculated Key */
  19040. for (i = 0, tv = sshKdfTestVectors; i < tc; i++, tv++) {
  19041. result = wc_SSH_KDF(tv->hashId, tv->keyId,
  19042. cKey, tv->expectedKeySz,
  19043. tv->k, tv->kSz, tv->h, tv->hSz,
  19044. tv->sessionId, tv->sessionIdSz);
  19045. if (result != 0) {
  19046. printf("KDF: Could not derive key.\n");
  19047. result = -101;
  19048. }
  19049. else {
  19050. if (XMEMCMP(cKey, tv->expectedKey, tv->expectedKeySz) != 0) {
  19051. printf("KDF: Calculated Key does not match Expected Key.\n");
  19052. result = -102;
  19053. }
  19054. }
  19055. if (result != 0) break;
  19056. }
  19057. return result;
  19058. }
  19059. #endif /* WOLFSSL_WOLFSSH */
  19060. #ifdef WOLFSSL_TLS13
  19061. #define TLSV13_PSK_DHE_SZ 40
  19062. typedef struct {
  19063. enum wc_HashType hashAlg;
  19064. word32 pskSz;
  19065. word32 dheSz;
  19066. byte psk[TLSV13_PSK_DHE_SZ];
  19067. byte dhe[TLSV13_PSK_DHE_SZ];
  19068. byte hashHello1[WC_MAX_DIGEST_SIZE];
  19069. byte hashHello2[WC_MAX_DIGEST_SIZE];
  19070. byte hashFinished1[WC_MAX_DIGEST_SIZE];
  19071. byte hashFinished2[WC_MAX_DIGEST_SIZE];
  19072. /* Expected */
  19073. byte clientEarlyTrafficSecret[WC_MAX_DIGEST_SIZE];
  19074. byte earlyExporterMasterSecret[WC_MAX_DIGEST_SIZE];
  19075. byte clientHandshakeTrafficSecret[WC_MAX_DIGEST_SIZE];
  19076. byte serverHandshakeTrafficSecret[WC_MAX_DIGEST_SIZE];
  19077. byte clientApplicationTrafficSecret[WC_MAX_DIGEST_SIZE];
  19078. byte serverApplicationTrafficSecret[WC_MAX_DIGEST_SIZE];
  19079. byte exporterMasterSecret[WC_MAX_DIGEST_SIZE];
  19080. byte resumptionMasterSecret[WC_MAX_DIGEST_SIZE];
  19081. } Tls13KdfTestVector;
  19082. /* The following tests come from the CAVP test vectors we used for
  19083. * our FIPS validation. The hash values used are the components from
  19084. * the test hashed together. hashHello1 is the hash of the
  19085. * clientHelloRandom value of the test vector. hashHello2 is the hash
  19086. * of the clientHelloRandom and serverHelloRandom values from the test
  19087. * vector. hashFinished1 is clientHelloRandom, serverHelloRandom, and
  19088. * serverFinishedRandom. hashFinished2 is clientHelloRandom,
  19089. * serverHelloRandom, serverFinishedRandom, and clietnFinishedRandom
  19090. * hashed together. */
  19091. static const Tls13KdfTestVector tls13KdfTestVectors[] = {
  19092. { /* 1 */
  19093. WC_HASH_TYPE_SHA256, 35, 35,
  19094. { /* PSK */
  19095. 0x7b, 0xf1, 0x05, 0x31, 0x36, 0xfa, 0x03, 0xdc,
  19096. 0x31, 0x97, 0x88, 0x04, 0x9c, 0xbc, 0xee, 0xf7,
  19097. 0x8d, 0x84, 0x95, 0x26, 0xaf, 0x1d, 0x68, 0xb0,
  19098. 0x60, 0x7a, 0xcc, 0x4f, 0xc1, 0xd3, 0xa1, 0x68,
  19099. 0x7f, 0x6d, 0xbe
  19100. },
  19101. { /* DHE */
  19102. 0x6e, 0xa1, 0x77, 0xab, 0x2f, 0x43, 0xd2, 0x4b,
  19103. 0xe5, 0xa1, 0x09, 0xe0, 0x7a, 0xd0, 0x01, 0x35,
  19104. 0x8d, 0xf8, 0xf2, 0x5c, 0x91, 0x02, 0xb0, 0x6c,
  19105. 0x3f, 0xeb, 0xee, 0xa4, 0x42, 0x19, 0xce, 0xdc,
  19106. 0x81, 0x26, 0x40
  19107. },
  19108. { /* Hello 1 */
  19109. 0xd9, 0x4b, 0xe4, 0x17, 0xef, 0x58, 0x73, 0x7d,
  19110. 0x28, 0x3d, 0xf0, 0xcc, 0x05, 0x03, 0xaf, 0xac,
  19111. 0x3d, 0x92, 0x79, 0x48, 0xe8, 0x8c, 0xdb, 0xce,
  19112. 0x95, 0x82, 0x21, 0x31, 0x7b, 0x61, 0xd7, 0xc6
  19113. },
  19114. { /* Hello 2 */
  19115. 0xb7, 0x7f, 0x29, 0x91, 0xa4, 0x8b, 0x34, 0xdb,
  19116. 0xbd, 0xc7, 0x54, 0x1c, 0x3b, 0x86, 0xa3, 0x69,
  19117. 0xfe, 0x26, 0xe4, 0x7b, 0xac, 0x57, 0x71, 0xb3,
  19118. 0x32, 0x97, 0xed, 0xd2, 0x0e, 0x95, 0xb8, 0x63
  19119. },
  19120. { /* Finished 1 */
  19121. 0x65, 0xdb, 0x6d, 0x71, 0x71, 0xd0, 0xd8, 0x49,
  19122. 0xd0, 0x3c, 0x8e, 0x2b, 0x24, 0xdf, 0xc2, 0xe9,
  19123. 0xd6, 0xfd, 0xea, 0x04, 0x95, 0x7c, 0xf0, 0x7e,
  19124. 0x57, 0x74, 0x7c, 0xdd, 0xa3, 0x0b, 0x2b, 0x36
  19125. },
  19126. { /* Finished 2 */
  19127. 0x28, 0xf2, 0xf2, 0x79, 0xcf, 0x20, 0x52, 0x90,
  19128. 0x1d, 0x91, 0x05, 0xad, 0x44, 0x26, 0x23, 0x96,
  19129. 0x32, 0xce, 0xec, 0x61, 0xd1, 0xbf, 0x00, 0x48,
  19130. 0x4a, 0xa5, 0x60, 0xcc, 0x28, 0xb5, 0x8d, 0x98
  19131. },
  19132. { /* Client Early Traffic Secret */
  19133. 0x07, 0x14, 0x6a, 0x26, 0x5b, 0x6c, 0x7f, 0x4d, 0x6b, 0x47, 0x3f, 0xd5,
  19134. 0x03, 0x1d, 0xd2, 0x23, 0x3d, 0x89, 0x3e, 0xc6, 0x51, 0xd1, 0xac, 0xf8,
  19135. 0x28, 0xae, 0x4b, 0x76, 0xc8, 0x10, 0x7e, 0xdd
  19136. },
  19137. { /* Early Exporter Master Secret */
  19138. 0xb8, 0xd3, 0x25, 0x7e, 0x2d, 0x41, 0x7b, 0xcb, 0x5e, 0x82, 0x49, 0xf5,
  19139. 0x51, 0x3d, 0xb7, 0x59, 0x32, 0xb3, 0xdf, 0x99, 0x4e, 0x04, 0x69, 0xc6,
  19140. 0x96, 0x8e, 0xe6, 0x3d, 0x91, 0xe4, 0x81, 0x11
  19141. },
  19142. { /* Client Handshake Traffic Secret */
  19143. 0xd9, 0x3b, 0x54, 0xe2, 0xb0, 0xd1, 0x85, 0xf0, 0xfd, 0xf3, 0x48, 0x4a,
  19144. 0xf8, 0x0b, 0xa5, 0xdc, 0x4c, 0x37, 0xcb, 0xd4, 0x20, 0xaf, 0x60, 0xc7,
  19145. 0xd5, 0x50, 0x5d, 0x0c, 0x77, 0x3b, 0x6f, 0xd2
  19146. },
  19147. { /* Server Handshake Traffic Secret */
  19148. 0x4d, 0x40, 0x2b, 0xd2, 0x8c, 0x33, 0x90, 0x39, 0x67, 0x67, 0x05, 0xf7,
  19149. 0x5d, 0x37, 0x1e, 0xdc, 0x4a, 0x70, 0x6b, 0x9e, 0xf8, 0x06, 0x61, 0x89,
  19150. 0x70, 0xe1, 0x3d, 0x36, 0xad, 0x88, 0x7e, 0x5b
  19151. },
  19152. { /* Client Application Traffic Secret */
  19153. 0x74, 0x6e, 0xa0, 0x13, 0x18, 0x34, 0x48, 0x4d, 0x23, 0x31, 0xf1, 0xf9,
  19154. 0xee, 0x44, 0x6d, 0xad, 0xc1, 0xad, 0x92, 0x73, 0xca, 0x27, 0x16, 0x91,
  19155. 0xa2, 0x50, 0x9a, 0xfc, 0xec, 0xf0, 0x6b, 0x24
  19156. },
  19157. { /* Server Application Traffic Secret */
  19158. 0x89, 0x18, 0x7e, 0x34, 0x8d, 0xfc, 0x14, 0xb1, 0x4f, 0x21, 0xd8, 0x29,
  19159. 0xdb, 0x9b, 0xfb, 0x55, 0xcf, 0xa1, 0x4f, 0x95, 0xf8, 0xe0, 0xb0, 0x83,
  19160. 0xd5, 0x34, 0x9e, 0x0b, 0x83, 0x37, 0x42, 0x93
  19161. },
  19162. { /* Exporter Master Secret */
  19163. 0x7d, 0xc8, 0x88, 0x46, 0xd5, 0x57, 0x15, 0xb6, 0x24, 0x25, 0x92, 0x61,
  19164. 0xb1, 0x18, 0x86, 0x2a, 0x6d, 0xa5, 0x84, 0xeb, 0x59, 0xdf, 0x13, 0xbd,
  19165. 0x73, 0xaa, 0x5d, 0x65, 0xab, 0xd9, 0xb4, 0x56
  19166. },
  19167. { /* Resumption Master Secret */
  19168. 0x20, 0xb7, 0xd0, 0xe3, 0x82, 0x01, 0xa1, 0x04, 0xb8, 0x13, 0x29, 0xed,
  19169. 0x35, 0xe4, 0x2f, 0xbf, 0x58, 0x23, 0x7f, 0x21, 0xdb, 0x9f, 0xf8, 0xe0,
  19170. 0xe8, 0xe4, 0xab, 0xc4, 0xa1, 0x61, 0xb9, 0xbb
  19171. }
  19172. },
  19173. { /* 6 */
  19174. WC_HASH_TYPE_SHA256, 0, 33,
  19175. { 0 }, /* PSK */
  19176. { /* DHE */
  19177. 0x7a, 0x46, 0x8c, 0x5a, 0xd1, 0x8e, 0x95, 0xba,
  19178. 0x61, 0xe6, 0x6f, 0xe6, 0x76, 0x0c, 0x20, 0x43,
  19179. 0x16, 0x82, 0x15, 0xfe, 0x54, 0xa3, 0xc7, 0xfd,
  19180. 0x3b, 0x2c, 0x88, 0xb4, 0xd3, 0x42, 0x70, 0x12,
  19181. 0x18
  19182. },
  19183. { /* Hello 1 */
  19184. 0x63, 0x83, 0x58, 0xab, 0x36, 0xcd, 0x0c, 0xf3,
  19185. 0x26, 0x07, 0xb5, 0x5f, 0x0b, 0x8b, 0x45, 0xd6,
  19186. 0x7d, 0x5b, 0x42, 0xdc, 0xa8, 0xaa, 0x06, 0xfb,
  19187. 0x20, 0xa5, 0xbb, 0x85, 0xdb, 0x54, 0xd8, 0x8b
  19188. },
  19189. { /* Hello 2 */
  19190. 0xea, 0xfe, 0x9e, 0x8e, 0xff, 0x1f, 0x6f, 0x43,
  19191. 0xf9, 0x5d, 0xfd, 0xbf, 0xe2, 0x5f, 0x02, 0x2f,
  19192. 0x6d, 0x47, 0x60, 0x9a, 0x48, 0x9a, 0x75, 0xfb,
  19193. 0xb5, 0x4a, 0xbf, 0x9c, 0x4e, 0xff, 0xbf, 0x0b
  19194. },
  19195. { /* Finished 1 */
  19196. 0xca, 0x25, 0xb3, 0x53, 0x8e, 0x6d, 0xc3, 0x36,
  19197. 0x17, 0x30, 0x07, 0xdf, 0x0d, 0xd7, 0x79, 0xb0,
  19198. 0x7f, 0xcb, 0xbe, 0x7a, 0xbc, 0x2d, 0x9f, 0x2d,
  19199. 0x94, 0x44, 0x94, 0xe6, 0xa4, 0xf3, 0xe8, 0x53
  19200. },
  19201. { /* Finished 2 */
  19202. 0x2e, 0xa6, 0x5a, 0xaf, 0xb5, 0xba, 0x9f, 0x2f,
  19203. 0x74, 0x83, 0x5d, 0xbf, 0x86, 0xa4, 0xa6, 0xf6,
  19204. 0xb9, 0x89, 0xdf, 0x17, 0xe1, 0xa8, 0x14, 0xc0,
  19205. 0xe1, 0x50, 0xfa, 0xec, 0xfa, 0xae, 0x8b, 0x7b
  19206. },
  19207. {
  19208. 0x20, 0x18, 0x72, 0x7c, 0xde, 0x3a, 0x85, 0x17, 0x72, 0xdc, 0xd7, 0x72,
  19209. 0xb0, 0xfc, 0x45, 0xd0, 0x62, 0xb9, 0xbb, 0x38, 0x69, 0x05, 0x7b, 0xb4,
  19210. 0x5e, 0x58, 0x5d, 0xed, 0xcd, 0x0b, 0x96, 0xd3
  19211. },
  19212. {
  19213. 0x68, 0x10, 0x20, 0xd1, 0x5e, 0xfc, 0x0c, 0x53, 0x85, 0xbb, 0xdb, 0x18,
  19214. 0xa8, 0x78, 0xf1, 0x2b, 0x13, 0xba, 0x64, 0x1d, 0xe7, 0x09, 0xbe, 0x13,
  19215. 0x49, 0x26, 0xf9, 0x98, 0x56, 0xf1, 0x43, 0xfb
  19216. },
  19217. {
  19218. 0x24, 0x35, 0x3e, 0x10, 0x6f, 0x39, 0x50, 0xd6, 0xa2, 0x12, 0x99, 0xf2,
  19219. 0xd5, 0xf5, 0x19, 0xf5, 0x84, 0xed, 0xee, 0x78, 0x2a, 0xa6, 0xfa, 0x3d,
  19220. 0x06, 0xa8, 0xa7, 0x5d, 0x97, 0x78, 0xd6, 0x58
  19221. },
  19222. {
  19223. 0xf4, 0x57, 0xac, 0x24, 0x7a, 0xfb, 0x7c, 0x3b, 0xb6, 0x39, 0x17, 0x14,
  19224. 0xd9, 0xd4, 0x58, 0x4d, 0x46, 0xd5, 0x1b, 0xde, 0xf7, 0x9d, 0x06, 0xee,
  19225. 0x8d, 0x1a, 0x2c, 0x25, 0x6d, 0x64, 0xde, 0x89
  19226. },
  19227. {
  19228. 0xb6, 0x00, 0xce, 0x63, 0xed, 0x65, 0x8b, 0x66, 0x66, 0x42, 0xc6, 0xbd,
  19229. 0x89, 0xc4, 0x71, 0x6f, 0xce, 0x28, 0xb2, 0xac, 0x97, 0x07, 0x5b, 0xea,
  19230. 0xb8, 0x1d, 0x4c, 0xeb, 0x9e, 0x71, 0x07, 0x8f
  19231. },
  19232. {
  19233. 0xf8, 0x92, 0xc8, 0xba, 0xe7, 0x83, 0xfe, 0x68, 0xe4, 0xd6, 0x5e, 0xcb,
  19234. 0xb3, 0xef, 0x49, 0xd0, 0xe7, 0xb1, 0xac, 0xcb, 0x39, 0x19, 0xfd, 0xa7,
  19235. 0xf7, 0xca, 0xab, 0x1e, 0x42, 0x14, 0xd8, 0xe7
  19236. },
  19237. {
  19238. 0x32, 0x4a, 0x1a, 0xad, 0xe2, 0xbb, 0x55, 0x8a, 0xdd, 0xe9, 0xa5, 0x2a,
  19239. 0x46, 0x5e, 0x6c, 0x83, 0x66, 0x27, 0x27, 0x94, 0xdd, 0x68, 0x59, 0xa0,
  19240. 0xbb, 0xe8, 0x31, 0x7c, 0x39, 0xd7, 0xfd, 0x6d
  19241. },
  19242. {
  19243. 0x58, 0xbc, 0x6c, 0x5b, 0x24, 0xad, 0x82, 0xb3, 0xcc, 0xc7, 0xd1, 0xa1,
  19244. 0xaa, 0x2b, 0x98, 0x9f, 0x2f, 0x7e, 0xa9, 0x63, 0xc2, 0x8e, 0xb6, 0x06,
  19245. 0xc2, 0x2b, 0x74, 0x4b, 0x79, 0x19, 0x7e, 0x2e
  19246. }
  19247. },
  19248. { /* 11 */
  19249. WC_HASH_TYPE_SHA256, 33, 0,
  19250. { /* PSK */
  19251. 0x3d, 0x39, 0x49, 0x36, 0x98, 0xc5, 0xfd, 0xcd,
  19252. 0xa0, 0x17, 0xbd, 0x65, 0x0a, 0xdb, 0xd4, 0x07,
  19253. 0x56, 0xa2, 0x7b, 0xb8, 0x2a, 0x7e, 0xfb, 0x26,
  19254. 0x74, 0xe1, 0xbc, 0x08, 0x4b, 0xf0, 0x30, 0x14,
  19255. 0x12
  19256. },
  19257. { 0 }, /* DHE */
  19258. { /* Hello 1 */
  19259. 0xb7, 0x44, 0x74, 0x6c, 0x57, 0x1f, 0xf3, 0x84,
  19260. 0x8f, 0x63, 0xfb, 0x8c, 0x94, 0x6c, 0x16, 0x68,
  19261. 0x4b, 0xe1, 0xb5, 0xb5, 0x2a, 0x4e, 0x5f, 0xdf,
  19262. 0x4b, 0x53, 0xb2, 0x35, 0xfc, 0x30, 0xf1, 0x36
  19263. },
  19264. { /* Hello 2 */
  19265. 0xe6, 0x4f, 0x3a, 0x4f, 0xd7, 0xe0, 0x64, 0xd4,
  19266. 0x69, 0x50, 0xe4, 0x8b, 0xba, 0xbc, 0x47, 0x74,
  19267. 0xa7, 0x9b, 0x40, 0x91, 0x8f, 0xa8, 0x72, 0x22,
  19268. 0x97, 0xad, 0x43, 0xa7, 0x11, 0x86, 0xb5, 0x72
  19269. },
  19270. { /* Finished 1 */
  19271. 0x5f, 0xa6, 0x10, 0xe2, 0xa3, 0x99, 0x0b, 0x5e,
  19272. 0x57, 0xee, 0xc3, 0x3a, 0x8e, 0x04, 0xf3, 0x0e,
  19273. 0x58, 0x02, 0x09, 0xb2, 0x7e, 0x2d, 0xc6, 0xd2,
  19274. 0x08, 0xae, 0x68, 0x0a, 0x55, 0xa5, 0xda, 0x51
  19275. },
  19276. { /* Finished 2 */
  19277. 0xfc, 0x5b, 0xc0, 0x7e, 0x1b, 0xaa, 0xc0, 0xb4,
  19278. 0x34, 0x85, 0x49, 0x8e, 0x16, 0x31, 0x98, 0xdf,
  19279. 0x10, 0x54, 0x22, 0xda, 0x1e, 0x6b, 0x51, 0xf6,
  19280. 0x97, 0x57, 0xa0, 0x7a, 0x92, 0xe7, 0x47, 0x52
  19281. },
  19282. {
  19283. 0x80, 0xfa, 0x36, 0x30, 0xb8, 0x65, 0xb3, 0x2a, 0x1d, 0x68, 0x91, 0x06,
  19284. 0x98, 0xa0, 0x17, 0x8f, 0xee, 0xb7, 0x9e, 0x3d, 0xd8, 0x84, 0x99, 0x30,
  19285. 0xb9, 0xd6, 0x09, 0x25, 0x5e, 0xfb, 0x8f, 0xd3 },
  19286. {
  19287. 0xa9, 0x89, 0x29, 0x70, 0xe4, 0x55, 0xec, 0x97, 0xfb, 0x24, 0x5b, 0xf9,
  19288. 0xf1, 0xa3, 0x19, 0x3d, 0xf1, 0x31, 0x14, 0xcd, 0x2a, 0xed, 0x21, 0xc8,
  19289. 0xb1, 0x53, 0xad, 0x11, 0x0b, 0x9e, 0x5a, 0xee },
  19290. {
  19291. 0x72, 0xad, 0x8d, 0x7f, 0xfc, 0xb7, 0x68, 0xda, 0x27, 0x60, 0x37, 0xa3,
  19292. 0x4a, 0x63, 0xe8, 0xa5, 0xc8, 0xcd, 0x36, 0x6a, 0x77, 0x99, 0x0d, 0xa9,
  19293. 0xb1, 0x5b, 0x2f, 0x47, 0x2e, 0x22, 0xa7, 0x5e },
  19294. {
  19295. 0x95, 0x6e, 0x85, 0x09, 0xe5, 0x04, 0x88, 0x14, 0x28, 0x8d, 0xdf, 0xe6,
  19296. 0x0d, 0x0f, 0x0d, 0x6b, 0x4e, 0x66, 0x1c, 0x03, 0xb9, 0xaa, 0x2d, 0x45,
  19297. 0x56, 0x67, 0x5c, 0x55, 0x29, 0xd6, 0x89, 0xd0 },
  19298. {
  19299. 0xe8, 0xf2, 0x14, 0xf9, 0x9b, 0x2b, 0x9f, 0x24, 0x2b, 0x37, 0xbe, 0x86,
  19300. 0xdb, 0x23, 0x4b, 0xbe, 0x39, 0x57, 0xe8, 0xa9, 0xa5, 0xee, 0x08, 0xf2,
  19301. 0x75, 0x58, 0xdb, 0xd9, 0x51, 0xc1, 0x46, 0x02 },
  19302. {
  19303. 0x3d, 0x19, 0xaf, 0xa3, 0x0b, 0x21, 0xf7, 0x3d, 0xe7, 0x37, 0x6e, 0x32,
  19304. 0x13, 0x48, 0x9d, 0xea, 0xe0, 0x90, 0xbf, 0x64, 0x48, 0xf7, 0x1e, 0xcc,
  19305. 0xf0, 0xbc, 0x92, 0xd7, 0x8a, 0x4a, 0xa8, 0xc1 },
  19306. {
  19307. 0x16, 0x35, 0xb1, 0x66, 0x28, 0xa3, 0x3e, 0x19, 0xf5, 0x2d, 0x92, 0x22,
  19308. 0x95, 0x48, 0xe8, 0x34, 0x7b, 0x30, 0x50, 0xa2, 0xa0, 0xd9, 0xc2, 0x59,
  19309. 0x39, 0xf9, 0x8c, 0x69, 0xf2, 0x2a, 0xb9, 0xff },
  19310. {
  19311. 0x32, 0x71, 0xa6, 0x87, 0x0c, 0x97, 0x42, 0x07, 0xdd, 0x5f, 0xc9, 0x44,
  19312. 0xa5, 0x7c, 0x50, 0x14, 0xfd, 0xe7, 0x5f, 0x8b, 0xd3, 0x2f, 0xdc, 0x9b,
  19313. 0xa9, 0x93, 0x22, 0x19, 0xe6, 0xf2, 0x0c, 0xd8 }
  19314. },
  19315. #ifdef WOLFSSL_SHA384
  19316. { /* 26 */
  19317. WC_HASH_TYPE_SHA384, 35, 35,
  19318. { /* PSK */
  19319. 0x62, 0x83, 0x25, 0xc7, 0xcc, 0x08, 0x5e, 0x63,
  19320. 0x64, 0x56, 0xf0, 0xc6, 0x88, 0x27, 0x5a, 0x5b,
  19321. 0x68, 0x59, 0x0b, 0x14, 0x55, 0x13, 0x2e, 0xfd,
  19322. 0x8f, 0x28, 0x5b, 0x3d, 0xe3, 0xad, 0x67, 0xe4,
  19323. 0x68, 0xba, 0xf9
  19324. },
  19325. { /* DHE */
  19326. 0xa8, 0xb1, 0xab, 0xd8, 0xc8, 0x5b, 0x52, 0xdf,
  19327. 0x7f, 0x49, 0x10, 0xf4, 0xa1, 0x31, 0xd1, 0x91,
  19328. 0x36, 0xc1, 0x87, 0x5d, 0x42, 0x2a, 0xe7, 0x1d,
  19329. 0x2c, 0x29, 0x3d, 0x40, 0x64, 0x61, 0x63, 0x76,
  19330. 0xd8, 0x66, 0xac
  19331. },
  19332. { /* Hello 1 */
  19333. 0x6f, 0xc6, 0x4c, 0xe1, 0xc6, 0x68, 0x34, 0x8c,
  19334. 0x0a, 0xe1, 0xf8, 0xb8, 0x3e, 0xd4, 0xf8, 0x0b,
  19335. 0x54, 0x50, 0xe4, 0xc5, 0x4a, 0x33, 0x7d, 0xbd,
  19336. 0x90, 0xd2, 0xa2, 0xb9, 0xb7, 0x92, 0xed, 0xab,
  19337. 0x14, 0xf1, 0xe4, 0x86, 0x22, 0x67, 0xd7, 0x44,
  19338. 0x03, 0x21, 0xdc, 0x51, 0x52, 0x7f, 0x35, 0x80
  19339. },
  19340. { /* Hello 2 */
  19341. 0x3e, 0xcf, 0x2f, 0xc3, 0x87, 0xba, 0xc5, 0xbd,
  19342. 0x7c, 0xe8, 0x35, 0x5b, 0x95, 0x51, 0x30, 0x3b,
  19343. 0x08, 0xcc, 0x2a, 0x7d, 0xb5, 0x74, 0x7c, 0x16,
  19344. 0xb3, 0x0b, 0xe7, 0x61, 0xa3, 0x7c, 0x6c, 0xbd,
  19345. 0x39, 0x74, 0xfd, 0x1e, 0x4c, 0xff, 0xc8, 0xcc,
  19346. 0xa0, 0xef, 0x29, 0x4d, 0x94, 0xaa, 0x55, 0x6f,
  19347. },
  19348. { /* Finished 1 */
  19349. 0x06, 0xc1, 0x47, 0x78, 0x66, 0x53, 0x6f, 0x24,
  19350. 0x94, 0x61, 0x69, 0xec, 0xd8, 0x60, 0x31, 0x2f,
  19351. 0xbf, 0xd6, 0x8a, 0x29, 0x17, 0xff, 0xa3, 0x88,
  19352. 0x13, 0x09, 0x8c, 0x9d, 0x6c, 0x64, 0x84, 0x48,
  19353. 0x44, 0xdd, 0x2d, 0x29, 0x4d, 0xe6, 0x98, 0x2b,
  19354. 0x45, 0x3b, 0x84, 0x33, 0x79, 0xb2, 0x75, 0x68
  19355. },
  19356. { /* Finished 2 */
  19357. 0x28, 0x1e, 0x18, 0xf7, 0x9c, 0x32, 0xa9, 0xbf,
  19358. 0x0c, 0x24, 0x58, 0x21, 0xce, 0xbc, 0xf2, 0x44,
  19359. 0xb1, 0x18, 0xaf, 0x9d, 0xd9, 0x20, 0xf9, 0xf4,
  19360. 0xed, 0xcc, 0x53, 0x82, 0x66, 0x5c, 0x46, 0x94,
  19361. 0x8c, 0x36, 0x5e, 0xca, 0x9f, 0xd8, 0x9a, 0xd3,
  19362. 0xf0, 0xe1, 0x53, 0x71, 0xdd, 0x19, 0x1e, 0x59
  19363. },
  19364. {
  19365. 0xd0, 0xef, 0xa8, 0xcb, 0x5b, 0x14, 0x0f, 0x0a, 0x62, 0xba, 0x5a, 0xb1,
  19366. 0xc5, 0xb5, 0x3f, 0x11, 0xda, 0xa1, 0x0c, 0x9c, 0xb4, 0x32, 0x48, 0x4e,
  19367. 0xfa, 0x84, 0x4f, 0xe4, 0xe7, 0x91, 0x8f, 0x42, 0x3f, 0xc7, 0x4e, 0xd3,
  19368. 0x83, 0x3d, 0x7f, 0x70, 0x12, 0xee, 0x9a, 0x37, 0x01, 0xbb, 0x14, 0xd3
  19369. },
  19370. {
  19371. 0x48, 0x6f, 0x77, 0x1d, 0x39, 0x1b, 0xa5, 0x9a, 0x76, 0xd9, 0x1d, 0x7d,
  19372. 0xb3, 0xd9, 0xb9, 0x78, 0x35, 0x0f, 0xd0, 0xe1, 0x07, 0x1f, 0x8d, 0xe5,
  19373. 0x75, 0x00, 0xda, 0xc0, 0x19, 0x01, 0xfb, 0x08, 0x35, 0xe7, 0x18, 0x8f,
  19374. 0xf0, 0x19, 0xfb, 0x46, 0xf6, 0xa5, 0x77, 0x0e, 0x90, 0x38, 0x8b, 0x15
  19375. },
  19376. {
  19377. 0x80, 0x8c, 0xa7, 0x24, 0x97, 0xf9, 0xd3, 0x52, 0xb0, 0x69, 0x9d, 0x4b,
  19378. 0xa4, 0x19, 0x4a, 0xb1, 0x46, 0x53, 0x3a, 0xc8, 0xe4, 0x02, 0x69, 0xf2,
  19379. 0xe7, 0xb6, 0x1d, 0x33, 0x51, 0xcc, 0x14, 0x40, 0x4a, 0xb0, 0xe7, 0x58,
  19380. 0x84, 0xba, 0xc2, 0x14, 0x58, 0x6b, 0xb9, 0xdc, 0x50, 0x98, 0x67, 0x01
  19381. },
  19382. {
  19383. 0xb1, 0xa8, 0xc0, 0x06, 0xb3, 0x2e, 0xa7, 0x8a, 0x6a, 0x12, 0x88, 0x00,
  19384. 0x65, 0x88, 0x9c, 0x5d, 0x35, 0xee, 0xe5, 0x51, 0x0b, 0x62, 0xf8, 0x67,
  19385. 0xe5, 0xef, 0x15, 0x1f, 0x23, 0x02, 0x74, 0x08, 0x9c, 0xc8, 0xba, 0x27,
  19386. 0x5d, 0x32, 0x19, 0x6f, 0x6d, 0x5d, 0x72, 0x5e, 0x15, 0xde, 0x30, 0xc3
  19387. },
  19388. {
  19389. 0xfd, 0xce, 0xf5, 0x65, 0x45, 0x84, 0xfb, 0x8c, 0x79, 0xa4, 0x6c, 0x1b,
  19390. 0x0e, 0x1b, 0xfd, 0x26, 0xa2, 0x53, 0xf4, 0x4e, 0x00, 0x4d, 0x4b, 0x0b,
  19391. 0x24, 0x6d, 0x35, 0x35, 0xd9, 0x97, 0x70, 0xc5, 0xf4, 0xee, 0xe3, 0xba,
  19392. 0x31, 0x1e, 0x2a, 0x42, 0xcb, 0xdf, 0x40, 0xb1, 0x14, 0xb8, 0x53, 0xce
  19393. },
  19394. {
  19395. 0xbb, 0xb3, 0x26, 0x7c, 0x22, 0x21, 0x9b, 0x72, 0x32, 0xa1, 0x97, 0xfb,
  19396. 0x78, 0x8c, 0xbe, 0x3d, 0x71, 0x45, 0xb8, 0xf5, 0x24, 0x8f, 0x0f, 0xac,
  19397. 0x42, 0x5b, 0x81, 0xe8, 0xd0, 0x71, 0x4a, 0xcb, 0x32, 0x3f, 0x03, 0xfb,
  19398. 0xec, 0x6a, 0x1f, 0x76, 0x80, 0x65, 0x01, 0x7a, 0x3d, 0xce, 0xc4, 0xdf
  19399. },
  19400. {
  19401. 0x3f, 0xcf, 0x2f, 0x63, 0x94, 0x94, 0x99, 0xfd, 0x04, 0x3a, 0x89, 0x83,
  19402. 0xcf, 0x06, 0x05, 0xec, 0x20, 0x3e, 0x5f, 0x51, 0x9d, 0x6e, 0x4a, 0xc6,
  19403. 0xf1, 0x2b, 0x37, 0x17, 0x34, 0x72, 0x6e, 0x1d, 0x2a, 0xfd, 0xc7, 0x73,
  19404. 0xb5, 0x07, 0x22, 0x81, 0x32, 0x2e, 0x21, 0x85, 0xaf, 0x10, 0xb2, 0x73
  19405. },
  19406. {
  19407. 0x52, 0x0c, 0x3d, 0x2e, 0x2d, 0x4a, 0x11, 0xae, 0x96, 0x78, 0xe9, 0x5b,
  19408. 0xd8, 0x0f, 0x6c, 0xf4, 0xbd, 0x96, 0x13, 0x55, 0x88, 0xdd, 0xa3, 0x67,
  19409. 0x36, 0x86, 0x1e, 0x0b, 0x36, 0x41, 0xec, 0xf6, 0x04, 0xb2, 0xc4, 0x16,
  19410. 0xbc, 0x2c, 0xdb, 0x30, 0x02, 0x94, 0xd4, 0x42, 0xbf, 0x38, 0xee, 0x9d
  19411. }
  19412. },
  19413. { /* 36 */
  19414. WC_HASH_TYPE_SHA384, 0, 33,
  19415. { 0 }, /* PSK */
  19416. { /* DHE */
  19417. 0xd3, 0x00, 0x72, 0x9a, 0xa8, 0xc5, 0xf3, 0xc4,
  19418. 0xf1, 0xa0, 0x26, 0x89, 0x65, 0x70, 0xc7, 0x0b,
  19419. 0x77, 0xbb, 0xe1, 0x4b, 0x2b, 0xa8, 0x4f, 0xa6,
  19420. 0x09, 0x4b, 0xba, 0x45, 0x36, 0x15, 0xee, 0x68,
  19421. 0xfd
  19422. },
  19423. { /* Hello 1 */
  19424. 0x10, 0x9d, 0x8b, 0xa2, 0x93, 0xe7, 0xd3, 0xb9,
  19425. 0xb4, 0x0f, 0xeb, 0x6a, 0xb9, 0x69, 0xcb, 0x39,
  19426. 0x16, 0x29, 0xcc, 0xd3, 0xcc, 0x1a, 0x4c, 0x1b,
  19427. 0x53, 0x7c, 0x33, 0x88, 0x06, 0xbc, 0x0a, 0x02,
  19428. 0xa0, 0xbe, 0x62, 0xc0, 0xe6, 0x5e, 0x97, 0x5b,
  19429. 0x6a, 0xa1, 0x98, 0xf3, 0xd2, 0x1e, 0xcd, 0xc5
  19430. },
  19431. { /* Hello 2 */
  19432. 0x74, 0xc0, 0x07, 0x2c, 0xc1, 0x63, 0xcc, 0x11,
  19433. 0xad, 0x1a, 0x55, 0x63, 0xbc, 0x20, 0x77, 0x96,
  19434. 0x30, 0x1c, 0x68, 0x45, 0x1e, 0x9b, 0xa7, 0xb4,
  19435. 0xf3, 0x04, 0x45, 0x16, 0x76, 0x55, 0xf9, 0xdf,
  19436. 0x4b, 0x2f, 0x1a, 0xdf, 0x5a, 0xb0, 0x93, 0xc9,
  19437. 0xab, 0xf5, 0x32, 0x47, 0x79, 0x9c, 0x01, 0xeb
  19438. },
  19439. { /* Finished 1 */
  19440. 0x27, 0x08, 0x8e, 0xa5, 0xf1, 0x30, 0xe1, 0xd6,
  19441. 0x4f, 0xa2, 0x9e, 0x3b, 0x03, 0x2d, 0x2e, 0xa3,
  19442. 0x84, 0x75, 0x51, 0x3a, 0xc3, 0xf6, 0xee, 0x2e,
  19443. 0x37, 0x0c, 0xe3, 0x28, 0x46, 0xa5, 0x2d, 0xc7,
  19444. 0xf0, 0x64, 0x78, 0x53, 0x66, 0x43, 0x02, 0xa4,
  19445. 0x7a, 0x43, 0x66, 0x4b, 0xa7, 0xcb, 0x97, 0x16
  19446. },
  19447. { /* Finished 2 */
  19448. 0x1d, 0x0d, 0xf8, 0xe1, 0x81, 0xa5, 0xbd, 0xa8,
  19449. 0x6f, 0x9d, 0x01, 0xa4, 0x9a, 0x92, 0xe2, 0xef,
  19450. 0x08, 0xab, 0xef, 0x3e, 0x2d, 0xd4, 0x82, 0xac,
  19451. 0x68, 0x9d, 0xe0, 0x54, 0x17, 0xde, 0x1a, 0xed,
  19452. 0x57, 0xcb, 0xd9, 0x2d, 0xc8, 0xbc, 0x93, 0xe6,
  19453. 0xa3, 0xec, 0xde, 0xee, 0xa1, 0x1c, 0x41, 0x85
  19454. },
  19455. {
  19456. 0x7f, 0x1f, 0xe6, 0x7b, 0xd8, 0xf5, 0x2b, 0x37, 0xbe, 0xb7, 0xd0, 0x37,
  19457. 0xce, 0x46, 0xad, 0x04, 0x2f, 0xc7, 0xdb, 0xc9, 0x9a, 0xb6, 0x00, 0x3f,
  19458. 0xc1, 0x97, 0xe9, 0x5c, 0x5e, 0x14, 0xd1, 0x38, 0x4d, 0x55, 0xe1, 0x07,
  19459. 0xb5, 0x85, 0x6d, 0xfa, 0xa7, 0x66, 0xad, 0xfa, 0xb6, 0xad, 0x29, 0x44
  19460. },
  19461. {
  19462. 0x4e, 0x6b, 0x20, 0x99, 0x55, 0x1b, 0x21, 0x89, 0xb6, 0x70, 0xdb, 0xe8,
  19463. 0xa7, 0x16, 0x55, 0xf2, 0x93, 0x13, 0x90, 0x7d, 0xfa, 0x62, 0x65, 0x53,
  19464. 0xa0, 0x97, 0xe9, 0xb4, 0xc0, 0xf1, 0xc9, 0x1a, 0x67, 0xdd, 0xca, 0x57,
  19465. 0xbc, 0xca, 0x39, 0xe6, 0x39, 0x6b, 0x63, 0x47, 0x25, 0x08, 0x3a, 0xd7
  19466. },
  19467. {
  19468. 0x35, 0x0d, 0xac, 0xd8, 0x10, 0x6a, 0x46, 0x50, 0x66, 0xae, 0x02, 0xc9,
  19469. 0xde, 0x13, 0x48, 0xce, 0x53, 0xd4, 0x92, 0x62, 0xc5, 0x65, 0x10, 0x08,
  19470. 0xc2, 0xc2, 0x82, 0xed, 0x9d, 0xc9, 0x6f, 0xa8, 0xc3, 0xc1, 0x0b, 0x7c,
  19471. 0xe1, 0x97, 0x85, 0xd6, 0x46, 0x29, 0x0e, 0x42, 0x51, 0xc1, 0x35, 0xcf
  19472. },
  19473. {
  19474. 0x3d, 0x5d, 0x84, 0xbd, 0x16, 0x46, 0x34, 0xb3, 0xf6, 0x31, 0x49, 0x3e,
  19475. 0x8d, 0xdc, 0xcb, 0x8c, 0x6a, 0x42, 0xf4, 0x88, 0xfc, 0x19, 0xfa, 0xa2,
  19476. 0x25, 0xc7, 0xa0, 0xa4, 0xca, 0xf0, 0xea, 0x2d, 0xe8, 0xc4, 0x02, 0x14,
  19477. 0x63, 0xfb, 0xd3, 0x7b, 0x51, 0x1c, 0xce, 0xca, 0xa3, 0xc3, 0xe4, 0xa5
  19478. },
  19479. {
  19480. 0x7c, 0x3a, 0x55, 0x92, 0x2e, 0xdd, 0x75, 0xdd, 0x76, 0x54, 0x4a, 0x9f,
  19481. 0xd0, 0xa2, 0x88, 0x83, 0xe9, 0x27, 0xda, 0x30, 0xe9, 0x96, 0x58, 0xc5,
  19482. 0xb7, 0x56, 0xfc, 0x4b, 0xb8, 0x5d, 0xee, 0x46, 0x70, 0x4e, 0x1b, 0x06,
  19483. 0x86, 0xaf, 0x48, 0x5c, 0x17, 0x35, 0xfa, 0x69, 0xc2, 0x4d, 0xfb, 0x09
  19484. },
  19485. {
  19486. 0x00, 0x0e, 0x28, 0x51, 0xc1, 0x7f, 0x41, 0x89, 0x6f, 0x9a, 0xca, 0x15,
  19487. 0xee, 0xed, 0x43, 0xca, 0x6d, 0x65, 0x6f, 0x51, 0x18, 0x6c, 0x08, 0x4b,
  19488. 0x77, 0xca, 0x75, 0xc4, 0xc3, 0xde, 0x29, 0x41, 0x8b, 0xaf, 0xa7, 0x1c,
  19489. 0x28, 0x37, 0xa0, 0xa0, 0x74, 0x8e, 0x09, 0x42, 0x7a, 0x1b, 0x68, 0xdb
  19490. },
  19491. {
  19492. 0x14, 0x8f, 0xab, 0x28, 0x64, 0xea, 0x45, 0x88, 0xdb, 0xc1, 0xc6, 0xa0,
  19493. 0x48, 0xdf, 0x15, 0xd0, 0x28, 0x07, 0x2d, 0x6c, 0xb8, 0x42, 0xbb, 0x60,
  19494. 0x02, 0x08, 0x9e, 0x29, 0x9b, 0x8d, 0xd6, 0x1c, 0xaf, 0xf2, 0x1a, 0xdc,
  19495. 0xf0, 0x78, 0x0b, 0x4d, 0x90, 0xa1, 0x0c, 0xb3, 0x13, 0xde, 0xca, 0x5a
  19496. },
  19497. {
  19498. 0x4d, 0x80, 0x7d, 0x0b, 0xb9, 0x00, 0x6f, 0x65, 0x51, 0x65, 0x23, 0xde,
  19499. 0x72, 0xdc, 0x4f, 0x04, 0xa5, 0xa2, 0x90, 0x45, 0x51, 0x9e, 0xd0, 0x3a,
  19500. 0xe4, 0xd7, 0x78, 0xa3, 0x0f, 0x2d, 0x65, 0x12, 0xad, 0xc8, 0x92, 0x30,
  19501. 0x79, 0x9d, 0x9d, 0x08, 0x7a, 0x9c, 0x9f, 0x83, 0xb1, 0xca, 0x59, 0x56
  19502. }
  19503. },
  19504. { /* 41 */
  19505. WC_HASH_TYPE_SHA384, 33, 0,
  19506. { /* PSK */
  19507. 0xa4, 0x8b, 0x1b, 0x5f, 0xd0, 0xea, 0x75, 0x62,
  19508. 0x06, 0x4d, 0x68, 0x40, 0x85, 0x20, 0x45, 0x95,
  19509. 0x4a, 0x00, 0xca, 0x05, 0xeb, 0xd4, 0x1d, 0x48,
  19510. 0x81, 0x89, 0xe8, 0x86, 0x43, 0xfa, 0x28, 0x17,
  19511. 0x12
  19512. },
  19513. { 0 }, /* DHE */
  19514. { /* Hello 1 */
  19515. 0x03, 0x7c, 0x33, 0x75, 0xdc, 0xc5, 0x46, 0x3a,
  19516. 0x0d, 0x56, 0xc6, 0xfb, 0xab, 0x1e, 0x1d, 0xda,
  19517. 0x59, 0xc2, 0xb2, 0xb1, 0x7c, 0x48, 0x9b, 0x06,
  19518. 0x0a, 0x5a, 0xbb, 0xf8, 0x98, 0x53, 0x78, 0x2d,
  19519. 0xd2, 0xcc, 0x87, 0x68, 0x25, 0xdd, 0x88, 0x22,
  19520. 0xcd, 0xb7, 0x74, 0x55, 0x21, 0xf9, 0x34, 0x98
  19521. },
  19522. { /* Hello 2 */
  19523. 0x03, 0xb4, 0xfb, 0xcc, 0x28, 0x2c, 0xc1, 0x70,
  19524. 0x42, 0x73, 0x57, 0xac, 0xdb, 0x47, 0x71, 0xf6,
  19525. 0x2e, 0x11, 0x8a, 0x5b, 0x47, 0x2f, 0x02, 0x54,
  19526. 0x95, 0x34, 0xed, 0x5f, 0x19, 0xc1, 0x75, 0xe0,
  19527. 0x76, 0xad, 0xb0, 0x90, 0x57, 0xcd, 0xfd, 0xd7,
  19528. 0x58, 0x1f, 0x0d, 0x6b, 0x9e, 0x51, 0x3c, 0x08
  19529. },
  19530. { /* Finished 1 */
  19531. 0x2b, 0x50, 0xd9, 0xa7, 0x43, 0x24, 0xda, 0x2c,
  19532. 0x7a, 0xaa, 0x0e, 0x37, 0xd7, 0x6b, 0x2c, 0xab,
  19533. 0x8e, 0xb2, 0xfe, 0x31, 0x1b, 0xa8, 0x12, 0x59,
  19534. 0x5b, 0x7b, 0xdc, 0x3e, 0xa7, 0x86, 0xa5, 0x48,
  19535. 0xe4, 0x46, 0x2b, 0x4c, 0xc1, 0x66, 0x4b, 0xf3,
  19536. 0x2a, 0x99, 0x93, 0x08, 0xbc, 0x3d, 0x08, 0x76
  19537. },
  19538. { /* Finished 2 */
  19539. 0x7c, 0x34, 0xc8, 0x56, 0x17, 0xf1, 0x62, 0x1c,
  19540. 0x9f, 0x0b, 0xeb, 0xfd, 0x69, 0x72, 0x51, 0xc5,
  19541. 0xfa, 0x74, 0x87, 0xc9, 0xbd, 0x50, 0xe9, 0x48,
  19542. 0xa7, 0x3c, 0x94, 0x3e, 0x06, 0x7d, 0xe8, 0x8e,
  19543. 0xc1, 0xd1, 0x08, 0x1f, 0x5d, 0x48, 0x8a, 0x25,
  19544. 0xfc, 0xea, 0xe7, 0xd9, 0xd4, 0xd0, 0xf9, 0xad
  19545. },
  19546. {
  19547. 0x4b, 0x0b, 0xed, 0xb9, 0xc8, 0xb8, 0xa8, 0x1e, 0xb0, 0x81, 0x76, 0xd5,
  19548. 0x33, 0x22, 0x71, 0x33, 0x3a, 0x85, 0x19, 0x67, 0x7e, 0x91, 0x37, 0xf2,
  19549. 0xa6, 0x11, 0x22, 0xdf, 0x41, 0x04, 0x3d, 0xa9, 0x13, 0xb9, 0xb2, 0xb1,
  19550. 0xbb, 0xd8, 0xef, 0x23, 0x7c, 0xc2, 0xab, 0x70, 0x1b, 0x51, 0x9f, 0xc9
  19551. },
  19552. {
  19553. 0xeb, 0x96, 0x10, 0x8c, 0x7d, 0x92, 0xea, 0x80, 0x86, 0xb2, 0xf8, 0x27,
  19554. 0xf2, 0x9a, 0x09, 0xc1, 0x7c, 0x09, 0x43, 0xbc, 0xfe, 0xc8, 0x75, 0xe0,
  19555. 0x97, 0xe7, 0x6d, 0xd5, 0xb2, 0x3c, 0xed, 0x12, 0xb7, 0x74, 0x0e, 0xe3,
  19556. 0xb6, 0xe0, 0xba, 0xe1, 0x8d, 0x89, 0xcf, 0x4f, 0x57, 0xf6, 0x6d, 0x90
  19557. },
  19558. {
  19559. 0x22, 0xb0, 0x39, 0x34, 0xb6, 0x6c, 0x2d, 0x7a, 0x97, 0x1c, 0x5d, 0xcc,
  19560. 0x78, 0x84, 0x71, 0xbb, 0xc6, 0x7b, 0xb6, 0xbc, 0xcc, 0x0b, 0xf8, 0xac,
  19561. 0x8e, 0xd7, 0x20, 0xbd, 0xbe, 0x32, 0xf0, 0xd6, 0xe9, 0x69, 0x13, 0xf2,
  19562. 0x9a, 0xce, 0xfe, 0x86, 0xd3, 0xee, 0xba, 0x69, 0x51, 0xb6, 0x77, 0x56
  19563. },
  19564. {
  19565. 0x16, 0xfd, 0xda, 0xf3, 0x5e, 0xb9, 0xa6, 0x17, 0x24, 0xb2, 0x16, 0x9f,
  19566. 0xb6, 0x59, 0x13, 0x0f, 0x25, 0x5a, 0xf1, 0x5b, 0x5f, 0xe4, 0x54, 0x2a,
  19567. 0xa7, 0xbf, 0x29, 0xaf, 0x5a, 0x77, 0xf4, 0x4f, 0x25, 0xba, 0x94, 0xad,
  19568. 0x6b, 0x91, 0x3b, 0xe7, 0xd5, 0x73, 0x0d, 0xff, 0xaa, 0xe3, 0x72, 0x2c
  19569. },
  19570. {
  19571. 0x22, 0xb4, 0x94, 0xc0, 0x53, 0xd7, 0x82, 0x06, 0x38, 0x9d, 0x4a, 0xa0,
  19572. 0x3f, 0xf1, 0x5f, 0x6e, 0x23, 0x8d, 0x09, 0x62, 0xbf, 0x6f, 0x7c, 0x84,
  19573. 0xc6, 0x3e, 0x15, 0xad, 0x18, 0x37, 0x76, 0x29, 0xc7, 0xd6, 0x68, 0x0c,
  19574. 0x1e, 0xc6, 0x93, 0x31, 0xef, 0x85, 0x69, 0x30, 0x68, 0xf0, 0x1e, 0x37
  19575. },
  19576. {
  19577. 0x6d, 0x4d, 0x20, 0xaf, 0x47, 0xe8, 0x1b, 0xfa, 0xd0, 0xb6, 0xc8, 0x97,
  19578. 0xd1, 0x03, 0xfc, 0x9d, 0x59, 0xa0, 0x68, 0x9d, 0xe9, 0x17, 0x8b, 0xce,
  19579. 0x48, 0x2c, 0x77, 0x8a, 0x22, 0x4b, 0x5c, 0x54, 0x22, 0xa1, 0x15, 0x12,
  19580. 0xe1, 0x07, 0x8e, 0x15, 0xd8, 0x7b, 0x16, 0x65, 0x99, 0x6b, 0xcb, 0x71
  19581. },
  19582. {
  19583. 0x79, 0x64, 0x79, 0xdd, 0x75, 0x5c, 0x6f, 0x98, 0xac, 0x03, 0xe0, 0xcd,
  19584. 0x92, 0xba, 0x0e, 0x2d, 0xb4, 0xd1, 0x8b, 0x97, 0xd0, 0x85, 0xbb, 0x2e,
  19585. 0x4f, 0x26, 0x93, 0xf5, 0x1d, 0xf3, 0xd2, 0x43, 0x4f, 0xd2, 0x47, 0xaa,
  19586. 0x91, 0x1e, 0xf3, 0x67, 0x10, 0x18, 0x2c, 0xb9, 0x01, 0xba, 0x10, 0x9f
  19587. },
  19588. {
  19589. 0x79, 0xb6, 0x9c, 0xbe, 0xf1, 0x6a, 0xb0, 0x92, 0xa0, 0x29, 0x52, 0x61,
  19590. 0xf1, 0xcd, 0x3a, 0x67, 0xe1, 0x6b, 0xb8, 0x9d, 0x0d, 0x95, 0xb6, 0x03,
  19591. 0x80, 0x1f, 0xd5, 0x75, 0xb6, 0x1d, 0x79, 0x02, 0x93, 0x43, 0x77, 0xa7,
  19592. 0x9d, 0x2f, 0xc3, 0x84, 0xc6, 0x83, 0x76, 0x16, 0x06, 0x98, 0x7b, 0x79
  19593. }
  19594. },
  19595. #endif /* WOLFSSL_SHA384 */
  19596. };
  19597. const char protocolLabel[] = "tls13 ";
  19598. const char ceTrafficLabel[] = "c e traffic";
  19599. const char eExpMasterLabel[] = "e exp master";
  19600. const char cHsTrafficLabel[] = "c hs traffic";
  19601. const char sHsTrafficLabel[] = "s hs traffic";
  19602. const char cAppTrafficLabel[] = "c ap traffic";
  19603. const char sAppTrafficLabel[] = "s ap traffic";
  19604. const char expMasterLabel[] = "exp master";
  19605. const char resMasterLabel[] = "res master";
  19606. const char derivedLabel[] = "derived";
  19607. WOLFSSL_TEST_SUBROUTINE int tls13_kdf_test(void)
  19608. {
  19609. int ret = 0;
  19610. word32 i;
  19611. word32 tc = sizeof(tls13KdfTestVectors)/sizeof(Tls13KdfTestVector);
  19612. const Tls13KdfTestVector* tv = NULL;
  19613. for (i = 0, tv = tls13KdfTestVectors; i < tc; i++, tv++) {
  19614. byte output[WC_MAX_DIGEST_SIZE];
  19615. byte secret[WC_MAX_DIGEST_SIZE];
  19616. byte salt[WC_MAX_DIGEST_SIZE];
  19617. byte zeroes[WC_MAX_DIGEST_SIZE];
  19618. byte hashZero[WC_MAX_DIGEST_SIZE];
  19619. int hashAlgSz;
  19620. XMEMSET(zeroes, 0, sizeof zeroes);
  19621. hashAlgSz = wc_HashGetDigestSize(tv->hashAlg);
  19622. if (hashAlgSz == BAD_FUNC_ARG) break;
  19623. ret = wc_Hash(tv->hashAlg, NULL, 0, hashZero, hashAlgSz);
  19624. if (ret != 0) break;
  19625. ret = wc_Tls13_HKDF_Extract(secret, NULL, 0,
  19626. (tv->pskSz == 0) ? zeroes : (byte*)tv->psk,
  19627. tv->pskSz, tv->hashAlg);
  19628. if (ret != 0) break;
  19629. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19630. secret, hashAlgSz,
  19631. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19632. (byte*)ceTrafficLabel, (word32)strlen(ceTrafficLabel),
  19633. tv->hashHello1, hashAlgSz, tv->hashAlg);
  19634. if (ret != 0) break;
  19635. ret = XMEMCMP(tv->clientEarlyTrafficSecret, output, hashAlgSz);
  19636. if (ret != 0) break;
  19637. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19638. secret, hashAlgSz,
  19639. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19640. (byte*)eExpMasterLabel, (word32)strlen(eExpMasterLabel),
  19641. tv->hashHello1, hashAlgSz, tv->hashAlg);
  19642. if (ret != 0) break;
  19643. ret = XMEMCMP(tv->earlyExporterMasterSecret, output, hashAlgSz);
  19644. if (ret != 0) break;
  19645. ret = wc_Tls13_HKDF_Expand_Label(salt, hashAlgSz,
  19646. secret, hashAlgSz,
  19647. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19648. (byte*)derivedLabel, (word32)strlen(derivedLabel),
  19649. hashZero, hashAlgSz, tv->hashAlg);
  19650. if (ret != 0) break;
  19651. ret = wc_Tls13_HKDF_Extract(secret, salt, hashAlgSz,
  19652. (tv->dheSz == 0) ? zeroes : (byte*)tv->dhe,
  19653. tv->dheSz, tv->hashAlg);
  19654. if (ret != 0) break;
  19655. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19656. secret, hashAlgSz,
  19657. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19658. (byte*)cHsTrafficLabel, (word32)strlen(cHsTrafficLabel),
  19659. tv->hashHello2, hashAlgSz, tv->hashAlg);
  19660. if (ret != 0) break;
  19661. ret = XMEMCMP(tv->clientHandshakeTrafficSecret,
  19662. output, hashAlgSz);
  19663. if (ret != 0) break;
  19664. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19665. secret, hashAlgSz,
  19666. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19667. (byte*)sHsTrafficLabel, (word32)strlen(sHsTrafficLabel),
  19668. tv->hashHello2, hashAlgSz, tv->hashAlg);
  19669. if (ret != 0) break;
  19670. ret = XMEMCMP(tv->serverHandshakeTrafficSecret, output, hashAlgSz);
  19671. if (ret != 0) break;
  19672. ret = wc_Tls13_HKDF_Expand_Label(salt, hashAlgSz,
  19673. secret, hashAlgSz,
  19674. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19675. (byte*)derivedLabel, (word32)strlen(derivedLabel),
  19676. hashZero, hashAlgSz, tv->hashAlg);
  19677. if (ret != 0) break;
  19678. ret = wc_Tls13_HKDF_Extract(secret, salt, hashAlgSz,
  19679. zeroes, hashAlgSz, tv->hashAlg);
  19680. if (ret != 0) break;
  19681. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19682. secret, hashAlgSz,
  19683. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19684. (byte*)cAppTrafficLabel, (word32)strlen(cAppTrafficLabel),
  19685. tv->hashFinished1, hashAlgSz, tv->hashAlg);
  19686. if (ret != 0) break;
  19687. ret = XMEMCMP(tv->clientApplicationTrafficSecret, output, hashAlgSz);
  19688. if (ret != 0) break;
  19689. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19690. secret, hashAlgSz,
  19691. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19692. (byte*)sAppTrafficLabel, (word32)strlen(sAppTrafficLabel),
  19693. tv->hashFinished1, hashAlgSz, tv->hashAlg);
  19694. if (ret != 0) break;
  19695. ret = XMEMCMP(tv->serverApplicationTrafficSecret, output, hashAlgSz);
  19696. if (ret != 0) break;
  19697. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19698. secret, hashAlgSz,
  19699. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19700. (byte*)expMasterLabel, (word32)strlen(expMasterLabel),
  19701. tv->hashFinished1, hashAlgSz, tv->hashAlg);
  19702. if (ret != 0) break;
  19703. ret = XMEMCMP(tv->exporterMasterSecret, output, hashAlgSz);
  19704. if (ret != 0) break;
  19705. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19706. secret, hashAlgSz,
  19707. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19708. (byte*)resMasterLabel, (word32)strlen(resMasterLabel),
  19709. tv->hashFinished2, hashAlgSz, tv->hashAlg);
  19710. if (ret != 0) break;
  19711. ret = XMEMCMP(tv->resumptionMasterSecret, output, hashAlgSz);
  19712. if (ret != 0) break;
  19713. }
  19714. return ret;
  19715. }
  19716. #endif /* WOLFSSL_TLS13 */
  19717. #if defined(HAVE_ECC) && defined(HAVE_X963_KDF)
  19718. WOLFSSL_TEST_SUBROUTINE int x963kdf_test(void)
  19719. {
  19720. int ret;
  19721. byte kek[128];
  19722. #ifndef NO_SHA
  19723. /* SHA-1, COUNT = 0
  19724. * shared secret length: 192
  19725. * SharedInfo length: 0
  19726. * key data length: 128
  19727. */
  19728. WOLFSSL_SMALL_STACK_STATIC const byte Z[] = {
  19729. 0x1c, 0x7d, 0x7b, 0x5f, 0x05, 0x97, 0xb0, 0x3d,
  19730. 0x06, 0xa0, 0x18, 0x46, 0x6e, 0xd1, 0xa9, 0x3e,
  19731. 0x30, 0xed, 0x4b, 0x04, 0xdc, 0x64, 0xcc, 0xdd
  19732. };
  19733. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  19734. 0xbf, 0x71, 0xdf, 0xfd, 0x8f, 0x4d, 0x99, 0x22,
  19735. 0x39, 0x36, 0xbe, 0xb4, 0x6f, 0xee, 0x8c, 0xcc
  19736. };
  19737. #endif
  19738. #ifndef NO_SHA256
  19739. /* SHA-256, COUNT = 3
  19740. * shared secret length: 192
  19741. * SharedInfo length: 0
  19742. * key data length: 128
  19743. */
  19744. WOLFSSL_SMALL_STACK_STATIC const byte Z2[] = {
  19745. 0xd3, 0x8b, 0xdb, 0xe5, 0xc4, 0xfc, 0x16, 0x4c,
  19746. 0xdd, 0x96, 0x7f, 0x63, 0xc0, 0x4f, 0xe0, 0x7b,
  19747. 0x60, 0xcd, 0xe8, 0x81, 0xc2, 0x46, 0x43, 0x8c
  19748. };
  19749. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  19750. 0x5e, 0x67, 0x4d, 0xb9, 0x71, 0xba, 0xc2, 0x0a,
  19751. 0x80, 0xba, 0xd0, 0xd4, 0x51, 0x4d, 0xc4, 0x84
  19752. };
  19753. #endif
  19754. #ifdef WOLFSSL_SHA512
  19755. /* SHA-512, COUNT = 0
  19756. * shared secret length: 192
  19757. * SharedInfo length: 0
  19758. * key data length: 128
  19759. */
  19760. WOLFSSL_SMALL_STACK_STATIC const byte Z3[] = {
  19761. 0x87, 0xfc, 0x0d, 0x8c, 0x44, 0x77, 0x48, 0x5b,
  19762. 0xb5, 0x74, 0xf5, 0xfc, 0xea, 0x26, 0x4b, 0x30,
  19763. 0x88, 0x5d, 0xc8, 0xd9, 0x0a, 0xd8, 0x27, 0x82
  19764. };
  19765. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  19766. 0x94, 0x76, 0x65, 0xfb, 0xb9, 0x15, 0x21, 0x53,
  19767. 0xef, 0x46, 0x02, 0x38, 0x50, 0x6a, 0x02, 0x45
  19768. };
  19769. /* SHA-512, COUNT = 0
  19770. * shared secret length: 521
  19771. * SharedInfo length: 128
  19772. * key data length: 1024
  19773. */
  19774. WOLFSSL_SMALL_STACK_STATIC const byte Z4[] = {
  19775. 0x00, 0xaa, 0x5b, 0xb7, 0x9b, 0x33, 0xe3, 0x89,
  19776. 0xfa, 0x58, 0xce, 0xad, 0xc0, 0x47, 0x19, 0x7f,
  19777. 0x14, 0xe7, 0x37, 0x12, 0xf4, 0x52, 0xca, 0xa9,
  19778. 0xfc, 0x4c, 0x9a, 0xdb, 0x36, 0x93, 0x48, 0xb8,
  19779. 0x15, 0x07, 0x39, 0x2f, 0x1a, 0x86, 0xdd, 0xfd,
  19780. 0xb7, 0xc4, 0xff, 0x82, 0x31, 0xc4, 0xbd, 0x0f,
  19781. 0x44, 0xe4, 0x4a, 0x1b, 0x55, 0xb1, 0x40, 0x47,
  19782. 0x47, 0xa9, 0xe2, 0xe7, 0x53, 0xf5, 0x5e, 0xf0,
  19783. 0x5a, 0x2d
  19784. };
  19785. WOLFSSL_SMALL_STACK_STATIC const byte info4[] = {
  19786. 0xe3, 0xb5, 0xb4, 0xc1, 0xb0, 0xd5, 0xcf, 0x1d,
  19787. 0x2b, 0x3a, 0x2f, 0x99, 0x37, 0x89, 0x5d, 0x31
  19788. };
  19789. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  19790. 0x44, 0x63, 0xf8, 0x69, 0xf3, 0xcc, 0x18, 0x76,
  19791. 0x9b, 0x52, 0x26, 0x4b, 0x01, 0x12, 0xb5, 0x85,
  19792. 0x8f, 0x7a, 0xd3, 0x2a, 0x5a, 0x2d, 0x96, 0xd8,
  19793. 0xcf, 0xfa, 0xbf, 0x7f, 0xa7, 0x33, 0x63, 0x3d,
  19794. 0x6e, 0x4d, 0xd2, 0xa5, 0x99, 0xac, 0xce, 0xb3,
  19795. 0xea, 0x54, 0xa6, 0x21, 0x7c, 0xe0, 0xb5, 0x0e,
  19796. 0xef, 0x4f, 0x6b, 0x40, 0xa5, 0xc3, 0x02, 0x50,
  19797. 0xa5, 0xa8, 0xee, 0xee, 0x20, 0x80, 0x02, 0x26,
  19798. 0x70, 0x89, 0xdb, 0xf3, 0x51, 0xf3, 0xf5, 0x02,
  19799. 0x2a, 0xa9, 0x63, 0x8b, 0xf1, 0xee, 0x41, 0x9d,
  19800. 0xea, 0x9c, 0x4f, 0xf7, 0x45, 0xa2, 0x5a, 0xc2,
  19801. 0x7b, 0xda, 0x33, 0xca, 0x08, 0xbd, 0x56, 0xdd,
  19802. 0x1a, 0x59, 0xb4, 0x10, 0x6c, 0xf2, 0xdb, 0xbc,
  19803. 0x0a, 0xb2, 0xaa, 0x8e, 0x2e, 0xfa, 0x7b, 0x17,
  19804. 0x90, 0x2d, 0x34, 0x27, 0x69, 0x51, 0xce, 0xcc,
  19805. 0xab, 0x87, 0xf9, 0x66, 0x1c, 0x3e, 0x88, 0x16
  19806. };
  19807. #endif
  19808. #ifndef NO_SHA
  19809. ret = wc_X963_KDF(WC_HASH_TYPE_SHA, Z, sizeof(Z), NULL, 0,
  19810. kek, sizeof(verify));
  19811. if (ret != 0)
  19812. return -9800;
  19813. if (XMEMCMP(verify, kek, sizeof(verify)) != 0)
  19814. return -9801;
  19815. #endif
  19816. #ifndef NO_SHA256
  19817. ret = wc_X963_KDF(WC_HASH_TYPE_SHA256, Z2, sizeof(Z2), NULL, 0,
  19818. kek, sizeof(verify2));
  19819. if (ret != 0)
  19820. return -9802;
  19821. if (XMEMCMP(verify2, kek, sizeof(verify2)) != 0)
  19822. return -9803;
  19823. #endif
  19824. #ifdef WOLFSSL_SHA512
  19825. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z3, sizeof(Z3), NULL, 0,
  19826. kek, sizeof(verify3));
  19827. if (ret != 0)
  19828. return -9804;
  19829. if (XMEMCMP(verify3, kek, sizeof(verify3)) != 0)
  19830. return -9805;
  19831. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z4, sizeof(Z4), info4,
  19832. sizeof(info4), kek, sizeof(verify4));
  19833. if (ret != 0)
  19834. return -9806;
  19835. if (XMEMCMP(verify4, kek, sizeof(verify4)) != 0)
  19836. return -9807;
  19837. #endif
  19838. return 0;
  19839. }
  19840. #endif /* HAVE_X963_KDF */
  19841. #ifdef HAVE_ECC
  19842. /* size to use for ECC key gen tests */
  19843. #ifndef ECC_KEYGEN_SIZE
  19844. #ifndef NO_ECC256
  19845. #define ECC_KEYGEN_SIZE 32
  19846. #elif defined(HAVE_ECC384)
  19847. #define ECC_KEYGEN_SIZE 48
  19848. #elif defined(HAVE_ECC224)
  19849. #define ECC_KEYGEN_SIZE 28
  19850. #elif defined(HAVE_ECC521)
  19851. #define ECC_KEYGEN_SIZE 66
  19852. #else
  19853. #error No ECC keygen size defined for test
  19854. #endif
  19855. #endif
  19856. #ifdef BENCH_EMBEDDED
  19857. #define ECC_SHARED_SIZE 128
  19858. #else
  19859. #define ECC_SHARED_SIZE MAX_ECC_BYTES
  19860. #endif
  19861. #if defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  19862. #define HAVE_ECC_DETERMINISTIC_K
  19863. #define ECC_DIGEST_SIZE WC_SHA256_DIGEST_SIZE
  19864. #else
  19865. #define ECC_DIGEST_SIZE MAX_ECC_BYTES
  19866. #endif
  19867. #define ECC_SIG_SIZE ECC_MAX_SIG_SIZE
  19868. #ifndef NO_ECC_VECTOR_TEST
  19869. #if (defined(HAVE_ECC192) || defined(HAVE_ECC224) ||\
  19870. !defined(NO_ECC256) || defined(HAVE_ECC384) ||\
  19871. defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES))
  19872. #define HAVE_ECC_VECTOR_TEST
  19873. #endif
  19874. #endif
  19875. #ifdef HAVE_ECC_VECTOR_TEST
  19876. typedef struct eccVector {
  19877. const char* msg; /* SHA-1 Encoded Message */
  19878. const char* Qx;
  19879. const char* Qy;
  19880. const char* d; /* Private Key */
  19881. const char* R;
  19882. const char* S;
  19883. const char* curveName;
  19884. word32 msgLen;
  19885. word32 keySize;
  19886. #ifndef NO_ASN
  19887. const byte* r;
  19888. word32 rSz;
  19889. const byte* s;
  19890. word32 sSz;
  19891. #endif
  19892. } eccVector;
  19893. #if !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  19894. static int ecc_test_vector_item(const eccVector* vector)
  19895. {
  19896. int ret = 0, verify = 0;
  19897. word32 sigSz;
  19898. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19899. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19900. #else
  19901. ecc_key userA[1];
  19902. #endif
  19903. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  19904. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  19905. word32 sigRawSz, rSz = MAX_ECC_BYTES, sSz = MAX_ECC_BYTES;
  19906. WC_DECLARE_VAR(sigRaw, byte, ECC_SIG_SIZE, HEAP_HINT);
  19907. WC_DECLARE_VAR(r, byte, MAX_ECC_BYTES, HEAP_HINT);
  19908. WC_DECLARE_VAR(s, byte, MAX_ECC_BYTES, HEAP_HINT);
  19909. #endif
  19910. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  19911. if (sig == NULL)
  19912. ERROR_OUT(MEMORY_E, done);
  19913. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  19914. if (sigRaw == NULL || r == NULL || s == NULL)
  19915. ERROR_OUT(MEMORY_E, done);
  19916. #endif
  19917. #endif
  19918. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19919. if (userA == NULL)
  19920. ERROR_OUT(MEMORY_E, done);
  19921. #endif
  19922. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  19923. if (ret != 0)
  19924. goto done;
  19925. ret = wc_ecc_import_raw(userA, vector->Qx, vector->Qy,
  19926. vector->d, vector->curveName);
  19927. if (ret != 0)
  19928. goto done;
  19929. XMEMSET(sig, 0, ECC_SIG_SIZE);
  19930. sigSz = ECC_SIG_SIZE;
  19931. ret = wc_ecc_rs_to_sig(vector->R, vector->S, sig, &sigSz);
  19932. if (ret != 0)
  19933. goto done;
  19934. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  19935. XMEMSET(sigRaw, 0, ECC_SIG_SIZE);
  19936. sigRawSz = ECC_SIG_SIZE;
  19937. ret = wc_ecc_rs_raw_to_sig(vector->r, vector->rSz, vector->s, vector->sSz,
  19938. sigRaw, &sigRawSz);
  19939. if (ret != 0)
  19940. goto done;
  19941. if (sigSz != sigRawSz || XMEMCMP(sig, sigRaw, sigSz) != 0) {
  19942. ret = -9810;
  19943. goto done;
  19944. }
  19945. ret = wc_ecc_sig_to_rs(sig, sigSz, r, &rSz, s, &sSz);
  19946. if (ret != 0)
  19947. goto done;
  19948. if (rSz != vector->rSz || XMEMCMP(r, vector->r, rSz) != 0 ||
  19949. sSz != vector->sSz || XMEMCMP(s, vector->s, sSz) != 0) {
  19950. ret = -9811;
  19951. goto done;
  19952. }
  19953. #endif
  19954. do {
  19955. #if defined(WOLFSSL_ASYNC_CRYPT)
  19956. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19957. #endif
  19958. if (ret == 0)
  19959. ret = wc_ecc_verify_hash(sig, sigSz, (byte*)vector->msg,
  19960. vector->msgLen, &verify, userA);
  19961. } while (ret == WC_PENDING_E);
  19962. if (ret != 0)
  19963. goto done;
  19964. TEST_SLEEP();
  19965. if (verify != 1)
  19966. ret = -9812;
  19967. done:
  19968. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19969. if (userA != NULL) {
  19970. wc_ecc_free(userA);
  19971. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19972. }
  19973. #else
  19974. wc_ecc_free(userA);
  19975. #endif
  19976. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  19977. WC_FREE_VAR(sigRaw, HEAP_HINT);
  19978. WC_FREE_VAR(r, HEAP_HINT);
  19979. WC_FREE_VAR(s, HEAP_HINT);
  19980. #endif
  19981. WC_FREE_VAR(sig, HEAP_HINT);
  19982. return ret;
  19983. }
  19984. static int ecc_test_vector(int keySize)
  19985. {
  19986. int ret;
  19987. eccVector vec;
  19988. XMEMSET(&vec, 0, sizeof(vec));
  19989. vec.keySize = (word32)keySize;
  19990. switch(keySize) {
  19991. #if defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)
  19992. case 14:
  19993. return 0;
  19994. #endif /* HAVE_ECC112 */
  19995. #if defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)
  19996. case 16:
  19997. return 0;
  19998. #endif /* HAVE_ECC128 */
  19999. #if defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)
  20000. case 20:
  20001. return 0;
  20002. #endif /* HAVE_ECC160 */
  20003. #if defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)
  20004. case 24:
  20005. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  20006. #if 1
  20007. vec.msg = "\x60\x80\x79\x42\x3f\x12\x42\x1d\xe6\x16\xb7\x49\x3e\xbe\x55\x1c\xf4\xd6\x5b\x92";
  20008. vec.msgLen = 20;
  20009. #else
  20010. /* This is the raw message prior to SHA-1 */
  20011. vec.msg =
  20012. "\xeb\xf7\x48\xd7\x48\xeb\xbc\xa7\xd2\x9f\xb4\x73\x69\x8a\x6e\x6b"
  20013. "\x4f\xb1\x0c\x86\x5d\x4a\xf0\x24\xcc\x39\xae\x3d\xf3\x46\x4b\xa4"
  20014. "\xf1\xd6\xd4\x0f\x32\xbf\x96\x18\xa9\x1b\xb5\x98\x6f\xa1\xa2\xaf"
  20015. "\x04\x8a\x0e\x14\xdc\x51\xe5\x26\x7e\xb0\x5e\x12\x7d\x68\x9d\x0a"
  20016. "\xc6\xf1\xa7\xf1\x56\xce\x06\x63\x16\xb9\x71\xcc\x7a\x11\xd0\xfd"
  20017. "\x7a\x20\x93\xe2\x7c\xf2\xd0\x87\x27\xa4\xe6\x74\x8c\xc3\x2f\xd5"
  20018. "\x9c\x78\x10\xc5\xb9\x01\x9d\xf2\x1c\xdc\xc0\xbc\xa4\x32\xc0\xa3"
  20019. "\xee\xd0\x78\x53\x87\x50\x88\x77\x11\x43\x59\xce\xe4\xa0\x71\xcf";
  20020. vec.msgLen = 128;
  20021. #endif
  20022. vec.Qx = "07008ea40b08dbe76432096e80a2494c94982d2d5bcf98e6";
  20023. vec.Qy = "76fab681d00b414ea636ba215de26d98c41bd7f2e4d65477";
  20024. vec.d = "e14f37b3d1374ff8b03f41b9b3fdd2f0ebccf275d660d7f3";
  20025. vec.R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  20026. vec.S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  20027. vec.curveName = "SECP192R1";
  20028. #ifndef NO_ASN
  20029. vec.r = (byte*)"\x69\x94\xd9\x62\xbd\xd0\xd7\x93\xff\xdd\xf8\x55"
  20030. "\xec\x5b\xf2\xf9\x1a\x96\x98\xb4\x62\x58\xa6\x3e";
  20031. vec.rSz = 24;
  20032. vec.s = (byte*)"\x02\xba\x64\x65\xa2\x34\x90\x37\x44\xab\x02\xbc"
  20033. "\x85\x21\x40\x5b\x73\xcf\x5f\xc0\x0e\x1a\x9f\x41";
  20034. vec.sSz = 24;
  20035. #endif
  20036. break;
  20037. #endif /* HAVE_ECC192 */
  20038. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  20039. case 28:
  20040. /* first [P-224,SHA-1] vector from FIPS 186-3 NIST vectors */
  20041. #if 1
  20042. vec.msg = "\xb9\xa3\xb8\x6d\xb0\xba\x99\xfd\xc6\xd2\x94\x6b\xfe\xbe\x9c\xe8\x3f\x10\x74\xfc";
  20043. vec.msgLen = 20;
  20044. #else
  20045. /* This is the raw message prior to SHA-1 */
  20046. vec.msg =
  20047. "\x36\xc8\xb2\x29\x86\x48\x7f\x67\x7c\x18\xd0\x97\x2a\x9e\x20\x47"
  20048. "\xb3\xaf\xa5\x9e\xc1\x62\x76\x4e\xc3\x0b\x5b\x69\xe0\x63\x0f\x99"
  20049. "\x0d\x4e\x05\xc2\x73\xb0\xe5\xa9\xd4\x28\x27\xb6\x95\xfc\x2d\x64"
  20050. "\xd9\x13\x8b\x1c\xf4\xc1\x21\x55\x89\x4c\x42\x13\x21\xa7\xbb\x97"
  20051. "\x0b\xdc\xe0\xfb\xf0\xd2\xae\x85\x61\xaa\xd8\x71\x7f\x2e\x46\xdf"
  20052. "\xe3\xff\x8d\xea\xb4\xd7\x93\x23\x56\x03\x2c\x15\x13\x0d\x59\x9e"
  20053. "\x26\xc1\x0f\x2f\xec\x96\x30\x31\xac\x69\x38\xa1\x8d\x66\x45\x38"
  20054. "\xb9\x4d\xac\x55\x34\xef\x7b\x59\x94\x24\xd6\x9b\xe1\xf7\x1c\x20";
  20055. vec.msgLen = 128;
  20056. #endif
  20057. vec.Qx = "8a4dca35136c4b70e588e23554637ae251077d1365a6ba5db9585de7";
  20058. vec.Qy = "ad3dee06de0be8279d4af435d7245f14f3b4f82eb578e519ee0057b1";
  20059. vec.d = "97c4b796e1639dd1035b708fc00dc7ba1682cec44a1002a1a820619f";
  20060. vec.R = "147b33758321e722a0360a4719738af848449e2c1d08defebc1671a7";
  20061. vec.S = "24fc7ed7f1352ca3872aa0916191289e2e04d454935d50fe6af3ad5b";
  20062. vec.curveName = "SECP224R1";
  20063. #ifndef NO_ASN
  20064. vec.r = (byte*)"\x14\x7b\x33\x75\x83\x21\xe7\x22\xa0\x36\x0a\x47"
  20065. "\x19\x73\x8a\xf8\x48\x44\x9e\x2c\x1d\x08\xde\xfe"
  20066. "\xbc\x16\x71\xa7";
  20067. vec.rSz = 28;
  20068. vec.s = (byte*)"\x24\xfc\x7e\xd7\xf1\x35\x2c\xa3\x87\x2a\xa0\x91"
  20069. "\x61\x91\x28\x9e\x2e\x04\xd4\x54\x93\x5d\x50\xfe"
  20070. "\x6a\xf3\xad\x5b";
  20071. vec.sSz = 28;
  20072. #endif
  20073. break;
  20074. #endif /* HAVE_ECC224 */
  20075. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  20076. case 30:
  20077. return 0;
  20078. #endif /* HAVE_ECC239 */
  20079. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  20080. case 32:
  20081. /* first [P-256,SHA-1] vector from FIPS 186-3 NIST vectors */
  20082. #if 1
  20083. vec.msg = "\xa3\xf9\x1a\xe2\x1b\xa6\xb3\x03\x98\x64\x47\x2f\x18\x41\x44\xc6\xaf\x62\xcd\x0e";
  20084. vec.msgLen = 20;
  20085. #else
  20086. /* This is the raw message prior to SHA-1 */
  20087. vec.msg =
  20088. "\xa2\x4b\x21\x76\x2e\x6e\xdb\x15\x3c\xc1\x14\x38\xdb\x0e\x92\xcd"
  20089. "\xf5\x2b\x86\xb0\x6c\xa9\x70\x16\x06\x27\x59\xc7\x0d\x36\xd1\x56"
  20090. "\x2c\xc9\x63\x0d\x7f\xc7\xc7\x74\xb2\x8b\x54\xe3\x1e\xf5\x58\x72"
  20091. "\xb2\xa6\x5d\xf1\xd7\xec\x26\xde\xbb\x33\xe7\xd9\x27\xef\xcc\xf4"
  20092. "\x6b\x63\xde\x52\xa4\xf4\x31\xea\xca\x59\xb0\x5d\x2e\xde\xc4\x84"
  20093. "\x5f\xff\xc0\xee\x15\x03\x94\xd6\x1f\x3d\xfe\xcb\xcd\xbf\x6f\x5a"
  20094. "\x73\x38\xd0\xbe\x3f\x2a\x77\x34\x51\x98\x3e\xba\xeb\x48\xf6\x73"
  20095. "\x8f\xc8\x95\xdf\x35\x7e\x1a\x48\xa6\x53\xbb\x35\x5a\x31\xa1\xb4"
  20096. vec.msgLen = 128;
  20097. #endif
  20098. vec.Qx = "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  20099. vec.Qy = "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  20100. vec.d = "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  20101. vec.R = "2b826f5d44e2d0b6de531ad96b51e8f0c56fdfead3c236892e4d84eacfc3b75c";
  20102. vec.S = "a2248b62c03db35a7cd63e8a120a3521a89d3d2f61ff99035a2148ae32e3a248";
  20103. #ifndef NO_ASN
  20104. vec.r = (byte*)"\x2b\x82\x6f\x5d\x44\xe2\xd0\xb6\xde\x53\x1a\xd9"
  20105. "\x6b\x51\xe8\xf0\xc5\x6f\xdf\xea\xd3\xc2\x36\x89"
  20106. "\x2e\x4d\x84\xea\xcf\xc3\xb7\x5c";
  20107. vec.rSz = 32;
  20108. vec.s = (byte*)"\xa2\x24\x8b\x62\xc0\x3d\xb3\x5a\x7c\xd6\x3e\x8a"
  20109. "\x12\x0a\x35\x21\xa8\x9d\x3d\x2f\x61\xff\x99\x03"
  20110. "\x5a\x21\x48\xae\x32\xe3\xa2\x48";
  20111. vec.sSz = 32;
  20112. #endif
  20113. vec.curveName = "SECP256R1";
  20114. break;
  20115. #endif /* !NO_ECC256 */
  20116. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  20117. case 40:
  20118. return 0;
  20119. #endif /* HAVE_ECC320 */
  20120. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  20121. case 48:
  20122. /* first [P-384,SHA-1] vector from FIPS 186-3 NIST vectors */
  20123. #if 1
  20124. vec.msg = "\x9b\x9f\x8c\x95\x35\xa5\xca\x26\x60\x5d\xb7\xf2\xfa\x57\x3b\xdf\xc3\x2e\xab\x8b";
  20125. vec.msgLen = 20;
  20126. #else
  20127. /* This is the raw message prior to SHA-1 */
  20128. vec.msg =
  20129. "\xab\xe1\x0a\xce\x13\xe7\xe1\xd9\x18\x6c\x48\xf7\x88\x9d\x51\x47"
  20130. "\x3d\x3a\x09\x61\x98\x4b\xc8\x72\xdf\x70\x8e\xcc\x3e\xd3\xb8\x16"
  20131. "\x9d\x01\xe3\xd9\x6f\xc4\xf1\xd5\xea\x00\xa0\x36\x92\xbc\xc5\xcf"
  20132. "\xfd\x53\x78\x7c\x88\xb9\x34\xaf\x40\x4c\x03\x9d\x32\x89\xb5\xba"
  20133. "\xc5\xae\x7d\xb1\x49\x68\x75\xb5\xdc\x73\xc3\x09\xf9\x25\xc1\x3d"
  20134. "\x1c\x01\xab\xda\xaf\xeb\xcd\xac\x2c\xee\x43\x39\x39\xce\x8d\x4a"
  20135. "\x0a\x5d\x57\xbb\x70\x5f\x3b\xf6\xec\x08\x47\x95\x11\xd4\xb4\xa3"
  20136. "\x21\x1f\x61\x64\x9a\xd6\x27\x43\x14\xbf\x0d\x43\x8a\x81\xe0\x60"
  20137. vec.msgLen = 128;
  20138. #endif
  20139. vec.Qx = "e55fee6c49d8d523f5ce7bf9c0425ce4ff650708b7de5cfb095901523979a7f042602db30854735369813b5c3f5ef868";
  20140. vec.Qy = "28f59cc5dc509892a988d38a8e2519de3d0c4fd0fbdb0993e38f18506c17606c5e24249246f1ce94983a5361c5be983e";
  20141. vec.d = "a492ce8fa90084c227e1a32f7974d39e9ff67a7e8705ec3419b35fb607582bebd461e0b1520ac76ec2dd4e9b63ebae71";
  20142. vec.R = "6820b8585204648aed63bdff47f6d9acebdea62944774a7d14f0e14aa0b9a5b99545b2daee6b3c74ebf606667a3f39b7";
  20143. vec.S = "491af1d0cccd56ddd520b233775d0bc6b40a6255cc55207d8e9356741f23c96c14714221078dbd5c17f4fdd89b32a907";
  20144. vec.curveName = "SECP384R1";
  20145. #ifndef NO_ASN
  20146. vec.r = (byte*)"\x68\x20\xb8\x58\x52\x04\x64\x8a\xed\x63\xbd\xff"
  20147. "\x47\xf6\xd9\xac\xeb\xde\xa6\x29\x44\x77\x4a\x7d"
  20148. "\x14\xf0\xe1\x4a\xa0\xb9\xa5\xb9\x95\x45\xb2\xda"
  20149. "\xee\x6b\x3c\x74\xeb\xf6\x06\x66\x7a\x3f\x39\xb7";
  20150. vec.rSz = 48;
  20151. vec.s = (byte*)"\x49\x1a\xf1\xd0\xcc\xcd\x56\xdd\xd5\x20\xb2\x33"
  20152. "\x77\x5d\x0b\xc6\xb4\x0a\x62\x55\xcc\x55\x20\x7d"
  20153. "\x8e\x93\x56\x74\x1f\x23\xc9\x6c\x14\x71\x42\x21"
  20154. "\x07\x8d\xbd\x5c\x17\xf4\xfd\xd8\x9b\x32\xa9\x07";
  20155. vec.sSz = 48;
  20156. #endif
  20157. break;
  20158. #endif /* HAVE_ECC384 */
  20159. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  20160. case 64:
  20161. return 0;
  20162. #endif /* HAVE_ECC512 */
  20163. #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
  20164. case 66:
  20165. /* first [P-521,SHA-1] vector from FIPS 186-3 NIST vectors */
  20166. #if 1
  20167. vec.msg = "\x1b\xf7\x03\x9c\xca\x23\x94\x27\x3f\x11\xa1\xd4\x8d\xcc\xb4\x46\x6f\x31\x61\xdf";
  20168. vec.msgLen = 20;
  20169. #else
  20170. /* This is the raw message prior to SHA-1 */
  20171. vec.msg =
  20172. "\x50\x3f\x79\x39\x34\x0a\xc7\x23\xcd\x4a\x2f\x4e\x6c\xcc\x27\x33"
  20173. "\x38\x3a\xca\x2f\xba\x90\x02\x19\x9d\x9e\x1f\x94\x8b\xe0\x41\x21"
  20174. "\x07\xa3\xfd\xd5\x14\xd9\x0c\xd4\xf3\x7c\xc3\xac\x62\xef\x00\x3a"
  20175. "\x2d\xb1\xd9\x65\x7a\xb7\x7f\xe7\x55\xbf\x71\xfa\x59\xe4\xd9\x6e"
  20176. "\xa7\x2a\xe7\xbf\x9d\xe8\x7d\x79\x34\x3b\xc1\xa4\xbb\x14\x4d\x16"
  20177. "\x28\xd1\xe9\xe9\xc8\xed\x80\x8b\x96\x2c\x54\xe5\xf9\x6d\x53\xda"
  20178. "\x14\x7a\x96\x38\xf9\x4a\x91\x75\xd8\xed\x61\x05\x5f\x0b\xa5\x73"
  20179. "\xa8\x2b\xb7\xe0\x18\xee\xda\xc4\xea\x7b\x36\x2e\xc8\x9c\x38\x2b"
  20180. vec.msgLen = 128;
  20181. #endif
  20182. vec.Qx = "12fbcaeffa6a51f3ee4d3d2b51c5dec6d7c726ca353fc014ea2bf7cfbb9b910d32cbfa6a00fe39b6cdb8946f22775398b2e233c0cf144d78c8a7742b5c7a3bb5d23";
  20183. vec.Qy = "09cdef823dd7bf9a79e8cceacd2e4527c231d0ae5967af0958e931d7ddccf2805a3e618dc3039fec9febbd33052fe4c0fee98f033106064982d88f4e03549d4a64d";
  20184. vec.d = "1bd56bd106118eda246155bd43b42b8e13f0a6e25dd3bb376026fab4dc92b6157bc6dfec2d15dd3d0cf2a39aa68494042af48ba9601118da82c6f2108a3a203ad74";
  20185. vec.R = "0bd117b4807710898f9dd7778056485777668f0e78e6ddf5b000356121eb7a220e9493c7f9a57c077947f89ac45d5acb6661bbcd17abb3faea149ba0aa3bb1521be";
  20186. vec.S = "019cd2c5c3f9870ecdeb9b323abdf3a98cd5e231d85c6ddc5b71ab190739f7f226e6b134ba1d5889ddeb2751dabd97911dff90c34684cdbe7bb669b6c3d22f2480c";
  20187. vec.curveName = "SECP521R1";
  20188. #ifndef NO_ASN
  20189. vec.r = (byte*)"\xbd\x11\x7b\x48\x07\x71\x08\x98\xf9\xdd\x77\x78"
  20190. "\x05\x64\x85\x77\x76\x68\xf0\xe7\x8e\x6d\xdf\x5b"
  20191. "\x00\x03\x56\x12\x1e\xb7\xa2\x20\xe9\x49\x3c\x7f"
  20192. "\x9a\x57\xc0\x77\x94\x7f\x89\xac\x45\xd5\xac\xb6"
  20193. "\x66\x1b\xbc\xd1\x7a\xbb\x3f\xae\xa1\x49\xba\x0a"
  20194. "\xa3\xbb\x15\x21\xbe";
  20195. vec.rSz = 65;
  20196. vec.s = (byte*)"\x19\xcd\x2c\x5c\x3f\x98\x70\xec\xde\xb9\xb3\x23"
  20197. "\xab\xdf\x3a\x98\xcd\x5e\x23\x1d\x85\xc6\xdd\xc5"
  20198. "\xb7\x1a\xb1\x90\x73\x9f\x7f\x22\x6e\x6b\x13\x4b"
  20199. "\xa1\xd5\x88\x9d\xde\xb2\x75\x1d\xab\xd9\x79\x11"
  20200. "\xdf\xf9\x0c\x34\x68\x4c\xdb\xe7\xbb\x66\x9b\x6c"
  20201. "\x3d\x22\xf2\x48\x0c";
  20202. vec.sSz = 65;
  20203. #endif
  20204. break;
  20205. #endif /* HAVE_ECC521 */
  20206. default:
  20207. return NOT_COMPILED_IN; /* Invalid key size / Not supported */
  20208. }; /* Switch */
  20209. ret = ecc_test_vector_item(&vec);
  20210. if (ret < 0) {
  20211. return ret;
  20212. }
  20213. return 0;
  20214. }
  20215. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  20216. #if defined(HAVE_ECC_SIGN) && (defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  20217. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)) \
  20218. && (!defined(FIPS_VERSION_GE) || FIPS_VERSION_GE(5,3))
  20219. #if defined(HAVE_ECC256)
  20220. static int ecc_test_deterministic_k(WC_RNG* rng)
  20221. {
  20222. int ret;
  20223. #ifdef WOLFSSL_SMALL_STACK
  20224. ecc_key *key = NULL;
  20225. #else
  20226. ecc_key key[1];
  20227. #endif
  20228. int key_inited = 0;
  20229. byte sig[72];
  20230. word32 sigSz;
  20231. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  20232. unsigned char hash[32];
  20233. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  20234. "C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721";
  20235. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  20236. "60FED4BA255A9D31C961EB74C6356D68C049B8923B61FA6CE669622E60F29FB6";
  20237. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  20238. "7903FE1008B8BC99A41AE9E95628BC64F2F1B20C2D7E9F5177A3C294D4462299";
  20239. WOLFSSL_SMALL_STACK_STATIC const byte expSig[] = {
  20240. 0x30, 0x46, 0x02, 0x21, 0x00, 0xEF, 0xD4, 0x8B,
  20241. 0x2A, 0xAC, 0xB6, 0xA8, 0xFD, 0x11, 0x40, 0xDD,
  20242. 0x9C, 0xD4, 0x5E, 0x81, 0xD6, 0x9D, 0x2C, 0x87,
  20243. 0x7B, 0x56, 0xAA, 0xF9, 0x91, 0xC3, 0x4D, 0x0E,
  20244. 0xA8, 0x4E, 0xAF, 0x37, 0x16, 0x02, 0x21, 0x00,
  20245. 0xF7, 0xCB, 0x1C, 0x94, 0x2D, 0x65, 0x7C, 0x41,
  20246. 0xD4, 0x36, 0xC7, 0xA1, 0xB6, 0xE2, 0x9F, 0x65,
  20247. 0xF3, 0xE9, 0x00, 0xDB, 0xB9, 0xAF, 0xF4, 0x06,
  20248. 0x4D, 0xC4, 0xAB, 0x2F, 0x84, 0x3A, 0xCD, 0xA8
  20249. };
  20250. #ifdef WOLFSSL_SMALL_STACK
  20251. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20252. if (key == NULL)
  20253. return MEMORY_E;
  20254. #endif
  20255. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  20256. if (ret != 0) {
  20257. goto done;
  20258. }
  20259. key_inited = 1;
  20260. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP256R1");
  20261. if (ret != 0) {
  20262. goto done;
  20263. }
  20264. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  20265. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  20266. if (ret != 0) {
  20267. goto done;
  20268. }
  20269. ret = wc_ecc_set_deterministic(key, 1);
  20270. if (ret != 0) {
  20271. goto done;
  20272. }
  20273. sigSz = sizeof(sig);
  20274. do {
  20275. #if defined(WOLFSSL_ASYNC_CRYPT)
  20276. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20277. #endif
  20278. if (ret == 0)
  20279. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  20280. } while (ret == WC_PENDING_E);
  20281. if (ret != 0) {
  20282. goto done;
  20283. }
  20284. TEST_SLEEP();
  20285. if (sigSz != sizeof(expSig)) {
  20286. ret = -9830;
  20287. goto done;
  20288. }
  20289. if (XMEMCMP(sig, expSig, sigSz) != 0) {
  20290. ret = -9831;
  20291. goto done;
  20292. }
  20293. sigSz = sizeof(sig);
  20294. do {
  20295. #if defined(WOLFSSL_ASYNC_CRYPT)
  20296. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20297. #endif
  20298. if (ret == 0)
  20299. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  20300. } while (ret == WC_PENDING_E);
  20301. if (ret != 0) {
  20302. goto done;
  20303. }
  20304. TEST_SLEEP();
  20305. done:
  20306. if (key_inited)
  20307. wc_ecc_free(key);
  20308. #ifdef WOLFSSL_SMALL_STACK
  20309. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20310. #endif
  20311. return ret;
  20312. }
  20313. #endif
  20314. #ifdef WOLFSSL_PUBLIC_MP
  20315. #if defined(HAVE_ECC384)
  20316. /* KAT from RFC6979 */
  20317. static int ecc384_test_deterministic_k(WC_RNG* rng)
  20318. {
  20319. int ret;
  20320. #ifdef WOLFSSL_SMALL_STACK
  20321. ecc_key *key;
  20322. mp_int *r, *s, *expR, *expS;
  20323. #else
  20324. ecc_key key[1];
  20325. mp_int r[1], s[1], expR[1], expS[1];
  20326. #endif
  20327. int key_inited = 0;
  20328. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  20329. unsigned char hash[32];
  20330. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  20331. "6B9D3DAD2E1B8C1C05B19875B6659F4DE23C3B667BF297BA9AA47740787137D8"
  20332. "96D5724E4C70A825F872C9EA60D2EDF5";
  20333. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  20334. "EC3A4E415B4E19A4568618029F427FA5DA9A8BC4AE92E02E06AAE5286B300C64"
  20335. "DEF8F0EA9055866064A254515480BC13";
  20336. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  20337. "8015D9B72D7D57244EA8EF9AC0C621896708A59367F9DFB9F54CA84B3F1C9DB1"
  20338. "288B231C3AE0D4FE7344FD2533264720";
  20339. WOLFSSL_SMALL_STACK_STATIC const char* expRstr =
  20340. "21B13D1E013C7FA1392D03C5F99AF8B30C570C6F98D4EA8E354B63A21D3DAA33"
  20341. "BDE1E888E63355D92FA2B3C36D8FB2CD";
  20342. WOLFSSL_SMALL_STACK_STATIC const char* expSstr =
  20343. "F3AA443FB107745BF4BD77CB3891674632068A10CA67E3D45DB2266FA7D1FEEB"
  20344. "EFDC63ECCD1AC42EC0CB8668A4FA0AB0";
  20345. #ifdef WOLFSSL_SMALL_STACK
  20346. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20347. r = (mp_int *)XMALLOC(sizeof(*r), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20348. s = (mp_int *)XMALLOC(sizeof(*s), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20349. expR = (mp_int *)XMALLOC(sizeof(*expR), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20350. expS = (mp_int *)XMALLOC(sizeof(*expS), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20351. if ((key == NULL) ||
  20352. (r == NULL) ||
  20353. (s == NULL) ||
  20354. (expR == NULL) ||
  20355. (expS == NULL))
  20356. {
  20357. ret = MEMORY_E;
  20358. goto done;
  20359. }
  20360. #endif
  20361. ret = mp_init_multi(r, s, expR, expS, NULL, NULL);
  20362. if (ret != MP_OKAY) {
  20363. goto done;
  20364. }
  20365. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  20366. if (ret != 0) {
  20367. goto done;
  20368. }
  20369. key_inited = 1;
  20370. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP384R1");
  20371. if (ret != 0) {
  20372. goto done;
  20373. }
  20374. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  20375. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  20376. if (ret != 0) {
  20377. goto done;
  20378. }
  20379. ret = wc_ecc_set_deterministic(key, 1);
  20380. if (ret != 0) {
  20381. goto done;
  20382. }
  20383. do {
  20384. #if defined(WOLFSSL_ASYNC_CRYPT)
  20385. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20386. #endif
  20387. if (ret == 0)
  20388. ret = wc_ecc_sign_hash_ex(hash, sizeof(hash), rng, key, r, s);
  20389. } while (ret == WC_PENDING_E);
  20390. if (ret != 0) {
  20391. goto done;
  20392. }
  20393. TEST_SLEEP();
  20394. mp_read_radix(expR, expRstr, MP_RADIX_HEX);
  20395. mp_read_radix(expS, expSstr, MP_RADIX_HEX);
  20396. if (mp_cmp(r, expR) != MP_EQ) {
  20397. ret = -1;
  20398. }
  20399. done:
  20400. if (key_inited)
  20401. wc_ecc_free(key);
  20402. #ifdef WOLFSSL_SMALL_STACK
  20403. if (key != NULL)
  20404. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20405. if (r != NULL)
  20406. XFREE(r, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20407. if (s != NULL)
  20408. XFREE(s, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20409. if (expR != NULL)
  20410. XFREE(expR, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20411. if (expS != NULL)
  20412. XFREE(expS, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20413. #endif
  20414. return ret;
  20415. }
  20416. #endif /* HAVE_ECC384 */
  20417. #if defined(HAVE_ECC521)
  20418. /* KAT from RFC6979 */
  20419. static int ecc521_test_deterministic_k(WC_RNG* rng)
  20420. {
  20421. int ret;
  20422. #ifdef WOLFSSL_SMALL_STACK
  20423. ecc_key *key;
  20424. mp_int *r, *s, *expR, *expS;
  20425. #else
  20426. ecc_key key[1];
  20427. mp_int r[1], s[1], expR[1], expS[1];
  20428. #endif
  20429. int key_inited = 0;
  20430. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  20431. unsigned char hash[32];
  20432. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  20433. "0FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75C"
  20434. "AA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83"
  20435. "538";
  20436. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  20437. "1894550D0785932E00EAA23B694F213F8C3121F86DC97A04E5A7167DB4E5BCD3"
  20438. "71123D46E45DB6B5D5370A7F20FB633155D38FFA16D2BD761DCAC474B9A2F502"
  20439. "3A4";
  20440. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  20441. "0493101C962CD4D2FDDF782285E64584139C2F91B47F87FF82354D6630F746A2"
  20442. "8A0DB25741B5B34A828008B22ACC23F924FAAFBD4D33F81EA66956DFEAA2BFDF"
  20443. "CF5";
  20444. WOLFSSL_SMALL_STACK_STATIC const char* expRstr =
  20445. "1511BB4D675114FE266FC4372B87682BAECC01D3CC62CF2303C92B3526012659"
  20446. "D16876E25C7C1E57648F23B73564D67F61C6F14D527D54972810421E7D87589E"
  20447. "1A7";
  20448. WOLFSSL_SMALL_STACK_STATIC const char* expSstr =
  20449. "04A171143A83163D6DF460AAF61522695F207A58B95C0644D87E52AA1A347916"
  20450. "E4F7A72930B1BC06DBE22CE3F58264AFD23704CBB63B29B931F7DE6C9D949A7E"
  20451. "CFC";
  20452. #ifdef WOLFSSL_SMALL_STACK
  20453. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20454. r = (mp_int *)XMALLOC(sizeof(*r), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20455. s = (mp_int *)XMALLOC(sizeof(*s), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20456. expR = (mp_int *)XMALLOC(sizeof(*expR), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20457. expS = (mp_int *)XMALLOC(sizeof(*expS), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20458. if ((key == NULL) ||
  20459. (r == NULL) ||
  20460. (s == NULL) ||
  20461. (expR == NULL) ||
  20462. (expS == NULL))
  20463. {
  20464. ret = MEMORY_E;
  20465. goto done;
  20466. }
  20467. #endif
  20468. ret = mp_init_multi(r, s, expR, expS, NULL, NULL);
  20469. if (ret != MP_OKAY) {
  20470. goto done;
  20471. }
  20472. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  20473. if (ret != 0) {
  20474. return ret;
  20475. }
  20476. key_inited = 1;
  20477. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP521R1");
  20478. if (ret != 0) {
  20479. goto done;
  20480. }
  20481. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  20482. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  20483. if (ret != 0) {
  20484. goto done;
  20485. }
  20486. ret = wc_ecc_set_deterministic(key, 1);
  20487. if (ret != 0) {
  20488. goto done;
  20489. }
  20490. do {
  20491. #if defined(WOLFSSL_ASYNC_CRYPT)
  20492. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20493. #endif
  20494. if (ret == 0)
  20495. ret = wc_ecc_sign_hash_ex(hash, sizeof(hash), rng, key, r, s);
  20496. } while (ret == WC_PENDING_E);
  20497. if (ret != 0) {
  20498. goto done;
  20499. }
  20500. TEST_SLEEP();
  20501. mp_read_radix(expR, expRstr, MP_RADIX_HEX);
  20502. mp_read_radix(expS, expSstr, MP_RADIX_HEX);
  20503. if (mp_cmp(r, expR) != MP_EQ) {
  20504. ret = -1;
  20505. }
  20506. done:
  20507. if (key_inited)
  20508. wc_ecc_free(key);
  20509. #ifdef WOLFSSL_SMALL_STACK
  20510. if (key != NULL)
  20511. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20512. if (r != NULL)
  20513. XFREE(r, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20514. if (s != NULL)
  20515. XFREE(s, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20516. if (expR != NULL)
  20517. XFREE(expR, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20518. if (expS != NULL)
  20519. XFREE(expS, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20520. #endif
  20521. return ret;
  20522. }
  20523. #endif /* HAVE_ECC521 */
  20524. #endif /* WOLFSSL_PUBLIC_MP */
  20525. #endif /* HAVE_ECC_SIGN && (WOLFSSL_ECDSA_DETERMINISTIC_K ||
  20526. WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  20527. && (!FIPS_VERSION_GE || FIPS_VERSION_GE(5,3)) */
  20528. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K) && \
  20529. !defined(WOLFSSL_KCAPI_ECC)
  20530. static int ecc_test_sign_vectors(WC_RNG* rng)
  20531. {
  20532. int ret;
  20533. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20534. ecc_key *key = NULL;
  20535. #else
  20536. ecc_key key[1];
  20537. #endif
  20538. int key_inited = 0;
  20539. byte sig[72];
  20540. word32 sigSz;
  20541. WOLFSSL_SMALL_STACK_STATIC const unsigned char hash[32] = "test wolfSSL deterministic sign";
  20542. WOLFSSL_SMALL_STACK_STATIC const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  20543. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  20544. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  20545. WOLFSSL_SMALL_STACK_STATIC const byte k[1] = { 0x02 };
  20546. WOLFSSL_SMALL_STACK_STATIC const byte expSig[71] = {
  20547. 0x30, 0x45, 0x02, 0x20, 0x7c, 0xf2, 0x7b, 0x18,
  20548. 0x8d, 0x03, 0x4f, 0x7e, 0x8a, 0x52, 0x38, 0x03,
  20549. 0x04, 0xb5, 0x1a, 0xc3, 0xc0, 0x89, 0x69, 0xe2,
  20550. 0x77, 0xf2, 0x1b, 0x35, 0xa6, 0x0b, 0x48, 0xfc,
  20551. 0x47, 0x66, 0x99, 0x78, 0x02, 0x21, 0x00, 0xa8,
  20552. 0x43, 0xa0, 0xce, 0x6c, 0x5e, 0x17, 0x8a, 0x53,
  20553. 0x4d, 0xaf, 0xd2, 0x95, 0x78, 0x9f, 0x84, 0x4f,
  20554. 0x94, 0xb8, 0x75, 0xa3, 0x19, 0xa5, 0xd4, 0xdf,
  20555. 0xe1, 0xd4, 0x5e, 0x9d, 0x97, 0xfe, 0x81
  20556. };
  20557. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20558. if ((key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  20559. return MEMORY_E;
  20560. #endif
  20561. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  20562. if (ret != 0) {
  20563. goto done;
  20564. }
  20565. key_inited = 1;
  20566. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP256R1");
  20567. if (ret != 0) {
  20568. goto done;
  20569. }
  20570. wc_ecc_set_flags(key, WC_ECC_FLAG_DEC_SIGN);
  20571. ret = wc_ecc_sign_set_k(k, sizeof(k), key);
  20572. if (ret != 0) {
  20573. goto done;
  20574. }
  20575. sigSz = sizeof(sig);
  20576. do {
  20577. #if defined(WOLFSSL_ASYNC_CRYPT)
  20578. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20579. #endif
  20580. if (ret == 0)
  20581. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  20582. } while (ret == WC_PENDING_E);
  20583. if (ret != 0) {
  20584. goto done;
  20585. }
  20586. TEST_SLEEP();
  20587. if (sigSz != sizeof(expSig)) {
  20588. ret = -9830;
  20589. goto done;
  20590. }
  20591. if (XMEMCMP(sig, expSig, sigSz) != 0) {
  20592. ret = -9831;
  20593. goto done;
  20594. }
  20595. sigSz = sizeof(sig);
  20596. do {
  20597. #if defined(WOLFSSL_ASYNC_CRYPT)
  20598. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20599. #endif
  20600. if (ret == 0)
  20601. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  20602. } while (ret == WC_PENDING_E);
  20603. if (ret != 0) {
  20604. goto done;
  20605. }
  20606. TEST_SLEEP();
  20607. done:
  20608. if (key_inited)
  20609. wc_ecc_free(key);
  20610. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20611. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20612. #endif
  20613. return ret;
  20614. }
  20615. #endif
  20616. #ifdef HAVE_ECC_CDH
  20617. static int ecc_test_cdh_vectors(WC_RNG* rng)
  20618. {
  20619. int ret;
  20620. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20621. ecc_key *pub_key = (ecc_key *)XMALLOC(sizeof *pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20622. ecc_key *priv_key = (ecc_key *)XMALLOC(sizeof *priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20623. #else
  20624. ecc_key pub_key[1], priv_key[1];
  20625. #endif
  20626. byte sharedA[32] = {0}, sharedB[32] = {0};
  20627. word32 x, z;
  20628. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSx = "700c48f77f56584c5cc632ca65640db91b6bacce3a4df6b42ce7cc838833d287";
  20629. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSy = "db71e509e3fd9b060ddb20ba5c51dcc5948d46fbf640dfe0441782cab85fa4ac";
  20630. WOLFSSL_SMALL_STACK_STATIC const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  20631. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  20632. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  20633. WOLFSSL_SMALL_STACK_STATIC const char* ZIUT = "46fc62106420ff012e54a434fbdd2d25ccc5852060561e68040dd7778997bd7b";
  20634. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20635. if ((pub_key == NULL) ||
  20636. (priv_key == NULL)) {
  20637. ret = MEMORY_E;
  20638. goto done;
  20639. }
  20640. #endif
  20641. XMEMSET(pub_key, 0, sizeof *pub_key);
  20642. XMEMSET(priv_key, 0, sizeof *priv_key);
  20643. /* setup private and public keys */
  20644. ret = wc_ecc_init_ex(pub_key, HEAP_HINT, devId);
  20645. if (ret != 0)
  20646. goto done;
  20647. ret = wc_ecc_init_ex(priv_key, HEAP_HINT, devId);
  20648. if (ret != 0)
  20649. goto done;
  20650. wc_ecc_set_flags(pub_key, WC_ECC_FLAG_COFACTOR);
  20651. wc_ecc_set_flags(priv_key, WC_ECC_FLAG_COFACTOR);
  20652. ret = wc_ecc_import_raw(pub_key, QCAVSx, QCAVSy, NULL, "SECP256R1");
  20653. if (ret != 0)
  20654. goto done;
  20655. ret = wc_ecc_import_raw(priv_key, QIUTx, QIUTy, dIUT, "SECP256R1");
  20656. if (ret != 0)
  20657. goto done;
  20658. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  20659. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  20660. !defined(HAVE_SELFTEST)
  20661. ret = wc_ecc_set_rng(priv_key, rng);
  20662. if (ret != 0)
  20663. goto done;
  20664. #else
  20665. (void)rng;
  20666. #endif
  20667. /* compute ECC Cofactor shared secret */
  20668. x = sizeof(sharedA);
  20669. do {
  20670. #if defined(WOLFSSL_ASYNC_CRYPT)
  20671. ret = wc_AsyncWait(ret, &priv_key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20672. #endif
  20673. if (ret == 0)
  20674. ret = wc_ecc_shared_secret(priv_key, pub_key, sharedA, &x);
  20675. } while (ret == WC_PENDING_E);
  20676. if (ret != 0) {
  20677. goto done;
  20678. }
  20679. TEST_SLEEP();
  20680. /* read in expected Z */
  20681. z = sizeof(sharedB);
  20682. ret = Base16_Decode((const byte*)ZIUT, (word32)XSTRLEN(ZIUT), sharedB, &z);
  20683. if (ret != 0)
  20684. goto done;
  20685. /* compare results */
  20686. if (x != z || XMEMCMP(sharedA, sharedB, x)) {
  20687. ERROR_OUT(-9840, done);
  20688. }
  20689. done:
  20690. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20691. if (priv_key) {
  20692. wc_ecc_free(priv_key);
  20693. XFREE(priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20694. }
  20695. if (pub_key) {
  20696. wc_ecc_free(pub_key);
  20697. XFREE(pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20698. }
  20699. #else
  20700. wc_ecc_free(priv_key);
  20701. wc_ecc_free(pub_key);
  20702. #endif
  20703. return ret;
  20704. }
  20705. #endif /* HAVE_ECC_CDH */
  20706. #endif /* HAVE_ECC_VECTOR_TEST */
  20707. #ifdef HAVE_ECC_KEY_IMPORT
  20708. /* returns 0 on success */
  20709. static int ecc_test_make_pub(WC_RNG* rng)
  20710. {
  20711. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20712. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20713. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  20714. ecc_key *pub = (ecc_key *)XMALLOC(sizeof *pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20715. #endif
  20716. byte *exportBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20717. byte *tmp = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20718. #else
  20719. ecc_key key[1];
  20720. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  20721. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  20722. ecc_key pub[1];
  20723. #endif
  20724. byte exportBuf[ECC_BUFSIZE];
  20725. byte tmp[ECC_BUFSIZE];
  20726. #endif
  20727. const byte* msg = (const byte*)"test wolfSSL ECC public gen";
  20728. word32 x;
  20729. word32 tmpSz;
  20730. int ret = 0;
  20731. ecc_point* pubPoint = NULL;
  20732. #ifdef HAVE_ECC_VERIFY
  20733. int verify = 0;
  20734. #endif
  20735. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20736. if ((key == NULL) ||
  20737. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  20738. (pub == NULL) ||
  20739. #endif
  20740. (exportBuf == NULL) ||
  20741. (tmp == NULL))
  20742. ERROR_OUT(MEMORY_E, done);
  20743. #endif
  20744. (void)msg;
  20745. (void)verify;
  20746. (void)exportBuf;
  20747. (void)rng;
  20748. wc_ecc_init_ex(key, HEAP_HINT, devId);
  20749. #ifndef NO_ECC256
  20750. #ifdef USE_CERT_BUFFERS_256
  20751. XMEMCPY(tmp, ecc_key_der_256, (size_t)sizeof_ecc_key_der_256);
  20752. tmpSz = (size_t)sizeof_ecc_key_der_256;
  20753. #else
  20754. {
  20755. XFILE file = XFOPEN(eccKeyDerFile, "rb");
  20756. if (!file) {
  20757. ERROR_OUT(-9850, done);
  20758. }
  20759. tmpSz = (word32)XFREAD(tmp, 1, ECC_BUFSIZE, file);
  20760. XFCLOSE(file);
  20761. }
  20762. #endif /* USE_CERT_BUFFERS_256 */
  20763. /* import private only then test with */
  20764. ret = wc_ecc_import_private_key(tmp, tmpSz, NULL, 0, NULL);
  20765. if (ret == 0) {
  20766. ERROR_OUT(-9851, done);
  20767. }
  20768. ret = wc_ecc_import_private_key(NULL, tmpSz, NULL, 0, key);
  20769. if (ret == 0) {
  20770. ERROR_OUT(-9852, done);
  20771. }
  20772. x = 0;
  20773. ret = wc_EccPrivateKeyDecode(tmp, &x, key, tmpSz);
  20774. if (ret != 0) {
  20775. ERROR_OUT(-9853, done);
  20776. }
  20777. #ifdef HAVE_ECC_KEY_EXPORT
  20778. x = ECC_BUFSIZE;
  20779. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  20780. if (ret != 0) {
  20781. ERROR_OUT(-9854, done);
  20782. }
  20783. /* make private only key */
  20784. wc_ecc_free(key);
  20785. wc_ecc_init_ex(key, HEAP_HINT, devId);
  20786. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  20787. if (ret != 0) {
  20788. ERROR_OUT(-9855, done);
  20789. }
  20790. x = ECC_BUFSIZE;
  20791. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  20792. if (ret == 0) {
  20793. ERROR_OUT(-9856, done);
  20794. }
  20795. #endif /* HAVE_ECC_KEY_EXPORT */
  20796. ret = wc_ecc_make_pub(NULL, NULL);
  20797. if (ret == 0) {
  20798. ERROR_OUT(-9857, done);
  20799. }
  20800. TEST_SLEEP();
  20801. #ifndef WOLFSSL_NO_MALLOC
  20802. pubPoint = wc_ecc_new_point_h(HEAP_HINT);
  20803. if (pubPoint == NULL) {
  20804. ERROR_OUT(-9858, done);
  20805. }
  20806. #if !defined(WOLFSSL_CRYPTOCELL)
  20807. ret = wc_ecc_make_pub(key, pubPoint);
  20808. #if defined(WOLFSSL_ASYNC_CRYPT)
  20809. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  20810. #endif
  20811. if (ret != 0) {
  20812. ERROR_OUT(-9859, done);
  20813. }
  20814. #endif
  20815. TEST_SLEEP();
  20816. #ifdef HAVE_ECC_KEY_EXPORT
  20817. /* export should still fail, is private only key */
  20818. x = ECC_BUFSIZE;
  20819. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  20820. if (ret == 0) {
  20821. ERROR_OUT(-9860, done);
  20822. }
  20823. #endif /* HAVE_ECC_KEY_EXPORT */
  20824. #endif /* !WOLFSSL_NO_MALLOC */
  20825. #endif /* !NO_ECC256 */
  20826. /* create a new key since above test for loading key is not supported */
  20827. #if defined(WOLFSSL_CRYPTOCELL) || defined(NO_ECC256) || \
  20828. defined(WOLFSSL_QNX_CAAM) || defined(WOLFSSL_SE050) || \
  20829. defined(WOLFSSL_SECO_CAAM)
  20830. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  20831. if (ret != 0) {
  20832. ERROR_OUT(-9861, done);
  20833. }
  20834. #endif
  20835. #if defined(HAVE_ECC_SIGN) && (!defined(ECC_TIMING_RESISTANT) || \
  20836. (defined(ECC_TIMING_RESISTANT) && !defined(WC_NO_RNG))) && \
  20837. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(HAVE_ECC_DETERMINISTIC_K)
  20838. tmpSz = ECC_BUFSIZE;
  20839. ret = 0;
  20840. do {
  20841. #if defined(WOLFSSL_ASYNC_CRYPT)
  20842. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20843. #endif
  20844. if (ret == 0) {
  20845. ret = wc_ecc_sign_hash(msg, (word32)XSTRLEN((const char* )msg), tmp,
  20846. &tmpSz, rng, key);
  20847. }
  20848. } while (ret == WC_PENDING_E);
  20849. if (ret != 0) {
  20850. ERROR_OUT(-9862, done);
  20851. }
  20852. TEST_SLEEP();
  20853. #ifdef HAVE_ECC_VERIFY
  20854. /* try verify with private only key */
  20855. ret = 0;
  20856. do {
  20857. #if defined(WOLFSSL_ASYNC_CRYPT)
  20858. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20859. #endif
  20860. if (ret == 0) {
  20861. ret = wc_ecc_verify_hash(tmp, tmpSz, msg,
  20862. (word32)XSTRLEN((const char*)msg), &verify, key);
  20863. }
  20864. } while (ret == WC_PENDING_E);
  20865. if (ret != 0) {
  20866. ERROR_OUT(-9863, done);
  20867. }
  20868. if (verify != 1) {
  20869. ERROR_OUT(-9864, done);
  20870. }
  20871. TEST_SLEEP();
  20872. #ifdef HAVE_ECC_KEY_EXPORT
  20873. /* exporting the public part should now work */
  20874. x = ECC_BUFSIZE;
  20875. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  20876. if (ret != 0) {
  20877. ERROR_OUT(-9865, done);
  20878. }
  20879. #endif /* HAVE_ECC_KEY_EXPORT */
  20880. #endif /* HAVE_ECC_VERIFY */
  20881. #endif /* HAVE_ECC_SIGN */
  20882. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  20883. /* now test private only key with creating a shared secret */
  20884. x = ECC_BUFSIZE;
  20885. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  20886. if (ret != 0) {
  20887. ERROR_OUT(-9866, done);
  20888. }
  20889. #if !defined(WOLFSSL_QNX_CAAM) && !defined(WOLFSSL_SE050)
  20890. /* make private only key */
  20891. wc_ecc_free(key);
  20892. wc_ecc_init_ex(key, HEAP_HINT, devId);
  20893. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  20894. if (ret != 0) {
  20895. ERROR_OUT(-9867, done);
  20896. }
  20897. /* check that public export fails with private only key */
  20898. x = ECC_BUFSIZE;
  20899. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  20900. if (ret == 0) {
  20901. ERROR_OUT(-9868, done);
  20902. }
  20903. #endif /* WOLFSSL_QNX_CAAM */
  20904. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  20905. /* make public key for shared secret */
  20906. wc_ecc_init_ex(pub, HEAP_HINT, devId);
  20907. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, pub);
  20908. #ifdef HAVE_ECC_CDH
  20909. wc_ecc_set_flags(key, WC_ECC_FLAG_COFACTOR);
  20910. #endif
  20911. #if defined(WOLFSSL_ASYNC_CRYPT)
  20912. ret = wc_AsyncWait(ret, &pub->asyncDev, WC_ASYNC_FLAG_NONE);
  20913. #endif
  20914. if (ret != 0) {
  20915. ERROR_OUT(-9869, done);
  20916. }
  20917. TEST_SLEEP();
  20918. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  20919. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  20920. !defined(HAVE_SELFTEST)
  20921. ret = wc_ecc_set_rng(key, rng);
  20922. if (ret != 0)
  20923. goto done;
  20924. #endif
  20925. x = ECC_BUFSIZE;
  20926. do {
  20927. #if defined(WOLFSSL_ASYNC_CRYPT)
  20928. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20929. #endif
  20930. if (ret == 0) {
  20931. ret = wc_ecc_shared_secret(key, pub, exportBuf, &x);
  20932. }
  20933. } while (ret == WC_PENDING_E);
  20934. wc_ecc_free(pub);
  20935. if (ret != 0) {
  20936. ERROR_OUT(-9870, done);
  20937. }
  20938. TEST_SLEEP();
  20939. #endif /* HAVE_ECC_DHE && HAVE_ECC_KEY_EXPORT && !WC_NO_RNG */
  20940. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  20941. ret = 0;
  20942. done:
  20943. wc_ecc_del_point_h(pubPoint, HEAP_HINT);
  20944. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20945. if (key != NULL) {
  20946. wc_ecc_free(key);
  20947. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20948. }
  20949. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  20950. if (pub != NULL)
  20951. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20952. #endif
  20953. if (exportBuf != NULL)
  20954. XFREE(exportBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20955. if (tmp != NULL)
  20956. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20957. #else
  20958. wc_ecc_free(key);
  20959. #endif
  20960. return ret;
  20961. }
  20962. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && \
  20963. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  20964. static int ecc_test_key_decode(WC_RNG* rng, int keySize)
  20965. {
  20966. int ret;
  20967. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20968. ecc_key *eccKey = (ecc_key *)XMALLOC(sizeof *eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20969. byte *tmpBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20970. #else
  20971. ecc_key eccKey[1];
  20972. byte tmpBuf[ECC_BUFSIZE];
  20973. #endif
  20974. word32 tmpSz;
  20975. word32 idx;
  20976. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20977. if ((eccKey == NULL) || (tmpBuf == NULL))
  20978. ERROR_OUT(MEMORY_E, done);
  20979. #endif
  20980. ret = wc_ecc_init(eccKey);
  20981. if (ret != 0) {
  20982. goto done;
  20983. }
  20984. ret = wc_ecc_make_key(rng, keySize, eccKey);
  20985. #if defined(WOLFSSL_ASYNC_CRYPT)
  20986. ret = wc_AsyncWait(ret, &eccKey->asyncDev, WC_ASYNC_FLAG_NONE);
  20987. #endif
  20988. if (ret != 0) {
  20989. goto done;
  20990. }
  20991. tmpSz = ECC_BUFSIZE;
  20992. ret = wc_EccKeyToDer(eccKey, tmpBuf, tmpSz);
  20993. wc_ecc_free(eccKey);
  20994. if (ret < 0) {
  20995. goto done;
  20996. }
  20997. tmpSz = ret;
  20998. ret = wc_ecc_init(eccKey);
  20999. if (ret != 0) {
  21000. goto done;
  21001. }
  21002. idx = 0;
  21003. ret = wc_EccPrivateKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  21004. if (ret != 0) {
  21005. goto done;
  21006. }
  21007. wc_ecc_free(eccKey);
  21008. ret = wc_ecc_init(eccKey);
  21009. if (ret != 0) {
  21010. goto done;
  21011. }
  21012. idx = 0;
  21013. ret = wc_EccPublicKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  21014. if (ret != 0) {
  21015. goto done;
  21016. }
  21017. ret = 0;
  21018. done:
  21019. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21020. if (eccKey != NULL) {
  21021. wc_ecc_free(eccKey);
  21022. XFREE(eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21023. }
  21024. if (tmpBuf != NULL)
  21025. XFREE(tmpBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21026. #else
  21027. wc_ecc_free(eccKey);
  21028. #endif
  21029. return ret;
  21030. }
  21031. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  21032. #endif /* HAVE_ECC_KEY_IMPORT */
  21033. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && \
  21034. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21035. static int ecc_test_key_gen(WC_RNG* rng, int keySize)
  21036. {
  21037. int ret = 0;
  21038. int derSz;
  21039. #ifdef HAVE_PKCS8
  21040. word32 pkcs8Sz;
  21041. #endif
  21042. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21043. byte *der = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21044. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21045. #else
  21046. byte der[ECC_BUFSIZE];
  21047. ecc_key userA[1];
  21048. #endif
  21049. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21050. if ((der == NULL) || (userA == NULL))
  21051. ERROR_OUT(MEMORY_E, done);
  21052. #endif
  21053. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  21054. if (ret != 0)
  21055. goto done;
  21056. ret = wc_ecc_make_key(rng, keySize, userA);
  21057. #if defined(WOLFSSL_ASYNC_CRYPT)
  21058. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  21059. #endif
  21060. if (ret != 0)
  21061. goto done;
  21062. TEST_SLEEP();
  21063. ret = wc_ecc_check_key(userA);
  21064. if (ret != 0)
  21065. goto done;
  21066. TEST_SLEEP();
  21067. derSz = wc_EccKeyToDer(userA, der, ECC_BUFSIZE);
  21068. if (derSz < 0) {
  21069. ERROR_OUT(derSz, done);
  21070. }
  21071. ret = SaveDerAndPem(der, derSz, eccCaKeyTempFile, eccCaKeyPemFile,
  21072. ECC_PRIVATEKEY_TYPE, -8347);
  21073. if (ret != 0) {
  21074. goto done;
  21075. }
  21076. /* test export of public key */
  21077. derSz = wc_EccPublicKeyToDer(userA, der, ECC_BUFSIZE, 1);
  21078. if (derSz < 0) {
  21079. ERROR_OUT(derSz, done);
  21080. }
  21081. if (derSz == 0) {
  21082. ERROR_OUT(-9890, done);
  21083. }
  21084. #ifdef HAVE_COMP_KEY
  21085. /* test export of compressed public key */
  21086. derSz = wc_EccPublicKeyToDer_ex(userA, der, ECC_BUFSIZE, 1, 1);
  21087. if (derSz < 0) {
  21088. ERROR_OUT(derSz, done);
  21089. }
  21090. if (derSz == 0) {
  21091. ERROR_OUT(-9890, done);
  21092. }
  21093. #endif
  21094. ret = SaveDerAndPem(der, derSz, eccPubKeyDerFile, NULL, 0, -8348);
  21095. if (ret != 0) {
  21096. goto done;
  21097. }
  21098. #ifdef HAVE_PKCS8
  21099. /* test export of PKCS#8 unencrypted private key */
  21100. pkcs8Sz = FOURK_BUF;
  21101. derSz = wc_EccPrivateKeyToPKCS8(userA, der, &pkcs8Sz);
  21102. if (derSz < 0) {
  21103. ERROR_OUT(derSz, done);
  21104. }
  21105. if (derSz == 0) {
  21106. ERROR_OUT(-9891, done);
  21107. }
  21108. ret = SaveDerAndPem(der, derSz, eccPkcs8KeyDerFile, NULL, 0, -8349);
  21109. if (ret != 0) {
  21110. goto done;
  21111. }
  21112. #endif /* HAVE_PKCS8 */
  21113. done:
  21114. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21115. if (der != NULL)
  21116. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21117. if (userA != NULL) {
  21118. wc_ecc_free(userA);
  21119. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21120. }
  21121. #else
  21122. wc_ecc_free(userA);
  21123. #endif
  21124. return ret;
  21125. }
  21126. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  21127. static int ecc_test_curve_size(WC_RNG* rng, int keySize, int testVerifyCount,
  21128. int curve_id, const ecc_set_type* dp)
  21129. {
  21130. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG) && \
  21131. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  21132. WC_DECLARE_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  21133. WC_DECLARE_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  21134. #endif
  21135. #ifdef HAVE_ECC_KEY_EXPORT
  21136. #define ECC_KEY_EXPORT_BUF_SIZE (MAX_ECC_BYTES * 2 + 32)
  21137. WC_DECLARE_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  21138. #endif
  21139. word32 x = 0;
  21140. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG) && \
  21141. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  21142. word32 y;
  21143. #endif
  21144. #if defined(HAVE_ECC_SIGN) && !defined(WOLFSSL_KCAPI_ECC)
  21145. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  21146. WC_DECLARE_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  21147. int i;
  21148. #ifdef HAVE_ECC_VERIFY
  21149. int verify;
  21150. #endif /* HAVE_ECC_VERIFY */
  21151. #endif /* HAVE_ECC_SIGN */
  21152. int ret;
  21153. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21154. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21155. ecc_key *userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21156. ecc_key *pubKey = (ecc_key *)XMALLOC(sizeof *pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21157. #else
  21158. ecc_key userA[1];
  21159. ecc_key userB[1];
  21160. ecc_key pubKey[1];
  21161. #endif
  21162. #ifndef WC_NO_RNG
  21163. int curveSize;
  21164. #endif
  21165. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  21166. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG) && \
  21167. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  21168. if (sharedA == NULL || sharedB == NULL)
  21169. ERROR_OUT(-9900, done);
  21170. #endif
  21171. #ifdef HAVE_ECC_KEY_EXPORT
  21172. if (exportBuf == NULL)
  21173. ERROR_OUT(-9901, done);
  21174. #endif
  21175. #if defined(HAVE_ECC_SIGN) && !defined(WOLFSSL_KCAPI_ECC)
  21176. if (sig == NULL || digest == NULL)
  21177. ERROR_OUT(-9902, done);
  21178. #endif
  21179. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  21180. (void)testVerifyCount;
  21181. (void)dp;
  21182. (void)x;
  21183. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21184. if ((userA == NULL) ||
  21185. (userB == NULL) ||
  21186. (pubKey == NULL))
  21187. ERROR_OUT(-9903, done);
  21188. #endif
  21189. XMEMSET(userA, 0, sizeof *userA);
  21190. XMEMSET(userB, 0, sizeof *userB);
  21191. XMEMSET(pubKey, 0, sizeof *pubKey);
  21192. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  21193. if (ret != 0)
  21194. ERROR_OUT(-9904, done);
  21195. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  21196. if (ret != 0)
  21197. ERROR_OUT(-9905, done);
  21198. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  21199. if (ret != 0)
  21200. ERROR_OUT(-9906, done);
  21201. #ifdef WOLFSSL_CUSTOM_CURVES
  21202. if (dp != NULL) {
  21203. ret = wc_ecc_set_custom_curve(userA, dp);
  21204. if (ret != 0)
  21205. ERROR_OUT(-9907, done);
  21206. ret = wc_ecc_set_custom_curve(userB, dp);
  21207. if (ret != 0)
  21208. ERROR_OUT(-9908, done);
  21209. }
  21210. #endif
  21211. #ifndef WC_NO_RNG
  21212. ret = wc_ecc_make_key_ex(rng, keySize, userA, curve_id);
  21213. #if defined(WOLFSSL_ASYNC_CRYPT)
  21214. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  21215. #endif
  21216. #ifdef WOLF_CRYPTO_CB_ONLY_ECC
  21217. if (ret == NO_VALID_DEVID) {
  21218. ret = 0;
  21219. goto done; /* no software case */
  21220. }
  21221. #endif
  21222. if (ret == ECC_CURVE_OID_E)
  21223. goto done; /* catch case, where curve is not supported */
  21224. if (ret != 0)
  21225. ERROR_OUT(-9910, done);
  21226. TEST_SLEEP();
  21227. if (wc_ecc_get_curve_idx(curve_id) != -1) {
  21228. curveSize = wc_ecc_get_curve_size_from_id(userA->dp->id);
  21229. if (curveSize != userA->dp->size)
  21230. ERROR_OUT(-9911, done);
  21231. }
  21232. ret = wc_ecc_check_key(userA);
  21233. if (ret != 0)
  21234. ERROR_OUT(-9912, done);
  21235. TEST_SLEEP();
  21236. /* ATECC508/608 configuration may not support more than one ECDH key */
  21237. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  21238. ret = wc_ecc_make_key_ex(rng, keySize, userB, curve_id);
  21239. #if defined(WOLFSSL_ASYNC_CRYPT)
  21240. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  21241. #endif
  21242. if (ret != 0)
  21243. ERROR_OUT(-9914, done);
  21244. TEST_SLEEP();
  21245. /* only perform the below tests if the key size matches */
  21246. if (dp == NULL && keySize > 0 && wc_ecc_size(userA) != keySize)
  21247. ERROR_OUT(ECC_CURVE_OID_E, done);
  21248. #ifdef HAVE_ECC_DHE
  21249. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21250. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21251. !defined(HAVE_SELFTEST)
  21252. ret = wc_ecc_set_rng(userA, rng);
  21253. if (ret != 0)
  21254. ERROR_OUT(-9915, done);
  21255. ret = wc_ecc_set_rng(userB, rng);
  21256. if (ret != 0)
  21257. ERROR_OUT(-9916, done);
  21258. #endif
  21259. x = ECC_SHARED_SIZE;
  21260. do {
  21261. #if defined(WOLFSSL_ASYNC_CRYPT)
  21262. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21263. #endif
  21264. if (ret == 0)
  21265. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  21266. } while (ret == WC_PENDING_E);
  21267. if (ret != 0) {
  21268. ERROR_OUT(-9917, done);
  21269. }
  21270. TEST_SLEEP();
  21271. y = ECC_SHARED_SIZE;
  21272. do {
  21273. #if defined(WOLFSSL_ASYNC_CRYPT)
  21274. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21275. #endif
  21276. if (ret == 0)
  21277. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  21278. } while (ret == WC_PENDING_E);
  21279. if (ret != 0)
  21280. ERROR_OUT(-9918, done);
  21281. if (y != x)
  21282. ERROR_OUT(-9919, done);
  21283. if (XMEMCMP(sharedA, sharedB, x))
  21284. ERROR_OUT(-9920, done);
  21285. TEST_SLEEP();
  21286. #endif /* HAVE_ECC_DHE */
  21287. #ifdef HAVE_ECC_CDH
  21288. /* add cofactor flag */
  21289. wc_ecc_set_flags(userA, WC_ECC_FLAG_COFACTOR);
  21290. wc_ecc_set_flags(userB, WC_ECC_FLAG_COFACTOR);
  21291. x = ECC_SHARED_SIZE;
  21292. do {
  21293. #if defined(WOLFSSL_ASYNC_CRYPT)
  21294. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21295. #endif
  21296. if (ret == 0)
  21297. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  21298. } while (ret == WC_PENDING_E);
  21299. if (ret != 0)
  21300. ERROR_OUT(-9921, done);
  21301. TEST_SLEEP();
  21302. y = ECC_SHARED_SIZE;
  21303. do {
  21304. #if defined(WOLFSSL_ASYNC_CRYPT)
  21305. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21306. #endif
  21307. if (ret == 0)
  21308. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  21309. } while (ret == WC_PENDING_E);
  21310. if (ret != 0)
  21311. ERROR_OUT(-9922, done);
  21312. if (y != x)
  21313. ERROR_OUT(-9923, done);
  21314. if (XMEMCMP(sharedA, sharedB, x))
  21315. ERROR_OUT(-9924, done);
  21316. TEST_SLEEP();
  21317. /* remove cofactor flag */
  21318. wc_ecc_set_flags(userA, 0);
  21319. wc_ecc_set_flags(userB, 0);
  21320. #endif /* HAVE_ECC_CDH */
  21321. #endif /* !WOLFSSL_ATECC508A && WOLFSSL_ATECC608A */
  21322. #ifdef HAVE_ECC_KEY_EXPORT
  21323. x = ECC_KEY_EXPORT_BUF_SIZE;
  21324. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 0);
  21325. if (ret != 0)
  21326. ERROR_OUT(-9925, done);
  21327. #ifdef HAVE_ECC_KEY_IMPORT
  21328. #ifdef WOLFSSL_CUSTOM_CURVES
  21329. if (dp != NULL) {
  21330. ret = wc_ecc_set_custom_curve(pubKey, dp);
  21331. if (ret != 0)
  21332. ERROR_OUT(-9926, done);
  21333. }
  21334. #endif
  21335. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  21336. if (ret != 0)
  21337. ERROR_OUT(-9927, done);
  21338. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  21339. #ifdef HAVE_ECC_DHE
  21340. y = ECC_SHARED_SIZE;
  21341. do {
  21342. #if defined(WOLFSSL_ASYNC_CRYPT)
  21343. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21344. #endif
  21345. if (ret == 0)
  21346. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  21347. } while (ret == WC_PENDING_E);
  21348. if (ret != 0)
  21349. ERROR_OUT(-9928, done);
  21350. if (XMEMCMP(sharedA, sharedB, y))
  21351. ERROR_OUT(-9929, done);
  21352. TEST_SLEEP();
  21353. #endif /* HAVE_ECC_DHE */
  21354. #ifdef HAVE_COMP_KEY
  21355. /* try compressed export / import too */
  21356. x = ECC_KEY_EXPORT_BUF_SIZE;
  21357. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 1);
  21358. if (ret != 0)
  21359. ERROR_OUT(-9930, done);
  21360. wc_ecc_free(pubKey);
  21361. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  21362. if (ret != 0)
  21363. ERROR_OUT(-9931, done);
  21364. #ifdef WOLFSSL_CUSTOM_CURVES
  21365. if (dp != NULL) {
  21366. ret = wc_ecc_set_custom_curve(pubKey, dp);
  21367. if (ret != 0)
  21368. ERROR_OUT(-9932, done);
  21369. }
  21370. #endif
  21371. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  21372. if (ret != 0)
  21373. ERROR_OUT(-9933, done);
  21374. #ifdef HAVE_ECC_DHE
  21375. y = ECC_SHARED_SIZE;
  21376. do {
  21377. #if defined(WOLFSSL_ASYNC_CRYPT)
  21378. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21379. #endif
  21380. if (ret == 0)
  21381. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  21382. } while (ret == WC_PENDING_E);
  21383. if (ret != 0)
  21384. ERROR_OUT(-9934, done);
  21385. if (XMEMCMP(sharedA, sharedB, y))
  21386. ERROR_OUT(-9935, done);
  21387. TEST_SLEEP();
  21388. #endif /* HAVE_ECC_DHE */
  21389. #endif /* HAVE_COMP_KEY */
  21390. #endif /* !WOLFSSL_ATECC508A && !WOLFSSL_ATECC608A */
  21391. #endif /* !WC_NO_RNG */
  21392. #endif /* HAVE_ECC_KEY_IMPORT */
  21393. #endif /* HAVE_ECC_KEY_EXPORT */
  21394. /* For KCAPI cannot sign using generated ECDH key */
  21395. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  21396. !defined(WC_NO_RNG) && !defined(WOLFSSL_KCAPI_ECC))
  21397. #ifdef HAVE_ECC_SIGN
  21398. /* ECC w/out Shamir has issue with all 0 digest */
  21399. /* WC_BIGINT doesn't have 0 len well on hardware */
  21400. /* Cryptocell has issues with all 0 digest */
  21401. #if defined(ECC_SHAMIR) && !defined(WOLFSSL_ASYNC_CRYPT) && \
  21402. !defined(WOLFSSL_CRYPTOCELL)
  21403. /* test DSA sign hash with zeros */
  21404. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  21405. digest[i] = 0;
  21406. }
  21407. x = ECC_SIG_SIZE;
  21408. do {
  21409. #if defined(WOLFSSL_ASYNC_CRYPT)
  21410. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21411. #endif
  21412. if (ret == 0)
  21413. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng,
  21414. userA);
  21415. } while (ret == WC_PENDING_E);
  21416. if (ret != 0)
  21417. ERROR_OUT(-9936, done);
  21418. TEST_SLEEP();
  21419. #ifdef HAVE_ECC_VERIFY
  21420. for (i=0; i<testVerifyCount; i++) {
  21421. verify = 0;
  21422. do {
  21423. #if defined(WOLFSSL_ASYNC_CRYPT)
  21424. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21425. #endif
  21426. if (ret == 0)
  21427. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE,
  21428. &verify, userA);
  21429. } while (ret == WC_PENDING_E);
  21430. if (ret != 0)
  21431. ERROR_OUT(-9937, done);
  21432. if (verify != 1)
  21433. ERROR_OUT(-9938, done);
  21434. TEST_SLEEP();
  21435. }
  21436. #endif /* HAVE_ECC_VERIFY */
  21437. #endif /* ECC_SHAMIR && !WOLFSSL_ASYNC_CRYPT && !WOLFSSL_CRYPTOCELL */
  21438. /* test DSA sign hash with sequence (0,1,2,3,4,...) */
  21439. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  21440. digest[i] = (byte)i;
  21441. }
  21442. x = ECC_SIG_SIZE;
  21443. do {
  21444. #if defined(WOLFSSL_ASYNC_CRYPT)
  21445. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21446. #endif
  21447. if (ret == 0)
  21448. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng, userA);
  21449. } while (ret == WC_PENDING_E);
  21450. if (ret != 0)
  21451. ERROR_OUT(-9939, done);
  21452. TEST_SLEEP();
  21453. #ifdef HAVE_ECC_VERIFY
  21454. for (i=0; i<testVerifyCount; i++) {
  21455. verify = 0;
  21456. do {
  21457. #if defined(WOLFSSL_ASYNC_CRYPT)
  21458. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21459. #endif
  21460. if (ret == 0)
  21461. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE, &verify, userA);
  21462. } while (ret == WC_PENDING_E);
  21463. if (ret != 0)
  21464. ERROR_OUT(-9940, done);
  21465. if (verify != 1)
  21466. ERROR_OUT(-9941, done);
  21467. TEST_SLEEP();
  21468. }
  21469. #endif /* HAVE_ECC_VERIFY */
  21470. #endif /* HAVE_ECC_SIGN */
  21471. #endif /* !ECC_TIMING_RESISTANT || (ECC_TIMING_RESISTANT &&
  21472. * !WC_NO_RNG && !WOLFSSL_KCAPI_ECC) */
  21473. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  21474. !defined(WOLFSSL_ATECC508) && !defined(WOLFSSL_ATECC608A) && \
  21475. !defined(WOLFSSL_KCAPI_ECC)
  21476. x = ECC_KEY_EXPORT_BUF_SIZE;
  21477. ret = wc_ecc_export_private_only(userA, exportBuf, &x);
  21478. if (ret != 0)
  21479. ERROR_OUT(-9942, done);
  21480. #elif defined(HAVE_ECC_KEY_EXPORT)
  21481. (void)exportBuf;
  21482. #endif /* HAVE_ECC_KEY_EXPORT */
  21483. done:
  21484. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21485. if (userA != NULL) {
  21486. wc_ecc_free(userA);
  21487. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21488. }
  21489. if (userB != NULL) {
  21490. wc_ecc_free(userB);
  21491. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21492. }
  21493. if (pubKey != NULL) {
  21494. wc_ecc_free(pubKey);
  21495. XFREE(pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21496. }
  21497. #else
  21498. wc_ecc_free(pubKey);
  21499. wc_ecc_free(userB);
  21500. wc_ecc_free(userA);
  21501. #endif
  21502. #if defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)
  21503. WC_FREE_VAR(sharedA, HEAP_HINT);
  21504. WC_FREE_VAR(sharedB, HEAP_HINT);
  21505. #endif
  21506. #ifdef HAVE_ECC_KEY_EXPORT
  21507. WC_FREE_VAR(exportBuf, HEAP_HINT);
  21508. #endif
  21509. #ifdef HAVE_ECC_SIGN
  21510. WC_FREE_VAR(sig, HEAP_HINT);
  21511. WC_FREE_VAR(digest, HEAP_HINT);
  21512. #endif
  21513. (void)keySize;
  21514. (void)curve_id;
  21515. (void)rng;
  21516. return ret;
  21517. }
  21518. #undef ECC_TEST_VERIFY_COUNT
  21519. #define ECC_TEST_VERIFY_COUNT 2
  21520. static int ecc_test_curve(WC_RNG* rng, int keySize)
  21521. {
  21522. int ret;
  21523. ret = ecc_test_curve_size(rng, keySize, ECC_TEST_VERIFY_COUNT,
  21524. ECC_CURVE_DEF, NULL);
  21525. if (ret < 0) {
  21526. if (ret == ECC_CURVE_OID_E) {
  21527. /* ignore error for curves not found */
  21528. /* some curve sizes are only available with:
  21529. HAVE_ECC_SECPR2, HAVE_ECC_SECPR3, HAVE_ECC_BRAINPOOL
  21530. and HAVE_ECC_KOBLITZ */
  21531. }
  21532. else {
  21533. printf("ecc_test_curve_size %d failed!: %d\n", keySize, ret);
  21534. return ret;
  21535. }
  21536. }
  21537. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  21538. #ifdef HAVE_ECC_VECTOR_TEST
  21539. ret = ecc_test_vector(keySize);
  21540. if (ret < 0) {
  21541. printf("ecc_test_vector %d failed!: %d\n", keySize, ret);
  21542. return ret;
  21543. }
  21544. #endif
  21545. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  21546. !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  21547. ret = ecc_test_key_decode(rng, keySize);
  21548. if (ret < 0) {
  21549. if (ret == ECC_CURVE_OID_E) {
  21550. /* ignore error for curves not found */
  21551. }
  21552. else {
  21553. printf("ecc_test_key_decode %d failed!: %d\n", keySize, ret);
  21554. return ret;
  21555. }
  21556. }
  21557. #endif
  21558. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  21559. ret = ecc_test_key_gen(rng, keySize);
  21560. if (ret < 0) {
  21561. if (ret == ECC_CURVE_OID_E) {
  21562. /* ignore error for curves not found */
  21563. }
  21564. else {
  21565. printf("ecc_test_key_gen %d failed!: %d\n", keySize, ret);
  21566. return ret;
  21567. }
  21568. }
  21569. #endif
  21570. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  21571. return 0;
  21572. }
  21573. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  21574. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  21575. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  21576. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21577. static int ecc_point_test(void)
  21578. {
  21579. int ret;
  21580. ecc_point* point;
  21581. ecc_point* point2;
  21582. #ifdef HAVE_COMP_KEY
  21583. ecc_point* point3;
  21584. ecc_point* point4;
  21585. #endif
  21586. word32 outLen;
  21587. byte out[65];
  21588. byte der[] = { 0x04, /* = Uncompressed */
  21589. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21590. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21591. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21592. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21593. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21594. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21595. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21596. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  21597. #if defined(HAVE_COMP_KEY) && (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  21598. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  21599. byte derComp0[] = { 0x02, /* = Compressed, y even */
  21600. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21601. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21602. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21603. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  21604. byte derComp1[] = { 0x03, /* = Compressed, y odd */
  21605. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21606. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21607. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21608. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  21609. #endif
  21610. byte altDer[] = { 0x04, /* = Uncompressed */
  21611. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21612. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21613. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21614. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21615. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21616. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21617. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21618. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
  21619. int curve_idx = wc_ecc_get_curve_idx(ECC_SECP256R1);
  21620. /* if curve P256 is not enabled then test should not fail */
  21621. if (curve_idx == ECC_CURVE_INVALID)
  21622. return 0;
  21623. outLen = sizeof(out);
  21624. point = wc_ecc_new_point();
  21625. if (point == NULL)
  21626. return -10000;
  21627. point2 = wc_ecc_new_point();
  21628. if (point2 == NULL) {
  21629. wc_ecc_del_point(point);
  21630. return -10001;
  21631. }
  21632. #ifdef HAVE_COMP_KEY
  21633. point3 = wc_ecc_new_point();
  21634. if (point3 == NULL) {
  21635. wc_ecc_del_point(point2);
  21636. wc_ecc_del_point(point);
  21637. return -10002;
  21638. }
  21639. point4 = wc_ecc_new_point();
  21640. if (point4 == NULL) {
  21641. wc_ecc_del_point(point3);
  21642. wc_ecc_del_point(point2);
  21643. wc_ecc_del_point(point);
  21644. return -10003;
  21645. }
  21646. #endif
  21647. /* Parameter Validation testing. */
  21648. wc_ecc_del_point(NULL);
  21649. ret = wc_ecc_import_point_der(NULL, sizeof(der), curve_idx, point);
  21650. if (ret != ECC_BAD_ARG_E) {
  21651. ret = -10004;
  21652. goto done;
  21653. }
  21654. ret = wc_ecc_import_point_der(der, sizeof(der), ECC_CURVE_INVALID, point);
  21655. if (ret != ECC_BAD_ARG_E) {
  21656. ret = -10005;
  21657. goto done;
  21658. }
  21659. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, NULL);
  21660. if (ret != ECC_BAD_ARG_E) {
  21661. ret = -10006;
  21662. goto done;
  21663. }
  21664. ret = wc_ecc_export_point_der(-1, point, out, &outLen);
  21665. if (ret != ECC_BAD_ARG_E) {
  21666. ret = -10007;
  21667. goto done;
  21668. }
  21669. ret = wc_ecc_export_point_der(curve_idx, NULL, out, &outLen);
  21670. if (ret != ECC_BAD_ARG_E) {
  21671. ret = -10008;
  21672. goto done;
  21673. }
  21674. ret = wc_ecc_export_point_der(curve_idx, point, NULL, &outLen);
  21675. if (ret != LENGTH_ONLY_E || outLen != sizeof(out)) {
  21676. ret = -10009;
  21677. goto done;
  21678. }
  21679. ret = wc_ecc_export_point_der(curve_idx, point, out, NULL);
  21680. if (ret != ECC_BAD_ARG_E) {
  21681. ret = -10010;
  21682. goto done;
  21683. }
  21684. outLen = 0;
  21685. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  21686. if (ret != BUFFER_E) {
  21687. ret = -10011;
  21688. goto done;
  21689. }
  21690. ret = wc_ecc_copy_point(NULL, NULL);
  21691. if (ret != ECC_BAD_ARG_E) {
  21692. ret = -10012;
  21693. goto done;
  21694. }
  21695. ret = wc_ecc_copy_point(NULL, point2);
  21696. if (ret != ECC_BAD_ARG_E) {
  21697. ret = -10013;
  21698. goto done;
  21699. }
  21700. ret = wc_ecc_copy_point(point, NULL);
  21701. if (ret != ECC_BAD_ARG_E) {
  21702. ret = -10014;
  21703. goto done;
  21704. }
  21705. ret = wc_ecc_cmp_point(NULL, NULL);
  21706. if (ret != BAD_FUNC_ARG) {
  21707. ret = -10015;
  21708. goto done;
  21709. }
  21710. ret = wc_ecc_cmp_point(NULL, point2);
  21711. if (ret != BAD_FUNC_ARG) {
  21712. ret = -10016;
  21713. goto done;
  21714. }
  21715. ret = wc_ecc_cmp_point(point, NULL);
  21716. if (ret != BAD_FUNC_ARG) {
  21717. ret = -10017;
  21718. goto done;
  21719. }
  21720. /* Use API. */
  21721. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, point);
  21722. if (ret != 0) {
  21723. ret = -10018;
  21724. goto done;
  21725. }
  21726. outLen = sizeof(out);
  21727. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  21728. if (ret != 0) {
  21729. ret = -10019;
  21730. goto done;
  21731. }
  21732. if (outLen != sizeof(der)) {
  21733. ret = -10020;
  21734. goto done;
  21735. }
  21736. if (XMEMCMP(out, der, outLen) != 0) {
  21737. ret = -10021;
  21738. goto done;
  21739. }
  21740. ret = wc_ecc_copy_point(point2, point);
  21741. if (ret != MP_OKAY) {
  21742. ret = -10022;
  21743. goto done;
  21744. }
  21745. ret = wc_ecc_cmp_point(point2, point);
  21746. if (ret != MP_EQ) {
  21747. ret = -10023;
  21748. goto done;
  21749. }
  21750. ret = wc_ecc_import_point_der(altDer, sizeof(altDer), curve_idx, point2);
  21751. if (ret != 0) {
  21752. ret = -10024;
  21753. goto done;
  21754. }
  21755. ret = wc_ecc_cmp_point(point2, point);
  21756. if (ret != MP_GT) {
  21757. ret = -10025;
  21758. goto done;
  21759. }
  21760. #if defined(HAVE_COMP_KEY) && (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  21761. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  21762. ret = wc_ecc_import_point_der(derComp0, sizeof(derComp0)*2-1, curve_idx, point3);
  21763. if (ret != 0) {
  21764. ret = -10026;
  21765. goto done;
  21766. }
  21767. ret = wc_ecc_import_point_der_ex(derComp0, sizeof(derComp0), curve_idx, point4, 0);
  21768. if (ret != 0) {
  21769. ret = -10027;
  21770. goto done;
  21771. }
  21772. ret = wc_ecc_cmp_point(point3, point4);
  21773. if (ret != MP_EQ) {
  21774. ret = -10028;
  21775. goto done;
  21776. }
  21777. ret = wc_ecc_import_point_der(derComp1, sizeof(derComp1)*2-1, curve_idx, point3);
  21778. if (ret != 0) {
  21779. ret = -10029;
  21780. goto done;
  21781. }
  21782. ret = wc_ecc_import_point_der_ex(derComp1, sizeof(derComp1), curve_idx, point4, 0);
  21783. if (ret != 0) {
  21784. ret = -10030;
  21785. goto done;
  21786. }
  21787. ret = wc_ecc_cmp_point(point3, point4);
  21788. if (ret != MP_EQ) {
  21789. ret = -10031;
  21790. goto done;
  21791. }
  21792. #endif
  21793. done:
  21794. #ifdef HAVE_COMP_KEY
  21795. wc_ecc_del_point(point4);
  21796. wc_ecc_del_point(point3);
  21797. #endif
  21798. wc_ecc_del_point(point2);
  21799. wc_ecc_del_point(point);
  21800. return ret;
  21801. }
  21802. #endif /* !WOLFSSL_ATECC508A && HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  21803. #if !defined(NO_SIG_WRAPPER) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21804. static int ecc_sig_test(WC_RNG* rng, ecc_key* key)
  21805. {
  21806. int ret;
  21807. word32 sigSz;
  21808. int size;
  21809. byte out[ECC_MAX_SIG_SIZE];
  21810. byte in[] = TEST_STRING;
  21811. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  21812. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  21813. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  21814. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  21815. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  21816. };
  21817. word32 inLen = (word32)XSTRLEN((char*)in);
  21818. size = wc_ecc_sig_size(key);
  21819. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, sizeof(*key));
  21820. if (ret != size)
  21821. return -10040;
  21822. sigSz = (word32)ret;
  21823. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  21824. inLen, out, &sigSz, key, sizeof(*key), rng);
  21825. if (ret != 0)
  21826. return -10041;
  21827. TEST_SLEEP();
  21828. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  21829. inLen, out, sigSz, key, sizeof(*key));
  21830. if (ret != 0)
  21831. return -10042;
  21832. TEST_SLEEP();
  21833. sigSz = (word32)sizeof(out);
  21834. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  21835. hash, (int)sizeof(hash), out, &sigSz, key, sizeof(*key), rng);
  21836. if (ret != 0)
  21837. return -10043;
  21838. TEST_SLEEP();
  21839. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  21840. hash, (int)sizeof(hash), out, sigSz, key, sizeof(*key));
  21841. if (ret != 0)
  21842. return -10044;
  21843. TEST_SLEEP();
  21844. return 0;
  21845. }
  21846. #endif
  21847. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  21848. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21849. static int ecc_exp_imp_test(ecc_key* key)
  21850. {
  21851. int ret;
  21852. int curve_id;
  21853. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21854. ecc_key *keyImp = (ecc_key *)XMALLOC(sizeof *keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21855. #else
  21856. ecc_key keyImp[1];
  21857. #endif
  21858. byte priv[32];
  21859. word32 privLen;
  21860. byte pub[65*2];
  21861. word32 pubLen, pubLenX, pubLenY;
  21862. const char qx[] = "7a4e287890a1a47ad3457e52f2f76a83"
  21863. "ce46cbc947616d0cbaa82323818a793d";
  21864. const char qy[] = "eec4084f5b29ebf29c44cce3b3059610"
  21865. "922f8b30ea6e8811742ac7238fe87308";
  21866. const char d[] = "8c14b793cb19137e323a6d2e2a870bca"
  21867. "2e7a493ec1153b3a95feb8a4873f8d08";
  21868. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21869. if (keyImp == NULL)
  21870. ERROR_OUT(-10050, done);
  21871. #endif
  21872. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  21873. privLen = sizeof(priv);
  21874. ret = wc_ecc_export_private_only(key, priv, &privLen);
  21875. if (ret != 0) {
  21876. ret = -10051;
  21877. goto done;
  21878. }
  21879. pubLen = sizeof(pub);
  21880. ret = wc_ecc_export_point_der(key->idx, &key->pubkey, pub, &pubLen);
  21881. if (ret != 0) {
  21882. ret = -10052;
  21883. goto done;
  21884. }
  21885. ret = wc_ecc_import_private_key(priv, privLen, pub, pubLen, keyImp);
  21886. if (ret != 0) {
  21887. ret = -10053;
  21888. goto done;
  21889. }
  21890. wc_ecc_free(keyImp);
  21891. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  21892. ret = wc_ecc_import_raw_ex(keyImp, qx, qy, d, ECC_SECP256R1);
  21893. if (ret != 0) {
  21894. ret = -10054;
  21895. goto done;
  21896. }
  21897. wc_ecc_free(keyImp);
  21898. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  21899. curve_id = wc_ecc_get_curve_id(key->idx);
  21900. if (curve_id < 0) {
  21901. ret = -10055;
  21902. goto done;
  21903. }
  21904. /* test import private only */
  21905. ret = wc_ecc_import_private_key_ex(priv, privLen, NULL, 0, keyImp,
  21906. curve_id);
  21907. if (ret != 0) {
  21908. ret = -10056;
  21909. goto done;
  21910. }
  21911. wc_ecc_free(keyImp);
  21912. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  21913. /* test export public raw */
  21914. pubLenX = pubLenY = 32;
  21915. ret = wc_ecc_export_public_raw(key, pub, &pubLenX, &pub[32], &pubLenY);
  21916. if (ret != 0) {
  21917. ret = -10057;
  21918. goto done;
  21919. }
  21920. #ifndef HAVE_SELFTEST
  21921. /* test import of public */
  21922. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], NULL, ECC_SECP256R1);
  21923. if (ret != 0) {
  21924. ret = -10058;
  21925. goto done;
  21926. }
  21927. #endif
  21928. wc_ecc_free(keyImp);
  21929. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  21930. /* test export private and public raw */
  21931. pubLenX = pubLenY = privLen = 32;
  21932. ret = wc_ecc_export_private_raw(key, pub, &pubLenX, &pub[32], &pubLenY,
  21933. priv, &privLen);
  21934. if (ret != 0) {
  21935. ret = -10059;
  21936. goto done;
  21937. }
  21938. #ifndef HAVE_SELFTEST
  21939. /* test import of private and public */
  21940. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], priv, ECC_SECP256R1);
  21941. if (ret != 0) {
  21942. ret = -10060;
  21943. goto done;
  21944. }
  21945. #endif
  21946. done:
  21947. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21948. if (keyImp != NULL) {
  21949. wc_ecc_free(keyImp);
  21950. XFREE(keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21951. }
  21952. #else
  21953. wc_ecc_free(keyImp);
  21954. #endif
  21955. return ret;
  21956. }
  21957. #endif /* HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  21958. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT) && \
  21959. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21960. static int ecc_mulmod_test(ecc_key* key1)
  21961. {
  21962. int ret;
  21963. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21964. ecc_key *key2 = (ecc_key *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21965. ecc_key *key3 = (ecc_key *)XMALLOC(sizeof *key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21966. #else
  21967. ecc_key key2[1];
  21968. ecc_key key3[1];
  21969. #endif
  21970. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21971. if ((key2 == NULL) || (key3 == NULL))
  21972. ERROR_OUT(MEMORY_E, done);
  21973. #endif
  21974. wc_ecc_init_ex(key2, HEAP_HINT, devId);
  21975. wc_ecc_init_ex(key3, HEAP_HINT, devId);
  21976. /* TODO: Use test data, test with WOLFSSL_VALIDATE_ECC_IMPORT. */
  21977. /* Need base point (Gx,Gy) and parameter A - load them as the public and
  21978. * private key in key2.
  21979. */
  21980. ret = wc_ecc_import_raw_ex(key2, key1->dp->Gx, key1->dp->Gy, key1->dp->Af,
  21981. ECC_SECP256R1);
  21982. if (ret != 0)
  21983. goto done;
  21984. /* Need a point (Gx,Gy) and prime - load them as the public and private key
  21985. * in key3.
  21986. */
  21987. ret = wc_ecc_import_raw_ex(key3, key1->dp->Gx, key1->dp->Gy,
  21988. key1->dp->prime, ECC_SECP256R1);
  21989. if (ret != 0)
  21990. goto done;
  21991. ret = wc_ecc_mulmod(&key1->k, &key2->pubkey, &key3->pubkey, &key2->k, &key3->k,
  21992. 1);
  21993. if (ret != 0) {
  21994. ret = -10070;
  21995. goto done;
  21996. }
  21997. done:
  21998. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21999. if (key2 != NULL) {
  22000. wc_ecc_free(key2);
  22001. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22002. }
  22003. if (key3 != NULL) {
  22004. wc_ecc_free(key3);
  22005. XFREE(key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22006. }
  22007. #else
  22008. wc_ecc_free(key3);
  22009. wc_ecc_free(key2);
  22010. #endif
  22011. return ret;
  22012. }
  22013. #endif
  22014. #if defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG) && \
  22015. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  22016. static int ecc_ssh_test(ecc_key* key, WC_RNG* rng)
  22017. {
  22018. int ret;
  22019. byte out[128];
  22020. word32 outLen = sizeof(out);
  22021. /* Parameter Validation testing. */
  22022. ret = wc_ecc_shared_secret_ssh(NULL, &key->pubkey, out, &outLen);
  22023. if (ret != BAD_FUNC_ARG)
  22024. return -10080;
  22025. ret = wc_ecc_shared_secret_ssh(key, NULL, out, &outLen);
  22026. if (ret != BAD_FUNC_ARG)
  22027. return -10081;
  22028. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, NULL, &outLen);
  22029. if (ret != BAD_FUNC_ARG)
  22030. return -10082;
  22031. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, NULL);
  22032. if (ret != BAD_FUNC_ARG)
  22033. return -10083;
  22034. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22035. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22036. !defined(HAVE_SELFTEST)
  22037. ret = wc_ecc_set_rng(key, rng);
  22038. if (ret != 0)
  22039. return -10084;
  22040. #else
  22041. (void)rng;
  22042. #endif
  22043. /* Use API. */
  22044. ret = 0;
  22045. do {
  22046. #if defined(WOLFSSL_ASYNC_CRYPT)
  22047. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22048. #endif
  22049. if (ret == 0)
  22050. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, &outLen);
  22051. } while (ret == WC_PENDING_E);
  22052. if (ret != 0)
  22053. return -10085;
  22054. TEST_SLEEP();
  22055. return 0;
  22056. }
  22057. #endif /* HAVE_ECC_DHE && !WC_NO_RNG */
  22058. static int ecc_def_curve_test(WC_RNG *rng)
  22059. {
  22060. int ret;
  22061. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22062. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22063. #else
  22064. ecc_key key[1];
  22065. #endif
  22066. #if (defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)) || \
  22067. (defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  22068. word32 idx = 0;
  22069. #endif
  22070. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22071. if (key == NULL)
  22072. ERROR_OUT(MEMORY_E, done);
  22073. #endif
  22074. wc_ecc_init_ex(key, HEAP_HINT, devId);
  22075. /* Use API */
  22076. ret = wc_ecc_set_flags(NULL, 0);
  22077. if (ret != BAD_FUNC_ARG) {
  22078. ret = -10090;
  22079. goto done;
  22080. }
  22081. ret = wc_ecc_set_flags(key, 0);
  22082. if (ret != 0) {
  22083. ret = -10091;
  22084. goto done;
  22085. }
  22086. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  22087. #ifndef WC_NO_RNG
  22088. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  22089. #if defined(WOLFSSL_ASYNC_CRYPT)
  22090. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  22091. #endif
  22092. if (ret != 0) {
  22093. goto done;
  22094. }
  22095. #ifndef NO_SIG_WRAPPER
  22096. ret = ecc_sig_test(rng, key);
  22097. if (ret < 0)
  22098. goto done;
  22099. #endif
  22100. TEST_SLEEP();
  22101. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_CRYPTOCELL) && \
  22102. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  22103. ret = ecc_ssh_test(key, rng);
  22104. if (ret < 0)
  22105. goto done;
  22106. #endif
  22107. wc_ecc_free(key);
  22108. #else
  22109. (void)rng;
  22110. #endif /* !WC_NO_RNG */
  22111. #if (defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)) || \
  22112. (defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  22113. /* Use test ECC key - ensure real private "d" exists */
  22114. #ifdef USE_CERT_BUFFERS_256
  22115. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, key,
  22116. sizeof_ecc_key_der_256);
  22117. #else
  22118. {
  22119. XFILE file = XFOPEN(eccKeyDerFile, "rb");
  22120. byte der[128];
  22121. word32 derSz;
  22122. if (!file) {
  22123. ERROR_OUT(-10093, done);
  22124. }
  22125. derSz = (word32)XFREAD(der, 1, sizeof(der), file);
  22126. XFCLOSE(file);
  22127. ret = wc_EccPrivateKeyDecode(der, &idx, key, derSz);
  22128. }
  22129. #endif
  22130. if (ret != 0) {
  22131. goto done;
  22132. }
  22133. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)
  22134. ret = ecc_exp_imp_test(key);
  22135. if (ret < 0)
  22136. goto done;
  22137. #endif
  22138. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT) && \
  22139. !defined(WOLFSSL_CRYPTOCELL)
  22140. ret = ecc_mulmod_test(key);
  22141. if (ret < 0)
  22142. goto done;
  22143. #endif
  22144. #endif
  22145. #else
  22146. (void)rng;
  22147. (void)idx;
  22148. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  22149. done:
  22150. wc_ecc_free(key);
  22151. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22152. if (key != NULL) {
  22153. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22154. }
  22155. #endif
  22156. return ret;
  22157. }
  22158. #endif /* !NO_ECC256 || HAVE_ALL_CURVES */
  22159. #if defined(WOLFSSL_CERT_EXT) && \
  22160. (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  22161. static int ecc_decode_test(void)
  22162. {
  22163. int ret;
  22164. word32 inSz;
  22165. word32 inOutIdx;
  22166. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22167. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22168. #else
  22169. ecc_key key[1];
  22170. #endif
  22171. /* SECP256R1 OID: 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07 */
  22172. /* This is ecc_clikeypub_der_256. */
  22173. WOLFSSL_SMALL_STACK_STATIC const byte good[] = {
  22174. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce,
  22175. 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
  22176. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xbf, 0xf4,
  22177. 0x0f, 0x44, 0x50, 0x9a, 0x3d, 0xce, 0x9b, 0xb7, 0xf0, 0xc5,
  22178. 0x4d, 0xf5, 0x70, 0x7b, 0xd4, 0xec, 0x24, 0x8e, 0x19, 0x80,
  22179. 0xec, 0x5a, 0x4c, 0xa2, 0x24, 0x03, 0x62, 0x2c, 0x9b, 0xda,
  22180. 0xef, 0xa2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xc6, 0x56,
  22181. 0x95, 0x06, 0xcc, 0x01, 0xa9, 0xbd, 0xf6, 0x75, 0x1a, 0x42,
  22182. 0xf7, 0xbd, 0xa9, 0xb2, 0x36, 0x22, 0x5f, 0xc7, 0x5d, 0x7f,
  22183. 0xb4 };
  22184. WOLFSSL_SMALL_STACK_STATIC const byte badNoObjId[] = { 0x30, 0x08, 0x30, 0x06, 0x03, 0x04,
  22185. 0x00, 0x04, 0x01, 0x01 };
  22186. WOLFSSL_SMALL_STACK_STATIC const byte badOneObjId[] = { 0x30, 0x0a, 0x30, 0x08, 0x06, 0x00,
  22187. 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  22188. WOLFSSL_SMALL_STACK_STATIC const byte badObjId1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x09,
  22189. 0x06, 0x00, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  22190. WOLFSSL_SMALL_STACK_STATIC const byte badObj2d1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x00,
  22191. 0x06, 0x07, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  22192. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitStr[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  22193. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  22194. 0x04, 0x04, 0x00, 0x04, 0x01, 0x01 };
  22195. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrLen[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  22196. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  22197. 0x03, 0x05, 0x00, 0x04, 0x01, 0x01 };
  22198. WOLFSSL_SMALL_STACK_STATIC const byte badNoBitStrZero[] = { 0x30, 0x13, 0x30, 0x0a, 0x06, 0x00,
  22199. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  22200. 0x03, 0x03, 0x04, 0x01, 0x01 };
  22201. WOLFSSL_SMALL_STACK_STATIC const byte badPoint[] = { 0x30, 0x12, 0x30, 0x09, 0x06, 0x00,
  22202. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  22203. 0x03, 0x03, 0x00, 0x04, 0x01 };
  22204. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22205. if (key == NULL)
  22206. ERROR_OUT(MEMORY_E, done);
  22207. #endif
  22208. XMEMSET(key, 0, sizeof *key);
  22209. wc_ecc_init_ex(key, HEAP_HINT, devId);
  22210. inSz = sizeof(good);
  22211. ret = wc_EccPublicKeyDecode(NULL, &inOutIdx, key, inSz);
  22212. if (ret != BAD_FUNC_ARG) {
  22213. ret = -10100;
  22214. goto done;
  22215. }
  22216. ret = wc_EccPublicKeyDecode(good, NULL, key, inSz);
  22217. if (ret != BAD_FUNC_ARG) {
  22218. ret = -10101;
  22219. goto done;
  22220. }
  22221. ret = wc_EccPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  22222. if (ret != BAD_FUNC_ARG) {
  22223. ret = -10102;
  22224. goto done;
  22225. }
  22226. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, 0);
  22227. if (ret != BAD_FUNC_ARG) {
  22228. ret = -10103;
  22229. goto done;
  22230. }
  22231. /* Change offset to produce bad input data. */
  22232. inOutIdx = 2;
  22233. inSz = sizeof(good) - inOutIdx;
  22234. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  22235. if (ret != ASN_PARSE_E) {
  22236. ret = -10104;
  22237. goto done;
  22238. }
  22239. inOutIdx = 4;
  22240. inSz = sizeof(good) - inOutIdx;
  22241. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  22242. if (ret != ASN_PARSE_E) {
  22243. ret = -10105;
  22244. goto done;
  22245. }
  22246. /* Bad data. */
  22247. inSz = sizeof(badNoObjId);
  22248. inOutIdx = 0;
  22249. ret = wc_EccPublicKeyDecode(badNoObjId, &inOutIdx, key, inSz);
  22250. if (ret != ASN_OBJECT_ID_E && ret != ASN_PARSE_E) {
  22251. ret = -10106;
  22252. goto done;
  22253. }
  22254. inSz = sizeof(badOneObjId);
  22255. inOutIdx = 0;
  22256. ret = wc_EccPublicKeyDecode(badOneObjId, &inOutIdx, key, inSz);
  22257. if (ret != ASN_OBJECT_ID_E && ret != ASN_PARSE_E) {
  22258. ret = -10107;
  22259. goto done;
  22260. }
  22261. inSz = sizeof(badObjId1Len);
  22262. inOutIdx = 0;
  22263. ret = wc_EccPublicKeyDecode(badObjId1Len, &inOutIdx, key, inSz);
  22264. if (ret != ASN_PARSE_E) {
  22265. ret = -10108;
  22266. goto done;
  22267. }
  22268. inSz = sizeof(badObj2d1Len);
  22269. inOutIdx = 0;
  22270. ret = wc_EccPublicKeyDecode(badObj2d1Len, &inOutIdx, key, inSz);
  22271. if (ret != ASN_PARSE_E) {
  22272. ret = -10109;
  22273. goto done;
  22274. }
  22275. inSz = sizeof(badNotBitStr);
  22276. inOutIdx = 0;
  22277. ret = wc_EccPublicKeyDecode(badNotBitStr, &inOutIdx, key, inSz);
  22278. if (ret != ASN_BITSTR_E && ret != ASN_PARSE_E) {
  22279. ret = -10110;
  22280. goto done;
  22281. }
  22282. inSz = sizeof(badBitStrLen);
  22283. inOutIdx = 0;
  22284. ret = wc_EccPublicKeyDecode(badBitStrLen, &inOutIdx, key, inSz);
  22285. if (ret != ASN_PARSE_E) {
  22286. ret = -10111;
  22287. goto done;
  22288. }
  22289. inSz = sizeof(badNoBitStrZero);
  22290. inOutIdx = 0;
  22291. ret = wc_EccPublicKeyDecode(badNoBitStrZero, &inOutIdx, key, inSz);
  22292. if (ret != ASN_EXPECT_0_E && ret != ASN_PARSE_E) {
  22293. ret = -10112;
  22294. goto done;
  22295. }
  22296. inSz = sizeof(badPoint);
  22297. inOutIdx = 0;
  22298. ret = wc_EccPublicKeyDecode(badPoint, &inOutIdx, key, inSz);
  22299. if (ret != ASN_ECC_KEY_E && ret != ASN_PARSE_E) {
  22300. ret = -10113;
  22301. goto done;
  22302. }
  22303. inSz = sizeof(good);
  22304. inOutIdx = 0;
  22305. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  22306. if (ret != 0) {
  22307. ret = -10114;
  22308. goto done;
  22309. }
  22310. done:
  22311. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22312. if (key != NULL) {
  22313. wc_ecc_free(key);
  22314. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22315. }
  22316. #else
  22317. wc_ecc_free(key);
  22318. #endif
  22319. return ret;
  22320. }
  22321. #endif /* WOLFSSL_CERT_EXT */
  22322. #ifdef WOLFSSL_CUSTOM_CURVES
  22323. static const byte eccKeyExplicitCurve[] = {
  22324. 0x30, 0x81, 0xf5, 0x30, 0x81, 0xae, 0x06, 0x07,
  22325. 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x30,
  22326. 0x81, 0xa2, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06,
  22327. 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01,
  22328. 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
  22329. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  22330. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  22331. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff,
  22332. 0xff, 0xfc, 0x2f, 0x30, 0x06, 0x04, 0x01, 0x00,
  22333. 0x04, 0x01, 0x07, 0x04, 0x41, 0x04, 0x79, 0xbe,
  22334. 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0,
  22335. 0x62, 0x95, 0xce, 0x87, 0x0b, 0x07, 0x02, 0x9b,
  22336. 0xfc, 0xdb, 0x2d, 0xce, 0x28, 0xd9, 0x59, 0xf2,
  22337. 0x81, 0x5b, 0x16, 0xf8, 0x17, 0x98, 0x48, 0x3a,
  22338. 0xda, 0x77, 0x26, 0xa3, 0xc4, 0x65, 0x5d, 0xa4,
  22339. 0xfb, 0xfc, 0x0e, 0x11, 0x08, 0xa8, 0xfd, 0x17,
  22340. 0xb4, 0x48, 0xa6, 0x85, 0x54, 0x19, 0x9c, 0x47,
  22341. 0xd0, 0x8f, 0xfb, 0x10, 0xd4, 0xb8, 0x02, 0x21,
  22342. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  22343. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  22344. 0xfe, 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0,
  22345. 0x3b, 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41,
  22346. 0x41, 0x02, 0x01, 0x01, 0x03, 0x42, 0x00, 0x04,
  22347. 0x3c, 0x4c, 0xc9, 0x5e, 0x2e, 0xa2, 0x3d, 0x49,
  22348. 0xcc, 0x5b, 0xff, 0x4f, 0xc9, 0x2e, 0x1d, 0x4a,
  22349. 0xc6, 0x21, 0xf6, 0xf3, 0xe6, 0x0b, 0x4f, 0xa9,
  22350. 0x9d, 0x74, 0x99, 0xdd, 0x97, 0xc7, 0x6e, 0xbe,
  22351. 0x14, 0x2b, 0x39, 0x9d, 0x63, 0xc7, 0x97, 0x0d,
  22352. 0x45, 0x25, 0x40, 0x30, 0x77, 0x05, 0x76, 0x88,
  22353. 0x38, 0x96, 0x29, 0x7d, 0x9c, 0xe1, 0x50, 0xbe,
  22354. 0xac, 0xf0, 0x1d, 0x86, 0xf4, 0x2f, 0x65, 0x0b
  22355. };
  22356. static int ecc_test_custom_curves(WC_RNG* rng)
  22357. {
  22358. int ret;
  22359. word32 inOutIdx;
  22360. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22361. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22362. #else
  22363. ecc_key key[1];
  22364. #endif
  22365. /* test use of custom curve - using BRAINPOOLP256R1 for test */
  22366. #if defined(HAVE_ECC_BRAINPOOL) && !defined(HAVE_INTEL_QA)
  22367. #ifndef WOLFSSL_ECC_CURVE_STATIC
  22368. WOLFSSL_SMALL_STACK_STATIC const ecc_oid_t ecc_oid_brainpoolp256r1[] = {
  22369. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07
  22370. };
  22371. #define ecc_oid_brainpoolp256r1_sz \
  22372. (sizeof(ecc_oid_brainpoolp256r1) / sizeof(ecc_oid_t))
  22373. #else
  22374. #define ecc_oid_brainpoolp256r1 { \
  22375. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07 \
  22376. }
  22377. #define ecc_oid_brainpoolp256r1_sz 9
  22378. #endif
  22379. #define ecc_oid_brainpoolp256r1_sum 104
  22380. WOLFSSL_SMALL_STACK_STATIC const ecc_set_type ecc_dp_brainpool256r1 = {
  22381. 32, /* size/bytes */
  22382. ECC_CURVE_CUSTOM, /* ID */
  22383. "BRAINPOOLP256R1", /* curve name */
  22384. "A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377", /* prime */
  22385. "7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9", /* A */
  22386. "26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6", /* B */
  22387. "A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7", /* order */
  22388. "8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262", /* Gx */
  22389. "547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997", /* Gy */
  22390. ecc_oid_brainpoolp256r1, /* oid/oidSz */
  22391. ecc_oid_brainpoolp256r1_sz,
  22392. ecc_oid_brainpoolp256r1_sum, /* oid sum */
  22393. 1, /* cofactor */
  22394. };
  22395. #endif /* HAVE_ECC_BRAINPOOL */
  22396. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22397. if (! key) {
  22398. ret = MEMORY_E;
  22399. goto done;
  22400. }
  22401. #endif
  22402. XMEMSET(key, 0, sizeof *key);
  22403. #if defined(HAVE_ECC_BRAINPOOL) && !defined(HAVE_INTEL_QA)
  22404. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, ECC_CURVE_DEF,
  22405. &ecc_dp_brainpool256r1);
  22406. if (ret != 0) {
  22407. printf("ECC test for custom curve failed! %d\n", ret);
  22408. goto done;
  22409. }
  22410. #endif
  22411. #if defined(HAVE_ECC_BRAINPOOL) || defined(HAVE_ECC_KOBLITZ)
  22412. {
  22413. int curve_id;
  22414. #ifdef HAVE_ECC_BRAINPOOL
  22415. curve_id = ECC_BRAINPOOLP256R1;
  22416. #else
  22417. curve_id = ECC_SECP256K1;
  22418. #endif
  22419. /* Test and demonstrate use of non-SECP curve */
  22420. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, curve_id, NULL);
  22421. if (ret < 0) {
  22422. printf("ECC test for curve_id %d failed! %d\n", curve_id, ret);
  22423. goto done;
  22424. }
  22425. }
  22426. #endif
  22427. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  22428. if (ret != 0) {
  22429. ret = -10120;
  22430. goto done;
  22431. }
  22432. inOutIdx = 0;
  22433. ret = wc_EccPublicKeyDecode(eccKeyExplicitCurve, &inOutIdx, key,
  22434. sizeof(eccKeyExplicitCurve));
  22435. if (ret != 0)
  22436. ret = -10121;
  22437. done:
  22438. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22439. if (key) {
  22440. wc_ecc_free(key);
  22441. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22442. }
  22443. #else
  22444. wc_ecc_free(key);
  22445. #endif
  22446. (void)rng;
  22447. return ret;
  22448. }
  22449. #endif /* WOLFSSL_CUSTOM_CURVES */
  22450. #ifdef WOLFSSL_CERT_GEN
  22451. /* Make Cert / Sign example for ECC cert and ECC CA */
  22452. static int ecc_test_cert_gen(WC_RNG* rng)
  22453. {
  22454. int ret;
  22455. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22456. Cert *myCert = (Cert *)XMALLOC(sizeof *myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22457. #ifdef WOLFSSL_TEST_CERT
  22458. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22459. #endif
  22460. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22461. ecc_key *certPubKey = (ecc_key *)XMALLOC(sizeof *certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22462. #else
  22463. Cert myCert[1];
  22464. #ifdef WOLFSSL_TEST_CERT
  22465. DecodedCert decode[1];
  22466. #endif
  22467. ecc_key caEccKey[1];
  22468. ecc_key certPubKey[1];
  22469. #endif
  22470. int certSz;
  22471. size_t bytes;
  22472. word32 idx = 0;
  22473. #ifndef USE_CERT_BUFFERS_256
  22474. XFILE file;
  22475. #endif
  22476. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22477. byte* der = NULL;
  22478. #else
  22479. byte der[FOURK_BUF];
  22480. #endif
  22481. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22482. if ((myCert == NULL)
  22483. #ifdef WOLFSSL_TEST_CERT
  22484. || (decode == NULL)
  22485. #endif
  22486. || (caEccKey == NULL) || (certPubKey == NULL))
  22487. ERROR_OUT(MEMORY_E, exit);
  22488. #endif
  22489. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  22490. XMEMSET(certPubKey, 0, sizeof *certPubKey);
  22491. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22492. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22493. if (der == NULL) {
  22494. ERROR_OUT(-10130, exit);
  22495. }
  22496. #endif
  22497. /* Get cert private key */
  22498. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  22499. /* Get Cert Key 384 */
  22500. #ifdef USE_CERT_BUFFERS_256
  22501. XMEMCPY(der, ca_ecc_key_der_384, sizeof_ca_ecc_key_der_384);
  22502. bytes = sizeof_ca_ecc_key_der_384;
  22503. #else
  22504. file = XFOPEN(eccCaKey384File, "rb");
  22505. if (!file) {
  22506. ERROR_OUT(-10131, exit);
  22507. }
  22508. bytes = XFREAD(der, 1, FOURK_BUF, file);
  22509. XFCLOSE(file);
  22510. (void)eccCaKeyFile;
  22511. #endif /* USE_CERT_BUFFERS_256 */
  22512. #else
  22513. #ifdef USE_CERT_BUFFERS_256
  22514. XMEMCPY(der, ca_ecc_key_der_256, sizeof_ca_ecc_key_der_256);
  22515. bytes = sizeof_ca_ecc_key_der_256;
  22516. #else
  22517. file = XFOPEN(eccCaKeyFile, "rb");
  22518. if (!file) {
  22519. ERROR_OUT(-10132, exit);
  22520. }
  22521. bytes = XFREAD(der, 1, FOURK_BUF, file);
  22522. XFCLOSE(file);
  22523. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  22524. (void)eccCaKey384File;
  22525. #endif
  22526. #endif /* USE_CERT_BUFFERS_256 */
  22527. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  22528. /* Get CA Key */
  22529. ret = wc_ecc_init_ex(caEccKey, HEAP_HINT, devId);
  22530. if (ret != 0) {
  22531. ERROR_OUT(-10133, exit);
  22532. }
  22533. ret = wc_EccPrivateKeyDecode(der, &idx, caEccKey, (word32)bytes);
  22534. if (ret != 0) {
  22535. ERROR_OUT(-10134, exit);
  22536. }
  22537. /* Make a public key */
  22538. ret = wc_ecc_init_ex(certPubKey, HEAP_HINT, devId);
  22539. if (ret != 0) {
  22540. ERROR_OUT(-10135, exit);
  22541. }
  22542. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, certPubKey);
  22543. #if defined(WOLFSSL_ASYNC_CRYPT)
  22544. ret = wc_AsyncWait(ret, &certPubKey->asyncDev, WC_ASYNC_FLAG_NONE);
  22545. #endif
  22546. if (ret != 0) {
  22547. ERROR_OUT(-10136, exit);
  22548. }
  22549. TEST_SLEEP();
  22550. /* Setup Certificate */
  22551. if (wc_InitCert_ex(myCert, HEAP_HINT, devId)) {
  22552. ERROR_OUT(-10137, exit);
  22553. }
  22554. #ifndef NO_SHA256
  22555. myCert->sigType = CTC_SHA256wECDSA;
  22556. #else
  22557. myCert->sigType = CTC_SHAwECDSA;
  22558. #endif
  22559. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  22560. #ifdef WOLFSSL_CERT_EXT
  22561. /* add Policies */
  22562. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  22563. CTC_MAX_CERTPOL_SZ);
  22564. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  22565. CTC_MAX_CERTPOL_SZ);
  22566. myCert->certPoliciesNb = 2;
  22567. /* add SKID from the Public Key */
  22568. if (wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, certPubKey) != 0) {
  22569. ERROR_OUT(-10138, exit);
  22570. }
  22571. /* add AKID from the Public Key */
  22572. if (wc_SetAuthKeyIdFromPublicKey(myCert, NULL, caEccKey) != 0) {
  22573. ERROR_OUT(-10139, exit);
  22574. }
  22575. /* add Key Usage */
  22576. if (wc_SetKeyUsage(myCert, certKeyUsage) != 0) {
  22577. ERROR_OUT(-10140, exit);
  22578. }
  22579. #endif /* WOLFSSL_CERT_EXT */
  22580. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  22581. #if defined(USE_CERT_BUFFERS_256)
  22582. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_384,
  22583. sizeof_ca_ecc_cert_der_384);
  22584. #else
  22585. ret = wc_SetIssuer(myCert, eccCaCert384File);
  22586. (void)eccCaCertFile;
  22587. #endif
  22588. #else
  22589. #if defined(USE_CERT_BUFFERS_256)
  22590. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_256,
  22591. sizeof_ca_ecc_cert_der_256);
  22592. #else
  22593. ret = wc_SetIssuer(myCert, eccCaCertFile);
  22594. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  22595. (void)eccCaCert384File;
  22596. #endif
  22597. #endif
  22598. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  22599. if (ret < 0) {
  22600. ERROR_OUT(-10141, exit);
  22601. }
  22602. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, certPubKey, rng);
  22603. if (certSz < 0) {
  22604. ERROR_OUT(-10142, exit);
  22605. }
  22606. ret = 0;
  22607. do {
  22608. #if defined(WOLFSSL_ASYNC_CRYPT)
  22609. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22610. #endif
  22611. if (ret >= 0) {
  22612. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  22613. FOURK_BUF, NULL, caEccKey, rng);
  22614. }
  22615. } while (ret == WC_PENDING_E);
  22616. if (ret < 0) {
  22617. ERROR_OUT(-10143, exit);
  22618. }
  22619. certSz = ret;
  22620. TEST_SLEEP();
  22621. #ifdef WOLFSSL_TEST_CERT
  22622. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  22623. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  22624. if (ret != 0) {
  22625. FreeDecodedCert(decode);
  22626. ERROR_OUT(-10144, exit);
  22627. }
  22628. FreeDecodedCert(decode);
  22629. #endif
  22630. ret = SaveDerAndPem(der, certSz, certEccDerFile, certEccPemFile,
  22631. CERT_TYPE, -6735);
  22632. if (ret != 0) {
  22633. goto exit;
  22634. }
  22635. exit:
  22636. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22637. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22638. #endif
  22639. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22640. if (myCert != NULL)
  22641. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22642. #ifdef WOLFSSL_TEST_CERT
  22643. if (decode != NULL)
  22644. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22645. #endif
  22646. if (caEccKey != NULL) {
  22647. wc_ecc_free(caEccKey);
  22648. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22649. }
  22650. if (certPubKey != NULL) {
  22651. wc_ecc_free(certPubKey);
  22652. XFREE(certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22653. }
  22654. #else
  22655. wc_ecc_free(certPubKey);
  22656. wc_ecc_free(caEccKey);
  22657. #endif
  22658. return ret;
  22659. }
  22660. #endif /* WOLFSSL_CERT_GEN */
  22661. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  22662. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  22663. /* Test for the wc_ecc_key_new() and wc_ecc_key_free() functions. */
  22664. static int ecc_test_allocator(WC_RNG* rng)
  22665. {
  22666. int ret = 0;
  22667. ecc_key* key;
  22668. #ifdef WC_NO_RNG
  22669. word32 idx = 0;
  22670. #endif
  22671. key = wc_ecc_key_new(HEAP_HINT);
  22672. if (key == NULL) {
  22673. ERROR_OUT(-10150, exit);
  22674. }
  22675. #ifndef WC_NO_RNG
  22676. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  22677. #if defined(WOLFSSL_ASYNC_CRYPT)
  22678. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  22679. #endif
  22680. if (ret != 0) {
  22681. ERROR_OUT(-10151, exit);
  22682. }
  22683. #else
  22684. /* use test ECC key */
  22685. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, key,
  22686. (word32)sizeof_ecc_key_der_256);
  22687. (void)rng;
  22688. #endif
  22689. exit:
  22690. wc_ecc_key_free(key);
  22691. return ret;
  22692. }
  22693. #endif
  22694. /* ECC Non-blocking tests for Sign and Verify */
  22695. /* Requires SP math and supports P384 or P256 */
  22696. /* ./configure --enable-ecc=nonblock --enable-sp=yes,nonblock CFLAGS="-DWOLFSSL_PUBLIC_MP" */
  22697. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_HAVE_SP_ECC) && \
  22698. defined(WOLFSSL_PUBLIC_MP)
  22699. /* ECC Private Key "d" */
  22700. static const byte p256PrivKey[] = {
  22701. /* SECP256R1 */
  22702. /* d */
  22703. 0x1e, 0xe7, 0x70, 0x07, 0xd3, 0x30, 0x94, 0x39,
  22704. 0x28, 0x90, 0xdf, 0x23, 0x88, 0x2c, 0x4a, 0x34,
  22705. 0x15, 0xdb, 0x4c, 0x43, 0xcd, 0xfa, 0xe5, 0x1f,
  22706. 0x3d, 0x4c, 0x37, 0xfe, 0x59, 0x3b, 0x96, 0xd8
  22707. };
  22708. #ifdef HAVE_ECC384
  22709. static const byte p384PrivKey[] = {
  22710. /* SECP384R1 */
  22711. /* d */
  22712. 0xa4, 0xe5, 0x06, 0xe8, 0x06, 0x16, 0x3e, 0xab,
  22713. 0x89, 0xf8, 0x60, 0x43, 0xc0, 0x60, 0x25, 0xdb,
  22714. 0xba, 0x7b, 0xfe, 0x19, 0x35, 0x08, 0x55, 0x65,
  22715. 0x76, 0xe2, 0xdc, 0xe0, 0x01, 0x8b, 0x6b, 0x68,
  22716. 0xdf, 0xcf, 0x6f, 0x80, 0x12, 0xce, 0x79, 0x37,
  22717. 0xeb, 0x2b, 0x9c, 0x7b, 0xc4, 0x68, 0x1c, 0x74
  22718. };
  22719. #endif /* HAVE_ECC384 */
  22720. #ifdef HAVE_ECC521
  22721. static const byte p521PrivKey[] = {
  22722. /* SECP521R1 */
  22723. /* d */
  22724. 0x01, 0x68, 0x91, 0x33, 0x53, 0xe2, 0x90, 0x68,
  22725. 0x11, 0x8f, 0xaa, 0xa8, 0x76, 0x0c, 0xf7, 0x2a,
  22726. 0x07, 0x1b, 0x92, 0x2a, 0xa7, 0x82, 0x3d, 0xfa,
  22727. 0x83, 0xce, 0x70, 0xc8, 0xc2, 0x60, 0x82, 0xfe,
  22728. 0x18, 0x88, 0x68, 0xda, 0x6a, 0x83, 0x46, 0x78,
  22729. 0xe4, 0xe9, 0xe9, 0xcc, 0x51, 0x7f, 0xed, 0x81,
  22730. 0x02, 0x32, 0xee, 0x26, 0x87, 0xcc, 0xed, 0x63,
  22731. 0x3f, 0x39, 0x27, 0xf0, 0xd7, 0x17, 0x77, 0xa1,
  22732. 0xa4, 0x36
  22733. };
  22734. #endif /* HAVE_ECC521 */
  22735. /* ECC public key Qx/Qy */
  22736. static const byte p256PubKey[] = {
  22737. /* SECP256R1 */
  22738. /* Qx */
  22739. 0x96, 0x93, 0x1c, 0x53, 0x0b, 0x43, 0x6c, 0x42,
  22740. 0x0c, 0x52, 0x90, 0xe4, 0xa7, 0xec, 0x98, 0xb1,
  22741. 0xaf, 0xd4, 0x14, 0x49, 0xd8, 0xc1, 0x42, 0x82,
  22742. 0x04, 0x78, 0xd1, 0x90, 0xae, 0xa0, 0x6c, 0x07,
  22743. /* Qy */
  22744. 0xf2, 0x3a, 0xb5, 0x10, 0x32, 0x8d, 0xce, 0x9e,
  22745. 0x76, 0xa0, 0xd2, 0x8c, 0xf3, 0xfc, 0xa9, 0x94,
  22746. 0x43, 0x24, 0xe6, 0x82, 0x00, 0x40, 0xc6, 0xdb,
  22747. 0x1c, 0x2f, 0xcd, 0x38, 0x4b, 0x60, 0xdd, 0x61
  22748. };
  22749. #ifdef HAVE_ECC384
  22750. static const byte p384PubKey[] = {
  22751. /* SECP384R1 */
  22752. /* Qx */
  22753. 0xea, 0xcf, 0x93, 0x4f, 0x2c, 0x09, 0xbb, 0x39,
  22754. 0x14, 0x0f, 0x56, 0x64, 0xc3, 0x40, 0xb4, 0xdf,
  22755. 0x0e, 0x63, 0xae, 0xe5, 0x71, 0x4b, 0x00, 0xcc,
  22756. 0x04, 0x97, 0xff, 0xe1, 0xe9, 0x38, 0x96, 0xbb,
  22757. 0x5f, 0x91, 0xb2, 0x6a, 0xcc, 0xb5, 0x39, 0x5f,
  22758. 0x8f, 0x70, 0x59, 0xf1, 0x01, 0xf6, 0x5a, 0x2b,
  22759. /* Qy */
  22760. 0x01, 0x6c, 0x68, 0x0b, 0xcf, 0x55, 0x25, 0xaf,
  22761. 0x6d, 0x98, 0x48, 0x0a, 0xa8, 0x74, 0xc9, 0xa9,
  22762. 0x17, 0xa0, 0x0c, 0xc3, 0xfb, 0xd3, 0x23, 0x68,
  22763. 0xfe, 0x04, 0x3c, 0x63, 0x50, 0x88, 0x3b, 0xb9,
  22764. 0x4f, 0x7c, 0x67, 0x34, 0xf7, 0x3b, 0xa9, 0x73,
  22765. 0xe7, 0x1b, 0xc3, 0x51, 0x5e, 0x22, 0x18, 0xec
  22766. };
  22767. #endif
  22768. #ifdef HAVE_ECC521
  22769. static const byte p521PubKey[] = {
  22770. /* SECP521R1 */
  22771. /* Qx */
  22772. 0x01, 0x62, 0x6e, 0xf1, 0x00, 0xec, 0xd8, 0x99,
  22773. 0x58, 0x9b, 0x80, 0x6b, 0xfe, 0x2c, 0xf1, 0xb2,
  22774. 0xf0, 0xc8, 0x48, 0xdf, 0xac, 0xd2, 0x3b, 0x71,
  22775. 0x29, 0xab, 0xf0, 0x66, 0x63, 0xd8, 0x8e, 0xb5,
  22776. 0xc8, 0xc2, 0xfc, 0x99, 0x44, 0xe2, 0x45, 0xb1,
  22777. 0x5a, 0x7b, 0xb9, 0x73, 0x01, 0xda, 0x79, 0xec,
  22778. 0x9c, 0x26, 0x27, 0x34, 0x45, 0x26, 0xd5, 0x89,
  22779. 0x4b, 0x44, 0xfe, 0x69, 0x4e, 0x72, 0x14, 0xe3,
  22780. 0x8b, 0xbc,
  22781. /* Qy */
  22782. 0x00, 0x0f, 0x09, 0xa2, 0x03, 0xc3, 0x5a, 0xdc,
  22783. 0x95, 0x82, 0xf6, 0xf9, 0xf6, 0x9c, 0xff, 0xb5,
  22784. 0x6b, 0x75, 0x95, 0x4b, 0xa4, 0x28, 0x5d, 0x9e,
  22785. 0x90, 0x04, 0xd1, 0xc0, 0x1e, 0xd5, 0xfd, 0x43,
  22786. 0x9e, 0x1e, 0x83, 0xc0, 0x11, 0x2b, 0x2b, 0x07,
  22787. 0x6d, 0xa9, 0x7a, 0x10, 0xd7, 0x67, 0xe7, 0x51,
  22788. 0x37, 0x24, 0xd8, 0xbf, 0x03, 0x0d, 0x8b, 0xb5,
  22789. 0x40, 0x5c, 0x4f, 0xd6, 0x13, 0x73, 0x42, 0xbc,
  22790. 0x91, 0xd9
  22791. };
  22792. /* perform verify of signature and hash using public key */
  22793. /* key is public Qx + public Qy */
  22794. /* sig is r + s */
  22795. static int crypto_ecc_verify(const byte *key, uint32_t keySz,
  22796. const byte *hash, uint32_t hashSz, const byte *sig, uint32_t sigSz,
  22797. uint32_t curveSz, int curveId)
  22798. {
  22799. int ret, verify_res = 0, count = 0;
  22800. mp_int r, s;
  22801. ecc_key ecc;
  22802. ecc_nb_ctx_t nb_ctx;
  22803. /* validate arguments */
  22804. if (key == NULL || hash == NULL || sig == NULL || curveSz == 0 ||
  22805. hashSz == 0 || keySz < (curveSz*2) || sigSz < (curveSz*2))
  22806. {
  22807. return BAD_FUNC_ARG;
  22808. }
  22809. /* Setup the ECC key */
  22810. ret = wc_ecc_init(&ecc);
  22811. if (ret < 0) {
  22812. return ret;
  22813. }
  22814. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  22815. if (ret != MP_OKAY) {
  22816. wc_ecc_free(&ecc);
  22817. return ret;
  22818. }
  22819. /* Setup the signature r/s variables */
  22820. ret = mp_init(&r);
  22821. if (ret != MP_OKAY) {
  22822. wc_ecc_free(&ecc);
  22823. return ret;
  22824. }
  22825. ret = mp_init(&s);
  22826. if (ret != MP_OKAY) {
  22827. mp_clear(&r);
  22828. wc_ecc_free(&ecc);
  22829. return ret;
  22830. }
  22831. /* Import public key x/y */
  22832. ret = wc_ecc_import_unsigned(
  22833. &ecc,
  22834. (byte*)key, /* Public "x" Coordinate */
  22835. (byte*)(key + curveSz), /* Public "y" Coordinate */
  22836. NULL, /* Private "d" (optional) */
  22837. curveId /* ECC Curve Id */
  22838. );
  22839. /* Make sure it was a public key imported */
  22840. if (ret == 0 && ecc.type != ECC_PUBLICKEY) {
  22841. ret = ECC_BAD_ARG_E;
  22842. }
  22843. /* Import signature r/s */
  22844. if (ret == 0) {
  22845. ret = mp_read_unsigned_bin(&r, sig, curveSz);
  22846. }
  22847. if (ret == 0) {
  22848. ret = mp_read_unsigned_bin(&s, sig + curveSz, curveSz);
  22849. }
  22850. /* Verify ECC Signature */
  22851. if (ret == 0) {
  22852. do {
  22853. ret = wc_ecc_verify_hash_ex(
  22854. &r, &s, /* r/s as mp_int */
  22855. hash, hashSz, /* computed hash digest */
  22856. &verify_res, /* verification result 1=success */
  22857. &ecc
  22858. );
  22859. count++;
  22860. /* This is where real-time work could be called */
  22861. } while (ret == FP_WOULDBLOCK);
  22862. #ifdef DEBUG_WOLFSSL
  22863. printf("ECC non-block verify: %d times\n", count);
  22864. #endif
  22865. }
  22866. /* check verify result */
  22867. if (ret == 0 && verify_res == 0) {
  22868. ret = SIG_VERIFY_E;
  22869. }
  22870. mp_clear(&r);
  22871. mp_clear(&s);
  22872. wc_ecc_free(&ecc);
  22873. (void)count;
  22874. return ret;
  22875. }
  22876. /* perform signature operation against hash using private key */
  22877. static int crypto_ecc_sign(const byte *key, uint32_t keySz,
  22878. const byte *hash, uint32_t hashSz, byte *sig, uint32_t* sigSz,
  22879. uint32_t curveSz, int curveId, WC_RNG* rng)
  22880. {
  22881. int ret, count = 0;
  22882. mp_int r, s;
  22883. ecc_key ecc;
  22884. ecc_nb_ctx_t nb_ctx;
  22885. /* validate arguments */
  22886. if (key == NULL || hash == NULL || sig == NULL || sigSz == NULL ||
  22887. curveSz == 0 || hashSz == 0 || keySz < curveSz || *sigSz < (curveSz*2))
  22888. {
  22889. return BAD_FUNC_ARG;
  22890. }
  22891. /* Initialize signature result */
  22892. memset(sig, 0, curveSz*2);
  22893. /* Setup the ECC key */
  22894. ret = wc_ecc_init(&ecc);
  22895. if (ret < 0) {
  22896. return ret;
  22897. }
  22898. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  22899. if (ret != MP_OKAY) {
  22900. wc_ecc_free(&ecc);
  22901. return ret;
  22902. }
  22903. /* Setup the signature r/s variables */
  22904. ret = mp_init(&r);
  22905. if (ret != MP_OKAY) {
  22906. wc_ecc_free(&ecc);
  22907. return ret;
  22908. }
  22909. ret = mp_init(&s);
  22910. if (ret != MP_OKAY) {
  22911. mp_clear(&r);
  22912. wc_ecc_free(&ecc);
  22913. return ret;
  22914. }
  22915. /* Import private key "k" */
  22916. ret = wc_ecc_import_private_key_ex(
  22917. key, keySz, /* private key "d" */
  22918. NULL, 0, /* public (optional) */
  22919. &ecc,
  22920. curveId /* ECC Curve Id */
  22921. );
  22922. if (ret == 0) {
  22923. do {
  22924. /* Verify ECC Signature */
  22925. ret = wc_ecc_sign_hash_ex(
  22926. hash, hashSz, /* computed hash digest */
  22927. rng, &ecc, /* random and key context */
  22928. &r, &s /* r/s as mp_int */
  22929. );
  22930. count++;
  22931. /* This is where real-time work could be called */
  22932. } while (ret == FP_WOULDBLOCK);
  22933. #ifdef DEBUG_WOLFSSL
  22934. printf("ECC non-block sign: %d times\n", count);
  22935. #endif
  22936. }
  22937. if (ret == 0) {
  22938. /* export r/s */
  22939. mp_to_unsigned_bin_len(&r, sig, curveSz);
  22940. mp_to_unsigned_bin_len(&s, sig + curveSz, curveSz);
  22941. }
  22942. mp_clear(&r);
  22943. mp_clear(&s);
  22944. wc_ecc_free(&ecc);
  22945. (void)count;
  22946. return ret;
  22947. }
  22948. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  22949. /*
  22950. * This test doesn't work with WOLFSSL_VALIDATE_ECC_KEYGEN defined because we
  22951. * don't have non-blocking versions of the key checking functions, yet.
  22952. */
  22953. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_VALIDATE_ECC_KEYGEN)
  22954. static int ecc_test_nonblock_dhe(int curveId, word32 curveSz,
  22955. const byte* privKey, const byte* pubKey, WC_RNG* rng)
  22956. {
  22957. int ret;
  22958. ecc_key keyA;
  22959. ecc_key keyB;
  22960. ecc_nb_ctx_t nbCtxA;
  22961. ecc_nb_ctx_t nbCtxB;
  22962. byte secretA[ECC_SHARED_SIZE];
  22963. byte secretB[ECC_SHARED_SIZE];
  22964. word32 secretSzA = ECC_SHARED_SIZE;
  22965. word32 secretSzB = ECC_SHARED_SIZE;
  22966. int count = 0;
  22967. ret = wc_ecc_init(&keyA);
  22968. if (ret == 0) {
  22969. ret = wc_ecc_init(&keyB);
  22970. }
  22971. if (ret == 0) {
  22972. ret = wc_ecc_set_nonblock(&keyA, &nbCtxA);
  22973. }
  22974. if (ret == 0) {
  22975. ret = wc_ecc_set_nonblock(&keyB, &nbCtxB);
  22976. }
  22977. if (ret == 0) {
  22978. do {
  22979. ret = wc_ecc_make_key_ex(rng, curveSz, &keyA, curveId);
  22980. count++;
  22981. } while (ret == FP_WOULDBLOCK);
  22982. }
  22983. #ifdef DEBUG_WOLFSSL
  22984. fprintf(stderr, "ECC non-block key gen: %d times\n", count);
  22985. #endif
  22986. if (ret == 0) {
  22987. ret = wc_ecc_check_key(&keyA);
  22988. }
  22989. if (ret == 0) {
  22990. ret = wc_ecc_import_unsigned(&keyB, pubKey, pubKey + curveSz,
  22991. privKey, curveId);
  22992. }
  22993. count = 0;
  22994. if (ret == 0) {
  22995. do {
  22996. ret = wc_ecc_shared_secret(&keyA, &keyB, secretA, &secretSzA);
  22997. count++;
  22998. } while (ret == FP_WOULDBLOCK);
  22999. }
  23000. #ifdef DEBUG_WOLFSSL
  23001. fprintf(stderr, "ECC non-block shared secret: %d times\n", count);
  23002. #endif
  23003. if (ret == 0) {
  23004. do {
  23005. ret = wc_ecc_shared_secret(&keyB, &keyA, secretB, &secretSzB);
  23006. } while (ret == FP_WOULDBLOCK);
  23007. }
  23008. if (ret == 0) {
  23009. if (secretSzA != secretSzB ||
  23010. XMEMCMP(secretA, secretB, secretSzA) != 0) {
  23011. ret = -1;
  23012. }
  23013. }
  23014. wc_ecc_free(&keyA);
  23015. wc_ecc_free(&keyB);
  23016. return ret;
  23017. }
  23018. #endif /* HAVE_ECC_DHE && !WOLFSSL_VALIDATE_ECC_KEYGEN */
  23019. #if defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  23020. static int ecc_test_nonblock_ecdsa(int curveId, word32 curveSz,
  23021. const byte* privKey, word32 privKeySz, const byte* pubKey, word32 pubKeySz,
  23022. WC_RNG* rng)
  23023. {
  23024. int ret = 0;
  23025. byte* sig = NULL;
  23026. word32 sigSz = curveSz * 2;
  23027. static const byte hash[] = {
  23028. 0x8d, 0x28, 0xa3, 0x8b, 0x0b, 0xa9, 0xfe, 0xd4, 0x0e, 0x54, 0xc4, 0x17,
  23029. 0x3d, 0x54, 0x66, 0x34, 0xbf, 0x5d, 0x6f, 0x46, 0xc2, 0x20, 0xcb, 0xc3,
  23030. 0x22, 0xe9, 0xb0, 0xdf, 0xe7, 0x64, 0x3f, 0xd9
  23031. };
  23032. sig = (byte*)XMALLOC(sigSz, HEAP_HINT, DYNAMIC_TYPE_SIGNATURE);
  23033. if (sig == NULL) {
  23034. ret = -1;
  23035. }
  23036. if (ret == 0) {
  23037. /* Sign hash using private key */
  23038. /* Note: result of an ECC sign varies for each call even with same
  23039. private key and hash. This is because a new random public key is
  23040. used for each operation. */
  23041. ret = crypto_ecc_sign(privKey, privKeySz, hash, sizeof(hash), sig,
  23042. &sigSz, curveSz, curveId, rng);
  23043. }
  23044. if (ret == 0) {
  23045. /* Verify generated signature is valid */
  23046. ret = crypto_ecc_verify(pubKey, pubKeySz, hash, sizeof(hash), sig,
  23047. sigSz, curveSz, curveId);
  23048. }
  23049. if (sig != NULL) {
  23050. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_SIGNATURE);
  23051. }
  23052. return ret;
  23053. }
  23054. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  23055. static int ecc_test_nonblock(WC_RNG* rng)
  23056. {
  23057. int ret = 0;
  23058. word32 i;
  23059. int curveIds[3] = {0, 0, 0};
  23060. word32 curveSzs[3] = {0, 0, 0};
  23061. const byte* privKeys[3] = {NULL, NULL, NULL};
  23062. word32 privKeySzs[3] = {0, 0, 0};
  23063. const byte* pubKeys[3] = {NULL, NULL, NULL};
  23064. word32 pubKeySzs[3] = {0, 0, 0};
  23065. curveIds[0] = ECC_SECP256R1;
  23066. curveSzs[0] = 32;
  23067. privKeys[0] = p256PrivKey;
  23068. privKeySzs[0] = sizeof(p256PrivKey);
  23069. pubKeys[0] = p256PubKey;
  23070. pubKeySzs[0] = sizeof(p256PubKey);
  23071. #ifdef HAVE_ECC384
  23072. curveIds[1] = ECC_SECP384R1;
  23073. curveSzs[1] = 48;
  23074. privKeys[1] = p384PrivKey;
  23075. privKeySzs[1] = sizeof(p384PrivKey);
  23076. pubKeys[1] = p384PubKey;
  23077. pubKeySzs[1] = sizeof(p384PubKey);
  23078. #endif
  23079. #ifdef HAVE_ECC521
  23080. curveIds[2] = ECC_SECP521R1;
  23081. curveSzs[2] = 66;
  23082. privKeys[2] = p521PrivKey;
  23083. privKeySzs[2] = sizeof(p521PrivKey);
  23084. pubKeys[2] = p521PubKey;
  23085. pubKeySzs[2] = sizeof(p521PubKey);
  23086. #endif
  23087. for (i = 0; ret == 0 && i < sizeof(curveIds) / sizeof(curveIds[0]); ++i) {
  23088. if (curveIds[i] == 0) {
  23089. continue;
  23090. }
  23091. #if defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  23092. ret = ecc_test_nonblock_ecdsa(curveIds[i], curveSzs[i], privKeys[i],
  23093. privKeySzs[i], pubKeys[i], pubKeySzs[i], rng);
  23094. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  23095. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_VALIDATE_ECC_KEYGEN)
  23096. if (ret == 0) {
  23097. ret = ecc_test_nonblock_dhe(curveIds[i], curveSzs[i], privKeys[i],
  23098. pubKeys[i], rng);
  23099. }
  23100. #endif /* HAVE_ECC_DHE && !WOLFSSL_VALIDATE_ECC_KEYGEN */
  23101. }
  23102. return ret;
  23103. }
  23104. #endif /* WC_ECC_NONBLOCK && WOLFSSL_HAVE_SP_ECC && WOLFSSL_PUBLIC_MP */
  23105. WOLFSSL_TEST_SUBROUTINE int ecc_test(void)
  23106. {
  23107. int ret;
  23108. WC_RNG rng;
  23109. #if defined(WOLFSSL_CERT_EXT) && \
  23110. (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  23111. ret = ecc_decode_test();
  23112. if (ret < 0)
  23113. return ret;
  23114. #endif
  23115. #ifndef HAVE_FIPS
  23116. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  23117. #else
  23118. ret = wc_InitRng(&rng);
  23119. #endif
  23120. #ifndef WC_NO_RNG
  23121. if (ret != 0)
  23122. return -10300;
  23123. #else
  23124. (void)ret;
  23125. #endif
  23126. #if (defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 112
  23127. ret = ecc_test_curve(&rng, 14);
  23128. if (ret < 0) {
  23129. goto done;
  23130. }
  23131. #endif /* HAVE_ECC112 */
  23132. #if (defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 128
  23133. ret = ecc_test_curve(&rng, 16);
  23134. if (ret < 0) {
  23135. goto done;
  23136. }
  23137. #endif /* HAVE_ECC128 */
  23138. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  23139. ret = ecc_test_curve(&rng, 20);
  23140. if (ret < 0) {
  23141. goto done;
  23142. }
  23143. #endif /* HAVE_ECC160 */
  23144. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  23145. ret = ecc_test_curve(&rng, 24);
  23146. if (ret < 0) {
  23147. goto done;
  23148. }
  23149. #endif /* HAVE_ECC192 */
  23150. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  23151. ret = ecc_test_curve(&rng, 28);
  23152. if (ret < 0) {
  23153. goto done;
  23154. }
  23155. #endif /* HAVE_ECC224 */
  23156. #if (defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 239
  23157. ret = ecc_test_curve(&rng, 30);
  23158. if (ret < 0) {
  23159. goto done;
  23160. }
  23161. #endif /* HAVE_ECC239 */
  23162. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  23163. ret = ecc_test_curve(&rng, 32);
  23164. if (ret < 0) {
  23165. goto done;
  23166. }
  23167. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  23168. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  23169. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23170. ret = ecc_point_test();
  23171. if (ret < 0) {
  23172. goto done;
  23173. }
  23174. #endif
  23175. ret = ecc_def_curve_test(&rng);
  23176. if (ret < 0) {
  23177. goto done;
  23178. }
  23179. #endif /* !NO_ECC256 */
  23180. #if (defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 320
  23181. ret = ecc_test_curve(&rng, 40);
  23182. if (ret < 0) {
  23183. goto done;
  23184. }
  23185. #endif /* HAVE_ECC320 */
  23186. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  23187. ret = ecc_test_curve(&rng, 48);
  23188. if (ret < 0) {
  23189. goto done;
  23190. }
  23191. #endif /* HAVE_ECC384 */
  23192. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  23193. ret = ecc_test_curve(&rng, 64);
  23194. if (ret < 0) {
  23195. goto done;
  23196. }
  23197. #endif /* HAVE_ECC512 */
  23198. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  23199. ret = ecc_test_curve(&rng, 66);
  23200. if (ret < 0) {
  23201. goto done;
  23202. }
  23203. #endif /* HAVE_ECC521 */
  23204. #if defined(WOLFSSL_CUSTOM_CURVES)
  23205. ret = ecc_test_custom_curves(&rng);
  23206. if (ret != 0) {
  23207. goto done;
  23208. }
  23209. #endif
  23210. #if defined(HAVE_ECC_SIGN) && (defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  23211. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)) \
  23212. && (!defined(FIPS_VERSION_GE) || FIPS_VERSION_GE(5,3))
  23213. #ifdef HAVE_ECC256
  23214. ret = ecc_test_deterministic_k(&rng);
  23215. if (ret != 0) {
  23216. printf("ecc_test_deterministic_k failed! %d\n", ret);
  23217. goto done;
  23218. }
  23219. #endif
  23220. #ifdef WOLFSSL_PUBLIC_MP
  23221. #if defined(HAVE_ECC384)
  23222. ret = ecc384_test_deterministic_k(&rng);
  23223. if (ret != 0) {
  23224. printf("ecc384_test_deterministic_k failed! %d\n", ret);
  23225. goto done;
  23226. }
  23227. #endif
  23228. #if defined(HAVE_ECC521)
  23229. ret = ecc521_test_deterministic_k(&rng);
  23230. if (ret != 0) {
  23231. printf("ecc512_test_deterministic_k failed! %d\n", ret);
  23232. goto done;
  23233. }
  23234. #endif
  23235. #endif
  23236. #endif
  23237. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K) && \
  23238. !defined(WOLFSSL_KCAPI_ECC)
  23239. ret = ecc_test_sign_vectors(&rng);
  23240. if (ret != 0) {
  23241. printf("ecc_test_sign_vectors failed! %d\n", ret);
  23242. goto done;
  23243. }
  23244. #endif
  23245. #ifdef HAVE_ECC_CDH
  23246. ret = ecc_test_cdh_vectors(&rng);
  23247. if (ret != 0) {
  23248. printf("ecc_test_cdh_vectors failed! %d\n", ret);
  23249. goto done;
  23250. }
  23251. #endif
  23252. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  23253. !defined(WOLFSSL_STM32_PKA) && !defined(WOLFSSL_SILABS_SE_ACCEL) && \
  23254. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23255. ret = ecc_test_make_pub(&rng);
  23256. if (ret != 0) {
  23257. printf("ecc_test_make_pub failed!: %d\n", ret);
  23258. goto done;
  23259. }
  23260. #elif defined(HAVE_ECC_KEY_IMPORT)
  23261. (void)ecc_test_make_pub; /* for compiler warning */
  23262. #endif
  23263. #ifdef WOLFSSL_CERT_GEN
  23264. ret = ecc_test_cert_gen(&rng);
  23265. if (ret != 0) {
  23266. printf("ecc_test_cert_gen failed!: %d\n", ret);
  23267. goto done;
  23268. }
  23269. #endif
  23270. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC) && \
  23271. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23272. ret = ecc_test_allocator(&rng);
  23273. if (ret != 0) {
  23274. printf("ecc_test_allocator failed!: %d\n", ret);
  23275. goto done;
  23276. }
  23277. #endif
  23278. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_PUBLIC_MP) && \
  23279. defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  23280. ret = ecc_test_nonblock(&rng);
  23281. if (ret != 0) {
  23282. printf("ecc_test_nonblock failed!: %d\n", ret);
  23283. goto done;
  23284. }
  23285. #endif
  23286. done:
  23287. wc_FreeRng(&rng);
  23288. return ret;
  23289. }
  23290. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  23291. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  23292. /* ecc_encrypt_e2e_test() uses wc_ecc_ctx_set_algo(), which was added in
  23293. * wolfFIPS 5.3.
  23294. * ecc_encrypt_kat() is used only by ecc_encrypt_e2e_test().
  23295. */
  23296. #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))
  23297. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  23298. ECC_MIN_KEY_SZ <= 256 && defined(WOLFSSL_AES_128)
  23299. static int ecc_encrypt_kat(WC_RNG *rng)
  23300. {
  23301. int ret = 0;
  23302. #ifdef WOLFSSL_ECIES_OLD
  23303. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23304. ecc_key* userA = NULL;
  23305. #else
  23306. ecc_key userA[1];
  23307. #endif
  23308. int userAInit = 0;
  23309. #endif
  23310. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23311. ecc_key* userB = NULL;
  23312. #else
  23313. ecc_key userB[1];
  23314. #endif
  23315. int userBInit = 0;
  23316. ecc_key* tmpKey;
  23317. byte plain[48];
  23318. word32 plainSz = sizeof(plain);
  23319. WOLFSSL_SMALL_STACK_STATIC const byte privKey[] = {
  23320. 0x04, 0x80, 0xef, 0x1d, 0xbe, 0x02, 0x0c, 0x20,
  23321. 0x5b, 0xab, 0x80, 0x35, 0x5b, 0x2a, 0x0f, 0x6d,
  23322. 0xd3, 0xb0, 0x7f, 0x7e, 0x7f, 0x86, 0x8a, 0x49,
  23323. 0xee, 0xb4, 0xaa, 0x09, 0x2d, 0x1e, 0x1d, 0x02
  23324. };
  23325. #if defined(WOLFSSL_ECIES_OLD) || defined(WOLFSSL_QNX_CAAM)
  23326. WOLFSSL_SMALL_STACK_STATIC const byte pubKey[] = {
  23327. 0x04,
  23328. /* X */
  23329. 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0, 0x5a,
  23330. 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c, 0x3a,
  23331. 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3, 0xc1,
  23332. 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63, 0xa0,
  23333. /* X */
  23334. 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7, 0xcd,
  23335. 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75, 0xaa,
  23336. 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe, 0xe8,
  23337. 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3, 0x28
  23338. };
  23339. #endif
  23340. WOLFSSL_SMALL_STACK_STATIC const byte enc_msg[] = {
  23341. #ifdef WOLFSSL_ECIES_OLD
  23342. 0x42, 0x70, 0xbf, 0xf9, 0xf4, 0x7e, 0x4b, 0x9b,
  23343. 0xb5, 0x4c, 0xcc, 0xc5, 0x94, 0xa7, 0xef, 0xaa,
  23344. 0xc3, 0x7c, 0x85, 0xa6, 0x51, 0x6e, 0xd3, 0xfa,
  23345. 0x56, 0xc9, 0x10, 0x4d, 0x14, 0x32, 0x61, 0xb8,
  23346. 0xbb, 0x66, 0x7a, 0xb5, 0xbc, 0x95, 0xf8, 0xca,
  23347. 0xd1, 0x2a, 0x19, 0x51, 0x44, 0xd8, 0x0e, 0x57,
  23348. 0x34, 0xed, 0x45, 0x89, 0x2e, 0x57, 0xbe, 0xd5,
  23349. 0x06, 0x22, 0xd7, 0x13, 0x0a, 0x0e, 0x40, 0x36,
  23350. 0x0d, 0x05, 0x0d, 0xb6, 0xae, 0x61, 0x37, 0x18,
  23351. 0x83, 0x90, 0x0a, 0x27, 0x95, 0x41, 0x8c, 0x45
  23352. #elif defined(WOLFSSL_ECIES_ISO18033)
  23353. 0x04, 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0,
  23354. 0x5a, 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c,
  23355. 0x3a, 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3,
  23356. 0xc1, 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63,
  23357. 0xa0, 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7,
  23358. 0xcd, 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75,
  23359. 0xaa, 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe,
  23360. 0xe8, 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3,
  23361. 0x28, 0xbb, 0x9f, 0xa8, 0x2d, 0xe1, 0xf1, 0x67,
  23362. 0x45, 0x02, 0x19, 0xdc, 0xc8, 0x24, 0x8b, 0x20,
  23363. 0x02, 0xa0, 0x8f, 0x95, 0x12, 0x55, 0x51, 0xf8,
  23364. 0x03, 0xc4, 0x54, 0x13, 0x98, 0x2d, 0xf0, 0x31,
  23365. 0x51, 0x80, 0x45, 0x24, 0xcb, 0x8b, 0x48, 0xa6,
  23366. 0x8b, 0x8e, 0x97, 0x9c, 0x56, 0x4d, 0x70, 0x00,
  23367. 0x53, 0xd3, 0x47, 0x00, 0x5a, 0x23, 0x8c, 0xf9,
  23368. 0xfd, 0xd2, 0x33, 0x2c, 0x43, 0x6e, 0x9e, 0xb2,
  23369. 0xf4, 0x95, 0xd4, 0xcf, 0x30, 0xd6, 0xa2, 0xc5,
  23370. 0x35, 0x96, 0x6a, 0xd4, 0x36, 0x15, 0xa9, 0xbd,
  23371. 0x7f
  23372. #elif defined(WOLFSSL_ECIES_GEN_IV)
  23373. /* EC P-256 point */
  23374. 0x04,
  23375. /* X */
  23376. 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0, 0x5a,
  23377. 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c, 0x3a,
  23378. 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3, 0xc1,
  23379. 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63, 0xa0,
  23380. /* Y */
  23381. 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7, 0xcd,
  23382. 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75, 0xaa,
  23383. 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe, 0xe8,
  23384. 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3, 0x28,
  23385. /* IV */
  23386. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  23387. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  23388. /* Encrypted Msg */
  23389. 0xe5, 0x17, 0xaf, 0x0d, 0x65, 0x4d, 0x3d, 0x50,
  23390. 0x96, 0x05, 0xc9, 0x63, 0x2c, 0xef, 0x1c, 0x1f,
  23391. 0x78, 0xc9, 0x90, 0x7a, 0x14, 0x00, 0xfc, 0x44,
  23392. 0x71, 0x6d, 0x57, 0x8c, 0xdf, 0x23, 0xca, 0x65,
  23393. 0xcf, 0x93, 0x06, 0xb6, 0x9a, 0xf4, 0x61, 0xbd,
  23394. 0x44, 0x1a, 0xeb, 0x52, 0x68, 0x0f, 0xd1, 0xde,
  23395. /* HMAC */
  23396. 0x5a, 0x22, 0xc1, 0x5d, 0x99, 0x66, 0x3f, 0x24,
  23397. 0x35, 0x96, 0xac, 0xf7, 0xf6, 0x28, 0x45, 0x16,
  23398. 0x52, 0x19, 0x0d, 0xe4, 0xb2, 0xca, 0x5b, 0x28,
  23399. 0x4e, 0xbb, 0xf3, 0x98, 0x57, 0xd7, 0x3b, 0xe2
  23400. #else
  23401. 0x04, 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0,
  23402. 0x5a, 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c,
  23403. 0x3a, 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3,
  23404. 0xc1, 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63,
  23405. 0xa0, 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7,
  23406. 0xcd, 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75,
  23407. 0xaa, 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe,
  23408. 0xe8, 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3,
  23409. 0x28, 0xe5, 0x17, 0xaf, 0x0d, 0x65, 0x4d, 0x3d,
  23410. 0x50, 0x96, 0x05, 0xc9, 0x63, 0x2c, 0xef, 0x1c,
  23411. 0x1f, 0x78, 0xc9, 0x90, 0x7a, 0x14, 0x00, 0xfc,
  23412. 0x44, 0x71, 0x6d, 0x57, 0x8c, 0xdf, 0x23, 0xca,
  23413. 0x65, 0xcf, 0x93, 0x06, 0xb6, 0x9a, 0xf4, 0x61,
  23414. 0xbd, 0x44, 0x1a, 0xeb, 0x52, 0x68, 0x0f, 0xd1,
  23415. 0xde, 0xc7, 0x3f, 0x6f, 0xce, 0xbe, 0x49, 0x61,
  23416. 0x48, 0x01, 0x77, 0x41, 0xd0, 0xd8, 0x5b, 0x48,
  23417. 0xca, 0x4e, 0x47, 0x3e, 0x47, 0xbf, 0x1d, 0x28,
  23418. 0x4c, 0x18, 0x1a, 0xfb, 0x96, 0x95, 0xda, 0xde,
  23419. 0x55
  23420. #endif
  23421. };
  23422. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  23423. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  23424. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  23425. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  23426. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  23427. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  23428. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  23429. };
  23430. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23431. userB = (ecc_key *)XMALLOC(sizeof(*userB), HEAP_HINT,
  23432. DYNAMIC_TYPE_TMP_BUFFER);
  23433. if (userB == NULL) {
  23434. ret = -10451;
  23435. }
  23436. #ifdef WOLFSSL_ECIES_OLD
  23437. if (ret == 0) {
  23438. userA = (ecc_key *)XMALLOC(sizeof(*userA), HEAP_HINT,
  23439. DYNAMIC_TYPE_TMP_BUFFER);
  23440. if (userA == NULL) {
  23441. ret = -10450;
  23442. }
  23443. }
  23444. #endif
  23445. #endif
  23446. if (ret == 0) {
  23447. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  23448. if (ret != 0)
  23449. ret = -10453;
  23450. }
  23451. if (ret == 0) {
  23452. userBInit = 1;
  23453. #ifdef WOLFSSL_ECIES_OLD
  23454. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  23455. if (ret != 0)
  23456. ret = -10452;
  23457. }
  23458. if (ret == 0) {
  23459. userAInit = 1;
  23460. tmpKey = userA;
  23461. #else
  23462. tmpKey = NULL;
  23463. #endif
  23464. }
  23465. if (ret == 0) {
  23466. #ifdef WOLFSSL_QNX_CAAM
  23467. ret = wc_ecc_import_private_key_ex(privKey, sizeof(privKey), pubKey,
  23468. sizeof(pubKey), userB, ECC_SECP256R1);
  23469. #else
  23470. ret = wc_ecc_import_private_key_ex(privKey, sizeof(privKey), NULL, 0,
  23471. userB, ECC_SECP256R1);
  23472. #endif
  23473. if (ret != 0)
  23474. ret = -10454;
  23475. }
  23476. #ifdef WOLFSSL_ECIES_OLD
  23477. if (ret == 0) {
  23478. ret = wc_ecc_import_x963_ex(pubKey, sizeof(pubKey), userA,
  23479. ECC_SECP256R1);
  23480. if (ret != 0)
  23481. ret = -10455;
  23482. }
  23483. #endif
  23484. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  23485. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  23486. !defined(HAVE_SELFTEST)
  23487. if (ret == 0) {
  23488. ret = wc_ecc_set_rng(userB, rng);
  23489. if (ret != 0) {
  23490. ret = -10456;
  23491. }
  23492. }
  23493. #else
  23494. (void)rng;
  23495. #endif
  23496. if (ret == 0) {
  23497. ret = wc_ecc_decrypt(userB, tmpKey, enc_msg, sizeof(enc_msg), plain,
  23498. &plainSz, NULL);
  23499. if (ret != 0)
  23500. ret = -10457;
  23501. }
  23502. if (ret == 0) {
  23503. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  23504. ret = -10458;
  23505. }
  23506. }
  23507. if (userBInit)
  23508. wc_ecc_free(userB);
  23509. #ifdef WOLFSSL_ECIES_OLD
  23510. if (userAInit)
  23511. wc_ecc_free(userA);
  23512. #endif
  23513. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23514. if (userB != NULL) {
  23515. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23516. }
  23517. #ifdef WOLFSSL_ECIES_OLD
  23518. if (userA != NULL) {
  23519. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23520. }
  23521. #endif
  23522. #endif
  23523. return ret;
  23524. }
  23525. #endif
  23526. static int ecc_encrypt_e2e_test(WC_RNG* rng, ecc_key* userA, ecc_key* userB,
  23527. byte encAlgo, byte kdfAlgo, byte macAlgo)
  23528. {
  23529. int ret = 0;
  23530. byte msg[48];
  23531. byte plain[48];
  23532. #ifdef WOLFSSL_ECIES_OLD
  23533. byte out[80];
  23534. #elif defined(WOLFSSL_ECIES_GEN_IV)
  23535. byte out[1 + ECC_KEYGEN_SIZE * 2 + 16 + 80];
  23536. #else
  23537. byte out[1 + ECC_KEYGEN_SIZE * 2 + 80];
  23538. #endif
  23539. word32 outSz = sizeof(out);
  23540. word32 plainSz = sizeof(plain);
  23541. int i;
  23542. ecEncCtx* cliCtx = NULL;
  23543. ecEncCtx* srvCtx = NULL;
  23544. byte cliSalt[EXCHANGE_SALT_SZ];
  23545. byte srvSalt[EXCHANGE_SALT_SZ];
  23546. const byte* tmpSalt;
  23547. byte msg2[48];
  23548. byte plain2[48];
  23549. #ifdef WOLFSSL_ECIES_OLD
  23550. byte out2[80];
  23551. #elif defined(WOLFSSL_ECIES_GEN_IV)
  23552. byte out2[1 + ECC_KEYGEN_SIZE * 2 + 16 + 80];
  23553. #else
  23554. byte out2[1 + ECC_KEYGEN_SIZE * 2 + 80];
  23555. #endif
  23556. word32 outSz2 = sizeof(out2);
  23557. word32 plainSz2 = sizeof(plain2);
  23558. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23559. ecc_key *tmpKey = (ecc_key *)XMALLOC(sizeof(ecc_key), HEAP_HINT,
  23560. DYNAMIC_TYPE_TMP_BUFFER);
  23561. #else
  23562. ecc_key tmpKey[1];
  23563. #endif
  23564. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23565. if (tmpKey == NULL) {
  23566. ERROR_OUT(MEMORY_E, done);
  23567. }
  23568. #endif
  23569. ret = wc_ecc_init_ex(tmpKey, HEAP_HINT, devId);
  23570. if (ret != 0)
  23571. goto done;
  23572. /* set message to incrementing 0,1,2,etc... */
  23573. for (i = 0; i < (int)sizeof(msg); i++)
  23574. msg[i] = i;
  23575. /* encrypt msg to B */
  23576. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz, NULL);
  23577. if (ret != 0) {
  23578. ret = -10405; goto done;
  23579. }
  23580. #ifdef WOLFSSL_ECIES_OLD
  23581. tmpKey->dp = userA->dp;
  23582. ret = wc_ecc_copy_point(&userA->pubkey, &tmpKey->pubkey);
  23583. if (ret != 0) {
  23584. ret = -10413; goto done;
  23585. }
  23586. #endif
  23587. /* decrypt msg from A */
  23588. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, NULL);
  23589. if (ret != 0) {
  23590. ret = -10406; goto done;
  23591. }
  23592. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  23593. ret = -10407; goto done;
  23594. }
  23595. #ifndef WOLFSSL_ECIES_OLD
  23596. /* A decrypts msg (response) from B */
  23597. ret = wc_ecc_decrypt(userB, NULL, out, outSz, plain2, &plainSz2, NULL);
  23598. if (ret != 0)
  23599. goto done;
  23600. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  23601. ret = -10415; goto done;
  23602. }
  23603. #endif
  23604. /* let's verify message exchange works, A is client, B is server */
  23605. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  23606. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  23607. if (cliCtx == NULL || srvCtx == NULL) {
  23608. ret = -10408; goto done;
  23609. }
  23610. ret = wc_ecc_ctx_set_algo(cliCtx, encAlgo, kdfAlgo, macAlgo);
  23611. if (ret != 0)
  23612. goto done;
  23613. ret = wc_ecc_ctx_set_algo(srvCtx, encAlgo, kdfAlgo, macAlgo);
  23614. if (ret != 0)
  23615. goto done;
  23616. /* get salt to send to peer */
  23617. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  23618. if (tmpSalt == NULL) {
  23619. ret = -10409; goto done;
  23620. }
  23621. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  23622. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  23623. if (tmpSalt == NULL) {
  23624. ret = -10410; goto done;
  23625. }
  23626. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  23627. /* in actual use, we'd get the peer's salt over the transport */
  23628. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  23629. if (ret != 0)
  23630. goto done;
  23631. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  23632. if (ret != 0)
  23633. goto done;
  23634. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 11);
  23635. if (ret != 0)
  23636. goto done;
  23637. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 11);
  23638. if (ret != 0)
  23639. goto done;
  23640. /* get encrypted msg (request) to send to B */
  23641. outSz = sizeof(out);
  23642. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz,cliCtx);
  23643. if (ret != 0)
  23644. goto done;
  23645. #ifndef WOLFSSL_ECIES_OLD
  23646. wc_ecc_free(tmpKey);
  23647. #endif
  23648. /* B decrypts msg (request) from A */
  23649. plainSz = sizeof(plain);
  23650. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, srvCtx);
  23651. if (ret != 0)
  23652. goto done;
  23653. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  23654. ret = -10411; goto done;
  23655. }
  23656. /* msg2 (response) from B to A */
  23657. for (i = 0; i < (int)sizeof(msg2); i++)
  23658. msg2[i] = i + sizeof(msg2);
  23659. /* get encrypted msg (response) to send to B */
  23660. ret = wc_ecc_encrypt(userB, userA, msg2, sizeof(msg2), out2,
  23661. &outSz2, srvCtx);
  23662. if (ret != 0)
  23663. goto done;
  23664. #ifdef WOLFSSL_ECIES_OLD
  23665. tmpKey->dp = userB->dp;
  23666. ret = wc_ecc_copy_point(&userB->pubkey, &tmpKey->pubkey);
  23667. if (ret != 0) {
  23668. ret = -10414; goto done;
  23669. }
  23670. #else
  23671. wc_ecc_free(tmpKey);
  23672. #endif
  23673. /* A decrypts msg (response) from B */
  23674. ret = wc_ecc_decrypt(userA, tmpKey, out2, outSz2, plain2, &plainSz2,
  23675. cliCtx);
  23676. if (ret != 0)
  23677. goto done;
  23678. if (XMEMCMP(plain2, msg2, sizeof(msg2)) != 0) {
  23679. ret = -10412; goto done;
  23680. }
  23681. #if defined(HAVE_COMP_KEY) && \
  23682. (! defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  23683. /* Create new client and server contexts. */
  23684. wc_ecc_ctx_free(srvCtx);
  23685. wc_ecc_ctx_free(cliCtx);
  23686. /* let's verify message exchange works, A is client, B is server */
  23687. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  23688. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  23689. if (cliCtx == NULL || srvCtx == NULL) {
  23690. ret = -10416; goto done;
  23691. }
  23692. ret = wc_ecc_ctx_set_algo(cliCtx, encAlgo, kdfAlgo, macAlgo);
  23693. if (ret != 0)
  23694. goto done;
  23695. ret = wc_ecc_ctx_set_algo(srvCtx, encAlgo, kdfAlgo, macAlgo);
  23696. if (ret != 0)
  23697. goto done;
  23698. /* get salt to send to peer */
  23699. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  23700. if (tmpSalt == NULL) {
  23701. ret = -10417; goto done;
  23702. }
  23703. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  23704. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  23705. if (tmpSalt == NULL) {
  23706. ret = -10418; goto done;
  23707. }
  23708. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  23709. /* in actual use, we'd get the peer's salt over the transport */
  23710. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  23711. if (ret != 0)
  23712. goto done;
  23713. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  23714. if (ret != 0)
  23715. goto done;
  23716. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 12);
  23717. if (ret != 0)
  23718. goto done;
  23719. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 12);
  23720. if (ret != 0)
  23721. goto done;
  23722. /* get encrypted msg (request) to send to B - compressed public key */
  23723. outSz = sizeof(out);
  23724. ret = wc_ecc_encrypt_ex(userA, userB, msg, sizeof(msg), out, &outSz, cliCtx,
  23725. 1);
  23726. if (ret != 0)
  23727. goto done;
  23728. #ifndef WOLFSSL_ECIES_OLD
  23729. wc_ecc_free(tmpKey);
  23730. #endif
  23731. /* B decrypts msg (request) from A - out has a compressed public key */
  23732. plainSz = sizeof(plain);
  23733. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, srvCtx);
  23734. if (ret != 0)
  23735. goto done;
  23736. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  23737. ret = -10419; goto done;
  23738. }
  23739. #endif /* HAVE_COMP_KEY && (!FIPS || FIPS>=5.3) */
  23740. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  23741. (ECC_MIN_KEY_SZ <= 256) && defined(WOLFSSL_AES_128)
  23742. ret = ecc_encrypt_kat(rng);
  23743. #endif
  23744. done:
  23745. /* cleanup */
  23746. wc_ecc_ctx_free(srvCtx);
  23747. wc_ecc_ctx_free(cliCtx);
  23748. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23749. if (tmpKey != NULL) {
  23750. wc_ecc_free(tmpKey);
  23751. XFREE(tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23752. }
  23753. #else
  23754. wc_ecc_free(tmpKey);
  23755. #endif
  23756. return ret;
  23757. }
  23758. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  23759. WOLFSSL_TEST_SUBROUTINE int ecc_encrypt_test(void)
  23760. {
  23761. WC_RNG rng;
  23762. int ret;
  23763. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23764. ecc_key *userA;
  23765. ecc_key *userB;
  23766. #else
  23767. ecc_key userA[1];
  23768. ecc_key userB[1];
  23769. #endif
  23770. #ifndef HAVE_FIPS
  23771. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  23772. #else
  23773. ret = wc_InitRng(&rng);
  23774. #endif
  23775. if (ret != 0)
  23776. return -10400;
  23777. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23778. userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT,
  23779. DYNAMIC_TYPE_TMP_BUFFER);
  23780. userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT,
  23781. DYNAMIC_TYPE_TMP_BUFFER);
  23782. if ((userA == NULL) || (userB == NULL)) {
  23783. ERROR_OUT(MEMORY_E, done);
  23784. }
  23785. #endif
  23786. XMEMSET(userA, 0, sizeof *userA);
  23787. XMEMSET(userB, 0, sizeof *userB);
  23788. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  23789. if (ret != 0)
  23790. goto done;
  23791. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  23792. if (ret != 0)
  23793. goto done;
  23794. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userA);
  23795. #if defined(WOLFSSL_ASYNC_CRYPT)
  23796. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  23797. #endif
  23798. if (ret != 0){
  23799. ret = -10401; goto done;
  23800. }
  23801. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userB);
  23802. #if defined(WOLFSSL_ASYNC_CRYPT)
  23803. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  23804. #endif
  23805. if (ret != 0){
  23806. ret = -10402; goto done;
  23807. }
  23808. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  23809. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  23810. !defined(HAVE_SELFTEST)
  23811. ret = wc_ecc_set_rng(userA, &rng);
  23812. if (ret != 0) {
  23813. ret = -10403; goto done;
  23814. }
  23815. ret = wc_ecc_set_rng(userB, &rng);
  23816. if (ret != 0) {
  23817. ret = -10404; goto done;
  23818. }
  23819. #endif
  23820. #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))
  23821. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  23822. #ifdef WOLFSSL_AES_128
  23823. if (ret == 0) {
  23824. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CBC,
  23825. ecHKDF_SHA256, ecHMAC_SHA256);
  23826. if (ret != 0) {
  23827. printf("ECIES: AES_128_CBC, HKDF_SHA256, HMAC_SHA256\n");
  23828. }
  23829. }
  23830. #endif
  23831. #ifdef WOLFSSL_AES_256
  23832. if (ret == 0) {
  23833. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_256_CBC,
  23834. ecHKDF_SHA256, ecHMAC_SHA256);
  23835. if (ret != 0) {
  23836. printf("ECIES: AES_256_CBC, HKDF_SHA256, HMAC_SHA256\n");
  23837. }
  23838. }
  23839. #endif
  23840. #endif
  23841. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER)
  23842. #ifdef WOLFSSL_AES_128
  23843. if (ret == 0) {
  23844. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CTR,
  23845. ecHKDF_SHA256, ecHMAC_SHA256);
  23846. if (ret != 0) {
  23847. printf("ECIES: AES_128_CTR, HKDF_SHA256, HMAC_SHA256\n");
  23848. }
  23849. }
  23850. #endif
  23851. #ifdef WOLFSSL_AES_256
  23852. if (ret == 0) {
  23853. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_256_CTR,
  23854. ecHKDF_SHA256, ecHMAC_SHA256);
  23855. if (ret != 0) {
  23856. printf("ECIES: AES_256_CTR, HKDF_SHA256, HMAC_SHA256\n");
  23857. }
  23858. }
  23859. #endif
  23860. #endif
  23861. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  23862. done:
  23863. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23864. if (userA != NULL) {
  23865. wc_ecc_free(userA);
  23866. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23867. }
  23868. if (userB != NULL) {
  23869. wc_ecc_free(userB);
  23870. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23871. }
  23872. #else
  23873. wc_ecc_free(userB);
  23874. wc_ecc_free(userA);
  23875. #endif
  23876. wc_FreeRng(&rng);
  23877. return ret;
  23878. }
  23879. #endif /* HAVE_ECC_ENCRYPT && HAVE_AES_CBC && WOLFSSL_AES_128 */
  23880. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  23881. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  23882. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  23883. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23884. WOLFSSL_TEST_SUBROUTINE int ecc_test_buffers(void)
  23885. {
  23886. size_t bytes;
  23887. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23888. ecc_key *cliKey = (ecc_key *)XMALLOC(sizeof *cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23889. ecc_key *servKey = (ecc_key *)XMALLOC(sizeof *servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23890. ecc_key *tmpKey = (ecc_key *)XMALLOC(sizeof *tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23891. #else
  23892. ecc_key cliKey[1];
  23893. ecc_key servKey[1];
  23894. ecc_key tmpKey[1];
  23895. #endif
  23896. WC_RNG rng;
  23897. word32 idx = 0;
  23898. int ret;
  23899. /* pad our test message to 32 bytes so evenly divisible by AES_BLOCK_SZ */
  23900. byte in[] = "Everyone gets Friday off. ecc p";
  23901. word32 inLen = (word32)XSTRLEN((char*)in);
  23902. byte out[256];
  23903. byte plain[256];
  23904. int verify = 0;
  23905. word32 x;
  23906. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23907. if ((cliKey == NULL) || (servKey == NULL) || (tmpKey == NULL))
  23908. ERROR_OUT(MEMORY_E, done);
  23909. #endif
  23910. ret = wc_ecc_init_ex(cliKey, HEAP_HINT, devId);
  23911. if (ret != 0)
  23912. ERROR_OUT(-10420, done);
  23913. ret = wc_ecc_init_ex(servKey, HEAP_HINT, devId);
  23914. if (ret != 0)
  23915. ERROR_OUT(-10421, done);
  23916. ret = wc_ecc_init_ex(tmpKey, HEAP_HINT, devId);
  23917. if (ret != 0)
  23918. ERROR_OUT(-10421, done);
  23919. bytes = (size_t)sizeof_ecc_clikey_der_256;
  23920. /* place client key into ecc_key struct cliKey */
  23921. ret = wc_EccPrivateKeyDecode(ecc_clikey_der_256, &idx, cliKey,
  23922. (word32)bytes);
  23923. if (ret != 0)
  23924. ERROR_OUT(-10422, done);
  23925. idx = 0;
  23926. bytes = (size_t)sizeof_ecc_key_der_256;
  23927. /* place server key into ecc_key struct servKey */
  23928. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, servKey,
  23929. (word32)bytes);
  23930. if (ret != 0)
  23931. ERROR_OUT(-10423, done);
  23932. #ifndef WC_NO_RNG
  23933. #ifndef HAVE_FIPS
  23934. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  23935. #else
  23936. ret = wc_InitRng(&rng);
  23937. #endif
  23938. if (ret != 0)
  23939. ERROR_OUT(-10424, done);
  23940. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  23941. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  23942. !defined(HAVE_SELFTEST)
  23943. ret = wc_ecc_set_rng(cliKey, &rng);
  23944. if (ret != 0) {
  23945. ERROR_OUT(-10425, done);
  23946. }
  23947. ret = wc_ecc_set_rng(servKey, &rng);
  23948. if (ret != 0) {
  23949. ERROR_OUT(-10425, done);
  23950. }
  23951. #endif
  23952. #endif /* !WC_NO_RNG */
  23953. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_HKDF) && \
  23954. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  23955. {
  23956. word32 y;
  23957. /* test encrypt and decrypt if they're available */
  23958. x = sizeof(out);
  23959. ret = wc_ecc_encrypt(cliKey, servKey, in, sizeof(in), out, &x, NULL);
  23960. if (ret < 0)
  23961. ERROR_OUT(-10426, done);
  23962. #ifdef WOLFSSL_ECIES_OLD
  23963. tmpKey->dp = cliKey->dp;
  23964. ret = wc_ecc_copy_point(&cliKey->pubkey, &tmpKey->pubkey);
  23965. if (ret != 0) {
  23966. ret = -10414; goto done;
  23967. }
  23968. #endif
  23969. y = sizeof(plain);
  23970. ret = wc_ecc_decrypt(servKey, tmpKey, out, x, plain, &y, NULL);
  23971. if (ret < 0)
  23972. ERROR_OUT(-10427, done);
  23973. if (XMEMCMP(plain, in, inLen))
  23974. ERROR_OUT(-10428, done);
  23975. }
  23976. #endif
  23977. x = sizeof(out);
  23978. do {
  23979. #if defined(WOLFSSL_ASYNC_CRYPT)
  23980. ret = wc_AsyncWait(ret, &cliKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  23981. #endif
  23982. if (ret == 0)
  23983. ret = wc_ecc_sign_hash(in, inLen, out, &x, &rng, cliKey);
  23984. } while (ret == WC_PENDING_E);
  23985. if (ret < 0)
  23986. ERROR_OUT(-10429, done);
  23987. TEST_SLEEP();
  23988. XMEMSET(plain, 0, sizeof(plain));
  23989. do {
  23990. #if defined(WOLFSSL_ASYNC_CRYPT)
  23991. ret = wc_AsyncWait(ret, &cliKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  23992. #endif
  23993. if (ret == 0)
  23994. ret = wc_ecc_verify_hash(out, x, in, inLen, &verify,
  23995. cliKey);
  23996. } while (ret == WC_PENDING_E);
  23997. if (ret < 0)
  23998. ERROR_OUT(-10430, done);
  23999. if (verify != 1)
  24000. ERROR_OUT(-10431, done);
  24001. TEST_SLEEP();
  24002. #ifdef WOLFSSL_CERT_EXT
  24003. idx = 0;
  24004. bytes = sizeof_ecc_clikeypub_der_256;
  24005. ret = wc_EccPublicKeyDecode(ecc_clikeypub_der_256, &idx, cliKey,
  24006. (word32) bytes);
  24007. if (ret != 0)
  24008. ERROR_OUT(-10432, done);
  24009. #endif
  24010. ret = 0;
  24011. done:
  24012. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24013. if (cliKey != NULL) {
  24014. wc_ecc_free(cliKey);
  24015. XFREE(cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24016. }
  24017. if (servKey != NULL) {
  24018. wc_ecc_free(servKey);
  24019. XFREE(servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24020. }
  24021. if (tmpKey != NULL) {
  24022. wc_ecc_free(tmpKey);
  24023. XFREE(tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24024. }
  24025. #else
  24026. wc_ecc_free(cliKey);
  24027. wc_ecc_free(servKey);
  24028. wc_ecc_free(tmpKey);
  24029. #endif
  24030. wc_FreeRng(&rng);
  24031. return ret;
  24032. }
  24033. #endif /* USE_CERT_BUFFERS_256 && !WOLFSSL_ATECCX08A && !NO_ECC256 */
  24034. #endif /* HAVE_ECC */
  24035. #ifdef HAVE_CURVE25519
  24036. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  24037. defined(HAVE_CURVE25519_KEY_IMPORT)
  24038. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  24039. #define X25519_TEST_CNT 5
  24040. #else
  24041. #define X25519_TEST_CNT 1
  24042. #endif
  24043. static int curve25519_overflow_test(void)
  24044. {
  24045. /* secret key for party a */
  24046. byte sa[X25519_TEST_CNT][32] = {
  24047. {
  24048. 0x8d,0xaf,0x6e,0x7a,0xc1,0xeb,0x8d,0x30,
  24049. 0x99,0x86,0xd3,0x90,0x47,0x96,0x21,0x3c,
  24050. 0x3a,0x75,0xc0,0x7b,0x75,0x01,0x75,0xa3,
  24051. 0x81,0x4b,0xff,0x5a,0xbc,0x96,0x87,0x28
  24052. },
  24053. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  24054. {
  24055. 0x9d,0x63,0x5f,0xce,0xe2,0xe8,0xd7,0xfb,
  24056. 0x68,0x77,0x0e,0x44,0xd1,0xad,0x87,0x2b,
  24057. 0xf4,0x65,0x06,0xb7,0xbb,0xdb,0xbe,0x6e,
  24058. 0x02,0x43,0x24,0xc7,0x3d,0x7b,0x88,0x60
  24059. },
  24060. {
  24061. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  24062. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  24063. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  24064. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  24065. },
  24066. {
  24067. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  24068. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  24069. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  24070. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  24071. },
  24072. {
  24073. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  24074. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  24075. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  24076. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  24077. }
  24078. #endif
  24079. };
  24080. /* public key for party b */
  24081. byte pb[X25519_TEST_CNT][32] = {
  24082. {
  24083. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  24084. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  24085. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  24086. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  24087. },
  24088. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  24089. {
  24090. /* 0xff first byte in original - invalid! */
  24091. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  24092. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  24093. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  24094. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  24095. },
  24096. {
  24097. 0x36,0x1a,0x74,0x87,0x28,0x59,0xe0,0xb6,
  24098. 0xe4,0x2b,0x17,0x9b,0x16,0xb0,0x3b,0xf8,
  24099. 0xb8,0x9f,0x2a,0x8f,0xc5,0x33,0x68,0x4f,
  24100. 0xde,0x4d,0xd8,0x80,0x63,0xe7,0xb4,0x0a
  24101. },
  24102. {
  24103. 0x00,0x80,0x38,0x59,0x19,0x3a,0x66,0x12,
  24104. 0xfd,0xa1,0xec,0x1c,0x40,0x84,0x40,0xbd,
  24105. 0x64,0x10,0x8b,0x53,0x81,0x21,0x03,0x2d,
  24106. 0x7d,0x33,0xb4,0x01,0x57,0x0d,0xe1,0x89
  24107. },
  24108. {
  24109. 0x1d,0xf8,0xf8,0x33,0x89,0x6c,0xb7,0xba,
  24110. 0x94,0x73,0xfa,0xc2,0x36,0xac,0xbe,0x49,
  24111. 0xaf,0x85,0x3e,0x93,0x5f,0xae,0xb2,0xc0,
  24112. 0xc8,0x80,0x8f,0x4a,0xaa,0xd3,0x55,0x2b
  24113. }
  24114. #endif
  24115. };
  24116. /* expected shared key */
  24117. byte ss[X25519_TEST_CNT][32] = {
  24118. {
  24119. 0x5c,0x4c,0x85,0x5f,0xfb,0x20,0x38,0xcc,
  24120. 0x55,0x16,0x5b,0x8a,0xa7,0xed,0x57,0x6e,
  24121. 0x35,0xaa,0x71,0x67,0x85,0x1f,0xb6,0x28,
  24122. 0x17,0x07,0x7b,0xda,0x76,0xdd,0xe0,0xb4
  24123. },
  24124. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  24125. {
  24126. 0x33,0xf6,0xc1,0x34,0x62,0x92,0x06,0x02,
  24127. 0x95,0xdb,0x91,0x4c,0x5d,0x52,0x54,0xc7,
  24128. 0xd2,0x5b,0x24,0xb5,0x4f,0x33,0x59,0x79,
  24129. 0x9f,0x6d,0x7e,0x4a,0x4c,0x30,0xd6,0x38
  24130. },
  24131. {
  24132. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24133. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24134. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24135. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02
  24136. },
  24137. {
  24138. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24139. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24140. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24141. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09
  24142. },
  24143. {
  24144. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24145. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24146. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24147. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10
  24148. }
  24149. #endif
  24150. };
  24151. int ret = 0;
  24152. int i;
  24153. word32 y;
  24154. byte shared[32];
  24155. curve25519_key userA;
  24156. wc_curve25519_init_ex(&userA, HEAP_HINT, devId);
  24157. for (i = 0; i < X25519_TEST_CNT; i++) {
  24158. if (wc_curve25519_import_private_raw(sa[i], sizeof(sa[i]), pb[i],
  24159. sizeof(pb[i]), &userA) != 0) {
  24160. ret = -10500 - i; break;
  24161. }
  24162. /* test against known test vector */
  24163. XMEMSET(shared, 0, sizeof(shared));
  24164. y = sizeof(shared);
  24165. if (wc_curve25519_shared_secret(&userA, &userA, shared, &y) != 0) {
  24166. ret = -10510 - i; break;
  24167. }
  24168. if (XMEMCMP(ss[i], shared, y)) {
  24169. ret = -10520 - i; break;
  24170. }
  24171. }
  24172. wc_curve25519_free(&userA);
  24173. return ret;
  24174. }
  24175. /* Test the wc_curve25519_check_public API.
  24176. *
  24177. * returns 0 on success and -ve on failure.
  24178. */
  24179. static int curve25519_check_public_test(void)
  24180. {
  24181. /* Little-endian values that will fail */
  24182. byte fail_le[][CURVE25519_KEYSIZE] = {
  24183. {
  24184. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24185. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24186. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24187. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  24188. },
  24189. {
  24190. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24191. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24192. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24193. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  24194. },
  24195. {
  24196. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24197. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24198. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24199. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x81
  24200. },
  24201. };
  24202. /* Big-endian values that will fail */
  24203. byte fail_be[][CURVE25519_KEYSIZE] = {
  24204. {
  24205. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24206. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24207. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24208. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  24209. },
  24210. {
  24211. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24212. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24213. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24214. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  24215. },
  24216. {
  24217. 0x81,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24218. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24219. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24220. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  24221. },
  24222. };
  24223. /* Good or valid public value */
  24224. byte good[CURVE25519_KEYSIZE] = {
  24225. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24226. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24227. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24228. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  24229. };
  24230. int i;
  24231. /* Parameter checks */
  24232. /* NULL pointer */
  24233. if (wc_curve25519_check_public(NULL, 0, EC25519_LITTLE_ENDIAN) !=
  24234. BAD_FUNC_ARG) {
  24235. return -10600;
  24236. }
  24237. if (wc_curve25519_check_public(NULL, 0, EC25519_BIG_ENDIAN) !=
  24238. BAD_FUNC_ARG) {
  24239. return -10601;
  24240. }
  24241. /* Length of 0 treated differently to other invalid lengths for TLS */
  24242. if (wc_curve25519_check_public(good, 0, EC25519_LITTLE_ENDIAN) != BUFFER_E)
  24243. return -10602;
  24244. if (wc_curve25519_check_public(good, 0, EC25519_BIG_ENDIAN) != BUFFER_E)
  24245. return -10603;
  24246. /* Length not CURVE25519_KEYSIZE */
  24247. for (i = 1; i < CURVE25519_KEYSIZE + 2; i++) {
  24248. if (i == CURVE25519_KEYSIZE)
  24249. continue;
  24250. if (wc_curve25519_check_public(good, i, EC25519_LITTLE_ENDIAN) !=
  24251. ECC_BAD_ARG_E) {
  24252. return -10604 - i;
  24253. }
  24254. if (wc_curve25519_check_public(good, i, EC25519_BIG_ENDIAN) !=
  24255. ECC_BAD_ARG_E) {
  24256. return -10614 - i;
  24257. }
  24258. }
  24259. /* Little-endian fail cases */
  24260. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  24261. if (wc_curve25519_check_public(fail_le[i], CURVE25519_KEYSIZE,
  24262. EC25519_LITTLE_ENDIAN) == 0) {
  24263. return -10624 - i;
  24264. }
  24265. }
  24266. /* Big-endian fail cases */
  24267. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  24268. if (wc_curve25519_check_public(fail_be[i], CURVE25519_KEYSIZE,
  24269. EC25519_BIG_ENDIAN) == 0) {
  24270. return -10634 - i;
  24271. }
  24272. }
  24273. /* Check a valid public value works! */
  24274. if (wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  24275. EC25519_LITTLE_ENDIAN) != 0) {
  24276. return -10644;
  24277. }
  24278. if (wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  24279. EC25519_BIG_ENDIAN) != 0) {
  24280. return -10645;
  24281. }
  24282. return 0;
  24283. }
  24284. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  24285. #if !defined(NO_ASN) && defined(HAVE_CURVE25519_KEY_EXPORT) && \
  24286. defined(HAVE_CURVE25519_KEY_IMPORT)
  24287. static int curve255519_der_test(void)
  24288. {
  24289. int ret = 0;
  24290. /* certs/statickeys/x25519.der */
  24291. const byte kCurve25519PrivDer[] = {
  24292. 0x30, 0x2E, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x6E,
  24293. 0x04, 0x22, 0x04, 0x20, 0x78, 0x8E, 0x31, 0x5C, 0x33, 0xA9, 0x19, 0xC0,
  24294. 0x5E, 0x36, 0x70, 0x1B, 0xA4, 0xE8, 0xEF, 0xC1, 0x89, 0x8C, 0xB3, 0x15,
  24295. 0xC6, 0x79, 0xD3, 0xAC, 0x22, 0x00, 0xAE, 0xFA, 0xB3, 0xB7, 0x0F, 0x78
  24296. };
  24297. /* certs/statickeys/x25519-pub.der */
  24298. const byte kCurve25519PubDer[] = {
  24299. 0x30, 0x2A, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x6E, 0x03, 0x21, 0x00,
  24300. 0x09, 0xBC, 0x8C, 0xC7, 0x45, 0x0D, 0xC1, 0xC2, 0x02, 0x57, 0x9A, 0x68,
  24301. 0x3A, 0xFD, 0x7A, 0xA8, 0xA5, 0x2F, 0xF0, 0x99, 0x39, 0x98, 0xEA, 0x26,
  24302. 0xA2, 0x5B, 0x38, 0xFD, 0x96, 0xDB, 0x2A, 0x26
  24303. };
  24304. curve25519_key key;
  24305. byte output[128];
  24306. word32 outputSz = 128;
  24307. word32 idx;
  24308. if (wc_curve25519_init_ex(&key, HEAP_HINT, devId) != 0) {
  24309. return -10723;
  24310. }
  24311. /* Test decode / encode of Curve25519 private key only */
  24312. if (ret == 0) {
  24313. idx = 0;
  24314. ret = wc_Curve25519PrivateKeyDecode(kCurve25519PrivDer, &idx, &key,
  24315. (word32)sizeof(kCurve25519PrivDer));
  24316. }
  24317. if (ret == 0) {
  24318. outputSz = (word32)sizeof(output);
  24319. ret = wc_Curve25519PrivateKeyToDer(&key, output, outputSz);
  24320. if (ret >= 0) {
  24321. outputSz = ret;
  24322. ret = 0;
  24323. }
  24324. else {
  24325. ret = -10724;
  24326. }
  24327. }
  24328. if (ret == 0 && (outputSz != (word32)sizeof(kCurve25519PrivDer) ||
  24329. XMEMCMP(output, kCurve25519PrivDer, outputSz) != 0)) {
  24330. ret = -10725;
  24331. }
  24332. /* Test decode / encode of Curve25519 public key only */
  24333. if (ret == 0) {
  24334. idx = 0;
  24335. ret = wc_Curve25519PublicKeyDecode(kCurve25519PubDer, &idx, &key,
  24336. (word32)sizeof(kCurve25519PubDer));
  24337. }
  24338. if (ret == 0) {
  24339. outputSz = (word32)sizeof(output);
  24340. ret = wc_Curve25519PublicKeyToDer(&key, output, outputSz, 1);
  24341. if (ret >= 0) {
  24342. outputSz = ret;
  24343. ret = 0;
  24344. }
  24345. else {
  24346. ret = -10726;
  24347. }
  24348. }
  24349. if (ret == 0 && (outputSz != (word32)sizeof(kCurve25519PubDer) ||
  24350. XMEMCMP(output, kCurve25519PubDer, outputSz) != 0)) {
  24351. ret = -10727;
  24352. }
  24353. wc_curve25519_free(&key);
  24354. return ret;
  24355. }
  24356. #endif /* !NO_ASN && HAVE_CURVE25519_KEY_EXPORT && HAVE_CURVE25519_KEY_IMPORT */
  24357. WOLFSSL_TEST_SUBROUTINE int curve25519_test(void)
  24358. {
  24359. WC_RNG rng;
  24360. int ret;
  24361. #ifdef HAVE_CURVE25519_SHARED_SECRET
  24362. byte sharedA[32];
  24363. byte sharedB[32];
  24364. word32 y;
  24365. #endif
  24366. #ifdef HAVE_CURVE25519_KEY_EXPORT
  24367. byte exportBuf[32];
  24368. #endif
  24369. word32 x = 0;
  24370. curve25519_key userA, userB, pubKey;
  24371. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  24372. defined(HAVE_CURVE25519_KEY_IMPORT)
  24373. /* test vectors from
  24374. https://tools.ietf.org/html/draft-josefsson-tls-curve25519-03
  24375. */
  24376. /* secret key for party a */
  24377. byte sa[] = {
  24378. 0x5A,0xC9,0x9F,0x33,0x63,0x2E,0x5A,0x76,
  24379. 0x8D,0xE7,0xE8,0x1B,0xF8,0x54,0xC2,0x7C,
  24380. 0x46,0xE3,0xFB,0xF2,0xAB,0xBA,0xCD,0x29,
  24381. 0xEC,0x4A,0xFF,0x51,0x73,0x69,0xC6,0x60
  24382. };
  24383. /* public key for party a */
  24384. byte pa[] = {
  24385. 0x05,0x7E,0x23,0xEA,0x9F,0x1C,0xBE,0x8A,
  24386. 0x27,0x16,0x8F,0x6E,0x69,0x6A,0x79,0x1D,
  24387. 0xE6,0x1D,0xD3,0xAF,0x7A,0xCD,0x4E,0xEA,
  24388. 0xCC,0x6E,0x7B,0xA5,0x14,0xFD,0xA8,0x63
  24389. };
  24390. /* secret key for party b */
  24391. byte sb[] = {
  24392. 0x47,0xDC,0x3D,0x21,0x41,0x74,0x82,0x0E,
  24393. 0x11,0x54,0xB4,0x9B,0xC6,0xCD,0xB2,0xAB,
  24394. 0xD4,0x5E,0xE9,0x58,0x17,0x05,0x5D,0x25,
  24395. 0x5A,0xA3,0x58,0x31,0xB7,0x0D,0x32,0x60
  24396. };
  24397. /* public key for party b */
  24398. byte pb[] = {
  24399. 0x6E,0xB8,0x9D,0xA9,0x19,0x89,0xAE,0x37,
  24400. 0xC7,0xEA,0xC7,0x61,0x8D,0x9E,0x5C,0x49,
  24401. 0x51,0xDB,0xA1,0xD7,0x3C,0x28,0x5A,0xE1,
  24402. 0xCD,0x26,0xA8,0x55,0x02,0x0E,0xEF,0x04
  24403. };
  24404. /* expected shared key */
  24405. byte ss[] = {
  24406. 0x61,0x45,0x0C,0xD9,0x8E,0x36,0x01,0x6B,
  24407. 0x58,0x77,0x6A,0x89,0x7A,0x9F,0x0A,0xEF,
  24408. 0x73,0x8B,0x99,0xF0,0x94,0x68,0xB8,0xD6,
  24409. 0xB8,0x51,0x11,0x84,0xD5,0x34,0x94,0xAB
  24410. };
  24411. #endif /* HAVE_CURVE25519_SHARED_SECRET */
  24412. (void)x;
  24413. #ifndef HAVE_FIPS
  24414. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  24415. #else
  24416. ret = wc_InitRng(&rng);
  24417. #endif
  24418. if (ret != 0)
  24419. return -10700;
  24420. wc_curve25519_init_ex(&userA, HEAP_HINT, devId);
  24421. wc_curve25519_init_ex(&userB, HEAP_HINT, devId);
  24422. wc_curve25519_init_ex(&pubKey, HEAP_HINT, devId);
  24423. /* make curve25519 keys */
  24424. if (wc_curve25519_make_key(&rng, 32, &userA) != 0)
  24425. return -10701;
  24426. if (wc_curve25519_make_key(&rng, 32, &userB) != 0)
  24427. return -10702;
  24428. #ifdef HAVE_CURVE25519_SHARED_SECRET
  24429. /* find shared secret key */
  24430. x = sizeof(sharedA);
  24431. if ((ret = wc_curve25519_shared_secret(&userA, &userB, sharedA, &x)) != 0) {
  24432. printf("wc_curve25519_shared_secret 1 %d\n", ret);
  24433. return -10703;
  24434. }
  24435. y = sizeof(sharedB);
  24436. if ((ret = wc_curve25519_shared_secret(&userB, &userA, sharedB, &y)) != 0) {
  24437. printf("wc_curve25519_shared_secret 2 %d\n", ret);
  24438. return -10704;
  24439. }
  24440. /* compare shared secret keys to test they are the same */
  24441. if (y != x)
  24442. return -10705;
  24443. if (XMEMCMP(sharedA, sharedB, x))
  24444. return -10706;
  24445. #endif
  24446. #ifdef HAVE_CURVE25519_KEY_EXPORT
  24447. /* export a public key and import it for another user */
  24448. x = sizeof(exportBuf);
  24449. if (wc_curve25519_export_public(&userA, exportBuf, &x) != 0)
  24450. return -10707;
  24451. #ifdef HAVE_CURVE25519_KEY_IMPORT
  24452. if (wc_curve25519_import_public(exportBuf, x, &pubKey) != 0)
  24453. return -10708;
  24454. #endif
  24455. #endif
  24456. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  24457. defined(HAVE_CURVE25519_KEY_IMPORT)
  24458. /* test shared key after importing a public key */
  24459. XMEMSET(sharedB, 0, sizeof(sharedB));
  24460. y = sizeof(sharedB);
  24461. if (wc_curve25519_shared_secret(&userB, &pubKey, sharedB, &y) != 0)
  24462. return -10709;
  24463. if (XMEMCMP(sharedA, sharedB, y))
  24464. return -10710;
  24465. /* import RFC test vectors and compare shared key */
  24466. if (wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  24467. != 0)
  24468. return -10711;
  24469. if (wc_curve25519_import_private_raw(sb, sizeof(sb), pb, sizeof(pb), &userB)
  24470. != 0)
  24471. return -10712;
  24472. /* test against known test vector */
  24473. XMEMSET(sharedB, 0, sizeof(sharedB));
  24474. y = sizeof(sharedB);
  24475. if (wc_curve25519_shared_secret(&userA, &userB, sharedB, &y) != 0)
  24476. return -10713;
  24477. if (XMEMCMP(ss, sharedB, y))
  24478. return -10714;
  24479. /* test swapping roles of keys and generating same shared key */
  24480. XMEMSET(sharedB, 0, sizeof(sharedB));
  24481. y = sizeof(sharedB);
  24482. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  24483. return -10715;
  24484. if (XMEMCMP(ss, sharedB, y))
  24485. return -10716;
  24486. /* test with 1 generated key and 1 from known test vector */
  24487. if (wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  24488. != 0)
  24489. return -10717;
  24490. wc_curve25519_free(&userB);
  24491. wc_curve25519_init_ex(&userB, HEAP_HINT, devId);
  24492. if (wc_curve25519_make_key(&rng, 32, &userB) != 0)
  24493. return -10718;
  24494. x = sizeof(sharedA);
  24495. if (wc_curve25519_shared_secret(&userA, &userB, sharedA, &x) != 0)
  24496. return -10719;
  24497. y = sizeof(sharedB);
  24498. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  24499. return -10720;
  24500. /* compare shared secret keys to test they are the same */
  24501. if (y != x)
  24502. return -10721;
  24503. if (XMEMCMP(sharedA, sharedB, x))
  24504. return -10722;
  24505. ret = curve25519_overflow_test();
  24506. if (ret != 0)
  24507. return ret;
  24508. ret = curve25519_check_public_test();
  24509. if (ret != 0)
  24510. return ret;
  24511. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  24512. #if !defined(NO_ASN) && defined(HAVE_CURVE25519_KEY_EXPORT) && \
  24513. defined(HAVE_CURVE25519_KEY_IMPORT)
  24514. ret = curve255519_der_test();
  24515. if (ret != 0)
  24516. return ret;
  24517. #endif
  24518. /* clean up keys when done */
  24519. wc_curve25519_free(&pubKey);
  24520. wc_curve25519_free(&userB);
  24521. wc_curve25519_free(&userA);
  24522. wc_FreeRng(&rng);
  24523. return 0;
  24524. }
  24525. #endif /* HAVE_CURVE25519 */
  24526. #ifdef HAVE_ED25519
  24527. #ifdef WOLFSSL_TEST_CERT
  24528. static int ed25519_test_cert(void)
  24529. {
  24530. DecodedCert cert[2];
  24531. DecodedCert* serverCert = NULL;
  24532. DecodedCert* caCert = NULL;
  24533. #ifdef HAVE_ED25519_VERIFY
  24534. ed25519_key key;
  24535. ed25519_key* pubKey = NULL;
  24536. int verify;
  24537. #endif /* HAVE_ED25519_VERIFY */
  24538. int ret;
  24539. byte* tmp;
  24540. size_t bytes;
  24541. XFILE file;
  24542. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24543. if (tmp == NULL) {
  24544. ERROR_OUT(-10730, done);
  24545. }
  24546. #ifdef USE_CERT_BUFFERS_256
  24547. XMEMCPY(tmp, ca_ed25519_cert, sizeof_ca_ed25519_cert);
  24548. bytes = sizeof_ca_ed25519_cert;
  24549. #elif !defined(NO_FILESYSTEM)
  24550. file = XFOPEN(caEd25519Cert, "rb");
  24551. if (file == NULL) {
  24552. ERROR_OUT(-10731, done);
  24553. }
  24554. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  24555. XFCLOSE(file);
  24556. #else
  24557. /* No certificate to use. */
  24558. ERROR_OUT(-10732, done);
  24559. #endif
  24560. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  24561. caCert = &cert[0];
  24562. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  24563. if (ret != 0) {
  24564. ERROR_OUT(-10733, done);
  24565. }
  24566. #ifdef USE_CERT_BUFFERS_256
  24567. XMEMCPY(tmp, server_ed25519_cert, sizeof_server_ed25519_cert);
  24568. bytes = sizeof_server_ed25519_cert;
  24569. #elif !defined(NO_FILESYSTEM)
  24570. file = XFOPEN(serverEd25519Cert, "rb");
  24571. if (file == NULL) {
  24572. ERROR_OUT(-10734, done);
  24573. }
  24574. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  24575. XFCLOSE(file);
  24576. #else
  24577. /* No certificate to use. */
  24578. ERROR_OUT(-10735, done);
  24579. #endif
  24580. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  24581. serverCert = &cert[1];
  24582. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  24583. if (ret != 0) {
  24584. ERROR_OUT(-10736, done);
  24585. }
  24586. #ifdef HAVE_ED25519_VERIFY
  24587. ret = wc_ed25519_init(&key);
  24588. if (ret < 0) {
  24589. ERROR_OUT(-10737, done);
  24590. }
  24591. pubKey = &key;
  24592. ret = wc_ed25519_import_public(caCert->publicKey, caCert->pubKeySize,
  24593. pubKey);
  24594. if (ret < 0) {
  24595. ERROR_OUT(-10738, done);
  24596. }
  24597. if (wc_ed25519_verify_msg(serverCert->signature, serverCert->sigLength,
  24598. serverCert->source + serverCert->certBegin,
  24599. serverCert->sigIndex - serverCert->certBegin,
  24600. &verify, pubKey) < 0 || verify != 1) {
  24601. ERROR_OUT(-10739, done);
  24602. }
  24603. #endif /* HAVE_ED25519_VERIFY */
  24604. done:
  24605. if (tmp != NULL)
  24606. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24607. #ifdef HAVE_ED25519_VERIFY
  24608. wc_ed25519_free(pubKey);
  24609. #endif /* HAVE_ED25519_VERIFY */
  24610. if (caCert != NULL)
  24611. FreeDecodedCert(caCert);
  24612. if (serverCert != NULL)
  24613. FreeDecodedCert(serverCert);
  24614. return ret;
  24615. }
  24616. static int ed25519_test_make_cert(void)
  24617. {
  24618. WC_RNG rng;
  24619. Cert cert;
  24620. DecodedCert decode;
  24621. ed25519_key key;
  24622. ed25519_key* privKey = NULL;
  24623. int ret = 0;
  24624. byte* tmp = NULL;
  24625. wc_InitCert_ex(&cert, HEAP_HINT, devId);
  24626. #ifndef HAVE_FIPS
  24627. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  24628. #else
  24629. ret = wc_InitRng(&rng);
  24630. #endif
  24631. if (ret != 0)
  24632. return -10750;
  24633. wc_ed25519_init(&key);
  24634. privKey = &key;
  24635. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, privKey);
  24636. cert.daysValid = 365 * 2;
  24637. cert.selfSigned = 1;
  24638. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  24639. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  24640. cert.isCA = 0;
  24641. #ifdef WOLFSSL_CERT_EXT
  24642. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  24643. if (ret < 0) {
  24644. ERROR_OUT(-10751, done);
  24645. }
  24646. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  24647. if (ret < 0) {
  24648. ERROR_OUT(-10752, done);
  24649. }
  24650. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  24651. if (ret < 0) {
  24652. ERROR_OUT(-10753, done);
  24653. }
  24654. #endif
  24655. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24656. if (tmp == NULL) {
  24657. ERROR_OUT(-10754, done);
  24658. }
  24659. cert.sigType = CTC_ED25519;
  24660. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED25519_TYPE, privKey, &rng);
  24661. if (ret < 0) {
  24662. ERROR_OUT(-10755, done);
  24663. }
  24664. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF,
  24665. ED25519_TYPE, privKey, &rng);
  24666. if (ret < 0) {
  24667. ERROR_OUT(-10756, done);
  24668. }
  24669. InitDecodedCert(&decode, tmp, ret, HEAP_HINT);
  24670. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  24671. FreeDecodedCert(&decode);
  24672. if (ret != 0) {
  24673. ERROR_OUT(-10757, done);
  24674. }
  24675. done:
  24676. if (tmp != NULL)
  24677. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24678. wc_ed25519_free(privKey);
  24679. wc_FreeRng(&rng);
  24680. return ret;
  24681. }
  24682. #endif /* WOLFSSL_TEST_CERT */
  24683. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  24684. defined(HAVE_ED25519_KEY_IMPORT)
  24685. static int ed25519ctx_test(void)
  24686. {
  24687. int ret;
  24688. byte out[ED25519_SIG_SIZE];
  24689. word32 outlen;
  24690. #ifdef HAVE_ED25519_VERIFY
  24691. int verify = 0;
  24692. #endif /* HAVE_ED25519_VERIFY */
  24693. ed25519_key key;
  24694. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  24695. 0x03,0x05,0x33,0x4e,0x38,0x1a,0xf7,0x8f,
  24696. 0x14,0x1c,0xb6,0x66,0xf6,0x19,0x9f,0x57,
  24697. 0xbc,0x34,0x95,0x33,0x5a,0x25,0x6a,0x95,
  24698. 0xbd,0x2a,0x55,0xbf,0x54,0x66,0x63,0xf6
  24699. };
  24700. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  24701. 0xdf,0xc9,0x42,0x5e,0x4f,0x96,0x8f,0x7f,
  24702. 0x0c,0x29,0xf0,0x25,0x9c,0xf5,0xf9,0xae,
  24703. 0xd6,0x85,0x1c,0x2b,0xb4,0xad,0x8b,0xfb,
  24704. 0x86,0x0c,0xfe,0xe0,0xab,0x24,0x82,0x92
  24705. };
  24706. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx1[] = {
  24707. 0x55,0xa4,0xcc,0x2f,0x70,0xa5,0x4e,0x04,
  24708. 0x28,0x8c,0x5f,0x4c,0xd1,0xe4,0x5a,0x7b,
  24709. 0xb5,0x20,0xb3,0x62,0x92,0x91,0x18,0x76,
  24710. 0xca,0xda,0x73,0x23,0x19,0x8d,0xd8,0x7a,
  24711. 0x8b,0x36,0x95,0x0b,0x95,0x13,0x00,0x22,
  24712. 0x90,0x7a,0x7f,0xb7,0xc4,0xe9,0xb2,0xd5,
  24713. 0xf6,0xcc,0xa6,0x85,0xa5,0x87,0xb4,0xb2,
  24714. 0x1f,0x4b,0x88,0x8e,0x4e,0x7e,0xdb,0x0d
  24715. };
  24716. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx2[] = {
  24717. 0xcc,0x5e,0x63,0xa2,0x7e,0x94,0xaf,0xd3,
  24718. 0x41,0x83,0x38,0xd2,0x48,0x6f,0xa9,0x2a,
  24719. 0xf9,0x91,0x7c,0x2d,0x98,0x9e,0x06,0xe5,
  24720. 0x02,0x77,0x72,0x1c,0x34,0x38,0x18,0xb4,
  24721. 0x21,0x96,0xbc,0x29,0x2e,0x68,0xf3,0x4d,
  24722. 0x85,0x9b,0xbe,0xad,0x17,0x9f,0x54,0x54,
  24723. 0x2d,0x4b,0x04,0xdc,0xfb,0xfa,0x4a,0x68,
  24724. 0x4e,0x39,0x50,0xfb,0x1c,0xcd,0x8d,0x0d
  24725. };
  24726. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  24727. 0xf7,0x26,0x93,0x6d,0x19,0xc8,0x00,0x49,
  24728. 0x4e,0x3f,0xda,0xff,0x20,0xb2,0x76,0xa8
  24729. };
  24730. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  24731. 0x66,0x6f,0x6f
  24732. };
  24733. outlen = sizeof(out);
  24734. XMEMSET(out, 0, sizeof(out));
  24735. ret = wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  24736. if (ret != 0)
  24737. return 10800;
  24738. ret = wc_ed25519_import_private_key(sKeyCtx, ED25519_KEY_SIZE, pKeyCtx,
  24739. sizeof(pKeyCtx), &key);
  24740. if (ret == 0)
  24741. ret = wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  24742. contextCtx, sizeof(contextCtx));
  24743. if (ret == 0 && XMEMCMP(out, sigCtx1, 64) != 0)
  24744. ret = -10801;
  24745. #if defined(HAVE_ED25519_VERIFY)
  24746. /* test verify on good msg */
  24747. if (ret == 0)
  24748. ret = wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx),
  24749. &verify, &key, contextCtx, sizeof(contextCtx));
  24750. if (ret == 0 && verify != 1)
  24751. ret = -10802;
  24752. #endif
  24753. if (ret == 0)
  24754. ret = wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  24755. NULL, 0);
  24756. if (ret == 0 && XMEMCMP(out, sigCtx2, 64) != 0)
  24757. ret = -10803;
  24758. #if defined(HAVE_ED25519_VERIFY)
  24759. /* test verify on good msg */
  24760. if (ret == 0)
  24761. ret = wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx),
  24762. &verify, &key, NULL, 0);
  24763. if (ret == 0 && verify != 1)
  24764. ret = -10804;
  24765. #endif
  24766. wc_ed25519_free(&key);
  24767. return ret;
  24768. }
  24769. static int ed25519ph_test(void)
  24770. {
  24771. int ret = 0;
  24772. byte out[ED25519_SIG_SIZE];
  24773. word32 outlen;
  24774. #ifdef HAVE_ED25519_VERIFY
  24775. int verify = 0;
  24776. #endif /* HAVE_ED25519_VERIFY */
  24777. ed25519_key key;
  24778. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  24779. 0x83,0x3f,0xe6,0x24,0x09,0x23,0x7b,0x9d,
  24780. 0x62,0xec,0x77,0x58,0x75,0x20,0x91,0x1e,
  24781. 0x9a,0x75,0x9c,0xec,0x1d,0x19,0x75,0x5b,
  24782. 0x7d,0xa9,0x01,0xb9,0x6d,0xca,0x3d,0x42
  24783. };
  24784. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  24785. 0xec,0x17,0x2b,0x93,0xad,0x5e,0x56,0x3b,
  24786. 0xf4,0x93,0x2c,0x70,0xe1,0x24,0x50,0x34,
  24787. 0xc3,0x54,0x67,0xef,0x2e,0xfd,0x4d,0x64,
  24788. 0xeb,0xf8,0x19,0x68,0x34,0x67,0xe2,0xbf
  24789. };
  24790. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  24791. 0x98,0xa7,0x02,0x22,0xf0,0xb8,0x12,0x1a,
  24792. 0xa9,0xd3,0x0f,0x81,0x3d,0x68,0x3f,0x80,
  24793. 0x9e,0x46,0x2b,0x46,0x9c,0x7f,0xf8,0x76,
  24794. 0x39,0x49,0x9b,0xb9,0x4e,0x6d,0xae,0x41,
  24795. 0x31,0xf8,0x50,0x42,0x46,0x3c,0x2a,0x35,
  24796. 0x5a,0x20,0x03,0xd0,0x62,0xad,0xf5,0xaa,
  24797. 0xa1,0x0b,0x8c,0x61,0xe6,0x36,0x06,0x2a,
  24798. 0xaa,0xd1,0x1c,0x2a,0x26,0x08,0x34,0x06
  24799. };
  24800. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  24801. 0xe0,0x39,0x70,0x2b,0x4c,0x25,0x95,0xa6,
  24802. 0xa5,0x41,0xac,0x85,0x09,0x23,0x6e,0x29,
  24803. 0x90,0x47,0x47,0x95,0x33,0x0c,0x9b,0x34,
  24804. 0xa7,0x5f,0x58,0xa6,0x60,0x12,0x9e,0x08,
  24805. 0xfd,0x73,0x69,0x43,0xfb,0x19,0x43,0xa5,
  24806. 0x57,0x20,0xb9,0xe0,0x95,0x7b,0x1e,0xd6,
  24807. 0x73,0x48,0x16,0x61,0x9f,0x13,0x88,0xf4,
  24808. 0x3f,0x73,0xe6,0xe3,0xba,0xa8,0x1c,0x0e
  24809. };
  24810. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  24811. 0x61,0x62,0x63
  24812. };
  24813. /* SHA-512 hash of msgPh */
  24814. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  24815. 0xdd,0xaf,0x35,0xa1,0x93,0x61,0x7a,0xba,
  24816. 0xcc,0x41,0x73,0x49,0xae,0x20,0x41,0x31,
  24817. 0x12,0xe6,0xfa,0x4e,0x89,0xa9,0x7e,0xa2,
  24818. 0x0a,0x9e,0xee,0xe6,0x4b,0x55,0xd3,0x9a,
  24819. 0x21,0x92,0x99,0x2a,0x27,0x4f,0xc1,0xa8,
  24820. 0x36,0xba,0x3c,0x23,0xa3,0xfe,0xeb,0xbd,
  24821. 0x45,0x4d,0x44,0x23,0x64,0x3c,0xe8,0x0e,
  24822. 0x2a,0x9a,0xc9,0x4f,0xa5,0x4c,0xa4,0x9f
  24823. };
  24824. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  24825. 0x66,0x6f,0x6f
  24826. };
  24827. outlen = sizeof(out);
  24828. XMEMSET(out, 0, sizeof(out));
  24829. ret = wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  24830. if (ret != 0)
  24831. return -10900;
  24832. ret = wc_ed25519_import_private_key(sKeyPh, ED25519_KEY_SIZE, pKeyPh,
  24833. sizeof(pKeyPh), &key);
  24834. if (ret == 0)
  24835. ret = wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  24836. NULL, 0);
  24837. if (ret == 0 && XMEMCMP(out, sigPh1, 64) != 0)
  24838. ret = -10901;
  24839. #if defined(HAVE_ED25519_VERIFY)
  24840. /* test verify on good msg */
  24841. if (ret == 0)
  24842. ret = wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh),
  24843. &verify, &key, NULL, 0);
  24844. if (ret == 0 && verify != 1)
  24845. ret = -10902;
  24846. #endif
  24847. if (ret == 0)
  24848. ret = wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  24849. contextPh2, sizeof(contextPh2));
  24850. if (ret == 0 && XMEMCMP(out, sigPh2, 64) != 0)
  24851. ret = -10903;
  24852. #if defined(HAVE_ED25519_VERIFY)
  24853. /* test verify on good msg */
  24854. if (ret == 0)
  24855. ret = wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify,
  24856. &key, contextPh2, sizeof(contextPh2));
  24857. if (ret == 0 && verify != 1)
  24858. ret = -10904;
  24859. #endif
  24860. if (ret == 0)
  24861. ret = wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  24862. NULL, 0);
  24863. if (ret == 0 && XMEMCMP(out, sigPh1, 64) != 0)
  24864. ret = -10905;
  24865. #if defined(HAVE_ED25519_VERIFY)
  24866. if (ret == 0)
  24867. ret = wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh),
  24868. &verify, &key, NULL, 0);
  24869. if (ret == 0 && verify != 1)
  24870. ret = -10906;
  24871. #endif
  24872. if (ret == 0)
  24873. ret = wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  24874. contextPh2, sizeof(contextPh2));
  24875. if (ret == 0 && XMEMCMP(out, sigPh2, 64) != 0)
  24876. ret = -10907;
  24877. #if defined(HAVE_ED25519_VERIFY)
  24878. if (ret == 0)
  24879. ret = wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  24880. &key, contextPh2, sizeof(contextPh2));
  24881. if (ret == 0 && verify != 1)
  24882. ret = -10908;
  24883. #endif
  24884. wc_ed25519_free(&key);
  24885. return ret;
  24886. }
  24887. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  24888. WOLFSSL_TEST_SUBROUTINE int ed25519_test(void)
  24889. {
  24890. int ret;
  24891. WC_RNG rng;
  24892. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  24893. defined(HAVE_ED25519_KEY_IMPORT)
  24894. byte out[ED25519_SIG_SIZE];
  24895. byte exportPKey[ED25519_KEY_SIZE];
  24896. byte exportSKey[ED25519_KEY_SIZE];
  24897. word32 exportPSz;
  24898. word32 exportSSz;
  24899. int i;
  24900. word32 outlen;
  24901. #ifdef HAVE_ED25519_VERIFY
  24902. #ifdef WOLFSSL_ED25519_STREAMING_VERIFY
  24903. int j;
  24904. #endif
  24905. int verify;
  24906. #endif /* HAVE_ED25519_VERIFY */
  24907. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  24908. word32 keySz, sigSz;
  24909. ed25519_key key;
  24910. ed25519_key key2;
  24911. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  24912. defined(HAVE_ED25519_KEY_IMPORT)
  24913. /* test vectors from
  24914. https://tools.ietf.org/html/draft-josefsson-eddsa-ed25519-02
  24915. */
  24916. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  24917. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  24918. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  24919. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  24920. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  24921. };
  24922. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  24923. 0x4c,0xcd,0x08,0x9b,0x28,0xff,0x96,0xda,
  24924. 0x9d,0xb6,0xc3,0x46,0xec,0x11,0x4e,0x0f,
  24925. 0x5b,0x8a,0x31,0x9f,0x35,0xab,0xa6,0x24,
  24926. 0xda,0x8c,0xf6,0xed,0x4f,0xb8,0xa6,0xfb
  24927. };
  24928. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  24929. 0xc5,0xaa,0x8d,0xf4,0x3f,0x9f,0x83,0x7b,
  24930. 0xed,0xb7,0x44,0x2f,0x31,0xdc,0xb7,0xb1,
  24931. 0x66,0xd3,0x85,0x35,0x07,0x6f,0x09,0x4b,
  24932. 0x85,0xce,0x3a,0x2e,0x0b,0x44,0x58,0xf7
  24933. };
  24934. /* uncompressed test */
  24935. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  24936. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  24937. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  24938. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  24939. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  24940. };
  24941. /* compressed prefix test */
  24942. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  24943. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  24944. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  24945. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  24946. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  24947. };
  24948. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  24949. 0xf5,0xe5,0x76,0x7c,0xf1,0x53,0x31,0x95,
  24950. 0x17,0x63,0x0f,0x22,0x68,0x76,0xb8,0x6c,
  24951. 0x81,0x60,0xcc,0x58,0x3b,0xc0,0x13,0x74,
  24952. 0x4c,0x6b,0xf2,0x55,0xf5,0xcc,0x0e,0xe5
  24953. };
  24954. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  24955. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  24956. 0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  24957. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  24958. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  24959. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  24960. };
  24961. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  24962. 0x3d,0x40,0x17,0xc3,0xe8,0x43,0x89,0x5a,
  24963. 0x92,0xb7,0x0a,0xa7,0x4d,0x1b,0x7e,0xbc,
  24964. 0x9c,0x98,0x2c,0xcf,0x2e,0xc4,0x96,0x8c,
  24965. 0xc0,0xcd,0x55,0xf1,0x2a,0xf4,0x66,0x0c
  24966. };
  24967. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  24968. 0xfc,0x51,0xcd,0x8e,0x62,0x18,0xa1,0xa3,
  24969. 0x8d,0xa4,0x7e,0xd0,0x02,0x30,0xf0,0x58,
  24970. 0x08,0x16,0xed,0x13,0xba,0x33,0x03,0xac,
  24971. 0x5d,0xeb,0x91,0x15,0x48,0x90,0x80,0x25
  24972. };
  24973. /* uncompressed test */
  24974. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  24975. 0x04,0x55,0xd0,0xe0,0x9a,0x2b,0x9d,0x34,
  24976. 0x29,0x22,0x97,0xe0,0x8d,0x60,0xd0,0xf6,
  24977. 0x20,0xc5,0x13,0xd4,0x72,0x53,0x18,0x7c,
  24978. 0x24,0xb1,0x27,0x86,0xbd,0x77,0x76,0x45,
  24979. 0xce,0x1a,0x51,0x07,0xf7,0x68,0x1a,0x02,
  24980. 0xaf,0x25,0x23,0xa6,0xda,0xf3,0x72,0xe1,
  24981. 0x0e,0x3a,0x07,0x64,0xc9,0xd3,0xfe,0x4b,
  24982. 0xd5,0xb7,0x0a,0xb1,0x82,0x01,0x98,0x5a,
  24983. 0xd7
  24984. };
  24985. /* compressed prefix */
  24986. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  24987. 0x40,0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  24988. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  24989. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  24990. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  24991. };
  24992. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  24993. 0x27,0x81,0x17,0xfc,0x14,0x4c,0x72,0x34,
  24994. 0x0f,0x67,0xd0,0xf2,0x31,0x6e,0x83,0x86,
  24995. 0xce,0xff,0xbf,0x2b,0x24,0x28,0xc9,0xc5,
  24996. 0x1f,0xef,0x7c,0x59,0x7f,0x1d,0x42,0x6e
  24997. };
  24998. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  24999. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  25000. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  25001. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  25002. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  25003. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  25004. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  25005. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  25006. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  25007. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  25008. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  25009. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  25010. };
  25011. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  25012. 0x92,0xa0,0x09,0xa9,0xf0,0xd4,0xca,0xb8,
  25013. 0x72,0x0e,0x82,0x0b,0x5f,0x64,0x25,0x40,
  25014. 0xa2,0xb2,0x7b,0x54,0x16,0x50,0x3f,0x8f,
  25015. 0xb3,0x76,0x22,0x23,0xeb,0xdb,0x69,0xda,
  25016. 0x08,0x5a,0xc1,0xe4,0x3e,0x15,0x99,0x6e,
  25017. 0x45,0x8f,0x36,0x13,0xd0,0xf1,0x1d,0x8c,
  25018. 0x38,0x7b,0x2e,0xae,0xb4,0x30,0x2a,0xee,
  25019. 0xb0,0x0d,0x29,0x16,0x12,0xbb,0x0c,0x00
  25020. };
  25021. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  25022. 0x62,0x91,0xd6,0x57,0xde,0xec,0x24,0x02,
  25023. 0x48,0x27,0xe6,0x9c,0x3a,0xbe,0x01,0xa3,
  25024. 0x0c,0xe5,0x48,0xa2,0x84,0x74,0x3a,0x44,
  25025. 0x5e,0x36,0x80,0xd7,0xdb,0x5a,0xc3,0xac,
  25026. 0x18,0xff,0x9b,0x53,0x8d,0x16,0xf2,0x90,
  25027. 0xae,0x67,0xf7,0x60,0x98,0x4d,0xc6,0x59,
  25028. 0x4a,0x7c,0x15,0xe9,0x71,0x6e,0xd2,0x8d,
  25029. 0xc0,0x27,0xbe,0xce,0xea,0x1e,0xc4,0x0a
  25030. };
  25031. /* uncompressed test */
  25032. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  25033. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  25034. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  25035. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  25036. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  25037. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  25038. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  25039. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  25040. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  25041. };
  25042. /* compressed prefix */
  25043. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  25044. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  25045. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  25046. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  25047. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  25048. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  25049. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  25050. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  25051. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  25052. };
  25053. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  25054. 0x0a,0xab,0x4c,0x90,0x05,0x01,0xb3,0xe2,
  25055. 0x4d,0x7c,0xdf,0x46,0x63,0x32,0x6a,0x3a,
  25056. 0x87,0xdf,0x5e,0x48,0x43,0xb2,0xcb,0xdb,
  25057. 0x67,0xcb,0xf6,0xe4,0x60,0xfe,0xc3,0x50,
  25058. 0xaa,0x53,0x71,0xb1,0x50,0x8f,0x9f,0x45,
  25059. 0x28,0xec,0xea,0x23,0xc4,0x36,0xd9,0x4b,
  25060. 0x5e,0x8f,0xcd,0x4f,0x68,0x1e,0x30,0xa6,
  25061. 0xac,0x00,0xa9,0x70,0x4a,0x18,0x8a,0x03
  25062. };
  25063. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  25064. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = {0x0 };
  25065. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = {0x72};
  25066. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = {0xAF,0x82};
  25067. /* test of a 1024 byte long message */
  25068. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  25069. 0x08,0xb8,0xb2,0xb7,0x33,0x42,0x42,0x43,
  25070. 0x76,0x0f,0xe4,0x26,0xa4,0xb5,0x49,0x08,
  25071. 0x63,0x21,0x10,0xa6,0x6c,0x2f,0x65,0x91,
  25072. 0xea,0xbd,0x33,0x45,0xe3,0xe4,0xeb,0x98,
  25073. 0xfa,0x6e,0x26,0x4b,0xf0,0x9e,0xfe,0x12,
  25074. 0xee,0x50,0xf8,0xf5,0x4e,0x9f,0x77,0xb1,
  25075. 0xe3,0x55,0xf6,0xc5,0x05,0x44,0xe2,0x3f,
  25076. 0xb1,0x43,0x3d,0xdf,0x73,0xbe,0x84,0xd8,
  25077. 0x79,0xde,0x7c,0x00,0x46,0xdc,0x49,0x96,
  25078. 0xd9,0xe7,0x73,0xf4,0xbc,0x9e,0xfe,0x57,
  25079. 0x38,0x82,0x9a,0xdb,0x26,0xc8,0x1b,0x37,
  25080. 0xc9,0x3a,0x1b,0x27,0x0b,0x20,0x32,0x9d,
  25081. 0x65,0x86,0x75,0xfc,0x6e,0xa5,0x34,0xe0,
  25082. 0x81,0x0a,0x44,0x32,0x82,0x6b,0xf5,0x8c,
  25083. 0x94,0x1e,0xfb,0x65,0xd5,0x7a,0x33,0x8b,
  25084. 0xbd,0x2e,0x26,0x64,0x0f,0x89,0xff,0xbc,
  25085. 0x1a,0x85,0x8e,0xfc,0xb8,0x55,0x0e,0xe3,
  25086. 0xa5,0xe1,0x99,0x8b,0xd1,0x77,0xe9,0x3a,
  25087. 0x73,0x63,0xc3,0x44,0xfe,0x6b,0x19,0x9e,
  25088. 0xe5,0xd0,0x2e,0x82,0xd5,0x22,0xc4,0xfe,
  25089. 0xba,0x15,0x45,0x2f,0x80,0x28,0x8a,0x82,
  25090. 0x1a,0x57,0x91,0x16,0xec,0x6d,0xad,0x2b,
  25091. 0x3b,0x31,0x0d,0xa9,0x03,0x40,0x1a,0xa6,
  25092. 0x21,0x00,0xab,0x5d,0x1a,0x36,0x55,0x3e,
  25093. 0x06,0x20,0x3b,0x33,0x89,0x0c,0xc9,0xb8,
  25094. 0x32,0xf7,0x9e,0xf8,0x05,0x60,0xcc,0xb9,
  25095. 0xa3,0x9c,0xe7,0x67,0x96,0x7e,0xd6,0x28,
  25096. 0xc6,0xad,0x57,0x3c,0xb1,0x16,0xdb,0xef,
  25097. 0xef,0xd7,0x54,0x99,0xda,0x96,0xbd,0x68,
  25098. 0xa8,0xa9,0x7b,0x92,0x8a,0x8b,0xbc,0x10,
  25099. 0x3b,0x66,0x21,0xfc,0xde,0x2b,0xec,0xa1,
  25100. 0x23,0x1d,0x20,0x6b,0xe6,0xcd,0x9e,0xc7,
  25101. 0xaf,0xf6,0xf6,0xc9,0x4f,0xcd,0x72,0x04,
  25102. 0xed,0x34,0x55,0xc6,0x8c,0x83,0xf4,0xa4,
  25103. 0x1d,0xa4,0xaf,0x2b,0x74,0xef,0x5c,0x53,
  25104. 0xf1,0xd8,0xac,0x70,0xbd,0xcb,0x7e,0xd1,
  25105. 0x85,0xce,0x81,0xbd,0x84,0x35,0x9d,0x44,
  25106. 0x25,0x4d,0x95,0x62,0x9e,0x98,0x55,0xa9,
  25107. 0x4a,0x7c,0x19,0x58,0xd1,0xf8,0xad,0xa5,
  25108. 0xd0,0x53,0x2e,0xd8,0xa5,0xaa,0x3f,0xb2,
  25109. 0xd1,0x7b,0xa7,0x0e,0xb6,0x24,0x8e,0x59,
  25110. 0x4e,0x1a,0x22,0x97,0xac,0xbb,0xb3,0x9d,
  25111. 0x50,0x2f,0x1a,0x8c,0x6e,0xb6,0xf1,0xce,
  25112. 0x22,0xb3,0xde,0x1a,0x1f,0x40,0xcc,0x24,
  25113. 0x55,0x41,0x19,0xa8,0x31,0xa9,0xaa,0xd6,
  25114. 0x07,0x9c,0xad,0x88,0x42,0x5d,0xe6,0xbd,
  25115. 0xe1,0xa9,0x18,0x7e,0xbb,0x60,0x92,0xcf,
  25116. 0x67,0xbf,0x2b,0x13,0xfd,0x65,0xf2,0x70,
  25117. 0x88,0xd7,0x8b,0x7e,0x88,0x3c,0x87,0x59,
  25118. 0xd2,0xc4,0xf5,0xc6,0x5a,0xdb,0x75,0x53,
  25119. 0x87,0x8a,0xd5,0x75,0xf9,0xfa,0xd8,0x78,
  25120. 0xe8,0x0a,0x0c,0x9b,0xa6,0x3b,0xcb,0xcc,
  25121. 0x27,0x32,0xe6,0x94,0x85,0xbb,0xc9,0xc9,
  25122. 0x0b,0xfb,0xd6,0x24,0x81,0xd9,0x08,0x9b,
  25123. 0xec,0xcf,0x80,0xcf,0xe2,0xdf,0x16,0xa2,
  25124. 0xcf,0x65,0xbd,0x92,0xdd,0x59,0x7b,0x07,
  25125. 0x07,0xe0,0x91,0x7a,0xf4,0x8b,0xbb,0x75,
  25126. 0xfe,0xd4,0x13,0xd2,0x38,0xf5,0x55,0x5a,
  25127. 0x7a,0x56,0x9d,0x80,0xc3,0x41,0x4a,0x8d,
  25128. 0x08,0x59,0xdc,0x65,0xa4,0x61,0x28,0xba,
  25129. 0xb2,0x7a,0xf8,0x7a,0x71,0x31,0x4f,0x31,
  25130. 0x8c,0x78,0x2b,0x23,0xeb,0xfe,0x80,0x8b,
  25131. 0x82,0xb0,0xce,0x26,0x40,0x1d,0x2e,0x22,
  25132. 0xf0,0x4d,0x83,0xd1,0x25,0x5d,0xc5,0x1a,
  25133. 0xdd,0xd3,0xb7,0x5a,0x2b,0x1a,0xe0,0x78,
  25134. 0x45,0x04,0xdf,0x54,0x3a,0xf8,0x96,0x9b,
  25135. 0xe3,0xea,0x70,0x82,0xff,0x7f,0xc9,0x88,
  25136. 0x8c,0x14,0x4d,0xa2,0xaf,0x58,0x42,0x9e,
  25137. 0xc9,0x60,0x31,0xdb,0xca,0xd3,0xda,0xd9,
  25138. 0xaf,0x0d,0xcb,0xaa,0xaf,0x26,0x8c,0xb8,
  25139. 0xfc,0xff,0xea,0xd9,0x4f,0x3c,0x7c,0xa4,
  25140. 0x95,0xe0,0x56,0xa9,0xb4,0x7a,0xcd,0xb7,
  25141. 0x51,0xfb,0x73,0xe6,0x66,0xc6,0xc6,0x55,
  25142. 0xad,0xe8,0x29,0x72,0x97,0xd0,0x7a,0xd1,
  25143. 0xba,0x5e,0x43,0xf1,0xbc,0xa3,0x23,0x01,
  25144. 0x65,0x13,0x39,0xe2,0x29,0x04,0xcc,0x8c,
  25145. 0x42,0xf5,0x8c,0x30,0xc0,0x4a,0xaf,0xdb,
  25146. 0x03,0x8d,0xda,0x08,0x47,0xdd,0x98,0x8d,
  25147. 0xcd,0xa6,0xf3,0xbf,0xd1,0x5c,0x4b,0x4c,
  25148. 0x45,0x25,0x00,0x4a,0xa0,0x6e,0xef,0xf8,
  25149. 0xca,0x61,0x78,0x3a,0xac,0xec,0x57,0xfb,
  25150. 0x3d,0x1f,0x92,0xb0,0xfe,0x2f,0xd1,0xa8,
  25151. 0x5f,0x67,0x24,0x51,0x7b,0x65,0xe6,0x14,
  25152. 0xad,0x68,0x08,0xd6,0xf6,0xee,0x34,0xdf,
  25153. 0xf7,0x31,0x0f,0xdc,0x82,0xae,0xbf,0xd9,
  25154. 0x04,0xb0,0x1e,0x1d,0xc5,0x4b,0x29,0x27,
  25155. 0x09,0x4b,0x2d,0xb6,0x8d,0x6f,0x90,0x3b,
  25156. 0x68,0x40,0x1a,0xde,0xbf,0x5a,0x7e,0x08,
  25157. 0xd7,0x8f,0xf4,0xef,0x5d,0x63,0x65,0x3a,
  25158. 0x65,0x04,0x0c,0xf9,0xbf,0xd4,0xac,0xa7,
  25159. 0x98,0x4a,0x74,0xd3,0x71,0x45,0x98,0x67,
  25160. 0x80,0xfc,0x0b,0x16,0xac,0x45,0x16,0x49,
  25161. 0xde,0x61,0x88,0xa7,0xdb,0xdf,0x19,0x1f,
  25162. 0x64,0xb5,0xfc,0x5e,0x2a,0xb4,0x7b,0x57,
  25163. 0xf7,0xf7,0x27,0x6c,0xd4,0x19,0xc1,0x7a,
  25164. 0x3c,0xa8,0xe1,0xb9,0x39,0xae,0x49,0xe4,
  25165. 0x88,0xac,0xba,0x6b,0x96,0x56,0x10,0xb5,
  25166. 0x48,0x01,0x09,0xc8,0xb1,0x7b,0x80,0xe1,
  25167. 0xb7,0xb7,0x50,0xdf,0xc7,0x59,0x8d,0x5d,
  25168. 0x50,0x11,0xfd,0x2d,0xcc,0x56,0x00,0xa3,
  25169. 0x2e,0xf5,0xb5,0x2a,0x1e,0xcc,0x82,0x0e,
  25170. 0x30,0x8a,0xa3,0x42,0x72,0x1a,0xac,0x09,
  25171. 0x43,0xbf,0x66,0x86,0xb6,0x4b,0x25,0x79,
  25172. 0x37,0x65,0x04,0xcc,0xc4,0x93,0xd9,0x7e,
  25173. 0x6a,0xed,0x3f,0xb0,0xf9,0xcd,0x71,0xa4,
  25174. 0x3d,0xd4,0x97,0xf0,0x1f,0x17,0xc0,0xe2,
  25175. 0xcb,0x37,0x97,0xaa,0x2a,0x2f,0x25,0x66,
  25176. 0x56,0x16,0x8e,0x6c,0x49,0x6a,0xfc,0x5f,
  25177. 0xb9,0x32,0x46,0xf6,0xb1,0x11,0x63,0x98,
  25178. 0xa3,0x46,0xf1,0xa6,0x41,0xf3,0xb0,0x41,
  25179. 0xe9,0x89,0xf7,0x91,0x4f,0x90,0xcc,0x2c,
  25180. 0x7f,0xff,0x35,0x78,0x76,0xe5,0x06,0xb5,
  25181. 0x0d,0x33,0x4b,0xa7,0x7c,0x22,0x5b,0xc3,
  25182. 0x07,0xba,0x53,0x71,0x52,0xf3,0xf1,0x61,
  25183. 0x0e,0x4e,0xaf,0xe5,0x95,0xf6,0xd9,0xd9,
  25184. 0x0d,0x11,0xfa,0xa9,0x33,0xa1,0x5e,0xf1,
  25185. 0x36,0x95,0x46,0x86,0x8a,0x7f,0x3a,0x45,
  25186. 0xa9,0x67,0x68,0xd4,0x0f,0xd9,0xd0,0x34,
  25187. 0x12,0xc0,0x91,0xc6,0x31,0x5c,0xf4,0xfd,
  25188. 0xe7,0xcb,0x68,0x60,0x69,0x37,0x38,0x0d,
  25189. 0xb2,0xea,0xaa,0x70,0x7b,0x4c,0x41,0x85,
  25190. 0xc3,0x2e,0xdd,0xcd,0xd3,0x06,0x70,0x5e,
  25191. 0x4d,0xc1,0xff,0xc8,0x72,0xee,0xee,0x47,
  25192. 0x5a,0x64,0xdf,0xac,0x86,0xab,0xa4,0x1c,
  25193. 0x06,0x18,0x98,0x3f,0x87,0x41,0xc5,0xef,
  25194. 0x68,0xd3,0xa1,0x01,0xe8,0xa3,0xb8,0xca,
  25195. 0xc6,0x0c,0x90,0x5c,0x15,0xfc,0x91,0x08,
  25196. 0x40,0xb9,0x4c,0x00,0xa0,0xb9,0xd0
  25197. };
  25198. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  25199. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  25200. sizeof(msg2),
  25201. sizeof(msg3),
  25202. 0 /*sizeof(msg1)*/,
  25203. 0 /*sizeof(msg1)*/,
  25204. sizeof(msg4)
  25205. };
  25206. #ifndef NO_ASN
  25207. static byte privateEd25519[] = {
  25208. 0x30,0x2e,0x02,0x01,0x00,0x30,0x05,0x06,
  25209. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  25210. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  25211. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  25212. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  25213. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  25214. };
  25215. static byte badPrivateEd25519[] = {
  25216. 0x30,0x52,0x02,0x01,0x00,0x30,0x05,0x06,
  25217. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  25218. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  25219. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  25220. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  25221. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60,
  25222. 0xa1,0x22,0x04,0x21,0xd7,0x5a,0x98,0x01, /* octet len 0x20 -> 0x21 */
  25223. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  25224. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  25225. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  25226. 0xf7,0x07,0x51,0x1a,
  25227. 0x00 /* add additional bytes to make the pubkey bigger */
  25228. };
  25229. static byte publicEd25519[] = {
  25230. 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,
  25231. 0x70,0x03,0x21,0x00,0xd7,0x5a,0x98,0x01,
  25232. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  25233. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  25234. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  25235. 0xf7,0x07,0x51,0x1a
  25236. };
  25237. /* size has been altered to catch if sanity check is done */
  25238. static byte badPublicEd25519[] = {
  25239. 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,
  25240. 0x70,0x03,0x21,0x00,0xd7,0x5a,0x98,0x01,
  25241. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  25242. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  25243. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  25244. 0xf7,0x07,0x51,0x1a,
  25245. 0x00 /* add an additional byte to make the pubkey appear bigger */
  25246. };
  25247. static byte privPubEd25519[] = {
  25248. 0x30,0x50,0x02,0x01,0x00,0x30,0x05,0x06,
  25249. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  25250. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  25251. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  25252. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  25253. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60,
  25254. 0x81,0x20,0xd7,0x5a,0x98,0x01,0x82,0xb1,
  25255. 0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,0xc9,0x64,
  25256. 0x07,0x3a,0x0e,0xe1,0x72,0xf3,0xda,0xa6,
  25257. 0x23,0x25,0xaf,0x02,0x1a,0x68,0xf7,0x07,
  25258. 0x51,0x1a
  25259. };
  25260. word32 idx;
  25261. #endif /* NO_ASN */
  25262. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  25263. #if !defined(NO_ASN) && defined(HAVE_ED25519_SIGN)
  25264. ed25519_key key3;
  25265. #endif
  25266. /* create ed25519 keys */
  25267. #ifndef HAVE_FIPS
  25268. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  25269. #else
  25270. ret = wc_InitRng(&rng);
  25271. #endif
  25272. if (ret != 0)
  25273. return -11000;
  25274. wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  25275. wc_ed25519_init_ex(&key2, HEAP_HINT, devId);
  25276. #if !defined(NO_ASN) && defined(HAVE_ED25519_SIGN)
  25277. wc_ed25519_init_ex(&key3, HEAP_HINT, devId);
  25278. #endif
  25279. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  25280. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key2);
  25281. /* helper functions for signature and key size */
  25282. keySz = wc_ed25519_size(&key);
  25283. sigSz = wc_ed25519_sig_size(&key);
  25284. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  25285. defined(HAVE_ED25519_KEY_IMPORT)
  25286. for (i = 0; i < 6; i++) {
  25287. outlen = sizeof(out);
  25288. XMEMSET(out, 0, sizeof(out));
  25289. if (wc_ed25519_import_private_key(sKeys[i], ED25519_KEY_SIZE, pKeys[i],
  25290. pKeySz[i], &key) != 0)
  25291. return -11001 - i;
  25292. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key) != 0)
  25293. return -11011 - i;
  25294. if (XMEMCMP(out, sigs[i], 64))
  25295. return -11021 - i;
  25296. #if defined(HAVE_ED25519_VERIFY)
  25297. /* test verify on good msg */
  25298. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  25299. &key) != 0 || verify != 1)
  25300. return -11031 - i;
  25301. #ifdef WOLFSSL_ED25519_STREAMING_VERIFY
  25302. /* test verify on good msg using streaming interface directly */
  25303. if (wc_ed25519_verify_msg_init(out, outlen,
  25304. &key, (byte)Ed25519, NULL, 0) != 0)
  25305. return -11211 - i;
  25306. for (j = 0; j < msgSz[i]; j += i) {
  25307. if (wc_ed25519_verify_msg_update(msgs[i] + j, MIN(i, msgSz[i] - j), &key) != 0)
  25308. return -11221 - i;
  25309. }
  25310. if (wc_ed25519_verify_msg_final(out, outlen, &verify,
  25311. &key) != 0 || verify != 1)
  25312. return -11231 - i;
  25313. #endif /* WOLFSSL_ED25519_STREAMING_VERIFY */
  25314. /* test verify on bad msg */
  25315. out[outlen-1] = out[outlen-1] + 1;
  25316. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  25317. &key) == 0 || verify == 1)
  25318. return -11041 - i;
  25319. #endif /* HAVE_ED25519_VERIFY */
  25320. /* test api for import/exporting keys */
  25321. exportPSz = sizeof(exportPKey);
  25322. exportSSz = sizeof(exportSKey);
  25323. if (wc_ed25519_export_public(&key, exportPKey, &exportPSz) != 0)
  25324. return -11051 - i;
  25325. if (wc_ed25519_import_public_ex(exportPKey, exportPSz, &key2, 1) != 0)
  25326. return -11061 - i;
  25327. if (wc_ed25519_export_private_only(&key, exportSKey, &exportSSz) != 0)
  25328. return -11071 - i;
  25329. if (wc_ed25519_import_private_key(exportSKey, exportSSz,
  25330. exportPKey, exportPSz, &key2) != 0)
  25331. return -11081 - i;
  25332. /* clear "out" buffer and test sign with imported keys */
  25333. outlen = sizeof(out);
  25334. XMEMSET(out, 0, sizeof(out));
  25335. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key2) != 0)
  25336. return -11091 - i;
  25337. #if defined(HAVE_ED25519_VERIFY)
  25338. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  25339. &key2) != 0 || verify != 1)
  25340. return -11101 - i;
  25341. if (XMEMCMP(out, sigs[i], 64))
  25342. return -11111 - i;
  25343. #endif /* HAVE_ED25519_VERIFY */
  25344. }
  25345. ret = ed25519ctx_test();
  25346. if (ret != 0)
  25347. return ret;
  25348. ret = ed25519ph_test();
  25349. if (ret != 0)
  25350. return ret;
  25351. #ifndef NO_ASN
  25352. /* Try ASN.1 encoded private-only key and public key. */
  25353. idx = 0;
  25354. if (wc_Ed25519PrivateKeyDecode(privateEd25519, &idx, &key3,
  25355. sizeof(privateEd25519)) != 0)
  25356. return -11121;
  25357. idx = 0;
  25358. if (wc_Ed25519PrivateKeyDecode(badPrivateEd25519, &idx, &key3,
  25359. sizeof(badPrivateEd25519)) == 0)
  25360. return -11122;
  25361. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3)
  25362. != BAD_FUNC_ARG)
  25363. return -11131;
  25364. /* try with a buffer size that is too large */
  25365. idx = 0;
  25366. if (wc_Ed25519PublicKeyDecode(badPublicEd25519, &idx, &key3,
  25367. sizeof(badPublicEd25519)) == 0)
  25368. return -11140;
  25369. idx = 0;
  25370. if (wc_Ed25519PublicKeyDecode(publicEd25519, &idx, &key3,
  25371. sizeof(publicEd25519)) != 0)
  25372. return -11141;
  25373. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3) != 0)
  25374. return -11151;
  25375. if (XMEMCMP(out, sigs[0], 64))
  25376. return -11161;
  25377. #if defined(HAVE_ED25519_VERIFY)
  25378. /* test verify on good msg */
  25379. if (wc_ed25519_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, &key3)
  25380. != 0 || verify != 1)
  25381. return -11171;
  25382. #endif /* HAVE_ED25519_VERIFY */
  25383. wc_ed25519_free(&key3);
  25384. wc_ed25519_init(&key3);
  25385. idx = 0;
  25386. if (wc_Ed25519PrivateKeyDecode(privPubEd25519, &idx, &key3,
  25387. sizeof(privPubEd25519)) != 0)
  25388. return -11181;
  25389. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3) != 0)
  25390. return -11191;
  25391. if (XMEMCMP(out, sigs[0], 64))
  25392. return -11201;
  25393. wc_ed25519_free(&key3);
  25394. #endif /* NO_ASN */
  25395. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  25396. /* clean up keys when done */
  25397. wc_ed25519_free(&key);
  25398. wc_ed25519_free(&key2);
  25399. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  25400. wc_FreeRng(&rng);
  25401. #endif
  25402. /* hush warnings of unused keySz and sigSz */
  25403. (void)keySz;
  25404. (void)sigSz;
  25405. #ifdef WOLFSSL_TEST_CERT
  25406. ret = ed25519_test_cert();
  25407. if (ret < 0)
  25408. return ret;
  25409. #ifdef WOLFSSL_CERT_GEN
  25410. ret = ed25519_test_make_cert();
  25411. if (ret < 0)
  25412. return ret;
  25413. #endif /* WOLFSSL_CERT_GEN */
  25414. #endif /* WOLFSSL_TEST_CERT */
  25415. return 0;
  25416. }
  25417. #endif /* HAVE_ED25519 */
  25418. #ifdef HAVE_CURVE448
  25419. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  25420. defined(HAVE_CURVE448_KEY_IMPORT)
  25421. /* Test the wc_curve448_check_public API.
  25422. *
  25423. * returns 0 on success and -ve on failure.
  25424. */
  25425. static int curve448_check_public_test(void)
  25426. {
  25427. /* Little-endian values that will fail */
  25428. byte fail_le[][CURVE448_KEY_SIZE] = {
  25429. {
  25430. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25431. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25432. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25433. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25434. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25435. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25436. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  25437. },
  25438. {
  25439. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25440. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25441. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25442. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25443. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25444. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25445. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  25446. },
  25447. };
  25448. /* Big-endian values that will fail */
  25449. byte fail_be[][CURVE448_KEY_SIZE] = {
  25450. {
  25451. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25452. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25453. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25454. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25455. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25456. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25457. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  25458. },
  25459. {
  25460. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25461. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25462. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25463. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25464. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25465. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25466. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  25467. },
  25468. };
  25469. /* Good or valid public value */
  25470. byte good[CURVE448_KEY_SIZE] = {
  25471. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25472. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25473. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25474. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25475. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25476. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25477. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  25478. };
  25479. int i;
  25480. /* Parameter checks */
  25481. /* NULL pointer */
  25482. if (wc_curve448_check_public(NULL, 0, EC448_LITTLE_ENDIAN) !=
  25483. BAD_FUNC_ARG) {
  25484. return -11300;
  25485. }
  25486. if (wc_curve448_check_public(NULL, 0, EC448_BIG_ENDIAN) != BAD_FUNC_ARG) {
  25487. return -11301;
  25488. }
  25489. /* Length of 0 treated differently to other invalid lengths for TLS */
  25490. if (wc_curve448_check_public(good, 0, EC448_LITTLE_ENDIAN) != BUFFER_E)
  25491. return -11302;
  25492. if (wc_curve448_check_public(good, 0, EC448_BIG_ENDIAN) != BUFFER_E)
  25493. return -11303;
  25494. /* Length not CURVE448_KEY_SIZE */
  25495. for (i = 1; i < CURVE448_KEY_SIZE + 2; i++) {
  25496. if (i == CURVE448_KEY_SIZE)
  25497. continue;
  25498. if (wc_curve448_check_public(good, i, EC448_LITTLE_ENDIAN) !=
  25499. ECC_BAD_ARG_E) {
  25500. return -11304 - i;
  25501. }
  25502. if (wc_curve448_check_public(good, i, EC448_BIG_ENDIAN) !=
  25503. ECC_BAD_ARG_E) {
  25504. return -11314 - i;
  25505. }
  25506. }
  25507. /* Little-endian fail cases */
  25508. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  25509. if (wc_curve448_check_public(fail_le[i], CURVE448_KEY_SIZE,
  25510. EC448_LITTLE_ENDIAN) == 0) {
  25511. return -11324 - i;
  25512. }
  25513. }
  25514. /* Big-endian fail cases */
  25515. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  25516. if (wc_curve448_check_public(fail_be[i], CURVE448_KEY_SIZE,
  25517. EC448_BIG_ENDIAN) == 0) {
  25518. return -11334 - i;
  25519. }
  25520. }
  25521. /* Check a valid public value works! */
  25522. if (wc_curve448_check_public(good, CURVE448_KEY_SIZE,
  25523. EC448_LITTLE_ENDIAN) != 0) {
  25524. return -11344;
  25525. }
  25526. if (wc_curve448_check_public(good, CURVE448_KEY_SIZE,
  25527. EC448_BIG_ENDIAN) != 0) {
  25528. return -11345;
  25529. }
  25530. return 0;
  25531. }
  25532. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  25533. WOLFSSL_TEST_SUBROUTINE int curve448_test(void)
  25534. {
  25535. WC_RNG rng;
  25536. int ret;
  25537. #ifdef HAVE_CURVE448_SHARED_SECRET
  25538. byte sharedA[CURVE448_KEY_SIZE];
  25539. byte sharedB[CURVE448_KEY_SIZE];
  25540. word32 y;
  25541. #endif
  25542. #ifdef HAVE_CURVE448_KEY_EXPORT
  25543. byte exportBuf[CURVE448_KEY_SIZE];
  25544. #endif
  25545. word32 x;
  25546. curve448_key userA, userB, pubKey;
  25547. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  25548. defined(HAVE_CURVE448_KEY_IMPORT)
  25549. /* test vectors from
  25550. https://www.rfc-editor.org/rfc/rfc7748.html
  25551. */
  25552. /* secret key for party a */
  25553. byte sa[] = {
  25554. 0x6b, 0x72, 0x98, 0xa5, 0xc0, 0xd8, 0xc2, 0x9a,
  25555. 0x1d, 0xab, 0x27, 0xf1, 0xa6, 0x82, 0x63, 0x00,
  25556. 0x91, 0x73, 0x89, 0x44, 0x97, 0x41, 0xa9, 0x74,
  25557. 0xf5, 0xba, 0xc9, 0xd9, 0x8d, 0xc2, 0x98, 0xd4,
  25558. 0x65, 0x55, 0xbc, 0xe8, 0xba, 0xe8, 0x9e, 0xee,
  25559. 0xd4, 0x00, 0x58, 0x4b, 0xb0, 0x46, 0xcf, 0x75,
  25560. 0x57, 0x9f, 0x51, 0xd1, 0x25, 0x49, 0x8f, 0x9a,
  25561. };
  25562. /* public key for party a */
  25563. byte pa[] = {
  25564. 0xa0, 0x1f, 0xc4, 0x32, 0xe5, 0x80, 0x7f, 0x17,
  25565. 0x53, 0x0d, 0x12, 0x88, 0xda, 0x12, 0x5b, 0x0c,
  25566. 0xd4, 0x53, 0xd9, 0x41, 0x72, 0x64, 0x36, 0xc8,
  25567. 0xbb, 0xd9, 0xc5, 0x22, 0x2c, 0x3d, 0xa7, 0xfa,
  25568. 0x63, 0x9c, 0xe0, 0x3d, 0xb8, 0xd2, 0x3b, 0x27,
  25569. 0x4a, 0x07, 0x21, 0xa1, 0xae, 0xd5, 0x22, 0x7d,
  25570. 0xe6, 0xe3, 0xb7, 0x31, 0xcc, 0xf7, 0x08, 0x9b,
  25571. };
  25572. /* secret key for party b */
  25573. byte sb[] = {
  25574. 0x2d, 0x99, 0x73, 0x51, 0xb6, 0x10, 0x6f, 0x36,
  25575. 0xb0, 0xd1, 0x09, 0x1b, 0x92, 0x9c, 0x4c, 0x37,
  25576. 0x21, 0x3e, 0x0d, 0x2b, 0x97, 0xe8, 0x5e, 0xbb,
  25577. 0x20, 0xc1, 0x27, 0x69, 0x1d, 0x0d, 0xad, 0x8f,
  25578. 0x1d, 0x81, 0x75, 0xb0, 0x72, 0x37, 0x45, 0xe6,
  25579. 0x39, 0xa3, 0xcb, 0x70, 0x44, 0x29, 0x0b, 0x99,
  25580. 0xe0, 0xe2, 0xa0, 0xc2, 0x7a, 0x6a, 0x30, 0x1c,
  25581. };
  25582. /* public key for party b */
  25583. byte pb[] = {
  25584. 0x09, 0x36, 0xf3, 0x7b, 0xc6, 0xc1, 0xbd, 0x07,
  25585. 0xae, 0x3d, 0xec, 0x7a, 0xb5, 0xdc, 0x06, 0xa7,
  25586. 0x3c, 0xa1, 0x32, 0x42, 0xfb, 0x34, 0x3e, 0xfc,
  25587. 0x72, 0xb9, 0xd8, 0x27, 0x30, 0xb4, 0x45, 0xf3,
  25588. 0xd4, 0xb0, 0xbd, 0x07, 0x71, 0x62, 0xa4, 0x6d,
  25589. 0xcf, 0xec, 0x6f, 0x9b, 0x59, 0x0b, 0xfc, 0xbc,
  25590. 0xf5, 0x20, 0xcd, 0xb0, 0x29, 0xa8, 0xb7, 0x3e,
  25591. };
  25592. /* expected shared key */
  25593. byte ss[] = {
  25594. 0x9d, 0x87, 0x4a, 0x51, 0x37, 0x50, 0x9a, 0x44,
  25595. 0x9a, 0xd5, 0x85, 0x30, 0x40, 0x24, 0x1c, 0x52,
  25596. 0x36, 0x39, 0x54, 0x35, 0xc3, 0x64, 0x24, 0xfd,
  25597. 0x56, 0x0b, 0x0c, 0xb6, 0x2b, 0x28, 0x1d, 0x28,
  25598. 0x52, 0x75, 0xa7, 0x40, 0xce, 0x32, 0xa2, 0x2d,
  25599. 0xd1, 0x74, 0x0f, 0x4a, 0xa9, 0x16, 0x1c, 0xec,
  25600. 0x95, 0xcc, 0xc6, 0x1a, 0x18, 0xf4, 0xff, 0x07,
  25601. };
  25602. #endif /* HAVE_CURVE448_SHARED_SECRET */
  25603. (void)x;
  25604. #ifndef HAVE_FIPS
  25605. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  25606. #else
  25607. ret = wc_InitRng(&rng);
  25608. #endif
  25609. if (ret != 0)
  25610. return -11400;
  25611. wc_curve448_init(&userA);
  25612. wc_curve448_init(&userB);
  25613. wc_curve448_init(&pubKey);
  25614. /* make curve448 keys */
  25615. if (wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userA) != 0)
  25616. return -11401;
  25617. if (wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userB) != 0)
  25618. return -11402;
  25619. #ifdef HAVE_CURVE448_SHARED_SECRET
  25620. /* find shared secret key */
  25621. x = sizeof(sharedA);
  25622. if (wc_curve448_shared_secret(&userA, &userB, sharedA, &x) != 0)
  25623. return -11403;
  25624. y = sizeof(sharedB);
  25625. if (wc_curve448_shared_secret(&userB, &userA, sharedB, &y) != 0)
  25626. return -11404;
  25627. /* compare shared secret keys to test they are the same */
  25628. if (y != x)
  25629. return -11405;
  25630. if (XMEMCMP(sharedA, sharedB, x))
  25631. return -11406;
  25632. #endif
  25633. #ifdef HAVE_CURVE448_KEY_EXPORT
  25634. /* export a public key and import it for another user */
  25635. x = sizeof(exportBuf);
  25636. if (wc_curve448_export_public(&userA, exportBuf, &x) != 0)
  25637. return -11407;
  25638. #ifdef HAVE_CURVE448_KEY_IMPORT
  25639. if (wc_curve448_import_public(exportBuf, x, &pubKey) != 0)
  25640. return -11408;
  25641. #endif
  25642. #endif
  25643. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  25644. defined(HAVE_CURVE448_KEY_IMPORT)
  25645. /* test shared key after importing a public key */
  25646. XMEMSET(sharedB, 0, sizeof(sharedB));
  25647. y = sizeof(sharedB);
  25648. if (wc_curve448_shared_secret(&userB, &pubKey, sharedB, &y) != 0)
  25649. return -11409;
  25650. if (XMEMCMP(sharedA, sharedB, y))
  25651. return -11410;
  25652. /* import RFC test vectors and compare shared key */
  25653. if (wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  25654. != 0)
  25655. return -11411;
  25656. if (wc_curve448_import_private_raw(sb, sizeof(sb), pb, sizeof(pb), &userB)
  25657. != 0)
  25658. return -11412;
  25659. /* test against known test vector */
  25660. XMEMSET(sharedB, 0, sizeof(sharedB));
  25661. y = sizeof(sharedB);
  25662. if (wc_curve448_shared_secret(&userA, &userB, sharedB, &y) != 0)
  25663. return -11413;
  25664. if (XMEMCMP(ss, sharedB, y))
  25665. return -11414;
  25666. /* test swapping roles of keys and generating same shared key */
  25667. XMEMSET(sharedB, 0, sizeof(sharedB));
  25668. y = sizeof(sharedB);
  25669. if (wc_curve448_shared_secret(&userB, &userA, sharedB, &y) != 0)
  25670. return -11415;
  25671. if (XMEMCMP(ss, sharedB, y))
  25672. return -11416;
  25673. /* test with 1 generated key and 1 from known test vector */
  25674. if (wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  25675. != 0)
  25676. return -11417;
  25677. if (wc_curve448_make_key(&rng, 56, &userB) != 0)
  25678. return -11418;
  25679. x = sizeof(sharedA);
  25680. if (wc_curve448_shared_secret(&userA, &userB, sharedA, &x) != 0)
  25681. return -11419;
  25682. y = sizeof(sharedB);
  25683. if (wc_curve448_shared_secret(&userB, &userA, sharedB, &y) != 0)
  25684. return -11420;
  25685. /* compare shared secret keys to test they are the same */
  25686. if (y != x)
  25687. return -11421;
  25688. if (XMEMCMP(sharedA, sharedB, x))
  25689. return -11422;
  25690. ret = curve448_check_public_test();
  25691. if (ret != 0)
  25692. return ret;
  25693. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  25694. /* clean up keys when done */
  25695. wc_curve448_free(&pubKey);
  25696. wc_curve448_free(&userB);
  25697. wc_curve448_free(&userA);
  25698. wc_FreeRng(&rng);
  25699. return 0;
  25700. }
  25701. #endif /* HAVE_CURVE448 */
  25702. #ifdef HAVE_ED448
  25703. #ifdef WOLFSSL_TEST_CERT
  25704. static int ed448_test_cert(void)
  25705. {
  25706. DecodedCert cert[2];
  25707. DecodedCert* serverCert = NULL;
  25708. DecodedCert* caCert = NULL;
  25709. #ifdef HAVE_ED448_VERIFY
  25710. ed448_key key;
  25711. ed448_key* pubKey = NULL;
  25712. int verify;
  25713. #endif /* HAVE_ED448_VERIFY */
  25714. int ret;
  25715. byte* tmp;
  25716. size_t bytes;
  25717. XFILE file;
  25718. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25719. if (tmp == NULL) {
  25720. ERROR_OUT(-11430, done);
  25721. }
  25722. #ifdef USE_CERT_BUFFERS_256
  25723. XMEMCPY(tmp, ca_ed448_cert, sizeof_ca_ed448_cert);
  25724. bytes = sizeof_ca_ed448_cert;
  25725. #elif !defined(NO_FILESYSTEM)
  25726. file = XFOPEN(caEd448Cert, "rb");
  25727. if (file == NULL) {
  25728. ERROR_OUT(-11431, done);
  25729. }
  25730. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  25731. XFCLOSE(file);
  25732. #else
  25733. /* No certificate to use. */
  25734. ERROR_OUT(-11432, done);
  25735. #endif
  25736. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  25737. caCert = &cert[0];
  25738. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  25739. if (ret != 0) {
  25740. ERROR_OUT(-11433, done);
  25741. }
  25742. #ifdef USE_CERT_BUFFERS_256
  25743. XMEMCPY(tmp, server_ed448_cert, sizeof_server_ed448_cert);
  25744. bytes = sizeof_server_ed448_cert;
  25745. #elif !defined(NO_FILESYSTEM)
  25746. file = XFOPEN(serverEd448Cert, "rb");
  25747. if (file == NULL) {
  25748. ERROR_OUT(-11434, done);
  25749. }
  25750. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  25751. XFCLOSE(file);
  25752. #else
  25753. /* No certificate to use. */
  25754. ERROR_OUT(-11435, done);
  25755. #endif
  25756. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  25757. serverCert = &cert[1];
  25758. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  25759. if (ret != 0) {
  25760. ERROR_OUT(-11436, done);
  25761. }
  25762. #ifdef HAVE_ED448_VERIFY
  25763. ret = wc_ed448_init(&key);
  25764. if (ret < 0) {
  25765. ERROR_OUT(-11437, done);
  25766. }
  25767. pubKey = &key;
  25768. ret = wc_ed448_import_public(caCert->publicKey, caCert->pubKeySize, pubKey);
  25769. if (ret < 0) {
  25770. ERROR_OUT(-11438, done);
  25771. }
  25772. if (wc_ed448_verify_msg(serverCert->signature, serverCert->sigLength,
  25773. serverCert->source + serverCert->certBegin,
  25774. serverCert->sigIndex - serverCert->certBegin,
  25775. &verify, pubKey, NULL, 0) < 0 || verify != 1) {
  25776. ERROR_OUT(-11439, done);
  25777. }
  25778. #endif /* HAVE_ED448_VERIFY */
  25779. done:
  25780. if (tmp != NULL)
  25781. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25782. #ifdef HAVE_ED448_VERIFY
  25783. wc_ed448_free(pubKey);
  25784. #endif /* HAVE_ED448_VERIFY */
  25785. if (caCert != NULL)
  25786. FreeDecodedCert(caCert);
  25787. if (serverCert != NULL)
  25788. FreeDecodedCert(serverCert);
  25789. return ret;
  25790. }
  25791. static int ed448_test_make_cert(void)
  25792. {
  25793. WC_RNG rng;
  25794. Cert cert;
  25795. DecodedCert decode;
  25796. ed448_key key;
  25797. ed448_key* privKey = NULL;
  25798. int ret = 0;
  25799. byte* tmp = NULL;
  25800. wc_InitCert_ex(&cert, HEAP_HINT, devId);
  25801. #ifndef HAVE_FIPS
  25802. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  25803. #else
  25804. ret = wc_InitRng(&rng);
  25805. #endif
  25806. if (ret != 0)
  25807. return -11450;
  25808. wc_ed448_init(&key);
  25809. privKey = &key;
  25810. wc_ed448_make_key(&rng, ED448_KEY_SIZE, privKey);
  25811. cert.daysValid = 365 * 2;
  25812. cert.selfSigned = 1;
  25813. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  25814. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  25815. cert.isCA = 0;
  25816. #ifdef WOLFSSL_CERT_EXT
  25817. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  25818. if (ret < 0) {
  25819. ERROR_OUT(-11451, done);
  25820. }
  25821. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  25822. if (ret < 0) {
  25823. ERROR_OUT(-11452, done);
  25824. }
  25825. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  25826. if (ret < 0) {
  25827. ERROR_OUT(-11453, done);
  25828. }
  25829. #endif
  25830. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25831. if (tmp == NULL) {
  25832. ERROR_OUT(-11454, done);
  25833. }
  25834. cert.sigType = CTC_ED448;
  25835. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED448_TYPE, privKey, &rng);
  25836. if (ret < 0) {
  25837. ERROR_OUT(-11455, done);
  25838. }
  25839. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF, ED448_TYPE,
  25840. privKey, &rng);
  25841. if (ret < 0) {
  25842. ERROR_OUT(-11456, done);
  25843. }
  25844. InitDecodedCert(&decode, tmp, ret, HEAP_HINT);
  25845. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  25846. FreeDecodedCert(&decode);
  25847. if (ret != 0) {
  25848. ERROR_OUT(-11457, done);
  25849. }
  25850. done:
  25851. if (tmp != NULL)
  25852. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25853. wc_ed448_free(privKey);
  25854. wc_FreeRng(&rng);
  25855. return ret;
  25856. }
  25857. #endif /* WOLFSSL_TEST_CERT */
  25858. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  25859. defined(HAVE_ED448_KEY_IMPORT)
  25860. static int ed448_ctx_test(void)
  25861. {
  25862. byte out[ED448_SIG_SIZE];
  25863. word32 outlen;
  25864. #ifdef HAVE_ED448_VERIFY
  25865. int verify;
  25866. #endif /* HAVE_ED448_VERIFY */
  25867. ed448_key key;
  25868. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  25869. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  25870. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  25871. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  25872. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  25873. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  25874. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  25875. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  25876. 0x4e
  25877. };
  25878. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  25879. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  25880. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  25881. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  25882. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  25883. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  25884. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  25885. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  25886. 0x80
  25887. };
  25888. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx[] = {
  25889. 0xd4, 0xf8, 0xf6, 0x13, 0x17, 0x70, 0xdd, 0x46,
  25890. 0xf4, 0x08, 0x67, 0xd6, 0xfd, 0x5d, 0x50, 0x55,
  25891. 0xde, 0x43, 0x54, 0x1f, 0x8c, 0x5e, 0x35, 0xab,
  25892. 0xbc, 0xd0, 0x01, 0xb3, 0x2a, 0x89, 0xf7, 0xd2,
  25893. 0x15, 0x1f, 0x76, 0x47, 0xf1, 0x1d, 0x8c, 0xa2,
  25894. 0xae, 0x27, 0x9f, 0xb8, 0x42, 0xd6, 0x07, 0x21,
  25895. 0x7f, 0xce, 0x6e, 0x04, 0x2f, 0x68, 0x15, 0xea,
  25896. 0x00, 0x0c, 0x85, 0x74, 0x1d, 0xe5, 0xc8, 0xda,
  25897. 0x11, 0x44, 0xa6, 0xa1, 0xab, 0xa7, 0xf9, 0x6d,
  25898. 0xe4, 0x25, 0x05, 0xd7, 0xa7, 0x29, 0x85, 0x24,
  25899. 0xfd, 0xa5, 0x38, 0xfc, 0xcb, 0xbb, 0x75, 0x4f,
  25900. 0x57, 0x8c, 0x1c, 0xad, 0x10, 0xd5, 0x4d, 0x0d,
  25901. 0x54, 0x28, 0x40, 0x7e, 0x85, 0xdc, 0xbc, 0x98,
  25902. 0xa4, 0x91, 0x55, 0xc1, 0x37, 0x64, 0xe6, 0x6c,
  25903. 0x3c, 0x00
  25904. };
  25905. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  25906. 0x03
  25907. };
  25908. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  25909. 0x66,0x6f,0x6f
  25910. };
  25911. outlen = sizeof(out);
  25912. XMEMSET(out, 0, sizeof(out));
  25913. if (wc_ed448_init_ex(&key, HEAP_HINT, devId) != 0)
  25914. return -11500;
  25915. if (wc_ed448_import_private_key(sKeyCtx, ED448_KEY_SIZE, pKeyCtx,
  25916. sizeof(pKeyCtx), &key) != 0)
  25917. return -11501;
  25918. if (wc_ed448_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  25919. contextCtx, sizeof(contextCtx)) != 0)
  25920. return -11502;
  25921. if (XMEMCMP(out, sigCtx, sizeof(sigCtx)))
  25922. return -11503;
  25923. #if defined(HAVE_ED448_VERIFY)
  25924. /* test verify on good msg */
  25925. if (wc_ed448_verify_msg(out, outlen, msgCtx, sizeof(msgCtx), &verify, &key,
  25926. contextCtx, sizeof(contextCtx)) != 0 || verify != 1)
  25927. return -11504;
  25928. #endif
  25929. wc_ed448_free(&key);
  25930. return 0;
  25931. }
  25932. static int ed448ph_test(void)
  25933. {
  25934. byte out[ED448_SIG_SIZE];
  25935. word32 outlen;
  25936. #ifdef HAVE_ED448_VERIFY
  25937. int verify;
  25938. #endif /* HAVE_ED448_VERIFY */
  25939. ed448_key key;
  25940. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  25941. 0x83, 0x3f, 0xe6, 0x24, 0x09, 0x23, 0x7b, 0x9d,
  25942. 0x62, 0xec, 0x77, 0x58, 0x75, 0x20, 0x91, 0x1e,
  25943. 0x9a, 0x75, 0x9c, 0xec, 0x1d, 0x19, 0x75, 0x5b,
  25944. 0x7d, 0xa9, 0x01, 0xb9, 0x6d, 0xca, 0x3d, 0x42,
  25945. 0xef, 0x78, 0x22, 0xe0, 0xd5, 0x10, 0x41, 0x27,
  25946. 0xdc, 0x05, 0xd6, 0xdb, 0xef, 0xde, 0x69, 0xe3,
  25947. 0xab, 0x2c, 0xec, 0x7c, 0x86, 0x7c, 0x6e, 0x2c,
  25948. 0x49
  25949. };
  25950. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  25951. 0x25, 0x9b, 0x71, 0xc1, 0x9f, 0x83, 0xef, 0x77,
  25952. 0xa7, 0xab, 0xd2, 0x65, 0x24, 0xcb, 0xdb, 0x31,
  25953. 0x61, 0xb5, 0x90, 0xa4, 0x8f, 0x7d, 0x17, 0xde,
  25954. 0x3e, 0xe0, 0xba, 0x9c, 0x52, 0xbe, 0xb7, 0x43,
  25955. 0xc0, 0x94, 0x28, 0xa1, 0x31, 0xd6, 0xb1, 0xb5,
  25956. 0x73, 0x03, 0xd9, 0x0d, 0x81, 0x32, 0xc2, 0x76,
  25957. 0xd5, 0xed, 0x3d, 0x5d, 0x01, 0xc0, 0xf5, 0x38,
  25958. 0x80
  25959. };
  25960. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  25961. 0x82, 0x2f, 0x69, 0x01, 0xf7, 0x48, 0x0f, 0x3d,
  25962. 0x5f, 0x56, 0x2c, 0x59, 0x29, 0x94, 0xd9, 0x69,
  25963. 0x36, 0x02, 0x87, 0x56, 0x14, 0x48, 0x32, 0x56,
  25964. 0x50, 0x56, 0x00, 0xbb, 0xc2, 0x81, 0xae, 0x38,
  25965. 0x1f, 0x54, 0xd6, 0xbc, 0xe2, 0xea, 0x91, 0x15,
  25966. 0x74, 0x93, 0x2f, 0x52, 0xa4, 0xe6, 0xca, 0xdd,
  25967. 0x78, 0x76, 0x93, 0x75, 0xec, 0x3f, 0xfd, 0x1b,
  25968. 0x80, 0x1a, 0x0d, 0x9b, 0x3f, 0x40, 0x30, 0xcd,
  25969. 0x43, 0x39, 0x64, 0xb6, 0x45, 0x7e, 0xa3, 0x94,
  25970. 0x76, 0x51, 0x12, 0x14, 0xf9, 0x74, 0x69, 0xb5,
  25971. 0x7d, 0xd3, 0x2d, 0xbc, 0x56, 0x0a, 0x9a, 0x94,
  25972. 0xd0, 0x0b, 0xff, 0x07, 0x62, 0x04, 0x64, 0xa3,
  25973. 0xad, 0x20, 0x3d, 0xf7, 0xdc, 0x7c, 0xe3, 0x60,
  25974. 0xc3, 0xcd, 0x36, 0x96, 0xd9, 0xd9, 0xfa, 0xb9,
  25975. 0x0f, 0x00
  25976. };
  25977. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  25978. 0xc3, 0x22, 0x99, 0xd4, 0x6e, 0xc8, 0xff, 0x02,
  25979. 0xb5, 0x45, 0x40, 0x98, 0x28, 0x14, 0xdc, 0xe9,
  25980. 0xa0, 0x58, 0x12, 0xf8, 0x19, 0x62, 0xb6, 0x49,
  25981. 0xd5, 0x28, 0x09, 0x59, 0x16, 0xa2, 0xaa, 0x48,
  25982. 0x10, 0x65, 0xb1, 0x58, 0x04, 0x23, 0xef, 0x92,
  25983. 0x7e, 0xcf, 0x0a, 0xf5, 0x88, 0x8f, 0x90, 0xda,
  25984. 0x0f, 0x6a, 0x9a, 0x85, 0xad, 0x5d, 0xc3, 0xf2,
  25985. 0x80, 0xd9, 0x12, 0x24, 0xba, 0x99, 0x11, 0xa3,
  25986. 0x65, 0x3d, 0x00, 0xe4, 0x84, 0xe2, 0xce, 0x23,
  25987. 0x25, 0x21, 0x48, 0x1c, 0x86, 0x58, 0xdf, 0x30,
  25988. 0x4b, 0xb7, 0x74, 0x5a, 0x73, 0x51, 0x4c, 0xdb,
  25989. 0x9b, 0xf3, 0xe1, 0x57, 0x84, 0xab, 0x71, 0x28,
  25990. 0x4f, 0x8d, 0x07, 0x04, 0xa6, 0x08, 0xc5, 0x4a,
  25991. 0x6b, 0x62, 0xd9, 0x7b, 0xeb, 0x51, 0x1d, 0x13,
  25992. 0x21, 0x00
  25993. };
  25994. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  25995. 0x61,0x62,0x63
  25996. };
  25997. /* SHA-512 hash of msgPh */
  25998. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  25999. 0x48, 0x33, 0x66, 0x60, 0x13, 0x60, 0xa8, 0x77,
  26000. 0x1c, 0x68, 0x63, 0x08, 0x0c, 0xc4, 0x11, 0x4d,
  26001. 0x8d, 0xb4, 0x45, 0x30, 0xf8, 0xf1, 0xe1, 0xee,
  26002. 0x4f, 0x94, 0xea, 0x37, 0xe7, 0x8b, 0x57, 0x39,
  26003. 0xd5, 0xa1, 0x5b, 0xef, 0x18, 0x6a, 0x53, 0x86,
  26004. 0xc7, 0x57, 0x44, 0xc0, 0x52, 0x7e, 0x1f, 0xaa,
  26005. 0x9f, 0x87, 0x26, 0xe4, 0x62, 0xa1, 0x2a, 0x4f,
  26006. 0xeb, 0x06, 0xbd, 0x88, 0x01, 0xe7, 0x51, 0xe4
  26007. };
  26008. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  26009. 0x66,0x6f,0x6f
  26010. };
  26011. outlen = sizeof(out);
  26012. XMEMSET(out, 0, sizeof(out));
  26013. if (wc_ed448_init_ex(&key, HEAP_HINT, devId) != 0)
  26014. return -11600;
  26015. if (wc_ed448_import_private_key(sKeyPh, ED448_KEY_SIZE, pKeyPh,
  26016. sizeof(pKeyPh), &key) != 0) {
  26017. return -11601;
  26018. }
  26019. if (wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key, NULL,
  26020. 0) != 0) {
  26021. return -11602;
  26022. }
  26023. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  26024. return -11603;
  26025. #if defined(HAVE_ED448_VERIFY)
  26026. /* test verify on good msg */
  26027. if (wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  26028. NULL, 0) != 0 || verify != 1) {
  26029. return -11604;
  26030. }
  26031. #endif
  26032. if (wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  26033. contextPh2, sizeof(contextPh2)) != 0) {
  26034. return -11605;
  26035. }
  26036. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  26037. return -11606;
  26038. #if defined(HAVE_ED448_VERIFY)
  26039. /* test verify on good msg */
  26040. if (wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  26041. contextPh2, sizeof(contextPh2)) != 0 ||
  26042. verify != 1) {
  26043. return -11607;
  26044. }
  26045. #endif
  26046. if (wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key, NULL,
  26047. 0) != 0) {
  26048. return -11608;
  26049. }
  26050. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  26051. return -11609;
  26052. #if defined(HAVE_ED448_VERIFY)
  26053. if (wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  26054. &key, NULL, 0) != 0 || verify != 1) {
  26055. return -11610;
  26056. }
  26057. #endif
  26058. if (wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  26059. contextPh2, sizeof(contextPh2)) != 0) {
  26060. return -11611;
  26061. }
  26062. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  26063. return -11612;
  26064. #if defined(HAVE_ED448_VERIFY)
  26065. if (wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  26066. &key, contextPh2, sizeof(contextPh2)) != 0 ||
  26067. verify != 1) {
  26068. return -11613;
  26069. }
  26070. #endif
  26071. wc_ed448_free(&key);
  26072. return 0;
  26073. }
  26074. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  26075. WOLFSSL_TEST_SUBROUTINE int ed448_test(void)
  26076. {
  26077. int ret;
  26078. WC_RNG rng;
  26079. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  26080. defined(HAVE_ED448_KEY_IMPORT)
  26081. byte out[ED448_SIG_SIZE];
  26082. int i;
  26083. word32 outlen;
  26084. #ifdef HAVE_ED448_VERIFY
  26085. #ifdef WOLFSSL_ED448_STREAMING_VERIFY
  26086. int j;
  26087. #endif /* WOLFSSL_ED448_STREAMING_VERIFY */
  26088. int verify;
  26089. #endif /* HAVE_ED448_VERIFY */
  26090. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  26091. word32 keySz, sigSz;
  26092. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26093. ed448_key *key = NULL;
  26094. ed448_key *key2 = NULL;
  26095. #else
  26096. ed448_key key[1];
  26097. ed448_key key2[1];
  26098. #endif
  26099. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  26100. defined(HAVE_ED448_KEY_IMPORT)
  26101. /* test vectors from
  26102. https://tools.ietf.org/html/rfc8032
  26103. */
  26104. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  26105. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  26106. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  26107. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  26108. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  26109. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  26110. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  26111. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  26112. 0x5b
  26113. };
  26114. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  26115. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  26116. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  26117. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  26118. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  26119. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  26120. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  26121. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  26122. 0x4e
  26123. };
  26124. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  26125. 0x25, 0x8c, 0xdd, 0x4a, 0xda, 0x32, 0xed, 0x9c,
  26126. 0x9f, 0xf5, 0x4e, 0x63, 0x75, 0x6a, 0xe5, 0x82,
  26127. 0xfb, 0x8f, 0xab, 0x2a, 0xc7, 0x21, 0xf2, 0xc8,
  26128. 0xe6, 0x76, 0xa7, 0x27, 0x68, 0x51, 0x3d, 0x93,
  26129. 0x9f, 0x63, 0xdd, 0xdb, 0x55, 0x60, 0x91, 0x33,
  26130. 0xf2, 0x9a, 0xdf, 0x86, 0xec, 0x99, 0x29, 0xdc,
  26131. 0xcb, 0x52, 0xc1, 0xc5, 0xfd, 0x2f, 0xf7, 0xe2,
  26132. 0x1b
  26133. };
  26134. /* uncompressed test */
  26135. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  26136. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  26137. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  26138. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  26139. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  26140. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  26141. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  26142. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  26143. 0x5b
  26144. };
  26145. /* compressed prefix test */
  26146. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  26147. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  26148. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  26149. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  26150. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  26151. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  26152. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  26153. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  26154. 0x5b
  26155. };
  26156. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  26157. 0x87, 0x2d, 0x09, 0x37, 0x80, 0xf5, 0xd3, 0x73,
  26158. 0x0d, 0xf7, 0xc2, 0x12, 0x66, 0x4b, 0x37, 0xb8,
  26159. 0xa0, 0xf2, 0x4f, 0x56, 0x81, 0x0d, 0xaa, 0x83,
  26160. 0x82, 0xcd, 0x4f, 0xa3, 0xf7, 0x76, 0x34, 0xec,
  26161. 0x44, 0xdc, 0x54, 0xf1, 0xc2, 0xed, 0x9b, 0xea,
  26162. 0x86, 0xfa, 0xfb, 0x76, 0x32, 0xd8, 0xbe, 0x19,
  26163. 0x9e, 0xa1, 0x65, 0xf5, 0xad, 0x55, 0xdd, 0x9c,
  26164. 0xe8
  26165. };
  26166. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  26167. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  26168. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  26169. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  26170. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  26171. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  26172. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  26173. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  26174. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  26175. 0x80
  26176. };
  26177. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  26178. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  26179. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  26180. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  26181. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  26182. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  26183. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  26184. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  26185. 0x80
  26186. };
  26187. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  26188. 0x3b, 0xa1, 0x6d, 0xa0, 0xc6, 0xf2, 0xcc, 0x1f,
  26189. 0x30, 0x18, 0x77, 0x40, 0x75, 0x6f, 0x5e, 0x79,
  26190. 0x8d, 0x6b, 0xc5, 0xfc, 0x01, 0x5d, 0x7c, 0x63,
  26191. 0xcc, 0x95, 0x10, 0xee, 0x3f, 0xd4, 0x4a, 0xdc,
  26192. 0x24, 0xd8, 0xe9, 0x68, 0xb6, 0xe4, 0x6e, 0x6f,
  26193. 0x94, 0xd1, 0x9b, 0x94, 0x53, 0x61, 0x72, 0x6b,
  26194. 0xd7, 0x5e, 0x14, 0x9e, 0xf0, 0x98, 0x17, 0xf5,
  26195. 0x80
  26196. };
  26197. /* uncompressed test */
  26198. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  26199. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  26200. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  26201. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  26202. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  26203. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  26204. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  26205. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  26206. 0x80
  26207. };
  26208. /* compressed prefix */
  26209. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  26210. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  26211. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  26212. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  26213. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  26214. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  26215. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  26216. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  26217. 0x80
  26218. };
  26219. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  26220. 0xa8, 0x1b, 0x2e, 0x8a, 0x70, 0xa5, 0xac, 0x94,
  26221. 0xff, 0xdb, 0xcc, 0x9b, 0xad, 0xfc, 0x3f, 0xeb,
  26222. 0x08, 0x01, 0xf2, 0x58, 0x57, 0x8b, 0xb1, 0x14,
  26223. 0xad, 0x44, 0xec, 0xe1, 0xec, 0x0e, 0x79, 0x9d,
  26224. 0xa0, 0x8e, 0xff, 0xb8, 0x1c, 0x5d, 0x68, 0x5c,
  26225. 0x0c, 0x56, 0xf6, 0x4e, 0xec, 0xae, 0xf8, 0xcd,
  26226. 0xf1, 0x1c, 0xc3, 0x87, 0x37, 0x83, 0x8c, 0xf4,
  26227. 0x00
  26228. };
  26229. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  26230. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  26231. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  26232. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  26233. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  26234. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  26235. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  26236. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  26237. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  26238. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  26239. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  26240. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  26241. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  26242. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  26243. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  26244. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  26245. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  26246. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  26247. 0x26, 0x00
  26248. };
  26249. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  26250. 0x26, 0xb8, 0xf9, 0x17, 0x27, 0xbd, 0x62, 0x89,
  26251. 0x7a, 0xf1, 0x5e, 0x41, 0xeb, 0x43, 0xc3, 0x77,
  26252. 0xef, 0xb9, 0xc6, 0x10, 0xd4, 0x8f, 0x23, 0x35,
  26253. 0xcb, 0x0b, 0xd0, 0x08, 0x78, 0x10, 0xf4, 0x35,
  26254. 0x25, 0x41, 0xb1, 0x43, 0xc4, 0xb9, 0x81, 0xb7,
  26255. 0xe1, 0x8f, 0x62, 0xde, 0x8c, 0xcd, 0xf6, 0x33,
  26256. 0xfc, 0x1b, 0xf0, 0x37, 0xab, 0x7c, 0xd7, 0x79,
  26257. 0x80, 0x5e, 0x0d, 0xbc, 0xc0, 0xaa, 0xe1, 0xcb,
  26258. 0xce, 0xe1, 0xaf, 0xb2, 0xe0, 0x27, 0xdf, 0x36,
  26259. 0xbc, 0x04, 0xdc, 0xec, 0xbf, 0x15, 0x43, 0x36,
  26260. 0xc1, 0x9f, 0x0a, 0xf7, 0xe0, 0xa6, 0x47, 0x29,
  26261. 0x05, 0xe7, 0x99, 0xf1, 0x95, 0x3d, 0x2a, 0x0f,
  26262. 0xf3, 0x34, 0x8a, 0xb2, 0x1a, 0xa4, 0xad, 0xaf,
  26263. 0xd1, 0xd2, 0x34, 0x44, 0x1c, 0xf8, 0x07, 0xc0,
  26264. 0x3a, 0x00
  26265. };
  26266. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  26267. 0x7e, 0xee, 0xab, 0x7c, 0x4e, 0x50, 0xfb, 0x79,
  26268. 0x9b, 0x41, 0x8e, 0xe5, 0xe3, 0x19, 0x7f, 0xf6,
  26269. 0xbf, 0x15, 0xd4, 0x3a, 0x14, 0xc3, 0x43, 0x89,
  26270. 0xb5, 0x9d, 0xd1, 0xa7, 0xb1, 0xb8, 0x5b, 0x4a,
  26271. 0xe9, 0x04, 0x38, 0xac, 0xa6, 0x34, 0xbe, 0xa4,
  26272. 0x5e, 0x3a, 0x26, 0x95, 0xf1, 0x27, 0x0f, 0x07,
  26273. 0xfd, 0xcd, 0xf7, 0xc6, 0x2b, 0x8e, 0xfe, 0xaf,
  26274. 0x00, 0xb4, 0x5c, 0x2c, 0x96, 0xba, 0x45, 0x7e,
  26275. 0xb1, 0xa8, 0xbf, 0x07, 0x5a, 0x3d, 0xb2, 0x8e,
  26276. 0x5c, 0x24, 0xf6, 0xb9, 0x23, 0xed, 0x4a, 0xd7,
  26277. 0x47, 0xc3, 0xc9, 0xe0, 0x3c, 0x70, 0x79, 0xef,
  26278. 0xb8, 0x7c, 0xb1, 0x10, 0xd3, 0xa9, 0x98, 0x61,
  26279. 0xe7, 0x20, 0x03, 0xcb, 0xae, 0x6d, 0x6b, 0x8b,
  26280. 0x82, 0x7e, 0x4e, 0x6c, 0x14, 0x30, 0x64, 0xff,
  26281. 0x3c, 0x00
  26282. };
  26283. /* uncompressed test */
  26284. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  26285. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  26286. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  26287. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  26288. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  26289. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  26290. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  26291. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  26292. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  26293. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  26294. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  26295. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  26296. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  26297. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  26298. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  26299. 0x26, 0x00
  26300. };
  26301. /* compressed prefix */
  26302. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  26303. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  26304. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  26305. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  26306. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  26307. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  26308. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  26309. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  26310. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  26311. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  26312. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  26313. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  26314. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  26315. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  26316. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  26317. 0x26, 0x00
  26318. };
  26319. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  26320. 0xe3, 0x01, 0x34, 0x5a, 0x41, 0xa3, 0x9a, 0x4d,
  26321. 0x72, 0xff, 0xf8, 0xdf, 0x69, 0xc9, 0x80, 0x75,
  26322. 0xa0, 0xcc, 0x08, 0x2b, 0x80, 0x2f, 0xc9, 0xb2,
  26323. 0xb6, 0xbc, 0x50, 0x3f, 0x92, 0x6b, 0x65, 0xbd,
  26324. 0xdf, 0x7f, 0x4c, 0x8f, 0x1c, 0xb4, 0x9f, 0x63,
  26325. 0x96, 0xaf, 0xc8, 0xa7, 0x0a, 0xbe, 0x6d, 0x8a,
  26326. 0xef, 0x0d, 0xb4, 0x78, 0xd4, 0xc6, 0xb2, 0x97,
  26327. 0x00, 0x76, 0xc6, 0xa0, 0x48, 0x4f, 0xe7, 0x6d,
  26328. 0x76, 0xb3, 0xa9, 0x76, 0x25, 0xd7, 0x9f, 0x1c,
  26329. 0xe2, 0x40, 0xe7, 0xc5, 0x76, 0x75, 0x0d, 0x29,
  26330. 0x55, 0x28, 0x28, 0x6f, 0x71, 0x9b, 0x41, 0x3d,
  26331. 0xe9, 0xad, 0xa3, 0xe8, 0xeb, 0x78, 0xed, 0x57,
  26332. 0x36, 0x03, 0xce, 0x30, 0xd8, 0xbb, 0x76, 0x17,
  26333. 0x85, 0xdc, 0x30, 0xdb, 0xc3, 0x20, 0x86, 0x9e,
  26334. 0x1a, 0x00
  26335. };
  26336. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  26337. #define SIGSZ sizeof(sig1)
  26338. PEDANTIC_EXTENSION WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = { };
  26339. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = { 0x03 };
  26340. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = { 0x64, 0xa6, 0x5f, 0x3c, 0xde, 0xdc, 0xdd,
  26341. 0x66, 0x81, 0x1e, 0x29, 0x15 };
  26342. /* test of a 1023 byte long message */
  26343. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  26344. 0x6d, 0xdf, 0x80, 0x2e, 0x1a, 0xae, 0x49, 0x86,
  26345. 0x93, 0x5f, 0x7f, 0x98, 0x1b, 0xa3, 0xf0, 0x35,
  26346. 0x1d, 0x62, 0x73, 0xc0, 0xa0, 0xc2, 0x2c, 0x9c,
  26347. 0x0e, 0x83, 0x39, 0x16, 0x8e, 0x67, 0x54, 0x12,
  26348. 0xa3, 0xde, 0xbf, 0xaf, 0x43, 0x5e, 0xd6, 0x51,
  26349. 0x55, 0x80, 0x07, 0xdb, 0x43, 0x84, 0xb6, 0x50,
  26350. 0xfc, 0xc0, 0x7e, 0x3b, 0x58, 0x6a, 0x27, 0xa4,
  26351. 0xf7, 0xa0, 0x0a, 0xc8, 0xa6, 0xfe, 0xc2, 0xcd,
  26352. 0x86, 0xae, 0x4b, 0xf1, 0x57, 0x0c, 0x41, 0xe6,
  26353. 0xa4, 0x0c, 0x93, 0x1d, 0xb2, 0x7b, 0x2f, 0xaa,
  26354. 0x15, 0xa8, 0xce, 0xdd, 0x52, 0xcf, 0xf7, 0x36,
  26355. 0x2c, 0x4e, 0x6e, 0x23, 0xda, 0xec, 0x0f, 0xbc,
  26356. 0x3a, 0x79, 0xb6, 0x80, 0x6e, 0x31, 0x6e, 0xfc,
  26357. 0xc7, 0xb6, 0x81, 0x19, 0xbf, 0x46, 0xbc, 0x76,
  26358. 0xa2, 0x60, 0x67, 0xa5, 0x3f, 0x29, 0x6d, 0xaf,
  26359. 0xdb, 0xdc, 0x11, 0xc7, 0x7f, 0x77, 0x77, 0xe9,
  26360. 0x72, 0x66, 0x0c, 0xf4, 0xb6, 0xa9, 0xb3, 0x69,
  26361. 0xa6, 0x66, 0x5f, 0x02, 0xe0, 0xcc, 0x9b, 0x6e,
  26362. 0xdf, 0xad, 0x13, 0x6b, 0x4f, 0xab, 0xe7, 0x23,
  26363. 0xd2, 0x81, 0x3d, 0xb3, 0x13, 0x6c, 0xfd, 0xe9,
  26364. 0xb6, 0xd0, 0x44, 0x32, 0x2f, 0xee, 0x29, 0x47,
  26365. 0x95, 0x2e, 0x03, 0x1b, 0x73, 0xab, 0x5c, 0x60,
  26366. 0x33, 0x49, 0xb3, 0x07, 0xbd, 0xc2, 0x7b, 0xc6,
  26367. 0xcb, 0x8b, 0x8b, 0xbd, 0x7b, 0xd3, 0x23, 0x21,
  26368. 0x9b, 0x80, 0x33, 0xa5, 0x81, 0xb5, 0x9e, 0xad,
  26369. 0xeb, 0xb0, 0x9b, 0x3c, 0x4f, 0x3d, 0x22, 0x77,
  26370. 0xd4, 0xf0, 0x34, 0x36, 0x24, 0xac, 0xc8, 0x17,
  26371. 0x80, 0x47, 0x28, 0xb2, 0x5a, 0xb7, 0x97, 0x17,
  26372. 0x2b, 0x4c, 0x5c, 0x21, 0xa2, 0x2f, 0x9c, 0x78,
  26373. 0x39, 0xd6, 0x43, 0x00, 0x23, 0x2e, 0xb6, 0x6e,
  26374. 0x53, 0xf3, 0x1c, 0x72, 0x3f, 0xa3, 0x7f, 0xe3,
  26375. 0x87, 0xc7, 0xd3, 0xe5, 0x0b, 0xdf, 0x98, 0x13,
  26376. 0xa3, 0x0e, 0x5b, 0xb1, 0x2c, 0xf4, 0xcd, 0x93,
  26377. 0x0c, 0x40, 0xcf, 0xb4, 0xe1, 0xfc, 0x62, 0x25,
  26378. 0x92, 0xa4, 0x95, 0x88, 0x79, 0x44, 0x94, 0xd5,
  26379. 0x6d, 0x24, 0xea, 0x4b, 0x40, 0xc8, 0x9f, 0xc0,
  26380. 0x59, 0x6c, 0xc9, 0xeb, 0xb9, 0x61, 0xc8, 0xcb,
  26381. 0x10, 0xad, 0xde, 0x97, 0x6a, 0x5d, 0x60, 0x2b,
  26382. 0x1c, 0x3f, 0x85, 0xb9, 0xb9, 0xa0, 0x01, 0xed,
  26383. 0x3c, 0x6a, 0x4d, 0x3b, 0x14, 0x37, 0xf5, 0x20,
  26384. 0x96, 0xcd, 0x19, 0x56, 0xd0, 0x42, 0xa5, 0x97,
  26385. 0xd5, 0x61, 0xa5, 0x96, 0xec, 0xd3, 0xd1, 0x73,
  26386. 0x5a, 0x8d, 0x57, 0x0e, 0xa0, 0xec, 0x27, 0x22,
  26387. 0x5a, 0x2c, 0x4a, 0xaf, 0xf2, 0x63, 0x06, 0xd1,
  26388. 0x52, 0x6c, 0x1a, 0xf3, 0xca, 0x6d, 0x9c, 0xf5,
  26389. 0xa2, 0xc9, 0x8f, 0x47, 0xe1, 0xc4, 0x6d, 0xb9,
  26390. 0xa3, 0x32, 0x34, 0xcf, 0xd4, 0xd8, 0x1f, 0x2c,
  26391. 0x98, 0x53, 0x8a, 0x09, 0xeb, 0xe7, 0x69, 0x98,
  26392. 0xd0, 0xd8, 0xfd, 0x25, 0x99, 0x7c, 0x7d, 0x25,
  26393. 0x5c, 0x6d, 0x66, 0xec, 0xe6, 0xfa, 0x56, 0xf1,
  26394. 0x11, 0x44, 0x95, 0x0f, 0x02, 0x77, 0x95, 0xe6,
  26395. 0x53, 0x00, 0x8f, 0x4b, 0xd7, 0xca, 0x2d, 0xee,
  26396. 0x85, 0xd8, 0xe9, 0x0f, 0x3d, 0xc3, 0x15, 0x13,
  26397. 0x0c, 0xe2, 0xa0, 0x03, 0x75, 0xa3, 0x18, 0xc7,
  26398. 0xc3, 0xd9, 0x7b, 0xe2, 0xc8, 0xce, 0x5b, 0x6d,
  26399. 0xb4, 0x1a, 0x62, 0x54, 0xff, 0x26, 0x4f, 0xa6,
  26400. 0x15, 0x5b, 0xae, 0xe3, 0xb0, 0x77, 0x3c, 0x0f,
  26401. 0x49, 0x7c, 0x57, 0x3f, 0x19, 0xbb, 0x4f, 0x42,
  26402. 0x40, 0x28, 0x1f, 0x0b, 0x1f, 0x4f, 0x7b, 0xe8,
  26403. 0x57, 0xa4, 0xe5, 0x9d, 0x41, 0x6c, 0x06, 0xb4,
  26404. 0xc5, 0x0f, 0xa0, 0x9e, 0x18, 0x10, 0xdd, 0xc6,
  26405. 0xb1, 0x46, 0x7b, 0xae, 0xac, 0x5a, 0x36, 0x68,
  26406. 0xd1, 0x1b, 0x6e, 0xca, 0xa9, 0x01, 0x44, 0x00,
  26407. 0x16, 0xf3, 0x89, 0xf8, 0x0a, 0xcc, 0x4d, 0xb9,
  26408. 0x77, 0x02, 0x5e, 0x7f, 0x59, 0x24, 0x38, 0x8c,
  26409. 0x7e, 0x34, 0x0a, 0x73, 0x2e, 0x55, 0x44, 0x40,
  26410. 0xe7, 0x65, 0x70, 0xf8, 0xdd, 0x71, 0xb7, 0xd6,
  26411. 0x40, 0xb3, 0x45, 0x0d, 0x1f, 0xd5, 0xf0, 0x41,
  26412. 0x0a, 0x18, 0xf9, 0xa3, 0x49, 0x4f, 0x70, 0x7c,
  26413. 0x71, 0x7b, 0x79, 0xb4, 0xbf, 0x75, 0xc9, 0x84,
  26414. 0x00, 0xb0, 0x96, 0xb2, 0x16, 0x53, 0xb5, 0xd2,
  26415. 0x17, 0xcf, 0x35, 0x65, 0xc9, 0x59, 0x74, 0x56,
  26416. 0xf7, 0x07, 0x03, 0x49, 0x7a, 0x07, 0x87, 0x63,
  26417. 0x82, 0x9b, 0xc0, 0x1b, 0xb1, 0xcb, 0xc8, 0xfa,
  26418. 0x04, 0xea, 0xdc, 0x9a, 0x6e, 0x3f, 0x66, 0x99,
  26419. 0x58, 0x7a, 0x9e, 0x75, 0xc9, 0x4e, 0x5b, 0xab,
  26420. 0x00, 0x36, 0xe0, 0xb2, 0xe7, 0x11, 0x39, 0x2c,
  26421. 0xff, 0x00, 0x47, 0xd0, 0xd6, 0xb0, 0x5b, 0xd2,
  26422. 0xa5, 0x88, 0xbc, 0x10, 0x97, 0x18, 0x95, 0x42,
  26423. 0x59, 0xf1, 0xd8, 0x66, 0x78, 0xa5, 0x79, 0xa3,
  26424. 0x12, 0x0f, 0x19, 0xcf, 0xb2, 0x96, 0x3f, 0x17,
  26425. 0x7a, 0xeb, 0x70, 0xf2, 0xd4, 0x84, 0x48, 0x26,
  26426. 0x26, 0x2e, 0x51, 0xb8, 0x02, 0x71, 0x27, 0x20,
  26427. 0x68, 0xef, 0x5b, 0x38, 0x56, 0xfa, 0x85, 0x35,
  26428. 0xaa, 0x2a, 0x88, 0xb2, 0xd4, 0x1f, 0x2a, 0x0e,
  26429. 0x2f, 0xda, 0x76, 0x24, 0xc2, 0x85, 0x02, 0x72,
  26430. 0xac, 0x4a, 0x2f, 0x56, 0x1f, 0x8f, 0x2f, 0x7a,
  26431. 0x31, 0x8b, 0xfd, 0x5c, 0xaf, 0x96, 0x96, 0x14,
  26432. 0x9e, 0x4a, 0xc8, 0x24, 0xad, 0x34, 0x60, 0x53,
  26433. 0x8f, 0xdc, 0x25, 0x42, 0x1b, 0xee, 0xc2, 0xcc,
  26434. 0x68, 0x18, 0x16, 0x2d, 0x06, 0xbb, 0xed, 0x0c,
  26435. 0x40, 0xa3, 0x87, 0x19, 0x23, 0x49, 0xdb, 0x67,
  26436. 0xa1, 0x18, 0xba, 0xda, 0x6c, 0xd5, 0xab, 0x01,
  26437. 0x40, 0xee, 0x27, 0x32, 0x04, 0xf6, 0x28, 0xaa,
  26438. 0xd1, 0xc1, 0x35, 0xf7, 0x70, 0x27, 0x9a, 0x65,
  26439. 0x1e, 0x24, 0xd8, 0xc1, 0x4d, 0x75, 0xa6, 0x05,
  26440. 0x9d, 0x76, 0xb9, 0x6a, 0x6f, 0xd8, 0x57, 0xde,
  26441. 0xf5, 0xe0, 0xb3, 0x54, 0xb2, 0x7a, 0xb9, 0x37,
  26442. 0xa5, 0x81, 0x5d, 0x16, 0xb5, 0xfa, 0xe4, 0x07,
  26443. 0xff, 0x18, 0x22, 0x2c, 0x6d, 0x1e, 0xd2, 0x63,
  26444. 0xbe, 0x68, 0xc9, 0x5f, 0x32, 0xd9, 0x08, 0xbd,
  26445. 0x89, 0x5c, 0xd7, 0x62, 0x07, 0xae, 0x72, 0x64,
  26446. 0x87, 0x56, 0x7f, 0x9a, 0x67, 0xda, 0xd7, 0x9a,
  26447. 0xbe, 0xc3, 0x16, 0xf6, 0x83, 0xb1, 0x7f, 0x2d,
  26448. 0x02, 0xbf, 0x07, 0xe0, 0xac, 0x8b, 0x5b, 0xc6,
  26449. 0x16, 0x2c, 0xf9, 0x46, 0x97, 0xb3, 0xc2, 0x7c,
  26450. 0xd1, 0xfe, 0xa4, 0x9b, 0x27, 0xf2, 0x3b, 0xa2,
  26451. 0x90, 0x18, 0x71, 0x96, 0x25, 0x06, 0x52, 0x0c,
  26452. 0x39, 0x2d, 0xa8, 0xb6, 0xad, 0x0d, 0x99, 0xf7,
  26453. 0x01, 0x3f, 0xbc, 0x06, 0xc2, 0xc1, 0x7a, 0x56,
  26454. 0x95, 0x00, 0xc8, 0xa7, 0x69, 0x64, 0x81, 0xc1,
  26455. 0xcd, 0x33, 0xe9, 0xb1, 0x4e, 0x40, 0xb8, 0x2e,
  26456. 0x79, 0xa5, 0xf5, 0xdb, 0x82, 0x57, 0x1b, 0xa9,
  26457. 0x7b, 0xae, 0x3a, 0xd3, 0xe0, 0x47, 0x95, 0x15,
  26458. 0xbb, 0x0e, 0x2b, 0x0f, 0x3b, 0xfc, 0xd1, 0xfd,
  26459. 0x33, 0x03, 0x4e, 0xfc, 0x62, 0x45, 0xed, 0xdd,
  26460. 0x7e, 0xe2, 0x08, 0x6d, 0xda, 0xe2, 0x60, 0x0d,
  26461. 0x8c, 0xa7, 0x3e, 0x21, 0x4e, 0x8c, 0x2b, 0x0b,
  26462. 0xdb, 0x2b, 0x04, 0x7c, 0x6a, 0x46, 0x4a, 0x56,
  26463. 0x2e, 0xd7, 0x7b, 0x73, 0xd2, 0xd8, 0x41, 0xc4,
  26464. 0xb3, 0x49, 0x73, 0x55, 0x12, 0x57, 0x71, 0x3b,
  26465. 0x75, 0x36, 0x32, 0xef, 0xba, 0x34, 0x81, 0x69,
  26466. 0xab, 0xc9, 0x0a, 0x68, 0xf4, 0x26, 0x11, 0xa4,
  26467. 0x01, 0x26, 0xd7, 0xcb, 0x21, 0xb5, 0x86, 0x95,
  26468. 0x56, 0x81, 0x86, 0xf7, 0xe5, 0x69, 0xd2, 0xff,
  26469. 0x0f, 0x9e, 0x74, 0x5d, 0x04, 0x87, 0xdd, 0x2e,
  26470. 0xb9, 0x97, 0xca, 0xfc, 0x5a, 0xbf, 0x9d, 0xd1,
  26471. 0x02, 0xe6, 0x2f, 0xf6, 0x6c, 0xba, 0x87
  26472. };
  26473. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  26474. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  26475. sizeof(msg2),
  26476. sizeof(msg3),
  26477. 0 /*sizeof(msg1)*/,
  26478. 0 /*sizeof(msg1)*/,
  26479. sizeof(msg4)
  26480. };
  26481. #ifndef NO_ASN
  26482. static const byte privateEd448[] = {
  26483. 0x30, 0x47, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06,
  26484. 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04, 0x39,
  26485. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  26486. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  26487. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  26488. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  26489. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  26490. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  26491. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  26492. 0x5b
  26493. };
  26494. static const byte publicEd448[] = {
  26495. 0x30, 0x43, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65,
  26496. 0x71, 0x03, 0x3a, 0x00, 0x5f, 0xd7, 0x44, 0x9b,
  26497. 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec,
  26498. 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  26499. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d,
  26500. 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76,
  26501. 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d,
  26502. 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe,
  26503. 0xaf, 0xe8, 0x25, 0x61, 0x80
  26504. };
  26505. static const byte privPubEd448[] = {
  26506. 0x30, 0x81, 0x82, 0x02, 0x01, 0x00, 0x30, 0x05,
  26507. 0x06, 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04,
  26508. 0x39, 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d,
  26509. 0x10, 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e,
  26510. 0xbf, 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c,
  26511. 0x9f, 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48,
  26512. 0xa3, 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04,
  26513. 0x4e, 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f,
  26514. 0x8f, 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98,
  26515. 0xf9, 0x5b, 0x81, 0x39, 0x5f, 0xd7, 0x44, 0x9b,
  26516. 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec,
  26517. 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  26518. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d,
  26519. 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76,
  26520. 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d,
  26521. 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe,
  26522. 0xaf, 0xe8, 0x25, 0x61, 0x80
  26523. };
  26524. word32 idx;
  26525. #endif /* NO_ASN */
  26526. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  26527. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  26528. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26529. ed448_key *key3 = NULL;
  26530. #else
  26531. ed448_key key3[1];
  26532. #endif
  26533. #endif
  26534. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26535. key = (ed448_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26536. key2 = (ed448_key *)XMALLOC(sizeof(*key2), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26537. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  26538. key3 = (ed448_key *)XMALLOC(sizeof(*key3), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26539. #endif
  26540. #endif
  26541. /* create ed448 keys */
  26542. #ifndef HAVE_FIPS
  26543. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  26544. #else
  26545. ret = wc_InitRng(&rng);
  26546. #endif
  26547. if (ret != 0) {
  26548. XMEMSET(&rng, 0, sizeof(rng));
  26549. ERROR_OUT(-11700, out);
  26550. }
  26551. if (wc_ed448_init(key) < 0)
  26552. ERROR_OUT(-11903, out);
  26553. if (wc_ed448_init(key2) < 0)
  26554. ERROR_OUT(-11904, out);
  26555. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  26556. if (wc_ed448_init(key3) < 0)
  26557. ERROR_OUT(-11905, out);
  26558. #endif
  26559. if (wc_ed448_make_key(&rng, ED448_KEY_SIZE, key) < 0)
  26560. ERROR_OUT(-11906, out);
  26561. if (wc_ed448_make_key(&rng, ED448_KEY_SIZE, key2) < 0)
  26562. ERROR_OUT(-11907, out);
  26563. /* helper functions for signature and key size */
  26564. keySz = wc_ed448_size(key);
  26565. sigSz = wc_ed448_sig_size(key);
  26566. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  26567. defined(HAVE_ED448_KEY_IMPORT)
  26568. for (i = 0; i < 6; i++) {
  26569. outlen = sizeof(out);
  26570. XMEMSET(out, 0, sizeof(out));
  26571. if (wc_ed448_import_private_key(sKeys[i], ED448_KEY_SIZE, pKeys[i],
  26572. pKeySz[i], key) != 0)
  26573. ERROR_OUT(-11701 - i, out);
  26574. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, key, NULL,
  26575. 0) != 0)
  26576. ERROR_OUT(-11711 - i, out);
  26577. if (XMEMCMP(out, sigs[i], 114))
  26578. ERROR_OUT(-11721 - i, out);
  26579. #if defined(HAVE_ED448_VERIFY)
  26580. /* test verify on good msg */
  26581. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key,
  26582. NULL, 0) != 0 || verify != 1)
  26583. ERROR_OUT(-11731 - i, out);
  26584. #ifdef WOLFSSL_ED448_STREAMING_VERIFY
  26585. /* test verify on good msg using streaming interface directly */
  26586. if (wc_ed448_verify_msg_init(out, outlen,
  26587. key, (byte)Ed448, NULL, 0) != 0)
  26588. ERROR_OUT(-11911 - i, out);
  26589. for (j = 0; j < msgSz[i]; j += i) {
  26590. if (wc_ed448_verify_msg_update(msgs[i] + j, MIN(i, msgSz[i] - j), key) != 0)
  26591. ERROR_OUT(-11921 - i, out);
  26592. }
  26593. if (wc_ed448_verify_msg_final(out, outlen, &verify,
  26594. key) != 0 || verify != 1)
  26595. ERROR_OUT(-11931 - i, out);
  26596. #endif /* WOLFSSL_ED448_STREAMING_VERIFY */
  26597. /* test verify on bad msg */
  26598. out[outlen-2] = out[outlen-2] + 1;
  26599. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key,
  26600. NULL, 0) == 0 || verify == 1)
  26601. ERROR_OUT(-11741 - i, out);
  26602. #endif /* HAVE_ED448_VERIFY */
  26603. /* test api for import/exporting keys */
  26604. {
  26605. byte *exportPKey = NULL;
  26606. byte *exportSKey = NULL;
  26607. word32 exportPSz = ED448_KEY_SIZE;
  26608. word32 exportSSz = ED448_KEY_SIZE;
  26609. exportPKey = (byte *)XMALLOC(exportPSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26610. exportSKey = (byte *)XMALLOC(exportSSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26611. if ((exportPKey == NULL) || (exportSKey == NULL))
  26612. ERROR_OUT(-11902, out);
  26613. ret = 0;
  26614. do {
  26615. if (wc_ed448_export_public(key, exportPKey, &exportPSz) != 0) {
  26616. ret = -11751 - i;
  26617. break;
  26618. }
  26619. if (wc_ed448_import_public_ex(exportPKey, exportPSz, key2, 1) != 0) {
  26620. ret = -11761 - i;
  26621. break;
  26622. }
  26623. if (wc_ed448_export_private_only(key, exportSKey, &exportSSz) != 0) {
  26624. ret = -11771 - i;
  26625. break;
  26626. }
  26627. if (wc_ed448_import_private_key(exportSKey, exportSSz,
  26628. exportPKey, exportPSz, key2) != 0) {
  26629. ret = -11781 - i;
  26630. break;
  26631. }
  26632. /* clear "out" buffer and test sign with imported keys */
  26633. outlen = sizeof(out);
  26634. XMEMSET(out, 0, sizeof(out));
  26635. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, key2, NULL,
  26636. 0) != 0) {
  26637. ret = -11791 - i;
  26638. break;
  26639. }
  26640. } while(0);
  26641. XFREE(exportPKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26642. XFREE(exportSKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26643. if (ret != 0)
  26644. goto out;
  26645. }
  26646. #if defined(HAVE_ED448_VERIFY)
  26647. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key2,
  26648. NULL, 0) != 0 || verify != 1)
  26649. ERROR_OUT(-11801 - i, out);
  26650. if (XMEMCMP(out, sigs[i], SIGSZ))
  26651. ERROR_OUT(-11811 - i, out);
  26652. #endif /* HAVE_ED448_VERIFY */
  26653. }
  26654. ret = ed448_ctx_test();
  26655. if (ret != 0)
  26656. goto out;
  26657. ret = ed448ph_test();
  26658. if (ret != 0)
  26659. goto out;
  26660. #ifndef NO_ASN
  26661. /* Try ASN.1 encoded private-only key and public key. */
  26662. idx = 0;
  26663. if (wc_Ed448PrivateKeyDecode(privateEd448, &idx, key3,
  26664. sizeof(privateEd448)) != 0)
  26665. ERROR_OUT(-11821, out);
  26666. if (wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0)
  26667. != BAD_FUNC_ARG)
  26668. ERROR_OUT(-11831, out);
  26669. idx = 0;
  26670. if (wc_Ed448PublicKeyDecode(publicEd448, &idx, key3,
  26671. sizeof(publicEd448)) != 0)
  26672. ERROR_OUT(-11841, out);
  26673. if (wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0) != 0)
  26674. ERROR_OUT(-11851, out);
  26675. if (XMEMCMP(out, sigs[0], SIGSZ))
  26676. ERROR_OUT(-11861, out);
  26677. #if defined(HAVE_ED448_VERIFY)
  26678. /* test verify on good msg */
  26679. if (wc_ed448_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, key3,
  26680. NULL, 0) != 0 || verify != 1)
  26681. ERROR_OUT(-11871, out);
  26682. #endif /* HAVE_ED448_VERIFY */
  26683. wc_ed448_free(key3);
  26684. if (wc_ed448_init(key3) < 0)
  26685. ERROR_OUT(-11908, out);
  26686. idx = 0;
  26687. if (wc_Ed448PrivateKeyDecode(privPubEd448, &idx, key3,
  26688. sizeof(privPubEd448)) != 0)
  26689. ERROR_OUT(-11881, out);
  26690. if (wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0) != 0)
  26691. ERROR_OUT(-11891, out);
  26692. if (XMEMCMP(out, sigs[0], SIGSZ))
  26693. ERROR_OUT(-11901, out);
  26694. #endif /* NO_ASN */
  26695. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  26696. ret = 0;
  26697. out:
  26698. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26699. if (key) {
  26700. wc_ed448_free(key);
  26701. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26702. }
  26703. if (key2) {
  26704. wc_ed448_free(key2);
  26705. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26706. }
  26707. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  26708. if (key3) {
  26709. wc_ed448_free(key3);
  26710. XFREE(key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26711. }
  26712. #endif
  26713. #else
  26714. wc_ed448_free(key);
  26715. wc_ed448_free(key2);
  26716. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  26717. wc_ed448_free(key3);
  26718. #endif
  26719. #endif
  26720. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  26721. wc_FreeRng(&rng);
  26722. #endif
  26723. if (ret < 0)
  26724. return ret;
  26725. /* hush warnings of unused keySz and sigSz */
  26726. (void)keySz;
  26727. (void)sigSz;
  26728. #ifdef WOLFSSL_TEST_CERT
  26729. ret = ed448_test_cert();
  26730. if (ret < 0)
  26731. return ret;
  26732. #ifdef WOLFSSL_CERT_GEN
  26733. ret = ed448_test_make_cert();
  26734. if (ret < 0)
  26735. return ret;
  26736. #endif /* WOLFSSL_CERT_GEN */
  26737. #endif /* WOLFSSL_TEST_CERT */
  26738. return 0;
  26739. }
  26740. #endif /* HAVE_ED448 */
  26741. #ifdef WOLFSSL_HAVE_KYBER
  26742. #ifdef WOLFSSL_WC_KYBER /* OQS and PQM4 do not support KATs */
  26743. #ifdef WOLFSSL_KYBER512
  26744. static int kyber512_kat(void)
  26745. {
  26746. KyberKey key;
  26747. int ret;
  26748. byte priv[KYBER512_PRIVATE_KEY_SIZE];
  26749. byte pub[KYBER512_PUBLIC_KEY_SIZE];
  26750. byte ct[KYBER512_CIPHER_TEXT_SIZE];
  26751. byte ss[KYBER_SS_SZ];
  26752. byte ss_dec[KYBER_SS_SZ];
  26753. const byte kyber512_rand[] = {
  26754. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  26755. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  26756. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  26757. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  26758. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  26759. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  26760. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  26761. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  26762. };
  26763. const byte kyber512enc_rand[] = {
  26764. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  26765. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  26766. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  26767. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  26768. };
  26769. const byte kyber512_pk[] = {
  26770. 0x11, 0x5A, 0xCE, 0x0E, 0x64, 0x67, 0x7C, 0xBB,
  26771. 0x7D, 0xCF, 0xC9, 0x3C, 0x16, 0xD3, 0xA3, 0x05,
  26772. 0xF6, 0x76, 0x15, 0xA4, 0x88, 0xD7, 0x11, 0xAA,
  26773. 0x56, 0x69, 0x8C, 0x56, 0x63, 0xAB, 0x7A, 0xC9,
  26774. 0xCE, 0x66, 0xD5, 0x47, 0xC0, 0x59, 0x5F, 0x98,
  26775. 0xA4, 0x3F, 0x46, 0x50, 0xBB, 0xE0, 0x8C, 0x36,
  26776. 0x4D, 0x97, 0x67, 0x89, 0x11, 0x7D, 0x34, 0xF6,
  26777. 0xAE, 0x51, 0xAC, 0x06, 0x3C, 0xB5, 0x5C, 0x6C,
  26778. 0xA3, 0x25, 0x58, 0x22, 0x7D, 0xFE, 0xF8, 0x07,
  26779. 0xD1, 0x9C, 0x30, 0xDE, 0x41, 0x44, 0x24, 0x09,
  26780. 0x7F, 0x6A, 0xA2, 0x36, 0xA1, 0x05, 0x3B, 0x4A,
  26781. 0x07, 0xA7, 0x6B, 0xE3, 0x72, 0xA5, 0xC6, 0xB6,
  26782. 0x00, 0x27, 0x91, 0xEB, 0xE0, 0xAF, 0xDA, 0xF5,
  26783. 0x4E, 0x1C, 0xA2, 0x37, 0xFF, 0x54, 0x5B, 0xA6,
  26784. 0x83, 0x43, 0xE7, 0x45, 0xC0, 0x4A, 0xD1, 0x63,
  26785. 0x9D, 0xBC, 0x59, 0x03, 0x46, 0xB6, 0xB9, 0x56,
  26786. 0x9B, 0x56, 0xDB, 0xBF, 0xE5, 0x31, 0x51, 0x91,
  26787. 0x30, 0x66, 0xE5, 0xC8, 0x55, 0x27, 0xDC, 0x94,
  26788. 0x68, 0x11, 0x0A, 0x13, 0x6A, 0x41, 0x14, 0x97,
  26789. 0xC2, 0x27, 0xDC, 0xB8, 0xC9, 0xB2, 0x55, 0x70,
  26790. 0xB7, 0xA0, 0xE4, 0x2A, 0xAD, 0xA6, 0x70, 0x9F,
  26791. 0x23, 0x20, 0x8F, 0x5D, 0x49, 0x6E, 0xBA, 0xB7,
  26792. 0x84, 0x3F, 0x64, 0x83, 0xBF, 0x0C, 0x0C, 0x73,
  26793. 0xA4, 0x02, 0x96, 0xEC, 0x2C, 0x64, 0x40, 0x00,
  26794. 0x13, 0x94, 0xC9, 0x9C, 0xA1, 0x73, 0xD5, 0xC7,
  26795. 0x75, 0xB7, 0xF4, 0x15, 0xD0, 0x2A, 0x5A, 0x26,
  26796. 0xA0, 0x74, 0x07, 0x91, 0x85, 0x87, 0xC4, 0x11,
  26797. 0x69, 0xF2, 0xB7, 0x17, 0x87, 0x55, 0xAC, 0xC2,
  26798. 0x7F, 0xC8, 0xB1, 0x9C, 0x4C, 0x4B, 0x3F, 0xCD,
  26799. 0x41, 0x05, 0x3F, 0x2C, 0x74, 0xC8, 0xA1, 0x0A,
  26800. 0x83, 0x21, 0x24, 0x1B, 0x28, 0x02, 0x43, 0x28,
  26801. 0x75, 0xAE, 0x80, 0x8B, 0x9E, 0xF1, 0x36, 0x5C,
  26802. 0x7B, 0x8A, 0x52, 0x90, 0x2F, 0x13, 0x17, 0xBA,
  26803. 0x2F, 0xB0, 0x26, 0x9F, 0x47, 0x93, 0x06, 0x72,
  26804. 0x10, 0x7B, 0x47, 0x26, 0xFE, 0xF6, 0x45, 0x47,
  26805. 0x39, 0x4D, 0x33, 0x20, 0xC8, 0xF1, 0x20, 0xB3,
  26806. 0xC2, 0xF4, 0x72, 0x5B, 0x03, 0x05, 0xFA, 0xB8,
  26807. 0x8C, 0xC7, 0x98, 0x1F, 0xCB, 0x09, 0xA7, 0x6A,
  26808. 0x1C, 0xBF, 0x7F, 0x17, 0x9F, 0x43, 0xBB, 0x0A,
  26809. 0x4C, 0x8B, 0x05, 0x90, 0x85, 0x7F, 0x1E, 0x69,
  26810. 0x70, 0x84, 0x66, 0xC7, 0xF8, 0x60, 0x73, 0x91,
  26811. 0xE7, 0xBC, 0x52, 0x68, 0xBF, 0xD3, 0xD7, 0xA1,
  26812. 0xDF, 0xFC, 0xB4, 0xEC, 0xA2, 0xA1, 0xC9, 0xB5,
  26813. 0x97, 0x59, 0x30, 0x13, 0xD5, 0xFC, 0x42, 0x02,
  26814. 0xEC, 0x2B, 0x74, 0xE5, 0x7A, 0xB7, 0x6B, 0xBC,
  26815. 0xF3, 0x63, 0x2B, 0xBA, 0xF9, 0x7C, 0xDC, 0x41,
  26816. 0x8A, 0x6F, 0x16, 0x39, 0x28, 0x38, 0xCA, 0x9B,
  26817. 0xF4, 0x5D, 0xDF, 0x02, 0x37, 0x77, 0xB7, 0x56,
  26818. 0x18, 0x33, 0xC1, 0x05, 0x19, 0x0F, 0x94, 0xF3,
  26819. 0x02, 0xC5, 0x9B, 0x53, 0x19, 0x00, 0xBB, 0xC8,
  26820. 0x16, 0x36, 0x1F, 0xAA, 0x5B, 0x33, 0x80, 0xCA,
  26821. 0x3A, 0x89, 0x31, 0x04, 0xCA, 0x73, 0x88, 0xB1,
  26822. 0x85, 0x67, 0x1B, 0x3E, 0x5F, 0xE3, 0x79, 0x0E,
  26823. 0x9A, 0x62, 0x6E, 0xC4, 0x6D, 0x9B, 0x0B, 0x33,
  26824. 0xC7, 0xA4, 0x19, 0xAF, 0x7B, 0x32, 0xB6, 0x85,
  26825. 0x98, 0x94, 0xF5, 0x75, 0xD8, 0x2A, 0xC5, 0x45,
  26826. 0x6B, 0x54, 0x90, 0xA7, 0xAF, 0x8F, 0xE6, 0x10,
  26827. 0x46, 0x36, 0x05, 0x89, 0xEC, 0xBA, 0x72, 0x44,
  26828. 0x23, 0x6F, 0x41, 0x23, 0x11, 0x6B, 0x61, 0x74,
  26829. 0xAA, 0x17, 0x92, 0x49, 0xA4, 0x91, 0x95, 0xB3,
  26830. 0x56, 0xC7, 0x2F, 0xC6, 0x64, 0x1F, 0x02, 0x51,
  26831. 0x81, 0x2E, 0xAA, 0x98, 0x57, 0x0B, 0x04, 0x66,
  26832. 0x99, 0x07, 0x0E, 0x08, 0x19, 0xDC, 0x27, 0x13,
  26833. 0xF4, 0x69, 0x13, 0x7D, 0xFC, 0x6A, 0x3D, 0x7B,
  26834. 0x92, 0xB2, 0x98, 0x99, 0x5E, 0xE7, 0x80, 0x36,
  26835. 0x91, 0x53, 0xAC, 0x36, 0x6B, 0x06, 0xD7, 0x24,
  26836. 0x9C, 0xD0, 0x9E, 0x1B, 0x33, 0x78, 0xFB, 0x04,
  26837. 0x39, 0x9C, 0xEC, 0xB8, 0x65, 0x05, 0x81, 0xD6,
  26838. 0x37, 0xC7, 0x9A, 0xE6, 0x7D, 0x6F, 0x2C, 0xAF,
  26839. 0x6A, 0xBA, 0xCF, 0x59, 0x81, 0x59, 0xA7, 0x79,
  26840. 0x2C, 0xB3, 0xC9, 0x71, 0xD1, 0x49, 0x9D, 0x23,
  26841. 0x73, 0xAD, 0x20, 0xF6, 0x3F, 0x03, 0xBB, 0x59,
  26842. 0xED, 0x13, 0x73, 0x84, 0xAC, 0x61, 0xA7, 0x15,
  26843. 0x51, 0x43, 0xB8, 0xCA, 0x49, 0x32, 0x61, 0x2E,
  26844. 0xC9, 0x15, 0xE4, 0xCA, 0x34, 0x6A, 0x9B, 0xCE,
  26845. 0x5D, 0xD6, 0x04, 0x17, 0xC6, 0xB2, 0xA8, 0x9B,
  26846. 0x1C, 0xC4, 0x35, 0x64, 0x3F, 0x87, 0x5B, 0xDC,
  26847. 0x5A, 0x7E, 0x5B, 0x34, 0x81, 0xCF, 0x91, 0x9E,
  26848. 0xA0, 0x91, 0x72, 0xFE, 0xBC, 0x46, 0xD4, 0xFC,
  26849. 0x3F, 0xB0, 0xCB, 0x95, 0x91, 0x70, 0x4E, 0xE2,
  26850. 0xDB, 0xB6, 0x18, 0x44, 0xB2, 0xF3, 0x31, 0x4A,
  26851. 0x06, 0xBB, 0x6C, 0x6D, 0x34, 0x00, 0x5E, 0x48,
  26852. 0x5C, 0xE6, 0x67, 0xBD, 0xC7, 0xD0, 0x98, 0x58,
  26853. 0x69, 0x28, 0xD2, 0xD9, 0x13, 0x40, 0xF0, 0x04,
  26854. 0x19, 0xEA, 0x40, 0x13, 0x51, 0xA2, 0x40, 0xA0,
  26855. 0xB0, 0x41, 0x05, 0x8B, 0xEF, 0xB0, 0xC2, 0xFD,
  26856. 0x32, 0x64, 0x5B, 0x7A, 0x2D, 0xF8, 0xF5, 0xCB,
  26857. 0xFD, 0x87, 0x33, 0x27, 0xC9, 0x78, 0xD7, 0xB3,
  26858. 0x51, 0xA2, 0x80, 0x88, 0x43, 0x88, 0x37, 0x02,
  26859. 0x4C, 0x52, 0xB9, 0xC2, 0x95, 0xCD, 0x71, 0x36,
  26860. 0x46, 0xFB, 0x5D, 0x6C, 0x0C, 0xCF, 0xB4, 0x70,
  26861. 0x73, 0x4A, 0xC2, 0xB2, 0xBC, 0x81, 0x23, 0xC2,
  26862. 0xC1, 0x3D, 0xF6, 0x93, 0x8E, 0x92, 0x45, 0x5A,
  26863. 0x86, 0x26, 0x39, 0xFE, 0xB8, 0xA6, 0x4B, 0x85,
  26864. 0x16, 0x3E, 0x32, 0x70, 0x7E, 0x03, 0x7B, 0x38,
  26865. 0xD8, 0xAC, 0x39, 0x22, 0xB4, 0x51, 0x87, 0xBB,
  26866. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  26867. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  26868. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  26869. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  26870. };
  26871. const byte kyber512_sk[] = {
  26872. 0x6C, 0x89, 0x2B, 0x02, 0x97, 0xA9, 0xC7, 0x64,
  26873. 0x14, 0x93, 0xF8, 0x7D, 0xAF, 0x35, 0x33, 0xEE,
  26874. 0xD6, 0x1F, 0x07, 0xF4, 0x65, 0x20, 0x66, 0x33,
  26875. 0x7E, 0xD7, 0x40, 0x46, 0xDC, 0xC7, 0x1B, 0xA0,
  26876. 0x3F, 0x30, 0x96, 0x01, 0x03, 0x16, 0x1F, 0x7D,
  26877. 0xEB, 0x53, 0xA7, 0x1B, 0x11, 0x61, 0x72, 0x63,
  26878. 0xFE, 0x2A, 0x80, 0x97, 0x69, 0xCE, 0x6D, 0x70,
  26879. 0xA8, 0x5F, 0xE6, 0x00, 0xEC, 0xE2, 0x9D, 0x7F,
  26880. 0x36, 0xA1, 0x6D, 0x33, 0x1B, 0x8B, 0x2A, 0x9E,
  26881. 0x1D, 0xB8, 0xC0, 0x90, 0x74, 0x2D, 0xF0, 0x73,
  26882. 0x9F, 0xF0, 0x60, 0xCE, 0xB4, 0xEC, 0xC5, 0xAB,
  26883. 0x1C, 0x5E, 0x55, 0xAC, 0x97, 0xBB, 0x66, 0xA7,
  26884. 0xF8, 0x95, 0x10, 0x5D, 0x57, 0x78, 0x2B, 0x22,
  26885. 0x95, 0x38, 0xE3, 0x42, 0x15, 0x44, 0xA3, 0x42,
  26886. 0x14, 0x08, 0xDB, 0xF4, 0x49, 0x10, 0x93, 0x4C,
  26887. 0xC4, 0x23, 0x77, 0x4F, 0x16, 0x76, 0xFF, 0x1C,
  26888. 0x30, 0x6F, 0x97, 0x55, 0x5F, 0x57, 0xB4, 0xAE,
  26889. 0xD7, 0xA6, 0xBA, 0xB9, 0x50, 0xA8, 0x16, 0x3C,
  26890. 0x8D, 0x31, 0x8D, 0xEA, 0x62, 0x75, 0x1B, 0xD6,
  26891. 0xAB, 0xC5, 0x06, 0x9C, 0x06, 0xC8, 0x8F, 0x33,
  26892. 0x00, 0x26, 0xA1, 0x98, 0x06, 0xA0, 0x3B, 0x97,
  26893. 0xA7, 0x69, 0x6B, 0x56, 0xDA, 0x21, 0x82, 0x7B,
  26894. 0xB4, 0xE8, 0xDC, 0x03, 0x11, 0x52, 0xB4, 0x1B,
  26895. 0x89, 0x2A, 0x9E, 0x99, 0xAD, 0xF6, 0xE1, 0x96,
  26896. 0x3E, 0x96, 0x57, 0x88, 0x28, 0x15, 0x4F, 0x46,
  26897. 0x70, 0x33, 0x84, 0x69, 0x20, 0xFB, 0xB4, 0xB8,
  26898. 0x05, 0x44, 0xE7, 0xE8, 0xA8, 0x1A, 0xE9, 0x63,
  26899. 0xCF, 0x36, 0x8C, 0x9B, 0xA0, 0x37, 0xA8, 0xC2,
  26900. 0xAD, 0x62, 0xE3, 0x2B, 0x6E, 0x61, 0xC9, 0x1D,
  26901. 0x75, 0xCE, 0x00, 0x5A, 0xB3, 0x0F, 0x80, 0x99,
  26902. 0xA1, 0xF2, 0x9D, 0x7B, 0x63, 0x05, 0xB4, 0xDC,
  26903. 0x06, 0xE2, 0x56, 0x80, 0xBB, 0x00, 0x99, 0x2F,
  26904. 0x71, 0x7F, 0xE6, 0xC1, 0x15, 0xA8, 0x08, 0x42,
  26905. 0x31, 0xCC, 0x79, 0xDD, 0x70, 0x0E, 0xA6, 0x91,
  26906. 0x2A, 0xC7, 0xFA, 0x0D, 0x93, 0x7B, 0xB6, 0xA7,
  26907. 0x56, 0x66, 0x22, 0x30, 0x47, 0x0C, 0x18, 0x9B,
  26908. 0x5A, 0xA1, 0x65, 0x3D, 0xEB, 0x93, 0x7D, 0x5A,
  26909. 0x9C, 0x25, 0xA2, 0x1D, 0x93, 0xB1, 0x90, 0x74,
  26910. 0xFC, 0x23, 0x9D, 0x81, 0x53, 0x53, 0x97, 0x97,
  26911. 0xC7, 0xD4, 0xAB, 0x62, 0x64, 0x9D, 0x76, 0xAA,
  26912. 0x55, 0x37, 0x36, 0xA9, 0x49, 0x02, 0x2C, 0x22,
  26913. 0xC5, 0x2B, 0xAE, 0xEC, 0x60, 0x5B, 0x32, 0xCE,
  26914. 0x9E, 0x5B, 0x93, 0x84, 0x90, 0x35, 0x58, 0xCA,
  26915. 0x9D, 0x6A, 0x3A, 0xBA, 0x90, 0x42, 0x3E, 0xED,
  26916. 0xA0, 0x1C, 0x94, 0x19, 0x8B, 0x19, 0x2A, 0x8B,
  26917. 0xA9, 0x06, 0x34, 0x97, 0xA0, 0xC5, 0x01, 0x33,
  26918. 0x07, 0xDD, 0xD8, 0x63, 0x52, 0x64, 0x71, 0xA4,
  26919. 0xD9, 0x95, 0x23, 0xEB, 0x41, 0x7F, 0x29, 0x1A,
  26920. 0xAC, 0x0C, 0x3A, 0x58, 0x1B, 0x6D, 0xA0, 0x07,
  26921. 0x32, 0xE5, 0xE8, 0x1B, 0x1F, 0x7C, 0x87, 0x9B,
  26922. 0x16, 0x93, 0xC1, 0x3B, 0x6F, 0x9F, 0x79, 0x31,
  26923. 0x62, 0x24, 0x29, 0xE5, 0x42, 0xAF, 0x40, 0x69,
  26924. 0x22, 0x2F, 0x04, 0x55, 0x44, 0xE0, 0xCC, 0x4F,
  26925. 0xB2, 0x4D, 0x44, 0x48, 0xCF, 0x2C, 0x65, 0x96,
  26926. 0xF5, 0xCB, 0x08, 0x62, 0x4B, 0x11, 0x85, 0x01,
  26927. 0x3B, 0x6B, 0x02, 0x08, 0x92, 0xF9, 0x6B, 0xDF,
  26928. 0xD4, 0xAD, 0xA9, 0x17, 0x9D, 0xE7, 0x27, 0xB8,
  26929. 0xD9, 0x42, 0x6E, 0x09, 0x96, 0xB5, 0xD3, 0x49,
  26930. 0x48, 0xCE, 0x02, 0xD0, 0xC3, 0x69, 0xB3, 0x7C,
  26931. 0xBB, 0x54, 0xD3, 0x47, 0x9E, 0xD8, 0xB5, 0x82,
  26932. 0xE9, 0xE7, 0x28, 0x92, 0x9B, 0x4C, 0x71, 0xC9,
  26933. 0xBE, 0x11, 0xD4, 0x5B, 0x20, 0xC4, 0xBD, 0xC3,
  26934. 0xC7, 0x43, 0x13, 0x22, 0x3F, 0x58, 0x27, 0x4E,
  26935. 0x8B, 0xA5, 0x24, 0x44, 0x47, 0xC4, 0x95, 0x95,
  26936. 0x0B, 0x84, 0xCB, 0x0C, 0x3C, 0x27, 0x36, 0x40,
  26937. 0x10, 0x8A, 0x33, 0x97, 0x94, 0x45, 0x73, 0x27,
  26938. 0x93, 0x28, 0x99, 0x6C, 0xDC, 0x0C, 0x91, 0x3C,
  26939. 0x95, 0x8A, 0xD6, 0x20, 0xBA, 0x8B, 0x5E, 0x5E,
  26940. 0xCB, 0xBB, 0x7E, 0x13, 0xCB, 0x9C, 0x70, 0xBD,
  26941. 0x5A, 0xB3, 0x0E, 0xB7, 0x48, 0x8C, 0x97, 0x00,
  26942. 0x1C, 0x20, 0x49, 0x8F, 0x1D, 0x7C, 0xC0, 0x6D,
  26943. 0xA7, 0x6B, 0xF5, 0x20, 0xC6, 0x58, 0xCC, 0xAD,
  26944. 0xFA, 0x29, 0x56, 0x42, 0x45, 0x57, 0xAB, 0xEA,
  26945. 0x8A, 0xB8, 0x92, 0x39, 0xC1, 0x78, 0x33, 0xDC,
  26946. 0x3A, 0x49, 0xB3, 0x6A, 0x9A, 0xE9, 0xA4, 0x86,
  26947. 0x94, 0x05, 0x40, 0xEB, 0x44, 0x4F, 0x97, 0x15,
  26948. 0x23, 0x57, 0xE0, 0x20, 0x35, 0x93, 0x9D, 0x75,
  26949. 0xA3, 0xC0, 0x25, 0xF4, 0x1A, 0x40, 0x08, 0x23,
  26950. 0x82, 0xA0, 0x73, 0x3C, 0x39, 0xB0, 0x62, 0x2B,
  26951. 0x74, 0x0E, 0x40, 0x75, 0x92, 0xC6, 0x2E, 0xCA,
  26952. 0xEB, 0x14, 0x32, 0xC4, 0x45, 0xB3, 0x70, 0x3A,
  26953. 0x86, 0xF6, 0x98, 0x1A, 0x27, 0x81, 0x57, 0xEA,
  26954. 0x95, 0xA6, 0xE9, 0x2D, 0x55, 0xE4, 0xB9, 0x72,
  26955. 0xF9, 0x36, 0xC2, 0xF0, 0xA6, 0x58, 0x28, 0x0E,
  26956. 0xA2, 0xB0, 0x7A, 0x48, 0x99, 0x2D, 0xF8, 0x93,
  26957. 0x7E, 0x0A, 0x2A, 0xC1, 0xDC, 0xC9, 0x74, 0xFE,
  26958. 0x00, 0xAA, 0xE1, 0xF5, 0x61, 0xFA, 0x25, 0x8E,
  26959. 0x2D, 0x25, 0x9C, 0x3E, 0x86, 0x1D, 0xCE, 0x23,
  26960. 0x60, 0x39, 0x12, 0x76, 0x06, 0xFC, 0x1C, 0xE0,
  26961. 0x09, 0x00, 0x3A, 0x7B, 0xAC, 0x94, 0x21, 0x01,
  26962. 0xDC, 0xB8, 0x22, 0xB1, 0xF3, 0xC1, 0x2B, 0xF7,
  26963. 0x32, 0x38, 0xF5, 0x46, 0xE0, 0x1C, 0x36, 0xB5,
  26964. 0xA6, 0x93, 0x61, 0x92, 0x99, 0x5C, 0xC6, 0x9C,
  26965. 0x63, 0x23, 0x74, 0x09, 0xCB, 0x53, 0xC2, 0xE3,
  26966. 0x5D, 0x74, 0x89, 0x0D, 0x18, 0x88, 0x53, 0x76,
  26967. 0xFA, 0x55, 0x03, 0xB1, 0x07, 0xA2, 0xA3, 0x92,
  26968. 0x11, 0x5A, 0xCE, 0x0E, 0x64, 0x67, 0x7C, 0xBB,
  26969. 0x7D, 0xCF, 0xC9, 0x3C, 0x16, 0xD3, 0xA3, 0x05,
  26970. 0xF6, 0x76, 0x15, 0xA4, 0x88, 0xD7, 0x11, 0xAA,
  26971. 0x56, 0x69, 0x8C, 0x56, 0x63, 0xAB, 0x7A, 0xC9,
  26972. 0xCE, 0x66, 0xD5, 0x47, 0xC0, 0x59, 0x5F, 0x98,
  26973. 0xA4, 0x3F, 0x46, 0x50, 0xBB, 0xE0, 0x8C, 0x36,
  26974. 0x4D, 0x97, 0x67, 0x89, 0x11, 0x7D, 0x34, 0xF6,
  26975. 0xAE, 0x51, 0xAC, 0x06, 0x3C, 0xB5, 0x5C, 0x6C,
  26976. 0xA3, 0x25, 0x58, 0x22, 0x7D, 0xFE, 0xF8, 0x07,
  26977. 0xD1, 0x9C, 0x30, 0xDE, 0x41, 0x44, 0x24, 0x09,
  26978. 0x7F, 0x6A, 0xA2, 0x36, 0xA1, 0x05, 0x3B, 0x4A,
  26979. 0x07, 0xA7, 0x6B, 0xE3, 0x72, 0xA5, 0xC6, 0xB6,
  26980. 0x00, 0x27, 0x91, 0xEB, 0xE0, 0xAF, 0xDA, 0xF5,
  26981. 0x4E, 0x1C, 0xA2, 0x37, 0xFF, 0x54, 0x5B, 0xA6,
  26982. 0x83, 0x43, 0xE7, 0x45, 0xC0, 0x4A, 0xD1, 0x63,
  26983. 0x9D, 0xBC, 0x59, 0x03, 0x46, 0xB6, 0xB9, 0x56,
  26984. 0x9B, 0x56, 0xDB, 0xBF, 0xE5, 0x31, 0x51, 0x91,
  26985. 0x30, 0x66, 0xE5, 0xC8, 0x55, 0x27, 0xDC, 0x94,
  26986. 0x68, 0x11, 0x0A, 0x13, 0x6A, 0x41, 0x14, 0x97,
  26987. 0xC2, 0x27, 0xDC, 0xB8, 0xC9, 0xB2, 0x55, 0x70,
  26988. 0xB7, 0xA0, 0xE4, 0x2A, 0xAD, 0xA6, 0x70, 0x9F,
  26989. 0x23, 0x20, 0x8F, 0x5D, 0x49, 0x6E, 0xBA, 0xB7,
  26990. 0x84, 0x3F, 0x64, 0x83, 0xBF, 0x0C, 0x0C, 0x73,
  26991. 0xA4, 0x02, 0x96, 0xEC, 0x2C, 0x64, 0x40, 0x00,
  26992. 0x13, 0x94, 0xC9, 0x9C, 0xA1, 0x73, 0xD5, 0xC7,
  26993. 0x75, 0xB7, 0xF4, 0x15, 0xD0, 0x2A, 0x5A, 0x26,
  26994. 0xA0, 0x74, 0x07, 0x91, 0x85, 0x87, 0xC4, 0x11,
  26995. 0x69, 0xF2, 0xB7, 0x17, 0x87, 0x55, 0xAC, 0xC2,
  26996. 0x7F, 0xC8, 0xB1, 0x9C, 0x4C, 0x4B, 0x3F, 0xCD,
  26997. 0x41, 0x05, 0x3F, 0x2C, 0x74, 0xC8, 0xA1, 0x0A,
  26998. 0x83, 0x21, 0x24, 0x1B, 0x28, 0x02, 0x43, 0x28,
  26999. 0x75, 0xAE, 0x80, 0x8B, 0x9E, 0xF1, 0x36, 0x5C,
  27000. 0x7B, 0x8A, 0x52, 0x90, 0x2F, 0x13, 0x17, 0xBA,
  27001. 0x2F, 0xB0, 0x26, 0x9F, 0x47, 0x93, 0x06, 0x72,
  27002. 0x10, 0x7B, 0x47, 0x26, 0xFE, 0xF6, 0x45, 0x47,
  27003. 0x39, 0x4D, 0x33, 0x20, 0xC8, 0xF1, 0x20, 0xB3,
  27004. 0xC2, 0xF4, 0x72, 0x5B, 0x03, 0x05, 0xFA, 0xB8,
  27005. 0x8C, 0xC7, 0x98, 0x1F, 0xCB, 0x09, 0xA7, 0x6A,
  27006. 0x1C, 0xBF, 0x7F, 0x17, 0x9F, 0x43, 0xBB, 0x0A,
  27007. 0x4C, 0x8B, 0x05, 0x90, 0x85, 0x7F, 0x1E, 0x69,
  27008. 0x70, 0x84, 0x66, 0xC7, 0xF8, 0x60, 0x73, 0x91,
  27009. 0xE7, 0xBC, 0x52, 0x68, 0xBF, 0xD3, 0xD7, 0xA1,
  27010. 0xDF, 0xFC, 0xB4, 0xEC, 0xA2, 0xA1, 0xC9, 0xB5,
  27011. 0x97, 0x59, 0x30, 0x13, 0xD5, 0xFC, 0x42, 0x02,
  27012. 0xEC, 0x2B, 0x74, 0xE5, 0x7A, 0xB7, 0x6B, 0xBC,
  27013. 0xF3, 0x63, 0x2B, 0xBA, 0xF9, 0x7C, 0xDC, 0x41,
  27014. 0x8A, 0x6F, 0x16, 0x39, 0x28, 0x38, 0xCA, 0x9B,
  27015. 0xF4, 0x5D, 0xDF, 0x02, 0x37, 0x77, 0xB7, 0x56,
  27016. 0x18, 0x33, 0xC1, 0x05, 0x19, 0x0F, 0x94, 0xF3,
  27017. 0x02, 0xC5, 0x9B, 0x53, 0x19, 0x00, 0xBB, 0xC8,
  27018. 0x16, 0x36, 0x1F, 0xAA, 0x5B, 0x33, 0x80, 0xCA,
  27019. 0x3A, 0x89, 0x31, 0x04, 0xCA, 0x73, 0x88, 0xB1,
  27020. 0x85, 0x67, 0x1B, 0x3E, 0x5F, 0xE3, 0x79, 0x0E,
  27021. 0x9A, 0x62, 0x6E, 0xC4, 0x6D, 0x9B, 0x0B, 0x33,
  27022. 0xC7, 0xA4, 0x19, 0xAF, 0x7B, 0x32, 0xB6, 0x85,
  27023. 0x98, 0x94, 0xF5, 0x75, 0xD8, 0x2A, 0xC5, 0x45,
  27024. 0x6B, 0x54, 0x90, 0xA7, 0xAF, 0x8F, 0xE6, 0x10,
  27025. 0x46, 0x36, 0x05, 0x89, 0xEC, 0xBA, 0x72, 0x44,
  27026. 0x23, 0x6F, 0x41, 0x23, 0x11, 0x6B, 0x61, 0x74,
  27027. 0xAA, 0x17, 0x92, 0x49, 0xA4, 0x91, 0x95, 0xB3,
  27028. 0x56, 0xC7, 0x2F, 0xC6, 0x64, 0x1F, 0x02, 0x51,
  27029. 0x81, 0x2E, 0xAA, 0x98, 0x57, 0x0B, 0x04, 0x66,
  27030. 0x99, 0x07, 0x0E, 0x08, 0x19, 0xDC, 0x27, 0x13,
  27031. 0xF4, 0x69, 0x13, 0x7D, 0xFC, 0x6A, 0x3D, 0x7B,
  27032. 0x92, 0xB2, 0x98, 0x99, 0x5E, 0xE7, 0x80, 0x36,
  27033. 0x91, 0x53, 0xAC, 0x36, 0x6B, 0x06, 0xD7, 0x24,
  27034. 0x9C, 0xD0, 0x9E, 0x1B, 0x33, 0x78, 0xFB, 0x04,
  27035. 0x39, 0x9C, 0xEC, 0xB8, 0x65, 0x05, 0x81, 0xD6,
  27036. 0x37, 0xC7, 0x9A, 0xE6, 0x7D, 0x6F, 0x2C, 0xAF,
  27037. 0x6A, 0xBA, 0xCF, 0x59, 0x81, 0x59, 0xA7, 0x79,
  27038. 0x2C, 0xB3, 0xC9, 0x71, 0xD1, 0x49, 0x9D, 0x23,
  27039. 0x73, 0xAD, 0x20, 0xF6, 0x3F, 0x03, 0xBB, 0x59,
  27040. 0xED, 0x13, 0x73, 0x84, 0xAC, 0x61, 0xA7, 0x15,
  27041. 0x51, 0x43, 0xB8, 0xCA, 0x49, 0x32, 0x61, 0x2E,
  27042. 0xC9, 0x15, 0xE4, 0xCA, 0x34, 0x6A, 0x9B, 0xCE,
  27043. 0x5D, 0xD6, 0x04, 0x17, 0xC6, 0xB2, 0xA8, 0x9B,
  27044. 0x1C, 0xC4, 0x35, 0x64, 0x3F, 0x87, 0x5B, 0xDC,
  27045. 0x5A, 0x7E, 0x5B, 0x34, 0x81, 0xCF, 0x91, 0x9E,
  27046. 0xA0, 0x91, 0x72, 0xFE, 0xBC, 0x46, 0xD4, 0xFC,
  27047. 0x3F, 0xB0, 0xCB, 0x95, 0x91, 0x70, 0x4E, 0xE2,
  27048. 0xDB, 0xB6, 0x18, 0x44, 0xB2, 0xF3, 0x31, 0x4A,
  27049. 0x06, 0xBB, 0x6C, 0x6D, 0x34, 0x00, 0x5E, 0x48,
  27050. 0x5C, 0xE6, 0x67, 0xBD, 0xC7, 0xD0, 0x98, 0x58,
  27051. 0x69, 0x28, 0xD2, 0xD9, 0x13, 0x40, 0xF0, 0x04,
  27052. 0x19, 0xEA, 0x40, 0x13, 0x51, 0xA2, 0x40, 0xA0,
  27053. 0xB0, 0x41, 0x05, 0x8B, 0xEF, 0xB0, 0xC2, 0xFD,
  27054. 0x32, 0x64, 0x5B, 0x7A, 0x2D, 0xF8, 0xF5, 0xCB,
  27055. 0xFD, 0x87, 0x33, 0x27, 0xC9, 0x78, 0xD7, 0xB3,
  27056. 0x51, 0xA2, 0x80, 0x88, 0x43, 0x88, 0x37, 0x02,
  27057. 0x4C, 0x52, 0xB9, 0xC2, 0x95, 0xCD, 0x71, 0x36,
  27058. 0x46, 0xFB, 0x5D, 0x6C, 0x0C, 0xCF, 0xB4, 0x70,
  27059. 0x73, 0x4A, 0xC2, 0xB2, 0xBC, 0x81, 0x23, 0xC2,
  27060. 0xC1, 0x3D, 0xF6, 0x93, 0x8E, 0x92, 0x45, 0x5A,
  27061. 0x86, 0x26, 0x39, 0xFE, 0xB8, 0xA6, 0x4B, 0x85,
  27062. 0x16, 0x3E, 0x32, 0x70, 0x7E, 0x03, 0x7B, 0x38,
  27063. 0xD8, 0xAC, 0x39, 0x22, 0xB4, 0x51, 0x87, 0xBB,
  27064. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  27065. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  27066. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  27067. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  27068. 0x7F, 0xFA, 0xD1, 0xBC, 0x8A, 0xF7, 0x3B, 0x7E,
  27069. 0x87, 0x49, 0x56, 0xB8, 0x1C, 0x2A, 0x2E, 0xF0,
  27070. 0xBF, 0xAB, 0xE8, 0xDC, 0x93, 0xD7, 0x7B, 0x2F,
  27071. 0xBC, 0x9E, 0x0C, 0x64, 0xEF, 0xA0, 0x1E, 0x84,
  27072. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  27073. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  27074. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  27075. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  27076. };
  27077. const byte kyber512_ct[] = {
  27078. 0xED, 0xF2, 0x41, 0x45, 0xE4, 0x3B, 0x4F, 0x6D,
  27079. 0xC6, 0xBF, 0x83, 0x32, 0xF5, 0x4E, 0x02, 0xCA,
  27080. 0xB0, 0x2D, 0xBF, 0x3B, 0x56, 0x05, 0xDD, 0xC9,
  27081. 0x0A, 0x15, 0xC8, 0x86, 0xAD, 0x3E, 0xD4, 0x89,
  27082. 0x46, 0x26, 0x99, 0xE4, 0xAB, 0xED, 0x44, 0x35,
  27083. 0x0B, 0xC3, 0x75, 0x7E, 0x26, 0x96, 0xFB, 0xFB,
  27084. 0x25, 0x34, 0x41, 0x2E, 0x8D, 0xD2, 0x01, 0xF1,
  27085. 0xE4, 0x54, 0x0A, 0x39, 0x70, 0xB0, 0x55, 0xFE,
  27086. 0x3B, 0x0B, 0xEC, 0x3A, 0x71, 0xF9, 0xE1, 0x15,
  27087. 0xB3, 0xF9, 0xF3, 0x91, 0x02, 0x06, 0x5B, 0x1C,
  27088. 0xCA, 0x83, 0x14, 0xDC, 0xC7, 0x95, 0xE3, 0xC0,
  27089. 0xE8, 0xFA, 0x98, 0xEE, 0x83, 0xCA, 0x66, 0x28,
  27090. 0x45, 0x70, 0x28, 0xA4, 0xD0, 0x9E, 0x83, 0x9E,
  27091. 0x55, 0x48, 0x62, 0xCF, 0x0B, 0x7B, 0xF5, 0x6C,
  27092. 0x5C, 0x0A, 0x82, 0x9E, 0x86, 0x57, 0x94, 0x79,
  27093. 0x45, 0xFE, 0x9C, 0x22, 0x56, 0x4F, 0xBA, 0xEB,
  27094. 0xC1, 0xB3, 0xAF, 0x35, 0x0D, 0x79, 0x55, 0x50,
  27095. 0x8A, 0x26, 0xD8, 0xA8, 0xEB, 0x54, 0x7B, 0x8B,
  27096. 0x1A, 0x2C, 0xF0, 0x3C, 0xCA, 0x1A, 0xAB, 0xCE,
  27097. 0x6C, 0x34, 0x97, 0x78, 0x3B, 0x64, 0x65, 0xBA,
  27098. 0x0B, 0x6E, 0x7A, 0xCB, 0xA8, 0x21, 0x19, 0x51,
  27099. 0x24, 0xAE, 0xF0, 0x9E, 0x62, 0x83, 0x82, 0xA1,
  27100. 0xF9, 0x14, 0x04, 0x3B, 0xE7, 0x09, 0x6E, 0x95,
  27101. 0x2C, 0xBC, 0x4F, 0xB4, 0xAF, 0xED, 0x13, 0x60,
  27102. 0x90, 0x46, 0x11, 0x7C, 0x01, 0x1F, 0xD7, 0x41,
  27103. 0xEE, 0x28, 0x6C, 0x83, 0x77, 0x16, 0x90, 0xF0,
  27104. 0xAE, 0xB5, 0x0D, 0xA0, 0xD7, 0x12, 0x85, 0xA1,
  27105. 0x79, 0xB2, 0x15, 0xC6, 0x03, 0x6D, 0xEB, 0x78,
  27106. 0x0F, 0x4D, 0x16, 0x76, 0x9F, 0x72, 0xDE, 0x16,
  27107. 0xFD, 0xAD, 0xAC, 0x73, 0xBE, 0xFA, 0x5B, 0xEF,
  27108. 0x89, 0x43, 0x19, 0x7F, 0x44, 0xC5, 0x95, 0x89,
  27109. 0xDC, 0x9F, 0x49, 0x73, 0xDE, 0x14, 0x50, 0xBA,
  27110. 0x1D, 0x0C, 0x32, 0x90, 0xD6, 0xB1, 0xD6, 0x83,
  27111. 0xF2, 0x94, 0xE7, 0x59, 0xC9, 0x54, 0xAB, 0xE8,
  27112. 0xA7, 0xDA, 0x5B, 0x10, 0x54, 0xFD, 0x6D, 0x21,
  27113. 0x32, 0x9B, 0x8E, 0x73, 0xD3, 0x75, 0x6A, 0xFD,
  27114. 0xA0, 0xDC, 0xB1, 0xFC, 0x8B, 0x15, 0x82, 0xD1,
  27115. 0xF9, 0x0C, 0xF2, 0x75, 0xA1, 0x02, 0xAB, 0xC6,
  27116. 0xAC, 0x69, 0x9D, 0xF0, 0xC5, 0x87, 0x0E, 0x50,
  27117. 0xA1, 0xF9, 0x89, 0xE4, 0xE6, 0x24, 0x1B, 0x60,
  27118. 0xAA, 0xA2, 0xEC, 0xF9, 0xE8, 0xE3, 0x3E, 0x0F,
  27119. 0xFC, 0xF4, 0x0F, 0xE8, 0x31, 0xE8, 0xFD, 0xC2,
  27120. 0xE8, 0x3B, 0x52, 0xCA, 0x7A, 0xB6, 0xD9, 0x3F,
  27121. 0x14, 0x6D, 0x29, 0xDC, 0xA5, 0x3C, 0x7D, 0xA1,
  27122. 0xDB, 0x4A, 0xC4, 0xF2, 0xDB, 0x39, 0xEA, 0x12,
  27123. 0x0D, 0x90, 0xFA, 0x60, 0xF4, 0xD4, 0x37, 0xC6,
  27124. 0xD0, 0x0E, 0xF4, 0x83, 0xBC, 0x94, 0xA3, 0x17,
  27125. 0x5C, 0xDA, 0x16, 0x3F, 0xC1, 0xC2, 0x82, 0x8B,
  27126. 0xE4, 0xDB, 0xD6, 0x43, 0x05, 0x07, 0xB5, 0x84,
  27127. 0xBB, 0x51, 0x77, 0xE1, 0x71, 0xB8, 0xDD, 0xA9,
  27128. 0xA4, 0x29, 0x3C, 0x32, 0x00, 0x29, 0x5C, 0x80,
  27129. 0x3A, 0x86, 0x5D, 0x6D, 0x21, 0x66, 0xF6, 0x6B,
  27130. 0xA5, 0x40, 0x1F, 0xB7, 0xA0, 0xE8, 0x53, 0x16,
  27131. 0x86, 0x00, 0xA2, 0x94, 0x84, 0x37, 0xE0, 0x36,
  27132. 0xE3, 0xBF, 0x19, 0xE1, 0x2F, 0xD3, 0xF2, 0xA2,
  27133. 0xB8, 0xB3, 0x43, 0xF7, 0x84, 0x24, 0x8E, 0x8D,
  27134. 0x68, 0x5E, 0xB0, 0xAF, 0xDE, 0x63, 0x15, 0x33,
  27135. 0x87, 0x30, 0xE7, 0xA1, 0x00, 0x1C, 0x27, 0xD8,
  27136. 0xD2, 0xA7, 0x6F, 0xA6, 0x9D, 0x15, 0x7B, 0xA1,
  27137. 0xAC, 0x7A, 0xD5, 0x6D, 0xA5, 0xA8, 0xC7, 0x0F,
  27138. 0xE4, 0xB5, 0xB8, 0xD7, 0x86, 0xDC, 0x6F, 0xC0,
  27139. 0x56, 0x6B, 0xA8, 0xE1, 0xB8, 0x81, 0x63, 0x34,
  27140. 0xD3, 0x2A, 0x3F, 0xB1, 0xCE, 0x7D, 0x4D, 0x5E,
  27141. 0x4C, 0x33, 0x2A, 0xF7, 0xB0, 0x03, 0xD0, 0x91,
  27142. 0x74, 0x1A, 0x3D, 0x5C, 0x96, 0x52, 0x92, 0x25,
  27143. 0x5D, 0xFF, 0x8E, 0xD2, 0xBB, 0xF1, 0xF9, 0x11,
  27144. 0x6B, 0xE5, 0x0C, 0x17, 0xB8, 0xE5, 0x48, 0x74,
  27145. 0x8A, 0xD4, 0xB2, 0xE9, 0x57, 0xBB, 0xD1, 0x95,
  27146. 0x34, 0x82, 0xA2, 0xE1, 0x71, 0x8C, 0xEC, 0x66,
  27147. 0xCD, 0x2C, 0x81, 0xF5, 0x72, 0xD5, 0x52, 0xB7,
  27148. 0x18, 0x78, 0x85, 0xE6, 0xB8, 0x94, 0x3D, 0x64,
  27149. 0x31, 0x41, 0x3C, 0x59, 0xEB, 0xB7, 0xE0, 0x36,
  27150. 0x04, 0x84, 0x90, 0xBE, 0x52, 0x89, 0xE9, 0x5B,
  27151. 0x20, 0xA8, 0x9E, 0x8B, 0x15, 0x9F, 0x61, 0xA9,
  27152. 0xA9, 0x88, 0x6E, 0x14, 0x75, 0x68, 0xF4, 0xC9,
  27153. 0x02, 0x1F, 0x36, 0x2F, 0x02, 0x68, 0x8A, 0x1C,
  27154. 0x8C, 0x3B, 0xB0, 0xD2, 0x40, 0x86, 0x88, 0x0E,
  27155. 0x55, 0xB6, 0xED, 0xB4, 0x3F, 0x37, 0x45, 0xD2,
  27156. 0xC1, 0x66, 0xDC, 0x1C, 0xB7, 0x43, 0xC7, 0x6F,
  27157. 0xE6, 0xBE, 0x52, 0x3A, 0x89, 0x3C, 0xC7, 0x64,
  27158. 0xD1, 0x64, 0x35, 0xC3, 0x78, 0x51, 0x25, 0x2A,
  27159. 0x81, 0xE2, 0xFF, 0xBA, 0x0F, 0x18, 0x97, 0x1A,
  27160. 0x3D, 0xEE, 0x37, 0xD4, 0x87, 0x7C, 0xB9, 0x28,
  27161. 0xE3, 0x6E, 0x52, 0x35, 0x03, 0x7A, 0x6B, 0x20,
  27162. 0x57, 0x89, 0x7D, 0x51, 0x8A, 0x5F, 0x0E, 0x34,
  27163. 0x8E, 0x3A, 0xB6, 0xD5, 0xB5, 0x2D, 0xFC, 0x60,
  27164. 0x75, 0x7F, 0x3B, 0x41, 0xA4, 0xFE, 0xC7, 0x82,
  27165. 0x8F, 0x1D, 0xEE, 0xAF, 0x45, 0x87, 0xCC, 0xC8,
  27166. 0xEA, 0xDF, 0x64, 0x7F, 0x4D, 0x20, 0x3B, 0x2F,
  27167. 0xAA, 0x05, 0xA6, 0x49, 0xB5, 0x82, 0x34, 0x0C,
  27168. 0xB4, 0xCA, 0xCE, 0x57, 0xA3, 0x07, 0x11, 0xBE,
  27169. 0x75, 0x2F, 0xAC, 0xF0, 0x22, 0x7D, 0x0A, 0x80,
  27170. 0xC4, 0x12, 0x84, 0x42, 0xDD, 0xC5, 0x44, 0xBE,
  27171. 0x80, 0x5B, 0x9C, 0xFE, 0x8F, 0xE9, 0xB1, 0x23,
  27172. 0x7C, 0x80, 0xF9, 0x67, 0x87, 0xCD, 0x92, 0x81,
  27173. 0xCC, 0xF2, 0x70, 0xC1, 0xAF, 0xC0, 0x67, 0x0D
  27174. };
  27175. const byte kyber512_ss[] = {
  27176. 0x0A, 0x69, 0x25, 0x67, 0x6F, 0x24, 0xB2, 0x2C,
  27177. 0x28, 0x6F, 0x4C, 0x81, 0xA4, 0x22, 0x4C, 0xEC,
  27178. 0x50, 0x6C, 0x9B, 0x25, 0x7D, 0x48, 0x0E, 0x02,
  27179. 0xE3, 0xB4, 0x9F, 0x44, 0xCA, 0xA3, 0x23, 0x7F
  27180. };
  27181. ret = wc_KyberKey_Init(KYBER512, &key, HEAP_HINT, INVALID_DEVID);
  27182. if (ret != 0)
  27183. return -20101;
  27184. ret = wc_KyberKey_MakeKeyWithRandom(&key, kyber512_rand,
  27185. sizeof(kyber512_rand));
  27186. if (ret != 0)
  27187. return -20102;
  27188. ret = wc_KyberKey_EncodePublicKey(&key, pub, sizeof(pub));
  27189. if (ret != 0)
  27190. return -20103;
  27191. ret = wc_KyberKey_EncodePrivateKey(&key, priv, sizeof(priv));
  27192. if (ret != 0)
  27193. return -20104;
  27194. if (XMEMCMP(pub, kyber512_pk, sizeof(kyber512_pk)) != 0)
  27195. return -20105;
  27196. if (XMEMCMP(priv, kyber512_sk, sizeof(kyber512_sk)) != 0)
  27197. return -20106;
  27198. ret = wc_KyberKey_EncapsulateWithRandom(&key, ct, ss, kyber512enc_rand,
  27199. sizeof(kyber512enc_rand));
  27200. if (ret != 0)
  27201. return -20107;
  27202. if (XMEMCMP(ct, kyber512_ct, sizeof(kyber512_ct)) != 0)
  27203. return -20108;
  27204. if (XMEMCMP(ss, kyber512_ss, sizeof(kyber512_ss)) != 0)
  27205. return -20109;
  27206. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, sizeof(kyber512_ct));
  27207. if (ret != 0)
  27208. return -20110;
  27209. if (XMEMCMP(ss_dec, kyber512_ss, sizeof(kyber512_ss)) != 0)
  27210. return -20111;
  27211. wc_KyberKey_Free(&key);
  27212. return 0;
  27213. }
  27214. #endif /* WOLFSSL_KYBER512 */
  27215. #ifdef WOLFSSL_KYBER768
  27216. static int kyber768_kat(void)
  27217. {
  27218. KyberKey key;
  27219. int ret;
  27220. byte priv[KYBER768_PRIVATE_KEY_SIZE];
  27221. byte pub[KYBER768_PUBLIC_KEY_SIZE];
  27222. byte ct[KYBER768_CIPHER_TEXT_SIZE];
  27223. byte ss[KYBER_SS_SZ];
  27224. byte ss_dec[KYBER_SS_SZ];
  27225. const byte kyber768_rand[] = {
  27226. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  27227. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  27228. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  27229. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  27230. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  27231. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  27232. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  27233. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  27234. };
  27235. const byte kyber768enc_rand[] = {
  27236. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  27237. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  27238. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  27239. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  27240. };
  27241. const byte kyber768_pk[] = {
  27242. 0xA7, 0x2C, 0x2D, 0x9C, 0x84, 0x3E, 0xE9, 0xF8,
  27243. 0x31, 0x3E, 0xCC, 0x7F, 0x86, 0xD6, 0x29, 0x4D,
  27244. 0x59, 0x15, 0x9D, 0x9A, 0x87, 0x9A, 0x54, 0x2E,
  27245. 0x26, 0x09, 0x22, 0xAD, 0xF9, 0x99, 0x05, 0x1C,
  27246. 0xC4, 0x52, 0x00, 0xC9, 0xFF, 0xDB, 0x60, 0x44,
  27247. 0x9C, 0x49, 0x46, 0x59, 0x79, 0x27, 0x23, 0x67,
  27248. 0xC0, 0x83, 0xA7, 0xD6, 0x26, 0x7A, 0x3E, 0xD7,
  27249. 0xA7, 0xFD, 0x47, 0x95, 0x7C, 0x21, 0x93, 0x27,
  27250. 0xF7, 0xCA, 0x73, 0xA4, 0x00, 0x7E, 0x16, 0x27,
  27251. 0xF0, 0x0B, 0x11, 0xCC, 0x80, 0x57, 0x3C, 0x15,
  27252. 0xAE, 0xE6, 0x64, 0x0F, 0xB8, 0x56, 0x2D, 0xFA,
  27253. 0x6B, 0x24, 0x0C, 0xA0, 0xAD, 0x35, 0x1A, 0xC4,
  27254. 0xAC, 0x15, 0x5B, 0x96, 0xC1, 0x4C, 0x8A, 0xB1,
  27255. 0x3D, 0xD2, 0x62, 0xCD, 0xFD, 0x51, 0xC4, 0xBB,
  27256. 0x55, 0x72, 0xFD, 0x61, 0x65, 0x53, 0xD1, 0x7B,
  27257. 0xDD, 0x43, 0x0A, 0xCB, 0xEA, 0x3E, 0x95, 0xF0,
  27258. 0xB6, 0x98, 0xD6, 0x69, 0x90, 0xAB, 0x51, 0xE5,
  27259. 0xD0, 0x37, 0x83, 0xA8, 0xB3, 0xD2, 0x78, 0xA5,
  27260. 0x72, 0x04, 0x54, 0xCF, 0x96, 0x95, 0xCF, 0xDC,
  27261. 0xA0, 0x84, 0x85, 0xBA, 0x09, 0x9C, 0x51, 0xCD,
  27262. 0x92, 0xA7, 0xEA, 0x75, 0x87, 0xC1, 0xD1, 0x5C,
  27263. 0x28, 0xE6, 0x09, 0xA8, 0x18, 0x52, 0x60, 0x1B,
  27264. 0x06, 0x04, 0x01, 0x06, 0x79, 0xAA, 0x48, 0x2D,
  27265. 0x51, 0x26, 0x1E, 0xC3, 0x6E, 0x36, 0xB8, 0x71,
  27266. 0x96, 0x76, 0x21, 0x7F, 0xD7, 0x4C, 0x54, 0x78,
  27267. 0x64, 0x88, 0xF4, 0xB4, 0x96, 0x9C, 0x05, 0xA8,
  27268. 0xBA, 0x27, 0xCA, 0x3A, 0x77, 0xCC, 0xE7, 0x3B,
  27269. 0x96, 0x59, 0x23, 0xCA, 0x55, 0x4E, 0x42, 0x2B,
  27270. 0x9B, 0x61, 0xF4, 0x75, 0x46, 0x41, 0x60, 0x8A,
  27271. 0xC1, 0x6C, 0x9B, 0x85, 0x87, 0xA3, 0x2C, 0x1C,
  27272. 0x5D, 0xD7, 0x88, 0xF8, 0x8B, 0x36, 0xB7, 0x17,
  27273. 0xA4, 0x69, 0x65, 0x63, 0x5D, 0xEB, 0x67, 0xF4,
  27274. 0x5B, 0x12, 0x9B, 0x99, 0x07, 0x09, 0x09, 0xC9,
  27275. 0x3E, 0xB8, 0x0B, 0x42, 0xC2, 0xB3, 0xF3, 0xF7,
  27276. 0x03, 0x43, 0xA7, 0xCF, 0x37, 0xE8, 0x52, 0x0E,
  27277. 0x7B, 0xCF, 0xC4, 0x16, 0xAC, 0xA4, 0xF1, 0x8C,
  27278. 0x79, 0x81, 0x26, 0x2B, 0xA2, 0xBF, 0xC7, 0x56,
  27279. 0xAE, 0x03, 0x27, 0x8F, 0x0E, 0xC6, 0x6D, 0xC2,
  27280. 0x05, 0x76, 0x96, 0x82, 0x4B, 0xA6, 0x76, 0x98,
  27281. 0x65, 0xA6, 0x01, 0xD7, 0x14, 0x8E, 0xF6, 0xF5,
  27282. 0x4E, 0x5A, 0xF5, 0x68, 0x6A, 0xA2, 0x90, 0x6F,
  27283. 0x99, 0x4C, 0xE3, 0x8A, 0x5E, 0x0B, 0x93, 0x8F,
  27284. 0x23, 0x90, 0x07, 0x00, 0x30, 0x22, 0xC0, 0x33,
  27285. 0x92, 0xDF, 0x34, 0x01, 0xB1, 0xE4, 0xA3, 0xA7,
  27286. 0xEB, 0xC6, 0x16, 0x14, 0x49, 0xF7, 0x33, 0x74,
  27287. 0xC8, 0xB0, 0x14, 0x03, 0x69, 0x34, 0x3D, 0x92,
  27288. 0x95, 0xFD, 0xF5, 0x11, 0x84, 0x5C, 0x4A, 0x46,
  27289. 0xEB, 0xAA, 0xB6, 0xCA, 0x54, 0x92, 0xF6, 0x80,
  27290. 0x0B, 0x98, 0xC0, 0xCC, 0x80, 0x36, 0x53, 0xA4,
  27291. 0xB1, 0xD6, 0xE6, 0xAA, 0xED, 0x19, 0x32, 0xBA,
  27292. 0xCC, 0x5F, 0xEF, 0xAA, 0x81, 0x8B, 0xA5, 0x02,
  27293. 0x85, 0x9B, 0xA5, 0x49, 0x4C, 0x5F, 0x54, 0x02,
  27294. 0xC8, 0x53, 0x6A, 0x9C, 0x4C, 0x18, 0x88, 0x15,
  27295. 0x06, 0x17, 0xF8, 0x00, 0x98, 0xF6, 0xB2, 0xA9,
  27296. 0x9C, 0x39, 0xBC, 0x5D, 0xC7, 0xCF, 0x3B, 0x59,
  27297. 0x00, 0xA2, 0x13, 0x29, 0xAB, 0x59, 0x05, 0x3A,
  27298. 0xBA, 0xA6, 0x4E, 0xD1, 0x63, 0xE8, 0x59, 0xA8,
  27299. 0xB3, 0xB3, 0xCA, 0x33, 0x59, 0xB7, 0x50, 0xCC,
  27300. 0xC3, 0xE7, 0x10, 0xC7, 0xAC, 0x43, 0xC8, 0x19,
  27301. 0x1C, 0xB5, 0xD6, 0x88, 0x70, 0xC0, 0x63, 0x91,
  27302. 0xC0, 0xCB, 0x8A, 0xEC, 0x72, 0xB8, 0x97, 0xAC,
  27303. 0x6B, 0xE7, 0xFB, 0xAA, 0xCC, 0x67, 0x6E, 0xD6,
  27304. 0x63, 0x14, 0xC8, 0x36, 0x30, 0xE8, 0x94, 0x48,
  27305. 0xC8, 0x8A, 0x1D, 0xF0, 0x4A, 0xCE, 0xB2, 0x3A,
  27306. 0xBF, 0x2E, 0x40, 0x9E, 0xF3, 0x33, 0xC6, 0x22,
  27307. 0x28, 0x9C, 0x18, 0xA2, 0x13, 0x4E, 0x65, 0x0C,
  27308. 0x45, 0x25, 0x7E, 0x47, 0x47, 0x5F, 0xA3, 0x3A,
  27309. 0xA5, 0x37, 0xA5, 0xA8, 0xF7, 0x68, 0x02, 0x14,
  27310. 0x71, 0x6C, 0x50, 0xD4, 0x70, 0xE3, 0x28, 0x49,
  27311. 0x63, 0xCA, 0x64, 0xF5, 0x46, 0x77, 0xAE, 0xC5,
  27312. 0x4B, 0x52, 0x72, 0x16, 0x2B, 0xF5, 0x2B, 0xC8,
  27313. 0x14, 0x2E, 0x1D, 0x41, 0x83, 0xFC, 0x01, 0x74,
  27314. 0x54, 0xA6, 0xB5, 0xA4, 0x96, 0x83, 0x17, 0x59,
  27315. 0x06, 0x40, 0x24, 0x74, 0x59, 0x78, 0xCB, 0xD5,
  27316. 0x1A, 0x6C, 0xED, 0xC8, 0x95, 0x5D, 0xE4, 0xCC,
  27317. 0x6D, 0x36, 0x36, 0x70, 0xA4, 0x74, 0x66, 0xE8,
  27318. 0x2B, 0xE5, 0xC2, 0x36, 0x03, 0xA1, 0x7B, 0xF2,
  27319. 0x2A, 0xCD, 0xB7, 0xCC, 0x98, 0x4A, 0xF0, 0x8C,
  27320. 0x87, 0xE1, 0x4E, 0x27, 0x75, 0x3C, 0xF5, 0x87,
  27321. 0xA8, 0xEC, 0x34, 0x47, 0xE6, 0x2C, 0x64, 0x9E,
  27322. 0x88, 0x7A, 0x67, 0xC3, 0x6C, 0x9C, 0xE9, 0x87,
  27323. 0x21, 0xB6, 0x97, 0x21, 0x32, 0x75, 0x64, 0x6B,
  27324. 0x19, 0x4F, 0x36, 0x75, 0x86, 0x73, 0xA8, 0xED,
  27325. 0x11, 0x28, 0x44, 0x55, 0xAF, 0xC7, 0xA8, 0x52,
  27326. 0x9F, 0x69, 0xC9, 0x7A, 0x3C, 0x2D, 0x7B, 0x8C,
  27327. 0x63, 0x6C, 0x0B, 0xA5, 0x56, 0x14, 0xB7, 0x68,
  27328. 0xE6, 0x24, 0xE7, 0x12, 0x93, 0x0F, 0x77, 0x61,
  27329. 0x69, 0xB0, 0x17, 0x15, 0x72, 0x53, 0x51, 0xBC,
  27330. 0x74, 0xB4, 0x73, 0x95, 0xED, 0x52, 0xB2, 0x5A,
  27331. 0x13, 0x13, 0xC9, 0x51, 0x64, 0x81, 0x4C, 0x34,
  27332. 0xC9, 0x79, 0xCB, 0xDF, 0xAB, 0x85, 0x95, 0x46,
  27333. 0x62, 0xCA, 0xB4, 0x85, 0xE7, 0x50, 0x87, 0xA9,
  27334. 0x8C, 0xC7, 0x4B, 0xB8, 0x2C, 0xA2, 0xD1, 0xB5,
  27335. 0xBF, 0x28, 0x03, 0x23, 0x84, 0x80, 0x63, 0x8C,
  27336. 0x40, 0xE9, 0x0B, 0x43, 0xC7, 0x46, 0x0E, 0x7A,
  27337. 0xA9, 0x17, 0xF0, 0x10, 0x15, 0x1F, 0xAB, 0x11,
  27338. 0x69, 0x98, 0x7B, 0x37, 0x2A, 0xBB, 0x59, 0x27,
  27339. 0x1F, 0x70, 0x06, 0xC2, 0x4E, 0x60, 0x23, 0x6B,
  27340. 0x84, 0xB9, 0xDD, 0xD6, 0x00, 0x62, 0x37, 0x04,
  27341. 0x25, 0x46, 0x17, 0xFB, 0x49, 0x8D, 0x89, 0xE5,
  27342. 0x8B, 0x03, 0x68, 0xBC, 0xB2, 0x10, 0x3E, 0x79,
  27343. 0x35, 0x3E, 0xB5, 0x87, 0x86, 0x0C, 0x14, 0x22,
  27344. 0xE4, 0x76, 0x16, 0x2E, 0x42, 0x5B, 0xC2, 0x38,
  27345. 0x1D, 0xB8, 0x2C, 0x65, 0x92, 0x73, 0x7E, 0x1D,
  27346. 0xD6, 0x02, 0x86, 0x4B, 0x01, 0x67, 0xA7, 0x1E,
  27347. 0xC1, 0xF2, 0x23, 0x30, 0x5C, 0x02, 0xFE, 0x25,
  27348. 0x05, 0x2A, 0xF2, 0xB3, 0xB5, 0xA5, 0x5A, 0x0D,
  27349. 0x7A, 0x20, 0x22, 0xD9, 0xA7, 0x98, 0xDC, 0x0C,
  27350. 0x58, 0x74, 0xA9, 0x87, 0x02, 0xAA, 0xF4, 0x05,
  27351. 0x4C, 0x5D, 0x80, 0x33, 0x8A, 0x52, 0x48, 0xB5,
  27352. 0xB7, 0xBD, 0x09, 0xC5, 0x3B, 0x5E, 0x2A, 0x08,
  27353. 0x4B, 0x04, 0x7D, 0x27, 0x7A, 0x86, 0x1B, 0x1A,
  27354. 0x73, 0xBB, 0x51, 0x48, 0x8D, 0xE0, 0x4E, 0xF5,
  27355. 0x73, 0xC8, 0x52, 0x30, 0xA0, 0x47, 0x0B, 0x73,
  27356. 0x17, 0x5C, 0x9F, 0xA5, 0x05, 0x94, 0xF6, 0x6A,
  27357. 0x5F, 0x50, 0xB4, 0x15, 0x00, 0x54, 0xC9, 0x3B,
  27358. 0x68, 0x18, 0x6F, 0x8B, 0x5C, 0xBC, 0x49, 0x31,
  27359. 0x6C, 0x85, 0x48, 0xA6, 0x42, 0xB2, 0xB3, 0x6A,
  27360. 0x1D, 0x45, 0x4C, 0x74, 0x89, 0xAC, 0x33, 0xB2,
  27361. 0xD2, 0xCE, 0x66, 0x68, 0x09, 0x67, 0x82, 0xA2,
  27362. 0xC1, 0xE0, 0x86, 0x6D, 0x21, 0xA6, 0x5E, 0x16,
  27363. 0xB5, 0x85, 0xE7, 0xAF, 0x86, 0x18, 0xBD, 0xF3,
  27364. 0x18, 0x4C, 0x19, 0x86, 0x87, 0x85, 0x08, 0x91,
  27365. 0x72, 0x77, 0xB9, 0x3E, 0x10, 0x70, 0x6B, 0x16,
  27366. 0x14, 0x97, 0x2B, 0x2A, 0x94, 0xC7, 0x31, 0x0F,
  27367. 0xE9, 0xC7, 0x08, 0xC2, 0x31, 0xA1, 0xA8, 0xAC,
  27368. 0x8D, 0x93, 0x14, 0xA5, 0x29, 0xA9, 0x7F, 0x46,
  27369. 0x9B, 0xF6, 0x49, 0x62, 0xD8, 0x20, 0x64, 0x84,
  27370. 0x43, 0x09, 0x9A, 0x07, 0x6D, 0x55, 0xD4, 0xCE,
  27371. 0xA8, 0x24, 0xA5, 0x83, 0x04, 0x84, 0x4F, 0x99,
  27372. 0x49, 0x7C, 0x10, 0xA2, 0x51, 0x48, 0x61, 0x8A,
  27373. 0x31, 0x5D, 0x72, 0xCA, 0x85, 0x7D, 0x1B, 0x04,
  27374. 0xD5, 0x75, 0xB9, 0x4F, 0x85, 0xC0, 0x1D, 0x19,
  27375. 0xBE, 0xF2, 0x11, 0xBF, 0x0A, 0xA3, 0x36, 0x2E,
  27376. 0x70, 0x41, 0xFD, 0x16, 0x59, 0x6D, 0x80, 0x8E,
  27377. 0x86, 0x7B, 0x44, 0xC4, 0xC0, 0x0D, 0x1C, 0xDA,
  27378. 0x34, 0x18, 0x96, 0x77, 0x17, 0xF1, 0x47, 0xD0,
  27379. 0xEB, 0x21, 0xB4, 0x2A, 0xAE, 0xE7, 0x4A, 0xC3,
  27380. 0x5D, 0x0B, 0x92, 0x41, 0x4B, 0x95, 0x85, 0x31,
  27381. 0xAA, 0xDF, 0x46, 0x3E, 0xC6, 0x30, 0x5A, 0xE5,
  27382. 0xEC, 0xAF, 0x79, 0x17, 0x40, 0x02, 0xF2, 0x6D,
  27383. 0xDE, 0xCC, 0x81, 0x3B, 0xF3, 0x26, 0x72, 0xE8,
  27384. 0x52, 0x9D, 0x95, 0xA4, 0xE7, 0x30, 0xA7, 0xAB,
  27385. 0x4A, 0x3E, 0x8F, 0x8A, 0x8A, 0xF9, 0x79, 0xA6,
  27386. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  27387. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  27388. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  27389. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  27390. };
  27391. const byte kyber768_sk[] = {
  27392. 0x07, 0x63, 0x8F, 0xB6, 0x98, 0x68, 0xF3, 0xD3,
  27393. 0x20, 0xE5, 0x86, 0x2B, 0xD9, 0x69, 0x33, 0xFE,
  27394. 0xB3, 0x11, 0xB3, 0x62, 0x09, 0x3C, 0x9B, 0x5D,
  27395. 0x50, 0x17, 0x0B, 0xCE, 0xD4, 0x3F, 0x1B, 0x53,
  27396. 0x6D, 0x9A, 0x20, 0x4B, 0xB1, 0xF2, 0x26, 0x95,
  27397. 0x95, 0x0B, 0xA1, 0xF2, 0xA9, 0xE8, 0xEB, 0x82,
  27398. 0x8B, 0x28, 0x44, 0x88, 0x76, 0x0B, 0x3F, 0xC8,
  27399. 0x4F, 0xAB, 0xA0, 0x42, 0x75, 0xD5, 0x62, 0x8E,
  27400. 0x39, 0xC5, 0xB2, 0x47, 0x13, 0x74, 0x28, 0x3C,
  27401. 0x50, 0x32, 0x99, 0xC0, 0xAB, 0x49, 0xB6, 0x6B,
  27402. 0x8B, 0xBB, 0x56, 0xA4, 0x18, 0x66, 0x24, 0xF9,
  27403. 0x19, 0xA2, 0xBA, 0x59, 0xBB, 0x08, 0xD8, 0x55,
  27404. 0x18, 0x80, 0xC2, 0xBE, 0xFC, 0x4F, 0x87, 0xF2,
  27405. 0x5F, 0x59, 0xAB, 0x58, 0x7A, 0x79, 0xC3, 0x27,
  27406. 0xD7, 0x92, 0xD5, 0x4C, 0x97, 0x4A, 0x69, 0x26,
  27407. 0x2F, 0xF8, 0xA7, 0x89, 0x38, 0x28, 0x9E, 0x9A,
  27408. 0x87, 0xB6, 0x88, 0xB0, 0x83, 0xE0, 0x59, 0x5F,
  27409. 0xE2, 0x18, 0xB6, 0xBB, 0x15, 0x05, 0x94, 0x1C,
  27410. 0xE2, 0xE8, 0x1A, 0x5A, 0x64, 0xC5, 0xAA, 0xC6,
  27411. 0x04, 0x17, 0x25, 0x69, 0x85, 0x34, 0x9E, 0xE4,
  27412. 0x7A, 0x52, 0x42, 0x0A, 0x5F, 0x97, 0x47, 0x7B,
  27413. 0x72, 0x36, 0xAC, 0x76, 0xBC, 0x70, 0xE8, 0x28,
  27414. 0x87, 0x29, 0x28, 0x7E, 0xE3, 0xE3, 0x4A, 0x3D,
  27415. 0xBC, 0x36, 0x83, 0xC0, 0xB7, 0xB1, 0x00, 0x29,
  27416. 0xFC, 0x20, 0x34, 0x18, 0x53, 0x7E, 0x74, 0x66,
  27417. 0xBA, 0x63, 0x85, 0xA8, 0xFF, 0x30, 0x1E, 0xE1,
  27418. 0x27, 0x08, 0xF8, 0x2A, 0xAA, 0x1E, 0x38, 0x0F,
  27419. 0xC7, 0xA8, 0x8F, 0x8F, 0x20, 0x5A, 0xB7, 0xE8,
  27420. 0x8D, 0x7E, 0x95, 0x95, 0x2A, 0x55, 0xBA, 0x20,
  27421. 0xD0, 0x9B, 0x79, 0xA4, 0x71, 0x41, 0xD6, 0x2B,
  27422. 0xF6, 0xEB, 0x7D, 0xD3, 0x07, 0xB0, 0x8E, 0xCA,
  27423. 0x13, 0xA5, 0xBC, 0x5F, 0x6B, 0x68, 0x58, 0x1C,
  27424. 0x68, 0x65, 0xB2, 0x7B, 0xBC, 0xDD, 0xAB, 0x14,
  27425. 0x2F, 0x4B, 0x2C, 0xBF, 0xF4, 0x88, 0xC8, 0xA2,
  27426. 0x27, 0x05, 0xFA, 0xA9, 0x8A, 0x2B, 0x9E, 0xEA,
  27427. 0x35, 0x30, 0xC7, 0x66, 0x62, 0x33, 0x5C, 0xC7,
  27428. 0xEA, 0x3A, 0x00, 0x77, 0x77, 0x25, 0xEB, 0xCC,
  27429. 0xCD, 0x2A, 0x46, 0x36, 0xB2, 0xD9, 0x12, 0x2F,
  27430. 0xF3, 0xAB, 0x77, 0x12, 0x3C, 0xE0, 0x88, 0x3C,
  27431. 0x19, 0x11, 0x11, 0x5E, 0x50, 0xC9, 0xE8, 0xA9,
  27432. 0x41, 0x94, 0xE4, 0x8D, 0xD0, 0xD0, 0x9C, 0xFF,
  27433. 0xB3, 0xAD, 0xCD, 0x2C, 0x1E, 0x92, 0x43, 0x09,
  27434. 0x03, 0xD0, 0x7A, 0xDB, 0xF0, 0x05, 0x32, 0x03,
  27435. 0x15, 0x75, 0xAA, 0x7F, 0x9E, 0x7B, 0x5A, 0x1F,
  27436. 0x33, 0x62, 0xDE, 0xC9, 0x36, 0xD4, 0x04, 0x3C,
  27437. 0x05, 0xF2, 0x47, 0x6C, 0x07, 0x57, 0x8B, 0xC9,
  27438. 0xCB, 0xAF, 0x2A, 0xB4, 0xE3, 0x82, 0x72, 0x7A,
  27439. 0xD4, 0x16, 0x86, 0xA9, 0x6B, 0x25, 0x48, 0x82,
  27440. 0x0B, 0xB0, 0x3B, 0x32, 0xF1, 0x1B, 0x28, 0x11,
  27441. 0xAD, 0x62, 0xF4, 0x89, 0xE9, 0x51, 0x63, 0x2A,
  27442. 0xBA, 0x0D, 0x1D, 0xF8, 0x96, 0x80, 0xCC, 0x8A,
  27443. 0x8B, 0x53, 0xB4, 0x81, 0xD9, 0x2A, 0x68, 0xD7,
  27444. 0x0B, 0x4E, 0xA1, 0xC3, 0xA6, 0xA5, 0x61, 0xC0,
  27445. 0x69, 0x28, 0x82, 0xB5, 0xCA, 0x8C, 0xC9, 0x42,
  27446. 0xA8, 0xD4, 0x95, 0xAF, 0xCB, 0x06, 0xDE, 0x89,
  27447. 0x49, 0x8F, 0xB9, 0x35, 0xB7, 0x75, 0x90, 0x8F,
  27448. 0xE7, 0xA0, 0x3E, 0x32, 0x4D, 0x54, 0xCC, 0x19,
  27449. 0xD4, 0xE1, 0xAA, 0xBD, 0x35, 0x93, 0xB3, 0x8B,
  27450. 0x19, 0xEE, 0x13, 0x88, 0xFE, 0x49, 0x2B, 0x43,
  27451. 0x12, 0x7E, 0x5A, 0x50, 0x42, 0x53, 0x78, 0x6A,
  27452. 0x0D, 0x69, 0xAD, 0x32, 0x60, 0x1C, 0x28, 0xE2,
  27453. 0xC8, 0x85, 0x04, 0xA5, 0xBA, 0x59, 0x97, 0x06,
  27454. 0x02, 0x3A, 0x61, 0x36, 0x3E, 0x17, 0xC6, 0xB9,
  27455. 0xBB, 0x59, 0xBD, 0xC6, 0x97, 0x45, 0x2C, 0xD0,
  27456. 0x59, 0x45, 0x19, 0x83, 0xD7, 0x38, 0xCA, 0x3F,
  27457. 0xD0, 0x34, 0xE3, 0xF5, 0x98, 0x88, 0x54, 0xCA,
  27458. 0x05, 0x03, 0x1D, 0xB0, 0x96, 0x11, 0x49, 0x89,
  27459. 0x88, 0x19, 0x7C, 0x6B, 0x30, 0xD2, 0x58, 0xDF,
  27460. 0xE2, 0x62, 0x65, 0x54, 0x1C, 0x89, 0xA4, 0xB3,
  27461. 0x1D, 0x68, 0x64, 0xE9, 0x38, 0x9B, 0x03, 0xCB,
  27462. 0x74, 0xF7, 0xEC, 0x43, 0x23, 0xFB, 0x94, 0x21,
  27463. 0xA4, 0xB9, 0x79, 0x0A, 0x26, 0xD1, 0x7B, 0x03,
  27464. 0x98, 0xA2, 0x67, 0x67, 0x35, 0x09, 0x09, 0xF8,
  27465. 0x4D, 0x57, 0xB6, 0x69, 0x4D, 0xF8, 0x30, 0x66,
  27466. 0x4C, 0xA8, 0xB3, 0xC3, 0xC0, 0x3E, 0xD2, 0xAE,
  27467. 0x67, 0xB8, 0x90, 0x06, 0x86, 0x8A, 0x68, 0x52,
  27468. 0x7C, 0xCD, 0x66, 0x64, 0x59, 0xAB, 0x7F, 0x05,
  27469. 0x66, 0x71, 0x00, 0x0C, 0x61, 0x64, 0xD3, 0xA7,
  27470. 0xF2, 0x66, 0xA1, 0x4D, 0x97, 0xCB, 0xD7, 0x00,
  27471. 0x4D, 0x6C, 0x92, 0xCA, 0xCA, 0x77, 0x0B, 0x84,
  27472. 0x4A, 0x4F, 0xA9, 0xB1, 0x82, 0xE7, 0xB1, 0x8C,
  27473. 0xA8, 0x85, 0x08, 0x2A, 0xC5, 0x64, 0x6F, 0xCB,
  27474. 0x4A, 0x14, 0xE1, 0x68, 0x5F, 0xEB, 0x0C, 0x9C,
  27475. 0xE3, 0x37, 0x2A, 0xB9, 0x53, 0x65, 0xC0, 0x4F,
  27476. 0xD8, 0x30, 0x84, 0xF8, 0x0A, 0x23, 0xFF, 0x10,
  27477. 0xA0, 0x5B, 0xF1, 0x5F, 0x7F, 0xA5, 0xAC, 0xC6,
  27478. 0xC0, 0xCB, 0x46, 0x2C, 0x33, 0xCA, 0x52, 0x4F,
  27479. 0xA6, 0xB8, 0xBB, 0x35, 0x90, 0x43, 0xBA, 0x68,
  27480. 0x60, 0x9E, 0xAA, 0x25, 0x36, 0xE8, 0x1D, 0x08,
  27481. 0x46, 0x3B, 0x19, 0x65, 0x3B, 0x54, 0x35, 0xBA,
  27482. 0x94, 0x6C, 0x9A, 0xDD, 0xEB, 0x20, 0x2B, 0x04,
  27483. 0xB0, 0x31, 0xCC, 0x96, 0x0D, 0xCC, 0x12, 0xE4,
  27484. 0x51, 0x8D, 0x42, 0x8B, 0x32, 0xB2, 0x57, 0xA4,
  27485. 0xFC, 0x73, 0x13, 0xD3, 0xA7, 0x98, 0x0D, 0x80,
  27486. 0x08, 0x2E, 0x93, 0x4F, 0x9D, 0x95, 0xC3, 0x2B,
  27487. 0x0A, 0x01, 0x91, 0xA2, 0x36, 0x04, 0x38, 0x4D,
  27488. 0xD9, 0xE0, 0x79, 0xBB, 0xBA, 0xA2, 0x66, 0xD1,
  27489. 0x4C, 0x3F, 0x75, 0x6B, 0x9F, 0x21, 0x33, 0x10,
  27490. 0x74, 0x33, 0xA4, 0xE8, 0x3F, 0xA7, 0x18, 0x72,
  27491. 0x82, 0xA8, 0x09, 0x20, 0x3A, 0x4F, 0xAF, 0x84,
  27492. 0x18, 0x51, 0x83, 0x3D, 0x12, 0x1A, 0xC3, 0x83,
  27493. 0x84, 0x3A, 0x5E, 0x55, 0xBC, 0x23, 0x81, 0x42,
  27494. 0x5E, 0x16, 0xC7, 0xDB, 0x4C, 0xC9, 0xAB, 0x5C,
  27495. 0x1B, 0x0D, 0x91, 0xA4, 0x7E, 0x2B, 0x8D, 0xE0,
  27496. 0xE5, 0x82, 0xC8, 0x6B, 0x6B, 0x0D, 0x90, 0x7B,
  27497. 0xB3, 0x60, 0xB9, 0x7F, 0x40, 0xAB, 0x5D, 0x03,
  27498. 0x8F, 0x6B, 0x75, 0xC8, 0x14, 0xB2, 0x7D, 0x9B,
  27499. 0x96, 0x8D, 0x41, 0x98, 0x32, 0xBC, 0x8C, 0x2B,
  27500. 0xEE, 0x60, 0x5E, 0xF6, 0xE5, 0x05, 0x9D, 0x33,
  27501. 0x10, 0x0D, 0x90, 0x48, 0x5D, 0x37, 0x84, 0x50,
  27502. 0x01, 0x42, 0x21, 0x73, 0x6C, 0x07, 0x40, 0x7C,
  27503. 0xAC, 0x26, 0x04, 0x08, 0xAA, 0x64, 0x92, 0x66,
  27504. 0x19, 0x78, 0x8B, 0x86, 0x01, 0xC2, 0xA7, 0x52,
  27505. 0xD1, 0xA6, 0xCB, 0xF8, 0x20, 0xD7, 0xC7, 0xA0,
  27506. 0x47, 0x16, 0x20, 0x32, 0x25, 0xB3, 0x89, 0x5B,
  27507. 0x93, 0x42, 0xD1, 0x47, 0xA8, 0x18, 0x5C, 0xFC,
  27508. 0x1B, 0xB6, 0x5B, 0xA0, 0x6B, 0x41, 0x42, 0x33,
  27509. 0x99, 0x03, 0xC0, 0xAC, 0x46, 0x51, 0x38, 0x5B,
  27510. 0x45, 0xD9, 0x8A, 0x8B, 0x19, 0xD2, 0x8C, 0xD6,
  27511. 0xBA, 0xB0, 0x88, 0x78, 0x7F, 0x7E, 0xE1, 0xB1,
  27512. 0x24, 0x61, 0x76, 0x6B, 0x43, 0xCB, 0xCC, 0xB9,
  27513. 0x64, 0x34, 0x42, 0x7D, 0x93, 0xC0, 0x65, 0x55,
  27514. 0x06, 0x88, 0xF6, 0x94, 0x8E, 0xD1, 0xB5, 0x47,
  27515. 0x5A, 0x42, 0x5F, 0x1B, 0x85, 0x20, 0x9D, 0x06,
  27516. 0x1C, 0x08, 0xB5, 0x6C, 0x1C, 0xC0, 0x69, 0xF6,
  27517. 0xC0, 0xA7, 0xC6, 0xF2, 0x93, 0x58, 0xCA, 0xB9,
  27518. 0x11, 0x08, 0x77, 0x32, 0xA6, 0x49, 0xD2, 0x7C,
  27519. 0x9B, 0x98, 0xF9, 0xA4, 0x88, 0x79, 0x38, 0x7D,
  27520. 0x9B, 0x00, 0xC2, 0x59, 0x59, 0xA7, 0x16, 0x54,
  27521. 0xD6, 0xF6, 0xA9, 0x46, 0x16, 0x45, 0x13, 0xE4,
  27522. 0x7A, 0x75, 0xD0, 0x05, 0x98, 0x6C, 0x23, 0x63,
  27523. 0xC0, 0x9F, 0x6B, 0x53, 0x7E, 0xCA, 0x78, 0xB9,
  27524. 0x30, 0x3A, 0x5F, 0xA4, 0x57, 0x60, 0x8A, 0x58,
  27525. 0x6A, 0x65, 0x3A, 0x34, 0x7D, 0xB0, 0x4D, 0xFC,
  27526. 0xC1, 0x91, 0x75, 0xB3, 0xA3, 0x01, 0x17, 0x25,
  27527. 0x36, 0x06, 0x2A, 0x65, 0x8A, 0x95, 0x27, 0x75,
  27528. 0x70, 0xC8, 0x85, 0x2C, 0xA8, 0x97, 0x3F, 0x4A,
  27529. 0xE1, 0x23, 0xA3, 0x34, 0x04, 0x7D, 0xD7, 0x11,
  27530. 0xC8, 0x92, 0x7A, 0x63, 0x4A, 0x03, 0x38, 0x8A,
  27531. 0x52, 0x7B, 0x03, 0x4B, 0xF7, 0xA8, 0x17, 0x0F,
  27532. 0xA7, 0x02, 0xC1, 0xF7, 0xC2, 0x3E, 0xC3, 0x2D,
  27533. 0x18, 0xA2, 0x37, 0x48, 0x90, 0xBE, 0x9C, 0x78,
  27534. 0x7A, 0x94, 0x09, 0xC8, 0x2D, 0x19, 0x2C, 0x4B,
  27535. 0xB7, 0x05, 0xA2, 0xF9, 0x96, 0xCE, 0x40, 0x5D,
  27536. 0xA7, 0x2C, 0x2D, 0x9C, 0x84, 0x3E, 0xE9, 0xF8,
  27537. 0x31, 0x3E, 0xCC, 0x7F, 0x86, 0xD6, 0x29, 0x4D,
  27538. 0x59, 0x15, 0x9D, 0x9A, 0x87, 0x9A, 0x54, 0x2E,
  27539. 0x26, 0x09, 0x22, 0xAD, 0xF9, 0x99, 0x05, 0x1C,
  27540. 0xC4, 0x52, 0x00, 0xC9, 0xFF, 0xDB, 0x60, 0x44,
  27541. 0x9C, 0x49, 0x46, 0x59, 0x79, 0x27, 0x23, 0x67,
  27542. 0xC0, 0x83, 0xA7, 0xD6, 0x26, 0x7A, 0x3E, 0xD7,
  27543. 0xA7, 0xFD, 0x47, 0x95, 0x7C, 0x21, 0x93, 0x27,
  27544. 0xF7, 0xCA, 0x73, 0xA4, 0x00, 0x7E, 0x16, 0x27,
  27545. 0xF0, 0x0B, 0x11, 0xCC, 0x80, 0x57, 0x3C, 0x15,
  27546. 0xAE, 0xE6, 0x64, 0x0F, 0xB8, 0x56, 0x2D, 0xFA,
  27547. 0x6B, 0x24, 0x0C, 0xA0, 0xAD, 0x35, 0x1A, 0xC4,
  27548. 0xAC, 0x15, 0x5B, 0x96, 0xC1, 0x4C, 0x8A, 0xB1,
  27549. 0x3D, 0xD2, 0x62, 0xCD, 0xFD, 0x51, 0xC4, 0xBB,
  27550. 0x55, 0x72, 0xFD, 0x61, 0x65, 0x53, 0xD1, 0x7B,
  27551. 0xDD, 0x43, 0x0A, 0xCB, 0xEA, 0x3E, 0x95, 0xF0,
  27552. 0xB6, 0x98, 0xD6, 0x69, 0x90, 0xAB, 0x51, 0xE5,
  27553. 0xD0, 0x37, 0x83, 0xA8, 0xB3, 0xD2, 0x78, 0xA5,
  27554. 0x72, 0x04, 0x54, 0xCF, 0x96, 0x95, 0xCF, 0xDC,
  27555. 0xA0, 0x84, 0x85, 0xBA, 0x09, 0x9C, 0x51, 0xCD,
  27556. 0x92, 0xA7, 0xEA, 0x75, 0x87, 0xC1, 0xD1, 0x5C,
  27557. 0x28, 0xE6, 0x09, 0xA8, 0x18, 0x52, 0x60, 0x1B,
  27558. 0x06, 0x04, 0x01, 0x06, 0x79, 0xAA, 0x48, 0x2D,
  27559. 0x51, 0x26, 0x1E, 0xC3, 0x6E, 0x36, 0xB8, 0x71,
  27560. 0x96, 0x76, 0x21, 0x7F, 0xD7, 0x4C, 0x54, 0x78,
  27561. 0x64, 0x88, 0xF4, 0xB4, 0x96, 0x9C, 0x05, 0xA8,
  27562. 0xBA, 0x27, 0xCA, 0x3A, 0x77, 0xCC, 0xE7, 0x3B,
  27563. 0x96, 0x59, 0x23, 0xCA, 0x55, 0x4E, 0x42, 0x2B,
  27564. 0x9B, 0x61, 0xF4, 0x75, 0x46, 0x41, 0x60, 0x8A,
  27565. 0xC1, 0x6C, 0x9B, 0x85, 0x87, 0xA3, 0x2C, 0x1C,
  27566. 0x5D, 0xD7, 0x88, 0xF8, 0x8B, 0x36, 0xB7, 0x17,
  27567. 0xA4, 0x69, 0x65, 0x63, 0x5D, 0xEB, 0x67, 0xF4,
  27568. 0x5B, 0x12, 0x9B, 0x99, 0x07, 0x09, 0x09, 0xC9,
  27569. 0x3E, 0xB8, 0x0B, 0x42, 0xC2, 0xB3, 0xF3, 0xF7,
  27570. 0x03, 0x43, 0xA7, 0xCF, 0x37, 0xE8, 0x52, 0x0E,
  27571. 0x7B, 0xCF, 0xC4, 0x16, 0xAC, 0xA4, 0xF1, 0x8C,
  27572. 0x79, 0x81, 0x26, 0x2B, 0xA2, 0xBF, 0xC7, 0x56,
  27573. 0xAE, 0x03, 0x27, 0x8F, 0x0E, 0xC6, 0x6D, 0xC2,
  27574. 0x05, 0x76, 0x96, 0x82, 0x4B, 0xA6, 0x76, 0x98,
  27575. 0x65, 0xA6, 0x01, 0xD7, 0x14, 0x8E, 0xF6, 0xF5,
  27576. 0x4E, 0x5A, 0xF5, 0x68, 0x6A, 0xA2, 0x90, 0x6F,
  27577. 0x99, 0x4C, 0xE3, 0x8A, 0x5E, 0x0B, 0x93, 0x8F,
  27578. 0x23, 0x90, 0x07, 0x00, 0x30, 0x22, 0xC0, 0x33,
  27579. 0x92, 0xDF, 0x34, 0x01, 0xB1, 0xE4, 0xA3, 0xA7,
  27580. 0xEB, 0xC6, 0x16, 0x14, 0x49, 0xF7, 0x33, 0x74,
  27581. 0xC8, 0xB0, 0x14, 0x03, 0x69, 0x34, 0x3D, 0x92,
  27582. 0x95, 0xFD, 0xF5, 0x11, 0x84, 0x5C, 0x4A, 0x46,
  27583. 0xEB, 0xAA, 0xB6, 0xCA, 0x54, 0x92, 0xF6, 0x80,
  27584. 0x0B, 0x98, 0xC0, 0xCC, 0x80, 0x36, 0x53, 0xA4,
  27585. 0xB1, 0xD6, 0xE6, 0xAA, 0xED, 0x19, 0x32, 0xBA,
  27586. 0xCC, 0x5F, 0xEF, 0xAA, 0x81, 0x8B, 0xA5, 0x02,
  27587. 0x85, 0x9B, 0xA5, 0x49, 0x4C, 0x5F, 0x54, 0x02,
  27588. 0xC8, 0x53, 0x6A, 0x9C, 0x4C, 0x18, 0x88, 0x15,
  27589. 0x06, 0x17, 0xF8, 0x00, 0x98, 0xF6, 0xB2, 0xA9,
  27590. 0x9C, 0x39, 0xBC, 0x5D, 0xC7, 0xCF, 0x3B, 0x59,
  27591. 0x00, 0xA2, 0x13, 0x29, 0xAB, 0x59, 0x05, 0x3A,
  27592. 0xBA, 0xA6, 0x4E, 0xD1, 0x63, 0xE8, 0x59, 0xA8,
  27593. 0xB3, 0xB3, 0xCA, 0x33, 0x59, 0xB7, 0x50, 0xCC,
  27594. 0xC3, 0xE7, 0x10, 0xC7, 0xAC, 0x43, 0xC8, 0x19,
  27595. 0x1C, 0xB5, 0xD6, 0x88, 0x70, 0xC0, 0x63, 0x91,
  27596. 0xC0, 0xCB, 0x8A, 0xEC, 0x72, 0xB8, 0x97, 0xAC,
  27597. 0x6B, 0xE7, 0xFB, 0xAA, 0xCC, 0x67, 0x6E, 0xD6,
  27598. 0x63, 0x14, 0xC8, 0x36, 0x30, 0xE8, 0x94, 0x48,
  27599. 0xC8, 0x8A, 0x1D, 0xF0, 0x4A, 0xCE, 0xB2, 0x3A,
  27600. 0xBF, 0x2E, 0x40, 0x9E, 0xF3, 0x33, 0xC6, 0x22,
  27601. 0x28, 0x9C, 0x18, 0xA2, 0x13, 0x4E, 0x65, 0x0C,
  27602. 0x45, 0x25, 0x7E, 0x47, 0x47, 0x5F, 0xA3, 0x3A,
  27603. 0xA5, 0x37, 0xA5, 0xA8, 0xF7, 0x68, 0x02, 0x14,
  27604. 0x71, 0x6C, 0x50, 0xD4, 0x70, 0xE3, 0x28, 0x49,
  27605. 0x63, 0xCA, 0x64, 0xF5, 0x46, 0x77, 0xAE, 0xC5,
  27606. 0x4B, 0x52, 0x72, 0x16, 0x2B, 0xF5, 0x2B, 0xC8,
  27607. 0x14, 0x2E, 0x1D, 0x41, 0x83, 0xFC, 0x01, 0x74,
  27608. 0x54, 0xA6, 0xB5, 0xA4, 0x96, 0x83, 0x17, 0x59,
  27609. 0x06, 0x40, 0x24, 0x74, 0x59, 0x78, 0xCB, 0xD5,
  27610. 0x1A, 0x6C, 0xED, 0xC8, 0x95, 0x5D, 0xE4, 0xCC,
  27611. 0x6D, 0x36, 0x36, 0x70, 0xA4, 0x74, 0x66, 0xE8,
  27612. 0x2B, 0xE5, 0xC2, 0x36, 0x03, 0xA1, 0x7B, 0xF2,
  27613. 0x2A, 0xCD, 0xB7, 0xCC, 0x98, 0x4A, 0xF0, 0x8C,
  27614. 0x87, 0xE1, 0x4E, 0x27, 0x75, 0x3C, 0xF5, 0x87,
  27615. 0xA8, 0xEC, 0x34, 0x47, 0xE6, 0x2C, 0x64, 0x9E,
  27616. 0x88, 0x7A, 0x67, 0xC3, 0x6C, 0x9C, 0xE9, 0x87,
  27617. 0x21, 0xB6, 0x97, 0x21, 0x32, 0x75, 0x64, 0x6B,
  27618. 0x19, 0x4F, 0x36, 0x75, 0x86, 0x73, 0xA8, 0xED,
  27619. 0x11, 0x28, 0x44, 0x55, 0xAF, 0xC7, 0xA8, 0x52,
  27620. 0x9F, 0x69, 0xC9, 0x7A, 0x3C, 0x2D, 0x7B, 0x8C,
  27621. 0x63, 0x6C, 0x0B, 0xA5, 0x56, 0x14, 0xB7, 0x68,
  27622. 0xE6, 0x24, 0xE7, 0x12, 0x93, 0x0F, 0x77, 0x61,
  27623. 0x69, 0xB0, 0x17, 0x15, 0x72, 0x53, 0x51, 0xBC,
  27624. 0x74, 0xB4, 0x73, 0x95, 0xED, 0x52, 0xB2, 0x5A,
  27625. 0x13, 0x13, 0xC9, 0x51, 0x64, 0x81, 0x4C, 0x34,
  27626. 0xC9, 0x79, 0xCB, 0xDF, 0xAB, 0x85, 0x95, 0x46,
  27627. 0x62, 0xCA, 0xB4, 0x85, 0xE7, 0x50, 0x87, 0xA9,
  27628. 0x8C, 0xC7, 0x4B, 0xB8, 0x2C, 0xA2, 0xD1, 0xB5,
  27629. 0xBF, 0x28, 0x03, 0x23, 0x84, 0x80, 0x63, 0x8C,
  27630. 0x40, 0xE9, 0x0B, 0x43, 0xC7, 0x46, 0x0E, 0x7A,
  27631. 0xA9, 0x17, 0xF0, 0x10, 0x15, 0x1F, 0xAB, 0x11,
  27632. 0x69, 0x98, 0x7B, 0x37, 0x2A, 0xBB, 0x59, 0x27,
  27633. 0x1F, 0x70, 0x06, 0xC2, 0x4E, 0x60, 0x23, 0x6B,
  27634. 0x84, 0xB9, 0xDD, 0xD6, 0x00, 0x62, 0x37, 0x04,
  27635. 0x25, 0x46, 0x17, 0xFB, 0x49, 0x8D, 0x89, 0xE5,
  27636. 0x8B, 0x03, 0x68, 0xBC, 0xB2, 0x10, 0x3E, 0x79,
  27637. 0x35, 0x3E, 0xB5, 0x87, 0x86, 0x0C, 0x14, 0x22,
  27638. 0xE4, 0x76, 0x16, 0x2E, 0x42, 0x5B, 0xC2, 0x38,
  27639. 0x1D, 0xB8, 0x2C, 0x65, 0x92, 0x73, 0x7E, 0x1D,
  27640. 0xD6, 0x02, 0x86, 0x4B, 0x01, 0x67, 0xA7, 0x1E,
  27641. 0xC1, 0xF2, 0x23, 0x30, 0x5C, 0x02, 0xFE, 0x25,
  27642. 0x05, 0x2A, 0xF2, 0xB3, 0xB5, 0xA5, 0x5A, 0x0D,
  27643. 0x7A, 0x20, 0x22, 0xD9, 0xA7, 0x98, 0xDC, 0x0C,
  27644. 0x58, 0x74, 0xA9, 0x87, 0x02, 0xAA, 0xF4, 0x05,
  27645. 0x4C, 0x5D, 0x80, 0x33, 0x8A, 0x52, 0x48, 0xB5,
  27646. 0xB7, 0xBD, 0x09, 0xC5, 0x3B, 0x5E, 0x2A, 0x08,
  27647. 0x4B, 0x04, 0x7D, 0x27, 0x7A, 0x86, 0x1B, 0x1A,
  27648. 0x73, 0xBB, 0x51, 0x48, 0x8D, 0xE0, 0x4E, 0xF5,
  27649. 0x73, 0xC8, 0x52, 0x30, 0xA0, 0x47, 0x0B, 0x73,
  27650. 0x17, 0x5C, 0x9F, 0xA5, 0x05, 0x94, 0xF6, 0x6A,
  27651. 0x5F, 0x50, 0xB4, 0x15, 0x00, 0x54, 0xC9, 0x3B,
  27652. 0x68, 0x18, 0x6F, 0x8B, 0x5C, 0xBC, 0x49, 0x31,
  27653. 0x6C, 0x85, 0x48, 0xA6, 0x42, 0xB2, 0xB3, 0x6A,
  27654. 0x1D, 0x45, 0x4C, 0x74, 0x89, 0xAC, 0x33, 0xB2,
  27655. 0xD2, 0xCE, 0x66, 0x68, 0x09, 0x67, 0x82, 0xA2,
  27656. 0xC1, 0xE0, 0x86, 0x6D, 0x21, 0xA6, 0x5E, 0x16,
  27657. 0xB5, 0x85, 0xE7, 0xAF, 0x86, 0x18, 0xBD, 0xF3,
  27658. 0x18, 0x4C, 0x19, 0x86, 0x87, 0x85, 0x08, 0x91,
  27659. 0x72, 0x77, 0xB9, 0x3E, 0x10, 0x70, 0x6B, 0x16,
  27660. 0x14, 0x97, 0x2B, 0x2A, 0x94, 0xC7, 0x31, 0x0F,
  27661. 0xE9, 0xC7, 0x08, 0xC2, 0x31, 0xA1, 0xA8, 0xAC,
  27662. 0x8D, 0x93, 0x14, 0xA5, 0x29, 0xA9, 0x7F, 0x46,
  27663. 0x9B, 0xF6, 0x49, 0x62, 0xD8, 0x20, 0x64, 0x84,
  27664. 0x43, 0x09, 0x9A, 0x07, 0x6D, 0x55, 0xD4, 0xCE,
  27665. 0xA8, 0x24, 0xA5, 0x83, 0x04, 0x84, 0x4F, 0x99,
  27666. 0x49, 0x7C, 0x10, 0xA2, 0x51, 0x48, 0x61, 0x8A,
  27667. 0x31, 0x5D, 0x72, 0xCA, 0x85, 0x7D, 0x1B, 0x04,
  27668. 0xD5, 0x75, 0xB9, 0x4F, 0x85, 0xC0, 0x1D, 0x19,
  27669. 0xBE, 0xF2, 0x11, 0xBF, 0x0A, 0xA3, 0x36, 0x2E,
  27670. 0x70, 0x41, 0xFD, 0x16, 0x59, 0x6D, 0x80, 0x8E,
  27671. 0x86, 0x7B, 0x44, 0xC4, 0xC0, 0x0D, 0x1C, 0xDA,
  27672. 0x34, 0x18, 0x96, 0x77, 0x17, 0xF1, 0x47, 0xD0,
  27673. 0xEB, 0x21, 0xB4, 0x2A, 0xAE, 0xE7, 0x4A, 0xC3,
  27674. 0x5D, 0x0B, 0x92, 0x41, 0x4B, 0x95, 0x85, 0x31,
  27675. 0xAA, 0xDF, 0x46, 0x3E, 0xC6, 0x30, 0x5A, 0xE5,
  27676. 0xEC, 0xAF, 0x79, 0x17, 0x40, 0x02, 0xF2, 0x6D,
  27677. 0xDE, 0xCC, 0x81, 0x3B, 0xF3, 0x26, 0x72, 0xE8,
  27678. 0x52, 0x9D, 0x95, 0xA4, 0xE7, 0x30, 0xA7, 0xAB,
  27679. 0x4A, 0x3E, 0x8F, 0x8A, 0x8A, 0xF9, 0x79, 0xA6,
  27680. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  27681. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  27682. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  27683. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  27684. 0xD4, 0xEC, 0x14, 0x3B, 0x50, 0xF0, 0x14, 0x23,
  27685. 0xB1, 0x77, 0x89, 0x5E, 0xDE, 0xE2, 0x2B, 0xB7,
  27686. 0x39, 0xF6, 0x47, 0xEC, 0xF8, 0x5F, 0x50, 0xBC,
  27687. 0x25, 0xEF, 0x7B, 0x5A, 0x72, 0x5D, 0xEE, 0x86,
  27688. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  27689. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  27690. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  27691. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  27692. };
  27693. const byte kyber768_ct[] = {
  27694. 0xB5, 0x2C, 0x56, 0xB9, 0x2A, 0x4B, 0x7C, 0xE9,
  27695. 0xE4, 0xCB, 0x7C, 0x5B, 0x1B, 0x16, 0x31, 0x67,
  27696. 0xA8, 0xA1, 0x67, 0x5B, 0x2F, 0xDE, 0xF8, 0x4A,
  27697. 0x5B, 0x67, 0xCA, 0x15, 0xDB, 0x69, 0x4C, 0x9F,
  27698. 0x11, 0xBD, 0x02, 0x7C, 0x30, 0xAE, 0x22, 0xEC,
  27699. 0x92, 0x1A, 0x1D, 0x91, 0x15, 0x99, 0xAF, 0x05,
  27700. 0x85, 0xE4, 0x8D, 0x20, 0xDA, 0x70, 0xDF, 0x9F,
  27701. 0x39, 0xE3, 0x2E, 0xF9, 0x5D, 0x4C, 0x8F, 0x44,
  27702. 0xBF, 0xEF, 0xDA, 0xA5, 0xDA, 0x64, 0xF1, 0x05,
  27703. 0x46, 0x31, 0xD0, 0x4D, 0x6D, 0x3C, 0xFD, 0x0A,
  27704. 0x54, 0x0D, 0xD7, 0xBA, 0x38, 0x86, 0xE4, 0xB5,
  27705. 0xF1, 0x3E, 0x87, 0x87, 0x88, 0x60, 0x4C, 0x95,
  27706. 0xC0, 0x96, 0xEA, 0xB3, 0x91, 0x9F, 0x42, 0x75,
  27707. 0x21, 0x41, 0x9A, 0x94, 0x6C, 0x26, 0xCC, 0x04,
  27708. 0x14, 0x75, 0xD7, 0x12, 0x4C, 0xDC, 0x01, 0xD0,
  27709. 0x37, 0x3E, 0x5B, 0x09, 0xC7, 0xA7, 0x06, 0x03,
  27710. 0xCF, 0xDB, 0x4F, 0xB3, 0x40, 0x50, 0x23, 0xF2,
  27711. 0x26, 0x4D, 0xC3, 0xF9, 0x83, 0xC4, 0xFC, 0x02,
  27712. 0xA2, 0xD1, 0xB2, 0x68, 0xF2, 0x20, 0x8A, 0x1F,
  27713. 0x6E, 0x2A, 0x62, 0x09, 0xBF, 0xF1, 0x2F, 0x6F,
  27714. 0x46, 0x5F, 0x0B, 0x06, 0x9C, 0x3A, 0x7F, 0x84,
  27715. 0xF6, 0x06, 0xD8, 0xA9, 0x40, 0x64, 0x00, 0x3D,
  27716. 0x6E, 0xC1, 0x14, 0xC8, 0xE8, 0x08, 0xD3, 0x05,
  27717. 0x38, 0x84, 0xC1, 0xD5, 0xA1, 0x42, 0xFB, 0xF2,
  27718. 0x01, 0x12, 0xEB, 0x36, 0x0F, 0xDA, 0x3F, 0x0F,
  27719. 0x28, 0xB1, 0x72, 0xAE, 0x50, 0xF5, 0xE7, 0xD8,
  27720. 0x38, 0x01, 0xFB, 0x3F, 0x00, 0x64, 0xB6, 0x87,
  27721. 0x18, 0x70, 0x74, 0xBD, 0x7F, 0xE3, 0x0E, 0xDD,
  27722. 0xAA, 0x33, 0x4C, 0xF8, 0xFC, 0x04, 0xFA, 0x8C,
  27723. 0xED, 0x89, 0x9C, 0xEA, 0xDE, 0x4B, 0x4F, 0x28,
  27724. 0xB6, 0x83, 0x72, 0xBA, 0xF9, 0x8F, 0xF4, 0x82,
  27725. 0xA4, 0x15, 0xB7, 0x31, 0x15, 0x5B, 0x75, 0xCE,
  27726. 0xB9, 0x76, 0xBE, 0x0E, 0xA0, 0x28, 0x5B, 0xA0,
  27727. 0x1A, 0x27, 0xF1, 0x85, 0x7A, 0x8F, 0xB3, 0x77,
  27728. 0xA3, 0xAE, 0x0C, 0x23, 0xB2, 0xAA, 0x9A, 0x07,
  27729. 0x9B, 0xFA, 0xBF, 0xF0, 0xD5, 0xB2, 0xF1, 0xCD,
  27730. 0x9B, 0x71, 0x8B, 0xEA, 0x03, 0xC4, 0x2F, 0x34,
  27731. 0x3A, 0x39, 0xB4, 0xF1, 0x42, 0xD0, 0x1A, 0xD8,
  27732. 0xAC, 0xBB, 0x50, 0xE3, 0x88, 0x53, 0xCF, 0x9A,
  27733. 0x50, 0xC8, 0xB4, 0x4C, 0x3C, 0xF6, 0x71, 0xA4,
  27734. 0xA9, 0x04, 0x3B, 0x26, 0xDD, 0xBB, 0x24, 0x95,
  27735. 0x9A, 0xD6, 0x71, 0x5C, 0x08, 0x52, 0x18, 0x55,
  27736. 0xC7, 0x9A, 0x23, 0xB9, 0xC3, 0xD6, 0x47, 0x17,
  27737. 0x49, 0xC4, 0x07, 0x25, 0xBD, 0xD5, 0xC2, 0x77,
  27738. 0x6D, 0x43, 0xAE, 0xD2, 0x02, 0x04, 0xBA, 0xA1,
  27739. 0x41, 0xEF, 0xB3, 0x30, 0x49, 0x17, 0x47, 0x4B,
  27740. 0x7F, 0x9F, 0x7A, 0x4B, 0x08, 0xB1, 0xA9, 0x3D,
  27741. 0xAE, 0xD9, 0x8C, 0x67, 0x49, 0x53, 0x59, 0xD3,
  27742. 0x7D, 0x67, 0xF7, 0x43, 0x8B, 0xEE, 0x5E, 0x43,
  27743. 0x58, 0x56, 0x34, 0xB2, 0x6C, 0x6B, 0x38, 0x10,
  27744. 0xD7, 0xCD, 0xCB, 0xC0, 0xF6, 0xEB, 0x87, 0x7A,
  27745. 0x60, 0x87, 0xE6, 0x8A, 0xCB, 0x84, 0x80, 0xD3,
  27746. 0xA8, 0xCF, 0x69, 0x00, 0x44, 0x7E, 0x49, 0xB4,
  27747. 0x17, 0xF1, 0x5A, 0x53, 0xB6, 0x07, 0xA0, 0xE2,
  27748. 0x16, 0xB8, 0x55, 0x97, 0x0D, 0x37, 0x40, 0x68,
  27749. 0x70, 0xB4, 0x56, 0x87, 0x22, 0xDA, 0x77, 0xA4,
  27750. 0x08, 0x47, 0x03, 0x81, 0x67, 0x84, 0xE2, 0xF1,
  27751. 0x6B, 0xED, 0x18, 0x99, 0x65, 0x32, 0xC5, 0xD8,
  27752. 0xB7, 0xF5, 0xD2, 0x14, 0x46, 0x4E, 0x5F, 0x3F,
  27753. 0x6E, 0x90, 0x58, 0x67, 0xB0, 0xCE, 0x11, 0x9E,
  27754. 0x25, 0x2A, 0x66, 0x71, 0x32, 0x53, 0x54, 0x46,
  27755. 0x85, 0xD2, 0x08, 0xE1, 0x72, 0x39, 0x08, 0xA0,
  27756. 0xCE, 0x97, 0x83, 0x46, 0x52, 0xE0, 0x8A, 0xE7,
  27757. 0xBD, 0xC8, 0x81, 0xA1, 0x31, 0xB7, 0x3C, 0x71,
  27758. 0xE8, 0x4D, 0x20, 0xD6, 0x8F, 0xDE, 0xFF, 0x4F,
  27759. 0x5D, 0x70, 0xCD, 0x1A, 0xF5, 0x7B, 0x78, 0xE3,
  27760. 0x49, 0x1A, 0x98, 0x65, 0x94, 0x23, 0x21, 0x80,
  27761. 0x0A, 0x20, 0x3C, 0x05, 0xED, 0x1F, 0xEE, 0xB5,
  27762. 0xA2, 0x8E, 0x58, 0x4E, 0x19, 0xF6, 0x53, 0x5E,
  27763. 0x7F, 0x84, 0xE4, 0xA2, 0x4F, 0x84, 0xA7, 0x2D,
  27764. 0xCA, 0xF5, 0x64, 0x8B, 0x4A, 0x42, 0x35, 0xDD,
  27765. 0x66, 0x44, 0x64, 0x48, 0x2F, 0x03, 0x17, 0x6E,
  27766. 0x88, 0x8C, 0x28, 0xBF, 0xC6, 0xC1, 0xCB, 0x23,
  27767. 0x8C, 0xFF, 0xA3, 0x5A, 0x32, 0x1E, 0x71, 0x79,
  27768. 0x1D, 0x9E, 0xA8, 0xED, 0x08, 0x78, 0xC6, 0x11,
  27769. 0x21, 0xBF, 0x8D, 0x2A, 0x4A, 0xB2, 0xC1, 0xA5,
  27770. 0xE1, 0x20, 0xBC, 0x40, 0xAB, 0xB1, 0x89, 0x2D,
  27771. 0x17, 0x15, 0x09, 0x0A, 0x0E, 0xE4, 0x82, 0x52,
  27772. 0xCA, 0x29, 0x7A, 0x99, 0xAA, 0x0E, 0x51, 0x0C,
  27773. 0xF2, 0x6B, 0x1A, 0xDD, 0x06, 0xCA, 0x54, 0x3E,
  27774. 0x1C, 0x5D, 0x6B, 0xDC, 0xD3, 0xB9, 0xC5, 0x85,
  27775. 0xC8, 0x53, 0x80, 0x45, 0xDB, 0x5C, 0x25, 0x2E,
  27776. 0xC3, 0xC8, 0xC3, 0xC9, 0x54, 0xD9, 0xBE, 0x59,
  27777. 0x07, 0x09, 0x4A, 0x89, 0x4E, 0x60, 0xEA, 0xB4,
  27778. 0x35, 0x38, 0xCF, 0xEE, 0x82, 0xE8, 0xFF, 0xC0,
  27779. 0x79, 0x1B, 0x0D, 0x0F, 0x43, 0xAC, 0x16, 0x27,
  27780. 0x83, 0x0A, 0x61, 0xD5, 0x6D, 0xAD, 0x96, 0xC6,
  27781. 0x29, 0x58, 0xB0, 0xDE, 0x78, 0x0B, 0x78, 0xBD,
  27782. 0x47, 0xA6, 0x04, 0x55, 0x0D, 0xAB, 0x83, 0xFF,
  27783. 0xF2, 0x27, 0xC3, 0x24, 0x04, 0x94, 0x71, 0xF3,
  27784. 0x52, 0x48, 0xCF, 0xB8, 0x49, 0xB2, 0x57, 0x24,
  27785. 0xFF, 0x70, 0x4D, 0x52, 0x77, 0xAA, 0x35, 0x2D,
  27786. 0x55, 0x09, 0x58, 0xBE, 0x3B, 0x23, 0x7D, 0xFF,
  27787. 0x47, 0x3E, 0xC2, 0xAD, 0xBA, 0xEA, 0x48, 0xCA,
  27788. 0x26, 0x58, 0xAE, 0xFC, 0xC7, 0x7B, 0xBD, 0x42,
  27789. 0x64, 0xAB, 0x37, 0x4D, 0x70, 0xEA, 0xE5, 0xB9,
  27790. 0x64, 0x41, 0x6C, 0xE8, 0x22, 0x6A, 0x7E, 0x32,
  27791. 0x55, 0xA0, 0xF8, 0xD7, 0xE2, 0xAD, 0xCA, 0x06,
  27792. 0x2B, 0xCD, 0x6D, 0x78, 0xD6, 0x0D, 0x1B, 0x32,
  27793. 0xE1, 0x14, 0x05, 0xBE, 0x54, 0xB6, 0x6E, 0xF0,
  27794. 0xFD, 0xDD, 0x56, 0x77, 0x02, 0xA3, 0xBC, 0xCF,
  27795. 0xED, 0xE3, 0xC5, 0x84, 0x70, 0x12, 0x69, 0xED,
  27796. 0x14, 0x80, 0x9F, 0x06, 0xF8, 0x96, 0x83, 0x56,
  27797. 0xBB, 0x92, 0x67, 0xFE, 0x86, 0xE5, 0x14, 0x25,
  27798. 0x2E, 0x88, 0xBB, 0x5C, 0x30, 0xA7, 0xEC, 0xB3,
  27799. 0xD0, 0xE6, 0x21, 0x02, 0x1E, 0xE0, 0xFB, 0xF7,
  27800. 0x87, 0x1B, 0x09, 0x34, 0x2B, 0xF8, 0x4F, 0x55,
  27801. 0xC9, 0x7E, 0xAF, 0x86, 0xC4, 0x81, 0x89, 0xC7,
  27802. 0xFF, 0x4D, 0xF3, 0x89, 0xF0, 0x77, 0xE2, 0x80,
  27803. 0x6E, 0x5F, 0xA7, 0x3B, 0x3E, 0x94, 0x58, 0xA1,
  27804. 0x6C, 0x7E, 0x27, 0x5F, 0x4F, 0x60, 0x22, 0x75,
  27805. 0x58, 0x0E, 0xB7, 0xB7, 0x13, 0x5F, 0xB5, 0x37,
  27806. 0xFA, 0x0C, 0xD9, 0x5D, 0x6E, 0xA5, 0x8C, 0x10,
  27807. 0x8C, 0xD8, 0x94, 0x3D, 0x70, 0xC1, 0x64, 0x31,
  27808. 0x11, 0xF4, 0xF0, 0x1C, 0xA8, 0xA8, 0x27, 0x6A,
  27809. 0x90, 0x26, 0x66, 0xED, 0x81, 0xB7, 0x8D, 0x16,
  27810. 0x8B, 0x00, 0x6F, 0x16, 0xAA, 0xA3, 0xD8, 0xE4,
  27811. 0xCE, 0x4F, 0x4D, 0x0F, 0xB0, 0x99, 0x7E, 0x41,
  27812. 0xAE, 0xFF, 0xB5, 0xB3, 0xDA, 0xA8, 0x38, 0x73,
  27813. 0x2F, 0x35, 0x73, 0x49, 0x44, 0x7F, 0x38, 0x77,
  27814. 0x76, 0xC7, 0x93, 0xC0, 0x47, 0x9D, 0xE9, 0xE9,
  27815. 0x94, 0x98, 0xCC, 0x35, 0x6F, 0xDB, 0x00, 0x75,
  27816. 0xA7, 0x03, 0xF2, 0x3C, 0x55, 0xD4, 0x7B, 0x55,
  27817. 0x0E, 0xC8, 0x9B, 0x02, 0xAD, 0xE8, 0x93, 0x29,
  27818. 0x08, 0x6A, 0x50, 0x84, 0x34, 0x56, 0xFE, 0xDC,
  27819. 0x37, 0x88, 0xAC, 0x8D, 0x97, 0x23, 0x3C, 0x54,
  27820. 0x56, 0x04, 0x67, 0xEE, 0x1D, 0x0F, 0x02, 0x4B,
  27821. 0x18, 0x42, 0x8F, 0x0D, 0x73, 0xB3, 0x0E, 0x19,
  27822. 0xF5, 0xC6, 0x3B, 0x9A, 0xBF, 0x11, 0x41, 0x5B,
  27823. 0xEA, 0x4D, 0x01, 0x70, 0x13, 0x0B, 0xAA, 0xBD,
  27824. 0x33, 0xC0, 0x5E, 0x65, 0x24, 0xE5, 0xFB, 0x55,
  27825. 0x81, 0xB2, 0x2B, 0x04, 0x33, 0x34, 0x22, 0x48,
  27826. 0x26, 0x6D, 0x0F, 0x10, 0x53, 0xB2, 0x45, 0xCC,
  27827. 0x24, 0x62, 0xDC, 0x44, 0xD3, 0x49, 0x65, 0x10,
  27828. 0x24, 0x82, 0xA8, 0xED, 0x9E, 0x4E, 0x96, 0x4D,
  27829. 0x56, 0x83, 0xE5, 0xD4, 0x5D, 0x0C, 0x82, 0x69
  27830. };
  27831. const byte kyber768_ss[] = {
  27832. 0x91, 0x4C, 0xB6, 0x7F, 0xE5, 0xC3, 0x8E, 0x73,
  27833. 0xBF, 0x74, 0x18, 0x1C, 0x0A, 0xC5, 0x04, 0x28,
  27834. 0xDE, 0xDF, 0x77, 0x50, 0xA9, 0x80, 0x58, 0xF7,
  27835. 0xD5, 0x36, 0x70, 0x87, 0x74, 0x53, 0x5B, 0x29
  27836. };
  27837. ret = wc_KyberKey_Init(KYBER768, &key, HEAP_HINT, INVALID_DEVID);
  27838. if (ret != 0)
  27839. return -20201;
  27840. ret = wc_KyberKey_MakeKeyWithRandom(&key, kyber768_rand,
  27841. sizeof(kyber768_rand));
  27842. if (ret != 0)
  27843. return -20202;
  27844. ret = wc_KyberKey_EncodePublicKey(&key, pub, sizeof(pub));
  27845. if (ret != 0)
  27846. return -20203;
  27847. ret = wc_KyberKey_EncodePrivateKey(&key, priv, sizeof(priv));
  27848. if (ret != 0)
  27849. return -20204;
  27850. if (XMEMCMP(pub, kyber768_pk, sizeof(kyber768_pk)) != 0)
  27851. return -20205;
  27852. if (XMEMCMP(priv, kyber768_sk, sizeof(kyber768_sk)) != 0)
  27853. return -20206;
  27854. ret = wc_KyberKey_EncapsulateWithRandom(&key, ct, ss, kyber768enc_rand,
  27855. sizeof(kyber768enc_rand));
  27856. if (ret != 0)
  27857. return -20207;
  27858. if (XMEMCMP(ct, kyber768_ct, sizeof(kyber768_ct)) != 0)
  27859. return -20208;
  27860. if (XMEMCMP(ss, kyber768_ss, sizeof(kyber768_ss)) != 0)
  27861. return -20209;
  27862. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, sizeof(kyber768_ct));
  27863. if (ret != 0)
  27864. return -20210;
  27865. if (XMEMCMP(ss_dec, kyber768_ss, sizeof(kyber768_ss)) != 0)
  27866. return -20211;
  27867. wc_KyberKey_Free(&key);
  27868. return 0;
  27869. }
  27870. #endif /* WOLFSSL_KYBER768 */
  27871. #ifdef WOLFSSL_KYBER1024
  27872. static int kyber1024_kat(void)
  27873. {
  27874. KyberKey key;
  27875. int ret;
  27876. byte priv[KYBER1024_PRIVATE_KEY_SIZE];
  27877. byte pub[KYBER1024_PUBLIC_KEY_SIZE];
  27878. byte ct[KYBER1024_CIPHER_TEXT_SIZE];
  27879. byte ss[KYBER_SS_SZ];
  27880. byte ss_dec[KYBER_SS_SZ];
  27881. const byte kyber1024_rand[] = {
  27882. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  27883. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  27884. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  27885. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  27886. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  27887. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  27888. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  27889. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  27890. };
  27891. const byte kyber1024enc_rand[] = {
  27892. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  27893. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  27894. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  27895. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  27896. };
  27897. const byte kyber1024_pk[] = {
  27898. 0xD2, 0x23, 0x02, 0xCB, 0xD3, 0x39, 0x9F, 0xAC,
  27899. 0xC6, 0x30, 0x99, 0x1F, 0xC8, 0xF2, 0x8B, 0xDB,
  27900. 0x43, 0x54, 0x76, 0x25, 0x41, 0x52, 0x76, 0x78,
  27901. 0xBC, 0xF6, 0x1F, 0x65, 0xC2, 0x41, 0x14, 0x6C,
  27902. 0x42, 0x6D, 0x23, 0xB9, 0xBF, 0xAA, 0x6B, 0x7D,
  27903. 0xF1, 0x8C, 0x97, 0xF2, 0x0C, 0x1B, 0x61, 0x25,
  27904. 0xBF, 0x87, 0x4B, 0x1D, 0x89, 0x47, 0x58, 0x52,
  27905. 0xC4, 0x48, 0x21, 0x5D, 0xB0, 0xEB, 0x77, 0x37,
  27906. 0xF9, 0x14, 0x80, 0xE8, 0xCE, 0xBD, 0x9A, 0x08,
  27907. 0x71, 0x57, 0x4F, 0x5A, 0xB6, 0x2D, 0x90, 0x20,
  27908. 0x17, 0x5E, 0xC6, 0x92, 0x7C, 0xA0, 0xB5, 0x4C,
  27909. 0x09, 0x81, 0x8E, 0x42, 0xCF, 0x92, 0xA3, 0x83,
  27910. 0x17, 0x24, 0x22, 0xC7, 0xDC, 0x18, 0x31, 0xD6,
  27911. 0x3B, 0x0C, 0x29, 0x5D, 0xE7, 0x51, 0x59, 0xDB,
  27912. 0x80, 0x34, 0xE9, 0xE0, 0x7F, 0x7B, 0x0B, 0x91,
  27913. 0x0C, 0x3C, 0x1E, 0x5F, 0xB6, 0x6B, 0x3D, 0xC5,
  27914. 0x23, 0xF1, 0xFA, 0x6E, 0xB4, 0x91, 0x0C, 0xB8,
  27915. 0x9A, 0x6C, 0x17, 0x56, 0x2C, 0x83, 0xAB, 0x4C,
  27916. 0x18, 0xD0, 0xCD, 0x7E, 0x07, 0x96, 0x59, 0x2A,
  27917. 0x37, 0x2A, 0xA4, 0x09, 0xB1, 0xC5, 0x57, 0x34,
  27918. 0x7C, 0xCA, 0xCD, 0xC4, 0x64, 0x4A, 0x11, 0x90,
  27919. 0x64, 0xD0, 0x6D, 0xD4, 0x74, 0x92, 0x9D, 0x1C,
  27920. 0x6F, 0xB4, 0xD6, 0x86, 0xE5, 0x49, 0x1C, 0xE4,
  27921. 0xBC, 0x89, 0xA3, 0x0B, 0xB4, 0xB8, 0xC4, 0x1B,
  27922. 0xCE, 0x51, 0x57, 0xDF, 0xC1, 0x36, 0x08, 0x23,
  27923. 0xB1, 0xAB, 0x61, 0x8C, 0x14, 0xB1, 0x0F, 0x98,
  27924. 0xC2, 0x50, 0x67, 0x39, 0x8E, 0xA7, 0x01, 0x8C,
  27925. 0x27, 0x8A, 0x4B, 0x3D, 0xF3, 0x13, 0x34, 0xD6,
  27926. 0x03, 0xB2, 0x04, 0x4E, 0xF1, 0x87, 0xCD, 0x9B,
  27927. 0xC6, 0xCE, 0x42, 0x72, 0x5B, 0xD9, 0x62, 0xC2,
  27928. 0x64, 0x98, 0x3E, 0x9E, 0x18, 0x15, 0x5A, 0x8B,
  27929. 0x9C, 0x47, 0x14, 0x3D, 0x70, 0x46, 0x0A, 0x26,
  27930. 0xA5, 0x6F, 0xE7, 0x65, 0x8C, 0x1F, 0x15, 0x03,
  27931. 0x48, 0xC6, 0x08, 0x7E, 0xF7, 0x58, 0xAD, 0x16,
  27932. 0x78, 0x87, 0x86, 0x0A, 0x00, 0x7A, 0x5F, 0xC3,
  27933. 0x73, 0x58, 0xD4, 0x3B, 0x5E, 0xBE, 0xE8, 0x20,
  27934. 0xAC, 0xEA, 0x47, 0x4F, 0x0A, 0xC0, 0x7B, 0x76,
  27935. 0x80, 0x28, 0x66, 0x19, 0x9C, 0x61, 0x23, 0x1D,
  27936. 0x5C, 0x74, 0x7C, 0x93, 0x77, 0x4D, 0x2C, 0x1E,
  27937. 0x0C, 0x1C, 0x67, 0xE6, 0xC8, 0x1B, 0x82, 0x75,
  27938. 0x21, 0x73, 0xE1, 0x25, 0xBA, 0xF3, 0x9B, 0x4F,
  27939. 0xD1, 0x9A, 0x4F, 0x45, 0x3D, 0xC5, 0x79, 0x76,
  27940. 0xB1, 0xD9, 0x7F, 0xE6, 0x99, 0x69, 0x92, 0xBB,
  27941. 0xB6, 0x5B, 0x7C, 0xB2, 0x5D, 0x07, 0x7B, 0xBA,
  27942. 0xA6, 0xA1, 0x33, 0x22, 0x89, 0x9A, 0xF6, 0x59,
  27943. 0xCF, 0x1B, 0x35, 0x58, 0xC1, 0xB5, 0x00, 0x11,
  27944. 0x54, 0xB6, 0x25, 0x80, 0x9E, 0xD8, 0x9A, 0xEE,
  27945. 0xBB, 0x89, 0xE6, 0xEA, 0x7D, 0x67, 0xF7, 0x23,
  27946. 0xD0, 0x45, 0xAB, 0x05, 0x71, 0x5C, 0x42, 0x35,
  27947. 0x5D, 0xA6, 0xA5, 0xC8, 0xDD, 0x39, 0xC8, 0xAB,
  27948. 0xE3, 0x03, 0x77, 0x51, 0xA0, 0x1E, 0xD1, 0xC7,
  27949. 0x37, 0x49, 0x19, 0xF3, 0x12, 0x1B, 0x5A, 0x52,
  27950. 0xC5, 0x3D, 0x14, 0x87, 0x31, 0x67, 0x69, 0xF8,
  27951. 0x07, 0x21, 0xDE, 0xEA, 0xAA, 0xD3, 0xC9, 0x0F,
  27952. 0x76, 0xE7, 0xAE, 0x9E, 0x12, 0xBA, 0x92, 0xB3,
  27953. 0x2B, 0x5F, 0xD4, 0x57, 0xE3, 0xC7, 0x52, 0xC2,
  27954. 0x65, 0x0D, 0xFB, 0x88, 0x57, 0x71, 0xCB, 0x77,
  27955. 0xAC, 0x3C, 0x78, 0x5A, 0x8C, 0x56, 0x2E, 0x6A,
  27956. 0x1C, 0x63, 0xC2, 0xA5, 0x5E, 0xA4, 0x7C, 0xF8,
  27957. 0xB9, 0x0E, 0xB8, 0x22, 0x5C, 0x12, 0x3C, 0x34,
  27958. 0x64, 0x52, 0x56, 0x62, 0x35, 0xB2, 0xF3, 0x18,
  27959. 0x23, 0xA3, 0x35, 0x21, 0xE0, 0x87, 0x93, 0x7A,
  27960. 0x34, 0x5D, 0x8D, 0x66, 0x3E, 0xEA, 0xA0, 0x56,
  27961. 0x58, 0x91, 0x7B, 0xBA, 0xA0, 0x08, 0xC2, 0xE3,
  27962. 0x35, 0xF8, 0x85, 0x0A, 0x90, 0xA3, 0x26, 0xD0,
  27963. 0xE6, 0x64, 0x32, 0xF4, 0x4C, 0xEB, 0x82, 0x89,
  27964. 0xE4, 0xEC, 0xB2, 0xD1, 0x29, 0x58, 0xE9, 0x84,
  27965. 0x07, 0x2E, 0xCA, 0xCB, 0x88, 0xE1, 0x34, 0x8F,
  27966. 0xF0, 0xB5, 0x56, 0x54, 0xAC, 0xBA, 0x5B, 0x54,
  27967. 0x97, 0x1C, 0xBA, 0xEB, 0xA8, 0x8E, 0xC4, 0xB9,
  27968. 0x1A, 0x94, 0xC3, 0x71, 0x92, 0xFA, 0x98, 0x2B,
  27969. 0xEC, 0xB9, 0xF3, 0xDA, 0x42, 0x16, 0x03, 0xB6,
  27970. 0x1A, 0x51, 0xBC, 0x8E, 0x36, 0xCB, 0xD0, 0x53,
  27971. 0x85, 0x1C, 0x77, 0xB1, 0xB9, 0x26, 0xB1, 0x7A,
  27972. 0x27, 0x2A, 0xA9, 0x02, 0x32, 0x46, 0xB0, 0x2B,
  27973. 0x3E, 0xD4, 0x7F, 0x66, 0xA0, 0x0B, 0xD5, 0x68,
  27974. 0x48, 0x23, 0x63, 0x4E, 0x7C, 0xE5, 0x8C, 0xF8,
  27975. 0xF3, 0x06, 0xE3, 0x5B, 0x1E, 0x53, 0x22, 0x82,
  27976. 0x4D, 0x90, 0x48, 0x01, 0xF0, 0xA2, 0xFA, 0x7C,
  27977. 0x2B, 0xC9, 0xC2, 0x52, 0xB0, 0xA5, 0x6B, 0x7B,
  27978. 0xA2, 0xAB, 0x0F, 0x63, 0x60, 0x21, 0x74, 0x5A,
  27979. 0x70, 0xA9, 0xA4, 0x3E, 0x2B, 0x0A, 0x8D, 0x61,
  27980. 0x59, 0x70, 0xB6, 0x53, 0x09, 0x62, 0x4B, 0x51,
  27981. 0x84, 0xBC, 0xC3, 0x0B, 0x91, 0x16, 0x79, 0xAE,
  27982. 0xDD, 0x76, 0x02, 0x5F, 0xE3, 0x90, 0x8F, 0xD6,
  27983. 0x78, 0x97, 0xB0, 0xCF, 0x4B, 0xE5, 0xA6, 0xF5,
  27984. 0x41, 0x3D, 0x7D, 0xD9, 0x85, 0x64, 0xB2, 0x3E,
  27985. 0x42, 0xA9, 0x3E, 0x4A, 0xA8, 0x82, 0x1C, 0xD4,
  27986. 0x50, 0x54, 0xC6, 0x43, 0xED, 0xC1, 0x15, 0x8D,
  27987. 0xB6, 0xB3, 0xDE, 0xB1, 0x3F, 0xB5, 0xA5, 0x1E,
  27988. 0xBD, 0x1A, 0x8A, 0x78, 0xB8, 0x72, 0x25, 0xA7,
  27989. 0x33, 0x8E, 0x10, 0x11, 0x04, 0xC4, 0xA2, 0x20,
  27990. 0xD9, 0xBD, 0xED, 0xD4, 0x8C, 0x85, 0xA1, 0xC2,
  27991. 0xDA, 0xE7, 0x81, 0xA8, 0x0C, 0x40, 0xE1, 0x3B,
  27992. 0x87, 0xEA, 0xC7, 0x3A, 0x76, 0x42, 0x01, 0xC9,
  27993. 0xB7, 0x60, 0xCC, 0xFB, 0x1A, 0xE3, 0x92, 0x69,
  27994. 0x9C, 0x70, 0x39, 0xD2, 0x7C, 0x39, 0x36, 0x2B,
  27995. 0x27, 0xB8, 0xFC, 0x6F, 0x07, 0xA8, 0xA3, 0xD4,
  27996. 0x41, 0x0F, 0x15, 0x47, 0xC4, 0x8A, 0x99, 0x97,
  27997. 0xF6, 0x2C, 0x61, 0x07, 0x44, 0x52, 0xEF, 0x15,
  27998. 0x15, 0xF8, 0xA6, 0x49, 0xEB, 0xCA, 0x94, 0x37,
  27999. 0x20, 0x5A, 0x4E, 0x8A, 0x61, 0x60, 0x6B, 0x41,
  28000. 0xDA, 0xF6, 0x83, 0x4D, 0x67, 0x1F, 0x4D, 0x85,
  28001. 0x2C, 0x0C, 0x9C, 0x40, 0x96, 0x61, 0x16, 0x48,
  28002. 0xC6, 0xA3, 0x17, 0x06, 0x78, 0xB1, 0x53, 0x7C,
  28003. 0xC1, 0x82, 0x8D, 0x93, 0x58, 0x0C, 0x9E, 0x58,
  28004. 0x49, 0xA9, 0x65, 0x31, 0x75, 0xAC, 0xB7, 0x53,
  28005. 0xF2, 0xBE, 0x74, 0x37, 0xBE, 0x45, 0xF6, 0xC6,
  28006. 0x03, 0xE4, 0x85, 0xF2, 0xEC, 0x30, 0x1B, 0xB4,
  28007. 0x2B, 0x6C, 0x37, 0xC2, 0x25, 0xD7, 0x49, 0x5A,
  28008. 0x58, 0x4A, 0xE2, 0x31, 0x89, 0x0A, 0xB5, 0xC8,
  28009. 0xC3, 0x5C, 0x26, 0x8C, 0xF4, 0xBB, 0xB0, 0x21,
  28010. 0x3C, 0x09, 0x60, 0x19, 0x31, 0x95, 0x61, 0xA8,
  28011. 0xA6, 0x94, 0x76, 0x37, 0xAA, 0x40, 0xD0, 0x06,
  28012. 0xB4, 0x15, 0xBB, 0x2C, 0xFA, 0x22, 0x37, 0xE0,
  28013. 0x89, 0x0B, 0x6A, 0x3B, 0xC1, 0x34, 0xAB, 0xF8,
  28014. 0xF6, 0x58, 0x5E, 0x10, 0x8D, 0x15, 0x94, 0x0F,
  28015. 0x91, 0xF4, 0xBF, 0x5B, 0x0C, 0x81, 0x80, 0x55,
  28016. 0xB2, 0x1D, 0xEA, 0x6E, 0x63, 0xB5, 0x53, 0x98,
  28017. 0x8C, 0x47, 0xF4, 0xB9, 0x4E, 0x7C, 0xF8, 0x00,
  28018. 0xA4, 0x93, 0xB4, 0x73, 0x47, 0x05, 0xED, 0xC5,
  28019. 0x6A, 0x4B, 0x60, 0x21, 0xC6, 0x29, 0x50, 0x06,
  28020. 0x75, 0x87, 0x68, 0x04, 0xCF, 0x0B, 0x95, 0x1F,
  28021. 0x03, 0x8A, 0x5C, 0x7F, 0xE5, 0x8E, 0x89, 0x77,
  28022. 0x4E, 0xF2, 0x99, 0x2F, 0xD7, 0xC6, 0x30, 0x99,
  28023. 0xD3, 0x52, 0xA7, 0xD2, 0x15, 0x60, 0xB7, 0x88,
  28024. 0xB4, 0x05, 0x70, 0x98, 0x61, 0x81, 0x7E, 0x59,
  28025. 0xA9, 0x6B, 0x3A, 0x3A, 0x83, 0xCB, 0xA8, 0x03,
  28026. 0xB1, 0x69, 0x34, 0x33, 0x10, 0x71, 0x90, 0x5B,
  28027. 0xBE, 0xC6, 0x53, 0x29, 0x00, 0x15, 0x5D, 0x8A,
  28028. 0xC8, 0x8C, 0xB3, 0x2E, 0x4E, 0x21, 0xA3, 0xBD,
  28029. 0x3A, 0x03, 0xFD, 0xEC, 0x32, 0x5A, 0x51, 0xCD,
  28030. 0x27, 0x73, 0x96, 0x4E, 0x67, 0x84, 0xFC, 0xF1,
  28031. 0x85, 0x37, 0x37, 0xAA, 0x64, 0xEB, 0x67, 0x56,
  28032. 0x47, 0x27, 0x27, 0x26, 0x61, 0xAB, 0xF8, 0x43,
  28033. 0x13, 0xA5, 0x7A, 0x44, 0xB1, 0x23, 0xC6, 0x55,
  28034. 0x09, 0xCF, 0xB7, 0xA6, 0xF6, 0x64, 0x1C, 0xDC,
  28035. 0xC3, 0xB5, 0x7F, 0xE6, 0x28, 0xC7, 0xB8, 0x19,
  28036. 0x2D, 0xB4, 0x4F, 0xFB, 0xF5, 0x79, 0x6A, 0x86,
  28037. 0x13, 0xB1, 0xFA, 0x12, 0x6F, 0x60, 0x76, 0x88,
  28038. 0x3C, 0x78, 0x3D, 0xC2, 0x4E, 0x2A, 0x44, 0x64,
  28039. 0xC4, 0x0B, 0x3A, 0x41, 0xCA, 0x70, 0xAE, 0x87,
  28040. 0x62, 0x08, 0x66, 0xCF, 0x4F, 0xCB, 0x2B, 0xD2,
  28041. 0x04, 0xBF, 0x5C, 0x28, 0x38, 0x12, 0xBA, 0x05,
  28042. 0x6A, 0xC0, 0xC3, 0x45, 0xE3, 0x79, 0xC4, 0xBA,
  28043. 0x24, 0xD7, 0x50, 0x90, 0x12, 0x79, 0xBB, 0x2F,
  28044. 0x3A, 0x16, 0xF6, 0x12, 0xBF, 0xAD, 0xB3, 0x57,
  28045. 0x03, 0x33, 0x2C, 0x7C, 0x13, 0x6F, 0x68, 0xEA,
  28046. 0xB6, 0x75, 0x5C, 0x66, 0xB6, 0xA4, 0xAD, 0x1A,
  28047. 0xAB, 0xA7, 0xB7, 0x68, 0xA5, 0x8A, 0xCA, 0xAC,
  28048. 0xC1, 0x0A, 0x45, 0x9A, 0x1C, 0xC8, 0xEF, 0x29,
  28049. 0x37, 0x7B, 0xC2, 0x00, 0xE4, 0xD3, 0x15, 0xA3,
  28050. 0x0A, 0x6B, 0xCC, 0x32, 0x56, 0xF9, 0x73, 0x4D,
  28051. 0x06, 0xE9, 0x77, 0x9C, 0xAA, 0x54, 0x42, 0xA9,
  28052. 0xA1, 0x60, 0x69, 0x08, 0x13, 0x77, 0xC7, 0x6E,
  28053. 0x75, 0x15, 0x43, 0x68, 0x07, 0x2D, 0xC4, 0x46,
  28054. 0xED, 0x6C, 0x8B, 0x8E, 0x62, 0x2A, 0x21, 0xE3,
  28055. 0x83, 0xCF, 0x9B, 0xA1, 0xFB, 0x43, 0x4E, 0x2E,
  28056. 0xCC, 0x81, 0xE7, 0xB7, 0x8C, 0xEE, 0x98, 0x6B,
  28057. 0x8F, 0xF7, 0x98, 0xAB, 0x18, 0xCF, 0x96, 0x34,
  28058. 0x54, 0x35, 0x46, 0x28, 0x4E, 0xDA, 0x2A, 0x26,
  28059. 0xB4, 0x7F, 0x05, 0xB7, 0x35, 0xBC, 0xDB, 0x12,
  28060. 0x02, 0x22, 0x00, 0x76, 0xDC, 0x8B, 0x4E, 0x4B,
  28061. 0x9F, 0x85, 0x35, 0x33, 0xC8, 0xF6, 0xC7, 0xFF,
  28062. 0x38, 0x81, 0x7B, 0xA4, 0x97, 0x12, 0x83, 0x57,
  28063. 0x85, 0xF1, 0x7F, 0x14, 0xCA, 0x01, 0xD0, 0xC1,
  28064. 0xC1, 0xE9, 0x88, 0x10, 0xFE, 0x0B, 0x36, 0xE5,
  28065. 0xB4, 0x27, 0x15, 0x7B, 0x94, 0x18, 0x44, 0x9C,
  28066. 0xED, 0xD6, 0x41, 0xA4, 0x29, 0x3C, 0x85, 0xC3,
  28067. 0x27, 0x00, 0x10, 0x2A, 0xCE, 0xC2, 0x2E, 0xBA,
  28068. 0xD9, 0x8E, 0xD1, 0x60, 0xA5, 0xF0, 0x27, 0xBD,
  28069. 0x4C, 0xDA, 0x57, 0xF1, 0xF3, 0x72, 0x0A, 0x12,
  28070. 0xC1, 0x34, 0x65, 0x4D, 0xD5, 0xE7, 0x3F, 0x82,
  28071. 0x96, 0x76, 0x49, 0x53, 0x90, 0xD0, 0xE7, 0x92,
  28072. 0x9D, 0x60, 0x34, 0xE9, 0xC5, 0x5F, 0x7D, 0x55,
  28073. 0xBA, 0x65, 0x8B, 0xC5, 0x87, 0x98, 0x8E, 0x8A,
  28074. 0xF9, 0x49, 0x60, 0xF6, 0xCF, 0xB8, 0xD5, 0xAF,
  28075. 0x7A, 0x00, 0x21, 0x53, 0x5A, 0x6E, 0x25, 0xE4,
  28076. 0x37, 0xD4, 0x9A, 0x78, 0x06, 0x98, 0xBE, 0x22,
  28077. 0xAC, 0x99, 0x53, 0x94, 0x9F, 0x57, 0x1B, 0x85,
  28078. 0xA6, 0x85, 0x72, 0x5F, 0x82, 0x07, 0xA2, 0xB0,
  28079. 0xAE, 0x84, 0x9B, 0x60, 0x1A, 0xB9, 0x1B, 0x15,
  28080. 0x9B, 0x3D, 0xF4, 0xA1, 0x54, 0xC2, 0x04, 0x1E,
  28081. 0x77, 0x60, 0x70, 0xAF, 0xC4, 0x29, 0x69, 0x32,
  28082. 0x23, 0x80, 0x91, 0x7C, 0x97, 0x51, 0x07, 0x99,
  28083. 0xF3, 0x14, 0x91, 0x31, 0x47, 0x7E, 0x16, 0x66,
  28084. 0x3D, 0x31, 0x74, 0xC7, 0xC1, 0xCA, 0xEA, 0x78,
  28085. 0x85, 0x35, 0xC6, 0xC0, 0x05, 0xA6, 0x4F, 0x28,
  28086. 0x68, 0x63, 0x1B, 0x31, 0xB6, 0x6E, 0x20, 0x5F,
  28087. 0xD3, 0x8C, 0x1D, 0x84, 0x54, 0x2D, 0x0F, 0x1B,
  28088. 0x57, 0x8F, 0x58, 0xC9, 0xBF, 0x5A, 0x0F, 0xAE,
  28089. 0xAB, 0x6A, 0xB6, 0x49, 0x48, 0x93, 0x05, 0x31,
  28090. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  28091. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  28092. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  28093. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  28094. };
  28095. const byte kyber1024_sk[] = {
  28096. 0x07, 0x63, 0x8F, 0xB6, 0x98, 0x68, 0xF3, 0xD3,
  28097. 0x20, 0xE5, 0x86, 0x2B, 0xD9, 0x69, 0x33, 0xFE,
  28098. 0xB3, 0x11, 0xB3, 0x62, 0x09, 0x3C, 0x9B, 0x5D,
  28099. 0x50, 0x17, 0x0B, 0xCE, 0xD4, 0x3F, 0x1B, 0x53,
  28100. 0x6D, 0x9A, 0x20, 0x4B, 0xB1, 0xF2, 0x26, 0x95,
  28101. 0x95, 0x0B, 0xA1, 0xF2, 0xA9, 0xE8, 0xEB, 0x82,
  28102. 0x8B, 0x28, 0x44, 0x88, 0x76, 0x0B, 0x3F, 0xC8,
  28103. 0x4F, 0xAB, 0xA0, 0x42, 0x75, 0xD5, 0x62, 0x8E,
  28104. 0x39, 0xC5, 0xB2, 0x47, 0x13, 0x74, 0x28, 0x3C,
  28105. 0x50, 0x32, 0x99, 0xC0, 0xAB, 0x49, 0xB6, 0x6B,
  28106. 0x8B, 0xBB, 0x56, 0xA4, 0x18, 0x66, 0x24, 0xF9,
  28107. 0x19, 0xA2, 0xBA, 0x59, 0xBB, 0x08, 0xD8, 0x55,
  28108. 0x18, 0x80, 0xC2, 0xBE, 0xFC, 0x4F, 0x87, 0xF2,
  28109. 0x5F, 0x59, 0xAB, 0x58, 0x7A, 0x79, 0xC3, 0x27,
  28110. 0xD7, 0x92, 0xD5, 0x4C, 0x97, 0x4A, 0x69, 0x26,
  28111. 0x2F, 0xF8, 0xA7, 0x89, 0x38, 0x28, 0x9E, 0x9A,
  28112. 0x87, 0xB6, 0x88, 0xB0, 0x83, 0xE0, 0x59, 0x5F,
  28113. 0xE2, 0x18, 0xB6, 0xBB, 0x15, 0x05, 0x94, 0x1C,
  28114. 0xE2, 0xE8, 0x1A, 0x5A, 0x64, 0xC5, 0xAA, 0xC6,
  28115. 0x04, 0x17, 0x25, 0x69, 0x85, 0x34, 0x9E, 0xE4,
  28116. 0x7A, 0x52, 0x42, 0x0A, 0x5F, 0x97, 0x47, 0x7B,
  28117. 0x72, 0x36, 0xAC, 0x76, 0xBC, 0x70, 0xE8, 0x28,
  28118. 0x87, 0x29, 0x28, 0x7E, 0xE3, 0xE3, 0x4A, 0x3D,
  28119. 0xBC, 0x36, 0x83, 0xC0, 0xB7, 0xB1, 0x00, 0x29,
  28120. 0xFC, 0x20, 0x34, 0x18, 0x53, 0x7E, 0x74, 0x66,
  28121. 0xBA, 0x63, 0x85, 0xA8, 0xFF, 0x30, 0x1E, 0xE1,
  28122. 0x27, 0x08, 0xF8, 0x2A, 0xAA, 0x1E, 0x38, 0x0F,
  28123. 0xC7, 0xA8, 0x8F, 0x8F, 0x20, 0x5A, 0xB7, 0xE8,
  28124. 0x8D, 0x7E, 0x95, 0x95, 0x2A, 0x55, 0xBA, 0x20,
  28125. 0xD0, 0x9B, 0x79, 0xA4, 0x71, 0x41, 0xD6, 0x2B,
  28126. 0xF6, 0xEB, 0x7D, 0xD3, 0x07, 0xB0, 0x8E, 0xCA,
  28127. 0x13, 0xA5, 0xBC, 0x5F, 0x6B, 0x68, 0x58, 0x1C,
  28128. 0x68, 0x65, 0xB2, 0x7B, 0xBC, 0xDD, 0xAB, 0x14,
  28129. 0x2F, 0x4B, 0x2C, 0xBF, 0xF4, 0x88, 0xC8, 0xA2,
  28130. 0x27, 0x05, 0xFA, 0xA9, 0x8A, 0x2B, 0x9E, 0xEA,
  28131. 0x35, 0x30, 0xC7, 0x66, 0x62, 0x33, 0x5C, 0xC7,
  28132. 0xEA, 0x3A, 0x00, 0x77, 0x77, 0x25, 0xEB, 0xCC,
  28133. 0xCD, 0x2A, 0x46, 0x36, 0xB2, 0xD9, 0x12, 0x2F,
  28134. 0xF3, 0xAB, 0x77, 0x12, 0x3C, 0xE0, 0x88, 0x3C,
  28135. 0x19, 0x11, 0x11, 0x5E, 0x50, 0xC9, 0xE8, 0xA9,
  28136. 0x41, 0x94, 0xE4, 0x8D, 0xD0, 0xD0, 0x9C, 0xFF,
  28137. 0xB3, 0xAD, 0xCD, 0x2C, 0x1E, 0x92, 0x43, 0x09,
  28138. 0x03, 0xD0, 0x7A, 0xDB, 0xF0, 0x05, 0x32, 0x03,
  28139. 0x15, 0x75, 0xAA, 0x7F, 0x9E, 0x7B, 0x5A, 0x1F,
  28140. 0x33, 0x62, 0xDE, 0xC9, 0x36, 0xD4, 0x04, 0x3C,
  28141. 0x05, 0xF2, 0x47, 0x6C, 0x07, 0x57, 0x8B, 0xC9,
  28142. 0xCB, 0xAF, 0x2A, 0xB4, 0xE3, 0x82, 0x72, 0x7A,
  28143. 0xD4, 0x16, 0x86, 0xA9, 0x6B, 0x25, 0x48, 0x82,
  28144. 0x0B, 0xB0, 0x3B, 0x32, 0xF1, 0x1B, 0x28, 0x11,
  28145. 0xAD, 0x62, 0xF4, 0x89, 0xE9, 0x51, 0x63, 0x2A,
  28146. 0xBA, 0x0D, 0x1D, 0xF8, 0x96, 0x80, 0xCC, 0x8A,
  28147. 0x8B, 0x53, 0xB4, 0x81, 0xD9, 0x2A, 0x68, 0xD7,
  28148. 0x0B, 0x4E, 0xA1, 0xC3, 0xA6, 0xA5, 0x61, 0xC0,
  28149. 0x69, 0x28, 0x82, 0xB5, 0xCA, 0x8C, 0xC9, 0x42,
  28150. 0xA8, 0xD4, 0x95, 0xAF, 0xCB, 0x06, 0xDE, 0x89,
  28151. 0x49, 0x8F, 0xB9, 0x35, 0xB7, 0x75, 0x90, 0x8F,
  28152. 0xE7, 0xA0, 0x3E, 0x32, 0x4D, 0x54, 0xCC, 0x19,
  28153. 0xD4, 0xE1, 0xAA, 0xBD, 0x35, 0x93, 0xB3, 0x8B,
  28154. 0x19, 0xEE, 0x13, 0x88, 0xFE, 0x49, 0x2B, 0x43,
  28155. 0x12, 0x7E, 0x5A, 0x50, 0x42, 0x53, 0x78, 0x6A,
  28156. 0x0D, 0x69, 0xAD, 0x32, 0x60, 0x1C, 0x28, 0xE2,
  28157. 0xC8, 0x85, 0x04, 0xA5, 0xBA, 0x59, 0x97, 0x06,
  28158. 0x02, 0x3A, 0x61, 0x36, 0x3E, 0x17, 0xC6, 0xB9,
  28159. 0xBB, 0x59, 0xBD, 0xC6, 0x97, 0x45, 0x2C, 0xD0,
  28160. 0x59, 0x45, 0x19, 0x83, 0xD7, 0x38, 0xCA, 0x3F,
  28161. 0xD0, 0x34, 0xE3, 0xF5, 0x98, 0x88, 0x54, 0xCA,
  28162. 0x05, 0x03, 0x1D, 0xB0, 0x96, 0x11, 0x49, 0x89,
  28163. 0x88, 0x19, 0x7C, 0x6B, 0x30, 0xD2, 0x58, 0xDF,
  28164. 0xE2, 0x62, 0x65, 0x54, 0x1C, 0x89, 0xA4, 0xB3,
  28165. 0x1D, 0x68, 0x64, 0xE9, 0x38, 0x9B, 0x03, 0xCB,
  28166. 0x74, 0xF7, 0xEC, 0x43, 0x23, 0xFB, 0x94, 0x21,
  28167. 0xA4, 0xB9, 0x79, 0x0A, 0x26, 0xD1, 0x7B, 0x03,
  28168. 0x98, 0xA2, 0x67, 0x67, 0x35, 0x09, 0x09, 0xF8,
  28169. 0x4D, 0x57, 0xB6, 0x69, 0x4D, 0xF8, 0x30, 0x66,
  28170. 0x4C, 0xA8, 0xB3, 0xC3, 0xC0, 0x3E, 0xD2, 0xAE,
  28171. 0x67, 0xB8, 0x90, 0x06, 0x86, 0x8A, 0x68, 0x52,
  28172. 0x7C, 0xCD, 0x66, 0x64, 0x59, 0xAB, 0x7F, 0x05,
  28173. 0x66, 0x71, 0x00, 0x0C, 0x61, 0x64, 0xD3, 0xA7,
  28174. 0xF2, 0x66, 0xA1, 0x4D, 0x97, 0xCB, 0xD7, 0x00,
  28175. 0x4D, 0x6C, 0x92, 0xCA, 0xCA, 0x77, 0x0B, 0x84,
  28176. 0x4A, 0x4F, 0xA9, 0xB1, 0x82, 0xE7, 0xB1, 0x8C,
  28177. 0xA8, 0x85, 0x08, 0x2A, 0xC5, 0x64, 0x6F, 0xCB,
  28178. 0x4A, 0x14, 0xE1, 0x68, 0x5F, 0xEB, 0x0C, 0x9C,
  28179. 0xE3, 0x37, 0x2A, 0xB9, 0x53, 0x65, 0xC0, 0x4F,
  28180. 0xD8, 0x30, 0x84, 0xF8, 0x0A, 0x23, 0xFF, 0x10,
  28181. 0xA0, 0x5B, 0xF1, 0x5F, 0x7F, 0xA5, 0xAC, 0xC6,
  28182. 0xC0, 0xCB, 0x46, 0x2C, 0x33, 0xCA, 0x52, 0x4F,
  28183. 0xA6, 0xB8, 0xBB, 0x35, 0x90, 0x43, 0xBA, 0x68,
  28184. 0x60, 0x9E, 0xAA, 0x25, 0x36, 0xE8, 0x1D, 0x08,
  28185. 0x46, 0x3B, 0x19, 0x65, 0x3B, 0x54, 0x35, 0xBA,
  28186. 0x94, 0x6C, 0x9A, 0xDD, 0xEB, 0x20, 0x2B, 0x04,
  28187. 0xB0, 0x31, 0xCC, 0x96, 0x0D, 0xCC, 0x12, 0xE4,
  28188. 0x51, 0x8D, 0x42, 0x8B, 0x32, 0xB2, 0x57, 0xA4,
  28189. 0xFC, 0x73, 0x13, 0xD3, 0xA7, 0x98, 0x0D, 0x80,
  28190. 0x08, 0x2E, 0x93, 0x4F, 0x9D, 0x95, 0xC3, 0x2B,
  28191. 0x0A, 0x01, 0x91, 0xA2, 0x36, 0x04, 0x38, 0x4D,
  28192. 0xD9, 0xE0, 0x79, 0xBB, 0xBA, 0xA2, 0x66, 0xD1,
  28193. 0x4C, 0x3F, 0x75, 0x6B, 0x9F, 0x21, 0x33, 0x10,
  28194. 0x74, 0x33, 0xA4, 0xE8, 0x3F, 0xA7, 0x18, 0x72,
  28195. 0x82, 0xA8, 0x09, 0x20, 0x3A, 0x4F, 0xAF, 0x84,
  28196. 0x18, 0x51, 0x83, 0x3D, 0x12, 0x1A, 0xC3, 0x83,
  28197. 0x84, 0x3A, 0x5E, 0x55, 0xBC, 0x23, 0x81, 0x42,
  28198. 0x5E, 0x16, 0xC7, 0xDB, 0x4C, 0xC9, 0xAB, 0x5C,
  28199. 0x1B, 0x0D, 0x91, 0xA4, 0x7E, 0x2B, 0x8D, 0xE0,
  28200. 0xE5, 0x82, 0xC8, 0x6B, 0x6B, 0x0D, 0x90, 0x7B,
  28201. 0xB3, 0x60, 0xB9, 0x7F, 0x40, 0xAB, 0x5D, 0x03,
  28202. 0x8F, 0x6B, 0x75, 0xC8, 0x14, 0xB2, 0x7D, 0x9B,
  28203. 0x96, 0x8D, 0x41, 0x98, 0x32, 0xBC, 0x8C, 0x2B,
  28204. 0xEE, 0x60, 0x5E, 0xF6, 0xE5, 0x05, 0x9D, 0x33,
  28205. 0x10, 0x0D, 0x90, 0x48, 0x5D, 0x37, 0x84, 0x50,
  28206. 0x01, 0x42, 0x21, 0x73, 0x6C, 0x07, 0x40, 0x7C,
  28207. 0xAC, 0x26, 0x04, 0x08, 0xAA, 0x64, 0x92, 0x66,
  28208. 0x19, 0x78, 0x8B, 0x86, 0x01, 0xC2, 0xA7, 0x52,
  28209. 0xD1, 0xA6, 0xCB, 0xF8, 0x20, 0xD7, 0xC7, 0xA0,
  28210. 0x47, 0x16, 0x20, 0x32, 0x25, 0xB3, 0x89, 0x5B,
  28211. 0x93, 0x42, 0xD1, 0x47, 0xA8, 0x18, 0x5C, 0xFC,
  28212. 0x1B, 0xB6, 0x5B, 0xA0, 0x6B, 0x41, 0x42, 0x33,
  28213. 0x99, 0x03, 0xC0, 0xAC, 0x46, 0x51, 0x38, 0x5B,
  28214. 0x45, 0xD9, 0x8A, 0x8B, 0x19, 0xD2, 0x8C, 0xD6,
  28215. 0xBA, 0xB0, 0x88, 0x78, 0x7F, 0x7E, 0xE1, 0xB1,
  28216. 0x24, 0x61, 0x76, 0x6B, 0x43, 0xCB, 0xCC, 0xB9,
  28217. 0x64, 0x34, 0x42, 0x7D, 0x93, 0xC0, 0x65, 0x55,
  28218. 0x06, 0x88, 0xF6, 0x94, 0x8E, 0xD1, 0xB5, 0x47,
  28219. 0x5A, 0x42, 0x5F, 0x1B, 0x85, 0x20, 0x9D, 0x06,
  28220. 0x1C, 0x08, 0xB5, 0x6C, 0x1C, 0xC0, 0x69, 0xF6,
  28221. 0xC0, 0xA7, 0xC6, 0xF2, 0x93, 0x58, 0xCA, 0xB9,
  28222. 0x11, 0x08, 0x77, 0x32, 0xA6, 0x49, 0xD2, 0x7C,
  28223. 0x9B, 0x98, 0xF9, 0xA4, 0x88, 0x79, 0x38, 0x7D,
  28224. 0x9B, 0x00, 0xC2, 0x59, 0x59, 0xA7, 0x16, 0x54,
  28225. 0xD6, 0xF6, 0xA9, 0x46, 0x16, 0x45, 0x13, 0xE4,
  28226. 0x7A, 0x75, 0xD0, 0x05, 0x98, 0x6C, 0x23, 0x63,
  28227. 0xC0, 0x9F, 0x6B, 0x53, 0x7E, 0xCA, 0x78, 0xB9,
  28228. 0x30, 0x3A, 0x5F, 0xA4, 0x57, 0x60, 0x8A, 0x58,
  28229. 0x6A, 0x65, 0x3A, 0x34, 0x7D, 0xB0, 0x4D, 0xFC,
  28230. 0xC1, 0x91, 0x75, 0xB3, 0xA3, 0x01, 0x17, 0x25,
  28231. 0x36, 0x06, 0x2A, 0x65, 0x8A, 0x95, 0x27, 0x75,
  28232. 0x70, 0xC8, 0x85, 0x2C, 0xA8, 0x97, 0x3F, 0x4A,
  28233. 0xE1, 0x23, 0xA3, 0x34, 0x04, 0x7D, 0xD7, 0x11,
  28234. 0xC8, 0x92, 0x7A, 0x63, 0x4A, 0x03, 0x38, 0x8A,
  28235. 0x52, 0x7B, 0x03, 0x4B, 0xF7, 0xA8, 0x17, 0x0F,
  28236. 0xA7, 0x02, 0xC1, 0xF7, 0xC2, 0x3E, 0xC3, 0x2D,
  28237. 0x18, 0xA2, 0x37, 0x48, 0x90, 0xBE, 0x9C, 0x78,
  28238. 0x7A, 0x94, 0x09, 0xC8, 0x2D, 0x19, 0x2C, 0x4B,
  28239. 0xB7, 0x05, 0xA2, 0xF9, 0x96, 0xCE, 0x40, 0x5D,
  28240. 0x85, 0xA4, 0xC1, 0xA1, 0xAB, 0x9B, 0x6A, 0xEB,
  28241. 0x49, 0xCC, 0xE1, 0xC2, 0xF8, 0xA9, 0x7C, 0x35,
  28242. 0x16, 0xC7, 0x2A, 0x00, 0xA4, 0x62, 0x63, 0xBA,
  28243. 0xA6, 0x96, 0xBF, 0x25, 0x72, 0x77, 0x19, 0xC3,
  28244. 0x21, 0x64, 0x23, 0x61, 0x8F, 0xF3, 0x33, 0x80,
  28245. 0x93, 0x4A, 0x6C, 0x10, 0x54, 0x5C, 0x4C, 0x5C,
  28246. 0x51, 0x55, 0xB1, 0x24, 0x86, 0x18, 0x1F, 0xC7,
  28247. 0xA2, 0x31, 0x98, 0x73, 0x97, 0x8B, 0x6A, 0x2A,
  28248. 0x67, 0x49, 0x0F, 0x82, 0x56, 0xBD, 0x21, 0x96,
  28249. 0xFE, 0x17, 0x92, 0xA4, 0xC0, 0x00, 0x77, 0xB8,
  28250. 0x12, 0xEA, 0xE8, 0xBE, 0xD3, 0x57, 0x24, 0x99,
  28251. 0x68, 0x4A, 0xB3, 0x37, 0x18, 0x76, 0x76, 0x1E,
  28252. 0x45, 0x0C, 0x9F, 0x9D, 0x27, 0x68, 0xA3, 0x68,
  28253. 0x06, 0xD7, 0xAB, 0x20, 0x46, 0xC9, 0x1F, 0x17,
  28254. 0x59, 0x9E, 0x9A, 0xC5, 0x92, 0x99, 0x08, 0x08,
  28255. 0xDC, 0xD7, 0xB4, 0xD0, 0x91, 0x90, 0x72, 0xF1,
  28256. 0x4E, 0xC3, 0x61, 0x77, 0x3B, 0x72, 0x52, 0x44,
  28257. 0x4C, 0x32, 0x3C, 0x30, 0x83, 0x26, 0xF4, 0xA3,
  28258. 0x0F, 0x86, 0x80, 0xD2, 0xF7, 0x48, 0xF5, 0x6A,
  28259. 0x13, 0x2B, 0x82, 0x67, 0x4E, 0xD0, 0x18, 0x46,
  28260. 0x20, 0xB8, 0x2A, 0xD2, 0xCB, 0x18, 0x2C, 0x97,
  28261. 0xB4, 0x81, 0x62, 0x66, 0x47, 0x49, 0x12, 0x90,
  28262. 0xA0, 0x11, 0xCC, 0x73, 0x82, 0x86, 0x85, 0xA8,
  28263. 0xC3, 0x67, 0xA5, 0xB9, 0xCF, 0x8D, 0x62, 0x1B,
  28264. 0x0D, 0x5C, 0x1E, 0xFF, 0x03, 0x17, 0x27, 0x58,
  28265. 0xBD, 0x00, 0x49, 0x78, 0xC2, 0x51, 0xCD, 0x51,
  28266. 0x34, 0x22, 0x28, 0x98, 0x9C, 0xAE, 0x63, 0x32,
  28267. 0xAC, 0x48, 0x64, 0x37, 0xCB, 0x5C, 0x57, 0xD4,
  28268. 0x30, 0x74, 0x62, 0x86, 0x52, 0x53, 0xBE, 0x21,
  28269. 0x7B, 0x35, 0x15, 0xC7, 0x3D, 0xF4, 0x05, 0xB7,
  28270. 0xF2, 0x82, 0x17, 0xAD, 0x0B, 0x8C, 0xF6, 0x0C,
  28271. 0x2F, 0xFF, 0xAA, 0x0A, 0x00, 0x48, 0xB1, 0xFB,
  28272. 0x4A, 0xCD, 0xCD, 0xC3, 0x8B, 0x52, 0x50, 0xCF,
  28273. 0xEC, 0x35, 0x6A, 0x6D, 0xE2, 0x6C, 0xFA, 0x7A,
  28274. 0x58, 0x8F, 0xDC, 0x86, 0xF9, 0x8C, 0x85, 0x4A,
  28275. 0xC6, 0x4C, 0x7B, 0xFA, 0xA9, 0x6F, 0x5A, 0x32,
  28276. 0xCC, 0x06, 0x10, 0x93, 0x4B, 0xAA, 0x6A, 0x58,
  28277. 0x6B, 0x9A, 0x20, 0x54, 0xF1, 0x3B, 0xA2, 0x74,
  28278. 0x17, 0x4A, 0xA0, 0xD2, 0xB3, 0xA8, 0x1B, 0x96,
  28279. 0xA9, 0x40, 0x66, 0x6F, 0x78, 0x9B, 0x5A, 0x6B,
  28280. 0xCD, 0xC0, 0xA6, 0xA0, 0x17, 0x8A, 0x0C, 0x9A,
  28281. 0x02, 0x57, 0x8A, 0x49, 0x3F, 0x6E, 0xEA, 0x0D,
  28282. 0x2E, 0x6C, 0x13, 0x95, 0x1C, 0x9F, 0x24, 0x9A,
  28283. 0x5E, 0x8D, 0xD7, 0x1D, 0xD4, 0x9A, 0x74, 0x2D,
  28284. 0x45, 0x1F, 0x1A, 0xBB, 0xA1, 0x9A, 0xF8, 0xC5,
  28285. 0x47, 0x85, 0x5E, 0x0A, 0xFC, 0x72, 0x8E, 0x90,
  28286. 0xAB, 0xB4, 0x99, 0xC9, 0xBE, 0xEB, 0x76, 0x6F,
  28287. 0x47, 0x29, 0xCD, 0xA2, 0x22, 0x63, 0xE3, 0x24,
  28288. 0xD2, 0x23, 0x02, 0xCB, 0xD3, 0x39, 0x9F, 0xAC,
  28289. 0xC6, 0x30, 0x99, 0x1F, 0xC8, 0xF2, 0x8B, 0xDB,
  28290. 0x43, 0x54, 0x76, 0x25, 0x41, 0x52, 0x76, 0x78,
  28291. 0xBC, 0xF6, 0x1F, 0x65, 0xC2, 0x41, 0x14, 0x6C,
  28292. 0x42, 0x6D, 0x23, 0xB9, 0xBF, 0xAA, 0x6B, 0x7D,
  28293. 0xF1, 0x8C, 0x97, 0xF2, 0x0C, 0x1B, 0x61, 0x25,
  28294. 0xBF, 0x87, 0x4B, 0x1D, 0x89, 0x47, 0x58, 0x52,
  28295. 0xC4, 0x48, 0x21, 0x5D, 0xB0, 0xEB, 0x77, 0x37,
  28296. 0xF9, 0x14, 0x80, 0xE8, 0xCE, 0xBD, 0x9A, 0x08,
  28297. 0x71, 0x57, 0x4F, 0x5A, 0xB6, 0x2D, 0x90, 0x20,
  28298. 0x17, 0x5E, 0xC6, 0x92, 0x7C, 0xA0, 0xB5, 0x4C,
  28299. 0x09, 0x81, 0x8E, 0x42, 0xCF, 0x92, 0xA3, 0x83,
  28300. 0x17, 0x24, 0x22, 0xC7, 0xDC, 0x18, 0x31, 0xD6,
  28301. 0x3B, 0x0C, 0x29, 0x5D, 0xE7, 0x51, 0x59, 0xDB,
  28302. 0x80, 0x34, 0xE9, 0xE0, 0x7F, 0x7B, 0x0B, 0x91,
  28303. 0x0C, 0x3C, 0x1E, 0x5F, 0xB6, 0x6B, 0x3D, 0xC5,
  28304. 0x23, 0xF1, 0xFA, 0x6E, 0xB4, 0x91, 0x0C, 0xB8,
  28305. 0x9A, 0x6C, 0x17, 0x56, 0x2C, 0x83, 0xAB, 0x4C,
  28306. 0x18, 0xD0, 0xCD, 0x7E, 0x07, 0x96, 0x59, 0x2A,
  28307. 0x37, 0x2A, 0xA4, 0x09, 0xB1, 0xC5, 0x57, 0x34,
  28308. 0x7C, 0xCA, 0xCD, 0xC4, 0x64, 0x4A, 0x11, 0x90,
  28309. 0x64, 0xD0, 0x6D, 0xD4, 0x74, 0x92, 0x9D, 0x1C,
  28310. 0x6F, 0xB4, 0xD6, 0x86, 0xE5, 0x49, 0x1C, 0xE4,
  28311. 0xBC, 0x89, 0xA3, 0x0B, 0xB4, 0xB8, 0xC4, 0x1B,
  28312. 0xCE, 0x51, 0x57, 0xDF, 0xC1, 0x36, 0x08, 0x23,
  28313. 0xB1, 0xAB, 0x61, 0x8C, 0x14, 0xB1, 0x0F, 0x98,
  28314. 0xC2, 0x50, 0x67, 0x39, 0x8E, 0xA7, 0x01, 0x8C,
  28315. 0x27, 0x8A, 0x4B, 0x3D, 0xF3, 0x13, 0x34, 0xD6,
  28316. 0x03, 0xB2, 0x04, 0x4E, 0xF1, 0x87, 0xCD, 0x9B,
  28317. 0xC6, 0xCE, 0x42, 0x72, 0x5B, 0xD9, 0x62, 0xC2,
  28318. 0x64, 0x98, 0x3E, 0x9E, 0x18, 0x15, 0x5A, 0x8B,
  28319. 0x9C, 0x47, 0x14, 0x3D, 0x70, 0x46, 0x0A, 0x26,
  28320. 0xA5, 0x6F, 0xE7, 0x65, 0x8C, 0x1F, 0x15, 0x03,
  28321. 0x48, 0xC6, 0x08, 0x7E, 0xF7, 0x58, 0xAD, 0x16,
  28322. 0x78, 0x87, 0x86, 0x0A, 0x00, 0x7A, 0x5F, 0xC3,
  28323. 0x73, 0x58, 0xD4, 0x3B, 0x5E, 0xBE, 0xE8, 0x20,
  28324. 0xAC, 0xEA, 0x47, 0x4F, 0x0A, 0xC0, 0x7B, 0x76,
  28325. 0x80, 0x28, 0x66, 0x19, 0x9C, 0x61, 0x23, 0x1D,
  28326. 0x5C, 0x74, 0x7C, 0x93, 0x77, 0x4D, 0x2C, 0x1E,
  28327. 0x0C, 0x1C, 0x67, 0xE6, 0xC8, 0x1B, 0x82, 0x75,
  28328. 0x21, 0x73, 0xE1, 0x25, 0xBA, 0xF3, 0x9B, 0x4F,
  28329. 0xD1, 0x9A, 0x4F, 0x45, 0x3D, 0xC5, 0x79, 0x76,
  28330. 0xB1, 0xD9, 0x7F, 0xE6, 0x99, 0x69, 0x92, 0xBB,
  28331. 0xB6, 0x5B, 0x7C, 0xB2, 0x5D, 0x07, 0x7B, 0xBA,
  28332. 0xA6, 0xA1, 0x33, 0x22, 0x89, 0x9A, 0xF6, 0x59,
  28333. 0xCF, 0x1B, 0x35, 0x58, 0xC1, 0xB5, 0x00, 0x11,
  28334. 0x54, 0xB6, 0x25, 0x80, 0x9E, 0xD8, 0x9A, 0xEE,
  28335. 0xBB, 0x89, 0xE6, 0xEA, 0x7D, 0x67, 0xF7, 0x23,
  28336. 0xD0, 0x45, 0xAB, 0x05, 0x71, 0x5C, 0x42, 0x35,
  28337. 0x5D, 0xA6, 0xA5, 0xC8, 0xDD, 0x39, 0xC8, 0xAB,
  28338. 0xE3, 0x03, 0x77, 0x51, 0xA0, 0x1E, 0xD1, 0xC7,
  28339. 0x37, 0x49, 0x19, 0xF3, 0x12, 0x1B, 0x5A, 0x52,
  28340. 0xC5, 0x3D, 0x14, 0x87, 0x31, 0x67, 0x69, 0xF8,
  28341. 0x07, 0x21, 0xDE, 0xEA, 0xAA, 0xD3, 0xC9, 0x0F,
  28342. 0x76, 0xE7, 0xAE, 0x9E, 0x12, 0xBA, 0x92, 0xB3,
  28343. 0x2B, 0x5F, 0xD4, 0x57, 0xE3, 0xC7, 0x52, 0xC2,
  28344. 0x65, 0x0D, 0xFB, 0x88, 0x57, 0x71, 0xCB, 0x77,
  28345. 0xAC, 0x3C, 0x78, 0x5A, 0x8C, 0x56, 0x2E, 0x6A,
  28346. 0x1C, 0x63, 0xC2, 0xA5, 0x5E, 0xA4, 0x7C, 0xF8,
  28347. 0xB9, 0x0E, 0xB8, 0x22, 0x5C, 0x12, 0x3C, 0x34,
  28348. 0x64, 0x52, 0x56, 0x62, 0x35, 0xB2, 0xF3, 0x18,
  28349. 0x23, 0xA3, 0x35, 0x21, 0xE0, 0x87, 0x93, 0x7A,
  28350. 0x34, 0x5D, 0x8D, 0x66, 0x3E, 0xEA, 0xA0, 0x56,
  28351. 0x58, 0x91, 0x7B, 0xBA, 0xA0, 0x08, 0xC2, 0xE3,
  28352. 0x35, 0xF8, 0x85, 0x0A, 0x90, 0xA3, 0x26, 0xD0,
  28353. 0xE6, 0x64, 0x32, 0xF4, 0x4C, 0xEB, 0x82, 0x89,
  28354. 0xE4, 0xEC, 0xB2, 0xD1, 0x29, 0x58, 0xE9, 0x84,
  28355. 0x07, 0x2E, 0xCA, 0xCB, 0x88, 0xE1, 0x34, 0x8F,
  28356. 0xF0, 0xB5, 0x56, 0x54, 0xAC, 0xBA, 0x5B, 0x54,
  28357. 0x97, 0x1C, 0xBA, 0xEB, 0xA8, 0x8E, 0xC4, 0xB9,
  28358. 0x1A, 0x94, 0xC3, 0x71, 0x92, 0xFA, 0x98, 0x2B,
  28359. 0xEC, 0xB9, 0xF3, 0xDA, 0x42, 0x16, 0x03, 0xB6,
  28360. 0x1A, 0x51, 0xBC, 0x8E, 0x36, 0xCB, 0xD0, 0x53,
  28361. 0x85, 0x1C, 0x77, 0xB1, 0xB9, 0x26, 0xB1, 0x7A,
  28362. 0x27, 0x2A, 0xA9, 0x02, 0x32, 0x46, 0xB0, 0x2B,
  28363. 0x3E, 0xD4, 0x7F, 0x66, 0xA0, 0x0B, 0xD5, 0x68,
  28364. 0x48, 0x23, 0x63, 0x4E, 0x7C, 0xE5, 0x8C, 0xF8,
  28365. 0xF3, 0x06, 0xE3, 0x5B, 0x1E, 0x53, 0x22, 0x82,
  28366. 0x4D, 0x90, 0x48, 0x01, 0xF0, 0xA2, 0xFA, 0x7C,
  28367. 0x2B, 0xC9, 0xC2, 0x52, 0xB0, 0xA5, 0x6B, 0x7B,
  28368. 0xA2, 0xAB, 0x0F, 0x63, 0x60, 0x21, 0x74, 0x5A,
  28369. 0x70, 0xA9, 0xA4, 0x3E, 0x2B, 0x0A, 0x8D, 0x61,
  28370. 0x59, 0x70, 0xB6, 0x53, 0x09, 0x62, 0x4B, 0x51,
  28371. 0x84, 0xBC, 0xC3, 0x0B, 0x91, 0x16, 0x79, 0xAE,
  28372. 0xDD, 0x76, 0x02, 0x5F, 0xE3, 0x90, 0x8F, 0xD6,
  28373. 0x78, 0x97, 0xB0, 0xCF, 0x4B, 0xE5, 0xA6, 0xF5,
  28374. 0x41, 0x3D, 0x7D, 0xD9, 0x85, 0x64, 0xB2, 0x3E,
  28375. 0x42, 0xA9, 0x3E, 0x4A, 0xA8, 0x82, 0x1C, 0xD4,
  28376. 0x50, 0x54, 0xC6, 0x43, 0xED, 0xC1, 0x15, 0x8D,
  28377. 0xB6, 0xB3, 0xDE, 0xB1, 0x3F, 0xB5, 0xA5, 0x1E,
  28378. 0xBD, 0x1A, 0x8A, 0x78, 0xB8, 0x72, 0x25, 0xA7,
  28379. 0x33, 0x8E, 0x10, 0x11, 0x04, 0xC4, 0xA2, 0x20,
  28380. 0xD9, 0xBD, 0xED, 0xD4, 0x8C, 0x85, 0xA1, 0xC2,
  28381. 0xDA, 0xE7, 0x81, 0xA8, 0x0C, 0x40, 0xE1, 0x3B,
  28382. 0x87, 0xEA, 0xC7, 0x3A, 0x76, 0x42, 0x01, 0xC9,
  28383. 0xB7, 0x60, 0xCC, 0xFB, 0x1A, 0xE3, 0x92, 0x69,
  28384. 0x9C, 0x70, 0x39, 0xD2, 0x7C, 0x39, 0x36, 0x2B,
  28385. 0x27, 0xB8, 0xFC, 0x6F, 0x07, 0xA8, 0xA3, 0xD4,
  28386. 0x41, 0x0F, 0x15, 0x47, 0xC4, 0x8A, 0x99, 0x97,
  28387. 0xF6, 0x2C, 0x61, 0x07, 0x44, 0x52, 0xEF, 0x15,
  28388. 0x15, 0xF8, 0xA6, 0x49, 0xEB, 0xCA, 0x94, 0x37,
  28389. 0x20, 0x5A, 0x4E, 0x8A, 0x61, 0x60, 0x6B, 0x41,
  28390. 0xDA, 0xF6, 0x83, 0x4D, 0x67, 0x1F, 0x4D, 0x85,
  28391. 0x2C, 0x0C, 0x9C, 0x40, 0x96, 0x61, 0x16, 0x48,
  28392. 0xC6, 0xA3, 0x17, 0x06, 0x78, 0xB1, 0x53, 0x7C,
  28393. 0xC1, 0x82, 0x8D, 0x93, 0x58, 0x0C, 0x9E, 0x58,
  28394. 0x49, 0xA9, 0x65, 0x31, 0x75, 0xAC, 0xB7, 0x53,
  28395. 0xF2, 0xBE, 0x74, 0x37, 0xBE, 0x45, 0xF6, 0xC6,
  28396. 0x03, 0xE4, 0x85, 0xF2, 0xEC, 0x30, 0x1B, 0xB4,
  28397. 0x2B, 0x6C, 0x37, 0xC2, 0x25, 0xD7, 0x49, 0x5A,
  28398. 0x58, 0x4A, 0xE2, 0x31, 0x89, 0x0A, 0xB5, 0xC8,
  28399. 0xC3, 0x5C, 0x26, 0x8C, 0xF4, 0xBB, 0xB0, 0x21,
  28400. 0x3C, 0x09, 0x60, 0x19, 0x31, 0x95, 0x61, 0xA8,
  28401. 0xA6, 0x94, 0x76, 0x37, 0xAA, 0x40, 0xD0, 0x06,
  28402. 0xB4, 0x15, 0xBB, 0x2C, 0xFA, 0x22, 0x37, 0xE0,
  28403. 0x89, 0x0B, 0x6A, 0x3B, 0xC1, 0x34, 0xAB, 0xF8,
  28404. 0xF6, 0x58, 0x5E, 0x10, 0x8D, 0x15, 0x94, 0x0F,
  28405. 0x91, 0xF4, 0xBF, 0x5B, 0x0C, 0x81, 0x80, 0x55,
  28406. 0xB2, 0x1D, 0xEA, 0x6E, 0x63, 0xB5, 0x53, 0x98,
  28407. 0x8C, 0x47, 0xF4, 0xB9, 0x4E, 0x7C, 0xF8, 0x00,
  28408. 0xA4, 0x93, 0xB4, 0x73, 0x47, 0x05, 0xED, 0xC5,
  28409. 0x6A, 0x4B, 0x60, 0x21, 0xC6, 0x29, 0x50, 0x06,
  28410. 0x75, 0x87, 0x68, 0x04, 0xCF, 0x0B, 0x95, 0x1F,
  28411. 0x03, 0x8A, 0x5C, 0x7F, 0xE5, 0x8E, 0x89, 0x77,
  28412. 0x4E, 0xF2, 0x99, 0x2F, 0xD7, 0xC6, 0x30, 0x99,
  28413. 0xD3, 0x52, 0xA7, 0xD2, 0x15, 0x60, 0xB7, 0x88,
  28414. 0xB4, 0x05, 0x70, 0x98, 0x61, 0x81, 0x7E, 0x59,
  28415. 0xA9, 0x6B, 0x3A, 0x3A, 0x83, 0xCB, 0xA8, 0x03,
  28416. 0xB1, 0x69, 0x34, 0x33, 0x10, 0x71, 0x90, 0x5B,
  28417. 0xBE, 0xC6, 0x53, 0x29, 0x00, 0x15, 0x5D, 0x8A,
  28418. 0xC8, 0x8C, 0xB3, 0x2E, 0x4E, 0x21, 0xA3, 0xBD,
  28419. 0x3A, 0x03, 0xFD, 0xEC, 0x32, 0x5A, 0x51, 0xCD,
  28420. 0x27, 0x73, 0x96, 0x4E, 0x67, 0x84, 0xFC, 0xF1,
  28421. 0x85, 0x37, 0x37, 0xAA, 0x64, 0xEB, 0x67, 0x56,
  28422. 0x47, 0x27, 0x27, 0x26, 0x61, 0xAB, 0xF8, 0x43,
  28423. 0x13, 0xA5, 0x7A, 0x44, 0xB1, 0x23, 0xC6, 0x55,
  28424. 0x09, 0xCF, 0xB7, 0xA6, 0xF6, 0x64, 0x1C, 0xDC,
  28425. 0xC3, 0xB5, 0x7F, 0xE6, 0x28, 0xC7, 0xB8, 0x19,
  28426. 0x2D, 0xB4, 0x4F, 0xFB, 0xF5, 0x79, 0x6A, 0x86,
  28427. 0x13, 0xB1, 0xFA, 0x12, 0x6F, 0x60, 0x76, 0x88,
  28428. 0x3C, 0x78, 0x3D, 0xC2, 0x4E, 0x2A, 0x44, 0x64,
  28429. 0xC4, 0x0B, 0x3A, 0x41, 0xCA, 0x70, 0xAE, 0x87,
  28430. 0x62, 0x08, 0x66, 0xCF, 0x4F, 0xCB, 0x2B, 0xD2,
  28431. 0x04, 0xBF, 0x5C, 0x28, 0x38, 0x12, 0xBA, 0x05,
  28432. 0x6A, 0xC0, 0xC3, 0x45, 0xE3, 0x79, 0xC4, 0xBA,
  28433. 0x24, 0xD7, 0x50, 0x90, 0x12, 0x79, 0xBB, 0x2F,
  28434. 0x3A, 0x16, 0xF6, 0x12, 0xBF, 0xAD, 0xB3, 0x57,
  28435. 0x03, 0x33, 0x2C, 0x7C, 0x13, 0x6F, 0x68, 0xEA,
  28436. 0xB6, 0x75, 0x5C, 0x66, 0xB6, 0xA4, 0xAD, 0x1A,
  28437. 0xAB, 0xA7, 0xB7, 0x68, 0xA5, 0x8A, 0xCA, 0xAC,
  28438. 0xC1, 0x0A, 0x45, 0x9A, 0x1C, 0xC8, 0xEF, 0x29,
  28439. 0x37, 0x7B, 0xC2, 0x00, 0xE4, 0xD3, 0x15, 0xA3,
  28440. 0x0A, 0x6B, 0xCC, 0x32, 0x56, 0xF9, 0x73, 0x4D,
  28441. 0x06, 0xE9, 0x77, 0x9C, 0xAA, 0x54, 0x42, 0xA9,
  28442. 0xA1, 0x60, 0x69, 0x08, 0x13, 0x77, 0xC7, 0x6E,
  28443. 0x75, 0x15, 0x43, 0x68, 0x07, 0x2D, 0xC4, 0x46,
  28444. 0xED, 0x6C, 0x8B, 0x8E, 0x62, 0x2A, 0x21, 0xE3,
  28445. 0x83, 0xCF, 0x9B, 0xA1, 0xFB, 0x43, 0x4E, 0x2E,
  28446. 0xCC, 0x81, 0xE7, 0xB7, 0x8C, 0xEE, 0x98, 0x6B,
  28447. 0x8F, 0xF7, 0x98, 0xAB, 0x18, 0xCF, 0x96, 0x34,
  28448. 0x54, 0x35, 0x46, 0x28, 0x4E, 0xDA, 0x2A, 0x26,
  28449. 0xB4, 0x7F, 0x05, 0xB7, 0x35, 0xBC, 0xDB, 0x12,
  28450. 0x02, 0x22, 0x00, 0x76, 0xDC, 0x8B, 0x4E, 0x4B,
  28451. 0x9F, 0x85, 0x35, 0x33, 0xC8, 0xF6, 0xC7, 0xFF,
  28452. 0x38, 0x81, 0x7B, 0xA4, 0x97, 0x12, 0x83, 0x57,
  28453. 0x85, 0xF1, 0x7F, 0x14, 0xCA, 0x01, 0xD0, 0xC1,
  28454. 0xC1, 0xE9, 0x88, 0x10, 0xFE, 0x0B, 0x36, 0xE5,
  28455. 0xB4, 0x27, 0x15, 0x7B, 0x94, 0x18, 0x44, 0x9C,
  28456. 0xED, 0xD6, 0x41, 0xA4, 0x29, 0x3C, 0x85, 0xC3,
  28457. 0x27, 0x00, 0x10, 0x2A, 0xCE, 0xC2, 0x2E, 0xBA,
  28458. 0xD9, 0x8E, 0xD1, 0x60, 0xA5, 0xF0, 0x27, 0xBD,
  28459. 0x4C, 0xDA, 0x57, 0xF1, 0xF3, 0x72, 0x0A, 0x12,
  28460. 0xC1, 0x34, 0x65, 0x4D, 0xD5, 0xE7, 0x3F, 0x82,
  28461. 0x96, 0x76, 0x49, 0x53, 0x90, 0xD0, 0xE7, 0x92,
  28462. 0x9D, 0x60, 0x34, 0xE9, 0xC5, 0x5F, 0x7D, 0x55,
  28463. 0xBA, 0x65, 0x8B, 0xC5, 0x87, 0x98, 0x8E, 0x8A,
  28464. 0xF9, 0x49, 0x60, 0xF6, 0xCF, 0xB8, 0xD5, 0xAF,
  28465. 0x7A, 0x00, 0x21, 0x53, 0x5A, 0x6E, 0x25, 0xE4,
  28466. 0x37, 0xD4, 0x9A, 0x78, 0x06, 0x98, 0xBE, 0x22,
  28467. 0xAC, 0x99, 0x53, 0x94, 0x9F, 0x57, 0x1B, 0x85,
  28468. 0xA6, 0x85, 0x72, 0x5F, 0x82, 0x07, 0xA2, 0xB0,
  28469. 0xAE, 0x84, 0x9B, 0x60, 0x1A, 0xB9, 0x1B, 0x15,
  28470. 0x9B, 0x3D, 0xF4, 0xA1, 0x54, 0xC2, 0x04, 0x1E,
  28471. 0x77, 0x60, 0x70, 0xAF, 0xC4, 0x29, 0x69, 0x32,
  28472. 0x23, 0x80, 0x91, 0x7C, 0x97, 0x51, 0x07, 0x99,
  28473. 0xF3, 0x14, 0x91, 0x31, 0x47, 0x7E, 0x16, 0x66,
  28474. 0x3D, 0x31, 0x74, 0xC7, 0xC1, 0xCA, 0xEA, 0x78,
  28475. 0x85, 0x35, 0xC6, 0xC0, 0x05, 0xA6, 0x4F, 0x28,
  28476. 0x68, 0x63, 0x1B, 0x31, 0xB6, 0x6E, 0x20, 0x5F,
  28477. 0xD3, 0x8C, 0x1D, 0x84, 0x54, 0x2D, 0x0F, 0x1B,
  28478. 0x57, 0x8F, 0x58, 0xC9, 0xBF, 0x5A, 0x0F, 0xAE,
  28479. 0xAB, 0x6A, 0xB6, 0x49, 0x48, 0x93, 0x05, 0x31,
  28480. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  28481. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  28482. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  28483. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  28484. 0x8A, 0x39, 0xE8, 0x7D, 0x53, 0x1F, 0x35, 0x27,
  28485. 0xC2, 0x07, 0xED, 0xCC, 0x1D, 0xB7, 0xFA, 0xDD,
  28486. 0xCF, 0x96, 0x28, 0x39, 0x18, 0x79, 0xB3, 0x35,
  28487. 0xC7, 0x07, 0x83, 0x9A, 0x0D, 0xB0, 0x51, 0xA8,
  28488. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  28489. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  28490. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  28491. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  28492. };
  28493. const byte kyber1024_ct[] = {
  28494. 0xA6, 0xAF, 0x29, 0xD5, 0xF5, 0xB8, 0x0B, 0xD1,
  28495. 0x30, 0xF5, 0x18, 0xBA, 0xDD, 0xD6, 0xC8, 0xF1,
  28496. 0x75, 0x45, 0x41, 0x3D, 0x86, 0x0F, 0xB3, 0xDE,
  28497. 0x45, 0x19, 0x79, 0xEB, 0xFA, 0x5E, 0x4E, 0x31,
  28498. 0x12, 0xC7, 0xC0, 0xAD, 0xF9, 0x98, 0x24, 0xBB,
  28499. 0x52, 0x6F, 0x2C, 0x35, 0x50, 0x74, 0x8E, 0xD0,
  28500. 0xE1, 0x34, 0xF0, 0x45, 0x7A, 0x7C, 0x61, 0xF9,
  28501. 0xF5, 0x26, 0xF0, 0x02, 0xBA, 0xAD, 0xC0, 0x3F,
  28502. 0xC1, 0x3E, 0x38, 0x13, 0x12, 0x19, 0x51, 0x3C,
  28503. 0x3E, 0xDE, 0x06, 0x16, 0x61, 0xE7, 0x4F, 0x60,
  28504. 0x3C, 0x4F, 0xCF, 0x79, 0x51, 0xC8, 0xE5, 0x2C,
  28505. 0x9C, 0x21, 0x3B, 0x0D, 0x22, 0xD9, 0x29, 0x36,
  28506. 0x63, 0xD6, 0x69, 0xA6, 0xB5, 0x8E, 0xD8, 0xFC,
  28507. 0xEF, 0xCF, 0x82, 0x49, 0xD7, 0xBB, 0x52, 0x98,
  28508. 0xF5, 0x57, 0x61, 0x44, 0x5B, 0x2B, 0x83, 0xCE,
  28509. 0x7F, 0x00, 0x5C, 0xB0, 0x42, 0x48, 0xAE, 0xC8,
  28510. 0xBD, 0xA2, 0x2F, 0xD2, 0xD4, 0x2A, 0xA7, 0x66,
  28511. 0x32, 0x20, 0x14, 0xEA, 0x03, 0x8C, 0xC3, 0x2C,
  28512. 0x55, 0xC8, 0xE4, 0xB9, 0xE2, 0x8E, 0xC9, 0x11,
  28513. 0x9F, 0x52, 0x73, 0x41, 0xE4, 0xF6, 0x6A, 0x03,
  28514. 0x51, 0x21, 0x07, 0x3B, 0x85, 0xDE, 0x67, 0x06,
  28515. 0xDA, 0x19, 0xE0, 0x83, 0x8A, 0x9F, 0x33, 0xB7,
  28516. 0x19, 0xA6, 0x8F, 0x03, 0x9B, 0x66, 0x4D, 0xC0,
  28517. 0x02, 0x65, 0x9E, 0xAB, 0xFC, 0x39, 0x86, 0x79,
  28518. 0xAA, 0x70, 0x09, 0xCE, 0x0C, 0xD0, 0x1C, 0xDA,
  28519. 0xFB, 0x6C, 0xD2, 0xA2, 0x6F, 0xE4, 0x10, 0x16,
  28520. 0x72, 0xC9, 0x8F, 0xF5, 0x8F, 0x7C, 0x47, 0xD5,
  28521. 0xBD, 0xA2, 0x90, 0x66, 0x53, 0xB3, 0xA6, 0xF9,
  28522. 0x65, 0x1F, 0x7A, 0x12, 0x1E, 0xA7, 0x7E, 0xA7,
  28523. 0x47, 0x23, 0xFA, 0xE5, 0xB8, 0x73, 0xF9, 0xBB,
  28524. 0x7B, 0x66, 0x4F, 0x0C, 0x8A, 0x93, 0x83, 0x1E,
  28525. 0xF9, 0xD5, 0x1C, 0x7C, 0xC1, 0xEF, 0x44, 0xAC,
  28526. 0x0E, 0x55, 0xA5, 0x5C, 0xA7, 0x6D, 0x13, 0x7F,
  28527. 0xE9, 0xB7, 0x5F, 0x40, 0x50, 0x9C, 0xEF, 0x15,
  28528. 0x6E, 0x5A, 0xD1, 0x8F, 0x9F, 0xB9, 0x99, 0x68,
  28529. 0x00, 0x08, 0xE5, 0x47, 0xD5, 0x5E, 0xEC, 0xD5,
  28530. 0xB4, 0xD1, 0xCB, 0x1D, 0x9F, 0x07, 0x6C, 0xEC,
  28531. 0x21, 0x50, 0x1C, 0x74, 0x02, 0x50, 0x9E, 0xCB,
  28532. 0x77, 0xAF, 0xB2, 0xCB, 0x9A, 0x61, 0x34, 0x0A,
  28533. 0x8B, 0xD1, 0x51, 0x4C, 0x6E, 0x71, 0xB4, 0xAA,
  28534. 0x45, 0xE4, 0x7E, 0xC3, 0x75, 0x12, 0x27, 0x1B,
  28535. 0x91, 0x1F, 0x8F, 0xB4, 0x6C, 0x90, 0x82, 0xC9,
  28536. 0xDF, 0x07, 0x20, 0x4A, 0xBB, 0x5A, 0x50, 0xE6,
  28537. 0xE3, 0x64, 0x7A, 0x8A, 0xD4, 0xD8, 0xD5, 0xD7,
  28538. 0xBF, 0xF1, 0x9C, 0x8A, 0x50, 0x93, 0x08, 0xBC,
  28539. 0xFB, 0x89, 0x55, 0x36, 0xD0, 0x45, 0xCA, 0x2B,
  28540. 0x97, 0xCB, 0x16, 0xA2, 0x9B, 0xB7, 0x18, 0x1C,
  28541. 0xAD, 0x05, 0x09, 0xDD, 0xB9, 0x17, 0x35, 0x02,
  28542. 0x8E, 0xBA, 0x8C, 0x31, 0xD7, 0x4B, 0xD2, 0x75,
  28543. 0xEA, 0xA6, 0x5B, 0x53, 0x40, 0xB3, 0xA4, 0x3F,
  28544. 0xBF, 0xE0, 0xB3, 0x06, 0x1D, 0x6B, 0xAE, 0x7E,
  28545. 0x75, 0xB7, 0x09, 0x8C, 0xDA, 0xBE, 0x91, 0xD4,
  28546. 0xB3, 0x1E, 0x36, 0xC9, 0xAA, 0x7A, 0x82, 0x98,
  28547. 0x86, 0x2A, 0xD6, 0x3C, 0x8F, 0xD2, 0x82, 0xE0,
  28548. 0x3B, 0x46, 0x0B, 0x3A, 0xB4, 0x64, 0xCE, 0x0F,
  28549. 0x27, 0xB1, 0xC3, 0xD1, 0x11, 0x55, 0xAC, 0xAA,
  28550. 0x01, 0x1E, 0xB9, 0xE2, 0xAE, 0x3E, 0x6D, 0xDA,
  28551. 0x07, 0xD6, 0xF4, 0x91, 0x73, 0x7C, 0xBC, 0xE9,
  28552. 0xB0, 0x5F, 0x9B, 0xC5, 0x6B, 0xE2, 0x0E, 0x8D,
  28553. 0x32, 0x6B, 0xA1, 0x32, 0xC5, 0x7F, 0xB2, 0x35,
  28554. 0x16, 0x11, 0x44, 0x51, 0x9C, 0xDF, 0x40, 0x56,
  28555. 0x0F, 0xBE, 0x27, 0x9B, 0xDE, 0x41, 0x1E, 0x11,
  28556. 0x25, 0x31, 0xF8, 0x26, 0xD6, 0xAB, 0x10, 0xD4,
  28557. 0x54, 0x73, 0x50, 0xAD, 0xD2, 0xA9, 0xDE, 0x8D,
  28558. 0x62, 0xC2, 0xAC, 0x82, 0xCA, 0xBE, 0x68, 0x15,
  28559. 0x64, 0x6F, 0x4D, 0xC9, 0x74, 0x2B, 0xB0, 0xC2,
  28560. 0xA3, 0xF7, 0x7E, 0xC7, 0xB4, 0x6C, 0x6B, 0x53,
  28561. 0x76, 0x05, 0xFA, 0x31, 0x79, 0x8C, 0xD8, 0x92,
  28562. 0x81, 0x22, 0x1A, 0x33, 0xDF, 0xB9, 0x79, 0x6E,
  28563. 0x64, 0x43, 0x05, 0x63, 0x03, 0x32, 0xC2, 0xCB,
  28564. 0x93, 0x14, 0x08, 0xAB, 0x48, 0x1A, 0x16, 0xD9,
  28565. 0x53, 0xF6, 0xBE, 0xAE, 0x38, 0x91, 0xD6, 0xD9,
  28566. 0xAC, 0x1F, 0xAB, 0x38, 0x22, 0x2D, 0x92, 0x71,
  28567. 0x87, 0x2D, 0x9D, 0x0C, 0xAD, 0xB9, 0x1A, 0xBE,
  28568. 0x9B, 0x4E, 0x26, 0x5F, 0x75, 0xC6, 0xE5, 0xE8,
  28569. 0x29, 0xE1, 0x46, 0xC3, 0xD8, 0xCE, 0x1E, 0x9D,
  28570. 0x12, 0xE0, 0xD1, 0x29, 0x80, 0x19, 0x57, 0xF4,
  28571. 0x6B, 0x0D, 0x2D, 0xBE, 0x1F, 0x74, 0x9B, 0x1D,
  28572. 0x08, 0xE2, 0x34, 0x5F, 0x62, 0x39, 0xA7, 0x31,
  28573. 0x34, 0x2E, 0xB7, 0x5B, 0x0C, 0xF1, 0xBF, 0x41,
  28574. 0x17, 0x49, 0xBC, 0x2C, 0xAF, 0x28, 0x10, 0xB7,
  28575. 0x88, 0xC6, 0xB7, 0x23, 0x8B, 0x4D, 0x3D, 0xA2,
  28576. 0xD6, 0x31, 0x5C, 0xE9, 0x54, 0x2E, 0x24, 0x40,
  28577. 0x4F, 0x14, 0x57, 0x55, 0xA3, 0x0A, 0xB8, 0x51,
  28578. 0xE4, 0x44, 0x58, 0x41, 0xBD, 0x33, 0xF7, 0x16,
  28579. 0xA5, 0x86, 0x88, 0x48, 0x88, 0xEC, 0xC6, 0xBC,
  28580. 0x64, 0x98, 0xAA, 0x32, 0x91, 0x9A, 0xE8, 0x1D,
  28581. 0x20, 0xC2, 0x69, 0x73, 0xC2, 0xBD, 0x54, 0x58,
  28582. 0x2A, 0x0F, 0x6A, 0xD9, 0x8A, 0xBF, 0xD2, 0x62,
  28583. 0x7E, 0x15, 0x69, 0x0A, 0x72, 0x7E, 0x69, 0xF5,
  28584. 0x81, 0xDD, 0x2A, 0x71, 0x27, 0x98, 0x2A, 0x90,
  28585. 0xE3, 0x3E, 0x2D, 0x4A, 0x03, 0xFE, 0x33, 0x91,
  28586. 0x42, 0xC7, 0xE4, 0x4C, 0x32, 0x6A, 0xC4, 0x6E,
  28587. 0xD3, 0x95, 0xA2, 0x25, 0xD3, 0x03, 0x33, 0x89,
  28588. 0x91, 0x73, 0x28, 0xB4, 0x53, 0x16, 0xB1, 0x58,
  28589. 0x5A, 0x01, 0xB2, 0xC3, 0x04, 0xB2, 0x94, 0x4E,
  28590. 0x90, 0x3A, 0xBB, 0xB3, 0xEC, 0x56, 0x19, 0x44,
  28591. 0x1C, 0xFC, 0x89, 0x65, 0xA4, 0x46, 0xDF, 0x75,
  28592. 0xDE, 0xFA, 0x80, 0xC6, 0xE1, 0x5A, 0xDB, 0xD5,
  28593. 0x06, 0xB7, 0xAB, 0x2D, 0xE1, 0x2D, 0xDA, 0x9B,
  28594. 0xC8, 0x14, 0x41, 0xCF, 0xC8, 0x90, 0x52, 0xE2,
  28595. 0xE5, 0x80, 0x8F, 0x71, 0x26, 0xC6, 0xFD, 0x3A,
  28596. 0xC6, 0xAC, 0x80, 0x81, 0x25, 0x8A, 0x84, 0xA0,
  28597. 0x9A, 0xE5, 0x0F, 0x6C, 0xD7, 0xCC, 0x0F, 0x4A,
  28598. 0xF3, 0x36, 0xFD, 0x1D, 0x64, 0x3E, 0x99, 0x07,
  28599. 0x99, 0x96, 0x26, 0x8C, 0x2D, 0x32, 0xD9, 0x09,
  28600. 0xF2, 0x2E, 0x35, 0x04, 0xF0, 0x7F, 0xBB, 0x56,
  28601. 0x31, 0x96, 0xD4, 0x31, 0x2F, 0xDD, 0xB9, 0x33,
  28602. 0x5D, 0x5C, 0x1D, 0x36, 0xE8, 0xC5, 0xEE, 0xA2,
  28603. 0x27, 0x8D, 0xBA, 0x23, 0xB9, 0x4D, 0x19, 0x3C,
  28604. 0x94, 0x7C, 0xC4, 0x1C, 0xA9, 0x93, 0xDC, 0x7D,
  28605. 0xB1, 0x39, 0x63, 0x40, 0xAD, 0x9C, 0x4F, 0xE6,
  28606. 0x87, 0xDD, 0x7B, 0x8D, 0x0C, 0x7A, 0x51, 0x20,
  28607. 0xAE, 0x02, 0x04, 0xF2, 0xC6, 0x65, 0xBD, 0x5F,
  28608. 0x47, 0x3D, 0x64, 0x4C, 0x7F, 0xF2, 0x6B, 0xFF,
  28609. 0xBA, 0x7A, 0x36, 0x98, 0x08, 0x30, 0x70, 0x21,
  28610. 0x28, 0xA7, 0xE6, 0x61, 0xD6, 0x77, 0xA0, 0x92,
  28611. 0xA3, 0x6E, 0x74, 0x28, 0xA4, 0x13, 0x9F, 0xB2,
  28612. 0x9B, 0x00, 0x95, 0xCC, 0x11, 0x08, 0x6F, 0x44,
  28613. 0x7D, 0x2A, 0x9E, 0xF6, 0xC9, 0xB1, 0x61, 0xF1,
  28614. 0x89, 0xC6, 0x29, 0x9E, 0x08, 0x4C, 0xB7, 0xAA,
  28615. 0x00, 0xFA, 0xF7, 0x87, 0x79, 0x7B, 0xFB, 0x06,
  28616. 0x9F, 0xBC, 0x08, 0x7F, 0xDE, 0x26, 0x25, 0x2A,
  28617. 0x16, 0x64, 0xF1, 0x9C, 0x5A, 0x8A, 0x22, 0xEC,
  28618. 0x5E, 0xE1, 0xAE, 0xB0, 0x76, 0x35, 0x7B, 0x7D,
  28619. 0xC3, 0x7E, 0x6B, 0x0F, 0x15, 0x20, 0xF9, 0x58,
  28620. 0xF7, 0x85, 0x1B, 0xAC, 0xB9, 0x2C, 0x89, 0xFD,
  28621. 0x11, 0x4A, 0x72, 0xFE, 0xAC, 0x54, 0x65, 0x2D,
  28622. 0x45, 0xB0, 0x9E, 0x1A, 0xE7, 0x65, 0x1A, 0xBD,
  28623. 0x16, 0x4B, 0xCD, 0x53, 0x7D, 0x58, 0xFA, 0x39,
  28624. 0xD3, 0xEC, 0x8A, 0xCD, 0xCD, 0xF9, 0x84, 0x25,
  28625. 0x00, 0x58, 0x62, 0xFA, 0x59, 0x69, 0x2D, 0xE1,
  28626. 0x62, 0xB7, 0x7E, 0x62, 0x97, 0xC6, 0x62, 0x33,
  28627. 0x34, 0x84, 0x08, 0xA8, 0xAB, 0x69, 0x5C, 0xE2,
  28628. 0xF2, 0x72, 0x8D, 0xB9, 0xFB, 0xE2, 0x7E, 0x95,
  28629. 0x89, 0x67, 0xEC, 0x59, 0x74, 0x76, 0x7C, 0x5A,
  28630. 0x66, 0x02, 0x30, 0x74, 0xB4, 0xA7, 0x1A, 0xFD,
  28631. 0x26, 0x4A, 0xD2, 0x89, 0x0E, 0x97, 0x0A, 0x1F,
  28632. 0x31, 0xD6, 0xE3, 0x31, 0x1B, 0x73, 0x6F, 0x9F,
  28633. 0x94, 0x88, 0x79, 0x3D, 0xDC, 0x88, 0xF2, 0x34,
  28634. 0x58, 0x06, 0x42, 0x54, 0xC8, 0x2A, 0x1D, 0x9E,
  28635. 0x59, 0xEA, 0xD2, 0xFC, 0xEC, 0x40, 0xB4, 0x30,
  28636. 0x68, 0x7C, 0x4B, 0x7E, 0x28, 0x96, 0x09, 0x26,
  28637. 0xAF, 0xCA, 0xCC, 0x9B, 0xD7, 0x56, 0xA7, 0x10,
  28638. 0x88, 0xC7, 0x84, 0x50, 0xE2, 0x0A, 0x2E, 0x98,
  28639. 0x0A, 0xED, 0xE9, 0xEB, 0xED, 0xFE, 0x7F, 0xAB,
  28640. 0xD6, 0xAB, 0xFE, 0x96, 0xF9, 0x34, 0xC4, 0xB0,
  28641. 0x2C, 0x01, 0xCA, 0x19, 0x4D, 0x01, 0xB7, 0x3C,
  28642. 0x25, 0xD5, 0x99, 0x70, 0x39, 0xD3, 0xFC, 0xD0,
  28643. 0xF0, 0x99, 0x52, 0x1F, 0x70, 0xCA, 0xEE, 0x69,
  28644. 0x11, 0x0A, 0xC1, 0xFC, 0x5A, 0x99, 0x91, 0x7A,
  28645. 0xD7, 0x52, 0xFC, 0x96, 0xAD, 0xFA, 0xD7, 0x18,
  28646. 0x6D, 0x0A, 0x7C, 0x9C, 0xFE, 0x56, 0x01, 0xC0,
  28647. 0x75, 0x14, 0xEA, 0x64, 0x48, 0xD6, 0x61, 0xC5,
  28648. 0x7A, 0xA2, 0x02, 0x42, 0x10, 0x3C, 0x42, 0x76,
  28649. 0xA0, 0x70, 0xA4, 0x89, 0xA4, 0xCB, 0x6B, 0xCA,
  28650. 0x0F, 0x9E, 0xCC, 0x43, 0x79, 0xFB, 0x22, 0x02,
  28651. 0x15, 0xFD, 0x91, 0xF8, 0x10, 0x19, 0xD5, 0xB0,
  28652. 0xAE, 0x61, 0x93, 0x58, 0xB5, 0x24, 0x68, 0xF2,
  28653. 0x72, 0xC1, 0x78, 0xE3, 0xA7, 0x4C, 0xF6, 0x77,
  28654. 0x5A, 0xA9, 0x24, 0xFE, 0x32, 0x9C, 0x31, 0x75,
  28655. 0xD9, 0xE4, 0xC3, 0xE2, 0x1A, 0xB9, 0xEC, 0x83,
  28656. 0x6E, 0xDC, 0x3A, 0xCA, 0xB2, 0xE3, 0x89, 0x1E,
  28657. 0xE8, 0xDE, 0xDA, 0x51, 0x5D, 0x39, 0xAF, 0x9B,
  28658. 0x8D, 0xDD, 0x0E, 0xE7, 0xB0, 0x16, 0x4F, 0x80,
  28659. 0x5C, 0x38, 0x35, 0xF6, 0xD2, 0xBA, 0xBD, 0xB3,
  28660. 0x0E, 0xAB, 0x47, 0x56, 0xE7, 0xEC, 0x7F, 0x82,
  28661. 0x9E, 0xCE, 0x01, 0xE8, 0xEA, 0xDF, 0xBB, 0xED,
  28662. 0x12, 0xFC, 0x28, 0x3B, 0x3D, 0x4C, 0x69, 0xF5,
  28663. 0x75, 0xE7, 0xF8, 0x04, 0x17, 0x68, 0x9F, 0xDF,
  28664. 0xCF, 0xC7, 0xBE, 0x27, 0xEE, 0x3B, 0x8C, 0xDF,
  28665. 0x57, 0xAA, 0xEB, 0xEC, 0x4A, 0x95, 0xB7, 0xE5,
  28666. 0xBB, 0x58, 0x5B, 0x85, 0x22, 0x7F, 0x7C, 0x32,
  28667. 0xBE, 0x30, 0xDB, 0x3E, 0x65, 0xE4, 0x2E, 0x30,
  28668. 0xDC, 0xF5, 0xA5, 0xFA, 0x07, 0x3D, 0xBA, 0x39,
  28669. 0x9D, 0x94, 0x2F, 0x22, 0x22, 0xAD, 0xB9, 0xB9,
  28670. 0x89, 0x81, 0x02, 0xAF, 0xE5, 0x43, 0x2E, 0xDC,
  28671. 0x7F, 0x04, 0xAE, 0x34, 0xA8, 0xFE, 0xC2, 0xD8,
  28672. 0x1C, 0xB4, 0x9A, 0x9A, 0x9B, 0x43, 0x81, 0x4C,
  28673. 0xE7, 0x1D, 0x97, 0xF7, 0x26, 0xE2, 0xB1, 0xE8,
  28674. 0xF6, 0x4B, 0x50, 0xE6, 0x5D, 0xFB, 0x48, 0x16,
  28675. 0xE1, 0x2E, 0x82, 0xA3, 0x19, 0x74, 0x84, 0xA4,
  28676. 0xE9, 0xBB, 0xA4, 0xD2, 0xD6, 0x9E, 0x3F, 0x19,
  28677. 0xD0, 0xB7, 0x5C, 0x21, 0xE2, 0xBF, 0xFE, 0x9F,
  28678. 0xC0, 0xC9, 0x8C, 0xF4, 0x8A, 0x3A, 0xAF, 0x08,
  28679. 0xD4, 0x67, 0xF7, 0x26, 0x87, 0xDF, 0x01, 0x78,
  28680. 0x17, 0x4B, 0x78, 0x97, 0xF7, 0x34, 0x34, 0x9B,
  28681. 0x18, 0x1E, 0xCA, 0x86, 0xA5, 0x98, 0xA0, 0xC5,
  28682. 0xE8, 0xC2, 0x59, 0x46, 0xF2, 0x4D, 0xC5, 0x57,
  28683. 0x2B, 0xD3, 0x24, 0xA4, 0x04, 0x58, 0xA7, 0x88,
  28684. 0xE5, 0x13, 0x7F, 0x3C, 0x7A, 0x7C, 0x97, 0xFC,
  28685. 0x9F, 0x12, 0xA3, 0xC4, 0x63, 0xA8, 0xFE, 0x94,
  28686. 0x49, 0x10, 0x1C, 0xCE, 0x96, 0x6D, 0x7C, 0x00,
  28687. 0x93, 0x23, 0x93, 0x29, 0x98, 0xD5, 0x6E, 0xF4,
  28688. 0x30, 0xC7, 0x3B, 0xC2, 0x4F, 0x5D, 0x95, 0xF7,
  28689. 0x37, 0x85, 0x8D, 0xDC, 0x4F, 0x32, 0xC0, 0x13
  28690. };
  28691. const byte kyber1024_ss[] = {
  28692. 0xB1, 0x0F, 0x73, 0x94, 0x92, 0x6A, 0xD3, 0xB4,
  28693. 0x9C, 0x5D, 0x62, 0xD5, 0xAE, 0xB5, 0x31, 0xD5,
  28694. 0x75, 0x75, 0x38, 0xBC, 0xC0, 0xDA, 0x9E, 0x55,
  28695. 0x0D, 0x43, 0x8F, 0x1B, 0x61, 0xBD, 0x74, 0x19
  28696. };
  28697. ret = wc_KyberKey_Init(KYBER1024, &key, HEAP_HINT, INVALID_DEVID);
  28698. if (ret != 0)
  28699. return -20301;
  28700. ret = wc_KyberKey_MakeKeyWithRandom(&key, kyber1024_rand,
  28701. sizeof(kyber1024_rand));
  28702. if (ret != 0)
  28703. return -20302;
  28704. ret = wc_KyberKey_EncodePublicKey(&key, pub, sizeof(pub));
  28705. if (ret != 0)
  28706. return -20303;
  28707. ret = wc_KyberKey_EncodePrivateKey(&key, priv, sizeof(priv));
  28708. if (ret != 0)
  28709. return -20304;
  28710. if (XMEMCMP(pub, kyber1024_pk, sizeof(kyber1024_pk)) != 0)
  28711. return -20305;
  28712. if (XMEMCMP(priv, kyber1024_sk, sizeof(kyber1024_sk)) != 0)
  28713. return -20306;
  28714. ret = wc_KyberKey_EncapsulateWithRandom(&key, ct, ss, kyber1024enc_rand,
  28715. sizeof(kyber1024enc_rand));
  28716. if (ret != 0)
  28717. return -20307;
  28718. if (XMEMCMP(ct, kyber1024_ct, sizeof(kyber1024_ct)) != 0)
  28719. return -20308;
  28720. if (XMEMCMP(ss, kyber1024_ss, sizeof(kyber1024_ss)) != 0)
  28721. return -20309;
  28722. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, sizeof(kyber1024_ct));
  28723. if (ret != 0)
  28724. return -20310;
  28725. if (XMEMCMP(ss_dec, kyber1024_ss, sizeof(kyber1024_ss)) != 0)
  28726. return -20311;
  28727. wc_KyberKey_Free(&key);
  28728. return 0;
  28729. }
  28730. #endif /* WOLFSSL_KYBER1024 */
  28731. #endif /* WOLFSSL_WC_KYBER */
  28732. WOLFSSL_TEST_SUBROUTINE int kyber_test(void)
  28733. {
  28734. int ret;
  28735. KyberKey key;
  28736. WC_RNG rng;
  28737. int i;
  28738. byte priv[KYBER_MAX_PRIVATE_KEY_SIZE];
  28739. byte pub[KYBER_MAX_PUBLIC_KEY_SIZE];
  28740. byte priv2[KYBER_MAX_PRIVATE_KEY_SIZE];
  28741. byte pub2[KYBER_MAX_PUBLIC_KEY_SIZE];
  28742. byte ct[KYBER_MAX_CIPHER_TEXT_SIZE];
  28743. byte ss[KYBER_SS_SZ];
  28744. byte ss_dec[KYBER_SS_SZ];
  28745. int testData[][4] = {
  28746. #ifdef WOLFSSL_KYBER512
  28747. { KYBER512, KYBER512_PRIVATE_KEY_SIZE, KYBER512_PUBLIC_KEY_SIZE,
  28748. KYBER512_CIPHER_TEXT_SIZE },
  28749. #endif
  28750. #ifdef WOLFSSL_KYBER768
  28751. { KYBER768, KYBER768_PRIVATE_KEY_SIZE, KYBER768_PUBLIC_KEY_SIZE,
  28752. KYBER768_CIPHER_TEXT_SIZE },
  28753. #endif
  28754. #ifdef WOLFSSL_KYBER1024
  28755. { KYBER1024, KYBER1024_PRIVATE_KEY_SIZE, KYBER1024_PUBLIC_KEY_SIZE,
  28756. KYBER1024_CIPHER_TEXT_SIZE },
  28757. #endif
  28758. };
  28759. #ifndef HAVE_FIPS
  28760. ret = wc_InitRng_ex(&rng, HEAP_HINT, INVALID_DEVID);
  28761. #else
  28762. ret = wc_InitRng(&rng);
  28763. #endif
  28764. if (ret != 0)
  28765. return -20000;
  28766. for (i = 0; i < (int)(sizeof(testData) / sizeof(*testData)); i++) {
  28767. ret = wc_KyberKey_Init(testData[i][0], &key, HEAP_HINT, INVALID_DEVID);
  28768. if (ret != 0)
  28769. return -20001 - i * 20;
  28770. ret = wc_KyberKey_MakeKey(&key, &rng);
  28771. if (ret != 0)
  28772. return -20002 - i * 20;
  28773. ret = wc_KyberKey_EncodePublicKey(&key, pub, testData[i][2]);
  28774. if (ret != 0)
  28775. return -20003 - i * 20;
  28776. ret = wc_KyberKey_EncodePrivateKey(&key, priv, testData[i][1]);
  28777. if (ret != 0)
  28778. return -20004 - i * 20;
  28779. ret = wc_KyberKey_Init(testData[i][0], &key, HEAP_HINT, INVALID_DEVID);
  28780. if (ret != 0)
  28781. return -20005 - i * 20;
  28782. ret = wc_KyberKey_DecodePublicKey(&key, pub, testData[i][2]);
  28783. if (ret != 0)
  28784. return -20006 - i * 20;
  28785. ret = wc_KyberKey_Encapsulate(&key, ct, ss, &rng);
  28786. if (ret != 0)
  28787. return -20007 - i * 20;
  28788. ret = wc_KyberKey_EncodePublicKey(&key, pub2, testData[i][2]);
  28789. if (ret != 0)
  28790. return -20008 - i * 20;
  28791. if (XMEMCMP(pub, pub2, testData[i][2]) != 0)
  28792. return -20009 - i * 20;
  28793. ret = wc_KyberKey_Init(testData[i][0], &key, HEAP_HINT, INVALID_DEVID);
  28794. if (ret != 0)
  28795. return -20010 - i * 20;
  28796. ret = wc_KyberKey_DecodePrivateKey(&key, priv, testData[i][1]);
  28797. if (ret != 0)
  28798. return -20011 - i * 20;
  28799. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, testData[i][3]);
  28800. if (ret != 0)
  28801. return -20012 - i * 20;
  28802. if (XMEMCMP(ss, ss_dec, sizeof(ss)) != 0)
  28803. return -20013 - i * 20;
  28804. ret = wc_KyberKey_EncodePrivateKey(&key, priv2, testData[i][1]);
  28805. if (ret != 0)
  28806. return -20014 - i * 20;
  28807. if (XMEMCMP(priv, priv2, testData[i][2]) != 0)
  28808. return -20015 - i * 20;
  28809. wc_KyberKey_Free(&key);
  28810. }
  28811. wc_FreeRng(&rng);
  28812. #ifdef WOLFSSL_WC_KYBER
  28813. #ifdef WOLFSSL_KYBER512
  28814. ret = kyber512_kat();
  28815. if (ret != 0)
  28816. return ret;
  28817. #endif
  28818. #ifdef WOLFSSL_KYBER768
  28819. ret = kyber768_kat();
  28820. if (ret != 0)
  28821. return ret;
  28822. #endif
  28823. #ifdef WOLFSSL_KYBER1024
  28824. ret = kyber1024_kat();
  28825. if (ret != 0)
  28826. return ret;
  28827. #endif
  28828. #endif /* WOLFSSL_WC_KYBER */
  28829. return 0;
  28830. }
  28831. #endif /* WOLFSSL_HAVE_KYBER */
  28832. #ifdef WOLFCRYPT_HAVE_ECCSI
  28833. static int eccsi_api_test(WC_RNG* rng, EccsiKey* key, mp_int* ssk,
  28834. ecc_point* pvt)
  28835. {
  28836. int ret;
  28837. byte id[1] = { 0x00 };
  28838. int valid;
  28839. word32 sz;
  28840. byte data[256];
  28841. byte hash[WC_MAX_DIGEST_SIZE];
  28842. byte hashSz;
  28843. byte sig[257];
  28844. word32 sigSz;
  28845. ret = wc_InitEccsiKey_ex(NULL, 32, ECC_SECP256R1, HEAP_HINT, INVALID_DEVID);
  28846. if (ret != BAD_FUNC_ARG)
  28847. return -10023;
  28848. ret = wc_InitEccsiKey_ex(NULL, 32, ECC_SECP256R1, HEAP_HINT, INVALID_DEVID);
  28849. if (ret != BAD_FUNC_ARG)
  28850. return -10024;
  28851. ret = wc_InitEccsiKey(NULL, NULL, INVALID_DEVID);
  28852. if (ret != BAD_FUNC_ARG)
  28853. return -10025;
  28854. ret = wc_InitEccsiKey(NULL, HEAP_HINT, INVALID_DEVID);
  28855. if (ret != BAD_FUNC_ARG)
  28856. return -10026;
  28857. wc_FreeEccsiKey(NULL);
  28858. /* Create a valid key. */
  28859. ret = wc_InitEccsiKey(key, NULL, INVALID_DEVID);
  28860. if (ret != 0)
  28861. return -10027;
  28862. ret = wc_MakeEccsiKey(NULL, NULL);
  28863. if (ret != BAD_FUNC_ARG)
  28864. return -10028;
  28865. ret = wc_MakeEccsiKey(key, NULL);
  28866. if (ret != BAD_FUNC_ARG)
  28867. return -10029;
  28868. ret = wc_MakeEccsiKey(NULL, rng);
  28869. if (ret != BAD_FUNC_ARG)
  28870. return -10030;
  28871. ret = wc_MakeEccsiPair(NULL, NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL,
  28872. NULL);
  28873. if (ret != BAD_FUNC_ARG)
  28874. return -10031;
  28875. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, NULL);
  28876. if (ret != BAD_FUNC_ARG)
  28877. return -10032;
  28878. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, NULL, pvt);
  28879. if (ret != BAD_FUNC_ARG)
  28880. return -10033;
  28881. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, NULL, 1, ssk, pvt);
  28882. if (ret != BAD_FUNC_ARG)
  28883. return -10034;
  28884. ret = wc_MakeEccsiPair(key, NULL, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  28885. if (ret != BAD_FUNC_ARG)
  28886. return -10035;
  28887. ret = wc_MakeEccsiPair(NULL, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  28888. if (ret != BAD_FUNC_ARG)
  28889. return -10036;
  28890. /* No key set */
  28891. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  28892. if (ret != BAD_STATE_E)
  28893. return -10037;
  28894. ret = wc_ValidateEccsiPair(NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL, NULL,
  28895. NULL);
  28896. if (ret != BAD_FUNC_ARG)
  28897. return -10038;
  28898. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  28899. NULL);
  28900. if (ret != BAD_FUNC_ARG)
  28901. return -10039;
  28902. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, NULL,
  28903. &valid);
  28904. if (ret != BAD_FUNC_ARG)
  28905. return -10040;
  28906. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, NULL, pvt,
  28907. &valid);
  28908. if (ret != BAD_FUNC_ARG)
  28909. return -10041;
  28910. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, NULL, 1, ssk, pvt,
  28911. &valid);
  28912. if (ret != BAD_FUNC_ARG)
  28913. return -10042;
  28914. ret = wc_ValidateEccsiPair(NULL, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  28915. &valid);
  28916. if (ret != BAD_FUNC_ARG)
  28917. return -10043;
  28918. /* No key set */
  28919. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  28920. &valid);
  28921. if (ret != BAD_STATE_E)
  28922. return -10044;
  28923. ret = wc_ValidateEccsiPvt(NULL, NULL, NULL);
  28924. if (ret != BAD_FUNC_ARG)
  28925. return -10045;
  28926. ret = wc_ValidateEccsiPvt(key, NULL, NULL);
  28927. if (ret != BAD_FUNC_ARG)
  28928. return -10046;
  28929. ret = wc_ValidateEccsiPvt(NULL, pvt, NULL);
  28930. if (ret != BAD_FUNC_ARG)
  28931. return -10047;
  28932. ret = wc_ValidateEccsiPvt(NULL, NULL, &valid);
  28933. if (ret != BAD_FUNC_ARG)
  28934. return -10048;
  28935. ret = wc_ValidateEccsiPvt(key, pvt, NULL);
  28936. if (ret != BAD_FUNC_ARG)
  28937. return -10049;
  28938. ret = wc_ValidateEccsiPvt(key, NULL, &valid);
  28939. if (ret != BAD_FUNC_ARG)
  28940. return -10050;
  28941. ret = wc_ValidateEccsiPvt(NULL, pvt, &valid);
  28942. if (ret != BAD_FUNC_ARG)
  28943. return -10051;
  28944. ret = wc_EncodeEccsiPair(NULL, NULL, NULL, data, NULL);
  28945. if (ret != BAD_FUNC_ARG)
  28946. return -10052;
  28947. ret = wc_EncodeEccsiPair(key, ssk, pvt, data, NULL);
  28948. if (ret != BAD_FUNC_ARG)
  28949. return -10053;
  28950. ret = wc_EncodeEccsiPair(key, ssk, NULL, data, &sz);
  28951. if (ret != BAD_FUNC_ARG)
  28952. return -10054;
  28953. ret = wc_EncodeEccsiPair(key, NULL, pvt, data, &sz);
  28954. if (ret != BAD_FUNC_ARG)
  28955. return -10055;
  28956. ret = wc_EncodeEccsiPair(NULL, ssk, pvt, data, &sz);
  28957. if (ret != BAD_FUNC_ARG)
  28958. return -10056;
  28959. /* No key created so no curve information. */
  28960. ret = wc_EncodeEccsiPair(key, ssk, pvt, NULL, &sz);
  28961. if (ret != LENGTH_ONLY_E)
  28962. return -10057;
  28963. ret = wc_EncodeEccsiSsk(NULL, NULL, data, NULL);
  28964. if (ret != BAD_FUNC_ARG)
  28965. return -10058;
  28966. ret = wc_EncodeEccsiSsk(key, ssk, data, NULL);
  28967. if (ret != BAD_FUNC_ARG)
  28968. return -10059;
  28969. ret = wc_EncodeEccsiSsk(key, NULL, data, &sz);
  28970. if (ret != BAD_FUNC_ARG)
  28971. return -10060;
  28972. ret = wc_EncodeEccsiSsk(NULL, ssk, data, &sz);
  28973. if (ret != BAD_FUNC_ARG)
  28974. return -10061;
  28975. ret = wc_EncodeEccsiPvt(NULL, NULL, data, NULL, 1);
  28976. if (ret != BAD_FUNC_ARG)
  28977. return -10058;
  28978. ret = wc_EncodeEccsiPvt(key, pvt, data, NULL, 1);
  28979. if (ret != BAD_FUNC_ARG)
  28980. return -10059;
  28981. ret = wc_EncodeEccsiPvt(key, NULL, data, &sz, 1);
  28982. if (ret != BAD_FUNC_ARG)
  28983. return -10060;
  28984. ret = wc_EncodeEccsiPvt(NULL, pvt, data, &sz, 1);
  28985. if (ret != BAD_FUNC_ARG)
  28986. return -10061;
  28987. ret = wc_DecodeEccsiPair(NULL, NULL, 0, NULL, NULL);
  28988. if (ret != BAD_FUNC_ARG)
  28989. return -10062;
  28990. ret = wc_DecodeEccsiPair(key, data, 0, ssk, NULL);
  28991. if (ret != BAD_FUNC_ARG)
  28992. return -10063;
  28993. ret = wc_DecodeEccsiPair(key, data, 0, NULL, pvt);
  28994. if (ret != BAD_FUNC_ARG)
  28995. return -10064;
  28996. ret = wc_DecodeEccsiPair(key, NULL, 0, ssk, pvt);
  28997. if (ret != BAD_FUNC_ARG)
  28998. return -10065;
  28999. ret = wc_DecodeEccsiPair(NULL, data, 0, ssk, pvt);
  29000. if (ret != BAD_FUNC_ARG)
  29001. return -10066;
  29002. ret = wc_DecodeEccsiSsk(NULL, NULL, 0, NULL);
  29003. if (ret != BAD_FUNC_ARG)
  29004. return -10067;
  29005. ret = wc_DecodeEccsiSsk(key, data, 0, NULL);
  29006. if (ret != BAD_FUNC_ARG)
  29007. return -10068;
  29008. ret = wc_DecodeEccsiSsk(key, NULL, 0, ssk);
  29009. if (ret != BAD_FUNC_ARG)
  29010. return -10069;
  29011. ret = wc_DecodeEccsiSsk(NULL, data, 0, ssk);
  29012. if (ret != BAD_FUNC_ARG)
  29013. return -10070;
  29014. ret = wc_DecodeEccsiPvt(NULL, NULL, 0, NULL);
  29015. if (ret != BAD_FUNC_ARG)
  29016. return -10067;
  29017. ret = wc_DecodeEccsiPvt(key, data, 0, NULL);
  29018. if (ret != BAD_FUNC_ARG)
  29019. return -10068;
  29020. ret = wc_DecodeEccsiPvt(key, NULL, 0, pvt);
  29021. if (ret != BAD_FUNC_ARG)
  29022. return -10069;
  29023. ret = wc_DecodeEccsiPvt(NULL, data, 0, pvt);
  29024. if (ret != BAD_FUNC_ARG)
  29025. return -10070;
  29026. ret = wc_DecodeEccsiPvtFromSig(NULL, NULL, 0, NULL);
  29027. if (ret != BAD_FUNC_ARG)
  29028. return -10067;
  29029. ret = wc_DecodeEccsiPvtFromSig(key, data, 0, NULL);
  29030. if (ret != BAD_FUNC_ARG)
  29031. return -10068;
  29032. ret = wc_DecodeEccsiPvtFromSig(key, NULL, 0, pvt);
  29033. if (ret != BAD_FUNC_ARG)
  29034. return -10069;
  29035. ret = wc_DecodeEccsiPvtFromSig(NULL, data, 0, pvt);
  29036. if (ret != BAD_FUNC_ARG)
  29037. return -10070;
  29038. ret = wc_ExportEccsiKey(NULL, data, NULL);
  29039. if (ret != BAD_FUNC_ARG)
  29040. return -10071;
  29041. ret = wc_ExportEccsiKey(key, data, NULL);
  29042. if (ret != BAD_FUNC_ARG)
  29043. return -10072;
  29044. ret = wc_ExportEccsiKey(NULL, data, &sz);
  29045. if (ret != BAD_FUNC_ARG)
  29046. return -10073;
  29047. /* No key to export */
  29048. ret = wc_ExportEccsiKey(key, NULL, &sz);
  29049. if (ret != BAD_STATE_E)
  29050. return -10074;
  29051. ret = wc_ImportEccsiKey(NULL, NULL, 0);
  29052. if (ret != BAD_FUNC_ARG)
  29053. return -10075;
  29054. ret = wc_ImportEccsiKey(key, NULL, 0);
  29055. if (ret != BAD_FUNC_ARG)
  29056. return -10076;
  29057. ret = wc_ImportEccsiKey(NULL, data, 0);
  29058. if (ret != BAD_FUNC_ARG)
  29059. return -10077;
  29060. ret = wc_ExportEccsiPrivateKey(NULL, data, NULL);
  29061. if (ret != BAD_FUNC_ARG)
  29062. return -10071;
  29063. ret = wc_ExportEccsiPrivateKey(key, data, NULL);
  29064. if (ret != BAD_FUNC_ARG)
  29065. return -10072;
  29066. ret = wc_ExportEccsiPrivateKey(NULL, data, &sz);
  29067. if (ret != BAD_FUNC_ARG)
  29068. return -10073;
  29069. /* No key to export */
  29070. ret = wc_ExportEccsiPrivateKey(key, NULL, &sz);
  29071. if (ret != BAD_STATE_E)
  29072. return -10074;
  29073. ret = wc_ImportEccsiPrivateKey(NULL, NULL, 0);
  29074. if (ret != BAD_FUNC_ARG)
  29075. return -10075;
  29076. ret = wc_ImportEccsiPrivateKey(key, NULL, 0);
  29077. if (ret != BAD_FUNC_ARG)
  29078. return -10076;
  29079. ret = wc_ImportEccsiPrivateKey(NULL, data, 0);
  29080. if (ret != BAD_FUNC_ARG)
  29081. return -10077;
  29082. ret = wc_ExportEccsiPublicKey(NULL, data, NULL, 1);
  29083. if (ret != BAD_FUNC_ARG)
  29084. return -10078;
  29085. ret = wc_ExportEccsiPublicKey(key, data, NULL, 1);
  29086. if (ret != BAD_FUNC_ARG)
  29087. return -10079;
  29088. ret = wc_ExportEccsiPublicKey(NULL, data, &sz, 1);
  29089. if (ret != BAD_FUNC_ARG)
  29090. return -10080;
  29091. /* No key to export */
  29092. ret = wc_ExportEccsiPublicKey(key, data, &sz, 1);
  29093. if (ret != BAD_STATE_E)
  29094. return -10081;
  29095. ret = wc_ImportEccsiPublicKey(NULL, NULL, 0, 1);
  29096. if (ret != BAD_FUNC_ARG)
  29097. return -10082;
  29098. ret = wc_ImportEccsiPublicKey(key, NULL, 0, 1);
  29099. if (ret != BAD_FUNC_ARG)
  29100. return -10083;
  29101. ret = wc_ImportEccsiPublicKey(NULL, data, 0, 1);
  29102. if (ret != BAD_FUNC_ARG)
  29103. return -10084;
  29104. ret = wc_HashEccsiId(NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL, NULL, NULL);
  29105. if (ret != BAD_FUNC_ARG)
  29106. return -10085;
  29107. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, NULL);
  29108. if (ret != BAD_FUNC_ARG)
  29109. return -10086;
  29110. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, NULL, &hashSz);
  29111. if (ret != BAD_FUNC_ARG)
  29112. return -10087;
  29113. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, NULL, hash, &hashSz);
  29114. if (ret != BAD_FUNC_ARG)
  29115. return -10088;
  29116. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, NULL, 1, pvt, hash,
  29117. &hashSz);
  29118. if (ret != BAD_FUNC_ARG)
  29119. return -10089;
  29120. ret = wc_HashEccsiId(NULL, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, &hashSz);
  29121. if (ret != BAD_FUNC_ARG)
  29122. return -10090;
  29123. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, &hashSz);
  29124. if (ret != BAD_STATE_E)
  29125. return -10091;
  29126. ret = wc_SetEccsiHash(NULL, NULL, 1);
  29127. if (ret != BAD_FUNC_ARG)
  29128. return -10090;
  29129. ret = wc_SetEccsiHash(key, NULL, 1);
  29130. if (ret != BAD_FUNC_ARG)
  29131. return -10090;
  29132. ret = wc_SetEccsiHash(NULL, hash, 1);
  29133. if (ret != BAD_FUNC_ARG)
  29134. return -10090;
  29135. ret = wc_SetEccsiPair(NULL, NULL, NULL);
  29136. if (ret != BAD_FUNC_ARG)
  29137. return -10090;
  29138. ret = wc_SetEccsiPair(key, NULL, NULL);
  29139. if (ret != BAD_FUNC_ARG)
  29140. return -10090;
  29141. ret = wc_SetEccsiPair(NULL, ssk, NULL);
  29142. if (ret != BAD_FUNC_ARG)
  29143. return -10090;
  29144. ret = wc_SetEccsiPair(NULL, NULL, pvt);
  29145. if (ret != BAD_FUNC_ARG)
  29146. return -10090;
  29147. ret = wc_SetEccsiPair(key, ssk, NULL);
  29148. if (ret != BAD_FUNC_ARG)
  29149. return -10090;
  29150. ret = wc_SetEccsiPair(key, NULL, pvt);
  29151. if (ret != BAD_FUNC_ARG)
  29152. return -10090;
  29153. ret = wc_SetEccsiPair(NULL, ssk, pvt);
  29154. if (ret != BAD_FUNC_ARG)
  29155. return -10090;
  29156. ret = wc_SignEccsiHash(NULL, NULL, WC_HASH_TYPE_SHA256, NULL, 0, sig, NULL);
  29157. if (ret != BAD_FUNC_ARG)
  29158. return -10092;
  29159. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, sig, NULL);
  29160. if (ret != BAD_FUNC_ARG)
  29161. return -10093;
  29162. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, NULL, 0, sig,
  29163. &sigSz);
  29164. if (ret != BAD_FUNC_ARG)
  29165. return -10096;
  29166. ret = wc_SignEccsiHash(key, NULL, WC_HASH_TYPE_SHA256, data, 0, sig,
  29167. &sigSz);
  29168. if (ret != BAD_FUNC_ARG)
  29169. return -10098;
  29170. ret = wc_SignEccsiHash(NULL, rng, WC_HASH_TYPE_SHA256, data, 0, sig,
  29171. &sigSz);
  29172. if (ret != BAD_FUNC_ARG)
  29173. return -10099;
  29174. /* Key not set. */
  29175. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, NULL,
  29176. &sigSz);
  29177. if (ret != BAD_STATE_E)
  29178. return -10100;
  29179. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0, NULL);
  29180. if (ret != BAD_FUNC_ARG)
  29181. return -10101;
  29182. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0, NULL);
  29183. if (ret != BAD_FUNC_ARG)
  29184. return -10101;
  29185. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, data, 0, NULL, 0, NULL);
  29186. if (ret != BAD_FUNC_ARG)
  29187. return -10101;
  29188. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, sig, 0, NULL);
  29189. if (ret != BAD_FUNC_ARG)
  29190. return -10101;
  29191. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0,
  29192. &valid);
  29193. if (ret != BAD_FUNC_ARG)
  29194. return -10101;
  29195. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, sig, 0, NULL);
  29196. if (ret != BAD_FUNC_ARG)
  29197. return -10102;
  29198. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, NULL, 0,
  29199. &valid);
  29200. if (ret != BAD_FUNC_ARG)
  29201. return -10103;
  29202. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, NULL, 0, sig, 0,
  29203. &valid);
  29204. if (ret != BAD_FUNC_ARG)
  29205. return -10104;
  29206. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, data, 0, sig, 0,
  29207. &valid);
  29208. if (ret != BAD_FUNC_ARG)
  29209. return -10106;
  29210. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, sig, 0,
  29211. &valid);
  29212. if (ret != BAD_STATE_E)
  29213. return -10106;
  29214. ret = wc_SetEccsiPair(key, ssk, pvt);
  29215. if (ret != 0)
  29216. return -10107;
  29217. /* Identity hash not set. */
  29218. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, NULL,
  29219. &sigSz);
  29220. if (ret != BAD_STATE_E)
  29221. return -10108;
  29222. wc_FreeEccsiKey(key);
  29223. return 0;
  29224. }
  29225. /* RFC 6507: Appendix A */
  29226. static int eccsi_kat_verify_test(EccsiKey* key, ecc_point* pvt)
  29227. {
  29228. int ret;
  29229. int verified;
  29230. const byte msg[] = { 0x6D, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x00 };
  29231. word32 msgSz = sizeof(msg);
  29232. byte hash[WC_SHA256_DIGEST_SIZE];
  29233. byte hashSz = WC_SHA256_DIGEST_SIZE;
  29234. static const byte id[] = {
  29235. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  29236. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  29237. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  29238. 0x33, 0x00
  29239. };
  29240. word32 idSz = sizeof(id);
  29241. static const byte sig[] = {
  29242. 0x26, 0x9D, 0x4C, 0x8F, 0xDE, 0xB6, 0x6A, 0x74,
  29243. 0xE4, 0xEF, 0x8C, 0x0D, 0x5D, 0xCC, 0x59, 0x7D,
  29244. 0xDF, 0xE6, 0x02, 0x9C, 0x2A, 0xFF, 0xC4, 0x93,
  29245. 0x60, 0x08, 0xCD, 0x2C, 0xC1, 0x04, 0x5D, 0x81,
  29246. 0xE0, 0x9B, 0x52, 0x8D, 0x0E, 0xF8, 0xD6, 0xDF,
  29247. 0x1A, 0xA3, 0xEC, 0xBF, 0x80, 0x11, 0x0C, 0xFC,
  29248. 0xEC, 0x9F, 0xC6, 0x82, 0x52, 0xCE, 0xBB, 0x67,
  29249. 0x9F, 0x41, 0x34, 0x84, 0x69, 0x40, 0xCC, 0xFD,
  29250. 0x04,
  29251. 0x75, 0x8A, 0x14, 0x27, 0x79, 0xBE, 0x89, 0xE8,
  29252. 0x29, 0xE7, 0x19, 0x84, 0xCB, 0x40, 0xEF, 0x75,
  29253. 0x8C, 0xC4, 0xAD, 0x77, 0x5F, 0xC5, 0xB9, 0xA3,
  29254. 0xE1, 0xC8, 0xED, 0x52, 0xF6, 0xFA, 0x36, 0xD9,
  29255. 0xA7, 0x9D, 0x24, 0x76, 0x92, 0xF4, 0xED, 0xA3,
  29256. 0xA6, 0xBD, 0xAB, 0x77, 0xD6, 0xAA, 0x64, 0x74,
  29257. 0xA4, 0x64, 0xAE, 0x49, 0x34, 0x66, 0x3C, 0x52,
  29258. 0x65, 0xBA, 0x70, 0x18, 0xBA, 0x09, 0x1F, 0x79
  29259. };
  29260. word32 sigSz = sizeof(sig);
  29261. static const byte pubData[] = {
  29262. 0x50, 0xD4, 0x67, 0x0B, 0xDE, 0x75, 0x24, 0x4F,
  29263. 0x28, 0xD2, 0x83, 0x8A, 0x0D, 0x25, 0x55, 0x8A,
  29264. 0x7A, 0x72, 0x68, 0x6D, 0x45, 0x22, 0xD4, 0xC8,
  29265. 0x27, 0x3F, 0xB6, 0x44, 0x2A, 0xEB, 0xFA, 0x93,
  29266. 0xDB, 0xDD, 0x37, 0x55, 0x1A, 0xFD, 0x26, 0x3B,
  29267. 0x5D, 0xFD, 0x61, 0x7F, 0x39, 0x60, 0xC6, 0x5A,
  29268. 0x8C, 0x29, 0x88, 0x50, 0xFF, 0x99, 0xF2, 0x03,
  29269. 0x66, 0xDC, 0xE7, 0xD4, 0x36, 0x72, 0x17, 0xF4
  29270. };
  29271. static const byte expHash[] = {
  29272. 0x49, 0x0f, 0x3f, 0xeb, 0xbc, 0x1c, 0x90, 0x2f,
  29273. 0x62, 0x89, 0x72, 0x3d, 0x7f, 0x8c, 0xbf, 0x79,
  29274. 0xdb, 0x88, 0x93, 0x08, 0x49, 0xd1, 0x9f, 0x38,
  29275. 0xf0, 0x29, 0x5b, 0x5c, 0x27, 0x6c, 0x14, 0xd1
  29276. };
  29277. ret = wc_ImportEccsiPublicKey(key, pubData, sizeof(pubData), 0);
  29278. if (ret != 0)
  29279. return -10108;
  29280. ret = wc_DecodeEccsiPvtFromSig(key, sig, sigSz, pvt);
  29281. if (ret != 0)
  29282. return -10109;
  29283. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, idSz, pvt, hash,
  29284. &hashSz);
  29285. if (ret != 0)
  29286. return -10112;
  29287. if (hashSz != sizeof(expHash))
  29288. return -10113;
  29289. if (XMEMCMP(hash, expHash, hashSz) != 0)
  29290. return -10114;
  29291. ret = wc_SetEccsiHash(key, hash, hashSz);
  29292. if (ret != 0)
  29293. return -10112;
  29294. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  29295. &verified);
  29296. if (ret != 0)
  29297. return -10115;
  29298. if (!verified)
  29299. return -10116;
  29300. return 0;
  29301. }
  29302. static int eccsi_enc_dec_pair_test(EccsiKey* priv, mp_int* ssk, ecc_point* pvt)
  29303. {
  29304. int ret;
  29305. byte data[32 * 3];
  29306. word32 sz;
  29307. ecc_point* decPvt = NULL;
  29308. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29309. mp_int *decSsk = (mp_int *)XMALLOC(sizeof(*decSsk), HEAP_HINT,
  29310. DYNAMIC_TYPE_TMP_BUFFER);
  29311. if (decSsk == NULL)
  29312. return -10173;
  29313. #else
  29314. mp_int decSsk[1];
  29315. #endif
  29316. ret = mp_init(decSsk);
  29317. if (ret != 0)
  29318. ERROR_OUT(-10117, out);
  29319. decPvt = wc_ecc_new_point();
  29320. if (decPvt == NULL)
  29321. ERROR_OUT(-10118, out);
  29322. ret = wc_EncodeEccsiPair(priv, ssk, pvt, NULL, &sz);
  29323. if (ret != LENGTH_ONLY_E)
  29324. ERROR_OUT(-10119, out);
  29325. if (sz != 32 * 3)
  29326. ERROR_OUT(-10120, out);
  29327. ret = wc_EncodeEccsiPair(priv, ssk, pvt, data, &sz);
  29328. if (ret != 0)
  29329. ERROR_OUT(-10121, out);
  29330. if (sz != 32* 3)
  29331. ERROR_OUT(-10122, out);
  29332. ret = wc_DecodeEccsiPair(priv, data, sz, decSsk, decPvt);
  29333. if (ret != 0)
  29334. ERROR_OUT(-10123, out);
  29335. if (mp_cmp(ssk, decSsk) != MP_EQ)
  29336. ERROR_OUT(-10124, out);
  29337. if (wc_ecc_cmp_point(pvt, decPvt) != MP_EQ)
  29338. ERROR_OUT(-10125, out);
  29339. ret = wc_EncodeEccsiSsk(priv, ssk, NULL, &sz);
  29340. if (ret != LENGTH_ONLY_E)
  29341. ERROR_OUT(-10119, out);
  29342. if (sz != 32)
  29343. ERROR_OUT(-10120, out);
  29344. ret = wc_EncodeEccsiSsk(priv, ssk, data, &sz);
  29345. if (ret != 0)
  29346. ERROR_OUT(-10121, out);
  29347. if (sz != 32)
  29348. ERROR_OUT(-10122, out);
  29349. ret = wc_DecodeEccsiSsk(priv, data, sz, decSsk);
  29350. if (ret != 0)
  29351. ERROR_OUT(-10123, out);
  29352. if (mp_cmp(ssk, decSsk) != MP_EQ)
  29353. ERROR_OUT(-10124, out);
  29354. ret = wc_EncodeEccsiPvt(priv, pvt, NULL, &sz, 1);
  29355. if (ret != LENGTH_ONLY_E)
  29356. ERROR_OUT(-10126, out);
  29357. if (sz != 32 * 2)
  29358. ERROR_OUT(-10127, out);
  29359. ret = wc_EncodeEccsiPvt(priv, pvt, data, &sz, 1);
  29360. if (ret != 0)
  29361. ERROR_OUT(-10128, out);
  29362. if (sz != 32 * 2)
  29363. ERROR_OUT(-10129, out);
  29364. ret = wc_DecodeEccsiPvt(priv, data, sz, decPvt);
  29365. if (ret != 0)
  29366. ERROR_OUT(-10130, out);
  29367. if (wc_ecc_cmp_point(pvt, decPvt) != MP_EQ)
  29368. ERROR_OUT(-10131, out);
  29369. sz = sizeof(data);
  29370. ret = wc_EncodeEccsiPvt(priv, pvt, data, &sz, 0);
  29371. if (ret != 0)
  29372. ERROR_OUT(-10128, out);
  29373. if (sz != 32 * 2 + 1)
  29374. ERROR_OUT(-10129, out);
  29375. ret = wc_DecodeEccsiPvt(priv, data, sz, decPvt);
  29376. if (ret != 0)
  29377. ERROR_OUT(-10130, out);
  29378. if (wc_ecc_cmp_point(pvt, decPvt) != MP_EQ)
  29379. ERROR_OUT(-10131, out);
  29380. wc_ecc_del_point(decPvt);
  29381. out:
  29382. mp_free(decSsk);
  29383. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29384. XFREE(decSsk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29385. #endif
  29386. return ret;
  29387. }
  29388. static int eccsi_imp_exp_key_test(EccsiKey* priv)
  29389. {
  29390. int ret;
  29391. byte data[32 * 3];
  29392. byte out[32 * 3];
  29393. word32 sz;
  29394. ret = wc_ExportEccsiKey(priv, NULL, &sz);
  29395. if (ret != LENGTH_ONLY_E)
  29396. return -10132;
  29397. if (sz != 32 * 3)
  29398. return -10133;
  29399. ret = wc_ExportEccsiKey(priv, data, &sz);
  29400. if (ret != 0)
  29401. return -10134;
  29402. ret = wc_ImportEccsiKey(priv, data, sz);
  29403. if (ret != 0)
  29404. return -10135;
  29405. ret = wc_ExportEccsiKey(priv, NULL, &sz);
  29406. if (ret != LENGTH_ONLY_E)
  29407. return -10132;
  29408. if (sz != 32 * 3)
  29409. return -10143;
  29410. ret = wc_ExportEccsiKey(priv, out, &sz);
  29411. if (ret != 0)
  29412. return -10144;
  29413. if (sz != 32 * 3)
  29414. return -10145;
  29415. if (XMEMCMP(data, out, sz) != 0)
  29416. return -10146;
  29417. ret = wc_ExportEccsiPrivateKey(priv, NULL, &sz);
  29418. if (ret != LENGTH_ONLY_E)
  29419. return -10156;
  29420. if (sz != 32)
  29421. return -10157;
  29422. ret = wc_ExportEccsiPrivateKey(priv, data, &sz);
  29423. if (ret != 0)
  29424. return -10158;
  29425. ret = wc_ImportEccsiPrivateKey(priv, data, sz);
  29426. if (ret != 0)
  29427. return -10159;
  29428. ret = wc_ExportEccsiPrivateKey(priv, NULL, &sz);
  29429. if (ret != LENGTH_ONLY_E)
  29430. return -10152;
  29431. if (sz != 32)
  29432. return -10163;
  29433. ret = wc_ExportEccsiPrivateKey(priv, out, &sz);
  29434. if (ret != 0)
  29435. return -10164;
  29436. if (sz != 32)
  29437. return -10165;
  29438. if (XMEMCMP(data, out, sz) != 0)
  29439. return -10166;
  29440. return 0;
  29441. }
  29442. static int eccsi_imp_exp_pubkey_test(EccsiKey* key1, EccsiKey* key2)
  29443. {
  29444. int ret;
  29445. byte data[32 * 2 + 1];
  29446. byte pubData[32 * 2 + 1];
  29447. word32 sz;
  29448. ret = wc_ExportEccsiPublicKey(key1, NULL, &sz, 1);
  29449. if (ret != LENGTH_ONLY_E)
  29450. return -10136;
  29451. if (sz != 32 * 2)
  29452. return -10137;
  29453. ret = wc_ExportEccsiPublicKey(key1, data, &sz, 1);
  29454. if (ret != 0)
  29455. return -10138;
  29456. ret = wc_ImportEccsiPublicKey(key2, data, sz, 1);
  29457. if (ret != 0)
  29458. return -10139;
  29459. sz = sizeof(pubData);
  29460. ret = wc_ExportEccsiPublicKey(key2, pubData, &sz, 1);
  29461. if (ret != 0)
  29462. return -10140;
  29463. if (sz != 32 * 2)
  29464. return -10141;
  29465. if (XMEMCMP(data, pubData, sz) != 0)
  29466. return -10142;
  29467. sz = sizeof(pubData);
  29468. ret = wc_ExportEccsiPublicKey(key2, pubData, &sz, 0);
  29469. if (ret != 0)
  29470. return -10140;
  29471. if (sz != 32 * 2 + 1)
  29472. return -10141;
  29473. if (pubData[0] != 0x04)
  29474. return -10140;
  29475. if (XMEMCMP(pubData + 1, data, sz - 1) != 0)
  29476. return -10142;
  29477. ret = wc_ImportEccsiPublicKey(key2, pubData, sz, 0);
  29478. if (ret != 0)
  29479. return -10139;
  29480. return 0;
  29481. }
  29482. static int eccsi_make_key_test(EccsiKey* priv, EccsiKey* pub, WC_RNG* rng,
  29483. mp_int* ssk, ecc_point* pvt)
  29484. {
  29485. int ret;
  29486. char mail[] = "test@wolfssl.com";
  29487. byte* id = (byte*)mail;
  29488. word32 idSz = (word32) XSTRLEN(mail);
  29489. int valid;
  29490. ret = wc_MakeEccsiKey(priv, rng);
  29491. if (ret != 0)
  29492. return -10143;
  29493. ret = eccsi_imp_exp_key_test(priv);
  29494. if (ret < 0)
  29495. return ret;
  29496. ret = eccsi_imp_exp_pubkey_test(priv, pub);
  29497. if (ret < 0)
  29498. return ret;
  29499. ret = wc_MakeEccsiPair(priv, rng, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt);
  29500. if (ret != 0)
  29501. return -10144;
  29502. ret = wc_ValidateEccsiPair(pub, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt,
  29503. &valid);
  29504. if (ret != 0)
  29505. return -10145;
  29506. if (!valid)
  29507. return -10146;
  29508. ret = eccsi_enc_dec_pair_test(priv, ssk, pvt);
  29509. if (ret != 0)
  29510. return ret;
  29511. return 0;
  29512. }
  29513. static int eccsi_sign_verify_test(EccsiKey* priv, EccsiKey* pub, WC_RNG* rng,
  29514. mp_int* ssk, ecc_point* pvt)
  29515. {
  29516. int ret;
  29517. byte hashPriv[WC_MAX_DIGEST_SIZE];
  29518. byte hashPub[WC_MAX_DIGEST_SIZE];
  29519. byte hashSz;
  29520. byte sig[144];
  29521. word32 sigSz;
  29522. int verified, valid;
  29523. char mail[] = "test@wolfssl.com";
  29524. byte* id = (byte*)mail;
  29525. word32 idSz = (word32) XSTRLEN(mail);
  29526. byte msg[] = { 0x00 };
  29527. word32 msgSz = sizeof(msg);
  29528. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA256, id, idSz, pvt, hashPriv,
  29529. &hashSz);
  29530. if (ret != 0)
  29531. return -10147;
  29532. if (hashSz != 32)
  29533. return -10148;
  29534. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA256, id, idSz, pvt, hashPub,
  29535. &hashSz);
  29536. if (ret != 0)
  29537. return -10149;
  29538. if (hashSz != 32)
  29539. return -10150;
  29540. if (XMEMCMP(hashPriv, hashPub, hashSz) != 0)
  29541. return -10151;
  29542. ret = wc_SetEccsiHash(priv, hashPriv, hashSz);
  29543. if (ret != 0)
  29544. return -10149;
  29545. ret = wc_SetEccsiPair(priv, ssk, pvt);
  29546. if (ret != 0)
  29547. return -10149;
  29548. ret = wc_SignEccsiHash(priv, rng, WC_HASH_TYPE_SHA256, msg, msgSz, NULL,
  29549. &sigSz);
  29550. if (ret != LENGTH_ONLY_E)
  29551. return -10152;
  29552. if (sigSz != 129)
  29553. return -10153;
  29554. ret = wc_SignEccsiHash(priv, rng, WC_HASH_TYPE_SHA256, msg, msgSz, sig,
  29555. &sigSz);
  29556. if (ret != 0)
  29557. return -10154;
  29558. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  29559. if (ret != 0)
  29560. return -10149;
  29561. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  29562. &verified);
  29563. if (ret != 0)
  29564. return -10155;
  29565. if (!verified)
  29566. return -10156;
  29567. /* Check that changing HS results in verification failure. */
  29568. hashPub[0] ^= 0x80;
  29569. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  29570. if (ret != 0)
  29571. return -10149;
  29572. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  29573. &verified);
  29574. if (ret != 0)
  29575. return -10157;
  29576. if (verified)
  29577. return -10158;
  29578. hashPub[0] ^= 0x80;
  29579. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  29580. if (ret != 0)
  29581. return -10149;
  29582. /* Check that changing msg results in verification failure. */
  29583. msg[0] ^= 0x80;
  29584. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  29585. &verified);
  29586. if (ret != 0)
  29587. return -10159;
  29588. if (verified)
  29589. return -10160;
  29590. msg[0] ^= 0x80;
  29591. /* Check that changing signature results in verification failure. */
  29592. sig[0] ^= 0x80;
  29593. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  29594. &verified);
  29595. if (ret != 0)
  29596. return -10161;
  29597. if (verified)
  29598. return -10162;
  29599. sig[0] ^= 0x80;
  29600. /* Check that key state hasn't been invalidated. */
  29601. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  29602. &verified);
  29603. if (ret != 0)
  29604. return -10163;
  29605. if (!verified)
  29606. return -10164;
  29607. /* Check that verifying with the private key works. */
  29608. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  29609. &verified);
  29610. if (ret != 0)
  29611. return -10165;
  29612. if (!verified)
  29613. return -10166;
  29614. /* Check that the KPAK is converted from montgomery form. */
  29615. ret = eccsi_imp_exp_key_test(priv);
  29616. if (ret != 0)
  29617. return ret;
  29618. /* Check that KPAK can converted to Montgomery form again. */
  29619. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  29620. &verified);
  29621. if (ret != 0)
  29622. return -10167;
  29623. if (!verified)
  29624. return -10168;
  29625. /* Check that the KPAK is converted from montgomery form. */
  29626. ret = wc_ValidateEccsiPair(pub, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt,
  29627. &valid);
  29628. if (ret != 0)
  29629. return -10169;
  29630. if (!valid)
  29631. return -10170;
  29632. /* Check that KPAK can converted to Montgomery form again. */
  29633. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  29634. &verified);
  29635. if (ret != 0)
  29636. return -10171;
  29637. if (!verified)
  29638. return -10172;
  29639. /* Check that the KPAK is converted from montgomery form. */
  29640. ret = eccsi_imp_exp_pubkey_test(priv, pub);
  29641. if (ret != 0)
  29642. return ret;
  29643. return 0;
  29644. }
  29645. int eccsi_test(void)
  29646. {
  29647. int ret = 0;
  29648. WC_RNG rng;
  29649. int rng_inited = 0;
  29650. EccsiKey* priv = NULL;
  29651. EccsiKey* pub = NULL;
  29652. mp_int* ssk = NULL;
  29653. ecc_point* pvt = NULL;
  29654. priv = (EccsiKey*)XMALLOC(sizeof(EccsiKey), HEAP_HINT,
  29655. DYNAMIC_TYPE_TMP_BUFFER);
  29656. if (priv == NULL)
  29657. ret = -10205;
  29658. else
  29659. XMEMSET(priv, 0, sizeof(*priv));
  29660. if (ret == 0) {
  29661. pub = (EccsiKey*)XMALLOC(sizeof(EccsiKey), HEAP_HINT,
  29662. DYNAMIC_TYPE_TMP_BUFFER);
  29663. if (pub == NULL)
  29664. ret = -10206;
  29665. else
  29666. XMEMSET(pub, 0, sizeof(*pub));
  29667. }
  29668. if (ret == 0) {
  29669. ssk = (mp_int*)XMALLOC(sizeof(mp_int), HEAP_HINT,
  29670. DYNAMIC_TYPE_TMP_BUFFER);
  29671. if (ssk == NULL)
  29672. ret = -10207;
  29673. else
  29674. XMEMSET(ssk, 0, sizeof(*ssk));
  29675. }
  29676. if (ret == 0) {
  29677. #ifndef HAVE_FIPS
  29678. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  29679. #else
  29680. ret = wc_InitRng(&rng);
  29681. #endif
  29682. if (ret != 0)
  29683. ret = -10200;
  29684. else
  29685. rng_inited = 1;
  29686. }
  29687. if (ret == 0) {
  29688. pvt = wc_ecc_new_point();
  29689. if (pvt == NULL)
  29690. ret = -10201;
  29691. }
  29692. if (ret == 0) {
  29693. ret = mp_init(ssk);
  29694. if (ret != 0)
  29695. ret = -10202;
  29696. }
  29697. if (ret == 0) {
  29698. ret = eccsi_api_test(&rng, priv, ssk, pvt);
  29699. }
  29700. if (ret == 0) {
  29701. ret = wc_InitEccsiKey(pub, HEAP_HINT, INVALID_DEVID);
  29702. if (ret != 0)
  29703. ret = -10203;
  29704. }
  29705. if (ret == 0) {
  29706. ret = wc_InitEccsiKey(priv, HEAP_HINT, INVALID_DEVID);
  29707. if (ret != 0)
  29708. ret = -10204;
  29709. }
  29710. if (ret == 0) {
  29711. ret = eccsi_kat_verify_test(pub, pvt);
  29712. }
  29713. if (ret == 0) {
  29714. ret = eccsi_make_key_test(priv, pub, &rng, ssk, pvt);
  29715. }
  29716. if (ret == 0) {
  29717. ret = eccsi_sign_verify_test(priv, pub, &rng, ssk, pvt);
  29718. }
  29719. if (pvt != NULL)
  29720. wc_ecc_del_point(pvt);
  29721. if (rng_inited)
  29722. wc_FreeRng(&rng);
  29723. if (ssk != NULL) {
  29724. mp_free(ssk);
  29725. XFREE(ssk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29726. }
  29727. if (pub != NULL) {
  29728. wc_FreeEccsiKey(pub);
  29729. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29730. }
  29731. if (priv != NULL) {
  29732. wc_FreeEccsiKey(priv);
  29733. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29734. }
  29735. return ret;
  29736. }
  29737. #endif /* WOLFCRYPT_HAVE_ECCSI */
  29738. #ifdef WOLFCRYPT_HAVE_SAKKE
  29739. static int sakke_api_test(WC_RNG* rng, SakkeKey* key, ecc_point* rsk)
  29740. {
  29741. int ret;
  29742. byte id[1] = { 0x00 };
  29743. int valid;
  29744. byte data[256];
  29745. word32 sz;
  29746. byte auth[257];
  29747. word16 authSz;
  29748. byte ssv[256];
  29749. word16 ssvSz;
  29750. word32 len;
  29751. ret = wc_InitSakkeKey_ex(NULL, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  29752. if (ret != BAD_FUNC_ARG)
  29753. return -10205;
  29754. ret = wc_InitSakkeKey_ex(NULL, 128, ECC_SAKKE_1, HEAP_HINT, INVALID_DEVID);
  29755. if (ret != BAD_FUNC_ARG)
  29756. return -10206;
  29757. wc_FreeSakkeKey(NULL);
  29758. XMEMSET(key, 0, sizeof(*key));
  29759. wc_FreeSakkeKey(key);
  29760. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, HEAP_HINT, INVALID_DEVID);
  29761. if (ret != 0)
  29762. return -10207;
  29763. ret = wc_MakeSakkeKey(NULL, NULL);
  29764. if (ret != BAD_FUNC_ARG)
  29765. return -10208;
  29766. ret = wc_MakeSakkeKey(key, NULL);
  29767. if (ret != BAD_FUNC_ARG)
  29768. return -10209;
  29769. ret = wc_MakeSakkeKey(NULL, rng);
  29770. if (ret != BAD_FUNC_ARG)
  29771. return -10210;
  29772. ret = wc_MakeSakkePublicKey(NULL, NULL);
  29773. if (ret != BAD_FUNC_ARG)
  29774. return -10211;
  29775. ret = wc_MakeSakkePublicKey(key, NULL);
  29776. if (ret != BAD_FUNC_ARG)
  29777. return -10212;
  29778. ret = wc_MakeSakkePublicKey(NULL, rsk);
  29779. if (ret != BAD_FUNC_ARG)
  29780. return -10213;
  29781. ret = wc_MakeSakkeRsk(NULL, NULL, 1, NULL);
  29782. if (ret != BAD_FUNC_ARG)
  29783. return -10214;
  29784. ret = wc_MakeSakkeRsk(key, id, 1, NULL);
  29785. if (ret != BAD_FUNC_ARG)
  29786. return -10215;
  29787. ret = wc_MakeSakkeRsk(key, NULL, 1, rsk);
  29788. if (ret != BAD_FUNC_ARG)
  29789. return -10216;
  29790. ret = wc_MakeSakkeRsk(NULL, id, 1, rsk);
  29791. if (ret != BAD_FUNC_ARG)
  29792. return -10217;
  29793. ret = wc_ValidateSakkeRsk(NULL, NULL, 1, NULL, NULL);
  29794. if (ret != BAD_FUNC_ARG)
  29795. return -10218;
  29796. ret = wc_ValidateSakkeRsk(key, id, 1, rsk, NULL);
  29797. if (ret != BAD_FUNC_ARG)
  29798. return -10219;
  29799. ret = wc_ValidateSakkeRsk(NULL, id, 1, rsk, &valid);
  29800. if (ret != BAD_FUNC_ARG)
  29801. return -10220;
  29802. ret = wc_ExportSakkeKey(NULL, NULL, NULL);
  29803. if (ret != BAD_FUNC_ARG)
  29804. return -10221;
  29805. ret = wc_ExportSakkeKey(key, data, NULL);
  29806. if (ret != BAD_FUNC_ARG)
  29807. return -10222;
  29808. ret = wc_ExportSakkeKey(NULL, data, &sz);
  29809. if (ret != BAD_FUNC_ARG)
  29810. return -10223;
  29811. ret = wc_ImportSakkeKey(NULL, NULL, 1);
  29812. if (ret != BAD_FUNC_ARG)
  29813. return -10224;
  29814. ret = wc_ImportSakkeKey(key, NULL, 1);
  29815. if (ret != BAD_FUNC_ARG)
  29816. return -10225;
  29817. ret = wc_ImportSakkeKey(NULL, data, 1);
  29818. if (ret != BAD_FUNC_ARG)
  29819. return -10226;
  29820. ret = wc_ExportSakkePrivateKey(NULL, NULL, NULL);
  29821. if (ret != BAD_FUNC_ARG)
  29822. return -10227;
  29823. ret = wc_ExportSakkePrivateKey(key, data, NULL);
  29824. if (ret != BAD_FUNC_ARG)
  29825. return -10228;
  29826. ret = wc_ExportSakkePrivateKey(NULL, data, &sz);
  29827. if (ret != BAD_FUNC_ARG)
  29828. return -10229;
  29829. ret = wc_ImportSakkePrivateKey(NULL, NULL, 1);
  29830. if (ret != BAD_FUNC_ARG)
  29831. return -10230;
  29832. ret = wc_ImportSakkePrivateKey(key, NULL, 1);
  29833. if (ret != BAD_FUNC_ARG)
  29834. return -10231;
  29835. ret = wc_ImportSakkePrivateKey(NULL, data, 1);
  29836. if (ret != BAD_FUNC_ARG)
  29837. return -10232;
  29838. sz = sizeof(data);
  29839. ret = wc_EncodeSakkeRsk(NULL, NULL, data, NULL, 1);
  29840. if (ret != BAD_FUNC_ARG)
  29841. return -10233;
  29842. ret = wc_EncodeSakkeRsk(key, rsk, data, NULL, 1);
  29843. if (ret != BAD_FUNC_ARG)
  29844. return -10234;
  29845. ret = wc_EncodeSakkeRsk(key, NULL, data, &sz, 1);
  29846. if (ret != BAD_FUNC_ARG)
  29847. return -10235;
  29848. ret = wc_EncodeSakkeRsk(NULL, rsk, data, &sz, 1);
  29849. if (ret != BAD_FUNC_ARG)
  29850. return -10236;
  29851. ret = wc_DecodeSakkeRsk(NULL, NULL, sz, NULL);
  29852. if (ret != BAD_FUNC_ARG)
  29853. return -10237;
  29854. ret = wc_DecodeSakkeRsk(key, data, sz, NULL);
  29855. if (ret != BAD_FUNC_ARG)
  29856. return -10238;
  29857. ret = wc_DecodeSakkeRsk(key, NULL, sz, rsk);
  29858. if (ret != BAD_FUNC_ARG)
  29859. return -10239;
  29860. ret = wc_DecodeSakkeRsk(NULL, data, sz, rsk);
  29861. if (ret != BAD_FUNC_ARG)
  29862. return -10240;
  29863. ret = wc_ImportSakkeRsk(NULL, NULL, sz);
  29864. if (ret != BAD_FUNC_ARG)
  29865. return -10237;
  29866. ret = wc_ImportSakkeRsk(key, NULL, sz);
  29867. if (ret != BAD_FUNC_ARG)
  29868. return -10237;
  29869. ret = wc_ImportSakkeRsk(NULL, data, sz);
  29870. if (ret != BAD_FUNC_ARG)
  29871. return -10237;
  29872. ret = wc_ImportSakkeRsk(key, data, 1);
  29873. if (ret != BUFFER_E)
  29874. return -10237;
  29875. ret = wc_GenerateSakkeRskTable(NULL, NULL, data, NULL);
  29876. if (ret != BAD_FUNC_ARG)
  29877. return -10241;
  29878. ret = wc_GenerateSakkeRskTable(key, NULL, data, NULL);
  29879. if (ret != BAD_FUNC_ARG)
  29880. return -10242;
  29881. ret = wc_GenerateSakkeRskTable(NULL, rsk, data, NULL);
  29882. if (ret != BAD_FUNC_ARG)
  29883. return -10243;
  29884. ret = wc_GenerateSakkeRskTable(NULL, NULL, data, &len);
  29885. if (ret != BAD_FUNC_ARG)
  29886. return -10244;
  29887. ret = wc_GenerateSakkeRskTable(key, rsk, data, NULL);
  29888. if (ret != BAD_FUNC_ARG)
  29889. return -10245;
  29890. ret = wc_GenerateSakkeRskTable(key, NULL, data, &len);
  29891. if (ret != BAD_FUNC_ARG)
  29892. return -10246;
  29893. ret = wc_GenerateSakkeRskTable(NULL, rsk, data, &len);
  29894. if (ret != BAD_FUNC_ARG)
  29895. return -10247;
  29896. ret = wc_GenerateSakkeRskTable(key, rsk, NULL, &len);
  29897. if (ret != LENGTH_ONLY_E)
  29898. return -10248;
  29899. len--;
  29900. ret = wc_GenerateSakkeRskTable(key, rsk, data, &len);
  29901. if (ret != BUFFER_E)
  29902. return -10249;
  29903. ret = wc_ExportSakkePublicKey(NULL, data, NULL, 1);
  29904. if (ret != BAD_FUNC_ARG)
  29905. return -10250;
  29906. ret = wc_ExportSakkePublicKey(key, data, NULL, 1);
  29907. if (ret != BAD_FUNC_ARG)
  29908. return -10251;
  29909. ret = wc_ExportSakkePublicKey(NULL, data, &sz, 1);
  29910. if (ret != BAD_FUNC_ARG)
  29911. return -10252;
  29912. ret = wc_ImportSakkePublicKey(NULL, NULL, sz, 1);
  29913. if (ret != BAD_FUNC_ARG)
  29914. return -10253;
  29915. ret = wc_ImportSakkePublicKey(key, NULL, sz, 1);
  29916. if (ret != BAD_FUNC_ARG)
  29917. return -10254;
  29918. ret = wc_ImportSakkePublicKey(NULL, data, sz, 1);
  29919. if (ret != BAD_FUNC_ARG)
  29920. return -10255;
  29921. ret = wc_GetSakkeAuthSize(NULL, NULL);
  29922. if (ret != BAD_FUNC_ARG)
  29923. return -10256;
  29924. ret = wc_GetSakkeAuthSize(key, NULL);
  29925. if (ret != BAD_FUNC_ARG)
  29926. return -10257;
  29927. ret = wc_GetSakkeAuthSize(NULL, &authSz);
  29928. if (ret != BAD_FUNC_ARG)
  29929. return -10258;
  29930. ret = wc_MakeSakkePointI(NULL, NULL, SAKKE_ID_MAX_SIZE + 1);
  29931. if (ret != BAD_FUNC_ARG)
  29932. return -10259;
  29933. ret = wc_MakeSakkePointI(key, NULL, SAKKE_ID_MAX_SIZE + 1);
  29934. if (ret != BAD_FUNC_ARG)
  29935. return -10260;
  29936. ret = wc_MakeSakkePointI(NULL, id, 1);
  29937. if (ret != BAD_FUNC_ARG)
  29938. return -10261;
  29939. ret = wc_MakeSakkePointI(NULL, NULL, 1);
  29940. if (ret != BAD_FUNC_ARG)
  29941. return -10262;
  29942. ret = wc_MakeSakkePointI(key, id, SAKKE_ID_MAX_SIZE + 1);
  29943. if (ret != BAD_FUNC_ARG)
  29944. return -10263;
  29945. ret = wc_MakeSakkePointI(key, NULL, 1);
  29946. if (ret != BAD_FUNC_ARG)
  29947. return -10264;
  29948. ret = wc_MakeSakkePointI(NULL, id, 1);
  29949. if (ret != BAD_FUNC_ARG)
  29950. return -10265;
  29951. ret = wc_GenerateSakkePointITable(NULL, data, NULL);
  29952. if (ret != BAD_FUNC_ARG)
  29953. return -10266;
  29954. ret = wc_GenerateSakkePointITable(key, data, NULL);
  29955. if (ret != BAD_FUNC_ARG)
  29956. return -10267;
  29957. ret = wc_GenerateSakkePointITable(NULL, data, &len);
  29958. if (ret != BAD_FUNC_ARG)
  29959. return -10268;
  29960. ret = wc_GenerateSakkePointITable(key, NULL, &len);
  29961. if (ret != LENGTH_ONLY_E)
  29962. return -10269;
  29963. len--;
  29964. ret = wc_GenerateSakkePointITable(key, data, &len);
  29965. if (ret != BUFFER_E)
  29966. return -10270;
  29967. ret = wc_SetSakkePointITable(NULL, NULL, 1);
  29968. if (ret != BAD_FUNC_ARG)
  29969. return -10271;
  29970. ret = wc_SetSakkePointITable(key, NULL, 1);
  29971. if (ret != BAD_FUNC_ARG)
  29972. return -10272;
  29973. ret = wc_SetSakkePointITable(NULL, data, 1);
  29974. if (ret != BAD_FUNC_ARG)
  29975. return -10273;
  29976. ret = wc_SetSakkePointITable(key, data, 1);
  29977. if (ret != BUFFER_E)
  29978. return -10274;
  29979. ret = wc_ClearSakkePointITable(NULL);
  29980. if (ret != BAD_FUNC_ARG)
  29981. return -10275;
  29982. ret = wc_GetSakkePointI(NULL, data, NULL);
  29983. if (ret != BAD_FUNC_ARG)
  29984. return -10276;
  29985. ret = wc_GetSakkePointI(key, data, NULL);
  29986. if (ret != BAD_FUNC_ARG)
  29987. return -10277;
  29988. ret = wc_GetSakkePointI(NULL, data, &sz);
  29989. if (ret != BAD_FUNC_ARG)
  29990. return -10278;
  29991. sz = 1;
  29992. ret = wc_GetSakkePointI(key, data, &sz);
  29993. if (ret != BUFFER_E)
  29994. return -10279;
  29995. sz = 256;
  29996. ret = wc_SetSakkePointI(NULL, NULL, 1, NULL, sz);
  29997. if (ret != BAD_FUNC_ARG)
  29998. return -10280;
  29999. ret = wc_SetSakkePointI(key, NULL, 1, NULL, sz);
  30000. if (ret != BAD_FUNC_ARG)
  30001. return -10281;
  30002. ret = wc_SetSakkePointI(NULL, id, 1, NULL, sz);
  30003. if (ret != BAD_FUNC_ARG)
  30004. return -10282;
  30005. ret = wc_SetSakkePointI(NULL, NULL, 1, data, sz);
  30006. if (ret != BAD_FUNC_ARG)
  30007. return -10283;
  30008. ret = wc_SetSakkePointI(key, id, 1, NULL, sz);
  30009. if (ret != BAD_FUNC_ARG)
  30010. return -10284;
  30011. ret = wc_SetSakkePointI(key, NULL, 1, data, sz);
  30012. if (ret != BAD_FUNC_ARG)
  30013. return -10285;
  30014. ret = wc_SetSakkePointI(NULL, id, 1, data, sz);
  30015. if (ret != BAD_FUNC_ARG)
  30016. return -10286;
  30017. ret = wc_SetSakkePointI(key, id, SAKKE_ID_MAX_SIZE + 1, data, sz);
  30018. if (ret != BUFFER_E)
  30019. return -10287;
  30020. ret = wc_SetSakkePointI(key, id, 1, data, sz - 1);
  30021. if (ret != BUFFER_E)
  30022. return -10288;
  30023. ret = wc_SetSakkeIdentity(NULL, NULL, 1);
  30024. if (ret != BAD_FUNC_ARG)
  30025. return -10286;
  30026. ret = wc_SetSakkeIdentity(key, NULL, 1);
  30027. if (ret != BAD_FUNC_ARG)
  30028. return -10286;
  30029. ret = wc_SetSakkeIdentity(NULL, id, 1);
  30030. if (ret != BAD_FUNC_ARG)
  30031. return -10286;
  30032. ssvSz = sizeof(ssv);
  30033. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  30034. auth, NULL);
  30035. if (ret != BAD_FUNC_ARG)
  30036. return -10289;
  30037. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  30038. auth, NULL);
  30039. if (ret != BAD_FUNC_ARG)
  30040. return -10290;
  30041. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  30042. auth, NULL);
  30043. if (ret != BAD_FUNC_ARG)
  30044. return -10291;
  30045. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  30046. auth, &authSz);
  30047. if (ret != BAD_FUNC_ARG)
  30048. return -10292;
  30049. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  30050. auth, NULL);
  30051. if (ret != BAD_FUNC_ARG)
  30052. return -10293;
  30053. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  30054. auth, &authSz);
  30055. if (ret != BAD_FUNC_ARG)
  30056. return -10294;
  30057. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  30058. auth, &authSz);
  30059. if (ret != BAD_FUNC_ARG)
  30060. return -10295;
  30061. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  30062. auth, &authSz);
  30063. if (ret != BAD_STATE_E)
  30064. return -10295;
  30065. ret = wc_GenerateSakkeSSV(NULL, NULL, data, NULL);
  30066. if (ret != BAD_FUNC_ARG)
  30067. return -10296;
  30068. ret = wc_GenerateSakkeSSV(key, rng, data, NULL);
  30069. if (ret != BAD_FUNC_ARG)
  30070. return -10297;
  30071. ret = wc_GenerateSakkeSSV(key, NULL, data, &ssvSz);
  30072. if (ret != BAD_FUNC_ARG)
  30073. return -10298;
  30074. ret = wc_GenerateSakkeSSV(NULL, rng, data, &ssvSz);
  30075. if (ret != BAD_FUNC_ARG)
  30076. return -10299;
  30077. ret = wc_SetSakkeRsk(NULL, NULL, data, 1);
  30078. if (ret != BAD_FUNC_ARG)
  30079. return -10286;
  30080. ret = wc_SetSakkeRsk(key, NULL, data, 1);
  30081. if (ret != BAD_FUNC_ARG)
  30082. return -10286;
  30083. ret = wc_SetSakkeRsk(NULL, rsk, data, 1);
  30084. if (ret != BAD_FUNC_ARG)
  30085. return -10286;
  30086. ssvSz = sizeof(ssv);
  30087. authSz = sizeof(auth);
  30088. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz, NULL,
  30089. authSz);
  30090. if (ret != BAD_FUNC_ARG)
  30091. return -10300;
  30092. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz, NULL,
  30093. authSz);
  30094. if (ret != BAD_FUNC_ARG)
  30095. return -10300;
  30096. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz, NULL,
  30097. authSz);
  30098. if (ret != BAD_FUNC_ARG)
  30099. return -10300;
  30100. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz, auth,
  30101. authSz);
  30102. if (ret != BAD_FUNC_ARG)
  30103. return -10300;
  30104. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, NULL,
  30105. authSz);
  30106. if (ret != BAD_FUNC_ARG)
  30107. return -10300;
  30108. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz, auth,
  30109. authSz);
  30110. if (ret != BAD_FUNC_ARG)
  30111. return -10300;
  30112. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  30113. authSz);
  30114. if (ret != BAD_FUNC_ARG)
  30115. return -10300;
  30116. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  30117. authSz);
  30118. if (ret != BAD_STATE_E)
  30119. return -10300;
  30120. ret = wc_SetSakkeIdentity(key, id, 1);
  30121. if (ret != 0)
  30122. return -10286;
  30123. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  30124. authSz);
  30125. if (ret != BAD_STATE_E)
  30126. return -10300;
  30127. ret = wc_SetSakkeIdentity(key, id, 0);
  30128. if (ret != 0)
  30129. return -10286;
  30130. ret = wc_SetSakkeRsk(key, rsk, data, 1);
  30131. if (ret != 0)
  30132. return -10286;
  30133. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  30134. authSz);
  30135. if (ret != BAD_STATE_E)
  30136. return -10300;
  30137. wc_FreeSakkeKey(key);
  30138. return 0;
  30139. }
  30140. static int sakke_kat_derive_test(SakkeKey* key, ecc_point* rsk)
  30141. {
  30142. WOLFSSL_SMALL_STACK_STATIC const byte pubData[] = {
  30143. 0x59, 0x58, 0xEF, 0x1B, 0x16, 0x79, 0xBF, 0x09,
  30144. 0x9B, 0x3A, 0x03, 0x0D, 0xF2, 0x55, 0xAA, 0x6A,
  30145. 0x23, 0xC1, 0xD8, 0xF1, 0x43, 0xD4, 0xD2, 0x3F,
  30146. 0x75, 0x3E, 0x69, 0xBD, 0x27, 0xA8, 0x32, 0xF3,
  30147. 0x8C, 0xB4, 0xAD, 0x53, 0xDD, 0xEF, 0x42, 0x60,
  30148. 0xB0, 0xFE, 0x8B, 0xB4, 0x5C, 0x4C, 0x1F, 0xF5,
  30149. 0x10, 0xEF, 0xFE, 0x30, 0x03, 0x67, 0xA3, 0x7B,
  30150. 0x61, 0xF7, 0x01, 0xD9, 0x14, 0xAE, 0xF0, 0x97,
  30151. 0x24, 0x82, 0x5F, 0xA0, 0x70, 0x7D, 0x61, 0xA6,
  30152. 0xDF, 0xF4, 0xFB, 0xD7, 0x27, 0x35, 0x66, 0xCD,
  30153. 0xDE, 0x35, 0x2A, 0x0B, 0x04, 0xB7, 0xC1, 0x6A,
  30154. 0x78, 0x30, 0x9B, 0xE6, 0x40, 0x69, 0x7D, 0xE7,
  30155. 0x47, 0x61, 0x3A, 0x5F, 0xC1, 0x95, 0xE8, 0xB9,
  30156. 0xF3, 0x28, 0x85, 0x2A, 0x57, 0x9D, 0xB8, 0xF9,
  30157. 0x9B, 0x1D, 0x00, 0x34, 0x47, 0x9E, 0xA9, 0xC5,
  30158. 0x59, 0x5F, 0x47, 0xC4, 0xB2, 0xF5, 0x4F, 0xF2,
  30159. 0x15, 0x08, 0xD3, 0x75, 0x14, 0xDC, 0xF7, 0xA8,
  30160. 0xE1, 0x43, 0xA6, 0x05, 0x8C, 0x09, 0xA6, 0xBF,
  30161. 0x2C, 0x98, 0x58, 0xCA, 0x37, 0xC2, 0x58, 0x06,
  30162. 0x5A, 0xE6, 0xBF, 0x75, 0x32, 0xBC, 0x8B, 0x5B,
  30163. 0x63, 0x38, 0x38, 0x66, 0xE0, 0x75, 0x3C, 0x5A,
  30164. 0xC0, 0xE7, 0x27, 0x09, 0xF8, 0x44, 0x5F, 0x2E,
  30165. 0x61, 0x78, 0xE0, 0x65, 0x85, 0x7E, 0x0E, 0xDA,
  30166. 0x10, 0xF6, 0x82, 0x06, 0xB6, 0x35, 0x05, 0xED,
  30167. 0x87, 0xE5, 0x34, 0xFB, 0x28, 0x31, 0xFF, 0x95,
  30168. 0x7F, 0xB7, 0xDC, 0x61, 0x9D, 0xAE, 0x61, 0x30,
  30169. 0x1E, 0xEA, 0xCC, 0x2F, 0xDA, 0x36, 0x80, 0xEA,
  30170. 0x49, 0x99, 0x25, 0x8A, 0x83, 0x3C, 0xEA, 0x8F,
  30171. 0xC6, 0x7C, 0x6D, 0x19, 0x48, 0x7F, 0xB4, 0x49,
  30172. 0x05, 0x9F, 0x26, 0xCC, 0x8A, 0xAB, 0x65, 0x5A,
  30173. 0xB5, 0x8B, 0x7C, 0xC7, 0x96, 0xE2, 0x4E, 0x9A,
  30174. 0x39, 0x40, 0x95, 0x75, 0x4F, 0x5F, 0x8B, 0xAE
  30175. };
  30176. WOLFSSL_SMALL_STACK_STATIC const byte rskData[] = {
  30177. 0x93, 0xAF, 0x67, 0xE5, 0x00, 0x7B, 0xA6, 0xE6,
  30178. 0xA8, 0x0D, 0xA7, 0x93, 0xDA, 0x30, 0x0F, 0xA4,
  30179. 0xB5, 0x2D, 0x0A, 0x74, 0xE2, 0x5E, 0x6E, 0x7B,
  30180. 0x2B, 0x3D, 0x6E, 0xE9, 0xD1, 0x8A, 0x9B, 0x5C,
  30181. 0x50, 0x23, 0x59, 0x7B, 0xD8, 0x2D, 0x80, 0x62,
  30182. 0xD3, 0x40, 0x19, 0x56, 0x3B, 0xA1, 0xD2, 0x5C,
  30183. 0x0D, 0xC5, 0x6B, 0x7B, 0x97, 0x9D, 0x74, 0xAA,
  30184. 0x50, 0xF2, 0x9F, 0xBF, 0x11, 0xCC, 0x2C, 0x93,
  30185. 0xF5, 0xDF, 0xCA, 0x61, 0x5E, 0x60, 0x92, 0x79,
  30186. 0xF6, 0x17, 0x5C, 0xEA, 0xDB, 0x00, 0xB5, 0x8C,
  30187. 0x6B, 0xEE, 0x1E, 0x7A, 0x2A, 0x47, 0xC4, 0xF0,
  30188. 0xC4, 0x56, 0xF0, 0x52, 0x59, 0xA6, 0xFA, 0x94,
  30189. 0xA6, 0x34, 0xA4, 0x0D, 0xAE, 0x1D, 0xF5, 0x93,
  30190. 0xD4, 0xFE, 0xCF, 0x68, 0x8D, 0x5F, 0xC6, 0x78,
  30191. 0xBE, 0x7E, 0xFC, 0x6D, 0xF3, 0xD6, 0x83, 0x53,
  30192. 0x25, 0xB8, 0x3B, 0x2C, 0x6E, 0x69, 0x03, 0x6B,
  30193. 0x15, 0x5F, 0x0A, 0x27, 0x24, 0x10, 0x94, 0xB0,
  30194. 0x4B, 0xFB, 0x0B, 0xDF, 0xAC, 0x6C, 0x67, 0x0A,
  30195. 0x65, 0xC3, 0x25, 0xD3, 0x9A, 0x06, 0x9F, 0x03,
  30196. 0x65, 0x9D, 0x44, 0xCA, 0x27, 0xD3, 0xBE, 0x8D,
  30197. 0xF3, 0x11, 0x17, 0x2B, 0x55, 0x41, 0x60, 0x18,
  30198. 0x1C, 0xBE, 0x94, 0xA2, 0xA7, 0x83, 0x32, 0x0C,
  30199. 0xED, 0x59, 0x0B, 0xC4, 0x26, 0x44, 0x70, 0x2C,
  30200. 0xF3, 0x71, 0x27, 0x1E, 0x49, 0x6B, 0xF2, 0x0F,
  30201. 0x58, 0x8B, 0x78, 0xA1, 0xBC, 0x01, 0xEC, 0xBB,
  30202. 0x65, 0x59, 0x93, 0x4B, 0xDD, 0x2F, 0xB6, 0x5D,
  30203. 0x28, 0x84, 0x31, 0x8A, 0x33, 0xD1, 0xA4, 0x2A,
  30204. 0xDF, 0x5E, 0x33, 0xCC, 0x58, 0x00, 0x28, 0x0B,
  30205. 0x28, 0x35, 0x64, 0x97, 0xF8, 0x71, 0x35, 0xBA,
  30206. 0xB9, 0x61, 0x2A, 0x17, 0x26, 0x04, 0x24, 0x40,
  30207. 0x9A, 0xC1, 0x5F, 0xEE, 0x99, 0x6B, 0x74, 0x4C,
  30208. 0x33, 0x21, 0x51, 0x23, 0x5D, 0xEC, 0xB0, 0xF5
  30209. };
  30210. WOLFSSL_SMALL_STACK_STATIC const byte id[] = {
  30211. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  30212. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  30213. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  30214. 0x33, 0x00
  30215. };
  30216. WOLFSSL_SMALL_STACK_STATIC const byte ssv[] = {
  30217. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
  30218. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0
  30219. };
  30220. WOLFSSL_SMALL_STACK_STATIC const byte auth[] = {
  30221. 0x04,
  30222. 0x44, 0xE8, 0xAD, 0x44, 0xAB, 0x85, 0x92, 0xA6,
  30223. 0xA5, 0xA3, 0xDD, 0xCA, 0x5C, 0xF8, 0x96, 0xC7,
  30224. 0x18, 0x04, 0x36, 0x06, 0xA0, 0x1D, 0x65, 0x0D,
  30225. 0xEF, 0x37, 0xA0, 0x1F, 0x37, 0xC2, 0x28, 0xC3,
  30226. 0x32, 0xFC, 0x31, 0x73, 0x54, 0xE2, 0xC2, 0x74,
  30227. 0xD4, 0xDA, 0xF8, 0xAD, 0x00, 0x10, 0x54, 0xC7,
  30228. 0x6C, 0xE5, 0x79, 0x71, 0xC6, 0xF4, 0x48, 0x6D,
  30229. 0x57, 0x23, 0x04, 0x32, 0x61, 0xC5, 0x06, 0xEB,
  30230. 0xF5, 0xBE, 0x43, 0x8F, 0x53, 0xDE, 0x04, 0xF0,
  30231. 0x67, 0xC7, 0x76, 0xE0, 0xDD, 0x3B, 0x71, 0xA6,
  30232. 0x29, 0x01, 0x33, 0x28, 0x37, 0x25, 0xA5, 0x32,
  30233. 0xF2, 0x1A, 0xF1, 0x45, 0x12, 0x6D, 0xC1, 0xD7,
  30234. 0x77, 0xEC, 0xC2, 0x7B, 0xE5, 0x08, 0x35, 0xBD,
  30235. 0x28, 0x09, 0x8B, 0x8A, 0x73, 0xD9, 0xF8, 0x01,
  30236. 0xD8, 0x93, 0x79, 0x3A, 0x41, 0xFF, 0x5C, 0x49,
  30237. 0xB8, 0x7E, 0x79, 0xF2, 0xBE, 0x4D, 0x56, 0xCE,
  30238. 0x55, 0x7E, 0x13, 0x4A, 0xD8, 0x5B, 0xB1, 0xD4,
  30239. 0xB9, 0xCE, 0x4F, 0x8B, 0xE4, 0xB0, 0x8A, 0x12,
  30240. 0xBA, 0xBF, 0x55, 0xB1, 0xD6, 0xF1, 0xD7, 0xA6,
  30241. 0x38, 0x01, 0x9E, 0xA2, 0x8E, 0x15, 0xAB, 0x1C,
  30242. 0x9F, 0x76, 0x37, 0x5F, 0xDD, 0x12, 0x10, 0xD4,
  30243. 0xF4, 0x35, 0x1B, 0x9A, 0x00, 0x94, 0x86, 0xB7,
  30244. 0xF3, 0xED, 0x46, 0xC9, 0x65, 0xDE, 0xD2, 0xD8,
  30245. 0x0D, 0xAD, 0xE4, 0xF3, 0x8C, 0x67, 0x21, 0xD5,
  30246. 0x2C, 0x3A, 0xD1, 0x03, 0xA1, 0x0E, 0xBD, 0x29,
  30247. 0x59, 0x24, 0x8B, 0x4E, 0xF0, 0x06, 0x83, 0x6B,
  30248. 0xF0, 0x97, 0x44, 0x8E, 0x61, 0x07, 0xC9, 0xED,
  30249. 0xEE, 0x9F, 0xB7, 0x04, 0x82, 0x3D, 0xF1, 0x99,
  30250. 0xF8, 0x32, 0xC9, 0x05, 0xAE, 0x45, 0xF8, 0xA2,
  30251. 0x47, 0xA0, 0x72, 0xD8, 0xEF, 0x72, 0x9E, 0xAB,
  30252. 0xC5, 0xE2, 0x75, 0x74, 0xB0, 0x77, 0x39, 0xB3,
  30253. 0x4B, 0xE7, 0x4A, 0x53, 0x2F, 0x74, 0x7B, 0x86
  30254. };
  30255. WOLFSSL_SMALL_STACK_STATIC const byte encSsv[] = {
  30256. 0x89, 0xE0, 0xBC, 0x66, 0x1A, 0xA1, 0xE9, 0x16,
  30257. 0x38, 0xE6, 0xAC, 0xC8, 0x4E, 0x49, 0x65, 0x07
  30258. };
  30259. int ret;
  30260. int valid;
  30261. byte pubKey[sizeof(pubData) + 1];
  30262. word32 sz = sizeof(pubKey);
  30263. byte tmpSsv[sizeof(encSsv)];
  30264. byte* iTable = NULL;
  30265. word32 iTableLen;
  30266. byte* table = NULL;
  30267. word32 len;
  30268. ret = wc_ImportSakkePublicKey(key, pubData, sizeof(pubData), 0);
  30269. if (ret != 0)
  30270. return -10315;
  30271. ret = wc_DecodeSakkeRsk(key, rskData, sizeof(rskData), rsk);
  30272. if (ret != 0)
  30273. return -10316;
  30274. ret = wc_ValidateSakkeRsk(key, id, sizeof(id), rsk, &valid);
  30275. if (ret != 0)
  30276. return -10317;
  30277. if (valid != 1)
  30278. return -10318;
  30279. ret = wc_SetSakkeRsk(key, rsk, NULL, 0);
  30280. if (ret != 0)
  30281. return -10319;
  30282. ret = wc_SetSakkeIdentity(key, id, sizeof(id));
  30283. if (ret != 0)
  30284. return -10319;
  30285. XMEMCPY(tmpSsv, encSsv, sizeof(encSsv));
  30286. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, tmpSsv, sizeof(tmpSsv),
  30287. auth, sizeof(auth));
  30288. if (ret != 0)
  30289. return -10322;
  30290. if (XMEMCMP(tmpSsv, ssv, sizeof(ssv)) != 0)
  30291. return -10320;
  30292. ret = wc_MakeSakkePointI(key, id, sizeof(id));
  30293. if (ret != 0)
  30294. return -10321;
  30295. iTableLen = 0;
  30296. ret = wc_GenerateSakkePointITable(key, NULL, &iTableLen);
  30297. if (ret != LENGTH_ONLY_E)
  30298. return -10322;
  30299. if (iTableLen != 0) {
  30300. iTable = (byte*)XMALLOC(iTableLen, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30301. if (iTable == NULL)
  30302. return -10323;
  30303. ret = wc_GenerateSakkePointITable(key, iTable, &iTableLen);
  30304. if (ret != 0)
  30305. return -10324;
  30306. }
  30307. len = 0;
  30308. ret = wc_GenerateSakkeRskTable(key, rsk, NULL, &len);
  30309. if (ret != LENGTH_ONLY_E)
  30310. return -10325;
  30311. if (len > 0) {
  30312. table = (byte*)XMALLOC(len, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30313. if (table == NULL)
  30314. return -10326;
  30315. ret = wc_GenerateSakkeRskTable(key, rsk, table, &len);
  30316. if (ret != 0)
  30317. return -10327;
  30318. }
  30319. ret = wc_SetSakkeRsk(key, rsk, table, len);
  30320. if (ret != 0)
  30321. return -10319;
  30322. XMEMCPY(tmpSsv, encSsv, sizeof(encSsv));
  30323. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, tmpSsv, sizeof(tmpSsv),
  30324. auth, sizeof(auth));
  30325. if (ret != 0)
  30326. return -10328;
  30327. if (XMEMCMP(tmpSsv, ssv, sizeof(ssv)) != 0)
  30328. return -10329;
  30329. /* Don't reference table that is about to be freed. */
  30330. ret = wc_ClearSakkePointITable(key);
  30331. if (ret != 0)
  30332. return -10330;
  30333. /* Dispose of tables */
  30334. if (iTable != NULL)
  30335. XFREE(iTable, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30336. if (table != NULL)
  30337. XFREE(table, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30338. /* Make sure the key public key is exportable - convert to Montgomery form
  30339. * in Validation.
  30340. */
  30341. ret = wc_ExportSakkePublicKey(key, pubKey, &sz, 1);
  30342. if (ret != 0)
  30343. return -10331;
  30344. if (sz != sizeof(pubData))
  30345. return -10332;
  30346. if (XMEMCMP(pubKey, pubData, sizeof(pubData)) != 0)
  30347. return -10333;
  30348. sz = sizeof(pubData) + 1;
  30349. ret = wc_ExportSakkePublicKey(key, pubKey, &sz, 0);
  30350. if (ret != 0)
  30351. return -10334;
  30352. if (sz != sizeof(pubData) + 1)
  30353. return -10335;
  30354. if (pubKey[0] != 0x04)
  30355. return -10336;
  30356. if (XMEMCMP(pubKey + 1, pubData, sizeof(pubData)) != 0)
  30357. return -10337;
  30358. return 0;
  30359. }
  30360. static int sakke_kat_encapsulate_test(SakkeKey* key)
  30361. {
  30362. static const byte pubData[] = {
  30363. 0x59, 0x58, 0xEF, 0x1B, 0x16, 0x79, 0xBF, 0x09,
  30364. 0x9B, 0x3A, 0x03, 0x0D, 0xF2, 0x55, 0xAA, 0x6A,
  30365. 0x23, 0xC1, 0xD8, 0xF1, 0x43, 0xD4, 0xD2, 0x3F,
  30366. 0x75, 0x3E, 0x69, 0xBD, 0x27, 0xA8, 0x32, 0xF3,
  30367. 0x8C, 0xB4, 0xAD, 0x53, 0xDD, 0xEF, 0x42, 0x60,
  30368. 0xB0, 0xFE, 0x8B, 0xB4, 0x5C, 0x4C, 0x1F, 0xF5,
  30369. 0x10, 0xEF, 0xFE, 0x30, 0x03, 0x67, 0xA3, 0x7B,
  30370. 0x61, 0xF7, 0x01, 0xD9, 0x14, 0xAE, 0xF0, 0x97,
  30371. 0x24, 0x82, 0x5F, 0xA0, 0x70, 0x7D, 0x61, 0xA6,
  30372. 0xDF, 0xF4, 0xFB, 0xD7, 0x27, 0x35, 0x66, 0xCD,
  30373. 0xDE, 0x35, 0x2A, 0x0B, 0x04, 0xB7, 0xC1, 0x6A,
  30374. 0x78, 0x30, 0x9B, 0xE6, 0x40, 0x69, 0x7D, 0xE7,
  30375. 0x47, 0x61, 0x3A, 0x5F, 0xC1, 0x95, 0xE8, 0xB9,
  30376. 0xF3, 0x28, 0x85, 0x2A, 0x57, 0x9D, 0xB8, 0xF9,
  30377. 0x9B, 0x1D, 0x00, 0x34, 0x47, 0x9E, 0xA9, 0xC5,
  30378. 0x59, 0x5F, 0x47, 0xC4, 0xB2, 0xF5, 0x4F, 0xF2,
  30379. 0x15, 0x08, 0xD3, 0x75, 0x14, 0xDC, 0xF7, 0xA8,
  30380. 0xE1, 0x43, 0xA6, 0x05, 0x8C, 0x09, 0xA6, 0xBF,
  30381. 0x2C, 0x98, 0x58, 0xCA, 0x37, 0xC2, 0x58, 0x06,
  30382. 0x5A, 0xE6, 0xBF, 0x75, 0x32, 0xBC, 0x8B, 0x5B,
  30383. 0x63, 0x38, 0x38, 0x66, 0xE0, 0x75, 0x3C, 0x5A,
  30384. 0xC0, 0xE7, 0x27, 0x09, 0xF8, 0x44, 0x5F, 0x2E,
  30385. 0x61, 0x78, 0xE0, 0x65, 0x85, 0x7E, 0x0E, 0xDA,
  30386. 0x10, 0xF6, 0x82, 0x06, 0xB6, 0x35, 0x05, 0xED,
  30387. 0x87, 0xE5, 0x34, 0xFB, 0x28, 0x31, 0xFF, 0x95,
  30388. 0x7F, 0xB7, 0xDC, 0x61, 0x9D, 0xAE, 0x61, 0x30,
  30389. 0x1E, 0xEA, 0xCC, 0x2F, 0xDA, 0x36, 0x80, 0xEA,
  30390. 0x49, 0x99, 0x25, 0x8A, 0x83, 0x3C, 0xEA, 0x8F,
  30391. 0xC6, 0x7C, 0x6D, 0x19, 0x48, 0x7F, 0xB4, 0x49,
  30392. 0x05, 0x9F, 0x26, 0xCC, 0x8A, 0xAB, 0x65, 0x5A,
  30393. 0xB5, 0x8B, 0x7C, 0xC7, 0x96, 0xE2, 0x4E, 0x9A,
  30394. 0x39, 0x40, 0x95, 0x75, 0x4F, 0x5F, 0x8B, 0xAE
  30395. };
  30396. static const byte id[] = {
  30397. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  30398. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  30399. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  30400. 0x33, 0x00
  30401. };
  30402. static word32 idSz = sizeof(id);
  30403. byte ssv[] = {
  30404. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
  30405. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0
  30406. };
  30407. static word16 ssvSz = sizeof(ssv);
  30408. static const byte expAuth[] = {
  30409. 0x04,
  30410. 0x44, 0xE8, 0xAD, 0x44, 0xAB, 0x85, 0x92, 0xA6,
  30411. 0xA5, 0xA3, 0xDD, 0xCA, 0x5C, 0xF8, 0x96, 0xC7,
  30412. 0x18, 0x04, 0x36, 0x06, 0xA0, 0x1D, 0x65, 0x0D,
  30413. 0xEF, 0x37, 0xA0, 0x1F, 0x37, 0xC2, 0x28, 0xC3,
  30414. 0x32, 0xFC, 0x31, 0x73, 0x54, 0xE2, 0xC2, 0x74,
  30415. 0xD4, 0xDA, 0xF8, 0xAD, 0x00, 0x10, 0x54, 0xC7,
  30416. 0x6C, 0xE5, 0x79, 0x71, 0xC6, 0xF4, 0x48, 0x6D,
  30417. 0x57, 0x23, 0x04, 0x32, 0x61, 0xC5, 0x06, 0xEB,
  30418. 0xF5, 0xBE, 0x43, 0x8F, 0x53, 0xDE, 0x04, 0xF0,
  30419. 0x67, 0xC7, 0x76, 0xE0, 0xDD, 0x3B, 0x71, 0xA6,
  30420. 0x29, 0x01, 0x33, 0x28, 0x37, 0x25, 0xA5, 0x32,
  30421. 0xF2, 0x1A, 0xF1, 0x45, 0x12, 0x6D, 0xC1, 0xD7,
  30422. 0x77, 0xEC, 0xC2, 0x7B, 0xE5, 0x08, 0x35, 0xBD,
  30423. 0x28, 0x09, 0x8B, 0x8A, 0x73, 0xD9, 0xF8, 0x01,
  30424. 0xD8, 0x93, 0x79, 0x3A, 0x41, 0xFF, 0x5C, 0x49,
  30425. 0xB8, 0x7E, 0x79, 0xF2, 0xBE, 0x4D, 0x56, 0xCE,
  30426. 0x55, 0x7E, 0x13, 0x4A, 0xD8, 0x5B, 0xB1, 0xD4,
  30427. 0xB9, 0xCE, 0x4F, 0x8B, 0xE4, 0xB0, 0x8A, 0x12,
  30428. 0xBA, 0xBF, 0x55, 0xB1, 0xD6, 0xF1, 0xD7, 0xA6,
  30429. 0x38, 0x01, 0x9E, 0xA2, 0x8E, 0x15, 0xAB, 0x1C,
  30430. 0x9F, 0x76, 0x37, 0x5F, 0xDD, 0x12, 0x10, 0xD4,
  30431. 0xF4, 0x35, 0x1B, 0x9A, 0x00, 0x94, 0x86, 0xB7,
  30432. 0xF3, 0xED, 0x46, 0xC9, 0x65, 0xDE, 0xD2, 0xD8,
  30433. 0x0D, 0xAD, 0xE4, 0xF3, 0x8C, 0x67, 0x21, 0xD5,
  30434. 0x2C, 0x3A, 0xD1, 0x03, 0xA1, 0x0E, 0xBD, 0x29,
  30435. 0x59, 0x24, 0x8B, 0x4E, 0xF0, 0x06, 0x83, 0x6B,
  30436. 0xF0, 0x97, 0x44, 0x8E, 0x61, 0x07, 0xC9, 0xED,
  30437. 0xEE, 0x9F, 0xB7, 0x04, 0x82, 0x3D, 0xF1, 0x99,
  30438. 0xF8, 0x32, 0xC9, 0x05, 0xAE, 0x45, 0xF8, 0xA2,
  30439. 0x47, 0xA0, 0x72, 0xD8, 0xEF, 0x72, 0x9E, 0xAB,
  30440. 0xC5, 0xE2, 0x75, 0x74, 0xB0, 0x77, 0x39, 0xB3,
  30441. 0x4B, 0xE7, 0x4A, 0x53, 0x2F, 0x74, 0x7B, 0x86
  30442. };
  30443. static const byte encSsv[] = {
  30444. 0x89, 0xE0, 0xBC, 0x66, 0x1A, 0xA1, 0xE9, 0x16,
  30445. 0x38, 0xE6, 0xAC, 0xC8, 0x4E, 0x49, 0x65, 0x07
  30446. };
  30447. int ret;
  30448. byte auth[257];
  30449. word16 authSz = sizeof(auth);
  30450. ret = wc_ImportSakkePublicKey(key, pubData, sizeof(pubData), 0);
  30451. if (ret != 0)
  30452. return -10334;
  30453. ret = wc_SetSakkeIdentity(key, id, idSz);
  30454. if (ret != 0)
  30455. return -10335;
  30456. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  30457. auth, &authSz);
  30458. if (ret != 0)
  30459. return -10336;
  30460. if (authSz != 257)
  30461. return -10337;
  30462. if (XMEMCMP(ssv, encSsv, ssvSz) != 0)
  30463. return -10338;
  30464. if (XMEMCMP(auth, expAuth, authSz) != 0)
  30465. return -10339;
  30466. return 0;
  30467. }
  30468. static int sakke_make_key_test(SakkeKey* priv, SakkeKey* pub, SakkeKey* key,
  30469. WC_RNG* rng, ecc_point* rsk)
  30470. {
  30471. int ret;
  30472. byte data[440];
  30473. byte pubData[257];
  30474. word32 sz;
  30475. char mail[] = "test@wolfssl.com";
  30476. byte* id = (byte*)mail;
  30477. word32 idSz = (word32)XSTRLEN(mail);
  30478. int valid;
  30479. ecc_point* pubKey = rsk;
  30480. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  30481. if (ret != 0)
  30482. return -10339;
  30483. ret = wc_MakeSakkeKey(priv, rng);
  30484. if (ret != 0)
  30485. return -10340;
  30486. ret = wc_ExportSakkeKey(priv, NULL, &sz);
  30487. if (ret != LENGTH_ONLY_E)
  30488. return -10341;
  30489. if (sz != 384)
  30490. return -10342;
  30491. sz--;
  30492. ret = wc_ExportSakkeKey(priv, data, &sz);
  30493. if (ret == 0)
  30494. return -10343;
  30495. sz++;
  30496. ret = wc_ExportSakkeKey(priv, data, &sz);
  30497. if (ret != 0)
  30498. return -10344;
  30499. if (sz != 384)
  30500. return -10345;
  30501. ret = wc_ImportSakkeKey(key, data, sz - 1);
  30502. if (ret == 0)
  30503. return -10346;
  30504. ret = wc_ImportSakkeKey(key, data, sz);
  30505. if (ret != 0)
  30506. return -10347;
  30507. wc_FreeSakkeKey(key);
  30508. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  30509. if (ret != 0)
  30510. return -10348;
  30511. ret = wc_ExportSakkePrivateKey(priv, NULL, &sz);
  30512. if (ret != LENGTH_ONLY_E)
  30513. return -10349;
  30514. if (sz != 128)
  30515. return -10350;
  30516. sz--;
  30517. ret = wc_ExportSakkePrivateKey(priv, data, &sz);
  30518. if (ret == 0)
  30519. return -10351;
  30520. sz++;
  30521. ret = wc_ExportSakkePrivateKey(priv, data, &sz);
  30522. if (ret != 0)
  30523. return -10352;
  30524. if (sz != 128)
  30525. return -10353;
  30526. ret = wc_ImportSakkePrivateKey(key, data, sz - 1);
  30527. if (ret == 0)
  30528. return -10354;
  30529. ret = wc_ImportSakkePrivateKey(key, data, sz);
  30530. if (ret != 0)
  30531. return -10355;
  30532. ret = wc_MakeSakkePublicKey(key, pubKey);
  30533. if (ret != 0)
  30534. return -10356;
  30535. ret = wc_ExportSakkePublicKey(priv, NULL, &sz, 1);
  30536. if (ret != LENGTH_ONLY_E)
  30537. return -10357;
  30538. if (sz != 256)
  30539. return -10358;
  30540. sz--;
  30541. ret = wc_ExportSakkePublicKey(priv, data, &sz, 1);
  30542. if (ret == 0)
  30543. return -10359;
  30544. sz++;
  30545. ret = wc_ExportSakkePublicKey(priv, data, &sz, 1);
  30546. if (ret != 0)
  30547. return -10360;
  30548. if (sz != 256)
  30549. return -10361;
  30550. ret = wc_ImportSakkePublicKey(pub, data, sz - 1, 1);
  30551. if (ret == 0)
  30552. return -10362;
  30553. ret = wc_ImportSakkePublicKey(pub, data, sz, 1);
  30554. if (ret != 0)
  30555. return -10363;
  30556. ret = wc_ExportSakkePublicKey(pub, pubData, &sz, 1);
  30557. if (ret != 0)
  30558. return -10364;
  30559. if (sz != 256)
  30560. return -10365;
  30561. if (XMEMCMP(data, pubData, sz) != 0)
  30562. return -10366;
  30563. ret = wc_MakeSakkeRsk(priv, id, idSz, rsk);
  30564. if (ret != 0)
  30565. return -10367;
  30566. ret = wc_ValidateSakkeRsk(priv, id, idSz, rsk, &valid);
  30567. if (ret != 0)
  30568. return -10368;
  30569. if (valid != 1)
  30570. return -10369;
  30571. ret = wc_ValidateSakkeRsk(pub, id, idSz, rsk, &valid);
  30572. if (ret != 0)
  30573. return -10370;
  30574. if (valid != 1)
  30575. return -10371;
  30576. sz = sizeof(data);
  30577. ret = wc_EncodeSakkeRsk(priv, rsk, data, &sz, 1);
  30578. if (ret != 0)
  30579. return -10372;
  30580. if (sz != 256)
  30581. return -10373;
  30582. ret = wc_DecodeSakkeRsk(priv, data, sz, rsk);
  30583. if (ret != 0)
  30584. return -10374;
  30585. sz = sizeof(pubData);
  30586. ret = wc_EncodeSakkeRsk(priv, rsk, pubData, &sz, 0);
  30587. if (ret != 0)
  30588. return -10375;
  30589. if (sz != sizeof(pubData))
  30590. return -10376;
  30591. ret = wc_DecodeSakkeRsk(priv, pubData, sz, rsk);
  30592. if (ret != 0)
  30593. return -10377;
  30594. wc_FreeSakkeKey(key);
  30595. return 0;
  30596. }
  30597. static int sakke_op_test(SakkeKey* priv, SakkeKey* pub, WC_RNG* rng,
  30598. ecc_point* rsk)
  30599. {
  30600. int ret;
  30601. byte ssv[16];
  30602. word16 ssvSz;
  30603. byte auth[257];
  30604. word16 authSz;
  30605. char mail[] = "test@wolfssl.com";
  30606. byte* id = (byte*)mail;
  30607. word32 idSz = (word32)XSTRLEN(mail);
  30608. byte pointI[256];
  30609. word32 sz;
  30610. ret = wc_GenerateSakkeSSV(pub, rng, NULL, &ssvSz);
  30611. if (ret != LENGTH_ONLY_E)
  30612. return -10375;
  30613. if (ssvSz != 16)
  30614. return -10376;
  30615. ssvSz += 128;
  30616. ret = wc_GenerateSakkeSSV(pub, rng, ssv, &ssvSz);
  30617. if (ret == 0)
  30618. return -10377;
  30619. ssvSz -= 128;
  30620. ret = wc_GenerateSakkeSSV(pub, rng, ssv, &ssvSz);
  30621. if (ret != 0)
  30622. return -10378;
  30623. if (ssvSz != 16)
  30624. return -10379;
  30625. ret = wc_GetSakkeAuthSize(pub, &authSz);
  30626. if (ret != 0)
  30627. return -10380;
  30628. ret = wc_SetSakkeIdentity(pub, id, idSz);
  30629. if (ret != 0)
  30630. return -10380;
  30631. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  30632. NULL, &authSz);
  30633. if (ret != LENGTH_ONLY_E)
  30634. return -10381;
  30635. if (authSz != 257)
  30636. return -10382;
  30637. authSz--;
  30638. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  30639. auth, &authSz);
  30640. if (ret == 0)
  30641. return -10383;
  30642. authSz++;
  30643. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  30644. auth, &authSz);
  30645. if (ret != 0)
  30646. return -10384;
  30647. if (authSz != 257)
  30648. return -10385;
  30649. ret = wc_GetSakkePointI(pub, NULL, &sz);
  30650. if (ret != LENGTH_ONLY_E)
  30651. return -10386;
  30652. if (sz != 256)
  30653. return -10387;
  30654. ret = wc_GetSakkePointI(pub, pointI, &sz);
  30655. if (ret != 0)
  30656. return -10388;
  30657. if (sz != 256)
  30658. return -10389;
  30659. /* Bogus identity - make it check and regenerate I. */
  30660. ret = wc_MakeSakkePointI(pub, ssv, ssvSz);
  30661. if (ret != 0)
  30662. return -10391;
  30663. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  30664. auth, &authSz);
  30665. if (ret != 0)
  30666. return -10392;
  30667. if (authSz != 257)
  30668. return -10393;
  30669. ret = wc_SetSakkeRsk(priv, rsk, NULL, 0);
  30670. if (ret != 0)
  30671. return -10392;
  30672. ret = wc_SetSakkeIdentity(priv, id, idSz);
  30673. if (ret != 0)
  30674. return -10392;
  30675. authSz--;
  30676. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  30677. authSz);
  30678. if (ret == 0)
  30679. return -10394;
  30680. authSz++;
  30681. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  30682. authSz);
  30683. if (ret != 0)
  30684. return -10395;
  30685. ssv[0] ^= 0x80;
  30686. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  30687. authSz);
  30688. if (ret != SAKKE_VERIFY_FAIL_E)
  30689. return -10396;
  30690. ssv[0] ^= 0x80;
  30691. /* Bogus identity - make it check and regenerate I. */
  30692. ret = wc_MakeSakkePointI(pub, ssv, idSz);
  30693. if (ret != 0)
  30694. return -10397;
  30695. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  30696. authSz);
  30697. if (ret != 0)
  30698. return -10398;
  30699. return 0;
  30700. }
  30701. int sakke_test(void)
  30702. {
  30703. int ret = 0;
  30704. WC_RNG rng;
  30705. int rng_inited = 0;
  30706. SakkeKey* priv = NULL;
  30707. SakkeKey* pub = NULL;
  30708. SakkeKey* key = NULL;
  30709. ecc_point* rsk = NULL;
  30710. priv = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  30711. DYNAMIC_TYPE_TMP_BUFFER);
  30712. if (priv == NULL)
  30713. ret = -10404;
  30714. else
  30715. XMEMSET(priv, 0, sizeof(*priv));
  30716. if (ret == 0) {
  30717. pub = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  30718. DYNAMIC_TYPE_TMP_BUFFER);
  30719. if (pub == NULL)
  30720. ret = -10405;
  30721. else
  30722. XMEMSET(pub, 0, sizeof(*pub));
  30723. }
  30724. if (ret == 0) {
  30725. key = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  30726. DYNAMIC_TYPE_TMP_BUFFER);
  30727. if (key == NULL)
  30728. ret = -10406;
  30729. else
  30730. XMEMSET(key, 0, sizeof(*key));
  30731. }
  30732. if (ret == 0) {
  30733. #ifndef HAVE_FIPS
  30734. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  30735. #else
  30736. ret = wc_InitRng(&rng);
  30737. #endif
  30738. if (ret == 0)
  30739. rng_inited = 1;
  30740. else
  30741. ret = -10400;
  30742. }
  30743. if (ret == 0) {
  30744. rsk = wc_ecc_new_point();
  30745. if (rsk == NULL)
  30746. ret = -10401;
  30747. }
  30748. if (ret == 0) {
  30749. ret = wc_InitSakkeKey(pub, HEAP_HINT, INVALID_DEVID);
  30750. if (ret != 0)
  30751. ret = -10402;
  30752. }
  30753. if (ret == 0) {
  30754. ret = wc_InitSakkeKey(priv, HEAP_HINT, INVALID_DEVID);
  30755. if (ret != 0)
  30756. ret = -10403;
  30757. }
  30758. if (ret == 0) {
  30759. ret = sakke_api_test(&rng, key, rsk);
  30760. }
  30761. if (ret == 0) {
  30762. ret = sakke_kat_derive_test(pub, rsk);
  30763. }
  30764. if (ret == 0) {
  30765. ret = sakke_kat_encapsulate_test(pub);
  30766. }
  30767. if (ret == 0) {
  30768. ret = sakke_make_key_test(priv, pub, key, &rng, rsk);
  30769. }
  30770. if (ret == 0) {
  30771. ret = sakke_op_test(priv, pub, &rng, rsk);
  30772. }
  30773. if (rsk != NULL) {
  30774. wc_ecc_forcezero_point(rsk);
  30775. wc_ecc_del_point(rsk);
  30776. }
  30777. if (rng_inited)
  30778. wc_FreeRng(&rng);
  30779. if (key != NULL)
  30780. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30781. if (pub != NULL) {
  30782. wc_FreeSakkeKey(pub);
  30783. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30784. }
  30785. if (priv != NULL) {
  30786. wc_FreeSakkeKey(priv);
  30787. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30788. }
  30789. return ret;
  30790. }
  30791. #endif /* WOLFCRYPT_HAVE_SAKKE */
  30792. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  30793. typedef struct CMAC_Test_Case {
  30794. int type;
  30795. int partial;
  30796. const byte* m;
  30797. word32 mSz;
  30798. const byte* k;
  30799. word32 kSz;
  30800. const byte* t;
  30801. word32 tSz;
  30802. } CMAC_Test_Case;
  30803. WOLFSSL_TEST_SUBROUTINE int cmac_test(void)
  30804. {
  30805. #ifdef WOLFSSL_AES_128
  30806. WOLFSSL_SMALL_STACK_STATIC const byte k128[] =
  30807. {
  30808. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  30809. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  30810. };
  30811. #define KLEN_128 (sizeof(k128))
  30812. #endif
  30813. #ifdef WOLFSSL_AES_192
  30814. WOLFSSL_SMALL_STACK_STATIC const byte k192[] =
  30815. {
  30816. 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52,
  30817. 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
  30818. 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b
  30819. };
  30820. #define KLEN_192 (sizeof(k192))
  30821. #endif
  30822. #ifdef WOLFSSL_AES_256
  30823. WOLFSSL_SMALL_STACK_STATIC const byte k256[] =
  30824. {
  30825. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  30826. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  30827. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  30828. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  30829. };
  30830. #define KLEN_256 (sizeof(k256))
  30831. #endif
  30832. WOLFSSL_SMALL_STACK_STATIC const byte m[] =
  30833. {
  30834. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  30835. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  30836. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  30837. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  30838. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
  30839. 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  30840. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
  30841. 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
  30842. };
  30843. #define MLEN_0 (0)
  30844. #define MLEN_128 (128/8)
  30845. #define MLEN_320 (320/8)
  30846. #define MLEN_319 (MLEN_320 - 1)
  30847. #define MLEN_512 (512/8)
  30848. #ifdef WOLFSSL_AES_128
  30849. WOLFSSL_SMALL_STACK_STATIC const byte t128_0[] =
  30850. {
  30851. 0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28,
  30852. 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46
  30853. };
  30854. WOLFSSL_SMALL_STACK_STATIC const byte t128_128[] =
  30855. {
  30856. 0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
  30857. 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c
  30858. };
  30859. WOLFSSL_SMALL_STACK_STATIC const byte t128_319[] =
  30860. {
  30861. 0x2c, 0x17, 0x84, 0x4c, 0x93, 0x1c, 0x07, 0x95,
  30862. 0x15, 0x92, 0x73, 0x0a, 0x34, 0xd0, 0xd9, 0xd2
  30863. };
  30864. WOLFSSL_SMALL_STACK_STATIC const byte t128_320[] =
  30865. {
  30866. 0xdf, 0xa6, 0x67, 0x47, 0xde, 0x9a, 0xe6, 0x30,
  30867. 0x30, 0xca, 0x32, 0x61, 0x14, 0x97, 0xc8, 0x27
  30868. };
  30869. WOLFSSL_SMALL_STACK_STATIC const byte t128_512[] =
  30870. {
  30871. 0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92,
  30872. 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe
  30873. };
  30874. #endif
  30875. #ifdef WOLFSSL_AES_192
  30876. WOLFSSL_SMALL_STACK_STATIC const byte t192_0[] =
  30877. {
  30878. 0xd1, 0x7d, 0xdf, 0x46, 0xad, 0xaa, 0xcd, 0xe5,
  30879. 0x31, 0xca, 0xc4, 0x83, 0xde, 0x7a, 0x93, 0x67
  30880. };
  30881. WOLFSSL_SMALL_STACK_STATIC const byte t192_128[] =
  30882. {
  30883. 0x9e, 0x99, 0xa7, 0xbf, 0x31, 0xe7, 0x10, 0x90,
  30884. 0x06, 0x62, 0xf6, 0x5e, 0x61, 0x7c, 0x51, 0x84
  30885. };
  30886. WOLFSSL_SMALL_STACK_STATIC const byte t192_320[] =
  30887. {
  30888. 0x8a, 0x1d, 0xe5, 0xbe, 0x2e, 0xb3, 0x1a, 0xad,
  30889. 0x08, 0x9a, 0x82, 0xe6, 0xee, 0x90, 0x8b, 0x0e
  30890. };
  30891. WOLFSSL_SMALL_STACK_STATIC const byte t192_512[] =
  30892. {
  30893. 0xa1, 0xd5, 0xdf, 0x0e, 0xed, 0x79, 0x0f, 0x79,
  30894. 0x4d, 0x77, 0x58, 0x96, 0x59, 0xf3, 0x9a, 0x11
  30895. };
  30896. #endif
  30897. #ifdef WOLFSSL_AES_256
  30898. WOLFSSL_SMALL_STACK_STATIC const byte t256_0[] =
  30899. {
  30900. 0x02, 0x89, 0x62, 0xf6, 0x1b, 0x7b, 0xf8, 0x9e,
  30901. 0xfc, 0x6b, 0x55, 0x1f, 0x46, 0x67, 0xd9, 0x83
  30902. };
  30903. WOLFSSL_SMALL_STACK_STATIC const byte t256_128[] =
  30904. {
  30905. 0x28, 0xa7, 0x02, 0x3f, 0x45, 0x2e, 0x8f, 0x82,
  30906. 0xbd, 0x4b, 0xf2, 0x8d, 0x8c, 0x37, 0xc3, 0x5c
  30907. };
  30908. WOLFSSL_SMALL_STACK_STATIC const byte t256_320[] =
  30909. {
  30910. 0xaa, 0xf3, 0xd8, 0xf1, 0xde, 0x56, 0x40, 0xc2,
  30911. 0x32, 0xf5, 0xb1, 0x69, 0xb9, 0xc9, 0x11, 0xe6
  30912. };
  30913. WOLFSSL_SMALL_STACK_STATIC const byte t256_512[] =
  30914. {
  30915. 0xe1, 0x99, 0x21, 0x90, 0x54, 0x9f, 0x6e, 0xd5,
  30916. 0x69, 0x6a, 0x2c, 0x05, 0x6c, 0x31, 0x54, 0x10
  30917. };
  30918. #endif
  30919. const CMAC_Test_Case testCases[] =
  30920. {
  30921. #ifdef WOLFSSL_AES_128
  30922. {WC_CMAC_AES, 0, m, MLEN_0, k128, KLEN_128, t128_0, AES_BLOCK_SIZE},
  30923. {WC_CMAC_AES, 0, m, MLEN_128, k128, KLEN_128, t128_128, AES_BLOCK_SIZE},
  30924. {WC_CMAC_AES, 0, m, MLEN_320, k128, KLEN_128, t128_320, AES_BLOCK_SIZE},
  30925. {WC_CMAC_AES, 0, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  30926. {WC_CMAC_AES, 5, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  30927. #endif
  30928. #ifdef WOLFSSL_AES_192
  30929. {WC_CMAC_AES, 0, m, MLEN_0, k192, KLEN_192, t192_0, AES_BLOCK_SIZE},
  30930. {WC_CMAC_AES, 0, m, MLEN_128, k192, KLEN_192, t192_128, AES_BLOCK_SIZE},
  30931. {WC_CMAC_AES, 0, m, MLEN_320, k192, KLEN_192, t192_320, AES_BLOCK_SIZE},
  30932. {WC_CMAC_AES, 0, m, MLEN_512, k192, KLEN_192, t192_512, AES_BLOCK_SIZE},
  30933. #endif
  30934. #ifdef WOLFSSL_AES_256
  30935. {WC_CMAC_AES, 0, m, MLEN_0, k256, KLEN_256, t256_0, AES_BLOCK_SIZE},
  30936. {WC_CMAC_AES, 0, m, MLEN_128, k256, KLEN_256, t256_128, AES_BLOCK_SIZE},
  30937. {WC_CMAC_AES, 0, m, MLEN_320, k256, KLEN_256, t256_320, AES_BLOCK_SIZE},
  30938. {WC_CMAC_AES, 0, m, MLEN_512, k256, KLEN_256, t256_512, AES_BLOCK_SIZE},
  30939. #endif
  30940. #ifdef WOLFSSL_AES_128
  30941. {WC_CMAC_AES, 0, m, MLEN_319, k128, KLEN_128, t128_319, AES_BLOCK_SIZE}
  30942. #endif
  30943. };
  30944. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30945. Cmac *cmac;
  30946. #else
  30947. Cmac cmac[1];
  30948. #endif
  30949. byte tag[AES_BLOCK_SIZE];
  30950. const CMAC_Test_Case* tc;
  30951. word32 i, tagSz;
  30952. int ret;
  30953. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30954. if ((cmac = (Cmac *)XMALLOC(sizeof *cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC)) == NULL)
  30955. ERROR_OUT(-12009, out);
  30956. #endif
  30957. for (i = 0, tc = testCases;
  30958. i < sizeof(testCases)/sizeof(CMAC_Test_Case);
  30959. i++, tc++) {
  30960. XMEMSET(tag, 0, sizeof(tag));
  30961. tagSz = AES_BLOCK_SIZE;
  30962. #if !defined(HAVE_FIPS) || \
  30963. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)
  30964. if (wc_InitCmac_ex(cmac, tc->k, tc->kSz, tc->type, NULL, HEAP_HINT, devId) != 0)
  30965. #else
  30966. if (wc_InitCmac(cmac, tc->k, tc->kSz, tc->type, NULL) != 0)
  30967. #endif
  30968. {
  30969. ERROR_OUT(-12000, out);
  30970. }
  30971. if (tc->partial) {
  30972. if (wc_CmacUpdate(cmac, tc->m,
  30973. tc->mSz/2 - tc->partial) != 0)
  30974. ERROR_OUT(-12001, out);
  30975. if (wc_CmacUpdate(cmac, tc->m + tc->mSz/2 - tc->partial,
  30976. tc->mSz/2 + tc->partial) != 0)
  30977. ERROR_OUT(-12002, out);
  30978. }
  30979. else {
  30980. if (wc_CmacUpdate(cmac, tc->m, tc->mSz) != 0)
  30981. ERROR_OUT(-12003, out);
  30982. }
  30983. if (wc_CmacFinal(cmac, tag, &tagSz) != 0)
  30984. ERROR_OUT(-12004, out);
  30985. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  30986. ERROR_OUT(-12005, out);
  30987. XMEMSET(tag, 0, sizeof(tag));
  30988. tagSz = sizeof(tag);
  30989. if (wc_AesCmacGenerate(tag, &tagSz, tc->m, tc->mSz,
  30990. tc->k, tc->kSz) != 0)
  30991. ERROR_OUT(-12006, out);
  30992. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  30993. ERROR_OUT(-12007, out);
  30994. if (wc_AesCmacVerify(tc->t, tc->tSz, tc->m, tc->mSz,
  30995. tc->k, tc->kSz) != 0)
  30996. ERROR_OUT(-12008, out);
  30997. }
  30998. ret = 0;
  30999. out:
  31000. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  31001. if (cmac)
  31002. XFREE(cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC);
  31003. #endif
  31004. return ret;
  31005. }
  31006. #endif /* NO_AES && WOLFSSL_CMAC */
  31007. #if defined(WOLFSSL_SIPHASH)
  31008. #if WOLFSSL_SIPHASH_CROUNDS == 2 && WOLFSSL_SIPHASH_DROUNDS == 4
  31009. /* Test vectors from:
  31010. * https://github.com/veorq/SipHash/blob/master/vectors.h
  31011. */
  31012. static const unsigned char siphash_key[SIPHASH_KEY_SIZE] = {
  31013. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  31014. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  31015. };
  31016. static const unsigned char siphash_msg[64] = {
  31017. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  31018. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  31019. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  31020. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  31021. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  31022. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  31023. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  31024. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f
  31025. };
  31026. static const unsigned char siphash_r8[64][SIPHASH_MAC_SIZE_8] = {
  31027. { 0x31, 0x0e, 0x0e, 0xdd, 0x47, 0xdb, 0x6f, 0x72, },
  31028. { 0xfd, 0x67, 0xdc, 0x93, 0xc5, 0x39, 0xf8, 0x74, },
  31029. { 0x5a, 0x4f, 0xa9, 0xd9, 0x09, 0x80, 0x6c, 0x0d, },
  31030. { 0x2d, 0x7e, 0xfb, 0xd7, 0x96, 0x66, 0x67, 0x85, },
  31031. { 0xb7, 0x87, 0x71, 0x27, 0xe0, 0x94, 0x27, 0xcf, },
  31032. { 0x8d, 0xa6, 0x99, 0xcd, 0x64, 0x55, 0x76, 0x18, },
  31033. { 0xce, 0xe3, 0xfe, 0x58, 0x6e, 0x46, 0xc9, 0xcb, },
  31034. { 0x37, 0xd1, 0x01, 0x8b, 0xf5, 0x00, 0x02, 0xab, },
  31035. { 0x62, 0x24, 0x93, 0x9a, 0x79, 0xf5, 0xf5, 0x93, },
  31036. { 0xb0, 0xe4, 0xa9, 0x0b, 0xdf, 0x82, 0x00, 0x9e, },
  31037. { 0xf3, 0xb9, 0xdd, 0x94, 0xc5, 0xbb, 0x5d, 0x7a, },
  31038. { 0xa7, 0xad, 0x6b, 0x22, 0x46, 0x2f, 0xb3, 0xf4, },
  31039. { 0xfb, 0xe5, 0x0e, 0x86, 0xbc, 0x8f, 0x1e, 0x75, },
  31040. { 0x90, 0x3d, 0x84, 0xc0, 0x27, 0x56, 0xea, 0x14, },
  31041. { 0xee, 0xf2, 0x7a, 0x8e, 0x90, 0xca, 0x23, 0xf7, },
  31042. { 0xe5, 0x45, 0xbe, 0x49, 0x61, 0xca, 0x29, 0xa1, },
  31043. { 0xdb, 0x9b, 0xc2, 0x57, 0x7f, 0xcc, 0x2a, 0x3f, },
  31044. { 0x94, 0x47, 0xbe, 0x2c, 0xf5, 0xe9, 0x9a, 0x69, },
  31045. { 0x9c, 0xd3, 0x8d, 0x96, 0xf0, 0xb3, 0xc1, 0x4b, },
  31046. { 0xbd, 0x61, 0x79, 0xa7, 0x1d, 0xc9, 0x6d, 0xbb, },
  31047. { 0x98, 0xee, 0xa2, 0x1a, 0xf2, 0x5c, 0xd6, 0xbe, },
  31048. { 0xc7, 0x67, 0x3b, 0x2e, 0xb0, 0xcb, 0xf2, 0xd0, },
  31049. { 0x88, 0x3e, 0xa3, 0xe3, 0x95, 0x67, 0x53, 0x93, },
  31050. { 0xc8, 0xce, 0x5c, 0xcd, 0x8c, 0x03, 0x0c, 0xa8, },
  31051. { 0x94, 0xaf, 0x49, 0xf6, 0xc6, 0x50, 0xad, 0xb8, },
  31052. { 0xea, 0xb8, 0x85, 0x8a, 0xde, 0x92, 0xe1, 0xbc, },
  31053. { 0xf3, 0x15, 0xbb, 0x5b, 0xb8, 0x35, 0xd8, 0x17, },
  31054. { 0xad, 0xcf, 0x6b, 0x07, 0x63, 0x61, 0x2e, 0x2f, },
  31055. { 0xa5, 0xc9, 0x1d, 0xa7, 0xac, 0xaa, 0x4d, 0xde, },
  31056. { 0x71, 0x65, 0x95, 0x87, 0x66, 0x50, 0xa2, 0xa6, },
  31057. { 0x28, 0xef, 0x49, 0x5c, 0x53, 0xa3, 0x87, 0xad, },
  31058. { 0x42, 0xc3, 0x41, 0xd8, 0xfa, 0x92, 0xd8, 0x32, },
  31059. { 0xce, 0x7c, 0xf2, 0x72, 0x2f, 0x51, 0x27, 0x71, },
  31060. { 0xe3, 0x78, 0x59, 0xf9, 0x46, 0x23, 0xf3, 0xa7, },
  31061. { 0x38, 0x12, 0x05, 0xbb, 0x1a, 0xb0, 0xe0, 0x12, },
  31062. { 0xae, 0x97, 0xa1, 0x0f, 0xd4, 0x34, 0xe0, 0x15, },
  31063. { 0xb4, 0xa3, 0x15, 0x08, 0xbe, 0xff, 0x4d, 0x31, },
  31064. { 0x81, 0x39, 0x62, 0x29, 0xf0, 0x90, 0x79, 0x02, },
  31065. { 0x4d, 0x0c, 0xf4, 0x9e, 0xe5, 0xd4, 0xdc, 0xca, },
  31066. { 0x5c, 0x73, 0x33, 0x6a, 0x76, 0xd8, 0xbf, 0x9a, },
  31067. { 0xd0, 0xa7, 0x04, 0x53, 0x6b, 0xa9, 0x3e, 0x0e, },
  31068. { 0x92, 0x59, 0x58, 0xfc, 0xd6, 0x42, 0x0c, 0xad, },
  31069. { 0xa9, 0x15, 0xc2, 0x9b, 0xc8, 0x06, 0x73, 0x18, },
  31070. { 0x95, 0x2b, 0x79, 0xf3, 0xbc, 0x0a, 0xa6, 0xd4, },
  31071. { 0xf2, 0x1d, 0xf2, 0xe4, 0x1d, 0x45, 0x35, 0xf9, },
  31072. { 0x87, 0x57, 0x75, 0x19, 0x04, 0x8f, 0x53, 0xa9, },
  31073. { 0x10, 0xa5, 0x6c, 0xf5, 0xdf, 0xcd, 0x9a, 0xdb, },
  31074. { 0xeb, 0x75, 0x09, 0x5c, 0xcd, 0x98, 0x6c, 0xd0, },
  31075. { 0x51, 0xa9, 0xcb, 0x9e, 0xcb, 0xa3, 0x12, 0xe6, },
  31076. { 0x96, 0xaf, 0xad, 0xfc, 0x2c, 0xe6, 0x66, 0xc7, },
  31077. { 0x72, 0xfe, 0x52, 0x97, 0x5a, 0x43, 0x64, 0xee, },
  31078. { 0x5a, 0x16, 0x45, 0xb2, 0x76, 0xd5, 0x92, 0xa1, },
  31079. { 0xb2, 0x74, 0xcb, 0x8e, 0xbf, 0x87, 0x87, 0x0a, },
  31080. { 0x6f, 0x9b, 0xb4, 0x20, 0x3d, 0xe7, 0xb3, 0x81, },
  31081. { 0xea, 0xec, 0xb2, 0xa3, 0x0b, 0x22, 0xa8, 0x7f, },
  31082. { 0x99, 0x24, 0xa4, 0x3c, 0xc1, 0x31, 0x57, 0x24, },
  31083. { 0xbd, 0x83, 0x8d, 0x3a, 0xaf, 0xbf, 0x8d, 0xb7, },
  31084. { 0x0b, 0x1a, 0x2a, 0x32, 0x65, 0xd5, 0x1a, 0xea, },
  31085. { 0x13, 0x50, 0x79, 0xa3, 0x23, 0x1c, 0xe6, 0x60, },
  31086. { 0x93, 0x2b, 0x28, 0x46, 0xe4, 0xd7, 0x06, 0x66, },
  31087. { 0xe1, 0x91, 0x5f, 0x5c, 0xb1, 0xec, 0xa4, 0x6c, },
  31088. { 0xf3, 0x25, 0x96, 0x5c, 0xa1, 0x6d, 0x62, 0x9f, },
  31089. { 0x57, 0x5f, 0xf2, 0x8e, 0x60, 0x38, 0x1b, 0xe5, },
  31090. { 0x72, 0x45, 0x06, 0xeb, 0x4c, 0x32, 0x8a, 0x95, },
  31091. };
  31092. static const unsigned char siphash_r16[64][SIPHASH_MAC_SIZE_16] = {
  31093. { 0xa3, 0x81, 0x7f, 0x04, 0xba, 0x25, 0xa8, 0xe6,
  31094. 0x6d, 0xf6, 0x72, 0x14, 0xc7, 0x55, 0x02, 0x93, },
  31095. { 0xda, 0x87, 0xc1, 0xd8, 0x6b, 0x99, 0xaf, 0x44,
  31096. 0x34, 0x76, 0x59, 0x11, 0x9b, 0x22, 0xfc, 0x45, },
  31097. { 0x81, 0x77, 0x22, 0x8d, 0xa4, 0xa4, 0x5d, 0xc7,
  31098. 0xfc, 0xa3, 0x8b, 0xde, 0xf6, 0x0a, 0xff, 0xe4, },
  31099. { 0x9c, 0x70, 0xb6, 0x0c, 0x52, 0x67, 0xa9, 0x4e,
  31100. 0x5f, 0x33, 0xb6, 0xb0, 0x29, 0x85, 0xed, 0x51, },
  31101. { 0xf8, 0x81, 0x64, 0xc1, 0x2d, 0x9c, 0x8f, 0xaf,
  31102. 0x7d, 0x0f, 0x6e, 0x7c, 0x7b, 0xcd, 0x55, 0x79, },
  31103. { 0x13, 0x68, 0x87, 0x59, 0x80, 0x77, 0x6f, 0x88,
  31104. 0x54, 0x52, 0x7a, 0x07, 0x69, 0x0e, 0x96, 0x27, },
  31105. { 0x14, 0xee, 0xca, 0x33, 0x8b, 0x20, 0x86, 0x13,
  31106. 0x48, 0x5e, 0xa0, 0x30, 0x8f, 0xd7, 0xa1, 0x5e, },
  31107. { 0xa1, 0xf1, 0xeb, 0xbe, 0xd8, 0xdb, 0xc1, 0x53,
  31108. 0xc0, 0xb8, 0x4a, 0xa6, 0x1f, 0xf0, 0x82, 0x39, },
  31109. { 0x3b, 0x62, 0xa9, 0xba, 0x62, 0x58, 0xf5, 0x61,
  31110. 0x0f, 0x83, 0xe2, 0x64, 0xf3, 0x14, 0x97, 0xb4, },
  31111. { 0x26, 0x44, 0x99, 0x06, 0x0a, 0xd9, 0xba, 0xab,
  31112. 0xc4, 0x7f, 0x8b, 0x02, 0xbb, 0x6d, 0x71, 0xed, },
  31113. { 0x00, 0x11, 0x0d, 0xc3, 0x78, 0x14, 0x69, 0x56,
  31114. 0xc9, 0x54, 0x47, 0xd3, 0xf3, 0xd0, 0xfb, 0xba, },
  31115. { 0x01, 0x51, 0xc5, 0x68, 0x38, 0x6b, 0x66, 0x77,
  31116. 0xa2, 0xb4, 0xdc, 0x6f, 0x81, 0xe5, 0xdc, 0x18, },
  31117. { 0xd6, 0x26, 0xb2, 0x66, 0x90, 0x5e, 0xf3, 0x58,
  31118. 0x82, 0x63, 0x4d, 0xf6, 0x85, 0x32, 0xc1, 0x25, },
  31119. { 0x98, 0x69, 0xe2, 0x47, 0xe9, 0xc0, 0x8b, 0x10,
  31120. 0xd0, 0x29, 0x93, 0x4f, 0xc4, 0xb9, 0x52, 0xf7, },
  31121. { 0x31, 0xfc, 0xef, 0xac, 0x66, 0xd7, 0xde, 0x9c,
  31122. 0x7e, 0xc7, 0x48, 0x5f, 0xe4, 0x49, 0x49, 0x02, },
  31123. { 0x54, 0x93, 0xe9, 0x99, 0x33, 0xb0, 0xa8, 0x11,
  31124. 0x7e, 0x08, 0xec, 0x0f, 0x97, 0xcf, 0xc3, 0xd9, },
  31125. { 0x6e, 0xe2, 0xa4, 0xca, 0x67, 0xb0, 0x54, 0xbb,
  31126. 0xfd, 0x33, 0x15, 0xbf, 0x85, 0x23, 0x05, 0x77, },
  31127. { 0x47, 0x3d, 0x06, 0xe8, 0x73, 0x8d, 0xb8, 0x98,
  31128. 0x54, 0xc0, 0x66, 0xc4, 0x7a, 0xe4, 0x77, 0x40, },
  31129. { 0xa4, 0x26, 0xe5, 0xe4, 0x23, 0xbf, 0x48, 0x85,
  31130. 0x29, 0x4d, 0xa4, 0x81, 0xfe, 0xae, 0xf7, 0x23, },
  31131. { 0x78, 0x01, 0x77, 0x31, 0xcf, 0x65, 0xfa, 0xb0,
  31132. 0x74, 0xd5, 0x20, 0x89, 0x52, 0x51, 0x2e, 0xb1, },
  31133. { 0x9e, 0x25, 0xfc, 0x83, 0x3f, 0x22, 0x90, 0x73,
  31134. 0x3e, 0x93, 0x44, 0xa5, 0xe8, 0x38, 0x39, 0xeb, },
  31135. { 0x56, 0x8e, 0x49, 0x5a, 0xbe, 0x52, 0x5a, 0x21,
  31136. 0x8a, 0x22, 0x14, 0xcd, 0x3e, 0x07, 0x1d, 0x12, },
  31137. { 0x4a, 0x29, 0xb5, 0x45, 0x52, 0xd1, 0x6b, 0x9a,
  31138. 0x46, 0x9c, 0x10, 0x52, 0x8e, 0xff, 0x0a, 0xae, },
  31139. { 0xc9, 0xd1, 0x84, 0xdd, 0xd5, 0xa9, 0xf5, 0xe0,
  31140. 0xcf, 0x8c, 0xe2, 0x9a, 0x9a, 0xbf, 0x69, 0x1c, },
  31141. { 0x2d, 0xb4, 0x79, 0xae, 0x78, 0xbd, 0x50, 0xd8,
  31142. 0x88, 0x2a, 0x8a, 0x17, 0x8a, 0x61, 0x32, 0xad, },
  31143. { 0x8e, 0xce, 0x5f, 0x04, 0x2d, 0x5e, 0x44, 0x7b,
  31144. 0x50, 0x51, 0xb9, 0xea, 0xcb, 0x8d, 0x8f, 0x6f, },
  31145. { 0x9c, 0x0b, 0x53, 0xb4, 0xb3, 0xc3, 0x07, 0xe8,
  31146. 0x7e, 0xae, 0xe0, 0x86, 0x78, 0x14, 0x1f, 0x66, },
  31147. { 0xab, 0xf2, 0x48, 0xaf, 0x69, 0xa6, 0xea, 0xe4,
  31148. 0xbf, 0xd3, 0xeb, 0x2f, 0x12, 0x9e, 0xeb, 0x94, },
  31149. { 0x06, 0x64, 0xda, 0x16, 0x68, 0x57, 0x4b, 0x88,
  31150. 0xb9, 0x35, 0xf3, 0x02, 0x73, 0x58, 0xae, 0xf4, },
  31151. { 0xaa, 0x4b, 0x9d, 0xc4, 0xbf, 0x33, 0x7d, 0xe9,
  31152. 0x0c, 0xd4, 0xfd, 0x3c, 0x46, 0x7c, 0x6a, 0xb7, },
  31153. { 0xea, 0x5c, 0x7f, 0x47, 0x1f, 0xaf, 0x6b, 0xde,
  31154. 0x2b, 0x1a, 0xd7, 0xd4, 0x68, 0x6d, 0x22, 0x87, },
  31155. { 0x29, 0x39, 0xb0, 0x18, 0x32, 0x23, 0xfa, 0xfc,
  31156. 0x17, 0x23, 0xde, 0x4f, 0x52, 0xc4, 0x3d, 0x35, },
  31157. { 0x7c, 0x39, 0x56, 0xca, 0x5e, 0xea, 0xfc, 0x3e,
  31158. 0x36, 0x3e, 0x9d, 0x55, 0x65, 0x46, 0xeb, 0x68, },
  31159. { 0x77, 0xc6, 0x07, 0x71, 0x46, 0xf0, 0x1c, 0x32,
  31160. 0xb6, 0xb6, 0x9d, 0x5f, 0x4e, 0xa9, 0xff, 0xcf, },
  31161. { 0x37, 0xa6, 0x98, 0x6c, 0xb8, 0x84, 0x7e, 0xdf,
  31162. 0x09, 0x25, 0xf0, 0xf1, 0x30, 0x9b, 0x54, 0xde, },
  31163. { 0xa7, 0x05, 0xf0, 0xe6, 0x9d, 0xa9, 0xa8, 0xf9,
  31164. 0x07, 0x24, 0x1a, 0x2e, 0x92, 0x3c, 0x8c, 0xc8, },
  31165. { 0x3d, 0xc4, 0x7d, 0x1f, 0x29, 0xc4, 0x48, 0x46,
  31166. 0x1e, 0x9e, 0x76, 0xed, 0x90, 0x4f, 0x67, 0x11, },
  31167. { 0x0d, 0x62, 0xbf, 0x01, 0xe6, 0xfc, 0x0e, 0x1a,
  31168. 0x0d, 0x3c, 0x47, 0x51, 0xc5, 0xd3, 0x69, 0x2b, },
  31169. { 0x8c, 0x03, 0x46, 0x8b, 0xca, 0x7c, 0x66, 0x9e,
  31170. 0xe4, 0xfd, 0x5e, 0x08, 0x4b, 0xbe, 0xe7, 0xb5, },
  31171. { 0x52, 0x8a, 0x5b, 0xb9, 0x3b, 0xaf, 0x2c, 0x9c,
  31172. 0x44, 0x73, 0xcc, 0xe5, 0xd0, 0xd2, 0x2b, 0xd9, },
  31173. { 0xdf, 0x6a, 0x30, 0x1e, 0x95, 0xc9, 0x5d, 0xad,
  31174. 0x97, 0xae, 0x0c, 0xc8, 0xc6, 0x91, 0x3b, 0xd8, },
  31175. { 0x80, 0x11, 0x89, 0x90, 0x2c, 0x85, 0x7f, 0x39,
  31176. 0xe7, 0x35, 0x91, 0x28, 0x5e, 0x70, 0xb6, 0xdb, },
  31177. { 0xe6, 0x17, 0x34, 0x6a, 0xc9, 0xc2, 0x31, 0xbb,
  31178. 0x36, 0x50, 0xae, 0x34, 0xcc, 0xca, 0x0c, 0x5b, },
  31179. { 0x27, 0xd9, 0x34, 0x37, 0xef, 0xb7, 0x21, 0xaa,
  31180. 0x40, 0x18, 0x21, 0xdc, 0xec, 0x5a, 0xdf, 0x89, },
  31181. { 0x89, 0x23, 0x7d, 0x9d, 0xed, 0x9c, 0x5e, 0x78,
  31182. 0xd8, 0xb1, 0xc9, 0xb1, 0x66, 0xcc, 0x73, 0x42, },
  31183. { 0x4a, 0x6d, 0x80, 0x91, 0xbf, 0x5e, 0x7d, 0x65,
  31184. 0x11, 0x89, 0xfa, 0x94, 0xa2, 0x50, 0xb1, 0x4c, },
  31185. { 0x0e, 0x33, 0xf9, 0x60, 0x55, 0xe7, 0xae, 0x89,
  31186. 0x3f, 0xfc, 0x0e, 0x3d, 0xcf, 0x49, 0x29, 0x02, },
  31187. { 0xe6, 0x1c, 0x43, 0x2b, 0x72, 0x0b, 0x19, 0xd1,
  31188. 0x8e, 0xc8, 0xd8, 0x4b, 0xdc, 0x63, 0x15, 0x1b, },
  31189. { 0xf7, 0xe5, 0xae, 0xf5, 0x49, 0xf7, 0x82, 0xcf,
  31190. 0x37, 0x90, 0x55, 0xa6, 0x08, 0x26, 0x9b, 0x16, },
  31191. { 0x43, 0x8d, 0x03, 0x0f, 0xd0, 0xb7, 0xa5, 0x4f,
  31192. 0xa8, 0x37, 0xf2, 0xad, 0x20, 0x1a, 0x64, 0x03, },
  31193. { 0xa5, 0x90, 0xd3, 0xee, 0x4f, 0xbf, 0x04, 0xe3,
  31194. 0x24, 0x7e, 0x0d, 0x27, 0xf2, 0x86, 0x42, 0x3f, },
  31195. { 0x5f, 0xe2, 0xc1, 0xa1, 0x72, 0xfe, 0x93, 0xc4,
  31196. 0xb1, 0x5c, 0xd3, 0x7c, 0xae, 0xf9, 0xf5, 0x38, },
  31197. { 0x2c, 0x97, 0x32, 0x5c, 0xbd, 0x06, 0xb3, 0x6e,
  31198. 0xb2, 0x13, 0x3d, 0xd0, 0x8b, 0x3a, 0x01, 0x7c, },
  31199. { 0x92, 0xc8, 0x14, 0x22, 0x7a, 0x6b, 0xca, 0x94,
  31200. 0x9f, 0xf0, 0x65, 0x9f, 0x00, 0x2a, 0xd3, 0x9e, },
  31201. { 0xdc, 0xe8, 0x50, 0x11, 0x0b, 0xd8, 0x32, 0x8c,
  31202. 0xfb, 0xd5, 0x08, 0x41, 0xd6, 0x91, 0x1d, 0x87, },
  31203. { 0x67, 0xf1, 0x49, 0x84, 0xc7, 0xda, 0x79, 0x12,
  31204. 0x48, 0xe3, 0x2b, 0xb5, 0x92, 0x25, 0x83, 0xda, },
  31205. { 0x19, 0x38, 0xf2, 0xcf, 0x72, 0xd5, 0x4e, 0xe9,
  31206. 0x7e, 0x94, 0x16, 0x6f, 0xa9, 0x1d, 0x2a, 0x36, },
  31207. { 0x74, 0x48, 0x1e, 0x96, 0x46, 0xed, 0x49, 0xfe,
  31208. 0x0f, 0x62, 0x24, 0x30, 0x16, 0x04, 0x69, 0x8e, },
  31209. { 0x57, 0xfc, 0xa5, 0xde, 0x98, 0xa9, 0xd6, 0xd8,
  31210. 0x00, 0x64, 0x38, 0xd0, 0x58, 0x3d, 0x8a, 0x1d, },
  31211. { 0x9f, 0xec, 0xde, 0x1c, 0xef, 0xdc, 0x1c, 0xbe,
  31212. 0xd4, 0x76, 0x36, 0x74, 0xd9, 0x57, 0x53, 0x59, },
  31213. { 0xe3, 0x04, 0x0c, 0x00, 0xeb, 0x28, 0xf1, 0x53,
  31214. 0x66, 0xca, 0x73, 0xcb, 0xd8, 0x72, 0xe7, 0x40, },
  31215. { 0x76, 0x97, 0x00, 0x9a, 0x6a, 0x83, 0x1d, 0xfe,
  31216. 0xcc, 0xa9, 0x1c, 0x59, 0x93, 0x67, 0x0f, 0x7a, },
  31217. { 0x58, 0x53, 0x54, 0x23, 0x21, 0xf5, 0x67, 0xa0,
  31218. 0x05, 0xd5, 0x47, 0xa4, 0xf0, 0x47, 0x59, 0xbd, },
  31219. { 0x51, 0x50, 0xd1, 0x77, 0x2f, 0x50, 0x83, 0x4a,
  31220. 0x50, 0x3e, 0x06, 0x9a, 0x97, 0x3f, 0xbd, 0x7c, },
  31221. };
  31222. #endif
  31223. WOLFSSL_TEST_SUBROUTINE int siphash_test(void)
  31224. {
  31225. int ret = 0;
  31226. int i;
  31227. #if WOLFSSL_SIPHASH_CROUNDS == 2 && WOLFSSL_SIPHASH_DROUNDS == 4
  31228. unsigned char res[SIPHASH_MAC_SIZE_16];
  31229. SipHash siphash;
  31230. for (i = 0; i < 64; i++) {
  31231. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  31232. if (ret != 0)
  31233. return -12100 - i;
  31234. ret = wc_SipHashUpdate(&siphash, siphash_msg, i);
  31235. if (ret != 0)
  31236. return -12200 - i;
  31237. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_8);
  31238. if (ret != 0)
  31239. return -12300 - i;
  31240. if (XMEMCMP(res, siphash_r8[i], SIPHASH_MAC_SIZE_8) != 0)
  31241. return -12400 - i;
  31242. ret = wc_SipHash(siphash_key, siphash_msg, i, res, SIPHASH_MAC_SIZE_8);
  31243. if (ret != 0)
  31244. return -12500 - i;
  31245. if (XMEMCMP(res, siphash_r8[i], SIPHASH_MAC_SIZE_8) != 0)
  31246. return -12600 - i;
  31247. }
  31248. for (i = 0; i < 64; i++) {
  31249. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_16);
  31250. if (ret != 0)
  31251. return -12700 - i;
  31252. ret = wc_SipHashUpdate(&siphash, siphash_msg, i);
  31253. if (ret != 0)
  31254. return -12800 - i;
  31255. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_16);
  31256. if (ret != 0)
  31257. return -12900 - i;
  31258. if (XMEMCMP(res, siphash_r16[i], SIPHASH_MAC_SIZE_16) != 0)
  31259. return -13000 - i;
  31260. ret = wc_SipHash(siphash_key, siphash_msg, i, res, SIPHASH_MAC_SIZE_16);
  31261. if (ret != 0)
  31262. return -13100 - i;
  31263. if (XMEMCMP(res, siphash_r16[i], SIPHASH_MAC_SIZE_16) != 0)
  31264. return -13200 - i;
  31265. }
  31266. #endif
  31267. /* Testing bad parameters. */
  31268. ret = wc_InitSipHash(NULL, NULL, SIPHASH_MAC_SIZE_8);
  31269. if (ret != BAD_FUNC_ARG)
  31270. return -13300;
  31271. ret = wc_InitSipHash(NULL, siphash_key, SIPHASH_MAC_SIZE_8);
  31272. if (ret != BAD_FUNC_ARG)
  31273. return -13301;
  31274. ret = wc_InitSipHash(&siphash, NULL, SIPHASH_MAC_SIZE_8);
  31275. if (ret != BAD_FUNC_ARG)
  31276. return -13302;
  31277. ret = wc_InitSipHash(&siphash, siphash_key, 7);
  31278. if (ret != BAD_FUNC_ARG)
  31279. return -13303;
  31280. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  31281. if (ret != 0)
  31282. return -13304;
  31283. ret = wc_SipHashUpdate(NULL, NULL, 0);
  31284. if (ret != BAD_FUNC_ARG)
  31285. return -13305;
  31286. ret = wc_SipHashUpdate(&siphash, NULL, 1);
  31287. if (ret != BAD_FUNC_ARG)
  31288. return -13306;
  31289. ret = wc_SipHashFinal(NULL, NULL, SIPHASH_MAC_SIZE_8);
  31290. if (ret != BAD_FUNC_ARG)
  31291. return -13307;
  31292. ret = wc_SipHashFinal(&siphash, NULL, SIPHASH_MAC_SIZE_8);
  31293. if (ret != BAD_FUNC_ARG)
  31294. return -13308;
  31295. ret = wc_SipHashFinal(NULL, res, SIPHASH_MAC_SIZE_8);
  31296. if (ret != BAD_FUNC_ARG)
  31297. return -13309;
  31298. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_16);
  31299. if (ret != BAD_FUNC_ARG)
  31300. return -13310;
  31301. ret = wc_SipHash(NULL, NULL, 0, NULL, SIPHASH_MAC_SIZE_16);
  31302. if (ret != BAD_FUNC_ARG)
  31303. return -13311;
  31304. ret = wc_SipHash(siphash_key, NULL, 0, NULL, SIPHASH_MAC_SIZE_16);
  31305. if (ret != BAD_FUNC_ARG)
  31306. return -13312;
  31307. ret = wc_SipHash(NULL, NULL, 0, res, SIPHASH_MAC_SIZE_16);
  31308. if (ret != BAD_FUNC_ARG)
  31309. return -13313;
  31310. ret = wc_SipHash(siphash_key, NULL, 0, res, 15);
  31311. if (ret != BAD_FUNC_ARG)
  31312. return -13314;
  31313. ret = wc_SipHash(siphash_key, NULL, 1, res, SIPHASH_MAC_SIZE_16);
  31314. if (ret != BAD_FUNC_ARG)
  31315. return -13315;
  31316. return 0;
  31317. }
  31318. #endif /* WOLFSSL_SIPHASH */
  31319. #ifdef HAVE_LIBZ
  31320. static const byte sample_text[] =
  31321. "Biodiesel cupidatat marfa, cliche aute put a bird on it incididunt elit\n"
  31322. "polaroid. Sunt tattooed bespoke reprehenderit. Sint twee organic id\n"
  31323. "marfa. Commodo veniam ad esse gastropub. 3 wolf moon sartorial vero,\n"
  31324. "plaid delectus biodiesel squid +1 vice. Post-ironic keffiyeh leggings\n"
  31325. "selfies cray fap hoodie, forage anim. Carles cupidatat shoreditch, VHS\n"
  31326. "small batch meggings kogi dolore food truck bespoke gastropub.\n"
  31327. "\n"
  31328. "Terry richardson adipisicing actually typewriter tumblr, twee whatever\n"
  31329. "four loko you probably haven't heard of them high life. Messenger bag\n"
  31330. "whatever tattooed deep v mlkshk. Brooklyn pinterest assumenda chillwave\n"
  31331. "et, banksy ullamco messenger bag umami pariatur direct trade forage.\n"
  31332. "Typewriter culpa try-hard, pariatur sint brooklyn meggings. Gentrify\n"
  31333. "food truck next level, tousled irony non semiotics PBR ethical anim cred\n"
  31334. "readymade. Mumblecore brunch lomo odd future, portland organic terry\n"
  31335. "richardson elit leggings adipisicing ennui raw denim banjo hella. Godard\n"
  31336. "mixtape polaroid, pork belly readymade organic cray typewriter helvetica\n"
  31337. "four loko whatever street art yr farm-to-table.\n"
  31338. "\n"
  31339. "Vinyl keytar vice tofu. Locavore you probably haven't heard of them pug\n"
  31340. "pickled, hella tonx labore truffaut DIY mlkshk elit cosby sweater sint\n"
  31341. "et mumblecore. Elit swag semiotics, reprehenderit DIY sartorial nisi ugh\n"
  31342. "nesciunt pug pork belly wayfarers selfies delectus. Ethical hoodie\n"
  31343. "seitan fingerstache kale chips. Terry richardson artisan williamsburg,\n"
  31344. "eiusmod fanny pack irony tonx ennui lo-fi incididunt tofu YOLO\n"
  31345. "readymade. 8-bit sed ethnic beard officia. Pour-over iphone DIY butcher,\n"
  31346. "ethnic art party qui letterpress nisi proident jean shorts mlkshk\n"
  31347. "locavore.\n"
  31348. "\n"
  31349. "Narwhal flexitarian letterpress, do gluten-free voluptate next level\n"
  31350. "banh mi tonx incididunt carles DIY. Odd future nulla 8-bit beard ut\n"
  31351. "cillum pickled velit, YOLO officia you probably haven't heard of them\n"
  31352. "trust fund gastropub. Nisi adipisicing tattooed, Austin mlkshk 90's\n"
  31353. "small batch american apparel. Put a bird on it cosby sweater before they\n"
  31354. "sold out pork belly kogi hella. Street art mollit sustainable polaroid,\n"
  31355. "DIY ethnic ea pug beard dreamcatcher cosby sweater magna scenester nisi.\n"
  31356. "Sed pork belly skateboard mollit, labore proident eiusmod. Sriracha\n"
  31357. "excepteur cosby sweater, anim deserunt laborum eu aliquip ethical et\n"
  31358. "neutra PBR selvage.\n"
  31359. "\n"
  31360. "Raw denim pork belly truffaut, irony plaid sustainable put a bird on it\n"
  31361. "next level jean shorts exercitation. Hashtag keytar whatever, nihil\n"
  31362. "authentic aliquip disrupt laborum. Tattooed selfies deserunt trust fund\n"
  31363. "wayfarers. 3 wolf moon synth church-key sartorial, gastropub leggings\n"
  31364. "tattooed. Labore high life commodo, meggings raw denim fingerstache pug\n"
  31365. "trust fund leggings seitan forage. Nostrud ullamco duis, reprehenderit\n"
  31366. "incididunt flannel sustainable helvetica pork belly pug banksy you\n"
  31367. "probably haven't heard of them nesciunt farm-to-table. Disrupt nostrud\n"
  31368. "mollit magna, sriracha sartorial helvetica.\n"
  31369. "\n"
  31370. "Nulla kogi reprehenderit, skateboard sustainable duis adipisicing viral\n"
  31371. "ad fanny pack salvia. Fanny pack trust fund you probably haven't heard\n"
  31372. "of them YOLO vice nihil. Keffiyeh cray lo-fi pinterest cardigan aliqua,\n"
  31373. "reprehenderit aute. Culpa tousled williamsburg, marfa lomo actually anim\n"
  31374. "skateboard. Iphone aliqua ugh, semiotics pariatur vero readymade\n"
  31375. "organic. Marfa squid nulla, in laborum disrupt laboris irure gastropub.\n"
  31376. "Veniam sunt food truck leggings, sint vinyl fap.\n"
  31377. "\n"
  31378. "Hella dolore pork belly, truffaut carles you probably haven't heard of\n"
  31379. "them PBR helvetica in sapiente. Fashion axe ugh bushwick american\n"
  31380. "apparel. Fingerstache sed iphone, jean shorts blue bottle nisi bushwick\n"
  31381. "flexitarian officia veniam plaid bespoke fap YOLO lo-fi. Blog\n"
  31382. "letterpress mumblecore, food truck id cray brooklyn cillum ad sed.\n"
  31383. "Assumenda chambray wayfarers vinyl mixtape sustainable. VHS vinyl\n"
  31384. "delectus, culpa williamsburg polaroid cliche swag church-key synth kogi\n"
  31385. "magna pop-up literally. Swag thundercats ennui shoreditch vegan\n"
  31386. "pitchfork neutra truffaut etsy, sed single-origin coffee craft beer.\n"
  31387. "\n"
  31388. "Odio letterpress brooklyn elit. Nulla single-origin coffee in occaecat\n"
  31389. "meggings. Irony meggings 8-bit, chillwave lo-fi adipisicing cred\n"
  31390. "dreamcatcher veniam. Put a bird on it irony umami, trust fund bushwick\n"
  31391. "locavore kale chips. Sriracha swag thundercats, chillwave disrupt\n"
  31392. "tousled beard mollit mustache leggings portland next level. Nihil esse\n"
  31393. "est, skateboard art party etsy thundercats sed dreamcatcher ut iphone\n"
  31394. "swag consectetur et. Irure skateboard banjo, nulla deserunt messenger\n"
  31395. "bag dolor terry richardson sapiente.\n";
  31396. static const byte sample_text_gz[] = {
  31397. 0x1F, 0x8B, 0x08, 0x08, 0xC5, 0x49, 0xB5, 0x5B, 0x00, 0x03, 0x63, 0x69, 0x70,
  31398. 0x68, 0x65, 0x72, 0x74, 0x65, 0x78, 0x74, 0x2E, 0x74, 0x78, 0x74, 0x00, 0x8D,
  31399. 0x58, 0xCB, 0x92, 0xE4, 0xB6, 0x11, 0xBC, 0xE3, 0x2B, 0xEA, 0xA6, 0x83, 0xD9,
  31400. 0x1D, 0x72, 0xF8, 0x22, 0x1F, 0xB5, 0x96, 0xA5, 0xDD, 0x90, 0xBC, 0xAB, 0xD0,
  31401. 0x28, 0x36, 0x42, 0x47, 0x90, 0x2C, 0x36, 0xA1, 0x06, 0x09, 0x0A, 0x8F, 0xEE,
  31402. 0xE1, 0xDF, 0x3B, 0x0B, 0xE0, 0x73, 0x2C, 0x4B, 0xBA, 0xCD, 0xCE, 0x80, 0x78,
  31403. 0x64, 0x65, 0x65, 0x66, 0xED, 0x3B, 0xE3, 0x5A, 0xC3, 0x81, 0x2D, 0x35, 0x69,
  31404. 0x32, 0xAD, 0x8E, 0x3A, 0xD2, 0xA0, 0x7D, 0xA7, 0x2B, 0x6A, 0xAC, 0x69, 0x7A,
  31405. 0x26, 0x9D, 0x22, 0xD3, 0x94, 0x22, 0x69, 0xAA, 0x8D, 0x6F, 0xC9, 0x8D, 0x64,
  31406. 0x22, 0x99, 0xB1, 0x31, 0xAD, 0x69, 0xD3, 0x18, 0x89, 0xAD, 0x89, 0x6A, 0x72,
  31407. 0x56, 0x7B, 0x67, 0xDA, 0x2B, 0xBD, 0xC8, 0xEF, 0xB0, 0x4D, 0x74, 0x8E, 0x5B,
  31408. 0xAA, 0x39, 0x4C, 0xEE, 0xCE, 0xE4, 0x79, 0xF2, 0xDC, 0xF3, 0xD8, 0xB2, 0x37,
  31409. 0x11, 0x8B, 0x8C, 0x2C, 0x7A, 0x32, 0x93, 0xF3, 0x37, 0x3D, 0x9A, 0x86, 0x4C,
  31410. 0xAB, 0xF2, 0xB9, 0x57, 0xFA, 0x97, 0x1B, 0x06, 0xD7, 0x3A, 0x7A, 0xF0, 0x68,
  31411. 0xF4, 0x40, 0xBA, 0x25, 0x0E, 0x81, 0xE9, 0xA6, 0x43, 0xF4, 0x6E, 0x4A, 0xF5,
  31412. 0x95, 0xFE, 0x41, 0x4F, 0x67, 0x3B, 0x1A, 0x1C, 0xEE, 0x12, 0xB4, 0x8F, 0xCE,
  31413. 0x1B, 0x6D, 0xB1, 0xDE, 0xBB, 0x4A, 0x4D, 0x56, 0x9B, 0x96, 0x5A, 0xB6, 0xDC,
  31414. 0xC4, 0x14, 0x70, 0xE5, 0xF5, 0x7D, 0xE1, 0xB7, 0x84, 0x3F, 0xFC, 0xED, 0xEF,
  31415. 0xF4, 0x30, 0x0D, 0x5F, 0xE9, 0x47, 0x17, 0xE2, 0xC5, 0x78, 0x27, 0x67, 0xDF,
  31416. 0xB9, 0xEB, 0xCC, 0xCC, 0x3D, 0x59, 0xBE, 0xDD, 0xCC, 0x78, 0x0B, 0x0A, 0x1F,
  31417. 0x74, 0xF8, 0x8C, 0x1A, 0xAF, 0x67, 0xEA, 0xF4, 0x44, 0xBD, 0x93, 0x7D, 0x2A,
  31418. 0xEA, 0x9C, 0xD7, 0x37, 0x80, 0x32, 0x9A, 0x01, 0x37, 0xD5, 0xDE, 0xCA, 0xA2,
  31419. 0x0D, 0xB9, 0xD0, 0x3B, 0xCF, 0xAD, 0x89, 0x4D, 0x5F, 0xD1, 0xE7, 0xF7, 0x2F,
  31420. 0x2A, 0x0C, 0xDA, 0x5A, 0xAA, 0x35, 0x7E, 0x41, 0xC3, 0xB2, 0x37, 0xDD, 0xDD,
  31421. 0xCD, 0x50, 0xEB, 0x2C, 0x96, 0x62, 0x3B, 0xD7, 0x52, 0xF4, 0xA9, 0xB9, 0x6F,
  31422. 0x48, 0xED, 0xEF, 0x54, 0xEA, 0x67, 0xF6, 0x7E, 0x26, 0x8F, 0x3A, 0x68, 0xDF,
  31423. 0x06, 0xBC, 0x56, 0xB7, 0x66, 0x32, 0xC1, 0x34, 0xD8, 0x88, 0x34, 0x1E, 0x88,
  31424. 0xED, 0x67, 0x8A, 0xF3, 0xC4, 0x4F, 0xC0, 0xCA, 0x9E, 0x62, 0x1A, 0x6A, 0xEB,
  31425. 0xAB, 0x02, 0xED, 0xB3, 0xD7, 0x91, 0x81, 0x8A, 0xEA, 0x5C, 0xF2, 0x64, 0xDD,
  31426. 0xDD, 0xD1, 0xEC, 0x12, 0x4D, 0xDE, 0xD5, 0xBA, 0xC6, 0x77, 0xBD, 0x06, 0xC4,
  31427. 0x5F, 0x44, 0xEA, 0x59, 0x4B, 0x5D, 0x3B, 0x8A, 0x3D, 0x0F, 0xD4, 0x9B, 0x1B,
  31428. 0x80, 0x30, 0x1D, 0x30, 0xFA, 0x8F, 0x00, 0x3F, 0xDE, 0xB0, 0x6F, 0xAD, 0x6F,
  31429. 0x6A, 0xDD, 0x6E, 0x2F, 0x6E, 0xCB, 0x3C, 0xD1, 0x83, 0x06, 0x7B, 0x0F, 0xFD,
  31430. 0xFD, 0x4A, 0xEF, 0xBC, 0x73, 0x77, 0x3B, 0x8F, 0x34, 0xA1, 0xBA, 0xEC, 0x39,
  31431. 0x80, 0x33, 0x21, 0xA4, 0x01, 0x55, 0xD7, 0xD4, 0xF4, 0xC6, 0xDA, 0x27, 0x4E,
  31432. 0x54, 0x1C, 0x2B, 0xEC, 0x37, 0xDE, 0xC3, 0x4C, 0xC9, 0x5A, 0x3D, 0x34, 0x0E,
  31433. 0xD8, 0x1C, 0x0E, 0xA2, 0x34, 0xE8, 0xC1, 0xD0, 0xA4, 0x51, 0xD5, 0x88, 0x8B,
  31434. 0xB7, 0xC6, 0xA3, 0x96, 0x40, 0x49, 0xB7, 0xBC, 0xE0, 0x7F, 0x55, 0x3F, 0xEF,
  31435. 0x6F, 0x6E, 0x92, 0x9D, 0x34, 0xFE, 0x3C, 0x5F, 0x04, 0xA5, 0x6A, 0xFF, 0x30,
  31436. 0x08, 0xC9, 0xEA, 0xF5, 0x52, 0x2B, 0xFE, 0x57, 0xFA, 0x8E, 0xC7, 0xE8, 0x4D,
  31437. 0x37, 0xAB, 0x03, 0xFA, 0x23, 0xBF, 0x46, 0x94, 0xFF, 0xC1, 0x16, 0xE0, 0xB9,
  31438. 0x14, 0x2C, 0x9E, 0x27, 0xEC, 0x98, 0x69, 0x14, 0x92, 0xF1, 0x60, 0x5C, 0x34,
  31439. 0x4D, 0xA0, 0x1F, 0xDF, 0xFD, 0x44, 0x1C, 0x7B, 0xD3, 0x80, 0x70, 0x42, 0x02,
  31440. 0x30, 0x84, 0x5B, 0xE5, 0x59, 0xB7, 0xF3, 0x80, 0xFB, 0x01, 0x33, 0xA9, 0x00,
  31441. 0x37, 0x52, 0xDC, 0xDA, 0xA7, 0x11, 0x85, 0xB7, 0x6E, 0x70, 0xE4, 0xDA, 0x96,
  31442. 0xBA, 0x84, 0x5B, 0x81, 0x43, 0x93, 0xF3, 0xD1, 0xEA, 0xB1, 0xDD, 0xB8, 0x1F,
  31443. 0xA5, 0xCC, 0xEA, 0x50, 0x66, 0x69, 0xA9, 0x8D, 0x8C, 0xA7, 0xA2, 0xF3, 0x38,
  31444. 0x26, 0x43, 0x5E, 0x3F, 0x01, 0xBE, 0x1C, 0x0F, 0x20, 0x7F, 0x75, 0xA8, 0x20,
  31445. 0x80, 0xC4, 0xC3, 0x5C, 0x8B, 0x0D, 0xD4, 0x60, 0x5E, 0xA3, 0x9E, 0xD0, 0xB4,
  31446. 0x4B, 0x4F, 0xE6, 0x13, 0x85, 0x60, 0x42, 0x96, 0xED, 0xAA, 0xDB, 0xE9, 0x99,
  31447. 0xE3, 0x07, 0x0E, 0x61, 0xB3, 0x07, 0xE3, 0xB1, 0xFA, 0xC0, 0x9B, 0xAD, 0xF6,
  31448. 0xE0, 0x26, 0x33, 0xEA, 0xEA, 0x23, 0xCD, 0x1E, 0x9D, 0xE1, 0x87, 0x4B, 0x74,
  31449. 0x97, 0x08, 0x3E, 0xA1, 0x28, 0xEA, 0xB3, 0x19, 0x67, 0x8B, 0x76, 0x9A, 0xA3,
  31450. 0xF6, 0xB9, 0xCF, 0x80, 0x65, 0x97, 0xAE, 0xF4, 0x83, 0x6B, 0xF4, 0x43, 0x20,
  31451. 0xF9, 0x0B, 0xFC, 0x9B, 0xD2, 0x4D, 0x4D, 0xA6, 0xB9, 0xA3, 0x02, 0x55, 0x79,
  31452. 0x18, 0x36, 0x19, 0x5F, 0xC9, 0xEA, 0x5A, 0x76, 0x40, 0xB9, 0xBA, 0x0E, 0x9A,
  31453. 0x44, 0xDF, 0x7C, 0xF8, 0x65, 0x61, 0x5E, 0x81, 0xAB, 0x71, 0xA1, 0x9E, 0x29,
  31454. 0x3C, 0x59, 0xCB, 0x23, 0xA4, 0xF6, 0x60, 0x1A, 0x0D, 0x5B, 0x39, 0xAE, 0xF4,
  31455. 0x6F, 0x59, 0x16, 0x9E, 0x60, 0xD8, 0x56, 0xCF, 0xEA, 0x2C, 0x4C, 0x79, 0xD3,
  31456. 0x5D, 0x51, 0x46, 0xA0, 0x4E, 0xE9, 0xD6, 0xAB, 0x91, 0x43, 0x63, 0x44, 0xD7,
  31457. 0x70, 0xB9, 0x23, 0x98, 0x4F, 0x3D, 0x03, 0x02, 0xF6, 0x81, 0x56, 0xC1, 0x58,
  31458. 0x85, 0x07, 0xA7, 0x2D, 0x2C, 0x29, 0xCA, 0x01, 0x45, 0x31, 0x51, 0x8F, 0xD4,
  31459. 0x19, 0xA1, 0x79, 0x88, 0x5A, 0xA4, 0xF5, 0xAE, 0x2D, 0x4B, 0x63, 0x4C, 0x58,
  31460. 0xFE, 0xBF, 0xAD, 0xEE, 0xA3, 0x09, 0xF8, 0xE2, 0x89, 0xBE, 0x81, 0x0E, 0x86,
  31461. 0x3A, 0xF9, 0x5B, 0xA5, 0xD8, 0xA4, 0x00, 0x75, 0x04, 0xF2, 0x23, 0xB8, 0x39,
  31462. 0x69, 0x50, 0xB7, 0xD0, 0x34, 0x63, 0x54, 0xD8, 0x61, 0xDD, 0xA5, 0x33, 0x47,
  31463. 0x85, 0x96, 0x22, 0xD0, 0x2F, 0x9F, 0x7E, 0xF8, 0x74, 0x24, 0xEA, 0x57, 0x97,
  31464. 0x5A, 0xE0, 0x00, 0xCF, 0xC1, 0x67, 0xE1, 0x41, 0xBD, 0x94, 0xA1, 0x03, 0xD3,
  31465. 0xB4, 0x08, 0x64, 0xF2, 0x17, 0x27, 0x35, 0x37, 0x53, 0xEF, 0x46, 0xCE, 0xD8,
  31466. 0xD4, 0x09, 0x52, 0xC6, 0x1E, 0xF7, 0x28, 0xDF, 0x08, 0x0F, 0xD0, 0x6F, 0x71,
  31467. 0xA6, 0xDF, 0xE4, 0x60, 0x8E, 0xC0, 0x1E, 0x78, 0x86, 0x50, 0xB0, 0x9B, 0x84,
  31468. 0x7E, 0xE8, 0x36, 0xFA, 0x95, 0xF1, 0x12, 0x51, 0xC7, 0x18, 0x96, 0xA2, 0x29,
  31469. 0xBB, 0x70, 0x02, 0xB4, 0xF9, 0xA8, 0x3D, 0x08, 0x66, 0xA9, 0xB3, 0xFC, 0x0A,
  31470. 0x94, 0x80, 0xFD, 0x78, 0xDC, 0xAB, 0x82, 0x5A, 0xD2, 0xCD, 0xC2, 0x87, 0xC6,
  31471. 0x4B, 0x07, 0xFA, 0xD1, 0xC3, 0xD9, 0x34, 0x41, 0x85, 0xF8, 0xD0, 0xB6, 0x0A,
  31472. 0x9D, 0x00, 0x91, 0x35, 0x05, 0x88, 0xC3, 0xE3, 0x9B, 0x22, 0xD2, 0xB8, 0xFD,
  31473. 0x95, 0x3E, 0x6D, 0x5D, 0x48, 0xA3, 0x68, 0xCF, 0x02, 0x42, 0x79, 0x79, 0x8A,
  31474. 0xAA, 0x01, 0xD6, 0x09, 0x14, 0x2C, 0xF4, 0x83, 0xA3, 0x80, 0x31, 0x55, 0x46,
  31475. 0x6E, 0xC5, 0xE5, 0x2F, 0x30, 0x58, 0x81, 0xA2, 0x90, 0xBE, 0x2E, 0xA1, 0xC3,
  31476. 0x0F, 0xA6, 0xF5, 0x51, 0x00, 0x39, 0xB6, 0xF2, 0x2A, 0xA3, 0x15, 0x7D, 0x8D,
  31477. 0xF5, 0x66, 0x5C, 0xD9, 0xFC, 0xCF, 0x2F, 0xBF, 0x08, 0x27, 0xE7, 0xD0, 0x03,
  31478. 0xB8, 0xD9, 0x00, 0x13, 0x3D, 0x01, 0x6B, 0xB6, 0xA8, 0xCD, 0x5B, 0x3B, 0x3E,
  31479. 0x93, 0xBF, 0xE6, 0x2E, 0xB7, 0x4A, 0xCF, 0xB3, 0x0A, 0xCE, 0x62, 0x11, 0xD6,
  31480. 0x1F, 0x68, 0x9B, 0x1D, 0x68, 0xD1, 0x8C, 0x97, 0xBD, 0xA1, 0x07, 0x67, 0x73,
  31481. 0x87, 0xE0, 0x36, 0xDA, 0x8C, 0xD2, 0xD2, 0xBB, 0x84, 0x28, 0xA9, 0xFE, 0x52,
  31482. 0x74, 0xD6, 0xB9, 0x0F, 0x0A, 0x6A, 0x2D, 0x28, 0x35, 0x34, 0x3A, 0xD3, 0xE2,
  31483. 0xCD, 0x35, 0x06, 0x7D, 0x1B, 0x35, 0x85, 0x86, 0xD1, 0x3E, 0xF2, 0x6F, 0xA1,
  31484. 0xC4, 0x55, 0xBD, 0x00, 0xD8, 0xC3, 0x5D, 0xC2, 0x1D, 0x6B, 0x6B, 0x27, 0x5B,
  31485. 0x95, 0xF3, 0xAB, 0xB5, 0xD3, 0x37, 0xF2, 0x2C, 0x9C, 0xC7, 0x5D, 0xBD, 0xF1,
  31486. 0x68, 0x1C, 0xAD, 0xF8, 0xB5, 0xE1, 0x29, 0x72, 0x7A, 0x73, 0x62, 0x55, 0x24,
  31487. 0xB9, 0x85, 0xDF, 0x7B, 0x29, 0x7D, 0xDE, 0x08, 0xF5, 0xE4, 0x44, 0xDA, 0x1A,
  31488. 0x30, 0x74, 0xDA, 0xB4, 0x9B, 0x23, 0x9A, 0x3A, 0xC1, 0x53, 0xB2, 0xA2, 0xA3,
  31489. 0x7B, 0x1F, 0xD9, 0x56, 0xD4, 0x4F, 0x9B, 0xB2, 0x1E, 0xEE, 0xB8, 0x6A, 0x4E,
  31490. 0xB5, 0xF4, 0x5A, 0xC9, 0x18, 0x27, 0x9C, 0xDE, 0x14, 0x44, 0xED, 0xC4, 0x3C,
  31491. 0x71, 0x9F, 0x5F, 0xD9, 0x37, 0xA0, 0x78, 0x34, 0x6E, 0xBC, 0xD2, 0x7B, 0x1D,
  31492. 0xFA, 0x08, 0x39, 0x5A, 0x04, 0x73, 0x15, 0xD9, 0x0A, 0x48, 0xC1, 0x2D, 0x15,
  31493. 0x4E, 0x84, 0x30, 0x45, 0x69, 0xB3, 0xE5, 0xF6, 0xAD, 0x09, 0x1E, 0xCC, 0x5F,
  31494. 0x1F, 0x06, 0xD5, 0x58, 0xAD, 0x78, 0xD7, 0x9F, 0xE5, 0xED, 0x3B, 0x09, 0xD5,
  31495. 0xA6, 0x52, 0x6F, 0x92, 0xD3, 0x3C, 0xC6, 0x1E, 0xF2, 0x93, 0x7C, 0xD3, 0x5F,
  31496. 0x70, 0x85, 0x5D, 0xF8, 0xAA, 0x9D, 0xB7, 0x7B, 0x24, 0x5A, 0xE9, 0x0A, 0x35,
  31497. 0x2F, 0xF5, 0xD9, 0x82, 0x02, 0x8A, 0x90, 0x13, 0x5B, 0xB5, 0x67, 0x9C, 0xDD,
  31498. 0xA0, 0x4E, 0x82, 0x27, 0xDA, 0x7E, 0xE8, 0x8E, 0xCD, 0xE1, 0x56, 0x71, 0x2C,
  31499. 0xE6, 0x4E, 0x1F, 0x91, 0xCD, 0x7C, 0x6A, 0xB7, 0x78, 0xD0, 0x26, 0xF3, 0x56,
  31500. 0xA9, 0xD5, 0xA1, 0xC3, 0x3B, 0x98, 0xE9, 0x28, 0x09, 0xEF, 0x50, 0x90, 0xCD,
  31501. 0xC4, 0x8E, 0x75, 0xCC, 0xAC, 0x2D, 0xC9, 0x03, 0x6D, 0xAC, 0xFE, 0xC4, 0x88,
  31502. 0x36, 0xD1, 0x3F, 0xBB, 0x1C, 0x7D, 0xB3, 0x14, 0x61, 0x2C, 0xB7, 0x54, 0x4B,
  31503. 0xDB, 0x64, 0xB6, 0x57, 0x14, 0x16, 0x8E, 0x1E, 0x6C, 0x64, 0xBB, 0x8B, 0x48,
  31504. 0x5D, 0x96, 0x9D, 0xDC, 0x80, 0xA7, 0xF7, 0x54, 0xC7, 0x46, 0x38, 0x3E, 0x44,
  31505. 0xDE, 0x7E, 0x92, 0x8D, 0x07, 0xF6, 0x07, 0x37, 0x4E, 0x16, 0x10, 0xB4, 0x7D,
  31506. 0x88, 0x66, 0x7F, 0xBB, 0xFF, 0xEA, 0x00, 0xF3, 0xFF, 0x97, 0x2C, 0xB5, 0xBE,
  31507. 0x35, 0x4B, 0x5C, 0x36, 0xEC, 0x4C, 0xBD, 0x2B, 0x7D, 0xBF, 0x46, 0xE2, 0x9C,
  31508. 0x0E, 0x8A, 0xA3, 0xEC, 0xB1, 0x0E, 0x9A, 0xDA, 0x9A, 0x9B, 0x28, 0x92, 0x10,
  31509. 0x53, 0x57, 0xEA, 0xEC, 0xA2, 0x32, 0x32, 0x20, 0x1D, 0x97, 0x5C, 0xB6, 0x84,
  31510. 0xA9, 0x93, 0x8D, 0x95, 0x11, 0xA3, 0x24, 0xA3, 0x2D, 0xC6, 0x4A, 0xEF, 0xAA,
  31511. 0x1D, 0x85, 0x2B, 0x7D, 0x28, 0xBE, 0x53, 0xCE, 0x10, 0x1F, 0xAE, 0x0E, 0x41,
  31512. 0x6C, 0x4B, 0x79, 0x12, 0xFB, 0xF7, 0x54, 0xA3, 0x96, 0x54, 0x83, 0x20, 0x96,
  31513. 0x8F, 0x28, 0xA9, 0x3F, 0x8B, 0x3D, 0xBA, 0x77, 0xDC, 0x24, 0xE1, 0xD4, 0x49,
  31514. 0x40, 0xD8, 0x78, 0x31, 0x85, 0x43, 0xF6, 0xFE, 0x5C, 0xA6, 0x8F, 0x90, 0x09,
  31515. 0xB0, 0xE7, 0xC4, 0x95, 0xB2, 0x55, 0x49, 0x97, 0x8F, 0x1C, 0x78, 0x30, 0x20,
  31516. 0xA0, 0xB4, 0xEF, 0x73, 0x56, 0x59, 0x82, 0xFD, 0xCE, 0xBA, 0x6A, 0x8F, 0x2C,
  31517. 0x8B, 0x15, 0xFD, 0xA1, 0x85, 0xA8, 0x5C, 0x0F, 0x11, 0xA5, 0x9D, 0xC2, 0x46,
  31518. 0xC6, 0x9C, 0xC9, 0x40, 0x0B, 0x58, 0x6A, 0x1C, 0x7A, 0x23, 0xF9, 0xE0, 0x95,
  31519. 0x05, 0x13, 0x58, 0x72, 0xE8, 0x9F, 0x30, 0xAC, 0xCD, 0x26, 0xD4, 0x66, 0x13,
  31520. 0xDF, 0x1E, 0x7B, 0x4F, 0x9C, 0xBE, 0x38, 0x79, 0x75, 0x92, 0xA4, 0xDA, 0x26,
  31521. 0x44, 0x55, 0x17, 0xA3, 0xE5, 0x62, 0xDA, 0xEB, 0x86, 0xEA, 0x68, 0xC7, 0xAB,
  31522. 0xFD, 0x2D, 0x43, 0x59, 0x51, 0xC0, 0x75, 0x64, 0x91, 0x01, 0x29, 0x33, 0x28,
  31523. 0xF3, 0x04, 0x83, 0x80, 0x75, 0x37, 0x75, 0x0C, 0x03, 0x7B, 0x0A, 0xAB, 0x8E,
  31524. 0x60, 0x62, 0x8B, 0x4C, 0xAF, 0x2D, 0xA3, 0x2F, 0xFE, 0xAB, 0x45, 0xCF, 0xDA,
  31525. 0xAB, 0xFA, 0xFA, 0x30, 0x3D, 0xE8, 0xA1, 0x96, 0xA5, 0x7B, 0xE2, 0x2A, 0xD0,
  31526. 0xAF, 0x59, 0xF7, 0xD0, 0x32, 0x57, 0x19, 0xBD, 0xCA, 0x9F, 0xD5, 0x1A, 0xC7,
  31527. 0xAA, 0x65, 0x4A, 0x38, 0xB2, 0x70, 0x33, 0xB7, 0x75, 0xD2, 0xCD, 0xD1, 0xF0,
  31528. 0xA8, 0x87, 0x59, 0x20, 0xA5, 0x57, 0x55, 0xB1, 0xB2, 0xC9, 0x4D, 0x97, 0x34,
  31529. 0x41, 0xF3, 0xF0, 0x30, 0xA1, 0x2C, 0x1C, 0x49, 0x3E, 0x89, 0x7D, 0x12, 0xE2,
  31530. 0xC3, 0x04, 0xC3, 0x92, 0xC0, 0xF6, 0x39, 0x10, 0x80, 0x81, 0x8F, 0x08, 0xB4,
  31531. 0xF8, 0xB9, 0x13, 0x4E, 0x2C, 0xAE, 0xB3, 0x71, 0x82, 0x63, 0x98, 0xAB, 0x5C,
  31532. 0x1C, 0x10, 0xEA, 0x66, 0xF9, 0x02, 0x3A, 0x82, 0x61, 0xD0, 0xD4, 0xAE, 0x43,
  31533. 0xD4, 0x01, 0x3E, 0x9D, 0x04, 0x14, 0xF6, 0x60, 0xD8, 0xA7, 0xD6, 0xB8, 0x53,
  31534. 0xC8, 0xDA, 0x80, 0x93, 0xA0, 0x02, 0xDD, 0xCC, 0xE2, 0xF2, 0xBB, 0xFB, 0xE0,
  31535. 0x27, 0xD7, 0x34, 0x9A, 0x71, 0x49, 0xB5, 0x4F, 0x42, 0x1F, 0xB2, 0x9D, 0x6D,
  31536. 0xAA, 0x9D, 0xD3, 0x50, 0xB5, 0x8F, 0x6A, 0x4B, 0xDF, 0x1F, 0xD5, 0x27, 0x8F,
  31537. 0x3B, 0x27, 0xCF, 0x2F, 0x8C, 0xF8, 0x9D, 0x4C, 0x52, 0xBC, 0x32, 0x0F, 0x73,
  31538. 0xD5, 0x51, 0x8E, 0x36, 0x7E, 0xAD, 0x09, 0xF0, 0x94, 0x83, 0x5F, 0x36, 0xFD,
  31539. 0x7C, 0x03, 0xED, 0xF1, 0x5E, 0x4B, 0xF7, 0xAA, 0x55, 0x5C, 0x4A, 0x14, 0x59,
  31540. 0x85, 0x38, 0x2D, 0x8C, 0xDF, 0xEC, 0x65, 0x1B, 0xB8, 0x76, 0x57, 0x96, 0x3C,
  31541. 0x86, 0xED, 0xF2, 0x7F, 0x2D, 0x28, 0x48, 0xDA, 0x49, 0x7F, 0xF7, 0x54, 0x2B,
  31542. 0xD5, 0x39, 0xD5, 0x57, 0x0A, 0x75, 0x7A, 0x3E, 0x5E, 0x5D, 0xBA, 0x4A, 0x15,
  31543. 0xFA, 0xB8, 0x31, 0x80, 0x71, 0x2C, 0xCA, 0xC4, 0x51, 0x10, 0x16, 0x5D, 0x39,
  31544. 0xEC, 0x9D, 0x07, 0xB6, 0x6A, 0x89, 0x9F, 0x9B, 0x5B, 0x6F, 0x03, 0xB0, 0x92,
  31545. 0x01, 0x38, 0x6B, 0x48, 0x99, 0x0A, 0x8F, 0x13, 0xC1, 0xA6, 0x01, 0xEA, 0xBF,
  31546. 0x6F, 0x86, 0x43, 0x51, 0xB6, 0x11, 0x00, 0x00
  31547. };
  31548. WOLFSSL_TEST_SUBROUTINE int compress_test(void)
  31549. {
  31550. int ret = 0;
  31551. word32 dSz = sizeof(sample_text);
  31552. word32 cSz = (dSz + (word32)(dSz * 0.001) + 12);
  31553. byte *c;
  31554. byte *d;
  31555. c = (byte *)XMALLOC(cSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31556. d = (byte *)XMALLOC(dSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31557. if (c == NULL || d == NULL) {
  31558. ERROR_OUT(-12100, exit);
  31559. }
  31560. /* follow calloc and initialize to 0 */
  31561. XMEMSET(c, 0, cSz);
  31562. XMEMSET(d, 0, dSz);
  31563. if ((ret = wc_Compress(c, cSz, sample_text, dSz, 0)) < 0) {
  31564. ERROR_OUT(-12101, exit);
  31565. }
  31566. cSz = (word32)ret;
  31567. if ((ret = wc_DeCompress(d, dSz, c, cSz)) != (int)dSz) {
  31568. ERROR_OUT(-12102, exit);
  31569. }
  31570. dSz = (word32)ret;
  31571. if (XMEMCMP(d, sample_text, dSz) != 0) {
  31572. ERROR_OUT(-12103, exit);
  31573. }
  31574. /* GZIP tests */
  31575. cSz = (dSz + (word32)(dSz * 0.001) + 12); /* reset cSz */
  31576. XMEMSET(c, 0, cSz);
  31577. XMEMSET(d, 0, dSz);
  31578. ret = wc_Compress_ex(c, cSz, sample_text, dSz, 0, LIBZ_WINBITS_GZIP);
  31579. if (ret < 0) {
  31580. ERROR_OUT(-12104, exit);
  31581. }
  31582. cSz = (word32)ret;
  31583. ret = wc_DeCompress_ex(d, dSz, c, cSz, LIBZ_WINBITS_GZIP);
  31584. if (ret < 0) {
  31585. ERROR_OUT(-12105, exit);
  31586. }
  31587. if (XMEMCMP(d, sample_text, dSz) != 0) {
  31588. ERROR_OUT(-12106, exit);
  31589. }
  31590. /* Try with gzip generated output */
  31591. XMEMSET(d, 0, dSz);
  31592. ret = wc_DeCompress_ex(d, dSz, sample_text_gz, sizeof(sample_text_gz),
  31593. LIBZ_WINBITS_GZIP);
  31594. if (ret < 0) {
  31595. ERROR_OUT(-12107, exit);
  31596. }
  31597. dSz = (word32)ret;
  31598. if (XMEMCMP(d, sample_text, dSz) != 0) {
  31599. ERROR_OUT(-12108, exit);
  31600. }
  31601. ret = 0; /* success */
  31602. exit:
  31603. if (c) XFREE(c, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31604. if (d) XFREE(d, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31605. return ret;
  31606. }
  31607. #endif /* HAVE_LIBZ */
  31608. #ifdef HAVE_PKCS7
  31609. /* External Debugging/Testing Note:
  31610. *
  31611. * PKCS#7 test functions can output generated PKCS#7/CMS bundles for
  31612. * additional testing. To dump bundles to files DER encoded files, please
  31613. * define:
  31614. *
  31615. * #define PKCS7_OUTPUT_TEST_BUNDLES
  31616. */
  31617. /* Loads certs and keys for use with PKCS7 tests, from either files
  31618. * or buffers.
  31619. *
  31620. * rsaClientCertBuf - output buffer for RSA client cert
  31621. * rsaClientCertBufSz - IN/OUT size of output buffer, size of RSA client cert
  31622. * rsaClientPrivKeyBuf - output buffer for RSA client private key
  31623. * rsaClientPrivKeyBufSz - IN/OUT size of output buffer, size of RSA client key
  31624. *
  31625. * rsaServerCertBuf - output buffer for RSA server cert
  31626. * rsaServerCertBufSz - IN/OUT size of output buffer, size of RSA server cert
  31627. * rsaServerPrivKeyBuf - output buffer for RSA server private key
  31628. * rsaServerPrivKeyBufSz - IN/OUT size of output buffer, size of RSA server key
  31629. *
  31630. * rsaCaCertBuf - output buffer for RSA CA cert
  31631. * rsaCaCertBufSz - IN/OUT size of output buffer, size of RSA ca cert
  31632. * rsaCaPrivKeyBuf - output buffer for RSA CA private key
  31633. * rsaCaPrivKeyBufSz - IN/OUT size of output buffer, size of RSA CA key
  31634. *
  31635. * eccClientCertBuf - output buffer for ECC cert
  31636. * eccClientCertBufSz - IN/OUT size of output buffer, size of ECC cert
  31637. * eccClientPrivKeyBuf - output buffer for ECC private key
  31638. * eccClientPrivKeyBufSz - IN/OUT size of output buffer, size of ECC private key
  31639. *
  31640. * Returns 0 on success, negative on error
  31641. */
  31642. static int pkcs7_load_certs_keys(
  31643. byte* rsaClientCertBuf, word32* rsaClientCertBufSz,
  31644. byte* rsaClientPrivKeyBuf, word32* rsaClientPrivKeyBufSz,
  31645. byte* rsaServerCertBuf, word32* rsaServerCertBufSz,
  31646. byte* rsaServerPrivKeyBuf, word32* rsaServerPrivKeyBufSz,
  31647. byte* rsaCaCertBuf, word32* rsaCaCertBufSz,
  31648. byte* rsaCaPrivKeyBuf, word32* rsaCaPrivKeyBufSz,
  31649. byte* eccClientCertBuf, word32* eccClientCertBufSz,
  31650. byte* eccClientPrivKeyBuf, word32* eccClientPrivKeyBufSz)
  31651. {
  31652. #ifndef NO_FILESYSTEM
  31653. XFILE certFile;
  31654. XFILE keyFile;
  31655. (void)certFile;
  31656. (void)keyFile;
  31657. #endif
  31658. #ifndef NO_RSA
  31659. if (rsaClientCertBuf == NULL || rsaClientCertBufSz == NULL ||
  31660. rsaClientPrivKeyBuf == NULL || rsaClientPrivKeyBufSz == NULL)
  31661. return BAD_FUNC_ARG;
  31662. #endif
  31663. #ifdef HAVE_ECC
  31664. if (eccClientCertBuf == NULL || eccClientCertBufSz == NULL ||
  31665. eccClientPrivKeyBuf == NULL || eccClientPrivKeyBufSz == NULL)
  31666. return BAD_FUNC_ARG;
  31667. #endif
  31668. /* RSA */
  31669. #ifndef NO_RSA
  31670. #ifdef USE_CERT_BUFFERS_1024
  31671. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_1024)
  31672. return -12110;
  31673. XMEMCPY(rsaClientCertBuf, client_cert_der_1024,
  31674. sizeof_client_cert_der_1024);
  31675. *rsaClientCertBufSz = sizeof_client_cert_der_1024;
  31676. if (rsaServerCertBuf != NULL) {
  31677. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_1024)
  31678. return -12111;
  31679. XMEMCPY(rsaServerCertBuf, server_cert_der_1024,
  31680. sizeof_server_cert_der_1024);
  31681. *rsaServerCertBufSz = sizeof_server_cert_der_1024;
  31682. }
  31683. if (rsaCaCertBuf != NULL) {
  31684. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_1024)
  31685. return -12112;
  31686. XMEMCPY(rsaCaCertBuf, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  31687. *rsaCaCertBufSz = sizeof_ca_cert_der_1024;
  31688. }
  31689. #elif defined(USE_CERT_BUFFERS_2048)
  31690. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_2048)
  31691. return -12113;
  31692. XMEMCPY(rsaClientCertBuf, client_cert_der_2048,
  31693. sizeof_client_cert_der_2048);
  31694. *rsaClientCertBufSz = sizeof_client_cert_der_2048;
  31695. if (rsaServerCertBuf != NULL) {
  31696. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_2048)
  31697. return -12114;
  31698. XMEMCPY(rsaServerCertBuf, server_cert_der_2048,
  31699. sizeof_server_cert_der_2048);
  31700. *rsaServerCertBufSz = sizeof_server_cert_der_2048;
  31701. }
  31702. if (rsaCaCertBuf != NULL) {
  31703. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_2048)
  31704. return -12115;
  31705. XMEMCPY(rsaCaCertBuf, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  31706. *rsaCaCertBufSz = sizeof_ca_cert_der_2048;
  31707. }
  31708. #else
  31709. certFile = XFOPEN(clientCert, "rb");
  31710. if (!certFile)
  31711. return -12116;
  31712. *rsaClientCertBufSz = (word32)XFREAD(rsaClientCertBuf, 1,
  31713. *rsaClientCertBufSz, certFile);
  31714. XFCLOSE(certFile);
  31715. if (rsaServerCertBuf != NULL) {
  31716. certFile = XFOPEN(rsaServerCertDerFile, "rb");
  31717. if (!certFile)
  31718. return -12117;
  31719. *rsaServerCertBufSz = (word32)XFREAD(rsaServerCertBuf, 1,
  31720. *rsaServerCertBufSz, certFile);
  31721. XFCLOSE(certFile);
  31722. }
  31723. if (rsaCaCertBuf != NULL) {
  31724. certFile = XFOPEN(rsaCaCertDerFile, "rb");
  31725. if (!certFile)
  31726. return -12118;
  31727. *rsaCaCertBufSz = (word32)XFREAD(rsaCaCertBuf, 1, *rsaCaCertBufSz,
  31728. certFile);
  31729. XFCLOSE(certFile);
  31730. }
  31731. #endif
  31732. #ifdef USE_CERT_BUFFERS_1024
  31733. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_1024)
  31734. return -12119;
  31735. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_1024,
  31736. sizeof_client_key_der_1024);
  31737. *rsaClientPrivKeyBufSz = sizeof_client_key_der_1024;
  31738. if (rsaServerPrivKeyBuf != NULL) {
  31739. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_1024)
  31740. return -12120;
  31741. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_1024,
  31742. sizeof_server_key_der_1024);
  31743. *rsaServerPrivKeyBufSz = sizeof_server_key_der_1024;
  31744. }
  31745. if (rsaCaPrivKeyBuf != NULL) {
  31746. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_1024)
  31747. return -12121;
  31748. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_1024, sizeof_ca_key_der_1024);
  31749. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_1024;
  31750. }
  31751. #elif defined(USE_CERT_BUFFERS_2048)
  31752. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_2048)
  31753. return -12122;
  31754. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_2048,
  31755. sizeof_client_key_der_2048);
  31756. *rsaClientPrivKeyBufSz = sizeof_client_key_der_2048;
  31757. if (rsaServerPrivKeyBuf != NULL) {
  31758. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_2048)
  31759. return -12123;
  31760. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_2048,
  31761. sizeof_server_key_der_2048);
  31762. *rsaServerPrivKeyBufSz = sizeof_server_key_der_2048;
  31763. }
  31764. if (rsaCaPrivKeyBuf != NULL) {
  31765. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_2048)
  31766. return -12124;
  31767. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_2048, sizeof_ca_key_der_2048);
  31768. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_2048;
  31769. }
  31770. #else
  31771. keyFile = XFOPEN(clientKey, "rb");
  31772. if (!keyFile)
  31773. return -12125;
  31774. *rsaClientPrivKeyBufSz = (word32)XFREAD(rsaClientPrivKeyBuf, 1,
  31775. *rsaClientPrivKeyBufSz, keyFile);
  31776. XFCLOSE(keyFile);
  31777. if (rsaServerPrivKeyBuf != NULL) {
  31778. keyFile = XFOPEN(rsaServerKeyDerFile, "rb");
  31779. if (!keyFile)
  31780. return -12126;
  31781. *rsaServerPrivKeyBufSz = (word32)XFREAD(rsaServerPrivKeyBuf, 1,
  31782. *rsaServerPrivKeyBufSz, keyFile);
  31783. XFCLOSE(keyFile);
  31784. }
  31785. if (rsaCaPrivKeyBuf != NULL) {
  31786. keyFile = XFOPEN(rsaCaKeyFile, "rb");
  31787. if (!keyFile)
  31788. return -12127;
  31789. *rsaCaPrivKeyBufSz = (word32)XFREAD(rsaCaPrivKeyBuf, 1,
  31790. *rsaCaPrivKeyBufSz, keyFile);
  31791. XFCLOSE(keyFile);
  31792. }
  31793. #endif /* USE_CERT_BUFFERS */
  31794. #endif /* NO_RSA */
  31795. /* ECC */
  31796. #ifdef HAVE_ECC
  31797. #ifdef USE_CERT_BUFFERS_256
  31798. if (*eccClientCertBufSz < (word32)sizeof_cliecc_cert_der_256)
  31799. return -12128;
  31800. XMEMCPY(eccClientCertBuf, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  31801. *eccClientCertBufSz = sizeof_cliecc_cert_der_256;
  31802. #else
  31803. certFile = XFOPEN(eccClientCert, "rb");
  31804. if (!certFile)
  31805. return -12129;
  31806. *eccClientCertBufSz = (word32)XFREAD(eccClientCertBuf, 1,
  31807. *eccClientCertBufSz, certFile);
  31808. XFCLOSE(certFile);
  31809. #endif /* USE_CERT_BUFFERS_256 */
  31810. #ifdef USE_CERT_BUFFERS_256
  31811. if (*eccClientPrivKeyBufSz < (word32)sizeof_ecc_clikey_der_256)
  31812. return -12130;
  31813. XMEMCPY(eccClientPrivKeyBuf, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  31814. *eccClientPrivKeyBufSz = sizeof_ecc_clikey_der_256;
  31815. #else
  31816. keyFile = XFOPEN(eccClientKey, "rb");
  31817. if (!keyFile)
  31818. return -12131;
  31819. *eccClientPrivKeyBufSz = (word32)XFREAD(eccClientPrivKeyBuf, 1,
  31820. *eccClientPrivKeyBufSz, keyFile);
  31821. XFCLOSE(keyFile);
  31822. #endif /* USE_CERT_BUFFERS_256 */
  31823. #endif /* HAVE_ECC */
  31824. #ifdef NO_RSA
  31825. (void)rsaClientCertBuf;
  31826. (void)rsaClientCertBufSz;
  31827. (void)rsaClientPrivKeyBuf;
  31828. (void)rsaClientPrivKeyBufSz;
  31829. (void)rsaServerCertBuf;
  31830. (void)rsaServerCertBufSz;
  31831. (void)rsaServerPrivKeyBuf;
  31832. (void)rsaServerPrivKeyBufSz;
  31833. (void)rsaCaCertBuf;
  31834. (void)rsaCaCertBufSz;
  31835. (void)rsaCaPrivKeyBuf;
  31836. (void)rsaCaPrivKeyBufSz;
  31837. #endif
  31838. #ifndef HAVE_ECC
  31839. (void)eccClientCertBuf;
  31840. (void)eccClientCertBufSz;
  31841. (void)eccClientPrivKeyBuf;
  31842. (void)eccClientPrivKeyBufSz;
  31843. #endif
  31844. #ifndef NO_FILESYSTEM
  31845. (void)certFile;
  31846. (void)keyFile;
  31847. #endif
  31848. return 0;
  31849. }
  31850. typedef struct {
  31851. const byte* content;
  31852. word32 contentSz;
  31853. int contentOID;
  31854. int encryptOID;
  31855. int keyWrapOID;
  31856. int keyAgreeOID;
  31857. byte* cert;
  31858. size_t certSz;
  31859. byte* privateKey;
  31860. word32 privateKeySz;
  31861. byte* optionalUkm;
  31862. word32 optionalUkmSz;
  31863. int ktriOptions; /* KTRI options flags */
  31864. int kariOptions; /* KARI options flags */
  31865. /* KEKRI specific */
  31866. const byte* secretKey; /* key, only for kekri RecipientInfo types */
  31867. word32 secretKeySz; /* size of secretKey, bytes */
  31868. const byte* secretKeyId; /* key identifier */
  31869. word32 secretKeyIdSz; /* size of key identifier, bytes */
  31870. void* timePtr; /* time_t pointer */
  31871. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  31872. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  31873. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  31874. word32 otherAttrSz; /* size of otherAttr, bytes */
  31875. int kekriOptions; /* KEKRI options flags */
  31876. /* PWRI specific */
  31877. const char* password;
  31878. word32 passwordSz;
  31879. const byte* salt;
  31880. word32 saltSz;
  31881. int kdfOID;
  31882. int hashOID;
  31883. int kdfIterations;
  31884. int pwriOptions; /* PWRI options flags */
  31885. /* ORI specific */
  31886. int isOri;
  31887. int oriOptions; /* ORI options flags */
  31888. const char* outFileName;
  31889. } pkcs7EnvelopedVector;
  31890. static const byte asnDataOid[] = {
  31891. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01
  31892. };
  31893. /* ORI encrypt callback, responsible for encrypting content-encryption key (CEK)
  31894. * and giving wolfCrypt the value for oriOID and oriValue to place in
  31895. * OtherRecipientInfo.
  31896. *
  31897. * Returns 0 on success, negative upon error. */
  31898. static int myOriEncryptCb(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* oriType,
  31899. word32* oriTypeSz, byte* oriValue, word32* oriValueSz,
  31900. void* ctx)
  31901. {
  31902. int i;
  31903. /* make sure buffers are large enough */
  31904. if ((*oriValueSz < (2 + cekSz)) || (*oriTypeSz < sizeof(oriType)))
  31905. return -12140;
  31906. /* our simple encryption algorithm will be take the bitwise complement */
  31907. oriValue[0] = 0x04; /*ASN OCTET STRING */
  31908. oriValue[1] = (byte)cekSz; /* length */
  31909. for (i = 0; i < (int)cekSz; i++) {
  31910. oriValue[2 + i] = ~cek[i];
  31911. }
  31912. *oriValueSz = 2 + cekSz;
  31913. /* set oriType to ASN.1 encoded data OID */
  31914. XMEMCPY(oriType, asnDataOid, sizeof(asnDataOid));
  31915. *oriTypeSz = sizeof(asnDataOid);
  31916. (void)pkcs7;
  31917. (void)ctx;
  31918. return 0;
  31919. }
  31920. /* ORI decrypt callback, responsible for providing a decrypted content
  31921. * encryption key (CEK) placed into decryptedKey and size placed into
  31922. * decryptedKeySz. oriOID and oriValue are given to the callback to help
  31923. * in decrypting the encrypted CEK.
  31924. *
  31925. * Returns 0 on success, negative upon error. */
  31926. static int myOriDecryptCb(PKCS7* pkcs7, byte* oriType, word32 oriTypeSz,
  31927. byte* oriValue, word32 oriValueSz, byte* decryptedKey,
  31928. word32* decryptedKeySz, void* ctx)
  31929. {
  31930. int i;
  31931. /* make sure oriType matches what we expect */
  31932. if (oriTypeSz != sizeof(asnDataOid))
  31933. return -12150;
  31934. if (XMEMCMP(oriType, asnDataOid, sizeof(asnDataOid)) != 0)
  31935. return -12151;
  31936. /* make sure decrypted buffer is large enough */
  31937. if (*decryptedKeySz < oriValueSz)
  31938. return -12152;
  31939. /* decrypt encrypted CEK using simple bitwise complement,
  31940. only for example */
  31941. for (i = 0; i < (int)oriValueSz - 2; i++) {
  31942. decryptedKey[i] = ~oriValue[2 + i];
  31943. }
  31944. *decryptedKeySz = oriValueSz - 2;
  31945. (void)pkcs7;
  31946. (void)ctx;
  31947. return 0;
  31948. }
  31949. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  31950. /* returns 0 on success */
  31951. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  31952. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  31953. byte* in, int inSz, byte* out, void* usrCtx)
  31954. {
  31955. int keyId = -1, ret, keySz;
  31956. word32 keyIdSz = 8;
  31957. const byte* key;
  31958. byte keyIdRaw[8];
  31959. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  31960. Aes *aes;
  31961. #else
  31962. Aes aes[1];
  31963. #endif
  31964. /* looking for KEY ID
  31965. * fwDecryptKeyID OID "1.2.840.113549.1.9.16.2.37
  31966. */
  31967. WOLFSSL_SMALL_STACK_STATIC const unsigned char OID[] = {
  31968. /* 0x06, 0x0B do not pass in tag and length */
  31969. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  31970. 0x01, 0x09, 0x10, 0x02, 0x25
  31971. };
  31972. WOLFSSL_SMALL_STACK_STATIC const byte defKey[] = {
  31973. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  31974. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  31975. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  31976. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  31977. };
  31978. WOLFSSL_SMALL_STACK_STATIC const byte altKey[] = {
  31979. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  31980. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  31981. };
  31982. /* test user context passed in */
  31983. if (usrCtx == NULL || *(int*)usrCtx != 1) {
  31984. return -12160;
  31985. }
  31986. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  31987. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  31988. return -12164;
  31989. #endif
  31990. /* if needing to find keyIdSz can call with NULL */
  31991. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), NULL,
  31992. &keyIdSz);
  31993. if (ret != LENGTH_ONLY_E) {
  31994. printf("Unexpected error %d when getting keyIdSz\n", ret);
  31995. printf("Possibly no KEY ID attribute set\n");
  31996. ERROR_OUT(-12161, out);
  31997. }
  31998. else {
  31999. XMEMSET(keyIdRaw, 0, sizeof(keyIdRaw));
  32000. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), keyIdRaw,
  32001. &keyIdSz);
  32002. if (ret < 0) {
  32003. goto out;
  32004. }
  32005. if (keyIdSz < 3) {
  32006. printf("keyIdSz is smaller than expected\n");
  32007. ERROR_OUT(-12162, out);
  32008. }
  32009. if (keyIdSz > 2 + sizeof(int)) {
  32010. printf("example case was only expecting a keyId of int size\n");
  32011. ERROR_OUT(-12163, out);
  32012. }
  32013. /* keyIdRaw[0] OCTET TAG */
  32014. /* keyIdRaw[1] Length */
  32015. #ifdef BIG_ENDIAN_ORDER
  32016. if (keyIdRaw[1] == 0x01) {
  32017. keyId = 1;
  32018. }
  32019. #else
  32020. XMEMCPY(&keyId, keyIdRaw + 2, sizeof(keyId));
  32021. #endif
  32022. }
  32023. /* Use keyID here if found to select key and decrypt in HSM or in this
  32024. * example just select key and do software decryption */
  32025. if (keyId == 1) {
  32026. key = altKey;
  32027. keySz = sizeof(altKey);
  32028. }
  32029. else {
  32030. key = defKey;
  32031. keySz = sizeof(defKey);
  32032. }
  32033. switch (encryptOID) {
  32034. #ifdef WOLFSSL_AES_256
  32035. case AES256CBCb:
  32036. if ((keySz != 32 ) || (ivSz != AES_BLOCK_SIZE))
  32037. ERROR_OUT(BAD_FUNC_ARG, out);
  32038. break;
  32039. #endif
  32040. #ifdef WOLFSSL_AES_128
  32041. case AES128CBCb:
  32042. if ((keySz != 16 ) || (ivSz != AES_BLOCK_SIZE))
  32043. ERROR_OUT(BAD_FUNC_ARG, out);
  32044. break;
  32045. #endif
  32046. default:
  32047. printf("Unsupported content cipher type for example");
  32048. ERROR_OUT(ALGO_ID_E, out);
  32049. };
  32050. ret = wc_AesInit(aes, HEAP_HINT, INVALID_DEVID);
  32051. if (ret == 0) {
  32052. ret = wc_AesSetKey(aes, key, keySz, iv, AES_DECRYPTION);
  32053. if (ret == 0)
  32054. ret = wc_AesCbcDecrypt(aes, out, in, inSz);
  32055. wc_AesFree(aes);
  32056. }
  32057. out:
  32058. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  32059. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32060. #endif
  32061. (void)aad;
  32062. (void)aadSz;
  32063. (void)authTag;
  32064. (void)authTagSz;
  32065. return ret;
  32066. }
  32067. #endif /* !NO_AES && HAVE_AES_CBC */
  32068. #define PKCS7_BUF_SIZE 2048
  32069. static int pkcs7enveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  32070. byte* rsaPrivKey, word32 rsaPrivKeySz,
  32071. byte* eccCert, word32 eccCertSz,
  32072. byte* eccPrivKey, word32 eccPrivKeySz)
  32073. {
  32074. int ret = 0, testSz = 0, i;
  32075. int envelopedSz, decodedSz;
  32076. byte *enveloped = NULL;
  32077. byte *decoded = NULL;
  32078. PKCS7* pkcs7 = NULL;
  32079. #ifdef ECC_TIMING_RESISTANT
  32080. WC_RNG rng;
  32081. #endif
  32082. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  32083. XFILE pkcs7File;
  32084. #endif
  32085. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  32086. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  32087. 0x72,0x6c,0x64
  32088. };
  32089. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256) && \
  32090. defined(HAVE_ECC) && defined(WOLFSSL_SHA512)
  32091. byte optionalUkm[] = {
  32092. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  32093. };
  32094. #endif /* NO_AES */
  32095. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128) && \
  32096. !defined(NO_SHA)
  32097. /* encryption key for kekri recipient types */
  32098. WOLFSSL_SMALL_STACK_STATIC const byte secretKey[] = {
  32099. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  32100. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  32101. };
  32102. /* encryption key identifier */
  32103. WOLFSSL_SMALL_STACK_STATIC const byte secretKeyId[] = {
  32104. 0x02,0x02,0x03,0x04
  32105. };
  32106. #endif
  32107. #if !defined(NO_PWDBASED) && !defined(NO_SHA) && \
  32108. !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  32109. #ifndef HAVE_FIPS
  32110. WOLFSSL_SMALL_STACK_STATIC const char password[] = "password"; /* NOTE: Password is too short for FIPS */
  32111. #else
  32112. WOLFSSL_SMALL_STACK_STATIC const char password[] = "passwordFIPS_MODE";
  32113. #endif
  32114. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = {
  32115. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  32116. };
  32117. #endif
  32118. #define MAX_TESTVECTORS_LEN 13
  32119. #define ADD_PKCS7ENVELOPEDVECTOR(...) { \
  32120. pkcs7EnvelopedVector _this_vector = { __VA_ARGS__ }; \
  32121. if (testSz == MAX_TESTVECTORS_LEN) { \
  32122. ret = -12534; \
  32123. goto out; \
  32124. } \
  32125. XMEMCPY(&testVectors[testSz++], &_this_vector, sizeof _this_vector);\
  32126. }
  32127. pkcs7EnvelopedVector *testVectors = NULL;
  32128. #ifdef ECC_TIMING_RESISTANT
  32129. XMEMSET(&rng, 0, sizeof(rng));
  32130. #endif
  32131. testVectors = (pkcs7EnvelopedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  32132. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32133. if (testVectors == NULL) {
  32134. ret = -12534;
  32135. goto out;
  32136. }
  32137. {
  32138. /* key transport key encryption technique */
  32139. #ifndef NO_RSA
  32140. #ifndef NO_DES3
  32141. ADD_PKCS7ENVELOPEDVECTOR(
  32142. data, (word32)sizeof(data), DATA, DES3b, 0, 0, rsaCert, rsaCertSz,
  32143. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  32144. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  32145. "pkcs7envelopedDataDES3.der");
  32146. #endif
  32147. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  32148. #ifdef WOLFSSL_AES_128
  32149. ADD_PKCS7ENVELOPEDVECTOR(
  32150. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, rsaCert, rsaCertSz,
  32151. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  32152. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  32153. "pkcs7envelopedDataAES128CBC.der");
  32154. #endif
  32155. #ifdef WOLFSSL_AES_192
  32156. ADD_PKCS7ENVELOPEDVECTOR(
  32157. data, (word32)sizeof(data), DATA, AES192CBCb, 0, 0, rsaCert, rsaCertSz,
  32158. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  32159. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  32160. "pkcs7envelopedDataAES192CBC.der");
  32161. #endif
  32162. #ifdef WOLFSSL_AES_256
  32163. ADD_PKCS7ENVELOPEDVECTOR(
  32164. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  32165. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  32166. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  32167. "pkcs7envelopedDataAES256CBC.der");
  32168. /* explicitly using SKID for SubjectKeyIdentifier */
  32169. ADD_PKCS7ENVELOPEDVECTOR(
  32170. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  32171. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_SKID, 0, NULL, 0, NULL, 0, NULL,
  32172. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  32173. "pkcs7envelopedDataAES256CBC_SKID.der");
  32174. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  32175. ADD_PKCS7ENVELOPEDVECTOR(
  32176. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  32177. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_ISSUER_AND_SERIAL_NUMBER, 0,
  32178. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  32179. 0, 0, 0, 0, "pkcs7envelopedDataAES256CBC_IANDS.der");
  32180. #endif
  32181. #endif /* !NO_AES && HAVE_AES_CBC */
  32182. #endif
  32183. /* key agreement key encryption technique*/
  32184. #ifdef HAVE_ECC
  32185. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  32186. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  32187. ADD_PKCS7ENVELOPEDVECTOR(
  32188. data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP,
  32189. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  32190. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  32191. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  32192. "pkcs7envelopedDataAES128CBC_ECDH_SHA1KDF.der");
  32193. #endif
  32194. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  32195. ADD_PKCS7ENVELOPEDVECTOR(
  32196. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  32197. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  32198. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  32199. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  32200. "pkcs7envelopedDataAES256CBC_ECDH_SHA256KDF.der");
  32201. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  32202. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  32203. ADD_PKCS7ENVELOPEDVECTOR(
  32204. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  32205. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  32206. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  32207. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  32208. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF.der");
  32209. /* with optional user keying material (ukm) */
  32210. ADD_PKCS7ENVELOPEDVECTOR(
  32211. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  32212. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  32213. eccPrivKeySz, optionalUkm, sizeof(optionalUkm), 0, 0, NULL, 0,
  32214. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  32215. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF_ukm.der");
  32216. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  32217. #endif /* !NO_AES && HAVE_AES_CBC */
  32218. #endif
  32219. /* kekri (KEKRecipientInfo) recipient types */
  32220. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  32221. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  32222. ADD_PKCS7ENVELOPEDVECTOR(
  32223. data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP, 0,
  32224. NULL, 0, NULL, 0, NULL, 0, 0, 0, secretKey, sizeof(secretKey),
  32225. secretKeyId, sizeof(secretKeyId), NULL, NULL, 0, NULL, 0,
  32226. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  32227. "pkcs7envelopedDataAES128CBC_KEKRI.der");
  32228. #endif
  32229. #endif /* !NO_AES && HAVE_AES_CBC */
  32230. /* pwri (PasswordRecipientInfo) recipient types */
  32231. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  32232. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  32233. ADD_PKCS7ENVELOPEDVECTOR(
  32234. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0,
  32235. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  32236. NULL, 0, NULL, NULL, 0, NULL, 0, 0, password,
  32237. (word32)XSTRLEN(password), salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  32238. 0, 0, 0, "pkcs7envelopedDataAES128CBC_PWRI.der");
  32239. #endif
  32240. #endif
  32241. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_128)
  32242. /* ori (OtherRecipientInfo) recipient types */
  32243. ADD_PKCS7ENVELOPEDVECTOR(
  32244. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, NULL, 0, NULL, 0,
  32245. NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0,
  32246. NULL, 0, 0, 0, 0, 0, 1, 0, "pkcs7envelopedDataAES128CBC_ORI.der");
  32247. #endif
  32248. };
  32249. #undef MAX_TESTVECTORS_LEN
  32250. #undef ADD_PKCS7ENVELOPEDVECTOR
  32251. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32252. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32253. if ((! enveloped) || (! decoded)) {
  32254. ERROR_OUT(-12170, out);
  32255. }
  32256. #ifdef ECC_TIMING_RESISTANT
  32257. #ifndef HAVE_FIPS
  32258. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  32259. #else
  32260. ret = wc_InitRng(&rng);
  32261. #endif
  32262. if (ret != 0) {
  32263. ERROR_OUT(-12171, out);
  32264. }
  32265. #endif
  32266. for (i = 0; i < testSz; i++) {
  32267. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  32268. #ifdef WOLFSSL_ASYNC_CRYPT
  32269. INVALID_DEVID /* async PKCS7 is not supported */
  32270. #else
  32271. devId
  32272. #endif
  32273. );
  32274. if (pkcs7 == NULL) {
  32275. ERROR_OUT(-12172, out);
  32276. }
  32277. if (testVectors[i].secretKey != NULL) {
  32278. /* KEKRI recipient type */
  32279. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  32280. if (ret != 0) {
  32281. ERROR_OUT(-12173, out);
  32282. }
  32283. pkcs7->content = (byte*)testVectors[i].content;
  32284. pkcs7->contentSz = testVectors[i].contentSz;
  32285. pkcs7->contentOID = testVectors[i].contentOID;
  32286. pkcs7->encryptOID = testVectors[i].encryptOID;
  32287. pkcs7->ukm = testVectors[i].optionalUkm;
  32288. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  32289. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  32290. (byte *)testVectors[i].secretKey, testVectors[i].secretKeySz,
  32291. (byte *)testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  32292. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  32293. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  32294. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  32295. if (ret < 0) {
  32296. wc_PKCS7_Free(pkcs7);
  32297. ERROR_OUT(-12174, out);
  32298. }
  32299. /* set key, for decryption */
  32300. ret = wc_PKCS7_SetKey(pkcs7, (byte *)testVectors[i].secretKey,
  32301. testVectors[i].secretKeySz);
  32302. if (ret != 0) {
  32303. wc_PKCS7_Free(pkcs7);
  32304. ERROR_OUT(-12175, out);
  32305. }
  32306. } else if (testVectors[i].password != NULL) {
  32307. #if !defined(NO_PWDBASED) && !defined(NO_SHA)
  32308. /* PWRI recipient type */
  32309. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  32310. if (ret != 0) {
  32311. ERROR_OUT(-12176, out);
  32312. }
  32313. pkcs7->content = (byte*)testVectors[i].content;
  32314. pkcs7->contentSz = testVectors[i].contentSz;
  32315. pkcs7->contentOID = testVectors[i].contentOID;
  32316. pkcs7->encryptOID = testVectors[i].encryptOID;
  32317. pkcs7->ukm = testVectors[i].optionalUkm;
  32318. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  32319. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  32320. (byte *)testVectors[i].password, testVectors[i].passwordSz,
  32321. (byte *)testVectors[i].salt, testVectors[i].saltSz,
  32322. testVectors[i].kdfOID,
  32323. testVectors[i].hashOID, testVectors[i].kdfIterations,
  32324. testVectors[i].encryptOID, testVectors[i].pwriOptions);
  32325. if (ret < 0) {
  32326. wc_PKCS7_Free(pkcs7);
  32327. ERROR_OUT(-12177, out);
  32328. }
  32329. /* set password, for decryption */
  32330. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  32331. testVectors[i].passwordSz);
  32332. if (ret < 0) {
  32333. wc_PKCS7_Free(pkcs7);
  32334. ERROR_OUT(-12178, out);
  32335. }
  32336. #endif /* ! NO_PWDBASED && ! NO_SHA */
  32337. } else if (testVectors[i].isOri == 1) {
  32338. /* ORI recipient type */
  32339. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  32340. if (ret != 0) {
  32341. ERROR_OUT(-12179, out);
  32342. }
  32343. pkcs7->content = (byte*)testVectors[i].content;
  32344. pkcs7->contentSz = testVectors[i].contentSz;
  32345. pkcs7->contentOID = testVectors[i].contentOID;
  32346. pkcs7->encryptOID = testVectors[i].encryptOID;
  32347. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  32348. testVectors[i].oriOptions);
  32349. if (ret < 0) {
  32350. wc_PKCS7_Free(pkcs7);
  32351. ERROR_OUT(-12180, out);
  32352. }
  32353. /* set decrypt callback for decryption */
  32354. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  32355. if (ret < 0) {
  32356. wc_PKCS7_Free(pkcs7);
  32357. ERROR_OUT(-12181, out);
  32358. }
  32359. } else {
  32360. /* KTRI or KARI recipient types */
  32361. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  32362. if (ret != 0) {
  32363. ERROR_OUT(-12182, out);
  32364. }
  32365. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  32366. (word32)testVectors[i].certSz);
  32367. if (ret != 0) {
  32368. wc_PKCS7_Free(pkcs7);
  32369. ERROR_OUT(-12183, out);
  32370. }
  32371. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  32372. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  32373. pkcs7->privateKey = testVectors[i].privateKey;
  32374. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  32375. pkcs7->content = (byte*)testVectors[i].content;
  32376. pkcs7->contentSz = testVectors[i].contentSz;
  32377. pkcs7->contentOID = testVectors[i].contentOID;
  32378. pkcs7->encryptOID = testVectors[i].encryptOID;
  32379. pkcs7->ukm = testVectors[i].optionalUkm;
  32380. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  32381. /* set SubjectIdentifier type for KTRI types */
  32382. if (testVectors[i].ktriOptions & CMS_SKID) {
  32383. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  32384. if (ret != 0) {
  32385. wc_PKCS7_Free(pkcs7);
  32386. ERROR_OUT(-12184, out);
  32387. }
  32388. } else if (testVectors[i].ktriOptions &
  32389. CMS_ISSUER_AND_SERIAL_NUMBER) {
  32390. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  32391. CMS_ISSUER_AND_SERIAL_NUMBER);
  32392. if (ret != 0) {
  32393. wc_PKCS7_Free(pkcs7);
  32394. ERROR_OUT(-12185, out);
  32395. }
  32396. }
  32397. }
  32398. #ifdef ECC_TIMING_RESISTANT
  32399. pkcs7->rng = &rng;
  32400. #endif
  32401. /* encode envelopedData */
  32402. envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, enveloped,
  32403. PKCS7_BUF_SIZE);
  32404. if (envelopedSz <= 0) {
  32405. wc_PKCS7_Free(pkcs7);
  32406. ERROR_OUT(-12186, out);
  32407. }
  32408. /* decode envelopedData */
  32409. pkcs7->contentOID = 0;
  32410. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped, envelopedSz,
  32411. decoded, PKCS7_BUF_SIZE);
  32412. if (pkcs7->contentOID != testVectors[i].contentOID ||
  32413. decodedSz <= 0) {
  32414. wc_PKCS7_Free(pkcs7);
  32415. ERROR_OUT(-12187, out);
  32416. }
  32417. /* test decode result */
  32418. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  32419. wc_PKCS7_Free(pkcs7);
  32420. ERROR_OUT(-12188, out);
  32421. }
  32422. #ifndef NO_PKCS7_STREAM
  32423. { /* test reading byte by byte */
  32424. int z;
  32425. for (z = 0; z < envelopedSz; z++) {
  32426. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped + z, 1,
  32427. decoded, PKCS7_BUF_SIZE);
  32428. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  32429. printf("unexpected error %d\n", decodedSz);
  32430. ERROR_OUT(-12189, out);
  32431. }
  32432. }
  32433. /* test decode result */
  32434. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  32435. printf("stream read compare failed\n");
  32436. wc_PKCS7_Free(pkcs7);
  32437. ERROR_OUT(-12190, out);
  32438. }
  32439. }
  32440. #endif
  32441. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  32442. /* output pkcs7 envelopedData for external testing */
  32443. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  32444. if (!pkcs7File) {
  32445. wc_PKCS7_Free(pkcs7);
  32446. ERROR_OUT(-12191, out);
  32447. }
  32448. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  32449. XFCLOSE(pkcs7File);
  32450. if (ret != envelopedSz) {
  32451. wc_PKCS7_Free(pkcs7);
  32452. ERROR_OUT(-12192, out);
  32453. } else {
  32454. /* reset ret to 0 for success */
  32455. ret = 0;
  32456. }
  32457. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  32458. wc_PKCS7_Free(pkcs7);
  32459. pkcs7 = NULL;
  32460. }
  32461. #ifdef ECC_TIMING_RESISTANT
  32462. wc_FreeRng(&rng);
  32463. #endif
  32464. (void)eccCert;
  32465. (void)eccCertSz;
  32466. (void)eccPrivKey;
  32467. (void)eccPrivKeySz;
  32468. (void)rsaCert;
  32469. (void)rsaCertSz;
  32470. (void)rsaPrivKey;
  32471. (void)rsaPrivKeySz;
  32472. out:
  32473. if (testVectors)
  32474. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32475. if (enveloped)
  32476. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32477. if (decoded)
  32478. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32479. return ret;
  32480. }
  32481. WOLFSSL_TEST_SUBROUTINE int pkcs7enveloped_test(void)
  32482. {
  32483. int ret = 0;
  32484. byte* rsaCert = NULL;
  32485. byte* rsaPrivKey = NULL;
  32486. word32 rsaCertSz = 0;
  32487. word32 rsaPrivKeySz = 0;
  32488. byte* eccCert = NULL;
  32489. byte* eccPrivKey = NULL;
  32490. word32 eccCertSz = 0;
  32491. word32 eccPrivKeySz = 0;
  32492. #ifndef NO_RSA
  32493. /* read client RSA cert and key in DER format */
  32494. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32495. if (rsaCert == NULL)
  32496. return -12200;
  32497. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32498. if (rsaPrivKey == NULL) {
  32499. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32500. return -12201;
  32501. }
  32502. rsaCertSz = FOURK_BUF;
  32503. rsaPrivKeySz = FOURK_BUF;
  32504. #endif /* NO_RSA */
  32505. #ifdef HAVE_ECC
  32506. /* read client ECC cert and key in DER format */
  32507. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32508. if (eccCert == NULL) {
  32509. #ifndef NO_RSA
  32510. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32511. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32512. #endif
  32513. return -12202;
  32514. }
  32515. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32516. if (eccPrivKey == NULL) {
  32517. #ifndef NO_RSA
  32518. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32519. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32520. #endif
  32521. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32522. return -12203;
  32523. }
  32524. eccCertSz = FOURK_BUF;
  32525. eccPrivKeySz = FOURK_BUF;
  32526. #endif /* HAVE_ECC */
  32527. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  32528. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  32529. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  32530. eccPrivKey, &eccPrivKeySz);
  32531. if (ret < 0) {
  32532. #ifndef NO_RSA
  32533. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32534. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32535. #endif
  32536. #ifdef HAVE_ECC
  32537. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32538. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32539. #endif
  32540. return -12204;
  32541. }
  32542. ret = pkcs7enveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  32543. rsaPrivKey, (word32)rsaPrivKeySz,
  32544. eccCert, (word32)eccCertSz,
  32545. eccPrivKey, (word32)eccPrivKeySz);
  32546. #ifndef NO_RSA
  32547. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32548. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32549. #endif
  32550. #ifdef HAVE_ECC
  32551. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32552. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32553. #endif
  32554. return ret;
  32555. }
  32556. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  32557. typedef struct {
  32558. const byte* content;
  32559. word32 contentSz;
  32560. int contentOID;
  32561. int encryptOID;
  32562. int keyWrapOID;
  32563. int keyAgreeOID;
  32564. byte* cert;
  32565. size_t certSz;
  32566. byte* privateKey;
  32567. word32 privateKeySz;
  32568. PKCS7Attrib* authAttribs;
  32569. word32 authAttribsSz;
  32570. PKCS7Attrib* unauthAttribs;
  32571. word32 unauthAttribsSz;
  32572. /* KARI / KTRI specific */
  32573. byte* optionalUkm;
  32574. word32 optionalUkmSz;
  32575. int ktriOptions; /* KTRI options flags */
  32576. int kariOptions; /* KARI options flags */
  32577. /* KEKRI specific */
  32578. byte* secretKey; /* key, only for kekri RecipientInfo types */
  32579. word32 secretKeySz; /* size of secretKey, bytes */
  32580. byte* secretKeyId; /* key identifier */
  32581. word32 secretKeyIdSz; /* size of key identifier, bytes */
  32582. void* timePtr; /* time_t pointer */
  32583. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  32584. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  32585. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  32586. word32 otherAttrSz; /* size of otherAttr, bytes */
  32587. int kekriOptions; /* KEKRI options flags */
  32588. /* PWRI specific */
  32589. char* password; /* password */
  32590. word32 passwordSz; /* password size, bytes */
  32591. byte* salt; /* KDF salt */
  32592. word32 saltSz; /* KDF salt size, bytes */
  32593. int kdfOID; /* KDF OID */
  32594. int hashOID; /* KDF hash algorithm OID */
  32595. int kdfIterations; /* KDF iterations */
  32596. int kekEncryptOID; /* KEK encryption algorithm OID */
  32597. int pwriOptions; /* PWRI options flags */
  32598. /* ORI specific */
  32599. int isOri;
  32600. int oriOptions; /* ORI options flags */
  32601. const char* outFileName;
  32602. } pkcs7AuthEnvelopedVector;
  32603. static int pkcs7authenveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  32604. byte* rsaPrivKey, word32 rsaPrivKeySz,
  32605. byte* eccCert, word32 eccCertSz,
  32606. byte* eccPrivKey, word32 eccPrivKeySz)
  32607. {
  32608. int ret = 0, testSz = 0, i;
  32609. int envelopedSz, decodedSz;
  32610. byte *enveloped = NULL;
  32611. byte *decoded = NULL;
  32612. WC_RNG rng;
  32613. PKCS7* pkcs7;
  32614. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  32615. XFILE pkcs7File;
  32616. #endif
  32617. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  32618. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  32619. 0x72,0x6c,0x64
  32620. };
  32621. byte senderNonce[PKCS7_NONCE_SZ + 2];
  32622. #ifdef HAVE_ECC
  32623. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  32624. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  32625. WOLFSSL_SMALL_STACK_STATIC const byte senderNonceOid[] =
  32626. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  32627. 0x09, 0x05 };
  32628. PKCS7Attrib attribs[] =
  32629. {
  32630. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  32631. sizeof(senderNonce) }
  32632. };
  32633. #endif
  32634. #endif
  32635. #endif
  32636. #if !defined(NO_AES) && defined(WOLFSSL_AES_256) && defined(HAVE_ECC) && \
  32637. defined(WOLFSSL_SHA512)
  32638. WOLFSSL_SMALL_STACK_STATIC const byte optionalUkm[] = {
  32639. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  32640. };
  32641. #endif /* NO_AES */
  32642. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  32643. /* encryption key for kekri recipient types */
  32644. WOLFSSL_SMALL_STACK_STATIC const byte secretKey[] = {
  32645. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  32646. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  32647. };
  32648. /* encryption key identifier */
  32649. WOLFSSL_SMALL_STACK_STATIC const byte secretKeyId[] = {
  32650. 0x02,0x02,0x03,0x04
  32651. };
  32652. #endif
  32653. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  32654. !defined(NO_SHA) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  32655. #ifndef HAVE_FIPS
  32656. WOLFSSL_SMALL_STACK_STATIC const char password[] = "password";
  32657. #else
  32658. WOLFSSL_SMALL_STACK_STATIC const char password[] = "passwordFIPS_MODE";
  32659. #endif
  32660. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = {
  32661. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  32662. };
  32663. #endif
  32664. #define MAX_TESTVECTORS_LEN 20
  32665. #define ADD_PKCS7AUTHENVELOPEDVECTOR(...) { \
  32666. pkcs7AuthEnvelopedVector _this_vector = { __VA_ARGS__ }; \
  32667. if (testSz == MAX_TESTVECTORS_LEN) { \
  32668. ret = -12534; \
  32669. goto out; \
  32670. } \
  32671. XMEMCPY(&testVectors[testSz++], &_this_vector, sizeof _this_vector);\
  32672. }
  32673. pkcs7AuthEnvelopedVector *testVectors = NULL;
  32674. XMEMSET(&rng, 0, sizeof(rng));
  32675. testVectors = (pkcs7AuthEnvelopedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  32676. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32677. if (testVectors == NULL) {
  32678. ret = -12534;
  32679. goto out;
  32680. }
  32681. {
  32682. /* key transport key encryption technique */
  32683. #ifndef NO_RSA
  32684. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  32685. #ifdef WOLFSSL_AES_128
  32686. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32687. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, rsaCert, rsaCertSz,
  32688. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  32689. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  32690. 0, 0, "pkcs7authEnvelopedDataAES128GCM.der");
  32691. #endif
  32692. #ifdef WOLFSSL_AES_192
  32693. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32694. data, (word32)sizeof(data), DATA, AES192GCMb, 0, 0, rsaCert, rsaCertSz,
  32695. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  32696. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  32697. 0, 0, "pkcs7authEnvelopedDataAES192GCM.der");
  32698. #endif
  32699. #ifdef WOLFSSL_AES_256
  32700. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32701. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  32702. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  32703. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  32704. 0, 0, "pkcs7authEnvelopedDataAES256GCM.der");
  32705. /* test with contentType set to FirmwarePkgData */
  32706. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32707. data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, 0, 0,
  32708. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL,
  32709. 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL,
  32710. 0, 0, 0, 0, 0, 0, 0, 0,
  32711. "pkcs7authEnvelopedDataAES256GCM_firmwarePkgData.der");
  32712. /* explicitly using SKID for SubjectKeyIdentifier */
  32713. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32714. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  32715. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, CMS_SKID, 0,
  32716. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  32717. 0, 0, 0, 0, 0, "pkcs7authEnvelopedDataAES256GCM_SKID.der");
  32718. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  32719. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32720. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  32721. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0,
  32722. CMS_ISSUER_AND_SERIAL_NUMBER, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  32723. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  32724. "pkcs7authEnvelopedDataAES256GCM_IANDS.der");
  32725. #endif
  32726. #endif /* NO_AES */
  32727. #endif
  32728. /* key agreement key encryption technique*/
  32729. #ifdef HAVE_ECC
  32730. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  32731. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  32732. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32733. data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP,
  32734. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  32735. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  32736. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  32737. "pkcs7authEnvelopedDataAES128GCM_ECDH_SHA1KDF.der");
  32738. #endif
  32739. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  32740. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32741. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  32742. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  32743. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  32744. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  32745. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF.der");
  32746. /* with authenticated attributes */
  32747. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32748. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  32749. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  32750. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  32751. NULL, 0, NULL, 0, 0, 0, NULL, 0,
  32752. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  32753. 0, 0, 0,
  32754. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_authAttribs.der");
  32755. /* with unauthenticated attributes */
  32756. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32757. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  32758. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  32759. eccPrivKeySz, NULL, 0, attribs,
  32760. (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0, NULL, 0,
  32761. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  32762. 0, 0, 0,
  32763. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_unauthAttribs.der");
  32764. /* with authenticated AND unauthenticated attributes */
  32765. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32766. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  32767. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  32768. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  32769. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  32770. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  32771. 0, 0, 0, 0, 0, 0,
  32772. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_bothAttribs.der");
  32773. /* with authenticated AND unauthenticated attributes AND
  32774. * contentType of FirmwarePkgData */
  32775. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32776. data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, AES256_WRAP,
  32777. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  32778. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  32779. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  32780. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  32781. 0, 0, 0, 0, 0, 0,
  32782. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_fw_bothAttribs.der");
  32783. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  32784. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  32785. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32786. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  32787. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  32788. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL,
  32789. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  32790. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF.der");
  32791. /* with optional user keying material (ukm) */
  32792. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32793. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  32794. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  32795. eccPrivKeySz, NULL, 0, NULL, 0, (byte *)optionalUkm, sizeof(optionalUkm), 0,
  32796. 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  32797. 0, 0, 0, 0, 0, 0,
  32798. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF_ukm.der");
  32799. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  32800. #endif /* NO_AES */
  32801. #endif
  32802. /* kekri (KEKRecipientInfo) recipient types */
  32803. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  32804. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  32805. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32806. data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP, 0,
  32807. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0,
  32808. (byte *)secretKey, sizeof(secretKey), (byte *)secretKeyId, sizeof(secretKeyId),
  32809. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  32810. "pkcs7authEnvelopedDataAES128GCM_KEKRI.der");
  32811. #endif
  32812. #endif
  32813. /* pwri (PasswordRecipientInfo) recipient types */
  32814. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM)
  32815. #if !defined(NO_SHA) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  32816. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32817. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0,
  32818. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  32819. NULL, 0, NULL, NULL, 0, NULL, 0, 0, (char *)password,
  32820. (word32)XSTRLEN(password), (byte *)salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  32821. AES128CBCb, 0, 0, 0, "pkcs7authEnvelopedDataAES128GCM_PWRI.der");
  32822. #endif
  32823. #endif
  32824. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  32825. #ifdef WOLFSSL_AES_128
  32826. /* ori (OtherRecipientInfo) recipient types */
  32827. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32828. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, NULL, 0, NULL, 0,
  32829. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  32830. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 1, 0,
  32831. "pkcs7authEnvelopedDataAES128GCM_ORI.der");
  32832. #endif
  32833. #endif
  32834. }
  32835. #undef MAX_TESTVECTORS_LEN
  32836. #undef ADD_PKCS7AUTHENVELOPEDVECTOR
  32837. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32838. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32839. if ((! enveloped) || (! decoded)) {
  32840. ERROR_OUT(-12210, out);
  32841. }
  32842. /* generate senderNonce */
  32843. {
  32844. #ifndef HAVE_FIPS
  32845. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  32846. #else
  32847. ret = wc_InitRng(&rng);
  32848. #endif
  32849. if (ret != 0) {
  32850. ERROR_OUT(-12211, out);
  32851. }
  32852. senderNonce[0] = 0x04;
  32853. senderNonce[1] = PKCS7_NONCE_SZ;
  32854. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  32855. if (ret != 0) {
  32856. wc_FreeRng(&rng);
  32857. ERROR_OUT(-12212, out);
  32858. }
  32859. }
  32860. for (i = 0; i < testSz; i++) {
  32861. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  32862. #ifdef WOLFSSL_ASYNC_CRYPT
  32863. INVALID_DEVID /* async PKCS7 is not supported */
  32864. #else
  32865. devId
  32866. #endif
  32867. );
  32868. if (pkcs7 == NULL) {
  32869. ERROR_OUT(-12213, out);
  32870. }
  32871. if (testVectors[i].secretKey != NULL) {
  32872. /* KEKRI recipient type */
  32873. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  32874. if (ret != 0) {
  32875. ERROR_OUT(-12214, out);
  32876. }
  32877. pkcs7->content = (byte*)testVectors[i].content;
  32878. pkcs7->contentSz = testVectors[i].contentSz;
  32879. pkcs7->contentOID = testVectors[i].contentOID;
  32880. pkcs7->encryptOID = testVectors[i].encryptOID;
  32881. pkcs7->ukm = testVectors[i].optionalUkm;
  32882. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  32883. pkcs7->authAttribs = testVectors[i].authAttribs;
  32884. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  32885. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  32886. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  32887. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  32888. testVectors[i].secretKey, testVectors[i].secretKeySz,
  32889. testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  32890. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  32891. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  32892. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  32893. if (ret < 0) {
  32894. wc_PKCS7_Free(pkcs7);
  32895. ERROR_OUT(-12215, out);
  32896. }
  32897. /* set key, for decryption */
  32898. ret = wc_PKCS7_SetKey(pkcs7, testVectors[i].secretKey,
  32899. testVectors[i].secretKeySz);
  32900. if (ret != 0) {
  32901. wc_PKCS7_Free(pkcs7);
  32902. ERROR_OUT(-12216, out);
  32903. }
  32904. } else if (testVectors[i].password != NULL) {
  32905. #if !defined(NO_PWDBASED) && !defined(NO_SHA)
  32906. /* PWRI recipient type */
  32907. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  32908. if (ret != 0) {
  32909. ERROR_OUT(-12217, out);
  32910. }
  32911. pkcs7->content = (byte*)testVectors[i].content;
  32912. pkcs7->contentSz = testVectors[i].contentSz;
  32913. pkcs7->contentOID = testVectors[i].contentOID;
  32914. pkcs7->encryptOID = testVectors[i].encryptOID;
  32915. pkcs7->ukm = testVectors[i].optionalUkm;
  32916. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  32917. pkcs7->authAttribs = testVectors[i].authAttribs;
  32918. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  32919. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  32920. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  32921. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  32922. (byte*)testVectors[i].password,
  32923. testVectors[i].passwordSz, testVectors[i].salt,
  32924. testVectors[i].saltSz, testVectors[i].kdfOID,
  32925. testVectors[i].hashOID, testVectors[i].kdfIterations,
  32926. testVectors[i].kekEncryptOID, testVectors[i].pwriOptions);
  32927. if (ret < 0) {
  32928. wc_PKCS7_Free(pkcs7);
  32929. ERROR_OUT(-12218, out);
  32930. }
  32931. /* set password, for decryption */
  32932. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  32933. testVectors[i].passwordSz);
  32934. if (ret < 0) {
  32935. wc_PKCS7_Free(pkcs7);
  32936. ERROR_OUT(-12219, out);
  32937. }
  32938. #endif /* ! NO_PWDBASED && ! NO_SHA */
  32939. } else if (testVectors[i].isOri == 1) {
  32940. /* ORI recipient type */
  32941. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  32942. if (ret != 0) {
  32943. ERROR_OUT(-12220, out);
  32944. }
  32945. pkcs7->content = (byte*)testVectors[i].content;
  32946. pkcs7->contentSz = testVectors[i].contentSz;
  32947. pkcs7->contentOID = testVectors[i].contentOID;
  32948. pkcs7->encryptOID = testVectors[i].encryptOID;
  32949. pkcs7->authAttribs = testVectors[i].authAttribs;
  32950. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  32951. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  32952. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  32953. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  32954. testVectors[i].oriOptions);
  32955. if (ret < 0) {
  32956. wc_PKCS7_Free(pkcs7);
  32957. ERROR_OUT(-12221, out);
  32958. }
  32959. /* set decrypt callback for decryption */
  32960. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  32961. if (ret < 0) {
  32962. wc_PKCS7_Free(pkcs7);
  32963. ERROR_OUT(-12222, out);
  32964. }
  32965. } else {
  32966. /* KTRI or KARI recipient types */
  32967. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  32968. (word32)testVectors[i].certSz);
  32969. if (ret != 0) {
  32970. wc_PKCS7_Free(pkcs7);
  32971. ERROR_OUT(-12223, out);
  32972. }
  32973. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  32974. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  32975. pkcs7->privateKey = testVectors[i].privateKey;
  32976. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  32977. pkcs7->content = (byte*)testVectors[i].content;
  32978. pkcs7->contentSz = testVectors[i].contentSz;
  32979. pkcs7->contentOID = testVectors[i].contentOID;
  32980. pkcs7->encryptOID = testVectors[i].encryptOID;
  32981. pkcs7->ukm = testVectors[i].optionalUkm;
  32982. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  32983. pkcs7->authAttribs = testVectors[i].authAttribs;
  32984. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  32985. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  32986. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  32987. /* set SubjectIdentifier type for KTRI types */
  32988. if (testVectors[i].ktriOptions & CMS_SKID) {
  32989. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  32990. if (ret != 0) {
  32991. wc_PKCS7_Free(pkcs7);
  32992. ERROR_OUT(-12224, out);
  32993. }
  32994. } else if (testVectors[i].ktriOptions &
  32995. CMS_ISSUER_AND_SERIAL_NUMBER) {
  32996. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  32997. CMS_ISSUER_AND_SERIAL_NUMBER);
  32998. if (ret != 0) {
  32999. wc_PKCS7_Free(pkcs7);
  33000. ERROR_OUT(-12225, out);
  33001. }
  33002. }
  33003. }
  33004. #ifdef ECC_TIMING_RESISTANT
  33005. pkcs7->rng = &rng;
  33006. #endif
  33007. /* encode envelopedData */
  33008. envelopedSz = wc_PKCS7_EncodeAuthEnvelopedData(pkcs7, enveloped,
  33009. PKCS7_BUF_SIZE);
  33010. if (envelopedSz <= 0) {
  33011. wc_PKCS7_Free(pkcs7);
  33012. ERROR_OUT(-12226, out);
  33013. }
  33014. #ifndef NO_PKCS7_STREAM
  33015. { /* test reading byte by byte */
  33016. int z;
  33017. for (z = 0; z < envelopedSz; z++) {
  33018. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7,
  33019. enveloped + z, 1, decoded, PKCS7_BUF_SIZE);
  33020. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  33021. printf("unexpected error %d\n", decodedSz);
  33022. ERROR_OUT(-12227, out);
  33023. }
  33024. }
  33025. /* test decode result */
  33026. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  33027. printf("stream read compare failed\n");
  33028. wc_PKCS7_Free(pkcs7);
  33029. ERROR_OUT(-12228, out);
  33030. }
  33031. }
  33032. #endif
  33033. /* decode envelopedData */
  33034. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7, enveloped,
  33035. envelopedSz, decoded,
  33036. PKCS7_BUF_SIZE);
  33037. if (decodedSz <= 0) {
  33038. wc_PKCS7_Free(pkcs7);
  33039. ERROR_OUT(-12229, out);
  33040. }
  33041. /* test decode result */
  33042. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  33043. wc_PKCS7_Free(pkcs7);
  33044. ERROR_OUT(-12230, out);
  33045. }
  33046. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  33047. /* output pkcs7 envelopedData for external testing */
  33048. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  33049. if (!pkcs7File) {
  33050. wc_PKCS7_Free(pkcs7);
  33051. ERROR_OUT(-12231, out);
  33052. }
  33053. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  33054. XFCLOSE(pkcs7File);
  33055. if (ret != envelopedSz) {
  33056. wc_PKCS7_Free(pkcs7);
  33057. ERROR_OUT(-12232, out);
  33058. } else {
  33059. /* reset ret to 0 for success */
  33060. ret = 0;
  33061. }
  33062. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  33063. wc_PKCS7_Free(pkcs7);
  33064. pkcs7 = NULL;
  33065. }
  33066. wc_FreeRng(&rng);
  33067. (void)eccCert;
  33068. (void)eccCertSz;
  33069. (void)eccPrivKey;
  33070. (void)eccPrivKeySz;
  33071. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  33072. (void)secretKey;
  33073. (void)secretKeyId;
  33074. #endif
  33075. #ifdef NO_RSA
  33076. (void)rsaCert;
  33077. (void)rsaCertSz;
  33078. (void)rsaPrivKey;
  33079. (void)rsaPrivKeySz;
  33080. #endif
  33081. out:
  33082. if (testVectors)
  33083. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33084. if (enveloped)
  33085. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33086. if (decoded)
  33087. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33088. return ret;
  33089. }
  33090. WOLFSSL_TEST_SUBROUTINE int pkcs7authenveloped_test(void)
  33091. {
  33092. int ret = 0;
  33093. byte* rsaCert = NULL;
  33094. byte* rsaPrivKey = NULL;
  33095. word32 rsaCertSz = 0;
  33096. word32 rsaPrivKeySz = 0;
  33097. byte* eccCert = NULL;
  33098. byte* eccPrivKey = NULL;
  33099. word32 eccCertSz = 0;
  33100. word32 eccPrivKeySz = 0;
  33101. #ifndef NO_RSA
  33102. /* read client RSA cert and key in DER format */
  33103. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33104. if (rsaCert == NULL)
  33105. return -12300;
  33106. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33107. if (rsaPrivKey == NULL) {
  33108. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33109. return -12301;
  33110. }
  33111. rsaCertSz = FOURK_BUF;
  33112. rsaPrivKeySz = FOURK_BUF;
  33113. #endif /* NO_RSA */
  33114. #ifdef HAVE_ECC
  33115. /* read client ECC cert and key in DER format */
  33116. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33117. if (eccCert == NULL) {
  33118. #ifndef NO_RSA
  33119. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33120. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33121. #endif
  33122. return -12302;
  33123. }
  33124. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33125. if (eccPrivKey == NULL) {
  33126. #ifndef NO_RSA
  33127. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33128. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33129. #endif
  33130. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33131. return -12303;
  33132. }
  33133. eccCertSz = FOURK_BUF;
  33134. eccPrivKeySz = FOURK_BUF;
  33135. #endif /* HAVE_ECC */
  33136. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  33137. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  33138. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  33139. eccPrivKey, &eccPrivKeySz);
  33140. if (ret < 0) {
  33141. #ifndef NO_RSA
  33142. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33143. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33144. #endif
  33145. #ifdef HAVE_ECC
  33146. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33147. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33148. #endif
  33149. return -12304;
  33150. }
  33151. ret = pkcs7authenveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  33152. rsaPrivKey, (word32)rsaPrivKeySz,
  33153. eccCert, (word32)eccCertSz,
  33154. eccPrivKey, (word32)eccPrivKeySz);
  33155. #ifndef NO_RSA
  33156. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33157. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33158. #endif
  33159. #ifdef HAVE_ECC
  33160. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33161. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33162. #endif
  33163. return ret;
  33164. }
  33165. #endif /* HAVE_AESGCM || HAVE_AESCCM */
  33166. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  33167. static const byte p7DefKey[] = {
  33168. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33169. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33170. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33171. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  33172. };
  33173. static const byte p7AltKey[] = {
  33174. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33175. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  33176. };
  33177. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  33178. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  33179. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  33180. {
  33181. int ret;
  33182. if (cek == NULL || out == NULL)
  33183. return BAD_FUNC_ARG;
  33184. /* test case sanity checks */
  33185. if (keyIdSz != 1) {
  33186. return -12310;
  33187. }
  33188. if (keyId[0] != 0x00) {
  33189. return -12311;
  33190. }
  33191. if (type != (int)PKCS7_KEKRI) {
  33192. return -12312;
  33193. }
  33194. switch (keyWrapAlgo) {
  33195. case AES256_WRAP:
  33196. ret = wc_AesKeyUnWrap(p7DefKey, sizeof(p7DefKey), cek, cekSz,
  33197. out, outSz, NULL);
  33198. if (ret <= 0)
  33199. return ret;
  33200. break;
  33201. default:
  33202. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  33203. return BAD_KEYWRAP_ALG_E;
  33204. };
  33205. (void)pkcs7;
  33206. (void)direction;
  33207. (void)orginKey; /* used with KAKRI */
  33208. (void)orginKeySz;
  33209. return ret;
  33210. }
  33211. /* returns key size on success */
  33212. static int getFirmwareKey(PKCS7* pkcs7, byte* key, word32 keySz)
  33213. {
  33214. int ret;
  33215. word32 atrSz;
  33216. byte atr[256];
  33217. /* Additionally can look for fwWrappedFirmwareKey
  33218. * 1.2.840.113529.1.9.16.1.16 */
  33219. const unsigned char fwWrappedFirmwareKey[] = {
  33220. /* 0x06, 0x0B */
  33221. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  33222. 0x01, 0x09, 0x10, 0x02, 0x27
  33223. };
  33224. /* find keyID in fwWrappedFirmwareKey */
  33225. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  33226. sizeof(fwWrappedFirmwareKey), NULL, &atrSz);
  33227. if (ret == LENGTH_ONLY_E) {
  33228. XMEMSET(atr, 0, sizeof(atr));
  33229. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  33230. sizeof(fwWrappedFirmwareKey), atr, &atrSz);
  33231. /* keyIdRaw[0] OCTET TAG */
  33232. /* keyIdRaw[1] Length */
  33233. if (ret > 0) {
  33234. PKCS7* envPkcs7;
  33235. envPkcs7 = wc_PKCS7_New(NULL, 0);
  33236. if (envPkcs7 == NULL) {
  33237. return MEMORY_E;
  33238. }
  33239. wc_PKCS7_Init(envPkcs7, NULL, 0);
  33240. ret = wc_PKCS7_SetWrapCEKCb(envPkcs7, myCEKwrapFunc);
  33241. if (ret == 0) {
  33242. /* expecting FIRMWARE_PKG_DATA content */
  33243. envPkcs7->contentOID = FIRMWARE_PKG_DATA;
  33244. ret = wc_PKCS7_DecodeEnvelopedData(envPkcs7, atr, atrSz,
  33245. key, keySz);
  33246. if (envPkcs7->contentOID != FIRMWARE_PKG_DATA) {
  33247. /* the contentOID should have been set to the inner
  33248. * FIRMWARE_PKG_DATA content */
  33249. ret = BAD_STATE_E;
  33250. }
  33251. }
  33252. wc_PKCS7_Free(envPkcs7);
  33253. }
  33254. }
  33255. return ret;
  33256. }
  33257. /* create a KEKRI enveloped data
  33258. * return size on success */
  33259. static int envelopedData_encrypt(byte* in, word32 inSz, byte* out,
  33260. word32 outSz)
  33261. {
  33262. int ret;
  33263. PKCS7* pkcs7;
  33264. WOLFSSL_SMALL_STACK_STATIC const byte keyId[] = { 0x00 };
  33265. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  33266. if (pkcs7 == NULL)
  33267. return -12330;
  33268. pkcs7->content = in;
  33269. pkcs7->contentSz = inSz;
  33270. pkcs7->contentOID = FIRMWARE_PKG_DATA;
  33271. pkcs7->encryptOID = AES256CBCb;
  33272. pkcs7->ukm = NULL;
  33273. pkcs7->ukmSz = 0;
  33274. /* add recipient (KEKRI type) */
  33275. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP, (byte*)p7DefKey,
  33276. sizeof(p7DefKey), (byte*)keyId,
  33277. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0);
  33278. if (ret < 0) {
  33279. printf("wc_PKCS7_AddRecipient_KEKRI() failed, ret = %d\n", ret);
  33280. wc_PKCS7_Free(pkcs7);
  33281. return -12331;
  33282. }
  33283. /* encode envelopedData, returns size */
  33284. ret = wc_PKCS7_EncodeEnvelopedData(pkcs7, out, outSz);
  33285. if (ret <= 0) {
  33286. printf("wc_PKCS7_EncodeEnvelopedData() failed, ret = %d\n", ret);
  33287. wc_PKCS7_Free(pkcs7);
  33288. return -12332;
  33289. }
  33290. wc_PKCS7_Free(pkcs7);
  33291. return ret;
  33292. }
  33293. /*
  33294. * keyHint is the KeyID to be set in the fwDecryptKeyID attribute
  33295. * returns size of buffer output on success
  33296. */
  33297. static int generateBundle(byte* out, word32 *outSz, const byte* encryptKey,
  33298. word32 encryptKeySz, byte keyHint, byte* cert, word32 certSz,
  33299. byte* key, word32 keySz)
  33300. {
  33301. int ret, attribNum = 1;
  33302. PKCS7* pkcs7;
  33303. /* KEY ID
  33304. * fwDecryptKeyID OID 1.2.840.113549.1.9.16.2.37
  33305. */
  33306. const unsigned char fwDecryptKeyID[] = {
  33307. 0x06, 0x0B,
  33308. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  33309. 0x01, 0x09, 0x10, 0x02, 0x25
  33310. };
  33311. /* fwWrappedFirmwareKey 1.2.840.113529.1.9.16.1.16 */
  33312. const unsigned char fwWrappedFirmwareKey[] = {
  33313. 0x06, 0x0B, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  33314. 0x01, 0x09, 0x10, 0x02, 0x27
  33315. };
  33316. byte keyID[] = { 0x04, 0x01, 0x00 };
  33317. byte env[256];
  33318. char data[] = "Test of wolfSSL PKCS7 decrypt callback";
  33319. PKCS7Attrib attribs[] =
  33320. {
  33321. { fwDecryptKeyID, sizeof(fwDecryptKeyID), keyID, sizeof(keyID) },
  33322. { fwWrappedFirmwareKey, sizeof(fwWrappedFirmwareKey), env, 0 }
  33323. };
  33324. keyID[2] = keyHint;
  33325. /* If using keyHint 0 then create a bundle with fwWrappedFirmwareKey */
  33326. if (keyHint == 0) {
  33327. ret = envelopedData_encrypt((byte*)p7DefKey, sizeof(p7DefKey), env,
  33328. sizeof(env));
  33329. if (ret <= 0) {
  33330. return ret;
  33331. }
  33332. attribs[1].valueSz = ret;
  33333. attribNum++;
  33334. }
  33335. /* init PKCS7 */
  33336. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  33337. if (pkcs7 == NULL)
  33338. return -12340;
  33339. ret = wc_PKCS7_InitWithCert(pkcs7, cert, certSz);
  33340. if (ret != 0) {
  33341. printf("ERROR: wc_PKCS7_InitWithCert() failed, ret = %d\n", ret);
  33342. wc_PKCS7_Free(pkcs7);
  33343. return -12341;
  33344. }
  33345. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  33346. if (ret != 0) {
  33347. wc_PKCS7_Free(pkcs7);
  33348. return -12342;
  33349. }
  33350. /* encode Signed Encrypted FirmwarePkgData */
  33351. if (encryptKeySz == 16) {
  33352. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  33353. encryptKeySz, key, keySz, AES128CBCb, RSAk, SHA256h,
  33354. (byte*)data, sizeof(data), NULL, 0,
  33355. attribs, attribNum, out, *outSz);
  33356. }
  33357. else {
  33358. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  33359. encryptKeySz, key, keySz, AES256CBCb, RSAk, SHA256h,
  33360. (byte*)data, sizeof(data), NULL, 0,
  33361. attribs, attribNum, out, *outSz);
  33362. }
  33363. if (ret <= 0) {
  33364. printf("ERROR: wc_PKCS7_EncodeSignedEncryptedFPD() failed, "
  33365. "ret = %d\n", ret);
  33366. wc_PKCS7_Free(pkcs7);
  33367. return -12343;
  33368. } else {
  33369. *outSz = ret;
  33370. }
  33371. wc_PKCS7_Free(pkcs7);
  33372. return ret;
  33373. }
  33374. /* test verification and decryption of PKCS7 bundle
  33375. * return 0 on success
  33376. */
  33377. static int verifyBundle(byte* derBuf, word32 derSz, int keyHint)
  33378. {
  33379. int ret = 0;
  33380. int usrCtx = 1; /* test value to pass as user context to callback */
  33381. PKCS7* pkcs7 = NULL;
  33382. byte* sid = NULL;
  33383. word32 sidSz;
  33384. byte key[256];
  33385. word32 keySz = sizeof(key);
  33386. byte *decoded = NULL;
  33387. int decodedSz = FOURK_BUF/2;
  33388. WOLFSSL_SMALL_STACK_STATIC const byte expectedSid[] = {
  33389. #ifdef NO_SHA
  33390. #ifdef USE_CERT_BUFFERS_1024
  33391. 0x70, 0xe7, 0x79, 0x60, 0x8f, 0x41, 0xdc, 0xe9,
  33392. 0xad, 0x8b, 0x3d, 0x0c, 0x20, 0xf4, 0xc3, 0xf2,
  33393. 0x8e, 0x05, 0xe8, 0xa1, 0xb6, 0x68, 0x74, 0x06,
  33394. 0xbc, 0xe7, 0xc5, 0x3c, 0x13, 0x99, 0x79, 0xb9
  33395. #else
  33396. 0xce, 0x06, 0x07, 0xbe, 0xf1, 0xa6, 0x1e, 0x36,
  33397. 0xef, 0xfa, 0xbc, 0x89, 0x71, 0xf3, 0x23, 0x9e,
  33398. 0x34, 0x6d, 0xae, 0x86, 0xae, 0x2b, 0xdc, 0xf4,
  33399. 0x4a, 0x27, 0xd5, 0x63, 0x59, 0x4f, 0x4a, 0x71
  33400. #endif
  33401. #else /* !NO_SHA */
  33402. #ifdef USE_CERT_BUFFERS_1024
  33403. 0x81, 0x69, 0x0f, 0xf8, 0xdf, 0xdd, 0xcf, 0x34,
  33404. 0x29, 0xd5, 0x67, 0x75, 0x71, 0x85, 0xc7, 0x75,
  33405. 0x10, 0x69, 0x59, 0xec,
  33406. #else
  33407. 0x33, 0xD8, 0x45, 0x66, 0xD7, 0x68, 0x87, 0x18,
  33408. 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26,
  33409. 0xD7, 0x85, 0x65, 0xC0
  33410. #endif
  33411. #endif /* !NO_SHA */
  33412. };
  33413. decoded = (byte *)XMALLOC(decodedSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33414. if (decoded == NULL) {
  33415. ret = MEMORY_E;
  33416. goto out;
  33417. }
  33418. pkcs7 = wc_PKCS7_New(HEAP_HINT, INVALID_DEVID);
  33419. if (pkcs7 == NULL) {
  33420. ret = MEMORY_E;
  33421. goto out;
  33422. }
  33423. /* Test verify */
  33424. ret = wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID);
  33425. if (ret != 0)
  33426. goto out;
  33427. ret = wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  33428. if (ret != 0)
  33429. goto out;
  33430. ret = wc_PKCS7_VerifySignedData(pkcs7, derBuf, derSz);
  33431. if (ret != 0)
  33432. goto out;
  33433. /* Get size of SID and print it out */
  33434. ret = wc_PKCS7_GetSignerSID(pkcs7, NULL, &sidSz);
  33435. if (ret != LENGTH_ONLY_E)
  33436. goto out;
  33437. sid = (byte*)XMALLOC(sidSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33438. if (sid == NULL) {
  33439. ret = MEMORY_E;
  33440. goto out;
  33441. }
  33442. ret = wc_PKCS7_GetSignerSID(pkcs7, sid, &sidSz);
  33443. if (ret != 0)
  33444. goto out;
  33445. ret = XMEMCMP(sid, expectedSid, sidSz);
  33446. if (ret != 0) {
  33447. ret = PKCS7_NO_SIGNER_E; /* close enough */
  33448. goto out;
  33449. }
  33450. /* get expected fwWrappedFirmwareKey */
  33451. if (keyHint == 0) {
  33452. ret = getFirmwareKey(pkcs7, key, keySz);
  33453. if (ret < 0)
  33454. goto out;
  33455. pkcs7->encryptionKey = key;
  33456. pkcs7->encryptionKeySz = ret;
  33457. }
  33458. else {
  33459. decodedSz = PKCS7_BUF_SIZE;
  33460. ret = wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc);
  33461. if (ret != 0)
  33462. goto out;
  33463. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)&usrCtx);
  33464. if (ret != 0)
  33465. goto out;
  33466. }
  33467. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  33468. pkcs7->contentSz, decoded, decodedSz);
  33469. if (decodedSz < 0) {
  33470. ret = decodedSz;
  33471. goto out;
  33472. }
  33473. ret = 0;
  33474. out:
  33475. if (decoded)
  33476. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33477. if (pkcs7)
  33478. wc_PKCS7_Free(pkcs7);
  33479. if (sid)
  33480. XFREE(sid, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33481. return ret;
  33482. }
  33483. WOLFSSL_TEST_SUBROUTINE int pkcs7callback_test(byte* cert, word32 certSz, byte* key, word32 keySz)
  33484. {
  33485. int ret = 0;
  33486. word32 derSz;
  33487. byte *derBuf = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33488. if (! derBuf)
  33489. ERROR_OUT(-12360, out);
  33490. /* Doing default generation and verify */
  33491. derSz = FOURK_BUF;
  33492. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 0, cert,
  33493. certSz, key, keySz);
  33494. if (ret <= 0) {
  33495. ERROR_OUT(-12361, out);
  33496. }
  33497. ret = verifyBundle(derBuf, derSz, 0);
  33498. if (ret != 0) {
  33499. ERROR_OUT(-12362, out);
  33500. }
  33501. /* test choosing other key with keyID */
  33502. derSz = FOURK_BUF;
  33503. ret = generateBundle(derBuf, &derSz, p7AltKey, sizeof(p7AltKey), 1,
  33504. cert, certSz, key, keySz);
  33505. if (ret <= 0) {
  33506. ERROR_OUT(-12363, out);
  33507. }
  33508. ret = verifyBundle(derBuf, derSz, 1);
  33509. if (ret != 0) {
  33510. ERROR_OUT(-12364, out);
  33511. }
  33512. /* test fail case with wrong keyID */
  33513. derSz = FOURK_BUF;
  33514. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 1,
  33515. cert, certSz, key, keySz);
  33516. if (ret <= 0) {
  33517. ERROR_OUT(-12365, out);
  33518. }
  33519. ret = verifyBundle(derBuf, derSz, 1);
  33520. if (ret == 0) {
  33521. ERROR_OUT(-12366, out);
  33522. }
  33523. ret = 0;
  33524. out:
  33525. if (derBuf)
  33526. XFREE(derBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33527. return ret;
  33528. }
  33529. #endif /* !NO_AES && HAVE_AES_CBC */
  33530. #ifndef NO_PKCS7_ENCRYPTED_DATA
  33531. typedef struct {
  33532. const byte* content;
  33533. word32 contentSz;
  33534. int contentOID;
  33535. int encryptOID;
  33536. byte* encryptionKey;
  33537. word32 encryptionKeySz;
  33538. PKCS7Attrib* attribs;
  33539. word32 attribsSz;
  33540. const char* outFileName;
  33541. } pkcs7EncryptedVector;
  33542. WOLFSSL_TEST_SUBROUTINE int pkcs7encrypted_test(void)
  33543. {
  33544. int ret = 0;
  33545. int i, testSz;
  33546. int encryptedSz, decodedSz, attribIdx;
  33547. PKCS7* pkcs7;
  33548. byte *encrypted;
  33549. byte *decoded;
  33550. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  33551. XFILE pkcs7File;
  33552. #endif
  33553. PKCS7Attrib* expectedAttrib;
  33554. PKCS7DecodedAttrib* decodedAttrib;
  33555. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  33556. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  33557. 0x72,0x6c,0x64
  33558. };
  33559. #ifndef NO_DES3
  33560. byte desKey[] = {
  33561. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  33562. };
  33563. byte des3Key[] = {
  33564. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  33565. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  33566. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  33567. };
  33568. #endif
  33569. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  33570. #ifdef WOLFSSL_AES_128
  33571. byte aes128Key[] = {
  33572. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33573. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  33574. };
  33575. #endif
  33576. #ifdef WOLFSSL_AES_192
  33577. byte aes192Key[] = {
  33578. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33579. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33580. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  33581. };
  33582. #endif
  33583. #ifdef WOLFSSL_AES_256
  33584. byte aes256Key[] = {
  33585. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33586. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33587. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33588. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  33589. };
  33590. #endif
  33591. #ifdef WOLFSSL_AES_256
  33592. /* Attribute example from RFC 4134, Section 7.2
  33593. * OID = 1.2.5555
  33594. * OCTET STRING = 'This is a test General ASN Attribute, number 1.' */
  33595. static byte genAttrOid[] = { 0x06, 0x03, 0x2a, 0xab, 0x33 };
  33596. static byte genAttr[] = { 0x04, 47,
  33597. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  33598. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  33599. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  33600. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  33601. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  33602. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x31, 0x2e };
  33603. static byte genAttrOid2[] = { 0x06, 0x03, 0x2a, 0xab, 0x34 };
  33604. static byte genAttr2[] = { 0x04, 47,
  33605. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  33606. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  33607. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  33608. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  33609. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  33610. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x32, 0x2e };
  33611. PKCS7Attrib attribs[] =
  33612. {
  33613. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) }
  33614. };
  33615. PKCS7Attrib multiAttribs[] =
  33616. {
  33617. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) },
  33618. { genAttrOid2, sizeof(genAttrOid2), genAttr2, sizeof(genAttr2) }
  33619. };
  33620. #endif
  33621. #endif /* NO_AES */
  33622. const pkcs7EncryptedVector testVectors[] =
  33623. {
  33624. #ifndef NO_DES3
  33625. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key),
  33626. NULL, 0, "pkcs7encryptedDataDES3.der"},
  33627. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey),
  33628. NULL, 0, "pkcs7encryptedDataDES.der"},
  33629. #endif /* NO_DES3 */
  33630. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  33631. #ifdef WOLFSSL_AES_128
  33632. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  33633. sizeof(aes128Key), NULL, 0, "pkcs7encryptedDataAES128CBC.der"},
  33634. #endif
  33635. #ifdef WOLFSSL_AES_192
  33636. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  33637. sizeof(aes192Key), NULL, 0, "pkcs7encryptedDataAES192CBC.der"},
  33638. #endif
  33639. #ifdef WOLFSSL_AES_256
  33640. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  33641. sizeof(aes256Key), NULL, 0, "pkcs7encryptedDataAES256CBC.der"},
  33642. /* test with optional unprotected attributes */
  33643. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  33644. sizeof(aes256Key), attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  33645. "pkcs7encryptedDataAES256CBC_attribs.der"},
  33646. /* test with multiple optional unprotected attributes */
  33647. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  33648. sizeof(aes256Key), multiAttribs,
  33649. (sizeof(multiAttribs)/sizeof(PKCS7Attrib)),
  33650. "pkcs7encryptedDataAES256CBC_multi_attribs.der"},
  33651. /* test with contentType set to FirmwarePkgData */
  33652. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256CBCb, aes256Key,
  33653. sizeof(aes256Key), NULL, 0,
  33654. "pkcs7encryptedDataAES256CBC_firmwarePkgData.der"},
  33655. #endif
  33656. #endif /* !NO_AES && HAVE_AES_CBC */
  33657. };
  33658. encrypted = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33659. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33660. if ((! encrypted) || (! decoded)) {
  33661. ERROR_OUT(MEMORY_E, out);
  33662. }
  33663. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  33664. for (i = 0; i < testSz; i++) {
  33665. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  33666. if (pkcs7 == NULL) {
  33667. ERROR_OUT(-12400, out);
  33668. }
  33669. pkcs7->content = (byte*)testVectors[i].content;
  33670. pkcs7->contentSz = testVectors[i].contentSz;
  33671. pkcs7->contentOID = testVectors[i].contentOID;
  33672. pkcs7->encryptOID = testVectors[i].encryptOID;
  33673. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  33674. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  33675. pkcs7->unprotectedAttribs = testVectors[i].attribs;
  33676. pkcs7->unprotectedAttribsSz = testVectors[i].attribsSz;
  33677. /* encode encryptedData */
  33678. encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  33679. PKCS7_BUF_SIZE);
  33680. if (encryptedSz <= 0) {
  33681. wc_PKCS7_Free(pkcs7);
  33682. ERROR_OUT(-12401, out);
  33683. }
  33684. /* decode encryptedData */
  33685. #ifndef NO_PKCS7_STREAM
  33686. { /* test reading byte by byte */
  33687. int z;
  33688. for (z = 0; z < encryptedSz; z++) {
  33689. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted + z, 1,
  33690. decoded, PKCS7_BUF_SIZE);
  33691. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  33692. printf("unexpected error %d\n", decodedSz);
  33693. ERROR_OUT(-12402, out);
  33694. }
  33695. }
  33696. /* test decode result */
  33697. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  33698. printf("stream read failed\n");
  33699. wc_PKCS7_Free(pkcs7);
  33700. ERROR_OUT(-12403, out);
  33701. }
  33702. }
  33703. #endif
  33704. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  33705. decoded, PKCS7_BUF_SIZE);
  33706. if (decodedSz <= 0){
  33707. wc_PKCS7_Free(pkcs7);
  33708. ERROR_OUT(-12404, out);
  33709. }
  33710. /* test decode result */
  33711. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  33712. wc_PKCS7_Free(pkcs7);
  33713. ERROR_OUT(-12405, out);
  33714. }
  33715. /* verify decoded unprotected attributes */
  33716. if (pkcs7->decodedAttrib != NULL) {
  33717. decodedAttrib = pkcs7->decodedAttrib;
  33718. attribIdx = 1;
  33719. while (decodedAttrib != NULL) {
  33720. /* expected attribute, stored list is reversed */
  33721. expectedAttrib = &(pkcs7->unprotectedAttribs
  33722. [pkcs7->unprotectedAttribsSz - attribIdx]);
  33723. /* verify oid */
  33724. if (XMEMCMP(decodedAttrib->oid, expectedAttrib->oid,
  33725. decodedAttrib->oidSz) != 0) {
  33726. wc_PKCS7_Free(pkcs7);
  33727. ERROR_OUT(-12406, out);
  33728. }
  33729. /* verify value */
  33730. if (XMEMCMP(decodedAttrib->value, expectedAttrib->value,
  33731. decodedAttrib->valueSz) != 0) {
  33732. wc_PKCS7_Free(pkcs7);
  33733. ERROR_OUT(-12407, out);
  33734. }
  33735. decodedAttrib = decodedAttrib->next;
  33736. attribIdx++;
  33737. }
  33738. }
  33739. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  33740. /* output pkcs7 envelopedData for external testing */
  33741. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  33742. if (!pkcs7File) {
  33743. wc_PKCS7_Free(pkcs7);
  33744. ERROR_OUT(-12408, out);
  33745. }
  33746. ret = (int)XFWRITE(encrypted, encryptedSz, 1, pkcs7File);
  33747. XFCLOSE(pkcs7File);
  33748. if (ret > 0)
  33749. ret = 0;
  33750. #endif
  33751. wc_PKCS7_Free(pkcs7);
  33752. }
  33753. out:
  33754. if (encrypted)
  33755. XFREE(encrypted, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33756. if (decoded)
  33757. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33758. return ret;
  33759. }
  33760. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  33761. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  33762. typedef struct {
  33763. const byte* content;
  33764. word32 contentSz;
  33765. int contentOID;
  33766. const char* outFileName;
  33767. } pkcs7CompressedVector;
  33768. WOLFSSL_TEST_SUBROUTINE int pkcs7compressed_test(void)
  33769. {
  33770. int ret = 0;
  33771. int i, testSz;
  33772. int compressedSz, decodedSz;
  33773. PKCS7* pkcs7;
  33774. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33775. byte *compressed;
  33776. byte *decoded;
  33777. #else
  33778. byte compressed[PKCS7_BUF_SIZE];
  33779. byte decoded[PKCS7_BUF_SIZE];
  33780. #endif
  33781. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  33782. XFILE pkcs7File;
  33783. #endif
  33784. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  33785. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  33786. 0x72,0x6c,0x64
  33787. };
  33788. const pkcs7CompressedVector testVectors[] =
  33789. {
  33790. {data, (word32)sizeof(data), DATA,
  33791. "pkcs7compressedData_data_zlib.der"},
  33792. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA,
  33793. "pkcs7compressedData_firmwarePkgData_zlib.der"},
  33794. };
  33795. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33796. compressed = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33797. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33798. if ((! compressed) || (! decoded)) {
  33799. ERROR_OUT(MEMORY_E, out);
  33800. }
  33801. #endif
  33802. testSz = sizeof(testVectors) / sizeof(pkcs7CompressedVector);
  33803. for (i = 0; i < testSz; i++) {
  33804. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  33805. if (pkcs7 == NULL) {
  33806. ERROR_OUT(-12500, out);
  33807. }
  33808. pkcs7->content = (byte*)testVectors[i].content;
  33809. pkcs7->contentSz = testVectors[i].contentSz;
  33810. pkcs7->contentOID = testVectors[i].contentOID;
  33811. /* encode compressedData */
  33812. compressedSz = wc_PKCS7_EncodeCompressedData(pkcs7, compressed,
  33813. PKCS7_BUF_SIZE);
  33814. if (compressedSz <= 0) {
  33815. wc_PKCS7_Free(pkcs7);
  33816. ERROR_OUT(-12501, out);
  33817. }
  33818. /* decode compressedData */
  33819. decodedSz = wc_PKCS7_DecodeCompressedData(pkcs7, compressed,
  33820. compressedSz, decoded,
  33821. PKCS7_BUF_SIZE);
  33822. if (decodedSz <= 0){
  33823. wc_PKCS7_Free(pkcs7);
  33824. ERROR_OUT(-12502, out);
  33825. }
  33826. /* test decode result */
  33827. if (XMEMCMP(decoded, testVectors[i].content,
  33828. testVectors[i].contentSz) != 0) {
  33829. wc_PKCS7_Free(pkcs7);
  33830. ERROR_OUT(-12503, out);
  33831. }
  33832. /* make sure content type is the same */
  33833. if (testVectors[i].contentOID != pkcs7->contentOID) {
  33834. ERROR_OUT(-12504, out);
  33835. }
  33836. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  33837. /* output pkcs7 compressedData for external testing */
  33838. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  33839. if (!pkcs7File) {
  33840. wc_PKCS7_Free(pkcs7);
  33841. ERROR_OUT(-12505, out);
  33842. }
  33843. ret = (int)XFWRITE(compressed, compressedSz, 1, pkcs7File);
  33844. XFCLOSE(pkcs7File);
  33845. if (ret > 0)
  33846. ret = 0;
  33847. #endif
  33848. wc_PKCS7_Free(pkcs7);
  33849. }
  33850. out:
  33851. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33852. if (compressed)
  33853. XFREE(compressed, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33854. if (decoded)
  33855. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33856. #endif
  33857. return ret;
  33858. } /* pkcs7compressed_test() */
  33859. #undef PKCS7_BUF_SIZE
  33860. #endif /* HAVE_LIBZ */
  33861. typedef struct {
  33862. const byte* content;
  33863. word32 contentSz;
  33864. int hashOID;
  33865. int signOID;
  33866. byte* privateKey;
  33867. word32 privateKeySz;
  33868. byte* cert;
  33869. size_t certSz;
  33870. byte* caCert;
  33871. size_t caCertSz;
  33872. PKCS7Attrib* signedAttribs;
  33873. word32 signedAttribsSz;
  33874. const char* outFileName;
  33875. int contentOID;
  33876. byte* contentType;
  33877. word32 contentTypeSz;
  33878. int sidType;
  33879. int encryptOID; /* for single-shot encrypt alg OID */
  33880. int encCompFlag; /* for single-shot. 1 = enc, 2 = comp, 3 = both*/
  33881. byte* encryptKey; /* for single-shot, encryptedData */
  33882. word32 encryptKeySz; /* for single-shot, encryptedData */
  33883. PKCS7Attrib* unprotectedAttribs; /* for single-shot, encryptedData */
  33884. word32 unprotectedAttribsSz; /* for single-shot, encryptedData */
  33885. word16 detachedSignature; /* generate detached signature (0:1) */
  33886. } pkcs7SignedVector;
  33887. static int pkcs7signed_run_vectors(
  33888. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  33889. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  33890. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  33891. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  33892. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  33893. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  33894. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  33895. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  33896. {
  33897. int ret, testSz = 0, i;
  33898. int encodedSz;
  33899. byte* out = NULL;
  33900. word32 outSz;
  33901. WC_RNG rng;
  33902. PKCS7* pkcs7 = NULL;
  33903. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  33904. XFILE file;
  33905. #endif
  33906. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  33907. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  33908. 0x72,0x6c,0x64
  33909. };
  33910. static byte transIdOid[] =
  33911. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  33912. 0x09, 0x07 };
  33913. static byte messageTypeOid[] =
  33914. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  33915. 0x09, 0x02 };
  33916. static byte senderNonceOid[] =
  33917. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  33918. 0x09, 0x05 };
  33919. #ifndef NO_SHA
  33920. static byte transId[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  33921. #else
  33922. static byte transId[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  33923. #endif
  33924. static byte messageType[] = { 0x13, 2, '1', '9' };
  33925. static byte senderNonce[PKCS7_NONCE_SZ + 2];
  33926. static PKCS7Attrib attribs[] =
  33927. {
  33928. { transIdOid, sizeof(transIdOid), transId,
  33929. sizeof(transId) - 1 }, /* take off the null */
  33930. { messageTypeOid, sizeof(messageTypeOid), messageType,
  33931. sizeof(messageType) },
  33932. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  33933. sizeof(senderNonce) }
  33934. };
  33935. /* for testing custom contentType, FirmwarePkgData */
  33936. static byte customContentType[] = { 0x06, 0x0B, 0x2A, 0x86,
  33937. 0x48, 0x86, 0xF7, 0x0D,
  33938. 0x01, 0x09, 0x10, 0x01, 0x10 };
  33939. #define MAX_TESTVECTORS_LEN 20
  33940. #define ADD_PKCS7SIGNEDVECTOR(...) { \
  33941. pkcs7SignedVector _this_vector = { __VA_ARGS__ }; \
  33942. if (testSz == MAX_TESTVECTORS_LEN) { \
  33943. ret = -12534; \
  33944. goto out; \
  33945. } \
  33946. XMEMCPY(&testVectors[testSz++], &_this_vector, sizeof _this_vector);\
  33947. }
  33948. pkcs7SignedVector *testVectors = NULL;
  33949. XMEMSET(&rng, 0, sizeof(rng));
  33950. testVectors = (pkcs7SignedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  33951. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33952. if (testVectors == NULL) {
  33953. ret = -12567;
  33954. goto out;
  33955. }
  33956. {
  33957. #ifndef NO_RSA
  33958. #ifndef NO_SHA
  33959. /* RSA with SHA */
  33960. ADD_PKCS7SIGNEDVECTOR(
  33961. data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  33962. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  33963. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  33964. "pkcs7signedData_RSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0, NULL,
  33965. 0, 0);
  33966. /* RSA with SHA, no signed attributes */
  33967. ADD_PKCS7SIGNEDVECTOR(
  33968. data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  33969. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz,
  33970. NULL, 0, NULL, 0,
  33971. "pkcs7signedData_RSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  33972. NULL, 0, 0);
  33973. #endif
  33974. #ifdef WOLFSSL_SHA224
  33975. /* RSA with SHA224 */
  33976. ADD_PKCS7SIGNEDVECTOR(
  33977. data, (word32)sizeof(data), SHA224h, RSAk, rsaClientPrivKeyBuf,
  33978. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  33979. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  33980. "pkcs7signedData_RSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  33981. NULL, 0, 0);
  33982. #endif
  33983. #ifndef NO_SHA256
  33984. /* RSA with SHA256 */
  33985. ADD_PKCS7SIGNEDVECTOR(
  33986. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  33987. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  33988. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  33989. "pkcs7signedData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  33990. NULL, 0, 0);
  33991. /* RSA with SHA256, detached signature */
  33992. ADD_PKCS7SIGNEDVECTOR(
  33993. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  33994. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  33995. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  33996. "pkcs7signedData_RSA_SHA256_detachedSig.der", 0, NULL, 0, 0, 0, 0,
  33997. NULL, 0, NULL, 0, 1);
  33998. /* RSA with SHA256 and SubjectKeyIdentifier in SignerIdentifier */
  33999. ADD_PKCS7SIGNEDVECTOR(
  34000. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34001. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34002. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34003. "pkcs7signedData_RSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  34004. NULL, 0, NULL, 0, 0);
  34005. /* RSA with SHA256 and custom contentType */
  34006. ADD_PKCS7SIGNEDVECTOR(
  34007. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34008. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34009. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34010. "pkcs7signedData_RSA_SHA256_custom_contentType.der", 0,
  34011. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  34012. NULL, 0, 0);
  34013. /* RSA with SHA256 and FirmwarePkgData contentType */
  34014. ADD_PKCS7SIGNEDVECTOR(
  34015. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34016. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34017. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34018. "pkcs7signedData_RSA_SHA256_firmwarePkgData.der",
  34019. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  34020. /* RSA with SHA256 using server cert and ca cert */
  34021. ADD_PKCS7SIGNEDVECTOR(
  34022. data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  34023. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  34024. rsaCaCertBuf, rsaCaCertBufSz,
  34025. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34026. "pkcs7signedData_RSA_SHA256_with_ca_cert.der", 0, NULL, 0, 0, 0, 0,
  34027. NULL, 0, NULL, 0, 0);
  34028. #endif
  34029. #if defined(WOLFSSL_SHA384)
  34030. /* RSA with SHA384 */
  34031. ADD_PKCS7SIGNEDVECTOR(
  34032. data, (word32)sizeof(data), SHA384h, RSAk, rsaClientPrivKeyBuf,
  34033. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34034. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34035. "pkcs7signedData_RSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  34036. NULL, 0, 0);
  34037. #endif
  34038. #if defined(WOLFSSL_SHA512)
  34039. /* RSA with SHA512 */
  34040. ADD_PKCS7SIGNEDVECTOR(
  34041. data, (word32)sizeof(data), SHA512h, RSAk, rsaClientPrivKeyBuf,
  34042. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34043. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34044. "pkcs7signedData_RSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  34045. NULL, 0, 0);
  34046. #endif
  34047. #endif /* NO_RSA */
  34048. #ifdef HAVE_ECC
  34049. #ifndef NO_SHA
  34050. /* ECDSA with SHA */
  34051. ADD_PKCS7SIGNEDVECTOR(
  34052. data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  34053. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34054. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34055. "pkcs7signedData_ECDSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  34056. NULL, 0, 0);
  34057. /* ECDSA with SHA, no signed attributes */
  34058. ADD_PKCS7SIGNEDVECTOR(
  34059. data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  34060. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz,
  34061. NULL, 0, NULL, 0,
  34062. "pkcs7signedData_ECDSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  34063. NULL, 0, 0);
  34064. #endif
  34065. #ifdef WOLFSSL_SHA224
  34066. /* ECDSA with SHA224 */
  34067. ADD_PKCS7SIGNEDVECTOR(
  34068. data, (word32)sizeof(data), SHA224h, ECDSAk, eccClientPrivKeyBuf,
  34069. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34070. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34071. "pkcs7signedData_ECDSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  34072. NULL, 0, 0);
  34073. #endif
  34074. #ifndef NO_SHA256
  34075. /* ECDSA with SHA256 */
  34076. ADD_PKCS7SIGNEDVECTOR(
  34077. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34078. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34079. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34080. "pkcs7signedData_ECDSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  34081. NULL, 0, 0);
  34082. /* ECDSA with SHA256 and SubjectKeyIdentifier in SigherIdentifier */
  34083. ADD_PKCS7SIGNEDVECTOR(
  34084. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34085. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34086. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34087. "pkcs7signedData_ECDSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  34088. NULL, 0, NULL, 0, 0);
  34089. /* ECDSA with SHA256 and custom contentType */
  34090. ADD_PKCS7SIGNEDVECTOR(
  34091. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34092. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34093. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34094. "pkcs7signedData_ECDSA_SHA256_custom_contentType.der", 0,
  34095. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  34096. NULL, 0, 0);
  34097. /* ECDSA with SHA256 and FirmwarePkgData contentType */
  34098. ADD_PKCS7SIGNEDVECTOR(
  34099. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34100. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34101. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34102. "pkcs7signedData_ECDSA_SHA256_firmwarePkgData.der",
  34103. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  34104. #endif
  34105. #ifdef WOLFSSL_SHA384
  34106. /* ECDSA with SHA384 */
  34107. ADD_PKCS7SIGNEDVECTOR(
  34108. data, (word32)sizeof(data), SHA384h, ECDSAk, eccClientPrivKeyBuf,
  34109. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34110. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34111. "pkcs7signedData_ECDSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  34112. NULL, 0, 0);
  34113. #endif
  34114. #ifdef WOLFSSL_SHA512
  34115. /* ECDSA with SHA512 */
  34116. ADD_PKCS7SIGNEDVECTOR(
  34117. data, (word32)sizeof(data), SHA512h, ECDSAk, eccClientPrivKeyBuf,
  34118. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34119. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34120. "pkcs7signedData_ECDSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  34121. NULL, 0, 0);
  34122. #endif
  34123. #endif /* HAVE_ECC */
  34124. };
  34125. #undef MAX_TESTVECTORS_LEN
  34126. #undef ADD_PKCS7SIGNEDVECTOR
  34127. outSz = FOURK_BUF;
  34128. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34129. if (out == NULL)
  34130. ERROR_OUT(-12510, out);
  34131. XMEMSET(out, 0, outSz);
  34132. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  34133. if (ret < 0)
  34134. ERROR_OUT(-12511, out);
  34135. #ifndef HAVE_FIPS
  34136. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  34137. #else
  34138. ret = wc_InitRng(&rng);
  34139. #endif
  34140. if (ret != 0)
  34141. ERROR_OUT(-12512, out);
  34142. for (i = 0; i < testSz; i++) {
  34143. if (pkcs7)
  34144. wc_PKCS7_Free(pkcs7);
  34145. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  34146. if (pkcs7 == NULL)
  34147. ERROR_OUT(-12513, out);
  34148. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  34149. (word32)testVectors[i].certSz);
  34150. if (ret != 0)
  34151. ERROR_OUT(-12514, out);
  34152. /* load CA certificate, if present */
  34153. if (testVectors[i].caCert != NULL) {
  34154. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  34155. (word32)testVectors[i].caCertSz);
  34156. if (ret != 0)
  34157. ERROR_OUT(-12515, out);
  34158. }
  34159. pkcs7->rng = &rng;
  34160. pkcs7->content = (byte*)testVectors[i].content;
  34161. pkcs7->contentSz = testVectors[i].contentSz;
  34162. pkcs7->contentOID = testVectors[i].contentOID;
  34163. pkcs7->hashOID = testVectors[i].hashOID;
  34164. pkcs7->encryptOID = testVectors[i].signOID;
  34165. pkcs7->privateKey = testVectors[i].privateKey;
  34166. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  34167. pkcs7->signedAttribs = testVectors[i].signedAttribs;
  34168. pkcs7->signedAttribsSz = testVectors[i].signedAttribsSz;
  34169. /* optional custom contentType, default is DATA,
  34170. overrides contentOID if set */
  34171. if (testVectors[i].contentType != NULL) {
  34172. ret = wc_PKCS7_SetContentType(pkcs7, testVectors[i].contentType,
  34173. testVectors[i].contentTypeSz);
  34174. if (ret != 0)
  34175. ERROR_OUT(-12516, out);
  34176. }
  34177. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  34178. default is IssuerAndSerialNumber */
  34179. if (testVectors[i].sidType == CMS_SKID) {
  34180. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  34181. if (ret != 0)
  34182. ERROR_OUT(-12517, out);
  34183. }
  34184. /* generate senderNonce */
  34185. {
  34186. senderNonce[0] = 0x04;
  34187. senderNonce[1] = PKCS7_NONCE_SZ;
  34188. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  34189. if (ret != 0)
  34190. ERROR_OUT(-12518, out);
  34191. }
  34192. /* generate transactionID (used with SCEP) */
  34193. {
  34194. #ifndef NO_SHA
  34195. wc_Sha sha;
  34196. byte digest[WC_SHA_DIGEST_SIZE];
  34197. #else
  34198. wc_Sha256 sha;
  34199. byte digest[WC_SHA256_DIGEST_SIZE];
  34200. #endif
  34201. int j,k;
  34202. transId[0] = 0x13;
  34203. transId[1] = sizeof(digest) * 2;
  34204. #ifndef NO_SHA
  34205. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  34206. if (ret != 0)
  34207. ERROR_OUT(-12519, out);
  34208. wc_ShaUpdate(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  34209. wc_ShaFinal(&sha, digest);
  34210. wc_ShaFree(&sha);
  34211. #else
  34212. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  34213. if (ret != 0)
  34214. ERROR_OUT(-12520, out);
  34215. wc_Sha256Update(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  34216. wc_Sha256Final(&sha, digest);
  34217. wc_Sha256Free(&sha);
  34218. #endif
  34219. for (j = 0, k = 2; j < (int)sizeof(digest); j++, k += 2) {
  34220. #if defined(WOLF_C89)
  34221. XSPRINTF((char*)&transId[k], "%02x", digest[j]);
  34222. #else
  34223. (void)XSNPRINTF((char*)&transId[k], 3, "%02x", digest[j]);
  34224. #endif
  34225. }
  34226. }
  34227. /* enable detached signature generation, if set */
  34228. if (testVectors[i].detachedSignature == 1) {
  34229. ret = wc_PKCS7_SetDetached(pkcs7, 1);
  34230. if (ret != 0)
  34231. ERROR_OUT(-12521, out);
  34232. }
  34233. encodedSz = wc_PKCS7_EncodeSignedData(pkcs7, out, outSz);
  34234. if (encodedSz < 0)
  34235. ERROR_OUT(-12522, out);
  34236. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  34237. /* write PKCS#7 to output file for more testing */
  34238. file = XFOPEN(testVectors[i].outFileName, "wb");
  34239. if (!file) {
  34240. ERROR_OUT(-12523, out);
  34241. }
  34242. ret = (int)XFWRITE(out, 1, encodedSz, file);
  34243. XFCLOSE(file);
  34244. if (ret != (int)encodedSz)
  34245. ERROR_OUT(-12524, out);
  34246. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  34247. wc_PKCS7_Free(pkcs7);
  34248. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  34249. if (pkcs7 == NULL)
  34250. ERROR_OUT(-12525, out);
  34251. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  34252. if (testVectors[i].detachedSignature == 1) {
  34253. /* set content for verifying detached signatures */
  34254. pkcs7->content = (byte*)testVectors[i].content;
  34255. pkcs7->contentSz = testVectors[i].contentSz;
  34256. }
  34257. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  34258. if (ret < 0)
  34259. ERROR_OUT(-12526, out);
  34260. /* verify contentType extracted successfully for custom content types */
  34261. if (testVectors[i].contentTypeSz > 0) {
  34262. if (pkcs7->contentTypeSz != testVectors[i].contentTypeSz) {
  34263. ERROR_OUT(-12527, out);
  34264. } else if (XMEMCMP(pkcs7->contentType, testVectors[i].contentType,
  34265. pkcs7->contentTypeSz) != 0) {
  34266. ERROR_OUT(-12528, out);
  34267. }
  34268. }
  34269. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0)
  34270. ERROR_OUT(-12529, out);
  34271. {
  34272. /* check getting signed attributes */
  34273. #ifndef NO_SHA
  34274. byte buf[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  34275. #else
  34276. byte buf[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  34277. #endif
  34278. byte* oidPt = transIdOid + 2; /* skip object id tag and size */
  34279. int oidSz = (int)sizeof(transIdOid) - 2;
  34280. int bufSz = 0;
  34281. if (testVectors[i].signedAttribs != NULL &&
  34282. wc_PKCS7_GetAttributeValue(pkcs7, oidPt, oidSz,
  34283. NULL, (word32*)&bufSz) != LENGTH_ONLY_E)
  34284. ERROR_OUT(-12530, out);
  34285. if (bufSz > (int)sizeof(buf))
  34286. ERROR_OUT(-12531, out);
  34287. bufSz = wc_PKCS7_GetAttributeValue(pkcs7, oidPt, oidSz,
  34288. buf, (word32*)&bufSz);
  34289. if ((testVectors[i].signedAttribs != NULL && bufSz < 0) ||
  34290. (testVectors[i].signedAttribs == NULL && bufSz > 0))
  34291. ERROR_OUT(-12532, out);
  34292. }
  34293. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  34294. file = XFOPEN("./pkcs7cert.der", "wb");
  34295. if (!file)
  34296. ERROR_OUT(-12533, out);
  34297. ret = (int)XFWRITE(pkcs7->singleCert, 1, pkcs7->singleCertSz, file);
  34298. XFCLOSE(file);
  34299. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  34300. }
  34301. out:
  34302. if (pkcs7 != NULL)
  34303. wc_PKCS7_Free(pkcs7);
  34304. if (out != NULL)
  34305. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34306. if (testVectors != NULL)
  34307. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34308. wc_FreeRng(&rng);
  34309. if (ret > 0)
  34310. return 0;
  34311. (void)rsaClientCertBuf;
  34312. (void)rsaClientCertBufSz;
  34313. (void)rsaClientPrivKeyBuf;
  34314. (void)rsaClientPrivKeyBufSz;
  34315. (void)rsaServerCertBuf;
  34316. (void)rsaServerCertBufSz;
  34317. (void)rsaServerPrivKeyBuf;
  34318. (void)rsaServerPrivKeyBufSz;
  34319. (void)rsaCaCertBuf;
  34320. (void)rsaCaCertBufSz;
  34321. (void)rsaCaPrivKeyBuf;
  34322. (void)rsaCaPrivKeyBufSz;
  34323. (void)eccClientCertBuf;
  34324. (void)eccClientCertBufSz;
  34325. (void)eccClientPrivKeyBuf;
  34326. (void)eccClientPrivKeyBufSz;
  34327. return ret;
  34328. }
  34329. static int pkcs7signed_run_SingleShotVectors(
  34330. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  34331. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  34332. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  34333. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  34334. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  34335. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  34336. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  34337. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  34338. {
  34339. int ret, testSz = 0, i;
  34340. int encodedSz;
  34341. byte* out = NULL;
  34342. word32 outSz;
  34343. WC_RNG rng;
  34344. PKCS7* pkcs7 = NULL;
  34345. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  34346. XFILE file;
  34347. #endif
  34348. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA) && \
  34349. !defined(NO_PKCS7_ENCRYPTED_DATA)
  34350. byte* encryptedTmp = NULL;
  34351. int encryptedTmpSz;
  34352. #endif
  34353. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  34354. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  34355. 0x72,0x6c,0x64
  34356. };
  34357. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  34358. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  34359. static byte aes256Key[] = {
  34360. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  34361. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  34362. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  34363. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  34364. };
  34365. #endif
  34366. static byte messageTypeOid[] =
  34367. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  34368. 0x09, 0x02 };
  34369. static byte messageType[] = { 0x13, 2, '1', '9' };
  34370. PKCS7Attrib attribs[] =
  34371. {
  34372. { messageTypeOid, sizeof(messageTypeOid), messageType,
  34373. sizeof(messageType) },
  34374. };
  34375. #define MAX_TESTVECTORS_LEN 19
  34376. #define ADD_PKCS7SIGNEDVECTOR(...) { \
  34377. pkcs7SignedVector _this_vector = { __VA_ARGS__ }; \
  34378. if (testSz == MAX_TESTVECTORS_LEN) { \
  34379. ret = -12568; \
  34380. goto out; \
  34381. } \
  34382. XMEMCPY(&testVectors[testSz++], &_this_vector, sizeof _this_vector);\
  34383. }
  34384. pkcs7SignedVector *testVectors = NULL;
  34385. XMEMSET(&rng, 0, sizeof(rng));
  34386. testVectors = (pkcs7SignedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  34387. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34388. if (testVectors == NULL) {
  34389. ret = -12567;
  34390. goto out;
  34391. }
  34392. {
  34393. #ifndef NO_RSA
  34394. #ifndef NO_SHA256
  34395. /* Signed FirmwarePkgData, RSA, SHA256, no attribs */
  34396. ADD_PKCS7SIGNEDVECTOR(
  34397. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34398. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34399. NULL, 0,
  34400. "pkcs7signedFirmwarePkgData_RSA_SHA256_noattr.der", 0, NULL, 0, 0,
  34401. 0, 0, NULL, 0, NULL, 0, 0);
  34402. /* Signed FirmwarePkgData, RSA, SHA256, attrs */
  34403. ADD_PKCS7SIGNEDVECTOR(
  34404. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34405. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34406. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34407. "pkcs7signedFirmwarePkgData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0,
  34408. NULL, 0, NULL, 0, 0);
  34409. /* Signed FirmwarePkgData, RSA, SHA256, SubjectKeyIdentifier, attrs */
  34410. ADD_PKCS7SIGNEDVECTOR(
  34411. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34412. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34413. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34414. "pkcs7signedFirmwarePkgData_RSA_SHA256_SKID.der", 0, NULL,
  34415. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0);
  34416. /* Signed FirmwraePkgData, RSA, SHA256, server cert and ca cert, attr */
  34417. ADD_PKCS7SIGNEDVECTOR(
  34418. data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  34419. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  34420. rsaCaCertBuf, rsaCaCertBufSz,
  34421. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34422. "pkcs7signedFirmwarePkgData_RSA_SHA256_with_ca_cert.der", 0, NULL,
  34423. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  34424. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  34425. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  34426. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, no attribs */
  34427. ADD_PKCS7SIGNEDVECTOR(
  34428. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34429. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34430. NULL, 0,
  34431. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  34432. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0);
  34433. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, attribs */
  34434. ADD_PKCS7SIGNEDVECTOR(
  34435. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34436. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34437. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34438. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256.der", 0,
  34439. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  34440. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  34441. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  34442. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  34443. /* Signed Compressed FirmwarePkgData, RSA, SHA256, no attribs */
  34444. ADD_PKCS7SIGNEDVECTOR(
  34445. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34446. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34447. NULL, 0,
  34448. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  34449. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  34450. /* Signed Compressed FirmwarePkgData, RSA, SHA256, attribs */
  34451. ADD_PKCS7SIGNEDVECTOR(
  34452. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34453. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34454. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34455. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256.der", 0,
  34456. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  34457. #ifndef NO_PKCS7_ENCRYPTED_DATA
  34458. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  34459. no attribs */
  34460. ADD_PKCS7SIGNEDVECTOR(
  34461. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34462. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34463. NULL, 0,
  34464. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256_noattr.der",
  34465. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  34466. 0, 0);
  34467. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  34468. attribs */
  34469. ADD_PKCS7SIGNEDVECTOR(
  34470. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34471. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34472. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34473. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256.der",
  34474. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  34475. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  34476. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  34477. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  34478. #endif /* NO_SHA256 */
  34479. #endif /* NO_RSA */
  34480. #ifdef HAVE_ECC
  34481. #ifndef NO_SHA256
  34482. /* Signed FirmwarePkgData, ECDSA, SHA256, no attribs */
  34483. ADD_PKCS7SIGNEDVECTOR(
  34484. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34485. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34486. NULL, 0,
  34487. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  34488. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  34489. /* Signed FirmwarePkgData, ECDSA, SHA256, attribs */
  34490. ADD_PKCS7SIGNEDVECTOR(
  34491. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34492. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34493. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34494. "pkcs7signedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  34495. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  34496. /* Signed FirmwarePkgData, ECDSA, SHA256, SubjectKeyIdentifier, attr */
  34497. ADD_PKCS7SIGNEDVECTOR(
  34498. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34499. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34500. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34501. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_SKID.der", 0, NULL,
  34502. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0);
  34503. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  34504. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  34505. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, no attribs */
  34506. ADD_PKCS7SIGNEDVECTOR(
  34507. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34508. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34509. NULL, 0,
  34510. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  34511. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0);
  34512. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, attribs */
  34513. ADD_PKCS7SIGNEDVECTOR(
  34514. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34515. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34516. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34517. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  34518. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  34519. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  34520. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  34521. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  34522. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, no attribs */
  34523. ADD_PKCS7SIGNEDVECTOR(
  34524. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34525. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34526. NULL, 0,
  34527. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  34528. 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  34529. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, attrib */
  34530. ADD_PKCS7SIGNEDVECTOR(
  34531. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34532. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34533. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34534. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  34535. 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  34536. #ifndef NO_PKCS7_ENCRYPTED_DATA
  34537. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  34538. no attribs */
  34539. ADD_PKCS7SIGNEDVECTOR(
  34540. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34541. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34542. NULL, 0,
  34543. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der",
  34544. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  34545. 0, 0);
  34546. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  34547. attribs */
  34548. ADD_PKCS7SIGNEDVECTOR(
  34549. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34550. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34551. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34552. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256.der",
  34553. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  34554. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  34555. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  34556. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  34557. #endif /* NO_SHA256 */
  34558. #endif /* HAVE_ECC */
  34559. };
  34560. #undef MAX_TESTVECTORS_LEN
  34561. #undef ADD_PKCS7SIGNEDVECTOR
  34562. outSz = FOURK_BUF;
  34563. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34564. if (out == NULL)
  34565. ERROR_OUT(-12540, out);
  34566. XMEMSET(out, 0, outSz);
  34567. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  34568. if (ret < 0)
  34569. ERROR_OUT(-12541, out);
  34570. #ifndef HAVE_FIPS
  34571. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  34572. #else
  34573. ret = wc_InitRng(&rng);
  34574. #endif
  34575. if (ret != 0)
  34576. ERROR_OUT(-12542, out);
  34577. for (i = 0; i < testSz; i++) {
  34578. if (pkcs7)
  34579. wc_PKCS7_Free(pkcs7);
  34580. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  34581. if (pkcs7 == NULL)
  34582. ERROR_OUT(-12543, out);
  34583. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  34584. (word32)testVectors[i].certSz);
  34585. if (ret != 0)
  34586. ERROR_OUT(-12544, out);
  34587. /* load CA certificate, if present */
  34588. if (testVectors[i].caCert != NULL) {
  34589. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  34590. (word32)testVectors[i].caCertSz);
  34591. if (ret != 0)
  34592. ERROR_OUT(-12545, out);
  34593. }
  34594. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  34595. default is IssuerAndSerialNumber */
  34596. if (testVectors[i].sidType == CMS_SKID) {
  34597. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  34598. if (ret != 0)
  34599. ERROR_OUT(-12546, out);
  34600. }
  34601. if (testVectors[i].encCompFlag == 0) {
  34602. /* encode Signed FirmwarePkgData */
  34603. encodedSz = wc_PKCS7_EncodeSignedFPD(pkcs7,
  34604. testVectors[i].privateKey, testVectors[i].privateKeySz,
  34605. testVectors[i].signOID, testVectors[i].hashOID,
  34606. (byte*)testVectors[i].content, testVectors[i].contentSz,
  34607. testVectors[i].signedAttribs,
  34608. testVectors[i].signedAttribsSz, out, outSz);
  34609. if (encodedSz < 0)
  34610. ERROR_OUT(-12547, out);
  34611. #ifndef NO_PKCS7_ENCRYPTED_DATA
  34612. } else if (testVectors[i].encCompFlag == 1) {
  34613. /* encode Signed Encrypted FirmwarePkgData */
  34614. encodedSz = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7,
  34615. testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  34616. testVectors[i].privateKey, testVectors[i].privateKeySz,
  34617. testVectors[i].encryptOID, testVectors[i].signOID,
  34618. testVectors[i].hashOID, (byte*)testVectors[i].content,
  34619. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  34620. testVectors[i].unprotectedAttribsSz,
  34621. testVectors[i].signedAttribs,
  34622. testVectors[i].signedAttribsSz, out, outSz);
  34623. if (encodedSz <= 0)
  34624. ERROR_OUT(-12548, out);
  34625. #endif
  34626. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  34627. } else if (testVectors[i].encCompFlag == 2) {
  34628. /* encode Signed Compressed FirmwarePkgData */
  34629. encodedSz = wc_PKCS7_EncodeSignedCompressedFPD(pkcs7,
  34630. testVectors[i].privateKey, testVectors[i].privateKeySz,
  34631. testVectors[i].signOID, testVectors[i].hashOID,
  34632. (byte*)testVectors[i].content, testVectors[i].contentSz,
  34633. testVectors[i].signedAttribs,
  34634. testVectors[i].signedAttribsSz, out, outSz);
  34635. if (encodedSz <= 0)
  34636. ERROR_OUT(-12549, out);
  34637. #ifndef NO_PKCS7_ENCRYPTED_DATA
  34638. } else if (testVectors[i].encCompFlag == 3) {
  34639. /* encode Signed Encrypted Compressed FirmwarePkgData */
  34640. encodedSz = wc_PKCS7_EncodeSignedEncryptedCompressedFPD(pkcs7,
  34641. testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  34642. testVectors[i].privateKey, testVectors[i].privateKeySz,
  34643. testVectors[i].encryptOID, testVectors[i].signOID,
  34644. testVectors[i].hashOID, (byte*)testVectors[i].content,
  34645. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  34646. testVectors[i].unprotectedAttribsSz,
  34647. testVectors[i].signedAttribs,
  34648. testVectors[i].signedAttribsSz, out, outSz);
  34649. if (encodedSz <= 0)
  34650. ERROR_OUT(-12550, out);
  34651. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  34652. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  34653. } else {
  34654. /* unsupported SignedData single-shot combination */
  34655. ERROR_OUT(-12551, out);
  34656. }
  34657. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  34658. /* write PKCS#7 to output file for more testing */
  34659. file = XFOPEN(testVectors[i].outFileName, "wb");
  34660. if (!file)
  34661. ERROR_OUT(-12552, out);
  34662. ret = (int)XFWRITE(out, 1, encodedSz, file);
  34663. XFCLOSE(file);
  34664. file = NULL;
  34665. if (ret != (int)encodedSz)
  34666. ERROR_OUT(-12553, out);
  34667. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  34668. wc_PKCS7_Free(pkcs7);
  34669. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  34670. if (pkcs7 == NULL)
  34671. ERROR_OUT(-12554, out);
  34672. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  34673. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  34674. if (ret < 0)
  34675. ERROR_OUT(-12555, out);
  34676. #ifndef NO_PKCS7_STREAM
  34677. {
  34678. word32 z;
  34679. for (z = 0; z < outSz && ret != 0; z++) {
  34680. ret = wc_PKCS7_VerifySignedData(pkcs7, out + z, 1);
  34681. if (ret < 0 && ret != WC_PKCS7_WANT_READ_E) {
  34682. printf("unexpected error %d\n", ret);
  34683. ERROR_OUT(-12556, out);
  34684. }
  34685. }
  34686. }
  34687. #endif
  34688. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0)
  34689. ERROR_OUT(-12557, out);
  34690. if (testVectors[i].encCompFlag == 0) {
  34691. /* verify decoded content matches expected */
  34692. if ((pkcs7->contentSz != testVectors[i].contentSz) ||
  34693. XMEMCMP(pkcs7->content, testVectors[i].content,
  34694. pkcs7->contentSz)) {
  34695. ERROR_OUT(-12558, out);
  34696. }
  34697. }
  34698. #ifndef NO_PKCS7_ENCRYPTED_DATA
  34699. else if (testVectors[i].encCompFlag == 1) {
  34700. /* decrypt inner encryptedData */
  34701. pkcs7->encryptionKey = testVectors[i].encryptKey;
  34702. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  34703. ret = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  34704. pkcs7->contentSz, out, outSz);
  34705. if (ret < 0)
  34706. ERROR_OUT(-12559, out);
  34707. /* compare decrypted to expected */
  34708. if (((word32)ret != testVectors[i].contentSz) ||
  34709. XMEMCMP(out, testVectors[i].content, ret))
  34710. ERROR_OUT(-12560, out);
  34711. }
  34712. #endif
  34713. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  34714. else if (testVectors[i].encCompFlag == 2) {
  34715. /* decompress inner compressedData */
  34716. ret = wc_PKCS7_DecodeCompressedData(pkcs7, pkcs7->content,
  34717. pkcs7->contentSz, out, outSz);
  34718. if (ret < 0)
  34719. ERROR_OUT(-12561, out);
  34720. /* compare decompressed to expected */
  34721. if (((word32)ret != testVectors[i].contentSz) ||
  34722. XMEMCMP(out, testVectors[i].content, ret))
  34723. ERROR_OUT(-12562, out);
  34724. }
  34725. #ifndef NO_PKCS7_ENCRYPTED_DATA
  34726. else if (testVectors[i].encCompFlag == 3) {
  34727. encryptedTmpSz = FOURK_BUF;
  34728. encryptedTmp = (byte*)XMALLOC(encryptedTmpSz, HEAP_HINT,
  34729. DYNAMIC_TYPE_TMP_BUFFER);
  34730. if (encryptedTmp == NULL)
  34731. ERROR_OUT(-12563, out);
  34732. XMEMSET(encryptedTmp, 0, encryptedTmpSz);
  34733. /* decrypt inner encryptedData */
  34734. pkcs7->encryptionKey = testVectors[i].encryptKey;
  34735. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  34736. encryptedTmpSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  34737. pkcs7->contentSz, encryptedTmp,
  34738. encryptedTmpSz);
  34739. if (encryptedTmpSz < 0 || pkcs7->contentOID != COMPRESSED_DATA)
  34740. ERROR_OUT(-12564, out);
  34741. /* decompress inner compressedData */
  34742. ret = wc_PKCS7_DecodeCompressedData(pkcs7, encryptedTmp,
  34743. encryptedTmpSz, out, outSz);
  34744. if (ret < 0)
  34745. ERROR_OUT(-12565, out);
  34746. /* compare decompressed to expected */
  34747. if (((word32)ret != testVectors[i].contentSz) ||
  34748. XMEMCMP(out, testVectors[i].content, ret))
  34749. ERROR_OUT(-12566, out);
  34750. }
  34751. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  34752. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  34753. }
  34754. out:
  34755. if (pkcs7 != NULL)
  34756. wc_PKCS7_Free(pkcs7);
  34757. if (out != NULL)
  34758. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34759. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA) && \
  34760. !defined(NO_PKCS7_ENCRYPTED_DATA)
  34761. if (encryptedTmp != NULL)
  34762. XFREE(encryptedTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34763. #endif
  34764. if (testVectors != NULL)
  34765. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34766. wc_FreeRng(&rng);
  34767. if (ret > 0)
  34768. return 0;
  34769. (void)eccClientCertBuf;
  34770. (void)eccClientCertBufSz;
  34771. (void)eccClientPrivKeyBuf;
  34772. (void)eccClientPrivKeyBufSz;
  34773. (void)rsaClientCertBuf;
  34774. (void)rsaClientCertBufSz;
  34775. (void)rsaClientPrivKeyBuf;
  34776. (void)rsaClientPrivKeyBufSz;
  34777. (void)rsaServerCertBuf;
  34778. (void)rsaServerCertBufSz;
  34779. (void)rsaServerPrivKeyBuf;
  34780. (void)rsaServerPrivKeyBufSz;
  34781. (void)rsaCaCertBuf;
  34782. (void)rsaCaCertBufSz;
  34783. (void)rsaCaPrivKeyBuf;
  34784. (void)rsaCaPrivKeyBufSz;
  34785. return ret;
  34786. }
  34787. WOLFSSL_TEST_SUBROUTINE int pkcs7signed_test(void)
  34788. {
  34789. int ret = 0;
  34790. byte* rsaClientCertBuf = NULL;
  34791. byte* rsaServerCertBuf = NULL;
  34792. byte* rsaCaCertBuf = NULL;
  34793. byte* eccClientCertBuf = NULL;
  34794. byte* rsaClientPrivKeyBuf = NULL;
  34795. byte* rsaServerPrivKeyBuf = NULL;
  34796. byte* rsaCaPrivKeyBuf = NULL;
  34797. byte* eccClientPrivKeyBuf = NULL;
  34798. word32 rsaClientCertBufSz = 0;
  34799. word32 rsaServerCertBufSz = 0;
  34800. word32 rsaCaCertBufSz = 0;
  34801. word32 eccClientCertBufSz = 0;
  34802. word32 rsaClientPrivKeyBufSz = 0;
  34803. word32 rsaServerPrivKeyBufSz = 0;
  34804. word32 rsaCaPrivKeyBufSz = 0;
  34805. word32 eccClientPrivKeyBufSz = 0;
  34806. #ifndef NO_RSA
  34807. /* read client RSA cert and key in DER format */
  34808. rsaClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  34809. DYNAMIC_TYPE_TMP_BUFFER);
  34810. if (rsaClientCertBuf == NULL)
  34811. ret = -12600;
  34812. rsaClientPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  34813. DYNAMIC_TYPE_TMP_BUFFER);
  34814. if (ret == 0 && rsaClientPrivKeyBuf == NULL) {
  34815. ret = -12601;
  34816. }
  34817. rsaClientCertBufSz = FOURK_BUF;
  34818. rsaClientPrivKeyBufSz = FOURK_BUF;
  34819. /* read server RSA cert and key in DER format */
  34820. rsaServerCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  34821. DYNAMIC_TYPE_TMP_BUFFER);
  34822. if (ret == 0 && rsaServerCertBuf == NULL)
  34823. ret = -12602;
  34824. rsaServerPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  34825. DYNAMIC_TYPE_TMP_BUFFER);
  34826. if (ret == 0 && rsaServerPrivKeyBuf == NULL) {
  34827. ret = -12603;
  34828. }
  34829. rsaServerCertBufSz = FOURK_BUF;
  34830. rsaServerPrivKeyBufSz = FOURK_BUF;
  34831. /* read CA RSA cert and key in DER format, for use with server cert */
  34832. rsaCaCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  34833. DYNAMIC_TYPE_TMP_BUFFER);
  34834. if (ret == 0 && rsaCaCertBuf == NULL)
  34835. ret = -12604;
  34836. rsaCaPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  34837. DYNAMIC_TYPE_TMP_BUFFER);
  34838. if (ret == 0 && rsaCaPrivKeyBuf == NULL) {
  34839. ret = -12605;
  34840. }
  34841. rsaCaCertBufSz = FOURK_BUF;
  34842. rsaCaPrivKeyBufSz = FOURK_BUF;
  34843. #endif /* NO_RSA */
  34844. #ifdef HAVE_ECC
  34845. /* read client ECC cert and key in DER format */
  34846. eccClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  34847. DYNAMIC_TYPE_TMP_BUFFER);
  34848. if (ret == 0 && eccClientCertBuf == NULL) {
  34849. ret = -12606;
  34850. }
  34851. eccClientPrivKeyBuf =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  34852. DYNAMIC_TYPE_TMP_BUFFER);
  34853. if (ret == 0 && eccClientPrivKeyBuf == NULL) {
  34854. ret = -12607;
  34855. }
  34856. eccClientCertBufSz = FOURK_BUF;
  34857. eccClientPrivKeyBufSz = FOURK_BUF;
  34858. #endif /* HAVE_ECC */
  34859. if (ret >= 0)
  34860. ret = pkcs7_load_certs_keys(rsaClientCertBuf, &rsaClientCertBufSz,
  34861. rsaClientPrivKeyBuf, &rsaClientPrivKeyBufSz,
  34862. rsaServerCertBuf, &rsaServerCertBufSz,
  34863. rsaServerPrivKeyBuf, &rsaServerPrivKeyBufSz,
  34864. rsaCaCertBuf, &rsaCaCertBufSz,
  34865. rsaCaPrivKeyBuf, &rsaCaPrivKeyBufSz,
  34866. eccClientCertBuf, &eccClientCertBufSz,
  34867. eccClientPrivKeyBuf, &eccClientPrivKeyBufSz);
  34868. if (ret < 0) {
  34869. ret = -12608;
  34870. }
  34871. if (ret >= 0)
  34872. ret = pkcs7signed_run_vectors(rsaClientCertBuf, (word32)rsaClientCertBufSz,
  34873. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  34874. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  34875. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  34876. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  34877. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  34878. eccClientCertBuf, (word32)eccClientCertBufSz,
  34879. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  34880. if (ret >= 0)
  34881. ret = pkcs7signed_run_SingleShotVectors(
  34882. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  34883. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  34884. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  34885. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  34886. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  34887. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  34888. eccClientCertBuf, (word32)eccClientCertBufSz,
  34889. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  34890. #if !defined(NO_RSA) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  34891. if (ret >= 0)
  34892. ret = pkcs7callback_test(
  34893. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  34894. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz);
  34895. #endif
  34896. XFREE(rsaClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34897. XFREE(rsaClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34898. XFREE(rsaServerCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34899. XFREE(rsaServerPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34900. XFREE(rsaCaCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34901. XFREE(rsaCaPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34902. XFREE(eccClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34903. XFREE(eccClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34904. return ret;
  34905. }
  34906. #endif /* HAVE_PKCS7 */
  34907. #ifdef HAVE_VALGRIND
  34908. /* Need a static build to have access to symbols. */
  34909. /* Maximum number of bytes in a number to test. */
  34910. #define MP_MAX_TEST_BYTE_LEN 32
  34911. static int randNum(mp_int* n, int len, WC_RNG* rng, void* heap)
  34912. {
  34913. byte d[MP_MAX_TEST_BYTE_LEN];
  34914. int ret;
  34915. (void)heap;
  34916. do {
  34917. ret = wc_RNG_GenerateBlock(rng, d, len);
  34918. if (ret != 0)
  34919. return ret;
  34920. ret = mp_read_unsigned_bin(n, d, len);
  34921. if (ret != 0)
  34922. return ret;
  34923. } while (mp_iszero(n));
  34924. return 0;
  34925. }
  34926. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  34927. static int mp_test_div_3(mp_int* a, mp_int* r, WC_RNG* rng)
  34928. {
  34929. int i, j;
  34930. mp_digit rem;
  34931. mp_digit rem2;
  34932. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  34933. defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  34934. for (i = 0; i < 10; i++) {
  34935. for (j = 1; j < 10; j++) {
  34936. if (randNum(a, j, rng, NULL) != 0)
  34937. return -12620;
  34938. if (mp_div_3(a, r, &rem) != 0)
  34939. return -12621;
  34940. if (mp_mul_d(r, 3, r) != 0)
  34941. return -12622;
  34942. if (mp_add_d(r, rem, r) != 0)
  34943. return -12623;
  34944. if (mp_cmp(r, a) != MP_EQ)
  34945. return -12624;
  34946. }
  34947. }
  34948. if (mp_div_3(a, r, &rem) != 0)
  34949. return -12625;
  34950. if (mp_div_3(a, a, NULL) != 0)
  34951. return -12626;
  34952. if (mp_cmp(r, a) != MP_EQ)
  34953. return -12627;
  34954. #endif
  34955. #if defined(WOLFSSL_SP_MATH_ALL)
  34956. if (mp_div_d(a, 10, r, &rem) != 0)
  34957. return -12628;
  34958. if (mp_div_d(a, 10, a, NULL) != 0)
  34959. return -12629;
  34960. if (mp_cmp(r, a) != MP_EQ)
  34961. return -12630;
  34962. if (mp_div_d(a, 12, r, &rem) != 0)
  34963. return -12631;
  34964. if (mp_div_d(a, 12, a, NULL) != 0)
  34965. return -12632;
  34966. if (mp_cmp(r, a) != MP_EQ)
  34967. return -12633;
  34968. if (mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), r, &rem) != 0)
  34969. return -12634;
  34970. if (mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), NULL, &rem2) != 0)
  34971. return -12635;
  34972. if (mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), a, NULL) != 0)
  34973. return -12636;
  34974. if (mp_cmp(r, a) != MP_EQ)
  34975. return -12637;
  34976. if (rem != rem2)
  34977. return -12638;
  34978. #endif
  34979. (void)a;
  34980. (void)r;
  34981. (void)rng;
  34982. (void)i;
  34983. (void)j;
  34984. (void)rem;
  34985. (void)rem2;
  34986. return 0;
  34987. }
  34988. #endif /* WOLFSSL_SP_MATH || !USE_FAST_MATH */
  34989. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  34990. !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  34991. (!defined WOLFSSL_SP_MATH && !defined(WOLFSSL_SP_MATH_ALL) && \
  34992. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  34993. static int mp_test_radix_10(mp_int* a, mp_int* r, WC_RNG* rng)
  34994. {
  34995. int ret;
  34996. int i, j;
  34997. int size;
  34998. char str[30];
  34999. WOLFSSL_SMALL_STACK_STATIC const char* badStr1 = "A";
  35000. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "a";
  35001. WOLFSSL_SMALL_STACK_STATIC const char* badStr3 = " ";
  35002. WOLFSSL_SMALL_STACK_STATIC const char* zeros = "000";
  35003. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  35004. for (i = 0; i < 10; i++) {
  35005. for (j = 2; j < 12; j++) {
  35006. if (randNum(a, j, rng, NULL) != 0)
  35007. return -12640;
  35008. if (mp_radix_size(a, MP_RADIX_DEC, &size) != MP_OKAY)
  35009. return -12641;
  35010. if (mp_toradix(a, str, MP_RADIX_DEC) != MP_OKAY)
  35011. return -12660;
  35012. if ((int)XSTRLEN(str) != size - 1)
  35013. return -12642;
  35014. if (mp_read_radix(r, str, MP_RADIX_DEC) != MP_OKAY)
  35015. return -12661;
  35016. if (mp_cmp(a, r) != MP_EQ)
  35017. return -12643;
  35018. }
  35019. }
  35020. if (mp_read_radix(r, badStr1, MP_RADIX_DEC) != MP_VAL)
  35021. return -12644;
  35022. if (mp_read_radix(r, badStr2, MP_RADIX_DEC) != MP_VAL)
  35023. return -12645;
  35024. if (mp_read_radix(r, badStr3, MP_RADIX_DEC) != MP_VAL)
  35025. return -12646;
  35026. if (mp_read_radix(r, zeros, MP_RADIX_DEC) != MP_OKAY)
  35027. return -12647;
  35028. if (!mp_iszero(r))
  35029. return -12648;
  35030. mp_set(r, 1);
  35031. if (mp_read_radix(r, empty, MP_RADIX_DEC) != MP_OKAY)
  35032. return -12649;
  35033. if (!mp_iszero(r))
  35034. return -12650;
  35035. mp_zero(a);
  35036. ret = mp_radix_size(a, MP_RADIX_DEC, &size);
  35037. if (ret != 0)
  35038. return -12651;
  35039. if (size != 2)
  35040. return -12652;
  35041. ret = mp_toradix(a, str, MP_RADIX_DEC);
  35042. if (ret != 0)
  35043. return -12653;
  35044. if ((int)XSTRLEN(str) != size - 1)
  35045. return -12654;
  35046. ret = mp_read_radix(r, str, MP_RADIX_DEC);
  35047. if (ret != 0)
  35048. return -12655;
  35049. if (!mp_iszero(r))
  35050. return -12656;
  35051. return 0;
  35052. }
  35053. #endif
  35054. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  35055. defined(HAVE_ECC))
  35056. static int mp_test_radix_16(mp_int* a, mp_int* r, WC_RNG* rng)
  35057. {
  35058. int ret;
  35059. int i, j;
  35060. int size;
  35061. char str[30];
  35062. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  35063. static char longStr[2 * sizeof(a->dp) + 2];
  35064. #endif
  35065. WOLFSSL_SMALL_STACK_STATIC const char* badStr1 = " ";
  35066. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "}";
  35067. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  35068. for (i = 0; i < 10; i++) {
  35069. for (j = 2; j < 12; j++) {
  35070. if (randNum(a, j, rng, NULL) != 0)
  35071. return -12660;
  35072. mp_radix_size(a, MP_RADIX_HEX, &size);
  35073. mp_toradix(a, str, MP_RADIX_HEX);
  35074. if ((int)XSTRLEN(str) != size - 1)
  35075. return -12661;
  35076. mp_read_radix(r, str, MP_RADIX_HEX);
  35077. if (mp_cmp(a, r) != MP_EQ)
  35078. return -12662;
  35079. }
  35080. }
  35081. if (mp_read_radix(r, badStr1, MP_RADIX_HEX) != MP_VAL)
  35082. return -12663;
  35083. if (mp_read_radix(r, badStr2, MP_RADIX_HEX) != MP_VAL)
  35084. return -12664;
  35085. mp_set(r, 1);
  35086. if (mp_read_radix(r, empty, MP_RADIX_HEX) != MP_OKAY)
  35087. return -12665;
  35088. if (!mp_iszero(r))
  35089. return -12666;
  35090. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  35091. /* Fixed MP data size - string can be too long. */
  35092. longStr[0] = '8';
  35093. XMEMSET(longStr+1, '0', sizeof(longStr) - 2);
  35094. longStr[sizeof(longStr)-1] = '\0';
  35095. if (mp_read_radix(r, longStr, MP_RADIX_HEX) != MP_VAL)
  35096. return -12667;
  35097. #endif
  35098. mp_zero(a);
  35099. ret = mp_radix_size(a, MP_RADIX_HEX, &size);
  35100. if (ret != 0)
  35101. return -12668;
  35102. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  35103. if (size != 3)
  35104. #else
  35105. if (size != 2)
  35106. #endif
  35107. return -12669;
  35108. ret = mp_toradix(a, str, MP_RADIX_HEX);
  35109. if (ret != 0)
  35110. return -12670;
  35111. if ((int)XSTRLEN(str) != size - 1)
  35112. return -12671;
  35113. ret = mp_read_radix(r, str, MP_RADIX_HEX);
  35114. if (ret != 0)
  35115. return -12672;
  35116. if (!mp_iszero(r))
  35117. return -12673;
  35118. #ifdef WOLFSSL_SP_MATH
  35119. ret = mp_toradix(a, str, 8);
  35120. if (ret != MP_VAL)
  35121. return -12674;
  35122. ret = mp_radix_size(a, 8, &size);
  35123. if (ret != MP_VAL)
  35124. return -12675;
  35125. #endif
  35126. return 0;
  35127. }
  35128. #endif
  35129. static int mp_test_shift(mp_int* a, mp_int* r1, WC_RNG* rng)
  35130. {
  35131. int i;
  35132. if (randNum(a, 4, rng, NULL) != 0)
  35133. return -12680;
  35134. for (i = 0; i < 4; i++) {
  35135. mp_copy(r1, a);
  35136. if (mp_lshd(r1, i) != MP_OKAY)
  35137. return -12681;
  35138. #ifndef WOLFSSL_SP_MATH
  35139. mp_rshd(r1, i);
  35140. #else
  35141. mp_rshb(r1, i * SP_WORD_SIZE);
  35142. #endif
  35143. if (mp_cmp(a, r1) != MP_EQ)
  35144. return -12682;
  35145. }
  35146. #ifndef WOLFSSL_SP_MATH
  35147. for (i = 0; i < DIGIT_BIT+1; i++) {
  35148. if (mp_mul_2d(a, i, r1) != MP_OKAY)
  35149. return -12683;
  35150. mp_rshb(r1, i);
  35151. if (mp_cmp(a, r1) != MP_EQ)
  35152. return -12684;
  35153. }
  35154. #endif
  35155. return 0;
  35156. }
  35157. static int mp_test_add_sub_d(mp_int* a, mp_int* r1)
  35158. {
  35159. int i, j;
  35160. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  35161. mp_zero(a);
  35162. mp_set_bit(a, i);
  35163. if (a->used != (i + DIGIT_BIT) / DIGIT_BIT)
  35164. return -12690;
  35165. for (j = 0; j < i && j < DIGIT_BIT; j++) {
  35166. mp_zero(r1);
  35167. mp_set_bit(r1, i);
  35168. if (mp_sub_d(r1, (mp_digit)1 << j, r1) != MP_OKAY)
  35169. return -12691;
  35170. if (mp_add_d(r1, (mp_digit)1 << j, r1) != MP_OKAY)
  35171. return -12692;
  35172. if (mp_cmp(a, r1) != MP_EQ)
  35173. return -12693;
  35174. }
  35175. }
  35176. mp_zero(r1);
  35177. if (mp_add_d(r1, 1, r1) != MP_OKAY)
  35178. return -12694;
  35179. if (r1->used != 1)
  35180. return -12695;
  35181. if (mp_sub_d(r1, 1, r1) != MP_OKAY)
  35182. return -12696;
  35183. if (r1->used != 0)
  35184. return -12697;
  35185. return 0;
  35186. }
  35187. static int mp_test_read_to_bin(mp_int* a)
  35188. {
  35189. WOLFSSL_SMALL_STACK_STATIC const byte in[16] = {
  35190. 0x91, 0xa2, 0xb3, 0xc4, 0xd5, 0xe6, 0xf7, 0x08,
  35191. 0x93, 0xa4, 0xb4, 0xc5, 0xd6, 0xe7, 0xf8, 0x09
  35192. };
  35193. byte out[24];
  35194. int i, j, k;
  35195. const byte* p;
  35196. int ret;
  35197. for (i = 0; i < (int)sizeof(in); i++) {
  35198. p = in + sizeof(in) - i;
  35199. ret = mp_read_unsigned_bin(a, p, i);
  35200. if (ret != 0)
  35201. return -12710;
  35202. for (j = i; j < (int)sizeof(out); j++) {
  35203. XMEMSET(out, 0xff, sizeof(out));
  35204. ret = mp_to_unsigned_bin_len(a, out, j);
  35205. if (ret != 0)
  35206. return -12711;
  35207. for (k = 0; k < j - i; k++) {
  35208. if (out[k] != 0)
  35209. return -12712;
  35210. }
  35211. for (; k < j; k++) {
  35212. if (out[k] != p[k - (j - i)])
  35213. return -12713;
  35214. }
  35215. }
  35216. }
  35217. ret = mp_read_unsigned_bin(a, NULL, 0);
  35218. if (ret != 0)
  35219. return -12714;
  35220. if (!mp_iszero(a))
  35221. return -12715;
  35222. return 0;
  35223. }
  35224. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  35225. static int mp_test_set_int(mp_int* a)
  35226. {
  35227. #if SP_ULONG_BITS == 64
  35228. unsigned long n = 0xfedcba9876543210UL;
  35229. byte exp[8] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
  35230. byte out[8] = { 0 };
  35231. #elif SP_ULONG_BITS == 32
  35232. unsigned long n = 0xfedcba98UL;
  35233. byte exp[4] = { 0xfe, 0xdc, 0xba, 0x98 };
  35234. byte out[4] = { 0 };
  35235. #elif SP_ULONG_BITS == 16
  35236. unsigned long n = 0xfedc;
  35237. byte exp[2] = { 0xfe, 0xdc };
  35238. byte out[2] = { 0 };
  35239. #elif SP_ULONG_BITS == 8
  35240. unsigned long n = 0xfe;
  35241. byte exp[1] = { 0xfe };
  35242. byte out[1] = { 0 };
  35243. #endif
  35244. int ret;
  35245. ret = mp_set_int(a, n);
  35246. if (ret != 0)
  35247. return -12720;
  35248. ret = mp_unsigned_bin_size(a);
  35249. if (ret != sizeof(exp))
  35250. return -12721;
  35251. ret = mp_to_unsigned_bin(a, out);
  35252. if (ret != 0)
  35253. return -12722;
  35254. if (XMEMCMP(exp, out, sizeof(exp)) != 0)
  35255. return -12723;
  35256. return 0;
  35257. }
  35258. #endif
  35259. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  35260. static int mp_test_param(mp_int* a, mp_int* b, mp_int* r, WC_RNG* rng)
  35261. {
  35262. byte buffer[16];
  35263. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  35264. char hexStr[] = "abcdef0123456789";
  35265. #ifndef WOLFSSL_SP_INT_NEGATIVE
  35266. char negStr[] = "-1234";
  35267. #endif
  35268. #endif
  35269. #if !defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_KEY_GEN) || \
  35270. defined(HAVE_COMP_KEY)
  35271. char decStr[] = "0987654321";
  35272. #endif
  35273. int ret;
  35274. #ifdef WOLFSSL_SP_MATH_ALL
  35275. mp_digit rho;
  35276. int size;
  35277. #endif
  35278. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  35279. int result;
  35280. #endif
  35281. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  35282. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  35283. mp_digit rd;
  35284. #endif
  35285. (void)rng;
  35286. (void)r;
  35287. ret = mp_init(NULL);
  35288. if (ret != MP_VAL)
  35289. return -12730;
  35290. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  35291. ret = mp_init_multi(NULL, NULL, NULL, NULL, NULL, NULL);
  35292. if (ret != MP_OKAY)
  35293. return -12731;
  35294. #endif
  35295. mp_free(NULL);
  35296. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || !defined(NO_DH) || defined(HAVE_ECC)
  35297. ret = mp_grow(NULL, 1);
  35298. if (ret != MP_VAL)
  35299. return -12732;
  35300. #ifdef WOLFSSL_SP_MATH
  35301. ret = mp_grow(a, SP_INT_DIGITS + 1);
  35302. if (ret != MP_MEM)
  35303. return -12733;
  35304. #endif
  35305. #endif
  35306. mp_clear(NULL);
  35307. ret = mp_abs(NULL, NULL);
  35308. if (ret != MP_VAL)
  35309. return -12734;
  35310. ret = mp_abs(a, NULL);
  35311. if (ret != MP_VAL)
  35312. return -12735;
  35313. ret = mp_abs(NULL, b);
  35314. if (ret != MP_VAL)
  35315. return -12736;
  35316. ret = mp_unsigned_bin_size(NULL);
  35317. if (ret != 0)
  35318. return -12737;
  35319. ret = mp_read_unsigned_bin(NULL, NULL, sizeof(buffer));
  35320. if (ret != MP_VAL)
  35321. return -12738;
  35322. ret = mp_read_unsigned_bin(NULL, buffer, sizeof(buffer));
  35323. if (ret != MP_VAL)
  35324. return -12739;
  35325. ret = mp_read_unsigned_bin(a, NULL, sizeof(buffer));
  35326. if (ret != MP_VAL)
  35327. return -12740;
  35328. ret = mp_read_unsigned_bin(a, buffer, SP_INT_DIGITS * SP_WORD_SIZEOF + 1);
  35329. if (ret != MP_VAL)
  35330. return -12741;
  35331. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  35332. ret = mp_read_radix(NULL, NULL, 16);
  35333. if (ret != MP_VAL)
  35334. return -12742;
  35335. ret = mp_read_radix(a, NULL, 16);
  35336. if (ret != MP_VAL)
  35337. return -12743;
  35338. ret = mp_read_radix(NULL, hexStr, 16);
  35339. if (ret != MP_VAL)
  35340. return -12744;
  35341. #ifndef WOLFSSL_SP_INT_NEGATIVE
  35342. ret = mp_read_radix(a, negStr, 16);
  35343. if (ret != MP_VAL)
  35344. return -12745;
  35345. #ifdef WOLFSSL_SP_MATH_ALL
  35346. ret = mp_read_radix(a, negStr, 10);
  35347. if (ret != MP_VAL)
  35348. return -12746;
  35349. #endif /* WOLFSSL_SP_MATH_ALL */
  35350. #endif /* WOLFSSL_SP_INT_NEGATIVE */
  35351. #endif
  35352. #ifndef WOLFSSL_SP_MATH_ALL
  35353. /* Radix 10 only supported with ALL. */
  35354. ret = mp_read_radix(a, decStr, 10);
  35355. if (ret != MP_VAL)
  35356. return -12747;
  35357. #endif
  35358. /* Radix 8 not supported SP_INT. */
  35359. ret = mp_read_radix(a, "0123", 8);
  35360. if (ret != MP_VAL)
  35361. return -12748;
  35362. ret = mp_count_bits(NULL);
  35363. if (ret != 0)
  35364. return -12749;
  35365. ret = mp_is_bit_set(NULL, 0);
  35366. if (ret != 0)
  35367. return -12750;
  35368. ret = mp_leading_bit(NULL);
  35369. if (ret != 0)
  35370. return -12751;
  35371. mp_zero(a);
  35372. ret = mp_leading_bit(a);
  35373. if (ret != 0)
  35374. return -12752;
  35375. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  35376. defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || \
  35377. !defined(NO_RSA)
  35378. ret = mp_set_bit(NULL, 1);
  35379. if (ret != MP_VAL)
  35380. return -12753;
  35381. #endif
  35382. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  35383. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  35384. ret = mp_to_unsigned_bin(NULL, NULL);
  35385. if (ret != MP_VAL)
  35386. return -12754;
  35387. ret = mp_to_unsigned_bin(a, NULL);
  35388. if (ret != MP_VAL)
  35389. return -12755;
  35390. ret = mp_to_unsigned_bin(NULL, buffer);
  35391. if (ret != MP_VAL)
  35392. return -12756;
  35393. #endif
  35394. ret = mp_to_unsigned_bin_len(NULL, NULL, 1);
  35395. if (ret != MP_VAL)
  35396. return -12757;
  35397. ret = mp_to_unsigned_bin_len(a, NULL, 1);
  35398. if (ret != MP_VAL)
  35399. return -12758;
  35400. ret = mp_to_unsigned_bin_len(NULL, buffer, 1);
  35401. if (ret != MP_VAL)
  35402. return -12759;
  35403. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  35404. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  35405. ret = mp_to_unsigned_bin_at_pos(0, NULL, NULL);
  35406. if (ret != MP_VAL)
  35407. return -12760;
  35408. ret = mp_to_unsigned_bin_at_pos(0, a, NULL);
  35409. if (ret != MP_VAL)
  35410. return -12761;
  35411. ret = mp_to_unsigned_bin_at_pos(0, NULL, buffer);
  35412. if (ret != MP_VAL)
  35413. return -12762;
  35414. ret = mp_to_unsigned_bin_at_pos(0, a, buffer);
  35415. if (ret != MP_OKAY)
  35416. return -12763;
  35417. #endif
  35418. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  35419. ret = mp_copy(NULL, NULL);
  35420. if (ret != MP_VAL)
  35421. return -12764;
  35422. ret = mp_copy(a, NULL);
  35423. if (ret != MP_VAL)
  35424. return -12765;
  35425. ret = mp_copy(NULL, b);
  35426. if (ret != MP_VAL)
  35427. return -12766;
  35428. #endif
  35429. #if defined(WOLFSSL_KEY_GEN) || !defined(NO_DH)
  35430. ret = sp_2expt(NULL, 1);
  35431. if (ret != MP_VAL)
  35432. return -12767;
  35433. #endif
  35434. ret = mp_set(NULL, 0);
  35435. if (ret != MP_VAL)
  35436. return -12768;
  35437. ret = mp_cmp_d(NULL, 0);
  35438. if (ret != MP_LT)
  35439. return -12769;
  35440. ret = mp_cmp(NULL, NULL);
  35441. if (ret != MP_EQ)
  35442. return -12770;
  35443. ret = mp_cmp(a, NULL);
  35444. if (ret != MP_GT)
  35445. return -12771;
  35446. ret = mp_cmp(NULL, b);
  35447. if (ret != MP_LT)
  35448. return -12772;
  35449. #ifdef WOLFSSL_SP_MATH_ALL
  35450. mp_rshd(NULL, 1);
  35451. #endif
  35452. mp_zero(NULL);
  35453. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  35454. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  35455. ret = mp_lshd(NULL, 0);
  35456. if (ret != MP_VAL)
  35457. return -12773;
  35458. ret = mp_lshd(a, SP_INT_DIGITS + 1);
  35459. if (ret != MP_VAL)
  35460. return -12774;
  35461. #endif
  35462. #if defined(WOLFSSL_SP_MATH_ALL)
  35463. ret = mp_div(NULL, NULL, a, b);
  35464. if (ret != MP_VAL)
  35465. return -12775;
  35466. ret = mp_div(a, NULL, a, b);
  35467. if (ret != MP_VAL)
  35468. return -12776;
  35469. ret = mp_div(NULL, b, a, b);
  35470. if (ret != MP_VAL)
  35471. return -12777;
  35472. ret = mp_div(a, b, NULL, NULL);
  35473. if (ret != MP_VAL)
  35474. return -12778;
  35475. #endif
  35476. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  35477. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  35478. ret = mp_mod(NULL, NULL, NULL);
  35479. if (ret != MP_VAL)
  35480. return -12779;
  35481. ret = mp_mod(a, NULL, NULL);
  35482. if (ret != MP_VAL)
  35483. return -12780;
  35484. ret = mp_mod(NULL, b, NULL);
  35485. if (ret != MP_VAL)
  35486. return -12781;
  35487. ret = mp_mod(NULL, NULL, r);
  35488. if (ret != MP_VAL)
  35489. return -12782;
  35490. ret = mp_mod(a, b, NULL);
  35491. if (ret != MP_VAL)
  35492. return -12783;
  35493. ret = mp_mod(a, NULL, r);
  35494. if (ret != MP_VAL)
  35495. return -12784;
  35496. ret = mp_mod(NULL, b, r);
  35497. if (ret != MP_VAL)
  35498. return -12785;
  35499. #endif
  35500. #if !defined(NO_RSA) || defined(WOLFSSL_SP_MATH_ALL)
  35501. ret = mp_set_int(NULL, 0);
  35502. if (ret != MP_VAL)
  35503. return -12786;
  35504. #endif
  35505. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  35506. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  35507. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  35508. if (ret != MP_VAL)
  35509. return 9950;
  35510. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  35511. if (ret != MP_VAL)
  35512. return 9951;
  35513. ret = mp_exptmod_ex(NULL, a, 1, NULL, NULL);
  35514. if (ret != MP_VAL)
  35515. return 9952;
  35516. ret = mp_exptmod_ex(NULL, NULL, 1, a, NULL);
  35517. if (ret != MP_VAL)
  35518. return 9953;
  35519. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  35520. if (ret != MP_VAL)
  35521. return 9954;
  35522. ret = mp_exptmod_ex(a, a, 1, a, NULL);
  35523. if (ret != MP_VAL)
  35524. return 9955;
  35525. ret = mp_exptmod_ex(a, a, 1, NULL, a);
  35526. if (ret != MP_VAL)
  35527. return 9956;
  35528. ret = mp_exptmod_ex(a, NULL, 1, a, a);
  35529. if (ret != MP_VAL)
  35530. return 9957;
  35531. ret = mp_exptmod_ex(NULL, a, 1, a, a);
  35532. if (ret != MP_VAL)
  35533. return 9958;
  35534. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  35535. if (ret != MP_VAL)
  35536. return 9960;
  35537. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  35538. if (ret != MP_VAL)
  35539. return 9961;
  35540. ret = mp_exptmod_nct(NULL, a, NULL, NULL);
  35541. if (ret != MP_VAL)
  35542. return 9962;
  35543. ret = mp_exptmod_nct(NULL, NULL, a, NULL);
  35544. if (ret != MP_VAL)
  35545. return 9963;
  35546. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  35547. if (ret != MP_VAL)
  35548. return 9964;
  35549. ret = mp_exptmod_nct(a, a, a, NULL);
  35550. if (ret != MP_VAL)
  35551. return 9965;
  35552. ret = mp_exptmod_nct(a, a, NULL, a);
  35553. if (ret != MP_VAL)
  35554. return 9966;
  35555. ret = mp_exptmod_nct(a, NULL, a, a);
  35556. if (ret != MP_VAL)
  35557. return 9967;
  35558. ret = mp_exptmod_nct(NULL, a, a, a);
  35559. if (ret != MP_VAL)
  35560. return 9968;
  35561. #endif
  35562. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  35563. !defined(WC_NO_RNG)
  35564. ret = mp_rand_prime(NULL, 32, NULL, NULL);
  35565. if (ret != MP_VAL)
  35566. return -12787;
  35567. ret = mp_rand_prime(a, 32, NULL, NULL);
  35568. if (ret != MP_VAL)
  35569. return -12788;
  35570. ret = mp_rand_prime(NULL, 32, rng, NULL);
  35571. if (ret != MP_VAL)
  35572. return -12789;
  35573. ret = mp_rand_prime(a, 0, rng, NULL);
  35574. if (ret != MP_VAL)
  35575. return -9969;
  35576. #endif
  35577. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  35578. ret = mp_mul(NULL, NULL, NULL);
  35579. if (ret != MP_VAL)
  35580. return -12790;
  35581. ret = mp_mul(a, NULL, NULL);
  35582. if (ret != MP_VAL)
  35583. return -12791;
  35584. ret = mp_mul(NULL, b, NULL);
  35585. if (ret != MP_VAL)
  35586. return -12792;
  35587. ret = mp_mul(NULL, NULL, r);
  35588. if (ret != MP_VAL)
  35589. return -12793;
  35590. ret = mp_mul(a, b, NULL);
  35591. if (ret != MP_VAL)
  35592. return -12794;
  35593. ret = mp_mul(a, NULL, r);
  35594. if (ret != MP_VAL)
  35595. return -12795;
  35596. ret = mp_mul(NULL, b, r);
  35597. if (ret != MP_VAL)
  35598. return -12796;
  35599. #endif
  35600. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  35601. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  35602. ret = mp_sqr(NULL, NULL);
  35603. if (ret != MP_VAL)
  35604. return -12797;
  35605. ret = mp_sqr(a, NULL);
  35606. if (ret != MP_VAL)
  35607. return -12798;
  35608. ret = mp_sqr(NULL, r);
  35609. if (ret != MP_VAL)
  35610. return -12799;
  35611. #endif
  35612. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  35613. ret = mp_sqrmod(NULL, NULL, NULL);
  35614. if (ret != MP_VAL)
  35615. return -12800;
  35616. ret = mp_sqrmod(a, NULL, NULL);
  35617. if (ret != MP_VAL)
  35618. return -12801;
  35619. ret = mp_sqrmod(NULL, a, NULL);
  35620. if (ret != MP_VAL)
  35621. return -12802;
  35622. ret = mp_sqrmod(NULL, NULL, a);
  35623. if (ret != MP_VAL)
  35624. return -12803;
  35625. ret = mp_sqrmod(a, b, NULL);
  35626. if (ret != MP_VAL)
  35627. return -12804;
  35628. ret = mp_sqrmod(a, NULL, b);
  35629. if (ret != MP_VAL)
  35630. return -12805;
  35631. ret = mp_sqrmod(NULL, a, b);
  35632. if (ret != MP_VAL)
  35633. return -12806;
  35634. ret = mp_mulmod(NULL, NULL, NULL, NULL);
  35635. if (ret != MP_VAL)
  35636. return -12807;
  35637. ret = mp_mulmod(a, NULL, NULL, NULL);
  35638. if (ret != MP_VAL)
  35639. return -12808;
  35640. ret = mp_mulmod(NULL, a, NULL, NULL);
  35641. if (ret != MP_VAL)
  35642. return -12809;
  35643. ret = mp_mulmod(NULL, NULL, a, NULL);
  35644. if (ret != MP_VAL)
  35645. return -12810;
  35646. ret = mp_mulmod(NULL, NULL, NULL, a);
  35647. if (ret != MP_VAL)
  35648. return -12811;
  35649. ret = mp_mulmod(a, b, b, NULL);
  35650. if (ret != MP_VAL)
  35651. return -12812;
  35652. ret = mp_mulmod(a, b, NULL, a);
  35653. if (ret != MP_VAL)
  35654. return -12813;
  35655. ret = mp_mulmod(a, NULL, b, a);
  35656. if (ret != MP_VAL)
  35657. return -12814;
  35658. ret = mp_mulmod(NULL, b, b, a);
  35659. if (ret != MP_VAL)
  35660. return -12815;
  35661. #endif
  35662. #if !defined(NO_PWDBASED) || defined(WOLFSSL_KEY_GEN) || !defined(NO_DH) || \
  35663. !defined(NO_RSA) || !defined(NO_DSA)
  35664. ret = mp_add_d(NULL, 1, NULL);
  35665. if (ret != MP_VAL)
  35666. return -12816;
  35667. ret = mp_add_d(a, 1, NULL);
  35668. if (ret != MP_VAL)
  35669. return -12817;
  35670. ret = mp_add_d(NULL, 1, b);
  35671. if (ret != MP_VAL)
  35672. return -12818;
  35673. #endif
  35674. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  35675. !defined(NO_DH) || defined(HAVE_ECC) || !defined(NO_DSA)
  35676. ret = mp_sub_d(NULL, 1, NULL);
  35677. if (ret != MP_VAL)
  35678. return -12819;
  35679. ret = mp_sub_d(a, 1, NULL);
  35680. if (ret != MP_VAL)
  35681. return -12820;
  35682. ret = mp_sub_d(NULL, 1, b);
  35683. if (ret != MP_VAL)
  35684. return -12821;
  35685. #endif
  35686. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  35687. defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  35688. ret = mp_div_d(NULL, 0, NULL, NULL);
  35689. if (ret != MP_VAL)
  35690. return -12822;
  35691. ret = mp_div_d(a, 0, NULL, NULL);
  35692. if (ret != MP_VAL)
  35693. return -12823;
  35694. ret = mp_div_d(NULL, 1, NULL, NULL);
  35695. if (ret != MP_VAL)
  35696. return -12824;
  35697. #endif
  35698. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  35699. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  35700. ret = mp_mod_d(NULL, 0, NULL);
  35701. if (ret != MP_VAL)
  35702. return -12825;
  35703. ret = mp_mod_d(a, 0, NULL);
  35704. if (ret != MP_VAL)
  35705. return -12826;
  35706. ret = mp_mod_d(NULL, 0, &rd);
  35707. if (ret != MP_VAL)
  35708. return -12827;
  35709. #endif
  35710. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  35711. ret = mp_gcd(NULL, NULL, NULL);
  35712. if (ret != MP_VAL)
  35713. return -12828;
  35714. ret = mp_gcd(a, NULL, NULL);
  35715. if (ret != MP_VAL)
  35716. return -12829;
  35717. ret = mp_gcd(NULL, a, NULL);
  35718. if (ret != MP_VAL)
  35719. return -12830;
  35720. ret = mp_gcd(NULL, NULL, a);
  35721. if (ret != MP_VAL)
  35722. return -12831;
  35723. ret = mp_gcd(a, b, NULL);
  35724. if (ret != MP_VAL)
  35725. return -12832;
  35726. ret = mp_gcd(a, NULL, b);
  35727. if (ret != MP_VAL)
  35728. return -12833;
  35729. ret = mp_gcd(NULL, a, b);
  35730. if (ret != MP_VAL)
  35731. return -12834;
  35732. #endif
  35733. #if !defined(WOLFSSL_SP_MATH) && defined(HAVE_ECC)
  35734. ret = mp_div_2_mod_ct(NULL, NULL, NULL);
  35735. if (ret != MP_VAL)
  35736. return -12835;
  35737. ret = mp_div_2_mod_ct(a, NULL, NULL);
  35738. if (ret != MP_VAL)
  35739. return -12836;
  35740. ret = mp_div_2_mod_ct(NULL, b, NULL);
  35741. if (ret != MP_VAL)
  35742. return -12837;
  35743. ret = mp_div_2_mod_ct(NULL, NULL, a);
  35744. if (ret != MP_VAL)
  35745. return -12838;
  35746. ret = mp_div_2_mod_ct(a, b, NULL);
  35747. if (ret != MP_VAL)
  35748. return -12839;
  35749. ret = mp_div_2_mod_ct(a, b, NULL);
  35750. if (ret != MP_VAL)
  35751. return -12840;
  35752. ret = mp_div_2_mod_ct(NULL, b, a);
  35753. if (ret != MP_VAL)
  35754. return -12841;
  35755. ret = mp_div_2(NULL, NULL);
  35756. if (ret != MP_VAL)
  35757. return -12842;
  35758. ret = mp_div_2(a, NULL);
  35759. if (ret != MP_VAL)
  35760. return -12843;
  35761. ret = mp_div_2(NULL, a);
  35762. if (ret != MP_VAL)
  35763. return -12844;
  35764. #endif
  35765. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  35766. defined(HAVE_ECC) || !defined(NO_DSA) || defined(OPENSSL_EXTRA)
  35767. ret = mp_invmod(NULL, NULL, NULL);
  35768. if (ret != MP_VAL)
  35769. return -12845;
  35770. ret = mp_invmod(a, NULL, NULL);
  35771. if (ret != MP_VAL)
  35772. return -12846;
  35773. ret = mp_invmod(NULL, b, NULL);
  35774. if (ret != MP_VAL)
  35775. return -12847;
  35776. ret = mp_invmod(NULL, NULL, a);
  35777. if (ret != MP_VAL)
  35778. return -12848;
  35779. ret = mp_invmod(a, b, NULL);
  35780. if (ret != MP_VAL)
  35781. return -12849;
  35782. ret = mp_invmod(a, NULL, a);
  35783. if (ret != MP_VAL)
  35784. return -12850;
  35785. ret = mp_invmod(NULL, b, a);
  35786. if (ret != MP_VAL)
  35787. return -12851;
  35788. #endif
  35789. #if !defined(WOLFSSL_SP_MATH) && defined(HAVE_ECC)
  35790. ret = mp_invmod_mont_ct(NULL, NULL, NULL, 1);
  35791. if (ret != MP_VAL)
  35792. return -12852;
  35793. ret = mp_invmod_mont_ct(a, NULL, NULL, 1);
  35794. if (ret != MP_VAL)
  35795. return -12853;
  35796. ret = mp_invmod_mont_ct(NULL, b, NULL, 1);
  35797. if (ret != MP_VAL)
  35798. return -12854;
  35799. ret = mp_invmod_mont_ct(NULL, NULL, a, 1);
  35800. if (ret != MP_VAL)
  35801. return -12855;
  35802. ret = mp_invmod_mont_ct(a, b, NULL, 1);
  35803. if (ret != MP_VAL)
  35804. return -12856;
  35805. ret = mp_invmod_mont_ct(a, NULL, a, 1);
  35806. if (ret != MP_VAL)
  35807. return -12857;
  35808. ret = mp_invmod_mont_ct(NULL, b, a, 1);
  35809. if (ret != MP_VAL)
  35810. return -12858;
  35811. #endif
  35812. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  35813. ret = mp_lcm(NULL, NULL, NULL);
  35814. if (ret != MP_VAL)
  35815. return -12859;
  35816. ret = mp_lcm(a, NULL, NULL);
  35817. if (ret != MP_VAL)
  35818. return -12860;
  35819. ret = mp_lcm(NULL, b, NULL);
  35820. if (ret != MP_VAL)
  35821. return -12861;
  35822. ret = mp_lcm(NULL, NULL, a);
  35823. if (ret != MP_VAL)
  35824. return -12862;
  35825. ret = mp_lcm(a, b, NULL);
  35826. if (ret != MP_VAL)
  35827. return -12863;
  35828. ret = mp_lcm(a, NULL, a);
  35829. if (ret != MP_VAL)
  35830. return -12864;
  35831. ret = mp_lcm(NULL, b, a);
  35832. if (ret != MP_VAL)
  35833. return -12865;
  35834. #endif
  35835. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  35836. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  35837. if (ret != MP_VAL)
  35838. return -12866;
  35839. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  35840. if (ret != MP_VAL)
  35841. return -12867;
  35842. ret = mp_exptmod_ex(NULL, b, 1, NULL, NULL);
  35843. if (ret != MP_VAL)
  35844. return -12868;
  35845. ret = mp_exptmod_ex(NULL, NULL, 1, b, NULL);
  35846. if (ret != MP_VAL)
  35847. return -12869;
  35848. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  35849. if (ret != MP_VAL)
  35850. return -12870;
  35851. ret = mp_exptmod_ex(a, b, 1, b, NULL);
  35852. if (ret != MP_VAL)
  35853. return -12871;
  35854. ret = mp_exptmod_ex(a, b, 1, NULL, a);
  35855. if (ret != MP_VAL)
  35856. return -12872;
  35857. ret = mp_exptmod_ex(a, NULL, 1, b, a);
  35858. if (ret != MP_VAL)
  35859. return -12873;
  35860. ret = mp_exptmod_ex(NULL, b, 1, b, a);
  35861. if (ret != MP_VAL)
  35862. return -12874;
  35863. ret = mp_exptmod(NULL, NULL, NULL, NULL);
  35864. if (ret != MP_VAL)
  35865. return -12875;
  35866. ret = mp_exptmod(a, NULL, NULL, NULL);
  35867. if (ret != MP_VAL)
  35868. return -12876;
  35869. ret = mp_exptmod(NULL, b, NULL, NULL);
  35870. if (ret != MP_VAL)
  35871. return -12877;
  35872. ret = mp_exptmod(NULL, NULL, b, NULL);
  35873. if (ret != MP_VAL)
  35874. return -12878;
  35875. ret = mp_exptmod(NULL, NULL, NULL, a);
  35876. if (ret != MP_VAL)
  35877. return -12879;
  35878. ret = mp_exptmod(a, b, b, NULL);
  35879. if (ret != MP_VAL)
  35880. return -12880;
  35881. ret = mp_exptmod(a, b, NULL, a);
  35882. if (ret != MP_VAL)
  35883. return -12881;
  35884. ret = mp_exptmod(a, NULL, b, a);
  35885. if (ret != MP_VAL)
  35886. return -12882;
  35887. ret = mp_exptmod(NULL, b, b, a);
  35888. if (ret != MP_VAL)
  35889. return -12883;
  35890. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  35891. if (ret != MP_VAL)
  35892. return -12884;
  35893. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  35894. if (ret != MP_VAL)
  35895. return -12885;
  35896. ret = mp_exptmod_nct(NULL, b, NULL, NULL);
  35897. if (ret != MP_VAL)
  35898. return -12886;
  35899. ret = mp_exptmod_nct(NULL, NULL, b, NULL);
  35900. if (ret != MP_VAL)
  35901. return -12887;
  35902. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  35903. if (ret != MP_VAL)
  35904. return -12888;
  35905. ret = mp_exptmod_nct(a, b, b, NULL);
  35906. if (ret != MP_VAL)
  35907. return -12889;
  35908. ret = mp_exptmod_nct(a, b, NULL, a);
  35909. if (ret != MP_VAL)
  35910. return -12890;
  35911. ret = mp_exptmod_nct(a, NULL, b, a);
  35912. if (ret != MP_VAL)
  35913. return -12891;
  35914. ret = mp_exptmod_nct(NULL, b, b, a);
  35915. if (ret != MP_VAL)
  35916. return -12892;
  35917. #endif
  35918. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  35919. ret = mp_cnt_lsb(NULL);
  35920. if (ret != 0)
  35921. return -12893;
  35922. #endif
  35923. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  35924. ret = mp_prime_is_prime(NULL, 1, NULL);
  35925. if (ret != MP_VAL)
  35926. return -12894;
  35927. ret = mp_prime_is_prime(a, 1, NULL);
  35928. if (ret != MP_VAL)
  35929. return -12895;
  35930. ret = mp_prime_is_prime(NULL, 1, &result);
  35931. if (ret != MP_VAL)
  35932. return -12896;
  35933. ret = mp_prime_is_prime(a, 0, &result);
  35934. if (ret != MP_VAL)
  35935. return -12897;
  35936. ret = mp_prime_is_prime(a, 1024, &result);
  35937. if (ret != MP_VAL)
  35938. return -12898;
  35939. ret = mp_prime_is_prime_ex(NULL, 1, NULL, NULL);
  35940. if (ret != MP_VAL)
  35941. return -12899;
  35942. ret = mp_prime_is_prime_ex(a, 1, NULL, NULL);
  35943. if (ret != MP_VAL)
  35944. return -12900;
  35945. ret = mp_prime_is_prime_ex(NULL, 1, &result, NULL);
  35946. if (ret != MP_VAL)
  35947. return -12901;
  35948. ret = mp_prime_is_prime_ex(NULL, 1, NULL, rng);
  35949. if (ret != MP_VAL)
  35950. return -12902;
  35951. ret = mp_prime_is_prime_ex(a, 1, &result, NULL);
  35952. if (ret != MP_VAL)
  35953. return -12903;
  35954. ret = mp_prime_is_prime_ex(a, 1, NULL, rng);
  35955. if (ret != MP_VAL)
  35956. return -12904;
  35957. ret = mp_prime_is_prime_ex(NULL, 1, &result, rng);
  35958. if (ret != MP_VAL)
  35959. return -12905;
  35960. #endif
  35961. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || !defined(NO_DSA)
  35962. ret = mp_exch(NULL, NULL);
  35963. if (ret != MP_VAL)
  35964. return -12906;
  35965. ret = mp_exch(a, NULL);
  35966. if (ret != MP_VAL)
  35967. return -12907;
  35968. ret = mp_exch(NULL, b);
  35969. if (ret != MP_VAL)
  35970. return -12908;
  35971. #endif
  35972. #if (defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)) || \
  35973. defined(WOLFSSL_SP_MATH_ALL)
  35974. ret = mp_mul_d(NULL, 1, NULL);
  35975. if (ret != MP_VAL)
  35976. return -12909;
  35977. ret = mp_mul_d(a, 1, NULL);
  35978. if (ret != MP_VAL)
  35979. return -12910;
  35980. ret = mp_mul_d(NULL, 1, b);
  35981. if (ret != MP_VAL)
  35982. return -12911;
  35983. #endif
  35984. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  35985. ret = mp_add(NULL, NULL, NULL);
  35986. if (ret != MP_VAL)
  35987. return -12912;
  35988. ret = mp_add(a, NULL, NULL);
  35989. if (ret != MP_VAL)
  35990. return -12913;
  35991. ret = mp_add(NULL, b, NULL);
  35992. if (ret != MP_VAL)
  35993. return -12914;
  35994. ret = mp_add(NULL, NULL, r);
  35995. if (ret != MP_VAL)
  35996. return -12915;
  35997. ret = mp_add(a, b, NULL);
  35998. if (ret != MP_VAL)
  35999. return -12916;
  36000. ret = mp_add(a, NULL, r);
  36001. if (ret != MP_VAL)
  36002. return -12917;
  36003. ret = mp_add(NULL, b, r);
  36004. if (ret != MP_VAL)
  36005. return -12918;
  36006. #endif
  36007. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  36008. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  36009. ret = mp_sub(NULL, NULL, NULL);
  36010. if (ret != MP_VAL)
  36011. return -12919;
  36012. ret = mp_sub(a, NULL, NULL);
  36013. if (ret != MP_VAL)
  36014. return -12920;
  36015. ret = mp_sub(NULL, b, NULL);
  36016. if (ret != MP_VAL)
  36017. return -12921;
  36018. ret = mp_sub(NULL, NULL, r);
  36019. if (ret != MP_VAL)
  36020. return -12922;
  36021. ret = mp_sub(a, b, NULL);
  36022. if (ret != MP_VAL)
  36023. return -12923;
  36024. ret = mp_sub(a, NULL, r);
  36025. if (ret != MP_VAL)
  36026. return -12924;
  36027. ret = mp_sub(NULL, b, r);
  36028. if (ret != MP_VAL)
  36029. return -12925;
  36030. #endif
  36031. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined(WOLFSSL_SP_MATH) && \
  36032. defined(WOLFSSL_CUSTOM_CURVES))
  36033. ret = mp_addmod(NULL, NULL, NULL, NULL);
  36034. if (ret != MP_VAL)
  36035. return -12926;
  36036. ret = mp_addmod(a, NULL, NULL, NULL);
  36037. if (ret != MP_VAL)
  36038. return -12927;
  36039. ret = mp_addmod(NULL, b, NULL, NULL);
  36040. if (ret != MP_VAL)
  36041. return -12928;
  36042. ret = mp_addmod(NULL, NULL, b, NULL);
  36043. if (ret != MP_VAL)
  36044. return -12929;
  36045. ret = mp_addmod(NULL, NULL, NULL, a);
  36046. if (ret != MP_VAL)
  36047. return -12930;
  36048. ret = mp_addmod(a, b, b, NULL);
  36049. if (ret != MP_VAL)
  36050. return -12931;
  36051. ret = mp_addmod(a, b, NULL, a);
  36052. if (ret != MP_VAL)
  36053. return -12932;
  36054. ret = mp_addmod(a, NULL, b, a);
  36055. if (ret != MP_VAL)
  36056. return -12933;
  36057. ret = mp_addmod(NULL, b, b, a);
  36058. if (ret != MP_VAL)
  36059. return -12934;
  36060. #endif
  36061. #ifdef WOLFSSL_SP_MATH_ALL
  36062. ret = mp_submod(NULL, NULL, NULL, NULL);
  36063. if (ret != MP_VAL)
  36064. return -12935;
  36065. ret = mp_submod(a, NULL, NULL, NULL);
  36066. if (ret != MP_VAL)
  36067. return -12936;
  36068. ret = mp_submod(NULL, b, NULL, NULL);
  36069. if (ret != MP_VAL)
  36070. return -12937;
  36071. ret = mp_submod(NULL, NULL, b, NULL);
  36072. if (ret != MP_VAL)
  36073. return -12938;
  36074. ret = mp_submod(NULL, NULL, NULL, a);
  36075. if (ret != MP_VAL)
  36076. return -12939;
  36077. ret = mp_submod(a, b, b, NULL);
  36078. if (ret != MP_VAL)
  36079. return -12940;
  36080. ret = mp_submod(a, b, NULL, a);
  36081. if (ret != MP_VAL)
  36082. return -12941;
  36083. ret = mp_submod(a, NULL, b, a);
  36084. if (ret != MP_VAL)
  36085. return -12942;
  36086. ret = mp_submod(NULL, b, b, a);
  36087. if (ret != MP_VAL)
  36088. return -12943;
  36089. #endif
  36090. #ifdef WOLFSSL_SP_MATH_ALL
  36091. ret = mp_div_2d(NULL, 1, a, b);
  36092. if (ret != MP_VAL)
  36093. return -12944;
  36094. ret = mp_mod_2d(NULL, 1, NULL);
  36095. if (ret != MP_VAL)
  36096. return -12945;
  36097. ret = mp_mod_2d(a, 1, NULL);
  36098. if (ret != MP_VAL)
  36099. return -12946;
  36100. ret = mp_mod_2d(NULL, 1, b);
  36101. if (ret != MP_VAL)
  36102. return -12947;
  36103. ret = mp_mul_2d(NULL, 1, NULL);
  36104. if (ret != MP_VAL)
  36105. return -12948;
  36106. ret = mp_mul_2d(a, 1, NULL);
  36107. if (ret != MP_VAL)
  36108. return -12949;
  36109. ret = mp_mul_2d(NULL, 1, b);
  36110. if (ret != MP_VAL)
  36111. return -12950;
  36112. #endif
  36113. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  36114. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  36115. ret = mp_montgomery_reduce(NULL, NULL, 1);
  36116. if (ret != MP_VAL)
  36117. return -12951;
  36118. ret = mp_montgomery_reduce(a, NULL, 1);
  36119. if (ret != MP_VAL)
  36120. return -12952;
  36121. ret = mp_montgomery_reduce(NULL, b, 1);
  36122. if (ret != MP_VAL)
  36123. return -12953;
  36124. mp_zero(b);
  36125. ret = mp_montgomery_reduce(a, b, 1);
  36126. if (ret != MP_VAL)
  36127. return -12954;
  36128. #endif
  36129. #ifdef WOLFSSL_SP_MATH_ALL
  36130. ret = mp_montgomery_setup(NULL, NULL);
  36131. if (ret != MP_VAL)
  36132. return -12955;
  36133. ret = mp_montgomery_setup(a, NULL);
  36134. if (ret != MP_VAL)
  36135. return -12956;
  36136. ret = mp_montgomery_setup(NULL, &rho);
  36137. if (ret != MP_VAL)
  36138. return -12957;
  36139. ret = mp_montgomery_calc_normalization(NULL, NULL);
  36140. if (ret != MP_VAL)
  36141. return -12958;
  36142. ret = mp_montgomery_calc_normalization(a, NULL);
  36143. if (ret != MP_VAL)
  36144. return -12959;
  36145. ret = mp_montgomery_calc_normalization(NULL, b);
  36146. if (ret != MP_VAL)
  36147. return -12960;
  36148. #endif
  36149. ret = mp_unsigned_bin_size(NULL);
  36150. if (ret != 0)
  36151. return -12961;
  36152. #if defined(WC_MP_TO_RADIX) || defined(WOLFSSL_SP_MATH_ALL)
  36153. ret = mp_tohex(NULL, NULL);
  36154. if (ret != MP_VAL)
  36155. return -12962;
  36156. ret = mp_tohex(a, NULL);
  36157. if (ret != MP_VAL)
  36158. return -12963;
  36159. ret = mp_tohex(NULL, hexStr);
  36160. if (ret != MP_VAL)
  36161. return -12964;
  36162. #endif
  36163. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  36164. ret = mp_todecimal(NULL, NULL);
  36165. if (ret != MP_VAL)
  36166. return -12965;
  36167. ret = mp_todecimal(a, NULL);
  36168. if (ret != MP_VAL)
  36169. return -12966;
  36170. ret = mp_todecimal(NULL, decStr);
  36171. if (ret != MP_VAL)
  36172. return -12967;
  36173. #endif
  36174. #ifdef WOLFSSL_SP_MATH_ALL
  36175. ret = mp_toradix(NULL, NULL, MP_RADIX_HEX);
  36176. if (ret != MP_VAL)
  36177. return -12968;
  36178. ret = mp_toradix(a, NULL, MP_RADIX_HEX);
  36179. if (ret != MP_VAL)
  36180. return -12969;
  36181. ret = mp_toradix(NULL, hexStr, MP_RADIX_HEX);
  36182. if (ret != MP_VAL)
  36183. return -12970;
  36184. ret = mp_toradix(a, hexStr, 3);
  36185. if (ret != MP_VAL)
  36186. return -12971;
  36187. ret = mp_radix_size(NULL, MP_RADIX_HEX, NULL);
  36188. if (ret != MP_VAL)
  36189. return -12972;
  36190. ret = mp_radix_size(a, MP_RADIX_HEX, NULL);
  36191. if (ret != MP_VAL)
  36192. return -12973;
  36193. ret = mp_radix_size(NULL, MP_RADIX_HEX, &size);
  36194. if (ret != MP_VAL)
  36195. return -12974;
  36196. ret = mp_radix_size(a, 3, &size);
  36197. if (ret != MP_VAL)
  36198. return -12975;
  36199. #endif
  36200. return 0;
  36201. }
  36202. #endif
  36203. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  36204. static int mp_test_set_is_bit(mp_int* a)
  36205. {
  36206. int i, j;
  36207. mp_zero(a);
  36208. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  36209. if (mp_is_bit_set(a, i))
  36210. return -12980;
  36211. for (j = 0; j < i; j++) {
  36212. if (!mp_is_bit_set(a, j))
  36213. return -12981;
  36214. }
  36215. if (mp_set_bit(a, i) != 0)
  36216. return -12982;
  36217. if (!mp_is_bit_set(a, i))
  36218. return -12983;
  36219. }
  36220. mp_zero(a);
  36221. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  36222. if (mp_is_bit_set(a, i))
  36223. return -12984;
  36224. }
  36225. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  36226. mp_zero(a);
  36227. if (mp_set_bit(a, i) != 0)
  36228. return -12985;
  36229. for (j = 0; j < i; j++) {
  36230. if (mp_is_bit_set(a, j))
  36231. return -12986;
  36232. }
  36233. if (!mp_is_bit_set(a, i))
  36234. return -12987;
  36235. }
  36236. #ifdef WOLFSSL_KEY_GEN
  36237. for (i = 0; i < DIGIT_BIT * 2; i++) {
  36238. mp_set(a, 1);
  36239. if (mp_2expt(a, i) != 0)
  36240. return -12988;
  36241. for (j = 0; j < i; j++) {
  36242. if (mp_is_bit_set(a, j))
  36243. return -12989;
  36244. }
  36245. if (!mp_is_bit_set(a, i))
  36246. return -12990;
  36247. }
  36248. #endif
  36249. #ifdef WOLFSSL_SP_MATH
  36250. mp_zero(a);
  36251. for (j = 1; j <= 3; j++) {
  36252. i = SP_INT_MAX_BITS - j;
  36253. if (mp_is_bit_set(a, i))
  36254. return -12991;
  36255. if (mp_set_bit(a, i) != 0)
  36256. return -12992;
  36257. if (!mp_is_bit_set(a, i))
  36258. return -12993;
  36259. #ifdef WOLFSSL_KEY_GEN
  36260. if (mp_2expt(a, i) != 0)
  36261. return -12994;
  36262. if (!mp_is_bit_set(a, i))
  36263. return -12995;
  36264. #endif
  36265. }
  36266. mp_zero(a);
  36267. for (j = 0; j <= 3; j++) {
  36268. i = SP_INT_MAX_BITS + j;
  36269. if (mp_is_bit_set(a, i))
  36270. return -12996;
  36271. if (mp_set_bit(a, i) != MP_VAL)
  36272. return -12997;
  36273. #ifdef WOLFSSL_KEY_GEN
  36274. if (mp_2expt(a, i) != MP_VAL)
  36275. return -12998;
  36276. #endif
  36277. }
  36278. #endif
  36279. return 0;
  36280. }
  36281. #endif /* !WOLFSSL_SP_MATH || WOLFSSL_SP_MATH_ALL */
  36282. static int mp_test_cmp(mp_int* a, mp_int* b)
  36283. {
  36284. int ret;
  36285. mp_zero(a);
  36286. mp_zero(b);
  36287. ret = mp_cmp_d(a, 0);
  36288. if (ret != MP_EQ)
  36289. return -13000;
  36290. ret = mp_cmp_d(a, 1);
  36291. if (ret != MP_LT)
  36292. return -13001;
  36293. ret = mp_cmp(a, b);
  36294. if (ret != MP_EQ)
  36295. return -13002;
  36296. mp_set(a, 1);
  36297. ret = mp_cmp_d(a, 0);
  36298. if (ret != MP_GT)
  36299. return -13003;
  36300. ret = mp_cmp_d(a, 1);
  36301. if (ret != MP_EQ)
  36302. return -13004;
  36303. ret = mp_cmp_d(a, 2);
  36304. if (ret != MP_LT)
  36305. return -13005;
  36306. ret = mp_cmp(a, b);
  36307. if (ret != MP_GT)
  36308. return -13006;
  36309. mp_read_radix(b, "1234567890123456789", MP_RADIX_HEX);
  36310. ret = mp_cmp_d(b, -1);
  36311. if (ret != MP_GT)
  36312. return -13007;
  36313. ret = mp_cmp(a, b);
  36314. if (ret != MP_LT)
  36315. return -13008;
  36316. ret = mp_cmp(b, a);
  36317. if (ret != MP_GT)
  36318. return -13009;
  36319. ret = mp_cmp(b, b);
  36320. if (ret != MP_EQ)
  36321. return -13010;
  36322. #if (!defined(WOLFSSL_SP_MATH) && !defined(WOLFSSL_SP_MATH_ALL)) || \
  36323. defined(WOLFSSL_SP_INT_NEGATIVE)
  36324. mp_read_radix(a, "-1", MP_RADIX_HEX);
  36325. mp_read_radix(a, "1", MP_RADIX_HEX);
  36326. ret = mp_cmp(a, b);
  36327. if (ret != MP_LT)
  36328. return -13011;
  36329. ret = mp_cmp(b, a);
  36330. if (ret != MP_GT)
  36331. return -13012;
  36332. mp_read_radix(b, "-2", MP_RADIX_HEX);
  36333. ret = mp_cmp(a, b);
  36334. if (ret != MP_GT)
  36335. return -13013;
  36336. ret = mp_cmp(b, a);
  36337. if (ret != MP_LT)
  36338. return -13014;
  36339. mp_read_radix(a, "-2", MP_RADIX_HEX);
  36340. ret = mp_cmp(a, b);
  36341. if (ret != MP_EQ)
  36342. return -13015;
  36343. #endif
  36344. return 0;
  36345. }
  36346. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  36347. static int mp_test_shbd(mp_int* a, mp_int* b, WC_RNG* rng)
  36348. {
  36349. int ret;
  36350. int i, j, k;
  36351. #ifndef WOLFSSL_SP_MATH
  36352. for (i = 0; i < 10; i++) {
  36353. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  36354. ret = randNum(a, j, rng, NULL);
  36355. if (ret != MP_OKAY)
  36356. return -13020;
  36357. mp_copy(a, b);
  36358. for (k = 0; k <= DIGIT_BIT * 2; k++) {
  36359. ret = mp_mul_2d(a, k, a);
  36360. if (ret != MP_OKAY)
  36361. return -13021;
  36362. mp_rshb(a, k);
  36363. if (mp_cmp(a, b) != MP_EQ)
  36364. return -13022;
  36365. }
  36366. }
  36367. }
  36368. #endif
  36369. for (i = 0; i < 10; i++) {
  36370. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  36371. ret = randNum(a, j, rng, NULL);
  36372. if (ret != MP_OKAY)
  36373. return -13023;
  36374. mp_copy(a, b);
  36375. for (k = 0; k < 10; k++) {
  36376. ret = mp_lshd(a, k);
  36377. if (ret != MP_OKAY)
  36378. return -13024;
  36379. #ifndef WOLFSSL_SP_MATH
  36380. mp_rshd(a, k);
  36381. #else
  36382. mp_rshb(a, k * SP_WORD_SIZE);
  36383. #endif
  36384. if (mp_cmp(a, b) != MP_EQ)
  36385. return -13025;
  36386. }
  36387. }
  36388. }
  36389. #ifndef WOLFSSL_SP_MATH
  36390. mp_zero(a);
  36391. mp_rshd(a, 1);
  36392. if (!mp_iszero(a))
  36393. return -13026;
  36394. mp_set(a, 1);
  36395. mp_rshd(a, 1);
  36396. if (!mp_iszero(a))
  36397. return -13027;
  36398. mp_set(a, 1);
  36399. mp_rshd(a, 2);
  36400. if (!mp_iszero(a))
  36401. return -13028;
  36402. #endif
  36403. return 0;
  36404. }
  36405. #endif
  36406. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  36407. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY) && \
  36408. !defined(WOLFSSL_RSA_PUBLIC_ONLY))
  36409. static int mp_test_div(mp_int* a, mp_int* d, mp_int* r, mp_int* rem,
  36410. WC_RNG* rng)
  36411. {
  36412. int ret;
  36413. int i, j, k;
  36414. mp_zero(a);
  36415. mp_zero(d);
  36416. ret = mp_div(a, d, r, rem);
  36417. if (ret != MP_VAL)
  36418. return -13030;
  36419. mp_set(d, 1);
  36420. ret = mp_div(a, d, r, rem);
  36421. if (ret != MP_OKAY)
  36422. return -13031;
  36423. if (!mp_iszero(r))
  36424. return -13032;
  36425. if (!mp_iszero(rem))
  36426. return -13033;
  36427. mp_set(a, 1);
  36428. ret = mp_div(a, d, r, rem);
  36429. if (ret != MP_OKAY)
  36430. return -13034;
  36431. if (!mp_isone(r))
  36432. return -13035;
  36433. if (!mp_iszero(rem))
  36434. return -13036;
  36435. for (i = 0; i < 100; i++) {
  36436. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 2; j++) {
  36437. ret = randNum(d, j, rng, NULL);
  36438. if (ret != MP_OKAY)
  36439. return -13037;
  36440. for (k = 1; k < (DIGIT_BIT + 7) / 8 * 2 + 1; k++) {
  36441. ret = randNum(a, k, rng, NULL);
  36442. if (ret != MP_OKAY)
  36443. return -13038;
  36444. ret = mp_div(a, d, NULL, rem);
  36445. if (ret != MP_OKAY)
  36446. return -13039;
  36447. ret = mp_div(a, d, r, NULL);
  36448. if (ret != MP_OKAY)
  36449. return -13040;
  36450. ret = mp_div(a, d, r, rem);
  36451. if (ret != MP_OKAY)
  36452. return -13041;
  36453. mp_mul(r, d, r);
  36454. mp_add(r, rem, r);
  36455. if (mp_cmp(r, a) != MP_EQ)
  36456. return -13042;
  36457. }
  36458. }
  36459. }
  36460. ret = randNum(d, (DIGIT_BIT + 7) / 8 * 2, rng, NULL);
  36461. if (ret != MP_OKAY)
  36462. return -13043;
  36463. mp_add(d, d, a);
  36464. mp_set(rem, 1);
  36465. mp_div(a, d, NULL, rem);
  36466. if (ret != MP_OKAY)
  36467. return -13044;
  36468. if (!mp_iszero(rem))
  36469. return -13045;
  36470. mp_set(r, 1);
  36471. mp_div(a, d, r, NULL);
  36472. if (ret != MP_OKAY)
  36473. return -13046;
  36474. if (mp_cmp_d(r, 2) != MP_EQ)
  36475. return -13047;
  36476. mp_set(r, 1);
  36477. mp_set(rem, 1);
  36478. mp_div(a, d, r, rem);
  36479. if (ret != MP_OKAY)
  36480. return -13048;
  36481. if (mp_cmp_d(r, 2) != MP_EQ)
  36482. return -13049;
  36483. if (!mp_iszero(rem))
  36484. return -13050;
  36485. mp_set(a, 0xfe);
  36486. mp_lshd(a, 3);
  36487. mp_add_d(a, 0xff, a);
  36488. mp_set(d, 0xfe);
  36489. mp_lshd(d, 2);
  36490. ret = mp_div(a, d, r, rem);
  36491. if (ret != MP_OKAY)
  36492. return -13051;
  36493. mp_mul(r, d, d);
  36494. mp_add(rem, d, d);
  36495. if (mp_cmp(a, d) != MP_EQ)
  36496. return -13052;
  36497. /* Force (hi | lo) / d to be (d | 0) / d which will would not fit in
  36498. * a digit. So mp_div must detect and handle.
  36499. * For example: 0x800000 / 0x8001, DIGIT_BIT = 8
  36500. */
  36501. mp_zero(a);
  36502. mp_set_bit(a, DIGIT_BIT * 3 - 1);
  36503. mp_zero(d);
  36504. mp_set_bit(d, DIGIT_BIT * 2 - 1);
  36505. mp_add_d(d, 1, d);
  36506. ret = mp_div(a, d, r, rem);
  36507. if (ret != MP_OKAY)
  36508. return -13053;
  36509. /* Make sure [d | d] / d is handled. */
  36510. mp_zero(a);
  36511. mp_set_bit(a, DIGIT_BIT * 2 - 1);
  36512. mp_set_bit(a, DIGIT_BIT * 1 - 1);
  36513. mp_zero(d);
  36514. mp_set_bit(d, DIGIT_BIT - 1);
  36515. ret = mp_div(a, d, r, rem);
  36516. if (ret != MP_OKAY)
  36517. return -13054;
  36518. mp_zero(a);
  36519. mp_set_bit(a, DIGIT_BIT);
  36520. mp_set_bit(a, 0);
  36521. mp_zero(d);
  36522. if (mp_cmp(r, a) != MP_EQ)
  36523. return -13055;
  36524. if (mp_cmp(rem, d) != MP_EQ)
  36525. return -13056;
  36526. return 0;
  36527. }
  36528. #endif
  36529. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  36530. !defined(WC_NO_RNG)
  36531. static int mp_test_prime(mp_int* a, WC_RNG* rng)
  36532. {
  36533. int ret;
  36534. int res;
  36535. ret = mp_rand_prime(a, 1, rng, NULL);
  36536. #if defined(WOLFSSL_SP_MATH_ALL)
  36537. if (ret != 0)
  36538. return -13060;
  36539. #else
  36540. if (ret != MP_VAL)
  36541. return -13060;
  36542. #endif
  36543. #ifndef WOLFSSL_SP_MATH
  36544. ret = mp_rand_prime(a, -5, rng, NULL);
  36545. if (ret != 0 || (a->dp[0] & 3) != 3)
  36546. return -13061;
  36547. #endif
  36548. ret = mp_prime_is_prime(a, 1, &res);
  36549. if (ret != MP_OKAY)
  36550. return -13062;
  36551. #ifndef WOLFSSL_SP_MATH
  36552. if (res != MP_YES)
  36553. return -13063;
  36554. #else
  36555. if (res != MP_NO)
  36556. return -13063;
  36557. #endif
  36558. ret = mp_prime_is_prime(a, 0, &res);
  36559. if (ret != MP_VAL)
  36560. return -13064;
  36561. ret = mp_prime_is_prime(a, -1, &res);
  36562. if (ret != MP_VAL)
  36563. return -13065;
  36564. ret = mp_prime_is_prime(a, 257, &res);
  36565. if (ret != MP_VAL)
  36566. return -13066;
  36567. mp_set(a, 1);
  36568. ret = mp_prime_is_prime(a, 1, &res);
  36569. if (ret != MP_OKAY)
  36570. return -13067;
  36571. if (res != MP_NO)
  36572. return -13068;
  36573. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  36574. if (ret != MP_OKAY)
  36575. return -13069;
  36576. if (res != MP_NO)
  36577. return -13070;
  36578. mp_set(a, 2);
  36579. ret = mp_prime_is_prime(a, 1, &res);
  36580. if (ret != MP_OKAY)
  36581. return -13071;
  36582. if (res != MP_YES)
  36583. return -13072;
  36584. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  36585. if (ret != MP_OKAY)
  36586. return -13073;
  36587. if (res != MP_YES)
  36588. return -13074;
  36589. mp_set(a, 0xfb);
  36590. ret = mp_prime_is_prime(a, 1, &res);
  36591. if (ret != MP_OKAY)
  36592. return -13075;
  36593. if (res != MP_YES)
  36594. return -13076;
  36595. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  36596. if (ret != MP_OKAY)
  36597. return -13077;
  36598. if (res != MP_YES)
  36599. return -13078;
  36600. mp_set(a, 0x6);
  36601. ret = mp_prime_is_prime(a, 1, &res);
  36602. if (ret != MP_OKAY)
  36603. return -13079;
  36604. if (res != MP_NO)
  36605. return -13080;
  36606. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  36607. if (ret != MP_OKAY)
  36608. return -13081;
  36609. if (res != MP_NO)
  36610. return -13082;
  36611. mp_set_int(a, 0x655 * 0x65b);
  36612. ret = mp_prime_is_prime(a, 10, &res);
  36613. if (ret != MP_OKAY)
  36614. return -13083;
  36615. if (res != MP_NO)
  36616. return -13084;
  36617. ret = mp_prime_is_prime_ex(a, 10, &res, rng);
  36618. if (ret != MP_OKAY)
  36619. return -13085;
  36620. if (res != MP_NO)
  36621. return -13086;
  36622. return 0;
  36623. }
  36624. #endif
  36625. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  36626. static int mp_test_lcm_gcd(mp_int* a, mp_int* b, mp_int* r, mp_int* exp,
  36627. WC_RNG* rng)
  36628. {
  36629. int ret;
  36630. int i;
  36631. WOLFSSL_SMALL_STACK_STATIC const int kat[][3] = {
  36632. { 1, 1, 1 }, { 2, 1, 2 }, { 1, 2, 2 }, { 2, 4, 4 }, { 4, 2, 4 },
  36633. { 12, 56, 168 }, { 56, 12, 168 }
  36634. };
  36635. (void)exp;
  36636. mp_set(a, 0);
  36637. mp_set(b, 1);
  36638. ret = mp_lcm(a, a, r);
  36639. if (ret != MP_VAL)
  36640. return -13090;
  36641. ret = mp_lcm(a, b, r);
  36642. if (ret != MP_VAL)
  36643. return -13091;
  36644. ret = mp_lcm(b, a, r);
  36645. if (ret != MP_VAL)
  36646. return -13092;
  36647. for (i = 0; i < (int)(sizeof(kat) / sizeof(*kat)); i++) {
  36648. mp_set(a, kat[i][0]);
  36649. mp_set(b, kat[i][1]);
  36650. ret = mp_lcm(a, b, r);
  36651. if (ret != MP_OKAY)
  36652. return -13093;
  36653. mp_set(exp, kat[i][2]);
  36654. if (mp_cmp(r, exp) != MP_EQ)
  36655. return -13094;
  36656. }
  36657. (void)rng;
  36658. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  36659. !defined(WC_NO_RNG)
  36660. if (mp_rand_prime(a, 20, rng, NULL) != MP_OKAY)
  36661. return -13095;
  36662. if (mp_rand_prime(b, 20, rng, NULL) != MP_OKAY)
  36663. return -13096;
  36664. if (mp_mul(a, b, exp) != MP_OKAY)
  36665. return -13097;
  36666. ret = mp_lcm(a, b, r);
  36667. if (ret != MP_OKAY)
  36668. return -13098;
  36669. if (mp_cmp(r, exp) != MP_EQ)
  36670. return -13099;
  36671. ret = mp_lcm(b, a, r);
  36672. if (ret != MP_OKAY)
  36673. return -13100;
  36674. if (mp_cmp(r, exp) != MP_EQ)
  36675. return -13101;
  36676. #endif
  36677. mp_set(a, 11);
  36678. mp_zero(b);
  36679. ret = mp_gcd(a, b, r);
  36680. if (ret != MP_OKAY)
  36681. return -13102;
  36682. if (mp_cmp_d(r, 11) != MP_EQ)
  36683. return -13103;
  36684. ret = mp_gcd(b, a, r);
  36685. if (ret != MP_OKAY)
  36686. return -13104;
  36687. if (mp_cmp_d(r, 11) != MP_EQ)
  36688. return -13105;
  36689. ret = mp_gcd(b, b, r);
  36690. if (ret != MP_VAL)
  36691. return -13106;
  36692. return 0;
  36693. }
  36694. #endif
  36695. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  36696. defined(WOLFSSL_SP_MATH_ALL)
  36697. static int mp_test_mod_2d(mp_int* a, mp_int* r, mp_int* t, WC_RNG* rng)
  36698. {
  36699. int ret;
  36700. int i;
  36701. int j;
  36702. mp_set(a, 10);
  36703. ret = mp_mod_2d(a, 0, r);
  36704. if (ret != MP_OKAY)
  36705. return -13110;
  36706. if (!mp_iszero(r))
  36707. return -13111;
  36708. ret = mp_mod_2d(a, 1, r);
  36709. if (ret != MP_OKAY)
  36710. return -13112;
  36711. if (!mp_iszero(r))
  36712. return -13113;
  36713. ret = mp_mod_2d(a, 2, r);
  36714. if (ret != MP_OKAY)
  36715. return -13114;
  36716. if (mp_cmp_d(r, 2))
  36717. return -13115;
  36718. for (i = 2; i < 20; i++) {
  36719. ret = randNum(a, i, rng, NULL);
  36720. if (ret != 0)
  36721. return -13116;
  36722. for (j = 1; j <= mp_count_bits(a); j++) {
  36723. /* Get top part */
  36724. ret = mp_div_2d(a, j, t, NULL);
  36725. if (ret != 0)
  36726. return -13117;
  36727. ret = mp_mul_2d(t, j, t);
  36728. if (ret != 0)
  36729. return -13118;
  36730. /* Get bottom part */
  36731. ret = mp_mod_2d(a, j, r);
  36732. if (ret != 0)
  36733. return -13119;
  36734. /* Reassemble */
  36735. ret = mp_add(t, r, r);
  36736. if (ret != 0)
  36737. return -13120;
  36738. if (mp_cmp(a, r) != MP_EQ)
  36739. return -13121;
  36740. }
  36741. }
  36742. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_SP_INT_NEGATIVE)
  36743. /* Test negative value being moded. */
  36744. for (j = 0; j < 20; j++) {
  36745. ret = randNum(a, 2, rng, NULL);
  36746. if (ret != 0)
  36747. return -13122;
  36748. a->sign = MP_NEG;
  36749. for (i = 1; i < DIGIT_BIT * 3 + 1; i++) {
  36750. ret = mp_mod_2d(a, i, r);
  36751. if (ret != 0)
  36752. return -13124;
  36753. mp_zero(t);
  36754. ret = mp_set_bit(t, i);
  36755. if (ret != 0)
  36756. return -13125;
  36757. ret = mp_mod(a, t, t);
  36758. if (ret != 0)
  36759. return -13126;
  36760. if (mp_cmp(r, t) != MP_EQ)
  36761. return -13127;
  36762. }
  36763. }
  36764. #endif
  36765. return 0;
  36766. }
  36767. #endif
  36768. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(WOLFSSL_KEY_GEN)
  36769. static int mp_test_mod_d(mp_int* a, WC_RNG* rng)
  36770. {
  36771. int ret;
  36772. mp_digit r;
  36773. mp_digit rem;
  36774. int i;
  36775. if (mp_set(a, 1) != MP_OKAY)
  36776. return -13130;
  36777. ret = mp_mod_d(a, 0, &r);
  36778. if (ret != MP_VAL)
  36779. return -13131;
  36780. mp_zero(a);
  36781. ret = mp_mod_d(a, 1, &r);
  36782. if (ret != MP_OKAY)
  36783. return -13132;
  36784. ret = mp_mod_d(a, 3, &r);
  36785. if (ret != MP_OKAY)
  36786. return -13133;
  36787. ret = mp_mod_d(a, 5, &r);
  36788. if (ret != MP_OKAY)
  36789. return -13134;
  36790. for (i = MP_MAX_TEST_BYTE_LEN - 16; i <= MP_MAX_TEST_BYTE_LEN; i++) {
  36791. ret = randNum(a, i, rng, NULL);
  36792. if (ret != MP_OKAY)
  36793. return -13135;
  36794. ret = mp_mod_d(a, 3, &r);
  36795. if (ret != MP_OKAY)
  36796. return -13136;
  36797. ret = mp_div_d(a, 3, a, &rem);
  36798. if (ret != MP_OKAY)
  36799. return -13137;
  36800. if (r != rem)
  36801. return -13138;
  36802. }
  36803. return 0;
  36804. }
  36805. #endif
  36806. static int mp_test_mul_sqr(mp_int* a, mp_int* b, mp_int* r1, mp_int* r2,
  36807. WC_RNG* rng)
  36808. {
  36809. int ret;
  36810. int i;
  36811. for (i = 1; i < 16; i++) {
  36812. ret = randNum(a, i, rng, NULL);
  36813. if (ret != 0)
  36814. return -13140;
  36815. ret = mp_mul(a, a, r1);
  36816. if (ret != 0)
  36817. return -13141;
  36818. ret = mp_sqr(a, r2);
  36819. if (ret != 0)
  36820. return -13142;
  36821. if (mp_cmp(r1, r2) != MP_EQ)
  36822. return -13143;
  36823. }
  36824. ret = mp_set(b, 0);
  36825. if (ret != MP_OKAY)
  36826. return -13144;
  36827. ret = mp_mul(a, b, r1);
  36828. if (ret != MP_OKAY)
  36829. return -13145;
  36830. if (!mp_iszero(r1))
  36831. return -13146;
  36832. ret = mp_sqr(b, r1);
  36833. if (ret != MP_OKAY)
  36834. return -13147;
  36835. if (!mp_iszero(r1))
  36836. return -13148;
  36837. #ifdef WOLFSSL_SP_MATH_ALL
  36838. ret = mp_set(a, 1);
  36839. if (ret != MP_OKAY)
  36840. return -13149;
  36841. i = (SP_INT_DIGITS / 2) + 1;
  36842. ret = mp_mul_2d(a, i * SP_WORD_SIZE - 1, a);
  36843. if (ret != MP_OKAY)
  36844. return -13150;
  36845. ret = mp_set(b, 1);
  36846. if (ret != MP_OKAY)
  36847. return -13151;
  36848. ret = mp_mul_2d(b, (SP_INT_DIGITS - 1 - i) * SP_WORD_SIZE - 1, b);
  36849. if (ret != MP_OKAY)
  36850. return -13152;
  36851. ret = mp_mul(a, b, r1);
  36852. if (ret != MP_OKAY)
  36853. return -13153;
  36854. ret = mp_mul(a, a, r1);
  36855. if (ret == MP_OKAY)
  36856. return -13154;
  36857. ret = mp_sqr(a, r1);
  36858. if (ret == MP_OKAY)
  36859. return -13155;
  36860. ret = mp_sqr(b, r1);
  36861. if (ret != MP_OKAY)
  36862. return -13156;
  36863. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  36864. (defined(HAVE_ECC) && defined(FP_ECC))
  36865. ret = mp_mulmod(a, b, b, r1);
  36866. if (ret != MP_OKAY)
  36867. return -13157;
  36868. ret = mp_mulmod(a, a, b, r1);
  36869. if (ret == MP_OKAY)
  36870. return -13158;
  36871. #if defined(HAVE_ECC) && (defined(ECC_SHAMIR) || defined(FP_ECC))
  36872. ret = mp_sqrmod(a, b, r1);
  36873. if (ret == MP_OKAY)
  36874. return -13159;
  36875. ret = mp_sqrmod(b, a, r1);
  36876. if (ret != MP_OKAY)
  36877. return -13160;
  36878. #endif /* HAVE_ECC && (ECC_SHAMIR || FP_ECC) */
  36879. #endif /* WOLFSSL_SP_MATH_ALL || WOLFSSL_HAVE_SP_DH || (HAVE_ECC && FP_ECC) */
  36880. #endif /* WOLFSSL_SP_MATH_ALL */
  36881. return 0;
  36882. }
  36883. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  36884. defined(OPENSSL_EXTRA)
  36885. static int mp_test_invmod(mp_int* a, mp_int* m, mp_int* r)
  36886. {
  36887. int ret;
  36888. mp_set(a, 0);
  36889. mp_set(m, 1);
  36890. ret = mp_invmod(a, m, r);
  36891. if (ret != MP_VAL)
  36892. return -13170;
  36893. ret = mp_invmod(m, a, r);
  36894. if (ret != MP_VAL)
  36895. return -13171;
  36896. mp_set(a, 2);
  36897. mp_set(m, 4);
  36898. ret = mp_invmod(a, m, r);
  36899. if (ret != MP_VAL)
  36900. return -13172;
  36901. mp_set(a, 3);
  36902. mp_set(m, 6);
  36903. ret = mp_invmod(a, m, r);
  36904. if (ret != MP_VAL)
  36905. return -13181;
  36906. mp_set(a, 5*9);
  36907. mp_set(m, 6*9);
  36908. ret = mp_invmod(a, m, r);
  36909. if (ret != MP_VAL)
  36910. return -13182;
  36911. mp_set(a, 1);
  36912. mp_set(m, 4);
  36913. ret = mp_invmod(a, m, r);
  36914. if (ret != MP_OKAY)
  36915. return -13173;
  36916. if (!mp_isone(r))
  36917. return -13174;
  36918. mp_set(a, 3);
  36919. mp_set(m, 4);
  36920. ret = mp_invmod(a, m, r);
  36921. if (ret != MP_OKAY)
  36922. return -13175;
  36923. if (mp_cmp_d(r, 3))
  36924. return -13176;
  36925. mp_set(a, 3);
  36926. mp_set(m, 5);
  36927. ret = mp_invmod(a, m, r);
  36928. if (ret != MP_OKAY)
  36929. return -13177;
  36930. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  36931. /* Maximum 'a' */
  36932. mp_set(a, 0);
  36933. mp_set_bit(a, (r->size / 2)* SP_WORD_SIZE - 1);
  36934. mp_sub_d(a, 1, a);
  36935. /* Modulus too big. */
  36936. mp_set(m, 0);
  36937. mp_set_bit(m, (r->size / 2) * SP_WORD_SIZE);
  36938. ret = mp_invmod(a, m, r);
  36939. if (ret != MP_VAL)
  36940. return -13178;
  36941. /* Maximum modulus - even. */
  36942. mp_set(m, 0);
  36943. mp_set_bit(m, (r->size / 2) * SP_WORD_SIZE - 1);
  36944. ret = mp_invmod(a, m, r);
  36945. if (ret != MP_OKAY)
  36946. return -13179;
  36947. #endif
  36948. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_INT_NEGATIVE)
  36949. mp_read_radix(a, "-3", 16);
  36950. ret = mp_invmod(a, m, r);
  36951. if (ret != MP_OKAY)
  36952. return -13180;
  36953. #endif
  36954. #if defined(WOLFSSL_SP_MATH_ALL) && defined(HAVE_ECC)
  36955. mp_set(a, 0);
  36956. mp_set(m, 3);
  36957. ret = mp_invmod_mont_ct(a, m, r, 1);
  36958. if (ret != MP_VAL)
  36959. return -13190;
  36960. mp_set(a, 1);
  36961. mp_set(m, 0);
  36962. ret = mp_invmod_mont_ct(a, m, r, 1);
  36963. if (ret != MP_VAL)
  36964. return -13191;
  36965. mp_set(a, 1);
  36966. mp_set(m, 1);
  36967. ret = mp_invmod_mont_ct(a, m, r, 1);
  36968. if (ret != MP_VAL)
  36969. return -13192;
  36970. mp_set(a, 1);
  36971. mp_set(m, 2);
  36972. ret = mp_invmod_mont_ct(a, m, r, 1);
  36973. if (ret != MP_VAL)
  36974. return -13193;
  36975. mp_set(a, 1);
  36976. mp_set(m, 3);
  36977. ret = mp_invmod_mont_ct(a, m, r, 1);
  36978. if (ret != MP_OKAY)
  36979. return -13194;
  36980. #endif
  36981. return 0;
  36982. }
  36983. #endif /* !NO_RSA || HAVE_ECC || !NO_DSA || OPENSSL_EXTRA */
  36984. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  36985. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  36986. static int mp_test_exptmod(mp_int* b, mp_int* e, mp_int* m, mp_int* r)
  36987. {
  36988. int ret;
  36989. mp_set(b, 0x2);
  36990. mp_set(e, 0x3);
  36991. mp_set(m, 0x0);
  36992. ret = mp_exptmod_ex(b, e, 1, m, r);
  36993. if (ret != MP_VAL)
  36994. return -13190;
  36995. ret = mp_exptmod_nct(b, e, m, r);
  36996. if (ret != MP_VAL)
  36997. return -13191;
  36998. mp_set(b, 0x2);
  36999. mp_set(e, 0x3);
  37000. mp_set(m, 0x1);
  37001. ret = mp_exptmod_ex(b, e, 1, m, r);
  37002. if (ret != MP_OKAY)
  37003. return -13192;
  37004. if (!mp_iszero(r))
  37005. return -13193;
  37006. ret = mp_exptmod_nct(b, e, m, r);
  37007. if (ret != MP_OKAY)
  37008. return -13194;
  37009. if (!mp_iszero(r))
  37010. return -13195;
  37011. mp_set(b, 0x2);
  37012. mp_set(e, 0x0);
  37013. mp_set(m, 0x7);
  37014. ret = mp_exptmod_ex(b, e, 1, m, r);
  37015. if (ret != MP_OKAY)
  37016. return -13196;
  37017. if (!mp_isone(r))
  37018. return -13197;
  37019. ret = mp_exptmod_nct(b, e, m, r);
  37020. if (ret != MP_OKAY)
  37021. return -13198;
  37022. if (!mp_isone(r))
  37023. return -13199;
  37024. mp_set(b, 0x0);
  37025. mp_set(e, 0x3);
  37026. mp_set(m, 0x7);
  37027. ret = mp_exptmod_ex(b, e, 1, m, r);
  37028. if (ret != MP_OKAY)
  37029. return -13200;
  37030. if (!mp_iszero(r))
  37031. return -13201;
  37032. ret = mp_exptmod_nct(b, e, m, r);
  37033. if (ret != MP_OKAY)
  37034. return -13202;
  37035. if (!mp_iszero(r))
  37036. return -13203;
  37037. mp_set(b, 0x10);
  37038. mp_set(e, 0x3);
  37039. mp_set(m, 0x7);
  37040. ret = mp_exptmod_ex(b, e, 1, m, r);
  37041. if (ret != MP_OKAY)
  37042. return -13204;
  37043. ret = mp_exptmod_nct(b, e, m, r);
  37044. if (ret != MP_OKAY)
  37045. return -13205;
  37046. mp_set(b, 0x7);
  37047. mp_set(e, 0x3);
  37048. mp_set(m, 0x7);
  37049. ret = mp_exptmod_ex(b, e, 1, m, r);
  37050. if (ret != MP_OKAY)
  37051. return -13206;
  37052. if (!mp_iszero(r))
  37053. return -13207;
  37054. ret = mp_exptmod_nct(b, e, m, r);
  37055. if (ret != MP_OKAY)
  37056. return -13208;
  37057. if (!mp_iszero(r))
  37058. return -13209;
  37059. #ifndef WOLFSSL_SP_MATH
  37060. mp_set(b, 0x01);
  37061. mp_mul_2d(b, DIGIT_BIT, b);
  37062. mp_add_d(b, 1, b);
  37063. mp_set(e, 0x3);
  37064. mp_copy(b, m);
  37065. ret = mp_exptmod_ex(b, e, 1, m, r);
  37066. if (ret != MP_OKAY)
  37067. return -13210;
  37068. if (!mp_iszero(r))
  37069. return -13211;
  37070. ret = mp_exptmod_nct(b, e, m, r);
  37071. if (ret != MP_OKAY)
  37072. return -13212;
  37073. if (!mp_iszero(r))
  37074. return -13213;
  37075. #endif
  37076. mp_set(b, 0x2);
  37077. mp_set(e, 0x3);
  37078. mp_set(m, 0x7);
  37079. ret = mp_exptmod_ex(b, e, 1, m, r);
  37080. if (ret != MP_OKAY)
  37081. return -13214;
  37082. ret = mp_exptmod_nct(b, e, m, r);
  37083. if (ret != MP_OKAY)
  37084. return -13215;
  37085. #ifdef WOLFSSL_SP_MATH_ALL
  37086. mp_set(b, 0x2);
  37087. mp_set(e, 0x3);
  37088. mp_set(m, 0x01);
  37089. mp_mul_2d(m, SP_WORD_SIZE * SP_INT_DIGITS / 2, m);
  37090. mp_add_d(m, 0x01, m);
  37091. ret = mp_exptmod_ex(b, e, 1, m, r);
  37092. if (ret != MP_VAL)
  37093. return -13216;
  37094. ret = mp_exptmod_nct(b, e, m, r);
  37095. if (ret != MP_VAL)
  37096. return -13217;
  37097. #endif
  37098. return 0;
  37099. }
  37100. #endif /* !NO_RSA || !NO_DSA || !NO_DH || (HAVE_ECC && HAVE_COMP_KEY) ||
  37101. * OPENSSL_EXTRA */
  37102. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  37103. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  37104. static int mp_test_mont(mp_int* a, mp_int* m, mp_int* n, mp_int* r, WC_RNG* rng)
  37105. {
  37106. int ret;
  37107. mp_digit mp;
  37108. static int exp[] = { 7, 8, 16, 27, 32, 64,
  37109. 127, 128, 255, 256,
  37110. #if defined(SP_WORD_SIZE) && SP_WORD_SIZE > 8
  37111. 383, 384, 2033, 2048
  37112. #endif
  37113. };
  37114. static mp_digit sub[] = { 0x01, 0x05, 0x0f, 0x27, 0x05, 0x3b,
  37115. 0x01, 0x9f, 0x13, 0xbd,
  37116. #if defined(SP_WORD_SIZE) && SP_WORD_SIZE > 8
  37117. 0x1f, 0x13d, 0x45, 0x615
  37118. #endif
  37119. };
  37120. int bits[] = { 256, 384,
  37121. #if defined(SP_INT_MAX_BITS) && SP_INT_MAX_BITS > 4096
  37122. 2048,
  37123. #endif
  37124. #if defined(SP_INT_MAX_BITS) && SP_INT_MAX_BITS > 6144
  37125. 3072
  37126. #endif
  37127. };
  37128. int i;
  37129. int j;
  37130. for (i = 0; i < (int)(sizeof(exp) / sizeof(*exp)); i++) {
  37131. if (exp[i] >= DIGIT_BIT)
  37132. continue;
  37133. mp_zero(m);
  37134. ret = mp_set_bit(m, exp[i]);
  37135. if (ret != MP_OKAY)
  37136. return -13220;
  37137. ret = mp_sub_d(m, sub[i], m);
  37138. if (ret != MP_OKAY)
  37139. return -13221;
  37140. ret = mp_montgomery_setup(m, &mp);
  37141. if (ret != MP_OKAY)
  37142. return -13222;
  37143. ret = mp_montgomery_calc_normalization(n, m);
  37144. if (ret != MP_OKAY)
  37145. return -13223;
  37146. for (j = 0; j < 10; j++) {
  37147. ret = randNum(a, (exp[i] + DIGIT_BIT - 1) / DIGIT_BIT, rng, NULL);
  37148. if (ret != 0)
  37149. return -13224;
  37150. ret = mp_mod(a, m, a);
  37151. if (ret != 0)
  37152. return -13225;
  37153. /* r = a * a */
  37154. ret = mp_sqrmod(a, m, r);
  37155. if (ret != MP_OKAY)
  37156. return -13226;
  37157. /* Convert to Montgomery form = a*n */
  37158. ret = mp_mulmod(a, n, m, a);
  37159. if (ret != MP_OKAY)
  37160. return -13227;
  37161. /* a*a mod m == ((a*n) * (a*n)) / n / n */
  37162. ret = mp_sqr(a, a);
  37163. if (ret != MP_OKAY)
  37164. return -13228;
  37165. ret = mp_montgomery_reduce(a, m, mp);
  37166. if (ret != MP_OKAY)
  37167. return -13229;
  37168. ret = mp_montgomery_reduce(a, m, mp);
  37169. if (ret != MP_OKAY)
  37170. return -13230;
  37171. if (mp_cmp(a, r) != MP_EQ)
  37172. return -13231;
  37173. }
  37174. }
  37175. /* Force carries. */
  37176. for (i = 0; i < (int)(sizeof(bits) / sizeof(*bits)); i++) {
  37177. /* a = 2^(bits*2) - 1 */
  37178. mp_zero(a);
  37179. mp_set_bit(a, bits[i] * 2);
  37180. mp_sub_d(a, 1, a);
  37181. /* m = 2^(bits) - 1 */
  37182. mp_zero(m);
  37183. mp_set_bit(m, bits[i]);
  37184. mp_sub_d(m, 1, m);
  37185. mp = 1;
  37186. /* result = r = 2^(bits) - 1 */
  37187. mp_zero(r);
  37188. mp_set_bit(r, bits[i]);
  37189. mp_sub_d(r, 1, r);
  37190. ret = mp_montgomery_reduce(a, m, mp);
  37191. if (ret != MP_OKAY)
  37192. return -13240;
  37193. /* Result is m or 0 if reduced to range of modulus. */
  37194. if (mp_cmp(a, r) != MP_EQ && mp_iszero(a) != MP_YES)
  37195. return -13241;
  37196. }
  37197. return 0;
  37198. }
  37199. #endif
  37200. WOLFSSL_TEST_SUBROUTINE int mp_test(void)
  37201. {
  37202. WC_RNG rng;
  37203. int ret;
  37204. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  37205. int i, j;
  37206. #ifndef WOLFSSL_SP_MATH
  37207. int k;
  37208. #endif
  37209. mp_digit d;
  37210. #endif
  37211. mp_int a, b, r1, r2, p;
  37212. ret = mp_init_multi(&a, &b, &r1, &r2, NULL, NULL);
  37213. if (ret != 0)
  37214. return -13300;
  37215. #ifdef WOLFSSL_SP_MATH_ALL
  37216. mp_init_copy(&p, &a);
  37217. #else
  37218. ret = mp_init(&p);
  37219. if (ret != 0)
  37220. return -13301;
  37221. #endif
  37222. #ifndef HAVE_FIPS
  37223. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  37224. #else
  37225. ret = wc_InitRng(&rng);
  37226. #endif
  37227. if (ret != 0)
  37228. goto done;
  37229. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  37230. mp_set_int(&a, 0);
  37231. if (a.used != 0 || a.dp[0] != 0)
  37232. return -13302;
  37233. for (j = 1; j <= MP_MAX_TEST_BYTE_LEN; j++) {
  37234. for (i = 0; i < 4 * j; i++) {
  37235. /* New values to use. */
  37236. ret = randNum(&p, j, &rng, NULL);
  37237. if (ret != 0)
  37238. return -13303;
  37239. ret = randNum(&a, j, &rng, NULL);
  37240. if (ret != 0)
  37241. return -13304;
  37242. ret = randNum(&b, j, &rng, NULL);
  37243. if (ret != 0)
  37244. return -13305;
  37245. ret = wc_RNG_GenerateBlock(&rng, (byte*)&d, sizeof(d));
  37246. if (ret != 0)
  37247. return -13306;
  37248. d &= MP_MASK;
  37249. #if !defined(WOLFSSL_SP_MATH) || (defined(HAVE_ECC) && \
  37250. (defined(ECC_SHAMIR) || defined(FP_ECC)))
  37251. /* Ensure sqrmod produce same result as mulmod. */
  37252. ret = mp_sqrmod(&a, &p, &r1);
  37253. if (ret != 0)
  37254. return -13307;
  37255. ret = mp_mulmod(&a, &a, &p, &r2);
  37256. if (ret != 0)
  37257. return -13308;
  37258. if (mp_cmp(&r1, &r2) != 0)
  37259. return -13309;
  37260. #endif
  37261. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37262. #if defined(WOLFSSL_SP_MATH) || (defined(WOLFSSL_SP_MATH_ALL) && \
  37263. !defined(WOLFSSL_SP_INT_NEGATIVE))
  37264. ret = mp_addmod(&a, &b, &p, &r1);
  37265. if (ret != 0)
  37266. return -13310;
  37267. ret = mp_submod(&r1, &b, &p, &r2);
  37268. if (ret != 0)
  37269. return -13311;
  37270. ret = mp_mod(&a, &p, &r1);
  37271. if (ret != 0)
  37272. return -13312;
  37273. if (mp_cmp(&r1, &r2) != MP_EQ)
  37274. return -13313;
  37275. #else
  37276. /* Ensure add with mod produce same result as sub with mod. */
  37277. ret = mp_addmod(&a, &b, &p, &r1);
  37278. if (ret != 0)
  37279. return -13314;
  37280. b.sign ^= 1;
  37281. ret = mp_submod(&a, &b, &p, &r2);
  37282. if (ret != 0)
  37283. return -13315;
  37284. if (mp_cmp(&r1, &r2) != 0)
  37285. return -13316;
  37286. #endif
  37287. #endif
  37288. /* Ensure add digit produce same result as sub digit. */
  37289. ret = mp_add_d(&a, d, &r1);
  37290. if (ret != 0)
  37291. return -13317;
  37292. ret = mp_sub_d(&r1, d, &r2);
  37293. if (ret != 0)
  37294. return -13318;
  37295. if (mp_cmp(&a, &r2) != 0)
  37296. return -13319;
  37297. /* Invert - if p is even it will use the slow impl.
  37298. * - if p and a are even it will fail.
  37299. */
  37300. ret = mp_invmod(&a, &p, &r1);
  37301. if (ret != 0 && ret != MP_VAL)
  37302. return -13320;
  37303. ret = 0;
  37304. #ifndef WOLFSSL_SP_MATH
  37305. /* Shift up and down number all bits in a digit. */
  37306. for (k = 0; k < DIGIT_BIT; k++) {
  37307. mp_mul_2d(&a, k, &r1);
  37308. mp_div_2d(&r1, k, &r2, &p);
  37309. if (mp_cmp(&a, &r2) != 0)
  37310. return -13321;
  37311. if (!mp_iszero(&p))
  37312. return -13322;
  37313. mp_rshb(&r1, k);
  37314. if (mp_cmp(&a, &r1) != 0)
  37315. return -13323;
  37316. }
  37317. #endif
  37318. }
  37319. }
  37320. /* Test adding and subtracting zero from zero. */
  37321. mp_zero(&a);
  37322. ret = mp_add_d(&a, 0, &r1);
  37323. if (ret != 0)
  37324. return -13329;
  37325. if (!mp_iszero(&r1)) {
  37326. return -13330;
  37327. }
  37328. ret = mp_sub_d(&a, 0, &r2);
  37329. if (ret != 0)
  37330. return -13331;
  37331. if (!mp_iszero(&r2)) {
  37332. return -13332;
  37333. }
  37334. #if DIGIT_BIT >= 32
  37335. /* Check that setting a 32-bit digit works. */
  37336. d &= 0xffffffffU;
  37337. mp_set_int(&a, d);
  37338. if (a.used != 1 || a.dp[0] != d)
  37339. return -13324;
  37340. #endif
  37341. /* Check setting a bit and testing a bit works. */
  37342. for (i = 0; i < MP_MAX_TEST_BYTE_LEN * 8; i++) {
  37343. mp_zero(&a);
  37344. mp_set_bit(&a, i);
  37345. if (!mp_is_bit_set(&a, i))
  37346. return -13325;
  37347. }
  37348. #endif
  37349. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  37350. mp_zero(&a);
  37351. i = mp_cnt_lsb(&a);
  37352. if (i != 0)
  37353. return -13326;
  37354. mp_set(&a, 1);
  37355. i = mp_cnt_lsb(&a);
  37356. if (i != 0)
  37357. return -13327;
  37358. mp_set(&a, 32);
  37359. i = mp_cnt_lsb(&a);
  37360. if (i != 5)
  37361. return -13328;
  37362. mp_zero(&a);
  37363. mp_set_bit(&a, 129);
  37364. i = mp_cnt_lsb(&a);
  37365. if (i != 129)
  37366. return -13328;
  37367. #endif
  37368. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  37369. if ((ret = mp_test_param(&a, &b, &r1, &rng)) != 0)
  37370. return ret;
  37371. #endif
  37372. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  37373. if ((ret = mp_test_div_3(&a, &r1, &rng)) != 0)
  37374. return ret;
  37375. #endif
  37376. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  37377. !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  37378. (!defined WOLFSSL_SP_MATH && !defined(WOLFSSL_SP_MATH_ALL) && \
  37379. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  37380. if ((ret = mp_test_radix_10(&a, &r1, &rng)) != 0)
  37381. return ret;
  37382. #endif
  37383. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  37384. defined(HAVE_ECC))
  37385. if ((ret = mp_test_radix_16(&a, &r1, &rng)) != 0)
  37386. return ret;
  37387. #endif
  37388. if ((ret = mp_test_shift(&a, &r1, &rng)) != 0)
  37389. return ret;
  37390. if ((ret = mp_test_add_sub_d(&a, &r1)) != 0)
  37391. return ret;
  37392. if ((ret = mp_test_read_to_bin(&a)) != 0)
  37393. return ret;
  37394. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  37395. if ((ret = mp_test_set_int(&a)) != 0)
  37396. return ret;
  37397. #endif
  37398. if ((ret = mp_test_cmp(&a, &r1)) != 0)
  37399. return ret;
  37400. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37401. if ((ret = mp_test_shbd(&a, &b, &rng)) != 0)
  37402. return ret;
  37403. #endif
  37404. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  37405. if ((ret = mp_test_set_is_bit(&a)) != 0)
  37406. return ret;
  37407. #endif
  37408. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  37409. if ((ret = mp_test_div(&a, &b, &r1, &r2, &rng)) != 0)
  37410. return ret;
  37411. #endif
  37412. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  37413. !defined(WC_NO_RNG)
  37414. if ((ret = mp_test_prime(&a, &rng)) != 0)
  37415. return ret;
  37416. #endif
  37417. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  37418. if ((ret = mp_test_lcm_gcd(&a, &b, &r1, &r2, &rng)) != 0)
  37419. return ret;
  37420. #endif
  37421. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  37422. defined(WOLFSSL_SP_MATH_ALL)
  37423. if ((ret = mp_test_mod_2d(&a, &r1, &p, &rng)) != 0)
  37424. return ret;
  37425. #endif
  37426. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(WOLFSSL_KEY_GEN)
  37427. if ((ret = mp_test_mod_d(&a, &rng)) != 0)
  37428. return ret;
  37429. #endif
  37430. if ((ret = mp_test_mul_sqr(&a, &b, &r1, &r2, &rng)) != 0)
  37431. return ret;
  37432. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  37433. defined(OPENSSL_EXTRA)
  37434. if ((ret = mp_test_invmod(&a, &b, &r1)) != 0)
  37435. return ret;
  37436. #endif
  37437. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  37438. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  37439. if ((ret = mp_test_exptmod(&a, &b, &r1, &r2)) != 0)
  37440. return ret;
  37441. #endif
  37442. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  37443. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  37444. if ((ret = mp_test_mont(&a, &b, &r1, &r2, &rng)) != 0)
  37445. return ret;
  37446. #endif
  37447. done:
  37448. mp_clear(&p);
  37449. mp_clear(&r2);
  37450. mp_clear(&r1);
  37451. mp_clear(&b);
  37452. mp_clear(&a);
  37453. wc_FreeRng(&rng);
  37454. return ret;
  37455. }
  37456. #endif
  37457. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  37458. typedef struct pairs_t {
  37459. const unsigned char* coeff;
  37460. int coeffSz;
  37461. int exp;
  37462. } pairs_t;
  37463. /*
  37464. n =p1p2p3, where pi = ki(p1-1)+1 with (k2,k3) = (173,293)
  37465. p1 = 2^192 * 0x000000000000e24fd4f6d6363200bf2323ec46285cac1d3a
  37466. + 2^0 * 0x0b2488b0c29d96c5e67f8bec15b54b189ae5636efe89b45b
  37467. */
  37468. static const unsigned char c192a[] =
  37469. {
  37470. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe2, 0x4f,
  37471. 0xd4, 0xf6, 0xd6, 0x36, 0x32, 0x00, 0xbf, 0x23,
  37472. 0x23, 0xec, 0x46, 0x28, 0x5c, 0xac, 0x1d, 0x3a
  37473. };
  37474. static const unsigned char c0a[] =
  37475. {
  37476. 0x0b, 0x24, 0x88, 0xb0, 0xc2, 0x9d, 0x96, 0xc5,
  37477. 0xe6, 0x7f, 0x8b, 0xec, 0x15, 0xb5, 0x4b, 0x18,
  37478. 0x9a, 0xe5, 0x63, 0x6e, 0xfe, 0x89, 0xb4, 0x5b
  37479. };
  37480. static const pairs_t ecPairsA[] =
  37481. {
  37482. {c192a, sizeof(c192a), 192},
  37483. {c0a, sizeof(c0a), 0}
  37484. };
  37485. static const int kA[] = {173, 293};
  37486. static const unsigned char controlPrime[] = {
  37487. 0xe1, 0x76, 0x45, 0x80, 0x59, 0xb6, 0xd3, 0x49,
  37488. 0xdf, 0x0a, 0xef, 0x12, 0xd6, 0x0f, 0xf0, 0xb7,
  37489. 0xcb, 0x2a, 0x37, 0xbf, 0xa7, 0xf8, 0xb5, 0x4d,
  37490. 0xf5, 0x31, 0x35, 0xad, 0xe4, 0xa3, 0x94, 0xa1,
  37491. 0xdb, 0xf1, 0x96, 0xad, 0xb5, 0x05, 0x64, 0x85,
  37492. 0x83, 0xfc, 0x1b, 0x5b, 0x29, 0xaa, 0xbe, 0xf8,
  37493. 0x26, 0x3f, 0x76, 0x7e, 0xad, 0x1c, 0xf0, 0xcb,
  37494. 0xd7, 0x26, 0xb4, 0x1b, 0x05, 0x8e, 0x56, 0x86,
  37495. 0x7e, 0x08, 0x62, 0x21, 0xc1, 0x86, 0xd6, 0x47,
  37496. 0x79, 0x3e, 0xb7, 0x5d, 0xa4, 0xc6, 0x3a, 0xd7,
  37497. 0xb1, 0x74, 0x20, 0xf6, 0x50, 0x97, 0x41, 0x04,
  37498. 0x53, 0xed, 0x3f, 0x26, 0xd6, 0x6f, 0x91, 0xfa,
  37499. 0x68, 0x26, 0xec, 0x2a, 0xdc, 0x9a, 0xf1, 0xe7,
  37500. 0xdc, 0xfb, 0x73, 0xf0, 0x79, 0x43, 0x1b, 0x21,
  37501. 0xa3, 0x59, 0x04, 0x63, 0x52, 0x07, 0xc9, 0xd7,
  37502. 0xe6, 0xd1, 0x1b, 0x5d, 0x5e, 0x96, 0xfa, 0x53
  37503. };
  37504. static const unsigned char testOne[] = { 1 };
  37505. static int GenerateNextP(mp_int* p1, mp_int* p2, int k)
  37506. {
  37507. int ret;
  37508. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  37509. mp_int *ki = (mp_int *)XMALLOC(sizeof(*ki), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37510. if (ki == NULL)
  37511. return MEMORY_E;
  37512. #else
  37513. mp_int ki[1];
  37514. #endif
  37515. ret = mp_init(ki);
  37516. if (ret == 0)
  37517. ret = mp_set(ki, k);
  37518. if (ret == 0)
  37519. ret = mp_sub_d(p1, 1, p2);
  37520. if (ret == 0)
  37521. ret = mp_mul(p2, ki, p2);
  37522. if (ret == 0)
  37523. ret = mp_add_d(p2, 1, p2);
  37524. mp_clear(ki);
  37525. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  37526. XFREE(ki, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37527. #endif
  37528. return ret;
  37529. }
  37530. static int GenerateP(mp_int* p1, mp_int* p2, mp_int* p3,
  37531. const pairs_t* ecPairs, int ecPairsSz,
  37532. const int* k)
  37533. {
  37534. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  37535. mp_int *x = NULL, *y = NULL;
  37536. #else
  37537. mp_int x[1], y[1];
  37538. #endif
  37539. int ret, i;
  37540. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  37541. if (((x = (mp_int *)XMALLOC(sizeof(*x), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL) ||
  37542. ((y = (mp_int *)XMALLOC(sizeof(*x), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)) {
  37543. ret = MEMORY_E;
  37544. goto out;
  37545. }
  37546. #endif
  37547. ret = mp_init_multi(x, y, NULL, NULL, NULL, NULL);
  37548. if (ret != 0) {
  37549. ret = MP_MEM;
  37550. goto out;
  37551. }
  37552. for (i = 0; ret == 0 && i < ecPairsSz; i++) {
  37553. ret = mp_read_unsigned_bin(x, ecPairs[i].coeff, ecPairs[i].coeffSz);
  37554. /* p1 = 2^exp */
  37555. if (ret == 0)
  37556. ret = mp_2expt(y, ecPairs[i].exp);
  37557. /* p1 = p1 * m */
  37558. if (ret == 0)
  37559. ret = mp_mul(x, y, x);
  37560. /* p1 += */
  37561. if (ret == 0)
  37562. ret = mp_add(p1, x, p1);
  37563. mp_zero(x);
  37564. mp_zero(y);
  37565. }
  37566. if (ret == 0)
  37567. ret = GenerateNextP(p1, p2, k[0]);
  37568. if (ret == 0)
  37569. ret = GenerateNextP(p1, p3, k[1]);
  37570. out:
  37571. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  37572. if (x != NULL) {
  37573. mp_clear(x);
  37574. XFREE(x, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37575. }
  37576. if (y != NULL) {
  37577. mp_clear(y);
  37578. XFREE(y, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37579. }
  37580. #else
  37581. mp_clear(x);
  37582. mp_clear(y);
  37583. #endif
  37584. return ret;
  37585. }
  37586. WOLFSSL_TEST_SUBROUTINE int prime_test(void)
  37587. {
  37588. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  37589. mp_int *n = (mp_int *)XMALLOC(sizeof *n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  37590. *p1 = (mp_int *)XMALLOC(sizeof *p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  37591. *p2 = (mp_int *)XMALLOC(sizeof *p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  37592. *p3 = (mp_int *)XMALLOC(sizeof *p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37593. #else
  37594. mp_int n[1],
  37595. p1[1],
  37596. p2[1],
  37597. p3[1];
  37598. #endif
  37599. int ret, isPrime = 0;
  37600. WC_RNG rng;
  37601. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  37602. if ((n == NULL) ||
  37603. (p1 == NULL) ||
  37604. (p2 == NULL) ||
  37605. (p3 == NULL))
  37606. ERROR_OUT(MEMORY_E, out);
  37607. #endif
  37608. ret = wc_InitRng(&rng);
  37609. if (ret == 0)
  37610. ret = mp_init_multi(n, p1, p2, p3, NULL, NULL);
  37611. if (ret == 0)
  37612. ret = GenerateP(p1, p2, p3,
  37613. ecPairsA, sizeof(ecPairsA) / sizeof(ecPairsA[0]), kA);
  37614. if (ret == 0)
  37615. ret = mp_mul(p1, p2, n);
  37616. if (ret == 0)
  37617. ret = mp_mul(n, p3, n);
  37618. if (ret != 0)
  37619. ERROR_OUT(-13400, out);
  37620. /* Check the old prime test using the number that false positives.
  37621. * This test result should indicate as not prime. */
  37622. ret = mp_prime_is_prime(n, 40, &isPrime);
  37623. if (ret != 0)
  37624. ERROR_OUT(-13401, out);
  37625. if (isPrime)
  37626. ERROR_OUT(-13402, out);
  37627. /* This test result should fail. It should indicate the value as prime. */
  37628. ret = mp_prime_is_prime(n, 8, &isPrime);
  37629. if (ret != 0)
  37630. ERROR_OUT(-13403, out);
  37631. if (!isPrime)
  37632. ERROR_OUT(-13404, out);
  37633. /* This test result should indicate the value as not prime. */
  37634. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  37635. if (ret != 0)
  37636. ERROR_OUT(-13405, out);
  37637. if (isPrime)
  37638. ERROR_OUT(-13406, out);
  37639. ret = mp_read_unsigned_bin(n, controlPrime, sizeof(controlPrime));
  37640. if (ret != 0)
  37641. ERROR_OUT(-13407, out);
  37642. /* This test result should indicate the value as prime. */
  37643. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  37644. if (ret != 0)
  37645. ERROR_OUT(-13408, out);
  37646. if (!isPrime)
  37647. ERROR_OUT(-13409, out);
  37648. /* This test result should indicate the value as prime. */
  37649. isPrime = -1;
  37650. ret = mp_prime_is_prime(n, 8, &isPrime);
  37651. if (ret != 0)
  37652. ERROR_OUT(-13410, out);
  37653. if (!isPrime)
  37654. ERROR_OUT(-13411, out);
  37655. ret = mp_read_unsigned_bin(n, testOne, sizeof(testOne));
  37656. if (ret != 0)
  37657. ERROR_OUT(-13412, out);
  37658. /* This test result should indicate the value as not prime. */
  37659. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  37660. if (ret != 0)
  37661. ERROR_OUT(-13413, out);
  37662. if (isPrime)
  37663. ERROR_OUT(-13414, out);
  37664. ret = mp_prime_is_prime(n, 8, &isPrime);
  37665. if (ret != 0)
  37666. ERROR_OUT(-13415, out);
  37667. if (isPrime)
  37668. ERROR_OUT(-13416, out);
  37669. ret = 0;
  37670. out:
  37671. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  37672. if (n != NULL) {
  37673. mp_clear(n);
  37674. XFREE(n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37675. }
  37676. if (p1 != NULL) {
  37677. mp_clear(p1);
  37678. XFREE(p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37679. }
  37680. if (p2 != NULL) {
  37681. mp_clear(p2);
  37682. XFREE(p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37683. }
  37684. if (p3 != NULL) {
  37685. mp_clear(p3);
  37686. XFREE(p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37687. }
  37688. #else
  37689. mp_clear(p3);
  37690. mp_clear(p2);
  37691. mp_clear(p1);
  37692. mp_clear(n);
  37693. #endif
  37694. wc_FreeRng(&rng);
  37695. return ret;
  37696. }
  37697. #endif /* WOLFSSL_PUBLIC_MP */
  37698. #if defined(ASN_BER_TO_DER) && \
  37699. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  37700. defined(OPENSSL_EXTRA_X509_SMALL))
  37701. /* wc_BerToDer is only public facing in the case of test cert or opensslextra */
  37702. typedef struct berDerTestData {
  37703. const byte *in;
  37704. word32 inSz;
  37705. const byte *out;
  37706. word32 outSz;
  37707. } berDerTestData;
  37708. WOLFSSL_TEST_SUBROUTINE int berder_test(void)
  37709. {
  37710. int ret;
  37711. int i;
  37712. word32 len = 0, l;
  37713. byte out[32];
  37714. WOLFSSL_SMALL_STACK_STATIC const byte good1_in[] = { 0x30, 0x80, 0x00, 0x00 };
  37715. WOLFSSL_SMALL_STACK_STATIC const byte good1_out[] = { 0x30, 0x00 };
  37716. WOLFSSL_SMALL_STACK_STATIC const byte good2_in[] = { 0x30, 0x80, 0x02, 0x01, 0x01, 0x00, 0x00 };
  37717. WOLFSSL_SMALL_STACK_STATIC const byte good2_out[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  37718. WOLFSSL_SMALL_STACK_STATIC const byte good3_in[] = {
  37719. 0x24, 0x80, 0x04, 0x01, 0x01, 0x00, 0x00
  37720. };
  37721. WOLFSSL_SMALL_STACK_STATIC const byte good3_out[] = { 0x04, 0x1, 0x01 };
  37722. WOLFSSL_SMALL_STACK_STATIC const byte good4_in[] = {
  37723. 0x30, 0x80,
  37724. 0x02, 0x01, 0x01,
  37725. 0x30, 0x80,
  37726. 0x24, 0x80,
  37727. 0x04, 0x01, 0x01,
  37728. 0x04, 0x02, 0x02, 0x03,
  37729. 0x00, 0x00,
  37730. 0x06, 0x01, 0x01,
  37731. 0x00, 0x00,
  37732. 0x31, 0x80,
  37733. 0x06, 0x01, 0x01,
  37734. 0x00, 0x00,
  37735. 0x00, 0x00,
  37736. };
  37737. WOLFSSL_SMALL_STACK_STATIC const byte good4_out[] = {
  37738. 0x30, 0x12,
  37739. 0x02, 0x01, 0x01,
  37740. 0x30, 0x08,
  37741. 0x04, 0x03, 0x01, 0x02, 0x03,
  37742. 0x06, 0x01, 0x01,
  37743. 0x31, 0x03,
  37744. 0x06, 0x01, 0x01
  37745. };
  37746. WOLFSSL_SMALL_STACK_STATIC const byte good5_in[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  37747. berDerTestData testData[] = {
  37748. { good1_in, sizeof(good1_in), good1_out, sizeof(good1_out) },
  37749. { good2_in, sizeof(good2_in), good2_out, sizeof(good2_out) },
  37750. { good3_in, sizeof(good3_in), good3_out, sizeof(good3_out) },
  37751. { good4_in, sizeof(good4_in), good4_out, sizeof(good4_out) },
  37752. { good5_in, sizeof(good5_in), good5_in , sizeof(good5_in ) },
  37753. };
  37754. for (i = 0; i < (int)(sizeof(testData) / sizeof(*testData)); i++) {
  37755. ret = wc_BerToDer(testData[i].in, testData[i].inSz, NULL, &len);
  37756. if (ret != LENGTH_ONLY_E)
  37757. return -13500 - i;
  37758. if (len != testData[i].outSz)
  37759. return -13510 - i;
  37760. len = testData[i].outSz;
  37761. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &len);
  37762. if (ret != 0)
  37763. return -13520 - i;
  37764. if (XMEMCMP(out, testData[i].out, len) != 0)
  37765. return -13530 - i;
  37766. for (l = 1; l < testData[i].inSz; l++) {
  37767. ret = wc_BerToDer(testData[i].in, l, NULL, &len);
  37768. if (ret != ASN_PARSE_E)
  37769. return -13540;
  37770. len = testData[i].outSz;
  37771. ret = wc_BerToDer(testData[i].in, l, out, &len);
  37772. if (ret != ASN_PARSE_E)
  37773. return -13541;
  37774. }
  37775. for (l = 0; l < testData[i].outSz-1; l++) {
  37776. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &l);
  37777. if (ret != BUFFER_E)
  37778. return -13542;
  37779. }
  37780. }
  37781. ret = wc_BerToDer(NULL, 4, NULL, NULL);
  37782. if (ret != BAD_FUNC_ARG)
  37783. return -13543;
  37784. ret = wc_BerToDer(out, 4, NULL, NULL);
  37785. if (ret != BAD_FUNC_ARG)
  37786. return -13544;
  37787. ret = wc_BerToDer(NULL, 4, NULL, &len);
  37788. if (ret != BAD_FUNC_ARG)
  37789. return -13545;
  37790. ret = wc_BerToDer(NULL, 4, out, NULL);
  37791. if (ret != BAD_FUNC_ARG)
  37792. return -13546;
  37793. ret = wc_BerToDer(out, 4, out, NULL);
  37794. if (ret != BAD_FUNC_ARG)
  37795. return -13547;
  37796. ret = wc_BerToDer(NULL, 4, out, &len);
  37797. if (ret != BAD_FUNC_ARG)
  37798. return -13548;
  37799. for (l = 1; l < sizeof(good4_out); l++) {
  37800. len = l;
  37801. ret = wc_BerToDer(good4_in, sizeof(good4_in), out, &len);
  37802. if (ret != BUFFER_E)
  37803. return -13549;
  37804. }
  37805. return 0;
  37806. }
  37807. #endif
  37808. #ifdef DEBUG_WOLFSSL
  37809. static THREAD_LS_T int log_cnt = 0;
  37810. static void my_Logging_cb(const int logLevel, const char *const logMessage)
  37811. {
  37812. (void)logLevel;
  37813. (void)logMessage;
  37814. log_cnt++;
  37815. }
  37816. #endif /* DEBUG_WOLFSSL */
  37817. WOLFSSL_TEST_SUBROUTINE int logging_test(void)
  37818. {
  37819. #ifdef DEBUG_WOLFSSL
  37820. const char* msg = "Testing, testing. 1, 2, 3, 4 ...";
  37821. byte a[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  37822. byte b[256];
  37823. int i;
  37824. for (i = 0; i < (int)sizeof(b); i++)
  37825. b[i] = i;
  37826. if (wolfSSL_Debugging_ON() != 0)
  37827. return -13600;
  37828. if (wolfSSL_SetLoggingCb(my_Logging_cb) != 0)
  37829. return -13601;
  37830. WOLFSSL_MSG(msg);
  37831. WOLFSSL_BUFFER(a, sizeof(a));
  37832. WOLFSSL_BUFFER(b, sizeof(b));
  37833. WOLFSSL_BUFFER(NULL, 0);
  37834. WOLFSSL_ERROR(MEMORY_E);
  37835. WOLFSSL_ERROR_MSG(msg);
  37836. /* turn off logs */
  37837. wolfSSL_Debugging_OFF();
  37838. /* capture log count */
  37839. i = log_cnt;
  37840. /* validate no logs are output when disabled */
  37841. WOLFSSL_MSG(msg);
  37842. WOLFSSL_BUFFER(a, sizeof(a));
  37843. WOLFSSL_BUFFER(b, sizeof(b));
  37844. WOLFSSL_BUFFER(NULL, 0);
  37845. WOLFSSL_ERROR(MEMORY_E);
  37846. WOLFSSL_ERROR_MSG(msg);
  37847. /* check the logs were disabled */
  37848. if (i != log_cnt)
  37849. return -13602;
  37850. /* restore callback and leave logging enabled */
  37851. wolfSSL_SetLoggingCb(NULL);
  37852. wolfSSL_Debugging_ON();
  37853. /* suppress unused args */
  37854. (void)a;
  37855. (void)b;
  37856. #else
  37857. if (wolfSSL_Debugging_ON() != NOT_COMPILED_IN)
  37858. return -13603;
  37859. wolfSSL_Debugging_OFF();
  37860. if (wolfSSL_SetLoggingCb(NULL) != NOT_COMPILED_IN)
  37861. return -13604;
  37862. #endif /* DEBUG_WOLFSSL */
  37863. return 0;
  37864. }
  37865. WOLFSSL_TEST_SUBROUTINE int mutex_test(void)
  37866. {
  37867. #ifdef WOLFSSL_PTHREADS
  37868. wolfSSL_Mutex m;
  37869. #endif
  37870. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_USER_MUTEX)
  37871. #ifndef WOLFSSL_STATIC_MEMORY
  37872. wolfSSL_Mutex *mm = wc_InitAndAllocMutex();
  37873. #else
  37874. wolfSSL_Mutex *mm = (wolfSSL_Mutex*) XMALLOC(sizeof(wolfSSL_Mutex),
  37875. HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  37876. if (mm != NULL) {
  37877. if (wc_InitMutex(mm) != 0) {
  37878. WOLFSSL_MSG("Init Mutex failed");
  37879. XFREE(mm, HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  37880. mm = NULL;
  37881. }
  37882. }
  37883. #endif
  37884. if (mm == NULL)
  37885. return -13700;
  37886. wc_FreeMutex(mm);
  37887. XFREE(mm, HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  37888. #endif
  37889. /* Can optionally enable advanced pthread tests using "ENABLE_PTHREAD_LOCKFREE_TESTS" */
  37890. #ifdef WOLFSSL_PTHREADS
  37891. if (wc_InitMutex(&m) != 0)
  37892. return -13701;
  37893. if (wc_LockMutex(&m) != 0)
  37894. return -13702;
  37895. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  37896. /* trying to free a locked mutex is not portable behavior with pthread */
  37897. /* Attempting to destroy a locked mutex results in undefined behavior */
  37898. if (wc_FreeMutex(&m) != BAD_MUTEX_E)
  37899. return -13703;
  37900. #endif
  37901. if (wc_UnLockMutex(&m) != 0)
  37902. return -13704;
  37903. if (wc_FreeMutex(&m) != 0)
  37904. return -13705;
  37905. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  37906. /* Trying to use a pthread after free'ing is not portable behavior */
  37907. if (wc_LockMutex(&m) != BAD_MUTEX_E)
  37908. return -13706;
  37909. if (wc_UnLockMutex(&m) != BAD_MUTEX_E)
  37910. return -13707;
  37911. #endif
  37912. #endif
  37913. return 0;
  37914. }
  37915. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  37916. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  37917. !defined(WOLFSSL_STATIC_MEMORY)
  37918. static int malloc_cnt = 0;
  37919. static int realloc_cnt = 0;
  37920. static int free_cnt = 0;
  37921. #ifdef WOLFSSL_DEBUG_MEMORY
  37922. static void *my_Malloc_cb(size_t size, const char* func, unsigned int line)
  37923. {
  37924. (void) func;
  37925. (void) line;
  37926. #else
  37927. static void *my_Malloc_cb(size_t size)
  37928. {
  37929. #endif
  37930. malloc_cnt++;
  37931. #ifndef WOLFSSL_NO_MALLOC
  37932. return malloc(size);
  37933. #else
  37934. WOLFSSL_MSG("No malloc available");
  37935. (void)size;
  37936. return NULL;
  37937. #endif
  37938. }
  37939. #ifdef WOLFSSL_DEBUG_MEMORY
  37940. static void my_Free_cb(void *ptr, const char* func, unsigned int line)
  37941. {
  37942. (void) func;
  37943. (void) line;
  37944. #else
  37945. static void my_Free_cb(void *ptr)
  37946. {
  37947. #endif
  37948. free_cnt++;
  37949. #ifndef WOLFSSL_NO_MALLOC
  37950. free(ptr);
  37951. #else
  37952. WOLFSSL_MSG("No free available");
  37953. (void)ptr;
  37954. #endif
  37955. }
  37956. #ifdef WOLFSSL_DEBUG_MEMORY
  37957. static void *my_Realloc_cb(void *ptr, size_t size, const char* func, unsigned int line)
  37958. {
  37959. (void) func;
  37960. (void) line;
  37961. #else
  37962. static void *my_Realloc_cb(void *ptr, size_t size)
  37963. {
  37964. #endif
  37965. realloc_cnt++;
  37966. #ifndef WOLFSSL_NO_MALLOC
  37967. return realloc(ptr, size);
  37968. #else
  37969. WOLFSSL_MSG("No realloc available");
  37970. (void)ptr;
  37971. (void)size;
  37972. return NULL;
  37973. #endif
  37974. }
  37975. #endif /* !WOLFSSL_NO_MALLOC */
  37976. WOLFSSL_TEST_SUBROUTINE int memcb_test(void)
  37977. {
  37978. int ret = 0;
  37979. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  37980. !defined(WOLFSSL_STATIC_MEMORY)
  37981. byte* b = NULL;
  37982. #endif
  37983. wolfSSL_Malloc_cb mc;
  37984. wolfSSL_Free_cb fc;
  37985. wolfSSL_Realloc_cb rc;
  37986. /* Save existing memory callbacks */
  37987. if (wolfSSL_GetAllocators(&mc, &fc, &rc) != 0)
  37988. return -13800;
  37989. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  37990. !defined(WOLFSSL_STATIC_MEMORY)
  37991. /* test realloc */
  37992. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37993. if (b == NULL) {
  37994. ERROR_OUT(-13801, exit_memcb);
  37995. }
  37996. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37997. b = NULL;
  37998. /* Use API. */
  37999. if (wolfSSL_SetAllocators((wolfSSL_Malloc_cb)my_Malloc_cb,
  38000. (wolfSSL_Free_cb)my_Free_cb,
  38001. (wolfSSL_Realloc_cb)my_Realloc_cb) != 0) {
  38002. ERROR_OUT(-13802, exit_memcb);
  38003. }
  38004. b = (byte*)XMALLOC(1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38005. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38006. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38007. #ifndef WOLFSSL_STATIC_MEMORY
  38008. #ifndef WOLFSSL_CHECK_MEM_ZERO
  38009. if (malloc_cnt != 1 || free_cnt != 1 || realloc_cnt != 1)
  38010. #else
  38011. /* Checking zeroized memory means realloc is a malloc and free. */
  38012. if (malloc_cnt != 2 || free_cnt != 2 || realloc_cnt != 0)
  38013. #endif
  38014. #else
  38015. if (malloc_cnt != 0 || free_cnt != 0 || realloc_cnt != 0)
  38016. #endif
  38017. ret = -13803;
  38018. #endif /* !WOLFSSL_NO_MALLOC */
  38019. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  38020. !defined(WOLFSSL_STATIC_MEMORY)
  38021. exit_memcb:
  38022. /* reset malloc/free/realloc counts */
  38023. malloc_cnt = 0;
  38024. free_cnt = 0;
  38025. realloc_cnt = 0;
  38026. #endif
  38027. /* restore memory callbacks */
  38028. wolfSSL_SetAllocators(mc, fc, rc);
  38029. return ret;
  38030. }
  38031. #endif /* USE_WOLFSSL_MEMORY && !WOLFSSL_NO_MALLOC */
  38032. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  38033. WOLFSSL_TEST_SUBROUTINE int blob_test(void)
  38034. {
  38035. int ret = 0;
  38036. byte out[112];
  38037. byte blob[112];
  38038. word32 outSz;
  38039. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  38040. {
  38041. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  38042. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  38043. };
  38044. WOLFSSL_SMALL_STACK_STATIC const byte text[] =
  38045. {
  38046. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  38047. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  38048. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  38049. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  38050. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  38051. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  38052. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  38053. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  38054. };
  38055. XMEMSET(blob, 0, sizeof(blob));
  38056. XMEMSET(out, 0, sizeof(out));
  38057. outSz = sizeof(blob);
  38058. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  38059. if (ret != 0) {
  38060. ERROR_OUT(-13900, exit_blob);
  38061. }
  38062. blob[outSz - 2] += 1;
  38063. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  38064. if (ret == 0) { /* should fail with altered blob */
  38065. ERROR_OUT(-13901, exit_blob);
  38066. }
  38067. XMEMSET(blob, 0, sizeof(blob));
  38068. outSz = sizeof(blob);
  38069. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  38070. if (ret != 0) {
  38071. ERROR_OUT(-13902, exit_blob);
  38072. }
  38073. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  38074. if (ret != 0) {
  38075. ERROR_OUT(-13903, exit_blob);
  38076. }
  38077. if (XMEMCMP(out, iv, sizeof(iv))) {
  38078. ERROR_OUT(-13904, exit_blob);
  38079. }
  38080. XMEMSET(blob, 0, sizeof(blob));
  38081. outSz = sizeof(blob);
  38082. ret = wc_caamCreateBlob((byte*)text, sizeof(text), blob, &outSz);
  38083. if (ret != 0) {
  38084. ERROR_OUT(-13905, exit_blob);
  38085. }
  38086. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  38087. if (ret != 0) {
  38088. ERROR_OUT(-13906, exit_blob);
  38089. }
  38090. if (XMEMCMP(out, text, sizeof(text))) {
  38091. ERROR_OUT(-13907, exit_blob);
  38092. }
  38093. exit_blob:
  38094. return ret;
  38095. }
  38096. #endif /* WOLFSSL_IMX6_CAAM_BLOB */
  38097. #ifdef WOLF_CRYPTO_CB
  38098. /* Example custom context for crypto callback */
  38099. typedef struct {
  38100. int exampleVar; /* flag for testing if only crypt is enabled. */
  38101. } myCryptoDevCtx;
  38102. #ifdef WOLF_CRYPTO_CB_ONLY_RSA
  38103. /* Testing rsa cb when CB_ONLY_RSA is enabled
  38104. * When CB_ONLY_RSA is enabled, software imple. is not available.
  38105. *
  38106. * ctx callback ctx
  38107. * returen 0 on success, otherwise return -8000 - -8007
  38108. */
  38109. static int rsa_onlycb_test(myCryptoDevCtx *ctx)
  38110. {
  38111. int ret = 0;
  38112. #if !defined(NO_RSA)
  38113. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  38114. RsaKey *key = (RsaKey *)XMALLOC(sizeof *key,
  38115. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38116. byte* tmp = NULL;
  38117. #else
  38118. RsaKey key[1];
  38119. byte tmp[FOURK_BUF];
  38120. #endif
  38121. size_t bytes;
  38122. const word32 inLen = (word32)TEST_STRING_SZ;
  38123. word32 idx = 0;
  38124. word32 sigSz;
  38125. WOLFSSL_SMALL_STACK_STATIC const byte in[] = TEST_STRING;
  38126. byte out[RSA_TEST_BYTES];
  38127. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  38128. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  38129. !defined(NO_FILESYSTEM)
  38130. XFILE file;
  38131. #endif
  38132. #ifdef WOLFSSL_KEY_GEN
  38133. WC_RNG rng;
  38134. #endif
  38135. #ifdef USE_CERT_BUFFERS_1024
  38136. bytes = (size_t)sizeof_client_key_der_1024;
  38137. if (bytes < (size_t)sizeof_client_cert_der_1024)
  38138. bytes = (size_t)sizeof_client_cert_der_1024;
  38139. #elif defined(USE_CERT_BUFFERS_2048)
  38140. bytes = (size_t)sizeof_client_key_der_2048;
  38141. if (bytes < (size_t)sizeof_client_cert_der_2048)
  38142. bytes = (size_t)sizeof_client_cert_der_2048;
  38143. #elif defined(USE_CERT_BUFFERS_3072)
  38144. bytes = (size_t)sizeof_client_key_der_3072;
  38145. if (bytes < (size_t)sizeof_client_cert_der_3072)
  38146. bytes = (size_t)sizeof_client_cert_der_3072;
  38147. #elif defined(USE_CERT_BUFFERS_4096)
  38148. bytes = (size_t)sizeof_client_key_der_4096;
  38149. if (bytes < (size_t)sizeof_client_cert_der_4096)
  38150. bytes = (size_t)sizeof_client_cert_der_4096;
  38151. #else
  38152. bytes = FOURK_BUF;
  38153. #endif
  38154. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  38155. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38156. if (tmp == NULL)
  38157. ERROR_OUT(-8000, exit_onlycb);
  38158. #endif
  38159. #ifdef USE_CERT_BUFFERS_1024
  38160. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  38161. #elif defined(USE_CERT_BUFFERS_2048)
  38162. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  38163. #elif defined(USE_CERT_BUFFERS_3072)
  38164. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  38165. #elif defined(USE_CERT_BUFFERS_4096)
  38166. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  38167. #elif !defined(NO_FILESYSTEM)
  38168. file = XFOPEN(clientKey, "rb");
  38169. if (!file) {
  38170. err_sys("can't open ./certs/client-key.der, "
  38171. "Please run from wolfSSL home dir", -40);
  38172. ERROR_OUT(-8001, exit_onlycb);
  38173. }
  38174. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  38175. XFCLOSE(file);
  38176. #endif
  38177. #ifdef WOLFSSL_KEY_GEN
  38178. /* wc_CryptoCb_MakeRsaKey cb test, no actual making key
  38179. * wc_MakeRsaKey() -> rsa cb ->
  38180. * myCryptoDevCb -> wc_MakeRsaKey(CBONLY_TEST_DEVID)
  38181. * wc_MakeRsaKey(CBONLY_TEST_DEVID) expects to return 0(success)
  38182. */
  38183. ctx->exampleVar = 99;
  38184. ret = wc_MakeRsaKey(key, keySz, WC_RSA_EXPONENT, rng);
  38185. if (ret != 0) {
  38186. ERROR_OUT(-8002, exit_onlycb);
  38187. }
  38188. /* wc_MakeRsaKey() -> rsa cb ->
  38189. * myCryptoDevCb -> wc_MakeRsaKey(INVALID_DEVID)
  38190. * wc_MakeRsaKey(CBONLY_TEST_DEVID) expects to return NO_VALID_DEVID(failure)
  38191. */
  38192. ctx->exampleVar = 1;
  38193. ret = wc_MakeRsaKey(key, keySz, WC_RSA_EXPONENT, rng);
  38194. if (ret != NO_VALID_DEVID) {
  38195. ERROR_OUT(-8003, exit_onlycb);
  38196. } else
  38197. /* reset return code */
  38198. ret = 0;
  38199. #endif
  38200. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  38201. if (ret != 0) {
  38202. ERROR_OUT(-8004, exit_onlycb);
  38203. }
  38204. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  38205. if (ret != 0) {
  38206. ERROR_OUT(-8005, exit_onlycb);
  38207. }
  38208. sigSz = (word32)wc_RsaEncryptSize(key);
  38209. /* wc_CryptoCb_Rsa cb test, no actual rsa operation */
  38210. if (ret == 0) {
  38211. /* wc_SignatureGenerate() -> rsa cb ->
  38212. * myCryptoDevCb -> wc_RsaFunction(CBONLY_TEST_DEVID)
  38213. * wc_RsaFunction(CBONLY_TEST_DEVID) expects to return 0(success)
  38214. */
  38215. ctx->exampleVar = 99;
  38216. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  38217. in, inLen, out, &sigSz, key, sizeof(*key), NULL);
  38218. if (ret != 0) {
  38219. ERROR_OUT(-8006, exit_onlycb);
  38220. }
  38221. }
  38222. if (ret == 0) {
  38223. /* wc_SignatureGenerate() -> rsa cb ->
  38224. * myCryptoDevCb -> wc_RsaFunction(INVALID_DEVID)
  38225. * wc_SignatureGenerate(INVALID_DEVID) expects to
  38226. * return NO_VALID_DEVID(failure)
  38227. */
  38228. ctx->exampleVar = 1;
  38229. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  38230. in, inLen, out, &sigSz, key, sizeof(*key), NULL);
  38231. if (ret != NO_VALID_DEVID) {
  38232. ERROR_OUT(-8007, exit_onlycb);
  38233. } else
  38234. /* reset return code */
  38235. ret = 0;
  38236. }
  38237. exit_onlycb:
  38238. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  38239. if (key != NULL) {
  38240. wc_FreeRsaKey(key);
  38241. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38242. }
  38243. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38244. #else
  38245. wc_FreeRsaKey(key);
  38246. #endif
  38247. #endif
  38248. return ret;
  38249. }
  38250. #endif
  38251. #ifdef WOLF_CRYPTO_CB_ONLY_ECC
  38252. /* Testing rsa cb when CB_ONLY_ECC is enabled
  38253. * When CB_ONLY_ECC is enabled, software imple. is not available.
  38254. *
  38255. * ctx callback ctx
  38256. * returen 0 on success, otherwise return -8008 - -8018
  38257. */
  38258. static int ecc_onlycb_test(myCryptoDevCtx *ctx)
  38259. {
  38260. int ret = 0;
  38261. #if defined(HAVE_ECC)
  38262. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  38263. ecc_key* key = (ecc_key *)XMALLOC(sizeof *key,
  38264. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38265. ecc_key* pub = (ecc_key *)XMALLOC(sizeof *pub,
  38266. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38267. byte* out = (byte*)XMALLOC(sizeof(byte),
  38268. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38269. #ifdef OPENSSL_EXTRA
  38270. byte* check = (byte*)XMALLOC(sizeof(byte)*(256), HEAP_HINT,
  38271. DYNAMIC_TYPE_TMP_BUFFER);
  38272. #endif
  38273. #else
  38274. ecc_key key[1];
  38275. ecc_key pub[1];
  38276. byte out[256];
  38277. #ifdef OPENSSL_EXTRA
  38278. unsigned char check[256];
  38279. #endif
  38280. #endif
  38281. #ifdef OPENSSL_EXTRA
  38282. EVP_PKEY* privKey = NULL;
  38283. EVP_PKEY* pubKey = NULL;
  38284. ecc_key* pkey;
  38285. EVP_MD_CTX mdCtx;
  38286. const char testData[] = "Hi There";
  38287. size_t checkSz = -1;
  38288. const unsigned char* cp;
  38289. const unsigned char* p;
  38290. const unsigned char check_v[256] = {
  38291. 0x30,0x45,0x02,0x20,0x1b,0x5c,0x2a,0xf0,0x18,0x09,
  38292. 0x74,0x65,0xa1,0x04,0x76,0x3a,0xce,0xcc,0xe5,0x34,
  38293. 0x5e,0x89,0xed,0x40,0x1e,0x5a,0xb1,0x53,0xb4,0xff,
  38294. 0xc7,0x18,0xfe,0x0f,0xc7,0xa6,0x02,0x21,0x00,0xe5,
  38295. 0x70,0x21,0xfc,0xf9,0x63,0x36,0xfd,0x16,0x18,0x08,
  38296. 0x9a,0x63,0x61,0x0f,0xe7,0x7c,0xa3,0xc9,0x14,0xa3,
  38297. 0x30,0x87,0xf7,0xf5,0x70,0x19,0xaf,0x56,0x96,0x9b,
  38298. 0xd8,0x64,0xcd,0xd9,0xff,0x7b,0x2a,0x55,0x52,0xca,
  38299. 0x41,0xb2,0xa6,0xa4,0x8a,0x3b,0x02,0x20,0x8c,0xc5,
  38300. 0xf9,0xc1,0x7d,0x2a,0x65,0x6c,0xe6,0x5a,0xe3,0x76,
  38301. 0x9b,0xab,0x0b,0x9f,0xaf,0x62,0x5d,0xb2,0x60,0xd7,
  38302. 0xeb,0xb4,0x1b,0x73,0xdc,0x01,0x7d,0x7b,0xab,0xc1,
  38303. 0x0c,0x74,0x96,0x41,0xe6,0x3f,0xc5,0x86,0xe6,0x7d,
  38304. 0x2b,0x9d,0x54,0x6b,0xcd,0x31,0x35,0x1f,0xdb,0x49,
  38305. 0x1f,0x32,0x34,0xf8,0x57,0x12,0x86,0x5c,0x0e,0x80,
  38306. 0x55,0x8d,0xff,0xd8,0xbd,0xdf,0x32,0x26,0x62,0x42,
  38307. 0x09,0xda,0xf7,0x74,0xf2,0x3f,0xe6,0xf1,0x77,0x82,
  38308. 0xce,0xe4,0xbb,0x61,0xa6,0xc0,0x17,0x0c,0x6c,0x47,
  38309. 0x2a,0x40,0x1c,0x2b,0xe0,0x98,0x3b,0xbf,0xc6,0xf8,
  38310. 0x6d,0xfd,0xd0,0xfa,0xc1,0x02,0xfb,0x5f,0xfb,0xb0,
  38311. 0xcb,0xd9,0xa3,0x59,0x94,0xe9,0x0f,0x74,0xbb,0x3f,
  38312. 0x64,0xa3,0x83,0xc4,0x2b,0xf7,0xd2,0x97,0xbf,0x3b,
  38313. 0xcf,0xbb,0x60,0x81,0x33,0x94,0xfa,0x0d,0x35,0xd2,
  38314. 0x3d,0xb9,0x99,0xe3,0x12,0xf8,0xf4,0xa3,0x74,0xf4,
  38315. 0x94,0x1d,0x7a,0x66,0xf8,0xd1,0x1d,0xcf,0xb0,0x48,
  38316. 0xef,0x8c,0x94,0x6f,0xdd,0x62,
  38317. };
  38318. #endif
  38319. WC_RNG rng;
  38320. EncryptedInfo encInfo;
  38321. int keyFormat = 0;
  38322. word32 keyIdx = 0;
  38323. byte in[] = "Everyone gets Friday off. ecc p";
  38324. word32 inLen = (word32)XSTRLEN((char*)in);
  38325. word32 outLen;
  38326. int verify;
  38327. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  38328. if (key == NULL || pub == NULL) {
  38329. ERROR_OUT(-8008, exit_onlycb);
  38330. }
  38331. #endif
  38332. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  38333. if (ret != 0) {
  38334. ERROR_OUT(-8009, exit_onlycb);
  38335. }
  38336. /* wc_CryptoCb_MakeEccKey cb test, , no actual testing */
  38337. ctx->exampleVar = 99;
  38338. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, key);
  38339. if (ret != 0) {
  38340. ERROR_OUT(-8010, exit_onlycb);
  38341. }
  38342. ctx->exampleVar = 1;
  38343. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, key);
  38344. if (ret != NO_VALID_DEVID) {
  38345. ERROR_OUT(-8011, exit_onlycb);
  38346. } else
  38347. /* reset return code */
  38348. ret = 0;
  38349. #ifdef USE_CERT_BUFFERS_256
  38350. if (ret == 0) {
  38351. /* load ECC private key and perform private transform */
  38352. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &keyIdx,
  38353. key, sizeof_ecc_key_der_256);
  38354. }
  38355. if (ret != 0) {
  38356. ERROR_OUT(-8012, exit_onlycb);
  38357. }
  38358. /* wc_CryptoCb_EccSign cb test, no actual testing */
  38359. ctx->exampleVar = 99;
  38360. if (ret == 0) {
  38361. ret = wc_ecc_sign_hash(in, inLen, out, &outLen, &rng, key);
  38362. }
  38363. if (ret != 0) {
  38364. ERROR_OUT(-8013, exit_onlycb);
  38365. }
  38366. ctx->exampleVar = 1;
  38367. if (ret == 0) {
  38368. ret = wc_ecc_sign_hash(in, inLen, out, &outLen, &rng, key);
  38369. }
  38370. if (ret != NO_VALID_DEVID) {
  38371. ERROR_OUT(-8014, exit_onlycb);
  38372. }
  38373. else
  38374. ret = 0;
  38375. /* wc_CryptoCb_EccVerify cb test, no actual testing */
  38376. ctx->exampleVar = 99;
  38377. if (ret == 0) {
  38378. ret = wc_ecc_verify_hash(in, inLen, out, outLen, &verify, key);
  38379. }
  38380. if (ret != 0) {
  38381. ERROR_OUT(-8015, exit_onlycb);
  38382. }
  38383. ctx->exampleVar = 1;
  38384. if (ret == 0) {
  38385. ret = wc_ecc_verify_hash(in, inLen, out, outLen, &verify, key);
  38386. }
  38387. if (ret != NO_VALID_DEVID) {
  38388. ERROR_OUT(-8016, exit_onlycb);
  38389. }
  38390. else
  38391. ret = 0;
  38392. /* wc_CryptoCb_Ecdh cb test, no actual testing */
  38393. /* make public key for shared secret */
  38394. wc_ecc_init_ex(pub, HEAP_HINT, devId);
  38395. ctx->exampleVar = 99;
  38396. if (ret == 0) {
  38397. ret = wc_ecc_shared_secret(key, pub, out, &outLen);
  38398. }
  38399. if (ret != 0) {
  38400. ERROR_OUT(-8017, exit_onlycb);
  38401. }
  38402. ctx->exampleVar = 1;
  38403. if (ret == 0) {
  38404. ret = wc_ecc_shared_secret(key, pub, out, &outLen);
  38405. }
  38406. if (ret != NO_VALID_DEVID) {
  38407. ERROR_OUT(-8018, exit_onlycb);
  38408. }
  38409. else
  38410. ret = 0;
  38411. #ifdef OPENSSL_EXTRA
  38412. (void)pkey;
  38413. cp = ecc_clikey_der_256;
  38414. privKey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  38415. sizeof_ecc_clikey_der_256);
  38416. if (privKey == NULL) {
  38417. ERROR_OUT(-8019, exit_onlycb);
  38418. }
  38419. pkey = (ecc_key*)privKey->ecc->internal;
  38420. pkey->devId = devId;
  38421. p = ecc_clikeypub_der_256;
  38422. pubKey = d2i_PUBKEY(NULL, &p, sizeof_ecc_clikeypub_der_256);
  38423. if (pubKey == NULL) {
  38424. ERROR_OUT(-8020, exit_onlycb);
  38425. }
  38426. pkey = (ecc_key*)pubKey->ecc->internal;
  38427. pkey->devId = devId;
  38428. /* sign */
  38429. EVP_MD_CTX_init(&mdCtx);
  38430. ret = EVP_DigestSignInit(&mdCtx, NULL, EVP_sha256(), NULL, privKey);
  38431. if (ret != WOLFSSL_SUCCESS) {
  38432. ERROR_OUT(-8021, exit_onlycb);
  38433. }
  38434. ret = EVP_DigestSignUpdate(&mdCtx, testData,
  38435. (unsigned int)XSTRLEN(testData));
  38436. if (ret != WOLFSSL_SUCCESS) {
  38437. ERROR_OUT(-8022, exit_onlycb);
  38438. }
  38439. ret = EVP_DigestSignFinal(&mdCtx, NULL, &checkSz);
  38440. if (ret != WOLFSSL_SUCCESS) {
  38441. ERROR_OUT(-8023, exit_onlycb);
  38442. }
  38443. ctx->exampleVar = 99;
  38444. ret = EVP_DigestSignFinal(&mdCtx, check, &checkSz);
  38445. /* just called crypt callback as dummy
  38446. * EVP_DigestSignFinal returns 0 internally.
  38447. */
  38448. if (ret != 0) {
  38449. ERROR_OUT(-8024, exit_onlycb);
  38450. }
  38451. ctx->exampleVar = 1;
  38452. ret = EVP_DigestSignFinal(&mdCtx, check, &checkSz);
  38453. /* just called crypt callback as dummy
  38454. * EVP_DigestSignFinal returns 0 internally.
  38455. */
  38456. if (ret != 0) {
  38457. ERROR_OUT(-8025, exit_onlycb);
  38458. }
  38459. /* restore checkSz for verify */
  38460. checkSz = 71;
  38461. ret = EVP_MD_CTX_cleanup(&mdCtx);
  38462. if (ret != SSL_SUCCESS) {
  38463. ERROR_OUT(-8026, exit_onlycb);
  38464. }
  38465. /* verify */
  38466. EVP_MD_CTX_init(&mdCtx);
  38467. if (ret == SSL_SUCCESS) {
  38468. ret = EVP_DigestVerifyInit(&mdCtx, NULL, EVP_sha256(), NULL, pubKey);
  38469. }
  38470. if (ret != WOLFSSL_SUCCESS) {
  38471. ERROR_OUT(-8027, exit_onlycb);
  38472. }
  38473. if (ret == WOLFSSL_SUCCESS) {
  38474. ret = EVP_DigestVerifyUpdate(&mdCtx, testData,
  38475. (unsigned int)XSTRLEN(testData));
  38476. }
  38477. if (ret != WOLFSSL_SUCCESS) {
  38478. ERROR_OUT(-8028, exit_onlycb);
  38479. }
  38480. ctx->exampleVar = 99;
  38481. ret = EVP_DigestVerifyFinal(&mdCtx, check_v, checkSz);
  38482. /* just called crypt callback as dummy
  38483. * EVP_DigestSignFinal returns 0 internally.
  38484. */
  38485. if (ret != 0) {
  38486. ERROR_OUT(-8029, exit_onlycb);
  38487. }
  38488. ctx->exampleVar = 1;
  38489. ret = EVP_DigestVerifyFinal(&mdCtx, check_v, checkSz);
  38490. /* just called crypt callback as dummy
  38491. * EVP_DigestVerifyFinal returns -1 internally rather than NO_VALID_DEVID.
  38492. */
  38493. if (ret != -1) {
  38494. ERROR_OUT(-8030, exit_onlycb);
  38495. }
  38496. ret = EVP_MD_CTX_cleanup(&mdCtx);
  38497. if (ret != SSL_SUCCESS) {
  38498. ERROR_OUT(-8031, exit_onlycb);
  38499. } else
  38500. ret = 0;
  38501. #endif
  38502. #else
  38503. (void)verify;
  38504. (void)outLen;
  38505. (void)inLen;
  38506. (void)out;
  38507. (void)pub;
  38508. #ifdef OPENSSL_EXTRA
  38509. (void)privKey;
  38510. (void)pubKey;
  38511. (void)mdCtx;
  38512. (void)check;
  38513. (void)checkSz;
  38514. (void)p;
  38515. #endif
  38516. #endif
  38517. (void)keyFormat;
  38518. (void)encInfo;
  38519. exit_onlycb:
  38520. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  38521. if (key != NULL) {
  38522. wc_ecc_free(key);
  38523. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38524. }
  38525. if (pub != NULL) {
  38526. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38527. }
  38528. if (out != NULL) {
  38529. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38530. }
  38531. #ifdef OPENSSL_EXTRA
  38532. if (check) {
  38533. FREE(check, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38534. }
  38535. #endif
  38536. #else
  38537. wc_ecc_free(key);
  38538. #ifdef OPENSSL_EXTRA
  38539. if (privKey)
  38540. EVP_PKEY_free(privKey);
  38541. if (pubKey)
  38542. EVP_PKEY_free(pubKey);
  38543. #endif
  38544. #endif
  38545. #endif /* HAVE_ECC */
  38546. return ret;
  38547. }
  38548. #endif
  38549. /* Example crypto dev callback function that calls software version */
  38550. static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
  38551. {
  38552. int ret = NOT_COMPILED_IN; /* return this to bypass HW and use SW */
  38553. myCryptoDevCtx* myCtx = (myCryptoDevCtx*)ctx;
  38554. if (info == NULL)
  38555. return BAD_FUNC_ARG;
  38556. #ifdef DEBUG_WOLFSSL
  38557. printf("CryptoDevCb: Algo Type %d\n", info->algo_type);
  38558. #endif
  38559. if (info->algo_type == WC_ALGO_TYPE_RNG) {
  38560. #ifndef WC_NO_RNG
  38561. /* set devId to invalid, so software is used */
  38562. info->rng.rng->devId = INVALID_DEVID;
  38563. ret = wc_RNG_GenerateBlock(info->rng.rng,
  38564. info->rng.out, info->rng.sz);
  38565. /* reset devId */
  38566. info->rng.rng->devId = devIdArg;
  38567. #endif
  38568. }
  38569. else if (info->algo_type == WC_ALGO_TYPE_SEED) {
  38570. #ifndef WC_NO_RNG
  38571. ALIGN32 static byte seed[sizeof(word32)] = { 0x00, 0x00, 0x00, 0x01 };
  38572. word32* seedWord32 = (word32*)seed;
  38573. word32 len;
  38574. /* wc_GenerateSeed is a local symbol so we need to fake the entropy. */
  38575. while (info->seed.sz > 0) {
  38576. len = (word32)sizeof(seed);
  38577. if (info->seed.sz < len)
  38578. len = info->seed.sz;
  38579. XMEMCPY(info->seed.seed, seed, sizeof(seed));
  38580. info->seed.seed += len;
  38581. info->seed.sz -= len;
  38582. (*seedWord32)++;
  38583. }
  38584. ret = 0;
  38585. #endif
  38586. }
  38587. else if (info->algo_type == WC_ALGO_TYPE_PK) {
  38588. #ifdef DEBUG_WOLFSSL
  38589. printf("CryptoDevCb: Pk Type %d\n", info->pk.type);
  38590. #endif
  38591. #ifndef NO_RSA
  38592. if (info->pk.type == WC_PK_TYPE_RSA) {
  38593. /* set devId to invalid, so software is used */
  38594. info->pk.rsa.key->devId = INVALID_DEVID;
  38595. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  38596. #ifdef DEBUG_WOLFSSL
  38597. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  38598. #endif
  38599. if (myCtx->exampleVar == 99) {
  38600. info->pk.rsa.key->devId = devIdArg;
  38601. return 0;
  38602. }
  38603. #endif
  38604. switch (info->pk.rsa.type) {
  38605. case RSA_PUBLIC_ENCRYPT:
  38606. case RSA_PUBLIC_DECRYPT:
  38607. /* perform software based RSA public op */
  38608. ret = wc_RsaFunction(
  38609. info->pk.rsa.in, info->pk.rsa.inLen,
  38610. info->pk.rsa.out, info->pk.rsa.outLen,
  38611. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  38612. break;
  38613. case RSA_PRIVATE_ENCRYPT:
  38614. case RSA_PRIVATE_DECRYPT:
  38615. /* perform software based RSA private op */
  38616. ret = wc_RsaFunction(
  38617. info->pk.rsa.in, info->pk.rsa.inLen,
  38618. info->pk.rsa.out, info->pk.rsa.outLen,
  38619. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  38620. break;
  38621. }
  38622. /* reset devId */
  38623. info->pk.rsa.key->devId = devIdArg;
  38624. }
  38625. #ifdef WOLFSSL_KEY_GEN
  38626. else if (info->pk.type == WC_PK_TYPE_RSA_KEYGEN) {
  38627. info->pk.rsakg.key->devId = INVALID_DEVID;
  38628. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  38629. #ifdef DEBUG_WOLFSSL
  38630. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  38631. #endif
  38632. if (myCtx->exampleVar == 99) {
  38633. info->pk.rsakg.key->devId = devIdArg;
  38634. return 0;
  38635. }
  38636. #endif
  38637. #ifdef HAVE_FIPS
  38638. for (;;) {
  38639. #endif
  38640. ret = wc_MakeRsaKey(info->pk.rsakg.key, info->pk.rsakg.size,
  38641. info->pk.rsakg.e, info->pk.rsakg.rng);
  38642. #ifdef HAVE_FIPS
  38643. if (ret == PRIME_GEN_E)
  38644. continue;
  38645. break;
  38646. }
  38647. #endif
  38648. /* reset devId */
  38649. info->pk.rsakg.key->devId = devIdArg;
  38650. }
  38651. #endif
  38652. #endif /* !NO_RSA */
  38653. #ifdef HAVE_ECC
  38654. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  38655. /* set devId to invalid, so software is used */
  38656. info->pk.eckg.key->devId = INVALID_DEVID;
  38657. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  38658. #ifdef DEBUG_WOLFSSL
  38659. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  38660. #endif
  38661. if (myCtx->exampleVar == 99) {
  38662. info->pk.eckg.key->devId = devIdArg;
  38663. return 0;
  38664. }
  38665. #endif
  38666. ret = wc_ecc_make_key_ex(info->pk.eckg.rng, info->pk.eckg.size,
  38667. info->pk.eckg.key, info->pk.eckg.curveId);
  38668. /* reset devId */
  38669. info->pk.eckg.key->devId = devIdArg;
  38670. }
  38671. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  38672. /* set devId to invalid, so software is used */
  38673. info->pk.eccsign.key->devId = INVALID_DEVID;
  38674. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  38675. #ifdef DEBUG_WOLFSSL
  38676. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  38677. #endif
  38678. if (myCtx->exampleVar == 99) {
  38679. info->pk.eccsign.key->devId = devIdArg;
  38680. return 0;
  38681. }
  38682. #endif
  38683. ret = wc_ecc_sign_hash(
  38684. info->pk.eccsign.in, info->pk.eccsign.inlen,
  38685. info->pk.eccsign.out, info->pk.eccsign.outlen,
  38686. info->pk.eccsign.rng, info->pk.eccsign.key);
  38687. /* reset devId */
  38688. info->pk.eccsign.key->devId = devIdArg;
  38689. }
  38690. else if (info->pk.type == WC_PK_TYPE_ECDSA_VERIFY) {
  38691. /* set devId to invalid, so software is used */
  38692. info->pk.eccverify.key->devId = INVALID_DEVID;
  38693. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  38694. #ifdef DEBUG_WOLFSSL
  38695. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  38696. #endif
  38697. if (myCtx->exampleVar == 99) {
  38698. info->pk.eccverify.key->devId = devIdArg;
  38699. return 0;
  38700. }
  38701. #endif
  38702. ret = wc_ecc_verify_hash(
  38703. info->pk.eccverify.sig, info->pk.eccverify.siglen,
  38704. info->pk.eccverify.hash, info->pk.eccverify.hashlen,
  38705. info->pk.eccverify.res, info->pk.eccverify.key);
  38706. /* reset devId */
  38707. info->pk.eccverify.key->devId = devIdArg;
  38708. }
  38709. else if (info->pk.type == WC_PK_TYPE_ECDH) {
  38710. /* set devId to invalid, so software is used */
  38711. info->pk.ecdh.private_key->devId = INVALID_DEVID;
  38712. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  38713. #ifdef DEBUG_WOLFSSL
  38714. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  38715. #endif
  38716. if (myCtx->exampleVar == 99) {
  38717. info->pk.ecdh.private_key->devId = devIdArg;
  38718. return 0;
  38719. }
  38720. #endif
  38721. ret = wc_ecc_shared_secret(
  38722. info->pk.ecdh.private_key, info->pk.ecdh.public_key,
  38723. info->pk.ecdh.out, info->pk.ecdh.outlen);
  38724. /* reset devId */
  38725. info->pk.ecdh.private_key->devId = devIdArg;
  38726. }
  38727. #endif /* HAVE_ECC */
  38728. #ifdef HAVE_CURVE25519
  38729. if (info->pk.type == WC_PK_TYPE_CURVE25519_KEYGEN) {
  38730. /* set devId to invalid, so software is used */
  38731. info->pk.curve25519kg.key->devId = INVALID_DEVID;
  38732. ret = wc_curve25519_make_key(info->pk.curve25519kg.rng,
  38733. info->pk.curve25519kg.size, info->pk.curve25519kg.key);
  38734. /* reset devId */
  38735. info->pk.curve25519kg.key->devId = devIdArg;
  38736. }
  38737. else if (info->pk.type == WC_PK_TYPE_CURVE25519) {
  38738. /* set devId to invalid, so software is used */
  38739. info->pk.curve25519.private_key->devId = INVALID_DEVID;
  38740. ret = wc_curve25519_shared_secret_ex(
  38741. info->pk.curve25519.private_key, info->pk.curve25519.public_key,
  38742. info->pk.curve25519.out, info->pk.curve25519.outlen,
  38743. info->pk.curve25519.endian);
  38744. /* reset devId */
  38745. info->pk.curve25519.private_key->devId = devIdArg;
  38746. }
  38747. #endif /* HAVE_CURVE25519 */
  38748. #ifdef HAVE_ED25519
  38749. if (info->pk.type == WC_PK_TYPE_ED25519_KEYGEN) {
  38750. /* set devId to invalid, so software is used */
  38751. info->pk.ed25519kg.key->devId = INVALID_DEVID;
  38752. ret = wc_ed25519_make_key(info->pk.ed25519kg.rng,
  38753. info->pk.ed25519kg.size, info->pk.ed25519kg.key);
  38754. /* reset devId */
  38755. info->pk.ed25519kg.key->devId = devIdArg;
  38756. }
  38757. #ifdef HAVE_ED25519_SIGN
  38758. else if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  38759. /* set devId to invalid, so software is used */
  38760. info->pk.ed25519sign.key->devId = INVALID_DEVID;
  38761. ret = wc_ed25519_sign_msg_ex(
  38762. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  38763. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  38764. info->pk.ed25519sign.key, info->pk.ed25519sign.type,
  38765. info->pk.ed25519sign.context, info->pk.ed25519sign.contextLen);
  38766. /* reset devId */
  38767. info->pk.ed25519sign.key->devId = devIdArg;
  38768. }
  38769. #endif
  38770. #ifdef HAVE_ED25519_VERIFY
  38771. else if (info->pk.type == WC_PK_TYPE_ED25519_VERIFY) {
  38772. /* set devId to invalid, so software is used */
  38773. info->pk.ed25519verify.key->devId = INVALID_DEVID;
  38774. ret = wc_ed25519_verify_msg_ex(
  38775. info->pk.ed25519verify.sig, info->pk.ed25519verify.sigLen,
  38776. info->pk.ed25519verify.msg, info->pk.ed25519verify.msgLen,
  38777. info->pk.ed25519verify.res, info->pk.ed25519verify.key,
  38778. info->pk.ed25519verify.type, info->pk.ed25519verify.context,
  38779. info->pk.ed25519verify.contextLen);
  38780. /* reset devId */
  38781. info->pk.ed25519verify.key->devId = devIdArg;
  38782. }
  38783. #endif
  38784. #endif /* HAVE_ED25519 */
  38785. }
  38786. else if (info->algo_type == WC_ALGO_TYPE_CIPHER) {
  38787. #if !defined(NO_AES) || !defined(NO_DES3)
  38788. #ifdef HAVE_AESGCM
  38789. if (info->cipher.type == WC_CIPHER_AES_GCM) {
  38790. if (info->cipher.enc) {
  38791. /* set devId to invalid, so software is used */
  38792. info->cipher.aesgcm_enc.aes->devId = INVALID_DEVID;
  38793. ret = wc_AesGcmEncrypt(
  38794. info->cipher.aesgcm_enc.aes,
  38795. info->cipher.aesgcm_enc.out,
  38796. info->cipher.aesgcm_enc.in,
  38797. info->cipher.aesgcm_enc.sz,
  38798. info->cipher.aesgcm_enc.iv,
  38799. info->cipher.aesgcm_enc.ivSz,
  38800. info->cipher.aesgcm_enc.authTag,
  38801. info->cipher.aesgcm_enc.authTagSz,
  38802. info->cipher.aesgcm_enc.authIn,
  38803. info->cipher.aesgcm_enc.authInSz);
  38804. /* reset devId */
  38805. info->cipher.aesgcm_enc.aes->devId = devIdArg;
  38806. }
  38807. else {
  38808. /* set devId to invalid, so software is used */
  38809. info->cipher.aesgcm_dec.aes->devId = INVALID_DEVID;
  38810. ret = wc_AesGcmDecrypt(
  38811. info->cipher.aesgcm_dec.aes,
  38812. info->cipher.aesgcm_dec.out,
  38813. info->cipher.aesgcm_dec.in,
  38814. info->cipher.aesgcm_dec.sz,
  38815. info->cipher.aesgcm_dec.iv,
  38816. info->cipher.aesgcm_dec.ivSz,
  38817. info->cipher.aesgcm_dec.authTag,
  38818. info->cipher.aesgcm_dec.authTagSz,
  38819. info->cipher.aesgcm_dec.authIn,
  38820. info->cipher.aesgcm_dec.authInSz);
  38821. /* reset devId */
  38822. info->cipher.aesgcm_dec.aes->devId = devIdArg;
  38823. }
  38824. }
  38825. #endif /* HAVE_AESGCM */
  38826. #ifdef HAVE_AES_CBC
  38827. if (info->cipher.type == WC_CIPHER_AES_CBC) {
  38828. if (info->cipher.enc) {
  38829. /* set devId to invalid, so software is used */
  38830. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  38831. ret = wc_AesCbcEncrypt(
  38832. info->cipher.aescbc.aes,
  38833. info->cipher.aescbc.out,
  38834. info->cipher.aescbc.in,
  38835. info->cipher.aescbc.sz);
  38836. /* reset devId */
  38837. info->cipher.aescbc.aes->devId = devIdArg;
  38838. }
  38839. else {
  38840. /* set devId to invalid, so software is used */
  38841. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  38842. ret = wc_AesCbcDecrypt(
  38843. info->cipher.aescbc.aes,
  38844. info->cipher.aescbc.out,
  38845. info->cipher.aescbc.in,
  38846. info->cipher.aescbc.sz);
  38847. /* reset devId */
  38848. info->cipher.aescbc.aes->devId = devIdArg;
  38849. }
  38850. }
  38851. #endif /* HAVE_AES_CBC */
  38852. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  38853. if (info->cipher.type == WC_CIPHER_AES_ECB) {
  38854. if (info->cipher.enc) {
  38855. /* set devId to invalid, so software is used */
  38856. info->cipher.aesecb.aes->devId = INVALID_DEVID;
  38857. ret = wc_AesEcbEncrypt(
  38858. info->cipher.aesecb.aes,
  38859. info->cipher.aesecb.out,
  38860. info->cipher.aesecb.in,
  38861. info->cipher.aesecb.sz);
  38862. /* reset devId */
  38863. info->cipher.aesecb.aes->devId = devIdArg;
  38864. }
  38865. else {
  38866. /* set devId to invalid, so software is used */
  38867. info->cipher.aesecb.aes->devId = INVALID_DEVID;
  38868. ret = wc_AesEcbDecrypt(
  38869. info->cipher.aesecb.aes,
  38870. info->cipher.aesecb.out,
  38871. info->cipher.aesecb.in,
  38872. info->cipher.aesecb.sz);
  38873. /* reset devId */
  38874. info->cipher.aesecb.aes->devId = devIdArg;
  38875. }
  38876. }
  38877. #endif /* HAVE_AES_ECB */
  38878. #if defined(WOLFSSL_AES_COUNTER) && !defined(HAVE_FIPS) && \
  38879. !defined(HAVE_SELFTEST)
  38880. if (info->cipher.type == WC_CIPHER_AES_CTR) {
  38881. /* set devId to invalid, so software is used */
  38882. info->cipher.aesctr.aes->devId = INVALID_DEVID;
  38883. ret = wc_AesCtrEncrypt(
  38884. info->cipher.aesctr.aes,
  38885. info->cipher.aesctr.out,
  38886. info->cipher.aesctr.in,
  38887. info->cipher.aesctr.sz);
  38888. /* reset devId */
  38889. info->cipher.aesctr.aes->devId = devIdArg;
  38890. }
  38891. #endif /* WOLFSSL_AES_COUNTER */
  38892. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  38893. if (info->cipher.type == WC_CIPHER_AES_CCM) {
  38894. if (info->cipher.enc) {
  38895. /* set devId to invalid, so software is used */
  38896. info->cipher.aesccm_enc.aes->devId = INVALID_DEVID;
  38897. ret = wc_AesCcmEncrypt(
  38898. info->cipher.aesccm_enc.aes,
  38899. info->cipher.aesccm_enc.out,
  38900. info->cipher.aesccm_enc.in,
  38901. info->cipher.aesccm_enc.sz,
  38902. info->cipher.aesccm_enc.nonce,
  38903. info->cipher.aesccm_enc.nonceSz,
  38904. info->cipher.aesccm_enc.authTag,
  38905. info->cipher.aesccm_enc.authTagSz,
  38906. info->cipher.aesccm_enc.authIn,
  38907. info->cipher.aesccm_enc.authInSz);
  38908. /* reset devId */
  38909. info->cipher.aesccm_enc.aes->devId = devIdArg;
  38910. }
  38911. else {
  38912. /* set devId to invalid, so software is used */
  38913. info->cipher.aesccm_dec.aes->devId = INVALID_DEVID;
  38914. ret = wc_AesCcmDecrypt(
  38915. info->cipher.aesccm_dec.aes,
  38916. info->cipher.aesccm_dec.out,
  38917. info->cipher.aesccm_dec.in,
  38918. info->cipher.aesccm_dec.sz,
  38919. info->cipher.aesccm_dec.nonce,
  38920. info->cipher.aesccm_dec.nonceSz,
  38921. info->cipher.aesccm_dec.authTag,
  38922. info->cipher.aesccm_dec.authTagSz,
  38923. info->cipher.aesccm_dec.authIn,
  38924. info->cipher.aesccm_dec.authInSz);
  38925. /* reset devId */
  38926. info->cipher.aesccm_dec.aes->devId = devIdArg;
  38927. }
  38928. }
  38929. #endif
  38930. #ifndef NO_DES3
  38931. if (info->cipher.type == WC_CIPHER_DES3) {
  38932. if (info->cipher.enc) {
  38933. /* set devId to invalid, so software is used */
  38934. info->cipher.des3.des->devId = INVALID_DEVID;
  38935. ret = wc_Des3_CbcEncrypt(
  38936. info->cipher.des3.des,
  38937. info->cipher.des3.out,
  38938. info->cipher.des3.in,
  38939. info->cipher.des3.sz);
  38940. /* reset devId */
  38941. info->cipher.des3.des->devId = devIdArg;
  38942. }
  38943. else {
  38944. /* set devId to invalid, so software is used */
  38945. info->cipher.des3.des->devId = INVALID_DEVID;
  38946. ret = wc_Des3_CbcDecrypt(
  38947. info->cipher.des3.des,
  38948. info->cipher.des3.out,
  38949. info->cipher.des3.in,
  38950. info->cipher.des3.sz);
  38951. /* reset devId */
  38952. info->cipher.des3.des->devId = devIdArg;
  38953. }
  38954. }
  38955. #endif /* !NO_DES3 */
  38956. #endif /* !NO_AES || !NO_DES3 */
  38957. }
  38958. #if !defined(NO_SHA) || !defined(NO_SHA256) || \
  38959. defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)
  38960. else if (info->algo_type == WC_ALGO_TYPE_HASH) {
  38961. #if !defined(NO_SHA)
  38962. if (info->hash.type == WC_HASH_TYPE_SHA) {
  38963. if (info->hash.sha1 == NULL)
  38964. return NOT_COMPILED_IN;
  38965. /* set devId to invalid, so software is used */
  38966. info->hash.sha1->devId = INVALID_DEVID;
  38967. if (info->hash.in != NULL) {
  38968. ret = wc_ShaUpdate(
  38969. info->hash.sha1,
  38970. info->hash.in,
  38971. info->hash.inSz);
  38972. }
  38973. if (info->hash.digest != NULL) {
  38974. ret = wc_ShaFinal(
  38975. info->hash.sha1,
  38976. info->hash.digest);
  38977. }
  38978. /* reset devId */
  38979. info->hash.sha1->devId = devIdArg;
  38980. }
  38981. else
  38982. #endif
  38983. #if !defined(NO_SHA256)
  38984. if (info->hash.type == WC_HASH_TYPE_SHA256) {
  38985. if (info->hash.sha256 == NULL)
  38986. return NOT_COMPILED_IN;
  38987. /* set devId to invalid, so software is used */
  38988. info->hash.sha256->devId = INVALID_DEVID;
  38989. if (info->hash.in != NULL) {
  38990. ret = wc_Sha256Update(
  38991. info->hash.sha256,
  38992. info->hash.in,
  38993. info->hash.inSz);
  38994. }
  38995. if (info->hash.digest != NULL) {
  38996. ret = wc_Sha256Final(
  38997. info->hash.sha256,
  38998. info->hash.digest);
  38999. }
  39000. /* reset devId */
  39001. info->hash.sha256->devId = devIdArg;
  39002. }
  39003. else
  39004. #endif
  39005. #ifdef WOLFSSL_SHA384
  39006. if (info->hash.type == WC_HASH_TYPE_SHA384) {
  39007. if (info->hash.sha384 == NULL)
  39008. return NOT_COMPILED_IN;
  39009. #ifndef NO_SHA2_CRYPTO_CB
  39010. /* set devId to invalid, so software is used */
  39011. info->hash.sha384->devId = INVALID_DEVID;
  39012. #endif
  39013. if (info->hash.in != NULL) {
  39014. ret = wc_Sha384Update(
  39015. info->hash.sha384,
  39016. info->hash.in,
  39017. info->hash.inSz);
  39018. }
  39019. if (info->hash.digest != NULL) {
  39020. ret = wc_Sha384Final(
  39021. info->hash.sha384,
  39022. info->hash.digest);
  39023. }
  39024. #ifndef NO_SHA2_CRYPTO_CB
  39025. /* reset devId */
  39026. info->hash.sha384->devId = devIdArg;
  39027. #endif
  39028. }
  39029. else
  39030. #endif
  39031. #ifdef WOLFSSL_SHA512
  39032. if (info->hash.type == WC_HASH_TYPE_SHA512) {
  39033. if (info->hash.sha512 == NULL)
  39034. return NOT_COMPILED_IN;
  39035. #ifndef NO_SHA2_CRYPTO_CB
  39036. /* set devId to invalid, so software is used */
  39037. info->hash.sha512->devId = INVALID_DEVID;
  39038. #endif
  39039. if (info->hash.in != NULL) {
  39040. ret = wc_Sha512Update(
  39041. info->hash.sha512,
  39042. info->hash.in,
  39043. info->hash.inSz);
  39044. }
  39045. if (info->hash.digest != NULL) {
  39046. ret = wc_Sha512Final(
  39047. info->hash.sha512,
  39048. info->hash.digest);
  39049. }
  39050. #ifndef NO_SHA2_CRYPTO_CB
  39051. /* reset devId */
  39052. info->hash.sha512->devId = devIdArg;
  39053. #endif
  39054. }
  39055. else
  39056. #endif
  39057. {
  39058. }
  39059. }
  39060. #endif /* !NO_SHA || !NO_SHA256 */
  39061. #ifndef NO_HMAC
  39062. else if (info->algo_type == WC_ALGO_TYPE_HMAC) {
  39063. if (info->hmac.hmac == NULL)
  39064. return NOT_COMPILED_IN;
  39065. /* set devId to invalid, so software is used */
  39066. info->hmac.hmac->devId = INVALID_DEVID;
  39067. if (info->hash.in != NULL) {
  39068. ret = wc_HmacUpdate(
  39069. info->hmac.hmac,
  39070. info->hmac.in,
  39071. info->hmac.inSz);
  39072. }
  39073. else if (info->hash.digest != NULL) {
  39074. ret = wc_HmacFinal(
  39075. info->hmac.hmac,
  39076. info->hmac.digest);
  39077. }
  39078. /* reset devId */
  39079. info->hmac.hmac->devId = devIdArg;
  39080. }
  39081. #endif
  39082. (void)devIdArg;
  39083. (void)myCtx;
  39084. return ret;
  39085. }
  39086. WOLFSSL_TEST_SUBROUTINE int cryptocb_test(void)
  39087. {
  39088. int ret = 0;
  39089. myCryptoDevCtx myCtx;
  39090. /* example data for callback */
  39091. myCtx.exampleVar = 1;
  39092. /* set devId to something other than INVALID_DEVID */
  39093. devId = 1;
  39094. ret = wc_CryptoCb_RegisterDevice(devId, myCryptoDevCb, &myCtx);
  39095. #ifndef WC_NO_RNG
  39096. if (ret == 0)
  39097. ret = random_test();
  39098. #endif /* WC_NO_RNG */
  39099. #if !defined(NO_RSA)
  39100. PRIVATE_KEY_UNLOCK();
  39101. if (ret == 0)
  39102. ret = rsa_test();
  39103. PRIVATE_KEY_LOCK();
  39104. #endif
  39105. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  39106. PRIVATE_KEY_UNLOCK();
  39107. if (ret == 0)
  39108. ret = rsa_onlycb_test(&myCtx);
  39109. PRIVATE_KEY_LOCK();
  39110. #endif
  39111. #if defined(HAVE_ECC)
  39112. PRIVATE_KEY_UNLOCK();
  39113. if (ret == 0)
  39114. ret = ecc_test();
  39115. PRIVATE_KEY_LOCK();
  39116. #endif
  39117. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  39118. PRIVATE_KEY_UNLOCK();
  39119. if (ret == 0)
  39120. ret = ecc_onlycb_test(&myCtx);
  39121. PRIVATE_KEY_LOCK();
  39122. #endif
  39123. #ifdef HAVE_ED25519
  39124. if (ret == 0)
  39125. ret = ed25519_test();
  39126. #endif
  39127. #ifdef HAVE_CURVE25519
  39128. if (ret == 0)
  39129. ret = curve25519_test();
  39130. #endif
  39131. #ifndef NO_AES
  39132. #ifdef HAVE_AESGCM
  39133. if (ret == 0)
  39134. ret = aesgcm_test();
  39135. #endif
  39136. #ifdef HAVE_AES_CBC
  39137. if (ret == 0)
  39138. ret = aes_test();
  39139. #endif
  39140. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  39141. if (ret == 0)
  39142. ret = aesccm_test();
  39143. #endif
  39144. #endif /* !NO_AES */
  39145. #ifndef NO_DES3
  39146. if (ret == 0)
  39147. ret = des3_test();
  39148. #endif /* !NO_DES3 */
  39149. #ifndef NO_SHA
  39150. if (ret == 0)
  39151. ret = sha_test();
  39152. #endif
  39153. #ifndef NO_SHA256
  39154. if (ret == 0)
  39155. ret = sha256_test();
  39156. #endif
  39157. #ifdef WOLFSSL_SHA384
  39158. if (ret == 0)
  39159. ret = sha384_test();
  39160. #endif
  39161. #ifdef WOLFSSL_SHA512
  39162. if (ret == 0)
  39163. ret = sha512_test();
  39164. #endif
  39165. #ifndef NO_HMAC
  39166. #ifndef NO_SHA
  39167. if (ret == 0)
  39168. ret = hmac_sha_test();
  39169. #endif
  39170. #ifndef NO_SHA256
  39171. if (ret == 0)
  39172. ret = hmac_sha256_test();
  39173. #endif
  39174. #endif
  39175. #ifndef NO_PWDBASED
  39176. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256)
  39177. if (ret == 0)
  39178. ret = pbkdf2_test();
  39179. #endif
  39180. #endif
  39181. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  39182. if (ret == 0)
  39183. ret = cmac_test();
  39184. #endif
  39185. /* reset devId */
  39186. devId = INVALID_DEVID;
  39187. return ret;
  39188. }
  39189. #endif /* WOLF_CRYPTO_CB */
  39190. #ifdef WOLFSSL_CERT_PIV
  39191. WOLFSSL_TEST_SUBROUTINE int certpiv_test(void)
  39192. {
  39193. int ret;
  39194. wc_CertPIV piv;
  39195. /* Template for Identiv PIV cert, nonce and signature */
  39196. WOLFSSL_SMALL_STACK_STATIC const byte pivCertIdentiv[] = {
  39197. 0x0A, 0x0B,
  39198. 0x53, 0x09, /* NIST PIV Cert */
  39199. 0x70, 0x02, /* Certificate */
  39200. 0x30, 0x00,
  39201. 0x71, 0x01, 0x05, /* Cert Info */
  39202. 0xFE, 0x00, /* Error Detection */
  39203. 0x0B, 0x01, 0x00, /* Nonce */
  39204. 0x0C, 0x01, 0x00, /* Signed Nonce */
  39205. };
  39206. /* PIV certificate data including certificate, info and error dectection. */
  39207. WOLFSSL_SMALL_STACK_STATIC const byte pivCert[] = {
  39208. 0x53, 0x09, /* NIST PIV Cert */
  39209. 0x70, 0x02, /* Certificate */
  39210. 0x30, 0x00,
  39211. 0x71, 0x01, 0x04, /* Cert Info */
  39212. 0xFE, 0x00, /* Error Detection */
  39213. };
  39214. XMEMSET(&piv, 0, sizeof(piv));
  39215. /* Test with Identiv 0x0A, 0x0B and 0x0C markers */
  39216. ret = wc_ParseCertPIV(&piv, pivCertIdentiv, sizeof(pivCertIdentiv));
  39217. if (ret != 0) {
  39218. return -14000;
  39219. }
  39220. if (!piv.isIdentiv) {
  39221. return -14001;
  39222. }
  39223. if ((piv.cert == NULL) || (piv.certSz != 2)) {
  39224. return -14002;
  39225. }
  39226. if ((piv.certErrDet == NULL) || (piv.certErrDetSz != 0)) {
  39227. return -14003;
  39228. }
  39229. if ((piv.compression != ASN_PIV_CERT_INFO_GZIP)) {
  39230. return -14004;
  39231. }
  39232. if (!piv.isX509) {
  39233. return -14005;
  39234. }
  39235. if ((piv.nonce == NULL) || (piv.nonceSz != 1)) {
  39236. return -14006;
  39237. }
  39238. if ((piv.signedNonce == NULL) || (piv.signedNonceSz != 1)) {
  39239. return -14007;
  39240. }
  39241. XMEMSET(&piv, 0, sizeof(piv));
  39242. /* Test with NIST PIV format */
  39243. ret = wc_ParseCertPIV(&piv, pivCert, sizeof(pivCert));
  39244. if (ret != 0) {
  39245. return -14010;
  39246. }
  39247. if (piv.isIdentiv) {
  39248. return -14011;
  39249. }
  39250. if ((piv.cert == NULL) || (piv.certSz != 2)) {
  39251. return -14012;
  39252. }
  39253. if ((piv.certErrDet == NULL) || (piv.certErrDetSz != 0)) {
  39254. return -14013;
  39255. }
  39256. if ((piv.compression != 0)) {
  39257. return -14014;
  39258. }
  39259. if (!piv.isX509) {
  39260. return -14015;
  39261. }
  39262. return ret;
  39263. }
  39264. #endif /* WOLFSSL_CERT_PIV */
  39265. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  39266. static time_t time_cb(time_t* t)
  39267. {
  39268. if (t != NULL) {
  39269. *t = 99;
  39270. }
  39271. return 99;
  39272. }
  39273. WOLFSSL_TEST_SUBROUTINE int time_test(void)
  39274. {
  39275. time_t t;
  39276. if (wc_SetTimeCb(time_cb) != 0)
  39277. return -15000;
  39278. t = wc_Time(NULL);
  39279. if (t != 99)
  39280. return -15001;
  39281. if (wc_GetTime(&t, sizeof(time_t)) != 0)
  39282. return -15002;
  39283. if (t != 99)
  39284. return -15003;
  39285. if (wc_SetTimeCb(NULL) != 0)
  39286. return -15004;
  39287. return 0;
  39288. }
  39289. #endif
  39290. #ifdef WOLFSSL_AES_SIV
  39291. typedef struct {
  39292. const byte key[33];
  39293. word32 keySz;
  39294. const byte nonce[49];
  39295. word32 nonceSz;
  39296. const byte assoc[81];
  39297. word32 assocSz;
  39298. const byte plaintext[83];
  39299. word32 plaintextSz;
  39300. const byte siv[AES_BLOCK_SIZE+1];
  39301. const byte ciphertext[82];
  39302. word32 ciphertextSz;
  39303. } AesSivTestVector;
  39304. #define AES_SIV_TEST_VECTORS 7
  39305. WOLFSSL_TEST_SUBROUTINE int aes_siv_test(void)
  39306. {
  39307. /* These test vectors come from chrony 4.1's SIV unit tests. */
  39308. WOLFSSL_SMALL_STACK_STATIC const AesSivTestVector testVectors[AES_SIV_TEST_VECTORS] = {
  39309. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  39310. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  39311. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  39312. "", 0,
  39313. "", 0,
  39314. "\x22\x3e\xb5\x94\xe0\xe0\x25\x4b\x00\x25\x8e\x21\x9a\x1c\xa4\x21",
  39315. "", 0
  39316. },
  39317. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  39318. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  39319. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  39320. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  39321. "", 0,
  39322. "\xd7\x20\x19\x89\xc6\xdb\xc6\xd6\x61\xfc\x62\xbc\x86\x5e\xee\xef",
  39323. "", 0
  39324. },
  39325. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  39326. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  39327. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  39328. "", 0,
  39329. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  39330. "\xb6\xc1\x60\xe9\xc2\xfd\x2a\xe8\xde\xc5\x36\x8b\x2a\x33\xed\xe1",
  39331. "\x14\xff\xb3\x97\x34\x5c\xcb\xe4\x4a\xa4\xde\xac\xd9\x36\x90\x46", 16
  39332. },
  39333. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  39334. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  39335. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e", 15,
  39336. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c", 15,
  39337. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4", 15,
  39338. "\x03\x8c\x41\x51\xba\x7a\x8f\x77\x6e\x56\x31\x99\x42\x0b\xc7\x03",
  39339. "\xe7\x6c\x67\xc9\xda\xb7\x0d\x5b\x44\x06\x26\x5a\xd0\xd2\x3b", 15
  39340. },
  39341. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  39342. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  39343. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  39344. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  39345. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4\xa7", 16,
  39346. "\x5c\x05\x23\x65\xf4\x57\x0a\xa0\xfb\x38\x3e\xce\x9b\x75\x85\xeb",
  39347. "\x68\x85\x19\x36\x0c\x7c\x48\x11\x40\xcb\x9b\x57\x9a\x0e\x65\x32", 16
  39348. },
  39349. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  39350. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  39351. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
  39352. "\xd5", 17,
  39353. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b"
  39354. "\xa0", 17,
  39355. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4\xa7"
  39356. "\x08", 17,
  39357. "\xaf\x58\x4b\xe7\x82\x1e\x96\x19\x29\x91\x25\xe0\xdd\x80\x3b\x49",
  39358. "\xa5\x11\xcd\xb6\x08\xf3\x76\xa0\xb6\xfa\x15\x82\xf3\x95\xe1\xeb"
  39359. "\xbd", 17
  39360. },
  39361. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  39362. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  39363. "\xb0\x5a\x1b\xc7\x56\xe7\xb6\x2c\xb4\x85\xe5\x56\xa5\x28\xc0\x6c"
  39364. "\x2f\x3b\x0b\x9d\x1a\x0c\xdf\x69\x47\xe0\xcc\xc0\x87\xaa\x5c\x09"
  39365. "\x98\x48\x8d\x6a\x8e\x1e\x05\xd7\x8b\x68\x74\x83\xb5\x1d\xf1\x2c", 48,
  39366. "\xe5\x8b\xd2\x6a\x30\xc5\xc5\x61\xcc\xbd\x7c\x27\xbf\xfe\xf9\x06"
  39367. "\x00\x5b\xd7\xfc\x11\x0b\xcf\x16\x61\xef\xac\x05\xa7\xaf\xec\x27"
  39368. "\x41\xc8\x5e\x9e\x0d\xf9\x2f\xaf\x20\x79\x17\xe5\x17\x91\x2a\x27"
  39369. "\x34\x1c\xbc\xaf\xeb\xef\x7f\x52\xe7\x1e\x4c\x2a\xca\xbd\x2b\xbe"
  39370. "\x34\xd6\xfb\x69\xd3\x3e\x49\x59\x60\xb4\x26\xc9\xb8\xce\xba", 79,
  39371. "\x6c\xe7\xcf\x7e\xab\x7b\xa0\xe1\xa7\x22\xcb\x88\xde\x5e\x42\xd2"
  39372. "\xec\x79\xe0\xa2\xcf\x5f\x0f\x6f\x6b\x89\x57\xcd\xae\x17\xd4\xc2"
  39373. "\xf3\x1b\xa2\xa8\x13\x78\x23\x2f\x83\xa8\xd4\x0c\xc0\xd2\xf3\x99"
  39374. "\xae\x81\xa1\xca\x5b\x5f\x45\xa6\x6f\x0c\x8a\xf3\xd4\x67\x40\x81"
  39375. "\x26\xe2\x01\x86\xe8\x5a\xd5\xf8\x58\x80\x9f\x56\xaa\x76\x96\xbf"
  39376. "\x31", 81,
  39377. "\x9a\x06\x33\xe0\xee\x00\x6a\x9b\xc8\x20\xd5\xe2\xc2\xed\xb5\x75",
  39378. "\xfa\x9e\x42\x2a\x31\x6b\xda\xca\xaa\x7d\x31\x8b\x84\x7a\xb8\xd7"
  39379. "\x8a\x81\x25\x64\xed\x41\x9b\xa9\x77\x10\xbd\x05\x0c\x4e\xc5\x31"
  39380. "\x0c\xa2\x86\xec\x8a\x94\xc8\x24\x23\x3c\x13\xee\xa5\x51\xc9\xdf"
  39381. "\x48\xc9\x55\xc5\x2f\x40\x73\x3f\x98\xbb\x8d\x69\x78\x46\x64\x17"
  39382. "\x8d\x49\x2f\x14\x62\xa4\x7c\x2a\x57\x38\x87\xce\xc6\x72\xd3\x5c"
  39383. "\xa1", 81
  39384. }};
  39385. int i;
  39386. byte computedCiphertext[82];
  39387. byte computedPlaintext[82];
  39388. byte siv[AES_BLOCK_SIZE];
  39389. int rc = 0;
  39390. for (i = 0; i < AES_SIV_TEST_VECTORS; ++i) {
  39391. rc = wc_AesSivEncrypt(testVectors[i].key, testVectors[i].keySz,
  39392. testVectors[i].assoc, testVectors[i].assocSz,
  39393. testVectors[i].nonce, testVectors[i].nonceSz,
  39394. testVectors[i].plaintext,
  39395. testVectors[i].plaintextSz, siv,
  39396. computedCiphertext);
  39397. if (rc != 0) {
  39398. return -16000;
  39399. }
  39400. rc = XMEMCMP(siv, testVectors[i].siv, AES_BLOCK_SIZE);
  39401. if (rc != 0) {
  39402. return -16001;
  39403. }
  39404. rc = XMEMCMP(computedCiphertext, testVectors[i].ciphertext,
  39405. testVectors[i].ciphertextSz);
  39406. if (rc != 0) {
  39407. return -16002;
  39408. }
  39409. rc = wc_AesSivDecrypt(testVectors[i].key, testVectors[i].keySz,
  39410. testVectors[i].assoc, testVectors[i].assocSz,
  39411. testVectors[i].nonce, testVectors[i].nonceSz,
  39412. computedCiphertext, testVectors[i].plaintextSz,
  39413. siv, computedPlaintext);
  39414. if (rc != 0) {
  39415. return -16003;
  39416. }
  39417. rc = XMEMCMP(computedPlaintext, testVectors[i].plaintext,
  39418. testVectors[i].plaintextSz);
  39419. if (rc != 0) {
  39420. return -16004;
  39421. }
  39422. }
  39423. return 0;
  39424. }
  39425. #endif
  39426. #undef ERROR_OUT
  39427. #else
  39428. #ifndef NO_MAIN_DRIVER
  39429. int main() { return 0; }
  39430. #endif
  39431. #endif /* NO_CRYPT_TEST */