test.c 1.5 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924349253492634927349283492934930349313493234933349343493534936349373493834939349403494134942349433494434945349463494734948349493495034951349523495334954349553495634957349583495934960349613496234963349643496534966349673496834969349703497134972349733497434975349763497734978349793498034981349823498334984349853498634987349883498934990349913499234993349943499534996349973499834999350003500135002350033500435005350063500735008350093501035011350123501335014350153501635017350183501935020350213502235023350243502535026350273502835029350303503135032350333503435035350363503735038350393504035041350423504335044350453504635047350483504935050350513505235053350543505535056350573505835059350603506135062350633506435065350663506735068350693507035071350723507335074350753507635077350783507935080350813508235083350843508535086350873508835089350903509135092350933509435095350963509735098350993510035101351023510335104351053510635107351083510935110351113511235113351143511535116351173511835119351203512135122351233512435125351263512735128351293513035131351323513335134351353513635137351383513935140351413514235143351443514535146351473514835149351503515135152351533515435155351563515735158351593516035161351623516335164351653516635167351683516935170351713517235173351743517535176351773517835179351803518135182351833518435185351863518735188351893519035191351923519335194351953519635197351983519935200352013520235203352043520535206352073520835209352103521135212352133521435215352163521735218352193522035221352223522335224352253522635227352283522935230352313523235233352343523535236352373523835239352403524135242352433524435245352463524735248352493525035251352523525335254352553525635257352583525935260352613526235263352643526535266352673526835269352703527135272352733527435275352763527735278352793528035281352823528335284352853528635287352883528935290352913529235293352943529535296352973529835299353003530135302353033530435305353063530735308353093531035311353123531335314353153531635317353183531935320353213532235323353243532535326353273532835329353303533135332353333533435335353363533735338353393534035341353423534335344353453534635347353483534935350353513535235353353543535535356353573535835359353603536135362353633536435365353663536735368353693537035371353723537335374353753537635377353783537935380353813538235383353843538535386353873538835389353903539135392353933539435395353963539735398353993540035401354023540335404354053540635407354083540935410354113541235413354143541535416354173541835419354203542135422354233542435425354263542735428354293543035431354323543335434354353543635437354383543935440354413544235443354443544535446354473544835449354503545135452354533545435455354563545735458354593546035461354623546335464354653546635467354683546935470354713547235473354743547535476354773547835479354803548135482354833548435485354863548735488354893549035491354923549335494354953549635497354983549935500355013550235503355043550535506355073550835509355103551135512355133551435515355163551735518355193552035521355223552335524355253552635527355283552935530355313553235533355343553535536355373553835539355403554135542355433554435545355463554735548355493555035551355523555335554355553555635557355583555935560355613556235563355643556535566355673556835569355703557135572355733557435575355763557735578355793558035581355823558335584355853558635587355883558935590355913559235593355943559535596355973559835599356003560135602356033560435605356063560735608356093561035611356123561335614356153561635617356183561935620356213562235623356243562535626356273562835629356303563135632356333563435635356363563735638356393564035641356423564335644356453564635647356483564935650356513565235653356543565535656356573565835659356603566135662356633566435665356663566735668356693567035671356723567335674356753567635677356783567935680356813568235683356843568535686356873568835689356903569135692356933569435695356963569735698356993570035701357023570335704357053570635707357083570935710357113571235713357143571535716357173571835719357203572135722357233572435725357263572735728357293573035731357323573335734357353573635737357383573935740357413574235743357443574535746357473574835749357503575135752357533575435755357563575735758357593576035761357623576335764357653576635767357683576935770357713577235773357743577535776357773577835779357803578135782357833578435785357863578735788357893579035791357923579335794357953579635797357983579935800358013580235803358043580535806358073580835809358103581135812358133581435815358163581735818358193582035821358223582335824358253582635827358283582935830358313583235833358343583535836358373583835839358403584135842358433584435845358463584735848358493585035851358523585335854358553585635857358583585935860358613586235863358643586535866358673586835869358703587135872358733587435875358763587735878358793588035881358823588335884358853588635887358883588935890358913589235893358943589535896358973589835899359003590135902359033590435905359063590735908359093591035911359123591335914359153591635917359183591935920359213592235923359243592535926359273592835929359303593135932359333593435935359363593735938359393594035941359423594335944359453594635947359483594935950359513595235953359543595535956359573595835959359603596135962359633596435965359663596735968359693597035971359723597335974359753597635977359783597935980359813598235983359843598535986359873598835989359903599135992359933599435995359963599735998359993600036001360023600336004360053600636007360083600936010360113601236013360143601536016360173601836019360203602136022360233602436025360263602736028360293603036031360323603336034360353603636037360383603936040360413604236043360443604536046360473604836049360503605136052360533605436055360563605736058360593606036061360623606336064360653606636067360683606936070360713607236073360743607536076360773607836079360803608136082360833608436085360863608736088360893609036091360923609336094360953609636097360983609936100361013610236103361043610536106361073610836109361103611136112361133611436115361163611736118361193612036121361223612336124361253612636127361283612936130361313613236133361343613536136361373613836139361403614136142361433614436145361463614736148361493615036151361523615336154361553615636157361583615936160361613616236163361643616536166361673616836169361703617136172361733617436175361763617736178361793618036181361823618336184361853618636187361883618936190361913619236193361943619536196361973619836199362003620136202362033620436205362063620736208362093621036211362123621336214362153621636217362183621936220362213622236223362243622536226362273622836229362303623136232362333623436235362363623736238362393624036241362423624336244362453624636247362483624936250362513625236253362543625536256362573625836259362603626136262362633626436265362663626736268362693627036271362723627336274362753627636277362783627936280362813628236283362843628536286362873628836289362903629136292362933629436295362963629736298362993630036301363023630336304363053630636307363083630936310363113631236313363143631536316363173631836319363203632136322363233632436325363263632736328363293633036331363323633336334363353633636337363383633936340363413634236343363443634536346363473634836349363503635136352363533635436355363563635736358363593636036361363623636336364363653636636367363683636936370363713637236373363743637536376363773637836379363803638136382363833638436385363863638736388363893639036391363923639336394363953639636397363983639936400364013640236403364043640536406364073640836409364103641136412364133641436415364163641736418364193642036421364223642336424364253642636427364283642936430364313643236433364343643536436364373643836439364403644136442364433644436445364463644736448364493645036451364523645336454364553645636457364583645936460364613646236463364643646536466364673646836469364703647136472364733647436475364763647736478364793648036481364823648336484364853648636487364883648936490364913649236493364943649536496364973649836499365003650136502365033650436505365063650736508365093651036511365123651336514365153651636517365183651936520365213652236523365243652536526365273652836529365303653136532365333653436535365363653736538365393654036541365423654336544365453654636547365483654936550365513655236553365543655536556365573655836559365603656136562365633656436565365663656736568365693657036571365723657336574365753657636577365783657936580365813658236583365843658536586365873658836589365903659136592365933659436595365963659736598365993660036601366023660336604366053660636607366083660936610366113661236613366143661536616366173661836619366203662136622366233662436625366263662736628366293663036631366323663336634366353663636637366383663936640366413664236643366443664536646366473664836649366503665136652366533665436655366563665736658366593666036661366623666336664366653666636667366683666936670366713667236673366743667536676366773667836679366803668136682366833668436685366863668736688366893669036691366923669336694366953669636697366983669936700367013670236703367043670536706367073670836709367103671136712367133671436715367163671736718367193672036721367223672336724367253672636727367283672936730367313673236733367343673536736367373673836739367403674136742367433674436745367463674736748367493675036751367523675336754367553675636757367583675936760367613676236763367643676536766367673676836769367703677136772367733677436775367763677736778367793678036781367823678336784367853678636787367883678936790367913679236793367943679536796367973679836799368003680136802368033680436805368063680736808368093681036811368123681336814368153681636817368183681936820368213682236823368243682536826368273682836829368303683136832368333683436835368363683736838368393684036841368423684336844368453684636847368483684936850368513685236853368543685536856368573685836859368603686136862368633686436865368663686736868368693687036871368723687336874368753687636877368783687936880368813688236883368843688536886368873688836889368903689136892368933689436895368963689736898368993690036901369023690336904369053690636907369083690936910369113691236913369143691536916369173691836919369203692136922369233692436925369263692736928369293693036931369323693336934369353693636937369383693936940369413694236943369443694536946369473694836949369503695136952369533695436955369563695736958369593696036961369623696336964369653696636967369683696936970369713697236973369743697536976369773697836979369803698136982369833698436985369863698736988369893699036991369923699336994369953699636997369983699937000370013700237003370043700537006370073700837009370103701137012370133701437015370163701737018370193702037021370223702337024370253702637027370283702937030370313703237033370343703537036370373703837039370403704137042370433704437045370463704737048370493705037051370523705337054370553705637057370583705937060370613706237063370643706537066370673706837069370703707137072370733707437075370763707737078370793708037081370823708337084370853708637087370883708937090370913709237093370943709537096370973709837099371003710137102371033710437105371063710737108371093711037111371123711337114371153711637117371183711937120371213712237123371243712537126371273712837129371303713137132371333713437135371363713737138371393714037141371423714337144371453714637147371483714937150371513715237153371543715537156371573715837159371603716137162371633716437165371663716737168371693717037171371723717337174371753717637177371783717937180371813718237183371843718537186371873718837189371903719137192371933719437195371963719737198371993720037201372023720337204372053720637207372083720937210372113721237213372143721537216372173721837219372203722137222372233722437225372263722737228372293723037231372323723337234372353723637237372383723937240372413724237243372443724537246372473724837249372503725137252372533725437255372563725737258372593726037261372623726337264372653726637267372683726937270372713727237273372743727537276372773727837279372803728137282372833728437285372863728737288372893729037291372923729337294372953729637297372983729937300373013730237303373043730537306373073730837309373103731137312373133731437315373163731737318373193732037321373223732337324373253732637327373283732937330373313733237333373343733537336373373733837339373403734137342373433734437345373463734737348373493735037351373523735337354373553735637357373583735937360373613736237363373643736537366373673736837369373703737137372373733737437375373763737737378373793738037381373823738337384373853738637387373883738937390373913739237393373943739537396373973739837399374003740137402374033740437405374063740737408374093741037411374123741337414374153741637417374183741937420374213742237423374243742537426374273742837429374303743137432374333743437435374363743737438374393744037441374423744337444374453744637447374483744937450374513745237453374543745537456374573745837459374603746137462374633746437465374663746737468374693747037471374723747337474374753747637477374783747937480374813748237483374843748537486374873748837489374903749137492374933749437495374963749737498374993750037501375023750337504375053750637507375083750937510375113751237513375143751537516375173751837519375203752137522375233752437525375263752737528375293753037531375323753337534375353753637537375383753937540375413754237543375443754537546375473754837549375503755137552375533755437555375563755737558375593756037561375623756337564375653756637567375683756937570375713757237573375743757537576375773757837579375803758137582375833758437585375863758737588375893759037591375923759337594375953759637597375983759937600376013760237603376043760537606376073760837609376103761137612376133761437615376163761737618376193762037621376223762337624376253762637627376283762937630376313763237633376343763537636376373763837639376403764137642376433764437645376463764737648376493765037651376523765337654376553765637657376583765937660376613766237663376643766537666376673766837669376703767137672376733767437675376763767737678376793768037681376823768337684376853768637687376883768937690376913769237693376943769537696376973769837699377003770137702377033770437705377063770737708377093771037711377123771337714377153771637717377183771937720377213772237723377243772537726377273772837729377303773137732377333773437735377363773737738377393774037741377423774337744377453774637747377483774937750377513775237753377543775537756377573775837759377603776137762377633776437765377663776737768377693777037771377723777337774377753777637777377783777937780377813778237783377843778537786377873778837789377903779137792377933779437795377963779737798377993780037801378023780337804378053780637807378083780937810378113781237813378143781537816378173781837819378203782137822378233782437825378263782737828378293783037831378323783337834378353783637837378383783937840378413784237843378443784537846378473784837849378503785137852378533785437855378563785737858378593786037861378623786337864378653786637867378683786937870378713787237873378743787537876378773787837879378803788137882378833788437885378863788737888378893789037891378923789337894378953789637897378983789937900379013790237903379043790537906379073790837909379103791137912379133791437915379163791737918379193792037921379223792337924379253792637927379283792937930379313793237933379343793537936379373793837939379403794137942379433794437945379463794737948379493795037951379523795337954379553795637957379583795937960379613796237963379643796537966379673796837969379703797137972379733797437975379763797737978379793798037981379823798337984379853798637987379883798937990379913799237993379943799537996379973799837999380003800138002380033800438005380063800738008380093801038011380123801338014380153801638017380183801938020380213802238023380243802538026380273802838029380303803138032380333803438035380363803738038380393804038041380423804338044380453804638047380483804938050380513805238053380543805538056380573805838059380603806138062380633806438065380663806738068380693807038071380723807338074380753807638077380783807938080380813808238083380843808538086380873808838089380903809138092380933809438095380963809738098380993810038101381023810338104381053810638107381083810938110381113811238113381143811538116381173811838119381203812138122381233812438125381263812738128381293813038131381323813338134381353813638137381383813938140381413814238143381443814538146381473814838149381503815138152381533815438155381563815738158381593816038161381623816338164381653816638167381683816938170381713817238173381743817538176381773817838179381803818138182381833818438185381863818738188381893819038191381923819338194381953819638197381983819938200382013820238203382043820538206382073820838209382103821138212382133821438215382163821738218382193822038221382223822338224382253822638227382283822938230382313823238233382343823538236382373823838239382403824138242382433824438245382463824738248382493825038251382523825338254382553825638257382583825938260382613826238263382643826538266382673826838269382703827138272382733827438275382763827738278382793828038281382823828338284382853828638287382883828938290382913829238293382943829538296382973829838299383003830138302383033830438305383063830738308383093831038311383123831338314383153831638317383183831938320383213832238323383243832538326383273832838329383303833138332383333833438335383363833738338383393834038341383423834338344383453834638347383483834938350383513835238353383543835538356383573835838359383603836138362383633836438365383663836738368383693837038371383723837338374383753837638377383783837938380383813838238383383843838538386383873838838389383903839138392383933839438395383963839738398383993840038401384023840338404384053840638407384083840938410384113841238413384143841538416384173841838419384203842138422384233842438425384263842738428384293843038431384323843338434384353843638437384383843938440384413844238443384443844538446384473844838449384503845138452384533845438455384563845738458384593846038461384623846338464384653846638467384683846938470384713847238473384743847538476384773847838479384803848138482384833848438485384863848738488384893849038491384923849338494384953849638497384983849938500385013850238503385043850538506385073850838509385103851138512385133851438515385163851738518385193852038521385223852338524385253852638527385283852938530385313853238533385343853538536385373853838539385403854138542385433854438545385463854738548385493855038551385523855338554385553855638557385583855938560385613856238563385643856538566385673856838569385703857138572385733857438575385763857738578385793858038581385823858338584385853858638587385883858938590385913859238593385943859538596385973859838599386003860138602386033860438605386063860738608386093861038611386123861338614386153861638617386183861938620386213862238623386243862538626386273862838629386303863138632386333863438635386363863738638386393864038641386423864338644386453864638647386483864938650386513865238653386543865538656386573865838659386603866138662386633866438665386663866738668386693867038671386723867338674386753867638677386783867938680386813868238683386843868538686386873868838689386903869138692386933869438695386963869738698386993870038701387023870338704387053870638707387083870938710387113871238713387143871538716387173871838719387203872138722387233872438725387263872738728387293873038731387323873338734387353873638737387383873938740387413874238743387443874538746387473874838749387503875138752387533875438755387563875738758387593876038761387623876338764387653876638767387683876938770387713877238773387743877538776387773877838779387803878138782387833878438785387863878738788387893879038791387923879338794387953879638797387983879938800388013880238803388043880538806388073880838809388103881138812388133881438815388163881738818388193882038821388223882338824388253882638827388283882938830388313883238833388343883538836388373883838839388403884138842388433884438845388463884738848388493885038851388523885338854388553885638857388583885938860388613886238863388643886538866388673886838869388703887138872388733887438875388763887738878388793888038881388823888338884388853888638887388883888938890388913889238893388943889538896388973889838899389003890138902389033890438905389063890738908389093891038911389123891338914389153891638917389183891938920389213892238923389243892538926389273892838929389303893138932389333893438935389363893738938389393894038941389423894338944389453894638947389483894938950389513895238953389543895538956389573895838959389603896138962389633896438965389663896738968389693897038971389723897338974389753897638977389783897938980389813898238983389843898538986389873898838989389903899138992389933899438995389963899738998389993900039001390023900339004390053900639007390083900939010390113901239013390143901539016390173901839019390203902139022390233902439025390263902739028390293903039031390323903339034390353903639037390383903939040390413904239043390443904539046390473904839049390503905139052390533905439055390563905739058390593906039061390623906339064390653906639067390683906939070390713907239073390743907539076390773907839079390803908139082390833908439085390863908739088390893909039091390923909339094390953909639097390983909939100391013910239103391043910539106391073910839109391103911139112391133911439115391163911739118391193912039121391223912339124391253912639127391283912939130391313913239133391343913539136391373913839139391403914139142391433914439145391463914739148391493915039151391523915339154391553915639157391583915939160391613916239163391643916539166391673916839169391703917139172391733917439175391763917739178391793918039181391823918339184391853918639187391883918939190391913919239193391943919539196391973919839199392003920139202392033920439205392063920739208392093921039211392123921339214392153921639217392183921939220392213922239223392243922539226392273922839229392303923139232392333923439235392363923739238392393924039241392423924339244392453924639247392483924939250392513925239253392543925539256392573925839259392603926139262392633926439265392663926739268392693927039271392723927339274392753927639277392783927939280392813928239283392843928539286392873928839289392903929139292392933929439295392963929739298392993930039301393023930339304393053930639307393083930939310393113931239313393143931539316393173931839319393203932139322393233932439325393263932739328393293933039331393323933339334393353933639337393383933939340393413934239343393443934539346393473934839349393503935139352393533935439355393563935739358393593936039361393623936339364393653936639367393683936939370393713937239373393743937539376393773937839379393803938139382393833938439385393863938739388393893939039391393923939339394393953939639397393983939939400394013940239403394043940539406394073940839409394103941139412394133941439415394163941739418394193942039421394223942339424394253942639427394283942939430394313943239433394343943539436394373943839439394403944139442394433944439445394463944739448394493945039451394523945339454394553945639457394583945939460394613946239463394643946539466394673946839469394703947139472394733947439475394763947739478394793948039481394823948339484394853948639487394883948939490394913949239493394943949539496394973949839499395003950139502395033950439505395063950739508395093951039511395123951339514395153951639517395183951939520395213952239523395243952539526395273952839529395303953139532395333953439535395363953739538395393954039541395423954339544395453954639547395483954939550395513955239553395543955539556395573955839559395603956139562395633956439565395663956739568395693957039571395723957339574395753957639577395783957939580395813958239583395843958539586395873958839589395903959139592395933959439595395963959739598395993960039601396023960339604396053960639607396083960939610396113961239613396143961539616396173961839619396203962139622396233962439625396263962739628396293963039631396323963339634396353963639637396383963939640396413964239643396443964539646396473964839649396503965139652396533965439655396563965739658396593966039661396623966339664396653966639667396683966939670396713967239673396743967539676396773967839679396803968139682396833968439685396863968739688396893969039691396923969339694396953969639697396983969939700397013970239703397043970539706397073970839709397103971139712397133971439715397163971739718397193972039721397223972339724397253972639727397283972939730397313973239733397343973539736397373973839739397403974139742397433974439745397463974739748397493975039751397523975339754397553975639757397583975939760397613976239763397643976539766397673976839769397703977139772397733977439775397763977739778397793978039781397823978339784397853978639787397883978939790397913979239793397943979539796397973979839799398003980139802398033980439805398063980739808398093981039811398123981339814398153981639817398183981939820398213982239823398243982539826398273982839829398303983139832398333983439835398363983739838398393984039841398423984339844398453984639847398483984939850398513985239853398543985539856398573985839859398603986139862398633986439865398663986739868398693987039871398723987339874398753987639877398783987939880398813988239883398843988539886398873988839889398903989139892398933989439895398963989739898398993990039901399023990339904399053990639907399083990939910399113991239913399143991539916399173991839919399203992139922399233992439925399263992739928399293993039931399323993339934399353993639937399383993939940399413994239943399443994539946399473994839949399503995139952399533995439955399563995739958399593996039961399623996339964399653996639967399683996939970399713997239973399743997539976399773997839979399803998139982399833998439985399863998739988399893999039991399923999339994399953999639997399983999940000400014000240003400044000540006400074000840009400104001140012400134001440015400164001740018400194002040021400224002340024400254002640027400284002940030400314003240033400344003540036400374003840039400404004140042400434004440045400464004740048400494005040051400524005340054400554005640057400584005940060400614006240063400644006540066400674006840069400704007140072400734007440075400764007740078400794008040081400824008340084400854008640087400884008940090400914009240093400944009540096400974009840099401004010140102401034010440105401064010740108401094011040111401124011340114401154011640117401184011940120401214012240123401244012540126401274012840129401304013140132401334013440135401364013740138401394014040141401424014340144401454014640147401484014940150401514015240153401544015540156401574015840159401604016140162401634016440165401664016740168401694017040171401724017340174401754017640177401784017940180401814018240183401844018540186401874018840189401904019140192401934019440195401964019740198401994020040201402024020340204402054020640207402084020940210402114021240213402144021540216402174021840219402204022140222402234022440225402264022740228402294023040231402324023340234402354023640237402384023940240402414024240243402444024540246402474024840249402504025140252402534025440255402564025740258402594026040261402624026340264402654026640267402684026940270402714027240273402744027540276402774027840279402804028140282402834028440285402864028740288402894029040291402924029340294402954029640297402984029940300403014030240303403044030540306403074030840309403104031140312403134031440315403164031740318403194032040321403224032340324403254032640327403284032940330403314033240333403344033540336403374033840339403404034140342403434034440345403464034740348403494035040351403524035340354403554035640357403584035940360403614036240363403644036540366403674036840369403704037140372403734037440375403764037740378403794038040381403824038340384403854038640387403884038940390403914039240393403944039540396403974039840399404004040140402404034040440405404064040740408404094041040411404124041340414404154041640417404184041940420404214042240423404244042540426404274042840429404304043140432404334043440435404364043740438404394044040441404424044340444404454044640447404484044940450404514045240453404544045540456404574045840459404604046140462404634046440465404664046740468404694047040471404724047340474404754047640477404784047940480404814048240483404844048540486404874048840489404904049140492404934049440495404964049740498404994050040501405024050340504405054050640507405084050940510405114051240513405144051540516405174051840519405204052140522405234052440525405264052740528405294053040531405324053340534405354053640537405384053940540405414054240543405444054540546405474054840549405504055140552405534055440555405564055740558405594056040561405624056340564405654056640567405684056940570405714057240573405744057540576405774057840579405804058140582405834058440585405864058740588405894059040591405924059340594405954059640597405984059940600406014060240603406044060540606406074060840609406104061140612406134061440615406164061740618406194062040621406224062340624406254062640627406284062940630406314063240633406344063540636406374063840639406404064140642406434064440645406464064740648406494065040651406524065340654406554065640657406584065940660406614066240663406644066540666406674066840669406704067140672406734067440675406764067740678406794068040681406824068340684406854068640687406884068940690406914069240693406944069540696406974069840699407004070140702407034070440705407064070740708407094071040711407124071340714407154071640717407184071940720407214072240723407244072540726407274072840729407304073140732407334073440735407364073740738407394074040741407424074340744407454074640747407484074940750407514075240753407544075540756407574075840759407604076140762407634076440765407664076740768407694077040771407724077340774407754077640777407784077940780407814078240783407844078540786407874078840789407904079140792407934079440795407964079740798407994080040801408024080340804408054080640807408084080940810408114081240813408144081540816408174081840819408204082140822408234082440825408264082740828408294083040831408324083340834408354083640837408384083940840408414084240843408444084540846408474084840849408504085140852408534085440855408564085740858408594086040861408624086340864408654086640867408684086940870408714087240873408744087540876408774087840879408804088140882408834088440885408864088740888408894089040891408924089340894408954089640897408984089940900409014090240903409044090540906409074090840909409104091140912409134091440915409164091740918409194092040921409224092340924409254092640927409284092940930409314093240933409344093540936409374093840939409404094140942409434094440945409464094740948409494095040951409524095340954409554095640957409584095940960409614096240963409644096540966409674096840969409704097140972409734097440975409764097740978409794098040981409824098340984409854098640987409884098940990409914099240993409944099540996409974099840999410004100141002410034100441005410064100741008410094101041011410124101341014410154101641017410184101941020410214102241023410244102541026410274102841029410304103141032410334103441035410364103741038410394104041041410424104341044410454104641047410484104941050410514105241053410544105541056410574105841059410604106141062410634106441065410664106741068410694107041071410724107341074410754107641077410784107941080410814108241083410844108541086410874108841089410904109141092410934109441095410964109741098410994110041101411024110341104411054110641107411084110941110411114111241113411144111541116411174111841119411204112141122411234112441125411264112741128411294113041131411324113341134411354113641137411384113941140411414114241143411444114541146411474114841149411504115141152411534115441155411564115741158411594116041161411624116341164411654116641167411684116941170411714117241173411744117541176411774117841179411804118141182411834118441185411864118741188411894119041191411924119341194411954119641197411984119941200412014120241203412044120541206412074120841209412104121141212412134121441215412164121741218412194122041221412224122341224412254122641227412284122941230412314123241233412344123541236412374123841239412404124141242412434124441245412464124741248412494125041251412524125341254412554125641257412584125941260412614126241263412644126541266412674126841269412704127141272412734127441275412764127741278412794128041281412824128341284412854128641287412884128941290412914129241293412944129541296412974129841299413004130141302413034130441305413064130741308413094131041311413124131341314413154131641317413184131941320413214132241323413244132541326413274132841329413304133141332413334133441335413364133741338413394134041341413424134341344413454134641347413484134941350413514135241353413544135541356413574135841359413604136141362413634136441365413664136741368413694137041371413724137341374413754137641377413784137941380413814138241383413844138541386413874138841389413904139141392413934139441395413964139741398413994140041401414024140341404414054140641407414084140941410414114141241413414144141541416414174141841419414204142141422414234142441425414264142741428414294143041431414324143341434414354143641437414384143941440414414144241443414444144541446414474144841449414504145141452414534145441455414564145741458414594146041461414624146341464414654146641467414684146941470414714147241473414744147541476414774147841479414804148141482414834148441485414864148741488414894149041491414924149341494414954149641497414984149941500415014150241503415044150541506415074150841509415104151141512415134151441515415164151741518415194152041521415224152341524415254152641527415284152941530415314153241533415344153541536415374153841539415404154141542415434154441545415464154741548415494155041551415524155341554415554155641557415584155941560415614156241563415644156541566415674156841569415704157141572415734157441575415764157741578415794158041581415824158341584415854158641587415884158941590415914159241593415944159541596415974159841599416004160141602416034160441605416064160741608416094161041611416124161341614416154161641617416184161941620416214162241623416244162541626416274162841629416304163141632416334163441635416364163741638416394164041641416424164341644416454164641647416484164941650416514165241653416544165541656416574165841659416604166141662416634166441665416664166741668416694167041671416724167341674416754167641677416784167941680416814168241683416844168541686416874168841689416904169141692416934169441695416964169741698416994170041701417024170341704417054170641707417084170941710417114171241713417144171541716417174171841719417204172141722417234172441725417264172741728417294173041731417324173341734417354173641737417384173941740417414174241743417444174541746417474174841749417504175141752417534175441755417564175741758417594176041761417624176341764417654176641767417684176941770417714177241773417744177541776417774177841779417804178141782417834178441785417864178741788417894179041791417924179341794417954179641797417984179941800418014180241803418044180541806418074180841809418104181141812418134181441815418164181741818418194182041821418224182341824418254182641827418284182941830418314183241833418344183541836418374183841839418404184141842418434184441845418464184741848418494185041851418524185341854418554185641857418584185941860418614186241863418644186541866418674186841869418704187141872418734187441875418764187741878418794188041881418824188341884418854188641887418884188941890418914189241893418944189541896418974189841899419004190141902419034190441905419064190741908419094191041911419124191341914419154191641917419184191941920419214192241923419244192541926419274192841929419304193141932419334193441935419364193741938419394194041941419424194341944419454194641947419484194941950419514195241953419544195541956419574195841959419604196141962419634196441965419664196741968419694197041971419724197341974419754197641977419784197941980419814198241983419844198541986419874198841989419904199141992419934199441995419964199741998419994200042001420024200342004420054200642007420084200942010420114201242013420144201542016420174201842019420204202142022420234202442025420264202742028420294203042031420324203342034420354203642037420384203942040420414204242043420444204542046420474204842049420504205142052420534205442055420564205742058420594206042061420624206342064420654206642067420684206942070420714207242073420744207542076420774207842079420804208142082420834208442085420864208742088420894209042091420924209342094420954209642097420984209942100421014210242103421044210542106421074210842109421104211142112421134211442115421164211742118421194212042121421224212342124421254212642127421284212942130421314213242133421344213542136421374213842139421404214142142421434214442145421464214742148421494215042151421524215342154421554215642157421584215942160421614216242163421644216542166421674216842169421704217142172421734217442175421764217742178421794218042181421824218342184421854218642187421884218942190421914219242193421944219542196421974219842199422004220142202422034220442205422064220742208422094221042211422124221342214422154221642217422184221942220422214222242223422244222542226422274222842229422304223142232422334223442235422364223742238422394224042241422424224342244422454224642247422484224942250422514225242253422544225542256422574225842259422604226142262422634226442265422664226742268422694227042271422724227342274422754227642277422784227942280422814228242283422844228542286422874228842289422904229142292422934229442295422964229742298422994230042301423024230342304423054230642307423084230942310423114231242313423144231542316423174231842319423204232142322423234232442325423264232742328423294233042331423324233342334423354233642337423384233942340423414234242343423444234542346423474234842349423504235142352423534235442355423564235742358423594236042361423624236342364423654236642367423684236942370423714237242373423744237542376423774237842379423804238142382423834238442385423864238742388423894239042391423924239342394423954239642397423984239942400424014240242403424044240542406424074240842409424104241142412424134241442415424164241742418424194242042421424224242342424424254242642427424284242942430424314243242433424344243542436424374243842439424404244142442424434244442445424464244742448424494245042451424524245342454424554245642457424584245942460424614246242463424644246542466424674246842469424704247142472424734247442475424764247742478424794248042481424824248342484424854248642487424884248942490424914249242493424944249542496424974249842499425004250142502425034250442505425064250742508425094251042511425124251342514425154251642517425184251942520425214252242523425244252542526425274252842529425304253142532425334253442535425364253742538425394254042541425424254342544425454254642547425484254942550425514255242553425544255542556425574255842559425604256142562425634256442565425664256742568425694257042571425724257342574425754257642577425784257942580425814258242583425844258542586425874258842589425904259142592425934259442595425964259742598425994260042601426024260342604426054260642607426084260942610426114261242613426144261542616426174261842619426204262142622426234262442625426264262742628426294263042631426324263342634426354263642637426384263942640426414264242643426444264542646426474264842649426504265142652426534265442655426564265742658426594266042661426624266342664426654266642667426684266942670426714267242673426744267542676426774267842679426804268142682426834268442685426864268742688426894269042691426924269342694426954269642697426984269942700427014270242703427044270542706427074270842709427104271142712427134271442715427164271742718427194272042721427224272342724427254272642727427284272942730427314273242733427344273542736427374273842739427404274142742427434274442745427464274742748427494275042751427524275342754427554275642757427584275942760427614276242763427644276542766427674276842769427704277142772427734277442775427764277742778427794278042781427824278342784427854278642787427884278942790427914279242793427944279542796427974279842799428004280142802428034280442805428064280742808428094281042811428124281342814428154281642817428184281942820428214282242823428244282542826428274282842829428304283142832428334283442835428364283742838428394284042841428424284342844428454284642847428484284942850428514285242853428544285542856428574285842859428604286142862428634286442865428664286742868428694287042871428724287342874428754287642877428784287942880428814288242883428844288542886428874288842889428904289142892428934289442895428964289742898428994290042901429024290342904429054290642907429084290942910429114291242913429144291542916429174291842919429204292142922429234292442925429264292742928429294293042931429324293342934429354293642937429384293942940429414294242943429444294542946429474294842949429504295142952429534295442955429564295742958429594296042961429624296342964429654296642967429684296942970429714297242973429744297542976429774297842979429804298142982429834298442985429864298742988429894299042991429924299342994429954299642997429984299943000430014300243003430044300543006430074300843009430104301143012430134301443015430164301743018430194302043021430224302343024430254302643027430284302943030430314303243033430344303543036430374303843039430404304143042430434304443045430464304743048430494305043051430524305343054430554305643057430584305943060430614306243063430644306543066430674306843069430704307143072430734307443075430764307743078430794308043081430824308343084430854308643087430884308943090430914309243093430944309543096430974309843099431004310143102431034310443105431064310743108431094311043111431124311343114431154311643117431184311943120431214312243123431244312543126431274312843129431304313143132431334313443135431364313743138431394314043141431424314343144431454314643147431484314943150431514315243153431544315543156431574315843159431604316143162431634316443165431664316743168431694317043171431724317343174431754317643177431784317943180431814318243183431844318543186431874318843189431904319143192431934319443195431964319743198431994320043201432024320343204432054320643207432084320943210432114321243213432144321543216432174321843219432204322143222432234322443225432264322743228432294323043231432324323343234432354323643237432384323943240432414324243243432444324543246432474324843249432504325143252432534325443255432564325743258432594326043261432624326343264432654326643267432684326943270432714327243273432744327543276432774327843279432804328143282432834328443285432864328743288432894329043291432924329343294432954329643297432984329943300433014330243303433044330543306433074330843309433104331143312433134331443315433164331743318433194332043321433224332343324433254332643327433284332943330433314333243333433344333543336433374333843339433404334143342433434334443345433464334743348433494335043351433524335343354433554335643357433584335943360433614336243363433644336543366433674336843369433704337143372433734337443375433764337743378433794338043381433824338343384433854338643387433884338943390433914339243393433944339543396433974339843399434004340143402434034340443405434064340743408434094341043411434124341343414434154341643417434184341943420434214342243423434244342543426434274342843429434304343143432434334343443435434364343743438434394344043441434424344343444434454344643447434484344943450434514345243453434544345543456434574345843459434604346143462434634346443465434664346743468434694347043471434724347343474434754347643477434784347943480434814348243483434844348543486434874348843489434904349143492434934349443495434964349743498434994350043501435024350343504435054350643507435084350943510435114351243513435144351543516435174351843519435204352143522435234352443525435264352743528435294353043531435324353343534435354353643537435384353943540435414354243543435444354543546435474354843549435504355143552435534355443555435564355743558435594356043561435624356343564435654356643567435684356943570435714357243573435744357543576435774357843579435804358143582435834358443585435864358743588435894359043591435924359343594435954359643597435984359943600436014360243603436044360543606436074360843609436104361143612436134361443615436164361743618436194362043621436224362343624436254362643627436284362943630436314363243633436344363543636436374363843639436404364143642436434364443645436464364743648436494365043651436524365343654436554365643657436584365943660436614366243663436644366543666436674366843669436704367143672436734367443675436764367743678436794368043681436824368343684436854368643687436884368943690436914369243693436944369543696436974369843699437004370143702437034370443705437064370743708437094371043711437124371343714437154371643717437184371943720437214372243723437244372543726437274372843729437304373143732437334373443735437364373743738437394374043741437424374343744437454374643747437484374943750437514375243753437544375543756437574375843759437604376143762437634376443765437664376743768437694377043771437724377343774437754377643777437784377943780437814378243783437844378543786437874378843789437904379143792437934379443795437964379743798437994380043801438024380343804438054380643807438084380943810438114381243813438144381543816438174381843819438204382143822438234382443825438264382743828438294383043831438324383343834438354383643837438384383943840438414384243843438444384543846438474384843849438504385143852438534385443855438564385743858438594386043861438624386343864438654386643867438684386943870438714387243873438744387543876438774387843879438804388143882438834388443885438864388743888438894389043891438924389343894438954389643897438984389943900439014390243903439044390543906439074390843909439104391143912439134391443915439164391743918439194392043921439224392343924439254392643927439284392943930439314393243933439344393543936439374393843939439404394143942439434394443945439464394743948439494395043951439524395343954439554395643957439584395943960439614396243963439644396543966439674396843969439704397143972439734397443975439764397743978439794398043981439824398343984439854398643987439884398943990439914399243993439944399543996439974399843999440004400144002440034400444005440064400744008440094401044011440124401344014440154401644017440184401944020440214402244023440244402544026440274402844029440304403144032440334403444035440364403744038440394404044041440424404344044440454404644047440484404944050440514405244053440544405544056440574405844059440604406144062440634406444065440664406744068440694407044071440724407344074440754407644077440784407944080440814408244083440844408544086440874408844089440904409144092440934409444095440964409744098440994410044101441024410344104441054410644107441084410944110441114411244113441144411544116441174411844119441204412144122441234412444125441264412744128441294413044131441324413344134441354413644137441384413944140441414414244143441444414544146441474414844149441504415144152441534415444155441564415744158441594416044161441624416344164441654416644167441684416944170441714417244173441744417544176441774417844179441804418144182441834418444185441864418744188441894419044191441924419344194441954419644197441984419944200442014420244203442044420544206442074420844209442104421144212442134421444215442164421744218442194422044221442224422344224442254422644227442284422944230442314423244233442344423544236442374423844239442404424144242442434424444245442464424744248442494425044251442524425344254442554425644257442584425944260442614426244263442644426544266442674426844269442704427144272442734427444275442764427744278442794428044281442824428344284442854428644287442884428944290442914429244293442944429544296442974429844299443004430144302443034430444305443064430744308443094431044311443124431344314443154431644317443184431944320443214432244323443244432544326443274432844329443304433144332443334433444335443364433744338443394434044341443424434344344443454434644347443484434944350443514435244353443544435544356443574435844359443604436144362443634436444365443664436744368443694437044371443724437344374443754437644377443784437944380443814438244383443844438544386443874438844389443904439144392443934439444395443964439744398443994440044401444024440344404444054440644407444084440944410444114441244413444144441544416444174441844419444204442144422444234442444425444264442744428444294443044431444324443344434444354443644437444384443944440444414444244443444444444544446444474444844449444504445144452444534445444455444564445744458444594446044461444624446344464444654446644467444684446944470444714447244473444744447544476444774447844479444804448144482444834448444485444864448744488444894449044491444924449344494444954449644497444984449944500445014450244503445044450544506445074450844509445104451144512445134451444515445164451744518445194452044521445224452344524445254452644527445284452944530445314453244533445344453544536445374453844539445404454144542445434454444545445464454744548445494455044551445524455344554445554455644557445584455944560445614456244563445644456544566445674456844569445704457144572445734457444575445764457744578445794458044581
  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_HPKE
  242. #include <wolfssl/wolfcrypt/hpke.h>
  243. #endif
  244. #ifdef HAVE_CURVE25519
  245. #include <wolfssl/wolfcrypt/curve25519.h>
  246. #endif
  247. #ifdef HAVE_ED25519
  248. #include <wolfssl/wolfcrypt/ed25519.h>
  249. #endif
  250. #ifdef HAVE_CURVE448
  251. #include <wolfssl/wolfcrypt/curve448.h>
  252. #endif
  253. #ifdef HAVE_ED448
  254. #include <wolfssl/wolfcrypt/ed448.h>
  255. #endif
  256. #ifdef WOLFSSL_HAVE_KYBER
  257. #include <wolfssl/wolfcrypt/kyber.h>
  258. #ifdef WOLFSSL_WC_KYBER
  259. #include <wolfssl/wolfcrypt/wc_kyber.h>
  260. #endif
  261. #if defined(HAVE_LIBOQS) || defined(HAVE_PQM4)
  262. #include <wolfssl/wolfcrypt/ext_kyber.h>
  263. #endif
  264. #endif
  265. #ifdef WOLFCRYPT_HAVE_ECCSI
  266. #include <wolfssl/wolfcrypt/eccsi.h>
  267. #endif
  268. #ifdef WOLFCRYPT_HAVE_SAKKE
  269. #include <wolfssl/wolfcrypt/sakke.h>
  270. #endif
  271. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  272. #include <wolfssl/wolfcrypt/blake2.h>
  273. #endif
  274. #ifdef WOLFSSL_SHA3
  275. #include <wolfssl/wolfcrypt/sha3.h>
  276. #endif
  277. #ifdef HAVE_LIBZ
  278. #include <wolfssl/wolfcrypt/compress.h>
  279. #endif
  280. #ifdef HAVE_PKCS7
  281. #include <wolfssl/wolfcrypt/pkcs7.h>
  282. #endif
  283. #ifdef HAVE_FIPS
  284. #include <wolfssl/wolfcrypt/fips_test.h>
  285. #endif
  286. #ifdef HAVE_SELFTEST
  287. #include <wolfssl/wolfcrypt/selftest.h>
  288. #endif
  289. #ifdef WOLFSSL_ASYNC_CRYPT
  290. #include <wolfssl/wolfcrypt/async.h>
  291. #endif
  292. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  293. #include <wolfssl/wolfcrypt/logging.h>
  294. #endif
  295. #ifdef WOLFSSL_CAAM
  296. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  297. #endif
  298. #ifdef WOLF_CRYPTO_CB
  299. #include <wolfssl/wolfcrypt/cryptocb.h>
  300. #ifdef HAVE_INTEL_QA_SYNC
  301. #include <wolfssl/wolfcrypt/port/intel/quickassist_sync.h>
  302. #endif
  303. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  304. #include <wolfssl/wolfcrypt/port/cavium/cavium_octeon_sync.h>
  305. #endif
  306. #endif
  307. #ifdef _MSC_VER
  308. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  309. #pragma warning(disable: 4996)
  310. #endif
  311. #ifdef OPENSSL_EXTRA
  312. #ifndef WOLFCRYPT_ONLY
  313. #include <wolfssl/openssl/evp.h>
  314. #include <wolfssl/openssl/hmac.h>
  315. #endif
  316. #include <wolfssl/openssl/rand.h>
  317. #include <wolfssl/openssl/aes.h>
  318. #include <wolfssl/openssl/des.h>
  319. #endif
  320. #if defined(NO_FILESYSTEM) || defined(WC_NO_RNG)
  321. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  322. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  323. #define USE_CERT_BUFFERS_2048
  324. #endif
  325. #if !defined(USE_CERT_BUFFERS_256)
  326. #define USE_CERT_BUFFERS_256
  327. #endif
  328. #endif
  329. #if defined(WOLFSSL_CERT_GEN) && (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES))
  330. #define ENABLE_ECC384_CERT_GEN_TEST
  331. #endif
  332. #include <wolfssl/certs_test.h>
  333. #ifdef DEVKITPRO
  334. #include <wiiuse/wpad.h>
  335. #endif
  336. #ifdef WOLFSSL_STATIC_MEMORY
  337. static WOLFSSL_HEAP_HINT* HEAP_HINT;
  338. #else
  339. #define HEAP_HINT NULL
  340. #endif /* WOLFSSL_STATIC_MEMORY */
  341. /* these cases do not have intermediate hashing support */
  342. #if (defined(WOLFSSL_AFALG_XILINX_SHA3) && !defined(WOLFSSL_AFALG_HASH_KEEP)) \
  343. && !defined(WOLFSSL_XILINX_CRYPT) || defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  344. #define NO_INTM_HASH_TEST
  345. #endif
  346. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  347. defined(WOLFSSL_SECO_CAAM)
  348. #define HASH_SIZE_LIMIT
  349. #endif
  350. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  351. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  352. static void initDefaultName(void);
  353. #endif
  354. /* for async devices */
  355. #ifdef WOLFSSL_CAAM_DEVID
  356. static int devId = WOLFSSL_CAAM_DEVID;
  357. #else
  358. static int devId = INVALID_DEVID;
  359. #endif
  360. #ifdef HAVE_WNR
  361. const char* wnrConfigFile = "wnr-example.conf";
  362. #endif
  363. #define TEST_STRING "Everyone gets Friday off."
  364. #define TEST_STRING_SZ 25
  365. typedef struct testVector {
  366. const char* input;
  367. const char* output;
  368. size_t inLen;
  369. size_t outLen;
  370. } testVector;
  371. #ifndef WOLFSSL_TEST_SUBROUTINE
  372. #define WOLFSSL_TEST_SUBROUTINE
  373. #endif
  374. PRAGMA_GCC("GCC diagnostic ignored \"-Wunused-function\"")
  375. PRAGMA_CLANG("clang diagnostic ignored \"-Wunused-function\"")
  376. WOLFSSL_TEST_SUBROUTINE int error_test(void);
  377. WOLFSSL_TEST_SUBROUTINE int base64_test(void);
  378. WOLFSSL_TEST_SUBROUTINE int base16_test(void);
  379. WOLFSSL_TEST_SUBROUTINE int asn_test(void);
  380. WOLFSSL_TEST_SUBROUTINE int md2_test(void);
  381. WOLFSSL_TEST_SUBROUTINE int md5_test(void);
  382. WOLFSSL_TEST_SUBROUTINE int md4_test(void);
  383. WOLFSSL_TEST_SUBROUTINE int sha_test(void);
  384. WOLFSSL_TEST_SUBROUTINE int sha224_test(void);
  385. WOLFSSL_TEST_SUBROUTINE int sha256_test(void);
  386. WOLFSSL_TEST_SUBROUTINE int sha512_test(void);
  387. WOLFSSL_TEST_SUBROUTINE int sha384_test(void);
  388. WOLFSSL_TEST_SUBROUTINE int sha3_test(void);
  389. WOLFSSL_TEST_SUBROUTINE int shake128_test(void);
  390. WOLFSSL_TEST_SUBROUTINE int shake256_test(void);
  391. WOLFSSL_TEST_SUBROUTINE int hash_test(void);
  392. WOLFSSL_TEST_SUBROUTINE int hmac_md5_test(void);
  393. WOLFSSL_TEST_SUBROUTINE int hmac_sha_test(void);
  394. WOLFSSL_TEST_SUBROUTINE int hmac_sha224_test(void);
  395. WOLFSSL_TEST_SUBROUTINE int hmac_sha256_test(void);
  396. WOLFSSL_TEST_SUBROUTINE int hmac_sha384_test(void);
  397. WOLFSSL_TEST_SUBROUTINE int hmac_sha512_test(void);
  398. WOLFSSL_TEST_SUBROUTINE int hmac_sha3_test(void);
  399. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  400. /* hkdf_test has issue with WOLFSSL_TEST_SUBROUTINE set on Xilinx with afalg */
  401. static int hkdf_test(void);
  402. #endif
  403. WOLFSSL_TEST_SUBROUTINE int sshkdf_test(void);
  404. #ifdef WOLFSSL_TLS13
  405. WOLFSSL_TEST_SUBROUTINE int tls13_kdf_test(void);
  406. #endif
  407. WOLFSSL_TEST_SUBROUTINE int x963kdf_test(void);
  408. WOLFSSL_TEST_SUBROUTINE int hpke_test(void);
  409. WOLFSSL_TEST_SUBROUTINE int arc4_test(void);
  410. #ifdef WC_RC2
  411. WOLFSSL_TEST_SUBROUTINE int rc2_test(void);
  412. #endif
  413. WOLFSSL_TEST_SUBROUTINE int chacha_test(void);
  414. WOLFSSL_TEST_SUBROUTINE int XChaCha_test(void);
  415. WOLFSSL_TEST_SUBROUTINE int chacha20_poly1305_aead_test(void);
  416. WOLFSSL_TEST_SUBROUTINE int XChaCha20Poly1305_test(void);
  417. WOLFSSL_TEST_SUBROUTINE int des_test(void);
  418. WOLFSSL_TEST_SUBROUTINE int des3_test(void);
  419. WOLFSSL_TEST_SUBROUTINE int aes_test(void);
  420. WOLFSSL_TEST_SUBROUTINE int aes192_test(void);
  421. WOLFSSL_TEST_SUBROUTINE int aes256_test(void);
  422. WOLFSSL_TEST_SUBROUTINE int aesofb_test(void);
  423. WOLFSSL_TEST_SUBROUTINE int cmac_test(void);
  424. #if defined(WOLFSSL_SIPHASH)
  425. WOLFSSL_TEST_SUBROUTINE int siphash_test(void);
  426. #endif
  427. WOLFSSL_TEST_SUBROUTINE int poly1305_test(void);
  428. WOLFSSL_TEST_SUBROUTINE int aesgcm_test(void);
  429. WOLFSSL_TEST_SUBROUTINE int aesgcm_default_test(void);
  430. WOLFSSL_TEST_SUBROUTINE int gmac_test(void);
  431. WOLFSSL_TEST_SUBROUTINE int aesccm_test(void);
  432. WOLFSSL_TEST_SUBROUTINE int aeskeywrap_test(void);
  433. WOLFSSL_TEST_SUBROUTINE int camellia_test(void);
  434. #ifdef WC_RSA_NO_PADDING
  435. WOLFSSL_TEST_SUBROUTINE int rsa_no_pad_test(void);
  436. #endif
  437. WOLFSSL_TEST_SUBROUTINE int rsa_test(void);
  438. WOLFSSL_TEST_SUBROUTINE int dh_test(void);
  439. WOLFSSL_TEST_SUBROUTINE int dsa_test(void);
  440. WOLFSSL_TEST_SUBROUTINE int srp_test(void);
  441. #ifndef WC_NO_RNG
  442. WOLFSSL_TEST_SUBROUTINE int random_test(void);
  443. #endif /* WC_NO_RNG */
  444. WOLFSSL_TEST_SUBROUTINE int pwdbased_test(void);
  445. WOLFSSL_TEST_SUBROUTINE int ripemd_test(void);
  446. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  447. WOLFSSL_TEST_SUBROUTINE int openssl_test(void); /* test mini api */
  448. WOLFSSL_TEST_SUBROUTINE int openssl_pkey0_test(void);
  449. WOLFSSL_TEST_SUBROUTINE int openssl_pkey1_test(void);
  450. WOLFSSL_TEST_SUBROUTINE int openSSL_evpMD_test(void);
  451. WOLFSSL_TEST_SUBROUTINE int openssl_evpSig_test(void);
  452. #endif
  453. WOLFSSL_TEST_SUBROUTINE int pbkdf1_test(void);
  454. WOLFSSL_TEST_SUBROUTINE int pkcs12_test(void);
  455. WOLFSSL_TEST_SUBROUTINE int pbkdf2_test(void);
  456. WOLFSSL_TEST_SUBROUTINE int scrypt_test(void);
  457. #ifdef HAVE_ECC
  458. WOLFSSL_TEST_SUBROUTINE int ecc_test(void);
  459. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  460. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  461. WOLFSSL_TEST_SUBROUTINE int ecc_encrypt_test(void);
  462. #endif
  463. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  464. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  465. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  466. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  467. /* skip for ATECC508/608A, cannot import private key buffers */
  468. WOLFSSL_TEST_SUBROUTINE int ecc_test_buffers(void);
  469. #endif
  470. #endif
  471. #ifdef HAVE_CURVE25519
  472. WOLFSSL_TEST_SUBROUTINE int curve25519_test(void);
  473. #endif
  474. #ifdef HAVE_ED25519
  475. WOLFSSL_TEST_SUBROUTINE int ed25519_test(void);
  476. #endif
  477. #ifdef HAVE_CURVE448
  478. WOLFSSL_TEST_SUBROUTINE int curve448_test(void);
  479. #endif
  480. #ifdef HAVE_ED448
  481. WOLFSSL_TEST_SUBROUTINE int ed448_test(void);
  482. #endif
  483. #ifdef WOLFSSL_HAVE_KYBER
  484. WOLFSSL_TEST_SUBROUTINE int kyber_test(void);
  485. #endif
  486. #ifdef WOLFCRYPT_HAVE_ECCSI
  487. WOLFSSL_TEST_SUBROUTINE int eccsi_test(void);
  488. #endif
  489. #ifdef WOLFCRYPT_HAVE_SAKKE
  490. WOLFSSL_TEST_SUBROUTINE int sakke_test(void);
  491. #endif
  492. #ifdef HAVE_BLAKE2
  493. WOLFSSL_TEST_SUBROUTINE int blake2b_test(void);
  494. #endif
  495. #ifdef HAVE_BLAKE2S
  496. WOLFSSL_TEST_SUBROUTINE int blake2s_test(void);
  497. #endif
  498. #ifdef HAVE_LIBZ
  499. WOLFSSL_TEST_SUBROUTINE int compress_test(void);
  500. #endif
  501. #ifdef HAVE_PKCS7
  502. #ifndef NO_PKCS7_ENCRYPTED_DATA
  503. WOLFSSL_TEST_SUBROUTINE int pkcs7encrypted_test(void);
  504. #endif
  505. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  506. WOLFSSL_TEST_SUBROUTINE int pkcs7compressed_test(void);
  507. #endif
  508. WOLFSSL_TEST_SUBROUTINE int pkcs7signed_test(void);
  509. WOLFSSL_TEST_SUBROUTINE int pkcs7enveloped_test(void);
  510. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  511. WOLFSSL_TEST_SUBROUTINE int pkcs7authenveloped_test(void);
  512. #endif
  513. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  514. WOLFSSL_TEST_SUBROUTINE int pkcs7callback_test(byte* cert, word32 certSz, byte* key,
  515. word32 keySz);
  516. #endif
  517. #endif
  518. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  519. !defined(NO_FILESYSTEM)
  520. WOLFSSL_TEST_SUBROUTINE int cert_test(void);
  521. #endif
  522. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  523. !defined(NO_FILESYSTEM) && defined(WOLFSSL_CERT_GEN)
  524. WOLFSSL_TEST_SUBROUTINE int certext_test(void);
  525. #endif
  526. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  527. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  528. WOLFSSL_TEST_SUBROUTINE int decodedCertCache_test(void);
  529. #endif
  530. WOLFSSL_TEST_SUBROUTINE int memory_test(void);
  531. #ifdef HAVE_VALGRIND
  532. WOLFSSL_TEST_SUBROUTINE int mp_test(void);
  533. #endif
  534. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  535. WOLFSSL_TEST_SUBROUTINE int prime_test(void);
  536. #endif
  537. #if defined(ASN_BER_TO_DER) && \
  538. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  539. defined(OPENSSL_EXTRA_X509_SMALL))
  540. WOLFSSL_TEST_SUBROUTINE int berder_test(void);
  541. #endif
  542. WOLFSSL_TEST_SUBROUTINE int logging_test(void);
  543. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  544. WOLFSSL_TEST_SUBROUTINE int time_test(void);
  545. #endif
  546. #if defined(__INCLUDE_NUTTX_CONFIG_H)
  547. WOLFSSL_TEST_SUBROUTINE int wolfcrypt_mutex_test(void);
  548. #else
  549. WOLFSSL_TEST_SUBROUTINE int mutex_test(void);
  550. #endif
  551. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  552. WOLFSSL_TEST_SUBROUTINE int memcb_test(void);
  553. #endif
  554. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  555. WOLFSSL_TEST_SUBROUTINE int blob_test(void);
  556. #endif
  557. #ifdef WOLF_CRYPTO_CB
  558. WOLFSSL_TEST_SUBROUTINE int cryptocb_test(void);
  559. #endif
  560. #ifdef WOLFSSL_CERT_PIV
  561. WOLFSSL_TEST_SUBROUTINE int certpiv_test(void);
  562. #endif
  563. #ifdef WOLFSSL_AES_SIV
  564. WOLFSSL_TEST_SUBROUTINE int aes_siv_test(void);
  565. #endif
  566. /* General big buffer size for many tests. */
  567. #define FOURK_BUF 4096
  568. #define ERROR_OUT(err, eLabel) do { ret = (err); goto eLabel; } while (0)
  569. #ifdef HAVE_STACK_SIZE
  570. static THREAD_RETURN err_sys(const char* msg, int es)
  571. #else
  572. static int err_sys(const char* msg, int es)
  573. #endif
  574. {
  575. (void)msg;
  576. (void)es;
  577. #ifdef WOLFSSL_LINUXKM
  578. lkm_printf("%s error = %d\n", msg, es);
  579. EXIT_TEST(es);
  580. #else
  581. printf("%s error = %d\n", msg, es);
  582. EXIT_TEST(-1);
  583. #endif
  584. }
  585. #ifndef HAVE_WOLFCRYPT_TEST_OPTIONS
  586. /* func_args from test.h, so don't have to pull in other stuff */
  587. typedef struct func_args {
  588. int argc;
  589. char** argv;
  590. int return_code;
  591. } func_args;
  592. #endif /* !HAVE_WOLFCRYPT_TEST_OPTIONS */
  593. #if defined(HAVE_FIPS) && !defined(WOLFSSL_LINUXKM)
  594. static void myFipsCb(int ok, int err, const char* hash)
  595. {
  596. printf("in my Fips callback, ok = %d, err = %d\n", ok, err);
  597. printf("message = %s\n", wc_GetErrorString(err));
  598. printf("hash = %s\n", hash);
  599. if (err == IN_CORE_FIPS_E) {
  600. printf("In core integrity hash check failure, copy above hash\n");
  601. printf("into verifyCore[] in fips_test.c and rebuild\n");
  602. }
  603. }
  604. #endif /* HAVE_FIPS && !WOLFSSL_LINUXKM */
  605. #ifdef WOLFSSL_STATIC_MEMORY
  606. #if defined(WOLFSSL_STATIC_MEMORY_TEST_SZ)
  607. static byte gTestMemory[WOLFSSL_STATIC_MEMORY_TEST_SZ];
  608. #elif defined(BENCH_EMBEDDED)
  609. static byte gTestMemory[14000];
  610. #elif defined(WOLFSSL_CERT_EXT)
  611. static byte gTestMemory[140000];
  612. #elif (defined(WOLFSSL_SP_MATH_ALL) || defined(USE_FAST_MATH)) && \
  613. !defined(ALT_ECC_SIZE)
  614. static byte gTestMemory[160000];
  615. #else
  616. static byte gTestMemory[80000];
  617. #endif
  618. #endif
  619. #ifdef WOLFSSL_PB
  620. static int wolfssl_pb_print(const char* msg, ...)
  621. {
  622. int ret;
  623. va_list args;
  624. char tmpBuf[80];
  625. va_start(args, msg);
  626. ret = vsprint(tmpBuf, msg, args);
  627. va_end(args);
  628. fnDumpStringToSystemLog(tmpBuf);
  629. return ret;
  630. }
  631. #endif /* WOLFSSL_PB */
  632. /* optional macro to add sleep between tests */
  633. #ifndef TEST_SLEEP
  634. #define TEST_SLEEP()
  635. #else
  636. #define TEST_PASS test_pass
  637. #include <stdarg.h> /* for var args */
  638. static WC_INLINE void test_pass(const char* fmt, ...)
  639. {
  640. va_list args;
  641. va_start(args, fmt);
  642. STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK(max_relative_stack, vprintf(fmt, args));
  643. va_end(args);
  644. PRINT_HEAP_CHECKPOINT();
  645. TEST_SLEEP();
  646. ASSERT_RESTORED_VECTOR_REGISTERS(exit(1););
  647. }
  648. #endif
  649. /* set test pass output to printf if not overriden */
  650. #ifndef TEST_PASS
  651. /* redirect to printf */
  652. #define TEST_PASS(...) { \
  653. if (STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK \
  654. (max_relative_stack, printf(__VA_ARGS__)) < 0) { \
  655. return err_sys("post-test check failed", -1); \
  656. } \
  657. PRINT_HEAP_CHECKPOINT(); \
  658. ASSERT_RESTORED_VECTOR_REGISTERS(exit(1);); \
  659. }
  660. #endif
  661. #ifdef HAVE_STACK_SIZE
  662. THREAD_RETURN WOLFSSL_THREAD wolfcrypt_test(void* args)
  663. #else
  664. int wolfcrypt_test(void* args)
  665. #endif
  666. {
  667. int ret;
  668. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  669. long heap_baselineAllocs, heap_baselineBytes;
  670. #endif
  671. STACK_SIZE_INIT();
  672. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  673. (void)wolfCrypt_heap_peakAllocs_checkpoint();
  674. heap_baselineAllocs = wolfCrypt_heap_peakAllocs_checkpoint();
  675. (void)wolfCrypt_heap_peakBytes_checkpoint();
  676. heap_baselineBytes = wolfCrypt_heap_peakBytes_checkpoint();
  677. #endif
  678. printf("------------------------------------------------------------------------------\n");
  679. printf(" wolfSSL version %s\n", LIBWOLFSSL_VERSION_STRING);
  680. printf("------------------------------------------------------------------------------\n");
  681. if (args) {
  682. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  683. int ch;
  684. #endif
  685. ((func_args*)args)->return_code = -1; /* error state */
  686. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  687. while ((ch = mygetopt(((func_args*)args)->argc, ((func_args*)args)->argv, "s:m:a:h")) != -1) {
  688. switch(ch) {
  689. case 's':
  690. #ifdef HAVE_STACK_SIZE_VERBOSE
  691. max_relative_stack = (ssize_t)atoi(myoptarg);
  692. break;
  693. #else
  694. return err_sys("-s (max relative stack bytes) requires HAVE_STACK_SIZE_VERBOSE (--enable-stacksize=verbose).", -1);
  695. #endif
  696. case 'm':
  697. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  698. max_relative_heap_bytes = (ssize_t)atoi(myoptarg);
  699. break;
  700. #else
  701. return err_sys("-m (max relative heap memory bytes) requires WOLFSSL_TRACK_MEMORY_VERBOSE (--enable-trackmemory=verbose).", -1);
  702. #endif
  703. case 'a':
  704. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  705. max_relative_heap_allocs = (ssize_t)atoi(myoptarg);
  706. break;
  707. #else
  708. return err_sys("-a (max relative heap allocs) requires WOLFSSL_TRACK_MEMORY_VERBOSE (--enable-trackmemory=verbose).", -1);
  709. #endif
  710. case 'h':
  711. return err_sys("\
  712. options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\
  713. [-a max_relative_heap_allocs] [-h]\n", 0);
  714. default:
  715. return err_sys("unknown test option. try -h.", -1);
  716. }
  717. }
  718. #endif
  719. }
  720. #ifdef WOLFSSL_STATIC_MEMORY
  721. if (wc_LoadStaticMemory(&HEAP_HINT, gTestMemory, sizeof(gTestMemory),
  722. WOLFMEM_GENERAL, 1) != 0) {
  723. printf("unable to load static memory.\n");
  724. return(EXIT_FAILURE);
  725. }
  726. #endif
  727. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
  728. wolfSSL_Debugging_ON();
  729. #endif
  730. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  731. wc_SetLoggingHeap(HEAP_HINT);
  732. #endif
  733. #if defined(HAVE_FIPS) && !defined(WOLFSSL_LINUXKM)
  734. wolfCrypt_SetCb_fips(myFipsCb);
  735. #endif
  736. #if !defined(NO_BIG_INT)
  737. if (CheckCtcSettings() != 1) {
  738. printf("Sizeof mismatch (build) %x != (run) %lx\n",
  739. CTC_SETTINGS, (unsigned long)CheckRunTimeSettings());
  740. return err_sys("Build vs runtime math mismatch\n", -1000);
  741. }
  742. #if defined(USE_FAST_MATH) && \
  743. (!defined(NO_RSA) || !defined(NO_DH) || defined(HAVE_ECC))
  744. if (CheckFastMathSettings() != 1)
  745. return err_sys("Build vs runtime fastmath FP_MAX_BITS mismatch\n",
  746. -1001);
  747. #endif /* USE_FAST_MATH */
  748. #endif /* !NO_BIG_INT */
  749. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  750. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  751. initDefaultName();
  752. #endif
  753. #ifdef WOLFSSL_ASYNC_CRYPT
  754. ret = wolfAsync_DevOpen(&devId);
  755. if (ret < 0) {
  756. printf("Async device open failed\nRunning without async\n");
  757. }
  758. #else
  759. (void)devId;
  760. #endif /* WOLFSSL_ASYNC_CRYPT */
  761. #ifdef WOLF_CRYPTO_CB
  762. #ifdef HAVE_INTEL_QA_SYNC
  763. devId = wc_CryptoCb_InitIntelQa();
  764. if (INVALID_DEVID == devId) {
  765. printf("Couldn't init the Intel QA\n");
  766. }
  767. #endif
  768. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  769. devId = wc_CryptoCb_InitOcteon();
  770. if (INVALID_DEVID == devId) {
  771. printf("Couldn't init the Cavium Octeon\n");
  772. }
  773. #endif
  774. #endif
  775. #ifdef HAVE_SELFTEST
  776. if ( (ret = wolfCrypt_SelfTest()) != 0)
  777. return err_sys("CAVP selftest failed!\n", ret);
  778. else
  779. TEST_PASS("CAVP selftest passed!\n");
  780. #endif
  781. if ( (ret = error_test()) != 0)
  782. return err_sys("error test failed!\n", ret);
  783. else
  784. TEST_PASS("error test passed!\n");
  785. if ( (ret = memory_test()) != 0)
  786. return err_sys("MEMORY test failed!\n", ret);
  787. else
  788. TEST_PASS("MEMORY test passed!\n");
  789. #ifndef NO_CODING
  790. if ( (ret = base64_test()) != 0)
  791. return err_sys("base64 test failed!\n", ret);
  792. else
  793. TEST_PASS("base64 test passed!\n");
  794. #ifdef WOLFSSL_BASE16
  795. if ( (ret = base16_test()) != 0)
  796. return err_sys("base16 test failed!\n", ret);
  797. else
  798. TEST_PASS("base16 test passed!\n");
  799. #endif
  800. #endif /* !NO_CODING */
  801. #ifndef NO_ASN
  802. if ( (ret = asn_test()) != 0)
  803. return err_sys("asn test failed!\n", ret);
  804. else
  805. TEST_PASS("asn test passed!\n");
  806. #endif
  807. #ifndef WC_NO_RNG
  808. if ( (ret = random_test()) != 0)
  809. return err_sys("RANDOM test failed!\n", ret);
  810. else
  811. TEST_PASS("RANDOM test passed!\n");
  812. #endif /* WC_NO_RNG */
  813. #ifndef NO_MD5
  814. if ( (ret = md5_test()) != 0)
  815. return err_sys("MD5 test failed!\n", ret);
  816. else
  817. TEST_PASS("MD5 test passed!\n");
  818. #endif
  819. #ifdef WOLFSSL_MD2
  820. if ( (ret = md2_test()) != 0)
  821. return err_sys("MD2 test failed!\n", ret);
  822. else
  823. TEST_PASS("MD2 test passed!\n");
  824. #endif
  825. #ifndef NO_MD4
  826. if ( (ret = md4_test()) != 0)
  827. return err_sys("MD4 test failed!\n", ret);
  828. else
  829. TEST_PASS("MD4 test passed!\n");
  830. #endif
  831. #ifndef NO_SHA
  832. if ( (ret = sha_test()) != 0)
  833. return err_sys("SHA test failed!\n", ret);
  834. else
  835. TEST_PASS("SHA test passed!\n");
  836. #endif
  837. #ifdef WOLFSSL_SHA224
  838. if ( (ret = sha224_test()) != 0)
  839. return err_sys("SHA-224 test failed!\n", ret);
  840. else
  841. TEST_PASS("SHA-224 test passed!\n");
  842. #endif
  843. #ifndef NO_SHA256
  844. if ( (ret = sha256_test()) != 0)
  845. return err_sys("SHA-256 test failed!\n", ret);
  846. else
  847. TEST_PASS("SHA-256 test passed!\n");
  848. #endif
  849. #ifdef WOLFSSL_SHA384
  850. if ( (ret = sha384_test()) != 0)
  851. return err_sys("SHA-384 test failed!\n", ret);
  852. else
  853. TEST_PASS("SHA-384 test passed!\n");
  854. #endif
  855. #ifdef WOLFSSL_SHA512
  856. if ( (ret = sha512_test()) != 0)
  857. return err_sys("SHA-512 test failed!\n", ret);
  858. else
  859. TEST_PASS("SHA-512 test passed!\n");
  860. #endif
  861. #ifdef WOLFSSL_SHA3
  862. if ( (ret = sha3_test()) != 0)
  863. return err_sys("SHA-3 test failed!\n", ret);
  864. else
  865. TEST_PASS("SHA-3 test passed!\n");
  866. #endif
  867. #ifdef WOLFSSL_SHAKE128
  868. if ( (ret = shake128_test()) != 0)
  869. return err_sys("SHAKE128 test failed!\n", ret);
  870. else
  871. TEST_PASS("SHAKE128 test passed!\n");
  872. #endif
  873. #ifdef WOLFSSL_SHAKE256
  874. if ( (ret = shake256_test()) != 0)
  875. return err_sys("SHAKE256 test failed!\n", ret);
  876. else
  877. TEST_PASS("SHAKE256 test passed!\n");
  878. #endif
  879. #ifndef NO_HASH_WRAPPER
  880. if ( (ret = hash_test()) != 0)
  881. return err_sys("Hash test failed!\n", ret);
  882. else
  883. TEST_PASS("Hash test passed!\n");
  884. #endif
  885. #ifdef WOLFSSL_RIPEMD
  886. if ( (ret = ripemd_test()) != 0)
  887. return err_sys("RIPEMD test failed!\n", ret);
  888. else
  889. TEST_PASS("RIPEMD test passed!\n");
  890. #endif
  891. #ifdef HAVE_BLAKE2
  892. if ( (ret = blake2b_test()) != 0)
  893. return err_sys("BLAKE2b test failed!\n", ret);
  894. else
  895. TEST_PASS("BLAKE2b test passed!\n");
  896. #endif
  897. #ifdef HAVE_BLAKE2S
  898. if ( (ret = blake2s_test()) != 0)
  899. return err_sys("BLAKE2s test failed!\n", ret);
  900. else
  901. TEST_PASS("BLAKE2s test passed!\n");
  902. #endif
  903. #ifndef NO_HMAC
  904. #if !defined(NO_MD5) && !(defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) \
  905. && (HAVE_FIPS_VERSION >= 5))
  906. if ( (ret = hmac_md5_test()) != 0)
  907. return err_sys("HMAC-MD5 test failed!\n", ret);
  908. else
  909. TEST_PASS("HMAC-MD5 test passed!\n");
  910. #endif
  911. #ifndef NO_SHA
  912. if ( (ret = hmac_sha_test()) != 0)
  913. return err_sys("HMAC-SHA test failed!\n", ret);
  914. else
  915. TEST_PASS("HMAC-SHA test passed!\n");
  916. #endif
  917. #ifdef WOLFSSL_SHA224
  918. if ( (ret = hmac_sha224_test()) != 0)
  919. return err_sys("HMAC-SHA224 test failed!\n", ret);
  920. else
  921. TEST_PASS("HMAC-SHA224 test passed!\n");
  922. #endif
  923. #ifndef NO_SHA256
  924. if ( (ret = hmac_sha256_test()) != 0)
  925. return err_sys("HMAC-SHA256 test failed!\n", ret);
  926. else
  927. TEST_PASS("HMAC-SHA256 test passed!\n");
  928. #endif
  929. #ifdef WOLFSSL_SHA384
  930. if ( (ret = hmac_sha384_test()) != 0)
  931. return err_sys("HMAC-SHA384 test failed!\n", ret);
  932. else
  933. TEST_PASS("HMAC-SHA384 test passed!\n");
  934. #endif
  935. #ifdef WOLFSSL_SHA512
  936. if ( (ret = hmac_sha512_test()) != 0)
  937. return err_sys("HMAC-SHA512 test failed!\n", ret);
  938. else
  939. TEST_PASS("HMAC-SHA512 test passed!\n");
  940. #endif
  941. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  942. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  943. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  944. if ( (ret = hmac_sha3_test()) != 0)
  945. return err_sys("HMAC-SHA3 test failed!\n", ret);
  946. else
  947. TEST_PASS("HMAC-SHA3 test passed!\n");
  948. #endif
  949. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  950. PRIVATE_KEY_UNLOCK();
  951. if ( (ret = hkdf_test()) != 0)
  952. return err_sys("HMAC-KDF test failed!\n", ret);
  953. else
  954. TEST_PASS("HMAC-KDF test passed!\n");
  955. PRIVATE_KEY_LOCK();
  956. #endif
  957. #endif /* !NO_HMAC */
  958. #ifdef WOLFSSL_WOLFSSH
  959. PRIVATE_KEY_UNLOCK();
  960. if ( (ret = sshkdf_test()) != 0)
  961. return err_sys("SSH-KDF test failed!\n", ret);
  962. else
  963. TEST_PASS("SSH-KDF test passed!\n");
  964. PRIVATE_KEY_LOCK();
  965. #endif /* WOLFSSL_WOLFSSH */
  966. #ifdef WOLFSSL_TLS13
  967. PRIVATE_KEY_UNLOCK();
  968. if ( (ret = tls13_kdf_test()) != 0)
  969. return err_sys("TLSv1.3 KDF test failed!\n", ret);
  970. else
  971. TEST_PASS("TLSv1.3 KDF test passed!\n");
  972. PRIVATE_KEY_LOCK();
  973. #endif /* WOLFSSL_TLS13 */
  974. #if defined(HAVE_X963_KDF) && defined(HAVE_ECC)
  975. if ( (ret = x963kdf_test()) != 0)
  976. return err_sys("X963-KDF test failed!\n", ret);
  977. else
  978. TEST_PASS("X963-KDF test passed!\n");
  979. #endif
  980. #if defined(HAVE_HPKE) && defined(HAVE_ECC) && defined(HAVE_AESGCM)
  981. if ( (ret = hpke_test()) != 0)
  982. return err_sys("HPKE test failed!\n", ret);
  983. else
  984. TEST_PASS("HPKE test passed!\n");
  985. #endif
  986. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128) && \
  987. !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  988. if ( (ret = gmac_test()) != 0)
  989. return err_sys("GMAC test failed!\n", ret);
  990. else
  991. TEST_PASS("GMAC test passed!\n");
  992. #endif
  993. #ifdef WC_RC2
  994. if ( (ret = rc2_test()) != 0)
  995. return err_sys("RC2 test failed!\n", ret);
  996. else
  997. TEST_PASS("RC2 test passed!\n");
  998. #endif
  999. #ifndef NO_RC4
  1000. if ( (ret = arc4_test()) != 0)
  1001. return err_sys("ARC4 test failed!\n", ret);
  1002. else
  1003. TEST_PASS("ARC4 test passed!\n");
  1004. #endif
  1005. #ifdef HAVE_CHACHA
  1006. if ( (ret = chacha_test()) != 0)
  1007. return err_sys("Chacha test failed!\n", ret);
  1008. else
  1009. TEST_PASS("Chacha test passed!\n");
  1010. #endif
  1011. #ifdef HAVE_XCHACHA
  1012. if ( (ret = XChaCha_test()) != 0)
  1013. return err_sys("XChacha test failed!\n", ret);
  1014. else
  1015. TEST_PASS("XChacha test passed!\n");
  1016. #endif
  1017. #ifdef HAVE_POLY1305
  1018. if ( (ret = poly1305_test()) != 0)
  1019. return err_sys("POLY1305 test failed!\n", ret);
  1020. else
  1021. TEST_PASS("POLY1305 test passed!\n");
  1022. #endif
  1023. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  1024. if ( (ret = chacha20_poly1305_aead_test()) != 0)
  1025. return err_sys("ChaCha20-Poly1305 AEAD test failed!\n", ret);
  1026. else
  1027. TEST_PASS("ChaCha20-Poly1305 AEAD test passed!\n");
  1028. #endif
  1029. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  1030. if ( (ret = XChaCha20Poly1305_test()) != 0)
  1031. return err_sys("XChaCha20-Poly1305 AEAD test failed!\n", ret);
  1032. else
  1033. TEST_PASS("XChaCha20-Poly1305 AEAD test passed!\n");
  1034. #endif
  1035. #ifndef NO_DES3
  1036. if ( (ret = des_test()) != 0)
  1037. return err_sys("DES test failed!\n", ret);
  1038. else
  1039. TEST_PASS("DES test passed!\n");
  1040. #endif
  1041. #ifndef NO_DES3
  1042. if ( (ret = des3_test()) != 0)
  1043. return err_sys("DES3 test failed!\n", ret);
  1044. else
  1045. TEST_PASS("DES3 test passed!\n");
  1046. #endif
  1047. #ifndef NO_AES
  1048. if ( (ret = aes_test()) != 0)
  1049. return err_sys("AES test failed!\n", ret);
  1050. else
  1051. TEST_PASS("AES test passed!\n");
  1052. #ifdef WOLFSSL_AES_192
  1053. if ( (ret = aes192_test()) != 0)
  1054. return err_sys("AES192 test failed!\n", ret);
  1055. else
  1056. TEST_PASS("AES192 test passed!\n");
  1057. #endif
  1058. #ifdef WOLFSSL_AES_256
  1059. if ( (ret = aes256_test()) != 0)
  1060. return err_sys("AES256 test failed!\n", ret);
  1061. else
  1062. TEST_PASS("AES256 test passed!\n");
  1063. #endif
  1064. #ifdef WOLFSSL_AES_OFB
  1065. if ( (ret = aesofb_test()) != 0)
  1066. return err_sys("AES-OFB test failed!\n", ret);
  1067. else
  1068. TEST_PASS("AESOFB test passed!\n");
  1069. #endif
  1070. #ifdef HAVE_AESGCM
  1071. #if !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO)
  1072. if ( (ret = aesgcm_test()) != 0)
  1073. return err_sys("AES-GCM test failed!\n", ret);
  1074. #endif
  1075. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT) && \
  1076. !defined(WOLFSSL_KCAPI_AES) && !(defined(WOLF_CRYPTO_CB) && \
  1077. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  1078. if ((ret = aesgcm_default_test()) != 0) {
  1079. return err_sys("AES-GCM test failed!\n", ret);
  1080. }
  1081. #endif
  1082. if (ret == 0) {
  1083. TEST_PASS("AES-GCM test passed!\n");
  1084. }
  1085. #endif
  1086. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  1087. if ( (ret = aesccm_test()) != 0)
  1088. return err_sys("AES-CCM test failed!\n", ret);
  1089. else
  1090. TEST_PASS("AES-CCM test passed!\n");
  1091. #endif
  1092. #ifdef HAVE_AES_KEYWRAP
  1093. if ( (ret = aeskeywrap_test()) != 0)
  1094. return err_sys("AES Key Wrap test failed!\n", ret);
  1095. else
  1096. TEST_PASS("AES Key Wrap test passed!\n");
  1097. #endif
  1098. #ifdef WOLFSSL_AES_SIV
  1099. if ( (ret = aes_siv_test()) != 0)
  1100. return err_sys("AES-SIV test failed!\n", ret);
  1101. else
  1102. TEST_PASS("AES-SIV test passed!\n");
  1103. #endif
  1104. #endif
  1105. #ifdef HAVE_CAMELLIA
  1106. if ( (ret = camellia_test()) != 0)
  1107. return err_sys("CAMELLIA test failed!\n", ret);
  1108. else
  1109. TEST_PASS("CAMELLIA test passed!\n");
  1110. #endif
  1111. #if !defined(NO_RSA)
  1112. #ifdef WC_RSA_NO_PADDING
  1113. if ( (ret = rsa_no_pad_test()) != 0)
  1114. return err_sys("RSA NOPAD test failed!\n", ret);
  1115. else
  1116. TEST_PASS("RSA NOPAD test passed!\n");
  1117. #endif
  1118. if ( (ret = rsa_test()) != 0)
  1119. return err_sys("RSA test failed!\n", ret);
  1120. else
  1121. TEST_PASS("RSA test passed!\n");
  1122. #endif
  1123. #ifndef NO_DH
  1124. PRIVATE_KEY_UNLOCK();
  1125. if ( (ret = dh_test()) != 0)
  1126. return err_sys("DH test failed!\n", ret);
  1127. else
  1128. TEST_PASS("DH test passed!\n");
  1129. PRIVATE_KEY_LOCK();
  1130. #endif
  1131. #ifndef NO_DSA
  1132. if ( (ret = dsa_test()) != 0)
  1133. return err_sys("DSA test failed!\n", ret);
  1134. else
  1135. TEST_PASS("DSA test passed!\n");
  1136. #endif
  1137. #ifdef WOLFCRYPT_HAVE_SRP
  1138. if ( (ret = srp_test()) != 0)
  1139. return err_sys("SRP test failed!\n", ret);
  1140. else
  1141. TEST_PASS("SRP test passed!\n");
  1142. #endif
  1143. #ifndef NO_PWDBASED
  1144. if ( (ret = pwdbased_test()) != 0)
  1145. return err_sys("PWDBASED test failed!\n", ret);
  1146. else
  1147. TEST_PASS("PWDBASED test passed!\n");
  1148. #endif
  1149. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  1150. if ( (ret = openssl_test()) != 0)
  1151. return err_sys("OPENSSL test failed!\n", ret);
  1152. else
  1153. TEST_PASS("OPENSSL test passed!\n");
  1154. if ( (ret = openSSL_evpMD_test()) != 0)
  1155. return err_sys("OPENSSL (EVP MD) test failed!\n", ret);
  1156. else
  1157. TEST_PASS("OPENSSL (EVP MD) passed!\n");
  1158. if ( (ret = openssl_pkey0_test()) != 0)
  1159. return err_sys("OPENSSL (PKEY0) test failed!\n", ret);
  1160. else
  1161. TEST_PASS("OPENSSL (PKEY0) passed!\n");
  1162. if ( (ret = openssl_pkey1_test()) != 0)
  1163. return err_sys("OPENSSL (PKEY1) test failed!\n", ret);
  1164. else
  1165. TEST_PASS("OPENSSL (PKEY1) passed!\n");
  1166. #if !defined(WOLF_CRYPTO_CB_ONLY_RSA) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  1167. if ( (ret = openssl_evpSig_test()) != 0)
  1168. return err_sys("OPENSSL (EVP Sign/Verify) test failed!\n", ret);
  1169. else
  1170. TEST_PASS("OPENSSL (EVP Sign/Verify) passed!\n");
  1171. #endif
  1172. #endif
  1173. #if defined(HAVE_ECC)
  1174. PRIVATE_KEY_UNLOCK();
  1175. if ( (ret = ecc_test()) != 0)
  1176. return err_sys("ECC test failed!\n", ret);
  1177. else
  1178. TEST_PASS("ECC test passed!\n");
  1179. PRIVATE_KEY_LOCK();
  1180. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  1181. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  1182. if ( (ret = ecc_encrypt_test()) != 0)
  1183. return err_sys("ECC Enc test failed!\n", ret);
  1184. else
  1185. TEST_PASS("ECC Enc test passed!\n");
  1186. #endif
  1187. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  1188. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  1189. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  1190. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  1191. /* skip for ATECC508/608A, cannot import private key buffers */
  1192. if ( (ret = ecc_test_buffers()) != 0)
  1193. return err_sys("ECC buffer test failed!\n", ret);
  1194. else
  1195. TEST_PASS("ECC buffer test passed!\n");
  1196. #endif
  1197. #endif
  1198. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  1199. !defined(NO_FILESYSTEM)
  1200. if ( (ret = cert_test()) != 0)
  1201. return err_sys("CERT test failed!\n", ret);
  1202. else
  1203. TEST_PASS("CERT test passed!\n");
  1204. #endif
  1205. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  1206. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(WOLFSSL_GEN_CERT)
  1207. if ( (ret = certext_test()) != 0)
  1208. return err_sys("CERT EXT test failed!\n", ret);
  1209. else
  1210. TEST_PASS("CERT EXT test passed!\n");
  1211. #endif
  1212. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  1213. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  1214. if ( (ret = decodedCertCache_test()) != 0)
  1215. return err_sys("DECODED CERT CACHE test failed!\n", ret);
  1216. else
  1217. TEST_PASS("DECODED CERT CACHE test passed!\n");
  1218. #endif
  1219. #ifdef HAVE_CURVE25519
  1220. if ( (ret = curve25519_test()) != 0)
  1221. return err_sys("CURVE25519 test failed!\n", ret);
  1222. else
  1223. TEST_PASS("CURVE25519 test passed!\n");
  1224. #endif
  1225. #ifdef HAVE_ED25519
  1226. if ( (ret = ed25519_test()) != 0)
  1227. return err_sys("ED25519 test failed!\n", ret);
  1228. else
  1229. TEST_PASS("ED25519 test passed!\n");
  1230. #endif
  1231. #ifdef HAVE_CURVE448
  1232. if ( (ret = curve448_test()) != 0)
  1233. return err_sys("CURVE448 test failed!\n", ret);
  1234. else
  1235. TEST_PASS("CURVE448 test passed!\n");
  1236. #endif
  1237. #ifdef HAVE_ED448
  1238. if ( (ret = ed448_test()) != 0)
  1239. return err_sys("ED448 test failed!\n", ret);
  1240. else
  1241. TEST_PASS("ED448 test passed!\n");
  1242. #endif
  1243. #ifdef WOLFSSL_HAVE_KYBER
  1244. if ( (ret = kyber_test()) != 0)
  1245. return err_sys("KYBER test failed!\n", ret);
  1246. else
  1247. TEST_PASS("KYBER test passed!\n");
  1248. #endif
  1249. #ifdef WOLFCRYPT_HAVE_ECCSI
  1250. if ( (ret = eccsi_test()) != 0)
  1251. return err_sys("ECCSI test failed!\n", ret);
  1252. else
  1253. TEST_PASS("ECCSI test passed!\n");
  1254. #endif
  1255. #ifdef WOLFCRYPT_HAVE_SAKKE
  1256. if ( (ret = sakke_test()) != 0)
  1257. return err_sys("SAKKE test failed!\n", ret);
  1258. else
  1259. TEST_PASS("SAKKE test passed!\n");
  1260. #endif
  1261. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  1262. if ( (ret = cmac_test()) != 0)
  1263. return err_sys("CMAC test failed!\n", ret);
  1264. else
  1265. TEST_PASS("CMAC test passed!\n");
  1266. #endif
  1267. #if defined(WOLFSSL_SIPHASH)
  1268. if ( (ret = siphash_test()) != 0)
  1269. return err_sys("SipHash test failed!\n", ret);
  1270. else
  1271. TEST_PASS("SipHash test passed!\n");
  1272. #endif
  1273. #ifdef HAVE_LIBZ
  1274. if ( (ret = compress_test()) != 0)
  1275. return err_sys("COMPRESS test failed!\n", ret);
  1276. else
  1277. TEST_PASS("COMPRESS test passed!\n");
  1278. #endif
  1279. #ifdef HAVE_PKCS7
  1280. #ifndef NO_PKCS7_ENCRYPTED_DATA
  1281. if ( (ret = pkcs7encrypted_test()) != 0)
  1282. return err_sys("PKCS7encrypted test failed!\n", ret);
  1283. else
  1284. TEST_PASS("PKCS7encrypted test passed!\n");
  1285. #endif
  1286. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  1287. if ( (ret = pkcs7compressed_test()) != 0)
  1288. return err_sys("PKCS7compressed test failed!\n", ret);
  1289. else
  1290. TEST_PASS("PKCS7compressed test passed!\n");
  1291. #endif
  1292. if ( (ret = pkcs7signed_test()) != 0)
  1293. return err_sys("PKCS7signed test failed!\n", ret);
  1294. else
  1295. TEST_PASS("PKCS7signed test passed!\n");
  1296. if ( (ret = pkcs7enveloped_test()) != 0)
  1297. return err_sys("PKCS7enveloped test failed!\n", ret);
  1298. else
  1299. TEST_PASS("PKCS7enveloped test passed!\n");
  1300. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  1301. if ( (ret = pkcs7authenveloped_test()) != 0)
  1302. return err_sys("PKCS7authenveloped test failed!\n", ret);
  1303. else
  1304. TEST_PASS("PKCS7authenveloped test passed!\n");
  1305. #endif
  1306. #endif
  1307. #ifdef HAVE_VALGRIND
  1308. if ( (ret = mp_test()) != 0)
  1309. return err_sys("mp test failed!\n", ret);
  1310. else
  1311. TEST_PASS("mp test passed!\n");
  1312. #endif
  1313. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  1314. if ( (ret = prime_test()) != 0)
  1315. return err_sys("prime test failed!\n", ret);
  1316. else
  1317. TEST_PASS("prime test passed!\n");
  1318. #endif
  1319. #if defined(ASN_BER_TO_DER) && \
  1320. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  1321. defined(OPENSSL_EXTRA_X509_SMALL))
  1322. if ( (ret = berder_test()) != 0)
  1323. return err_sys("ber-der test failed!\n", ret);
  1324. else
  1325. TEST_PASS("ber-der test passed!\n");
  1326. #endif
  1327. if ( (ret = logging_test()) != 0)
  1328. return err_sys("logging test failed!\n", ret);
  1329. else
  1330. TEST_PASS("logging test passed!\n");
  1331. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  1332. if ( (ret = time_test()) != 0)
  1333. return err_sys("time test failed!\n", ret);
  1334. else
  1335. TEST_PASS("time test passed!\n");
  1336. #endif
  1337. #if defined(__INCLUDE_NUTTX_CONFIG_H)
  1338. if ((ret = wolfcrypt_mutex_test()) != 0)
  1339. #else
  1340. if ((ret = mutex_test()) != 0)
  1341. #endif
  1342. return err_sys("mutex test failed!\n", ret);
  1343. else
  1344. TEST_PASS("mutex test passed!\n");
  1345. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  1346. if ( (ret = memcb_test()) != 0)
  1347. return err_sys("memcb test failed!\n", ret);
  1348. else
  1349. TEST_PASS("memcb test passed!\n");
  1350. #endif
  1351. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  1352. if ( (ret = blob_test()) != 0)
  1353. return err_sys("blob test failed!\n", ret);
  1354. else
  1355. TEST_PASS("blob test passed!\n");
  1356. #endif
  1357. #if defined(WOLF_CRYPTO_CB) && \
  1358. !(defined(HAVE_INTEL_QAT_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC) || \
  1359. defined(WOLFSSL_QNX_CAAM))
  1360. if ( (ret = cryptocb_test()) != 0)
  1361. return err_sys("crypto callback test failed!\n", ret);
  1362. else
  1363. TEST_PASS("crypto callback test passed!\n");
  1364. #endif
  1365. #ifdef WOLFSSL_CERT_PIV
  1366. if ( (ret = certpiv_test()) != 0)
  1367. return err_sys("cert piv test failed!\n", ret);
  1368. else
  1369. TEST_PASS("cert piv test passed!\n");
  1370. #endif
  1371. #ifdef WOLF_CRYPTO_CB
  1372. #ifdef HAVE_INTEL_QA_SYNC
  1373. wc_CryptoCb_CleanupIntelQa(&devId);
  1374. #endif
  1375. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  1376. wc_CryptoCb_CleanupOcteon(&devId);
  1377. #endif
  1378. #endif
  1379. #ifdef WOLFSSL_ASYNC_CRYPT
  1380. wolfAsync_DevClose(&devId);
  1381. #endif
  1382. /* cleanup the thread if fixed point cache is enabled and have thread local */
  1383. #if defined(HAVE_THREAD_LS) && defined(HAVE_ECC) && defined(FP_ECC)
  1384. wc_ecc_fp_free();
  1385. #endif
  1386. if (args)
  1387. ((func_args*)args)->return_code = ret;
  1388. TEST_PASS("Test complete\n");
  1389. EXIT_TEST(ret);
  1390. }
  1391. #ifndef NO_MAIN_DRIVER
  1392. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  1393. int myoptind = 0;
  1394. char* myoptarg = NULL;
  1395. #endif
  1396. /* so overall tests can pull in test function */
  1397. #if defined(WOLFSSL_ESPIDF) || defined(_WIN32_WCE)
  1398. int wolf_test_task(void)
  1399. #else
  1400. #ifndef NO_MAIN_FUNCTION
  1401. int main(int argc, char** argv)
  1402. {
  1403. return wolfcrypt_test_main(argc, argv);
  1404. }
  1405. #endif
  1406. int wolfcrypt_test_main(int argc, char** argv)
  1407. #endif
  1408. {
  1409. int ret;
  1410. func_args args;
  1411. #if defined(WOLFSSL_ESPIDF) || defined(WOLFSSL_SE050)
  1412. /* set dummy wallclock time. */
  1413. struct timeval utctime;
  1414. struct timezone tz;
  1415. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1416. utctime.tv_usec = 0;
  1417. tz.tz_minuteswest = 0;
  1418. tz.tz_dsttime = 0;
  1419. settimeofday(&utctime, &tz);
  1420. #endif
  1421. #ifdef WOLFSSL_APACHE_MYNEWT
  1422. #ifdef ARCH_sim
  1423. mcu_sim_parse_args(argc, argv);
  1424. #endif
  1425. sysinit();
  1426. /* set dummy wallclock time. */
  1427. struct os_timeval utctime;
  1428. struct os_timezone tz;
  1429. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1430. utctime.tv_usec = 0;
  1431. tz.tz_minuteswest = 0;
  1432. tz.tz_dsttime = 0;
  1433. os_settimeofday(&utctime, &tz);
  1434. #endif
  1435. #ifdef WOLFSSL_ZEPHYR
  1436. /* set dummy wallclock time. */
  1437. struct timespec utctime;
  1438. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1439. utctime.tv_nsec = 0;
  1440. clock_settime(CLOCK_REALTIME, &utctime);
  1441. #endif
  1442. #ifdef DEVKITPRO
  1443. void *framebuffer;
  1444. GXRModeObj *rmode = NULL;
  1445. VIDEO_Init();
  1446. WPAD_Init();
  1447. rmode = VIDEO_GetPreferredMode(NULL);
  1448. #pragma GCC diagnostic ignored "-Wbad-function-cast"
  1449. framebuffer = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
  1450. #pragma GCC diagnostic pop
  1451. console_init(framebuffer,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
  1452. VIDEO_Configure(rmode);
  1453. VIDEO_SetNextFramebuffer(framebuffer);
  1454. VIDEO_SetBlack(FALSE);
  1455. VIDEO_Flush();
  1456. VIDEO_WaitVSync();
  1457. if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
  1458. #endif
  1459. #ifdef HAVE_WNR
  1460. if (wc_InitNetRandom(wnrConfigFile, NULL, 5000) != 0) {
  1461. err_sys("Whitewood netRandom global config failed", -1001);
  1462. return -1002;
  1463. }
  1464. #endif
  1465. #ifndef WOLFSSL_ESPIDF
  1466. args.argc = argc;
  1467. args.argv = argv;
  1468. #endif
  1469. if ((ret = wolfCrypt_Init()) != 0) {
  1470. printf("wolfCrypt_Init failed %d\n", ret);
  1471. err_sys("Error with wolfCrypt_Init!\n", -1003);
  1472. }
  1473. #ifdef WC_RNG_SEED_CB
  1474. wc_SetSeed_Cb(wc_GenerateSeed);
  1475. #endif
  1476. #ifdef HAVE_STACK_SIZE
  1477. StackSizeCheck(&args, wolfcrypt_test);
  1478. #else
  1479. wolfcrypt_test(&args);
  1480. #endif
  1481. if ((ret = wolfCrypt_Cleanup()) != 0) {
  1482. printf("wolfCrypt_Cleanup failed %d\n", ret);
  1483. err_sys("Error with wolfCrypt_Cleanup!\n", -1004);
  1484. }
  1485. #ifdef HAVE_WNR
  1486. if (wc_FreeNetRandom() < 0)
  1487. err_sys("Failed to free netRandom context", -1005);
  1488. #endif /* HAVE_WNR */
  1489. #ifdef DOLPHIN_EMULATOR
  1490. /* Returning from main panics the emulator. Just hang
  1491. * and let the user force quit the emulator window. */
  1492. printf("args.return_code: %d\n", args.return_code);
  1493. printf("Testing complete. You may close the window now\n");
  1494. while (1);
  1495. #endif
  1496. #ifdef WOLFSSL_ESPIDF
  1497. /* ESP_LOGI to print takes up a lot less memory than printf */
  1498. ESP_LOGI("wolfcrypt_test", "Exiting main with return code: % d\n", args.return_code);
  1499. #endif
  1500. /* everything else will use printf */
  1501. #if !defined(WOLFSSL_ESPIDF)
  1502. /* gate this for target platforms wishing to avoid printf reference */
  1503. printf("Exiting main with return code: %d\n", args.return_code);
  1504. #endif
  1505. return args.return_code;
  1506. } /* wolfcrypt_test_main or wolf_test_task */
  1507. #endif /* NO_MAIN_DRIVER */
  1508. /* helper to save DER, convert to PEM and save PEM */
  1509. #if !defined(NO_ASN) && (defined(HAVE_ECC) || !defined(NO_DSA) || \
  1510. (!defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)))) \
  1511. && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  1512. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1513. #define SaveDerAndPem(d, dSz, fD, fP, pT, eB) _SaveDerAndPem(d, dSz, fD, fP, pT, eB)
  1514. #else
  1515. #define SaveDerAndPem(d, dSz, fD, fP, pT, eB) _SaveDerAndPem(d, dSz, NULL, NULL, pT, eB)
  1516. #endif
  1517. static int _SaveDerAndPem(const byte* der, int derSz,
  1518. const char* fileDer, const char* filePem, int pemType, int errBase)
  1519. {
  1520. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1521. int ret;
  1522. XFILE derFile;
  1523. derFile = XFOPEN(fileDer, "wb");
  1524. if (!derFile) {
  1525. return errBase + 0;
  1526. }
  1527. ret = (int)XFWRITE(der, 1, derSz, derFile);
  1528. XFCLOSE(derFile);
  1529. if (ret != derSz) {
  1530. return errBase + 1;
  1531. }
  1532. #endif
  1533. #ifdef WOLFSSL_DER_TO_PEM
  1534. if (filePem) {
  1535. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1536. XFILE pemFile;
  1537. #endif
  1538. byte* pem;
  1539. int pemSz;
  1540. /* calculate PEM size */
  1541. pemSz = wc_DerToPem(der, derSz, NULL, 0, pemType);
  1542. if (pemSz < 0) {
  1543. return pemSz;
  1544. }
  1545. pem = (byte*)XMALLOC(pemSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1546. if (pem == NULL) {
  1547. return MEMORY_E;
  1548. }
  1549. /* Convert to PEM */
  1550. pemSz = wc_DerToPem(der, derSz, pem, pemSz, pemType);
  1551. if (pemSz < 0) {
  1552. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1553. return errBase + 2;
  1554. }
  1555. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1556. pemFile = XFOPEN(filePem, "wb");
  1557. if (!pemFile) {
  1558. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1559. return errBase + 3;
  1560. }
  1561. ret = (int)XFWRITE(pem, 1, pemSz, pemFile);
  1562. XFCLOSE(pemFile);
  1563. if (ret != pemSz) {
  1564. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1565. return errBase + 4;
  1566. }
  1567. #endif
  1568. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1569. }
  1570. #endif /* WOLFSSL_DER_TO_PEM */
  1571. /* suppress unused variable warnings */
  1572. (void)der;
  1573. (void)derSz;
  1574. (void)filePem;
  1575. (void)fileDer;
  1576. (void)pemType;
  1577. (void)errBase;
  1578. return 0;
  1579. }
  1580. #endif /* WOLFSSL_KEY_GEN || WOLFSSL_CERT_GEN */
  1581. WOLFSSL_TEST_SUBROUTINE int error_test(void)
  1582. {
  1583. const char* errStr;
  1584. char out[WOLFSSL_MAX_ERROR_SZ];
  1585. const char* unknownStr = wc_GetErrorString(0);
  1586. #ifdef NO_ERROR_STRINGS
  1587. /* Ensure a valid error code's string matches an invalid code's.
  1588. * The string is that error strings are not available.
  1589. */
  1590. errStr = wc_GetErrorString(OPEN_RAN_E);
  1591. wc_ErrorString(OPEN_RAN_E, out);
  1592. if (XSTRCMP(errStr, unknownStr) != 0)
  1593. return -1100;
  1594. if (XSTRCMP(out, unknownStr) != 0)
  1595. return -1101;
  1596. #else
  1597. int i;
  1598. int j = 0;
  1599. /* Values that are not or no longer error codes. */
  1600. int missing[] = { -122, -123, -124, -127, -128, -129, -159,
  1601. -163, -164, -165, -166, -167, -168, -169, -233,
  1602. 0 };
  1603. /* Check that all errors have a string and it's the same through the two
  1604. * APIs. Check that the values that are not errors map to the unknown
  1605. * string.
  1606. */
  1607. for (i = MAX_CODE_E-1; i >= WC_LAST_E; i--) {
  1608. errStr = wc_GetErrorString(i);
  1609. wc_ErrorString(i, out);
  1610. if (i != missing[j]) {
  1611. if (XSTRCMP(errStr, unknownStr) == 0)
  1612. return -1102;
  1613. if (XSTRCMP(out, unknownStr) == 0)
  1614. return -1103;
  1615. if (XSTRCMP(errStr, out) != 0)
  1616. return -1104;
  1617. if (XSTRLEN(errStr) >= WOLFSSL_MAX_ERROR_SZ)
  1618. return -1105;
  1619. }
  1620. else {
  1621. j++;
  1622. if (XSTRCMP(errStr, unknownStr) != 0)
  1623. return -1106;
  1624. if (XSTRCMP(out, unknownStr) != 0)
  1625. return -1107;
  1626. }
  1627. }
  1628. /* Check if the next possible value has been given a string. */
  1629. errStr = wc_GetErrorString(i);
  1630. wc_ErrorString(i, out);
  1631. if (XSTRCMP(errStr, unknownStr) != 0)
  1632. return -1108;
  1633. if (XSTRCMP(out, unknownStr) != 0)
  1634. return -1109;
  1635. #endif
  1636. return 0;
  1637. }
  1638. #ifndef NO_CODING
  1639. WOLFSSL_TEST_SUBROUTINE int base64_test(void)
  1640. {
  1641. int ret;
  1642. WOLFSSL_SMALL_STACK_STATIC const byte good[] = "A+Gd\0\0\0";
  1643. WOLFSSL_SMALL_STACK_STATIC const byte goodEnd[] = "A+Gd \r\n";
  1644. WOLFSSL_SMALL_STACK_STATIC const byte good_spaces[] = " A + G d \0";
  1645. byte out[128];
  1646. word32 outLen;
  1647. #ifdef WOLFSSL_BASE64_ENCODE
  1648. byte data[3];
  1649. word32 dataLen;
  1650. byte longData[79] = { 0 };
  1651. WOLFSSL_SMALL_STACK_STATIC const byte symbols[] = "+/A=";
  1652. #endif
  1653. WOLFSSL_SMALL_STACK_STATIC const byte badSmall[] = "AAA!Gdj=";
  1654. WOLFSSL_SMALL_STACK_STATIC const byte badLarge[] = "AAA~Gdj=";
  1655. WOLFSSL_SMALL_STACK_STATIC const byte badEOL[] = "A+Gd!AA";
  1656. WOLFSSL_SMALL_STACK_STATIC const byte badPadding[] = "AA=A";
  1657. WOLFSSL_SMALL_STACK_STATIC const byte badChar[] = ",-.:;<=>?@[\\]^_`";
  1658. byte goodChar[] =
  1659. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1660. "abcdefghijklmnopqrstuvwxyz"
  1661. "0123456789+/;";
  1662. byte charTest[] = "A+Gd\0\0\0";
  1663. int i;
  1664. /* Good Base64 encodings. */
  1665. outLen = sizeof(out);
  1666. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  1667. if (ret != 0)
  1668. return -1200;
  1669. outLen = sizeof(out);
  1670. ret = Base64_Decode(goodEnd, sizeof(goodEnd), out, &outLen);
  1671. if (ret != 0)
  1672. return -1201;
  1673. outLen = sizeof(goodChar);
  1674. ret = Base64_Decode(goodChar, sizeof(goodChar), goodChar, &outLen);
  1675. if (ret != 0)
  1676. return -1235;
  1677. if (outLen != 64 / 4 * 3)
  1678. return -1236;
  1679. outLen = sizeof(out);
  1680. ret = Base64_Decode(good_spaces, sizeof(good_spaces), out, &outLen);
  1681. if (ret != 0)
  1682. return -1201;
  1683. /* Bad parameters. */
  1684. outLen = 1;
  1685. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  1686. if (ret != BAD_FUNC_ARG)
  1687. return -1202;
  1688. outLen = sizeof(out);
  1689. ret = Base64_Decode(badEOL, sizeof(badEOL), out, &outLen);
  1690. if (ret != ASN_INPUT_E)
  1691. return -1203;
  1692. outLen = sizeof(out);
  1693. ret = Base64_Decode(badPadding, sizeof(badPadding), out, &outLen);
  1694. if (ret != ASN_INPUT_E)
  1695. return -1203;
  1696. /* Bad character at each offset 0-3. */
  1697. for (i = 0; i < 4; i++) {
  1698. outLen = sizeof(out);
  1699. ret = Base64_Decode(badSmall + i, 4, out, &outLen);
  1700. if (ret != ASN_INPUT_E)
  1701. return -1204 - i;
  1702. ret = Base64_Decode(badLarge + i, 4, out, &outLen);
  1703. if (ret != ASN_INPUT_E)
  1704. return -1214 - i;
  1705. }
  1706. /* Invalid character less than 0x2b */
  1707. for (i = 1; i < 0x2b; i++) {
  1708. outLen = sizeof(out);
  1709. charTest[0] = (byte)i;
  1710. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1711. if (ret != ASN_INPUT_E)
  1712. return -1240 - i;
  1713. }
  1714. /* Bad characters in range 0x2b - 0x7a. */
  1715. for (i = 0; i < (int)sizeof(badChar) - 1; i++) {
  1716. outLen = sizeof(out);
  1717. charTest[0] = badChar[i];
  1718. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1719. if (ret != ASN_INPUT_E)
  1720. return -1270 - i;
  1721. }
  1722. /* Invalid character greater than 0x7a */
  1723. for (i = 0x7b; i < 0x100; i++) {
  1724. outLen = sizeof(out);
  1725. charTest[0] = (byte)i;
  1726. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1727. if (ret != ASN_INPUT_E)
  1728. return -1290 - i;
  1729. }
  1730. #ifdef WOLFSSL_BASE64_ENCODE
  1731. /* Decode and encode all symbols - non-alphanumeric. */
  1732. dataLen = sizeof(data);
  1733. ret = Base64_Decode(symbols, sizeof(symbols), data, &dataLen);
  1734. if (ret != 0)
  1735. return -1224;
  1736. outLen = sizeof(out);
  1737. ret = Base64_Encode(data, dataLen, NULL, &outLen);
  1738. if (ret != LENGTH_ONLY_E)
  1739. return -1225;
  1740. outLen = sizeof(out);
  1741. ret = Base64_Encode(data, dataLen, out, &outLen);
  1742. if (ret != 0)
  1743. return -1226;
  1744. outLen = 7;
  1745. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  1746. if (ret != BUFFER_E)
  1747. return -1227;
  1748. outLen = sizeof(out);
  1749. ret = Base64_EncodeEsc(data, dataLen, NULL, &outLen);
  1750. if (ret != LENGTH_ONLY_E)
  1751. return -1228;
  1752. outLen = sizeof(out);
  1753. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  1754. if (ret != 0)
  1755. return -1229;
  1756. outLen = sizeof(out);
  1757. ret = Base64_Encode_NoNl(data, dataLen, out, &outLen);
  1758. if (ret != 0)
  1759. return -1230;
  1760. /* Data that results in an encoding longer than one line. */
  1761. outLen = sizeof(out);
  1762. dataLen = sizeof(longData);
  1763. ret = Base64_Encode(longData, dataLen, out, &outLen);
  1764. if (ret != 0)
  1765. return -1231;
  1766. outLen = sizeof(out);
  1767. ret = Base64_EncodeEsc(longData, dataLen, out, &outLen);
  1768. if (ret != 0)
  1769. return -1232;
  1770. outLen = sizeof(out);
  1771. ret = Base64_Encode_NoNl(longData, dataLen, out, &outLen);
  1772. if (ret != 0)
  1773. return -1233;
  1774. #endif
  1775. return 0;
  1776. }
  1777. #ifdef WOLFSSL_BASE16
  1778. WOLFSSL_TEST_SUBROUTINE int base16_test(void)
  1779. {
  1780. int ret;
  1781. WOLFSSL_SMALL_STACK_STATIC const byte testData[] = "SomeDataToEncode\n";
  1782. WOLFSSL_SMALL_STACK_STATIC const byte encodedTestData[] = "536F6D6544617461546F456E636F64650A00";
  1783. byte encoded[40];
  1784. word32 encodedLen;
  1785. byte plain[40];
  1786. word32 len;
  1787. /* length returned includes null termination */
  1788. encodedLen = sizeof(encoded);
  1789. ret = Base16_Encode(testData, sizeof(testData), encoded, &encodedLen);
  1790. if (ret != 0)
  1791. return -1300;
  1792. len = (word32)XSTRLEN((char*)encoded);
  1793. if (len != encodedLen - 1)
  1794. return -1301;
  1795. len = sizeof(plain);
  1796. ret = Base16_Decode(encoded, encodedLen - 1, plain, &len);
  1797. if (ret != 0)
  1798. return -1302;
  1799. if (len != sizeof(testData) || XMEMCMP(testData, plain, len) != 0)
  1800. return -1303;
  1801. if (encodedLen != sizeof(encodedTestData) ||
  1802. XMEMCMP(encoded, encodedTestData, encodedLen) != 0) {
  1803. return -1304;
  1804. }
  1805. return 0;
  1806. }
  1807. #endif /* WOLFSSL_BASE16 */
  1808. #endif /* !NO_CODING */
  1809. #ifndef NO_ASN
  1810. WOLFSSL_TEST_SUBROUTINE int asn_test(void)
  1811. {
  1812. int ret;
  1813. /* ASN1 encoded date buffer */
  1814. WOLFSSL_SMALL_STACK_STATIC const byte dateBuf[] = {0x17, 0x0d, 0x31, 0x36, 0x30, 0x38, 0x31, 0x31,
  1815. 0x32, 0x30, 0x30, 0x37, 0x33, 0x37, 0x5a};
  1816. byte format;
  1817. int length;
  1818. const byte* datePart;
  1819. #ifndef NO_ASN_TIME
  1820. struct tm timearg;
  1821. time_t now;
  1822. #endif
  1823. ret = wc_GetDateInfo(dateBuf, (int)sizeof(dateBuf), &datePart, &format,
  1824. &length);
  1825. if (ret != 0)
  1826. return -1400;
  1827. #ifndef NO_ASN_TIME
  1828. /* Parameter Validation tests. */
  1829. if (wc_GetTime(NULL, sizeof(now)) != BAD_FUNC_ARG)
  1830. return -1401;
  1831. if (wc_GetTime(&now, 0) != BUFFER_E)
  1832. return -1402;
  1833. now = 0;
  1834. if (wc_GetTime(&now, sizeof(now)) != 0) {
  1835. return -1403;
  1836. }
  1837. if (now == 0) {
  1838. printf("RTC/Time not set!\n");
  1839. return -1404;
  1840. }
  1841. ret = wc_GetDateAsCalendarTime(datePart, length, format, &timearg);
  1842. if (ret != 0)
  1843. return -1405;
  1844. #endif /* !NO_ASN_TIME */
  1845. return 0;
  1846. }
  1847. #endif /* !NO_ASN */
  1848. #ifdef WOLFSSL_MD2
  1849. WOLFSSL_TEST_SUBROUTINE int md2_test(void)
  1850. {
  1851. int ret = 0;
  1852. Md2 md2;
  1853. byte hash[MD2_DIGEST_SIZE];
  1854. testVector a, b, c, d, e, f, g;
  1855. testVector test_md2[7];
  1856. int times = sizeof(test_md2) / sizeof(testVector), i;
  1857. a.input = "";
  1858. a.output = "\x83\x50\xe5\xa3\xe2\x4c\x15\x3d\xf2\x27\x5c\x9f\x80\x69"
  1859. "\x27\x73";
  1860. a.inLen = XSTRLEN(a.input);
  1861. a.outLen = MD2_DIGEST_SIZE;
  1862. b.input = "a";
  1863. b.output = "\x32\xec\x01\xec\x4a\x6d\xac\x72\xc0\xab\x96\xfb\x34\xc0"
  1864. "\xb5\xd1";
  1865. b.inLen = XSTRLEN(b.input);
  1866. b.outLen = MD2_DIGEST_SIZE;
  1867. c.input = "abc";
  1868. c.output = "\xda\x85\x3b\x0d\x3f\x88\xd9\x9b\x30\x28\x3a\x69\xe6\xde"
  1869. "\xd6\xbb";
  1870. c.inLen = XSTRLEN(c.input);
  1871. c.outLen = MD2_DIGEST_SIZE;
  1872. d.input = "message digest";
  1873. d.output = "\xab\x4f\x49\x6b\xfb\x2a\x53\x0b\x21\x9f\xf3\x30\x31\xfe"
  1874. "\x06\xb0";
  1875. d.inLen = XSTRLEN(d.input);
  1876. d.outLen = MD2_DIGEST_SIZE;
  1877. e.input = "abcdefghijklmnopqrstuvwxyz";
  1878. e.output = "\x4e\x8d\xdf\xf3\x65\x02\x92\xab\x5a\x41\x08\xc3\xaa\x47"
  1879. "\x94\x0b";
  1880. e.inLen = XSTRLEN(e.input);
  1881. e.outLen = MD2_DIGEST_SIZE;
  1882. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  1883. "6789";
  1884. f.output = "\xda\x33\xde\xf2\xa4\x2d\xf1\x39\x75\x35\x28\x46\xc3\x03"
  1885. "\x38\xcd";
  1886. f.inLen = XSTRLEN(f.input);
  1887. f.outLen = MD2_DIGEST_SIZE;
  1888. g.input = "1234567890123456789012345678901234567890123456789012345678"
  1889. "9012345678901234567890";
  1890. g.output = "\xd5\x97\x6f\x79\xd8\x3d\x3a\x0d\xc9\x80\x6c\x3c\x66\xf3"
  1891. "\xef\xd8";
  1892. g.inLen = XSTRLEN(g.input);
  1893. g.outLen = MD2_DIGEST_SIZE;
  1894. test_md2[0] = a;
  1895. test_md2[1] = b;
  1896. test_md2[2] = c;
  1897. test_md2[3] = d;
  1898. test_md2[4] = e;
  1899. test_md2[5] = f;
  1900. test_md2[6] = g;
  1901. wc_InitMd2(&md2);
  1902. for (i = 0; i < times; ++i) {
  1903. wc_Md2Update(&md2, (byte*)test_md2[i].input, (word32)test_md2[i].inLen);
  1904. wc_Md2Final(&md2, hash);
  1905. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0)
  1906. return -1500 - i;
  1907. }
  1908. for (i = 0; i < times; ++i) {
  1909. ret = wc_Md2Hash((byte*)test_md2[i].input, (word32)test_md2[i].inLen, hash);
  1910. if (ret != 0) {
  1911. return -1507 - i;
  1912. }
  1913. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0) {
  1914. return -1507 - i;
  1915. }
  1916. }
  1917. return 0;
  1918. }
  1919. #endif
  1920. #ifndef NO_MD5
  1921. WOLFSSL_TEST_SUBROUTINE int md5_test(void)
  1922. {
  1923. int ret = 0;
  1924. wc_Md5 md5, md5Copy;
  1925. byte hash[WC_MD5_DIGEST_SIZE];
  1926. byte hashcopy[WC_MD5_DIGEST_SIZE];
  1927. testVector a, b, c, d, e, f;
  1928. testVector test_md5[6];
  1929. int times = sizeof(test_md5) / sizeof(testVector), i;
  1930. a.input = "";
  1931. a.output = "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42"
  1932. "\x7e";
  1933. a.inLen = XSTRLEN(a.input);
  1934. a.outLen = WC_MD5_DIGEST_SIZE;
  1935. b.input = "abc";
  1936. b.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  1937. "\x72";
  1938. b.inLen = XSTRLEN(b.input);
  1939. b.outLen = WC_MD5_DIGEST_SIZE;
  1940. c.input = "message digest";
  1941. c.output = "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
  1942. "\xd0";
  1943. c.inLen = XSTRLEN(c.input);
  1944. c.outLen = WC_MD5_DIGEST_SIZE;
  1945. d.input = "abcdefghijklmnopqrstuvwxyz";
  1946. d.output = "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
  1947. "\x3b";
  1948. d.inLen = XSTRLEN(d.input);
  1949. d.outLen = WC_MD5_DIGEST_SIZE;
  1950. e.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  1951. "6789";
  1952. e.output = "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
  1953. "\x9f";
  1954. e.inLen = XSTRLEN(e.input);
  1955. e.outLen = WC_MD5_DIGEST_SIZE;
  1956. f.input = "1234567890123456789012345678901234567890123456789012345678"
  1957. "9012345678901234567890";
  1958. f.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  1959. "\x7a";
  1960. f.inLen = XSTRLEN(f.input);
  1961. f.outLen = WC_MD5_DIGEST_SIZE;
  1962. test_md5[0] = a;
  1963. test_md5[1] = b;
  1964. test_md5[2] = c;
  1965. test_md5[3] = d;
  1966. test_md5[4] = e;
  1967. test_md5[5] = f;
  1968. ret = wc_InitMd5_ex(&md5, HEAP_HINT, devId);
  1969. if (ret != 0)
  1970. return -1600;
  1971. ret = wc_InitMd5_ex(&md5Copy, HEAP_HINT, devId);
  1972. if (ret != 0) {
  1973. wc_Md5Free(&md5);
  1974. return -1601;
  1975. }
  1976. for (i = 0; i < times; ++i) {
  1977. ret = wc_Md5Update(&md5, (byte*)test_md5[i].input,
  1978. (word32)test_md5[i].inLen);
  1979. if (ret != 0)
  1980. ERROR_OUT(-1602 - i, exit);
  1981. ret = wc_Md5GetHash(&md5, hashcopy);
  1982. if (ret != 0)
  1983. ERROR_OUT(-1603 - i, exit);
  1984. ret = wc_Md5Copy(&md5, &md5Copy);
  1985. if (ret != 0)
  1986. ERROR_OUT(-1604 - i, exit);
  1987. ret = wc_Md5Final(&md5, hash);
  1988. if (ret != 0)
  1989. ERROR_OUT(-1605 - i, exit);
  1990. wc_Md5Free(&md5Copy);
  1991. if (XMEMCMP(hash, test_md5[i].output, WC_MD5_DIGEST_SIZE) != 0)
  1992. ERROR_OUT(-1606 - i, exit);
  1993. if (XMEMCMP(hash, hashcopy, WC_MD5_DIGEST_SIZE) != 0)
  1994. ERROR_OUT(-1607 - i, exit);
  1995. }
  1996. #ifndef NO_LARGE_HASH_TEST
  1997. /* BEGIN LARGE HASH TEST */ {
  1998. byte large_input[1024];
  1999. const char* large_digest =
  2000. "\x44\xd0\x88\xce\xf1\x36\xd1\x78\xe9\xc8\xba\x84\xc3\xfd\xf6\xca";
  2001. for (i = 0; i < (int)sizeof(large_input); i++) {
  2002. large_input[i] = (byte)(i & 0xFF);
  2003. }
  2004. times = 100;
  2005. #ifdef WOLFSSL_PIC32MZ_HASH
  2006. wc_Md5SizeSet(&md5, times * sizeof(large_input));
  2007. #endif
  2008. for (i = 0; i < times; ++i) {
  2009. ret = wc_Md5Update(&md5, (byte*)large_input,
  2010. (word32)sizeof(large_input));
  2011. if (ret != 0)
  2012. ERROR_OUT(-1608, exit);
  2013. }
  2014. ret = wc_Md5Final(&md5, hash);
  2015. if (ret != 0)
  2016. ERROR_OUT(-1609, exit);
  2017. if (XMEMCMP(hash, large_digest, WC_MD5_DIGEST_SIZE) != 0)
  2018. ERROR_OUT(-1610, exit);
  2019. } /* END LARGE HASH TEST */
  2020. #endif /* NO_LARGE_HASH_TEST */
  2021. exit:
  2022. wc_Md5Free(&md5);
  2023. wc_Md5Free(&md5Copy);
  2024. return ret;
  2025. }
  2026. #endif /* NO_MD5 */
  2027. #ifndef NO_MD4
  2028. WOLFSSL_TEST_SUBROUTINE int md4_test(void)
  2029. {
  2030. Md4 md4;
  2031. byte hash[MD4_DIGEST_SIZE];
  2032. testVector a, b, c, d, e, f, g;
  2033. testVector test_md4[7];
  2034. int times = sizeof(test_md4) / sizeof(testVector), i;
  2035. a.input = "";
  2036. a.output = "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"
  2037. "\xc0";
  2038. a.inLen = XSTRLEN(a.input);
  2039. a.outLen = MD4_DIGEST_SIZE;
  2040. b.input = "a";
  2041. b.output = "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"
  2042. "\x24";
  2043. b.inLen = XSTRLEN(b.input);
  2044. b.outLen = MD4_DIGEST_SIZE;
  2045. c.input = "abc";
  2046. c.output = "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"
  2047. "\x9d";
  2048. c.inLen = XSTRLEN(c.input);
  2049. c.outLen = MD4_DIGEST_SIZE;
  2050. d.input = "message digest";
  2051. d.output = "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"
  2052. "\x4b";
  2053. d.inLen = XSTRLEN(d.input);
  2054. d.outLen = MD4_DIGEST_SIZE;
  2055. e.input = "abcdefghijklmnopqrstuvwxyz";
  2056. e.output = "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"
  2057. "\xa9";
  2058. e.inLen = XSTRLEN(e.input);
  2059. e.outLen = MD4_DIGEST_SIZE;
  2060. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  2061. "6789";
  2062. f.output = "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"
  2063. "\xe4";
  2064. f.inLen = XSTRLEN(f.input);
  2065. f.outLen = MD4_DIGEST_SIZE;
  2066. g.input = "1234567890123456789012345678901234567890123456789012345678"
  2067. "9012345678901234567890";
  2068. g.output = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"
  2069. "\x36";
  2070. g.inLen = XSTRLEN(g.input);
  2071. g.outLen = MD4_DIGEST_SIZE;
  2072. test_md4[0] = a;
  2073. test_md4[1] = b;
  2074. test_md4[2] = c;
  2075. test_md4[3] = d;
  2076. test_md4[4] = e;
  2077. test_md4[5] = f;
  2078. test_md4[6] = g;
  2079. wc_InitMd4(&md4);
  2080. for (i = 0; i < times; ++i) {
  2081. wc_Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen);
  2082. wc_Md4Final(&md4, hash);
  2083. if (XMEMCMP(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0)
  2084. return -1700 - i;
  2085. }
  2086. return 0;
  2087. }
  2088. #endif /* NO_MD4 */
  2089. #ifndef NO_SHA
  2090. WOLFSSL_TEST_SUBROUTINE int sha_test(void)
  2091. {
  2092. int ret = 0;
  2093. wc_Sha sha, shaCopy;
  2094. byte hash[WC_SHA_DIGEST_SIZE];
  2095. byte hashcopy[WC_SHA_DIGEST_SIZE];
  2096. testVector a, b, c, d, e;
  2097. testVector test_sha[5];
  2098. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2099. a.input = "";
  2100. a.output = "\xda\x39\xa3\xee\x5e\x6b\x4b\x0d\x32\x55\xbf\xef\x95\x60\x18"
  2101. "\x90\xaf\xd8\x07\x09";
  2102. a.inLen = XSTRLEN(a.input);
  2103. a.outLen = WC_SHA_DIGEST_SIZE;
  2104. b.input = "abc";
  2105. b.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  2106. "\x6C\x9C\xD0\xD8\x9D";
  2107. b.inLen = XSTRLEN(b.input);
  2108. b.outLen = WC_SHA_DIGEST_SIZE;
  2109. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2110. c.output = "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
  2111. "\xE5\xE5\x46\x70\xF1";
  2112. c.inLen = XSTRLEN(c.input);
  2113. c.outLen = WC_SHA_DIGEST_SIZE;
  2114. d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2115. "aaaaaa";
  2116. d.output = "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
  2117. "\x2A\x25\xEC\x64\x4D";
  2118. d.inLen = XSTRLEN(d.input);
  2119. d.outLen = WC_SHA_DIGEST_SIZE;
  2120. e.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2121. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2122. "aaaaaaaaaa";
  2123. e.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  2124. "\x53\x99\x5E\x26\xA0";
  2125. e.inLen = XSTRLEN(e.input);
  2126. e.outLen = WC_SHA_DIGEST_SIZE;
  2127. test_sha[0] = a;
  2128. test_sha[1] = b;
  2129. test_sha[2] = c;
  2130. test_sha[3] = d;
  2131. test_sha[4] = e;
  2132. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  2133. if (ret != 0)
  2134. return -1800;
  2135. ret = wc_InitSha_ex(&shaCopy, HEAP_HINT, devId);
  2136. if (ret != 0) {
  2137. wc_ShaFree(&sha);
  2138. return -1801;
  2139. }
  2140. for (i = 0; i < times; ++i) {
  2141. ret = wc_ShaUpdate(&sha, (byte*)test_sha[i].input,
  2142. (word32)test_sha[i].inLen);
  2143. if (ret != 0)
  2144. ERROR_OUT(-1802 - i, exit);
  2145. ret = wc_ShaGetHash(&sha, hashcopy);
  2146. if (ret != 0)
  2147. ERROR_OUT(-1803 - i, exit);
  2148. ret = wc_ShaCopy(&sha, &shaCopy);
  2149. if (ret != 0)
  2150. ERROR_OUT(-1804 - i, exit);
  2151. ret = wc_ShaFinal(&sha, hash);
  2152. if (ret != 0)
  2153. ERROR_OUT(-1805 - i, exit);
  2154. wc_ShaFree(&shaCopy);
  2155. if (XMEMCMP(hash, test_sha[i].output, WC_SHA_DIGEST_SIZE) != 0)
  2156. ERROR_OUT(-1806 - i, exit);
  2157. if (XMEMCMP(hash, hashcopy, WC_SHA_DIGEST_SIZE) != 0)
  2158. ERROR_OUT(-1807 - i, exit);
  2159. }
  2160. #ifndef NO_LARGE_HASH_TEST
  2161. /* BEGIN LARGE HASH TEST */ {
  2162. byte large_input[1024];
  2163. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  2164. defined(HASH_SIZE_LIMIT)
  2165. const char* large_digest =
  2166. "\x1d\x6a\x5a\xf6\xe5\x7c\x86\xce\x7f\x7c\xaf\xd5\xdb\x08\xcd\x59"
  2167. "\x15\x8c\x6d\xb6";
  2168. #else
  2169. const char* large_digest =
  2170. "\x8b\x77\x02\x48\x39\xe8\xdb\xd3\x9a\xf4\x05\x24\x66\x12\x2d\x9e"
  2171. "\xc5\xd9\x0a\xac";
  2172. #endif
  2173. for (i = 0; i < (int)sizeof(large_input); i++) {
  2174. large_input[i] = (byte)(i & 0xFF);
  2175. }
  2176. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  2177. defined(HASH_SIZE_LIMIT)
  2178. times = 20;
  2179. #else
  2180. times = 100;
  2181. #endif
  2182. #ifdef WOLFSSL_PIC32MZ_HASH
  2183. wc_ShaSizeSet(&sha, times * sizeof(large_input));
  2184. #endif
  2185. for (i = 0; i < times; ++i) {
  2186. ret = wc_ShaUpdate(&sha, (byte*)large_input,
  2187. (word32)sizeof(large_input));
  2188. if (ret != 0)
  2189. ERROR_OUT(-1808, exit);
  2190. }
  2191. ret = wc_ShaFinal(&sha, hash);
  2192. if (ret != 0)
  2193. ERROR_OUT(-1809, exit);
  2194. if (XMEMCMP(hash, large_digest, WC_SHA_DIGEST_SIZE) != 0)
  2195. ERROR_OUT(-1810, exit);
  2196. } /* END LARGE HASH TEST */
  2197. #endif /* NO_LARGE_HASH_TEST */
  2198. exit:
  2199. wc_ShaFree(&sha);
  2200. wc_ShaFree(&shaCopy);
  2201. return ret;
  2202. }
  2203. #endif /* NO_SHA */
  2204. #ifdef WOLFSSL_RIPEMD
  2205. WOLFSSL_TEST_SUBROUTINE int ripemd_test(void)
  2206. {
  2207. RipeMd ripemd;
  2208. int ret;
  2209. byte hash[RIPEMD_DIGEST_SIZE];
  2210. testVector a, b, c, d;
  2211. testVector test_ripemd[4];
  2212. int times = sizeof(test_ripemd) / sizeof(struct testVector), i;
  2213. a.input = "abc";
  2214. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  2215. "\xb0\x87\xf1\x5a\x0b\xfc";
  2216. a.inLen = XSTRLEN(a.input);
  2217. a.outLen = RIPEMD_DIGEST_SIZE;
  2218. b.input = "message digest";
  2219. b.output = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
  2220. "\x5f\xfa\x21\x59\x5f\x36";
  2221. b.inLen = XSTRLEN(b.input);
  2222. b.outLen = RIPEMD_DIGEST_SIZE;
  2223. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2224. c.output = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
  2225. "\xf4\x9a\xda\x62\xeb\x2b";
  2226. c.inLen = XSTRLEN(c.input);
  2227. c.outLen = RIPEMD_DIGEST_SIZE;
  2228. d.input = "12345678901234567890123456789012345678901234567890123456"
  2229. "789012345678901234567890";
  2230. d.output = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
  2231. "\x82\xbf\x63\x32\x6b\xfb";
  2232. d.inLen = XSTRLEN(d.input);
  2233. d.outLen = RIPEMD_DIGEST_SIZE;
  2234. test_ripemd[0] = a;
  2235. test_ripemd[1] = b;
  2236. test_ripemd[2] = c;
  2237. test_ripemd[3] = d;
  2238. ret = wc_InitRipeMd(&ripemd);
  2239. if (ret != 0) {
  2240. return -1900;
  2241. }
  2242. for (i = 0; i < times; ++i) {
  2243. ret = wc_RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input,
  2244. (word32)test_ripemd[i].inLen);
  2245. if (ret != 0) {
  2246. return -1901 - i;
  2247. }
  2248. ret = wc_RipeMdFinal(&ripemd, hash);
  2249. if (ret != 0) {
  2250. return -1911 - i;
  2251. }
  2252. if (XMEMCMP(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0)
  2253. return -1921 - i;
  2254. }
  2255. return 0;
  2256. }
  2257. #endif /* WOLFSSL_RIPEMD */
  2258. #ifdef HAVE_BLAKE2
  2259. #define BLAKE2B_TESTS 3
  2260. static const byte blake2b_vec[BLAKE2B_TESTS][BLAKE2B_OUTBYTES] =
  2261. {
  2262. {
  2263. 0x78, 0x6A, 0x02, 0xF7, 0x42, 0x01, 0x59, 0x03,
  2264. 0xC6, 0xC6, 0xFD, 0x85, 0x25, 0x52, 0xD2, 0x72,
  2265. 0x91, 0x2F, 0x47, 0x40, 0xE1, 0x58, 0x47, 0x61,
  2266. 0x8A, 0x86, 0xE2, 0x17, 0xF7, 0x1F, 0x54, 0x19,
  2267. 0xD2, 0x5E, 0x10, 0x31, 0xAF, 0xEE, 0x58, 0x53,
  2268. 0x13, 0x89, 0x64, 0x44, 0x93, 0x4E, 0xB0, 0x4B,
  2269. 0x90, 0x3A, 0x68, 0x5B, 0x14, 0x48, 0xB7, 0x55,
  2270. 0xD5, 0x6F, 0x70, 0x1A, 0xFE, 0x9B, 0xE2, 0xCE
  2271. },
  2272. {
  2273. 0x2F, 0xA3, 0xF6, 0x86, 0xDF, 0x87, 0x69, 0x95,
  2274. 0x16, 0x7E, 0x7C, 0x2E, 0x5D, 0x74, 0xC4, 0xC7,
  2275. 0xB6, 0xE4, 0x8F, 0x80, 0x68, 0xFE, 0x0E, 0x44,
  2276. 0x20, 0x83, 0x44, 0xD4, 0x80, 0xF7, 0x90, 0x4C,
  2277. 0x36, 0x96, 0x3E, 0x44, 0x11, 0x5F, 0xE3, 0xEB,
  2278. 0x2A, 0x3A, 0xC8, 0x69, 0x4C, 0x28, 0xBC, 0xB4,
  2279. 0xF5, 0xA0, 0xF3, 0x27, 0x6F, 0x2E, 0x79, 0x48,
  2280. 0x7D, 0x82, 0x19, 0x05, 0x7A, 0x50, 0x6E, 0x4B
  2281. },
  2282. {
  2283. 0x1C, 0x08, 0x79, 0x8D, 0xC6, 0x41, 0xAB, 0xA9,
  2284. 0xDE, 0xE4, 0x35, 0xE2, 0x25, 0x19, 0xA4, 0x72,
  2285. 0x9A, 0x09, 0xB2, 0xBF, 0xE0, 0xFF, 0x00, 0xEF,
  2286. 0x2D, 0xCD, 0x8E, 0xD6, 0xF8, 0xA0, 0x7D, 0x15,
  2287. 0xEA, 0xF4, 0xAE, 0xE5, 0x2B, 0xBF, 0x18, 0xAB,
  2288. 0x56, 0x08, 0xA6, 0x19, 0x0F, 0x70, 0xB9, 0x04,
  2289. 0x86, 0xC8, 0xA7, 0xD4, 0x87, 0x37, 0x10, 0xB1,
  2290. 0x11, 0x5D, 0x3D, 0xEB, 0xBB, 0x43, 0x27, 0xB5
  2291. }
  2292. };
  2293. WOLFSSL_TEST_SUBROUTINE int blake2b_test(void)
  2294. {
  2295. Blake2b b2b;
  2296. byte digest[64];
  2297. byte input[64];
  2298. int i, ret;
  2299. for (i = 0; i < (int)sizeof(input); i++)
  2300. input[i] = (byte)i;
  2301. for (i = 0; i < BLAKE2B_TESTS; i++) {
  2302. ret = wc_InitBlake2b(&b2b, 64);
  2303. if (ret != 0)
  2304. return -2000 - i;
  2305. ret = wc_Blake2bUpdate(&b2b, input, i);
  2306. if (ret != 0)
  2307. return -2010 - 1;
  2308. ret = wc_Blake2bFinal(&b2b, digest, 64);
  2309. if (ret != 0)
  2310. return -2020 - i;
  2311. if (XMEMCMP(digest, blake2b_vec[i], 64) != 0) {
  2312. return -2030 - i;
  2313. }
  2314. }
  2315. return 0;
  2316. }
  2317. #endif /* HAVE_BLAKE2 */
  2318. #ifdef HAVE_BLAKE2S
  2319. #define BLAKE2S_TESTS 3
  2320. static const byte blake2s_vec[BLAKE2S_TESTS][BLAKE2S_OUTBYTES] =
  2321. {
  2322. {
  2323. 0x69, 0x21, 0x7a, 0x30, 0x79, 0x90, 0x80, 0x94,
  2324. 0xe1, 0x11, 0x21, 0xd0, 0x42, 0x35, 0x4a, 0x7c,
  2325. 0x1f, 0x55, 0xb6, 0x48, 0x2c, 0xa1, 0xa5, 0x1e,
  2326. 0x1b, 0x25, 0x0d, 0xfd, 0x1e, 0xd0, 0xee, 0xf9,
  2327. },
  2328. {
  2329. 0xe3, 0x4d, 0x74, 0xdb, 0xaf, 0x4f, 0xf4, 0xc6,
  2330. 0xab, 0xd8, 0x71, 0xcc, 0x22, 0x04, 0x51, 0xd2,
  2331. 0xea, 0x26, 0x48, 0x84, 0x6c, 0x77, 0x57, 0xfb,
  2332. 0xaa, 0xc8, 0x2f, 0xe5, 0x1a, 0xd6, 0x4b, 0xea,
  2333. },
  2334. {
  2335. 0xdd, 0xad, 0x9a, 0xb1, 0x5d, 0xac, 0x45, 0x49,
  2336. 0xba, 0x42, 0xf4, 0x9d, 0x26, 0x24, 0x96, 0xbe,
  2337. 0xf6, 0xc0, 0xba, 0xe1, 0xdd, 0x34, 0x2a, 0x88,
  2338. 0x08, 0xf8, 0xea, 0x26, 0x7c, 0x6e, 0x21, 0x0c,
  2339. }
  2340. };
  2341. WOLFSSL_TEST_SUBROUTINE int blake2s_test(void)
  2342. {
  2343. Blake2s b2s;
  2344. byte digest[32];
  2345. byte input[64];
  2346. int i, ret;
  2347. for (i = 0; i < (int)sizeof(input); i++)
  2348. input[i] = (byte)i;
  2349. for (i = 0; i < BLAKE2S_TESTS; i++) {
  2350. ret = wc_InitBlake2s(&b2s, 32);
  2351. if (ret != 0)
  2352. return -2100 - i;
  2353. ret = wc_Blake2sUpdate(&b2s, input, i);
  2354. if (ret != 0)
  2355. return -2110 - 1;
  2356. ret = wc_Blake2sFinal(&b2s, digest, 32);
  2357. if (ret != 0)
  2358. return -2120 - i;
  2359. if (XMEMCMP(digest, blake2s_vec[i], 32) != 0) {
  2360. return -2130 - i;
  2361. }
  2362. }
  2363. return 0;
  2364. }
  2365. #endif /* HAVE_BLAKE2S */
  2366. #ifdef WOLFSSL_SHA224
  2367. WOLFSSL_TEST_SUBROUTINE int sha224_test(void)
  2368. {
  2369. wc_Sha224 sha, shaCopy;
  2370. byte hash[WC_SHA224_DIGEST_SIZE];
  2371. byte hashcopy[WC_SHA224_DIGEST_SIZE];
  2372. int ret = 0;
  2373. testVector a, b, c;
  2374. testVector test_sha[3];
  2375. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2376. a.input = "";
  2377. a.output = "\xd1\x4a\x02\x8c\x2a\x3a\x2b\xc9\x47\x61\x02\xbb\x28\x82\x34"
  2378. "\xc4\x15\xa2\xb0\x1f\x82\x8e\xa6\x2a\xc5\xb3\xe4\x2f";
  2379. a.inLen = XSTRLEN(a.input);
  2380. a.outLen = WC_SHA224_DIGEST_SIZE;
  2381. b.input = "abc";
  2382. b.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2\x55"
  2383. "\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  2384. b.inLen = XSTRLEN(b.input);
  2385. b.outLen = WC_SHA224_DIGEST_SIZE;
  2386. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2387. c.output = "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  2388. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  2389. c.inLen = XSTRLEN(c.input);
  2390. c.outLen = WC_SHA224_DIGEST_SIZE;
  2391. test_sha[0] = a;
  2392. test_sha[1] = b;
  2393. test_sha[2] = c;
  2394. ret = wc_InitSha224_ex(&sha, HEAP_HINT, devId);
  2395. if (ret != 0)
  2396. return -2200;
  2397. ret = wc_InitSha224_ex(&shaCopy, HEAP_HINT, devId);
  2398. if (ret != 0) {
  2399. wc_Sha224Free(&sha);
  2400. return -2201;
  2401. }
  2402. for (i = 0; i < times; ++i) {
  2403. ret = wc_Sha224Update(&sha, (byte*)test_sha[i].input,
  2404. (word32)test_sha[i].inLen);
  2405. if (ret != 0)
  2406. ERROR_OUT(-2202 - i, exit);
  2407. ret = wc_Sha224GetHash(&sha, hashcopy);
  2408. if (ret != 0)
  2409. ERROR_OUT(-2203 - i, exit);
  2410. ret = wc_Sha224Copy(&sha, &shaCopy);
  2411. if (ret != 0)
  2412. ERROR_OUT(-2204 - i, exit);
  2413. ret = wc_Sha224Final(&sha, hash);
  2414. if (ret != 0)
  2415. ERROR_OUT(-2205 - i, exit);
  2416. wc_Sha224Free(&shaCopy);
  2417. if (XMEMCMP(hash, test_sha[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  2418. ERROR_OUT(-2206 - i, exit);
  2419. if (XMEMCMP(hash, hashcopy, WC_SHA224_DIGEST_SIZE) != 0)
  2420. ERROR_OUT(-2207 - i, exit);
  2421. }
  2422. exit:
  2423. wc_Sha224Free(&sha);
  2424. wc_Sha224Free(&shaCopy);
  2425. return ret;
  2426. }
  2427. #endif
  2428. #ifndef NO_SHA256
  2429. WOLFSSL_TEST_SUBROUTINE int sha256_test(void)
  2430. {
  2431. wc_Sha256 sha, shaCopy;
  2432. byte hash[WC_SHA256_DIGEST_SIZE];
  2433. byte hashcopy[WC_SHA256_DIGEST_SIZE];
  2434. int ret = 0;
  2435. testVector a, b, c;
  2436. testVector test_sha[3];
  2437. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2438. a.input = "";
  2439. a.output = "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9"
  2440. "\x24\x27\xae\x41\xe4\x64\x9b\x93\x4c\xa4\x95\x99\x1b\x78\x52"
  2441. "\xb8\x55";
  2442. a.inLen = XSTRLEN(a.input);
  2443. a.outLen = WC_SHA256_DIGEST_SIZE;
  2444. b.input = "abc";
  2445. b.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  2446. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  2447. "\x15\xAD";
  2448. b.inLen = XSTRLEN(b.input);
  2449. b.outLen = WC_SHA256_DIGEST_SIZE;
  2450. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2451. c.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  2452. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  2453. "\x06\xC1";
  2454. c.inLen = XSTRLEN(c.input);
  2455. c.outLen = WC_SHA256_DIGEST_SIZE;
  2456. test_sha[0] = a;
  2457. test_sha[1] = b;
  2458. test_sha[2] = c;
  2459. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  2460. if (ret != 0)
  2461. return -2300;
  2462. ret = wc_InitSha256_ex(&shaCopy, HEAP_HINT, devId);
  2463. if (ret != 0) {
  2464. wc_Sha256Free(&sha);
  2465. return -2301;
  2466. }
  2467. for (i = 0; i < times; ++i) {
  2468. ret = wc_Sha256Update(&sha, (byte*)test_sha[i].input,
  2469. (word32)test_sha[i].inLen);
  2470. if (ret != 0) {
  2471. ERROR_OUT(-2302 - i, exit);
  2472. }
  2473. ret = wc_Sha256GetHash(&sha, hashcopy);
  2474. if (ret != 0)
  2475. ERROR_OUT(-2303 - i, exit);
  2476. ret = wc_Sha256Copy(&sha, &shaCopy);
  2477. if (ret != 0)
  2478. ERROR_OUT(-2304 - i, exit);
  2479. ret = wc_Sha256Final(&sha, hash);
  2480. if (ret != 0)
  2481. ERROR_OUT(-2305 - i, exit);
  2482. wc_Sha256Free(&shaCopy);
  2483. if (XMEMCMP(hash, test_sha[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  2484. ERROR_OUT(-2306 - i, exit);
  2485. if (XMEMCMP(hash, hashcopy, WC_SHA256_DIGEST_SIZE) != 0)
  2486. ERROR_OUT(-2307 - i, exit);
  2487. }
  2488. #ifndef NO_LARGE_HASH_TEST
  2489. /* BEGIN LARGE HASH TEST */ {
  2490. byte large_input[1024];
  2491. #ifdef HASH_SIZE_LIMIT
  2492. const char* large_digest =
  2493. "\xa4\x75\x9e\x7a\xa2\x03\x38\x32\x88\x66\xa2\xea\x17\xea\xf8\xc7"
  2494. "\xfe\x4e\xc6\xbb\xe3\xbb\x71\xce\xe7\xdf\x7c\x04\x61\xb3\xc2\x2f";
  2495. #else
  2496. const char* large_digest =
  2497. "\x27\x78\x3e\x87\x96\x3a\x4e\xfb\x68\x29\xb5\x31\xc9\xba\x57\xb4"
  2498. "\x4f\x45\x79\x7f\x67\x70\xbd\x63\x7f\xbf\x0d\x80\x7c\xbd\xba\xe0";
  2499. #endif
  2500. for (i = 0; i < (int)sizeof(large_input); i++) {
  2501. large_input[i] = (byte)(i & 0xFF);
  2502. }
  2503. #ifdef HASH_SIZE_LIMIT
  2504. times = 20;
  2505. #else
  2506. times = 100;
  2507. #endif
  2508. #ifdef WOLFSSL_PIC32MZ_HASH
  2509. wc_Sha256SizeSet(&sha, times * sizeof(large_input));
  2510. #endif
  2511. for (i = 0; i < times; ++i) {
  2512. ret = wc_Sha256Update(&sha, (byte*)large_input,
  2513. (word32)sizeof(large_input));
  2514. if (ret != 0)
  2515. ERROR_OUT(-2308, exit);
  2516. }
  2517. ret = wc_Sha256Final(&sha, hash);
  2518. if (ret != 0)
  2519. ERROR_OUT(-2309, exit);
  2520. if (XMEMCMP(hash, large_digest, WC_SHA256_DIGEST_SIZE) != 0)
  2521. ERROR_OUT(-2310, exit);
  2522. } /* END LARGE HASH TEST */
  2523. #endif /* NO_LARGE_HASH_TEST */
  2524. exit:
  2525. wc_Sha256Free(&sha);
  2526. wc_Sha256Free(&shaCopy);
  2527. return ret;
  2528. }
  2529. #endif
  2530. #ifdef WOLFSSL_SHA512
  2531. WOLFSSL_TEST_SUBROUTINE int sha512_test(void)
  2532. {
  2533. wc_Sha512 sha, shaCopy;
  2534. byte hash[WC_SHA512_DIGEST_SIZE];
  2535. byte hashcopy[WC_SHA512_DIGEST_SIZE];
  2536. int ret = 0;
  2537. testVector a, b, c;
  2538. testVector test_sha[3];
  2539. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2540. a.input = "";
  2541. a.output = "\xcf\x83\xe1\x35\x7e\xef\xb8\xbd\xf1\x54\x28\x50\xd6\x6d\x80"
  2542. "\x07\xd6\x20\xe4\x05\x0b\x57\x15\xdc\x83\xf4\xa9\x21\xd3\x6c"
  2543. "\xe9\xce\x47\xd0\xd1\x3c\x5d\x85\xf2\xb0\xff\x83\x18\xd2\x87"
  2544. "\x7e\xec\x2f\x63\xb9\x31\xbd\x47\x41\x7a\x81\xa5\x38\x32\x7a"
  2545. "\xf9\x27\xda\x3e";
  2546. a.inLen = XSTRLEN(a.input);
  2547. a.outLen = WC_SHA512_DIGEST_SIZE;
  2548. b.input = "abc";
  2549. b.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  2550. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  2551. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  2552. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  2553. "\xa5\x4c\xa4\x9f";
  2554. b.inLen = XSTRLEN(b.input);
  2555. b.outLen = WC_SHA512_DIGEST_SIZE;
  2556. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  2557. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  2558. c.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  2559. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  2560. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  2561. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  2562. "\x87\x4b\xe9\x09";
  2563. c.inLen = XSTRLEN(c.input);
  2564. c.outLen = WC_SHA512_DIGEST_SIZE;
  2565. test_sha[0] = a;
  2566. test_sha[1] = b;
  2567. test_sha[2] = c;
  2568. ret = wc_InitSha512_ex(&sha, HEAP_HINT, devId);
  2569. if (ret != 0)
  2570. return -2400;
  2571. ret = wc_InitSha512_ex(&shaCopy, HEAP_HINT, devId);
  2572. if (ret != 0) {
  2573. wc_Sha512Free(&sha);
  2574. return -2401;
  2575. }
  2576. for (i = 0; i < times; ++i) {
  2577. ret = wc_Sha512Update(&sha, (byte*)test_sha[i].input,
  2578. (word32)test_sha[i].inLen);
  2579. if (ret != 0)
  2580. ERROR_OUT(-2402 - i, exit);
  2581. ret = wc_Sha512GetHash(&sha, hashcopy);
  2582. if (ret != 0)
  2583. ERROR_OUT(-2403 - i, exit);
  2584. ret = wc_Sha512Copy(&sha, &shaCopy);
  2585. if (ret != 0)
  2586. ERROR_OUT(-2404 - i, exit);
  2587. ret = wc_Sha512Final(&sha, hash);
  2588. if (ret != 0)
  2589. ERROR_OUT(-2405 - i, exit);
  2590. wc_Sha512Free(&shaCopy);
  2591. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  2592. ERROR_OUT(-2406 - i, exit);
  2593. if (XMEMCMP(hash, hashcopy, WC_SHA512_DIGEST_SIZE) != 0)
  2594. ERROR_OUT(-2407 - i, exit);
  2595. }
  2596. #ifndef NO_LARGE_HASH_TEST
  2597. /* BEGIN LARGE HASH TEST */ {
  2598. byte large_input[1024];
  2599. #ifdef HASH_SIZE_LIMIT
  2600. const char* large_digest =
  2601. "\x30\x9B\x96\xA6\xE9\x43\x78\x30\xA3\x71\x51\x61\xC1\xEB\xE1\xBE"
  2602. "\xC8\xA5\xF9\x13\x5A\xD6\x6D\x9E\x46\x31\x31\x67\x8D\xE2\xC0\x0B"
  2603. "\x2A\x1A\x03\xE1\xF3\x48\xA7\x33\xBD\x49\xF8\xFF\xF1\xC2\xC2\x95"
  2604. "\xCB\xF0\xAF\x87\x61\x85\x58\x63\x6A\xCA\x70\x9C\x8B\x83\x3F\x5D";
  2605. #else
  2606. const char* large_digest =
  2607. "\x5a\x1f\x73\x90\xbd\x8c\xe4\x63\x54\xce\xa0\x9b\xef\x32\x78\x2d"
  2608. "\x2e\xe7\x0d\x5e\x2f\x9d\x15\x1b\xdd\x2d\xde\x65\x0c\x7b\xfa\x83"
  2609. "\x5e\x80\x02\x13\x84\xb8\x3f\xff\x71\x62\xb5\x09\x89\x63\xe1\xdc"
  2610. "\xa5\xdc\xfc\xfa\x9d\x1a\x4d\xc0\xfa\x3a\x14\xf6\x01\x51\x90\xa4";
  2611. #endif
  2612. for (i = 0; i < (int)sizeof(large_input); i++) {
  2613. large_input[i] = (byte)(i & 0xFF);
  2614. }
  2615. #ifdef HASH_SIZE_LIMIT
  2616. times = 20;
  2617. #else
  2618. times = 100;
  2619. #endif
  2620. for (i = 0; i < times; ++i) {
  2621. ret = wc_Sha512Update(&sha, (byte*)large_input,
  2622. (word32)sizeof(large_input));
  2623. if (ret != 0)
  2624. ERROR_OUT(-2408, exit);
  2625. }
  2626. ret = wc_Sha512Final(&sha, hash);
  2627. if (ret != 0)
  2628. ERROR_OUT(-2409, exit);
  2629. if (XMEMCMP(hash, large_digest, WC_SHA512_DIGEST_SIZE) != 0)
  2630. ERROR_OUT(-2410, exit);
  2631. #ifndef NO_UNALIGNED_MEMORY_TEST
  2632. /* Unaligned memory access test */
  2633. for (i = 1; i < 16; i++) {
  2634. ret = wc_Sha512Update(&sha, (byte*)large_input + i,
  2635. (word32)sizeof(large_input) - i);
  2636. if (ret != 0)
  2637. ERROR_OUT(-2411, exit);
  2638. ret = wc_Sha512Final(&sha, hash);
  2639. }
  2640. #endif
  2641. } /* END LARGE HASH TEST */
  2642. #endif /* NO_LARGE_HASH_TEST */
  2643. exit:
  2644. wc_Sha512Free(&sha);
  2645. wc_Sha512Free(&shaCopy);
  2646. return ret;
  2647. }
  2648. #endif
  2649. #ifdef WOLFSSL_SHA384
  2650. WOLFSSL_TEST_SUBROUTINE int sha384_test(void)
  2651. {
  2652. wc_Sha384 sha, shaCopy;
  2653. byte hash[WC_SHA384_DIGEST_SIZE];
  2654. byte hashcopy[WC_SHA384_DIGEST_SIZE];
  2655. int ret = 0;
  2656. testVector a, b, c;
  2657. testVector test_sha[3];
  2658. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2659. a.input = "";
  2660. a.output = "\x38\xb0\x60\xa7\x51\xac\x96\x38\x4c\xd9\x32\x7e\xb1\xb1\xe3"
  2661. "\x6a\x21\xfd\xb7\x11\x14\xbe\x07\x43\x4c\x0c\xc7\xbf\x63\xf6"
  2662. "\xe1\xda\x27\x4e\xde\xbf\xe7\x6f\x65\xfb\xd5\x1a\xd2\xf1\x48"
  2663. "\x98\xb9\x5b";
  2664. a.inLen = XSTRLEN(a.input);
  2665. a.outLen = WC_SHA384_DIGEST_SIZE;
  2666. b.input = "abc";
  2667. b.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  2668. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  2669. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  2670. "\xc8\x25\xa7";
  2671. b.inLen = XSTRLEN(b.input);
  2672. b.outLen = WC_SHA384_DIGEST_SIZE;
  2673. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  2674. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  2675. c.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  2676. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  2677. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  2678. "\x74\x60\x39";
  2679. c.inLen = XSTRLEN(c.input);
  2680. c.outLen = WC_SHA384_DIGEST_SIZE;
  2681. test_sha[0] = a;
  2682. test_sha[1] = b;
  2683. test_sha[2] = c;
  2684. ret = wc_InitSha384_ex(&sha, HEAP_HINT, devId);
  2685. if (ret != 0)
  2686. return -2500;
  2687. ret = wc_InitSha384_ex(&shaCopy, HEAP_HINT, devId);
  2688. if (ret != 0) {
  2689. wc_Sha384Free(&sha);
  2690. return -2501;
  2691. }
  2692. for (i = 0; i < times; ++i) {
  2693. ret = wc_Sha384Update(&sha, (byte*)test_sha[i].input,
  2694. (word32)test_sha[i].inLen);
  2695. if (ret != 0)
  2696. ERROR_OUT(-2502 - i, exit);
  2697. ret = wc_Sha384GetHash(&sha, hashcopy);
  2698. if (ret != 0)
  2699. ERROR_OUT(-2503 - i, exit);
  2700. ret = wc_Sha384Copy(&sha, &shaCopy);
  2701. if (ret != 0)
  2702. ERROR_OUT(-2504 - i, exit);
  2703. ret = wc_Sha384Final(&sha, hash);
  2704. if (ret != 0)
  2705. ERROR_OUT(-2505 - i, exit);
  2706. wc_Sha384Free(&shaCopy);
  2707. if (XMEMCMP(hash, test_sha[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  2708. ERROR_OUT(-2506 - i, exit);
  2709. if (XMEMCMP(hash, hashcopy, WC_SHA384_DIGEST_SIZE) != 0)
  2710. ERROR_OUT(-2507 - i, exit);
  2711. }
  2712. #ifndef NO_LARGE_HASH_TEST
  2713. /* BEGIN LARGE HASH TEST */ {
  2714. byte large_input[1024];
  2715. #ifdef HASH_SIZE_LIMIT
  2716. const char* large_digest =
  2717. "\xB5\xAD\x66\x6F\xD9\x58\x5E\x68\xDD\x5E\x30\xD3\x95\x72\x33\xA4"
  2718. "\xE9\x4B\x99\x3A\xEF\xF8\xE1\xBF\x1F\x05\x32\xAA\x16\x00\x82\xEC"
  2719. "\x15\xDA\xF2\x75\xEE\xE9\x06\xAF\x52\x8A\x5C\xEF\x72\x81\x80\xD6";
  2720. #else
  2721. const char* large_digest =
  2722. "\x37\x01\xdb\xff\x1e\x40\x4f\xe1\xe2\xea\x0b\x40\xbb\x3b\x39\x9a"
  2723. "\xcc\xe8\x44\x8e\x7e\xe5\x64\xb5\x6b\x7f\x56\x64\xa7\x2b\x84\xe3"
  2724. "\xc5\xd7\x79\x03\x25\x90\xf7\xa4\x58\xcb\x97\xa8\x8b\xb1\xa4\x81";
  2725. #endif
  2726. for (i = 0; i < (int)sizeof(large_input); i++) {
  2727. large_input[i] = (byte)(i & 0xFF);
  2728. }
  2729. #ifdef HASH_SIZE_LIMIT
  2730. times = 20;
  2731. #else
  2732. times = 100;
  2733. #endif
  2734. for (i = 0; i < times; ++i) {
  2735. ret = wc_Sha384Update(&sha, (byte*)large_input,
  2736. (word32)sizeof(large_input));
  2737. if (ret != 0)
  2738. ERROR_OUT(-2508, exit);
  2739. }
  2740. ret = wc_Sha384Final(&sha, hash);
  2741. if (ret != 0)
  2742. ERROR_OUT(-2509, exit);
  2743. if (XMEMCMP(hash, large_digest, WC_SHA384_DIGEST_SIZE) != 0)
  2744. ERROR_OUT(-2510, exit);
  2745. } /* END LARGE HASH TEST */
  2746. #endif /* NO_LARGE_HASH_TEST */
  2747. exit:
  2748. wc_Sha384Free(&sha);
  2749. wc_Sha384Free(&shaCopy);
  2750. return ret;
  2751. }
  2752. #endif /* WOLFSSL_SHA384 */
  2753. #ifdef WOLFSSL_SHA3
  2754. #ifndef WOLFSSL_NOSHA3_224
  2755. static int sha3_224_test(void)
  2756. {
  2757. wc_Sha3 sha;
  2758. byte hash[WC_SHA3_224_DIGEST_SIZE];
  2759. byte hashcopy[WC_SHA3_224_DIGEST_SIZE];
  2760. testVector a, b, c;
  2761. testVector test_sha[3];
  2762. int ret = 0;
  2763. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2764. a.input = "";
  2765. a.output = "\x6b\x4e\x03\x42\x36\x67\xdb\xb7\x3b\x6e\x15\x45\x4f\x0e\xb1"
  2766. "\xab\xd4\x59\x7f\x9a\x1b\x07\x8e\x3f\x5b\x5a\x6b\xc7";
  2767. a.inLen = XSTRLEN(a.input);
  2768. a.outLen = WC_SHA3_224_DIGEST_SIZE;
  2769. b.input = "abc";
  2770. b.output = "\xe6\x42\x82\x4c\x3f\x8c\xf2\x4a\xd0\x92\x34\xee\x7d\x3c\x76"
  2771. "\x6f\xc9\xa3\xa5\x16\x8d\x0c\x94\xad\x73\xb4\x6f\xdf";
  2772. b.inLen = XSTRLEN(b.input);
  2773. b.outLen = WC_SHA3_224_DIGEST_SIZE;
  2774. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2775. c.output = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55\x74\x49\x44\x79"
  2776. "\xba\x5c\x7e\x7a\xb7\x6e\xf2\x64\xea\xd0\xfc\xce\x33";
  2777. c.inLen = XSTRLEN(c.input);
  2778. c.outLen = WC_SHA3_224_DIGEST_SIZE;
  2779. test_sha[0] = a;
  2780. test_sha[1] = b;
  2781. test_sha[2] = c;
  2782. ret = wc_InitSha3_224(&sha, HEAP_HINT, devId);
  2783. if (ret != 0)
  2784. return -2600;
  2785. for (i = 0; i < times; ++i) {
  2786. ret = wc_Sha3_224_Update(&sha, (byte*)test_sha[i].input,
  2787. (word32)test_sha[i].inLen);
  2788. if (ret != 0)
  2789. ERROR_OUT(-2601 - i, exit);
  2790. ret = wc_Sha3_224_GetHash(&sha, hashcopy);
  2791. if (ret != 0)
  2792. ERROR_OUT(-2602 - i, exit);
  2793. ret = wc_Sha3_224_Final(&sha, hash);
  2794. if (ret != 0)
  2795. ERROR_OUT(-2603 - i, exit);
  2796. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_224_DIGEST_SIZE) != 0)
  2797. ERROR_OUT(-2604 - i, exit);
  2798. if (XMEMCMP(hash, hashcopy, WC_SHA3_224_DIGEST_SIZE) != 0)
  2799. ERROR_OUT(-2605 - i, exit);
  2800. }
  2801. #ifndef NO_LARGE_HASH_TEST
  2802. /* BEGIN LARGE HASH TEST */ {
  2803. byte large_input[1024];
  2804. const char* large_digest =
  2805. "\x13\xe5\xd3\x98\x7b\x94\xda\x41\x12\xc7\x1e\x92\x3a\x19"
  2806. "\x21\x20\x86\x6f\x24\xbf\x0a\x31\xbc\xfd\xd6\x70\x36\xf3";
  2807. for (i = 0; i < (int)sizeof(large_input); i++) {
  2808. large_input[i] = (byte)(i & 0xFF);
  2809. }
  2810. times = 100;
  2811. for (i = 0; i < times; ++i) {
  2812. ret = wc_Sha3_224_Update(&sha, (byte*)large_input,
  2813. (word32)sizeof(large_input));
  2814. if (ret != 0)
  2815. ERROR_OUT(-2606, exit);
  2816. }
  2817. ret = wc_Sha3_224_Final(&sha, hash);
  2818. if (ret != 0)
  2819. ERROR_OUT(-2607, exit);
  2820. if (XMEMCMP(hash, large_digest, WC_SHA3_224_DIGEST_SIZE) != 0)
  2821. ERROR_OUT(-2608, exit);
  2822. } /* END LARGE HASH TEST */
  2823. #endif /* NO_LARGE_HASH_TEST */
  2824. exit:
  2825. wc_Sha3_224_Free(&sha);
  2826. return ret;
  2827. }
  2828. #endif /* WOLFSSL_NOSHA3_224 */
  2829. #ifndef WOLFSSL_NOSHA3_256
  2830. static int sha3_256_test(void)
  2831. {
  2832. wc_Sha3 sha;
  2833. byte hash[WC_SHA3_256_DIGEST_SIZE];
  2834. byte hashcopy[WC_SHA3_256_DIGEST_SIZE];
  2835. testVector a, b, c;
  2836. testVector test_sha[3];
  2837. int ret = 0;
  2838. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2839. byte large_input[1024];
  2840. const char* large_digest =
  2841. "\xdc\x90\xc0\xb1\x25\xdb\x2c\x34\x81\xa3\xff\xbc\x1e\x2e\x87\xeb"
  2842. "\x6d\x70\x85\x61\xe0\xe9\x63\x61\xff\xe5\x84\x4b\x1f\x68\x05\x15";
  2843. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  2844. /* test vector with hash of empty string */
  2845. const char* Keccak256EmptyOut =
  2846. "\xc5\xd2\x46\x01\x86\xf7\x23\x3c\x92\x7e\x7d\xb2\xdc\xc7\x03\xc0"
  2847. "\xe5\x00\xb6\x53\xca\x82\x27\x3b\x7b\xfa\xd8\x04\x5d\x85\xa4\x70";
  2848. #endif
  2849. a.input = "";
  2850. a.output = "\xa7\xff\xc6\xf8\xbf\x1e\xd7\x66\x51\xc1\x47\x56\xa0\x61\xd6"
  2851. "\x62\xf5\x80\xff\x4d\xe4\x3b\x49\xfa\x82\xd8\x0a\x4b\x80\xf8"
  2852. "\x43\x4a";
  2853. a.inLen = XSTRLEN(a.input);
  2854. a.outLen = WC_SHA3_256_DIGEST_SIZE;
  2855. b.input = "abc";
  2856. b.output = "\x3a\x98\x5d\xa7\x4f\xe2\x25\xb2\x04\x5c\x17\x2d\x6b\xd3\x90"
  2857. "\xbd\x85\x5f\x08\x6e\x3e\x9d\x52\x5b\x46\xbf\xe2\x45\x11\x43"
  2858. "\x15\x32";
  2859. b.inLen = XSTRLEN(b.input);
  2860. b.outLen = WC_SHA3_256_DIGEST_SIZE;
  2861. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2862. c.output = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8\x23\x5e"
  2863. "\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32\xdd\x97\x49\x6d"
  2864. "\x33\x76";
  2865. c.inLen = XSTRLEN(c.input);
  2866. c.outLen = WC_SHA3_256_DIGEST_SIZE;
  2867. test_sha[0] = a;
  2868. test_sha[1] = b;
  2869. test_sha[2] = c;
  2870. ret = wc_InitSha3_256(&sha, HEAP_HINT, devId);
  2871. if (ret != 0)
  2872. return -2700;
  2873. for (i = 0; i < times; ++i) {
  2874. ret = wc_Sha3_256_Update(&sha, (byte*)test_sha[i].input,
  2875. (word32)test_sha[i].inLen);
  2876. if (ret != 0)
  2877. ERROR_OUT(-2701 - i, exit);
  2878. ret = wc_Sha3_256_GetHash(&sha, hashcopy);
  2879. if (ret != 0)
  2880. ERROR_OUT(-2702 - i, exit);
  2881. ret = wc_Sha3_256_Final(&sha, hash);
  2882. if (ret != 0)
  2883. ERROR_OUT(-2703 - i, exit);
  2884. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_256_DIGEST_SIZE) != 0)
  2885. ERROR_OUT(-2704 - i, exit);
  2886. if (XMEMCMP(hash, hashcopy, WC_SHA3_256_DIGEST_SIZE) != 0)
  2887. ERROR_OUT(-2705 - i, exit);
  2888. }
  2889. #ifndef NO_LARGE_HASH_TEST
  2890. /* BEGIN LARGE HASH TEST */ {
  2891. for (i = 0; i < (int)sizeof(large_input); i++) {
  2892. large_input[i] = (byte)(i & 0xFF);
  2893. }
  2894. times = 100;
  2895. for (i = 0; i < times; ++i) {
  2896. ret = wc_Sha3_256_Update(&sha, (byte*)large_input,
  2897. (word32)sizeof(large_input));
  2898. if (ret != 0)
  2899. ERROR_OUT(-2706, exit);
  2900. }
  2901. ret = wc_Sha3_256_Final(&sha, hash);
  2902. if (ret != 0)
  2903. ERROR_OUT(-2707, exit);
  2904. if (XMEMCMP(hash, large_digest, WC_SHA3_256_DIGEST_SIZE) != 0)
  2905. ERROR_OUT(-2708, exit);
  2906. } /* END LARGE HASH TEST */
  2907. #endif /* NO_LARGE_HASH_TEST */
  2908. /* this is a software only variant of SHA3 not supported by external hardware devices */
  2909. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  2910. /* Test for Keccak256 */
  2911. ret = wc_Sha3_SetFlags(&sha, WC_HASH_SHA3_KECCAK256);
  2912. if (ret != 0) {
  2913. ERROR_OUT(-2709, exit);
  2914. }
  2915. ret = wc_Sha3_256_Update(&sha, (byte*)"", 0);
  2916. if (ret != 0) {
  2917. ERROR_OUT(-2710, exit);
  2918. }
  2919. ret = wc_Sha3_256_Final(&sha, hash);
  2920. if (ret != 0) {
  2921. ERROR_OUT(-2711, exit);
  2922. }
  2923. if (XMEMCMP(hash, Keccak256EmptyOut, WC_SHA3_256_DIGEST_SIZE) != 0) {
  2924. ERROR_OUT(-2712, exit);
  2925. }
  2926. #endif /* WOLFSSL_HASH_FLAGS && !WOLFSSL_ASYNC_CRYPT */
  2927. exit:
  2928. wc_Sha3_256_Free(&sha);
  2929. return ret;
  2930. }
  2931. #endif /* WOLFSSL_NOSHA3_256 */
  2932. #ifndef WOLFSSL_NOSHA3_384
  2933. static int sha3_384_test(void)
  2934. {
  2935. wc_Sha3 sha;
  2936. byte hash[WC_SHA3_384_DIGEST_SIZE];
  2937. byte buf[64];
  2938. #ifndef NO_INTM_HASH_TEST
  2939. byte hashcopy[WC_SHA3_384_DIGEST_SIZE];
  2940. #endif
  2941. testVector a, b, c;
  2942. testVector test_sha[3];
  2943. int ret;
  2944. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2945. a.input = "";
  2946. a.output = "\x0c\x63\xa7\x5b\x84\x5e\x4f\x7d\x01\x10\x7d\x85\x2e\x4c\x24"
  2947. "\x85\xc5\x1a\x50\xaa\xaa\x94\xfc\x61\x99\x5e\x71\xbb\xee\x98"
  2948. "\x3a\x2a\xc3\x71\x38\x31\x26\x4a\xdb\x47\xfb\x6b\xd1\xe0\x58"
  2949. "\xd5\xf0\x04";
  2950. a.inLen = XSTRLEN(a.input);
  2951. a.outLen = WC_SHA3_384_DIGEST_SIZE;
  2952. #if defined(WOLFSSL_AFALG_XILINX_SHA3) || defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  2953. /* NIST test vector with a length that is a multiple of 4 */
  2954. b.input = "\x7d\x80\xb1\x60\xc4\xb5\x36\xa3\xbe\xb7\x99\x80\x59\x93\x44"
  2955. "\x04\x7c\x5f\x82\xa1\xdf\xc3\xee\xd4";
  2956. b.output = "\x04\x1c\xc5\x86\x1b\xa3\x34\x56\x3c\x61\xd4\xef\x97\x10\xd4"
  2957. "\x89\x6c\x31\x1c\x92\xed\xbe\x0d\x7c\xd5\x3e\x80\x3b\xf2\xf4"
  2958. "\xeb\x60\x57\x23\x55\x70\x77\x0c\xe8\x7c\x55\x20\xd7\xec\x14"
  2959. "\x19\x87\x22";
  2960. b.inLen = XSTRLEN(b.input);
  2961. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  2962. #else
  2963. b.input = "abc";
  2964. b.output = "\xec\x01\x49\x82\x88\x51\x6f\xc9\x26\x45\x9f\x58\xe2\xc6\xad"
  2965. "\x8d\xf9\xb4\x73\xcb\x0f\xc0\x8c\x25\x96\xda\x7c\xf0\xe4\x9b"
  2966. "\xe4\xb2\x98\xd8\x8c\xea\x92\x7a\xc7\xf5\x39\xf1\xed\xf2\x28"
  2967. "\x37\x6d\x25";
  2968. b.inLen = XSTRLEN(b.input);
  2969. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  2970. #endif
  2971. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2972. c.output = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7\x8a\x49"
  2973. "\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd\xbc\x32\xb9\xd4"
  2974. "\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe\xa1\x9e\xef\x51\xac\xd0"
  2975. "\x65\x7c\x22";
  2976. c.inLen = XSTRLEN(c.input);
  2977. c.outLen = WC_SHA3_384_DIGEST_SIZE;
  2978. #if defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  2979. test_sha[0] = b; /* hardware acc. pre-Versal can not handle "" string */
  2980. #else
  2981. test_sha[0] = a;
  2982. #endif
  2983. test_sha[1] = b;
  2984. test_sha[2] = c;
  2985. ret = wc_InitSha3_384(&sha, HEAP_HINT, devId);
  2986. if (ret != 0)
  2987. return -2800;
  2988. for (i = 0; i < times; ++i) {
  2989. XMEMCPY(buf, test_sha[i].input, test_sha[i].inLen);
  2990. ret = wc_Sha3_384_Update(&sha, buf,
  2991. (word32)test_sha[i].inLen);
  2992. if (ret != 0)
  2993. ERROR_OUT(-2801 - (i * 10), exit);
  2994. #ifndef NO_INTM_HASH_TEST
  2995. ret = wc_Sha3_384_GetHash(&sha, hashcopy);
  2996. if (ret != 0)
  2997. ERROR_OUT(-2802 - (i * 10), exit);
  2998. #endif
  2999. ret = wc_Sha3_384_Final(&sha, hash);
  3000. if (ret != 0)
  3001. ERROR_OUT(-2803 - (i * 10), exit);
  3002. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_384_DIGEST_SIZE) != 0)
  3003. ERROR_OUT(-2804 - (i * 10), exit);
  3004. #ifndef NO_INTM_HASH_TEST
  3005. if (XMEMCMP(hash, hashcopy, WC_SHA3_384_DIGEST_SIZE) != 0)
  3006. ERROR_OUT(-2805 - (i * 10), exit);
  3007. #endif
  3008. }
  3009. #ifndef NO_LARGE_HASH_TEST
  3010. /* BEGIN LARGE HASH TEST */ {
  3011. byte large_input[1024];
  3012. const char* large_digest =
  3013. "\x30\x44\xec\x17\xef\x47\x9f\x55\x36\x11\xd6\x3f\x8a\x31\x5a\x71"
  3014. "\x8a\x71\xa7\x1d\x8e\x84\xe8\x6c\x24\x02\x2f\x7a\x08\x4e\xea\xd7"
  3015. "\x42\x36\x5d\xa8\xc2\xb7\x42\xad\xec\x19\xfb\xca\xc6\x64\xb3\xa4";
  3016. for (i = 0; i < (int)sizeof(large_input); i++) {
  3017. large_input[i] = (byte)(i & 0xFF);
  3018. }
  3019. times = 100;
  3020. for (i = 0; i < times; ++i) {
  3021. ret = wc_Sha3_384_Update(&sha, (byte*)large_input,
  3022. (word32)sizeof(large_input));
  3023. if (ret != 0)
  3024. ERROR_OUT(-2806, exit);
  3025. }
  3026. ret = wc_Sha3_384_Final(&sha, hash);
  3027. if (ret != 0)
  3028. ERROR_OUT(-2807, exit);
  3029. if (XMEMCMP(hash, large_digest, WC_SHA3_384_DIGEST_SIZE) != 0)
  3030. ERROR_OUT(-2808, exit);
  3031. } /* END LARGE HASH TEST */
  3032. #endif /* NO_LARGE_HASH_TEST */
  3033. exit:
  3034. wc_Sha3_384_Free(&sha);
  3035. return ret;
  3036. }
  3037. #endif /* WOLFSSL_NOSHA3_384 */
  3038. #ifndef WOLFSSL_NOSHA3_512
  3039. static int sha3_512_test(void)
  3040. {
  3041. wc_Sha3 sha;
  3042. byte hash[WC_SHA3_512_DIGEST_SIZE];
  3043. byte hashcopy[WC_SHA3_512_DIGEST_SIZE];
  3044. testVector a, b, c;
  3045. testVector test_sha[3];
  3046. int ret;
  3047. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3048. a.input = "";
  3049. a.output = "\xa6\x9f\x73\xcc\xa2\x3a\x9a\xc5\xc8\xb5\x67\xdc\x18\x5a\x75"
  3050. "\x6e\x97\xc9\x82\x16\x4f\xe2\x58\x59\xe0\xd1\xdc\xc1\x47\x5c"
  3051. "\x80\xa6\x15\xb2\x12\x3a\xf1\xf5\xf9\x4c\x11\xe3\xe9\x40\x2c"
  3052. "\x3a\xc5\x58\xf5\x00\x19\x9d\x95\xb6\xd3\xe3\x01\x75\x85\x86"
  3053. "\x28\x1d\xcd\x26";
  3054. a.inLen = XSTRLEN(a.input);
  3055. a.outLen = WC_SHA3_512_DIGEST_SIZE;
  3056. b.input = "abc";
  3057. b.output = "\xb7\x51\x85\x0b\x1a\x57\x16\x8a\x56\x93\xcd\x92\x4b\x6b\x09"
  3058. "\x6e\x08\xf6\x21\x82\x74\x44\xf7\x0d\x88\x4f\x5d\x02\x40\xd2"
  3059. "\x71\x2e\x10\xe1\x16\xe9\x19\x2a\xf3\xc9\x1a\x7e\xc5\x76\x47"
  3060. "\xe3\x93\x40\x57\x34\x0b\x4c\xf4\x08\xd5\xa5\x65\x92\xf8\x27"
  3061. "\x4e\xec\x53\xf0";
  3062. b.inLen = XSTRLEN(b.input);
  3063. b.outLen = WC_SHA3_512_DIGEST_SIZE;
  3064. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3065. c.output = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10\xfc\xa8"
  3066. "\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7\xec\x2f\x1e\x91"
  3067. "\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53\x02\xba\x1b\x0d\x8d\xc7"
  3068. "\x8c\x08\x63\x46\xb5\x33\xb4\x9c\x03\x0d\x99\xa2\x7d\xaf\x11"
  3069. "\x39\xd6\xe7\x5e";
  3070. c.inLen = XSTRLEN(c.input);
  3071. c.outLen = WC_SHA3_512_DIGEST_SIZE;
  3072. test_sha[0] = a;
  3073. test_sha[1] = b;
  3074. test_sha[2] = c;
  3075. ret = wc_InitSha3_512(&sha, HEAP_HINT, devId);
  3076. if (ret != 0)
  3077. return -2900;
  3078. for (i = 0; i < times; ++i) {
  3079. ret = wc_Sha3_512_Update(&sha, (byte*)test_sha[i].input,
  3080. (word32)test_sha[i].inLen);
  3081. if (ret != 0)
  3082. ERROR_OUT(-2901 - i, exit);
  3083. ret = wc_Sha3_512_GetHash(&sha, hashcopy);
  3084. if (ret != 0)
  3085. ERROR_OUT(-2902 - i, exit);
  3086. ret = wc_Sha3_512_Final(&sha, hash);
  3087. if (ret != 0)
  3088. ERROR_OUT(-2903 - i, exit);
  3089. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_512_DIGEST_SIZE) != 0)
  3090. ERROR_OUT(-2904 - i, exit);
  3091. if (XMEMCMP(hash, hashcopy, WC_SHA3_512_DIGEST_SIZE) != 0)
  3092. ERROR_OUT(-2905 - i, exit);
  3093. }
  3094. #ifndef NO_LARGE_HASH_TEST
  3095. /* BEGIN LARGE HASH TEST */ {
  3096. byte large_input[1024];
  3097. const char* large_digest =
  3098. "\x9c\x13\x26\xb6\x26\xb2\x94\x31\xbc\xf4\x34\xe9\x6f\xf2\xd6\x29"
  3099. "\x9a\xd0\x9b\x32\x63\x2f\x18\xa7\x5f\x23\xc9\x60\xc2\x32\x0c\xbc"
  3100. "\x57\x77\x33\xf1\x83\x81\x8a\xd3\x15\x7c\x93\xdc\x80\x9f\xed\x61"
  3101. "\x41\xa7\x5b\xfd\x32\x0e\x38\x15\xb0\x46\x3b\x7a\x4f\xfd\x44\x88";
  3102. for (i = 0; i < (int)sizeof(large_input); i++) {
  3103. large_input[i] = (byte)(i & 0xFF);
  3104. }
  3105. times = 100;
  3106. for (i = 0; i < times; ++i) {
  3107. ret = wc_Sha3_512_Update(&sha, (byte*)large_input,
  3108. (word32)sizeof(large_input));
  3109. if (ret != 0)
  3110. ERROR_OUT(-2906, exit);
  3111. }
  3112. ret = wc_Sha3_512_Final(&sha, hash);
  3113. if (ret != 0)
  3114. ERROR_OUT(-2907, exit);
  3115. if (XMEMCMP(hash, large_digest, WC_SHA3_512_DIGEST_SIZE) != 0)
  3116. ERROR_OUT(-2908, exit);
  3117. } /* END LARGE HASH TEST */
  3118. #endif /* NO_LARGE_HASH_TEST */
  3119. exit:
  3120. wc_Sha3_512_Free(&sha);
  3121. return ret;
  3122. }
  3123. #endif /* WOLFSSL_NOSHA3_512 */
  3124. WOLFSSL_TEST_SUBROUTINE int sha3_test(void)
  3125. {
  3126. int ret;
  3127. (void)ret;
  3128. #ifndef WOLFSSL_NOSHA3_224
  3129. if ((ret = sha3_224_test()) != 0)
  3130. return ret;
  3131. #endif
  3132. #ifndef WOLFSSL_NOSHA3_256
  3133. if ((ret = sha3_256_test()) != 0)
  3134. return ret;
  3135. #endif
  3136. #ifndef WOLFSSL_NOSHA3_384
  3137. if ((ret = sha3_384_test()) != 0)
  3138. return ret;
  3139. #endif
  3140. #ifndef WOLFSSL_NOSHA3_512
  3141. if ((ret = sha3_512_test()) != 0)
  3142. return ret;
  3143. #endif
  3144. return 0;
  3145. }
  3146. #endif /* WOLFSSL_SHA3 */
  3147. #ifdef WOLFSSL_SHAKE128
  3148. static int shake128_absorb_test(wc_Shake* sha)
  3149. {
  3150. byte hash[WC_SHA3_128_BLOCK_SIZE*2];
  3151. testVector a, b, c, d, e;
  3152. testVector test_sha[5];
  3153. int ret = 0;
  3154. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3155. byte large_input[1024];
  3156. const char* large_digest =
  3157. "\x2b\xd1\x69\x9f\xb3\x75\x40\x74\xb8\xb2\xd2\x0b\x92\x47\x9b\xfe"
  3158. "\xc9\x91\x48\xbe\xda\xa4\x09\xd7\x61\x35\x18\x05\x07\x71\xa5\x61"
  3159. "\x4d\xc4\x94\xad\xbe\x04\x7d\xad\x95\x2f\xeb\x2c\xc0\x10\x67\x43"
  3160. "\x40\xf1\x4a\x58\x1c\x54\xfa\x24\x1c\x1a\x4e\x8d\x9b\xbc\xea\xa7"
  3161. "\x32\xf2\x4c\xc7\x86\x05\x36\xdc\xb4\x42\xd8\x35\xd1\xb4\xa2\x79"
  3162. "\xa2\xe6\xee\x67\x4f\xbf\x2a\x93\x41\x88\x25\x56\x29\x90\x1a\x06"
  3163. "\xba\xfe\x9f\xa6\x1a\x74\xe8\x7e\x85\x4a\xc8\x58\x60\xb1\x7b\x18"
  3164. "\xdf\x77\x59\x46\x04\xc1\xff\x4b\x9b\xcb\xad\xfe\x91\x28\xf0\x01"
  3165. "\xc1\x33\xd0\x99\x99\x2e\x0c\x86\x84\x67\x4d\x37\xa4\x42\x45\x10"
  3166. "\xdc\x8f\xdb\x6f\xa6\x9b\xee\x8a\x60\xa5\x1f\x95\x3f\x8f\xf5\x31"
  3167. "\x4b\x1d\x48\x1e\x45\xff\x79\x5c\xbe\x72\xfc\x56\xed\x6d\x1a\x99"
  3168. "\x7f\x23\x7c\xd1\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b"
  3169. "\xa3\x60\x51\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41"
  3170. "\x77\x2c\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59"
  3171. "\x0d\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  3172. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4\xcb\x87\xd8\xb7\x9d\xa8"
  3173. "\xbf\xc5\x2e\x5e\xfc\xd3\x6c\x45\xd4\x5d\x72\x0f\x66\xeb\x67\x86"
  3174. "\xfa\x6c\xd6\x80\xa4\x23\xcb\x5d\xed\x3c\xde\xdc\x5b\x3d\xca\x95"
  3175. "\x43\x4b\xdc\xe8\x49\xd3\xe1\x01\xd4\xf1\xe4\x47\xcf\x56\xba\x71"
  3176. "\xb4\x69\xed\xe7\xdb\x0f\x89\xd6\xbb\xcd\x1a\xff\xb4\xbe\x72\x26"
  3177. "\xdc\x76\x79\xb3\x1a\x4b\xe6\x8d\x9b\x8e\xd9\xe9\xe6\xf9\xff\xa5";
  3178. a.input = "";
  3179. a.output = "\x7f\x9c\x2b\xa4\xe8\x8f\x82\x7d\x61\x60\x45\x50\x76\x05\x85"
  3180. "\x3e\xd7\x3b\x80\x93\xf6\xef\xbc\x88\xeb\x1a\x6e\xac\xfa\x66"
  3181. "\xef\x26\x3c\xb1\xee\xa9\x88\x00\x4b\x93\x10\x3c\xfb\x0a\xee"
  3182. "\xfd\x2a\x68\x6e\x01\xfa\x4a\x58\xe8\xa3\x63\x9c\xa8\xa1\xe3"
  3183. "\xf9\xae\x57\xe2\x35\xb8\xcc\x87\x3c\x23\xdc\x62\xb8\xd2\x60"
  3184. "\x16\x9a\xfa\x2f\x75\xab\x91\x6a\x58\xd9\x74\x91\x88\x35\xd2"
  3185. "\x5e\x6a\x43\x50\x85\xb2\xba\xdf\xd6\xdf\xaa\xc3\x59\xa5\xef"
  3186. "\xbb\x7b\xcc\x4b\x59\xd5\x38\xdf\x9a\x04\x30\x2e\x10\xc8\xbc"
  3187. "\x1c\xbf\x1a\x0b\x3a\x51\x20\xea\x17\xcd\xa7\xcf\xad\x76\x5f"
  3188. "\x56\x23\x47\x4d\x36\x8c\xcc\xa8\xaf\x00\x07\xcd\x9f\x5e\x4c"
  3189. "\x84\x9f\x16\x7a\x58\x0b\x14\xaa\xbd\xef\xae\xe7\xee\xf4\x7c"
  3190. "\xb0\xfc\xa9";
  3191. a.inLen = XSTRLEN(a.input);
  3192. a.outLen = WC_SHA3_128_BLOCK_SIZE;
  3193. b.input = "abc";
  3194. b.output = "\x58\x81\x09\x2d\xd8\x18\xbf\x5c\xf8\xa3\xdd\xb7\x93\xfb\xcb"
  3195. "\xa7\x40\x97\xd5\xc5\x26\xa6\xd3\x5f\x97\xb8\x33\x51\x94\x0f"
  3196. "\x2c\xc8\x44\xc5\x0a\xf3\x2a\xcd\x3f\x2c\xdd\x06\x65\x68\x70"
  3197. "\x6f\x50\x9b\xc1\xbd\xde\x58\x29\x5d\xae\x3f\x89\x1a\x9a\x0f"
  3198. "\xca\x57\x83\x78\x9a\x41\xf8\x61\x12\x14\xce\x61\x23\x94\xdf"
  3199. "\x28\x6a\x62\xd1\xa2\x25\x2a\xa9\x4d\xb9\xc5\x38\x95\x6c\x71"
  3200. "\x7d\xc2\xbe\xd4\xf2\x32\xa0\x29\x4c\x85\x7c\x73\x0a\xa1\x60"
  3201. "\x67\xac\x10\x62\xf1\x20\x1f\xb0\xd3\x77\xcf\xb9\xcd\xe4\xc6"
  3202. "\x35\x99\xb2\x7f\x34\x62\xbb\xa4\xa0\xed\x29\x6c\x80\x1f\x9f"
  3203. "\xf7\xf5\x73\x02\xbb\x30\x76\xee\x14\x5f\x97\xa3\x2a\xe6\x8e"
  3204. "\x76\xab\x66\xc4\x8d\x51\x67\x5b\xd4\x9a\xcc\x29\x08\x2f\x56"
  3205. "\x47\x58\x4e";
  3206. b.inLen = XSTRLEN(b.input);
  3207. b.outLen = WC_SHA3_128_BLOCK_SIZE;
  3208. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3209. c.output = "\x1a\x96\x18\x2b\x50\xfb\x8c\x7e\x74\xe0\xa7\x07\x78\x8f\x55"
  3210. "\xe9\x82\x09\xb8\xd9\x1f\xad\xe8\xf3\x2f\x8d\xd5\xcf\xf7\xbf"
  3211. "\x21\xf5\x4e\xe5\xf1\x95\x50\x82\x5a\x6e\x07\x00\x30\x51\x9e"
  3212. "\x94\x42\x63\xac\x1c\x67\x65\x28\x70\x65\x62\x1f\x9f\xcb\x32"
  3213. "\x01\x72\x3e\x32\x23\xb6\x3a\x46\xc2\x93\x8a\xa9\x53\xba\x84"
  3214. "\x01\xd0\xea\x77\xb8\xd2\x64\x90\x77\x55\x66\x40\x7b\x95\x67"
  3215. "\x3c\x0f\x4c\xc1\xce\x9f\xd9\x66\x14\x8d\x7e\xfd\xff\x26\xbb"
  3216. "\xf9\xf4\x8a\x21\xc6\xda\x35\xbf\xaa\x54\x56\x54\xf7\x0a\xe5"
  3217. "\x86\xff\x10\x13\x14\x20\x77\x14\x83\xec\x92\xed\xab\x40\x8c"
  3218. "\x76\x7b\xf4\xc5\xb4\xff\xfa\xa8\x0c\x8c\xa2\x14\xd8\x4c\x4d"
  3219. "\xc7\x00\xd0\xc5\x06\x30\xb2\xff\xc3\x79\x3e\xa4\xd8\x72\x58"
  3220. "\xb4\xc9\x54";
  3221. c.inLen = XSTRLEN(c.input);
  3222. c.outLen = WC_SHA3_128_BLOCK_SIZE;
  3223. /* Taken from NIST CAVP test vectors - full rate output. */
  3224. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  3225. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  3226. "\x85\xe0";
  3227. d.output = "\x93\x68\xf0\x15\x10\x92\x44\xeb\x02\x47\xfa\x3a\x0e\x57\xf5"
  3228. "\x2e\xa7\xd9\xeb\xa2\x3d\xae\x7a\x19\x7f\x0a\x29\xe9\x22\x55"
  3229. "\x06\x05\x98\x16\xb7\x84\x48\xb6\x49\x7a\x76\xeb\x96\x2d\xb3"
  3230. "\xf8\x4d\x37\x60\xf1\xfe\xb4\xbd\xc1\xfd\x4a\xc9\x4e\x91\x7a"
  3231. "\xc2\xea\x5e\x4f\x38\x37\x4a\xa5\x6e\x4f\x47\x67\xb8\xd7\x83"
  3232. "\x1b\x2d\x51\x49\x5a\xb8\xea\xb7\xc9\x82\x20\xaf\x13\x41\x5a"
  3233. "\x59\xbb\x7c\x17\x7a\xcd\x62\x8e\xf0\xff\xe3\x6c\xeb\x18\x59"
  3234. "\x5d\x14\x4c\xbf\x25\xef\xc0\x6c\xd9\x56\xa5\x78\x20\x6e\xa8"
  3235. "\xf9\x14\x5e\xf9\xce\x19\x50\x6a\x9d\x04\x4e\xc7\x00\x79\x9f"
  3236. "\xa1\x41\x9b\xaf\x60\x52\xc0\xc1\xb4\x45\xf8\x35\x17\x57\xb0"
  3237. "\xd0\x22\x87\x21\x89\xe2\xc0\x27\x3f\x82\xd9\x69\x69\x66\x3e"
  3238. "\x55\x4d\x09";
  3239. d.inLen = 32;
  3240. d.outLen = WC_SHA3_128_BLOCK_SIZE;
  3241. /* Taken from NIST CAVP test vectors - more than one output block. */
  3242. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  3243. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  3244. "\x47\xe4";
  3245. e.output = "\xe1\x7e\xab\x0d\xa4\x04\xf9\xb6\xac\xc0\x84\x97\x2f\xc5\x79"
  3246. "\xe8\x6d\xaa\x76\x10\xa5\xe1\x7c\x23\x2f\x79\x19\x83\x96\xfd"
  3247. "\x01\xc2\x4c\x34\xbb\x54\xf4\xb0\x1e\xf7\x40\xb4\x25\x33\x4a"
  3248. "\x55\xdd\x24\x81\x3d\xc8\xea\x86\xf5\x6e\xf7\x27\x67\x26\x2b"
  3249. "\xf2\x25\x74\x8c\xcc\x3d\x9f\x48\x6f\xfb\x72\x8f\x4e\xad\x29"
  3250. "\x60\xc9\x6c\x3e\x44\x63\x86\xea\xce\x21\x9c\x84\x28\x16\x11"
  3251. "\x63\x58\xb0\xf4\x2d\x7d\xff\xf7\xdd\x24\x11\xfa\x2a\x56\x79"
  3252. "\xfd\x7a\x94\x77\x45\x75\xba\xf9\xfc\xad\x68\xa1\x9e\x30\xd1"
  3253. "\x49\xb0\x59\xb5\x9c\x44\x6c\x4e\xdc\xa5\x9b\xc5\xa4\x79\x9d"
  3254. "\xc4\x65\xaa\x9e\x78\x2c\xed\x9f\x21\xc5\x5d\xe2\x42\xdd\x25"
  3255. "\xd0\xd9\xde\x60\xd0\x9f\xf8\x6a\xba\xf3\xa0\x3a\x76\x71\xb3"
  3256. "\x05\x42\xdf\xbe\x72\xfc\x56\xed\x6d\x1a\x99\x7f\x23\x7c\xd1"
  3257. "\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b\xa3\x60\x51"
  3258. "\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41\x77\x2c"
  3259. "\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59\x0d"
  3260. "\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  3261. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4\xcb\x87\xd8\xb7\x9d"
  3262. "\xa8\xbf\xc5\x2e\x5e\xfc\xd3\x6c\x45\xd4\x5d\x72\x0f\x66\xeb"
  3263. "\x67\x86\xfa\x6c\xd6\x80\xa4\x23\xcb\x5d\xed\x3c\xde\xdc\x5b"
  3264. "\x3d\xca\x95\x43\x4b\xdc\xe8\x49\xd3\xe1\x01\xd4\xf1\xe4\x47"
  3265. "\xcf\x56\xba\x71\xb4\x69\xed\xe7\xdb\x0f\x89\xd6\xbb\xcd\x1a"
  3266. "\xff\xb4\xbe\x72\x26\xdc\x76\x79\xb3\x1a\x4b\xe6\x8d\x9b\x8e"
  3267. "\xd9\xe9\xe6\xf9\xff\xa5";
  3268. e.inLen = 32;
  3269. e.outLen = 2 * WC_SHA3_128_BLOCK_SIZE;
  3270. test_sha[0] = a;
  3271. test_sha[1] = b;
  3272. test_sha[2] = c;
  3273. test_sha[3] = d;
  3274. test_sha[4] = e;
  3275. for (i = 0; i < times; ++i) {
  3276. ret = wc_InitShake128(sha, HEAP_HINT, devId);
  3277. if (ret != 0)
  3278. ERROR_OUT(-3100 - i, exit);
  3279. ret = wc_Shake128_Absorb(sha, (byte*)test_sha[i].input,
  3280. (word32)test_sha[i].inLen);
  3281. if (ret != 0)
  3282. ERROR_OUT(-3101 - i, exit);
  3283. ret = wc_Shake128_SqueezeBlocks(sha, hash,
  3284. (word32)test_sha[i].outLen / WC_SHA3_128_BLOCK_SIZE);
  3285. if (ret != 0)
  3286. ERROR_OUT(-3102 - i, exit);
  3287. if (XMEMCMP(hash, test_sha[i].output, (word32)test_sha[i].outLen) != 0)
  3288. ERROR_OUT(-3103 - i, exit);
  3289. }
  3290. #ifndef NO_LARGE_HASH_TEST
  3291. /* BEGIN LARGE HASH TEST */ {
  3292. for (i = 0; i < (int)sizeof(large_input); i++) {
  3293. large_input[i] = (byte)(i & 0xFF);
  3294. }
  3295. ret = wc_InitShake128(sha, HEAP_HINT, devId);
  3296. if (ret != 0)
  3297. ERROR_OUT(-3104, exit);
  3298. /* Absorb is non-incremental. */
  3299. ret = wc_Shake128_Absorb(sha, (byte*)large_input,
  3300. (word32)sizeof(large_input));
  3301. if (ret != 0)
  3302. ERROR_OUT(-3105, exit);
  3303. /* Able to squeeze out blocks incrementally. */
  3304. ret = wc_Shake128_SqueezeBlocks(sha, hash, 1);
  3305. if (ret != 0)
  3306. ERROR_OUT(-3106, exit);
  3307. ret = wc_Shake128_SqueezeBlocks(sha, hash,
  3308. ((word32)sizeof(hash) / WC_SHA3_128_BLOCK_SIZE) - 1);
  3309. if (ret != 0)
  3310. ERROR_OUT(-3106, exit);
  3311. if (XMEMCMP(hash, large_digest, sizeof(hash)) != 0)
  3312. ERROR_OUT(-3107, exit);
  3313. } /* END LARGE HASH TEST */
  3314. #endif /* NO_LARGE_HASH_TEST */
  3315. exit:
  3316. return ret;
  3317. }
  3318. WOLFSSL_TEST_SUBROUTINE int shake128_test(void)
  3319. {
  3320. wc_Shake sha;
  3321. byte hash[250];
  3322. testVector a, b, c, d, e;
  3323. testVector test_sha[5];
  3324. int ret = 0;
  3325. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3326. byte large_input[1024];
  3327. const char* large_digest =
  3328. "\x88\xd7\x0e\x86\x46\x72\x6b\x3d\x7d\x22\xe1\xa9\x2d\x02\xdb\x35"
  3329. "\x92\x4f\x1b\x03\x90\xee\xa3\xce\xd1\x3a\x08\x3a\xd7\x4e\x10\xdf"
  3330. "\x09\x67\x33\x35\x4f\xdd\x38\x50\x5b\xcb\x75\xc7\xba\x65\xe5\xe8"
  3331. "\xb8\x76\xde\xc5\xee\xd7\xf1\x65\x93\x4e\x5e\xc4\xb1\xd7\x6b\xee"
  3332. "\x4b\x57\x48\xf5\x38\x49\x9e\x45\xa0\xf7\x32\xe9\x05\x26\x6a\x10"
  3333. "\x70\xd4\x7c\x19\x01\x1f\x6d\x37\xba\x7b\x74\xc2\xbc\xb6\xbc\x74"
  3334. "\xa3\x66\x6c\x9b\x11\x84\x9d\x4a\x36\xbc\x8a\x0d\x4c\xe3\x39\xfa"
  3335. "\xfa\x1b";
  3336. a.input = "";
  3337. a.output = "\x7f\x9c\x2b\xa4\xe8\x8f\x82\x7d\x61\x60\x45\x50\x76\x05\x85"
  3338. "\x3e\xd7\x3b\x80\x93\xf6\xef\xbc\x88\xeb\x1a\x6e\xac\xfa\x66"
  3339. "\xef\x26\x3c\xb1\xee\xa9\x88\x00\x4b\x93\x10\x3c\xfb\x0a\xee"
  3340. "\xfd\x2a\x68\x6e\x01\xfa\x4a\x58\xe8\xa3\x63\x9c\xa8\xa1\xe3"
  3341. "\xf9\xae\x57\xe2\x35\xb8\xcc\x87\x3c\x23\xdc\x62\xb8\xd2\x60"
  3342. "\x16\x9a\xfa\x2f\x75\xab\x91\x6a\x58\xd9\x74\x91\x88\x35\xd2"
  3343. "\x5e\x6a\x43\x50\x85\xb2\xba\xdf\xd6\xdf\xaa\xc3\x59\xa5\xef"
  3344. "\xbb\x7b\xcc\x4b\x59\xd5\x38\xdf\x9a";
  3345. a.inLen = XSTRLEN(a.input);
  3346. a.outLen = 114;
  3347. b.input = "abc";
  3348. b.output = "\x58\x81\x09\x2d\xd8\x18\xbf\x5c\xf8\xa3\xdd\xb7\x93\xfb\xcb"
  3349. "\xa7\x40\x97\xd5\xc5\x26\xa6\xd3\x5f\x97\xb8\x33\x51\x94\x0f"
  3350. "\x2c\xc8\x44\xc5\x0a\xf3\x2a\xcd\x3f\x2c\xdd\x06\x65\x68\x70"
  3351. "\x6f\x50\x9b\xc1\xbd\xde\x58\x29\x5d\xae\x3f\x89\x1a\x9a\x0f"
  3352. "\xca\x57\x83\x78\x9a\x41\xf8\x61\x12\x14\xce\x61\x23\x94\xdf"
  3353. "\x28\x6a\x62\xd1\xa2\x25\x2a\xa9\x4d\xb9\xc5\x38\x95\x6c\x71"
  3354. "\x7d\xc2\xbe\xd4\xf2\x32\xa0\x29\x4c\x85\x7c\x73\x0a\xa1\x60"
  3355. "\x67\xac\x10\x62\xf1\x20\x1f\xb0\xd3";
  3356. b.inLen = XSTRLEN(b.input);
  3357. b.outLen = 114;
  3358. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3359. c.output = "\x1a\x96\x18\x2b\x50\xfb\x8c\x7e\x74\xe0\xa7\x07\x78\x8f\x55"
  3360. "\xe9\x82\x09\xb8\xd9\x1f\xad\xe8\xf3\x2f\x8d\xd5\xcf\xf7\xbf"
  3361. "\x21\xf5\x4e\xe5\xf1\x95\x50\x82\x5a\x6e\x07\x00\x30\x51\x9e"
  3362. "\x94\x42\x63\xac\x1c\x67\x65\x28\x70\x65\x62\x1f\x9f\xcb\x32"
  3363. "\x01\x72\x3e\x32\x23\xb6\x3a\x46\xc2\x93\x8a\xa9\x53\xba\x84"
  3364. "\x01\xd0\xea\x77\xb8\xd2\x64\x90\x77\x55\x66\x40\x7b\x95\x67"
  3365. "\x3c\x0f\x4c\xc1\xce\x9f\xd9\x66\x14\x8d\x7e\xfd\xff\x26\xbb"
  3366. "\xf9\xf4\x8a\x21\xc6\xda\x35\xbf\xaa";
  3367. c.inLen = XSTRLEN(c.input);
  3368. c.outLen = 114;
  3369. /* Taken from NIST CAVP test vectors - full rate output. */
  3370. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  3371. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  3372. "\x85\xe0";
  3373. d.output = "\x93\x68\xf0\x15\x10\x92\x44\xeb\x02\x47\xfa\x3a\x0e\x57\xf5"
  3374. "\x2e\xa7\xd9\xeb\xa2\x3d\xae\x7a\x19\x7f\x0a\x29\xe9\x22\x55"
  3375. "\x06\x05\x98\x16\xb7\x84\x48\xb6\x49\x7a\x76\xeb\x96\x2d\xb3"
  3376. "\xf8\x4d\x37\x60\xf1\xfe\xb4\xbd\xc1\xfd\x4a\xc9\x4e\x91\x7a"
  3377. "\xc2\xea\x5e\x4f\x38\x37\x4a\xa5\x6e\x4f\x47\x67\xb8\xd7\x83"
  3378. "\x1b\x2d\x51\x49\x5a\xb8\xea\xb7\xc9\x82\x20\xaf\x13\x41\x5a"
  3379. "\x59\xbb\x7c\x17\x7a\xcd\x62\x8e\xf0\xff\xe3\x6c\xeb\x18\x59"
  3380. "\x5d\x14\x4c\xbf\x25\xef\xc0\x6c\xd9\x56\xa5\x78\x20\x6e\xa8"
  3381. "\xf9\x14\x5e\xf9\xce\x19\x50\x6a\x9d\x04\x4e\xc7\x00\x79\x9f"
  3382. "\xa1";
  3383. d.inLen = 32;
  3384. d.outLen = 136;
  3385. /* Taken from NIST CAVP test vectors - more than one output block. */
  3386. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  3387. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  3388. "\x47\xe4";
  3389. e.output = "\xe1\x7e\xab\x0d\xa4\x04\xf9\xb6\xac\xc0\x84\x97\x2f\xc5\x79"
  3390. "\xe8\x6d\xaa\x76\x10\xa5\xe1\x7c\x23\x2f\x79\x19\x83\x96\xfd"
  3391. "\x01\xc2\x4c\x34\xbb\x54\xf4\xb0\x1e\xf7\x40\xb4\x25\x33\x4a"
  3392. "\x55\xdd\x24\x81\x3d\xc8\xea\x86\xf5\x6e\xf7\x27\x67\x26\x2b"
  3393. "\xf2\x25\x74\x8c\xcc\x3d\x9f\x48\x6f\xfb\x72\x8f\x4e\xad\x29"
  3394. "\x60\xc9\x6c\x3e\x44\x63\x86\xea\xce\x21\x9c\x84\x28\x16\x11"
  3395. "\x63\x58\xb0\xf4\x2d\x7d\xff\xf7\xdd\x24\x11\xfa\x2a\x56\x79"
  3396. "\xfd\x7a\x94\x77\x45\x75\xba\xf9\xfc\xad\x68\xa1\x9e\x30\xd1"
  3397. "\x49\xb0\x59\xb5\x9c\x44\x6c\x4e\xdc\xa5\x9b\xc5\xa4\x79\x9d"
  3398. "\xc4\x65\xaa\x9e\x78\x2c\xed\x9f\x21\xc5\x5d\xe2\x42\xdd\x25"
  3399. "\xd0\xd9\xde\x60\xd0\x9f\xf8\x6a\xba\xf3\xa0\x3a\x76\x71\xb3"
  3400. "\x05\x42\xdf\xbe\x72\xfc\x56\xed\x6d\x1a\x99\x7f\x23\x7c\xd1"
  3401. "\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b\xa3\x60\x51"
  3402. "\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41\x77\x2c"
  3403. "\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59\x0d"
  3404. "\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  3405. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4";
  3406. e.inLen = 32;
  3407. e.outLen = 250;
  3408. test_sha[0] = a;
  3409. test_sha[1] = b;
  3410. test_sha[2] = c;
  3411. test_sha[3] = d;
  3412. test_sha[4] = e;
  3413. ret = wc_InitShake128(&sha, HEAP_HINT, devId);
  3414. if (ret != 0)
  3415. return -3100;
  3416. for (i = 0; i < times; ++i) {
  3417. ret = wc_Shake128_Update(&sha, (byte*)test_sha[i].input,
  3418. (word32)test_sha[i].inLen);
  3419. if (ret != 0)
  3420. ERROR_OUT(-3101 - i, exit);
  3421. ret = wc_Shake128_Final(&sha, hash, (word32)test_sha[i].outLen);
  3422. if (ret != 0)
  3423. ERROR_OUT(-3102 - i, exit);
  3424. if (XMEMCMP(hash, test_sha[i].output, test_sha[i].outLen) != 0)
  3425. ERROR_OUT(-3103 - i, exit);
  3426. }
  3427. #ifndef NO_LARGE_HASH_TEST
  3428. /* BEGIN LARGE HASH TEST */ {
  3429. for (i = 0; i < (int)sizeof(large_input); i++) {
  3430. large_input[i] = (byte)(i & 0xFF);
  3431. }
  3432. times = 100;
  3433. for (i = 0; i < times; ++i) {
  3434. ret = wc_Shake128_Update(&sha, (byte*)large_input,
  3435. (word32)sizeof(large_input));
  3436. if (ret != 0)
  3437. ERROR_OUT(-3104, exit);
  3438. }
  3439. ret = wc_Shake128_Final(&sha, hash, (word32)sizeof(hash));
  3440. if (ret != 0)
  3441. ERROR_OUT(-3105, exit);
  3442. if (XMEMCMP(hash, large_digest, 114) != 0)
  3443. ERROR_OUT(-3106, exit);
  3444. } /* END LARGE HASH TEST */
  3445. #endif /* NO_LARGE_HASH_TEST */
  3446. ret = shake128_absorb_test(&sha);
  3447. exit:
  3448. wc_Shake128_Free(&sha);
  3449. return ret;
  3450. }
  3451. #endif
  3452. #ifdef WOLFSSL_SHAKE256
  3453. static int shake256_absorb_test(wc_Shake* sha, byte *large_input_buf,
  3454. size_t large_input_buf_size)
  3455. {
  3456. byte hash[WC_SHA3_256_BLOCK_SIZE*2];
  3457. testVector a, b, c, d, e;
  3458. testVector test_sha[5];
  3459. int ret = 0;
  3460. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3461. const char* large_digest =
  3462. "\x21\x25\x8e\xae\x6e\x4f\xa7\xe1\xb9\x6d\xa7\xc9\x7d\x46\x03\x69"
  3463. "\x29\x0d\x81\x49\xba\x5d\xaf\x37\xfd\xeb\x25\x52\x1d\xd9\xbd\x65"
  3464. "\xfa\x99\xb9\xd1\x70\x6b\xeb\xd4\xc1\x2c\xea\x24\x20\x27\xa7\xcd"
  3465. "\xfa\xe1\x81\xd9\xd5\xc1\x1c\xc7\xe9\x70\xc3\xc7\x21\x6f\x32\x22"
  3466. "\xe3\x27\xdb\x58\x5e\xea\x18\x2d\x63\x4d\x14\x6c\x94\xcf\x2b\x7e"
  3467. "\x6e\x2a\x74\xf3\xe0\xac\xb3\xb2\xcc\xef\x38\xe9\xe7\x35\xb3\xc5"
  3468. "\x77\x9d\xff\xe3\x08\x8e\xf8\x2c\x89\xbb\x45\x22\x16\x99\x91\xc0"
  3469. "\xe7\x71\x57\x75\xc5\xb1\xc6\xaf\x27\xcb\x64\x8c\xc4\xee\x3d\x5f"
  3470. "\x4c\x35\xfb\x1c\xf3\xf8\x0e\xfd\x5e\xfc\x07\xd8\x4d\x55\x32\x49"
  3471. "\x45\x0d\xab\x4a\x49\xc4\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93"
  3472. "\x7a\xe6\x6b\xb4\x36\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43"
  3473. "\x2f\x3b\xfc\x09\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48"
  3474. "\x3d\x0e\xda\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08"
  3475. "\xd9\xdc\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a"
  3476. "\xb7\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  3477. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c\x68\x1b\x30\xdd\xc4\xe6"
  3478. "\x83\x8b\x0f\x23\x58\x7e\x06\x5f\x4a\x2b\xed\xc9\x6c\x97\x68\x44";
  3479. a.input = "";
  3480. a.output = "\x46\xb9\xdd\x2b\x0b\xa8\x8d\x13\x23\x3b\x3f\xeb\x74\x3e\xeb"
  3481. "\x24\x3f\xcd\x52\xea\x62\xb8\x1b\x82\xb5\x0c\x27\x64\x6e\xd5"
  3482. "\x76\x2f\xd7\x5d\xc4\xdd\xd8\xc0\xf2\x00\xcb\x05\x01\x9d\x67"
  3483. "\xb5\x92\xf6\xfc\x82\x1c\x49\x47\x9a\xb4\x86\x40\x29\x2e\xac"
  3484. "\xb3\xb7\xc4\xbe\x14\x1e\x96\x61\x6f\xb1\x39\x57\x69\x2c\xc7"
  3485. "\xed\xd0\xb4\x5a\xe3\xdc\x07\x22\x3c\x8e\x92\x93\x7b\xef\x84"
  3486. "\xbc\x0e\xab\x86\x28\x53\x34\x9e\xc7\x55\x46\xf5\x8f\xb7\xc2"
  3487. "\x77\x5c\x38\x46\x2c\x50\x10\xd8\x46\xc1\x85\xc1\x51\x11\xe5"
  3488. "\x95\x52\x2a\x6b\xcd\x16\xcf\x86\xf3\xd1\x22\x10\x9e\x3b\x1f"
  3489. "\xdd";
  3490. a.inLen = XSTRLEN(a.input);
  3491. a.outLen = WC_SHA3_256_BLOCK_SIZE;
  3492. b.input = "abc";
  3493. b.output = "\x48\x33\x66\x60\x13\x60\xa8\x77\x1c\x68\x63\x08\x0c\xc4\x11"
  3494. "\x4d\x8d\xb4\x45\x30\xf8\xf1\xe1\xee\x4f\x94\xea\x37\xe7\x8b"
  3495. "\x57\x39\xd5\xa1\x5b\xef\x18\x6a\x53\x86\xc7\x57\x44\xc0\x52"
  3496. "\x7e\x1f\xaa\x9f\x87\x26\xe4\x62\xa1\x2a\x4f\xeb\x06\xbd\x88"
  3497. "\x01\xe7\x51\xe4\x13\x85\x14\x12\x04\xf3\x29\x97\x9f\xd3\x04"
  3498. "\x7a\x13\xc5\x65\x77\x24\xad\xa6\x4d\x24\x70\x15\x7b\x3c\xdc"
  3499. "\x28\x86\x20\x94\x4d\x78\xdb\xcd\xdb\xd9\x12\x99\x3f\x09\x13"
  3500. "\xf1\x64\xfb\x2c\xe9\x51\x31\xa2\xd0\x9a\x3e\x6d\x51\xcb\xfc"
  3501. "\x62\x27\x20\xd7\xa7\x5c\x63\x34\xe8\xa2\xd7\xec\x71\xa7\xcc"
  3502. "\x29";
  3503. b.inLen = XSTRLEN(b.input);
  3504. b.outLen = WC_SHA3_256_BLOCK_SIZE;
  3505. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3506. c.output = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f\x6f\x87"
  3507. "\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b\xe5\xd4\xfd\x2e"
  3508. "\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59\xaa\x80\x60\xf1\xf9\xbc"
  3509. "\x99\x6c\x05\xac\xa3\xc6\x96\xa8\xb6\x62\x79\xdc\x67\x2c\x74"
  3510. "\x0b\xb2\x24\xec\x37\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55"
  3511. "\xf5\x1d\x97\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a"
  3512. "\xf2\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67\x60"
  3513. "\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4\x9d\xd3\xef\x7e\x18\x2b"
  3514. "\x15\x24\xdf\x82\xea\x1c\xef\xe1\xc6\xc3\x96\x61\x75\xf0\x22"
  3515. "\x8d";
  3516. c.inLen = XSTRLEN(c.input);
  3517. c.outLen = WC_SHA3_256_BLOCK_SIZE;
  3518. /* Taken from NIST CAVP test vectors - full rate output. */
  3519. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  3520. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  3521. "\x85\xe0";
  3522. d.output = "\x00\x64\x8a\xfb\xc5\xe6\x51\x64\x9d\xb1\xfd\x82\x93\x6b\x00"
  3523. "\xdb\xbc\x12\x2f\xb4\xc8\x77\x86\x0d\x38\x5c\x49\x50\xd5\x6d"
  3524. "\xe7\xe0\x96\xd6\x13\xd7\xa3\xf2\x7e\xd8\xf2\x63\x34\xb0\xcc"
  3525. "\xc1\x40\x7b\x41\xdc\xcb\x23\xdf\xaa\x52\x98\x18\xd1\x12\x5c"
  3526. "\xd5\x34\x80\x92\x52\x43\x66\xb8\x5f\xab\xb9\x7c\x6c\xd1\xe6"
  3527. "\x06\x6f\x45\x9b\xcc\x56\x6d\xa8\x7e\xc9\xb7\xba\x36\x79\x2d"
  3528. "\x11\x8a\xc3\x9a\x4c\xce\xf6\x19\x2b\xbf\x3a\x54\xaf\x18\xe5"
  3529. "\x7b\x0c\x14\x61\x01\xf6\xae\xaa\x82\x2b\xc4\xb4\xc9\x70\x8b"
  3530. "\x09\xf0\xb3\xba\xb4\x1b\xcc\xe9\x64\xd9\x99\xd1\x10\x7b\xd7"
  3531. "\xc2";
  3532. d.inLen = 32;
  3533. d.outLen = WC_SHA3_256_BLOCK_SIZE;
  3534. /* Taken from NIST CAVP test vectors - more than one output block. */
  3535. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  3536. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  3537. "\x47\xe4";
  3538. e.output = "\x2e\x97\x5f\x6a\x8a\x14\xf0\x70\x4d\x51\xb1\x36\x67\xd8\x19"
  3539. "\x5c\x21\x9f\x71\xe6\x34\x56\x96\xc4\x9f\xa4\xb9\xd0\x8e\x92"
  3540. "\x25\xd3\xd3\x93\x93\x42\x51\x52\xc9\x7e\x71\xdd\x24\x60\x1c"
  3541. "\x11\xab\xcf\xa0\xf1\x2f\x53\xc6\x80\xbd\x3a\xe7\x57\xb8\x13"
  3542. "\x4a\x9c\x10\xd4\x29\x61\x58\x69\x21\x7f\xdd\x58\x85\xc4\xdb"
  3543. "\x17\x49\x85\x70\x3a\x6d\x6d\xe9\x4a\x66\x7e\xac\x30\x23\x44"
  3544. "\x3a\x83\x37\xae\x1b\xc6\x01\xb7\x6d\x7d\x38\xec\x3c\x34\x46"
  3545. "\x31\x05\xf0\xd3\x94\x9d\x78\xe5\x62\xa0\x39\xe4\x46\x95\x48"
  3546. "\xb6\x09\x39\x5d\xe5\xa4\xfd\x43\xc4\x6c\xa9\xfd\x6e\xe2\x9a"
  3547. "\xda\x5e\xfc\x07\xd8\x4d\x55\x32\x49\x45\x0d\xab\x4a\x49\xc4"
  3548. "\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93\x7a\xe6\x6b\xb4\x36"
  3549. "\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43\x2f\x3b\xfc\x09"
  3550. "\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48\x3d\x0e\xda"
  3551. "\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08\xd9\xdc"
  3552. "\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a\xb7"
  3553. "\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  3554. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c\x68\x1b\x30\xdd\xc4"
  3555. "\xe6\x83\x8b\x0f\x23\x58\x7e\x06\x5f\x4a\x2b\xed\xc9\x6c\x97"
  3556. "\x68\x44";
  3557. e.inLen = 32;
  3558. e.outLen = 2 * WC_SHA3_256_BLOCK_SIZE;
  3559. test_sha[0] = a;
  3560. test_sha[1] = b;
  3561. test_sha[2] = c;
  3562. test_sha[3] = d;
  3563. test_sha[4] = e;
  3564. for (i = 0; i < times; ++i) {
  3565. ret = wc_InitShake256(sha, HEAP_HINT, devId);
  3566. if (ret != 0)
  3567. ERROR_OUT(-3100 - i, exit);
  3568. ret = wc_Shake256_Absorb(sha, (byte*)test_sha[i].input,
  3569. (word32)test_sha[i].inLen);
  3570. if (ret != 0)
  3571. ERROR_OUT(-3101 - i, exit);
  3572. ret = wc_Shake256_SqueezeBlocks(sha, hash,
  3573. (word32)test_sha[i].outLen / WC_SHA3_256_BLOCK_SIZE);
  3574. if (ret != 0)
  3575. ERROR_OUT(-3102 - i, exit);
  3576. if (XMEMCMP(hash, test_sha[i].output, (word32)test_sha[i].outLen) != 0)
  3577. ERROR_OUT(-3103 - i, exit);
  3578. }
  3579. #ifndef NO_LARGE_HASH_TEST
  3580. /* BEGIN LARGE HASH TEST */ {
  3581. for (i = 0; i < (int)large_input_buf_size; i++) {
  3582. large_input_buf[i] = (byte)(i & 0xFF);
  3583. }
  3584. ret = wc_InitShake256(sha, HEAP_HINT, devId);
  3585. if (ret != 0)
  3586. ERROR_OUT(-3104, exit);
  3587. /* Absorb is non-incremental. */
  3588. ret = wc_Shake256_Absorb(sha, large_input_buf,
  3589. (word32)large_input_buf_size);
  3590. if (ret != 0)
  3591. ERROR_OUT(-3105, exit);
  3592. /* Able to squeeze out blocks incrementally. */
  3593. ret = wc_Shake256_SqueezeBlocks(sha, hash, 1);
  3594. if (ret != 0)
  3595. ERROR_OUT(-3106, exit);
  3596. ret = wc_Shake256_SqueezeBlocks(sha, hash,
  3597. ((word32)sizeof(hash) / WC_SHA3_256_BLOCK_SIZE) - 1);
  3598. if (ret != 0)
  3599. ERROR_OUT(-3106, exit);
  3600. if (XMEMCMP(hash, large_digest, sizeof(hash)) != 0)
  3601. ERROR_OUT(-3107, exit);
  3602. } /* END LARGE HASH TEST */
  3603. #endif /* NO_LARGE_HASH_TEST */
  3604. exit:
  3605. return ret;
  3606. }
  3607. WOLFSSL_TEST_SUBROUTINE int shake256_test(void)
  3608. {
  3609. wc_Shake sha;
  3610. byte hash[250];
  3611. testVector a, b, c, d, e;
  3612. testVector test_sha[5];
  3613. int ret = 0;
  3614. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3615. #define SHAKE256_LARGE_INPUT_BUFSIZ 1024
  3616. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  3617. byte *large_input = NULL;
  3618. #else
  3619. byte large_input[SHAKE256_LARGE_INPUT_BUFSIZ];
  3620. #endif
  3621. const char* large_digest =
  3622. "\x90\x32\x4a\xcc\xd1\xdf\xb8\x0b\x79\x1f\xb8\xc8\x5b\x54\xc8\xe7"
  3623. "\x45\xf5\x60\x6b\x38\x26\xb2\x0a\xee\x38\x01\xf3\xd9\xfa\x96\x9f"
  3624. "\x6a\xd7\x15\xdf\xb6\xc2\xf4\x20\x33\x44\x55\xe8\x2a\x09\x2b\x68"
  3625. "\x2e\x18\x65\x5e\x65\x93\x28\xbc\xb1\x9e\xe2\xb1\x92\xea\x98\xac"
  3626. "\x21\xef\x4c\xe1\xb4\xb7\xbe\x81\x5c\x1d\xd3\xb7\x17\xe5\xbb\xc5"
  3627. "\x8c\x68\xb7\xfb\xac\x55\x8a\x9b\x4d\x91\xe4\x9f\x72\xbb\x6e\x38"
  3628. "\xaf\x21\x7d\x21\xaa\x98\x4e\x75\xc4\xb4\x1c\x7c\x50\x45\x54\xf9"
  3629. "\xea\x26";
  3630. a.input = "";
  3631. a.output = "\x46\xb9\xdd\x2b\x0b\xa8\x8d\x13\x23\x3b\x3f\xeb\x74\x3e\xeb"
  3632. "\x24\x3f\xcd\x52\xea\x62\xb8\x1b\x82\xb5\x0c\x27\x64\x6e\xd5"
  3633. "\x76\x2f\xd7\x5d\xc4\xdd\xd8\xc0\xf2\x00\xcb\x05\x01\x9d\x67"
  3634. "\xb5\x92\xf6\xfc\x82\x1c\x49\x47\x9a\xb4\x86\x40\x29\x2e\xac"
  3635. "\xb3\xb7\xc4\xbe\x14\x1e\x96\x61\x6f\xb1\x39\x57\x69\x2c\xc7"
  3636. "\xed\xd0\xb4\x5a\xe3\xdc\x07\x22\x3c\x8e\x92\x93\x7b\xef\x84"
  3637. "\xbc\x0e\xab\x86\x28\x53\x34\x9e\xc7\x55\x46\xf5\x8f\xb7\xc2"
  3638. "\x77\x5c\x38\x46\x2c\x50\x10\xd8\x46";
  3639. a.inLen = XSTRLEN(a.input);
  3640. a.outLen = 114;
  3641. b.input = "abc";
  3642. b.output = "\x48\x33\x66\x60\x13\x60\xa8\x77\x1c\x68\x63\x08\x0c\xc4\x11"
  3643. "\x4d\x8d\xb4\x45\x30\xf8\xf1\xe1\xee\x4f\x94\xea\x37\xe7\x8b"
  3644. "\x57\x39\xd5\xa1\x5b\xef\x18\x6a\x53\x86\xc7\x57\x44\xc0\x52"
  3645. "\x7e\x1f\xaa\x9f\x87\x26\xe4\x62\xa1\x2a\x4f\xeb\x06\xbd\x88"
  3646. "\x01\xe7\x51\xe4\x13\x85\x14\x12\x04\xf3\x29\x97\x9f\xd3\x04"
  3647. "\x7a\x13\xc5\x65\x77\x24\xad\xa6\x4d\x24\x70\x15\x7b\x3c\xdc"
  3648. "\x28\x86\x20\x94\x4d\x78\xdb\xcd\xdb\xd9\x12\x99\x3f\x09\x13"
  3649. "\xf1\x64\xfb\x2c\xe9\x51\x31\xa2\xd0";
  3650. b.inLen = XSTRLEN(b.input);
  3651. b.outLen = 114;
  3652. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3653. c.output = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f\x6f\x87"
  3654. "\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b\xe5\xd4\xfd\x2e"
  3655. "\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59\xaa\x80\x60\xf1\xf9\xbc"
  3656. "\x99\x6c\x05\xac\xa3\xc6\x96\xa8\xb6\x62\x79\xdc\x67\x2c\x74"
  3657. "\x0b\xb2\x24\xec\x37\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55"
  3658. "\xf5\x1d\x97\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a"
  3659. "\xf2\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67\x60"
  3660. "\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  3661. c.inLen = XSTRLEN(c.input);
  3662. c.outLen = 114;
  3663. /* Taken from NIST CAVP test vectors - full rate output. */
  3664. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  3665. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  3666. "\x85\xe0";
  3667. d.output = "\x00\x64\x8a\xfb\xc5\xe6\x51\x64\x9d\xb1\xfd\x82\x93\x6b\x00"
  3668. "\xdb\xbc\x12\x2f\xb4\xc8\x77\x86\x0d\x38\x5c\x49\x50\xd5\x6d"
  3669. "\xe7\xe0\x96\xd6\x13\xd7\xa3\xf2\x7e\xd8\xf2\x63\x34\xb0\xcc"
  3670. "\xc1\x40\x7b\x41\xdc\xcb\x23\xdf\xaa\x52\x98\x18\xd1\x12\x5c"
  3671. "\xd5\x34\x80\x92\x52\x43\x66\xb8\x5f\xab\xb9\x7c\x6c\xd1\xe6"
  3672. "\x06\x6f\x45\x9b\xcc\x56\x6d\xa8\x7e\xc9\xb7\xba\x36\x79\x2d"
  3673. "\x11\x8a\xc3\x9a\x4c\xce\xf6\x19\x2b\xbf\x3a\x54\xaf\x18\xe5"
  3674. "\x7b\x0c\x14\x61\x01\xf6\xae\xaa\x82\x2b\xc4\xb4\xc9\x70\x8b"
  3675. "\x09\xf0\xb3\xba\xb4\x1b\xcc\xe9\x64\xd9\x99\xd1\x10\x7b\xd7"
  3676. "\xc2";
  3677. d.inLen = 32;
  3678. d.outLen = 136;
  3679. /* Taken from NIST CAVP test vectors - more than one output block. */
  3680. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  3681. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  3682. "\x47\xe4";
  3683. e.output = "\x2e\x97\x5f\x6a\x8a\x14\xf0\x70\x4d\x51\xb1\x36\x67\xd8\x19"
  3684. "\x5c\x21\x9f\x71\xe6\x34\x56\x96\xc4\x9f\xa4\xb9\xd0\x8e\x92"
  3685. "\x25\xd3\xd3\x93\x93\x42\x51\x52\xc9\x7e\x71\xdd\x24\x60\x1c"
  3686. "\x11\xab\xcf\xa0\xf1\x2f\x53\xc6\x80\xbd\x3a\xe7\x57\xb8\x13"
  3687. "\x4a\x9c\x10\xd4\x29\x61\x58\x69\x21\x7f\xdd\x58\x85\xc4\xdb"
  3688. "\x17\x49\x85\x70\x3a\x6d\x6d\xe9\x4a\x66\x7e\xac\x30\x23\x44"
  3689. "\x3a\x83\x37\xae\x1b\xc6\x01\xb7\x6d\x7d\x38\xec\x3c\x34\x46"
  3690. "\x31\x05\xf0\xd3\x94\x9d\x78\xe5\x62\xa0\x39\xe4\x46\x95\x48"
  3691. "\xb6\x09\x39\x5d\xe5\xa4\xfd\x43\xc4\x6c\xa9\xfd\x6e\xe2\x9a"
  3692. "\xda\x5e\xfc\x07\xd8\x4d\x55\x32\x49\x45\x0d\xab\x4a\x49\xc4"
  3693. "\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93\x7a\xe6\x6b\xb4\x36"
  3694. "\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43\x2f\x3b\xfc\x09"
  3695. "\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48\x3d\x0e\xda"
  3696. "\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08\xd9\xdc"
  3697. "\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a\xb7"
  3698. "\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  3699. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c";
  3700. e.inLen = 32;
  3701. e.outLen = 250;
  3702. test_sha[0] = a;
  3703. test_sha[1] = b;
  3704. test_sha[2] = c;
  3705. test_sha[3] = d;
  3706. test_sha[4] = e;
  3707. ret = wc_InitShake256(&sha, HEAP_HINT, devId);
  3708. if (ret != 0)
  3709. return -3100;
  3710. for (i = 0; i < times; ++i) {
  3711. ret = wc_Shake256_Update(&sha, (byte*)test_sha[i].input,
  3712. (word32)test_sha[i].inLen);
  3713. if (ret != 0)
  3714. ERROR_OUT(-3101 - i, exit);
  3715. ret = wc_Shake256_Final(&sha, hash, (word32)test_sha[i].outLen);
  3716. if (ret != 0)
  3717. ERROR_OUT(-3102 - i, exit);
  3718. if (XMEMCMP(hash, test_sha[i].output, test_sha[i].outLen) != 0)
  3719. ERROR_OUT(-3103 - i, exit);
  3720. }
  3721. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  3722. large_input = (byte *)XMALLOC(SHAKE256_LARGE_INPUT_BUFSIZ, NULL,
  3723. DYNAMIC_TYPE_TMP_BUFFER);
  3724. if (large_input == NULL)
  3725. ERROR_OUT(-3107, exit);
  3726. #endif
  3727. #ifndef NO_LARGE_HASH_TEST
  3728. /* BEGIN LARGE HASH TEST */ {
  3729. for (i = 0; i < SHAKE256_LARGE_INPUT_BUFSIZ; i++) {
  3730. large_input[i] = (byte)(i & 0xFF);
  3731. }
  3732. times = 100;
  3733. for (i = 0; i < times; ++i) {
  3734. ret = wc_Shake256_Update(&sha, (byte*)large_input,
  3735. SHAKE256_LARGE_INPUT_BUFSIZ);
  3736. if (ret != 0)
  3737. ERROR_OUT(-3104, exit);
  3738. }
  3739. ret = wc_Shake256_Final(&sha, hash, (word32)sizeof(hash));
  3740. if (ret != 0)
  3741. ERROR_OUT(-3105, exit);
  3742. if (XMEMCMP(hash, large_digest, 114) != 0)
  3743. ERROR_OUT(-3106, exit);
  3744. } /* END LARGE HASH TEST */
  3745. #endif /* NO_LARGE_HASH_TEST */
  3746. ret = shake256_absorb_test(&sha, large_input, SHAKE256_LARGE_INPUT_BUFSIZ);
  3747. exit:
  3748. wc_Shake256_Free(&sha);
  3749. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  3750. if (large_input != NULL)
  3751. XFREE(large_input, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3752. #endif
  3753. return ret;
  3754. }
  3755. #endif
  3756. #ifndef NO_HASH_WRAPPER
  3757. WOLFSSL_TEST_SUBROUTINE int hash_test(void)
  3758. {
  3759. wc_HashAlg hash;
  3760. int ret, exp_ret;
  3761. int i, j;
  3762. int digestSz;
  3763. byte data[] = "0123456789abcdef0123456789abcdef0123456";
  3764. byte out[WC_MAX_DIGEST_SIZE];
  3765. byte hashOut[WC_MAX_DIGEST_SIZE];
  3766. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  3767. enum wc_HashType hashType;
  3768. #endif
  3769. enum wc_HashType typesGood[] = { WC_HASH_TYPE_MD5, WC_HASH_TYPE_SHA,
  3770. WC_HASH_TYPE_SHA224, WC_HASH_TYPE_SHA256,
  3771. WC_HASH_TYPE_SHA384, WC_HASH_TYPE_SHA512,
  3772. WC_HASH_TYPE_SHA3_224,
  3773. WC_HASH_TYPE_SHA3_256,
  3774. WC_HASH_TYPE_SHA3_384,
  3775. WC_HASH_TYPE_SHA3_512 };
  3776. enum wc_HashType typesNoImpl[] = {
  3777. #ifdef NO_MD5
  3778. WC_HASH_TYPE_MD5,
  3779. #endif
  3780. #ifdef NO_SHA
  3781. WC_HASH_TYPE_SHA,
  3782. #endif
  3783. #ifndef WOLFSSL_SHA224
  3784. WC_HASH_TYPE_SHA224,
  3785. #endif
  3786. #ifdef NO_SHA256
  3787. WC_HASH_TYPE_SHA256,
  3788. #endif
  3789. #ifndef WOLFSSL_SHA384
  3790. WC_HASH_TYPE_SHA384,
  3791. #endif
  3792. #ifndef WOLFSSL_SHA512
  3793. WC_HASH_TYPE_SHA512,
  3794. #endif
  3795. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_224)
  3796. WC_HASH_TYPE_SHA3_224,
  3797. #endif
  3798. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_256)
  3799. WC_HASH_TYPE_SHA3_256,
  3800. #endif
  3801. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_384)
  3802. WC_HASH_TYPE_SHA3_384,
  3803. #endif
  3804. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_512)
  3805. WC_HASH_TYPE_SHA3_512,
  3806. #endif
  3807. WC_HASH_TYPE_NONE
  3808. };
  3809. enum wc_HashType typesBad[] = { WC_HASH_TYPE_NONE, WC_HASH_TYPE_MD5_SHA,
  3810. WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4 };
  3811. enum wc_HashType typesHashBad[] = { WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4,
  3812. WC_HASH_TYPE_BLAKE2B,
  3813. WC_HASH_TYPE_NONE };
  3814. /* Parameter Validation testing. */
  3815. ret = wc_HashInit(NULL, WC_HASH_TYPE_SHA256);
  3816. if (ret != BAD_FUNC_ARG)
  3817. return -3200;
  3818. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  3819. if (ret != BAD_FUNC_ARG)
  3820. return -3201;
  3821. ret = wc_HashUpdate(&hash, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  3822. if (ret != BAD_FUNC_ARG)
  3823. return -3202;
  3824. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, data, sizeof(data));
  3825. if (ret != BAD_FUNC_ARG)
  3826. return -3203;
  3827. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, NULL);
  3828. if (ret != BAD_FUNC_ARG)
  3829. return -3204;
  3830. ret = wc_HashFinal(&hash, WC_HASH_TYPE_SHA256, NULL);
  3831. if (ret != BAD_FUNC_ARG)
  3832. return -3205;
  3833. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, out);
  3834. if (ret != BAD_FUNC_ARG)
  3835. return -3206;
  3836. /* Try invalid hash algorithms. */
  3837. for (i = 0; i < (int)(sizeof(typesBad)/sizeof(*typesBad)); i++) {
  3838. ret = wc_HashInit(&hash, typesBad[i]);
  3839. if (ret != BAD_FUNC_ARG)
  3840. return -3207 - i;
  3841. ret = wc_HashUpdate(&hash, typesBad[i], data, sizeof(data));
  3842. if (ret != BAD_FUNC_ARG)
  3843. return -3217 - i;
  3844. ret = wc_HashFinal(&hash, typesBad[i], out);
  3845. if (ret != BAD_FUNC_ARG)
  3846. return -3227 - i;
  3847. wc_HashFree(&hash, typesBad[i]);
  3848. }
  3849. /* Try valid hash algorithms. */
  3850. for (i = 0, j = 0; i < (int)(sizeof(typesGood)/sizeof(*typesGood)); i++) {
  3851. exp_ret = 0;
  3852. if (typesGood[i] == typesNoImpl[j]) {
  3853. /* Recognized but no implementation compiled in. */
  3854. exp_ret = HASH_TYPE_E;
  3855. j++;
  3856. }
  3857. ret = wc_HashInit(&hash, typesGood[i]);
  3858. if (ret != exp_ret)
  3859. return -3237 - i;
  3860. ret = wc_HashUpdate(&hash, typesGood[i], data, sizeof(data));
  3861. if (ret != exp_ret)
  3862. return -3247 - i;
  3863. ret = wc_HashFinal(&hash, typesGood[i], out);
  3864. if (ret != exp_ret)
  3865. return -3257 - i;
  3866. wc_HashFree(&hash, typesGood[i]);
  3867. digestSz = wc_HashGetDigestSize(typesGood[i]);
  3868. if (exp_ret < 0 && digestSz != exp_ret)
  3869. return -3267 - i;
  3870. if (exp_ret == 0 && digestSz < 0)
  3871. return -3277 - i;
  3872. if (exp_ret == 0) {
  3873. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut,
  3874. digestSz - 1);
  3875. if (ret != BUFFER_E)
  3876. return -3287 - i;
  3877. }
  3878. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut, digestSz);
  3879. if (ret != exp_ret)
  3880. return -3297 - i;
  3881. if (exp_ret == 0 && XMEMCMP(out, hashOut, digestSz) != 0)
  3882. return -3307 -i;
  3883. ret = wc_HashGetBlockSize(typesGood[i]);
  3884. if (exp_ret < 0 && ret != exp_ret)
  3885. return -3308 - i;
  3886. if (exp_ret == 0 && ret < 0)
  3887. return -3318 - i;
  3888. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  3889. ret = wc_HashGetOID(typesGood[i]);
  3890. if (ret == BAD_FUNC_ARG ||
  3891. (exp_ret == 0 && ret == HASH_TYPE_E) ||
  3892. (exp_ret != 0 && ret != HASH_TYPE_E)) {
  3893. return -3328 - i;
  3894. }
  3895. hashType = wc_OidGetHash(ret);
  3896. if (exp_ret == 0 && hashType != typesGood[i])
  3897. return -3338 - i;
  3898. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  3899. }
  3900. for (i = 0; i < (int)(sizeof(typesHashBad)/sizeof(*typesHashBad)); i++) {
  3901. ret = wc_Hash(typesHashBad[i], data, sizeof(data), out, sizeof(out));
  3902. if (ret != BAD_FUNC_ARG && ret != BUFFER_E)
  3903. return -3348 - i;
  3904. }
  3905. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  3906. ret = wc_HashGetOID(WC_HASH_TYPE_MD2);
  3907. #ifdef WOLFSSL_MD2
  3908. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3909. return -3358;
  3910. #else
  3911. if (ret != HASH_TYPE_E)
  3912. return -3359;
  3913. #endif
  3914. hashType = wc_OidGetHash(646); /* Md2h */
  3915. #ifdef WOLFSSL_MD2
  3916. if (hashType != WC_HASH_TYPE_MD2)
  3917. return -3360;
  3918. #else
  3919. if (hashType != WC_HASH_TYPE_NONE)
  3920. return -3361;
  3921. #endif
  3922. ret = wc_HashGetOID(WC_HASH_TYPE_MD5_SHA);
  3923. #ifndef NO_MD5
  3924. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3925. return -3362;
  3926. #else
  3927. if (ret != HASH_TYPE_E)
  3928. return -3363;
  3929. #endif
  3930. ret = wc_HashGetOID(WC_HASH_TYPE_MD4);
  3931. if (ret != BAD_FUNC_ARG)
  3932. return -3364;
  3933. ret = wc_HashGetOID(WC_HASH_TYPE_NONE);
  3934. if (ret != BAD_FUNC_ARG)
  3935. return -3365;
  3936. hashType = wc_OidGetHash(0);
  3937. if (hashType != WC_HASH_TYPE_NONE)
  3938. return -3366;
  3939. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  3940. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD2);
  3941. #ifdef WOLFSSL_MD2
  3942. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3943. return -3367;
  3944. #else
  3945. if (ret != HASH_TYPE_E)
  3946. return -3368;
  3947. #endif
  3948. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD2);
  3949. #ifdef WOLFSSL_MD2
  3950. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3951. return -3369;
  3952. #else
  3953. if (ret != HASH_TYPE_E)
  3954. return -3370;
  3955. #endif
  3956. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD4);
  3957. #ifndef NO_MD4
  3958. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3959. return -3371;
  3960. #else
  3961. if (ret != HASH_TYPE_E)
  3962. return -3372;
  3963. #endif
  3964. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD4);
  3965. #ifndef NO_MD4
  3966. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3967. return -3373;
  3968. #else
  3969. if (ret != HASH_TYPE_E)
  3970. return -3374;
  3971. #endif
  3972. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD5_SHA);
  3973. #if !defined(NO_MD5) && !defined(NO_SHA)
  3974. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3975. return -3375;
  3976. #else
  3977. if (ret != HASH_TYPE_E)
  3978. return -3376;
  3979. #endif
  3980. ret = wc_HashGetBlockSize(WC_HASH_TYPE_BLAKE2B);
  3981. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  3982. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3983. return -3377;
  3984. #else
  3985. if (ret != HASH_TYPE_E)
  3986. return -3378;
  3987. #endif
  3988. ret = wc_HashGetDigestSize(WC_HASH_TYPE_BLAKE2B);
  3989. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  3990. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3991. return -3379;
  3992. #else
  3993. if (ret != HASH_TYPE_E)
  3994. return -3380;
  3995. #endif
  3996. ret = wc_HashGetBlockSize(WC_HASH_TYPE_NONE);
  3997. if (ret != BAD_FUNC_ARG)
  3998. return -3381;
  3999. ret = wc_HashGetDigestSize(WC_HASH_TYPE_NONE);
  4000. if (ret != BAD_FUNC_ARG)
  4001. return -3382;
  4002. #if !defined(NO_CERTS) && !defined(NO_ASN)
  4003. #if defined(WOLFSSL_MD2) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  4004. ret = wc_GetCTC_HashOID(MD2);
  4005. if (ret == 0)
  4006. return -3383;
  4007. #endif
  4008. #ifndef NO_MD5
  4009. ret = wc_GetCTC_HashOID(WC_MD5);
  4010. if (ret == 0)
  4011. return -3384;
  4012. #endif
  4013. #ifndef NO_SHA
  4014. ret = wc_GetCTC_HashOID(WC_SHA);
  4015. if (ret == 0)
  4016. return -3385;
  4017. #endif
  4018. #ifdef WOLFSSL_SHA224
  4019. ret = wc_GetCTC_HashOID(WC_SHA224);
  4020. if (ret == 0)
  4021. return -3386;
  4022. #endif
  4023. #ifndef NO_SHA256
  4024. ret = wc_GetCTC_HashOID(WC_SHA256);
  4025. if (ret == 0)
  4026. return -3387;
  4027. #endif
  4028. #ifdef WOLFSSL_SHA384
  4029. ret = wc_GetCTC_HashOID(WC_SHA384);
  4030. if (ret == 0)
  4031. return -3388;
  4032. #endif
  4033. #ifdef WOLFSSL_SHA512
  4034. ret = wc_GetCTC_HashOID(WC_SHA512);
  4035. if (ret == 0)
  4036. return -3389;
  4037. #endif
  4038. ret = wc_GetCTC_HashOID(-1);
  4039. if (ret != 0)
  4040. return -3390;
  4041. #endif
  4042. return 0;
  4043. }
  4044. #endif /* !NO_HASH_WRAPPER */
  4045. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS) && \
  4046. defined(HAVE_FIPS_VERSION) && \
  4047. (HAVE_FIPS_VERSION >= 5))
  4048. WOLFSSL_TEST_SUBROUTINE int hmac_md5_test(void)
  4049. {
  4050. Hmac hmac;
  4051. byte hash[WC_MD5_DIGEST_SIZE];
  4052. const char* keys[]=
  4053. {
  4054. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  4055. "Jefe",
  4056. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4057. };
  4058. testVector a, b, c;
  4059. testVector test_hmac[3];
  4060. int ret;
  4061. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4062. a.input = "Hi There";
  4063. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  4064. "\x9d";
  4065. a.inLen = XSTRLEN(a.input);
  4066. a.outLen = WC_MD5_DIGEST_SIZE;
  4067. b.input = "what do ya want for nothing?";
  4068. b.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
  4069. "\x38";
  4070. b.inLen = XSTRLEN(b.input);
  4071. b.outLen = WC_MD5_DIGEST_SIZE;
  4072. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4073. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4074. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4075. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4076. c.output = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
  4077. "\xf6";
  4078. c.inLen = XSTRLEN(c.input);
  4079. c.outLen = WC_MD5_DIGEST_SIZE;
  4080. test_hmac[0] = a;
  4081. test_hmac[1] = b;
  4082. test_hmac[2] = c;
  4083. for (i = 0; i < times; ++i) {
  4084. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4085. if (i == 1) {
  4086. continue; /* cavium can't handle short keys, fips not allowed */
  4087. }
  4088. #endif
  4089. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0) {
  4090. return -3400;
  4091. }
  4092. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[i],
  4093. (word32)XSTRLEN(keys[i]));
  4094. if (ret != 0)
  4095. return -3401;
  4096. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4097. (word32)test_hmac[i].inLen);
  4098. if (ret != 0)
  4099. return -3402;
  4100. ret = wc_HmacFinal(&hmac, hash);
  4101. if (ret != 0)
  4102. return -3403;
  4103. if (XMEMCMP(hash, test_hmac[i].output, WC_MD5_DIGEST_SIZE) != 0)
  4104. return -3404 - i;
  4105. wc_HmacFree(&hmac);
  4106. }
  4107. #ifndef HAVE_FIPS
  4108. if (wc_HmacSizeByType(WC_MD5) != WC_MD5_DIGEST_SIZE)
  4109. return -3414;
  4110. #endif
  4111. return 0;
  4112. }
  4113. #endif /* !NO_HMAC && !NO_MD5 && (!HAVE_FIPS || (HAVE_FIPS_VERSION < 5)) */
  4114. #if !defined(NO_HMAC) && !defined(NO_SHA)
  4115. WOLFSSL_TEST_SUBROUTINE int hmac_sha_test(void)
  4116. {
  4117. Hmac hmac;
  4118. byte hash[WC_SHA_DIGEST_SIZE];
  4119. const char* keys[]=
  4120. {
  4121. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4122. "\x0b\x0b\x0b",
  4123. "Jefe",
  4124. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4125. "\xAA\xAA\xAA"
  4126. };
  4127. testVector a, b, c;
  4128. testVector test_hmac[3];
  4129. int ret;
  4130. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4131. a.input = "Hi There";
  4132. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  4133. "\x8e\xf1\x46\xbe\x00";
  4134. a.inLen = XSTRLEN(a.input);
  4135. a.outLen = WC_SHA_DIGEST_SIZE;
  4136. b.input = "what do ya want for nothing?";
  4137. b.output = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf"
  4138. "\x9c\x25\x9a\x7c\x79";
  4139. b.inLen = XSTRLEN(b.input);
  4140. b.outLen = WC_SHA_DIGEST_SIZE;
  4141. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4142. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4143. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4144. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4145. c.output = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b"
  4146. "\x4f\x63\xf1\x75\xd3";
  4147. c.inLen = XSTRLEN(c.input);
  4148. c.outLen = WC_SHA_DIGEST_SIZE;
  4149. test_hmac[0] = a;
  4150. test_hmac[1] = b;
  4151. test_hmac[2] = c;
  4152. for (i = 0; i < times; ++i) {
  4153. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4154. if (i == 1)
  4155. continue; /* cavium can't handle short keys, fips not allowed */
  4156. #endif
  4157. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4158. return -3500;
  4159. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[i],
  4160. (word32)XSTRLEN(keys[i]));
  4161. if (ret != 0)
  4162. return -3501;
  4163. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4164. (word32)test_hmac[i].inLen);
  4165. if (ret != 0)
  4166. return -3502;
  4167. ret = wc_HmacFinal(&hmac, hash);
  4168. if (ret != 0)
  4169. return -3503;
  4170. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA_DIGEST_SIZE) != 0)
  4171. return -3504 - i;
  4172. wc_HmacFree(&hmac);
  4173. }
  4174. #ifndef HAVE_FIPS
  4175. if (wc_HmacSizeByType(WC_SHA) != WC_SHA_DIGEST_SIZE)
  4176. return -3514;
  4177. #endif
  4178. return 0;
  4179. }
  4180. #endif
  4181. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  4182. WOLFSSL_TEST_SUBROUTINE int hmac_sha224_test(void)
  4183. {
  4184. Hmac hmac;
  4185. byte hash[WC_SHA224_DIGEST_SIZE];
  4186. const char* keys[]=
  4187. {
  4188. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4189. "\x0b\x0b\x0b",
  4190. "Jefe",
  4191. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4192. "\xAA\xAA\xAA",
  4193. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4194. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4195. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4196. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4197. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4198. };
  4199. testVector a, b, c, d;
  4200. testVector test_hmac[4];
  4201. int ret;
  4202. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4203. a.input = "Hi There";
  4204. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  4205. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  4206. a.inLen = XSTRLEN(a.input);
  4207. a.outLen = WC_SHA224_DIGEST_SIZE;
  4208. b.input = "what do ya want for nothing?";
  4209. b.output = "\xa3\x0e\x01\x09\x8b\xc6\xdb\xbf\x45\x69\x0f\x3a\x7e\x9e\x6d"
  4210. "\x0f\x8b\xbe\xa2\xa3\x9e\x61\x48\x00\x8f\xd0\x5e\x44";
  4211. b.inLen = XSTRLEN(b.input);
  4212. b.outLen = WC_SHA224_DIGEST_SIZE;
  4213. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4214. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4215. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4216. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4217. c.output = "\x7f\xb3\xcb\x35\x88\xc6\xc1\xf6\xff\xa9\x69\x4d\x7d\x6a\xd2"
  4218. "\x64\x93\x65\xb0\xc1\xf6\x5d\x69\xd1\xec\x83\x33\xea";
  4219. c.inLen = XSTRLEN(c.input);
  4220. c.outLen = WC_SHA224_DIGEST_SIZE;
  4221. d.input = "Big Key Input";
  4222. d.output = "\xe7\x4e\x2b\x8a\xa9\xf0\x37\x2f\xed\xae\x70\x0c\x49\x47\xf1"
  4223. "\x46\x54\xa7\x32\x6b\x55\x01\x87\xd2\xc8\x02\x0e\x3a";
  4224. d.inLen = XSTRLEN(d.input);
  4225. d.outLen = WC_SHA224_DIGEST_SIZE;
  4226. test_hmac[0] = a;
  4227. test_hmac[1] = b;
  4228. test_hmac[2] = c;
  4229. test_hmac[3] = d;
  4230. for (i = 0; i < times; ++i) {
  4231. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4232. if (i == 1)
  4233. continue; /* cavium can't handle short keys, fips not allowed */
  4234. #endif
  4235. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4236. return -3600;
  4237. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[i],
  4238. (word32)XSTRLEN(keys[i]));
  4239. if (ret != 0)
  4240. return -3601;
  4241. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4242. (word32)test_hmac[i].inLen);
  4243. if (ret != 0)
  4244. return -3602;
  4245. ret = wc_HmacFinal(&hmac, hash);
  4246. if (ret != 0)
  4247. return -3603;
  4248. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  4249. return -3604 - i;
  4250. wc_HmacFree(&hmac);
  4251. }
  4252. #ifndef HAVE_FIPS
  4253. if (wc_HmacSizeByType(WC_SHA224) != WC_SHA224_DIGEST_SIZE)
  4254. return -3614;
  4255. #endif
  4256. return 0;
  4257. }
  4258. #endif
  4259. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  4260. WOLFSSL_TEST_SUBROUTINE int hmac_sha256_test(void)
  4261. {
  4262. Hmac hmac;
  4263. byte hash[WC_SHA256_DIGEST_SIZE];
  4264. const char* keys[]=
  4265. {
  4266. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4267. "\x0b\x0b\x0b",
  4268. "Jefe",
  4269. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4270. "\xAA\xAA\xAA",
  4271. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4272. "\xAA\xAA\xAA",
  4273. };
  4274. testVector a, b, c, d;
  4275. testVector test_hmac[4];
  4276. int ret;
  4277. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4278. a.input = "Hi There";
  4279. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  4280. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  4281. "\xcf\xf7";
  4282. a.inLen = XSTRLEN(a.input);
  4283. a.outLen = WC_SHA256_DIGEST_SIZE;
  4284. b.input = "what do ya want for nothing?";
  4285. b.output = "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75"
  4286. "\xc7\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec"
  4287. "\x38\x43";
  4288. b.inLen = XSTRLEN(b.input);
  4289. b.outLen = WC_SHA256_DIGEST_SIZE;
  4290. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4291. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4292. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4293. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4294. c.output = "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81"
  4295. "\xa7\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5"
  4296. "\x65\xfe";
  4297. c.inLen = XSTRLEN(c.input);
  4298. c.outLen = WC_SHA256_DIGEST_SIZE;
  4299. d.input = 0;
  4300. d.output = "\x86\xe5\x4f\xd4\x48\x72\x5d\x7e\x5d\xcf\xe2\x23\x53\xc8\x28"
  4301. "\xaf\x48\x78\x1e\xb4\x8c\xae\x81\x06\xa7\xe1\xd4\x98\x94\x9f"
  4302. "\x3e\x46";
  4303. d.inLen = 0;
  4304. d.outLen = WC_SHA256_DIGEST_SIZE;
  4305. test_hmac[0] = a;
  4306. test_hmac[1] = b;
  4307. test_hmac[2] = c;
  4308. test_hmac[3] = d;
  4309. for (i = 0; i < times; ++i) {
  4310. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4311. if (i == 1)
  4312. continue; /* cavium can't handle short keys, fips not allowed */
  4313. #endif
  4314. #if defined(HAVE_INTEL_QA) || defined(HAVE_CAVIUM)
  4315. if (i == 3)
  4316. continue; /* QuickAssist can't handle empty HMAC */
  4317. #endif
  4318. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4319. return -3700 - i;
  4320. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[i],
  4321. (word32)XSTRLEN(keys[i]));
  4322. if (ret != 0)
  4323. return -3710 - i;
  4324. if (test_hmac[i].input != NULL) {
  4325. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4326. (word32)test_hmac[i].inLen);
  4327. if (ret != 0)
  4328. return -3720 - i;
  4329. }
  4330. ret = wc_HmacFinal(&hmac, hash);
  4331. if (ret != 0)
  4332. return -3730 - i;
  4333. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  4334. return -3740 - i;
  4335. wc_HmacFree(&hmac);
  4336. }
  4337. #ifndef HAVE_FIPS
  4338. if (wc_HmacSizeByType(WC_SHA256) != WC_SHA256_DIGEST_SIZE)
  4339. return -3750;
  4340. if (wc_HmacSizeByType(20) != BAD_FUNC_ARG)
  4341. return -3751;
  4342. #endif
  4343. if (wolfSSL_GetHmacMaxSize() != WC_MAX_DIGEST_SIZE)
  4344. return -3752;
  4345. return 0;
  4346. }
  4347. #endif
  4348. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  4349. WOLFSSL_TEST_SUBROUTINE int hmac_sha384_test(void)
  4350. {
  4351. Hmac hmac;
  4352. byte hash[WC_SHA384_DIGEST_SIZE];
  4353. const char* keys[]=
  4354. {
  4355. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4356. "\x0b\x0b\x0b",
  4357. "Jefe",
  4358. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4359. "\xAA\xAA\xAA",
  4360. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4361. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4362. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4363. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4364. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4365. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4366. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4367. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4368. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4369. };
  4370. testVector a, b, c, d;
  4371. testVector test_hmac[4];
  4372. int ret;
  4373. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4374. a.input = "Hi There";
  4375. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  4376. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  4377. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  4378. "\xfa\x9c\xb6";
  4379. a.inLen = XSTRLEN(a.input);
  4380. a.outLen = WC_SHA384_DIGEST_SIZE;
  4381. b.input = "what do ya want for nothing?";
  4382. b.output = "\xaf\x45\xd2\xe3\x76\x48\x40\x31\x61\x7f\x78\xd2\xb5\x8a\x6b"
  4383. "\x1b\x9c\x7e\xf4\x64\xf5\xa0\x1b\x47\xe4\x2e\xc3\x73\x63\x22"
  4384. "\x44\x5e\x8e\x22\x40\xca\x5e\x69\xe2\xc7\x8b\x32\x39\xec\xfa"
  4385. "\xb2\x16\x49";
  4386. b.inLen = XSTRLEN(b.input);
  4387. b.outLen = WC_SHA384_DIGEST_SIZE;
  4388. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4389. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4390. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4391. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4392. c.output = "\x88\x06\x26\x08\xd3\xe6\xad\x8a\x0a\xa2\xac\xe0\x14\xc8\xa8"
  4393. "\x6f\x0a\xa6\x35\xd9\x47\xac\x9f\xeb\xe8\x3e\xf4\xe5\x59\x66"
  4394. "\x14\x4b\x2a\x5a\xb3\x9d\xc1\x38\x14\xb9\x4e\x3a\xb6\xe1\x01"
  4395. "\xa3\x4f\x27";
  4396. c.inLen = XSTRLEN(c.input);
  4397. c.outLen = WC_SHA384_DIGEST_SIZE;
  4398. d.input = "Big Key Input";
  4399. d.output = "\xd2\x3d\x29\x6e\xf5\x1e\x23\x23\x49\x18\xb3\xbf\x4c\x38\x7b"
  4400. "\x31\x21\x17\xbb\x09\x73\x27\xf8\x12\x9d\xe9\xc6\x5d\xf9\x54"
  4401. "\xd6\x38\x5a\x68\x53\x14\xee\xe0\xa6\x4f\x36\x7e\xb2\xf3\x1a"
  4402. "\x57\x41\x69";
  4403. d.inLen = XSTRLEN(d.input);
  4404. d.outLen = WC_SHA384_DIGEST_SIZE;
  4405. test_hmac[0] = a;
  4406. test_hmac[1] = b;
  4407. test_hmac[2] = c;
  4408. test_hmac[3] = d;
  4409. for (i = 0; i < times; ++i) {
  4410. #if defined(HAVE_FIPS)
  4411. if (i == 1)
  4412. continue; /* fips not allowed */
  4413. #endif
  4414. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4415. return -3800;
  4416. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[i],
  4417. (word32)XSTRLEN(keys[i]));
  4418. if (ret != 0)
  4419. return -3801;
  4420. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4421. (word32)test_hmac[i].inLen);
  4422. if (ret != 0)
  4423. return -3802;
  4424. ret = wc_HmacFinal(&hmac, hash);
  4425. if (ret != 0)
  4426. return -3803;
  4427. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  4428. return -3804 - i;
  4429. wc_HmacFree(&hmac);
  4430. }
  4431. #ifndef HAVE_FIPS
  4432. if (wc_HmacSizeByType(WC_SHA384) != WC_SHA384_DIGEST_SIZE)
  4433. return -3814;
  4434. #endif
  4435. return 0;
  4436. }
  4437. #endif
  4438. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA512)
  4439. WOLFSSL_TEST_SUBROUTINE int hmac_sha512_test(void)
  4440. {
  4441. Hmac hmac;
  4442. byte hash[WC_SHA512_DIGEST_SIZE];
  4443. const char* keys[]=
  4444. {
  4445. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4446. "\x0b\x0b\x0b",
  4447. "Jefe",
  4448. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4449. "\xAA\xAA\xAA",
  4450. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4451. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4452. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4453. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4454. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4455. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4456. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4457. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4458. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4459. };
  4460. testVector a, b, c, d;
  4461. testVector test_hmac[4];
  4462. int ret;
  4463. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4464. a.input = "Hi There";
  4465. a.output = "\x87\xaa\x7c\xde\xa5\xef\x61\x9d\x4f\xf0\xb4\x24\x1a\x1d\x6c"
  4466. "\xb0\x23\x79\xf4\xe2\xce\x4e\xc2\x78\x7a\xd0\xb3\x05\x45\xe1"
  4467. "\x7c\xde\xda\xa8\x33\xb7\xd6\xb8\xa7\x02\x03\x8b\x27\x4e\xae"
  4468. "\xa3\xf4\xe4\xbe\x9d\x91\x4e\xeb\x61\xf1\x70\x2e\x69\x6c\x20"
  4469. "\x3a\x12\x68\x54";
  4470. a.inLen = XSTRLEN(a.input);
  4471. a.outLen = WC_SHA512_DIGEST_SIZE;
  4472. b.input = "what do ya want for nothing?";
  4473. b.output = "\x16\x4b\x7a\x7b\xfc\xf8\x19\xe2\xe3\x95\xfb\xe7\x3b\x56\xe0"
  4474. "\xa3\x87\xbd\x64\x22\x2e\x83\x1f\xd6\x10\x27\x0c\xd7\xea\x25"
  4475. "\x05\x54\x97\x58\xbf\x75\xc0\x5a\x99\x4a\x6d\x03\x4f\x65\xf8"
  4476. "\xf0\xe6\xfd\xca\xea\xb1\xa3\x4d\x4a\x6b\x4b\x63\x6e\x07\x0a"
  4477. "\x38\xbc\xe7\x37";
  4478. b.inLen = XSTRLEN(b.input);
  4479. b.outLen = WC_SHA512_DIGEST_SIZE;
  4480. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4481. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4482. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4483. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4484. c.output = "\xfa\x73\xb0\x08\x9d\x56\xa2\x84\xef\xb0\xf0\x75\x6c\x89\x0b"
  4485. "\xe9\xb1\xb5\xdb\xdd\x8e\xe8\x1a\x36\x55\xf8\x3e\x33\xb2\x27"
  4486. "\x9d\x39\xbf\x3e\x84\x82\x79\xa7\x22\xc8\x06\xb4\x85\xa4\x7e"
  4487. "\x67\xc8\x07\xb9\x46\xa3\x37\xbe\xe8\x94\x26\x74\x27\x88\x59"
  4488. "\xe1\x32\x92\xfb";
  4489. c.inLen = XSTRLEN(c.input);
  4490. c.outLen = WC_SHA512_DIGEST_SIZE;
  4491. d.input = "Big Key Input";
  4492. d.output = "\x3f\xa9\xc9\xe1\xbd\xbb\x04\x55\x1f\xef\xcc\x92\x33\x08\xeb"
  4493. "\xcf\xc1\x9a\x5b\x5b\xc0\x7c\x86\x84\xae\x8c\x40\xaf\xb1\x27"
  4494. "\x87\x38\x92\x04\xa8\xed\xd7\xd7\x07\xa9\x85\xa0\xc2\xcd\x30"
  4495. "\xc0\x56\x14\x49\xbc\x2f\x69\x15\x6a\x97\xd8\x79\x2f\xb3\x3b"
  4496. "\x1e\x18\xfe\xfa";
  4497. d.inLen = XSTRLEN(d.input);
  4498. d.outLen = WC_SHA512_DIGEST_SIZE;
  4499. test_hmac[0] = a;
  4500. test_hmac[1] = b;
  4501. test_hmac[2] = c;
  4502. test_hmac[3] = d;
  4503. for (i = 0; i < times; ++i) {
  4504. #if defined(HAVE_FIPS)
  4505. if (i == 1)
  4506. continue; /* fips not allowed */
  4507. #endif
  4508. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4509. return -3900;
  4510. ret = wc_HmacSetKey(&hmac, WC_SHA512, (byte*)keys[i],
  4511. (word32)XSTRLEN(keys[i]));
  4512. if (ret != 0)
  4513. return -3901;
  4514. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4515. (word32)test_hmac[i].inLen);
  4516. if (ret != 0)
  4517. return -3902;
  4518. ret = wc_HmacFinal(&hmac, hash);
  4519. if (ret != 0)
  4520. return -3903;
  4521. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  4522. return -3904 - i;
  4523. wc_HmacFree(&hmac);
  4524. }
  4525. #ifndef HAVE_FIPS
  4526. if (wc_HmacSizeByType(WC_SHA512) != WC_SHA512_DIGEST_SIZE)
  4527. return -3914;
  4528. #endif
  4529. return 0;
  4530. }
  4531. #endif
  4532. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  4533. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  4534. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  4535. WOLFSSL_TEST_SUBROUTINE int hmac_sha3_test(void)
  4536. {
  4537. Hmac hmac;
  4538. byte hash[WC_SHA3_512_DIGEST_SIZE];
  4539. const char* key[4] =
  4540. {
  4541. "Jefe",
  4542. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4543. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  4544. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  4545. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
  4546. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4547. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4548. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4549. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4550. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4551. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4552. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4553. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4554. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4555. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4556. };
  4557. const char* input[4] =
  4558. {
  4559. "what do ya want for nothing?",
  4560. "Hi There",
  4561. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  4562. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  4563. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  4564. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  4565. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd",
  4566. "Big Key Input"
  4567. };
  4568. const int hashType[4] =
  4569. {
  4570. WC_SHA3_224, WC_SHA3_256, WC_SHA3_384, WC_SHA3_512
  4571. };
  4572. const int hashSz[4] =
  4573. {
  4574. WC_SHA3_224_DIGEST_SIZE, WC_SHA3_256_DIGEST_SIZE,
  4575. WC_SHA3_384_DIGEST_SIZE, WC_SHA3_512_DIGEST_SIZE
  4576. };
  4577. const char* output[16] =
  4578. {
  4579. /* key = jefe, input = what do ya want for nothing? */
  4580. /* HMAC-SHA3-224 */
  4581. "\x7f\xdb\x8d\xd8\x8b\xd2\xf6\x0d\x1b\x79\x86\x34\xad\x38\x68\x11"
  4582. "\xc2\xcf\xc8\x5b\xfa\xf5\xd5\x2b\xba\xce\x5e\x66",
  4583. /* HMAC-SHA3-256 */
  4584. "\xc7\xd4\x07\x2e\x78\x88\x77\xae\x35\x96\xbb\xb0\xda\x73\xb8\x87"
  4585. "\xc9\x17\x1f\x93\x09\x5b\x29\x4a\xe8\x57\xfb\xe2\x64\x5e\x1b\xa5",
  4586. /* HMAC-SHA3-384 */
  4587. "\xf1\x10\x1f\x8c\xbf\x97\x66\xfd\x67\x64\xd2\xed\x61\x90\x3f\x21"
  4588. "\xca\x9b\x18\xf5\x7c\xf3\xe1\xa2\x3c\xa1\x35\x08\xa9\x32\x43\xce"
  4589. "\x48\xc0\x45\xdc\x00\x7f\x26\xa2\x1b\x3f\x5e\x0e\x9d\xf4\xc2\x0a",
  4590. /* HMAC-SHA3-512 */
  4591. "\x5a\x4b\xfe\xab\x61\x66\x42\x7c\x7a\x36\x47\xb7\x47\x29\x2b\x83"
  4592. "\x84\x53\x7c\xdb\x89\xaf\xb3\xbf\x56\x65\xe4\xc5\xe7\x09\x35\x0b"
  4593. "\x28\x7b\xae\xc9\x21\xfd\x7c\xa0\xee\x7a\x0c\x31\xd0\x22\xa9\x5e"
  4594. "\x1f\xc9\x2b\xa9\xd7\x7d\xf8\x83\x96\x02\x75\xbe\xb4\xe6\x20\x24",
  4595. /* key = 0b..., input = Hi There */
  4596. /* HMAC-SHA3-224 */
  4597. "\x3b\x16\x54\x6b\xbc\x7b\xe2\x70\x6a\x03\x1d\xca\xfd\x56\x37\x3d"
  4598. "\x98\x84\x36\x76\x41\xd8\xc5\x9a\xf3\xc8\x60\xf7",
  4599. /* HMAC-SHA3-256 */
  4600. "\xba\x85\x19\x23\x10\xdf\xfa\x96\xe2\xa3\xa4\x0e\x69\x77\x43\x51"
  4601. "\x14\x0b\xb7\x18\x5e\x12\x02\xcd\xcc\x91\x75\x89\xf9\x5e\x16\xbb",
  4602. /* HMAC-SHA3-384 */
  4603. "\x68\xd2\xdc\xf7\xfd\x4d\xdd\x0a\x22\x40\xc8\xa4\x37\x30\x5f\x61"
  4604. "\xfb\x73\x34\xcf\xb5\xd0\x22\x6e\x1b\xc2\x7d\xc1\x0a\x2e\x72\x3a"
  4605. "\x20\xd3\x70\xb4\x77\x43\x13\x0e\x26\xac\x7e\x3d\x53\x28\x86\xbd",
  4606. /* HMAC-SHA3-512 */
  4607. "\xeb\x3f\xbd\x4b\x2e\xaa\xb8\xf5\xc5\x04\xbd\x3a\x41\x46\x5a\xac"
  4608. "\xec\x15\x77\x0a\x7c\xab\xac\x53\x1e\x48\x2f\x86\x0b\x5e\xc7\xba"
  4609. "\x47\xcc\xb2\xc6\xf2\xaf\xce\x8f\x88\xd2\x2b\x6d\xc6\x13\x80\xf2"
  4610. "\x3a\x66\x8f\xd3\x88\x8b\xb8\x05\x37\xc0\xa0\xb8\x64\x07\x68\x9e",
  4611. /* key = aa..., output = dd... */
  4612. /* HMAC-SHA3-224 */
  4613. "\x67\x6c\xfc\x7d\x16\x15\x36\x38\x78\x03\x90\x69\x2b\xe1\x42\xd2"
  4614. "\xdf\x7c\xe9\x24\xb9\x09\xc0\xc0\x8d\xbf\xdc\x1a",
  4615. /* HMAC-SHA3-256 */
  4616. "\x84\xec\x79\x12\x4a\x27\x10\x78\x65\xce\xdd\x8b\xd8\x2d\xa9\x96"
  4617. "\x5e\x5e\xd8\xc3\x7b\x0a\xc9\x80\x05\xa7\xf3\x9e\xd5\x8a\x42\x07",
  4618. /* HMAC-SHA3-384 */
  4619. "\x27\x5c\xd0\xe6\x61\xbb\x8b\x15\x1c\x64\xd2\x88\xf1\xf7\x82\xfb"
  4620. "\x91\xa8\xab\xd5\x68\x58\xd7\x2b\xab\xb2\xd4\x76\xf0\x45\x83\x73"
  4621. "\xb4\x1b\x6a\xb5\xbf\x17\x4b\xec\x42\x2e\x53\xfc\x31\x35\xac\x6e",
  4622. /* HMAC-SHA3-512 */
  4623. "\x30\x9e\x99\xf9\xec\x07\x5e\xc6\xc6\xd4\x75\xed\xa1\x18\x06\x87"
  4624. "\xfc\xf1\x53\x11\x95\x80\x2a\x99\xb5\x67\x74\x49\xa8\x62\x51\x82"
  4625. "\x85\x1c\xb3\x32\xaf\xb6\xa8\x9c\x41\x13\x25\xfb\xcb\xcd\x42\xaf"
  4626. "\xcb\x7b\x6e\x5a\xab\x7e\xa4\x2c\x66\x0f\x97\xfd\x85\x84\xbf\x03",
  4627. /* key = big key, input = Big Key Input */
  4628. /* HMAC-SHA3-224 */
  4629. "\x29\xe0\x5e\x46\xc4\xa4\x5e\x46\x74\xbf\xd7\x2d\x1a\xd8\x66\xdb"
  4630. "\x2d\x0d\x10\x4e\x2b\xfa\xad\x53\x7d\x15\x69\x8b",
  4631. /* HMAC-SHA3-256 */
  4632. "\xb5\x5b\x8d\x64\xb6\x9c\x21\xd0\xbf\x20\x5c\xa2\xf7\xb9\xb1\x4e"
  4633. "\x88\x21\x61\x2c\x66\xc3\x91\xae\x6c\x95\x16\x85\x83\xe6\xf4\x9b",
  4634. /* HMAC-SHA3-384 */
  4635. "\xaa\x91\xb3\xa6\x2f\x56\xa1\xbe\x8c\x3e\x74\x38\xdb\x58\xd9\xd3"
  4636. "\x34\xde\xa0\x60\x6d\x8d\x46\xe0\xec\xa9\xf6\x06\x35\x14\xe6\xed"
  4637. "\x83\xe6\x7c\x77\x24\x6c\x11\xb5\x90\x82\xb5\x75\xda\x7b\x83\x2d",
  4638. /* HMAC-SHA3-512 */
  4639. "\x1c\xc3\xa9\x24\x4a\x4a\x3f\xbd\xc7\x20\x00\x16\x9b\x79\x47\x03"
  4640. "\x78\x75\x2c\xb5\xf1\x2e\x62\x7c\xbe\xef\x4e\x8f\x0b\x11\x2b\x32"
  4641. "\xa0\xee\xc9\xd0\x4d\x64\x64\x0b\x37\xf4\xdd\x66\xf7\x8b\xb3\xad"
  4642. "\x52\x52\x6b\x65\x12\xde\x0d\x7c\xc0\x8b\x60\x01\x6c\x37\xd7\xa8"
  4643. };
  4644. int i = 0, iMax = sizeof(input) / sizeof(input[0]),
  4645. j, jMax = sizeof(hashType) / sizeof(hashType[0]),
  4646. ret;
  4647. #ifdef HAVE_FIPS
  4648. /* FIPS requires a minimum length for HMAC keys, and "Jefe" is too
  4649. * short. Skip it in FIPS builds. */
  4650. i = 1;
  4651. #endif
  4652. for (; i < iMax; i++) {
  4653. for (j = 0; j < jMax; j++) {
  4654. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4655. return -4000;
  4656. ret = wc_HmacSetKey(&hmac, hashType[j], (byte*)key[i],
  4657. (word32)XSTRLEN(key[i]));
  4658. if (ret != 0)
  4659. return -4001;
  4660. ret = wc_HmacUpdate(&hmac, (byte*)input[i],
  4661. (word32)XSTRLEN(input[i]));
  4662. if (ret != 0)
  4663. return -4002;
  4664. ret = wc_HmacFinal(&hmac, hash);
  4665. if (ret != 0)
  4666. return -4003;
  4667. if (XMEMCMP(hash, output[(i*jMax) + j], hashSz[j]) != 0)
  4668. return -4004;
  4669. wc_HmacFree(&hmac);
  4670. if (i > 0)
  4671. continue;
  4672. #ifndef HAVE_FIPS
  4673. ret = wc_HmacSizeByType(hashType[j]);
  4674. if (ret != hashSz[j])
  4675. return -4005;
  4676. #endif
  4677. }
  4678. }
  4679. return 0;
  4680. }
  4681. #endif
  4682. #ifdef WC_RC2
  4683. typedef struct rc2TestVector {
  4684. const char* input;
  4685. const char* output;
  4686. const char* key; /* Key, variable up to 128 bytes */
  4687. const char* iv; /* IV, 8-bytes */
  4688. int inLen;
  4689. int outLen;
  4690. int keyLen;
  4691. int effectiveKeyBits; /* Up to 1024 bits supported */
  4692. } rc2TestVector;
  4693. static int rc2_ecb_test(void)
  4694. {
  4695. int ret = 0;
  4696. byte cipher[RC2_BLOCK_SIZE];
  4697. byte plain[RC2_BLOCK_SIZE];
  4698. rc2TestVector a, b, c, d, e, f, g, h;
  4699. rc2TestVector test_rc2[8];
  4700. int times = sizeof(test_rc2) / sizeof(rc2TestVector), i;
  4701. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4702. a.output = "\xeb\xb7\x73\xf9\x93\x27\x8e\xff";
  4703. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4704. a.inLen = RC2_BLOCK_SIZE;
  4705. a.outLen = RC2_BLOCK_SIZE;
  4706. a.keyLen = 8;
  4707. a.effectiveKeyBits = 63;
  4708. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4709. b.output = "\x27\x8b\x27\xe4\x2e\x2f\x0d\x49";
  4710. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4711. b.inLen = RC2_BLOCK_SIZE;
  4712. b.outLen = RC2_BLOCK_SIZE;
  4713. b.keyLen = 8;
  4714. b.effectiveKeyBits = 64;
  4715. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01";
  4716. c.output = "\x30\x64\x9e\xdf\x9b\xe7\xd2\xc2";
  4717. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  4718. c.inLen = RC2_BLOCK_SIZE;
  4719. c.outLen = RC2_BLOCK_SIZE;
  4720. c.keyLen = 8;
  4721. c.effectiveKeyBits = 64;
  4722. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4723. d.output = "\x61\xa8\xa2\x44\xad\xac\xcc\xf0";
  4724. d.key = "\x88";
  4725. d.inLen = RC2_BLOCK_SIZE;
  4726. d.outLen = RC2_BLOCK_SIZE;
  4727. d.keyLen = 1;
  4728. d.effectiveKeyBits = 64;
  4729. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4730. e.output = "\x6c\xcf\x43\x08\x97\x4c\x26\x7f";
  4731. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  4732. e.inLen = RC2_BLOCK_SIZE;
  4733. e.outLen = RC2_BLOCK_SIZE;
  4734. e.keyLen = 7;
  4735. e.effectiveKeyBits = 64;
  4736. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4737. f.output = "\x1a\x80\x7d\x27\x2b\xbe\x5d\xb1";
  4738. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4739. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4740. f.inLen = RC2_BLOCK_SIZE;
  4741. f.outLen = RC2_BLOCK_SIZE;
  4742. f.keyLen = 16;
  4743. f.effectiveKeyBits = 64;
  4744. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4745. g.output = "\x22\x69\x55\x2a\xb0\xf8\x5c\xa6";
  4746. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4747. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4748. g.inLen = RC2_BLOCK_SIZE;
  4749. g.outLen = RC2_BLOCK_SIZE;
  4750. g.keyLen = 16;
  4751. g.effectiveKeyBits = 128;
  4752. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4753. h.output = "\x5b\x78\xd3\xa4\x3d\xff\xf1\xf1";
  4754. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4755. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  4756. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  4757. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  4758. "\x1e";
  4759. h.inLen = RC2_BLOCK_SIZE;
  4760. h.outLen = RC2_BLOCK_SIZE;
  4761. h.keyLen = 33;
  4762. h.effectiveKeyBits = 129;
  4763. a.iv = b.iv = c.iv = d.iv = e.iv = f.iv = g.iv = h.iv = NULL;
  4764. test_rc2[0] = a;
  4765. test_rc2[1] = b;
  4766. test_rc2[2] = c;
  4767. test_rc2[3] = d;
  4768. test_rc2[4] = e;
  4769. test_rc2[5] = f;
  4770. test_rc2[6] = g;
  4771. test_rc2[7] = h;
  4772. for (i = 0; i < times; ++i) {
  4773. Rc2 enc;
  4774. XMEMSET(cipher, 0, RC2_BLOCK_SIZE);
  4775. XMEMSET(plain, 0, RC2_BLOCK_SIZE);
  4776. ret = wc_Rc2SetKey(&enc, (byte*)test_rc2[i].key, test_rc2[i].keyLen,
  4777. NULL, test_rc2[i].effectiveKeyBits);
  4778. if (ret != 0) {
  4779. return -4100;
  4780. }
  4781. /* ECB encrypt */
  4782. ret = wc_Rc2EcbEncrypt(&enc, cipher, (byte*)test_rc2[i].input,
  4783. (word32)test_rc2[i].outLen);
  4784. if (ret != 0) {
  4785. return -4101;
  4786. }
  4787. if (XMEMCMP(cipher, test_rc2[i].output, test_rc2[i].outLen)) {
  4788. return -4102;
  4789. }
  4790. /* ECB decrypt */
  4791. ret = wc_Rc2EcbDecrypt(&enc, plain, cipher, RC2_BLOCK_SIZE);
  4792. if (ret != 0) {
  4793. return -4103;
  4794. }
  4795. if (XMEMCMP(plain, test_rc2[i].input, RC2_BLOCK_SIZE)) {
  4796. return -4104;
  4797. }
  4798. }
  4799. return 0;
  4800. }
  4801. static int rc2_cbc_test(void)
  4802. {
  4803. int ret = 0;
  4804. byte cipher[128];
  4805. byte plain[128];
  4806. rc2TestVector a, b, c, d, e, f, g, h, i;
  4807. rc2TestVector test_rc2[9];
  4808. int times = sizeof(test_rc2) / sizeof(rc2TestVector), j;
  4809. /* key length = 7, effective key bits = 63 */
  4810. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4811. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4812. a.output = "\xEB\xB7\x73\xF9\x93\x27\x8E\xFF"
  4813. "\xF0\x51\x77\x8B\x65\xDB\x13\x57";
  4814. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4815. a.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4816. a.inLen = RC2_BLOCK_SIZE*2;
  4817. a.outLen = RC2_BLOCK_SIZE*2;
  4818. a.keyLen = 8;
  4819. a.effectiveKeyBits = 63;
  4820. /* key length = 8, effective key bits = 64, all 0xFF */
  4821. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff"
  4822. "\xff\xff\xff\xff\xff\xff\xff\xff";
  4823. b.output = "\xA3\xA1\x12\x65\x4F\x81\xC5\xCD"
  4824. "\xB6\x94\x3E\xEA\x3E\x8B\x9D\x1F";
  4825. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4826. b.iv = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4827. b.inLen = RC2_BLOCK_SIZE*2;
  4828. b.outLen = RC2_BLOCK_SIZE*2;
  4829. b.keyLen = 8;
  4830. b.effectiveKeyBits = 64;
  4831. /* key length = 8, effective key bits = 64 */
  4832. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01"
  4833. "\x10\x00\x00\x00\x00\x00\x00\x01";
  4834. c.output = "\xB5\x70\x14\xA2\x5F\x40\xE3\x6D"
  4835. "\x81\x99\x8D\xE0\xB5\xD5\x3A\x05";
  4836. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  4837. c.iv = "\x30\x00\x00\x00\x00\x00\x00\x00";
  4838. c.inLen = RC2_BLOCK_SIZE*2;
  4839. c.outLen = RC2_BLOCK_SIZE*2;
  4840. c.keyLen = 8;
  4841. c.effectiveKeyBits = 64;
  4842. /* key length = 1, effective key bits = 64 */
  4843. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4844. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4845. d.output = "\x61\xA8\xA2\x44\xAD\xAC\xCC\xF0"
  4846. "\x6D\x19\xE8\xF1\xFC\xE7\x38\x87";
  4847. d.key = "\x88";
  4848. d.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4849. d.inLen = RC2_BLOCK_SIZE*2;
  4850. d.outLen = RC2_BLOCK_SIZE*2;
  4851. d.keyLen = 1;
  4852. d.effectiveKeyBits = 64;
  4853. /* key length = 7, effective key bits = 64 */
  4854. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4855. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4856. e.output = "\x6C\xCF\x43\x08\x97\x4C\x26\x7F"
  4857. "\xCC\x3C\x53\x57\x7C\xA1\xA4\x4B";
  4858. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  4859. e.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4860. e.inLen = RC2_BLOCK_SIZE*2;
  4861. e.outLen = RC2_BLOCK_SIZE*2;
  4862. e.keyLen = 7;
  4863. e.effectiveKeyBits = 64;
  4864. /* key length = 16, effective key bits = 64 */
  4865. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4866. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4867. f.output = "\x1A\x80\x7D\x27\x2B\xBE\x5D\xB1"
  4868. "\x64\xEF\xE1\xC3\xB8\xAD\xFB\xBA";
  4869. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4870. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4871. f.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4872. f.inLen = RC2_BLOCK_SIZE*2;
  4873. f.outLen = RC2_BLOCK_SIZE*2;
  4874. f.keyLen = 16;
  4875. f.effectiveKeyBits = 64;
  4876. /* key length = 16, effective bits = 128 */
  4877. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4878. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4879. g.output = "\x22\x69\x55\x2A\xB0\xF8\x5C\xA6"
  4880. "\x53\x6E\xFD\x2D\x89\xE1\x2A\x73";
  4881. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4882. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4883. g.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4884. g.inLen = RC2_BLOCK_SIZE*2;
  4885. g.outLen = RC2_BLOCK_SIZE*2;
  4886. g.keyLen = 16;
  4887. g.effectiveKeyBits = 128;
  4888. /* key length = 33, effective bits = 129 */
  4889. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4890. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4891. h.output = "\x5B\x78\xD3\xA4\x3D\xFF\xF1\xF1"
  4892. "\x45\x30\xA8\xD5\xC7\x7C\x46\x19";
  4893. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4894. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  4895. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  4896. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  4897. "\x1e";
  4898. h.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4899. h.inLen = RC2_BLOCK_SIZE*2;
  4900. h.outLen = RC2_BLOCK_SIZE*2;
  4901. h.keyLen = 33;
  4902. h.effectiveKeyBits = 129;
  4903. /* key length = 10, effective bits = 40 */
  4904. i.input = "\x11\x22\x33\x44\x55\x66\x77\x88"
  4905. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00"
  4906. "\x11\x22\x33\x44\x55\x66\x77\x88"
  4907. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00";
  4908. i.output = "\x71\x2D\x11\x99\xC9\xA0\x78\x4F"
  4909. "\xCD\xF1\x1E\x3D\xFD\x21\x7E\xDB"
  4910. "\xB2\x6E\x0D\xA4\x72\xBC\x31\x51"
  4911. "\x48\xEF\x4E\x68\x3B\xDC\xCD\x7D";
  4912. i.key = "\x26\x1E\x57\x8E\xC9\x62\xBF\xB8"
  4913. "\x3E\x96";
  4914. i.iv = "\x01\x02\x03\x04\x05\x06\x07\x08";
  4915. i.inLen = RC2_BLOCK_SIZE*4;
  4916. i.outLen = RC2_BLOCK_SIZE*4;
  4917. i.keyLen = 10;
  4918. i.effectiveKeyBits = 40;
  4919. test_rc2[0] = a;
  4920. test_rc2[1] = b;
  4921. test_rc2[2] = c;
  4922. test_rc2[3] = d;
  4923. test_rc2[4] = e;
  4924. test_rc2[5] = f;
  4925. test_rc2[6] = g;
  4926. test_rc2[7] = h;
  4927. test_rc2[8] = i;
  4928. for (j = 0; j < times; ++j) {
  4929. Rc2 rc2;
  4930. XMEMSET(cipher, 0, sizeof(cipher));
  4931. XMEMSET(plain, 0, sizeof(plain));
  4932. ret = wc_Rc2SetKey(&rc2, (byte*)test_rc2[j].key, test_rc2[j].keyLen,
  4933. (byte*)test_rc2[j].iv, test_rc2[j].effectiveKeyBits);
  4934. if (ret != 0) {
  4935. return -4200;
  4936. }
  4937. ret = wc_Rc2CbcEncrypt(&rc2, cipher, (byte*)test_rc2[j].input,
  4938. test_rc2[j].inLen);
  4939. if (ret != 0) {
  4940. return -4201;
  4941. }
  4942. if (XMEMCMP(cipher, (byte*)test_rc2[j].output, test_rc2[j].outLen)) {
  4943. return -4202;
  4944. }
  4945. /* reset IV for decrypt, since overriden by encrypt operation */
  4946. ret = wc_Rc2SetIV(&rc2, (byte*)test_rc2[j].iv);
  4947. if (ret != 0) {
  4948. return -4203;
  4949. }
  4950. ret = wc_Rc2CbcDecrypt(&rc2, plain, cipher, test_rc2[j].outLen);
  4951. if (ret != 0) {
  4952. return -4204;
  4953. }
  4954. if (XMEMCMP(plain, (byte*)test_rc2[j].input, test_rc2[j].inLen)) {
  4955. return -4205;
  4956. }
  4957. }
  4958. return 0;
  4959. }
  4960. WOLFSSL_TEST_SUBROUTINE int rc2_test(void)
  4961. {
  4962. int ret = 0;
  4963. ret = rc2_ecb_test();
  4964. if (ret != 0) {
  4965. return ret;
  4966. }
  4967. return rc2_cbc_test();
  4968. }
  4969. #endif
  4970. #ifndef NO_RC4
  4971. WOLFSSL_TEST_SUBROUTINE int arc4_test(void)
  4972. {
  4973. byte cipher[16];
  4974. byte plain[16];
  4975. const char* keys[] =
  4976. {
  4977. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  4978. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  4979. "\x00\x00\x00\x00\x00\x00\x00\x00",
  4980. "\xef\x01\x23\x45"
  4981. };
  4982. testVector a, b, c, d;
  4983. testVector test_arc4[4];
  4984. int times = sizeof(test_arc4) / sizeof(testVector), i;
  4985. a.input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  4986. a.output = "\x75\xb7\x87\x80\x99\xe0\xc5\x96";
  4987. a.inLen = 8;
  4988. a.outLen = 8;
  4989. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4990. b.output = "\x74\x94\xc2\xe7\x10\x4b\x08\x79";
  4991. b.inLen = 8;
  4992. b.outLen = 8;
  4993. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4994. c.output = "\xde\x18\x89\x41\xa3\x37\x5d\x3a";
  4995. c.inLen = 8;
  4996. c.outLen = 8;
  4997. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  4998. d.output = "\xd6\xa1\x41\xa7\xec\x3c\x38\xdf\xbd\x61";
  4999. d.inLen = 10;
  5000. d.outLen = 10;
  5001. test_arc4[0] = a;
  5002. test_arc4[1] = b;
  5003. test_arc4[2] = c;
  5004. test_arc4[3] = d;
  5005. for (i = 0; i < times; ++i) {
  5006. Arc4 enc;
  5007. Arc4 dec;
  5008. int keylen = 8; /* XSTRLEN with key 0x00 not good */
  5009. if (i == 3)
  5010. keylen = 4;
  5011. if (wc_Arc4Init(&enc, HEAP_HINT, devId) != 0)
  5012. return -4400;
  5013. if (wc_Arc4Init(&dec, HEAP_HINT, devId) != 0)
  5014. return -4401;
  5015. wc_Arc4SetKey(&enc, (byte*)keys[i], keylen);
  5016. wc_Arc4SetKey(&dec, (byte*)keys[i], keylen);
  5017. wc_Arc4Process(&enc, cipher, (byte*)test_arc4[i].input,
  5018. (word32)test_arc4[i].outLen);
  5019. wc_Arc4Process(&dec, plain, cipher, (word32)test_arc4[i].outLen);
  5020. if (XMEMCMP(plain, test_arc4[i].input, test_arc4[i].outLen))
  5021. return -4402 - i;
  5022. if (XMEMCMP(cipher, test_arc4[i].output, test_arc4[i].outLen))
  5023. return -4412 - i;
  5024. wc_Arc4Free(&enc);
  5025. wc_Arc4Free(&dec);
  5026. }
  5027. return 0;
  5028. }
  5029. #endif
  5030. #ifdef HAVE_CHACHA
  5031. WOLFSSL_TEST_SUBROUTINE int chacha_test(void)
  5032. {
  5033. ChaCha enc;
  5034. ChaCha dec;
  5035. byte cipher[128];
  5036. byte plain[128];
  5037. byte sliver[64];
  5038. byte input[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  5039. word32 keySz = 32;
  5040. int ret = 0;
  5041. int i;
  5042. int times = 4;
  5043. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  5044. {
  5045. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5046. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5047. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5048. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5049. };
  5050. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  5051. {
  5052. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5053. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5054. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5055. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  5056. };
  5057. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  5058. {
  5059. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5060. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5061. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5062. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5063. };
  5064. /* 128 bit key */
  5065. WOLFSSL_SMALL_STACK_STATIC const byte key4[] =
  5066. {
  5067. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5068. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5069. };
  5070. const byte* keys[] = {key1, key2, key3, key4};
  5071. WOLFSSL_SMALL_STACK_STATIC const byte ivs1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  5072. WOLFSSL_SMALL_STACK_STATIC const byte ivs2[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  5073. WOLFSSL_SMALL_STACK_STATIC const byte ivs3[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00};
  5074. WOLFSSL_SMALL_STACK_STATIC const byte ivs4[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  5075. const byte* ivs[] = {ivs1, ivs2, ivs3, ivs4};
  5076. #ifndef BENCH_EMBEDDED
  5077. WOLFSSL_SMALL_STACK_STATIC const byte cipher_big_result[] = {
  5078. 0x06, 0xa6, 0x5d, 0x31, 0x21, 0x6c, 0xdb, 0x37, 0x48, 0x7c, 0x01, 0x9d,
  5079. 0x72, 0xdf, 0x0a, 0x5b, 0x64, 0x74, 0x20, 0xba, 0x9e, 0xe0, 0x26, 0x7a,
  5080. 0xbf, 0xdf, 0x83, 0x34, 0x3b, 0x4f, 0x94, 0x3f, 0x37, 0x89, 0xaf, 0x00,
  5081. 0xdf, 0x0f, 0x2e, 0x75, 0x16, 0x41, 0xf6, 0x7a, 0x86, 0x94, 0x9d, 0x32,
  5082. 0x56, 0xf0, 0x79, 0x71, 0x68, 0x6f, 0xa6, 0x6b, 0xc6, 0x59, 0x49, 0xf6,
  5083. 0x10, 0x34, 0x03, 0x03, 0x16, 0x53, 0x9a, 0x98, 0x2a, 0x46, 0xde, 0x17,
  5084. 0x06, 0x65, 0x70, 0xca, 0x0a, 0x1f, 0xab, 0x80, 0x26, 0x96, 0x3f, 0x3e,
  5085. 0x7a, 0x3c, 0xa8, 0x87, 0xbb, 0x65, 0xdd, 0x5e, 0x07, 0x7b, 0x34, 0xe0,
  5086. 0x56, 0xda, 0x32, 0x13, 0x30, 0xc9, 0x0c, 0xd7, 0xba, 0xe4, 0x1f, 0xa6,
  5087. 0x91, 0x4f, 0x72, 0x9f, 0xd9, 0x5c, 0x62, 0x7d, 0xa6, 0xc2, 0xbc, 0x87,
  5088. 0xae, 0x64, 0x11, 0x94, 0x3b, 0xbc, 0x6c, 0x23, 0xbd, 0x7d, 0x00, 0xb4,
  5089. 0x99, 0xf2, 0x68, 0xb5, 0x59, 0x70, 0x93, 0xad, 0x69, 0xd0, 0xb1, 0x28,
  5090. 0x70, 0x92, 0xeb, 0xec, 0x39, 0x80, 0x82, 0xde, 0x44, 0xe2, 0x8a, 0x26,
  5091. 0xb3, 0xe9, 0x45, 0xcf, 0x83, 0x76, 0x9f, 0x6a, 0xa0, 0x46, 0x4a, 0x3d,
  5092. 0x26, 0x56, 0xaf, 0x49, 0x41, 0x26, 0x1b, 0x6a, 0x41, 0x37, 0x65, 0x91,
  5093. 0x72, 0xc4, 0xe7, 0x3c, 0x17, 0x31, 0xae, 0x2e, 0x2b, 0x31, 0x45, 0xe4,
  5094. 0x93, 0xd3, 0x10, 0xaa, 0xc5, 0x62, 0xd5, 0x11, 0x4b, 0x57, 0x1d, 0xad,
  5095. 0x48, 0x06, 0xd0, 0x0d, 0x98, 0xa5, 0xc6, 0x5b, 0xd0, 0x9e, 0x22, 0xc0,
  5096. 0x00, 0x32, 0x5a, 0xf5, 0x1c, 0x89, 0x6d, 0x54, 0x97, 0x55, 0x6b, 0x46,
  5097. 0xc5, 0xc7, 0xc4, 0x48, 0x9c, 0xbf, 0x47, 0xdc, 0x03, 0xc4, 0x1b, 0xcb,
  5098. 0x65, 0xa6, 0x91, 0x9d, 0x6d, 0xf1, 0xb0, 0x7a, 0x4d, 0x3b, 0x03, 0x95,
  5099. 0xf4, 0x8b, 0x0b, 0xae, 0x39, 0xff, 0x3f, 0xf6, 0xc0, 0x14, 0x18, 0x8a,
  5100. 0xe5, 0x19, 0xbd, 0xc1, 0xb4, 0x05, 0x4e, 0x29, 0x2f, 0x0b, 0x33, 0x76,
  5101. 0x28, 0x16, 0xa4, 0xa6, 0x93, 0x04, 0xb5, 0x55, 0x6b, 0x89, 0x3d, 0xa5,
  5102. 0x0f, 0xd3, 0xad, 0xfa, 0xd9, 0xfd, 0x05, 0x5d, 0x48, 0x94, 0x25, 0x5a,
  5103. 0x2c, 0x9a, 0x94, 0x80, 0xb0, 0xe7, 0xcb, 0x4d, 0x77, 0xbf, 0xca, 0xd8,
  5104. 0x55, 0x48, 0xbd, 0x66, 0xb1, 0x85, 0x81, 0xb1, 0x37, 0x79, 0xab, 0x52,
  5105. 0x08, 0x14, 0x12, 0xac, 0xcd, 0x45, 0x4d, 0x53, 0x6b, 0xca, 0x96, 0xc7,
  5106. 0x3b, 0x2f, 0x73, 0xb1, 0x5a, 0x23, 0xbd, 0x65, 0xd5, 0xea, 0x17, 0xb3,
  5107. 0xdc, 0xa1, 0x17, 0x1b, 0x2d, 0xb3, 0x9c, 0xd0, 0xdb, 0x41, 0x77, 0xef,
  5108. 0x93, 0x20, 0x52, 0x3e, 0x9d, 0xf5, 0xbf, 0x33, 0xf7, 0x52, 0xc1, 0x90,
  5109. 0xa0, 0x15, 0x17, 0xce, 0xf7, 0xf7, 0xd0, 0x3a, 0x3b, 0xd1, 0x72, 0x56,
  5110. 0x31, 0x81, 0xae, 0x60, 0xab, 0x40, 0xc1, 0xd1, 0x28, 0x77, 0x53, 0xac,
  5111. 0x9f, 0x11, 0x0a, 0x88, 0x36, 0x4b, 0xda, 0x57, 0xa7, 0x28, 0x5c, 0x85,
  5112. 0xd3, 0x85, 0x9b, 0x79, 0xad, 0x05, 0x1c, 0x37, 0x14, 0x5e, 0x0d, 0xd0,
  5113. 0x23, 0x03, 0x42, 0x1d, 0x48, 0x5d, 0xc5, 0x3c, 0x5a, 0x08, 0xa9, 0x0d,
  5114. 0x6e, 0x82, 0x7c, 0x2e, 0x3c, 0x41, 0xcc, 0x96, 0x8e, 0xad, 0xee, 0x2a,
  5115. 0x61, 0x0b, 0x16, 0x0f, 0xa9, 0x24, 0x40, 0x85, 0xbc, 0x9f, 0x28, 0x8d,
  5116. 0xe6, 0x68, 0x4d, 0x8f, 0x30, 0x48, 0xd9, 0x73, 0x73, 0x6c, 0x9a, 0x7f,
  5117. 0x67, 0xf7, 0xde, 0x4c, 0x0a, 0x8b, 0xe4, 0xb3, 0x08, 0x2a, 0x52, 0xda,
  5118. 0x54, 0xee, 0xcd, 0xb5, 0x62, 0x4a, 0x26, 0x20, 0xfb, 0x40, 0xbb, 0x39,
  5119. 0x3a, 0x0f, 0x09, 0xe8, 0x00, 0xd1, 0x24, 0x97, 0x60, 0xe9, 0x83, 0x83,
  5120. 0xfe, 0x9f, 0x9c, 0x15, 0xcf, 0x69, 0x03, 0x9f, 0x03, 0xe1, 0xe8, 0x6e,
  5121. 0xbd, 0x87, 0x58, 0x68, 0xee, 0xec, 0xd8, 0x29, 0x46, 0x23, 0x49, 0x92,
  5122. 0x72, 0x95, 0x5b, 0x49, 0xca, 0xe0, 0x45, 0x59, 0xb2, 0xca, 0xf4, 0xfc,
  5123. 0xb7, 0x59, 0x37, 0x49, 0x28, 0xbc, 0xf3, 0xd7, 0x61, 0xbc, 0x4b, 0xf3,
  5124. 0xa9, 0x4b, 0x2f, 0x05, 0xa8, 0x01, 0xa5, 0xdc, 0x00, 0x6e, 0x01, 0xb6,
  5125. 0x45, 0x3c, 0xd5, 0x49, 0x7d, 0x5c, 0x25, 0xe8, 0x31, 0x87, 0xb2, 0xb9,
  5126. 0xbf, 0xb3, 0x01, 0x62, 0x0c, 0xd0, 0x48, 0x77, 0xa2, 0x34, 0x0f, 0x16,
  5127. 0x22, 0x28, 0xee, 0x54, 0x08, 0x93, 0x3b, 0xe4, 0xde, 0x7e, 0x63, 0xf7,
  5128. 0x97, 0x16, 0x5d, 0x71, 0x58, 0xc2, 0x2e, 0xf2, 0x36, 0xa6, 0x12, 0x65,
  5129. 0x94, 0x17, 0xac, 0x66, 0x23, 0x7e, 0xc6, 0x72, 0x79, 0x24, 0xce, 0x8f,
  5130. 0x55, 0x19, 0x97, 0x44, 0xfc, 0x55, 0xec, 0x85, 0x26, 0x27, 0xdb, 0x38,
  5131. 0xb1, 0x42, 0x0a, 0xdd, 0x05, 0x99, 0x28, 0xeb, 0x03, 0x6c, 0x9a, 0xe9,
  5132. 0x17, 0xf6, 0x2c, 0xb0, 0xfe, 0xe7, 0xa4, 0xa7, 0x31, 0xda, 0x4d, 0xb0,
  5133. 0x29, 0xdb, 0xdd, 0x8d, 0x12, 0x13, 0x9c, 0xb4, 0xcc, 0x83, 0x97, 0xfb,
  5134. 0x1a, 0xdc, 0x08, 0xd6, 0x30, 0x62, 0xe8, 0xeb, 0x8b, 0x61, 0xcb, 0x1d,
  5135. 0x06, 0xe3, 0xa5, 0x4d, 0x35, 0xdb, 0x59, 0xa8, 0x2d, 0x87, 0x27, 0x44,
  5136. 0x6f, 0xc0, 0x38, 0x97, 0xe4, 0x85, 0x00, 0x02, 0x09, 0xf6, 0x69, 0x3a,
  5137. 0xcf, 0x08, 0x1b, 0x21, 0xbb, 0x79, 0xb1, 0xa1, 0x34, 0x09, 0xe0, 0x80,
  5138. 0xca, 0xb0, 0x78, 0x8a, 0x11, 0x97, 0xd4, 0x07, 0xbe, 0x1b, 0x6a, 0x5d,
  5139. 0xdb, 0xd6, 0x1f, 0x76, 0x6b, 0x16, 0xf0, 0x58, 0x84, 0x5f, 0x59, 0xce,
  5140. 0x62, 0x34, 0xc3, 0xdf, 0x94, 0xb8, 0x2f, 0x84, 0x68, 0xf0, 0xb8, 0x51,
  5141. 0xd9, 0x6d, 0x8e, 0x4a, 0x1d, 0xe6, 0x5c, 0xd8, 0x86, 0x25, 0xe3, 0x24,
  5142. 0xfd, 0x21, 0x61, 0x13, 0x48, 0x3e, 0xf6, 0x7d, 0xa6, 0x71, 0x9b, 0xd2,
  5143. 0x6e, 0xe6, 0xd2, 0x08, 0x94, 0x62, 0x6c, 0x98, 0xfe, 0x2f, 0x9c, 0x88,
  5144. 0x7e, 0x78, 0x15, 0x02, 0x00, 0xf0, 0xba, 0x24, 0x91, 0xf2, 0xdc, 0x47,
  5145. 0x51, 0x4d, 0x15, 0x5e, 0x91, 0x5f, 0x57, 0x5b, 0x1d, 0x35, 0x24, 0x45,
  5146. 0x75, 0x9b, 0x88, 0x75, 0xf1, 0x2f, 0x85, 0xe7, 0x89, 0xd1, 0x01, 0xb4,
  5147. 0xc8, 0x18, 0xb7, 0x97, 0xef, 0x4b, 0x90, 0xf4, 0xbf, 0x10, 0x27, 0x3c,
  5148. 0x60, 0xff, 0xc4, 0x94, 0x20, 0x2f, 0x93, 0x4b, 0x4d, 0xe3, 0x80, 0xf7,
  5149. 0x2c, 0x71, 0xd9, 0xe3, 0x68, 0xb4, 0x77, 0x2b, 0xc7, 0x0d, 0x39, 0x92,
  5150. 0xef, 0x91, 0x0d, 0xb2, 0x11, 0x50, 0x0e, 0xe8, 0xad, 0x3b, 0xf6, 0xb5,
  5151. 0xc6, 0x14, 0x4d, 0x33, 0x53, 0xa7, 0x60, 0x15, 0xc7, 0x27, 0x51, 0xdc,
  5152. 0x54, 0x29, 0xa7, 0x0d, 0x6a, 0x7b, 0x72, 0x13, 0xad, 0x7d, 0x41, 0x19,
  5153. 0x4e, 0x42, 0x49, 0xcc, 0x42, 0xe4, 0xbd, 0x99, 0x13, 0xd9, 0x7f, 0xf3,
  5154. 0x38, 0xa4, 0xb6, 0x33, 0xed, 0x07, 0x48, 0x7e, 0x8e, 0x82, 0xfe, 0x3a,
  5155. 0x9d, 0x75, 0x93, 0xba, 0x25, 0x4e, 0x37, 0x3c, 0x0c, 0xd5, 0x69, 0xa9,
  5156. 0x2d, 0x9e, 0xfd, 0xe8, 0xbb, 0xf5, 0x0c, 0xe2, 0x86, 0xb9, 0x5e, 0x6f,
  5157. 0x28, 0xe4, 0x19, 0xb3, 0x0b, 0xa4, 0x86, 0xd7, 0x24, 0xd0, 0xb8, 0x89,
  5158. 0x7b, 0x76, 0xec, 0x05, 0x10, 0x5b, 0x68, 0xe9, 0x58, 0x66, 0xa3, 0xc5,
  5159. 0xb6, 0x63, 0x20, 0x0e, 0x0e, 0xea, 0x3d, 0x61, 0x5e, 0xda, 0x3d, 0x3c,
  5160. 0xf9, 0xfd, 0xed, 0xa9, 0xdb, 0x52, 0x94, 0x8a, 0x00, 0xca, 0x3c, 0x8d,
  5161. 0x66, 0x8f, 0xb0, 0xf0, 0x5a, 0xca, 0x3f, 0x63, 0x71, 0xbf, 0xca, 0x99,
  5162. 0x37, 0x9b, 0x75, 0x97, 0x89, 0x10, 0x6e, 0xcf, 0xf2, 0xf5, 0xe3, 0xd5,
  5163. 0x45, 0x9b, 0xad, 0x10, 0x71, 0x6c, 0x5f, 0x6f, 0x7f, 0x22, 0x77, 0x18,
  5164. 0x2f, 0xf9, 0x99, 0xc5, 0x69, 0x58, 0x03, 0x12, 0x86, 0x82, 0x3e, 0xbf,
  5165. 0xc2, 0x12, 0x35, 0x43, 0xa3, 0xd9, 0x18, 0x4f, 0x41, 0x11, 0x6b, 0xf3,
  5166. 0x67, 0xaf, 0x3d, 0x78, 0xe4, 0x22, 0x2d, 0xb3, 0x48, 0x43, 0x31, 0x1d,
  5167. 0xef, 0xa8, 0xba, 0x49, 0x8e, 0xa9, 0xa7, 0xb6, 0x18, 0x77, 0x84, 0xca,
  5168. 0xbd, 0xa2, 0x02, 0x1b, 0x6a, 0xf8, 0x5f, 0xda, 0xff, 0xcf, 0x01, 0x6a,
  5169. 0x86, 0x69, 0xa9, 0xe9, 0xcb, 0x60, 0x1e, 0x15, 0xdc, 0x8f, 0x5d, 0x39,
  5170. 0xb5, 0xce, 0x55, 0x5f, 0x47, 0x97, 0xb1, 0x19, 0x6e, 0x21, 0xd6, 0x13,
  5171. 0x39, 0xb2, 0x24, 0xe0, 0x62, 0x82, 0x9f, 0xed, 0x12, 0x81, 0xed, 0xee,
  5172. 0xab, 0xd0, 0x2f, 0x19, 0x89, 0x3f, 0x57, 0x2e, 0xc2, 0xe2, 0x67, 0xe8,
  5173. 0xae, 0x03, 0x56, 0xba, 0xd4, 0xd0, 0xa4, 0x89, 0x03, 0x06, 0x5b, 0xcc,
  5174. 0xf2, 0x22, 0xb8, 0x0e, 0x76, 0x79, 0x4a, 0x42, 0x1d, 0x37, 0x51, 0x5a,
  5175. 0xaa, 0x46, 0x6c, 0x2a, 0xdd, 0x66, 0xfe, 0xc6, 0x68, 0xc3, 0x38, 0xa2,
  5176. 0xae, 0x5b, 0x98, 0x24, 0x5d, 0x43, 0x05, 0x82, 0x38, 0x12, 0xd3, 0xd1,
  5177. 0x75, 0x2d, 0x4f, 0x61, 0xbd, 0xb9, 0x10, 0x87, 0x44, 0x2a, 0x78, 0x07,
  5178. 0xff, 0xf4, 0x0f, 0xa1, 0xf3, 0x68, 0x9f, 0xbe, 0xae, 0xa2, 0x91, 0xf0,
  5179. 0xc7, 0x55, 0x7a, 0x52, 0xd5, 0xa3, 0x8d, 0x6f, 0xe4, 0x90, 0x5c, 0xf3,
  5180. 0x5f, 0xce, 0x3d, 0x23, 0xf9, 0x8e, 0xae, 0x14, 0xfb, 0x82, 0x9a, 0xa3,
  5181. 0x04, 0x5f, 0xbf, 0xad, 0x3e, 0xf2, 0x97, 0x0a, 0x60, 0x40, 0x70, 0x19,
  5182. 0x72, 0xad, 0x66, 0xfb, 0x78, 0x1b, 0x84, 0x6c, 0x98, 0xbc, 0x8c, 0xf8,
  5183. 0x4f, 0xcb, 0xb5, 0xf6, 0xaf, 0x7a, 0xb7, 0x93, 0xef, 0x67, 0x48, 0x02,
  5184. 0x2c, 0xcb, 0xe6, 0x77, 0x0f, 0x7b, 0xc1, 0xee, 0xc5, 0xb6, 0x2d, 0x7e,
  5185. 0x62, 0xa0, 0xc0, 0xa7, 0xa5, 0x80, 0x31, 0x92, 0x50, 0xa1, 0x28, 0x22,
  5186. 0x95, 0x03, 0x17, 0xd1, 0x0f, 0xf6, 0x08, 0xe5, 0xec
  5187. };
  5188. #define CHACHA_BIG_TEST_SIZE 1305
  5189. #if !defined(WOLFSSL_SMALL_STACK) || defined(WOLFSSL_NO_MALLOC)
  5190. byte cipher_big[CHACHA_BIG_TEST_SIZE] = {0};
  5191. byte plain_big[CHACHA_BIG_TEST_SIZE] = {0};
  5192. byte input_big[CHACHA_BIG_TEST_SIZE] = {0};
  5193. #else
  5194. byte* cipher_big;
  5195. byte* plain_big;
  5196. byte* input_big;
  5197. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  5198. int block_size;
  5199. #endif /* BENCH_EMBEDDED */
  5200. byte a[] = {0x76,0xb8,0xe0,0xad,0xa0,0xf1,0x3d,0x90};
  5201. byte b[] = {0x45,0x40,0xf0,0x5a,0x9f,0x1f,0xb2,0x96};
  5202. byte c[] = {0xde,0x9c,0xba,0x7b,0xf3,0xd6,0x9e,0xf5};
  5203. byte d[] = {0x89,0x67,0x09,0x52,0x60,0x83,0x64,0xfd};
  5204. byte* test_chacha[4];
  5205. test_chacha[0] = a;
  5206. test_chacha[1] = b;
  5207. test_chacha[2] = c;
  5208. test_chacha[3] = d;
  5209. #ifndef BENCH_EMBEDDED
  5210. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  5211. cipher_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  5212. DYNAMIC_TYPE_TMP_BUFFER);
  5213. if (cipher_big == NULL) {
  5214. return MEMORY_E;
  5215. }
  5216. plain_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  5217. DYNAMIC_TYPE_TMP_BUFFER);
  5218. if (plain_big == NULL) {
  5219. return MEMORY_E;
  5220. }
  5221. input_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  5222. DYNAMIC_TYPE_TMP_BUFFER);
  5223. if (input_big == NULL) {
  5224. return MEMORY_E;
  5225. }
  5226. XMEMSET(cipher_big, 0, CHACHA_BIG_TEST_SIZE);
  5227. XMEMSET(plain_big, 0, CHACHA_BIG_TEST_SIZE);
  5228. XMEMSET(input_big, 0, CHACHA_BIG_TEST_SIZE);
  5229. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  5230. #endif /* BENCH_EMBEDDED */
  5231. for (i = 0; i < times; ++i) {
  5232. if (i < 3) {
  5233. keySz = 32;
  5234. }
  5235. else {
  5236. keySz = 16;
  5237. }
  5238. XMEMCPY(plain, keys[i], keySz);
  5239. XMEMSET(cipher, 0, 32);
  5240. XMEMCPY(cipher + 4, ivs[i], 8);
  5241. ret |= wc_Chacha_SetKey(&enc, keys[i], keySz);
  5242. ret |= wc_Chacha_SetKey(&dec, keys[i], keySz);
  5243. if (ret != 0)
  5244. return ret;
  5245. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  5246. ret |= wc_Chacha_SetIV(&dec, cipher, 0);
  5247. if (ret != 0)
  5248. return ret;
  5249. XMEMCPY(plain, input, 8);
  5250. ret |= wc_Chacha_Process(&enc, cipher, plain, (word32)8);
  5251. ret |= wc_Chacha_Process(&dec, plain, cipher, (word32)8);
  5252. if (ret != 0)
  5253. return ret;
  5254. if (XMEMCMP(test_chacha[i], cipher, 8))
  5255. return -4700 - i;
  5256. if (XMEMCMP(plain, input, 8))
  5257. return -4710 - i;
  5258. }
  5259. /* test of starting at a different counter
  5260. encrypts all of the information and decrypts starting at 2nd chunk */
  5261. XMEMSET(plain, 0, sizeof(plain));
  5262. XMEMSET(sliver, 1, sizeof(sliver)); /* set as 1's to not match plain */
  5263. XMEMSET(cipher, 0, sizeof(cipher));
  5264. XMEMCPY(cipher + 4, ivs[0], 8);
  5265. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  5266. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  5267. if (ret != 0)
  5268. return ret;
  5269. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  5270. ret |= wc_Chacha_SetIV(&dec, cipher, 1);
  5271. if (ret != 0)
  5272. return ret;
  5273. ret |= wc_Chacha_Process(&enc, cipher, plain, sizeof(plain));
  5274. ret |= wc_Chacha_Process(&dec, sliver, cipher + 64, sizeof(sliver));
  5275. if (ret != 0)
  5276. return ret;
  5277. if (XMEMCMP(plain + 64, sliver, 64))
  5278. return -4720;
  5279. #ifndef BENCH_EMBEDDED
  5280. /* test of encrypting more data */
  5281. keySz = 32;
  5282. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  5283. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  5284. if (ret != 0)
  5285. return ret;
  5286. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  5287. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  5288. if (ret != 0)
  5289. return ret;
  5290. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big, CHACHA_BIG_TEST_SIZE);
  5291. ret |= wc_Chacha_Process(&dec, plain_big, cipher_big,
  5292. CHACHA_BIG_TEST_SIZE);
  5293. if (ret != 0)
  5294. return ret;
  5295. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  5296. return -4721;
  5297. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  5298. return -4722;
  5299. for (i = 0; i < 18; ++i) {
  5300. /* this will test all paths
  5301. * block sizes: 1 3 7 15 31 63 127 255 511 (i = 0- 8)
  5302. * 2 4 8 16 32 64 128 256 512 (i = 9-17)
  5303. */
  5304. block_size = (2 << (i%9)) - (i<9?1:0);
  5305. keySz = 32;
  5306. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  5307. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  5308. if (ret != 0)
  5309. return ret;
  5310. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  5311. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  5312. if (ret != 0)
  5313. return ret;
  5314. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big , block_size);
  5315. ret |= wc_Chacha_Process(&dec, plain_big , cipher_big, block_size);
  5316. if (ret != 0)
  5317. return ret;
  5318. if (XMEMCMP(plain_big, input_big, block_size))
  5319. return -4740-i*2;
  5320. if (XMEMCMP(cipher_big, cipher_big_result, block_size))
  5321. return -4741-i*2;
  5322. }
  5323. /* Streaming test */
  5324. for (i = 1; i <= (int)CHACHA_CHUNK_BYTES + 1; i++) {
  5325. int j, rem;
  5326. ret = wc_Chacha_SetKey(&enc, keys[0], keySz);
  5327. if (ret != 0)
  5328. return -4725;
  5329. ret = wc_Chacha_SetKey(&dec, keys[0], keySz);
  5330. if (ret != 0)
  5331. return -4726;
  5332. ret = wc_Chacha_SetIV(&enc, ivs[2], 0);
  5333. if (ret != 0)
  5334. return -4727;
  5335. ret = wc_Chacha_SetIV(&dec, ivs[2], 0);
  5336. if (ret != 0)
  5337. return -4728;
  5338. for (j = 0; j < CHACHA_BIG_TEST_SIZE - i; j+= i) {
  5339. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, i);
  5340. if (ret != 0)
  5341. return -4729;
  5342. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, i);
  5343. if (ret != 0)
  5344. return -4730;
  5345. }
  5346. rem = CHACHA_BIG_TEST_SIZE - j;
  5347. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, rem);
  5348. if (ret != 0)
  5349. return -4731;
  5350. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, rem);
  5351. if (ret != 0)
  5352. return -4732;
  5353. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  5354. return -4733;
  5355. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  5356. return -4734;
  5357. }
  5358. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  5359. XFREE(cipher_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5360. XFREE(plain_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5361. XFREE(input_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5362. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  5363. #endif /* BENCH_EMBEDDED */
  5364. return 0;
  5365. }
  5366. #endif /* HAVE_CHACHA */
  5367. #ifdef HAVE_POLY1305
  5368. WOLFSSL_TEST_SUBROUTINE int poly1305_test(void)
  5369. {
  5370. int ret = 0;
  5371. int i;
  5372. byte tag[16];
  5373. Poly1305 enc;
  5374. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  5375. {
  5376. 0x43,0x72,0x79,0x70,0x74,0x6f,0x67,0x72,
  5377. 0x61,0x70,0x68,0x69,0x63,0x20,0x46,0x6f,
  5378. 0x72,0x75,0x6d,0x20,0x52,0x65,0x73,0x65,
  5379. 0x61,0x72,0x63,0x68,0x20,0x47,0x72,0x6f,
  5380. 0x75,0x70
  5381. };
  5382. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  5383. {
  5384. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x77,0x6f,0x72,
  5385. 0x6c,0x64,0x21
  5386. };
  5387. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  5388. {
  5389. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5390. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5391. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5392. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5393. };
  5394. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] =
  5395. {
  5396. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  5397. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  5398. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  5399. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  5400. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  5401. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  5402. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  5403. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  5404. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  5405. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  5406. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  5407. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  5408. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  5409. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  5410. 0x61,0x16
  5411. };
  5412. WOLFSSL_SMALL_STACK_STATIC const byte msg5[] =
  5413. {
  5414. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  5415. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  5416. };
  5417. WOLFSSL_SMALL_STACK_STATIC const byte msg6[] =
  5418. {
  5419. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  5420. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  5421. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  5422. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  5423. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  5424. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  5425. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  5426. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  5427. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  5428. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  5429. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  5430. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  5431. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  5432. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  5433. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  5434. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  5435. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  5436. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  5437. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  5438. 0x61,0x16
  5439. };
  5440. byte additional[] =
  5441. {
  5442. 0x50,0x51,0x52,0x53,0xc0,0xc1,0xc2,0xc3,
  5443. 0xc4,0xc5,0xc6,0xc7
  5444. };
  5445. WOLFSSL_SMALL_STACK_STATIC const byte correct0[] =
  5446. {
  5447. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  5448. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  5449. };
  5450. WOLFSSL_SMALL_STACK_STATIC const byte correct1[] =
  5451. {
  5452. 0xa8,0x06,0x1d,0xc1,0x30,0x51,0x36,0xc6,
  5453. 0xc2,0x2b,0x8b,0xaf,0x0c,0x01,0x27,0xa9
  5454. };
  5455. WOLFSSL_SMALL_STACK_STATIC const byte correct2[] =
  5456. {
  5457. 0xa6,0xf7,0x45,0x00,0x8f,0x81,0xc9,0x16,
  5458. 0xa2,0x0d,0xcc,0x74,0xee,0xf2,0xb2,0xf0
  5459. };
  5460. WOLFSSL_SMALL_STACK_STATIC const byte correct3[] =
  5461. {
  5462. 0x49,0xec,0x78,0x09,0x0e,0x48,0x1e,0xc6,
  5463. 0xc2,0x6b,0x33,0xb9,0x1c,0xcc,0x03,0x07
  5464. };
  5465. WOLFSSL_SMALL_STACK_STATIC const byte correct4[] =
  5466. {
  5467. 0x1a,0xe1,0x0b,0x59,0x4f,0x09,0xe2,0x6a,
  5468. 0x7e,0x90,0x2e,0xcb,0xd0,0x60,0x06,0x91
  5469. };
  5470. WOLFSSL_SMALL_STACK_STATIC const byte correct5[] =
  5471. {
  5472. 0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5473. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5474. };
  5475. WOLFSSL_SMALL_STACK_STATIC const byte correct6[] =
  5476. {
  5477. 0xea,0x11,0x5c,0x4f,0xd0,0xc0,0x10,0xae,
  5478. 0xf7,0xdf,0xda,0x77,0xa2,0xe9,0xaf,0xca
  5479. };
  5480. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  5481. 0x85,0xd6,0xbe,0x78,0x57,0x55,0x6d,0x33,
  5482. 0x7f,0x44,0x52,0xfe,0x42,0xd5,0x06,0xa8,
  5483. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  5484. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  5485. };
  5486. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  5487. 0x74,0x68,0x69,0x73,0x20,0x69,0x73,0x20,
  5488. 0x33,0x32,0x2d,0x62,0x79,0x74,0x65,0x20,
  5489. 0x6b,0x65,0x79,0x20,0x66,0x6f,0x72,0x20,
  5490. 0x50,0x6f,0x6c,0x79,0x31,0x33,0x30,0x35
  5491. };
  5492. WOLFSSL_SMALL_STACK_STATIC const byte key4[] = {
  5493. 0x7b,0xac,0x2b,0x25,0x2d,0xb4,0x47,0xaf,
  5494. 0x09,0xb6,0x7a,0x55,0xa4,0xe9,0x55,0x84,
  5495. 0x0a,0xe1,0xd6,0x73,0x10,0x75,0xd9,0xeb,
  5496. 0x2a,0x93,0x75,0x78,0x3e,0xd5,0x53,0xff
  5497. };
  5498. WOLFSSL_SMALL_STACK_STATIC const byte key5[] = {
  5499. 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5500. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5501. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5502. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5503. };
  5504. const byte* msgs[] = {NULL, msg1, msg2, msg3, msg5, msg6};
  5505. word32 szm[] = {0, sizeof(msg1), sizeof(msg2),
  5506. sizeof(msg3), sizeof(msg5), sizeof(msg6)};
  5507. const byte* keys[] = {key, key, key2, key2, key5, key};
  5508. const byte* tests[] = {correct0, correct1, correct2, correct3, correct5,
  5509. correct6};
  5510. for (i = 0; i < 6; i++) {
  5511. ret = wc_Poly1305SetKey(&enc, keys[i], 32);
  5512. if (ret != 0)
  5513. return -4800 - i;
  5514. ret = wc_Poly1305Update(&enc, msgs[i], szm[i]);
  5515. if (ret != 0)
  5516. return -4810 - i;
  5517. ret = wc_Poly1305Final(&enc, tag);
  5518. if (ret != 0)
  5519. return -4820 - i;
  5520. if (XMEMCMP(tag, tests[i], sizeof(tag)))
  5521. return -4830 - i;
  5522. }
  5523. /* Check TLS MAC function from 2.8.2 https://tools.ietf.org/html/rfc7539 */
  5524. XMEMSET(tag, 0, sizeof(tag));
  5525. ret = wc_Poly1305SetKey(&enc, key4, sizeof(key4));
  5526. if (ret != 0)
  5527. return -4840;
  5528. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  5529. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  5530. if (ret != 0)
  5531. return -4841;
  5532. if (XMEMCMP(tag, correct4, sizeof(tag)))
  5533. return -4842;
  5534. /* Check fail of TLS MAC function if altering additional data */
  5535. XMEMSET(tag, 0, sizeof(tag));
  5536. additional[0]++;
  5537. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  5538. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  5539. if (ret != 0)
  5540. return -4843;
  5541. if (XMEMCMP(tag, correct4, sizeof(tag)) == 0)
  5542. return -4844;
  5543. return 0;
  5544. }
  5545. #endif /* HAVE_POLY1305 */
  5546. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  5547. WOLFSSL_TEST_SUBROUTINE int chacha20_poly1305_aead_test(void)
  5548. {
  5549. /* Test #1 from Section 2.8.2 of draft-irtf-cfrg-chacha20-poly1305-10 */
  5550. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  5551. WOLFSSL_SMALL_STACK_STATIC const byte key1[] = {
  5552. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  5553. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  5554. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  5555. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  5556. };
  5557. WOLFSSL_SMALL_STACK_STATIC const byte plaintext1[] = {
  5558. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  5559. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  5560. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  5561. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  5562. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  5563. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  5564. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  5565. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  5566. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  5567. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  5568. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  5569. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  5570. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  5571. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  5572. 0x74, 0x2e
  5573. };
  5574. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] = {
  5575. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  5576. 0x44, 0x45, 0x46, 0x47
  5577. };
  5578. WOLFSSL_SMALL_STACK_STATIC const byte aad1[] = { /* additional data */
  5579. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  5580. 0xc4, 0xc5, 0xc6, 0xc7
  5581. };
  5582. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] = { /* expected output from operation */
  5583. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  5584. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  5585. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  5586. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  5587. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  5588. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  5589. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  5590. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  5591. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  5592. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  5593. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  5594. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  5595. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  5596. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  5597. 0x61, 0x16
  5598. };
  5599. WOLFSSL_SMALL_STACK_STATIC const byte authTag1[] = { /* expected output from operation */
  5600. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  5601. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  5602. };
  5603. /* Test #2 from Appendix A.2 in draft-irtf-cfrg-chacha20-poly1305-10 */
  5604. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  5605. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  5606. 0x1c, 0x92, 0x40, 0xa5, 0xeb, 0x55, 0xd3, 0x8a,
  5607. 0xf3, 0x33, 0x88, 0x86, 0x04, 0xf6, 0xb5, 0xf0,
  5608. 0x47, 0x39, 0x17, 0xc1, 0x40, 0x2b, 0x80, 0x09,
  5609. 0x9d, 0xca, 0x5c, 0xbc, 0x20, 0x70, 0x75, 0xc0
  5610. };
  5611. WOLFSSL_SMALL_STACK_STATIC const byte plaintext2[] = {
  5612. 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74,
  5613. 0x2d, 0x44, 0x72, 0x61, 0x66, 0x74, 0x73, 0x20,
  5614. 0x61, 0x72, 0x65, 0x20, 0x64, 0x72, 0x61, 0x66,
  5615. 0x74, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  5616. 0x6e, 0x74, 0x73, 0x20, 0x76, 0x61, 0x6c, 0x69,
  5617. 0x64, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x61, 0x20,
  5618. 0x6d, 0x61, 0x78, 0x69, 0x6d, 0x75, 0x6d, 0x20,
  5619. 0x6f, 0x66, 0x20, 0x73, 0x69, 0x78, 0x20, 0x6d,
  5620. 0x6f, 0x6e, 0x74, 0x68, 0x73, 0x20, 0x61, 0x6e,
  5621. 0x64, 0x20, 0x6d, 0x61, 0x79, 0x20, 0x62, 0x65,
  5622. 0x20, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64,
  5623. 0x2c, 0x20, 0x72, 0x65, 0x70, 0x6c, 0x61, 0x63,
  5624. 0x65, 0x64, 0x2c, 0x20, 0x6f, 0x72, 0x20, 0x6f,
  5625. 0x62, 0x73, 0x6f, 0x6c, 0x65, 0x74, 0x65, 0x64,
  5626. 0x20, 0x62, 0x79, 0x20, 0x6f, 0x74, 0x68, 0x65,
  5627. 0x72, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  5628. 0x6e, 0x74, 0x73, 0x20, 0x61, 0x74, 0x20, 0x61,
  5629. 0x6e, 0x79, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x2e,
  5630. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x69,
  5631. 0x6e, 0x61, 0x70, 0x70, 0x72, 0x6f, 0x70, 0x72,
  5632. 0x69, 0x61, 0x74, 0x65, 0x20, 0x74, 0x6f, 0x20,
  5633. 0x75, 0x73, 0x65, 0x20, 0x49, 0x6e, 0x74, 0x65,
  5634. 0x72, 0x6e, 0x65, 0x74, 0x2d, 0x44, 0x72, 0x61,
  5635. 0x66, 0x74, 0x73, 0x20, 0x61, 0x73, 0x20, 0x72,
  5636. 0x65, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x65,
  5637. 0x20, 0x6d, 0x61, 0x74, 0x65, 0x72, 0x69, 0x61,
  5638. 0x6c, 0x20, 0x6f, 0x72, 0x20, 0x74, 0x6f, 0x20,
  5639. 0x63, 0x69, 0x74, 0x65, 0x20, 0x74, 0x68, 0x65,
  5640. 0x6d, 0x20, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20,
  5641. 0x74, 0x68, 0x61, 0x6e, 0x20, 0x61, 0x73, 0x20,
  5642. 0x2f, 0xe2, 0x80, 0x9c, 0x77, 0x6f, 0x72, 0x6b,
  5643. 0x20, 0x69, 0x6e, 0x20, 0x70, 0x72, 0x6f, 0x67,
  5644. 0x72, 0x65, 0x73, 0x73, 0x2e, 0x2f, 0xe2, 0x80,
  5645. 0x9d
  5646. };
  5647. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  5648. 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
  5649. 0x05, 0x06, 0x07, 0x08
  5650. };
  5651. WOLFSSL_SMALL_STACK_STATIC const byte aad2[] = { /* additional data */
  5652. 0xf3, 0x33, 0x88, 0x86, 0x00, 0x00, 0x00, 0x00,
  5653. 0x00, 0x00, 0x4e, 0x91
  5654. };
  5655. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] = { /* expected output from operation */
  5656. 0x64, 0xa0, 0x86, 0x15, 0x75, 0x86, 0x1a, 0xf4,
  5657. 0x60, 0xf0, 0x62, 0xc7, 0x9b, 0xe6, 0x43, 0xbd,
  5658. 0x5e, 0x80, 0x5c, 0xfd, 0x34, 0x5c, 0xf3, 0x89,
  5659. 0xf1, 0x08, 0x67, 0x0a, 0xc7, 0x6c, 0x8c, 0xb2,
  5660. 0x4c, 0x6c, 0xfc, 0x18, 0x75, 0x5d, 0x43, 0xee,
  5661. 0xa0, 0x9e, 0xe9, 0x4e, 0x38, 0x2d, 0x26, 0xb0,
  5662. 0xbd, 0xb7, 0xb7, 0x3c, 0x32, 0x1b, 0x01, 0x00,
  5663. 0xd4, 0xf0, 0x3b, 0x7f, 0x35, 0x58, 0x94, 0xcf,
  5664. 0x33, 0x2f, 0x83, 0x0e, 0x71, 0x0b, 0x97, 0xce,
  5665. 0x98, 0xc8, 0xa8, 0x4a, 0xbd, 0x0b, 0x94, 0x81,
  5666. 0x14, 0xad, 0x17, 0x6e, 0x00, 0x8d, 0x33, 0xbd,
  5667. 0x60, 0xf9, 0x82, 0xb1, 0xff, 0x37, 0xc8, 0x55,
  5668. 0x97, 0x97, 0xa0, 0x6e, 0xf4, 0xf0, 0xef, 0x61,
  5669. 0xc1, 0x86, 0x32, 0x4e, 0x2b, 0x35, 0x06, 0x38,
  5670. 0x36, 0x06, 0x90, 0x7b, 0x6a, 0x7c, 0x02, 0xb0,
  5671. 0xf9, 0xf6, 0x15, 0x7b, 0x53, 0xc8, 0x67, 0xe4,
  5672. 0xb9, 0x16, 0x6c, 0x76, 0x7b, 0x80, 0x4d, 0x46,
  5673. 0xa5, 0x9b, 0x52, 0x16, 0xcd, 0xe7, 0xa4, 0xe9,
  5674. 0x90, 0x40, 0xc5, 0xa4, 0x04, 0x33, 0x22, 0x5e,
  5675. 0xe2, 0x82, 0xa1, 0xb0, 0xa0, 0x6c, 0x52, 0x3e,
  5676. 0xaf, 0x45, 0x34, 0xd7, 0xf8, 0x3f, 0xa1, 0x15,
  5677. 0x5b, 0x00, 0x47, 0x71, 0x8c, 0xbc, 0x54, 0x6a,
  5678. 0x0d, 0x07, 0x2b, 0x04, 0xb3, 0x56, 0x4e, 0xea,
  5679. 0x1b, 0x42, 0x22, 0x73, 0xf5, 0x48, 0x27, 0x1a,
  5680. 0x0b, 0xb2, 0x31, 0x60, 0x53, 0xfa, 0x76, 0x99,
  5681. 0x19, 0x55, 0xeb, 0xd6, 0x31, 0x59, 0x43, 0x4e,
  5682. 0xce, 0xbb, 0x4e, 0x46, 0x6d, 0xae, 0x5a, 0x10,
  5683. 0x73, 0xa6, 0x72, 0x76, 0x27, 0x09, 0x7a, 0x10,
  5684. 0x49, 0xe6, 0x17, 0xd9, 0x1d, 0x36, 0x10, 0x94,
  5685. 0xfa, 0x68, 0xf0, 0xff, 0x77, 0x98, 0x71, 0x30,
  5686. 0x30, 0x5b, 0xea, 0xba, 0x2e, 0xda, 0x04, 0xdf,
  5687. 0x99, 0x7b, 0x71, 0x4d, 0x6c, 0x6f, 0x2c, 0x29,
  5688. 0xa6, 0xad, 0x5c, 0xb4, 0x02, 0x2b, 0x02, 0x70,
  5689. 0x9b
  5690. };
  5691. WOLFSSL_SMALL_STACK_STATIC const byte authTag2[] = { /* expected output from operation */
  5692. 0xee, 0xad, 0x9d, 0x67, 0x89, 0x0c, 0xbb, 0x22,
  5693. 0x39, 0x23, 0x36, 0xfe, 0xa1, 0x85, 0x1f, 0x38
  5694. };
  5695. byte generatedCiphertext[265]; /* max plaintext2/cipher2 */
  5696. byte generatedPlaintext[265]; /* max plaintext2/cipher2 */
  5697. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  5698. int err;
  5699. ChaChaPoly_Aead aead;
  5700. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  5701. #define TEST_SMALL_CHACHA_CHUNKS 32
  5702. #else
  5703. #define TEST_SMALL_CHACHA_CHUNKS 64
  5704. #endif
  5705. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5706. word32 testLen;
  5707. #endif
  5708. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5709. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5710. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5711. /* Parameter Validation testing */
  5712. /* Encrypt */
  5713. err = wc_ChaCha20Poly1305_Encrypt(NULL, iv1, aad1, sizeof(aad1), plaintext1,
  5714. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  5715. if (err != BAD_FUNC_ARG)
  5716. return -4900;
  5717. err = wc_ChaCha20Poly1305_Encrypt(key1, NULL, aad1, sizeof(aad1),
  5718. plaintext1, sizeof(plaintext1), generatedCiphertext,
  5719. generatedAuthTag);
  5720. if (err != BAD_FUNC_ARG)
  5721. return -4901;
  5722. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL,
  5723. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  5724. if (err != BAD_FUNC_ARG)
  5725. return -4902;
  5726. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  5727. sizeof(plaintext1), NULL, generatedAuthTag);
  5728. if (err != BAD_FUNC_ARG)
  5729. return -4903;
  5730. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  5731. sizeof(plaintext1), generatedCiphertext, NULL);
  5732. if (err != BAD_FUNC_ARG)
  5733. return -4904;
  5734. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL,
  5735. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  5736. if (err != BAD_FUNC_ARG)
  5737. return -4905;
  5738. /* Decrypt */
  5739. err = wc_ChaCha20Poly1305_Decrypt(NULL, iv2, aad2, sizeof(aad2), cipher2,
  5740. sizeof(cipher2), authTag2, generatedPlaintext);
  5741. if (err != BAD_FUNC_ARG)
  5742. return -4906;
  5743. err = wc_ChaCha20Poly1305_Decrypt(key2, NULL, aad2, sizeof(aad2), cipher2,
  5744. sizeof(cipher2), authTag2, generatedPlaintext);
  5745. if (err != BAD_FUNC_ARG)
  5746. return -4907;
  5747. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL,
  5748. sizeof(cipher2), authTag2, generatedPlaintext);
  5749. if (err != BAD_FUNC_ARG)
  5750. return -4908;
  5751. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  5752. sizeof(cipher2), NULL, generatedPlaintext);
  5753. if (err != BAD_FUNC_ARG)
  5754. return -4909;
  5755. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  5756. sizeof(cipher2), authTag2, NULL);
  5757. if (err != BAD_FUNC_ARG)
  5758. return -4910;
  5759. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL,
  5760. sizeof(cipher2), authTag2, generatedPlaintext);
  5761. if (err != BAD_FUNC_ARG)
  5762. return -4911;
  5763. /* Test #1 */
  5764. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1,
  5765. aad1, sizeof(aad1),
  5766. plaintext1, sizeof(plaintext1),
  5767. generatedCiphertext, generatedAuthTag);
  5768. if (err) {
  5769. return err;
  5770. }
  5771. /* -- Check the ciphertext and authtag */
  5772. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  5773. return -4912;
  5774. }
  5775. if (XMEMCMP(generatedAuthTag, authTag1, sizeof(authTag1))) {
  5776. return -4913;
  5777. }
  5778. /* -- Verify decryption works */
  5779. err = wc_ChaCha20Poly1305_Decrypt(key1, iv1,
  5780. aad1, sizeof(aad1),
  5781. cipher1, sizeof(cipher1),
  5782. authTag1, generatedPlaintext);
  5783. if (err) {
  5784. return err;
  5785. }
  5786. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  5787. return -4914;
  5788. }
  5789. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5790. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5791. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5792. /* Test #2 */
  5793. err = wc_ChaCha20Poly1305_Encrypt(key2, iv2,
  5794. aad2, sizeof(aad2),
  5795. plaintext2, sizeof(plaintext2),
  5796. generatedCiphertext, generatedAuthTag);
  5797. if (err) {
  5798. return err;
  5799. }
  5800. /* -- Check the ciphertext and authtag */
  5801. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  5802. return -4915;
  5803. }
  5804. if (XMEMCMP(generatedAuthTag, authTag2, sizeof(authTag2))) {
  5805. return -4916;
  5806. }
  5807. /* -- Verify decryption works */
  5808. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2,
  5809. aad2, sizeof(aad2),
  5810. cipher2, sizeof(cipher2),
  5811. authTag2, generatedPlaintext);
  5812. if (err) {
  5813. return err;
  5814. }
  5815. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  5816. return -4917;
  5817. }
  5818. /* AEAD init/update/final - bad argument tests */
  5819. err = wc_ChaCha20Poly1305_Init(NULL, key1, iv1,
  5820. CHACHA20_POLY1305_AEAD_DECRYPT);
  5821. if (err != BAD_FUNC_ARG)
  5822. return -4918;
  5823. err = wc_ChaCha20Poly1305_Init(&aead, NULL, iv1,
  5824. CHACHA20_POLY1305_AEAD_DECRYPT);
  5825. if (err != BAD_FUNC_ARG)
  5826. return -4919;
  5827. err = wc_ChaCha20Poly1305_Init(&aead, key1, NULL,
  5828. CHACHA20_POLY1305_AEAD_DECRYPT);
  5829. if (err != BAD_FUNC_ARG)
  5830. return -4920;
  5831. err = wc_ChaCha20Poly1305_UpdateAad(NULL, aad1, sizeof(aad1));
  5832. if (err != BAD_FUNC_ARG)
  5833. return -4921;
  5834. err = wc_ChaCha20Poly1305_UpdateAad(&aead, NULL, sizeof(aad1));
  5835. if (err != BAD_FUNC_ARG)
  5836. return -4922;
  5837. err = wc_ChaCha20Poly1305_UpdateData(NULL, generatedPlaintext,
  5838. generatedPlaintext, sizeof(plaintext1));
  5839. if (err != BAD_FUNC_ARG)
  5840. return -4923;
  5841. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext, NULL,
  5842. sizeof(plaintext1));
  5843. if (err != BAD_FUNC_ARG)
  5844. return -4924;
  5845. err = wc_ChaCha20Poly1305_UpdateData(&aead, NULL, generatedPlaintext,
  5846. sizeof(plaintext1));
  5847. if (err != BAD_FUNC_ARG)
  5848. return -4925;
  5849. err = wc_ChaCha20Poly1305_Final(NULL, generatedAuthTag);
  5850. if (err != BAD_FUNC_ARG)
  5851. return -4926;
  5852. err = wc_ChaCha20Poly1305_Final(&aead, NULL);
  5853. if (err != BAD_FUNC_ARG)
  5854. return -4927;
  5855. /* AEAD init/update/final - bad state tests */
  5856. /* clear struct - make valgrind happy to resolve
  5857. "Conditional jump or move depends on uninitialised value(s)".
  5858. The enum is "int" size and aead.state is "byte" */
  5859. /* The wc_ChaCha20Poly1305_Init function does this normally */
  5860. XMEMSET(&aead, 0, sizeof(aead));
  5861. aead.state = CHACHA20_POLY1305_STATE_INIT;
  5862. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5863. if (err != BAD_STATE_E)
  5864. return -4928;
  5865. aead.state = CHACHA20_POLY1305_STATE_DATA;
  5866. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5867. if (err != BAD_STATE_E)
  5868. return -4929;
  5869. aead.state = CHACHA20_POLY1305_STATE_INIT;
  5870. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext,
  5871. generatedPlaintext, sizeof(plaintext1));
  5872. if (err != BAD_STATE_E)
  5873. return -4930;
  5874. aead.state = CHACHA20_POLY1305_STATE_INIT;
  5875. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5876. if (err != BAD_STATE_E)
  5877. return -4931;
  5878. aead.state = CHACHA20_POLY1305_STATE_READY;
  5879. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5880. if (err != BAD_STATE_E)
  5881. return -4932;
  5882. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5883. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5884. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5885. /* Test 1 - Encrypt */
  5886. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  5887. CHACHA20_POLY1305_AEAD_ENCRYPT);
  5888. if (err != 0)
  5889. return -4933;
  5890. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5891. if (err != 0)
  5892. return -4934;
  5893. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5894. /* test doing data in smaller chunks */
  5895. for (testLen=0; testLen<sizeof(plaintext1); ) {
  5896. word32 dataLen = sizeof(plaintext1) - testLen;
  5897. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5898. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5899. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext1[testLen],
  5900. &generatedCiphertext[testLen], dataLen);
  5901. if (err != 0)
  5902. return -4935;
  5903. testLen += dataLen;
  5904. }
  5905. #else
  5906. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext1,
  5907. generatedCiphertext, sizeof(plaintext1));
  5908. #endif
  5909. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5910. if (err != 0)
  5911. return -4936;
  5912. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  5913. if (err != 0)
  5914. return -4937;
  5915. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  5916. return -4938;
  5917. }
  5918. /* Test 1 - Decrypt */
  5919. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  5920. CHACHA20_POLY1305_AEAD_DECRYPT);
  5921. if (err != 0)
  5922. return -4939;
  5923. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5924. if (err != 0)
  5925. return -4940;
  5926. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5927. /* test doing data in smaller chunks */
  5928. for (testLen=0; testLen<sizeof(plaintext1); ) {
  5929. word32 dataLen = sizeof(plaintext1) - testLen;
  5930. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5931. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5932. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  5933. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  5934. dataLen);
  5935. if (err != 0)
  5936. return -4941;
  5937. testLen += dataLen;
  5938. }
  5939. #else
  5940. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  5941. generatedPlaintext, sizeof(cipher1));
  5942. #endif
  5943. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5944. if (err != 0)
  5945. return -4942;
  5946. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  5947. if (err != 0)
  5948. return -4943;
  5949. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  5950. return -4944;
  5951. }
  5952. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5953. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5954. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5955. /* Test 2 - Encrypt */
  5956. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  5957. CHACHA20_POLY1305_AEAD_ENCRYPT);
  5958. if (err != 0)
  5959. return -4945;
  5960. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  5961. if (err != 0)
  5962. return -4946;
  5963. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5964. /* test doing data in smaller chunks */
  5965. for (testLen=0; testLen<sizeof(plaintext2); ) {
  5966. word32 dataLen = sizeof(plaintext2) - testLen;
  5967. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5968. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5969. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext2[testLen],
  5970. &generatedCiphertext[testLen], dataLen);
  5971. if (err != 0)
  5972. return -4947;
  5973. testLen += dataLen;
  5974. }
  5975. #else
  5976. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext2, generatedCiphertext,
  5977. sizeof(plaintext2));
  5978. #endif
  5979. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5980. if (err != 0)
  5981. return -4948;
  5982. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  5983. if (err != 0)
  5984. return -4949;
  5985. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  5986. return -4950;
  5987. }
  5988. /* Test 2 - Decrypt */
  5989. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  5990. CHACHA20_POLY1305_AEAD_DECRYPT);
  5991. if (err != 0)
  5992. return -4951;
  5993. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  5994. if (err != 0)
  5995. return -4952;
  5996. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5997. /* test doing data in smaller chunks */
  5998. for (testLen=0; testLen<sizeof(plaintext2); ) {
  5999. word32 dataLen = sizeof(plaintext2) - testLen;
  6000. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  6001. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  6002. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  6003. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  6004. dataLen);
  6005. if (err != 0)
  6006. return -4953;
  6007. testLen += dataLen;
  6008. }
  6009. #else
  6010. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  6011. generatedPlaintext, sizeof(cipher2));
  6012. #endif
  6013. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  6014. if (err != 0)
  6015. return -4954;
  6016. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  6017. if (err != 0)
  6018. return -4955;
  6019. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  6020. return -4956;
  6021. }
  6022. return err;
  6023. }
  6024. #endif /* HAVE_CHACHA && HAVE_POLY1305 */
  6025. #ifndef NO_DES3
  6026. WOLFSSL_TEST_SUBROUTINE int des_test(void)
  6027. {
  6028. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  6029. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  6030. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  6031. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  6032. };
  6033. byte plain[24];
  6034. byte cipher[24];
  6035. Des enc;
  6036. Des dec;
  6037. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  6038. {
  6039. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  6040. };
  6041. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  6042. {
  6043. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  6044. };
  6045. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  6046. {
  6047. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  6048. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  6049. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  6050. };
  6051. int ret;
  6052. ret = wc_Des_SetKey(&enc, key, iv, DES_ENCRYPTION);
  6053. if (ret != 0)
  6054. return -5000;
  6055. ret = wc_Des_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  6056. if (ret != 0)
  6057. return -5001;
  6058. ret = wc_Des_SetKey(&dec, key, iv, DES_DECRYPTION);
  6059. if (ret != 0)
  6060. return -5002;
  6061. ret = wc_Des_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  6062. if (ret != 0)
  6063. return -5003;
  6064. if (XMEMCMP(plain, vector, sizeof(plain)))
  6065. return -5004;
  6066. if (XMEMCMP(cipher, verify, sizeof(cipher)))
  6067. return -5005;
  6068. ret = wc_Des_CbcEncryptWithKey(cipher, vector, sizeof(vector), key, iv);
  6069. if (ret != 0)
  6070. return -5006;
  6071. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  6072. {
  6073. EncryptedInfo info;
  6074. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  6075. XMEMCPY(info.iv, iv, sizeof(iv));
  6076. info.ivSz = sizeof(iv);
  6077. info.keySz = sizeof(key);
  6078. info.cipherType = WC_CIPHER_DES;
  6079. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  6080. sizeof(key), WC_HASH_TYPE_SHA);
  6081. if (ret != 0)
  6082. return -5007;
  6083. /* Test invalid info ptr */
  6084. ret = wc_BufferKeyEncrypt(NULL, cipher, sizeof(cipher), key,
  6085. sizeof(key), WC_HASH_TYPE_SHA);
  6086. if (ret != BAD_FUNC_ARG)
  6087. return -5008;
  6088. #ifndef NO_PWDBASED
  6089. /* Test invalid hash type - only applies to wc_PBKDF1 call */
  6090. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  6091. sizeof(key), WC_HASH_TYPE_NONE);
  6092. if (ret == 0)
  6093. return -5009;
  6094. #endif /* !NO_PWDBASED */
  6095. }
  6096. #endif
  6097. return 0;
  6098. }
  6099. #endif /* !NO_DES3 */
  6100. #ifndef NO_DES3
  6101. WOLFSSL_TEST_SUBROUTINE int des3_test(void)
  6102. {
  6103. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  6104. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  6105. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  6106. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  6107. };
  6108. byte plain[24];
  6109. byte cipher[24];
  6110. Des3 enc;
  6111. Des3 dec;
  6112. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6113. {
  6114. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  6115. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  6116. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  6117. };
  6118. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  6119. {
  6120. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  6121. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  6122. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  6123. };
  6124. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] =
  6125. {
  6126. 0x43,0xa0,0x29,0x7e,0xd1,0x84,0xf8,0x0e,
  6127. 0x89,0x64,0x84,0x32,0x12,0xd5,0x08,0x98,
  6128. 0x18,0x94,0x15,0x74,0x87,0x12,0x7d,0xb0
  6129. };
  6130. int ret;
  6131. if (wc_Des3Init(&enc, HEAP_HINT, devId) != 0)
  6132. return -5100;
  6133. if (wc_Des3Init(&dec, HEAP_HINT, devId) != 0)
  6134. return -5101;
  6135. ret = wc_Des3_SetKey(&enc, key3, iv3, DES_ENCRYPTION);
  6136. if (ret != 0)
  6137. return -5102;
  6138. ret = wc_Des3_SetKey(&dec, key3, iv3, DES_DECRYPTION);
  6139. if (ret != 0)
  6140. return -5103;
  6141. ret = wc_Des3_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  6142. #if defined(WOLFSSL_ASYNC_CRYPT)
  6143. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  6144. #endif
  6145. if (ret != 0)
  6146. return -5104;
  6147. ret = wc_Des3_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  6148. #if defined(WOLFSSL_ASYNC_CRYPT)
  6149. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  6150. #endif
  6151. if (ret != 0)
  6152. return -5105;
  6153. if (XMEMCMP(plain, vector, sizeof(plain)))
  6154. return -5106;
  6155. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  6156. return -5107;
  6157. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  6158. /* test the same vectors with using compatibility layer */
  6159. {
  6160. DES_key_schedule ks1;
  6161. DES_key_schedule ks2;
  6162. DES_key_schedule ks3;
  6163. DES_cblock iv4;
  6164. XMEMCPY(ks1, key3, sizeof(DES_key_schedule));
  6165. XMEMCPY(ks2, key3 + 8, sizeof(DES_key_schedule));
  6166. XMEMCPY(ks3, key3 + 16, sizeof(DES_key_schedule));
  6167. XMEMCPY(iv4, iv3, sizeof(DES_cblock));
  6168. XMEMSET(plain, 0, sizeof(plain));
  6169. XMEMSET(cipher, 0, sizeof(cipher));
  6170. DES_ede3_cbc_encrypt(vector, cipher, sizeof(vector), &ks1, &ks2, &ks3,
  6171. &iv4, DES_ENCRYPT);
  6172. DES_ede3_cbc_encrypt(cipher, plain, sizeof(cipher), &ks1, &ks2, &ks3,
  6173. &iv4, DES_DECRYPT);
  6174. if (XMEMCMP(plain, vector, sizeof(plain)))
  6175. return -5108;
  6176. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  6177. return -5109;
  6178. }
  6179. #endif /* OPENSSL_EXTRA */
  6180. wc_Des3Free(&enc);
  6181. wc_Des3Free(&dec);
  6182. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  6183. {
  6184. EncryptedInfo info;
  6185. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  6186. XMEMCPY(info.iv, iv3, sizeof(iv3));
  6187. info.ivSz = sizeof(iv3);
  6188. info.keySz = sizeof(key3);
  6189. info.cipherType = WC_CIPHER_DES3;
  6190. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key3,
  6191. sizeof(key3), WC_HASH_TYPE_SHA);
  6192. if (ret != 0)
  6193. return -5110;
  6194. }
  6195. #endif
  6196. return 0;
  6197. }
  6198. #endif /* NO_DES3 */
  6199. #ifndef NO_AES
  6200. #if defined(WOLFSSL_AES_OFB) || defined(WOLFSSL_AES_CFB) || \
  6201. defined(WOLFSSL_AES_XTS)
  6202. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6203. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6204. /* pass in the function, key, iv, plain text and expected and this function
  6205. * tests that the encryption and decryption is successful */
  6206. static int EVP_test(const WOLFSSL_EVP_CIPHER* type, const byte* key,
  6207. const byte* iv, const byte* plain, int plainSz,
  6208. const byte* expected, int expectedSz)
  6209. {
  6210. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6211. EVP_CIPHER_CTX *ctx = NULL;
  6212. #else
  6213. EVP_CIPHER_CTX ctx[1];
  6214. #endif
  6215. int idx, ret = 0, cipherSz;
  6216. byte* cipher;
  6217. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6218. if ((ctx = wolfSSL_EVP_CIPHER_CTX_new()) == NULL)
  6219. return MEMORY_E;
  6220. #endif
  6221. cipher = (byte*)XMALLOC(plainSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6222. if (cipher == NULL) {
  6223. ret = -5120;
  6224. goto EVP_TEST_END;
  6225. }
  6226. /* test encrypt */
  6227. EVP_CIPHER_CTX_init(ctx);
  6228. if (EVP_CipherInit(ctx, type, key, iv, 1) == 0) {
  6229. ret = -5121;
  6230. goto EVP_TEST_END;
  6231. }
  6232. if (EVP_CipherUpdate(ctx, cipher, &idx, plain, expectedSz) == 0) {
  6233. ret = -5122;
  6234. goto EVP_TEST_END;
  6235. }
  6236. cipherSz = idx;
  6237. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  6238. ret = -5123;
  6239. goto EVP_TEST_END;
  6240. }
  6241. cipherSz += idx;
  6242. if (XMEMCMP(cipher, expected, plainSz)) {
  6243. ret = -5124;
  6244. goto EVP_TEST_END;
  6245. }
  6246. /* test decrypt */
  6247. EVP_CIPHER_CTX_init(ctx);
  6248. if (EVP_CipherInit(ctx, type, key, iv, 0) == 0) {
  6249. ret = -5125;
  6250. goto EVP_TEST_END;
  6251. }
  6252. if (EVP_CipherUpdate(ctx, cipher, &idx, cipher, expectedSz) == 0) {
  6253. ret = -5126;
  6254. goto EVP_TEST_END;
  6255. }
  6256. cipherSz = idx;
  6257. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  6258. ret = -5127;
  6259. goto EVP_TEST_END;
  6260. }
  6261. cipherSz += idx;
  6262. if ((expectedSz != cipherSz) || XMEMCMP(plain, cipher, plainSz)) {
  6263. ret = -5128;
  6264. goto EVP_TEST_END;
  6265. }
  6266. EVP_TEST_END:
  6267. if (cipher)
  6268. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6269. (void)cipherSz;
  6270. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6271. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  6272. #endif
  6273. return ret;
  6274. }
  6275. #endif /* OPENSSL_EXTRA */
  6276. #endif /* WOLFSSL_AES_OFB || WOLFSSL_AES_CFB */
  6277. #ifdef WOLFSSL_AES_OFB
  6278. /* test vector from https://csrc.nist.gov/Projects/cryptographic-algorithm-validation-program/Block-Ciphers */
  6279. WOLFSSL_TEST_SUBROUTINE int aesofb_test(void)
  6280. {
  6281. #ifdef WOLFSSL_AES_256
  6282. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6283. {
  6284. 0xc4,0xc7,0xfa,0xd6,0x53,0x5c,0xb8,0x71,
  6285. 0x4a,0x5c,0x40,0x77,0x9a,0x8b,0xa1,0xd2,
  6286. 0x53,0x3e,0x23,0xb4,0xb2,0x58,0x73,0x2a,
  6287. 0x5b,0x78,0x01,0xf4,0xe3,0x71,0xa7,0x94
  6288. };
  6289. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  6290. {
  6291. 0x5e,0xb9,0x33,0x13,0xb8,0x71,0xff,0x16,
  6292. 0xb9,0x8a,0x9b,0xcb,0x43,0x33,0x0d,0x6f
  6293. };
  6294. WOLFSSL_SMALL_STACK_STATIC const byte plain1[] =
  6295. {
  6296. 0x6d,0x0b,0xb0,0x79,0x63,0x84,0x71,0xe9,
  6297. 0x39,0xd4,0x53,0x14,0x86,0xc1,0x4c,0x25,
  6298. 0x9a,0xee,0xc6,0xf3,0xc0,0x0d,0xfd,0xd6,
  6299. 0xc0,0x50,0xa8,0xba,0xa8,0x20,0xdb,0x71,
  6300. 0xcc,0x12,0x2c,0x4e,0x0c,0x17,0x15,0xef,
  6301. 0x55,0xf3,0x99,0x5a,0x6b,0xf0,0x2a,0x4c
  6302. };
  6303. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  6304. {
  6305. 0x0f,0x54,0x61,0x71,0x59,0xd0,0x3f,0xfc,
  6306. 0x1b,0xfa,0xfb,0x60,0x29,0x30,0xd7,0x00,
  6307. 0xf4,0xa4,0xa8,0xe6,0xdd,0x93,0x94,0x46,
  6308. 0x64,0xd2,0x19,0xc4,0xc5,0x4d,0xde,0x1b,
  6309. 0x04,0x53,0xe1,0x73,0xf5,0x18,0x74,0xae,
  6310. 0xfd,0x64,0xa2,0xe1,0xe2,0x76,0x13,0xb0
  6311. };
  6312. #endif /* WOLFSSL_AES_256 */
  6313. #ifdef WOLFSSL_AES_128
  6314. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6315. {
  6316. 0x10,0xa5,0x88,0x69,0xd7,0x4b,0xe5,0xa3,
  6317. 0x74,0xcf,0x86,0x7c,0xfb,0x47,0x38,0x59
  6318. };
  6319. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  6320. {
  6321. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6322. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6323. };
  6324. WOLFSSL_SMALL_STACK_STATIC const byte plain2[] =
  6325. {
  6326. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6327. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6328. };
  6329. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  6330. {
  6331. 0x6d,0x25,0x1e,0x69,0x44,0xb0,0x51,0xe0,
  6332. 0x4e,0xaa,0x6f,0xb4,0xdb,0xf7,0x84,0x65
  6333. };
  6334. #endif /* WOLFSSL_AES_128 */
  6335. #ifdef WOLFSSL_AES_192
  6336. WOLFSSL_SMALL_STACK_STATIC const byte key3[] = {
  6337. 0xd0,0x77,0xa0,0x3b,0xd8,0xa3,0x89,0x73,
  6338. 0x92,0x8c,0xca,0xfe,0x4a,0x9d,0x2f,0x45,
  6339. 0x51,0x30,0xbd,0x0a,0xf5,0xae,0x46,0xa9
  6340. };
  6341. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  6342. {
  6343. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6344. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6345. };
  6346. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  6347. {
  6348. 0xab,0xc7,0x86,0xfb,0x1e,0xdb,0x50,0x45,
  6349. 0x80,0xc4,0xd8,0x82,0xef,0x29,0xa0,0xc7
  6350. };
  6351. WOLFSSL_SMALL_STACK_STATIC const byte plain3[] =
  6352. {
  6353. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6354. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6355. };
  6356. #endif /* WOLFSSL_AES_192 */
  6357. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6358. Aes *enc = NULL;
  6359. #else
  6360. Aes enc[1];
  6361. #endif
  6362. byte cipher[AES_BLOCK_SIZE * 4];
  6363. #ifdef HAVE_AES_DECRYPT
  6364. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6365. Aes *dec = NULL;
  6366. #else
  6367. Aes dec[1];
  6368. #endif
  6369. byte plain [AES_BLOCK_SIZE * 4];
  6370. #endif
  6371. int ret = 0;
  6372. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6373. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6374. ERROR_OUT(-1, out);
  6375. #ifdef HAVE_AES_DECRYPT
  6376. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6377. ERROR_OUT(-1, out);
  6378. #endif
  6379. #endif
  6380. XMEMSET(enc, 0, sizeof *enc);
  6381. #ifdef HAVE_AES_DECRYPT
  6382. XMEMSET(dec, 0, sizeof *dec);
  6383. #endif
  6384. #ifdef WOLFSSL_AES_128
  6385. /* 128 key size test */
  6386. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6387. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6388. ret = EVP_test(EVP_aes_128_ofb(), key2, iv2, plain2, sizeof(plain2),
  6389. cipher2, sizeof(cipher2));
  6390. if (ret != 0) {
  6391. goto out;
  6392. }
  6393. #endif
  6394. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  6395. if (ret != 0)
  6396. ERROR_OUT(-5129, out);
  6397. #ifdef HAVE_AES_DECRYPT
  6398. /* decrypt uses AES_ENCRYPTION */
  6399. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  6400. if (ret != 0)
  6401. ERROR_OUT(-5130, out);
  6402. #endif
  6403. XMEMSET(cipher, 0, sizeof(cipher));
  6404. ret = wc_AesOfbEncrypt(enc, cipher, plain2, AES_BLOCK_SIZE);
  6405. if (ret != 0)
  6406. ERROR_OUT(-5131, out);
  6407. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE))
  6408. ERROR_OUT(-5132, out);
  6409. #ifdef HAVE_AES_DECRYPT
  6410. ret = wc_AesOfbDecrypt(dec, plain, cipher2, AES_BLOCK_SIZE);
  6411. if (ret != 0)
  6412. ERROR_OUT(-5133, out);
  6413. if (XMEMCMP(plain, plain2, AES_BLOCK_SIZE))
  6414. ERROR_OUT(-5134, out);
  6415. #endif /* HAVE_AES_DECRYPT */
  6416. #endif /* WOLFSSL_AES_128 */
  6417. #ifdef WOLFSSL_AES_192
  6418. /* 192 key size test */
  6419. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6420. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6421. ret = EVP_test(EVP_aes_192_ofb(), key3, iv3, plain3, sizeof(plain3),
  6422. cipher3, sizeof(cipher3));
  6423. if (ret != 0) {
  6424. goto out;
  6425. }
  6426. #endif
  6427. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  6428. if (ret != 0)
  6429. ERROR_OUT(-5135, out);
  6430. #ifdef HAVE_AES_DECRYPT
  6431. /* decrypt uses AES_ENCRYPTION */
  6432. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  6433. if (ret != 0)
  6434. ERROR_OUT(-5136, out);
  6435. #endif
  6436. XMEMSET(cipher, 0, sizeof(cipher));
  6437. ret = wc_AesOfbEncrypt(enc, cipher, plain3, AES_BLOCK_SIZE);
  6438. if (ret != 0)
  6439. ERROR_OUT(-5137, out);
  6440. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE))
  6441. ERROR_OUT(-5138, out);
  6442. #ifdef HAVE_AES_DECRYPT
  6443. ret = wc_AesOfbDecrypt(dec, plain, cipher3, AES_BLOCK_SIZE);
  6444. if (ret != 0)
  6445. ERROR_OUT(-5139, out);
  6446. if (XMEMCMP(plain, plain3, AES_BLOCK_SIZE))
  6447. ERROR_OUT(-5140, out);
  6448. #endif /* HAVE_AES_DECRYPT */
  6449. #endif /* WOLFSSL_AES_192 */
  6450. #ifdef WOLFSSL_AES_256
  6451. /* 256 key size test */
  6452. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6453. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6454. ret = EVP_test(EVP_aes_256_ofb(), key1, iv1, plain1, sizeof(plain1),
  6455. cipher1, sizeof(cipher1));
  6456. if (ret != 0) {
  6457. goto out;
  6458. }
  6459. #endif
  6460. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6461. if (ret != 0)
  6462. ERROR_OUT(-5141, out);
  6463. #ifdef HAVE_AES_DECRYPT
  6464. /* decrypt uses AES_ENCRYPTION */
  6465. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6466. if (ret != 0)
  6467. ERROR_OUT(-5142, out);
  6468. #endif
  6469. XMEMSET(cipher, 0, sizeof(cipher));
  6470. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE);
  6471. if (ret != 0)
  6472. ERROR_OUT(-5143, out);
  6473. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE))
  6474. ERROR_OUT(-5144, out);
  6475. ret = wc_AesOfbEncrypt(enc, cipher + AES_BLOCK_SIZE,
  6476. plain1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  6477. if (ret != 0)
  6478. ERROR_OUT(-5145, out);
  6479. if (XMEMCMP(cipher + AES_BLOCK_SIZE, cipher1 + AES_BLOCK_SIZE,
  6480. AES_BLOCK_SIZE))
  6481. ERROR_OUT(-5146, out);
  6482. #ifdef HAVE_AES_DECRYPT
  6483. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE);
  6484. if (ret != 0)
  6485. ERROR_OUT(-5147, out);
  6486. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE))
  6487. ERROR_OUT(-5148, out);
  6488. ret = wc_AesOfbDecrypt(dec, plain + AES_BLOCK_SIZE,
  6489. cipher1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  6490. if (ret != 0)
  6491. ERROR_OUT(-5149, out);
  6492. if (XMEMCMP(plain + AES_BLOCK_SIZE, plain1 + AES_BLOCK_SIZE,
  6493. AES_BLOCK_SIZE))
  6494. ERROR_OUT(-5150, out);
  6495. #endif /* HAVE_AES_DECRYPT */
  6496. /* multiple blocks at once */
  6497. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6498. if (ret != 0)
  6499. ERROR_OUT(-5151, out);
  6500. #ifdef HAVE_AES_DECRYPT
  6501. /* decrypt uses AES_ENCRYPTION */
  6502. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6503. if (ret != 0)
  6504. ERROR_OUT(-5152, out);
  6505. #endif
  6506. XMEMSET(cipher, 0, sizeof(cipher));
  6507. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE * 3);
  6508. if (ret != 0)
  6509. ERROR_OUT(-5153, out);
  6510. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 3))
  6511. ERROR_OUT(-5154, out);
  6512. #ifdef HAVE_AES_DECRYPT
  6513. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE * 3);
  6514. if (ret != 0)
  6515. ERROR_OUT(-5155, out);
  6516. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE * 3))
  6517. ERROR_OUT(-5156, out);
  6518. #endif /* HAVE_AES_DECRYPT */
  6519. /* inline decrypt/encrypt*/
  6520. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6521. if (ret != 0)
  6522. ERROR_OUT(-5157, out);
  6523. #ifdef HAVE_AES_DECRYPT
  6524. /* decrypt uses AES_ENCRYPTION */
  6525. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6526. if (ret != 0)
  6527. ERROR_OUT(-5158, out);
  6528. #endif
  6529. XMEMCPY(cipher, plain1, AES_BLOCK_SIZE * 2);
  6530. ret = wc_AesOfbEncrypt(enc, cipher, cipher, AES_BLOCK_SIZE * 2);
  6531. if (ret != 0)
  6532. ERROR_OUT(-5159, out);
  6533. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  6534. ERROR_OUT(-5160, out);
  6535. #ifdef HAVE_AES_DECRYPT
  6536. ret = wc_AesOfbDecrypt(dec, cipher, cipher, AES_BLOCK_SIZE * 2);
  6537. if (ret != 0)
  6538. ERROR_OUT(-5161, out);
  6539. if (XMEMCMP(cipher, plain1, AES_BLOCK_SIZE * 2))
  6540. ERROR_OUT(-5162, out);
  6541. #endif /* HAVE_AES_DECRYPT */
  6542. /* 256 key size test leftover support */
  6543. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6544. if (ret != 0)
  6545. ERROR_OUT(-5163, out);
  6546. #ifdef HAVE_AES_DECRYPT
  6547. /* decrypt uses AES_ENCRYPTION */
  6548. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6549. if (ret != 0)
  6550. ERROR_OUT(-5164, out);
  6551. #endif
  6552. XMEMSET(cipher, 0, sizeof(cipher));
  6553. ret = wc_AesOfbEncrypt(enc, cipher, plain1, 3);
  6554. if (ret != 0)
  6555. ERROR_OUT(-5165, out);
  6556. if (XMEMCMP(cipher, cipher1, 3))
  6557. ERROR_OUT(-5166, out);
  6558. ret = wc_AesOfbEncrypt(enc, cipher + 3, plain1 + 3, AES_BLOCK_SIZE);
  6559. if (ret != 0)
  6560. ERROR_OUT(-5167, out);
  6561. if (XMEMCMP(cipher + 3, cipher1 + 3, AES_BLOCK_SIZE))
  6562. ERROR_OUT(-5168, out);
  6563. #ifdef HAVE_AES_DECRYPT
  6564. ret = wc_AesOfbDecrypt(dec, plain, cipher1, 6);
  6565. if (ret != 0)
  6566. ERROR_OUT(-5169, out);
  6567. if (XMEMCMP(plain, plain1, 6))
  6568. ERROR_OUT(-5170, out);
  6569. ret = wc_AesOfbDecrypt(dec, plain + 6, cipher1 + 6, AES_BLOCK_SIZE);
  6570. if (ret != 0)
  6571. ERROR_OUT(-5171, out);
  6572. if (XMEMCMP(plain + 6, plain1 + 6, AES_BLOCK_SIZE))
  6573. ERROR_OUT(-5172, out);
  6574. #endif /* HAVE_AES_DECRYPT */
  6575. out:
  6576. wc_AesFree(enc);
  6577. wc_AesFree(dec);
  6578. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6579. if (enc)
  6580. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  6581. #ifdef HAVE_AES_DECRYPT
  6582. if (dec)
  6583. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  6584. #endif
  6585. #endif
  6586. #endif /* WOLFSSL_AES_256 */
  6587. return ret;
  6588. }
  6589. #endif /* WOLFSSL_AES_OFB */
  6590. #if defined(WOLFSSL_AES_CFB)
  6591. /* Test cases from NIST SP 800-38A, Recommendation for Block Cipher Modes of Operation Methods an*/
  6592. static int aescfb_test(void)
  6593. {
  6594. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6595. Aes *enc = NULL;
  6596. #else
  6597. Aes enc[1];
  6598. #endif
  6599. int enc_inited = 0;
  6600. byte cipher[AES_BLOCK_SIZE * 4];
  6601. #ifdef HAVE_AES_DECRYPT
  6602. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6603. Aes *dec = NULL;
  6604. #else
  6605. Aes dec[1];
  6606. #endif
  6607. int dec_inited = 0;
  6608. byte plain [AES_BLOCK_SIZE * 4];
  6609. #endif
  6610. int ret = 0;
  6611. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  6612. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  6613. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  6614. };
  6615. #ifdef WOLFSSL_AES_128
  6616. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6617. {
  6618. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  6619. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  6620. };
  6621. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  6622. {
  6623. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  6624. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  6625. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  6626. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b,
  6627. 0x26,0x75,0x1f,0x67,0xa3,0xcb,0xb1,0x40,
  6628. 0xb1,0x80,0x8c,0xf1,0x87,0xa4,0xf4,0xdf
  6629. };
  6630. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  6631. {
  6632. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  6633. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  6634. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  6635. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  6636. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  6637. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef
  6638. };
  6639. #endif /* WOLFSSL_AES_128 */
  6640. #ifdef WOLFSSL_AES_192
  6641. /* 192 size key test */
  6642. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6643. {
  6644. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  6645. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  6646. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  6647. };
  6648. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  6649. {
  6650. 0xcd,0xc8,0x0d,0x6f,0xdd,0xf1,0x8c,0xab,
  6651. 0x34,0xc2,0x59,0x09,0xc9,0x9a,0x41,0x74,
  6652. 0x67,0xce,0x7f,0x7f,0x81,0x17,0x36,0x21,
  6653. 0x96,0x1a,0x2b,0x70,0x17,0x1d,0x3d,0x7a,
  6654. 0x2e,0x1e,0x8a,0x1d,0xd5,0x9b,0x88,0xb1,
  6655. 0xc8,0xe6,0x0f,0xed,0x1e,0xfa,0xc4,0xc9,
  6656. 0xc0,0x5f,0x9f,0x9c,0xa9,0x83,0x4f,0xa0,
  6657. 0x42,0xae,0x8f,0xba,0x58,0x4b,0x09,0xff
  6658. };
  6659. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  6660. {
  6661. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  6662. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  6663. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  6664. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  6665. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  6666. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  6667. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  6668. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  6669. };
  6670. #endif /* WOLFSSL_AES_192 */
  6671. #ifdef WOLFSSL_AES_256
  6672. /* 256 size key simple test */
  6673. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6674. {
  6675. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  6676. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  6677. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  6678. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  6679. };
  6680. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  6681. {
  6682. 0xdc,0x7e,0x84,0xbf,0xda,0x79,0x16,0x4b,
  6683. 0x7e,0xcd,0x84,0x86,0x98,0x5d,0x38,0x60,
  6684. 0x39,0xff,0xed,0x14,0x3b,0x28,0xb1,0xc8,
  6685. 0x32,0x11,0x3c,0x63,0x31,0xe5,0x40,0x7b,
  6686. 0xdf,0x10,0x13,0x24,0x15,0xe5,0x4b,0x92,
  6687. 0xa1,0x3e,0xd0,0xa8,0x26,0x7a,0xe2,0xf9,
  6688. 0x75,0xa3,0x85,0x74,0x1a,0xb9,0xce,0xf8,
  6689. 0x20,0x31,0x62,0x3d,0x55,0xb1,0xe4,0x71
  6690. };
  6691. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  6692. {
  6693. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  6694. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  6695. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  6696. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  6697. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  6698. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  6699. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  6700. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  6701. };
  6702. #endif /* WOLFSSL_AES_256 */
  6703. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6704. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6705. ERROR_OUT(-1, out);
  6706. #ifdef HAVE_AES_DECRYPT
  6707. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6708. ERROR_OUT(-1, out);
  6709. #endif
  6710. #endif
  6711. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  6712. ERROR_OUT(-5173, out);
  6713. else
  6714. enc_inited = 1;
  6715. #ifdef HAVE_AES_DECRYPT
  6716. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  6717. ERROR_OUT(-5174, out);
  6718. else
  6719. dec_inited = 1;
  6720. #endif
  6721. #ifdef WOLFSSL_AES_128
  6722. /* 128 key tests */
  6723. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6724. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6725. ret = EVP_test(EVP_aes_128_cfb128(), key1, iv, msg1, sizeof(msg1),
  6726. cipher1, sizeof(cipher1));
  6727. if (ret != 0) {
  6728. return ret;
  6729. }
  6730. #endif
  6731. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6732. if (ret != 0)
  6733. ERROR_OUT(-5175, out);
  6734. #ifdef HAVE_AES_DECRYPT
  6735. /* decrypt uses AES_ENCRYPTION */
  6736. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6737. if (ret != 0)
  6738. ERROR_OUT(-5176, out);
  6739. #endif
  6740. XMEMSET(cipher, 0, sizeof(cipher));
  6741. ret = wc_AesCfbEncrypt(enc, cipher, msg1, AES_BLOCK_SIZE * 2);
  6742. if (ret != 0)
  6743. ERROR_OUT(-5177, out);
  6744. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  6745. ERROR_OUT(-5178, out);
  6746. /* test restarting encryption process */
  6747. ret = wc_AesCfbEncrypt(enc, cipher + (AES_BLOCK_SIZE * 2),
  6748. msg1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE);
  6749. if (ret != 0)
  6750. ERROR_OUT(-5179, out);
  6751. if (XMEMCMP(cipher + (AES_BLOCK_SIZE * 2),
  6752. cipher1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE))
  6753. ERROR_OUT(-5180, out);
  6754. #ifdef HAVE_AES_DECRYPT
  6755. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 3);
  6756. if (ret != 0)
  6757. ERROR_OUT(-5181, out);
  6758. if (XMEMCMP(plain, msg1, AES_BLOCK_SIZE * 3))
  6759. ERROR_OUT(-5182, out);
  6760. #endif /* HAVE_AES_DECRYPT */
  6761. #endif /* WOLFSSL_AES_128 */
  6762. #ifdef WOLFSSL_AES_192
  6763. /* 192 key size test */
  6764. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6765. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6766. ret = EVP_test(EVP_aes_192_cfb128(), key2, iv, msg2, sizeof(msg2),
  6767. cipher2, sizeof(cipher2));
  6768. if (ret != 0) {
  6769. return ret;
  6770. }
  6771. #endif
  6772. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv, AES_ENCRYPTION);
  6773. if (ret != 0)
  6774. ERROR_OUT(-5183, out);
  6775. #ifdef HAVE_AES_DECRYPT
  6776. /* decrypt uses AES_ENCRYPTION */
  6777. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv, AES_ENCRYPTION);
  6778. if (ret != 0)
  6779. ERROR_OUT(-5184, out);
  6780. #endif
  6781. XMEMSET(cipher, 0, sizeof(cipher));
  6782. ret = wc_AesCfbEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE * 4);
  6783. if (ret != 0)
  6784. ERROR_OUT(-5185, out);
  6785. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE * 4))
  6786. ERROR_OUT(-5186, out);
  6787. #ifdef HAVE_AES_DECRYPT
  6788. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 4);
  6789. if (ret != 0)
  6790. ERROR_OUT(-5187, out);
  6791. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE * 4))
  6792. ERROR_OUT(-5188, out);
  6793. #endif /* HAVE_AES_DECRYPT */
  6794. #endif /* WOLFSSL_AES_192 */
  6795. #ifdef WOLFSSL_AES_256
  6796. /* 256 key size test */
  6797. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6798. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6799. ret = EVP_test(EVP_aes_256_cfb128(), key3, iv, msg3, sizeof(msg3),
  6800. cipher3, sizeof(cipher3));
  6801. if (ret != 0) {
  6802. return ret;
  6803. }
  6804. #endif
  6805. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv, AES_ENCRYPTION);
  6806. if (ret != 0)
  6807. ERROR_OUT(-5189, out);
  6808. #ifdef HAVE_AES_DECRYPT
  6809. /* decrypt uses AES_ENCRYPTION */
  6810. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv, AES_ENCRYPTION);
  6811. if (ret != 0)
  6812. ERROR_OUT(-5190, out);
  6813. #endif
  6814. /* test with data left overs, magic lengths are checking near edges */
  6815. XMEMSET(cipher, 0, sizeof(cipher));
  6816. ret = wc_AesCfbEncrypt(enc, cipher, msg3, 4);
  6817. if (ret != 0)
  6818. ERROR_OUT(-5191, out);
  6819. if (XMEMCMP(cipher, cipher3, 4))
  6820. ERROR_OUT(-5192, out);
  6821. ret = wc_AesCfbEncrypt(enc, cipher + 4, msg3 + 4, 27);
  6822. if (ret != 0)
  6823. ERROR_OUT(-5193, out);
  6824. if (XMEMCMP(cipher + 4, cipher3 + 4, 27))
  6825. ERROR_OUT(-5194, out);
  6826. ret = wc_AesCfbEncrypt(enc, cipher + 31, msg3 + 31,
  6827. (AES_BLOCK_SIZE * 4) - 31);
  6828. if (ret != 0)
  6829. ERROR_OUT(-5195, out);
  6830. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE * 4))
  6831. ERROR_OUT(-5196, out);
  6832. #ifdef HAVE_AES_DECRYPT
  6833. ret = wc_AesCfbDecrypt(dec, plain, cipher, 4);
  6834. if (ret != 0)
  6835. ERROR_OUT(-5197, out);
  6836. if (XMEMCMP(plain, msg3, 4))
  6837. ERROR_OUT(-5198, out);
  6838. ret = wc_AesCfbDecrypt(dec, plain + 4, cipher + 4, 4);
  6839. if (ret != 0)
  6840. ERROR_OUT(-5199, out);
  6841. ret = wc_AesCfbDecrypt(dec, plain + 8, cipher + 8, 23);
  6842. if (ret != 0)
  6843. ERROR_OUT(-5200, out);
  6844. if (XMEMCMP(plain + 4, msg3 + 4, 27))
  6845. ERROR_OUT(-5201, out);
  6846. ret = wc_AesCfbDecrypt(dec, plain + 31, cipher + 31,
  6847. (AES_BLOCK_SIZE * 4) - 31);
  6848. if (ret != 0)
  6849. ERROR_OUT(-5202, out);
  6850. if (XMEMCMP(plain, msg3, AES_BLOCK_SIZE * 4))
  6851. ERROR_OUT(-5203, out);
  6852. #endif /* HAVE_AES_DECRYPT */
  6853. #endif /* WOLFSSL_AES_256 */
  6854. out:
  6855. if (enc_inited)
  6856. wc_AesFree(enc);
  6857. if (dec_inited)
  6858. wc_AesFree(dec);
  6859. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6860. if (enc)
  6861. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  6862. #ifdef HAVE_AES_DECRYPT
  6863. if (dec)
  6864. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  6865. #endif
  6866. #endif
  6867. return ret;
  6868. }
  6869. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6870. static int aescfb1_test(void)
  6871. {
  6872. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6873. Aes *enc = NULL;
  6874. #else
  6875. Aes enc[1];
  6876. #endif
  6877. int enc_inited = 0;
  6878. byte cipher[AES_BLOCK_SIZE];
  6879. #ifdef HAVE_AES_DECRYPT
  6880. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6881. Aes *dec = NULL;
  6882. #else
  6883. Aes dec[1];
  6884. #endif
  6885. int dec_inited = 0;
  6886. byte plain [AES_BLOCK_SIZE];
  6887. #endif
  6888. int ret = 0;
  6889. #ifdef WOLFSSL_AES_128
  6890. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  6891. 0x4d,0xbb,0xdc,0xaa,0x59,0xf3,0x63,0xc9,
  6892. 0x2a,0x3b,0x98,0x43,0xad,0x20,0xe2,0xb7
  6893. };
  6894. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6895. {
  6896. 0xcd,0xef,0x9d,0x06,0x61,0xba,0xe4,0x73,
  6897. 0x8d,0x1a,0x58,0xa2,0xa6,0x22,0x8b,0x66
  6898. };
  6899. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  6900. {
  6901. 0x00
  6902. };
  6903. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  6904. {
  6905. 0xC0
  6906. };
  6907. #endif /* WOLFSSL_AES_128 */
  6908. #ifdef WOLFSSL_AES_192
  6909. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  6910. 0x57,0xc6,0x89,0x7c,0x99,0x52,0x28,0x13,
  6911. 0xbf,0x67,0x9c,0xe1,0x13,0x70,0xaf,0x5e
  6912. };
  6913. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6914. {
  6915. 0xba,0xa1,0x58,0xa1,0x6b,0x50,0x4a,0x10,
  6916. 0x8e,0xd4,0x33,0x2e,0xe7,0xf2,0x9b,0xf6,
  6917. 0xd1,0xac,0x46,0xa8,0xde,0x5a,0xfe,0x7a
  6918. };
  6919. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  6920. {
  6921. 0x30
  6922. };
  6923. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  6924. {
  6925. 0x80
  6926. };
  6927. #endif /* WOLFSSL_AES_192 */
  6928. #ifdef WOLFSSL_AES_256
  6929. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  6930. 0x63,0x2e,0x9f,0x83,0x1f,0xa3,0x80,0x5e,
  6931. 0x52,0x02,0xbc,0xe0,0x6d,0x04,0xf9,0xa0
  6932. };
  6933. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6934. {
  6935. 0xf6,0xfa,0xe4,0xf1,0x5d,0x91,0xfc,0x50,
  6936. 0x88,0x78,0x4f,0x84,0xa5,0x37,0x12,0x7e,
  6937. 0x32,0x63,0x55,0x9c,0x62,0x73,0x88,0x20,
  6938. 0xc2,0xcf,0x3d,0xe1,0x1c,0x2a,0x30,0x40
  6939. };
  6940. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  6941. {
  6942. 0xF7, 0x00
  6943. };
  6944. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  6945. {
  6946. 0x41, 0xC0
  6947. };
  6948. #endif /* WOLFSSL_AES_256 */
  6949. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6950. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6951. ERROR_OUT(-1, out);
  6952. #ifdef HAVE_AES_DECRYPT
  6953. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6954. ERROR_OUT(-1, out);
  6955. #endif
  6956. #endif
  6957. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  6958. ERROR_OUT(-5204, out);
  6959. else
  6960. enc_inited = 1;
  6961. #ifdef HAVE_AES_DECRYPT
  6962. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  6963. ERROR_OUT(-5205, out);
  6964. else
  6965. dec_inited = 1;
  6966. #endif
  6967. #ifdef WOLFSSL_AES_128
  6968. /* 128 key tests */
  6969. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6970. if (ret != 0)
  6971. ERROR_OUT(-5206, out);
  6972. #ifdef HAVE_AES_DECRYPT
  6973. /* decrypt uses AES_ENCRYPTION */
  6974. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6975. if (ret != 0)
  6976. ERROR_OUT(-5207, out);
  6977. #endif
  6978. XMEMSET(cipher, 0, sizeof(cipher));
  6979. ret = wc_AesCfb1Encrypt(enc, cipher, msg1, 2);
  6980. if (ret != 0)
  6981. ERROR_OUT(-5208, out);
  6982. if (cipher[0] != cipher1[0])
  6983. ERROR_OUT(-5209, out);
  6984. #ifdef HAVE_AES_DECRYPT
  6985. ret = wc_AesCfb1Decrypt(dec, plain, cipher, 2);
  6986. if (ret != 0)
  6987. ERROR_OUT(-5210, out);
  6988. if (plain[0] != msg1[0])
  6989. ERROR_OUT(-5211, out);
  6990. #endif /* HAVE_AES_DECRYPT */
  6991. #ifdef OPENSSL_EXTRA
  6992. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6993. if (ret != 0)
  6994. ERROR_OUT(-5212, out);
  6995. XMEMSET(cipher, 0, sizeof(cipher));
  6996. ret = wc_AesCfb1Encrypt(enc, cipher, msg1,
  6997. sizeof(msg1) * WOLFSSL_BIT_SIZE);
  6998. if (ret != 0)
  6999. ERROR_OUT(-5213, out);
  7000. #ifndef WOLFCRYPT_ONLY
  7001. ret = EVP_test(EVP_aes_128_cfb1(), key1, iv, msg1, sizeof(msg1),
  7002. cipher, sizeof(msg1));
  7003. if (ret != 0) {
  7004. goto out;
  7005. }
  7006. #endif
  7007. #endif
  7008. #endif /* WOLFSSL_AES_128 */
  7009. #ifdef WOLFSSL_AES_192
  7010. /* 192 key tests */
  7011. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  7012. if (ret != 0)
  7013. ERROR_OUT(-5214, out);
  7014. XMEMSET(cipher, 0, sizeof(cipher));
  7015. ret = wc_AesCfb1Encrypt(enc, cipher, msg2, 4);
  7016. if (ret != 0)
  7017. ERROR_OUT(-5215, out);
  7018. if (XMEMCMP(cipher, cipher2, sizeof(cipher2)) != 0)
  7019. ERROR_OUT(-5216, out);
  7020. #ifdef OPENSSL_EXTRA
  7021. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  7022. if (ret != 0)
  7023. ERROR_OUT(-5217, out);
  7024. XMEMSET(cipher, 0, sizeof(cipher));
  7025. ret = wc_AesCfb1Encrypt(enc, cipher, msg2,
  7026. sizeof(msg2) * WOLFSSL_BIT_SIZE);
  7027. if (ret != 0)
  7028. ERROR_OUT(-5218, out);
  7029. #ifndef WOLFCRYPT_ONLY
  7030. ret = EVP_test(EVP_aes_192_cfb1(), key2, iv2, msg2, sizeof(msg2),
  7031. cipher, sizeof(msg2));
  7032. if (ret != 0) {
  7033. goto out;
  7034. }
  7035. #endif
  7036. #endif
  7037. #endif /* WOLFSSL_AES_192 */
  7038. #ifdef WOLFSSL_AES_256
  7039. /* 256 key tests */
  7040. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  7041. if (ret != 0)
  7042. ERROR_OUT(-5219, out);
  7043. XMEMSET(cipher, 0, sizeof(cipher));
  7044. ret = wc_AesCfb1Encrypt(enc, cipher, msg3, 10);
  7045. if (ret != 0)
  7046. ERROR_OUT(-5220, out);
  7047. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  7048. ERROR_OUT(-5221, out);
  7049. #ifdef OPENSSL_EXTRA
  7050. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  7051. if (ret != 0)
  7052. ERROR_OUT(-5222, out);
  7053. XMEMSET(cipher, 0, sizeof(cipher));
  7054. ret = wc_AesCfb1Encrypt(enc, cipher, msg3,
  7055. sizeof(msg3) * WOLFSSL_BIT_SIZE);
  7056. if (ret != 0)
  7057. ERROR_OUT(-5223, out);
  7058. #ifndef WOLFCRYPT_ONLY
  7059. ret = EVP_test(EVP_aes_256_cfb1(), key3, iv3, msg3, sizeof(msg3),
  7060. cipher, sizeof(msg3));
  7061. if (ret != 0) {
  7062. goto out;
  7063. }
  7064. #endif
  7065. #endif
  7066. out:
  7067. if (enc_inited)
  7068. wc_AesFree(enc);
  7069. #ifdef HAVE_AES_DECRYPT
  7070. if (dec_inited)
  7071. wc_AesFree(dec);
  7072. #endif
  7073. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7074. if (enc)
  7075. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7076. #ifdef HAVE_AES_DECRYPT
  7077. if (dec)
  7078. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7079. #endif
  7080. #endif
  7081. #endif /* WOLFSSL_AES_256 */
  7082. return ret;
  7083. }
  7084. static int aescfb8_test(void)
  7085. {
  7086. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7087. Aes *enc = NULL;
  7088. #else
  7089. Aes enc[1];
  7090. #endif
  7091. int enc_inited = 0;
  7092. byte cipher[AES_BLOCK_SIZE];
  7093. #ifdef HAVE_AES_DECRYPT
  7094. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7095. Aes *dec = NULL;
  7096. #else
  7097. Aes dec[1];
  7098. #endif
  7099. int dec_inited = 0;
  7100. byte plain [AES_BLOCK_SIZE];
  7101. #endif
  7102. int ret = 0;
  7103. #ifdef WOLFSSL_AES_128
  7104. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  7105. 0xf4,0x75,0xc6,0x49,0x91,0xb2,0x0e,0xae,
  7106. 0xe1,0x83,0xa2,0x26,0x29,0xe2,0x1e,0x22
  7107. };
  7108. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  7109. {
  7110. 0xc8,0xfe,0x9b,0xf7,0x7b,0x93,0x0f,0x46,
  7111. 0xd2,0x07,0x8b,0x8c,0x0e,0x65,0x7c,0xd4
  7112. };
  7113. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  7114. {
  7115. 0xd2,0x76,0x91
  7116. };
  7117. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  7118. {
  7119. 0xc9,0x06,0x35
  7120. };
  7121. #endif /* WOLFSSL_AES_128 */
  7122. #ifdef WOLFSSL_AES_192
  7123. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  7124. 0x0a,0x02,0x84,0x6b,0x62,0xab,0xb6,0x93,
  7125. 0xef,0x31,0xd7,0x54,0x84,0x2e,0xed,0x29
  7126. };
  7127. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  7128. {
  7129. 0xba,0xf0,0x8b,0x76,0x31,0x7a,0x65,0xc5,
  7130. 0xf0,0x7a,0xe6,0xf5,0x7e,0xb0,0xe6,0x54,
  7131. 0x88,0x65,0x93,0x24,0xd2,0x97,0x09,0xe3
  7132. };
  7133. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  7134. {
  7135. 0x72,0x9c,0x0b,0x6d,0xeb,0x75,0xfa,0x6e,
  7136. 0xb5,0xe8
  7137. };
  7138. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  7139. {
  7140. 0x98,0x95,0x93,0x24,0x02,0x39,0x3d,0xc3,
  7141. 0x3a,0x60
  7142. };
  7143. #endif
  7144. #ifdef WOLFSSL_AES_256
  7145. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  7146. 0x33,0x8c,0x55,0x2f,0xf1,0xec,0xa1,0x44,
  7147. 0x08,0xe0,0x5d,0x8c,0xf9,0xf3,0xb3,0x1b
  7148. };
  7149. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  7150. {
  7151. 0x06,0x48,0x74,0x09,0x2f,0x7a,0x13,0xcc,
  7152. 0x44,0x62,0x24,0x7a,0xd4,0x23,0xd0,0xe9,
  7153. 0x6e,0xdf,0x42,0xe8,0xb6,0x7a,0x5a,0x23,
  7154. 0xb7,0xa0,0xa6,0x47,0x7b,0x09,0x8e,0x66
  7155. };
  7156. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  7157. {
  7158. 0x1c,0xff,0x95
  7159. };
  7160. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  7161. {
  7162. 0xb9,0x74,0xfa
  7163. };
  7164. #endif
  7165. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7166. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7167. ERROR_OUT(-5238, out);
  7168. #ifdef HAVE_AES_DECRYPT
  7169. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7170. ERROR_OUT(-5239, out);
  7171. #endif
  7172. #endif
  7173. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  7174. ERROR_OUT(-5224, out);
  7175. else
  7176. enc_inited = 1;
  7177. #ifdef HAVE_AES_DECRYPT
  7178. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  7179. ERROR_OUT(-5225, out);
  7180. else
  7181. dec_inited = 1;
  7182. #endif
  7183. #ifdef WOLFSSL_AES_128
  7184. /* 128 key tests */
  7185. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7186. ret = EVP_test(EVP_aes_128_cfb8(), key1, iv, msg1, sizeof(msg1),
  7187. cipher1, sizeof(cipher1));
  7188. if (ret != 0) {
  7189. return ret;
  7190. }
  7191. #endif
  7192. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7193. if (ret != 0)
  7194. ERROR_OUT(-5226, out);
  7195. #ifdef HAVE_AES_DECRYPT
  7196. /* decrypt uses AES_ENCRYPTION */
  7197. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7198. if (ret != 0)
  7199. ERROR_OUT(-5227, out);
  7200. #endif
  7201. XMEMSET(cipher, 0, sizeof(cipher));
  7202. ret = wc_AesCfb8Encrypt(enc, cipher, msg1, sizeof(msg1));
  7203. if (ret != 0)
  7204. ERROR_OUT(-5228, out);
  7205. if (XMEMCMP(cipher, cipher1, sizeof(cipher1)) != 0)
  7206. ERROR_OUT(-5229, out);
  7207. #ifdef HAVE_AES_DECRYPT
  7208. ret = wc_AesCfb8Decrypt(dec, plain, cipher, sizeof(msg1));
  7209. if (ret != 0)
  7210. ERROR_OUT(-5230, out);
  7211. if (XMEMCMP(plain, msg1, sizeof(msg1)) != 0)
  7212. ERROR_OUT(-5231, out);
  7213. #endif /* HAVE_AES_DECRYPT */
  7214. #endif /* WOLFSSL_AES_128 */
  7215. #ifdef WOLFSSL_AES_192
  7216. /* 192 key tests */
  7217. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  7218. if (ret != 0)
  7219. ERROR_OUT(-5232, out);
  7220. XMEMSET(cipher, 0, sizeof(cipher));
  7221. ret = wc_AesCfb8Encrypt(enc, cipher, msg2, sizeof(msg2));
  7222. if (ret != 0)
  7223. ERROR_OUT(-5233, out);
  7224. if (XMEMCMP(cipher, cipher2, sizeof(msg2)) != 0)
  7225. ERROR_OUT(-5234, out);
  7226. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7227. ret = EVP_test(EVP_aes_192_cfb8(), key2, iv2, msg2, sizeof(msg2),
  7228. cipher2, sizeof(msg2));
  7229. if (ret != 0) {
  7230. return ret;
  7231. }
  7232. #endif
  7233. #endif /* WOLFSSL_AES_192 */
  7234. #ifdef WOLFSSL_AES_256
  7235. /* 256 key tests */
  7236. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  7237. if (ret != 0)
  7238. ERROR_OUT(-5235, out);
  7239. XMEMSET(cipher, 0, sizeof(cipher));
  7240. ret = wc_AesCfb8Encrypt(enc, cipher, msg3, sizeof(msg3));
  7241. if (ret != 0)
  7242. ERROR_OUT(-5236, out);
  7243. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  7244. ERROR_OUT(-5237, out);
  7245. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7246. ret = EVP_test(EVP_aes_256_cfb8(), key3, iv3, msg3, sizeof(msg3),
  7247. cipher3, sizeof(msg3));
  7248. if (ret != 0) {
  7249. goto out;
  7250. }
  7251. #endif
  7252. out:
  7253. if (enc_inited)
  7254. wc_AesFree(enc);
  7255. #ifdef HAVE_AES_DECRYPT
  7256. if (dec_inited)
  7257. wc_AesFree(dec);
  7258. #endif
  7259. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7260. if (enc)
  7261. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7262. #ifdef HAVE_AES_DECRYPT
  7263. if (dec)
  7264. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7265. #endif
  7266. #endif
  7267. #endif /* WOLFSSL_AES_256 */
  7268. return ret;
  7269. }
  7270. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  7271. #endif /* WOLFSSL_AES_CFB */
  7272. static int aes_key_size_test(void)
  7273. {
  7274. int ret;
  7275. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7276. Aes *aes;
  7277. #else
  7278. Aes aes[1];
  7279. #endif
  7280. byte key16[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7281. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  7282. #ifndef WOLFSSL_CRYPTOCELL
  7283. byte key24[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7284. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  7285. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37 };
  7286. #endif
  7287. byte key32[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7288. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  7289. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7290. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  7291. byte iv[] = "1234567890abcdef";
  7292. #ifndef HAVE_FIPS
  7293. word32 keySize;
  7294. #endif
  7295. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7296. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7297. return -5315;
  7298. #endif
  7299. #if !defined(HAVE_FIPS) || \
  7300. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  7301. /* w/ FIPS v1 (cert 2425) wc_AesInit just returns 0 always as it's not
  7302. * supported with that FIPS version */
  7303. ret = wc_AesInit(NULL, HEAP_HINT, devId);
  7304. if (ret != BAD_FUNC_ARG)
  7305. ERROR_OUT(-5300, out);
  7306. #endif
  7307. ret = wc_AesInit(aes, HEAP_HINT, devId);
  7308. /* 0 check OK for FIPSv1 */
  7309. if (ret != 0)
  7310. ERROR_OUT(-5301, out);
  7311. #ifndef HAVE_FIPS
  7312. /* Parameter Validation testing. */
  7313. ret = wc_AesGetKeySize(NULL, NULL);
  7314. if (ret != BAD_FUNC_ARG)
  7315. ERROR_OUT(-5302, out);
  7316. ret = wc_AesGetKeySize(aes, NULL);
  7317. if (ret != BAD_FUNC_ARG)
  7318. ERROR_OUT(-5303, out);
  7319. ret = wc_AesGetKeySize(NULL, &keySize);
  7320. if (ret != BAD_FUNC_ARG)
  7321. ERROR_OUT(-5304, out);
  7322. /* Crashes in FIPS */
  7323. ret = wc_AesSetKey(NULL, key16, sizeof(key16), iv, AES_ENCRYPTION);
  7324. if (ret != BAD_FUNC_ARG)
  7325. ERROR_OUT(-5305, out);
  7326. #endif
  7327. /* NULL IV indicates to use all zeros IV. */
  7328. ret = wc_AesSetKey(aes, key16, sizeof(key16), NULL, AES_ENCRYPTION);
  7329. #ifdef WOLFSSL_AES_128
  7330. if (ret != 0)
  7331. #else
  7332. if (ret != BAD_FUNC_ARG)
  7333. #endif
  7334. ERROR_OUT(-5306, out);
  7335. ret = wc_AesSetKey(aes, key32, sizeof(key32) - 1, iv, AES_ENCRYPTION);
  7336. if (ret != BAD_FUNC_ARG)
  7337. ERROR_OUT(-5307, out);
  7338. /* CryptoCell handles rounds internally */
  7339. #if !defined(HAVE_FIPS) && !defined(WOLFSSL_CRYPTOCELL)
  7340. /* PSA don't use aes->rounds */
  7341. #if !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_AES)
  7342. /* Force invalid rounds */
  7343. aes->rounds = 16;
  7344. ret = wc_AesGetKeySize(aes, &keySize);
  7345. if (ret != BAD_FUNC_ARG)
  7346. ERROR_OUT(-5308, out);
  7347. #endif
  7348. #endif
  7349. ret = wc_AesSetKey(aes, key16, sizeof(key16), iv, AES_ENCRYPTION);
  7350. #ifdef WOLFSSL_AES_128
  7351. if (ret != 0)
  7352. #else
  7353. if (ret != BAD_FUNC_ARG)
  7354. #endif
  7355. ERROR_OUT(-5309, out);
  7356. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_128)
  7357. ret = wc_AesGetKeySize(aes, &keySize);
  7358. if (ret != 0 || keySize != sizeof(key16))
  7359. ERROR_OUT(-5310, out);
  7360. #endif
  7361. #ifndef WOLFSSL_CRYPTOCELL
  7362. /* Cryptocell only supports AES-128 key size */
  7363. ret = wc_AesSetKey(aes, key24, sizeof(key24), iv, AES_ENCRYPTION);
  7364. #ifdef WOLFSSL_AES_192
  7365. if (ret != 0)
  7366. #else
  7367. if (ret != BAD_FUNC_ARG)
  7368. #endif
  7369. ERROR_OUT(-5311, out);
  7370. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_192)
  7371. ret = wc_AesGetKeySize(aes, &keySize);
  7372. if (ret != 0 || keySize != sizeof(key24))
  7373. ERROR_OUT(-5312, out);
  7374. #endif
  7375. ret = wc_AesSetKey(aes, key32, sizeof(key32), iv, AES_ENCRYPTION);
  7376. #ifdef WOLFSSL_AES_256
  7377. if (ret != 0)
  7378. #else
  7379. if (ret != BAD_FUNC_ARG)
  7380. #endif
  7381. ERROR_OUT(-5313, out);
  7382. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_256)
  7383. ret = wc_AesGetKeySize(aes, &keySize);
  7384. if (ret != 0 || keySize != sizeof(key32))
  7385. ERROR_OUT(-5314, out);
  7386. #endif
  7387. #endif /* !WOLFSSL_CRYPTOCELL */
  7388. ret = 0; /* success */
  7389. out:
  7390. wc_AesFree(aes);
  7391. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7392. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7393. #endif
  7394. return ret;
  7395. }
  7396. #if defined(WOLFSSL_AES_XTS)
  7397. /* test vectors from http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html */
  7398. #ifdef WOLFSSL_AES_128
  7399. static int aes_xts_128_test(void)
  7400. {
  7401. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7402. XtsAes *aes = NULL;
  7403. #else
  7404. XtsAes aes[1];
  7405. #endif
  7406. int aes_inited = 0;
  7407. int ret = 0;
  7408. unsigned char buf[AES_BLOCK_SIZE * 2 + 8];
  7409. unsigned char cipher[AES_BLOCK_SIZE * 2 + 8];
  7410. /* 128 key tests */
  7411. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7412. 0xa1, 0xb9, 0x0c, 0xba, 0x3f, 0x06, 0xac, 0x35,
  7413. 0x3b, 0x2c, 0x34, 0x38, 0x76, 0x08, 0x17, 0x62,
  7414. 0x09, 0x09, 0x23, 0x02, 0x6e, 0x91, 0x77, 0x18,
  7415. 0x15, 0xf2, 0x9d, 0xab, 0x01, 0x93, 0x2f, 0x2f
  7416. };
  7417. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  7418. 0x4f, 0xae, 0xf7, 0x11, 0x7c, 0xda, 0x59, 0xc6,
  7419. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  7420. };
  7421. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7422. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  7423. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c
  7424. };
  7425. /* plain text test of partial block is not from NIST test vector list */
  7426. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  7427. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  7428. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  7429. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  7430. };
  7431. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7432. 0x77, 0x8a, 0xe8, 0xb4, 0x3c, 0xb9, 0x8d, 0x5a,
  7433. 0x82, 0x50, 0x81, 0xd5, 0xbe, 0x47, 0x1c, 0x63
  7434. };
  7435. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  7436. 0x39, 0x25, 0x79, 0x05, 0xdf, 0xcc, 0x77, 0x76,
  7437. 0x6c, 0x87, 0x0a, 0x80, 0x6a, 0x60, 0xe3, 0xc0,
  7438. 0x93, 0xd1, 0x2a, 0xcf, 0xcb, 0x51, 0x42, 0xfa,
  7439. 0x09, 0x69, 0x89, 0x62, 0x5b, 0x60, 0xdb, 0x16
  7440. };
  7441. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  7442. 0x5c, 0xf7, 0x9d, 0xb6, 0xc5, 0xcd, 0x99, 0x1a,
  7443. 0x1c, 0x78, 0x81, 0x42, 0x24, 0x95, 0x1e, 0x84
  7444. };
  7445. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  7446. 0xbd, 0xc5, 0x46, 0x8f, 0xbc, 0x8d, 0x50, 0xa1,
  7447. 0x0d, 0x1c, 0x85, 0x7f, 0x79, 0x1c, 0x5c, 0xba,
  7448. 0xb3, 0x81, 0x0d, 0x0d, 0x73, 0xcf, 0x8f, 0x20,
  7449. 0x46, 0xb1, 0xd1, 0x9e, 0x7d, 0x5d, 0x8a, 0x56
  7450. };
  7451. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  7452. 0xd6, 0xbe, 0x04, 0x6d, 0x41, 0xf2, 0x3b, 0x5e,
  7453. 0xd7, 0x0b, 0x6b, 0x3d, 0x5c, 0x8e, 0x66, 0x23,
  7454. 0x2b, 0xe6, 0xb8, 0x07, 0xd4, 0xdc, 0xc6, 0x0e,
  7455. 0xff, 0x8d, 0xbc, 0x1d, 0x9f, 0x7f, 0xc8, 0x22
  7456. };
  7457. #if !defined(HAVE_FIPS) || FIPS_VERSION_GE(5,3)
  7458. WOLFSSL_SMALL_STACK_STATIC unsigned char k3[] = {
  7459. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  7460. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  7461. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  7462. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  7463. };
  7464. WOLFSSL_SMALL_STACK_STATIC unsigned char i3[] = {
  7465. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  7466. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  7467. };
  7468. WOLFSSL_SMALL_STACK_STATIC unsigned char p3[] = {
  7469. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  7470. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  7471. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  7472. 0x20, 0xff, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  7473. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20
  7474. };
  7475. WOLFSSL_SMALL_STACK_STATIC unsigned char c3[] = {
  7476. 0xA2, 0x07, 0x47, 0x76, 0x3F, 0xEC, 0x0C, 0x23,
  7477. 0x1B, 0xD0, 0xBD, 0x46, 0x9A, 0x27, 0x38, 0x12,
  7478. 0x95, 0x02, 0x3D, 0x5D, 0xC6, 0x94, 0x51, 0x36,
  7479. 0xA0, 0x85, 0xD2, 0x69, 0x6E, 0x87, 0x0A, 0xBF,
  7480. 0xB5, 0x5A, 0xDD, 0xCB, 0x80, 0xE0, 0xFC, 0xCD
  7481. };
  7482. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  7483. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7484. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7485. ERROR_OUT(-5417, out);
  7486. #endif
  7487. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7488. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7489. ret = EVP_test(EVP_aes_128_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  7490. if (ret != 0) {
  7491. printf("EVP_aes_128_xts failed!\n");
  7492. goto out;
  7493. }
  7494. #endif
  7495. XMEMSET(buf, 0, sizeof(buf));
  7496. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  7497. HEAP_HINT, devId) != 0)
  7498. ERROR_OUT(-5400, out);
  7499. else
  7500. aes_inited = 1;
  7501. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  7502. #if defined(WOLFSSL_ASYNC_CRYPT)
  7503. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7504. #endif
  7505. if (ret != 0)
  7506. ERROR_OUT(-5401, out);
  7507. if (XMEMCMP(c2, buf, sizeof(c2)))
  7508. ERROR_OUT(-5402, out);
  7509. XMEMSET(buf, 0, sizeof(buf));
  7510. wc_AesXtsFree(aes);
  7511. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7512. HEAP_HINT, devId) != 0)
  7513. ERROR_OUT(-5403, out);
  7514. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  7515. #if defined(WOLFSSL_ASYNC_CRYPT)
  7516. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7517. #endif
  7518. if (ret != 0)
  7519. ERROR_OUT(-5404, out);
  7520. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  7521. ERROR_OUT(-5405, out);
  7522. /* partial block encryption test */
  7523. XMEMSET(cipher, 0, sizeof(cipher));
  7524. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  7525. #if defined(WOLFSSL_ASYNC_CRYPT)
  7526. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7527. #endif
  7528. if (ret != 0)
  7529. ERROR_OUT(-5406, out);
  7530. wc_AesXtsFree(aes);
  7531. /* partial block decrypt test */
  7532. XMEMSET(buf, 0, sizeof(buf));
  7533. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7534. HEAP_HINT, devId) != 0)
  7535. ERROR_OUT(-5407, out);
  7536. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  7537. #if defined(WOLFSSL_ASYNC_CRYPT)
  7538. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7539. #endif
  7540. if (ret != 0)
  7541. ERROR_OUT(-5408, out);
  7542. if (XMEMCMP(pp, buf, sizeof(pp)))
  7543. ERROR_OUT(-5409, out);
  7544. /* NIST decrypt test vector */
  7545. XMEMSET(buf, 0, sizeof(buf));
  7546. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  7547. #if defined(WOLFSSL_ASYNC_CRYPT)
  7548. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7549. #endif
  7550. if (ret != 0)
  7551. ERROR_OUT(-5410, out);
  7552. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  7553. ERROR_OUT(-5411, out);
  7554. /* fail case with decrypting using wrong key */
  7555. XMEMSET(buf, 0, sizeof(buf));
  7556. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  7557. #if defined(WOLFSSL_ASYNC_CRYPT)
  7558. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7559. #endif
  7560. if (ret != 0)
  7561. ERROR_OUT(-5412, out);
  7562. if (XMEMCMP(p2, buf, sizeof(p2)) == 0) /* fail case with wrong key */
  7563. ERROR_OUT(-5413, out);
  7564. wc_AesXtsFree(aes);
  7565. /* set correct key and retest */
  7566. XMEMSET(buf, 0, sizeof(buf));
  7567. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  7568. HEAP_HINT, devId) != 0)
  7569. ERROR_OUT(-5414, out);
  7570. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  7571. #if defined(WOLFSSL_ASYNC_CRYPT)
  7572. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7573. #endif
  7574. if (ret != 0)
  7575. ERROR_OUT(-5415, out);
  7576. if (XMEMCMP(p2, buf, sizeof(p2)))
  7577. ERROR_OUT(-5416, out);
  7578. wc_AesXtsFree(aes);
  7579. #if !defined(HAVE_FIPS) || FIPS_VERSION_GE(5,3)
  7580. /* Test ciphertext stealing in-place. */
  7581. XMEMCPY(buf, p3, sizeof(p3));
  7582. if (wc_AesXtsSetKey(aes, k3, sizeof(k3), AES_ENCRYPTION,
  7583. HEAP_HINT, devId) != 0)
  7584. ERROR_OUT(-5417, out);
  7585. else
  7586. aes_inited = 1;
  7587. ret = wc_AesXtsEncrypt(aes, buf, buf, sizeof(p3), i3, sizeof(i3));
  7588. #if defined(WOLFSSL_ASYNC_CRYPT)
  7589. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7590. #endif
  7591. if (ret != 0)
  7592. ERROR_OUT(-5418, out);
  7593. if (XMEMCMP(c3, buf, sizeof(c3)))
  7594. ERROR_OUT(-5419, out);
  7595. wc_AesXtsFree(aes);
  7596. if (wc_AesXtsSetKey(aes, k3, sizeof(k3), AES_DECRYPTION,
  7597. HEAP_HINT, devId) != 0)
  7598. ERROR_OUT(-5420, out);
  7599. ret = wc_AesXtsDecrypt(aes, buf, buf, sizeof(c3), i3, sizeof(i3));
  7600. #if defined(WOLFSSL_ASYNC_CRYPT)
  7601. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7602. #endif
  7603. if (ret != 0)
  7604. ERROR_OUT(-5421, out);
  7605. if (XMEMCMP(p3, buf, sizeof(p3)))
  7606. ERROR_OUT(-5422, out);
  7607. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  7608. out:
  7609. if (aes_inited)
  7610. wc_AesXtsFree(aes);
  7611. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7612. if (aes)
  7613. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7614. #endif
  7615. return ret;
  7616. }
  7617. #endif /* WOLFSSL_AES_128 */
  7618. #ifdef WOLFSSL_AES_256
  7619. static int aes_xts_256_test(void)
  7620. {
  7621. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7622. XtsAes *aes = NULL;
  7623. #else
  7624. XtsAes aes[1];
  7625. #endif
  7626. int aes_inited = 0;
  7627. int ret = 0;
  7628. unsigned char buf[AES_BLOCK_SIZE * 3];
  7629. unsigned char cipher[AES_BLOCK_SIZE * 3];
  7630. /* 256 key tests */
  7631. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7632. 0x1e, 0xa6, 0x61, 0xc5, 0x8d, 0x94, 0x3a, 0x0e,
  7633. 0x48, 0x01, 0xe4, 0x2f, 0x4b, 0x09, 0x47, 0x14,
  7634. 0x9e, 0x7f, 0x9f, 0x8e, 0x3e, 0x68, 0xd0, 0xc7,
  7635. 0x50, 0x52, 0x10, 0xbd, 0x31, 0x1a, 0x0e, 0x7c,
  7636. 0xd6, 0xe1, 0x3f, 0xfd, 0xf2, 0x41, 0x8d, 0x8d,
  7637. 0x19, 0x11, 0xc0, 0x04, 0xcd, 0xa5, 0x8d, 0xa3,
  7638. 0xd6, 0x19, 0xb7, 0xe2, 0xb9, 0x14, 0x1e, 0x58,
  7639. 0x31, 0x8e, 0xea, 0x39, 0x2c, 0xf4, 0x1b, 0x08
  7640. };
  7641. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  7642. 0xad, 0xf8, 0xd9, 0x26, 0x27, 0x46, 0x4a, 0xd2,
  7643. 0xf0, 0x42, 0x8e, 0x84, 0xa9, 0xf8, 0x75, 0x64
  7644. };
  7645. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7646. 0x2e, 0xed, 0xea, 0x52, 0xcd, 0x82, 0x15, 0xe1,
  7647. 0xac, 0xc6, 0x47, 0xe8, 0x10, 0xbb, 0xc3, 0x64,
  7648. 0x2e, 0x87, 0x28, 0x7f, 0x8d, 0x2e, 0x57, 0xe3,
  7649. 0x6c, 0x0a, 0x24, 0xfb, 0xc1, 0x2a, 0x20, 0x2e
  7650. };
  7651. /* plain text test of partial block is not from NIST test vector list */
  7652. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  7653. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  7654. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  7655. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  7656. };
  7657. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7658. 0xcb, 0xaa, 0xd0, 0xe2, 0xf6, 0xce, 0xa3, 0xf5,
  7659. 0x0b, 0x37, 0xf9, 0x34, 0xd4, 0x6a, 0x9b, 0x13,
  7660. 0x0b, 0x9d, 0x54, 0xf0, 0x7e, 0x34, 0xf3, 0x6a,
  7661. 0xf7, 0x93, 0xe8, 0x6f, 0x73, 0xc6, 0xd7, 0xdb
  7662. };
  7663. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  7664. 0xad, 0x50, 0x4b, 0x85, 0xd7, 0x51, 0xbf, 0xba,
  7665. 0x69, 0x13, 0xb4, 0xcc, 0x79, 0xb6, 0x5a, 0x62,
  7666. 0xf7, 0xf3, 0x9d, 0x36, 0x0f, 0x35, 0xb5, 0xec,
  7667. 0x4a, 0x7e, 0x95, 0xbd, 0x9b, 0xa5, 0xf2, 0xec,
  7668. 0xc1, 0xd7, 0x7e, 0xa3, 0xc3, 0x74, 0xbd, 0x4b,
  7669. 0x13, 0x1b, 0x07, 0x83, 0x87, 0xdd, 0x55, 0x5a,
  7670. 0xb5, 0xb0, 0xc7, 0xe5, 0x2d, 0xb5, 0x06, 0x12,
  7671. 0xd2, 0xb5, 0x3a, 0xcb, 0x47, 0x8a, 0x53, 0xb4
  7672. };
  7673. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  7674. 0xe6, 0x42, 0x19, 0xed, 0xe0, 0xe1, 0xc2, 0xa0,
  7675. 0x0e, 0xf5, 0x58, 0x6a, 0xc4, 0x9b, 0xeb, 0x6f
  7676. };
  7677. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  7678. 0x24, 0xcb, 0x76, 0x22, 0x55, 0xb5, 0xa8, 0x00,
  7679. 0xf4, 0x6e, 0x80, 0x60, 0x56, 0x9e, 0x05, 0x53,
  7680. 0xbc, 0xfe, 0x86, 0x55, 0x3b, 0xca, 0xd5, 0x89,
  7681. 0xc7, 0x54, 0x1a, 0x73, 0xac, 0xc3, 0x9a, 0xbd,
  7682. 0x53, 0xc4, 0x07, 0x76, 0xd8, 0xe8, 0x22, 0x61,
  7683. 0x9e, 0xa9, 0xad, 0x77, 0xa0, 0x13, 0x4c, 0xfc
  7684. };
  7685. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  7686. 0xa3, 0xc6, 0xf3, 0xf3, 0x82, 0x79, 0x5b, 0x10,
  7687. 0x87, 0xd7, 0x02, 0x50, 0xdb, 0x2c, 0xd3, 0xb1,
  7688. 0xa1, 0x62, 0xa8, 0xb6, 0xdc, 0x12, 0x60, 0x61,
  7689. 0xc1, 0x0a, 0x84, 0xa5, 0x85, 0x3f, 0x3a, 0x89,
  7690. 0xe6, 0x6c, 0xdb, 0xb7, 0x9a, 0xb4, 0x28, 0x9b,
  7691. 0xc3, 0xea, 0xd8, 0x10, 0xe9, 0xc0, 0xaf, 0x92
  7692. };
  7693. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7694. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7695. ERROR_OUT(-5515, out);
  7696. #endif
  7697. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7698. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7699. ret = EVP_test(EVP_aes_256_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  7700. if (ret != 0) {
  7701. printf("EVP_aes_256_xts failed\n");
  7702. goto out;
  7703. }
  7704. #endif
  7705. XMEMSET(buf, 0, sizeof(buf));
  7706. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  7707. HEAP_HINT, devId) != 0)
  7708. ERROR_OUT(-5500, out);
  7709. else
  7710. aes_inited = 1;
  7711. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  7712. #if defined(WOLFSSL_ASYNC_CRYPT)
  7713. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7714. #endif
  7715. if (ret != 0)
  7716. ERROR_OUT(-5501, out);
  7717. if (XMEMCMP(c2, buf, sizeof(c2)))
  7718. ERROR_OUT(-5502, out);
  7719. wc_AesXtsFree(aes);
  7720. XMEMSET(buf, 0, sizeof(buf));
  7721. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7722. HEAP_HINT, devId) != 0)
  7723. ERROR_OUT(-5503, out);
  7724. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  7725. #if defined(WOLFSSL_ASYNC_CRYPT)
  7726. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7727. #endif
  7728. if (ret != 0)
  7729. ERROR_OUT(-5504, out);
  7730. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  7731. ERROR_OUT(-5505, out);
  7732. /* partial block encryption test */
  7733. XMEMSET(cipher, 0, sizeof(cipher));
  7734. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  7735. #if defined(WOLFSSL_ASYNC_CRYPT)
  7736. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7737. #endif
  7738. if (ret != 0)
  7739. ERROR_OUT(-5506, out);
  7740. wc_AesXtsFree(aes);
  7741. /* partial block decrypt test */
  7742. XMEMSET(buf, 0, sizeof(buf));
  7743. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7744. HEAP_HINT, devId) != 0)
  7745. ERROR_OUT(-5507, out);
  7746. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  7747. #if defined(WOLFSSL_ASYNC_CRYPT)
  7748. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7749. #endif
  7750. if (ret != 0)
  7751. ERROR_OUT(-5508, out);
  7752. if (XMEMCMP(pp, buf, sizeof(pp)))
  7753. ERROR_OUT(-5509, out);
  7754. /* NIST decrypt test vector */
  7755. XMEMSET(buf, 0, sizeof(buf));
  7756. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  7757. #if defined(WOLFSSL_ASYNC_CRYPT)
  7758. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7759. #endif
  7760. if (ret != 0)
  7761. ERROR_OUT(-5510, out);
  7762. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  7763. ERROR_OUT(-5511, out);
  7764. wc_AesXtsFree(aes);
  7765. XMEMSET(buf, 0, sizeof(buf));
  7766. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  7767. HEAP_HINT, devId) != 0)
  7768. ERROR_OUT(-5512, out);
  7769. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  7770. #if defined(WOLFSSL_ASYNC_CRYPT)
  7771. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7772. #endif
  7773. if (ret != 0)
  7774. ERROR_OUT(-5513, out);
  7775. if (XMEMCMP(p2, buf, sizeof(p2)))
  7776. ERROR_OUT(-5514, out);
  7777. out:
  7778. if (aes_inited)
  7779. wc_AesXtsFree(aes);
  7780. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7781. if (aes)
  7782. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7783. #endif
  7784. return ret;
  7785. }
  7786. #endif /* WOLFSSL_AES_256 */
  7787. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  7788. /* both 128 and 256 bit key test */
  7789. static int aes_xts_sector_test(void)
  7790. {
  7791. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7792. XtsAes *aes = NULL;
  7793. #else
  7794. XtsAes aes[1];
  7795. #endif
  7796. int aes_inited = 0;
  7797. int ret = 0;
  7798. unsigned char buf[AES_BLOCK_SIZE * 2];
  7799. /* 128 key tests */
  7800. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7801. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  7802. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  7803. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  7804. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  7805. };
  7806. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7807. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  7808. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  7809. };
  7810. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7811. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  7812. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  7813. };
  7814. word64 s1 = 141;
  7815. /* 256 key tests */
  7816. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  7817. 0xef, 0x01, 0x0c, 0xa1, 0xa3, 0x66, 0x3e, 0x32,
  7818. 0x53, 0x43, 0x49, 0xbc, 0x0b, 0xae, 0x62, 0x23,
  7819. 0x2a, 0x15, 0x73, 0x34, 0x85, 0x68, 0xfb, 0x9e,
  7820. 0xf4, 0x17, 0x68, 0xa7, 0x67, 0x4f, 0x50, 0x7a,
  7821. 0x72, 0x7f, 0x98, 0x75, 0x53, 0x97, 0xd0, 0xe0,
  7822. 0xaa, 0x32, 0xf8, 0x30, 0x33, 0x8c, 0xc7, 0xa9,
  7823. 0x26, 0xc7, 0x73, 0xf0, 0x9e, 0x57, 0xb3, 0x57,
  7824. 0xcd, 0x15, 0x6a, 0xfb, 0xca, 0x46, 0xe1, 0xa0
  7825. };
  7826. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  7827. 0xed, 0x98, 0xe0, 0x17, 0x70, 0xa8, 0x53, 0xb4,
  7828. 0x9d, 0xb9, 0xe6, 0xaa, 0xf8, 0x8f, 0x0a, 0x41,
  7829. 0xb9, 0xb5, 0x6e, 0x91, 0xa5, 0xa2, 0xb1, 0x1d,
  7830. 0x40, 0x52, 0x92, 0x54, 0xf5, 0x52, 0x3e, 0x75
  7831. };
  7832. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  7833. 0xca, 0x20, 0xc5, 0x5e, 0x8d, 0xc1, 0x49, 0x68,
  7834. 0x7d, 0x25, 0x41, 0xde, 0x39, 0xc3, 0xdf, 0x63,
  7835. 0x00, 0xbb, 0x5a, 0x16, 0x3c, 0x10, 0xce, 0xd3,
  7836. 0x66, 0x6b, 0x13, 0x57, 0xdb, 0x8b, 0xd3, 0x9d
  7837. };
  7838. word64 s2 = 187;
  7839. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7840. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7841. ERROR_OUT(-5612, out);
  7842. #endif
  7843. XMEMSET(buf, 0, sizeof(buf));
  7844. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7845. HEAP_HINT, devId) != 0)
  7846. ERROR_OUT(-5600, out);
  7847. else
  7848. aes_inited = 1;
  7849. ret = wc_AesXtsEncryptSector(aes, buf, p1, sizeof(p1), s1);
  7850. #if defined(WOLFSSL_ASYNC_CRYPT)
  7851. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7852. #endif
  7853. if (ret != 0)
  7854. ERROR_OUT(-5601, out);
  7855. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  7856. ERROR_OUT(-5602, out);
  7857. wc_AesXtsFree(aes);
  7858. /* decrypt test */
  7859. XMEMSET(buf, 0, sizeof(buf));
  7860. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7861. HEAP_HINT, devId) != 0)
  7862. ERROR_OUT(-5603, out);
  7863. ret = wc_AesXtsDecryptSector(aes, buf, c1, sizeof(c1), s1);
  7864. #if defined(WOLFSSL_ASYNC_CRYPT)
  7865. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7866. #endif
  7867. if (ret != 0)
  7868. ERROR_OUT(-5604, out);
  7869. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  7870. ERROR_OUT(-5605, out);
  7871. wc_AesXtsFree(aes);
  7872. /* 256 bit key tests */
  7873. XMEMSET(buf, 0, sizeof(buf));
  7874. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  7875. HEAP_HINT, devId) != 0)
  7876. ERROR_OUT(-5606, out);
  7877. ret = wc_AesXtsEncryptSector(aes, buf, p2, sizeof(p2), s2);
  7878. #if defined(WOLFSSL_ASYNC_CRYPT)
  7879. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7880. #endif
  7881. if (ret != 0)
  7882. ERROR_OUT(-5607, out);
  7883. if (XMEMCMP(c2, buf, sizeof(c2)))
  7884. ERROR_OUT(-5608, out);
  7885. wc_AesXtsFree(aes);
  7886. /* decrypt test */
  7887. XMEMSET(buf, 0, sizeof(buf));
  7888. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  7889. HEAP_HINT, devId) != 0)
  7890. ERROR_OUT(-5609, out);
  7891. ret = wc_AesXtsDecryptSector(aes, buf, c2, sizeof(c2), s2);
  7892. #if defined(WOLFSSL_ASYNC_CRYPT)
  7893. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7894. #endif
  7895. if (ret != 0)
  7896. ERROR_OUT(-5610, out);
  7897. if (XMEMCMP(p2, buf, sizeof(p2)))
  7898. ERROR_OUT(-5611, out);
  7899. out:
  7900. if (aes_inited)
  7901. wc_AesXtsFree(aes);
  7902. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7903. if (aes)
  7904. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7905. #endif
  7906. return ret;
  7907. }
  7908. #endif /* WOLFSSL_AES_128 && WOLFSSL_AES_256 */
  7909. #ifdef WOLFSSL_AES_128
  7910. /* testing of bad arguments */
  7911. static int aes_xts_args_test(void)
  7912. {
  7913. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7914. XtsAes *aes = NULL;
  7915. #else
  7916. XtsAes aes[1];
  7917. #endif
  7918. int aes_inited = 0;
  7919. int ret;
  7920. unsigned char buf[AES_BLOCK_SIZE * 2];
  7921. /* 128 key tests */
  7922. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7923. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  7924. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  7925. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  7926. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  7927. };
  7928. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7929. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  7930. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  7931. };
  7932. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7933. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  7934. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  7935. };
  7936. word64 s1 = 141;
  7937. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7938. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7939. ERROR_OUT(-5708, out);
  7940. #endif
  7941. if (wc_AesXtsSetKey(NULL, k1, sizeof(k1), AES_ENCRYPTION,
  7942. HEAP_HINT, devId) == 0)
  7943. ERROR_OUT(-5700, out);
  7944. if (wc_AesXtsSetKey(aes, NULL, sizeof(k1), AES_ENCRYPTION,
  7945. HEAP_HINT, devId) == 0)
  7946. ERROR_OUT(-5701, out);
  7947. /* encryption operations */
  7948. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7949. HEAP_HINT, devId) != 0)
  7950. ERROR_OUT(-5702, out);
  7951. else
  7952. aes_inited = 1;
  7953. ret = wc_AesXtsEncryptSector(NULL, buf, p1, sizeof(p1), s1);
  7954. #if defined(WOLFSSL_ASYNC_CRYPT)
  7955. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7956. #endif
  7957. if (ret == 0)
  7958. ERROR_OUT(-5703, out);
  7959. ret = wc_AesXtsEncryptSector(aes, NULL, p1, sizeof(p1), s1);
  7960. #if defined(WOLFSSL_ASYNC_CRYPT)
  7961. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7962. #endif
  7963. if (ret == 0)
  7964. ERROR_OUT(-5704, out);
  7965. wc_AesXtsFree(aes);
  7966. /* decryption operations */
  7967. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7968. HEAP_HINT, devId) != 0)
  7969. ERROR_OUT(-5705, out);
  7970. ret = wc_AesXtsDecryptSector(NULL, buf, c1, sizeof(c1), s1);
  7971. #if defined(WOLFSSL_ASYNC_CRYPT)
  7972. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7973. #endif
  7974. if (ret == 0)
  7975. ERROR_OUT(-5706, out);
  7976. ret = wc_AesXtsDecryptSector(aes, NULL, c1, sizeof(c1), s1);
  7977. #if defined(WOLFSSL_ASYNC_CRYPT)
  7978. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7979. #endif
  7980. if (ret == 0)
  7981. ERROR_OUT(-5707, out);
  7982. ret = 0;
  7983. out:
  7984. if (aes_inited)
  7985. wc_AesXtsFree(aes);
  7986. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7987. if (aes)
  7988. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7989. #endif
  7990. return ret;
  7991. }
  7992. #endif /* WOLFSSL_AES_128 */
  7993. #endif /* WOLFSSL_AES_XTS */
  7994. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  7995. static int aes_cbc_test(void)
  7996. {
  7997. byte cipher[AES_BLOCK_SIZE];
  7998. byte plain[AES_BLOCK_SIZE];
  7999. int ret;
  8000. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  8001. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  8002. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  8003. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  8004. };
  8005. byte key[] = "0123456789abcdef "; /* align */
  8006. byte iv[] = "1234567890abcdef "; /* align */
  8007. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  8008. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  8009. /* Parameter Validation testing. */
  8010. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key, 17, NULL);
  8011. if (ret != BAD_FUNC_ARG)
  8012. return -5800;
  8013. #ifdef HAVE_AES_DECRYPT
  8014. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key, 17, NULL);
  8015. if (ret != BAD_FUNC_ARG)
  8016. return -5801;
  8017. #endif
  8018. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key,
  8019. AES_BLOCK_SIZE, iv);
  8020. if (ret != 0)
  8021. return -5802;
  8022. #ifdef HAVE_AES_DECRYPT
  8023. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key,
  8024. AES_BLOCK_SIZE, iv);
  8025. if (ret != 0)
  8026. return -5803;
  8027. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE) != 0)
  8028. return -5804;
  8029. #endif /* HAVE_AES_DECRYPT */
  8030. (void)plain;
  8031. return 0;
  8032. }
  8033. #endif
  8034. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  8035. static int aesecb_test(void)
  8036. {
  8037. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8038. Aes *enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8039. #else
  8040. Aes enc[1];
  8041. #endif
  8042. byte cipher[AES_BLOCK_SIZE * 4];
  8043. #ifdef HAVE_AES_DECRYPT
  8044. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8045. Aes *dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8046. #else
  8047. Aes dec[1];
  8048. #endif
  8049. byte plain [AES_BLOCK_SIZE * 4];
  8050. #endif /* HAVE_AES_DECRYPT */
  8051. int ret = 0;
  8052. #if defined(WOLFSSL_AES_256)
  8053. {
  8054. WOLFSSL_SMALL_STACK_STATIC const byte niPlain[] =
  8055. {
  8056. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8057. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  8058. };
  8059. WOLFSSL_SMALL_STACK_STATIC const byte niCipher[] =
  8060. {
  8061. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  8062. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  8063. };
  8064. WOLFSSL_SMALL_STACK_STATIC const byte niKey[] =
  8065. {
  8066. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  8067. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  8068. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  8069. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  8070. };
  8071. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  8072. ERROR_OUT(-5900, out);
  8073. #if defined(HAVE_AES_DECRYPT)
  8074. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  8075. ERROR_OUT(-5901, out);
  8076. #endif
  8077. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  8078. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  8079. if (ret != 0)
  8080. ERROR_OUT(-5923, out);
  8081. if (wc_AesEcbEncrypt(enc, cipher, niPlain, AES_BLOCK_SIZE) != 0)
  8082. ERROR_OUT(-5924, out);
  8083. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  8084. ERROR_OUT(-5925, out);
  8085. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  8086. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  8087. if (ret != 0)
  8088. ERROR_OUT(-5926, out);
  8089. if (wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE) != 0)
  8090. ERROR_OUT(-5927, out);
  8091. wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE);
  8092. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  8093. ERROR_OUT(-5928, out);
  8094. }
  8095. wc_AesFree(enc);
  8096. #ifdef HAVE_AES_DECRYPT
  8097. wc_AesFree(dec);
  8098. #endif
  8099. out:
  8100. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8101. if (enc)
  8102. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8103. #ifdef HAVE_AES_DECRYPT
  8104. if (dec)
  8105. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8106. #endif
  8107. #endif
  8108. #endif /* WOLFSSL_AES_256 */
  8109. return ret;
  8110. }
  8111. #endif /* HAVE_AES_ECB */
  8112. #ifdef WOLFSSL_AES_COUNTER
  8113. static int aesctr_test(Aes* enc, Aes* dec, byte* cipher, byte* plain)
  8114. {
  8115. int ret;
  8116. /* test vectors from "Recommendation for Block Cipher Modes of
  8117. * Operation" NIST Special Publication 800-38A */
  8118. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  8119. {
  8120. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  8121. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  8122. };
  8123. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  8124. {
  8125. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8126. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  8127. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  8128. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  8129. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  8130. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  8131. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  8132. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  8133. };
  8134. #ifdef WOLFSSL_ARMASM
  8135. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap32[] =
  8136. {
  8137. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,
  8138. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  8139. };
  8140. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap32_2[] =
  8141. {
  8142. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8143. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xfe
  8144. };
  8145. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap64[] =
  8146. {
  8147. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8148. 0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  8149. };
  8150. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap64_2[] =
  8151. {
  8152. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0,
  8153. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  8154. };
  8155. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap96[] =
  8156. {
  8157. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8158. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff
  8159. };
  8160. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap96_2[] =
  8161. {
  8162. 0xff,0xff,0xff,0xf0,0xff,0xff,0xff,0xff,
  8163. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  8164. };
  8165. #endif
  8166. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap128[] =
  8167. {
  8168. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8169. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  8170. };
  8171. #ifdef WOLFSSL_ARMASM
  8172. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap128_2[] =
  8173. {
  8174. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8175. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  8176. };
  8177. #endif
  8178. #ifdef WOLFSSL_AES_128
  8179. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  8180. {
  8181. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  8182. 0xc2
  8183. };
  8184. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Key[] =
  8185. {
  8186. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  8187. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  8188. };
  8189. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Cipher[] =
  8190. {
  8191. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  8192. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  8193. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  8194. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  8195. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  8196. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  8197. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  8198. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  8199. };
  8200. #ifdef WOLFSSL_ARMASM
  8201. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32Cipher[] =
  8202. {
  8203. 0xb3,0x8b,0x58,0xbc,0xce,0xf4,0x71,0x78,
  8204. 0xf6,0x7c,0xdb,0xb4,0x27,0x2b,0x0a,0xbf,
  8205. 0x7e,0xad,0xea,0x5c,0xd1
  8206. };
  8207. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32CipherLong[] =
  8208. {
  8209. 0xb3,0x8b,0x58,0xbc,0xce,0xf4,0x71,0x78,
  8210. 0xf6,0x7c,0xdb,0xb4,0x27,0x2b,0x0a,0xbf,
  8211. 0x7e,0xad,0xea,0x5c,0xd1,0xb7,0x98,0xf0,
  8212. 0x22,0x20,0xfe,0x67,0xb0,0x02,0x23,0x50
  8213. };
  8214. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32_2CipherLong[] =
  8215. {
  8216. 0x6e,0xa1,0x27,0x4d,0xea,0x20,0x5f,0x39,
  8217. 0x68,0xc8,0xb6,0x78,0xde,0xfc,0x53,0x5c,
  8218. 0x90,0xc8,0xf6,0xc6,0xfa,0xe0,0x7b,0x09,
  8219. 0x7c,0xf8,0x9c,0x6a,0x5a,0xa5,0x17,0x7f,
  8220. 0x03,0x92,0x5f,0x4e,0x85,0xea,0x26,0xc9,
  8221. 0x5a,0xc2,0x74,0xe2,0xbf,0xe4,0x1b,0xd4
  8222. };
  8223. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64Cipher[] =
  8224. {
  8225. 0xdd,0x17,0x10,0x7c,0x45,0x04,0xac,0x43,
  8226. 0xef,0xa8,0xcc,0x32,0x34,0x87,0x88,0xd7,
  8227. 0xae,0x74,0x94,0x72,0x8e
  8228. };
  8229. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64CipherLong[] =
  8230. {
  8231. 0xdd,0x17,0x10,0x7c,0x45,0x04,0xac,0x43,
  8232. 0xef,0xa8,0xcc,0x32,0x34,0x87,0x88,0xd7,
  8233. 0xae,0x74,0x94,0x72,0x8e,0xd0,0x71,0xc0,
  8234. 0x89,0x8a,0xa1,0xb0,0x29,0xa0,0x10,0x9e
  8235. };
  8236. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64_2CipherLong[] =
  8237. {
  8238. 0x3f,0xe7,0xd5,0xf3,0xfa,0x09,0xfe,0x40,
  8239. 0xa6,0xa1,0x32,0x8b,0x57,0x12,0xb9,0xfa,
  8240. 0xf2,0x2d,0xe4,0x3c,0x66,0x1d,0x0a,0x8e,
  8241. 0x46,0xf8,0x2e,0x33,0xce,0x8d,0x4e,0x3b,
  8242. 0x17,0x67,0x9e,0x9f,0x76,0x9e,0xc2,0x99,
  8243. 0xd5,0xd4,0x71,0xed,0xb4,0x33,0xb2,0xcd
  8244. };
  8245. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96Cipher[] =
  8246. {
  8247. 0x55,0x24,0xc2,0x73,0xca,0xa3,0x48,0x03,
  8248. 0x0b,0x72,0x8d,0xd7,0x6c,0x99,0x8e,0x04,
  8249. 0x9d,0x77,0xc9,0x5f,0x38
  8250. };
  8251. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96CipherLong[] =
  8252. {
  8253. 0x55,0x24,0xc2,0x73,0xca,0xa3,0x48,0x03,
  8254. 0x0b,0x72,0x8d,0xd7,0x6c,0x99,0x8e,0x04,
  8255. 0x9d,0x77,0xc9,0x5f,0x38,0xb5,0x6e,0x44,
  8256. 0x21,0x8e,0xda,0x57,0xe0,0x41,0xc7,0x6a
  8257. };
  8258. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96_2CipherLong[] =
  8259. {
  8260. 0xc8,0x81,0x1a,0xbe,0xc7,0x5b,0x93,0x6f,
  8261. 0xe6,0x52,0xe4,0xb1,0x2d,0x1c,0x39,0xbc,
  8262. 0xeb,0x82,0x27,0x0a,0x7e,0xa5,0x0e,0x2d,
  8263. 0x32,0xda,0xbe,0x10,0x7a,0x10,0xcc,0xd3,
  8264. 0x6f,0xc6,0x83,0x28,0x05,0x57,0x8a,0x24,
  8265. 0x44,0x76,0x17,0x81,0xb9,0x5c,0x94,0x81
  8266. };
  8267. #endif
  8268. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128Cipher[] =
  8269. {
  8270. 0xe1,0x33,0x38,0xe3,0x6c,0xb7,0x19,0x62,
  8271. 0xe0,0x0d,0x02,0x0b,0x4c,0xed,0xbd,0x86,
  8272. 0xd3,0xda,0xe1,0x5b,0x04
  8273. };
  8274. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128CipherLong[] =
  8275. {
  8276. 0xe1,0x33,0x38,0xe3,0x6c,0xb7,0x19,0x62,
  8277. 0xe0,0x0d,0x02,0x0b,0x4c,0xed,0xbd,0x86,
  8278. 0xd3,0xda,0xe1,0x5b,0x04,0xbb,0x35,0x2f,
  8279. 0xa0,0xf5,0x9f,0xeb,0xfc,0xb4,0xda,0x3e
  8280. };
  8281. #ifdef WOLFSSL_ARMASM
  8282. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128_2CipherLong[] =
  8283. {
  8284. 0xba,0x76,0xaa,0x54,0xd5,0xb5,0x60,0x67,
  8285. 0xc1,0xa7,0x90,0x3b,0x3f,0xdd,0xfa,0x89,
  8286. 0x24,0xdf,0x0c,0x56,0x5c,0xf4,0x2a,0x68,
  8287. 0x97,0x87,0x13,0xb6,0x7a,0xd1,0x24,0xfd,
  8288. 0x4d,0x3f,0x77,0x4a,0xb9,0xe4,0x7d,0xa2,
  8289. 0xdb,0xb9,0x31,0x5e,0xa3,0x11,0x06,0x80
  8290. };
  8291. #endif
  8292. #endif /* WOLFSSL_AES_128 */
  8293. #ifdef WOLFSSL_AES_192
  8294. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  8295. {
  8296. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  8297. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  8298. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  8299. };
  8300. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  8301. {
  8302. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  8303. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b,
  8304. 0x09,0x03,0x39,0xec,0x0a,0xa6,0xfa,0xef,
  8305. 0xd5,0xcc,0xc2,0xc6,0xf4,0xce,0x8e,0x94,
  8306. 0x1e,0x36,0xb2,0x6b,0xd1,0xeb,0xc6,0x70,
  8307. 0xd1,0xbd,0x1d,0x66,0x56,0x20,0xab,0xf7,
  8308. 0x4f,0x78,0xa7,0xf6,0xd2,0x98,0x09,0x58,
  8309. 0x5a,0x97,0xda,0xec,0x58,0xc6,0xb0,0x50
  8310. };
  8311. #ifdef WOLFSSL_ARMASM
  8312. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32Cipher[] =
  8313. {
  8314. 0x28,0xaa,0xfa,0x90,0x72,0x74,0x86,0xaf,
  8315. 0x72,0x73,0x35,0x17,0x70,0x4e,0x7d,0xca,
  8316. 0x0c,0x33,0x97,0x06,0xc0
  8317. };
  8318. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32CipherLong[] =
  8319. {
  8320. 0x28,0xaa,0xfa,0x90,0x72,0x74,0x86,0xaf,
  8321. 0x72,0x73,0x35,0x17,0x70,0x4e,0x7d,0xca,
  8322. 0x0c,0x33,0x97,0x06,0xc0,0xbe,0x83,0x87,
  8323. 0xdd,0xd3,0xff,0xd8,0xe4,0x6a,0x5b,0x84
  8324. };
  8325. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32_2CipherLong[] =
  8326. {
  8327. 0xf5,0x00,0xa2,0x91,0x54,0xa3,0x76,0xa2,
  8328. 0xdd,0xad,0x16,0x89,0xe5,0xf0,0x1d,0x40,
  8329. 0x84,0xcd,0x74,0x84,0xcb,0x8b,0x9e,0x29,
  8330. 0xa9,0x8a,0x12,0x65,0xa0,0x79,0x5e,0xce,
  8331. 0xd9,0x50,0x65,0x21,0x86,0xb0,0x85,0x0d,
  8332. 0x98,0x2d,0x9a,0x5a,0x11,0xbe,0xa5,0x7f
  8333. };
  8334. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64Cipher[] =
  8335. {
  8336. 0xfe,0x39,0x27,0x97,0xac,0xe5,0xb8,0x74,
  8337. 0xb9,0x8c,0xbf,0x58,0x71,0xa4,0x80,0x33,
  8338. 0x3d,0xf7,0xb4,0xfd,0x8c
  8339. };
  8340. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64CipherLong[] =
  8341. {
  8342. 0xfe,0x39,0x27,0x97,0xac,0xe5,0xb8,0x74,
  8343. 0xb9,0x8c,0xbf,0x58,0x71,0xa4,0x80,0x33,
  8344. 0x3d,0xf7,0xb4,0xfd,0x8c,0x55,0x47,0x10,
  8345. 0xd5,0x91,0x35,0xbe,0xd8,0x0d,0xa5,0x9e
  8346. };
  8347. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64_2CipherLong[] =
  8348. {
  8349. 0x59,0xf1,0xed,0x70,0x62,0x42,0xa8,0x06,
  8350. 0x07,0x36,0xe1,0xc5,0x04,0x79,0xc3,0x9b,
  8351. 0xd1,0x14,0x5c,0xcc,0x6f,0x81,0x5f,0x2f,
  8352. 0xa0,0xde,0xcf,0x61,0x55,0x18,0x7a,0xac,
  8353. 0xb0,0x59,0x37,0x90,0x53,0xb3,0x00,0x88,
  8354. 0xb4,0x49,0x90,0x7b,0x96,0xcd,0xcc,0xc3
  8355. };
  8356. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96Cipher[] =
  8357. {
  8358. 0x41,0x21,0x40,0x31,0xfb,0xc8,0xad,0x23,
  8359. 0xde,0x00,0x03,0xd8,0x96,0x45,0xc7,0xb5,
  8360. 0x47,0xb5,0xf3,0x30,0x3b
  8361. };
  8362. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96CipherLong[] =
  8363. {
  8364. 0x41,0x21,0x40,0x31,0xfb,0xc8,0xad,0x23,
  8365. 0xde,0x00,0x03,0xd8,0x96,0x45,0xc7,0xb5,
  8366. 0x47,0xb5,0xf3,0x30,0x3b,0xef,0xcd,0x80,
  8367. 0xe3,0x61,0x34,0xef,0x4e,0x1b,0x79,0xc1
  8368. };
  8369. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96_2CipherLong[] =
  8370. {
  8371. 0x3c,0xb2,0xff,0xc0,0x24,0xe1,0xf5,0xc4,
  8372. 0x0f,0xd1,0x0a,0x1b,0xbe,0x1f,0x23,0xa1,
  8373. 0x8e,0xbf,0x2b,0x96,0xb6,0x37,0xc8,0x25,
  8374. 0x06,0x90,0xe2,0xca,0x71,0x24,0x52,0x95,
  8375. 0xaa,0x8c,0x80,0xdf,0xb7,0xd7,0x30,0xb0,
  8376. 0xcc,0x06,0x4f,0x28,0xa2,0x74,0x27,0xf8
  8377. };
  8378. #endif
  8379. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128Cipher[] =
  8380. {
  8381. 0x5c,0xc3,0x8f,0xab,0x30,0xb6,0xac,0x67,
  8382. 0xdc,0xc2,0x1e,0x7b,0x01,0x2e,0xcf,0x98,
  8383. 0x8c,0x68,0xa7,0xd9,0x57
  8384. };
  8385. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128CipherLong[] =
  8386. {
  8387. 0x5c,0xc3,0x8f,0xab,0x30,0xb6,0xac,0x67,
  8388. 0xdc,0xc2,0x1e,0x7b,0x01,0x2e,0xcf,0x98,
  8389. 0x8c,0x68,0xa7,0xd9,0x57,0xab,0x09,0x0f,
  8390. 0x01,0xc4,0x4e,0x62,0xaf,0xc2,0xdf,0x1a
  8391. };
  8392. #ifdef WOLFSSL_ARMASM
  8393. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128_2CipherLong[] =
  8394. {
  8395. 0x88,0x0a,0x26,0x4e,0xa8,0x26,0x21,0xe0,
  8396. 0xfc,0xbc,0x63,0xdc,0xd9,0x60,0x52,0xb2,
  8397. 0x99,0x2f,0xbb,0x1e,0x00,0xf5,0x9f,0x6d,
  8398. 0xab,0x48,0x0f,0xc6,0x37,0x12,0x56,0xe3,
  8399. 0x12,0x8d,0x31,0xc8,0xea,0xf4,0x41,0x82,
  8400. 0x7a,0x88,0xe0,0xd7,0xf0,0x67,0x03,0xa4
  8401. };
  8402. #endif
  8403. #endif
  8404. #ifdef WOLFSSL_AES_256
  8405. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  8406. {
  8407. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  8408. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  8409. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  8410. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  8411. };
  8412. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  8413. {
  8414. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  8415. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28,
  8416. 0xf4,0x43,0xe3,0xca,0x4d,0x62,0xb5,0x9a,
  8417. 0xca,0x84,0xe9,0x90,0xca,0xca,0xf5,0xc5,
  8418. 0x2b,0x09,0x30,0xda,0xa2,0x3d,0xe9,0x4c,
  8419. 0xe8,0x70,0x17,0xba,0x2d,0x84,0x98,0x8d,
  8420. 0xdf,0xc9,0xc5,0x8d,0xb6,0x7a,0xad,0xa6,
  8421. 0x13,0xc2,0xdd,0x08,0x45,0x79,0x41,0xa6
  8422. };
  8423. #ifdef WOLFSSL_ARMASM
  8424. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32Cipher[] =
  8425. {
  8426. 0xb0,0xa8,0xc0,0x65,0x85,0x20,0x0d,0x5c,
  8427. 0x25,0xcf,0xe7,0x58,0x63,0xc8,0xd4,0xea,
  8428. 0xa2,0x13,0x47,0x74,0xda
  8429. };
  8430. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32CipherLong[] =
  8431. {
  8432. 0xb0,0xa8,0xc0,0x65,0x85,0x20,0x0d,0x5c,
  8433. 0x25,0xcf,0xe7,0x58,0x63,0xc8,0xd4,0xea,
  8434. 0xa2,0x13,0x47,0x74,0xda,0x89,0x77,0x40,
  8435. 0x28,0x9c,0xe8,0x19,0x26,0x32,0xd8,0x1f
  8436. };
  8437. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32_2CipherLong[] =
  8438. {
  8439. 0xf6,0xd9,0x22,0xc6,0x80,0x29,0xaf,0x14,
  8440. 0x54,0x6c,0x0a,0xce,0x42,0xea,0x3c,0xa1,
  8441. 0x7c,0xeb,0x36,0x0d,0x8e,0xd7,0x8c,0x59,
  8442. 0xa8,0x09,0x9f,0x9e,0xba,0x5b,0x95,0xfa,
  8443. 0x26,0x8c,0x37,0x59,0xf8,0xae,0x8e,0xaa,
  8444. 0x4d,0xe4,0x1c,0xfe,0x51,0xc7,0xb7,0xcc
  8445. };
  8446. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64Cipher[] =
  8447. {
  8448. 0x74,0x1a,0x52,0x41,0x76,0xb4,0x11,0x8f,
  8449. 0xfd,0x57,0x31,0xfd,0x3d,0x76,0x8f,0x07,
  8450. 0xd4,0x94,0x4c,0xcd,0x4d
  8451. };
  8452. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64CipherLong[] =
  8453. {
  8454. 0x74,0x1a,0x52,0x41,0x76,0xb4,0x11,0x8f,
  8455. 0xfd,0x57,0x31,0xfd,0x3d,0x76,0x8f,0x07,
  8456. 0xd4,0x94,0x4c,0xcd,0x4d,0x47,0x5a,0x92,
  8457. 0x26,0x49,0x81,0x7a,0xda,0x36,0x27,0x01
  8458. };
  8459. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64_2CipherLong[] =
  8460. {
  8461. 0xf7,0x9c,0xbf,0xf6,0xa2,0xaa,0x8a,0x0a,
  8462. 0x63,0x8a,0x20,0x2f,0x12,0xf1,0x8e,0x49,
  8463. 0x30,0xc0,0x8d,0x5c,0x5f,0x8b,0xbc,0x16,
  8464. 0xdd,0x71,0xee,0x13,0x14,0x7b,0xe1,0x25,
  8465. 0xcb,0x87,0x8a,0xc6,0xdc,0x1d,0x54,0x7a,
  8466. 0xe1,0xe4,0x6f,0x0d,0x95,0x1b,0xd1,0x8b
  8467. };
  8468. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96Cipher[] =
  8469. {
  8470. 0xb9,0x07,0x02,0xb8,0xbe,0x94,0xbf,0x53,
  8471. 0xdf,0x83,0x8e,0x23,0x8c,0x67,0x0c,0x81,
  8472. 0xb8,0x69,0xa1,0x48,0x45
  8473. };
  8474. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96CipherLong[] =
  8475. {
  8476. 0xb9,0x07,0x02,0xb8,0xbe,0x94,0xbf,0x53,
  8477. 0xdf,0x83,0x8e,0x23,0x8c,0x67,0x0c,0x81,
  8478. 0xb8,0x69,0xa1,0x48,0x45,0xf1,0xc6,0x27,
  8479. 0x36,0xa8,0xb2,0x4b,0x0e,0x62,0x6b,0x72
  8480. };
  8481. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96_2CipherLong[] =
  8482. {
  8483. 0xd5,0x56,0x73,0xaa,0xb8,0xe4,0x06,0xf6,
  8484. 0x83,0x45,0x3a,0xb4,0xb9,0x63,0xec,0xad,
  8485. 0x73,0xc5,0xab,0x78,0xb1,0x21,0xab,0xef,
  8486. 0x69,0x15,0xb7,0x0c,0xe9,0xb4,0x3a,0xe7,
  8487. 0xbc,0xc4,0x22,0xbd,0x93,0xba,0x52,0xe0,
  8488. 0x91,0x2f,0x5e,0x8d,0x6d,0x59,0xf7,0xc2
  8489. };
  8490. #endif
  8491. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128Cipher[] =
  8492. {
  8493. 0x50,0xfd,0x97,0xc3,0xe6,0x1a,0xbb,0x48,
  8494. 0x73,0xfb,0x78,0xdf,0x1e,0x8e,0x77,0xe6,
  8495. 0x4b,0x45,0x7c,0xd6,0x8a
  8496. };
  8497. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128CipherLong[] =
  8498. {
  8499. 0x50,0xfd,0x97,0xc3,0xe6,0x1a,0xbb,0x48,
  8500. 0x73,0xfb,0x78,0xdf,0x1e,0x8e,0x77,0xe6,
  8501. 0x4b,0x45,0x7c,0xd6,0x8a,0xcc,0xda,0x4a,
  8502. 0x89,0xfa,0x23,0x6c,0x06,0xbf,0x26,0x05
  8503. };
  8504. #ifdef WOLFSSL_ARMASM
  8505. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128_2CipherLong[] =
  8506. {
  8507. 0x24,0x5c,0x09,0xa0,0x3b,0x1a,0x5a,0x94,
  8508. 0x2b,0x93,0x56,0x13,0x48,0xa0,0x21,0xce,
  8509. 0x95,0x11,0xa3,0x76,0xd6,0x59,0x88,0x42,
  8510. 0x04,0x71,0x69,0x62,0x28,0xb2,0xee,0x9d,
  8511. 0xd5,0xa0,0xea,0xc7,0x37,0x93,0x92,0xc7,
  8512. 0xf2,0xb6,0x8d,0xd9,0x59,0x1a,0xfa,0xbb
  8513. };
  8514. #endif
  8515. #endif
  8516. int i;
  8517. struct {
  8518. const byte* key;
  8519. int keySz;
  8520. const byte* iv;
  8521. const byte* plain;
  8522. int len;
  8523. const byte* cipher;
  8524. } testVec[] = {
  8525. #ifdef WOLFSSL_AES_128
  8526. { ctr128Key, (int)sizeof(ctr128Key), ctrIv,
  8527. ctrPlain, (int)sizeof(ctrPlain), ctr128Cipher },
  8528. /* let's try with just 9 bytes, non block size test */
  8529. { ctr128Key, (int)sizeof(ctr128Key), ctrIv,
  8530. ctrPlain, (int)sizeof(oddCipher), ctr128Cipher },
  8531. /* and an additional 9 bytes to reuse tmp left buffer */
  8532. { NULL, 0, NULL, ctrPlain, (int)sizeof(oddCipher), oddCipher },
  8533. /* Counter wrapping */
  8534. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128,
  8535. ctrPlain, (int)sizeof(ctr128Wrap128Cipher), ctr128Wrap128Cipher },
  8536. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128,
  8537. ctrPlain, (int)sizeof(ctr128Wrap128CipherLong),
  8538. ctr128Wrap128CipherLong },
  8539. #ifdef WOLFSSL_ARMASM
  8540. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128_2,
  8541. ctrPlain, (int)sizeof(ctr128Wrap128_2CipherLong),
  8542. ctr128Wrap128_2CipherLong },
  8543. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96,
  8544. ctrPlain, (int)sizeof(ctr128Wrap96Cipher), ctr128Wrap96Cipher },
  8545. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96,
  8546. ctrPlain, (int)sizeof(ctr128Wrap96CipherLong),
  8547. ctr128Wrap96CipherLong },
  8548. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96_2,
  8549. ctrPlain, (int)sizeof(ctr128Wrap96_2CipherLong),
  8550. ctr128Wrap96_2CipherLong },
  8551. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64,
  8552. ctrPlain, (int)sizeof(ctr128Wrap64Cipher), ctr128Wrap64Cipher },
  8553. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64,
  8554. ctrPlain, (int)sizeof(ctr128Wrap64CipherLong),
  8555. ctr128Wrap64CipherLong },
  8556. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64_2,
  8557. ctrPlain, (int)sizeof(ctr128Wrap64_2CipherLong),
  8558. ctr128Wrap64_2CipherLong },
  8559. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32,
  8560. ctrPlain, (int)sizeof(ctr128Wrap32Cipher), ctr128Wrap32Cipher },
  8561. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32,
  8562. ctrPlain, (int)sizeof(ctr128Wrap32CipherLong),
  8563. ctr128Wrap32CipherLong },
  8564. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32_2,
  8565. ctrPlain, (int)sizeof(ctr128Wrap32_2CipherLong),
  8566. ctr128Wrap32_2CipherLong },
  8567. #endif
  8568. #endif
  8569. #ifdef WOLFSSL_AES_192
  8570. { ctr192Key, (int)sizeof(ctr192Key), ctrIv,
  8571. ctrPlain, (int)sizeof(ctrPlain), ctr192Cipher },
  8572. /* let's try with just 9 bytes, non block size test */
  8573. { ctr192Key, (int)sizeof(ctr192Key), ctrIv,
  8574. ctrPlain, (int)sizeof(oddCipher), ctr192Cipher },
  8575. /* Counter wrapping */
  8576. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128,
  8577. ctrPlain, (int)sizeof(ctr192Wrap128Cipher), ctr192Wrap128Cipher },
  8578. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128,
  8579. ctrPlain, (int)sizeof(ctr192Wrap128CipherLong),
  8580. ctr192Wrap128CipherLong },
  8581. #ifdef WOLFSSL_ARMASM
  8582. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128_2,
  8583. ctrPlain, (int)sizeof(ctr192Wrap128_2CipherLong),
  8584. ctr192Wrap128_2CipherLong },
  8585. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96,
  8586. ctrPlain, (int)sizeof(ctr192Wrap96Cipher), ctr192Wrap96Cipher },
  8587. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96,
  8588. ctrPlain, (int)sizeof(ctr192Wrap96CipherLong),
  8589. ctr192Wrap96CipherLong },
  8590. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96_2,
  8591. ctrPlain, (int)sizeof(ctr192Wrap96_2CipherLong),
  8592. ctr192Wrap96_2CipherLong },
  8593. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64,
  8594. ctrPlain, (int)sizeof(ctr192Wrap64Cipher), ctr192Wrap64Cipher },
  8595. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64,
  8596. ctrPlain, (int)sizeof(ctr192Wrap64CipherLong),
  8597. ctr192Wrap64CipherLong },
  8598. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64_2,
  8599. ctrPlain, (int)sizeof(ctr192Wrap64_2CipherLong),
  8600. ctr192Wrap64_2CipherLong },
  8601. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32,
  8602. ctrPlain, (int)sizeof(ctr192Wrap32Cipher), ctr192Wrap32Cipher },
  8603. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32,
  8604. ctrPlain, (int)sizeof(ctr192Wrap32CipherLong),
  8605. ctr192Wrap32CipherLong },
  8606. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32_2,
  8607. ctrPlain, (int)sizeof(ctr192Wrap32_2CipherLong),
  8608. ctr192Wrap32_2CipherLong },
  8609. #endif
  8610. #endif
  8611. #ifdef WOLFSSL_AES_256
  8612. { ctr256Key, (int)sizeof(ctr256Key), ctrIv,
  8613. ctrPlain, (int)sizeof(ctrPlain), ctr256Cipher },
  8614. /* let's try with just 9 bytes, non block size test */
  8615. { ctr256Key, (int)sizeof(ctr256Key), ctrIv,
  8616. ctrPlain, (int)sizeof(oddCipher), ctr256Cipher },
  8617. /* Counter wrapping */
  8618. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128,
  8619. ctrPlain, (int)sizeof(ctr256Wrap128Cipher), ctr256Wrap128Cipher },
  8620. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128,
  8621. ctrPlain, (int)sizeof(ctr256Wrap128CipherLong),
  8622. ctr256Wrap128CipherLong },
  8623. #ifdef WOLFSSL_ARMASM
  8624. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128_2,
  8625. ctrPlain, (int)sizeof(ctr256Wrap128_2CipherLong),
  8626. ctr256Wrap128_2CipherLong },
  8627. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96,
  8628. ctrPlain, (int)sizeof(ctr256Wrap96Cipher), ctr256Wrap96Cipher },
  8629. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96,
  8630. ctrPlain, (int)sizeof(ctr256Wrap96CipherLong),
  8631. ctr256Wrap96CipherLong },
  8632. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96_2,
  8633. ctrPlain, (int)sizeof(ctr256Wrap96_2CipherLong),
  8634. ctr256Wrap96_2CipherLong },
  8635. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64,
  8636. ctrPlain, (int)sizeof(ctr256Wrap64Cipher), ctr256Wrap64Cipher },
  8637. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64,
  8638. ctrPlain, (int)sizeof(ctr256Wrap64CipherLong),
  8639. ctr256Wrap64CipherLong },
  8640. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64_2,
  8641. ctrPlain, (int)sizeof(ctr256Wrap64_2CipherLong),
  8642. ctr256Wrap64_2CipherLong },
  8643. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32,
  8644. ctrPlain, (int)sizeof(ctr256Wrap32Cipher), ctr256Wrap32Cipher },
  8645. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32,
  8646. ctrPlain, (int)sizeof(ctr256Wrap32CipherLong),
  8647. ctr256Wrap32CipherLong },
  8648. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32_2,
  8649. ctrPlain, (int)sizeof(ctr256Wrap32_2CipherLong),
  8650. ctr256Wrap32_2CipherLong },
  8651. #endif
  8652. #endif
  8653. };
  8654. #define AES_CTR_TEST_LEN (int)(sizeof(testVec) / sizeof(*testVec))
  8655. for (i = 0; i < AES_CTR_TEST_LEN; i++) {
  8656. if (testVec[i].key != NULL) {
  8657. ret = wc_AesSetKeyDirect(enc, testVec[i].key, testVec[i].keySz,
  8658. testVec[i].iv, AES_ENCRYPTION);
  8659. if (ret != 0) {
  8660. ERROR_OUT(-5930 - i * 10, out);
  8661. }
  8662. /* Ctr only uses encrypt, even on key setup */
  8663. ret = wc_AesSetKeyDirect(dec, testVec[i].key, testVec[i].keySz,
  8664. testVec[i].iv, AES_ENCRYPTION);
  8665. if (ret != 0) {
  8666. ERROR_OUT(-5931 - i * 10, out);
  8667. }
  8668. }
  8669. ret = wc_AesCtrEncrypt(enc, cipher, testVec[i].plain, testVec[i].len);
  8670. if (ret != 0) {
  8671. ERROR_OUT(-5932 - i * 10, out);
  8672. }
  8673. ret = wc_AesCtrEncrypt(dec, plain, cipher, testVec[i].len);
  8674. if (ret != 0) {
  8675. ERROR_OUT(-5933 - i * 10, out);
  8676. }
  8677. if (XMEMCMP(plain, ctrPlain, testVec[i].len)) {
  8678. ERROR_OUT(-5934 - i * 10, out);
  8679. }
  8680. #if !(FIPS_VERSION_EQ(2,0) && defined(WOLFSSL_ARMASM))
  8681. if (XMEMCMP(cipher, testVec[i].cipher, testVec[i].len)) {
  8682. ERROR_OUT(-5935 - i * 10, out);
  8683. }
  8684. #endif
  8685. }
  8686. out:
  8687. return ret;
  8688. }
  8689. #endif /* WOLFSSL_AES_COUNTER */
  8690. WOLFSSL_TEST_SUBROUTINE int aes_test(void)
  8691. {
  8692. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  8693. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8694. Aes *enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8695. #else
  8696. Aes enc[1];
  8697. #endif
  8698. byte cipher[AES_BLOCK_SIZE * 4];
  8699. #ifdef HAVE_AES_DECRYPT
  8700. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8701. Aes *dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8702. #else
  8703. Aes dec[1];
  8704. #endif
  8705. byte plain [AES_BLOCK_SIZE * 4];
  8706. #endif /* HAVE_AES_DECRYPT */
  8707. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER || WOLFSSL_AES_DIRECT */
  8708. int ret = 0;
  8709. #ifdef HAVE_AES_CBC
  8710. #ifdef WOLFSSL_AES_128
  8711. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  8712. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  8713. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  8714. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  8715. };
  8716. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  8717. {
  8718. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  8719. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  8720. };
  8721. WOLFSSL_SMALL_STACK_STATIC const byte key[] = "0123456789abcdef "; /* align */
  8722. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = "1234567890abcdef "; /* align */
  8723. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8724. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  8725. if (enc == NULL)
  8726. ERROR_OUT(-5990, out);
  8727. #endif
  8728. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  8729. if (dec == NULL)
  8730. ERROR_OUT(-5991, out);
  8731. #endif
  8732. #endif
  8733. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  8734. ERROR_OUT(-5900, out); /* note this error code is used programmatically in cleanup. */
  8735. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  8736. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  8737. ERROR_OUT(-5901, out); /* note this error code is used programmatically in cleanup. */
  8738. #endif
  8739. ret = wc_AesSetKey(enc, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8740. if (ret != 0)
  8741. ERROR_OUT(-5902, out);
  8742. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  8743. ret = wc_AesSetKey(dec, key, AES_BLOCK_SIZE, iv, AES_DECRYPTION);
  8744. if (ret != 0)
  8745. ERROR_OUT(-5903, out);
  8746. #endif
  8747. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 4);
  8748. ret = wc_AesCbcEncrypt(enc, cipher, msg, AES_BLOCK_SIZE);
  8749. #if defined(WOLFSSL_ASYNC_CRYPT)
  8750. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8751. #endif
  8752. if (ret != 0)
  8753. ERROR_OUT(-5904, out);
  8754. #ifdef HAVE_AES_DECRYPT
  8755. XMEMSET(plain, 0, AES_BLOCK_SIZE * 4);
  8756. ret = wc_AesCbcDecrypt(dec, plain, cipher, AES_BLOCK_SIZE);
  8757. #if defined(WOLFSSL_ASYNC_CRYPT)
  8758. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8759. #endif
  8760. if (ret != 0)
  8761. ERROR_OUT(-5905, out);
  8762. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  8763. ERROR_OUT(-5906, out);
  8764. #endif /* HAVE_AES_DECRYPT */
  8765. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  8766. ERROR_OUT(-5907, out);
  8767. #endif /* WOLFSSL_AES_128 */
  8768. #if defined(WOLFSSL_AESNI) && defined(HAVE_AES_DECRYPT)
  8769. {
  8770. WOLFSSL_SMALL_STACK_STATIC const byte bigMsg[] = {
  8771. /* "All work and no play makes Jack a dull boy. " */
  8772. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  8773. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  8774. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  8775. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  8776. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  8777. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  8778. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  8779. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  8780. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  8781. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  8782. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  8783. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  8784. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  8785. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  8786. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  8787. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  8788. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  8789. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  8790. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  8791. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  8792. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  8793. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  8794. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  8795. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  8796. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  8797. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  8798. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  8799. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  8800. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  8801. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  8802. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  8803. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  8804. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  8805. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  8806. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  8807. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  8808. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  8809. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  8810. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  8811. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  8812. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  8813. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  8814. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  8815. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  8816. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  8817. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  8818. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  8819. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20
  8820. };
  8821. WOLFSSL_SMALL_STACK_STATIC const byte bigKey[] = "0123456789abcdeffedcba9876543210";
  8822. word32 keySz, msgSz;
  8823. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8824. byte *bigCipher = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8825. byte *bigPlain = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8826. if ((bigCipher == NULL) ||
  8827. (bigPlain == NULL)) {
  8828. if (bigCipher != NULL)
  8829. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8830. ERROR_OUT(-5992, out);
  8831. }
  8832. #else
  8833. byte bigCipher[sizeof(bigMsg)];
  8834. byte bigPlain[sizeof(bigMsg)];
  8835. #endif
  8836. /* Iterate from one AES_BLOCK_SIZE of bigMsg through the whole
  8837. * message by AES_BLOCK_SIZE for each size of AES key. */
  8838. for (keySz = 16; keySz <= 32; keySz += 8) {
  8839. for (msgSz = AES_BLOCK_SIZE;
  8840. msgSz <= sizeof(bigMsg);
  8841. msgSz += AES_BLOCK_SIZE) {
  8842. XMEMSET(bigCipher, 0, sizeof(bigMsg));
  8843. XMEMSET(bigPlain, 0, sizeof(bigMsg));
  8844. ret = wc_AesSetKey(enc, bigKey, keySz, iv, AES_ENCRYPTION);
  8845. if (ret != 0) {
  8846. ret = -5908;
  8847. break;
  8848. }
  8849. ret = wc_AesSetKey(dec, bigKey, keySz, iv, AES_DECRYPTION);
  8850. if (ret != 0) {
  8851. ret = -5909;
  8852. break;
  8853. }
  8854. ret = wc_AesCbcEncrypt(enc, bigCipher, bigMsg, msgSz);
  8855. #if defined(WOLFSSL_ASYNC_CRYPT)
  8856. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8857. #endif
  8858. if (ret != 0) {
  8859. ret = -5910;
  8860. break;
  8861. }
  8862. ret = wc_AesCbcDecrypt(dec, bigPlain, bigCipher, msgSz);
  8863. #if defined(WOLFSSL_ASYNC_CRYPT)
  8864. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8865. #endif
  8866. if (ret != 0) {
  8867. ret = -5911;
  8868. break;
  8869. }
  8870. if (XMEMCMP(bigPlain, bigMsg, msgSz)) {
  8871. ret = -5912;
  8872. break;
  8873. }
  8874. }
  8875. if (ret != 0)
  8876. break;
  8877. }
  8878. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8879. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8880. XFREE(bigPlain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8881. #endif
  8882. if (ret != 0)
  8883. goto out;
  8884. }
  8885. #endif /* WOLFSSL_AESNI && HAVE_AES_DECRYPT */
  8886. /* Test of AES IV state with encrypt/decrypt */
  8887. #ifdef WOLFSSL_AES_128
  8888. {
  8889. /* Test Vector from "NIST Special Publication 800-38A, 2001 Edition"
  8890. * https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38a.pdf
  8891. */
  8892. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  8893. {
  8894. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  8895. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  8896. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  8897. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51
  8898. };
  8899. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] =
  8900. {
  8901. 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46,
  8902. 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
  8903. 0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee,
  8904. 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2
  8905. };
  8906. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  8907. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  8908. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  8909. };
  8910. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  8911. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  8912. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  8913. };
  8914. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  8915. if (ret != 0)
  8916. ERROR_OUT(-5913, out);
  8917. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 2);
  8918. ret = wc_AesCbcEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE);
  8919. #if defined(WOLFSSL_ASYNC_CRYPT)
  8920. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8921. #endif
  8922. if (ret != 0)
  8923. ERROR_OUT(-5914, out);
  8924. if (XMEMCMP(cipher, verify2, AES_BLOCK_SIZE))
  8925. ERROR_OUT(-5915, out);
  8926. ret = wc_AesCbcEncrypt(enc, cipher + AES_BLOCK_SIZE,
  8927. msg2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  8928. #if defined(WOLFSSL_ASYNC_CRYPT)
  8929. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8930. #endif
  8931. if (ret != 0)
  8932. ERROR_OUT(-5916, out);
  8933. if (XMEMCMP(cipher + AES_BLOCK_SIZE, verify2 + AES_BLOCK_SIZE,
  8934. AES_BLOCK_SIZE))
  8935. ERROR_OUT(-5917, out);
  8936. #if defined(HAVE_AES_DECRYPT)
  8937. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_DECRYPTION);
  8938. if (ret != 0)
  8939. ERROR_OUT(-5918, out);
  8940. XMEMSET(plain, 0, AES_BLOCK_SIZE * 2);
  8941. ret = wc_AesCbcDecrypt(dec, plain, verify2, AES_BLOCK_SIZE);
  8942. #if defined(WOLFSSL_ASYNC_CRYPT)
  8943. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8944. #endif
  8945. if (ret != 0)
  8946. ERROR_OUT(-5919, out);
  8947. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE))
  8948. ERROR_OUT(-5920, out);
  8949. ret = wc_AesCbcDecrypt(dec, plain + AES_BLOCK_SIZE,
  8950. verify2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  8951. #if defined(WOLFSSL_ASYNC_CRYPT)
  8952. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8953. #endif
  8954. if (ret != 0)
  8955. ERROR_OUT(-5921, out);
  8956. if (XMEMCMP(plain + AES_BLOCK_SIZE, msg2 + AES_BLOCK_SIZE,
  8957. AES_BLOCK_SIZE))
  8958. ERROR_OUT(-5922, out);
  8959. #endif /* HAVE_AES_DECRYPT */
  8960. }
  8961. #endif /* WOLFSSL_AES_128 */
  8962. #endif /* HAVE_AES_CBC */
  8963. #ifdef WOLFSSL_AES_COUNTER
  8964. ret = aesctr_test(enc, dec, cipher, plain);
  8965. if (ret != 0)
  8966. return ret;
  8967. #endif
  8968. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  8969. {
  8970. WOLFSSL_SMALL_STACK_STATIC const byte niPlain[] =
  8971. {
  8972. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8973. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  8974. };
  8975. WOLFSSL_SMALL_STACK_STATIC const byte niCipher[] =
  8976. {
  8977. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  8978. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  8979. };
  8980. WOLFSSL_SMALL_STACK_STATIC const byte niKey[] =
  8981. {
  8982. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  8983. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  8984. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  8985. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  8986. };
  8987. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  8988. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  8989. if (ret != 0)
  8990. ERROR_OUT(-5994, out);
  8991. #if !defined(HAVE_SELFTEST) && \
  8992. (defined(WOLFSSL_LINUXKM) || \
  8993. !defined(HAVE_FIPS) || \
  8994. (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  8995. if (wc_AesEncryptDirect(enc, cipher, niPlain) != 0)
  8996. ERROR_OUT(-5995, out);
  8997. #else
  8998. wc_AesEncryptDirect(enc, cipher, niPlain);
  8999. #endif
  9000. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  9001. ERROR_OUT(-5996, out);
  9002. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  9003. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  9004. if (ret != 0)
  9005. ERROR_OUT(-5997, out);
  9006. #if !defined(HAVE_SELFTEST) && \
  9007. (defined(WOLFSSL_LINUXKM) || \
  9008. !defined(HAVE_FIPS) || \
  9009. (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  9010. if (wc_AesDecryptDirect(dec, plain, niCipher) != 0)
  9011. ERROR_OUT(-5998, out);
  9012. #else
  9013. wc_AesDecryptDirect(dec, plain, niCipher);
  9014. #endif
  9015. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  9016. ERROR_OUT(-5999, out);
  9017. }
  9018. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  9019. ret = aes_key_size_test();
  9020. if (ret != 0)
  9021. goto out;
  9022. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  9023. ret = aes_cbc_test();
  9024. if (ret != 0)
  9025. goto out;
  9026. #endif
  9027. #if defined(WOLFSSL_AES_XTS)
  9028. #ifdef WOLFSSL_AES_128
  9029. ret = aes_xts_128_test();
  9030. if (ret != 0)
  9031. goto out;
  9032. #endif
  9033. #ifdef WOLFSSL_AES_256
  9034. ret = aes_xts_256_test();
  9035. if (ret != 0)
  9036. goto out;
  9037. #endif
  9038. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  9039. ret = aes_xts_sector_test();
  9040. if (ret != 0)
  9041. goto out;
  9042. #endif
  9043. #ifdef WOLFSSL_AES_128
  9044. ret = aes_xts_args_test();
  9045. if (ret != 0)
  9046. goto out;
  9047. #endif
  9048. #endif
  9049. #if defined(WOLFSSL_AES_CFB)
  9050. ret = aescfb_test();
  9051. if (ret != 0)
  9052. goto out;
  9053. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  9054. ret = aescfb1_test();
  9055. if (ret != 0)
  9056. goto out;
  9057. ret = aescfb8_test();
  9058. if (ret != 0)
  9059. goto out;
  9060. #endif
  9061. #endif
  9062. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9063. ret = aesecb_test();
  9064. if (ret != 0)
  9065. goto out;
  9066. #endif
  9067. out:
  9068. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  9069. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9070. if (enc) {
  9071. if (ret != -5900) /* note this must match ERRROR_OUT() code
  9072. * for wc_AesInit(enc, ...) failure above.
  9073. */
  9074. wc_AesFree(enc);
  9075. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9076. }
  9077. #else
  9078. if (ret != -5900)
  9079. wc_AesFree(enc);
  9080. #endif
  9081. (void)cipher;
  9082. #ifdef HAVE_AES_DECRYPT
  9083. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9084. if (dec) {
  9085. if ((ret != -5900) && (ret != -5901))
  9086. /* note these codes must match the ERRROR_OUT() codes for
  9087. * wc_AesInit() failures above.
  9088. */
  9089. wc_AesFree(dec);
  9090. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9091. }
  9092. #else
  9093. if ((ret != -5900) && (ret != -5901))
  9094. wc_AesFree(dec);
  9095. #endif
  9096. (void)plain;
  9097. #endif /* HAVE_AES_DECRYPT */
  9098. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER || WOLFSSL_AES_DIRECT */
  9099. return ret;
  9100. }
  9101. #ifdef WOLFSSL_AES_192
  9102. WOLFSSL_TEST_SUBROUTINE int aes192_test(void)
  9103. {
  9104. #ifdef HAVE_AES_CBC
  9105. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9106. Aes *enc = NULL;
  9107. #else
  9108. Aes enc[1];
  9109. #endif
  9110. byte cipher[AES_BLOCK_SIZE];
  9111. #ifdef HAVE_AES_DECRYPT
  9112. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9113. Aes *dec = NULL;
  9114. #else
  9115. Aes dec[1];
  9116. #endif
  9117. byte plain[AES_BLOCK_SIZE];
  9118. #endif
  9119. #endif /* HAVE_AES_CBC */
  9120. int ret = 0;
  9121. #ifdef HAVE_AES_CBC
  9122. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  9123. * Appendix F.2.3 */
  9124. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  9125. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  9126. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  9127. };
  9128. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  9129. {
  9130. 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  9131. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8
  9132. };
  9133. WOLFSSL_SMALL_STACK_STATIC byte key[] = {
  9134. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  9135. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  9136. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  9137. };
  9138. WOLFSSL_SMALL_STACK_STATIC byte iv[] = {
  9139. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  9140. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  9141. };
  9142. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9143. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9144. ERROR_OUT(-6008, out);
  9145. #ifdef HAVE_AES_DECRYPT
  9146. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9147. ERROR_OUT(-6009, out);
  9148. #endif
  9149. #endif
  9150. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  9151. ERROR_OUT(-6000, out);
  9152. #ifdef HAVE_AES_DECRYPT
  9153. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  9154. ERROR_OUT(-6001, out);
  9155. #endif
  9156. ret = wc_AesSetKey(enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  9157. if (ret != 0)
  9158. ERROR_OUT(-6002, out);
  9159. #ifdef HAVE_AES_DECRYPT
  9160. ret = wc_AesSetKey(dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  9161. if (ret != 0)
  9162. ERROR_OUT(-6003, out);
  9163. #endif
  9164. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  9165. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  9166. #if defined(WOLFSSL_ASYNC_CRYPT)
  9167. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9168. #endif
  9169. if (ret != 0)
  9170. ERROR_OUT(-6004, out);
  9171. #ifdef HAVE_AES_DECRYPT
  9172. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  9173. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  9174. #if defined(WOLFSSL_ASYNC_CRYPT)
  9175. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9176. #endif
  9177. if (ret != 0)
  9178. ERROR_OUT(-6005, out);
  9179. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  9180. ERROR_OUT(-6006, out);
  9181. }
  9182. #endif
  9183. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  9184. ERROR_OUT(-6007, out);
  9185. wc_AesFree(enc);
  9186. #ifdef HAVE_AES_DECRYPT
  9187. wc_AesFree(dec);
  9188. #endif
  9189. out:
  9190. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9191. if (enc)
  9192. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9193. #ifdef HAVE_AES_DECRYPT
  9194. if (dec)
  9195. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9196. #endif
  9197. #endif
  9198. #endif /* HAVE_AES_CBC */
  9199. return ret;
  9200. }
  9201. #endif /* WOLFSSL_AES_192 */
  9202. #ifdef WOLFSSL_AES_256
  9203. WOLFSSL_TEST_SUBROUTINE int aes256_test(void)
  9204. {
  9205. #ifdef HAVE_AES_CBC
  9206. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9207. Aes *enc = NULL;
  9208. #else
  9209. Aes enc[1];
  9210. #endif
  9211. byte cipher[AES_BLOCK_SIZE];
  9212. #ifdef HAVE_AES_DECRYPT
  9213. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9214. Aes *dec = NULL;
  9215. #else
  9216. Aes dec[1];
  9217. #endif
  9218. byte plain[AES_BLOCK_SIZE];
  9219. #endif
  9220. #endif /* HAVE_AES_CBC */
  9221. int ret = 0;
  9222. #ifdef HAVE_AES_CBC
  9223. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  9224. * Appendix F.2.5 */
  9225. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  9226. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  9227. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  9228. };
  9229. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  9230. {
  9231. 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  9232. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6
  9233. };
  9234. WOLFSSL_SMALL_STACK_STATIC byte key[] = {
  9235. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  9236. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  9237. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  9238. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  9239. };
  9240. WOLFSSL_SMALL_STACK_STATIC byte iv[] = {
  9241. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  9242. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  9243. };
  9244. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9245. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9246. ERROR_OUT(-6108, out);
  9247. #ifdef HAVE_AES_DECRYPT
  9248. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9249. ERROR_OUT(-6109, out);
  9250. #endif
  9251. #endif
  9252. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  9253. ERROR_OUT(-6100, out);
  9254. #ifdef HAVE_AES_DECRYPT
  9255. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  9256. ERROR_OUT(-6101, out);
  9257. #endif
  9258. ret = wc_AesSetKey(enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  9259. if (ret != 0)
  9260. ERROR_OUT(-6102, out);
  9261. #ifdef HAVE_AES_DECRYPT
  9262. ret = wc_AesSetKey(dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  9263. if (ret != 0)
  9264. ERROR_OUT(-6103, out);
  9265. #endif
  9266. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  9267. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  9268. #if defined(WOLFSSL_ASYNC_CRYPT)
  9269. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9270. #endif
  9271. if (ret != 0)
  9272. ERROR_OUT(-6104, out);
  9273. #ifdef HAVE_AES_DECRYPT
  9274. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  9275. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  9276. #if defined(WOLFSSL_ASYNC_CRYPT)
  9277. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9278. #endif
  9279. if (ret != 0)
  9280. ERROR_OUT(-6105, out);
  9281. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  9282. ERROR_OUT(-6106, out);
  9283. }
  9284. #endif
  9285. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  9286. ERROR_OUT(-6107, out);
  9287. wc_AesFree(enc);
  9288. #ifdef HAVE_AES_DECRYPT
  9289. wc_AesFree(dec);
  9290. #endif
  9291. out:
  9292. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9293. if (enc)
  9294. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9295. #ifdef HAVE_AES_DECRYPT
  9296. if (dec)
  9297. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9298. #endif
  9299. #endif
  9300. #endif /* HAVE_AES_CBC */
  9301. return ret;
  9302. }
  9303. #endif /* WOLFSSL_AES_256 */
  9304. #ifdef HAVE_AESGCM
  9305. #ifdef WOLFSSL_AES_128
  9306. static int aesgcm_default_test_helper(byte* key, int keySz, byte* iv, int ivSz,
  9307. byte* plain, int plainSz, byte* cipher, int cipherSz,
  9308. byte* aad, int aadSz, byte* tag, int tagSz)
  9309. {
  9310. int ret, enc_inited = 0, dec_inited = 0;
  9311. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9312. Aes *enc = NULL;
  9313. Aes *dec = NULL;
  9314. #else
  9315. Aes enc[1];
  9316. Aes dec[1];
  9317. #endif
  9318. byte resultT[AES_BLOCK_SIZE];
  9319. byte resultP[AES_BLOCK_SIZE * 3];
  9320. byte resultC[AES_BLOCK_SIZE * 3];
  9321. int result;
  9322. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9323. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9324. ERROR_OUT(-6118, out);
  9325. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9326. ERROR_OUT(-6119, out);
  9327. #endif
  9328. XMEMSET(resultT, 0, sizeof(resultT));
  9329. XMEMSET(resultC, 0, sizeof(resultC));
  9330. XMEMSET(resultP, 0, sizeof(resultP));
  9331. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  9332. ERROR_OUT(-6110, out);
  9333. else
  9334. enc_inited = 1;
  9335. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  9336. ERROR_OUT(-6111, out);
  9337. else
  9338. dec_inited = 1;
  9339. result = wc_AesGcmSetKey(enc, key, keySz);
  9340. if (result != 0)
  9341. ERROR_OUT(-6112, out);
  9342. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9343. result = wc_AesGcmEncrypt(enc, resultC, plain, plainSz, iv, ivSz,
  9344. resultT, tagSz, aad, aadSz);
  9345. #if defined(WOLFSSL_ASYNC_CRYPT)
  9346. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9347. #endif
  9348. if (result != 0)
  9349. ERROR_OUT(-6113, out);
  9350. if (cipher != NULL) {
  9351. if (XMEMCMP(cipher, resultC, cipherSz))
  9352. ERROR_OUT(-6114, out);
  9353. }
  9354. if (XMEMCMP(tag, resultT, tagSz))
  9355. ERROR_OUT(-6115, out);
  9356. #ifdef HAVE_AES_DECRYPT
  9357. result = wc_AesGcmSetKey(dec, key, keySz);
  9358. if (result != 0)
  9359. ERROR_OUT(-6116, out);
  9360. result = wc_AesGcmDecrypt(dec, resultP, resultC, cipherSz,
  9361. iv, ivSz, resultT, tagSz, aad, aadSz);
  9362. #if defined(WOLFSSL_ASYNC_CRYPT)
  9363. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9364. #endif
  9365. if (result != 0)
  9366. ERROR_OUT(-6117, out);
  9367. if (plain != NULL) {
  9368. if (XMEMCMP(plain, resultP, plainSz))
  9369. ERROR_OUT(-6118, out);
  9370. }
  9371. #endif /* HAVE_AES_DECRYPT */
  9372. ret = 0;
  9373. out:
  9374. if (enc_inited)
  9375. wc_AesFree(enc);
  9376. if (dec_inited)
  9377. wc_AesFree(dec);
  9378. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9379. if (enc)
  9380. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9381. if (dec)
  9382. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9383. #endif
  9384. return ret;
  9385. }
  9386. #endif
  9387. /* tests that only use 12 byte IV and 16 or less byte AAD
  9388. * test vectors are from NIST SP 800-38D
  9389. * https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/CAVP-TESTING-BLOCK-CIPHER-MODES*/
  9390. WOLFSSL_TEST_SUBROUTINE int aesgcm_default_test(void)
  9391. {
  9392. #ifdef WOLFSSL_AES_128
  9393. byte key1[] = {
  9394. 0x29, 0x8e, 0xfa, 0x1c, 0xcf, 0x29, 0xcf, 0x62,
  9395. 0xae, 0x68, 0x24, 0xbf, 0xc1, 0x95, 0x57, 0xfc
  9396. };
  9397. byte iv1[] = {
  9398. 0x6f, 0x58, 0xa9, 0x3f, 0xe1, 0xd2, 0x07, 0xfa,
  9399. 0xe4, 0xed, 0x2f, 0x6d
  9400. };
  9401. ALIGN64 byte plain1[] = {
  9402. 0xcc, 0x38, 0xbc, 0xcd, 0x6b, 0xc5, 0x36, 0xad,
  9403. 0x91, 0x9b, 0x13, 0x95, 0xf5, 0xd6, 0x38, 0x01,
  9404. 0xf9, 0x9f, 0x80, 0x68, 0xd6, 0x5c, 0xa5, 0xac,
  9405. 0x63, 0x87, 0x2d, 0xaf, 0x16, 0xb9, 0x39, 0x01
  9406. };
  9407. byte aad1[] = {
  9408. 0x02, 0x1f, 0xaf, 0xd2, 0x38, 0x46, 0x39, 0x73,
  9409. 0xff, 0xe8, 0x02, 0x56, 0xe5, 0xb1, 0xc6, 0xb1
  9410. };
  9411. ALIGN64 byte cipher1[] = {
  9412. 0xdf, 0xce, 0x4e, 0x9c, 0xd2, 0x91, 0x10, 0x3d,
  9413. 0x7f, 0xe4, 0xe6, 0x33, 0x51, 0xd9, 0xe7, 0x9d,
  9414. 0x3d, 0xfd, 0x39, 0x1e, 0x32, 0x67, 0x10, 0x46,
  9415. 0x58, 0x21, 0x2d, 0xa9, 0x65, 0x21, 0xb7, 0xdb
  9416. };
  9417. byte tag1[] = {
  9418. 0x54, 0x24, 0x65, 0xef, 0x59, 0x93, 0x16, 0xf7,
  9419. 0x3a, 0x7a, 0x56, 0x05, 0x09, 0xa2, 0xd9, 0xf2
  9420. };
  9421. byte key2[] = {
  9422. 0x01, 0x6d, 0xbb, 0x38, 0xda, 0xa7, 0x6d, 0xfe,
  9423. 0x7d, 0xa3, 0x84, 0xeb, 0xf1, 0x24, 0x03, 0x64
  9424. };
  9425. byte iv2[] = {
  9426. 0x07, 0x93, 0xef, 0x3a, 0xda, 0x78, 0x2f, 0x78,
  9427. 0xc9, 0x8a, 0xff, 0xe3
  9428. };
  9429. ALIGN64 byte plain2[] = {
  9430. 0x4b, 0x34, 0xa9, 0xec, 0x57, 0x63, 0x52, 0x4b,
  9431. 0x19, 0x1d, 0x56, 0x16, 0xc5, 0x47, 0xf6, 0xb7
  9432. };
  9433. ALIGN64 byte cipher2[] = {
  9434. 0x60, 0x9a, 0xa3, 0xf4, 0x54, 0x1b, 0xc0, 0xfe,
  9435. 0x99, 0x31, 0xda, 0xad, 0x2e, 0xe1, 0x5d, 0x0c
  9436. };
  9437. byte tag2[] = {
  9438. 0x33, 0xaf, 0xec, 0x59, 0xc4, 0x5b, 0xaf, 0x68,
  9439. 0x9a, 0x5e, 0x1b, 0x13, 0xae, 0x42, 0x36, 0x19
  9440. };
  9441. byte key3[] = {
  9442. 0xb0, 0x1e, 0x45, 0xcc, 0x30, 0x88, 0xaa, 0xba,
  9443. 0x9f, 0xa4, 0x3d, 0x81, 0xd4, 0x81, 0x82, 0x3f
  9444. };
  9445. byte iv3[] = {
  9446. 0x5a, 0x2c, 0x4a, 0x66, 0x46, 0x87, 0x13, 0x45,
  9447. 0x6a, 0x4b, 0xd5, 0xe1
  9448. };
  9449. byte tag3[] = {
  9450. 0x01, 0x42, 0x80, 0xf9, 0x44, 0xf5, 0x3c, 0x68,
  9451. 0x11, 0x64, 0xb2, 0xff
  9452. };
  9453. int ret;
  9454. ret = aesgcm_default_test_helper(key1, sizeof(key1), iv1, sizeof(iv1),
  9455. plain1, sizeof(plain1), cipher1, sizeof(cipher1),
  9456. aad1, sizeof(aad1), tag1, sizeof(tag1));
  9457. if (ret != 0) {
  9458. return ret;
  9459. }
  9460. ret = aesgcm_default_test_helper(key2, sizeof(key2), iv2, sizeof(iv2),
  9461. plain2, sizeof(plain2), cipher2, sizeof(cipher2),
  9462. NULL, 0, tag2, sizeof(tag2));
  9463. if (ret != 0) {
  9464. return ret;
  9465. }
  9466. ret = aesgcm_default_test_helper(key3, sizeof(key3), iv3, sizeof(iv3),
  9467. NULL, 0, NULL, 0,
  9468. NULL, 0, tag3, sizeof(tag3));
  9469. if (ret != 0) {
  9470. return ret;
  9471. }
  9472. #endif
  9473. return 0;
  9474. }
  9475. WOLFSSL_TEST_SUBROUTINE int aesgcm_test(void)
  9476. {
  9477. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9478. Aes *enc = NULL;
  9479. Aes *dec = NULL;
  9480. #else
  9481. Aes enc[1];
  9482. Aes dec[1];
  9483. #endif
  9484. /*
  9485. * This is Test Case 16 from the document Galois/
  9486. * Counter Mode of Operation (GCM) by McGrew and
  9487. * Viega.
  9488. */
  9489. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  9490. {
  9491. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  9492. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  9493. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  9494. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  9495. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  9496. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  9497. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  9498. 0xba, 0x63, 0x7b, 0x39
  9499. };
  9500. #if defined(WOLFSSL_AES_256) || defined(WOLFSSL_AES_192)
  9501. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  9502. {
  9503. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  9504. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  9505. 0xab, 0xad, 0xda, 0xd2
  9506. };
  9507. #endif
  9508. #ifdef WOLFSSL_AES_256
  9509. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  9510. {
  9511. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  9512. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  9513. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  9514. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08
  9515. };
  9516. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  9517. {
  9518. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
  9519. 0xde, 0xca, 0xf8, 0x88
  9520. };
  9521. #endif /* WOLFSSL_AES_256 */
  9522. #if defined(WOLFSSL_AES_256) || defined(WOLFSSL_AES_192)
  9523. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  9524. {
  9525. 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
  9526. 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
  9527. 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  9528. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
  9529. 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
  9530. 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  9531. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
  9532. 0xbc, 0xc9, 0xf6, 0x62
  9533. };
  9534. #endif /* WOLFSSL_AES_256 || WOLFSSL_AES_192 */
  9535. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  9536. {
  9537. 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68,
  9538. 0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b
  9539. };
  9540. /* FIPS, QAT and PIC32MZ HW Crypto only support 12-byte IV */
  9541. #if !defined(HAVE_FIPS) && \
  9542. !defined(WOLFSSL_PIC32MZ_CRYPT) && \
  9543. !defined(FREESCALE_LTC) && !defined(FREESCALE_MMCAU) && \
  9544. !defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  9545. !defined(WOLFSSL_SILABS_SE_ACCEL) && !defined(WOLFSSL_KCAPI_AES) && \
  9546. !(defined(WOLF_CRYPTO_CB) && \
  9547. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  9548. #define ENABLE_NON_12BYTE_IV_TEST
  9549. #ifdef WOLFSSL_AES_192
  9550. /* Test Case 12, uses same plaintext and AAD data. */
  9551. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  9552. {
  9553. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  9554. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  9555. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c
  9556. };
  9557. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  9558. {
  9559. 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5,
  9560. 0x55, 0x90, 0x9c, 0x5a, 0xff, 0x52, 0x69, 0xaa,
  9561. 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1,
  9562. 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28,
  9563. 0xc3, 0xc0, 0xc9, 0x51, 0x56, 0x80, 0x95, 0x39,
  9564. 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54,
  9565. 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57,
  9566. 0xa6, 0x37, 0xb3, 0x9b
  9567. };
  9568. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  9569. {
  9570. 0xd2, 0x7e, 0x88, 0x68, 0x1c, 0xe3, 0x24, 0x3c,
  9571. 0x48, 0x30, 0x16, 0x5a, 0x8f, 0xdc, 0xf9, 0xff,
  9572. 0x1d, 0xe9, 0xa1, 0xd8, 0xe6, 0xb4, 0x47, 0xef,
  9573. 0x6e, 0xf7, 0xb7, 0x98, 0x28, 0x66, 0x6e, 0x45,
  9574. 0x81, 0xe7, 0x90, 0x12, 0xaf, 0x34, 0xdd, 0xd9,
  9575. 0xe2, 0xf0, 0x37, 0x58, 0x9b, 0x29, 0x2d, 0xb3,
  9576. 0xe6, 0x7c, 0x03, 0x67, 0x45, 0xfa, 0x22, 0xe7,
  9577. 0xe9, 0xb7, 0x37, 0x3b
  9578. };
  9579. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  9580. {
  9581. 0xdc, 0xf5, 0x66, 0xff, 0x29, 0x1c, 0x25, 0xbb,
  9582. 0xb8, 0x56, 0x8f, 0xc3, 0xd3, 0x76, 0xa6, 0xd9
  9583. };
  9584. #endif /* WOLFSSL_AES_192 */
  9585. #ifdef WOLFSSL_AES_128
  9586. /* The following is an interesting test case from the example
  9587. * FIPS test vectors for AES-GCM. IVlen = 1 byte */
  9588. WOLFSSL_SMALL_STACK_STATIC const byte p3[] =
  9589. {
  9590. 0x57, 0xce, 0x45, 0x1f, 0xa5, 0xe2, 0x35, 0xa5,
  9591. 0x8e, 0x1a, 0xa2, 0x3b, 0x77, 0xcb, 0xaf, 0xe2
  9592. };
  9593. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  9594. {
  9595. 0xbb, 0x01, 0xd7, 0x03, 0x81, 0x1c, 0x10, 0x1a,
  9596. 0x35, 0xe0, 0xff, 0xd2, 0x91, 0xba, 0xf2, 0x4b
  9597. };
  9598. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  9599. {
  9600. 0xca
  9601. };
  9602. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  9603. {
  9604. 0x6b, 0x5f, 0xb3, 0x9d, 0xc1, 0xc5, 0x7a, 0x4f,
  9605. 0xf3, 0x51, 0x4d, 0xc2, 0xd5, 0xf0, 0xd0, 0x07
  9606. };
  9607. WOLFSSL_SMALL_STACK_STATIC const byte a3[] =
  9608. {
  9609. 0x40, 0xfc, 0xdc, 0xd7, 0x4a, 0xd7, 0x8b, 0xf1,
  9610. 0x3e, 0x7c, 0x60, 0x55, 0x50, 0x51, 0xdd, 0x54
  9611. };
  9612. WOLFSSL_SMALL_STACK_STATIC const byte t3[] =
  9613. {
  9614. 0x06, 0x90, 0xed, 0x01, 0x34, 0xdd, 0xc6, 0x95,
  9615. 0x31, 0x2e, 0x2a, 0xf9, 0x57, 0x7a, 0x1e, 0xa6
  9616. };
  9617. #endif /* WOLFSSL_AES_128 */
  9618. #ifdef WOLFSSL_AES_256
  9619. int ivlen;
  9620. #endif
  9621. #endif
  9622. byte resultT[sizeof(t1) + AES_BLOCK_SIZE];
  9623. byte resultP[sizeof(p) + AES_BLOCK_SIZE];
  9624. byte resultC[sizeof(p) + AES_BLOCK_SIZE];
  9625. int result = 0;
  9626. int ret;
  9627. #ifdef WOLFSSL_AES_256
  9628. #if !(defined(WOLF_CRYPTO_CB) && defined(HAVE_INTEL_QA_SYNC))
  9629. int alen;
  9630. #endif
  9631. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  9632. int plen;
  9633. #endif
  9634. #endif
  9635. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  9636. byte buf[sizeof(p) + AES_BLOCK_SIZE];
  9637. byte bufA[sizeof(a) + 1];
  9638. byte *large_aad = (byte*)XMALLOC((size_t)1024 + 16, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9639. #endif
  9640. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM)
  9641. #if !defined(BENCH_AESGCM_LARGE)
  9642. #define BENCH_AESGCM_LARGE 1024
  9643. #endif
  9644. #ifndef WOLFSSL_NO_MALLOC
  9645. byte *large_input = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9646. byte *large_output = (byte *)XMALLOC(BENCH_AESGCM_LARGE + AES_BLOCK_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9647. byte *large_outdec = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9648. if ((! large_input) || (! large_output) || (! large_outdec))
  9649. ERROR_OUT(MEMORY_E, out);
  9650. #else
  9651. byte large_input[BENCH_AESGCM_LARGE];
  9652. byte large_output[BENCH_AESGCM_LARGE];
  9653. byte large_outdec[BENCH_AESGCM_LARGE];
  9654. #endif
  9655. XMEMSET(large_input, 0, BENCH_AESGCM_LARGE);
  9656. XMEMSET(large_output, 0, BENCH_AESGCM_LARGE + AES_BLOCK_SIZE);
  9657. XMEMSET(large_outdec, 0, BENCH_AESGCM_LARGE);
  9658. #endif
  9659. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9660. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9661. ERROR_OUT(-6342, out);
  9662. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9663. ERROR_OUT(-6343, out);
  9664. #endif
  9665. (void)result;
  9666. XMEMSET(resultT, 0, sizeof(resultT));
  9667. XMEMSET(resultC, 0, sizeof(resultC));
  9668. XMEMSET(resultP, 0, sizeof(resultP));
  9669. if (wc_AesInit(enc, HEAP_HINT, devId) != 0) {
  9670. ERROR_OUT(-6300, out);
  9671. }
  9672. if (wc_AesInit(dec, HEAP_HINT, devId) != 0) {
  9673. ERROR_OUT(-6301, out);
  9674. }
  9675. #ifdef WOLFSSL_AES_256
  9676. result = wc_AesGcmSetKey(enc, k1, sizeof(k1));
  9677. if (result != 0)
  9678. ERROR_OUT(-6302, out);
  9679. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9680. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  9681. resultT, sizeof(t1), a, sizeof(a));
  9682. #if defined(WOLFSSL_ASYNC_CRYPT)
  9683. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9684. #endif
  9685. if (result != 0)
  9686. ERROR_OUT(-6303, out);
  9687. if (XMEMCMP(c1, resultC, sizeof(c1)))
  9688. ERROR_OUT(-6304, out);
  9689. if (XMEMCMP(t1, resultT, sizeof(t1)))
  9690. ERROR_OUT(-6305, out);
  9691. #ifdef HAVE_AES_DECRYPT
  9692. result = wc_AesGcmSetKey(dec, k1, sizeof(k1));
  9693. if (result != 0)
  9694. ERROR_OUT(-6306, out);
  9695. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1),
  9696. iv1, sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  9697. #if defined(WOLFSSL_ASYNC_CRYPT)
  9698. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9699. #endif
  9700. if (result != 0)
  9701. ERROR_OUT(-6307, out);
  9702. if (XMEMCMP(p, resultP, sizeof(p)))
  9703. ERROR_OUT(-6308, out);
  9704. #endif /* HAVE_AES_DECRYPT */
  9705. /* Large buffer test */
  9706. #ifdef BENCH_AESGCM_LARGE
  9707. /* setup test buffer */
  9708. for (alen=0; alen<BENCH_AESGCM_LARGE; alen++)
  9709. large_input[alen] = (byte)alen;
  9710. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9711. result = wc_AesGcmEncrypt(enc, large_output, large_input,
  9712. BENCH_AESGCM_LARGE, iv1, sizeof(iv1),
  9713. resultT, sizeof(t1), a, sizeof(a));
  9714. #if defined(WOLFSSL_ASYNC_CRYPT)
  9715. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9716. #endif
  9717. if (result != 0)
  9718. ERROR_OUT(-6309, out);
  9719. #ifdef HAVE_AES_DECRYPT
  9720. result = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  9721. BENCH_AESGCM_LARGE, iv1, sizeof(iv1), resultT,
  9722. sizeof(t1), a, sizeof(a));
  9723. #if defined(WOLFSSL_ASYNC_CRYPT)
  9724. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9725. #endif
  9726. if (result != 0)
  9727. ERROR_OUT(-6310, out);
  9728. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  9729. ERROR_OUT(-6311, out);
  9730. #endif /* HAVE_AES_DECRYPT */
  9731. #endif /* BENCH_AESGCM_LARGE */
  9732. #if defined(ENABLE_NON_12BYTE_IV_TEST) && defined(WOLFSSL_AES_256)
  9733. /* Variable IV length test */
  9734. for (ivlen=1; ivlen<(int)sizeof(k1); ivlen++) {
  9735. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9736. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), k1,
  9737. (word32)ivlen, resultT, sizeof(t1), a, sizeof(a));
  9738. #if defined(WOLFSSL_ASYNC_CRYPT)
  9739. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9740. #endif
  9741. if (result != 0)
  9742. ERROR_OUT(-6312, out);
  9743. #ifdef HAVE_AES_DECRYPT
  9744. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), k1,
  9745. (word32)ivlen, resultT, sizeof(t1), a, sizeof(a));
  9746. #if defined(WOLFSSL_ASYNC_CRYPT)
  9747. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9748. #endif
  9749. if (result != 0)
  9750. ERROR_OUT(-6313, out);
  9751. #endif /* HAVE_AES_DECRYPT */
  9752. }
  9753. #endif
  9754. #if !(defined(WOLF_CRYPTO_CB) && defined(HAVE_INTEL_QA_SYNC))
  9755. /* Variable authenticated data length test */
  9756. for (alen=0; alen<(int)sizeof(p); alen++) {
  9757. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9758. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1,
  9759. sizeof(iv1), resultT, sizeof(t1), p, (word32)alen);
  9760. #if defined(WOLFSSL_ASYNC_CRYPT)
  9761. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9762. #endif
  9763. if (result != 0)
  9764. ERROR_OUT(-6314, out);
  9765. #ifdef HAVE_AES_DECRYPT
  9766. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), iv1,
  9767. sizeof(iv1), resultT, sizeof(t1), p, (word32)alen);
  9768. #if defined(WOLFSSL_ASYNC_CRYPT)
  9769. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9770. #endif
  9771. if (result != 0)
  9772. ERROR_OUT(-6315, out);
  9773. #endif /* HAVE_AES_DECRYPT */
  9774. }
  9775. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  9776. if (! large_aad)
  9777. ERROR_OUT(MEMORY_E, out);
  9778. XMEMSET(large_aad, 0, 1024+16);
  9779. /* Variable authenticated data length test */
  9780. for (alen=0; alen<=1024; alen+=16) {
  9781. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9782. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1,
  9783. sizeof(iv1), resultT, sizeof(t1), large_aad, (word32)alen);
  9784. if (result != 0)
  9785. ERROR_OUT(-6316, out);
  9786. #ifdef HAVE_AES_DECRYPT
  9787. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), iv1,
  9788. sizeof(iv1), resultT, sizeof(t1), large_aad, (word32)alen);
  9789. if (result != 0)
  9790. ERROR_OUT(-6317, out);
  9791. #endif /* HAVE_AES_DECRYPT */
  9792. }
  9793. /* Test unaligned memory of all potential arguments */
  9794. result = wc_AesGcmSetKey(enc, k1, sizeof(k1));
  9795. if (result != 0)
  9796. ERROR_OUT(-6318, out);
  9797. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9798. XMEMCPY(&buf[1], p, sizeof(p));
  9799. XMEMCPY(&bufA[1], a, sizeof(a));
  9800. result = wc_AesGcmEncrypt(enc, &resultC[1], &buf[1], sizeof(p), iv1, sizeof(iv1),
  9801. &resultT[1], sizeof(t1), &bufA[1], sizeof(a));
  9802. if (result != 0)
  9803. ERROR_OUT(-6319, out);
  9804. if (XMEMCMP(c1, &resultC[1], sizeof(c1)))
  9805. ERROR_OUT(-6320, out);
  9806. if (XMEMCMP(t1, &resultT[1], sizeof(t1)))
  9807. ERROR_OUT(-6321, out);
  9808. #ifdef HAVE_AES_DECRYPT
  9809. result = wc_AesGcmSetKey(dec, k1, sizeof(k1));
  9810. if (result != 0)
  9811. ERROR_OUT(-6322, out);
  9812. result = wc_AesGcmDecrypt(dec, &resultP[1], &resultC[1], sizeof(c1),
  9813. iv1, sizeof(iv1), &resultT[1], sizeof(t1), &bufA[1], sizeof(a));
  9814. if (result != 0)
  9815. ERROR_OUT(-6323, out);
  9816. if (XMEMCMP(p, &resultP[1], sizeof(p)))
  9817. ERROR_OUT(-6324, out);
  9818. #endif /* HAVE_AES_DECRYPT */
  9819. #endif /* Xilinx Versal */
  9820. #endif
  9821. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  9822. #ifdef BENCH_AESGCM_LARGE
  9823. /* Variable plain text length test */
  9824. for (plen=1; plen<BENCH_AESGCM_LARGE; plen++) {
  9825. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9826. result = wc_AesGcmEncrypt(enc, large_output, large_input,
  9827. plen, iv1, sizeof(iv1), resultT,
  9828. sizeof(t1), a, sizeof(a));
  9829. #if defined(WOLFSSL_ASYNC_CRYPT)
  9830. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9831. #endif
  9832. if (result != 0)
  9833. ERROR_OUT(-6316, out);
  9834. #ifdef HAVE_AES_DECRYPT
  9835. result = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  9836. plen, iv1, sizeof(iv1), resultT,
  9837. sizeof(t1), a, sizeof(a));
  9838. #if defined(WOLFSSL_ASYNC_CRYPT)
  9839. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9840. #endif
  9841. if (result != 0)
  9842. ERROR_OUT(-6317, out);
  9843. #endif /* HAVE_AES_DECRYPT */
  9844. }
  9845. #else /* BENCH_AESGCM_LARGE */
  9846. /* Variable plain text length test */
  9847. for (plen=1; plen<(int)sizeof(p); plen++) {
  9848. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9849. result = wc_AesGcmEncrypt(enc, resultC, p, (word32)plen, iv1,
  9850. sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  9851. #if defined(WOLFSSL_ASYNC_CRYPT)
  9852. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9853. #endif
  9854. if (result != 0)
  9855. ERROR_OUT(-6318, out);
  9856. #ifdef HAVE_AES_DECRYPT
  9857. result = wc_AesGcmDecrypt(dec, resultP, resultC, (word32)plen, iv1,
  9858. sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  9859. #if defined(WOLFSSL_ASYNC_CRYPT)
  9860. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9861. #endif
  9862. if (result != 0)
  9863. ERROR_OUT(-6319, out);
  9864. #endif /* HAVE_AES_DECRYPT */
  9865. }
  9866. #endif /* BENCH_AESGCM_LARGE */
  9867. #endif
  9868. #endif /* WOLFSSL_AES_256 */
  9869. /* test with IV != 12 bytes */
  9870. #ifdef ENABLE_NON_12BYTE_IV_TEST
  9871. XMEMSET(resultT, 0, sizeof(resultT));
  9872. XMEMSET(resultC, 0, sizeof(resultC));
  9873. XMEMSET(resultP, 0, sizeof(resultP));
  9874. #ifdef WOLFSSL_AES_192
  9875. wc_AesGcmSetKey(enc, k2, sizeof(k2));
  9876. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9877. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv2, sizeof(iv2),
  9878. resultT, sizeof(t1), 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(-6320, out);
  9884. if (XMEMCMP(c2, resultC, sizeof(c2)))
  9885. ERROR_OUT(-6321, out);
  9886. if (XMEMCMP(t2, resultT, sizeof(t1)))
  9887. ERROR_OUT(-6322, out);
  9888. #ifdef HAVE_AES_DECRYPT
  9889. result = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c1),
  9890. iv2, sizeof(iv2), resultT, sizeof(t1), a, sizeof(a));
  9891. #if defined(WOLFSSL_ASYNC_CRYPT)
  9892. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9893. #endif
  9894. if (result != 0)
  9895. ERROR_OUT(-6323, out);
  9896. if (XMEMCMP(p, resultP, sizeof(p)))
  9897. ERROR_OUT(-6324, out);
  9898. #endif /* HAVE_AES_DECRYPT */
  9899. XMEMSET(resultT, 0, sizeof(resultT));
  9900. XMEMSET(resultC, 0, sizeof(resultC));
  9901. XMEMSET(resultP, 0, sizeof(resultP));
  9902. #endif /* WOLFSSL_AES_192 */
  9903. #ifdef WOLFSSL_AES_128
  9904. wc_AesGcmSetKey(enc, k3, sizeof(k3));
  9905. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9906. result = wc_AesGcmEncrypt(enc, resultC, p3, sizeof(p3), iv3, sizeof(iv3),
  9907. resultT, sizeof(t3), a3, sizeof(a3));
  9908. #if defined(WOLFSSL_ASYNC_CRYPT)
  9909. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9910. #endif
  9911. if (result != 0)
  9912. ERROR_OUT(-6325, out);
  9913. if (XMEMCMP(c3, resultC, sizeof(c3)))
  9914. ERROR_OUT(-6326, out);
  9915. if (XMEMCMP(t3, resultT, sizeof(t3)))
  9916. ERROR_OUT(-6327, out);
  9917. #ifdef HAVE_AES_DECRYPT
  9918. result = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c3),
  9919. iv3, sizeof(iv3), resultT, sizeof(t3), a3, sizeof(a3));
  9920. #if defined(WOLFSSL_ASYNC_CRYPT)
  9921. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9922. #endif
  9923. if (result != 0)
  9924. ERROR_OUT(-6328, out);
  9925. if (XMEMCMP(p3, resultP, sizeof(p3)))
  9926. ERROR_OUT(-6329, out);
  9927. #endif /* HAVE_AES_DECRYPT */
  9928. #endif /* WOLFSSL_AES_128 */
  9929. #endif /* ENABLE_NON_12BYTE_IV_TEST */
  9930. #if defined(WOLFSSL_AES_256) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  9931. !defined(WOLFSSL_XILINX_CRYPT) && \
  9932. !(defined(WOLF_CRYPTO_CB) && \
  9933. defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC))
  9934. XMEMSET(resultT, 0, sizeof(resultT));
  9935. XMEMSET(resultC, 0, sizeof(resultC));
  9936. XMEMSET(resultP, 0, sizeof(resultP));
  9937. wc_AesGcmSetKey(enc, k1, sizeof(k1));
  9938. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9939. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  9940. resultT + 1, sizeof(t1) - 1, a, sizeof(a));
  9941. #if defined(WOLFSSL_ASYNC_CRYPT)
  9942. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9943. #endif
  9944. if (result != 0)
  9945. ERROR_OUT(-6330, out);
  9946. if (XMEMCMP(c1, resultC, sizeof(c1)))
  9947. ERROR_OUT(-6331, out);
  9948. if (XMEMCMP(t1, resultT + 1, sizeof(t1) - 1))
  9949. ERROR_OUT(-6332, out);
  9950. #ifdef HAVE_AES_DECRYPT
  9951. result = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(p),
  9952. iv1, sizeof(iv1), resultT + 1, sizeof(t1) - 1, a, sizeof(a));
  9953. #if defined(WOLFSSL_ASYNC_CRYPT)
  9954. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9955. #endif
  9956. if (result != 0)
  9957. ERROR_OUT(-6333, out);
  9958. if (XMEMCMP(p, resultP, sizeof(p)))
  9959. ERROR_OUT(-6334, out);
  9960. #endif /* HAVE_AES_DECRYPT */
  9961. #endif /* WOLFSSL_AES_256 */
  9962. #if !defined(HAVE_FIPS) || \
  9963. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  9964. /* Test encrypt with internally generated IV */
  9965. #if defined(WOLFSSL_AES_256) && !(defined(WC_NO_RNG) || defined(HAVE_SELFTEST)) \
  9966. && !(defined(WOLF_CRYPTO_CB) && defined(HAVE_CAVIUM_OCTEON_SYNC))
  9967. {
  9968. WC_RNG rng;
  9969. byte randIV[12];
  9970. result = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  9971. if (result != 0)
  9972. ERROR_OUT(-6335, out);
  9973. XMEMSET(randIV, 0, sizeof(randIV));
  9974. XMEMSET(resultT, 0, sizeof(resultT));
  9975. XMEMSET(resultC, 0, sizeof(resultC));
  9976. XMEMSET(resultP, 0, sizeof(resultP));
  9977. wc_AesGcmSetKey(enc, k1, sizeof(k1));
  9978. result = wc_AesGcmSetIV(enc, sizeof(randIV), NULL, 0, &rng);
  9979. if (result != 0)
  9980. ERROR_OUT(-6336, out);
  9981. result = wc_AesGcmEncrypt_ex(enc,
  9982. resultC, p, sizeof(p),
  9983. randIV, sizeof(randIV),
  9984. resultT, sizeof(t1),
  9985. a, sizeof(a));
  9986. #if defined(WOLFSSL_ASYNC_CRYPT)
  9987. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9988. #endif
  9989. if (result != 0)
  9990. ERROR_OUT(-6337, out);
  9991. /* Check the IV has been set. */
  9992. {
  9993. word32 i, ivSum = 0;
  9994. for (i = 0; i < sizeof(randIV); i++)
  9995. ivSum += randIV[i];
  9996. if (ivSum == 0)
  9997. ERROR_OUT(-6338, out);
  9998. }
  9999. #ifdef HAVE_AES_DECRYPT
  10000. wc_AesGcmSetKey(dec, k1, sizeof(k1));
  10001. result = wc_AesGcmSetIV(dec, sizeof(randIV), NULL, 0, &rng);
  10002. if (result != 0)
  10003. ERROR_OUT(-6339, out);
  10004. result = wc_AesGcmDecrypt(dec,
  10005. resultP, resultC, sizeof(c1),
  10006. randIV, sizeof(randIV),
  10007. resultT, sizeof(t1),
  10008. a, sizeof(a));
  10009. #if defined(WOLFSSL_ASYNC_CRYPT)
  10010. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  10011. #endif
  10012. if (result != 0)
  10013. ERROR_OUT(-6340, out);
  10014. if (XMEMCMP(p, resultP, sizeof(p)))
  10015. ERROR_OUT(-6341, out);
  10016. #endif /* HAVE_AES_DECRYPT */
  10017. wc_FreeRng(&rng);
  10018. }
  10019. #endif /* WOLFSSL_AES_256 && !(WC_NO_RNG || HAVE_SELFTEST) */
  10020. #endif /* HAVE_FIPS_VERSION >= 2 */
  10021. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  10022. #ifdef WOLFSSL_AES_256
  10023. #ifdef WOLFSSL_AESGCM_STREAM
  10024. result = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  10025. if (result != 0)
  10026. ERROR_OUT(-6360, out);
  10027. result = wc_AesGcmEncryptUpdate(enc, resultC, p, sizeof(p), a, sizeof(a));
  10028. if (result != 0)
  10029. ERROR_OUT(-6361, out);
  10030. result = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  10031. if (result != 0)
  10032. ERROR_OUT(-6362, out);
  10033. if (XMEMCMP(resultC, c1, sizeof(c1)) != 0)
  10034. ERROR_OUT(-6363, out);
  10035. if (XMEMCMP(resultT, t1, sizeof(t1)) != 0)
  10036. ERROR_OUT(-6364, out);
  10037. #ifdef HAVE_AES_DECRYPT
  10038. result = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  10039. if (result != 0)
  10040. ERROR_OUT(-6370, out);
  10041. result = wc_AesGcmDecryptUpdate(enc, resultP, c1, sizeof(c1), a, sizeof(a));
  10042. if (result != 0)
  10043. ERROR_OUT(-6371, out);
  10044. result = wc_AesGcmDecryptFinal(enc, t1, sizeof(t1));
  10045. if (result != 0)
  10046. ERROR_OUT(-6372, out);
  10047. if (XMEMCMP(resultP, p, sizeof(p)) != 0)
  10048. ERROR_OUT(-6373, out);
  10049. #endif
  10050. /* alen is the size to pass in with each update. */
  10051. for (alen = 1; alen < AES_BLOCK_SIZE + 1; alen++) {
  10052. result = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  10053. if (result != 0)
  10054. ERROR_OUT(-6380, out);
  10055. /* plen is the offset into AAD to update with. */
  10056. for (plen = 0; plen < (int)sizeof(a); plen += alen) {
  10057. int len = sizeof(a) - plen;
  10058. if (len > alen) len = alen;
  10059. result = wc_AesGcmEncryptUpdate(enc, NULL, NULL, 0, a + plen, len);
  10060. if (result != 0)
  10061. ERROR_OUT(-6381, out);
  10062. }
  10063. /* plen is the offset into plaintext to update with. */
  10064. for (plen = 0; plen < (int)sizeof(p); plen += alen) {
  10065. int len = sizeof(p) - plen;
  10066. if (len > alen) len = alen;
  10067. result = wc_AesGcmEncryptUpdate(enc, resultC + plen, p + plen, len,
  10068. NULL, 0);
  10069. if (result != 0)
  10070. ERROR_OUT(-6382, out);
  10071. }
  10072. result = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  10073. if (result != 0)
  10074. ERROR_OUT(-6383, out);
  10075. if (XMEMCMP(resultC, c1, sizeof(c1)) != 0)
  10076. ERROR_OUT(-6384, out);
  10077. if (XMEMCMP(resultT, t1, sizeof(t1)) != 0)
  10078. ERROR_OUT(-6385, out);
  10079. }
  10080. #ifdef HAVE_AES_DECRYPT
  10081. for (alen = 1; alen < AES_BLOCK_SIZE + 1; alen++) {
  10082. result = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  10083. if (result != 0)
  10084. ERROR_OUT(-6390, out);
  10085. /* plen is the offset into AAD to update with. */
  10086. for (plen = 0; plen < (int)sizeof(a); plen += alen) {
  10087. int len = sizeof(a) - plen;
  10088. if (len > alen) len = alen;
  10089. result = wc_AesGcmDecryptUpdate(enc, NULL, NULL, 0, a + plen, len);
  10090. if (result != 0)
  10091. ERROR_OUT(-6391, out);
  10092. }
  10093. /* plen is the offset into cipher text to update with. */
  10094. for (plen = 0; plen < (int)sizeof(c1); plen += alen) {
  10095. int len = sizeof(c1) - plen;
  10096. if (len > alen) len = alen;
  10097. result = wc_AesGcmDecryptUpdate(enc, resultP + plen, c1 + plen, len,
  10098. NULL, 0);
  10099. if (result != 0)
  10100. ERROR_OUT(-6392, out);
  10101. }
  10102. result = wc_AesGcmDecryptFinal(enc, t1, sizeof(t1));
  10103. if (result != 0)
  10104. ERROR_OUT(-6393, out);
  10105. if (XMEMCMP(resultP, p, sizeof(p)) != 0)
  10106. ERROR_OUT(-6394, out);
  10107. }
  10108. #endif /* HAVE_AES_DECRYPT */
  10109. #ifdef BENCH_AESGCM_LARGE
  10110. /* setup test buffer */
  10111. result = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  10112. if (result != 0)
  10113. ERROR_OUT(-6360, out);
  10114. result = wc_AesGcmEncryptUpdate(enc, large_output, large_input,
  10115. BENCH_AESGCM_LARGE, a, sizeof(a));
  10116. if (result != 0)
  10117. ERROR_OUT(-6361, out);
  10118. result = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  10119. if (result != 0)
  10120. ERROR_OUT(-6362, out);
  10121. #ifdef HAVE_AES_DECRYPT
  10122. result = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  10123. if (result != 0)
  10124. ERROR_OUT(-6363, out);
  10125. result = wc_AesGcmDecryptUpdate(enc, large_outdec, large_output,
  10126. BENCH_AESGCM_LARGE, a, sizeof(a));
  10127. if (result != 0)
  10128. ERROR_OUT(-6364, out);
  10129. result = wc_AesGcmDecryptFinal(enc, resultT, sizeof(t1));
  10130. if (result != 0)
  10131. ERROR_OUT(-6365, out);
  10132. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  10133. ERROR_OUT(-6366, out);
  10134. #endif /* HAVE_AES_DECRYPT */
  10135. #endif /* BENCH_AESGCM_LARGE */
  10136. #endif /* WOLFSSL_AESGCM_STREAM */
  10137. #endif /* WOLFSSL_AES_256 */
  10138. #endif /* !WOLFSSL_AFALG_XILINX_AES && !WOLFSSL_XILINX_CRYPT */
  10139. wc_AesFree(enc);
  10140. wc_AesFree(dec);
  10141. ret = 0;
  10142. out:
  10143. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  10144. !defined(WOLFSSL_NO_MALLOC)
  10145. if (large_input)
  10146. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10147. if (large_output)
  10148. XFREE(large_output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10149. if (large_outdec)
  10150. XFREE(large_outdec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10151. #endif
  10152. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10153. if (enc)
  10154. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  10155. if (dec)
  10156. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  10157. #endif
  10158. return ret;
  10159. }
  10160. #ifdef WOLFSSL_AES_128
  10161. WOLFSSL_TEST_SUBROUTINE int gmac_test(void)
  10162. {
  10163. int ret;
  10164. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10165. Gmac *gmac;
  10166. #else
  10167. Gmac gmac[1];
  10168. #endif
  10169. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  10170. {
  10171. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  10172. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  10173. };
  10174. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  10175. {
  10176. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  10177. 0xe2, 0x8c, 0x8f, 0x16
  10178. };
  10179. WOLFSSL_SMALL_STACK_STATIC const byte a1[] =
  10180. {
  10181. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  10182. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  10183. };
  10184. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  10185. {
  10186. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  10187. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  10188. };
  10189. #if (!defined(HAVE_FIPS) || \
  10190. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
  10191. /* FIPS builds only allow 16-byte auth tags. */
  10192. /* This sample uses a 15-byte auth tag. */
  10193. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  10194. {
  10195. 0x40, 0xf7, 0xec, 0xb2, 0x52, 0x6d, 0xaa, 0xd4,
  10196. 0x74, 0x25, 0x1d, 0xf4, 0x88, 0x9e, 0xf6, 0x5b
  10197. };
  10198. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  10199. {
  10200. 0xee, 0x9c, 0x6e, 0x06, 0x15, 0x45, 0x45, 0x03,
  10201. 0x1a, 0x60, 0x24, 0xa7
  10202. };
  10203. WOLFSSL_SMALL_STACK_STATIC const byte a2[] =
  10204. {
  10205. 0x94, 0x81, 0x2c, 0x87, 0x07, 0x4e, 0x15, 0x18,
  10206. 0x34, 0xb8, 0x35, 0xaf, 0x1c, 0xa5, 0x7e, 0x56
  10207. };
  10208. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  10209. {
  10210. 0xc6, 0x81, 0x79, 0x8e, 0x3d, 0xda, 0xb0, 0x9f,
  10211. 0x8d, 0x83, 0xb0, 0xbb, 0x14, 0xb6, 0x91
  10212. };
  10213. #endif
  10214. byte tag[16];
  10215. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10216. if ((gmac = (Gmac *)XMALLOC(sizeof *gmac, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10217. return -6409;
  10218. #endif
  10219. XMEMSET(gmac, 0, sizeof *gmac); /* clear context */
  10220. (void)wc_AesInit(&gmac->aes, HEAP_HINT, INVALID_DEVID); /* Make sure devId updated */
  10221. XMEMSET(tag, 0, sizeof(tag));
  10222. wc_GmacSetKey(gmac, k1, sizeof(k1));
  10223. wc_GmacUpdate(gmac, iv1, sizeof(iv1), a1, sizeof(a1), tag, sizeof(t1));
  10224. if (XMEMCMP(t1, tag, sizeof(t1)) != 0)
  10225. ERROR_OUT(-6400, out);
  10226. #if (!defined(HAVE_FIPS) || \
  10227. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)) )
  10228. XMEMSET(tag, 0, sizeof(tag));
  10229. wc_GmacSetKey(gmac, k2, sizeof(k2));
  10230. wc_GmacUpdate(gmac, iv2, sizeof(iv2), a2, sizeof(a2), tag, sizeof(t2));
  10231. if (XMEMCMP(t2, tag, sizeof(t2)) != 0)
  10232. ERROR_OUT(-6401, out);
  10233. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && !defined(NO_AES_DECRYPT)
  10234. {
  10235. WOLFSSL_SMALL_STACK_STATIC const byte badT[] =
  10236. {
  10237. 0xde, 0xad, 0xbe, 0xef, 0x17, 0x2e, 0xd0, 0x43,
  10238. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  10239. };
  10240. WC_RNG rng;
  10241. byte iv[12];
  10242. #ifndef HAVE_FIPS
  10243. if (wc_InitRng_ex(&rng, HEAP_HINT, devId) != 0)
  10244. ERROR_OUT(-6402, out);
  10245. #else
  10246. if (wc_InitRng(&rng) != 0)
  10247. ERROR_OUT(-6403, out);
  10248. #endif
  10249. if (wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  10250. t1, sizeof(t1)) != 0)
  10251. ERROR_OUT(-6404, out);
  10252. if (wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  10253. badT, sizeof(badT)) != AES_GCM_AUTH_E)
  10254. ERROR_OUT(-6405, out);
  10255. if (wc_GmacVerify(k2, sizeof(k2), iv2, sizeof(iv2), a2, sizeof(a2),
  10256. t2, sizeof(t2)) != 0)
  10257. ERROR_OUT(-6406, out);
  10258. XMEMSET(tag, 0, sizeof(tag));
  10259. XMEMSET(iv, 0, sizeof(iv));
  10260. if (wc_Gmac(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  10261. tag, sizeof(tag), &rng) != 0)
  10262. ERROR_OUT(-6407, out);
  10263. if (wc_GmacVerify(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  10264. tag, sizeof(tag)) != 0)
  10265. ERROR_OUT(-6408, out);
  10266. wc_FreeRng(&rng);
  10267. }
  10268. #endif /* !WC_NO_RNG && !HAVE_SELFTEST && !NO_AES_DECRYPT */
  10269. #endif /* HAVE_FIPS */
  10270. ret = 0;
  10271. out:
  10272. wc_AesFree(&gmac->aes);
  10273. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10274. XFREE(gmac, HEAP_HINT, DYNAMIC_TYPE_AES);
  10275. #endif
  10276. return ret;
  10277. }
  10278. #endif /* WOLFSSL_AES_128 */
  10279. #endif /* HAVE_AESGCM */
  10280. #if defined(HAVE_AESCCM)
  10281. #if defined(WOLFSSL_AES_256)
  10282. static int aesccm_256_test(void)
  10283. {
  10284. int ret;
  10285. /* Test vectors from NIST AES CCM 256-bit CAST Example #1 */
  10286. WOLFSSL_SMALL_STACK_STATIC const byte in_key[32] = {
  10287. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  10288. 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
  10289. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  10290. 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F
  10291. };
  10292. WOLFSSL_SMALL_STACK_STATIC const byte in_nonce[7] = {
  10293. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
  10294. WOLFSSL_SMALL_STACK_STATIC const byte in_auth[8] = {
  10295. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  10296. WOLFSSL_SMALL_STACK_STATIC const byte in_plaintext[4] = {
  10297. 0x20, 0x21, 0x22, 0x23};
  10298. WOLFSSL_SMALL_STACK_STATIC const byte exp_ciphertext[4] = {
  10299. 0x8A, 0xB1, 0xA8, 0x74};
  10300. WOLFSSL_SMALL_STACK_STATIC const byte exp_tag[4] = {
  10301. 0x95, 0xFC, 0x08, 0x20};
  10302. byte output[sizeof(in_plaintext)];
  10303. byte atag[sizeof(exp_tag)];
  10304. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10305. Aes* aes = (Aes*)XMALLOC(sizeof(Aes), HEAP_HINT, DYNAMIC_TYPE_AES);
  10306. if (aes == NULL) {
  10307. return MEMORY_E;
  10308. }
  10309. #else
  10310. Aes aes[1];
  10311. #endif
  10312. ret = wc_AesInit(aes, HEAP_HINT, devId);
  10313. if (ret == 0) {
  10314. ret = wc_AesCcmSetKey(aes, in_key, sizeof(in_key));
  10315. }
  10316. if (ret == 0) {
  10317. ret = wc_AesCcmEncrypt(aes, output, in_plaintext, sizeof(in_plaintext),
  10318. in_nonce, sizeof(in_nonce),
  10319. atag, sizeof(atag),
  10320. in_auth, sizeof(in_auth));
  10321. }
  10322. /* Verify we produce the proper ciphertext and tag */
  10323. if (ret == 0 &&
  10324. (XMEMCMP(output, exp_ciphertext, sizeof(output)) ||
  10325. XMEMCMP(atag, exp_tag, sizeof(atag)))) {
  10326. ret = -1;
  10327. }
  10328. if (ret == 0) {
  10329. /* decrypt inline */
  10330. ret = wc_AesCcmDecrypt(aes, output, output, sizeof(output),
  10331. in_nonce, sizeof(in_nonce),
  10332. atag, sizeof(atag),
  10333. in_auth, sizeof(in_auth));
  10334. }
  10335. /* Verify decryption was successful */
  10336. if (ret == 0 &&
  10337. XMEMCMP(output, in_plaintext, sizeof(output))) {
  10338. ret = -1;
  10339. }
  10340. wc_AesFree(aes);
  10341. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10342. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  10343. #endif
  10344. return ret;
  10345. }
  10346. #endif /* WOLFSSL_AES_256 */
  10347. #if defined(WOLFSSL_AES_128)
  10348. static int aesccm_128_test(void)
  10349. {
  10350. int ret;
  10351. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10352. Aes *enc;
  10353. #else
  10354. Aes enc[1];
  10355. #endif
  10356. /* key */
  10357. WOLFSSL_SMALL_STACK_STATIC const byte k[] =
  10358. {
  10359. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  10360. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  10361. };
  10362. /* nonce */
  10363. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  10364. {
  10365. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  10366. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  10367. };
  10368. /* plaintext */
  10369. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  10370. {
  10371. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  10372. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10373. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  10374. };
  10375. /* plaintext - long */
  10376. WOLFSSL_SMALL_STACK_STATIC const byte pl[] =
  10377. {
  10378. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  10379. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10380. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  10381. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  10382. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  10383. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10384. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  10385. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  10386. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  10387. 0x50
  10388. };
  10389. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  10390. {
  10391. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  10392. };
  10393. /* ciphertext */
  10394. WOLFSSL_SMALL_STACK_STATIC const byte c[] =
  10395. {
  10396. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  10397. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  10398. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  10399. };
  10400. /* tag - authentication */
  10401. WOLFSSL_SMALL_STACK_STATIC const byte t[] =
  10402. {
  10403. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  10404. };
  10405. /* ciphertext - long */
  10406. WOLFSSL_SMALL_STACK_STATIC const byte cl[] =
  10407. {
  10408. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  10409. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  10410. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84, 0xe0,
  10411. 0x44, 0x2d, 0xbe, 0x25, 0xfa, 0x48, 0x2b, 0xa8,
  10412. 0x36, 0x0b, 0xbf, 0x01, 0xc0, 0x12, 0x45, 0xa4,
  10413. 0x82, 0x9f, 0x20, 0x6c, 0xc3, 0xd6, 0xae, 0x5b,
  10414. 0x54, 0x8d, 0xd0, 0xb1, 0x69, 0x2c, 0xec, 0x5e,
  10415. 0x95, 0xa5, 0x6b, 0x48, 0xc3, 0xc6, 0xc8, 0x9e,
  10416. 0xc7, 0x92, 0x98, 0x9d, 0x26, 0x7d, 0x2a, 0x10,
  10417. 0x0b
  10418. };
  10419. /* tag - authentication - long */
  10420. WOLFSSL_SMALL_STACK_STATIC const byte tl[] =
  10421. {
  10422. 0x89, 0xd8, 0xd2, 0x02, 0xc5, 0xcf, 0xae, 0xf4
  10423. };
  10424. /* tag - authentication - empty plaintext */
  10425. WOLFSSL_SMALL_STACK_STATIC const byte t_empty[] =
  10426. {
  10427. 0xe4, 0x28, 0x8a, 0xc3, 0x78, 0x00, 0x0f, 0xf5
  10428. };
  10429. byte t2[sizeof(t)];
  10430. byte p2[sizeof(p)];
  10431. byte c2[sizeof(c)];
  10432. byte iv2[sizeof(iv)];
  10433. byte pl2[sizeof(pl)];
  10434. byte cl2[sizeof(cl)];
  10435. byte tl2[sizeof(tl)];
  10436. byte t_empty2[sizeof(t_empty)];
  10437. int result;
  10438. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10439. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10440. return -6521;
  10441. #endif
  10442. XMEMSET(enc, 0, sizeof *enc); /* clear context */
  10443. XMEMSET(t2, 0, sizeof(t2));
  10444. XMEMSET(c2, 0, sizeof(c2));
  10445. XMEMSET(p2, 0, sizeof(p2));
  10446. result = wc_AesInit(enc, HEAP_HINT, devId);
  10447. if (result != 0)
  10448. ERROR_OUT(-6499, out);
  10449. result = wc_AesCcmSetKey(enc, k, sizeof(k));
  10450. if (result != 0)
  10451. ERROR_OUT(-6500, out);
  10452. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  10453. result = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  10454. t2, sizeof(t2), a, sizeof(a));
  10455. if (result != 0)
  10456. ERROR_OUT(-6501, out);
  10457. if (XMEMCMP(c, c2, sizeof(c2)))
  10458. ERROR_OUT(-6502, out);
  10459. if (XMEMCMP(t, t2, sizeof(t2)))
  10460. ERROR_OUT(-6503, out);
  10461. result = wc_AesCcmDecrypt(enc, p2, c2, sizeof(p2), iv, sizeof(iv),
  10462. t2, sizeof(t2), a, sizeof(a));
  10463. if (result != 0)
  10464. ERROR_OUT(-6504, out);
  10465. if (XMEMCMP(p, p2, sizeof(p2)))
  10466. ERROR_OUT(-6505, out);
  10467. /* Test the authentication failure */
  10468. t2[0]++; /* Corrupt the authentication tag. */
  10469. result = wc_AesCcmDecrypt(enc, p2, c, sizeof(p2), iv, sizeof(iv),
  10470. t2, sizeof(t2), a, sizeof(a));
  10471. if (result == 0)
  10472. ERROR_OUT(-6506, out);
  10473. /* Clear c2 to compare against p2. p2 should be set to zero in case of
  10474. * authentication fail. */
  10475. XMEMSET(c2, 0, sizeof(c2));
  10476. if (XMEMCMP(p2, c2, sizeof(p2)))
  10477. ERROR_OUT(-6507, out);
  10478. wc_AesFree(enc);
  10479. XMEMSET(enc, 0, sizeof(Aes)); /* clear context */
  10480. XMEMSET(t2, 0, sizeof(t2));
  10481. XMEMSET(c2, 0, sizeof(c2));
  10482. XMEMSET(p2, 0, sizeof(p2));
  10483. XMEMSET(iv2, 0, sizeof(iv2));
  10484. #ifndef HAVE_SELFTEST
  10485. /* selftest build does not have wc_AesCcmSetNonce() or
  10486. * wc_AesCcmEncrypt_ex() */
  10487. if (wc_AesCcmSetKey(enc, k, sizeof(k)) != 0)
  10488. ERROR_OUT(-6508, out);
  10489. if (wc_AesCcmSetNonce(enc, iv, sizeof(iv)) != 0)
  10490. ERROR_OUT(-6509, out);
  10491. if (wc_AesCcmEncrypt_ex(enc, c2, p, sizeof(c2), iv2, sizeof(iv2),
  10492. t2, sizeof(t2), a, sizeof(a)) != 0)
  10493. ERROR_OUT(-6510, out);
  10494. if (XMEMCMP(iv, iv2, sizeof(iv2)))
  10495. ERROR_OUT(-6511, out);
  10496. if (XMEMCMP(c, c2, sizeof(c2)))
  10497. ERROR_OUT(-6512, out);
  10498. if (XMEMCMP(t, t2, sizeof(t2)))
  10499. ERROR_OUT(-6513, out);
  10500. #endif
  10501. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10502. /* test fail on invalid IV sizes */
  10503. result = wc_AesCcmSetKey(enc, k, sizeof(k));
  10504. if (result != 0)
  10505. ERROR_OUT(-6514, out);
  10506. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  10507. result = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  10508. t2, 1, a, sizeof(a));
  10509. if (result == 0) {
  10510. ERROR_OUT(-6515, out);
  10511. }
  10512. #endif
  10513. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  10514. result = wc_AesCcmEncrypt(enc, cl2, pl, sizeof(cl2), iv, sizeof(iv),
  10515. tl2, sizeof(tl2), a, sizeof(a));
  10516. if (result != 0)
  10517. ERROR_OUT(-6516, out);
  10518. if (XMEMCMP(cl, cl2, sizeof(cl2)))
  10519. ERROR_OUT(-6517, out);
  10520. if (XMEMCMP(tl, tl2, sizeof(tl2)))
  10521. ERROR_OUT(-6518, out);
  10522. result = wc_AesCcmDecrypt(enc, pl2, cl2, sizeof(pl2), iv, sizeof(iv),
  10523. tl2, sizeof(tl2), a, sizeof(a));
  10524. if (result != 0)
  10525. ERROR_OUT(-6519, out);
  10526. if (XMEMCMP(pl, pl2, sizeof(pl2)))
  10527. ERROR_OUT(-6520, out);
  10528. /* test empty message as null input or output with nonzero inSz. */
  10529. result = wc_AesCcmEncrypt(enc, pl2 /* out */, NULL /* in */, 1 /* inSz */,
  10530. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  10531. a, sizeof(a));
  10532. if (result != BAD_FUNC_ARG)
  10533. ERROR_OUT(-6527, out);
  10534. result = wc_AesCcmEncrypt(enc, NULL /* out */, (const byte *)"" /* in */, 1 /* inSz */,
  10535. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  10536. a, sizeof(a));
  10537. if (result != BAD_FUNC_ARG)
  10538. ERROR_OUT(-6528, out);
  10539. result = wc_AesCcmDecrypt(enc, pl2, NULL /* in */, 1 /* inSz */,
  10540. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  10541. sizeof(a));
  10542. if (result != BAD_FUNC_ARG)
  10543. ERROR_OUT(-6529, out);
  10544. result = wc_AesCcmDecrypt(enc, NULL /* out */, (const byte *)"" /* in */, 1 /* inSz */,
  10545. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  10546. sizeof(a));
  10547. if (result != BAD_FUNC_ARG)
  10548. ERROR_OUT(-6530, out);
  10549. /* test empty message as null input and output with zero inSz --
  10550. * must either succeed, or fail early with BAD_FUNC_ARG.
  10551. */
  10552. result = wc_AesCcmEncrypt(enc, NULL /* out */, NULL /* in */, 0 /* inSz */,
  10553. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  10554. a, sizeof(a));
  10555. if (result != BAD_FUNC_ARG) {
  10556. if (result != 0)
  10557. ERROR_OUT(-6521, out);
  10558. if (XMEMCMP(t_empty, t_empty2, sizeof(t_empty2)))
  10559. ERROR_OUT(-6522, out);
  10560. result = wc_AesCcmDecrypt(enc, NULL /* out */, NULL /* in */,
  10561. 0 /* inSz */, iv, sizeof(iv), t_empty2,
  10562. sizeof(t_empty2), a, sizeof(a));
  10563. if (result != 0)
  10564. ERROR_OUT(-6523, out);
  10565. }
  10566. /* test empty message as zero-length string -- must work. */
  10567. result = wc_AesCcmEncrypt(enc, pl2, (const byte *)"", 0 /* inSz */, iv,
  10568. sizeof(iv), t_empty2, sizeof(t_empty2), a,
  10569. sizeof(a));
  10570. if (result != 0)
  10571. ERROR_OUT(-6524, out);
  10572. if (XMEMCMP(t_empty, t_empty2, sizeof(t_empty2)))
  10573. ERROR_OUT(-6525, out);
  10574. result = wc_AesCcmDecrypt(enc, pl2, (const byte *)"", 0 /* inSz */,
  10575. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  10576. sizeof(a));
  10577. if (result != 0)
  10578. ERROR_OUT(-6526, out);
  10579. wc_AesFree(enc);
  10580. ret = 0;
  10581. out:
  10582. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10583. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  10584. #endif
  10585. return ret;
  10586. }
  10587. #endif /* WOLFSSL_AES_128 */
  10588. WOLFSSL_TEST_SUBROUTINE int aesccm_test(void)
  10589. {
  10590. int ret = 0;
  10591. #ifdef WOLFSSL_AES_128
  10592. if (ret == 0)
  10593. ret = aesccm_128_test();
  10594. #endif
  10595. #ifdef WOLFSSL_AES_256
  10596. if (ret == 0)
  10597. ret = aesccm_256_test();
  10598. #endif
  10599. return ret;
  10600. }
  10601. #endif /* HAVE_AESCCM */
  10602. #ifdef HAVE_AES_KEYWRAP
  10603. #define MAX_KEYWRAP_TEST_OUTLEN 40
  10604. #define MAX_KEYWRAP_TEST_PLAINLEN 32
  10605. typedef struct keywrapVector {
  10606. const byte* kek;
  10607. const byte* data;
  10608. const byte* verify;
  10609. word32 kekLen;
  10610. word32 dataLen;
  10611. word32 verifyLen;
  10612. } keywrapVector;
  10613. WOLFSSL_TEST_SUBROUTINE int aeskeywrap_test(void)
  10614. {
  10615. int wrapSz, plainSz, testSz, i;
  10616. /* test vectors from RFC 3394 (kek, data, verify) */
  10617. #ifdef WOLFSSL_AES_128
  10618. /* Wrap 128 bits of Key Data with a 128-bit KEK */
  10619. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  10620. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10621. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  10622. };
  10623. WOLFSSL_SMALL_STACK_STATIC const byte d1[] = {
  10624. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10625. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  10626. };
  10627. WOLFSSL_SMALL_STACK_STATIC const byte v1[] = {
  10628. 0x1F, 0xA6, 0x8B, 0x0A, 0x81, 0x12, 0xB4, 0x47,
  10629. 0xAE, 0xF3, 0x4B, 0xD8, 0xFB, 0x5A, 0x7B, 0x82,
  10630. 0x9D, 0x3E, 0x86, 0x23, 0x71, 0xD2, 0xCF, 0xE5
  10631. };
  10632. #endif /* WOLFSSL_AES_128 */
  10633. #ifdef WOLFSSL_AES_192
  10634. /* Wrap 128 bits of Key Data with a 192-bit KEK */
  10635. WOLFSSL_SMALL_STACK_STATIC const byte k2[] = {
  10636. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10637. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  10638. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  10639. };
  10640. WOLFSSL_SMALL_STACK_STATIC const byte d2[] = {
  10641. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10642. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  10643. };
  10644. WOLFSSL_SMALL_STACK_STATIC const byte v2[] = {
  10645. 0x96, 0x77, 0x8B, 0x25, 0xAE, 0x6C, 0xA4, 0x35,
  10646. 0xF9, 0x2B, 0x5B, 0x97, 0xC0, 0x50, 0xAE, 0xD2,
  10647. 0x46, 0x8A, 0xB8, 0xA1, 0x7A, 0xD8, 0x4E, 0x5D
  10648. };
  10649. #endif
  10650. #ifdef WOLFSSL_AES_256
  10651. /* Wrap 128 bits of Key Data with a 256-bit KEK */
  10652. WOLFSSL_SMALL_STACK_STATIC const byte k3[] = {
  10653. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10654. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  10655. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10656. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  10657. };
  10658. WOLFSSL_SMALL_STACK_STATIC const byte d3[] = {
  10659. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10660. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  10661. };
  10662. WOLFSSL_SMALL_STACK_STATIC const byte v3[] = {
  10663. 0x64, 0xE8, 0xC3, 0xF9, 0xCE, 0x0F, 0x5B, 0xA2,
  10664. 0x63, 0xE9, 0x77, 0x79, 0x05, 0x81, 0x8A, 0x2A,
  10665. 0x93, 0xC8, 0x19, 0x1E, 0x7D, 0x6E, 0x8A, 0xE7
  10666. };
  10667. #endif
  10668. #ifdef WOLFSSL_AES_192
  10669. /* Wrap 192 bits of Key Data with a 192-bit KEK */
  10670. WOLFSSL_SMALL_STACK_STATIC const byte k4[] = {
  10671. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10672. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  10673. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  10674. };
  10675. WOLFSSL_SMALL_STACK_STATIC const byte d4[] = {
  10676. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10677. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  10678. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  10679. };
  10680. WOLFSSL_SMALL_STACK_STATIC const byte v4[] = {
  10681. 0x03, 0x1D, 0x33, 0x26, 0x4E, 0x15, 0xD3, 0x32,
  10682. 0x68, 0xF2, 0x4E, 0xC2, 0x60, 0x74, 0x3E, 0xDC,
  10683. 0xE1, 0xC6, 0xC7, 0xDD, 0xEE, 0x72, 0x5A, 0x93,
  10684. 0x6B, 0xA8, 0x14, 0x91, 0x5C, 0x67, 0x62, 0xD2
  10685. };
  10686. #endif
  10687. #ifdef WOLFSSL_AES_256
  10688. /* Wrap 192 bits of Key Data with a 256-bit KEK */
  10689. WOLFSSL_SMALL_STACK_STATIC const byte k5[] = {
  10690. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10691. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  10692. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10693. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  10694. };
  10695. WOLFSSL_SMALL_STACK_STATIC const byte d5[] = {
  10696. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10697. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  10698. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  10699. };
  10700. WOLFSSL_SMALL_STACK_STATIC const byte v5[] = {
  10701. 0xA8, 0xF9, 0xBC, 0x16, 0x12, 0xC6, 0x8B, 0x3F,
  10702. 0xF6, 0xE6, 0xF4, 0xFB, 0xE3, 0x0E, 0x71, 0xE4,
  10703. 0x76, 0x9C, 0x8B, 0x80, 0xA3, 0x2C, 0xB8, 0x95,
  10704. 0x8C, 0xD5, 0xD1, 0x7D, 0x6B, 0x25, 0x4D, 0xA1
  10705. };
  10706. /* Wrap 256 bits of Key Data with a 256-bit KEK */
  10707. WOLFSSL_SMALL_STACK_STATIC const byte k6[] = {
  10708. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10709. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  10710. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10711. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  10712. };
  10713. WOLFSSL_SMALL_STACK_STATIC const byte d6[] = {
  10714. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10715. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  10716. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10717. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  10718. };
  10719. WOLFSSL_SMALL_STACK_STATIC const byte v6[] = {
  10720. 0x28, 0xC9, 0xF4, 0x04, 0xC4, 0xB8, 0x10, 0xF4,
  10721. 0xCB, 0xCC, 0xB3, 0x5C, 0xFB, 0x87, 0xF8, 0x26,
  10722. 0x3F, 0x57, 0x86, 0xE2, 0xD8, 0x0E, 0xD3, 0x26,
  10723. 0xCB, 0xC7, 0xF0, 0xE7, 0x1A, 0x99, 0xF4, 0x3B,
  10724. 0xFB, 0x98, 0x8B, 0x9B, 0x7A, 0x02, 0xDD, 0x21
  10725. };
  10726. #endif /* WOLFSSL_AES_256 */
  10727. byte output[MAX_KEYWRAP_TEST_OUTLEN];
  10728. byte plain [MAX_KEYWRAP_TEST_PLAINLEN];
  10729. const keywrapVector test_wrap[] =
  10730. {
  10731. #ifdef WOLFSSL_AES_128
  10732. {k1, d1, v1, sizeof(k1), sizeof(d1), sizeof(v1)},
  10733. #endif
  10734. #ifdef WOLFSSL_AES_192
  10735. {k2, d2, v2, sizeof(k2), sizeof(d2), sizeof(v2)},
  10736. #endif
  10737. #ifdef WOLFSSL_AES_256
  10738. {k3, d3, v3, sizeof(k3), sizeof(d3), sizeof(v3)},
  10739. #endif
  10740. #ifdef WOLFSSL_AES_192
  10741. {k4, d4, v4, sizeof(k4), sizeof(d4), sizeof(v4)},
  10742. #endif
  10743. #ifdef WOLFSSL_AES_256
  10744. {k5, d5, v5, sizeof(k5), sizeof(d5), sizeof(v5)},
  10745. {k6, d6, v6, sizeof(k6), sizeof(d6), sizeof(v6)}
  10746. #endif
  10747. };
  10748. testSz = sizeof(test_wrap) / sizeof(keywrapVector);
  10749. XMEMSET(output, 0, sizeof(output));
  10750. XMEMSET(plain, 0, sizeof(plain));
  10751. for (i = 0; i < testSz; i++) {
  10752. wrapSz = wc_AesKeyWrap(test_wrap[i].kek, test_wrap[i].kekLen,
  10753. test_wrap[i].data, test_wrap[i].dataLen,
  10754. output, sizeof(output), NULL);
  10755. if ( (wrapSz < 0) || (wrapSz != (int)test_wrap[i].verifyLen) )
  10756. return -6600;
  10757. if (XMEMCMP(output, test_wrap[i].verify, test_wrap[i].verifyLen) != 0)
  10758. return -6601;
  10759. plainSz = wc_AesKeyUnWrap((byte*)test_wrap[i].kek, test_wrap[i].kekLen,
  10760. output, wrapSz,
  10761. plain, sizeof(plain), NULL);
  10762. if ( (plainSz < 0) || (plainSz != (int)test_wrap[i].dataLen) )
  10763. return -6602;
  10764. if (XMEMCMP(plain, test_wrap[i].data, test_wrap[i].dataLen) != 0)
  10765. return -6603 - i;
  10766. }
  10767. return 0;
  10768. }
  10769. #endif /* HAVE_AES_KEYWRAP */
  10770. #endif /* NO_AES */
  10771. #ifdef HAVE_CAMELLIA
  10772. enum {
  10773. CAM_ECB_ENC, CAM_ECB_DEC, CAM_CBC_ENC, CAM_CBC_DEC
  10774. };
  10775. typedef struct {
  10776. int type;
  10777. const byte* plaintext;
  10778. const byte* iv;
  10779. const byte* ciphertext;
  10780. const byte* key;
  10781. word32 keySz;
  10782. int errorCode;
  10783. } test_vector_t;
  10784. WOLFSSL_TEST_SUBROUTINE int camellia_test(void)
  10785. {
  10786. /* Camellia ECB Test Plaintext */
  10787. WOLFSSL_SMALL_STACK_STATIC const byte pte[] =
  10788. {
  10789. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10790. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  10791. };
  10792. /* Camellia ECB Test Initialization Vector */
  10793. WOLFSSL_SMALL_STACK_STATIC const byte ive[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  10794. /* Test 1: Camellia ECB 128-bit key */
  10795. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  10796. {
  10797. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10798. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  10799. };
  10800. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  10801. {
  10802. 0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73,
  10803. 0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43
  10804. };
  10805. /* Test 2: Camellia ECB 192-bit key */
  10806. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  10807. {
  10808. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10809. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  10810. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  10811. };
  10812. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  10813. {
  10814. 0xb4, 0x99, 0x34, 0x01, 0xb3, 0xe9, 0x96, 0xf8,
  10815. 0x4e, 0xe5, 0xce, 0xe7, 0xd7, 0x9b, 0x09, 0xb9
  10816. };
  10817. /* Test 3: Camellia ECB 256-bit key */
  10818. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  10819. {
  10820. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10821. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  10822. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10823. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  10824. };
  10825. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  10826. {
  10827. 0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c,
  10828. 0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09
  10829. };
  10830. /* Camellia CBC Test Plaintext */
  10831. WOLFSSL_SMALL_STACK_STATIC const byte ptc[] =
  10832. {
  10833. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  10834. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  10835. };
  10836. /* Camellia CBC Test Initialization Vector */
  10837. WOLFSSL_SMALL_STACK_STATIC const byte ivc[] =
  10838. {
  10839. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10840. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  10841. };
  10842. /* Test 4: Camellia-CBC 128-bit key */
  10843. WOLFSSL_SMALL_STACK_STATIC const byte k4[] =
  10844. {
  10845. 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
  10846. 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
  10847. };
  10848. WOLFSSL_SMALL_STACK_STATIC const byte c4[] =
  10849. {
  10850. 0x16, 0x07, 0xCF, 0x49, 0x4B, 0x36, 0xBB, 0xF0,
  10851. 0x0D, 0xAE, 0xB0, 0xB5, 0x03, 0xC8, 0x31, 0xAB
  10852. };
  10853. /* Test 5: Camellia-CBC 192-bit key */
  10854. WOLFSSL_SMALL_STACK_STATIC const byte k5[] =
  10855. {
  10856. 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52,
  10857. 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5,
  10858. 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B
  10859. };
  10860. WOLFSSL_SMALL_STACK_STATIC const byte c5[] =
  10861. {
  10862. 0x2A, 0x48, 0x30, 0xAB, 0x5A, 0xC4, 0xA1, 0xA2,
  10863. 0x40, 0x59, 0x55, 0xFD, 0x21, 0x95, 0xCF, 0x93
  10864. };
  10865. /* Test 6: CBC 256-bit key */
  10866. WOLFSSL_SMALL_STACK_STATIC const byte k6[] =
  10867. {
  10868. 0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE,
  10869. 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
  10870. 0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7,
  10871. 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4
  10872. };
  10873. WOLFSSL_SMALL_STACK_STATIC const byte c6[] =
  10874. {
  10875. 0xE6, 0xCF, 0xA3, 0x5F, 0xC0, 0x2B, 0x13, 0x4A,
  10876. 0x4D, 0x2C, 0x0B, 0x67, 0x37, 0xAC, 0x3E, 0xDA
  10877. };
  10878. byte out[CAMELLIA_BLOCK_SIZE];
  10879. Camellia cam;
  10880. int i, testsSz, ret;
  10881. WOLFSSL_SMALL_STACK_STATIC const test_vector_t testVectors[] =
  10882. {
  10883. {CAM_ECB_ENC, pte, ive, c1, k1, sizeof(k1), -114},
  10884. {CAM_ECB_ENC, pte, ive, c2, k2, sizeof(k2), -115},
  10885. {CAM_ECB_ENC, pte, ive, c3, k3, sizeof(k3), -116},
  10886. {CAM_ECB_DEC, pte, ive, c1, k1, sizeof(k1), -117},
  10887. {CAM_ECB_DEC, pte, ive, c2, k2, sizeof(k2), -118},
  10888. {CAM_ECB_DEC, pte, ive, c3, k3, sizeof(k3), -119},
  10889. {CAM_CBC_ENC, ptc, ivc, c4, k4, sizeof(k4), -120},
  10890. {CAM_CBC_ENC, ptc, ivc, c5, k5, sizeof(k5), -121},
  10891. {CAM_CBC_ENC, ptc, ivc, c6, k6, sizeof(k6), -122},
  10892. {CAM_CBC_DEC, ptc, ivc, c4, k4, sizeof(k4), -123},
  10893. {CAM_CBC_DEC, ptc, ivc, c5, k5, sizeof(k5), -124},
  10894. {CAM_CBC_DEC, ptc, ivc, c6, k6, sizeof(k6), -125}
  10895. };
  10896. testsSz = sizeof(testVectors)/sizeof(test_vector_t);
  10897. for (i = 0; i < testsSz; i++) {
  10898. if (wc_CamelliaSetKey(&cam, testVectors[i].key, testVectors[i].keySz,
  10899. testVectors[i].iv) != 0)
  10900. return testVectors[i].errorCode;
  10901. switch (testVectors[i].type) {
  10902. case CAM_ECB_ENC:
  10903. ret = wc_CamelliaEncryptDirect(&cam, out,
  10904. testVectors[i].plaintext);
  10905. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  10906. CAMELLIA_BLOCK_SIZE))
  10907. return testVectors[i].errorCode;
  10908. break;
  10909. case CAM_ECB_DEC:
  10910. ret = wc_CamelliaDecryptDirect(&cam, out,
  10911. testVectors[i].ciphertext);
  10912. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  10913. CAMELLIA_BLOCK_SIZE))
  10914. return testVectors[i].errorCode;
  10915. break;
  10916. case CAM_CBC_ENC:
  10917. ret = wc_CamelliaCbcEncrypt(&cam, out, testVectors[i].plaintext,
  10918. CAMELLIA_BLOCK_SIZE);
  10919. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  10920. CAMELLIA_BLOCK_SIZE))
  10921. return testVectors[i].errorCode;
  10922. break;
  10923. case CAM_CBC_DEC:
  10924. ret = wc_CamelliaCbcDecrypt(&cam, out,
  10925. testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE);
  10926. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  10927. CAMELLIA_BLOCK_SIZE))
  10928. return testVectors[i].errorCode;
  10929. break;
  10930. default:
  10931. break;
  10932. }
  10933. }
  10934. /* Setting the IV and checking it was actually set. */
  10935. ret = wc_CamelliaSetIV(&cam, ivc);
  10936. if (ret != 0 || XMEMCMP(cam.reg, ivc, CAMELLIA_BLOCK_SIZE))
  10937. return -6700;
  10938. /* Setting the IV to NULL should be same as all zeros IV */
  10939. if (wc_CamelliaSetIV(&cam, NULL) != 0 ||
  10940. XMEMCMP(cam.reg, ive, CAMELLIA_BLOCK_SIZE))
  10941. return -6701;
  10942. /* First parameter should never be null */
  10943. if (wc_CamelliaSetIV(NULL, NULL) == 0)
  10944. return -6702;
  10945. /* First parameter should never be null, check it fails */
  10946. if (wc_CamelliaSetKey(NULL, k1, sizeof(k1), NULL) == 0)
  10947. return -6703;
  10948. /* Key should have a size of 16, 24, or 32 */
  10949. if (wc_CamelliaSetKey(&cam, k1, 0, NULL) == 0)
  10950. return -6704;
  10951. return 0;
  10952. }
  10953. #endif /* HAVE_CAMELLIA */
  10954. #ifdef HAVE_XCHACHA
  10955. WOLFSSL_TEST_SUBROUTINE int XChaCha_test(void) {
  10956. int ret = -6830;
  10957. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  10958. 0x54, 0x68, 0x65, 0x20, 0x64, 0x68, 0x6f, 0x6c, 0x65, 0x20, 0x28, 0x70, 0x72, 0x6f, 0x6e, 0x6f, /* The dhole (prono */
  10959. 0x75, 0x6e, 0x63, 0x65, 0x64, 0x20, 0x22, 0x64, 0x6f, 0x6c, 0x65, 0x22, 0x29, 0x20, 0x69, 0x73, /* unced "dole") is */
  10960. 0x20, 0x61, 0x6c, 0x73, 0x6f, 0x20, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x20, 0x61, 0x73, 0x20, 0x74, /* also known as t */
  10961. 0x68, 0x65, 0x20, 0x41, 0x73, 0x69, 0x61, 0x74, 0x69, 0x63, 0x20, 0x77, 0x69, 0x6c, 0x64, 0x20, /* he Asiatic wild */
  10962. 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x72, 0x65, 0x64, 0x20, 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x61, 0x6e, /* dog, red dog, an */
  10963. 0x64, 0x20, 0x77, 0x68, 0x69, 0x73, 0x74, 0x6c, 0x69, 0x6e, 0x67, 0x20, 0x64, 0x6f, 0x67, 0x2e, /* d whistling dog. */
  10964. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x61, 0x62, 0x6f, 0x75, 0x74, 0x20, 0x74, 0x68, 0x65, /* It is about the */
  10965. 0x20, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x6f, 0x66, 0x20, 0x61, 0x20, 0x47, 0x65, 0x72, 0x6d, 0x61, /* size of a Germa */
  10966. 0x6e, 0x20, 0x73, 0x68, 0x65, 0x70, 0x68, 0x65, 0x72, 0x64, 0x20, 0x62, 0x75, 0x74, 0x20, 0x6c, /* n shepherd but l */
  10967. 0x6f, 0x6f, 0x6b, 0x73, 0x20, 0x6d, 0x6f, 0x72, 0x65, 0x20, 0x6c, 0x69, 0x6b, 0x65, 0x20, 0x61, /* ooks more like a */
  10968. 0x20, 0x6c, 0x6f, 0x6e, 0x67, 0x2d, 0x6c, 0x65, 0x67, 0x67, 0x65, 0x64, 0x20, 0x66, 0x6f, 0x78, /* long-legged fox */
  10969. 0x2e, 0x20, 0x54, 0x68, 0x69, 0x73, 0x20, 0x68, 0x69, 0x67, 0x68, 0x6c, 0x79, 0x20, 0x65, 0x6c, /* . This highly el */
  10970. 0x75, 0x73, 0x69, 0x76, 0x65, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x6b, 0x69, 0x6c, 0x6c, 0x65, /* usive and skille */
  10971. 0x64, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x65, 0x72, 0x20, 0x69, 0x73, 0x20, 0x63, 0x6c, 0x61, 0x73, /* d jumper is clas */
  10972. 0x73, 0x69, 0x66, 0x69, 0x65, 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x77, 0x6f, 0x6c, 0x76, /* sified with wolv */
  10973. 0x65, 0x73, 0x2c, 0x20, 0x63, 0x6f, 0x79, 0x6f, 0x74, 0x65, 0x73, 0x2c, 0x20, 0x6a, 0x61, 0x63, /* es, coyotes, jac */
  10974. 0x6b, 0x61, 0x6c, 0x73, 0x2c, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x66, 0x6f, 0x78, 0x65, 0x73, 0x20, /* kals, and foxes */
  10975. 0x69, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74, 0x61, 0x78, 0x6f, 0x6e, 0x6f, 0x6d, 0x69, 0x63, /* in the taxonomic */
  10976. 0x20, 0x66, 0x61, 0x6d, 0x69, 0x6c, 0x79, 0x20, 0x43, 0x61, 0x6e, 0x69, 0x64, 0x61, 0x65, 0x2e /* family Canidae. */
  10977. };
  10978. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  10979. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  10980. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  10981. };
  10982. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  10983. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  10984. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x58 }; /* PQRSTUVW */
  10985. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  10986. 0x45, 0x59, 0xab, 0xba, 0x4e, 0x48, 0xc1, 0x61, 0x02, 0xe8, 0xbb, 0x2c, 0x05, 0xe6, 0x94, 0x7f,
  10987. 0x50, 0xa7, 0x86, 0xde, 0x16, 0x2f, 0x9b, 0x0b, 0x7e, 0x59, 0x2a, 0x9b, 0x53, 0xd0, 0xd4, 0xe9,
  10988. 0x8d, 0x8d, 0x64, 0x10, 0xd5, 0x40, 0xa1, 0xa6, 0x37, 0x5b, 0x26, 0xd8, 0x0d, 0xac, 0xe4, 0xfa,
  10989. 0xb5, 0x23, 0x84, 0xc7, 0x31, 0xac, 0xbf, 0x16, 0xa5, 0x92, 0x3c, 0x0c, 0x48, 0xd3, 0x57, 0x5d,
  10990. 0x4d, 0x0d, 0x2c, 0x67, 0x3b, 0x66, 0x6f, 0xaa, 0x73, 0x10, 0x61, 0x27, 0x77, 0x01, 0x09, 0x3a,
  10991. 0x6b, 0xf7, 0xa1, 0x58, 0xa8, 0x86, 0x42, 0x92, 0xa4, 0x1c, 0x48, 0xe3, 0xa9, 0xb4, 0xc0, 0xda,
  10992. 0xec, 0xe0, 0xf8, 0xd9, 0x8d, 0x0d, 0x7e, 0x05, 0xb3, 0x7a, 0x30, 0x7b, 0xbb, 0x66, 0x33, 0x31,
  10993. 0x64, 0xec, 0x9e, 0x1b, 0x24, 0xea, 0x0d, 0x6c, 0x3f, 0xfd, 0xdc, 0xec, 0x4f, 0x68, 0xe7, 0x44,
  10994. 0x30, 0x56, 0x19, 0x3a, 0x03, 0xc8, 0x10, 0xe1, 0x13, 0x44, 0xca, 0x06, 0xd8, 0xed, 0x8a, 0x2b,
  10995. 0xfb, 0x1e, 0x8d, 0x48, 0xcf, 0xa6, 0xbc, 0x0e, 0xb4, 0xe2, 0x46, 0x4b, 0x74, 0x81, 0x42, 0x40,
  10996. 0x7c, 0x9f, 0x43, 0x1a, 0xee, 0x76, 0x99, 0x60, 0xe1, 0x5b, 0xa8, 0xb9, 0x68, 0x90, 0x46, 0x6e,
  10997. 0xf2, 0x45, 0x75, 0x99, 0x85, 0x23, 0x85, 0xc6, 0x61, 0xf7, 0x52, 0xce, 0x20, 0xf9, 0xda, 0x0c,
  10998. 0x09, 0xab, 0x6b, 0x19, 0xdf, 0x74, 0xe7, 0x6a, 0x95, 0x96, 0x74, 0x46, 0xf8, 0xd0, 0xfd, 0x41,
  10999. 0x5e, 0x7b, 0xee, 0x2a, 0x12, 0xa1, 0x14, 0xc2, 0x0e, 0xb5, 0x29, 0x2a, 0xe7, 0xa3, 0x49, 0xae,
  11000. 0x57, 0x78, 0x20, 0xd5, 0x52, 0x0a, 0x1f, 0x3f, 0xb6, 0x2a, 0x17, 0xce, 0x6a, 0x7e, 0x68, 0xfa,
  11001. 0x7c, 0x79, 0x11, 0x1d, 0x88, 0x60, 0x92, 0x0b, 0xc0, 0x48, 0xef, 0x43, 0xfe, 0x84, 0x48, 0x6c,
  11002. 0xcb, 0x87, 0xc2, 0x5f, 0x0a, 0xe0, 0x45, 0xf0, 0xcc, 0xe1, 0xe7, 0x98, 0x9a, 0x9a, 0xa2, 0x20,
  11003. 0xa2, 0x8b, 0xdd, 0x48, 0x27, 0xe7, 0x51, 0xa2, 0x4a, 0x6d, 0x5c, 0x62, 0xd7, 0x90, 0xa6, 0x63,
  11004. 0x93, 0xb9, 0x31, 0x11, 0xc1, 0xa5, 0x5d, 0xd7, 0x42, 0x1a, 0x10, 0x18, 0x49, 0x74, 0xc7, 0xc5
  11005. };
  11006. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11007. struct ChaCha *chacha = (struct ChaCha *)XMALLOC(sizeof *chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  11008. byte *buf1 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11009. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11010. if ((chacha == NULL) || (buf1 == NULL) || (buf2 == NULL))
  11011. ERROR_OUT(MEMORY_E, out);
  11012. #else
  11013. struct ChaCha chacha[1];
  11014. byte buf1[sizeof Plaintext];
  11015. byte buf2[sizeof Plaintext];
  11016. #endif
  11017. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  11018. if (ret < 0)
  11019. ERROR_OUT(-6831, out);
  11020. ret = wc_Chacha_Process(chacha, buf1, Plaintext, sizeof Plaintext);
  11021. if (ret < 0)
  11022. ERROR_OUT(-6832, out);
  11023. if (XMEMCMP(buf1, Ciphertext, sizeof Plaintext))
  11024. ERROR_OUT(-6833, out);
  11025. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  11026. if (ret < 0)
  11027. ERROR_OUT(-6834, out);
  11028. ret = wc_Chacha_Process(chacha, buf2, buf1, sizeof Plaintext);
  11029. if (ret < 0)
  11030. ERROR_OUT(-6835, out);
  11031. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  11032. ERROR_OUT(-6836, out);
  11033. out:
  11034. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11035. if (chacha)
  11036. XFREE(chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  11037. if (buf1)
  11038. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11039. if (buf2)
  11040. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11041. #endif
  11042. return ret;
  11043. }
  11044. #endif /* HAVE_XCHACHA */
  11045. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  11046. WOLFSSL_TEST_SUBROUTINE int XChaCha20Poly1305_test(void) {
  11047. int ret;
  11048. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  11049. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c, /* Ladies and Gentl */
  11050. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73, /* emen of the clas */
  11051. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39, 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63, /* s of '99: If I c */
  11052. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66, 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f, /* ould offer you o */
  11053. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20, 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20, /* nly one tip for */
  11054. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75, 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73, /* the future, suns */
  11055. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69, /* creen would be i */
  11056. 0x74, 0x2e }; /* t. */
  11057. WOLFSSL_SMALL_STACK_STATIC const byte AAD[] = { 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7 }; /* PQRS........ */
  11058. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  11059. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  11060. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  11061. };
  11062. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  11063. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  11064. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57 }; /* PQRSTUVW */
  11065. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  11066. 0xbd, 0x6d, 0x17, 0x9d, 0x3e, 0x83, 0xd4, 0x3b, 0x95, 0x76, 0x57, 0x94, 0x93, 0xc0, 0xe9, 0x39,
  11067. 0x57, 0x2a, 0x17, 0x00, 0x25, 0x2b, 0xfa, 0xcc, 0xbe, 0xd2, 0x90, 0x2c, 0x21, 0x39, 0x6c, 0xbb,
  11068. 0x73, 0x1c, 0x7f, 0x1b, 0x0b, 0x4a, 0xa6, 0x44, 0x0b, 0xf3, 0xa8, 0x2f, 0x4e, 0xda, 0x7e, 0x39,
  11069. 0xae, 0x64, 0xc6, 0x70, 0x8c, 0x54, 0xc2, 0x16, 0xcb, 0x96, 0xb7, 0x2e, 0x12, 0x13, 0xb4, 0x52,
  11070. 0x2f, 0x8c, 0x9b, 0xa4, 0x0d, 0xb5, 0xd9, 0x45, 0xb1, 0x1b, 0x69, 0xb9, 0x82, 0xc1, 0xbb, 0x9e,
  11071. 0x3f, 0x3f, 0xac, 0x2b, 0xc3, 0x69, 0x48, 0x8f, 0x76, 0xb2, 0x38, 0x35, 0x65, 0xd3, 0xff, 0xf9,
  11072. 0x21, 0xf9, 0x66, 0x4c, 0x97, 0x63, 0x7d, 0xa9, 0x76, 0x88, 0x12, 0xf6, 0x15, 0xc6, 0x8b, 0x13,
  11073. 0xb5, 0x2e };
  11074. WOLFSSL_SMALL_STACK_STATIC const byte Tag[] = {
  11075. 0xc0, 0x87, 0x59, 0x24, 0xc1, 0xc7, 0x98, 0x79, 0x47, 0xde, 0xaf, 0xd8, 0x78, 0x0a, 0xcf, 0x49
  11076. };
  11077. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11078. byte *buf1 = (byte *)XMALLOC(sizeof Ciphertext + sizeof Tag, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11079. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11080. if ((buf1 == NULL) || (buf2 == NULL))
  11081. ERROR_OUT(-6480, out);
  11082. #else
  11083. byte buf1[sizeof Ciphertext + sizeof Tag];
  11084. byte buf2[sizeof Plaintext];
  11085. #endif
  11086. ret = wc_XChaCha20Poly1305_Encrypt(buf1, sizeof Ciphertext + sizeof Tag,
  11087. Plaintext, sizeof Plaintext,
  11088. AAD, sizeof AAD,
  11089. IV, sizeof IV,
  11090. Key, sizeof Key);
  11091. if (ret < 0)
  11092. ERROR_OUT(-6841, out);
  11093. if (XMEMCMP(buf1, Ciphertext, sizeof Ciphertext))
  11094. ERROR_OUT(-6842, out);
  11095. if (XMEMCMP(buf1 + sizeof Ciphertext, Tag, CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE))
  11096. ERROR_OUT(-6843, out);
  11097. ret = wc_XChaCha20Poly1305_Decrypt(buf2, sizeof Plaintext,
  11098. buf1, sizeof Ciphertext + sizeof Tag,
  11099. AAD, sizeof AAD,
  11100. IV, sizeof IV,
  11101. Key, sizeof Key);
  11102. if (ret < 0)
  11103. ERROR_OUT(-6844, out);
  11104. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  11105. ERROR_OUT(-6845, out);
  11106. out:
  11107. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  11108. if (buf1 != NULL)
  11109. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11110. if (buf2 != NULL)
  11111. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11112. #endif
  11113. return ret;
  11114. }
  11115. #endif /* defined(HAVE_XCHACHA) && defined(HAVE_POLY1305) */
  11116. #ifndef WC_NO_RNG
  11117. static int _rng_test(WC_RNG* rng, int errorOffset)
  11118. {
  11119. byte block[32];
  11120. int ret, i;
  11121. XMEMSET(block, 0, sizeof(block));
  11122. ret = wc_RNG_GenerateBlock(rng, block, sizeof(block));
  11123. if (ret != 0) {
  11124. ret = -6850;
  11125. goto exit;
  11126. }
  11127. /* Check for 0's */
  11128. for (i=0; i<(int)sizeof(block); i++) {
  11129. if (block[i] == 0) {
  11130. ret++;
  11131. }
  11132. }
  11133. /* All zeros count check */
  11134. if (ret >= (int)sizeof(block)) {
  11135. ret = -6851;
  11136. goto exit;
  11137. }
  11138. ret = wc_RNG_GenerateByte(rng, block);
  11139. if (ret != 0) {
  11140. ret = -6852;
  11141. goto exit;
  11142. }
  11143. /* Parameter validation testing. */
  11144. ret = wc_RNG_GenerateBlock(NULL, block, sizeof(block));
  11145. if (ret != BAD_FUNC_ARG) {
  11146. ret = -6853;
  11147. goto exit;
  11148. }
  11149. ret = wc_RNG_GenerateBlock(rng, NULL, sizeof(block));
  11150. if (ret != BAD_FUNC_ARG) {
  11151. ret = -6854;
  11152. goto exit;
  11153. }
  11154. ret = wc_RNG_GenerateByte(NULL, block);
  11155. if (ret != BAD_FUNC_ARG) {
  11156. ret = -6855;
  11157. goto exit;
  11158. }
  11159. ret = wc_RNG_GenerateByte(rng, NULL);
  11160. if (ret != BAD_FUNC_ARG) {
  11161. ret = -6856;
  11162. goto exit;
  11163. }
  11164. ret = 0;
  11165. exit:
  11166. if (ret != 0)
  11167. ret += errorOffset;
  11168. return ret;
  11169. }
  11170. static int random_rng_test(void)
  11171. {
  11172. WC_RNG localRng;
  11173. WC_RNG* rng;
  11174. int ret;
  11175. rng = &localRng;
  11176. /* Test stack based RNG. */
  11177. #ifndef HAVE_FIPS
  11178. ret = wc_InitRng_ex(rng, HEAP_HINT, devId);
  11179. #else
  11180. ret = wc_InitRng(rng);
  11181. #endif
  11182. if (ret != 0) return -6900;
  11183. ret = _rng_test(rng, -6300);
  11184. /* Make sure and free RNG */
  11185. wc_FreeRng(rng);
  11186. if (ret != 0) return ret;
  11187. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC)
  11188. {
  11189. byte nonce[8] = { 0 };
  11190. /* Test dynamic RNG. */
  11191. rng = wc_rng_new(nonce, (word32)sizeof(nonce), HEAP_HINT);
  11192. if (rng == NULL) return -6901;
  11193. ret = _rng_test(rng, -6310);
  11194. wc_rng_free(rng);
  11195. }
  11196. #endif
  11197. return ret;
  11198. }
  11199. #if defined(HAVE_HASHDRBG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  11200. #ifdef WC_RNG_SEED_CB
  11201. static int seed_cb(OS_Seed* os, byte* output, word32 sz)
  11202. {
  11203. word32 i;
  11204. (void)os;
  11205. /* Known answer test. Set the seed to the same value every time. */
  11206. for (i = 0; i < sz; i++)
  11207. output[i] = (byte)i;
  11208. return 0;
  11209. }
  11210. static int rng_seed_test(void)
  11211. {
  11212. #ifndef HAVE_FIPS
  11213. WOLFSSL_SMALL_STACK_STATIC const byte check[] =
  11214. {
  11215. 0x83, 0x46, 0x65, 0x2f, 0x5c, 0x44, 0x16, 0x5f,
  11216. 0xb3, 0x89, 0x26, 0xde, 0x0b, 0x6b, 0xa2, 0x06,
  11217. 0x7e, 0xa7, 0x9a, 0x55, 0x22, 0x01, 0xb0, 0x22,
  11218. 0xf4, 0x7e, 0xa2, 0x66, 0xc4, 0x08, 0x6f, 0xba
  11219. };
  11220. #else
  11221. /* FIPS uses a longer seed, so different check value. */
  11222. WOLFSSL_SMALL_STACK_STATIC const byte check[] =
  11223. {
  11224. 0xaf, 0x31, 0xcc, 0xef, 0xa9, 0x29, 0x4c, 0x24,
  11225. 0xbd, 0xa5, 0xa3, 0x52, 0x69, 0xf3, 0xb9, 0xb2,
  11226. 0x1e, 0xd4, 0x52, 0x3b, 0x9a, 0x96, 0x06, 0x20,
  11227. 0xc0, 0x5f, 0x44, 0x06, 0x1f, 0x80, 0xdf, 0xe0
  11228. };
  11229. #endif
  11230. byte output[WC_SHA256_DIGEST_SIZE];
  11231. WC_RNG rng;
  11232. int ret;
  11233. ret = wc_SetSeed_Cb(seed_cb);
  11234. if (ret != 0) {
  11235. ret = -7007;
  11236. goto exit;
  11237. }
  11238. ret = wc_InitRng(&rng);
  11239. if (ret != 0) {
  11240. ret = -7008;
  11241. goto exit;
  11242. }
  11243. ret = wc_RNG_GenerateBlock(&rng, output, sizeof(output));
  11244. if (ret != 0) {
  11245. ret = -7009;
  11246. goto exit;
  11247. }
  11248. ret = XMEMCMP(output, check, sizeof(output));
  11249. if (ret != 0) {
  11250. ret = -7010;
  11251. goto exit;
  11252. }
  11253. ret = wc_FreeRng(&rng);
  11254. if (ret != 0) {
  11255. ret = -7011;
  11256. goto exit;
  11257. }
  11258. ret = wc_SetSeed_Cb(wc_GenerateSeed);
  11259. if (ret != 0) {
  11260. ret = -7012;
  11261. }
  11262. exit:
  11263. return ret;
  11264. }
  11265. #endif
  11266. WOLFSSL_TEST_SUBROUTINE int random_test(void)
  11267. {
  11268. WOLFSSL_SMALL_STACK_STATIC const byte test1Entropy[] =
  11269. {
  11270. 0xa6, 0x5a, 0xd0, 0xf3, 0x45, 0xdb, 0x4e, 0x0e, 0xff, 0xe8, 0x75, 0xc3,
  11271. 0xa2, 0xe7, 0x1f, 0x42, 0xc7, 0x12, 0x9d, 0x62, 0x0f, 0xf5, 0xc1, 0x19,
  11272. 0xa9, 0xef, 0x55, 0xf0, 0x51, 0x85, 0xe0, 0xfb, 0x85, 0x81, 0xf9, 0x31,
  11273. 0x75, 0x17, 0x27, 0x6e, 0x06, 0xe9, 0x60, 0x7d, 0xdb, 0xcb, 0xcc, 0x2e
  11274. };
  11275. WOLFSSL_SMALL_STACK_STATIC const byte test1Output[] =
  11276. {
  11277. 0xd3, 0xe1, 0x60, 0xc3, 0x5b, 0x99, 0xf3, 0x40, 0xb2, 0x62, 0x82, 0x64,
  11278. 0xd1, 0x75, 0x10, 0x60, 0xe0, 0x04, 0x5d, 0xa3, 0x83, 0xff, 0x57, 0xa5,
  11279. 0x7d, 0x73, 0xa6, 0x73, 0xd2, 0xb8, 0xd8, 0x0d, 0xaa, 0xf6, 0xa6, 0xc3,
  11280. 0x5a, 0x91, 0xbb, 0x45, 0x79, 0xd7, 0x3f, 0xd0, 0xc8, 0xfe, 0xd1, 0x11,
  11281. 0xb0, 0x39, 0x13, 0x06, 0x82, 0x8a, 0xdf, 0xed, 0x52, 0x8f, 0x01, 0x81,
  11282. 0x21, 0xb3, 0xfe, 0xbd, 0xc3, 0x43, 0xe7, 0x97, 0xb8, 0x7d, 0xbb, 0x63,
  11283. 0xdb, 0x13, 0x33, 0xde, 0xd9, 0xd1, 0xec, 0xe1, 0x77, 0xcf, 0xa6, 0xb7,
  11284. 0x1f, 0xe8, 0xab, 0x1d, 0xa4, 0x66, 0x24, 0xed, 0x64, 0x15, 0xe5, 0x1c,
  11285. 0xcd, 0xe2, 0xc7, 0xca, 0x86, 0xe2, 0x83, 0x99, 0x0e, 0xea, 0xeb, 0x91,
  11286. 0x12, 0x04, 0x15, 0x52, 0x8b, 0x22, 0x95, 0x91, 0x02, 0x81, 0xb0, 0x2d,
  11287. 0xd4, 0x31, 0xf4, 0xc9, 0xf7, 0x04, 0x27, 0xdf
  11288. };
  11289. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyA[] =
  11290. {
  11291. 0x63, 0x36, 0x33, 0x77, 0xe4, 0x1e, 0x86, 0x46, 0x8d, 0xeb, 0x0a, 0xb4,
  11292. 0xa8, 0xed, 0x68, 0x3f, 0x6a, 0x13, 0x4e, 0x47, 0xe0, 0x14, 0xc7, 0x00,
  11293. 0x45, 0x4e, 0x81, 0xe9, 0x53, 0x58, 0xa5, 0x69, 0x80, 0x8a, 0xa3, 0x8f,
  11294. 0x2a, 0x72, 0xa6, 0x23, 0x59, 0x91, 0x5a, 0x9f, 0x8a, 0x04, 0xca, 0x68
  11295. };
  11296. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyB[] =
  11297. {
  11298. 0xe6, 0x2b, 0x8a, 0x8e, 0xe8, 0xf1, 0x41, 0xb6, 0x98, 0x05, 0x66, 0xe3,
  11299. 0xbf, 0xe3, 0xc0, 0x49, 0x03, 0xda, 0xd4, 0xac, 0x2c, 0xdf, 0x9f, 0x22,
  11300. 0x80, 0x01, 0x0a, 0x67, 0x39, 0xbc, 0x83, 0xd3
  11301. };
  11302. WOLFSSL_SMALL_STACK_STATIC const byte test2Output[] =
  11303. {
  11304. 0x04, 0xee, 0xc6, 0x3b, 0xb2, 0x31, 0xdf, 0x2c, 0x63, 0x0a, 0x1a, 0xfb,
  11305. 0xe7, 0x24, 0x94, 0x9d, 0x00, 0x5a, 0x58, 0x78, 0x51, 0xe1, 0xaa, 0x79,
  11306. 0x5e, 0x47, 0x73, 0x47, 0xc8, 0xb0, 0x56, 0x62, 0x1c, 0x18, 0xbd, 0xdc,
  11307. 0xdd, 0x8d, 0x99, 0xfc, 0x5f, 0xc2, 0xb9, 0x20, 0x53, 0xd8, 0xcf, 0xac,
  11308. 0xfb, 0x0b, 0xb8, 0x83, 0x12, 0x05, 0xfa, 0xd1, 0xdd, 0xd6, 0xc0, 0x71,
  11309. 0x31, 0x8a, 0x60, 0x18, 0xf0, 0x3b, 0x73, 0xf5, 0xed, 0xe4, 0xd4, 0xd0,
  11310. 0x71, 0xf9, 0xde, 0x03, 0xfd, 0x7a, 0xea, 0x10, 0x5d, 0x92, 0x99, 0xb8,
  11311. 0xaf, 0x99, 0xaa, 0x07, 0x5b, 0xdb, 0x4d, 0xb9, 0xaa, 0x28, 0xc1, 0x8d,
  11312. 0x17, 0x4b, 0x56, 0xee, 0x2a, 0x01, 0x4d, 0x09, 0x88, 0x96, 0xff, 0x22,
  11313. 0x82, 0xc9, 0x55, 0xa8, 0x19, 0x69, 0xe0, 0x69, 0xfa, 0x8c, 0xe0, 0x07,
  11314. 0xa1, 0x80, 0x18, 0x3a, 0x07, 0xdf, 0xae, 0x17
  11315. };
  11316. byte output[WC_SHA256_DIGEST_SIZE * 4];
  11317. int ret;
  11318. ret = wc_RNG_HealthTest(0, test1Entropy, sizeof(test1Entropy), NULL, 0,
  11319. output, sizeof(output));
  11320. if (ret != 0)
  11321. return -7000;
  11322. if (XMEMCMP(test1Output, output, sizeof(output)) != 0)
  11323. return -7001;
  11324. ret = wc_RNG_HealthTest(1, test2EntropyA, sizeof(test2EntropyA),
  11325. test2EntropyB, sizeof(test2EntropyB),
  11326. output, sizeof(output));
  11327. if (ret != 0)
  11328. return -7002;
  11329. if (XMEMCMP(test2Output, output, sizeof(output)) != 0)
  11330. return -7003;
  11331. /* Basic RNG generate block test */
  11332. if ((ret = random_rng_test()) != 0)
  11333. return ret;
  11334. /* Test the seed check function. */
  11335. #if !(defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) || \
  11336. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  11337. {
  11338. word32 i, outputSz;
  11339. /* Repeat the same byte over and over. Should fail. */
  11340. outputSz = sizeof(output);
  11341. XMEMSET(output, 1, outputSz);
  11342. ret = wc_RNG_TestSeed(output, outputSz);
  11343. if (ret == 0)
  11344. return -7004;
  11345. /* Every byte of the entropy scratch is different,
  11346. * entropy is a single byte that shouldn't match. */
  11347. outputSz = (sizeof(output) / 2) + 1;
  11348. for (i = 0; i < outputSz; i++)
  11349. output[i] = (byte)i;
  11350. ret = wc_RNG_TestSeed(output, outputSz);
  11351. if (ret != 0)
  11352. return -7005;
  11353. outputSz = sizeof(output);
  11354. for (i = 0; i < outputSz; i++)
  11355. output[i] = (byte)i;
  11356. ret = wc_RNG_TestSeed(output, outputSz);
  11357. if (ret != 0)
  11358. return -7006;
  11359. }
  11360. #endif
  11361. /* Test the seed callback. */
  11362. #ifdef WC_RNG_SEED_CB
  11363. if ((ret = rng_seed_test()) != 0)
  11364. return ret;
  11365. #endif
  11366. return 0;
  11367. }
  11368. #else
  11369. WOLFSSL_TEST_SUBROUTINE int random_test(void)
  11370. {
  11371. /* Basic RNG generate block test */
  11372. return random_rng_test();
  11373. }
  11374. #endif /* HAVE_HASHDRBG && !CUSTOM_RAND_GENERATE_BLOCK */
  11375. #endif /* WC_NO_RNG */
  11376. #ifndef MEM_TEST_SZ
  11377. #define MEM_TEST_SZ 1024
  11378. #endif
  11379. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  11380. static int simple_mem_test(int sz)
  11381. {
  11382. int ret = 0;
  11383. byte* b;
  11384. int i;
  11385. b = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11386. if (b == NULL) {
  11387. return -7110;
  11388. }
  11389. /* utilize memory */
  11390. for (i = 0; i < sz; i++) {
  11391. b[i] = (byte)i;
  11392. }
  11393. /* read back and verify */
  11394. for (i = 0; i < sz; i++) {
  11395. if (b[i] != (byte)i) {
  11396. ret = -7111;
  11397. break;
  11398. }
  11399. }
  11400. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11401. return ret;
  11402. }
  11403. #endif
  11404. WOLFSSL_TEST_SUBROUTINE int memory_test(void)
  11405. {
  11406. int ret = 0;
  11407. #if defined(COMPLEX_MEM_TEST) || defined(WOLFSSL_STATIC_MEMORY)
  11408. int i;
  11409. #endif
  11410. #ifdef WOLFSSL_STATIC_MEMORY
  11411. word32 size[] = { WOLFMEM_BUCKETS };
  11412. word32 dist[] = { WOLFMEM_DIST };
  11413. byte buffer[30000]; /* make large enough to involve many bucket sizes */
  11414. int pad = -(int)((wc_ptr_t)buffer) & (WOLFSSL_STATIC_ALIGN - 1);
  11415. /* pad to account for if head of buffer is not at set memory
  11416. * alignment when tests are ran */
  11417. #endif
  11418. #ifdef WOLFSSL_STATIC_MEMORY
  11419. /* check macro settings */
  11420. if (sizeof(size)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
  11421. return -7200;
  11422. }
  11423. if (sizeof(dist)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
  11424. return -7201;
  11425. }
  11426. for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
  11427. if ((size[i] % WOLFSSL_STATIC_ALIGN) != 0) {
  11428. /* each element in array should be divisible by alignment size */
  11429. return -7202;
  11430. }
  11431. }
  11432. for (i = 1; i < WOLFMEM_MAX_BUCKETS; i++) {
  11433. if (size[i - 1] >= size[i]) {
  11434. return -7203; /* sizes should be in increasing order */
  11435. }
  11436. }
  11437. /* check that padding size returned is possible */
  11438. if (wolfSSL_MemoryPaddingSz() < WOLFSSL_STATIC_ALIGN) {
  11439. return -7204; /* no room for wc_Memory struct */
  11440. }
  11441. if (wolfSSL_MemoryPaddingSz() < 0) {
  11442. return -7205;
  11443. }
  11444. if (wolfSSL_MemoryPaddingSz() % WOLFSSL_STATIC_ALIGN != 0) {
  11445. return -7206; /* not aligned! */
  11446. }
  11447. /* check function to return optimum buffer size (rounded down) */
  11448. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_GENERAL);
  11449. if ((ret - pad) % WOLFSSL_STATIC_ALIGN != 0) {
  11450. return -7207; /* not aligned! */
  11451. }
  11452. if (ret < 0) {
  11453. return -7208;
  11454. }
  11455. if ((unsigned int)ret > sizeof(buffer)) {
  11456. return -7209; /* did not round down as expected */
  11457. }
  11458. if (ret != wolfSSL_StaticBufferSz(buffer, ret, WOLFMEM_GENERAL)) {
  11459. return -7210; /* return value changed when using suggested value */
  11460. }
  11461. ret = wolfSSL_MemoryPaddingSz();
  11462. ret += pad; /* add space that is going to be needed if buffer not aligned */
  11463. if (wolfSSL_StaticBufferSz(buffer, size[0] + ret + 1, WOLFMEM_GENERAL) !=
  11464. (ret + (int)size[0])) {
  11465. return -7211; /* did not round down to nearest bucket value */
  11466. }
  11467. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_IO_POOL);
  11468. if ((ret - pad) < 0) {
  11469. return -7212;
  11470. }
  11471. if (((ret - pad) % (WOLFMEM_IO_SZ + wolfSSL_MemoryPaddingSz())) != 0) {
  11472. return -7213; /* not even chunks of memory for IO size */
  11473. }
  11474. if (((ret - pad) % WOLFSSL_STATIC_ALIGN) != 0) {
  11475. return -7214; /* memory not aligned */
  11476. }
  11477. /* check for passing bad or unknown arguments to functions */
  11478. if (wolfSSL_StaticBufferSz(NULL, 1, WOLFMEM_GENERAL) > 0) {
  11479. return -7215;
  11480. }
  11481. if (wolfSSL_StaticBufferSz(buffer, 1, WOLFMEM_GENERAL) != 0) {
  11482. return -7216; /* should round to 0 since struct + bucket will not fit */
  11483. }
  11484. (void)dist; /* avoid static analysis warning of variable not used */
  11485. #endif
  11486. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  11487. /* simple test */
  11488. ret = simple_mem_test(MEM_TEST_SZ);
  11489. if (ret != 0)
  11490. return ret;
  11491. #endif
  11492. #ifdef COMPLEX_MEM_TEST
  11493. /* test various size blocks */
  11494. for (i = 1; i < MEM_TEST_SZ; i*=2) {
  11495. ret = simple_mem_test(i);
  11496. if (ret != 0)
  11497. return ret;
  11498. }
  11499. #endif
  11500. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_NO_MALLOC) && defined(XREALLOC)
  11501. /* realloc test */
  11502. {
  11503. byte *c = NULL;
  11504. byte *b = (byte*)XMALLOC(MEM_TEST_SZ, HEAP_HINT,
  11505. DYNAMIC_TYPE_TMP_BUFFER);
  11506. if (b) {
  11507. c = (byte*)XREALLOC(b, MEM_TEST_SZ+sizeof(word32), HEAP_HINT,
  11508. DYNAMIC_TYPE_TMP_BUFFER);
  11509. if (c)
  11510. b = c;
  11511. }
  11512. if (b)
  11513. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11514. if ((b == NULL) || (c == NULL)) {
  11515. return -7217;
  11516. }
  11517. }
  11518. #endif
  11519. return ret;
  11520. }
  11521. #ifndef NO_FILESYSTEM
  11522. /* Cert Paths */
  11523. #ifdef FREESCALE_MQX
  11524. #define CERT_PREFIX "a:\\"
  11525. #define CERT_PATH_SEP "\\"
  11526. #elif defined(WOLFSSL_uTKERNEL2)
  11527. #define CERT_PREFIX "/uda/"
  11528. #define CERT_PATH_SEP "/"
  11529. #elif defined(_WIN32_WCE)
  11530. #define CERT_PREFIX "\\windows\\"
  11531. #define CERT_PATH_SEP "\\"
  11532. #endif
  11533. #ifndef CERT_PREFIX
  11534. #define CERT_PREFIX "./"
  11535. #endif
  11536. #ifndef CERT_PATH_SEP
  11537. #define CERT_PATH_SEP "/"
  11538. #endif
  11539. #ifndef CERT_WRITE_TEMP_DIR
  11540. #define CERT_WRITE_TEMP_DIR CERT_PREFIX
  11541. #endif
  11542. #define CERT_ROOT CERT_PREFIX "certs" CERT_PATH_SEP
  11543. /* Generated Test Certs */
  11544. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  11545. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  11546. #if !defined(NO_RSA) && !defined(NO_ASN)
  11547. static const char* clientKey = CERT_ROOT "client-key.der";
  11548. static const char* clientCert = CERT_ROOT "client-cert.der";
  11549. #ifdef WOLFSSL_CERT_EXT
  11550. static const char* clientKeyPub = CERT_ROOT "client-keyPub.der";
  11551. #endif
  11552. #endif /* !NO_RSA && !NO_ASN */
  11553. #endif
  11554. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  11555. #if !defined(NO_RSA) && !defined(NO_ASN)
  11556. #if defined(WOLFSSL_CERT_GEN) || defined(HAVE_PKCS7)
  11557. static const char* rsaCaKeyFile = CERT_ROOT "ca-key.der";
  11558. #ifdef WOLFSSL_CERT_GEN
  11559. static const char* rsaCaCertFile = CERT_ROOT "ca-cert.pem";
  11560. #endif
  11561. #if defined(WOLFSSL_ALT_NAMES) || defined(HAVE_PKCS7)
  11562. static const char* rsaCaCertDerFile = CERT_ROOT "ca-cert.der";
  11563. #endif
  11564. #ifdef HAVE_PKCS7
  11565. static const char* rsaServerCertDerFile =
  11566. CERT_ROOT "server-cert.der";
  11567. static const char* rsaServerKeyDerFile =
  11568. CERT_ROOT "server-key.der";
  11569. #endif
  11570. #endif
  11571. #endif /* !NO_RSA && !NO_ASN */
  11572. #endif /* !USE_CERT_BUFFER_* */
  11573. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  11574. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  11575. #if !defined(NO_ASN) && !defined(NO_DH)
  11576. static const char* dhParamsFile = CERT_ROOT "dh2048.der";
  11577. #endif
  11578. #endif
  11579. #if !defined(NO_ASN) && !defined(NO_DH)
  11580. #if defined(WOLFSSL_DH_EXTRA) && (!defined(HAVE_FIPS) || \
  11581. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  11582. static const char* dhKeyFile = CERT_ROOT "statickeys/dh-ffdhe2048.der";
  11583. static const char* dhKeyPubFile = CERT_ROOT "statickeys/dh-ffdhe2048-pub.der";
  11584. #endif
  11585. #endif
  11586. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  11587. #ifndef NO_DSA
  11588. static const char* dsaKey = CERT_ROOT "dsa2048.der";
  11589. #endif
  11590. #endif /* !USE_CERT_BUFFER_* */
  11591. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ECC256)
  11592. #ifdef HAVE_ECC
  11593. /* cert files to be used in rsa cert gen test, check if RSA enabled */
  11594. #ifdef HAVE_ECC_KEY_IMPORT
  11595. static const char* eccKeyDerFile = CERT_ROOT "ecc-key.der";
  11596. #endif
  11597. #endif
  11598. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ASN)
  11599. #if defined(HAVE_ECC) && defined(WOLFSSL_CERT_GEN)
  11600. #ifndef NO_RSA
  11601. static const char* eccKeyPubFileDer = CERT_ROOT "ecc-keyPub.der";
  11602. #endif
  11603. #ifndef NO_ASN_TIME
  11604. static const char* eccCaKeyFile = CERT_ROOT "ca-ecc-key.der";
  11605. static const char* eccCaCertFile = CERT_ROOT "ca-ecc-cert.pem";
  11606. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  11607. static const char* eccCaKey384File =
  11608. CERT_ROOT "ca-ecc384-key.der";
  11609. static const char* eccCaCert384File =
  11610. CERT_ROOT "ca-ecc384-cert.pem";
  11611. #endif
  11612. #endif
  11613. #endif
  11614. #if defined(HAVE_PKCS7) && defined(HAVE_ECC)
  11615. static const char* eccClientKey = CERT_ROOT "ecc-client-key.der";
  11616. static const char* eccClientCert = CERT_ROOT "client-ecc-cert.der";
  11617. #endif
  11618. #endif /* HAVE_ECC */
  11619. #ifdef HAVE_ED25519
  11620. #ifdef WOLFSSL_TEST_CERT
  11621. static const char* serverEd25519Cert =
  11622. CERT_ROOT "ed25519/server-ed25519.der";
  11623. static const char* caEd25519Cert =
  11624. CERT_ROOT "ed25519/ca-ed25519.der";
  11625. #endif
  11626. #endif
  11627. #ifdef HAVE_ED448
  11628. #ifdef WOLFSSL_TEST_CERT
  11629. static const char* serverEd448Cert =
  11630. CERT_ROOT "ed448/server-ed448.der";
  11631. static const char* caEd448Cert = CERT_ROOT "ed448/ca-ed448.der";
  11632. #endif
  11633. #endif
  11634. #endif /* !USE_CERT_BUFFER_* */
  11635. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  11636. !defined(NO_FILESYSTEM)
  11637. static const char* certExtNc =
  11638. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-nc.der";
  11639. static const char* certExtIa =
  11640. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-ia.der";
  11641. static const char* certExtNct =
  11642. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-nct.der";
  11643. #endif
  11644. #ifndef NO_WRITE_TEMP_FILES
  11645. #ifdef HAVE_ECC
  11646. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  11647. static const char* certEccPemFile = CERT_WRITE_TEMP_DIR "certecc.pem";
  11648. static const char* certEccDerFile = CERT_WRITE_TEMP_DIR "certecc.der";
  11649. #endif
  11650. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  11651. static const char* certEccRsaPemFile = CERT_WRITE_TEMP_DIR "certeccrsa.pem";
  11652. static const char* certEccRsaDerFile = CERT_WRITE_TEMP_DIR "certeccrsa.der";
  11653. #endif
  11654. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  11655. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(NO_ASN_CRYPT)
  11656. static const char* eccCaKeyPemFile = CERT_WRITE_TEMP_DIR "ecc-key.pem";
  11657. static const char* eccPubKeyDerFile = CERT_WRITE_TEMP_DIR "ecc-public-key.der";
  11658. static const char* eccCaKeyTempFile = CERT_WRITE_TEMP_DIR "ecc-key.der";
  11659. #if defined(HAVE_PKCS8) && !defined(WC_NO_RNG) && \
  11660. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  11661. static const char* eccPkcs8KeyDerFile = CERT_WRITE_TEMP_DIR "ecc-key-pkcs8.der";
  11662. #endif
  11663. #endif /* HAVE_ECC_KEY_EXPORT */
  11664. #endif /* HAVE_ECC */
  11665. #ifndef NO_RSA
  11666. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  11667. static const char* otherCertDerFile = CERT_WRITE_TEMP_DIR "othercert.der";
  11668. static const char* certDerFile = CERT_WRITE_TEMP_DIR "cert.der";
  11669. static const char* otherCertPemFile = CERT_WRITE_TEMP_DIR "othercert.pem";
  11670. static const char* certPemFile = CERT_WRITE_TEMP_DIR "cert.pem";
  11671. #if defined(WOLFSSL_CERT_REQ) && !defined(WOLFSSL_NO_MALLOC)
  11672. static const char* certReqDerFile = CERT_WRITE_TEMP_DIR "certreq.der";
  11673. static const char* certReqPemFile = CERT_WRITE_TEMP_DIR "certreq.pem";
  11674. #endif
  11675. #endif
  11676. #endif /* !NO_RSA */
  11677. #if !defined(NO_RSA) || !defined(NO_DSA)
  11678. #ifdef WOLFSSL_KEY_GEN
  11679. static const char* keyDerFile = CERT_WRITE_TEMP_DIR "key.der";
  11680. static const char* keyPemFile = CERT_WRITE_TEMP_DIR "key.pem";
  11681. #endif
  11682. #endif
  11683. #endif /* !NO_WRITE_TEMP_FILES */
  11684. #endif /* !NO_FILESYSTEM */
  11685. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  11686. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  11687. static CertName certDefaultName;
  11688. static void initDefaultName(void)
  11689. {
  11690. #if defined(WOLFSSL_MULTI_ATTRIB) && defined(WOLFSSL_TEST_CERT)
  11691. NameAttrib* n;
  11692. #endif
  11693. XMEMCPY(certDefaultName.country, "US", sizeof("US"));
  11694. certDefaultName.countryEnc = CTC_PRINTABLE;
  11695. XMEMCPY(certDefaultName.state, "Oregon", sizeof("Oregon"));
  11696. certDefaultName.stateEnc = CTC_UTF8;
  11697. XMEMCPY(certDefaultName.street, "Main St", sizeof("Main St"));
  11698. certDefaultName.streetEnc = CTC_UTF8;
  11699. XMEMCPY(certDefaultName.locality, "Portland", sizeof("Portland"));
  11700. certDefaultName.localityEnc = CTC_UTF8;
  11701. XMEMCPY(certDefaultName.sur, "Test", sizeof("Test"));
  11702. certDefaultName.surEnc = CTC_UTF8;
  11703. XMEMCPY(certDefaultName.org, "wolfSSL", sizeof("wolfSSL"));
  11704. certDefaultName.orgEnc = CTC_UTF8;
  11705. XMEMCPY(certDefaultName.unit, "Development", sizeof("Development"));
  11706. certDefaultName.unitEnc = CTC_UTF8;
  11707. XMEMCPY(certDefaultName.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  11708. certDefaultName.commonNameEnc = CTC_UTF8;
  11709. XMEMCPY(certDefaultName.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  11710. certDefaultName.serialDevEnc = CTC_PRINTABLE;
  11711. XMEMCPY(certDefaultName.postalCode, "12-456", sizeof("12-456"));
  11712. certDefaultName.postalCodeEnc = CTC_PRINTABLE;
  11713. #ifdef WOLFSSL_CERT_EXT
  11714. XMEMCPY(certDefaultName.busCat, "Private Organization", sizeof("Private Organization"));
  11715. certDefaultName.busCatEnc = CTC_UTF8;
  11716. XMEMCPY(certDefaultName.joiSt, "US", sizeof("US"));
  11717. certDefaultName.joiStEnc = CTC_PRINTABLE;
  11718. XMEMCPY(certDefaultName.joiC, "Oregon", sizeof("Oregon"));
  11719. certDefaultName.joiCEnc = CTC_PRINTABLE;
  11720. #endif
  11721. XMEMCPY(certDefaultName.email, "info@wolfssl.com", sizeof("info@wolfssl.com"));
  11722. XMEMCPY(certDefaultName.userId, "TestUserID", sizeof("TestUserID"));
  11723. certDefaultName.userIdEnc = CTC_PRINTABLE;
  11724. #if defined(WOLFSSL_MULTI_ATTRIB) && defined(WOLFSSL_TEST_CERT)
  11725. /* test having additional OUs and setting DC */
  11726. n = &certDefaultName.name[0];
  11727. n->id = ASN_ORGUNIT_NAME;
  11728. n->type = CTC_UTF8;
  11729. n->sz = sizeof("Development-2");
  11730. XMEMCPY(n->value, "Development-2", sizeof("Development-2"));
  11731. #if CTC_MAX_ATTRIB > 3
  11732. n = &certDefaultName.name[1];
  11733. n->id = ASN_DOMAIN_COMPONENT;
  11734. n->type = CTC_UTF8;
  11735. n->sz = sizeof("com");
  11736. XMEMCPY(n->value, "com", sizeof("com"));
  11737. n = &certDefaultName.name[2];
  11738. n->id = ASN_DOMAIN_COMPONENT;
  11739. n->type = CTC_UTF8;
  11740. n->sz = sizeof("wolfssl");
  11741. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  11742. #endif
  11743. #endif /* WOLFSSL_MULTI_ATTRIB && WOLFSSL_TEST_CERT */
  11744. #ifdef WOLFSSL_CUSTOM_OID
  11745. /* TODO: Add test case for custom OID's */
  11746. #endif
  11747. }
  11748. #ifdef WOLFSSL_CERT_EXT
  11749. #if ((defined(HAVE_ED25519) || defined(HAVE_ED448)) && \
  11750. defined(WOLFSSL_TEST_CERT)) || defined(HAVE_ECC)
  11751. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage[] =
  11752. "digitalSignature,nonRepudiation";
  11753. #endif
  11754. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  11755. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage2[] =
  11756. "digitalSignature,nonRepudiation,keyEncipherment,keyAgreement";
  11757. #endif
  11758. #endif /* WOLFSSL_CERT_EXT */
  11759. #endif /* WOLFSSL_CERT_GEN */
  11760. #ifndef NO_RSA
  11761. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  11762. !defined(NO_FILESYSTEM)
  11763. static byte minSerial[] = { 0x02, 0x01, 0x01 };
  11764. static byte minName[] = { 0x30, 0x00 };
  11765. static byte nameBad[] = {
  11766. 0x30, 0x08,
  11767. 0x31, 0x06,
  11768. 0x30, 0x04,
  11769. 0x06, 0x02,
  11770. 0x55, 0x04,
  11771. };
  11772. static byte minDates[] = {
  11773. 0x30, 0x1e,
  11774. 0x17, 0x0d,
  11775. 0x31, 0x38, 0x30, 0x34, 0x31, 0x33, 0x31, 0x35,
  11776. 0x32, 0x33, 0x31, 0x30, 0x5a,
  11777. 0x17, 0x0d,
  11778. 0x32, 0x31, 0x30, 0x31, 0x30, 0x37, 0x31, 0x35,
  11779. 0x32, 0x33, 0x31, 0x30, 0x5a
  11780. };
  11781. static byte minPubKey[] = {
  11782. 0x30, 0x1c,
  11783. 0x30, 0x0d,
  11784. 0x06, 0x09,
  11785. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  11786. 0x01,
  11787. 0x05, 0x00,
  11788. 0x03, 0x0b,
  11789. 0x00, 0x30, 0x08,
  11790. 0x02, 0x01,
  11791. 0x03,
  11792. 0x02, 0x03,
  11793. 0x01, 0x00, 0x01
  11794. };
  11795. static byte minSigAlg[] = {
  11796. 0x30, 0x0d,
  11797. 0x06, 0x09,
  11798. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  11799. 0x0b,
  11800. 0x05, 0x00
  11801. };
  11802. static byte minSig[] = {
  11803. 0x03, 0x01,
  11804. 0x00
  11805. };
  11806. static int add_seq(byte* certData, int offset, byte* data, byte length)
  11807. {
  11808. XMEMMOVE(certData + offset + 2, data, length);
  11809. certData[offset++] = 0x30;
  11810. certData[offset++] = length;
  11811. return offset + length;
  11812. }
  11813. static int add_data(byte* certData, int offset, byte* data, byte length)
  11814. {
  11815. XMEMCPY(certData + offset, data, length);
  11816. return offset + length;
  11817. }
  11818. static int cert_asn1_test(void)
  11819. {
  11820. int ret;
  11821. int len[3];
  11822. DecodedCert cert;
  11823. byte certData[114];
  11824. byte* badCert = NULL;
  11825. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  11826. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  11827. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  11828. len[2] = add_data(certData, len[2], minDates, (byte)sizeof(minDates));
  11829. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  11830. len[2] = add_data(certData, len[2], minPubKey, (byte)sizeof(minPubKey));
  11831. len[1] = add_seq(certData, 0, certData, len[2]);
  11832. len[1] = add_data(certData, len[1], minSigAlg, (byte)sizeof(minSigAlg));
  11833. len[1] = add_data(certData, len[1], minSig, (byte)sizeof(minSig));
  11834. len[0] = add_seq(certData, 0, certData, len[1]);
  11835. /* Minimal good certificate */
  11836. InitDecodedCert(&cert, certData, len[0], 0);
  11837. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  11838. FreeDecodedCert(&cert);
  11839. if (ret != 0) {
  11840. ERROR_OUT(-7300, done);
  11841. }
  11842. /* Bad issuer name */
  11843. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  11844. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  11845. len[2] = add_data(certData, len[2], nameBad, (byte)sizeof(nameBad));
  11846. len[2] = add_data(certData, len[2], minDates, (byte)sizeof(minDates));
  11847. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  11848. len[2] = add_data(certData, len[2], minPubKey, (byte)sizeof(minPubKey));
  11849. len[1] = add_seq(certData, 0, certData, len[2]);
  11850. len[1] = add_data(certData, len[1], minSigAlg, (byte)sizeof(minSigAlg));
  11851. len[1] = add_data(certData, len[1], minSig, (byte)sizeof(minSig));
  11852. len[0] = add_seq(certData, 0, certData, len[1]);
  11853. /* Put data into allocated buffer to allow access error checking. */
  11854. badCert = (byte*)XMALLOC(len[0], HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11855. XMEMCPY(badCert, certData, len[0]);
  11856. InitDecodedCert(&cert, badCert, len[0], 0);
  11857. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  11858. FreeDecodedCert(&cert);
  11859. if (ret != ASN_PARSE_E) {
  11860. ERROR_OUT(-7301, done);
  11861. }
  11862. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11863. badCert = NULL;
  11864. ret = 0;
  11865. done:
  11866. if (badCert != NULL)
  11867. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11868. return ret;
  11869. }
  11870. WOLFSSL_TEST_SUBROUTINE int cert_test(void)
  11871. {
  11872. #if !defined(NO_FILESYSTEM)
  11873. DecodedCert cert;
  11874. byte* tmp;
  11875. size_t bytes;
  11876. XFILE file;
  11877. int ret;
  11878. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11879. if (tmp == NULL)
  11880. return -7400;
  11881. /* Certificate with Name Constraints extension. */
  11882. file = XFOPEN(certExtNc, "rb");
  11883. if (!file) {
  11884. ERROR_OUT(-7401, done);
  11885. }
  11886. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11887. XFCLOSE(file);
  11888. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11889. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  11890. if (ret != 0) {
  11891. ERROR_OUT(-7402, done);
  11892. }
  11893. FreeDecodedCert(&cert);
  11894. /* Certificate with Inhibit Any Policy extension. */
  11895. file = XFOPEN(certExtIa, "rb");
  11896. if (!file) {
  11897. ERROR_OUT(-7403, done);
  11898. }
  11899. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11900. XFCLOSE(file);
  11901. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11902. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  11903. if (ret != 0) {
  11904. ERROR_OUT(-7404, done);
  11905. }
  11906. FreeDecodedCert(&cert);
  11907. /* Certificate with Netscape Certificate Type extension. */
  11908. file = XFOPEN(certExtNct, "rb");
  11909. if (!file) {
  11910. ERROR_OUT(-7405, done);
  11911. }
  11912. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11913. XFCLOSE(file);
  11914. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11915. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  11916. #ifndef IGNORE_NETSCAPE_CERT_TYPE
  11917. if (ret != 0) {
  11918. ERROR_OUT(-7406, done);
  11919. }
  11920. #else
  11921. if (ret != ASN_CRIT_EXT_E) {
  11922. ERROR_OUT(-7407, done);
  11923. }
  11924. ret = 0;
  11925. #endif
  11926. done:
  11927. FreeDecodedCert(&cert);
  11928. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11929. #endif /* !NO_FILESYSTEM */
  11930. if (ret == 0)
  11931. ret = cert_asn1_test();
  11932. return ret;
  11933. }
  11934. #endif /* WOLFSSL_TEST_CERT */
  11935. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  11936. !defined(NO_FILESYSTEM) && defined(WOLFSSL_CERT_GEN)
  11937. WOLFSSL_TEST_SUBROUTINE int certext_test(void)
  11938. {
  11939. DecodedCert cert;
  11940. byte* tmp;
  11941. size_t bytes;
  11942. XFILE file;
  11943. int ret;
  11944. /* created from rsa_test : othercert.der */
  11945. byte skid_rsa[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  11946. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  11947. /* created from rsa_test : othercert.der */
  11948. byte akid_rsa[] = "\x27\x8E\x67\x11\x74\xC3\x26\x1D\x3F\xED"
  11949. "\x33\x63\xB3\xA4\xD8\x1D\x30\xE5\xE8\xD5";
  11950. #ifdef HAVE_ECC
  11951. /* created from ecc_test_cert_gen : certecc.der */
  11952. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  11953. /* Authority key id from ./certs/ca-ecc384-cert.pem */
  11954. byte akid_ecc[] = "\xAB\xE0\xC3\x26\x4C\x18\xD4\x72\xBB\xD2"
  11955. "\x84\x8C\x9C\x0A\x05\x92\x80\x12\x53\x52";
  11956. #else
  11957. /* Authority key id from ./certs/ca-ecc-cert.pem */
  11958. byte akid_ecc[] = "\x56\x8E\x9A\xC3\xF0\x42\xDE\x18\xB9\x45"
  11959. "\x55\x6E\xF9\x93\xCF\xEA\xC3\xF3\xA5\x21";
  11960. #endif
  11961. #endif /* HAVE_ECC */
  11962. /* created from rsa_test : cert.der */
  11963. byte kid_ca[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  11964. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  11965. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11966. if (tmp == NULL)
  11967. return -7500;
  11968. /* load othercert.der (Cert signed by an authority) */
  11969. file = XFOPEN(otherCertDerFile, "rb");
  11970. if (!file) {
  11971. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  11972. return -7501;
  11973. }
  11974. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11975. XFCLOSE(file);
  11976. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11977. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  11978. if (ret != 0)
  11979. return -7502;
  11980. /* check the SKID from a RSA certificate */
  11981. if (XMEMCMP(skid_rsa, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  11982. return -7503;
  11983. /* check the AKID from an RSA certificate */
  11984. if (XMEMCMP(akid_rsa, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  11985. return -7504;
  11986. /* check the Key Usage from an RSA certificate */
  11987. if (!cert.extKeyUsageSet)
  11988. return -7505;
  11989. if (cert.extKeyUsage != (KEYUSE_KEY_ENCIPHER|KEYUSE_KEY_AGREE))
  11990. return -7506;
  11991. /* check the CA Basic Constraints from an RSA certificate */
  11992. if (cert.isCA)
  11993. return -7507;
  11994. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  11995. /* check the Certificate Policies Id */
  11996. if (cert.extCertPoliciesNb != 1)
  11997. return -7508;
  11998. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  11999. return -7509;
  12000. #endif
  12001. FreeDecodedCert(&cert);
  12002. #ifdef HAVE_ECC
  12003. /* load certecc.der (Cert signed by our ECC CA test in ecc_test_cert_gen) */
  12004. file = XFOPEN(certEccDerFile, "rb");
  12005. if (!file) {
  12006. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  12007. return -7510;
  12008. }
  12009. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  12010. XFCLOSE(file);
  12011. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  12012. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  12013. if (ret != 0)
  12014. return -7511;
  12015. /* check the SKID from a ECC certificate - generated dynamically */
  12016. /* check the AKID from an ECC certificate */
  12017. if (XMEMCMP(akid_ecc, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  12018. return -7512;
  12019. /* check the Key Usage from an ECC certificate */
  12020. if (!cert.extKeyUsageSet)
  12021. return -7513;
  12022. if (cert.extKeyUsage != (KEYUSE_DIGITAL_SIG|KEYUSE_CONTENT_COMMIT))
  12023. return -7514;
  12024. /* check the CA Basic Constraints from an ECC certificate */
  12025. if (cert.isCA)
  12026. return -7515;
  12027. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  12028. /* check the Certificate Policies Id */
  12029. if (cert.extCertPoliciesNb != 2)
  12030. return -7516;
  12031. if (strncmp(cert.extCertPolicies[0], "2.4.589440.587.101.2.1.9632587.1", 32))
  12032. return -7517;
  12033. if (strncmp(cert.extCertPolicies[1], "1.2.13025.489.1.113549", 22))
  12034. return -7518;
  12035. #endif
  12036. FreeDecodedCert(&cert);
  12037. #endif /* HAVE_ECC */
  12038. /* load cert.der (self signed certificate) */
  12039. file = XFOPEN(certDerFile, "rb");
  12040. if (!file) {
  12041. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  12042. return -7519;
  12043. }
  12044. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  12045. XFCLOSE(file);
  12046. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  12047. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  12048. if (ret != 0)
  12049. return -7520;
  12050. /* check the SKID from a CA certificate */
  12051. if (XMEMCMP(kid_ca, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  12052. return -7521;
  12053. /* check the AKID from an CA certificate */
  12054. if (XMEMCMP(kid_ca, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  12055. return -7522;
  12056. /* check the Key Usage from CA certificate */
  12057. if (!cert.extKeyUsageSet)
  12058. return -7523;
  12059. if (cert.extKeyUsage != (KEYUSE_KEY_CERT_SIGN|KEYUSE_CRL_SIGN))
  12060. return -7524;
  12061. /* check the CA Basic Constraints CA certificate */
  12062. if (!cert.isCA)
  12063. return -7525;
  12064. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  12065. /* check the Certificate Policies Id */
  12066. if (cert.extCertPoliciesNb != 2)
  12067. return -7526;
  12068. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  12069. return -7527;
  12070. if (strncmp(cert.extCertPolicies[1], "1.2.840.113549.1.9.16.6.5", 25))
  12071. return -7528;
  12072. #endif
  12073. FreeDecodedCert(&cert);
  12074. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  12075. return 0;
  12076. }
  12077. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_TEST_CERT &&
  12078. !NO_FILESYSTEM && WOLFSSL_CERT_GEN */
  12079. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  12080. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  12081. WOLFSSL_TEST_SUBROUTINE int decodedCertCache_test(void)
  12082. {
  12083. int ret = 0;
  12084. Cert cert;
  12085. FILE* file;
  12086. byte* der;
  12087. word32 derSz;
  12088. derSz = FOURK_BUF;
  12089. der = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12090. if (der == NULL)
  12091. ret = -7600;
  12092. if (ret == 0) {
  12093. /* load cert.der */
  12094. file = XFOPEN(certDerFile, "rb");
  12095. if (file != NULL) {
  12096. derSz = (word32)XFREAD(der, 1, FOURK_BUF, file);
  12097. XFCLOSE(file);
  12098. }
  12099. else
  12100. ret = -7601;
  12101. }
  12102. if (ret == 0) {
  12103. if (wc_InitCert_ex(&cert, HEAP_HINT, devId)) {
  12104. ret = -7602;
  12105. }
  12106. }
  12107. if (ret == 0) {
  12108. ret = wc_SetSubjectBuffer(&cert, der, derSz);
  12109. }
  12110. if (ret == 0) {
  12111. if(wc_SetSubjectBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  12112. ret = -7603;
  12113. }
  12114. if (ret == 0) {
  12115. if (wc_SetSubjectRaw(&cert, der, derSz) != 0)
  12116. ret = -7604;
  12117. }
  12118. if (ret == 0) {
  12119. if(wc_SetSubjectRaw(NULL, der, derSz) != BAD_FUNC_ARG)
  12120. ret = -7605;
  12121. }
  12122. if (ret == 0) {
  12123. if(wc_SetIssuerBuffer(&cert, der, derSz) != 0)
  12124. ret = -7606;
  12125. }
  12126. if (ret == 0) {
  12127. if(wc_SetIssuerBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  12128. ret = -7607;
  12129. }
  12130. if (ret == 0) {
  12131. if(wc_SetIssuerRaw(&cert, der, derSz) != 0)
  12132. ret = -7608;
  12133. }
  12134. if (ret == 0) {
  12135. if(wc_SetIssuerRaw(NULL, der, derSz) != BAD_FUNC_ARG)
  12136. ret = -7609;
  12137. }
  12138. #ifdef WOLFSSL_ALT_NAMES
  12139. if (ret == 0) {
  12140. if(wc_SetAltNamesBuffer(&cert, der, derSz) != 0)
  12141. ret = -7610;
  12142. }
  12143. if (ret == 0) {
  12144. if(wc_SetAltNamesBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  12145. ret = -7611;
  12146. }
  12147. if (ret == 0) {
  12148. if(wc_SetDatesBuffer(&cert, der, derSz) != 0)
  12149. ret = -7612;
  12150. }
  12151. if (ret == 0) {
  12152. if(wc_SetDatesBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  12153. ret = -7613;
  12154. }
  12155. #endif
  12156. if (ret == 0) {
  12157. if(wc_SetAuthKeyIdFromCert(&cert, der, derSz) != 0)
  12158. ret = -7614;
  12159. }
  12160. if (ret == 0) {
  12161. if(wc_SetAuthKeyIdFromCert(NULL, der, derSz) != BAD_FUNC_ARG)
  12162. ret = -7615;
  12163. }
  12164. wc_SetCert_Free(&cert);
  12165. if (ret == 0) {
  12166. if(cert.decodedCert != NULL)
  12167. ret = -7616;
  12168. }
  12169. XFREE(der, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  12170. return ret;
  12171. }
  12172. #endif /* defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) &&
  12173. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) */
  12174. #define RSA_TEST_BYTES 512 /* up to 4096-bit key */
  12175. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  12176. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  12177. static int rsa_flatten_test(RsaKey* key)
  12178. {
  12179. int ret;
  12180. byte e[RSA_TEST_BYTES];
  12181. byte n[RSA_TEST_BYTES];
  12182. word32 eSz = sizeof(e);
  12183. word32 nSz = sizeof(n);
  12184. /* Parameter Validation testing. */
  12185. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  12186. #ifdef HAVE_USER_RSA
  12187. /* Implementation using IPP Libraries returns:
  12188. * -101 = USER_CRYPTO_ERROR
  12189. */
  12190. if (ret == 0)
  12191. #else
  12192. if (ret != BAD_FUNC_ARG)
  12193. #endif
  12194. return -7620;
  12195. ret = wc_RsaFlattenPublicKey(key, NULL, &eSz, n, &nSz);
  12196. #ifdef HAVE_USER_RSA
  12197. /* Implementation using IPP Libraries returns:
  12198. * -101 = USER_CRYPTO_ERROR
  12199. */
  12200. if (ret == 0)
  12201. #else
  12202. if (ret != BAD_FUNC_ARG)
  12203. #endif
  12204. return -7621;
  12205. ret = wc_RsaFlattenPublicKey(key, e, NULL, n, &nSz);
  12206. #ifdef HAVE_USER_RSA
  12207. /* Implementation using IPP Libraries returns:
  12208. * -101 = USER_CRYPTO_ERROR
  12209. */
  12210. if (ret == 0)
  12211. #else
  12212. if (ret != BAD_FUNC_ARG)
  12213. #endif
  12214. return -7622;
  12215. ret = wc_RsaFlattenPublicKey(key, e, &eSz, NULL, &nSz);
  12216. #ifdef HAVE_USER_RSA
  12217. /* Implementation using IPP Libraries returns:
  12218. * -101 = USER_CRYPTO_ERROR
  12219. */
  12220. if (ret == 0)
  12221. #else
  12222. if (ret != BAD_FUNC_ARG)
  12223. #endif
  12224. return -7623;
  12225. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, NULL);
  12226. #ifdef HAVE_USER_RSA
  12227. /* Implementation using IPP Libraries returns:
  12228. * -101 = USER_CRYPTO_ERROR
  12229. */
  12230. if (ret == 0)
  12231. #else
  12232. if (ret != BAD_FUNC_ARG)
  12233. #endif
  12234. return -7624;
  12235. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  12236. if (ret != 0)
  12237. return -7625;
  12238. eSz = 0;
  12239. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  12240. #ifdef HAVE_USER_RSA
  12241. /* Implementation using IPP Libraries returns:
  12242. * -101 = USER_CRYPTO_ERROR
  12243. */
  12244. if (ret == 0)
  12245. #elif defined(HAVE_FIPS) && \
  12246. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
  12247. if (ret != 0)
  12248. #else
  12249. if (ret != RSA_BUFFER_E)
  12250. #endif
  12251. return -7626;
  12252. eSz = sizeof(e);
  12253. nSz = 0;
  12254. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  12255. #ifdef HAVE_USER_RSA
  12256. /* Implementation using IPP Libraries returns:
  12257. * -101 = USER_CRYPTO_ERROR
  12258. */
  12259. if (ret == 0)
  12260. #else
  12261. if (ret != RSA_BUFFER_E)
  12262. #endif
  12263. return -7627;
  12264. return 0;
  12265. }
  12266. #endif /* NO_ASN */
  12267. #if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(NO_ASN) \
  12268. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  12269. static int rsa_export_key_test(RsaKey* key)
  12270. {
  12271. int ret;
  12272. byte e[3];
  12273. word32 eSz = sizeof(e);
  12274. byte n[RSA_TEST_BYTES];
  12275. word32 nSz = sizeof(n);
  12276. byte d[RSA_TEST_BYTES];
  12277. word32 dSz = sizeof(d);
  12278. byte p[RSA_TEST_BYTES/2];
  12279. word32 pSz = sizeof(p);
  12280. byte q[RSA_TEST_BYTES/2];
  12281. word32 qSz = sizeof(q);
  12282. word32 zero = 0;
  12283. ret = wc_RsaExportKey(NULL, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12284. if (ret != BAD_FUNC_ARG)
  12285. return -7630;
  12286. ret = wc_RsaExportKey(key, NULL, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12287. if (ret != BAD_FUNC_ARG)
  12288. return -7631;
  12289. ret = wc_RsaExportKey(key, e, NULL, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12290. if (ret != BAD_FUNC_ARG)
  12291. return -7632;
  12292. ret = wc_RsaExportKey(key, e, &eSz, NULL, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12293. if (ret != BAD_FUNC_ARG)
  12294. return -7633;
  12295. ret = wc_RsaExportKey(key, e, &eSz, n, NULL, d, &dSz, p, &pSz, q, &qSz);
  12296. if (ret != BAD_FUNC_ARG)
  12297. return -7634;
  12298. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, NULL, &dSz, p, &pSz, q, &qSz);
  12299. if (ret != BAD_FUNC_ARG)
  12300. return -7635;
  12301. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, NULL, p, &pSz, q, &qSz);
  12302. if (ret != BAD_FUNC_ARG)
  12303. return -7636;
  12304. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, NULL, &pSz, q, &qSz);
  12305. if (ret != BAD_FUNC_ARG)
  12306. return -7637;
  12307. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, NULL, q, &qSz);
  12308. if (ret != BAD_FUNC_ARG)
  12309. return -7638;
  12310. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, NULL, &qSz);
  12311. if (ret != BAD_FUNC_ARG)
  12312. return -7639;
  12313. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, NULL);
  12314. if (ret != BAD_FUNC_ARG)
  12315. return -7640;
  12316. ret = wc_RsaExportKey(key, e, &zero, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12317. if (ret != RSA_BUFFER_E)
  12318. return -7641;
  12319. ret = wc_RsaExportKey(key, e, &eSz, n, &zero, d, &dSz, p, &pSz, q, &qSz);
  12320. if (ret != RSA_BUFFER_E)
  12321. return -7642;
  12322. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  12323. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &zero, p, &pSz, q, &qSz);
  12324. if (ret != RSA_BUFFER_E)
  12325. return -7643;
  12326. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &zero, q, &qSz);
  12327. if (ret != RSA_BUFFER_E)
  12328. return -7644;
  12329. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &zero);
  12330. if (ret != RSA_BUFFER_E)
  12331. return -7645;
  12332. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  12333. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12334. if (ret != 0)
  12335. return -7646;
  12336. return 0;
  12337. }
  12338. #endif /* !HAVE_FIPS && !USER_RSA && !NO_ASN */
  12339. #ifndef NO_SIG_WRAPPER
  12340. static int rsa_sig_test(RsaKey* key, word32 keyLen, int modLen, WC_RNG* rng)
  12341. {
  12342. int ret;
  12343. word32 sigSz;
  12344. WOLFSSL_SMALL_STACK_STATIC const byte in[] = TEST_STRING;
  12345. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  12346. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  12347. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  12348. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  12349. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  12350. };
  12351. WOLFSSL_SMALL_STACK_STATIC const byte hashEnc[] = {
  12352. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  12353. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  12354. 0x00, 0x04, 0x20,
  12355. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  12356. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  12357. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  12358. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  12359. };
  12360. word32 inLen = (word32)XSTRLEN((char*)in);
  12361. byte out[RSA_TEST_BYTES];
  12362. /* Parameter Validation testing. */
  12363. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_NONE, key, keyLen);
  12364. if (ret != BAD_FUNC_ARG)
  12365. return -7650;
  12366. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, 0);
  12367. if (ret != BAD_FUNC_ARG)
  12368. return -7651;
  12369. sigSz = (word32)modLen;
  12370. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  12371. inLen, out, &sigSz, key, keyLen, rng);
  12372. if (ret != BAD_FUNC_ARG)
  12373. return -7652;
  12374. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12375. 0, out, &sigSz, key, keyLen, rng);
  12376. if (ret != BAD_FUNC_ARG)
  12377. return -7653;
  12378. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12379. inLen, NULL, &sigSz, key, keyLen, rng);
  12380. if (ret != BAD_FUNC_ARG)
  12381. return -7654;
  12382. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12383. inLen, out, NULL, key, keyLen, rng);
  12384. if (ret != BAD_FUNC_ARG)
  12385. return -7655;
  12386. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12387. inLen, out, &sigSz, NULL, keyLen, rng);
  12388. if (ret != BAD_FUNC_ARG)
  12389. return -7656;
  12390. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12391. inLen, out, &sigSz, key, 0, rng);
  12392. if (ret != BAD_FUNC_ARG)
  12393. return -7657;
  12394. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12395. inLen, out, &sigSz, key, keyLen, NULL);
  12396. #ifdef HAVE_USER_RSA
  12397. /* Implementation using IPP Libraries returns:
  12398. * -101 = USER_CRYPTO_ERROR
  12399. */
  12400. if (ret == 0)
  12401. #elif defined(WOLFSSL_AFALG_XILINX_RSA) || defined(WOLFSSL_XILINX_CRYPT)
  12402. /* blinding / rng handled with hardware acceleration */
  12403. if (ret != 0)
  12404. #elif defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
  12405. /* async may not require RNG */
  12406. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  12407. if (ret != NO_VALID_DEVID)
  12408. #else
  12409. if (ret != 0 && ret != MISSING_RNG_E)
  12410. #endif
  12411. #elif defined(HAVE_FIPS) || !defined(WC_RSA_BLINDING)
  12412. /* FIPS140 implementation does not do blinding */
  12413. if (ret != 0)
  12414. #elif defined(WOLFSSL_RSA_PUBLIC_ONLY) || defined(WOLFSSL_RSA_VERIFY_ONLY)
  12415. if (ret != SIG_TYPE_E)
  12416. #elif defined(WOLFSSL_CRYPTOCELL) || defined(WOLFSSL_SE050)
  12417. /* RNG is handled by hardware */
  12418. if (ret != 0)
  12419. #else
  12420. if (ret != MISSING_RNG_E)
  12421. #endif
  12422. return -7658;
  12423. sigSz = 0;
  12424. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12425. inLen, out, &sigSz, key, keyLen, rng);
  12426. if (ret != BAD_FUNC_ARG)
  12427. return -7659;
  12428. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  12429. inLen, out, (word32)modLen, key, keyLen);
  12430. if (ret != BAD_FUNC_ARG)
  12431. return -7660;
  12432. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12433. 0, out, (word32)modLen, key, keyLen);
  12434. if (ret != BAD_FUNC_ARG)
  12435. return -7661;
  12436. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12437. inLen, NULL, (word32)modLen, key, keyLen);
  12438. if (ret != BAD_FUNC_ARG)
  12439. return -7662;
  12440. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12441. inLen, out, 0, key, keyLen);
  12442. if (ret != BAD_FUNC_ARG)
  12443. return -7663;
  12444. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12445. inLen, out, (word32)modLen, NULL, keyLen);
  12446. if (ret != BAD_FUNC_ARG)
  12447. return -7664;
  12448. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12449. inLen, out, (word32)modLen, key, 0);
  12450. if (ret != BAD_FUNC_ARG)
  12451. return -7665;
  12452. #ifndef HAVE_ECC
  12453. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, keyLen);
  12454. if (ret != SIG_TYPE_E)
  12455. return -7666;
  12456. #endif
  12457. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  12458. return 0;
  12459. #endif
  12460. /* Use APIs. */
  12461. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, keyLen);
  12462. if (ret != modLen)
  12463. return -7667;
  12464. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA_W_ENC, key, keyLen);
  12465. if (ret != modLen)
  12466. return -7668;
  12467. sigSz = (word32)ret;
  12468. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  12469. XMEMSET(out, 0, sizeof(out));
  12470. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12471. inLen, out, &sigSz, key, keyLen, rng);
  12472. if (ret != 0)
  12473. return -7669;
  12474. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12475. inLen, out, (word32)modLen, key, keyLen);
  12476. if (ret != 0)
  12477. return -7670;
  12478. sigSz = (word32)sizeof(out);
  12479. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  12480. in, inLen, out, &sigSz, key, keyLen, rng);
  12481. if (ret != 0)
  12482. return -7671;
  12483. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  12484. in, inLen, out, (word32)modLen, key, keyLen);
  12485. if (ret != 0)
  12486. return -7672;
  12487. /* Wrong signature type. */
  12488. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12489. inLen, out, (word32)modLen, key, keyLen);
  12490. if (ret == 0)
  12491. return -7673;
  12492. /* check hash functions */
  12493. sigSz = (word32)sizeof(out);
  12494. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  12495. hash, (int)sizeof(hash), out, &sigSz, key, keyLen, rng);
  12496. if (ret != 0)
  12497. return -7674;
  12498. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  12499. hash, (int)sizeof(hash), out, (word32)modLen, key, keyLen);
  12500. if (ret != 0)
  12501. return -7675;
  12502. sigSz = (word32)sizeof(out);
  12503. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  12504. hashEnc, (int)sizeof(hashEnc), out, &sigSz, key, keyLen, rng);
  12505. if (ret != 0)
  12506. return -7676;
  12507. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  12508. hashEnc, (int)sizeof(hashEnc), out, (word32)modLen, key, keyLen);
  12509. if (ret != 0)
  12510. return -7677;
  12511. #else
  12512. (void)hash;
  12513. (void)hashEnc;
  12514. #endif /* !WOLFSSL_RSA_PUBLIC_ONLY && !WOLFSSL_RSA_VERIFY_ONLY */
  12515. return 0;
  12516. }
  12517. #endif /* !NO_SIG_WRAPPER */
  12518. #ifdef WC_RSA_NONBLOCK
  12519. static int rsa_nb_test(RsaKey* key, const byte* in, word32 inLen, byte* out,
  12520. word32 outSz, byte* plain, word32 plainSz, WC_RNG* rng)
  12521. {
  12522. int ret = 0, count;
  12523. int signSz = 0;
  12524. RsaNb nb;
  12525. byte* inlinePlain = NULL;
  12526. /* Enable non-blocking RSA mode - provide context */
  12527. ret = wc_RsaSetNonBlock(key, &nb);
  12528. if (ret != 0)
  12529. return ret;
  12530. #ifdef WC_RSA_NONBLOCK_TIME
  12531. /* Enable time based RSA blocking. 8 microseconds max (3.1GHz) */
  12532. ret = wc_RsaSetNonBlockTime(key, 8, 3100);
  12533. if (ret != 0)
  12534. return ret;
  12535. #endif
  12536. count = 0;
  12537. do {
  12538. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, rng);
  12539. count++; /* track number of would blocks */
  12540. if (ret == FP_WOULDBLOCK) {
  12541. /* do "other" work here */
  12542. }
  12543. } while (ret == FP_WOULDBLOCK);
  12544. if (ret < 0) {
  12545. return ret;
  12546. }
  12547. #ifdef DEBUG_WOLFSSL
  12548. printf("RSA non-block sign: %d times\n", count);
  12549. #endif
  12550. signSz = ret;
  12551. /* Test non-blocking verify */
  12552. XMEMSET(plain, 0, plainSz);
  12553. count = 0;
  12554. do {
  12555. ret = wc_RsaSSL_Verify(out, (word32)signSz, plain, plainSz, key);
  12556. count++; /* track number of would blocks */
  12557. if (ret == FP_WOULDBLOCK) {
  12558. /* do "other" work here */
  12559. }
  12560. } while (ret == FP_WOULDBLOCK);
  12561. if (ret < 0) {
  12562. return ret;
  12563. }
  12564. #ifdef DEBUG_WOLFSSL
  12565. printf("RSA non-block verify: %d times\n", count);
  12566. #endif
  12567. if (signSz == ret && XMEMCMP(plain, in, (size_t)ret)) {
  12568. return SIG_VERIFY_E;
  12569. }
  12570. /* Test inline non-blocking verify */
  12571. count = 0;
  12572. do {
  12573. ret = wc_RsaSSL_VerifyInline(out, (word32)signSz, &inlinePlain, key);
  12574. count++; /* track number of would blocks */
  12575. if (ret == FP_WOULDBLOCK) {
  12576. /* do "other" work here */
  12577. }
  12578. } while (ret == FP_WOULDBLOCK);
  12579. if (ret < 0) {
  12580. return ret;
  12581. }
  12582. #ifdef DEBUG_WOLFSSL
  12583. printf("RSA non-block inline verify: %d times\n", count);
  12584. #endif
  12585. if (signSz == ret && XMEMCMP(inlinePlain, in, (size_t)ret)) {
  12586. return SIG_VERIFY_E;
  12587. }
  12588. /* Disabling non-block RSA mode */
  12589. ret = wc_RsaSetNonBlock(key, NULL);
  12590. (void)count;
  12591. return 0;
  12592. }
  12593. #endif
  12594. #if !defined(HAVE_USER_RSA) && !defined(NO_ASN)
  12595. static int rsa_decode_test(RsaKey* keyPub)
  12596. {
  12597. int ret;
  12598. word32 inSz;
  12599. word32 inOutIdx;
  12600. WOLFSSL_SMALL_STACK_STATIC const byte n[2] = { 0x00, 0x23 };
  12601. WOLFSSL_SMALL_STACK_STATIC const byte e[2] = { 0x00, 0x03 };
  12602. WOLFSSL_SMALL_STACK_STATIC const byte good[] = { 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1,
  12603. 0x03 };
  12604. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgId[] = {
  12605. 0x30, 0x18, 0x30, 0x16,
  12606. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12607. 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12608. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgIdNull[] = {
  12609. 0x30, 0x1a, 0x30, 0x18,
  12610. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12611. 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  12612. 0x02, 0x1, 0x03 };
  12613. WOLFSSL_SMALL_STACK_STATIC const byte badAlgIdNull[] = {
  12614. 0x30, 0x1b, 0x30, 0x19,
  12615. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12616. 0x05, 0x01, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  12617. 0x02, 0x1, 0x03 };
  12618. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitString[] = {
  12619. 0x30, 0x18, 0x30, 0x16,
  12620. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12621. 0x04, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12622. WOLFSSL_SMALL_STACK_STATIC const byte badBitStringLen[] = {
  12623. 0x30, 0x18, 0x30, 0x16,
  12624. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12625. 0x03, 0x0a, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12626. WOLFSSL_SMALL_STACK_STATIC const byte badNoSeq[] = {
  12627. 0x30, 0x16, 0x30, 0x14,
  12628. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12629. 0x07, 0x00, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12630. WOLFSSL_SMALL_STACK_STATIC const byte badNoObj[] = {
  12631. 0x30, 0x0f, 0x30, 0x0d, 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06,
  12632. 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12633. WOLFSSL_SMALL_STACK_STATIC const byte badIntN[] = {
  12634. 0x30, 0x06, 0x02, 0x05, 0x23, 0x02, 0x1, 0x03 };
  12635. WOLFSSL_SMALL_STACK_STATIC const byte badNotIntE[] = {
  12636. 0x30, 0x06, 0x02, 0x01, 0x23, 0x04, 0x1, 0x03 };
  12637. WOLFSSL_SMALL_STACK_STATIC const byte badLength[] = {
  12638. 0x30, 0x04, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12639. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrNoZero[] = {
  12640. 0x30, 0x17, 0x30, 0x15,
  12641. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12642. 0x03, 0x08, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12643. ret = wc_InitRsaKey(keyPub, NULL);
  12644. if (ret != 0)
  12645. return -7690;
  12646. /* Parameter Validation testing. */
  12647. ret = wc_RsaPublicKeyDecodeRaw(NULL, sizeof(n), e, sizeof(e), keyPub);
  12648. if (ret != BAD_FUNC_ARG) {
  12649. ret = -7691;
  12650. goto done;
  12651. }
  12652. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), NULL, sizeof(e), keyPub);
  12653. if (ret != BAD_FUNC_ARG) {
  12654. ret = -7692;
  12655. goto done;
  12656. }
  12657. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), NULL);
  12658. if (ret != BAD_FUNC_ARG) {
  12659. ret = -7693;
  12660. goto done;
  12661. }
  12662. ret = wc_RsaPublicKeyDecodeRaw(n, (word32)-1, e, sizeof(e), keyPub);
  12663. #if !defined(WOLFSSL_SP_MATH) & !defined(WOLFSSL_SP_MATH_ALL)
  12664. if (ret != 0) {
  12665. #else
  12666. if (ret != ASN_GETINT_E) {
  12667. #endif
  12668. ret = -7694;
  12669. goto done;
  12670. }
  12671. wc_FreeRsaKey(keyPub);
  12672. ret = wc_InitRsaKey(keyPub, NULL);
  12673. if (ret != 0)
  12674. return -7695;
  12675. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, (word32)-1, keyPub);
  12676. #if !defined(WOLFSSL_SP_MATH) & !defined(WOLFSSL_SP_MATH_ALL)
  12677. if (ret != 0) {
  12678. #else
  12679. if (ret != ASN_GETINT_E) {
  12680. #endif
  12681. ret = -7696;
  12682. goto done;
  12683. }
  12684. wc_FreeRsaKey(keyPub);
  12685. ret = wc_InitRsaKey(keyPub, NULL);
  12686. if (ret != 0)
  12687. return -7697;
  12688. /* Use API. */
  12689. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), keyPub);
  12690. if (ret != 0) {
  12691. ret = -7698;
  12692. goto done;
  12693. }
  12694. wc_FreeRsaKey(keyPub);
  12695. ret = wc_InitRsaKey(keyPub, NULL);
  12696. if (ret != 0)
  12697. return -7699;
  12698. /* Parameter Validation testing. */
  12699. inSz = sizeof(good);
  12700. ret = wc_RsaPublicKeyDecode(NULL, &inOutIdx, keyPub, inSz);
  12701. if (ret != BAD_FUNC_ARG) {
  12702. ret = -7700;
  12703. goto done;
  12704. }
  12705. ret = wc_RsaPublicKeyDecode(good, NULL, keyPub, inSz);
  12706. if (ret != BAD_FUNC_ARG) {
  12707. ret = -7701;
  12708. goto done;
  12709. }
  12710. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  12711. if (ret != BAD_FUNC_ARG) {
  12712. ret = -7702;
  12713. goto done;
  12714. }
  12715. /* Use good data and offset to bad data. */
  12716. inOutIdx = 2;
  12717. inSz = sizeof(good) - inOutIdx;
  12718. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  12719. if (ret != ASN_PARSE_E) {
  12720. ret = -7703;
  12721. goto done;
  12722. }
  12723. inOutIdx = 2;
  12724. inSz = sizeof(goodAlgId) - inOutIdx;
  12725. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  12726. if (ret != ASN_PARSE_E) {
  12727. ret = -7704;
  12728. goto done;
  12729. }
  12730. inOutIdx = 2;
  12731. inSz = sizeof(goodAlgId);
  12732. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  12733. #ifndef WOLFSSL_NO_DECODE_EXTRA
  12734. if (ret != ASN_PARSE_E)
  12735. #else
  12736. if (ret != ASN_RSA_KEY_E)
  12737. #endif
  12738. {
  12739. ret = -7705;
  12740. goto done;
  12741. }
  12742. /* Try different bad data. */
  12743. inSz = sizeof(badAlgIdNull);
  12744. inOutIdx = 0;
  12745. ret = wc_RsaPublicKeyDecode(badAlgIdNull, &inOutIdx, keyPub, inSz);
  12746. if (ret != ASN_EXPECT_0_E) {
  12747. ret = -7706;
  12748. goto done;
  12749. }
  12750. inSz = sizeof(badNotBitString);
  12751. inOutIdx = 0;
  12752. ret = wc_RsaPublicKeyDecode(badNotBitString, &inOutIdx, keyPub, inSz);
  12753. if (ret != ASN_BITSTR_E) {
  12754. ret = -7707;
  12755. goto done;
  12756. }
  12757. inSz = sizeof(badBitStringLen);
  12758. inOutIdx = 0;
  12759. ret = wc_RsaPublicKeyDecode(badBitStringLen, &inOutIdx, keyPub, inSz);
  12760. if (ret != ASN_PARSE_E) {
  12761. ret = -7708;
  12762. goto done;
  12763. }
  12764. inSz = sizeof(badNoSeq);
  12765. inOutIdx = 0;
  12766. ret = wc_RsaPublicKeyDecode(badNoSeq, &inOutIdx, keyPub, inSz);
  12767. if (ret != ASN_PARSE_E) {
  12768. ret = -7709;
  12769. goto done;
  12770. }
  12771. inSz = sizeof(badNoObj);
  12772. inOutIdx = 0;
  12773. ret = wc_RsaPublicKeyDecode(badNoObj, &inOutIdx, keyPub, inSz);
  12774. if (ret != ASN_PARSE_E && ret != ASN_OBJECT_ID_E) {
  12775. ret = -7710;
  12776. goto done;
  12777. }
  12778. inSz = sizeof(badIntN);
  12779. inOutIdx = 0;
  12780. ret = wc_RsaPublicKeyDecode(badIntN, &inOutIdx, keyPub, inSz);
  12781. if (ret != ASN_RSA_KEY_E && ret != ASN_PARSE_E) {
  12782. ret = -7711;
  12783. goto done;
  12784. }
  12785. inSz = sizeof(badNotIntE);
  12786. inOutIdx = 0;
  12787. ret = wc_RsaPublicKeyDecode(badNotIntE, &inOutIdx, keyPub, inSz);
  12788. if (ret != ASN_RSA_KEY_E && ret != ASN_PARSE_E) {
  12789. ret = -7712;
  12790. goto done;
  12791. }
  12792. /* TODO: Shouldn't pass as the sequence length is too small. */
  12793. inSz = sizeof(badLength);
  12794. inOutIdx = 0;
  12795. ret = wc_RsaPublicKeyDecode(badLength, &inOutIdx, keyPub, inSz);
  12796. #ifndef WOLFSSL_ASN_TEMPLATE
  12797. if (ret != 0)
  12798. #else
  12799. if (ret != ASN_PARSE_E)
  12800. #endif
  12801. {
  12802. ret = -7713;
  12803. goto done;
  12804. }
  12805. /* TODO: Shouldn't ignore object id's data. */
  12806. wc_FreeRsaKey(keyPub);
  12807. ret = wc_InitRsaKey(keyPub, NULL);
  12808. if (ret != 0)
  12809. return -7714;
  12810. inSz = sizeof(badBitStrNoZero);
  12811. inOutIdx = 0;
  12812. ret = wc_RsaPublicKeyDecode(badBitStrNoZero, &inOutIdx, keyPub, inSz);
  12813. if (ret != ASN_EXPECT_0_E && ret != ASN_PARSE_E) {
  12814. ret = -7715;
  12815. goto done;
  12816. }
  12817. wc_FreeRsaKey(keyPub);
  12818. ret = wc_InitRsaKey(keyPub, NULL);
  12819. if (ret != 0)
  12820. return -7716;
  12821. /* Valid data cases. */
  12822. inSz = sizeof(good);
  12823. inOutIdx = 0;
  12824. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  12825. if (ret != 0) {
  12826. ret = -7717;
  12827. goto done;
  12828. }
  12829. if (inOutIdx != inSz) {
  12830. ret = -7718;
  12831. goto done;
  12832. }
  12833. wc_FreeRsaKey(keyPub);
  12834. ret = wc_InitRsaKey(keyPub, NULL);
  12835. if (ret != 0)
  12836. return -7719;
  12837. inSz = sizeof(goodAlgId);
  12838. inOutIdx = 0;
  12839. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  12840. if (ret != 0) {
  12841. ret = -7720;
  12842. goto done;
  12843. }
  12844. if (inOutIdx != inSz) {
  12845. ret = -7721;
  12846. goto done;
  12847. }
  12848. wc_FreeRsaKey(keyPub);
  12849. ret = wc_InitRsaKey(keyPub, NULL);
  12850. if (ret != 0)
  12851. return -7722;
  12852. inSz = sizeof(goodAlgIdNull);
  12853. inOutIdx = 0;
  12854. ret = wc_RsaPublicKeyDecode(goodAlgIdNull, &inOutIdx, keyPub, inSz);
  12855. if (ret != 0) {
  12856. ret = -7723;
  12857. goto done;
  12858. }
  12859. if (inOutIdx != inSz) {
  12860. ret = -7724;
  12861. goto done;
  12862. }
  12863. done:
  12864. wc_FreeRsaKey(keyPub);
  12865. return ret;
  12866. }
  12867. #endif
  12868. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  12869. /* Need to create known good signatures to test with this. */
  12870. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  12871. !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  12872. static int rsa_pss_test(WC_RNG* rng, RsaKey* key)
  12873. {
  12874. byte digest[WC_MAX_DIGEST_SIZE];
  12875. int ret = 0;
  12876. const char inStr[] = TEST_STRING;
  12877. word32 inLen = (word32)TEST_STRING_SZ;
  12878. word32 outSz;
  12879. word32 plainSz;
  12880. word32 digestSz;
  12881. int i, j;
  12882. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  12883. int k, l;
  12884. #endif
  12885. #ifndef WOLFSSL_SE050
  12886. int len;
  12887. #endif
  12888. byte* plain;
  12889. int mgf[] = {
  12890. #ifndef NO_SHA
  12891. WC_MGF1SHA1,
  12892. #endif
  12893. #ifdef WOLFSSL_SHA224
  12894. WC_MGF1SHA224,
  12895. #endif
  12896. WC_MGF1SHA256,
  12897. #ifdef WOLFSSL_SHA384
  12898. WC_MGF1SHA384,
  12899. #endif
  12900. #ifdef WOLFSSL_SHA512
  12901. WC_MGF1SHA512
  12902. #endif
  12903. };
  12904. enum wc_HashType hash[] = {
  12905. #ifndef NO_SHA
  12906. WC_HASH_TYPE_SHA,
  12907. #endif
  12908. #ifdef WOLFSSL_SHA224
  12909. WC_HASH_TYPE_SHA224,
  12910. #endif
  12911. WC_HASH_TYPE_SHA256,
  12912. #ifdef WOLFSSL_SHA384
  12913. WC_HASH_TYPE_SHA384,
  12914. #endif
  12915. #ifdef WOLFSSL_SHA512
  12916. WC_HASH_TYPE_SHA512,
  12917. #endif
  12918. };
  12919. WC_DECLARE_VAR(in, byte, RSA_TEST_BYTES, HEAP_HINT);
  12920. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  12921. WC_DECLARE_VAR(sig, byte, RSA_TEST_BYTES, HEAP_HINT);
  12922. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  12923. if (in == NULL || out == NULL || sig == NULL)
  12924. ERROR_OUT(MEMORY_E, exit_rsa_pss);
  12925. #endif
  12926. XMEMCPY(in, inStr, inLen);
  12927. /* Test all combinations of hash and MGF. */
  12928. for (j = 0; j < (int)(sizeof(hash)/sizeof(*hash)); j++) {
  12929. /* Calculate hash of message. */
  12930. ret = wc_Hash(hash[j], in, inLen, digest, sizeof(digest));
  12931. if (ret != 0)
  12932. ERROR_OUT(-7730, exit_rsa_pss);
  12933. digestSz = wc_HashGetDigestSize(hash[j]);
  12934. #ifdef WOLFSSL_SE050
  12935. /* SE050 only supports MGF matched to same hash type */
  12936. i = j;
  12937. #else
  12938. for (i = 0; i < (int)(sizeof(mgf)/sizeof(*mgf)); i++) {
  12939. #endif
  12940. outSz = RSA_TEST_BYTES;
  12941. do {
  12942. #if defined(WOLFSSL_ASYNC_CRYPT)
  12943. ret = wc_AsyncWait(ret, &key->asyncDev,
  12944. WC_ASYNC_FLAG_CALL_AGAIN);
  12945. #endif
  12946. if (ret >= 0) {
  12947. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz,
  12948. hash[j], mgf[i], -1, key, rng);
  12949. }
  12950. } while (ret == WC_PENDING_E);
  12951. if (ret <= 0)
  12952. ERROR_OUT(-7731, exit_rsa_pss);
  12953. outSz = ret;
  12954. XMEMCPY(sig, out, outSz);
  12955. plain = NULL;
  12956. TEST_SLEEP();
  12957. do {
  12958. #if defined(WOLFSSL_ASYNC_CRYPT)
  12959. ret = wc_AsyncWait(ret, &key->asyncDev,
  12960. WC_ASYNC_FLAG_CALL_AGAIN);
  12961. #endif
  12962. if (ret >= 0) {
  12963. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[j],
  12964. mgf[i], -1, key);
  12965. }
  12966. } while (ret == WC_PENDING_E);
  12967. if (ret <= 0)
  12968. ERROR_OUT(-7732, exit_rsa_pss);
  12969. plainSz = ret;
  12970. TEST_SLEEP();
  12971. #if defined(HAVE_SELFTEST) && \
  12972. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  12973. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  12974. hash[j], -1);
  12975. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  12976. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  12977. hash[j], -1, 0);
  12978. #else
  12979. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz,
  12980. hash[j], -1, wc_RsaEncryptSize(key)*8, HEAP_HINT);
  12981. #endif
  12982. if (ret != 0)
  12983. ERROR_OUT(-7733, exit_rsa_pss);
  12984. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  12985. for (k = 0; k < (int)(sizeof(mgf)/sizeof(*mgf)); k++) {
  12986. for (l = 0; l < (int)(sizeof(hash)/sizeof(*hash)); l++) {
  12987. if (i == k && j == l)
  12988. continue;
  12989. XMEMCPY(sig, out, outSz);
  12990. do {
  12991. #if defined(WOLFSSL_ASYNC_CRYPT)
  12992. ret = wc_AsyncWait(ret, &key->asyncDev,
  12993. WC_ASYNC_FLAG_CALL_AGAIN);
  12994. #endif
  12995. if (ret >= 0) {
  12996. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz,
  12997. (byte**)&plain, hash[l], mgf[k], -1, key);
  12998. }
  12999. } while (ret == WC_PENDING_E);
  13000. if (ret >= 0)
  13001. ERROR_OUT(-7734, exit_rsa_pss);
  13002. }
  13003. }
  13004. #endif
  13005. #ifndef WOLFSSL_SE050
  13006. } /* end mgf for loop */
  13007. #endif
  13008. }
  13009. /* SE050 generates salts internally only of hash length */
  13010. #ifndef WOLFSSL_SE050
  13011. /* Test that a salt length of zero works. */
  13012. digestSz = wc_HashGetDigestSize(hash[0]);
  13013. outSz = RSA_TEST_BYTES;
  13014. do {
  13015. #if defined(WOLFSSL_ASYNC_CRYPT)
  13016. ret = wc_AsyncWait(ret, &key->asyncDev,
  13017. WC_ASYNC_FLAG_CALL_AGAIN);
  13018. #endif
  13019. if (ret >= 0) {
  13020. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  13021. mgf[0], 0, key, rng);
  13022. }
  13023. } while (ret == WC_PENDING_E);
  13024. if (ret <= 0)
  13025. ERROR_OUT(-7735, exit_rsa_pss);
  13026. outSz = ret;
  13027. TEST_SLEEP();
  13028. do {
  13029. #if defined(WOLFSSL_ASYNC_CRYPT)
  13030. ret = wc_AsyncWait(ret, &key->asyncDev,
  13031. WC_ASYNC_FLAG_CALL_AGAIN);
  13032. #endif
  13033. if (ret >= 0) {
  13034. ret = wc_RsaPSS_Verify_ex(out, outSz, sig, outSz, hash[0], mgf[0],
  13035. 0, key);
  13036. }
  13037. } while (ret == WC_PENDING_E);
  13038. if (ret <= 0)
  13039. ERROR_OUT(-7736, exit_rsa_pss);
  13040. plainSz = ret;
  13041. TEST_SLEEP();
  13042. do {
  13043. #if defined(WOLFSSL_ASYNC_CRYPT)
  13044. ret = wc_AsyncWait(ret, &key->asyncDev,
  13045. WC_ASYNC_FLAG_CALL_AGAIN);
  13046. #endif
  13047. if (ret >= 0) {
  13048. #if defined(HAVE_SELFTEST) && \
  13049. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  13050. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  13051. hash[0], 0);
  13052. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  13053. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  13054. hash[0], 0, 0);
  13055. #else
  13056. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, sig, plainSz,
  13057. hash[0], 0, 0, HEAP_HINT);
  13058. #endif
  13059. }
  13060. } while (ret == WC_PENDING_E);
  13061. if (ret != 0)
  13062. ERROR_OUT(-7737, exit_rsa_pss);
  13063. XMEMCPY(sig, out, outSz);
  13064. plain = NULL;
  13065. do {
  13066. #if defined(WOLFSSL_ASYNC_CRYPT)
  13067. ret = wc_AsyncWait(ret, &key->asyncDev,
  13068. WC_ASYNC_FLAG_CALL_AGAIN);
  13069. #endif
  13070. if (ret >= 0) {
  13071. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  13072. 0, key);
  13073. }
  13074. } while (ret == WC_PENDING_E);
  13075. if (ret <= 0)
  13076. ERROR_OUT(-7738, exit_rsa_pss);
  13077. plainSz = ret;
  13078. TEST_SLEEP();
  13079. #if defined(HAVE_SELFTEST) && \
  13080. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  13081. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  13082. hash[0], 0);
  13083. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  13084. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  13085. hash[0], 0, 0);
  13086. #else
  13087. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  13088. 0, 0, HEAP_HINT);
  13089. #endif
  13090. if (ret != 0)
  13091. ERROR_OUT(-7739, exit_rsa_pss);
  13092. /* Test bad salt lengths in various APIs. */
  13093. digestSz = wc_HashGetDigestSize(hash[0]);
  13094. outSz = RSA_TEST_BYTES;
  13095. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  13096. len = -2;
  13097. #else
  13098. len = -3;
  13099. #endif
  13100. do {
  13101. #if defined(WOLFSSL_ASYNC_CRYPT)
  13102. ret = wc_AsyncWait(ret, &key->asyncDev,
  13103. WC_ASYNC_FLAG_CALL_AGAIN);
  13104. #endif
  13105. if (ret >= 0) {
  13106. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  13107. mgf[0], len, key, rng);
  13108. }
  13109. } while (ret == WC_PENDING_E);
  13110. if (ret != PSS_SALTLEN_E)
  13111. ERROR_OUT(-7740, exit_rsa_pss);
  13112. do {
  13113. #if defined(WOLFSSL_ASYNC_CRYPT)
  13114. ret = wc_AsyncWait(ret, &key->asyncDev,
  13115. WC_ASYNC_FLAG_CALL_AGAIN);
  13116. #endif
  13117. if (ret >= 0) {
  13118. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  13119. mgf[0], digestSz + 1, key, rng);
  13120. }
  13121. } while (ret == WC_PENDING_E);
  13122. if (ret != PSS_SALTLEN_E)
  13123. ERROR_OUT(-7741, exit_rsa_pss);
  13124. TEST_SLEEP();
  13125. do {
  13126. #if defined(WOLFSSL_ASYNC_CRYPT)
  13127. ret = wc_AsyncWait(ret, &key->asyncDev,
  13128. WC_ASYNC_FLAG_CALL_AGAIN);
  13129. #endif
  13130. if (ret >= 0) {
  13131. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0],
  13132. mgf[0], -2, key);
  13133. }
  13134. } while (ret == WC_PENDING_E);
  13135. if (ret != PSS_SALTLEN_E)
  13136. ERROR_OUT(-7742, exit_rsa_pss);
  13137. TEST_SLEEP();
  13138. do {
  13139. #if defined(WOLFSSL_ASYNC_CRYPT)
  13140. ret = wc_AsyncWait(ret, &key->asyncDev,
  13141. WC_ASYNC_FLAG_CALL_AGAIN);
  13142. #endif
  13143. if (ret >= 0) {
  13144. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  13145. digestSz + 1, key);
  13146. }
  13147. } while (ret == WC_PENDING_E);
  13148. if (ret != PSS_SALTLEN_E)
  13149. ERROR_OUT(-7743, exit_rsa_pss);
  13150. TEST_SLEEP();
  13151. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  13152. len = -2;
  13153. #else
  13154. len = -3;
  13155. #endif
  13156. #if defined(HAVE_SELFTEST) && \
  13157. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  13158. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  13159. hash[0], len);
  13160. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  13161. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  13162. hash[0], len, 0);
  13163. #else
  13164. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  13165. len, 0, HEAP_HINT);
  13166. #endif
  13167. if (ret != PSS_SALTLEN_E)
  13168. ERROR_OUT(-7744, exit_rsa_pss);
  13169. #ifndef WOLFSSL_PSS_LONG_SALT
  13170. len = digestSz + 1;
  13171. #else
  13172. len = plainSz - digestSz - 1;
  13173. #endif
  13174. #if defined(HAVE_SELFTEST) && \
  13175. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  13176. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  13177. hash[0], len);
  13178. if (ret != PSS_SALTLEN_E)
  13179. ERROR_OUT(-7745, exit_rsa_pss);
  13180. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  13181. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  13182. hash[0], len, 0);
  13183. if (ret != BAD_PADDING_E)
  13184. ERROR_OUT(-7745, exit_rsa_pss);
  13185. #else
  13186. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  13187. len, 0, HEAP_HINT);
  13188. if (ret != PSS_SALTLEN_E)
  13189. ERROR_OUT(-7745, exit_rsa_pss);
  13190. #endif
  13191. ret = 0;
  13192. #endif /* WOLFSSL_SE050 */
  13193. exit_rsa_pss:
  13194. WC_FREE_VAR(sig, HEAP_HINT);
  13195. WC_FREE_VAR(in, HEAP_HINT);
  13196. WC_FREE_VAR(out, HEAP_HINT);
  13197. return ret;
  13198. }
  13199. #endif /* !WOLFSSL_RSA_VERIFY_ONLY && !WOLFSSL_RSA_PUBLIC_ONLY */
  13200. #endif
  13201. #ifdef WC_RSA_NO_PADDING
  13202. WOLFSSL_TEST_SUBROUTINE int rsa_no_pad_test(void)
  13203. {
  13204. WC_RNG rng;
  13205. byte* tmp = NULL;
  13206. size_t bytes;
  13207. int ret;
  13208. word32 inLen = 0;
  13209. word32 idx = 0;
  13210. word32 outSz = RSA_TEST_BYTES;
  13211. word32 plainSz = RSA_TEST_BYTES;
  13212. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  13213. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  13214. !defined(NO_FILESYSTEM)
  13215. XFILE file;
  13216. #endif
  13217. WC_DECLARE_VAR(key, RsaKey, 1, HEAP_HINT);
  13218. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  13219. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  13220. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  13221. if (key == NULL || out == NULL || plain == NULL)
  13222. ERROR_OUT(MEMORY_E, exit_rsa_nopadding);
  13223. #endif
  13224. /* initialize stack structures */
  13225. XMEMSET(&rng, 0, sizeof(rng));
  13226. XMEMSET(key, 0, sizeof(RsaKey));
  13227. #ifdef USE_CERT_BUFFERS_1024
  13228. bytes = (size_t)sizeof_client_key_der_1024;
  13229. if (bytes < (size_t)sizeof_client_cert_der_1024)
  13230. bytes = (size_t)sizeof_client_cert_der_1024;
  13231. #elif defined(USE_CERT_BUFFERS_2048)
  13232. bytes = (size_t)sizeof_client_key_der_2048;
  13233. if (bytes < (size_t)sizeof_client_cert_der_2048)
  13234. bytes = (size_t)sizeof_client_cert_der_2048;
  13235. #else
  13236. bytes = FOURK_BUF;
  13237. #endif
  13238. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13239. if (tmp == NULL
  13240. #ifdef WOLFSSL_ASYNC_CRYPT
  13241. || out == NULL || plain == NULL
  13242. #endif
  13243. ) {
  13244. ERROR_OUT(-7800, exit_rsa_nopadding);
  13245. }
  13246. #ifdef USE_CERT_BUFFERS_1024
  13247. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  13248. #elif defined(USE_CERT_BUFFERS_2048)
  13249. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  13250. #elif defined(USE_CERT_BUFFERS_3072)
  13251. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  13252. #elif defined(USE_CERT_BUFFERS_4096)
  13253. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  13254. #elif !defined(NO_FILESYSTEM)
  13255. file = XFOPEN(clientKey, "rb");
  13256. if (!file) {
  13257. err_sys("can't open clientKey, Please run from wolfSSL home dir", -40);
  13258. ERROR_OUT(-7801, exit_rsa_nopadding);
  13259. }
  13260. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  13261. XFCLOSE(file);
  13262. #else
  13263. /* No key to use. */
  13264. ERROR_OUT(-7802, exit_rsa_nopadding);
  13265. #endif /* USE_CERT_BUFFERS */
  13266. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  13267. if (ret != 0) {
  13268. ERROR_OUT(-7803, exit_rsa_nopadding);
  13269. }
  13270. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  13271. if (ret != 0) {
  13272. ERROR_OUT(-7804, exit_rsa_nopadding);
  13273. }
  13274. /* after loading in key use tmp as the test buffer */
  13275. #ifndef HAVE_FIPS
  13276. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  13277. #else
  13278. ret = wc_InitRng(&rng);
  13279. #endif
  13280. if (ret != 0) {
  13281. ERROR_OUT(-7805, exit_rsa_nopadding);
  13282. }
  13283. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  13284. inLen = wc_RsaEncryptSize(key);
  13285. outSz = inLen;
  13286. plainSz = inLen;
  13287. XMEMSET(tmp, 7, inLen);
  13288. do {
  13289. #if defined(WOLFSSL_ASYNC_CRYPT)
  13290. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13291. #endif
  13292. if (ret >= 0) {
  13293. ret = wc_RsaDirect(tmp, inLen, out, &outSz, key,
  13294. RSA_PRIVATE_ENCRYPT, &rng);
  13295. }
  13296. } while (ret == WC_PENDING_E);
  13297. if (ret <= 0) {
  13298. ERROR_OUT(-7806, exit_rsa_nopadding);
  13299. }
  13300. /* encrypted result should not be the same as input */
  13301. if (XMEMCMP(out, tmp, inLen) == 0) {
  13302. ERROR_OUT(-7807, exit_rsa_nopadding);
  13303. }
  13304. TEST_SLEEP();
  13305. /* decrypt with public key and compare result */
  13306. do {
  13307. #if defined(WOLFSSL_ASYNC_CRYPT)
  13308. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13309. #endif
  13310. if (ret >= 0) {
  13311. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key,
  13312. RSA_PUBLIC_DECRYPT, &rng);
  13313. }
  13314. } while (ret == WC_PENDING_E);
  13315. if (ret <= 0) {
  13316. ERROR_OUT(-7808, exit_rsa_nopadding);
  13317. }
  13318. if (XMEMCMP(plain, tmp, inLen) != 0) {
  13319. ERROR_OUT(-7809, exit_rsa_nopadding);
  13320. }
  13321. TEST_SLEEP();
  13322. #endif
  13323. #ifdef WC_RSA_BLINDING
  13324. ret = wc_RsaSetRNG(NULL, &rng);
  13325. if (ret != BAD_FUNC_ARG) {
  13326. ERROR_OUT(-7810, exit_rsa_nopadding);
  13327. }
  13328. ret = wc_RsaSetRNG(key, &rng);
  13329. if (ret < 0) {
  13330. ERROR_OUT(-7811, exit_rsa_nopadding);
  13331. }
  13332. #endif
  13333. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  13334. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  13335. do {
  13336. #if defined(WOLFSSL_ASYNC_CRYPT)
  13337. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13338. #endif
  13339. if (ret >= 0) {
  13340. ret = wc_RsaPublicEncrypt_ex(tmp, inLen, out, (int)outSz, key, &rng,
  13341. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  13342. }
  13343. } while (ret == WC_PENDING_E);
  13344. if (ret < 0) {
  13345. ERROR_OUT(-7812, exit_rsa_nopadding);
  13346. }
  13347. TEST_SLEEP();
  13348. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  13349. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13350. do {
  13351. #if defined(WOLFSSL_ASYNC_CRYPT)
  13352. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13353. #endif
  13354. if (ret >= 0) {
  13355. ret = wc_RsaPrivateDecrypt_ex(out, outSz, plain, (int)plainSz, key,
  13356. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  13357. }
  13358. } while (ret == WC_PENDING_E);
  13359. if (ret < 0) {
  13360. ERROR_OUT(-7813, exit_rsa_nopadding);
  13361. }
  13362. if (XMEMCMP(plain, tmp, inLen) != 0) {
  13363. ERROR_OUT(-7814, exit_rsa_nopadding);
  13364. }
  13365. TEST_SLEEP();
  13366. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13367. /* test some bad arguments */
  13368. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key, -1,
  13369. &rng);
  13370. if (ret != BAD_FUNC_ARG) {
  13371. ERROR_OUT(-7815, exit_rsa_nopadding);
  13372. }
  13373. ret = wc_RsaDirect(out, outSz, plain, &plainSz, NULL, RSA_PUBLIC_DECRYPT,
  13374. &rng);
  13375. if (ret != BAD_FUNC_ARG) {
  13376. ERROR_OUT(-7816, exit_rsa_nopadding);
  13377. }
  13378. ret = wc_RsaDirect(out, outSz, NULL, &plainSz, key, RSA_PUBLIC_DECRYPT,
  13379. &rng);
  13380. if (ret != LENGTH_ONLY_E || plainSz != inLen) {
  13381. ERROR_OUT(-7817, exit_rsa_nopadding);
  13382. }
  13383. ret = wc_RsaDirect(out, outSz - 10, plain, &plainSz, key,
  13384. RSA_PUBLIC_DECRYPT, &rng);
  13385. if (ret != BAD_FUNC_ARG) {
  13386. ERROR_OUT(-7818, exit_rsa_nopadding);
  13387. }
  13388. /* if making it to this point of code without hitting an ERROR_OUT then
  13389. * all tests have passed */
  13390. ret = 0;
  13391. exit_rsa_nopadding:
  13392. wc_FreeRsaKey(key);
  13393. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13394. WC_FREE_VAR(key, HEAP_HINT);
  13395. WC_FREE_VAR(out, HEAP_HINT);
  13396. WC_FREE_VAR(plain, HEAP_HINT);
  13397. wc_FreeRng(&rng);
  13398. return ret;
  13399. }
  13400. #endif /* WC_RSA_NO_PADDING */
  13401. #if defined(WOLFSSL_HAVE_SP_RSA) && defined(USE_FAST_MATH)
  13402. static int rsa_even_mod_test(WC_RNG* rng, RsaKey* key)
  13403. {
  13404. byte* tmp = NULL;
  13405. size_t bytes;
  13406. int ret;
  13407. word32 inLen = 0;
  13408. #ifndef NO_ASN
  13409. word32 idx = 0;
  13410. #endif
  13411. word32 outSz = RSA_TEST_BYTES;
  13412. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13413. word32 plainSz = RSA_TEST_BYTES;
  13414. #endif
  13415. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_3072) && \
  13416. !defined(USE_CERT_BUFFERS_4096) && !defined(NO_FILESYSTEM)
  13417. XFILE file;
  13418. #endif
  13419. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  13420. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13421. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  13422. #endif
  13423. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  13424. if (out == NULL
  13425. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13426. || plain == NULL
  13427. #endif
  13428. ) {
  13429. ERROR_OUT(MEMORY_E, exit_rsa_even_mod);
  13430. }
  13431. #endif
  13432. #if defined(USE_CERT_BUFFERS_2048)
  13433. bytes = (size_t)sizeof_client_key_der_2048;
  13434. if (bytes < (size_t)sizeof_client_cert_der_2048)
  13435. bytes = (size_t)sizeof_client_cert_der_2048;
  13436. #else
  13437. bytes = FOURK_BUF;
  13438. #endif
  13439. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13440. if (tmp == NULL
  13441. #ifdef WOLFSSL_ASYNC_CRYPT
  13442. || out == NULL || plain == NULL
  13443. #endif
  13444. ) {
  13445. ERROR_OUT(-7800, exit_rsa_even_mod);
  13446. }
  13447. #if defined(USE_CERT_BUFFERS_2048)
  13448. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  13449. #elif defined(USE_CERT_BUFFERS_3072)
  13450. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  13451. #elif defined(USE_CERT_BUFFERS_4096)
  13452. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  13453. #elif !defined(NO_FILESYSTEM)
  13454. file = XFOPEN(clientKey, "rb");
  13455. if (!file) {
  13456. err_sys("can't open ./certs/client-key.der, "
  13457. "Please run from wolfSSL home dir", -40);
  13458. ERROR_OUT(-7801, exit_rsa_even_mod);
  13459. }
  13460. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  13461. XFCLOSE(file);
  13462. #else
  13463. /* No key to use. */
  13464. ERROR_OUT(-7802, exit_rsa_even_mod);
  13465. #endif /* USE_CERT_BUFFERS */
  13466. #ifndef NO_ASN
  13467. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  13468. if (ret != 0) {
  13469. ERROR_OUT(-7804, exit_rsa_even_mod);
  13470. }
  13471. #else
  13472. #ifdef USE_CERT_BUFFERS_2048
  13473. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  13474. if (ret != 0) {
  13475. ERROR_OUT(-7804, exit_rsa_even_mod);
  13476. }
  13477. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  13478. if (ret != 0) {
  13479. ERROR_OUT(-7804, exit_rsa_even_mod);
  13480. }
  13481. #ifndef NO_SIG_WRAPPER
  13482. modLen = 2048;
  13483. #endif
  13484. #else
  13485. #error Not supported yet!
  13486. #endif
  13487. #endif
  13488. key->n.dp[0] &= (mp_digit)-2;
  13489. if (ret != 0) {
  13490. ERROR_OUT(-7804, exit_rsa_even_mod);
  13491. }
  13492. /* after loading in key use tmp as the test buffer */
  13493. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  13494. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM))) && \
  13495. !defined(WOLFSSL_XILINX_CRYPT)
  13496. /* The ARM64_ASM code that was FIPS validated did not return these expected
  13497. * failure codes. These tests cases were added after the assembly was
  13498. * in-lined in the module and validated, these tests will be available in
  13499. * the 140-3 module */
  13500. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  13501. inLen = 32;
  13502. outSz = wc_RsaEncryptSize(key);
  13503. XMEMSET(tmp, 7, plainSz);
  13504. ret = wc_RsaSSL_Sign(tmp, inLen, out, outSz, key, rng);
  13505. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  13506. ERROR_OUT(-7806, exit_rsa_even_mod);
  13507. }
  13508. ret = wc_RsaSSL_Verify(out, outSz, tmp, inLen, key);
  13509. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  13510. ERROR_OUT(-7808, exit_rsa_even_mod);
  13511. }
  13512. #endif
  13513. #ifdef WC_RSA_BLINDING
  13514. ret = wc_RsaSetRNG(key, rng);
  13515. if (ret < 0) {
  13516. ERROR_OUT(-7811, exit_rsa_even_mod);
  13517. }
  13518. #endif
  13519. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  13520. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  13521. ret = wc_RsaPublicEncrypt(tmp, inLen, out, (int)outSz, key, rng);
  13522. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  13523. ERROR_OUT(-7812, exit_rsa_even_mod);
  13524. }
  13525. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  13526. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13527. ret = wc_RsaPrivateDecrypt(out, outSz, plain, (int)plainSz, key);
  13528. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  13529. ERROR_OUT(-7813, exit_rsa_even_mod);
  13530. }
  13531. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13532. #endif /* !(HAVE_FIPS_VERSION == 2 && WOLFSSL_SP_ARMxx_ASM) */
  13533. /* if making it to this point of code without hitting an ERROR_OUT then
  13534. * all tests have passed */
  13535. ret = 0;
  13536. exit_rsa_even_mod:
  13537. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13538. WC_FREE_VAR(out, HEAP_HINT);
  13539. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13540. WC_FREE_VAR(plain, HEAP_HINT);
  13541. #endif
  13542. (void)out;
  13543. (void)outSz;
  13544. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13545. (void)plain;
  13546. (void)plainSz;
  13547. #endif
  13548. (void)inLen;
  13549. (void)rng;
  13550. return ret;
  13551. }
  13552. #endif /* WOLFSSL_HAVE_SP_RSA */
  13553. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  13554. static int rsa_certgen_test(RsaKey* key, RsaKey* keypub, WC_RNG* rng, byte* tmp)
  13555. {
  13556. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13557. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13558. #ifdef WOLFSSL_TEST_CERT
  13559. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13560. #endif
  13561. #else
  13562. RsaKey caKey[1];
  13563. #ifdef WOLFSSL_TEST_CERT
  13564. DecodedCert decode[1];
  13565. #endif
  13566. #endif
  13567. byte* der = NULL;
  13568. int ret;
  13569. Cert* myCert = NULL;
  13570. int certSz;
  13571. size_t bytes3;
  13572. word32 idx3 = 0;
  13573. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  13574. XFILE file3;
  13575. #endif
  13576. #if defined(WOLFSSL_ALT_NAMES) && !defined(NO_ASN_TIME)
  13577. struct tm beforeTime;
  13578. struct tm afterTime;
  13579. #endif
  13580. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  13581. (void)keypub;
  13582. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13583. if (caKey == NULL)
  13584. ERROR_OUT(MEMORY_E, exit_rsa);
  13585. #ifdef WOLFSSL_TEST_CERT
  13586. if (decode == NULL)
  13587. ERROR_OUT(MEMORY_E, exit_rsa);
  13588. #endif
  13589. #endif
  13590. XMEMSET(caKey, 0, sizeof *caKey);
  13591. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13592. if (der == NULL) {
  13593. ERROR_OUT(-7820, exit_rsa);
  13594. }
  13595. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13596. if (myCert == NULL) {
  13597. ERROR_OUT(-7821, exit_rsa);
  13598. }
  13599. /* self signed */
  13600. if (wc_InitCert_ex(myCert, HEAP_HINT, devId)) {
  13601. ERROR_OUT(-7822, exit_rsa);
  13602. }
  13603. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  13604. XMEMCPY(myCert->serial, mySerial, sizeof(mySerial));
  13605. myCert->serialSz = (int)sizeof(mySerial);
  13606. myCert->isCA = 1;
  13607. #ifndef NO_SHA256
  13608. myCert->sigType = CTC_SHA256wRSA;
  13609. #else
  13610. myCert->sigType = CTC_SHAwRSA;
  13611. #endif
  13612. #ifdef WOLFSSL_CERT_EXT
  13613. /* add Policies */
  13614. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  13615. CTC_MAX_CERTPOL_SZ);
  13616. XSTRNCPY(myCert->certPolicies[1], "1.2.840.113549.1.9.16.6.5",
  13617. CTC_MAX_CERTPOL_SZ);
  13618. myCert->certPoliciesNb = 2;
  13619. /* add SKID from the Public Key */
  13620. if (wc_SetSubjectKeyIdFromPublicKey(myCert, keypub, NULL) != 0) {
  13621. ERROR_OUT(-7823, exit_rsa);
  13622. }
  13623. /* add AKID from the Public Key */
  13624. if (wc_SetAuthKeyIdFromPublicKey(myCert, keypub, NULL) != 0) {
  13625. ERROR_OUT(-7824, exit_rsa);
  13626. }
  13627. /* add Key Usage */
  13628. if (wc_SetKeyUsage(myCert,"cRLSign,keyCertSign") != 0) {
  13629. ERROR_OUT(-7825, exit_rsa);
  13630. }
  13631. #ifdef WOLFSSL_EKU_OID
  13632. {
  13633. const char unique[] = "2.16.840.1.111111.100.1.10.1";
  13634. if (wc_SetExtKeyUsageOID(myCert, unique, sizeof(unique), 0,
  13635. HEAP_HINT) != 0) {
  13636. ERROR_OUT(-7826, exit_rsa);
  13637. }
  13638. }
  13639. #endif /* WOLFSSL_EKU_OID */
  13640. #endif /* WOLFSSL_CERT_EXT */
  13641. ret = 0;
  13642. do {
  13643. #if defined(WOLFSSL_ASYNC_CRYPT)
  13644. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13645. #endif
  13646. if (ret >= 0) {
  13647. ret = wc_MakeSelfCert(myCert, der, FOURK_BUF, key, rng);
  13648. }
  13649. } while (ret == WC_PENDING_E);
  13650. if (ret < 0) {
  13651. ERROR_OUT(-7827, exit_rsa);
  13652. }
  13653. certSz = ret;
  13654. #ifdef WOLFSSL_TEST_CERT
  13655. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  13656. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  13657. if (ret != 0) {
  13658. FreeDecodedCert(decode);
  13659. ERROR_OUT(-7828, exit_rsa);
  13660. }
  13661. FreeDecodedCert(decode);
  13662. #endif
  13663. ret = SaveDerAndPem(der, certSz, certDerFile, certPemFile,
  13664. CERT_TYPE, -5578);
  13665. if (ret != 0) {
  13666. goto exit_rsa;
  13667. }
  13668. /* Setup Certificate */
  13669. if (wc_InitCert_ex(myCert, HEAP_HINT, devId)) {
  13670. ERROR_OUT(-7829, exit_rsa);
  13671. }
  13672. #ifdef WOLFSSL_ALT_NAMES
  13673. /* Get CA Cert for testing */
  13674. #ifdef USE_CERT_BUFFERS_1024
  13675. XMEMCPY(tmp, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  13676. bytes3 = sizeof_ca_cert_der_1024;
  13677. #elif defined(USE_CERT_BUFFERS_2048)
  13678. XMEMCPY(tmp, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  13679. bytes3 = sizeof_ca_cert_der_2048;
  13680. #else
  13681. file3 = XFOPEN(rsaCaCertDerFile, "rb");
  13682. if (!file3) {
  13683. ERROR_OUT(-7830, exit_rsa);
  13684. }
  13685. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  13686. XFCLOSE(file3);
  13687. #endif /* USE_CERT_BUFFERS */
  13688. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_1024) && \
  13689. !defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  13690. ret = wc_SetAltNames(myCert, rsaCaCertFile);
  13691. if (ret != 0) {
  13692. ERROR_OUT(-7831, exit_rsa);
  13693. }
  13694. #endif
  13695. /* get alt names from der */
  13696. ret = wc_SetAltNamesBuffer(myCert, tmp, (int)bytes3);
  13697. if (ret != 0) {
  13698. ERROR_OUT(-7832, exit_rsa);
  13699. }
  13700. /* get dates from der */
  13701. ret = wc_SetDatesBuffer(myCert, tmp, (int)bytes3);
  13702. if (ret != 0) {
  13703. ERROR_OUT(-7833, exit_rsa);
  13704. }
  13705. #ifndef NO_ASN_TIME
  13706. ret = wc_GetCertDates(myCert, &beforeTime, &afterTime);
  13707. if (ret < 0) {
  13708. ERROR_OUT(-7834, exit_rsa);
  13709. }
  13710. #endif
  13711. #endif /* WOLFSSL_ALT_NAMES */
  13712. /* Get CA Key */
  13713. #ifdef USE_CERT_BUFFERS_1024
  13714. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  13715. bytes3 = sizeof_ca_key_der_1024;
  13716. #elif defined(USE_CERT_BUFFERS_2048)
  13717. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  13718. bytes3 = sizeof_ca_key_der_2048;
  13719. #else
  13720. file3 = XFOPEN(rsaCaKeyFile, "rb");
  13721. if (!file3) {
  13722. ERROR_OUT(-7835, exit_rsa);
  13723. }
  13724. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  13725. XFCLOSE(file3);
  13726. #endif /* USE_CERT_BUFFERS */
  13727. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  13728. if (ret != 0) {
  13729. ERROR_OUT(-7836, exit_rsa);
  13730. }
  13731. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  13732. if (ret != 0) {
  13733. ERROR_OUT(-7837, exit_rsa);
  13734. }
  13735. #ifndef NO_SHA256
  13736. myCert->sigType = CTC_SHA256wRSA;
  13737. #else
  13738. myCert->sigType = CTC_SHAwRSA;
  13739. #endif
  13740. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  13741. #ifdef WOLFSSL_CERT_EXT
  13742. /* add Policies */
  13743. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  13744. CTC_MAX_CERTPOL_SZ);
  13745. myCert->certPoliciesNb =1;
  13746. /* add SKID from the Public Key */
  13747. if (wc_SetSubjectKeyIdFromPublicKey(myCert, key, NULL) != 0) {
  13748. ERROR_OUT(-7838, exit_rsa);
  13749. }
  13750. /* add AKID from the CA certificate */
  13751. #if defined(USE_CERT_BUFFERS_2048)
  13752. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  13753. sizeof_ca_cert_der_2048);
  13754. #elif defined(USE_CERT_BUFFERS_1024)
  13755. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  13756. sizeof_ca_cert_der_1024);
  13757. #else
  13758. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  13759. #endif
  13760. if (ret != 0) {
  13761. ERROR_OUT(-7839, exit_rsa);
  13762. }
  13763. /* add Key Usage */
  13764. if (wc_SetKeyUsage(myCert,"keyEncipherment,keyAgreement") != 0) {
  13765. ERROR_OUT(-7840, exit_rsa);
  13766. }
  13767. #endif /* WOLFSSL_CERT_EXT */
  13768. #if defined(USE_CERT_BUFFERS_2048)
  13769. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  13770. sizeof_ca_cert_der_2048);
  13771. #elif defined(USE_CERT_BUFFERS_1024)
  13772. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  13773. sizeof_ca_cert_der_1024);
  13774. #else
  13775. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  13776. #endif
  13777. if (ret < 0) {
  13778. ERROR_OUT(-7841, exit_rsa);
  13779. }
  13780. certSz = wc_MakeCert(myCert, der, FOURK_BUF, key, NULL, rng);
  13781. if (certSz < 0) {
  13782. ERROR_OUT(-7842, exit_rsa);
  13783. }
  13784. ret = 0;
  13785. do {
  13786. #if defined(WOLFSSL_ASYNC_CRYPT)
  13787. ret = wc_AsyncWait(ret, &caKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13788. #endif
  13789. if (ret >= 0) {
  13790. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der, FOURK_BUF,
  13791. caKey, NULL, rng);
  13792. }
  13793. } while (ret == WC_PENDING_E);
  13794. if (ret < 0) {
  13795. ERROR_OUT(-7843, exit_rsa);
  13796. }
  13797. certSz = ret;
  13798. #ifdef WOLFSSL_TEST_CERT
  13799. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  13800. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  13801. if (ret != 0) {
  13802. FreeDecodedCert(decode);
  13803. ERROR_OUT(-7844, exit_rsa);
  13804. }
  13805. FreeDecodedCert(decode);
  13806. #endif
  13807. ret = SaveDerAndPem(der, certSz, otherCertDerFile, otherCertPemFile,
  13808. CERT_TYPE, -5598);
  13809. if (ret != 0) {
  13810. goto exit_rsa;
  13811. }
  13812. exit_rsa:
  13813. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13814. if (caKey != NULL) {
  13815. wc_FreeRsaKey(caKey);
  13816. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13817. }
  13818. #ifdef WOLFSSL_TEST_CERT
  13819. if (decode != NULL)
  13820. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13821. #endif
  13822. #else
  13823. wc_FreeRsaKey(caKey);
  13824. #endif
  13825. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13826. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13827. return ret;
  13828. }
  13829. #endif
  13830. #if !defined(NO_RSA) && defined(HAVE_ECC) && defined(WOLFSSL_CERT_GEN)
  13831. /* Make Cert / Sign example for ECC cert and RSA CA */
  13832. static int rsa_ecc_certgen_test(WC_RNG* rng, byte* tmp)
  13833. {
  13834. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13835. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13836. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13837. ecc_key *caEccKeyPub = (ecc_key *)XMALLOC(sizeof *caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13838. #ifdef WOLFSSL_TEST_CERT
  13839. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13840. #endif
  13841. #else
  13842. RsaKey caKey[1];
  13843. ecc_key caEccKey[1];
  13844. ecc_key caEccKeyPub[1];
  13845. #ifdef WOLFSSL_TEST_CERT
  13846. DecodedCert decode[1];
  13847. #endif
  13848. #endif
  13849. byte* der = NULL;
  13850. Cert* myCert = NULL;
  13851. int certSz;
  13852. size_t bytes3;
  13853. word32 idx3 = 0;
  13854. #if (!defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)) \
  13855. || !defined(USE_CERT_BUFFERS_256)
  13856. XFILE file3;
  13857. #endif
  13858. int ret;
  13859. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13860. if ((caKey == NULL) || (caEccKey == NULL) || (caEccKeyPub == NULL)
  13861. #ifdef WOLFSSL_TEST_CERT
  13862. || (decode == NULL)
  13863. #endif
  13864. )
  13865. ERROR_OUT(MEMORY_E, exit_rsa);
  13866. #endif
  13867. XMEMSET(caKey, 0, sizeof *caKey);
  13868. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  13869. XMEMSET(caEccKeyPub, 0, sizeof *caEccKeyPub);
  13870. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13871. if (der == NULL) {
  13872. ERROR_OUT(-7850, exit_rsa);
  13873. }
  13874. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13875. if (myCert == NULL) {
  13876. ERROR_OUT(-7851, exit_rsa);
  13877. }
  13878. /* Get CA Key */
  13879. #ifdef USE_CERT_BUFFERS_1024
  13880. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  13881. bytes3 = sizeof_ca_key_der_1024;
  13882. #elif defined(USE_CERT_BUFFERS_2048)
  13883. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  13884. bytes3 = sizeof_ca_key_der_2048;
  13885. #else
  13886. file3 = XFOPEN(rsaCaKeyFile, "rb");
  13887. if (!file3) {
  13888. ERROR_OUT(-7852, exit_rsa);
  13889. }
  13890. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  13891. XFCLOSE(file3);
  13892. #endif /* USE_CERT_BUFFERS */
  13893. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  13894. if (ret != 0) {
  13895. ERROR_OUT(-7853, exit_rsa);
  13896. }
  13897. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  13898. if (ret != 0) {
  13899. ERROR_OUT(-7854, exit_rsa);
  13900. }
  13901. /* Get Cert Key */
  13902. #ifdef USE_CERT_BUFFERS_256
  13903. XMEMCPY(tmp, ecc_key_pub_der_256, sizeof_ecc_key_pub_der_256);
  13904. bytes3 = sizeof_ecc_key_pub_der_256;
  13905. #else
  13906. file3 = XFOPEN(eccKeyPubFileDer, "rb");
  13907. if (!file3) {
  13908. ERROR_OUT(-7855, exit_rsa);
  13909. }
  13910. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  13911. XFCLOSE(file3);
  13912. #endif
  13913. ret = wc_ecc_init_ex(caEccKeyPub, HEAP_HINT, devId);
  13914. if (ret != 0) {
  13915. ERROR_OUT(-7856, exit_rsa);
  13916. }
  13917. idx3 = 0;
  13918. ret = wc_EccPublicKeyDecode(tmp, &idx3, caEccKeyPub, (word32)bytes3);
  13919. if (ret != 0) {
  13920. ERROR_OUT(-7857, exit_rsa);
  13921. }
  13922. /* Setup Certificate */
  13923. if (wc_InitCert_ex(myCert, HEAP_HINT, devId)) {
  13924. ERROR_OUT(-7858, exit_rsa);
  13925. }
  13926. #ifndef NO_SHA256
  13927. myCert->sigType = CTC_SHA256wRSA;
  13928. #else
  13929. myCert->sigType = CTC_SHAwRSA;
  13930. #endif
  13931. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  13932. #ifdef WOLFSSL_CERT_EXT
  13933. /* add Policies */
  13934. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  13935. CTC_MAX_CERTPOL_SZ);
  13936. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  13937. CTC_MAX_CERTPOL_SZ);
  13938. myCert->certPoliciesNb = 2;
  13939. /* add SKID from the Public Key */
  13940. if (wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, caEccKeyPub) != 0) {
  13941. ERROR_OUT(-7859, exit_rsa);
  13942. }
  13943. /* add AKID from the CA certificate */
  13944. #if defined(USE_CERT_BUFFERS_2048)
  13945. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  13946. sizeof_ca_cert_der_2048);
  13947. #elif defined(USE_CERT_BUFFERS_1024)
  13948. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  13949. sizeof_ca_cert_der_1024);
  13950. #else
  13951. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  13952. #endif
  13953. if (ret != 0) {
  13954. ERROR_OUT(-7860, exit_rsa);
  13955. }
  13956. /* add Key Usage */
  13957. if (wc_SetKeyUsage(myCert, certKeyUsage) != 0) {
  13958. ERROR_OUT(-7861, exit_rsa);
  13959. }
  13960. #endif /* WOLFSSL_CERT_EXT */
  13961. #if defined(USE_CERT_BUFFERS_2048)
  13962. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  13963. sizeof_ca_cert_der_2048);
  13964. #elif defined(USE_CERT_BUFFERS_1024)
  13965. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  13966. sizeof_ca_cert_der_1024);
  13967. #else
  13968. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  13969. #endif
  13970. if (ret < 0) {
  13971. ERROR_OUT(-7862, exit_rsa);
  13972. }
  13973. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, caEccKeyPub, rng);
  13974. if (certSz < 0) {
  13975. ERROR_OUT(-7863, exit_rsa);
  13976. }
  13977. ret = 0;
  13978. do {
  13979. #if defined(WOLFSSL_ASYNC_CRYPT)
  13980. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13981. #endif
  13982. if (ret >= 0) {
  13983. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  13984. FOURK_BUF, caKey, NULL, rng);
  13985. }
  13986. } while (ret == WC_PENDING_E);
  13987. if (ret < 0) {
  13988. ERROR_OUT(-7864, exit_rsa);
  13989. }
  13990. certSz = ret;
  13991. #ifdef WOLFSSL_TEST_CERT
  13992. InitDecodedCert(decode, der, certSz, 0);
  13993. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  13994. if (ret != 0) {
  13995. FreeDecodedCert(decode);
  13996. ERROR_OUT(-7865, exit_rsa);
  13997. }
  13998. FreeDecodedCert(decode);
  13999. #endif
  14000. ret = SaveDerAndPem(der, certSz, certEccRsaDerFile, certEccRsaPemFile,
  14001. CERT_TYPE, -5616);
  14002. if (ret != 0) {
  14003. goto exit_rsa;
  14004. }
  14005. exit_rsa:
  14006. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14007. if (caKey != NULL) {
  14008. wc_FreeRsaKey(caKey);
  14009. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14010. }
  14011. if (caEccKey != NULL) {
  14012. wc_ecc_free(caEccKey);
  14013. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14014. }
  14015. if (caEccKeyPub != NULL) {
  14016. wc_ecc_free(caEccKeyPub);
  14017. XFREE(caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14018. }
  14019. #ifdef WOLFSSL_TEST_CERT
  14020. if (decode != NULL)
  14021. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14022. #endif
  14023. #else
  14024. wc_FreeRsaKey(caKey);
  14025. wc_ecc_free(caEccKey);
  14026. wc_ecc_free(caEccKeyPub);
  14027. #endif
  14028. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14029. myCert = NULL;
  14030. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14031. der = NULL;
  14032. if (ret >= 0)
  14033. ret = 0;
  14034. return ret;
  14035. }
  14036. #endif /* !NO_RSA && HAVE_ECC && WOLFSSL_CERT_GEN */
  14037. #ifdef WOLFSSL_KEY_GEN
  14038. static int rsa_keygen_test(WC_RNG* rng)
  14039. {
  14040. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14041. RsaKey *genKey = (RsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14042. #else
  14043. RsaKey genKey[1];
  14044. #endif
  14045. int ret;
  14046. byte* der = NULL;
  14047. #ifndef WOLFSSL_CRYPTOCELL
  14048. word32 idx = 0;
  14049. #endif
  14050. int derSz = 0;
  14051. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FIPS)
  14052. int keySz = 1024;
  14053. #else
  14054. int keySz = 2048;
  14055. #endif
  14056. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14057. if (! genKey)
  14058. ERROR_OUT(MEMORY_E, exit_rsa);
  14059. #endif
  14060. XMEMSET(genKey, 0, sizeof *genKey);
  14061. ret = wc_InitRsaKey_ex(genKey, HEAP_HINT, devId);
  14062. if (ret != 0) {
  14063. ERROR_OUT(-7870, exit_rsa);
  14064. }
  14065. #ifdef HAVE_FIPS
  14066. for (;;) {
  14067. #endif
  14068. ret = wc_MakeRsaKey(genKey, keySz, WC_RSA_EXPONENT, rng);
  14069. #if defined(WOLFSSL_ASYNC_CRYPT)
  14070. ret = wc_AsyncWait(ret, &genKey->asyncDev, WC_ASYNC_FLAG_NONE);
  14071. #endif
  14072. #ifdef HAVE_FIPS
  14073. if (ret == PRIME_GEN_E)
  14074. continue;
  14075. break;
  14076. }
  14077. #endif
  14078. if (ret != 0) {
  14079. ERROR_OUT(-7871, exit_rsa);
  14080. }
  14081. TEST_SLEEP();
  14082. #ifdef WOLFSSL_RSA_KEY_CHECK
  14083. ret = wc_CheckRsaKey(genKey);
  14084. if (ret != 0) {
  14085. ERROR_OUT(-7872, exit_rsa);
  14086. }
  14087. #endif
  14088. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14089. if (der == NULL) {
  14090. ERROR_OUT(-7873, exit_rsa);
  14091. }
  14092. derSz = wc_RsaKeyToDer(genKey, der, FOURK_BUF);
  14093. if (derSz < 0) {
  14094. ERROR_OUT(-7874, exit_rsa);
  14095. }
  14096. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  14097. PRIVATEKEY_TYPE, -5555);
  14098. if (ret != 0) {
  14099. goto exit_rsa;
  14100. }
  14101. wc_FreeRsaKey(genKey);
  14102. ret = wc_InitRsaKey(genKey, HEAP_HINT);
  14103. if (ret != 0) {
  14104. ERROR_OUT(-7875, exit_rsa);
  14105. }
  14106. #ifndef WOLFSSL_CRYPTOCELL
  14107. idx = 0;
  14108. /* The private key part of the key gen pairs from cryptocell can't be exported */
  14109. ret = wc_RsaPrivateKeyDecode(der, &idx, genKey, derSz);
  14110. if (ret != 0) {
  14111. ERROR_OUT(-7876, exit_rsa);
  14112. }
  14113. #endif /* WOLFSSL_CRYPTOCELL */
  14114. exit_rsa:
  14115. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14116. if (genKey) {
  14117. wc_FreeRsaKey(genKey);
  14118. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14119. }
  14120. #else
  14121. wc_FreeRsaKey(genKey);
  14122. #endif
  14123. if (der != NULL) {
  14124. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14125. der = NULL;
  14126. }
  14127. return ret;
  14128. }
  14129. #endif
  14130. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  14131. #if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG) && \
  14132. !defined(HAVE_FAST_RSA) && !defined(HAVE_USER_RSA) && \
  14133. (!defined(HAVE_FIPS) || \
  14134. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) \
  14135. && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14136. static int rsa_oaep_padding_test(RsaKey* key, WC_RNG* rng)
  14137. {
  14138. int ret = 0;
  14139. word32 idx = 0;
  14140. const char inStr[] = TEST_STRING;
  14141. const word32 inLen = (word32)TEST_STRING_SZ;
  14142. const word32 outSz = RSA_TEST_BYTES;
  14143. const word32 plainSz = RSA_TEST_BYTES;
  14144. byte* res = NULL;
  14145. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  14146. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  14147. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  14148. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  14149. if (in == NULL || out == NULL || plain == NULL)
  14150. ERROR_OUT(MEMORY_E, exit_rsa);
  14151. #endif
  14152. XMEMCPY(in, inStr, inLen);
  14153. #ifndef NO_SHA
  14154. do {
  14155. #if defined(WOLFSSL_ASYNC_CRYPT)
  14156. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14157. #endif
  14158. if (ret >= 0) {
  14159. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14160. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  14161. }
  14162. } while (ret == WC_PENDING_E);
  14163. if (ret < 0) {
  14164. ERROR_OUT(-7918, exit_rsa);
  14165. }
  14166. TEST_SLEEP();
  14167. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14168. idx = (word32)ret;
  14169. do {
  14170. #if defined(WOLFSSL_ASYNC_CRYPT)
  14171. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14172. #endif
  14173. if (ret >= 0) {
  14174. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14175. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  14176. }
  14177. } while (ret == WC_PENDING_E);
  14178. if (ret < 0) {
  14179. ERROR_OUT(-7919, exit_rsa);
  14180. }
  14181. if (XMEMCMP(plain, in, inLen)) {
  14182. ERROR_OUT(-7920, exit_rsa);
  14183. }
  14184. TEST_SLEEP();
  14185. #endif /* NO_SHA */
  14186. #endif
  14187. #ifndef NO_SHA256
  14188. XMEMSET(plain, 0, plainSz);
  14189. do {
  14190. #if defined(WOLFSSL_ASYNC_CRYPT)
  14191. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14192. #endif
  14193. if (ret >= 0) {
  14194. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14195. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  14196. }
  14197. } while (ret == WC_PENDING_E);
  14198. if (ret < 0) {
  14199. ERROR_OUT(-7921, exit_rsa);
  14200. }
  14201. TEST_SLEEP();
  14202. idx = (word32)ret;
  14203. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  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_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14210. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  14211. }
  14212. } while (ret == WC_PENDING_E);
  14213. if (ret < 0) {
  14214. ERROR_OUT(-7922, exit_rsa);
  14215. }
  14216. if (XMEMCMP(plain, in, inLen)) {
  14217. ERROR_OUT(-7923, exit_rsa);
  14218. }
  14219. TEST_SLEEP();
  14220. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14221. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14222. do {
  14223. #if defined(WOLFSSL_ASYNC_CRYPT)
  14224. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14225. #endif
  14226. if (ret >= 0) {
  14227. ret = wc_RsaPrivateDecryptInline_ex(out, idx, &res, key,
  14228. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  14229. }
  14230. } while (ret == WC_PENDING_E);
  14231. if (ret < 0) {
  14232. ERROR_OUT(-7924, exit_rsa);
  14233. }
  14234. if (ret != (int)inLen) {
  14235. ERROR_OUT(-7925, exit_rsa);
  14236. }
  14237. if (XMEMCMP(res, in, inLen)) {
  14238. ERROR_OUT(-7926, exit_rsa);
  14239. }
  14240. TEST_SLEEP();
  14241. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14242. /* check fails if not using the same optional label */
  14243. XMEMSET(plain, 0, plainSz);
  14244. do {
  14245. #if defined(WOLFSSL_ASYNC_CRYPT)
  14246. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14247. #endif
  14248. if (ret >= 0) {
  14249. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14250. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  14251. }
  14252. } while (ret == WC_PENDING_E);
  14253. if (ret < 0) {
  14254. ERROR_OUT(-7927, exit_rsa);
  14255. }
  14256. TEST_SLEEP();
  14257. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  14258. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  14259. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SE050)
  14260. /* label is unused in cryptocell and SE050 so it won't detect decrypt error
  14261. * due to label */
  14262. idx = (word32)ret;
  14263. do {
  14264. #if defined(WOLFSSL_ASYNC_CRYPT)
  14265. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14266. #endif
  14267. if (ret >= 0) {
  14268. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14269. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  14270. }
  14271. } while (ret == WC_PENDING_E);
  14272. if (ret > 0) { /* in this case decrypt should fail */
  14273. ERROR_OUT(-7928, exit_rsa);
  14274. }
  14275. ret = 0;
  14276. TEST_SLEEP();
  14277. #endif /* !HAVE_CAVIUM */
  14278. /* check using optional label with encrypt/decrypt */
  14279. XMEMSET(plain, 0, plainSz);
  14280. do {
  14281. #if defined(WOLFSSL_ASYNC_CRYPT)
  14282. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14283. #endif
  14284. if (ret >= 0) {
  14285. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14286. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  14287. }
  14288. } while (ret == WC_PENDING_E);
  14289. if (ret < 0) {
  14290. ERROR_OUT(-7929, exit_rsa);
  14291. }
  14292. TEST_SLEEP();
  14293. idx = (word32)ret;
  14294. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14295. do {
  14296. #if defined(WOLFSSL_ASYNC_CRYPT)
  14297. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14298. #endif
  14299. if (ret >= 0) {
  14300. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14301. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  14302. }
  14303. } while (ret == WC_PENDING_E);
  14304. if (ret < 0) {
  14305. ERROR_OUT(-7930, exit_rsa);
  14306. }
  14307. if (XMEMCMP(plain, in, inLen)) {
  14308. ERROR_OUT(-7931, exit_rsa);
  14309. }
  14310. TEST_SLEEP();
  14311. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14312. #ifndef NO_SHA
  14313. /* check fail using mismatch hash algorithms */
  14314. XMEMSET(plain, 0, plainSz);
  14315. do {
  14316. #if defined(WOLFSSL_ASYNC_CRYPT)
  14317. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14318. #endif
  14319. if (ret >= 0) {
  14320. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14321. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, in, inLen);
  14322. }
  14323. } while (ret == WC_PENDING_E);
  14324. if (ret < 0) {
  14325. ERROR_OUT(-7932, exit_rsa);
  14326. }
  14327. TEST_SLEEP();
  14328. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  14329. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  14330. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SE050)
  14331. idx = (word32)ret;
  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_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256,
  14339. in, inLen);
  14340. }
  14341. } while (ret == WC_PENDING_E);
  14342. if (ret > 0) { /* should fail */
  14343. ERROR_OUT(-7933, exit_rsa);
  14344. }
  14345. ret = 0;
  14346. TEST_SLEEP();
  14347. #endif /* !HAVE_CAVIUM */
  14348. #endif /* NO_SHA */
  14349. #endif /* NO_SHA256 */
  14350. #ifdef WOLFSSL_SHA512
  14351. /* Check valid RSA key size is used while using hash length of SHA512
  14352. If key size is less than (hash length * 2) + 2 then is invalid use
  14353. and test, since OAEP padding requires this.
  14354. BAD_FUNC_ARG is returned when this case is not met */
  14355. if (wc_RsaEncryptSize(key) > ((int)WC_SHA512_DIGEST_SIZE * 2) + 2) {
  14356. XMEMSET(plain, 0, plainSz);
  14357. do {
  14358. #if defined(WOLFSSL_ASYNC_CRYPT)
  14359. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14360. #endif
  14361. if (ret >= 0) {
  14362. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14363. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  14364. }
  14365. } while (ret == WC_PENDING_E);
  14366. if (ret < 0) {
  14367. ERROR_OUT(-7934, exit_rsa);
  14368. }
  14369. TEST_SLEEP();
  14370. idx = ret;
  14371. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14372. do {
  14373. #if defined(WOLFSSL_ASYNC_CRYPT)
  14374. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14375. #endif
  14376. if (ret >= 0) {
  14377. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14378. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  14379. }
  14380. } while (ret == WC_PENDING_E);
  14381. if (ret < 0) {
  14382. ERROR_OUT(-7935, exit_rsa);
  14383. }
  14384. if (XMEMCMP(plain, in, inLen)) {
  14385. ERROR_OUT(-7936, exit_rsa);
  14386. }
  14387. TEST_SLEEP();
  14388. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14389. }
  14390. #endif /* WOLFSSL_SHA512 */
  14391. /* check using pkcsv15 padding with _ex API */
  14392. XMEMSET(plain, 0, plainSz);
  14393. do {
  14394. #if defined(WOLFSSL_ASYNC_CRYPT)
  14395. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14396. #endif
  14397. if (ret >= 0) {
  14398. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14399. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  14400. }
  14401. } while (ret == WC_PENDING_E);
  14402. if (ret < 0) {
  14403. ERROR_OUT(-7937, exit_rsa);
  14404. }
  14405. TEST_SLEEP();
  14406. idx = (word32)ret;
  14407. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14408. do {
  14409. #if defined(WOLFSSL_ASYNC_CRYPT)
  14410. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14411. #endif
  14412. if (ret >= 0) {
  14413. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14414. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  14415. }
  14416. } while (ret == WC_PENDING_E);
  14417. if (ret < 0) {
  14418. ERROR_OUT(-7938, exit_rsa);
  14419. }
  14420. if (XMEMCMP(plain, in, inLen)) {
  14421. ERROR_OUT(-7939, exit_rsa);
  14422. }
  14423. TEST_SLEEP();
  14424. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14425. exit_rsa:
  14426. WC_FREE_VAR(in, HEAP_HINT);
  14427. WC_FREE_VAR(out, HEAP_HINT);
  14428. WC_FREE_VAR(plain, HEAP_HINT);
  14429. (void)idx;
  14430. (void)inStr;
  14431. (void)res;
  14432. if (ret >= 0)
  14433. ret = 0;
  14434. return ret;
  14435. }
  14436. #endif
  14437. #endif
  14438. WOLFSSL_TEST_SUBROUTINE int rsa_test(void)
  14439. {
  14440. int ret;
  14441. size_t bytes;
  14442. WC_RNG rng;
  14443. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14444. byte* tmp = NULL;
  14445. byte* der = NULL;
  14446. RsaKey *key = (RsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14447. #else
  14448. RsaKey key[1];
  14449. byte tmp[FOURK_BUF];
  14450. #endif
  14451. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  14452. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14453. RsaKey *keypub = (RsaKey *)XMALLOC(sizeof *keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14454. #else
  14455. RsaKey keypub[1];
  14456. #endif
  14457. #endif
  14458. word32 idx = 0;
  14459. const char inStr[] = TEST_STRING;
  14460. const word32 inLen = (word32)TEST_STRING_SZ;
  14461. const word32 outSz = RSA_TEST_BYTES;
  14462. const word32 plainSz = RSA_TEST_BYTES;
  14463. byte* res = NULL;
  14464. #ifndef NO_SIG_WRAPPER
  14465. int modLen;
  14466. #endif
  14467. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  14468. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  14469. !defined(NO_FILESYSTEM)
  14470. XFILE file;
  14471. #ifdef WOLFSSL_TEST_CERT
  14472. XFILE file2;
  14473. #endif
  14474. #endif
  14475. #ifdef WOLFSSL_TEST_CERT
  14476. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14477. DecodedCert *cert = (DecodedCert *)XMALLOC(sizeof *cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14478. #else
  14479. DecodedCert cert[1];
  14480. #endif
  14481. #ifndef NO_ASN_TIME
  14482. struct tm timearg;
  14483. const byte* date;
  14484. byte dateFormat;
  14485. int dateLength;
  14486. #endif
  14487. #endif
  14488. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  14489. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  14490. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  14491. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  14492. if (in == NULL || out == NULL || plain == NULL)
  14493. ERROR_OUT(MEMORY_E, exit_rsa);
  14494. #endif
  14495. XMEMCPY(in, inStr, inLen);
  14496. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14497. if (key == NULL)
  14498. ERROR_OUT(MEMORY_E, exit_rsa);
  14499. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  14500. if (keypub == NULL)
  14501. ERROR_OUT(MEMORY_E, exit_rsa);
  14502. #endif
  14503. #ifdef WOLFSSL_TEST_CERT
  14504. if (cert == NULL)
  14505. ERROR_OUT(MEMORY_E, exit_rsa);
  14506. #endif
  14507. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  14508. /* initialize stack structures */
  14509. XMEMSET(&rng, 0, sizeof(rng));
  14510. XMEMSET(key, 0, sizeof *key);
  14511. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  14512. XMEMSET(keypub, 0, sizeof *keypub);
  14513. #endif
  14514. #if !defined(HAVE_USER_RSA) && !defined(NO_ASN)
  14515. ret = rsa_decode_test(key);
  14516. if (ret != 0)
  14517. ERROR_OUT(ret, exit_rsa);
  14518. #endif
  14519. #ifdef USE_CERT_BUFFERS_1024
  14520. bytes = (size_t)sizeof_client_key_der_1024;
  14521. if (bytes < (size_t)sizeof_client_cert_der_1024)
  14522. bytes = (size_t)sizeof_client_cert_der_1024;
  14523. #elif defined(USE_CERT_BUFFERS_2048)
  14524. bytes = (size_t)sizeof_client_key_der_2048;
  14525. if (bytes < (size_t)sizeof_client_cert_der_2048)
  14526. bytes = (size_t)sizeof_client_cert_der_2048;
  14527. #elif defined(USE_CERT_BUFFERS_3072)
  14528. bytes = (size_t)sizeof_client_key_der_3072;
  14529. if (bytes < (size_t)sizeof_client_cert_der_3072)
  14530. bytes = (size_t)sizeof_client_cert_der_3072;
  14531. #elif defined(USE_CERT_BUFFERS_4096)
  14532. bytes = (size_t)sizeof_client_key_der_4096;
  14533. if (bytes < (size_t)sizeof_client_cert_der_4096)
  14534. bytes = (size_t)sizeof_client_cert_der_4096;
  14535. #else
  14536. bytes = FOURK_BUF;
  14537. #endif
  14538. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14539. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14540. if (tmp == NULL)
  14541. ERROR_OUT(-7900, exit_rsa);
  14542. #endif
  14543. #ifdef USE_CERT_BUFFERS_1024
  14544. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  14545. #elif defined(USE_CERT_BUFFERS_2048)
  14546. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  14547. #elif defined(USE_CERT_BUFFERS_3072)
  14548. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  14549. #elif defined(USE_CERT_BUFFERS_4096)
  14550. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  14551. #elif !defined(NO_FILESYSTEM)
  14552. file = XFOPEN(clientKey, "rb");
  14553. if (!file) {
  14554. err_sys("can't open ./certs/client-key.der, "
  14555. "Please run from wolfSSL home dir", -40);
  14556. ERROR_OUT(-7901, exit_rsa);
  14557. }
  14558. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  14559. XFCLOSE(file);
  14560. #else
  14561. /* No key to use. */
  14562. ERROR_OUT(-7902, exit_rsa);
  14563. #endif /* USE_CERT_BUFFERS */
  14564. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  14565. if (ret != 0) {
  14566. ERROR_OUT(-7903, exit_rsa);
  14567. }
  14568. #ifndef NO_ASN
  14569. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  14570. if (ret != 0) {
  14571. ERROR_OUT(-7904, exit_rsa);
  14572. }
  14573. #ifndef NO_SIG_WRAPPER
  14574. modLen = wc_RsaEncryptSize(key);
  14575. #endif
  14576. #else
  14577. #ifdef USE_CERT_BUFFERS_2048
  14578. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  14579. if (ret != 0) {
  14580. ERROR_OUT(-7905, exit_rsa);
  14581. }
  14582. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  14583. if (ret != 0) {
  14584. ERROR_OUT(-7906, exit_rsa);
  14585. }
  14586. #ifndef NO_SIG_WRAPPER
  14587. modLen = 2048;
  14588. #endif
  14589. #else
  14590. #error Not supported yet!
  14591. #endif
  14592. #endif
  14593. #ifndef WC_NO_RNG
  14594. #ifndef HAVE_FIPS
  14595. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  14596. #else
  14597. ret = wc_InitRng(&rng);
  14598. #endif
  14599. if (ret != 0) {
  14600. ERROR_OUT(-7907, exit_rsa);
  14601. }
  14602. #endif
  14603. #ifndef NO_SIG_WRAPPER
  14604. ret = rsa_sig_test(key, sizeof *key, modLen, &rng);
  14605. if (ret != 0)
  14606. goto exit_rsa;
  14607. #endif
  14608. #ifdef WC_RSA_NONBLOCK
  14609. ret = rsa_nb_test(key, in, inLen, out, outSz, plain, plainSz, &rng);
  14610. if (ret != 0)
  14611. goto exit_rsa;
  14612. #endif
  14613. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  14614. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14615. do {
  14616. #if defined(WOLFSSL_ASYNC_CRYPT)
  14617. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14618. #endif
  14619. if (ret >= 0) {
  14620. ret = wc_RsaPublicEncrypt(in, inLen, out, outSz, key, &rng);
  14621. }
  14622. } while (ret == WC_PENDING_E);
  14623. if (ret < 0) {
  14624. ERROR_OUT(-7908, exit_rsa);
  14625. }
  14626. TEST_SLEEP();
  14627. #ifdef WC_RSA_BLINDING
  14628. {
  14629. int tmpret = ret;
  14630. ret = wc_RsaSetRNG(key, &rng);
  14631. if (ret < 0) {
  14632. ERROR_OUT(-7909, exit_rsa);
  14633. }
  14634. ret = tmpret;
  14635. }
  14636. #endif
  14637. idx = (word32)ret; /* save off encrypted length */
  14638. do {
  14639. #if defined(WOLFSSL_ASYNC_CRYPT)
  14640. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14641. #endif
  14642. if (ret >= 0) {
  14643. ret = wc_RsaPrivateDecrypt(out, idx, plain, plainSz, key);
  14644. }
  14645. } while (ret == WC_PENDING_E);
  14646. if (ret < 0) {
  14647. ERROR_OUT(-7910, exit_rsa);
  14648. }
  14649. if (XMEMCMP(plain, in, inLen)) {
  14650. ERROR_OUT(-7911, exit_rsa);
  14651. }
  14652. TEST_SLEEP();
  14653. do {
  14654. #if defined(WOLFSSL_ASYNC_CRYPT)
  14655. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14656. #endif
  14657. if (ret >= 0) {
  14658. ret = wc_RsaPrivateDecryptInline(out, idx, &res, key);
  14659. }
  14660. } while (ret == WC_PENDING_E);
  14661. if (ret < 0) {
  14662. ERROR_OUT(-7912, exit_rsa);
  14663. }
  14664. if (ret != (int)inLen) {
  14665. ERROR_OUT(-7913, exit_rsa);
  14666. }
  14667. if (XMEMCMP(res, in, inLen)) {
  14668. ERROR_OUT(-7914, exit_rsa);
  14669. }
  14670. TEST_SLEEP();
  14671. do {
  14672. #if defined(WOLFSSL_ASYNC_CRYPT)
  14673. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14674. #endif
  14675. if (ret >= 0) {
  14676. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, &rng);
  14677. }
  14678. } while (ret == WC_PENDING_E);
  14679. if (ret < 0) {
  14680. ERROR_OUT(-7915, exit_rsa);
  14681. }
  14682. TEST_SLEEP();
  14683. #elif defined(WOLFSSL_PUBLIC_MP)
  14684. {
  14685. static byte signature_2048[] = {
  14686. 0x07, 0x6f, 0xc9, 0x85, 0x73, 0x9e, 0x21, 0x79,
  14687. 0x47, 0xf1, 0xa3, 0xd7, 0xf4, 0x27, 0x29, 0xbe,
  14688. 0x99, 0x5d, 0xac, 0xb2, 0x10, 0x3f, 0x95, 0xda,
  14689. 0x89, 0x23, 0xb8, 0x96, 0x13, 0x57, 0x72, 0x30,
  14690. 0xa1, 0xfe, 0x5a, 0x68, 0x9c, 0x99, 0x9d, 0x1e,
  14691. 0x05, 0xa4, 0x80, 0xb0, 0xbb, 0xd9, 0xd9, 0xa1,
  14692. 0x69, 0x97, 0x74, 0xb3, 0x41, 0x21, 0x3b, 0x47,
  14693. 0xf5, 0x51, 0xb1, 0xfb, 0xc7, 0xaa, 0xcc, 0xdc,
  14694. 0xcd, 0x76, 0xa0, 0x28, 0x4d, 0x27, 0x14, 0xa4,
  14695. 0xb9, 0x41, 0x68, 0x7c, 0xb3, 0x66, 0xe6, 0x6f,
  14696. 0x40, 0x76, 0xe4, 0x12, 0xfd, 0xae, 0x29, 0xb5,
  14697. 0x63, 0x60, 0x87, 0xce, 0x49, 0x6b, 0xf3, 0x05,
  14698. 0x9a, 0x14, 0xb5, 0xcc, 0xcd, 0xf7, 0x30, 0x95,
  14699. 0xd2, 0x72, 0x52, 0x1d, 0x5b, 0x7e, 0xef, 0x4a,
  14700. 0x02, 0x96, 0x21, 0x6c, 0x55, 0xa5, 0x15, 0xb1,
  14701. 0x57, 0x63, 0x2c, 0xa3, 0x8e, 0x9d, 0x3d, 0x45,
  14702. 0xcc, 0xb8, 0xe6, 0xa1, 0xc8, 0x59, 0xcd, 0xf5,
  14703. 0xdc, 0x0a, 0x51, 0xb6, 0x9d, 0xfb, 0xf4, 0x6b,
  14704. 0xfd, 0x32, 0x71, 0x6e, 0xcf, 0xcb, 0xb3, 0xd9,
  14705. 0xe0, 0x4a, 0x77, 0x34, 0xd6, 0x61, 0xf5, 0x7c,
  14706. 0xf9, 0xa9, 0xa4, 0xb0, 0x8e, 0x3b, 0xd6, 0x04,
  14707. 0xe0, 0xde, 0x2b, 0x5b, 0x5a, 0xbf, 0xd9, 0xef,
  14708. 0x8d, 0xa3, 0xf5, 0xb1, 0x67, 0xf3, 0xb9, 0x72,
  14709. 0x0a, 0x37, 0x12, 0x35, 0x6c, 0x8e, 0x10, 0x8b,
  14710. 0x38, 0x06, 0x16, 0x4b, 0x20, 0x20, 0x13, 0x00,
  14711. 0x2e, 0x6d, 0xc2, 0x59, 0x23, 0x67, 0x4a, 0x6d,
  14712. 0xa1, 0x46, 0x8b, 0xee, 0xcf, 0x44, 0xb4, 0x3e,
  14713. 0x56, 0x75, 0x00, 0x68, 0xb5, 0x7d, 0x0f, 0x20,
  14714. 0x79, 0x5d, 0x7f, 0x12, 0x15, 0x32, 0x89, 0x61,
  14715. 0x6b, 0x29, 0xb7, 0x52, 0xf5, 0x25, 0xd8, 0x98,
  14716. 0xe8, 0x6f, 0xf9, 0x22, 0xb4, 0xbb, 0xe5, 0xff,
  14717. 0xd0, 0x92, 0x86, 0x9a, 0x88, 0xa2, 0xaf, 0x6b
  14718. };
  14719. ret = sizeof(signature_2048);
  14720. XMEMCPY(out, signature_2048, ret);
  14721. }
  14722. #endif
  14723. #if !defined(WC_NO_RNG) && !defined(WC_NO_RSA_OAEP) && \
  14724. ((!defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || \
  14725. defined(WOLFSSL_PUBLIC_MP)) && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14726. idx = (word32)ret;
  14727. XMEMSET(plain, 0, plainSz);
  14728. do {
  14729. #if defined(WOLFSSL_ASYNC_CRYPT)
  14730. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14731. #endif
  14732. if (ret >= 0) {
  14733. #ifndef WOLFSSL_RSA_VERIFY_INLINE
  14734. #if defined(WOLFSSL_CRYPTOCELL)
  14735. /*
  14736. Cryptocell requires the input data and signature byte array to verify.
  14737. first argument must be the input data
  14738. second argument must be the length of input data
  14739. third argument must be the signature byte array or the output from
  14740. wc_RsaSSL_Sign()
  14741. fourth argument must be the length of the signature byte array
  14742. */
  14743. ret = wc_RsaSSL_Verify(in, inLen, out, outSz, key);
  14744. #else
  14745. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, key);
  14746. #endif /* WOLFSSL_CRYPTOCELL */
  14747. #else
  14748. byte* dec = NULL;
  14749. ret = wc_RsaSSL_VerifyInline(out, idx, &dec, key);
  14750. if (ret > 0) {
  14751. XMEMCPY(plain, dec, ret);
  14752. }
  14753. #endif
  14754. }
  14755. } while (ret == WC_PENDING_E);
  14756. if (ret < 0) {
  14757. ERROR_OUT(-7916, exit_rsa);
  14758. }
  14759. if (XMEMCMP(plain, in, (size_t)ret)) {
  14760. ERROR_OUT(-7917, exit_rsa);
  14761. }
  14762. TEST_SLEEP();
  14763. #endif
  14764. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  14765. #if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG)
  14766. #if !defined(HAVE_FAST_RSA) && !defined(HAVE_USER_RSA) && \
  14767. (!defined(HAVE_FIPS) || \
  14768. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) \
  14769. && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14770. ret = rsa_oaep_padding_test(key, &rng);
  14771. if (ret != 0)
  14772. return ret;
  14773. #endif /* !HAVE_FAST_RSA && !HAVE_FIPS */
  14774. #endif /* WC_NO_RSA_OAEP && !WC_NO_RNG */
  14775. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  14776. #if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(NO_ASN) \
  14777. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  14778. ret = rsa_export_key_test(key);
  14779. if (ret != 0)
  14780. return ret;
  14781. #endif
  14782. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  14783. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  14784. ret = rsa_flatten_test(key);
  14785. if (ret != 0)
  14786. return ret;
  14787. #endif
  14788. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_ASN) && \
  14789. !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  14790. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  14791. (void)clientCert;
  14792. #endif
  14793. #ifdef WOLFSSL_TEST_CERT
  14794. #if defined(WOLFSSL_MDK_ARM)
  14795. #define sizeof(s) XSTRLEN((char *)(s))
  14796. #endif
  14797. #ifdef USE_CERT_BUFFERS_1024
  14798. XMEMCPY(tmp, client_cert_der_1024, (size_t)sizeof_client_cert_der_1024);
  14799. bytes = (size_t)sizeof_client_cert_der_1024;
  14800. #elif defined(USE_CERT_BUFFERS_2048)
  14801. XMEMCPY(tmp, client_cert_der_2048, (size_t)sizeof_client_cert_der_2048);
  14802. bytes = (size_t)sizeof_client_cert_der_2048;
  14803. #elif defined(USE_CERT_BUFFERS_3072)
  14804. XMEMCPY(tmp, client_cert_der_3072, (size_t)sizeof_client_cert_der_3072);
  14805. bytes = (size_t)sizeof_client_cert_der_3072;
  14806. #elif defined(USE_CERT_BUFFERS_4096)
  14807. XMEMCPY(tmp, client_cert_der_4096, (size_t)sizeof_client_cert_der_4096);
  14808. bytes = (size_t)sizeof_client_cert_der_4096;
  14809. #elif !defined(NO_FILESYSTEM)
  14810. file2 = XFOPEN(clientCert, "rb");
  14811. if (!file2) {
  14812. ERROR_OUT(-7940, exit_rsa);
  14813. }
  14814. bytes = XFREAD(tmp, 1, FOURK_BUF, file2);
  14815. XFCLOSE(file2);
  14816. #else
  14817. /* No certificate to use. */
  14818. ERROR_OUT(-7941, exit_rsa);
  14819. #endif
  14820. #ifdef sizeof
  14821. #undef sizeof
  14822. #endif
  14823. InitDecodedCert(cert, tmp, (word32)bytes, NULL);
  14824. ret = ParseCert(cert, CERT_TYPE, NO_VERIFY, NULL);
  14825. if (ret != 0) {
  14826. FreeDecodedCert(cert);
  14827. ERROR_OUT(-7942, exit_rsa);
  14828. }
  14829. #ifndef NO_ASN_TIME
  14830. ret = wc_GetDateInfo(cert->afterDate, cert->afterDateLen, &date,
  14831. &dateFormat, &dateLength);
  14832. if (ret != 0) {
  14833. FreeDecodedCert(cert);
  14834. ERROR_OUT(-7943, exit_rsa);
  14835. }
  14836. ret = wc_GetDateAsCalendarTime(date, dateLength, dateFormat, &timearg);
  14837. if (ret != 0) {
  14838. FreeDecodedCert(cert);
  14839. ERROR_OUT(-7944, exit_rsa);
  14840. }
  14841. #endif
  14842. FreeDecodedCert(cert);
  14843. #endif /* WOLFSSL_TEST_CERT */
  14844. #ifdef WOLFSSL_CERT_EXT
  14845. #ifdef USE_CERT_BUFFERS_1024
  14846. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  14847. bytes = sizeof_client_keypub_der_1024;
  14848. #elif defined(USE_CERT_BUFFERS_2048)
  14849. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  14850. bytes = sizeof_client_keypub_der_2048;
  14851. #elif defined(USE_CERT_BUFFERS_3072)
  14852. XMEMCPY(tmp, client_keypub_der_3072, sizeof_client_keypub_der_3072);
  14853. bytes = sizeof_client_keypub_der_3072;
  14854. #elif defined(USE_CERT_BUFFERS_4096)
  14855. XMEMCPY(tmp, client_keypub_der_4096, sizeof_client_keypub_der_4096);
  14856. bytes = sizeof_client_keypub_der_4096;
  14857. #else
  14858. file = XFOPEN(clientKeyPub, "rb");
  14859. if (!file) {
  14860. err_sys("can't open ./certs/client-keyPub.der, "
  14861. "Please run from wolfSSL home dir", -40);
  14862. ERROR_OUT(-7945, exit_rsa);
  14863. }
  14864. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  14865. XFCLOSE(file);
  14866. #endif /* USE_CERT_BUFFERS */
  14867. ret = wc_InitRsaKey(keypub, HEAP_HINT);
  14868. if (ret != 0) {
  14869. ERROR_OUT(-7946, exit_rsa);
  14870. }
  14871. idx = 0;
  14872. ret = wc_RsaPublicKeyDecode(tmp, &idx, keypub, (word32)bytes);
  14873. if (ret != 0) {
  14874. ERROR_OUT(-7947, exit_rsa);
  14875. }
  14876. #endif /* WOLFSSL_CERT_EXT */
  14877. #ifdef WOLFSSL_KEY_GEN
  14878. ret = rsa_keygen_test(&rng);
  14879. if (ret != 0)
  14880. goto exit_rsa;
  14881. #endif
  14882. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  14883. /* Make Cert / Sign example for RSA cert and RSA CA */
  14884. ret = rsa_certgen_test(key, keypub, &rng, tmp);
  14885. if (ret != 0)
  14886. goto exit_rsa;
  14887. #if !defined(NO_RSA) && defined(HAVE_ECC)
  14888. ret = rsa_ecc_certgen_test(&rng, tmp);
  14889. if (ret != 0)
  14890. goto exit_rsa;
  14891. #endif
  14892. #if defined(WOLFSSL_CERT_REQ) && !defined(WOLFSSL_NO_MALLOC)
  14893. {
  14894. Cert *req;
  14895. int derSz;
  14896. #ifndef WOLFSSL_SMALL_STACK
  14897. byte* der = NULL;
  14898. #endif
  14899. req = (Cert *)XMALLOC(sizeof *req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14900. if (! req)
  14901. ERROR_OUT(MEMORY_E, exit_rsa);
  14902. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,DYNAMIC_TYPE_TMP_BUFFER);
  14903. if (der == NULL) {
  14904. ERROR_OUT(-7964, exit_rsa);
  14905. }
  14906. if (wc_InitCert_ex(req, HEAP_HINT, devId)) {
  14907. ERROR_OUT(-7965, exit_rsa);
  14908. }
  14909. req->version = 0;
  14910. req->isCA = 1;
  14911. XSTRNCPY(req->challengePw, "wolf123", CTC_NAME_SIZE);
  14912. XMEMCPY(&req->subject, &certDefaultName, sizeof(CertName));
  14913. #ifndef NO_SHA256
  14914. req->sigType = CTC_SHA256wRSA;
  14915. #else
  14916. req->sigType = CTC_SHAwRSA;
  14917. #endif
  14918. #ifdef WOLFSSL_CERT_EXT
  14919. /* add SKID from the Public Key */
  14920. if (wc_SetSubjectKeyIdFromPublicKey(req, keypub, NULL) != 0) {
  14921. ERROR_OUT(-7966, exit_rsa);
  14922. }
  14923. /* add Key Usage */
  14924. if (wc_SetKeyUsage(req, certKeyUsage2) != 0) {
  14925. ERROR_OUT(-7967, exit_rsa);
  14926. }
  14927. /* add Extended Key Usage */
  14928. if (wc_SetExtKeyUsage(req, "serverAuth,clientAuth,codeSigning,"
  14929. "emailProtection,timeStamping,OCSPSigning") != 0) {
  14930. ERROR_OUT(-7968, exit_rsa);
  14931. }
  14932. #ifdef WOLFSSL_EKU_OID
  14933. {
  14934. WOLFSSL_SMALL_STACK_STATIC const char unique[] = "2.16.840.1.111111.100.1.10.1";
  14935. if (wc_SetExtKeyUsageOID(req, unique, sizeof(unique), 0,
  14936. HEAP_HINT) != 0) {
  14937. ERROR_OUT(-7969, exit_rsa);
  14938. }
  14939. }
  14940. #endif /* WOLFSSL_EKU_OID */
  14941. #endif /* WOLFSSL_CERT_EXT */
  14942. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  14943. if (derSz < 0) {
  14944. ERROR_OUT(-7970, exit_rsa);
  14945. }
  14946. #ifdef WOLFSSL_CERT_EXT
  14947. /* Try again with "any" flag set, will override all others */
  14948. if (wc_SetExtKeyUsage(req, "any") != 0) {
  14949. ERROR_OUT(-7971, exit_rsa);
  14950. }
  14951. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  14952. if (derSz < 0) {
  14953. ERROR_OUT(-7972, exit_rsa);
  14954. }
  14955. #endif /* WOLFSSL_CERT_EXT */
  14956. ret = 0;
  14957. do {
  14958. #if defined(WOLFSSL_ASYNC_CRYPT)
  14959. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14960. #endif
  14961. if (ret >= 0) {
  14962. ret = wc_SignCert(req->bodySz, req->sigType, der, FOURK_BUF,
  14963. key, NULL, &rng);
  14964. }
  14965. } while (ret == WC_PENDING_E);
  14966. if (ret < 0) {
  14967. ERROR_OUT(-7973, exit_rsa);
  14968. }
  14969. derSz = ret;
  14970. ret = SaveDerAndPem(der, derSz, certReqDerFile, certReqPemFile,
  14971. CERTREQ_TYPE, -5650);
  14972. if (ret != 0) {
  14973. goto exit_rsa;
  14974. }
  14975. derSz = wc_MakeCertReq_ex(req, der, FOURK_BUF, RSA_TYPE, key);
  14976. if (derSz < 0) {
  14977. ERROR_OUT(-7974, exit_rsa);
  14978. }
  14979. /* Test getting the size of the buffer without providing the buffer.
  14980. * derSz is set to the "largest buffer" we are willing to allocate. */
  14981. derSz = wc_MakeCertReq(req, NULL, 10000, key, NULL);
  14982. if (derSz < 0) {
  14983. ERROR_OUT(-7975, exit_rsa);
  14984. }
  14985. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14986. XFREE(req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14987. der = NULL;
  14988. }
  14989. #endif /* WOLFSSL_CERT_REQ */
  14990. #endif /* WOLFSSL_CERT_GEN */
  14991. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  14992. /* Need to create known good signatures to test with this. */
  14993. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  14994. !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14995. ret = rsa_pss_test(&rng, key);
  14996. if (ret != 0)
  14997. goto exit_rsa;
  14998. #endif
  14999. #endif
  15000. #if defined(WOLFSSL_HAVE_SP_RSA) && defined(USE_FAST_MATH)
  15001. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15002. /* New key to be loaded in rsa_even_mod_test(). */
  15003. if (key != NULL)
  15004. #endif
  15005. wc_FreeRsaKey(key);
  15006. /* New key to be loaded in rsa_even_mod_test(). */
  15007. ret = rsa_even_mod_test(&rng, key);
  15008. #endif
  15009. exit_rsa:
  15010. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15011. if (key != NULL) {
  15012. wc_FreeRsaKey(key);
  15013. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15014. }
  15015. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  15016. if (keypub != NULL) {
  15017. wc_FreeRsaKey(keypub);
  15018. XFREE(keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15019. }
  15020. #endif
  15021. #ifdef WOLFSSL_TEST_CERT
  15022. if (cert != NULL)
  15023. XFREE(cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15024. #endif
  15025. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15026. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15027. #else
  15028. wc_FreeRsaKey(key);
  15029. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  15030. wc_FreeRsaKey(keypub);
  15031. #endif
  15032. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  15033. wc_FreeRng(&rng);
  15034. WC_FREE_VAR(in, HEAP_HINT);
  15035. WC_FREE_VAR(out, HEAP_HINT);
  15036. WC_FREE_VAR(plain, HEAP_HINT);
  15037. (void)res;
  15038. (void)bytes;
  15039. (void)idx;
  15040. (void)in;
  15041. (void)out;
  15042. (void)plain;
  15043. (void)idx;
  15044. (void)inStr;
  15045. (void)inLen;
  15046. (void)outSz;
  15047. (void)plainSz;
  15048. /* ret can be greater then 0 with certgen but all negative values should
  15049. * be returned and treated as an error */
  15050. if (ret >= 0) {
  15051. return 0;
  15052. }
  15053. else {
  15054. return ret;
  15055. }
  15056. }
  15057. #endif /* !NO_RSA */
  15058. #ifndef NO_DH
  15059. static int dh_fips_generate_test(WC_RNG *rng)
  15060. {
  15061. int ret = 0;
  15062. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15063. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15064. #else
  15065. DhKey key[1];
  15066. #endif
  15067. WOLFSSL_SMALL_STACK_STATIC const byte p[] = {
  15068. 0xc5, 0x7c, 0xa2, 0x4f, 0x4b, 0xd6, 0x8c, 0x3c,
  15069. 0xda, 0xc7, 0xba, 0xaa, 0xea, 0x2e, 0x5c, 0x1e,
  15070. 0x18, 0xb2, 0x7b, 0x8c, 0x55, 0x65, 0x9f, 0xea,
  15071. 0xe0, 0xa1, 0x36, 0x53, 0x2b, 0x36, 0xe0, 0x4e,
  15072. 0x3e, 0x64, 0xa9, 0xe4, 0xfc, 0x8f, 0x32, 0x62,
  15073. 0x97, 0xe4, 0xbe, 0xf7, 0xc1, 0xde, 0x07, 0x5a,
  15074. 0x89, 0x28, 0xf3, 0xfe, 0x4f, 0xfe, 0x68, 0xbc,
  15075. 0xfb, 0x0a, 0x7c, 0xa4, 0xb3, 0x14, 0x48, 0x89,
  15076. 0x9f, 0xaf, 0xb8, 0x43, 0xe2, 0xa0, 0x62, 0x5c,
  15077. 0xb4, 0x88, 0x3f, 0x06, 0x50, 0x11, 0xfe, 0x65,
  15078. 0x8d, 0x49, 0xd2, 0xf5, 0x4b, 0x74, 0x79, 0xdb,
  15079. 0x06, 0x62, 0x92, 0x89, 0xed, 0xda, 0xcb, 0x87,
  15080. 0x37, 0x16, 0xd2, 0xa1, 0x7a, 0xe8, 0xde, 0x92,
  15081. 0xee, 0x3e, 0x41, 0x4a, 0x91, 0x5e, 0xed, 0xf3,
  15082. 0x6c, 0x6b, 0x7e, 0xfd, 0x15, 0x92, 0x18, 0xfc,
  15083. 0xa7, 0xac, 0x42, 0x85, 0x57, 0xe9, 0xdc, 0xda,
  15084. 0x55, 0xc9, 0x8b, 0x28, 0x9e, 0xc1, 0xc4, 0x46,
  15085. 0x4d, 0x88, 0xed, 0x62, 0x8e, 0xdb, 0x3f, 0xb9,
  15086. 0xd7, 0xc8, 0xe3, 0xcf, 0xb8, 0x34, 0x2c, 0xd2,
  15087. 0x6f, 0x28, 0x06, 0x41, 0xe3, 0x66, 0x8c, 0xfc,
  15088. 0x72, 0xff, 0x26, 0x3b, 0x6b, 0x6c, 0x6f, 0x73,
  15089. 0xde, 0xf2, 0x90, 0x29, 0xe0, 0x61, 0x32, 0xc4,
  15090. 0x12, 0x74, 0x09, 0x52, 0xec, 0xf3, 0x1b, 0xa6,
  15091. 0x45, 0x98, 0xac, 0xf9, 0x1c, 0x65, 0x8e, 0x3a,
  15092. 0x91, 0x84, 0x4b, 0x23, 0x8a, 0xb2, 0x3c, 0xc9,
  15093. 0xfa, 0xea, 0xf1, 0x38, 0xce, 0xd8, 0x05, 0xe0,
  15094. 0xfa, 0x44, 0x68, 0x1f, 0xeb, 0xd9, 0x57, 0xb8,
  15095. 0x4a, 0x97, 0x5b, 0x88, 0xc5, 0xf1, 0xbb, 0xb0,
  15096. 0x49, 0xc3, 0x91, 0x7c, 0xd3, 0x13, 0xb9, 0x47,
  15097. 0xbb, 0x91, 0x8f, 0xe5, 0x26, 0x07, 0xab, 0xa9,
  15098. 0xc5, 0xd0, 0x3d, 0x95, 0x41, 0x26, 0x92, 0x9d,
  15099. 0x13, 0x67, 0xf2, 0x7e, 0x11, 0x88, 0xdc, 0x2d
  15100. };
  15101. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  15102. 0x4a, 0x1a, 0xf3, 0xa4, 0x92, 0xe9, 0xee, 0x74,
  15103. 0x6e, 0x57, 0xd5, 0x8c, 0x2c, 0x5b, 0x41, 0x41,
  15104. 0x5e, 0xd4, 0x55, 0x19, 0xdc, 0xd9, 0x32, 0x91,
  15105. 0xf7, 0xfd, 0xc2, 0x57, 0xff, 0x03, 0x14, 0xdb,
  15106. 0xf1, 0xb7, 0x60, 0x0c, 0x43, 0x59, 0x3f, 0xff,
  15107. 0xac, 0xf1, 0x80, 0x9a, 0x15, 0x6f, 0xd8, 0x6e,
  15108. 0xb7, 0x85, 0x18, 0xc8, 0xec, 0x4e, 0x59, 0x4a,
  15109. 0xe2, 0x91, 0x43, 0x4c, 0xeb, 0x95, 0xb6, 0x2e,
  15110. 0x9a, 0xea, 0x53, 0x68, 0x80, 0x64, 0x69, 0x40,
  15111. 0xf9, 0xec, 0xbd, 0x85, 0x89, 0x26, 0x97, 0x67,
  15112. 0xaf, 0xb0, 0xad, 0x00, 0x1b, 0xd4, 0xfd, 0x94,
  15113. 0xd3, 0xe9, 0x92, 0xb1, 0xb4, 0xbc, 0x5a, 0xaa,
  15114. 0x92, 0x80, 0x89, 0x3b, 0x39, 0x05, 0x6c, 0x22,
  15115. 0x26, 0xfe, 0x5a, 0x28, 0x6c, 0x37, 0x50, 0x5a,
  15116. 0x38, 0x99, 0xcf, 0xf3, 0xc1, 0x96, 0x45, 0xdc,
  15117. 0x01, 0xcb, 0x20, 0x87, 0xa5, 0x00, 0x8c, 0xf5,
  15118. 0x4d, 0xc2, 0xef, 0xb8, 0x9b, 0xd1, 0x87, 0xbe,
  15119. 0xed, 0xd5, 0x0a, 0x29, 0x15, 0x34, 0x59, 0x4c,
  15120. 0x3a, 0x05, 0x22, 0x05, 0x44, 0x4f, 0x9f, 0xc8,
  15121. 0x47, 0x12, 0x24, 0x8e, 0xa8, 0x79, 0xe4, 0x67,
  15122. 0xba, 0x4d, 0x5b, 0x75, 0x56, 0x95, 0xeb, 0xe8,
  15123. 0x8a, 0xfa, 0x8e, 0x01, 0x8c, 0x1b, 0x74, 0x63,
  15124. 0xd9, 0x2f, 0xf7, 0xd3, 0x44, 0x8f, 0xa8, 0xf5,
  15125. 0xaf, 0x6c, 0x4f, 0xdb, 0xe7, 0xc9, 0x6c, 0x71,
  15126. 0x22, 0xa3, 0x1d, 0xf1, 0x40, 0xb2, 0xe0, 0x9a,
  15127. 0xb6, 0x72, 0xc9, 0xc0, 0x13, 0x16, 0xa2, 0x4a,
  15128. 0xe1, 0x92, 0xc7, 0x54, 0x23, 0xab, 0x9d, 0xa1,
  15129. 0xa1, 0xe5, 0x0b, 0xed, 0xba, 0xe8, 0x84, 0x37,
  15130. 0xb2, 0xe7, 0xfe, 0x32, 0x8d, 0xfa, 0x1c, 0x53,
  15131. 0x77, 0x97, 0xc7, 0xf3, 0x48, 0xc9, 0xdb, 0x2d,
  15132. 0x75, 0x52, 0x9d, 0x42, 0x51, 0x78, 0x62, 0x68,
  15133. 0x05, 0x45, 0x15, 0xf8, 0xa2, 0x4e, 0xf3, 0x0b
  15134. };
  15135. WOLFSSL_SMALL_STACK_STATIC const byte q[] = {
  15136. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  15137. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  15138. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  15139. 0x40, 0x52, 0xed, 0x41
  15140. };
  15141. WOLFSSL_SMALL_STACK_STATIC const byte q0[] = {
  15142. 0x00,
  15143. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  15144. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  15145. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  15146. 0x40, 0x52, 0xed, 0x41
  15147. };
  15148. byte priv[256];
  15149. byte pub[256];
  15150. word32 privSz = sizeof(priv);
  15151. word32 pubSz = sizeof(pub);
  15152. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15153. if (key == NULL)
  15154. ERROR_OUT(MEMORY_E, exit_gen_test);
  15155. #endif
  15156. /* Parameter Validation testing. */
  15157. ret = wc_DhGenerateKeyPair(NULL, rng, priv, &privSz, pub, &pubSz);
  15158. if (ret != BAD_FUNC_ARG)
  15159. ERROR_OUT(-7980, exit_gen_test);
  15160. ret = wc_DhGenerateKeyPair(key, NULL, priv, &privSz, pub, &pubSz);
  15161. if (ret != BAD_FUNC_ARG)
  15162. ERROR_OUT(-7981, exit_gen_test);
  15163. ret = wc_DhGenerateKeyPair(key, rng, NULL, &privSz, pub, &pubSz);
  15164. if (ret != BAD_FUNC_ARG)
  15165. ERROR_OUT(-7982, exit_gen_test);
  15166. ret = wc_DhGenerateKeyPair(key, rng, priv, NULL, pub, &pubSz);
  15167. if (ret != BAD_FUNC_ARG)
  15168. ERROR_OUT(-7983, exit_gen_test);
  15169. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, NULL, &pubSz);
  15170. if (ret != BAD_FUNC_ARG)
  15171. ERROR_OUT(-7984, exit_gen_test);
  15172. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, NULL);
  15173. if (ret != BAD_FUNC_ARG)
  15174. ERROR_OUT(-7985, exit_gen_test);
  15175. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15176. if (ret != 0)
  15177. ERROR_OUT(-7986, exit_gen_test);
  15178. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q0, sizeof(q0));
  15179. if (ret != 0) {
  15180. ERROR_OUT(-7987, exit_gen_test);
  15181. }
  15182. wc_FreeDhKey(key);
  15183. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15184. if (ret != 0)
  15185. ERROR_OUT(-7988, exit_gen_test);
  15186. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q, sizeof(q));
  15187. if (ret != 0) {
  15188. ERROR_OUT(-7989, exit_gen_test);
  15189. }
  15190. /* Use API. */
  15191. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  15192. #if defined(WOLFSSL_ASYNC_CRYPT)
  15193. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15194. #endif
  15195. if (ret != 0) {
  15196. ERROR_OUT(-7990, exit_gen_test);
  15197. }
  15198. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q0, sizeof(q0));
  15199. if (ret != 0) {
  15200. ERROR_OUT(-7991, exit_gen_test);
  15201. }
  15202. wc_FreeDhKey(key);
  15203. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15204. if (ret != 0)
  15205. ERROR_OUT(-7992, exit_gen_test);
  15206. ret = wc_DhSetKey(key, p, sizeof(p), g, sizeof(g));
  15207. if (ret != 0) {
  15208. ERROR_OUT(-7993, exit_gen_test);
  15209. }
  15210. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q, sizeof(q));
  15211. if (ret != 0) {
  15212. ERROR_OUT(-7994, exit_gen_test);
  15213. }
  15214. #ifndef HAVE_SELFTEST
  15215. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  15216. if (ret != 0) {
  15217. ERROR_OUT(-7995, exit_gen_test);
  15218. }
  15219. /* Taint the public key so the check fails. */
  15220. pub[0]++;
  15221. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  15222. if (ret != MP_CMP_E) {
  15223. ERROR_OUT(-7996, exit_gen_test);
  15224. }
  15225. #ifdef WOLFSSL_KEY_GEN
  15226. wc_FreeDhKey(key);
  15227. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15228. if (ret != 0)
  15229. ERROR_OUT(-7997, exit_gen_test);
  15230. ret = wc_DhGenerateParams(rng, 2048, key);
  15231. if (ret != 0) {
  15232. ERROR_OUT(-7998, exit_gen_test);
  15233. }
  15234. privSz = sizeof(priv);
  15235. pubSz = sizeof(pub);
  15236. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  15237. #if defined(WOLFSSL_ASYNC_CRYPT)
  15238. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15239. #endif
  15240. if (ret != 0) {
  15241. ERROR_OUT(-7999, exit_gen_test);
  15242. }
  15243. #endif /* WOLFSSL_KEY_GEN */
  15244. #endif /* HAVE_SELFTEST */
  15245. ret = 0;
  15246. exit_gen_test:
  15247. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15248. if (key) {
  15249. wc_FreeDhKey(key);
  15250. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15251. }
  15252. #else
  15253. wc_FreeDhKey(key);
  15254. #endif
  15255. return ret;
  15256. }
  15257. static int dh_generate_test(WC_RNG *rng)
  15258. {
  15259. int ret = 0;
  15260. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15261. DhKey *smallKey = NULL;
  15262. #else
  15263. DhKey smallKey[1];
  15264. #endif
  15265. byte p[2] = { 1, 7 }; /* 263 in decimal */
  15266. byte g[2] = { 0, 2 };
  15267. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FFDHE)
  15268. #ifdef WOLFSSL_DH_CONST
  15269. /* the table for constant DH lookup will round to the lowest byte size 21 */
  15270. byte priv[21];
  15271. byte pub[21];
  15272. #else
  15273. byte priv[2];
  15274. byte pub[2];
  15275. #endif
  15276. word32 privSz = sizeof(priv);
  15277. word32 pubSz = sizeof(pub);
  15278. #endif
  15279. int smallKey_inited = 0;
  15280. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15281. if ((smallKey = (DhKey *)XMALLOC(sizeof(*smallKey), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  15282. return -8019;
  15283. #endif
  15284. ret = wc_InitDhKey_ex(smallKey, HEAP_HINT, devId);
  15285. if (ret != 0)
  15286. ERROR_OUT(-8010, exit_gen_test);
  15287. smallKey_inited = 1;
  15288. /* Parameter Validation testing. */
  15289. ret = wc_InitDhKey_ex(NULL, HEAP_HINT, devId);
  15290. if (ret != BAD_FUNC_ARG)
  15291. return -8011;
  15292. wc_FreeDhKey(NULL);
  15293. ret = wc_DhSetKey(NULL, p, sizeof(p), g, sizeof(g));
  15294. if (ret != BAD_FUNC_ARG) {
  15295. ERROR_OUT(-8012, exit_gen_test);
  15296. }
  15297. ret = wc_DhSetKey(smallKey, NULL, sizeof(p), g, sizeof(g));
  15298. if (ret != BAD_FUNC_ARG) {
  15299. ERROR_OUT(-8013, exit_gen_test);
  15300. }
  15301. ret = wc_DhSetKey(smallKey, p, 0, g, sizeof(g));
  15302. if (ret != BAD_FUNC_ARG) {
  15303. ERROR_OUT(-8014, exit_gen_test);
  15304. }
  15305. ret = wc_DhSetKey(smallKey, p, sizeof(p), NULL, sizeof(g));
  15306. if (ret != BAD_FUNC_ARG) {
  15307. ERROR_OUT(-8015, exit_gen_test);
  15308. }
  15309. ret = wc_DhSetKey(smallKey, p, sizeof(p), g, 0);
  15310. if (ret != BAD_FUNC_ARG) {
  15311. ERROR_OUT(-8016, exit_gen_test);
  15312. }
  15313. ret = wc_DhSetKey(smallKey, p, sizeof(p), g, sizeof(g));
  15314. if (ret != 0) {
  15315. ERROR_OUT(-8017, exit_gen_test);
  15316. }
  15317. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FFDHE)
  15318. /* Use API. */
  15319. ret = wc_DhGenerateKeyPair(smallKey, rng, priv, &privSz, pub, &pubSz);
  15320. #if defined(WOLFSSL_ASYNC_CRYPT)
  15321. ret = wc_AsyncWait(ret, &smallKey->asyncDev, WC_ASYNC_FLAG_NONE);
  15322. #endif
  15323. if (ret != 0) {
  15324. ret = -8018;
  15325. }
  15326. #else
  15327. (void)rng;
  15328. #if defined(HAVE_FIPS) || !defined(WOLFSSL_NO_DH186)
  15329. ret = 0;
  15330. #endif
  15331. #endif
  15332. #if !defined(HAVE_FIPS) && defined(WOLFSSL_NO_DH186)
  15333. {
  15334. byte priv[260];
  15335. byte pub[260];
  15336. word32 privSz = sizeof(priv);
  15337. word32 pubSz = sizeof(pub);
  15338. /* test odd ball param generation with DH */
  15339. wc_FreeDhKey(smallKey);
  15340. ret = wc_InitDhKey_ex(smallKey, HEAP_HINT, devId);
  15341. if (ret != 0)
  15342. ERROR_OUT(-8019, exit_gen_test);
  15343. ret = wc_DhGenerateParams(rng, 2056, smallKey);
  15344. if (ret != 0) {
  15345. ERROR_OUT(-8020, exit_gen_test);
  15346. }
  15347. privSz = sizeof(priv);
  15348. pubSz = sizeof(pub);
  15349. ret = wc_DhGenerateKeyPair(smallKey, rng, priv, &privSz, pub, &pubSz);
  15350. #if defined(WOLFSSL_ASYNC_CRYPT)
  15351. ret = wc_AsyncWait(ret, &smallKey->asyncDev, WC_ASYNC_FLAG_NONE);
  15352. #endif
  15353. if (ret != 0) {
  15354. ERROR_OUT(-8021, exit_gen_test);
  15355. }
  15356. }
  15357. #endif /* !HAVE_FIPS and WOLFSSL_NO_DH186 */
  15358. exit_gen_test:
  15359. if (smallKey_inited)
  15360. wc_FreeDhKey(smallKey);
  15361. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15362. if (smallKey != NULL)
  15363. XFREE(smallKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15364. #endif
  15365. return ret;
  15366. }
  15367. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  15368. typedef struct dh_pubvalue_test {
  15369. const byte* data;
  15370. word32 len;
  15371. } dh_pubvalue_test;
  15372. static int dh_test_check_pubvalue(void)
  15373. {
  15374. int ret;
  15375. word32 i;
  15376. WOLFSSL_SMALL_STACK_STATIC const byte prime[] = {0x01, 0x00, 0x01};
  15377. WOLFSSL_SMALL_STACK_STATIC const byte pubValZero[] = { 0x00 };
  15378. WOLFSSL_SMALL_STACK_STATIC const byte pubValZeroLong[] = { 0x00, 0x00, 0x00 };
  15379. WOLFSSL_SMALL_STACK_STATIC const byte pubValOne[] = { 0x01 };
  15380. WOLFSSL_SMALL_STACK_STATIC const byte pubValOneLong[] = { 0x00, 0x00, 0x01 };
  15381. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeMinusOne[] = { 0x01, 0x00, 0x00 };
  15382. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeLong[] = {0x00, 0x01, 0x00, 0x01};
  15383. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimePlusOne[] = { 0x01, 0x00, 0x02 };
  15384. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig0[] = { 0x02, 0x00, 0x01 };
  15385. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig1[] = { 0x01, 0x01, 0x01 };
  15386. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooLong[] = { 0x01, 0x00, 0x00, 0x01 };
  15387. const dh_pubvalue_test dh_pubval_fail[] = {
  15388. { prime, sizeof(prime) },
  15389. { pubValZero, sizeof(pubValZero) },
  15390. { pubValZeroLong, sizeof(pubValZeroLong) },
  15391. { pubValOne, sizeof(pubValOne) },
  15392. { pubValOneLong, sizeof(pubValOneLong) },
  15393. { pubValPrimeMinusOne, sizeof(pubValPrimeMinusOne) },
  15394. { pubValPrimeLong, sizeof(pubValPrimeLong) },
  15395. { pubValPrimePlusOne, sizeof(pubValPrimePlusOne) },
  15396. { pubValTooBig0, sizeof(pubValTooBig0) },
  15397. { pubValTooBig1, sizeof(pubValTooBig1) },
  15398. { pubValTooLong, sizeof(pubValTooLong) },
  15399. };
  15400. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwo[] = { 0x02 };
  15401. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwoLong[] = { 0x00, 0x00, 0x02 };
  15402. WOLFSSL_SMALL_STACK_STATIC const byte pubValGood[] = { 0x12, 0x34 };
  15403. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLen[] = { 0x00, 0x12, 0x34 };
  15404. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLong[] = { 0x00, 0x00, 0x12, 0x34 };
  15405. const dh_pubvalue_test dh_pubval_pass[] = {
  15406. { pubValTwo, sizeof(pubValTwo) },
  15407. { pubValTwoLong, sizeof(pubValTwoLong) },
  15408. { pubValGood, sizeof(pubValGood) },
  15409. { pubValGoodLen, sizeof(pubValGoodLen) },
  15410. { pubValGoodLong, sizeof(pubValGoodLong) },
  15411. };
  15412. for (i = 0; i < sizeof(dh_pubval_fail) / sizeof(*dh_pubval_fail); i++) {
  15413. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_fail[i].data,
  15414. dh_pubval_fail[i].len);
  15415. if (ret != MP_VAL)
  15416. return -8020 - (int)i;
  15417. }
  15418. for (i = 0; i < sizeof(dh_pubval_pass) / sizeof(*dh_pubval_pass); i++) {
  15419. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_pass[i].data,
  15420. dh_pubval_pass[i].len);
  15421. if (ret != 0)
  15422. return -8030 - (int)i;
  15423. }
  15424. return 0;
  15425. }
  15426. #endif
  15427. #if defined(HAVE_FFDHE)
  15428. #if defined(HAVE_FFDHE_4096)
  15429. #define MAX_DH_PRIV_SZ 39
  15430. #define MAX_DH_KEY_SZ 512
  15431. #elif defined(HAVE_FFDHE_3072)
  15432. #define MAX_DH_PRIV_SZ 34
  15433. #define MAX_DH_KEY_SZ 384
  15434. #else
  15435. #define MAX_DH_PRIV_SZ 29
  15436. #define MAX_DH_KEY_SZ 256
  15437. #endif
  15438. #ifndef WC_NO_RNG
  15439. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  15440. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM)))
  15441. #ifdef HAVE_PUBLIC_FFDHE
  15442. static int dh_ffdhe_test(WC_RNG *rng, const DhParams* params)
  15443. #else
  15444. static int dh_ffdhe_test(WC_RNG *rng, int name)
  15445. #endif
  15446. {
  15447. int ret;
  15448. word32 privSz, pubSz, privSz2, pubSz2;
  15449. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15450. byte *priv = (byte*)XMALLOC(MAX_DH_PRIV_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15451. byte *pub = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15452. byte *priv2 = (byte*)XMALLOC(MAX_DH_PRIV_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15453. byte *pub2 = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15454. byte *agree = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15455. byte *agree2 = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15456. DhKey *key = (DhKey*)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15457. DhKey *key2 = (DhKey*)XMALLOC(sizeof(*key2), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15458. #else
  15459. byte priv[MAX_DH_PRIV_SZ];
  15460. byte pub[MAX_DH_KEY_SZ];
  15461. byte priv2[MAX_DH_PRIV_SZ];
  15462. byte pub2[MAX_DH_KEY_SZ];
  15463. byte agree[MAX_DH_KEY_SZ];
  15464. byte agree2[MAX_DH_KEY_SZ];
  15465. DhKey key[1];
  15466. DhKey key2[1];
  15467. #endif
  15468. word32 agreeSz = MAX_DH_KEY_SZ;
  15469. word32 agreeSz2 = MAX_DH_KEY_SZ;
  15470. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15471. if ((priv == NULL) ||
  15472. (pub == NULL) ||
  15473. (priv2 == NULL) ||
  15474. (pub2 == NULL) ||
  15475. (agree == NULL) ||
  15476. (agree2 == NULL) ||
  15477. (key == NULL) ||
  15478. (key2 == NULL))
  15479. ERROR_OUT(-8050, done);
  15480. #endif
  15481. pubSz = MAX_DH_KEY_SZ;
  15482. pubSz2 = MAX_DH_KEY_SZ;
  15483. #ifdef HAVE_PUBLIC_FFDHE
  15484. privSz = MAX_DH_PRIV_SZ;
  15485. privSz2 = MAX_DH_PRIV_SZ;
  15486. #else
  15487. privSz = wc_DhGetNamedKeyMinSize(name);
  15488. privSz2 = privSz;
  15489. #endif
  15490. XMEMSET(key, 0, sizeof(*key));
  15491. XMEMSET(key2, 0, sizeof(*key2));
  15492. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15493. if (ret != 0) {
  15494. ERROR_OUT(-8051, done);
  15495. }
  15496. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  15497. if (ret != 0) {
  15498. ERROR_OUT(-8052, done);
  15499. }
  15500. #ifdef HAVE_PUBLIC_FFDHE
  15501. ret = wc_DhSetKey(key, params->p, params->p_len, params->g, params->g_len);
  15502. #else
  15503. ret = wc_DhSetNamedKey(key, name);
  15504. #endif
  15505. if (ret != 0) {
  15506. ERROR_OUT(-8053, done);
  15507. }
  15508. #ifdef HAVE_PUBLIC_FFDHE
  15509. ret = wc_DhSetKey(key2, params->p, params->p_len, params->g,
  15510. params->g_len);
  15511. #else
  15512. ret = wc_DhSetNamedKey(key2, name);
  15513. #endif
  15514. if (ret != 0) {
  15515. ERROR_OUT(-8054, done);
  15516. }
  15517. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  15518. #if defined(WOLFSSL_ASYNC_CRYPT)
  15519. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15520. #endif
  15521. if (ret != 0) {
  15522. ERROR_OUT(-8055, done);
  15523. }
  15524. ret = wc_DhGenerateKeyPair(key2, rng, priv2, &privSz2, pub2, &pubSz2);
  15525. #if defined(WOLFSSL_ASYNC_CRYPT)
  15526. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  15527. #endif
  15528. if (ret != 0) {
  15529. ERROR_OUT(-8056, done);
  15530. }
  15531. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  15532. #if defined(WOLFSSL_ASYNC_CRYPT)
  15533. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15534. #endif
  15535. if (ret != 0) {
  15536. ERROR_OUT(-8057, done);
  15537. }
  15538. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  15539. #if defined(WOLFSSL_ASYNC_CRYPT)
  15540. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  15541. #endif
  15542. if (ret != 0) {
  15543. ERROR_OUT(-8058, done);
  15544. }
  15545. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  15546. ERROR_OUT(-8059, done);
  15547. }
  15548. #if defined(WOLFSSL_HAVE_SP_DH) || defined(USE_FAST_MATH)
  15549. /* Make p even */
  15550. key->p.dp[0] &= (mp_digit)-2;
  15551. if (ret != 0) {
  15552. ERROR_OUT(-8058, done);
  15553. }
  15554. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  15555. #if defined(WOLFSSL_ASYNC_CRYPT)
  15556. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15557. #endif
  15558. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  15559. ERROR_OUT(-8058, done);
  15560. }
  15561. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  15562. #if defined(WOLFSSL_ASYNC_CRYPT)
  15563. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15564. #endif
  15565. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != ASYNC_OP_E) {
  15566. ERROR_OUT(-8057, done);
  15567. }
  15568. #ifndef HAVE_SELFTEST
  15569. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  15570. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_CMP_E &&
  15571. ret != ASYNC_OP_E) {
  15572. ERROR_OUT(-8057, done);
  15573. }
  15574. #endif
  15575. /* Getting here means success - set ret to 0. */
  15576. ret = 0;
  15577. #endif
  15578. done:
  15579. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) && \
  15580. !defined(WC_NO_RNG)
  15581. if (priv)
  15582. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15583. if (pub)
  15584. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15585. if (priv2)
  15586. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15587. if (pub2)
  15588. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15589. if (agree)
  15590. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15591. if (agree2)
  15592. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15593. if (key) {
  15594. wc_FreeDhKey(key);
  15595. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15596. }
  15597. if (key2) {
  15598. wc_FreeDhKey(key2);
  15599. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15600. }
  15601. #else
  15602. wc_FreeDhKey(key);
  15603. wc_FreeDhKey(key2);
  15604. #endif
  15605. return ret;
  15606. }
  15607. #endif /* !(HAVE_FIPS_VERSION == 2 && WOLFSSL_SP_ARMxx_ASM) */
  15608. #endif /* !WC_NO_RNG */
  15609. #endif /* HAVE_FFDHE */
  15610. WOLFSSL_TEST_SUBROUTINE int dh_test(void)
  15611. {
  15612. int ret;
  15613. word32 bytes;
  15614. word32 idx = 0, privSz, pubSz, privSz2, pubSz2;
  15615. #ifndef WC_NO_RNG
  15616. WC_RNG rng;
  15617. int rngInit = 0;
  15618. #endif
  15619. int keyInit = 0, key2Init = 0;
  15620. #define DH_TEST_TMP_SIZE 1024
  15621. #if !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  15622. #define DH_TEST_BUF_SIZE 256
  15623. #else
  15624. #define DH_TEST_BUF_SIZE 512
  15625. #endif
  15626. #ifndef WC_NO_RNG
  15627. word32 agreeSz = DH_TEST_BUF_SIZE;
  15628. word32 agreeSz2 = DH_TEST_BUF_SIZE;
  15629. #endif
  15630. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15631. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15632. DhKey *key2 = (DhKey *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15633. byte *tmp = (byte *)XMALLOC(DH_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15634. #else
  15635. DhKey key[1];
  15636. DhKey key2[1];
  15637. byte tmp[DH_TEST_TMP_SIZE];
  15638. #endif
  15639. #ifndef WC_NO_RNG
  15640. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15641. byte *priv = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15642. byte *pub = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15643. byte *priv2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15644. byte *pub2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15645. byte *agree = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15646. byte *agree2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15647. if (priv == NULL || pub == NULL || priv2 == NULL || pub2 == NULL ||
  15648. agree == NULL || agree2 == NULL) {
  15649. ERROR_OUT(-8100, done);
  15650. }
  15651. #else
  15652. byte priv[DH_TEST_BUF_SIZE];
  15653. byte pub[DH_TEST_BUF_SIZE];
  15654. byte priv2[DH_TEST_BUF_SIZE];
  15655. byte pub2[DH_TEST_BUF_SIZE];
  15656. byte agree[DH_TEST_BUF_SIZE];
  15657. byte agree2[DH_TEST_BUF_SIZE];
  15658. #endif
  15659. #endif /* !WC_NO_RNG */
  15660. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15661. if (key == NULL || key2 == NULL || tmp == NULL) {
  15662. ERROR_OUT(-8100, done);
  15663. }
  15664. #endif
  15665. #ifdef USE_CERT_BUFFERS_1024
  15666. XMEMCPY(tmp, dh_key_der_1024, (size_t)sizeof_dh_key_der_1024);
  15667. bytes = (size_t)sizeof_dh_key_der_1024;
  15668. #elif defined(USE_CERT_BUFFERS_2048)
  15669. XMEMCPY(tmp, dh_key_der_2048, (size_t)sizeof_dh_key_der_2048);
  15670. bytes = (size_t)sizeof_dh_key_der_2048;
  15671. #elif defined(USE_CERT_BUFFERS_3072)
  15672. XMEMCPY(tmp, dh_key_der_3072, (size_t)sizeof_dh_key_der_3072);
  15673. bytes = (size_t)sizeof_dh_key_der_3072;
  15674. #elif defined(USE_CERT_BUFFERS_4096)
  15675. XMEMCPY(tmp, dh_key_der_4096, (size_t)sizeof_dh_key_der_4096);
  15676. bytes = (size_t)sizeof_dh_key_der_4096;
  15677. #elif defined(NO_ASN)
  15678. /* don't use file, no DER parsing */
  15679. #elif !defined(NO_FILESYSTEM)
  15680. {
  15681. XFILE file = XFOPEN(dhParamsFile, "rb");
  15682. if (! file)
  15683. ERROR_OUT(-8101, done);
  15684. bytes = (word32) XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  15685. XFCLOSE(file);
  15686. }
  15687. #else
  15688. /* No DH key to use. */
  15689. ERROR_OUT(-8102, done);
  15690. #endif /* USE_CERT_BUFFERS */
  15691. (void)idx;
  15692. (void)tmp;
  15693. (void)bytes;
  15694. pubSz = DH_TEST_BUF_SIZE;
  15695. pubSz2 = DH_TEST_BUF_SIZE;
  15696. privSz = DH_TEST_BUF_SIZE;
  15697. privSz2 = DH_TEST_BUF_SIZE;
  15698. #ifndef WC_NO_RNG
  15699. XMEMSET(&rng, 0, sizeof(rng));
  15700. #endif
  15701. /* Use API for coverage. */
  15702. ret = wc_InitDhKey(key);
  15703. if (ret != 0) {
  15704. ERROR_OUT(-8103, done);
  15705. }
  15706. wc_FreeDhKey(key);
  15707. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15708. if (ret != 0) {
  15709. ERROR_OUT(-8104, done);
  15710. }
  15711. keyInit = 1;
  15712. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  15713. if (ret != 0) {
  15714. ERROR_OUT(-8105, done);
  15715. }
  15716. key2Init = 1;
  15717. #ifdef NO_ASN
  15718. #ifndef WOLFSSL_SP_MATH
  15719. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  15720. if (ret != 0) {
  15721. ERROR_OUT(-8106, done);
  15722. }
  15723. ret = wc_DhSetKey(key2, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  15724. if (ret != 0) {
  15725. ERROR_OUT(-8107, done);
  15726. }
  15727. #else
  15728. ret = wc_DhSetKey(key, dh2048_p, sizeof(dh2048_p), dh2048_g,
  15729. sizeof(dh2048_g));
  15730. if (ret != 0) {
  15731. ERROR_OUT(-8106, done);
  15732. }
  15733. ret = wc_DhSetKey(key2, dh2048_p, sizeof(dh2048_p), dh2048_g,
  15734. sizeof(dh2048_g));
  15735. if (ret != 0) {
  15736. ERROR_OUT(-8107, done);
  15737. }
  15738. #endif
  15739. #else
  15740. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  15741. if (ret != 0) {
  15742. ERROR_OUT(-8108, done);
  15743. }
  15744. idx = 0;
  15745. ret = wc_DhKeyDecode(tmp, &idx, key2, bytes);
  15746. if (ret != 0) {
  15747. ERROR_OUT(-8109, done);
  15748. }
  15749. #endif
  15750. #ifndef WC_NO_RNG
  15751. #ifndef HAVE_FIPS
  15752. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  15753. #else
  15754. ret = wc_InitRng(&rng);
  15755. #endif
  15756. if (ret != 0) {
  15757. ERROR_OUT(-8110, done);
  15758. }
  15759. rngInit = 1;
  15760. ret = wc_DhGenerateKeyPair(key, &rng, priv, &privSz, pub, &pubSz);
  15761. #if defined(WOLFSSL_ASYNC_CRYPT)
  15762. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15763. #endif
  15764. if (ret != 0) {
  15765. ERROR_OUT(-8111, done);
  15766. }
  15767. ret = wc_DhGenerateKeyPair(key2, &rng, priv2, &privSz2, pub2, &pubSz2);
  15768. #if defined(WOLFSSL_ASYNC_CRYPT)
  15769. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  15770. #endif
  15771. if (ret != 0) {
  15772. ERROR_OUT(-8112, done);
  15773. }
  15774. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  15775. #if defined(WOLFSSL_ASYNC_CRYPT)
  15776. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15777. #endif
  15778. if (ret != 0) {
  15779. ERROR_OUT(-8113, done);
  15780. }
  15781. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  15782. #if defined(WOLFSSL_ASYNC_CRYPT)
  15783. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  15784. #endif
  15785. if (ret != 0) {
  15786. ERROR_OUT(-8114, done);
  15787. }
  15788. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  15789. ERROR_OUT(-8115, done);
  15790. }
  15791. #endif /* !WC_NO_RNG */
  15792. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  15793. if (wc_DhCheckPrivKey(NULL, NULL, 0) != BAD_FUNC_ARG)
  15794. ERROR_OUT(-8116, done);
  15795. if (wc_DhCheckPrivKey(key, priv, privSz) != 0)
  15796. ERROR_OUT(-8117, done);
  15797. if (wc_DhExportParamsRaw(NULL, NULL, NULL, NULL, NULL, NULL, NULL) != BAD_FUNC_ARG)
  15798. ERROR_OUT(-8118, done);
  15799. {
  15800. word32 pSz, qSz, gSz;
  15801. if (wc_DhExportParamsRaw(key, NULL, &pSz, NULL, &qSz, NULL, &gSz) != LENGTH_ONLY_E)
  15802. ERROR_OUT(-8119, done);
  15803. }
  15804. #endif
  15805. /* Test DH key import / export */
  15806. #if defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM) && \
  15807. (!defined(HAVE_FIPS) || \
  15808. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  15809. wc_FreeDhKey(key);
  15810. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15811. if (ret != 0) {
  15812. ERROR_OUT(-8120, done);
  15813. }
  15814. #ifndef NO_ASN
  15815. {
  15816. /* DH Private - Key Export / Import */
  15817. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15818. byte *tmp2;
  15819. #else
  15820. byte tmp2[DH_TEST_TMP_SIZE];
  15821. #endif
  15822. XFILE file = XFOPEN(dhKeyFile, "rb");
  15823. if (!file)
  15824. ERROR_OUT(-8130, done);
  15825. bytes = (word32)XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  15826. XFCLOSE(file);
  15827. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15828. tmp2 = (byte*)XMALLOC(DH_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15829. if (tmp2 == NULL)
  15830. ERROR_OUT(-8131, done);
  15831. #endif
  15832. idx = 0;
  15833. XMEMSET(tmp2, 0, DH_TEST_TMP_SIZE);
  15834. /* Import DH Private key as DER */
  15835. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  15836. if (ret == 0) {
  15837. /* Export as DER */
  15838. idx = DH_TEST_TMP_SIZE;
  15839. ret = wc_DhPrivKeyToDer(key, tmp2, &idx);
  15840. }
  15841. /* Verify export matches original */
  15842. if (ret <= 0 || bytes != idx || XMEMCMP(tmp, tmp2, bytes) != 0) {
  15843. ERROR_OUT(-8132, done);
  15844. }
  15845. /* DH Public Key - Export / Import */
  15846. file = XFOPEN(dhKeyPubFile, "rb");
  15847. if (!file)
  15848. ERROR_OUT(-8133, done);
  15849. bytes = (word32)XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  15850. XFCLOSE(file);
  15851. /* for HAVE_WOLF_BIGINT prevent leak */
  15852. wc_FreeDhKey(key);
  15853. (void)wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15854. idx = 0;
  15855. XMEMSET(tmp2, 0, DH_TEST_TMP_SIZE);
  15856. /* Import DH Public key as DER */
  15857. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  15858. if (ret == 0) {
  15859. /* Export as DER */
  15860. idx = DH_TEST_TMP_SIZE;
  15861. ret = wc_DhPubKeyToDer(key, tmp2, &idx);
  15862. }
  15863. /* Verify export matches original */
  15864. if (ret <= 0 || bytes != idx || XMEMCMP(tmp, tmp2, bytes) != 0) {
  15865. ERROR_OUT(-8134, done);
  15866. }
  15867. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15868. XFREE(tmp2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15869. #endif
  15870. }
  15871. #else
  15872. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  15873. if (ret != 0) {
  15874. ERROR_OUT(-8121, done);
  15875. }
  15876. #endif /* !NO_ASN */
  15877. privSz = DH_TEST_BUF_SIZE;
  15878. pubSz = DH_TEST_BUF_SIZE;
  15879. ret = wc_DhExportKeyPair(key, priv, &privSz, pub, &pubSz);
  15880. if (ret != 0) {
  15881. ERROR_OUT(-8122, done);
  15882. }
  15883. ret = wc_DhImportKeyPair(key2, priv, privSz, pub, pubSz);
  15884. if (ret != 0) {
  15885. ERROR_OUT(-8125, done);
  15886. }
  15887. #endif /* WOLFSSL_DH_EXTRA && !NO_FILESYSTEM && !FIPS <= 2 */
  15888. #ifndef WC_NO_RNG
  15889. ret = dh_generate_test(&rng);
  15890. if (ret != 0)
  15891. ERROR_OUT(-8123, done);
  15892. ret = dh_fips_generate_test(&rng);
  15893. if (ret != 0)
  15894. ERROR_OUT(-8124, done);
  15895. #endif /* !WC_NO_RNG */
  15896. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  15897. ret = dh_test_check_pubvalue();
  15898. if (ret != 0)
  15899. ERROR_OUT(-8125, done);
  15900. #endif
  15901. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  15902. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM)))
  15903. /* RNG with DH and SP_ASM code not supported in the in-lined FIPS ASM code,
  15904. * this will be available for testing in the 140-3 module */
  15905. #ifndef WC_NO_RNG
  15906. /* Specialized code for key gen when using FFDHE-2048, FFDHE-3072 and FFDHE-4096 */
  15907. #ifdef HAVE_FFDHE_2048
  15908. #ifdef HAVE_PUBLIC_FFDHE
  15909. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe2048_Get());
  15910. #else
  15911. ret = dh_ffdhe_test(&rng, WC_FFDHE_2048);
  15912. #endif
  15913. if (ret != 0)
  15914. ERROR_OUT(-8126, done);
  15915. #endif
  15916. #ifdef HAVE_FFDHE_3072
  15917. #ifdef HAVE_PUBLIC_FFDHE
  15918. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe3072_Get());
  15919. #else
  15920. ret = dh_ffdhe_test(&rng, WC_FFDHE_3072);
  15921. #endif
  15922. if (ret != 0)
  15923. ERROR_OUT(-8127, done);
  15924. #endif
  15925. #ifdef HAVE_FFDHE_4096
  15926. #ifdef HAVE_PUBLIC_FFDHE
  15927. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe4096_Get());
  15928. #else
  15929. ret = dh_ffdhe_test(&rng, WC_FFDHE_4096);
  15930. #endif
  15931. if (ret != 0)
  15932. ERROR_OUT(-8128, done);
  15933. #endif
  15934. #endif /* !WC_NO_RNG */
  15935. #endif /* HAVE_FIPS_VERSION == 2 && !WOLFSSL_SP_ARM64_ASM */
  15936. wc_FreeDhKey(key);
  15937. keyInit = 0;
  15938. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  15939. !defined(WOLFSSL_OLD_PRIME_CHECK) && !defined(WC_NO_RNG)
  15940. /* Test Check Key */
  15941. ret = wc_DhSetCheckKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g),
  15942. NULL, 0, 0, &rng);
  15943. if (ret != 0)
  15944. ERROR_OUT(-8129, done);
  15945. keyInit = 1; /* DhSetCheckKey also initializes the key, free it */
  15946. #endif
  15947. done:
  15948. #ifndef WC_NO_RNG
  15949. if (rngInit)
  15950. wc_FreeRng(&rng);
  15951. #endif
  15952. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15953. if (key) {
  15954. if (keyInit)
  15955. wc_FreeDhKey(key);
  15956. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15957. }
  15958. if (key2) {
  15959. if (key2Init)
  15960. wc_FreeDhKey(key2);
  15961. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15962. }
  15963. if (tmp)
  15964. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15965. if (priv)
  15966. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15967. if (pub)
  15968. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15969. if (priv2)
  15970. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15971. if (pub2)
  15972. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15973. if (agree)
  15974. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15975. if (agree2)
  15976. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15977. #else
  15978. if (keyInit)
  15979. wc_FreeDhKey(key);
  15980. if (key2Init)
  15981. wc_FreeDhKey(key2);
  15982. #endif
  15983. (void)privSz;
  15984. (void)pubSz;
  15985. (void)pubSz2;
  15986. (void)privSz2;
  15987. return ret;
  15988. #undef DH_TEST_BUF_SIZE
  15989. #undef DH_TEST_TMP_SIZE
  15990. }
  15991. #endif /* NO_DH */
  15992. #ifndef NO_DSA
  15993. WOLFSSL_TEST_SUBROUTINE int dsa_test(void)
  15994. {
  15995. int ret = 0, answer;
  15996. word32 bytes;
  15997. word32 idx = 0;
  15998. WC_RNG rng;
  15999. wc_Sha sha;
  16000. byte hash[WC_SHA_DIGEST_SIZE];
  16001. byte signature[40];
  16002. #ifdef WOLFSSL_KEY_GEN
  16003. byte* der = 0;
  16004. #endif
  16005. #define DSA_TEST_TMP_SIZE 1024
  16006. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16007. byte *tmp = (byte *)XMALLOC(DSA_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16008. DsaKey *key = (DsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16009. #ifdef WOLFSSL_KEY_GEN
  16010. DsaKey *derIn = (DsaKey *)XMALLOC(sizeof *derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16011. DsaKey *genKey = (DsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16012. #endif
  16013. if ((tmp == NULL) ||
  16014. (key == NULL)
  16015. #ifdef WOLFSSL_KEY_GEN
  16016. || (derIn == NULL)
  16017. || (genKey == NULL)
  16018. #endif
  16019. ) {
  16020. ret = -8216;
  16021. goto out;
  16022. }
  16023. #else
  16024. byte tmp[1024];
  16025. DsaKey key[1];
  16026. #ifdef WOLFSSL_KEY_GEN
  16027. DsaKey derIn[1];
  16028. DsaKey genKey[1];
  16029. #endif
  16030. #endif
  16031. #ifdef USE_CERT_BUFFERS_1024
  16032. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  16033. bytes = sizeof_dsa_key_der_1024;
  16034. #elif defined(USE_CERT_BUFFERS_2048)
  16035. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  16036. bytes = sizeof_dsa_key_der_2048;
  16037. #else
  16038. {
  16039. XFILE file = XFOPEN(dsaKey, "rb");
  16040. if (!file)
  16041. ERROR_OUT(-8200, out);
  16042. bytes = (word32) XFREAD(tmp, 1, DSA_TEST_TMP_SIZE, file);
  16043. XFCLOSE(file);
  16044. }
  16045. #endif /* USE_CERT_BUFFERS */
  16046. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  16047. if (ret != 0)
  16048. ERROR_OUT(-8201, out);
  16049. wc_ShaUpdate(&sha, tmp, bytes);
  16050. wc_ShaFinal(&sha, hash);
  16051. wc_ShaFree(&sha);
  16052. ret = wc_InitDsaKey(key);
  16053. if (ret != 0)
  16054. ERROR_OUT(-8202, out);
  16055. ret = wc_DsaPrivateKeyDecode(tmp, &idx, key, bytes);
  16056. if (ret != 0)
  16057. ERROR_OUT(-8203, out);
  16058. #ifndef HAVE_FIPS
  16059. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  16060. #else
  16061. ret = wc_InitRng(&rng);
  16062. #endif
  16063. if (ret != 0)
  16064. ERROR_OUT(-8204, out);
  16065. ret = wc_DsaSign(hash, signature, key, &rng);
  16066. if (ret != 0)
  16067. ERROR_OUT(-8205, out);
  16068. ret = wc_DsaVerify(hash, signature, key, &answer);
  16069. if (ret != 0)
  16070. ERROR_OUT(-8206, out);
  16071. if (answer != 1)
  16072. ERROR_OUT(-8207, out);
  16073. wc_FreeDsaKey(key);
  16074. #ifdef WOLFSSL_KEY_GEN
  16075. {
  16076. int derSz = 0;
  16077. ret = wc_InitDsaKey(genKey);
  16078. if (ret != 0)
  16079. ERROR_OUT(-8208, out);
  16080. ret = wc_MakeDsaParameters(&rng, 1024, genKey);
  16081. if (ret != 0) {
  16082. wc_FreeDsaKey(genKey);
  16083. ERROR_OUT(-8209, out);
  16084. }
  16085. ret = wc_MakeDsaKey(&rng, genKey);
  16086. if (ret != 0) {
  16087. wc_FreeDsaKey(genKey);
  16088. ERROR_OUT(-8210, out);
  16089. }
  16090. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16091. if (der == NULL) {
  16092. wc_FreeDsaKey(genKey);
  16093. ERROR_OUT(-8211, out);
  16094. }
  16095. derSz = wc_DsaKeyToDer(genKey, der, FOURK_BUF);
  16096. if (derSz < 0) {
  16097. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16098. ERROR_OUT(-8212, out);
  16099. }
  16100. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  16101. DSA_PRIVATEKEY_TYPE, -5814);
  16102. if (ret != 0) {
  16103. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16104. wc_FreeDsaKey(genKey);
  16105. goto out;
  16106. }
  16107. ret = wc_InitDsaKey(derIn);
  16108. if (ret != 0) {
  16109. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16110. wc_FreeDsaKey(genKey);
  16111. ERROR_OUT(-8213, out);
  16112. }
  16113. idx = 0;
  16114. ret = wc_DsaPrivateKeyDecode(der, &idx, derIn, derSz);
  16115. if (ret != 0) {
  16116. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16117. wc_FreeDsaKey(derIn);
  16118. wc_FreeDsaKey(genKey);
  16119. ERROR_OUT(-8214, out);
  16120. }
  16121. }
  16122. #endif /* WOLFSSL_KEY_GEN */
  16123. out:
  16124. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16125. if (key) {
  16126. #endif
  16127. if (wc_InitDsaKey_h(key, NULL) != 0)
  16128. ret = -8215;
  16129. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16130. }
  16131. #endif
  16132. #ifdef WOLFSSL_KEY_GEN
  16133. if (der)
  16134. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16135. #endif
  16136. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16137. if (tmp)
  16138. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16139. if (key)
  16140. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16141. #ifdef WOLFSSL_KEY_GEN
  16142. if (derIn) {
  16143. wc_FreeDsaKey(derIn);
  16144. XFREE(derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16145. }
  16146. if (genKey) {
  16147. wc_FreeDsaKey(genKey);
  16148. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16149. }
  16150. #endif
  16151. #else /* !WOLFSSL_SMALL_STACK || WOLFSSL_NO_MALLOC */
  16152. #ifdef WOLFSSL_KEY_GEN
  16153. wc_FreeDsaKey(derIn);
  16154. wc_FreeDsaKey(genKey);
  16155. #endif
  16156. #endif
  16157. wc_FreeRng(&rng);
  16158. return ret;
  16159. }
  16160. #endif /* NO_DSA */
  16161. #ifdef WOLFCRYPT_HAVE_SRP
  16162. static int generate_random_salt(byte *buf, word32 size)
  16163. {
  16164. int ret = -8220;
  16165. WC_RNG rng;
  16166. if(NULL == buf || !size)
  16167. return -8221;
  16168. if (buf && size && wc_InitRng_ex(&rng, HEAP_HINT, devId) == 0) {
  16169. ret = wc_RNG_GenerateBlock(&rng, (byte *)buf, size);
  16170. wc_FreeRng(&rng);
  16171. }
  16172. return ret;
  16173. }
  16174. static int srp_test_digest(SrpType dgstType)
  16175. {
  16176. int r;
  16177. byte clientPubKey[192]; /* A */
  16178. byte serverPubKey[192]; /* B */
  16179. word32 clientPubKeySz = 192;
  16180. word32 serverPubKeySz = 192;
  16181. byte username[] = "user";
  16182. word32 usernameSz = 4;
  16183. byte password[] = "password";
  16184. word32 passwordSz = 8;
  16185. WOLFSSL_SMALL_STACK_STATIC const byte N[] = {
  16186. 0xfc, 0x58, 0x7a, 0x8a, 0x70, 0xfb, 0x5a, 0x9a,
  16187. 0x5d, 0x39, 0x48, 0xbf, 0x1c, 0x46, 0xd8, 0x3b,
  16188. 0x7a, 0xe9, 0x1f, 0x85, 0x36, 0x18, 0xc4, 0x35,
  16189. 0x3f, 0xf8, 0x8a, 0x8f, 0x8c, 0x10, 0x2e, 0x01,
  16190. 0x58, 0x1d, 0x41, 0xcb, 0xc4, 0x47, 0xa8, 0xaf,
  16191. 0x9a, 0x6f, 0x58, 0x14, 0xa4, 0x68, 0xf0, 0x9c,
  16192. 0xa6, 0xe7, 0xbf, 0x0d, 0xe9, 0x62, 0x0b, 0xd7,
  16193. 0x26, 0x46, 0x5b, 0x27, 0xcb, 0x4c, 0xf9, 0x7e,
  16194. 0x1e, 0x8b, 0xe6, 0xdd, 0x29, 0xb7, 0xb7, 0x15,
  16195. 0x2e, 0xcf, 0x23, 0xa6, 0x4b, 0x97, 0x9f, 0x89,
  16196. 0xd4, 0x86, 0xc4, 0x90, 0x63, 0x92, 0xf4, 0x30,
  16197. 0x26, 0x69, 0x48, 0x9d, 0x7a, 0x4f, 0xad, 0xb5,
  16198. 0x6a, 0x51, 0xad, 0xeb, 0xf9, 0x90, 0x31, 0x77,
  16199. 0x53, 0x30, 0x2a, 0x85, 0xf7, 0x11, 0x21, 0x0c,
  16200. 0xb8, 0x4b, 0x56, 0x03, 0x5e, 0xbb, 0x25, 0x33,
  16201. 0x7c, 0xd9, 0x5a, 0xd1, 0x5c, 0xb2, 0xd4, 0x53,
  16202. 0xc5, 0x16, 0x68, 0xf0, 0xdf, 0x48, 0x55, 0x3e,
  16203. 0xd4, 0x59, 0x87, 0x64, 0x59, 0xaa, 0x39, 0x01,
  16204. 0x45, 0x89, 0x9c, 0x72, 0xff, 0xdd, 0x8f, 0x6d,
  16205. 0xa0, 0x42, 0xbc, 0x6f, 0x6e, 0x62, 0x18, 0x2d,
  16206. 0x50, 0xe8, 0x18, 0x97, 0x87, 0xfc, 0xef, 0x1f,
  16207. 0xf5, 0x53, 0x68, 0xe8, 0x49, 0xd1, 0xa2, 0xe8,
  16208. 0xb9, 0x26, 0x03, 0xba, 0xb5, 0x58, 0x6f, 0x6c,
  16209. 0x8b, 0x08, 0xa1, 0x7b, 0x6f, 0x42, 0xc9, 0x53
  16210. };
  16211. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  16212. 0x02
  16213. };
  16214. byte salt[10];
  16215. byte verifier[192];
  16216. word32 v_size = sizeof(verifier);
  16217. word32 clientProofSz = SRP_MAX_DIGEST_SIZE;
  16218. word32 serverProofSz = SRP_MAX_DIGEST_SIZE;
  16219. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16220. Srp *cli = (Srp *)XMALLOC(sizeof *cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16221. Srp *srv = (Srp *)XMALLOC(sizeof *srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16222. byte *clientProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT,
  16223. DYNAMIC_TYPE_TMP_BUFFER); /* M1 */
  16224. byte *serverProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT,
  16225. DYNAMIC_TYPE_TMP_BUFFER); /* M2 */
  16226. if ((cli == NULL) ||
  16227. (srv == NULL) ||
  16228. (clientProof == NULL) ||
  16229. (serverProof == NULL)) {
  16230. r = -8222;
  16231. goto out;
  16232. }
  16233. #else
  16234. Srp cli[1], srv[1];
  16235. byte clientProof[SRP_MAX_DIGEST_SIZE]; /* M1 */
  16236. byte serverProof[SRP_MAX_DIGEST_SIZE]; /* M2 */
  16237. #endif
  16238. /* set as 0's so if second init on srv not called SrpTerm is not on
  16239. * garbage values */
  16240. XMEMSET(srv, 0, sizeof *srv);
  16241. XMEMSET(cli, 0, sizeof *cli);
  16242. /* generating random salt */
  16243. r = generate_random_salt(salt, sizeof(salt));
  16244. /* client knows username and password. */
  16245. /* server knows N, g, salt and verifier. */
  16246. if (!r) r = wc_SrpInit_ex(cli, dgstType, SRP_CLIENT_SIDE, HEAP_HINT, devId);
  16247. if (!r) r = wc_SrpSetUsername(cli, username, usernameSz);
  16248. /* loading N, g and salt in advance to generate the verifier. */
  16249. if (!r) r = wc_SrpSetParams(cli, N, sizeof(N),
  16250. g, sizeof(g),
  16251. salt, sizeof(salt));
  16252. if (!r) r = wc_SrpSetPassword(cli, password, passwordSz);
  16253. if (!r) r = wc_SrpGetVerifier(cli, verifier, &v_size);
  16254. /* client sends username to server */
  16255. if (!r) r = wc_SrpInit_ex(srv, dgstType, SRP_SERVER_SIDE, HEAP_HINT, devId);
  16256. if (!r) r = wc_SrpSetUsername(srv, username, usernameSz);
  16257. if (!r) r = wc_SrpSetParams(srv, N, sizeof(N),
  16258. g, sizeof(g),
  16259. salt, sizeof(salt));
  16260. if (!r) r = wc_SrpSetVerifier(srv, verifier, v_size);
  16261. if (!r) r = wc_SrpGetPublic(srv, serverPubKey, &serverPubKeySz);
  16262. /* server sends N, g, salt and B to client */
  16263. if (!r) r = wc_SrpGetPublic(cli, clientPubKey, &clientPubKeySz);
  16264. if (!r) r = wc_SrpComputeKey(cli, clientPubKey, clientPubKeySz,
  16265. serverPubKey, serverPubKeySz);
  16266. if (!r) r = wc_SrpGetProof(cli, clientProof, &clientProofSz);
  16267. /* client sends A and M1 to server */
  16268. if (!r) r = wc_SrpComputeKey(srv, clientPubKey, clientPubKeySz,
  16269. serverPubKey, serverPubKeySz);
  16270. if (!r) r = wc_SrpVerifyPeersProof(srv, clientProof, clientProofSz);
  16271. if (!r) r = wc_SrpGetProof(srv, serverProof, &serverProofSz);
  16272. /* server sends M2 to client */
  16273. if (!r) r = wc_SrpVerifyPeersProof(cli, serverProof, serverProofSz);
  16274. wc_SrpTerm(cli);
  16275. wc_SrpTerm(srv);
  16276. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16277. out:
  16278. if (cli)
  16279. XFREE(cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16280. if (srv)
  16281. XFREE(srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16282. if (clientProof)
  16283. XFREE(clientProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16284. if (serverProof)
  16285. XFREE(serverProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16286. #endif
  16287. return r;
  16288. }
  16289. WOLFSSL_TEST_SUBROUTINE int srp_test(void)
  16290. {
  16291. int ret;
  16292. #ifndef NO_SHA
  16293. ret = srp_test_digest(SRP_TYPE_SHA);
  16294. if (ret != 0)
  16295. return ret;
  16296. #endif
  16297. #ifndef NO_SHA256
  16298. ret = srp_test_digest(SRP_TYPE_SHA256);
  16299. if (ret != 0)
  16300. return ret;
  16301. #endif
  16302. #ifdef WOLFSSL_SHA384
  16303. ret = srp_test_digest(SRP_TYPE_SHA384);
  16304. if (ret != 0)
  16305. return ret;
  16306. #endif
  16307. #ifdef WOLFSSL_SHA512
  16308. ret = srp_test_digest(SRP_TYPE_SHA512);
  16309. if (ret != 0)
  16310. return ret;
  16311. #endif
  16312. return ret;
  16313. }
  16314. #endif /* WOLFCRYPT_HAVE_SRP */
  16315. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  16316. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  16317. static int openssl_aes_test(void)
  16318. {
  16319. #ifdef HAVE_AES_CBC
  16320. #ifdef WOLFSSL_AES_128
  16321. {
  16322. /* EVP_CipherUpdate test */
  16323. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  16324. {
  16325. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16326. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  16327. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  16328. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  16329. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  16330. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  16331. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  16332. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  16333. };
  16334. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16335. "0123456789abcdef "; /* align */
  16336. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  16337. "1234567890abcdef "; /* align */
  16338. byte cipher[AES_BLOCK_SIZE * 4];
  16339. byte plain [AES_BLOCK_SIZE * 4];
  16340. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16341. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  16342. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  16343. #else
  16344. EVP_CIPHER_CTX en[1];
  16345. EVP_CIPHER_CTX de[1];
  16346. #endif
  16347. int outlen ;
  16348. int total = 0;
  16349. int i;
  16350. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16351. if ((en == NULL) || (de == NULL))
  16352. return MEMORY_E;
  16353. #endif
  16354. EVP_CIPHER_CTX_init(en);
  16355. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16356. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16357. return -8400;
  16358. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  16359. (byte*)cbcPlain, 9) == 0)
  16360. return -8401;
  16361. if (outlen != 0)
  16362. return -8402;
  16363. total += outlen;
  16364. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  16365. (byte*)&cbcPlain[9] , 9) == 0)
  16366. return -8403;
  16367. if (outlen != 16)
  16368. return -8404;
  16369. total += outlen;
  16370. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  16371. return -8405;
  16372. if (outlen != 16)
  16373. return -8406;
  16374. total += outlen;
  16375. if (total != 32)
  16376. return 3408;
  16377. total = 0;
  16378. EVP_CIPHER_CTX_init(de);
  16379. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16380. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16381. return -8407;
  16382. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  16383. return -8408;
  16384. if (outlen != 0)
  16385. return -8409;
  16386. total += outlen;
  16387. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  16388. (byte*)&cipher[6], 12) == 0)
  16389. return -8410;
  16390. if (outlen != 0)
  16391. total += outlen;
  16392. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  16393. (byte*)&cipher[6+12], 14) == 0)
  16394. return -8411;
  16395. if (outlen != 16)
  16396. return -8412;
  16397. total += outlen;
  16398. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  16399. return -8413;
  16400. if (outlen != 2)
  16401. return -8414;
  16402. total += outlen;
  16403. if (total != 18)
  16404. return 3427;
  16405. if (XMEMCMP(plain, cbcPlain, 18))
  16406. return -8415;
  16407. /* test with encrypting/decrypting more than 16 bytes at once */
  16408. total = 0;
  16409. EVP_CIPHER_CTX_init(en);
  16410. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16411. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16412. return -8416;
  16413. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  16414. (byte*)cbcPlain, 17) == 0)
  16415. return -8417;
  16416. if (outlen != 16)
  16417. return -8418;
  16418. total += outlen;
  16419. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  16420. (byte*)&cbcPlain[17] , 1) == 0)
  16421. return -8419;
  16422. if (outlen != 0)
  16423. return -8420;
  16424. total += outlen;
  16425. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  16426. return -8421;
  16427. if (outlen != 16)
  16428. return -8422;
  16429. total += outlen;
  16430. if (total != 32)
  16431. return -8423;
  16432. total = 0;
  16433. EVP_CIPHER_CTX_init(de);
  16434. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16435. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16436. return -8424;
  16437. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 17) == 0)
  16438. return -8425;
  16439. if (outlen != 16)
  16440. return -8426;
  16441. total += outlen;
  16442. /* final call on non block size should fail */
  16443. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  16444. return -8427;
  16445. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  16446. (byte*)&cipher[17], 1) == 0)
  16447. return -8428;
  16448. if (outlen != 0)
  16449. total += outlen;
  16450. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  16451. (byte*)&cipher[17+1], 14) == 0)
  16452. return -8429;
  16453. if (outlen != 0)
  16454. return -8430;
  16455. total += outlen;
  16456. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  16457. return -8431;
  16458. if (outlen != 2)
  16459. return -8432;
  16460. total += outlen;
  16461. if (total != 18)
  16462. return -8433;
  16463. if (XMEMCMP(plain, cbcPlain, 18))
  16464. return -8434;
  16465. /* test byte by byte decrypt */
  16466. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  16467. plain[i] = i;
  16468. }
  16469. total = 0;
  16470. EVP_CIPHER_CTX_init(en);
  16471. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16472. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16473. return -8435;
  16474. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  16475. (byte*)plain, AES_BLOCK_SIZE * 3) == 0)
  16476. return -8436;
  16477. if (outlen != AES_BLOCK_SIZE * 3)
  16478. return -8437;
  16479. total += outlen;
  16480. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  16481. return -8438;
  16482. if (outlen != AES_BLOCK_SIZE)
  16483. return -8439;
  16484. total += outlen;
  16485. if (total != sizeof(plain))
  16486. return -8440;
  16487. total = 0;
  16488. EVP_CIPHER_CTX_init(de);
  16489. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16490. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16491. return -8441;
  16492. for (i = 0; i < AES_BLOCK_SIZE * 4; i++) {
  16493. if (EVP_CipherUpdate(de, (byte*)plain + total, &outlen,
  16494. (byte*)cipher + i, 1) == 0)
  16495. return -8442;
  16496. if (outlen > 0) {
  16497. int j;
  16498. total += outlen;
  16499. for (j = 0; j < total; j++) {
  16500. if (plain[j] != j) {
  16501. return -8443;
  16502. }
  16503. }
  16504. }
  16505. }
  16506. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  16507. return -8444;
  16508. total += outlen;
  16509. if (total != AES_BLOCK_SIZE * 3) {
  16510. return -8445;
  16511. }
  16512. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  16513. if (plain[i] != i) {
  16514. return -8446;
  16515. }
  16516. }
  16517. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16518. wolfSSL_EVP_CIPHER_CTX_free(en);
  16519. wolfSSL_EVP_CIPHER_CTX_free(de);
  16520. #endif
  16521. }
  16522. /* set buffers to be exact size to catch potential over read/write */
  16523. {
  16524. /* EVP_CipherUpdate test */
  16525. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  16526. {
  16527. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16528. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  16529. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  16530. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  16531. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  16532. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  16533. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  16534. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  16535. };
  16536. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16537. "0123456789abcdef "; /* align */
  16538. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  16539. "1234567890abcdef "; /* align */
  16540. #define EVP_TEST_BUF_SZ 18
  16541. #define EVP_TEST_BUF_PAD 32
  16542. byte cipher[EVP_TEST_BUF_SZ];
  16543. byte plain [EVP_TEST_BUF_SZ];
  16544. byte padded[EVP_TEST_BUF_PAD];
  16545. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16546. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  16547. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  16548. #else
  16549. EVP_CIPHER_CTX en[1];
  16550. EVP_CIPHER_CTX de[1];
  16551. #endif
  16552. int outlen ;
  16553. int total = 0;
  16554. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16555. if ((en == NULL) || (de == NULL))
  16556. return MEMORY_E;
  16557. #endif
  16558. EVP_CIPHER_CTX_init(en);
  16559. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16560. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16561. return -8447;
  16562. if (EVP_CIPHER_CTX_set_padding(en, 0) != 1)
  16563. return -8448;
  16564. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  16565. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  16566. return -8449;
  16567. if (outlen != 16)
  16568. return -8450;
  16569. total += outlen;
  16570. /* should fail here */
  16571. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) != 0)
  16572. return -8451;
  16573. /* turn padding back on and do successful encrypt */
  16574. total = 0;
  16575. EVP_CIPHER_CTX_init(en);
  16576. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16577. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16578. return -8452;
  16579. if (EVP_CIPHER_CTX_set_padding(en, 1) != 1)
  16580. return -8453;
  16581. if (EVP_CipherUpdate(en, (byte*)padded, &outlen,
  16582. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  16583. return -8454;
  16584. if (outlen != 16)
  16585. return -8455;
  16586. total += outlen;
  16587. if (EVP_CipherFinal(en, (byte*)&padded[total], &outlen) == 0)
  16588. return -8456;
  16589. total += outlen;
  16590. if (total != 32)
  16591. return -8457;
  16592. XMEMCPY(cipher, padded, EVP_TEST_BUF_SZ);
  16593. /* test out of bounds read on buffers w/o padding during decryption */
  16594. total = 0;
  16595. EVP_CIPHER_CTX_init(de);
  16596. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16597. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16598. return -8458;
  16599. if (EVP_CIPHER_CTX_set_padding(de, 0) != 1)
  16600. return -8459;
  16601. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher,
  16602. EVP_TEST_BUF_SZ) == 0)
  16603. return -8460;
  16604. if (outlen != 16)
  16605. return -8461;
  16606. total += outlen;
  16607. /* should fail since not using padding */
  16608. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  16609. return -8462;
  16610. total = 0;
  16611. EVP_CIPHER_CTX_init(de);
  16612. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16613. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16614. return -8463;
  16615. if (EVP_CIPHER_CTX_set_padding(de, 1) != 1)
  16616. return -8464;
  16617. if (EVP_CipherUpdate(de, (byte*)padded, &outlen, (byte*)padded,
  16618. EVP_TEST_BUF_PAD) == 0)
  16619. return -8465;
  16620. if (outlen != 16)
  16621. return -8466;
  16622. total += outlen;
  16623. if (EVP_CipherFinal(de, (byte*)&padded[total], &outlen) == 0)
  16624. return -8467;
  16625. if (XMEMCMP(padded, cbcPlain, EVP_TEST_BUF_SZ))
  16626. return -8468;
  16627. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16628. wolfSSL_EVP_CIPHER_CTX_free(en);
  16629. wolfSSL_EVP_CIPHER_CTX_free(de);
  16630. #endif
  16631. }
  16632. { /* evp_cipher test: EVP_aes_128_cbc */
  16633. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16634. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  16635. #else
  16636. EVP_CIPHER_CTX ctx[1];
  16637. #endif
  16638. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  16639. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16640. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16641. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16642. };
  16643. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  16644. {
  16645. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  16646. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  16647. };
  16648. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16649. "0123456789abcdef "; /* align */
  16650. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  16651. "1234567890abcdef "; /* align */
  16652. byte cipher[AES_BLOCK_SIZE * 4];
  16653. byte plain [AES_BLOCK_SIZE * 4];
  16654. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16655. if (ctx == NULL)
  16656. return MEMORY_E;
  16657. #endif
  16658. EVP_CIPHER_CTX_init(ctx);
  16659. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1) == 0)
  16660. return -8469;
  16661. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  16662. return -8470;
  16663. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  16664. return -8471;
  16665. EVP_CIPHER_CTX_init(ctx);
  16666. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0) == 0)
  16667. return -8472;
  16668. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  16669. return -8473;
  16670. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  16671. return -8474;
  16672. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16673. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  16674. #endif
  16675. } /* end evp_cipher test: EVP_aes_128_cbc*/
  16676. #endif /* WOLFSSL_AES_128 */
  16677. #endif /* HAVE_AES_CBC */
  16678. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  16679. { /* evp_cipher test: EVP_aes_256_ecb*/
  16680. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16681. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  16682. #else
  16683. EVP_CIPHER_CTX ctx[1];
  16684. #endif
  16685. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  16686. {
  16687. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16688. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16689. };
  16690. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  16691. {
  16692. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  16693. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  16694. };
  16695. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16696. {
  16697. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  16698. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  16699. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  16700. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  16701. };
  16702. byte cipher[AES_BLOCK_SIZE * 4];
  16703. byte plain [AES_BLOCK_SIZE * 4];
  16704. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16705. if (ctx == NULL)
  16706. return MEMORY_E;
  16707. #endif
  16708. EVP_CIPHER_CTX_init(ctx);
  16709. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1) == 0)
  16710. return -8475;
  16711. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  16712. return -8476;
  16713. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  16714. return -8477;
  16715. EVP_CIPHER_CTX_init(ctx);
  16716. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0) == 0)
  16717. return -8478;
  16718. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  16719. return -8479;
  16720. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  16721. return -8480;
  16722. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16723. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  16724. #endif
  16725. } /* end evp_cipher test */
  16726. #endif /* HAVE_AES_ECB && WOLFSSL_AES_256 */
  16727. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  16728. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  16729. {
  16730. /* Test: AES_encrypt/decrypt/set Key */
  16731. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16732. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  16733. #ifdef HAVE_AES_DECRYPT
  16734. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  16735. #endif
  16736. #else
  16737. AES_KEY enc[1];
  16738. #ifdef HAVE_AES_DECRYPT
  16739. AES_KEY dec[1];
  16740. #endif
  16741. #endif
  16742. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  16743. {
  16744. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16745. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16746. };
  16747. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  16748. {
  16749. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  16750. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  16751. };
  16752. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16753. {
  16754. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  16755. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  16756. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  16757. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  16758. };
  16759. byte plain[sizeof(msg)];
  16760. byte cipher[sizeof(msg)];
  16761. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16762. if (enc == NULL)
  16763. return MEMORY_E;
  16764. #ifdef HAVE_AES_DECRYPT
  16765. if (dec == NULL)
  16766. return MEMORY_E;
  16767. #endif
  16768. #endif
  16769. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  16770. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  16771. AES_encrypt(msg, cipher, enc);
  16772. #ifdef HAVE_AES_DECRYPT
  16773. AES_decrypt(cipher, plain, dec);
  16774. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  16775. return -8481;
  16776. #endif /* HAVE_AES_DECRYPT */
  16777. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  16778. return -8482;
  16779. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16780. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  16781. #ifdef HAVE_AES_DECRYPT
  16782. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  16783. #endif
  16784. #endif
  16785. }
  16786. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  16787. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  16788. #ifdef WOLFSSL_AES_COUNTER
  16789. {
  16790. byte plainBuff [64];
  16791. byte cipherBuff[64];
  16792. #ifdef WOLFSSL_AES_128
  16793. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  16794. {
  16795. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  16796. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  16797. };
  16798. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  16799. {
  16800. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  16801. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  16802. };
  16803. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  16804. {
  16805. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16806. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  16807. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  16808. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  16809. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  16810. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  16811. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  16812. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  16813. };
  16814. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  16815. {
  16816. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  16817. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  16818. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  16819. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  16820. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  16821. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  16822. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  16823. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  16824. };
  16825. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  16826. {
  16827. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  16828. 0xc2
  16829. };
  16830. #endif
  16831. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  16832. * NIST Special Publication 800-38A */
  16833. #ifdef WOLFSSL_AES_192
  16834. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  16835. {
  16836. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  16837. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  16838. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  16839. };
  16840. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  16841. {
  16842. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  16843. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  16844. };
  16845. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  16846. {
  16847. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16848. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16849. };
  16850. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  16851. {
  16852. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  16853. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  16854. };
  16855. #endif /* WOLFSSL_AES_192 */
  16856. #ifdef WOLFSSL_AES_256
  16857. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  16858. * NIST Special Publication 800-38A */
  16859. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  16860. {
  16861. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  16862. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  16863. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  16864. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  16865. };
  16866. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  16867. {
  16868. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  16869. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  16870. };
  16871. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  16872. {
  16873. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16874. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16875. };
  16876. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  16877. {
  16878. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  16879. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  16880. };
  16881. #endif /* WOLFSSL_AES_256 */
  16882. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16883. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  16884. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  16885. #else
  16886. EVP_CIPHER_CTX en[1];
  16887. EVP_CIPHER_CTX de[1];
  16888. #endif
  16889. #ifdef WOLFSSL_AES_128
  16890. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16891. EVP_CIPHER_CTX *p_en;
  16892. EVP_CIPHER_CTX *p_de;
  16893. #endif
  16894. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16895. if ((en == NULL) || (de == NULL))
  16896. return MEMORY_E;
  16897. #endif
  16898. EVP_CIPHER_CTX_init(en);
  16899. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  16900. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16901. return -8483;
  16902. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  16903. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16904. return -8484;
  16905. EVP_CIPHER_CTX_init(de);
  16906. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  16907. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16908. return -8485;
  16909. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  16910. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16911. return -8486;
  16912. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  16913. return -8487;
  16914. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  16915. return -8488;
  16916. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16917. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  16918. if (p_en == NULL)
  16919. return -8489;
  16920. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  16921. if (p_de == NULL)
  16922. return -8490;
  16923. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  16924. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16925. return -8491;
  16926. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  16927. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16928. return -8492;
  16929. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  16930. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16931. return -8493;
  16932. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  16933. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16934. return -8494;
  16935. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  16936. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  16937. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  16938. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  16939. return -8495;
  16940. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  16941. return -8496;
  16942. EVP_CIPHER_CTX_init(en);
  16943. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  16944. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16945. return -8497;
  16946. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  16947. return -8498;
  16948. EVP_CIPHER_CTX_init(de);
  16949. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  16950. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16951. return -8499;
  16952. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  16953. return -8500;
  16954. if (XMEMCMP(plainBuff, ctrPlain, 9))
  16955. return -8501;
  16956. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  16957. return -8502;
  16958. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  16959. return -8503;
  16960. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  16961. return -8504;
  16962. if (XMEMCMP(plainBuff, ctrPlain, 9))
  16963. return -8505;
  16964. if (XMEMCMP(cipherBuff, oddCipher, 9))
  16965. return -8506;
  16966. #endif /* WOLFSSL_AES_128 */
  16967. #ifdef WOLFSSL_AES_192
  16968. EVP_CIPHER_CTX_init(en);
  16969. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  16970. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  16971. return -8507;
  16972. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  16973. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16974. return -8508;
  16975. EVP_CIPHER_CTX_init(de);
  16976. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  16977. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  16978. return -8509;
  16979. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  16980. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  16981. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16982. return -8510;
  16983. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  16984. return -8511;
  16985. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  16986. return -8512;
  16987. #endif /* WOLFSSL_AES_192 */
  16988. #ifdef WOLFSSL_AES_256
  16989. EVP_CIPHER_CTX_init(en);
  16990. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  16991. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  16992. return -8513;
  16993. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  16994. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16995. return -8514;
  16996. EVP_CIPHER_CTX_init(de);
  16997. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  16998. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  16999. return -8515;
  17000. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  17001. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  17002. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  17003. return -8516;
  17004. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  17005. return -8517;
  17006. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  17007. return -8518;
  17008. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17009. wolfSSL_EVP_CIPHER_CTX_free(en);
  17010. wolfSSL_EVP_CIPHER_CTX_free(de);
  17011. #endif
  17012. #endif /* WOLFSSL_AES_256 */
  17013. }
  17014. #endif /* HAVE_AES_COUNTER */
  17015. #if defined(WOLFSSL_AES_CFB) && defined(WOLFSSL_AES_128)
  17016. {
  17017. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17018. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  17019. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  17020. #else
  17021. AES_KEY enc[1];
  17022. AES_KEY dec[1];
  17023. #endif
  17024. WOLFSSL_SMALL_STACK_STATIC const byte setIv[] = {
  17025. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  17026. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  17027. };
  17028. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  17029. {
  17030. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  17031. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  17032. };
  17033. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  17034. {
  17035. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  17036. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  17037. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  17038. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b
  17039. };
  17040. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  17041. {
  17042. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17043. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  17044. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  17045. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51
  17046. };
  17047. byte cipher[AES_BLOCK_SIZE * 2];
  17048. byte iv[AES_BLOCK_SIZE]; /* iv buffer is updeated by API */
  17049. int num = 0;
  17050. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17051. if ((enc == NULL) || (dec == NULL))
  17052. return MEMORY_E;
  17053. #endif
  17054. XMEMCPY(iv, setIv, sizeof(setIv));
  17055. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, enc);
  17056. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, dec);
  17057. wolfSSL_AES_cfb128_encrypt(msg, cipher, AES_BLOCK_SIZE - 1, enc, iv,
  17058. &num, AES_ENCRYPT);
  17059. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE - 1))
  17060. return -8519;
  17061. if (num != 15) /* should have used 15 of the 16 bytes */
  17062. return -8520;
  17063. wolfSSL_AES_cfb128_encrypt(msg + AES_BLOCK_SIZE - 1,
  17064. cipher + AES_BLOCK_SIZE - 1, AES_BLOCK_SIZE + 1, enc, iv,
  17065. &num, AES_ENCRYPT);
  17066. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  17067. return -8521;
  17068. if (num != 0)
  17069. return -8522;
  17070. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17071. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  17072. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  17073. #endif
  17074. }
  17075. #endif /* WOLFSSL_AES_CFB && WOLFSSL_AES_128 */
  17076. return 0;
  17077. }
  17078. #endif /* !defined(NO_AES) && !defined(WOLFCRYPT_ONLY) */
  17079. WOLFSSL_TEST_SUBROUTINE int openssl_test(void)
  17080. {
  17081. int ret;
  17082. EVP_MD_CTX md_ctx;
  17083. testVector a, b, c, d, e, f;
  17084. byte hash[WC_SHA256_DIGEST_SIZE*2]; /* max size */
  17085. a.inLen = 0;
  17086. b.inLen = c.inLen = d.inLen = e.inLen = f.inLen = a.inLen;
  17087. (void)a;
  17088. (void)b;
  17089. (void)c;
  17090. (void)d;
  17091. (void)e;
  17092. (void)f;
  17093. /* test malloc / free , 10 is an arbitrary amount of memory chosen */
  17094. {
  17095. byte* p;
  17096. p = (byte*)CRYPTO_malloc(10, "", 0);
  17097. if (p == NULL) {
  17098. return -8600;
  17099. }
  17100. XMEMSET(p, 0, 10);
  17101. CRYPTO_free(p, "", 0);
  17102. }
  17103. #ifndef NO_MD5
  17104. a.input = "1234567890123456789012345678901234567890123456789012345678"
  17105. "9012345678901234567890";
  17106. a.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  17107. "\x7a";
  17108. a.inLen = XSTRLEN(a.input);
  17109. a.outLen = WC_MD5_DIGEST_SIZE;
  17110. EVP_MD_CTX_init(&md_ctx);
  17111. ret = EVP_DigestInit(&md_ctx, EVP_md5());
  17112. if (ret == WOLFSSL_SUCCESS) {
  17113. ret = EVP_DigestUpdate(&md_ctx, a.input, (unsigned long)a.inLen);
  17114. }
  17115. if (ret == WOLFSSL_SUCCESS) {
  17116. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17117. }
  17118. EVP_MD_CTX_cleanup(&md_ctx);
  17119. if (ret != WOLFSSL_SUCCESS)
  17120. return -18601;
  17121. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0)
  17122. return -8601;
  17123. #endif /* NO_MD5 */
  17124. #ifndef NO_SHA
  17125. b.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  17126. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  17127. "aaaaaaaaaa";
  17128. b.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  17129. "\x53\x99\x5E\x26\xA0";
  17130. b.inLen = XSTRLEN(b.input);
  17131. b.outLen = WC_SHA_DIGEST_SIZE;
  17132. EVP_MD_CTX_init(&md_ctx);
  17133. ret = EVP_DigestInit(&md_ctx, EVP_sha1());
  17134. if (ret == WOLFSSL_SUCCESS) {
  17135. ret = EVP_DigestUpdate(&md_ctx, b.input, (unsigned long)b.inLen);
  17136. if (ret == WOLFSSL_SUCCESS)
  17137. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17138. }
  17139. EVP_MD_CTX_cleanup(&md_ctx);
  17140. if (ret != WOLFSSL_SUCCESS)
  17141. return -18602;
  17142. if (XMEMCMP(hash, b.output, WC_SHA_DIGEST_SIZE) != 0)
  17143. return -8602;
  17144. #endif /* NO_SHA */
  17145. #ifdef WOLFSSL_SHA224
  17146. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17147. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17148. e.output = "\xc9\x7c\xa9\xa5\x59\x85\x0c\xe9\x7a\x04\xa9\x6d\xef\x6d\x99"
  17149. "\xa9\xe0\xe0\xe2\xab\x14\xe6\xb8\xdf\x26\x5f\xc0\xb3";
  17150. e.inLen = XSTRLEN(e.input);
  17151. e.outLen = WC_SHA224_DIGEST_SIZE;
  17152. EVP_MD_CTX_init(&md_ctx);
  17153. ret = EVP_DigestInit(&md_ctx, EVP_sha224());
  17154. if (ret == WOLFSSL_SUCCESS) {
  17155. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  17156. if (ret == WOLFSSL_SUCCESS)
  17157. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17158. }
  17159. EVP_MD_CTX_cleanup(&md_ctx);
  17160. if (ret != WOLFSSL_SUCCESS ||
  17161. XMEMCMP(hash, e.output, WC_SHA224_DIGEST_SIZE) != 0) {
  17162. return -8603;
  17163. }
  17164. #endif /* WOLFSSL_SHA224 */
  17165. #ifndef NO_SHA256
  17166. d.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  17167. d.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  17168. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  17169. "\x06\xC1";
  17170. d.inLen = XSTRLEN(d.input);
  17171. d.outLen = WC_SHA256_DIGEST_SIZE;
  17172. EVP_MD_CTX_init(&md_ctx);
  17173. ret = EVP_DigestInit(&md_ctx, EVP_sha256());
  17174. if (ret == WOLFSSL_SUCCESS) {
  17175. ret = EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen);
  17176. if (ret == WOLFSSL_SUCCESS)
  17177. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17178. }
  17179. EVP_MD_CTX_cleanup(&md_ctx);
  17180. if (ret != WOLFSSL_SUCCESS ||
  17181. XMEMCMP(hash, d.output, WC_SHA256_DIGEST_SIZE) != 0) {
  17182. return -8604;
  17183. }
  17184. #endif /* !NO_SHA256 */
  17185. #ifdef WOLFSSL_SHA384
  17186. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17187. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17188. e.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  17189. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  17190. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  17191. "\x74\x60\x39";
  17192. e.inLen = XSTRLEN(e.input);
  17193. e.outLen = WC_SHA384_DIGEST_SIZE;
  17194. EVP_MD_CTX_init(&md_ctx);
  17195. ret = EVP_DigestInit(&md_ctx, EVP_sha384());
  17196. if (ret == WOLFSSL_SUCCESS) {
  17197. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  17198. if (ret == WOLFSSL_SUCCESS)
  17199. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17200. }
  17201. EVP_MD_CTX_cleanup(&md_ctx);
  17202. if (ret != WOLFSSL_SUCCESS ||
  17203. XMEMCMP(hash, e.output, WC_SHA384_DIGEST_SIZE) != 0) {
  17204. return -8605;
  17205. }
  17206. #endif /* WOLFSSL_SHA384 */
  17207. #ifdef WOLFSSL_SHA512
  17208. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17209. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17210. f.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  17211. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  17212. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  17213. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  17214. "\x87\x4b\xe9\x09";
  17215. f.inLen = XSTRLEN(f.input);
  17216. f.outLen = WC_SHA512_DIGEST_SIZE;
  17217. EVP_MD_CTX_init(&md_ctx);
  17218. ret = EVP_DigestInit(&md_ctx, EVP_sha512());
  17219. if (ret == WOLFSSL_SUCCESS) {
  17220. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  17221. if (ret == WOLFSSL_SUCCESS)
  17222. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17223. }
  17224. EVP_MD_CTX_cleanup(&md_ctx);
  17225. if (ret != WOLFSSL_SUCCESS ||
  17226. XMEMCMP(hash, f.output, WC_SHA512_DIGEST_SIZE) != 0) {
  17227. return -8606;
  17228. }
  17229. #endif /* WOLFSSL_SHA512 */
  17230. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  17231. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  17232. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17233. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17234. f.output = "\x23\xfe\xc5\xbb\x94\xd6\x0b\x23\x30\x81\x92\x64\x0b\x0c\x45"
  17235. "\x33\x35\xd6\x64\x73\x4f\xe4\x0e\x72\x68\x67\x4a\xf9";
  17236. f.inLen = XSTRLEN(f.input);
  17237. f.outLen = WC_SHA512_224_DIGEST_SIZE;
  17238. EVP_MD_CTX_init(&md_ctx);
  17239. ret = EVP_DigestInit(&md_ctx, EVP_sha512_224());
  17240. if (ret == WOLFSSL_SUCCESS) {
  17241. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  17242. if (ret == WOLFSSL_SUCCESS)
  17243. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17244. }
  17245. EVP_MD_CTX_cleanup(&md_ctx);
  17246. if (ret != WOLFSSL_SUCCESS ||
  17247. XMEMCMP(hash, f.output, WC_SHA512_224_DIGEST_SIZE) != 0) {
  17248. return -8722;
  17249. }
  17250. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  17251. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  17252. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  17253. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  17254. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17255. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17256. f.output = "\x39\x28\xe1\x84\xfb\x86\x90\xf8\x40\xda\x39\x88\x12\x1d\x31"
  17257. "\xbe\x65\xcb\x9d\x3e\xf8\x3e\xe6\x14\x6f\xea\xc8\x61\xe1\x9b"
  17258. "\x56\x3a";
  17259. f.inLen = XSTRLEN(f.input);
  17260. f.outLen = WC_SHA512_256_DIGEST_SIZE;
  17261. EVP_MD_CTX_init(&md_ctx);
  17262. ret = EVP_DigestInit(&md_ctx, EVP_sha512_256());
  17263. if (ret == WOLFSSL_SUCCESS) {
  17264. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  17265. if (ret == WOLFSSL_SUCCESS)
  17266. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17267. }
  17268. EVP_MD_CTX_cleanup(&md_ctx);
  17269. if (ret != WOLFSSL_SUCCESS ||
  17270. XMEMCMP(hash, f.output, WC_SHA512_256_DIGEST_SIZE) != 0) {
  17271. return -8723;
  17272. }
  17273. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  17274. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  17275. #ifdef WOLFSSL_SHA3
  17276. #ifndef WOLFSSL_NOSHA3_224
  17277. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17278. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17279. e.output = "\x54\x3e\x68\x68\xe1\x66\x6c\x1a\x64\x36\x30\xdf\x77\x36\x7a"
  17280. "\xe5\xa6\x2a\x85\x07\x0a\x51\xc1\x4c\xbf\x66\x5c\xbc";
  17281. e.inLen = XSTRLEN(e.input);
  17282. e.outLen = WC_SHA3_224_DIGEST_SIZE;
  17283. EVP_MD_CTX_init(&md_ctx);
  17284. ret = EVP_DigestInit(&md_ctx, EVP_sha3_224());
  17285. if (ret == WOLFSSL_SUCCESS) {
  17286. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  17287. if (ret == WOLFSSL_SUCCESS)
  17288. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17289. }
  17290. EVP_MD_CTX_cleanup(&md_ctx);
  17291. if (ret != WOLFSSL_SUCCESS ||
  17292. XMEMCMP(hash, e.output, WC_SHA3_224_DIGEST_SIZE) != 0) {
  17293. return -8607;
  17294. }
  17295. #endif /* WOLFSSL_NOSHA3_224 */
  17296. #ifndef WOLFSSL_NOSHA3_256
  17297. d.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17298. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17299. d.output = "\x91\x6f\x60\x61\xfe\x87\x97\x41\xca\x64\x69\xb4\x39\x71\xdf"
  17300. "\xdb\x28\xb1\xa3\x2d\xc3\x6c\xb3\x25\x4e\x81\x2b\xe2\x7a\xad"
  17301. "\x1d\x18";
  17302. d.inLen = XSTRLEN(d.input);
  17303. d.outLen = WC_SHA3_256_DIGEST_SIZE;
  17304. EVP_MD_CTX_init(&md_ctx);
  17305. ret = EVP_DigestInit(&md_ctx, EVP_sha3_256());
  17306. if (ret == WOLFSSL_SUCCESS) {
  17307. ret = EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen);
  17308. if (ret == WOLFSSL_SUCCESS)
  17309. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17310. }
  17311. EVP_MD_CTX_cleanup(&md_ctx);
  17312. if (ret != WOLFSSL_SUCCESS ||
  17313. XMEMCMP(hash, d.output, WC_SHA3_256_DIGEST_SIZE) != 0) {
  17314. return -8608;
  17315. }
  17316. #endif /* WOLFSSL_NOSHA3_256 */
  17317. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17318. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17319. e.output = "\x79\x40\x7d\x3b\x59\x16\xb5\x9c\x3e\x30\xb0\x98\x22\x97\x47"
  17320. "\x91\xc3\x13\xfb\x9e\xcc\x84\x9e\x40\x6f\x23\x59\x2d\x04\xf6"
  17321. "\x25\xdc\x8c\x70\x9b\x98\xb4\x3b\x38\x52\xb3\x37\x21\x61\x79"
  17322. "\xaa\x7f\xc7";
  17323. e.inLen = XSTRLEN(e.input);
  17324. e.outLen = WC_SHA3_384_DIGEST_SIZE;
  17325. EVP_MD_CTX_init(&md_ctx);
  17326. ret = EVP_DigestInit(&md_ctx, EVP_sha3_384());
  17327. if (ret == WOLFSSL_SUCCESS) {
  17328. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  17329. if (ret == WOLFSSL_SUCCESS)
  17330. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17331. }
  17332. EVP_MD_CTX_cleanup(&md_ctx);
  17333. if (ret != WOLFSSL_SUCCESS ||
  17334. XMEMCMP(hash, e.output, WC_SHA3_384_DIGEST_SIZE) != 0) {
  17335. return -8609;
  17336. }
  17337. #ifndef WOLFSSL_NOSHA3_512
  17338. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17339. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17340. f.output = "\xaf\xeb\xb2\xef\x54\x2e\x65\x79\xc5\x0c\xad\x06\xd2\xe5\x78"
  17341. "\xf9\xf8\xdd\x68\x81\xd7\xdc\x82\x4d\x26\x36\x0f\xee\xbf\x18"
  17342. "\xa4\xfa\x73\xe3\x26\x11\x22\x94\x8e\xfc\xfd\x49\x2e\x74\xe8"
  17343. "\x2e\x21\x89\xed\x0f\xb4\x40\xd1\x87\xf3\x82\x27\x0c\xb4\x55"
  17344. "\xf2\x1d\xd1\x85";
  17345. f.inLen = XSTRLEN(f.input);
  17346. f.outLen = WC_SHA3_512_DIGEST_SIZE;
  17347. EVP_MD_CTX_init(&md_ctx);
  17348. ret = EVP_DigestInit(&md_ctx, EVP_sha3_512());
  17349. if (ret == WOLFSSL_SUCCESS) {
  17350. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  17351. if (ret == WOLFSSL_SUCCESS)
  17352. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17353. }
  17354. EVP_MD_CTX_cleanup(&md_ctx);
  17355. if (ret != WOLFSSL_SUCCESS ||
  17356. XMEMCMP(hash, f.output, WC_SHA3_512_DIGEST_SIZE) != 0) {
  17357. return -8610;
  17358. }
  17359. #endif /* WOLFSSL_NOSHA3_512 */
  17360. #endif /* WOLFSSL_SHA3 */
  17361. #ifndef WC_NO_RNG
  17362. if (RAND_bytes(hash, sizeof(hash)) != WOLFSSL_SUCCESS)
  17363. return -8611;
  17364. #endif
  17365. #ifndef NO_MD5
  17366. c.input = "what do ya want for nothing?";
  17367. c.output = "\x55\x78\xe8\x48\x4b\xcc\x93\x80\x93\xec\x53\xaf\x22\xd6\x14"
  17368. "\x76";
  17369. c.inLen = XSTRLEN(c.input);
  17370. c.outLen = WC_MD5_DIGEST_SIZE;
  17371. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)
  17372. /* Expect failure with MD5 + HMAC when using FIPS 140-3. */
  17373. if (HMAC(EVP_md5(), "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen,
  17374. hash, 0) != NULL)
  17375. #else
  17376. if (HMAC(EVP_md5(), "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen,
  17377. hash, 0) == NULL ||
  17378. XMEMCMP(hash, c.output, WC_MD5_DIGEST_SIZE) != 0)
  17379. #endif
  17380. {
  17381. return -8612;
  17382. }
  17383. #endif /* NO_MD5 */
  17384. #ifndef NO_DES3
  17385. { /* des test */
  17386. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  17387. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  17388. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  17389. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  17390. };
  17391. byte plain[24];
  17392. byte cipher[24];
  17393. const_DES_cblock key = {
  17394. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  17395. };
  17396. DES_cblock iv = {
  17397. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  17398. };
  17399. DES_key_schedule sched;
  17400. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17401. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  17402. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  17403. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  17404. };
  17405. DES_key_sched(&key, &sched);
  17406. DES_cbc_encrypt(vector, cipher, sizeof(vector), &sched, &iv, DES_ENCRYPT);
  17407. DES_cbc_encrypt(cipher, plain, sizeof(vector), &sched, &iv, DES_DECRYPT);
  17408. if (XMEMCMP(plain, vector, sizeof(vector)) != 0)
  17409. return -8613;
  17410. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  17411. return -8614;
  17412. /* test changing iv */
  17413. DES_ncbc_encrypt(vector, cipher, 8, &sched, &iv, DES_ENCRYPT);
  17414. DES_ncbc_encrypt(vector + 8, cipher + 8, 16, &sched, &iv, DES_ENCRYPT);
  17415. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  17416. return -8615;
  17417. } /* end des test */
  17418. #endif /* NO_DES3 */
  17419. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  17420. if ((ret = openssl_aes_test()) != 0) {
  17421. return ret;
  17422. }
  17423. #if defined(WOLFSSL_AES_128) && defined(HAVE_AES_CBC)
  17424. { /* evp_cipher test: EVP_aes_128_cbc */
  17425. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17426. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  17427. #else
  17428. EVP_CIPHER_CTX ctx[1];
  17429. #endif
  17430. int idx, cipherSz, plainSz;
  17431. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  17432. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  17433. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  17434. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  17435. };
  17436. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17437. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  17438. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  17439. 0x3b,0x5d,0x41,0x97,0x94,0x25,0xa4,0xb4,
  17440. 0xae,0x7b,0x34,0xd0,0x3f,0x0c,0xbc,0x06
  17441. };
  17442. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  17443. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  17444. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  17445. 0x7d,0x37,0x7b,0x0b,0x44,0xaa,0xb5,0xf0,
  17446. 0x5f,0x34,0xb4,0xde,0xb5,0xbd,0x2a,0xbb
  17447. };
  17448. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  17449. "0123456789abcdef "; /* align */
  17450. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  17451. "1234567890abcdef "; /* align */
  17452. byte cipher[AES_BLOCK_SIZE * 4];
  17453. byte plain [AES_BLOCK_SIZE * 4];
  17454. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17455. if (ctx == NULL)
  17456. return MEMORY_E;
  17457. #endif
  17458. cipherSz = 0;
  17459. EVP_CIPHER_CTX_init(ctx);
  17460. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  17461. if (ret == WOLFSSL_SUCCESS) {
  17462. ret = EVP_CipherUpdate(ctx, cipher, &idx, (byte*)msg, sizeof(msg));
  17463. if (ret == WOLFSSL_SUCCESS)
  17464. cipherSz += idx;
  17465. }
  17466. if (ret == WOLFSSL_SUCCESS) {
  17467. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  17468. if (ret == WOLFSSL_SUCCESS)
  17469. cipherSz += idx;
  17470. }
  17471. EVP_CIPHER_CTX_cleanup(ctx);
  17472. if (ret != WOLFSSL_SUCCESS)
  17473. return -8617;
  17474. if (cipherSz != (int)sizeof(verify) || XMEMCMP(cipher, verify, cipherSz))
  17475. return -8618;
  17476. /* check partial decrypt (not enough padding for full block) */
  17477. plainSz = 0;
  17478. EVP_CIPHER_CTX_init(ctx);
  17479. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  17480. if (ret == WOLFSSL_SUCCESS) {
  17481. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, 1);
  17482. if (ret == WOLFSSL_SUCCESS)
  17483. plainSz += idx;
  17484. }
  17485. if (ret == WOLFSSL_SUCCESS) {
  17486. /* this test should fail... not enough padding for full block */
  17487. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  17488. if (plainSz == 0 && ret != WOLFSSL_SUCCESS)
  17489. ret = WOLFSSL_SUCCESS;
  17490. else
  17491. ret = -8619;
  17492. }
  17493. else
  17494. ret = -8620;
  17495. EVP_CIPHER_CTX_cleanup(ctx);
  17496. if (ret != WOLFSSL_SUCCESS)
  17497. return ret;
  17498. plainSz = 0;
  17499. EVP_CIPHER_CTX_init(ctx);
  17500. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  17501. if (ret == WOLFSSL_SUCCESS) {
  17502. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, cipherSz);
  17503. if (ret == WOLFSSL_SUCCESS)
  17504. plainSz += idx;
  17505. }
  17506. if (ret == WOLFSSL_SUCCESS) {
  17507. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  17508. if (ret == WOLFSSL_SUCCESS)
  17509. plainSz += idx;
  17510. }
  17511. EVP_CIPHER_CTX_cleanup(ctx);
  17512. if (ret != WOLFSSL_SUCCESS)
  17513. return -8621;
  17514. if (plainSz != (int)sizeof(msg) || XMEMCMP(plain, msg, sizeof(msg)))
  17515. return -8622;
  17516. cipherSz = 0;
  17517. EVP_CIPHER_CTX_init(ctx);
  17518. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  17519. if (ret == WOLFSSL_SUCCESS) {
  17520. ret = EVP_CipherUpdate(ctx, cipher, &idx, msg, AES_BLOCK_SIZE);
  17521. if (ret == WOLFSSL_SUCCESS)
  17522. cipherSz += idx;
  17523. }
  17524. if (ret == WOLFSSL_SUCCESS) {
  17525. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  17526. if (ret == WOLFSSL_SUCCESS)
  17527. cipherSz += idx;
  17528. }
  17529. EVP_CIPHER_CTX_cleanup(ctx);
  17530. if (ret != WOLFSSL_SUCCESS)
  17531. return -8623;
  17532. if (cipherSz != (int)sizeof(verify2) || XMEMCMP(cipher, verify2, cipherSz))
  17533. return -8624;
  17534. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17535. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  17536. #endif
  17537. } /* end evp_cipher test: EVP_aes_128_cbc*/
  17538. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  17539. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  17540. { /* evp_cipher test: EVP_aes_256_ecb*/
  17541. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17542. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  17543. #else
  17544. EVP_CIPHER_CTX ctx[1];
  17545. #endif
  17546. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  17547. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17548. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  17549. };
  17550. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17551. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  17552. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  17553. };
  17554. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  17555. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  17556. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  17557. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  17558. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  17559. };
  17560. byte cipher[AES_BLOCK_SIZE * 4];
  17561. byte plain [AES_BLOCK_SIZE * 4];
  17562. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17563. if (ctx == NULL)
  17564. return MEMORY_E;
  17565. #endif
  17566. EVP_CIPHER_CTX_init(ctx);
  17567. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1);
  17568. if (ret == WOLFSSL_SUCCESS)
  17569. ret = EVP_Cipher(ctx, cipher, (byte*)msg, 16);
  17570. EVP_CIPHER_CTX_cleanup(ctx);
  17571. if (ret != 16)
  17572. return -8625;
  17573. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  17574. return -8626;
  17575. EVP_CIPHER_CTX_init(ctx);
  17576. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0);
  17577. if (ret == WOLFSSL_SUCCESS)
  17578. ret = EVP_Cipher(ctx, plain, cipher, 16);
  17579. EVP_CIPHER_CTX_cleanup(ctx);
  17580. if (ret != 16)
  17581. return -8627;
  17582. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  17583. return -8628;
  17584. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17585. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  17586. #endif
  17587. } /* end evp_cipher test */
  17588. #endif /* HAVE_AES_ECB && WOLFSSL_AES_128 */
  17589. #define OPENSSL_TEST_ERROR (-10000)
  17590. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  17591. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  17592. {
  17593. /* Test: AES_encrypt/decrypt/set Key */
  17594. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17595. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  17596. #ifdef HAVE_AES_DECRYPT
  17597. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  17598. #endif
  17599. #else
  17600. AES_KEY enc[1];
  17601. #ifdef HAVE_AES_DECRYPT
  17602. AES_KEY dec[1];
  17603. #endif
  17604. #endif
  17605. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  17606. {
  17607. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17608. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  17609. };
  17610. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  17611. {
  17612. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  17613. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  17614. };
  17615. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  17616. {
  17617. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  17618. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  17619. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  17620. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  17621. };
  17622. byte plain[sizeof(msg)];
  17623. byte cipher[sizeof(msg)];
  17624. printf("openSSL extra test\n") ;
  17625. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17626. if (enc == NULL)
  17627. return MEMORY_E;
  17628. #ifdef HAVE_AES_DECRYPT
  17629. if (dec == NULL)
  17630. return MEMORY_E;
  17631. #endif
  17632. #endif
  17633. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  17634. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  17635. AES_encrypt(msg, cipher, enc);
  17636. #ifdef HAVE_AES_DECRYPT
  17637. AES_decrypt(cipher, plain, dec);
  17638. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  17639. return OPENSSL_TEST_ERROR-60;
  17640. #endif /* HAVE_AES_DECRYPT */
  17641. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  17642. return OPENSSL_TEST_ERROR-61;
  17643. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17644. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  17645. #ifdef HAVE_AES_DECRYPT
  17646. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  17647. #endif
  17648. #endif
  17649. }
  17650. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  17651. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  17652. #ifdef WOLFSSL_AES_COUNTER
  17653. {
  17654. byte plainBuff [64];
  17655. byte cipherBuff[64];
  17656. #ifdef WOLFSSL_AES_128
  17657. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  17658. {
  17659. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  17660. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  17661. };
  17662. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  17663. {
  17664. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  17665. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  17666. };
  17667. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  17668. {
  17669. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17670. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  17671. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  17672. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  17673. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  17674. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  17675. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  17676. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  17677. };
  17678. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  17679. {
  17680. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  17681. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  17682. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  17683. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  17684. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  17685. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  17686. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  17687. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  17688. };
  17689. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  17690. {
  17691. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  17692. 0xc2
  17693. };
  17694. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17695. EVP_CIPHER_CTX *p_en;
  17696. EVP_CIPHER_CTX *p_de;
  17697. #endif
  17698. #endif /* WOLFSSL_AES_128 */
  17699. #ifdef WOLFSSL_AES_192
  17700. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  17701. * NIST Special Publication 800-38A */
  17702. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  17703. {
  17704. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  17705. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  17706. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  17707. };
  17708. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  17709. {
  17710. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  17711. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  17712. };
  17713. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  17714. {
  17715. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17716. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  17717. };
  17718. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  17719. {
  17720. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  17721. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  17722. };
  17723. #endif /* WOLFSSL_AES_192 */
  17724. #ifdef WOLFSSL_AES_256
  17725. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  17726. * NIST Special Publication 800-38A */
  17727. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  17728. {
  17729. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  17730. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  17731. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  17732. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  17733. };
  17734. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  17735. {
  17736. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  17737. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  17738. };
  17739. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  17740. {
  17741. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17742. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  17743. };
  17744. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  17745. {
  17746. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  17747. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  17748. };
  17749. #endif /* WOLFSSL_AES_256 */
  17750. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17751. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  17752. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  17753. if ((en == NULL) || (de == NULL))
  17754. return MEMORY_E;
  17755. #else
  17756. EVP_CIPHER_CTX en[1];
  17757. EVP_CIPHER_CTX de[1];
  17758. #endif
  17759. #ifdef WOLFSSL_AES_128
  17760. EVP_CIPHER_CTX_init(en);
  17761. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  17762. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17763. return -8629;
  17764. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  17765. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  17766. return -8630;
  17767. EVP_CIPHER_CTX_init(de);
  17768. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  17769. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17770. return -8631;
  17771. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  17772. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  17773. return -8632;
  17774. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  17775. return -8633;
  17776. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  17777. return -8634;
  17778. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17779. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  17780. if (p_en == NULL)
  17781. return -8635;
  17782. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  17783. if (p_de == NULL)
  17784. return -8636;
  17785. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  17786. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17787. return -8637;
  17788. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  17789. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  17790. return -8638;
  17791. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  17792. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17793. return -8639;
  17794. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  17795. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  17796. return -8640;
  17797. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  17798. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  17799. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  17800. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  17801. return -8641;
  17802. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  17803. return -8642;
  17804. EVP_CIPHER_CTX_init(en);
  17805. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  17806. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17807. return -8643;
  17808. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  17809. return -8644;
  17810. EVP_CIPHER_CTX_init(de);
  17811. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  17812. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17813. return -8645;
  17814. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  17815. return -8646;
  17816. if (XMEMCMP(plainBuff, ctrPlain, 9))
  17817. return -8647;
  17818. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  17819. return -8648;
  17820. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  17821. return -8649;
  17822. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  17823. return -8650;
  17824. if (XMEMCMP(plainBuff, ctrPlain, 9))
  17825. return -8651;
  17826. if (XMEMCMP(cipherBuff, oddCipher, 9))
  17827. return -8652;
  17828. #endif /* WOLFSSL_AES_128 */
  17829. #ifdef WOLFSSL_AES_192
  17830. EVP_CIPHER_CTX_init(en);
  17831. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  17832. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  17833. return -8653;
  17834. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  17835. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  17836. return -8654;
  17837. EVP_CIPHER_CTX_init(de);
  17838. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  17839. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  17840. return -8655;
  17841. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  17842. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  17843. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  17844. return -8656;
  17845. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  17846. return -8657;
  17847. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  17848. return -8658;
  17849. #endif /* WOLFSSL_AES_192 */
  17850. #ifdef WOLFSSL_AES_256
  17851. EVP_CIPHER_CTX_init(en);
  17852. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  17853. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  17854. return -8659;
  17855. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  17856. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  17857. return -8660;
  17858. EVP_CIPHER_CTX_init(de);
  17859. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  17860. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  17861. return -8661;
  17862. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  17863. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  17864. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  17865. return -8662;
  17866. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  17867. return -8663;
  17868. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  17869. return -8664;
  17870. #endif /* WOLFSSL_AES_256 */
  17871. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17872. wolfSSL_EVP_CIPHER_CTX_free(en);
  17873. wolfSSL_EVP_CIPHER_CTX_free(de);
  17874. #endif
  17875. }
  17876. #endif /* HAVE_AES_COUNTER */
  17877. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  17878. {
  17879. /* EVP_CipherUpdate test */
  17880. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  17881. {
  17882. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17883. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  17884. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  17885. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  17886. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  17887. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  17888. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  17889. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  17890. };
  17891. byte key[] = "0123456789abcdef "; /* align */
  17892. byte iv[] = "1234567890abcdef "; /* align */
  17893. byte cipher[AES_BLOCK_SIZE * 4];
  17894. byte plain [AES_BLOCK_SIZE * 4];
  17895. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17896. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  17897. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  17898. #else
  17899. EVP_CIPHER_CTX en[1];
  17900. EVP_CIPHER_CTX de[1];
  17901. #endif
  17902. int outlen ;
  17903. int total = 0;
  17904. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17905. if ((en == NULL) || (de == NULL))
  17906. return MEMORY_E;
  17907. #endif
  17908. EVP_CIPHER_CTX_init(en);
  17909. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  17910. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  17911. return -8665;
  17912. /* openSSL compatibility, if(inlen == 0)return 1; */
  17913. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  17914. (byte*)cbcPlain, 0) != 1)
  17915. return -8666;
  17916. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  17917. return -8724;
  17918. EVP_CIPHER_CTX_init(en);
  17919. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  17920. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  17921. return -8667;
  17922. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  17923. (byte*)cbcPlain, 9) == 0)
  17924. return -8668;
  17925. if(outlen != 0)
  17926. return -8669;
  17927. total += outlen;
  17928. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  17929. (byte*)&cbcPlain[9] , 9) == 0)
  17930. return -8670;
  17931. if(outlen != 16)
  17932. return -8671;
  17933. total += outlen;
  17934. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  17935. return -8672;
  17936. if(outlen != 16)
  17937. return -8673;
  17938. total += outlen;
  17939. if(total != 32)
  17940. return -8674;
  17941. total = 0;
  17942. EVP_CIPHER_CTX_init(de);
  17943. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  17944. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  17945. return -8675;
  17946. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  17947. return -8676;
  17948. if(outlen != 0)
  17949. return -8677;
  17950. total += outlen;
  17951. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  17952. (byte*)&cipher[6], 12) == 0)
  17953. return -8678;
  17954. if(outlen != 0)
  17955. total += outlen;
  17956. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  17957. (byte*)&cipher[6+12], 14) == 0)
  17958. return -8679;
  17959. if(outlen != 16)
  17960. return -8680;
  17961. total += outlen;
  17962. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  17963. return -8681;
  17964. if(outlen != 2)
  17965. return -8682;
  17966. total += outlen;
  17967. if(total != 18)
  17968. return -8683;
  17969. if (XMEMCMP(plain, cbcPlain, 18))
  17970. return -8684;
  17971. total = 0;
  17972. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  17973. return -8725;
  17974. EVP_CIPHER_CTX_init(en);
  17975. if (EVP_EncryptInit(en, EVP_aes_128_cbc(),
  17976. (unsigned char*)key, (unsigned char*)iv) == 0)
  17977. return -8685;
  17978. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen, (byte*)cbcPlain, 9) == 0)
  17979. return -8686;
  17980. if(outlen != 0)
  17981. return -8687;
  17982. total += outlen;
  17983. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen, (byte*)&cbcPlain[9] , 9) == 0)
  17984. return -8688;
  17985. if(outlen != 16)
  17986. return -8689;
  17987. total += outlen;
  17988. if (EVP_EncryptFinal(en, (byte*)&cipher[total], &outlen) == 0)
  17989. return -8690;
  17990. if(outlen != 16)
  17991. return -8691;
  17992. total += outlen;
  17993. if(total != 32)
  17994. return 3438;
  17995. total = 0;
  17996. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  17997. return -8726;
  17998. EVP_CIPHER_CTX_init(de);
  17999. if (EVP_DecryptInit(de, EVP_aes_128_cbc(),
  18000. (unsigned char*)key, (unsigned char*)iv) == 0)
  18001. return -8692;
  18002. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  18003. return -8693;
  18004. if(outlen != 0)
  18005. return -8694;
  18006. total += outlen;
  18007. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6], 12) == 0)
  18008. return -8695;
  18009. if(outlen != 0)
  18010. total += outlen;
  18011. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6+12], 14) == 0)
  18012. return -8696;
  18013. if(outlen != 16)
  18014. return -8697;
  18015. total += outlen;
  18016. if (EVP_DecryptFinal(de, (byte*)&plain[total], &outlen) == 0)
  18017. return -8698;
  18018. if(outlen != 2)
  18019. return -8699;
  18020. total += outlen;
  18021. if(total != 18)
  18022. return 3447;
  18023. if (XMEMCMP(plain, cbcPlain, 18))
  18024. return -8700;
  18025. if (EVP_CIPHER_key_length(NULL) != 0)
  18026. return -8701;
  18027. if (EVP_CIPHER_key_length(EVP_aes_128_cbc()) != 16)
  18028. return -8702;
  18029. if (EVP_CIPHER_CTX_mode(NULL) != 0)
  18030. return -8703;
  18031. if (EVP_CIPHER_CTX_mode(en) != (en->flags & WOLFSSL_EVP_CIPH_MODE))
  18032. return -8704;
  18033. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  18034. return -8727;
  18035. EVP_CIPHER_CTX_init(en);
  18036. if (EVP_CipherInit_ex(en, EVP_aes_128_cbc(), NULL,
  18037. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  18038. return -8705;
  18039. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  18040. return -8728;
  18041. EVP_CIPHER_CTX_init(en);
  18042. if (EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  18043. (unsigned char*)key, (unsigned char*)iv) == 0)
  18044. return -8706;
  18045. if (wolfSSL_EVP_EncryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  18046. return -8707;
  18047. if (wolfSSL_EVP_EncryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  18048. return -8708;
  18049. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  18050. return -8729;
  18051. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  18052. return -8730;
  18053. EVP_CIPHER_CTX_init(de);
  18054. if (EVP_DecryptInit_ex(de, EVP_aes_128_cbc(), NULL,
  18055. (unsigned char*)key, (unsigned char*)iv) == 0)
  18056. return -8709;
  18057. if (wolfSSL_EVP_DecryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  18058. return -8710;
  18059. if (wolfSSL_EVP_DecryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  18060. return -8711;
  18061. if (EVP_CIPHER_CTX_block_size(NULL) != BAD_FUNC_ARG)
  18062. return -8712;
  18063. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  18064. return -8731;
  18065. EVP_CIPHER_CTX_init(en);
  18066. EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  18067. (unsigned char*)key, (unsigned char*)iv);
  18068. if (EVP_CIPHER_CTX_block_size(en) != en->block_size)
  18069. return -8713;
  18070. if (EVP_CIPHER_block_size(NULL) != BAD_FUNC_ARG)
  18071. return -8714;
  18072. if (EVP_CIPHER_block_size(EVP_aes_128_cbc()) != AES_BLOCK_SIZE)
  18073. return -8715;
  18074. if (WOLFSSL_EVP_CIPHER_mode(NULL) != 0)
  18075. return -8716;
  18076. if (EVP_CIPHER_flags(EVP_aes_128_cbc()) != WOLFSSL_EVP_CIPH_CBC_MODE)
  18077. return -8717;
  18078. EVP_CIPHER_CTX_clear_flags(en, 0xFFFFFFFF);
  18079. EVP_CIPHER_CTX_set_flags(en, 42);
  18080. if (en->flags != 42)
  18081. return -8718;
  18082. if (EVP_CIPHER_CTX_set_padding(NULL, 0) != BAD_FUNC_ARG)
  18083. return -8719;
  18084. if (EVP_CIPHER_CTX_set_padding(en, 0) != WOLFSSL_SUCCESS)
  18085. return -8720;
  18086. if (EVP_CIPHER_CTX_set_padding(en, 1) != WOLFSSL_SUCCESS)
  18087. return -8721;
  18088. if (wolfSSL_EVP_CIPHER_CTX_cleanup(en) != WOLFSSL_SUCCESS)
  18089. return -8732;
  18090. if (wolfSSL_EVP_CIPHER_CTX_cleanup(de) != WOLFSSL_SUCCESS)
  18091. return -8733;
  18092. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18093. wolfSSL_EVP_CIPHER_CTX_free(en);
  18094. wolfSSL_EVP_CIPHER_CTX_free(de);
  18095. #endif
  18096. }
  18097. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  18098. #endif /* ifndef NO_AES */
  18099. return 0;
  18100. }
  18101. WOLFSSL_TEST_SUBROUTINE int openSSL_evpMD_test(void)
  18102. {
  18103. int ret = 0;
  18104. #if !defined(NO_SHA256) && !defined(NO_SHA)
  18105. WOLFSSL_EVP_MD_CTX* ctx;
  18106. WOLFSSL_EVP_MD_CTX* ctx2;
  18107. ctx = EVP_MD_CTX_create();
  18108. ctx2 = EVP_MD_CTX_create();
  18109. ret = EVP_DigestInit(ctx, EVP_sha256());
  18110. if (ret != SSL_SUCCESS) {
  18111. ret = -8800;
  18112. goto openSSL_evpMD_test_done;
  18113. }
  18114. ret = EVP_MD_CTX_copy(ctx2, ctx);
  18115. if (ret != SSL_SUCCESS) {
  18116. ret = -8801;
  18117. goto openSSL_evpMD_test_done;
  18118. }
  18119. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  18120. ret = -8802;
  18121. goto openSSL_evpMD_test_done;
  18122. }
  18123. ret = EVP_DigestInit(ctx, EVP_sha1());
  18124. if (ret != SSL_SUCCESS) {
  18125. ret = -8803;
  18126. goto openSSL_evpMD_test_done;
  18127. }
  18128. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  18129. ret = -8804;
  18130. goto openSSL_evpMD_test_done;
  18131. }
  18132. ret = EVP_MD_CTX_copy_ex(ctx2, ctx);
  18133. if (ret != SSL_SUCCESS) {
  18134. ret = -8805;
  18135. goto openSSL_evpMD_test_done;
  18136. }
  18137. if (EVP_MD_type(EVP_sha256()) == EVP_MD_CTX_type(ctx2)) {
  18138. ret = -8806;
  18139. goto openSSL_evpMD_test_done;
  18140. }
  18141. if (EVP_MD_type(EVP_sha1()) != EVP_MD_CTX_type(ctx2)) {
  18142. ret = -8807;
  18143. goto openSSL_evpMD_test_done;
  18144. }
  18145. if (EVP_DigestInit_ex(ctx, EVP_sha1(), NULL) != SSL_SUCCESS) {
  18146. ret = -8808;
  18147. goto openSSL_evpMD_test_done;
  18148. }
  18149. if (EVP_add_digest(NULL) != 0) {
  18150. ret = -8809;
  18151. goto openSSL_evpMD_test_done;
  18152. }
  18153. if (wolfSSL_EVP_add_cipher(NULL) != 0) {
  18154. ret = -8810;
  18155. goto openSSL_evpMD_test_done;
  18156. }
  18157. ret = 0; /* got to success state without jumping to end with a fail */
  18158. openSSL_evpMD_test_done:
  18159. EVP_MD_CTX_destroy(ctx);
  18160. EVP_MD_CTX_destroy(ctx2);
  18161. #endif /* NO_SHA256 */
  18162. return ret;
  18163. }
  18164. #ifdef DEBUG_SIGN
  18165. static void show(const char *title, const char *p, unsigned int s) {
  18166. char* i;
  18167. printf("%s: ", title);
  18168. for (i = p;
  18169. i < p + s;
  18170. printf("%c", *i), i++);
  18171. printf("\n");
  18172. }
  18173. #else
  18174. #define show(a,b,c)
  18175. #endif
  18176. #define FOURK_BUFF 4096
  18177. #define ERR_BASE_PKEY (-5000)
  18178. WOLFSSL_TEST_SUBROUTINE int openssl_pkey0_test(void)
  18179. {
  18180. int ret = 0;
  18181. #if !defined(NO_RSA) && !defined(HAVE_USER_RSA) && !defined(NO_SHA)
  18182. byte* prvTmp;
  18183. byte* pubTmp;
  18184. int prvBytes;
  18185. int pubBytes;
  18186. RSA *prvRsa = NULL;
  18187. RSA *pubRsa = NULL;
  18188. EVP_PKEY *prvPkey = NULL;
  18189. EVP_PKEY *pubPkey = NULL;
  18190. EVP_PKEY_CTX *enc = NULL;
  18191. EVP_PKEY_CTX *dec = NULL;
  18192. byte in[] = TEST_STRING;
  18193. byte out[256];
  18194. size_t outlen;
  18195. size_t keySz;
  18196. byte plain[256];
  18197. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  18198. XFILE keyFile;
  18199. XFILE keypubFile;
  18200. char cliKey[] = "./certs/client-key.der";
  18201. char cliKeypub[] = "./certs/client-keyPub.der";
  18202. #endif
  18203. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18204. if (prvTmp == NULL)
  18205. return ERR_BASE_PKEY-1;
  18206. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18207. if (pubTmp == NULL) {
  18208. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18209. return ERR_BASE_PKEY-2;
  18210. }
  18211. #ifdef USE_CERT_BUFFERS_1024
  18212. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  18213. prvBytes = sizeof_client_key_der_1024;
  18214. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  18215. pubBytes = sizeof_client_keypub_der_1024;
  18216. #elif defined(USE_CERT_BUFFERS_2048)
  18217. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  18218. prvBytes = sizeof_client_key_der_2048;
  18219. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  18220. pubBytes = sizeof_client_keypub_der_2048;
  18221. #else
  18222. keyFile = XFOPEN(cliKey, "rb");
  18223. if (!keyFile) {
  18224. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18225. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18226. err_sys("can't open ./certs/client-key.der, "
  18227. "Please run from wolfSSL home dir", ERR_BASE_PKEY-3);
  18228. return ERR_BASE_PKEY-3;
  18229. }
  18230. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  18231. XFCLOSE(keyFile);
  18232. keypubFile = XFOPEN(cliKeypub, "rb");
  18233. if (!keypubFile) {
  18234. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18235. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18236. err_sys("can't open ./certs/client-cert.der, "
  18237. "Please run from wolfSSL home dir", -4);
  18238. return ERR_BASE_PKEY-4;
  18239. }
  18240. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  18241. XFCLOSE(keypubFile);
  18242. #endif /* USE_CERT_BUFFERS */
  18243. prvRsa = wolfSSL_RSA_new();
  18244. pubRsa = wolfSSL_RSA_new();
  18245. if((prvRsa == NULL) || (pubRsa == NULL)){
  18246. printf("error with RSA_new\n");
  18247. ret = ERR_BASE_PKEY-10;
  18248. goto openssl_pkey0_test_done;
  18249. }
  18250. ret = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  18251. if(ret != SSL_SUCCESS){
  18252. printf("error with RSA_LoadDer_ex\n");
  18253. ret = ERR_BASE_PKEY-11;
  18254. goto openssl_pkey0_test_done;
  18255. }
  18256. ret = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  18257. if(ret != SSL_SUCCESS){
  18258. printf("error with RSA_LoadDer_ex\n");
  18259. ret = ERR_BASE_PKEY-12;
  18260. goto openssl_pkey0_test_done;
  18261. }
  18262. keySz = (size_t)RSA_size(pubRsa);
  18263. prvPkey = wolfSSL_EVP_PKEY_new();
  18264. pubPkey = wolfSSL_EVP_PKEY_new();
  18265. if((prvPkey == NULL) || (pubPkey == NULL)){
  18266. printf("error with PKEY_new\n");
  18267. ret = ERR_BASE_PKEY-13;
  18268. goto openssl_pkey0_test_done;
  18269. }
  18270. ret = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  18271. ret += wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  18272. if(ret != 2){
  18273. printf("error with PKEY_set1_RSA\n");
  18274. ret = ERR_BASE_PKEY-14;
  18275. goto openssl_pkey0_test_done;
  18276. }
  18277. dec = EVP_PKEY_CTX_new(prvPkey, NULL);
  18278. enc = EVP_PKEY_CTX_new(pubPkey, NULL);
  18279. if((dec == NULL)||(enc==NULL)){
  18280. printf("error with EVP_PKEY_CTX_new\n");
  18281. ret = ERR_BASE_PKEY-15;
  18282. goto openssl_pkey0_test_done;
  18283. }
  18284. ret = EVP_PKEY_decrypt_init(dec);
  18285. if (ret != 1) {
  18286. printf("error with decrypt init\n");
  18287. ret = ERR_BASE_PKEY-16;
  18288. goto openssl_pkey0_test_done;
  18289. }
  18290. ret = EVP_PKEY_encrypt_init(enc);
  18291. if (ret != 1) {
  18292. printf("error with encrypt init\n");
  18293. ret = ERR_BASE_PKEY-17;
  18294. goto openssl_pkey0_test_done;
  18295. }
  18296. XMEMSET(out, 0, sizeof(out));
  18297. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  18298. if (ret != 1) {
  18299. printf("error encrypting msg\n");
  18300. ret = ERR_BASE_PKEY-18;
  18301. goto openssl_pkey0_test_done;
  18302. }
  18303. show("encrypted msg", out, outlen);
  18304. XMEMSET(plain, 0, sizeof(plain));
  18305. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  18306. if (ret != 1) {
  18307. printf("error decrypting msg\n");
  18308. ret = ERR_BASE_PKEY-19;
  18309. goto openssl_pkey0_test_done;
  18310. }
  18311. show("decrypted msg", plain, outlen);
  18312. /* RSA_PKCS1_OAEP_PADDING test */
  18313. ret = EVP_PKEY_decrypt_init(dec);
  18314. if (ret != 1) {
  18315. printf("error with decrypt init\n");
  18316. ret = ERR_BASE_PKEY-30;
  18317. goto openssl_pkey0_test_done;
  18318. }
  18319. ret = EVP_PKEY_encrypt_init(enc);
  18320. if (ret != 1) {
  18321. printf("error with encrypt init\n");
  18322. ret = ERR_BASE_PKEY-31;
  18323. goto openssl_pkey0_test_done;
  18324. }
  18325. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  18326. printf("first set rsa padding error\n");
  18327. ret = ERR_BASE_PKEY-32;
  18328. goto openssl_pkey0_test_done;
  18329. }
  18330. #ifndef HAVE_FIPS
  18331. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  18332. printf("second set rsa padding error\n");
  18333. ret = ERR_BASE_PKEY-33;
  18334. goto openssl_pkey0_test_done;
  18335. }
  18336. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  18337. printf("third set rsa padding error\n");
  18338. ret = ERR_BASE_PKEY-34;
  18339. goto openssl_pkey0_test_done;
  18340. }
  18341. #endif
  18342. XMEMSET(out, 0, sizeof(out));
  18343. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  18344. if (ret != 1) {
  18345. printf("error encrypting msg\n");
  18346. ret = ERR_BASE_PKEY-35;
  18347. goto openssl_pkey0_test_done;
  18348. }
  18349. show("encrypted msg", out, outlen);
  18350. XMEMSET(plain, 0, sizeof(plain));
  18351. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  18352. if (ret != 1) {
  18353. printf("error decrypting msg\n");
  18354. ret = ERR_BASE_PKEY-36;
  18355. goto openssl_pkey0_test_done;
  18356. }
  18357. show("decrypted msg", plain, outlen);
  18358. ret = 0; /* made it to this point without error then set success */
  18359. openssl_pkey0_test_done:
  18360. wolfSSL_RSA_free(prvRsa);
  18361. wolfSSL_RSA_free(pubRsa);
  18362. EVP_PKEY_free(pubPkey);
  18363. EVP_PKEY_free(prvPkey);
  18364. EVP_PKEY_CTX_free(dec);
  18365. EVP_PKEY_CTX_free(enc);
  18366. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18367. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18368. #endif /* NO_RSA */
  18369. return ret;
  18370. }
  18371. WOLFSSL_TEST_SUBROUTINE int openssl_pkey1_test(void)
  18372. {
  18373. int ret = 0;
  18374. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  18375. !defined(NO_SHA)
  18376. EVP_PKEY_CTX* dec = NULL;
  18377. EVP_PKEY_CTX* enc = NULL;
  18378. EVP_PKEY* pubKey = NULL;
  18379. EVP_PKEY* prvKey = NULL;
  18380. X509* x509 = NULL;
  18381. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sugar slapped";
  18382. const unsigned char* clikey;
  18383. long cliKeySz;
  18384. size_t outlen;
  18385. int keyLenBits = 2048;
  18386. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18387. unsigned char *tmp = (unsigned char *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18388. unsigned char *cipher = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18389. unsigned char *plain = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18390. if ((tmp == NULL) ||
  18391. (cipher == NULL) ||
  18392. (plain == NULL)) {
  18393. ret = -9015;
  18394. goto openssl_pkey1_test_done;
  18395. }
  18396. #else
  18397. unsigned char tmp[FOURK_BUF];
  18398. unsigned char cipher[RSA_TEST_BYTES];
  18399. unsigned char plain[RSA_TEST_BYTES];
  18400. #endif
  18401. #if defined(USE_CERT_BUFFERS_1024)
  18402. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  18403. cliKeySz = (long)sizeof_client_key_der_1024;
  18404. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_1024,
  18405. sizeof_client_cert_der_1024, SSL_FILETYPE_ASN1);
  18406. keyLenBits = 1024;
  18407. #elif defined(USE_CERT_BUFFERS_2048)
  18408. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  18409. cliKeySz = (long)sizeof_client_key_der_2048;
  18410. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_2048,
  18411. sizeof_client_cert_der_2048, SSL_FILETYPE_ASN1);
  18412. #elif defined(USE_CERT_BUFFERS_3072)
  18413. XMEMCPY(tmp, client_key_der_3072, sizeof_client_key_der_3072);
  18414. cliKeySz = (long)sizeof_client_key_der_3072;
  18415. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_3072,
  18416. sizeof_client_cert_der_3072, SSL_FILETYPE_ASN1);
  18417. keyLenBits = 3072;
  18418. #elif defined(USE_CERT_BUFFERS_4096)
  18419. XMEMCPY(tmp, client_key_der_4096, sizeof_client_key_der_4096);
  18420. cliKeySz = (long)sizeof_client_key_der_4096;
  18421. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_4096,
  18422. sizeof_client_cert_der_4096, SSL_FILETYPE_ASN1);
  18423. keyLenBits = 4096;
  18424. #else
  18425. {
  18426. XFILE f;
  18427. f = XFOPEN(clientKey, "rb");
  18428. if (!f) {
  18429. err_sys("can't open ./certs/client-key.der, "
  18430. "Please run from wolfSSL home dir", -41);
  18431. ret = -9000;
  18432. goto openssl_pkey1_test_done;
  18433. }
  18434. cliKeySz = (long)XFREAD(tmp, 1, FOURK_BUF, f);
  18435. XFCLOSE(f);
  18436. }
  18437. /* using existing wolfSSL api to get public and private key */
  18438. x509 = wolfSSL_X509_load_certificate_file(clientCert, SSL_FILETYPE_ASN1);
  18439. #endif /* USE_CERT_BUFFERS */
  18440. clikey = tmp;
  18441. if ((prvKey = EVP_PKEY_new()) == NULL) {
  18442. ret = -9001;
  18443. goto openssl_pkey1_test_done;
  18444. }
  18445. EVP_PKEY_free(prvKey);
  18446. prvKey = NULL;
  18447. if (x509 == NULL) {
  18448. ret = -9002;
  18449. goto openssl_pkey1_test_done;
  18450. }
  18451. pubKey = X509_get_pubkey(x509);
  18452. if (pubKey == NULL) {
  18453. ret = -9003;
  18454. goto openssl_pkey1_test_done;
  18455. }
  18456. prvKey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &clikey, cliKeySz);
  18457. if (prvKey == NULL) {
  18458. ret = -9004;
  18459. goto openssl_pkey1_test_done;
  18460. }
  18461. /* phase 2 API to create EVP_PKEY_CTX and encrypt/decrypt */
  18462. if (EVP_PKEY_bits(prvKey) != keyLenBits) {
  18463. ret = -9005;
  18464. goto openssl_pkey1_test_done;
  18465. }
  18466. if (EVP_PKEY_size(prvKey) != keyLenBits/8) {
  18467. ret = -9006;
  18468. goto openssl_pkey1_test_done;
  18469. }
  18470. dec = EVP_PKEY_CTX_new(prvKey, NULL);
  18471. enc = EVP_PKEY_CTX_new(pubKey, NULL);
  18472. if (dec == NULL || enc == NULL) {
  18473. ret = -9007;
  18474. goto openssl_pkey1_test_done;
  18475. }
  18476. if (EVP_PKEY_decrypt_init(dec) != 1) {
  18477. ret = -9008;
  18478. goto openssl_pkey1_test_done;
  18479. }
  18480. if (EVP_PKEY_encrypt_init(enc) != 1) {
  18481. ret = -9009;
  18482. goto openssl_pkey1_test_done;
  18483. }
  18484. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  18485. ret = -9010;
  18486. goto openssl_pkey1_test_done;
  18487. }
  18488. #ifndef HAVE_FIPS
  18489. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  18490. ret = -9011;
  18491. goto openssl_pkey1_test_done;
  18492. }
  18493. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  18494. ret = -9012;
  18495. goto openssl_pkey1_test_done;
  18496. }
  18497. #endif
  18498. XMEMSET(cipher, 0, RSA_TEST_BYTES);
  18499. outlen = keyLenBits/8;
  18500. if (EVP_PKEY_encrypt(enc, cipher, &outlen, msg, sizeof(msg)) < 0) {
  18501. ret = -9013;
  18502. goto openssl_pkey1_test_done;
  18503. }
  18504. XMEMSET(plain, 0, RSA_TEST_BYTES);
  18505. if (EVP_PKEY_decrypt(dec, plain, &outlen, cipher, outlen) != 1) {
  18506. ret = -9014;
  18507. goto openssl_pkey1_test_done;
  18508. }
  18509. openssl_pkey1_test_done:
  18510. if (pubKey != NULL) {
  18511. EVP_PKEY_free(pubKey);
  18512. }
  18513. if (prvKey != NULL) {
  18514. EVP_PKEY_free(prvKey);
  18515. }
  18516. if (dec != NULL) {
  18517. EVP_PKEY_CTX_free(dec);
  18518. }
  18519. if (enc != NULL) {
  18520. EVP_PKEY_CTX_free(enc);
  18521. }
  18522. if (x509 != NULL) {
  18523. X509_free(x509);
  18524. }
  18525. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18526. if (tmp != NULL)
  18527. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18528. if (cipher != NULL)
  18529. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18530. if (plain != NULL)
  18531. XFREE(plain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18532. #endif
  18533. #endif
  18534. return ret;
  18535. }
  18536. #define ERR_BASE_EVPSIG (-5100)
  18537. WOLFSSL_TEST_SUBROUTINE int openssl_evpSig_test(void)
  18538. {
  18539. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(HAVE_USER_RSA)
  18540. byte* prvTmp;
  18541. byte* pubTmp;
  18542. int prvBytes;
  18543. int pubBytes;
  18544. RSA *prvRsa;
  18545. RSA *pubRsa;
  18546. EVP_PKEY *prvPkey;
  18547. EVP_PKEY *pubPkey;
  18548. EVP_MD_CTX* sign;
  18549. EVP_MD_CTX* verf;
  18550. char msg[] = "see spot run";
  18551. unsigned char sig[256];
  18552. unsigned int sigSz;
  18553. const void* pt;
  18554. unsigned int count;
  18555. int ret, ret1, ret2;
  18556. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  18557. XFILE keyFile;
  18558. XFILE keypubFile;
  18559. char cliKey[] = "./certs/client-key.der";
  18560. char cliKeypub[] = "./certs/client-keyPub.der";
  18561. #endif
  18562. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18563. if (prvTmp == NULL)
  18564. return ERR_BASE_EVPSIG-1;
  18565. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18566. if (pubTmp == NULL) {
  18567. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18568. return ERR_BASE_EVPSIG-2;
  18569. }
  18570. #ifdef USE_CERT_BUFFERS_1024
  18571. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  18572. prvBytes = sizeof_client_key_der_1024;
  18573. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  18574. pubBytes = sizeof_client_keypub_der_1024;
  18575. #elif defined(USE_CERT_BUFFERS_2048)
  18576. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  18577. prvBytes = sizeof_client_key_der_2048;
  18578. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  18579. pubBytes = sizeof_client_keypub_der_2048;
  18580. #else
  18581. keyFile = XFOPEN(cliKey, "rb");
  18582. if (!keyFile) {
  18583. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18584. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18585. err_sys("can't open ./certs/client-key.der, "
  18586. "Please run from wolfSSL home dir", -40);
  18587. return ERR_BASE_EVPSIG-3;
  18588. }
  18589. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  18590. XFCLOSE(keyFile);
  18591. keypubFile = XFOPEN(cliKeypub, "rb");
  18592. if (!keypubFile) {
  18593. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18594. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18595. err_sys("can't open ./certs/client-cert.der, "
  18596. "Please run from wolfSSL home dir", -41);
  18597. return ERR_BASE_EVPSIG-4;
  18598. }
  18599. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  18600. XFCLOSE(keypubFile);
  18601. #endif /* USE_CERT_BUFFERS */
  18602. prvRsa = wolfSSL_RSA_new();
  18603. pubRsa = wolfSSL_RSA_new();
  18604. if((prvRsa == NULL) || (pubRsa == NULL)){
  18605. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18606. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18607. err_sys("ERROR with RSA_new", -9100);
  18608. return ERR_BASE_EVPSIG-5;
  18609. }
  18610. ret1 = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  18611. ret2 = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  18612. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  18613. printf("error with RSA_LoadDer_ex\n");
  18614. return ERR_BASE_EVPSIG-6;
  18615. }
  18616. prvPkey = wolfSSL_EVP_PKEY_new();
  18617. pubPkey = wolfSSL_EVP_PKEY_new();
  18618. if((prvPkey == NULL) || (pubPkey == NULL)){
  18619. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18620. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18621. printf("error with KEY_new\n");
  18622. return ERR_BASE_EVPSIG-7;
  18623. }
  18624. ret1 = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  18625. ret2 = wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  18626. if((ret1 != 1) || (ret2 != 1)){
  18627. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18628. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18629. printf("error with EVP_PKEY_set1_RSA\n");
  18630. return ERR_BASE_EVPSIG-8;
  18631. }
  18632. /****************** sign and verify *******************/
  18633. sign = EVP_MD_CTX_create();
  18634. verf = EVP_MD_CTX_create();
  18635. if((sign == NULL)||(verf == NULL)){
  18636. printf("error with EVP_MD_CTX_create\n");
  18637. EVP_MD_CTX_destroy(sign);
  18638. EVP_MD_CTX_destroy(verf);
  18639. return ERR_BASE_EVPSIG-10;
  18640. }
  18641. ret = EVP_SignInit(sign, EVP_sha1());
  18642. if (ret != SSL_SUCCESS){
  18643. printf("error with EVP_SignInit\n");
  18644. EVP_MD_CTX_destroy(sign);
  18645. EVP_MD_CTX_destroy(verf);
  18646. return ERR_BASE_EVPSIG-11;
  18647. }
  18648. count = sizeof(msg);
  18649. show("message = ", (char *)msg, count);
  18650. /* sign */
  18651. XMEMSET(sig, 0, sizeof(sig));
  18652. pt = (const void*)msg;
  18653. ret1 = EVP_SignUpdate(sign, pt, count);
  18654. ret2 = EVP_SignFinal(sign, sig, &sigSz, prvPkey);
  18655. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  18656. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18657. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18658. EVP_MD_CTX_destroy(sign);
  18659. EVP_MD_CTX_destroy(verf);
  18660. printf("error with EVP_MD_CTX_create\n");
  18661. return ERR_BASE_EVPSIG-12;
  18662. }
  18663. show("signature = ", (char *)sig, sigSz);
  18664. /* verify */
  18665. pt = (const void*)msg;
  18666. ret1 = EVP_VerifyInit(verf, EVP_sha1());
  18667. ret2 = EVP_VerifyUpdate(verf, pt, count);
  18668. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  18669. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18670. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18671. EVP_MD_CTX_destroy(sign);
  18672. EVP_MD_CTX_destroy(verf);
  18673. printf("error with EVP_Verify\n");
  18674. return ERR_BASE_EVPSIG-13;
  18675. }
  18676. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) != 1) {
  18677. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18678. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18679. EVP_MD_CTX_destroy(sign);
  18680. EVP_MD_CTX_destroy(verf);
  18681. printf("error with EVP_VerifyFinal\n");
  18682. return ERR_BASE_EVPSIG-14;
  18683. }
  18684. /* expect fail without update */
  18685. EVP_VerifyInit(verf, EVP_sha1());
  18686. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) == 1) {
  18687. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18688. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18689. EVP_MD_CTX_destroy(sign);
  18690. EVP_MD_CTX_destroy(verf);
  18691. printf("EVP_VerifyInit without update not detected\n");
  18692. return ERR_BASE_EVPSIG-15;
  18693. }
  18694. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18695. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18696. EVP_MD_CTX_destroy(sign);
  18697. EVP_MD_CTX_destroy(verf);
  18698. wolfSSL_RSA_free(prvRsa);
  18699. wolfSSL_RSA_free(pubRsa);
  18700. EVP_PKEY_free(pubPkey);
  18701. EVP_PKEY_free(prvPkey);
  18702. #endif /* NO_RSA */
  18703. return 0;
  18704. }
  18705. #endif /* OPENSSL_EXTRA */
  18706. #ifndef NO_PWDBASED
  18707. #ifdef HAVE_SCRYPT
  18708. /* Test vectors taken from RFC 7914: scrypt PBKDF - Section 12. */
  18709. WOLFSSL_TEST_SUBROUTINE int scrypt_test(void)
  18710. {
  18711. #ifdef HAVE_FIPS
  18712. /* RFC 7914 test vector keys are too short for FIPS. */
  18713. #else
  18714. int ret;
  18715. byte derived[64];
  18716. WOLFSSL_SMALL_STACK_STATIC const byte verify1[] = {
  18717. 0x77, 0xd6, 0x57, 0x62, 0x38, 0x65, 0x7b, 0x20,
  18718. 0x3b, 0x19, 0xca, 0x42, 0xc1, 0x8a, 0x04, 0x97,
  18719. 0xf1, 0x6b, 0x48, 0x44, 0xe3, 0x07, 0x4a, 0xe8,
  18720. 0xdf, 0xdf, 0xfa, 0x3f, 0xed, 0xe2, 0x14, 0x42,
  18721. 0xfc, 0xd0, 0x06, 0x9d, 0xed, 0x09, 0x48, 0xf8,
  18722. 0x32, 0x6a, 0x75, 0x3a, 0x0f, 0xc8, 0x1f, 0x17,
  18723. 0xe8, 0xd3, 0xe0, 0xfb, 0x2e, 0x0d, 0x36, 0x28,
  18724. 0xcf, 0x35, 0xe2, 0x0c, 0x38, 0xd1, 0x89, 0x06
  18725. };
  18726. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  18727. 0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00,
  18728. 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe,
  18729. 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30,
  18730. 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62,
  18731. 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88,
  18732. 0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda,
  18733. 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d,
  18734. 0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40
  18735. };
  18736. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  18737. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  18738. 0x70, 0x23, 0xbd, 0xcb, 0x3a, 0xfd, 0x73, 0x48,
  18739. 0x46, 0x1c, 0x06, 0xcd, 0x81, 0xfd, 0x38, 0xeb,
  18740. 0xfd, 0xa8, 0xfb, 0xba, 0x90, 0x4f, 0x8e, 0x3e,
  18741. 0xa9, 0xb5, 0x43, 0xf6, 0x54, 0x5d, 0xa1, 0xf2,
  18742. 0xd5, 0x43, 0x29, 0x55, 0x61, 0x3f, 0x0f, 0xcf,
  18743. 0x62, 0xd4, 0x97, 0x05, 0x24, 0x2a, 0x9a, 0xf9,
  18744. 0xe6, 0x1e, 0x85, 0xdc, 0x0d, 0x65, 0x1e, 0x40,
  18745. 0xdf, 0xcf, 0x01, 0x7b, 0x45, 0x57, 0x58, 0x87
  18746. };
  18747. #endif
  18748. #ifdef SCRYPT_TEST_ALL
  18749. /* Test case is very slow.
  18750. * Use for confirmation after code change or new platform.
  18751. */
  18752. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  18753. 0x21, 0x01, 0xcb, 0x9b, 0x6a, 0x51, 0x1a, 0xae,
  18754. 0xad, 0xdb, 0xbe, 0x09, 0xcf, 0x70, 0xf8, 0x81,
  18755. 0xec, 0x56, 0x8d, 0x57, 0x4a, 0x2f, 0xfd, 0x4d,
  18756. 0xab, 0xe5, 0xee, 0x98, 0x20, 0xad, 0xaa, 0x47,
  18757. 0x8e, 0x56, 0xfd, 0x8f, 0x4b, 0xa5, 0xd0, 0x9f,
  18758. 0xfa, 0x1c, 0x6d, 0x92, 0x7c, 0x40, 0xf4, 0xc3,
  18759. 0x37, 0x30, 0x40, 0x49, 0xe8, 0xa9, 0x52, 0xfb,
  18760. 0xcb, 0xf4, 0x5c, 0x6f, 0xa7, 0x7a, 0x41, 0xa4
  18761. };
  18762. #endif
  18763. ret = wc_scrypt(derived, NULL, 0, NULL, 0, 4, 1, 1, sizeof(verify1));
  18764. if (ret != 0)
  18765. return -9200;
  18766. if (XMEMCMP(derived, verify1, sizeof(verify1)) != 0)
  18767. return -9201;
  18768. ret = wc_scrypt(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 10, 8, 16,
  18769. sizeof(verify2));
  18770. if (ret != 0)
  18771. return -9202;
  18772. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  18773. return -9203;
  18774. /* Test case with parallel overflowing */
  18775. ret = wc_scrypt(derived, (byte*)"password", 16, (byte*)"NaCl", 16, 2, 4, 8388608,
  18776. sizeof(verify2));
  18777. if (ret != BAD_FUNC_ARG)
  18778. return -9210;
  18779. /* Don't run these test on embedded, since they use large mallocs */
  18780. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  18781. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  18782. (byte*)"SodiumChloride", 14, 14, 8, 1, sizeof(verify3));
  18783. if (ret != 0)
  18784. return -9204;
  18785. if (XMEMCMP(derived, verify3, sizeof(verify3)) != 0)
  18786. return -9205;
  18787. #ifdef SCRYPT_TEST_ALL
  18788. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  18789. (byte*)"SodiumChloride", 14, 20, 8, 1, sizeof(verify4));
  18790. if (ret != 0)
  18791. return -9206;
  18792. if (XMEMCMP(derived, verify4, sizeof(verify4)) != 0)
  18793. return -9207;
  18794. #endif
  18795. #endif /* !BENCH_EMBEDDED && !defined(WOLFSSL_LINUXKM) && !HAVE_INTEL_QA */
  18796. ret = wc_scrypt_ex(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 1<<10,
  18797. 8, 16, sizeof(verify2));
  18798. if (ret != 0)
  18799. return -9208;
  18800. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  18801. return -9209;
  18802. #endif /* !HAVE_FIPS */
  18803. return 0;
  18804. }
  18805. #endif
  18806. #ifdef HAVE_PKCS12
  18807. WOLFSSL_TEST_SUBROUTINE int pkcs12_test(void)
  18808. {
  18809. WOLFSSL_SMALL_STACK_STATIC const byte passwd[] = { 0x00, 0x73, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x67,
  18810. 0x00, 0x00 };
  18811. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x0a, 0x58, 0xCF, 0x64, 0x53, 0x0d, 0x82, 0x3f };
  18812. WOLFSSL_SMALL_STACK_STATIC const byte passwd2[] = { 0x00, 0x71, 0x00, 0x75, 0x00, 0x65, 0x00, 0x65,
  18813. 0x00, 0x67, 0x00, 0x00 };
  18814. WOLFSSL_SMALL_STACK_STATIC const byte salt2[] = { 0x16, 0x82, 0xC0, 0xfC, 0x5b, 0x3f, 0x7e, 0xc5 };
  18815. byte derived[64];
  18816. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  18817. 0x27, 0xE9, 0x0D, 0x7E, 0xD5, 0xA1, 0xC4, 0x11,
  18818. 0xBA, 0x87, 0x8B, 0xC0, 0x90, 0xF5, 0xCE, 0xBE,
  18819. 0x5E, 0x9D, 0x5F, 0xE3, 0xD6, 0x2B, 0x73, 0xAA
  18820. };
  18821. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  18822. 0x90, 0x1B, 0x49, 0x70, 0xF0, 0x94, 0xF0, 0xF8,
  18823. 0x45, 0xC0, 0xF3, 0xF3, 0x13, 0x59, 0x18, 0x6A,
  18824. 0x35, 0xE3, 0x67, 0xFE, 0xD3, 0x21, 0xFD, 0x7C
  18825. };
  18826. int id = 1;
  18827. int kLen = 24;
  18828. int iterations = 1;
  18829. int ret = wc_PKCS12_PBKDF(derived, passwd, sizeof(passwd), salt, 8,
  18830. iterations, kLen, WC_SHA256, id);
  18831. if (ret < 0)
  18832. return -9300;
  18833. if (XMEMCMP(derived, verify, kLen) != 0)
  18834. return -9301;
  18835. iterations = 1000;
  18836. ret = wc_PKCS12_PBKDF(derived, passwd2, sizeof(passwd2), salt2, 8,
  18837. iterations, kLen, WC_SHA256, id);
  18838. if (ret < 0)
  18839. return -9302;
  18840. ret = wc_PKCS12_PBKDF_ex(derived, passwd2, sizeof(passwd2), salt2, 8,
  18841. iterations, kLen, WC_SHA256, id, HEAP_HINT);
  18842. if (ret < 0)
  18843. return -9303;
  18844. if (XMEMCMP(derived, verify2, 24) != 0)
  18845. return -9304;
  18846. return 0;
  18847. }
  18848. #endif /* HAVE_PKCS12 */
  18849. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  18850. WOLFSSL_TEST_SUBROUTINE int pbkdf2_test(void)
  18851. {
  18852. char passwd[] = "passwordpassword";
  18853. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  18854. int iterations = 2048;
  18855. int kLen = 24;
  18856. byte derived[64];
  18857. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  18858. 0x43, 0x6d, 0xb5, 0xe8, 0xd0, 0xfb, 0x3f, 0x35, 0x42, 0x48, 0x39, 0xbc,
  18859. 0x2d, 0xd4, 0xf9, 0x37, 0xd4, 0x95, 0x16, 0xa7, 0x2a, 0x9a, 0x21, 0xd1
  18860. };
  18861. int ret = wc_PBKDF2_ex(derived, (byte*)passwd, (int)XSTRLEN(passwd), salt,
  18862. (int)sizeof(salt), iterations, kLen, WC_SHA256, HEAP_HINT, devId);
  18863. if (ret != 0)
  18864. return ret;
  18865. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  18866. return -9400;
  18867. return 0;
  18868. }
  18869. #endif /* HAVE_PBKDF2 && !NO_SHA256 && !NO_HMAC */
  18870. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  18871. WOLFSSL_TEST_SUBROUTINE int pbkdf1_test(void)
  18872. {
  18873. char passwd[] = "password";
  18874. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  18875. int iterations = 1000;
  18876. int kLen = 16;
  18877. byte derived[16];
  18878. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  18879. 0xDC, 0x19, 0x84, 0x7E, 0x05, 0xC6, 0x4D, 0x2F,
  18880. 0xAF, 0x10, 0xEB, 0xFB, 0x4A, 0x3D, 0x2A, 0x20
  18881. };
  18882. int ret = wc_PBKDF1_ex(derived, kLen, NULL, 0, (byte*)passwd,
  18883. (int)XSTRLEN(passwd), salt, (int)sizeof(salt), iterations, WC_SHA,
  18884. HEAP_HINT);
  18885. if (ret != 0)
  18886. return ret;
  18887. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  18888. return -9500;
  18889. return 0;
  18890. }
  18891. #endif /* HAVE_PBKDF2 && !NO_SHA */
  18892. WOLFSSL_TEST_SUBROUTINE int pwdbased_test(void)
  18893. {
  18894. int ret = 0;
  18895. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  18896. ret = pbkdf1_test();
  18897. if (ret != 0)
  18898. return ret;
  18899. #endif
  18900. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  18901. ret = pbkdf2_test();
  18902. if (ret != 0)
  18903. return ret;
  18904. #endif
  18905. #ifdef HAVE_PKCS12
  18906. ret = pkcs12_test();
  18907. if (ret != 0)
  18908. return ret;
  18909. #endif
  18910. #ifdef HAVE_SCRYPT
  18911. ret = scrypt_test();
  18912. if (ret != 0)
  18913. return ret;
  18914. #endif
  18915. return ret;
  18916. }
  18917. #endif /* NO_PWDBASED */
  18918. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  18919. /* hkdf_test has issue with WOLFSSL_TEST_SUBROUTINE set on Xilinx with afalg */
  18920. static int hkdf_test(void)
  18921. {
  18922. int ret = 0;
  18923. #if !defined(NO_SHA) || !defined(NO_SHA256)
  18924. int L;
  18925. byte okm1[42];
  18926. byte ikm1[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  18927. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  18928. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  18929. #ifndef HAVE_FIPS
  18930. byte salt1[13] ={ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18931. 0x08, 0x09, 0x0a, 0x0b, 0x0c };
  18932. byte info1[10] ={ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  18933. 0xf8, 0xf9 };
  18934. #endif
  18935. #ifndef NO_SHA
  18936. byte res1[42] = { 0x0a, 0xc1, 0xaf, 0x70, 0x02, 0xb3, 0xd7, 0x61,
  18937. 0xd1, 0xe5, 0x52, 0x98, 0xda, 0x9d, 0x05, 0x06,
  18938. 0xb9, 0xae, 0x52, 0x05, 0x72, 0x20, 0xa3, 0x06,
  18939. 0xe0, 0x7b, 0x6b, 0x87, 0xe8, 0xdf, 0x21, 0xd0,
  18940. 0xea, 0x00, 0x03, 0x3d, 0xe0, 0x39, 0x84, 0xd3,
  18941. 0x49, 0x18 };
  18942. #ifndef HAVE_FIPS
  18943. byte res2[42] = { 0x08, 0x5a, 0x01, 0xea, 0x1b, 0x10, 0xf3, 0x69,
  18944. 0x33, 0x06, 0x8b, 0x56, 0xef, 0xa5, 0xad, 0x81,
  18945. 0xa4, 0xf1, 0x4b, 0x82, 0x2f, 0x5b, 0x09, 0x15,
  18946. 0x68, 0xa9, 0xcd, 0xd4, 0xf1, 0x55, 0xfd, 0xa2,
  18947. 0xc2, 0x2e, 0x42, 0x24, 0x78, 0xd3, 0x05, 0xf3,
  18948. 0xf8, 0x96 };
  18949. #endif
  18950. #endif /* !NO_SHA */
  18951. #ifndef NO_SHA256
  18952. byte res3[42] = { 0x8d, 0xa4, 0xe7, 0x75, 0xa5, 0x63, 0xc1, 0x8f,
  18953. 0x71, 0x5f, 0x80, 0x2a, 0x06, 0x3c, 0x5a, 0x31,
  18954. 0xb8, 0xa1, 0x1f, 0x5c, 0x5e, 0xe1, 0x87, 0x9e,
  18955. 0xc3, 0x45, 0x4e, 0x5f, 0x3c, 0x73, 0x8d, 0x2d,
  18956. 0x9d, 0x20, 0x13, 0x95, 0xfa, 0xa4, 0xb6, 0x1a,
  18957. 0x96, 0xc8 };
  18958. #ifndef HAVE_FIPS
  18959. byte res4[42] = { 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a,
  18960. 0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a,
  18961. 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c,
  18962. 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf,
  18963. 0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18,
  18964. 0x58, 0x65 };
  18965. #endif
  18966. #endif /* !NO_SHA256 */
  18967. XMEMSET(okm1, 0, sizeof(okm1));
  18968. L = (int)sizeof(okm1);
  18969. #ifndef NO_SHA
  18970. ret = wc_HKDF(WC_SHA, ikm1, (word32)sizeof(ikm1), NULL, 0, NULL, 0,
  18971. okm1, L);
  18972. if (ret != 0)
  18973. return -9700;
  18974. if (XMEMCMP(okm1, res1, L) != 0)
  18975. return -9701;
  18976. #ifndef HAVE_FIPS
  18977. /* fips can't have key size under 14 bytes, salt is key too */
  18978. L = (int)sizeof(okm1);
  18979. ret = wc_HKDF(WC_SHA, ikm1, 11, salt1, (word32)sizeof(salt1),
  18980. info1, (word32)sizeof(info1), okm1, L);
  18981. if (ret != 0)
  18982. return -9702;
  18983. if (XMEMCMP(okm1, res2, L) != 0)
  18984. return -9703;
  18985. #endif /* HAVE_FIPS */
  18986. #endif /* !NO_SHA */
  18987. #ifndef NO_SHA256
  18988. ret = wc_HKDF(WC_SHA256, ikm1, (word32)sizeof(ikm1), NULL, 0, NULL, 0,
  18989. okm1, L);
  18990. if (ret != 0)
  18991. return -9704;
  18992. if (XMEMCMP(okm1, res3, L) != 0)
  18993. return -9705;
  18994. #ifndef HAVE_FIPS
  18995. /* fips can't have key size under 14 bytes, salt is key too */
  18996. ret = wc_HKDF(WC_SHA256, ikm1, (word32)sizeof(ikm1),
  18997. salt1, (word32)sizeof(salt1), info1, (word32)sizeof(info1), okm1, L);
  18998. if (ret != 0)
  18999. return -9706;
  19000. if (XMEMCMP(okm1, res4, L) != 0)
  19001. return -9707;
  19002. #endif /* HAVE_FIPS */
  19003. #endif /* !NO_SHA256 */
  19004. #endif /* !NO_SHA || !NO_SHA256 */
  19005. return ret;
  19006. }
  19007. #endif /* HAVE_HKDF */
  19008. #ifdef WOLFSSL_WOLFSSH
  19009. typedef struct {
  19010. byte hashId;
  19011. byte keyId;
  19012. const byte* k;
  19013. word32 kSz;
  19014. const byte* h;
  19015. word32 hSz;
  19016. const byte* sessionId;
  19017. word32 sessionIdSz;
  19018. const byte* expectedKey;
  19019. word32 expectedKeySz;
  19020. } SshKdfTestVector;
  19021. /** Test Vector Set #3: SHA-256 **/
  19022. static const byte sshKdfTvSet3k[] = {
  19023. 0x6A, 0xC3, 0x82, 0xEA, 0xAC, 0xA0, 0x93, 0xE1,
  19024. 0x25, 0xE2, 0x5C, 0x24, 0xBE, 0xBC, 0x84, 0x64,
  19025. 0x0C, 0x11, 0x98, 0x75, 0x07, 0x34, 0x4B, 0x5C,
  19026. 0x73, 0x9C, 0xEB, 0x84, 0xA9, 0xE0, 0xB2, 0x22,
  19027. 0xB9, 0xA8, 0xB5, 0x1C, 0x83, 0x9E, 0x5E, 0xBE,
  19028. 0x49, 0xCF, 0xAD, 0xBF, 0xB3, 0x95, 0x99, 0x76,
  19029. 0x4E, 0xD5, 0x22, 0x09, 0x9D, 0xC9, 0x12, 0x75,
  19030. 0x19, 0x50, 0xDC, 0x7D, 0xC9, 0x7F, 0xBD, 0xC0,
  19031. 0x63, 0x28, 0xB6, 0x8F, 0x22, 0x78, 0x1F, 0xD3,
  19032. 0x15, 0xAF, 0x56, 0x80, 0x09, 0xA5, 0x50, 0x9E,
  19033. 0x5B, 0x87, 0xA1, 0x1B, 0xF5, 0x27, 0xC0, 0x56,
  19034. 0xDA, 0xFF, 0xD8, 0x2A, 0xB6, 0xCB, 0xC2, 0x5C,
  19035. 0xCA, 0x37, 0x14, 0x34, 0x59, 0xE7, 0xBC, 0x63,
  19036. 0xBC, 0xDE, 0x52, 0x75, 0x7A, 0xDE, 0xB7, 0xDF,
  19037. 0x01, 0xCF, 0x12, 0x17, 0x3F, 0x1F, 0xEF, 0x81,
  19038. 0x02, 0xEC, 0x5A, 0xB1, 0x42, 0xC2, 0x13, 0xDD,
  19039. 0x9D, 0x30, 0x69, 0x62, 0x78, 0xA8, 0xD8, 0xBC,
  19040. 0x32, 0xDD, 0xE9, 0x59, 0x2D, 0x28, 0xC0, 0x78,
  19041. 0xC6, 0xD9, 0x2B, 0x94, 0x7D, 0x82, 0x5A, 0xCA,
  19042. 0xAB, 0x64, 0x94, 0x84, 0x6A, 0x49, 0xDE, 0x24,
  19043. 0xB9, 0x62, 0x3F, 0x48, 0x89, 0xE8, 0xAD, 0xC3,
  19044. 0x8E, 0x8C, 0x66, 0x9E, 0xFF, 0xEF, 0x17, 0x60,
  19045. 0x40, 0xAD, 0x94, 0x5E, 0x90, 0xA7, 0xD3, 0xEE,
  19046. 0xC1, 0x5E, 0xFE, 0xEE, 0x78, 0xAE, 0x71, 0x04,
  19047. 0x3C, 0x96, 0x51, 0x11, 0x03, 0xA1, 0x6B, 0xA7,
  19048. 0xCA, 0xF0, 0xAC, 0xD0, 0x64, 0x2E, 0xFD, 0xBE,
  19049. 0x80, 0x99, 0x34, 0xFA, 0xA1, 0xA5, 0xF1, 0xBD,
  19050. 0x11, 0x04, 0x36, 0x49, 0xB2, 0x5C, 0xCD, 0x1F,
  19051. 0xEE, 0x2E, 0x38, 0x81, 0x5D, 0x4D, 0x5F, 0x5F,
  19052. 0xC6, 0xB4, 0x10, 0x29, 0x69, 0xF2, 0x1C, 0x22,
  19053. 0xAE, 0x1B, 0x0E, 0x7D, 0x36, 0x03, 0xA5, 0x56,
  19054. 0xA1, 0x32, 0x62, 0xFF, 0x62, 0x8D, 0xE2, 0x22
  19055. };
  19056. static const byte sshKdfTvSet3h[] = {
  19057. 0x7B, 0x70, 0x01, 0x18, 0x5E, 0x25, 0x6D, 0x44,
  19058. 0x93, 0x44, 0x5F, 0x39, 0xA5, 0x5F, 0xB9, 0x05,
  19059. 0xE6, 0x32, 0x1F, 0x4B, 0x5D, 0xD8, 0xBB, 0xF3,
  19060. 0x10, 0x0D, 0x51, 0xBA, 0x0B, 0xDA, 0x3D, 0x2D
  19061. };
  19062. static const byte sshKdfTvSet3sid[] = {
  19063. 0x7B, 0x70, 0x01, 0x18, 0x5E, 0x25, 0x6D, 0x44,
  19064. 0x93, 0x44, 0x5F, 0x39, 0xA5, 0x5F, 0xB9, 0x05,
  19065. 0xE6, 0x32, 0x1F, 0x4B, 0x5D, 0xD8, 0xBB, 0xF3,
  19066. 0x10, 0x0D, 0x51, 0xBA, 0x0B, 0xDA, 0x3D, 0x2D
  19067. };
  19068. static const byte sshKdfTvSet3a[] = {
  19069. 0x81, 0xF0, 0x33, 0x0E, 0xF6, 0xF0, 0x53, 0x61,
  19070. 0xB3, 0x82, 0x3B, 0xFD, 0xED, 0x6E, 0x1D, 0xE9
  19071. };
  19072. static const byte sshKdfTvSet3b[] = {
  19073. 0x3F, 0x6F, 0xD2, 0x06, 0x5E, 0xEB, 0x2B, 0x0B,
  19074. 0x1D, 0x93, 0x19, 0x5A, 0x1F, 0xED, 0x48, 0xA5
  19075. };
  19076. static const byte sshKdfTvSet3c[] = {
  19077. 0xC3, 0x54, 0x71, 0x03, 0x4E, 0x6F, 0xD6, 0x54,
  19078. 0x76, 0x13, 0x17, 0x8E, 0x23, 0x43, 0x5F, 0x21
  19079. };
  19080. static const byte sshKdfTvSet3d[] = {
  19081. 0x7E, 0x9D, 0x79, 0x03, 0x20, 0x90, 0xD9, 0x9F,
  19082. 0x98, 0xB0, 0x15, 0x63, 0x4D, 0xD9, 0xF4, 0x62
  19083. };
  19084. static const byte sshKdfTvSet3e[] = {
  19085. 0x24, 0xEE, 0x55, 0x9A, 0xD7, 0xCE, 0x71, 0x2B,
  19086. 0x68, 0x5D, 0x0B, 0x22, 0x71, 0xE4, 0x43, 0xC1,
  19087. 0x7A, 0xB1, 0xD1, 0xDC, 0xEB, 0x5A, 0x36, 0x05,
  19088. 0x69, 0xD2, 0x5D, 0x5D, 0xC2, 0x43, 0x00, 0x2F
  19089. };
  19090. static const byte sshKdfTvSet3f[] = {
  19091. 0xC3, 0x41, 0x9C, 0x2B, 0x96, 0x62, 0x35, 0x86,
  19092. 0x9D, 0x71, 0x4B, 0xA5, 0xAC, 0x48, 0xDD, 0xB7,
  19093. 0xD9, 0xE3, 0x5C, 0x8C, 0x19, 0xAA, 0xC7, 0x34,
  19094. 0x22, 0x33, 0x7A, 0x37, 0x34, 0x53, 0x60, 0x7E
  19095. };
  19096. static const SshKdfTestVector sshKdfTestVectors[] = {
  19097. {WC_HASH_TYPE_SHA256, 'A',
  19098. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  19099. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  19100. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  19101. sshKdfTvSet3a, sizeof(sshKdfTvSet3a)},
  19102. {WC_HASH_TYPE_SHA256, 'B',
  19103. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  19104. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  19105. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  19106. sshKdfTvSet3b, sizeof(sshKdfTvSet3b)},
  19107. {WC_HASH_TYPE_SHA256, 'C',
  19108. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  19109. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  19110. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  19111. sshKdfTvSet3c, sizeof(sshKdfTvSet3c)},
  19112. {WC_HASH_TYPE_SHA256, 'D',
  19113. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  19114. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  19115. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  19116. sshKdfTvSet3d, sizeof(sshKdfTvSet3d)},
  19117. {WC_HASH_TYPE_SHA256, 'E',
  19118. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  19119. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  19120. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  19121. sshKdfTvSet3e, sizeof(sshKdfTvSet3e)},
  19122. {WC_HASH_TYPE_SHA256, 'F',
  19123. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  19124. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  19125. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  19126. sshKdfTvSet3f, sizeof(sshKdfTvSet3f)},
  19127. };
  19128. int sshkdf_test(void)
  19129. {
  19130. int result = 0;
  19131. word32 i;
  19132. word32 tc = sizeof(sshKdfTestVectors)/sizeof(SshKdfTestVector);
  19133. const SshKdfTestVector* tv = NULL;
  19134. byte cKey[32]; /* Greater of SHA256_DIGEST_SIZE and AES_BLOCK_SIZE */
  19135. /* sId - Session ID, eKey - Expected Key, cKey - Calculated Key */
  19136. for (i = 0, tv = sshKdfTestVectors; i < tc; i++, tv++) {
  19137. result = wc_SSH_KDF(tv->hashId, tv->keyId,
  19138. cKey, tv->expectedKeySz,
  19139. tv->k, tv->kSz, tv->h, tv->hSz,
  19140. tv->sessionId, tv->sessionIdSz);
  19141. if (result != 0) {
  19142. printf("KDF: Could not derive key.\n");
  19143. result = -101;
  19144. }
  19145. else {
  19146. if (XMEMCMP(cKey, tv->expectedKey, tv->expectedKeySz) != 0) {
  19147. printf("KDF: Calculated Key does not match Expected Key.\n");
  19148. result = -102;
  19149. }
  19150. }
  19151. if (result != 0) break;
  19152. }
  19153. return result;
  19154. }
  19155. #endif /* WOLFSSL_WOLFSSH */
  19156. #ifdef WOLFSSL_TLS13
  19157. #define TLSV13_PSK_DHE_SZ 40
  19158. typedef struct {
  19159. enum wc_HashType hashAlg;
  19160. word32 pskSz;
  19161. word32 dheSz;
  19162. byte psk[TLSV13_PSK_DHE_SZ];
  19163. byte dhe[TLSV13_PSK_DHE_SZ];
  19164. byte hashHello1[WC_MAX_DIGEST_SIZE];
  19165. byte hashHello2[WC_MAX_DIGEST_SIZE];
  19166. byte hashFinished1[WC_MAX_DIGEST_SIZE];
  19167. byte hashFinished2[WC_MAX_DIGEST_SIZE];
  19168. /* Expected */
  19169. byte clientEarlyTrafficSecret[WC_MAX_DIGEST_SIZE];
  19170. byte earlyExporterMasterSecret[WC_MAX_DIGEST_SIZE];
  19171. byte clientHandshakeTrafficSecret[WC_MAX_DIGEST_SIZE];
  19172. byte serverHandshakeTrafficSecret[WC_MAX_DIGEST_SIZE];
  19173. byte clientApplicationTrafficSecret[WC_MAX_DIGEST_SIZE];
  19174. byte serverApplicationTrafficSecret[WC_MAX_DIGEST_SIZE];
  19175. byte exporterMasterSecret[WC_MAX_DIGEST_SIZE];
  19176. byte resumptionMasterSecret[WC_MAX_DIGEST_SIZE];
  19177. } Tls13KdfTestVector;
  19178. /* The following tests come from the CAVP test vectors we used for
  19179. * our FIPS validation. The hash values used are the components from
  19180. * the test hashed together. hashHello1 is the hash of the
  19181. * clientHelloRandom value of the test vector. hashHello2 is the hash
  19182. * of the clientHelloRandom and serverHelloRandom values from the test
  19183. * vector. hashFinished1 is clientHelloRandom, serverHelloRandom, and
  19184. * serverFinishedRandom. hashFinished2 is clientHelloRandom,
  19185. * serverHelloRandom, serverFinishedRandom, and clietnFinishedRandom
  19186. * hashed together. */
  19187. static const Tls13KdfTestVector tls13KdfTestVectors[] = {
  19188. { /* 1 */
  19189. WC_HASH_TYPE_SHA256, 35, 35,
  19190. { /* PSK */
  19191. 0x7b, 0xf1, 0x05, 0x31, 0x36, 0xfa, 0x03, 0xdc,
  19192. 0x31, 0x97, 0x88, 0x04, 0x9c, 0xbc, 0xee, 0xf7,
  19193. 0x8d, 0x84, 0x95, 0x26, 0xaf, 0x1d, 0x68, 0xb0,
  19194. 0x60, 0x7a, 0xcc, 0x4f, 0xc1, 0xd3, 0xa1, 0x68,
  19195. 0x7f, 0x6d, 0xbe
  19196. },
  19197. { /* DHE */
  19198. 0x6e, 0xa1, 0x77, 0xab, 0x2f, 0x43, 0xd2, 0x4b,
  19199. 0xe5, 0xa1, 0x09, 0xe0, 0x7a, 0xd0, 0x01, 0x35,
  19200. 0x8d, 0xf8, 0xf2, 0x5c, 0x91, 0x02, 0xb0, 0x6c,
  19201. 0x3f, 0xeb, 0xee, 0xa4, 0x42, 0x19, 0xce, 0xdc,
  19202. 0x81, 0x26, 0x40
  19203. },
  19204. { /* Hello 1 */
  19205. 0xd9, 0x4b, 0xe4, 0x17, 0xef, 0x58, 0x73, 0x7d,
  19206. 0x28, 0x3d, 0xf0, 0xcc, 0x05, 0x03, 0xaf, 0xac,
  19207. 0x3d, 0x92, 0x79, 0x48, 0xe8, 0x8c, 0xdb, 0xce,
  19208. 0x95, 0x82, 0x21, 0x31, 0x7b, 0x61, 0xd7, 0xc6
  19209. },
  19210. { /* Hello 2 */
  19211. 0xb7, 0x7f, 0x29, 0x91, 0xa4, 0x8b, 0x34, 0xdb,
  19212. 0xbd, 0xc7, 0x54, 0x1c, 0x3b, 0x86, 0xa3, 0x69,
  19213. 0xfe, 0x26, 0xe4, 0x7b, 0xac, 0x57, 0x71, 0xb3,
  19214. 0x32, 0x97, 0xed, 0xd2, 0x0e, 0x95, 0xb8, 0x63
  19215. },
  19216. { /* Finished 1 */
  19217. 0x65, 0xdb, 0x6d, 0x71, 0x71, 0xd0, 0xd8, 0x49,
  19218. 0xd0, 0x3c, 0x8e, 0x2b, 0x24, 0xdf, 0xc2, 0xe9,
  19219. 0xd6, 0xfd, 0xea, 0x04, 0x95, 0x7c, 0xf0, 0x7e,
  19220. 0x57, 0x74, 0x7c, 0xdd, 0xa3, 0x0b, 0x2b, 0x36
  19221. },
  19222. { /* Finished 2 */
  19223. 0x28, 0xf2, 0xf2, 0x79, 0xcf, 0x20, 0x52, 0x90,
  19224. 0x1d, 0x91, 0x05, 0xad, 0x44, 0x26, 0x23, 0x96,
  19225. 0x32, 0xce, 0xec, 0x61, 0xd1, 0xbf, 0x00, 0x48,
  19226. 0x4a, 0xa5, 0x60, 0xcc, 0x28, 0xb5, 0x8d, 0x98
  19227. },
  19228. { /* Client Early Traffic Secret */
  19229. 0x07, 0x14, 0x6a, 0x26, 0x5b, 0x6c, 0x7f, 0x4d, 0x6b, 0x47, 0x3f, 0xd5,
  19230. 0x03, 0x1d, 0xd2, 0x23, 0x3d, 0x89, 0x3e, 0xc6, 0x51, 0xd1, 0xac, 0xf8,
  19231. 0x28, 0xae, 0x4b, 0x76, 0xc8, 0x10, 0x7e, 0xdd
  19232. },
  19233. { /* Early Exporter Master Secret */
  19234. 0xb8, 0xd3, 0x25, 0x7e, 0x2d, 0x41, 0x7b, 0xcb, 0x5e, 0x82, 0x49, 0xf5,
  19235. 0x51, 0x3d, 0xb7, 0x59, 0x32, 0xb3, 0xdf, 0x99, 0x4e, 0x04, 0x69, 0xc6,
  19236. 0x96, 0x8e, 0xe6, 0x3d, 0x91, 0xe4, 0x81, 0x11
  19237. },
  19238. { /* Client Handshake Traffic Secret */
  19239. 0xd9, 0x3b, 0x54, 0xe2, 0xb0, 0xd1, 0x85, 0xf0, 0xfd, 0xf3, 0x48, 0x4a,
  19240. 0xf8, 0x0b, 0xa5, 0xdc, 0x4c, 0x37, 0xcb, 0xd4, 0x20, 0xaf, 0x60, 0xc7,
  19241. 0xd5, 0x50, 0x5d, 0x0c, 0x77, 0x3b, 0x6f, 0xd2
  19242. },
  19243. { /* Server Handshake Traffic Secret */
  19244. 0x4d, 0x40, 0x2b, 0xd2, 0x8c, 0x33, 0x90, 0x39, 0x67, 0x67, 0x05, 0xf7,
  19245. 0x5d, 0x37, 0x1e, 0xdc, 0x4a, 0x70, 0x6b, 0x9e, 0xf8, 0x06, 0x61, 0x89,
  19246. 0x70, 0xe1, 0x3d, 0x36, 0xad, 0x88, 0x7e, 0x5b
  19247. },
  19248. { /* Client Application Traffic Secret */
  19249. 0x74, 0x6e, 0xa0, 0x13, 0x18, 0x34, 0x48, 0x4d, 0x23, 0x31, 0xf1, 0xf9,
  19250. 0xee, 0x44, 0x6d, 0xad, 0xc1, 0xad, 0x92, 0x73, 0xca, 0x27, 0x16, 0x91,
  19251. 0xa2, 0x50, 0x9a, 0xfc, 0xec, 0xf0, 0x6b, 0x24
  19252. },
  19253. { /* Server Application Traffic Secret */
  19254. 0x89, 0x18, 0x7e, 0x34, 0x8d, 0xfc, 0x14, 0xb1, 0x4f, 0x21, 0xd8, 0x29,
  19255. 0xdb, 0x9b, 0xfb, 0x55, 0xcf, 0xa1, 0x4f, 0x95, 0xf8, 0xe0, 0xb0, 0x83,
  19256. 0xd5, 0x34, 0x9e, 0x0b, 0x83, 0x37, 0x42, 0x93
  19257. },
  19258. { /* Exporter Master Secret */
  19259. 0x7d, 0xc8, 0x88, 0x46, 0xd5, 0x57, 0x15, 0xb6, 0x24, 0x25, 0x92, 0x61,
  19260. 0xb1, 0x18, 0x86, 0x2a, 0x6d, 0xa5, 0x84, 0xeb, 0x59, 0xdf, 0x13, 0xbd,
  19261. 0x73, 0xaa, 0x5d, 0x65, 0xab, 0xd9, 0xb4, 0x56
  19262. },
  19263. { /* Resumption Master Secret */
  19264. 0x20, 0xb7, 0xd0, 0xe3, 0x82, 0x01, 0xa1, 0x04, 0xb8, 0x13, 0x29, 0xed,
  19265. 0x35, 0xe4, 0x2f, 0xbf, 0x58, 0x23, 0x7f, 0x21, 0xdb, 0x9f, 0xf8, 0xe0,
  19266. 0xe8, 0xe4, 0xab, 0xc4, 0xa1, 0x61, 0xb9, 0xbb
  19267. }
  19268. },
  19269. { /* 6 */
  19270. WC_HASH_TYPE_SHA256, 0, 33,
  19271. { 0 }, /* PSK */
  19272. { /* DHE */
  19273. 0x7a, 0x46, 0x8c, 0x5a, 0xd1, 0x8e, 0x95, 0xba,
  19274. 0x61, 0xe6, 0x6f, 0xe6, 0x76, 0x0c, 0x20, 0x43,
  19275. 0x16, 0x82, 0x15, 0xfe, 0x54, 0xa3, 0xc7, 0xfd,
  19276. 0x3b, 0x2c, 0x88, 0xb4, 0xd3, 0x42, 0x70, 0x12,
  19277. 0x18
  19278. },
  19279. { /* Hello 1 */
  19280. 0x63, 0x83, 0x58, 0xab, 0x36, 0xcd, 0x0c, 0xf3,
  19281. 0x26, 0x07, 0xb5, 0x5f, 0x0b, 0x8b, 0x45, 0xd6,
  19282. 0x7d, 0x5b, 0x42, 0xdc, 0xa8, 0xaa, 0x06, 0xfb,
  19283. 0x20, 0xa5, 0xbb, 0x85, 0xdb, 0x54, 0xd8, 0x8b
  19284. },
  19285. { /* Hello 2 */
  19286. 0xea, 0xfe, 0x9e, 0x8e, 0xff, 0x1f, 0x6f, 0x43,
  19287. 0xf9, 0x5d, 0xfd, 0xbf, 0xe2, 0x5f, 0x02, 0x2f,
  19288. 0x6d, 0x47, 0x60, 0x9a, 0x48, 0x9a, 0x75, 0xfb,
  19289. 0xb5, 0x4a, 0xbf, 0x9c, 0x4e, 0xff, 0xbf, 0x0b
  19290. },
  19291. { /* Finished 1 */
  19292. 0xca, 0x25, 0xb3, 0x53, 0x8e, 0x6d, 0xc3, 0x36,
  19293. 0x17, 0x30, 0x07, 0xdf, 0x0d, 0xd7, 0x79, 0xb0,
  19294. 0x7f, 0xcb, 0xbe, 0x7a, 0xbc, 0x2d, 0x9f, 0x2d,
  19295. 0x94, 0x44, 0x94, 0xe6, 0xa4, 0xf3, 0xe8, 0x53
  19296. },
  19297. { /* Finished 2 */
  19298. 0x2e, 0xa6, 0x5a, 0xaf, 0xb5, 0xba, 0x9f, 0x2f,
  19299. 0x74, 0x83, 0x5d, 0xbf, 0x86, 0xa4, 0xa6, 0xf6,
  19300. 0xb9, 0x89, 0xdf, 0x17, 0xe1, 0xa8, 0x14, 0xc0,
  19301. 0xe1, 0x50, 0xfa, 0xec, 0xfa, 0xae, 0x8b, 0x7b
  19302. },
  19303. {
  19304. 0x20, 0x18, 0x72, 0x7c, 0xde, 0x3a, 0x85, 0x17, 0x72, 0xdc, 0xd7, 0x72,
  19305. 0xb0, 0xfc, 0x45, 0xd0, 0x62, 0xb9, 0xbb, 0x38, 0x69, 0x05, 0x7b, 0xb4,
  19306. 0x5e, 0x58, 0x5d, 0xed, 0xcd, 0x0b, 0x96, 0xd3
  19307. },
  19308. {
  19309. 0x68, 0x10, 0x20, 0xd1, 0x5e, 0xfc, 0x0c, 0x53, 0x85, 0xbb, 0xdb, 0x18,
  19310. 0xa8, 0x78, 0xf1, 0x2b, 0x13, 0xba, 0x64, 0x1d, 0xe7, 0x09, 0xbe, 0x13,
  19311. 0x49, 0x26, 0xf9, 0x98, 0x56, 0xf1, 0x43, 0xfb
  19312. },
  19313. {
  19314. 0x24, 0x35, 0x3e, 0x10, 0x6f, 0x39, 0x50, 0xd6, 0xa2, 0x12, 0x99, 0xf2,
  19315. 0xd5, 0xf5, 0x19, 0xf5, 0x84, 0xed, 0xee, 0x78, 0x2a, 0xa6, 0xfa, 0x3d,
  19316. 0x06, 0xa8, 0xa7, 0x5d, 0x97, 0x78, 0xd6, 0x58
  19317. },
  19318. {
  19319. 0xf4, 0x57, 0xac, 0x24, 0x7a, 0xfb, 0x7c, 0x3b, 0xb6, 0x39, 0x17, 0x14,
  19320. 0xd9, 0xd4, 0x58, 0x4d, 0x46, 0xd5, 0x1b, 0xde, 0xf7, 0x9d, 0x06, 0xee,
  19321. 0x8d, 0x1a, 0x2c, 0x25, 0x6d, 0x64, 0xde, 0x89
  19322. },
  19323. {
  19324. 0xb6, 0x00, 0xce, 0x63, 0xed, 0x65, 0x8b, 0x66, 0x66, 0x42, 0xc6, 0xbd,
  19325. 0x89, 0xc4, 0x71, 0x6f, 0xce, 0x28, 0xb2, 0xac, 0x97, 0x07, 0x5b, 0xea,
  19326. 0xb8, 0x1d, 0x4c, 0xeb, 0x9e, 0x71, 0x07, 0x8f
  19327. },
  19328. {
  19329. 0xf8, 0x92, 0xc8, 0xba, 0xe7, 0x83, 0xfe, 0x68, 0xe4, 0xd6, 0x5e, 0xcb,
  19330. 0xb3, 0xef, 0x49, 0xd0, 0xe7, 0xb1, 0xac, 0xcb, 0x39, 0x19, 0xfd, 0xa7,
  19331. 0xf7, 0xca, 0xab, 0x1e, 0x42, 0x14, 0xd8, 0xe7
  19332. },
  19333. {
  19334. 0x32, 0x4a, 0x1a, 0xad, 0xe2, 0xbb, 0x55, 0x8a, 0xdd, 0xe9, 0xa5, 0x2a,
  19335. 0x46, 0x5e, 0x6c, 0x83, 0x66, 0x27, 0x27, 0x94, 0xdd, 0x68, 0x59, 0xa0,
  19336. 0xbb, 0xe8, 0x31, 0x7c, 0x39, 0xd7, 0xfd, 0x6d
  19337. },
  19338. {
  19339. 0x58, 0xbc, 0x6c, 0x5b, 0x24, 0xad, 0x82, 0xb3, 0xcc, 0xc7, 0xd1, 0xa1,
  19340. 0xaa, 0x2b, 0x98, 0x9f, 0x2f, 0x7e, 0xa9, 0x63, 0xc2, 0x8e, 0xb6, 0x06,
  19341. 0xc2, 0x2b, 0x74, 0x4b, 0x79, 0x19, 0x7e, 0x2e
  19342. }
  19343. },
  19344. { /* 11 */
  19345. WC_HASH_TYPE_SHA256, 33, 0,
  19346. { /* PSK */
  19347. 0x3d, 0x39, 0x49, 0x36, 0x98, 0xc5, 0xfd, 0xcd,
  19348. 0xa0, 0x17, 0xbd, 0x65, 0x0a, 0xdb, 0xd4, 0x07,
  19349. 0x56, 0xa2, 0x7b, 0xb8, 0x2a, 0x7e, 0xfb, 0x26,
  19350. 0x74, 0xe1, 0xbc, 0x08, 0x4b, 0xf0, 0x30, 0x14,
  19351. 0x12
  19352. },
  19353. { 0 }, /* DHE */
  19354. { /* Hello 1 */
  19355. 0xb7, 0x44, 0x74, 0x6c, 0x57, 0x1f, 0xf3, 0x84,
  19356. 0x8f, 0x63, 0xfb, 0x8c, 0x94, 0x6c, 0x16, 0x68,
  19357. 0x4b, 0xe1, 0xb5, 0xb5, 0x2a, 0x4e, 0x5f, 0xdf,
  19358. 0x4b, 0x53, 0xb2, 0x35, 0xfc, 0x30, 0xf1, 0x36
  19359. },
  19360. { /* Hello 2 */
  19361. 0xe6, 0x4f, 0x3a, 0x4f, 0xd7, 0xe0, 0x64, 0xd4,
  19362. 0x69, 0x50, 0xe4, 0x8b, 0xba, 0xbc, 0x47, 0x74,
  19363. 0xa7, 0x9b, 0x40, 0x91, 0x8f, 0xa8, 0x72, 0x22,
  19364. 0x97, 0xad, 0x43, 0xa7, 0x11, 0x86, 0xb5, 0x72
  19365. },
  19366. { /* Finished 1 */
  19367. 0x5f, 0xa6, 0x10, 0xe2, 0xa3, 0x99, 0x0b, 0x5e,
  19368. 0x57, 0xee, 0xc3, 0x3a, 0x8e, 0x04, 0xf3, 0x0e,
  19369. 0x58, 0x02, 0x09, 0xb2, 0x7e, 0x2d, 0xc6, 0xd2,
  19370. 0x08, 0xae, 0x68, 0x0a, 0x55, 0xa5, 0xda, 0x51
  19371. },
  19372. { /* Finished 2 */
  19373. 0xfc, 0x5b, 0xc0, 0x7e, 0x1b, 0xaa, 0xc0, 0xb4,
  19374. 0x34, 0x85, 0x49, 0x8e, 0x16, 0x31, 0x98, 0xdf,
  19375. 0x10, 0x54, 0x22, 0xda, 0x1e, 0x6b, 0x51, 0xf6,
  19376. 0x97, 0x57, 0xa0, 0x7a, 0x92, 0xe7, 0x47, 0x52
  19377. },
  19378. {
  19379. 0x80, 0xfa, 0x36, 0x30, 0xb8, 0x65, 0xb3, 0x2a, 0x1d, 0x68, 0x91, 0x06,
  19380. 0x98, 0xa0, 0x17, 0x8f, 0xee, 0xb7, 0x9e, 0x3d, 0xd8, 0x84, 0x99, 0x30,
  19381. 0xb9, 0xd6, 0x09, 0x25, 0x5e, 0xfb, 0x8f, 0xd3 },
  19382. {
  19383. 0xa9, 0x89, 0x29, 0x70, 0xe4, 0x55, 0xec, 0x97, 0xfb, 0x24, 0x5b, 0xf9,
  19384. 0xf1, 0xa3, 0x19, 0x3d, 0xf1, 0x31, 0x14, 0xcd, 0x2a, 0xed, 0x21, 0xc8,
  19385. 0xb1, 0x53, 0xad, 0x11, 0x0b, 0x9e, 0x5a, 0xee },
  19386. {
  19387. 0x72, 0xad, 0x8d, 0x7f, 0xfc, 0xb7, 0x68, 0xda, 0x27, 0x60, 0x37, 0xa3,
  19388. 0x4a, 0x63, 0xe8, 0xa5, 0xc8, 0xcd, 0x36, 0x6a, 0x77, 0x99, 0x0d, 0xa9,
  19389. 0xb1, 0x5b, 0x2f, 0x47, 0x2e, 0x22, 0xa7, 0x5e },
  19390. {
  19391. 0x95, 0x6e, 0x85, 0x09, 0xe5, 0x04, 0x88, 0x14, 0x28, 0x8d, 0xdf, 0xe6,
  19392. 0x0d, 0x0f, 0x0d, 0x6b, 0x4e, 0x66, 0x1c, 0x03, 0xb9, 0xaa, 0x2d, 0x45,
  19393. 0x56, 0x67, 0x5c, 0x55, 0x29, 0xd6, 0x89, 0xd0 },
  19394. {
  19395. 0xe8, 0xf2, 0x14, 0xf9, 0x9b, 0x2b, 0x9f, 0x24, 0x2b, 0x37, 0xbe, 0x86,
  19396. 0xdb, 0x23, 0x4b, 0xbe, 0x39, 0x57, 0xe8, 0xa9, 0xa5, 0xee, 0x08, 0xf2,
  19397. 0x75, 0x58, 0xdb, 0xd9, 0x51, 0xc1, 0x46, 0x02 },
  19398. {
  19399. 0x3d, 0x19, 0xaf, 0xa3, 0x0b, 0x21, 0xf7, 0x3d, 0xe7, 0x37, 0x6e, 0x32,
  19400. 0x13, 0x48, 0x9d, 0xea, 0xe0, 0x90, 0xbf, 0x64, 0x48, 0xf7, 0x1e, 0xcc,
  19401. 0xf0, 0xbc, 0x92, 0xd7, 0x8a, 0x4a, 0xa8, 0xc1 },
  19402. {
  19403. 0x16, 0x35, 0xb1, 0x66, 0x28, 0xa3, 0x3e, 0x19, 0xf5, 0x2d, 0x92, 0x22,
  19404. 0x95, 0x48, 0xe8, 0x34, 0x7b, 0x30, 0x50, 0xa2, 0xa0, 0xd9, 0xc2, 0x59,
  19405. 0x39, 0xf9, 0x8c, 0x69, 0xf2, 0x2a, 0xb9, 0xff },
  19406. {
  19407. 0x32, 0x71, 0xa6, 0x87, 0x0c, 0x97, 0x42, 0x07, 0xdd, 0x5f, 0xc9, 0x44,
  19408. 0xa5, 0x7c, 0x50, 0x14, 0xfd, 0xe7, 0x5f, 0x8b, 0xd3, 0x2f, 0xdc, 0x9b,
  19409. 0xa9, 0x93, 0x22, 0x19, 0xe6, 0xf2, 0x0c, 0xd8 }
  19410. },
  19411. #ifdef WOLFSSL_SHA384
  19412. { /* 26 */
  19413. WC_HASH_TYPE_SHA384, 35, 35,
  19414. { /* PSK */
  19415. 0x62, 0x83, 0x25, 0xc7, 0xcc, 0x08, 0x5e, 0x63,
  19416. 0x64, 0x56, 0xf0, 0xc6, 0x88, 0x27, 0x5a, 0x5b,
  19417. 0x68, 0x59, 0x0b, 0x14, 0x55, 0x13, 0x2e, 0xfd,
  19418. 0x8f, 0x28, 0x5b, 0x3d, 0xe3, 0xad, 0x67, 0xe4,
  19419. 0x68, 0xba, 0xf9
  19420. },
  19421. { /* DHE */
  19422. 0xa8, 0xb1, 0xab, 0xd8, 0xc8, 0x5b, 0x52, 0xdf,
  19423. 0x7f, 0x49, 0x10, 0xf4, 0xa1, 0x31, 0xd1, 0x91,
  19424. 0x36, 0xc1, 0x87, 0x5d, 0x42, 0x2a, 0xe7, 0x1d,
  19425. 0x2c, 0x29, 0x3d, 0x40, 0x64, 0x61, 0x63, 0x76,
  19426. 0xd8, 0x66, 0xac
  19427. },
  19428. { /* Hello 1 */
  19429. 0x6f, 0xc6, 0x4c, 0xe1, 0xc6, 0x68, 0x34, 0x8c,
  19430. 0x0a, 0xe1, 0xf8, 0xb8, 0x3e, 0xd4, 0xf8, 0x0b,
  19431. 0x54, 0x50, 0xe4, 0xc5, 0x4a, 0x33, 0x7d, 0xbd,
  19432. 0x90, 0xd2, 0xa2, 0xb9, 0xb7, 0x92, 0xed, 0xab,
  19433. 0x14, 0xf1, 0xe4, 0x86, 0x22, 0x67, 0xd7, 0x44,
  19434. 0x03, 0x21, 0xdc, 0x51, 0x52, 0x7f, 0x35, 0x80
  19435. },
  19436. { /* Hello 2 */
  19437. 0x3e, 0xcf, 0x2f, 0xc3, 0x87, 0xba, 0xc5, 0xbd,
  19438. 0x7c, 0xe8, 0x35, 0x5b, 0x95, 0x51, 0x30, 0x3b,
  19439. 0x08, 0xcc, 0x2a, 0x7d, 0xb5, 0x74, 0x7c, 0x16,
  19440. 0xb3, 0x0b, 0xe7, 0x61, 0xa3, 0x7c, 0x6c, 0xbd,
  19441. 0x39, 0x74, 0xfd, 0x1e, 0x4c, 0xff, 0xc8, 0xcc,
  19442. 0xa0, 0xef, 0x29, 0x4d, 0x94, 0xaa, 0x55, 0x6f,
  19443. },
  19444. { /* Finished 1 */
  19445. 0x06, 0xc1, 0x47, 0x78, 0x66, 0x53, 0x6f, 0x24,
  19446. 0x94, 0x61, 0x69, 0xec, 0xd8, 0x60, 0x31, 0x2f,
  19447. 0xbf, 0xd6, 0x8a, 0x29, 0x17, 0xff, 0xa3, 0x88,
  19448. 0x13, 0x09, 0x8c, 0x9d, 0x6c, 0x64, 0x84, 0x48,
  19449. 0x44, 0xdd, 0x2d, 0x29, 0x4d, 0xe6, 0x98, 0x2b,
  19450. 0x45, 0x3b, 0x84, 0x33, 0x79, 0xb2, 0x75, 0x68
  19451. },
  19452. { /* Finished 2 */
  19453. 0x28, 0x1e, 0x18, 0xf7, 0x9c, 0x32, 0xa9, 0xbf,
  19454. 0x0c, 0x24, 0x58, 0x21, 0xce, 0xbc, 0xf2, 0x44,
  19455. 0xb1, 0x18, 0xaf, 0x9d, 0xd9, 0x20, 0xf9, 0xf4,
  19456. 0xed, 0xcc, 0x53, 0x82, 0x66, 0x5c, 0x46, 0x94,
  19457. 0x8c, 0x36, 0x5e, 0xca, 0x9f, 0xd8, 0x9a, 0xd3,
  19458. 0xf0, 0xe1, 0x53, 0x71, 0xdd, 0x19, 0x1e, 0x59
  19459. },
  19460. {
  19461. 0xd0, 0xef, 0xa8, 0xcb, 0x5b, 0x14, 0x0f, 0x0a, 0x62, 0xba, 0x5a, 0xb1,
  19462. 0xc5, 0xb5, 0x3f, 0x11, 0xda, 0xa1, 0x0c, 0x9c, 0xb4, 0x32, 0x48, 0x4e,
  19463. 0xfa, 0x84, 0x4f, 0xe4, 0xe7, 0x91, 0x8f, 0x42, 0x3f, 0xc7, 0x4e, 0xd3,
  19464. 0x83, 0x3d, 0x7f, 0x70, 0x12, 0xee, 0x9a, 0x37, 0x01, 0xbb, 0x14, 0xd3
  19465. },
  19466. {
  19467. 0x48, 0x6f, 0x77, 0x1d, 0x39, 0x1b, 0xa5, 0x9a, 0x76, 0xd9, 0x1d, 0x7d,
  19468. 0xb3, 0xd9, 0xb9, 0x78, 0x35, 0x0f, 0xd0, 0xe1, 0x07, 0x1f, 0x8d, 0xe5,
  19469. 0x75, 0x00, 0xda, 0xc0, 0x19, 0x01, 0xfb, 0x08, 0x35, 0xe7, 0x18, 0x8f,
  19470. 0xf0, 0x19, 0xfb, 0x46, 0xf6, 0xa5, 0x77, 0x0e, 0x90, 0x38, 0x8b, 0x15
  19471. },
  19472. {
  19473. 0x80, 0x8c, 0xa7, 0x24, 0x97, 0xf9, 0xd3, 0x52, 0xb0, 0x69, 0x9d, 0x4b,
  19474. 0xa4, 0x19, 0x4a, 0xb1, 0x46, 0x53, 0x3a, 0xc8, 0xe4, 0x02, 0x69, 0xf2,
  19475. 0xe7, 0xb6, 0x1d, 0x33, 0x51, 0xcc, 0x14, 0x40, 0x4a, 0xb0, 0xe7, 0x58,
  19476. 0x84, 0xba, 0xc2, 0x14, 0x58, 0x6b, 0xb9, 0xdc, 0x50, 0x98, 0x67, 0x01
  19477. },
  19478. {
  19479. 0xb1, 0xa8, 0xc0, 0x06, 0xb3, 0x2e, 0xa7, 0x8a, 0x6a, 0x12, 0x88, 0x00,
  19480. 0x65, 0x88, 0x9c, 0x5d, 0x35, 0xee, 0xe5, 0x51, 0x0b, 0x62, 0xf8, 0x67,
  19481. 0xe5, 0xef, 0x15, 0x1f, 0x23, 0x02, 0x74, 0x08, 0x9c, 0xc8, 0xba, 0x27,
  19482. 0x5d, 0x32, 0x19, 0x6f, 0x6d, 0x5d, 0x72, 0x5e, 0x15, 0xde, 0x30, 0xc3
  19483. },
  19484. {
  19485. 0xfd, 0xce, 0xf5, 0x65, 0x45, 0x84, 0xfb, 0x8c, 0x79, 0xa4, 0x6c, 0x1b,
  19486. 0x0e, 0x1b, 0xfd, 0x26, 0xa2, 0x53, 0xf4, 0x4e, 0x00, 0x4d, 0x4b, 0x0b,
  19487. 0x24, 0x6d, 0x35, 0x35, 0xd9, 0x97, 0x70, 0xc5, 0xf4, 0xee, 0xe3, 0xba,
  19488. 0x31, 0x1e, 0x2a, 0x42, 0xcb, 0xdf, 0x40, 0xb1, 0x14, 0xb8, 0x53, 0xce
  19489. },
  19490. {
  19491. 0xbb, 0xb3, 0x26, 0x7c, 0x22, 0x21, 0x9b, 0x72, 0x32, 0xa1, 0x97, 0xfb,
  19492. 0x78, 0x8c, 0xbe, 0x3d, 0x71, 0x45, 0xb8, 0xf5, 0x24, 0x8f, 0x0f, 0xac,
  19493. 0x42, 0x5b, 0x81, 0xe8, 0xd0, 0x71, 0x4a, 0xcb, 0x32, 0x3f, 0x03, 0xfb,
  19494. 0xec, 0x6a, 0x1f, 0x76, 0x80, 0x65, 0x01, 0x7a, 0x3d, 0xce, 0xc4, 0xdf
  19495. },
  19496. {
  19497. 0x3f, 0xcf, 0x2f, 0x63, 0x94, 0x94, 0x99, 0xfd, 0x04, 0x3a, 0x89, 0x83,
  19498. 0xcf, 0x06, 0x05, 0xec, 0x20, 0x3e, 0x5f, 0x51, 0x9d, 0x6e, 0x4a, 0xc6,
  19499. 0xf1, 0x2b, 0x37, 0x17, 0x34, 0x72, 0x6e, 0x1d, 0x2a, 0xfd, 0xc7, 0x73,
  19500. 0xb5, 0x07, 0x22, 0x81, 0x32, 0x2e, 0x21, 0x85, 0xaf, 0x10, 0xb2, 0x73
  19501. },
  19502. {
  19503. 0x52, 0x0c, 0x3d, 0x2e, 0x2d, 0x4a, 0x11, 0xae, 0x96, 0x78, 0xe9, 0x5b,
  19504. 0xd8, 0x0f, 0x6c, 0xf4, 0xbd, 0x96, 0x13, 0x55, 0x88, 0xdd, 0xa3, 0x67,
  19505. 0x36, 0x86, 0x1e, 0x0b, 0x36, 0x41, 0xec, 0xf6, 0x04, 0xb2, 0xc4, 0x16,
  19506. 0xbc, 0x2c, 0xdb, 0x30, 0x02, 0x94, 0xd4, 0x42, 0xbf, 0x38, 0xee, 0x9d
  19507. }
  19508. },
  19509. { /* 36 */
  19510. WC_HASH_TYPE_SHA384, 0, 33,
  19511. { 0 }, /* PSK */
  19512. { /* DHE */
  19513. 0xd3, 0x00, 0x72, 0x9a, 0xa8, 0xc5, 0xf3, 0xc4,
  19514. 0xf1, 0xa0, 0x26, 0x89, 0x65, 0x70, 0xc7, 0x0b,
  19515. 0x77, 0xbb, 0xe1, 0x4b, 0x2b, 0xa8, 0x4f, 0xa6,
  19516. 0x09, 0x4b, 0xba, 0x45, 0x36, 0x15, 0xee, 0x68,
  19517. 0xfd
  19518. },
  19519. { /* Hello 1 */
  19520. 0x10, 0x9d, 0x8b, 0xa2, 0x93, 0xe7, 0xd3, 0xb9,
  19521. 0xb4, 0x0f, 0xeb, 0x6a, 0xb9, 0x69, 0xcb, 0x39,
  19522. 0x16, 0x29, 0xcc, 0xd3, 0xcc, 0x1a, 0x4c, 0x1b,
  19523. 0x53, 0x7c, 0x33, 0x88, 0x06, 0xbc, 0x0a, 0x02,
  19524. 0xa0, 0xbe, 0x62, 0xc0, 0xe6, 0x5e, 0x97, 0x5b,
  19525. 0x6a, 0xa1, 0x98, 0xf3, 0xd2, 0x1e, 0xcd, 0xc5
  19526. },
  19527. { /* Hello 2 */
  19528. 0x74, 0xc0, 0x07, 0x2c, 0xc1, 0x63, 0xcc, 0x11,
  19529. 0xad, 0x1a, 0x55, 0x63, 0xbc, 0x20, 0x77, 0x96,
  19530. 0x30, 0x1c, 0x68, 0x45, 0x1e, 0x9b, 0xa7, 0xb4,
  19531. 0xf3, 0x04, 0x45, 0x16, 0x76, 0x55, 0xf9, 0xdf,
  19532. 0x4b, 0x2f, 0x1a, 0xdf, 0x5a, 0xb0, 0x93, 0xc9,
  19533. 0xab, 0xf5, 0x32, 0x47, 0x79, 0x9c, 0x01, 0xeb
  19534. },
  19535. { /* Finished 1 */
  19536. 0x27, 0x08, 0x8e, 0xa5, 0xf1, 0x30, 0xe1, 0xd6,
  19537. 0x4f, 0xa2, 0x9e, 0x3b, 0x03, 0x2d, 0x2e, 0xa3,
  19538. 0x84, 0x75, 0x51, 0x3a, 0xc3, 0xf6, 0xee, 0x2e,
  19539. 0x37, 0x0c, 0xe3, 0x28, 0x46, 0xa5, 0x2d, 0xc7,
  19540. 0xf0, 0x64, 0x78, 0x53, 0x66, 0x43, 0x02, 0xa4,
  19541. 0x7a, 0x43, 0x66, 0x4b, 0xa7, 0xcb, 0x97, 0x16
  19542. },
  19543. { /* Finished 2 */
  19544. 0x1d, 0x0d, 0xf8, 0xe1, 0x81, 0xa5, 0xbd, 0xa8,
  19545. 0x6f, 0x9d, 0x01, 0xa4, 0x9a, 0x92, 0xe2, 0xef,
  19546. 0x08, 0xab, 0xef, 0x3e, 0x2d, 0xd4, 0x82, 0xac,
  19547. 0x68, 0x9d, 0xe0, 0x54, 0x17, 0xde, 0x1a, 0xed,
  19548. 0x57, 0xcb, 0xd9, 0x2d, 0xc8, 0xbc, 0x93, 0xe6,
  19549. 0xa3, 0xec, 0xde, 0xee, 0xa1, 0x1c, 0x41, 0x85
  19550. },
  19551. {
  19552. 0x7f, 0x1f, 0xe6, 0x7b, 0xd8, 0xf5, 0x2b, 0x37, 0xbe, 0xb7, 0xd0, 0x37,
  19553. 0xce, 0x46, 0xad, 0x04, 0x2f, 0xc7, 0xdb, 0xc9, 0x9a, 0xb6, 0x00, 0x3f,
  19554. 0xc1, 0x97, 0xe9, 0x5c, 0x5e, 0x14, 0xd1, 0x38, 0x4d, 0x55, 0xe1, 0x07,
  19555. 0xb5, 0x85, 0x6d, 0xfa, 0xa7, 0x66, 0xad, 0xfa, 0xb6, 0xad, 0x29, 0x44
  19556. },
  19557. {
  19558. 0x4e, 0x6b, 0x20, 0x99, 0x55, 0x1b, 0x21, 0x89, 0xb6, 0x70, 0xdb, 0xe8,
  19559. 0xa7, 0x16, 0x55, 0xf2, 0x93, 0x13, 0x90, 0x7d, 0xfa, 0x62, 0x65, 0x53,
  19560. 0xa0, 0x97, 0xe9, 0xb4, 0xc0, 0xf1, 0xc9, 0x1a, 0x67, 0xdd, 0xca, 0x57,
  19561. 0xbc, 0xca, 0x39, 0xe6, 0x39, 0x6b, 0x63, 0x47, 0x25, 0x08, 0x3a, 0xd7
  19562. },
  19563. {
  19564. 0x35, 0x0d, 0xac, 0xd8, 0x10, 0x6a, 0x46, 0x50, 0x66, 0xae, 0x02, 0xc9,
  19565. 0xde, 0x13, 0x48, 0xce, 0x53, 0xd4, 0x92, 0x62, 0xc5, 0x65, 0x10, 0x08,
  19566. 0xc2, 0xc2, 0x82, 0xed, 0x9d, 0xc9, 0x6f, 0xa8, 0xc3, 0xc1, 0x0b, 0x7c,
  19567. 0xe1, 0x97, 0x85, 0xd6, 0x46, 0x29, 0x0e, 0x42, 0x51, 0xc1, 0x35, 0xcf
  19568. },
  19569. {
  19570. 0x3d, 0x5d, 0x84, 0xbd, 0x16, 0x46, 0x34, 0xb3, 0xf6, 0x31, 0x49, 0x3e,
  19571. 0x8d, 0xdc, 0xcb, 0x8c, 0x6a, 0x42, 0xf4, 0x88, 0xfc, 0x19, 0xfa, 0xa2,
  19572. 0x25, 0xc7, 0xa0, 0xa4, 0xca, 0xf0, 0xea, 0x2d, 0xe8, 0xc4, 0x02, 0x14,
  19573. 0x63, 0xfb, 0xd3, 0x7b, 0x51, 0x1c, 0xce, 0xca, 0xa3, 0xc3, 0xe4, 0xa5
  19574. },
  19575. {
  19576. 0x7c, 0x3a, 0x55, 0x92, 0x2e, 0xdd, 0x75, 0xdd, 0x76, 0x54, 0x4a, 0x9f,
  19577. 0xd0, 0xa2, 0x88, 0x83, 0xe9, 0x27, 0xda, 0x30, 0xe9, 0x96, 0x58, 0xc5,
  19578. 0xb7, 0x56, 0xfc, 0x4b, 0xb8, 0x5d, 0xee, 0x46, 0x70, 0x4e, 0x1b, 0x06,
  19579. 0x86, 0xaf, 0x48, 0x5c, 0x17, 0x35, 0xfa, 0x69, 0xc2, 0x4d, 0xfb, 0x09
  19580. },
  19581. {
  19582. 0x00, 0x0e, 0x28, 0x51, 0xc1, 0x7f, 0x41, 0x89, 0x6f, 0x9a, 0xca, 0x15,
  19583. 0xee, 0xed, 0x43, 0xca, 0x6d, 0x65, 0x6f, 0x51, 0x18, 0x6c, 0x08, 0x4b,
  19584. 0x77, 0xca, 0x75, 0xc4, 0xc3, 0xde, 0x29, 0x41, 0x8b, 0xaf, 0xa7, 0x1c,
  19585. 0x28, 0x37, 0xa0, 0xa0, 0x74, 0x8e, 0x09, 0x42, 0x7a, 0x1b, 0x68, 0xdb
  19586. },
  19587. {
  19588. 0x14, 0x8f, 0xab, 0x28, 0x64, 0xea, 0x45, 0x88, 0xdb, 0xc1, 0xc6, 0xa0,
  19589. 0x48, 0xdf, 0x15, 0xd0, 0x28, 0x07, 0x2d, 0x6c, 0xb8, 0x42, 0xbb, 0x60,
  19590. 0x02, 0x08, 0x9e, 0x29, 0x9b, 0x8d, 0xd6, 0x1c, 0xaf, 0xf2, 0x1a, 0xdc,
  19591. 0xf0, 0x78, 0x0b, 0x4d, 0x90, 0xa1, 0x0c, 0xb3, 0x13, 0xde, 0xca, 0x5a
  19592. },
  19593. {
  19594. 0x4d, 0x80, 0x7d, 0x0b, 0xb9, 0x00, 0x6f, 0x65, 0x51, 0x65, 0x23, 0xde,
  19595. 0x72, 0xdc, 0x4f, 0x04, 0xa5, 0xa2, 0x90, 0x45, 0x51, 0x9e, 0xd0, 0x3a,
  19596. 0xe4, 0xd7, 0x78, 0xa3, 0x0f, 0x2d, 0x65, 0x12, 0xad, 0xc8, 0x92, 0x30,
  19597. 0x79, 0x9d, 0x9d, 0x08, 0x7a, 0x9c, 0x9f, 0x83, 0xb1, 0xca, 0x59, 0x56
  19598. }
  19599. },
  19600. { /* 41 */
  19601. WC_HASH_TYPE_SHA384, 33, 0,
  19602. { /* PSK */
  19603. 0xa4, 0x8b, 0x1b, 0x5f, 0xd0, 0xea, 0x75, 0x62,
  19604. 0x06, 0x4d, 0x68, 0x40, 0x85, 0x20, 0x45, 0x95,
  19605. 0x4a, 0x00, 0xca, 0x05, 0xeb, 0xd4, 0x1d, 0x48,
  19606. 0x81, 0x89, 0xe8, 0x86, 0x43, 0xfa, 0x28, 0x17,
  19607. 0x12
  19608. },
  19609. { 0 }, /* DHE */
  19610. { /* Hello 1 */
  19611. 0x03, 0x7c, 0x33, 0x75, 0xdc, 0xc5, 0x46, 0x3a,
  19612. 0x0d, 0x56, 0xc6, 0xfb, 0xab, 0x1e, 0x1d, 0xda,
  19613. 0x59, 0xc2, 0xb2, 0xb1, 0x7c, 0x48, 0x9b, 0x06,
  19614. 0x0a, 0x5a, 0xbb, 0xf8, 0x98, 0x53, 0x78, 0x2d,
  19615. 0xd2, 0xcc, 0x87, 0x68, 0x25, 0xdd, 0x88, 0x22,
  19616. 0xcd, 0xb7, 0x74, 0x55, 0x21, 0xf9, 0x34, 0x98
  19617. },
  19618. { /* Hello 2 */
  19619. 0x03, 0xb4, 0xfb, 0xcc, 0x28, 0x2c, 0xc1, 0x70,
  19620. 0x42, 0x73, 0x57, 0xac, 0xdb, 0x47, 0x71, 0xf6,
  19621. 0x2e, 0x11, 0x8a, 0x5b, 0x47, 0x2f, 0x02, 0x54,
  19622. 0x95, 0x34, 0xed, 0x5f, 0x19, 0xc1, 0x75, 0xe0,
  19623. 0x76, 0xad, 0xb0, 0x90, 0x57, 0xcd, 0xfd, 0xd7,
  19624. 0x58, 0x1f, 0x0d, 0x6b, 0x9e, 0x51, 0x3c, 0x08
  19625. },
  19626. { /* Finished 1 */
  19627. 0x2b, 0x50, 0xd9, 0xa7, 0x43, 0x24, 0xda, 0x2c,
  19628. 0x7a, 0xaa, 0x0e, 0x37, 0xd7, 0x6b, 0x2c, 0xab,
  19629. 0x8e, 0xb2, 0xfe, 0x31, 0x1b, 0xa8, 0x12, 0x59,
  19630. 0x5b, 0x7b, 0xdc, 0x3e, 0xa7, 0x86, 0xa5, 0x48,
  19631. 0xe4, 0x46, 0x2b, 0x4c, 0xc1, 0x66, 0x4b, 0xf3,
  19632. 0x2a, 0x99, 0x93, 0x08, 0xbc, 0x3d, 0x08, 0x76
  19633. },
  19634. { /* Finished 2 */
  19635. 0x7c, 0x34, 0xc8, 0x56, 0x17, 0xf1, 0x62, 0x1c,
  19636. 0x9f, 0x0b, 0xeb, 0xfd, 0x69, 0x72, 0x51, 0xc5,
  19637. 0xfa, 0x74, 0x87, 0xc9, 0xbd, 0x50, 0xe9, 0x48,
  19638. 0xa7, 0x3c, 0x94, 0x3e, 0x06, 0x7d, 0xe8, 0x8e,
  19639. 0xc1, 0xd1, 0x08, 0x1f, 0x5d, 0x48, 0x8a, 0x25,
  19640. 0xfc, 0xea, 0xe7, 0xd9, 0xd4, 0xd0, 0xf9, 0xad
  19641. },
  19642. {
  19643. 0x4b, 0x0b, 0xed, 0xb9, 0xc8, 0xb8, 0xa8, 0x1e, 0xb0, 0x81, 0x76, 0xd5,
  19644. 0x33, 0x22, 0x71, 0x33, 0x3a, 0x85, 0x19, 0x67, 0x7e, 0x91, 0x37, 0xf2,
  19645. 0xa6, 0x11, 0x22, 0xdf, 0x41, 0x04, 0x3d, 0xa9, 0x13, 0xb9, 0xb2, 0xb1,
  19646. 0xbb, 0xd8, 0xef, 0x23, 0x7c, 0xc2, 0xab, 0x70, 0x1b, 0x51, 0x9f, 0xc9
  19647. },
  19648. {
  19649. 0xeb, 0x96, 0x10, 0x8c, 0x7d, 0x92, 0xea, 0x80, 0x86, 0xb2, 0xf8, 0x27,
  19650. 0xf2, 0x9a, 0x09, 0xc1, 0x7c, 0x09, 0x43, 0xbc, 0xfe, 0xc8, 0x75, 0xe0,
  19651. 0x97, 0xe7, 0x6d, 0xd5, 0xb2, 0x3c, 0xed, 0x12, 0xb7, 0x74, 0x0e, 0xe3,
  19652. 0xb6, 0xe0, 0xba, 0xe1, 0x8d, 0x89, 0xcf, 0x4f, 0x57, 0xf6, 0x6d, 0x90
  19653. },
  19654. {
  19655. 0x22, 0xb0, 0x39, 0x34, 0xb6, 0x6c, 0x2d, 0x7a, 0x97, 0x1c, 0x5d, 0xcc,
  19656. 0x78, 0x84, 0x71, 0xbb, 0xc6, 0x7b, 0xb6, 0xbc, 0xcc, 0x0b, 0xf8, 0xac,
  19657. 0x8e, 0xd7, 0x20, 0xbd, 0xbe, 0x32, 0xf0, 0xd6, 0xe9, 0x69, 0x13, 0xf2,
  19658. 0x9a, 0xce, 0xfe, 0x86, 0xd3, 0xee, 0xba, 0x69, 0x51, 0xb6, 0x77, 0x56
  19659. },
  19660. {
  19661. 0x16, 0xfd, 0xda, 0xf3, 0x5e, 0xb9, 0xa6, 0x17, 0x24, 0xb2, 0x16, 0x9f,
  19662. 0xb6, 0x59, 0x13, 0x0f, 0x25, 0x5a, 0xf1, 0x5b, 0x5f, 0xe4, 0x54, 0x2a,
  19663. 0xa7, 0xbf, 0x29, 0xaf, 0x5a, 0x77, 0xf4, 0x4f, 0x25, 0xba, 0x94, 0xad,
  19664. 0x6b, 0x91, 0x3b, 0xe7, 0xd5, 0x73, 0x0d, 0xff, 0xaa, 0xe3, 0x72, 0x2c
  19665. },
  19666. {
  19667. 0x22, 0xb4, 0x94, 0xc0, 0x53, 0xd7, 0x82, 0x06, 0x38, 0x9d, 0x4a, 0xa0,
  19668. 0x3f, 0xf1, 0x5f, 0x6e, 0x23, 0x8d, 0x09, 0x62, 0xbf, 0x6f, 0x7c, 0x84,
  19669. 0xc6, 0x3e, 0x15, 0xad, 0x18, 0x37, 0x76, 0x29, 0xc7, 0xd6, 0x68, 0x0c,
  19670. 0x1e, 0xc6, 0x93, 0x31, 0xef, 0x85, 0x69, 0x30, 0x68, 0xf0, 0x1e, 0x37
  19671. },
  19672. {
  19673. 0x6d, 0x4d, 0x20, 0xaf, 0x47, 0xe8, 0x1b, 0xfa, 0xd0, 0xb6, 0xc8, 0x97,
  19674. 0xd1, 0x03, 0xfc, 0x9d, 0x59, 0xa0, 0x68, 0x9d, 0xe9, 0x17, 0x8b, 0xce,
  19675. 0x48, 0x2c, 0x77, 0x8a, 0x22, 0x4b, 0x5c, 0x54, 0x22, 0xa1, 0x15, 0x12,
  19676. 0xe1, 0x07, 0x8e, 0x15, 0xd8, 0x7b, 0x16, 0x65, 0x99, 0x6b, 0xcb, 0x71
  19677. },
  19678. {
  19679. 0x79, 0x64, 0x79, 0xdd, 0x75, 0x5c, 0x6f, 0x98, 0xac, 0x03, 0xe0, 0xcd,
  19680. 0x92, 0xba, 0x0e, 0x2d, 0xb4, 0xd1, 0x8b, 0x97, 0xd0, 0x85, 0xbb, 0x2e,
  19681. 0x4f, 0x26, 0x93, 0xf5, 0x1d, 0xf3, 0xd2, 0x43, 0x4f, 0xd2, 0x47, 0xaa,
  19682. 0x91, 0x1e, 0xf3, 0x67, 0x10, 0x18, 0x2c, 0xb9, 0x01, 0xba, 0x10, 0x9f
  19683. },
  19684. {
  19685. 0x79, 0xb6, 0x9c, 0xbe, 0xf1, 0x6a, 0xb0, 0x92, 0xa0, 0x29, 0x52, 0x61,
  19686. 0xf1, 0xcd, 0x3a, 0x67, 0xe1, 0x6b, 0xb8, 0x9d, 0x0d, 0x95, 0xb6, 0x03,
  19687. 0x80, 0x1f, 0xd5, 0x75, 0xb6, 0x1d, 0x79, 0x02, 0x93, 0x43, 0x77, 0xa7,
  19688. 0x9d, 0x2f, 0xc3, 0x84, 0xc6, 0x83, 0x76, 0x16, 0x06, 0x98, 0x7b, 0x79
  19689. }
  19690. },
  19691. #endif /* WOLFSSL_SHA384 */
  19692. };
  19693. const char protocolLabel[] = "tls13 ";
  19694. const char ceTrafficLabel[] = "c e traffic";
  19695. const char eExpMasterLabel[] = "e exp master";
  19696. const char cHsTrafficLabel[] = "c hs traffic";
  19697. const char sHsTrafficLabel[] = "s hs traffic";
  19698. const char cAppTrafficLabel[] = "c ap traffic";
  19699. const char sAppTrafficLabel[] = "s ap traffic";
  19700. const char expMasterLabel[] = "exp master";
  19701. const char resMasterLabel[] = "res master";
  19702. const char derivedLabel[] = "derived";
  19703. WOLFSSL_TEST_SUBROUTINE int tls13_kdf_test(void)
  19704. {
  19705. int ret = 0;
  19706. word32 i;
  19707. word32 tc = sizeof(tls13KdfTestVectors)/sizeof(Tls13KdfTestVector);
  19708. const Tls13KdfTestVector* tv = NULL;
  19709. for (i = 0, tv = tls13KdfTestVectors; i < tc; i++, tv++) {
  19710. byte output[WC_MAX_DIGEST_SIZE];
  19711. byte secret[WC_MAX_DIGEST_SIZE];
  19712. byte salt[WC_MAX_DIGEST_SIZE];
  19713. byte zeroes[WC_MAX_DIGEST_SIZE];
  19714. byte hashZero[WC_MAX_DIGEST_SIZE];
  19715. int hashAlgSz;
  19716. XMEMSET(zeroes, 0, sizeof zeroes);
  19717. hashAlgSz = wc_HashGetDigestSize(tv->hashAlg);
  19718. if (hashAlgSz == BAD_FUNC_ARG) break;
  19719. ret = wc_Hash(tv->hashAlg, NULL, 0, hashZero, hashAlgSz);
  19720. if (ret != 0) break;
  19721. ret = wc_Tls13_HKDF_Extract(secret, NULL, 0,
  19722. (tv->pskSz == 0) ? zeroes : (byte*)tv->psk,
  19723. tv->pskSz, tv->hashAlg);
  19724. if (ret != 0) break;
  19725. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19726. secret, hashAlgSz,
  19727. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  19728. (byte*)ceTrafficLabel, (word32)XSTRLEN(ceTrafficLabel),
  19729. tv->hashHello1, hashAlgSz, tv->hashAlg);
  19730. if (ret != 0) break;
  19731. ret = XMEMCMP(tv->clientEarlyTrafficSecret, output, hashAlgSz);
  19732. if (ret != 0) break;
  19733. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19734. secret, hashAlgSz,
  19735. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  19736. (byte*)eExpMasterLabel, (word32)XSTRLEN(eExpMasterLabel),
  19737. tv->hashHello1, hashAlgSz, tv->hashAlg);
  19738. if (ret != 0) break;
  19739. ret = XMEMCMP(tv->earlyExporterMasterSecret, output, hashAlgSz);
  19740. if (ret != 0) break;
  19741. ret = wc_Tls13_HKDF_Expand_Label(salt, hashAlgSz,
  19742. secret, hashAlgSz,
  19743. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  19744. (byte*)derivedLabel, (word32)XSTRLEN(derivedLabel),
  19745. hashZero, hashAlgSz, tv->hashAlg);
  19746. if (ret != 0) break;
  19747. ret = wc_Tls13_HKDF_Extract(secret, salt, hashAlgSz,
  19748. (tv->dheSz == 0) ? zeroes : (byte*)tv->dhe,
  19749. tv->dheSz, tv->hashAlg);
  19750. if (ret != 0) break;
  19751. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19752. secret, hashAlgSz,
  19753. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  19754. (byte*)cHsTrafficLabel, (word32)XSTRLEN(cHsTrafficLabel),
  19755. tv->hashHello2, hashAlgSz, tv->hashAlg);
  19756. if (ret != 0) break;
  19757. ret = XMEMCMP(tv->clientHandshakeTrafficSecret,
  19758. output, hashAlgSz);
  19759. if (ret != 0) break;
  19760. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19761. secret, hashAlgSz,
  19762. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  19763. (byte*)sHsTrafficLabel, (word32)XSTRLEN(sHsTrafficLabel),
  19764. tv->hashHello2, hashAlgSz, tv->hashAlg);
  19765. if (ret != 0) break;
  19766. ret = XMEMCMP(tv->serverHandshakeTrafficSecret, output, hashAlgSz);
  19767. if (ret != 0) break;
  19768. ret = wc_Tls13_HKDF_Expand_Label(salt, hashAlgSz,
  19769. secret, hashAlgSz,
  19770. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  19771. (byte*)derivedLabel, (word32)XSTRLEN(derivedLabel),
  19772. hashZero, hashAlgSz, tv->hashAlg);
  19773. if (ret != 0) break;
  19774. ret = wc_Tls13_HKDF_Extract(secret, salt, hashAlgSz,
  19775. zeroes, hashAlgSz, tv->hashAlg);
  19776. if (ret != 0) break;
  19777. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19778. secret, hashAlgSz,
  19779. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  19780. (byte*)cAppTrafficLabel, (word32)XSTRLEN(cAppTrafficLabel),
  19781. tv->hashFinished1, hashAlgSz, tv->hashAlg);
  19782. if (ret != 0) break;
  19783. ret = XMEMCMP(tv->clientApplicationTrafficSecret, output, hashAlgSz);
  19784. if (ret != 0) break;
  19785. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19786. secret, hashAlgSz,
  19787. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  19788. (byte*)sAppTrafficLabel, (word32)XSTRLEN(sAppTrafficLabel),
  19789. tv->hashFinished1, hashAlgSz, tv->hashAlg);
  19790. if (ret != 0) break;
  19791. ret = XMEMCMP(tv->serverApplicationTrafficSecret, output, hashAlgSz);
  19792. if (ret != 0) break;
  19793. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19794. secret, hashAlgSz,
  19795. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  19796. (byte*)expMasterLabel, (word32)XSTRLEN(expMasterLabel),
  19797. tv->hashFinished1, hashAlgSz, tv->hashAlg);
  19798. if (ret != 0) break;
  19799. ret = XMEMCMP(tv->exporterMasterSecret, output, hashAlgSz);
  19800. if (ret != 0) break;
  19801. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19802. secret, hashAlgSz,
  19803. (byte*)protocolLabel, (word32)XSTRLEN(protocolLabel),
  19804. (byte*)resMasterLabel, (word32)XSTRLEN(resMasterLabel),
  19805. tv->hashFinished2, hashAlgSz, tv->hashAlg);
  19806. if (ret != 0) break;
  19807. ret = XMEMCMP(tv->resumptionMasterSecret, output, hashAlgSz);
  19808. if (ret != 0) break;
  19809. }
  19810. return ret;
  19811. }
  19812. #endif /* WOLFSSL_TLS13 */
  19813. #if defined(HAVE_ECC) && defined(HAVE_X963_KDF)
  19814. WOLFSSL_TEST_SUBROUTINE int x963kdf_test(void)
  19815. {
  19816. int ret;
  19817. byte kek[128];
  19818. #ifndef NO_SHA
  19819. /* SHA-1, COUNT = 0
  19820. * shared secret length: 192
  19821. * SharedInfo length: 0
  19822. * key data length: 128
  19823. */
  19824. WOLFSSL_SMALL_STACK_STATIC const byte Z[] = {
  19825. 0x1c, 0x7d, 0x7b, 0x5f, 0x05, 0x97, 0xb0, 0x3d,
  19826. 0x06, 0xa0, 0x18, 0x46, 0x6e, 0xd1, 0xa9, 0x3e,
  19827. 0x30, 0xed, 0x4b, 0x04, 0xdc, 0x64, 0xcc, 0xdd
  19828. };
  19829. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  19830. 0xbf, 0x71, 0xdf, 0xfd, 0x8f, 0x4d, 0x99, 0x22,
  19831. 0x39, 0x36, 0xbe, 0xb4, 0x6f, 0xee, 0x8c, 0xcc
  19832. };
  19833. #endif
  19834. #ifndef NO_SHA256
  19835. /* SHA-256, COUNT = 3
  19836. * shared secret length: 192
  19837. * SharedInfo length: 0
  19838. * key data length: 128
  19839. */
  19840. WOLFSSL_SMALL_STACK_STATIC const byte Z2[] = {
  19841. 0xd3, 0x8b, 0xdb, 0xe5, 0xc4, 0xfc, 0x16, 0x4c,
  19842. 0xdd, 0x96, 0x7f, 0x63, 0xc0, 0x4f, 0xe0, 0x7b,
  19843. 0x60, 0xcd, 0xe8, 0x81, 0xc2, 0x46, 0x43, 0x8c
  19844. };
  19845. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  19846. 0x5e, 0x67, 0x4d, 0xb9, 0x71, 0xba, 0xc2, 0x0a,
  19847. 0x80, 0xba, 0xd0, 0xd4, 0x51, 0x4d, 0xc4, 0x84
  19848. };
  19849. #endif
  19850. #ifdef WOLFSSL_SHA512
  19851. /* SHA-512, COUNT = 0
  19852. * shared secret length: 192
  19853. * SharedInfo length: 0
  19854. * key data length: 128
  19855. */
  19856. WOLFSSL_SMALL_STACK_STATIC const byte Z3[] = {
  19857. 0x87, 0xfc, 0x0d, 0x8c, 0x44, 0x77, 0x48, 0x5b,
  19858. 0xb5, 0x74, 0xf5, 0xfc, 0xea, 0x26, 0x4b, 0x30,
  19859. 0x88, 0x5d, 0xc8, 0xd9, 0x0a, 0xd8, 0x27, 0x82
  19860. };
  19861. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  19862. 0x94, 0x76, 0x65, 0xfb, 0xb9, 0x15, 0x21, 0x53,
  19863. 0xef, 0x46, 0x02, 0x38, 0x50, 0x6a, 0x02, 0x45
  19864. };
  19865. /* SHA-512, COUNT = 0
  19866. * shared secret length: 521
  19867. * SharedInfo length: 128
  19868. * key data length: 1024
  19869. */
  19870. WOLFSSL_SMALL_STACK_STATIC const byte Z4[] = {
  19871. 0x00, 0xaa, 0x5b, 0xb7, 0x9b, 0x33, 0xe3, 0x89,
  19872. 0xfa, 0x58, 0xce, 0xad, 0xc0, 0x47, 0x19, 0x7f,
  19873. 0x14, 0xe7, 0x37, 0x12, 0xf4, 0x52, 0xca, 0xa9,
  19874. 0xfc, 0x4c, 0x9a, 0xdb, 0x36, 0x93, 0x48, 0xb8,
  19875. 0x15, 0x07, 0x39, 0x2f, 0x1a, 0x86, 0xdd, 0xfd,
  19876. 0xb7, 0xc4, 0xff, 0x82, 0x31, 0xc4, 0xbd, 0x0f,
  19877. 0x44, 0xe4, 0x4a, 0x1b, 0x55, 0xb1, 0x40, 0x47,
  19878. 0x47, 0xa9, 0xe2, 0xe7, 0x53, 0xf5, 0x5e, 0xf0,
  19879. 0x5a, 0x2d
  19880. };
  19881. WOLFSSL_SMALL_STACK_STATIC const byte info4[] = {
  19882. 0xe3, 0xb5, 0xb4, 0xc1, 0xb0, 0xd5, 0xcf, 0x1d,
  19883. 0x2b, 0x3a, 0x2f, 0x99, 0x37, 0x89, 0x5d, 0x31
  19884. };
  19885. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  19886. 0x44, 0x63, 0xf8, 0x69, 0xf3, 0xcc, 0x18, 0x76,
  19887. 0x9b, 0x52, 0x26, 0x4b, 0x01, 0x12, 0xb5, 0x85,
  19888. 0x8f, 0x7a, 0xd3, 0x2a, 0x5a, 0x2d, 0x96, 0xd8,
  19889. 0xcf, 0xfa, 0xbf, 0x7f, 0xa7, 0x33, 0x63, 0x3d,
  19890. 0x6e, 0x4d, 0xd2, 0xa5, 0x99, 0xac, 0xce, 0xb3,
  19891. 0xea, 0x54, 0xa6, 0x21, 0x7c, 0xe0, 0xb5, 0x0e,
  19892. 0xef, 0x4f, 0x6b, 0x40, 0xa5, 0xc3, 0x02, 0x50,
  19893. 0xa5, 0xa8, 0xee, 0xee, 0x20, 0x80, 0x02, 0x26,
  19894. 0x70, 0x89, 0xdb, 0xf3, 0x51, 0xf3, 0xf5, 0x02,
  19895. 0x2a, 0xa9, 0x63, 0x8b, 0xf1, 0xee, 0x41, 0x9d,
  19896. 0xea, 0x9c, 0x4f, 0xf7, 0x45, 0xa2, 0x5a, 0xc2,
  19897. 0x7b, 0xda, 0x33, 0xca, 0x08, 0xbd, 0x56, 0xdd,
  19898. 0x1a, 0x59, 0xb4, 0x10, 0x6c, 0xf2, 0xdb, 0xbc,
  19899. 0x0a, 0xb2, 0xaa, 0x8e, 0x2e, 0xfa, 0x7b, 0x17,
  19900. 0x90, 0x2d, 0x34, 0x27, 0x69, 0x51, 0xce, 0xcc,
  19901. 0xab, 0x87, 0xf9, 0x66, 0x1c, 0x3e, 0x88, 0x16
  19902. };
  19903. #endif
  19904. #ifndef NO_SHA
  19905. ret = wc_X963_KDF(WC_HASH_TYPE_SHA, Z, sizeof(Z), NULL, 0,
  19906. kek, sizeof(verify));
  19907. if (ret != 0)
  19908. return -9800;
  19909. if (XMEMCMP(verify, kek, sizeof(verify)) != 0)
  19910. return -9801;
  19911. #endif
  19912. #ifndef NO_SHA256
  19913. ret = wc_X963_KDF(WC_HASH_TYPE_SHA256, Z2, sizeof(Z2), NULL, 0,
  19914. kek, sizeof(verify2));
  19915. if (ret != 0)
  19916. return -9802;
  19917. if (XMEMCMP(verify2, kek, sizeof(verify2)) != 0)
  19918. return -9803;
  19919. #endif
  19920. #ifdef WOLFSSL_SHA512
  19921. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z3, sizeof(Z3), NULL, 0,
  19922. kek, sizeof(verify3));
  19923. if (ret != 0)
  19924. return -9804;
  19925. if (XMEMCMP(verify3, kek, sizeof(verify3)) != 0)
  19926. return -9805;
  19927. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z4, sizeof(Z4), info4,
  19928. sizeof(info4), kek, sizeof(verify4));
  19929. if (ret != 0)
  19930. return -9806;
  19931. if (XMEMCMP(verify4, kek, sizeof(verify4)) != 0)
  19932. return -9807;
  19933. #endif
  19934. return 0;
  19935. }
  19936. #endif /* HAVE_X963_KDF */
  19937. #if defined(HAVE_HPKE) && (defined(HAVE_ECC) || defined(HAVE_CURVE25519)) && \
  19938. defined(HAVE_AESGCM)
  19939. static int hpke_test_single(Hpke* hpke)
  19940. {
  19941. int ret = 0;
  19942. int rngRet = 0;
  19943. WC_RNG rng[1];
  19944. const char* start_text = "this is a test";
  19945. const char* info_text = "info";
  19946. const char* aad_text = "aad";
  19947. byte ciphertext[MAX_HPKE_LABEL_SZ];
  19948. byte plaintext[MAX_HPKE_LABEL_SZ];
  19949. void* receiverKey = NULL;
  19950. void* ephemeralKey = NULL;
  19951. #ifdef WOLFSSL_SMALL_STACK
  19952. uint8_t *pubKey = NULL; /* public key */
  19953. word16 pubKeySz = (word16)HPKE_Npk_MAX;
  19954. #else
  19955. uint8_t pubKey[HPKE_Npk_MAX]; /* public key */
  19956. word16 pubKeySz = (word16)sizeof(pubKey);
  19957. #endif
  19958. rngRet = ret = wc_InitRng(rng);
  19959. if (ret != 0)
  19960. return ret;
  19961. #ifdef WOLFSSL_SMALL_STACK
  19962. if (ret == 0) {
  19963. pubKey = (uint8_t *)XMALLOC(pubKeySz, HEAP_HINT,
  19964. DYNAMIC_TYPE_TMP_BUFFER);
  19965. if (pubKey == NULL)
  19966. ret = MEMORY_E;
  19967. }
  19968. #endif
  19969. /* generate the keys */
  19970. if (ret == 0)
  19971. ret = wc_HpkeGenerateKeyPair(hpke, &ephemeralKey, rng);
  19972. if (ret == 0)
  19973. ret = wc_HpkeGenerateKeyPair(hpke, &receiverKey, rng);
  19974. /* seal */
  19975. if (ret == 0) {
  19976. ret = wc_HpkeSealBase(hpke, ephemeralKey, receiverKey,
  19977. (byte*)info_text, (word32)XSTRLEN(info_text),
  19978. (byte*)aad_text, (word32)XSTRLEN(aad_text),
  19979. (byte*)start_text, (word32)XSTRLEN(start_text),
  19980. ciphertext);
  19981. }
  19982. /* export ephemeral key */
  19983. if (ret == 0)
  19984. ret = wc_HpkeSerializePublicKey(hpke, ephemeralKey, pubKey, &pubKeySz);
  19985. /* open with exported ephemeral key */
  19986. if (ret == 0) {
  19987. ret = wc_HpkeOpenBase(hpke, receiverKey, pubKey, pubKeySz,
  19988. (byte*)info_text, (word32)XSTRLEN(info_text),
  19989. (byte*)aad_text, (word32)XSTRLEN(aad_text),
  19990. ciphertext, (word32)XSTRLEN(start_text),
  19991. plaintext);
  19992. }
  19993. if (ret == 0)
  19994. ret = XMEMCMP(plaintext, start_text, XSTRLEN(start_text));
  19995. if (ephemeralKey != NULL)
  19996. wc_HpkeFreeKey(hpke, hpke->kem, ephemeralKey, hpke->heap);
  19997. if (receiverKey != NULL)
  19998. wc_HpkeFreeKey(hpke, hpke->kem, receiverKey, hpke->heap);
  19999. #ifdef WOLFSSL_SMALL_STACK
  20000. if (pubKey != NULL)
  20001. XFREE(pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20002. #endif
  20003. if (rngRet == 0)
  20004. wc_FreeRng(rng);
  20005. return ret;
  20006. }
  20007. WOLFSSL_TEST_SUBROUTINE int hpke_test(void)
  20008. {
  20009. int ret = 0;
  20010. Hpke hpke[1];
  20011. #if defined(HAVE_ECC)
  20012. #if defined(WOLFSSL_SHA224) || !defined(NO_SHA256)
  20013. /* p256 */
  20014. ret = wc_HpkeInit(hpke, DHKEM_P256_HKDF_SHA256, HKDF_SHA256,
  20015. HPKE_AES_128_GCM, NULL);
  20016. if (ret != 0)
  20017. return ret;
  20018. ret = hpke_test_single(hpke);
  20019. if (ret != 0)
  20020. return ret;
  20021. #endif
  20022. #if defined(WOLFSSL_SHA384) && \
  20023. (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES))
  20024. /* p384 */
  20025. ret = wc_HpkeInit(hpke, DHKEM_P384_HKDF_SHA384, HKDF_SHA384,
  20026. HPKE_AES_128_GCM, NULL);
  20027. if (ret != 0)
  20028. return ret;
  20029. ret = hpke_test_single(hpke);
  20030. if (ret != 0)
  20031. return ret;
  20032. #endif
  20033. #if (defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)) && \
  20034. (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES))
  20035. /* p521 */
  20036. ret = wc_HpkeInit(hpke, DHKEM_P521_HKDF_SHA512, HKDF_SHA512,
  20037. HPKE_AES_128_GCM, NULL);
  20038. if (ret != 0)
  20039. return ret;
  20040. ret = hpke_test_single(hpke);
  20041. if (ret != 0)
  20042. return ret;
  20043. #endif
  20044. #endif
  20045. #if defined(HAVE_CURVE25519)
  20046. /* test with curve25519 and aes256 */
  20047. ret = wc_HpkeInit(hpke, DHKEM_X25519_HKDF_SHA256, HKDF_SHA256,
  20048. HPKE_AES_256_GCM, NULL);
  20049. if (ret != 0)
  20050. return ret;
  20051. ret = hpke_test_single(hpke);
  20052. if (ret != 0)
  20053. return ret;
  20054. #endif
  20055. return ret;
  20056. /* x448 and chacha20 are unimplemented */
  20057. }
  20058. #endif /* HAVE_HPKE && HAVE_ECC && HAVE_AESGCM */
  20059. #ifdef HAVE_ECC
  20060. /* size to use for ECC key gen tests */
  20061. #ifndef ECC_KEYGEN_SIZE
  20062. #ifndef NO_ECC256
  20063. #define ECC_KEYGEN_SIZE 32
  20064. #elif defined(HAVE_ECC384)
  20065. #define ECC_KEYGEN_SIZE 48
  20066. #elif defined(HAVE_ECC224)
  20067. #define ECC_KEYGEN_SIZE 28
  20068. #elif defined(HAVE_ECC521)
  20069. #define ECC_KEYGEN_SIZE 66
  20070. #else
  20071. #error No ECC keygen size defined for test
  20072. #endif
  20073. #endif
  20074. #ifdef BENCH_EMBEDDED
  20075. #define ECC_SHARED_SIZE 128
  20076. #else
  20077. #define ECC_SHARED_SIZE MAX_ECC_BYTES
  20078. #endif
  20079. #if defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  20080. #define HAVE_ECC_DETERMINISTIC_K
  20081. #define ECC_DIGEST_SIZE WC_SHA256_DIGEST_SIZE
  20082. #else
  20083. #define ECC_DIGEST_SIZE MAX_ECC_BYTES
  20084. #endif
  20085. #define ECC_SIG_SIZE ECC_MAX_SIG_SIZE
  20086. #ifndef NO_ECC_VECTOR_TEST
  20087. #if (defined(HAVE_ECC192) || defined(HAVE_ECC224) ||\
  20088. !defined(NO_ECC256) || defined(HAVE_ECC384) ||\
  20089. defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES))
  20090. #define HAVE_ECC_VECTOR_TEST
  20091. #endif
  20092. #endif
  20093. #ifdef HAVE_ECC_VECTOR_TEST
  20094. typedef struct eccVector {
  20095. const char* msg; /* SHA-1 Encoded Message */
  20096. const char* Qx;
  20097. const char* Qy;
  20098. const char* d; /* Private Key */
  20099. const char* R;
  20100. const char* S;
  20101. const char* curveName;
  20102. word32 msgLen;
  20103. word32 keySize;
  20104. #ifndef NO_ASN
  20105. const byte* r;
  20106. word32 rSz;
  20107. const byte* s;
  20108. word32 sSz;
  20109. #endif
  20110. } eccVector;
  20111. #if !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  20112. static int ecc_test_vector_item(const eccVector* vector)
  20113. {
  20114. int ret = 0, verify = 0;
  20115. word32 sigSz;
  20116. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20117. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20118. #else
  20119. ecc_key userA[1];
  20120. #endif
  20121. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  20122. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  20123. word32 sigRawSz, rSz = MAX_ECC_BYTES, sSz = MAX_ECC_BYTES;
  20124. WC_DECLARE_VAR(sigRaw, byte, ECC_SIG_SIZE, HEAP_HINT);
  20125. WC_DECLARE_VAR(r, byte, MAX_ECC_BYTES, HEAP_HINT);
  20126. WC_DECLARE_VAR(s, byte, MAX_ECC_BYTES, HEAP_HINT);
  20127. #endif
  20128. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  20129. if (sig == NULL)
  20130. ERROR_OUT(MEMORY_E, done);
  20131. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  20132. if (sigRaw == NULL || r == NULL || s == NULL)
  20133. ERROR_OUT(MEMORY_E, done);
  20134. #endif
  20135. #endif
  20136. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20137. if (userA == NULL)
  20138. ERROR_OUT(MEMORY_E, done);
  20139. #endif
  20140. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  20141. if (ret != 0)
  20142. goto done;
  20143. ret = wc_ecc_import_raw(userA, vector->Qx, vector->Qy,
  20144. vector->d, vector->curveName);
  20145. if (ret != 0)
  20146. goto done;
  20147. XMEMSET(sig, 0, ECC_SIG_SIZE);
  20148. sigSz = ECC_SIG_SIZE;
  20149. ret = wc_ecc_rs_to_sig(vector->R, vector->S, sig, &sigSz);
  20150. if (ret != 0)
  20151. goto done;
  20152. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  20153. XMEMSET(sigRaw, 0, ECC_SIG_SIZE);
  20154. sigRawSz = ECC_SIG_SIZE;
  20155. ret = wc_ecc_rs_raw_to_sig(vector->r, vector->rSz, vector->s, vector->sSz,
  20156. sigRaw, &sigRawSz);
  20157. if (ret != 0)
  20158. goto done;
  20159. if (sigSz != sigRawSz || XMEMCMP(sig, sigRaw, sigSz) != 0) {
  20160. ret = -9810;
  20161. goto done;
  20162. }
  20163. ret = wc_ecc_sig_to_rs(sig, sigSz, r, &rSz, s, &sSz);
  20164. if (ret != 0)
  20165. goto done;
  20166. if (rSz != vector->rSz || XMEMCMP(r, vector->r, rSz) != 0 ||
  20167. sSz != vector->sSz || XMEMCMP(s, vector->s, sSz) != 0) {
  20168. ret = -9811;
  20169. goto done;
  20170. }
  20171. #endif
  20172. #ifdef HAVE_ECC_VERIFY
  20173. do {
  20174. #if defined(WOLFSSL_ASYNC_CRYPT)
  20175. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20176. #endif
  20177. if (ret == 0)
  20178. ret = wc_ecc_verify_hash(sig, sigSz, (byte*)vector->msg,
  20179. vector->msgLen, &verify, userA);
  20180. } while (ret == WC_PENDING_E);
  20181. if (ret != 0)
  20182. goto done;
  20183. TEST_SLEEP();
  20184. if (verify != 1)
  20185. ret = -9812;
  20186. #endif
  20187. done:
  20188. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20189. if (userA != NULL) {
  20190. wc_ecc_free(userA);
  20191. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20192. }
  20193. #else
  20194. wc_ecc_free(userA);
  20195. #endif
  20196. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  20197. WC_FREE_VAR(sigRaw, HEAP_HINT);
  20198. WC_FREE_VAR(r, HEAP_HINT);
  20199. WC_FREE_VAR(s, HEAP_HINT);
  20200. #endif
  20201. WC_FREE_VAR(sig, HEAP_HINT);
  20202. return ret;
  20203. }
  20204. static int ecc_test_vector(int keySize)
  20205. {
  20206. int ret;
  20207. eccVector vec;
  20208. XMEMSET(&vec, 0, sizeof(vec));
  20209. vec.keySize = (word32)keySize;
  20210. switch(keySize) {
  20211. #if defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)
  20212. case 14:
  20213. return 0;
  20214. #endif /* HAVE_ECC112 */
  20215. #if defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)
  20216. case 16:
  20217. return 0;
  20218. #endif /* HAVE_ECC128 */
  20219. #if defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)
  20220. case 20:
  20221. return 0;
  20222. #endif /* HAVE_ECC160 */
  20223. #if defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)
  20224. case 24:
  20225. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  20226. #if 1
  20227. vec.msg = "\x60\x80\x79\x42\x3f\x12\x42\x1d\xe6\x16\xb7\x49\x3e\xbe\x55\x1c\xf4\xd6\x5b\x92";
  20228. vec.msgLen = 20;
  20229. #else
  20230. /* This is the raw message prior to SHA-1 */
  20231. vec.msg =
  20232. "\xeb\xf7\x48\xd7\x48\xeb\xbc\xa7\xd2\x9f\xb4\x73\x69\x8a\x6e\x6b"
  20233. "\x4f\xb1\x0c\x86\x5d\x4a\xf0\x24\xcc\x39\xae\x3d\xf3\x46\x4b\xa4"
  20234. "\xf1\xd6\xd4\x0f\x32\xbf\x96\x18\xa9\x1b\xb5\x98\x6f\xa1\xa2\xaf"
  20235. "\x04\x8a\x0e\x14\xdc\x51\xe5\x26\x7e\xb0\x5e\x12\x7d\x68\x9d\x0a"
  20236. "\xc6\xf1\xa7\xf1\x56\xce\x06\x63\x16\xb9\x71\xcc\x7a\x11\xd0\xfd"
  20237. "\x7a\x20\x93\xe2\x7c\xf2\xd0\x87\x27\xa4\xe6\x74\x8c\xc3\x2f\xd5"
  20238. "\x9c\x78\x10\xc5\xb9\x01\x9d\xf2\x1c\xdc\xc0\xbc\xa4\x32\xc0\xa3"
  20239. "\xee\xd0\x78\x53\x87\x50\x88\x77\x11\x43\x59\xce\xe4\xa0\x71\xcf";
  20240. vec.msgLen = 128;
  20241. #endif
  20242. vec.Qx = "07008ea40b08dbe76432096e80a2494c94982d2d5bcf98e6";
  20243. vec.Qy = "76fab681d00b414ea636ba215de26d98c41bd7f2e4d65477";
  20244. vec.d = "e14f37b3d1374ff8b03f41b9b3fdd2f0ebccf275d660d7f3";
  20245. vec.R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  20246. vec.S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  20247. vec.curveName = "SECP192R1";
  20248. #ifndef NO_ASN
  20249. vec.r = (byte*)"\x69\x94\xd9\x62\xbd\xd0\xd7\x93\xff\xdd\xf8\x55"
  20250. "\xec\x5b\xf2\xf9\x1a\x96\x98\xb4\x62\x58\xa6\x3e";
  20251. vec.rSz = 24;
  20252. vec.s = (byte*)"\x02\xba\x64\x65\xa2\x34\x90\x37\x44\xab\x02\xbc"
  20253. "\x85\x21\x40\x5b\x73\xcf\x5f\xc0\x0e\x1a\x9f\x41";
  20254. vec.sSz = 24;
  20255. #endif
  20256. break;
  20257. #endif /* HAVE_ECC192 */
  20258. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  20259. case 28:
  20260. /* first [P-224,SHA-1] vector from FIPS 186-3 NIST vectors */
  20261. #if 1
  20262. vec.msg = "\xb9\xa3\xb8\x6d\xb0\xba\x99\xfd\xc6\xd2\x94\x6b\xfe\xbe\x9c\xe8\x3f\x10\x74\xfc";
  20263. vec.msgLen = 20;
  20264. #else
  20265. /* This is the raw message prior to SHA-1 */
  20266. vec.msg =
  20267. "\x36\xc8\xb2\x29\x86\x48\x7f\x67\x7c\x18\xd0\x97\x2a\x9e\x20\x47"
  20268. "\xb3\xaf\xa5\x9e\xc1\x62\x76\x4e\xc3\x0b\x5b\x69\xe0\x63\x0f\x99"
  20269. "\x0d\x4e\x05\xc2\x73\xb0\xe5\xa9\xd4\x28\x27\xb6\x95\xfc\x2d\x64"
  20270. "\xd9\x13\x8b\x1c\xf4\xc1\x21\x55\x89\x4c\x42\x13\x21\xa7\xbb\x97"
  20271. "\x0b\xdc\xe0\xfb\xf0\xd2\xae\x85\x61\xaa\xd8\x71\x7f\x2e\x46\xdf"
  20272. "\xe3\xff\x8d\xea\xb4\xd7\x93\x23\x56\x03\x2c\x15\x13\x0d\x59\x9e"
  20273. "\x26\xc1\x0f\x2f\xec\x96\x30\x31\xac\x69\x38\xa1\x8d\x66\x45\x38"
  20274. "\xb9\x4d\xac\x55\x34\xef\x7b\x59\x94\x24\xd6\x9b\xe1\xf7\x1c\x20";
  20275. vec.msgLen = 128;
  20276. #endif
  20277. vec.Qx = "8a4dca35136c4b70e588e23554637ae251077d1365a6ba5db9585de7";
  20278. vec.Qy = "ad3dee06de0be8279d4af435d7245f14f3b4f82eb578e519ee0057b1";
  20279. vec.d = "97c4b796e1639dd1035b708fc00dc7ba1682cec44a1002a1a820619f";
  20280. vec.R = "147b33758321e722a0360a4719738af848449e2c1d08defebc1671a7";
  20281. vec.S = "24fc7ed7f1352ca3872aa0916191289e2e04d454935d50fe6af3ad5b";
  20282. vec.curveName = "SECP224R1";
  20283. #ifndef NO_ASN
  20284. vec.r = (byte*)"\x14\x7b\x33\x75\x83\x21\xe7\x22\xa0\x36\x0a\x47"
  20285. "\x19\x73\x8a\xf8\x48\x44\x9e\x2c\x1d\x08\xde\xfe"
  20286. "\xbc\x16\x71\xa7";
  20287. vec.rSz = 28;
  20288. vec.s = (byte*)"\x24\xfc\x7e\xd7\xf1\x35\x2c\xa3\x87\x2a\xa0\x91"
  20289. "\x61\x91\x28\x9e\x2e\x04\xd4\x54\x93\x5d\x50\xfe"
  20290. "\x6a\xf3\xad\x5b";
  20291. vec.sSz = 28;
  20292. #endif
  20293. break;
  20294. #endif /* HAVE_ECC224 */
  20295. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  20296. case 30:
  20297. return 0;
  20298. #endif /* HAVE_ECC239 */
  20299. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  20300. case 32:
  20301. /* first [P-256,SHA-1] vector from FIPS 186-3 NIST vectors */
  20302. #if 1
  20303. vec.msg = "\xa3\xf9\x1a\xe2\x1b\xa6\xb3\x03\x98\x64\x47\x2f\x18\x41\x44\xc6\xaf\x62\xcd\x0e";
  20304. vec.msgLen = 20;
  20305. #else
  20306. /* This is the raw message prior to SHA-1 */
  20307. vec.msg =
  20308. "\xa2\x4b\x21\x76\x2e\x6e\xdb\x15\x3c\xc1\x14\x38\xdb\x0e\x92\xcd"
  20309. "\xf5\x2b\x86\xb0\x6c\xa9\x70\x16\x06\x27\x59\xc7\x0d\x36\xd1\x56"
  20310. "\x2c\xc9\x63\x0d\x7f\xc7\xc7\x74\xb2\x8b\x54\xe3\x1e\xf5\x58\x72"
  20311. "\xb2\xa6\x5d\xf1\xd7\xec\x26\xde\xbb\x33\xe7\xd9\x27\xef\xcc\xf4"
  20312. "\x6b\x63\xde\x52\xa4\xf4\x31\xea\xca\x59\xb0\x5d\x2e\xde\xc4\x84"
  20313. "\x5f\xff\xc0\xee\x15\x03\x94\xd6\x1f\x3d\xfe\xcb\xcd\xbf\x6f\x5a"
  20314. "\x73\x38\xd0\xbe\x3f\x2a\x77\x34\x51\x98\x3e\xba\xeb\x48\xf6\x73"
  20315. "\x8f\xc8\x95\xdf\x35\x7e\x1a\x48\xa6\x53\xbb\x35\x5a\x31\xa1\xb4"
  20316. vec.msgLen = 128;
  20317. #endif
  20318. vec.Qx = "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  20319. vec.Qy = "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  20320. vec.d = "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  20321. vec.R = "2b826f5d44e2d0b6de531ad96b51e8f0c56fdfead3c236892e4d84eacfc3b75c";
  20322. vec.S = "a2248b62c03db35a7cd63e8a120a3521a89d3d2f61ff99035a2148ae32e3a248";
  20323. #ifndef NO_ASN
  20324. vec.r = (byte*)"\x2b\x82\x6f\x5d\x44\xe2\xd0\xb6\xde\x53\x1a\xd9"
  20325. "\x6b\x51\xe8\xf0\xc5\x6f\xdf\xea\xd3\xc2\x36\x89"
  20326. "\x2e\x4d\x84\xea\xcf\xc3\xb7\x5c";
  20327. vec.rSz = 32;
  20328. vec.s = (byte*)"\xa2\x24\x8b\x62\xc0\x3d\xb3\x5a\x7c\xd6\x3e\x8a"
  20329. "\x12\x0a\x35\x21\xa8\x9d\x3d\x2f\x61\xff\x99\x03"
  20330. "\x5a\x21\x48\xae\x32\xe3\xa2\x48";
  20331. vec.sSz = 32;
  20332. #endif
  20333. vec.curveName = "SECP256R1";
  20334. break;
  20335. #endif /* !NO_ECC256 */
  20336. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  20337. case 40:
  20338. return 0;
  20339. #endif /* HAVE_ECC320 */
  20340. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  20341. case 48:
  20342. /* first [P-384,SHA-1] vector from FIPS 186-3 NIST vectors */
  20343. #if 1
  20344. vec.msg = "\x9b\x9f\x8c\x95\x35\xa5\xca\x26\x60\x5d\xb7\xf2\xfa\x57\x3b\xdf\xc3\x2e\xab\x8b";
  20345. vec.msgLen = 20;
  20346. #else
  20347. /* This is the raw message prior to SHA-1 */
  20348. vec.msg =
  20349. "\xab\xe1\x0a\xce\x13\xe7\xe1\xd9\x18\x6c\x48\xf7\x88\x9d\x51\x47"
  20350. "\x3d\x3a\x09\x61\x98\x4b\xc8\x72\xdf\x70\x8e\xcc\x3e\xd3\xb8\x16"
  20351. "\x9d\x01\xe3\xd9\x6f\xc4\xf1\xd5\xea\x00\xa0\x36\x92\xbc\xc5\xcf"
  20352. "\xfd\x53\x78\x7c\x88\xb9\x34\xaf\x40\x4c\x03\x9d\x32\x89\xb5\xba"
  20353. "\xc5\xae\x7d\xb1\x49\x68\x75\xb5\xdc\x73\xc3\x09\xf9\x25\xc1\x3d"
  20354. "\x1c\x01\xab\xda\xaf\xeb\xcd\xac\x2c\xee\x43\x39\x39\xce\x8d\x4a"
  20355. "\x0a\x5d\x57\xbb\x70\x5f\x3b\xf6\xec\x08\x47\x95\x11\xd4\xb4\xa3"
  20356. "\x21\x1f\x61\x64\x9a\xd6\x27\x43\x14\xbf\x0d\x43\x8a\x81\xe0\x60"
  20357. vec.msgLen = 128;
  20358. #endif
  20359. vec.Qx = "e55fee6c49d8d523f5ce7bf9c0425ce4ff650708b7de5cfb095901523979a7f042602db30854735369813b5c3f5ef868";
  20360. vec.Qy = "28f59cc5dc509892a988d38a8e2519de3d0c4fd0fbdb0993e38f18506c17606c5e24249246f1ce94983a5361c5be983e";
  20361. vec.d = "a492ce8fa90084c227e1a32f7974d39e9ff67a7e8705ec3419b35fb607582bebd461e0b1520ac76ec2dd4e9b63ebae71";
  20362. vec.R = "6820b8585204648aed63bdff47f6d9acebdea62944774a7d14f0e14aa0b9a5b99545b2daee6b3c74ebf606667a3f39b7";
  20363. vec.S = "491af1d0cccd56ddd520b233775d0bc6b40a6255cc55207d8e9356741f23c96c14714221078dbd5c17f4fdd89b32a907";
  20364. vec.curveName = "SECP384R1";
  20365. #ifndef NO_ASN
  20366. vec.r = (byte*)"\x68\x20\xb8\x58\x52\x04\x64\x8a\xed\x63\xbd\xff"
  20367. "\x47\xf6\xd9\xac\xeb\xde\xa6\x29\x44\x77\x4a\x7d"
  20368. "\x14\xf0\xe1\x4a\xa0\xb9\xa5\xb9\x95\x45\xb2\xda"
  20369. "\xee\x6b\x3c\x74\xeb\xf6\x06\x66\x7a\x3f\x39\xb7";
  20370. vec.rSz = 48;
  20371. vec.s = (byte*)"\x49\x1a\xf1\xd0\xcc\xcd\x56\xdd\xd5\x20\xb2\x33"
  20372. "\x77\x5d\x0b\xc6\xb4\x0a\x62\x55\xcc\x55\x20\x7d"
  20373. "\x8e\x93\x56\x74\x1f\x23\xc9\x6c\x14\x71\x42\x21"
  20374. "\x07\x8d\xbd\x5c\x17\xf4\xfd\xd8\x9b\x32\xa9\x07";
  20375. vec.sSz = 48;
  20376. #endif
  20377. break;
  20378. #endif /* HAVE_ECC384 */
  20379. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  20380. case 64:
  20381. return 0;
  20382. #endif /* HAVE_ECC512 */
  20383. #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
  20384. case 66:
  20385. /* first [P-521,SHA-1] vector from FIPS 186-3 NIST vectors */
  20386. #if 1
  20387. vec.msg = "\x1b\xf7\x03\x9c\xca\x23\x94\x27\x3f\x11\xa1\xd4\x8d\xcc\xb4\x46\x6f\x31\x61\xdf";
  20388. vec.msgLen = 20;
  20389. #else
  20390. /* This is the raw message prior to SHA-1 */
  20391. vec.msg =
  20392. "\x50\x3f\x79\x39\x34\x0a\xc7\x23\xcd\x4a\x2f\x4e\x6c\xcc\x27\x33"
  20393. "\x38\x3a\xca\x2f\xba\x90\x02\x19\x9d\x9e\x1f\x94\x8b\xe0\x41\x21"
  20394. "\x07\xa3\xfd\xd5\x14\xd9\x0c\xd4\xf3\x7c\xc3\xac\x62\xef\x00\x3a"
  20395. "\x2d\xb1\xd9\x65\x7a\xb7\x7f\xe7\x55\xbf\x71\xfa\x59\xe4\xd9\x6e"
  20396. "\xa7\x2a\xe7\xbf\x9d\xe8\x7d\x79\x34\x3b\xc1\xa4\xbb\x14\x4d\x16"
  20397. "\x28\xd1\xe9\xe9\xc8\xed\x80\x8b\x96\x2c\x54\xe5\xf9\x6d\x53\xda"
  20398. "\x14\x7a\x96\x38\xf9\x4a\x91\x75\xd8\xed\x61\x05\x5f\x0b\xa5\x73"
  20399. "\xa8\x2b\xb7\xe0\x18\xee\xda\xc4\xea\x7b\x36\x2e\xc8\x9c\x38\x2b"
  20400. vec.msgLen = 128;
  20401. #endif
  20402. vec.Qx = "12fbcaeffa6a51f3ee4d3d2b51c5dec6d7c726ca353fc014ea2bf7cfbb9b910d32cbfa6a00fe39b6cdb8946f22775398b2e233c0cf144d78c8a7742b5c7a3bb5d23";
  20403. vec.Qy = "09cdef823dd7bf9a79e8cceacd2e4527c231d0ae5967af0958e931d7ddccf2805a3e618dc3039fec9febbd33052fe4c0fee98f033106064982d88f4e03549d4a64d";
  20404. vec.d = "1bd56bd106118eda246155bd43b42b8e13f0a6e25dd3bb376026fab4dc92b6157bc6dfec2d15dd3d0cf2a39aa68494042af48ba9601118da82c6f2108a3a203ad74";
  20405. vec.R = "0bd117b4807710898f9dd7778056485777668f0e78e6ddf5b000356121eb7a220e9493c7f9a57c077947f89ac45d5acb6661bbcd17abb3faea149ba0aa3bb1521be";
  20406. vec.S = "019cd2c5c3f9870ecdeb9b323abdf3a98cd5e231d85c6ddc5b71ab190739f7f226e6b134ba1d5889ddeb2751dabd97911dff90c34684cdbe7bb669b6c3d22f2480c";
  20407. vec.curveName = "SECP521R1";
  20408. #ifndef NO_ASN
  20409. vec.r = (byte*)"\xbd\x11\x7b\x48\x07\x71\x08\x98\xf9\xdd\x77\x78"
  20410. "\x05\x64\x85\x77\x76\x68\xf0\xe7\x8e\x6d\xdf\x5b"
  20411. "\x00\x03\x56\x12\x1e\xb7\xa2\x20\xe9\x49\x3c\x7f"
  20412. "\x9a\x57\xc0\x77\x94\x7f\x89\xac\x45\xd5\xac\xb6"
  20413. "\x66\x1b\xbc\xd1\x7a\xbb\x3f\xae\xa1\x49\xba\x0a"
  20414. "\xa3\xbb\x15\x21\xbe";
  20415. vec.rSz = 65;
  20416. vec.s = (byte*)"\x19\xcd\x2c\x5c\x3f\x98\x70\xec\xde\xb9\xb3\x23"
  20417. "\xab\xdf\x3a\x98\xcd\x5e\x23\x1d\x85\xc6\xdd\xc5"
  20418. "\xb7\x1a\xb1\x90\x73\x9f\x7f\x22\x6e\x6b\x13\x4b"
  20419. "\xa1\xd5\x88\x9d\xde\xb2\x75\x1d\xab\xd9\x79\x11"
  20420. "\xdf\xf9\x0c\x34\x68\x4c\xdb\xe7\xbb\x66\x9b\x6c"
  20421. "\x3d\x22\xf2\x48\x0c";
  20422. vec.sSz = 65;
  20423. #endif
  20424. break;
  20425. #endif /* HAVE_ECC521 */
  20426. default:
  20427. return NOT_COMPILED_IN; /* Invalid key size / Not supported */
  20428. }; /* Switch */
  20429. ret = ecc_test_vector_item(&vec);
  20430. if (ret < 0) {
  20431. return ret;
  20432. }
  20433. return 0;
  20434. }
  20435. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  20436. #if defined(HAVE_ECC_SIGN) && (defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  20437. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)) \
  20438. && (!defined(FIPS_VERSION_GE) || FIPS_VERSION_GE(5,3))
  20439. #if defined(HAVE_ECC256)
  20440. static int ecc_test_deterministic_k(WC_RNG* rng)
  20441. {
  20442. int ret;
  20443. #ifdef WOLFSSL_SMALL_STACK
  20444. ecc_key *key = NULL;
  20445. #else
  20446. ecc_key key[1];
  20447. #endif
  20448. int key_inited = 0;
  20449. byte sig[72];
  20450. word32 sigSz;
  20451. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  20452. unsigned char hash[32];
  20453. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  20454. "C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721";
  20455. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  20456. "60FED4BA255A9D31C961EB74C6356D68C049B8923B61FA6CE669622E60F29FB6";
  20457. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  20458. "7903FE1008B8BC99A41AE9E95628BC64F2F1B20C2D7E9F5177A3C294D4462299";
  20459. WOLFSSL_SMALL_STACK_STATIC const byte expSig[] = {
  20460. 0x30, 0x46, 0x02, 0x21, 0x00, 0xEF, 0xD4, 0x8B,
  20461. 0x2A, 0xAC, 0xB6, 0xA8, 0xFD, 0x11, 0x40, 0xDD,
  20462. 0x9C, 0xD4, 0x5E, 0x81, 0xD6, 0x9D, 0x2C, 0x87,
  20463. 0x7B, 0x56, 0xAA, 0xF9, 0x91, 0xC3, 0x4D, 0x0E,
  20464. 0xA8, 0x4E, 0xAF, 0x37, 0x16, 0x02, 0x21, 0x00,
  20465. 0xF7, 0xCB, 0x1C, 0x94, 0x2D, 0x65, 0x7C, 0x41,
  20466. 0xD4, 0x36, 0xC7, 0xA1, 0xB6, 0xE2, 0x9F, 0x65,
  20467. 0xF3, 0xE9, 0x00, 0xDB, 0xB9, 0xAF, 0xF4, 0x06,
  20468. 0x4D, 0xC4, 0xAB, 0x2F, 0x84, 0x3A, 0xCD, 0xA8
  20469. };
  20470. #ifdef WOLFSSL_SMALL_STACK
  20471. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20472. if (key == NULL)
  20473. return MEMORY_E;
  20474. #endif
  20475. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  20476. if (ret != 0) {
  20477. goto done;
  20478. }
  20479. key_inited = 1;
  20480. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP256R1");
  20481. if (ret != 0) {
  20482. goto done;
  20483. }
  20484. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  20485. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  20486. if (ret != 0) {
  20487. goto done;
  20488. }
  20489. ret = wc_ecc_set_deterministic(key, 1);
  20490. if (ret != 0) {
  20491. goto done;
  20492. }
  20493. sigSz = sizeof(sig);
  20494. do {
  20495. #if defined(WOLFSSL_ASYNC_CRYPT)
  20496. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20497. #endif
  20498. if (ret == 0)
  20499. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  20500. } while (ret == WC_PENDING_E);
  20501. if (ret != 0) {
  20502. goto done;
  20503. }
  20504. TEST_SLEEP();
  20505. if (sigSz != sizeof(expSig)) {
  20506. ret = -9830;
  20507. goto done;
  20508. }
  20509. if (XMEMCMP(sig, expSig, sigSz) != 0) {
  20510. ret = -9831;
  20511. goto done;
  20512. }
  20513. sigSz = sizeof(sig);
  20514. do {
  20515. #if defined(WOLFSSL_ASYNC_CRYPT)
  20516. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20517. #endif
  20518. if (ret == 0)
  20519. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  20520. } while (ret == WC_PENDING_E);
  20521. if (ret != 0) {
  20522. goto done;
  20523. }
  20524. TEST_SLEEP();
  20525. done:
  20526. if (key_inited)
  20527. wc_ecc_free(key);
  20528. #ifdef WOLFSSL_SMALL_STACK
  20529. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20530. #endif
  20531. return ret;
  20532. }
  20533. #endif
  20534. #ifdef WOLFSSL_PUBLIC_MP
  20535. #if defined(HAVE_ECC384)
  20536. /* KAT from RFC6979 */
  20537. static int ecc384_test_deterministic_k(WC_RNG* rng)
  20538. {
  20539. int ret;
  20540. #ifdef WOLFSSL_SMALL_STACK
  20541. ecc_key *key;
  20542. mp_int *r, *s, *expR, *expS;
  20543. #else
  20544. ecc_key key[1];
  20545. mp_int r[1], s[1], expR[1], expS[1];
  20546. #endif
  20547. int key_inited = 0;
  20548. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  20549. unsigned char hash[32];
  20550. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  20551. "6B9D3DAD2E1B8C1C05B19875B6659F4DE23C3B667BF297BA9AA47740787137D8"
  20552. "96D5724E4C70A825F872C9EA60D2EDF5";
  20553. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  20554. "EC3A4E415B4E19A4568618029F427FA5DA9A8BC4AE92E02E06AAE5286B300C64"
  20555. "DEF8F0EA9055866064A254515480BC13";
  20556. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  20557. "8015D9B72D7D57244EA8EF9AC0C621896708A59367F9DFB9F54CA84B3F1C9DB1"
  20558. "288B231C3AE0D4FE7344FD2533264720";
  20559. WOLFSSL_SMALL_STACK_STATIC const char* expRstr =
  20560. "21B13D1E013C7FA1392D03C5F99AF8B30C570C6F98D4EA8E354B63A21D3DAA33"
  20561. "BDE1E888E63355D92FA2B3C36D8FB2CD";
  20562. WOLFSSL_SMALL_STACK_STATIC const char* expSstr =
  20563. "F3AA443FB107745BF4BD77CB3891674632068A10CA67E3D45DB2266FA7D1FEEB"
  20564. "EFDC63ECCD1AC42EC0CB8668A4FA0AB0";
  20565. #ifdef WOLFSSL_SMALL_STACK
  20566. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20567. r = (mp_int *)XMALLOC(sizeof(*r), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20568. s = (mp_int *)XMALLOC(sizeof(*s), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20569. expR = (mp_int *)XMALLOC(sizeof(*expR), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20570. expS = (mp_int *)XMALLOC(sizeof(*expS), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20571. if ((key == NULL) ||
  20572. (r == NULL) ||
  20573. (s == NULL) ||
  20574. (expR == NULL) ||
  20575. (expS == NULL))
  20576. {
  20577. ret = MEMORY_E;
  20578. goto done;
  20579. }
  20580. #endif
  20581. ret = mp_init_multi(r, s, expR, expS, NULL, NULL);
  20582. if (ret != MP_OKAY) {
  20583. goto done;
  20584. }
  20585. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  20586. if (ret != 0) {
  20587. goto done;
  20588. }
  20589. key_inited = 1;
  20590. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP384R1");
  20591. if (ret != 0) {
  20592. goto done;
  20593. }
  20594. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  20595. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  20596. if (ret != 0) {
  20597. goto done;
  20598. }
  20599. ret = wc_ecc_set_deterministic(key, 1);
  20600. if (ret != 0) {
  20601. goto done;
  20602. }
  20603. do {
  20604. #if defined(WOLFSSL_ASYNC_CRYPT)
  20605. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20606. #endif
  20607. if (ret == 0)
  20608. ret = wc_ecc_sign_hash_ex(hash, sizeof(hash), rng, key, r, s);
  20609. } while (ret == WC_PENDING_E);
  20610. if (ret != 0) {
  20611. goto done;
  20612. }
  20613. TEST_SLEEP();
  20614. mp_read_radix(expR, expRstr, MP_RADIX_HEX);
  20615. mp_read_radix(expS, expSstr, MP_RADIX_HEX);
  20616. if (mp_cmp(r, expR) != MP_EQ) {
  20617. ret = -1;
  20618. }
  20619. done:
  20620. if (key_inited)
  20621. wc_ecc_free(key);
  20622. #ifdef WOLFSSL_SMALL_STACK
  20623. if (key != NULL)
  20624. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20625. if (r != NULL)
  20626. XFREE(r, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20627. if (s != NULL)
  20628. XFREE(s, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20629. if (expR != NULL)
  20630. XFREE(expR, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20631. if (expS != NULL)
  20632. XFREE(expS, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20633. #endif
  20634. return ret;
  20635. }
  20636. #endif /* HAVE_ECC384 */
  20637. #if defined(HAVE_ECC521)
  20638. /* KAT from RFC6979 */
  20639. static int ecc521_test_deterministic_k(WC_RNG* rng)
  20640. {
  20641. int ret;
  20642. #ifdef WOLFSSL_SMALL_STACK
  20643. ecc_key *key;
  20644. mp_int *r, *s, *expR, *expS;
  20645. #else
  20646. ecc_key key[1];
  20647. mp_int r[1], s[1], expR[1], expS[1];
  20648. #endif
  20649. int key_inited = 0;
  20650. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  20651. unsigned char hash[32];
  20652. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  20653. "0FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75C"
  20654. "AA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83"
  20655. "538";
  20656. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  20657. "1894550D0785932E00EAA23B694F213F8C3121F86DC97A04E5A7167DB4E5BCD3"
  20658. "71123D46E45DB6B5D5370A7F20FB633155D38FFA16D2BD761DCAC474B9A2F502"
  20659. "3A4";
  20660. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  20661. "0493101C962CD4D2FDDF782285E64584139C2F91B47F87FF82354D6630F746A2"
  20662. "8A0DB25741B5B34A828008B22ACC23F924FAAFBD4D33F81EA66956DFEAA2BFDF"
  20663. "CF5";
  20664. WOLFSSL_SMALL_STACK_STATIC const char* expRstr =
  20665. "1511BB4D675114FE266FC4372B87682BAECC01D3CC62CF2303C92B3526012659"
  20666. "D16876E25C7C1E57648F23B73564D67F61C6F14D527D54972810421E7D87589E"
  20667. "1A7";
  20668. WOLFSSL_SMALL_STACK_STATIC const char* expSstr =
  20669. "04A171143A83163D6DF460AAF61522695F207A58B95C0644D87E52AA1A347916"
  20670. "E4F7A72930B1BC06DBE22CE3F58264AFD23704CBB63B29B931F7DE6C9D949A7E"
  20671. "CFC";
  20672. #ifdef WOLFSSL_SMALL_STACK
  20673. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20674. r = (mp_int *)XMALLOC(sizeof(*r), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20675. s = (mp_int *)XMALLOC(sizeof(*s), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20676. expR = (mp_int *)XMALLOC(sizeof(*expR), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20677. expS = (mp_int *)XMALLOC(sizeof(*expS), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20678. if ((key == NULL) ||
  20679. (r == NULL) ||
  20680. (s == NULL) ||
  20681. (expR == NULL) ||
  20682. (expS == NULL))
  20683. {
  20684. ret = MEMORY_E;
  20685. goto done;
  20686. }
  20687. #endif
  20688. ret = mp_init_multi(r, s, expR, expS, NULL, NULL);
  20689. if (ret != MP_OKAY) {
  20690. goto done;
  20691. }
  20692. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  20693. if (ret != 0) {
  20694. return ret;
  20695. }
  20696. key_inited = 1;
  20697. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP521R1");
  20698. if (ret != 0) {
  20699. goto done;
  20700. }
  20701. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  20702. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  20703. if (ret != 0) {
  20704. goto done;
  20705. }
  20706. ret = wc_ecc_set_deterministic(key, 1);
  20707. if (ret != 0) {
  20708. goto done;
  20709. }
  20710. do {
  20711. #if defined(WOLFSSL_ASYNC_CRYPT)
  20712. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20713. #endif
  20714. if (ret == 0)
  20715. ret = wc_ecc_sign_hash_ex(hash, sizeof(hash), rng, key, r, s);
  20716. } while (ret == WC_PENDING_E);
  20717. if (ret != 0) {
  20718. goto done;
  20719. }
  20720. TEST_SLEEP();
  20721. mp_read_radix(expR, expRstr, MP_RADIX_HEX);
  20722. mp_read_radix(expS, expSstr, MP_RADIX_HEX);
  20723. if (mp_cmp(r, expR) != MP_EQ) {
  20724. ret = -1;
  20725. }
  20726. done:
  20727. if (key_inited)
  20728. wc_ecc_free(key);
  20729. #ifdef WOLFSSL_SMALL_STACK
  20730. if (key != NULL)
  20731. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20732. if (r != NULL)
  20733. XFREE(r, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20734. if (s != NULL)
  20735. XFREE(s, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20736. if (expR != NULL)
  20737. XFREE(expR, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20738. if (expS != NULL)
  20739. XFREE(expS, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20740. #endif
  20741. return ret;
  20742. }
  20743. #endif /* HAVE_ECC521 */
  20744. #endif /* WOLFSSL_PUBLIC_MP */
  20745. #endif /* HAVE_ECC_SIGN && (WOLFSSL_ECDSA_DETERMINISTIC_K ||
  20746. WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  20747. && (!FIPS_VERSION_GE || FIPS_VERSION_GE(5,3)) */
  20748. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K) && \
  20749. !defined(WOLFSSL_KCAPI_ECC)
  20750. static int ecc_test_sign_vectors(WC_RNG* rng)
  20751. {
  20752. int ret;
  20753. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20754. ecc_key *key = NULL;
  20755. #else
  20756. ecc_key key[1];
  20757. #endif
  20758. int key_inited = 0;
  20759. byte sig[72];
  20760. word32 sigSz;
  20761. WOLFSSL_SMALL_STACK_STATIC const unsigned char hash[32] = "test wolfSSL deterministic sign";
  20762. WOLFSSL_SMALL_STACK_STATIC const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  20763. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  20764. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  20765. WOLFSSL_SMALL_STACK_STATIC const byte k[1] = { 0x02 };
  20766. WOLFSSL_SMALL_STACK_STATIC const byte expSig[71] = {
  20767. 0x30, 0x45, 0x02, 0x20, 0x7c, 0xf2, 0x7b, 0x18,
  20768. 0x8d, 0x03, 0x4f, 0x7e, 0x8a, 0x52, 0x38, 0x03,
  20769. 0x04, 0xb5, 0x1a, 0xc3, 0xc0, 0x89, 0x69, 0xe2,
  20770. 0x77, 0xf2, 0x1b, 0x35, 0xa6, 0x0b, 0x48, 0xfc,
  20771. 0x47, 0x66, 0x99, 0x78, 0x02, 0x21, 0x00, 0xa8,
  20772. 0x43, 0xa0, 0xce, 0x6c, 0x5e, 0x17, 0x8a, 0x53,
  20773. 0x4d, 0xaf, 0xd2, 0x95, 0x78, 0x9f, 0x84, 0x4f,
  20774. 0x94, 0xb8, 0x75, 0xa3, 0x19, 0xa5, 0xd4, 0xdf,
  20775. 0xe1, 0xd4, 0x5e, 0x9d, 0x97, 0xfe, 0x81
  20776. };
  20777. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20778. if ((key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  20779. return MEMORY_E;
  20780. #endif
  20781. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  20782. if (ret != 0) {
  20783. goto done;
  20784. }
  20785. key_inited = 1;
  20786. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP256R1");
  20787. if (ret != 0) {
  20788. goto done;
  20789. }
  20790. #if (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) \
  20791. && (HAVE_FIPS_VERSION > 2)))
  20792. wc_ecc_set_flags(key, WC_ECC_FLAG_DEC_SIGN);
  20793. #endif
  20794. ret = wc_ecc_sign_set_k(k, sizeof(k), key);
  20795. if (ret != 0) {
  20796. goto done;
  20797. }
  20798. sigSz = sizeof(sig);
  20799. do {
  20800. #if defined(WOLFSSL_ASYNC_CRYPT)
  20801. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20802. #endif
  20803. if (ret == 0)
  20804. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  20805. } while (ret == WC_PENDING_E);
  20806. if (ret != 0) {
  20807. goto done;
  20808. }
  20809. TEST_SLEEP();
  20810. if (sigSz != sizeof(expSig)) {
  20811. ret = -9830;
  20812. goto done;
  20813. }
  20814. if (XMEMCMP(sig, expSig, sigSz) != 0) {
  20815. ret = -9831;
  20816. goto done;
  20817. }
  20818. sigSz = sizeof(sig);
  20819. do {
  20820. #if defined(WOLFSSL_ASYNC_CRYPT)
  20821. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20822. #endif
  20823. if (ret == 0)
  20824. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  20825. } while (ret == WC_PENDING_E);
  20826. if (ret != 0) {
  20827. goto done;
  20828. }
  20829. TEST_SLEEP();
  20830. done:
  20831. if (key_inited)
  20832. wc_ecc_free(key);
  20833. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20834. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20835. #endif
  20836. return ret;
  20837. }
  20838. #endif
  20839. #if defined(HAVE_ECC_CDH) && defined(HAVE_ECC_DHE)
  20840. static int ecc_test_cdh_vectors(WC_RNG* rng)
  20841. {
  20842. int ret;
  20843. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20844. ecc_key *pub_key = (ecc_key *)XMALLOC(sizeof *pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20845. ecc_key *priv_key = (ecc_key *)XMALLOC(sizeof *priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20846. #else
  20847. ecc_key pub_key[1], priv_key[1];
  20848. #endif
  20849. byte sharedA[32] = {0}, sharedB[32] = {0};
  20850. word32 x, z;
  20851. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSx = "700c48f77f56584c5cc632ca65640db91b6bacce3a4df6b42ce7cc838833d287";
  20852. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSy = "db71e509e3fd9b060ddb20ba5c51dcc5948d46fbf640dfe0441782cab85fa4ac";
  20853. WOLFSSL_SMALL_STACK_STATIC const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  20854. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  20855. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  20856. WOLFSSL_SMALL_STACK_STATIC const char* ZIUT = "46fc62106420ff012e54a434fbdd2d25ccc5852060561e68040dd7778997bd7b";
  20857. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20858. if ((pub_key == NULL) ||
  20859. (priv_key == NULL)) {
  20860. ret = MEMORY_E;
  20861. goto done;
  20862. }
  20863. #endif
  20864. XMEMSET(pub_key, 0, sizeof *pub_key);
  20865. XMEMSET(priv_key, 0, sizeof *priv_key);
  20866. /* setup private and public keys */
  20867. ret = wc_ecc_init_ex(pub_key, HEAP_HINT, devId);
  20868. if (ret != 0)
  20869. goto done;
  20870. ret = wc_ecc_init_ex(priv_key, HEAP_HINT, devId);
  20871. if (ret != 0)
  20872. goto done;
  20873. wc_ecc_set_flags(pub_key, WC_ECC_FLAG_COFACTOR);
  20874. wc_ecc_set_flags(priv_key, WC_ECC_FLAG_COFACTOR);
  20875. ret = wc_ecc_import_raw(pub_key, QCAVSx, QCAVSy, NULL, "SECP256R1");
  20876. if (ret != 0)
  20877. goto done;
  20878. ret = wc_ecc_import_raw(priv_key, QIUTx, QIUTy, dIUT, "SECP256R1");
  20879. if (ret != 0)
  20880. goto done;
  20881. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  20882. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  20883. !defined(HAVE_SELFTEST)
  20884. ret = wc_ecc_set_rng(priv_key, rng);
  20885. if (ret != 0)
  20886. goto done;
  20887. #else
  20888. (void)rng;
  20889. #endif
  20890. /* compute ECC Cofactor shared secret */
  20891. x = sizeof(sharedA);
  20892. do {
  20893. #if defined(WOLFSSL_ASYNC_CRYPT)
  20894. ret = wc_AsyncWait(ret, &priv_key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20895. #endif
  20896. if (ret == 0)
  20897. ret = wc_ecc_shared_secret(priv_key, pub_key, sharedA, &x);
  20898. } while (ret == WC_PENDING_E);
  20899. if (ret != 0) {
  20900. goto done;
  20901. }
  20902. TEST_SLEEP();
  20903. /* read in expected Z */
  20904. z = sizeof(sharedB);
  20905. ret = Base16_Decode((const byte*)ZIUT, (word32)XSTRLEN(ZIUT), sharedB, &z);
  20906. if (ret != 0)
  20907. goto done;
  20908. /* compare results */
  20909. if (x != z || XMEMCMP(sharedA, sharedB, x)) {
  20910. ERROR_OUT(-9840, done);
  20911. }
  20912. done:
  20913. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20914. if (priv_key) {
  20915. wc_ecc_free(priv_key);
  20916. XFREE(priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20917. }
  20918. if (pub_key) {
  20919. wc_ecc_free(pub_key);
  20920. XFREE(pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20921. }
  20922. #else
  20923. wc_ecc_free(priv_key);
  20924. wc_ecc_free(pub_key);
  20925. #endif
  20926. return ret;
  20927. }
  20928. #endif /* HAVE_ECC_CDH && HAVE_ECC_DHE */
  20929. #endif /* HAVE_ECC_VECTOR_TEST */
  20930. #ifdef HAVE_ECC_KEY_IMPORT
  20931. /* returns 0 on success */
  20932. static int ecc_test_make_pub(WC_RNG* rng)
  20933. {
  20934. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20935. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20936. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  20937. ecc_key *pub = (ecc_key *)XMALLOC(sizeof *pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20938. #endif
  20939. byte *exportBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20940. byte *tmp = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20941. #else
  20942. ecc_key key[1];
  20943. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  20944. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  20945. ecc_key pub[1];
  20946. #endif
  20947. byte exportBuf[ECC_BUFSIZE];
  20948. byte tmp[ECC_BUFSIZE];
  20949. #endif
  20950. const byte* msg = (const byte*)"test wolfSSL ECC public gen";
  20951. word32 x;
  20952. word32 tmpSz;
  20953. int ret = 0;
  20954. ecc_point* pubPoint = NULL;
  20955. #ifdef HAVE_ECC_VERIFY
  20956. int verify = 0;
  20957. #endif
  20958. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20959. if ((key == NULL) ||
  20960. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  20961. (pub == NULL) ||
  20962. #endif
  20963. (exportBuf == NULL) ||
  20964. (tmp == NULL))
  20965. ERROR_OUT(MEMORY_E, done);
  20966. #endif
  20967. (void)msg;
  20968. (void)verify;
  20969. (void)exportBuf;
  20970. (void)rng;
  20971. wc_ecc_init_ex(key, HEAP_HINT, devId);
  20972. #ifndef NO_ECC256
  20973. #ifdef USE_CERT_BUFFERS_256
  20974. XMEMCPY(tmp, ecc_key_der_256, (size_t)sizeof_ecc_key_der_256);
  20975. tmpSz = (size_t)sizeof_ecc_key_der_256;
  20976. #else
  20977. {
  20978. XFILE file = XFOPEN(eccKeyDerFile, "rb");
  20979. if (!file) {
  20980. ERROR_OUT(-9850, done);
  20981. }
  20982. tmpSz = (word32)XFREAD(tmp, 1, ECC_BUFSIZE, file);
  20983. XFCLOSE(file);
  20984. }
  20985. #endif /* USE_CERT_BUFFERS_256 */
  20986. /* import private only then test with */
  20987. ret = wc_ecc_import_private_key(tmp, tmpSz, NULL, 0, NULL);
  20988. if (ret == 0) {
  20989. ERROR_OUT(-9851, done);
  20990. }
  20991. ret = wc_ecc_import_private_key(NULL, tmpSz, NULL, 0, key);
  20992. if (ret == 0) {
  20993. ERROR_OUT(-9852, done);
  20994. }
  20995. x = 0;
  20996. ret = wc_EccPrivateKeyDecode(tmp, &x, key, tmpSz);
  20997. if (ret != 0) {
  20998. ERROR_OUT(-9853, done);
  20999. }
  21000. #ifdef HAVE_ECC_KEY_EXPORT
  21001. x = ECC_BUFSIZE;
  21002. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  21003. if (ret != 0) {
  21004. ERROR_OUT(-9854, done);
  21005. }
  21006. /* make private only key */
  21007. wc_ecc_free(key);
  21008. wc_ecc_init_ex(key, HEAP_HINT, devId);
  21009. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  21010. if (ret != 0) {
  21011. ERROR_OUT(-9855, done);
  21012. }
  21013. x = ECC_BUFSIZE;
  21014. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  21015. if (ret == 0) {
  21016. ERROR_OUT(-9856, done);
  21017. }
  21018. #endif /* HAVE_ECC_KEY_EXPORT */
  21019. ret = wc_ecc_make_pub(NULL, NULL);
  21020. if (ret == 0) {
  21021. ERROR_OUT(-9857, done);
  21022. }
  21023. TEST_SLEEP();
  21024. #ifndef WOLFSSL_NO_MALLOC
  21025. pubPoint = wc_ecc_new_point_h(HEAP_HINT);
  21026. if (pubPoint == NULL) {
  21027. ERROR_OUT(-9858, done);
  21028. }
  21029. #if !defined(WOLFSSL_CRYPTOCELL)
  21030. ret = wc_ecc_make_pub(key, pubPoint);
  21031. #if defined(WOLFSSL_ASYNC_CRYPT)
  21032. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  21033. #endif
  21034. if (ret != 0) {
  21035. ERROR_OUT(-9859, done);
  21036. }
  21037. #endif
  21038. TEST_SLEEP();
  21039. #ifdef HAVE_ECC_KEY_EXPORT
  21040. /* export should still fail, is private only key */
  21041. x = ECC_BUFSIZE;
  21042. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  21043. if (ret == 0) {
  21044. ERROR_OUT(-9860, done);
  21045. }
  21046. #endif /* HAVE_ECC_KEY_EXPORT */
  21047. #endif /* !WOLFSSL_NO_MALLOC */
  21048. #endif /* !NO_ECC256 */
  21049. /* create a new key since above test for loading key is not supported */
  21050. #if defined(WOLFSSL_CRYPTOCELL) || defined(NO_ECC256) || \
  21051. defined(WOLFSSL_QNX_CAAM) || defined(WOLFSSL_SE050) || \
  21052. defined(WOLFSSL_SECO_CAAM)
  21053. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  21054. if (ret != 0) {
  21055. ERROR_OUT(-9861, done);
  21056. }
  21057. #endif
  21058. #if defined(HAVE_ECC_SIGN) && (!defined(ECC_TIMING_RESISTANT) || \
  21059. (defined(ECC_TIMING_RESISTANT) && !defined(WC_NO_RNG))) && \
  21060. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(HAVE_ECC_DETERMINISTIC_K)
  21061. tmpSz = ECC_BUFSIZE;
  21062. ret = 0;
  21063. do {
  21064. #if defined(WOLFSSL_ASYNC_CRYPT)
  21065. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21066. #endif
  21067. if (ret == 0) {
  21068. ret = wc_ecc_sign_hash(msg, (word32)XSTRLEN((const char* )msg), tmp,
  21069. &tmpSz, rng, key);
  21070. }
  21071. } while (ret == WC_PENDING_E);
  21072. if (ret != 0) {
  21073. ERROR_OUT(-9862, done);
  21074. }
  21075. TEST_SLEEP();
  21076. #ifdef HAVE_ECC_VERIFY
  21077. /* try verify with private only key */
  21078. ret = 0;
  21079. do {
  21080. #if defined(WOLFSSL_ASYNC_CRYPT)
  21081. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21082. #endif
  21083. if (ret == 0) {
  21084. ret = wc_ecc_verify_hash(tmp, tmpSz, msg,
  21085. (word32)XSTRLEN((const char*)msg), &verify, key);
  21086. }
  21087. } while (ret == WC_PENDING_E);
  21088. if (ret != 0) {
  21089. ERROR_OUT(-9863, done);
  21090. }
  21091. if (verify != 1) {
  21092. ERROR_OUT(-9864, done);
  21093. }
  21094. TEST_SLEEP();
  21095. #ifdef HAVE_ECC_KEY_EXPORT
  21096. /* exporting the public part should now work */
  21097. x = ECC_BUFSIZE;
  21098. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  21099. if (ret != 0) {
  21100. ERROR_OUT(-9865, done);
  21101. }
  21102. #endif /* HAVE_ECC_KEY_EXPORT */
  21103. #endif /* HAVE_ECC_VERIFY */
  21104. #endif /* HAVE_ECC_SIGN */
  21105. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21106. /* now test private only key with creating a shared secret */
  21107. x = ECC_BUFSIZE;
  21108. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  21109. if (ret != 0) {
  21110. ERROR_OUT(-9866, done);
  21111. }
  21112. #if !defined(WOLFSSL_QNX_CAAM) && !defined(WOLFSSL_SE050)
  21113. /* make private only key */
  21114. wc_ecc_free(key);
  21115. wc_ecc_init_ex(key, HEAP_HINT, devId);
  21116. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  21117. if (ret != 0) {
  21118. ERROR_OUT(-9867, done);
  21119. }
  21120. /* check that public export fails with private only key */
  21121. x = ECC_BUFSIZE;
  21122. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  21123. if (ret == 0) {
  21124. ERROR_OUT(-9868, done);
  21125. }
  21126. #endif /* WOLFSSL_QNX_CAAM */
  21127. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  21128. /* make public key for shared secret */
  21129. wc_ecc_init_ex(pub, HEAP_HINT, devId);
  21130. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, pub);
  21131. #ifdef HAVE_ECC_CDH
  21132. wc_ecc_set_flags(key, WC_ECC_FLAG_COFACTOR);
  21133. #endif
  21134. #if defined(WOLFSSL_ASYNC_CRYPT)
  21135. ret = wc_AsyncWait(ret, &pub->asyncDev, WC_ASYNC_FLAG_NONE);
  21136. #endif
  21137. if (ret != 0) {
  21138. ERROR_OUT(-9869, done);
  21139. }
  21140. TEST_SLEEP();
  21141. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21142. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21143. !defined(HAVE_SELFTEST)
  21144. ret = wc_ecc_set_rng(key, rng);
  21145. if (ret != 0)
  21146. goto done;
  21147. #endif
  21148. x = ECC_BUFSIZE;
  21149. do {
  21150. #if defined(WOLFSSL_ASYNC_CRYPT)
  21151. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21152. #endif
  21153. if (ret == 0) {
  21154. ret = wc_ecc_shared_secret(key, pub, exportBuf, &x);
  21155. }
  21156. } while (ret == WC_PENDING_E);
  21157. wc_ecc_free(pub);
  21158. if (ret != 0) {
  21159. ERROR_OUT(-9870, done);
  21160. }
  21161. TEST_SLEEP();
  21162. #endif /* HAVE_ECC_DHE && HAVE_ECC_KEY_EXPORT && !WC_NO_RNG */
  21163. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  21164. ret = 0;
  21165. done:
  21166. wc_ecc_del_point_h(pubPoint, HEAP_HINT);
  21167. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21168. if (key != NULL) {
  21169. wc_ecc_free(key);
  21170. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21171. }
  21172. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  21173. if (pub != NULL)
  21174. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21175. #endif
  21176. if (exportBuf != NULL)
  21177. XFREE(exportBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21178. if (tmp != NULL)
  21179. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21180. #else
  21181. wc_ecc_free(key);
  21182. #endif
  21183. return ret;
  21184. }
  21185. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && \
  21186. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21187. static int ecc_test_key_decode(WC_RNG* rng, int keySize)
  21188. {
  21189. int ret;
  21190. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21191. ecc_key *eccKey = (ecc_key *)XMALLOC(sizeof *eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21192. byte *tmpBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21193. #else
  21194. ecc_key eccKey[1];
  21195. byte tmpBuf[ECC_BUFSIZE];
  21196. #endif
  21197. word32 tmpSz;
  21198. word32 idx;
  21199. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21200. if ((eccKey == NULL) || (tmpBuf == NULL))
  21201. ERROR_OUT(MEMORY_E, done);
  21202. #endif
  21203. ret = wc_ecc_init(eccKey);
  21204. if (ret != 0) {
  21205. goto done;
  21206. }
  21207. ret = wc_ecc_make_key(rng, keySize, eccKey);
  21208. #if defined(WOLFSSL_ASYNC_CRYPT)
  21209. ret = wc_AsyncWait(ret, &eccKey->asyncDev, WC_ASYNC_FLAG_NONE);
  21210. #endif
  21211. if (ret != 0) {
  21212. goto done;
  21213. }
  21214. tmpSz = ECC_BUFSIZE;
  21215. ret = wc_EccKeyToDer(eccKey, tmpBuf, tmpSz);
  21216. wc_ecc_free(eccKey);
  21217. if (ret < 0) {
  21218. goto done;
  21219. }
  21220. tmpSz = ret;
  21221. ret = wc_ecc_init(eccKey);
  21222. if (ret != 0) {
  21223. goto done;
  21224. }
  21225. idx = 0;
  21226. ret = wc_EccPrivateKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  21227. if (ret != 0) {
  21228. goto done;
  21229. }
  21230. wc_ecc_free(eccKey);
  21231. ret = wc_ecc_init(eccKey);
  21232. if (ret != 0) {
  21233. goto done;
  21234. }
  21235. idx = 0;
  21236. ret = wc_EccPublicKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  21237. if (ret != 0) {
  21238. goto done;
  21239. }
  21240. ret = 0;
  21241. done:
  21242. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21243. if (eccKey != NULL) {
  21244. wc_ecc_free(eccKey);
  21245. XFREE(eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21246. }
  21247. if (tmpBuf != NULL)
  21248. XFREE(tmpBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21249. #else
  21250. wc_ecc_free(eccKey);
  21251. #endif
  21252. return ret;
  21253. }
  21254. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  21255. #endif /* HAVE_ECC_KEY_IMPORT */
  21256. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && \
  21257. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21258. static int ecc_test_key_gen(WC_RNG* rng, int keySize)
  21259. {
  21260. int ret = 0;
  21261. int derSz;
  21262. #ifdef HAVE_PKCS8
  21263. word32 pkcs8Sz;
  21264. #endif
  21265. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21266. byte *der = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21267. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21268. #else
  21269. byte der[ECC_BUFSIZE];
  21270. ecc_key userA[1];
  21271. #endif
  21272. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21273. if ((der == NULL) || (userA == NULL))
  21274. ERROR_OUT(MEMORY_E, done);
  21275. #endif
  21276. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  21277. if (ret != 0)
  21278. goto done;
  21279. ret = wc_ecc_make_key(rng, keySize, userA);
  21280. #if defined(WOLFSSL_ASYNC_CRYPT)
  21281. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  21282. #endif
  21283. if (ret != 0)
  21284. goto done;
  21285. TEST_SLEEP();
  21286. ret = wc_ecc_check_key(userA);
  21287. if (ret != 0)
  21288. goto done;
  21289. TEST_SLEEP();
  21290. derSz = wc_EccKeyToDer(userA, der, ECC_BUFSIZE);
  21291. if (derSz < 0) {
  21292. ERROR_OUT(derSz, done);
  21293. }
  21294. ret = SaveDerAndPem(der, derSz, eccCaKeyTempFile, eccCaKeyPemFile,
  21295. ECC_PRIVATEKEY_TYPE, -8347);
  21296. if (ret != 0) {
  21297. goto done;
  21298. }
  21299. /* test export of public key */
  21300. derSz = wc_EccPublicKeyToDer(userA, der, ECC_BUFSIZE, 1);
  21301. if (derSz < 0) {
  21302. ERROR_OUT(derSz, done);
  21303. }
  21304. if (derSz == 0) {
  21305. ERROR_OUT(-9890, done);
  21306. }
  21307. #ifdef HAVE_COMP_KEY
  21308. /* test export of compressed public key */
  21309. derSz = wc_EccPublicKeyToDer_ex(userA, der, ECC_BUFSIZE, 1, 1);
  21310. if (derSz < 0) {
  21311. ERROR_OUT(derSz, done);
  21312. }
  21313. if (derSz == 0) {
  21314. ERROR_OUT(-9890, done);
  21315. }
  21316. #endif
  21317. ret = SaveDerAndPem(der, derSz, eccPubKeyDerFile, NULL, 0, -8348);
  21318. if (ret != 0) {
  21319. goto done;
  21320. }
  21321. #ifdef HAVE_PKCS8
  21322. /* test export of PKCS#8 unencrypted private key */
  21323. pkcs8Sz = FOURK_BUF;
  21324. derSz = wc_EccPrivateKeyToPKCS8(userA, der, &pkcs8Sz);
  21325. if (derSz < 0) {
  21326. ERROR_OUT(derSz, done);
  21327. }
  21328. if (derSz == 0) {
  21329. ERROR_OUT(-9891, done);
  21330. }
  21331. ret = SaveDerAndPem(der, derSz, eccPkcs8KeyDerFile, NULL, 0, -8349);
  21332. if (ret != 0) {
  21333. goto done;
  21334. }
  21335. #endif /* HAVE_PKCS8 */
  21336. done:
  21337. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21338. if (der != NULL)
  21339. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21340. if (userA != NULL) {
  21341. wc_ecc_free(userA);
  21342. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21343. }
  21344. #else
  21345. wc_ecc_free(userA);
  21346. #endif
  21347. return ret;
  21348. }
  21349. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  21350. static int ecc_test_curve_size(WC_RNG* rng, int keySize, int testVerifyCount,
  21351. int curve_id, const ecc_set_type* dp)
  21352. {
  21353. #if defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG) && \
  21354. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  21355. WC_DECLARE_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  21356. WC_DECLARE_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  21357. word32 y;
  21358. #endif
  21359. #ifdef HAVE_ECC_KEY_EXPORT
  21360. #define ECC_KEY_EXPORT_BUF_SIZE (MAX_ECC_BYTES * 2 + 32)
  21361. WC_DECLARE_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  21362. #endif
  21363. word32 x = 0;
  21364. #if defined(HAVE_ECC_SIGN) && !defined(WOLFSSL_KCAPI_ECC)
  21365. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  21366. WC_DECLARE_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  21367. int i;
  21368. #ifdef HAVE_ECC_VERIFY
  21369. int verify;
  21370. #endif /* HAVE_ECC_VERIFY */
  21371. #endif /* HAVE_ECC_SIGN */
  21372. int ret;
  21373. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21374. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21375. ecc_key *userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21376. ecc_key *pubKey = (ecc_key *)XMALLOC(sizeof *pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21377. #else
  21378. ecc_key userA[1];
  21379. ecc_key userB[1];
  21380. ecc_key pubKey[1];
  21381. #endif
  21382. #ifndef WC_NO_RNG
  21383. int curveSize;
  21384. #endif
  21385. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  21386. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG) && \
  21387. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  21388. if (sharedA == NULL || sharedB == NULL)
  21389. ERROR_OUT(-9900, done);
  21390. #endif
  21391. #ifdef HAVE_ECC_KEY_EXPORT
  21392. if (exportBuf == NULL)
  21393. ERROR_OUT(-9901, done);
  21394. #endif
  21395. #if defined(HAVE_ECC_SIGN) && !defined(WOLFSSL_KCAPI_ECC)
  21396. if (sig == NULL || digest == NULL)
  21397. ERROR_OUT(-9902, done);
  21398. #endif
  21399. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  21400. (void)testVerifyCount;
  21401. (void)dp;
  21402. (void)x;
  21403. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21404. if ((userA == NULL) ||
  21405. (userB == NULL) ||
  21406. (pubKey == NULL))
  21407. ERROR_OUT(-9903, done);
  21408. #endif
  21409. XMEMSET(userA, 0, sizeof *userA);
  21410. XMEMSET(userB, 0, sizeof *userB);
  21411. XMEMSET(pubKey, 0, sizeof *pubKey);
  21412. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  21413. if (ret != 0)
  21414. ERROR_OUT(-9904, done);
  21415. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  21416. if (ret != 0)
  21417. ERROR_OUT(-9905, done);
  21418. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  21419. if (ret != 0)
  21420. ERROR_OUT(-9906, done);
  21421. #ifdef WOLFSSL_CUSTOM_CURVES
  21422. if (dp != NULL) {
  21423. ret = wc_ecc_set_custom_curve(userA, dp);
  21424. if (ret != 0)
  21425. ERROR_OUT(-9907, done);
  21426. ret = wc_ecc_set_custom_curve(userB, dp);
  21427. if (ret != 0)
  21428. ERROR_OUT(-9908, done);
  21429. }
  21430. #endif
  21431. #ifndef WC_NO_RNG
  21432. ret = wc_ecc_make_key_ex(rng, keySize, userA, curve_id);
  21433. #if defined(WOLFSSL_ASYNC_CRYPT)
  21434. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  21435. #endif
  21436. #ifdef WOLF_CRYPTO_CB_ONLY_ECC
  21437. if (ret == NO_VALID_DEVID) {
  21438. ret = 0;
  21439. goto done; /* no software case */
  21440. }
  21441. #endif
  21442. if (ret == ECC_CURVE_OID_E)
  21443. goto done; /* catch case, where curve is not supported */
  21444. if (ret != 0)
  21445. ERROR_OUT(-9910, done);
  21446. TEST_SLEEP();
  21447. if (wc_ecc_get_curve_idx(curve_id) != -1) {
  21448. curveSize = wc_ecc_get_curve_size_from_id(userA->dp->id);
  21449. if (curveSize != userA->dp->size)
  21450. ERROR_OUT(-9911, done);
  21451. }
  21452. ret = wc_ecc_check_key(userA);
  21453. if (ret != 0)
  21454. ERROR_OUT(-9912, done);
  21455. TEST_SLEEP();
  21456. /* ATECC508/608 configuration may not support more than one ECDH key */
  21457. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  21458. ret = wc_ecc_make_key_ex(rng, keySize, userB, curve_id);
  21459. #if defined(WOLFSSL_ASYNC_CRYPT)
  21460. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  21461. #endif
  21462. if (ret != 0)
  21463. ERROR_OUT(-9914, done);
  21464. TEST_SLEEP();
  21465. /* only perform the below tests if the key size matches */
  21466. if (dp == NULL && keySize > 0 && wc_ecc_size(userA) != keySize)
  21467. ERROR_OUT(ECC_CURVE_OID_E, done);
  21468. #ifdef HAVE_ECC_DHE
  21469. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21470. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21471. !defined(HAVE_SELFTEST)
  21472. ret = wc_ecc_set_rng(userA, rng);
  21473. if (ret != 0)
  21474. ERROR_OUT(-9915, done);
  21475. ret = wc_ecc_set_rng(userB, rng);
  21476. if (ret != 0)
  21477. ERROR_OUT(-9916, done);
  21478. #endif
  21479. x = ECC_SHARED_SIZE;
  21480. do {
  21481. #if defined(WOLFSSL_ASYNC_CRYPT)
  21482. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21483. #endif
  21484. if (ret == 0)
  21485. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  21486. } while (ret == WC_PENDING_E);
  21487. if (ret != 0) {
  21488. ERROR_OUT(-9917, done);
  21489. }
  21490. TEST_SLEEP();
  21491. y = ECC_SHARED_SIZE;
  21492. do {
  21493. #if defined(WOLFSSL_ASYNC_CRYPT)
  21494. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21495. #endif
  21496. if (ret == 0)
  21497. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  21498. } while (ret == WC_PENDING_E);
  21499. if (ret != 0)
  21500. ERROR_OUT(-9918, done);
  21501. if (y != x)
  21502. ERROR_OUT(-9919, done);
  21503. if (XMEMCMP(sharedA, sharedB, x))
  21504. ERROR_OUT(-9920, done);
  21505. TEST_SLEEP();
  21506. #ifdef HAVE_ECC_CDH
  21507. /* add cofactor flag */
  21508. wc_ecc_set_flags(userA, WC_ECC_FLAG_COFACTOR);
  21509. wc_ecc_set_flags(userB, WC_ECC_FLAG_COFACTOR);
  21510. x = ECC_SHARED_SIZE;
  21511. do {
  21512. #if defined(WOLFSSL_ASYNC_CRYPT)
  21513. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21514. #endif
  21515. if (ret == 0)
  21516. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  21517. } while (ret == WC_PENDING_E);
  21518. if (ret != 0)
  21519. ERROR_OUT(-9921, done);
  21520. TEST_SLEEP();
  21521. y = ECC_SHARED_SIZE;
  21522. do {
  21523. #if defined(WOLFSSL_ASYNC_CRYPT)
  21524. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21525. #endif
  21526. if (ret == 0)
  21527. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  21528. } while (ret == WC_PENDING_E);
  21529. if (ret != 0)
  21530. ERROR_OUT(-9922, done);
  21531. if (y != x)
  21532. ERROR_OUT(-9923, done);
  21533. if (XMEMCMP(sharedA, sharedB, x))
  21534. ERROR_OUT(-9924, done);
  21535. TEST_SLEEP();
  21536. /* remove cofactor flag */
  21537. wc_ecc_set_flags(userA, 0);
  21538. wc_ecc_set_flags(userB, 0);
  21539. #endif /* HAVE_ECC_CDH */
  21540. #endif /* HAVE_ECC_DHE */
  21541. #endif /* !WOLFSSL_ATECC508A && WOLFSSL_ATECC608A */
  21542. #ifdef HAVE_ECC_KEY_EXPORT
  21543. x = ECC_KEY_EXPORT_BUF_SIZE;
  21544. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 0);
  21545. if (ret != 0)
  21546. ERROR_OUT(-9925, done);
  21547. #ifdef HAVE_ECC_KEY_IMPORT
  21548. #ifdef WOLFSSL_CUSTOM_CURVES
  21549. if (dp != NULL) {
  21550. ret = wc_ecc_set_custom_curve(pubKey, dp);
  21551. if (ret != 0)
  21552. ERROR_OUT(-9926, done);
  21553. }
  21554. #endif
  21555. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  21556. if (ret != 0)
  21557. ERROR_OUT(-9927, done);
  21558. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  21559. #ifdef HAVE_ECC_DHE
  21560. y = ECC_SHARED_SIZE;
  21561. do {
  21562. #if defined(WOLFSSL_ASYNC_CRYPT)
  21563. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21564. #endif
  21565. if (ret == 0)
  21566. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  21567. } while (ret == WC_PENDING_E);
  21568. if (ret != 0)
  21569. ERROR_OUT(-9928, done);
  21570. if (XMEMCMP(sharedA, sharedB, y))
  21571. ERROR_OUT(-9929, done);
  21572. TEST_SLEEP();
  21573. #endif /* HAVE_ECC_DHE */
  21574. #ifdef HAVE_COMP_KEY
  21575. /* try compressed export / import too */
  21576. x = ECC_KEY_EXPORT_BUF_SIZE;
  21577. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 1);
  21578. if (ret != 0)
  21579. ERROR_OUT(-9930, done);
  21580. wc_ecc_free(pubKey);
  21581. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  21582. if (ret != 0)
  21583. ERROR_OUT(-9931, done);
  21584. #ifdef WOLFSSL_CUSTOM_CURVES
  21585. if (dp != NULL) {
  21586. ret = wc_ecc_set_custom_curve(pubKey, dp);
  21587. if (ret != 0)
  21588. ERROR_OUT(-9932, done);
  21589. }
  21590. #endif
  21591. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  21592. if (ret != 0)
  21593. ERROR_OUT(-9933, done);
  21594. #ifdef HAVE_ECC_DHE
  21595. y = ECC_SHARED_SIZE;
  21596. do {
  21597. #if defined(WOLFSSL_ASYNC_CRYPT)
  21598. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21599. #endif
  21600. if (ret == 0)
  21601. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  21602. } while (ret == WC_PENDING_E);
  21603. if (ret != 0)
  21604. ERROR_OUT(-9934, done);
  21605. if (XMEMCMP(sharedA, sharedB, y))
  21606. ERROR_OUT(-9935, done);
  21607. TEST_SLEEP();
  21608. #endif /* HAVE_ECC_DHE */
  21609. #endif /* HAVE_COMP_KEY */
  21610. #endif /* !WOLFSSL_ATECC508A && !WOLFSSL_ATECC608A */
  21611. #endif /* !WC_NO_RNG */
  21612. #endif /* HAVE_ECC_KEY_IMPORT */
  21613. #endif /* HAVE_ECC_KEY_EXPORT */
  21614. /* For KCAPI cannot sign using generated ECDH key */
  21615. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  21616. !defined(WC_NO_RNG) && !defined(WOLFSSL_KCAPI_ECC))
  21617. #ifdef HAVE_ECC_SIGN
  21618. /* ECC w/out Shamir has issue with all 0 digest */
  21619. /* WC_BIGINT doesn't have 0 len well on hardware */
  21620. /* Cryptocell has issues with all 0 digest */
  21621. #if defined(ECC_SHAMIR) && !defined(WOLFSSL_ASYNC_CRYPT) && \
  21622. !defined(WOLFSSL_CRYPTOCELL)
  21623. /* test DSA sign hash with zeros */
  21624. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  21625. digest[i] = 0;
  21626. }
  21627. x = ECC_SIG_SIZE;
  21628. do {
  21629. #if defined(WOLFSSL_ASYNC_CRYPT)
  21630. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21631. #endif
  21632. if (ret == 0)
  21633. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng,
  21634. userA);
  21635. } while (ret == WC_PENDING_E);
  21636. if (ret != 0)
  21637. ERROR_OUT(-9936, done);
  21638. TEST_SLEEP();
  21639. #ifdef HAVE_ECC_VERIFY
  21640. for (i=0; i<testVerifyCount; i++) {
  21641. verify = 0;
  21642. do {
  21643. #if defined(WOLFSSL_ASYNC_CRYPT)
  21644. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21645. #endif
  21646. if (ret == 0)
  21647. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE,
  21648. &verify, userA);
  21649. } while (ret == WC_PENDING_E);
  21650. if (ret != 0)
  21651. ERROR_OUT(-9937, done);
  21652. if (verify != 1)
  21653. ERROR_OUT(-9938, done);
  21654. TEST_SLEEP();
  21655. }
  21656. #endif /* HAVE_ECC_VERIFY */
  21657. #endif /* ECC_SHAMIR && !WOLFSSL_ASYNC_CRYPT && !WOLFSSL_CRYPTOCELL */
  21658. /* test DSA sign hash with sequence (0,1,2,3,4,...) */
  21659. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  21660. digest[i] = (byte)i;
  21661. }
  21662. x = ECC_SIG_SIZE;
  21663. do {
  21664. #if defined(WOLFSSL_ASYNC_CRYPT)
  21665. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21666. #endif
  21667. if (ret == 0)
  21668. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng, userA);
  21669. } while (ret == WC_PENDING_E);
  21670. if (ret != 0)
  21671. ERROR_OUT(-9939, done);
  21672. TEST_SLEEP();
  21673. #ifdef HAVE_ECC_VERIFY
  21674. for (i=0; i<testVerifyCount; i++) {
  21675. verify = 0;
  21676. do {
  21677. #if defined(WOLFSSL_ASYNC_CRYPT)
  21678. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21679. #endif
  21680. if (ret == 0)
  21681. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE, &verify, userA);
  21682. } while (ret == WC_PENDING_E);
  21683. if (ret != 0)
  21684. ERROR_OUT(-9940, done);
  21685. if (verify != 1)
  21686. ERROR_OUT(-9941, done);
  21687. TEST_SLEEP();
  21688. }
  21689. #endif /* HAVE_ECC_VERIFY */
  21690. #endif /* HAVE_ECC_SIGN */
  21691. #endif /* !ECC_TIMING_RESISTANT || (ECC_TIMING_RESISTANT &&
  21692. * !WC_NO_RNG && !WOLFSSL_KCAPI_ECC) */
  21693. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  21694. !defined(WOLFSSL_ATECC508) && !defined(WOLFSSL_ATECC608A) && \
  21695. !defined(WOLFSSL_KCAPI_ECC)
  21696. x = ECC_KEY_EXPORT_BUF_SIZE;
  21697. ret = wc_ecc_export_private_only(userA, exportBuf, &x);
  21698. if (ret != 0)
  21699. ERROR_OUT(-9942, done);
  21700. #elif defined(HAVE_ECC_KEY_EXPORT)
  21701. (void)exportBuf;
  21702. #endif /* HAVE_ECC_KEY_EXPORT */
  21703. done:
  21704. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21705. if (userA != NULL) {
  21706. wc_ecc_free(userA);
  21707. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21708. }
  21709. if (userB != NULL) {
  21710. wc_ecc_free(userB);
  21711. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21712. }
  21713. if (pubKey != NULL) {
  21714. wc_ecc_free(pubKey);
  21715. XFREE(pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21716. }
  21717. #else
  21718. wc_ecc_free(pubKey);
  21719. wc_ecc_free(userB);
  21720. wc_ecc_free(userA);
  21721. #endif
  21722. #if defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)
  21723. WC_FREE_VAR(sharedA, HEAP_HINT);
  21724. WC_FREE_VAR(sharedB, HEAP_HINT);
  21725. #endif
  21726. #ifdef HAVE_ECC_KEY_EXPORT
  21727. WC_FREE_VAR(exportBuf, HEAP_HINT);
  21728. #endif
  21729. #ifdef HAVE_ECC_SIGN
  21730. WC_FREE_VAR(sig, HEAP_HINT);
  21731. WC_FREE_VAR(digest, HEAP_HINT);
  21732. #endif
  21733. (void)keySize;
  21734. (void)curve_id;
  21735. (void)rng;
  21736. return ret;
  21737. }
  21738. #undef ECC_TEST_VERIFY_COUNT
  21739. #define ECC_TEST_VERIFY_COUNT 2
  21740. static int ecc_test_curve(WC_RNG* rng, int keySize)
  21741. {
  21742. int ret;
  21743. ret = ecc_test_curve_size(rng, keySize, ECC_TEST_VERIFY_COUNT,
  21744. ECC_CURVE_DEF, NULL);
  21745. if (ret < 0) {
  21746. if (ret == ECC_CURVE_OID_E) {
  21747. /* ignore error for curves not found */
  21748. /* some curve sizes are only available with:
  21749. HAVE_ECC_SECPR2, HAVE_ECC_SECPR3, HAVE_ECC_BRAINPOOL
  21750. and HAVE_ECC_KOBLITZ */
  21751. }
  21752. else {
  21753. printf("ecc_test_curve_size %d failed!: %d\n", keySize, ret);
  21754. return ret;
  21755. }
  21756. }
  21757. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  21758. #ifdef HAVE_ECC_VECTOR_TEST
  21759. ret = ecc_test_vector(keySize);
  21760. if (ret < 0) {
  21761. printf("ecc_test_vector %d failed!: %d\n", keySize, ret);
  21762. return ret;
  21763. }
  21764. #endif
  21765. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  21766. !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  21767. ret = ecc_test_key_decode(rng, keySize);
  21768. if (ret < 0) {
  21769. if (ret == ECC_CURVE_OID_E) {
  21770. /* ignore error for curves not found */
  21771. }
  21772. else {
  21773. printf("ecc_test_key_decode %d failed!: %d\n", keySize, ret);
  21774. return ret;
  21775. }
  21776. }
  21777. #endif
  21778. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  21779. ret = ecc_test_key_gen(rng, keySize);
  21780. if (ret < 0) {
  21781. if (ret == ECC_CURVE_OID_E) {
  21782. /* ignore error for curves not found */
  21783. }
  21784. else {
  21785. printf("ecc_test_key_gen %d failed!: %d\n", keySize, ret);
  21786. return ret;
  21787. }
  21788. }
  21789. #endif
  21790. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  21791. return 0;
  21792. }
  21793. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  21794. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  21795. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  21796. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21797. static int ecc_point_test(void)
  21798. {
  21799. int ret;
  21800. ecc_point* point;
  21801. ecc_point* point2;
  21802. #ifdef HAVE_COMP_KEY
  21803. ecc_point* point3;
  21804. ecc_point* point4;
  21805. #endif
  21806. word32 outLen;
  21807. byte out[65];
  21808. byte der[] = { 0x04, /* = Uncompressed */
  21809. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21810. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21811. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21812. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21813. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21814. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21815. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21816. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  21817. #if defined(HAVE_COMP_KEY) && (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  21818. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  21819. byte derComp0[] = { 0x02, /* = Compressed, y even */
  21820. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21821. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21822. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21823. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  21824. byte derComp1[] = { 0x03, /* = Compressed, y odd */
  21825. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21826. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21827. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21828. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  21829. #endif
  21830. byte altDer[] = { 0x04, /* = Uncompressed */
  21831. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21832. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21833. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21834. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21835. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21836. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21837. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21838. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
  21839. int curve_idx = wc_ecc_get_curve_idx(ECC_SECP256R1);
  21840. /* if curve P256 is not enabled then test should not fail */
  21841. if (curve_idx == ECC_CURVE_INVALID)
  21842. return 0;
  21843. outLen = sizeof(out);
  21844. point = wc_ecc_new_point();
  21845. if (point == NULL)
  21846. return -10000;
  21847. point2 = wc_ecc_new_point();
  21848. if (point2 == NULL) {
  21849. wc_ecc_del_point(point);
  21850. return -10001;
  21851. }
  21852. #ifdef HAVE_COMP_KEY
  21853. point3 = wc_ecc_new_point();
  21854. if (point3 == NULL) {
  21855. wc_ecc_del_point(point2);
  21856. wc_ecc_del_point(point);
  21857. return -10002;
  21858. }
  21859. point4 = wc_ecc_new_point();
  21860. if (point4 == NULL) {
  21861. wc_ecc_del_point(point3);
  21862. wc_ecc_del_point(point2);
  21863. wc_ecc_del_point(point);
  21864. return -10003;
  21865. }
  21866. #endif
  21867. /* Parameter Validation testing. */
  21868. wc_ecc_del_point(NULL);
  21869. ret = wc_ecc_import_point_der(NULL, sizeof(der), curve_idx, point);
  21870. if (ret != ECC_BAD_ARG_E) {
  21871. ret = -10004;
  21872. goto done;
  21873. }
  21874. ret = wc_ecc_import_point_der(der, sizeof(der), ECC_CURVE_INVALID, point);
  21875. if (ret != ECC_BAD_ARG_E) {
  21876. ret = -10005;
  21877. goto done;
  21878. }
  21879. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, NULL);
  21880. if (ret != ECC_BAD_ARG_E) {
  21881. ret = -10006;
  21882. goto done;
  21883. }
  21884. ret = wc_ecc_export_point_der(-1, point, out, &outLen);
  21885. if (ret != ECC_BAD_ARG_E) {
  21886. ret = -10007;
  21887. goto done;
  21888. }
  21889. ret = wc_ecc_export_point_der(curve_idx, NULL, out, &outLen);
  21890. if (ret != ECC_BAD_ARG_E) {
  21891. ret = -10008;
  21892. goto done;
  21893. }
  21894. ret = wc_ecc_export_point_der(curve_idx, point, NULL, &outLen);
  21895. if (ret != LENGTH_ONLY_E || outLen != sizeof(out)) {
  21896. ret = -10009;
  21897. goto done;
  21898. }
  21899. ret = wc_ecc_export_point_der(curve_idx, point, out, NULL);
  21900. if (ret != ECC_BAD_ARG_E) {
  21901. ret = -10010;
  21902. goto done;
  21903. }
  21904. outLen = 0;
  21905. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  21906. if (ret != BUFFER_E) {
  21907. ret = -10011;
  21908. goto done;
  21909. }
  21910. ret = wc_ecc_copy_point(NULL, NULL);
  21911. if (ret != ECC_BAD_ARG_E) {
  21912. ret = -10012;
  21913. goto done;
  21914. }
  21915. ret = wc_ecc_copy_point(NULL, point2);
  21916. if (ret != ECC_BAD_ARG_E) {
  21917. ret = -10013;
  21918. goto done;
  21919. }
  21920. ret = wc_ecc_copy_point(point, NULL);
  21921. if (ret != ECC_BAD_ARG_E) {
  21922. ret = -10014;
  21923. goto done;
  21924. }
  21925. ret = wc_ecc_cmp_point(NULL, NULL);
  21926. if (ret != BAD_FUNC_ARG) {
  21927. ret = -10015;
  21928. goto done;
  21929. }
  21930. ret = wc_ecc_cmp_point(NULL, point2);
  21931. if (ret != BAD_FUNC_ARG) {
  21932. ret = -10016;
  21933. goto done;
  21934. }
  21935. ret = wc_ecc_cmp_point(point, NULL);
  21936. if (ret != BAD_FUNC_ARG) {
  21937. ret = -10017;
  21938. goto done;
  21939. }
  21940. /* Use API. */
  21941. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, point);
  21942. if (ret != 0) {
  21943. ret = -10018;
  21944. goto done;
  21945. }
  21946. outLen = sizeof(out);
  21947. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  21948. if (ret != 0) {
  21949. ret = -10019;
  21950. goto done;
  21951. }
  21952. if (outLen != sizeof(der)) {
  21953. ret = -10020;
  21954. goto done;
  21955. }
  21956. if (XMEMCMP(out, der, outLen) != 0) {
  21957. ret = -10021;
  21958. goto done;
  21959. }
  21960. ret = wc_ecc_copy_point(point2, point);
  21961. if (ret != MP_OKAY) {
  21962. ret = -10022;
  21963. goto done;
  21964. }
  21965. ret = wc_ecc_cmp_point(point2, point);
  21966. if (ret != MP_EQ) {
  21967. ret = -10023;
  21968. goto done;
  21969. }
  21970. ret = wc_ecc_import_point_der(altDer, sizeof(altDer), curve_idx, point2);
  21971. if (ret != 0) {
  21972. ret = -10024;
  21973. goto done;
  21974. }
  21975. ret = wc_ecc_cmp_point(point2, point);
  21976. if (ret != MP_GT) {
  21977. ret = -10025;
  21978. goto done;
  21979. }
  21980. #if defined(HAVE_COMP_KEY) && (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  21981. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  21982. ret = wc_ecc_import_point_der(derComp0, sizeof(derComp0)*2-1, curve_idx, point3);
  21983. if (ret != 0) {
  21984. ret = -10026;
  21985. goto done;
  21986. }
  21987. ret = wc_ecc_import_point_der_ex(derComp0, sizeof(derComp0), curve_idx, point4, 0);
  21988. if (ret != 0) {
  21989. ret = -10027;
  21990. goto done;
  21991. }
  21992. ret = wc_ecc_cmp_point(point3, point4);
  21993. if (ret != MP_EQ) {
  21994. ret = -10028;
  21995. goto done;
  21996. }
  21997. ret = wc_ecc_import_point_der(derComp1, sizeof(derComp1)*2-1, curve_idx, point3);
  21998. if (ret != 0) {
  21999. ret = -10029;
  22000. goto done;
  22001. }
  22002. ret = wc_ecc_import_point_der_ex(derComp1, sizeof(derComp1), curve_idx, point4, 0);
  22003. if (ret != 0) {
  22004. ret = -10030;
  22005. goto done;
  22006. }
  22007. ret = wc_ecc_cmp_point(point3, point4);
  22008. if (ret != MP_EQ) {
  22009. ret = -10031;
  22010. goto done;
  22011. }
  22012. #endif
  22013. done:
  22014. #ifdef HAVE_COMP_KEY
  22015. wc_ecc_del_point(point4);
  22016. wc_ecc_del_point(point3);
  22017. #endif
  22018. wc_ecc_del_point(point2);
  22019. wc_ecc_del_point(point);
  22020. return ret;
  22021. }
  22022. #endif /* !WOLFSSL_ATECC508A && HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  22023. #if !defined(NO_SIG_WRAPPER) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  22024. static int ecc_sig_test(WC_RNG* rng, ecc_key* key)
  22025. {
  22026. int ret;
  22027. word32 sigSz;
  22028. int size;
  22029. byte out[ECC_MAX_SIG_SIZE];
  22030. byte in[] = TEST_STRING;
  22031. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  22032. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  22033. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  22034. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  22035. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  22036. };
  22037. word32 inLen = (word32)XSTRLEN((char*)in);
  22038. size = wc_ecc_sig_size(key);
  22039. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, sizeof(*key));
  22040. if (ret != size)
  22041. return -10040;
  22042. sigSz = (word32)ret;
  22043. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  22044. inLen, out, &sigSz, key, sizeof(*key), rng);
  22045. if (ret != 0)
  22046. return -10041;
  22047. TEST_SLEEP();
  22048. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  22049. inLen, out, sigSz, key, sizeof(*key));
  22050. if (ret != 0)
  22051. return -10042;
  22052. TEST_SLEEP();
  22053. sigSz = (word32)sizeof(out);
  22054. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  22055. hash, (int)sizeof(hash), out, &sigSz, key, sizeof(*key), rng);
  22056. if (ret != 0)
  22057. return -10043;
  22058. TEST_SLEEP();
  22059. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  22060. hash, (int)sizeof(hash), out, sigSz, key, sizeof(*key));
  22061. if (ret != 0)
  22062. return -10044;
  22063. TEST_SLEEP();
  22064. return 0;
  22065. }
  22066. #endif
  22067. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  22068. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  22069. static int ecc_exp_imp_test(ecc_key* key)
  22070. {
  22071. int ret;
  22072. int curve_id;
  22073. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22074. ecc_key *keyImp = (ecc_key *)XMALLOC(sizeof *keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22075. #else
  22076. ecc_key keyImp[1];
  22077. #endif
  22078. byte priv[32];
  22079. word32 privLen;
  22080. byte pub[65*2];
  22081. word32 pubLen, pubLenX, pubLenY;
  22082. const char qx[] = "7a4e287890a1a47ad3457e52f2f76a83"
  22083. "ce46cbc947616d0cbaa82323818a793d";
  22084. const char qy[] = "eec4084f5b29ebf29c44cce3b3059610"
  22085. "922f8b30ea6e8811742ac7238fe87308";
  22086. const char d[] = "8c14b793cb19137e323a6d2e2a870bca"
  22087. "2e7a493ec1153b3a95feb8a4873f8d08";
  22088. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22089. if (keyImp == NULL)
  22090. ERROR_OUT(-10050, done);
  22091. #endif
  22092. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  22093. privLen = sizeof(priv);
  22094. ret = wc_ecc_export_private_only(key, priv, &privLen);
  22095. if (ret != 0) {
  22096. ret = -10051;
  22097. goto done;
  22098. }
  22099. pubLen = sizeof(pub);
  22100. ret = wc_ecc_export_point_der(key->idx, &key->pubkey, pub, &pubLen);
  22101. if (ret != 0) {
  22102. ret = -10052;
  22103. goto done;
  22104. }
  22105. ret = wc_ecc_import_private_key(priv, privLen, pub, pubLen, keyImp);
  22106. if (ret != 0) {
  22107. ret = -10053;
  22108. goto done;
  22109. }
  22110. wc_ecc_free(keyImp);
  22111. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  22112. ret = wc_ecc_import_raw_ex(keyImp, qx, qy, d, ECC_SECP256R1);
  22113. if (ret != 0) {
  22114. ret = -10054;
  22115. goto done;
  22116. }
  22117. wc_ecc_free(keyImp);
  22118. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  22119. curve_id = wc_ecc_get_curve_id(key->idx);
  22120. if (curve_id < 0) {
  22121. ret = -10055;
  22122. goto done;
  22123. }
  22124. /* test import private only */
  22125. ret = wc_ecc_import_private_key_ex(priv, privLen, NULL, 0, keyImp,
  22126. curve_id);
  22127. if (ret != 0) {
  22128. ret = -10056;
  22129. goto done;
  22130. }
  22131. wc_ecc_free(keyImp);
  22132. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  22133. /* test export public raw */
  22134. pubLenX = pubLenY = 32;
  22135. ret = wc_ecc_export_public_raw(key, pub, &pubLenX, &pub[32], &pubLenY);
  22136. if (ret != 0) {
  22137. ret = -10057;
  22138. goto done;
  22139. }
  22140. #ifndef HAVE_SELFTEST
  22141. /* test import of public */
  22142. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], NULL, ECC_SECP256R1);
  22143. if (ret != 0) {
  22144. ret = -10058;
  22145. goto done;
  22146. }
  22147. #endif
  22148. wc_ecc_free(keyImp);
  22149. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  22150. /* test export private and public raw */
  22151. pubLenX = pubLenY = privLen = 32;
  22152. ret = wc_ecc_export_private_raw(key, pub, &pubLenX, &pub[32], &pubLenY,
  22153. priv, &privLen);
  22154. if (ret != 0) {
  22155. ret = -10059;
  22156. goto done;
  22157. }
  22158. #ifndef HAVE_SELFTEST
  22159. /* test import of private and public */
  22160. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], priv, ECC_SECP256R1);
  22161. if (ret != 0) {
  22162. ret = -10060;
  22163. goto done;
  22164. }
  22165. #endif
  22166. done:
  22167. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22168. if (keyImp != NULL) {
  22169. wc_ecc_free(keyImp);
  22170. XFREE(keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22171. }
  22172. #else
  22173. wc_ecc_free(keyImp);
  22174. #endif
  22175. return ret;
  22176. }
  22177. #endif /* HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  22178. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT) && \
  22179. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  22180. static int ecc_mulmod_test(ecc_key* key1)
  22181. {
  22182. int ret;
  22183. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22184. ecc_key *key2 = (ecc_key *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22185. ecc_key *key3 = (ecc_key *)XMALLOC(sizeof *key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22186. #else
  22187. ecc_key key2[1];
  22188. ecc_key key3[1];
  22189. #endif
  22190. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22191. if ((key2 == NULL) || (key3 == NULL))
  22192. ERROR_OUT(MEMORY_E, done);
  22193. #endif
  22194. wc_ecc_init_ex(key2, HEAP_HINT, devId);
  22195. wc_ecc_init_ex(key3, HEAP_HINT, devId);
  22196. /* TODO: Use test data, test with WOLFSSL_VALIDATE_ECC_IMPORT. */
  22197. /* Need base point (Gx,Gy) and parameter A - load them as the public and
  22198. * private key in key2.
  22199. */
  22200. ret = wc_ecc_import_raw_ex(key2, key1->dp->Gx, key1->dp->Gy, key1->dp->Af,
  22201. ECC_SECP256R1);
  22202. if (ret != 0)
  22203. goto done;
  22204. /* Need a point (Gx,Gy) and prime - load them as the public and private key
  22205. * in key3.
  22206. */
  22207. ret = wc_ecc_import_raw_ex(key3, key1->dp->Gx, key1->dp->Gy,
  22208. key1->dp->prime, ECC_SECP256R1);
  22209. if (ret != 0)
  22210. goto done;
  22211. ret = wc_ecc_mulmod(&key1->k, &key2->pubkey, &key3->pubkey, &key2->k, &key3->k,
  22212. 1);
  22213. if (ret != 0) {
  22214. ret = -10070;
  22215. goto done;
  22216. }
  22217. done:
  22218. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22219. if (key2 != NULL) {
  22220. wc_ecc_free(key2);
  22221. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22222. }
  22223. if (key3 != NULL) {
  22224. wc_ecc_free(key3);
  22225. XFREE(key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22226. }
  22227. #else
  22228. wc_ecc_free(key3);
  22229. wc_ecc_free(key2);
  22230. #endif
  22231. return ret;
  22232. }
  22233. #endif
  22234. #if defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG) && \
  22235. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  22236. static int ecc_ssh_test(ecc_key* key, WC_RNG* rng)
  22237. {
  22238. int ret;
  22239. byte out[128];
  22240. word32 outLen = sizeof(out);
  22241. /* Parameter Validation testing. */
  22242. ret = wc_ecc_shared_secret_ssh(NULL, &key->pubkey, out, &outLen);
  22243. if (ret != BAD_FUNC_ARG)
  22244. return -10080;
  22245. ret = wc_ecc_shared_secret_ssh(key, NULL, out, &outLen);
  22246. if (ret != BAD_FUNC_ARG)
  22247. return -10081;
  22248. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, NULL, &outLen);
  22249. if (ret != BAD_FUNC_ARG)
  22250. return -10082;
  22251. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, NULL);
  22252. if (ret != BAD_FUNC_ARG)
  22253. return -10083;
  22254. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22255. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22256. !defined(HAVE_SELFTEST)
  22257. ret = wc_ecc_set_rng(key, rng);
  22258. if (ret != 0)
  22259. return -10084;
  22260. #else
  22261. (void)rng;
  22262. #endif
  22263. /* Use API. */
  22264. ret = 0;
  22265. do {
  22266. #if defined(WOLFSSL_ASYNC_CRYPT)
  22267. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22268. #endif
  22269. if (ret == 0)
  22270. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, &outLen);
  22271. } while (ret == WC_PENDING_E);
  22272. if (ret != 0)
  22273. return -10085;
  22274. TEST_SLEEP();
  22275. return 0;
  22276. }
  22277. #endif /* HAVE_ECC_DHE && !WC_NO_RNG */
  22278. static int ecc_def_curve_test(WC_RNG *rng)
  22279. {
  22280. int ret;
  22281. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22282. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22283. #else
  22284. ecc_key key[1];
  22285. #endif
  22286. #if (defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)) || \
  22287. (defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  22288. word32 idx = 0;
  22289. #endif
  22290. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22291. if (key == NULL)
  22292. ERROR_OUT(MEMORY_E, done);
  22293. #endif
  22294. wc_ecc_init_ex(key, HEAP_HINT, devId);
  22295. /* Use API */
  22296. ret = wc_ecc_set_flags(NULL, 0);
  22297. if (ret != BAD_FUNC_ARG) {
  22298. ret = -10090;
  22299. goto done;
  22300. }
  22301. ret = wc_ecc_set_flags(key, 0);
  22302. if (ret != 0) {
  22303. ret = -10091;
  22304. goto done;
  22305. }
  22306. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  22307. #ifndef WC_NO_RNG
  22308. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  22309. #if defined(WOLFSSL_ASYNC_CRYPT)
  22310. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  22311. #endif
  22312. if (ret != 0) {
  22313. goto done;
  22314. }
  22315. #ifndef NO_SIG_WRAPPER
  22316. ret = ecc_sig_test(rng, key);
  22317. if (ret < 0)
  22318. goto done;
  22319. #endif
  22320. TEST_SLEEP();
  22321. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_CRYPTOCELL) && \
  22322. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  22323. ret = ecc_ssh_test(key, rng);
  22324. if (ret < 0)
  22325. goto done;
  22326. #endif
  22327. wc_ecc_free(key);
  22328. #else
  22329. (void)rng;
  22330. #endif /* !WC_NO_RNG */
  22331. #if (defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)) || \
  22332. (defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  22333. /* Use test ECC key - ensure real private "d" exists */
  22334. #ifdef USE_CERT_BUFFERS_256
  22335. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, key,
  22336. sizeof_ecc_key_der_256);
  22337. #else
  22338. {
  22339. XFILE file = XFOPEN(eccKeyDerFile, "rb");
  22340. byte der[128];
  22341. word32 derSz;
  22342. if (!file) {
  22343. ERROR_OUT(-10093, done);
  22344. }
  22345. derSz = (word32)XFREAD(der, 1, sizeof(der), file);
  22346. XFCLOSE(file);
  22347. ret = wc_EccPrivateKeyDecode(der, &idx, key, derSz);
  22348. }
  22349. #endif
  22350. if (ret != 0) {
  22351. goto done;
  22352. }
  22353. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)
  22354. ret = ecc_exp_imp_test(key);
  22355. if (ret < 0)
  22356. goto done;
  22357. #endif
  22358. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT) && \
  22359. !defined(WOLFSSL_CRYPTOCELL)
  22360. ret = ecc_mulmod_test(key);
  22361. if (ret < 0)
  22362. goto done;
  22363. #endif
  22364. #endif
  22365. #else
  22366. (void)rng;
  22367. (void)idx;
  22368. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  22369. done:
  22370. wc_ecc_free(key);
  22371. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22372. if (key != NULL) {
  22373. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22374. }
  22375. #endif
  22376. return ret;
  22377. }
  22378. #endif /* !NO_ECC256 || HAVE_ALL_CURVES */
  22379. #if defined(WOLFSSL_CERT_EXT) && \
  22380. (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  22381. static int ecc_decode_test(void)
  22382. {
  22383. int ret;
  22384. word32 inSz;
  22385. word32 inOutIdx;
  22386. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22387. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22388. #else
  22389. ecc_key key[1];
  22390. #endif
  22391. /* SECP256R1 OID: 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07 */
  22392. /* This is ecc_clikeypub_der_256. */
  22393. WOLFSSL_SMALL_STACK_STATIC const byte good[] = {
  22394. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce,
  22395. 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
  22396. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xbf, 0xf4,
  22397. 0x0f, 0x44, 0x50, 0x9a, 0x3d, 0xce, 0x9b, 0xb7, 0xf0, 0xc5,
  22398. 0x4d, 0xf5, 0x70, 0x7b, 0xd4, 0xec, 0x24, 0x8e, 0x19, 0x80,
  22399. 0xec, 0x5a, 0x4c, 0xa2, 0x24, 0x03, 0x62, 0x2c, 0x9b, 0xda,
  22400. 0xef, 0xa2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xc6, 0x56,
  22401. 0x95, 0x06, 0xcc, 0x01, 0xa9, 0xbd, 0xf6, 0x75, 0x1a, 0x42,
  22402. 0xf7, 0xbd, 0xa9, 0xb2, 0x36, 0x22, 0x5f, 0xc7, 0x5d, 0x7f,
  22403. 0xb4 };
  22404. WOLFSSL_SMALL_STACK_STATIC const byte badNoObjId[] = { 0x30, 0x08, 0x30, 0x06, 0x03, 0x04,
  22405. 0x00, 0x04, 0x01, 0x01 };
  22406. WOLFSSL_SMALL_STACK_STATIC const byte badOneObjId[] = { 0x30, 0x0a, 0x30, 0x08, 0x06, 0x00,
  22407. 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  22408. WOLFSSL_SMALL_STACK_STATIC const byte badObjId1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x09,
  22409. 0x06, 0x00, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  22410. WOLFSSL_SMALL_STACK_STATIC const byte badObj2d1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x00,
  22411. 0x06, 0x07, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  22412. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitStr[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  22413. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  22414. 0x04, 0x04, 0x00, 0x04, 0x01, 0x01 };
  22415. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrLen[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  22416. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  22417. 0x03, 0x05, 0x00, 0x04, 0x01, 0x01 };
  22418. WOLFSSL_SMALL_STACK_STATIC const byte badNoBitStrZero[] = { 0x30, 0x13, 0x30, 0x0a, 0x06, 0x00,
  22419. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  22420. 0x03, 0x03, 0x04, 0x01, 0x01 };
  22421. WOLFSSL_SMALL_STACK_STATIC const byte badPoint[] = { 0x30, 0x12, 0x30, 0x09, 0x06, 0x00,
  22422. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  22423. 0x03, 0x03, 0x00, 0x04, 0x01 };
  22424. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22425. if (key == NULL)
  22426. ERROR_OUT(MEMORY_E, done);
  22427. #endif
  22428. XMEMSET(key, 0, sizeof *key);
  22429. wc_ecc_init_ex(key, HEAP_HINT, devId);
  22430. inSz = sizeof(good);
  22431. ret = wc_EccPublicKeyDecode(NULL, &inOutIdx, key, inSz);
  22432. if (ret != BAD_FUNC_ARG) {
  22433. ret = -10100;
  22434. goto done;
  22435. }
  22436. ret = wc_EccPublicKeyDecode(good, NULL, key, inSz);
  22437. if (ret != BAD_FUNC_ARG) {
  22438. ret = -10101;
  22439. goto done;
  22440. }
  22441. ret = wc_EccPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  22442. if (ret != BAD_FUNC_ARG) {
  22443. ret = -10102;
  22444. goto done;
  22445. }
  22446. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, 0);
  22447. if (ret != BAD_FUNC_ARG) {
  22448. ret = -10103;
  22449. goto done;
  22450. }
  22451. /* Change offset to produce bad input data. */
  22452. inOutIdx = 2;
  22453. inSz = sizeof(good) - inOutIdx;
  22454. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  22455. if (ret != ASN_PARSE_E) {
  22456. ret = -10104;
  22457. goto done;
  22458. }
  22459. inOutIdx = 4;
  22460. inSz = sizeof(good) - inOutIdx;
  22461. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  22462. if (ret != ASN_PARSE_E) {
  22463. ret = -10105;
  22464. goto done;
  22465. }
  22466. /* Bad data. */
  22467. inSz = sizeof(badNoObjId);
  22468. inOutIdx = 0;
  22469. ret = wc_EccPublicKeyDecode(badNoObjId, &inOutIdx, key, inSz);
  22470. if (ret != ASN_OBJECT_ID_E && ret != ASN_PARSE_E) {
  22471. ret = -10106;
  22472. goto done;
  22473. }
  22474. inSz = sizeof(badOneObjId);
  22475. inOutIdx = 0;
  22476. ret = wc_EccPublicKeyDecode(badOneObjId, &inOutIdx, key, inSz);
  22477. if (ret != ASN_OBJECT_ID_E && ret != ASN_PARSE_E) {
  22478. ret = -10107;
  22479. goto done;
  22480. }
  22481. inSz = sizeof(badObjId1Len);
  22482. inOutIdx = 0;
  22483. ret = wc_EccPublicKeyDecode(badObjId1Len, &inOutIdx, key, inSz);
  22484. if (ret != ASN_PARSE_E) {
  22485. ret = -10108;
  22486. goto done;
  22487. }
  22488. inSz = sizeof(badObj2d1Len);
  22489. inOutIdx = 0;
  22490. ret = wc_EccPublicKeyDecode(badObj2d1Len, &inOutIdx, key, inSz);
  22491. if (ret != ASN_PARSE_E) {
  22492. ret = -10109;
  22493. goto done;
  22494. }
  22495. inSz = sizeof(badNotBitStr);
  22496. inOutIdx = 0;
  22497. ret = wc_EccPublicKeyDecode(badNotBitStr, &inOutIdx, key, inSz);
  22498. if (ret != ASN_BITSTR_E && ret != ASN_PARSE_E) {
  22499. ret = -10110;
  22500. goto done;
  22501. }
  22502. inSz = sizeof(badBitStrLen);
  22503. inOutIdx = 0;
  22504. ret = wc_EccPublicKeyDecode(badBitStrLen, &inOutIdx, key, inSz);
  22505. if (ret != ASN_PARSE_E) {
  22506. ret = -10111;
  22507. goto done;
  22508. }
  22509. inSz = sizeof(badNoBitStrZero);
  22510. inOutIdx = 0;
  22511. ret = wc_EccPublicKeyDecode(badNoBitStrZero, &inOutIdx, key, inSz);
  22512. if (ret != ASN_EXPECT_0_E && ret != ASN_PARSE_E) {
  22513. ret = -10112;
  22514. goto done;
  22515. }
  22516. inSz = sizeof(badPoint);
  22517. inOutIdx = 0;
  22518. ret = wc_EccPublicKeyDecode(badPoint, &inOutIdx, key, inSz);
  22519. if (ret != ASN_ECC_KEY_E && ret != ASN_PARSE_E) {
  22520. ret = -10113;
  22521. goto done;
  22522. }
  22523. inSz = sizeof(good);
  22524. inOutIdx = 0;
  22525. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  22526. if (ret != 0) {
  22527. ret = -10114;
  22528. goto done;
  22529. }
  22530. done:
  22531. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22532. if (key != NULL) {
  22533. wc_ecc_free(key);
  22534. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22535. }
  22536. #else
  22537. wc_ecc_free(key);
  22538. #endif
  22539. return ret;
  22540. }
  22541. #endif /* WOLFSSL_CERT_EXT */
  22542. #ifdef WOLFSSL_CUSTOM_CURVES
  22543. static const byte eccKeyExplicitCurve[] = {
  22544. 0x30, 0x81, 0xf5, 0x30, 0x81, 0xae, 0x06, 0x07,
  22545. 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x30,
  22546. 0x81, 0xa2, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06,
  22547. 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01,
  22548. 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
  22549. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  22550. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  22551. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff,
  22552. 0xff, 0xfc, 0x2f, 0x30, 0x06, 0x04, 0x01, 0x00,
  22553. 0x04, 0x01, 0x07, 0x04, 0x41, 0x04, 0x79, 0xbe,
  22554. 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0,
  22555. 0x62, 0x95, 0xce, 0x87, 0x0b, 0x07, 0x02, 0x9b,
  22556. 0xfc, 0xdb, 0x2d, 0xce, 0x28, 0xd9, 0x59, 0xf2,
  22557. 0x81, 0x5b, 0x16, 0xf8, 0x17, 0x98, 0x48, 0x3a,
  22558. 0xda, 0x77, 0x26, 0xa3, 0xc4, 0x65, 0x5d, 0xa4,
  22559. 0xfb, 0xfc, 0x0e, 0x11, 0x08, 0xa8, 0xfd, 0x17,
  22560. 0xb4, 0x48, 0xa6, 0x85, 0x54, 0x19, 0x9c, 0x47,
  22561. 0xd0, 0x8f, 0xfb, 0x10, 0xd4, 0xb8, 0x02, 0x21,
  22562. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  22563. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  22564. 0xfe, 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0,
  22565. 0x3b, 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41,
  22566. 0x41, 0x02, 0x01, 0x01, 0x03, 0x42, 0x00, 0x04,
  22567. 0x3c, 0x4c, 0xc9, 0x5e, 0x2e, 0xa2, 0x3d, 0x49,
  22568. 0xcc, 0x5b, 0xff, 0x4f, 0xc9, 0x2e, 0x1d, 0x4a,
  22569. 0xc6, 0x21, 0xf6, 0xf3, 0xe6, 0x0b, 0x4f, 0xa9,
  22570. 0x9d, 0x74, 0x99, 0xdd, 0x97, 0xc7, 0x6e, 0xbe,
  22571. 0x14, 0x2b, 0x39, 0x9d, 0x63, 0xc7, 0x97, 0x0d,
  22572. 0x45, 0x25, 0x40, 0x30, 0x77, 0x05, 0x76, 0x88,
  22573. 0x38, 0x96, 0x29, 0x7d, 0x9c, 0xe1, 0x50, 0xbe,
  22574. 0xac, 0xf0, 0x1d, 0x86, 0xf4, 0x2f, 0x65, 0x0b
  22575. };
  22576. static int ecc_test_custom_curves(WC_RNG* rng)
  22577. {
  22578. int ret;
  22579. word32 inOutIdx;
  22580. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22581. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22582. #else
  22583. ecc_key key[1];
  22584. #endif
  22585. /* test use of custom curve - using BRAINPOOLP256R1 for test */
  22586. #if defined(HAVE_ECC_BRAINPOOL) && !defined(HAVE_INTEL_QA)
  22587. #ifndef WOLFSSL_ECC_CURVE_STATIC
  22588. WOLFSSL_SMALL_STACK_STATIC const ecc_oid_t ecc_oid_brainpoolp256r1[] = {
  22589. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07
  22590. };
  22591. #define ecc_oid_brainpoolp256r1_sz \
  22592. (sizeof(ecc_oid_brainpoolp256r1) / sizeof(ecc_oid_t))
  22593. #else
  22594. #define ecc_oid_brainpoolp256r1 { \
  22595. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07 \
  22596. }
  22597. #define ecc_oid_brainpoolp256r1_sz 9
  22598. #endif
  22599. #define ecc_oid_brainpoolp256r1_sum 104
  22600. WOLFSSL_SMALL_STACK_STATIC const ecc_set_type ecc_dp_brainpool256r1 = {
  22601. 32, /* size/bytes */
  22602. ECC_CURVE_CUSTOM, /* ID */
  22603. "BRAINPOOLP256R1", /* curve name */
  22604. "A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377", /* prime */
  22605. "7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9", /* A */
  22606. "26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6", /* B */
  22607. "A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7", /* order */
  22608. "8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262", /* Gx */
  22609. "547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997", /* Gy */
  22610. ecc_oid_brainpoolp256r1, /* oid/oidSz */
  22611. ecc_oid_brainpoolp256r1_sz,
  22612. ecc_oid_brainpoolp256r1_sum, /* oid sum */
  22613. 1, /* cofactor */
  22614. };
  22615. #endif /* HAVE_ECC_BRAINPOOL */
  22616. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22617. if (! key) {
  22618. ret = MEMORY_E;
  22619. goto done;
  22620. }
  22621. #endif
  22622. XMEMSET(key, 0, sizeof *key);
  22623. #if defined(HAVE_ECC_BRAINPOOL) && !defined(HAVE_INTEL_QA)
  22624. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, ECC_CURVE_DEF,
  22625. &ecc_dp_brainpool256r1);
  22626. if (ret != 0) {
  22627. printf("ECC test for custom curve failed! %d\n", ret);
  22628. goto done;
  22629. }
  22630. #endif
  22631. #if defined(HAVE_ECC_BRAINPOOL) || defined(HAVE_ECC_KOBLITZ)
  22632. {
  22633. int curve_id;
  22634. #ifdef HAVE_ECC_BRAINPOOL
  22635. curve_id = ECC_BRAINPOOLP256R1;
  22636. #else
  22637. curve_id = ECC_SECP256K1;
  22638. #endif
  22639. /* Test and demonstrate use of non-SECP curve */
  22640. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, curve_id, NULL);
  22641. if (ret < 0) {
  22642. printf("ECC test for curve_id %d failed! %d\n", curve_id, ret);
  22643. goto done;
  22644. }
  22645. }
  22646. #endif
  22647. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  22648. if (ret != 0) {
  22649. ret = -10120;
  22650. goto done;
  22651. }
  22652. inOutIdx = 0;
  22653. ret = wc_EccPublicKeyDecode(eccKeyExplicitCurve, &inOutIdx, key,
  22654. sizeof(eccKeyExplicitCurve));
  22655. if (ret != 0)
  22656. ret = -10121;
  22657. done:
  22658. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22659. if (key) {
  22660. wc_ecc_free(key);
  22661. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22662. }
  22663. #else
  22664. wc_ecc_free(key);
  22665. #endif
  22666. (void)rng;
  22667. return ret;
  22668. }
  22669. #endif /* WOLFSSL_CUSTOM_CURVES */
  22670. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  22671. /* Make Cert / Sign example for ECC cert and ECC CA */
  22672. static int ecc_test_cert_gen(WC_RNG* rng)
  22673. {
  22674. int ret;
  22675. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22676. Cert *myCert = (Cert *)XMALLOC(sizeof *myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22677. #ifdef WOLFSSL_TEST_CERT
  22678. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22679. #endif
  22680. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22681. ecc_key *certPubKey = (ecc_key *)XMALLOC(sizeof *certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22682. #else
  22683. Cert myCert[1];
  22684. #ifdef WOLFSSL_TEST_CERT
  22685. DecodedCert decode[1];
  22686. #endif
  22687. ecc_key caEccKey[1];
  22688. ecc_key certPubKey[1];
  22689. #endif
  22690. int certSz;
  22691. size_t bytes;
  22692. word32 idx = 0;
  22693. #ifndef USE_CERT_BUFFERS_256
  22694. XFILE file;
  22695. #endif
  22696. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22697. byte* der = NULL;
  22698. #else
  22699. byte der[FOURK_BUF];
  22700. #endif
  22701. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22702. if ((myCert == NULL)
  22703. #ifdef WOLFSSL_TEST_CERT
  22704. || (decode == NULL)
  22705. #endif
  22706. || (caEccKey == NULL) || (certPubKey == NULL))
  22707. ERROR_OUT(MEMORY_E, exit);
  22708. #endif
  22709. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  22710. XMEMSET(certPubKey, 0, sizeof *certPubKey);
  22711. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22712. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22713. if (der == NULL) {
  22714. ERROR_OUT(-10130, exit);
  22715. }
  22716. #endif
  22717. /* Get cert private key */
  22718. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  22719. /* Get Cert Key 384 */
  22720. #ifdef USE_CERT_BUFFERS_256
  22721. XMEMCPY(der, ca_ecc_key_der_384, sizeof_ca_ecc_key_der_384);
  22722. bytes = sizeof_ca_ecc_key_der_384;
  22723. #else
  22724. file = XFOPEN(eccCaKey384File, "rb");
  22725. if (!file) {
  22726. ERROR_OUT(-10131, exit);
  22727. }
  22728. bytes = XFREAD(der, 1, FOURK_BUF, file);
  22729. XFCLOSE(file);
  22730. (void)eccCaKeyFile;
  22731. #endif /* USE_CERT_BUFFERS_256 */
  22732. #else
  22733. #ifdef USE_CERT_BUFFERS_256
  22734. XMEMCPY(der, ca_ecc_key_der_256, sizeof_ca_ecc_key_der_256);
  22735. bytes = sizeof_ca_ecc_key_der_256;
  22736. #else
  22737. file = XFOPEN(eccCaKeyFile, "rb");
  22738. if (!file) {
  22739. ERROR_OUT(-10132, exit);
  22740. }
  22741. bytes = XFREAD(der, 1, FOURK_BUF, file);
  22742. XFCLOSE(file);
  22743. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  22744. (void)eccCaKey384File;
  22745. #endif
  22746. #endif /* USE_CERT_BUFFERS_256 */
  22747. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  22748. /* Get CA Key */
  22749. ret = wc_ecc_init_ex(caEccKey, HEAP_HINT, devId);
  22750. if (ret != 0) {
  22751. ERROR_OUT(-10133, exit);
  22752. }
  22753. ret = wc_EccPrivateKeyDecode(der, &idx, caEccKey, (word32)bytes);
  22754. if (ret != 0) {
  22755. ERROR_OUT(-10134, exit);
  22756. }
  22757. /* Make a public key */
  22758. ret = wc_ecc_init_ex(certPubKey, HEAP_HINT, devId);
  22759. if (ret != 0) {
  22760. ERROR_OUT(-10135, exit);
  22761. }
  22762. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, certPubKey);
  22763. #if defined(WOLFSSL_ASYNC_CRYPT)
  22764. ret = wc_AsyncWait(ret, &certPubKey->asyncDev, WC_ASYNC_FLAG_NONE);
  22765. #endif
  22766. if (ret != 0) {
  22767. ERROR_OUT(-10136, exit);
  22768. }
  22769. TEST_SLEEP();
  22770. /* Setup Certificate */
  22771. if (wc_InitCert_ex(myCert, HEAP_HINT, devId)) {
  22772. ERROR_OUT(-10137, exit);
  22773. }
  22774. #ifndef NO_SHA256
  22775. myCert->sigType = CTC_SHA256wECDSA;
  22776. #else
  22777. myCert->sigType = CTC_SHAwECDSA;
  22778. #endif
  22779. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  22780. #ifdef WOLFSSL_CERT_EXT
  22781. /* add Policies */
  22782. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  22783. CTC_MAX_CERTPOL_SZ);
  22784. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  22785. CTC_MAX_CERTPOL_SZ);
  22786. myCert->certPoliciesNb = 2;
  22787. /* add SKID from the Public Key */
  22788. if (wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, certPubKey) != 0) {
  22789. ERROR_OUT(-10138, exit);
  22790. }
  22791. /* add AKID from the Public Key */
  22792. if (wc_SetAuthKeyIdFromPublicKey(myCert, NULL, caEccKey) != 0) {
  22793. ERROR_OUT(-10139, exit);
  22794. }
  22795. /* add Key Usage */
  22796. if (wc_SetKeyUsage(myCert, certKeyUsage) != 0) {
  22797. ERROR_OUT(-10140, exit);
  22798. }
  22799. #endif /* WOLFSSL_CERT_EXT */
  22800. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  22801. #if defined(USE_CERT_BUFFERS_256)
  22802. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_384,
  22803. sizeof_ca_ecc_cert_der_384);
  22804. #else
  22805. ret = wc_SetIssuer(myCert, eccCaCert384File);
  22806. (void)eccCaCertFile;
  22807. #endif
  22808. #else
  22809. #if defined(USE_CERT_BUFFERS_256)
  22810. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_256,
  22811. sizeof_ca_ecc_cert_der_256);
  22812. #else
  22813. ret = wc_SetIssuer(myCert, eccCaCertFile);
  22814. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  22815. (void)eccCaCert384File;
  22816. #endif
  22817. #endif
  22818. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  22819. if (ret < 0) {
  22820. ERROR_OUT(-10141, exit);
  22821. }
  22822. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, certPubKey, rng);
  22823. if (certSz < 0) {
  22824. ERROR_OUT(-10142, exit);
  22825. }
  22826. ret = 0;
  22827. do {
  22828. #if defined(WOLFSSL_ASYNC_CRYPT)
  22829. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22830. #endif
  22831. if (ret >= 0) {
  22832. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  22833. FOURK_BUF, NULL, caEccKey, rng);
  22834. }
  22835. } while (ret == WC_PENDING_E);
  22836. if (ret < 0) {
  22837. ERROR_OUT(-10143, exit);
  22838. }
  22839. certSz = ret;
  22840. TEST_SLEEP();
  22841. #ifdef WOLFSSL_TEST_CERT
  22842. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  22843. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  22844. if (ret != 0) {
  22845. FreeDecodedCert(decode);
  22846. ERROR_OUT(-10144, exit);
  22847. }
  22848. FreeDecodedCert(decode);
  22849. #endif
  22850. ret = SaveDerAndPem(der, certSz, certEccDerFile, certEccPemFile,
  22851. CERT_TYPE, -6735);
  22852. if (ret != 0) {
  22853. goto exit;
  22854. }
  22855. exit:
  22856. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22857. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22858. #endif
  22859. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22860. if (myCert != NULL)
  22861. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22862. #ifdef WOLFSSL_TEST_CERT
  22863. if (decode != NULL)
  22864. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22865. #endif
  22866. if (caEccKey != NULL) {
  22867. wc_ecc_free(caEccKey);
  22868. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22869. }
  22870. if (certPubKey != NULL) {
  22871. wc_ecc_free(certPubKey);
  22872. XFREE(certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22873. }
  22874. #else
  22875. wc_ecc_free(certPubKey);
  22876. wc_ecc_free(caEccKey);
  22877. #endif
  22878. return ret;
  22879. }
  22880. #endif /* WOLFSSL_CERT_GEN */
  22881. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  22882. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  22883. /* Test for the wc_ecc_key_new() and wc_ecc_key_free() functions. */
  22884. static int ecc_test_allocator(WC_RNG* rng)
  22885. {
  22886. int ret = 0;
  22887. ecc_key* key;
  22888. #ifdef WC_NO_RNG
  22889. word32 idx = 0;
  22890. #endif
  22891. key = wc_ecc_key_new(HEAP_HINT);
  22892. if (key == NULL) {
  22893. ERROR_OUT(-10150, exit);
  22894. }
  22895. #ifndef WC_NO_RNG
  22896. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  22897. #if defined(WOLFSSL_ASYNC_CRYPT)
  22898. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  22899. #endif
  22900. if (ret != 0) {
  22901. ERROR_OUT(-10151, exit);
  22902. }
  22903. #else
  22904. /* use test ECC key */
  22905. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, key,
  22906. (word32)sizeof_ecc_key_der_256);
  22907. (void)rng;
  22908. #endif
  22909. exit:
  22910. wc_ecc_key_free(key);
  22911. return ret;
  22912. }
  22913. #endif
  22914. /* ECC Non-blocking tests for Sign and Verify */
  22915. /* Requires SP math and supports P384 or P256 */
  22916. /* ./configure --enable-ecc=nonblock --enable-sp=yes,nonblock CFLAGS="-DWOLFSSL_PUBLIC_MP" */
  22917. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_HAVE_SP_ECC) && \
  22918. defined(WOLFSSL_PUBLIC_MP)
  22919. /* ECC Private Key "d" */
  22920. static const byte p256PrivKey[] = {
  22921. /* SECP256R1 */
  22922. /* d */
  22923. 0x1e, 0xe7, 0x70, 0x07, 0xd3, 0x30, 0x94, 0x39,
  22924. 0x28, 0x90, 0xdf, 0x23, 0x88, 0x2c, 0x4a, 0x34,
  22925. 0x15, 0xdb, 0x4c, 0x43, 0xcd, 0xfa, 0xe5, 0x1f,
  22926. 0x3d, 0x4c, 0x37, 0xfe, 0x59, 0x3b, 0x96, 0xd8
  22927. };
  22928. #ifdef HAVE_ECC384
  22929. static const byte p384PrivKey[] = {
  22930. /* SECP384R1 */
  22931. /* d */
  22932. 0xa4, 0xe5, 0x06, 0xe8, 0x06, 0x16, 0x3e, 0xab,
  22933. 0x89, 0xf8, 0x60, 0x43, 0xc0, 0x60, 0x25, 0xdb,
  22934. 0xba, 0x7b, 0xfe, 0x19, 0x35, 0x08, 0x55, 0x65,
  22935. 0x76, 0xe2, 0xdc, 0xe0, 0x01, 0x8b, 0x6b, 0x68,
  22936. 0xdf, 0xcf, 0x6f, 0x80, 0x12, 0xce, 0x79, 0x37,
  22937. 0xeb, 0x2b, 0x9c, 0x7b, 0xc4, 0x68, 0x1c, 0x74
  22938. };
  22939. #endif /* HAVE_ECC384 */
  22940. #ifdef HAVE_ECC521
  22941. static const byte p521PrivKey[] = {
  22942. /* SECP521R1 */
  22943. /* d */
  22944. 0x01, 0x68, 0x91, 0x33, 0x53, 0xe2, 0x90, 0x68,
  22945. 0x11, 0x8f, 0xaa, 0xa8, 0x76, 0x0c, 0xf7, 0x2a,
  22946. 0x07, 0x1b, 0x92, 0x2a, 0xa7, 0x82, 0x3d, 0xfa,
  22947. 0x83, 0xce, 0x70, 0xc8, 0xc2, 0x60, 0x82, 0xfe,
  22948. 0x18, 0x88, 0x68, 0xda, 0x6a, 0x83, 0x46, 0x78,
  22949. 0xe4, 0xe9, 0xe9, 0xcc, 0x51, 0x7f, 0xed, 0x81,
  22950. 0x02, 0x32, 0xee, 0x26, 0x87, 0xcc, 0xed, 0x63,
  22951. 0x3f, 0x39, 0x27, 0xf0, 0xd7, 0x17, 0x77, 0xa1,
  22952. 0xa4, 0x36
  22953. };
  22954. #endif /* HAVE_ECC521 */
  22955. /* ECC public key Qx/Qy */
  22956. static const byte p256PubKey[] = {
  22957. /* SECP256R1 */
  22958. /* Qx */
  22959. 0x96, 0x93, 0x1c, 0x53, 0x0b, 0x43, 0x6c, 0x42,
  22960. 0x0c, 0x52, 0x90, 0xe4, 0xa7, 0xec, 0x98, 0xb1,
  22961. 0xaf, 0xd4, 0x14, 0x49, 0xd8, 0xc1, 0x42, 0x82,
  22962. 0x04, 0x78, 0xd1, 0x90, 0xae, 0xa0, 0x6c, 0x07,
  22963. /* Qy */
  22964. 0xf2, 0x3a, 0xb5, 0x10, 0x32, 0x8d, 0xce, 0x9e,
  22965. 0x76, 0xa0, 0xd2, 0x8c, 0xf3, 0xfc, 0xa9, 0x94,
  22966. 0x43, 0x24, 0xe6, 0x82, 0x00, 0x40, 0xc6, 0xdb,
  22967. 0x1c, 0x2f, 0xcd, 0x38, 0x4b, 0x60, 0xdd, 0x61
  22968. };
  22969. #ifdef HAVE_ECC384
  22970. static const byte p384PubKey[] = {
  22971. /* SECP384R1 */
  22972. /* Qx */
  22973. 0xea, 0xcf, 0x93, 0x4f, 0x2c, 0x09, 0xbb, 0x39,
  22974. 0x14, 0x0f, 0x56, 0x64, 0xc3, 0x40, 0xb4, 0xdf,
  22975. 0x0e, 0x63, 0xae, 0xe5, 0x71, 0x4b, 0x00, 0xcc,
  22976. 0x04, 0x97, 0xff, 0xe1, 0xe9, 0x38, 0x96, 0xbb,
  22977. 0x5f, 0x91, 0xb2, 0x6a, 0xcc, 0xb5, 0x39, 0x5f,
  22978. 0x8f, 0x70, 0x59, 0xf1, 0x01, 0xf6, 0x5a, 0x2b,
  22979. /* Qy */
  22980. 0x01, 0x6c, 0x68, 0x0b, 0xcf, 0x55, 0x25, 0xaf,
  22981. 0x6d, 0x98, 0x48, 0x0a, 0xa8, 0x74, 0xc9, 0xa9,
  22982. 0x17, 0xa0, 0x0c, 0xc3, 0xfb, 0xd3, 0x23, 0x68,
  22983. 0xfe, 0x04, 0x3c, 0x63, 0x50, 0x88, 0x3b, 0xb9,
  22984. 0x4f, 0x7c, 0x67, 0x34, 0xf7, 0x3b, 0xa9, 0x73,
  22985. 0xe7, 0x1b, 0xc3, 0x51, 0x5e, 0x22, 0x18, 0xec
  22986. };
  22987. #endif
  22988. #ifdef HAVE_ECC521
  22989. static const byte p521PubKey[] = {
  22990. /* SECP521R1 */
  22991. /* Qx */
  22992. 0x01, 0x62, 0x6e, 0xf1, 0x00, 0xec, 0xd8, 0x99,
  22993. 0x58, 0x9b, 0x80, 0x6b, 0xfe, 0x2c, 0xf1, 0xb2,
  22994. 0xf0, 0xc8, 0x48, 0xdf, 0xac, 0xd2, 0x3b, 0x71,
  22995. 0x29, 0xab, 0xf0, 0x66, 0x63, 0xd8, 0x8e, 0xb5,
  22996. 0xc8, 0xc2, 0xfc, 0x99, 0x44, 0xe2, 0x45, 0xb1,
  22997. 0x5a, 0x7b, 0xb9, 0x73, 0x01, 0xda, 0x79, 0xec,
  22998. 0x9c, 0x26, 0x27, 0x34, 0x45, 0x26, 0xd5, 0x89,
  22999. 0x4b, 0x44, 0xfe, 0x69, 0x4e, 0x72, 0x14, 0xe3,
  23000. 0x8b, 0xbc,
  23001. /* Qy */
  23002. 0x00, 0x0f, 0x09, 0xa2, 0x03, 0xc3, 0x5a, 0xdc,
  23003. 0x95, 0x82, 0xf6, 0xf9, 0xf6, 0x9c, 0xff, 0xb5,
  23004. 0x6b, 0x75, 0x95, 0x4b, 0xa4, 0x28, 0x5d, 0x9e,
  23005. 0x90, 0x04, 0xd1, 0xc0, 0x1e, 0xd5, 0xfd, 0x43,
  23006. 0x9e, 0x1e, 0x83, 0xc0, 0x11, 0x2b, 0x2b, 0x07,
  23007. 0x6d, 0xa9, 0x7a, 0x10, 0xd7, 0x67, 0xe7, 0x51,
  23008. 0x37, 0x24, 0xd8, 0xbf, 0x03, 0x0d, 0x8b, 0xb5,
  23009. 0x40, 0x5c, 0x4f, 0xd6, 0x13, 0x73, 0x42, 0xbc,
  23010. 0x91, 0xd9
  23011. };
  23012. /* perform verify of signature and hash using public key */
  23013. /* key is public Qx + public Qy */
  23014. /* sig is r + s */
  23015. static int crypto_ecc_verify(const byte *key, uint32_t keySz,
  23016. const byte *hash, uint32_t hashSz, const byte *sig, uint32_t sigSz,
  23017. uint32_t curveSz, int curveId)
  23018. {
  23019. int ret, verify_res = 0, count = 0;
  23020. mp_int r, s;
  23021. ecc_key ecc;
  23022. ecc_nb_ctx_t nb_ctx;
  23023. /* validate arguments */
  23024. if (key == NULL || hash == NULL || sig == NULL || curveSz == 0 ||
  23025. hashSz == 0 || keySz < (curveSz*2) || sigSz < (curveSz*2))
  23026. {
  23027. return BAD_FUNC_ARG;
  23028. }
  23029. /* Setup the ECC key */
  23030. ret = wc_ecc_init(&ecc);
  23031. if (ret < 0) {
  23032. return ret;
  23033. }
  23034. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  23035. if (ret != MP_OKAY) {
  23036. wc_ecc_free(&ecc);
  23037. return ret;
  23038. }
  23039. /* Setup the signature r/s variables */
  23040. ret = mp_init(&r);
  23041. if (ret != MP_OKAY) {
  23042. wc_ecc_free(&ecc);
  23043. return ret;
  23044. }
  23045. ret = mp_init(&s);
  23046. if (ret != MP_OKAY) {
  23047. mp_clear(&r);
  23048. wc_ecc_free(&ecc);
  23049. return ret;
  23050. }
  23051. /* Import public key x/y */
  23052. ret = wc_ecc_import_unsigned(
  23053. &ecc,
  23054. (byte*)key, /* Public "x" Coordinate */
  23055. (byte*)(key + curveSz), /* Public "y" Coordinate */
  23056. NULL, /* Private "d" (optional) */
  23057. curveId /* ECC Curve Id */
  23058. );
  23059. /* Make sure it was a public key imported */
  23060. if (ret == 0 && ecc.type != ECC_PUBLICKEY) {
  23061. ret = ECC_BAD_ARG_E;
  23062. }
  23063. /* Import signature r/s */
  23064. if (ret == 0) {
  23065. ret = mp_read_unsigned_bin(&r, sig, curveSz);
  23066. }
  23067. if (ret == 0) {
  23068. ret = mp_read_unsigned_bin(&s, sig + curveSz, curveSz);
  23069. }
  23070. /* Verify ECC Signature */
  23071. if (ret == 0) {
  23072. do {
  23073. ret = wc_ecc_verify_hash_ex(
  23074. &r, &s, /* r/s as mp_int */
  23075. hash, hashSz, /* computed hash digest */
  23076. &verify_res, /* verification result 1=success */
  23077. &ecc
  23078. );
  23079. count++;
  23080. /* This is where real-time work could be called */
  23081. } while (ret == FP_WOULDBLOCK);
  23082. #ifdef DEBUG_WOLFSSL
  23083. printf("ECC non-block verify: %d times\n", count);
  23084. #endif
  23085. }
  23086. /* check verify result */
  23087. if (ret == 0 && verify_res == 0) {
  23088. ret = SIG_VERIFY_E;
  23089. }
  23090. mp_clear(&r);
  23091. mp_clear(&s);
  23092. wc_ecc_free(&ecc);
  23093. (void)count;
  23094. return ret;
  23095. }
  23096. /* perform signature operation against hash using private key */
  23097. static int crypto_ecc_sign(const byte *key, uint32_t keySz,
  23098. const byte *hash, uint32_t hashSz, byte *sig, uint32_t* sigSz,
  23099. uint32_t curveSz, int curveId, WC_RNG* rng)
  23100. {
  23101. int ret, count = 0;
  23102. mp_int r, s;
  23103. ecc_key ecc;
  23104. ecc_nb_ctx_t nb_ctx;
  23105. /* validate arguments */
  23106. if (key == NULL || hash == NULL || sig == NULL || sigSz == NULL ||
  23107. curveSz == 0 || hashSz == 0 || keySz < curveSz || *sigSz < (curveSz*2))
  23108. {
  23109. return BAD_FUNC_ARG;
  23110. }
  23111. /* Initialize signature result */
  23112. memset(sig, 0, curveSz*2);
  23113. /* Setup the ECC key */
  23114. ret = wc_ecc_init(&ecc);
  23115. if (ret < 0) {
  23116. return ret;
  23117. }
  23118. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  23119. if (ret != MP_OKAY) {
  23120. wc_ecc_free(&ecc);
  23121. return ret;
  23122. }
  23123. /* Setup the signature r/s variables */
  23124. ret = mp_init(&r);
  23125. if (ret != MP_OKAY) {
  23126. wc_ecc_free(&ecc);
  23127. return ret;
  23128. }
  23129. ret = mp_init(&s);
  23130. if (ret != MP_OKAY) {
  23131. mp_clear(&r);
  23132. wc_ecc_free(&ecc);
  23133. return ret;
  23134. }
  23135. /* Import private key "k" */
  23136. ret = wc_ecc_import_private_key_ex(
  23137. key, keySz, /* private key "d" */
  23138. NULL, 0, /* public (optional) */
  23139. &ecc,
  23140. curveId /* ECC Curve Id */
  23141. );
  23142. if (ret == 0) {
  23143. do {
  23144. /* Verify ECC Signature */
  23145. ret = wc_ecc_sign_hash_ex(
  23146. hash, hashSz, /* computed hash digest */
  23147. rng, &ecc, /* random and key context */
  23148. &r, &s /* r/s as mp_int */
  23149. );
  23150. count++;
  23151. /* This is where real-time work could be called */
  23152. } while (ret == FP_WOULDBLOCK);
  23153. #ifdef DEBUG_WOLFSSL
  23154. printf("ECC non-block sign: %d times\n", count);
  23155. #endif
  23156. }
  23157. if (ret == 0) {
  23158. /* export r/s */
  23159. mp_to_unsigned_bin_len(&r, sig, curveSz);
  23160. mp_to_unsigned_bin_len(&s, sig + curveSz, curveSz);
  23161. }
  23162. mp_clear(&r);
  23163. mp_clear(&s);
  23164. wc_ecc_free(&ecc);
  23165. (void)count;
  23166. return ret;
  23167. }
  23168. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  23169. /*
  23170. * This test doesn't work with WOLFSSL_VALIDATE_ECC_KEYGEN defined because we
  23171. * don't have non-blocking versions of the key checking functions, yet.
  23172. */
  23173. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_VALIDATE_ECC_KEYGEN)
  23174. static int ecc_test_nonblock_dhe(int curveId, word32 curveSz,
  23175. const byte* privKey, const byte* pubKey, WC_RNG* rng)
  23176. {
  23177. int ret;
  23178. ecc_key keyA;
  23179. ecc_key keyB;
  23180. ecc_nb_ctx_t nbCtxA;
  23181. ecc_nb_ctx_t nbCtxB;
  23182. byte secretA[ECC_SHARED_SIZE];
  23183. byte secretB[ECC_SHARED_SIZE];
  23184. word32 secretSzA = ECC_SHARED_SIZE;
  23185. word32 secretSzB = ECC_SHARED_SIZE;
  23186. int count = 0;
  23187. ret = wc_ecc_init(&keyA);
  23188. if (ret == 0) {
  23189. ret = wc_ecc_init(&keyB);
  23190. }
  23191. if (ret == 0) {
  23192. ret = wc_ecc_set_nonblock(&keyA, &nbCtxA);
  23193. }
  23194. if (ret == 0) {
  23195. ret = wc_ecc_set_nonblock(&keyB, &nbCtxB);
  23196. }
  23197. if (ret == 0) {
  23198. do {
  23199. ret = wc_ecc_make_key_ex(rng, curveSz, &keyA, curveId);
  23200. count++;
  23201. } while (ret == FP_WOULDBLOCK);
  23202. }
  23203. #ifdef DEBUG_WOLFSSL
  23204. fprintf(stderr, "ECC non-block key gen: %d times\n", count);
  23205. #endif
  23206. if (ret == 0) {
  23207. ret = wc_ecc_check_key(&keyA);
  23208. }
  23209. if (ret == 0) {
  23210. ret = wc_ecc_import_unsigned(&keyB, pubKey, pubKey + curveSz,
  23211. privKey, curveId);
  23212. }
  23213. count = 0;
  23214. if (ret == 0) {
  23215. do {
  23216. ret = wc_ecc_shared_secret(&keyA, &keyB, secretA, &secretSzA);
  23217. count++;
  23218. } while (ret == FP_WOULDBLOCK);
  23219. }
  23220. #ifdef DEBUG_WOLFSSL
  23221. fprintf(stderr, "ECC non-block shared secret: %d times\n", count);
  23222. #endif
  23223. if (ret == 0) {
  23224. do {
  23225. ret = wc_ecc_shared_secret(&keyB, &keyA, secretB, &secretSzB);
  23226. } while (ret == FP_WOULDBLOCK);
  23227. }
  23228. if (ret == 0) {
  23229. if (secretSzA != secretSzB ||
  23230. XMEMCMP(secretA, secretB, secretSzA) != 0) {
  23231. ret = -1;
  23232. }
  23233. }
  23234. wc_ecc_free(&keyA);
  23235. wc_ecc_free(&keyB);
  23236. return ret;
  23237. }
  23238. #endif /* HAVE_ECC_DHE && !WOLFSSL_VALIDATE_ECC_KEYGEN */
  23239. #if defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  23240. static int ecc_test_nonblock_ecdsa(int curveId, word32 curveSz,
  23241. const byte* privKey, word32 privKeySz, const byte* pubKey, word32 pubKeySz,
  23242. WC_RNG* rng)
  23243. {
  23244. int ret = 0;
  23245. byte* sig = NULL;
  23246. word32 sigSz = curveSz * 2;
  23247. static const byte hash[] = {
  23248. 0x8d, 0x28, 0xa3, 0x8b, 0x0b, 0xa9, 0xfe, 0xd4, 0x0e, 0x54, 0xc4, 0x17,
  23249. 0x3d, 0x54, 0x66, 0x34, 0xbf, 0x5d, 0x6f, 0x46, 0xc2, 0x20, 0xcb, 0xc3,
  23250. 0x22, 0xe9, 0xb0, 0xdf, 0xe7, 0x64, 0x3f, 0xd9
  23251. };
  23252. sig = (byte*)XMALLOC(sigSz, HEAP_HINT, DYNAMIC_TYPE_SIGNATURE);
  23253. if (sig == NULL) {
  23254. ret = -1;
  23255. }
  23256. if (ret == 0) {
  23257. /* Sign hash using private key */
  23258. /* Note: result of an ECC sign varies for each call even with same
  23259. private key and hash. This is because a new random public key is
  23260. used for each operation. */
  23261. ret = crypto_ecc_sign(privKey, privKeySz, hash, sizeof(hash), sig,
  23262. &sigSz, curveSz, curveId, rng);
  23263. }
  23264. if (ret == 0) {
  23265. /* Verify generated signature is valid */
  23266. ret = crypto_ecc_verify(pubKey, pubKeySz, hash, sizeof(hash), sig,
  23267. sigSz, curveSz, curveId);
  23268. }
  23269. if (sig != NULL) {
  23270. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_SIGNATURE);
  23271. }
  23272. return ret;
  23273. }
  23274. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  23275. static int ecc_test_nonblock(WC_RNG* rng)
  23276. {
  23277. int ret = 0;
  23278. word32 i;
  23279. int curveIds[3] = {0, 0, 0};
  23280. word32 curveSzs[3] = {0, 0, 0};
  23281. const byte* privKeys[3] = {NULL, NULL, NULL};
  23282. word32 privKeySzs[3] = {0, 0, 0};
  23283. const byte* pubKeys[3] = {NULL, NULL, NULL};
  23284. word32 pubKeySzs[3] = {0, 0, 0};
  23285. curveIds[0] = ECC_SECP256R1;
  23286. curveSzs[0] = 32;
  23287. privKeys[0] = p256PrivKey;
  23288. privKeySzs[0] = sizeof(p256PrivKey);
  23289. pubKeys[0] = p256PubKey;
  23290. pubKeySzs[0] = sizeof(p256PubKey);
  23291. #ifdef HAVE_ECC384
  23292. curveIds[1] = ECC_SECP384R1;
  23293. curveSzs[1] = 48;
  23294. privKeys[1] = p384PrivKey;
  23295. privKeySzs[1] = sizeof(p384PrivKey);
  23296. pubKeys[1] = p384PubKey;
  23297. pubKeySzs[1] = sizeof(p384PubKey);
  23298. #endif
  23299. #ifdef HAVE_ECC521
  23300. curveIds[2] = ECC_SECP521R1;
  23301. curveSzs[2] = 66;
  23302. privKeys[2] = p521PrivKey;
  23303. privKeySzs[2] = sizeof(p521PrivKey);
  23304. pubKeys[2] = p521PubKey;
  23305. pubKeySzs[2] = sizeof(p521PubKey);
  23306. #endif
  23307. for (i = 0; ret == 0 && i < sizeof(curveIds) / sizeof(curveIds[0]); ++i) {
  23308. if (curveIds[i] == 0) {
  23309. continue;
  23310. }
  23311. #if defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  23312. ret = ecc_test_nonblock_ecdsa(curveIds[i], curveSzs[i], privKeys[i],
  23313. privKeySzs[i], pubKeys[i], pubKeySzs[i], rng);
  23314. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  23315. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_VALIDATE_ECC_KEYGEN)
  23316. if (ret == 0) {
  23317. ret = ecc_test_nonblock_dhe(curveIds[i], curveSzs[i], privKeys[i],
  23318. pubKeys[i], rng);
  23319. }
  23320. #endif /* HAVE_ECC_DHE && !WOLFSSL_VALIDATE_ECC_KEYGEN */
  23321. }
  23322. return ret;
  23323. }
  23324. #endif /* WC_ECC_NONBLOCK && WOLFSSL_HAVE_SP_ECC && WOLFSSL_PUBLIC_MP */
  23325. WOLFSSL_TEST_SUBROUTINE int ecc_test(void)
  23326. {
  23327. int ret;
  23328. WC_RNG rng;
  23329. #if defined(WOLFSSL_CERT_EXT) && \
  23330. (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  23331. ret = ecc_decode_test();
  23332. if (ret < 0)
  23333. return ret;
  23334. #endif
  23335. #ifndef HAVE_FIPS
  23336. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  23337. #else
  23338. ret = wc_InitRng(&rng);
  23339. #endif
  23340. #ifndef WC_NO_RNG
  23341. if (ret != 0)
  23342. return -10300;
  23343. #else
  23344. (void)ret;
  23345. #endif
  23346. #if (defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 112
  23347. ret = ecc_test_curve(&rng, 14);
  23348. if (ret < 0) {
  23349. goto done;
  23350. }
  23351. #endif /* HAVE_ECC112 */
  23352. #if (defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 128
  23353. ret = ecc_test_curve(&rng, 16);
  23354. if (ret < 0) {
  23355. goto done;
  23356. }
  23357. #endif /* HAVE_ECC128 */
  23358. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  23359. ret = ecc_test_curve(&rng, 20);
  23360. if (ret < 0) {
  23361. goto done;
  23362. }
  23363. #endif /* HAVE_ECC160 */
  23364. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  23365. ret = ecc_test_curve(&rng, 24);
  23366. if (ret < 0) {
  23367. goto done;
  23368. }
  23369. #endif /* HAVE_ECC192 */
  23370. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  23371. ret = ecc_test_curve(&rng, 28);
  23372. if (ret < 0) {
  23373. goto done;
  23374. }
  23375. #endif /* HAVE_ECC224 */
  23376. #if (defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 239
  23377. ret = ecc_test_curve(&rng, 30);
  23378. if (ret < 0) {
  23379. goto done;
  23380. }
  23381. #endif /* HAVE_ECC239 */
  23382. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  23383. ret = ecc_test_curve(&rng, 32);
  23384. if (ret < 0) {
  23385. goto done;
  23386. }
  23387. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  23388. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  23389. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23390. ret = ecc_point_test();
  23391. if (ret < 0) {
  23392. goto done;
  23393. }
  23394. #endif
  23395. ret = ecc_def_curve_test(&rng);
  23396. if (ret < 0) {
  23397. goto done;
  23398. }
  23399. #endif /* !NO_ECC256 */
  23400. #if (defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 320
  23401. ret = ecc_test_curve(&rng, 40);
  23402. if (ret < 0) {
  23403. goto done;
  23404. }
  23405. #endif /* HAVE_ECC320 */
  23406. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  23407. ret = ecc_test_curve(&rng, 48);
  23408. if (ret < 0) {
  23409. goto done;
  23410. }
  23411. #endif /* HAVE_ECC384 */
  23412. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  23413. ret = ecc_test_curve(&rng, 64);
  23414. if (ret < 0) {
  23415. goto done;
  23416. }
  23417. #endif /* HAVE_ECC512 */
  23418. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  23419. ret = ecc_test_curve(&rng, 66);
  23420. if (ret < 0) {
  23421. goto done;
  23422. }
  23423. #endif /* HAVE_ECC521 */
  23424. #if defined(WOLFSSL_CUSTOM_CURVES)
  23425. ret = ecc_test_custom_curves(&rng);
  23426. if (ret != 0) {
  23427. goto done;
  23428. }
  23429. #endif
  23430. #if defined(HAVE_ECC_SIGN) && (defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  23431. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)) \
  23432. && (!defined(FIPS_VERSION_GE) || FIPS_VERSION_GE(5,3))
  23433. #ifdef HAVE_ECC256
  23434. ret = ecc_test_deterministic_k(&rng);
  23435. if (ret != 0) {
  23436. printf("ecc_test_deterministic_k failed! %d\n", ret);
  23437. goto done;
  23438. }
  23439. #endif
  23440. #ifdef WOLFSSL_PUBLIC_MP
  23441. #if defined(HAVE_ECC384)
  23442. ret = ecc384_test_deterministic_k(&rng);
  23443. if (ret != 0) {
  23444. printf("ecc384_test_deterministic_k failed! %d\n", ret);
  23445. goto done;
  23446. }
  23447. #endif
  23448. #if defined(HAVE_ECC521)
  23449. ret = ecc521_test_deterministic_k(&rng);
  23450. if (ret != 0) {
  23451. printf("ecc512_test_deterministic_k failed! %d\n", ret);
  23452. goto done;
  23453. }
  23454. #endif
  23455. #endif
  23456. #endif
  23457. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K) && \
  23458. !defined(WOLFSSL_KCAPI_ECC)
  23459. ret = ecc_test_sign_vectors(&rng);
  23460. if (ret != 0) {
  23461. printf("ecc_test_sign_vectors failed! %d\n", ret);
  23462. goto done;
  23463. }
  23464. #endif
  23465. #if defined(HAVE_ECC_CDH) && defined(HAVE_ECC_DHE)
  23466. ret = ecc_test_cdh_vectors(&rng);
  23467. if (ret != 0) {
  23468. printf("ecc_test_cdh_vectors failed! %d\n", ret);
  23469. goto done;
  23470. }
  23471. #endif
  23472. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  23473. !defined(WOLFSSL_STM32_PKA) && !defined(WOLFSSL_SILABS_SE_ACCEL) && \
  23474. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23475. ret = ecc_test_make_pub(&rng);
  23476. if (ret != 0) {
  23477. printf("ecc_test_make_pub failed!: %d\n", ret);
  23478. goto done;
  23479. }
  23480. #elif defined(HAVE_ECC_KEY_IMPORT)
  23481. (void)ecc_test_make_pub; /* for compiler warning */
  23482. #endif
  23483. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_ASN_TIME)
  23484. ret = ecc_test_cert_gen(&rng);
  23485. if (ret != 0) {
  23486. printf("ecc_test_cert_gen failed!: %d\n", ret);
  23487. goto done;
  23488. }
  23489. #endif
  23490. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC) && \
  23491. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23492. ret = ecc_test_allocator(&rng);
  23493. if (ret != 0) {
  23494. printf("ecc_test_allocator failed!: %d\n", ret);
  23495. goto done;
  23496. }
  23497. #endif
  23498. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_PUBLIC_MP) && \
  23499. defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  23500. ret = ecc_test_nonblock(&rng);
  23501. if (ret != 0) {
  23502. printf("ecc_test_nonblock failed!: %d\n", ret);
  23503. goto done;
  23504. }
  23505. #endif
  23506. done:
  23507. wc_FreeRng(&rng);
  23508. return ret;
  23509. }
  23510. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  23511. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  23512. /* ecc_encrypt_e2e_test() uses wc_ecc_ctx_set_algo(), which was added in
  23513. * wolfFIPS 5.3.
  23514. * ecc_encrypt_kat() is used only by ecc_encrypt_e2e_test().
  23515. */
  23516. #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))
  23517. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  23518. ECC_MIN_KEY_SZ <= 256 && defined(WOLFSSL_AES_128)
  23519. static int ecc_encrypt_kat(WC_RNG *rng)
  23520. {
  23521. int ret = 0;
  23522. #ifdef WOLFSSL_ECIES_OLD
  23523. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23524. ecc_key* userA = NULL;
  23525. #else
  23526. ecc_key userA[1];
  23527. #endif
  23528. int userAInit = 0;
  23529. #endif
  23530. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23531. ecc_key* userB = NULL;
  23532. #else
  23533. ecc_key userB[1];
  23534. #endif
  23535. int userBInit = 0;
  23536. ecc_key* tmpKey;
  23537. byte plain[48];
  23538. word32 plainSz = sizeof(plain);
  23539. WOLFSSL_SMALL_STACK_STATIC const byte privKey[] = {
  23540. 0x04, 0x80, 0xef, 0x1d, 0xbe, 0x02, 0x0c, 0x20,
  23541. 0x5b, 0xab, 0x80, 0x35, 0x5b, 0x2a, 0x0f, 0x6d,
  23542. 0xd3, 0xb0, 0x7f, 0x7e, 0x7f, 0x86, 0x8a, 0x49,
  23543. 0xee, 0xb4, 0xaa, 0x09, 0x2d, 0x1e, 0x1d, 0x02
  23544. };
  23545. #if defined(WOLFSSL_ECIES_OLD) || defined(WOLFSSL_QNX_CAAM)
  23546. WOLFSSL_SMALL_STACK_STATIC const byte pubKey[] = {
  23547. 0x04,
  23548. /* X */
  23549. 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0, 0x5a,
  23550. 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c, 0x3a,
  23551. 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3, 0xc1,
  23552. 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63, 0xa0,
  23553. /* X */
  23554. 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7, 0xcd,
  23555. 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75, 0xaa,
  23556. 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe, 0xe8,
  23557. 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3, 0x28
  23558. };
  23559. #endif
  23560. WOLFSSL_SMALL_STACK_STATIC const byte enc_msg[] = {
  23561. #ifdef WOLFSSL_ECIES_OLD
  23562. 0x42, 0x70, 0xbf, 0xf9, 0xf4, 0x7e, 0x4b, 0x9b,
  23563. 0xb5, 0x4c, 0xcc, 0xc5, 0x94, 0xa7, 0xef, 0xaa,
  23564. 0xc3, 0x7c, 0x85, 0xa6, 0x51, 0x6e, 0xd3, 0xfa,
  23565. 0x56, 0xc9, 0x10, 0x4d, 0x14, 0x32, 0x61, 0xb8,
  23566. 0xbb, 0x66, 0x7a, 0xb5, 0xbc, 0x95, 0xf8, 0xca,
  23567. 0xd1, 0x2a, 0x19, 0x51, 0x44, 0xd8, 0x0e, 0x57,
  23568. 0x34, 0xed, 0x45, 0x89, 0x2e, 0x57, 0xbe, 0xd5,
  23569. 0x06, 0x22, 0xd7, 0x13, 0x0a, 0x0e, 0x40, 0x36,
  23570. 0x0d, 0x05, 0x0d, 0xb6, 0xae, 0x61, 0x37, 0x18,
  23571. 0x83, 0x90, 0x0a, 0x27, 0x95, 0x41, 0x8c, 0x45
  23572. #elif defined(WOLFSSL_ECIES_ISO18033)
  23573. 0x04, 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0,
  23574. 0x5a, 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c,
  23575. 0x3a, 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3,
  23576. 0xc1, 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63,
  23577. 0xa0, 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7,
  23578. 0xcd, 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75,
  23579. 0xaa, 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe,
  23580. 0xe8, 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3,
  23581. 0x28, 0xbb, 0x9f, 0xa8, 0x2d, 0xe1, 0xf1, 0x67,
  23582. 0x45, 0x02, 0x19, 0xdc, 0xc8, 0x24, 0x8b, 0x20,
  23583. 0x02, 0xa0, 0x8f, 0x95, 0x12, 0x55, 0x51, 0xf8,
  23584. 0x03, 0xc4, 0x54, 0x13, 0x98, 0x2d, 0xf0, 0x31,
  23585. 0x51, 0x80, 0x45, 0x24, 0xcb, 0x8b, 0x48, 0xa6,
  23586. 0x8b, 0x8e, 0x97, 0x9c, 0x56, 0x4d, 0x70, 0x00,
  23587. 0x53, 0xd3, 0x47, 0x00, 0x5a, 0x23, 0x8c, 0xf9,
  23588. 0xfd, 0xd2, 0x33, 0x2c, 0x43, 0x6e, 0x9e, 0xb2,
  23589. 0xf4, 0x95, 0xd4, 0xcf, 0x30, 0xd6, 0xa2, 0xc5,
  23590. 0x35, 0x96, 0x6a, 0xd4, 0x36, 0x15, 0xa9, 0xbd,
  23591. 0x7f
  23592. #elif defined(WOLFSSL_ECIES_GEN_IV)
  23593. /* EC P-256 point */
  23594. 0x04,
  23595. /* X */
  23596. 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0, 0x5a,
  23597. 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c, 0x3a,
  23598. 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3, 0xc1,
  23599. 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63, 0xa0,
  23600. /* Y */
  23601. 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7, 0xcd,
  23602. 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75, 0xaa,
  23603. 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe, 0xe8,
  23604. 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3, 0x28,
  23605. /* IV */
  23606. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  23607. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  23608. /* Encrypted Msg */
  23609. 0xe5, 0x17, 0xaf, 0x0d, 0x65, 0x4d, 0x3d, 0x50,
  23610. 0x96, 0x05, 0xc9, 0x63, 0x2c, 0xef, 0x1c, 0x1f,
  23611. 0x78, 0xc9, 0x90, 0x7a, 0x14, 0x00, 0xfc, 0x44,
  23612. 0x71, 0x6d, 0x57, 0x8c, 0xdf, 0x23, 0xca, 0x65,
  23613. 0xcf, 0x93, 0x06, 0xb6, 0x9a, 0xf4, 0x61, 0xbd,
  23614. 0x44, 0x1a, 0xeb, 0x52, 0x68, 0x0f, 0xd1, 0xde,
  23615. /* HMAC */
  23616. 0x5a, 0x22, 0xc1, 0x5d, 0x99, 0x66, 0x3f, 0x24,
  23617. 0x35, 0x96, 0xac, 0xf7, 0xf6, 0x28, 0x45, 0x16,
  23618. 0x52, 0x19, 0x0d, 0xe4, 0xb2, 0xca, 0x5b, 0x28,
  23619. 0x4e, 0xbb, 0xf3, 0x98, 0x57, 0xd7, 0x3b, 0xe2
  23620. #else
  23621. 0x04, 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0,
  23622. 0x5a, 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c,
  23623. 0x3a, 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3,
  23624. 0xc1, 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63,
  23625. 0xa0, 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7,
  23626. 0xcd, 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75,
  23627. 0xaa, 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe,
  23628. 0xe8, 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3,
  23629. 0x28, 0xe5, 0x17, 0xaf, 0x0d, 0x65, 0x4d, 0x3d,
  23630. 0x50, 0x96, 0x05, 0xc9, 0x63, 0x2c, 0xef, 0x1c,
  23631. 0x1f, 0x78, 0xc9, 0x90, 0x7a, 0x14, 0x00, 0xfc,
  23632. 0x44, 0x71, 0x6d, 0x57, 0x8c, 0xdf, 0x23, 0xca,
  23633. 0x65, 0xcf, 0x93, 0x06, 0xb6, 0x9a, 0xf4, 0x61,
  23634. 0xbd, 0x44, 0x1a, 0xeb, 0x52, 0x68, 0x0f, 0xd1,
  23635. 0xde, 0xc7, 0x3f, 0x6f, 0xce, 0xbe, 0x49, 0x61,
  23636. 0x48, 0x01, 0x77, 0x41, 0xd0, 0xd8, 0x5b, 0x48,
  23637. 0xca, 0x4e, 0x47, 0x3e, 0x47, 0xbf, 0x1d, 0x28,
  23638. 0x4c, 0x18, 0x1a, 0xfb, 0x96, 0x95, 0xda, 0xde,
  23639. 0x55
  23640. #endif
  23641. };
  23642. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  23643. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  23644. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  23645. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  23646. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  23647. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  23648. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  23649. };
  23650. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23651. userB = (ecc_key *)XMALLOC(sizeof(*userB), HEAP_HINT,
  23652. DYNAMIC_TYPE_TMP_BUFFER);
  23653. if (userB == NULL) {
  23654. ret = -10451;
  23655. }
  23656. #ifdef WOLFSSL_ECIES_OLD
  23657. if (ret == 0) {
  23658. userA = (ecc_key *)XMALLOC(sizeof(*userA), HEAP_HINT,
  23659. DYNAMIC_TYPE_TMP_BUFFER);
  23660. if (userA == NULL) {
  23661. ret = -10450;
  23662. }
  23663. }
  23664. #endif
  23665. #endif
  23666. if (ret == 0) {
  23667. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  23668. if (ret != 0)
  23669. ret = -10453;
  23670. }
  23671. if (ret == 0) {
  23672. userBInit = 1;
  23673. #ifdef WOLFSSL_ECIES_OLD
  23674. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  23675. if (ret != 0)
  23676. ret = -10452;
  23677. }
  23678. if (ret == 0) {
  23679. userAInit = 1;
  23680. tmpKey = userA;
  23681. #else
  23682. tmpKey = NULL;
  23683. #endif
  23684. }
  23685. if (ret == 0) {
  23686. #ifdef WOLFSSL_QNX_CAAM
  23687. ret = wc_ecc_import_private_key_ex(privKey, sizeof(privKey), pubKey,
  23688. sizeof(pubKey), userB, ECC_SECP256R1);
  23689. #else
  23690. ret = wc_ecc_import_private_key_ex(privKey, sizeof(privKey), NULL, 0,
  23691. userB, ECC_SECP256R1);
  23692. #endif
  23693. if (ret != 0)
  23694. ret = -10454;
  23695. }
  23696. #ifdef WOLFSSL_ECIES_OLD
  23697. if (ret == 0) {
  23698. ret = wc_ecc_import_x963_ex(pubKey, sizeof(pubKey), userA,
  23699. ECC_SECP256R1);
  23700. if (ret != 0)
  23701. ret = -10455;
  23702. }
  23703. #endif
  23704. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  23705. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  23706. !defined(HAVE_SELFTEST)
  23707. if (ret == 0) {
  23708. ret = wc_ecc_set_rng(userB, rng);
  23709. if (ret != 0) {
  23710. ret = -10456;
  23711. }
  23712. }
  23713. #else
  23714. (void)rng;
  23715. #endif
  23716. if (ret == 0) {
  23717. ret = wc_ecc_decrypt(userB, tmpKey, enc_msg, sizeof(enc_msg), plain,
  23718. &plainSz, NULL);
  23719. if (ret != 0)
  23720. ret = -10457;
  23721. }
  23722. if (ret == 0) {
  23723. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  23724. ret = -10458;
  23725. }
  23726. }
  23727. if (userBInit)
  23728. wc_ecc_free(userB);
  23729. #ifdef WOLFSSL_ECIES_OLD
  23730. if (userAInit)
  23731. wc_ecc_free(userA);
  23732. #endif
  23733. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23734. if (userB != NULL) {
  23735. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23736. }
  23737. #ifdef WOLFSSL_ECIES_OLD
  23738. if (userA != NULL) {
  23739. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23740. }
  23741. #endif
  23742. #endif
  23743. return ret;
  23744. }
  23745. #endif
  23746. static int ecc_encrypt_e2e_test(WC_RNG* rng, ecc_key* userA, ecc_key* userB,
  23747. byte encAlgo, byte kdfAlgo, byte macAlgo)
  23748. {
  23749. int ret = 0;
  23750. byte msg[48];
  23751. byte plain[48];
  23752. #ifdef WOLFSSL_ECIES_OLD
  23753. byte out[80];
  23754. #elif defined(WOLFSSL_ECIES_GEN_IV)
  23755. byte out[1 + ECC_KEYGEN_SIZE * 2 + 16 + 80];
  23756. #else
  23757. byte out[1 + ECC_KEYGEN_SIZE * 2 + 80];
  23758. #endif
  23759. word32 outSz = sizeof(out);
  23760. word32 plainSz = sizeof(plain);
  23761. int i;
  23762. ecEncCtx* cliCtx = NULL;
  23763. ecEncCtx* srvCtx = NULL;
  23764. byte cliSalt[EXCHANGE_SALT_SZ];
  23765. byte srvSalt[EXCHANGE_SALT_SZ];
  23766. const byte* tmpSalt;
  23767. byte msg2[48];
  23768. byte plain2[48];
  23769. #ifdef WOLFSSL_ECIES_OLD
  23770. byte out2[80];
  23771. #elif defined(WOLFSSL_ECIES_GEN_IV)
  23772. byte out2[1 + ECC_KEYGEN_SIZE * 2 + 16 + 80];
  23773. #else
  23774. byte out2[1 + ECC_KEYGEN_SIZE * 2 + 80];
  23775. #endif
  23776. word32 outSz2 = sizeof(out2);
  23777. word32 plainSz2 = sizeof(plain2);
  23778. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23779. ecc_key *tmpKey = (ecc_key *)XMALLOC(sizeof(ecc_key), HEAP_HINT,
  23780. DYNAMIC_TYPE_TMP_BUFFER);
  23781. #else
  23782. ecc_key tmpKey[1];
  23783. #endif
  23784. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23785. if (tmpKey == NULL) {
  23786. ERROR_OUT(MEMORY_E, done);
  23787. }
  23788. #endif
  23789. ret = wc_ecc_init_ex(tmpKey, HEAP_HINT, devId);
  23790. if (ret != 0)
  23791. goto done;
  23792. /* set message to incrementing 0,1,2,etc... */
  23793. for (i = 0; i < (int)sizeof(msg); i++)
  23794. msg[i] = i;
  23795. /* encrypt msg to B */
  23796. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz, NULL);
  23797. if (ret != 0) {
  23798. ret = -10405; goto done;
  23799. }
  23800. #ifdef WOLFSSL_ECIES_OLD
  23801. tmpKey->dp = userA->dp;
  23802. ret = wc_ecc_copy_point(&userA->pubkey, &tmpKey->pubkey);
  23803. if (ret != 0) {
  23804. ret = -10413; goto done;
  23805. }
  23806. #endif
  23807. /* decrypt msg from A */
  23808. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, NULL);
  23809. if (ret != 0) {
  23810. ret = -10406; goto done;
  23811. }
  23812. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  23813. ret = -10407; goto done;
  23814. }
  23815. #ifndef WOLFSSL_ECIES_OLD
  23816. /* A decrypts msg (response) from B */
  23817. ret = wc_ecc_decrypt(userB, NULL, out, outSz, plain2, &plainSz2, NULL);
  23818. if (ret != 0)
  23819. goto done;
  23820. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  23821. ret = -10415; goto done;
  23822. }
  23823. #endif
  23824. /* let's verify message exchange works, A is client, B is server */
  23825. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  23826. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  23827. if (cliCtx == NULL || srvCtx == NULL) {
  23828. ret = -10408; goto done;
  23829. }
  23830. ret = wc_ecc_ctx_set_algo(cliCtx, encAlgo, kdfAlgo, macAlgo);
  23831. if (ret != 0)
  23832. goto done;
  23833. ret = wc_ecc_ctx_set_algo(srvCtx, encAlgo, kdfAlgo, macAlgo);
  23834. if (ret != 0)
  23835. goto done;
  23836. /* get salt to send to peer */
  23837. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  23838. if (tmpSalt == NULL) {
  23839. ret = -10409; goto done;
  23840. }
  23841. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  23842. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  23843. if (tmpSalt == NULL) {
  23844. ret = -10410; goto done;
  23845. }
  23846. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  23847. /* in actual use, we'd get the peer's salt over the transport */
  23848. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  23849. if (ret != 0)
  23850. goto done;
  23851. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  23852. if (ret != 0)
  23853. goto done;
  23854. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 11);
  23855. if (ret != 0)
  23856. goto done;
  23857. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 11);
  23858. if (ret != 0)
  23859. goto done;
  23860. /* get encrypted msg (request) to send to B */
  23861. outSz = sizeof(out);
  23862. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz,cliCtx);
  23863. if (ret != 0)
  23864. goto done;
  23865. #ifndef WOLFSSL_ECIES_OLD
  23866. wc_ecc_free(tmpKey);
  23867. #endif
  23868. /* B decrypts msg (request) from A */
  23869. plainSz = sizeof(plain);
  23870. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, srvCtx);
  23871. if (ret != 0)
  23872. goto done;
  23873. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  23874. ret = -10411; goto done;
  23875. }
  23876. /* msg2 (response) from B to A */
  23877. for (i = 0; i < (int)sizeof(msg2); i++)
  23878. msg2[i] = i + sizeof(msg2);
  23879. /* get encrypted msg (response) to send to B */
  23880. ret = wc_ecc_encrypt(userB, userA, msg2, sizeof(msg2), out2,
  23881. &outSz2, srvCtx);
  23882. if (ret != 0)
  23883. goto done;
  23884. #ifdef WOLFSSL_ECIES_OLD
  23885. tmpKey->dp = userB->dp;
  23886. ret = wc_ecc_copy_point(&userB->pubkey, &tmpKey->pubkey);
  23887. if (ret != 0) {
  23888. ret = -10414; goto done;
  23889. }
  23890. #else
  23891. wc_ecc_free(tmpKey);
  23892. #endif
  23893. /* A decrypts msg (response) from B */
  23894. ret = wc_ecc_decrypt(userA, tmpKey, out2, outSz2, plain2, &plainSz2,
  23895. cliCtx);
  23896. if (ret != 0)
  23897. goto done;
  23898. if (XMEMCMP(plain2, msg2, sizeof(msg2)) != 0) {
  23899. ret = -10412; goto done;
  23900. }
  23901. #if defined(HAVE_COMP_KEY) && \
  23902. (! defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  23903. /* Create new client and server contexts. */
  23904. wc_ecc_ctx_free(srvCtx);
  23905. wc_ecc_ctx_free(cliCtx);
  23906. /* let's verify message exchange works, A is client, B is server */
  23907. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  23908. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  23909. if (cliCtx == NULL || srvCtx == NULL) {
  23910. ret = -10416; goto done;
  23911. }
  23912. ret = wc_ecc_ctx_set_algo(cliCtx, encAlgo, kdfAlgo, macAlgo);
  23913. if (ret != 0)
  23914. goto done;
  23915. ret = wc_ecc_ctx_set_algo(srvCtx, encAlgo, kdfAlgo, macAlgo);
  23916. if (ret != 0)
  23917. goto done;
  23918. /* get salt to send to peer */
  23919. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  23920. if (tmpSalt == NULL) {
  23921. ret = -10417; goto done;
  23922. }
  23923. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  23924. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  23925. if (tmpSalt == NULL) {
  23926. ret = -10418; goto done;
  23927. }
  23928. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  23929. /* in actual use, we'd get the peer's salt over the transport */
  23930. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  23931. if (ret != 0)
  23932. goto done;
  23933. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  23934. if (ret != 0)
  23935. goto done;
  23936. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 12);
  23937. if (ret != 0)
  23938. goto done;
  23939. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 12);
  23940. if (ret != 0)
  23941. goto done;
  23942. /* get encrypted msg (request) to send to B - compressed public key */
  23943. outSz = sizeof(out);
  23944. ret = wc_ecc_encrypt_ex(userA, userB, msg, sizeof(msg), out, &outSz, cliCtx,
  23945. 1);
  23946. if (ret != 0)
  23947. goto done;
  23948. #ifndef WOLFSSL_ECIES_OLD
  23949. wc_ecc_free(tmpKey);
  23950. #endif
  23951. /* B decrypts msg (request) from A - out has a compressed public key */
  23952. plainSz = sizeof(plain);
  23953. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, srvCtx);
  23954. if (ret != 0)
  23955. goto done;
  23956. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  23957. ret = -10419; goto done;
  23958. }
  23959. #endif /* HAVE_COMP_KEY && (!FIPS || FIPS>=5.3) */
  23960. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  23961. (ECC_MIN_KEY_SZ <= 256) && defined(WOLFSSL_AES_128)
  23962. ret = ecc_encrypt_kat(rng);
  23963. #endif
  23964. done:
  23965. /* cleanup */
  23966. wc_ecc_ctx_free(srvCtx);
  23967. wc_ecc_ctx_free(cliCtx);
  23968. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23969. if (tmpKey != NULL) {
  23970. wc_ecc_free(tmpKey);
  23971. XFREE(tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23972. }
  23973. #else
  23974. wc_ecc_free(tmpKey);
  23975. #endif
  23976. return ret;
  23977. }
  23978. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  23979. WOLFSSL_TEST_SUBROUTINE int ecc_encrypt_test(void)
  23980. {
  23981. WC_RNG rng;
  23982. int ret;
  23983. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23984. ecc_key *userA;
  23985. ecc_key *userB;
  23986. #else
  23987. ecc_key userA[1];
  23988. ecc_key userB[1];
  23989. #endif
  23990. #ifndef HAVE_FIPS
  23991. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  23992. #else
  23993. ret = wc_InitRng(&rng);
  23994. #endif
  23995. if (ret != 0)
  23996. return -10400;
  23997. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23998. userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT,
  23999. DYNAMIC_TYPE_TMP_BUFFER);
  24000. userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT,
  24001. DYNAMIC_TYPE_TMP_BUFFER);
  24002. if ((userA == NULL) || (userB == NULL)) {
  24003. ERROR_OUT(MEMORY_E, done);
  24004. }
  24005. #endif
  24006. XMEMSET(userA, 0, sizeof *userA);
  24007. XMEMSET(userB, 0, sizeof *userB);
  24008. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  24009. if (ret != 0)
  24010. goto done;
  24011. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  24012. if (ret != 0)
  24013. goto done;
  24014. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userA);
  24015. #if defined(WOLFSSL_ASYNC_CRYPT)
  24016. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  24017. #endif
  24018. if (ret != 0){
  24019. ret = -10401; goto done;
  24020. }
  24021. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userB);
  24022. #if defined(WOLFSSL_ASYNC_CRYPT)
  24023. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  24024. #endif
  24025. if (ret != 0){
  24026. ret = -10402; goto done;
  24027. }
  24028. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  24029. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  24030. !defined(HAVE_SELFTEST)
  24031. ret = wc_ecc_set_rng(userA, &rng);
  24032. if (ret != 0) {
  24033. ret = -10403; goto done;
  24034. }
  24035. ret = wc_ecc_set_rng(userB, &rng);
  24036. if (ret != 0) {
  24037. ret = -10404; goto done;
  24038. }
  24039. #endif
  24040. #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))
  24041. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  24042. #ifdef WOLFSSL_AES_128
  24043. if (ret == 0) {
  24044. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CBC,
  24045. ecHKDF_SHA256, ecHMAC_SHA256);
  24046. if (ret != 0) {
  24047. printf("ECIES: AES_128_CBC, HKDF_SHA256, HMAC_SHA256\n");
  24048. }
  24049. }
  24050. #endif
  24051. #ifdef WOLFSSL_AES_256
  24052. if (ret == 0) {
  24053. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_256_CBC,
  24054. ecHKDF_SHA256, ecHMAC_SHA256);
  24055. if (ret != 0) {
  24056. printf("ECIES: AES_256_CBC, HKDF_SHA256, HMAC_SHA256\n");
  24057. }
  24058. }
  24059. #endif
  24060. #endif
  24061. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER)
  24062. #ifdef WOLFSSL_AES_128
  24063. if (ret == 0) {
  24064. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CTR,
  24065. ecHKDF_SHA256, ecHMAC_SHA256);
  24066. if (ret != 0) {
  24067. printf("ECIES: AES_128_CTR, HKDF_SHA256, HMAC_SHA256\n");
  24068. }
  24069. }
  24070. #endif
  24071. #ifdef WOLFSSL_AES_256
  24072. if (ret == 0) {
  24073. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_256_CTR,
  24074. ecHKDF_SHA256, ecHMAC_SHA256);
  24075. if (ret != 0) {
  24076. printf("ECIES: AES_256_CTR, HKDF_SHA256, HMAC_SHA256\n");
  24077. }
  24078. }
  24079. #endif
  24080. #endif
  24081. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  24082. done:
  24083. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24084. if (userA != NULL) {
  24085. wc_ecc_free(userA);
  24086. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24087. }
  24088. if (userB != NULL) {
  24089. wc_ecc_free(userB);
  24090. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24091. }
  24092. #else
  24093. wc_ecc_free(userB);
  24094. wc_ecc_free(userA);
  24095. #endif
  24096. wc_FreeRng(&rng);
  24097. return ret;
  24098. }
  24099. #endif /* HAVE_ECC_ENCRYPT && HAVE_AES_CBC && WOLFSSL_AES_128 */
  24100. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  24101. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  24102. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  24103. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  24104. WOLFSSL_TEST_SUBROUTINE int ecc_test_buffers(void)
  24105. {
  24106. size_t bytes;
  24107. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24108. ecc_key *cliKey = (ecc_key *)XMALLOC(sizeof *cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24109. ecc_key *servKey = (ecc_key *)XMALLOC(sizeof *servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24110. ecc_key *tmpKey = (ecc_key *)XMALLOC(sizeof *tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24111. #else
  24112. ecc_key cliKey[1];
  24113. ecc_key servKey[1];
  24114. ecc_key tmpKey[1];
  24115. #endif
  24116. WC_RNG rng;
  24117. word32 idx = 0;
  24118. int ret;
  24119. /* pad our test message to 32 bytes so evenly divisible by AES_BLOCK_SZ */
  24120. byte in[] = "Everyone gets Friday off. ecc p";
  24121. word32 inLen = (word32)XSTRLEN((char*)in);
  24122. byte out[256];
  24123. byte plain[256];
  24124. int verify = 0;
  24125. word32 x;
  24126. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24127. if ((cliKey == NULL) || (servKey == NULL) || (tmpKey == NULL))
  24128. ERROR_OUT(MEMORY_E, done);
  24129. #endif
  24130. ret = wc_ecc_init_ex(cliKey, HEAP_HINT, devId);
  24131. if (ret != 0)
  24132. ERROR_OUT(-10420, done);
  24133. ret = wc_ecc_init_ex(servKey, HEAP_HINT, devId);
  24134. if (ret != 0)
  24135. ERROR_OUT(-10421, done);
  24136. ret = wc_ecc_init_ex(tmpKey, HEAP_HINT, devId);
  24137. if (ret != 0)
  24138. ERROR_OUT(-10421, done);
  24139. bytes = (size_t)sizeof_ecc_clikey_der_256;
  24140. /* place client key into ecc_key struct cliKey */
  24141. ret = wc_EccPrivateKeyDecode(ecc_clikey_der_256, &idx, cliKey,
  24142. (word32)bytes);
  24143. if (ret != 0)
  24144. ERROR_OUT(-10422, done);
  24145. idx = 0;
  24146. bytes = (size_t)sizeof_ecc_key_der_256;
  24147. /* place server key into ecc_key struct servKey */
  24148. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, servKey,
  24149. (word32)bytes);
  24150. if (ret != 0)
  24151. ERROR_OUT(-10423, done);
  24152. #ifndef WC_NO_RNG
  24153. #ifndef HAVE_FIPS
  24154. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  24155. #else
  24156. ret = wc_InitRng(&rng);
  24157. #endif
  24158. if (ret != 0)
  24159. ERROR_OUT(-10424, done);
  24160. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  24161. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  24162. !defined(HAVE_SELFTEST)
  24163. ret = wc_ecc_set_rng(cliKey, &rng);
  24164. if (ret != 0) {
  24165. ERROR_OUT(-10425, done);
  24166. }
  24167. ret = wc_ecc_set_rng(servKey, &rng);
  24168. if (ret != 0) {
  24169. ERROR_OUT(-10425, done);
  24170. }
  24171. #endif
  24172. #endif /* !WC_NO_RNG */
  24173. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_HKDF) && \
  24174. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  24175. {
  24176. word32 y;
  24177. /* test encrypt and decrypt if they're available */
  24178. x = sizeof(out);
  24179. ret = wc_ecc_encrypt(cliKey, servKey, in, sizeof(in), out, &x, NULL);
  24180. if (ret < 0)
  24181. ERROR_OUT(-10426, done);
  24182. #ifdef WOLFSSL_ECIES_OLD
  24183. tmpKey->dp = cliKey->dp;
  24184. ret = wc_ecc_copy_point(&cliKey->pubkey, &tmpKey->pubkey);
  24185. if (ret != 0) {
  24186. ret = -10414; goto done;
  24187. }
  24188. #endif
  24189. y = sizeof(plain);
  24190. ret = wc_ecc_decrypt(servKey, tmpKey, out, x, plain, &y, NULL);
  24191. if (ret < 0)
  24192. ERROR_OUT(-10427, done);
  24193. if (XMEMCMP(plain, in, inLen))
  24194. ERROR_OUT(-10428, done);
  24195. }
  24196. #endif
  24197. x = sizeof(out);
  24198. do {
  24199. #if defined(WOLFSSL_ASYNC_CRYPT)
  24200. ret = wc_AsyncWait(ret, &cliKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  24201. #endif
  24202. if (ret == 0)
  24203. ret = wc_ecc_sign_hash(in, inLen, out, &x, &rng, cliKey);
  24204. } while (ret == WC_PENDING_E);
  24205. if (ret < 0)
  24206. ERROR_OUT(-10429, done);
  24207. TEST_SLEEP();
  24208. XMEMSET(plain, 0, sizeof(plain));
  24209. do {
  24210. #if defined(WOLFSSL_ASYNC_CRYPT)
  24211. ret = wc_AsyncWait(ret, &cliKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  24212. #endif
  24213. if (ret == 0)
  24214. ret = wc_ecc_verify_hash(out, x, in, inLen, &verify,
  24215. cliKey);
  24216. } while (ret == WC_PENDING_E);
  24217. if (ret < 0)
  24218. ERROR_OUT(-10430, done);
  24219. if (verify != 1)
  24220. ERROR_OUT(-10431, done);
  24221. TEST_SLEEP();
  24222. #ifdef WOLFSSL_CERT_EXT
  24223. idx = 0;
  24224. bytes = sizeof_ecc_clikeypub_der_256;
  24225. ret = wc_EccPublicKeyDecode(ecc_clikeypub_der_256, &idx, cliKey,
  24226. (word32) bytes);
  24227. if (ret != 0)
  24228. ERROR_OUT(-10432, done);
  24229. #endif
  24230. ret = 0;
  24231. done:
  24232. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  24233. if (cliKey != NULL) {
  24234. wc_ecc_free(cliKey);
  24235. XFREE(cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24236. }
  24237. if (servKey != NULL) {
  24238. wc_ecc_free(servKey);
  24239. XFREE(servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24240. }
  24241. if (tmpKey != NULL) {
  24242. wc_ecc_free(tmpKey);
  24243. XFREE(tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24244. }
  24245. #else
  24246. wc_ecc_free(cliKey);
  24247. wc_ecc_free(servKey);
  24248. wc_ecc_free(tmpKey);
  24249. #endif
  24250. wc_FreeRng(&rng);
  24251. return ret;
  24252. }
  24253. #endif /* USE_CERT_BUFFERS_256 && !WOLFSSL_ATECCX08A && !NO_ECC256 */
  24254. #endif /* HAVE_ECC */
  24255. #ifdef HAVE_CURVE25519
  24256. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  24257. defined(HAVE_CURVE25519_KEY_IMPORT)
  24258. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  24259. #define X25519_TEST_CNT 5
  24260. #else
  24261. #define X25519_TEST_CNT 1
  24262. #endif
  24263. static int curve25519_overflow_test(void)
  24264. {
  24265. /* secret key for party a */
  24266. byte sa[X25519_TEST_CNT][32] = {
  24267. {
  24268. 0x8d,0xaf,0x6e,0x7a,0xc1,0xeb,0x8d,0x30,
  24269. 0x99,0x86,0xd3,0x90,0x47,0x96,0x21,0x3c,
  24270. 0x3a,0x75,0xc0,0x7b,0x75,0x01,0x75,0xa3,
  24271. 0x81,0x4b,0xff,0x5a,0xbc,0x96,0x87,0x28
  24272. },
  24273. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  24274. {
  24275. 0x9d,0x63,0x5f,0xce,0xe2,0xe8,0xd7,0xfb,
  24276. 0x68,0x77,0x0e,0x44,0xd1,0xad,0x87,0x2b,
  24277. 0xf4,0x65,0x06,0xb7,0xbb,0xdb,0xbe,0x6e,
  24278. 0x02,0x43,0x24,0xc7,0x3d,0x7b,0x88,0x60
  24279. },
  24280. {
  24281. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  24282. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  24283. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  24284. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  24285. },
  24286. {
  24287. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  24288. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  24289. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  24290. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  24291. },
  24292. {
  24293. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  24294. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  24295. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  24296. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  24297. }
  24298. #endif
  24299. };
  24300. /* public key for party b */
  24301. byte pb[X25519_TEST_CNT][32] = {
  24302. {
  24303. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  24304. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  24305. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  24306. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  24307. },
  24308. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  24309. {
  24310. /* 0xff first byte in original - invalid! */
  24311. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  24312. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  24313. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  24314. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  24315. },
  24316. {
  24317. 0x36,0x1a,0x74,0x87,0x28,0x59,0xe0,0xb6,
  24318. 0xe4,0x2b,0x17,0x9b,0x16,0xb0,0x3b,0xf8,
  24319. 0xb8,0x9f,0x2a,0x8f,0xc5,0x33,0x68,0x4f,
  24320. 0xde,0x4d,0xd8,0x80,0x63,0xe7,0xb4,0x0a
  24321. },
  24322. {
  24323. 0x00,0x80,0x38,0x59,0x19,0x3a,0x66,0x12,
  24324. 0xfd,0xa1,0xec,0x1c,0x40,0x84,0x40,0xbd,
  24325. 0x64,0x10,0x8b,0x53,0x81,0x21,0x03,0x2d,
  24326. 0x7d,0x33,0xb4,0x01,0x57,0x0d,0xe1,0x89
  24327. },
  24328. {
  24329. 0x1d,0xf8,0xf8,0x33,0x89,0x6c,0xb7,0xba,
  24330. 0x94,0x73,0xfa,0xc2,0x36,0xac,0xbe,0x49,
  24331. 0xaf,0x85,0x3e,0x93,0x5f,0xae,0xb2,0xc0,
  24332. 0xc8,0x80,0x8f,0x4a,0xaa,0xd3,0x55,0x2b
  24333. }
  24334. #endif
  24335. };
  24336. /* expected shared key */
  24337. byte ss[X25519_TEST_CNT][32] = {
  24338. {
  24339. 0x5c,0x4c,0x85,0x5f,0xfb,0x20,0x38,0xcc,
  24340. 0x55,0x16,0x5b,0x8a,0xa7,0xed,0x57,0x6e,
  24341. 0x35,0xaa,0x71,0x67,0x85,0x1f,0xb6,0x28,
  24342. 0x17,0x07,0x7b,0xda,0x76,0xdd,0xe0,0xb4
  24343. },
  24344. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  24345. {
  24346. 0x33,0xf6,0xc1,0x34,0x62,0x92,0x06,0x02,
  24347. 0x95,0xdb,0x91,0x4c,0x5d,0x52,0x54,0xc7,
  24348. 0xd2,0x5b,0x24,0xb5,0x4f,0x33,0x59,0x79,
  24349. 0x9f,0x6d,0x7e,0x4a,0x4c,0x30,0xd6,0x38
  24350. },
  24351. {
  24352. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24353. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24354. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24355. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02
  24356. },
  24357. {
  24358. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24359. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24360. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24361. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09
  24362. },
  24363. {
  24364. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24365. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24366. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24367. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10
  24368. }
  24369. #endif
  24370. };
  24371. int ret = 0;
  24372. int i;
  24373. word32 y;
  24374. byte shared[32];
  24375. curve25519_key userA;
  24376. wc_curve25519_init_ex(&userA, HEAP_HINT, devId);
  24377. for (i = 0; i < X25519_TEST_CNT; i++) {
  24378. if (wc_curve25519_import_private_raw(sa[i], sizeof(sa[i]), pb[i],
  24379. sizeof(pb[i]), &userA) != 0) {
  24380. ret = -10500 - i; break;
  24381. }
  24382. /* test against known test vector */
  24383. XMEMSET(shared, 0, sizeof(shared));
  24384. y = sizeof(shared);
  24385. if (wc_curve25519_shared_secret(&userA, &userA, shared, &y) != 0) {
  24386. ret = -10510 - i; break;
  24387. }
  24388. if (XMEMCMP(ss[i], shared, y)) {
  24389. ret = -10520 - i; break;
  24390. }
  24391. }
  24392. wc_curve25519_free(&userA);
  24393. return ret;
  24394. }
  24395. /* Test the wc_curve25519_check_public API.
  24396. *
  24397. * returns 0 on success and -ve on failure.
  24398. */
  24399. static int curve25519_check_public_test(void)
  24400. {
  24401. /* Little-endian values that will fail */
  24402. byte fail_le[][CURVE25519_KEYSIZE] = {
  24403. {
  24404. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24405. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24406. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24407. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  24408. },
  24409. {
  24410. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24411. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24412. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24413. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  24414. },
  24415. {
  24416. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24417. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24418. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24419. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x81
  24420. },
  24421. };
  24422. /* Big-endian values that will fail */
  24423. byte fail_be[][CURVE25519_KEYSIZE] = {
  24424. {
  24425. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24426. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24427. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24428. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  24429. },
  24430. {
  24431. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24432. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24433. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24434. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  24435. },
  24436. {
  24437. 0x81,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24438. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24439. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24440. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  24441. },
  24442. };
  24443. /* Good or valid public value */
  24444. byte good[CURVE25519_KEYSIZE] = {
  24445. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24446. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24447. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24448. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  24449. };
  24450. int i;
  24451. /* Parameter checks */
  24452. /* NULL pointer */
  24453. if (wc_curve25519_check_public(NULL, 0, EC25519_LITTLE_ENDIAN) !=
  24454. BAD_FUNC_ARG) {
  24455. return -10600;
  24456. }
  24457. if (wc_curve25519_check_public(NULL, 0, EC25519_BIG_ENDIAN) !=
  24458. BAD_FUNC_ARG) {
  24459. return -10601;
  24460. }
  24461. /* Length of 0 treated differently to other invalid lengths for TLS */
  24462. if (wc_curve25519_check_public(good, 0, EC25519_LITTLE_ENDIAN) != BUFFER_E)
  24463. return -10602;
  24464. if (wc_curve25519_check_public(good, 0, EC25519_BIG_ENDIAN) != BUFFER_E)
  24465. return -10603;
  24466. /* Length not CURVE25519_KEYSIZE */
  24467. for (i = 1; i < CURVE25519_KEYSIZE + 2; i++) {
  24468. if (i == CURVE25519_KEYSIZE)
  24469. continue;
  24470. if (wc_curve25519_check_public(good, i, EC25519_LITTLE_ENDIAN) !=
  24471. ECC_BAD_ARG_E) {
  24472. return -10604 - i;
  24473. }
  24474. if (wc_curve25519_check_public(good, i, EC25519_BIG_ENDIAN) !=
  24475. ECC_BAD_ARG_E) {
  24476. return -10614 - i;
  24477. }
  24478. }
  24479. /* Little-endian fail cases */
  24480. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  24481. if (wc_curve25519_check_public(fail_le[i], CURVE25519_KEYSIZE,
  24482. EC25519_LITTLE_ENDIAN) == 0) {
  24483. return -10624 - i;
  24484. }
  24485. }
  24486. /* Big-endian fail cases */
  24487. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  24488. if (wc_curve25519_check_public(fail_be[i], CURVE25519_KEYSIZE,
  24489. EC25519_BIG_ENDIAN) == 0) {
  24490. return -10634 - i;
  24491. }
  24492. }
  24493. /* Check a valid public value works! */
  24494. if (wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  24495. EC25519_LITTLE_ENDIAN) != 0) {
  24496. return -10644;
  24497. }
  24498. if (wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  24499. EC25519_BIG_ENDIAN) != 0) {
  24500. return -10645;
  24501. }
  24502. return 0;
  24503. }
  24504. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  24505. #if !defined(NO_ASN) && defined(HAVE_CURVE25519_KEY_EXPORT) && \
  24506. defined(HAVE_CURVE25519_KEY_IMPORT)
  24507. static int curve255519_der_test(void)
  24508. {
  24509. int ret = 0;
  24510. /* certs/statickeys/x25519.der */
  24511. const byte kCurve25519PrivDer[] = {
  24512. 0x30, 0x2E, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x6E,
  24513. 0x04, 0x22, 0x04, 0x20, 0x78, 0x8E, 0x31, 0x5C, 0x33, 0xA9, 0x19, 0xC0,
  24514. 0x5E, 0x36, 0x70, 0x1B, 0xA4, 0xE8, 0xEF, 0xC1, 0x89, 0x8C, 0xB3, 0x15,
  24515. 0xC6, 0x79, 0xD3, 0xAC, 0x22, 0x00, 0xAE, 0xFA, 0xB3, 0xB7, 0x0F, 0x78
  24516. };
  24517. /* certs/statickeys/x25519-pub.der */
  24518. const byte kCurve25519PubDer[] = {
  24519. 0x30, 0x2A, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x6E, 0x03, 0x21, 0x00,
  24520. 0x09, 0xBC, 0x8C, 0xC7, 0x45, 0x0D, 0xC1, 0xC2, 0x02, 0x57, 0x9A, 0x68,
  24521. 0x3A, 0xFD, 0x7A, 0xA8, 0xA5, 0x2F, 0xF0, 0x99, 0x39, 0x98, 0xEA, 0x26,
  24522. 0xA2, 0x5B, 0x38, 0xFD, 0x96, 0xDB, 0x2A, 0x26
  24523. };
  24524. curve25519_key key;
  24525. byte output[128];
  24526. word32 outputSz = 128;
  24527. word32 idx;
  24528. if (wc_curve25519_init_ex(&key, HEAP_HINT, devId) != 0) {
  24529. return -10723;
  24530. }
  24531. /* Test decode / encode of Curve25519 private key only */
  24532. if (ret == 0) {
  24533. idx = 0;
  24534. ret = wc_Curve25519PrivateKeyDecode(kCurve25519PrivDer, &idx, &key,
  24535. (word32)sizeof(kCurve25519PrivDer));
  24536. }
  24537. if (ret == 0) {
  24538. outputSz = (word32)sizeof(output);
  24539. ret = wc_Curve25519PrivateKeyToDer(&key, output, outputSz);
  24540. if (ret >= 0) {
  24541. outputSz = ret;
  24542. ret = 0;
  24543. }
  24544. else {
  24545. ret = -10724;
  24546. }
  24547. }
  24548. if (ret == 0 && (outputSz != (word32)sizeof(kCurve25519PrivDer) ||
  24549. XMEMCMP(output, kCurve25519PrivDer, outputSz) != 0)) {
  24550. ret = -10725;
  24551. }
  24552. /* Test decode / encode of Curve25519 public key only */
  24553. if (ret == 0) {
  24554. idx = 0;
  24555. ret = wc_Curve25519PublicKeyDecode(kCurve25519PubDer, &idx, &key,
  24556. (word32)sizeof(kCurve25519PubDer));
  24557. }
  24558. if (ret == 0) {
  24559. outputSz = (word32)sizeof(output);
  24560. ret = wc_Curve25519PublicKeyToDer(&key, output, outputSz, 1);
  24561. if (ret >= 0) {
  24562. outputSz = ret;
  24563. ret = 0;
  24564. }
  24565. else {
  24566. ret = -10726;
  24567. }
  24568. }
  24569. if (ret == 0 && (outputSz != (word32)sizeof(kCurve25519PubDer) ||
  24570. XMEMCMP(output, kCurve25519PubDer, outputSz) != 0)) {
  24571. ret = -10727;
  24572. }
  24573. wc_curve25519_free(&key);
  24574. return ret;
  24575. }
  24576. #endif /* !NO_ASN && HAVE_CURVE25519_KEY_EXPORT && HAVE_CURVE25519_KEY_IMPORT */
  24577. WOLFSSL_TEST_SUBROUTINE int curve25519_test(void)
  24578. {
  24579. WC_RNG rng;
  24580. int ret;
  24581. #ifdef HAVE_CURVE25519_SHARED_SECRET
  24582. byte sharedA[32];
  24583. byte sharedB[32];
  24584. word32 y;
  24585. #endif
  24586. #ifdef HAVE_CURVE25519_KEY_EXPORT
  24587. byte exportBuf[32];
  24588. #endif
  24589. word32 x = 0;
  24590. curve25519_key userA, userB, pubKey;
  24591. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  24592. defined(HAVE_CURVE25519_KEY_IMPORT)
  24593. /* test vectors from
  24594. https://tools.ietf.org/html/draft-josefsson-tls-curve25519-03
  24595. */
  24596. /* secret key for party a */
  24597. byte sa[] = {
  24598. 0x5A,0xC9,0x9F,0x33,0x63,0x2E,0x5A,0x76,
  24599. 0x8D,0xE7,0xE8,0x1B,0xF8,0x54,0xC2,0x7C,
  24600. 0x46,0xE3,0xFB,0xF2,0xAB,0xBA,0xCD,0x29,
  24601. 0xEC,0x4A,0xFF,0x51,0x73,0x69,0xC6,0x60
  24602. };
  24603. /* public key for party a */
  24604. byte pa[] = {
  24605. 0x05,0x7E,0x23,0xEA,0x9F,0x1C,0xBE,0x8A,
  24606. 0x27,0x16,0x8F,0x6E,0x69,0x6A,0x79,0x1D,
  24607. 0xE6,0x1D,0xD3,0xAF,0x7A,0xCD,0x4E,0xEA,
  24608. 0xCC,0x6E,0x7B,0xA5,0x14,0xFD,0xA8,0x63
  24609. };
  24610. /* secret key for party b */
  24611. byte sb[] = {
  24612. 0x47,0xDC,0x3D,0x21,0x41,0x74,0x82,0x0E,
  24613. 0x11,0x54,0xB4,0x9B,0xC6,0xCD,0xB2,0xAB,
  24614. 0xD4,0x5E,0xE9,0x58,0x17,0x05,0x5D,0x25,
  24615. 0x5A,0xA3,0x58,0x31,0xB7,0x0D,0x32,0x60
  24616. };
  24617. /* public key for party b */
  24618. byte pb[] = {
  24619. 0x6E,0xB8,0x9D,0xA9,0x19,0x89,0xAE,0x37,
  24620. 0xC7,0xEA,0xC7,0x61,0x8D,0x9E,0x5C,0x49,
  24621. 0x51,0xDB,0xA1,0xD7,0x3C,0x28,0x5A,0xE1,
  24622. 0xCD,0x26,0xA8,0x55,0x02,0x0E,0xEF,0x04
  24623. };
  24624. /* expected shared key */
  24625. byte ss[] = {
  24626. 0x61,0x45,0x0C,0xD9,0x8E,0x36,0x01,0x6B,
  24627. 0x58,0x77,0x6A,0x89,0x7A,0x9F,0x0A,0xEF,
  24628. 0x73,0x8B,0x99,0xF0,0x94,0x68,0xB8,0xD6,
  24629. 0xB8,0x51,0x11,0x84,0xD5,0x34,0x94,0xAB
  24630. };
  24631. #endif /* HAVE_CURVE25519_SHARED_SECRET */
  24632. (void)x;
  24633. #ifndef HAVE_FIPS
  24634. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  24635. #else
  24636. ret = wc_InitRng(&rng);
  24637. #endif
  24638. if (ret != 0)
  24639. return -10700;
  24640. wc_curve25519_init_ex(&userA, HEAP_HINT, devId);
  24641. wc_curve25519_init_ex(&userB, HEAP_HINT, devId);
  24642. wc_curve25519_init_ex(&pubKey, HEAP_HINT, devId);
  24643. /* make curve25519 keys */
  24644. if (wc_curve25519_make_key(&rng, 32, &userA) != 0)
  24645. return -10701;
  24646. if (wc_curve25519_make_key(&rng, 32, &userB) != 0)
  24647. return -10702;
  24648. #ifdef HAVE_CURVE25519_SHARED_SECRET
  24649. /* find shared secret key */
  24650. x = sizeof(sharedA);
  24651. if ((ret = wc_curve25519_shared_secret(&userA, &userB, sharedA, &x)) != 0) {
  24652. printf("wc_curve25519_shared_secret 1 %d\n", ret);
  24653. return -10703;
  24654. }
  24655. y = sizeof(sharedB);
  24656. if ((ret = wc_curve25519_shared_secret(&userB, &userA, sharedB, &y)) != 0) {
  24657. printf("wc_curve25519_shared_secret 2 %d\n", ret);
  24658. return -10704;
  24659. }
  24660. /* compare shared secret keys to test they are the same */
  24661. if (y != x)
  24662. return -10705;
  24663. if (XMEMCMP(sharedA, sharedB, x))
  24664. return -10706;
  24665. #endif
  24666. #ifdef HAVE_CURVE25519_KEY_EXPORT
  24667. /* export a public key and import it for another user */
  24668. x = sizeof(exportBuf);
  24669. if (wc_curve25519_export_public(&userA, exportBuf, &x) != 0)
  24670. return -10707;
  24671. #ifdef HAVE_CURVE25519_KEY_IMPORT
  24672. if (wc_curve25519_import_public(exportBuf, x, &pubKey) != 0)
  24673. return -10708;
  24674. #endif
  24675. #endif
  24676. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  24677. defined(HAVE_CURVE25519_KEY_IMPORT)
  24678. /* test shared key after importing a public key */
  24679. XMEMSET(sharedB, 0, sizeof(sharedB));
  24680. y = sizeof(sharedB);
  24681. if (wc_curve25519_shared_secret(&userB, &pubKey, sharedB, &y) != 0)
  24682. return -10709;
  24683. if (XMEMCMP(sharedA, sharedB, y))
  24684. return -10710;
  24685. /* import RFC test vectors and compare shared key */
  24686. if (wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  24687. != 0)
  24688. return -10711;
  24689. if (wc_curve25519_import_private_raw(sb, sizeof(sb), pb, sizeof(pb), &userB)
  24690. != 0)
  24691. return -10712;
  24692. /* test against known test vector */
  24693. XMEMSET(sharedB, 0, sizeof(sharedB));
  24694. y = sizeof(sharedB);
  24695. if (wc_curve25519_shared_secret(&userA, &userB, sharedB, &y) != 0)
  24696. return -10713;
  24697. if (XMEMCMP(ss, sharedB, y))
  24698. return -10714;
  24699. /* test swapping roles of keys and generating same shared key */
  24700. XMEMSET(sharedB, 0, sizeof(sharedB));
  24701. y = sizeof(sharedB);
  24702. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  24703. return -10715;
  24704. if (XMEMCMP(ss, sharedB, y))
  24705. return -10716;
  24706. /* test with 1 generated key and 1 from known test vector */
  24707. if (wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  24708. != 0)
  24709. return -10717;
  24710. wc_curve25519_free(&userB);
  24711. wc_curve25519_init_ex(&userB, HEAP_HINT, devId);
  24712. if (wc_curve25519_make_key(&rng, 32, &userB) != 0)
  24713. return -10718;
  24714. x = sizeof(sharedA);
  24715. if (wc_curve25519_shared_secret(&userA, &userB, sharedA, &x) != 0)
  24716. return -10719;
  24717. y = sizeof(sharedB);
  24718. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  24719. return -10720;
  24720. /* compare shared secret keys to test they are the same */
  24721. if (y != x)
  24722. return -10721;
  24723. if (XMEMCMP(sharedA, sharedB, x))
  24724. return -10722;
  24725. ret = curve25519_overflow_test();
  24726. if (ret != 0)
  24727. return ret;
  24728. ret = curve25519_check_public_test();
  24729. if (ret != 0)
  24730. return ret;
  24731. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  24732. #if !defined(NO_ASN) && defined(HAVE_CURVE25519_KEY_EXPORT) && \
  24733. defined(HAVE_CURVE25519_KEY_IMPORT)
  24734. ret = curve255519_der_test();
  24735. if (ret != 0)
  24736. return ret;
  24737. #endif
  24738. /* clean up keys when done */
  24739. wc_curve25519_free(&pubKey);
  24740. wc_curve25519_free(&userB);
  24741. wc_curve25519_free(&userA);
  24742. wc_FreeRng(&rng);
  24743. return 0;
  24744. }
  24745. #endif /* HAVE_CURVE25519 */
  24746. #ifdef HAVE_ED25519
  24747. #ifdef WOLFSSL_TEST_CERT
  24748. static int ed25519_test_cert(void)
  24749. {
  24750. DecodedCert cert[2];
  24751. DecodedCert* serverCert = NULL;
  24752. DecodedCert* caCert = NULL;
  24753. #ifdef HAVE_ED25519_VERIFY
  24754. ed25519_key key;
  24755. ed25519_key* pubKey = NULL;
  24756. int verify;
  24757. #endif /* HAVE_ED25519_VERIFY */
  24758. int ret;
  24759. byte* tmp;
  24760. size_t bytes;
  24761. XFILE file;
  24762. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24763. if (tmp == NULL) {
  24764. ERROR_OUT(-10730, done);
  24765. }
  24766. #ifdef USE_CERT_BUFFERS_256
  24767. XMEMCPY(tmp, ca_ed25519_cert, sizeof_ca_ed25519_cert);
  24768. bytes = sizeof_ca_ed25519_cert;
  24769. #elif !defined(NO_FILESYSTEM)
  24770. file = XFOPEN(caEd25519Cert, "rb");
  24771. if (file == NULL) {
  24772. ERROR_OUT(-10731, done);
  24773. }
  24774. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  24775. XFCLOSE(file);
  24776. #else
  24777. /* No certificate to use. */
  24778. ERROR_OUT(-10732, done);
  24779. #endif
  24780. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  24781. caCert = &cert[0];
  24782. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  24783. if (ret != 0) {
  24784. ERROR_OUT(-10733, done);
  24785. }
  24786. #ifdef USE_CERT_BUFFERS_256
  24787. XMEMCPY(tmp, server_ed25519_cert, sizeof_server_ed25519_cert);
  24788. bytes = sizeof_server_ed25519_cert;
  24789. #elif !defined(NO_FILESYSTEM)
  24790. file = XFOPEN(serverEd25519Cert, "rb");
  24791. if (file == NULL) {
  24792. ERROR_OUT(-10734, done);
  24793. }
  24794. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  24795. XFCLOSE(file);
  24796. #else
  24797. /* No certificate to use. */
  24798. ERROR_OUT(-10735, done);
  24799. #endif
  24800. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  24801. serverCert = &cert[1];
  24802. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  24803. if (ret != 0) {
  24804. ERROR_OUT(-10736, done);
  24805. }
  24806. #ifdef HAVE_ED25519_VERIFY
  24807. ret = wc_ed25519_init(&key);
  24808. if (ret < 0) {
  24809. ERROR_OUT(-10737, done);
  24810. }
  24811. pubKey = &key;
  24812. ret = wc_ed25519_import_public(caCert->publicKey, caCert->pubKeySize,
  24813. pubKey);
  24814. if (ret < 0) {
  24815. ERROR_OUT(-10738, done);
  24816. }
  24817. if (wc_ed25519_verify_msg(serverCert->signature, serverCert->sigLength,
  24818. serverCert->source + serverCert->certBegin,
  24819. serverCert->sigIndex - serverCert->certBegin,
  24820. &verify, pubKey) < 0 || verify != 1) {
  24821. ERROR_OUT(-10739, done);
  24822. }
  24823. #endif /* HAVE_ED25519_VERIFY */
  24824. done:
  24825. if (tmp != NULL)
  24826. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24827. #ifdef HAVE_ED25519_VERIFY
  24828. wc_ed25519_free(pubKey);
  24829. #endif /* HAVE_ED25519_VERIFY */
  24830. if (caCert != NULL)
  24831. FreeDecodedCert(caCert);
  24832. if (serverCert != NULL)
  24833. FreeDecodedCert(serverCert);
  24834. return ret;
  24835. }
  24836. static int ed25519_test_make_cert(void)
  24837. {
  24838. WC_RNG rng;
  24839. Cert cert;
  24840. DecodedCert decode;
  24841. ed25519_key key;
  24842. ed25519_key* privKey = NULL;
  24843. int ret = 0;
  24844. byte* tmp = NULL;
  24845. wc_InitCert_ex(&cert, HEAP_HINT, devId);
  24846. #ifndef HAVE_FIPS
  24847. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  24848. #else
  24849. ret = wc_InitRng(&rng);
  24850. #endif
  24851. if (ret != 0)
  24852. return -10750;
  24853. wc_ed25519_init(&key);
  24854. privKey = &key;
  24855. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, privKey);
  24856. cert.daysValid = 365 * 2;
  24857. cert.selfSigned = 1;
  24858. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  24859. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  24860. cert.isCA = 0;
  24861. #ifdef WOLFSSL_CERT_EXT
  24862. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  24863. if (ret < 0) {
  24864. ERROR_OUT(-10751, done);
  24865. }
  24866. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  24867. if (ret < 0) {
  24868. ERROR_OUT(-10752, done);
  24869. }
  24870. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  24871. if (ret < 0) {
  24872. ERROR_OUT(-10753, done);
  24873. }
  24874. #endif
  24875. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24876. if (tmp == NULL) {
  24877. ERROR_OUT(-10754, done);
  24878. }
  24879. cert.sigType = CTC_ED25519;
  24880. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED25519_TYPE, privKey, &rng);
  24881. if (ret < 0) {
  24882. ERROR_OUT(-10755, done);
  24883. }
  24884. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF,
  24885. ED25519_TYPE, privKey, &rng);
  24886. if (ret < 0) {
  24887. ERROR_OUT(-10756, done);
  24888. }
  24889. InitDecodedCert(&decode, tmp, ret, HEAP_HINT);
  24890. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  24891. FreeDecodedCert(&decode);
  24892. if (ret != 0) {
  24893. ERROR_OUT(-10757, done);
  24894. }
  24895. done:
  24896. if (tmp != NULL)
  24897. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24898. wc_ed25519_free(privKey);
  24899. wc_FreeRng(&rng);
  24900. return ret;
  24901. }
  24902. #endif /* WOLFSSL_TEST_CERT */
  24903. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  24904. defined(HAVE_ED25519_KEY_IMPORT)
  24905. static int ed25519ctx_test(void)
  24906. {
  24907. int ret;
  24908. byte out[ED25519_SIG_SIZE];
  24909. word32 outlen;
  24910. #ifdef HAVE_ED25519_VERIFY
  24911. int verify = 0;
  24912. #endif /* HAVE_ED25519_VERIFY */
  24913. ed25519_key key;
  24914. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  24915. 0x03,0x05,0x33,0x4e,0x38,0x1a,0xf7,0x8f,
  24916. 0x14,0x1c,0xb6,0x66,0xf6,0x19,0x9f,0x57,
  24917. 0xbc,0x34,0x95,0x33,0x5a,0x25,0x6a,0x95,
  24918. 0xbd,0x2a,0x55,0xbf,0x54,0x66,0x63,0xf6
  24919. };
  24920. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  24921. 0xdf,0xc9,0x42,0x5e,0x4f,0x96,0x8f,0x7f,
  24922. 0x0c,0x29,0xf0,0x25,0x9c,0xf5,0xf9,0xae,
  24923. 0xd6,0x85,0x1c,0x2b,0xb4,0xad,0x8b,0xfb,
  24924. 0x86,0x0c,0xfe,0xe0,0xab,0x24,0x82,0x92
  24925. };
  24926. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx1[] = {
  24927. 0x55,0xa4,0xcc,0x2f,0x70,0xa5,0x4e,0x04,
  24928. 0x28,0x8c,0x5f,0x4c,0xd1,0xe4,0x5a,0x7b,
  24929. 0xb5,0x20,0xb3,0x62,0x92,0x91,0x18,0x76,
  24930. 0xca,0xda,0x73,0x23,0x19,0x8d,0xd8,0x7a,
  24931. 0x8b,0x36,0x95,0x0b,0x95,0x13,0x00,0x22,
  24932. 0x90,0x7a,0x7f,0xb7,0xc4,0xe9,0xb2,0xd5,
  24933. 0xf6,0xcc,0xa6,0x85,0xa5,0x87,0xb4,0xb2,
  24934. 0x1f,0x4b,0x88,0x8e,0x4e,0x7e,0xdb,0x0d
  24935. };
  24936. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx2[] = {
  24937. 0xcc,0x5e,0x63,0xa2,0x7e,0x94,0xaf,0xd3,
  24938. 0x41,0x83,0x38,0xd2,0x48,0x6f,0xa9,0x2a,
  24939. 0xf9,0x91,0x7c,0x2d,0x98,0x9e,0x06,0xe5,
  24940. 0x02,0x77,0x72,0x1c,0x34,0x38,0x18,0xb4,
  24941. 0x21,0x96,0xbc,0x29,0x2e,0x68,0xf3,0x4d,
  24942. 0x85,0x9b,0xbe,0xad,0x17,0x9f,0x54,0x54,
  24943. 0x2d,0x4b,0x04,0xdc,0xfb,0xfa,0x4a,0x68,
  24944. 0x4e,0x39,0x50,0xfb,0x1c,0xcd,0x8d,0x0d
  24945. };
  24946. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  24947. 0xf7,0x26,0x93,0x6d,0x19,0xc8,0x00,0x49,
  24948. 0x4e,0x3f,0xda,0xff,0x20,0xb2,0x76,0xa8
  24949. };
  24950. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  24951. 0x66,0x6f,0x6f
  24952. };
  24953. outlen = sizeof(out);
  24954. XMEMSET(out, 0, sizeof(out));
  24955. ret = wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  24956. if (ret != 0)
  24957. return 10800;
  24958. ret = wc_ed25519_import_private_key(sKeyCtx, ED25519_KEY_SIZE, pKeyCtx,
  24959. sizeof(pKeyCtx), &key);
  24960. if (ret == 0)
  24961. ret = wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  24962. contextCtx, sizeof(contextCtx));
  24963. if (ret == 0 && XMEMCMP(out, sigCtx1, 64) != 0)
  24964. ret = -10801;
  24965. #if defined(HAVE_ED25519_VERIFY)
  24966. /* test verify on good msg */
  24967. if (ret == 0)
  24968. ret = wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx),
  24969. &verify, &key, contextCtx, sizeof(contextCtx));
  24970. if (ret == 0 && verify != 1)
  24971. ret = -10802;
  24972. #endif
  24973. if (ret == 0)
  24974. ret = wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  24975. NULL, 0);
  24976. if (ret == 0 && XMEMCMP(out, sigCtx2, 64) != 0)
  24977. ret = -10803;
  24978. #if defined(HAVE_ED25519_VERIFY)
  24979. /* test verify on good msg */
  24980. if (ret == 0)
  24981. ret = wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx),
  24982. &verify, &key, NULL, 0);
  24983. if (ret == 0 && verify != 1)
  24984. ret = -10804;
  24985. #endif
  24986. wc_ed25519_free(&key);
  24987. return ret;
  24988. }
  24989. static int ed25519ph_test(void)
  24990. {
  24991. int ret = 0;
  24992. byte out[ED25519_SIG_SIZE];
  24993. word32 outlen;
  24994. #ifdef HAVE_ED25519_VERIFY
  24995. int verify = 0;
  24996. #endif /* HAVE_ED25519_VERIFY */
  24997. ed25519_key key;
  24998. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  24999. 0x83,0x3f,0xe6,0x24,0x09,0x23,0x7b,0x9d,
  25000. 0x62,0xec,0x77,0x58,0x75,0x20,0x91,0x1e,
  25001. 0x9a,0x75,0x9c,0xec,0x1d,0x19,0x75,0x5b,
  25002. 0x7d,0xa9,0x01,0xb9,0x6d,0xca,0x3d,0x42
  25003. };
  25004. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  25005. 0xec,0x17,0x2b,0x93,0xad,0x5e,0x56,0x3b,
  25006. 0xf4,0x93,0x2c,0x70,0xe1,0x24,0x50,0x34,
  25007. 0xc3,0x54,0x67,0xef,0x2e,0xfd,0x4d,0x64,
  25008. 0xeb,0xf8,0x19,0x68,0x34,0x67,0xe2,0xbf
  25009. };
  25010. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  25011. 0x98,0xa7,0x02,0x22,0xf0,0xb8,0x12,0x1a,
  25012. 0xa9,0xd3,0x0f,0x81,0x3d,0x68,0x3f,0x80,
  25013. 0x9e,0x46,0x2b,0x46,0x9c,0x7f,0xf8,0x76,
  25014. 0x39,0x49,0x9b,0xb9,0x4e,0x6d,0xae,0x41,
  25015. 0x31,0xf8,0x50,0x42,0x46,0x3c,0x2a,0x35,
  25016. 0x5a,0x20,0x03,0xd0,0x62,0xad,0xf5,0xaa,
  25017. 0xa1,0x0b,0x8c,0x61,0xe6,0x36,0x06,0x2a,
  25018. 0xaa,0xd1,0x1c,0x2a,0x26,0x08,0x34,0x06
  25019. };
  25020. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  25021. 0xe0,0x39,0x70,0x2b,0x4c,0x25,0x95,0xa6,
  25022. 0xa5,0x41,0xac,0x85,0x09,0x23,0x6e,0x29,
  25023. 0x90,0x47,0x47,0x95,0x33,0x0c,0x9b,0x34,
  25024. 0xa7,0x5f,0x58,0xa6,0x60,0x12,0x9e,0x08,
  25025. 0xfd,0x73,0x69,0x43,0xfb,0x19,0x43,0xa5,
  25026. 0x57,0x20,0xb9,0xe0,0x95,0x7b,0x1e,0xd6,
  25027. 0x73,0x48,0x16,0x61,0x9f,0x13,0x88,0xf4,
  25028. 0x3f,0x73,0xe6,0xe3,0xba,0xa8,0x1c,0x0e
  25029. };
  25030. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  25031. 0x61,0x62,0x63
  25032. };
  25033. /* SHA-512 hash of msgPh */
  25034. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  25035. 0xdd,0xaf,0x35,0xa1,0x93,0x61,0x7a,0xba,
  25036. 0xcc,0x41,0x73,0x49,0xae,0x20,0x41,0x31,
  25037. 0x12,0xe6,0xfa,0x4e,0x89,0xa9,0x7e,0xa2,
  25038. 0x0a,0x9e,0xee,0xe6,0x4b,0x55,0xd3,0x9a,
  25039. 0x21,0x92,0x99,0x2a,0x27,0x4f,0xc1,0xa8,
  25040. 0x36,0xba,0x3c,0x23,0xa3,0xfe,0xeb,0xbd,
  25041. 0x45,0x4d,0x44,0x23,0x64,0x3c,0xe8,0x0e,
  25042. 0x2a,0x9a,0xc9,0x4f,0xa5,0x4c,0xa4,0x9f
  25043. };
  25044. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  25045. 0x66,0x6f,0x6f
  25046. };
  25047. outlen = sizeof(out);
  25048. XMEMSET(out, 0, sizeof(out));
  25049. ret = wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  25050. if (ret != 0)
  25051. return -10900;
  25052. ret = wc_ed25519_import_private_key(sKeyPh, ED25519_KEY_SIZE, pKeyPh,
  25053. sizeof(pKeyPh), &key);
  25054. if (ret == 0)
  25055. ret = wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  25056. NULL, 0);
  25057. if (ret == 0 && XMEMCMP(out, sigPh1, 64) != 0)
  25058. ret = -10901;
  25059. #if defined(HAVE_ED25519_VERIFY)
  25060. /* test verify on good msg */
  25061. if (ret == 0)
  25062. ret = wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh),
  25063. &verify, &key, NULL, 0);
  25064. if (ret == 0 && verify != 1)
  25065. ret = -10902;
  25066. #endif
  25067. if (ret == 0)
  25068. ret = wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  25069. contextPh2, sizeof(contextPh2));
  25070. if (ret == 0 && XMEMCMP(out, sigPh2, 64) != 0)
  25071. ret = -10903;
  25072. #if defined(HAVE_ED25519_VERIFY)
  25073. /* test verify on good msg */
  25074. if (ret == 0)
  25075. ret = wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify,
  25076. &key, contextPh2, sizeof(contextPh2));
  25077. if (ret == 0 && verify != 1)
  25078. ret = -10904;
  25079. #endif
  25080. if (ret == 0)
  25081. ret = wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  25082. NULL, 0);
  25083. if (ret == 0 && XMEMCMP(out, sigPh1, 64) != 0)
  25084. ret = -10905;
  25085. #if defined(HAVE_ED25519_VERIFY)
  25086. if (ret == 0)
  25087. ret = wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh),
  25088. &verify, &key, NULL, 0);
  25089. if (ret == 0 && verify != 1)
  25090. ret = -10906;
  25091. #endif
  25092. if (ret == 0)
  25093. ret = wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  25094. contextPh2, sizeof(contextPh2));
  25095. if (ret == 0 && XMEMCMP(out, sigPh2, 64) != 0)
  25096. ret = -10907;
  25097. #if defined(HAVE_ED25519_VERIFY)
  25098. if (ret == 0)
  25099. ret = wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  25100. &key, contextPh2, sizeof(contextPh2));
  25101. if (ret == 0 && verify != 1)
  25102. ret = -10908;
  25103. #endif
  25104. wc_ed25519_free(&key);
  25105. return ret;
  25106. }
  25107. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  25108. WOLFSSL_TEST_SUBROUTINE int ed25519_test(void)
  25109. {
  25110. int ret;
  25111. WC_RNG rng;
  25112. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  25113. defined(HAVE_ED25519_KEY_IMPORT)
  25114. byte out[ED25519_SIG_SIZE];
  25115. byte exportPKey[ED25519_KEY_SIZE];
  25116. byte exportSKey[ED25519_KEY_SIZE];
  25117. word32 exportPSz;
  25118. word32 exportSSz;
  25119. int i;
  25120. word32 outlen;
  25121. #ifdef HAVE_ED25519_VERIFY
  25122. #ifdef WOLFSSL_ED25519_STREAMING_VERIFY
  25123. int j;
  25124. #endif
  25125. int verify;
  25126. #endif /* HAVE_ED25519_VERIFY */
  25127. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  25128. word32 keySz, sigSz;
  25129. ed25519_key key;
  25130. ed25519_key key2;
  25131. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  25132. defined(HAVE_ED25519_KEY_IMPORT)
  25133. /* test vectors from
  25134. https://tools.ietf.org/html/draft-josefsson-eddsa-ed25519-02
  25135. */
  25136. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  25137. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  25138. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  25139. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  25140. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  25141. };
  25142. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  25143. 0x4c,0xcd,0x08,0x9b,0x28,0xff,0x96,0xda,
  25144. 0x9d,0xb6,0xc3,0x46,0xec,0x11,0x4e,0x0f,
  25145. 0x5b,0x8a,0x31,0x9f,0x35,0xab,0xa6,0x24,
  25146. 0xda,0x8c,0xf6,0xed,0x4f,0xb8,0xa6,0xfb
  25147. };
  25148. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  25149. 0xc5,0xaa,0x8d,0xf4,0x3f,0x9f,0x83,0x7b,
  25150. 0xed,0xb7,0x44,0x2f,0x31,0xdc,0xb7,0xb1,
  25151. 0x66,0xd3,0x85,0x35,0x07,0x6f,0x09,0x4b,
  25152. 0x85,0xce,0x3a,0x2e,0x0b,0x44,0x58,0xf7
  25153. };
  25154. /* uncompressed test */
  25155. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  25156. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  25157. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  25158. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  25159. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  25160. };
  25161. /* compressed prefix test */
  25162. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  25163. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  25164. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  25165. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  25166. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  25167. };
  25168. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  25169. 0xf5,0xe5,0x76,0x7c,0xf1,0x53,0x31,0x95,
  25170. 0x17,0x63,0x0f,0x22,0x68,0x76,0xb8,0x6c,
  25171. 0x81,0x60,0xcc,0x58,0x3b,0xc0,0x13,0x74,
  25172. 0x4c,0x6b,0xf2,0x55,0xf5,0xcc,0x0e,0xe5
  25173. };
  25174. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  25175. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  25176. 0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  25177. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  25178. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  25179. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  25180. };
  25181. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  25182. 0x3d,0x40,0x17,0xc3,0xe8,0x43,0x89,0x5a,
  25183. 0x92,0xb7,0x0a,0xa7,0x4d,0x1b,0x7e,0xbc,
  25184. 0x9c,0x98,0x2c,0xcf,0x2e,0xc4,0x96,0x8c,
  25185. 0xc0,0xcd,0x55,0xf1,0x2a,0xf4,0x66,0x0c
  25186. };
  25187. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  25188. 0xfc,0x51,0xcd,0x8e,0x62,0x18,0xa1,0xa3,
  25189. 0x8d,0xa4,0x7e,0xd0,0x02,0x30,0xf0,0x58,
  25190. 0x08,0x16,0xed,0x13,0xba,0x33,0x03,0xac,
  25191. 0x5d,0xeb,0x91,0x15,0x48,0x90,0x80,0x25
  25192. };
  25193. /* uncompressed test */
  25194. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  25195. 0x04,0x55,0xd0,0xe0,0x9a,0x2b,0x9d,0x34,
  25196. 0x29,0x22,0x97,0xe0,0x8d,0x60,0xd0,0xf6,
  25197. 0x20,0xc5,0x13,0xd4,0x72,0x53,0x18,0x7c,
  25198. 0x24,0xb1,0x27,0x86,0xbd,0x77,0x76,0x45,
  25199. 0xce,0x1a,0x51,0x07,0xf7,0x68,0x1a,0x02,
  25200. 0xaf,0x25,0x23,0xa6,0xda,0xf3,0x72,0xe1,
  25201. 0x0e,0x3a,0x07,0x64,0xc9,0xd3,0xfe,0x4b,
  25202. 0xd5,0xb7,0x0a,0xb1,0x82,0x01,0x98,0x5a,
  25203. 0xd7
  25204. };
  25205. /* compressed prefix */
  25206. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  25207. 0x40,0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  25208. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  25209. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  25210. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  25211. };
  25212. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  25213. 0x27,0x81,0x17,0xfc,0x14,0x4c,0x72,0x34,
  25214. 0x0f,0x67,0xd0,0xf2,0x31,0x6e,0x83,0x86,
  25215. 0xce,0xff,0xbf,0x2b,0x24,0x28,0xc9,0xc5,
  25216. 0x1f,0xef,0x7c,0x59,0x7f,0x1d,0x42,0x6e
  25217. };
  25218. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  25219. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  25220. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  25221. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  25222. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  25223. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  25224. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  25225. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  25226. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  25227. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  25228. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  25229. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  25230. };
  25231. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  25232. 0x92,0xa0,0x09,0xa9,0xf0,0xd4,0xca,0xb8,
  25233. 0x72,0x0e,0x82,0x0b,0x5f,0x64,0x25,0x40,
  25234. 0xa2,0xb2,0x7b,0x54,0x16,0x50,0x3f,0x8f,
  25235. 0xb3,0x76,0x22,0x23,0xeb,0xdb,0x69,0xda,
  25236. 0x08,0x5a,0xc1,0xe4,0x3e,0x15,0x99,0x6e,
  25237. 0x45,0x8f,0x36,0x13,0xd0,0xf1,0x1d,0x8c,
  25238. 0x38,0x7b,0x2e,0xae,0xb4,0x30,0x2a,0xee,
  25239. 0xb0,0x0d,0x29,0x16,0x12,0xbb,0x0c,0x00
  25240. };
  25241. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  25242. 0x62,0x91,0xd6,0x57,0xde,0xec,0x24,0x02,
  25243. 0x48,0x27,0xe6,0x9c,0x3a,0xbe,0x01,0xa3,
  25244. 0x0c,0xe5,0x48,0xa2,0x84,0x74,0x3a,0x44,
  25245. 0x5e,0x36,0x80,0xd7,0xdb,0x5a,0xc3,0xac,
  25246. 0x18,0xff,0x9b,0x53,0x8d,0x16,0xf2,0x90,
  25247. 0xae,0x67,0xf7,0x60,0x98,0x4d,0xc6,0x59,
  25248. 0x4a,0x7c,0x15,0xe9,0x71,0x6e,0xd2,0x8d,
  25249. 0xc0,0x27,0xbe,0xce,0xea,0x1e,0xc4,0x0a
  25250. };
  25251. /* uncompressed test */
  25252. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  25253. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  25254. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  25255. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  25256. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  25257. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  25258. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  25259. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  25260. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  25261. };
  25262. /* compressed prefix */
  25263. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  25264. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  25265. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  25266. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  25267. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  25268. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  25269. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  25270. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  25271. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  25272. };
  25273. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  25274. 0x0a,0xab,0x4c,0x90,0x05,0x01,0xb3,0xe2,
  25275. 0x4d,0x7c,0xdf,0x46,0x63,0x32,0x6a,0x3a,
  25276. 0x87,0xdf,0x5e,0x48,0x43,0xb2,0xcb,0xdb,
  25277. 0x67,0xcb,0xf6,0xe4,0x60,0xfe,0xc3,0x50,
  25278. 0xaa,0x53,0x71,0xb1,0x50,0x8f,0x9f,0x45,
  25279. 0x28,0xec,0xea,0x23,0xc4,0x36,0xd9,0x4b,
  25280. 0x5e,0x8f,0xcd,0x4f,0x68,0x1e,0x30,0xa6,
  25281. 0xac,0x00,0xa9,0x70,0x4a,0x18,0x8a,0x03
  25282. };
  25283. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  25284. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = {0x0 };
  25285. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = {0x72};
  25286. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = {0xAF,0x82};
  25287. /* test of a 1024 byte long message */
  25288. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  25289. 0x08,0xb8,0xb2,0xb7,0x33,0x42,0x42,0x43,
  25290. 0x76,0x0f,0xe4,0x26,0xa4,0xb5,0x49,0x08,
  25291. 0x63,0x21,0x10,0xa6,0x6c,0x2f,0x65,0x91,
  25292. 0xea,0xbd,0x33,0x45,0xe3,0xe4,0xeb,0x98,
  25293. 0xfa,0x6e,0x26,0x4b,0xf0,0x9e,0xfe,0x12,
  25294. 0xee,0x50,0xf8,0xf5,0x4e,0x9f,0x77,0xb1,
  25295. 0xe3,0x55,0xf6,0xc5,0x05,0x44,0xe2,0x3f,
  25296. 0xb1,0x43,0x3d,0xdf,0x73,0xbe,0x84,0xd8,
  25297. 0x79,0xde,0x7c,0x00,0x46,0xdc,0x49,0x96,
  25298. 0xd9,0xe7,0x73,0xf4,0xbc,0x9e,0xfe,0x57,
  25299. 0x38,0x82,0x9a,0xdb,0x26,0xc8,0x1b,0x37,
  25300. 0xc9,0x3a,0x1b,0x27,0x0b,0x20,0x32,0x9d,
  25301. 0x65,0x86,0x75,0xfc,0x6e,0xa5,0x34,0xe0,
  25302. 0x81,0x0a,0x44,0x32,0x82,0x6b,0xf5,0x8c,
  25303. 0x94,0x1e,0xfb,0x65,0xd5,0x7a,0x33,0x8b,
  25304. 0xbd,0x2e,0x26,0x64,0x0f,0x89,0xff,0xbc,
  25305. 0x1a,0x85,0x8e,0xfc,0xb8,0x55,0x0e,0xe3,
  25306. 0xa5,0xe1,0x99,0x8b,0xd1,0x77,0xe9,0x3a,
  25307. 0x73,0x63,0xc3,0x44,0xfe,0x6b,0x19,0x9e,
  25308. 0xe5,0xd0,0x2e,0x82,0xd5,0x22,0xc4,0xfe,
  25309. 0xba,0x15,0x45,0x2f,0x80,0x28,0x8a,0x82,
  25310. 0x1a,0x57,0x91,0x16,0xec,0x6d,0xad,0x2b,
  25311. 0x3b,0x31,0x0d,0xa9,0x03,0x40,0x1a,0xa6,
  25312. 0x21,0x00,0xab,0x5d,0x1a,0x36,0x55,0x3e,
  25313. 0x06,0x20,0x3b,0x33,0x89,0x0c,0xc9,0xb8,
  25314. 0x32,0xf7,0x9e,0xf8,0x05,0x60,0xcc,0xb9,
  25315. 0xa3,0x9c,0xe7,0x67,0x96,0x7e,0xd6,0x28,
  25316. 0xc6,0xad,0x57,0x3c,0xb1,0x16,0xdb,0xef,
  25317. 0xef,0xd7,0x54,0x99,0xda,0x96,0xbd,0x68,
  25318. 0xa8,0xa9,0x7b,0x92,0x8a,0x8b,0xbc,0x10,
  25319. 0x3b,0x66,0x21,0xfc,0xde,0x2b,0xec,0xa1,
  25320. 0x23,0x1d,0x20,0x6b,0xe6,0xcd,0x9e,0xc7,
  25321. 0xaf,0xf6,0xf6,0xc9,0x4f,0xcd,0x72,0x04,
  25322. 0xed,0x34,0x55,0xc6,0x8c,0x83,0xf4,0xa4,
  25323. 0x1d,0xa4,0xaf,0x2b,0x74,0xef,0x5c,0x53,
  25324. 0xf1,0xd8,0xac,0x70,0xbd,0xcb,0x7e,0xd1,
  25325. 0x85,0xce,0x81,0xbd,0x84,0x35,0x9d,0x44,
  25326. 0x25,0x4d,0x95,0x62,0x9e,0x98,0x55,0xa9,
  25327. 0x4a,0x7c,0x19,0x58,0xd1,0xf8,0xad,0xa5,
  25328. 0xd0,0x53,0x2e,0xd8,0xa5,0xaa,0x3f,0xb2,
  25329. 0xd1,0x7b,0xa7,0x0e,0xb6,0x24,0x8e,0x59,
  25330. 0x4e,0x1a,0x22,0x97,0xac,0xbb,0xb3,0x9d,
  25331. 0x50,0x2f,0x1a,0x8c,0x6e,0xb6,0xf1,0xce,
  25332. 0x22,0xb3,0xde,0x1a,0x1f,0x40,0xcc,0x24,
  25333. 0x55,0x41,0x19,0xa8,0x31,0xa9,0xaa,0xd6,
  25334. 0x07,0x9c,0xad,0x88,0x42,0x5d,0xe6,0xbd,
  25335. 0xe1,0xa9,0x18,0x7e,0xbb,0x60,0x92,0xcf,
  25336. 0x67,0xbf,0x2b,0x13,0xfd,0x65,0xf2,0x70,
  25337. 0x88,0xd7,0x8b,0x7e,0x88,0x3c,0x87,0x59,
  25338. 0xd2,0xc4,0xf5,0xc6,0x5a,0xdb,0x75,0x53,
  25339. 0x87,0x8a,0xd5,0x75,0xf9,0xfa,0xd8,0x78,
  25340. 0xe8,0x0a,0x0c,0x9b,0xa6,0x3b,0xcb,0xcc,
  25341. 0x27,0x32,0xe6,0x94,0x85,0xbb,0xc9,0xc9,
  25342. 0x0b,0xfb,0xd6,0x24,0x81,0xd9,0x08,0x9b,
  25343. 0xec,0xcf,0x80,0xcf,0xe2,0xdf,0x16,0xa2,
  25344. 0xcf,0x65,0xbd,0x92,0xdd,0x59,0x7b,0x07,
  25345. 0x07,0xe0,0x91,0x7a,0xf4,0x8b,0xbb,0x75,
  25346. 0xfe,0xd4,0x13,0xd2,0x38,0xf5,0x55,0x5a,
  25347. 0x7a,0x56,0x9d,0x80,0xc3,0x41,0x4a,0x8d,
  25348. 0x08,0x59,0xdc,0x65,0xa4,0x61,0x28,0xba,
  25349. 0xb2,0x7a,0xf8,0x7a,0x71,0x31,0x4f,0x31,
  25350. 0x8c,0x78,0x2b,0x23,0xeb,0xfe,0x80,0x8b,
  25351. 0x82,0xb0,0xce,0x26,0x40,0x1d,0x2e,0x22,
  25352. 0xf0,0x4d,0x83,0xd1,0x25,0x5d,0xc5,0x1a,
  25353. 0xdd,0xd3,0xb7,0x5a,0x2b,0x1a,0xe0,0x78,
  25354. 0x45,0x04,0xdf,0x54,0x3a,0xf8,0x96,0x9b,
  25355. 0xe3,0xea,0x70,0x82,0xff,0x7f,0xc9,0x88,
  25356. 0x8c,0x14,0x4d,0xa2,0xaf,0x58,0x42,0x9e,
  25357. 0xc9,0x60,0x31,0xdb,0xca,0xd3,0xda,0xd9,
  25358. 0xaf,0x0d,0xcb,0xaa,0xaf,0x26,0x8c,0xb8,
  25359. 0xfc,0xff,0xea,0xd9,0x4f,0x3c,0x7c,0xa4,
  25360. 0x95,0xe0,0x56,0xa9,0xb4,0x7a,0xcd,0xb7,
  25361. 0x51,0xfb,0x73,0xe6,0x66,0xc6,0xc6,0x55,
  25362. 0xad,0xe8,0x29,0x72,0x97,0xd0,0x7a,0xd1,
  25363. 0xba,0x5e,0x43,0xf1,0xbc,0xa3,0x23,0x01,
  25364. 0x65,0x13,0x39,0xe2,0x29,0x04,0xcc,0x8c,
  25365. 0x42,0xf5,0x8c,0x30,0xc0,0x4a,0xaf,0xdb,
  25366. 0x03,0x8d,0xda,0x08,0x47,0xdd,0x98,0x8d,
  25367. 0xcd,0xa6,0xf3,0xbf,0xd1,0x5c,0x4b,0x4c,
  25368. 0x45,0x25,0x00,0x4a,0xa0,0x6e,0xef,0xf8,
  25369. 0xca,0x61,0x78,0x3a,0xac,0xec,0x57,0xfb,
  25370. 0x3d,0x1f,0x92,0xb0,0xfe,0x2f,0xd1,0xa8,
  25371. 0x5f,0x67,0x24,0x51,0x7b,0x65,0xe6,0x14,
  25372. 0xad,0x68,0x08,0xd6,0xf6,0xee,0x34,0xdf,
  25373. 0xf7,0x31,0x0f,0xdc,0x82,0xae,0xbf,0xd9,
  25374. 0x04,0xb0,0x1e,0x1d,0xc5,0x4b,0x29,0x27,
  25375. 0x09,0x4b,0x2d,0xb6,0x8d,0x6f,0x90,0x3b,
  25376. 0x68,0x40,0x1a,0xde,0xbf,0x5a,0x7e,0x08,
  25377. 0xd7,0x8f,0xf4,0xef,0x5d,0x63,0x65,0x3a,
  25378. 0x65,0x04,0x0c,0xf9,0xbf,0xd4,0xac,0xa7,
  25379. 0x98,0x4a,0x74,0xd3,0x71,0x45,0x98,0x67,
  25380. 0x80,0xfc,0x0b,0x16,0xac,0x45,0x16,0x49,
  25381. 0xde,0x61,0x88,0xa7,0xdb,0xdf,0x19,0x1f,
  25382. 0x64,0xb5,0xfc,0x5e,0x2a,0xb4,0x7b,0x57,
  25383. 0xf7,0xf7,0x27,0x6c,0xd4,0x19,0xc1,0x7a,
  25384. 0x3c,0xa8,0xe1,0xb9,0x39,0xae,0x49,0xe4,
  25385. 0x88,0xac,0xba,0x6b,0x96,0x56,0x10,0xb5,
  25386. 0x48,0x01,0x09,0xc8,0xb1,0x7b,0x80,0xe1,
  25387. 0xb7,0xb7,0x50,0xdf,0xc7,0x59,0x8d,0x5d,
  25388. 0x50,0x11,0xfd,0x2d,0xcc,0x56,0x00,0xa3,
  25389. 0x2e,0xf5,0xb5,0x2a,0x1e,0xcc,0x82,0x0e,
  25390. 0x30,0x8a,0xa3,0x42,0x72,0x1a,0xac,0x09,
  25391. 0x43,0xbf,0x66,0x86,0xb6,0x4b,0x25,0x79,
  25392. 0x37,0x65,0x04,0xcc,0xc4,0x93,0xd9,0x7e,
  25393. 0x6a,0xed,0x3f,0xb0,0xf9,0xcd,0x71,0xa4,
  25394. 0x3d,0xd4,0x97,0xf0,0x1f,0x17,0xc0,0xe2,
  25395. 0xcb,0x37,0x97,0xaa,0x2a,0x2f,0x25,0x66,
  25396. 0x56,0x16,0x8e,0x6c,0x49,0x6a,0xfc,0x5f,
  25397. 0xb9,0x32,0x46,0xf6,0xb1,0x11,0x63,0x98,
  25398. 0xa3,0x46,0xf1,0xa6,0x41,0xf3,0xb0,0x41,
  25399. 0xe9,0x89,0xf7,0x91,0x4f,0x90,0xcc,0x2c,
  25400. 0x7f,0xff,0x35,0x78,0x76,0xe5,0x06,0xb5,
  25401. 0x0d,0x33,0x4b,0xa7,0x7c,0x22,0x5b,0xc3,
  25402. 0x07,0xba,0x53,0x71,0x52,0xf3,0xf1,0x61,
  25403. 0x0e,0x4e,0xaf,0xe5,0x95,0xf6,0xd9,0xd9,
  25404. 0x0d,0x11,0xfa,0xa9,0x33,0xa1,0x5e,0xf1,
  25405. 0x36,0x95,0x46,0x86,0x8a,0x7f,0x3a,0x45,
  25406. 0xa9,0x67,0x68,0xd4,0x0f,0xd9,0xd0,0x34,
  25407. 0x12,0xc0,0x91,0xc6,0x31,0x5c,0xf4,0xfd,
  25408. 0xe7,0xcb,0x68,0x60,0x69,0x37,0x38,0x0d,
  25409. 0xb2,0xea,0xaa,0x70,0x7b,0x4c,0x41,0x85,
  25410. 0xc3,0x2e,0xdd,0xcd,0xd3,0x06,0x70,0x5e,
  25411. 0x4d,0xc1,0xff,0xc8,0x72,0xee,0xee,0x47,
  25412. 0x5a,0x64,0xdf,0xac,0x86,0xab,0xa4,0x1c,
  25413. 0x06,0x18,0x98,0x3f,0x87,0x41,0xc5,0xef,
  25414. 0x68,0xd3,0xa1,0x01,0xe8,0xa3,0xb8,0xca,
  25415. 0xc6,0x0c,0x90,0x5c,0x15,0xfc,0x91,0x08,
  25416. 0x40,0xb9,0x4c,0x00,0xa0,0xb9,0xd0
  25417. };
  25418. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  25419. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  25420. sizeof(msg2),
  25421. sizeof(msg3),
  25422. 0 /*sizeof(msg1)*/,
  25423. 0 /*sizeof(msg1)*/,
  25424. sizeof(msg4)
  25425. };
  25426. #ifndef NO_ASN
  25427. static byte privateEd25519[] = {
  25428. 0x30,0x2e,0x02,0x01,0x00,0x30,0x05,0x06,
  25429. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  25430. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  25431. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  25432. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  25433. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  25434. };
  25435. static byte badPrivateEd25519[] = {
  25436. 0x30,0x52,0x02,0x01,0x00,0x30,0x05,0x06,
  25437. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  25438. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  25439. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  25440. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  25441. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60,
  25442. 0xa1,0x22,0x04,0x21,0xd7,0x5a,0x98,0x01, /* octet len 0x20 -> 0x21 */
  25443. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  25444. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  25445. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  25446. 0xf7,0x07,0x51,0x1a,
  25447. 0x00 /* add additional bytes to make the pubkey bigger */
  25448. };
  25449. static byte publicEd25519[] = {
  25450. 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,
  25451. 0x70,0x03,0x21,0x00,0xd7,0x5a,0x98,0x01,
  25452. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  25453. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  25454. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  25455. 0xf7,0x07,0x51,0x1a
  25456. };
  25457. /* size has been altered to catch if sanity check is done */
  25458. static byte badPublicEd25519[] = {
  25459. 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,
  25460. 0x70,0x03,0x21,0x00,0xd7,0x5a,0x98,0x01,
  25461. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  25462. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  25463. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  25464. 0xf7,0x07,0x51,0x1a,
  25465. 0x00 /* add an additional byte to make the pubkey appear bigger */
  25466. };
  25467. static byte privPubEd25519[] = {
  25468. 0x30,0x50,0x02,0x01,0x00,0x30,0x05,0x06,
  25469. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  25470. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  25471. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  25472. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  25473. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60,
  25474. 0x81,0x20,0xd7,0x5a,0x98,0x01,0x82,0xb1,
  25475. 0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,0xc9,0x64,
  25476. 0x07,0x3a,0x0e,0xe1,0x72,0xf3,0xda,0xa6,
  25477. 0x23,0x25,0xaf,0x02,0x1a,0x68,0xf7,0x07,
  25478. 0x51,0x1a
  25479. };
  25480. word32 idx;
  25481. #endif /* NO_ASN */
  25482. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  25483. #if !defined(NO_ASN) && defined(HAVE_ED25519_SIGN)
  25484. ed25519_key key3;
  25485. #endif
  25486. /* create ed25519 keys */
  25487. #ifndef HAVE_FIPS
  25488. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  25489. #else
  25490. ret = wc_InitRng(&rng);
  25491. #endif
  25492. if (ret != 0)
  25493. return -11000;
  25494. wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  25495. wc_ed25519_init_ex(&key2, HEAP_HINT, devId);
  25496. #if !defined(NO_ASN) && defined(HAVE_ED25519_SIGN)
  25497. wc_ed25519_init_ex(&key3, HEAP_HINT, devId);
  25498. #endif
  25499. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  25500. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key2);
  25501. /* helper functions for signature and key size */
  25502. keySz = wc_ed25519_size(&key);
  25503. sigSz = wc_ed25519_sig_size(&key);
  25504. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  25505. defined(HAVE_ED25519_KEY_IMPORT)
  25506. for (i = 0; i < 6; i++) {
  25507. outlen = sizeof(out);
  25508. XMEMSET(out, 0, sizeof(out));
  25509. if (wc_ed25519_import_private_key(sKeys[i], ED25519_KEY_SIZE, pKeys[i],
  25510. pKeySz[i], &key) != 0)
  25511. return -11001 - i;
  25512. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key) != 0)
  25513. return -11011 - i;
  25514. if (XMEMCMP(out, sigs[i], 64))
  25515. return -11021 - i;
  25516. #if defined(HAVE_ED25519_VERIFY)
  25517. /* test verify on good msg */
  25518. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  25519. &key) != 0 || verify != 1)
  25520. return -11031 - i;
  25521. #ifdef WOLFSSL_ED25519_STREAMING_VERIFY
  25522. /* test verify on good msg using streaming interface directly */
  25523. if (wc_ed25519_verify_msg_init(out, outlen,
  25524. &key, (byte)Ed25519, NULL, 0) != 0)
  25525. return -11211 - i;
  25526. for (j = 0; j < msgSz[i]; j += i) {
  25527. if (wc_ed25519_verify_msg_update(msgs[i] + j, MIN(i, msgSz[i] - j), &key) != 0)
  25528. return -11221 - i;
  25529. }
  25530. if (wc_ed25519_verify_msg_final(out, outlen, &verify,
  25531. &key) != 0 || verify != 1)
  25532. return -11231 - i;
  25533. #endif /* WOLFSSL_ED25519_STREAMING_VERIFY */
  25534. /* test verify on bad msg */
  25535. out[outlen-1] = out[outlen-1] + 1;
  25536. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  25537. &key) == 0 || verify == 1)
  25538. return -11041 - i;
  25539. #endif /* HAVE_ED25519_VERIFY */
  25540. /* test api for import/exporting keys */
  25541. exportPSz = sizeof(exportPKey);
  25542. exportSSz = sizeof(exportSKey);
  25543. if (wc_ed25519_export_public(&key, exportPKey, &exportPSz) != 0)
  25544. return -11051 - i;
  25545. if (wc_ed25519_import_public_ex(exportPKey, exportPSz, &key2, 1) != 0)
  25546. return -11061 - i;
  25547. if (wc_ed25519_export_private_only(&key, exportSKey, &exportSSz) != 0)
  25548. return -11071 - i;
  25549. if (wc_ed25519_import_private_key(exportSKey, exportSSz,
  25550. exportPKey, exportPSz, &key2) != 0)
  25551. return -11081 - i;
  25552. /* clear "out" buffer and test sign with imported keys */
  25553. outlen = sizeof(out);
  25554. XMEMSET(out, 0, sizeof(out));
  25555. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key2) != 0)
  25556. return -11091 - i;
  25557. #if defined(HAVE_ED25519_VERIFY)
  25558. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  25559. &key2) != 0 || verify != 1)
  25560. return -11101 - i;
  25561. if (XMEMCMP(out, sigs[i], 64))
  25562. return -11111 - i;
  25563. #endif /* HAVE_ED25519_VERIFY */
  25564. }
  25565. ret = ed25519ctx_test();
  25566. if (ret != 0)
  25567. return ret;
  25568. ret = ed25519ph_test();
  25569. if (ret != 0)
  25570. return ret;
  25571. #ifndef NO_ASN
  25572. /* Try ASN.1 encoded private-only key and public key. */
  25573. idx = 0;
  25574. if (wc_Ed25519PrivateKeyDecode(privateEd25519, &idx, &key3,
  25575. sizeof(privateEd25519)) != 0)
  25576. return -11121;
  25577. idx = 0;
  25578. if (wc_Ed25519PrivateKeyDecode(badPrivateEd25519, &idx, &key3,
  25579. sizeof(badPrivateEd25519)) == 0)
  25580. return -11122;
  25581. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3)
  25582. != BAD_FUNC_ARG)
  25583. return -11131;
  25584. /* try with a buffer size that is too large */
  25585. idx = 0;
  25586. if (wc_Ed25519PublicKeyDecode(badPublicEd25519, &idx, &key3,
  25587. sizeof(badPublicEd25519)) == 0)
  25588. return -11140;
  25589. idx = 0;
  25590. if (wc_Ed25519PublicKeyDecode(publicEd25519, &idx, &key3,
  25591. sizeof(publicEd25519)) != 0)
  25592. return -11141;
  25593. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3) != 0)
  25594. return -11151;
  25595. if (XMEMCMP(out, sigs[0], 64))
  25596. return -11161;
  25597. #if defined(HAVE_ED25519_VERIFY)
  25598. /* test verify on good msg */
  25599. if (wc_ed25519_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, &key3)
  25600. != 0 || verify != 1)
  25601. return -11171;
  25602. #endif /* HAVE_ED25519_VERIFY */
  25603. wc_ed25519_free(&key3);
  25604. wc_ed25519_init(&key3);
  25605. idx = 0;
  25606. if (wc_Ed25519PrivateKeyDecode(privPubEd25519, &idx, &key3,
  25607. sizeof(privPubEd25519)) != 0)
  25608. return -11181;
  25609. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3) != 0)
  25610. return -11191;
  25611. if (XMEMCMP(out, sigs[0], 64))
  25612. return -11201;
  25613. wc_ed25519_free(&key3);
  25614. #endif /* NO_ASN */
  25615. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  25616. /* clean up keys when done */
  25617. wc_ed25519_free(&key);
  25618. wc_ed25519_free(&key2);
  25619. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  25620. wc_FreeRng(&rng);
  25621. #endif
  25622. /* hush warnings of unused keySz and sigSz */
  25623. (void)keySz;
  25624. (void)sigSz;
  25625. #ifdef WOLFSSL_TEST_CERT
  25626. ret = ed25519_test_cert();
  25627. if (ret < 0)
  25628. return ret;
  25629. #ifdef WOLFSSL_CERT_GEN
  25630. ret = ed25519_test_make_cert();
  25631. if (ret < 0)
  25632. return ret;
  25633. #endif /* WOLFSSL_CERT_GEN */
  25634. #endif /* WOLFSSL_TEST_CERT */
  25635. return 0;
  25636. }
  25637. #endif /* HAVE_ED25519 */
  25638. #ifdef HAVE_CURVE448
  25639. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  25640. defined(HAVE_CURVE448_KEY_IMPORT)
  25641. /* Test the wc_curve448_check_public API.
  25642. *
  25643. * returns 0 on success and -ve on failure.
  25644. */
  25645. static int curve448_check_public_test(void)
  25646. {
  25647. /* Little-endian values that will fail */
  25648. byte fail_le[][CURVE448_KEY_SIZE] = {
  25649. {
  25650. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25651. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25652. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25653. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25654. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25655. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25656. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  25657. },
  25658. {
  25659. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25660. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25661. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25662. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25663. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25664. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25665. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  25666. },
  25667. };
  25668. /* Big-endian values that will fail */
  25669. byte fail_be[][CURVE448_KEY_SIZE] = {
  25670. {
  25671. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25672. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25673. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25674. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25675. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25676. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25677. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  25678. },
  25679. {
  25680. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25681. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25682. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25683. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25684. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25685. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25686. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  25687. },
  25688. };
  25689. /* Good or valid public value */
  25690. byte good[CURVE448_KEY_SIZE] = {
  25691. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25692. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25693. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25694. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25695. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25696. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25697. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  25698. };
  25699. int i;
  25700. /* Parameter checks */
  25701. /* NULL pointer */
  25702. if (wc_curve448_check_public(NULL, 0, EC448_LITTLE_ENDIAN) !=
  25703. BAD_FUNC_ARG) {
  25704. return -11300;
  25705. }
  25706. if (wc_curve448_check_public(NULL, 0, EC448_BIG_ENDIAN) != BAD_FUNC_ARG) {
  25707. return -11301;
  25708. }
  25709. /* Length of 0 treated differently to other invalid lengths for TLS */
  25710. if (wc_curve448_check_public(good, 0, EC448_LITTLE_ENDIAN) != BUFFER_E)
  25711. return -11302;
  25712. if (wc_curve448_check_public(good, 0, EC448_BIG_ENDIAN) != BUFFER_E)
  25713. return -11303;
  25714. /* Length not CURVE448_KEY_SIZE */
  25715. for (i = 1; i < CURVE448_KEY_SIZE + 2; i++) {
  25716. if (i == CURVE448_KEY_SIZE)
  25717. continue;
  25718. if (wc_curve448_check_public(good, i, EC448_LITTLE_ENDIAN) !=
  25719. ECC_BAD_ARG_E) {
  25720. return -11304 - i;
  25721. }
  25722. if (wc_curve448_check_public(good, i, EC448_BIG_ENDIAN) !=
  25723. ECC_BAD_ARG_E) {
  25724. return -11314 - i;
  25725. }
  25726. }
  25727. /* Little-endian fail cases */
  25728. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  25729. if (wc_curve448_check_public(fail_le[i], CURVE448_KEY_SIZE,
  25730. EC448_LITTLE_ENDIAN) == 0) {
  25731. return -11324 - i;
  25732. }
  25733. }
  25734. /* Big-endian fail cases */
  25735. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  25736. if (wc_curve448_check_public(fail_be[i], CURVE448_KEY_SIZE,
  25737. EC448_BIG_ENDIAN) == 0) {
  25738. return -11334 - i;
  25739. }
  25740. }
  25741. /* Check a valid public value works! */
  25742. if (wc_curve448_check_public(good, CURVE448_KEY_SIZE,
  25743. EC448_LITTLE_ENDIAN) != 0) {
  25744. return -11344;
  25745. }
  25746. if (wc_curve448_check_public(good, CURVE448_KEY_SIZE,
  25747. EC448_BIG_ENDIAN) != 0) {
  25748. return -11345;
  25749. }
  25750. return 0;
  25751. }
  25752. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  25753. WOLFSSL_TEST_SUBROUTINE int curve448_test(void)
  25754. {
  25755. WC_RNG rng;
  25756. int ret;
  25757. #ifdef HAVE_CURVE448_SHARED_SECRET
  25758. byte sharedA[CURVE448_KEY_SIZE];
  25759. byte sharedB[CURVE448_KEY_SIZE];
  25760. word32 y;
  25761. #endif
  25762. #ifdef HAVE_CURVE448_KEY_EXPORT
  25763. byte exportBuf[CURVE448_KEY_SIZE];
  25764. #endif
  25765. word32 x;
  25766. curve448_key userA, userB, pubKey;
  25767. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  25768. defined(HAVE_CURVE448_KEY_IMPORT)
  25769. /* test vectors from
  25770. https://www.rfc-editor.org/rfc/rfc7748.html
  25771. */
  25772. /* secret key for party a */
  25773. byte sa[] = {
  25774. 0x6b, 0x72, 0x98, 0xa5, 0xc0, 0xd8, 0xc2, 0x9a,
  25775. 0x1d, 0xab, 0x27, 0xf1, 0xa6, 0x82, 0x63, 0x00,
  25776. 0x91, 0x73, 0x89, 0x44, 0x97, 0x41, 0xa9, 0x74,
  25777. 0xf5, 0xba, 0xc9, 0xd9, 0x8d, 0xc2, 0x98, 0xd4,
  25778. 0x65, 0x55, 0xbc, 0xe8, 0xba, 0xe8, 0x9e, 0xee,
  25779. 0xd4, 0x00, 0x58, 0x4b, 0xb0, 0x46, 0xcf, 0x75,
  25780. 0x57, 0x9f, 0x51, 0xd1, 0x25, 0x49, 0x8f, 0x9a,
  25781. };
  25782. /* public key for party a */
  25783. byte pa[] = {
  25784. 0xa0, 0x1f, 0xc4, 0x32, 0xe5, 0x80, 0x7f, 0x17,
  25785. 0x53, 0x0d, 0x12, 0x88, 0xda, 0x12, 0x5b, 0x0c,
  25786. 0xd4, 0x53, 0xd9, 0x41, 0x72, 0x64, 0x36, 0xc8,
  25787. 0xbb, 0xd9, 0xc5, 0x22, 0x2c, 0x3d, 0xa7, 0xfa,
  25788. 0x63, 0x9c, 0xe0, 0x3d, 0xb8, 0xd2, 0x3b, 0x27,
  25789. 0x4a, 0x07, 0x21, 0xa1, 0xae, 0xd5, 0x22, 0x7d,
  25790. 0xe6, 0xe3, 0xb7, 0x31, 0xcc, 0xf7, 0x08, 0x9b,
  25791. };
  25792. /* secret key for party b */
  25793. byte sb[] = {
  25794. 0x2d, 0x99, 0x73, 0x51, 0xb6, 0x10, 0x6f, 0x36,
  25795. 0xb0, 0xd1, 0x09, 0x1b, 0x92, 0x9c, 0x4c, 0x37,
  25796. 0x21, 0x3e, 0x0d, 0x2b, 0x97, 0xe8, 0x5e, 0xbb,
  25797. 0x20, 0xc1, 0x27, 0x69, 0x1d, 0x0d, 0xad, 0x8f,
  25798. 0x1d, 0x81, 0x75, 0xb0, 0x72, 0x37, 0x45, 0xe6,
  25799. 0x39, 0xa3, 0xcb, 0x70, 0x44, 0x29, 0x0b, 0x99,
  25800. 0xe0, 0xe2, 0xa0, 0xc2, 0x7a, 0x6a, 0x30, 0x1c,
  25801. };
  25802. /* public key for party b */
  25803. byte pb[] = {
  25804. 0x09, 0x36, 0xf3, 0x7b, 0xc6, 0xc1, 0xbd, 0x07,
  25805. 0xae, 0x3d, 0xec, 0x7a, 0xb5, 0xdc, 0x06, 0xa7,
  25806. 0x3c, 0xa1, 0x32, 0x42, 0xfb, 0x34, 0x3e, 0xfc,
  25807. 0x72, 0xb9, 0xd8, 0x27, 0x30, 0xb4, 0x45, 0xf3,
  25808. 0xd4, 0xb0, 0xbd, 0x07, 0x71, 0x62, 0xa4, 0x6d,
  25809. 0xcf, 0xec, 0x6f, 0x9b, 0x59, 0x0b, 0xfc, 0xbc,
  25810. 0xf5, 0x20, 0xcd, 0xb0, 0x29, 0xa8, 0xb7, 0x3e,
  25811. };
  25812. /* expected shared key */
  25813. byte ss[] = {
  25814. 0x9d, 0x87, 0x4a, 0x51, 0x37, 0x50, 0x9a, 0x44,
  25815. 0x9a, 0xd5, 0x85, 0x30, 0x40, 0x24, 0x1c, 0x52,
  25816. 0x36, 0x39, 0x54, 0x35, 0xc3, 0x64, 0x24, 0xfd,
  25817. 0x56, 0x0b, 0x0c, 0xb6, 0x2b, 0x28, 0x1d, 0x28,
  25818. 0x52, 0x75, 0xa7, 0x40, 0xce, 0x32, 0xa2, 0x2d,
  25819. 0xd1, 0x74, 0x0f, 0x4a, 0xa9, 0x16, 0x1c, 0xec,
  25820. 0x95, 0xcc, 0xc6, 0x1a, 0x18, 0xf4, 0xff, 0x07,
  25821. };
  25822. #endif /* HAVE_CURVE448_SHARED_SECRET */
  25823. (void)x;
  25824. #ifndef HAVE_FIPS
  25825. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  25826. #else
  25827. ret = wc_InitRng(&rng);
  25828. #endif
  25829. if (ret != 0)
  25830. return -11400;
  25831. wc_curve448_init(&userA);
  25832. wc_curve448_init(&userB);
  25833. wc_curve448_init(&pubKey);
  25834. /* make curve448 keys */
  25835. if (wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userA) != 0)
  25836. return -11401;
  25837. if (wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userB) != 0)
  25838. return -11402;
  25839. #ifdef HAVE_CURVE448_SHARED_SECRET
  25840. /* find shared secret key */
  25841. x = sizeof(sharedA);
  25842. if (wc_curve448_shared_secret(&userA, &userB, sharedA, &x) != 0)
  25843. return -11403;
  25844. y = sizeof(sharedB);
  25845. if (wc_curve448_shared_secret(&userB, &userA, sharedB, &y) != 0)
  25846. return -11404;
  25847. /* compare shared secret keys to test they are the same */
  25848. if (y != x)
  25849. return -11405;
  25850. if (XMEMCMP(sharedA, sharedB, x))
  25851. return -11406;
  25852. #endif
  25853. #ifdef HAVE_CURVE448_KEY_EXPORT
  25854. /* export a public key and import it for another user */
  25855. x = sizeof(exportBuf);
  25856. if (wc_curve448_export_public(&userA, exportBuf, &x) != 0)
  25857. return -11407;
  25858. #ifdef HAVE_CURVE448_KEY_IMPORT
  25859. if (wc_curve448_import_public(exportBuf, x, &pubKey) != 0)
  25860. return -11408;
  25861. #endif
  25862. #endif
  25863. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  25864. defined(HAVE_CURVE448_KEY_IMPORT)
  25865. /* test shared key after importing a public key */
  25866. XMEMSET(sharedB, 0, sizeof(sharedB));
  25867. y = sizeof(sharedB);
  25868. if (wc_curve448_shared_secret(&userB, &pubKey, sharedB, &y) != 0)
  25869. return -11409;
  25870. if (XMEMCMP(sharedA, sharedB, y))
  25871. return -11410;
  25872. /* import RFC test vectors and compare shared key */
  25873. if (wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  25874. != 0)
  25875. return -11411;
  25876. if (wc_curve448_import_private_raw(sb, sizeof(sb), pb, sizeof(pb), &userB)
  25877. != 0)
  25878. return -11412;
  25879. /* test against known test vector */
  25880. XMEMSET(sharedB, 0, sizeof(sharedB));
  25881. y = sizeof(sharedB);
  25882. if (wc_curve448_shared_secret(&userA, &userB, sharedB, &y) != 0)
  25883. return -11413;
  25884. if (XMEMCMP(ss, sharedB, y))
  25885. return -11414;
  25886. /* test swapping roles of keys and generating same shared key */
  25887. XMEMSET(sharedB, 0, sizeof(sharedB));
  25888. y = sizeof(sharedB);
  25889. if (wc_curve448_shared_secret(&userB, &userA, sharedB, &y) != 0)
  25890. return -11415;
  25891. if (XMEMCMP(ss, sharedB, y))
  25892. return -11416;
  25893. /* test with 1 generated key and 1 from known test vector */
  25894. if (wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  25895. != 0)
  25896. return -11417;
  25897. if (wc_curve448_make_key(&rng, 56, &userB) != 0)
  25898. return -11418;
  25899. x = sizeof(sharedA);
  25900. if (wc_curve448_shared_secret(&userA, &userB, sharedA, &x) != 0)
  25901. return -11419;
  25902. y = sizeof(sharedB);
  25903. if (wc_curve448_shared_secret(&userB, &userA, sharedB, &y) != 0)
  25904. return -11420;
  25905. /* compare shared secret keys to test they are the same */
  25906. if (y != x)
  25907. return -11421;
  25908. if (XMEMCMP(sharedA, sharedB, x))
  25909. return -11422;
  25910. ret = curve448_check_public_test();
  25911. if (ret != 0)
  25912. return ret;
  25913. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  25914. /* clean up keys when done */
  25915. wc_curve448_free(&pubKey);
  25916. wc_curve448_free(&userB);
  25917. wc_curve448_free(&userA);
  25918. wc_FreeRng(&rng);
  25919. return 0;
  25920. }
  25921. #endif /* HAVE_CURVE448 */
  25922. #ifdef HAVE_ED448
  25923. #ifdef WOLFSSL_TEST_CERT
  25924. static int ed448_test_cert(void)
  25925. {
  25926. DecodedCert cert[2];
  25927. DecodedCert* serverCert = NULL;
  25928. DecodedCert* caCert = NULL;
  25929. #ifdef HAVE_ED448_VERIFY
  25930. ed448_key key;
  25931. ed448_key* pubKey = NULL;
  25932. int verify;
  25933. #endif /* HAVE_ED448_VERIFY */
  25934. int ret;
  25935. byte* tmp;
  25936. size_t bytes;
  25937. XFILE file;
  25938. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25939. if (tmp == NULL) {
  25940. ERROR_OUT(-11430, done);
  25941. }
  25942. #ifdef USE_CERT_BUFFERS_256
  25943. XMEMCPY(tmp, ca_ed448_cert, sizeof_ca_ed448_cert);
  25944. bytes = sizeof_ca_ed448_cert;
  25945. #elif !defined(NO_FILESYSTEM)
  25946. file = XFOPEN(caEd448Cert, "rb");
  25947. if (file == NULL) {
  25948. ERROR_OUT(-11431, done);
  25949. }
  25950. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  25951. XFCLOSE(file);
  25952. #else
  25953. /* No certificate to use. */
  25954. ERROR_OUT(-11432, done);
  25955. #endif
  25956. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  25957. caCert = &cert[0];
  25958. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  25959. if (ret != 0) {
  25960. ERROR_OUT(-11433, done);
  25961. }
  25962. #ifdef USE_CERT_BUFFERS_256
  25963. XMEMCPY(tmp, server_ed448_cert, sizeof_server_ed448_cert);
  25964. bytes = sizeof_server_ed448_cert;
  25965. #elif !defined(NO_FILESYSTEM)
  25966. file = XFOPEN(serverEd448Cert, "rb");
  25967. if (file == NULL) {
  25968. ERROR_OUT(-11434, done);
  25969. }
  25970. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  25971. XFCLOSE(file);
  25972. #else
  25973. /* No certificate to use. */
  25974. ERROR_OUT(-11435, done);
  25975. #endif
  25976. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  25977. serverCert = &cert[1];
  25978. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  25979. if (ret != 0) {
  25980. ERROR_OUT(-11436, done);
  25981. }
  25982. #ifdef HAVE_ED448_VERIFY
  25983. ret = wc_ed448_init(&key);
  25984. if (ret < 0) {
  25985. ERROR_OUT(-11437, done);
  25986. }
  25987. pubKey = &key;
  25988. ret = wc_ed448_import_public(caCert->publicKey, caCert->pubKeySize, pubKey);
  25989. if (ret < 0) {
  25990. ERROR_OUT(-11438, done);
  25991. }
  25992. if (wc_ed448_verify_msg(serverCert->signature, serverCert->sigLength,
  25993. serverCert->source + serverCert->certBegin,
  25994. serverCert->sigIndex - serverCert->certBegin,
  25995. &verify, pubKey, NULL, 0) < 0 || verify != 1) {
  25996. ERROR_OUT(-11439, done);
  25997. }
  25998. #endif /* HAVE_ED448_VERIFY */
  25999. done:
  26000. if (tmp != NULL)
  26001. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26002. #ifdef HAVE_ED448_VERIFY
  26003. wc_ed448_free(pubKey);
  26004. #endif /* HAVE_ED448_VERIFY */
  26005. if (caCert != NULL)
  26006. FreeDecodedCert(caCert);
  26007. if (serverCert != NULL)
  26008. FreeDecodedCert(serverCert);
  26009. return ret;
  26010. }
  26011. static int ed448_test_make_cert(void)
  26012. {
  26013. WC_RNG rng;
  26014. Cert cert;
  26015. DecodedCert decode;
  26016. ed448_key key;
  26017. ed448_key* privKey = NULL;
  26018. int ret = 0;
  26019. byte* tmp = NULL;
  26020. wc_InitCert_ex(&cert, HEAP_HINT, devId);
  26021. #ifndef HAVE_FIPS
  26022. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  26023. #else
  26024. ret = wc_InitRng(&rng);
  26025. #endif
  26026. if (ret != 0)
  26027. return -11450;
  26028. wc_ed448_init(&key);
  26029. privKey = &key;
  26030. wc_ed448_make_key(&rng, ED448_KEY_SIZE, privKey);
  26031. cert.daysValid = 365 * 2;
  26032. cert.selfSigned = 1;
  26033. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  26034. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  26035. cert.isCA = 0;
  26036. #ifdef WOLFSSL_CERT_EXT
  26037. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  26038. if (ret < 0) {
  26039. ERROR_OUT(-11451, done);
  26040. }
  26041. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  26042. if (ret < 0) {
  26043. ERROR_OUT(-11452, done);
  26044. }
  26045. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  26046. if (ret < 0) {
  26047. ERROR_OUT(-11453, done);
  26048. }
  26049. #endif
  26050. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26051. if (tmp == NULL) {
  26052. ERROR_OUT(-11454, done);
  26053. }
  26054. cert.sigType = CTC_ED448;
  26055. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED448_TYPE, privKey, &rng);
  26056. if (ret < 0) {
  26057. ERROR_OUT(-11455, done);
  26058. }
  26059. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF, ED448_TYPE,
  26060. privKey, &rng);
  26061. if (ret < 0) {
  26062. ERROR_OUT(-11456, done);
  26063. }
  26064. InitDecodedCert(&decode, tmp, ret, HEAP_HINT);
  26065. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  26066. FreeDecodedCert(&decode);
  26067. if (ret != 0) {
  26068. ERROR_OUT(-11457, done);
  26069. }
  26070. done:
  26071. if (tmp != NULL)
  26072. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26073. wc_ed448_free(privKey);
  26074. wc_FreeRng(&rng);
  26075. return ret;
  26076. }
  26077. #endif /* WOLFSSL_TEST_CERT */
  26078. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  26079. defined(HAVE_ED448_KEY_IMPORT)
  26080. static int ed448_ctx_test(void)
  26081. {
  26082. byte out[ED448_SIG_SIZE];
  26083. word32 outlen;
  26084. #ifdef HAVE_ED448_VERIFY
  26085. int verify;
  26086. #endif /* HAVE_ED448_VERIFY */
  26087. ed448_key key;
  26088. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  26089. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  26090. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  26091. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  26092. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  26093. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  26094. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  26095. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  26096. 0x4e
  26097. };
  26098. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  26099. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  26100. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  26101. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  26102. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  26103. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  26104. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  26105. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  26106. 0x80
  26107. };
  26108. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx[] = {
  26109. 0xd4, 0xf8, 0xf6, 0x13, 0x17, 0x70, 0xdd, 0x46,
  26110. 0xf4, 0x08, 0x67, 0xd6, 0xfd, 0x5d, 0x50, 0x55,
  26111. 0xde, 0x43, 0x54, 0x1f, 0x8c, 0x5e, 0x35, 0xab,
  26112. 0xbc, 0xd0, 0x01, 0xb3, 0x2a, 0x89, 0xf7, 0xd2,
  26113. 0x15, 0x1f, 0x76, 0x47, 0xf1, 0x1d, 0x8c, 0xa2,
  26114. 0xae, 0x27, 0x9f, 0xb8, 0x42, 0xd6, 0x07, 0x21,
  26115. 0x7f, 0xce, 0x6e, 0x04, 0x2f, 0x68, 0x15, 0xea,
  26116. 0x00, 0x0c, 0x85, 0x74, 0x1d, 0xe5, 0xc8, 0xda,
  26117. 0x11, 0x44, 0xa6, 0xa1, 0xab, 0xa7, 0xf9, 0x6d,
  26118. 0xe4, 0x25, 0x05, 0xd7, 0xa7, 0x29, 0x85, 0x24,
  26119. 0xfd, 0xa5, 0x38, 0xfc, 0xcb, 0xbb, 0x75, 0x4f,
  26120. 0x57, 0x8c, 0x1c, 0xad, 0x10, 0xd5, 0x4d, 0x0d,
  26121. 0x54, 0x28, 0x40, 0x7e, 0x85, 0xdc, 0xbc, 0x98,
  26122. 0xa4, 0x91, 0x55, 0xc1, 0x37, 0x64, 0xe6, 0x6c,
  26123. 0x3c, 0x00
  26124. };
  26125. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  26126. 0x03
  26127. };
  26128. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  26129. 0x66,0x6f,0x6f
  26130. };
  26131. outlen = sizeof(out);
  26132. XMEMSET(out, 0, sizeof(out));
  26133. if (wc_ed448_init_ex(&key, HEAP_HINT, devId) != 0)
  26134. return -11500;
  26135. if (wc_ed448_import_private_key(sKeyCtx, ED448_KEY_SIZE, pKeyCtx,
  26136. sizeof(pKeyCtx), &key) != 0)
  26137. return -11501;
  26138. if (wc_ed448_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  26139. contextCtx, sizeof(contextCtx)) != 0)
  26140. return -11502;
  26141. if (XMEMCMP(out, sigCtx, sizeof(sigCtx)))
  26142. return -11503;
  26143. #if defined(HAVE_ED448_VERIFY)
  26144. /* test verify on good msg */
  26145. if (wc_ed448_verify_msg(out, outlen, msgCtx, sizeof(msgCtx), &verify, &key,
  26146. contextCtx, sizeof(contextCtx)) != 0 || verify != 1)
  26147. return -11504;
  26148. #endif
  26149. wc_ed448_free(&key);
  26150. return 0;
  26151. }
  26152. static int ed448ph_test(void)
  26153. {
  26154. byte out[ED448_SIG_SIZE];
  26155. word32 outlen;
  26156. #ifdef HAVE_ED448_VERIFY
  26157. int verify;
  26158. #endif /* HAVE_ED448_VERIFY */
  26159. ed448_key key;
  26160. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  26161. 0x83, 0x3f, 0xe6, 0x24, 0x09, 0x23, 0x7b, 0x9d,
  26162. 0x62, 0xec, 0x77, 0x58, 0x75, 0x20, 0x91, 0x1e,
  26163. 0x9a, 0x75, 0x9c, 0xec, 0x1d, 0x19, 0x75, 0x5b,
  26164. 0x7d, 0xa9, 0x01, 0xb9, 0x6d, 0xca, 0x3d, 0x42,
  26165. 0xef, 0x78, 0x22, 0xe0, 0xd5, 0x10, 0x41, 0x27,
  26166. 0xdc, 0x05, 0xd6, 0xdb, 0xef, 0xde, 0x69, 0xe3,
  26167. 0xab, 0x2c, 0xec, 0x7c, 0x86, 0x7c, 0x6e, 0x2c,
  26168. 0x49
  26169. };
  26170. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  26171. 0x25, 0x9b, 0x71, 0xc1, 0x9f, 0x83, 0xef, 0x77,
  26172. 0xa7, 0xab, 0xd2, 0x65, 0x24, 0xcb, 0xdb, 0x31,
  26173. 0x61, 0xb5, 0x90, 0xa4, 0x8f, 0x7d, 0x17, 0xde,
  26174. 0x3e, 0xe0, 0xba, 0x9c, 0x52, 0xbe, 0xb7, 0x43,
  26175. 0xc0, 0x94, 0x28, 0xa1, 0x31, 0xd6, 0xb1, 0xb5,
  26176. 0x73, 0x03, 0xd9, 0x0d, 0x81, 0x32, 0xc2, 0x76,
  26177. 0xd5, 0xed, 0x3d, 0x5d, 0x01, 0xc0, 0xf5, 0x38,
  26178. 0x80
  26179. };
  26180. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  26181. 0x82, 0x2f, 0x69, 0x01, 0xf7, 0x48, 0x0f, 0x3d,
  26182. 0x5f, 0x56, 0x2c, 0x59, 0x29, 0x94, 0xd9, 0x69,
  26183. 0x36, 0x02, 0x87, 0x56, 0x14, 0x48, 0x32, 0x56,
  26184. 0x50, 0x56, 0x00, 0xbb, 0xc2, 0x81, 0xae, 0x38,
  26185. 0x1f, 0x54, 0xd6, 0xbc, 0xe2, 0xea, 0x91, 0x15,
  26186. 0x74, 0x93, 0x2f, 0x52, 0xa4, 0xe6, 0xca, 0xdd,
  26187. 0x78, 0x76, 0x93, 0x75, 0xec, 0x3f, 0xfd, 0x1b,
  26188. 0x80, 0x1a, 0x0d, 0x9b, 0x3f, 0x40, 0x30, 0xcd,
  26189. 0x43, 0x39, 0x64, 0xb6, 0x45, 0x7e, 0xa3, 0x94,
  26190. 0x76, 0x51, 0x12, 0x14, 0xf9, 0x74, 0x69, 0xb5,
  26191. 0x7d, 0xd3, 0x2d, 0xbc, 0x56, 0x0a, 0x9a, 0x94,
  26192. 0xd0, 0x0b, 0xff, 0x07, 0x62, 0x04, 0x64, 0xa3,
  26193. 0xad, 0x20, 0x3d, 0xf7, 0xdc, 0x7c, 0xe3, 0x60,
  26194. 0xc3, 0xcd, 0x36, 0x96, 0xd9, 0xd9, 0xfa, 0xb9,
  26195. 0x0f, 0x00
  26196. };
  26197. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  26198. 0xc3, 0x22, 0x99, 0xd4, 0x6e, 0xc8, 0xff, 0x02,
  26199. 0xb5, 0x45, 0x40, 0x98, 0x28, 0x14, 0xdc, 0xe9,
  26200. 0xa0, 0x58, 0x12, 0xf8, 0x19, 0x62, 0xb6, 0x49,
  26201. 0xd5, 0x28, 0x09, 0x59, 0x16, 0xa2, 0xaa, 0x48,
  26202. 0x10, 0x65, 0xb1, 0x58, 0x04, 0x23, 0xef, 0x92,
  26203. 0x7e, 0xcf, 0x0a, 0xf5, 0x88, 0x8f, 0x90, 0xda,
  26204. 0x0f, 0x6a, 0x9a, 0x85, 0xad, 0x5d, 0xc3, 0xf2,
  26205. 0x80, 0xd9, 0x12, 0x24, 0xba, 0x99, 0x11, 0xa3,
  26206. 0x65, 0x3d, 0x00, 0xe4, 0x84, 0xe2, 0xce, 0x23,
  26207. 0x25, 0x21, 0x48, 0x1c, 0x86, 0x58, 0xdf, 0x30,
  26208. 0x4b, 0xb7, 0x74, 0x5a, 0x73, 0x51, 0x4c, 0xdb,
  26209. 0x9b, 0xf3, 0xe1, 0x57, 0x84, 0xab, 0x71, 0x28,
  26210. 0x4f, 0x8d, 0x07, 0x04, 0xa6, 0x08, 0xc5, 0x4a,
  26211. 0x6b, 0x62, 0xd9, 0x7b, 0xeb, 0x51, 0x1d, 0x13,
  26212. 0x21, 0x00
  26213. };
  26214. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  26215. 0x61,0x62,0x63
  26216. };
  26217. /* SHA-512 hash of msgPh */
  26218. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  26219. 0x48, 0x33, 0x66, 0x60, 0x13, 0x60, 0xa8, 0x77,
  26220. 0x1c, 0x68, 0x63, 0x08, 0x0c, 0xc4, 0x11, 0x4d,
  26221. 0x8d, 0xb4, 0x45, 0x30, 0xf8, 0xf1, 0xe1, 0xee,
  26222. 0x4f, 0x94, 0xea, 0x37, 0xe7, 0x8b, 0x57, 0x39,
  26223. 0xd5, 0xa1, 0x5b, 0xef, 0x18, 0x6a, 0x53, 0x86,
  26224. 0xc7, 0x57, 0x44, 0xc0, 0x52, 0x7e, 0x1f, 0xaa,
  26225. 0x9f, 0x87, 0x26, 0xe4, 0x62, 0xa1, 0x2a, 0x4f,
  26226. 0xeb, 0x06, 0xbd, 0x88, 0x01, 0xe7, 0x51, 0xe4
  26227. };
  26228. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  26229. 0x66,0x6f,0x6f
  26230. };
  26231. outlen = sizeof(out);
  26232. XMEMSET(out, 0, sizeof(out));
  26233. if (wc_ed448_init_ex(&key, HEAP_HINT, devId) != 0)
  26234. return -11600;
  26235. if (wc_ed448_import_private_key(sKeyPh, ED448_KEY_SIZE, pKeyPh,
  26236. sizeof(pKeyPh), &key) != 0) {
  26237. return -11601;
  26238. }
  26239. if (wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key, NULL,
  26240. 0) != 0) {
  26241. return -11602;
  26242. }
  26243. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  26244. return -11603;
  26245. #if defined(HAVE_ED448_VERIFY)
  26246. /* test verify on good msg */
  26247. if (wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  26248. NULL, 0) != 0 || verify != 1) {
  26249. return -11604;
  26250. }
  26251. #endif
  26252. if (wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  26253. contextPh2, sizeof(contextPh2)) != 0) {
  26254. return -11605;
  26255. }
  26256. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  26257. return -11606;
  26258. #if defined(HAVE_ED448_VERIFY)
  26259. /* test verify on good msg */
  26260. if (wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  26261. contextPh2, sizeof(contextPh2)) != 0 ||
  26262. verify != 1) {
  26263. return -11607;
  26264. }
  26265. #endif
  26266. if (wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key, NULL,
  26267. 0) != 0) {
  26268. return -11608;
  26269. }
  26270. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  26271. return -11609;
  26272. #if defined(HAVE_ED448_VERIFY)
  26273. if (wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  26274. &key, NULL, 0) != 0 || verify != 1) {
  26275. return -11610;
  26276. }
  26277. #endif
  26278. if (wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  26279. contextPh2, sizeof(contextPh2)) != 0) {
  26280. return -11611;
  26281. }
  26282. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  26283. return -11612;
  26284. #if defined(HAVE_ED448_VERIFY)
  26285. if (wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  26286. &key, contextPh2, sizeof(contextPh2)) != 0 ||
  26287. verify != 1) {
  26288. return -11613;
  26289. }
  26290. #endif
  26291. wc_ed448_free(&key);
  26292. return 0;
  26293. }
  26294. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  26295. WOLFSSL_TEST_SUBROUTINE int ed448_test(void)
  26296. {
  26297. int ret;
  26298. WC_RNG rng;
  26299. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  26300. defined(HAVE_ED448_KEY_IMPORT)
  26301. byte out[ED448_SIG_SIZE];
  26302. int i;
  26303. word32 outlen;
  26304. #ifdef HAVE_ED448_VERIFY
  26305. #ifdef WOLFSSL_ED448_STREAMING_VERIFY
  26306. int j;
  26307. #endif /* WOLFSSL_ED448_STREAMING_VERIFY */
  26308. int verify;
  26309. #endif /* HAVE_ED448_VERIFY */
  26310. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  26311. word32 keySz, sigSz;
  26312. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26313. ed448_key *key = NULL;
  26314. ed448_key *key2 = NULL;
  26315. #else
  26316. ed448_key key[1];
  26317. ed448_key key2[1];
  26318. #endif
  26319. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  26320. defined(HAVE_ED448_KEY_IMPORT)
  26321. /* test vectors from
  26322. https://tools.ietf.org/html/rfc8032
  26323. */
  26324. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  26325. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  26326. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  26327. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  26328. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  26329. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  26330. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  26331. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  26332. 0x5b
  26333. };
  26334. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  26335. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  26336. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  26337. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  26338. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  26339. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  26340. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  26341. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  26342. 0x4e
  26343. };
  26344. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  26345. 0x25, 0x8c, 0xdd, 0x4a, 0xda, 0x32, 0xed, 0x9c,
  26346. 0x9f, 0xf5, 0x4e, 0x63, 0x75, 0x6a, 0xe5, 0x82,
  26347. 0xfb, 0x8f, 0xab, 0x2a, 0xc7, 0x21, 0xf2, 0xc8,
  26348. 0xe6, 0x76, 0xa7, 0x27, 0x68, 0x51, 0x3d, 0x93,
  26349. 0x9f, 0x63, 0xdd, 0xdb, 0x55, 0x60, 0x91, 0x33,
  26350. 0xf2, 0x9a, 0xdf, 0x86, 0xec, 0x99, 0x29, 0xdc,
  26351. 0xcb, 0x52, 0xc1, 0xc5, 0xfd, 0x2f, 0xf7, 0xe2,
  26352. 0x1b
  26353. };
  26354. /* uncompressed test */
  26355. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  26356. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  26357. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  26358. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  26359. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  26360. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  26361. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  26362. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  26363. 0x5b
  26364. };
  26365. /* compressed prefix test */
  26366. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  26367. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  26368. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  26369. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  26370. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  26371. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  26372. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  26373. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  26374. 0x5b
  26375. };
  26376. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  26377. 0x87, 0x2d, 0x09, 0x37, 0x80, 0xf5, 0xd3, 0x73,
  26378. 0x0d, 0xf7, 0xc2, 0x12, 0x66, 0x4b, 0x37, 0xb8,
  26379. 0xa0, 0xf2, 0x4f, 0x56, 0x81, 0x0d, 0xaa, 0x83,
  26380. 0x82, 0xcd, 0x4f, 0xa3, 0xf7, 0x76, 0x34, 0xec,
  26381. 0x44, 0xdc, 0x54, 0xf1, 0xc2, 0xed, 0x9b, 0xea,
  26382. 0x86, 0xfa, 0xfb, 0x76, 0x32, 0xd8, 0xbe, 0x19,
  26383. 0x9e, 0xa1, 0x65, 0xf5, 0xad, 0x55, 0xdd, 0x9c,
  26384. 0xe8
  26385. };
  26386. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  26387. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  26388. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  26389. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  26390. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  26391. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  26392. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  26393. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  26394. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  26395. 0x80
  26396. };
  26397. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  26398. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  26399. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  26400. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  26401. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  26402. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  26403. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  26404. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  26405. 0x80
  26406. };
  26407. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  26408. 0x3b, 0xa1, 0x6d, 0xa0, 0xc6, 0xf2, 0xcc, 0x1f,
  26409. 0x30, 0x18, 0x77, 0x40, 0x75, 0x6f, 0x5e, 0x79,
  26410. 0x8d, 0x6b, 0xc5, 0xfc, 0x01, 0x5d, 0x7c, 0x63,
  26411. 0xcc, 0x95, 0x10, 0xee, 0x3f, 0xd4, 0x4a, 0xdc,
  26412. 0x24, 0xd8, 0xe9, 0x68, 0xb6, 0xe4, 0x6e, 0x6f,
  26413. 0x94, 0xd1, 0x9b, 0x94, 0x53, 0x61, 0x72, 0x6b,
  26414. 0xd7, 0x5e, 0x14, 0x9e, 0xf0, 0x98, 0x17, 0xf5,
  26415. 0x80
  26416. };
  26417. /* uncompressed test */
  26418. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  26419. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  26420. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  26421. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  26422. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  26423. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  26424. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  26425. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  26426. 0x80
  26427. };
  26428. /* compressed prefix */
  26429. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  26430. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  26431. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  26432. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  26433. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  26434. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  26435. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  26436. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  26437. 0x80
  26438. };
  26439. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  26440. 0xa8, 0x1b, 0x2e, 0x8a, 0x70, 0xa5, 0xac, 0x94,
  26441. 0xff, 0xdb, 0xcc, 0x9b, 0xad, 0xfc, 0x3f, 0xeb,
  26442. 0x08, 0x01, 0xf2, 0x58, 0x57, 0x8b, 0xb1, 0x14,
  26443. 0xad, 0x44, 0xec, 0xe1, 0xec, 0x0e, 0x79, 0x9d,
  26444. 0xa0, 0x8e, 0xff, 0xb8, 0x1c, 0x5d, 0x68, 0x5c,
  26445. 0x0c, 0x56, 0xf6, 0x4e, 0xec, 0xae, 0xf8, 0xcd,
  26446. 0xf1, 0x1c, 0xc3, 0x87, 0x37, 0x83, 0x8c, 0xf4,
  26447. 0x00
  26448. };
  26449. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  26450. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  26451. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  26452. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  26453. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  26454. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  26455. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  26456. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  26457. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  26458. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  26459. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  26460. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  26461. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  26462. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  26463. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  26464. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  26465. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  26466. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  26467. 0x26, 0x00
  26468. };
  26469. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  26470. 0x26, 0xb8, 0xf9, 0x17, 0x27, 0xbd, 0x62, 0x89,
  26471. 0x7a, 0xf1, 0x5e, 0x41, 0xeb, 0x43, 0xc3, 0x77,
  26472. 0xef, 0xb9, 0xc6, 0x10, 0xd4, 0x8f, 0x23, 0x35,
  26473. 0xcb, 0x0b, 0xd0, 0x08, 0x78, 0x10, 0xf4, 0x35,
  26474. 0x25, 0x41, 0xb1, 0x43, 0xc4, 0xb9, 0x81, 0xb7,
  26475. 0xe1, 0x8f, 0x62, 0xde, 0x8c, 0xcd, 0xf6, 0x33,
  26476. 0xfc, 0x1b, 0xf0, 0x37, 0xab, 0x7c, 0xd7, 0x79,
  26477. 0x80, 0x5e, 0x0d, 0xbc, 0xc0, 0xaa, 0xe1, 0xcb,
  26478. 0xce, 0xe1, 0xaf, 0xb2, 0xe0, 0x27, 0xdf, 0x36,
  26479. 0xbc, 0x04, 0xdc, 0xec, 0xbf, 0x15, 0x43, 0x36,
  26480. 0xc1, 0x9f, 0x0a, 0xf7, 0xe0, 0xa6, 0x47, 0x29,
  26481. 0x05, 0xe7, 0x99, 0xf1, 0x95, 0x3d, 0x2a, 0x0f,
  26482. 0xf3, 0x34, 0x8a, 0xb2, 0x1a, 0xa4, 0xad, 0xaf,
  26483. 0xd1, 0xd2, 0x34, 0x44, 0x1c, 0xf8, 0x07, 0xc0,
  26484. 0x3a, 0x00
  26485. };
  26486. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  26487. 0x7e, 0xee, 0xab, 0x7c, 0x4e, 0x50, 0xfb, 0x79,
  26488. 0x9b, 0x41, 0x8e, 0xe5, 0xe3, 0x19, 0x7f, 0xf6,
  26489. 0xbf, 0x15, 0xd4, 0x3a, 0x14, 0xc3, 0x43, 0x89,
  26490. 0xb5, 0x9d, 0xd1, 0xa7, 0xb1, 0xb8, 0x5b, 0x4a,
  26491. 0xe9, 0x04, 0x38, 0xac, 0xa6, 0x34, 0xbe, 0xa4,
  26492. 0x5e, 0x3a, 0x26, 0x95, 0xf1, 0x27, 0x0f, 0x07,
  26493. 0xfd, 0xcd, 0xf7, 0xc6, 0x2b, 0x8e, 0xfe, 0xaf,
  26494. 0x00, 0xb4, 0x5c, 0x2c, 0x96, 0xba, 0x45, 0x7e,
  26495. 0xb1, 0xa8, 0xbf, 0x07, 0x5a, 0x3d, 0xb2, 0x8e,
  26496. 0x5c, 0x24, 0xf6, 0xb9, 0x23, 0xed, 0x4a, 0xd7,
  26497. 0x47, 0xc3, 0xc9, 0xe0, 0x3c, 0x70, 0x79, 0xef,
  26498. 0xb8, 0x7c, 0xb1, 0x10, 0xd3, 0xa9, 0x98, 0x61,
  26499. 0xe7, 0x20, 0x03, 0xcb, 0xae, 0x6d, 0x6b, 0x8b,
  26500. 0x82, 0x7e, 0x4e, 0x6c, 0x14, 0x30, 0x64, 0xff,
  26501. 0x3c, 0x00
  26502. };
  26503. /* uncompressed test */
  26504. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  26505. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  26506. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  26507. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  26508. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  26509. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  26510. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  26511. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  26512. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  26513. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  26514. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  26515. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  26516. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  26517. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  26518. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  26519. 0x26, 0x00
  26520. };
  26521. /* compressed prefix */
  26522. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  26523. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  26524. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  26525. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  26526. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  26527. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  26528. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  26529. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  26530. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  26531. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  26532. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  26533. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  26534. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  26535. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  26536. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  26537. 0x26, 0x00
  26538. };
  26539. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  26540. 0xe3, 0x01, 0x34, 0x5a, 0x41, 0xa3, 0x9a, 0x4d,
  26541. 0x72, 0xff, 0xf8, 0xdf, 0x69, 0xc9, 0x80, 0x75,
  26542. 0xa0, 0xcc, 0x08, 0x2b, 0x80, 0x2f, 0xc9, 0xb2,
  26543. 0xb6, 0xbc, 0x50, 0x3f, 0x92, 0x6b, 0x65, 0xbd,
  26544. 0xdf, 0x7f, 0x4c, 0x8f, 0x1c, 0xb4, 0x9f, 0x63,
  26545. 0x96, 0xaf, 0xc8, 0xa7, 0x0a, 0xbe, 0x6d, 0x8a,
  26546. 0xef, 0x0d, 0xb4, 0x78, 0xd4, 0xc6, 0xb2, 0x97,
  26547. 0x00, 0x76, 0xc6, 0xa0, 0x48, 0x4f, 0xe7, 0x6d,
  26548. 0x76, 0xb3, 0xa9, 0x76, 0x25, 0xd7, 0x9f, 0x1c,
  26549. 0xe2, 0x40, 0xe7, 0xc5, 0x76, 0x75, 0x0d, 0x29,
  26550. 0x55, 0x28, 0x28, 0x6f, 0x71, 0x9b, 0x41, 0x3d,
  26551. 0xe9, 0xad, 0xa3, 0xe8, 0xeb, 0x78, 0xed, 0x57,
  26552. 0x36, 0x03, 0xce, 0x30, 0xd8, 0xbb, 0x76, 0x17,
  26553. 0x85, 0xdc, 0x30, 0xdb, 0xc3, 0x20, 0x86, 0x9e,
  26554. 0x1a, 0x00
  26555. };
  26556. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  26557. #define SIGSZ sizeof(sig1)
  26558. PEDANTIC_EXTENSION WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = { };
  26559. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = { 0x03 };
  26560. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = { 0x64, 0xa6, 0x5f, 0x3c, 0xde, 0xdc, 0xdd,
  26561. 0x66, 0x81, 0x1e, 0x29, 0x15 };
  26562. /* test of a 1023 byte long message */
  26563. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  26564. 0x6d, 0xdf, 0x80, 0x2e, 0x1a, 0xae, 0x49, 0x86,
  26565. 0x93, 0x5f, 0x7f, 0x98, 0x1b, 0xa3, 0xf0, 0x35,
  26566. 0x1d, 0x62, 0x73, 0xc0, 0xa0, 0xc2, 0x2c, 0x9c,
  26567. 0x0e, 0x83, 0x39, 0x16, 0x8e, 0x67, 0x54, 0x12,
  26568. 0xa3, 0xde, 0xbf, 0xaf, 0x43, 0x5e, 0xd6, 0x51,
  26569. 0x55, 0x80, 0x07, 0xdb, 0x43, 0x84, 0xb6, 0x50,
  26570. 0xfc, 0xc0, 0x7e, 0x3b, 0x58, 0x6a, 0x27, 0xa4,
  26571. 0xf7, 0xa0, 0x0a, 0xc8, 0xa6, 0xfe, 0xc2, 0xcd,
  26572. 0x86, 0xae, 0x4b, 0xf1, 0x57, 0x0c, 0x41, 0xe6,
  26573. 0xa4, 0x0c, 0x93, 0x1d, 0xb2, 0x7b, 0x2f, 0xaa,
  26574. 0x15, 0xa8, 0xce, 0xdd, 0x52, 0xcf, 0xf7, 0x36,
  26575. 0x2c, 0x4e, 0x6e, 0x23, 0xda, 0xec, 0x0f, 0xbc,
  26576. 0x3a, 0x79, 0xb6, 0x80, 0x6e, 0x31, 0x6e, 0xfc,
  26577. 0xc7, 0xb6, 0x81, 0x19, 0xbf, 0x46, 0xbc, 0x76,
  26578. 0xa2, 0x60, 0x67, 0xa5, 0x3f, 0x29, 0x6d, 0xaf,
  26579. 0xdb, 0xdc, 0x11, 0xc7, 0x7f, 0x77, 0x77, 0xe9,
  26580. 0x72, 0x66, 0x0c, 0xf4, 0xb6, 0xa9, 0xb3, 0x69,
  26581. 0xa6, 0x66, 0x5f, 0x02, 0xe0, 0xcc, 0x9b, 0x6e,
  26582. 0xdf, 0xad, 0x13, 0x6b, 0x4f, 0xab, 0xe7, 0x23,
  26583. 0xd2, 0x81, 0x3d, 0xb3, 0x13, 0x6c, 0xfd, 0xe9,
  26584. 0xb6, 0xd0, 0x44, 0x32, 0x2f, 0xee, 0x29, 0x47,
  26585. 0x95, 0x2e, 0x03, 0x1b, 0x73, 0xab, 0x5c, 0x60,
  26586. 0x33, 0x49, 0xb3, 0x07, 0xbd, 0xc2, 0x7b, 0xc6,
  26587. 0xcb, 0x8b, 0x8b, 0xbd, 0x7b, 0xd3, 0x23, 0x21,
  26588. 0x9b, 0x80, 0x33, 0xa5, 0x81, 0xb5, 0x9e, 0xad,
  26589. 0xeb, 0xb0, 0x9b, 0x3c, 0x4f, 0x3d, 0x22, 0x77,
  26590. 0xd4, 0xf0, 0x34, 0x36, 0x24, 0xac, 0xc8, 0x17,
  26591. 0x80, 0x47, 0x28, 0xb2, 0x5a, 0xb7, 0x97, 0x17,
  26592. 0x2b, 0x4c, 0x5c, 0x21, 0xa2, 0x2f, 0x9c, 0x78,
  26593. 0x39, 0xd6, 0x43, 0x00, 0x23, 0x2e, 0xb6, 0x6e,
  26594. 0x53, 0xf3, 0x1c, 0x72, 0x3f, 0xa3, 0x7f, 0xe3,
  26595. 0x87, 0xc7, 0xd3, 0xe5, 0x0b, 0xdf, 0x98, 0x13,
  26596. 0xa3, 0x0e, 0x5b, 0xb1, 0x2c, 0xf4, 0xcd, 0x93,
  26597. 0x0c, 0x40, 0xcf, 0xb4, 0xe1, 0xfc, 0x62, 0x25,
  26598. 0x92, 0xa4, 0x95, 0x88, 0x79, 0x44, 0x94, 0xd5,
  26599. 0x6d, 0x24, 0xea, 0x4b, 0x40, 0xc8, 0x9f, 0xc0,
  26600. 0x59, 0x6c, 0xc9, 0xeb, 0xb9, 0x61, 0xc8, 0xcb,
  26601. 0x10, 0xad, 0xde, 0x97, 0x6a, 0x5d, 0x60, 0x2b,
  26602. 0x1c, 0x3f, 0x85, 0xb9, 0xb9, 0xa0, 0x01, 0xed,
  26603. 0x3c, 0x6a, 0x4d, 0x3b, 0x14, 0x37, 0xf5, 0x20,
  26604. 0x96, 0xcd, 0x19, 0x56, 0xd0, 0x42, 0xa5, 0x97,
  26605. 0xd5, 0x61, 0xa5, 0x96, 0xec, 0xd3, 0xd1, 0x73,
  26606. 0x5a, 0x8d, 0x57, 0x0e, 0xa0, 0xec, 0x27, 0x22,
  26607. 0x5a, 0x2c, 0x4a, 0xaf, 0xf2, 0x63, 0x06, 0xd1,
  26608. 0x52, 0x6c, 0x1a, 0xf3, 0xca, 0x6d, 0x9c, 0xf5,
  26609. 0xa2, 0xc9, 0x8f, 0x47, 0xe1, 0xc4, 0x6d, 0xb9,
  26610. 0xa3, 0x32, 0x34, 0xcf, 0xd4, 0xd8, 0x1f, 0x2c,
  26611. 0x98, 0x53, 0x8a, 0x09, 0xeb, 0xe7, 0x69, 0x98,
  26612. 0xd0, 0xd8, 0xfd, 0x25, 0x99, 0x7c, 0x7d, 0x25,
  26613. 0x5c, 0x6d, 0x66, 0xec, 0xe6, 0xfa, 0x56, 0xf1,
  26614. 0x11, 0x44, 0x95, 0x0f, 0x02, 0x77, 0x95, 0xe6,
  26615. 0x53, 0x00, 0x8f, 0x4b, 0xd7, 0xca, 0x2d, 0xee,
  26616. 0x85, 0xd8, 0xe9, 0x0f, 0x3d, 0xc3, 0x15, 0x13,
  26617. 0x0c, 0xe2, 0xa0, 0x03, 0x75, 0xa3, 0x18, 0xc7,
  26618. 0xc3, 0xd9, 0x7b, 0xe2, 0xc8, 0xce, 0x5b, 0x6d,
  26619. 0xb4, 0x1a, 0x62, 0x54, 0xff, 0x26, 0x4f, 0xa6,
  26620. 0x15, 0x5b, 0xae, 0xe3, 0xb0, 0x77, 0x3c, 0x0f,
  26621. 0x49, 0x7c, 0x57, 0x3f, 0x19, 0xbb, 0x4f, 0x42,
  26622. 0x40, 0x28, 0x1f, 0x0b, 0x1f, 0x4f, 0x7b, 0xe8,
  26623. 0x57, 0xa4, 0xe5, 0x9d, 0x41, 0x6c, 0x06, 0xb4,
  26624. 0xc5, 0x0f, 0xa0, 0x9e, 0x18, 0x10, 0xdd, 0xc6,
  26625. 0xb1, 0x46, 0x7b, 0xae, 0xac, 0x5a, 0x36, 0x68,
  26626. 0xd1, 0x1b, 0x6e, 0xca, 0xa9, 0x01, 0x44, 0x00,
  26627. 0x16, 0xf3, 0x89, 0xf8, 0x0a, 0xcc, 0x4d, 0xb9,
  26628. 0x77, 0x02, 0x5e, 0x7f, 0x59, 0x24, 0x38, 0x8c,
  26629. 0x7e, 0x34, 0x0a, 0x73, 0x2e, 0x55, 0x44, 0x40,
  26630. 0xe7, 0x65, 0x70, 0xf8, 0xdd, 0x71, 0xb7, 0xd6,
  26631. 0x40, 0xb3, 0x45, 0x0d, 0x1f, 0xd5, 0xf0, 0x41,
  26632. 0x0a, 0x18, 0xf9, 0xa3, 0x49, 0x4f, 0x70, 0x7c,
  26633. 0x71, 0x7b, 0x79, 0xb4, 0xbf, 0x75, 0xc9, 0x84,
  26634. 0x00, 0xb0, 0x96, 0xb2, 0x16, 0x53, 0xb5, 0xd2,
  26635. 0x17, 0xcf, 0x35, 0x65, 0xc9, 0x59, 0x74, 0x56,
  26636. 0xf7, 0x07, 0x03, 0x49, 0x7a, 0x07, 0x87, 0x63,
  26637. 0x82, 0x9b, 0xc0, 0x1b, 0xb1, 0xcb, 0xc8, 0xfa,
  26638. 0x04, 0xea, 0xdc, 0x9a, 0x6e, 0x3f, 0x66, 0x99,
  26639. 0x58, 0x7a, 0x9e, 0x75, 0xc9, 0x4e, 0x5b, 0xab,
  26640. 0x00, 0x36, 0xe0, 0xb2, 0xe7, 0x11, 0x39, 0x2c,
  26641. 0xff, 0x00, 0x47, 0xd0, 0xd6, 0xb0, 0x5b, 0xd2,
  26642. 0xa5, 0x88, 0xbc, 0x10, 0x97, 0x18, 0x95, 0x42,
  26643. 0x59, 0xf1, 0xd8, 0x66, 0x78, 0xa5, 0x79, 0xa3,
  26644. 0x12, 0x0f, 0x19, 0xcf, 0xb2, 0x96, 0x3f, 0x17,
  26645. 0x7a, 0xeb, 0x70, 0xf2, 0xd4, 0x84, 0x48, 0x26,
  26646. 0x26, 0x2e, 0x51, 0xb8, 0x02, 0x71, 0x27, 0x20,
  26647. 0x68, 0xef, 0x5b, 0x38, 0x56, 0xfa, 0x85, 0x35,
  26648. 0xaa, 0x2a, 0x88, 0xb2, 0xd4, 0x1f, 0x2a, 0x0e,
  26649. 0x2f, 0xda, 0x76, 0x24, 0xc2, 0x85, 0x02, 0x72,
  26650. 0xac, 0x4a, 0x2f, 0x56, 0x1f, 0x8f, 0x2f, 0x7a,
  26651. 0x31, 0x8b, 0xfd, 0x5c, 0xaf, 0x96, 0x96, 0x14,
  26652. 0x9e, 0x4a, 0xc8, 0x24, 0xad, 0x34, 0x60, 0x53,
  26653. 0x8f, 0xdc, 0x25, 0x42, 0x1b, 0xee, 0xc2, 0xcc,
  26654. 0x68, 0x18, 0x16, 0x2d, 0x06, 0xbb, 0xed, 0x0c,
  26655. 0x40, 0xa3, 0x87, 0x19, 0x23, 0x49, 0xdb, 0x67,
  26656. 0xa1, 0x18, 0xba, 0xda, 0x6c, 0xd5, 0xab, 0x01,
  26657. 0x40, 0xee, 0x27, 0x32, 0x04, 0xf6, 0x28, 0xaa,
  26658. 0xd1, 0xc1, 0x35, 0xf7, 0x70, 0x27, 0x9a, 0x65,
  26659. 0x1e, 0x24, 0xd8, 0xc1, 0x4d, 0x75, 0xa6, 0x05,
  26660. 0x9d, 0x76, 0xb9, 0x6a, 0x6f, 0xd8, 0x57, 0xde,
  26661. 0xf5, 0xe0, 0xb3, 0x54, 0xb2, 0x7a, 0xb9, 0x37,
  26662. 0xa5, 0x81, 0x5d, 0x16, 0xb5, 0xfa, 0xe4, 0x07,
  26663. 0xff, 0x18, 0x22, 0x2c, 0x6d, 0x1e, 0xd2, 0x63,
  26664. 0xbe, 0x68, 0xc9, 0x5f, 0x32, 0xd9, 0x08, 0xbd,
  26665. 0x89, 0x5c, 0xd7, 0x62, 0x07, 0xae, 0x72, 0x64,
  26666. 0x87, 0x56, 0x7f, 0x9a, 0x67, 0xda, 0xd7, 0x9a,
  26667. 0xbe, 0xc3, 0x16, 0xf6, 0x83, 0xb1, 0x7f, 0x2d,
  26668. 0x02, 0xbf, 0x07, 0xe0, 0xac, 0x8b, 0x5b, 0xc6,
  26669. 0x16, 0x2c, 0xf9, 0x46, 0x97, 0xb3, 0xc2, 0x7c,
  26670. 0xd1, 0xfe, 0xa4, 0x9b, 0x27, 0xf2, 0x3b, 0xa2,
  26671. 0x90, 0x18, 0x71, 0x96, 0x25, 0x06, 0x52, 0x0c,
  26672. 0x39, 0x2d, 0xa8, 0xb6, 0xad, 0x0d, 0x99, 0xf7,
  26673. 0x01, 0x3f, 0xbc, 0x06, 0xc2, 0xc1, 0x7a, 0x56,
  26674. 0x95, 0x00, 0xc8, 0xa7, 0x69, 0x64, 0x81, 0xc1,
  26675. 0xcd, 0x33, 0xe9, 0xb1, 0x4e, 0x40, 0xb8, 0x2e,
  26676. 0x79, 0xa5, 0xf5, 0xdb, 0x82, 0x57, 0x1b, 0xa9,
  26677. 0x7b, 0xae, 0x3a, 0xd3, 0xe0, 0x47, 0x95, 0x15,
  26678. 0xbb, 0x0e, 0x2b, 0x0f, 0x3b, 0xfc, 0xd1, 0xfd,
  26679. 0x33, 0x03, 0x4e, 0xfc, 0x62, 0x45, 0xed, 0xdd,
  26680. 0x7e, 0xe2, 0x08, 0x6d, 0xda, 0xe2, 0x60, 0x0d,
  26681. 0x8c, 0xa7, 0x3e, 0x21, 0x4e, 0x8c, 0x2b, 0x0b,
  26682. 0xdb, 0x2b, 0x04, 0x7c, 0x6a, 0x46, 0x4a, 0x56,
  26683. 0x2e, 0xd7, 0x7b, 0x73, 0xd2, 0xd8, 0x41, 0xc4,
  26684. 0xb3, 0x49, 0x73, 0x55, 0x12, 0x57, 0x71, 0x3b,
  26685. 0x75, 0x36, 0x32, 0xef, 0xba, 0x34, 0x81, 0x69,
  26686. 0xab, 0xc9, 0x0a, 0x68, 0xf4, 0x26, 0x11, 0xa4,
  26687. 0x01, 0x26, 0xd7, 0xcb, 0x21, 0xb5, 0x86, 0x95,
  26688. 0x56, 0x81, 0x86, 0xf7, 0xe5, 0x69, 0xd2, 0xff,
  26689. 0x0f, 0x9e, 0x74, 0x5d, 0x04, 0x87, 0xdd, 0x2e,
  26690. 0xb9, 0x97, 0xca, 0xfc, 0x5a, 0xbf, 0x9d, 0xd1,
  26691. 0x02, 0xe6, 0x2f, 0xf6, 0x6c, 0xba, 0x87
  26692. };
  26693. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  26694. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  26695. sizeof(msg2),
  26696. sizeof(msg3),
  26697. 0 /*sizeof(msg1)*/,
  26698. 0 /*sizeof(msg1)*/,
  26699. sizeof(msg4)
  26700. };
  26701. #ifndef NO_ASN
  26702. static const byte privateEd448[] = {
  26703. 0x30, 0x47, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06,
  26704. 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04, 0x39,
  26705. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  26706. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  26707. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  26708. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  26709. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  26710. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  26711. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  26712. 0x5b
  26713. };
  26714. static const byte publicEd448[] = {
  26715. 0x30, 0x43, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65,
  26716. 0x71, 0x03, 0x3a, 0x00, 0x5f, 0xd7, 0x44, 0x9b,
  26717. 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec,
  26718. 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  26719. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d,
  26720. 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76,
  26721. 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d,
  26722. 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe,
  26723. 0xaf, 0xe8, 0x25, 0x61, 0x80
  26724. };
  26725. static const byte privPubEd448[] = {
  26726. 0x30, 0x81, 0x82, 0x02, 0x01, 0x00, 0x30, 0x05,
  26727. 0x06, 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04,
  26728. 0x39, 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d,
  26729. 0x10, 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e,
  26730. 0xbf, 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c,
  26731. 0x9f, 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48,
  26732. 0xa3, 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04,
  26733. 0x4e, 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f,
  26734. 0x8f, 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98,
  26735. 0xf9, 0x5b, 0x81, 0x39, 0x5f, 0xd7, 0x44, 0x9b,
  26736. 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec,
  26737. 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  26738. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d,
  26739. 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76,
  26740. 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d,
  26741. 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe,
  26742. 0xaf, 0xe8, 0x25, 0x61, 0x80
  26743. };
  26744. word32 idx;
  26745. #endif /* NO_ASN */
  26746. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  26747. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  26748. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26749. ed448_key *key3 = NULL;
  26750. #else
  26751. ed448_key key3[1];
  26752. #endif
  26753. #endif
  26754. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26755. key = (ed448_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26756. key2 = (ed448_key *)XMALLOC(sizeof(*key2), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26757. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  26758. key3 = (ed448_key *)XMALLOC(sizeof(*key3), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26759. #endif
  26760. #endif
  26761. /* create ed448 keys */
  26762. #ifndef HAVE_FIPS
  26763. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  26764. #else
  26765. ret = wc_InitRng(&rng);
  26766. #endif
  26767. if (ret != 0) {
  26768. XMEMSET(&rng, 0, sizeof(rng));
  26769. ERROR_OUT(-11700, out);
  26770. }
  26771. if (wc_ed448_init(key) < 0)
  26772. ERROR_OUT(-11903, out);
  26773. if (wc_ed448_init(key2) < 0)
  26774. ERROR_OUT(-11904, out);
  26775. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  26776. if (wc_ed448_init(key3) < 0)
  26777. ERROR_OUT(-11905, out);
  26778. #endif
  26779. if (wc_ed448_make_key(&rng, ED448_KEY_SIZE, key) < 0)
  26780. ERROR_OUT(-11906, out);
  26781. if (wc_ed448_make_key(&rng, ED448_KEY_SIZE, key2) < 0)
  26782. ERROR_OUT(-11907, out);
  26783. /* helper functions for signature and key size */
  26784. keySz = wc_ed448_size(key);
  26785. sigSz = wc_ed448_sig_size(key);
  26786. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  26787. defined(HAVE_ED448_KEY_IMPORT)
  26788. for (i = 0; i < 6; i++) {
  26789. outlen = sizeof(out);
  26790. XMEMSET(out, 0, sizeof(out));
  26791. if (wc_ed448_import_private_key(sKeys[i], ED448_KEY_SIZE, pKeys[i],
  26792. pKeySz[i], key) != 0)
  26793. ERROR_OUT(-11701 - i, out);
  26794. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, key, NULL,
  26795. 0) != 0)
  26796. ERROR_OUT(-11711 - i, out);
  26797. if (XMEMCMP(out, sigs[i], 114))
  26798. ERROR_OUT(-11721 - i, out);
  26799. #if defined(HAVE_ED448_VERIFY)
  26800. /* test verify on good msg */
  26801. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key,
  26802. NULL, 0) != 0 || verify != 1)
  26803. ERROR_OUT(-11731 - i, out);
  26804. #ifdef WOLFSSL_ED448_STREAMING_VERIFY
  26805. /* test verify on good msg using streaming interface directly */
  26806. if (wc_ed448_verify_msg_init(out, outlen,
  26807. key, (byte)Ed448, NULL, 0) != 0)
  26808. ERROR_OUT(-11911 - i, out);
  26809. for (j = 0; j < msgSz[i]; j += i) {
  26810. if (wc_ed448_verify_msg_update(msgs[i] + j, MIN(i, msgSz[i] - j), key) != 0)
  26811. ERROR_OUT(-11921 - i, out);
  26812. }
  26813. if (wc_ed448_verify_msg_final(out, outlen, &verify,
  26814. key) != 0 || verify != 1)
  26815. ERROR_OUT(-11931 - i, out);
  26816. #endif /* WOLFSSL_ED448_STREAMING_VERIFY */
  26817. /* test verify on bad msg */
  26818. out[outlen-2] = out[outlen-2] + 1;
  26819. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key,
  26820. NULL, 0) == 0 || verify == 1)
  26821. ERROR_OUT(-11741 - i, out);
  26822. #endif /* HAVE_ED448_VERIFY */
  26823. /* test api for import/exporting keys */
  26824. {
  26825. byte *exportPKey = NULL;
  26826. byte *exportSKey = NULL;
  26827. word32 exportPSz = ED448_KEY_SIZE;
  26828. word32 exportSSz = ED448_KEY_SIZE;
  26829. exportPKey = (byte *)XMALLOC(exportPSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26830. exportSKey = (byte *)XMALLOC(exportSSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26831. if ((exportPKey == NULL) || (exportSKey == NULL))
  26832. ERROR_OUT(-11902, out);
  26833. ret = 0;
  26834. do {
  26835. if (wc_ed448_export_public(key, exportPKey, &exportPSz) != 0) {
  26836. ret = -11751 - i;
  26837. break;
  26838. }
  26839. if (wc_ed448_import_public_ex(exportPKey, exportPSz, key2, 1) != 0) {
  26840. ret = -11761 - i;
  26841. break;
  26842. }
  26843. if (wc_ed448_export_private_only(key, exportSKey, &exportSSz) != 0) {
  26844. ret = -11771 - i;
  26845. break;
  26846. }
  26847. if (wc_ed448_import_private_key(exportSKey, exportSSz,
  26848. exportPKey, exportPSz, key2) != 0) {
  26849. ret = -11781 - i;
  26850. break;
  26851. }
  26852. /* clear "out" buffer and test sign with imported keys */
  26853. outlen = sizeof(out);
  26854. XMEMSET(out, 0, sizeof(out));
  26855. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, key2, NULL,
  26856. 0) != 0) {
  26857. ret = -11791 - i;
  26858. break;
  26859. }
  26860. } while(0);
  26861. XFREE(exportPKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26862. XFREE(exportSKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26863. if (ret != 0)
  26864. goto out;
  26865. }
  26866. #if defined(HAVE_ED448_VERIFY)
  26867. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key2,
  26868. NULL, 0) != 0 || verify != 1)
  26869. ERROR_OUT(-11801 - i, out);
  26870. if (XMEMCMP(out, sigs[i], SIGSZ))
  26871. ERROR_OUT(-11811 - i, out);
  26872. #endif /* HAVE_ED448_VERIFY */
  26873. }
  26874. ret = ed448_ctx_test();
  26875. if (ret != 0)
  26876. goto out;
  26877. ret = ed448ph_test();
  26878. if (ret != 0)
  26879. goto out;
  26880. #ifndef NO_ASN
  26881. /* Try ASN.1 encoded private-only key and public key. */
  26882. idx = 0;
  26883. if (wc_Ed448PrivateKeyDecode(privateEd448, &idx, key3,
  26884. sizeof(privateEd448)) != 0)
  26885. ERROR_OUT(-11821, out);
  26886. if (wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0)
  26887. != BAD_FUNC_ARG)
  26888. ERROR_OUT(-11831, out);
  26889. idx = 0;
  26890. if (wc_Ed448PublicKeyDecode(publicEd448, &idx, key3,
  26891. sizeof(publicEd448)) != 0)
  26892. ERROR_OUT(-11841, out);
  26893. if (wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0) != 0)
  26894. ERROR_OUT(-11851, out);
  26895. if (XMEMCMP(out, sigs[0], SIGSZ))
  26896. ERROR_OUT(-11861, out);
  26897. #if defined(HAVE_ED448_VERIFY)
  26898. /* test verify on good msg */
  26899. if (wc_ed448_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, key3,
  26900. NULL, 0) != 0 || verify != 1)
  26901. ERROR_OUT(-11871, out);
  26902. #endif /* HAVE_ED448_VERIFY */
  26903. wc_ed448_free(key3);
  26904. if (wc_ed448_init(key3) < 0)
  26905. ERROR_OUT(-11908, out);
  26906. idx = 0;
  26907. if (wc_Ed448PrivateKeyDecode(privPubEd448, &idx, key3,
  26908. sizeof(privPubEd448)) != 0)
  26909. ERROR_OUT(-11881, out);
  26910. if (wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0) != 0)
  26911. ERROR_OUT(-11891, out);
  26912. if (XMEMCMP(out, sigs[0], SIGSZ))
  26913. ERROR_OUT(-11901, out);
  26914. #endif /* NO_ASN */
  26915. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  26916. ret = 0;
  26917. out:
  26918. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26919. if (key) {
  26920. wc_ed448_free(key);
  26921. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26922. }
  26923. if (key2) {
  26924. wc_ed448_free(key2);
  26925. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26926. }
  26927. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  26928. if (key3) {
  26929. wc_ed448_free(key3);
  26930. XFREE(key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26931. }
  26932. #endif
  26933. #else
  26934. wc_ed448_free(key);
  26935. wc_ed448_free(key2);
  26936. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  26937. wc_ed448_free(key3);
  26938. #endif
  26939. #endif
  26940. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  26941. wc_FreeRng(&rng);
  26942. #endif
  26943. if (ret < 0)
  26944. return ret;
  26945. /* hush warnings of unused keySz and sigSz */
  26946. (void)keySz;
  26947. (void)sigSz;
  26948. #ifdef WOLFSSL_TEST_CERT
  26949. ret = ed448_test_cert();
  26950. if (ret < 0)
  26951. return ret;
  26952. #ifdef WOLFSSL_CERT_GEN
  26953. ret = ed448_test_make_cert();
  26954. if (ret < 0)
  26955. return ret;
  26956. #endif /* WOLFSSL_CERT_GEN */
  26957. #endif /* WOLFSSL_TEST_CERT */
  26958. return 0;
  26959. }
  26960. #endif /* HAVE_ED448 */
  26961. #ifdef WOLFSSL_HAVE_KYBER
  26962. #ifdef WOLFSSL_WC_KYBER /* OQS and PQM4 do not support KATs */
  26963. #ifdef WOLFSSL_KYBER512
  26964. static int kyber512_kat(void)
  26965. {
  26966. KyberKey key;
  26967. int ret;
  26968. byte priv[KYBER512_PRIVATE_KEY_SIZE];
  26969. byte pub[KYBER512_PUBLIC_KEY_SIZE];
  26970. byte ct[KYBER512_CIPHER_TEXT_SIZE];
  26971. byte ss[KYBER_SS_SZ];
  26972. byte ss_dec[KYBER_SS_SZ];
  26973. const byte kyber512_rand[] = {
  26974. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  26975. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  26976. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  26977. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  26978. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  26979. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  26980. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  26981. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  26982. };
  26983. const byte kyber512enc_rand[] = {
  26984. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  26985. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  26986. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  26987. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  26988. };
  26989. const byte kyber512_pk[] = {
  26990. 0x11, 0x5A, 0xCE, 0x0E, 0x64, 0x67, 0x7C, 0xBB,
  26991. 0x7D, 0xCF, 0xC9, 0x3C, 0x16, 0xD3, 0xA3, 0x05,
  26992. 0xF6, 0x76, 0x15, 0xA4, 0x88, 0xD7, 0x11, 0xAA,
  26993. 0x56, 0x69, 0x8C, 0x56, 0x63, 0xAB, 0x7A, 0xC9,
  26994. 0xCE, 0x66, 0xD5, 0x47, 0xC0, 0x59, 0x5F, 0x98,
  26995. 0xA4, 0x3F, 0x46, 0x50, 0xBB, 0xE0, 0x8C, 0x36,
  26996. 0x4D, 0x97, 0x67, 0x89, 0x11, 0x7D, 0x34, 0xF6,
  26997. 0xAE, 0x51, 0xAC, 0x06, 0x3C, 0xB5, 0x5C, 0x6C,
  26998. 0xA3, 0x25, 0x58, 0x22, 0x7D, 0xFE, 0xF8, 0x07,
  26999. 0xD1, 0x9C, 0x30, 0xDE, 0x41, 0x44, 0x24, 0x09,
  27000. 0x7F, 0x6A, 0xA2, 0x36, 0xA1, 0x05, 0x3B, 0x4A,
  27001. 0x07, 0xA7, 0x6B, 0xE3, 0x72, 0xA5, 0xC6, 0xB6,
  27002. 0x00, 0x27, 0x91, 0xEB, 0xE0, 0xAF, 0xDA, 0xF5,
  27003. 0x4E, 0x1C, 0xA2, 0x37, 0xFF, 0x54, 0x5B, 0xA6,
  27004. 0x83, 0x43, 0xE7, 0x45, 0xC0, 0x4A, 0xD1, 0x63,
  27005. 0x9D, 0xBC, 0x59, 0x03, 0x46, 0xB6, 0xB9, 0x56,
  27006. 0x9B, 0x56, 0xDB, 0xBF, 0xE5, 0x31, 0x51, 0x91,
  27007. 0x30, 0x66, 0xE5, 0xC8, 0x55, 0x27, 0xDC, 0x94,
  27008. 0x68, 0x11, 0x0A, 0x13, 0x6A, 0x41, 0x14, 0x97,
  27009. 0xC2, 0x27, 0xDC, 0xB8, 0xC9, 0xB2, 0x55, 0x70,
  27010. 0xB7, 0xA0, 0xE4, 0x2A, 0xAD, 0xA6, 0x70, 0x9F,
  27011. 0x23, 0x20, 0x8F, 0x5D, 0x49, 0x6E, 0xBA, 0xB7,
  27012. 0x84, 0x3F, 0x64, 0x83, 0xBF, 0x0C, 0x0C, 0x73,
  27013. 0xA4, 0x02, 0x96, 0xEC, 0x2C, 0x64, 0x40, 0x00,
  27014. 0x13, 0x94, 0xC9, 0x9C, 0xA1, 0x73, 0xD5, 0xC7,
  27015. 0x75, 0xB7, 0xF4, 0x15, 0xD0, 0x2A, 0x5A, 0x26,
  27016. 0xA0, 0x74, 0x07, 0x91, 0x85, 0x87, 0xC4, 0x11,
  27017. 0x69, 0xF2, 0xB7, 0x17, 0x87, 0x55, 0xAC, 0xC2,
  27018. 0x7F, 0xC8, 0xB1, 0x9C, 0x4C, 0x4B, 0x3F, 0xCD,
  27019. 0x41, 0x05, 0x3F, 0x2C, 0x74, 0xC8, 0xA1, 0x0A,
  27020. 0x83, 0x21, 0x24, 0x1B, 0x28, 0x02, 0x43, 0x28,
  27021. 0x75, 0xAE, 0x80, 0x8B, 0x9E, 0xF1, 0x36, 0x5C,
  27022. 0x7B, 0x8A, 0x52, 0x90, 0x2F, 0x13, 0x17, 0xBA,
  27023. 0x2F, 0xB0, 0x26, 0x9F, 0x47, 0x93, 0x06, 0x72,
  27024. 0x10, 0x7B, 0x47, 0x26, 0xFE, 0xF6, 0x45, 0x47,
  27025. 0x39, 0x4D, 0x33, 0x20, 0xC8, 0xF1, 0x20, 0xB3,
  27026. 0xC2, 0xF4, 0x72, 0x5B, 0x03, 0x05, 0xFA, 0xB8,
  27027. 0x8C, 0xC7, 0x98, 0x1F, 0xCB, 0x09, 0xA7, 0x6A,
  27028. 0x1C, 0xBF, 0x7F, 0x17, 0x9F, 0x43, 0xBB, 0x0A,
  27029. 0x4C, 0x8B, 0x05, 0x90, 0x85, 0x7F, 0x1E, 0x69,
  27030. 0x70, 0x84, 0x66, 0xC7, 0xF8, 0x60, 0x73, 0x91,
  27031. 0xE7, 0xBC, 0x52, 0x68, 0xBF, 0xD3, 0xD7, 0xA1,
  27032. 0xDF, 0xFC, 0xB4, 0xEC, 0xA2, 0xA1, 0xC9, 0xB5,
  27033. 0x97, 0x59, 0x30, 0x13, 0xD5, 0xFC, 0x42, 0x02,
  27034. 0xEC, 0x2B, 0x74, 0xE5, 0x7A, 0xB7, 0x6B, 0xBC,
  27035. 0xF3, 0x63, 0x2B, 0xBA, 0xF9, 0x7C, 0xDC, 0x41,
  27036. 0x8A, 0x6F, 0x16, 0x39, 0x28, 0x38, 0xCA, 0x9B,
  27037. 0xF4, 0x5D, 0xDF, 0x02, 0x37, 0x77, 0xB7, 0x56,
  27038. 0x18, 0x33, 0xC1, 0x05, 0x19, 0x0F, 0x94, 0xF3,
  27039. 0x02, 0xC5, 0x9B, 0x53, 0x19, 0x00, 0xBB, 0xC8,
  27040. 0x16, 0x36, 0x1F, 0xAA, 0x5B, 0x33, 0x80, 0xCA,
  27041. 0x3A, 0x89, 0x31, 0x04, 0xCA, 0x73, 0x88, 0xB1,
  27042. 0x85, 0x67, 0x1B, 0x3E, 0x5F, 0xE3, 0x79, 0x0E,
  27043. 0x9A, 0x62, 0x6E, 0xC4, 0x6D, 0x9B, 0x0B, 0x33,
  27044. 0xC7, 0xA4, 0x19, 0xAF, 0x7B, 0x32, 0xB6, 0x85,
  27045. 0x98, 0x94, 0xF5, 0x75, 0xD8, 0x2A, 0xC5, 0x45,
  27046. 0x6B, 0x54, 0x90, 0xA7, 0xAF, 0x8F, 0xE6, 0x10,
  27047. 0x46, 0x36, 0x05, 0x89, 0xEC, 0xBA, 0x72, 0x44,
  27048. 0x23, 0x6F, 0x41, 0x23, 0x11, 0x6B, 0x61, 0x74,
  27049. 0xAA, 0x17, 0x92, 0x49, 0xA4, 0x91, 0x95, 0xB3,
  27050. 0x56, 0xC7, 0x2F, 0xC6, 0x64, 0x1F, 0x02, 0x51,
  27051. 0x81, 0x2E, 0xAA, 0x98, 0x57, 0x0B, 0x04, 0x66,
  27052. 0x99, 0x07, 0x0E, 0x08, 0x19, 0xDC, 0x27, 0x13,
  27053. 0xF4, 0x69, 0x13, 0x7D, 0xFC, 0x6A, 0x3D, 0x7B,
  27054. 0x92, 0xB2, 0x98, 0x99, 0x5E, 0xE7, 0x80, 0x36,
  27055. 0x91, 0x53, 0xAC, 0x36, 0x6B, 0x06, 0xD7, 0x24,
  27056. 0x9C, 0xD0, 0x9E, 0x1B, 0x33, 0x78, 0xFB, 0x04,
  27057. 0x39, 0x9C, 0xEC, 0xB8, 0x65, 0x05, 0x81, 0xD6,
  27058. 0x37, 0xC7, 0x9A, 0xE6, 0x7D, 0x6F, 0x2C, 0xAF,
  27059. 0x6A, 0xBA, 0xCF, 0x59, 0x81, 0x59, 0xA7, 0x79,
  27060. 0x2C, 0xB3, 0xC9, 0x71, 0xD1, 0x49, 0x9D, 0x23,
  27061. 0x73, 0xAD, 0x20, 0xF6, 0x3F, 0x03, 0xBB, 0x59,
  27062. 0xED, 0x13, 0x73, 0x84, 0xAC, 0x61, 0xA7, 0x15,
  27063. 0x51, 0x43, 0xB8, 0xCA, 0x49, 0x32, 0x61, 0x2E,
  27064. 0xC9, 0x15, 0xE4, 0xCA, 0x34, 0x6A, 0x9B, 0xCE,
  27065. 0x5D, 0xD6, 0x04, 0x17, 0xC6, 0xB2, 0xA8, 0x9B,
  27066. 0x1C, 0xC4, 0x35, 0x64, 0x3F, 0x87, 0x5B, 0xDC,
  27067. 0x5A, 0x7E, 0x5B, 0x34, 0x81, 0xCF, 0x91, 0x9E,
  27068. 0xA0, 0x91, 0x72, 0xFE, 0xBC, 0x46, 0xD4, 0xFC,
  27069. 0x3F, 0xB0, 0xCB, 0x95, 0x91, 0x70, 0x4E, 0xE2,
  27070. 0xDB, 0xB6, 0x18, 0x44, 0xB2, 0xF3, 0x31, 0x4A,
  27071. 0x06, 0xBB, 0x6C, 0x6D, 0x34, 0x00, 0x5E, 0x48,
  27072. 0x5C, 0xE6, 0x67, 0xBD, 0xC7, 0xD0, 0x98, 0x58,
  27073. 0x69, 0x28, 0xD2, 0xD9, 0x13, 0x40, 0xF0, 0x04,
  27074. 0x19, 0xEA, 0x40, 0x13, 0x51, 0xA2, 0x40, 0xA0,
  27075. 0xB0, 0x41, 0x05, 0x8B, 0xEF, 0xB0, 0xC2, 0xFD,
  27076. 0x32, 0x64, 0x5B, 0x7A, 0x2D, 0xF8, 0xF5, 0xCB,
  27077. 0xFD, 0x87, 0x33, 0x27, 0xC9, 0x78, 0xD7, 0xB3,
  27078. 0x51, 0xA2, 0x80, 0x88, 0x43, 0x88, 0x37, 0x02,
  27079. 0x4C, 0x52, 0xB9, 0xC2, 0x95, 0xCD, 0x71, 0x36,
  27080. 0x46, 0xFB, 0x5D, 0x6C, 0x0C, 0xCF, 0xB4, 0x70,
  27081. 0x73, 0x4A, 0xC2, 0xB2, 0xBC, 0x81, 0x23, 0xC2,
  27082. 0xC1, 0x3D, 0xF6, 0x93, 0x8E, 0x92, 0x45, 0x5A,
  27083. 0x86, 0x26, 0x39, 0xFE, 0xB8, 0xA6, 0x4B, 0x85,
  27084. 0x16, 0x3E, 0x32, 0x70, 0x7E, 0x03, 0x7B, 0x38,
  27085. 0xD8, 0xAC, 0x39, 0x22, 0xB4, 0x51, 0x87, 0xBB,
  27086. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  27087. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  27088. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  27089. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  27090. };
  27091. const byte kyber512_sk[] = {
  27092. 0x6C, 0x89, 0x2B, 0x02, 0x97, 0xA9, 0xC7, 0x64,
  27093. 0x14, 0x93, 0xF8, 0x7D, 0xAF, 0x35, 0x33, 0xEE,
  27094. 0xD6, 0x1F, 0x07, 0xF4, 0x65, 0x20, 0x66, 0x33,
  27095. 0x7E, 0xD7, 0x40, 0x46, 0xDC, 0xC7, 0x1B, 0xA0,
  27096. 0x3F, 0x30, 0x96, 0x01, 0x03, 0x16, 0x1F, 0x7D,
  27097. 0xEB, 0x53, 0xA7, 0x1B, 0x11, 0x61, 0x72, 0x63,
  27098. 0xFE, 0x2A, 0x80, 0x97, 0x69, 0xCE, 0x6D, 0x70,
  27099. 0xA8, 0x5F, 0xE6, 0x00, 0xEC, 0xE2, 0x9D, 0x7F,
  27100. 0x36, 0xA1, 0x6D, 0x33, 0x1B, 0x8B, 0x2A, 0x9E,
  27101. 0x1D, 0xB8, 0xC0, 0x90, 0x74, 0x2D, 0xF0, 0x73,
  27102. 0x9F, 0xF0, 0x60, 0xCE, 0xB4, 0xEC, 0xC5, 0xAB,
  27103. 0x1C, 0x5E, 0x55, 0xAC, 0x97, 0xBB, 0x66, 0xA7,
  27104. 0xF8, 0x95, 0x10, 0x5D, 0x57, 0x78, 0x2B, 0x22,
  27105. 0x95, 0x38, 0xE3, 0x42, 0x15, 0x44, 0xA3, 0x42,
  27106. 0x14, 0x08, 0xDB, 0xF4, 0x49, 0x10, 0x93, 0x4C,
  27107. 0xC4, 0x23, 0x77, 0x4F, 0x16, 0x76, 0xFF, 0x1C,
  27108. 0x30, 0x6F, 0x97, 0x55, 0x5F, 0x57, 0xB4, 0xAE,
  27109. 0xD7, 0xA6, 0xBA, 0xB9, 0x50, 0xA8, 0x16, 0x3C,
  27110. 0x8D, 0x31, 0x8D, 0xEA, 0x62, 0x75, 0x1B, 0xD6,
  27111. 0xAB, 0xC5, 0x06, 0x9C, 0x06, 0xC8, 0x8F, 0x33,
  27112. 0x00, 0x26, 0xA1, 0x98, 0x06, 0xA0, 0x3B, 0x97,
  27113. 0xA7, 0x69, 0x6B, 0x56, 0xDA, 0x21, 0x82, 0x7B,
  27114. 0xB4, 0xE8, 0xDC, 0x03, 0x11, 0x52, 0xB4, 0x1B,
  27115. 0x89, 0x2A, 0x9E, 0x99, 0xAD, 0xF6, 0xE1, 0x96,
  27116. 0x3E, 0x96, 0x57, 0x88, 0x28, 0x15, 0x4F, 0x46,
  27117. 0x70, 0x33, 0x84, 0x69, 0x20, 0xFB, 0xB4, 0xB8,
  27118. 0x05, 0x44, 0xE7, 0xE8, 0xA8, 0x1A, 0xE9, 0x63,
  27119. 0xCF, 0x36, 0x8C, 0x9B, 0xA0, 0x37, 0xA8, 0xC2,
  27120. 0xAD, 0x62, 0xE3, 0x2B, 0x6E, 0x61, 0xC9, 0x1D,
  27121. 0x75, 0xCE, 0x00, 0x5A, 0xB3, 0x0F, 0x80, 0x99,
  27122. 0xA1, 0xF2, 0x9D, 0x7B, 0x63, 0x05, 0xB4, 0xDC,
  27123. 0x06, 0xE2, 0x56, 0x80, 0xBB, 0x00, 0x99, 0x2F,
  27124. 0x71, 0x7F, 0xE6, 0xC1, 0x15, 0xA8, 0x08, 0x42,
  27125. 0x31, 0xCC, 0x79, 0xDD, 0x70, 0x0E, 0xA6, 0x91,
  27126. 0x2A, 0xC7, 0xFA, 0x0D, 0x93, 0x7B, 0xB6, 0xA7,
  27127. 0x56, 0x66, 0x22, 0x30, 0x47, 0x0C, 0x18, 0x9B,
  27128. 0x5A, 0xA1, 0x65, 0x3D, 0xEB, 0x93, 0x7D, 0x5A,
  27129. 0x9C, 0x25, 0xA2, 0x1D, 0x93, 0xB1, 0x90, 0x74,
  27130. 0xFC, 0x23, 0x9D, 0x81, 0x53, 0x53, 0x97, 0x97,
  27131. 0xC7, 0xD4, 0xAB, 0x62, 0x64, 0x9D, 0x76, 0xAA,
  27132. 0x55, 0x37, 0x36, 0xA9, 0x49, 0x02, 0x2C, 0x22,
  27133. 0xC5, 0x2B, 0xAE, 0xEC, 0x60, 0x5B, 0x32, 0xCE,
  27134. 0x9E, 0x5B, 0x93, 0x84, 0x90, 0x35, 0x58, 0xCA,
  27135. 0x9D, 0x6A, 0x3A, 0xBA, 0x90, 0x42, 0x3E, 0xED,
  27136. 0xA0, 0x1C, 0x94, 0x19, 0x8B, 0x19, 0x2A, 0x8B,
  27137. 0xA9, 0x06, 0x34, 0x97, 0xA0, 0xC5, 0x01, 0x33,
  27138. 0x07, 0xDD, 0xD8, 0x63, 0x52, 0x64, 0x71, 0xA4,
  27139. 0xD9, 0x95, 0x23, 0xEB, 0x41, 0x7F, 0x29, 0x1A,
  27140. 0xAC, 0x0C, 0x3A, 0x58, 0x1B, 0x6D, 0xA0, 0x07,
  27141. 0x32, 0xE5, 0xE8, 0x1B, 0x1F, 0x7C, 0x87, 0x9B,
  27142. 0x16, 0x93, 0xC1, 0x3B, 0x6F, 0x9F, 0x79, 0x31,
  27143. 0x62, 0x24, 0x29, 0xE5, 0x42, 0xAF, 0x40, 0x69,
  27144. 0x22, 0x2F, 0x04, 0x55, 0x44, 0xE0, 0xCC, 0x4F,
  27145. 0xB2, 0x4D, 0x44, 0x48, 0xCF, 0x2C, 0x65, 0x96,
  27146. 0xF5, 0xCB, 0x08, 0x62, 0x4B, 0x11, 0x85, 0x01,
  27147. 0x3B, 0x6B, 0x02, 0x08, 0x92, 0xF9, 0x6B, 0xDF,
  27148. 0xD4, 0xAD, 0xA9, 0x17, 0x9D, 0xE7, 0x27, 0xB8,
  27149. 0xD9, 0x42, 0x6E, 0x09, 0x96, 0xB5, 0xD3, 0x49,
  27150. 0x48, 0xCE, 0x02, 0xD0, 0xC3, 0x69, 0xB3, 0x7C,
  27151. 0xBB, 0x54, 0xD3, 0x47, 0x9E, 0xD8, 0xB5, 0x82,
  27152. 0xE9, 0xE7, 0x28, 0x92, 0x9B, 0x4C, 0x71, 0xC9,
  27153. 0xBE, 0x11, 0xD4, 0x5B, 0x20, 0xC4, 0xBD, 0xC3,
  27154. 0xC7, 0x43, 0x13, 0x22, 0x3F, 0x58, 0x27, 0x4E,
  27155. 0x8B, 0xA5, 0x24, 0x44, 0x47, 0xC4, 0x95, 0x95,
  27156. 0x0B, 0x84, 0xCB, 0x0C, 0x3C, 0x27, 0x36, 0x40,
  27157. 0x10, 0x8A, 0x33, 0x97, 0x94, 0x45, 0x73, 0x27,
  27158. 0x93, 0x28, 0x99, 0x6C, 0xDC, 0x0C, 0x91, 0x3C,
  27159. 0x95, 0x8A, 0xD6, 0x20, 0xBA, 0x8B, 0x5E, 0x5E,
  27160. 0xCB, 0xBB, 0x7E, 0x13, 0xCB, 0x9C, 0x70, 0xBD,
  27161. 0x5A, 0xB3, 0x0E, 0xB7, 0x48, 0x8C, 0x97, 0x00,
  27162. 0x1C, 0x20, 0x49, 0x8F, 0x1D, 0x7C, 0xC0, 0x6D,
  27163. 0xA7, 0x6B, 0xF5, 0x20, 0xC6, 0x58, 0xCC, 0xAD,
  27164. 0xFA, 0x29, 0x56, 0x42, 0x45, 0x57, 0xAB, 0xEA,
  27165. 0x8A, 0xB8, 0x92, 0x39, 0xC1, 0x78, 0x33, 0xDC,
  27166. 0x3A, 0x49, 0xB3, 0x6A, 0x9A, 0xE9, 0xA4, 0x86,
  27167. 0x94, 0x05, 0x40, 0xEB, 0x44, 0x4F, 0x97, 0x15,
  27168. 0x23, 0x57, 0xE0, 0x20, 0x35, 0x93, 0x9D, 0x75,
  27169. 0xA3, 0xC0, 0x25, 0xF4, 0x1A, 0x40, 0x08, 0x23,
  27170. 0x82, 0xA0, 0x73, 0x3C, 0x39, 0xB0, 0x62, 0x2B,
  27171. 0x74, 0x0E, 0x40, 0x75, 0x92, 0xC6, 0x2E, 0xCA,
  27172. 0xEB, 0x14, 0x32, 0xC4, 0x45, 0xB3, 0x70, 0x3A,
  27173. 0x86, 0xF6, 0x98, 0x1A, 0x27, 0x81, 0x57, 0xEA,
  27174. 0x95, 0xA6, 0xE9, 0x2D, 0x55, 0xE4, 0xB9, 0x72,
  27175. 0xF9, 0x36, 0xC2, 0xF0, 0xA6, 0x58, 0x28, 0x0E,
  27176. 0xA2, 0xB0, 0x7A, 0x48, 0x99, 0x2D, 0xF8, 0x93,
  27177. 0x7E, 0x0A, 0x2A, 0xC1, 0xDC, 0xC9, 0x74, 0xFE,
  27178. 0x00, 0xAA, 0xE1, 0xF5, 0x61, 0xFA, 0x25, 0x8E,
  27179. 0x2D, 0x25, 0x9C, 0x3E, 0x86, 0x1D, 0xCE, 0x23,
  27180. 0x60, 0x39, 0x12, 0x76, 0x06, 0xFC, 0x1C, 0xE0,
  27181. 0x09, 0x00, 0x3A, 0x7B, 0xAC, 0x94, 0x21, 0x01,
  27182. 0xDC, 0xB8, 0x22, 0xB1, 0xF3, 0xC1, 0x2B, 0xF7,
  27183. 0x32, 0x38, 0xF5, 0x46, 0xE0, 0x1C, 0x36, 0xB5,
  27184. 0xA6, 0x93, 0x61, 0x92, 0x99, 0x5C, 0xC6, 0x9C,
  27185. 0x63, 0x23, 0x74, 0x09, 0xCB, 0x53, 0xC2, 0xE3,
  27186. 0x5D, 0x74, 0x89, 0x0D, 0x18, 0x88, 0x53, 0x76,
  27187. 0xFA, 0x55, 0x03, 0xB1, 0x07, 0xA2, 0xA3, 0x92,
  27188. 0x11, 0x5A, 0xCE, 0x0E, 0x64, 0x67, 0x7C, 0xBB,
  27189. 0x7D, 0xCF, 0xC9, 0x3C, 0x16, 0xD3, 0xA3, 0x05,
  27190. 0xF6, 0x76, 0x15, 0xA4, 0x88, 0xD7, 0x11, 0xAA,
  27191. 0x56, 0x69, 0x8C, 0x56, 0x63, 0xAB, 0x7A, 0xC9,
  27192. 0xCE, 0x66, 0xD5, 0x47, 0xC0, 0x59, 0x5F, 0x98,
  27193. 0xA4, 0x3F, 0x46, 0x50, 0xBB, 0xE0, 0x8C, 0x36,
  27194. 0x4D, 0x97, 0x67, 0x89, 0x11, 0x7D, 0x34, 0xF6,
  27195. 0xAE, 0x51, 0xAC, 0x06, 0x3C, 0xB5, 0x5C, 0x6C,
  27196. 0xA3, 0x25, 0x58, 0x22, 0x7D, 0xFE, 0xF8, 0x07,
  27197. 0xD1, 0x9C, 0x30, 0xDE, 0x41, 0x44, 0x24, 0x09,
  27198. 0x7F, 0x6A, 0xA2, 0x36, 0xA1, 0x05, 0x3B, 0x4A,
  27199. 0x07, 0xA7, 0x6B, 0xE3, 0x72, 0xA5, 0xC6, 0xB6,
  27200. 0x00, 0x27, 0x91, 0xEB, 0xE0, 0xAF, 0xDA, 0xF5,
  27201. 0x4E, 0x1C, 0xA2, 0x37, 0xFF, 0x54, 0x5B, 0xA6,
  27202. 0x83, 0x43, 0xE7, 0x45, 0xC0, 0x4A, 0xD1, 0x63,
  27203. 0x9D, 0xBC, 0x59, 0x03, 0x46, 0xB6, 0xB9, 0x56,
  27204. 0x9B, 0x56, 0xDB, 0xBF, 0xE5, 0x31, 0x51, 0x91,
  27205. 0x30, 0x66, 0xE5, 0xC8, 0x55, 0x27, 0xDC, 0x94,
  27206. 0x68, 0x11, 0x0A, 0x13, 0x6A, 0x41, 0x14, 0x97,
  27207. 0xC2, 0x27, 0xDC, 0xB8, 0xC9, 0xB2, 0x55, 0x70,
  27208. 0xB7, 0xA0, 0xE4, 0x2A, 0xAD, 0xA6, 0x70, 0x9F,
  27209. 0x23, 0x20, 0x8F, 0x5D, 0x49, 0x6E, 0xBA, 0xB7,
  27210. 0x84, 0x3F, 0x64, 0x83, 0xBF, 0x0C, 0x0C, 0x73,
  27211. 0xA4, 0x02, 0x96, 0xEC, 0x2C, 0x64, 0x40, 0x00,
  27212. 0x13, 0x94, 0xC9, 0x9C, 0xA1, 0x73, 0xD5, 0xC7,
  27213. 0x75, 0xB7, 0xF4, 0x15, 0xD0, 0x2A, 0x5A, 0x26,
  27214. 0xA0, 0x74, 0x07, 0x91, 0x85, 0x87, 0xC4, 0x11,
  27215. 0x69, 0xF2, 0xB7, 0x17, 0x87, 0x55, 0xAC, 0xC2,
  27216. 0x7F, 0xC8, 0xB1, 0x9C, 0x4C, 0x4B, 0x3F, 0xCD,
  27217. 0x41, 0x05, 0x3F, 0x2C, 0x74, 0xC8, 0xA1, 0x0A,
  27218. 0x83, 0x21, 0x24, 0x1B, 0x28, 0x02, 0x43, 0x28,
  27219. 0x75, 0xAE, 0x80, 0x8B, 0x9E, 0xF1, 0x36, 0x5C,
  27220. 0x7B, 0x8A, 0x52, 0x90, 0x2F, 0x13, 0x17, 0xBA,
  27221. 0x2F, 0xB0, 0x26, 0x9F, 0x47, 0x93, 0x06, 0x72,
  27222. 0x10, 0x7B, 0x47, 0x26, 0xFE, 0xF6, 0x45, 0x47,
  27223. 0x39, 0x4D, 0x33, 0x20, 0xC8, 0xF1, 0x20, 0xB3,
  27224. 0xC2, 0xF4, 0x72, 0x5B, 0x03, 0x05, 0xFA, 0xB8,
  27225. 0x8C, 0xC7, 0x98, 0x1F, 0xCB, 0x09, 0xA7, 0x6A,
  27226. 0x1C, 0xBF, 0x7F, 0x17, 0x9F, 0x43, 0xBB, 0x0A,
  27227. 0x4C, 0x8B, 0x05, 0x90, 0x85, 0x7F, 0x1E, 0x69,
  27228. 0x70, 0x84, 0x66, 0xC7, 0xF8, 0x60, 0x73, 0x91,
  27229. 0xE7, 0xBC, 0x52, 0x68, 0xBF, 0xD3, 0xD7, 0xA1,
  27230. 0xDF, 0xFC, 0xB4, 0xEC, 0xA2, 0xA1, 0xC9, 0xB5,
  27231. 0x97, 0x59, 0x30, 0x13, 0xD5, 0xFC, 0x42, 0x02,
  27232. 0xEC, 0x2B, 0x74, 0xE5, 0x7A, 0xB7, 0x6B, 0xBC,
  27233. 0xF3, 0x63, 0x2B, 0xBA, 0xF9, 0x7C, 0xDC, 0x41,
  27234. 0x8A, 0x6F, 0x16, 0x39, 0x28, 0x38, 0xCA, 0x9B,
  27235. 0xF4, 0x5D, 0xDF, 0x02, 0x37, 0x77, 0xB7, 0x56,
  27236. 0x18, 0x33, 0xC1, 0x05, 0x19, 0x0F, 0x94, 0xF3,
  27237. 0x02, 0xC5, 0x9B, 0x53, 0x19, 0x00, 0xBB, 0xC8,
  27238. 0x16, 0x36, 0x1F, 0xAA, 0x5B, 0x33, 0x80, 0xCA,
  27239. 0x3A, 0x89, 0x31, 0x04, 0xCA, 0x73, 0x88, 0xB1,
  27240. 0x85, 0x67, 0x1B, 0x3E, 0x5F, 0xE3, 0x79, 0x0E,
  27241. 0x9A, 0x62, 0x6E, 0xC4, 0x6D, 0x9B, 0x0B, 0x33,
  27242. 0xC7, 0xA4, 0x19, 0xAF, 0x7B, 0x32, 0xB6, 0x85,
  27243. 0x98, 0x94, 0xF5, 0x75, 0xD8, 0x2A, 0xC5, 0x45,
  27244. 0x6B, 0x54, 0x90, 0xA7, 0xAF, 0x8F, 0xE6, 0x10,
  27245. 0x46, 0x36, 0x05, 0x89, 0xEC, 0xBA, 0x72, 0x44,
  27246. 0x23, 0x6F, 0x41, 0x23, 0x11, 0x6B, 0x61, 0x74,
  27247. 0xAA, 0x17, 0x92, 0x49, 0xA4, 0x91, 0x95, 0xB3,
  27248. 0x56, 0xC7, 0x2F, 0xC6, 0x64, 0x1F, 0x02, 0x51,
  27249. 0x81, 0x2E, 0xAA, 0x98, 0x57, 0x0B, 0x04, 0x66,
  27250. 0x99, 0x07, 0x0E, 0x08, 0x19, 0xDC, 0x27, 0x13,
  27251. 0xF4, 0x69, 0x13, 0x7D, 0xFC, 0x6A, 0x3D, 0x7B,
  27252. 0x92, 0xB2, 0x98, 0x99, 0x5E, 0xE7, 0x80, 0x36,
  27253. 0x91, 0x53, 0xAC, 0x36, 0x6B, 0x06, 0xD7, 0x24,
  27254. 0x9C, 0xD0, 0x9E, 0x1B, 0x33, 0x78, 0xFB, 0x04,
  27255. 0x39, 0x9C, 0xEC, 0xB8, 0x65, 0x05, 0x81, 0xD6,
  27256. 0x37, 0xC7, 0x9A, 0xE6, 0x7D, 0x6F, 0x2C, 0xAF,
  27257. 0x6A, 0xBA, 0xCF, 0x59, 0x81, 0x59, 0xA7, 0x79,
  27258. 0x2C, 0xB3, 0xC9, 0x71, 0xD1, 0x49, 0x9D, 0x23,
  27259. 0x73, 0xAD, 0x20, 0xF6, 0x3F, 0x03, 0xBB, 0x59,
  27260. 0xED, 0x13, 0x73, 0x84, 0xAC, 0x61, 0xA7, 0x15,
  27261. 0x51, 0x43, 0xB8, 0xCA, 0x49, 0x32, 0x61, 0x2E,
  27262. 0xC9, 0x15, 0xE4, 0xCA, 0x34, 0x6A, 0x9B, 0xCE,
  27263. 0x5D, 0xD6, 0x04, 0x17, 0xC6, 0xB2, 0xA8, 0x9B,
  27264. 0x1C, 0xC4, 0x35, 0x64, 0x3F, 0x87, 0x5B, 0xDC,
  27265. 0x5A, 0x7E, 0x5B, 0x34, 0x81, 0xCF, 0x91, 0x9E,
  27266. 0xA0, 0x91, 0x72, 0xFE, 0xBC, 0x46, 0xD4, 0xFC,
  27267. 0x3F, 0xB0, 0xCB, 0x95, 0x91, 0x70, 0x4E, 0xE2,
  27268. 0xDB, 0xB6, 0x18, 0x44, 0xB2, 0xF3, 0x31, 0x4A,
  27269. 0x06, 0xBB, 0x6C, 0x6D, 0x34, 0x00, 0x5E, 0x48,
  27270. 0x5C, 0xE6, 0x67, 0xBD, 0xC7, 0xD0, 0x98, 0x58,
  27271. 0x69, 0x28, 0xD2, 0xD9, 0x13, 0x40, 0xF0, 0x04,
  27272. 0x19, 0xEA, 0x40, 0x13, 0x51, 0xA2, 0x40, 0xA0,
  27273. 0xB0, 0x41, 0x05, 0x8B, 0xEF, 0xB0, 0xC2, 0xFD,
  27274. 0x32, 0x64, 0x5B, 0x7A, 0x2D, 0xF8, 0xF5, 0xCB,
  27275. 0xFD, 0x87, 0x33, 0x27, 0xC9, 0x78, 0xD7, 0xB3,
  27276. 0x51, 0xA2, 0x80, 0x88, 0x43, 0x88, 0x37, 0x02,
  27277. 0x4C, 0x52, 0xB9, 0xC2, 0x95, 0xCD, 0x71, 0x36,
  27278. 0x46, 0xFB, 0x5D, 0x6C, 0x0C, 0xCF, 0xB4, 0x70,
  27279. 0x73, 0x4A, 0xC2, 0xB2, 0xBC, 0x81, 0x23, 0xC2,
  27280. 0xC1, 0x3D, 0xF6, 0x93, 0x8E, 0x92, 0x45, 0x5A,
  27281. 0x86, 0x26, 0x39, 0xFE, 0xB8, 0xA6, 0x4B, 0x85,
  27282. 0x16, 0x3E, 0x32, 0x70, 0x7E, 0x03, 0x7B, 0x38,
  27283. 0xD8, 0xAC, 0x39, 0x22, 0xB4, 0x51, 0x87, 0xBB,
  27284. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  27285. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  27286. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  27287. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  27288. 0x7F, 0xFA, 0xD1, 0xBC, 0x8A, 0xF7, 0x3B, 0x7E,
  27289. 0x87, 0x49, 0x56, 0xB8, 0x1C, 0x2A, 0x2E, 0xF0,
  27290. 0xBF, 0xAB, 0xE8, 0xDC, 0x93, 0xD7, 0x7B, 0x2F,
  27291. 0xBC, 0x9E, 0x0C, 0x64, 0xEF, 0xA0, 0x1E, 0x84,
  27292. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  27293. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  27294. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  27295. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  27296. };
  27297. const byte kyber512_ct[] = {
  27298. 0xED, 0xF2, 0x41, 0x45, 0xE4, 0x3B, 0x4F, 0x6D,
  27299. 0xC6, 0xBF, 0x83, 0x32, 0xF5, 0x4E, 0x02, 0xCA,
  27300. 0xB0, 0x2D, 0xBF, 0x3B, 0x56, 0x05, 0xDD, 0xC9,
  27301. 0x0A, 0x15, 0xC8, 0x86, 0xAD, 0x3E, 0xD4, 0x89,
  27302. 0x46, 0x26, 0x99, 0xE4, 0xAB, 0xED, 0x44, 0x35,
  27303. 0x0B, 0xC3, 0x75, 0x7E, 0x26, 0x96, 0xFB, 0xFB,
  27304. 0x25, 0x34, 0x41, 0x2E, 0x8D, 0xD2, 0x01, 0xF1,
  27305. 0xE4, 0x54, 0x0A, 0x39, 0x70, 0xB0, 0x55, 0xFE,
  27306. 0x3B, 0x0B, 0xEC, 0x3A, 0x71, 0xF9, 0xE1, 0x15,
  27307. 0xB3, 0xF9, 0xF3, 0x91, 0x02, 0x06, 0x5B, 0x1C,
  27308. 0xCA, 0x83, 0x14, 0xDC, 0xC7, 0x95, 0xE3, 0xC0,
  27309. 0xE8, 0xFA, 0x98, 0xEE, 0x83, 0xCA, 0x66, 0x28,
  27310. 0x45, 0x70, 0x28, 0xA4, 0xD0, 0x9E, 0x83, 0x9E,
  27311. 0x55, 0x48, 0x62, 0xCF, 0x0B, 0x7B, 0xF5, 0x6C,
  27312. 0x5C, 0x0A, 0x82, 0x9E, 0x86, 0x57, 0x94, 0x79,
  27313. 0x45, 0xFE, 0x9C, 0x22, 0x56, 0x4F, 0xBA, 0xEB,
  27314. 0xC1, 0xB3, 0xAF, 0x35, 0x0D, 0x79, 0x55, 0x50,
  27315. 0x8A, 0x26, 0xD8, 0xA8, 0xEB, 0x54, 0x7B, 0x8B,
  27316. 0x1A, 0x2C, 0xF0, 0x3C, 0xCA, 0x1A, 0xAB, 0xCE,
  27317. 0x6C, 0x34, 0x97, 0x78, 0x3B, 0x64, 0x65, 0xBA,
  27318. 0x0B, 0x6E, 0x7A, 0xCB, 0xA8, 0x21, 0x19, 0x51,
  27319. 0x24, 0xAE, 0xF0, 0x9E, 0x62, 0x83, 0x82, 0xA1,
  27320. 0xF9, 0x14, 0x04, 0x3B, 0xE7, 0x09, 0x6E, 0x95,
  27321. 0x2C, 0xBC, 0x4F, 0xB4, 0xAF, 0xED, 0x13, 0x60,
  27322. 0x90, 0x46, 0x11, 0x7C, 0x01, 0x1F, 0xD7, 0x41,
  27323. 0xEE, 0x28, 0x6C, 0x83, 0x77, 0x16, 0x90, 0xF0,
  27324. 0xAE, 0xB5, 0x0D, 0xA0, 0xD7, 0x12, 0x85, 0xA1,
  27325. 0x79, 0xB2, 0x15, 0xC6, 0x03, 0x6D, 0xEB, 0x78,
  27326. 0x0F, 0x4D, 0x16, 0x76, 0x9F, 0x72, 0xDE, 0x16,
  27327. 0xFD, 0xAD, 0xAC, 0x73, 0xBE, 0xFA, 0x5B, 0xEF,
  27328. 0x89, 0x43, 0x19, 0x7F, 0x44, 0xC5, 0x95, 0x89,
  27329. 0xDC, 0x9F, 0x49, 0x73, 0xDE, 0x14, 0x50, 0xBA,
  27330. 0x1D, 0x0C, 0x32, 0x90, 0xD6, 0xB1, 0xD6, 0x83,
  27331. 0xF2, 0x94, 0xE7, 0x59, 0xC9, 0x54, 0xAB, 0xE8,
  27332. 0xA7, 0xDA, 0x5B, 0x10, 0x54, 0xFD, 0x6D, 0x21,
  27333. 0x32, 0x9B, 0x8E, 0x73, 0xD3, 0x75, 0x6A, 0xFD,
  27334. 0xA0, 0xDC, 0xB1, 0xFC, 0x8B, 0x15, 0x82, 0xD1,
  27335. 0xF9, 0x0C, 0xF2, 0x75, 0xA1, 0x02, 0xAB, 0xC6,
  27336. 0xAC, 0x69, 0x9D, 0xF0, 0xC5, 0x87, 0x0E, 0x50,
  27337. 0xA1, 0xF9, 0x89, 0xE4, 0xE6, 0x24, 0x1B, 0x60,
  27338. 0xAA, 0xA2, 0xEC, 0xF9, 0xE8, 0xE3, 0x3E, 0x0F,
  27339. 0xFC, 0xF4, 0x0F, 0xE8, 0x31, 0xE8, 0xFD, 0xC2,
  27340. 0xE8, 0x3B, 0x52, 0xCA, 0x7A, 0xB6, 0xD9, 0x3F,
  27341. 0x14, 0x6D, 0x29, 0xDC, 0xA5, 0x3C, 0x7D, 0xA1,
  27342. 0xDB, 0x4A, 0xC4, 0xF2, 0xDB, 0x39, 0xEA, 0x12,
  27343. 0x0D, 0x90, 0xFA, 0x60, 0xF4, 0xD4, 0x37, 0xC6,
  27344. 0xD0, 0x0E, 0xF4, 0x83, 0xBC, 0x94, 0xA3, 0x17,
  27345. 0x5C, 0xDA, 0x16, 0x3F, 0xC1, 0xC2, 0x82, 0x8B,
  27346. 0xE4, 0xDB, 0xD6, 0x43, 0x05, 0x07, 0xB5, 0x84,
  27347. 0xBB, 0x51, 0x77, 0xE1, 0x71, 0xB8, 0xDD, 0xA9,
  27348. 0xA4, 0x29, 0x3C, 0x32, 0x00, 0x29, 0x5C, 0x80,
  27349. 0x3A, 0x86, 0x5D, 0x6D, 0x21, 0x66, 0xF6, 0x6B,
  27350. 0xA5, 0x40, 0x1F, 0xB7, 0xA0, 0xE8, 0x53, 0x16,
  27351. 0x86, 0x00, 0xA2, 0x94, 0x84, 0x37, 0xE0, 0x36,
  27352. 0xE3, 0xBF, 0x19, 0xE1, 0x2F, 0xD3, 0xF2, 0xA2,
  27353. 0xB8, 0xB3, 0x43, 0xF7, 0x84, 0x24, 0x8E, 0x8D,
  27354. 0x68, 0x5E, 0xB0, 0xAF, 0xDE, 0x63, 0x15, 0x33,
  27355. 0x87, 0x30, 0xE7, 0xA1, 0x00, 0x1C, 0x27, 0xD8,
  27356. 0xD2, 0xA7, 0x6F, 0xA6, 0x9D, 0x15, 0x7B, 0xA1,
  27357. 0xAC, 0x7A, 0xD5, 0x6D, 0xA5, 0xA8, 0xC7, 0x0F,
  27358. 0xE4, 0xB5, 0xB8, 0xD7, 0x86, 0xDC, 0x6F, 0xC0,
  27359. 0x56, 0x6B, 0xA8, 0xE1, 0xB8, 0x81, 0x63, 0x34,
  27360. 0xD3, 0x2A, 0x3F, 0xB1, 0xCE, 0x7D, 0x4D, 0x5E,
  27361. 0x4C, 0x33, 0x2A, 0xF7, 0xB0, 0x03, 0xD0, 0x91,
  27362. 0x74, 0x1A, 0x3D, 0x5C, 0x96, 0x52, 0x92, 0x25,
  27363. 0x5D, 0xFF, 0x8E, 0xD2, 0xBB, 0xF1, 0xF9, 0x11,
  27364. 0x6B, 0xE5, 0x0C, 0x17, 0xB8, 0xE5, 0x48, 0x74,
  27365. 0x8A, 0xD4, 0xB2, 0xE9, 0x57, 0xBB, 0xD1, 0x95,
  27366. 0x34, 0x82, 0xA2, 0xE1, 0x71, 0x8C, 0xEC, 0x66,
  27367. 0xCD, 0x2C, 0x81, 0xF5, 0x72, 0xD5, 0x52, 0xB7,
  27368. 0x18, 0x78, 0x85, 0xE6, 0xB8, 0x94, 0x3D, 0x64,
  27369. 0x31, 0x41, 0x3C, 0x59, 0xEB, 0xB7, 0xE0, 0x36,
  27370. 0x04, 0x84, 0x90, 0xBE, 0x52, 0x89, 0xE9, 0x5B,
  27371. 0x20, 0xA8, 0x9E, 0x8B, 0x15, 0x9F, 0x61, 0xA9,
  27372. 0xA9, 0x88, 0x6E, 0x14, 0x75, 0x68, 0xF4, 0xC9,
  27373. 0x02, 0x1F, 0x36, 0x2F, 0x02, 0x68, 0x8A, 0x1C,
  27374. 0x8C, 0x3B, 0xB0, 0xD2, 0x40, 0x86, 0x88, 0x0E,
  27375. 0x55, 0xB6, 0xED, 0xB4, 0x3F, 0x37, 0x45, 0xD2,
  27376. 0xC1, 0x66, 0xDC, 0x1C, 0xB7, 0x43, 0xC7, 0x6F,
  27377. 0xE6, 0xBE, 0x52, 0x3A, 0x89, 0x3C, 0xC7, 0x64,
  27378. 0xD1, 0x64, 0x35, 0xC3, 0x78, 0x51, 0x25, 0x2A,
  27379. 0x81, 0xE2, 0xFF, 0xBA, 0x0F, 0x18, 0x97, 0x1A,
  27380. 0x3D, 0xEE, 0x37, 0xD4, 0x87, 0x7C, 0xB9, 0x28,
  27381. 0xE3, 0x6E, 0x52, 0x35, 0x03, 0x7A, 0x6B, 0x20,
  27382. 0x57, 0x89, 0x7D, 0x51, 0x8A, 0x5F, 0x0E, 0x34,
  27383. 0x8E, 0x3A, 0xB6, 0xD5, 0xB5, 0x2D, 0xFC, 0x60,
  27384. 0x75, 0x7F, 0x3B, 0x41, 0xA4, 0xFE, 0xC7, 0x82,
  27385. 0x8F, 0x1D, 0xEE, 0xAF, 0x45, 0x87, 0xCC, 0xC8,
  27386. 0xEA, 0xDF, 0x64, 0x7F, 0x4D, 0x20, 0x3B, 0x2F,
  27387. 0xAA, 0x05, 0xA6, 0x49, 0xB5, 0x82, 0x34, 0x0C,
  27388. 0xB4, 0xCA, 0xCE, 0x57, 0xA3, 0x07, 0x11, 0xBE,
  27389. 0x75, 0x2F, 0xAC, 0xF0, 0x22, 0x7D, 0x0A, 0x80,
  27390. 0xC4, 0x12, 0x84, 0x42, 0xDD, 0xC5, 0x44, 0xBE,
  27391. 0x80, 0x5B, 0x9C, 0xFE, 0x8F, 0xE9, 0xB1, 0x23,
  27392. 0x7C, 0x80, 0xF9, 0x67, 0x87, 0xCD, 0x92, 0x81,
  27393. 0xCC, 0xF2, 0x70, 0xC1, 0xAF, 0xC0, 0x67, 0x0D
  27394. };
  27395. const byte kyber512_ss[] = {
  27396. 0x0A, 0x69, 0x25, 0x67, 0x6F, 0x24, 0xB2, 0x2C,
  27397. 0x28, 0x6F, 0x4C, 0x81, 0xA4, 0x22, 0x4C, 0xEC,
  27398. 0x50, 0x6C, 0x9B, 0x25, 0x7D, 0x48, 0x0E, 0x02,
  27399. 0xE3, 0xB4, 0x9F, 0x44, 0xCA, 0xA3, 0x23, 0x7F
  27400. };
  27401. ret = wc_KyberKey_Init(KYBER512, &key, HEAP_HINT, INVALID_DEVID);
  27402. if (ret != 0)
  27403. return -20101;
  27404. ret = wc_KyberKey_MakeKeyWithRandom(&key, kyber512_rand,
  27405. sizeof(kyber512_rand));
  27406. if (ret != 0)
  27407. return -20102;
  27408. ret = wc_KyberKey_EncodePublicKey(&key, pub, sizeof(pub));
  27409. if (ret != 0)
  27410. return -20103;
  27411. ret = wc_KyberKey_EncodePrivateKey(&key, priv, sizeof(priv));
  27412. if (ret != 0)
  27413. return -20104;
  27414. if (XMEMCMP(pub, kyber512_pk, sizeof(kyber512_pk)) != 0)
  27415. return -20105;
  27416. if (XMEMCMP(priv, kyber512_sk, sizeof(kyber512_sk)) != 0)
  27417. return -20106;
  27418. ret = wc_KyberKey_EncapsulateWithRandom(&key, ct, ss, kyber512enc_rand,
  27419. sizeof(kyber512enc_rand));
  27420. if (ret != 0)
  27421. return -20107;
  27422. if (XMEMCMP(ct, kyber512_ct, sizeof(kyber512_ct)) != 0)
  27423. return -20108;
  27424. if (XMEMCMP(ss, kyber512_ss, sizeof(kyber512_ss)) != 0)
  27425. return -20109;
  27426. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, sizeof(kyber512_ct));
  27427. if (ret != 0)
  27428. return -20110;
  27429. if (XMEMCMP(ss_dec, kyber512_ss, sizeof(kyber512_ss)) != 0)
  27430. return -20111;
  27431. wc_KyberKey_Free(&key);
  27432. return 0;
  27433. }
  27434. #endif /* WOLFSSL_KYBER512 */
  27435. #ifdef WOLFSSL_KYBER768
  27436. static int kyber768_kat(void)
  27437. {
  27438. KyberKey key;
  27439. int ret;
  27440. byte priv[KYBER768_PRIVATE_KEY_SIZE];
  27441. byte pub[KYBER768_PUBLIC_KEY_SIZE];
  27442. byte ct[KYBER768_CIPHER_TEXT_SIZE];
  27443. byte ss[KYBER_SS_SZ];
  27444. byte ss_dec[KYBER_SS_SZ];
  27445. const byte kyber768_rand[] = {
  27446. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  27447. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  27448. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  27449. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  27450. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  27451. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  27452. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  27453. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  27454. };
  27455. const byte kyber768enc_rand[] = {
  27456. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  27457. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  27458. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  27459. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  27460. };
  27461. const byte kyber768_pk[] = {
  27462. 0xA7, 0x2C, 0x2D, 0x9C, 0x84, 0x3E, 0xE9, 0xF8,
  27463. 0x31, 0x3E, 0xCC, 0x7F, 0x86, 0xD6, 0x29, 0x4D,
  27464. 0x59, 0x15, 0x9D, 0x9A, 0x87, 0x9A, 0x54, 0x2E,
  27465. 0x26, 0x09, 0x22, 0xAD, 0xF9, 0x99, 0x05, 0x1C,
  27466. 0xC4, 0x52, 0x00, 0xC9, 0xFF, 0xDB, 0x60, 0x44,
  27467. 0x9C, 0x49, 0x46, 0x59, 0x79, 0x27, 0x23, 0x67,
  27468. 0xC0, 0x83, 0xA7, 0xD6, 0x26, 0x7A, 0x3E, 0xD7,
  27469. 0xA7, 0xFD, 0x47, 0x95, 0x7C, 0x21, 0x93, 0x27,
  27470. 0xF7, 0xCA, 0x73, 0xA4, 0x00, 0x7E, 0x16, 0x27,
  27471. 0xF0, 0x0B, 0x11, 0xCC, 0x80, 0x57, 0x3C, 0x15,
  27472. 0xAE, 0xE6, 0x64, 0x0F, 0xB8, 0x56, 0x2D, 0xFA,
  27473. 0x6B, 0x24, 0x0C, 0xA0, 0xAD, 0x35, 0x1A, 0xC4,
  27474. 0xAC, 0x15, 0x5B, 0x96, 0xC1, 0x4C, 0x8A, 0xB1,
  27475. 0x3D, 0xD2, 0x62, 0xCD, 0xFD, 0x51, 0xC4, 0xBB,
  27476. 0x55, 0x72, 0xFD, 0x61, 0x65, 0x53, 0xD1, 0x7B,
  27477. 0xDD, 0x43, 0x0A, 0xCB, 0xEA, 0x3E, 0x95, 0xF0,
  27478. 0xB6, 0x98, 0xD6, 0x69, 0x90, 0xAB, 0x51, 0xE5,
  27479. 0xD0, 0x37, 0x83, 0xA8, 0xB3, 0xD2, 0x78, 0xA5,
  27480. 0x72, 0x04, 0x54, 0xCF, 0x96, 0x95, 0xCF, 0xDC,
  27481. 0xA0, 0x84, 0x85, 0xBA, 0x09, 0x9C, 0x51, 0xCD,
  27482. 0x92, 0xA7, 0xEA, 0x75, 0x87, 0xC1, 0xD1, 0x5C,
  27483. 0x28, 0xE6, 0x09, 0xA8, 0x18, 0x52, 0x60, 0x1B,
  27484. 0x06, 0x04, 0x01, 0x06, 0x79, 0xAA, 0x48, 0x2D,
  27485. 0x51, 0x26, 0x1E, 0xC3, 0x6E, 0x36, 0xB8, 0x71,
  27486. 0x96, 0x76, 0x21, 0x7F, 0xD7, 0x4C, 0x54, 0x78,
  27487. 0x64, 0x88, 0xF4, 0xB4, 0x96, 0x9C, 0x05, 0xA8,
  27488. 0xBA, 0x27, 0xCA, 0x3A, 0x77, 0xCC, 0xE7, 0x3B,
  27489. 0x96, 0x59, 0x23, 0xCA, 0x55, 0x4E, 0x42, 0x2B,
  27490. 0x9B, 0x61, 0xF4, 0x75, 0x46, 0x41, 0x60, 0x8A,
  27491. 0xC1, 0x6C, 0x9B, 0x85, 0x87, 0xA3, 0x2C, 0x1C,
  27492. 0x5D, 0xD7, 0x88, 0xF8, 0x8B, 0x36, 0xB7, 0x17,
  27493. 0xA4, 0x69, 0x65, 0x63, 0x5D, 0xEB, 0x67, 0xF4,
  27494. 0x5B, 0x12, 0x9B, 0x99, 0x07, 0x09, 0x09, 0xC9,
  27495. 0x3E, 0xB8, 0x0B, 0x42, 0xC2, 0xB3, 0xF3, 0xF7,
  27496. 0x03, 0x43, 0xA7, 0xCF, 0x37, 0xE8, 0x52, 0x0E,
  27497. 0x7B, 0xCF, 0xC4, 0x16, 0xAC, 0xA4, 0xF1, 0x8C,
  27498. 0x79, 0x81, 0x26, 0x2B, 0xA2, 0xBF, 0xC7, 0x56,
  27499. 0xAE, 0x03, 0x27, 0x8F, 0x0E, 0xC6, 0x6D, 0xC2,
  27500. 0x05, 0x76, 0x96, 0x82, 0x4B, 0xA6, 0x76, 0x98,
  27501. 0x65, 0xA6, 0x01, 0xD7, 0x14, 0x8E, 0xF6, 0xF5,
  27502. 0x4E, 0x5A, 0xF5, 0x68, 0x6A, 0xA2, 0x90, 0x6F,
  27503. 0x99, 0x4C, 0xE3, 0x8A, 0x5E, 0x0B, 0x93, 0x8F,
  27504. 0x23, 0x90, 0x07, 0x00, 0x30, 0x22, 0xC0, 0x33,
  27505. 0x92, 0xDF, 0x34, 0x01, 0xB1, 0xE4, 0xA3, 0xA7,
  27506. 0xEB, 0xC6, 0x16, 0x14, 0x49, 0xF7, 0x33, 0x74,
  27507. 0xC8, 0xB0, 0x14, 0x03, 0x69, 0x34, 0x3D, 0x92,
  27508. 0x95, 0xFD, 0xF5, 0x11, 0x84, 0x5C, 0x4A, 0x46,
  27509. 0xEB, 0xAA, 0xB6, 0xCA, 0x54, 0x92, 0xF6, 0x80,
  27510. 0x0B, 0x98, 0xC0, 0xCC, 0x80, 0x36, 0x53, 0xA4,
  27511. 0xB1, 0xD6, 0xE6, 0xAA, 0xED, 0x19, 0x32, 0xBA,
  27512. 0xCC, 0x5F, 0xEF, 0xAA, 0x81, 0x8B, 0xA5, 0x02,
  27513. 0x85, 0x9B, 0xA5, 0x49, 0x4C, 0x5F, 0x54, 0x02,
  27514. 0xC8, 0x53, 0x6A, 0x9C, 0x4C, 0x18, 0x88, 0x15,
  27515. 0x06, 0x17, 0xF8, 0x00, 0x98, 0xF6, 0xB2, 0xA9,
  27516. 0x9C, 0x39, 0xBC, 0x5D, 0xC7, 0xCF, 0x3B, 0x59,
  27517. 0x00, 0xA2, 0x13, 0x29, 0xAB, 0x59, 0x05, 0x3A,
  27518. 0xBA, 0xA6, 0x4E, 0xD1, 0x63, 0xE8, 0x59, 0xA8,
  27519. 0xB3, 0xB3, 0xCA, 0x33, 0x59, 0xB7, 0x50, 0xCC,
  27520. 0xC3, 0xE7, 0x10, 0xC7, 0xAC, 0x43, 0xC8, 0x19,
  27521. 0x1C, 0xB5, 0xD6, 0x88, 0x70, 0xC0, 0x63, 0x91,
  27522. 0xC0, 0xCB, 0x8A, 0xEC, 0x72, 0xB8, 0x97, 0xAC,
  27523. 0x6B, 0xE7, 0xFB, 0xAA, 0xCC, 0x67, 0x6E, 0xD6,
  27524. 0x63, 0x14, 0xC8, 0x36, 0x30, 0xE8, 0x94, 0x48,
  27525. 0xC8, 0x8A, 0x1D, 0xF0, 0x4A, 0xCE, 0xB2, 0x3A,
  27526. 0xBF, 0x2E, 0x40, 0x9E, 0xF3, 0x33, 0xC6, 0x22,
  27527. 0x28, 0x9C, 0x18, 0xA2, 0x13, 0x4E, 0x65, 0x0C,
  27528. 0x45, 0x25, 0x7E, 0x47, 0x47, 0x5F, 0xA3, 0x3A,
  27529. 0xA5, 0x37, 0xA5, 0xA8, 0xF7, 0x68, 0x02, 0x14,
  27530. 0x71, 0x6C, 0x50, 0xD4, 0x70, 0xE3, 0x28, 0x49,
  27531. 0x63, 0xCA, 0x64, 0xF5, 0x46, 0x77, 0xAE, 0xC5,
  27532. 0x4B, 0x52, 0x72, 0x16, 0x2B, 0xF5, 0x2B, 0xC8,
  27533. 0x14, 0x2E, 0x1D, 0x41, 0x83, 0xFC, 0x01, 0x74,
  27534. 0x54, 0xA6, 0xB5, 0xA4, 0x96, 0x83, 0x17, 0x59,
  27535. 0x06, 0x40, 0x24, 0x74, 0x59, 0x78, 0xCB, 0xD5,
  27536. 0x1A, 0x6C, 0xED, 0xC8, 0x95, 0x5D, 0xE4, 0xCC,
  27537. 0x6D, 0x36, 0x36, 0x70, 0xA4, 0x74, 0x66, 0xE8,
  27538. 0x2B, 0xE5, 0xC2, 0x36, 0x03, 0xA1, 0x7B, 0xF2,
  27539. 0x2A, 0xCD, 0xB7, 0xCC, 0x98, 0x4A, 0xF0, 0x8C,
  27540. 0x87, 0xE1, 0x4E, 0x27, 0x75, 0x3C, 0xF5, 0x87,
  27541. 0xA8, 0xEC, 0x34, 0x47, 0xE6, 0x2C, 0x64, 0x9E,
  27542. 0x88, 0x7A, 0x67, 0xC3, 0x6C, 0x9C, 0xE9, 0x87,
  27543. 0x21, 0xB6, 0x97, 0x21, 0x32, 0x75, 0x64, 0x6B,
  27544. 0x19, 0x4F, 0x36, 0x75, 0x86, 0x73, 0xA8, 0xED,
  27545. 0x11, 0x28, 0x44, 0x55, 0xAF, 0xC7, 0xA8, 0x52,
  27546. 0x9F, 0x69, 0xC9, 0x7A, 0x3C, 0x2D, 0x7B, 0x8C,
  27547. 0x63, 0x6C, 0x0B, 0xA5, 0x56, 0x14, 0xB7, 0x68,
  27548. 0xE6, 0x24, 0xE7, 0x12, 0x93, 0x0F, 0x77, 0x61,
  27549. 0x69, 0xB0, 0x17, 0x15, 0x72, 0x53, 0x51, 0xBC,
  27550. 0x74, 0xB4, 0x73, 0x95, 0xED, 0x52, 0xB2, 0x5A,
  27551. 0x13, 0x13, 0xC9, 0x51, 0x64, 0x81, 0x4C, 0x34,
  27552. 0xC9, 0x79, 0xCB, 0xDF, 0xAB, 0x85, 0x95, 0x46,
  27553. 0x62, 0xCA, 0xB4, 0x85, 0xE7, 0x50, 0x87, 0xA9,
  27554. 0x8C, 0xC7, 0x4B, 0xB8, 0x2C, 0xA2, 0xD1, 0xB5,
  27555. 0xBF, 0x28, 0x03, 0x23, 0x84, 0x80, 0x63, 0x8C,
  27556. 0x40, 0xE9, 0x0B, 0x43, 0xC7, 0x46, 0x0E, 0x7A,
  27557. 0xA9, 0x17, 0xF0, 0x10, 0x15, 0x1F, 0xAB, 0x11,
  27558. 0x69, 0x98, 0x7B, 0x37, 0x2A, 0xBB, 0x59, 0x27,
  27559. 0x1F, 0x70, 0x06, 0xC2, 0x4E, 0x60, 0x23, 0x6B,
  27560. 0x84, 0xB9, 0xDD, 0xD6, 0x00, 0x62, 0x37, 0x04,
  27561. 0x25, 0x46, 0x17, 0xFB, 0x49, 0x8D, 0x89, 0xE5,
  27562. 0x8B, 0x03, 0x68, 0xBC, 0xB2, 0x10, 0x3E, 0x79,
  27563. 0x35, 0x3E, 0xB5, 0x87, 0x86, 0x0C, 0x14, 0x22,
  27564. 0xE4, 0x76, 0x16, 0x2E, 0x42, 0x5B, 0xC2, 0x38,
  27565. 0x1D, 0xB8, 0x2C, 0x65, 0x92, 0x73, 0x7E, 0x1D,
  27566. 0xD6, 0x02, 0x86, 0x4B, 0x01, 0x67, 0xA7, 0x1E,
  27567. 0xC1, 0xF2, 0x23, 0x30, 0x5C, 0x02, 0xFE, 0x25,
  27568. 0x05, 0x2A, 0xF2, 0xB3, 0xB5, 0xA5, 0x5A, 0x0D,
  27569. 0x7A, 0x20, 0x22, 0xD9, 0xA7, 0x98, 0xDC, 0x0C,
  27570. 0x58, 0x74, 0xA9, 0x87, 0x02, 0xAA, 0xF4, 0x05,
  27571. 0x4C, 0x5D, 0x80, 0x33, 0x8A, 0x52, 0x48, 0xB5,
  27572. 0xB7, 0xBD, 0x09, 0xC5, 0x3B, 0x5E, 0x2A, 0x08,
  27573. 0x4B, 0x04, 0x7D, 0x27, 0x7A, 0x86, 0x1B, 0x1A,
  27574. 0x73, 0xBB, 0x51, 0x48, 0x8D, 0xE0, 0x4E, 0xF5,
  27575. 0x73, 0xC8, 0x52, 0x30, 0xA0, 0x47, 0x0B, 0x73,
  27576. 0x17, 0x5C, 0x9F, 0xA5, 0x05, 0x94, 0xF6, 0x6A,
  27577. 0x5F, 0x50, 0xB4, 0x15, 0x00, 0x54, 0xC9, 0x3B,
  27578. 0x68, 0x18, 0x6F, 0x8B, 0x5C, 0xBC, 0x49, 0x31,
  27579. 0x6C, 0x85, 0x48, 0xA6, 0x42, 0xB2, 0xB3, 0x6A,
  27580. 0x1D, 0x45, 0x4C, 0x74, 0x89, 0xAC, 0x33, 0xB2,
  27581. 0xD2, 0xCE, 0x66, 0x68, 0x09, 0x67, 0x82, 0xA2,
  27582. 0xC1, 0xE0, 0x86, 0x6D, 0x21, 0xA6, 0x5E, 0x16,
  27583. 0xB5, 0x85, 0xE7, 0xAF, 0x86, 0x18, 0xBD, 0xF3,
  27584. 0x18, 0x4C, 0x19, 0x86, 0x87, 0x85, 0x08, 0x91,
  27585. 0x72, 0x77, 0xB9, 0x3E, 0x10, 0x70, 0x6B, 0x16,
  27586. 0x14, 0x97, 0x2B, 0x2A, 0x94, 0xC7, 0x31, 0x0F,
  27587. 0xE9, 0xC7, 0x08, 0xC2, 0x31, 0xA1, 0xA8, 0xAC,
  27588. 0x8D, 0x93, 0x14, 0xA5, 0x29, 0xA9, 0x7F, 0x46,
  27589. 0x9B, 0xF6, 0x49, 0x62, 0xD8, 0x20, 0x64, 0x84,
  27590. 0x43, 0x09, 0x9A, 0x07, 0x6D, 0x55, 0xD4, 0xCE,
  27591. 0xA8, 0x24, 0xA5, 0x83, 0x04, 0x84, 0x4F, 0x99,
  27592. 0x49, 0x7C, 0x10, 0xA2, 0x51, 0x48, 0x61, 0x8A,
  27593. 0x31, 0x5D, 0x72, 0xCA, 0x85, 0x7D, 0x1B, 0x04,
  27594. 0xD5, 0x75, 0xB9, 0x4F, 0x85, 0xC0, 0x1D, 0x19,
  27595. 0xBE, 0xF2, 0x11, 0xBF, 0x0A, 0xA3, 0x36, 0x2E,
  27596. 0x70, 0x41, 0xFD, 0x16, 0x59, 0x6D, 0x80, 0x8E,
  27597. 0x86, 0x7B, 0x44, 0xC4, 0xC0, 0x0D, 0x1C, 0xDA,
  27598. 0x34, 0x18, 0x96, 0x77, 0x17, 0xF1, 0x47, 0xD0,
  27599. 0xEB, 0x21, 0xB4, 0x2A, 0xAE, 0xE7, 0x4A, 0xC3,
  27600. 0x5D, 0x0B, 0x92, 0x41, 0x4B, 0x95, 0x85, 0x31,
  27601. 0xAA, 0xDF, 0x46, 0x3E, 0xC6, 0x30, 0x5A, 0xE5,
  27602. 0xEC, 0xAF, 0x79, 0x17, 0x40, 0x02, 0xF2, 0x6D,
  27603. 0xDE, 0xCC, 0x81, 0x3B, 0xF3, 0x26, 0x72, 0xE8,
  27604. 0x52, 0x9D, 0x95, 0xA4, 0xE7, 0x30, 0xA7, 0xAB,
  27605. 0x4A, 0x3E, 0x8F, 0x8A, 0x8A, 0xF9, 0x79, 0xA6,
  27606. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  27607. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  27608. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  27609. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  27610. };
  27611. const byte kyber768_sk[] = {
  27612. 0x07, 0x63, 0x8F, 0xB6, 0x98, 0x68, 0xF3, 0xD3,
  27613. 0x20, 0xE5, 0x86, 0x2B, 0xD9, 0x69, 0x33, 0xFE,
  27614. 0xB3, 0x11, 0xB3, 0x62, 0x09, 0x3C, 0x9B, 0x5D,
  27615. 0x50, 0x17, 0x0B, 0xCE, 0xD4, 0x3F, 0x1B, 0x53,
  27616. 0x6D, 0x9A, 0x20, 0x4B, 0xB1, 0xF2, 0x26, 0x95,
  27617. 0x95, 0x0B, 0xA1, 0xF2, 0xA9, 0xE8, 0xEB, 0x82,
  27618. 0x8B, 0x28, 0x44, 0x88, 0x76, 0x0B, 0x3F, 0xC8,
  27619. 0x4F, 0xAB, 0xA0, 0x42, 0x75, 0xD5, 0x62, 0x8E,
  27620. 0x39, 0xC5, 0xB2, 0x47, 0x13, 0x74, 0x28, 0x3C,
  27621. 0x50, 0x32, 0x99, 0xC0, 0xAB, 0x49, 0xB6, 0x6B,
  27622. 0x8B, 0xBB, 0x56, 0xA4, 0x18, 0x66, 0x24, 0xF9,
  27623. 0x19, 0xA2, 0xBA, 0x59, 0xBB, 0x08, 0xD8, 0x55,
  27624. 0x18, 0x80, 0xC2, 0xBE, 0xFC, 0x4F, 0x87, 0xF2,
  27625. 0x5F, 0x59, 0xAB, 0x58, 0x7A, 0x79, 0xC3, 0x27,
  27626. 0xD7, 0x92, 0xD5, 0x4C, 0x97, 0x4A, 0x69, 0x26,
  27627. 0x2F, 0xF8, 0xA7, 0x89, 0x38, 0x28, 0x9E, 0x9A,
  27628. 0x87, 0xB6, 0x88, 0xB0, 0x83, 0xE0, 0x59, 0x5F,
  27629. 0xE2, 0x18, 0xB6, 0xBB, 0x15, 0x05, 0x94, 0x1C,
  27630. 0xE2, 0xE8, 0x1A, 0x5A, 0x64, 0xC5, 0xAA, 0xC6,
  27631. 0x04, 0x17, 0x25, 0x69, 0x85, 0x34, 0x9E, 0xE4,
  27632. 0x7A, 0x52, 0x42, 0x0A, 0x5F, 0x97, 0x47, 0x7B,
  27633. 0x72, 0x36, 0xAC, 0x76, 0xBC, 0x70, 0xE8, 0x28,
  27634. 0x87, 0x29, 0x28, 0x7E, 0xE3, 0xE3, 0x4A, 0x3D,
  27635. 0xBC, 0x36, 0x83, 0xC0, 0xB7, 0xB1, 0x00, 0x29,
  27636. 0xFC, 0x20, 0x34, 0x18, 0x53, 0x7E, 0x74, 0x66,
  27637. 0xBA, 0x63, 0x85, 0xA8, 0xFF, 0x30, 0x1E, 0xE1,
  27638. 0x27, 0x08, 0xF8, 0x2A, 0xAA, 0x1E, 0x38, 0x0F,
  27639. 0xC7, 0xA8, 0x8F, 0x8F, 0x20, 0x5A, 0xB7, 0xE8,
  27640. 0x8D, 0x7E, 0x95, 0x95, 0x2A, 0x55, 0xBA, 0x20,
  27641. 0xD0, 0x9B, 0x79, 0xA4, 0x71, 0x41, 0xD6, 0x2B,
  27642. 0xF6, 0xEB, 0x7D, 0xD3, 0x07, 0xB0, 0x8E, 0xCA,
  27643. 0x13, 0xA5, 0xBC, 0x5F, 0x6B, 0x68, 0x58, 0x1C,
  27644. 0x68, 0x65, 0xB2, 0x7B, 0xBC, 0xDD, 0xAB, 0x14,
  27645. 0x2F, 0x4B, 0x2C, 0xBF, 0xF4, 0x88, 0xC8, 0xA2,
  27646. 0x27, 0x05, 0xFA, 0xA9, 0x8A, 0x2B, 0x9E, 0xEA,
  27647. 0x35, 0x30, 0xC7, 0x66, 0x62, 0x33, 0x5C, 0xC7,
  27648. 0xEA, 0x3A, 0x00, 0x77, 0x77, 0x25, 0xEB, 0xCC,
  27649. 0xCD, 0x2A, 0x46, 0x36, 0xB2, 0xD9, 0x12, 0x2F,
  27650. 0xF3, 0xAB, 0x77, 0x12, 0x3C, 0xE0, 0x88, 0x3C,
  27651. 0x19, 0x11, 0x11, 0x5E, 0x50, 0xC9, 0xE8, 0xA9,
  27652. 0x41, 0x94, 0xE4, 0x8D, 0xD0, 0xD0, 0x9C, 0xFF,
  27653. 0xB3, 0xAD, 0xCD, 0x2C, 0x1E, 0x92, 0x43, 0x09,
  27654. 0x03, 0xD0, 0x7A, 0xDB, 0xF0, 0x05, 0x32, 0x03,
  27655. 0x15, 0x75, 0xAA, 0x7F, 0x9E, 0x7B, 0x5A, 0x1F,
  27656. 0x33, 0x62, 0xDE, 0xC9, 0x36, 0xD4, 0x04, 0x3C,
  27657. 0x05, 0xF2, 0x47, 0x6C, 0x07, 0x57, 0x8B, 0xC9,
  27658. 0xCB, 0xAF, 0x2A, 0xB4, 0xE3, 0x82, 0x72, 0x7A,
  27659. 0xD4, 0x16, 0x86, 0xA9, 0x6B, 0x25, 0x48, 0x82,
  27660. 0x0B, 0xB0, 0x3B, 0x32, 0xF1, 0x1B, 0x28, 0x11,
  27661. 0xAD, 0x62, 0xF4, 0x89, 0xE9, 0x51, 0x63, 0x2A,
  27662. 0xBA, 0x0D, 0x1D, 0xF8, 0x96, 0x80, 0xCC, 0x8A,
  27663. 0x8B, 0x53, 0xB4, 0x81, 0xD9, 0x2A, 0x68, 0xD7,
  27664. 0x0B, 0x4E, 0xA1, 0xC3, 0xA6, 0xA5, 0x61, 0xC0,
  27665. 0x69, 0x28, 0x82, 0xB5, 0xCA, 0x8C, 0xC9, 0x42,
  27666. 0xA8, 0xD4, 0x95, 0xAF, 0xCB, 0x06, 0xDE, 0x89,
  27667. 0x49, 0x8F, 0xB9, 0x35, 0xB7, 0x75, 0x90, 0x8F,
  27668. 0xE7, 0xA0, 0x3E, 0x32, 0x4D, 0x54, 0xCC, 0x19,
  27669. 0xD4, 0xE1, 0xAA, 0xBD, 0x35, 0x93, 0xB3, 0x8B,
  27670. 0x19, 0xEE, 0x13, 0x88, 0xFE, 0x49, 0x2B, 0x43,
  27671. 0x12, 0x7E, 0x5A, 0x50, 0x42, 0x53, 0x78, 0x6A,
  27672. 0x0D, 0x69, 0xAD, 0x32, 0x60, 0x1C, 0x28, 0xE2,
  27673. 0xC8, 0x85, 0x04, 0xA5, 0xBA, 0x59, 0x97, 0x06,
  27674. 0x02, 0x3A, 0x61, 0x36, 0x3E, 0x17, 0xC6, 0xB9,
  27675. 0xBB, 0x59, 0xBD, 0xC6, 0x97, 0x45, 0x2C, 0xD0,
  27676. 0x59, 0x45, 0x19, 0x83, 0xD7, 0x38, 0xCA, 0x3F,
  27677. 0xD0, 0x34, 0xE3, 0xF5, 0x98, 0x88, 0x54, 0xCA,
  27678. 0x05, 0x03, 0x1D, 0xB0, 0x96, 0x11, 0x49, 0x89,
  27679. 0x88, 0x19, 0x7C, 0x6B, 0x30, 0xD2, 0x58, 0xDF,
  27680. 0xE2, 0x62, 0x65, 0x54, 0x1C, 0x89, 0xA4, 0xB3,
  27681. 0x1D, 0x68, 0x64, 0xE9, 0x38, 0x9B, 0x03, 0xCB,
  27682. 0x74, 0xF7, 0xEC, 0x43, 0x23, 0xFB, 0x94, 0x21,
  27683. 0xA4, 0xB9, 0x79, 0x0A, 0x26, 0xD1, 0x7B, 0x03,
  27684. 0x98, 0xA2, 0x67, 0x67, 0x35, 0x09, 0x09, 0xF8,
  27685. 0x4D, 0x57, 0xB6, 0x69, 0x4D, 0xF8, 0x30, 0x66,
  27686. 0x4C, 0xA8, 0xB3, 0xC3, 0xC0, 0x3E, 0xD2, 0xAE,
  27687. 0x67, 0xB8, 0x90, 0x06, 0x86, 0x8A, 0x68, 0x52,
  27688. 0x7C, 0xCD, 0x66, 0x64, 0x59, 0xAB, 0x7F, 0x05,
  27689. 0x66, 0x71, 0x00, 0x0C, 0x61, 0x64, 0xD3, 0xA7,
  27690. 0xF2, 0x66, 0xA1, 0x4D, 0x97, 0xCB, 0xD7, 0x00,
  27691. 0x4D, 0x6C, 0x92, 0xCA, 0xCA, 0x77, 0x0B, 0x84,
  27692. 0x4A, 0x4F, 0xA9, 0xB1, 0x82, 0xE7, 0xB1, 0x8C,
  27693. 0xA8, 0x85, 0x08, 0x2A, 0xC5, 0x64, 0x6F, 0xCB,
  27694. 0x4A, 0x14, 0xE1, 0x68, 0x5F, 0xEB, 0x0C, 0x9C,
  27695. 0xE3, 0x37, 0x2A, 0xB9, 0x53, 0x65, 0xC0, 0x4F,
  27696. 0xD8, 0x30, 0x84, 0xF8, 0x0A, 0x23, 0xFF, 0x10,
  27697. 0xA0, 0x5B, 0xF1, 0x5F, 0x7F, 0xA5, 0xAC, 0xC6,
  27698. 0xC0, 0xCB, 0x46, 0x2C, 0x33, 0xCA, 0x52, 0x4F,
  27699. 0xA6, 0xB8, 0xBB, 0x35, 0x90, 0x43, 0xBA, 0x68,
  27700. 0x60, 0x9E, 0xAA, 0x25, 0x36, 0xE8, 0x1D, 0x08,
  27701. 0x46, 0x3B, 0x19, 0x65, 0x3B, 0x54, 0x35, 0xBA,
  27702. 0x94, 0x6C, 0x9A, 0xDD, 0xEB, 0x20, 0x2B, 0x04,
  27703. 0xB0, 0x31, 0xCC, 0x96, 0x0D, 0xCC, 0x12, 0xE4,
  27704. 0x51, 0x8D, 0x42, 0x8B, 0x32, 0xB2, 0x57, 0xA4,
  27705. 0xFC, 0x73, 0x13, 0xD3, 0xA7, 0x98, 0x0D, 0x80,
  27706. 0x08, 0x2E, 0x93, 0x4F, 0x9D, 0x95, 0xC3, 0x2B,
  27707. 0x0A, 0x01, 0x91, 0xA2, 0x36, 0x04, 0x38, 0x4D,
  27708. 0xD9, 0xE0, 0x79, 0xBB, 0xBA, 0xA2, 0x66, 0xD1,
  27709. 0x4C, 0x3F, 0x75, 0x6B, 0x9F, 0x21, 0x33, 0x10,
  27710. 0x74, 0x33, 0xA4, 0xE8, 0x3F, 0xA7, 0x18, 0x72,
  27711. 0x82, 0xA8, 0x09, 0x20, 0x3A, 0x4F, 0xAF, 0x84,
  27712. 0x18, 0x51, 0x83, 0x3D, 0x12, 0x1A, 0xC3, 0x83,
  27713. 0x84, 0x3A, 0x5E, 0x55, 0xBC, 0x23, 0x81, 0x42,
  27714. 0x5E, 0x16, 0xC7, 0xDB, 0x4C, 0xC9, 0xAB, 0x5C,
  27715. 0x1B, 0x0D, 0x91, 0xA4, 0x7E, 0x2B, 0x8D, 0xE0,
  27716. 0xE5, 0x82, 0xC8, 0x6B, 0x6B, 0x0D, 0x90, 0x7B,
  27717. 0xB3, 0x60, 0xB9, 0x7F, 0x40, 0xAB, 0x5D, 0x03,
  27718. 0x8F, 0x6B, 0x75, 0xC8, 0x14, 0xB2, 0x7D, 0x9B,
  27719. 0x96, 0x8D, 0x41, 0x98, 0x32, 0xBC, 0x8C, 0x2B,
  27720. 0xEE, 0x60, 0x5E, 0xF6, 0xE5, 0x05, 0x9D, 0x33,
  27721. 0x10, 0x0D, 0x90, 0x48, 0x5D, 0x37, 0x84, 0x50,
  27722. 0x01, 0x42, 0x21, 0x73, 0x6C, 0x07, 0x40, 0x7C,
  27723. 0xAC, 0x26, 0x04, 0x08, 0xAA, 0x64, 0x92, 0x66,
  27724. 0x19, 0x78, 0x8B, 0x86, 0x01, 0xC2, 0xA7, 0x52,
  27725. 0xD1, 0xA6, 0xCB, 0xF8, 0x20, 0xD7, 0xC7, 0xA0,
  27726. 0x47, 0x16, 0x20, 0x32, 0x25, 0xB3, 0x89, 0x5B,
  27727. 0x93, 0x42, 0xD1, 0x47, 0xA8, 0x18, 0x5C, 0xFC,
  27728. 0x1B, 0xB6, 0x5B, 0xA0, 0x6B, 0x41, 0x42, 0x33,
  27729. 0x99, 0x03, 0xC0, 0xAC, 0x46, 0x51, 0x38, 0x5B,
  27730. 0x45, 0xD9, 0x8A, 0x8B, 0x19, 0xD2, 0x8C, 0xD6,
  27731. 0xBA, 0xB0, 0x88, 0x78, 0x7F, 0x7E, 0xE1, 0xB1,
  27732. 0x24, 0x61, 0x76, 0x6B, 0x43, 0xCB, 0xCC, 0xB9,
  27733. 0x64, 0x34, 0x42, 0x7D, 0x93, 0xC0, 0x65, 0x55,
  27734. 0x06, 0x88, 0xF6, 0x94, 0x8E, 0xD1, 0xB5, 0x47,
  27735. 0x5A, 0x42, 0x5F, 0x1B, 0x85, 0x20, 0x9D, 0x06,
  27736. 0x1C, 0x08, 0xB5, 0x6C, 0x1C, 0xC0, 0x69, 0xF6,
  27737. 0xC0, 0xA7, 0xC6, 0xF2, 0x93, 0x58, 0xCA, 0xB9,
  27738. 0x11, 0x08, 0x77, 0x32, 0xA6, 0x49, 0xD2, 0x7C,
  27739. 0x9B, 0x98, 0xF9, 0xA4, 0x88, 0x79, 0x38, 0x7D,
  27740. 0x9B, 0x00, 0xC2, 0x59, 0x59, 0xA7, 0x16, 0x54,
  27741. 0xD6, 0xF6, 0xA9, 0x46, 0x16, 0x45, 0x13, 0xE4,
  27742. 0x7A, 0x75, 0xD0, 0x05, 0x98, 0x6C, 0x23, 0x63,
  27743. 0xC0, 0x9F, 0x6B, 0x53, 0x7E, 0xCA, 0x78, 0xB9,
  27744. 0x30, 0x3A, 0x5F, 0xA4, 0x57, 0x60, 0x8A, 0x58,
  27745. 0x6A, 0x65, 0x3A, 0x34, 0x7D, 0xB0, 0x4D, 0xFC,
  27746. 0xC1, 0x91, 0x75, 0xB3, 0xA3, 0x01, 0x17, 0x25,
  27747. 0x36, 0x06, 0x2A, 0x65, 0x8A, 0x95, 0x27, 0x75,
  27748. 0x70, 0xC8, 0x85, 0x2C, 0xA8, 0x97, 0x3F, 0x4A,
  27749. 0xE1, 0x23, 0xA3, 0x34, 0x04, 0x7D, 0xD7, 0x11,
  27750. 0xC8, 0x92, 0x7A, 0x63, 0x4A, 0x03, 0x38, 0x8A,
  27751. 0x52, 0x7B, 0x03, 0x4B, 0xF7, 0xA8, 0x17, 0x0F,
  27752. 0xA7, 0x02, 0xC1, 0xF7, 0xC2, 0x3E, 0xC3, 0x2D,
  27753. 0x18, 0xA2, 0x37, 0x48, 0x90, 0xBE, 0x9C, 0x78,
  27754. 0x7A, 0x94, 0x09, 0xC8, 0x2D, 0x19, 0x2C, 0x4B,
  27755. 0xB7, 0x05, 0xA2, 0xF9, 0x96, 0xCE, 0x40, 0x5D,
  27756. 0xA7, 0x2C, 0x2D, 0x9C, 0x84, 0x3E, 0xE9, 0xF8,
  27757. 0x31, 0x3E, 0xCC, 0x7F, 0x86, 0xD6, 0x29, 0x4D,
  27758. 0x59, 0x15, 0x9D, 0x9A, 0x87, 0x9A, 0x54, 0x2E,
  27759. 0x26, 0x09, 0x22, 0xAD, 0xF9, 0x99, 0x05, 0x1C,
  27760. 0xC4, 0x52, 0x00, 0xC9, 0xFF, 0xDB, 0x60, 0x44,
  27761. 0x9C, 0x49, 0x46, 0x59, 0x79, 0x27, 0x23, 0x67,
  27762. 0xC0, 0x83, 0xA7, 0xD6, 0x26, 0x7A, 0x3E, 0xD7,
  27763. 0xA7, 0xFD, 0x47, 0x95, 0x7C, 0x21, 0x93, 0x27,
  27764. 0xF7, 0xCA, 0x73, 0xA4, 0x00, 0x7E, 0x16, 0x27,
  27765. 0xF0, 0x0B, 0x11, 0xCC, 0x80, 0x57, 0x3C, 0x15,
  27766. 0xAE, 0xE6, 0x64, 0x0F, 0xB8, 0x56, 0x2D, 0xFA,
  27767. 0x6B, 0x24, 0x0C, 0xA0, 0xAD, 0x35, 0x1A, 0xC4,
  27768. 0xAC, 0x15, 0x5B, 0x96, 0xC1, 0x4C, 0x8A, 0xB1,
  27769. 0x3D, 0xD2, 0x62, 0xCD, 0xFD, 0x51, 0xC4, 0xBB,
  27770. 0x55, 0x72, 0xFD, 0x61, 0x65, 0x53, 0xD1, 0x7B,
  27771. 0xDD, 0x43, 0x0A, 0xCB, 0xEA, 0x3E, 0x95, 0xF0,
  27772. 0xB6, 0x98, 0xD6, 0x69, 0x90, 0xAB, 0x51, 0xE5,
  27773. 0xD0, 0x37, 0x83, 0xA8, 0xB3, 0xD2, 0x78, 0xA5,
  27774. 0x72, 0x04, 0x54, 0xCF, 0x96, 0x95, 0xCF, 0xDC,
  27775. 0xA0, 0x84, 0x85, 0xBA, 0x09, 0x9C, 0x51, 0xCD,
  27776. 0x92, 0xA7, 0xEA, 0x75, 0x87, 0xC1, 0xD1, 0x5C,
  27777. 0x28, 0xE6, 0x09, 0xA8, 0x18, 0x52, 0x60, 0x1B,
  27778. 0x06, 0x04, 0x01, 0x06, 0x79, 0xAA, 0x48, 0x2D,
  27779. 0x51, 0x26, 0x1E, 0xC3, 0x6E, 0x36, 0xB8, 0x71,
  27780. 0x96, 0x76, 0x21, 0x7F, 0xD7, 0x4C, 0x54, 0x78,
  27781. 0x64, 0x88, 0xF4, 0xB4, 0x96, 0x9C, 0x05, 0xA8,
  27782. 0xBA, 0x27, 0xCA, 0x3A, 0x77, 0xCC, 0xE7, 0x3B,
  27783. 0x96, 0x59, 0x23, 0xCA, 0x55, 0x4E, 0x42, 0x2B,
  27784. 0x9B, 0x61, 0xF4, 0x75, 0x46, 0x41, 0x60, 0x8A,
  27785. 0xC1, 0x6C, 0x9B, 0x85, 0x87, 0xA3, 0x2C, 0x1C,
  27786. 0x5D, 0xD7, 0x88, 0xF8, 0x8B, 0x36, 0xB7, 0x17,
  27787. 0xA4, 0x69, 0x65, 0x63, 0x5D, 0xEB, 0x67, 0xF4,
  27788. 0x5B, 0x12, 0x9B, 0x99, 0x07, 0x09, 0x09, 0xC9,
  27789. 0x3E, 0xB8, 0x0B, 0x42, 0xC2, 0xB3, 0xF3, 0xF7,
  27790. 0x03, 0x43, 0xA7, 0xCF, 0x37, 0xE8, 0x52, 0x0E,
  27791. 0x7B, 0xCF, 0xC4, 0x16, 0xAC, 0xA4, 0xF1, 0x8C,
  27792. 0x79, 0x81, 0x26, 0x2B, 0xA2, 0xBF, 0xC7, 0x56,
  27793. 0xAE, 0x03, 0x27, 0x8F, 0x0E, 0xC6, 0x6D, 0xC2,
  27794. 0x05, 0x76, 0x96, 0x82, 0x4B, 0xA6, 0x76, 0x98,
  27795. 0x65, 0xA6, 0x01, 0xD7, 0x14, 0x8E, 0xF6, 0xF5,
  27796. 0x4E, 0x5A, 0xF5, 0x68, 0x6A, 0xA2, 0x90, 0x6F,
  27797. 0x99, 0x4C, 0xE3, 0x8A, 0x5E, 0x0B, 0x93, 0x8F,
  27798. 0x23, 0x90, 0x07, 0x00, 0x30, 0x22, 0xC0, 0x33,
  27799. 0x92, 0xDF, 0x34, 0x01, 0xB1, 0xE4, 0xA3, 0xA7,
  27800. 0xEB, 0xC6, 0x16, 0x14, 0x49, 0xF7, 0x33, 0x74,
  27801. 0xC8, 0xB0, 0x14, 0x03, 0x69, 0x34, 0x3D, 0x92,
  27802. 0x95, 0xFD, 0xF5, 0x11, 0x84, 0x5C, 0x4A, 0x46,
  27803. 0xEB, 0xAA, 0xB6, 0xCA, 0x54, 0x92, 0xF6, 0x80,
  27804. 0x0B, 0x98, 0xC0, 0xCC, 0x80, 0x36, 0x53, 0xA4,
  27805. 0xB1, 0xD6, 0xE6, 0xAA, 0xED, 0x19, 0x32, 0xBA,
  27806. 0xCC, 0x5F, 0xEF, 0xAA, 0x81, 0x8B, 0xA5, 0x02,
  27807. 0x85, 0x9B, 0xA5, 0x49, 0x4C, 0x5F, 0x54, 0x02,
  27808. 0xC8, 0x53, 0x6A, 0x9C, 0x4C, 0x18, 0x88, 0x15,
  27809. 0x06, 0x17, 0xF8, 0x00, 0x98, 0xF6, 0xB2, 0xA9,
  27810. 0x9C, 0x39, 0xBC, 0x5D, 0xC7, 0xCF, 0x3B, 0x59,
  27811. 0x00, 0xA2, 0x13, 0x29, 0xAB, 0x59, 0x05, 0x3A,
  27812. 0xBA, 0xA6, 0x4E, 0xD1, 0x63, 0xE8, 0x59, 0xA8,
  27813. 0xB3, 0xB3, 0xCA, 0x33, 0x59, 0xB7, 0x50, 0xCC,
  27814. 0xC3, 0xE7, 0x10, 0xC7, 0xAC, 0x43, 0xC8, 0x19,
  27815. 0x1C, 0xB5, 0xD6, 0x88, 0x70, 0xC0, 0x63, 0x91,
  27816. 0xC0, 0xCB, 0x8A, 0xEC, 0x72, 0xB8, 0x97, 0xAC,
  27817. 0x6B, 0xE7, 0xFB, 0xAA, 0xCC, 0x67, 0x6E, 0xD6,
  27818. 0x63, 0x14, 0xC8, 0x36, 0x30, 0xE8, 0x94, 0x48,
  27819. 0xC8, 0x8A, 0x1D, 0xF0, 0x4A, 0xCE, 0xB2, 0x3A,
  27820. 0xBF, 0x2E, 0x40, 0x9E, 0xF3, 0x33, 0xC6, 0x22,
  27821. 0x28, 0x9C, 0x18, 0xA2, 0x13, 0x4E, 0x65, 0x0C,
  27822. 0x45, 0x25, 0x7E, 0x47, 0x47, 0x5F, 0xA3, 0x3A,
  27823. 0xA5, 0x37, 0xA5, 0xA8, 0xF7, 0x68, 0x02, 0x14,
  27824. 0x71, 0x6C, 0x50, 0xD4, 0x70, 0xE3, 0x28, 0x49,
  27825. 0x63, 0xCA, 0x64, 0xF5, 0x46, 0x77, 0xAE, 0xC5,
  27826. 0x4B, 0x52, 0x72, 0x16, 0x2B, 0xF5, 0x2B, 0xC8,
  27827. 0x14, 0x2E, 0x1D, 0x41, 0x83, 0xFC, 0x01, 0x74,
  27828. 0x54, 0xA6, 0xB5, 0xA4, 0x96, 0x83, 0x17, 0x59,
  27829. 0x06, 0x40, 0x24, 0x74, 0x59, 0x78, 0xCB, 0xD5,
  27830. 0x1A, 0x6C, 0xED, 0xC8, 0x95, 0x5D, 0xE4, 0xCC,
  27831. 0x6D, 0x36, 0x36, 0x70, 0xA4, 0x74, 0x66, 0xE8,
  27832. 0x2B, 0xE5, 0xC2, 0x36, 0x03, 0xA1, 0x7B, 0xF2,
  27833. 0x2A, 0xCD, 0xB7, 0xCC, 0x98, 0x4A, 0xF0, 0x8C,
  27834. 0x87, 0xE1, 0x4E, 0x27, 0x75, 0x3C, 0xF5, 0x87,
  27835. 0xA8, 0xEC, 0x34, 0x47, 0xE6, 0x2C, 0x64, 0x9E,
  27836. 0x88, 0x7A, 0x67, 0xC3, 0x6C, 0x9C, 0xE9, 0x87,
  27837. 0x21, 0xB6, 0x97, 0x21, 0x32, 0x75, 0x64, 0x6B,
  27838. 0x19, 0x4F, 0x36, 0x75, 0x86, 0x73, 0xA8, 0xED,
  27839. 0x11, 0x28, 0x44, 0x55, 0xAF, 0xC7, 0xA8, 0x52,
  27840. 0x9F, 0x69, 0xC9, 0x7A, 0x3C, 0x2D, 0x7B, 0x8C,
  27841. 0x63, 0x6C, 0x0B, 0xA5, 0x56, 0x14, 0xB7, 0x68,
  27842. 0xE6, 0x24, 0xE7, 0x12, 0x93, 0x0F, 0x77, 0x61,
  27843. 0x69, 0xB0, 0x17, 0x15, 0x72, 0x53, 0x51, 0xBC,
  27844. 0x74, 0xB4, 0x73, 0x95, 0xED, 0x52, 0xB2, 0x5A,
  27845. 0x13, 0x13, 0xC9, 0x51, 0x64, 0x81, 0x4C, 0x34,
  27846. 0xC9, 0x79, 0xCB, 0xDF, 0xAB, 0x85, 0x95, 0x46,
  27847. 0x62, 0xCA, 0xB4, 0x85, 0xE7, 0x50, 0x87, 0xA9,
  27848. 0x8C, 0xC7, 0x4B, 0xB8, 0x2C, 0xA2, 0xD1, 0xB5,
  27849. 0xBF, 0x28, 0x03, 0x23, 0x84, 0x80, 0x63, 0x8C,
  27850. 0x40, 0xE9, 0x0B, 0x43, 0xC7, 0x46, 0x0E, 0x7A,
  27851. 0xA9, 0x17, 0xF0, 0x10, 0x15, 0x1F, 0xAB, 0x11,
  27852. 0x69, 0x98, 0x7B, 0x37, 0x2A, 0xBB, 0x59, 0x27,
  27853. 0x1F, 0x70, 0x06, 0xC2, 0x4E, 0x60, 0x23, 0x6B,
  27854. 0x84, 0xB9, 0xDD, 0xD6, 0x00, 0x62, 0x37, 0x04,
  27855. 0x25, 0x46, 0x17, 0xFB, 0x49, 0x8D, 0x89, 0xE5,
  27856. 0x8B, 0x03, 0x68, 0xBC, 0xB2, 0x10, 0x3E, 0x79,
  27857. 0x35, 0x3E, 0xB5, 0x87, 0x86, 0x0C, 0x14, 0x22,
  27858. 0xE4, 0x76, 0x16, 0x2E, 0x42, 0x5B, 0xC2, 0x38,
  27859. 0x1D, 0xB8, 0x2C, 0x65, 0x92, 0x73, 0x7E, 0x1D,
  27860. 0xD6, 0x02, 0x86, 0x4B, 0x01, 0x67, 0xA7, 0x1E,
  27861. 0xC1, 0xF2, 0x23, 0x30, 0x5C, 0x02, 0xFE, 0x25,
  27862. 0x05, 0x2A, 0xF2, 0xB3, 0xB5, 0xA5, 0x5A, 0x0D,
  27863. 0x7A, 0x20, 0x22, 0xD9, 0xA7, 0x98, 0xDC, 0x0C,
  27864. 0x58, 0x74, 0xA9, 0x87, 0x02, 0xAA, 0xF4, 0x05,
  27865. 0x4C, 0x5D, 0x80, 0x33, 0x8A, 0x52, 0x48, 0xB5,
  27866. 0xB7, 0xBD, 0x09, 0xC5, 0x3B, 0x5E, 0x2A, 0x08,
  27867. 0x4B, 0x04, 0x7D, 0x27, 0x7A, 0x86, 0x1B, 0x1A,
  27868. 0x73, 0xBB, 0x51, 0x48, 0x8D, 0xE0, 0x4E, 0xF5,
  27869. 0x73, 0xC8, 0x52, 0x30, 0xA0, 0x47, 0x0B, 0x73,
  27870. 0x17, 0x5C, 0x9F, 0xA5, 0x05, 0x94, 0xF6, 0x6A,
  27871. 0x5F, 0x50, 0xB4, 0x15, 0x00, 0x54, 0xC9, 0x3B,
  27872. 0x68, 0x18, 0x6F, 0x8B, 0x5C, 0xBC, 0x49, 0x31,
  27873. 0x6C, 0x85, 0x48, 0xA6, 0x42, 0xB2, 0xB3, 0x6A,
  27874. 0x1D, 0x45, 0x4C, 0x74, 0x89, 0xAC, 0x33, 0xB2,
  27875. 0xD2, 0xCE, 0x66, 0x68, 0x09, 0x67, 0x82, 0xA2,
  27876. 0xC1, 0xE0, 0x86, 0x6D, 0x21, 0xA6, 0x5E, 0x16,
  27877. 0xB5, 0x85, 0xE7, 0xAF, 0x86, 0x18, 0xBD, 0xF3,
  27878. 0x18, 0x4C, 0x19, 0x86, 0x87, 0x85, 0x08, 0x91,
  27879. 0x72, 0x77, 0xB9, 0x3E, 0x10, 0x70, 0x6B, 0x16,
  27880. 0x14, 0x97, 0x2B, 0x2A, 0x94, 0xC7, 0x31, 0x0F,
  27881. 0xE9, 0xC7, 0x08, 0xC2, 0x31, 0xA1, 0xA8, 0xAC,
  27882. 0x8D, 0x93, 0x14, 0xA5, 0x29, 0xA9, 0x7F, 0x46,
  27883. 0x9B, 0xF6, 0x49, 0x62, 0xD8, 0x20, 0x64, 0x84,
  27884. 0x43, 0x09, 0x9A, 0x07, 0x6D, 0x55, 0xD4, 0xCE,
  27885. 0xA8, 0x24, 0xA5, 0x83, 0x04, 0x84, 0x4F, 0x99,
  27886. 0x49, 0x7C, 0x10, 0xA2, 0x51, 0x48, 0x61, 0x8A,
  27887. 0x31, 0x5D, 0x72, 0xCA, 0x85, 0x7D, 0x1B, 0x04,
  27888. 0xD5, 0x75, 0xB9, 0x4F, 0x85, 0xC0, 0x1D, 0x19,
  27889. 0xBE, 0xF2, 0x11, 0xBF, 0x0A, 0xA3, 0x36, 0x2E,
  27890. 0x70, 0x41, 0xFD, 0x16, 0x59, 0x6D, 0x80, 0x8E,
  27891. 0x86, 0x7B, 0x44, 0xC4, 0xC0, 0x0D, 0x1C, 0xDA,
  27892. 0x34, 0x18, 0x96, 0x77, 0x17, 0xF1, 0x47, 0xD0,
  27893. 0xEB, 0x21, 0xB4, 0x2A, 0xAE, 0xE7, 0x4A, 0xC3,
  27894. 0x5D, 0x0B, 0x92, 0x41, 0x4B, 0x95, 0x85, 0x31,
  27895. 0xAA, 0xDF, 0x46, 0x3E, 0xC6, 0x30, 0x5A, 0xE5,
  27896. 0xEC, 0xAF, 0x79, 0x17, 0x40, 0x02, 0xF2, 0x6D,
  27897. 0xDE, 0xCC, 0x81, 0x3B, 0xF3, 0x26, 0x72, 0xE8,
  27898. 0x52, 0x9D, 0x95, 0xA4, 0xE7, 0x30, 0xA7, 0xAB,
  27899. 0x4A, 0x3E, 0x8F, 0x8A, 0x8A, 0xF9, 0x79, 0xA6,
  27900. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  27901. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  27902. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  27903. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  27904. 0xD4, 0xEC, 0x14, 0x3B, 0x50, 0xF0, 0x14, 0x23,
  27905. 0xB1, 0x77, 0x89, 0x5E, 0xDE, 0xE2, 0x2B, 0xB7,
  27906. 0x39, 0xF6, 0x47, 0xEC, 0xF8, 0x5F, 0x50, 0xBC,
  27907. 0x25, 0xEF, 0x7B, 0x5A, 0x72, 0x5D, 0xEE, 0x86,
  27908. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  27909. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  27910. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  27911. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  27912. };
  27913. const byte kyber768_ct[] = {
  27914. 0xB5, 0x2C, 0x56, 0xB9, 0x2A, 0x4B, 0x7C, 0xE9,
  27915. 0xE4, 0xCB, 0x7C, 0x5B, 0x1B, 0x16, 0x31, 0x67,
  27916. 0xA8, 0xA1, 0x67, 0x5B, 0x2F, 0xDE, 0xF8, 0x4A,
  27917. 0x5B, 0x67, 0xCA, 0x15, 0xDB, 0x69, 0x4C, 0x9F,
  27918. 0x11, 0xBD, 0x02, 0x7C, 0x30, 0xAE, 0x22, 0xEC,
  27919. 0x92, 0x1A, 0x1D, 0x91, 0x15, 0x99, 0xAF, 0x05,
  27920. 0x85, 0xE4, 0x8D, 0x20, 0xDA, 0x70, 0xDF, 0x9F,
  27921. 0x39, 0xE3, 0x2E, 0xF9, 0x5D, 0x4C, 0x8F, 0x44,
  27922. 0xBF, 0xEF, 0xDA, 0xA5, 0xDA, 0x64, 0xF1, 0x05,
  27923. 0x46, 0x31, 0xD0, 0x4D, 0x6D, 0x3C, 0xFD, 0x0A,
  27924. 0x54, 0x0D, 0xD7, 0xBA, 0x38, 0x86, 0xE4, 0xB5,
  27925. 0xF1, 0x3E, 0x87, 0x87, 0x88, 0x60, 0x4C, 0x95,
  27926. 0xC0, 0x96, 0xEA, 0xB3, 0x91, 0x9F, 0x42, 0x75,
  27927. 0x21, 0x41, 0x9A, 0x94, 0x6C, 0x26, 0xCC, 0x04,
  27928. 0x14, 0x75, 0xD7, 0x12, 0x4C, 0xDC, 0x01, 0xD0,
  27929. 0x37, 0x3E, 0x5B, 0x09, 0xC7, 0xA7, 0x06, 0x03,
  27930. 0xCF, 0xDB, 0x4F, 0xB3, 0x40, 0x50, 0x23, 0xF2,
  27931. 0x26, 0x4D, 0xC3, 0xF9, 0x83, 0xC4, 0xFC, 0x02,
  27932. 0xA2, 0xD1, 0xB2, 0x68, 0xF2, 0x20, 0x8A, 0x1F,
  27933. 0x6E, 0x2A, 0x62, 0x09, 0xBF, 0xF1, 0x2F, 0x6F,
  27934. 0x46, 0x5F, 0x0B, 0x06, 0x9C, 0x3A, 0x7F, 0x84,
  27935. 0xF6, 0x06, 0xD8, 0xA9, 0x40, 0x64, 0x00, 0x3D,
  27936. 0x6E, 0xC1, 0x14, 0xC8, 0xE8, 0x08, 0xD3, 0x05,
  27937. 0x38, 0x84, 0xC1, 0xD5, 0xA1, 0x42, 0xFB, 0xF2,
  27938. 0x01, 0x12, 0xEB, 0x36, 0x0F, 0xDA, 0x3F, 0x0F,
  27939. 0x28, 0xB1, 0x72, 0xAE, 0x50, 0xF5, 0xE7, 0xD8,
  27940. 0x38, 0x01, 0xFB, 0x3F, 0x00, 0x64, 0xB6, 0x87,
  27941. 0x18, 0x70, 0x74, 0xBD, 0x7F, 0xE3, 0x0E, 0xDD,
  27942. 0xAA, 0x33, 0x4C, 0xF8, 0xFC, 0x04, 0xFA, 0x8C,
  27943. 0xED, 0x89, 0x9C, 0xEA, 0xDE, 0x4B, 0x4F, 0x28,
  27944. 0xB6, 0x83, 0x72, 0xBA, 0xF9, 0x8F, 0xF4, 0x82,
  27945. 0xA4, 0x15, 0xB7, 0x31, 0x15, 0x5B, 0x75, 0xCE,
  27946. 0xB9, 0x76, 0xBE, 0x0E, 0xA0, 0x28, 0x5B, 0xA0,
  27947. 0x1A, 0x27, 0xF1, 0x85, 0x7A, 0x8F, 0xB3, 0x77,
  27948. 0xA3, 0xAE, 0x0C, 0x23, 0xB2, 0xAA, 0x9A, 0x07,
  27949. 0x9B, 0xFA, 0xBF, 0xF0, 0xD5, 0xB2, 0xF1, 0xCD,
  27950. 0x9B, 0x71, 0x8B, 0xEA, 0x03, 0xC4, 0x2F, 0x34,
  27951. 0x3A, 0x39, 0xB4, 0xF1, 0x42, 0xD0, 0x1A, 0xD8,
  27952. 0xAC, 0xBB, 0x50, 0xE3, 0x88, 0x53, 0xCF, 0x9A,
  27953. 0x50, 0xC8, 0xB4, 0x4C, 0x3C, 0xF6, 0x71, 0xA4,
  27954. 0xA9, 0x04, 0x3B, 0x26, 0xDD, 0xBB, 0x24, 0x95,
  27955. 0x9A, 0xD6, 0x71, 0x5C, 0x08, 0x52, 0x18, 0x55,
  27956. 0xC7, 0x9A, 0x23, 0xB9, 0xC3, 0xD6, 0x47, 0x17,
  27957. 0x49, 0xC4, 0x07, 0x25, 0xBD, 0xD5, 0xC2, 0x77,
  27958. 0x6D, 0x43, 0xAE, 0xD2, 0x02, 0x04, 0xBA, 0xA1,
  27959. 0x41, 0xEF, 0xB3, 0x30, 0x49, 0x17, 0x47, 0x4B,
  27960. 0x7F, 0x9F, 0x7A, 0x4B, 0x08, 0xB1, 0xA9, 0x3D,
  27961. 0xAE, 0xD9, 0x8C, 0x67, 0x49, 0x53, 0x59, 0xD3,
  27962. 0x7D, 0x67, 0xF7, 0x43, 0x8B, 0xEE, 0x5E, 0x43,
  27963. 0x58, 0x56, 0x34, 0xB2, 0x6C, 0x6B, 0x38, 0x10,
  27964. 0xD7, 0xCD, 0xCB, 0xC0, 0xF6, 0xEB, 0x87, 0x7A,
  27965. 0x60, 0x87, 0xE6, 0x8A, 0xCB, 0x84, 0x80, 0xD3,
  27966. 0xA8, 0xCF, 0x69, 0x00, 0x44, 0x7E, 0x49, 0xB4,
  27967. 0x17, 0xF1, 0x5A, 0x53, 0xB6, 0x07, 0xA0, 0xE2,
  27968. 0x16, 0xB8, 0x55, 0x97, 0x0D, 0x37, 0x40, 0x68,
  27969. 0x70, 0xB4, 0x56, 0x87, 0x22, 0xDA, 0x77, 0xA4,
  27970. 0x08, 0x47, 0x03, 0x81, 0x67, 0x84, 0xE2, 0xF1,
  27971. 0x6B, 0xED, 0x18, 0x99, 0x65, 0x32, 0xC5, 0xD8,
  27972. 0xB7, 0xF5, 0xD2, 0x14, 0x46, 0x4E, 0x5F, 0x3F,
  27973. 0x6E, 0x90, 0x58, 0x67, 0xB0, 0xCE, 0x11, 0x9E,
  27974. 0x25, 0x2A, 0x66, 0x71, 0x32, 0x53, 0x54, 0x46,
  27975. 0x85, 0xD2, 0x08, 0xE1, 0x72, 0x39, 0x08, 0xA0,
  27976. 0xCE, 0x97, 0x83, 0x46, 0x52, 0xE0, 0x8A, 0xE7,
  27977. 0xBD, 0xC8, 0x81, 0xA1, 0x31, 0xB7, 0x3C, 0x71,
  27978. 0xE8, 0x4D, 0x20, 0xD6, 0x8F, 0xDE, 0xFF, 0x4F,
  27979. 0x5D, 0x70, 0xCD, 0x1A, 0xF5, 0x7B, 0x78, 0xE3,
  27980. 0x49, 0x1A, 0x98, 0x65, 0x94, 0x23, 0x21, 0x80,
  27981. 0x0A, 0x20, 0x3C, 0x05, 0xED, 0x1F, 0xEE, 0xB5,
  27982. 0xA2, 0x8E, 0x58, 0x4E, 0x19, 0xF6, 0x53, 0x5E,
  27983. 0x7F, 0x84, 0xE4, 0xA2, 0x4F, 0x84, 0xA7, 0x2D,
  27984. 0xCA, 0xF5, 0x64, 0x8B, 0x4A, 0x42, 0x35, 0xDD,
  27985. 0x66, 0x44, 0x64, 0x48, 0x2F, 0x03, 0x17, 0x6E,
  27986. 0x88, 0x8C, 0x28, 0xBF, 0xC6, 0xC1, 0xCB, 0x23,
  27987. 0x8C, 0xFF, 0xA3, 0x5A, 0x32, 0x1E, 0x71, 0x79,
  27988. 0x1D, 0x9E, 0xA8, 0xED, 0x08, 0x78, 0xC6, 0x11,
  27989. 0x21, 0xBF, 0x8D, 0x2A, 0x4A, 0xB2, 0xC1, 0xA5,
  27990. 0xE1, 0x20, 0xBC, 0x40, 0xAB, 0xB1, 0x89, 0x2D,
  27991. 0x17, 0x15, 0x09, 0x0A, 0x0E, 0xE4, 0x82, 0x52,
  27992. 0xCA, 0x29, 0x7A, 0x99, 0xAA, 0x0E, 0x51, 0x0C,
  27993. 0xF2, 0x6B, 0x1A, 0xDD, 0x06, 0xCA, 0x54, 0x3E,
  27994. 0x1C, 0x5D, 0x6B, 0xDC, 0xD3, 0xB9, 0xC5, 0x85,
  27995. 0xC8, 0x53, 0x80, 0x45, 0xDB, 0x5C, 0x25, 0x2E,
  27996. 0xC3, 0xC8, 0xC3, 0xC9, 0x54, 0xD9, 0xBE, 0x59,
  27997. 0x07, 0x09, 0x4A, 0x89, 0x4E, 0x60, 0xEA, 0xB4,
  27998. 0x35, 0x38, 0xCF, 0xEE, 0x82, 0xE8, 0xFF, 0xC0,
  27999. 0x79, 0x1B, 0x0D, 0x0F, 0x43, 0xAC, 0x16, 0x27,
  28000. 0x83, 0x0A, 0x61, 0xD5, 0x6D, 0xAD, 0x96, 0xC6,
  28001. 0x29, 0x58, 0xB0, 0xDE, 0x78, 0x0B, 0x78, 0xBD,
  28002. 0x47, 0xA6, 0x04, 0x55, 0x0D, 0xAB, 0x83, 0xFF,
  28003. 0xF2, 0x27, 0xC3, 0x24, 0x04, 0x94, 0x71, 0xF3,
  28004. 0x52, 0x48, 0xCF, 0xB8, 0x49, 0xB2, 0x57, 0x24,
  28005. 0xFF, 0x70, 0x4D, 0x52, 0x77, 0xAA, 0x35, 0x2D,
  28006. 0x55, 0x09, 0x58, 0xBE, 0x3B, 0x23, 0x7D, 0xFF,
  28007. 0x47, 0x3E, 0xC2, 0xAD, 0xBA, 0xEA, 0x48, 0xCA,
  28008. 0x26, 0x58, 0xAE, 0xFC, 0xC7, 0x7B, 0xBD, 0x42,
  28009. 0x64, 0xAB, 0x37, 0x4D, 0x70, 0xEA, 0xE5, 0xB9,
  28010. 0x64, 0x41, 0x6C, 0xE8, 0x22, 0x6A, 0x7E, 0x32,
  28011. 0x55, 0xA0, 0xF8, 0xD7, 0xE2, 0xAD, 0xCA, 0x06,
  28012. 0x2B, 0xCD, 0x6D, 0x78, 0xD6, 0x0D, 0x1B, 0x32,
  28013. 0xE1, 0x14, 0x05, 0xBE, 0x54, 0xB6, 0x6E, 0xF0,
  28014. 0xFD, 0xDD, 0x56, 0x77, 0x02, 0xA3, 0xBC, 0xCF,
  28015. 0xED, 0xE3, 0xC5, 0x84, 0x70, 0x12, 0x69, 0xED,
  28016. 0x14, 0x80, 0x9F, 0x06, 0xF8, 0x96, 0x83, 0x56,
  28017. 0xBB, 0x92, 0x67, 0xFE, 0x86, 0xE5, 0x14, 0x25,
  28018. 0x2E, 0x88, 0xBB, 0x5C, 0x30, 0xA7, 0xEC, 0xB3,
  28019. 0xD0, 0xE6, 0x21, 0x02, 0x1E, 0xE0, 0xFB, 0xF7,
  28020. 0x87, 0x1B, 0x09, 0x34, 0x2B, 0xF8, 0x4F, 0x55,
  28021. 0xC9, 0x7E, 0xAF, 0x86, 0xC4, 0x81, 0x89, 0xC7,
  28022. 0xFF, 0x4D, 0xF3, 0x89, 0xF0, 0x77, 0xE2, 0x80,
  28023. 0x6E, 0x5F, 0xA7, 0x3B, 0x3E, 0x94, 0x58, 0xA1,
  28024. 0x6C, 0x7E, 0x27, 0x5F, 0x4F, 0x60, 0x22, 0x75,
  28025. 0x58, 0x0E, 0xB7, 0xB7, 0x13, 0x5F, 0xB5, 0x37,
  28026. 0xFA, 0x0C, 0xD9, 0x5D, 0x6E, 0xA5, 0x8C, 0x10,
  28027. 0x8C, 0xD8, 0x94, 0x3D, 0x70, 0xC1, 0x64, 0x31,
  28028. 0x11, 0xF4, 0xF0, 0x1C, 0xA8, 0xA8, 0x27, 0x6A,
  28029. 0x90, 0x26, 0x66, 0xED, 0x81, 0xB7, 0x8D, 0x16,
  28030. 0x8B, 0x00, 0x6F, 0x16, 0xAA, 0xA3, 0xD8, 0xE4,
  28031. 0xCE, 0x4F, 0x4D, 0x0F, 0xB0, 0x99, 0x7E, 0x41,
  28032. 0xAE, 0xFF, 0xB5, 0xB3, 0xDA, 0xA8, 0x38, 0x73,
  28033. 0x2F, 0x35, 0x73, 0x49, 0x44, 0x7F, 0x38, 0x77,
  28034. 0x76, 0xC7, 0x93, 0xC0, 0x47, 0x9D, 0xE9, 0xE9,
  28035. 0x94, 0x98, 0xCC, 0x35, 0x6F, 0xDB, 0x00, 0x75,
  28036. 0xA7, 0x03, 0xF2, 0x3C, 0x55, 0xD4, 0x7B, 0x55,
  28037. 0x0E, 0xC8, 0x9B, 0x02, 0xAD, 0xE8, 0x93, 0x29,
  28038. 0x08, 0x6A, 0x50, 0x84, 0x34, 0x56, 0xFE, 0xDC,
  28039. 0x37, 0x88, 0xAC, 0x8D, 0x97, 0x23, 0x3C, 0x54,
  28040. 0x56, 0x04, 0x67, 0xEE, 0x1D, 0x0F, 0x02, 0x4B,
  28041. 0x18, 0x42, 0x8F, 0x0D, 0x73, 0xB3, 0x0E, 0x19,
  28042. 0xF5, 0xC6, 0x3B, 0x9A, 0xBF, 0x11, 0x41, 0x5B,
  28043. 0xEA, 0x4D, 0x01, 0x70, 0x13, 0x0B, 0xAA, 0xBD,
  28044. 0x33, 0xC0, 0x5E, 0x65, 0x24, 0xE5, 0xFB, 0x55,
  28045. 0x81, 0xB2, 0x2B, 0x04, 0x33, 0x34, 0x22, 0x48,
  28046. 0x26, 0x6D, 0x0F, 0x10, 0x53, 0xB2, 0x45, 0xCC,
  28047. 0x24, 0x62, 0xDC, 0x44, 0xD3, 0x49, 0x65, 0x10,
  28048. 0x24, 0x82, 0xA8, 0xED, 0x9E, 0x4E, 0x96, 0x4D,
  28049. 0x56, 0x83, 0xE5, 0xD4, 0x5D, 0x0C, 0x82, 0x69
  28050. };
  28051. const byte kyber768_ss[] = {
  28052. 0x91, 0x4C, 0xB6, 0x7F, 0xE5, 0xC3, 0x8E, 0x73,
  28053. 0xBF, 0x74, 0x18, 0x1C, 0x0A, 0xC5, 0x04, 0x28,
  28054. 0xDE, 0xDF, 0x77, 0x50, 0xA9, 0x80, 0x58, 0xF7,
  28055. 0xD5, 0x36, 0x70, 0x87, 0x74, 0x53, 0x5B, 0x29
  28056. };
  28057. ret = wc_KyberKey_Init(KYBER768, &key, HEAP_HINT, INVALID_DEVID);
  28058. if (ret != 0)
  28059. return -20201;
  28060. ret = wc_KyberKey_MakeKeyWithRandom(&key, kyber768_rand,
  28061. sizeof(kyber768_rand));
  28062. if (ret != 0)
  28063. return -20202;
  28064. ret = wc_KyberKey_EncodePublicKey(&key, pub, sizeof(pub));
  28065. if (ret != 0)
  28066. return -20203;
  28067. ret = wc_KyberKey_EncodePrivateKey(&key, priv, sizeof(priv));
  28068. if (ret != 0)
  28069. return -20204;
  28070. if (XMEMCMP(pub, kyber768_pk, sizeof(kyber768_pk)) != 0)
  28071. return -20205;
  28072. if (XMEMCMP(priv, kyber768_sk, sizeof(kyber768_sk)) != 0)
  28073. return -20206;
  28074. ret = wc_KyberKey_EncapsulateWithRandom(&key, ct, ss, kyber768enc_rand,
  28075. sizeof(kyber768enc_rand));
  28076. if (ret != 0)
  28077. return -20207;
  28078. if (XMEMCMP(ct, kyber768_ct, sizeof(kyber768_ct)) != 0)
  28079. return -20208;
  28080. if (XMEMCMP(ss, kyber768_ss, sizeof(kyber768_ss)) != 0)
  28081. return -20209;
  28082. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, sizeof(kyber768_ct));
  28083. if (ret != 0)
  28084. return -20210;
  28085. if (XMEMCMP(ss_dec, kyber768_ss, sizeof(kyber768_ss)) != 0)
  28086. return -20211;
  28087. wc_KyberKey_Free(&key);
  28088. return 0;
  28089. }
  28090. #endif /* WOLFSSL_KYBER768 */
  28091. #ifdef WOLFSSL_KYBER1024
  28092. static int kyber1024_kat(void)
  28093. {
  28094. KyberKey key;
  28095. int ret;
  28096. byte priv[KYBER1024_PRIVATE_KEY_SIZE];
  28097. byte pub[KYBER1024_PUBLIC_KEY_SIZE];
  28098. byte ct[KYBER1024_CIPHER_TEXT_SIZE];
  28099. byte ss[KYBER_SS_SZ];
  28100. byte ss_dec[KYBER_SS_SZ];
  28101. const byte kyber1024_rand[] = {
  28102. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  28103. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  28104. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  28105. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  28106. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  28107. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  28108. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  28109. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  28110. };
  28111. const byte kyber1024enc_rand[] = {
  28112. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  28113. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  28114. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  28115. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  28116. };
  28117. const byte kyber1024_pk[] = {
  28118. 0xD2, 0x23, 0x02, 0xCB, 0xD3, 0x39, 0x9F, 0xAC,
  28119. 0xC6, 0x30, 0x99, 0x1F, 0xC8, 0xF2, 0x8B, 0xDB,
  28120. 0x43, 0x54, 0x76, 0x25, 0x41, 0x52, 0x76, 0x78,
  28121. 0xBC, 0xF6, 0x1F, 0x65, 0xC2, 0x41, 0x14, 0x6C,
  28122. 0x42, 0x6D, 0x23, 0xB9, 0xBF, 0xAA, 0x6B, 0x7D,
  28123. 0xF1, 0x8C, 0x97, 0xF2, 0x0C, 0x1B, 0x61, 0x25,
  28124. 0xBF, 0x87, 0x4B, 0x1D, 0x89, 0x47, 0x58, 0x52,
  28125. 0xC4, 0x48, 0x21, 0x5D, 0xB0, 0xEB, 0x77, 0x37,
  28126. 0xF9, 0x14, 0x80, 0xE8, 0xCE, 0xBD, 0x9A, 0x08,
  28127. 0x71, 0x57, 0x4F, 0x5A, 0xB6, 0x2D, 0x90, 0x20,
  28128. 0x17, 0x5E, 0xC6, 0x92, 0x7C, 0xA0, 0xB5, 0x4C,
  28129. 0x09, 0x81, 0x8E, 0x42, 0xCF, 0x92, 0xA3, 0x83,
  28130. 0x17, 0x24, 0x22, 0xC7, 0xDC, 0x18, 0x31, 0xD6,
  28131. 0x3B, 0x0C, 0x29, 0x5D, 0xE7, 0x51, 0x59, 0xDB,
  28132. 0x80, 0x34, 0xE9, 0xE0, 0x7F, 0x7B, 0x0B, 0x91,
  28133. 0x0C, 0x3C, 0x1E, 0x5F, 0xB6, 0x6B, 0x3D, 0xC5,
  28134. 0x23, 0xF1, 0xFA, 0x6E, 0xB4, 0x91, 0x0C, 0xB8,
  28135. 0x9A, 0x6C, 0x17, 0x56, 0x2C, 0x83, 0xAB, 0x4C,
  28136. 0x18, 0xD0, 0xCD, 0x7E, 0x07, 0x96, 0x59, 0x2A,
  28137. 0x37, 0x2A, 0xA4, 0x09, 0xB1, 0xC5, 0x57, 0x34,
  28138. 0x7C, 0xCA, 0xCD, 0xC4, 0x64, 0x4A, 0x11, 0x90,
  28139. 0x64, 0xD0, 0x6D, 0xD4, 0x74, 0x92, 0x9D, 0x1C,
  28140. 0x6F, 0xB4, 0xD6, 0x86, 0xE5, 0x49, 0x1C, 0xE4,
  28141. 0xBC, 0x89, 0xA3, 0x0B, 0xB4, 0xB8, 0xC4, 0x1B,
  28142. 0xCE, 0x51, 0x57, 0xDF, 0xC1, 0x36, 0x08, 0x23,
  28143. 0xB1, 0xAB, 0x61, 0x8C, 0x14, 0xB1, 0x0F, 0x98,
  28144. 0xC2, 0x50, 0x67, 0x39, 0x8E, 0xA7, 0x01, 0x8C,
  28145. 0x27, 0x8A, 0x4B, 0x3D, 0xF3, 0x13, 0x34, 0xD6,
  28146. 0x03, 0xB2, 0x04, 0x4E, 0xF1, 0x87, 0xCD, 0x9B,
  28147. 0xC6, 0xCE, 0x42, 0x72, 0x5B, 0xD9, 0x62, 0xC2,
  28148. 0x64, 0x98, 0x3E, 0x9E, 0x18, 0x15, 0x5A, 0x8B,
  28149. 0x9C, 0x47, 0x14, 0x3D, 0x70, 0x46, 0x0A, 0x26,
  28150. 0xA5, 0x6F, 0xE7, 0x65, 0x8C, 0x1F, 0x15, 0x03,
  28151. 0x48, 0xC6, 0x08, 0x7E, 0xF7, 0x58, 0xAD, 0x16,
  28152. 0x78, 0x87, 0x86, 0x0A, 0x00, 0x7A, 0x5F, 0xC3,
  28153. 0x73, 0x58, 0xD4, 0x3B, 0x5E, 0xBE, 0xE8, 0x20,
  28154. 0xAC, 0xEA, 0x47, 0x4F, 0x0A, 0xC0, 0x7B, 0x76,
  28155. 0x80, 0x28, 0x66, 0x19, 0x9C, 0x61, 0x23, 0x1D,
  28156. 0x5C, 0x74, 0x7C, 0x93, 0x77, 0x4D, 0x2C, 0x1E,
  28157. 0x0C, 0x1C, 0x67, 0xE6, 0xC8, 0x1B, 0x82, 0x75,
  28158. 0x21, 0x73, 0xE1, 0x25, 0xBA, 0xF3, 0x9B, 0x4F,
  28159. 0xD1, 0x9A, 0x4F, 0x45, 0x3D, 0xC5, 0x79, 0x76,
  28160. 0xB1, 0xD9, 0x7F, 0xE6, 0x99, 0x69, 0x92, 0xBB,
  28161. 0xB6, 0x5B, 0x7C, 0xB2, 0x5D, 0x07, 0x7B, 0xBA,
  28162. 0xA6, 0xA1, 0x33, 0x22, 0x89, 0x9A, 0xF6, 0x59,
  28163. 0xCF, 0x1B, 0x35, 0x58, 0xC1, 0xB5, 0x00, 0x11,
  28164. 0x54, 0xB6, 0x25, 0x80, 0x9E, 0xD8, 0x9A, 0xEE,
  28165. 0xBB, 0x89, 0xE6, 0xEA, 0x7D, 0x67, 0xF7, 0x23,
  28166. 0xD0, 0x45, 0xAB, 0x05, 0x71, 0x5C, 0x42, 0x35,
  28167. 0x5D, 0xA6, 0xA5, 0xC8, 0xDD, 0x39, 0xC8, 0xAB,
  28168. 0xE3, 0x03, 0x77, 0x51, 0xA0, 0x1E, 0xD1, 0xC7,
  28169. 0x37, 0x49, 0x19, 0xF3, 0x12, 0x1B, 0x5A, 0x52,
  28170. 0xC5, 0x3D, 0x14, 0x87, 0x31, 0x67, 0x69, 0xF8,
  28171. 0x07, 0x21, 0xDE, 0xEA, 0xAA, 0xD3, 0xC9, 0x0F,
  28172. 0x76, 0xE7, 0xAE, 0x9E, 0x12, 0xBA, 0x92, 0xB3,
  28173. 0x2B, 0x5F, 0xD4, 0x57, 0xE3, 0xC7, 0x52, 0xC2,
  28174. 0x65, 0x0D, 0xFB, 0x88, 0x57, 0x71, 0xCB, 0x77,
  28175. 0xAC, 0x3C, 0x78, 0x5A, 0x8C, 0x56, 0x2E, 0x6A,
  28176. 0x1C, 0x63, 0xC2, 0xA5, 0x5E, 0xA4, 0x7C, 0xF8,
  28177. 0xB9, 0x0E, 0xB8, 0x22, 0x5C, 0x12, 0x3C, 0x34,
  28178. 0x64, 0x52, 0x56, 0x62, 0x35, 0xB2, 0xF3, 0x18,
  28179. 0x23, 0xA3, 0x35, 0x21, 0xE0, 0x87, 0x93, 0x7A,
  28180. 0x34, 0x5D, 0x8D, 0x66, 0x3E, 0xEA, 0xA0, 0x56,
  28181. 0x58, 0x91, 0x7B, 0xBA, 0xA0, 0x08, 0xC2, 0xE3,
  28182. 0x35, 0xF8, 0x85, 0x0A, 0x90, 0xA3, 0x26, 0xD0,
  28183. 0xE6, 0x64, 0x32, 0xF4, 0x4C, 0xEB, 0x82, 0x89,
  28184. 0xE4, 0xEC, 0xB2, 0xD1, 0x29, 0x58, 0xE9, 0x84,
  28185. 0x07, 0x2E, 0xCA, 0xCB, 0x88, 0xE1, 0x34, 0x8F,
  28186. 0xF0, 0xB5, 0x56, 0x54, 0xAC, 0xBA, 0x5B, 0x54,
  28187. 0x97, 0x1C, 0xBA, 0xEB, 0xA8, 0x8E, 0xC4, 0xB9,
  28188. 0x1A, 0x94, 0xC3, 0x71, 0x92, 0xFA, 0x98, 0x2B,
  28189. 0xEC, 0xB9, 0xF3, 0xDA, 0x42, 0x16, 0x03, 0xB6,
  28190. 0x1A, 0x51, 0xBC, 0x8E, 0x36, 0xCB, 0xD0, 0x53,
  28191. 0x85, 0x1C, 0x77, 0xB1, 0xB9, 0x26, 0xB1, 0x7A,
  28192. 0x27, 0x2A, 0xA9, 0x02, 0x32, 0x46, 0xB0, 0x2B,
  28193. 0x3E, 0xD4, 0x7F, 0x66, 0xA0, 0x0B, 0xD5, 0x68,
  28194. 0x48, 0x23, 0x63, 0x4E, 0x7C, 0xE5, 0x8C, 0xF8,
  28195. 0xF3, 0x06, 0xE3, 0x5B, 0x1E, 0x53, 0x22, 0x82,
  28196. 0x4D, 0x90, 0x48, 0x01, 0xF0, 0xA2, 0xFA, 0x7C,
  28197. 0x2B, 0xC9, 0xC2, 0x52, 0xB0, 0xA5, 0x6B, 0x7B,
  28198. 0xA2, 0xAB, 0x0F, 0x63, 0x60, 0x21, 0x74, 0x5A,
  28199. 0x70, 0xA9, 0xA4, 0x3E, 0x2B, 0x0A, 0x8D, 0x61,
  28200. 0x59, 0x70, 0xB6, 0x53, 0x09, 0x62, 0x4B, 0x51,
  28201. 0x84, 0xBC, 0xC3, 0x0B, 0x91, 0x16, 0x79, 0xAE,
  28202. 0xDD, 0x76, 0x02, 0x5F, 0xE3, 0x90, 0x8F, 0xD6,
  28203. 0x78, 0x97, 0xB0, 0xCF, 0x4B, 0xE5, 0xA6, 0xF5,
  28204. 0x41, 0x3D, 0x7D, 0xD9, 0x85, 0x64, 0xB2, 0x3E,
  28205. 0x42, 0xA9, 0x3E, 0x4A, 0xA8, 0x82, 0x1C, 0xD4,
  28206. 0x50, 0x54, 0xC6, 0x43, 0xED, 0xC1, 0x15, 0x8D,
  28207. 0xB6, 0xB3, 0xDE, 0xB1, 0x3F, 0xB5, 0xA5, 0x1E,
  28208. 0xBD, 0x1A, 0x8A, 0x78, 0xB8, 0x72, 0x25, 0xA7,
  28209. 0x33, 0x8E, 0x10, 0x11, 0x04, 0xC4, 0xA2, 0x20,
  28210. 0xD9, 0xBD, 0xED, 0xD4, 0x8C, 0x85, 0xA1, 0xC2,
  28211. 0xDA, 0xE7, 0x81, 0xA8, 0x0C, 0x40, 0xE1, 0x3B,
  28212. 0x87, 0xEA, 0xC7, 0x3A, 0x76, 0x42, 0x01, 0xC9,
  28213. 0xB7, 0x60, 0xCC, 0xFB, 0x1A, 0xE3, 0x92, 0x69,
  28214. 0x9C, 0x70, 0x39, 0xD2, 0x7C, 0x39, 0x36, 0x2B,
  28215. 0x27, 0xB8, 0xFC, 0x6F, 0x07, 0xA8, 0xA3, 0xD4,
  28216. 0x41, 0x0F, 0x15, 0x47, 0xC4, 0x8A, 0x99, 0x97,
  28217. 0xF6, 0x2C, 0x61, 0x07, 0x44, 0x52, 0xEF, 0x15,
  28218. 0x15, 0xF8, 0xA6, 0x49, 0xEB, 0xCA, 0x94, 0x37,
  28219. 0x20, 0x5A, 0x4E, 0x8A, 0x61, 0x60, 0x6B, 0x41,
  28220. 0xDA, 0xF6, 0x83, 0x4D, 0x67, 0x1F, 0x4D, 0x85,
  28221. 0x2C, 0x0C, 0x9C, 0x40, 0x96, 0x61, 0x16, 0x48,
  28222. 0xC6, 0xA3, 0x17, 0x06, 0x78, 0xB1, 0x53, 0x7C,
  28223. 0xC1, 0x82, 0x8D, 0x93, 0x58, 0x0C, 0x9E, 0x58,
  28224. 0x49, 0xA9, 0x65, 0x31, 0x75, 0xAC, 0xB7, 0x53,
  28225. 0xF2, 0xBE, 0x74, 0x37, 0xBE, 0x45, 0xF6, 0xC6,
  28226. 0x03, 0xE4, 0x85, 0xF2, 0xEC, 0x30, 0x1B, 0xB4,
  28227. 0x2B, 0x6C, 0x37, 0xC2, 0x25, 0xD7, 0x49, 0x5A,
  28228. 0x58, 0x4A, 0xE2, 0x31, 0x89, 0x0A, 0xB5, 0xC8,
  28229. 0xC3, 0x5C, 0x26, 0x8C, 0xF4, 0xBB, 0xB0, 0x21,
  28230. 0x3C, 0x09, 0x60, 0x19, 0x31, 0x95, 0x61, 0xA8,
  28231. 0xA6, 0x94, 0x76, 0x37, 0xAA, 0x40, 0xD0, 0x06,
  28232. 0xB4, 0x15, 0xBB, 0x2C, 0xFA, 0x22, 0x37, 0xE0,
  28233. 0x89, 0x0B, 0x6A, 0x3B, 0xC1, 0x34, 0xAB, 0xF8,
  28234. 0xF6, 0x58, 0x5E, 0x10, 0x8D, 0x15, 0x94, 0x0F,
  28235. 0x91, 0xF4, 0xBF, 0x5B, 0x0C, 0x81, 0x80, 0x55,
  28236. 0xB2, 0x1D, 0xEA, 0x6E, 0x63, 0xB5, 0x53, 0x98,
  28237. 0x8C, 0x47, 0xF4, 0xB9, 0x4E, 0x7C, 0xF8, 0x00,
  28238. 0xA4, 0x93, 0xB4, 0x73, 0x47, 0x05, 0xED, 0xC5,
  28239. 0x6A, 0x4B, 0x60, 0x21, 0xC6, 0x29, 0x50, 0x06,
  28240. 0x75, 0x87, 0x68, 0x04, 0xCF, 0x0B, 0x95, 0x1F,
  28241. 0x03, 0x8A, 0x5C, 0x7F, 0xE5, 0x8E, 0x89, 0x77,
  28242. 0x4E, 0xF2, 0x99, 0x2F, 0xD7, 0xC6, 0x30, 0x99,
  28243. 0xD3, 0x52, 0xA7, 0xD2, 0x15, 0x60, 0xB7, 0x88,
  28244. 0xB4, 0x05, 0x70, 0x98, 0x61, 0x81, 0x7E, 0x59,
  28245. 0xA9, 0x6B, 0x3A, 0x3A, 0x83, 0xCB, 0xA8, 0x03,
  28246. 0xB1, 0x69, 0x34, 0x33, 0x10, 0x71, 0x90, 0x5B,
  28247. 0xBE, 0xC6, 0x53, 0x29, 0x00, 0x15, 0x5D, 0x8A,
  28248. 0xC8, 0x8C, 0xB3, 0x2E, 0x4E, 0x21, 0xA3, 0xBD,
  28249. 0x3A, 0x03, 0xFD, 0xEC, 0x32, 0x5A, 0x51, 0xCD,
  28250. 0x27, 0x73, 0x96, 0x4E, 0x67, 0x84, 0xFC, 0xF1,
  28251. 0x85, 0x37, 0x37, 0xAA, 0x64, 0xEB, 0x67, 0x56,
  28252. 0x47, 0x27, 0x27, 0x26, 0x61, 0xAB, 0xF8, 0x43,
  28253. 0x13, 0xA5, 0x7A, 0x44, 0xB1, 0x23, 0xC6, 0x55,
  28254. 0x09, 0xCF, 0xB7, 0xA6, 0xF6, 0x64, 0x1C, 0xDC,
  28255. 0xC3, 0xB5, 0x7F, 0xE6, 0x28, 0xC7, 0xB8, 0x19,
  28256. 0x2D, 0xB4, 0x4F, 0xFB, 0xF5, 0x79, 0x6A, 0x86,
  28257. 0x13, 0xB1, 0xFA, 0x12, 0x6F, 0x60, 0x76, 0x88,
  28258. 0x3C, 0x78, 0x3D, 0xC2, 0x4E, 0x2A, 0x44, 0x64,
  28259. 0xC4, 0x0B, 0x3A, 0x41, 0xCA, 0x70, 0xAE, 0x87,
  28260. 0x62, 0x08, 0x66, 0xCF, 0x4F, 0xCB, 0x2B, 0xD2,
  28261. 0x04, 0xBF, 0x5C, 0x28, 0x38, 0x12, 0xBA, 0x05,
  28262. 0x6A, 0xC0, 0xC3, 0x45, 0xE3, 0x79, 0xC4, 0xBA,
  28263. 0x24, 0xD7, 0x50, 0x90, 0x12, 0x79, 0xBB, 0x2F,
  28264. 0x3A, 0x16, 0xF6, 0x12, 0xBF, 0xAD, 0xB3, 0x57,
  28265. 0x03, 0x33, 0x2C, 0x7C, 0x13, 0x6F, 0x68, 0xEA,
  28266. 0xB6, 0x75, 0x5C, 0x66, 0xB6, 0xA4, 0xAD, 0x1A,
  28267. 0xAB, 0xA7, 0xB7, 0x68, 0xA5, 0x8A, 0xCA, 0xAC,
  28268. 0xC1, 0x0A, 0x45, 0x9A, 0x1C, 0xC8, 0xEF, 0x29,
  28269. 0x37, 0x7B, 0xC2, 0x00, 0xE4, 0xD3, 0x15, 0xA3,
  28270. 0x0A, 0x6B, 0xCC, 0x32, 0x56, 0xF9, 0x73, 0x4D,
  28271. 0x06, 0xE9, 0x77, 0x9C, 0xAA, 0x54, 0x42, 0xA9,
  28272. 0xA1, 0x60, 0x69, 0x08, 0x13, 0x77, 0xC7, 0x6E,
  28273. 0x75, 0x15, 0x43, 0x68, 0x07, 0x2D, 0xC4, 0x46,
  28274. 0xED, 0x6C, 0x8B, 0x8E, 0x62, 0x2A, 0x21, 0xE3,
  28275. 0x83, 0xCF, 0x9B, 0xA1, 0xFB, 0x43, 0x4E, 0x2E,
  28276. 0xCC, 0x81, 0xE7, 0xB7, 0x8C, 0xEE, 0x98, 0x6B,
  28277. 0x8F, 0xF7, 0x98, 0xAB, 0x18, 0xCF, 0x96, 0x34,
  28278. 0x54, 0x35, 0x46, 0x28, 0x4E, 0xDA, 0x2A, 0x26,
  28279. 0xB4, 0x7F, 0x05, 0xB7, 0x35, 0xBC, 0xDB, 0x12,
  28280. 0x02, 0x22, 0x00, 0x76, 0xDC, 0x8B, 0x4E, 0x4B,
  28281. 0x9F, 0x85, 0x35, 0x33, 0xC8, 0xF6, 0xC7, 0xFF,
  28282. 0x38, 0x81, 0x7B, 0xA4, 0x97, 0x12, 0x83, 0x57,
  28283. 0x85, 0xF1, 0x7F, 0x14, 0xCA, 0x01, 0xD0, 0xC1,
  28284. 0xC1, 0xE9, 0x88, 0x10, 0xFE, 0x0B, 0x36, 0xE5,
  28285. 0xB4, 0x27, 0x15, 0x7B, 0x94, 0x18, 0x44, 0x9C,
  28286. 0xED, 0xD6, 0x41, 0xA4, 0x29, 0x3C, 0x85, 0xC3,
  28287. 0x27, 0x00, 0x10, 0x2A, 0xCE, 0xC2, 0x2E, 0xBA,
  28288. 0xD9, 0x8E, 0xD1, 0x60, 0xA5, 0xF0, 0x27, 0xBD,
  28289. 0x4C, 0xDA, 0x57, 0xF1, 0xF3, 0x72, 0x0A, 0x12,
  28290. 0xC1, 0x34, 0x65, 0x4D, 0xD5, 0xE7, 0x3F, 0x82,
  28291. 0x96, 0x76, 0x49, 0x53, 0x90, 0xD0, 0xE7, 0x92,
  28292. 0x9D, 0x60, 0x34, 0xE9, 0xC5, 0x5F, 0x7D, 0x55,
  28293. 0xBA, 0x65, 0x8B, 0xC5, 0x87, 0x98, 0x8E, 0x8A,
  28294. 0xF9, 0x49, 0x60, 0xF6, 0xCF, 0xB8, 0xD5, 0xAF,
  28295. 0x7A, 0x00, 0x21, 0x53, 0x5A, 0x6E, 0x25, 0xE4,
  28296. 0x37, 0xD4, 0x9A, 0x78, 0x06, 0x98, 0xBE, 0x22,
  28297. 0xAC, 0x99, 0x53, 0x94, 0x9F, 0x57, 0x1B, 0x85,
  28298. 0xA6, 0x85, 0x72, 0x5F, 0x82, 0x07, 0xA2, 0xB0,
  28299. 0xAE, 0x84, 0x9B, 0x60, 0x1A, 0xB9, 0x1B, 0x15,
  28300. 0x9B, 0x3D, 0xF4, 0xA1, 0x54, 0xC2, 0x04, 0x1E,
  28301. 0x77, 0x60, 0x70, 0xAF, 0xC4, 0x29, 0x69, 0x32,
  28302. 0x23, 0x80, 0x91, 0x7C, 0x97, 0x51, 0x07, 0x99,
  28303. 0xF3, 0x14, 0x91, 0x31, 0x47, 0x7E, 0x16, 0x66,
  28304. 0x3D, 0x31, 0x74, 0xC7, 0xC1, 0xCA, 0xEA, 0x78,
  28305. 0x85, 0x35, 0xC6, 0xC0, 0x05, 0xA6, 0x4F, 0x28,
  28306. 0x68, 0x63, 0x1B, 0x31, 0xB6, 0x6E, 0x20, 0x5F,
  28307. 0xD3, 0x8C, 0x1D, 0x84, 0x54, 0x2D, 0x0F, 0x1B,
  28308. 0x57, 0x8F, 0x58, 0xC9, 0xBF, 0x5A, 0x0F, 0xAE,
  28309. 0xAB, 0x6A, 0xB6, 0x49, 0x48, 0x93, 0x05, 0x31,
  28310. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  28311. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  28312. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  28313. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  28314. };
  28315. const byte kyber1024_sk[] = {
  28316. 0x07, 0x63, 0x8F, 0xB6, 0x98, 0x68, 0xF3, 0xD3,
  28317. 0x20, 0xE5, 0x86, 0x2B, 0xD9, 0x69, 0x33, 0xFE,
  28318. 0xB3, 0x11, 0xB3, 0x62, 0x09, 0x3C, 0x9B, 0x5D,
  28319. 0x50, 0x17, 0x0B, 0xCE, 0xD4, 0x3F, 0x1B, 0x53,
  28320. 0x6D, 0x9A, 0x20, 0x4B, 0xB1, 0xF2, 0x26, 0x95,
  28321. 0x95, 0x0B, 0xA1, 0xF2, 0xA9, 0xE8, 0xEB, 0x82,
  28322. 0x8B, 0x28, 0x44, 0x88, 0x76, 0x0B, 0x3F, 0xC8,
  28323. 0x4F, 0xAB, 0xA0, 0x42, 0x75, 0xD5, 0x62, 0x8E,
  28324. 0x39, 0xC5, 0xB2, 0x47, 0x13, 0x74, 0x28, 0x3C,
  28325. 0x50, 0x32, 0x99, 0xC0, 0xAB, 0x49, 0xB6, 0x6B,
  28326. 0x8B, 0xBB, 0x56, 0xA4, 0x18, 0x66, 0x24, 0xF9,
  28327. 0x19, 0xA2, 0xBA, 0x59, 0xBB, 0x08, 0xD8, 0x55,
  28328. 0x18, 0x80, 0xC2, 0xBE, 0xFC, 0x4F, 0x87, 0xF2,
  28329. 0x5F, 0x59, 0xAB, 0x58, 0x7A, 0x79, 0xC3, 0x27,
  28330. 0xD7, 0x92, 0xD5, 0x4C, 0x97, 0x4A, 0x69, 0x26,
  28331. 0x2F, 0xF8, 0xA7, 0x89, 0x38, 0x28, 0x9E, 0x9A,
  28332. 0x87, 0xB6, 0x88, 0xB0, 0x83, 0xE0, 0x59, 0x5F,
  28333. 0xE2, 0x18, 0xB6, 0xBB, 0x15, 0x05, 0x94, 0x1C,
  28334. 0xE2, 0xE8, 0x1A, 0x5A, 0x64, 0xC5, 0xAA, 0xC6,
  28335. 0x04, 0x17, 0x25, 0x69, 0x85, 0x34, 0x9E, 0xE4,
  28336. 0x7A, 0x52, 0x42, 0x0A, 0x5F, 0x97, 0x47, 0x7B,
  28337. 0x72, 0x36, 0xAC, 0x76, 0xBC, 0x70, 0xE8, 0x28,
  28338. 0x87, 0x29, 0x28, 0x7E, 0xE3, 0xE3, 0x4A, 0x3D,
  28339. 0xBC, 0x36, 0x83, 0xC0, 0xB7, 0xB1, 0x00, 0x29,
  28340. 0xFC, 0x20, 0x34, 0x18, 0x53, 0x7E, 0x74, 0x66,
  28341. 0xBA, 0x63, 0x85, 0xA8, 0xFF, 0x30, 0x1E, 0xE1,
  28342. 0x27, 0x08, 0xF8, 0x2A, 0xAA, 0x1E, 0x38, 0x0F,
  28343. 0xC7, 0xA8, 0x8F, 0x8F, 0x20, 0x5A, 0xB7, 0xE8,
  28344. 0x8D, 0x7E, 0x95, 0x95, 0x2A, 0x55, 0xBA, 0x20,
  28345. 0xD0, 0x9B, 0x79, 0xA4, 0x71, 0x41, 0xD6, 0x2B,
  28346. 0xF6, 0xEB, 0x7D, 0xD3, 0x07, 0xB0, 0x8E, 0xCA,
  28347. 0x13, 0xA5, 0xBC, 0x5F, 0x6B, 0x68, 0x58, 0x1C,
  28348. 0x68, 0x65, 0xB2, 0x7B, 0xBC, 0xDD, 0xAB, 0x14,
  28349. 0x2F, 0x4B, 0x2C, 0xBF, 0xF4, 0x88, 0xC8, 0xA2,
  28350. 0x27, 0x05, 0xFA, 0xA9, 0x8A, 0x2B, 0x9E, 0xEA,
  28351. 0x35, 0x30, 0xC7, 0x66, 0x62, 0x33, 0x5C, 0xC7,
  28352. 0xEA, 0x3A, 0x00, 0x77, 0x77, 0x25, 0xEB, 0xCC,
  28353. 0xCD, 0x2A, 0x46, 0x36, 0xB2, 0xD9, 0x12, 0x2F,
  28354. 0xF3, 0xAB, 0x77, 0x12, 0x3C, 0xE0, 0x88, 0x3C,
  28355. 0x19, 0x11, 0x11, 0x5E, 0x50, 0xC9, 0xE8, 0xA9,
  28356. 0x41, 0x94, 0xE4, 0x8D, 0xD0, 0xD0, 0x9C, 0xFF,
  28357. 0xB3, 0xAD, 0xCD, 0x2C, 0x1E, 0x92, 0x43, 0x09,
  28358. 0x03, 0xD0, 0x7A, 0xDB, 0xF0, 0x05, 0x32, 0x03,
  28359. 0x15, 0x75, 0xAA, 0x7F, 0x9E, 0x7B, 0x5A, 0x1F,
  28360. 0x33, 0x62, 0xDE, 0xC9, 0x36, 0xD4, 0x04, 0x3C,
  28361. 0x05, 0xF2, 0x47, 0x6C, 0x07, 0x57, 0x8B, 0xC9,
  28362. 0xCB, 0xAF, 0x2A, 0xB4, 0xE3, 0x82, 0x72, 0x7A,
  28363. 0xD4, 0x16, 0x86, 0xA9, 0x6B, 0x25, 0x48, 0x82,
  28364. 0x0B, 0xB0, 0x3B, 0x32, 0xF1, 0x1B, 0x28, 0x11,
  28365. 0xAD, 0x62, 0xF4, 0x89, 0xE9, 0x51, 0x63, 0x2A,
  28366. 0xBA, 0x0D, 0x1D, 0xF8, 0x96, 0x80, 0xCC, 0x8A,
  28367. 0x8B, 0x53, 0xB4, 0x81, 0xD9, 0x2A, 0x68, 0xD7,
  28368. 0x0B, 0x4E, 0xA1, 0xC3, 0xA6, 0xA5, 0x61, 0xC0,
  28369. 0x69, 0x28, 0x82, 0xB5, 0xCA, 0x8C, 0xC9, 0x42,
  28370. 0xA8, 0xD4, 0x95, 0xAF, 0xCB, 0x06, 0xDE, 0x89,
  28371. 0x49, 0x8F, 0xB9, 0x35, 0xB7, 0x75, 0x90, 0x8F,
  28372. 0xE7, 0xA0, 0x3E, 0x32, 0x4D, 0x54, 0xCC, 0x19,
  28373. 0xD4, 0xE1, 0xAA, 0xBD, 0x35, 0x93, 0xB3, 0x8B,
  28374. 0x19, 0xEE, 0x13, 0x88, 0xFE, 0x49, 0x2B, 0x43,
  28375. 0x12, 0x7E, 0x5A, 0x50, 0x42, 0x53, 0x78, 0x6A,
  28376. 0x0D, 0x69, 0xAD, 0x32, 0x60, 0x1C, 0x28, 0xE2,
  28377. 0xC8, 0x85, 0x04, 0xA5, 0xBA, 0x59, 0x97, 0x06,
  28378. 0x02, 0x3A, 0x61, 0x36, 0x3E, 0x17, 0xC6, 0xB9,
  28379. 0xBB, 0x59, 0xBD, 0xC6, 0x97, 0x45, 0x2C, 0xD0,
  28380. 0x59, 0x45, 0x19, 0x83, 0xD7, 0x38, 0xCA, 0x3F,
  28381. 0xD0, 0x34, 0xE3, 0xF5, 0x98, 0x88, 0x54, 0xCA,
  28382. 0x05, 0x03, 0x1D, 0xB0, 0x96, 0x11, 0x49, 0x89,
  28383. 0x88, 0x19, 0x7C, 0x6B, 0x30, 0xD2, 0x58, 0xDF,
  28384. 0xE2, 0x62, 0x65, 0x54, 0x1C, 0x89, 0xA4, 0xB3,
  28385. 0x1D, 0x68, 0x64, 0xE9, 0x38, 0x9B, 0x03, 0xCB,
  28386. 0x74, 0xF7, 0xEC, 0x43, 0x23, 0xFB, 0x94, 0x21,
  28387. 0xA4, 0xB9, 0x79, 0x0A, 0x26, 0xD1, 0x7B, 0x03,
  28388. 0x98, 0xA2, 0x67, 0x67, 0x35, 0x09, 0x09, 0xF8,
  28389. 0x4D, 0x57, 0xB6, 0x69, 0x4D, 0xF8, 0x30, 0x66,
  28390. 0x4C, 0xA8, 0xB3, 0xC3, 0xC0, 0x3E, 0xD2, 0xAE,
  28391. 0x67, 0xB8, 0x90, 0x06, 0x86, 0x8A, 0x68, 0x52,
  28392. 0x7C, 0xCD, 0x66, 0x64, 0x59, 0xAB, 0x7F, 0x05,
  28393. 0x66, 0x71, 0x00, 0x0C, 0x61, 0x64, 0xD3, 0xA7,
  28394. 0xF2, 0x66, 0xA1, 0x4D, 0x97, 0xCB, 0xD7, 0x00,
  28395. 0x4D, 0x6C, 0x92, 0xCA, 0xCA, 0x77, 0x0B, 0x84,
  28396. 0x4A, 0x4F, 0xA9, 0xB1, 0x82, 0xE7, 0xB1, 0x8C,
  28397. 0xA8, 0x85, 0x08, 0x2A, 0xC5, 0x64, 0x6F, 0xCB,
  28398. 0x4A, 0x14, 0xE1, 0x68, 0x5F, 0xEB, 0x0C, 0x9C,
  28399. 0xE3, 0x37, 0x2A, 0xB9, 0x53, 0x65, 0xC0, 0x4F,
  28400. 0xD8, 0x30, 0x84, 0xF8, 0x0A, 0x23, 0xFF, 0x10,
  28401. 0xA0, 0x5B, 0xF1, 0x5F, 0x7F, 0xA5, 0xAC, 0xC6,
  28402. 0xC0, 0xCB, 0x46, 0x2C, 0x33, 0xCA, 0x52, 0x4F,
  28403. 0xA6, 0xB8, 0xBB, 0x35, 0x90, 0x43, 0xBA, 0x68,
  28404. 0x60, 0x9E, 0xAA, 0x25, 0x36, 0xE8, 0x1D, 0x08,
  28405. 0x46, 0x3B, 0x19, 0x65, 0x3B, 0x54, 0x35, 0xBA,
  28406. 0x94, 0x6C, 0x9A, 0xDD, 0xEB, 0x20, 0x2B, 0x04,
  28407. 0xB0, 0x31, 0xCC, 0x96, 0x0D, 0xCC, 0x12, 0xE4,
  28408. 0x51, 0x8D, 0x42, 0x8B, 0x32, 0xB2, 0x57, 0xA4,
  28409. 0xFC, 0x73, 0x13, 0xD3, 0xA7, 0x98, 0x0D, 0x80,
  28410. 0x08, 0x2E, 0x93, 0x4F, 0x9D, 0x95, 0xC3, 0x2B,
  28411. 0x0A, 0x01, 0x91, 0xA2, 0x36, 0x04, 0x38, 0x4D,
  28412. 0xD9, 0xE0, 0x79, 0xBB, 0xBA, 0xA2, 0x66, 0xD1,
  28413. 0x4C, 0x3F, 0x75, 0x6B, 0x9F, 0x21, 0x33, 0x10,
  28414. 0x74, 0x33, 0xA4, 0xE8, 0x3F, 0xA7, 0x18, 0x72,
  28415. 0x82, 0xA8, 0x09, 0x20, 0x3A, 0x4F, 0xAF, 0x84,
  28416. 0x18, 0x51, 0x83, 0x3D, 0x12, 0x1A, 0xC3, 0x83,
  28417. 0x84, 0x3A, 0x5E, 0x55, 0xBC, 0x23, 0x81, 0x42,
  28418. 0x5E, 0x16, 0xC7, 0xDB, 0x4C, 0xC9, 0xAB, 0x5C,
  28419. 0x1B, 0x0D, 0x91, 0xA4, 0x7E, 0x2B, 0x8D, 0xE0,
  28420. 0xE5, 0x82, 0xC8, 0x6B, 0x6B, 0x0D, 0x90, 0x7B,
  28421. 0xB3, 0x60, 0xB9, 0x7F, 0x40, 0xAB, 0x5D, 0x03,
  28422. 0x8F, 0x6B, 0x75, 0xC8, 0x14, 0xB2, 0x7D, 0x9B,
  28423. 0x96, 0x8D, 0x41, 0x98, 0x32, 0xBC, 0x8C, 0x2B,
  28424. 0xEE, 0x60, 0x5E, 0xF6, 0xE5, 0x05, 0x9D, 0x33,
  28425. 0x10, 0x0D, 0x90, 0x48, 0x5D, 0x37, 0x84, 0x50,
  28426. 0x01, 0x42, 0x21, 0x73, 0x6C, 0x07, 0x40, 0x7C,
  28427. 0xAC, 0x26, 0x04, 0x08, 0xAA, 0x64, 0x92, 0x66,
  28428. 0x19, 0x78, 0x8B, 0x86, 0x01, 0xC2, 0xA7, 0x52,
  28429. 0xD1, 0xA6, 0xCB, 0xF8, 0x20, 0xD7, 0xC7, 0xA0,
  28430. 0x47, 0x16, 0x20, 0x32, 0x25, 0xB3, 0x89, 0x5B,
  28431. 0x93, 0x42, 0xD1, 0x47, 0xA8, 0x18, 0x5C, 0xFC,
  28432. 0x1B, 0xB6, 0x5B, 0xA0, 0x6B, 0x41, 0x42, 0x33,
  28433. 0x99, 0x03, 0xC0, 0xAC, 0x46, 0x51, 0x38, 0x5B,
  28434. 0x45, 0xD9, 0x8A, 0x8B, 0x19, 0xD2, 0x8C, 0xD6,
  28435. 0xBA, 0xB0, 0x88, 0x78, 0x7F, 0x7E, 0xE1, 0xB1,
  28436. 0x24, 0x61, 0x76, 0x6B, 0x43, 0xCB, 0xCC, 0xB9,
  28437. 0x64, 0x34, 0x42, 0x7D, 0x93, 0xC0, 0x65, 0x55,
  28438. 0x06, 0x88, 0xF6, 0x94, 0x8E, 0xD1, 0xB5, 0x47,
  28439. 0x5A, 0x42, 0x5F, 0x1B, 0x85, 0x20, 0x9D, 0x06,
  28440. 0x1C, 0x08, 0xB5, 0x6C, 0x1C, 0xC0, 0x69, 0xF6,
  28441. 0xC0, 0xA7, 0xC6, 0xF2, 0x93, 0x58, 0xCA, 0xB9,
  28442. 0x11, 0x08, 0x77, 0x32, 0xA6, 0x49, 0xD2, 0x7C,
  28443. 0x9B, 0x98, 0xF9, 0xA4, 0x88, 0x79, 0x38, 0x7D,
  28444. 0x9B, 0x00, 0xC2, 0x59, 0x59, 0xA7, 0x16, 0x54,
  28445. 0xD6, 0xF6, 0xA9, 0x46, 0x16, 0x45, 0x13, 0xE4,
  28446. 0x7A, 0x75, 0xD0, 0x05, 0x98, 0x6C, 0x23, 0x63,
  28447. 0xC0, 0x9F, 0x6B, 0x53, 0x7E, 0xCA, 0x78, 0xB9,
  28448. 0x30, 0x3A, 0x5F, 0xA4, 0x57, 0x60, 0x8A, 0x58,
  28449. 0x6A, 0x65, 0x3A, 0x34, 0x7D, 0xB0, 0x4D, 0xFC,
  28450. 0xC1, 0x91, 0x75, 0xB3, 0xA3, 0x01, 0x17, 0x25,
  28451. 0x36, 0x06, 0x2A, 0x65, 0x8A, 0x95, 0x27, 0x75,
  28452. 0x70, 0xC8, 0x85, 0x2C, 0xA8, 0x97, 0x3F, 0x4A,
  28453. 0xE1, 0x23, 0xA3, 0x34, 0x04, 0x7D, 0xD7, 0x11,
  28454. 0xC8, 0x92, 0x7A, 0x63, 0x4A, 0x03, 0x38, 0x8A,
  28455. 0x52, 0x7B, 0x03, 0x4B, 0xF7, 0xA8, 0x17, 0x0F,
  28456. 0xA7, 0x02, 0xC1, 0xF7, 0xC2, 0x3E, 0xC3, 0x2D,
  28457. 0x18, 0xA2, 0x37, 0x48, 0x90, 0xBE, 0x9C, 0x78,
  28458. 0x7A, 0x94, 0x09, 0xC8, 0x2D, 0x19, 0x2C, 0x4B,
  28459. 0xB7, 0x05, 0xA2, 0xF9, 0x96, 0xCE, 0x40, 0x5D,
  28460. 0x85, 0xA4, 0xC1, 0xA1, 0xAB, 0x9B, 0x6A, 0xEB,
  28461. 0x49, 0xCC, 0xE1, 0xC2, 0xF8, 0xA9, 0x7C, 0x35,
  28462. 0x16, 0xC7, 0x2A, 0x00, 0xA4, 0x62, 0x63, 0xBA,
  28463. 0xA6, 0x96, 0xBF, 0x25, 0x72, 0x77, 0x19, 0xC3,
  28464. 0x21, 0x64, 0x23, 0x61, 0x8F, 0xF3, 0x33, 0x80,
  28465. 0x93, 0x4A, 0x6C, 0x10, 0x54, 0x5C, 0x4C, 0x5C,
  28466. 0x51, 0x55, 0xB1, 0x24, 0x86, 0x18, 0x1F, 0xC7,
  28467. 0xA2, 0x31, 0x98, 0x73, 0x97, 0x8B, 0x6A, 0x2A,
  28468. 0x67, 0x49, 0x0F, 0x82, 0x56, 0xBD, 0x21, 0x96,
  28469. 0xFE, 0x17, 0x92, 0xA4, 0xC0, 0x00, 0x77, 0xB8,
  28470. 0x12, 0xEA, 0xE8, 0xBE, 0xD3, 0x57, 0x24, 0x99,
  28471. 0x68, 0x4A, 0xB3, 0x37, 0x18, 0x76, 0x76, 0x1E,
  28472. 0x45, 0x0C, 0x9F, 0x9D, 0x27, 0x68, 0xA3, 0x68,
  28473. 0x06, 0xD7, 0xAB, 0x20, 0x46, 0xC9, 0x1F, 0x17,
  28474. 0x59, 0x9E, 0x9A, 0xC5, 0x92, 0x99, 0x08, 0x08,
  28475. 0xDC, 0xD7, 0xB4, 0xD0, 0x91, 0x90, 0x72, 0xF1,
  28476. 0x4E, 0xC3, 0x61, 0x77, 0x3B, 0x72, 0x52, 0x44,
  28477. 0x4C, 0x32, 0x3C, 0x30, 0x83, 0x26, 0xF4, 0xA3,
  28478. 0x0F, 0x86, 0x80, 0xD2, 0xF7, 0x48, 0xF5, 0x6A,
  28479. 0x13, 0x2B, 0x82, 0x67, 0x4E, 0xD0, 0x18, 0x46,
  28480. 0x20, 0xB8, 0x2A, 0xD2, 0xCB, 0x18, 0x2C, 0x97,
  28481. 0xB4, 0x81, 0x62, 0x66, 0x47, 0x49, 0x12, 0x90,
  28482. 0xA0, 0x11, 0xCC, 0x73, 0x82, 0x86, 0x85, 0xA8,
  28483. 0xC3, 0x67, 0xA5, 0xB9, 0xCF, 0x8D, 0x62, 0x1B,
  28484. 0x0D, 0x5C, 0x1E, 0xFF, 0x03, 0x17, 0x27, 0x58,
  28485. 0xBD, 0x00, 0x49, 0x78, 0xC2, 0x51, 0xCD, 0x51,
  28486. 0x34, 0x22, 0x28, 0x98, 0x9C, 0xAE, 0x63, 0x32,
  28487. 0xAC, 0x48, 0x64, 0x37, 0xCB, 0x5C, 0x57, 0xD4,
  28488. 0x30, 0x74, 0x62, 0x86, 0x52, 0x53, 0xBE, 0x21,
  28489. 0x7B, 0x35, 0x15, 0xC7, 0x3D, 0xF4, 0x05, 0xB7,
  28490. 0xF2, 0x82, 0x17, 0xAD, 0x0B, 0x8C, 0xF6, 0x0C,
  28491. 0x2F, 0xFF, 0xAA, 0x0A, 0x00, 0x48, 0xB1, 0xFB,
  28492. 0x4A, 0xCD, 0xCD, 0xC3, 0x8B, 0x52, 0x50, 0xCF,
  28493. 0xEC, 0x35, 0x6A, 0x6D, 0xE2, 0x6C, 0xFA, 0x7A,
  28494. 0x58, 0x8F, 0xDC, 0x86, 0xF9, 0x8C, 0x85, 0x4A,
  28495. 0xC6, 0x4C, 0x7B, 0xFA, 0xA9, 0x6F, 0x5A, 0x32,
  28496. 0xCC, 0x06, 0x10, 0x93, 0x4B, 0xAA, 0x6A, 0x58,
  28497. 0x6B, 0x9A, 0x20, 0x54, 0xF1, 0x3B, 0xA2, 0x74,
  28498. 0x17, 0x4A, 0xA0, 0xD2, 0xB3, 0xA8, 0x1B, 0x96,
  28499. 0xA9, 0x40, 0x66, 0x6F, 0x78, 0x9B, 0x5A, 0x6B,
  28500. 0xCD, 0xC0, 0xA6, 0xA0, 0x17, 0x8A, 0x0C, 0x9A,
  28501. 0x02, 0x57, 0x8A, 0x49, 0x3F, 0x6E, 0xEA, 0x0D,
  28502. 0x2E, 0x6C, 0x13, 0x95, 0x1C, 0x9F, 0x24, 0x9A,
  28503. 0x5E, 0x8D, 0xD7, 0x1D, 0xD4, 0x9A, 0x74, 0x2D,
  28504. 0x45, 0x1F, 0x1A, 0xBB, 0xA1, 0x9A, 0xF8, 0xC5,
  28505. 0x47, 0x85, 0x5E, 0x0A, 0xFC, 0x72, 0x8E, 0x90,
  28506. 0xAB, 0xB4, 0x99, 0xC9, 0xBE, 0xEB, 0x76, 0x6F,
  28507. 0x47, 0x29, 0xCD, 0xA2, 0x22, 0x63, 0xE3, 0x24,
  28508. 0xD2, 0x23, 0x02, 0xCB, 0xD3, 0x39, 0x9F, 0xAC,
  28509. 0xC6, 0x30, 0x99, 0x1F, 0xC8, 0xF2, 0x8B, 0xDB,
  28510. 0x43, 0x54, 0x76, 0x25, 0x41, 0x52, 0x76, 0x78,
  28511. 0xBC, 0xF6, 0x1F, 0x65, 0xC2, 0x41, 0x14, 0x6C,
  28512. 0x42, 0x6D, 0x23, 0xB9, 0xBF, 0xAA, 0x6B, 0x7D,
  28513. 0xF1, 0x8C, 0x97, 0xF2, 0x0C, 0x1B, 0x61, 0x25,
  28514. 0xBF, 0x87, 0x4B, 0x1D, 0x89, 0x47, 0x58, 0x52,
  28515. 0xC4, 0x48, 0x21, 0x5D, 0xB0, 0xEB, 0x77, 0x37,
  28516. 0xF9, 0x14, 0x80, 0xE8, 0xCE, 0xBD, 0x9A, 0x08,
  28517. 0x71, 0x57, 0x4F, 0x5A, 0xB6, 0x2D, 0x90, 0x20,
  28518. 0x17, 0x5E, 0xC6, 0x92, 0x7C, 0xA0, 0xB5, 0x4C,
  28519. 0x09, 0x81, 0x8E, 0x42, 0xCF, 0x92, 0xA3, 0x83,
  28520. 0x17, 0x24, 0x22, 0xC7, 0xDC, 0x18, 0x31, 0xD6,
  28521. 0x3B, 0x0C, 0x29, 0x5D, 0xE7, 0x51, 0x59, 0xDB,
  28522. 0x80, 0x34, 0xE9, 0xE0, 0x7F, 0x7B, 0x0B, 0x91,
  28523. 0x0C, 0x3C, 0x1E, 0x5F, 0xB6, 0x6B, 0x3D, 0xC5,
  28524. 0x23, 0xF1, 0xFA, 0x6E, 0xB4, 0x91, 0x0C, 0xB8,
  28525. 0x9A, 0x6C, 0x17, 0x56, 0x2C, 0x83, 0xAB, 0x4C,
  28526. 0x18, 0xD0, 0xCD, 0x7E, 0x07, 0x96, 0x59, 0x2A,
  28527. 0x37, 0x2A, 0xA4, 0x09, 0xB1, 0xC5, 0x57, 0x34,
  28528. 0x7C, 0xCA, 0xCD, 0xC4, 0x64, 0x4A, 0x11, 0x90,
  28529. 0x64, 0xD0, 0x6D, 0xD4, 0x74, 0x92, 0x9D, 0x1C,
  28530. 0x6F, 0xB4, 0xD6, 0x86, 0xE5, 0x49, 0x1C, 0xE4,
  28531. 0xBC, 0x89, 0xA3, 0x0B, 0xB4, 0xB8, 0xC4, 0x1B,
  28532. 0xCE, 0x51, 0x57, 0xDF, 0xC1, 0x36, 0x08, 0x23,
  28533. 0xB1, 0xAB, 0x61, 0x8C, 0x14, 0xB1, 0x0F, 0x98,
  28534. 0xC2, 0x50, 0x67, 0x39, 0x8E, 0xA7, 0x01, 0x8C,
  28535. 0x27, 0x8A, 0x4B, 0x3D, 0xF3, 0x13, 0x34, 0xD6,
  28536. 0x03, 0xB2, 0x04, 0x4E, 0xF1, 0x87, 0xCD, 0x9B,
  28537. 0xC6, 0xCE, 0x42, 0x72, 0x5B, 0xD9, 0x62, 0xC2,
  28538. 0x64, 0x98, 0x3E, 0x9E, 0x18, 0x15, 0x5A, 0x8B,
  28539. 0x9C, 0x47, 0x14, 0x3D, 0x70, 0x46, 0x0A, 0x26,
  28540. 0xA5, 0x6F, 0xE7, 0x65, 0x8C, 0x1F, 0x15, 0x03,
  28541. 0x48, 0xC6, 0x08, 0x7E, 0xF7, 0x58, 0xAD, 0x16,
  28542. 0x78, 0x87, 0x86, 0x0A, 0x00, 0x7A, 0x5F, 0xC3,
  28543. 0x73, 0x58, 0xD4, 0x3B, 0x5E, 0xBE, 0xE8, 0x20,
  28544. 0xAC, 0xEA, 0x47, 0x4F, 0x0A, 0xC0, 0x7B, 0x76,
  28545. 0x80, 0x28, 0x66, 0x19, 0x9C, 0x61, 0x23, 0x1D,
  28546. 0x5C, 0x74, 0x7C, 0x93, 0x77, 0x4D, 0x2C, 0x1E,
  28547. 0x0C, 0x1C, 0x67, 0xE6, 0xC8, 0x1B, 0x82, 0x75,
  28548. 0x21, 0x73, 0xE1, 0x25, 0xBA, 0xF3, 0x9B, 0x4F,
  28549. 0xD1, 0x9A, 0x4F, 0x45, 0x3D, 0xC5, 0x79, 0x76,
  28550. 0xB1, 0xD9, 0x7F, 0xE6, 0x99, 0x69, 0x92, 0xBB,
  28551. 0xB6, 0x5B, 0x7C, 0xB2, 0x5D, 0x07, 0x7B, 0xBA,
  28552. 0xA6, 0xA1, 0x33, 0x22, 0x89, 0x9A, 0xF6, 0x59,
  28553. 0xCF, 0x1B, 0x35, 0x58, 0xC1, 0xB5, 0x00, 0x11,
  28554. 0x54, 0xB6, 0x25, 0x80, 0x9E, 0xD8, 0x9A, 0xEE,
  28555. 0xBB, 0x89, 0xE6, 0xEA, 0x7D, 0x67, 0xF7, 0x23,
  28556. 0xD0, 0x45, 0xAB, 0x05, 0x71, 0x5C, 0x42, 0x35,
  28557. 0x5D, 0xA6, 0xA5, 0xC8, 0xDD, 0x39, 0xC8, 0xAB,
  28558. 0xE3, 0x03, 0x77, 0x51, 0xA0, 0x1E, 0xD1, 0xC7,
  28559. 0x37, 0x49, 0x19, 0xF3, 0x12, 0x1B, 0x5A, 0x52,
  28560. 0xC5, 0x3D, 0x14, 0x87, 0x31, 0x67, 0x69, 0xF8,
  28561. 0x07, 0x21, 0xDE, 0xEA, 0xAA, 0xD3, 0xC9, 0x0F,
  28562. 0x76, 0xE7, 0xAE, 0x9E, 0x12, 0xBA, 0x92, 0xB3,
  28563. 0x2B, 0x5F, 0xD4, 0x57, 0xE3, 0xC7, 0x52, 0xC2,
  28564. 0x65, 0x0D, 0xFB, 0x88, 0x57, 0x71, 0xCB, 0x77,
  28565. 0xAC, 0x3C, 0x78, 0x5A, 0x8C, 0x56, 0x2E, 0x6A,
  28566. 0x1C, 0x63, 0xC2, 0xA5, 0x5E, 0xA4, 0x7C, 0xF8,
  28567. 0xB9, 0x0E, 0xB8, 0x22, 0x5C, 0x12, 0x3C, 0x34,
  28568. 0x64, 0x52, 0x56, 0x62, 0x35, 0xB2, 0xF3, 0x18,
  28569. 0x23, 0xA3, 0x35, 0x21, 0xE0, 0x87, 0x93, 0x7A,
  28570. 0x34, 0x5D, 0x8D, 0x66, 0x3E, 0xEA, 0xA0, 0x56,
  28571. 0x58, 0x91, 0x7B, 0xBA, 0xA0, 0x08, 0xC2, 0xE3,
  28572. 0x35, 0xF8, 0x85, 0x0A, 0x90, 0xA3, 0x26, 0xD0,
  28573. 0xE6, 0x64, 0x32, 0xF4, 0x4C, 0xEB, 0x82, 0x89,
  28574. 0xE4, 0xEC, 0xB2, 0xD1, 0x29, 0x58, 0xE9, 0x84,
  28575. 0x07, 0x2E, 0xCA, 0xCB, 0x88, 0xE1, 0x34, 0x8F,
  28576. 0xF0, 0xB5, 0x56, 0x54, 0xAC, 0xBA, 0x5B, 0x54,
  28577. 0x97, 0x1C, 0xBA, 0xEB, 0xA8, 0x8E, 0xC4, 0xB9,
  28578. 0x1A, 0x94, 0xC3, 0x71, 0x92, 0xFA, 0x98, 0x2B,
  28579. 0xEC, 0xB9, 0xF3, 0xDA, 0x42, 0x16, 0x03, 0xB6,
  28580. 0x1A, 0x51, 0xBC, 0x8E, 0x36, 0xCB, 0xD0, 0x53,
  28581. 0x85, 0x1C, 0x77, 0xB1, 0xB9, 0x26, 0xB1, 0x7A,
  28582. 0x27, 0x2A, 0xA9, 0x02, 0x32, 0x46, 0xB0, 0x2B,
  28583. 0x3E, 0xD4, 0x7F, 0x66, 0xA0, 0x0B, 0xD5, 0x68,
  28584. 0x48, 0x23, 0x63, 0x4E, 0x7C, 0xE5, 0x8C, 0xF8,
  28585. 0xF3, 0x06, 0xE3, 0x5B, 0x1E, 0x53, 0x22, 0x82,
  28586. 0x4D, 0x90, 0x48, 0x01, 0xF0, 0xA2, 0xFA, 0x7C,
  28587. 0x2B, 0xC9, 0xC2, 0x52, 0xB0, 0xA5, 0x6B, 0x7B,
  28588. 0xA2, 0xAB, 0x0F, 0x63, 0x60, 0x21, 0x74, 0x5A,
  28589. 0x70, 0xA9, 0xA4, 0x3E, 0x2B, 0x0A, 0x8D, 0x61,
  28590. 0x59, 0x70, 0xB6, 0x53, 0x09, 0x62, 0x4B, 0x51,
  28591. 0x84, 0xBC, 0xC3, 0x0B, 0x91, 0x16, 0x79, 0xAE,
  28592. 0xDD, 0x76, 0x02, 0x5F, 0xE3, 0x90, 0x8F, 0xD6,
  28593. 0x78, 0x97, 0xB0, 0xCF, 0x4B, 0xE5, 0xA6, 0xF5,
  28594. 0x41, 0x3D, 0x7D, 0xD9, 0x85, 0x64, 0xB2, 0x3E,
  28595. 0x42, 0xA9, 0x3E, 0x4A, 0xA8, 0x82, 0x1C, 0xD4,
  28596. 0x50, 0x54, 0xC6, 0x43, 0xED, 0xC1, 0x15, 0x8D,
  28597. 0xB6, 0xB3, 0xDE, 0xB1, 0x3F, 0xB5, 0xA5, 0x1E,
  28598. 0xBD, 0x1A, 0x8A, 0x78, 0xB8, 0x72, 0x25, 0xA7,
  28599. 0x33, 0x8E, 0x10, 0x11, 0x04, 0xC4, 0xA2, 0x20,
  28600. 0xD9, 0xBD, 0xED, 0xD4, 0x8C, 0x85, 0xA1, 0xC2,
  28601. 0xDA, 0xE7, 0x81, 0xA8, 0x0C, 0x40, 0xE1, 0x3B,
  28602. 0x87, 0xEA, 0xC7, 0x3A, 0x76, 0x42, 0x01, 0xC9,
  28603. 0xB7, 0x60, 0xCC, 0xFB, 0x1A, 0xE3, 0x92, 0x69,
  28604. 0x9C, 0x70, 0x39, 0xD2, 0x7C, 0x39, 0x36, 0x2B,
  28605. 0x27, 0xB8, 0xFC, 0x6F, 0x07, 0xA8, 0xA3, 0xD4,
  28606. 0x41, 0x0F, 0x15, 0x47, 0xC4, 0x8A, 0x99, 0x97,
  28607. 0xF6, 0x2C, 0x61, 0x07, 0x44, 0x52, 0xEF, 0x15,
  28608. 0x15, 0xF8, 0xA6, 0x49, 0xEB, 0xCA, 0x94, 0x37,
  28609. 0x20, 0x5A, 0x4E, 0x8A, 0x61, 0x60, 0x6B, 0x41,
  28610. 0xDA, 0xF6, 0x83, 0x4D, 0x67, 0x1F, 0x4D, 0x85,
  28611. 0x2C, 0x0C, 0x9C, 0x40, 0x96, 0x61, 0x16, 0x48,
  28612. 0xC6, 0xA3, 0x17, 0x06, 0x78, 0xB1, 0x53, 0x7C,
  28613. 0xC1, 0x82, 0x8D, 0x93, 0x58, 0x0C, 0x9E, 0x58,
  28614. 0x49, 0xA9, 0x65, 0x31, 0x75, 0xAC, 0xB7, 0x53,
  28615. 0xF2, 0xBE, 0x74, 0x37, 0xBE, 0x45, 0xF6, 0xC6,
  28616. 0x03, 0xE4, 0x85, 0xF2, 0xEC, 0x30, 0x1B, 0xB4,
  28617. 0x2B, 0x6C, 0x37, 0xC2, 0x25, 0xD7, 0x49, 0x5A,
  28618. 0x58, 0x4A, 0xE2, 0x31, 0x89, 0x0A, 0xB5, 0xC8,
  28619. 0xC3, 0x5C, 0x26, 0x8C, 0xF4, 0xBB, 0xB0, 0x21,
  28620. 0x3C, 0x09, 0x60, 0x19, 0x31, 0x95, 0x61, 0xA8,
  28621. 0xA6, 0x94, 0x76, 0x37, 0xAA, 0x40, 0xD0, 0x06,
  28622. 0xB4, 0x15, 0xBB, 0x2C, 0xFA, 0x22, 0x37, 0xE0,
  28623. 0x89, 0x0B, 0x6A, 0x3B, 0xC1, 0x34, 0xAB, 0xF8,
  28624. 0xF6, 0x58, 0x5E, 0x10, 0x8D, 0x15, 0x94, 0x0F,
  28625. 0x91, 0xF4, 0xBF, 0x5B, 0x0C, 0x81, 0x80, 0x55,
  28626. 0xB2, 0x1D, 0xEA, 0x6E, 0x63, 0xB5, 0x53, 0x98,
  28627. 0x8C, 0x47, 0xF4, 0xB9, 0x4E, 0x7C, 0xF8, 0x00,
  28628. 0xA4, 0x93, 0xB4, 0x73, 0x47, 0x05, 0xED, 0xC5,
  28629. 0x6A, 0x4B, 0x60, 0x21, 0xC6, 0x29, 0x50, 0x06,
  28630. 0x75, 0x87, 0x68, 0x04, 0xCF, 0x0B, 0x95, 0x1F,
  28631. 0x03, 0x8A, 0x5C, 0x7F, 0xE5, 0x8E, 0x89, 0x77,
  28632. 0x4E, 0xF2, 0x99, 0x2F, 0xD7, 0xC6, 0x30, 0x99,
  28633. 0xD3, 0x52, 0xA7, 0xD2, 0x15, 0x60, 0xB7, 0x88,
  28634. 0xB4, 0x05, 0x70, 0x98, 0x61, 0x81, 0x7E, 0x59,
  28635. 0xA9, 0x6B, 0x3A, 0x3A, 0x83, 0xCB, 0xA8, 0x03,
  28636. 0xB1, 0x69, 0x34, 0x33, 0x10, 0x71, 0x90, 0x5B,
  28637. 0xBE, 0xC6, 0x53, 0x29, 0x00, 0x15, 0x5D, 0x8A,
  28638. 0xC8, 0x8C, 0xB3, 0x2E, 0x4E, 0x21, 0xA3, 0xBD,
  28639. 0x3A, 0x03, 0xFD, 0xEC, 0x32, 0x5A, 0x51, 0xCD,
  28640. 0x27, 0x73, 0x96, 0x4E, 0x67, 0x84, 0xFC, 0xF1,
  28641. 0x85, 0x37, 0x37, 0xAA, 0x64, 0xEB, 0x67, 0x56,
  28642. 0x47, 0x27, 0x27, 0x26, 0x61, 0xAB, 0xF8, 0x43,
  28643. 0x13, 0xA5, 0x7A, 0x44, 0xB1, 0x23, 0xC6, 0x55,
  28644. 0x09, 0xCF, 0xB7, 0xA6, 0xF6, 0x64, 0x1C, 0xDC,
  28645. 0xC3, 0xB5, 0x7F, 0xE6, 0x28, 0xC7, 0xB8, 0x19,
  28646. 0x2D, 0xB4, 0x4F, 0xFB, 0xF5, 0x79, 0x6A, 0x86,
  28647. 0x13, 0xB1, 0xFA, 0x12, 0x6F, 0x60, 0x76, 0x88,
  28648. 0x3C, 0x78, 0x3D, 0xC2, 0x4E, 0x2A, 0x44, 0x64,
  28649. 0xC4, 0x0B, 0x3A, 0x41, 0xCA, 0x70, 0xAE, 0x87,
  28650. 0x62, 0x08, 0x66, 0xCF, 0x4F, 0xCB, 0x2B, 0xD2,
  28651. 0x04, 0xBF, 0x5C, 0x28, 0x38, 0x12, 0xBA, 0x05,
  28652. 0x6A, 0xC0, 0xC3, 0x45, 0xE3, 0x79, 0xC4, 0xBA,
  28653. 0x24, 0xD7, 0x50, 0x90, 0x12, 0x79, 0xBB, 0x2F,
  28654. 0x3A, 0x16, 0xF6, 0x12, 0xBF, 0xAD, 0xB3, 0x57,
  28655. 0x03, 0x33, 0x2C, 0x7C, 0x13, 0x6F, 0x68, 0xEA,
  28656. 0xB6, 0x75, 0x5C, 0x66, 0xB6, 0xA4, 0xAD, 0x1A,
  28657. 0xAB, 0xA7, 0xB7, 0x68, 0xA5, 0x8A, 0xCA, 0xAC,
  28658. 0xC1, 0x0A, 0x45, 0x9A, 0x1C, 0xC8, 0xEF, 0x29,
  28659. 0x37, 0x7B, 0xC2, 0x00, 0xE4, 0xD3, 0x15, 0xA3,
  28660. 0x0A, 0x6B, 0xCC, 0x32, 0x56, 0xF9, 0x73, 0x4D,
  28661. 0x06, 0xE9, 0x77, 0x9C, 0xAA, 0x54, 0x42, 0xA9,
  28662. 0xA1, 0x60, 0x69, 0x08, 0x13, 0x77, 0xC7, 0x6E,
  28663. 0x75, 0x15, 0x43, 0x68, 0x07, 0x2D, 0xC4, 0x46,
  28664. 0xED, 0x6C, 0x8B, 0x8E, 0x62, 0x2A, 0x21, 0xE3,
  28665. 0x83, 0xCF, 0x9B, 0xA1, 0xFB, 0x43, 0x4E, 0x2E,
  28666. 0xCC, 0x81, 0xE7, 0xB7, 0x8C, 0xEE, 0x98, 0x6B,
  28667. 0x8F, 0xF7, 0x98, 0xAB, 0x18, 0xCF, 0x96, 0x34,
  28668. 0x54, 0x35, 0x46, 0x28, 0x4E, 0xDA, 0x2A, 0x26,
  28669. 0xB4, 0x7F, 0x05, 0xB7, 0x35, 0xBC, 0xDB, 0x12,
  28670. 0x02, 0x22, 0x00, 0x76, 0xDC, 0x8B, 0x4E, 0x4B,
  28671. 0x9F, 0x85, 0x35, 0x33, 0xC8, 0xF6, 0xC7, 0xFF,
  28672. 0x38, 0x81, 0x7B, 0xA4, 0x97, 0x12, 0x83, 0x57,
  28673. 0x85, 0xF1, 0x7F, 0x14, 0xCA, 0x01, 0xD0, 0xC1,
  28674. 0xC1, 0xE9, 0x88, 0x10, 0xFE, 0x0B, 0x36, 0xE5,
  28675. 0xB4, 0x27, 0x15, 0x7B, 0x94, 0x18, 0x44, 0x9C,
  28676. 0xED, 0xD6, 0x41, 0xA4, 0x29, 0x3C, 0x85, 0xC3,
  28677. 0x27, 0x00, 0x10, 0x2A, 0xCE, 0xC2, 0x2E, 0xBA,
  28678. 0xD9, 0x8E, 0xD1, 0x60, 0xA5, 0xF0, 0x27, 0xBD,
  28679. 0x4C, 0xDA, 0x57, 0xF1, 0xF3, 0x72, 0x0A, 0x12,
  28680. 0xC1, 0x34, 0x65, 0x4D, 0xD5, 0xE7, 0x3F, 0x82,
  28681. 0x96, 0x76, 0x49, 0x53, 0x90, 0xD0, 0xE7, 0x92,
  28682. 0x9D, 0x60, 0x34, 0xE9, 0xC5, 0x5F, 0x7D, 0x55,
  28683. 0xBA, 0x65, 0x8B, 0xC5, 0x87, 0x98, 0x8E, 0x8A,
  28684. 0xF9, 0x49, 0x60, 0xF6, 0xCF, 0xB8, 0xD5, 0xAF,
  28685. 0x7A, 0x00, 0x21, 0x53, 0x5A, 0x6E, 0x25, 0xE4,
  28686. 0x37, 0xD4, 0x9A, 0x78, 0x06, 0x98, 0xBE, 0x22,
  28687. 0xAC, 0x99, 0x53, 0x94, 0x9F, 0x57, 0x1B, 0x85,
  28688. 0xA6, 0x85, 0x72, 0x5F, 0x82, 0x07, 0xA2, 0xB0,
  28689. 0xAE, 0x84, 0x9B, 0x60, 0x1A, 0xB9, 0x1B, 0x15,
  28690. 0x9B, 0x3D, 0xF4, 0xA1, 0x54, 0xC2, 0x04, 0x1E,
  28691. 0x77, 0x60, 0x70, 0xAF, 0xC4, 0x29, 0x69, 0x32,
  28692. 0x23, 0x80, 0x91, 0x7C, 0x97, 0x51, 0x07, 0x99,
  28693. 0xF3, 0x14, 0x91, 0x31, 0x47, 0x7E, 0x16, 0x66,
  28694. 0x3D, 0x31, 0x74, 0xC7, 0xC1, 0xCA, 0xEA, 0x78,
  28695. 0x85, 0x35, 0xC6, 0xC0, 0x05, 0xA6, 0x4F, 0x28,
  28696. 0x68, 0x63, 0x1B, 0x31, 0xB6, 0x6E, 0x20, 0x5F,
  28697. 0xD3, 0x8C, 0x1D, 0x84, 0x54, 0x2D, 0x0F, 0x1B,
  28698. 0x57, 0x8F, 0x58, 0xC9, 0xBF, 0x5A, 0x0F, 0xAE,
  28699. 0xAB, 0x6A, 0xB6, 0x49, 0x48, 0x93, 0x05, 0x31,
  28700. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  28701. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  28702. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  28703. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  28704. 0x8A, 0x39, 0xE8, 0x7D, 0x53, 0x1F, 0x35, 0x27,
  28705. 0xC2, 0x07, 0xED, 0xCC, 0x1D, 0xB7, 0xFA, 0xDD,
  28706. 0xCF, 0x96, 0x28, 0x39, 0x18, 0x79, 0xB3, 0x35,
  28707. 0xC7, 0x07, 0x83, 0x9A, 0x0D, 0xB0, 0x51, 0xA8,
  28708. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  28709. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  28710. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  28711. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  28712. };
  28713. const byte kyber1024_ct[] = {
  28714. 0xA6, 0xAF, 0x29, 0xD5, 0xF5, 0xB8, 0x0B, 0xD1,
  28715. 0x30, 0xF5, 0x18, 0xBA, 0xDD, 0xD6, 0xC8, 0xF1,
  28716. 0x75, 0x45, 0x41, 0x3D, 0x86, 0x0F, 0xB3, 0xDE,
  28717. 0x45, 0x19, 0x79, 0xEB, 0xFA, 0x5E, 0x4E, 0x31,
  28718. 0x12, 0xC7, 0xC0, 0xAD, 0xF9, 0x98, 0x24, 0xBB,
  28719. 0x52, 0x6F, 0x2C, 0x35, 0x50, 0x74, 0x8E, 0xD0,
  28720. 0xE1, 0x34, 0xF0, 0x45, 0x7A, 0x7C, 0x61, 0xF9,
  28721. 0xF5, 0x26, 0xF0, 0x02, 0xBA, 0xAD, 0xC0, 0x3F,
  28722. 0xC1, 0x3E, 0x38, 0x13, 0x12, 0x19, 0x51, 0x3C,
  28723. 0x3E, 0xDE, 0x06, 0x16, 0x61, 0xE7, 0x4F, 0x60,
  28724. 0x3C, 0x4F, 0xCF, 0x79, 0x51, 0xC8, 0xE5, 0x2C,
  28725. 0x9C, 0x21, 0x3B, 0x0D, 0x22, 0xD9, 0x29, 0x36,
  28726. 0x63, 0xD6, 0x69, 0xA6, 0xB5, 0x8E, 0xD8, 0xFC,
  28727. 0xEF, 0xCF, 0x82, 0x49, 0xD7, 0xBB, 0x52, 0x98,
  28728. 0xF5, 0x57, 0x61, 0x44, 0x5B, 0x2B, 0x83, 0xCE,
  28729. 0x7F, 0x00, 0x5C, 0xB0, 0x42, 0x48, 0xAE, 0xC8,
  28730. 0xBD, 0xA2, 0x2F, 0xD2, 0xD4, 0x2A, 0xA7, 0x66,
  28731. 0x32, 0x20, 0x14, 0xEA, 0x03, 0x8C, 0xC3, 0x2C,
  28732. 0x55, 0xC8, 0xE4, 0xB9, 0xE2, 0x8E, 0xC9, 0x11,
  28733. 0x9F, 0x52, 0x73, 0x41, 0xE4, 0xF6, 0x6A, 0x03,
  28734. 0x51, 0x21, 0x07, 0x3B, 0x85, 0xDE, 0x67, 0x06,
  28735. 0xDA, 0x19, 0xE0, 0x83, 0x8A, 0x9F, 0x33, 0xB7,
  28736. 0x19, 0xA6, 0x8F, 0x03, 0x9B, 0x66, 0x4D, 0xC0,
  28737. 0x02, 0x65, 0x9E, 0xAB, 0xFC, 0x39, 0x86, 0x79,
  28738. 0xAA, 0x70, 0x09, 0xCE, 0x0C, 0xD0, 0x1C, 0xDA,
  28739. 0xFB, 0x6C, 0xD2, 0xA2, 0x6F, 0xE4, 0x10, 0x16,
  28740. 0x72, 0xC9, 0x8F, 0xF5, 0x8F, 0x7C, 0x47, 0xD5,
  28741. 0xBD, 0xA2, 0x90, 0x66, 0x53, 0xB3, 0xA6, 0xF9,
  28742. 0x65, 0x1F, 0x7A, 0x12, 0x1E, 0xA7, 0x7E, 0xA7,
  28743. 0x47, 0x23, 0xFA, 0xE5, 0xB8, 0x73, 0xF9, 0xBB,
  28744. 0x7B, 0x66, 0x4F, 0x0C, 0x8A, 0x93, 0x83, 0x1E,
  28745. 0xF9, 0xD5, 0x1C, 0x7C, 0xC1, 0xEF, 0x44, 0xAC,
  28746. 0x0E, 0x55, 0xA5, 0x5C, 0xA7, 0x6D, 0x13, 0x7F,
  28747. 0xE9, 0xB7, 0x5F, 0x40, 0x50, 0x9C, 0xEF, 0x15,
  28748. 0x6E, 0x5A, 0xD1, 0x8F, 0x9F, 0xB9, 0x99, 0x68,
  28749. 0x00, 0x08, 0xE5, 0x47, 0xD5, 0x5E, 0xEC, 0xD5,
  28750. 0xB4, 0xD1, 0xCB, 0x1D, 0x9F, 0x07, 0x6C, 0xEC,
  28751. 0x21, 0x50, 0x1C, 0x74, 0x02, 0x50, 0x9E, 0xCB,
  28752. 0x77, 0xAF, 0xB2, 0xCB, 0x9A, 0x61, 0x34, 0x0A,
  28753. 0x8B, 0xD1, 0x51, 0x4C, 0x6E, 0x71, 0xB4, 0xAA,
  28754. 0x45, 0xE4, 0x7E, 0xC3, 0x75, 0x12, 0x27, 0x1B,
  28755. 0x91, 0x1F, 0x8F, 0xB4, 0x6C, 0x90, 0x82, 0xC9,
  28756. 0xDF, 0x07, 0x20, 0x4A, 0xBB, 0x5A, 0x50, 0xE6,
  28757. 0xE3, 0x64, 0x7A, 0x8A, 0xD4, 0xD8, 0xD5, 0xD7,
  28758. 0xBF, 0xF1, 0x9C, 0x8A, 0x50, 0x93, 0x08, 0xBC,
  28759. 0xFB, 0x89, 0x55, 0x36, 0xD0, 0x45, 0xCA, 0x2B,
  28760. 0x97, 0xCB, 0x16, 0xA2, 0x9B, 0xB7, 0x18, 0x1C,
  28761. 0xAD, 0x05, 0x09, 0xDD, 0xB9, 0x17, 0x35, 0x02,
  28762. 0x8E, 0xBA, 0x8C, 0x31, 0xD7, 0x4B, 0xD2, 0x75,
  28763. 0xEA, 0xA6, 0x5B, 0x53, 0x40, 0xB3, 0xA4, 0x3F,
  28764. 0xBF, 0xE0, 0xB3, 0x06, 0x1D, 0x6B, 0xAE, 0x7E,
  28765. 0x75, 0xB7, 0x09, 0x8C, 0xDA, 0xBE, 0x91, 0xD4,
  28766. 0xB3, 0x1E, 0x36, 0xC9, 0xAA, 0x7A, 0x82, 0x98,
  28767. 0x86, 0x2A, 0xD6, 0x3C, 0x8F, 0xD2, 0x82, 0xE0,
  28768. 0x3B, 0x46, 0x0B, 0x3A, 0xB4, 0x64, 0xCE, 0x0F,
  28769. 0x27, 0xB1, 0xC3, 0xD1, 0x11, 0x55, 0xAC, 0xAA,
  28770. 0x01, 0x1E, 0xB9, 0xE2, 0xAE, 0x3E, 0x6D, 0xDA,
  28771. 0x07, 0xD6, 0xF4, 0x91, 0x73, 0x7C, 0xBC, 0xE9,
  28772. 0xB0, 0x5F, 0x9B, 0xC5, 0x6B, 0xE2, 0x0E, 0x8D,
  28773. 0x32, 0x6B, 0xA1, 0x32, 0xC5, 0x7F, 0xB2, 0x35,
  28774. 0x16, 0x11, 0x44, 0x51, 0x9C, 0xDF, 0x40, 0x56,
  28775. 0x0F, 0xBE, 0x27, 0x9B, 0xDE, 0x41, 0x1E, 0x11,
  28776. 0x25, 0x31, 0xF8, 0x26, 0xD6, 0xAB, 0x10, 0xD4,
  28777. 0x54, 0x73, 0x50, 0xAD, 0xD2, 0xA9, 0xDE, 0x8D,
  28778. 0x62, 0xC2, 0xAC, 0x82, 0xCA, 0xBE, 0x68, 0x15,
  28779. 0x64, 0x6F, 0x4D, 0xC9, 0x74, 0x2B, 0xB0, 0xC2,
  28780. 0xA3, 0xF7, 0x7E, 0xC7, 0xB4, 0x6C, 0x6B, 0x53,
  28781. 0x76, 0x05, 0xFA, 0x31, 0x79, 0x8C, 0xD8, 0x92,
  28782. 0x81, 0x22, 0x1A, 0x33, 0xDF, 0xB9, 0x79, 0x6E,
  28783. 0x64, 0x43, 0x05, 0x63, 0x03, 0x32, 0xC2, 0xCB,
  28784. 0x93, 0x14, 0x08, 0xAB, 0x48, 0x1A, 0x16, 0xD9,
  28785. 0x53, 0xF6, 0xBE, 0xAE, 0x38, 0x91, 0xD6, 0xD9,
  28786. 0xAC, 0x1F, 0xAB, 0x38, 0x22, 0x2D, 0x92, 0x71,
  28787. 0x87, 0x2D, 0x9D, 0x0C, 0xAD, 0xB9, 0x1A, 0xBE,
  28788. 0x9B, 0x4E, 0x26, 0x5F, 0x75, 0xC6, 0xE5, 0xE8,
  28789. 0x29, 0xE1, 0x46, 0xC3, 0xD8, 0xCE, 0x1E, 0x9D,
  28790. 0x12, 0xE0, 0xD1, 0x29, 0x80, 0x19, 0x57, 0xF4,
  28791. 0x6B, 0x0D, 0x2D, 0xBE, 0x1F, 0x74, 0x9B, 0x1D,
  28792. 0x08, 0xE2, 0x34, 0x5F, 0x62, 0x39, 0xA7, 0x31,
  28793. 0x34, 0x2E, 0xB7, 0x5B, 0x0C, 0xF1, 0xBF, 0x41,
  28794. 0x17, 0x49, 0xBC, 0x2C, 0xAF, 0x28, 0x10, 0xB7,
  28795. 0x88, 0xC6, 0xB7, 0x23, 0x8B, 0x4D, 0x3D, 0xA2,
  28796. 0xD6, 0x31, 0x5C, 0xE9, 0x54, 0x2E, 0x24, 0x40,
  28797. 0x4F, 0x14, 0x57, 0x55, 0xA3, 0x0A, 0xB8, 0x51,
  28798. 0xE4, 0x44, 0x58, 0x41, 0xBD, 0x33, 0xF7, 0x16,
  28799. 0xA5, 0x86, 0x88, 0x48, 0x88, 0xEC, 0xC6, 0xBC,
  28800. 0x64, 0x98, 0xAA, 0x32, 0x91, 0x9A, 0xE8, 0x1D,
  28801. 0x20, 0xC2, 0x69, 0x73, 0xC2, 0xBD, 0x54, 0x58,
  28802. 0x2A, 0x0F, 0x6A, 0xD9, 0x8A, 0xBF, 0xD2, 0x62,
  28803. 0x7E, 0x15, 0x69, 0x0A, 0x72, 0x7E, 0x69, 0xF5,
  28804. 0x81, 0xDD, 0x2A, 0x71, 0x27, 0x98, 0x2A, 0x90,
  28805. 0xE3, 0x3E, 0x2D, 0x4A, 0x03, 0xFE, 0x33, 0x91,
  28806. 0x42, 0xC7, 0xE4, 0x4C, 0x32, 0x6A, 0xC4, 0x6E,
  28807. 0xD3, 0x95, 0xA2, 0x25, 0xD3, 0x03, 0x33, 0x89,
  28808. 0x91, 0x73, 0x28, 0xB4, 0x53, 0x16, 0xB1, 0x58,
  28809. 0x5A, 0x01, 0xB2, 0xC3, 0x04, 0xB2, 0x94, 0x4E,
  28810. 0x90, 0x3A, 0xBB, 0xB3, 0xEC, 0x56, 0x19, 0x44,
  28811. 0x1C, 0xFC, 0x89, 0x65, 0xA4, 0x46, 0xDF, 0x75,
  28812. 0xDE, 0xFA, 0x80, 0xC6, 0xE1, 0x5A, 0xDB, 0xD5,
  28813. 0x06, 0xB7, 0xAB, 0x2D, 0xE1, 0x2D, 0xDA, 0x9B,
  28814. 0xC8, 0x14, 0x41, 0xCF, 0xC8, 0x90, 0x52, 0xE2,
  28815. 0xE5, 0x80, 0x8F, 0x71, 0x26, 0xC6, 0xFD, 0x3A,
  28816. 0xC6, 0xAC, 0x80, 0x81, 0x25, 0x8A, 0x84, 0xA0,
  28817. 0x9A, 0xE5, 0x0F, 0x6C, 0xD7, 0xCC, 0x0F, 0x4A,
  28818. 0xF3, 0x36, 0xFD, 0x1D, 0x64, 0x3E, 0x99, 0x07,
  28819. 0x99, 0x96, 0x26, 0x8C, 0x2D, 0x32, 0xD9, 0x09,
  28820. 0xF2, 0x2E, 0x35, 0x04, 0xF0, 0x7F, 0xBB, 0x56,
  28821. 0x31, 0x96, 0xD4, 0x31, 0x2F, 0xDD, 0xB9, 0x33,
  28822. 0x5D, 0x5C, 0x1D, 0x36, 0xE8, 0xC5, 0xEE, 0xA2,
  28823. 0x27, 0x8D, 0xBA, 0x23, 0xB9, 0x4D, 0x19, 0x3C,
  28824. 0x94, 0x7C, 0xC4, 0x1C, 0xA9, 0x93, 0xDC, 0x7D,
  28825. 0xB1, 0x39, 0x63, 0x40, 0xAD, 0x9C, 0x4F, 0xE6,
  28826. 0x87, 0xDD, 0x7B, 0x8D, 0x0C, 0x7A, 0x51, 0x20,
  28827. 0xAE, 0x02, 0x04, 0xF2, 0xC6, 0x65, 0xBD, 0x5F,
  28828. 0x47, 0x3D, 0x64, 0x4C, 0x7F, 0xF2, 0x6B, 0xFF,
  28829. 0xBA, 0x7A, 0x36, 0x98, 0x08, 0x30, 0x70, 0x21,
  28830. 0x28, 0xA7, 0xE6, 0x61, 0xD6, 0x77, 0xA0, 0x92,
  28831. 0xA3, 0x6E, 0x74, 0x28, 0xA4, 0x13, 0x9F, 0xB2,
  28832. 0x9B, 0x00, 0x95, 0xCC, 0x11, 0x08, 0x6F, 0x44,
  28833. 0x7D, 0x2A, 0x9E, 0xF6, 0xC9, 0xB1, 0x61, 0xF1,
  28834. 0x89, 0xC6, 0x29, 0x9E, 0x08, 0x4C, 0xB7, 0xAA,
  28835. 0x00, 0xFA, 0xF7, 0x87, 0x79, 0x7B, 0xFB, 0x06,
  28836. 0x9F, 0xBC, 0x08, 0x7F, 0xDE, 0x26, 0x25, 0x2A,
  28837. 0x16, 0x64, 0xF1, 0x9C, 0x5A, 0x8A, 0x22, 0xEC,
  28838. 0x5E, 0xE1, 0xAE, 0xB0, 0x76, 0x35, 0x7B, 0x7D,
  28839. 0xC3, 0x7E, 0x6B, 0x0F, 0x15, 0x20, 0xF9, 0x58,
  28840. 0xF7, 0x85, 0x1B, 0xAC, 0xB9, 0x2C, 0x89, 0xFD,
  28841. 0x11, 0x4A, 0x72, 0xFE, 0xAC, 0x54, 0x65, 0x2D,
  28842. 0x45, 0xB0, 0x9E, 0x1A, 0xE7, 0x65, 0x1A, 0xBD,
  28843. 0x16, 0x4B, 0xCD, 0x53, 0x7D, 0x58, 0xFA, 0x39,
  28844. 0xD3, 0xEC, 0x8A, 0xCD, 0xCD, 0xF9, 0x84, 0x25,
  28845. 0x00, 0x58, 0x62, 0xFA, 0x59, 0x69, 0x2D, 0xE1,
  28846. 0x62, 0xB7, 0x7E, 0x62, 0x97, 0xC6, 0x62, 0x33,
  28847. 0x34, 0x84, 0x08, 0xA8, 0xAB, 0x69, 0x5C, 0xE2,
  28848. 0xF2, 0x72, 0x8D, 0xB9, 0xFB, 0xE2, 0x7E, 0x95,
  28849. 0x89, 0x67, 0xEC, 0x59, 0x74, 0x76, 0x7C, 0x5A,
  28850. 0x66, 0x02, 0x30, 0x74, 0xB4, 0xA7, 0x1A, 0xFD,
  28851. 0x26, 0x4A, 0xD2, 0x89, 0x0E, 0x97, 0x0A, 0x1F,
  28852. 0x31, 0xD6, 0xE3, 0x31, 0x1B, 0x73, 0x6F, 0x9F,
  28853. 0x94, 0x88, 0x79, 0x3D, 0xDC, 0x88, 0xF2, 0x34,
  28854. 0x58, 0x06, 0x42, 0x54, 0xC8, 0x2A, 0x1D, 0x9E,
  28855. 0x59, 0xEA, 0xD2, 0xFC, 0xEC, 0x40, 0xB4, 0x30,
  28856. 0x68, 0x7C, 0x4B, 0x7E, 0x28, 0x96, 0x09, 0x26,
  28857. 0xAF, 0xCA, 0xCC, 0x9B, 0xD7, 0x56, 0xA7, 0x10,
  28858. 0x88, 0xC7, 0x84, 0x50, 0xE2, 0x0A, 0x2E, 0x98,
  28859. 0x0A, 0xED, 0xE9, 0xEB, 0xED, 0xFE, 0x7F, 0xAB,
  28860. 0xD6, 0xAB, 0xFE, 0x96, 0xF9, 0x34, 0xC4, 0xB0,
  28861. 0x2C, 0x01, 0xCA, 0x19, 0x4D, 0x01, 0xB7, 0x3C,
  28862. 0x25, 0xD5, 0x99, 0x70, 0x39, 0xD3, 0xFC, 0xD0,
  28863. 0xF0, 0x99, 0x52, 0x1F, 0x70, 0xCA, 0xEE, 0x69,
  28864. 0x11, 0x0A, 0xC1, 0xFC, 0x5A, 0x99, 0x91, 0x7A,
  28865. 0xD7, 0x52, 0xFC, 0x96, 0xAD, 0xFA, 0xD7, 0x18,
  28866. 0x6D, 0x0A, 0x7C, 0x9C, 0xFE, 0x56, 0x01, 0xC0,
  28867. 0x75, 0x14, 0xEA, 0x64, 0x48, 0xD6, 0x61, 0xC5,
  28868. 0x7A, 0xA2, 0x02, 0x42, 0x10, 0x3C, 0x42, 0x76,
  28869. 0xA0, 0x70, 0xA4, 0x89, 0xA4, 0xCB, 0x6B, 0xCA,
  28870. 0x0F, 0x9E, 0xCC, 0x43, 0x79, 0xFB, 0x22, 0x02,
  28871. 0x15, 0xFD, 0x91, 0xF8, 0x10, 0x19, 0xD5, 0xB0,
  28872. 0xAE, 0x61, 0x93, 0x58, 0xB5, 0x24, 0x68, 0xF2,
  28873. 0x72, 0xC1, 0x78, 0xE3, 0xA7, 0x4C, 0xF6, 0x77,
  28874. 0x5A, 0xA9, 0x24, 0xFE, 0x32, 0x9C, 0x31, 0x75,
  28875. 0xD9, 0xE4, 0xC3, 0xE2, 0x1A, 0xB9, 0xEC, 0x83,
  28876. 0x6E, 0xDC, 0x3A, 0xCA, 0xB2, 0xE3, 0x89, 0x1E,
  28877. 0xE8, 0xDE, 0xDA, 0x51, 0x5D, 0x39, 0xAF, 0x9B,
  28878. 0x8D, 0xDD, 0x0E, 0xE7, 0xB0, 0x16, 0x4F, 0x80,
  28879. 0x5C, 0x38, 0x35, 0xF6, 0xD2, 0xBA, 0xBD, 0xB3,
  28880. 0x0E, 0xAB, 0x47, 0x56, 0xE7, 0xEC, 0x7F, 0x82,
  28881. 0x9E, 0xCE, 0x01, 0xE8, 0xEA, 0xDF, 0xBB, 0xED,
  28882. 0x12, 0xFC, 0x28, 0x3B, 0x3D, 0x4C, 0x69, 0xF5,
  28883. 0x75, 0xE7, 0xF8, 0x04, 0x17, 0x68, 0x9F, 0xDF,
  28884. 0xCF, 0xC7, 0xBE, 0x27, 0xEE, 0x3B, 0x8C, 0xDF,
  28885. 0x57, 0xAA, 0xEB, 0xEC, 0x4A, 0x95, 0xB7, 0xE5,
  28886. 0xBB, 0x58, 0x5B, 0x85, 0x22, 0x7F, 0x7C, 0x32,
  28887. 0xBE, 0x30, 0xDB, 0x3E, 0x65, 0xE4, 0x2E, 0x30,
  28888. 0xDC, 0xF5, 0xA5, 0xFA, 0x07, 0x3D, 0xBA, 0x39,
  28889. 0x9D, 0x94, 0x2F, 0x22, 0x22, 0xAD, 0xB9, 0xB9,
  28890. 0x89, 0x81, 0x02, 0xAF, 0xE5, 0x43, 0x2E, 0xDC,
  28891. 0x7F, 0x04, 0xAE, 0x34, 0xA8, 0xFE, 0xC2, 0xD8,
  28892. 0x1C, 0xB4, 0x9A, 0x9A, 0x9B, 0x43, 0x81, 0x4C,
  28893. 0xE7, 0x1D, 0x97, 0xF7, 0x26, 0xE2, 0xB1, 0xE8,
  28894. 0xF6, 0x4B, 0x50, 0xE6, 0x5D, 0xFB, 0x48, 0x16,
  28895. 0xE1, 0x2E, 0x82, 0xA3, 0x19, 0x74, 0x84, 0xA4,
  28896. 0xE9, 0xBB, 0xA4, 0xD2, 0xD6, 0x9E, 0x3F, 0x19,
  28897. 0xD0, 0xB7, 0x5C, 0x21, 0xE2, 0xBF, 0xFE, 0x9F,
  28898. 0xC0, 0xC9, 0x8C, 0xF4, 0x8A, 0x3A, 0xAF, 0x08,
  28899. 0xD4, 0x67, 0xF7, 0x26, 0x87, 0xDF, 0x01, 0x78,
  28900. 0x17, 0x4B, 0x78, 0x97, 0xF7, 0x34, 0x34, 0x9B,
  28901. 0x18, 0x1E, 0xCA, 0x86, 0xA5, 0x98, 0xA0, 0xC5,
  28902. 0xE8, 0xC2, 0x59, 0x46, 0xF2, 0x4D, 0xC5, 0x57,
  28903. 0x2B, 0xD3, 0x24, 0xA4, 0x04, 0x58, 0xA7, 0x88,
  28904. 0xE5, 0x13, 0x7F, 0x3C, 0x7A, 0x7C, 0x97, 0xFC,
  28905. 0x9F, 0x12, 0xA3, 0xC4, 0x63, 0xA8, 0xFE, 0x94,
  28906. 0x49, 0x10, 0x1C, 0xCE, 0x96, 0x6D, 0x7C, 0x00,
  28907. 0x93, 0x23, 0x93, 0x29, 0x98, 0xD5, 0x6E, 0xF4,
  28908. 0x30, 0xC7, 0x3B, 0xC2, 0x4F, 0x5D, 0x95, 0xF7,
  28909. 0x37, 0x85, 0x8D, 0xDC, 0x4F, 0x32, 0xC0, 0x13
  28910. };
  28911. const byte kyber1024_ss[] = {
  28912. 0xB1, 0x0F, 0x73, 0x94, 0x92, 0x6A, 0xD3, 0xB4,
  28913. 0x9C, 0x5D, 0x62, 0xD5, 0xAE, 0xB5, 0x31, 0xD5,
  28914. 0x75, 0x75, 0x38, 0xBC, 0xC0, 0xDA, 0x9E, 0x55,
  28915. 0x0D, 0x43, 0x8F, 0x1B, 0x61, 0xBD, 0x74, 0x19
  28916. };
  28917. ret = wc_KyberKey_Init(KYBER1024, &key, HEAP_HINT, INVALID_DEVID);
  28918. if (ret != 0)
  28919. return -20301;
  28920. ret = wc_KyberKey_MakeKeyWithRandom(&key, kyber1024_rand,
  28921. sizeof(kyber1024_rand));
  28922. if (ret != 0)
  28923. return -20302;
  28924. ret = wc_KyberKey_EncodePublicKey(&key, pub, sizeof(pub));
  28925. if (ret != 0)
  28926. return -20303;
  28927. ret = wc_KyberKey_EncodePrivateKey(&key, priv, sizeof(priv));
  28928. if (ret != 0)
  28929. return -20304;
  28930. if (XMEMCMP(pub, kyber1024_pk, sizeof(kyber1024_pk)) != 0)
  28931. return -20305;
  28932. if (XMEMCMP(priv, kyber1024_sk, sizeof(kyber1024_sk)) != 0)
  28933. return -20306;
  28934. ret = wc_KyberKey_EncapsulateWithRandom(&key, ct, ss, kyber1024enc_rand,
  28935. sizeof(kyber1024enc_rand));
  28936. if (ret != 0)
  28937. return -20307;
  28938. if (XMEMCMP(ct, kyber1024_ct, sizeof(kyber1024_ct)) != 0)
  28939. return -20308;
  28940. if (XMEMCMP(ss, kyber1024_ss, sizeof(kyber1024_ss)) != 0)
  28941. return -20309;
  28942. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, sizeof(kyber1024_ct));
  28943. if (ret != 0)
  28944. return -20310;
  28945. if (XMEMCMP(ss_dec, kyber1024_ss, sizeof(kyber1024_ss)) != 0)
  28946. return -20311;
  28947. wc_KyberKey_Free(&key);
  28948. return 0;
  28949. }
  28950. #endif /* WOLFSSL_KYBER1024 */
  28951. #endif /* WOLFSSL_WC_KYBER */
  28952. WOLFSSL_TEST_SUBROUTINE int kyber_test(void)
  28953. {
  28954. int ret;
  28955. KyberKey key;
  28956. WC_RNG rng;
  28957. int i;
  28958. byte priv[KYBER_MAX_PRIVATE_KEY_SIZE];
  28959. byte pub[KYBER_MAX_PUBLIC_KEY_SIZE];
  28960. byte priv2[KYBER_MAX_PRIVATE_KEY_SIZE];
  28961. byte pub2[KYBER_MAX_PUBLIC_KEY_SIZE];
  28962. byte ct[KYBER_MAX_CIPHER_TEXT_SIZE];
  28963. byte ss[KYBER_SS_SZ];
  28964. byte ss_dec[KYBER_SS_SZ];
  28965. int testData[][4] = {
  28966. #ifdef WOLFSSL_KYBER512
  28967. { KYBER512, KYBER512_PRIVATE_KEY_SIZE, KYBER512_PUBLIC_KEY_SIZE,
  28968. KYBER512_CIPHER_TEXT_SIZE },
  28969. #endif
  28970. #ifdef WOLFSSL_KYBER768
  28971. { KYBER768, KYBER768_PRIVATE_KEY_SIZE, KYBER768_PUBLIC_KEY_SIZE,
  28972. KYBER768_CIPHER_TEXT_SIZE },
  28973. #endif
  28974. #ifdef WOLFSSL_KYBER1024
  28975. { KYBER1024, KYBER1024_PRIVATE_KEY_SIZE, KYBER1024_PUBLIC_KEY_SIZE,
  28976. KYBER1024_CIPHER_TEXT_SIZE },
  28977. #endif
  28978. };
  28979. #ifndef HAVE_FIPS
  28980. ret = wc_InitRng_ex(&rng, HEAP_HINT, INVALID_DEVID);
  28981. #else
  28982. ret = wc_InitRng(&rng);
  28983. #endif
  28984. if (ret != 0)
  28985. return -20000;
  28986. for (i = 0; i < (int)(sizeof(testData) / sizeof(*testData)); i++) {
  28987. ret = wc_KyberKey_Init(testData[i][0], &key, HEAP_HINT, INVALID_DEVID);
  28988. if (ret != 0)
  28989. return -20001 - i * 20;
  28990. ret = wc_KyberKey_MakeKey(&key, &rng);
  28991. if (ret != 0)
  28992. return -20002 - i * 20;
  28993. ret = wc_KyberKey_EncodePublicKey(&key, pub, testData[i][2]);
  28994. if (ret != 0)
  28995. return -20003 - i * 20;
  28996. ret = wc_KyberKey_EncodePrivateKey(&key, priv, testData[i][1]);
  28997. if (ret != 0)
  28998. return -20004 - i * 20;
  28999. ret = wc_KyberKey_Init(testData[i][0], &key, HEAP_HINT, INVALID_DEVID);
  29000. if (ret != 0)
  29001. return -20005 - i * 20;
  29002. ret = wc_KyberKey_DecodePublicKey(&key, pub, testData[i][2]);
  29003. if (ret != 0)
  29004. return -20006 - i * 20;
  29005. ret = wc_KyberKey_Encapsulate(&key, ct, ss, &rng);
  29006. if (ret != 0)
  29007. return -20007 - i * 20;
  29008. ret = wc_KyberKey_EncodePublicKey(&key, pub2, testData[i][2]);
  29009. if (ret != 0)
  29010. return -20008 - i * 20;
  29011. if (XMEMCMP(pub, pub2, testData[i][2]) != 0)
  29012. return -20009 - i * 20;
  29013. ret = wc_KyberKey_Init(testData[i][0], &key, HEAP_HINT, INVALID_DEVID);
  29014. if (ret != 0)
  29015. return -20010 - i * 20;
  29016. ret = wc_KyberKey_DecodePrivateKey(&key, priv, testData[i][1]);
  29017. if (ret != 0)
  29018. return -20011 - i * 20;
  29019. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, testData[i][3]);
  29020. if (ret != 0)
  29021. return -20012 - i * 20;
  29022. if (XMEMCMP(ss, ss_dec, sizeof(ss)) != 0)
  29023. return -20013 - i * 20;
  29024. ret = wc_KyberKey_EncodePrivateKey(&key, priv2, testData[i][1]);
  29025. if (ret != 0)
  29026. return -20014 - i * 20;
  29027. if (XMEMCMP(priv, priv2, testData[i][2]) != 0)
  29028. return -20015 - i * 20;
  29029. wc_KyberKey_Free(&key);
  29030. }
  29031. wc_FreeRng(&rng);
  29032. #ifdef WOLFSSL_WC_KYBER
  29033. #ifdef WOLFSSL_KYBER512
  29034. ret = kyber512_kat();
  29035. if (ret != 0)
  29036. return ret;
  29037. #endif
  29038. #ifdef WOLFSSL_KYBER768
  29039. ret = kyber768_kat();
  29040. if (ret != 0)
  29041. return ret;
  29042. #endif
  29043. #ifdef WOLFSSL_KYBER1024
  29044. ret = kyber1024_kat();
  29045. if (ret != 0)
  29046. return ret;
  29047. #endif
  29048. #endif /* WOLFSSL_WC_KYBER */
  29049. return 0;
  29050. }
  29051. #endif /* WOLFSSL_HAVE_KYBER */
  29052. #ifdef WOLFCRYPT_HAVE_ECCSI
  29053. static int eccsi_api_test(WC_RNG* rng, EccsiKey* key, mp_int* ssk,
  29054. ecc_point* pvt)
  29055. {
  29056. int ret;
  29057. byte id[1] = { 0x00 };
  29058. int valid;
  29059. word32 sz;
  29060. byte data[256];
  29061. byte hash[WC_MAX_DIGEST_SIZE];
  29062. byte hashSz;
  29063. byte sig[257];
  29064. word32 sigSz;
  29065. ret = wc_InitEccsiKey_ex(NULL, 32, ECC_SECP256R1, HEAP_HINT, INVALID_DEVID);
  29066. if (ret != BAD_FUNC_ARG)
  29067. return -10023;
  29068. ret = wc_InitEccsiKey_ex(NULL, 32, ECC_SECP256R1, HEAP_HINT, INVALID_DEVID);
  29069. if (ret != BAD_FUNC_ARG)
  29070. return -10024;
  29071. ret = wc_InitEccsiKey(NULL, NULL, INVALID_DEVID);
  29072. if (ret != BAD_FUNC_ARG)
  29073. return -10025;
  29074. ret = wc_InitEccsiKey(NULL, HEAP_HINT, INVALID_DEVID);
  29075. if (ret != BAD_FUNC_ARG)
  29076. return -10026;
  29077. wc_FreeEccsiKey(NULL);
  29078. /* Create a valid key. */
  29079. ret = wc_InitEccsiKey(key, NULL, INVALID_DEVID);
  29080. if (ret != 0)
  29081. return -10027;
  29082. ret = wc_MakeEccsiKey(NULL, NULL);
  29083. if (ret != BAD_FUNC_ARG)
  29084. return -10028;
  29085. ret = wc_MakeEccsiKey(key, NULL);
  29086. if (ret != BAD_FUNC_ARG)
  29087. return -10029;
  29088. ret = wc_MakeEccsiKey(NULL, rng);
  29089. if (ret != BAD_FUNC_ARG)
  29090. return -10030;
  29091. ret = wc_MakeEccsiPair(NULL, NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL,
  29092. NULL);
  29093. if (ret != BAD_FUNC_ARG)
  29094. return -10031;
  29095. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, NULL);
  29096. if (ret != BAD_FUNC_ARG)
  29097. return -10032;
  29098. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, NULL, pvt);
  29099. if (ret != BAD_FUNC_ARG)
  29100. return -10033;
  29101. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, NULL, 1, ssk, pvt);
  29102. if (ret != BAD_FUNC_ARG)
  29103. return -10034;
  29104. ret = wc_MakeEccsiPair(key, NULL, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  29105. if (ret != BAD_FUNC_ARG)
  29106. return -10035;
  29107. ret = wc_MakeEccsiPair(NULL, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  29108. if (ret != BAD_FUNC_ARG)
  29109. return -10036;
  29110. /* No key set */
  29111. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  29112. if (ret != BAD_STATE_E)
  29113. return -10037;
  29114. ret = wc_ValidateEccsiPair(NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL, NULL,
  29115. NULL);
  29116. if (ret != BAD_FUNC_ARG)
  29117. return -10038;
  29118. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  29119. NULL);
  29120. if (ret != BAD_FUNC_ARG)
  29121. return -10039;
  29122. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, NULL,
  29123. &valid);
  29124. if (ret != BAD_FUNC_ARG)
  29125. return -10040;
  29126. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, NULL, pvt,
  29127. &valid);
  29128. if (ret != BAD_FUNC_ARG)
  29129. return -10041;
  29130. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, NULL, 1, ssk, pvt,
  29131. &valid);
  29132. if (ret != BAD_FUNC_ARG)
  29133. return -10042;
  29134. ret = wc_ValidateEccsiPair(NULL, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  29135. &valid);
  29136. if (ret != BAD_FUNC_ARG)
  29137. return -10043;
  29138. /* No key set */
  29139. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  29140. &valid);
  29141. if (ret != BAD_STATE_E)
  29142. return -10044;
  29143. ret = wc_ValidateEccsiPvt(NULL, NULL, NULL);
  29144. if (ret != BAD_FUNC_ARG)
  29145. return -10045;
  29146. ret = wc_ValidateEccsiPvt(key, NULL, NULL);
  29147. if (ret != BAD_FUNC_ARG)
  29148. return -10046;
  29149. ret = wc_ValidateEccsiPvt(NULL, pvt, NULL);
  29150. if (ret != BAD_FUNC_ARG)
  29151. return -10047;
  29152. ret = wc_ValidateEccsiPvt(NULL, NULL, &valid);
  29153. if (ret != BAD_FUNC_ARG)
  29154. return -10048;
  29155. ret = wc_ValidateEccsiPvt(key, pvt, NULL);
  29156. if (ret != BAD_FUNC_ARG)
  29157. return -10049;
  29158. ret = wc_ValidateEccsiPvt(key, NULL, &valid);
  29159. if (ret != BAD_FUNC_ARG)
  29160. return -10050;
  29161. ret = wc_ValidateEccsiPvt(NULL, pvt, &valid);
  29162. if (ret != BAD_FUNC_ARG)
  29163. return -10051;
  29164. ret = wc_EncodeEccsiPair(NULL, NULL, NULL, data, NULL);
  29165. if (ret != BAD_FUNC_ARG)
  29166. return -10052;
  29167. ret = wc_EncodeEccsiPair(key, ssk, pvt, data, NULL);
  29168. if (ret != BAD_FUNC_ARG)
  29169. return -10053;
  29170. ret = wc_EncodeEccsiPair(key, ssk, NULL, data, &sz);
  29171. if (ret != BAD_FUNC_ARG)
  29172. return -10054;
  29173. ret = wc_EncodeEccsiPair(key, NULL, pvt, data, &sz);
  29174. if (ret != BAD_FUNC_ARG)
  29175. return -10055;
  29176. ret = wc_EncodeEccsiPair(NULL, ssk, pvt, data, &sz);
  29177. if (ret != BAD_FUNC_ARG)
  29178. return -10056;
  29179. /* No key created so no curve information. */
  29180. ret = wc_EncodeEccsiPair(key, ssk, pvt, NULL, &sz);
  29181. if (ret != LENGTH_ONLY_E)
  29182. return -10057;
  29183. ret = wc_EncodeEccsiSsk(NULL, NULL, data, NULL);
  29184. if (ret != BAD_FUNC_ARG)
  29185. return -10058;
  29186. ret = wc_EncodeEccsiSsk(key, ssk, data, NULL);
  29187. if (ret != BAD_FUNC_ARG)
  29188. return -10059;
  29189. ret = wc_EncodeEccsiSsk(key, NULL, data, &sz);
  29190. if (ret != BAD_FUNC_ARG)
  29191. return -10060;
  29192. ret = wc_EncodeEccsiSsk(NULL, ssk, data, &sz);
  29193. if (ret != BAD_FUNC_ARG)
  29194. return -10061;
  29195. ret = wc_EncodeEccsiPvt(NULL, NULL, data, NULL, 1);
  29196. if (ret != BAD_FUNC_ARG)
  29197. return -10058;
  29198. ret = wc_EncodeEccsiPvt(key, pvt, data, NULL, 1);
  29199. if (ret != BAD_FUNC_ARG)
  29200. return -10059;
  29201. ret = wc_EncodeEccsiPvt(key, NULL, data, &sz, 1);
  29202. if (ret != BAD_FUNC_ARG)
  29203. return -10060;
  29204. ret = wc_EncodeEccsiPvt(NULL, pvt, data, &sz, 1);
  29205. if (ret != BAD_FUNC_ARG)
  29206. return -10061;
  29207. ret = wc_DecodeEccsiPair(NULL, NULL, 0, NULL, NULL);
  29208. if (ret != BAD_FUNC_ARG)
  29209. return -10062;
  29210. ret = wc_DecodeEccsiPair(key, data, 0, ssk, NULL);
  29211. if (ret != BAD_FUNC_ARG)
  29212. return -10063;
  29213. ret = wc_DecodeEccsiPair(key, data, 0, NULL, pvt);
  29214. if (ret != BAD_FUNC_ARG)
  29215. return -10064;
  29216. ret = wc_DecodeEccsiPair(key, NULL, 0, ssk, pvt);
  29217. if (ret != BAD_FUNC_ARG)
  29218. return -10065;
  29219. ret = wc_DecodeEccsiPair(NULL, data, 0, ssk, pvt);
  29220. if (ret != BAD_FUNC_ARG)
  29221. return -10066;
  29222. ret = wc_DecodeEccsiSsk(NULL, NULL, 0, NULL);
  29223. if (ret != BAD_FUNC_ARG)
  29224. return -10067;
  29225. ret = wc_DecodeEccsiSsk(key, data, 0, NULL);
  29226. if (ret != BAD_FUNC_ARG)
  29227. return -10068;
  29228. ret = wc_DecodeEccsiSsk(key, NULL, 0, ssk);
  29229. if (ret != BAD_FUNC_ARG)
  29230. return -10069;
  29231. ret = wc_DecodeEccsiSsk(NULL, data, 0, ssk);
  29232. if (ret != BAD_FUNC_ARG)
  29233. return -10070;
  29234. ret = wc_DecodeEccsiPvt(NULL, NULL, 0, NULL);
  29235. if (ret != BAD_FUNC_ARG)
  29236. return -10067;
  29237. ret = wc_DecodeEccsiPvt(key, data, 0, NULL);
  29238. if (ret != BAD_FUNC_ARG)
  29239. return -10068;
  29240. ret = wc_DecodeEccsiPvt(key, NULL, 0, pvt);
  29241. if (ret != BAD_FUNC_ARG)
  29242. return -10069;
  29243. ret = wc_DecodeEccsiPvt(NULL, data, 0, pvt);
  29244. if (ret != BAD_FUNC_ARG)
  29245. return -10070;
  29246. ret = wc_DecodeEccsiPvtFromSig(NULL, NULL, 0, NULL);
  29247. if (ret != BAD_FUNC_ARG)
  29248. return -10067;
  29249. ret = wc_DecodeEccsiPvtFromSig(key, data, 0, NULL);
  29250. if (ret != BAD_FUNC_ARG)
  29251. return -10068;
  29252. ret = wc_DecodeEccsiPvtFromSig(key, NULL, 0, pvt);
  29253. if (ret != BAD_FUNC_ARG)
  29254. return -10069;
  29255. ret = wc_DecodeEccsiPvtFromSig(NULL, data, 0, pvt);
  29256. if (ret != BAD_FUNC_ARG)
  29257. return -10070;
  29258. ret = wc_ExportEccsiKey(NULL, data, NULL);
  29259. if (ret != BAD_FUNC_ARG)
  29260. return -10071;
  29261. ret = wc_ExportEccsiKey(key, data, NULL);
  29262. if (ret != BAD_FUNC_ARG)
  29263. return -10072;
  29264. ret = wc_ExportEccsiKey(NULL, data, &sz);
  29265. if (ret != BAD_FUNC_ARG)
  29266. return -10073;
  29267. /* No key to export */
  29268. ret = wc_ExportEccsiKey(key, NULL, &sz);
  29269. if (ret != BAD_STATE_E)
  29270. return -10074;
  29271. ret = wc_ImportEccsiKey(NULL, NULL, 0);
  29272. if (ret != BAD_FUNC_ARG)
  29273. return -10075;
  29274. ret = wc_ImportEccsiKey(key, NULL, 0);
  29275. if (ret != BAD_FUNC_ARG)
  29276. return -10076;
  29277. ret = wc_ImportEccsiKey(NULL, data, 0);
  29278. if (ret != BAD_FUNC_ARG)
  29279. return -10077;
  29280. ret = wc_ExportEccsiPrivateKey(NULL, data, NULL);
  29281. if (ret != BAD_FUNC_ARG)
  29282. return -10071;
  29283. ret = wc_ExportEccsiPrivateKey(key, data, NULL);
  29284. if (ret != BAD_FUNC_ARG)
  29285. return -10072;
  29286. ret = wc_ExportEccsiPrivateKey(NULL, data, &sz);
  29287. if (ret != BAD_FUNC_ARG)
  29288. return -10073;
  29289. /* No key to export */
  29290. ret = wc_ExportEccsiPrivateKey(key, NULL, &sz);
  29291. if (ret != BAD_STATE_E)
  29292. return -10074;
  29293. ret = wc_ImportEccsiPrivateKey(NULL, NULL, 0);
  29294. if (ret != BAD_FUNC_ARG)
  29295. return -10075;
  29296. ret = wc_ImportEccsiPrivateKey(key, NULL, 0);
  29297. if (ret != BAD_FUNC_ARG)
  29298. return -10076;
  29299. ret = wc_ImportEccsiPrivateKey(NULL, data, 0);
  29300. if (ret != BAD_FUNC_ARG)
  29301. return -10077;
  29302. ret = wc_ExportEccsiPublicKey(NULL, data, NULL, 1);
  29303. if (ret != BAD_FUNC_ARG)
  29304. return -10078;
  29305. ret = wc_ExportEccsiPublicKey(key, data, NULL, 1);
  29306. if (ret != BAD_FUNC_ARG)
  29307. return -10079;
  29308. ret = wc_ExportEccsiPublicKey(NULL, data, &sz, 1);
  29309. if (ret != BAD_FUNC_ARG)
  29310. return -10080;
  29311. /* No key to export */
  29312. ret = wc_ExportEccsiPublicKey(key, data, &sz, 1);
  29313. if (ret != BAD_STATE_E)
  29314. return -10081;
  29315. ret = wc_ImportEccsiPublicKey(NULL, NULL, 0, 1);
  29316. if (ret != BAD_FUNC_ARG)
  29317. return -10082;
  29318. ret = wc_ImportEccsiPublicKey(key, NULL, 0, 1);
  29319. if (ret != BAD_FUNC_ARG)
  29320. return -10083;
  29321. ret = wc_ImportEccsiPublicKey(NULL, data, 0, 1);
  29322. if (ret != BAD_FUNC_ARG)
  29323. return -10084;
  29324. ret = wc_HashEccsiId(NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL, NULL, NULL);
  29325. if (ret != BAD_FUNC_ARG)
  29326. return -10085;
  29327. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, NULL);
  29328. if (ret != BAD_FUNC_ARG)
  29329. return -10086;
  29330. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, NULL, &hashSz);
  29331. if (ret != BAD_FUNC_ARG)
  29332. return -10087;
  29333. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, NULL, hash, &hashSz);
  29334. if (ret != BAD_FUNC_ARG)
  29335. return -10088;
  29336. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, NULL, 1, pvt, hash,
  29337. &hashSz);
  29338. if (ret != BAD_FUNC_ARG)
  29339. return -10089;
  29340. ret = wc_HashEccsiId(NULL, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, &hashSz);
  29341. if (ret != BAD_FUNC_ARG)
  29342. return -10090;
  29343. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, &hashSz);
  29344. if (ret != BAD_STATE_E)
  29345. return -10091;
  29346. ret = wc_SetEccsiHash(NULL, NULL, 1);
  29347. if (ret != BAD_FUNC_ARG)
  29348. return -10090;
  29349. ret = wc_SetEccsiHash(key, NULL, 1);
  29350. if (ret != BAD_FUNC_ARG)
  29351. return -10090;
  29352. ret = wc_SetEccsiHash(NULL, hash, 1);
  29353. if (ret != BAD_FUNC_ARG)
  29354. return -10090;
  29355. ret = wc_SetEccsiPair(NULL, NULL, NULL);
  29356. if (ret != BAD_FUNC_ARG)
  29357. return -10090;
  29358. ret = wc_SetEccsiPair(key, NULL, NULL);
  29359. if (ret != BAD_FUNC_ARG)
  29360. return -10090;
  29361. ret = wc_SetEccsiPair(NULL, ssk, NULL);
  29362. if (ret != BAD_FUNC_ARG)
  29363. return -10090;
  29364. ret = wc_SetEccsiPair(NULL, NULL, pvt);
  29365. if (ret != BAD_FUNC_ARG)
  29366. return -10090;
  29367. ret = wc_SetEccsiPair(key, ssk, NULL);
  29368. if (ret != BAD_FUNC_ARG)
  29369. return -10090;
  29370. ret = wc_SetEccsiPair(key, NULL, pvt);
  29371. if (ret != BAD_FUNC_ARG)
  29372. return -10090;
  29373. ret = wc_SetEccsiPair(NULL, ssk, pvt);
  29374. if (ret != BAD_FUNC_ARG)
  29375. return -10090;
  29376. ret = wc_SignEccsiHash(NULL, NULL, WC_HASH_TYPE_SHA256, NULL, 0, sig, NULL);
  29377. if (ret != BAD_FUNC_ARG)
  29378. return -10092;
  29379. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, sig, NULL);
  29380. if (ret != BAD_FUNC_ARG)
  29381. return -10093;
  29382. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, NULL, 0, sig,
  29383. &sigSz);
  29384. if (ret != BAD_FUNC_ARG)
  29385. return -10096;
  29386. ret = wc_SignEccsiHash(key, NULL, WC_HASH_TYPE_SHA256, data, 0, sig,
  29387. &sigSz);
  29388. if (ret != BAD_FUNC_ARG)
  29389. return -10098;
  29390. ret = wc_SignEccsiHash(NULL, rng, WC_HASH_TYPE_SHA256, data, 0, sig,
  29391. &sigSz);
  29392. if (ret != BAD_FUNC_ARG)
  29393. return -10099;
  29394. /* Key not set. */
  29395. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, NULL,
  29396. &sigSz);
  29397. if (ret != BAD_STATE_E)
  29398. return -10100;
  29399. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0, NULL);
  29400. if (ret != BAD_FUNC_ARG)
  29401. return -10101;
  29402. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0, NULL);
  29403. if (ret != BAD_FUNC_ARG)
  29404. return -10101;
  29405. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, data, 0, NULL, 0, NULL);
  29406. if (ret != BAD_FUNC_ARG)
  29407. return -10101;
  29408. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, sig, 0, NULL);
  29409. if (ret != BAD_FUNC_ARG)
  29410. return -10101;
  29411. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0,
  29412. &valid);
  29413. if (ret != BAD_FUNC_ARG)
  29414. return -10101;
  29415. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, sig, 0, NULL);
  29416. if (ret != BAD_FUNC_ARG)
  29417. return -10102;
  29418. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, NULL, 0,
  29419. &valid);
  29420. if (ret != BAD_FUNC_ARG)
  29421. return -10103;
  29422. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, NULL, 0, sig, 0,
  29423. &valid);
  29424. if (ret != BAD_FUNC_ARG)
  29425. return -10104;
  29426. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, data, 0, sig, 0,
  29427. &valid);
  29428. if (ret != BAD_FUNC_ARG)
  29429. return -10106;
  29430. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, sig, 0,
  29431. &valid);
  29432. if (ret != BAD_STATE_E)
  29433. return -10106;
  29434. ret = wc_SetEccsiPair(key, ssk, pvt);
  29435. if (ret != 0)
  29436. return -10107;
  29437. /* Identity hash not set. */
  29438. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, NULL,
  29439. &sigSz);
  29440. if (ret != BAD_STATE_E)
  29441. return -10108;
  29442. wc_FreeEccsiKey(key);
  29443. return 0;
  29444. }
  29445. /* RFC 6507: Appendix A */
  29446. static int eccsi_kat_verify_test(EccsiKey* key, ecc_point* pvt)
  29447. {
  29448. int ret;
  29449. int verified;
  29450. const byte msg[] = { 0x6D, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x00 };
  29451. word32 msgSz = sizeof(msg);
  29452. byte hash[WC_SHA256_DIGEST_SIZE];
  29453. byte hashSz = WC_SHA256_DIGEST_SIZE;
  29454. static const byte id[] = {
  29455. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  29456. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  29457. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  29458. 0x33, 0x00
  29459. };
  29460. word32 idSz = sizeof(id);
  29461. static const byte sig[] = {
  29462. 0x26, 0x9D, 0x4C, 0x8F, 0xDE, 0xB6, 0x6A, 0x74,
  29463. 0xE4, 0xEF, 0x8C, 0x0D, 0x5D, 0xCC, 0x59, 0x7D,
  29464. 0xDF, 0xE6, 0x02, 0x9C, 0x2A, 0xFF, 0xC4, 0x93,
  29465. 0x60, 0x08, 0xCD, 0x2C, 0xC1, 0x04, 0x5D, 0x81,
  29466. 0xE0, 0x9B, 0x52, 0x8D, 0x0E, 0xF8, 0xD6, 0xDF,
  29467. 0x1A, 0xA3, 0xEC, 0xBF, 0x80, 0x11, 0x0C, 0xFC,
  29468. 0xEC, 0x9F, 0xC6, 0x82, 0x52, 0xCE, 0xBB, 0x67,
  29469. 0x9F, 0x41, 0x34, 0x84, 0x69, 0x40, 0xCC, 0xFD,
  29470. 0x04,
  29471. 0x75, 0x8A, 0x14, 0x27, 0x79, 0xBE, 0x89, 0xE8,
  29472. 0x29, 0xE7, 0x19, 0x84, 0xCB, 0x40, 0xEF, 0x75,
  29473. 0x8C, 0xC4, 0xAD, 0x77, 0x5F, 0xC5, 0xB9, 0xA3,
  29474. 0xE1, 0xC8, 0xED, 0x52, 0xF6, 0xFA, 0x36, 0xD9,
  29475. 0xA7, 0x9D, 0x24, 0x76, 0x92, 0xF4, 0xED, 0xA3,
  29476. 0xA6, 0xBD, 0xAB, 0x77, 0xD6, 0xAA, 0x64, 0x74,
  29477. 0xA4, 0x64, 0xAE, 0x49, 0x34, 0x66, 0x3C, 0x52,
  29478. 0x65, 0xBA, 0x70, 0x18, 0xBA, 0x09, 0x1F, 0x79
  29479. };
  29480. word32 sigSz = sizeof(sig);
  29481. static const byte pubData[] = {
  29482. 0x50, 0xD4, 0x67, 0x0B, 0xDE, 0x75, 0x24, 0x4F,
  29483. 0x28, 0xD2, 0x83, 0x8A, 0x0D, 0x25, 0x55, 0x8A,
  29484. 0x7A, 0x72, 0x68, 0x6D, 0x45, 0x22, 0xD4, 0xC8,
  29485. 0x27, 0x3F, 0xB6, 0x44, 0x2A, 0xEB, 0xFA, 0x93,
  29486. 0xDB, 0xDD, 0x37, 0x55, 0x1A, 0xFD, 0x26, 0x3B,
  29487. 0x5D, 0xFD, 0x61, 0x7F, 0x39, 0x60, 0xC6, 0x5A,
  29488. 0x8C, 0x29, 0x88, 0x50, 0xFF, 0x99, 0xF2, 0x03,
  29489. 0x66, 0xDC, 0xE7, 0xD4, 0x36, 0x72, 0x17, 0xF4
  29490. };
  29491. static const byte expHash[] = {
  29492. 0x49, 0x0f, 0x3f, 0xeb, 0xbc, 0x1c, 0x90, 0x2f,
  29493. 0x62, 0x89, 0x72, 0x3d, 0x7f, 0x8c, 0xbf, 0x79,
  29494. 0xdb, 0x88, 0x93, 0x08, 0x49, 0xd1, 0x9f, 0x38,
  29495. 0xf0, 0x29, 0x5b, 0x5c, 0x27, 0x6c, 0x14, 0xd1
  29496. };
  29497. ret = wc_ImportEccsiPublicKey(key, pubData, sizeof(pubData), 0);
  29498. if (ret != 0)
  29499. return -10108;
  29500. ret = wc_DecodeEccsiPvtFromSig(key, sig, sigSz, pvt);
  29501. if (ret != 0)
  29502. return -10109;
  29503. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, idSz, pvt, hash,
  29504. &hashSz);
  29505. if (ret != 0)
  29506. return -10112;
  29507. if (hashSz != sizeof(expHash))
  29508. return -10113;
  29509. if (XMEMCMP(hash, expHash, hashSz) != 0)
  29510. return -10114;
  29511. ret = wc_SetEccsiHash(key, hash, hashSz);
  29512. if (ret != 0)
  29513. return -10112;
  29514. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  29515. &verified);
  29516. if (ret != 0)
  29517. return -10115;
  29518. if (!verified)
  29519. return -10116;
  29520. return 0;
  29521. }
  29522. static int eccsi_enc_dec_pair_test(EccsiKey* priv, mp_int* ssk, ecc_point* pvt)
  29523. {
  29524. int ret;
  29525. byte data[32 * 3];
  29526. word32 sz;
  29527. ecc_point* decPvt = NULL;
  29528. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29529. mp_int *decSsk = (mp_int *)XMALLOC(sizeof(*decSsk), HEAP_HINT,
  29530. DYNAMIC_TYPE_TMP_BUFFER);
  29531. if (decSsk == NULL)
  29532. return -10173;
  29533. #else
  29534. mp_int decSsk[1];
  29535. #endif
  29536. ret = mp_init(decSsk);
  29537. if (ret != 0)
  29538. ERROR_OUT(-10117, out);
  29539. decPvt = wc_ecc_new_point();
  29540. if (decPvt == NULL)
  29541. ERROR_OUT(-10118, out);
  29542. ret = wc_EncodeEccsiPair(priv, ssk, pvt, NULL, &sz);
  29543. if (ret != LENGTH_ONLY_E)
  29544. ERROR_OUT(-10119, out);
  29545. if (sz != 32 * 3)
  29546. ERROR_OUT(-10120, out);
  29547. ret = wc_EncodeEccsiPair(priv, ssk, pvt, data, &sz);
  29548. if (ret != 0)
  29549. ERROR_OUT(-10121, out);
  29550. if (sz != 32* 3)
  29551. ERROR_OUT(-10122, out);
  29552. ret = wc_DecodeEccsiPair(priv, data, sz, decSsk, decPvt);
  29553. if (ret != 0)
  29554. ERROR_OUT(-10123, out);
  29555. if (mp_cmp(ssk, decSsk) != MP_EQ)
  29556. ERROR_OUT(-10124, out);
  29557. if (wc_ecc_cmp_point(pvt, decPvt) != MP_EQ)
  29558. ERROR_OUT(-10125, out);
  29559. ret = wc_EncodeEccsiSsk(priv, ssk, NULL, &sz);
  29560. if (ret != LENGTH_ONLY_E)
  29561. ERROR_OUT(-10119, out);
  29562. if (sz != 32)
  29563. ERROR_OUT(-10120, out);
  29564. ret = wc_EncodeEccsiSsk(priv, ssk, data, &sz);
  29565. if (ret != 0)
  29566. ERROR_OUT(-10121, out);
  29567. if (sz != 32)
  29568. ERROR_OUT(-10122, out);
  29569. ret = wc_DecodeEccsiSsk(priv, data, sz, decSsk);
  29570. if (ret != 0)
  29571. ERROR_OUT(-10123, out);
  29572. if (mp_cmp(ssk, decSsk) != MP_EQ)
  29573. ERROR_OUT(-10124, out);
  29574. ret = wc_EncodeEccsiPvt(priv, pvt, NULL, &sz, 1);
  29575. if (ret != LENGTH_ONLY_E)
  29576. ERROR_OUT(-10126, out);
  29577. if (sz != 32 * 2)
  29578. ERROR_OUT(-10127, out);
  29579. ret = wc_EncodeEccsiPvt(priv, pvt, data, &sz, 1);
  29580. if (ret != 0)
  29581. ERROR_OUT(-10128, out);
  29582. if (sz != 32 * 2)
  29583. ERROR_OUT(-10129, out);
  29584. ret = wc_DecodeEccsiPvt(priv, data, sz, decPvt);
  29585. if (ret != 0)
  29586. ERROR_OUT(-10130, out);
  29587. if (wc_ecc_cmp_point(pvt, decPvt) != MP_EQ)
  29588. ERROR_OUT(-10131, out);
  29589. sz = sizeof(data);
  29590. ret = wc_EncodeEccsiPvt(priv, pvt, data, &sz, 0);
  29591. if (ret != 0)
  29592. ERROR_OUT(-10128, out);
  29593. if (sz != 32 * 2 + 1)
  29594. ERROR_OUT(-10129, out);
  29595. ret = wc_DecodeEccsiPvt(priv, data, sz, decPvt);
  29596. if (ret != 0)
  29597. ERROR_OUT(-10130, out);
  29598. if (wc_ecc_cmp_point(pvt, decPvt) != MP_EQ)
  29599. ERROR_OUT(-10131, out);
  29600. wc_ecc_del_point(decPvt);
  29601. out:
  29602. mp_free(decSsk);
  29603. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  29604. XFREE(decSsk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29605. #endif
  29606. return ret;
  29607. }
  29608. static int eccsi_imp_exp_key_test(EccsiKey* priv)
  29609. {
  29610. int ret;
  29611. byte data[32 * 3];
  29612. byte out[32 * 3];
  29613. word32 sz;
  29614. ret = wc_ExportEccsiKey(priv, NULL, &sz);
  29615. if (ret != LENGTH_ONLY_E)
  29616. return -10132;
  29617. if (sz != 32 * 3)
  29618. return -10133;
  29619. ret = wc_ExportEccsiKey(priv, data, &sz);
  29620. if (ret != 0)
  29621. return -10134;
  29622. ret = wc_ImportEccsiKey(priv, data, sz);
  29623. if (ret != 0)
  29624. return -10135;
  29625. ret = wc_ExportEccsiKey(priv, NULL, &sz);
  29626. if (ret != LENGTH_ONLY_E)
  29627. return -10132;
  29628. if (sz != 32 * 3)
  29629. return -10143;
  29630. ret = wc_ExportEccsiKey(priv, out, &sz);
  29631. if (ret != 0)
  29632. return -10144;
  29633. if (sz != 32 * 3)
  29634. return -10145;
  29635. if (XMEMCMP(data, out, sz) != 0)
  29636. return -10146;
  29637. ret = wc_ExportEccsiPrivateKey(priv, NULL, &sz);
  29638. if (ret != LENGTH_ONLY_E)
  29639. return -10156;
  29640. if (sz != 32)
  29641. return -10157;
  29642. ret = wc_ExportEccsiPrivateKey(priv, data, &sz);
  29643. if (ret != 0)
  29644. return -10158;
  29645. ret = wc_ImportEccsiPrivateKey(priv, data, sz);
  29646. if (ret != 0)
  29647. return -10159;
  29648. ret = wc_ExportEccsiPrivateKey(priv, NULL, &sz);
  29649. if (ret != LENGTH_ONLY_E)
  29650. return -10152;
  29651. if (sz != 32)
  29652. return -10163;
  29653. ret = wc_ExportEccsiPrivateKey(priv, out, &sz);
  29654. if (ret != 0)
  29655. return -10164;
  29656. if (sz != 32)
  29657. return -10165;
  29658. if (XMEMCMP(data, out, sz) != 0)
  29659. return -10166;
  29660. return 0;
  29661. }
  29662. static int eccsi_imp_exp_pubkey_test(EccsiKey* key1, EccsiKey* key2)
  29663. {
  29664. int ret;
  29665. byte data[32 * 2 + 1];
  29666. byte pubData[32 * 2 + 1];
  29667. word32 sz;
  29668. ret = wc_ExportEccsiPublicKey(key1, NULL, &sz, 1);
  29669. if (ret != LENGTH_ONLY_E)
  29670. return -10136;
  29671. if (sz != 32 * 2)
  29672. return -10137;
  29673. ret = wc_ExportEccsiPublicKey(key1, data, &sz, 1);
  29674. if (ret != 0)
  29675. return -10138;
  29676. ret = wc_ImportEccsiPublicKey(key2, data, sz, 1);
  29677. if (ret != 0)
  29678. return -10139;
  29679. sz = sizeof(pubData);
  29680. ret = wc_ExportEccsiPublicKey(key2, pubData, &sz, 1);
  29681. if (ret != 0)
  29682. return -10140;
  29683. if (sz != 32 * 2)
  29684. return -10141;
  29685. if (XMEMCMP(data, pubData, sz) != 0)
  29686. return -10142;
  29687. sz = sizeof(pubData);
  29688. ret = wc_ExportEccsiPublicKey(key2, pubData, &sz, 0);
  29689. if (ret != 0)
  29690. return -10140;
  29691. if (sz != 32 * 2 + 1)
  29692. return -10141;
  29693. if (pubData[0] != 0x04)
  29694. return -10140;
  29695. if (XMEMCMP(pubData + 1, data, sz - 1) != 0)
  29696. return -10142;
  29697. ret = wc_ImportEccsiPublicKey(key2, pubData, sz, 0);
  29698. if (ret != 0)
  29699. return -10139;
  29700. return 0;
  29701. }
  29702. static int eccsi_make_key_test(EccsiKey* priv, EccsiKey* pub, WC_RNG* rng,
  29703. mp_int* ssk, ecc_point* pvt)
  29704. {
  29705. int ret;
  29706. char mail[] = "test@wolfssl.com";
  29707. byte* id = (byte*)mail;
  29708. word32 idSz = (word32) XSTRLEN(mail);
  29709. int valid;
  29710. ret = wc_MakeEccsiKey(priv, rng);
  29711. if (ret != 0)
  29712. return -10143;
  29713. ret = eccsi_imp_exp_key_test(priv);
  29714. if (ret < 0)
  29715. return ret;
  29716. ret = eccsi_imp_exp_pubkey_test(priv, pub);
  29717. if (ret < 0)
  29718. return ret;
  29719. ret = wc_MakeEccsiPair(priv, rng, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt);
  29720. if (ret != 0)
  29721. return -10144;
  29722. ret = wc_ValidateEccsiPair(pub, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt,
  29723. &valid);
  29724. if (ret != 0)
  29725. return -10145;
  29726. if (!valid)
  29727. return -10146;
  29728. ret = eccsi_enc_dec_pair_test(priv, ssk, pvt);
  29729. if (ret != 0)
  29730. return ret;
  29731. return 0;
  29732. }
  29733. static int eccsi_sign_verify_test(EccsiKey* priv, EccsiKey* pub, WC_RNG* rng,
  29734. mp_int* ssk, ecc_point* pvt)
  29735. {
  29736. int ret;
  29737. byte hashPriv[WC_MAX_DIGEST_SIZE];
  29738. byte hashPub[WC_MAX_DIGEST_SIZE];
  29739. byte hashSz;
  29740. byte sig[144];
  29741. word32 sigSz;
  29742. int verified, valid;
  29743. char mail[] = "test@wolfssl.com";
  29744. byte* id = (byte*)mail;
  29745. word32 idSz = (word32) XSTRLEN(mail);
  29746. byte msg[] = { 0x00 };
  29747. word32 msgSz = sizeof(msg);
  29748. #ifdef WOLFSSL_SHA384
  29749. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA384, id, idSz, pvt, hashPriv,
  29750. &hashSz);
  29751. if (ret != BAD_FUNC_ARG)
  29752. return -10174;
  29753. #endif
  29754. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA256, id, idSz, pvt, hashPriv,
  29755. &hashSz);
  29756. if (ret != 0)
  29757. return -10147;
  29758. if (hashSz != 32)
  29759. return -10148;
  29760. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA256, id, idSz, pvt, hashPub,
  29761. &hashSz);
  29762. if (ret != 0)
  29763. return -10149;
  29764. if (hashSz != 32)
  29765. return -10150;
  29766. if (XMEMCMP(hashPriv, hashPub, hashSz) != 0)
  29767. return -10151;
  29768. ret = wc_SetEccsiHash(priv, hashPriv, hashSz);
  29769. if (ret != 0)
  29770. return -10149;
  29771. ret = wc_SetEccsiPair(priv, ssk, pvt);
  29772. if (ret != 0)
  29773. return -10149;
  29774. ret = wc_SignEccsiHash(priv, rng, WC_HASH_TYPE_SHA256, msg, msgSz, NULL,
  29775. &sigSz);
  29776. if (ret != LENGTH_ONLY_E)
  29777. return -10152;
  29778. if (sigSz != 129)
  29779. return -10153;
  29780. ret = wc_SignEccsiHash(priv, rng, WC_HASH_TYPE_SHA256, msg, msgSz, sig,
  29781. &sigSz);
  29782. if (ret != 0)
  29783. return -10154;
  29784. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  29785. if (ret != 0)
  29786. return -10149;
  29787. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  29788. &verified);
  29789. if (ret != 0)
  29790. return -10155;
  29791. if (!verified)
  29792. return -10156;
  29793. /* Check that changing HS results in verification failure. */
  29794. hashPub[0] ^= 0x80;
  29795. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  29796. if (ret != 0)
  29797. return -10149;
  29798. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  29799. &verified);
  29800. if (ret != 0)
  29801. return -10157;
  29802. if (verified)
  29803. return -10158;
  29804. hashPub[0] ^= 0x80;
  29805. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  29806. if (ret != 0)
  29807. return -10149;
  29808. /* Check that changing msg results in verification failure. */
  29809. msg[0] ^= 0x80;
  29810. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  29811. &verified);
  29812. if (ret != 0)
  29813. return -10159;
  29814. if (verified)
  29815. return -10160;
  29816. msg[0] ^= 0x80;
  29817. /* Check that changing signature results in verification failure. */
  29818. sig[0] ^= 0x80;
  29819. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  29820. &verified);
  29821. if (ret != 0)
  29822. return -10161;
  29823. if (verified)
  29824. return -10162;
  29825. sig[0] ^= 0x80;
  29826. /* Check that key state hasn't been invalidated. */
  29827. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  29828. &verified);
  29829. if (ret != 0)
  29830. return -10163;
  29831. if (!verified)
  29832. return -10164;
  29833. /* Check that verifying with the private key works. */
  29834. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  29835. &verified);
  29836. if (ret != 0)
  29837. return -10165;
  29838. if (!verified)
  29839. return -10166;
  29840. /* Check that the KPAK is converted from montgomery form. */
  29841. ret = eccsi_imp_exp_key_test(priv);
  29842. if (ret != 0)
  29843. return ret;
  29844. /* Check that KPAK can converted to Montgomery form again. */
  29845. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  29846. &verified);
  29847. if (ret != 0)
  29848. return -10167;
  29849. if (!verified)
  29850. return -10168;
  29851. /* Check that the KPAK is converted from montgomery form. */
  29852. ret = wc_ValidateEccsiPair(pub, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt,
  29853. &valid);
  29854. if (ret != 0)
  29855. return -10169;
  29856. if (!valid)
  29857. return -10170;
  29858. /* Check that KPAK can converted to Montgomery form again. */
  29859. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  29860. &verified);
  29861. if (ret != 0)
  29862. return -10171;
  29863. if (!verified)
  29864. return -10172;
  29865. /* Check that the KPAK is converted from montgomery form. */
  29866. ret = eccsi_imp_exp_pubkey_test(priv, pub);
  29867. if (ret != 0)
  29868. return ret;
  29869. return 0;
  29870. }
  29871. int eccsi_test(void)
  29872. {
  29873. int ret = 0;
  29874. WC_RNG rng;
  29875. int rng_inited = 0;
  29876. EccsiKey* priv = NULL;
  29877. EccsiKey* pub = NULL;
  29878. mp_int* ssk = NULL;
  29879. ecc_point* pvt = NULL;
  29880. priv = (EccsiKey*)XMALLOC(sizeof(EccsiKey), HEAP_HINT,
  29881. DYNAMIC_TYPE_TMP_BUFFER);
  29882. if (priv == NULL)
  29883. ret = -10205;
  29884. else
  29885. XMEMSET(priv, 0, sizeof(*priv));
  29886. if (ret == 0) {
  29887. pub = (EccsiKey*)XMALLOC(sizeof(EccsiKey), HEAP_HINT,
  29888. DYNAMIC_TYPE_TMP_BUFFER);
  29889. if (pub == NULL)
  29890. ret = -10206;
  29891. else
  29892. XMEMSET(pub, 0, sizeof(*pub));
  29893. }
  29894. if (ret == 0) {
  29895. ssk = (mp_int*)XMALLOC(sizeof(mp_int), HEAP_HINT,
  29896. DYNAMIC_TYPE_TMP_BUFFER);
  29897. if (ssk == NULL)
  29898. ret = -10207;
  29899. else
  29900. XMEMSET(ssk, 0, sizeof(*ssk));
  29901. }
  29902. if (ret == 0) {
  29903. #ifndef HAVE_FIPS
  29904. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  29905. #else
  29906. ret = wc_InitRng(&rng);
  29907. #endif
  29908. if (ret != 0)
  29909. ret = -10200;
  29910. else
  29911. rng_inited = 1;
  29912. }
  29913. if (ret == 0) {
  29914. pvt = wc_ecc_new_point();
  29915. if (pvt == NULL)
  29916. ret = -10201;
  29917. }
  29918. if (ret == 0) {
  29919. ret = mp_init(ssk);
  29920. if (ret != 0)
  29921. ret = -10202;
  29922. }
  29923. if (ret == 0) {
  29924. ret = eccsi_api_test(&rng, priv, ssk, pvt);
  29925. }
  29926. if (ret == 0) {
  29927. ret = wc_InitEccsiKey(pub, HEAP_HINT, INVALID_DEVID);
  29928. if (ret != 0)
  29929. ret = -10203;
  29930. }
  29931. if (ret == 0) {
  29932. ret = wc_InitEccsiKey(priv, HEAP_HINT, INVALID_DEVID);
  29933. if (ret != 0)
  29934. ret = -10204;
  29935. }
  29936. if (ret == 0) {
  29937. ret = eccsi_kat_verify_test(pub, pvt);
  29938. }
  29939. if (ret == 0) {
  29940. ret = eccsi_make_key_test(priv, pub, &rng, ssk, pvt);
  29941. }
  29942. if (ret == 0) {
  29943. ret = eccsi_sign_verify_test(priv, pub, &rng, ssk, pvt);
  29944. }
  29945. if (pvt != NULL)
  29946. wc_ecc_del_point(pvt);
  29947. if (rng_inited)
  29948. wc_FreeRng(&rng);
  29949. if (ssk != NULL) {
  29950. mp_free(ssk);
  29951. XFREE(ssk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29952. }
  29953. if (pub != NULL) {
  29954. wc_FreeEccsiKey(pub);
  29955. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29956. }
  29957. if (priv != NULL) {
  29958. wc_FreeEccsiKey(priv);
  29959. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  29960. }
  29961. return ret;
  29962. }
  29963. #endif /* WOLFCRYPT_HAVE_ECCSI */
  29964. #ifdef WOLFCRYPT_HAVE_SAKKE
  29965. static int sakke_api_test(WC_RNG* rng, SakkeKey* key, ecc_point* rsk)
  29966. {
  29967. int ret;
  29968. byte id[1] = { 0x00 };
  29969. int valid;
  29970. byte data[256];
  29971. word32 sz;
  29972. byte auth[257];
  29973. word16 authSz;
  29974. byte ssv[256];
  29975. word16 ssvSz;
  29976. word32 len;
  29977. ret = wc_InitSakkeKey_ex(NULL, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  29978. if (ret != BAD_FUNC_ARG)
  29979. return -10205;
  29980. ret = wc_InitSakkeKey_ex(NULL, 128, ECC_SAKKE_1, HEAP_HINT, INVALID_DEVID);
  29981. if (ret != BAD_FUNC_ARG)
  29982. return -10206;
  29983. wc_FreeSakkeKey(NULL);
  29984. XMEMSET(key, 0, sizeof(*key));
  29985. wc_FreeSakkeKey(key);
  29986. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, HEAP_HINT, INVALID_DEVID);
  29987. if (ret != 0)
  29988. return -10207;
  29989. ret = wc_MakeSakkeKey(NULL, NULL);
  29990. if (ret != BAD_FUNC_ARG)
  29991. return -10208;
  29992. ret = wc_MakeSakkeKey(key, NULL);
  29993. if (ret != BAD_FUNC_ARG)
  29994. return -10209;
  29995. ret = wc_MakeSakkeKey(NULL, rng);
  29996. if (ret != BAD_FUNC_ARG)
  29997. return -10210;
  29998. ret = wc_MakeSakkePublicKey(NULL, NULL);
  29999. if (ret != BAD_FUNC_ARG)
  30000. return -10211;
  30001. ret = wc_MakeSakkePublicKey(key, NULL);
  30002. if (ret != BAD_FUNC_ARG)
  30003. return -10212;
  30004. ret = wc_MakeSakkePublicKey(NULL, rsk);
  30005. if (ret != BAD_FUNC_ARG)
  30006. return -10213;
  30007. ret = wc_MakeSakkeRsk(NULL, NULL, 1, NULL);
  30008. if (ret != BAD_FUNC_ARG)
  30009. return -10214;
  30010. ret = wc_MakeSakkeRsk(key, id, 1, NULL);
  30011. if (ret != BAD_FUNC_ARG)
  30012. return -10215;
  30013. ret = wc_MakeSakkeRsk(key, NULL, 1, rsk);
  30014. if (ret != BAD_FUNC_ARG)
  30015. return -10216;
  30016. ret = wc_MakeSakkeRsk(NULL, id, 1, rsk);
  30017. if (ret != BAD_FUNC_ARG)
  30018. return -10217;
  30019. ret = wc_ValidateSakkeRsk(NULL, NULL, 1, NULL, NULL);
  30020. if (ret != BAD_FUNC_ARG)
  30021. return -10218;
  30022. ret = wc_ValidateSakkeRsk(key, id, 1, rsk, NULL);
  30023. if (ret != BAD_FUNC_ARG)
  30024. return -10219;
  30025. ret = wc_ValidateSakkeRsk(NULL, id, 1, rsk, &valid);
  30026. if (ret != BAD_FUNC_ARG)
  30027. return -10220;
  30028. ret = wc_ExportSakkeKey(NULL, NULL, NULL);
  30029. if (ret != BAD_FUNC_ARG)
  30030. return -10221;
  30031. ret = wc_ExportSakkeKey(key, data, NULL);
  30032. if (ret != BAD_FUNC_ARG)
  30033. return -10222;
  30034. ret = wc_ExportSakkeKey(NULL, data, &sz);
  30035. if (ret != BAD_FUNC_ARG)
  30036. return -10223;
  30037. ret = wc_ImportSakkeKey(NULL, NULL, 1);
  30038. if (ret != BAD_FUNC_ARG)
  30039. return -10224;
  30040. ret = wc_ImportSakkeKey(key, NULL, 1);
  30041. if (ret != BAD_FUNC_ARG)
  30042. return -10225;
  30043. ret = wc_ImportSakkeKey(NULL, data, 1);
  30044. if (ret != BAD_FUNC_ARG)
  30045. return -10226;
  30046. ret = wc_ExportSakkePrivateKey(NULL, NULL, NULL);
  30047. if (ret != BAD_FUNC_ARG)
  30048. return -10227;
  30049. ret = wc_ExportSakkePrivateKey(key, data, NULL);
  30050. if (ret != BAD_FUNC_ARG)
  30051. return -10228;
  30052. ret = wc_ExportSakkePrivateKey(NULL, data, &sz);
  30053. if (ret != BAD_FUNC_ARG)
  30054. return -10229;
  30055. ret = wc_ImportSakkePrivateKey(NULL, NULL, 1);
  30056. if (ret != BAD_FUNC_ARG)
  30057. return -10230;
  30058. ret = wc_ImportSakkePrivateKey(key, NULL, 1);
  30059. if (ret != BAD_FUNC_ARG)
  30060. return -10231;
  30061. ret = wc_ImportSakkePrivateKey(NULL, data, 1);
  30062. if (ret != BAD_FUNC_ARG)
  30063. return -10232;
  30064. sz = sizeof(data);
  30065. ret = wc_EncodeSakkeRsk(NULL, NULL, data, NULL, 1);
  30066. if (ret != BAD_FUNC_ARG)
  30067. return -10233;
  30068. ret = wc_EncodeSakkeRsk(key, rsk, data, NULL, 1);
  30069. if (ret != BAD_FUNC_ARG)
  30070. return -10234;
  30071. ret = wc_EncodeSakkeRsk(key, NULL, data, &sz, 1);
  30072. if (ret != BAD_FUNC_ARG)
  30073. return -10235;
  30074. ret = wc_EncodeSakkeRsk(NULL, rsk, data, &sz, 1);
  30075. if (ret != BAD_FUNC_ARG)
  30076. return -10236;
  30077. ret = wc_DecodeSakkeRsk(NULL, NULL, sz, NULL);
  30078. if (ret != BAD_FUNC_ARG)
  30079. return -10237;
  30080. ret = wc_DecodeSakkeRsk(key, data, sz, NULL);
  30081. if (ret != BAD_FUNC_ARG)
  30082. return -10238;
  30083. ret = wc_DecodeSakkeRsk(key, NULL, sz, rsk);
  30084. if (ret != BAD_FUNC_ARG)
  30085. return -10239;
  30086. ret = wc_DecodeSakkeRsk(NULL, data, sz, rsk);
  30087. if (ret != BAD_FUNC_ARG)
  30088. return -10240;
  30089. ret = wc_ImportSakkeRsk(NULL, NULL, sz);
  30090. if (ret != BAD_FUNC_ARG)
  30091. return -10237;
  30092. ret = wc_ImportSakkeRsk(key, NULL, sz);
  30093. if (ret != BAD_FUNC_ARG)
  30094. return -10237;
  30095. ret = wc_ImportSakkeRsk(NULL, data, sz);
  30096. if (ret != BAD_FUNC_ARG)
  30097. return -10237;
  30098. ret = wc_ImportSakkeRsk(key, data, 1);
  30099. if (ret != BUFFER_E)
  30100. return -10237;
  30101. ret = wc_GenerateSakkeRskTable(NULL, NULL, data, NULL);
  30102. if (ret != BAD_FUNC_ARG)
  30103. return -10241;
  30104. ret = wc_GenerateSakkeRskTable(key, NULL, data, NULL);
  30105. if (ret != BAD_FUNC_ARG)
  30106. return -10242;
  30107. ret = wc_GenerateSakkeRskTable(NULL, rsk, data, NULL);
  30108. if (ret != BAD_FUNC_ARG)
  30109. return -10243;
  30110. ret = wc_GenerateSakkeRskTable(NULL, NULL, data, &len);
  30111. if (ret != BAD_FUNC_ARG)
  30112. return -10244;
  30113. ret = wc_GenerateSakkeRskTable(key, rsk, data, NULL);
  30114. if (ret != BAD_FUNC_ARG)
  30115. return -10245;
  30116. ret = wc_GenerateSakkeRskTable(key, NULL, data, &len);
  30117. if (ret != BAD_FUNC_ARG)
  30118. return -10246;
  30119. ret = wc_GenerateSakkeRskTable(NULL, rsk, data, &len);
  30120. if (ret != BAD_FUNC_ARG)
  30121. return -10247;
  30122. ret = wc_GenerateSakkeRskTable(key, rsk, NULL, &len);
  30123. if (ret != LENGTH_ONLY_E)
  30124. return -10248;
  30125. len--;
  30126. ret = wc_GenerateSakkeRskTable(key, rsk, data, &len);
  30127. if (ret != BUFFER_E)
  30128. return -10249;
  30129. ret = wc_ExportSakkePublicKey(NULL, data, NULL, 1);
  30130. if (ret != BAD_FUNC_ARG)
  30131. return -10250;
  30132. ret = wc_ExportSakkePublicKey(key, data, NULL, 1);
  30133. if (ret != BAD_FUNC_ARG)
  30134. return -10251;
  30135. ret = wc_ExportSakkePublicKey(NULL, data, &sz, 1);
  30136. if (ret != BAD_FUNC_ARG)
  30137. return -10252;
  30138. ret = wc_ImportSakkePublicKey(NULL, NULL, sz, 1);
  30139. if (ret != BAD_FUNC_ARG)
  30140. return -10253;
  30141. ret = wc_ImportSakkePublicKey(key, NULL, sz, 1);
  30142. if (ret != BAD_FUNC_ARG)
  30143. return -10254;
  30144. ret = wc_ImportSakkePublicKey(NULL, data, sz, 1);
  30145. if (ret != BAD_FUNC_ARG)
  30146. return -10255;
  30147. ret = wc_GetSakkeAuthSize(NULL, NULL);
  30148. if (ret != BAD_FUNC_ARG)
  30149. return -10256;
  30150. ret = wc_GetSakkeAuthSize(key, NULL);
  30151. if (ret != BAD_FUNC_ARG)
  30152. return -10257;
  30153. ret = wc_GetSakkeAuthSize(NULL, &authSz);
  30154. if (ret != BAD_FUNC_ARG)
  30155. return -10258;
  30156. ret = wc_MakeSakkePointI(NULL, NULL, SAKKE_ID_MAX_SIZE + 1);
  30157. if (ret != BAD_FUNC_ARG)
  30158. return -10259;
  30159. ret = wc_MakeSakkePointI(key, NULL, SAKKE_ID_MAX_SIZE + 1);
  30160. if (ret != BAD_FUNC_ARG)
  30161. return -10260;
  30162. ret = wc_MakeSakkePointI(NULL, id, 1);
  30163. if (ret != BAD_FUNC_ARG)
  30164. return -10261;
  30165. ret = wc_MakeSakkePointI(NULL, NULL, 1);
  30166. if (ret != BAD_FUNC_ARG)
  30167. return -10262;
  30168. ret = wc_MakeSakkePointI(key, id, SAKKE_ID_MAX_SIZE + 1);
  30169. if (ret != BAD_FUNC_ARG)
  30170. return -10263;
  30171. ret = wc_MakeSakkePointI(key, NULL, 1);
  30172. if (ret != BAD_FUNC_ARG)
  30173. return -10264;
  30174. ret = wc_MakeSakkePointI(NULL, id, 1);
  30175. if (ret != BAD_FUNC_ARG)
  30176. return -10265;
  30177. ret = wc_GenerateSakkePointITable(NULL, data, NULL);
  30178. if (ret != BAD_FUNC_ARG)
  30179. return -10266;
  30180. ret = wc_GenerateSakkePointITable(key, data, NULL);
  30181. if (ret != BAD_FUNC_ARG)
  30182. return -10267;
  30183. ret = wc_GenerateSakkePointITable(NULL, data, &len);
  30184. if (ret != BAD_FUNC_ARG)
  30185. return -10268;
  30186. ret = wc_GenerateSakkePointITable(key, NULL, &len);
  30187. if (ret != LENGTH_ONLY_E)
  30188. return -10269;
  30189. len--;
  30190. ret = wc_GenerateSakkePointITable(key, data, &len);
  30191. if (ret != BUFFER_E)
  30192. return -10270;
  30193. ret = wc_SetSakkePointITable(NULL, NULL, 1);
  30194. if (ret != BAD_FUNC_ARG)
  30195. return -10271;
  30196. ret = wc_SetSakkePointITable(key, NULL, 1);
  30197. if (ret != BAD_FUNC_ARG)
  30198. return -10272;
  30199. ret = wc_SetSakkePointITable(NULL, data, 1);
  30200. if (ret != BAD_FUNC_ARG)
  30201. return -10273;
  30202. ret = wc_SetSakkePointITable(key, data, 1);
  30203. if (ret != BUFFER_E)
  30204. return -10274;
  30205. ret = wc_ClearSakkePointITable(NULL);
  30206. if (ret != BAD_FUNC_ARG)
  30207. return -10275;
  30208. ret = wc_GetSakkePointI(NULL, data, NULL);
  30209. if (ret != BAD_FUNC_ARG)
  30210. return -10276;
  30211. ret = wc_GetSakkePointI(key, data, NULL);
  30212. if (ret != BAD_FUNC_ARG)
  30213. return -10277;
  30214. ret = wc_GetSakkePointI(NULL, data, &sz);
  30215. if (ret != BAD_FUNC_ARG)
  30216. return -10278;
  30217. sz = 1;
  30218. ret = wc_GetSakkePointI(key, data, &sz);
  30219. if (ret != BUFFER_E)
  30220. return -10279;
  30221. sz = 256;
  30222. ret = wc_SetSakkePointI(NULL, NULL, 1, NULL, sz);
  30223. if (ret != BAD_FUNC_ARG)
  30224. return -10280;
  30225. ret = wc_SetSakkePointI(key, NULL, 1, NULL, sz);
  30226. if (ret != BAD_FUNC_ARG)
  30227. return -10281;
  30228. ret = wc_SetSakkePointI(NULL, id, 1, NULL, sz);
  30229. if (ret != BAD_FUNC_ARG)
  30230. return -10282;
  30231. ret = wc_SetSakkePointI(NULL, NULL, 1, data, sz);
  30232. if (ret != BAD_FUNC_ARG)
  30233. return -10283;
  30234. ret = wc_SetSakkePointI(key, id, 1, NULL, sz);
  30235. if (ret != BAD_FUNC_ARG)
  30236. return -10284;
  30237. ret = wc_SetSakkePointI(key, NULL, 1, data, sz);
  30238. if (ret != BAD_FUNC_ARG)
  30239. return -10285;
  30240. ret = wc_SetSakkePointI(NULL, id, 1, data, sz);
  30241. if (ret != BAD_FUNC_ARG)
  30242. return -10286;
  30243. ret = wc_SetSakkePointI(key, id, SAKKE_ID_MAX_SIZE + 1, data, sz);
  30244. if (ret != BUFFER_E)
  30245. return -10287;
  30246. ret = wc_SetSakkePointI(key, id, 1, data, sz - 1);
  30247. if (ret != BUFFER_E)
  30248. return -10288;
  30249. ret = wc_SetSakkeIdentity(NULL, NULL, 1);
  30250. if (ret != BAD_FUNC_ARG)
  30251. return -10286;
  30252. ret = wc_SetSakkeIdentity(key, NULL, 1);
  30253. if (ret != BAD_FUNC_ARG)
  30254. return -10286;
  30255. ret = wc_SetSakkeIdentity(NULL, id, 1);
  30256. if (ret != BAD_FUNC_ARG)
  30257. return -10286;
  30258. ssvSz = sizeof(ssv);
  30259. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  30260. auth, NULL);
  30261. if (ret != BAD_FUNC_ARG)
  30262. return -10289;
  30263. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  30264. auth, NULL);
  30265. if (ret != BAD_FUNC_ARG)
  30266. return -10290;
  30267. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  30268. auth, NULL);
  30269. if (ret != BAD_FUNC_ARG)
  30270. return -10291;
  30271. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  30272. auth, &authSz);
  30273. if (ret != BAD_FUNC_ARG)
  30274. return -10292;
  30275. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  30276. auth, NULL);
  30277. if (ret != BAD_FUNC_ARG)
  30278. return -10293;
  30279. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  30280. auth, &authSz);
  30281. if (ret != BAD_FUNC_ARG)
  30282. return -10294;
  30283. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  30284. auth, &authSz);
  30285. if (ret != BAD_FUNC_ARG)
  30286. return -10295;
  30287. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  30288. auth, &authSz);
  30289. if (ret != BAD_STATE_E)
  30290. return -10295;
  30291. ret = wc_GenerateSakkeSSV(NULL, NULL, data, NULL);
  30292. if (ret != BAD_FUNC_ARG)
  30293. return -10296;
  30294. ret = wc_GenerateSakkeSSV(key, rng, data, NULL);
  30295. if (ret != BAD_FUNC_ARG)
  30296. return -10297;
  30297. ret = wc_GenerateSakkeSSV(key, NULL, data, &ssvSz);
  30298. if (ret != BAD_FUNC_ARG)
  30299. return -10298;
  30300. ret = wc_GenerateSakkeSSV(NULL, rng, data, &ssvSz);
  30301. if (ret != BAD_FUNC_ARG)
  30302. return -10299;
  30303. ret = wc_SetSakkeRsk(NULL, NULL, data, 1);
  30304. if (ret != BAD_FUNC_ARG)
  30305. return -10286;
  30306. ret = wc_SetSakkeRsk(key, NULL, data, 1);
  30307. if (ret != BAD_FUNC_ARG)
  30308. return -10286;
  30309. ret = wc_SetSakkeRsk(NULL, rsk, data, 1);
  30310. if (ret != BAD_FUNC_ARG)
  30311. return -10286;
  30312. ssvSz = sizeof(ssv);
  30313. authSz = sizeof(auth);
  30314. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz, NULL,
  30315. authSz);
  30316. if (ret != BAD_FUNC_ARG)
  30317. return -10300;
  30318. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz, NULL,
  30319. authSz);
  30320. if (ret != BAD_FUNC_ARG)
  30321. return -10300;
  30322. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz, NULL,
  30323. authSz);
  30324. if (ret != BAD_FUNC_ARG)
  30325. return -10300;
  30326. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz, auth,
  30327. authSz);
  30328. if (ret != BAD_FUNC_ARG)
  30329. return -10300;
  30330. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, NULL,
  30331. authSz);
  30332. if (ret != BAD_FUNC_ARG)
  30333. return -10300;
  30334. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz, auth,
  30335. authSz);
  30336. if (ret != BAD_FUNC_ARG)
  30337. return -10300;
  30338. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  30339. authSz);
  30340. if (ret != BAD_FUNC_ARG)
  30341. return -10300;
  30342. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  30343. authSz);
  30344. if (ret != BAD_STATE_E)
  30345. return -10300;
  30346. ret = wc_SetSakkeIdentity(key, id, 1);
  30347. if (ret != 0)
  30348. return -10286;
  30349. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  30350. authSz);
  30351. if (ret != BAD_STATE_E)
  30352. return -10300;
  30353. ret = wc_SetSakkeIdentity(key, id, 0);
  30354. if (ret != 0)
  30355. return -10286;
  30356. ret = wc_SetSakkeRsk(key, rsk, data, 1);
  30357. if (ret != 0)
  30358. return -10286;
  30359. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  30360. authSz);
  30361. if (ret != BAD_STATE_E)
  30362. return -10300;
  30363. wc_FreeSakkeKey(key);
  30364. return 0;
  30365. }
  30366. static int sakke_kat_derive_test(SakkeKey* key, ecc_point* rsk)
  30367. {
  30368. WOLFSSL_SMALL_STACK_STATIC const byte pubData[] = {
  30369. 0x59, 0x58, 0xEF, 0x1B, 0x16, 0x79, 0xBF, 0x09,
  30370. 0x9B, 0x3A, 0x03, 0x0D, 0xF2, 0x55, 0xAA, 0x6A,
  30371. 0x23, 0xC1, 0xD8, 0xF1, 0x43, 0xD4, 0xD2, 0x3F,
  30372. 0x75, 0x3E, 0x69, 0xBD, 0x27, 0xA8, 0x32, 0xF3,
  30373. 0x8C, 0xB4, 0xAD, 0x53, 0xDD, 0xEF, 0x42, 0x60,
  30374. 0xB0, 0xFE, 0x8B, 0xB4, 0x5C, 0x4C, 0x1F, 0xF5,
  30375. 0x10, 0xEF, 0xFE, 0x30, 0x03, 0x67, 0xA3, 0x7B,
  30376. 0x61, 0xF7, 0x01, 0xD9, 0x14, 0xAE, 0xF0, 0x97,
  30377. 0x24, 0x82, 0x5F, 0xA0, 0x70, 0x7D, 0x61, 0xA6,
  30378. 0xDF, 0xF4, 0xFB, 0xD7, 0x27, 0x35, 0x66, 0xCD,
  30379. 0xDE, 0x35, 0x2A, 0x0B, 0x04, 0xB7, 0xC1, 0x6A,
  30380. 0x78, 0x30, 0x9B, 0xE6, 0x40, 0x69, 0x7D, 0xE7,
  30381. 0x47, 0x61, 0x3A, 0x5F, 0xC1, 0x95, 0xE8, 0xB9,
  30382. 0xF3, 0x28, 0x85, 0x2A, 0x57, 0x9D, 0xB8, 0xF9,
  30383. 0x9B, 0x1D, 0x00, 0x34, 0x47, 0x9E, 0xA9, 0xC5,
  30384. 0x59, 0x5F, 0x47, 0xC4, 0xB2, 0xF5, 0x4F, 0xF2,
  30385. 0x15, 0x08, 0xD3, 0x75, 0x14, 0xDC, 0xF7, 0xA8,
  30386. 0xE1, 0x43, 0xA6, 0x05, 0x8C, 0x09, 0xA6, 0xBF,
  30387. 0x2C, 0x98, 0x58, 0xCA, 0x37, 0xC2, 0x58, 0x06,
  30388. 0x5A, 0xE6, 0xBF, 0x75, 0x32, 0xBC, 0x8B, 0x5B,
  30389. 0x63, 0x38, 0x38, 0x66, 0xE0, 0x75, 0x3C, 0x5A,
  30390. 0xC0, 0xE7, 0x27, 0x09, 0xF8, 0x44, 0x5F, 0x2E,
  30391. 0x61, 0x78, 0xE0, 0x65, 0x85, 0x7E, 0x0E, 0xDA,
  30392. 0x10, 0xF6, 0x82, 0x06, 0xB6, 0x35, 0x05, 0xED,
  30393. 0x87, 0xE5, 0x34, 0xFB, 0x28, 0x31, 0xFF, 0x95,
  30394. 0x7F, 0xB7, 0xDC, 0x61, 0x9D, 0xAE, 0x61, 0x30,
  30395. 0x1E, 0xEA, 0xCC, 0x2F, 0xDA, 0x36, 0x80, 0xEA,
  30396. 0x49, 0x99, 0x25, 0x8A, 0x83, 0x3C, 0xEA, 0x8F,
  30397. 0xC6, 0x7C, 0x6D, 0x19, 0x48, 0x7F, 0xB4, 0x49,
  30398. 0x05, 0x9F, 0x26, 0xCC, 0x8A, 0xAB, 0x65, 0x5A,
  30399. 0xB5, 0x8B, 0x7C, 0xC7, 0x96, 0xE2, 0x4E, 0x9A,
  30400. 0x39, 0x40, 0x95, 0x75, 0x4F, 0x5F, 0x8B, 0xAE
  30401. };
  30402. WOLFSSL_SMALL_STACK_STATIC const byte rskData[] = {
  30403. 0x93, 0xAF, 0x67, 0xE5, 0x00, 0x7B, 0xA6, 0xE6,
  30404. 0xA8, 0x0D, 0xA7, 0x93, 0xDA, 0x30, 0x0F, 0xA4,
  30405. 0xB5, 0x2D, 0x0A, 0x74, 0xE2, 0x5E, 0x6E, 0x7B,
  30406. 0x2B, 0x3D, 0x6E, 0xE9, 0xD1, 0x8A, 0x9B, 0x5C,
  30407. 0x50, 0x23, 0x59, 0x7B, 0xD8, 0x2D, 0x80, 0x62,
  30408. 0xD3, 0x40, 0x19, 0x56, 0x3B, 0xA1, 0xD2, 0x5C,
  30409. 0x0D, 0xC5, 0x6B, 0x7B, 0x97, 0x9D, 0x74, 0xAA,
  30410. 0x50, 0xF2, 0x9F, 0xBF, 0x11, 0xCC, 0x2C, 0x93,
  30411. 0xF5, 0xDF, 0xCA, 0x61, 0x5E, 0x60, 0x92, 0x79,
  30412. 0xF6, 0x17, 0x5C, 0xEA, 0xDB, 0x00, 0xB5, 0x8C,
  30413. 0x6B, 0xEE, 0x1E, 0x7A, 0x2A, 0x47, 0xC4, 0xF0,
  30414. 0xC4, 0x56, 0xF0, 0x52, 0x59, 0xA6, 0xFA, 0x94,
  30415. 0xA6, 0x34, 0xA4, 0x0D, 0xAE, 0x1D, 0xF5, 0x93,
  30416. 0xD4, 0xFE, 0xCF, 0x68, 0x8D, 0x5F, 0xC6, 0x78,
  30417. 0xBE, 0x7E, 0xFC, 0x6D, 0xF3, 0xD6, 0x83, 0x53,
  30418. 0x25, 0xB8, 0x3B, 0x2C, 0x6E, 0x69, 0x03, 0x6B,
  30419. 0x15, 0x5F, 0x0A, 0x27, 0x24, 0x10, 0x94, 0xB0,
  30420. 0x4B, 0xFB, 0x0B, 0xDF, 0xAC, 0x6C, 0x67, 0x0A,
  30421. 0x65, 0xC3, 0x25, 0xD3, 0x9A, 0x06, 0x9F, 0x03,
  30422. 0x65, 0x9D, 0x44, 0xCA, 0x27, 0xD3, 0xBE, 0x8D,
  30423. 0xF3, 0x11, 0x17, 0x2B, 0x55, 0x41, 0x60, 0x18,
  30424. 0x1C, 0xBE, 0x94, 0xA2, 0xA7, 0x83, 0x32, 0x0C,
  30425. 0xED, 0x59, 0x0B, 0xC4, 0x26, 0x44, 0x70, 0x2C,
  30426. 0xF3, 0x71, 0x27, 0x1E, 0x49, 0x6B, 0xF2, 0x0F,
  30427. 0x58, 0x8B, 0x78, 0xA1, 0xBC, 0x01, 0xEC, 0xBB,
  30428. 0x65, 0x59, 0x93, 0x4B, 0xDD, 0x2F, 0xB6, 0x5D,
  30429. 0x28, 0x84, 0x31, 0x8A, 0x33, 0xD1, 0xA4, 0x2A,
  30430. 0xDF, 0x5E, 0x33, 0xCC, 0x58, 0x00, 0x28, 0x0B,
  30431. 0x28, 0x35, 0x64, 0x97, 0xF8, 0x71, 0x35, 0xBA,
  30432. 0xB9, 0x61, 0x2A, 0x17, 0x26, 0x04, 0x24, 0x40,
  30433. 0x9A, 0xC1, 0x5F, 0xEE, 0x99, 0x6B, 0x74, 0x4C,
  30434. 0x33, 0x21, 0x51, 0x23, 0x5D, 0xEC, 0xB0, 0xF5
  30435. };
  30436. WOLFSSL_SMALL_STACK_STATIC const byte id[] = {
  30437. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  30438. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  30439. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  30440. 0x33, 0x00
  30441. };
  30442. WOLFSSL_SMALL_STACK_STATIC const byte ssv[] = {
  30443. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
  30444. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0
  30445. };
  30446. WOLFSSL_SMALL_STACK_STATIC const byte auth[] = {
  30447. 0x04,
  30448. 0x44, 0xE8, 0xAD, 0x44, 0xAB, 0x85, 0x92, 0xA6,
  30449. 0xA5, 0xA3, 0xDD, 0xCA, 0x5C, 0xF8, 0x96, 0xC7,
  30450. 0x18, 0x04, 0x36, 0x06, 0xA0, 0x1D, 0x65, 0x0D,
  30451. 0xEF, 0x37, 0xA0, 0x1F, 0x37, 0xC2, 0x28, 0xC3,
  30452. 0x32, 0xFC, 0x31, 0x73, 0x54, 0xE2, 0xC2, 0x74,
  30453. 0xD4, 0xDA, 0xF8, 0xAD, 0x00, 0x10, 0x54, 0xC7,
  30454. 0x6C, 0xE5, 0x79, 0x71, 0xC6, 0xF4, 0x48, 0x6D,
  30455. 0x57, 0x23, 0x04, 0x32, 0x61, 0xC5, 0x06, 0xEB,
  30456. 0xF5, 0xBE, 0x43, 0x8F, 0x53, 0xDE, 0x04, 0xF0,
  30457. 0x67, 0xC7, 0x76, 0xE0, 0xDD, 0x3B, 0x71, 0xA6,
  30458. 0x29, 0x01, 0x33, 0x28, 0x37, 0x25, 0xA5, 0x32,
  30459. 0xF2, 0x1A, 0xF1, 0x45, 0x12, 0x6D, 0xC1, 0xD7,
  30460. 0x77, 0xEC, 0xC2, 0x7B, 0xE5, 0x08, 0x35, 0xBD,
  30461. 0x28, 0x09, 0x8B, 0x8A, 0x73, 0xD9, 0xF8, 0x01,
  30462. 0xD8, 0x93, 0x79, 0x3A, 0x41, 0xFF, 0x5C, 0x49,
  30463. 0xB8, 0x7E, 0x79, 0xF2, 0xBE, 0x4D, 0x56, 0xCE,
  30464. 0x55, 0x7E, 0x13, 0x4A, 0xD8, 0x5B, 0xB1, 0xD4,
  30465. 0xB9, 0xCE, 0x4F, 0x8B, 0xE4, 0xB0, 0x8A, 0x12,
  30466. 0xBA, 0xBF, 0x55, 0xB1, 0xD6, 0xF1, 0xD7, 0xA6,
  30467. 0x38, 0x01, 0x9E, 0xA2, 0x8E, 0x15, 0xAB, 0x1C,
  30468. 0x9F, 0x76, 0x37, 0x5F, 0xDD, 0x12, 0x10, 0xD4,
  30469. 0xF4, 0x35, 0x1B, 0x9A, 0x00, 0x94, 0x86, 0xB7,
  30470. 0xF3, 0xED, 0x46, 0xC9, 0x65, 0xDE, 0xD2, 0xD8,
  30471. 0x0D, 0xAD, 0xE4, 0xF3, 0x8C, 0x67, 0x21, 0xD5,
  30472. 0x2C, 0x3A, 0xD1, 0x03, 0xA1, 0x0E, 0xBD, 0x29,
  30473. 0x59, 0x24, 0x8B, 0x4E, 0xF0, 0x06, 0x83, 0x6B,
  30474. 0xF0, 0x97, 0x44, 0x8E, 0x61, 0x07, 0xC9, 0xED,
  30475. 0xEE, 0x9F, 0xB7, 0x04, 0x82, 0x3D, 0xF1, 0x99,
  30476. 0xF8, 0x32, 0xC9, 0x05, 0xAE, 0x45, 0xF8, 0xA2,
  30477. 0x47, 0xA0, 0x72, 0xD8, 0xEF, 0x72, 0x9E, 0xAB,
  30478. 0xC5, 0xE2, 0x75, 0x74, 0xB0, 0x77, 0x39, 0xB3,
  30479. 0x4B, 0xE7, 0x4A, 0x53, 0x2F, 0x74, 0x7B, 0x86
  30480. };
  30481. WOLFSSL_SMALL_STACK_STATIC const byte encSsv[] = {
  30482. 0x89, 0xE0, 0xBC, 0x66, 0x1A, 0xA1, 0xE9, 0x16,
  30483. 0x38, 0xE6, 0xAC, 0xC8, 0x4E, 0x49, 0x65, 0x07
  30484. };
  30485. int ret;
  30486. int valid;
  30487. byte pubKey[sizeof(pubData) + 1];
  30488. word32 sz = sizeof(pubKey);
  30489. byte tmpSsv[sizeof(encSsv)];
  30490. byte* iTable = NULL;
  30491. word32 iTableLen;
  30492. byte* table = NULL;
  30493. word32 len;
  30494. ret = wc_ImportSakkePublicKey(key, pubData, sizeof(pubData), 0);
  30495. if (ret != 0)
  30496. return -10315;
  30497. ret = wc_DecodeSakkeRsk(key, rskData, sizeof(rskData), rsk);
  30498. if (ret != 0)
  30499. return -10316;
  30500. ret = wc_ValidateSakkeRsk(key, id, sizeof(id), rsk, &valid);
  30501. if (ret != 0)
  30502. return -10317;
  30503. if (valid != 1)
  30504. return -10318;
  30505. ret = wc_SetSakkeRsk(key, rsk, NULL, 0);
  30506. if (ret != 0)
  30507. return -10319;
  30508. ret = wc_SetSakkeIdentity(key, id, sizeof(id));
  30509. if (ret != 0)
  30510. return -10319;
  30511. XMEMCPY(tmpSsv, encSsv, sizeof(encSsv));
  30512. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, tmpSsv, sizeof(tmpSsv),
  30513. auth, sizeof(auth));
  30514. if (ret != 0)
  30515. return -10322;
  30516. if (XMEMCMP(tmpSsv, ssv, sizeof(ssv)) != 0)
  30517. return -10320;
  30518. ret = wc_MakeSakkePointI(key, id, sizeof(id));
  30519. if (ret != 0)
  30520. return -10321;
  30521. iTableLen = 0;
  30522. ret = wc_GenerateSakkePointITable(key, NULL, &iTableLen);
  30523. if (ret != LENGTH_ONLY_E)
  30524. return -10322;
  30525. if (iTableLen != 0) {
  30526. iTable = (byte*)XMALLOC(iTableLen, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30527. if (iTable == NULL)
  30528. return -10323;
  30529. ret = wc_GenerateSakkePointITable(key, iTable, &iTableLen);
  30530. if (ret != 0)
  30531. return -10324;
  30532. }
  30533. len = 0;
  30534. ret = wc_GenerateSakkeRskTable(key, rsk, NULL, &len);
  30535. if (ret != LENGTH_ONLY_E)
  30536. return -10325;
  30537. if (len > 0) {
  30538. table = (byte*)XMALLOC(len, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30539. if (table == NULL)
  30540. return -10326;
  30541. ret = wc_GenerateSakkeRskTable(key, rsk, table, &len);
  30542. if (ret != 0)
  30543. return -10327;
  30544. }
  30545. ret = wc_SetSakkeRsk(key, rsk, table, len);
  30546. if (ret != 0)
  30547. return -10319;
  30548. XMEMCPY(tmpSsv, encSsv, sizeof(encSsv));
  30549. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, tmpSsv, sizeof(tmpSsv),
  30550. auth, sizeof(auth));
  30551. if (ret != 0)
  30552. return -10328;
  30553. if (XMEMCMP(tmpSsv, ssv, sizeof(ssv)) != 0)
  30554. return -10329;
  30555. /* Don't reference table that is about to be freed. */
  30556. ret = wc_ClearSakkePointITable(key);
  30557. if (ret != 0)
  30558. return -10330;
  30559. /* Dispose of tables */
  30560. if (iTable != NULL)
  30561. XFREE(iTable, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30562. if (table != NULL)
  30563. XFREE(table, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30564. /* Make sure the key public key is exportable - convert to Montgomery form
  30565. * in Validation.
  30566. */
  30567. ret = wc_ExportSakkePublicKey(key, pubKey, &sz, 1);
  30568. if (ret != 0)
  30569. return -10331;
  30570. if (sz != sizeof(pubData))
  30571. return -10332;
  30572. if (XMEMCMP(pubKey, pubData, sizeof(pubData)) != 0)
  30573. return -10333;
  30574. sz = sizeof(pubData) + 1;
  30575. ret = wc_ExportSakkePublicKey(key, pubKey, &sz, 0);
  30576. if (ret != 0)
  30577. return -10334;
  30578. if (sz != sizeof(pubData) + 1)
  30579. return -10335;
  30580. if (pubKey[0] != 0x04)
  30581. return -10336;
  30582. if (XMEMCMP(pubKey + 1, pubData, sizeof(pubData)) != 0)
  30583. return -10337;
  30584. return 0;
  30585. }
  30586. static int sakke_kat_encapsulate_test(SakkeKey* key)
  30587. {
  30588. static const byte pubData[] = {
  30589. 0x59, 0x58, 0xEF, 0x1B, 0x16, 0x79, 0xBF, 0x09,
  30590. 0x9B, 0x3A, 0x03, 0x0D, 0xF2, 0x55, 0xAA, 0x6A,
  30591. 0x23, 0xC1, 0xD8, 0xF1, 0x43, 0xD4, 0xD2, 0x3F,
  30592. 0x75, 0x3E, 0x69, 0xBD, 0x27, 0xA8, 0x32, 0xF3,
  30593. 0x8C, 0xB4, 0xAD, 0x53, 0xDD, 0xEF, 0x42, 0x60,
  30594. 0xB0, 0xFE, 0x8B, 0xB4, 0x5C, 0x4C, 0x1F, 0xF5,
  30595. 0x10, 0xEF, 0xFE, 0x30, 0x03, 0x67, 0xA3, 0x7B,
  30596. 0x61, 0xF7, 0x01, 0xD9, 0x14, 0xAE, 0xF0, 0x97,
  30597. 0x24, 0x82, 0x5F, 0xA0, 0x70, 0x7D, 0x61, 0xA6,
  30598. 0xDF, 0xF4, 0xFB, 0xD7, 0x27, 0x35, 0x66, 0xCD,
  30599. 0xDE, 0x35, 0x2A, 0x0B, 0x04, 0xB7, 0xC1, 0x6A,
  30600. 0x78, 0x30, 0x9B, 0xE6, 0x40, 0x69, 0x7D, 0xE7,
  30601. 0x47, 0x61, 0x3A, 0x5F, 0xC1, 0x95, 0xE8, 0xB9,
  30602. 0xF3, 0x28, 0x85, 0x2A, 0x57, 0x9D, 0xB8, 0xF9,
  30603. 0x9B, 0x1D, 0x00, 0x34, 0x47, 0x9E, 0xA9, 0xC5,
  30604. 0x59, 0x5F, 0x47, 0xC4, 0xB2, 0xF5, 0x4F, 0xF2,
  30605. 0x15, 0x08, 0xD3, 0x75, 0x14, 0xDC, 0xF7, 0xA8,
  30606. 0xE1, 0x43, 0xA6, 0x05, 0x8C, 0x09, 0xA6, 0xBF,
  30607. 0x2C, 0x98, 0x58, 0xCA, 0x37, 0xC2, 0x58, 0x06,
  30608. 0x5A, 0xE6, 0xBF, 0x75, 0x32, 0xBC, 0x8B, 0x5B,
  30609. 0x63, 0x38, 0x38, 0x66, 0xE0, 0x75, 0x3C, 0x5A,
  30610. 0xC0, 0xE7, 0x27, 0x09, 0xF8, 0x44, 0x5F, 0x2E,
  30611. 0x61, 0x78, 0xE0, 0x65, 0x85, 0x7E, 0x0E, 0xDA,
  30612. 0x10, 0xF6, 0x82, 0x06, 0xB6, 0x35, 0x05, 0xED,
  30613. 0x87, 0xE5, 0x34, 0xFB, 0x28, 0x31, 0xFF, 0x95,
  30614. 0x7F, 0xB7, 0xDC, 0x61, 0x9D, 0xAE, 0x61, 0x30,
  30615. 0x1E, 0xEA, 0xCC, 0x2F, 0xDA, 0x36, 0x80, 0xEA,
  30616. 0x49, 0x99, 0x25, 0x8A, 0x83, 0x3C, 0xEA, 0x8F,
  30617. 0xC6, 0x7C, 0x6D, 0x19, 0x48, 0x7F, 0xB4, 0x49,
  30618. 0x05, 0x9F, 0x26, 0xCC, 0x8A, 0xAB, 0x65, 0x5A,
  30619. 0xB5, 0x8B, 0x7C, 0xC7, 0x96, 0xE2, 0x4E, 0x9A,
  30620. 0x39, 0x40, 0x95, 0x75, 0x4F, 0x5F, 0x8B, 0xAE
  30621. };
  30622. static const byte id[] = {
  30623. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  30624. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  30625. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  30626. 0x33, 0x00
  30627. };
  30628. static word32 idSz = sizeof(id);
  30629. byte ssv[] = {
  30630. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
  30631. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0
  30632. };
  30633. static word16 ssvSz = sizeof(ssv);
  30634. static const byte expAuth[] = {
  30635. 0x04,
  30636. 0x44, 0xE8, 0xAD, 0x44, 0xAB, 0x85, 0x92, 0xA6,
  30637. 0xA5, 0xA3, 0xDD, 0xCA, 0x5C, 0xF8, 0x96, 0xC7,
  30638. 0x18, 0x04, 0x36, 0x06, 0xA0, 0x1D, 0x65, 0x0D,
  30639. 0xEF, 0x37, 0xA0, 0x1F, 0x37, 0xC2, 0x28, 0xC3,
  30640. 0x32, 0xFC, 0x31, 0x73, 0x54, 0xE2, 0xC2, 0x74,
  30641. 0xD4, 0xDA, 0xF8, 0xAD, 0x00, 0x10, 0x54, 0xC7,
  30642. 0x6C, 0xE5, 0x79, 0x71, 0xC6, 0xF4, 0x48, 0x6D,
  30643. 0x57, 0x23, 0x04, 0x32, 0x61, 0xC5, 0x06, 0xEB,
  30644. 0xF5, 0xBE, 0x43, 0x8F, 0x53, 0xDE, 0x04, 0xF0,
  30645. 0x67, 0xC7, 0x76, 0xE0, 0xDD, 0x3B, 0x71, 0xA6,
  30646. 0x29, 0x01, 0x33, 0x28, 0x37, 0x25, 0xA5, 0x32,
  30647. 0xF2, 0x1A, 0xF1, 0x45, 0x12, 0x6D, 0xC1, 0xD7,
  30648. 0x77, 0xEC, 0xC2, 0x7B, 0xE5, 0x08, 0x35, 0xBD,
  30649. 0x28, 0x09, 0x8B, 0x8A, 0x73, 0xD9, 0xF8, 0x01,
  30650. 0xD8, 0x93, 0x79, 0x3A, 0x41, 0xFF, 0x5C, 0x49,
  30651. 0xB8, 0x7E, 0x79, 0xF2, 0xBE, 0x4D, 0x56, 0xCE,
  30652. 0x55, 0x7E, 0x13, 0x4A, 0xD8, 0x5B, 0xB1, 0xD4,
  30653. 0xB9, 0xCE, 0x4F, 0x8B, 0xE4, 0xB0, 0x8A, 0x12,
  30654. 0xBA, 0xBF, 0x55, 0xB1, 0xD6, 0xF1, 0xD7, 0xA6,
  30655. 0x38, 0x01, 0x9E, 0xA2, 0x8E, 0x15, 0xAB, 0x1C,
  30656. 0x9F, 0x76, 0x37, 0x5F, 0xDD, 0x12, 0x10, 0xD4,
  30657. 0xF4, 0x35, 0x1B, 0x9A, 0x00, 0x94, 0x86, 0xB7,
  30658. 0xF3, 0xED, 0x46, 0xC9, 0x65, 0xDE, 0xD2, 0xD8,
  30659. 0x0D, 0xAD, 0xE4, 0xF3, 0x8C, 0x67, 0x21, 0xD5,
  30660. 0x2C, 0x3A, 0xD1, 0x03, 0xA1, 0x0E, 0xBD, 0x29,
  30661. 0x59, 0x24, 0x8B, 0x4E, 0xF0, 0x06, 0x83, 0x6B,
  30662. 0xF0, 0x97, 0x44, 0x8E, 0x61, 0x07, 0xC9, 0xED,
  30663. 0xEE, 0x9F, 0xB7, 0x04, 0x82, 0x3D, 0xF1, 0x99,
  30664. 0xF8, 0x32, 0xC9, 0x05, 0xAE, 0x45, 0xF8, 0xA2,
  30665. 0x47, 0xA0, 0x72, 0xD8, 0xEF, 0x72, 0x9E, 0xAB,
  30666. 0xC5, 0xE2, 0x75, 0x74, 0xB0, 0x77, 0x39, 0xB3,
  30667. 0x4B, 0xE7, 0x4A, 0x53, 0x2F, 0x74, 0x7B, 0x86
  30668. };
  30669. static const byte encSsv[] = {
  30670. 0x89, 0xE0, 0xBC, 0x66, 0x1A, 0xA1, 0xE9, 0x16,
  30671. 0x38, 0xE6, 0xAC, 0xC8, 0x4E, 0x49, 0x65, 0x07
  30672. };
  30673. int ret;
  30674. byte auth[257];
  30675. word16 authSz = sizeof(auth);
  30676. ret = wc_ImportSakkePublicKey(key, pubData, sizeof(pubData), 0);
  30677. if (ret != 0)
  30678. return -10334;
  30679. ret = wc_SetSakkeIdentity(key, id, idSz);
  30680. if (ret != 0)
  30681. return -10335;
  30682. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  30683. auth, &authSz);
  30684. if (ret != 0)
  30685. return -10336;
  30686. if (authSz != 257)
  30687. return -10337;
  30688. if (XMEMCMP(ssv, encSsv, ssvSz) != 0)
  30689. return -10338;
  30690. if (XMEMCMP(auth, expAuth, authSz) != 0)
  30691. return -10339;
  30692. return 0;
  30693. }
  30694. static int sakke_make_key_test(SakkeKey* priv, SakkeKey* pub, SakkeKey* key,
  30695. WC_RNG* rng, ecc_point* rsk)
  30696. {
  30697. int ret;
  30698. byte data[440];
  30699. byte pubData[257];
  30700. word32 sz;
  30701. char mail[] = "test@wolfssl.com";
  30702. byte* id = (byte*)mail;
  30703. word32 idSz = (word32)XSTRLEN(mail);
  30704. int valid;
  30705. ecc_point* pubKey = rsk;
  30706. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  30707. if (ret != 0)
  30708. return -10339;
  30709. ret = wc_MakeSakkeKey(priv, rng);
  30710. if (ret != 0)
  30711. return -10340;
  30712. ret = wc_ExportSakkeKey(priv, NULL, &sz);
  30713. if (ret != LENGTH_ONLY_E)
  30714. return -10341;
  30715. if (sz != 384)
  30716. return -10342;
  30717. sz--;
  30718. ret = wc_ExportSakkeKey(priv, data, &sz);
  30719. if (ret == 0)
  30720. return -10343;
  30721. sz++;
  30722. ret = wc_ExportSakkeKey(priv, data, &sz);
  30723. if (ret != 0)
  30724. return -10344;
  30725. if (sz != 384)
  30726. return -10345;
  30727. ret = wc_ImportSakkeKey(key, data, sz - 1);
  30728. if (ret == 0)
  30729. return -10346;
  30730. ret = wc_ImportSakkeKey(key, data, sz);
  30731. if (ret != 0)
  30732. return -10347;
  30733. wc_FreeSakkeKey(key);
  30734. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  30735. if (ret != 0)
  30736. return -10348;
  30737. ret = wc_ExportSakkePrivateKey(priv, NULL, &sz);
  30738. if (ret != LENGTH_ONLY_E)
  30739. return -10349;
  30740. if (sz != 128)
  30741. return -10350;
  30742. sz--;
  30743. ret = wc_ExportSakkePrivateKey(priv, data, &sz);
  30744. if (ret == 0)
  30745. return -10351;
  30746. sz++;
  30747. ret = wc_ExportSakkePrivateKey(priv, data, &sz);
  30748. if (ret != 0)
  30749. return -10352;
  30750. if (sz != 128)
  30751. return -10353;
  30752. ret = wc_ImportSakkePrivateKey(key, data, sz - 1);
  30753. if (ret == 0)
  30754. return -10354;
  30755. ret = wc_ImportSakkePrivateKey(key, data, sz);
  30756. if (ret != 0)
  30757. return -10355;
  30758. ret = wc_MakeSakkePublicKey(key, pubKey);
  30759. if (ret != 0)
  30760. return -10356;
  30761. ret = wc_ExportSakkePublicKey(priv, NULL, &sz, 1);
  30762. if (ret != LENGTH_ONLY_E)
  30763. return -10357;
  30764. if (sz != 256)
  30765. return -10358;
  30766. sz--;
  30767. ret = wc_ExportSakkePublicKey(priv, data, &sz, 1);
  30768. if (ret == 0)
  30769. return -10359;
  30770. sz++;
  30771. ret = wc_ExportSakkePublicKey(priv, data, &sz, 1);
  30772. if (ret != 0)
  30773. return -10360;
  30774. if (sz != 256)
  30775. return -10361;
  30776. ret = wc_ImportSakkePublicKey(pub, data, sz - 1, 1);
  30777. if (ret == 0)
  30778. return -10362;
  30779. ret = wc_ImportSakkePublicKey(pub, data, sz, 1);
  30780. if (ret != 0)
  30781. return -10363;
  30782. ret = wc_ExportSakkePublicKey(pub, pubData, &sz, 1);
  30783. if (ret != 0)
  30784. return -10364;
  30785. if (sz != 256)
  30786. return -10365;
  30787. if (XMEMCMP(data, pubData, sz) != 0)
  30788. return -10366;
  30789. ret = wc_MakeSakkeRsk(priv, id, idSz, rsk);
  30790. if (ret != 0)
  30791. return -10367;
  30792. ret = wc_ValidateSakkeRsk(priv, id, idSz, rsk, &valid);
  30793. if (ret != 0)
  30794. return -10368;
  30795. if (valid != 1)
  30796. return -10369;
  30797. ret = wc_ValidateSakkeRsk(pub, id, idSz, rsk, &valid);
  30798. if (ret != 0)
  30799. return -10370;
  30800. if (valid != 1)
  30801. return -10371;
  30802. sz = sizeof(data);
  30803. ret = wc_EncodeSakkeRsk(priv, rsk, data, &sz, 1);
  30804. if (ret != 0)
  30805. return -10372;
  30806. if (sz != 256)
  30807. return -10373;
  30808. ret = wc_DecodeSakkeRsk(priv, data, sz, rsk);
  30809. if (ret != 0)
  30810. return -10374;
  30811. sz = sizeof(pubData);
  30812. ret = wc_EncodeSakkeRsk(priv, rsk, pubData, &sz, 0);
  30813. if (ret != 0)
  30814. return -10375;
  30815. if (sz != sizeof(pubData))
  30816. return -10376;
  30817. ret = wc_DecodeSakkeRsk(priv, pubData, sz, rsk);
  30818. if (ret != 0)
  30819. return -10377;
  30820. wc_FreeSakkeKey(key);
  30821. return 0;
  30822. }
  30823. static int sakke_op_test(SakkeKey* priv, SakkeKey* pub, WC_RNG* rng,
  30824. ecc_point* rsk)
  30825. {
  30826. int ret;
  30827. byte ssv[16];
  30828. word16 ssvSz;
  30829. byte auth[257];
  30830. word16 authSz;
  30831. char mail[] = "test@wolfssl.com";
  30832. byte* id = (byte*)mail;
  30833. word32 idSz = (word32)XSTRLEN(mail);
  30834. byte pointI[256];
  30835. word32 sz;
  30836. ret = wc_GenerateSakkeSSV(pub, rng, NULL, &ssvSz);
  30837. if (ret != LENGTH_ONLY_E)
  30838. return -10375;
  30839. if (ssvSz != 16)
  30840. return -10376;
  30841. ssvSz += 128;
  30842. ret = wc_GenerateSakkeSSV(pub, rng, ssv, &ssvSz);
  30843. if (ret == 0)
  30844. return -10377;
  30845. ssvSz -= 128;
  30846. ret = wc_GenerateSakkeSSV(pub, rng, ssv, &ssvSz);
  30847. if (ret != 0)
  30848. return -10378;
  30849. if (ssvSz != 16)
  30850. return -10379;
  30851. ret = wc_GetSakkeAuthSize(pub, &authSz);
  30852. if (ret != 0)
  30853. return -10380;
  30854. ret = wc_SetSakkeIdentity(pub, id, idSz);
  30855. if (ret != 0)
  30856. return -10380;
  30857. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  30858. NULL, &authSz);
  30859. if (ret != LENGTH_ONLY_E)
  30860. return -10381;
  30861. if (authSz != 257)
  30862. return -10382;
  30863. authSz--;
  30864. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  30865. auth, &authSz);
  30866. if (ret == 0)
  30867. return -10383;
  30868. authSz++;
  30869. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  30870. auth, &authSz);
  30871. if (ret != 0)
  30872. return -10384;
  30873. if (authSz != 257)
  30874. return -10385;
  30875. ret = wc_GetSakkePointI(pub, NULL, &sz);
  30876. if (ret != LENGTH_ONLY_E)
  30877. return -10386;
  30878. if (sz != 256)
  30879. return -10387;
  30880. ret = wc_GetSakkePointI(pub, pointI, &sz);
  30881. if (ret != 0)
  30882. return -10388;
  30883. if (sz != 256)
  30884. return -10389;
  30885. /* Bogus identity - make it check and regenerate I. */
  30886. ret = wc_MakeSakkePointI(pub, ssv, ssvSz);
  30887. if (ret != 0)
  30888. return -10391;
  30889. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  30890. auth, &authSz);
  30891. if (ret != 0)
  30892. return -10392;
  30893. if (authSz != 257)
  30894. return -10393;
  30895. ret = wc_SetSakkeRsk(priv, rsk, NULL, 0);
  30896. if (ret != 0)
  30897. return -10392;
  30898. ret = wc_SetSakkeIdentity(priv, id, idSz);
  30899. if (ret != 0)
  30900. return -10392;
  30901. authSz--;
  30902. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  30903. authSz);
  30904. if (ret == 0)
  30905. return -10394;
  30906. authSz++;
  30907. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  30908. authSz);
  30909. if (ret != 0)
  30910. return -10395;
  30911. ssv[0] ^= 0x80;
  30912. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  30913. authSz);
  30914. if (ret != SAKKE_VERIFY_FAIL_E)
  30915. return -10396;
  30916. ssv[0] ^= 0x80;
  30917. /* Bogus identity - make it check and regenerate I. */
  30918. ret = wc_MakeSakkePointI(pub, ssv, idSz);
  30919. if (ret != 0)
  30920. return -10397;
  30921. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  30922. authSz);
  30923. if (ret != 0)
  30924. return -10398;
  30925. return 0;
  30926. }
  30927. int sakke_test(void)
  30928. {
  30929. int ret = 0;
  30930. WC_RNG rng;
  30931. int rng_inited = 0;
  30932. SakkeKey* priv = NULL;
  30933. SakkeKey* pub = NULL;
  30934. SakkeKey* key = NULL;
  30935. ecc_point* rsk = NULL;
  30936. priv = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  30937. DYNAMIC_TYPE_TMP_BUFFER);
  30938. if (priv == NULL)
  30939. ret = -10404;
  30940. else
  30941. XMEMSET(priv, 0, sizeof(*priv));
  30942. if (ret == 0) {
  30943. pub = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  30944. DYNAMIC_TYPE_TMP_BUFFER);
  30945. if (pub == NULL)
  30946. ret = -10405;
  30947. else
  30948. XMEMSET(pub, 0, sizeof(*pub));
  30949. }
  30950. if (ret == 0) {
  30951. key = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  30952. DYNAMIC_TYPE_TMP_BUFFER);
  30953. if (key == NULL)
  30954. ret = -10406;
  30955. else
  30956. XMEMSET(key, 0, sizeof(*key));
  30957. }
  30958. if (ret == 0) {
  30959. #ifndef HAVE_FIPS
  30960. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  30961. #else
  30962. ret = wc_InitRng(&rng);
  30963. #endif
  30964. if (ret == 0)
  30965. rng_inited = 1;
  30966. else
  30967. ret = -10400;
  30968. }
  30969. if (ret == 0) {
  30970. rsk = wc_ecc_new_point();
  30971. if (rsk == NULL)
  30972. ret = -10401;
  30973. }
  30974. if (ret == 0) {
  30975. ret = wc_InitSakkeKey(pub, HEAP_HINT, INVALID_DEVID);
  30976. if (ret != 0)
  30977. ret = -10402;
  30978. }
  30979. if (ret == 0) {
  30980. ret = wc_InitSakkeKey(priv, HEAP_HINT, INVALID_DEVID);
  30981. if (ret != 0)
  30982. ret = -10403;
  30983. }
  30984. if (ret == 0) {
  30985. ret = sakke_api_test(&rng, key, rsk);
  30986. }
  30987. if (ret == 0) {
  30988. ret = sakke_kat_derive_test(pub, rsk);
  30989. }
  30990. if (ret == 0) {
  30991. ret = sakke_kat_encapsulate_test(pub);
  30992. }
  30993. if (ret == 0) {
  30994. ret = sakke_make_key_test(priv, pub, key, &rng, rsk);
  30995. }
  30996. if (ret == 0) {
  30997. ret = sakke_op_test(priv, pub, &rng, rsk);
  30998. }
  30999. if (rsk != NULL) {
  31000. wc_ecc_forcezero_point(rsk);
  31001. wc_ecc_del_point(rsk);
  31002. }
  31003. if (rng_inited)
  31004. wc_FreeRng(&rng);
  31005. if (key != NULL)
  31006. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31007. if (pub != NULL) {
  31008. wc_FreeSakkeKey(pub);
  31009. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31010. }
  31011. if (priv != NULL) {
  31012. wc_FreeSakkeKey(priv);
  31013. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31014. }
  31015. return ret;
  31016. }
  31017. #endif /* WOLFCRYPT_HAVE_SAKKE */
  31018. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  31019. typedef struct CMAC_Test_Case {
  31020. int type;
  31021. int partial;
  31022. const byte* m;
  31023. word32 mSz;
  31024. const byte* k;
  31025. word32 kSz;
  31026. const byte* t;
  31027. word32 tSz;
  31028. } CMAC_Test_Case;
  31029. WOLFSSL_TEST_SUBROUTINE int cmac_test(void)
  31030. {
  31031. #ifdef WOLFSSL_AES_128
  31032. WOLFSSL_SMALL_STACK_STATIC const byte k128[] =
  31033. {
  31034. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  31035. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  31036. };
  31037. #define KLEN_128 (sizeof(k128))
  31038. #endif
  31039. #ifdef WOLFSSL_AES_192
  31040. WOLFSSL_SMALL_STACK_STATIC const byte k192[] =
  31041. {
  31042. 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52,
  31043. 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
  31044. 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b
  31045. };
  31046. #define KLEN_192 (sizeof(k192))
  31047. #endif
  31048. #ifdef WOLFSSL_AES_256
  31049. WOLFSSL_SMALL_STACK_STATIC const byte k256[] =
  31050. {
  31051. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  31052. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  31053. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  31054. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  31055. };
  31056. #define KLEN_256 (sizeof(k256))
  31057. #endif
  31058. WOLFSSL_SMALL_STACK_STATIC const byte m[] =
  31059. {
  31060. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  31061. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  31062. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  31063. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  31064. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
  31065. 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  31066. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
  31067. 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
  31068. };
  31069. #define MLEN_0 (0)
  31070. #define MLEN_128 (128/8)
  31071. #define MLEN_320 (320/8)
  31072. #define MLEN_319 (MLEN_320 - 1)
  31073. #define MLEN_512 (512/8)
  31074. #ifdef WOLFSSL_AES_128
  31075. WOLFSSL_SMALL_STACK_STATIC const byte t128_0[] =
  31076. {
  31077. 0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28,
  31078. 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46
  31079. };
  31080. WOLFSSL_SMALL_STACK_STATIC const byte t128_128[] =
  31081. {
  31082. 0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
  31083. 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c
  31084. };
  31085. WOLFSSL_SMALL_STACK_STATIC const byte t128_319[] =
  31086. {
  31087. 0x2c, 0x17, 0x84, 0x4c, 0x93, 0x1c, 0x07, 0x95,
  31088. 0x15, 0x92, 0x73, 0x0a, 0x34, 0xd0, 0xd9, 0xd2
  31089. };
  31090. WOLFSSL_SMALL_STACK_STATIC const byte t128_320[] =
  31091. {
  31092. 0xdf, 0xa6, 0x67, 0x47, 0xde, 0x9a, 0xe6, 0x30,
  31093. 0x30, 0xca, 0x32, 0x61, 0x14, 0x97, 0xc8, 0x27
  31094. };
  31095. WOLFSSL_SMALL_STACK_STATIC const byte t128_512[] =
  31096. {
  31097. 0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92,
  31098. 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe
  31099. };
  31100. #endif
  31101. #ifdef WOLFSSL_AES_192
  31102. WOLFSSL_SMALL_STACK_STATIC const byte t192_0[] =
  31103. {
  31104. 0xd1, 0x7d, 0xdf, 0x46, 0xad, 0xaa, 0xcd, 0xe5,
  31105. 0x31, 0xca, 0xc4, 0x83, 0xde, 0x7a, 0x93, 0x67
  31106. };
  31107. WOLFSSL_SMALL_STACK_STATIC const byte t192_128[] =
  31108. {
  31109. 0x9e, 0x99, 0xa7, 0xbf, 0x31, 0xe7, 0x10, 0x90,
  31110. 0x06, 0x62, 0xf6, 0x5e, 0x61, 0x7c, 0x51, 0x84
  31111. };
  31112. WOLFSSL_SMALL_STACK_STATIC const byte t192_320[] =
  31113. {
  31114. 0x8a, 0x1d, 0xe5, 0xbe, 0x2e, 0xb3, 0x1a, 0xad,
  31115. 0x08, 0x9a, 0x82, 0xe6, 0xee, 0x90, 0x8b, 0x0e
  31116. };
  31117. WOLFSSL_SMALL_STACK_STATIC const byte t192_512[] =
  31118. {
  31119. 0xa1, 0xd5, 0xdf, 0x0e, 0xed, 0x79, 0x0f, 0x79,
  31120. 0x4d, 0x77, 0x58, 0x96, 0x59, 0xf3, 0x9a, 0x11
  31121. };
  31122. #endif
  31123. #ifdef WOLFSSL_AES_256
  31124. WOLFSSL_SMALL_STACK_STATIC const byte t256_0[] =
  31125. {
  31126. 0x02, 0x89, 0x62, 0xf6, 0x1b, 0x7b, 0xf8, 0x9e,
  31127. 0xfc, 0x6b, 0x55, 0x1f, 0x46, 0x67, 0xd9, 0x83
  31128. };
  31129. WOLFSSL_SMALL_STACK_STATIC const byte t256_128[] =
  31130. {
  31131. 0x28, 0xa7, 0x02, 0x3f, 0x45, 0x2e, 0x8f, 0x82,
  31132. 0xbd, 0x4b, 0xf2, 0x8d, 0x8c, 0x37, 0xc3, 0x5c
  31133. };
  31134. WOLFSSL_SMALL_STACK_STATIC const byte t256_320[] =
  31135. {
  31136. 0xaa, 0xf3, 0xd8, 0xf1, 0xde, 0x56, 0x40, 0xc2,
  31137. 0x32, 0xf5, 0xb1, 0x69, 0xb9, 0xc9, 0x11, 0xe6
  31138. };
  31139. WOLFSSL_SMALL_STACK_STATIC const byte t256_512[] =
  31140. {
  31141. 0xe1, 0x99, 0x21, 0x90, 0x54, 0x9f, 0x6e, 0xd5,
  31142. 0x69, 0x6a, 0x2c, 0x05, 0x6c, 0x31, 0x54, 0x10
  31143. };
  31144. #endif
  31145. const CMAC_Test_Case testCases[] =
  31146. {
  31147. #ifdef WOLFSSL_AES_128
  31148. {WC_CMAC_AES, 0, m, MLEN_0, k128, KLEN_128, t128_0, AES_BLOCK_SIZE},
  31149. {WC_CMAC_AES, 0, m, MLEN_128, k128, KLEN_128, t128_128, AES_BLOCK_SIZE},
  31150. {WC_CMAC_AES, 0, m, MLEN_320, k128, KLEN_128, t128_320, AES_BLOCK_SIZE},
  31151. {WC_CMAC_AES, 0, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  31152. {WC_CMAC_AES, 5, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  31153. #endif
  31154. #ifdef WOLFSSL_AES_192
  31155. {WC_CMAC_AES, 0, m, MLEN_0, k192, KLEN_192, t192_0, AES_BLOCK_SIZE},
  31156. {WC_CMAC_AES, 0, m, MLEN_128, k192, KLEN_192, t192_128, AES_BLOCK_SIZE},
  31157. {WC_CMAC_AES, 0, m, MLEN_320, k192, KLEN_192, t192_320, AES_BLOCK_SIZE},
  31158. {WC_CMAC_AES, 0, m, MLEN_512, k192, KLEN_192, t192_512, AES_BLOCK_SIZE},
  31159. #endif
  31160. #ifdef WOLFSSL_AES_256
  31161. {WC_CMAC_AES, 0, m, MLEN_0, k256, KLEN_256, t256_0, AES_BLOCK_SIZE},
  31162. {WC_CMAC_AES, 0, m, MLEN_128, k256, KLEN_256, t256_128, AES_BLOCK_SIZE},
  31163. {WC_CMAC_AES, 0, m, MLEN_320, k256, KLEN_256, t256_320, AES_BLOCK_SIZE},
  31164. {WC_CMAC_AES, 0, m, MLEN_512, k256, KLEN_256, t256_512, AES_BLOCK_SIZE},
  31165. #endif
  31166. #ifdef WOLFSSL_AES_128
  31167. {WC_CMAC_AES, 0, m, MLEN_319, k128, KLEN_128, t128_319, AES_BLOCK_SIZE}
  31168. #endif
  31169. };
  31170. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  31171. Cmac *cmac;
  31172. #else
  31173. Cmac cmac[1];
  31174. #endif
  31175. byte tag[AES_BLOCK_SIZE];
  31176. const CMAC_Test_Case* tc;
  31177. word32 i, tagSz;
  31178. int ret;
  31179. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  31180. if ((cmac = (Cmac *)XMALLOC(sizeof *cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC)) == NULL)
  31181. ERROR_OUT(-12009, out);
  31182. #endif
  31183. for (i = 0, tc = testCases;
  31184. i < sizeof(testCases)/sizeof(CMAC_Test_Case);
  31185. i++, tc++) {
  31186. XMEMSET(tag, 0, sizeof(tag));
  31187. tagSz = AES_BLOCK_SIZE;
  31188. #if !defined(HAVE_FIPS) || \
  31189. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)
  31190. if (wc_InitCmac_ex(cmac, tc->k, tc->kSz, tc->type, NULL, HEAP_HINT, devId) != 0)
  31191. #else
  31192. if (wc_InitCmac(cmac, tc->k, tc->kSz, tc->type, NULL) != 0)
  31193. #endif
  31194. {
  31195. ERROR_OUT(-12000, out);
  31196. }
  31197. if (tc->partial) {
  31198. if (wc_CmacUpdate(cmac, tc->m,
  31199. tc->mSz/2 - tc->partial) != 0)
  31200. ERROR_OUT(-12001, out);
  31201. if (wc_CmacUpdate(cmac, tc->m + tc->mSz/2 - tc->partial,
  31202. tc->mSz/2 + tc->partial) != 0)
  31203. ERROR_OUT(-12002, out);
  31204. }
  31205. else {
  31206. if (wc_CmacUpdate(cmac, tc->m, tc->mSz) != 0)
  31207. ERROR_OUT(-12003, out);
  31208. }
  31209. if (wc_CmacFinal(cmac, tag, &tagSz) != 0)
  31210. ERROR_OUT(-12004, out);
  31211. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  31212. ERROR_OUT(-12005, out);
  31213. XMEMSET(tag, 0, sizeof(tag));
  31214. tagSz = sizeof(tag);
  31215. if (wc_AesCmacGenerate(tag, &tagSz, tc->m, tc->mSz,
  31216. tc->k, tc->kSz) != 0)
  31217. ERROR_OUT(-12006, out);
  31218. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  31219. ERROR_OUT(-12007, out);
  31220. if (wc_AesCmacVerify(tc->t, tc->tSz, tc->m, tc->mSz,
  31221. tc->k, tc->kSz) != 0)
  31222. ERROR_OUT(-12008, out);
  31223. }
  31224. ret = 0;
  31225. out:
  31226. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  31227. if (cmac)
  31228. XFREE(cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC);
  31229. #endif
  31230. return ret;
  31231. }
  31232. #endif /* NO_AES && WOLFSSL_CMAC */
  31233. #if defined(WOLFSSL_SIPHASH)
  31234. #if WOLFSSL_SIPHASH_CROUNDS == 2 && WOLFSSL_SIPHASH_DROUNDS == 4
  31235. /* Test vectors from:
  31236. * https://github.com/veorq/SipHash/blob/master/vectors.h
  31237. */
  31238. static const unsigned char siphash_key[SIPHASH_KEY_SIZE] = {
  31239. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  31240. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  31241. };
  31242. static const unsigned char siphash_msg[64] = {
  31243. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  31244. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  31245. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  31246. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  31247. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  31248. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  31249. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  31250. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f
  31251. };
  31252. static const unsigned char siphash_r8[64][SIPHASH_MAC_SIZE_8] = {
  31253. { 0x31, 0x0e, 0x0e, 0xdd, 0x47, 0xdb, 0x6f, 0x72, },
  31254. { 0xfd, 0x67, 0xdc, 0x93, 0xc5, 0x39, 0xf8, 0x74, },
  31255. { 0x5a, 0x4f, 0xa9, 0xd9, 0x09, 0x80, 0x6c, 0x0d, },
  31256. { 0x2d, 0x7e, 0xfb, 0xd7, 0x96, 0x66, 0x67, 0x85, },
  31257. { 0xb7, 0x87, 0x71, 0x27, 0xe0, 0x94, 0x27, 0xcf, },
  31258. { 0x8d, 0xa6, 0x99, 0xcd, 0x64, 0x55, 0x76, 0x18, },
  31259. { 0xce, 0xe3, 0xfe, 0x58, 0x6e, 0x46, 0xc9, 0xcb, },
  31260. { 0x37, 0xd1, 0x01, 0x8b, 0xf5, 0x00, 0x02, 0xab, },
  31261. { 0x62, 0x24, 0x93, 0x9a, 0x79, 0xf5, 0xf5, 0x93, },
  31262. { 0xb0, 0xe4, 0xa9, 0x0b, 0xdf, 0x82, 0x00, 0x9e, },
  31263. { 0xf3, 0xb9, 0xdd, 0x94, 0xc5, 0xbb, 0x5d, 0x7a, },
  31264. { 0xa7, 0xad, 0x6b, 0x22, 0x46, 0x2f, 0xb3, 0xf4, },
  31265. { 0xfb, 0xe5, 0x0e, 0x86, 0xbc, 0x8f, 0x1e, 0x75, },
  31266. { 0x90, 0x3d, 0x84, 0xc0, 0x27, 0x56, 0xea, 0x14, },
  31267. { 0xee, 0xf2, 0x7a, 0x8e, 0x90, 0xca, 0x23, 0xf7, },
  31268. { 0xe5, 0x45, 0xbe, 0x49, 0x61, 0xca, 0x29, 0xa1, },
  31269. { 0xdb, 0x9b, 0xc2, 0x57, 0x7f, 0xcc, 0x2a, 0x3f, },
  31270. { 0x94, 0x47, 0xbe, 0x2c, 0xf5, 0xe9, 0x9a, 0x69, },
  31271. { 0x9c, 0xd3, 0x8d, 0x96, 0xf0, 0xb3, 0xc1, 0x4b, },
  31272. { 0xbd, 0x61, 0x79, 0xa7, 0x1d, 0xc9, 0x6d, 0xbb, },
  31273. { 0x98, 0xee, 0xa2, 0x1a, 0xf2, 0x5c, 0xd6, 0xbe, },
  31274. { 0xc7, 0x67, 0x3b, 0x2e, 0xb0, 0xcb, 0xf2, 0xd0, },
  31275. { 0x88, 0x3e, 0xa3, 0xe3, 0x95, 0x67, 0x53, 0x93, },
  31276. { 0xc8, 0xce, 0x5c, 0xcd, 0x8c, 0x03, 0x0c, 0xa8, },
  31277. { 0x94, 0xaf, 0x49, 0xf6, 0xc6, 0x50, 0xad, 0xb8, },
  31278. { 0xea, 0xb8, 0x85, 0x8a, 0xde, 0x92, 0xe1, 0xbc, },
  31279. { 0xf3, 0x15, 0xbb, 0x5b, 0xb8, 0x35, 0xd8, 0x17, },
  31280. { 0xad, 0xcf, 0x6b, 0x07, 0x63, 0x61, 0x2e, 0x2f, },
  31281. { 0xa5, 0xc9, 0x1d, 0xa7, 0xac, 0xaa, 0x4d, 0xde, },
  31282. { 0x71, 0x65, 0x95, 0x87, 0x66, 0x50, 0xa2, 0xa6, },
  31283. { 0x28, 0xef, 0x49, 0x5c, 0x53, 0xa3, 0x87, 0xad, },
  31284. { 0x42, 0xc3, 0x41, 0xd8, 0xfa, 0x92, 0xd8, 0x32, },
  31285. { 0xce, 0x7c, 0xf2, 0x72, 0x2f, 0x51, 0x27, 0x71, },
  31286. { 0xe3, 0x78, 0x59, 0xf9, 0x46, 0x23, 0xf3, 0xa7, },
  31287. { 0x38, 0x12, 0x05, 0xbb, 0x1a, 0xb0, 0xe0, 0x12, },
  31288. { 0xae, 0x97, 0xa1, 0x0f, 0xd4, 0x34, 0xe0, 0x15, },
  31289. { 0xb4, 0xa3, 0x15, 0x08, 0xbe, 0xff, 0x4d, 0x31, },
  31290. { 0x81, 0x39, 0x62, 0x29, 0xf0, 0x90, 0x79, 0x02, },
  31291. { 0x4d, 0x0c, 0xf4, 0x9e, 0xe5, 0xd4, 0xdc, 0xca, },
  31292. { 0x5c, 0x73, 0x33, 0x6a, 0x76, 0xd8, 0xbf, 0x9a, },
  31293. { 0xd0, 0xa7, 0x04, 0x53, 0x6b, 0xa9, 0x3e, 0x0e, },
  31294. { 0x92, 0x59, 0x58, 0xfc, 0xd6, 0x42, 0x0c, 0xad, },
  31295. { 0xa9, 0x15, 0xc2, 0x9b, 0xc8, 0x06, 0x73, 0x18, },
  31296. { 0x95, 0x2b, 0x79, 0xf3, 0xbc, 0x0a, 0xa6, 0xd4, },
  31297. { 0xf2, 0x1d, 0xf2, 0xe4, 0x1d, 0x45, 0x35, 0xf9, },
  31298. { 0x87, 0x57, 0x75, 0x19, 0x04, 0x8f, 0x53, 0xa9, },
  31299. { 0x10, 0xa5, 0x6c, 0xf5, 0xdf, 0xcd, 0x9a, 0xdb, },
  31300. { 0xeb, 0x75, 0x09, 0x5c, 0xcd, 0x98, 0x6c, 0xd0, },
  31301. { 0x51, 0xa9, 0xcb, 0x9e, 0xcb, 0xa3, 0x12, 0xe6, },
  31302. { 0x96, 0xaf, 0xad, 0xfc, 0x2c, 0xe6, 0x66, 0xc7, },
  31303. { 0x72, 0xfe, 0x52, 0x97, 0x5a, 0x43, 0x64, 0xee, },
  31304. { 0x5a, 0x16, 0x45, 0xb2, 0x76, 0xd5, 0x92, 0xa1, },
  31305. { 0xb2, 0x74, 0xcb, 0x8e, 0xbf, 0x87, 0x87, 0x0a, },
  31306. { 0x6f, 0x9b, 0xb4, 0x20, 0x3d, 0xe7, 0xb3, 0x81, },
  31307. { 0xea, 0xec, 0xb2, 0xa3, 0x0b, 0x22, 0xa8, 0x7f, },
  31308. { 0x99, 0x24, 0xa4, 0x3c, 0xc1, 0x31, 0x57, 0x24, },
  31309. { 0xbd, 0x83, 0x8d, 0x3a, 0xaf, 0xbf, 0x8d, 0xb7, },
  31310. { 0x0b, 0x1a, 0x2a, 0x32, 0x65, 0xd5, 0x1a, 0xea, },
  31311. { 0x13, 0x50, 0x79, 0xa3, 0x23, 0x1c, 0xe6, 0x60, },
  31312. { 0x93, 0x2b, 0x28, 0x46, 0xe4, 0xd7, 0x06, 0x66, },
  31313. { 0xe1, 0x91, 0x5f, 0x5c, 0xb1, 0xec, 0xa4, 0x6c, },
  31314. { 0xf3, 0x25, 0x96, 0x5c, 0xa1, 0x6d, 0x62, 0x9f, },
  31315. { 0x57, 0x5f, 0xf2, 0x8e, 0x60, 0x38, 0x1b, 0xe5, },
  31316. { 0x72, 0x45, 0x06, 0xeb, 0x4c, 0x32, 0x8a, 0x95, },
  31317. };
  31318. static const unsigned char siphash_r16[64][SIPHASH_MAC_SIZE_16] = {
  31319. { 0xa3, 0x81, 0x7f, 0x04, 0xba, 0x25, 0xa8, 0xe6,
  31320. 0x6d, 0xf6, 0x72, 0x14, 0xc7, 0x55, 0x02, 0x93, },
  31321. { 0xda, 0x87, 0xc1, 0xd8, 0x6b, 0x99, 0xaf, 0x44,
  31322. 0x34, 0x76, 0x59, 0x11, 0x9b, 0x22, 0xfc, 0x45, },
  31323. { 0x81, 0x77, 0x22, 0x8d, 0xa4, 0xa4, 0x5d, 0xc7,
  31324. 0xfc, 0xa3, 0x8b, 0xde, 0xf6, 0x0a, 0xff, 0xe4, },
  31325. { 0x9c, 0x70, 0xb6, 0x0c, 0x52, 0x67, 0xa9, 0x4e,
  31326. 0x5f, 0x33, 0xb6, 0xb0, 0x29, 0x85, 0xed, 0x51, },
  31327. { 0xf8, 0x81, 0x64, 0xc1, 0x2d, 0x9c, 0x8f, 0xaf,
  31328. 0x7d, 0x0f, 0x6e, 0x7c, 0x7b, 0xcd, 0x55, 0x79, },
  31329. { 0x13, 0x68, 0x87, 0x59, 0x80, 0x77, 0x6f, 0x88,
  31330. 0x54, 0x52, 0x7a, 0x07, 0x69, 0x0e, 0x96, 0x27, },
  31331. { 0x14, 0xee, 0xca, 0x33, 0x8b, 0x20, 0x86, 0x13,
  31332. 0x48, 0x5e, 0xa0, 0x30, 0x8f, 0xd7, 0xa1, 0x5e, },
  31333. { 0xa1, 0xf1, 0xeb, 0xbe, 0xd8, 0xdb, 0xc1, 0x53,
  31334. 0xc0, 0xb8, 0x4a, 0xa6, 0x1f, 0xf0, 0x82, 0x39, },
  31335. { 0x3b, 0x62, 0xa9, 0xba, 0x62, 0x58, 0xf5, 0x61,
  31336. 0x0f, 0x83, 0xe2, 0x64, 0xf3, 0x14, 0x97, 0xb4, },
  31337. { 0x26, 0x44, 0x99, 0x06, 0x0a, 0xd9, 0xba, 0xab,
  31338. 0xc4, 0x7f, 0x8b, 0x02, 0xbb, 0x6d, 0x71, 0xed, },
  31339. { 0x00, 0x11, 0x0d, 0xc3, 0x78, 0x14, 0x69, 0x56,
  31340. 0xc9, 0x54, 0x47, 0xd3, 0xf3, 0xd0, 0xfb, 0xba, },
  31341. { 0x01, 0x51, 0xc5, 0x68, 0x38, 0x6b, 0x66, 0x77,
  31342. 0xa2, 0xb4, 0xdc, 0x6f, 0x81, 0xe5, 0xdc, 0x18, },
  31343. { 0xd6, 0x26, 0xb2, 0x66, 0x90, 0x5e, 0xf3, 0x58,
  31344. 0x82, 0x63, 0x4d, 0xf6, 0x85, 0x32, 0xc1, 0x25, },
  31345. { 0x98, 0x69, 0xe2, 0x47, 0xe9, 0xc0, 0x8b, 0x10,
  31346. 0xd0, 0x29, 0x93, 0x4f, 0xc4, 0xb9, 0x52, 0xf7, },
  31347. { 0x31, 0xfc, 0xef, 0xac, 0x66, 0xd7, 0xde, 0x9c,
  31348. 0x7e, 0xc7, 0x48, 0x5f, 0xe4, 0x49, 0x49, 0x02, },
  31349. { 0x54, 0x93, 0xe9, 0x99, 0x33, 0xb0, 0xa8, 0x11,
  31350. 0x7e, 0x08, 0xec, 0x0f, 0x97, 0xcf, 0xc3, 0xd9, },
  31351. { 0x6e, 0xe2, 0xa4, 0xca, 0x67, 0xb0, 0x54, 0xbb,
  31352. 0xfd, 0x33, 0x15, 0xbf, 0x85, 0x23, 0x05, 0x77, },
  31353. { 0x47, 0x3d, 0x06, 0xe8, 0x73, 0x8d, 0xb8, 0x98,
  31354. 0x54, 0xc0, 0x66, 0xc4, 0x7a, 0xe4, 0x77, 0x40, },
  31355. { 0xa4, 0x26, 0xe5, 0xe4, 0x23, 0xbf, 0x48, 0x85,
  31356. 0x29, 0x4d, 0xa4, 0x81, 0xfe, 0xae, 0xf7, 0x23, },
  31357. { 0x78, 0x01, 0x77, 0x31, 0xcf, 0x65, 0xfa, 0xb0,
  31358. 0x74, 0xd5, 0x20, 0x89, 0x52, 0x51, 0x2e, 0xb1, },
  31359. { 0x9e, 0x25, 0xfc, 0x83, 0x3f, 0x22, 0x90, 0x73,
  31360. 0x3e, 0x93, 0x44, 0xa5, 0xe8, 0x38, 0x39, 0xeb, },
  31361. { 0x56, 0x8e, 0x49, 0x5a, 0xbe, 0x52, 0x5a, 0x21,
  31362. 0x8a, 0x22, 0x14, 0xcd, 0x3e, 0x07, 0x1d, 0x12, },
  31363. { 0x4a, 0x29, 0xb5, 0x45, 0x52, 0xd1, 0x6b, 0x9a,
  31364. 0x46, 0x9c, 0x10, 0x52, 0x8e, 0xff, 0x0a, 0xae, },
  31365. { 0xc9, 0xd1, 0x84, 0xdd, 0xd5, 0xa9, 0xf5, 0xe0,
  31366. 0xcf, 0x8c, 0xe2, 0x9a, 0x9a, 0xbf, 0x69, 0x1c, },
  31367. { 0x2d, 0xb4, 0x79, 0xae, 0x78, 0xbd, 0x50, 0xd8,
  31368. 0x88, 0x2a, 0x8a, 0x17, 0x8a, 0x61, 0x32, 0xad, },
  31369. { 0x8e, 0xce, 0x5f, 0x04, 0x2d, 0x5e, 0x44, 0x7b,
  31370. 0x50, 0x51, 0xb9, 0xea, 0xcb, 0x8d, 0x8f, 0x6f, },
  31371. { 0x9c, 0x0b, 0x53, 0xb4, 0xb3, 0xc3, 0x07, 0xe8,
  31372. 0x7e, 0xae, 0xe0, 0x86, 0x78, 0x14, 0x1f, 0x66, },
  31373. { 0xab, 0xf2, 0x48, 0xaf, 0x69, 0xa6, 0xea, 0xe4,
  31374. 0xbf, 0xd3, 0xeb, 0x2f, 0x12, 0x9e, 0xeb, 0x94, },
  31375. { 0x06, 0x64, 0xda, 0x16, 0x68, 0x57, 0x4b, 0x88,
  31376. 0xb9, 0x35, 0xf3, 0x02, 0x73, 0x58, 0xae, 0xf4, },
  31377. { 0xaa, 0x4b, 0x9d, 0xc4, 0xbf, 0x33, 0x7d, 0xe9,
  31378. 0x0c, 0xd4, 0xfd, 0x3c, 0x46, 0x7c, 0x6a, 0xb7, },
  31379. { 0xea, 0x5c, 0x7f, 0x47, 0x1f, 0xaf, 0x6b, 0xde,
  31380. 0x2b, 0x1a, 0xd7, 0xd4, 0x68, 0x6d, 0x22, 0x87, },
  31381. { 0x29, 0x39, 0xb0, 0x18, 0x32, 0x23, 0xfa, 0xfc,
  31382. 0x17, 0x23, 0xde, 0x4f, 0x52, 0xc4, 0x3d, 0x35, },
  31383. { 0x7c, 0x39, 0x56, 0xca, 0x5e, 0xea, 0xfc, 0x3e,
  31384. 0x36, 0x3e, 0x9d, 0x55, 0x65, 0x46, 0xeb, 0x68, },
  31385. { 0x77, 0xc6, 0x07, 0x71, 0x46, 0xf0, 0x1c, 0x32,
  31386. 0xb6, 0xb6, 0x9d, 0x5f, 0x4e, 0xa9, 0xff, 0xcf, },
  31387. { 0x37, 0xa6, 0x98, 0x6c, 0xb8, 0x84, 0x7e, 0xdf,
  31388. 0x09, 0x25, 0xf0, 0xf1, 0x30, 0x9b, 0x54, 0xde, },
  31389. { 0xa7, 0x05, 0xf0, 0xe6, 0x9d, 0xa9, 0xa8, 0xf9,
  31390. 0x07, 0x24, 0x1a, 0x2e, 0x92, 0x3c, 0x8c, 0xc8, },
  31391. { 0x3d, 0xc4, 0x7d, 0x1f, 0x29, 0xc4, 0x48, 0x46,
  31392. 0x1e, 0x9e, 0x76, 0xed, 0x90, 0x4f, 0x67, 0x11, },
  31393. { 0x0d, 0x62, 0xbf, 0x01, 0xe6, 0xfc, 0x0e, 0x1a,
  31394. 0x0d, 0x3c, 0x47, 0x51, 0xc5, 0xd3, 0x69, 0x2b, },
  31395. { 0x8c, 0x03, 0x46, 0x8b, 0xca, 0x7c, 0x66, 0x9e,
  31396. 0xe4, 0xfd, 0x5e, 0x08, 0x4b, 0xbe, 0xe7, 0xb5, },
  31397. { 0x52, 0x8a, 0x5b, 0xb9, 0x3b, 0xaf, 0x2c, 0x9c,
  31398. 0x44, 0x73, 0xcc, 0xe5, 0xd0, 0xd2, 0x2b, 0xd9, },
  31399. { 0xdf, 0x6a, 0x30, 0x1e, 0x95, 0xc9, 0x5d, 0xad,
  31400. 0x97, 0xae, 0x0c, 0xc8, 0xc6, 0x91, 0x3b, 0xd8, },
  31401. { 0x80, 0x11, 0x89, 0x90, 0x2c, 0x85, 0x7f, 0x39,
  31402. 0xe7, 0x35, 0x91, 0x28, 0x5e, 0x70, 0xb6, 0xdb, },
  31403. { 0xe6, 0x17, 0x34, 0x6a, 0xc9, 0xc2, 0x31, 0xbb,
  31404. 0x36, 0x50, 0xae, 0x34, 0xcc, 0xca, 0x0c, 0x5b, },
  31405. { 0x27, 0xd9, 0x34, 0x37, 0xef, 0xb7, 0x21, 0xaa,
  31406. 0x40, 0x18, 0x21, 0xdc, 0xec, 0x5a, 0xdf, 0x89, },
  31407. { 0x89, 0x23, 0x7d, 0x9d, 0xed, 0x9c, 0x5e, 0x78,
  31408. 0xd8, 0xb1, 0xc9, 0xb1, 0x66, 0xcc, 0x73, 0x42, },
  31409. { 0x4a, 0x6d, 0x80, 0x91, 0xbf, 0x5e, 0x7d, 0x65,
  31410. 0x11, 0x89, 0xfa, 0x94, 0xa2, 0x50, 0xb1, 0x4c, },
  31411. { 0x0e, 0x33, 0xf9, 0x60, 0x55, 0xe7, 0xae, 0x89,
  31412. 0x3f, 0xfc, 0x0e, 0x3d, 0xcf, 0x49, 0x29, 0x02, },
  31413. { 0xe6, 0x1c, 0x43, 0x2b, 0x72, 0x0b, 0x19, 0xd1,
  31414. 0x8e, 0xc8, 0xd8, 0x4b, 0xdc, 0x63, 0x15, 0x1b, },
  31415. { 0xf7, 0xe5, 0xae, 0xf5, 0x49, 0xf7, 0x82, 0xcf,
  31416. 0x37, 0x90, 0x55, 0xa6, 0x08, 0x26, 0x9b, 0x16, },
  31417. { 0x43, 0x8d, 0x03, 0x0f, 0xd0, 0xb7, 0xa5, 0x4f,
  31418. 0xa8, 0x37, 0xf2, 0xad, 0x20, 0x1a, 0x64, 0x03, },
  31419. { 0xa5, 0x90, 0xd3, 0xee, 0x4f, 0xbf, 0x04, 0xe3,
  31420. 0x24, 0x7e, 0x0d, 0x27, 0xf2, 0x86, 0x42, 0x3f, },
  31421. { 0x5f, 0xe2, 0xc1, 0xa1, 0x72, 0xfe, 0x93, 0xc4,
  31422. 0xb1, 0x5c, 0xd3, 0x7c, 0xae, 0xf9, 0xf5, 0x38, },
  31423. { 0x2c, 0x97, 0x32, 0x5c, 0xbd, 0x06, 0xb3, 0x6e,
  31424. 0xb2, 0x13, 0x3d, 0xd0, 0x8b, 0x3a, 0x01, 0x7c, },
  31425. { 0x92, 0xc8, 0x14, 0x22, 0x7a, 0x6b, 0xca, 0x94,
  31426. 0x9f, 0xf0, 0x65, 0x9f, 0x00, 0x2a, 0xd3, 0x9e, },
  31427. { 0xdc, 0xe8, 0x50, 0x11, 0x0b, 0xd8, 0x32, 0x8c,
  31428. 0xfb, 0xd5, 0x08, 0x41, 0xd6, 0x91, 0x1d, 0x87, },
  31429. { 0x67, 0xf1, 0x49, 0x84, 0xc7, 0xda, 0x79, 0x12,
  31430. 0x48, 0xe3, 0x2b, 0xb5, 0x92, 0x25, 0x83, 0xda, },
  31431. { 0x19, 0x38, 0xf2, 0xcf, 0x72, 0xd5, 0x4e, 0xe9,
  31432. 0x7e, 0x94, 0x16, 0x6f, 0xa9, 0x1d, 0x2a, 0x36, },
  31433. { 0x74, 0x48, 0x1e, 0x96, 0x46, 0xed, 0x49, 0xfe,
  31434. 0x0f, 0x62, 0x24, 0x30, 0x16, 0x04, 0x69, 0x8e, },
  31435. { 0x57, 0xfc, 0xa5, 0xde, 0x98, 0xa9, 0xd6, 0xd8,
  31436. 0x00, 0x64, 0x38, 0xd0, 0x58, 0x3d, 0x8a, 0x1d, },
  31437. { 0x9f, 0xec, 0xde, 0x1c, 0xef, 0xdc, 0x1c, 0xbe,
  31438. 0xd4, 0x76, 0x36, 0x74, 0xd9, 0x57, 0x53, 0x59, },
  31439. { 0xe3, 0x04, 0x0c, 0x00, 0xeb, 0x28, 0xf1, 0x53,
  31440. 0x66, 0xca, 0x73, 0xcb, 0xd8, 0x72, 0xe7, 0x40, },
  31441. { 0x76, 0x97, 0x00, 0x9a, 0x6a, 0x83, 0x1d, 0xfe,
  31442. 0xcc, 0xa9, 0x1c, 0x59, 0x93, 0x67, 0x0f, 0x7a, },
  31443. { 0x58, 0x53, 0x54, 0x23, 0x21, 0xf5, 0x67, 0xa0,
  31444. 0x05, 0xd5, 0x47, 0xa4, 0xf0, 0x47, 0x59, 0xbd, },
  31445. { 0x51, 0x50, 0xd1, 0x77, 0x2f, 0x50, 0x83, 0x4a,
  31446. 0x50, 0x3e, 0x06, 0x9a, 0x97, 0x3f, 0xbd, 0x7c, },
  31447. };
  31448. #endif
  31449. WOLFSSL_TEST_SUBROUTINE int siphash_test(void)
  31450. {
  31451. int ret = 0;
  31452. int i;
  31453. #if WOLFSSL_SIPHASH_CROUNDS == 2 && WOLFSSL_SIPHASH_DROUNDS == 4
  31454. unsigned char res[SIPHASH_MAC_SIZE_16];
  31455. unsigned char tmp[SIPHASH_MAC_SIZE_8];
  31456. SipHash siphash;
  31457. for (i = 0; i < 64; i++) {
  31458. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  31459. if (ret != 0)
  31460. return -12100 - i;
  31461. ret = wc_SipHashUpdate(&siphash, siphash_msg, i);
  31462. if (ret != 0)
  31463. return -12200 - i;
  31464. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_8);
  31465. if (ret != 0)
  31466. return -12300 - i;
  31467. if (XMEMCMP(res, siphash_r8[i], SIPHASH_MAC_SIZE_8) != 0)
  31468. return -12400 - i;
  31469. ret = wc_SipHash(siphash_key, siphash_msg, i, res, SIPHASH_MAC_SIZE_8);
  31470. if (ret != 0)
  31471. return -12500 - i;
  31472. if (XMEMCMP(res, siphash_r8[i], SIPHASH_MAC_SIZE_8) != 0)
  31473. return -12600 - i;
  31474. }
  31475. for (i = 0; i < 64; i++) {
  31476. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_16);
  31477. if (ret != 0)
  31478. return -12700 - i;
  31479. ret = wc_SipHashUpdate(&siphash, siphash_msg, i);
  31480. if (ret != 0)
  31481. return -12800 - i;
  31482. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_16);
  31483. if (ret != 0)
  31484. return -12900 - i;
  31485. if (XMEMCMP(res, siphash_r16[i], SIPHASH_MAC_SIZE_16) != 0)
  31486. return -13000 - i;
  31487. ret = wc_SipHash(siphash_key, siphash_msg, i, res, SIPHASH_MAC_SIZE_16);
  31488. if (ret != 0)
  31489. return -13100 - i;
  31490. if (XMEMCMP(res, siphash_r16[i], SIPHASH_MAC_SIZE_16) != 0)
  31491. return -13200 - i;
  31492. }
  31493. #endif
  31494. /* Testing bad parameters. */
  31495. ret = wc_InitSipHash(NULL, NULL, SIPHASH_MAC_SIZE_8);
  31496. if (ret != BAD_FUNC_ARG)
  31497. return -13300;
  31498. ret = wc_InitSipHash(NULL, siphash_key, SIPHASH_MAC_SIZE_8);
  31499. if (ret != BAD_FUNC_ARG)
  31500. return -13301;
  31501. ret = wc_InitSipHash(&siphash, NULL, SIPHASH_MAC_SIZE_8);
  31502. if (ret != BAD_FUNC_ARG)
  31503. return -13302;
  31504. ret = wc_InitSipHash(&siphash, siphash_key, 7);
  31505. if (ret != BAD_FUNC_ARG)
  31506. return -13303;
  31507. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  31508. if (ret != 0)
  31509. return -13304;
  31510. ret = wc_SipHashUpdate(NULL, NULL, 0);
  31511. if (ret != BAD_FUNC_ARG)
  31512. return -13305;
  31513. ret = wc_SipHashUpdate(&siphash, NULL, 1);
  31514. if (ret != BAD_FUNC_ARG)
  31515. return -13306;
  31516. ret = wc_SipHashFinal(NULL, NULL, SIPHASH_MAC_SIZE_8);
  31517. if (ret != BAD_FUNC_ARG)
  31518. return -13307;
  31519. ret = wc_SipHashFinal(&siphash, NULL, SIPHASH_MAC_SIZE_8);
  31520. if (ret != BAD_FUNC_ARG)
  31521. return -13308;
  31522. ret = wc_SipHashFinal(NULL, res, SIPHASH_MAC_SIZE_8);
  31523. if (ret != BAD_FUNC_ARG)
  31524. return -13309;
  31525. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_16);
  31526. if (ret != BAD_FUNC_ARG)
  31527. return -13310;
  31528. ret = wc_SipHash(NULL, NULL, 0, NULL, SIPHASH_MAC_SIZE_16);
  31529. if (ret != BAD_FUNC_ARG)
  31530. return -13311;
  31531. ret = wc_SipHash(siphash_key, NULL, 0, NULL, SIPHASH_MAC_SIZE_16);
  31532. if (ret != BAD_FUNC_ARG)
  31533. return -13312;
  31534. ret = wc_SipHash(NULL, NULL, 0, res, SIPHASH_MAC_SIZE_16);
  31535. if (ret != BAD_FUNC_ARG)
  31536. return -13313;
  31537. ret = wc_SipHash(siphash_key, NULL, 0, res, 15);
  31538. if (ret != BAD_FUNC_ARG)
  31539. return -13314;
  31540. ret = wc_SipHash(siphash_key, NULL, 1, res, SIPHASH_MAC_SIZE_16);
  31541. if (ret != BAD_FUNC_ARG)
  31542. return -13315;
  31543. /* Test cache with multiple non blocksize bytes */
  31544. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  31545. if (ret != 0)
  31546. return -13316;
  31547. ret = wc_SipHashUpdate(&siphash, siphash_msg, 5);
  31548. if (ret != 0)
  31549. return -13317;
  31550. ret = wc_SipHashUpdate(&siphash, siphash_msg + 5, 4);
  31551. if (ret != 0)
  31552. return -13318;
  31553. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_8);
  31554. if (ret != 0)
  31555. return -13319;
  31556. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  31557. if (ret != 0)
  31558. return -13320;
  31559. ret = wc_SipHashUpdate(&siphash, siphash_msg, 9);
  31560. if (ret != 0)
  31561. return -13321;
  31562. ret = wc_SipHashFinal(&siphash, tmp, SIPHASH_MAC_SIZE_8);
  31563. if (ret != 0)
  31564. return -13322;
  31565. if (XMEMCMP(res, tmp, SIPHASH_MAC_SIZE_8) != 0)
  31566. return -13323;
  31567. return 0;
  31568. }
  31569. #endif /* WOLFSSL_SIPHASH */
  31570. #ifdef HAVE_LIBZ
  31571. static const byte sample_text[] =
  31572. "Biodiesel cupidatat marfa, cliche aute put a bird on it incididunt elit\n"
  31573. "polaroid. Sunt tattooed bespoke reprehenderit. Sint twee organic id\n"
  31574. "marfa. Commodo veniam ad esse gastropub. 3 wolf moon sartorial vero,\n"
  31575. "plaid delectus biodiesel squid +1 vice. Post-ironic keffiyeh leggings\n"
  31576. "selfies cray fap hoodie, forage anim. Carles cupidatat shoreditch, VHS\n"
  31577. "small batch meggings kogi dolore food truck bespoke gastropub.\n"
  31578. "\n"
  31579. "Terry richardson adipisicing actually typewriter tumblr, twee whatever\n"
  31580. "four loko you probably haven't heard of them high life. Messenger bag\n"
  31581. "whatever tattooed deep v mlkshk. Brooklyn pinterest assumenda chillwave\n"
  31582. "et, banksy ullamco messenger bag umami pariatur direct trade forage.\n"
  31583. "Typewriter culpa try-hard, pariatur sint brooklyn meggings. Gentrify\n"
  31584. "food truck next level, tousled irony non semiotics PBR ethical anim cred\n"
  31585. "readymade. Mumblecore brunch lomo odd future, portland organic terry\n"
  31586. "richardson elit leggings adipisicing ennui raw denim banjo hella. Godard\n"
  31587. "mixtape polaroid, pork belly readymade organic cray typewriter helvetica\n"
  31588. "four loko whatever street art yr farm-to-table.\n"
  31589. "\n"
  31590. "Vinyl keytar vice tofu. Locavore you probably haven't heard of them pug\n"
  31591. "pickled, hella tonx labore truffaut DIY mlkshk elit cosby sweater sint\n"
  31592. "et mumblecore. Elit swag semiotics, reprehenderit DIY sartorial nisi ugh\n"
  31593. "nesciunt pug pork belly wayfarers selfies delectus. Ethical hoodie\n"
  31594. "seitan fingerstache kale chips. Terry richardson artisan williamsburg,\n"
  31595. "eiusmod fanny pack irony tonx ennui lo-fi incididunt tofu YOLO\n"
  31596. "readymade. 8-bit sed ethnic beard officia. Pour-over iphone DIY butcher,\n"
  31597. "ethnic art party qui letterpress nisi proident jean shorts mlkshk\n"
  31598. "locavore.\n"
  31599. "\n"
  31600. "Narwhal flexitarian letterpress, do gluten-free voluptate next level\n"
  31601. "banh mi tonx incididunt carles DIY. Odd future nulla 8-bit beard ut\n"
  31602. "cillum pickled velit, YOLO officia you probably haven't heard of them\n"
  31603. "trust fund gastropub. Nisi adipisicing tattooed, Austin mlkshk 90's\n"
  31604. "small batch american apparel. Put a bird on it cosby sweater before they\n"
  31605. "sold out pork belly kogi hella. Street art mollit sustainable polaroid,\n"
  31606. "DIY ethnic ea pug beard dreamcatcher cosby sweater magna scenester nisi.\n"
  31607. "Sed pork belly skateboard mollit, labore proident eiusmod. Sriracha\n"
  31608. "excepteur cosby sweater, anim deserunt laborum eu aliquip ethical et\n"
  31609. "neutra PBR selvage.\n"
  31610. "\n"
  31611. "Raw denim pork belly truffaut, irony plaid sustainable put a bird on it\n"
  31612. "next level jean shorts exercitation. Hashtag keytar whatever, nihil\n"
  31613. "authentic aliquip disrupt laborum. Tattooed selfies deserunt trust fund\n"
  31614. "wayfarers. 3 wolf moon synth church-key sartorial, gastropub leggings\n"
  31615. "tattooed. Labore high life commodo, meggings raw denim fingerstache pug\n"
  31616. "trust fund leggings seitan forage. Nostrud ullamco duis, reprehenderit\n"
  31617. "incididunt flannel sustainable helvetica pork belly pug banksy you\n"
  31618. "probably haven't heard of them nesciunt farm-to-table. Disrupt nostrud\n"
  31619. "mollit magna, sriracha sartorial helvetica.\n"
  31620. "\n"
  31621. "Nulla kogi reprehenderit, skateboard sustainable duis adipisicing viral\n"
  31622. "ad fanny pack salvia. Fanny pack trust fund you probably haven't heard\n"
  31623. "of them YOLO vice nihil. Keffiyeh cray lo-fi pinterest cardigan aliqua,\n"
  31624. "reprehenderit aute. Culpa tousled williamsburg, marfa lomo actually anim\n"
  31625. "skateboard. Iphone aliqua ugh, semiotics pariatur vero readymade\n"
  31626. "organic. Marfa squid nulla, in laborum disrupt laboris irure gastropub.\n"
  31627. "Veniam sunt food truck leggings, sint vinyl fap.\n"
  31628. "\n"
  31629. "Hella dolore pork belly, truffaut carles you probably haven't heard of\n"
  31630. "them PBR helvetica in sapiente. Fashion axe ugh bushwick american\n"
  31631. "apparel. Fingerstache sed iphone, jean shorts blue bottle nisi bushwick\n"
  31632. "flexitarian officia veniam plaid bespoke fap YOLO lo-fi. Blog\n"
  31633. "letterpress mumblecore, food truck id cray brooklyn cillum ad sed.\n"
  31634. "Assumenda chambray wayfarers vinyl mixtape sustainable. VHS vinyl\n"
  31635. "delectus, culpa williamsburg polaroid cliche swag church-key synth kogi\n"
  31636. "magna pop-up literally. Swag thundercats ennui shoreditch vegan\n"
  31637. "pitchfork neutra truffaut etsy, sed single-origin coffee craft beer.\n"
  31638. "\n"
  31639. "Odio letterpress brooklyn elit. Nulla single-origin coffee in occaecat\n"
  31640. "meggings. Irony meggings 8-bit, chillwave lo-fi adipisicing cred\n"
  31641. "dreamcatcher veniam. Put a bird on it irony umami, trust fund bushwick\n"
  31642. "locavore kale chips. Sriracha swag thundercats, chillwave disrupt\n"
  31643. "tousled beard mollit mustache leggings portland next level. Nihil esse\n"
  31644. "est, skateboard art party etsy thundercats sed dreamcatcher ut iphone\n"
  31645. "swag consectetur et. Irure skateboard banjo, nulla deserunt messenger\n"
  31646. "bag dolor terry richardson sapiente.\n";
  31647. static const byte sample_text_gz[] = {
  31648. 0x1F, 0x8B, 0x08, 0x08, 0xC5, 0x49, 0xB5, 0x5B, 0x00, 0x03, 0x63, 0x69, 0x70,
  31649. 0x68, 0x65, 0x72, 0x74, 0x65, 0x78, 0x74, 0x2E, 0x74, 0x78, 0x74, 0x00, 0x8D,
  31650. 0x58, 0xCB, 0x92, 0xE4, 0xB6, 0x11, 0xBC, 0xE3, 0x2B, 0xEA, 0xA6, 0x83, 0xD9,
  31651. 0x1D, 0x72, 0xF8, 0x22, 0x1F, 0xB5, 0x96, 0xA5, 0xDD, 0x90, 0xBC, 0xAB, 0xD0,
  31652. 0x28, 0x36, 0x42, 0x47, 0x90, 0x2C, 0x36, 0xA1, 0x06, 0x09, 0x0A, 0x8F, 0xEE,
  31653. 0xE1, 0xDF, 0x3B, 0x0B, 0xE0, 0x73, 0x2C, 0x4B, 0xBA, 0xCD, 0xCE, 0x80, 0x78,
  31654. 0x64, 0x65, 0x65, 0x66, 0xED, 0x3B, 0xE3, 0x5A, 0xC3, 0x81, 0x2D, 0x35, 0x69,
  31655. 0x32, 0xAD, 0x8E, 0x3A, 0xD2, 0xA0, 0x7D, 0xA7, 0x2B, 0x6A, 0xAC, 0x69, 0x7A,
  31656. 0x26, 0x9D, 0x22, 0xD3, 0x94, 0x22, 0x69, 0xAA, 0x8D, 0x6F, 0xC9, 0x8D, 0x64,
  31657. 0x22, 0x99, 0xB1, 0x31, 0xAD, 0x69, 0xD3, 0x18, 0x89, 0xAD, 0x89, 0x6A, 0x72,
  31658. 0x56, 0x7B, 0x67, 0xDA, 0x2B, 0xBD, 0xC8, 0xEF, 0xB0, 0x4D, 0x74, 0x8E, 0x5B,
  31659. 0xAA, 0x39, 0x4C, 0xEE, 0xCE, 0xE4, 0x79, 0xF2, 0xDC, 0xF3, 0xD8, 0xB2, 0x37,
  31660. 0x11, 0x8B, 0x8C, 0x2C, 0x7A, 0x32, 0x93, 0xF3, 0x37, 0x3D, 0x9A, 0x86, 0x4C,
  31661. 0xAB, 0xF2, 0xB9, 0x57, 0xFA, 0x97, 0x1B, 0x06, 0xD7, 0x3A, 0x7A, 0xF0, 0x68,
  31662. 0xF4, 0x40, 0xBA, 0x25, 0x0E, 0x81, 0xE9, 0xA6, 0x43, 0xF4, 0x6E, 0x4A, 0xF5,
  31663. 0x95, 0xFE, 0x41, 0x4F, 0x67, 0x3B, 0x1A, 0x1C, 0xEE, 0x12, 0xB4, 0x8F, 0xCE,
  31664. 0x1B, 0x6D, 0xB1, 0xDE, 0xBB, 0x4A, 0x4D, 0x56, 0x9B, 0x96, 0x5A, 0xB6, 0xDC,
  31665. 0xC4, 0x14, 0x70, 0xE5, 0xF5, 0x7D, 0xE1, 0xB7, 0x84, 0x3F, 0xFC, 0xED, 0xEF,
  31666. 0xF4, 0x30, 0x0D, 0x5F, 0xE9, 0x47, 0x17, 0xE2, 0xC5, 0x78, 0x27, 0x67, 0xDF,
  31667. 0xB9, 0xEB, 0xCC, 0xCC, 0x3D, 0x59, 0xBE, 0xDD, 0xCC, 0x78, 0x0B, 0x0A, 0x1F,
  31668. 0x74, 0xF8, 0x8C, 0x1A, 0xAF, 0x67, 0xEA, 0xF4, 0x44, 0xBD, 0x93, 0x7D, 0x2A,
  31669. 0xEA, 0x9C, 0xD7, 0x37, 0x80, 0x32, 0x9A, 0x01, 0x37, 0xD5, 0xDE, 0xCA, 0xA2,
  31670. 0x0D, 0xB9, 0xD0, 0x3B, 0xCF, 0xAD, 0x89, 0x4D, 0x5F, 0xD1, 0xE7, 0xF7, 0x2F,
  31671. 0x2A, 0x0C, 0xDA, 0x5A, 0xAA, 0x35, 0x7E, 0x41, 0xC3, 0xB2, 0x37, 0xDD, 0xDD,
  31672. 0xCD, 0x50, 0xEB, 0x2C, 0x96, 0x62, 0x3B, 0xD7, 0x52, 0xF4, 0xA9, 0xB9, 0x6F,
  31673. 0x48, 0xED, 0xEF, 0x54, 0xEA, 0x67, 0xF6, 0x7E, 0x26, 0x8F, 0x3A, 0x68, 0xDF,
  31674. 0x06, 0xBC, 0x56, 0xB7, 0x66, 0x32, 0xC1, 0x34, 0xD8, 0x88, 0x34, 0x1E, 0x88,
  31675. 0xED, 0x67, 0x8A, 0xF3, 0xC4, 0x4F, 0xC0, 0xCA, 0x9E, 0x62, 0x1A, 0x6A, 0xEB,
  31676. 0xAB, 0x02, 0xED, 0xB3, 0xD7, 0x91, 0x81, 0x8A, 0xEA, 0x5C, 0xF2, 0x64, 0xDD,
  31677. 0xDD, 0xD1, 0xEC, 0x12, 0x4D, 0xDE, 0xD5, 0xBA, 0xC6, 0x77, 0xBD, 0x06, 0xC4,
  31678. 0x5F, 0x44, 0xEA, 0x59, 0x4B, 0x5D, 0x3B, 0x8A, 0x3D, 0x0F, 0xD4, 0x9B, 0x1B,
  31679. 0x80, 0x30, 0x1D, 0x30, 0xFA, 0x8F, 0x00, 0x3F, 0xDE, 0xB0, 0x6F, 0xAD, 0x6F,
  31680. 0x6A, 0xDD, 0x6E, 0x2F, 0x6E, 0xCB, 0x3C, 0xD1, 0x83, 0x06, 0x7B, 0x0F, 0xFD,
  31681. 0xFD, 0x4A, 0xEF, 0xBC, 0x73, 0x77, 0x3B, 0x8F, 0x34, 0xA1, 0xBA, 0xEC, 0x39,
  31682. 0x80, 0x33, 0x21, 0xA4, 0x01, 0x55, 0xD7, 0xD4, 0xF4, 0xC6, 0xDA, 0x27, 0x4E,
  31683. 0x54, 0x1C, 0x2B, 0xEC, 0x37, 0xDE, 0xC3, 0x4C, 0xC9, 0x5A, 0x3D, 0x34, 0x0E,
  31684. 0xD8, 0x1C, 0x0E, 0xA2, 0x34, 0xE8, 0xC1, 0xD0, 0xA4, 0x51, 0xD5, 0x88, 0x8B,
  31685. 0xB7, 0xC6, 0xA3, 0x96, 0x40, 0x49, 0xB7, 0xBC, 0xE0, 0x7F, 0x55, 0x3F, 0xEF,
  31686. 0x6F, 0x6E, 0x92, 0x9D, 0x34, 0xFE, 0x3C, 0x5F, 0x04, 0xA5, 0x6A, 0xFF, 0x30,
  31687. 0x08, 0xC9, 0xEA, 0xF5, 0x52, 0x2B, 0xFE, 0x57, 0xFA, 0x8E, 0xC7, 0xE8, 0x4D,
  31688. 0x37, 0xAB, 0x03, 0xFA, 0x23, 0xBF, 0x46, 0x94, 0xFF, 0xC1, 0x16, 0xE0, 0xB9,
  31689. 0x14, 0x2C, 0x9E, 0x27, 0xEC, 0x98, 0x69, 0x14, 0x92, 0xF1, 0x60, 0x5C, 0x34,
  31690. 0x4D, 0xA0, 0x1F, 0xDF, 0xFD, 0x44, 0x1C, 0x7B, 0xD3, 0x80, 0x70, 0x42, 0x02,
  31691. 0x30, 0x84, 0x5B, 0xE5, 0x59, 0xB7, 0xF3, 0x80, 0xFB, 0x01, 0x33, 0xA9, 0x00,
  31692. 0x37, 0x52, 0xDC, 0xDA, 0xA7, 0x11, 0x85, 0xB7, 0x6E, 0x70, 0xE4, 0xDA, 0x96,
  31693. 0xBA, 0x84, 0x5B, 0x81, 0x43, 0x93, 0xF3, 0xD1, 0xEA, 0xB1, 0xDD, 0xB8, 0x1F,
  31694. 0xA5, 0xCC, 0xEA, 0x50, 0x66, 0x69, 0xA9, 0x8D, 0x8C, 0xA7, 0xA2, 0xF3, 0x38,
  31695. 0x26, 0x43, 0x5E, 0x3F, 0x01, 0xBE, 0x1C, 0x0F, 0x20, 0x7F, 0x75, 0xA8, 0x20,
  31696. 0x80, 0xC4, 0xC3, 0x5C, 0x8B, 0x0D, 0xD4, 0x60, 0x5E, 0xA3, 0x9E, 0xD0, 0xB4,
  31697. 0x4B, 0x4F, 0xE6, 0x13, 0x85, 0x60, 0x42, 0x96, 0xED, 0xAA, 0xDB, 0xE9, 0x99,
  31698. 0xE3, 0x07, 0x0E, 0x61, 0xB3, 0x07, 0xE3, 0xB1, 0xFA, 0xC0, 0x9B, 0xAD, 0xF6,
  31699. 0xE0, 0x26, 0x33, 0xEA, 0xEA, 0x23, 0xCD, 0x1E, 0x9D, 0xE1, 0x87, 0x4B, 0x74,
  31700. 0x97, 0x08, 0x3E, 0xA1, 0x28, 0xEA, 0xB3, 0x19, 0x67, 0x8B, 0x76, 0x9A, 0xA3,
  31701. 0xF6, 0xB9, 0xCF, 0x80, 0x65, 0x97, 0xAE, 0xF4, 0x83, 0x6B, 0xF4, 0x43, 0x20,
  31702. 0xF9, 0x0B, 0xFC, 0x9B, 0xD2, 0x4D, 0x4D, 0xA6, 0xB9, 0xA3, 0x02, 0x55, 0x79,
  31703. 0x18, 0x36, 0x19, 0x5F, 0xC9, 0xEA, 0x5A, 0x76, 0x40, 0xB9, 0xBA, 0x0E, 0x9A,
  31704. 0x44, 0xDF, 0x7C, 0xF8, 0x65, 0x61, 0x5E, 0x81, 0xAB, 0x71, 0xA1, 0x9E, 0x29,
  31705. 0x3C, 0x59, 0xCB, 0x23, 0xA4, 0xF6, 0x60, 0x1A, 0x0D, 0x5B, 0x39, 0xAE, 0xF4,
  31706. 0x6F, 0x59, 0x16, 0x9E, 0x60, 0xD8, 0x56, 0xCF, 0xEA, 0x2C, 0x4C, 0x79, 0xD3,
  31707. 0x5D, 0x51, 0x46, 0xA0, 0x4E, 0xE9, 0xD6, 0xAB, 0x91, 0x43, 0x63, 0x44, 0xD7,
  31708. 0x70, 0xB9, 0x23, 0x98, 0x4F, 0x3D, 0x03, 0x02, 0xF6, 0x81, 0x56, 0xC1, 0x58,
  31709. 0x85, 0x07, 0xA7, 0x2D, 0x2C, 0x29, 0xCA, 0x01, 0x45, 0x31, 0x51, 0x8F, 0xD4,
  31710. 0x19, 0xA1, 0x79, 0x88, 0x5A, 0xA4, 0xF5, 0xAE, 0x2D, 0x4B, 0x63, 0x4C, 0x58,
  31711. 0xFE, 0xBF, 0xAD, 0xEE, 0xA3, 0x09, 0xF8, 0xE2, 0x89, 0xBE, 0x81, 0x0E, 0x86,
  31712. 0x3A, 0xF9, 0x5B, 0xA5, 0xD8, 0xA4, 0x00, 0x75, 0x04, 0xF2, 0x23, 0xB8, 0x39,
  31713. 0x69, 0x50, 0xB7, 0xD0, 0x34, 0x63, 0x54, 0xD8, 0x61, 0xDD, 0xA5, 0x33, 0x47,
  31714. 0x85, 0x96, 0x22, 0xD0, 0x2F, 0x9F, 0x7E, 0xF8, 0x74, 0x24, 0xEA, 0x57, 0x97,
  31715. 0x5A, 0xE0, 0x00, 0xCF, 0xC1, 0x67, 0xE1, 0x41, 0xBD, 0x94, 0xA1, 0x03, 0xD3,
  31716. 0xB4, 0x08, 0x64, 0xF2, 0x17, 0x27, 0x35, 0x37, 0x53, 0xEF, 0x46, 0xCE, 0xD8,
  31717. 0xD4, 0x09, 0x52, 0xC6, 0x1E, 0xF7, 0x28, 0xDF, 0x08, 0x0F, 0xD0, 0x6F, 0x71,
  31718. 0xA6, 0xDF, 0xE4, 0x60, 0x8E, 0xC0, 0x1E, 0x78, 0x86, 0x50, 0xB0, 0x9B, 0x84,
  31719. 0x7E, 0xE8, 0x36, 0xFA, 0x95, 0xF1, 0x12, 0x51, 0xC7, 0x18, 0x96, 0xA2, 0x29,
  31720. 0xBB, 0x70, 0x02, 0xB4, 0xF9, 0xA8, 0x3D, 0x08, 0x66, 0xA9, 0xB3, 0xFC, 0x0A,
  31721. 0x94, 0x80, 0xFD, 0x78, 0xDC, 0xAB, 0x82, 0x5A, 0xD2, 0xCD, 0xC2, 0x87, 0xC6,
  31722. 0x4B, 0x07, 0xFA, 0xD1, 0xC3, 0xD9, 0x34, 0x41, 0x85, 0xF8, 0xD0, 0xB6, 0x0A,
  31723. 0x9D, 0x00, 0x91, 0x35, 0x05, 0x88, 0xC3, 0xE3, 0x9B, 0x22, 0xD2, 0xB8, 0xFD,
  31724. 0x95, 0x3E, 0x6D, 0x5D, 0x48, 0xA3, 0x68, 0xCF, 0x02, 0x42, 0x79, 0x79, 0x8A,
  31725. 0xAA, 0x01, 0xD6, 0x09, 0x14, 0x2C, 0xF4, 0x83, 0xA3, 0x80, 0x31, 0x55, 0x46,
  31726. 0x6E, 0xC5, 0xE5, 0x2F, 0x30, 0x58, 0x81, 0xA2, 0x90, 0xBE, 0x2E, 0xA1, 0xC3,
  31727. 0x0F, 0xA6, 0xF5, 0x51, 0x00, 0x39, 0xB6, 0xF2, 0x2A, 0xA3, 0x15, 0x7D, 0x8D,
  31728. 0xF5, 0x66, 0x5C, 0xD9, 0xFC, 0xCF, 0x2F, 0xBF, 0x08, 0x27, 0xE7, 0xD0, 0x03,
  31729. 0xB8, 0xD9, 0x00, 0x13, 0x3D, 0x01, 0x6B, 0xB6, 0xA8, 0xCD, 0x5B, 0x3B, 0x3E,
  31730. 0x93, 0xBF, 0xE6, 0x2E, 0xB7, 0x4A, 0xCF, 0xB3, 0x0A, 0xCE, 0x62, 0x11, 0xD6,
  31731. 0x1F, 0x68, 0x9B, 0x1D, 0x68, 0xD1, 0x8C, 0x97, 0xBD, 0xA1, 0x07, 0x67, 0x73,
  31732. 0x87, 0xE0, 0x36, 0xDA, 0x8C, 0xD2, 0xD2, 0xBB, 0x84, 0x28, 0xA9, 0xFE, 0x52,
  31733. 0x74, 0xD6, 0xB9, 0x0F, 0x0A, 0x6A, 0x2D, 0x28, 0x35, 0x34, 0x3A, 0xD3, 0xE2,
  31734. 0xCD, 0x35, 0x06, 0x7D, 0x1B, 0x35, 0x85, 0x86, 0xD1, 0x3E, 0xF2, 0x6F, 0xA1,
  31735. 0xC4, 0x55, 0xBD, 0x00, 0xD8, 0xC3, 0x5D, 0xC2, 0x1D, 0x6B, 0x6B, 0x27, 0x5B,
  31736. 0x95, 0xF3, 0xAB, 0xB5, 0xD3, 0x37, 0xF2, 0x2C, 0x9C, 0xC7, 0x5D, 0xBD, 0xF1,
  31737. 0x68, 0x1C, 0xAD, 0xF8, 0xB5, 0xE1, 0x29, 0x72, 0x7A, 0x73, 0x62, 0x55, 0x24,
  31738. 0xB9, 0x85, 0xDF, 0x7B, 0x29, 0x7D, 0xDE, 0x08, 0xF5, 0xE4, 0x44, 0xDA, 0x1A,
  31739. 0x30, 0x74, 0xDA, 0xB4, 0x9B, 0x23, 0x9A, 0x3A, 0xC1, 0x53, 0xB2, 0xA2, 0xA3,
  31740. 0x7B, 0x1F, 0xD9, 0x56, 0xD4, 0x4F, 0x9B, 0xB2, 0x1E, 0xEE, 0xB8, 0x6A, 0x4E,
  31741. 0xB5, 0xF4, 0x5A, 0xC9, 0x18, 0x27, 0x9C, 0xDE, 0x14, 0x44, 0xED, 0xC4, 0x3C,
  31742. 0x71, 0x9F, 0x5F, 0xD9, 0x37, 0xA0, 0x78, 0x34, 0x6E, 0xBC, 0xD2, 0x7B, 0x1D,
  31743. 0xFA, 0x08, 0x39, 0x5A, 0x04, 0x73, 0x15, 0xD9, 0x0A, 0x48, 0xC1, 0x2D, 0x15,
  31744. 0x4E, 0x84, 0x30, 0x45, 0x69, 0xB3, 0xE5, 0xF6, 0xAD, 0x09, 0x1E, 0xCC, 0x5F,
  31745. 0x1F, 0x06, 0xD5, 0x58, 0xAD, 0x78, 0xD7, 0x9F, 0xE5, 0xED, 0x3B, 0x09, 0xD5,
  31746. 0xA6, 0x52, 0x6F, 0x92, 0xD3, 0x3C, 0xC6, 0x1E, 0xF2, 0x93, 0x7C, 0xD3, 0x5F,
  31747. 0x70, 0x85, 0x5D, 0xF8, 0xAA, 0x9D, 0xB7, 0x7B, 0x24, 0x5A, 0xE9, 0x0A, 0x35,
  31748. 0x2F, 0xF5, 0xD9, 0x82, 0x02, 0x8A, 0x90, 0x13, 0x5B, 0xB5, 0x67, 0x9C, 0xDD,
  31749. 0xA0, 0x4E, 0x82, 0x27, 0xDA, 0x7E, 0xE8, 0x8E, 0xCD, 0xE1, 0x56, 0x71, 0x2C,
  31750. 0xE6, 0x4E, 0x1F, 0x91, 0xCD, 0x7C, 0x6A, 0xB7, 0x78, 0xD0, 0x26, 0xF3, 0x56,
  31751. 0xA9, 0xD5, 0xA1, 0xC3, 0x3B, 0x98, 0xE9, 0x28, 0x09, 0xEF, 0x50, 0x90, 0xCD,
  31752. 0xC4, 0x8E, 0x75, 0xCC, 0xAC, 0x2D, 0xC9, 0x03, 0x6D, 0xAC, 0xFE, 0xC4, 0x88,
  31753. 0x36, 0xD1, 0x3F, 0xBB, 0x1C, 0x7D, 0xB3, 0x14, 0x61, 0x2C, 0xB7, 0x54, 0x4B,
  31754. 0xDB, 0x64, 0xB6, 0x57, 0x14, 0x16, 0x8E, 0x1E, 0x6C, 0x64, 0xBB, 0x8B, 0x48,
  31755. 0x5D, 0x96, 0x9D, 0xDC, 0x80, 0xA7, 0xF7, 0x54, 0xC7, 0x46, 0x38, 0x3E, 0x44,
  31756. 0xDE, 0x7E, 0x92, 0x8D, 0x07, 0xF6, 0x07, 0x37, 0x4E, 0x16, 0x10, 0xB4, 0x7D,
  31757. 0x88, 0x66, 0x7F, 0xBB, 0xFF, 0xEA, 0x00, 0xF3, 0xFF, 0x97, 0x2C, 0xB5, 0xBE,
  31758. 0x35, 0x4B, 0x5C, 0x36, 0xEC, 0x4C, 0xBD, 0x2B, 0x7D, 0xBF, 0x46, 0xE2, 0x9C,
  31759. 0x0E, 0x8A, 0xA3, 0xEC, 0xB1, 0x0E, 0x9A, 0xDA, 0x9A, 0x9B, 0x28, 0x92, 0x10,
  31760. 0x53, 0x57, 0xEA, 0xEC, 0xA2, 0x32, 0x32, 0x20, 0x1D, 0x97, 0x5C, 0xB6, 0x84,
  31761. 0xA9, 0x93, 0x8D, 0x95, 0x11, 0xA3, 0x24, 0xA3, 0x2D, 0xC6, 0x4A, 0xEF, 0xAA,
  31762. 0x1D, 0x85, 0x2B, 0x7D, 0x28, 0xBE, 0x53, 0xCE, 0x10, 0x1F, 0xAE, 0x0E, 0x41,
  31763. 0x6C, 0x4B, 0x79, 0x12, 0xFB, 0xF7, 0x54, 0xA3, 0x96, 0x54, 0x83, 0x20, 0x96,
  31764. 0x8F, 0x28, 0xA9, 0x3F, 0x8B, 0x3D, 0xBA, 0x77, 0xDC, 0x24, 0xE1, 0xD4, 0x49,
  31765. 0x40, 0xD8, 0x78, 0x31, 0x85, 0x43, 0xF6, 0xFE, 0x5C, 0xA6, 0x8F, 0x90, 0x09,
  31766. 0xB0, 0xE7, 0xC4, 0x95, 0xB2, 0x55, 0x49, 0x97, 0x8F, 0x1C, 0x78, 0x30, 0x20,
  31767. 0xA0, 0xB4, 0xEF, 0x73, 0x56, 0x59, 0x82, 0xFD, 0xCE, 0xBA, 0x6A, 0x8F, 0x2C,
  31768. 0x8B, 0x15, 0xFD, 0xA1, 0x85, 0xA8, 0x5C, 0x0F, 0x11, 0xA5, 0x9D, 0xC2, 0x46,
  31769. 0xC6, 0x9C, 0xC9, 0x40, 0x0B, 0x58, 0x6A, 0x1C, 0x7A, 0x23, 0xF9, 0xE0, 0x95,
  31770. 0x05, 0x13, 0x58, 0x72, 0xE8, 0x9F, 0x30, 0xAC, 0xCD, 0x26, 0xD4, 0x66, 0x13,
  31771. 0xDF, 0x1E, 0x7B, 0x4F, 0x9C, 0xBE, 0x38, 0x79, 0x75, 0x92, 0xA4, 0xDA, 0x26,
  31772. 0x44, 0x55, 0x17, 0xA3, 0xE5, 0x62, 0xDA, 0xEB, 0x86, 0xEA, 0x68, 0xC7, 0xAB,
  31773. 0xFD, 0x2D, 0x43, 0x59, 0x51, 0xC0, 0x75, 0x64, 0x91, 0x01, 0x29, 0x33, 0x28,
  31774. 0xF3, 0x04, 0x83, 0x80, 0x75, 0x37, 0x75, 0x0C, 0x03, 0x7B, 0x0A, 0xAB, 0x8E,
  31775. 0x60, 0x62, 0x8B, 0x4C, 0xAF, 0x2D, 0xA3, 0x2F, 0xFE, 0xAB, 0x45, 0xCF, 0xDA,
  31776. 0xAB, 0xFA, 0xFA, 0x30, 0x3D, 0xE8, 0xA1, 0x96, 0xA5, 0x7B, 0xE2, 0x2A, 0xD0,
  31777. 0xAF, 0x59, 0xF7, 0xD0, 0x32, 0x57, 0x19, 0xBD, 0xCA, 0x9F, 0xD5, 0x1A, 0xC7,
  31778. 0xAA, 0x65, 0x4A, 0x38, 0xB2, 0x70, 0x33, 0xB7, 0x75, 0xD2, 0xCD, 0xD1, 0xF0,
  31779. 0xA8, 0x87, 0x59, 0x20, 0xA5, 0x57, 0x55, 0xB1, 0xB2, 0xC9, 0x4D, 0x97, 0x34,
  31780. 0x41, 0xF3, 0xF0, 0x30, 0xA1, 0x2C, 0x1C, 0x49, 0x3E, 0x89, 0x7D, 0x12, 0xE2,
  31781. 0xC3, 0x04, 0xC3, 0x92, 0xC0, 0xF6, 0x39, 0x10, 0x80, 0x81, 0x8F, 0x08, 0xB4,
  31782. 0xF8, 0xB9, 0x13, 0x4E, 0x2C, 0xAE, 0xB3, 0x71, 0x82, 0x63, 0x98, 0xAB, 0x5C,
  31783. 0x1C, 0x10, 0xEA, 0x66, 0xF9, 0x02, 0x3A, 0x82, 0x61, 0xD0, 0xD4, 0xAE, 0x43,
  31784. 0xD4, 0x01, 0x3E, 0x9D, 0x04, 0x14, 0xF6, 0x60, 0xD8, 0xA7, 0xD6, 0xB8, 0x53,
  31785. 0xC8, 0xDA, 0x80, 0x93, 0xA0, 0x02, 0xDD, 0xCC, 0xE2, 0xF2, 0xBB, 0xFB, 0xE0,
  31786. 0x27, 0xD7, 0x34, 0x9A, 0x71, 0x49, 0xB5, 0x4F, 0x42, 0x1F, 0xB2, 0x9D, 0x6D,
  31787. 0xAA, 0x9D, 0xD3, 0x50, 0xB5, 0x8F, 0x6A, 0x4B, 0xDF, 0x1F, 0xD5, 0x27, 0x8F,
  31788. 0x3B, 0x27, 0xCF, 0x2F, 0x8C, 0xF8, 0x9D, 0x4C, 0x52, 0xBC, 0x32, 0x0F, 0x73,
  31789. 0xD5, 0x51, 0x8E, 0x36, 0x7E, 0xAD, 0x09, 0xF0, 0x94, 0x83, 0x5F, 0x36, 0xFD,
  31790. 0x7C, 0x03, 0xED, 0xF1, 0x5E, 0x4B, 0xF7, 0xAA, 0x55, 0x5C, 0x4A, 0x14, 0x59,
  31791. 0x85, 0x38, 0x2D, 0x8C, 0xDF, 0xEC, 0x65, 0x1B, 0xB8, 0x76, 0x57, 0x96, 0x3C,
  31792. 0x86, 0xED, 0xF2, 0x7F, 0x2D, 0x28, 0x48, 0xDA, 0x49, 0x7F, 0xF7, 0x54, 0x2B,
  31793. 0xD5, 0x39, 0xD5, 0x57, 0x0A, 0x75, 0x7A, 0x3E, 0x5E, 0x5D, 0xBA, 0x4A, 0x15,
  31794. 0xFA, 0xB8, 0x31, 0x80, 0x71, 0x2C, 0xCA, 0xC4, 0x51, 0x10, 0x16, 0x5D, 0x39,
  31795. 0xEC, 0x9D, 0x07, 0xB6, 0x6A, 0x89, 0x9F, 0x9B, 0x5B, 0x6F, 0x03, 0xB0, 0x92,
  31796. 0x01, 0x38, 0x6B, 0x48, 0x99, 0x0A, 0x8F, 0x13, 0xC1, 0xA6, 0x01, 0xEA, 0xBF,
  31797. 0x6F, 0x86, 0x43, 0x51, 0xB6, 0x11, 0x00, 0x00
  31798. };
  31799. WOLFSSL_TEST_SUBROUTINE int compress_test(void)
  31800. {
  31801. int ret = 0;
  31802. word32 dSz = sizeof(sample_text);
  31803. word32 cSz = (dSz + (word32)(dSz * 0.001) + 12);
  31804. byte *c;
  31805. byte *d;
  31806. c = (byte *)XMALLOC(cSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31807. d = (byte *)XMALLOC(dSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31808. if (c == NULL || d == NULL) {
  31809. ERROR_OUT(-12100, exit);
  31810. }
  31811. /* follow calloc and initialize to 0 */
  31812. XMEMSET(c, 0, cSz);
  31813. XMEMSET(d, 0, dSz);
  31814. if ((ret = wc_Compress(c, cSz, sample_text, dSz, 0)) < 0) {
  31815. ERROR_OUT(-12101, exit);
  31816. }
  31817. cSz = (word32)ret;
  31818. if ((ret = wc_DeCompress(d, dSz, c, cSz)) != (int)dSz) {
  31819. ERROR_OUT(-12102, exit);
  31820. }
  31821. dSz = (word32)ret;
  31822. if (XMEMCMP(d, sample_text, dSz) != 0) {
  31823. ERROR_OUT(-12103, exit);
  31824. }
  31825. /* GZIP tests */
  31826. cSz = (dSz + (word32)(dSz * 0.001) + 12); /* reset cSz */
  31827. XMEMSET(c, 0, cSz);
  31828. XMEMSET(d, 0, dSz);
  31829. ret = wc_Compress_ex(c, cSz, sample_text, dSz, 0, LIBZ_WINBITS_GZIP);
  31830. if (ret < 0) {
  31831. ERROR_OUT(-12104, exit);
  31832. }
  31833. cSz = (word32)ret;
  31834. ret = wc_DeCompress_ex(d, dSz, c, cSz, LIBZ_WINBITS_GZIP);
  31835. if (ret < 0) {
  31836. ERROR_OUT(-12105, exit);
  31837. }
  31838. if (XMEMCMP(d, sample_text, dSz) != 0) {
  31839. ERROR_OUT(-12106, exit);
  31840. }
  31841. /* Try with gzip generated output */
  31842. XMEMSET(d, 0, dSz);
  31843. ret = wc_DeCompress_ex(d, dSz, sample_text_gz, sizeof(sample_text_gz),
  31844. LIBZ_WINBITS_GZIP);
  31845. if (ret < 0) {
  31846. ERROR_OUT(-12107, exit);
  31847. }
  31848. dSz = (word32)ret;
  31849. if (XMEMCMP(d, sample_text, dSz) != 0) {
  31850. ERROR_OUT(-12108, exit);
  31851. }
  31852. ret = 0; /* success */
  31853. exit:
  31854. if (c) XFREE(c, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31855. if (d) XFREE(d, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31856. return ret;
  31857. }
  31858. #endif /* HAVE_LIBZ */
  31859. #ifdef HAVE_PKCS7
  31860. /* External Debugging/Testing Note:
  31861. *
  31862. * PKCS#7 test functions can output generated PKCS#7/CMS bundles for
  31863. * additional testing. To dump bundles to files DER encoded files, please
  31864. * define:
  31865. *
  31866. * #define PKCS7_OUTPUT_TEST_BUNDLES
  31867. */
  31868. /* Loads certs and keys for use with PKCS7 tests, from either files
  31869. * or buffers.
  31870. *
  31871. * rsaClientCertBuf - output buffer for RSA client cert
  31872. * rsaClientCertBufSz - IN/OUT size of output buffer, size of RSA client cert
  31873. * rsaClientPrivKeyBuf - output buffer for RSA client private key
  31874. * rsaClientPrivKeyBufSz - IN/OUT size of output buffer, size of RSA client key
  31875. *
  31876. * rsaServerCertBuf - output buffer for RSA server cert
  31877. * rsaServerCertBufSz - IN/OUT size of output buffer, size of RSA server cert
  31878. * rsaServerPrivKeyBuf - output buffer for RSA server private key
  31879. * rsaServerPrivKeyBufSz - IN/OUT size of output buffer, size of RSA server key
  31880. *
  31881. * rsaCaCertBuf - output buffer for RSA CA cert
  31882. * rsaCaCertBufSz - IN/OUT size of output buffer, size of RSA ca cert
  31883. * rsaCaPrivKeyBuf - output buffer for RSA CA private key
  31884. * rsaCaPrivKeyBufSz - IN/OUT size of output buffer, size of RSA CA key
  31885. *
  31886. * eccClientCertBuf - output buffer for ECC cert
  31887. * eccClientCertBufSz - IN/OUT size of output buffer, size of ECC cert
  31888. * eccClientPrivKeyBuf - output buffer for ECC private key
  31889. * eccClientPrivKeyBufSz - IN/OUT size of output buffer, size of ECC private key
  31890. *
  31891. * Returns 0 on success, negative on error
  31892. */
  31893. static int pkcs7_load_certs_keys(
  31894. byte* rsaClientCertBuf, word32* rsaClientCertBufSz,
  31895. byte* rsaClientPrivKeyBuf, word32* rsaClientPrivKeyBufSz,
  31896. byte* rsaServerCertBuf, word32* rsaServerCertBufSz,
  31897. byte* rsaServerPrivKeyBuf, word32* rsaServerPrivKeyBufSz,
  31898. byte* rsaCaCertBuf, word32* rsaCaCertBufSz,
  31899. byte* rsaCaPrivKeyBuf, word32* rsaCaPrivKeyBufSz,
  31900. byte* eccClientCertBuf, word32* eccClientCertBufSz,
  31901. byte* eccClientPrivKeyBuf, word32* eccClientPrivKeyBufSz)
  31902. {
  31903. #ifndef NO_FILESYSTEM
  31904. XFILE certFile;
  31905. XFILE keyFile;
  31906. (void)certFile;
  31907. (void)keyFile;
  31908. #endif
  31909. #ifndef NO_RSA
  31910. if (rsaClientCertBuf == NULL || rsaClientCertBufSz == NULL ||
  31911. rsaClientPrivKeyBuf == NULL || rsaClientPrivKeyBufSz == NULL)
  31912. return BAD_FUNC_ARG;
  31913. #endif
  31914. #ifdef HAVE_ECC
  31915. if (eccClientCertBuf == NULL || eccClientCertBufSz == NULL ||
  31916. eccClientPrivKeyBuf == NULL || eccClientPrivKeyBufSz == NULL)
  31917. return BAD_FUNC_ARG;
  31918. #endif
  31919. /* RSA */
  31920. #ifndef NO_RSA
  31921. #ifdef USE_CERT_BUFFERS_1024
  31922. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_1024)
  31923. return -12110;
  31924. XMEMCPY(rsaClientCertBuf, client_cert_der_1024,
  31925. sizeof_client_cert_der_1024);
  31926. *rsaClientCertBufSz = sizeof_client_cert_der_1024;
  31927. if (rsaServerCertBuf != NULL) {
  31928. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_1024)
  31929. return -12111;
  31930. XMEMCPY(rsaServerCertBuf, server_cert_der_1024,
  31931. sizeof_server_cert_der_1024);
  31932. *rsaServerCertBufSz = sizeof_server_cert_der_1024;
  31933. }
  31934. if (rsaCaCertBuf != NULL) {
  31935. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_1024)
  31936. return -12112;
  31937. XMEMCPY(rsaCaCertBuf, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  31938. *rsaCaCertBufSz = sizeof_ca_cert_der_1024;
  31939. }
  31940. #elif defined(USE_CERT_BUFFERS_2048)
  31941. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_2048)
  31942. return -12113;
  31943. XMEMCPY(rsaClientCertBuf, client_cert_der_2048,
  31944. sizeof_client_cert_der_2048);
  31945. *rsaClientCertBufSz = sizeof_client_cert_der_2048;
  31946. if (rsaServerCertBuf != NULL) {
  31947. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_2048)
  31948. return -12114;
  31949. XMEMCPY(rsaServerCertBuf, server_cert_der_2048,
  31950. sizeof_server_cert_der_2048);
  31951. *rsaServerCertBufSz = sizeof_server_cert_der_2048;
  31952. }
  31953. if (rsaCaCertBuf != NULL) {
  31954. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_2048)
  31955. return -12115;
  31956. XMEMCPY(rsaCaCertBuf, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  31957. *rsaCaCertBufSz = sizeof_ca_cert_der_2048;
  31958. }
  31959. #else
  31960. certFile = XFOPEN(clientCert, "rb");
  31961. if (!certFile)
  31962. return -12116;
  31963. *rsaClientCertBufSz = (word32)XFREAD(rsaClientCertBuf, 1,
  31964. *rsaClientCertBufSz, certFile);
  31965. XFCLOSE(certFile);
  31966. if (rsaServerCertBuf != NULL) {
  31967. certFile = XFOPEN(rsaServerCertDerFile, "rb");
  31968. if (!certFile)
  31969. return -12117;
  31970. *rsaServerCertBufSz = (word32)XFREAD(rsaServerCertBuf, 1,
  31971. *rsaServerCertBufSz, certFile);
  31972. XFCLOSE(certFile);
  31973. }
  31974. if (rsaCaCertBuf != NULL) {
  31975. certFile = XFOPEN(rsaCaCertDerFile, "rb");
  31976. if (!certFile)
  31977. return -12118;
  31978. *rsaCaCertBufSz = (word32)XFREAD(rsaCaCertBuf, 1, *rsaCaCertBufSz,
  31979. certFile);
  31980. XFCLOSE(certFile);
  31981. }
  31982. #endif
  31983. #ifdef USE_CERT_BUFFERS_1024
  31984. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_1024)
  31985. return -12119;
  31986. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_1024,
  31987. sizeof_client_key_der_1024);
  31988. *rsaClientPrivKeyBufSz = sizeof_client_key_der_1024;
  31989. if (rsaServerPrivKeyBuf != NULL) {
  31990. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_1024)
  31991. return -12120;
  31992. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_1024,
  31993. sizeof_server_key_der_1024);
  31994. *rsaServerPrivKeyBufSz = sizeof_server_key_der_1024;
  31995. }
  31996. if (rsaCaPrivKeyBuf != NULL) {
  31997. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_1024)
  31998. return -12121;
  31999. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_1024, sizeof_ca_key_der_1024);
  32000. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_1024;
  32001. }
  32002. #elif defined(USE_CERT_BUFFERS_2048)
  32003. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_2048)
  32004. return -12122;
  32005. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_2048,
  32006. sizeof_client_key_der_2048);
  32007. *rsaClientPrivKeyBufSz = sizeof_client_key_der_2048;
  32008. if (rsaServerPrivKeyBuf != NULL) {
  32009. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_2048)
  32010. return -12123;
  32011. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_2048,
  32012. sizeof_server_key_der_2048);
  32013. *rsaServerPrivKeyBufSz = sizeof_server_key_der_2048;
  32014. }
  32015. if (rsaCaPrivKeyBuf != NULL) {
  32016. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_2048)
  32017. return -12124;
  32018. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_2048, sizeof_ca_key_der_2048);
  32019. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_2048;
  32020. }
  32021. #else
  32022. keyFile = XFOPEN(clientKey, "rb");
  32023. if (!keyFile)
  32024. return -12125;
  32025. *rsaClientPrivKeyBufSz = (word32)XFREAD(rsaClientPrivKeyBuf, 1,
  32026. *rsaClientPrivKeyBufSz, keyFile);
  32027. XFCLOSE(keyFile);
  32028. if (rsaServerPrivKeyBuf != NULL) {
  32029. keyFile = XFOPEN(rsaServerKeyDerFile, "rb");
  32030. if (!keyFile)
  32031. return -12126;
  32032. *rsaServerPrivKeyBufSz = (word32)XFREAD(rsaServerPrivKeyBuf, 1,
  32033. *rsaServerPrivKeyBufSz, keyFile);
  32034. XFCLOSE(keyFile);
  32035. }
  32036. if (rsaCaPrivKeyBuf != NULL) {
  32037. keyFile = XFOPEN(rsaCaKeyFile, "rb");
  32038. if (!keyFile)
  32039. return -12127;
  32040. *rsaCaPrivKeyBufSz = (word32)XFREAD(rsaCaPrivKeyBuf, 1,
  32041. *rsaCaPrivKeyBufSz, keyFile);
  32042. XFCLOSE(keyFile);
  32043. }
  32044. #endif /* USE_CERT_BUFFERS */
  32045. #endif /* NO_RSA */
  32046. /* ECC */
  32047. #ifdef HAVE_ECC
  32048. #ifdef USE_CERT_BUFFERS_256
  32049. if (*eccClientCertBufSz < (word32)sizeof_cliecc_cert_der_256)
  32050. return -12128;
  32051. XMEMCPY(eccClientCertBuf, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  32052. *eccClientCertBufSz = sizeof_cliecc_cert_der_256;
  32053. #else
  32054. certFile = XFOPEN(eccClientCert, "rb");
  32055. if (!certFile)
  32056. return -12129;
  32057. *eccClientCertBufSz = (word32)XFREAD(eccClientCertBuf, 1,
  32058. *eccClientCertBufSz, certFile);
  32059. XFCLOSE(certFile);
  32060. #endif /* USE_CERT_BUFFERS_256 */
  32061. #ifdef USE_CERT_BUFFERS_256
  32062. if (*eccClientPrivKeyBufSz < (word32)sizeof_ecc_clikey_der_256)
  32063. return -12130;
  32064. XMEMCPY(eccClientPrivKeyBuf, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  32065. *eccClientPrivKeyBufSz = sizeof_ecc_clikey_der_256;
  32066. #else
  32067. keyFile = XFOPEN(eccClientKey, "rb");
  32068. if (!keyFile)
  32069. return -12131;
  32070. *eccClientPrivKeyBufSz = (word32)XFREAD(eccClientPrivKeyBuf, 1,
  32071. *eccClientPrivKeyBufSz, keyFile);
  32072. XFCLOSE(keyFile);
  32073. #endif /* USE_CERT_BUFFERS_256 */
  32074. #endif /* HAVE_ECC */
  32075. #ifdef NO_RSA
  32076. (void)rsaClientCertBuf;
  32077. (void)rsaClientCertBufSz;
  32078. (void)rsaClientPrivKeyBuf;
  32079. (void)rsaClientPrivKeyBufSz;
  32080. (void)rsaServerCertBuf;
  32081. (void)rsaServerCertBufSz;
  32082. (void)rsaServerPrivKeyBuf;
  32083. (void)rsaServerPrivKeyBufSz;
  32084. (void)rsaCaCertBuf;
  32085. (void)rsaCaCertBufSz;
  32086. (void)rsaCaPrivKeyBuf;
  32087. (void)rsaCaPrivKeyBufSz;
  32088. #endif
  32089. #ifndef HAVE_ECC
  32090. (void)eccClientCertBuf;
  32091. (void)eccClientCertBufSz;
  32092. (void)eccClientPrivKeyBuf;
  32093. (void)eccClientPrivKeyBufSz;
  32094. #endif
  32095. #ifndef NO_FILESYSTEM
  32096. (void)certFile;
  32097. (void)keyFile;
  32098. #endif
  32099. return 0;
  32100. }
  32101. typedef struct {
  32102. const byte* content;
  32103. word32 contentSz;
  32104. int contentOID;
  32105. int encryptOID;
  32106. int keyWrapOID;
  32107. int keyAgreeOID;
  32108. byte* cert;
  32109. size_t certSz;
  32110. byte* privateKey;
  32111. word32 privateKeySz;
  32112. byte* optionalUkm;
  32113. word32 optionalUkmSz;
  32114. int ktriOptions; /* KTRI options flags */
  32115. int kariOptions; /* KARI options flags */
  32116. /* KEKRI specific */
  32117. const byte* secretKey; /* key, only for kekri RecipientInfo types */
  32118. word32 secretKeySz; /* size of secretKey, bytes */
  32119. const byte* secretKeyId; /* key identifier */
  32120. word32 secretKeyIdSz; /* size of key identifier, bytes */
  32121. void* timePtr; /* time_t pointer */
  32122. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  32123. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  32124. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  32125. word32 otherAttrSz; /* size of otherAttr, bytes */
  32126. int kekriOptions; /* KEKRI options flags */
  32127. /* PWRI specific */
  32128. const char* password;
  32129. word32 passwordSz;
  32130. const byte* salt;
  32131. word32 saltSz;
  32132. int kdfOID;
  32133. int hashOID;
  32134. int kdfIterations;
  32135. int pwriOptions; /* PWRI options flags */
  32136. /* ORI specific */
  32137. int isOri;
  32138. int oriOptions; /* ORI options flags */
  32139. const char* outFileName;
  32140. } pkcs7EnvelopedVector;
  32141. static const byte asnDataOid[] = {
  32142. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01
  32143. };
  32144. /* ORI encrypt callback, responsible for encrypting content-encryption key (CEK)
  32145. * and giving wolfCrypt the value for oriOID and oriValue to place in
  32146. * OtherRecipientInfo.
  32147. *
  32148. * Returns 0 on success, negative upon error. */
  32149. static int myOriEncryptCb(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* oriType,
  32150. word32* oriTypeSz, byte* oriValue, word32* oriValueSz,
  32151. void* ctx)
  32152. {
  32153. int i;
  32154. /* make sure buffers are large enough */
  32155. if ((*oriValueSz < (2 + cekSz)) || (*oriTypeSz < sizeof(oriType)))
  32156. return -12140;
  32157. /* our simple encryption algorithm will be take the bitwise complement */
  32158. oriValue[0] = 0x04; /*ASN OCTET STRING */
  32159. oriValue[1] = (byte)cekSz; /* length */
  32160. for (i = 0; i < (int)cekSz; i++) {
  32161. oriValue[2 + i] = ~cek[i];
  32162. }
  32163. *oriValueSz = 2 + cekSz;
  32164. /* set oriType to ASN.1 encoded data OID */
  32165. XMEMCPY(oriType, asnDataOid, sizeof(asnDataOid));
  32166. *oriTypeSz = sizeof(asnDataOid);
  32167. (void)pkcs7;
  32168. (void)ctx;
  32169. return 0;
  32170. }
  32171. /* ORI decrypt callback, responsible for providing a decrypted content
  32172. * encryption key (CEK) placed into decryptedKey and size placed into
  32173. * decryptedKeySz. oriOID and oriValue are given to the callback to help
  32174. * in decrypting the encrypted CEK.
  32175. *
  32176. * Returns 0 on success, negative upon error. */
  32177. static int myOriDecryptCb(PKCS7* pkcs7, byte* oriType, word32 oriTypeSz,
  32178. byte* oriValue, word32 oriValueSz, byte* decryptedKey,
  32179. word32* decryptedKeySz, void* ctx)
  32180. {
  32181. int i;
  32182. /* make sure oriType matches what we expect */
  32183. if (oriTypeSz != sizeof(asnDataOid))
  32184. return -12150;
  32185. if (XMEMCMP(oriType, asnDataOid, sizeof(asnDataOid)) != 0)
  32186. return -12151;
  32187. /* make sure decrypted buffer is large enough */
  32188. if (*decryptedKeySz < oriValueSz)
  32189. return -12152;
  32190. /* decrypt encrypted CEK using simple bitwise complement,
  32191. only for example */
  32192. for (i = 0; i < (int)oriValueSz - 2; i++) {
  32193. decryptedKey[i] = ~oriValue[2 + i];
  32194. }
  32195. *decryptedKeySz = oriValueSz - 2;
  32196. (void)pkcs7;
  32197. (void)ctx;
  32198. return 0;
  32199. }
  32200. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  32201. /* returns 0 on success */
  32202. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  32203. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  32204. byte* in, int inSz, byte* out, void* usrCtx)
  32205. {
  32206. int keyId = -1, ret, keySz;
  32207. word32 keyIdSz = 8;
  32208. const byte* key;
  32209. byte keyIdRaw[8];
  32210. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  32211. Aes *aes;
  32212. #else
  32213. Aes aes[1];
  32214. #endif
  32215. /* looking for KEY ID
  32216. * fwDecryptKeyID OID "1.2.840.113549.1.9.16.2.37
  32217. */
  32218. WOLFSSL_SMALL_STACK_STATIC const unsigned char OID[] = {
  32219. /* 0x06, 0x0B do not pass in tag and length */
  32220. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  32221. 0x01, 0x09, 0x10, 0x02, 0x25
  32222. };
  32223. WOLFSSL_SMALL_STACK_STATIC const byte defKey[] = {
  32224. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  32225. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  32226. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  32227. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  32228. };
  32229. WOLFSSL_SMALL_STACK_STATIC const byte altKey[] = {
  32230. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  32231. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  32232. };
  32233. /* test user context passed in */
  32234. if (usrCtx == NULL || *(int*)usrCtx != 1) {
  32235. return -12160;
  32236. }
  32237. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  32238. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  32239. return -12164;
  32240. #endif
  32241. /* if needing to find keyIdSz can call with NULL */
  32242. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), NULL,
  32243. &keyIdSz);
  32244. if (ret != LENGTH_ONLY_E) {
  32245. printf("Unexpected error %d when getting keyIdSz\n", ret);
  32246. printf("Possibly no KEY ID attribute set\n");
  32247. ERROR_OUT(-12161, out);
  32248. }
  32249. else {
  32250. XMEMSET(keyIdRaw, 0, sizeof(keyIdRaw));
  32251. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), keyIdRaw,
  32252. &keyIdSz);
  32253. if (ret < 0) {
  32254. goto out;
  32255. }
  32256. if (keyIdSz < 3) {
  32257. printf("keyIdSz is smaller than expected\n");
  32258. ERROR_OUT(-12162, out);
  32259. }
  32260. if (keyIdSz > 2 + sizeof(int)) {
  32261. printf("example case was only expecting a keyId of int size\n");
  32262. ERROR_OUT(-12163, out);
  32263. }
  32264. /* keyIdRaw[0] OCTET TAG */
  32265. /* keyIdRaw[1] Length */
  32266. #ifdef BIG_ENDIAN_ORDER
  32267. if (keyIdRaw[1] == 0x01) {
  32268. keyId = 1;
  32269. }
  32270. #else
  32271. XMEMCPY(&keyId, keyIdRaw + 2, sizeof(keyId));
  32272. #endif
  32273. }
  32274. /* Use keyID here if found to select key and decrypt in HSM or in this
  32275. * example just select key and do software decryption */
  32276. if (keyId == 1) {
  32277. key = altKey;
  32278. keySz = sizeof(altKey);
  32279. }
  32280. else {
  32281. key = defKey;
  32282. keySz = sizeof(defKey);
  32283. }
  32284. switch (encryptOID) {
  32285. #ifdef WOLFSSL_AES_256
  32286. case AES256CBCb:
  32287. if ((keySz != 32 ) || (ivSz != AES_BLOCK_SIZE))
  32288. ERROR_OUT(BAD_FUNC_ARG, out);
  32289. break;
  32290. #endif
  32291. #ifdef WOLFSSL_AES_128
  32292. case AES128CBCb:
  32293. if ((keySz != 16 ) || (ivSz != AES_BLOCK_SIZE))
  32294. ERROR_OUT(BAD_FUNC_ARG, out);
  32295. break;
  32296. #endif
  32297. default:
  32298. printf("Unsupported content cipher type for example");
  32299. ERROR_OUT(ALGO_ID_E, out);
  32300. };
  32301. ret = wc_AesInit(aes, HEAP_HINT, INVALID_DEVID);
  32302. if (ret == 0) {
  32303. ret = wc_AesSetKey(aes, key, keySz, iv, AES_DECRYPTION);
  32304. if (ret == 0)
  32305. ret = wc_AesCbcDecrypt(aes, out, in, inSz);
  32306. wc_AesFree(aes);
  32307. }
  32308. out:
  32309. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  32310. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32311. #endif
  32312. (void)aad;
  32313. (void)aadSz;
  32314. (void)authTag;
  32315. (void)authTagSz;
  32316. return ret;
  32317. }
  32318. #endif /* !NO_AES && HAVE_AES_CBC */
  32319. #define PKCS7_BUF_SIZE 2048
  32320. static int pkcs7enveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  32321. byte* rsaPrivKey, word32 rsaPrivKeySz,
  32322. byte* eccCert, word32 eccCertSz,
  32323. byte* eccPrivKey, word32 eccPrivKeySz)
  32324. {
  32325. int ret = 0, testSz = 0, i;
  32326. int envelopedSz, decodedSz;
  32327. byte *enveloped = NULL;
  32328. byte *decoded = NULL;
  32329. PKCS7* pkcs7 = NULL;
  32330. #ifdef ECC_TIMING_RESISTANT
  32331. WC_RNG rng;
  32332. #endif
  32333. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  32334. XFILE pkcs7File;
  32335. #endif
  32336. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  32337. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  32338. 0x72,0x6c,0x64
  32339. };
  32340. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256) && \
  32341. defined(HAVE_ECC) && defined(WOLFSSL_SHA512)
  32342. byte optionalUkm[] = {
  32343. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  32344. };
  32345. #endif /* NO_AES */
  32346. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128) && \
  32347. !defined(NO_SHA)
  32348. /* encryption key for kekri recipient types */
  32349. WOLFSSL_SMALL_STACK_STATIC const byte secretKey[] = {
  32350. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  32351. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  32352. };
  32353. /* encryption key identifier */
  32354. WOLFSSL_SMALL_STACK_STATIC const byte secretKeyId[] = {
  32355. 0x02,0x02,0x03,0x04
  32356. };
  32357. #endif
  32358. #if !defined(NO_PWDBASED) && !defined(NO_SHA) && \
  32359. !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  32360. #ifndef HAVE_FIPS
  32361. WOLFSSL_SMALL_STACK_STATIC const char password[] = "password"; /* NOTE: Password is too short for FIPS */
  32362. #else
  32363. WOLFSSL_SMALL_STACK_STATIC const char password[] = "passwordFIPS_MODE";
  32364. #endif
  32365. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = {
  32366. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  32367. };
  32368. #endif
  32369. #define MAX_TESTVECTORS_LEN 13
  32370. #define ADD_PKCS7ENVELOPEDVECTOR(...) { \
  32371. pkcs7EnvelopedVector _this_vector = { __VA_ARGS__ }; \
  32372. if (testSz == MAX_TESTVECTORS_LEN) { \
  32373. ret = -12534; \
  32374. goto out; \
  32375. } \
  32376. XMEMCPY(&testVectors[testSz++], &_this_vector, sizeof _this_vector);\
  32377. }
  32378. pkcs7EnvelopedVector *testVectors = NULL;
  32379. #ifdef ECC_TIMING_RESISTANT
  32380. XMEMSET(&rng, 0, sizeof(rng));
  32381. #endif
  32382. testVectors = (pkcs7EnvelopedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  32383. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32384. if (testVectors == NULL) {
  32385. ret = -12534;
  32386. goto out;
  32387. }
  32388. {
  32389. /* key transport key encryption technique */
  32390. #ifndef NO_RSA
  32391. #ifndef NO_DES3
  32392. ADD_PKCS7ENVELOPEDVECTOR(
  32393. data, (word32)sizeof(data), DATA, DES3b, 0, 0, rsaCert, rsaCertSz,
  32394. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  32395. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  32396. "pkcs7envelopedDataDES3.der");
  32397. #endif
  32398. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  32399. #ifdef WOLFSSL_AES_128
  32400. ADD_PKCS7ENVELOPEDVECTOR(
  32401. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, rsaCert, rsaCertSz,
  32402. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  32403. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  32404. "pkcs7envelopedDataAES128CBC.der");
  32405. #endif
  32406. #ifdef WOLFSSL_AES_192
  32407. ADD_PKCS7ENVELOPEDVECTOR(
  32408. data, (word32)sizeof(data), DATA, AES192CBCb, 0, 0, rsaCert, rsaCertSz,
  32409. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  32410. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  32411. "pkcs7envelopedDataAES192CBC.der");
  32412. #endif
  32413. #ifdef WOLFSSL_AES_256
  32414. ADD_PKCS7ENVELOPEDVECTOR(
  32415. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  32416. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  32417. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  32418. "pkcs7envelopedDataAES256CBC.der");
  32419. /* explicitly using SKID for SubjectKeyIdentifier */
  32420. ADD_PKCS7ENVELOPEDVECTOR(
  32421. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  32422. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_SKID, 0, NULL, 0, NULL, 0, NULL,
  32423. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  32424. "pkcs7envelopedDataAES256CBC_SKID.der");
  32425. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  32426. ADD_PKCS7ENVELOPEDVECTOR(
  32427. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  32428. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_ISSUER_AND_SERIAL_NUMBER, 0,
  32429. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  32430. 0, 0, 0, 0, "pkcs7envelopedDataAES256CBC_IANDS.der");
  32431. #endif
  32432. #endif /* !NO_AES && HAVE_AES_CBC */
  32433. #endif
  32434. /* key agreement key encryption technique*/
  32435. #ifdef HAVE_ECC
  32436. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  32437. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  32438. ADD_PKCS7ENVELOPEDVECTOR(
  32439. data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP,
  32440. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  32441. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  32442. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  32443. "pkcs7envelopedDataAES128CBC_ECDH_SHA1KDF.der");
  32444. #endif
  32445. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  32446. ADD_PKCS7ENVELOPEDVECTOR(
  32447. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  32448. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  32449. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  32450. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  32451. "pkcs7envelopedDataAES256CBC_ECDH_SHA256KDF.der");
  32452. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  32453. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  32454. ADD_PKCS7ENVELOPEDVECTOR(
  32455. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  32456. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  32457. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  32458. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  32459. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF.der");
  32460. /* with optional user keying material (ukm) */
  32461. ADD_PKCS7ENVELOPEDVECTOR(
  32462. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  32463. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  32464. eccPrivKeySz, optionalUkm, sizeof(optionalUkm), 0, 0, NULL, 0,
  32465. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  32466. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF_ukm.der");
  32467. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  32468. #endif /* !NO_AES && HAVE_AES_CBC */
  32469. #endif
  32470. /* kekri (KEKRecipientInfo) recipient types */
  32471. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  32472. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  32473. ADD_PKCS7ENVELOPEDVECTOR(
  32474. data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP, 0,
  32475. NULL, 0, NULL, 0, NULL, 0, 0, 0, secretKey, sizeof(secretKey),
  32476. secretKeyId, sizeof(secretKeyId), NULL, NULL, 0, NULL, 0,
  32477. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  32478. "pkcs7envelopedDataAES128CBC_KEKRI.der");
  32479. #endif
  32480. #endif /* !NO_AES && HAVE_AES_CBC */
  32481. /* pwri (PasswordRecipientInfo) recipient types */
  32482. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  32483. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  32484. ADD_PKCS7ENVELOPEDVECTOR(
  32485. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0,
  32486. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  32487. NULL, 0, NULL, NULL, 0, NULL, 0, 0, password,
  32488. (word32)XSTRLEN(password), salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  32489. 0, 0, 0, "pkcs7envelopedDataAES128CBC_PWRI.der");
  32490. #endif
  32491. #endif
  32492. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_128)
  32493. /* ori (OtherRecipientInfo) recipient types */
  32494. ADD_PKCS7ENVELOPEDVECTOR(
  32495. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, NULL, 0, NULL, 0,
  32496. NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0,
  32497. NULL, 0, 0, 0, 0, 0, 1, 0, "pkcs7envelopedDataAES128CBC_ORI.der");
  32498. #endif
  32499. };
  32500. #undef MAX_TESTVECTORS_LEN
  32501. #undef ADD_PKCS7ENVELOPEDVECTOR
  32502. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32503. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32504. if ((! enveloped) || (! decoded)) {
  32505. ERROR_OUT(-12170, out);
  32506. }
  32507. #ifdef ECC_TIMING_RESISTANT
  32508. #ifndef HAVE_FIPS
  32509. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  32510. #else
  32511. ret = wc_InitRng(&rng);
  32512. #endif
  32513. if (ret != 0) {
  32514. ERROR_OUT(-12171, out);
  32515. }
  32516. #endif
  32517. for (i = 0; i < testSz; i++) {
  32518. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  32519. #ifdef WOLFSSL_ASYNC_CRYPT
  32520. INVALID_DEVID /* async PKCS7 is not supported */
  32521. #else
  32522. devId
  32523. #endif
  32524. );
  32525. if (pkcs7 == NULL) {
  32526. ERROR_OUT(-12172, out);
  32527. }
  32528. if (testVectors[i].secretKey != NULL) {
  32529. /* KEKRI recipient type */
  32530. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  32531. if (ret != 0) {
  32532. ERROR_OUT(-12173, out);
  32533. }
  32534. pkcs7->content = (byte*)testVectors[i].content;
  32535. pkcs7->contentSz = testVectors[i].contentSz;
  32536. pkcs7->contentOID = testVectors[i].contentOID;
  32537. pkcs7->encryptOID = testVectors[i].encryptOID;
  32538. pkcs7->ukm = testVectors[i].optionalUkm;
  32539. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  32540. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  32541. (byte *)testVectors[i].secretKey, testVectors[i].secretKeySz,
  32542. (byte *)testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  32543. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  32544. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  32545. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  32546. if (ret < 0) {
  32547. wc_PKCS7_Free(pkcs7);
  32548. ERROR_OUT(-12174, out);
  32549. }
  32550. /* set key, for decryption */
  32551. ret = wc_PKCS7_SetKey(pkcs7, (byte *)testVectors[i].secretKey,
  32552. testVectors[i].secretKeySz);
  32553. if (ret != 0) {
  32554. wc_PKCS7_Free(pkcs7);
  32555. ERROR_OUT(-12175, out);
  32556. }
  32557. } else if (testVectors[i].password != NULL) {
  32558. #if !defined(NO_PWDBASED) && !defined(NO_SHA)
  32559. /* PWRI recipient type */
  32560. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  32561. if (ret != 0) {
  32562. ERROR_OUT(-12176, out);
  32563. }
  32564. pkcs7->content = (byte*)testVectors[i].content;
  32565. pkcs7->contentSz = testVectors[i].contentSz;
  32566. pkcs7->contentOID = testVectors[i].contentOID;
  32567. pkcs7->encryptOID = testVectors[i].encryptOID;
  32568. pkcs7->ukm = testVectors[i].optionalUkm;
  32569. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  32570. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  32571. (byte *)testVectors[i].password, testVectors[i].passwordSz,
  32572. (byte *)testVectors[i].salt, testVectors[i].saltSz,
  32573. testVectors[i].kdfOID,
  32574. testVectors[i].hashOID, testVectors[i].kdfIterations,
  32575. testVectors[i].encryptOID, testVectors[i].pwriOptions);
  32576. if (ret < 0) {
  32577. wc_PKCS7_Free(pkcs7);
  32578. ERROR_OUT(-12177, out);
  32579. }
  32580. /* set password, for decryption */
  32581. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  32582. testVectors[i].passwordSz);
  32583. if (ret < 0) {
  32584. wc_PKCS7_Free(pkcs7);
  32585. ERROR_OUT(-12178, out);
  32586. }
  32587. #endif /* ! NO_PWDBASED && ! NO_SHA */
  32588. } else if (testVectors[i].isOri == 1) {
  32589. /* ORI recipient type */
  32590. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  32591. if (ret != 0) {
  32592. ERROR_OUT(-12179, out);
  32593. }
  32594. pkcs7->content = (byte*)testVectors[i].content;
  32595. pkcs7->contentSz = testVectors[i].contentSz;
  32596. pkcs7->contentOID = testVectors[i].contentOID;
  32597. pkcs7->encryptOID = testVectors[i].encryptOID;
  32598. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  32599. testVectors[i].oriOptions);
  32600. if (ret < 0) {
  32601. wc_PKCS7_Free(pkcs7);
  32602. ERROR_OUT(-12180, out);
  32603. }
  32604. /* set decrypt callback for decryption */
  32605. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  32606. if (ret < 0) {
  32607. wc_PKCS7_Free(pkcs7);
  32608. ERROR_OUT(-12181, out);
  32609. }
  32610. } else {
  32611. /* KTRI or KARI recipient types */
  32612. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  32613. if (ret != 0) {
  32614. ERROR_OUT(-12182, out);
  32615. }
  32616. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  32617. (word32)testVectors[i].certSz);
  32618. if (ret != 0) {
  32619. wc_PKCS7_Free(pkcs7);
  32620. ERROR_OUT(-12183, out);
  32621. }
  32622. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  32623. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  32624. pkcs7->privateKey = testVectors[i].privateKey;
  32625. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  32626. pkcs7->content = (byte*)testVectors[i].content;
  32627. pkcs7->contentSz = testVectors[i].contentSz;
  32628. pkcs7->contentOID = testVectors[i].contentOID;
  32629. pkcs7->encryptOID = testVectors[i].encryptOID;
  32630. pkcs7->ukm = testVectors[i].optionalUkm;
  32631. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  32632. /* set SubjectIdentifier type for KTRI types */
  32633. if (testVectors[i].ktriOptions & CMS_SKID) {
  32634. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  32635. if (ret != 0) {
  32636. wc_PKCS7_Free(pkcs7);
  32637. ERROR_OUT(-12184, out);
  32638. }
  32639. } else if (testVectors[i].ktriOptions &
  32640. CMS_ISSUER_AND_SERIAL_NUMBER) {
  32641. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  32642. CMS_ISSUER_AND_SERIAL_NUMBER);
  32643. if (ret != 0) {
  32644. wc_PKCS7_Free(pkcs7);
  32645. ERROR_OUT(-12185, out);
  32646. }
  32647. }
  32648. }
  32649. #ifdef ECC_TIMING_RESISTANT
  32650. pkcs7->rng = &rng;
  32651. #endif
  32652. /* encode envelopedData */
  32653. envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, enveloped,
  32654. PKCS7_BUF_SIZE);
  32655. if (envelopedSz <= 0) {
  32656. wc_PKCS7_Free(pkcs7);
  32657. ERROR_OUT(-12186, out);
  32658. }
  32659. /* decode envelopedData */
  32660. pkcs7->contentOID = 0;
  32661. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped, envelopedSz,
  32662. decoded, PKCS7_BUF_SIZE);
  32663. if (pkcs7->contentOID != testVectors[i].contentOID ||
  32664. decodedSz <= 0) {
  32665. wc_PKCS7_Free(pkcs7);
  32666. ERROR_OUT(-12187, out);
  32667. }
  32668. /* test decode result */
  32669. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  32670. wc_PKCS7_Free(pkcs7);
  32671. ERROR_OUT(-12188, out);
  32672. }
  32673. #ifndef NO_PKCS7_STREAM
  32674. { /* test reading byte by byte */
  32675. int z;
  32676. for (z = 0; z < envelopedSz; z++) {
  32677. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped + z, 1,
  32678. decoded, PKCS7_BUF_SIZE);
  32679. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  32680. printf("unexpected error %d\n", decodedSz);
  32681. ERROR_OUT(-12189, out);
  32682. }
  32683. }
  32684. /* test decode result */
  32685. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  32686. printf("stream read compare failed\n");
  32687. wc_PKCS7_Free(pkcs7);
  32688. ERROR_OUT(-12190, out);
  32689. }
  32690. }
  32691. #endif
  32692. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  32693. /* output pkcs7 envelopedData for external testing */
  32694. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  32695. if (!pkcs7File) {
  32696. wc_PKCS7_Free(pkcs7);
  32697. ERROR_OUT(-12191, out);
  32698. }
  32699. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  32700. XFCLOSE(pkcs7File);
  32701. if (ret != envelopedSz) {
  32702. wc_PKCS7_Free(pkcs7);
  32703. ERROR_OUT(-12192, out);
  32704. } else {
  32705. /* reset ret to 0 for success */
  32706. ret = 0;
  32707. }
  32708. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  32709. wc_PKCS7_Free(pkcs7);
  32710. pkcs7 = NULL;
  32711. }
  32712. #ifdef ECC_TIMING_RESISTANT
  32713. wc_FreeRng(&rng);
  32714. #endif
  32715. (void)eccCert;
  32716. (void)eccCertSz;
  32717. (void)eccPrivKey;
  32718. (void)eccPrivKeySz;
  32719. (void)rsaCert;
  32720. (void)rsaCertSz;
  32721. (void)rsaPrivKey;
  32722. (void)rsaPrivKeySz;
  32723. out:
  32724. if (testVectors)
  32725. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32726. if (enveloped)
  32727. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32728. if (decoded)
  32729. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32730. return ret;
  32731. }
  32732. WOLFSSL_TEST_SUBROUTINE int pkcs7enveloped_test(void)
  32733. {
  32734. int ret = 0;
  32735. byte* rsaCert = NULL;
  32736. byte* rsaPrivKey = NULL;
  32737. word32 rsaCertSz = 0;
  32738. word32 rsaPrivKeySz = 0;
  32739. byte* eccCert = NULL;
  32740. byte* eccPrivKey = NULL;
  32741. word32 eccCertSz = 0;
  32742. word32 eccPrivKeySz = 0;
  32743. #ifndef NO_RSA
  32744. /* read client RSA cert and key in DER format */
  32745. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32746. if (rsaCert == NULL)
  32747. return -12200;
  32748. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32749. if (rsaPrivKey == NULL) {
  32750. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32751. return -12201;
  32752. }
  32753. rsaCertSz = FOURK_BUF;
  32754. rsaPrivKeySz = FOURK_BUF;
  32755. #endif /* NO_RSA */
  32756. #ifdef HAVE_ECC
  32757. /* read client ECC cert and key in DER format */
  32758. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32759. if (eccCert == NULL) {
  32760. #ifndef NO_RSA
  32761. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32762. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32763. #endif
  32764. return -12202;
  32765. }
  32766. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32767. if (eccPrivKey == NULL) {
  32768. #ifndef NO_RSA
  32769. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32770. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32771. #endif
  32772. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32773. return -12203;
  32774. }
  32775. eccCertSz = FOURK_BUF;
  32776. eccPrivKeySz = FOURK_BUF;
  32777. #endif /* HAVE_ECC */
  32778. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  32779. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  32780. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  32781. eccPrivKey, &eccPrivKeySz);
  32782. if (ret < 0) {
  32783. #ifndef NO_RSA
  32784. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32785. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32786. #endif
  32787. #ifdef HAVE_ECC
  32788. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32789. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32790. #endif
  32791. return -12204;
  32792. }
  32793. ret = pkcs7enveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  32794. rsaPrivKey, (word32)rsaPrivKeySz,
  32795. eccCert, (word32)eccCertSz,
  32796. eccPrivKey, (word32)eccPrivKeySz);
  32797. #ifndef NO_RSA
  32798. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32799. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32800. #endif
  32801. #ifdef HAVE_ECC
  32802. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32803. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32804. #endif
  32805. return ret;
  32806. }
  32807. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  32808. typedef struct {
  32809. const byte* content;
  32810. word32 contentSz;
  32811. int contentOID;
  32812. int encryptOID;
  32813. int keyWrapOID;
  32814. int keyAgreeOID;
  32815. byte* cert;
  32816. size_t certSz;
  32817. byte* privateKey;
  32818. word32 privateKeySz;
  32819. PKCS7Attrib* authAttribs;
  32820. word32 authAttribsSz;
  32821. PKCS7Attrib* unauthAttribs;
  32822. word32 unauthAttribsSz;
  32823. /* KARI / KTRI specific */
  32824. byte* optionalUkm;
  32825. word32 optionalUkmSz;
  32826. int ktriOptions; /* KTRI options flags */
  32827. int kariOptions; /* KARI options flags */
  32828. /* KEKRI specific */
  32829. byte* secretKey; /* key, only for kekri RecipientInfo types */
  32830. word32 secretKeySz; /* size of secretKey, bytes */
  32831. byte* secretKeyId; /* key identifier */
  32832. word32 secretKeyIdSz; /* size of key identifier, bytes */
  32833. void* timePtr; /* time_t pointer */
  32834. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  32835. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  32836. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  32837. word32 otherAttrSz; /* size of otherAttr, bytes */
  32838. int kekriOptions; /* KEKRI options flags */
  32839. /* PWRI specific */
  32840. char* password; /* password */
  32841. word32 passwordSz; /* password size, bytes */
  32842. byte* salt; /* KDF salt */
  32843. word32 saltSz; /* KDF salt size, bytes */
  32844. int kdfOID; /* KDF OID */
  32845. int hashOID; /* KDF hash algorithm OID */
  32846. int kdfIterations; /* KDF iterations */
  32847. int kekEncryptOID; /* KEK encryption algorithm OID */
  32848. int pwriOptions; /* PWRI options flags */
  32849. /* ORI specific */
  32850. int isOri;
  32851. int oriOptions; /* ORI options flags */
  32852. const char* outFileName;
  32853. } pkcs7AuthEnvelopedVector;
  32854. static int pkcs7authenveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  32855. byte* rsaPrivKey, word32 rsaPrivKeySz,
  32856. byte* eccCert, word32 eccCertSz,
  32857. byte* eccPrivKey, word32 eccPrivKeySz)
  32858. {
  32859. int ret = 0, testSz = 0, i;
  32860. int envelopedSz, decodedSz;
  32861. byte *enveloped = NULL;
  32862. byte *decoded = NULL;
  32863. WC_RNG rng;
  32864. PKCS7* pkcs7;
  32865. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  32866. XFILE pkcs7File;
  32867. #endif
  32868. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  32869. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  32870. 0x72,0x6c,0x64
  32871. };
  32872. byte senderNonce[PKCS7_NONCE_SZ + 2];
  32873. #ifdef HAVE_ECC
  32874. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  32875. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  32876. WOLFSSL_SMALL_STACK_STATIC const byte senderNonceOid[] =
  32877. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  32878. 0x09, 0x05 };
  32879. PKCS7Attrib attribs[] =
  32880. {
  32881. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  32882. sizeof(senderNonce) }
  32883. };
  32884. #endif
  32885. #endif
  32886. #endif
  32887. #if !defined(NO_AES) && defined(WOLFSSL_AES_256) && defined(HAVE_ECC) && \
  32888. defined(WOLFSSL_SHA512)
  32889. WOLFSSL_SMALL_STACK_STATIC const byte optionalUkm[] = {
  32890. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  32891. };
  32892. #endif /* NO_AES */
  32893. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  32894. /* encryption key for kekri recipient types */
  32895. WOLFSSL_SMALL_STACK_STATIC const byte secretKey[] = {
  32896. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  32897. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  32898. };
  32899. /* encryption key identifier */
  32900. WOLFSSL_SMALL_STACK_STATIC const byte secretKeyId[] = {
  32901. 0x02,0x02,0x03,0x04
  32902. };
  32903. #endif
  32904. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  32905. !defined(NO_SHA) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  32906. #ifndef HAVE_FIPS
  32907. WOLFSSL_SMALL_STACK_STATIC const char password[] = "password";
  32908. #else
  32909. WOLFSSL_SMALL_STACK_STATIC const char password[] = "passwordFIPS_MODE";
  32910. #endif
  32911. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = {
  32912. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  32913. };
  32914. #endif
  32915. #define MAX_TESTVECTORS_LEN 20
  32916. #define ADD_PKCS7AUTHENVELOPEDVECTOR(...) { \
  32917. pkcs7AuthEnvelopedVector _this_vector = { __VA_ARGS__ }; \
  32918. if (testSz == MAX_TESTVECTORS_LEN) { \
  32919. ret = -12534; \
  32920. goto out; \
  32921. } \
  32922. XMEMCPY(&testVectors[testSz++], &_this_vector, sizeof _this_vector);\
  32923. }
  32924. pkcs7AuthEnvelopedVector *testVectors = NULL;
  32925. XMEMSET(&rng, 0, sizeof(rng));
  32926. testVectors = (pkcs7AuthEnvelopedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  32927. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32928. if (testVectors == NULL) {
  32929. ret = -12534;
  32930. goto out;
  32931. }
  32932. {
  32933. /* key transport key encryption technique */
  32934. #ifndef NO_RSA
  32935. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  32936. #ifdef WOLFSSL_AES_128
  32937. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32938. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, rsaCert, rsaCertSz,
  32939. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  32940. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  32941. 0, 0, "pkcs7authEnvelopedDataAES128GCM.der");
  32942. #endif
  32943. #ifdef WOLFSSL_AES_192
  32944. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32945. data, (word32)sizeof(data), DATA, AES192GCMb, 0, 0, rsaCert, rsaCertSz,
  32946. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  32947. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  32948. 0, 0, "pkcs7authEnvelopedDataAES192GCM.der");
  32949. #endif
  32950. #ifdef WOLFSSL_AES_256
  32951. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32952. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  32953. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  32954. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  32955. 0, 0, "pkcs7authEnvelopedDataAES256GCM.der");
  32956. /* test with contentType set to FirmwarePkgData */
  32957. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32958. data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, 0, 0,
  32959. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL,
  32960. 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL,
  32961. 0, 0, 0, 0, 0, 0, 0, 0,
  32962. "pkcs7authEnvelopedDataAES256GCM_firmwarePkgData.der");
  32963. /* explicitly using SKID for SubjectKeyIdentifier */
  32964. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32965. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  32966. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, CMS_SKID, 0,
  32967. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  32968. 0, 0, 0, 0, 0, "pkcs7authEnvelopedDataAES256GCM_SKID.der");
  32969. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  32970. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32971. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  32972. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0,
  32973. CMS_ISSUER_AND_SERIAL_NUMBER, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  32974. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  32975. "pkcs7authEnvelopedDataAES256GCM_IANDS.der");
  32976. #endif
  32977. #endif /* NO_AES */
  32978. #endif
  32979. /* key agreement key encryption technique*/
  32980. #ifdef HAVE_ECC
  32981. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  32982. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  32983. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32984. data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP,
  32985. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  32986. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  32987. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  32988. "pkcs7authEnvelopedDataAES128GCM_ECDH_SHA1KDF.der");
  32989. #endif
  32990. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  32991. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32992. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  32993. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  32994. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  32995. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  32996. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF.der");
  32997. /* with authenticated attributes */
  32998. ADD_PKCS7AUTHENVELOPEDVECTOR(
  32999. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  33000. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  33001. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  33002. NULL, 0, NULL, 0, 0, 0, NULL, 0,
  33003. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  33004. 0, 0, 0,
  33005. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_authAttribs.der");
  33006. /* with unauthenticated attributes */
  33007. ADD_PKCS7AUTHENVELOPEDVECTOR(
  33008. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  33009. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  33010. eccPrivKeySz, NULL, 0, attribs,
  33011. (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0, NULL, 0,
  33012. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  33013. 0, 0, 0,
  33014. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_unauthAttribs.der");
  33015. /* with authenticated AND unauthenticated attributes */
  33016. ADD_PKCS7AUTHENVELOPEDVECTOR(
  33017. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  33018. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  33019. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  33020. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  33021. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  33022. 0, 0, 0, 0, 0, 0,
  33023. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_bothAttribs.der");
  33024. /* with authenticated AND unauthenticated attributes AND
  33025. * contentType of FirmwarePkgData */
  33026. ADD_PKCS7AUTHENVELOPEDVECTOR(
  33027. data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, AES256_WRAP,
  33028. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  33029. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  33030. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  33031. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  33032. 0, 0, 0, 0, 0, 0,
  33033. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_fw_bothAttribs.der");
  33034. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  33035. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  33036. ADD_PKCS7AUTHENVELOPEDVECTOR(
  33037. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  33038. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  33039. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL,
  33040. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  33041. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF.der");
  33042. /* with optional user keying material (ukm) */
  33043. ADD_PKCS7AUTHENVELOPEDVECTOR(
  33044. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  33045. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  33046. eccPrivKeySz, NULL, 0, NULL, 0, (byte *)optionalUkm, sizeof(optionalUkm), 0,
  33047. 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  33048. 0, 0, 0, 0, 0, 0,
  33049. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF_ukm.der");
  33050. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  33051. #endif /* NO_AES */
  33052. #endif
  33053. /* kekri (KEKRecipientInfo) recipient types */
  33054. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  33055. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  33056. ADD_PKCS7AUTHENVELOPEDVECTOR(
  33057. data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP, 0,
  33058. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0,
  33059. (byte *)secretKey, sizeof(secretKey), (byte *)secretKeyId, sizeof(secretKeyId),
  33060. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  33061. "pkcs7authEnvelopedDataAES128GCM_KEKRI.der");
  33062. #endif
  33063. #endif
  33064. /* pwri (PasswordRecipientInfo) recipient types */
  33065. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM)
  33066. #if !defined(NO_SHA) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  33067. ADD_PKCS7AUTHENVELOPEDVECTOR(
  33068. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0,
  33069. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  33070. NULL, 0, NULL, NULL, 0, NULL, 0, 0, (char *)password,
  33071. (word32)XSTRLEN(password), (byte *)salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  33072. AES128CBCb, 0, 0, 0, "pkcs7authEnvelopedDataAES128GCM_PWRI.der");
  33073. #endif
  33074. #endif
  33075. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  33076. #ifdef WOLFSSL_AES_128
  33077. /* ori (OtherRecipientInfo) recipient types */
  33078. ADD_PKCS7AUTHENVELOPEDVECTOR(
  33079. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, NULL, 0, NULL, 0,
  33080. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  33081. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 1, 0,
  33082. "pkcs7authEnvelopedDataAES128GCM_ORI.der");
  33083. #endif
  33084. #endif
  33085. }
  33086. #undef MAX_TESTVECTORS_LEN
  33087. #undef ADD_PKCS7AUTHENVELOPEDVECTOR
  33088. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33089. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33090. if ((! enveloped) || (! decoded)) {
  33091. ERROR_OUT(-12210, out);
  33092. }
  33093. /* generate senderNonce */
  33094. {
  33095. #ifndef HAVE_FIPS
  33096. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  33097. #else
  33098. ret = wc_InitRng(&rng);
  33099. #endif
  33100. if (ret != 0) {
  33101. ERROR_OUT(-12211, out);
  33102. }
  33103. senderNonce[0] = 0x04;
  33104. senderNonce[1] = PKCS7_NONCE_SZ;
  33105. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  33106. if (ret != 0) {
  33107. wc_FreeRng(&rng);
  33108. ERROR_OUT(-12212, out);
  33109. }
  33110. }
  33111. for (i = 0; i < testSz; i++) {
  33112. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  33113. #ifdef WOLFSSL_ASYNC_CRYPT
  33114. INVALID_DEVID /* async PKCS7 is not supported */
  33115. #else
  33116. devId
  33117. #endif
  33118. );
  33119. if (pkcs7 == NULL) {
  33120. ERROR_OUT(-12213, out);
  33121. }
  33122. if (testVectors[i].secretKey != NULL) {
  33123. /* KEKRI recipient type */
  33124. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  33125. if (ret != 0) {
  33126. ERROR_OUT(-12214, out);
  33127. }
  33128. pkcs7->content = (byte*)testVectors[i].content;
  33129. pkcs7->contentSz = testVectors[i].contentSz;
  33130. pkcs7->contentOID = testVectors[i].contentOID;
  33131. pkcs7->encryptOID = testVectors[i].encryptOID;
  33132. pkcs7->ukm = testVectors[i].optionalUkm;
  33133. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  33134. pkcs7->authAttribs = testVectors[i].authAttribs;
  33135. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  33136. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  33137. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  33138. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  33139. testVectors[i].secretKey, testVectors[i].secretKeySz,
  33140. testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  33141. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  33142. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  33143. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  33144. if (ret < 0) {
  33145. wc_PKCS7_Free(pkcs7);
  33146. ERROR_OUT(-12215, out);
  33147. }
  33148. /* set key, for decryption */
  33149. ret = wc_PKCS7_SetKey(pkcs7, testVectors[i].secretKey,
  33150. testVectors[i].secretKeySz);
  33151. if (ret != 0) {
  33152. wc_PKCS7_Free(pkcs7);
  33153. ERROR_OUT(-12216, out);
  33154. }
  33155. } else if (testVectors[i].password != NULL) {
  33156. #if !defined(NO_PWDBASED) && !defined(NO_SHA)
  33157. /* PWRI recipient type */
  33158. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  33159. if (ret != 0) {
  33160. ERROR_OUT(-12217, out);
  33161. }
  33162. pkcs7->content = (byte*)testVectors[i].content;
  33163. pkcs7->contentSz = testVectors[i].contentSz;
  33164. pkcs7->contentOID = testVectors[i].contentOID;
  33165. pkcs7->encryptOID = testVectors[i].encryptOID;
  33166. pkcs7->ukm = testVectors[i].optionalUkm;
  33167. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  33168. pkcs7->authAttribs = testVectors[i].authAttribs;
  33169. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  33170. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  33171. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  33172. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  33173. (byte*)testVectors[i].password,
  33174. testVectors[i].passwordSz, testVectors[i].salt,
  33175. testVectors[i].saltSz, testVectors[i].kdfOID,
  33176. testVectors[i].hashOID, testVectors[i].kdfIterations,
  33177. testVectors[i].kekEncryptOID, testVectors[i].pwriOptions);
  33178. if (ret < 0) {
  33179. wc_PKCS7_Free(pkcs7);
  33180. ERROR_OUT(-12218, out);
  33181. }
  33182. /* set password, for decryption */
  33183. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  33184. testVectors[i].passwordSz);
  33185. if (ret < 0) {
  33186. wc_PKCS7_Free(pkcs7);
  33187. ERROR_OUT(-12219, out);
  33188. }
  33189. #endif /* ! NO_PWDBASED && ! NO_SHA */
  33190. } else if (testVectors[i].isOri == 1) {
  33191. /* ORI recipient type */
  33192. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  33193. if (ret != 0) {
  33194. ERROR_OUT(-12220, out);
  33195. }
  33196. pkcs7->content = (byte*)testVectors[i].content;
  33197. pkcs7->contentSz = testVectors[i].contentSz;
  33198. pkcs7->contentOID = testVectors[i].contentOID;
  33199. pkcs7->encryptOID = testVectors[i].encryptOID;
  33200. pkcs7->authAttribs = testVectors[i].authAttribs;
  33201. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  33202. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  33203. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  33204. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  33205. testVectors[i].oriOptions);
  33206. if (ret < 0) {
  33207. wc_PKCS7_Free(pkcs7);
  33208. ERROR_OUT(-12221, out);
  33209. }
  33210. /* set decrypt callback for decryption */
  33211. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  33212. if (ret < 0) {
  33213. wc_PKCS7_Free(pkcs7);
  33214. ERROR_OUT(-12222, out);
  33215. }
  33216. } else {
  33217. /* KTRI or KARI recipient types */
  33218. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  33219. (word32)testVectors[i].certSz);
  33220. if (ret != 0) {
  33221. wc_PKCS7_Free(pkcs7);
  33222. ERROR_OUT(-12223, out);
  33223. }
  33224. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  33225. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  33226. pkcs7->privateKey = testVectors[i].privateKey;
  33227. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  33228. pkcs7->content = (byte*)testVectors[i].content;
  33229. pkcs7->contentSz = testVectors[i].contentSz;
  33230. pkcs7->contentOID = testVectors[i].contentOID;
  33231. pkcs7->encryptOID = testVectors[i].encryptOID;
  33232. pkcs7->ukm = testVectors[i].optionalUkm;
  33233. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  33234. pkcs7->authAttribs = testVectors[i].authAttribs;
  33235. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  33236. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  33237. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  33238. /* set SubjectIdentifier type for KTRI types */
  33239. if (testVectors[i].ktriOptions & CMS_SKID) {
  33240. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  33241. if (ret != 0) {
  33242. wc_PKCS7_Free(pkcs7);
  33243. ERROR_OUT(-12224, out);
  33244. }
  33245. } else if (testVectors[i].ktriOptions &
  33246. CMS_ISSUER_AND_SERIAL_NUMBER) {
  33247. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  33248. CMS_ISSUER_AND_SERIAL_NUMBER);
  33249. if (ret != 0) {
  33250. wc_PKCS7_Free(pkcs7);
  33251. ERROR_OUT(-12225, out);
  33252. }
  33253. }
  33254. }
  33255. #ifdef ECC_TIMING_RESISTANT
  33256. pkcs7->rng = &rng;
  33257. #endif
  33258. /* encode envelopedData */
  33259. envelopedSz = wc_PKCS7_EncodeAuthEnvelopedData(pkcs7, enveloped,
  33260. PKCS7_BUF_SIZE);
  33261. if (envelopedSz <= 0) {
  33262. wc_PKCS7_Free(pkcs7);
  33263. ERROR_OUT(-12226, out);
  33264. }
  33265. #ifndef NO_PKCS7_STREAM
  33266. { /* test reading byte by byte */
  33267. int z;
  33268. for (z = 0; z < envelopedSz; z++) {
  33269. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7,
  33270. enveloped + z, 1, decoded, PKCS7_BUF_SIZE);
  33271. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  33272. printf("unexpected error %d\n", decodedSz);
  33273. ERROR_OUT(-12227, out);
  33274. }
  33275. }
  33276. /* test decode result */
  33277. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  33278. printf("stream read compare failed\n");
  33279. wc_PKCS7_Free(pkcs7);
  33280. ERROR_OUT(-12228, out);
  33281. }
  33282. }
  33283. #endif
  33284. /* decode envelopedData */
  33285. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7, enveloped,
  33286. envelopedSz, decoded,
  33287. PKCS7_BUF_SIZE);
  33288. if (decodedSz <= 0) {
  33289. wc_PKCS7_Free(pkcs7);
  33290. ERROR_OUT(-12229, out);
  33291. }
  33292. /* test decode result */
  33293. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  33294. wc_PKCS7_Free(pkcs7);
  33295. ERROR_OUT(-12230, out);
  33296. }
  33297. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  33298. /* output pkcs7 envelopedData for external testing */
  33299. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  33300. if (!pkcs7File) {
  33301. wc_PKCS7_Free(pkcs7);
  33302. ERROR_OUT(-12231, out);
  33303. }
  33304. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  33305. XFCLOSE(pkcs7File);
  33306. if (ret != envelopedSz) {
  33307. wc_PKCS7_Free(pkcs7);
  33308. ERROR_OUT(-12232, out);
  33309. } else {
  33310. /* reset ret to 0 for success */
  33311. ret = 0;
  33312. }
  33313. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  33314. wc_PKCS7_Free(pkcs7);
  33315. pkcs7 = NULL;
  33316. }
  33317. wc_FreeRng(&rng);
  33318. (void)eccCert;
  33319. (void)eccCertSz;
  33320. (void)eccPrivKey;
  33321. (void)eccPrivKeySz;
  33322. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  33323. (void)secretKey;
  33324. (void)secretKeyId;
  33325. #endif
  33326. #ifdef NO_RSA
  33327. (void)rsaCert;
  33328. (void)rsaCertSz;
  33329. (void)rsaPrivKey;
  33330. (void)rsaPrivKeySz;
  33331. #endif
  33332. out:
  33333. if (testVectors)
  33334. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33335. if (enveloped)
  33336. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33337. if (decoded)
  33338. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33339. return ret;
  33340. }
  33341. WOLFSSL_TEST_SUBROUTINE int pkcs7authenveloped_test(void)
  33342. {
  33343. int ret = 0;
  33344. byte* rsaCert = NULL;
  33345. byte* rsaPrivKey = NULL;
  33346. word32 rsaCertSz = 0;
  33347. word32 rsaPrivKeySz = 0;
  33348. byte* eccCert = NULL;
  33349. byte* eccPrivKey = NULL;
  33350. word32 eccCertSz = 0;
  33351. word32 eccPrivKeySz = 0;
  33352. #ifndef NO_RSA
  33353. /* read client RSA cert and key in DER format */
  33354. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33355. if (rsaCert == NULL)
  33356. return -12300;
  33357. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33358. if (rsaPrivKey == NULL) {
  33359. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33360. return -12301;
  33361. }
  33362. rsaCertSz = FOURK_BUF;
  33363. rsaPrivKeySz = FOURK_BUF;
  33364. #endif /* NO_RSA */
  33365. #ifdef HAVE_ECC
  33366. /* read client ECC cert and key in DER format */
  33367. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33368. if (eccCert == NULL) {
  33369. #ifndef NO_RSA
  33370. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33371. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33372. #endif
  33373. return -12302;
  33374. }
  33375. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33376. if (eccPrivKey == NULL) {
  33377. #ifndef NO_RSA
  33378. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33379. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33380. #endif
  33381. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33382. return -12303;
  33383. }
  33384. eccCertSz = FOURK_BUF;
  33385. eccPrivKeySz = FOURK_BUF;
  33386. #endif /* HAVE_ECC */
  33387. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  33388. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  33389. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  33390. eccPrivKey, &eccPrivKeySz);
  33391. if (ret < 0) {
  33392. #ifndef NO_RSA
  33393. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33394. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33395. #endif
  33396. #ifdef HAVE_ECC
  33397. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33398. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33399. #endif
  33400. return -12304;
  33401. }
  33402. ret = pkcs7authenveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  33403. rsaPrivKey, (word32)rsaPrivKeySz,
  33404. eccCert, (word32)eccCertSz,
  33405. eccPrivKey, (word32)eccPrivKeySz);
  33406. #ifndef NO_RSA
  33407. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33408. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33409. #endif
  33410. #ifdef HAVE_ECC
  33411. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33412. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33413. #endif
  33414. return ret;
  33415. }
  33416. #endif /* HAVE_AESGCM || HAVE_AESCCM */
  33417. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  33418. static const byte p7DefKey[] = {
  33419. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33420. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33421. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33422. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  33423. };
  33424. static const byte p7AltKey[] = {
  33425. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33426. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  33427. };
  33428. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  33429. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  33430. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  33431. {
  33432. int ret;
  33433. if (cek == NULL || out == NULL)
  33434. return BAD_FUNC_ARG;
  33435. /* test case sanity checks */
  33436. if (keyIdSz != 1) {
  33437. return -12310;
  33438. }
  33439. if (keyId[0] != 0x00) {
  33440. return -12311;
  33441. }
  33442. if (type != (int)PKCS7_KEKRI) {
  33443. return -12312;
  33444. }
  33445. switch (keyWrapAlgo) {
  33446. case AES256_WRAP:
  33447. ret = wc_AesKeyUnWrap(p7DefKey, sizeof(p7DefKey), cek, cekSz,
  33448. out, outSz, NULL);
  33449. if (ret <= 0)
  33450. return ret;
  33451. break;
  33452. default:
  33453. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  33454. return BAD_KEYWRAP_ALG_E;
  33455. };
  33456. (void)pkcs7;
  33457. (void)direction;
  33458. (void)orginKey; /* used with KAKRI */
  33459. (void)orginKeySz;
  33460. return ret;
  33461. }
  33462. /* returns key size on success */
  33463. static int getFirmwareKey(PKCS7* pkcs7, byte* key, word32 keySz)
  33464. {
  33465. int ret;
  33466. word32 atrSz;
  33467. byte atr[256];
  33468. /* Additionally can look for fwWrappedFirmwareKey
  33469. * 1.2.840.113529.1.9.16.1.16 */
  33470. const unsigned char fwWrappedFirmwareKey[] = {
  33471. /* 0x06, 0x0B */
  33472. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  33473. 0x01, 0x09, 0x10, 0x02, 0x27
  33474. };
  33475. /* find keyID in fwWrappedFirmwareKey */
  33476. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  33477. sizeof(fwWrappedFirmwareKey), NULL, &atrSz);
  33478. if (ret == LENGTH_ONLY_E) {
  33479. XMEMSET(atr, 0, sizeof(atr));
  33480. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  33481. sizeof(fwWrappedFirmwareKey), atr, &atrSz);
  33482. /* keyIdRaw[0] OCTET TAG */
  33483. /* keyIdRaw[1] Length */
  33484. if (ret > 0) {
  33485. PKCS7* envPkcs7;
  33486. envPkcs7 = wc_PKCS7_New(NULL, 0);
  33487. if (envPkcs7 == NULL) {
  33488. return MEMORY_E;
  33489. }
  33490. wc_PKCS7_Init(envPkcs7, NULL, 0);
  33491. ret = wc_PKCS7_SetWrapCEKCb(envPkcs7, myCEKwrapFunc);
  33492. if (ret == 0) {
  33493. /* expecting FIRMWARE_PKG_DATA content */
  33494. envPkcs7->contentOID = FIRMWARE_PKG_DATA;
  33495. ret = wc_PKCS7_DecodeEnvelopedData(envPkcs7, atr, atrSz,
  33496. key, keySz);
  33497. if (envPkcs7->contentOID != FIRMWARE_PKG_DATA) {
  33498. /* the contentOID should have been set to the inner
  33499. * FIRMWARE_PKG_DATA content */
  33500. ret = BAD_STATE_E;
  33501. }
  33502. }
  33503. wc_PKCS7_Free(envPkcs7);
  33504. }
  33505. }
  33506. return ret;
  33507. }
  33508. /* create a KEKRI enveloped data
  33509. * return size on success */
  33510. static int envelopedData_encrypt(byte* in, word32 inSz, byte* out,
  33511. word32 outSz)
  33512. {
  33513. int ret;
  33514. PKCS7* pkcs7;
  33515. WOLFSSL_SMALL_STACK_STATIC const byte keyId[] = { 0x00 };
  33516. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  33517. if (pkcs7 == NULL)
  33518. return -12330;
  33519. pkcs7->content = in;
  33520. pkcs7->contentSz = inSz;
  33521. pkcs7->contentOID = FIRMWARE_PKG_DATA;
  33522. pkcs7->encryptOID = AES256CBCb;
  33523. pkcs7->ukm = NULL;
  33524. pkcs7->ukmSz = 0;
  33525. /* add recipient (KEKRI type) */
  33526. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP, (byte*)p7DefKey,
  33527. sizeof(p7DefKey), (byte*)keyId,
  33528. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0);
  33529. if (ret < 0) {
  33530. printf("wc_PKCS7_AddRecipient_KEKRI() failed, ret = %d\n", ret);
  33531. wc_PKCS7_Free(pkcs7);
  33532. return -12331;
  33533. }
  33534. /* encode envelopedData, returns size */
  33535. ret = wc_PKCS7_EncodeEnvelopedData(pkcs7, out, outSz);
  33536. if (ret <= 0) {
  33537. printf("wc_PKCS7_EncodeEnvelopedData() failed, ret = %d\n", ret);
  33538. wc_PKCS7_Free(pkcs7);
  33539. return -12332;
  33540. }
  33541. wc_PKCS7_Free(pkcs7);
  33542. return ret;
  33543. }
  33544. /*
  33545. * keyHint is the KeyID to be set in the fwDecryptKeyID attribute
  33546. * returns size of buffer output on success
  33547. */
  33548. static int generateBundle(byte* out, word32 *outSz, const byte* encryptKey,
  33549. word32 encryptKeySz, byte keyHint, byte* cert, word32 certSz,
  33550. byte* key, word32 keySz)
  33551. {
  33552. int ret, attribNum = 1;
  33553. PKCS7* pkcs7;
  33554. /* KEY ID
  33555. * fwDecryptKeyID OID 1.2.840.113549.1.9.16.2.37
  33556. */
  33557. const unsigned char fwDecryptKeyID[] = {
  33558. 0x06, 0x0B,
  33559. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  33560. 0x01, 0x09, 0x10, 0x02, 0x25
  33561. };
  33562. /* fwWrappedFirmwareKey 1.2.840.113529.1.9.16.1.16 */
  33563. const unsigned char fwWrappedFirmwareKey[] = {
  33564. 0x06, 0x0B, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  33565. 0x01, 0x09, 0x10, 0x02, 0x27
  33566. };
  33567. byte keyID[] = { 0x04, 0x01, 0x00 };
  33568. byte env[256];
  33569. char data[] = "Test of wolfSSL PKCS7 decrypt callback";
  33570. PKCS7Attrib attribs[] =
  33571. {
  33572. { fwDecryptKeyID, sizeof(fwDecryptKeyID), keyID, sizeof(keyID) },
  33573. { fwWrappedFirmwareKey, sizeof(fwWrappedFirmwareKey), env, 0 }
  33574. };
  33575. keyID[2] = keyHint;
  33576. /* If using keyHint 0 then create a bundle with fwWrappedFirmwareKey */
  33577. if (keyHint == 0) {
  33578. ret = envelopedData_encrypt((byte*)p7DefKey, sizeof(p7DefKey), env,
  33579. sizeof(env));
  33580. if (ret <= 0) {
  33581. return ret;
  33582. }
  33583. attribs[1].valueSz = ret;
  33584. attribNum++;
  33585. }
  33586. /* init PKCS7 */
  33587. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  33588. if (pkcs7 == NULL)
  33589. return -12340;
  33590. ret = wc_PKCS7_InitWithCert(pkcs7, cert, certSz);
  33591. if (ret != 0) {
  33592. printf("ERROR: wc_PKCS7_InitWithCert() failed, ret = %d\n", ret);
  33593. wc_PKCS7_Free(pkcs7);
  33594. return -12341;
  33595. }
  33596. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  33597. if (ret != 0) {
  33598. wc_PKCS7_Free(pkcs7);
  33599. return -12342;
  33600. }
  33601. /* encode Signed Encrypted FirmwarePkgData */
  33602. if (encryptKeySz == 16) {
  33603. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  33604. encryptKeySz, key, keySz, AES128CBCb, RSAk, SHA256h,
  33605. (byte*)data, sizeof(data), NULL, 0,
  33606. attribs, attribNum, out, *outSz);
  33607. }
  33608. else {
  33609. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  33610. encryptKeySz, key, keySz, AES256CBCb, RSAk, SHA256h,
  33611. (byte*)data, sizeof(data), NULL, 0,
  33612. attribs, attribNum, out, *outSz);
  33613. }
  33614. if (ret <= 0) {
  33615. printf("ERROR: wc_PKCS7_EncodeSignedEncryptedFPD() failed, "
  33616. "ret = %d\n", ret);
  33617. wc_PKCS7_Free(pkcs7);
  33618. return -12343;
  33619. } else {
  33620. *outSz = ret;
  33621. }
  33622. wc_PKCS7_Free(pkcs7);
  33623. return ret;
  33624. }
  33625. /* test verification and decryption of PKCS7 bundle
  33626. * return 0 on success
  33627. */
  33628. static int verifyBundle(byte* derBuf, word32 derSz, int keyHint)
  33629. {
  33630. int ret = 0;
  33631. int usrCtx = 1; /* test value to pass as user context to callback */
  33632. PKCS7* pkcs7 = NULL;
  33633. byte* sid = NULL;
  33634. word32 sidSz;
  33635. byte key[256];
  33636. word32 keySz = sizeof(key);
  33637. byte *decoded = NULL;
  33638. int decodedSz = FOURK_BUF/2;
  33639. WOLFSSL_SMALL_STACK_STATIC const byte expectedSid[] = {
  33640. #ifdef NO_SHA
  33641. #ifdef USE_CERT_BUFFERS_1024
  33642. 0x70, 0xe7, 0x79, 0x60, 0x8f, 0x41, 0xdc, 0xe9,
  33643. 0xad, 0x8b, 0x3d, 0x0c, 0x20, 0xf4, 0xc3, 0xf2,
  33644. 0x8e, 0x05, 0xe8, 0xa1, 0xb6, 0x68, 0x74, 0x06,
  33645. 0xbc, 0xe7, 0xc5, 0x3c, 0x13, 0x99, 0x79, 0xb9
  33646. #else
  33647. 0xce, 0x06, 0x07, 0xbe, 0xf1, 0xa6, 0x1e, 0x36,
  33648. 0xef, 0xfa, 0xbc, 0x89, 0x71, 0xf3, 0x23, 0x9e,
  33649. 0x34, 0x6d, 0xae, 0x86, 0xae, 0x2b, 0xdc, 0xf4,
  33650. 0x4a, 0x27, 0xd5, 0x63, 0x59, 0x4f, 0x4a, 0x71
  33651. #endif
  33652. #else /* !NO_SHA */
  33653. #ifdef USE_CERT_BUFFERS_1024
  33654. 0x81, 0x69, 0x0f, 0xf8, 0xdf, 0xdd, 0xcf, 0x34,
  33655. 0x29, 0xd5, 0x67, 0x75, 0x71, 0x85, 0xc7, 0x75,
  33656. 0x10, 0x69, 0x59, 0xec,
  33657. #else
  33658. 0x33, 0xD8, 0x45, 0x66, 0xD7, 0x68, 0x87, 0x18,
  33659. 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26,
  33660. 0xD7, 0x85, 0x65, 0xC0
  33661. #endif
  33662. #endif /* !NO_SHA */
  33663. };
  33664. decoded = (byte *)XMALLOC(decodedSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33665. if (decoded == NULL) {
  33666. ret = MEMORY_E;
  33667. goto out;
  33668. }
  33669. pkcs7 = wc_PKCS7_New(HEAP_HINT, INVALID_DEVID);
  33670. if (pkcs7 == NULL) {
  33671. ret = MEMORY_E;
  33672. goto out;
  33673. }
  33674. /* Test verify */
  33675. ret = wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID);
  33676. if (ret != 0)
  33677. goto out;
  33678. ret = wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  33679. if (ret != 0)
  33680. goto out;
  33681. ret = wc_PKCS7_VerifySignedData(pkcs7, derBuf, derSz);
  33682. if (ret != 0)
  33683. goto out;
  33684. /* Get size of SID and print it out */
  33685. ret = wc_PKCS7_GetSignerSID(pkcs7, NULL, &sidSz);
  33686. if (ret != LENGTH_ONLY_E)
  33687. goto out;
  33688. sid = (byte*)XMALLOC(sidSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33689. if (sid == NULL) {
  33690. ret = MEMORY_E;
  33691. goto out;
  33692. }
  33693. ret = wc_PKCS7_GetSignerSID(pkcs7, sid, &sidSz);
  33694. if (ret != 0)
  33695. goto out;
  33696. ret = XMEMCMP(sid, expectedSid, sidSz);
  33697. if (ret != 0) {
  33698. ret = PKCS7_NO_SIGNER_E; /* close enough */
  33699. goto out;
  33700. }
  33701. /* get expected fwWrappedFirmwareKey */
  33702. if (keyHint == 0) {
  33703. ret = getFirmwareKey(pkcs7, key, keySz);
  33704. if (ret < 0)
  33705. goto out;
  33706. pkcs7->encryptionKey = key;
  33707. pkcs7->encryptionKeySz = ret;
  33708. }
  33709. else {
  33710. decodedSz = PKCS7_BUF_SIZE;
  33711. ret = wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc);
  33712. if (ret != 0)
  33713. goto out;
  33714. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)&usrCtx);
  33715. if (ret != 0)
  33716. goto out;
  33717. }
  33718. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  33719. pkcs7->contentSz, decoded, decodedSz);
  33720. if (decodedSz < 0) {
  33721. ret = decodedSz;
  33722. goto out;
  33723. }
  33724. ret = 0;
  33725. out:
  33726. if (decoded)
  33727. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33728. if (pkcs7)
  33729. wc_PKCS7_Free(pkcs7);
  33730. if (sid)
  33731. XFREE(sid, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33732. return ret;
  33733. }
  33734. WOLFSSL_TEST_SUBROUTINE int pkcs7callback_test(byte* cert, word32 certSz, byte* key, word32 keySz)
  33735. {
  33736. int ret = 0;
  33737. word32 derSz;
  33738. byte *derBuf = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33739. if (! derBuf)
  33740. ERROR_OUT(-12360, out);
  33741. /* Doing default generation and verify */
  33742. derSz = FOURK_BUF;
  33743. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 0, cert,
  33744. certSz, key, keySz);
  33745. if (ret <= 0) {
  33746. ERROR_OUT(-12361, out);
  33747. }
  33748. ret = verifyBundle(derBuf, derSz, 0);
  33749. if (ret != 0) {
  33750. ERROR_OUT(-12362, out);
  33751. }
  33752. /* test choosing other key with keyID */
  33753. derSz = FOURK_BUF;
  33754. ret = generateBundle(derBuf, &derSz, p7AltKey, sizeof(p7AltKey), 1,
  33755. cert, certSz, key, keySz);
  33756. if (ret <= 0) {
  33757. ERROR_OUT(-12363, out);
  33758. }
  33759. ret = verifyBundle(derBuf, derSz, 1);
  33760. if (ret != 0) {
  33761. ERROR_OUT(-12364, out);
  33762. }
  33763. /* test fail case with wrong keyID */
  33764. derSz = FOURK_BUF;
  33765. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 1,
  33766. cert, certSz, key, keySz);
  33767. if (ret <= 0) {
  33768. ERROR_OUT(-12365, out);
  33769. }
  33770. ret = verifyBundle(derBuf, derSz, 1);
  33771. if (ret == 0) {
  33772. ERROR_OUT(-12366, out);
  33773. }
  33774. ret = 0;
  33775. out:
  33776. if (derBuf)
  33777. XFREE(derBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33778. return ret;
  33779. }
  33780. #endif /* !NO_AES && HAVE_AES_CBC */
  33781. #ifndef NO_PKCS7_ENCRYPTED_DATA
  33782. typedef struct {
  33783. const byte* content;
  33784. word32 contentSz;
  33785. int contentOID;
  33786. int encryptOID;
  33787. byte* encryptionKey;
  33788. word32 encryptionKeySz;
  33789. PKCS7Attrib* attribs;
  33790. word32 attribsSz;
  33791. const char* outFileName;
  33792. } pkcs7EncryptedVector;
  33793. WOLFSSL_TEST_SUBROUTINE int pkcs7encrypted_test(void)
  33794. {
  33795. int ret = 0;
  33796. int i, testSz;
  33797. int encryptedSz, decodedSz, attribIdx;
  33798. PKCS7* pkcs7;
  33799. byte *encrypted;
  33800. byte *decoded;
  33801. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  33802. XFILE pkcs7File;
  33803. #endif
  33804. PKCS7Attrib* expectedAttrib;
  33805. PKCS7DecodedAttrib* decodedAttrib;
  33806. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  33807. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  33808. 0x72,0x6c,0x64
  33809. };
  33810. #ifndef NO_DES3
  33811. byte desKey[] = {
  33812. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  33813. };
  33814. byte des3Key[] = {
  33815. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  33816. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  33817. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  33818. };
  33819. #endif
  33820. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  33821. #ifdef WOLFSSL_AES_128
  33822. byte aes128Key[] = {
  33823. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33824. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  33825. };
  33826. #endif
  33827. #ifdef WOLFSSL_AES_192
  33828. byte aes192Key[] = {
  33829. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33830. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33831. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  33832. };
  33833. #endif
  33834. #ifdef WOLFSSL_AES_256
  33835. byte aes256Key[] = {
  33836. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33837. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33838. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33839. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  33840. };
  33841. #endif
  33842. #ifdef WOLFSSL_AES_256
  33843. /* Attribute example from RFC 4134, Section 7.2
  33844. * OID = 1.2.5555
  33845. * OCTET STRING = 'This is a test General ASN Attribute, number 1.' */
  33846. static byte genAttrOid[] = { 0x06, 0x03, 0x2a, 0xab, 0x33 };
  33847. static byte genAttr[] = { 0x04, 47,
  33848. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  33849. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  33850. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  33851. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  33852. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  33853. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x31, 0x2e };
  33854. static byte genAttrOid2[] = { 0x06, 0x03, 0x2a, 0xab, 0x34 };
  33855. static byte genAttr2[] = { 0x04, 47,
  33856. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  33857. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  33858. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  33859. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  33860. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  33861. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x32, 0x2e };
  33862. PKCS7Attrib attribs[] =
  33863. {
  33864. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) }
  33865. };
  33866. PKCS7Attrib multiAttribs[] =
  33867. {
  33868. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) },
  33869. { genAttrOid2, sizeof(genAttrOid2), genAttr2, sizeof(genAttr2) }
  33870. };
  33871. #endif
  33872. #endif /* NO_AES */
  33873. const pkcs7EncryptedVector testVectors[] =
  33874. {
  33875. #ifndef NO_DES3
  33876. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key),
  33877. NULL, 0, "pkcs7encryptedDataDES3.der"},
  33878. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey),
  33879. NULL, 0, "pkcs7encryptedDataDES.der"},
  33880. #endif /* NO_DES3 */
  33881. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  33882. #ifdef WOLFSSL_AES_128
  33883. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  33884. sizeof(aes128Key), NULL, 0, "pkcs7encryptedDataAES128CBC.der"},
  33885. #endif
  33886. #ifdef WOLFSSL_AES_192
  33887. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  33888. sizeof(aes192Key), NULL, 0, "pkcs7encryptedDataAES192CBC.der"},
  33889. #endif
  33890. #ifdef WOLFSSL_AES_256
  33891. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  33892. sizeof(aes256Key), NULL, 0, "pkcs7encryptedDataAES256CBC.der"},
  33893. /* test with optional unprotected attributes */
  33894. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  33895. sizeof(aes256Key), attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  33896. "pkcs7encryptedDataAES256CBC_attribs.der"},
  33897. /* test with multiple optional unprotected attributes */
  33898. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  33899. sizeof(aes256Key), multiAttribs,
  33900. (sizeof(multiAttribs)/sizeof(PKCS7Attrib)),
  33901. "pkcs7encryptedDataAES256CBC_multi_attribs.der"},
  33902. /* test with contentType set to FirmwarePkgData */
  33903. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256CBCb, aes256Key,
  33904. sizeof(aes256Key), NULL, 0,
  33905. "pkcs7encryptedDataAES256CBC_firmwarePkgData.der"},
  33906. #endif
  33907. #endif /* !NO_AES && HAVE_AES_CBC */
  33908. };
  33909. encrypted = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33910. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33911. if ((! encrypted) || (! decoded)) {
  33912. ERROR_OUT(MEMORY_E, out);
  33913. }
  33914. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  33915. for (i = 0; i < testSz; i++) {
  33916. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  33917. if (pkcs7 == NULL) {
  33918. ERROR_OUT(-12400, out);
  33919. }
  33920. pkcs7->content = (byte*)testVectors[i].content;
  33921. pkcs7->contentSz = testVectors[i].contentSz;
  33922. pkcs7->contentOID = testVectors[i].contentOID;
  33923. pkcs7->encryptOID = testVectors[i].encryptOID;
  33924. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  33925. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  33926. pkcs7->unprotectedAttribs = testVectors[i].attribs;
  33927. pkcs7->unprotectedAttribsSz = testVectors[i].attribsSz;
  33928. /* encode encryptedData */
  33929. encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  33930. PKCS7_BUF_SIZE);
  33931. if (encryptedSz <= 0) {
  33932. wc_PKCS7_Free(pkcs7);
  33933. ERROR_OUT(-12401, out);
  33934. }
  33935. /* decode encryptedData */
  33936. #ifndef NO_PKCS7_STREAM
  33937. { /* test reading byte by byte */
  33938. int z;
  33939. for (z = 0; z < encryptedSz; z++) {
  33940. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted + z, 1,
  33941. decoded, PKCS7_BUF_SIZE);
  33942. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  33943. printf("unexpected error %d\n", decodedSz);
  33944. ERROR_OUT(-12402, out);
  33945. }
  33946. }
  33947. /* test decode result */
  33948. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  33949. printf("stream read failed\n");
  33950. wc_PKCS7_Free(pkcs7);
  33951. ERROR_OUT(-12403, out);
  33952. }
  33953. }
  33954. #endif
  33955. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  33956. decoded, PKCS7_BUF_SIZE);
  33957. if (decodedSz <= 0){
  33958. wc_PKCS7_Free(pkcs7);
  33959. ERROR_OUT(-12404, out);
  33960. }
  33961. /* test decode result */
  33962. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  33963. wc_PKCS7_Free(pkcs7);
  33964. ERROR_OUT(-12405, out);
  33965. }
  33966. /* verify decoded unprotected attributes */
  33967. if (pkcs7->decodedAttrib != NULL) {
  33968. decodedAttrib = pkcs7->decodedAttrib;
  33969. attribIdx = 1;
  33970. while (decodedAttrib != NULL) {
  33971. /* expected attribute, stored list is reversed */
  33972. expectedAttrib = &(pkcs7->unprotectedAttribs
  33973. [pkcs7->unprotectedAttribsSz - attribIdx]);
  33974. /* verify oid */
  33975. if (XMEMCMP(decodedAttrib->oid, expectedAttrib->oid,
  33976. decodedAttrib->oidSz) != 0) {
  33977. wc_PKCS7_Free(pkcs7);
  33978. ERROR_OUT(-12406, out);
  33979. }
  33980. /* verify value */
  33981. if (XMEMCMP(decodedAttrib->value, expectedAttrib->value,
  33982. decodedAttrib->valueSz) != 0) {
  33983. wc_PKCS7_Free(pkcs7);
  33984. ERROR_OUT(-12407, out);
  33985. }
  33986. decodedAttrib = decodedAttrib->next;
  33987. attribIdx++;
  33988. }
  33989. }
  33990. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  33991. /* output pkcs7 envelopedData for external testing */
  33992. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  33993. if (!pkcs7File) {
  33994. wc_PKCS7_Free(pkcs7);
  33995. ERROR_OUT(-12408, out);
  33996. }
  33997. ret = (int)XFWRITE(encrypted, encryptedSz, 1, pkcs7File);
  33998. XFCLOSE(pkcs7File);
  33999. if (ret > 0)
  34000. ret = 0;
  34001. #endif
  34002. wc_PKCS7_Free(pkcs7);
  34003. }
  34004. out:
  34005. if (encrypted)
  34006. XFREE(encrypted, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34007. if (decoded)
  34008. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34009. return ret;
  34010. }
  34011. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  34012. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  34013. typedef struct {
  34014. const byte* content;
  34015. word32 contentSz;
  34016. int contentOID;
  34017. const char* outFileName;
  34018. } pkcs7CompressedVector;
  34019. WOLFSSL_TEST_SUBROUTINE int pkcs7compressed_test(void)
  34020. {
  34021. int ret = 0;
  34022. int i, testSz;
  34023. int compressedSz, decodedSz;
  34024. PKCS7* pkcs7;
  34025. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  34026. byte *compressed;
  34027. byte *decoded;
  34028. #else
  34029. byte compressed[PKCS7_BUF_SIZE];
  34030. byte decoded[PKCS7_BUF_SIZE];
  34031. #endif
  34032. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  34033. XFILE pkcs7File;
  34034. #endif
  34035. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  34036. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  34037. 0x72,0x6c,0x64
  34038. };
  34039. const pkcs7CompressedVector testVectors[] =
  34040. {
  34041. {data, (word32)sizeof(data), DATA,
  34042. "pkcs7compressedData_data_zlib.der"},
  34043. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA,
  34044. "pkcs7compressedData_firmwarePkgData_zlib.der"},
  34045. };
  34046. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  34047. compressed = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34048. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34049. if ((! compressed) || (! decoded)) {
  34050. ERROR_OUT(MEMORY_E, out);
  34051. }
  34052. #endif
  34053. testSz = sizeof(testVectors) / sizeof(pkcs7CompressedVector);
  34054. for (i = 0; i < testSz; i++) {
  34055. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  34056. if (pkcs7 == NULL) {
  34057. ERROR_OUT(-12500, out);
  34058. }
  34059. pkcs7->content = (byte*)testVectors[i].content;
  34060. pkcs7->contentSz = testVectors[i].contentSz;
  34061. pkcs7->contentOID = testVectors[i].contentOID;
  34062. /* encode compressedData */
  34063. compressedSz = wc_PKCS7_EncodeCompressedData(pkcs7, compressed,
  34064. PKCS7_BUF_SIZE);
  34065. if (compressedSz <= 0) {
  34066. wc_PKCS7_Free(pkcs7);
  34067. ERROR_OUT(-12501, out);
  34068. }
  34069. /* decode compressedData */
  34070. decodedSz = wc_PKCS7_DecodeCompressedData(pkcs7, compressed,
  34071. compressedSz, decoded,
  34072. PKCS7_BUF_SIZE);
  34073. if (decodedSz <= 0){
  34074. wc_PKCS7_Free(pkcs7);
  34075. ERROR_OUT(-12502, out);
  34076. }
  34077. /* test decode result */
  34078. if (XMEMCMP(decoded, testVectors[i].content,
  34079. testVectors[i].contentSz) != 0) {
  34080. wc_PKCS7_Free(pkcs7);
  34081. ERROR_OUT(-12503, out);
  34082. }
  34083. /* make sure content type is the same */
  34084. if (testVectors[i].contentOID != pkcs7->contentOID) {
  34085. ERROR_OUT(-12504, out);
  34086. }
  34087. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  34088. /* output pkcs7 compressedData for external testing */
  34089. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  34090. if (!pkcs7File) {
  34091. wc_PKCS7_Free(pkcs7);
  34092. ERROR_OUT(-12505, out);
  34093. }
  34094. ret = (int)XFWRITE(compressed, compressedSz, 1, pkcs7File);
  34095. XFCLOSE(pkcs7File);
  34096. if (ret > 0)
  34097. ret = 0;
  34098. #endif
  34099. wc_PKCS7_Free(pkcs7);
  34100. }
  34101. out:
  34102. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  34103. if (compressed)
  34104. XFREE(compressed, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34105. if (decoded)
  34106. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34107. #endif
  34108. return ret;
  34109. } /* pkcs7compressed_test() */
  34110. #undef PKCS7_BUF_SIZE
  34111. #endif /* HAVE_LIBZ */
  34112. typedef struct {
  34113. const byte* content;
  34114. word32 contentSz;
  34115. int hashOID;
  34116. int signOID;
  34117. byte* privateKey;
  34118. word32 privateKeySz;
  34119. byte* cert;
  34120. size_t certSz;
  34121. byte* caCert;
  34122. size_t caCertSz;
  34123. PKCS7Attrib* signedAttribs;
  34124. word32 signedAttribsSz;
  34125. const char* outFileName;
  34126. int contentOID;
  34127. byte* contentType;
  34128. word32 contentTypeSz;
  34129. int sidType;
  34130. int encryptOID; /* for single-shot encrypt alg OID */
  34131. int encCompFlag; /* for single-shot. 1 = enc, 2 = comp, 3 = both*/
  34132. byte* encryptKey; /* for single-shot, encryptedData */
  34133. word32 encryptKeySz; /* for single-shot, encryptedData */
  34134. PKCS7Attrib* unprotectedAttribs; /* for single-shot, encryptedData */
  34135. word32 unprotectedAttribsSz; /* for single-shot, encryptedData */
  34136. word16 detachedSignature; /* generate detached signature (0:1) */
  34137. } pkcs7SignedVector;
  34138. static int pkcs7signed_run_vectors(
  34139. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  34140. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  34141. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  34142. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  34143. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  34144. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  34145. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  34146. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  34147. {
  34148. int ret, testSz = 0, i;
  34149. int encodedSz;
  34150. byte* out = NULL;
  34151. word32 outSz;
  34152. WC_RNG rng;
  34153. PKCS7* pkcs7 = NULL;
  34154. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  34155. XFILE file;
  34156. #endif
  34157. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  34158. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  34159. 0x72,0x6c,0x64
  34160. };
  34161. static byte transIdOid[] =
  34162. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  34163. 0x09, 0x07 };
  34164. static byte messageTypeOid[] =
  34165. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  34166. 0x09, 0x02 };
  34167. static byte senderNonceOid[] =
  34168. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  34169. 0x09, 0x05 };
  34170. #ifndef NO_SHA
  34171. static byte transId[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  34172. #else
  34173. static byte transId[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  34174. #endif
  34175. static byte messageType[] = { 0x13, 2, '1', '9' };
  34176. static byte senderNonce[PKCS7_NONCE_SZ + 2];
  34177. static PKCS7Attrib attribs[] =
  34178. {
  34179. { transIdOid, sizeof(transIdOid), transId,
  34180. sizeof(transId) - 1 }, /* take off the null */
  34181. { messageTypeOid, sizeof(messageTypeOid), messageType,
  34182. sizeof(messageType) },
  34183. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  34184. sizeof(senderNonce) }
  34185. };
  34186. /* for testing custom contentType, FirmwarePkgData */
  34187. static byte customContentType[] = { 0x06, 0x0B, 0x2A, 0x86,
  34188. 0x48, 0x86, 0xF7, 0x0D,
  34189. 0x01, 0x09, 0x10, 0x01, 0x10 };
  34190. #define MAX_TESTVECTORS_LEN 20
  34191. #define ADD_PKCS7SIGNEDVECTOR(...) { \
  34192. pkcs7SignedVector _this_vector = { __VA_ARGS__ }; \
  34193. if (testSz == MAX_TESTVECTORS_LEN) { \
  34194. ret = -12534; \
  34195. goto out; \
  34196. } \
  34197. XMEMCPY(&testVectors[testSz++], &_this_vector, sizeof _this_vector);\
  34198. }
  34199. pkcs7SignedVector *testVectors = NULL;
  34200. XMEMSET(&rng, 0, sizeof(rng));
  34201. testVectors = (pkcs7SignedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  34202. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34203. if (testVectors == NULL) {
  34204. ret = -12567;
  34205. goto out;
  34206. }
  34207. {
  34208. #ifndef NO_RSA
  34209. #ifndef NO_SHA
  34210. /* RSA with SHA */
  34211. ADD_PKCS7SIGNEDVECTOR(
  34212. data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  34213. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34214. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34215. "pkcs7signedData_RSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0, NULL,
  34216. 0, 0);
  34217. /* RSA with SHA, no signed attributes */
  34218. ADD_PKCS7SIGNEDVECTOR(
  34219. data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  34220. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz,
  34221. NULL, 0, NULL, 0,
  34222. "pkcs7signedData_RSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  34223. NULL, 0, 0);
  34224. #endif
  34225. #ifdef WOLFSSL_SHA224
  34226. /* RSA with SHA224 */
  34227. ADD_PKCS7SIGNEDVECTOR(
  34228. data, (word32)sizeof(data), SHA224h, RSAk, rsaClientPrivKeyBuf,
  34229. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34230. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34231. "pkcs7signedData_RSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  34232. NULL, 0, 0);
  34233. #endif
  34234. #ifndef NO_SHA256
  34235. /* RSA with SHA256 */
  34236. ADD_PKCS7SIGNEDVECTOR(
  34237. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34238. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34239. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34240. "pkcs7signedData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  34241. NULL, 0, 0);
  34242. /* RSA with SHA256, detached signature */
  34243. ADD_PKCS7SIGNEDVECTOR(
  34244. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34245. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34246. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34247. "pkcs7signedData_RSA_SHA256_detachedSig.der", 0, NULL, 0, 0, 0, 0,
  34248. NULL, 0, NULL, 0, 1);
  34249. /* RSA with SHA256 and SubjectKeyIdentifier in SignerIdentifier */
  34250. ADD_PKCS7SIGNEDVECTOR(
  34251. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34252. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34253. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34254. "pkcs7signedData_RSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  34255. NULL, 0, NULL, 0, 0);
  34256. /* RSA with SHA256 and custom contentType */
  34257. ADD_PKCS7SIGNEDVECTOR(
  34258. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34259. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34260. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34261. "pkcs7signedData_RSA_SHA256_custom_contentType.der", 0,
  34262. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  34263. NULL, 0, 0);
  34264. /* RSA with SHA256 and FirmwarePkgData contentType */
  34265. ADD_PKCS7SIGNEDVECTOR(
  34266. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34267. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34268. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34269. "pkcs7signedData_RSA_SHA256_firmwarePkgData.der",
  34270. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  34271. /* RSA with SHA256 using server cert and ca cert */
  34272. ADD_PKCS7SIGNEDVECTOR(
  34273. data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  34274. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  34275. rsaCaCertBuf, rsaCaCertBufSz,
  34276. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34277. "pkcs7signedData_RSA_SHA256_with_ca_cert.der", 0, NULL, 0, 0, 0, 0,
  34278. NULL, 0, NULL, 0, 0);
  34279. #endif
  34280. #if defined(WOLFSSL_SHA384)
  34281. /* RSA with SHA384 */
  34282. ADD_PKCS7SIGNEDVECTOR(
  34283. data, (word32)sizeof(data), SHA384h, RSAk, rsaClientPrivKeyBuf,
  34284. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34285. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34286. "pkcs7signedData_RSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  34287. NULL, 0, 0);
  34288. #endif
  34289. #if defined(WOLFSSL_SHA512)
  34290. /* RSA with SHA512 */
  34291. ADD_PKCS7SIGNEDVECTOR(
  34292. data, (word32)sizeof(data), SHA512h, RSAk, rsaClientPrivKeyBuf,
  34293. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34294. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34295. "pkcs7signedData_RSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  34296. NULL, 0, 0);
  34297. #endif
  34298. #endif /* NO_RSA */
  34299. #ifdef HAVE_ECC
  34300. #ifndef NO_SHA
  34301. /* ECDSA with SHA */
  34302. ADD_PKCS7SIGNEDVECTOR(
  34303. data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  34304. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34305. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34306. "pkcs7signedData_ECDSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  34307. NULL, 0, 0);
  34308. /* ECDSA with SHA, no signed attributes */
  34309. ADD_PKCS7SIGNEDVECTOR(
  34310. data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  34311. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz,
  34312. NULL, 0, NULL, 0,
  34313. "pkcs7signedData_ECDSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  34314. NULL, 0, 0);
  34315. #endif
  34316. #ifdef WOLFSSL_SHA224
  34317. /* ECDSA with SHA224 */
  34318. ADD_PKCS7SIGNEDVECTOR(
  34319. data, (word32)sizeof(data), SHA224h, ECDSAk, eccClientPrivKeyBuf,
  34320. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34321. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34322. "pkcs7signedData_ECDSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  34323. NULL, 0, 0);
  34324. #endif
  34325. #ifndef NO_SHA256
  34326. /* ECDSA with SHA256 */
  34327. ADD_PKCS7SIGNEDVECTOR(
  34328. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34329. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34330. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34331. "pkcs7signedData_ECDSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  34332. NULL, 0, 0);
  34333. /* ECDSA with SHA256 and SubjectKeyIdentifier in SigherIdentifier */
  34334. ADD_PKCS7SIGNEDVECTOR(
  34335. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34336. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34337. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34338. "pkcs7signedData_ECDSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  34339. NULL, 0, NULL, 0, 0);
  34340. /* ECDSA with SHA256 and custom contentType */
  34341. ADD_PKCS7SIGNEDVECTOR(
  34342. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34343. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34344. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34345. "pkcs7signedData_ECDSA_SHA256_custom_contentType.der", 0,
  34346. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  34347. NULL, 0, 0);
  34348. /* ECDSA with SHA256 and FirmwarePkgData contentType */
  34349. ADD_PKCS7SIGNEDVECTOR(
  34350. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34351. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34352. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34353. "pkcs7signedData_ECDSA_SHA256_firmwarePkgData.der",
  34354. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  34355. #endif
  34356. #ifdef WOLFSSL_SHA384
  34357. /* ECDSA with SHA384 */
  34358. ADD_PKCS7SIGNEDVECTOR(
  34359. data, (word32)sizeof(data), SHA384h, ECDSAk, eccClientPrivKeyBuf,
  34360. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34361. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34362. "pkcs7signedData_ECDSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  34363. NULL, 0, 0);
  34364. #endif
  34365. #ifdef WOLFSSL_SHA512
  34366. /* ECDSA with SHA512 */
  34367. ADD_PKCS7SIGNEDVECTOR(
  34368. data, (word32)sizeof(data), SHA512h, ECDSAk, eccClientPrivKeyBuf,
  34369. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34370. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34371. "pkcs7signedData_ECDSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  34372. NULL, 0, 0);
  34373. #endif
  34374. #endif /* HAVE_ECC */
  34375. };
  34376. #undef MAX_TESTVECTORS_LEN
  34377. #undef ADD_PKCS7SIGNEDVECTOR
  34378. outSz = FOURK_BUF;
  34379. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34380. if (out == NULL)
  34381. ERROR_OUT(-12510, out);
  34382. XMEMSET(out, 0, outSz);
  34383. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  34384. if (ret < 0)
  34385. ERROR_OUT(-12511, out);
  34386. #ifndef HAVE_FIPS
  34387. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  34388. #else
  34389. ret = wc_InitRng(&rng);
  34390. #endif
  34391. if (ret != 0)
  34392. ERROR_OUT(-12512, out);
  34393. for (i = 0; i < testSz; i++) {
  34394. if (pkcs7)
  34395. wc_PKCS7_Free(pkcs7);
  34396. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  34397. if (pkcs7 == NULL)
  34398. ERROR_OUT(-12513, out);
  34399. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  34400. (word32)testVectors[i].certSz);
  34401. if (ret != 0)
  34402. ERROR_OUT(-12514, out);
  34403. /* load CA certificate, if present */
  34404. if (testVectors[i].caCert != NULL) {
  34405. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  34406. (word32)testVectors[i].caCertSz);
  34407. if (ret != 0)
  34408. ERROR_OUT(-12515, out);
  34409. }
  34410. pkcs7->rng = &rng;
  34411. pkcs7->content = (byte*)testVectors[i].content;
  34412. pkcs7->contentSz = testVectors[i].contentSz;
  34413. pkcs7->contentOID = testVectors[i].contentOID;
  34414. pkcs7->hashOID = testVectors[i].hashOID;
  34415. pkcs7->encryptOID = testVectors[i].signOID;
  34416. pkcs7->privateKey = testVectors[i].privateKey;
  34417. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  34418. pkcs7->signedAttribs = testVectors[i].signedAttribs;
  34419. pkcs7->signedAttribsSz = testVectors[i].signedAttribsSz;
  34420. /* optional custom contentType, default is DATA,
  34421. overrides contentOID if set */
  34422. if (testVectors[i].contentType != NULL) {
  34423. ret = wc_PKCS7_SetContentType(pkcs7, testVectors[i].contentType,
  34424. testVectors[i].contentTypeSz);
  34425. if (ret != 0)
  34426. ERROR_OUT(-12516, out);
  34427. }
  34428. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  34429. default is IssuerAndSerialNumber */
  34430. if (testVectors[i].sidType == CMS_SKID) {
  34431. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  34432. if (ret != 0)
  34433. ERROR_OUT(-12517, out);
  34434. }
  34435. /* generate senderNonce */
  34436. {
  34437. senderNonce[0] = 0x04;
  34438. senderNonce[1] = PKCS7_NONCE_SZ;
  34439. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  34440. if (ret != 0)
  34441. ERROR_OUT(-12518, out);
  34442. }
  34443. /* generate transactionID (used with SCEP) */
  34444. {
  34445. #ifndef NO_SHA
  34446. wc_Sha sha;
  34447. byte digest[WC_SHA_DIGEST_SIZE];
  34448. #else
  34449. wc_Sha256 sha;
  34450. byte digest[WC_SHA256_DIGEST_SIZE];
  34451. #endif
  34452. int j,k;
  34453. transId[0] = 0x13;
  34454. transId[1] = sizeof(digest) * 2;
  34455. #ifndef NO_SHA
  34456. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  34457. if (ret != 0)
  34458. ERROR_OUT(-12519, out);
  34459. wc_ShaUpdate(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  34460. wc_ShaFinal(&sha, digest);
  34461. wc_ShaFree(&sha);
  34462. #else
  34463. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  34464. if (ret != 0)
  34465. ERROR_OUT(-12520, out);
  34466. wc_Sha256Update(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  34467. wc_Sha256Final(&sha, digest);
  34468. wc_Sha256Free(&sha);
  34469. #endif
  34470. for (j = 0, k = 2; j < (int)sizeof(digest); j++, k += 2) {
  34471. #if defined(WOLF_C89)
  34472. XSPRINTF((char*)&transId[k], "%02x", digest[j]);
  34473. #else
  34474. (void)XSNPRINTF((char*)&transId[k], 3, "%02x", digest[j]);
  34475. #endif
  34476. }
  34477. }
  34478. /* enable detached signature generation, if set */
  34479. if (testVectors[i].detachedSignature == 1) {
  34480. ret = wc_PKCS7_SetDetached(pkcs7, 1);
  34481. if (ret != 0)
  34482. ERROR_OUT(-12521, out);
  34483. }
  34484. encodedSz = wc_PKCS7_EncodeSignedData(pkcs7, out, outSz);
  34485. if (encodedSz < 0)
  34486. ERROR_OUT(-12522, out);
  34487. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  34488. /* write PKCS#7 to output file for more testing */
  34489. file = XFOPEN(testVectors[i].outFileName, "wb");
  34490. if (!file) {
  34491. ERROR_OUT(-12523, out);
  34492. }
  34493. ret = (int)XFWRITE(out, 1, encodedSz, file);
  34494. XFCLOSE(file);
  34495. if (ret != (int)encodedSz)
  34496. ERROR_OUT(-12524, out);
  34497. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  34498. wc_PKCS7_Free(pkcs7);
  34499. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  34500. if (pkcs7 == NULL)
  34501. ERROR_OUT(-12525, out);
  34502. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  34503. if (testVectors[i].detachedSignature == 1) {
  34504. /* set content for verifying detached signatures */
  34505. pkcs7->content = (byte*)testVectors[i].content;
  34506. pkcs7->contentSz = testVectors[i].contentSz;
  34507. }
  34508. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  34509. if (ret < 0)
  34510. ERROR_OUT(-12526, out);
  34511. /* verify contentType extracted successfully for custom content types */
  34512. if (testVectors[i].contentTypeSz > 0) {
  34513. if (pkcs7->contentTypeSz != testVectors[i].contentTypeSz) {
  34514. ERROR_OUT(-12527, out);
  34515. } else if (XMEMCMP(pkcs7->contentType, testVectors[i].contentType,
  34516. pkcs7->contentTypeSz) != 0) {
  34517. ERROR_OUT(-12528, out);
  34518. }
  34519. }
  34520. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0)
  34521. ERROR_OUT(-12529, out);
  34522. {
  34523. /* check getting signed attributes */
  34524. #ifndef NO_SHA
  34525. byte buf[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  34526. #else
  34527. byte buf[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  34528. #endif
  34529. byte* oidPt = transIdOid + 2; /* skip object id tag and size */
  34530. int oidSz = (int)sizeof(transIdOid) - 2;
  34531. int bufSz = 0;
  34532. if (testVectors[i].signedAttribs != NULL &&
  34533. wc_PKCS7_GetAttributeValue(pkcs7, oidPt, oidSz,
  34534. NULL, (word32*)&bufSz) != LENGTH_ONLY_E)
  34535. ERROR_OUT(-12530, out);
  34536. if (bufSz > (int)sizeof(buf))
  34537. ERROR_OUT(-12531, out);
  34538. bufSz = wc_PKCS7_GetAttributeValue(pkcs7, oidPt, oidSz,
  34539. buf, (word32*)&bufSz);
  34540. if ((testVectors[i].signedAttribs != NULL && bufSz < 0) ||
  34541. (testVectors[i].signedAttribs == NULL && bufSz > 0))
  34542. ERROR_OUT(-12532, out);
  34543. }
  34544. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  34545. file = XFOPEN("./pkcs7cert.der", "wb");
  34546. if (!file)
  34547. ERROR_OUT(-12533, out);
  34548. ret = (int)XFWRITE(pkcs7->singleCert, 1, pkcs7->singleCertSz, file);
  34549. XFCLOSE(file);
  34550. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  34551. }
  34552. out:
  34553. if (pkcs7 != NULL)
  34554. wc_PKCS7_Free(pkcs7);
  34555. if (out != NULL)
  34556. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34557. if (testVectors != NULL)
  34558. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34559. wc_FreeRng(&rng);
  34560. if (ret > 0)
  34561. return 0;
  34562. (void)rsaClientCertBuf;
  34563. (void)rsaClientCertBufSz;
  34564. (void)rsaClientPrivKeyBuf;
  34565. (void)rsaClientPrivKeyBufSz;
  34566. (void)rsaServerCertBuf;
  34567. (void)rsaServerCertBufSz;
  34568. (void)rsaServerPrivKeyBuf;
  34569. (void)rsaServerPrivKeyBufSz;
  34570. (void)rsaCaCertBuf;
  34571. (void)rsaCaCertBufSz;
  34572. (void)rsaCaPrivKeyBuf;
  34573. (void)rsaCaPrivKeyBufSz;
  34574. (void)eccClientCertBuf;
  34575. (void)eccClientCertBufSz;
  34576. (void)eccClientPrivKeyBuf;
  34577. (void)eccClientPrivKeyBufSz;
  34578. return ret;
  34579. }
  34580. static int pkcs7signed_run_SingleShotVectors(
  34581. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  34582. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  34583. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  34584. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  34585. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  34586. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  34587. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  34588. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  34589. {
  34590. int ret, testSz = 0, i;
  34591. int encodedSz;
  34592. byte* out = NULL;
  34593. word32 outSz;
  34594. WC_RNG rng;
  34595. PKCS7* pkcs7 = NULL;
  34596. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  34597. XFILE file;
  34598. #endif
  34599. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA) && \
  34600. !defined(NO_PKCS7_ENCRYPTED_DATA)
  34601. byte* encryptedTmp = NULL;
  34602. int encryptedTmpSz;
  34603. #endif
  34604. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  34605. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  34606. 0x72,0x6c,0x64
  34607. };
  34608. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  34609. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  34610. static byte aes256Key[] = {
  34611. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  34612. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  34613. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  34614. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  34615. };
  34616. #endif
  34617. static byte messageTypeOid[] =
  34618. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  34619. 0x09, 0x02 };
  34620. static byte messageType[] = { 0x13, 2, '1', '9' };
  34621. PKCS7Attrib attribs[] =
  34622. {
  34623. { messageTypeOid, sizeof(messageTypeOid), messageType,
  34624. sizeof(messageType) },
  34625. };
  34626. #define MAX_TESTVECTORS_LEN 19
  34627. #define ADD_PKCS7SIGNEDVECTOR(...) { \
  34628. pkcs7SignedVector _this_vector = { __VA_ARGS__ }; \
  34629. if (testSz == MAX_TESTVECTORS_LEN) { \
  34630. ret = -12568; \
  34631. goto out; \
  34632. } \
  34633. XMEMCPY(&testVectors[testSz++], &_this_vector, sizeof _this_vector);\
  34634. }
  34635. pkcs7SignedVector *testVectors = NULL;
  34636. XMEMSET(&rng, 0, sizeof(rng));
  34637. testVectors = (pkcs7SignedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  34638. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34639. if (testVectors == NULL) {
  34640. ret = -12567;
  34641. goto out;
  34642. }
  34643. {
  34644. #ifndef NO_RSA
  34645. #ifndef NO_SHA256
  34646. /* Signed FirmwarePkgData, RSA, SHA256, no attribs */
  34647. ADD_PKCS7SIGNEDVECTOR(
  34648. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34649. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34650. NULL, 0,
  34651. "pkcs7signedFirmwarePkgData_RSA_SHA256_noattr.der", 0, NULL, 0, 0,
  34652. 0, 0, NULL, 0, NULL, 0, 0);
  34653. /* Signed FirmwarePkgData, RSA, SHA256, attrs */
  34654. ADD_PKCS7SIGNEDVECTOR(
  34655. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34656. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34657. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34658. "pkcs7signedFirmwarePkgData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0,
  34659. NULL, 0, NULL, 0, 0);
  34660. /* Signed FirmwarePkgData, RSA, SHA256, SubjectKeyIdentifier, attrs */
  34661. ADD_PKCS7SIGNEDVECTOR(
  34662. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34663. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34664. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34665. "pkcs7signedFirmwarePkgData_RSA_SHA256_SKID.der", 0, NULL,
  34666. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0);
  34667. /* Signed FirmwraePkgData, RSA, SHA256, server cert and ca cert, attr */
  34668. ADD_PKCS7SIGNEDVECTOR(
  34669. data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  34670. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  34671. rsaCaCertBuf, rsaCaCertBufSz,
  34672. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34673. "pkcs7signedFirmwarePkgData_RSA_SHA256_with_ca_cert.der", 0, NULL,
  34674. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  34675. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  34676. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  34677. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, no attribs */
  34678. ADD_PKCS7SIGNEDVECTOR(
  34679. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34680. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34681. NULL, 0,
  34682. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  34683. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0);
  34684. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, attribs */
  34685. ADD_PKCS7SIGNEDVECTOR(
  34686. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34687. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34688. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34689. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256.der", 0,
  34690. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  34691. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  34692. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  34693. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  34694. /* Signed Compressed FirmwarePkgData, RSA, SHA256, no attribs */
  34695. ADD_PKCS7SIGNEDVECTOR(
  34696. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34697. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34698. NULL, 0,
  34699. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  34700. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  34701. /* Signed Compressed FirmwarePkgData, RSA, SHA256, attribs */
  34702. ADD_PKCS7SIGNEDVECTOR(
  34703. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34704. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34705. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34706. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256.der", 0,
  34707. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  34708. #ifndef NO_PKCS7_ENCRYPTED_DATA
  34709. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  34710. no attribs */
  34711. ADD_PKCS7SIGNEDVECTOR(
  34712. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34713. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34714. NULL, 0,
  34715. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256_noattr.der",
  34716. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  34717. 0, 0);
  34718. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  34719. attribs */
  34720. ADD_PKCS7SIGNEDVECTOR(
  34721. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  34722. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  34723. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34724. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256.der",
  34725. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  34726. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  34727. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  34728. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  34729. #endif /* NO_SHA256 */
  34730. #endif /* NO_RSA */
  34731. #ifdef HAVE_ECC
  34732. #ifndef NO_SHA256
  34733. /* Signed FirmwarePkgData, ECDSA, SHA256, no attribs */
  34734. ADD_PKCS7SIGNEDVECTOR(
  34735. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34736. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34737. NULL, 0,
  34738. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  34739. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  34740. /* Signed FirmwarePkgData, ECDSA, SHA256, attribs */
  34741. ADD_PKCS7SIGNEDVECTOR(
  34742. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34743. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34744. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34745. "pkcs7signedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  34746. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  34747. /* Signed FirmwarePkgData, ECDSA, SHA256, SubjectKeyIdentifier, attr */
  34748. ADD_PKCS7SIGNEDVECTOR(
  34749. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34750. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34751. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34752. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_SKID.der", 0, NULL,
  34753. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0);
  34754. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  34755. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  34756. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, no attribs */
  34757. ADD_PKCS7SIGNEDVECTOR(
  34758. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34759. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34760. NULL, 0,
  34761. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  34762. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0);
  34763. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, attribs */
  34764. ADD_PKCS7SIGNEDVECTOR(
  34765. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34766. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34767. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34768. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  34769. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  34770. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  34771. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  34772. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  34773. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, no attribs */
  34774. ADD_PKCS7SIGNEDVECTOR(
  34775. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34776. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34777. NULL, 0,
  34778. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  34779. 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  34780. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, attrib */
  34781. ADD_PKCS7SIGNEDVECTOR(
  34782. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34783. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34784. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34785. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  34786. 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  34787. #ifndef NO_PKCS7_ENCRYPTED_DATA
  34788. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  34789. no attribs */
  34790. ADD_PKCS7SIGNEDVECTOR(
  34791. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34792. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34793. NULL, 0,
  34794. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der",
  34795. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  34796. 0, 0);
  34797. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  34798. attribs */
  34799. ADD_PKCS7SIGNEDVECTOR(
  34800. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  34801. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  34802. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  34803. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256.der",
  34804. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  34805. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  34806. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  34807. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  34808. #endif /* NO_SHA256 */
  34809. #endif /* HAVE_ECC */
  34810. };
  34811. #undef MAX_TESTVECTORS_LEN
  34812. #undef ADD_PKCS7SIGNEDVECTOR
  34813. outSz = FOURK_BUF;
  34814. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34815. if (out == NULL)
  34816. ERROR_OUT(-12540, out);
  34817. XMEMSET(out, 0, outSz);
  34818. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  34819. if (ret < 0)
  34820. ERROR_OUT(-12541, out);
  34821. #ifndef HAVE_FIPS
  34822. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  34823. #else
  34824. ret = wc_InitRng(&rng);
  34825. #endif
  34826. if (ret != 0)
  34827. ERROR_OUT(-12542, out);
  34828. for (i = 0; i < testSz; i++) {
  34829. if (pkcs7)
  34830. wc_PKCS7_Free(pkcs7);
  34831. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  34832. if (pkcs7 == NULL)
  34833. ERROR_OUT(-12543, out);
  34834. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  34835. (word32)testVectors[i].certSz);
  34836. if (ret != 0)
  34837. ERROR_OUT(-12544, out);
  34838. /* load CA certificate, if present */
  34839. if (testVectors[i].caCert != NULL) {
  34840. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  34841. (word32)testVectors[i].caCertSz);
  34842. if (ret != 0)
  34843. ERROR_OUT(-12545, out);
  34844. }
  34845. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  34846. default is IssuerAndSerialNumber */
  34847. if (testVectors[i].sidType == CMS_SKID) {
  34848. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  34849. if (ret != 0)
  34850. ERROR_OUT(-12546, out);
  34851. }
  34852. if (testVectors[i].encCompFlag == 0) {
  34853. /* encode Signed FirmwarePkgData */
  34854. encodedSz = wc_PKCS7_EncodeSignedFPD(pkcs7,
  34855. testVectors[i].privateKey, testVectors[i].privateKeySz,
  34856. testVectors[i].signOID, testVectors[i].hashOID,
  34857. (byte*)testVectors[i].content, testVectors[i].contentSz,
  34858. testVectors[i].signedAttribs,
  34859. testVectors[i].signedAttribsSz, out, outSz);
  34860. if (encodedSz < 0)
  34861. ERROR_OUT(-12547, out);
  34862. #ifndef NO_PKCS7_ENCRYPTED_DATA
  34863. } else if (testVectors[i].encCompFlag == 1) {
  34864. /* encode Signed Encrypted FirmwarePkgData */
  34865. encodedSz = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7,
  34866. testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  34867. testVectors[i].privateKey, testVectors[i].privateKeySz,
  34868. testVectors[i].encryptOID, testVectors[i].signOID,
  34869. testVectors[i].hashOID, (byte*)testVectors[i].content,
  34870. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  34871. testVectors[i].unprotectedAttribsSz,
  34872. testVectors[i].signedAttribs,
  34873. testVectors[i].signedAttribsSz, out, outSz);
  34874. if (encodedSz <= 0)
  34875. ERROR_OUT(-12548, out);
  34876. #endif
  34877. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  34878. } else if (testVectors[i].encCompFlag == 2) {
  34879. /* encode Signed Compressed FirmwarePkgData */
  34880. encodedSz = wc_PKCS7_EncodeSignedCompressedFPD(pkcs7,
  34881. testVectors[i].privateKey, testVectors[i].privateKeySz,
  34882. testVectors[i].signOID, testVectors[i].hashOID,
  34883. (byte*)testVectors[i].content, testVectors[i].contentSz,
  34884. testVectors[i].signedAttribs,
  34885. testVectors[i].signedAttribsSz, out, outSz);
  34886. if (encodedSz <= 0)
  34887. ERROR_OUT(-12549, out);
  34888. #ifndef NO_PKCS7_ENCRYPTED_DATA
  34889. } else if (testVectors[i].encCompFlag == 3) {
  34890. /* encode Signed Encrypted Compressed FirmwarePkgData */
  34891. encodedSz = wc_PKCS7_EncodeSignedEncryptedCompressedFPD(pkcs7,
  34892. testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  34893. testVectors[i].privateKey, testVectors[i].privateKeySz,
  34894. testVectors[i].encryptOID, testVectors[i].signOID,
  34895. testVectors[i].hashOID, (byte*)testVectors[i].content,
  34896. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  34897. testVectors[i].unprotectedAttribsSz,
  34898. testVectors[i].signedAttribs,
  34899. testVectors[i].signedAttribsSz, out, outSz);
  34900. if (encodedSz <= 0)
  34901. ERROR_OUT(-12550, out);
  34902. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  34903. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  34904. } else {
  34905. /* unsupported SignedData single-shot combination */
  34906. ERROR_OUT(-12551, out);
  34907. }
  34908. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  34909. /* write PKCS#7 to output file for more testing */
  34910. file = XFOPEN(testVectors[i].outFileName, "wb");
  34911. if (!file)
  34912. ERROR_OUT(-12552, out);
  34913. ret = (int)XFWRITE(out, 1, encodedSz, file);
  34914. XFCLOSE(file);
  34915. file = NULL;
  34916. if (ret != (int)encodedSz)
  34917. ERROR_OUT(-12553, out);
  34918. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  34919. wc_PKCS7_Free(pkcs7);
  34920. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  34921. if (pkcs7 == NULL)
  34922. ERROR_OUT(-12554, out);
  34923. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  34924. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  34925. if (ret < 0)
  34926. ERROR_OUT(-12555, out);
  34927. #ifndef NO_PKCS7_STREAM
  34928. {
  34929. word32 z;
  34930. for (z = 0; z < outSz && ret != 0; z++) {
  34931. ret = wc_PKCS7_VerifySignedData(pkcs7, out + z, 1);
  34932. if (ret < 0 && ret != WC_PKCS7_WANT_READ_E) {
  34933. printf("unexpected error %d\n", ret);
  34934. ERROR_OUT(-12556, out);
  34935. }
  34936. }
  34937. }
  34938. #endif
  34939. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0)
  34940. ERROR_OUT(-12557, out);
  34941. if (testVectors[i].encCompFlag == 0) {
  34942. /* verify decoded content matches expected */
  34943. if ((pkcs7->contentSz != testVectors[i].contentSz) ||
  34944. XMEMCMP(pkcs7->content, testVectors[i].content,
  34945. pkcs7->contentSz)) {
  34946. ERROR_OUT(-12558, out);
  34947. }
  34948. }
  34949. #ifndef NO_PKCS7_ENCRYPTED_DATA
  34950. else if (testVectors[i].encCompFlag == 1) {
  34951. /* decrypt inner encryptedData */
  34952. pkcs7->encryptionKey = testVectors[i].encryptKey;
  34953. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  34954. ret = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  34955. pkcs7->contentSz, out, outSz);
  34956. if (ret < 0)
  34957. ERROR_OUT(-12559, out);
  34958. /* compare decrypted to expected */
  34959. if (((word32)ret != testVectors[i].contentSz) ||
  34960. XMEMCMP(out, testVectors[i].content, ret))
  34961. ERROR_OUT(-12560, out);
  34962. }
  34963. #endif
  34964. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  34965. else if (testVectors[i].encCompFlag == 2) {
  34966. /* decompress inner compressedData */
  34967. ret = wc_PKCS7_DecodeCompressedData(pkcs7, pkcs7->content,
  34968. pkcs7->contentSz, out, outSz);
  34969. if (ret < 0)
  34970. ERROR_OUT(-12561, out);
  34971. /* compare decompressed to expected */
  34972. if (((word32)ret != testVectors[i].contentSz) ||
  34973. XMEMCMP(out, testVectors[i].content, ret))
  34974. ERROR_OUT(-12562, out);
  34975. }
  34976. #ifndef NO_PKCS7_ENCRYPTED_DATA
  34977. else if (testVectors[i].encCompFlag == 3) {
  34978. encryptedTmpSz = FOURK_BUF;
  34979. encryptedTmp = (byte*)XMALLOC(encryptedTmpSz, HEAP_HINT,
  34980. DYNAMIC_TYPE_TMP_BUFFER);
  34981. if (encryptedTmp == NULL)
  34982. ERROR_OUT(-12563, out);
  34983. XMEMSET(encryptedTmp, 0, encryptedTmpSz);
  34984. /* decrypt inner encryptedData */
  34985. pkcs7->encryptionKey = testVectors[i].encryptKey;
  34986. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  34987. encryptedTmpSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  34988. pkcs7->contentSz, encryptedTmp,
  34989. encryptedTmpSz);
  34990. if (encryptedTmpSz < 0 || pkcs7->contentOID != COMPRESSED_DATA)
  34991. ERROR_OUT(-12564, out);
  34992. /* decompress inner compressedData */
  34993. ret = wc_PKCS7_DecodeCompressedData(pkcs7, encryptedTmp,
  34994. encryptedTmpSz, out, outSz);
  34995. if (ret < 0)
  34996. ERROR_OUT(-12565, out);
  34997. /* compare decompressed to expected */
  34998. if (((word32)ret != testVectors[i].contentSz) ||
  34999. XMEMCMP(out, testVectors[i].content, ret))
  35000. ERROR_OUT(-12566, out);
  35001. }
  35002. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  35003. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  35004. }
  35005. out:
  35006. if (pkcs7 != NULL)
  35007. wc_PKCS7_Free(pkcs7);
  35008. if (out != NULL)
  35009. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35010. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA) && \
  35011. !defined(NO_PKCS7_ENCRYPTED_DATA)
  35012. if (encryptedTmp != NULL)
  35013. XFREE(encryptedTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35014. #endif
  35015. if (testVectors != NULL)
  35016. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35017. wc_FreeRng(&rng);
  35018. if (ret > 0)
  35019. return 0;
  35020. (void)eccClientCertBuf;
  35021. (void)eccClientCertBufSz;
  35022. (void)eccClientPrivKeyBuf;
  35023. (void)eccClientPrivKeyBufSz;
  35024. (void)rsaClientCertBuf;
  35025. (void)rsaClientCertBufSz;
  35026. (void)rsaClientPrivKeyBuf;
  35027. (void)rsaClientPrivKeyBufSz;
  35028. (void)rsaServerCertBuf;
  35029. (void)rsaServerCertBufSz;
  35030. (void)rsaServerPrivKeyBuf;
  35031. (void)rsaServerPrivKeyBufSz;
  35032. (void)rsaCaCertBuf;
  35033. (void)rsaCaCertBufSz;
  35034. (void)rsaCaPrivKeyBuf;
  35035. (void)rsaCaPrivKeyBufSz;
  35036. return ret;
  35037. }
  35038. WOLFSSL_TEST_SUBROUTINE int pkcs7signed_test(void)
  35039. {
  35040. int ret = 0;
  35041. byte* rsaClientCertBuf = NULL;
  35042. byte* rsaServerCertBuf = NULL;
  35043. byte* rsaCaCertBuf = NULL;
  35044. byte* eccClientCertBuf = NULL;
  35045. byte* rsaClientPrivKeyBuf = NULL;
  35046. byte* rsaServerPrivKeyBuf = NULL;
  35047. byte* rsaCaPrivKeyBuf = NULL;
  35048. byte* eccClientPrivKeyBuf = NULL;
  35049. word32 rsaClientCertBufSz = 0;
  35050. word32 rsaServerCertBufSz = 0;
  35051. word32 rsaCaCertBufSz = 0;
  35052. word32 eccClientCertBufSz = 0;
  35053. word32 rsaClientPrivKeyBufSz = 0;
  35054. word32 rsaServerPrivKeyBufSz = 0;
  35055. word32 rsaCaPrivKeyBufSz = 0;
  35056. word32 eccClientPrivKeyBufSz = 0;
  35057. #ifndef NO_RSA
  35058. /* read client RSA cert and key in DER format */
  35059. rsaClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  35060. DYNAMIC_TYPE_TMP_BUFFER);
  35061. if (rsaClientCertBuf == NULL)
  35062. ret = -12600;
  35063. rsaClientPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  35064. DYNAMIC_TYPE_TMP_BUFFER);
  35065. if (ret == 0 && rsaClientPrivKeyBuf == NULL) {
  35066. ret = -12601;
  35067. }
  35068. rsaClientCertBufSz = FOURK_BUF;
  35069. rsaClientPrivKeyBufSz = FOURK_BUF;
  35070. /* read server RSA cert and key in DER format */
  35071. rsaServerCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  35072. DYNAMIC_TYPE_TMP_BUFFER);
  35073. if (ret == 0 && rsaServerCertBuf == NULL)
  35074. ret = -12602;
  35075. rsaServerPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  35076. DYNAMIC_TYPE_TMP_BUFFER);
  35077. if (ret == 0 && rsaServerPrivKeyBuf == NULL) {
  35078. ret = -12603;
  35079. }
  35080. rsaServerCertBufSz = FOURK_BUF;
  35081. rsaServerPrivKeyBufSz = FOURK_BUF;
  35082. /* read CA RSA cert and key in DER format, for use with server cert */
  35083. rsaCaCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  35084. DYNAMIC_TYPE_TMP_BUFFER);
  35085. if (ret == 0 && rsaCaCertBuf == NULL)
  35086. ret = -12604;
  35087. rsaCaPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  35088. DYNAMIC_TYPE_TMP_BUFFER);
  35089. if (ret == 0 && rsaCaPrivKeyBuf == NULL) {
  35090. ret = -12605;
  35091. }
  35092. rsaCaCertBufSz = FOURK_BUF;
  35093. rsaCaPrivKeyBufSz = FOURK_BUF;
  35094. #endif /* NO_RSA */
  35095. #ifdef HAVE_ECC
  35096. /* read client ECC cert and key in DER format */
  35097. eccClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  35098. DYNAMIC_TYPE_TMP_BUFFER);
  35099. if (ret == 0 && eccClientCertBuf == NULL) {
  35100. ret = -12606;
  35101. }
  35102. eccClientPrivKeyBuf =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  35103. DYNAMIC_TYPE_TMP_BUFFER);
  35104. if (ret == 0 && eccClientPrivKeyBuf == NULL) {
  35105. ret = -12607;
  35106. }
  35107. eccClientCertBufSz = FOURK_BUF;
  35108. eccClientPrivKeyBufSz = FOURK_BUF;
  35109. #endif /* HAVE_ECC */
  35110. if (ret >= 0)
  35111. ret = pkcs7_load_certs_keys(rsaClientCertBuf, &rsaClientCertBufSz,
  35112. rsaClientPrivKeyBuf, &rsaClientPrivKeyBufSz,
  35113. rsaServerCertBuf, &rsaServerCertBufSz,
  35114. rsaServerPrivKeyBuf, &rsaServerPrivKeyBufSz,
  35115. rsaCaCertBuf, &rsaCaCertBufSz,
  35116. rsaCaPrivKeyBuf, &rsaCaPrivKeyBufSz,
  35117. eccClientCertBuf, &eccClientCertBufSz,
  35118. eccClientPrivKeyBuf, &eccClientPrivKeyBufSz);
  35119. if (ret < 0) {
  35120. ret = -12608;
  35121. }
  35122. if (ret >= 0)
  35123. ret = pkcs7signed_run_vectors(rsaClientCertBuf, (word32)rsaClientCertBufSz,
  35124. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  35125. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  35126. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  35127. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  35128. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  35129. eccClientCertBuf, (word32)eccClientCertBufSz,
  35130. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  35131. if (ret >= 0)
  35132. ret = pkcs7signed_run_SingleShotVectors(
  35133. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  35134. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  35135. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  35136. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  35137. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  35138. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  35139. eccClientCertBuf, (word32)eccClientCertBufSz,
  35140. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  35141. #if !defined(NO_RSA) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  35142. if (ret >= 0)
  35143. ret = pkcs7callback_test(
  35144. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  35145. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz);
  35146. #endif
  35147. XFREE(rsaClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35148. XFREE(rsaClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35149. XFREE(rsaServerCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35150. XFREE(rsaServerPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35151. XFREE(rsaCaCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35152. XFREE(rsaCaPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35153. XFREE(eccClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35154. XFREE(eccClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35155. return ret;
  35156. }
  35157. #endif /* HAVE_PKCS7 */
  35158. #ifdef HAVE_VALGRIND
  35159. /* Need a static build to have access to symbols. */
  35160. /* Maximum number of bytes in a number to test. */
  35161. #define MP_MAX_TEST_BYTE_LEN 32
  35162. static int randNum(mp_int* n, int len, WC_RNG* rng, void* heap)
  35163. {
  35164. byte d[MP_MAX_TEST_BYTE_LEN];
  35165. int ret;
  35166. (void)heap;
  35167. do {
  35168. ret = wc_RNG_GenerateBlock(rng, d, len);
  35169. if (ret != 0)
  35170. return ret;
  35171. ret = mp_read_unsigned_bin(n, d, len);
  35172. if (ret != 0)
  35173. return ret;
  35174. } while (mp_iszero(n));
  35175. return 0;
  35176. }
  35177. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  35178. static int mp_test_div_3(mp_int* a, mp_int* r, WC_RNG* rng)
  35179. {
  35180. int i, j;
  35181. mp_digit rem;
  35182. mp_digit rem2;
  35183. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  35184. defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  35185. for (i = 0; i < 10; i++) {
  35186. for (j = 1; j < 10; j++) {
  35187. if (randNum(a, j, rng, NULL) != 0)
  35188. return -12620;
  35189. if (mp_div_3(a, r, &rem) != 0)
  35190. return -12621;
  35191. if (mp_mul_d(r, 3, r) != 0)
  35192. return -12622;
  35193. if (mp_add_d(r, rem, r) != 0)
  35194. return -12623;
  35195. if (mp_cmp(r, a) != MP_EQ)
  35196. return -12624;
  35197. }
  35198. }
  35199. if (mp_div_3(a, r, &rem) != 0)
  35200. return -12625;
  35201. if (mp_div_3(a, a, NULL) != 0)
  35202. return -12626;
  35203. if (mp_cmp(r, a) != MP_EQ)
  35204. return -12627;
  35205. #endif
  35206. #if defined(WOLFSSL_SP_MATH_ALL)
  35207. if (mp_div_d(a, 10, r, &rem) != 0)
  35208. return -12628;
  35209. if (mp_div_d(a, 10, a, NULL) != 0)
  35210. return -12629;
  35211. if (mp_cmp(r, a) != MP_EQ)
  35212. return -12630;
  35213. if (mp_div_d(a, 12, r, &rem) != 0)
  35214. return -12631;
  35215. if (mp_div_d(a, 12, a, NULL) != 0)
  35216. return -12632;
  35217. if (mp_cmp(r, a) != MP_EQ)
  35218. return -12633;
  35219. if (mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), r, &rem) != 0)
  35220. return -12634;
  35221. if (mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), NULL, &rem2) != 0)
  35222. return -12635;
  35223. if (mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), a, NULL) != 0)
  35224. return -12636;
  35225. if (mp_cmp(r, a) != MP_EQ)
  35226. return -12637;
  35227. if (rem != rem2)
  35228. return -12638;
  35229. #endif
  35230. (void)a;
  35231. (void)r;
  35232. (void)rng;
  35233. (void)i;
  35234. (void)j;
  35235. (void)rem;
  35236. (void)rem2;
  35237. return 0;
  35238. }
  35239. #endif /* WOLFSSL_SP_MATH || !USE_FAST_MATH */
  35240. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  35241. !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  35242. (!defined WOLFSSL_SP_MATH && !defined(WOLFSSL_SP_MATH_ALL) && \
  35243. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  35244. static int mp_test_radix_10(mp_int* a, mp_int* r, WC_RNG* rng)
  35245. {
  35246. int ret;
  35247. int i, j;
  35248. int size;
  35249. char str[30];
  35250. WOLFSSL_SMALL_STACK_STATIC const char* badStr1 = "A";
  35251. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "a";
  35252. WOLFSSL_SMALL_STACK_STATIC const char* badStr3 = " ";
  35253. WOLFSSL_SMALL_STACK_STATIC const char* zeros = "000";
  35254. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  35255. for (i = 0; i < 10; i++) {
  35256. for (j = 2; j < 12; j++) {
  35257. if (randNum(a, j, rng, NULL) != 0)
  35258. return -12640;
  35259. if (mp_radix_size(a, MP_RADIX_DEC, &size) != MP_OKAY)
  35260. return -12641;
  35261. if (mp_toradix(a, str, MP_RADIX_DEC) != MP_OKAY)
  35262. return -12660;
  35263. if ((int)XSTRLEN(str) != size - 1)
  35264. return -12642;
  35265. if (mp_read_radix(r, str, MP_RADIX_DEC) != MP_OKAY)
  35266. return -12661;
  35267. if (mp_cmp(a, r) != MP_EQ)
  35268. return -12643;
  35269. }
  35270. }
  35271. if (mp_read_radix(r, badStr1, MP_RADIX_DEC) != MP_VAL)
  35272. return -12644;
  35273. if (mp_read_radix(r, badStr2, MP_RADIX_DEC) != MP_VAL)
  35274. return -12645;
  35275. if (mp_read_radix(r, badStr3, MP_RADIX_DEC) != MP_VAL)
  35276. return -12646;
  35277. if (mp_read_radix(r, zeros, MP_RADIX_DEC) != MP_OKAY)
  35278. return -12647;
  35279. if (!mp_iszero(r))
  35280. return -12648;
  35281. mp_set(r, 1);
  35282. if (mp_read_radix(r, empty, MP_RADIX_DEC) != MP_OKAY)
  35283. return -12649;
  35284. if (!mp_iszero(r))
  35285. return -12650;
  35286. mp_zero(a);
  35287. ret = mp_radix_size(a, MP_RADIX_DEC, &size);
  35288. if (ret != 0)
  35289. return -12651;
  35290. if (size != 2)
  35291. return -12652;
  35292. ret = mp_toradix(a, str, MP_RADIX_DEC);
  35293. if (ret != 0)
  35294. return -12653;
  35295. if ((int)XSTRLEN(str) != size - 1)
  35296. return -12654;
  35297. ret = mp_read_radix(r, str, MP_RADIX_DEC);
  35298. if (ret != 0)
  35299. return -12655;
  35300. if (!mp_iszero(r))
  35301. return -12656;
  35302. return 0;
  35303. }
  35304. #endif
  35305. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  35306. defined(HAVE_ECC))
  35307. static int mp_test_radix_16(mp_int* a, mp_int* r, WC_RNG* rng)
  35308. {
  35309. int ret;
  35310. int i, j;
  35311. int size;
  35312. char str[30];
  35313. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  35314. static char longStr[2 * sizeof(a->dp) + 2];
  35315. #endif
  35316. WOLFSSL_SMALL_STACK_STATIC const char* badStr1 = " ";
  35317. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "}";
  35318. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  35319. for (i = 0; i < 10; i++) {
  35320. for (j = 2; j < 12; j++) {
  35321. if (randNum(a, j, rng, NULL) != 0)
  35322. return -12660;
  35323. mp_radix_size(a, MP_RADIX_HEX, &size);
  35324. mp_toradix(a, str, MP_RADIX_HEX);
  35325. if ((int)XSTRLEN(str) != size - 1)
  35326. return -12661;
  35327. mp_read_radix(r, str, MP_RADIX_HEX);
  35328. if (mp_cmp(a, r) != MP_EQ)
  35329. return -12662;
  35330. }
  35331. }
  35332. if (mp_read_radix(r, badStr1, MP_RADIX_HEX) != MP_VAL)
  35333. return -12663;
  35334. if (mp_read_radix(r, badStr2, MP_RADIX_HEX) != MP_VAL)
  35335. return -12664;
  35336. mp_set(r, 1);
  35337. if (mp_read_radix(r, empty, MP_RADIX_HEX) != MP_OKAY)
  35338. return -12665;
  35339. if (!mp_iszero(r))
  35340. return -12666;
  35341. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  35342. /* Fixed MP data size - string can be too long. */
  35343. longStr[0] = '8';
  35344. XMEMSET(longStr+1, '0', sizeof(longStr) - 2);
  35345. longStr[sizeof(longStr)-1] = '\0';
  35346. if (mp_read_radix(r, longStr, MP_RADIX_HEX) != MP_VAL)
  35347. return -12667;
  35348. #endif
  35349. mp_zero(a);
  35350. ret = mp_radix_size(a, MP_RADIX_HEX, &size);
  35351. if (ret != 0)
  35352. return -12668;
  35353. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  35354. if (size != 3)
  35355. #else
  35356. if (size != 2)
  35357. #endif
  35358. return -12669;
  35359. ret = mp_toradix(a, str, MP_RADIX_HEX);
  35360. if (ret != 0)
  35361. return -12670;
  35362. if ((int)XSTRLEN(str) != size - 1)
  35363. return -12671;
  35364. ret = mp_read_radix(r, str, MP_RADIX_HEX);
  35365. if (ret != 0)
  35366. return -12672;
  35367. if (!mp_iszero(r))
  35368. return -12673;
  35369. #ifdef WOLFSSL_SP_MATH
  35370. ret = mp_toradix(a, str, 8);
  35371. if (ret != MP_VAL)
  35372. return -12674;
  35373. ret = mp_radix_size(a, 8, &size);
  35374. if (ret != MP_VAL)
  35375. return -12675;
  35376. #endif
  35377. return 0;
  35378. }
  35379. #endif
  35380. static int mp_test_shift(mp_int* a, mp_int* r1, WC_RNG* rng)
  35381. {
  35382. int i;
  35383. if (randNum(a, 4, rng, NULL) != 0)
  35384. return -12680;
  35385. for (i = 0; i < 4; i++) {
  35386. mp_copy(r1, a);
  35387. if (mp_lshd(r1, i) != MP_OKAY)
  35388. return -12681;
  35389. #ifndef WOLFSSL_SP_MATH
  35390. mp_rshd(r1, i);
  35391. #else
  35392. mp_rshb(r1, i * SP_WORD_SIZE);
  35393. #endif
  35394. if (mp_cmp(a, r1) != MP_EQ)
  35395. return -12682;
  35396. }
  35397. #ifndef WOLFSSL_SP_MATH
  35398. for (i = 0; i < DIGIT_BIT+1; i++) {
  35399. if (mp_mul_2d(a, i, r1) != MP_OKAY)
  35400. return -12683;
  35401. mp_rshb(r1, i);
  35402. if (mp_cmp(a, r1) != MP_EQ)
  35403. return -12684;
  35404. }
  35405. #endif
  35406. return 0;
  35407. }
  35408. static int mp_test_add_sub_d(mp_int* a, mp_int* r1)
  35409. {
  35410. int i, j;
  35411. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  35412. mp_zero(a);
  35413. mp_set_bit(a, i);
  35414. if ((int)a->used != (i + DIGIT_BIT) / DIGIT_BIT)
  35415. return -12690;
  35416. for (j = 0; j < i && j < DIGIT_BIT; j++) {
  35417. mp_zero(r1);
  35418. mp_set_bit(r1, i);
  35419. if (mp_sub_d(r1, (mp_digit)1 << j, r1) != MP_OKAY)
  35420. return -12691;
  35421. if (mp_add_d(r1, (mp_digit)1 << j, r1) != MP_OKAY)
  35422. return -12692;
  35423. if (mp_cmp(a, r1) != MP_EQ)
  35424. return -12693;
  35425. }
  35426. }
  35427. mp_zero(r1);
  35428. if (mp_add_d(r1, 1, r1) != MP_OKAY)
  35429. return -12694;
  35430. if (r1->used != 1)
  35431. return -12695;
  35432. if (mp_sub_d(r1, 1, r1) != MP_OKAY)
  35433. return -12696;
  35434. if (r1->used != 0)
  35435. return -12697;
  35436. return 0;
  35437. }
  35438. static int mp_test_read_to_bin(mp_int* a)
  35439. {
  35440. WOLFSSL_SMALL_STACK_STATIC const byte in[16] = {
  35441. 0x91, 0xa2, 0xb3, 0xc4, 0xd5, 0xe6, 0xf7, 0x08,
  35442. 0x93, 0xa4, 0xb4, 0xc5, 0xd6, 0xe7, 0xf8, 0x09
  35443. };
  35444. byte out[24];
  35445. int i, j, k;
  35446. const byte* p;
  35447. int ret;
  35448. for (i = 0; i < (int)sizeof(in); i++) {
  35449. p = in + sizeof(in) - i;
  35450. ret = mp_read_unsigned_bin(a, p, i);
  35451. if (ret != 0)
  35452. return -12710;
  35453. for (j = i; j < (int)sizeof(out); j++) {
  35454. XMEMSET(out, 0xff, sizeof(out));
  35455. ret = mp_to_unsigned_bin_len(a, out, j);
  35456. if (ret != 0)
  35457. return -12711;
  35458. for (k = 0; k < j - i; k++) {
  35459. if (out[k] != 0)
  35460. return -12712;
  35461. }
  35462. for (; k < j; k++) {
  35463. if (out[k] != p[k - (j - i)])
  35464. return -12713;
  35465. }
  35466. }
  35467. }
  35468. /* Length too small. */
  35469. ret = mp_to_unsigned_bin_len(a, out, 1);
  35470. if (ret != MP_VAL)
  35471. return -12716;
  35472. ret = mp_read_unsigned_bin(a, NULL, 0);
  35473. if (ret != 0)
  35474. return -12714;
  35475. if (!mp_iszero(a))
  35476. return -12715;
  35477. return 0;
  35478. }
  35479. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  35480. static int mp_test_set_int(mp_int* a)
  35481. {
  35482. #if SP_ULONG_BITS == 64
  35483. unsigned long n = 0xfedcba9876543210UL;
  35484. byte exp[8] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
  35485. byte out[8] = { 0 };
  35486. #elif SP_ULONG_BITS == 32
  35487. unsigned long n = 0xfedcba98UL;
  35488. byte exp[4] = { 0xfe, 0xdc, 0xba, 0x98 };
  35489. byte out[4] = { 0 };
  35490. #elif SP_ULONG_BITS == 16
  35491. unsigned long n = 0xfedc;
  35492. byte exp[2] = { 0xfe, 0xdc };
  35493. byte out[2] = { 0 };
  35494. #elif SP_ULONG_BITS == 8
  35495. unsigned long n = 0xfe;
  35496. byte exp[1] = { 0xfe };
  35497. byte out[1] = { 0 };
  35498. #endif
  35499. int ret;
  35500. ret = mp_set_int(a, n);
  35501. if (ret != 0)
  35502. return -12720;
  35503. ret = mp_unsigned_bin_size(a);
  35504. if (ret != sizeof(exp))
  35505. return -12721;
  35506. ret = mp_to_unsigned_bin(a, out);
  35507. if (ret != 0)
  35508. return -12722;
  35509. if (XMEMCMP(exp, out, sizeof(exp)) != 0)
  35510. return -12723;
  35511. return 0;
  35512. }
  35513. #endif
  35514. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  35515. static int mp_test_param(mp_int* a, mp_int* b, mp_int* r, WC_RNG* rng)
  35516. {
  35517. byte buffer[16];
  35518. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  35519. char hexStr[] = "abcdef0123456789";
  35520. #ifndef WOLFSSL_SP_INT_NEGATIVE
  35521. char negStr[] = "-1234";
  35522. #endif
  35523. #endif
  35524. #if !defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_KEY_GEN) || \
  35525. defined(HAVE_COMP_KEY)
  35526. char decStr[] = "0987654321";
  35527. #endif
  35528. int ret;
  35529. #ifdef WOLFSSL_SP_MATH_ALL
  35530. mp_digit rho;
  35531. int size;
  35532. #endif
  35533. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  35534. int result;
  35535. #endif
  35536. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  35537. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  35538. mp_digit rd;
  35539. #endif
  35540. (void)rng;
  35541. (void)r;
  35542. ret = mp_init(NULL);
  35543. if (ret != MP_VAL)
  35544. return -12730;
  35545. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  35546. ret = mp_init_multi(NULL, NULL, NULL, NULL, NULL, NULL);
  35547. if (ret != MP_OKAY)
  35548. return -12731;
  35549. #endif
  35550. mp_free(NULL);
  35551. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || !defined(NO_DH) || defined(HAVE_ECC)
  35552. ret = mp_grow(NULL, 1);
  35553. if (ret != MP_VAL)
  35554. return -12732;
  35555. #ifdef WOLFSSL_SP_MATH
  35556. ret = mp_grow(a, SP_INT_DIGITS + 1);
  35557. if (ret != MP_MEM)
  35558. return -12733;
  35559. #endif
  35560. #endif
  35561. mp_clear(NULL);
  35562. ret = mp_abs(NULL, NULL);
  35563. if (ret != MP_VAL)
  35564. return -12734;
  35565. ret = mp_abs(a, NULL);
  35566. if (ret != MP_VAL)
  35567. return -12735;
  35568. ret = mp_abs(NULL, b);
  35569. if (ret != MP_VAL)
  35570. return -12736;
  35571. ret = mp_unsigned_bin_size(NULL);
  35572. if (ret != 0)
  35573. return -12737;
  35574. ret = mp_read_unsigned_bin(NULL, NULL, sizeof(buffer));
  35575. if (ret != MP_VAL)
  35576. return -12738;
  35577. ret = mp_read_unsigned_bin(NULL, buffer, sizeof(buffer));
  35578. if (ret != MP_VAL)
  35579. return -12739;
  35580. ret = mp_read_unsigned_bin(a, NULL, sizeof(buffer));
  35581. if (ret != MP_VAL)
  35582. return -12740;
  35583. ret = mp_read_unsigned_bin(a, buffer, SP_INT_DIGITS * SP_WORD_SIZEOF + 1);
  35584. if (ret != MP_VAL)
  35585. return -12741;
  35586. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  35587. ret = mp_read_radix(NULL, NULL, 16);
  35588. if (ret != MP_VAL)
  35589. return -12742;
  35590. ret = mp_read_radix(a, NULL, 16);
  35591. if (ret != MP_VAL)
  35592. return -12743;
  35593. ret = mp_read_radix(NULL, hexStr, 16);
  35594. if (ret != MP_VAL)
  35595. return -12744;
  35596. #ifndef WOLFSSL_SP_INT_NEGATIVE
  35597. ret = mp_read_radix(a, negStr, 16);
  35598. if (ret != MP_VAL)
  35599. return -12745;
  35600. #ifdef WOLFSSL_SP_MATH_ALL
  35601. ret = mp_read_radix(a, negStr, 10);
  35602. if (ret != MP_VAL)
  35603. return -12746;
  35604. #endif /* WOLFSSL_SP_MATH_ALL */
  35605. #endif /* WOLFSSL_SP_INT_NEGATIVE */
  35606. #endif
  35607. #ifndef WOLFSSL_SP_MATH_ALL
  35608. /* Radix 10 only supported with ALL. */
  35609. ret = mp_read_radix(a, decStr, 10);
  35610. if (ret != MP_VAL)
  35611. return -12747;
  35612. #endif
  35613. /* Radix 8 not supported SP_INT. */
  35614. ret = mp_read_radix(a, "0123", 8);
  35615. if (ret != MP_VAL)
  35616. return -12748;
  35617. ret = mp_count_bits(NULL);
  35618. if (ret != 0)
  35619. return -12749;
  35620. ret = mp_is_bit_set(NULL, 0);
  35621. if (ret != 0)
  35622. return -12750;
  35623. ret = mp_leading_bit(NULL);
  35624. if (ret != 0)
  35625. return -12751;
  35626. mp_zero(a);
  35627. ret = mp_leading_bit(a);
  35628. if (ret != 0)
  35629. return -12752;
  35630. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  35631. defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || \
  35632. !defined(NO_RSA)
  35633. ret = mp_set_bit(NULL, 1);
  35634. if (ret != MP_VAL)
  35635. return -12753;
  35636. #endif
  35637. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  35638. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  35639. ret = mp_to_unsigned_bin(NULL, NULL);
  35640. if (ret != MP_VAL)
  35641. return -12754;
  35642. ret = mp_to_unsigned_bin(a, NULL);
  35643. if (ret != MP_VAL)
  35644. return -12755;
  35645. ret = mp_to_unsigned_bin(NULL, buffer);
  35646. if (ret != MP_VAL)
  35647. return -12756;
  35648. #endif
  35649. ret = mp_to_unsigned_bin_len(NULL, NULL, 1);
  35650. if (ret != MP_VAL)
  35651. return -12757;
  35652. ret = mp_to_unsigned_bin_len(a, NULL, 1);
  35653. if (ret != MP_VAL)
  35654. return -12758;
  35655. ret = mp_to_unsigned_bin_len(NULL, buffer, 1);
  35656. if (ret != MP_VAL)
  35657. return -12759;
  35658. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  35659. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  35660. ret = mp_to_unsigned_bin_at_pos(0, NULL, NULL);
  35661. if (ret != MP_VAL)
  35662. return -12760;
  35663. ret = mp_to_unsigned_bin_at_pos(0, a, NULL);
  35664. if (ret != MP_VAL)
  35665. return -12761;
  35666. ret = mp_to_unsigned_bin_at_pos(0, NULL, buffer);
  35667. if (ret != MP_VAL)
  35668. return -12762;
  35669. ret = mp_to_unsigned_bin_at_pos(0, a, buffer);
  35670. if (ret != MP_OKAY)
  35671. return -12763;
  35672. #endif
  35673. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  35674. ret = mp_copy(NULL, NULL);
  35675. if (ret != MP_VAL)
  35676. return -12764;
  35677. ret = mp_copy(a, NULL);
  35678. if (ret != MP_VAL)
  35679. return -12765;
  35680. ret = mp_copy(NULL, b);
  35681. if (ret != MP_VAL)
  35682. return -12766;
  35683. #endif
  35684. #if defined(WOLFSSL_KEY_GEN) || !defined(NO_DH)
  35685. ret = sp_2expt(NULL, 1);
  35686. if (ret != MP_VAL)
  35687. return -12767;
  35688. #endif
  35689. ret = mp_set(NULL, 0);
  35690. if (ret != MP_VAL)
  35691. return -12768;
  35692. ret = mp_cmp_d(NULL, 0);
  35693. if (ret != MP_LT)
  35694. return -12769;
  35695. ret = mp_cmp(NULL, NULL);
  35696. if (ret != MP_EQ)
  35697. return -12770;
  35698. ret = mp_cmp(a, NULL);
  35699. if (ret != MP_GT)
  35700. return -12771;
  35701. ret = mp_cmp(NULL, b);
  35702. if (ret != MP_LT)
  35703. return -12772;
  35704. #ifdef WOLFSSL_SP_MATH_ALL
  35705. mp_rshd(NULL, 1);
  35706. #endif
  35707. mp_zero(NULL);
  35708. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  35709. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  35710. ret = mp_lshd(NULL, 0);
  35711. if (ret != MP_VAL)
  35712. return -12773;
  35713. ret = mp_lshd(a, SP_INT_DIGITS + 1);
  35714. if (ret != MP_VAL)
  35715. return -12774;
  35716. #endif
  35717. #if defined(WOLFSSL_SP_MATH_ALL)
  35718. ret = mp_div(NULL, NULL, a, b);
  35719. if (ret != MP_VAL)
  35720. return -12775;
  35721. ret = mp_div(a, NULL, a, b);
  35722. if (ret != MP_VAL)
  35723. return -12776;
  35724. ret = mp_div(NULL, b, a, b);
  35725. if (ret != MP_VAL)
  35726. return -12777;
  35727. ret = mp_div(a, b, NULL, NULL);
  35728. if (ret != MP_VAL)
  35729. return -12778;
  35730. #endif
  35731. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  35732. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  35733. ret = mp_mod(NULL, NULL, NULL);
  35734. if (ret != MP_VAL)
  35735. return -12779;
  35736. ret = mp_mod(a, NULL, NULL);
  35737. if (ret != MP_VAL)
  35738. return -12780;
  35739. ret = mp_mod(NULL, b, NULL);
  35740. if (ret != MP_VAL)
  35741. return -12781;
  35742. ret = mp_mod(NULL, NULL, r);
  35743. if (ret != MP_VAL)
  35744. return -12782;
  35745. ret = mp_mod(a, b, NULL);
  35746. if (ret != MP_VAL)
  35747. return -12783;
  35748. ret = mp_mod(a, NULL, r);
  35749. if (ret != MP_VAL)
  35750. return -12784;
  35751. ret = mp_mod(NULL, b, r);
  35752. if (ret != MP_VAL)
  35753. return -12785;
  35754. #endif
  35755. #if !defined(NO_RSA) || defined(WOLFSSL_SP_MATH_ALL)
  35756. ret = mp_set_int(NULL, 0);
  35757. if (ret != MP_VAL)
  35758. return -12786;
  35759. #endif
  35760. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  35761. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  35762. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  35763. if (ret != MP_VAL)
  35764. return 9950;
  35765. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  35766. if (ret != MP_VAL)
  35767. return 9951;
  35768. ret = mp_exptmod_ex(NULL, a, 1, NULL, NULL);
  35769. if (ret != MP_VAL)
  35770. return 9952;
  35771. ret = mp_exptmod_ex(NULL, NULL, 1, a, NULL);
  35772. if (ret != MP_VAL)
  35773. return 9953;
  35774. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  35775. if (ret != MP_VAL)
  35776. return 9954;
  35777. ret = mp_exptmod_ex(a, a, 1, a, NULL);
  35778. if (ret != MP_VAL)
  35779. return 9955;
  35780. ret = mp_exptmod_ex(a, a, 1, NULL, a);
  35781. if (ret != MP_VAL)
  35782. return 9956;
  35783. ret = mp_exptmod_ex(a, NULL, 1, a, a);
  35784. if (ret != MP_VAL)
  35785. return 9957;
  35786. ret = mp_exptmod_ex(NULL, a, 1, a, a);
  35787. if (ret != MP_VAL)
  35788. return 9958;
  35789. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  35790. if (ret != MP_VAL)
  35791. return 9960;
  35792. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  35793. if (ret != MP_VAL)
  35794. return 9961;
  35795. ret = mp_exptmod_nct(NULL, a, NULL, NULL);
  35796. if (ret != MP_VAL)
  35797. return 9962;
  35798. ret = mp_exptmod_nct(NULL, NULL, a, NULL);
  35799. if (ret != MP_VAL)
  35800. return 9963;
  35801. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  35802. if (ret != MP_VAL)
  35803. return 9964;
  35804. ret = mp_exptmod_nct(a, a, a, NULL);
  35805. if (ret != MP_VAL)
  35806. return 9965;
  35807. ret = mp_exptmod_nct(a, a, NULL, a);
  35808. if (ret != MP_VAL)
  35809. return 9966;
  35810. ret = mp_exptmod_nct(a, NULL, a, a);
  35811. if (ret != MP_VAL)
  35812. return 9967;
  35813. ret = mp_exptmod_nct(NULL, a, a, a);
  35814. if (ret != MP_VAL)
  35815. return 9968;
  35816. #endif
  35817. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  35818. !defined(WC_NO_RNG)
  35819. ret = mp_rand_prime(NULL, 32, NULL, NULL);
  35820. if (ret != MP_VAL)
  35821. return -12787;
  35822. ret = mp_rand_prime(a, 32, NULL, NULL);
  35823. if (ret != MP_VAL)
  35824. return -12788;
  35825. ret = mp_rand_prime(NULL, 32, rng, NULL);
  35826. if (ret != MP_VAL)
  35827. return -12789;
  35828. ret = mp_rand_prime(a, 0, rng, NULL);
  35829. if (ret != MP_VAL)
  35830. return -9969;
  35831. #endif
  35832. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  35833. ret = mp_mul(NULL, NULL, NULL);
  35834. if (ret != MP_VAL)
  35835. return -12790;
  35836. ret = mp_mul(a, NULL, NULL);
  35837. if (ret != MP_VAL)
  35838. return -12791;
  35839. ret = mp_mul(NULL, b, NULL);
  35840. if (ret != MP_VAL)
  35841. return -12792;
  35842. ret = mp_mul(NULL, NULL, r);
  35843. if (ret != MP_VAL)
  35844. return -12793;
  35845. ret = mp_mul(a, b, NULL);
  35846. if (ret != MP_VAL)
  35847. return -12794;
  35848. ret = mp_mul(a, NULL, r);
  35849. if (ret != MP_VAL)
  35850. return -12795;
  35851. ret = mp_mul(NULL, b, r);
  35852. if (ret != MP_VAL)
  35853. return -12796;
  35854. #endif
  35855. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  35856. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  35857. ret = mp_sqr(NULL, NULL);
  35858. if (ret != MP_VAL)
  35859. return -12797;
  35860. ret = mp_sqr(a, NULL);
  35861. if (ret != MP_VAL)
  35862. return -12798;
  35863. ret = mp_sqr(NULL, r);
  35864. if (ret != MP_VAL)
  35865. return -12799;
  35866. #endif
  35867. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  35868. ret = mp_sqrmod(NULL, NULL, NULL);
  35869. if (ret != MP_VAL)
  35870. return -12800;
  35871. ret = mp_sqrmod(a, NULL, NULL);
  35872. if (ret != MP_VAL)
  35873. return -12801;
  35874. ret = mp_sqrmod(NULL, a, NULL);
  35875. if (ret != MP_VAL)
  35876. return -12802;
  35877. ret = mp_sqrmod(NULL, NULL, a);
  35878. if (ret != MP_VAL)
  35879. return -12803;
  35880. ret = mp_sqrmod(a, b, NULL);
  35881. if (ret != MP_VAL)
  35882. return -12804;
  35883. ret = mp_sqrmod(a, NULL, b);
  35884. if (ret != MP_VAL)
  35885. return -12805;
  35886. ret = mp_sqrmod(NULL, a, b);
  35887. if (ret != MP_VAL)
  35888. return -12806;
  35889. ret = mp_mulmod(NULL, NULL, NULL, NULL);
  35890. if (ret != MP_VAL)
  35891. return -12807;
  35892. ret = mp_mulmod(a, NULL, NULL, NULL);
  35893. if (ret != MP_VAL)
  35894. return -12808;
  35895. ret = mp_mulmod(NULL, a, NULL, NULL);
  35896. if (ret != MP_VAL)
  35897. return -12809;
  35898. ret = mp_mulmod(NULL, NULL, a, NULL);
  35899. if (ret != MP_VAL)
  35900. return -12810;
  35901. ret = mp_mulmod(NULL, NULL, NULL, a);
  35902. if (ret != MP_VAL)
  35903. return -12811;
  35904. ret = mp_mulmod(a, b, b, NULL);
  35905. if (ret != MP_VAL)
  35906. return -12812;
  35907. ret = mp_mulmod(a, b, NULL, a);
  35908. if (ret != MP_VAL)
  35909. return -12813;
  35910. ret = mp_mulmod(a, NULL, b, a);
  35911. if (ret != MP_VAL)
  35912. return -12814;
  35913. ret = mp_mulmod(NULL, b, b, a);
  35914. if (ret != MP_VAL)
  35915. return -12815;
  35916. #endif
  35917. #if !defined(NO_PWDBASED) || defined(WOLFSSL_KEY_GEN) || !defined(NO_DH) || \
  35918. !defined(NO_RSA) || !defined(NO_DSA)
  35919. ret = mp_add_d(NULL, 1, NULL);
  35920. if (ret != MP_VAL)
  35921. return -12816;
  35922. ret = mp_add_d(a, 1, NULL);
  35923. if (ret != MP_VAL)
  35924. return -12817;
  35925. ret = mp_add_d(NULL, 1, b);
  35926. if (ret != MP_VAL)
  35927. return -12818;
  35928. #endif
  35929. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  35930. !defined(NO_DH) || defined(HAVE_ECC) || !defined(NO_DSA)
  35931. ret = mp_sub_d(NULL, 1, NULL);
  35932. if (ret != MP_VAL)
  35933. return -12819;
  35934. ret = mp_sub_d(a, 1, NULL);
  35935. if (ret != MP_VAL)
  35936. return -12820;
  35937. ret = mp_sub_d(NULL, 1, b);
  35938. if (ret != MP_VAL)
  35939. return -12821;
  35940. #endif
  35941. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  35942. defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  35943. ret = mp_div_d(NULL, 0, NULL, NULL);
  35944. if (ret != MP_VAL)
  35945. return -12822;
  35946. ret = mp_div_d(a, 0, NULL, NULL);
  35947. if (ret != MP_VAL)
  35948. return -12823;
  35949. ret = mp_div_d(NULL, 1, NULL, NULL);
  35950. if (ret != MP_VAL)
  35951. return -12824;
  35952. #endif
  35953. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  35954. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  35955. ret = mp_mod_d(NULL, 0, NULL);
  35956. if (ret != MP_VAL)
  35957. return -12825;
  35958. ret = mp_mod_d(a, 0, NULL);
  35959. if (ret != MP_VAL)
  35960. return -12826;
  35961. ret = mp_mod_d(NULL, 0, &rd);
  35962. if (ret != MP_VAL)
  35963. return -12827;
  35964. #endif
  35965. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  35966. ret = mp_gcd(NULL, NULL, NULL);
  35967. if (ret != MP_VAL)
  35968. return -12828;
  35969. ret = mp_gcd(a, NULL, NULL);
  35970. if (ret != MP_VAL)
  35971. return -12829;
  35972. ret = mp_gcd(NULL, a, NULL);
  35973. if (ret != MP_VAL)
  35974. return -12830;
  35975. ret = mp_gcd(NULL, NULL, a);
  35976. if (ret != MP_VAL)
  35977. return -12831;
  35978. ret = mp_gcd(a, b, NULL);
  35979. if (ret != MP_VAL)
  35980. return -12832;
  35981. ret = mp_gcd(a, NULL, b);
  35982. if (ret != MP_VAL)
  35983. return -12833;
  35984. ret = mp_gcd(NULL, a, b);
  35985. if (ret != MP_VAL)
  35986. return -12834;
  35987. #endif
  35988. #if !defined(WOLFSSL_SP_MATH) && defined(HAVE_ECC)
  35989. ret = mp_div_2_mod_ct(NULL, NULL, NULL);
  35990. if (ret != MP_VAL)
  35991. return -12835;
  35992. ret = mp_div_2_mod_ct(a, NULL, NULL);
  35993. if (ret != MP_VAL)
  35994. return -12836;
  35995. ret = mp_div_2_mod_ct(NULL, b, NULL);
  35996. if (ret != MP_VAL)
  35997. return -12837;
  35998. ret = mp_div_2_mod_ct(NULL, NULL, a);
  35999. if (ret != MP_VAL)
  36000. return -12838;
  36001. ret = mp_div_2_mod_ct(a, b, NULL);
  36002. if (ret != MP_VAL)
  36003. return -12839;
  36004. ret = mp_div_2_mod_ct(a, b, NULL);
  36005. if (ret != MP_VAL)
  36006. return -12840;
  36007. ret = mp_div_2_mod_ct(NULL, b, a);
  36008. if (ret != MP_VAL)
  36009. return -12841;
  36010. ret = mp_div_2(NULL, NULL);
  36011. if (ret != MP_VAL)
  36012. return -12842;
  36013. ret = mp_div_2(a, NULL);
  36014. if (ret != MP_VAL)
  36015. return -12843;
  36016. ret = mp_div_2(NULL, a);
  36017. if (ret != MP_VAL)
  36018. return -12844;
  36019. #endif
  36020. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  36021. defined(HAVE_ECC) || !defined(NO_DSA) || defined(OPENSSL_EXTRA)
  36022. ret = mp_invmod(NULL, NULL, NULL);
  36023. if (ret != MP_VAL)
  36024. return -12845;
  36025. ret = mp_invmod(a, NULL, NULL);
  36026. if (ret != MP_VAL)
  36027. return -12846;
  36028. ret = mp_invmod(NULL, b, NULL);
  36029. if (ret != MP_VAL)
  36030. return -12847;
  36031. ret = mp_invmod(NULL, NULL, a);
  36032. if (ret != MP_VAL)
  36033. return -12848;
  36034. ret = mp_invmod(a, b, NULL);
  36035. if (ret != MP_VAL)
  36036. return -12849;
  36037. ret = mp_invmod(a, NULL, a);
  36038. if (ret != MP_VAL)
  36039. return -12850;
  36040. ret = mp_invmod(NULL, b, a);
  36041. if (ret != MP_VAL)
  36042. return -12851;
  36043. #endif
  36044. #if !defined(WOLFSSL_SP_MATH) && defined(HAVE_ECC)
  36045. ret = mp_invmod_mont_ct(NULL, NULL, NULL, 1);
  36046. if (ret != MP_VAL)
  36047. return -12852;
  36048. ret = mp_invmod_mont_ct(a, NULL, NULL, 1);
  36049. if (ret != MP_VAL)
  36050. return -12853;
  36051. ret = mp_invmod_mont_ct(NULL, b, NULL, 1);
  36052. if (ret != MP_VAL)
  36053. return -12854;
  36054. ret = mp_invmod_mont_ct(NULL, NULL, a, 1);
  36055. if (ret != MP_VAL)
  36056. return -12855;
  36057. ret = mp_invmod_mont_ct(a, b, NULL, 1);
  36058. if (ret != MP_VAL)
  36059. return -12856;
  36060. ret = mp_invmod_mont_ct(a, NULL, a, 1);
  36061. if (ret != MP_VAL)
  36062. return -12857;
  36063. ret = mp_invmod_mont_ct(NULL, b, a, 1);
  36064. if (ret != MP_VAL)
  36065. return -12858;
  36066. #endif
  36067. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  36068. ret = mp_lcm(NULL, NULL, NULL);
  36069. if (ret != MP_VAL)
  36070. return -12859;
  36071. ret = mp_lcm(a, NULL, NULL);
  36072. if (ret != MP_VAL)
  36073. return -12860;
  36074. ret = mp_lcm(NULL, b, NULL);
  36075. if (ret != MP_VAL)
  36076. return -12861;
  36077. ret = mp_lcm(NULL, NULL, a);
  36078. if (ret != MP_VAL)
  36079. return -12862;
  36080. ret = mp_lcm(a, b, NULL);
  36081. if (ret != MP_VAL)
  36082. return -12863;
  36083. ret = mp_lcm(a, NULL, a);
  36084. if (ret != MP_VAL)
  36085. return -12864;
  36086. ret = mp_lcm(NULL, b, a);
  36087. if (ret != MP_VAL)
  36088. return -12865;
  36089. #endif
  36090. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  36091. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  36092. if (ret != MP_VAL)
  36093. return -12866;
  36094. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  36095. if (ret != MP_VAL)
  36096. return -12867;
  36097. ret = mp_exptmod_ex(NULL, b, 1, NULL, NULL);
  36098. if (ret != MP_VAL)
  36099. return -12868;
  36100. ret = mp_exptmod_ex(NULL, NULL, 1, b, NULL);
  36101. if (ret != MP_VAL)
  36102. return -12869;
  36103. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  36104. if (ret != MP_VAL)
  36105. return -12870;
  36106. ret = mp_exptmod_ex(a, b, 1, b, NULL);
  36107. if (ret != MP_VAL)
  36108. return -12871;
  36109. ret = mp_exptmod_ex(a, b, 1, NULL, a);
  36110. if (ret != MP_VAL)
  36111. return -12872;
  36112. ret = mp_exptmod_ex(a, NULL, 1, b, a);
  36113. if (ret != MP_VAL)
  36114. return -12873;
  36115. ret = mp_exptmod_ex(NULL, b, 1, b, a);
  36116. if (ret != MP_VAL)
  36117. return -12874;
  36118. ret = mp_exptmod(NULL, NULL, NULL, NULL);
  36119. if (ret != MP_VAL)
  36120. return -12875;
  36121. ret = mp_exptmod(a, NULL, NULL, NULL);
  36122. if (ret != MP_VAL)
  36123. return -12876;
  36124. ret = mp_exptmod(NULL, b, NULL, NULL);
  36125. if (ret != MP_VAL)
  36126. return -12877;
  36127. ret = mp_exptmod(NULL, NULL, b, NULL);
  36128. if (ret != MP_VAL)
  36129. return -12878;
  36130. ret = mp_exptmod(NULL, NULL, NULL, a);
  36131. if (ret != MP_VAL)
  36132. return -12879;
  36133. ret = mp_exptmod(a, b, b, NULL);
  36134. if (ret != MP_VAL)
  36135. return -12880;
  36136. ret = mp_exptmod(a, b, NULL, a);
  36137. if (ret != MP_VAL)
  36138. return -12881;
  36139. ret = mp_exptmod(a, NULL, b, a);
  36140. if (ret != MP_VAL)
  36141. return -12882;
  36142. ret = mp_exptmod(NULL, b, b, a);
  36143. if (ret != MP_VAL)
  36144. return -12883;
  36145. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  36146. if (ret != MP_VAL)
  36147. return -12884;
  36148. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  36149. if (ret != MP_VAL)
  36150. return -12885;
  36151. ret = mp_exptmod_nct(NULL, b, NULL, NULL);
  36152. if (ret != MP_VAL)
  36153. return -12886;
  36154. ret = mp_exptmod_nct(NULL, NULL, b, NULL);
  36155. if (ret != MP_VAL)
  36156. return -12887;
  36157. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  36158. if (ret != MP_VAL)
  36159. return -12888;
  36160. ret = mp_exptmod_nct(a, b, b, NULL);
  36161. if (ret != MP_VAL)
  36162. return -12889;
  36163. ret = mp_exptmod_nct(a, b, NULL, a);
  36164. if (ret != MP_VAL)
  36165. return -12890;
  36166. ret = mp_exptmod_nct(a, NULL, b, a);
  36167. if (ret != MP_VAL)
  36168. return -12891;
  36169. ret = mp_exptmod_nct(NULL, b, b, a);
  36170. if (ret != MP_VAL)
  36171. return -12892;
  36172. #endif
  36173. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  36174. ret = mp_cnt_lsb(NULL);
  36175. if (ret != 0)
  36176. return -12893;
  36177. #endif
  36178. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  36179. ret = mp_prime_is_prime(NULL, 1, NULL);
  36180. if (ret != MP_VAL)
  36181. return -12894;
  36182. ret = mp_prime_is_prime(a, 1, NULL);
  36183. if (ret != MP_VAL)
  36184. return -12895;
  36185. ret = mp_prime_is_prime(NULL, 1, &result);
  36186. if (ret != MP_VAL)
  36187. return -12896;
  36188. ret = mp_prime_is_prime(a, 0, &result);
  36189. if (ret != MP_VAL)
  36190. return -12897;
  36191. ret = mp_prime_is_prime(a, 1024, &result);
  36192. if (ret != MP_VAL)
  36193. return -12898;
  36194. ret = mp_prime_is_prime_ex(NULL, 1, NULL, NULL);
  36195. if (ret != MP_VAL)
  36196. return -12899;
  36197. ret = mp_prime_is_prime_ex(a, 1, NULL, NULL);
  36198. if (ret != MP_VAL)
  36199. return -12900;
  36200. ret = mp_prime_is_prime_ex(NULL, 1, &result, NULL);
  36201. if (ret != MP_VAL)
  36202. return -12901;
  36203. ret = mp_prime_is_prime_ex(NULL, 1, NULL, rng);
  36204. if (ret != MP_VAL)
  36205. return -12902;
  36206. ret = mp_prime_is_prime_ex(a, 1, &result, NULL);
  36207. if (ret != MP_VAL)
  36208. return -12903;
  36209. ret = mp_prime_is_prime_ex(a, 1, NULL, rng);
  36210. if (ret != MP_VAL)
  36211. return -12904;
  36212. ret = mp_prime_is_prime_ex(NULL, 1, &result, rng);
  36213. if (ret != MP_VAL)
  36214. return -12905;
  36215. #endif
  36216. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || !defined(NO_DSA)
  36217. ret = mp_exch(NULL, NULL);
  36218. if (ret != MP_VAL)
  36219. return -12906;
  36220. ret = mp_exch(a, NULL);
  36221. if (ret != MP_VAL)
  36222. return -12907;
  36223. ret = mp_exch(NULL, b);
  36224. if (ret != MP_VAL)
  36225. return -12908;
  36226. #endif
  36227. #if (defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)) || \
  36228. defined(WOLFSSL_SP_MATH_ALL)
  36229. ret = mp_mul_d(NULL, 1, NULL);
  36230. if (ret != MP_VAL)
  36231. return -12909;
  36232. ret = mp_mul_d(a, 1, NULL);
  36233. if (ret != MP_VAL)
  36234. return -12910;
  36235. ret = mp_mul_d(NULL, 1, b);
  36236. if (ret != MP_VAL)
  36237. return -12911;
  36238. #endif
  36239. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  36240. ret = mp_add(NULL, NULL, NULL);
  36241. if (ret != MP_VAL)
  36242. return -12912;
  36243. ret = mp_add(a, NULL, NULL);
  36244. if (ret != MP_VAL)
  36245. return -12913;
  36246. ret = mp_add(NULL, b, NULL);
  36247. if (ret != MP_VAL)
  36248. return -12914;
  36249. ret = mp_add(NULL, NULL, r);
  36250. if (ret != MP_VAL)
  36251. return -12915;
  36252. ret = mp_add(a, b, NULL);
  36253. if (ret != MP_VAL)
  36254. return -12916;
  36255. ret = mp_add(a, NULL, r);
  36256. if (ret != MP_VAL)
  36257. return -12917;
  36258. ret = mp_add(NULL, b, r);
  36259. if (ret != MP_VAL)
  36260. return -12918;
  36261. #endif
  36262. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  36263. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  36264. ret = mp_sub(NULL, NULL, NULL);
  36265. if (ret != MP_VAL)
  36266. return -12919;
  36267. ret = mp_sub(a, NULL, NULL);
  36268. if (ret != MP_VAL)
  36269. return -12920;
  36270. ret = mp_sub(NULL, b, NULL);
  36271. if (ret != MP_VAL)
  36272. return -12921;
  36273. ret = mp_sub(NULL, NULL, r);
  36274. if (ret != MP_VAL)
  36275. return -12922;
  36276. ret = mp_sub(a, b, NULL);
  36277. if (ret != MP_VAL)
  36278. return -12923;
  36279. ret = mp_sub(a, NULL, r);
  36280. if (ret != MP_VAL)
  36281. return -12924;
  36282. ret = mp_sub(NULL, b, r);
  36283. if (ret != MP_VAL)
  36284. return -12925;
  36285. #endif
  36286. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined(WOLFSSL_SP_MATH) && \
  36287. defined(WOLFSSL_CUSTOM_CURVES))
  36288. ret = mp_addmod(NULL, NULL, NULL, NULL);
  36289. if (ret != MP_VAL)
  36290. return -12926;
  36291. ret = mp_addmod(a, NULL, NULL, NULL);
  36292. if (ret != MP_VAL)
  36293. return -12927;
  36294. ret = mp_addmod(NULL, b, NULL, NULL);
  36295. if (ret != MP_VAL)
  36296. return -12928;
  36297. ret = mp_addmod(NULL, NULL, b, NULL);
  36298. if (ret != MP_VAL)
  36299. return -12929;
  36300. ret = mp_addmod(NULL, NULL, NULL, a);
  36301. if (ret != MP_VAL)
  36302. return -12930;
  36303. ret = mp_addmod(a, b, b, NULL);
  36304. if (ret != MP_VAL)
  36305. return -12931;
  36306. ret = mp_addmod(a, b, NULL, a);
  36307. if (ret != MP_VAL)
  36308. return -12932;
  36309. ret = mp_addmod(a, NULL, b, a);
  36310. if (ret != MP_VAL)
  36311. return -12933;
  36312. ret = mp_addmod(NULL, b, b, a);
  36313. if (ret != MP_VAL)
  36314. return -12934;
  36315. #endif
  36316. #ifdef WOLFSSL_SP_MATH_ALL
  36317. ret = mp_submod(NULL, NULL, NULL, NULL);
  36318. if (ret != MP_VAL)
  36319. return -12935;
  36320. ret = mp_submod(a, NULL, NULL, NULL);
  36321. if (ret != MP_VAL)
  36322. return -12936;
  36323. ret = mp_submod(NULL, b, NULL, NULL);
  36324. if (ret != MP_VAL)
  36325. return -12937;
  36326. ret = mp_submod(NULL, NULL, b, NULL);
  36327. if (ret != MP_VAL)
  36328. return -12938;
  36329. ret = mp_submod(NULL, NULL, NULL, a);
  36330. if (ret != MP_VAL)
  36331. return -12939;
  36332. ret = mp_submod(a, b, b, NULL);
  36333. if (ret != MP_VAL)
  36334. return -12940;
  36335. ret = mp_submod(a, b, NULL, a);
  36336. if (ret != MP_VAL)
  36337. return -12941;
  36338. ret = mp_submod(a, NULL, b, a);
  36339. if (ret != MP_VAL)
  36340. return -12942;
  36341. ret = mp_submod(NULL, b, b, a);
  36342. if (ret != MP_VAL)
  36343. return -12943;
  36344. #endif
  36345. #ifdef WOLFSSL_SP_MATH_ALL
  36346. ret = mp_div_2d(NULL, 1, a, b);
  36347. if (ret != MP_VAL)
  36348. return -12944;
  36349. ret = mp_mod_2d(NULL, 1, NULL);
  36350. if (ret != MP_VAL)
  36351. return -12945;
  36352. ret = mp_mod_2d(a, 1, NULL);
  36353. if (ret != MP_VAL)
  36354. return -12946;
  36355. ret = mp_mod_2d(NULL, 1, b);
  36356. if (ret != MP_VAL)
  36357. return -12947;
  36358. ret = mp_mul_2d(NULL, 1, NULL);
  36359. if (ret != MP_VAL)
  36360. return -12948;
  36361. ret = mp_mul_2d(a, 1, NULL);
  36362. if (ret != MP_VAL)
  36363. return -12949;
  36364. ret = mp_mul_2d(NULL, 1, b);
  36365. if (ret != MP_VAL)
  36366. return -12950;
  36367. #endif
  36368. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  36369. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  36370. ret = mp_montgomery_reduce(NULL, NULL, 1);
  36371. if (ret != MP_VAL)
  36372. return -12951;
  36373. ret = mp_montgomery_reduce(a, NULL, 1);
  36374. if (ret != MP_VAL)
  36375. return -12952;
  36376. ret = mp_montgomery_reduce(NULL, b, 1);
  36377. if (ret != MP_VAL)
  36378. return -12953;
  36379. mp_zero(b);
  36380. ret = mp_montgomery_reduce(a, b, 1);
  36381. if (ret != MP_VAL)
  36382. return -12954;
  36383. #endif
  36384. #ifdef WOLFSSL_SP_MATH_ALL
  36385. ret = mp_montgomery_setup(NULL, NULL);
  36386. if (ret != MP_VAL)
  36387. return -12955;
  36388. ret = mp_montgomery_setup(a, NULL);
  36389. if (ret != MP_VAL)
  36390. return -12956;
  36391. ret = mp_montgomery_setup(NULL, &rho);
  36392. if (ret != MP_VAL)
  36393. return -12957;
  36394. ret = mp_montgomery_calc_normalization(NULL, NULL);
  36395. if (ret != MP_VAL)
  36396. return -12958;
  36397. ret = mp_montgomery_calc_normalization(a, NULL);
  36398. if (ret != MP_VAL)
  36399. return -12959;
  36400. ret = mp_montgomery_calc_normalization(NULL, b);
  36401. if (ret != MP_VAL)
  36402. return -12960;
  36403. #endif
  36404. ret = mp_unsigned_bin_size(NULL);
  36405. if (ret != 0)
  36406. return -12961;
  36407. #if defined(WC_MP_TO_RADIX) || defined(WOLFSSL_SP_MATH_ALL)
  36408. ret = mp_tohex(NULL, NULL);
  36409. if (ret != MP_VAL)
  36410. return -12962;
  36411. ret = mp_tohex(a, NULL);
  36412. if (ret != MP_VAL)
  36413. return -12963;
  36414. ret = mp_tohex(NULL, hexStr);
  36415. if (ret != MP_VAL)
  36416. return -12964;
  36417. #endif
  36418. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  36419. ret = mp_todecimal(NULL, NULL);
  36420. if (ret != MP_VAL)
  36421. return -12965;
  36422. ret = mp_todecimal(a, NULL);
  36423. if (ret != MP_VAL)
  36424. return -12966;
  36425. ret = mp_todecimal(NULL, decStr);
  36426. if (ret != MP_VAL)
  36427. return -12967;
  36428. #endif
  36429. #ifdef WOLFSSL_SP_MATH_ALL
  36430. ret = mp_toradix(NULL, NULL, MP_RADIX_HEX);
  36431. if (ret != MP_VAL)
  36432. return -12968;
  36433. ret = mp_toradix(a, NULL, MP_RADIX_HEX);
  36434. if (ret != MP_VAL)
  36435. return -12969;
  36436. ret = mp_toradix(NULL, hexStr, MP_RADIX_HEX);
  36437. if (ret != MP_VAL)
  36438. return -12970;
  36439. ret = mp_toradix(a, hexStr, 3);
  36440. if (ret != MP_VAL)
  36441. return -12971;
  36442. ret = mp_radix_size(NULL, MP_RADIX_HEX, NULL);
  36443. if (ret != MP_VAL)
  36444. return -12972;
  36445. ret = mp_radix_size(a, MP_RADIX_HEX, NULL);
  36446. if (ret != MP_VAL)
  36447. return -12973;
  36448. ret = mp_radix_size(NULL, MP_RADIX_HEX, &size);
  36449. if (ret != MP_VAL)
  36450. return -12974;
  36451. ret = mp_radix_size(a, 3, &size);
  36452. if (ret != MP_VAL)
  36453. return -12975;
  36454. #endif
  36455. return 0;
  36456. }
  36457. #endif
  36458. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  36459. static int mp_test_set_is_bit(mp_int* a)
  36460. {
  36461. int i, j;
  36462. mp_zero(a);
  36463. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  36464. if (mp_is_bit_set(a, i))
  36465. return -12980;
  36466. for (j = 0; j < i; j++) {
  36467. if (!mp_is_bit_set(a, j))
  36468. return -12981;
  36469. }
  36470. if (mp_set_bit(a, i) != 0)
  36471. return -12982;
  36472. if (!mp_is_bit_set(a, i))
  36473. return -12983;
  36474. }
  36475. mp_zero(a);
  36476. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  36477. if (mp_is_bit_set(a, i))
  36478. return -12984;
  36479. }
  36480. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  36481. mp_zero(a);
  36482. if (mp_set_bit(a, i) != 0)
  36483. return -12985;
  36484. for (j = 0; j < i; j++) {
  36485. if (mp_is_bit_set(a, j))
  36486. return -12986;
  36487. }
  36488. if (!mp_is_bit_set(a, i))
  36489. return -12987;
  36490. }
  36491. #ifdef WOLFSSL_KEY_GEN
  36492. for (i = 0; i < DIGIT_BIT * 2; i++) {
  36493. mp_set(a, 1);
  36494. if (mp_2expt(a, i) != 0)
  36495. return -12988;
  36496. for (j = 0; j < i; j++) {
  36497. if (mp_is_bit_set(a, j))
  36498. return -12989;
  36499. }
  36500. if (!mp_is_bit_set(a, i))
  36501. return -12990;
  36502. }
  36503. #endif
  36504. #ifdef WOLFSSL_SP_MATH
  36505. mp_zero(a);
  36506. for (j = 1; j <= 3; j++) {
  36507. i = SP_INT_MAX_BITS - j;
  36508. if (mp_is_bit_set(a, i))
  36509. return -12991;
  36510. if (mp_set_bit(a, i) != 0)
  36511. return -12992;
  36512. if (!mp_is_bit_set(a, i))
  36513. return -12993;
  36514. #ifdef WOLFSSL_KEY_GEN
  36515. if (mp_2expt(a, i) != 0)
  36516. return -12994;
  36517. if (!mp_is_bit_set(a, i))
  36518. return -12995;
  36519. #endif
  36520. }
  36521. mp_zero(a);
  36522. for (j = 0; j <= 3; j++) {
  36523. i = SP_INT_MAX_BITS + j;
  36524. if (mp_is_bit_set(a, i))
  36525. return -12996;
  36526. if (mp_set_bit(a, i) != MP_VAL)
  36527. return -12997;
  36528. #ifdef WOLFSSL_KEY_GEN
  36529. if (mp_2expt(a, i) != MP_VAL)
  36530. return -12998;
  36531. #endif
  36532. }
  36533. #endif
  36534. return 0;
  36535. }
  36536. #endif /* !WOLFSSL_SP_MATH || WOLFSSL_SP_MATH_ALL */
  36537. static int mp_test_cmp(mp_int* a, mp_int* b)
  36538. {
  36539. int ret;
  36540. mp_zero(a);
  36541. mp_zero(b);
  36542. ret = mp_cmp_d(a, 0);
  36543. if (ret != MP_EQ)
  36544. return -13000;
  36545. ret = mp_cmp_d(a, 1);
  36546. if (ret != MP_LT)
  36547. return -13001;
  36548. ret = mp_cmp(a, b);
  36549. if (ret != MP_EQ)
  36550. return -13002;
  36551. mp_set(a, 1);
  36552. ret = mp_cmp_d(a, 0);
  36553. if (ret != MP_GT)
  36554. return -13003;
  36555. ret = mp_cmp_d(a, 1);
  36556. if (ret != MP_EQ)
  36557. return -13004;
  36558. ret = mp_cmp_d(a, 2);
  36559. if (ret != MP_LT)
  36560. return -13005;
  36561. ret = mp_cmp(a, b);
  36562. if (ret != MP_GT)
  36563. return -13006;
  36564. mp_read_radix(b, "1234567890123456789", MP_RADIX_HEX);
  36565. ret = mp_cmp_d(b, -1);
  36566. if (ret != MP_GT)
  36567. return -13007;
  36568. ret = mp_cmp(a, b);
  36569. if (ret != MP_LT)
  36570. return -13008;
  36571. ret = mp_cmp(b, a);
  36572. if (ret != MP_GT)
  36573. return -13009;
  36574. ret = mp_cmp(b, b);
  36575. if (ret != MP_EQ)
  36576. return -13010;
  36577. #if (!defined(WOLFSSL_SP_MATH) && !defined(WOLFSSL_SP_MATH_ALL)) || \
  36578. defined(WOLFSSL_SP_INT_NEGATIVE)
  36579. mp_read_radix(a, "-1", MP_RADIX_HEX);
  36580. mp_read_radix(a, "1", MP_RADIX_HEX);
  36581. ret = mp_cmp(a, b);
  36582. if (ret != MP_LT)
  36583. return -13011;
  36584. ret = mp_cmp(b, a);
  36585. if (ret != MP_GT)
  36586. return -13012;
  36587. mp_read_radix(b, "-2", MP_RADIX_HEX);
  36588. ret = mp_cmp(a, b);
  36589. if (ret != MP_GT)
  36590. return -13013;
  36591. ret = mp_cmp(b, a);
  36592. if (ret != MP_LT)
  36593. return -13014;
  36594. mp_read_radix(a, "-2", MP_RADIX_HEX);
  36595. ret = mp_cmp(a, b);
  36596. if (ret != MP_EQ)
  36597. return -13015;
  36598. #endif
  36599. return 0;
  36600. }
  36601. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  36602. static int mp_test_shbd(mp_int* a, mp_int* b, WC_RNG* rng)
  36603. {
  36604. int ret;
  36605. int i, j, k;
  36606. #ifndef WOLFSSL_SP_MATH
  36607. for (i = 0; i < 10; i++) {
  36608. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  36609. ret = randNum(a, j, rng, NULL);
  36610. if (ret != MP_OKAY)
  36611. return -13020;
  36612. mp_copy(a, b);
  36613. for (k = 0; k <= DIGIT_BIT * 2; k++) {
  36614. ret = mp_mul_2d(a, k, a);
  36615. if (ret != MP_OKAY)
  36616. return -13021;
  36617. mp_rshb(a, k);
  36618. if (mp_cmp(a, b) != MP_EQ)
  36619. return -13022;
  36620. }
  36621. }
  36622. }
  36623. #endif
  36624. for (i = 0; i < 10; i++) {
  36625. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  36626. ret = randNum(a, j, rng, NULL);
  36627. if (ret != MP_OKAY)
  36628. return -13023;
  36629. mp_copy(a, b);
  36630. for (k = 0; k < 10; k++) {
  36631. ret = mp_lshd(a, k);
  36632. if (ret != MP_OKAY)
  36633. return -13024;
  36634. #ifndef WOLFSSL_SP_MATH
  36635. mp_rshd(a, k);
  36636. #else
  36637. mp_rshb(a, k * SP_WORD_SIZE);
  36638. #endif
  36639. if (mp_cmp(a, b) != MP_EQ)
  36640. return -13025;
  36641. }
  36642. }
  36643. }
  36644. #ifndef WOLFSSL_SP_MATH
  36645. mp_zero(a);
  36646. mp_rshd(a, 1);
  36647. if (!mp_iszero(a))
  36648. return -13026;
  36649. mp_set(a, 1);
  36650. mp_rshd(a, 1);
  36651. if (!mp_iszero(a))
  36652. return -13027;
  36653. mp_set(a, 1);
  36654. mp_rshd(a, 2);
  36655. if (!mp_iszero(a))
  36656. return -13028;
  36657. #endif
  36658. return 0;
  36659. }
  36660. #endif
  36661. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  36662. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY) && \
  36663. !defined(WOLFSSL_RSA_PUBLIC_ONLY))
  36664. static int mp_test_div(mp_int* a, mp_int* d, mp_int* r, mp_int* rem,
  36665. WC_RNG* rng)
  36666. {
  36667. int ret;
  36668. int i, j, k;
  36669. mp_zero(a);
  36670. mp_zero(d);
  36671. ret = mp_div(a, d, r, rem);
  36672. if (ret != MP_VAL)
  36673. return -13030;
  36674. mp_set(d, 1);
  36675. ret = mp_div(a, d, r, rem);
  36676. if (ret != MP_OKAY)
  36677. return -13031;
  36678. if (!mp_iszero(r))
  36679. return -13032;
  36680. if (!mp_iszero(rem))
  36681. return -13033;
  36682. mp_set(a, 1);
  36683. ret = mp_div(a, d, r, rem);
  36684. if (ret != MP_OKAY)
  36685. return -13034;
  36686. if (!mp_isone(r))
  36687. return -13035;
  36688. if (!mp_iszero(rem))
  36689. return -13036;
  36690. for (i = 0; i < 100; i++) {
  36691. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 2; j++) {
  36692. ret = randNum(d, j, rng, NULL);
  36693. if (ret != MP_OKAY)
  36694. return -13037;
  36695. for (k = 1; k < (DIGIT_BIT + 7) / 8 * 2 + 1; k++) {
  36696. ret = randNum(a, k, rng, NULL);
  36697. if (ret != MP_OKAY)
  36698. return -13038;
  36699. ret = mp_div(a, d, NULL, rem);
  36700. if (ret != MP_OKAY)
  36701. return -13039;
  36702. ret = mp_div(a, d, r, NULL);
  36703. if (ret != MP_OKAY)
  36704. return -13040;
  36705. ret = mp_div(a, d, r, rem);
  36706. if (ret != MP_OKAY)
  36707. return -13041;
  36708. mp_mul(r, d, r);
  36709. mp_add(r, rem, r);
  36710. if (mp_cmp(r, a) != MP_EQ)
  36711. return -13042;
  36712. }
  36713. }
  36714. }
  36715. ret = randNum(d, (DIGIT_BIT + 7) / 8 * 2, rng, NULL);
  36716. if (ret != MP_OKAY)
  36717. return -13043;
  36718. mp_add(d, d, a);
  36719. mp_set(rem, 1);
  36720. mp_div(a, d, NULL, rem);
  36721. if (ret != MP_OKAY)
  36722. return -13044;
  36723. if (!mp_iszero(rem))
  36724. return -13045;
  36725. mp_set(r, 1);
  36726. mp_div(a, d, r, NULL);
  36727. if (ret != MP_OKAY)
  36728. return -13046;
  36729. if (mp_cmp_d(r, 2) != MP_EQ)
  36730. return -13047;
  36731. mp_set(r, 1);
  36732. mp_set(rem, 1);
  36733. mp_div(a, d, r, rem);
  36734. if (ret != MP_OKAY)
  36735. return -13048;
  36736. if (mp_cmp_d(r, 2) != MP_EQ)
  36737. return -13049;
  36738. if (!mp_iszero(rem))
  36739. return -13050;
  36740. mp_set(a, 0xfe);
  36741. mp_lshd(a, 3);
  36742. mp_add_d(a, 0xff, a);
  36743. mp_set(d, 0xfe);
  36744. mp_lshd(d, 2);
  36745. ret = mp_div(a, d, r, rem);
  36746. if (ret != MP_OKAY)
  36747. return -13051;
  36748. mp_mul(r, d, d);
  36749. mp_add(rem, d, d);
  36750. if (mp_cmp(a, d) != MP_EQ)
  36751. return -13052;
  36752. /* Force (hi | lo) / d to be (d | 0) / d which will would not fit in
  36753. * a digit. So mp_div must detect and handle.
  36754. * For example: 0x800000 / 0x8001, DIGIT_BIT = 8
  36755. */
  36756. mp_zero(a);
  36757. mp_set_bit(a, DIGIT_BIT * 3 - 1);
  36758. mp_zero(d);
  36759. mp_set_bit(d, DIGIT_BIT * 2 - 1);
  36760. mp_add_d(d, 1, d);
  36761. ret = mp_div(a, d, r, rem);
  36762. if (ret != MP_OKAY)
  36763. return -13053;
  36764. /* Make sure [d | d] / d is handled. */
  36765. mp_zero(a);
  36766. mp_set_bit(a, DIGIT_BIT * 2 - 1);
  36767. mp_set_bit(a, DIGIT_BIT * 1 - 1);
  36768. mp_zero(d);
  36769. mp_set_bit(d, DIGIT_BIT - 1);
  36770. ret = mp_div(a, d, r, rem);
  36771. if (ret != MP_OKAY)
  36772. return -13054;
  36773. mp_zero(a);
  36774. mp_set_bit(a, DIGIT_BIT);
  36775. mp_set_bit(a, 0);
  36776. mp_zero(d);
  36777. if (mp_cmp(r, a) != MP_EQ)
  36778. return -13055;
  36779. if (mp_cmp(rem, d) != MP_EQ)
  36780. return -13056;
  36781. return 0;
  36782. }
  36783. #endif
  36784. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  36785. !defined(WC_NO_RNG)
  36786. static int mp_test_prime(mp_int* a, WC_RNG* rng)
  36787. {
  36788. int ret;
  36789. int res;
  36790. ret = mp_rand_prime(a, 1, rng, NULL);
  36791. #if defined(WOLFSSL_SP_MATH_ALL)
  36792. if (ret != 0)
  36793. return -13060;
  36794. #else
  36795. if (ret != MP_VAL)
  36796. return -13060;
  36797. #endif
  36798. #ifndef WOLFSSL_SP_MATH
  36799. ret = mp_rand_prime(a, -5, rng, NULL);
  36800. if (ret != 0 || (a->dp[0] & 3) != 3)
  36801. return -13061;
  36802. #endif
  36803. ret = mp_prime_is_prime(a, 1, &res);
  36804. if (ret != MP_OKAY)
  36805. return -13062;
  36806. #ifndef WOLFSSL_SP_MATH
  36807. if (res != MP_YES)
  36808. return -13063;
  36809. #else
  36810. if (res != MP_NO)
  36811. return -13063;
  36812. #endif
  36813. ret = mp_prime_is_prime(a, 0, &res);
  36814. if (ret != MP_VAL)
  36815. return -13064;
  36816. ret = mp_prime_is_prime(a, -1, &res);
  36817. if (ret != MP_VAL)
  36818. return -13065;
  36819. ret = mp_prime_is_prime(a, 257, &res);
  36820. if (ret != MP_VAL)
  36821. return -13066;
  36822. mp_set(a, 1);
  36823. ret = mp_prime_is_prime(a, 1, &res);
  36824. if (ret != MP_OKAY)
  36825. return -13067;
  36826. if (res != MP_NO)
  36827. return -13068;
  36828. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  36829. if (ret != MP_OKAY)
  36830. return -13069;
  36831. if (res != MP_NO)
  36832. return -13070;
  36833. mp_set(a, 2);
  36834. ret = mp_prime_is_prime(a, 1, &res);
  36835. if (ret != MP_OKAY)
  36836. return -13071;
  36837. if (res != MP_YES)
  36838. return -13072;
  36839. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  36840. if (ret != MP_OKAY)
  36841. return -13073;
  36842. if (res != MP_YES)
  36843. return -13074;
  36844. mp_set(a, 0xfb);
  36845. ret = mp_prime_is_prime(a, 1, &res);
  36846. if (ret != MP_OKAY)
  36847. return -13075;
  36848. if (res != MP_YES)
  36849. return -13076;
  36850. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  36851. if (ret != MP_OKAY)
  36852. return -13077;
  36853. if (res != MP_YES)
  36854. return -13078;
  36855. mp_set(a, 0x6);
  36856. ret = mp_prime_is_prime(a, 1, &res);
  36857. if (ret != MP_OKAY)
  36858. return -13079;
  36859. if (res != MP_NO)
  36860. return -13080;
  36861. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  36862. if (ret != MP_OKAY)
  36863. return -13081;
  36864. if (res != MP_NO)
  36865. return -13082;
  36866. mp_set_int(a, 0x655 * 0x65b);
  36867. ret = mp_prime_is_prime(a, 10, &res);
  36868. if (ret != MP_OKAY)
  36869. return -13083;
  36870. if (res != MP_NO)
  36871. return -13084;
  36872. ret = mp_prime_is_prime_ex(a, 10, &res, rng);
  36873. if (ret != MP_OKAY)
  36874. return -13085;
  36875. if (res != MP_NO)
  36876. return -13086;
  36877. return 0;
  36878. }
  36879. #endif
  36880. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  36881. static int mp_test_lcm_gcd(mp_int* a, mp_int* b, mp_int* r, mp_int* exp,
  36882. WC_RNG* rng)
  36883. {
  36884. int ret;
  36885. int i;
  36886. WOLFSSL_SMALL_STACK_STATIC const int kat[][3] = {
  36887. { 1, 1, 1 }, { 2, 1, 2 }, { 1, 2, 2 }, { 2, 4, 4 }, { 4, 2, 4 },
  36888. { 12, 56, 168 }, { 56, 12, 168 }
  36889. };
  36890. (void)exp;
  36891. mp_set(a, 0);
  36892. mp_set(b, 1);
  36893. ret = mp_lcm(a, a, r);
  36894. if (ret != MP_VAL)
  36895. return -13090;
  36896. ret = mp_lcm(a, b, r);
  36897. if (ret != MP_VAL)
  36898. return -13091;
  36899. ret = mp_lcm(b, a, r);
  36900. if (ret != MP_VAL)
  36901. return -13092;
  36902. for (i = 0; i < (int)(sizeof(kat) / sizeof(*kat)); i++) {
  36903. mp_set(a, kat[i][0]);
  36904. mp_set(b, kat[i][1]);
  36905. ret = mp_lcm(a, b, r);
  36906. if (ret != MP_OKAY)
  36907. return -13093;
  36908. mp_set(exp, kat[i][2]);
  36909. if (mp_cmp(r, exp) != MP_EQ)
  36910. return -13094;
  36911. }
  36912. (void)rng;
  36913. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  36914. !defined(WC_NO_RNG)
  36915. if (mp_rand_prime(a, 20, rng, NULL) != MP_OKAY)
  36916. return -13095;
  36917. if (mp_rand_prime(b, 20, rng, NULL) != MP_OKAY)
  36918. return -13096;
  36919. if (mp_mul(a, b, exp) != MP_OKAY)
  36920. return -13097;
  36921. ret = mp_lcm(a, b, r);
  36922. if (ret != MP_OKAY)
  36923. return -13098;
  36924. if (mp_cmp(r, exp) != MP_EQ)
  36925. return -13099;
  36926. ret = mp_lcm(b, a, r);
  36927. if (ret != MP_OKAY)
  36928. return -13100;
  36929. if (mp_cmp(r, exp) != MP_EQ)
  36930. return -13101;
  36931. #endif
  36932. mp_set(a, 11);
  36933. mp_zero(b);
  36934. ret = mp_gcd(a, b, r);
  36935. if (ret != MP_OKAY)
  36936. return -13102;
  36937. if (mp_cmp_d(r, 11) != MP_EQ)
  36938. return -13103;
  36939. ret = mp_gcd(b, a, r);
  36940. if (ret != MP_OKAY)
  36941. return -13104;
  36942. if (mp_cmp_d(r, 11) != MP_EQ)
  36943. return -13105;
  36944. ret = mp_gcd(b, b, r);
  36945. if (ret != MP_VAL)
  36946. return -13106;
  36947. return 0;
  36948. }
  36949. #endif
  36950. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  36951. defined(WOLFSSL_SP_MATH_ALL)
  36952. static int mp_test_mod_2d(mp_int* a, mp_int* r, mp_int* t, WC_RNG* rng)
  36953. {
  36954. int ret;
  36955. int i;
  36956. int j;
  36957. mp_set(a, 10);
  36958. ret = mp_mod_2d(a, 0, r);
  36959. if (ret != MP_OKAY)
  36960. return -13110;
  36961. if (!mp_iszero(r))
  36962. return -13111;
  36963. ret = mp_mod_2d(a, 1, r);
  36964. if (ret != MP_OKAY)
  36965. return -13112;
  36966. if (!mp_iszero(r))
  36967. return -13113;
  36968. ret = mp_mod_2d(a, 2, r);
  36969. if (ret != MP_OKAY)
  36970. return -13114;
  36971. if (mp_cmp_d(r, 2))
  36972. return -13115;
  36973. for (i = 2; i < 20; i++) {
  36974. ret = randNum(a, i, rng, NULL);
  36975. if (ret != 0)
  36976. return -13116;
  36977. for (j = 1; j <= mp_count_bits(a); j++) {
  36978. /* Get top part */
  36979. ret = mp_div_2d(a, j, t, NULL);
  36980. if (ret != 0)
  36981. return -13117;
  36982. ret = mp_mul_2d(t, j, t);
  36983. if (ret != 0)
  36984. return -13118;
  36985. /* Get bottom part */
  36986. ret = mp_mod_2d(a, j, r);
  36987. if (ret != 0)
  36988. return -13119;
  36989. /* Reassemble */
  36990. ret = mp_add(t, r, r);
  36991. if (ret != 0)
  36992. return -13120;
  36993. if (mp_cmp(a, r) != MP_EQ)
  36994. return -13121;
  36995. }
  36996. }
  36997. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_SP_INT_NEGATIVE)
  36998. /* Test negative value being moded. */
  36999. for (j = 0; j < 20; j++) {
  37000. ret = randNum(a, 2, rng, NULL);
  37001. if (ret != 0)
  37002. return -13122;
  37003. a->sign = MP_NEG;
  37004. for (i = 1; i < DIGIT_BIT * 3 + 1; i++) {
  37005. ret = mp_mod_2d(a, i, r);
  37006. if (ret != 0)
  37007. return -13124;
  37008. mp_zero(t);
  37009. ret = mp_set_bit(t, i);
  37010. if (ret != 0)
  37011. return -13125;
  37012. ret = mp_mod(a, t, t);
  37013. if (ret != 0)
  37014. return -13126;
  37015. if (mp_cmp(r, t) != MP_EQ)
  37016. return -13127;
  37017. }
  37018. }
  37019. #endif
  37020. return 0;
  37021. }
  37022. #endif
  37023. #if defined(WOLFSSL_SP_MATH_ALL) || defined(OPENSSL_EXTRA) || \
  37024. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY))
  37025. static int mp_test_mod_d(mp_int* a, WC_RNG* rng)
  37026. {
  37027. int ret;
  37028. mp_digit r;
  37029. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_SP_MATH)
  37030. mp_digit rem;
  37031. int i;
  37032. #endif
  37033. (void)rng;
  37034. if (mp_set(a, 1) != MP_OKAY)
  37035. return -13130;
  37036. ret = mp_mod_d(a, 0, &r);
  37037. if (ret != MP_VAL)
  37038. return -13131;
  37039. mp_zero(a);
  37040. ret = mp_mod_d(a, 1, &r);
  37041. if (ret != MP_OKAY)
  37042. return -13132;
  37043. ret = mp_mod_d(a, 3, &r);
  37044. if (ret != MP_OKAY)
  37045. return -13133;
  37046. ret = mp_mod_d(a, 5, &r);
  37047. if (ret != MP_OKAY)
  37048. return -13134;
  37049. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_SP_MATH)
  37050. for (i = MP_MAX_TEST_BYTE_LEN - 16; i <= MP_MAX_TEST_BYTE_LEN; i++) {
  37051. ret = randNum(a, i, rng, NULL);
  37052. if (ret != MP_OKAY)
  37053. return -13135;
  37054. ret = mp_mod_d(a, 3, &r);
  37055. if (ret != MP_OKAY)
  37056. return -13136;
  37057. ret = mp_div_d(a, 3, a, &rem);
  37058. if (ret != MP_OKAY)
  37059. return -13137;
  37060. if (r != rem)
  37061. return -13138;
  37062. }
  37063. #endif
  37064. return 0;
  37065. }
  37066. #endif
  37067. static int mp_test_mul_sqr(mp_int* a, mp_int* b, mp_int* r1, mp_int* r2,
  37068. WC_RNG* rng)
  37069. {
  37070. int ret;
  37071. int i;
  37072. for (i = 1; i < 16; i++) {
  37073. ret = randNum(a, i, rng, NULL);
  37074. if (ret != 0)
  37075. return -13140;
  37076. ret = mp_mul(a, a, r1);
  37077. if (ret != 0)
  37078. return -13141;
  37079. ret = mp_sqr(a, r2);
  37080. if (ret != 0)
  37081. return -13142;
  37082. if (mp_cmp(r1, r2) != MP_EQ)
  37083. return -13143;
  37084. }
  37085. ret = mp_set(b, 0);
  37086. if (ret != MP_OKAY)
  37087. return -13144;
  37088. ret = mp_mul(a, b, r1);
  37089. if (ret != MP_OKAY)
  37090. return -13145;
  37091. if (!mp_iszero(r1))
  37092. return -13146;
  37093. ret = mp_sqr(b, r1);
  37094. if (ret != MP_OKAY)
  37095. return -13147;
  37096. if (!mp_iszero(r1))
  37097. return -13148;
  37098. #ifdef WOLFSSL_SP_MATH_ALL
  37099. ret = mp_set(a, 1);
  37100. if (ret != MP_OKAY)
  37101. return -13149;
  37102. i = (SP_INT_DIGITS / 2) + 1;
  37103. ret = mp_mul_2d(a, i * SP_WORD_SIZE - 1, a);
  37104. if (ret != MP_OKAY)
  37105. return -13150;
  37106. ret = mp_set(b, 1);
  37107. if (ret != MP_OKAY)
  37108. return -13151;
  37109. ret = mp_mul_2d(b, (SP_INT_DIGITS - 1 - i) * SP_WORD_SIZE - 1, b);
  37110. if (ret != MP_OKAY)
  37111. return -13152;
  37112. ret = mp_mul(a, b, r1);
  37113. if (ret != MP_OKAY)
  37114. return -13153;
  37115. ret = mp_mul(a, a, r1);
  37116. if (ret == MP_OKAY)
  37117. return -13154;
  37118. ret = mp_sqr(a, r1);
  37119. if (ret == MP_OKAY)
  37120. return -13155;
  37121. ret = mp_sqr(b, r1);
  37122. if (ret != MP_OKAY)
  37123. return -13156;
  37124. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  37125. (defined(HAVE_ECC) && defined(FP_ECC))
  37126. ret = mp_mulmod(a, b, b, r1);
  37127. if (ret != MP_OKAY)
  37128. return -13157;
  37129. ret = mp_mulmod(a, a, b, r1);
  37130. if (ret == MP_OKAY)
  37131. return -13158;
  37132. #if defined(HAVE_ECC) && (defined(ECC_SHAMIR) || defined(FP_ECC))
  37133. ret = mp_sqrmod(a, b, r1);
  37134. if (ret == MP_OKAY)
  37135. return -13159;
  37136. ret = mp_sqrmod(b, a, r1);
  37137. if (ret != MP_OKAY)
  37138. return -13160;
  37139. #endif /* HAVE_ECC && (ECC_SHAMIR || FP_ECC) */
  37140. #endif /* WOLFSSL_SP_MATH_ALL || WOLFSSL_HAVE_SP_DH || (HAVE_ECC && FP_ECC) */
  37141. #endif /* WOLFSSL_SP_MATH_ALL */
  37142. return 0;
  37143. }
  37144. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  37145. defined(OPENSSL_EXTRA)
  37146. static int mp_test_invmod(mp_int* a, mp_int* m, mp_int* r)
  37147. {
  37148. int ret;
  37149. mp_set(a, 0);
  37150. mp_set(m, 1);
  37151. ret = mp_invmod(a, m, r);
  37152. if (ret != MP_VAL)
  37153. return -13170;
  37154. ret = mp_invmod(m, a, r);
  37155. if (ret != MP_VAL)
  37156. return -13171;
  37157. mp_set(a, 2);
  37158. mp_set(m, 4);
  37159. ret = mp_invmod(a, m, r);
  37160. if (ret != MP_VAL)
  37161. return -13172;
  37162. mp_set(a, 3);
  37163. mp_set(m, 6);
  37164. ret = mp_invmod(a, m, r);
  37165. if (ret != MP_VAL)
  37166. return -13181;
  37167. mp_set(a, 5*9);
  37168. mp_set(m, 6*9);
  37169. ret = mp_invmod(a, m, r);
  37170. if (ret != MP_VAL)
  37171. return -13182;
  37172. mp_set(a, 1);
  37173. mp_set(m, 4);
  37174. ret = mp_invmod(a, m, r);
  37175. if (ret != MP_OKAY)
  37176. return -13173;
  37177. if (!mp_isone(r))
  37178. return -13174;
  37179. mp_set(a, 3);
  37180. mp_set(m, 4);
  37181. ret = mp_invmod(a, m, r);
  37182. if (ret != MP_OKAY)
  37183. return -13175;
  37184. if (mp_cmp_d(r, 3))
  37185. return -13176;
  37186. mp_set(a, 3);
  37187. mp_set(m, 5);
  37188. ret = mp_invmod(a, m, r);
  37189. if (ret != MP_OKAY)
  37190. return -13177;
  37191. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  37192. /* Maximum 'a' */
  37193. mp_set(a, 0);
  37194. mp_set_bit(a, (r->size / 2)* SP_WORD_SIZE - 1);
  37195. mp_sub_d(a, 1, a);
  37196. /* Modulus too big. */
  37197. mp_set(m, 0);
  37198. mp_set_bit(m, (r->size / 2) * SP_WORD_SIZE);
  37199. ret = mp_invmod(a, m, r);
  37200. if (ret != MP_VAL)
  37201. return -13178;
  37202. /* Maximum modulus - even. */
  37203. mp_set(m, 0);
  37204. mp_set_bit(m, (r->size / 2) * SP_WORD_SIZE - 1);
  37205. ret = mp_invmod(a, m, r);
  37206. if (ret != MP_OKAY)
  37207. return -13179;
  37208. #endif
  37209. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_INT_NEGATIVE)
  37210. mp_read_radix(a, "-3", 16);
  37211. ret = mp_invmod(a, m, r);
  37212. if (ret != MP_OKAY)
  37213. return -13180;
  37214. #endif
  37215. #if defined(WOLFSSL_SP_MATH_ALL) && defined(HAVE_ECC)
  37216. mp_set(a, 0);
  37217. mp_set(m, 3);
  37218. ret = mp_invmod_mont_ct(a, m, r, 1);
  37219. if (ret != MP_VAL)
  37220. return -13190;
  37221. mp_set(a, 1);
  37222. mp_set(m, 0);
  37223. ret = mp_invmod_mont_ct(a, m, r, 1);
  37224. if (ret != MP_VAL)
  37225. return -13191;
  37226. mp_set(a, 1);
  37227. mp_set(m, 1);
  37228. ret = mp_invmod_mont_ct(a, m, r, 1);
  37229. if (ret != MP_VAL)
  37230. return -13192;
  37231. mp_set(a, 1);
  37232. mp_set(m, 2);
  37233. ret = mp_invmod_mont_ct(a, m, r, 1);
  37234. if (ret != MP_VAL)
  37235. return -13193;
  37236. mp_set(a, 1);
  37237. mp_set(m, 3);
  37238. ret = mp_invmod_mont_ct(a, m, r, 1);
  37239. if (ret != MP_OKAY)
  37240. return -13194;
  37241. #endif
  37242. return 0;
  37243. }
  37244. #endif /* !NO_RSA || HAVE_ECC || !NO_DSA || OPENSSL_EXTRA */
  37245. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  37246. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  37247. static int mp_test_exptmod(mp_int* b, mp_int* e, mp_int* m, mp_int* r)
  37248. {
  37249. int ret;
  37250. mp_set(b, 0x2);
  37251. mp_set(e, 0x3);
  37252. mp_set(m, 0x0);
  37253. ret = mp_exptmod_ex(b, e, 1, m, r);
  37254. if (ret != MP_VAL)
  37255. return -13190;
  37256. ret = mp_exptmod_nct(b, e, m, r);
  37257. if (ret != MP_VAL)
  37258. return -13191;
  37259. mp_set(b, 0x2);
  37260. mp_set(e, 0x3);
  37261. mp_set(m, 0x1);
  37262. ret = mp_exptmod_ex(b, e, 1, m, r);
  37263. if (ret != MP_OKAY)
  37264. return -13192;
  37265. if (!mp_iszero(r))
  37266. return -13193;
  37267. ret = mp_exptmod_nct(b, e, m, r);
  37268. if (ret != MP_OKAY)
  37269. return -13194;
  37270. if (!mp_iszero(r))
  37271. return -13195;
  37272. mp_set(b, 0x2);
  37273. mp_set(e, 0x0);
  37274. mp_set(m, 0x7);
  37275. ret = mp_exptmod_ex(b, e, 1, m, r);
  37276. if (ret != MP_OKAY)
  37277. return -13196;
  37278. if (!mp_isone(r))
  37279. return -13197;
  37280. ret = mp_exptmod_nct(b, e, m, r);
  37281. if (ret != MP_OKAY)
  37282. return -13198;
  37283. if (!mp_isone(r))
  37284. return -13199;
  37285. mp_set(b, 0x0);
  37286. mp_set(e, 0x3);
  37287. mp_set(m, 0x7);
  37288. ret = mp_exptmod_ex(b, e, 1, m, r);
  37289. if (ret != MP_OKAY)
  37290. return -13200;
  37291. if (!mp_iszero(r))
  37292. return -13201;
  37293. ret = mp_exptmod_nct(b, e, m, r);
  37294. if (ret != MP_OKAY)
  37295. return -13202;
  37296. if (!mp_iszero(r))
  37297. return -13203;
  37298. mp_set(b, 0x10);
  37299. mp_set(e, 0x3);
  37300. mp_set(m, 0x7);
  37301. ret = mp_exptmod_ex(b, e, 1, m, r);
  37302. if (ret != MP_OKAY)
  37303. return -13204;
  37304. ret = mp_exptmod_nct(b, e, m, r);
  37305. if (ret != MP_OKAY)
  37306. return -13205;
  37307. mp_set(b, 0x7);
  37308. mp_set(e, 0x3);
  37309. mp_set(m, 0x7);
  37310. ret = mp_exptmod_ex(b, e, 1, m, r);
  37311. if (ret != MP_OKAY)
  37312. return -13206;
  37313. if (!mp_iszero(r))
  37314. return -13207;
  37315. ret = mp_exptmod_nct(b, e, m, r);
  37316. if (ret != MP_OKAY)
  37317. return -13208;
  37318. if (!mp_iszero(r))
  37319. return -13209;
  37320. #ifndef WOLFSSL_SP_MATH
  37321. mp_set(b, 0x01);
  37322. mp_mul_2d(b, DIGIT_BIT, b);
  37323. mp_add_d(b, 1, b);
  37324. mp_set(e, 0x3);
  37325. mp_copy(b, m);
  37326. ret = mp_exptmod_ex(b, e, 1, m, r);
  37327. if (ret != MP_OKAY)
  37328. return -13210;
  37329. if (!mp_iszero(r))
  37330. return -13211;
  37331. ret = mp_exptmod_nct(b, e, m, r);
  37332. if (ret != MP_OKAY)
  37333. return -13212;
  37334. if (!mp_iszero(r))
  37335. return -13213;
  37336. #endif
  37337. mp_set(b, 0x2);
  37338. mp_set(e, 0x3);
  37339. mp_set(m, 0x7);
  37340. ret = mp_exptmod_ex(b, e, 1, m, r);
  37341. if (ret != MP_OKAY)
  37342. return -13214;
  37343. ret = mp_exptmod_nct(b, e, m, r);
  37344. if (ret != MP_OKAY)
  37345. return -13215;
  37346. #ifdef WOLFSSL_SP_MATH_ALL
  37347. mp_set(b, 0x2);
  37348. mp_set(e, 0x3);
  37349. mp_set(m, 0x01);
  37350. mp_mul_2d(m, SP_WORD_SIZE * SP_INT_DIGITS / 2, m);
  37351. mp_add_d(m, 0x01, m);
  37352. ret = mp_exptmod_ex(b, e, 1, m, r);
  37353. if (ret != MP_VAL)
  37354. return -13216;
  37355. ret = mp_exptmod_nct(b, e, m, r);
  37356. if (ret != MP_VAL)
  37357. return -13217;
  37358. #endif
  37359. return 0;
  37360. }
  37361. #endif /* !NO_RSA || !NO_DSA || !NO_DH || (HAVE_ECC && HAVE_COMP_KEY) ||
  37362. * OPENSSL_EXTRA */
  37363. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  37364. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  37365. static int mp_test_mont(mp_int* a, mp_int* m, mp_int* n, mp_int* r, WC_RNG* rng)
  37366. {
  37367. int ret;
  37368. mp_digit mp;
  37369. static int exp[] = { 7, 8, 16, 27, 32, 64,
  37370. 127, 128, 255, 256,
  37371. #if defined(SP_WORD_SIZE) && SP_WORD_SIZE > 8
  37372. 383, 384, 2033, 2048
  37373. #endif
  37374. };
  37375. static mp_digit sub[] = { 0x01, 0x05, 0x0f, 0x27, 0x05, 0x3b,
  37376. 0x01, 0x9f, 0x13, 0xbd,
  37377. #if defined(SP_WORD_SIZE) && SP_WORD_SIZE > 8
  37378. 0x1f, 0x13d, 0x45, 0x615
  37379. #endif
  37380. };
  37381. int bits[] = { 256, 384,
  37382. #if defined(SP_INT_MAX_BITS) && SP_INT_MAX_BITS > 4096
  37383. 2048,
  37384. #endif
  37385. #if defined(SP_INT_MAX_BITS) && SP_INT_MAX_BITS > 6144
  37386. 3072
  37387. #endif
  37388. };
  37389. int i;
  37390. int j;
  37391. for (i = 0; i < (int)(sizeof(exp) / sizeof(*exp)); i++) {
  37392. if (exp[i] >= DIGIT_BIT)
  37393. continue;
  37394. mp_zero(m);
  37395. ret = mp_set_bit(m, exp[i]);
  37396. if (ret != MP_OKAY)
  37397. return -13220;
  37398. ret = mp_sub_d(m, sub[i], m);
  37399. if (ret != MP_OKAY)
  37400. return -13221;
  37401. ret = mp_montgomery_setup(m, &mp);
  37402. if (ret != MP_OKAY)
  37403. return -13222;
  37404. ret = mp_montgomery_calc_normalization(n, m);
  37405. if (ret != MP_OKAY)
  37406. return -13223;
  37407. for (j = 0; j < 10; j++) {
  37408. ret = randNum(a, (exp[i] + DIGIT_BIT - 1) / DIGIT_BIT, rng, NULL);
  37409. if (ret != 0)
  37410. return -13224;
  37411. ret = mp_mod(a, m, a);
  37412. if (ret != 0)
  37413. return -13225;
  37414. /* r = a * a */
  37415. ret = mp_sqrmod(a, m, r);
  37416. if (ret != MP_OKAY)
  37417. return -13226;
  37418. /* Convert to Montgomery form = a*n */
  37419. ret = mp_mulmod(a, n, m, a);
  37420. if (ret != MP_OKAY)
  37421. return -13227;
  37422. /* a*a mod m == ((a*n) * (a*n)) / n / n */
  37423. ret = mp_sqr(a, a);
  37424. if (ret != MP_OKAY)
  37425. return -13228;
  37426. ret = mp_montgomery_reduce(a, m, mp);
  37427. if (ret != MP_OKAY)
  37428. return -13229;
  37429. ret = mp_montgomery_reduce(a, m, mp);
  37430. if (ret != MP_OKAY)
  37431. return -13230;
  37432. if (mp_cmp(a, r) != MP_EQ)
  37433. return -13231;
  37434. }
  37435. }
  37436. /* Force carries. */
  37437. for (i = 0; i < (int)(sizeof(bits) / sizeof(*bits)); i++) {
  37438. /* a = 2^(bits*2) - 1 */
  37439. mp_zero(a);
  37440. mp_set_bit(a, bits[i] * 2);
  37441. mp_sub_d(a, 1, a);
  37442. /* m = 2^(bits) - 1 */
  37443. mp_zero(m);
  37444. mp_set_bit(m, bits[i]);
  37445. mp_sub_d(m, 1, m);
  37446. mp = 1;
  37447. /* result = r = 2^(bits) - 1 */
  37448. mp_zero(r);
  37449. mp_set_bit(r, bits[i]);
  37450. mp_sub_d(r, 1, r);
  37451. ret = mp_montgomery_reduce(a, m, mp);
  37452. if (ret != MP_OKAY)
  37453. return -13240;
  37454. /* Result is m or 0 if reduced to range of modulus. */
  37455. if (mp_cmp(a, r) != MP_EQ && mp_iszero(a) != MP_YES)
  37456. return -13241;
  37457. }
  37458. return 0;
  37459. }
  37460. #endif
  37461. WOLFSSL_TEST_SUBROUTINE int mp_test(void)
  37462. {
  37463. WC_RNG rng;
  37464. int ret;
  37465. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  37466. int i, j;
  37467. #ifndef WOLFSSL_SP_MATH
  37468. int k;
  37469. #endif
  37470. mp_digit d;
  37471. #endif
  37472. mp_int a, b, r1, r2, p;
  37473. ret = mp_init_multi(&a, &b, &r1, &r2, NULL, NULL);
  37474. if (ret != 0)
  37475. return -13300;
  37476. #ifdef WOLFSSL_SP_MATH_ALL
  37477. mp_init_copy(&p, &a);
  37478. #else
  37479. ret = mp_init(&p);
  37480. if (ret != 0)
  37481. return -13301;
  37482. #endif
  37483. #ifndef HAVE_FIPS
  37484. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  37485. #else
  37486. ret = wc_InitRng(&rng);
  37487. #endif
  37488. if (ret != 0)
  37489. goto done;
  37490. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  37491. mp_set_int(&a, 0);
  37492. if (a.used != 0 || a.dp[0] != 0)
  37493. return -13302;
  37494. for (j = 1; j <= MP_MAX_TEST_BYTE_LEN; j++) {
  37495. for (i = 0; i < 4 * j; i++) {
  37496. /* New values to use. */
  37497. ret = randNum(&p, j, &rng, NULL);
  37498. if (ret != 0)
  37499. return -13303;
  37500. ret = randNum(&a, j, &rng, NULL);
  37501. if (ret != 0)
  37502. return -13304;
  37503. ret = randNum(&b, j, &rng, NULL);
  37504. if (ret != 0)
  37505. return -13305;
  37506. ret = wc_RNG_GenerateBlock(&rng, (byte*)&d, sizeof(d));
  37507. if (ret != 0)
  37508. return -13306;
  37509. d &= MP_MASK;
  37510. #if !defined(WOLFSSL_SP_MATH) || (defined(HAVE_ECC) && \
  37511. (defined(ECC_SHAMIR) || defined(FP_ECC)))
  37512. /* Ensure sqrmod produce same result as mulmod. */
  37513. ret = mp_sqrmod(&a, &p, &r1);
  37514. if (ret != 0)
  37515. return -13307;
  37516. ret = mp_mulmod(&a, &a, &p, &r2);
  37517. if (ret != 0)
  37518. return -13308;
  37519. if (mp_cmp(&r1, &r2) != 0)
  37520. return -13309;
  37521. #endif
  37522. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37523. #if defined(WOLFSSL_SP_MATH) || (defined(WOLFSSL_SP_MATH_ALL) && \
  37524. !defined(WOLFSSL_SP_INT_NEGATIVE))
  37525. ret = mp_addmod(&a, &b, &p, &r1);
  37526. if (ret != 0)
  37527. return -13310;
  37528. ret = mp_submod(&r1, &b, &p, &r2);
  37529. if (ret != 0)
  37530. return -13311;
  37531. ret = mp_mod(&a, &p, &r1);
  37532. if (ret != 0)
  37533. return -13312;
  37534. if (mp_cmp(&r1, &r2) != MP_EQ)
  37535. return -13313;
  37536. #else
  37537. /* Ensure add with mod produce same result as sub with mod. */
  37538. ret = mp_addmod(&a, &b, &p, &r1);
  37539. if (ret != 0)
  37540. return -13314;
  37541. b.sign ^= 1;
  37542. ret = mp_submod(&a, &b, &p, &r2);
  37543. if (ret != 0)
  37544. return -13315;
  37545. if (mp_cmp(&r1, &r2) != 0)
  37546. return -13316;
  37547. #endif
  37548. #endif
  37549. /* Ensure add digit produce same result as sub digit. */
  37550. ret = mp_add_d(&a, d, &r1);
  37551. if (ret != 0)
  37552. return -13317;
  37553. ret = mp_sub_d(&r1, d, &r2);
  37554. if (ret != 0)
  37555. return -13318;
  37556. if (mp_cmp(&a, &r2) != 0)
  37557. return -13319;
  37558. /* Invert - if p is even it will use the slow impl.
  37559. * - if p and a are even it will fail.
  37560. */
  37561. ret = mp_invmod(&a, &p, &r1);
  37562. if (ret != 0 && ret != MP_VAL)
  37563. return -13320;
  37564. ret = 0;
  37565. #ifndef WOLFSSL_SP_MATH
  37566. /* Shift up and down number all bits in a digit. */
  37567. for (k = 0; k < DIGIT_BIT; k++) {
  37568. mp_mul_2d(&a, k, &r1);
  37569. mp_div_2d(&r1, k, &r2, &p);
  37570. if (mp_cmp(&a, &r2) != 0)
  37571. return -13321;
  37572. if (!mp_iszero(&p))
  37573. return -13322;
  37574. mp_rshb(&r1, k);
  37575. if (mp_cmp(&a, &r1) != 0)
  37576. return -13323;
  37577. }
  37578. #endif
  37579. }
  37580. }
  37581. /* Test adding and subtracting zero from zero. */
  37582. mp_zero(&a);
  37583. ret = mp_add_d(&a, 0, &r1);
  37584. if (ret != 0)
  37585. return -13329;
  37586. if (!mp_iszero(&r1)) {
  37587. return -13330;
  37588. }
  37589. ret = mp_sub_d(&a, 0, &r2);
  37590. if (ret != 0)
  37591. return -13331;
  37592. if (!mp_iszero(&r2)) {
  37593. return -13332;
  37594. }
  37595. #if DIGIT_BIT >= 32
  37596. /* Check that setting a 32-bit digit works. */
  37597. d &= 0xffffffffU;
  37598. mp_set_int(&a, d);
  37599. if (a.used != 1 || a.dp[0] != d)
  37600. return -13324;
  37601. #endif
  37602. /* Check setting a bit and testing a bit works. */
  37603. for (i = 0; i < MP_MAX_TEST_BYTE_LEN * 8; i++) {
  37604. mp_zero(&a);
  37605. mp_set_bit(&a, i);
  37606. if (!mp_is_bit_set(&a, i))
  37607. return -13325;
  37608. }
  37609. #endif
  37610. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  37611. mp_zero(&a);
  37612. i = mp_cnt_lsb(&a);
  37613. if (i != 0)
  37614. return -13326;
  37615. mp_set(&a, 1);
  37616. i = mp_cnt_lsb(&a);
  37617. if (i != 0)
  37618. return -13327;
  37619. mp_set(&a, 32);
  37620. i = mp_cnt_lsb(&a);
  37621. if (i != 5)
  37622. return -13328;
  37623. mp_zero(&a);
  37624. mp_set_bit(&a, 129);
  37625. i = mp_cnt_lsb(&a);
  37626. if (i != 129)
  37627. return -13328;
  37628. #endif
  37629. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  37630. if ((ret = mp_test_param(&a, &b, &r1, &rng)) != 0)
  37631. return ret;
  37632. #endif
  37633. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  37634. if ((ret = mp_test_div_3(&a, &r1, &rng)) != 0)
  37635. return ret;
  37636. #endif
  37637. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  37638. !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  37639. (!defined WOLFSSL_SP_MATH && !defined(WOLFSSL_SP_MATH_ALL) && \
  37640. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  37641. if ((ret = mp_test_radix_10(&a, &r1, &rng)) != 0)
  37642. return ret;
  37643. #endif
  37644. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  37645. defined(HAVE_ECC))
  37646. if ((ret = mp_test_radix_16(&a, &r1, &rng)) != 0)
  37647. return ret;
  37648. #endif
  37649. if ((ret = mp_test_shift(&a, &r1, &rng)) != 0)
  37650. return ret;
  37651. if ((ret = mp_test_add_sub_d(&a, &r1)) != 0)
  37652. return ret;
  37653. if ((ret = mp_test_read_to_bin(&a)) != 0)
  37654. return ret;
  37655. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  37656. if ((ret = mp_test_set_int(&a)) != 0)
  37657. return ret;
  37658. #endif
  37659. if ((ret = mp_test_cmp(&a, &r1)) != 0)
  37660. return ret;
  37661. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37662. if ((ret = mp_test_shbd(&a, &b, &rng)) != 0)
  37663. return ret;
  37664. #endif
  37665. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  37666. if ((ret = mp_test_set_is_bit(&a)) != 0)
  37667. return ret;
  37668. #endif
  37669. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  37670. if ((ret = mp_test_div(&a, &b, &r1, &r2, &rng)) != 0)
  37671. return ret;
  37672. #endif
  37673. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  37674. !defined(WC_NO_RNG)
  37675. if ((ret = mp_test_prime(&a, &rng)) != 0)
  37676. return ret;
  37677. #endif
  37678. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  37679. if ((ret = mp_test_lcm_gcd(&a, &b, &r1, &r2, &rng)) != 0)
  37680. return ret;
  37681. #endif
  37682. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  37683. defined(WOLFSSL_SP_MATH_ALL)
  37684. if ((ret = mp_test_mod_2d(&a, &r1, &p, &rng)) != 0)
  37685. return ret;
  37686. #endif
  37687. #if defined(WOLFSSL_SP_MATH_ALL) || defined(OPENSSL_EXTRA) || \
  37688. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY))
  37689. if ((ret = mp_test_mod_d(&a, &rng)) != 0)
  37690. return ret;
  37691. #endif
  37692. if ((ret = mp_test_mul_sqr(&a, &b, &r1, &r2, &rng)) != 0)
  37693. return ret;
  37694. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  37695. defined(OPENSSL_EXTRA)
  37696. if ((ret = mp_test_invmod(&a, &b, &r1)) != 0)
  37697. return ret;
  37698. #endif
  37699. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  37700. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  37701. if ((ret = mp_test_exptmod(&a, &b, &r1, &r2)) != 0)
  37702. return ret;
  37703. #endif
  37704. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  37705. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  37706. if ((ret = mp_test_mont(&a, &b, &r1, &r2, &rng)) != 0)
  37707. return ret;
  37708. #endif
  37709. done:
  37710. mp_clear(&p);
  37711. mp_clear(&r2);
  37712. mp_clear(&r1);
  37713. mp_clear(&b);
  37714. mp_clear(&a);
  37715. wc_FreeRng(&rng);
  37716. return ret;
  37717. }
  37718. #endif
  37719. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  37720. typedef struct pairs_t {
  37721. const unsigned char* coeff;
  37722. int coeffSz;
  37723. int exp;
  37724. } pairs_t;
  37725. /*
  37726. n =p1p2p3, where pi = ki(p1-1)+1 with (k2,k3) = (173,293)
  37727. p1 = 2^192 * 0x000000000000e24fd4f6d6363200bf2323ec46285cac1d3a
  37728. + 2^0 * 0x0b2488b0c29d96c5e67f8bec15b54b189ae5636efe89b45b
  37729. */
  37730. static const unsigned char c192a[] =
  37731. {
  37732. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe2, 0x4f,
  37733. 0xd4, 0xf6, 0xd6, 0x36, 0x32, 0x00, 0xbf, 0x23,
  37734. 0x23, 0xec, 0x46, 0x28, 0x5c, 0xac, 0x1d, 0x3a
  37735. };
  37736. static const unsigned char c0a[] =
  37737. {
  37738. 0x0b, 0x24, 0x88, 0xb0, 0xc2, 0x9d, 0x96, 0xc5,
  37739. 0xe6, 0x7f, 0x8b, 0xec, 0x15, 0xb5, 0x4b, 0x18,
  37740. 0x9a, 0xe5, 0x63, 0x6e, 0xfe, 0x89, 0xb4, 0x5b
  37741. };
  37742. static const pairs_t ecPairsA[] =
  37743. {
  37744. {c192a, sizeof(c192a), 192},
  37745. {c0a, sizeof(c0a), 0}
  37746. };
  37747. static const int kA[] = {173, 293};
  37748. static const unsigned char controlPrime[] = {
  37749. 0xe1, 0x76, 0x45, 0x80, 0x59, 0xb6, 0xd3, 0x49,
  37750. 0xdf, 0x0a, 0xef, 0x12, 0xd6, 0x0f, 0xf0, 0xb7,
  37751. 0xcb, 0x2a, 0x37, 0xbf, 0xa7, 0xf8, 0xb5, 0x4d,
  37752. 0xf5, 0x31, 0x35, 0xad, 0xe4, 0xa3, 0x94, 0xa1,
  37753. 0xdb, 0xf1, 0x96, 0xad, 0xb5, 0x05, 0x64, 0x85,
  37754. 0x83, 0xfc, 0x1b, 0x5b, 0x29, 0xaa, 0xbe, 0xf8,
  37755. 0x26, 0x3f, 0x76, 0x7e, 0xad, 0x1c, 0xf0, 0xcb,
  37756. 0xd7, 0x26, 0xb4, 0x1b, 0x05, 0x8e, 0x56, 0x86,
  37757. 0x7e, 0x08, 0x62, 0x21, 0xc1, 0x86, 0xd6, 0x47,
  37758. 0x79, 0x3e, 0xb7, 0x5d, 0xa4, 0xc6, 0x3a, 0xd7,
  37759. 0xb1, 0x74, 0x20, 0xf6, 0x50, 0x97, 0x41, 0x04,
  37760. 0x53, 0xed, 0x3f, 0x26, 0xd6, 0x6f, 0x91, 0xfa,
  37761. 0x68, 0x26, 0xec, 0x2a, 0xdc, 0x9a, 0xf1, 0xe7,
  37762. 0xdc, 0xfb, 0x73, 0xf0, 0x79, 0x43, 0x1b, 0x21,
  37763. 0xa3, 0x59, 0x04, 0x63, 0x52, 0x07, 0xc9, 0xd7,
  37764. 0xe6, 0xd1, 0x1b, 0x5d, 0x5e, 0x96, 0xfa, 0x53
  37765. };
  37766. static const unsigned char testOne[] = { 1 };
  37767. static int GenerateNextP(mp_int* p1, mp_int* p2, int k)
  37768. {
  37769. int ret;
  37770. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  37771. mp_int *ki = (mp_int *)XMALLOC(sizeof(*ki), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37772. if (ki == NULL)
  37773. return MEMORY_E;
  37774. #else
  37775. mp_int ki[1];
  37776. #endif
  37777. ret = mp_init(ki);
  37778. if (ret == 0)
  37779. ret = mp_set(ki, k);
  37780. if (ret == 0)
  37781. ret = mp_sub_d(p1, 1, p2);
  37782. if (ret == 0)
  37783. ret = mp_mul(p2, ki, p2);
  37784. if (ret == 0)
  37785. ret = mp_add_d(p2, 1, p2);
  37786. mp_clear(ki);
  37787. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  37788. XFREE(ki, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37789. #endif
  37790. return ret;
  37791. }
  37792. static int GenerateP(mp_int* p1, mp_int* p2, mp_int* p3,
  37793. const pairs_t* ecPairs, int ecPairsSz,
  37794. const int* k)
  37795. {
  37796. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  37797. mp_int *x = NULL, *y = NULL;
  37798. #else
  37799. mp_int x[1], y[1];
  37800. #endif
  37801. int ret, i;
  37802. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  37803. if (((x = (mp_int *)XMALLOC(sizeof(*x), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL) ||
  37804. ((y = (mp_int *)XMALLOC(sizeof(*x), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)) {
  37805. ret = MEMORY_E;
  37806. goto out;
  37807. }
  37808. #endif
  37809. ret = mp_init_multi(x, y, NULL, NULL, NULL, NULL);
  37810. if (ret != 0) {
  37811. ret = MP_MEM;
  37812. goto out;
  37813. }
  37814. for (i = 0; ret == 0 && i < ecPairsSz; i++) {
  37815. ret = mp_read_unsigned_bin(x, ecPairs[i].coeff, ecPairs[i].coeffSz);
  37816. /* p1 = 2^exp */
  37817. if (ret == 0)
  37818. ret = mp_2expt(y, ecPairs[i].exp);
  37819. /* p1 = p1 * m */
  37820. if (ret == 0)
  37821. ret = mp_mul(x, y, x);
  37822. /* p1 += */
  37823. if (ret == 0)
  37824. ret = mp_add(p1, x, p1);
  37825. mp_zero(x);
  37826. mp_zero(y);
  37827. }
  37828. if (ret == 0)
  37829. ret = GenerateNextP(p1, p2, k[0]);
  37830. if (ret == 0)
  37831. ret = GenerateNextP(p1, p3, k[1]);
  37832. out:
  37833. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  37834. if (x != NULL) {
  37835. mp_clear(x);
  37836. XFREE(x, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37837. }
  37838. if (y != NULL) {
  37839. mp_clear(y);
  37840. XFREE(y, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37841. }
  37842. #else
  37843. mp_clear(x);
  37844. mp_clear(y);
  37845. #endif
  37846. return ret;
  37847. }
  37848. WOLFSSL_TEST_SUBROUTINE int prime_test(void)
  37849. {
  37850. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  37851. mp_int *n = (mp_int *)XMALLOC(sizeof *n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  37852. *p1 = (mp_int *)XMALLOC(sizeof *p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  37853. *p2 = (mp_int *)XMALLOC(sizeof *p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  37854. *p3 = (mp_int *)XMALLOC(sizeof *p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37855. #else
  37856. mp_int n[1],
  37857. p1[1],
  37858. p2[1],
  37859. p3[1];
  37860. #endif
  37861. int ret, isPrime = 0;
  37862. WC_RNG rng;
  37863. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  37864. if ((n == NULL) ||
  37865. (p1 == NULL) ||
  37866. (p2 == NULL) ||
  37867. (p3 == NULL))
  37868. ERROR_OUT(MEMORY_E, out);
  37869. #endif
  37870. ret = wc_InitRng(&rng);
  37871. if (ret == 0)
  37872. ret = mp_init_multi(n, p1, p2, p3, NULL, NULL);
  37873. if (ret == 0)
  37874. ret = GenerateP(p1, p2, p3,
  37875. ecPairsA, sizeof(ecPairsA) / sizeof(ecPairsA[0]), kA);
  37876. if (ret == 0)
  37877. ret = mp_mul(p1, p2, n);
  37878. if (ret == 0)
  37879. ret = mp_mul(n, p3, n);
  37880. if (ret != 0)
  37881. ERROR_OUT(-13400, out);
  37882. /* Check the old prime test using the number that false positives.
  37883. * This test result should indicate as not prime. */
  37884. ret = mp_prime_is_prime(n, 40, &isPrime);
  37885. if (ret != 0)
  37886. ERROR_OUT(-13401, out);
  37887. if (isPrime)
  37888. ERROR_OUT(-13402, out);
  37889. /* This test result should fail. It should indicate the value as prime. */
  37890. ret = mp_prime_is_prime(n, 8, &isPrime);
  37891. if (ret != 0)
  37892. ERROR_OUT(-13403, out);
  37893. if (!isPrime)
  37894. ERROR_OUT(-13404, out);
  37895. /* This test result should indicate the value as not prime. */
  37896. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  37897. if (ret != 0)
  37898. ERROR_OUT(-13405, out);
  37899. if (isPrime)
  37900. ERROR_OUT(-13406, out);
  37901. ret = mp_read_unsigned_bin(n, controlPrime, sizeof(controlPrime));
  37902. if (ret != 0)
  37903. ERROR_OUT(-13407, out);
  37904. /* This test result should indicate the value as prime. */
  37905. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  37906. if (ret != 0)
  37907. ERROR_OUT(-13408, out);
  37908. if (!isPrime)
  37909. ERROR_OUT(-13409, out);
  37910. /* This test result should indicate the value as prime. */
  37911. isPrime = -1;
  37912. ret = mp_prime_is_prime(n, 8, &isPrime);
  37913. if (ret != 0)
  37914. ERROR_OUT(-13410, out);
  37915. if (!isPrime)
  37916. ERROR_OUT(-13411, out);
  37917. ret = mp_read_unsigned_bin(n, testOne, sizeof(testOne));
  37918. if (ret != 0)
  37919. ERROR_OUT(-13412, out);
  37920. /* This test result should indicate the value as not prime. */
  37921. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  37922. if (ret != 0)
  37923. ERROR_OUT(-13413, out);
  37924. if (isPrime)
  37925. ERROR_OUT(-13414, out);
  37926. ret = mp_prime_is_prime(n, 8, &isPrime);
  37927. if (ret != 0)
  37928. ERROR_OUT(-13415, out);
  37929. if (isPrime)
  37930. ERROR_OUT(-13416, out);
  37931. ret = 0;
  37932. out:
  37933. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  37934. if (n != NULL) {
  37935. mp_clear(n);
  37936. XFREE(n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37937. }
  37938. if (p1 != NULL) {
  37939. mp_clear(p1);
  37940. XFREE(p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37941. }
  37942. if (p2 != NULL) {
  37943. mp_clear(p2);
  37944. XFREE(p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37945. }
  37946. if (p3 != NULL) {
  37947. mp_clear(p3);
  37948. XFREE(p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  37949. }
  37950. #else
  37951. mp_clear(p3);
  37952. mp_clear(p2);
  37953. mp_clear(p1);
  37954. mp_clear(n);
  37955. #endif
  37956. wc_FreeRng(&rng);
  37957. return ret;
  37958. }
  37959. #endif /* WOLFSSL_PUBLIC_MP */
  37960. #if defined(ASN_BER_TO_DER) && \
  37961. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  37962. defined(OPENSSL_EXTRA_X509_SMALL))
  37963. /* wc_BerToDer is only public facing in the case of test cert or opensslextra */
  37964. typedef struct berDerTestData {
  37965. const byte *in;
  37966. word32 inSz;
  37967. const byte *out;
  37968. word32 outSz;
  37969. } berDerTestData;
  37970. WOLFSSL_TEST_SUBROUTINE int berder_test(void)
  37971. {
  37972. int ret;
  37973. int i;
  37974. word32 len = 0, l;
  37975. byte out[32];
  37976. WOLFSSL_SMALL_STACK_STATIC const byte good1_in[] = { 0x30, 0x80, 0x00, 0x00 };
  37977. WOLFSSL_SMALL_STACK_STATIC const byte good1_out[] = { 0x30, 0x00 };
  37978. WOLFSSL_SMALL_STACK_STATIC const byte good2_in[] = { 0x30, 0x80, 0x02, 0x01, 0x01, 0x00, 0x00 };
  37979. WOLFSSL_SMALL_STACK_STATIC const byte good2_out[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  37980. WOLFSSL_SMALL_STACK_STATIC const byte good3_in[] = {
  37981. 0x24, 0x80, 0x04, 0x01, 0x01, 0x00, 0x00
  37982. };
  37983. WOLFSSL_SMALL_STACK_STATIC const byte good3_out[] = { 0x04, 0x1, 0x01 };
  37984. WOLFSSL_SMALL_STACK_STATIC const byte good4_in[] = {
  37985. 0x30, 0x80,
  37986. 0x02, 0x01, 0x01,
  37987. 0x30, 0x80,
  37988. 0x24, 0x80,
  37989. 0x04, 0x01, 0x01,
  37990. 0x04, 0x02, 0x02, 0x03,
  37991. 0x00, 0x00,
  37992. 0x06, 0x01, 0x01,
  37993. 0x00, 0x00,
  37994. 0x31, 0x80,
  37995. 0x06, 0x01, 0x01,
  37996. 0x00, 0x00,
  37997. 0x00, 0x00,
  37998. };
  37999. WOLFSSL_SMALL_STACK_STATIC const byte good4_out[] = {
  38000. 0x30, 0x12,
  38001. 0x02, 0x01, 0x01,
  38002. 0x30, 0x08,
  38003. 0x04, 0x03, 0x01, 0x02, 0x03,
  38004. 0x06, 0x01, 0x01,
  38005. 0x31, 0x03,
  38006. 0x06, 0x01, 0x01
  38007. };
  38008. WOLFSSL_SMALL_STACK_STATIC const byte good5_in[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  38009. berDerTestData testData[] = {
  38010. { good1_in, sizeof(good1_in), good1_out, sizeof(good1_out) },
  38011. { good2_in, sizeof(good2_in), good2_out, sizeof(good2_out) },
  38012. { good3_in, sizeof(good3_in), good3_out, sizeof(good3_out) },
  38013. { good4_in, sizeof(good4_in), good4_out, sizeof(good4_out) },
  38014. { good5_in, sizeof(good5_in), good5_in , sizeof(good5_in ) },
  38015. };
  38016. for (i = 0; i < (int)(sizeof(testData) / sizeof(*testData)); i++) {
  38017. ret = wc_BerToDer(testData[i].in, testData[i].inSz, NULL, &len);
  38018. if (ret != LENGTH_ONLY_E)
  38019. return -13500 - i;
  38020. if (len != testData[i].outSz)
  38021. return -13510 - i;
  38022. len = testData[i].outSz;
  38023. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &len);
  38024. if (ret != 0)
  38025. return -13520 - i;
  38026. if (XMEMCMP(out, testData[i].out, len) != 0)
  38027. return -13530 - i;
  38028. for (l = 1; l < testData[i].inSz; l++) {
  38029. ret = wc_BerToDer(testData[i].in, l, NULL, &len);
  38030. if (ret != ASN_PARSE_E)
  38031. return -13540;
  38032. len = testData[i].outSz;
  38033. ret = wc_BerToDer(testData[i].in, l, out, &len);
  38034. if (ret != ASN_PARSE_E)
  38035. return -13541;
  38036. }
  38037. for (l = 0; l < testData[i].outSz-1; l++) {
  38038. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &l);
  38039. if (ret != BUFFER_E)
  38040. return -13542;
  38041. }
  38042. }
  38043. ret = wc_BerToDer(NULL, 4, NULL, NULL);
  38044. if (ret != BAD_FUNC_ARG)
  38045. return -13543;
  38046. ret = wc_BerToDer(out, 4, NULL, NULL);
  38047. if (ret != BAD_FUNC_ARG)
  38048. return -13544;
  38049. ret = wc_BerToDer(NULL, 4, NULL, &len);
  38050. if (ret != BAD_FUNC_ARG)
  38051. return -13545;
  38052. ret = wc_BerToDer(NULL, 4, out, NULL);
  38053. if (ret != BAD_FUNC_ARG)
  38054. return -13546;
  38055. ret = wc_BerToDer(out, 4, out, NULL);
  38056. if (ret != BAD_FUNC_ARG)
  38057. return -13547;
  38058. ret = wc_BerToDer(NULL, 4, out, &len);
  38059. if (ret != BAD_FUNC_ARG)
  38060. return -13548;
  38061. for (l = 1; l < sizeof(good4_out); l++) {
  38062. len = l;
  38063. ret = wc_BerToDer(good4_in, sizeof(good4_in), out, &len);
  38064. if (ret != BUFFER_E)
  38065. return -13549;
  38066. }
  38067. return 0;
  38068. }
  38069. #endif
  38070. #ifdef DEBUG_WOLFSSL
  38071. static THREAD_LS_T int log_cnt = 0;
  38072. static void my_Logging_cb(const int logLevel, const char *const logMessage)
  38073. {
  38074. (void)logLevel;
  38075. (void)logMessage;
  38076. log_cnt++;
  38077. }
  38078. #endif /* DEBUG_WOLFSSL */
  38079. WOLFSSL_TEST_SUBROUTINE int logging_test(void)
  38080. {
  38081. #ifdef DEBUG_WOLFSSL
  38082. const char* msg = "Testing, testing. 1, 2, 3, 4 ...";
  38083. byte a[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  38084. byte b[256];
  38085. int i;
  38086. for (i = 0; i < (int)sizeof(b); i++)
  38087. b[i] = i;
  38088. if (wolfSSL_Debugging_ON() != 0)
  38089. return -13600;
  38090. if (wolfSSL_SetLoggingCb(my_Logging_cb) != 0)
  38091. return -13601;
  38092. WOLFSSL_MSG(msg);
  38093. WOLFSSL_BUFFER(a, sizeof(a));
  38094. WOLFSSL_BUFFER(b, sizeof(b));
  38095. WOLFSSL_BUFFER(NULL, 0);
  38096. WOLFSSL_ERROR(MEMORY_E);
  38097. WOLFSSL_ERROR_MSG(msg);
  38098. /* turn off logs */
  38099. wolfSSL_Debugging_OFF();
  38100. /* capture log count */
  38101. i = log_cnt;
  38102. /* validate no logs are output when disabled */
  38103. WOLFSSL_MSG(msg);
  38104. WOLFSSL_BUFFER(a, sizeof(a));
  38105. WOLFSSL_BUFFER(b, sizeof(b));
  38106. WOLFSSL_BUFFER(NULL, 0);
  38107. WOLFSSL_ERROR(MEMORY_E);
  38108. WOLFSSL_ERROR_MSG(msg);
  38109. /* check the logs were disabled */
  38110. if (i != log_cnt)
  38111. return -13602;
  38112. /* restore callback and leave logging enabled */
  38113. wolfSSL_SetLoggingCb(NULL);
  38114. wolfSSL_Debugging_ON();
  38115. /* suppress unused args */
  38116. (void)a;
  38117. (void)b;
  38118. #else
  38119. if (wolfSSL_Debugging_ON() != NOT_COMPILED_IN)
  38120. return -13603;
  38121. wolfSSL_Debugging_OFF();
  38122. if (wolfSSL_SetLoggingCb(NULL) != NOT_COMPILED_IN)
  38123. return -13604;
  38124. #endif /* DEBUG_WOLFSSL */
  38125. return 0;
  38126. }
  38127. #if defined(__INCLUDE_NUTTX_CONFIG_H)
  38128. WOLFSSL_TEST_SUBROUTINE int wolfcrypt_mutex_test(void)
  38129. #else
  38130. WOLFSSL_TEST_SUBROUTINE int mutex_test(void)
  38131. #endif
  38132. {
  38133. #ifdef WOLFSSL_PTHREADS
  38134. wolfSSL_Mutex m;
  38135. #endif
  38136. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_USER_MUTEX)
  38137. #ifndef WOLFSSL_STATIC_MEMORY
  38138. wolfSSL_Mutex *mm = wc_InitAndAllocMutex();
  38139. #else
  38140. wolfSSL_Mutex *mm = (wolfSSL_Mutex*) XMALLOC(sizeof(wolfSSL_Mutex),
  38141. HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  38142. if (mm != NULL) {
  38143. if (wc_InitMutex(mm) != 0) {
  38144. WOLFSSL_MSG("Init Mutex failed");
  38145. XFREE(mm, HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  38146. mm = NULL;
  38147. }
  38148. }
  38149. #endif
  38150. if (mm == NULL)
  38151. return -13700;
  38152. wc_FreeMutex(mm);
  38153. XFREE(mm, HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  38154. #endif
  38155. /* Can optionally enable advanced pthread tests using "ENABLE_PTHREAD_LOCKFREE_TESTS" */
  38156. #ifdef WOLFSSL_PTHREADS
  38157. if (wc_InitMutex(&m) != 0)
  38158. return -13701;
  38159. if (wc_LockMutex(&m) != 0)
  38160. return -13702;
  38161. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  38162. /* trying to free a locked mutex is not portable behavior with pthread */
  38163. /* Attempting to destroy a locked mutex results in undefined behavior */
  38164. if (wc_FreeMutex(&m) != BAD_MUTEX_E)
  38165. return -13703;
  38166. #endif
  38167. if (wc_UnLockMutex(&m) != 0)
  38168. return -13704;
  38169. if (wc_FreeMutex(&m) != 0)
  38170. return -13705;
  38171. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  38172. /* Trying to use a pthread after free'ing is not portable behavior */
  38173. if (wc_LockMutex(&m) != BAD_MUTEX_E)
  38174. return -13706;
  38175. if (wc_UnLockMutex(&m) != BAD_MUTEX_E)
  38176. return -13707;
  38177. #endif
  38178. #endif
  38179. return 0;
  38180. }
  38181. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  38182. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  38183. !defined(WOLFSSL_STATIC_MEMORY)
  38184. static int malloc_cnt = 0;
  38185. static int realloc_cnt = 0;
  38186. static int free_cnt = 0;
  38187. #ifdef WOLFSSL_DEBUG_MEMORY
  38188. static void *my_Malloc_cb(size_t size, const char* func, unsigned int line)
  38189. {
  38190. (void) func;
  38191. (void) line;
  38192. #else
  38193. static void *my_Malloc_cb(size_t size)
  38194. {
  38195. #endif
  38196. malloc_cnt++;
  38197. #ifndef WOLFSSL_NO_MALLOC
  38198. return malloc(size);
  38199. #else
  38200. WOLFSSL_MSG("No malloc available");
  38201. (void)size;
  38202. return NULL;
  38203. #endif
  38204. }
  38205. #ifdef WOLFSSL_DEBUG_MEMORY
  38206. static void my_Free_cb(void *ptr, const char* func, unsigned int line)
  38207. {
  38208. (void) func;
  38209. (void) line;
  38210. #else
  38211. static void my_Free_cb(void *ptr)
  38212. {
  38213. #endif
  38214. free_cnt++;
  38215. #ifndef WOLFSSL_NO_MALLOC
  38216. free(ptr);
  38217. #else
  38218. WOLFSSL_MSG("No free available");
  38219. (void)ptr;
  38220. #endif
  38221. }
  38222. #ifdef WOLFSSL_DEBUG_MEMORY
  38223. static void *my_Realloc_cb(void *ptr, size_t size, const char* func, unsigned int line)
  38224. {
  38225. (void) func;
  38226. (void) line;
  38227. #else
  38228. static void *my_Realloc_cb(void *ptr, size_t size)
  38229. {
  38230. #endif
  38231. realloc_cnt++;
  38232. #ifndef WOLFSSL_NO_MALLOC
  38233. return realloc(ptr, size);
  38234. #else
  38235. WOLFSSL_MSG("No realloc available");
  38236. (void)ptr;
  38237. (void)size;
  38238. return NULL;
  38239. #endif
  38240. }
  38241. #endif /* !WOLFSSL_NO_MALLOC */
  38242. WOLFSSL_TEST_SUBROUTINE int memcb_test(void)
  38243. {
  38244. int ret = 0;
  38245. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  38246. !defined(WOLFSSL_STATIC_MEMORY)
  38247. byte* b = NULL;
  38248. #endif
  38249. wolfSSL_Malloc_cb mc;
  38250. wolfSSL_Free_cb fc;
  38251. wolfSSL_Realloc_cb rc;
  38252. /* Save existing memory callbacks */
  38253. if (wolfSSL_GetAllocators(&mc, &fc, &rc) != 0)
  38254. return -13800;
  38255. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  38256. !defined(WOLFSSL_STATIC_MEMORY)
  38257. /* test realloc */
  38258. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38259. if (b == NULL) {
  38260. ERROR_OUT(-13801, exit_memcb);
  38261. }
  38262. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38263. b = NULL;
  38264. /* Use API. */
  38265. if (wolfSSL_SetAllocators((wolfSSL_Malloc_cb)my_Malloc_cb,
  38266. (wolfSSL_Free_cb)my_Free_cb,
  38267. (wolfSSL_Realloc_cb)my_Realloc_cb) != 0) {
  38268. ERROR_OUT(-13802, exit_memcb);
  38269. }
  38270. b = (byte*)XMALLOC(1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38271. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38272. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38273. #ifndef WOLFSSL_STATIC_MEMORY
  38274. #ifndef WOLFSSL_CHECK_MEM_ZERO
  38275. if (malloc_cnt != 1 || free_cnt != 1 || realloc_cnt != 1)
  38276. #else
  38277. /* Checking zeroized memory means realloc is a malloc and free. */
  38278. if (malloc_cnt != 2 || free_cnt != 2 || realloc_cnt != 0)
  38279. #endif
  38280. #else
  38281. if (malloc_cnt != 0 || free_cnt != 0 || realloc_cnt != 0)
  38282. #endif
  38283. ret = -13803;
  38284. #endif /* !WOLFSSL_NO_MALLOC */
  38285. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  38286. !defined(WOLFSSL_STATIC_MEMORY)
  38287. exit_memcb:
  38288. /* reset malloc/free/realloc counts */
  38289. malloc_cnt = 0;
  38290. free_cnt = 0;
  38291. realloc_cnt = 0;
  38292. #endif
  38293. /* restore memory callbacks */
  38294. wolfSSL_SetAllocators(mc, fc, rc);
  38295. return ret;
  38296. }
  38297. #endif /* USE_WOLFSSL_MEMORY && !WOLFSSL_NO_MALLOC */
  38298. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  38299. WOLFSSL_TEST_SUBROUTINE int blob_test(void)
  38300. {
  38301. int ret = 0;
  38302. byte out[112];
  38303. byte blob[112];
  38304. word32 outSz;
  38305. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  38306. {
  38307. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  38308. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  38309. };
  38310. WOLFSSL_SMALL_STACK_STATIC const byte text[] =
  38311. {
  38312. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  38313. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  38314. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  38315. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  38316. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  38317. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  38318. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  38319. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  38320. };
  38321. XMEMSET(blob, 0, sizeof(blob));
  38322. XMEMSET(out, 0, sizeof(out));
  38323. outSz = sizeof(blob);
  38324. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  38325. if (ret != 0) {
  38326. ERROR_OUT(-13900, exit_blob);
  38327. }
  38328. blob[outSz - 2] += 1;
  38329. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  38330. if (ret == 0) { /* should fail with altered blob */
  38331. ERROR_OUT(-13901, exit_blob);
  38332. }
  38333. XMEMSET(blob, 0, sizeof(blob));
  38334. outSz = sizeof(blob);
  38335. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  38336. if (ret != 0) {
  38337. ERROR_OUT(-13902, exit_blob);
  38338. }
  38339. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  38340. if (ret != 0) {
  38341. ERROR_OUT(-13903, exit_blob);
  38342. }
  38343. if (XMEMCMP(out, iv, sizeof(iv))) {
  38344. ERROR_OUT(-13904, exit_blob);
  38345. }
  38346. XMEMSET(blob, 0, sizeof(blob));
  38347. outSz = sizeof(blob);
  38348. ret = wc_caamCreateBlob((byte*)text, sizeof(text), blob, &outSz);
  38349. if (ret != 0) {
  38350. ERROR_OUT(-13905, exit_blob);
  38351. }
  38352. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  38353. if (ret != 0) {
  38354. ERROR_OUT(-13906, exit_blob);
  38355. }
  38356. if (XMEMCMP(out, text, sizeof(text))) {
  38357. ERROR_OUT(-13907, exit_blob);
  38358. }
  38359. exit_blob:
  38360. return ret;
  38361. }
  38362. #endif /* WOLFSSL_IMX6_CAAM_BLOB */
  38363. #ifdef WOLF_CRYPTO_CB
  38364. /* Example custom context for crypto callback */
  38365. typedef struct {
  38366. int exampleVar; /* flag for testing if only crypt is enabled. */
  38367. } myCryptoDevCtx;
  38368. #ifdef WOLF_CRYPTO_CB_ONLY_RSA
  38369. /* Testing rsa cb when CB_ONLY_RSA is enabled
  38370. * When CB_ONLY_RSA is enabled, software imple. is not available.
  38371. *
  38372. * ctx callback ctx
  38373. * returen 0 on success, otherwise return -8000 - -8007
  38374. */
  38375. static int rsa_onlycb_test(myCryptoDevCtx *ctx)
  38376. {
  38377. int ret = 0;
  38378. #if !defined(NO_RSA)
  38379. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  38380. RsaKey *key = (RsaKey *)XMALLOC(sizeof *key,
  38381. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38382. byte* tmp = NULL;
  38383. #else
  38384. RsaKey key[1];
  38385. byte tmp[FOURK_BUF];
  38386. #endif
  38387. size_t bytes;
  38388. const word32 inLen = (word32)TEST_STRING_SZ;
  38389. word32 idx = 0;
  38390. word32 sigSz;
  38391. WOLFSSL_SMALL_STACK_STATIC const byte in[] = TEST_STRING;
  38392. byte out[RSA_TEST_BYTES];
  38393. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  38394. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  38395. !defined(NO_FILESYSTEM)
  38396. XFILE file;
  38397. #endif
  38398. #ifdef WOLFSSL_KEY_GEN
  38399. WC_RNG rng;
  38400. #endif
  38401. #ifdef USE_CERT_BUFFERS_1024
  38402. bytes = (size_t)sizeof_client_key_der_1024;
  38403. if (bytes < (size_t)sizeof_client_cert_der_1024)
  38404. bytes = (size_t)sizeof_client_cert_der_1024;
  38405. #elif defined(USE_CERT_BUFFERS_2048)
  38406. bytes = (size_t)sizeof_client_key_der_2048;
  38407. if (bytes < (size_t)sizeof_client_cert_der_2048)
  38408. bytes = (size_t)sizeof_client_cert_der_2048;
  38409. #elif defined(USE_CERT_BUFFERS_3072)
  38410. bytes = (size_t)sizeof_client_key_der_3072;
  38411. if (bytes < (size_t)sizeof_client_cert_der_3072)
  38412. bytes = (size_t)sizeof_client_cert_der_3072;
  38413. #elif defined(USE_CERT_BUFFERS_4096)
  38414. bytes = (size_t)sizeof_client_key_der_4096;
  38415. if (bytes < (size_t)sizeof_client_cert_der_4096)
  38416. bytes = (size_t)sizeof_client_cert_der_4096;
  38417. #else
  38418. bytes = FOURK_BUF;
  38419. #endif
  38420. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  38421. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38422. if (tmp == NULL)
  38423. ERROR_OUT(-8000, exit_onlycb);
  38424. #endif
  38425. #ifdef USE_CERT_BUFFERS_1024
  38426. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  38427. #elif defined(USE_CERT_BUFFERS_2048)
  38428. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  38429. #elif defined(USE_CERT_BUFFERS_3072)
  38430. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  38431. #elif defined(USE_CERT_BUFFERS_4096)
  38432. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  38433. #elif !defined(NO_FILESYSTEM)
  38434. file = XFOPEN(clientKey, "rb");
  38435. if (!file) {
  38436. err_sys("can't open ./certs/client-key.der, "
  38437. "Please run from wolfSSL home dir", -40);
  38438. ERROR_OUT(-8001, exit_onlycb);
  38439. }
  38440. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  38441. XFCLOSE(file);
  38442. #endif
  38443. #ifdef WOLFSSL_KEY_GEN
  38444. /* wc_CryptoCb_MakeRsaKey cb test, no actual making key
  38445. * wc_MakeRsaKey() -> rsa cb ->
  38446. * myCryptoDevCb -> wc_MakeRsaKey(CBONLY_TEST_DEVID)
  38447. * wc_MakeRsaKey(CBONLY_TEST_DEVID) expects to return 0(success)
  38448. */
  38449. ctx->exampleVar = 99;
  38450. ret = wc_MakeRsaKey(key, keySz, WC_RSA_EXPONENT, rng);
  38451. if (ret != 0) {
  38452. ERROR_OUT(-8002, exit_onlycb);
  38453. }
  38454. /* wc_MakeRsaKey() -> rsa cb ->
  38455. * myCryptoDevCb -> wc_MakeRsaKey(INVALID_DEVID)
  38456. * wc_MakeRsaKey(CBONLY_TEST_DEVID) expects to return NO_VALID_DEVID(failure)
  38457. */
  38458. ctx->exampleVar = 1;
  38459. ret = wc_MakeRsaKey(key, keySz, WC_RSA_EXPONENT, rng);
  38460. if (ret != NO_VALID_DEVID) {
  38461. ERROR_OUT(-8003, exit_onlycb);
  38462. } else
  38463. /* reset return code */
  38464. ret = 0;
  38465. #endif
  38466. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  38467. if (ret != 0) {
  38468. ERROR_OUT(-8004, exit_onlycb);
  38469. }
  38470. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  38471. if (ret != 0) {
  38472. ERROR_OUT(-8005, exit_onlycb);
  38473. }
  38474. sigSz = (word32)wc_RsaEncryptSize(key);
  38475. /* wc_CryptoCb_Rsa cb test, no actual rsa operation */
  38476. if (ret == 0) {
  38477. /* wc_SignatureGenerate() -> rsa cb ->
  38478. * myCryptoDevCb -> wc_RsaFunction(CBONLY_TEST_DEVID)
  38479. * wc_RsaFunction(CBONLY_TEST_DEVID) expects to return 0(success)
  38480. */
  38481. ctx->exampleVar = 99;
  38482. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  38483. in, inLen, out, &sigSz, key, sizeof(*key), NULL);
  38484. if (ret != 0) {
  38485. ERROR_OUT(-8006, exit_onlycb);
  38486. }
  38487. }
  38488. if (ret == 0) {
  38489. /* wc_SignatureGenerate() -> rsa cb ->
  38490. * myCryptoDevCb -> wc_RsaFunction(INVALID_DEVID)
  38491. * wc_SignatureGenerate(INVALID_DEVID) expects to
  38492. * return NO_VALID_DEVID(failure)
  38493. */
  38494. ctx->exampleVar = 1;
  38495. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  38496. in, inLen, out, &sigSz, key, sizeof(*key), NULL);
  38497. if (ret != NO_VALID_DEVID) {
  38498. ERROR_OUT(-8007, exit_onlycb);
  38499. } else
  38500. /* reset return code */
  38501. ret = 0;
  38502. }
  38503. exit_onlycb:
  38504. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  38505. if (key != NULL) {
  38506. wc_FreeRsaKey(key);
  38507. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38508. }
  38509. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38510. #else
  38511. wc_FreeRsaKey(key);
  38512. #endif
  38513. #endif
  38514. return ret;
  38515. }
  38516. #endif
  38517. #ifdef WOLF_CRYPTO_CB_ONLY_ECC
  38518. /* Testing rsa cb when CB_ONLY_ECC is enabled
  38519. * When CB_ONLY_ECC is enabled, software imple. is not available.
  38520. *
  38521. * ctx callback ctx
  38522. * returen 0 on success, otherwise return -8008 - -8018
  38523. */
  38524. static int ecc_onlycb_test(myCryptoDevCtx *ctx)
  38525. {
  38526. int ret = 0;
  38527. #if defined(HAVE_ECC)
  38528. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  38529. ecc_key* key = (ecc_key *)XMALLOC(sizeof *key,
  38530. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38531. ecc_key* pub = (ecc_key *)XMALLOC(sizeof *pub,
  38532. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38533. byte* out = (byte*)XMALLOC(sizeof(byte),
  38534. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38535. #ifdef OPENSSL_EXTRA
  38536. byte* check = (byte*)XMALLOC(sizeof(byte)*(256), HEAP_HINT,
  38537. DYNAMIC_TYPE_TMP_BUFFER);
  38538. #endif
  38539. #else
  38540. ecc_key key[1];
  38541. ecc_key pub[1];
  38542. byte out[256];
  38543. #ifdef OPENSSL_EXTRA
  38544. unsigned char check[256];
  38545. #endif
  38546. #endif
  38547. #ifdef OPENSSL_EXTRA
  38548. EVP_PKEY* privKey = NULL;
  38549. EVP_PKEY* pubKey = NULL;
  38550. ecc_key* pkey;
  38551. EVP_MD_CTX mdCtx;
  38552. const char testData[] = "Hi There";
  38553. size_t checkSz = -1;
  38554. const unsigned char* cp;
  38555. const unsigned char* p;
  38556. const unsigned char check_v[256] = {
  38557. 0x30,0x45,0x02,0x20,0x1b,0x5c,0x2a,0xf0,0x18,0x09,
  38558. 0x74,0x65,0xa1,0x04,0x76,0x3a,0xce,0xcc,0xe5,0x34,
  38559. 0x5e,0x89,0xed,0x40,0x1e,0x5a,0xb1,0x53,0xb4,0xff,
  38560. 0xc7,0x18,0xfe,0x0f,0xc7,0xa6,0x02,0x21,0x00,0xe5,
  38561. 0x70,0x21,0xfc,0xf9,0x63,0x36,0xfd,0x16,0x18,0x08,
  38562. 0x9a,0x63,0x61,0x0f,0xe7,0x7c,0xa3,0xc9,0x14,0xa3,
  38563. 0x30,0x87,0xf7,0xf5,0x70,0x19,0xaf,0x56,0x96,0x9b,
  38564. 0xd8,0x64,0xcd,0xd9,0xff,0x7b,0x2a,0x55,0x52,0xca,
  38565. 0x41,0xb2,0xa6,0xa4,0x8a,0x3b,0x02,0x20,0x8c,0xc5,
  38566. 0xf9,0xc1,0x7d,0x2a,0x65,0x6c,0xe6,0x5a,0xe3,0x76,
  38567. 0x9b,0xab,0x0b,0x9f,0xaf,0x62,0x5d,0xb2,0x60,0xd7,
  38568. 0xeb,0xb4,0x1b,0x73,0xdc,0x01,0x7d,0x7b,0xab,0xc1,
  38569. 0x0c,0x74,0x96,0x41,0xe6,0x3f,0xc5,0x86,0xe6,0x7d,
  38570. 0x2b,0x9d,0x54,0x6b,0xcd,0x31,0x35,0x1f,0xdb,0x49,
  38571. 0x1f,0x32,0x34,0xf8,0x57,0x12,0x86,0x5c,0x0e,0x80,
  38572. 0x55,0x8d,0xff,0xd8,0xbd,0xdf,0x32,0x26,0x62,0x42,
  38573. 0x09,0xda,0xf7,0x74,0xf2,0x3f,0xe6,0xf1,0x77,0x82,
  38574. 0xce,0xe4,0xbb,0x61,0xa6,0xc0,0x17,0x0c,0x6c,0x47,
  38575. 0x2a,0x40,0x1c,0x2b,0xe0,0x98,0x3b,0xbf,0xc6,0xf8,
  38576. 0x6d,0xfd,0xd0,0xfa,0xc1,0x02,0xfb,0x5f,0xfb,0xb0,
  38577. 0xcb,0xd9,0xa3,0x59,0x94,0xe9,0x0f,0x74,0xbb,0x3f,
  38578. 0x64,0xa3,0x83,0xc4,0x2b,0xf7,0xd2,0x97,0xbf,0x3b,
  38579. 0xcf,0xbb,0x60,0x81,0x33,0x94,0xfa,0x0d,0x35,0xd2,
  38580. 0x3d,0xb9,0x99,0xe3,0x12,0xf8,0xf4,0xa3,0x74,0xf4,
  38581. 0x94,0x1d,0x7a,0x66,0xf8,0xd1,0x1d,0xcf,0xb0,0x48,
  38582. 0xef,0x8c,0x94,0x6f,0xdd,0x62,
  38583. };
  38584. #endif
  38585. WC_RNG rng;
  38586. EncryptedInfo encInfo;
  38587. int keyFormat = 0;
  38588. word32 keyIdx = 0;
  38589. byte in[] = "Everyone gets Friday off. ecc p";
  38590. word32 inLen = (word32)XSTRLEN((char*)in);
  38591. word32 outLen;
  38592. int verify;
  38593. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  38594. if (key == NULL || pub == NULL) {
  38595. ERROR_OUT(-8008, exit_onlycb);
  38596. }
  38597. #endif
  38598. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  38599. if (ret != 0) {
  38600. ERROR_OUT(-8009, exit_onlycb);
  38601. }
  38602. /* wc_CryptoCb_MakeEccKey cb test, , no actual testing */
  38603. ctx->exampleVar = 99;
  38604. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, key);
  38605. if (ret != 0) {
  38606. ERROR_OUT(-8010, exit_onlycb);
  38607. }
  38608. ctx->exampleVar = 1;
  38609. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, key);
  38610. if (ret != NO_VALID_DEVID) {
  38611. ERROR_OUT(-8011, exit_onlycb);
  38612. } else
  38613. /* reset return code */
  38614. ret = 0;
  38615. #ifdef USE_CERT_BUFFERS_256
  38616. if (ret == 0) {
  38617. /* load ECC private key and perform private transform */
  38618. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &keyIdx,
  38619. key, sizeof_ecc_key_der_256);
  38620. }
  38621. if (ret != 0) {
  38622. ERROR_OUT(-8012, exit_onlycb);
  38623. }
  38624. /* wc_CryptoCb_EccSign cb test, no actual testing */
  38625. ctx->exampleVar = 99;
  38626. if (ret == 0) {
  38627. ret = wc_ecc_sign_hash(in, inLen, out, &outLen, &rng, key);
  38628. }
  38629. if (ret != 0) {
  38630. ERROR_OUT(-8013, exit_onlycb);
  38631. }
  38632. ctx->exampleVar = 1;
  38633. if (ret == 0) {
  38634. ret = wc_ecc_sign_hash(in, inLen, out, &outLen, &rng, key);
  38635. }
  38636. if (ret != NO_VALID_DEVID) {
  38637. ERROR_OUT(-8014, exit_onlycb);
  38638. }
  38639. else
  38640. ret = 0;
  38641. /* wc_CryptoCb_EccVerify cb test, no actual testing */
  38642. ctx->exampleVar = 99;
  38643. if (ret == 0) {
  38644. ret = wc_ecc_verify_hash(in, inLen, out, outLen, &verify, key);
  38645. }
  38646. if (ret != 0) {
  38647. ERROR_OUT(-8015, exit_onlycb);
  38648. }
  38649. ctx->exampleVar = 1;
  38650. if (ret == 0) {
  38651. ret = wc_ecc_verify_hash(in, inLen, out, outLen, &verify, key);
  38652. }
  38653. if (ret != NO_VALID_DEVID) {
  38654. ERROR_OUT(-8016, exit_onlycb);
  38655. }
  38656. else
  38657. ret = 0;
  38658. /* wc_CryptoCb_Ecdh cb test, no actual testing */
  38659. /* make public key for shared secret */
  38660. wc_ecc_init_ex(pub, HEAP_HINT, devId);
  38661. ctx->exampleVar = 99;
  38662. if (ret == 0) {
  38663. ret = wc_ecc_shared_secret(key, pub, out, &outLen);
  38664. }
  38665. if (ret != 0) {
  38666. ERROR_OUT(-8017, exit_onlycb);
  38667. }
  38668. ctx->exampleVar = 1;
  38669. if (ret == 0) {
  38670. ret = wc_ecc_shared_secret(key, pub, out, &outLen);
  38671. }
  38672. if (ret != NO_VALID_DEVID) {
  38673. ERROR_OUT(-8018, exit_onlycb);
  38674. }
  38675. else
  38676. ret = 0;
  38677. #ifdef OPENSSL_EXTRA
  38678. (void)pkey;
  38679. cp = ecc_clikey_der_256;
  38680. privKey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  38681. sizeof_ecc_clikey_der_256);
  38682. if (privKey == NULL) {
  38683. ERROR_OUT(-8019, exit_onlycb);
  38684. }
  38685. pkey = (ecc_key*)privKey->ecc->internal;
  38686. pkey->devId = devId;
  38687. p = ecc_clikeypub_der_256;
  38688. pubKey = d2i_PUBKEY(NULL, &p, sizeof_ecc_clikeypub_der_256);
  38689. if (pubKey == NULL) {
  38690. ERROR_OUT(-8020, exit_onlycb);
  38691. }
  38692. pkey = (ecc_key*)pubKey->ecc->internal;
  38693. pkey->devId = devId;
  38694. /* sign */
  38695. EVP_MD_CTX_init(&mdCtx);
  38696. ret = EVP_DigestSignInit(&mdCtx, NULL, EVP_sha256(), NULL, privKey);
  38697. if (ret != WOLFSSL_SUCCESS) {
  38698. ERROR_OUT(-8021, exit_onlycb);
  38699. }
  38700. ret = EVP_DigestSignUpdate(&mdCtx, testData,
  38701. (unsigned int)XSTRLEN(testData));
  38702. if (ret != WOLFSSL_SUCCESS) {
  38703. ERROR_OUT(-8022, exit_onlycb);
  38704. }
  38705. ret = EVP_DigestSignFinal(&mdCtx, NULL, &checkSz);
  38706. if (ret != WOLFSSL_SUCCESS) {
  38707. ERROR_OUT(-8023, exit_onlycb);
  38708. }
  38709. ctx->exampleVar = 99;
  38710. ret = EVP_DigestSignFinal(&mdCtx, check, &checkSz);
  38711. /* just called crypt callback as dummy
  38712. * EVP_DigestSignFinal returns 0 internally.
  38713. */
  38714. if (ret != 0) {
  38715. ERROR_OUT(-8024, exit_onlycb);
  38716. }
  38717. ctx->exampleVar = 1;
  38718. ret = EVP_DigestSignFinal(&mdCtx, check, &checkSz);
  38719. /* just called crypt callback as dummy
  38720. * EVP_DigestSignFinal returns 0 internally.
  38721. */
  38722. if (ret != 0) {
  38723. ERROR_OUT(-8025, exit_onlycb);
  38724. }
  38725. /* restore checkSz for verify */
  38726. checkSz = 71;
  38727. ret = EVP_MD_CTX_cleanup(&mdCtx);
  38728. if (ret != SSL_SUCCESS) {
  38729. ERROR_OUT(-8026, exit_onlycb);
  38730. }
  38731. /* verify */
  38732. EVP_MD_CTX_init(&mdCtx);
  38733. if (ret == SSL_SUCCESS) {
  38734. ret = EVP_DigestVerifyInit(&mdCtx, NULL, EVP_sha256(), NULL, pubKey);
  38735. }
  38736. if (ret != WOLFSSL_SUCCESS) {
  38737. ERROR_OUT(-8027, exit_onlycb);
  38738. }
  38739. if (ret == WOLFSSL_SUCCESS) {
  38740. ret = EVP_DigestVerifyUpdate(&mdCtx, testData,
  38741. (unsigned int)XSTRLEN(testData));
  38742. }
  38743. if (ret != WOLFSSL_SUCCESS) {
  38744. ERROR_OUT(-8028, exit_onlycb);
  38745. }
  38746. ctx->exampleVar = 99;
  38747. ret = EVP_DigestVerifyFinal(&mdCtx, check_v, checkSz);
  38748. /* just called crypt callback as dummy
  38749. * EVP_DigestSignFinal returns 0 internally.
  38750. */
  38751. if (ret != 0) {
  38752. ERROR_OUT(-8029, exit_onlycb);
  38753. }
  38754. ctx->exampleVar = 1;
  38755. ret = EVP_DigestVerifyFinal(&mdCtx, check_v, checkSz);
  38756. /* just called crypt callback as dummy
  38757. * EVP_DigestVerifyFinal returns -1 internally rather than NO_VALID_DEVID.
  38758. */
  38759. if (ret != -1) {
  38760. ERROR_OUT(-8030, exit_onlycb);
  38761. }
  38762. ret = EVP_MD_CTX_cleanup(&mdCtx);
  38763. if (ret != SSL_SUCCESS) {
  38764. ERROR_OUT(-8031, exit_onlycb);
  38765. } else
  38766. ret = 0;
  38767. #endif
  38768. #else
  38769. (void)verify;
  38770. (void)outLen;
  38771. (void)inLen;
  38772. (void)out;
  38773. (void)pub;
  38774. #ifdef OPENSSL_EXTRA
  38775. (void)privKey;
  38776. (void)pubKey;
  38777. (void)mdCtx;
  38778. (void)check;
  38779. (void)checkSz;
  38780. (void)p;
  38781. #endif
  38782. #endif
  38783. (void)keyFormat;
  38784. (void)encInfo;
  38785. exit_onlycb:
  38786. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  38787. if (key != NULL) {
  38788. wc_ecc_free(key);
  38789. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38790. }
  38791. if (pub != NULL) {
  38792. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38793. }
  38794. if (out != NULL) {
  38795. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38796. }
  38797. #ifdef OPENSSL_EXTRA
  38798. if (check) {
  38799. FREE(check, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  38800. }
  38801. #endif
  38802. #else
  38803. wc_ecc_free(key);
  38804. #ifdef OPENSSL_EXTRA
  38805. if (privKey)
  38806. EVP_PKEY_free(privKey);
  38807. if (pubKey)
  38808. EVP_PKEY_free(pubKey);
  38809. #endif
  38810. #endif
  38811. #endif /* HAVE_ECC */
  38812. return ret;
  38813. }
  38814. #endif
  38815. /* Example crypto dev callback function that calls software version */
  38816. static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
  38817. {
  38818. int ret = NOT_COMPILED_IN; /* return this to bypass HW and use SW */
  38819. myCryptoDevCtx* myCtx = (myCryptoDevCtx*)ctx;
  38820. if (info == NULL)
  38821. return BAD_FUNC_ARG;
  38822. #ifdef DEBUG_WOLFSSL
  38823. printf("CryptoDevCb: Algo Type %d\n", info->algo_type);
  38824. #endif
  38825. if (info->algo_type == WC_ALGO_TYPE_RNG) {
  38826. #ifndef WC_NO_RNG
  38827. /* set devId to invalid, so software is used */
  38828. info->rng.rng->devId = INVALID_DEVID;
  38829. ret = wc_RNG_GenerateBlock(info->rng.rng,
  38830. info->rng.out, info->rng.sz);
  38831. /* reset devId */
  38832. info->rng.rng->devId = devIdArg;
  38833. #endif
  38834. }
  38835. else if (info->algo_type == WC_ALGO_TYPE_SEED) {
  38836. #ifndef WC_NO_RNG
  38837. ALIGN32 static byte seed[sizeof(word32)] = { 0x00, 0x00, 0x00, 0x01 };
  38838. word32* seedWord32 = (word32*)seed;
  38839. word32 len;
  38840. /* wc_GenerateSeed is a local symbol so we need to fake the entropy. */
  38841. while (info->seed.sz > 0) {
  38842. len = (word32)sizeof(seed);
  38843. if (info->seed.sz < len)
  38844. len = info->seed.sz;
  38845. XMEMCPY(info->seed.seed, seed, sizeof(seed));
  38846. info->seed.seed += len;
  38847. info->seed.sz -= len;
  38848. (*seedWord32)++;
  38849. }
  38850. ret = 0;
  38851. #endif
  38852. }
  38853. else if (info->algo_type == WC_ALGO_TYPE_PK) {
  38854. #ifdef DEBUG_WOLFSSL
  38855. printf("CryptoDevCb: Pk Type %d\n", info->pk.type);
  38856. #endif
  38857. #ifndef NO_RSA
  38858. if (info->pk.type == WC_PK_TYPE_RSA) {
  38859. /* set devId to invalid, so software is used */
  38860. info->pk.rsa.key->devId = INVALID_DEVID;
  38861. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  38862. #ifdef DEBUG_WOLFSSL
  38863. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  38864. #endif
  38865. if (myCtx->exampleVar == 99) {
  38866. info->pk.rsa.key->devId = devIdArg;
  38867. return 0;
  38868. }
  38869. #endif
  38870. switch (info->pk.rsa.type) {
  38871. case RSA_PUBLIC_ENCRYPT:
  38872. case RSA_PUBLIC_DECRYPT:
  38873. /* perform software based RSA public op */
  38874. ret = wc_RsaFunction(
  38875. info->pk.rsa.in, info->pk.rsa.inLen,
  38876. info->pk.rsa.out, info->pk.rsa.outLen,
  38877. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  38878. break;
  38879. case RSA_PRIVATE_ENCRYPT:
  38880. case RSA_PRIVATE_DECRYPT:
  38881. /* perform software based RSA private op */
  38882. ret = wc_RsaFunction(
  38883. info->pk.rsa.in, info->pk.rsa.inLen,
  38884. info->pk.rsa.out, info->pk.rsa.outLen,
  38885. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  38886. break;
  38887. }
  38888. /* reset devId */
  38889. info->pk.rsa.key->devId = devIdArg;
  38890. }
  38891. #ifdef WOLFSSL_KEY_GEN
  38892. else if (info->pk.type == WC_PK_TYPE_RSA_KEYGEN) {
  38893. info->pk.rsakg.key->devId = INVALID_DEVID;
  38894. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  38895. #ifdef DEBUG_WOLFSSL
  38896. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  38897. #endif
  38898. if (myCtx->exampleVar == 99) {
  38899. info->pk.rsakg.key->devId = devIdArg;
  38900. return 0;
  38901. }
  38902. #endif
  38903. #ifdef HAVE_FIPS
  38904. for (;;) {
  38905. #endif
  38906. ret = wc_MakeRsaKey(info->pk.rsakg.key, info->pk.rsakg.size,
  38907. info->pk.rsakg.e, info->pk.rsakg.rng);
  38908. #ifdef HAVE_FIPS
  38909. if (ret == PRIME_GEN_E)
  38910. continue;
  38911. break;
  38912. }
  38913. #endif
  38914. /* reset devId */
  38915. info->pk.rsakg.key->devId = devIdArg;
  38916. }
  38917. #endif
  38918. #endif /* !NO_RSA */
  38919. #ifdef HAVE_ECC
  38920. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  38921. /* set devId to invalid, so software is used */
  38922. info->pk.eckg.key->devId = INVALID_DEVID;
  38923. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  38924. #ifdef DEBUG_WOLFSSL
  38925. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  38926. #endif
  38927. if (myCtx->exampleVar == 99) {
  38928. info->pk.eckg.key->devId = devIdArg;
  38929. return 0;
  38930. }
  38931. #endif
  38932. ret = wc_ecc_make_key_ex(info->pk.eckg.rng, info->pk.eckg.size,
  38933. info->pk.eckg.key, info->pk.eckg.curveId);
  38934. /* reset devId */
  38935. info->pk.eckg.key->devId = devIdArg;
  38936. }
  38937. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  38938. /* set devId to invalid, so software is used */
  38939. info->pk.eccsign.key->devId = INVALID_DEVID;
  38940. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  38941. #ifdef DEBUG_WOLFSSL
  38942. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  38943. #endif
  38944. if (myCtx->exampleVar == 99) {
  38945. info->pk.eccsign.key->devId = devIdArg;
  38946. return 0;
  38947. }
  38948. #endif
  38949. ret = wc_ecc_sign_hash(
  38950. info->pk.eccsign.in, info->pk.eccsign.inlen,
  38951. info->pk.eccsign.out, info->pk.eccsign.outlen,
  38952. info->pk.eccsign.rng, info->pk.eccsign.key);
  38953. /* reset devId */
  38954. info->pk.eccsign.key->devId = devIdArg;
  38955. }
  38956. else if (info->pk.type == WC_PK_TYPE_ECDSA_VERIFY) {
  38957. /* set devId to invalid, so software is used */
  38958. info->pk.eccverify.key->devId = INVALID_DEVID;
  38959. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  38960. #ifdef DEBUG_WOLFSSL
  38961. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  38962. #endif
  38963. if (myCtx->exampleVar == 99) {
  38964. info->pk.eccverify.key->devId = devIdArg;
  38965. return 0;
  38966. }
  38967. #endif
  38968. ret = wc_ecc_verify_hash(
  38969. info->pk.eccverify.sig, info->pk.eccverify.siglen,
  38970. info->pk.eccverify.hash, info->pk.eccverify.hashlen,
  38971. info->pk.eccverify.res, info->pk.eccverify.key);
  38972. /* reset devId */
  38973. info->pk.eccverify.key->devId = devIdArg;
  38974. }
  38975. else if (info->pk.type == WC_PK_TYPE_ECDH) {
  38976. /* set devId to invalid, so software is used */
  38977. info->pk.ecdh.private_key->devId = INVALID_DEVID;
  38978. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  38979. #ifdef DEBUG_WOLFSSL
  38980. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  38981. #endif
  38982. if (myCtx->exampleVar == 99) {
  38983. info->pk.ecdh.private_key->devId = devIdArg;
  38984. return 0;
  38985. }
  38986. #endif
  38987. ret = wc_ecc_shared_secret(
  38988. info->pk.ecdh.private_key, info->pk.ecdh.public_key,
  38989. info->pk.ecdh.out, info->pk.ecdh.outlen);
  38990. /* reset devId */
  38991. info->pk.ecdh.private_key->devId = devIdArg;
  38992. }
  38993. #endif /* HAVE_ECC */
  38994. #ifdef HAVE_CURVE25519
  38995. if (info->pk.type == WC_PK_TYPE_CURVE25519_KEYGEN) {
  38996. /* set devId to invalid, so software is used */
  38997. info->pk.curve25519kg.key->devId = INVALID_DEVID;
  38998. ret = wc_curve25519_make_key(info->pk.curve25519kg.rng,
  38999. info->pk.curve25519kg.size, info->pk.curve25519kg.key);
  39000. /* reset devId */
  39001. info->pk.curve25519kg.key->devId = devIdArg;
  39002. }
  39003. else if (info->pk.type == WC_PK_TYPE_CURVE25519) {
  39004. /* set devId to invalid, so software is used */
  39005. info->pk.curve25519.private_key->devId = INVALID_DEVID;
  39006. ret = wc_curve25519_shared_secret_ex(
  39007. info->pk.curve25519.private_key, info->pk.curve25519.public_key,
  39008. info->pk.curve25519.out, info->pk.curve25519.outlen,
  39009. info->pk.curve25519.endian);
  39010. /* reset devId */
  39011. info->pk.curve25519.private_key->devId = devIdArg;
  39012. }
  39013. #endif /* HAVE_CURVE25519 */
  39014. #ifdef HAVE_ED25519
  39015. if (info->pk.type == WC_PK_TYPE_ED25519_KEYGEN) {
  39016. /* set devId to invalid, so software is used */
  39017. info->pk.ed25519kg.key->devId = INVALID_DEVID;
  39018. ret = wc_ed25519_make_key(info->pk.ed25519kg.rng,
  39019. info->pk.ed25519kg.size, info->pk.ed25519kg.key);
  39020. /* reset devId */
  39021. info->pk.ed25519kg.key->devId = devIdArg;
  39022. }
  39023. #ifdef HAVE_ED25519_SIGN
  39024. else if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  39025. /* set devId to invalid, so software is used */
  39026. info->pk.ed25519sign.key->devId = INVALID_DEVID;
  39027. ret = wc_ed25519_sign_msg_ex(
  39028. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  39029. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  39030. info->pk.ed25519sign.key, info->pk.ed25519sign.type,
  39031. info->pk.ed25519sign.context, info->pk.ed25519sign.contextLen);
  39032. /* reset devId */
  39033. info->pk.ed25519sign.key->devId = devIdArg;
  39034. }
  39035. #endif
  39036. #ifdef HAVE_ED25519_VERIFY
  39037. else if (info->pk.type == WC_PK_TYPE_ED25519_VERIFY) {
  39038. /* set devId to invalid, so software is used */
  39039. info->pk.ed25519verify.key->devId = INVALID_DEVID;
  39040. ret = wc_ed25519_verify_msg_ex(
  39041. info->pk.ed25519verify.sig, info->pk.ed25519verify.sigLen,
  39042. info->pk.ed25519verify.msg, info->pk.ed25519verify.msgLen,
  39043. info->pk.ed25519verify.res, info->pk.ed25519verify.key,
  39044. info->pk.ed25519verify.type, info->pk.ed25519verify.context,
  39045. info->pk.ed25519verify.contextLen);
  39046. /* reset devId */
  39047. info->pk.ed25519verify.key->devId = devIdArg;
  39048. }
  39049. #endif
  39050. #endif /* HAVE_ED25519 */
  39051. }
  39052. else if (info->algo_type == WC_ALGO_TYPE_CIPHER) {
  39053. #if !defined(NO_AES) || !defined(NO_DES3)
  39054. #ifdef HAVE_AESGCM
  39055. if (info->cipher.type == WC_CIPHER_AES_GCM) {
  39056. if (info->cipher.enc) {
  39057. /* set devId to invalid, so software is used */
  39058. info->cipher.aesgcm_enc.aes->devId = INVALID_DEVID;
  39059. ret = wc_AesGcmEncrypt(
  39060. info->cipher.aesgcm_enc.aes,
  39061. info->cipher.aesgcm_enc.out,
  39062. info->cipher.aesgcm_enc.in,
  39063. info->cipher.aesgcm_enc.sz,
  39064. info->cipher.aesgcm_enc.iv,
  39065. info->cipher.aesgcm_enc.ivSz,
  39066. info->cipher.aesgcm_enc.authTag,
  39067. info->cipher.aesgcm_enc.authTagSz,
  39068. info->cipher.aesgcm_enc.authIn,
  39069. info->cipher.aesgcm_enc.authInSz);
  39070. /* reset devId */
  39071. info->cipher.aesgcm_enc.aes->devId = devIdArg;
  39072. }
  39073. else {
  39074. /* set devId to invalid, so software is used */
  39075. info->cipher.aesgcm_dec.aes->devId = INVALID_DEVID;
  39076. ret = wc_AesGcmDecrypt(
  39077. info->cipher.aesgcm_dec.aes,
  39078. info->cipher.aesgcm_dec.out,
  39079. info->cipher.aesgcm_dec.in,
  39080. info->cipher.aesgcm_dec.sz,
  39081. info->cipher.aesgcm_dec.iv,
  39082. info->cipher.aesgcm_dec.ivSz,
  39083. info->cipher.aesgcm_dec.authTag,
  39084. info->cipher.aesgcm_dec.authTagSz,
  39085. info->cipher.aesgcm_dec.authIn,
  39086. info->cipher.aesgcm_dec.authInSz);
  39087. /* reset devId */
  39088. info->cipher.aesgcm_dec.aes->devId = devIdArg;
  39089. }
  39090. }
  39091. #endif /* HAVE_AESGCM */
  39092. #ifdef HAVE_AES_CBC
  39093. if (info->cipher.type == WC_CIPHER_AES_CBC) {
  39094. if (info->cipher.enc) {
  39095. /* set devId to invalid, so software is used */
  39096. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  39097. ret = wc_AesCbcEncrypt(
  39098. info->cipher.aescbc.aes,
  39099. info->cipher.aescbc.out,
  39100. info->cipher.aescbc.in,
  39101. info->cipher.aescbc.sz);
  39102. /* reset devId */
  39103. info->cipher.aescbc.aes->devId = devIdArg;
  39104. }
  39105. else {
  39106. /* set devId to invalid, so software is used */
  39107. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  39108. ret = wc_AesCbcDecrypt(
  39109. info->cipher.aescbc.aes,
  39110. info->cipher.aescbc.out,
  39111. info->cipher.aescbc.in,
  39112. info->cipher.aescbc.sz);
  39113. /* reset devId */
  39114. info->cipher.aescbc.aes->devId = devIdArg;
  39115. }
  39116. }
  39117. #endif /* HAVE_AES_CBC */
  39118. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  39119. if (info->cipher.type == WC_CIPHER_AES_ECB) {
  39120. if (info->cipher.enc) {
  39121. /* set devId to invalid, so software is used */
  39122. info->cipher.aesecb.aes->devId = INVALID_DEVID;
  39123. ret = wc_AesEcbEncrypt(
  39124. info->cipher.aesecb.aes,
  39125. info->cipher.aesecb.out,
  39126. info->cipher.aesecb.in,
  39127. info->cipher.aesecb.sz);
  39128. /* reset devId */
  39129. info->cipher.aesecb.aes->devId = devIdArg;
  39130. }
  39131. else {
  39132. /* set devId to invalid, so software is used */
  39133. info->cipher.aesecb.aes->devId = INVALID_DEVID;
  39134. ret = wc_AesEcbDecrypt(
  39135. info->cipher.aesecb.aes,
  39136. info->cipher.aesecb.out,
  39137. info->cipher.aesecb.in,
  39138. info->cipher.aesecb.sz);
  39139. /* reset devId */
  39140. info->cipher.aesecb.aes->devId = devIdArg;
  39141. }
  39142. }
  39143. #endif /* HAVE_AES_ECB */
  39144. #if defined(WOLFSSL_AES_COUNTER) && !defined(HAVE_FIPS) && \
  39145. !defined(HAVE_SELFTEST)
  39146. if (info->cipher.type == WC_CIPHER_AES_CTR) {
  39147. /* set devId to invalid, so software is used */
  39148. info->cipher.aesctr.aes->devId = INVALID_DEVID;
  39149. ret = wc_AesCtrEncrypt(
  39150. info->cipher.aesctr.aes,
  39151. info->cipher.aesctr.out,
  39152. info->cipher.aesctr.in,
  39153. info->cipher.aesctr.sz);
  39154. /* reset devId */
  39155. info->cipher.aesctr.aes->devId = devIdArg;
  39156. }
  39157. #endif /* WOLFSSL_AES_COUNTER */
  39158. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  39159. if (info->cipher.type == WC_CIPHER_AES_CCM) {
  39160. if (info->cipher.enc) {
  39161. /* set devId to invalid, so software is used */
  39162. info->cipher.aesccm_enc.aes->devId = INVALID_DEVID;
  39163. ret = wc_AesCcmEncrypt(
  39164. info->cipher.aesccm_enc.aes,
  39165. info->cipher.aesccm_enc.out,
  39166. info->cipher.aesccm_enc.in,
  39167. info->cipher.aesccm_enc.sz,
  39168. info->cipher.aesccm_enc.nonce,
  39169. info->cipher.aesccm_enc.nonceSz,
  39170. info->cipher.aesccm_enc.authTag,
  39171. info->cipher.aesccm_enc.authTagSz,
  39172. info->cipher.aesccm_enc.authIn,
  39173. info->cipher.aesccm_enc.authInSz);
  39174. /* reset devId */
  39175. info->cipher.aesccm_enc.aes->devId = devIdArg;
  39176. }
  39177. else {
  39178. /* set devId to invalid, so software is used */
  39179. info->cipher.aesccm_dec.aes->devId = INVALID_DEVID;
  39180. ret = wc_AesCcmDecrypt(
  39181. info->cipher.aesccm_dec.aes,
  39182. info->cipher.aesccm_dec.out,
  39183. info->cipher.aesccm_dec.in,
  39184. info->cipher.aesccm_dec.sz,
  39185. info->cipher.aesccm_dec.nonce,
  39186. info->cipher.aesccm_dec.nonceSz,
  39187. info->cipher.aesccm_dec.authTag,
  39188. info->cipher.aesccm_dec.authTagSz,
  39189. info->cipher.aesccm_dec.authIn,
  39190. info->cipher.aesccm_dec.authInSz);
  39191. /* reset devId */
  39192. info->cipher.aesccm_dec.aes->devId = devIdArg;
  39193. }
  39194. }
  39195. #endif
  39196. #ifndef NO_DES3
  39197. if (info->cipher.type == WC_CIPHER_DES3) {
  39198. if (info->cipher.enc) {
  39199. /* set devId to invalid, so software is used */
  39200. info->cipher.des3.des->devId = INVALID_DEVID;
  39201. ret = wc_Des3_CbcEncrypt(
  39202. info->cipher.des3.des,
  39203. info->cipher.des3.out,
  39204. info->cipher.des3.in,
  39205. info->cipher.des3.sz);
  39206. /* reset devId */
  39207. info->cipher.des3.des->devId = devIdArg;
  39208. }
  39209. else {
  39210. /* set devId to invalid, so software is used */
  39211. info->cipher.des3.des->devId = INVALID_DEVID;
  39212. ret = wc_Des3_CbcDecrypt(
  39213. info->cipher.des3.des,
  39214. info->cipher.des3.out,
  39215. info->cipher.des3.in,
  39216. info->cipher.des3.sz);
  39217. /* reset devId */
  39218. info->cipher.des3.des->devId = devIdArg;
  39219. }
  39220. }
  39221. #endif /* !NO_DES3 */
  39222. #endif /* !NO_AES || !NO_DES3 */
  39223. }
  39224. #if !defined(NO_SHA) || !defined(NO_SHA256) || \
  39225. defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)
  39226. else if (info->algo_type == WC_ALGO_TYPE_HASH) {
  39227. #if !defined(NO_SHA)
  39228. if (info->hash.type == WC_HASH_TYPE_SHA) {
  39229. if (info->hash.sha1 == NULL)
  39230. return NOT_COMPILED_IN;
  39231. /* set devId to invalid, so software is used */
  39232. info->hash.sha1->devId = INVALID_DEVID;
  39233. if (info->hash.in != NULL) {
  39234. ret = wc_ShaUpdate(
  39235. info->hash.sha1,
  39236. info->hash.in,
  39237. info->hash.inSz);
  39238. }
  39239. if (info->hash.digest != NULL) {
  39240. ret = wc_ShaFinal(
  39241. info->hash.sha1,
  39242. info->hash.digest);
  39243. }
  39244. /* reset devId */
  39245. info->hash.sha1->devId = devIdArg;
  39246. }
  39247. else
  39248. #endif
  39249. #if !defined(NO_SHA256)
  39250. if (info->hash.type == WC_HASH_TYPE_SHA256) {
  39251. if (info->hash.sha256 == NULL)
  39252. return NOT_COMPILED_IN;
  39253. /* set devId to invalid, so software is used */
  39254. info->hash.sha256->devId = INVALID_DEVID;
  39255. if (info->hash.in != NULL) {
  39256. ret = wc_Sha256Update(
  39257. info->hash.sha256,
  39258. info->hash.in,
  39259. info->hash.inSz);
  39260. }
  39261. if (info->hash.digest != NULL) {
  39262. ret = wc_Sha256Final(
  39263. info->hash.sha256,
  39264. info->hash.digest);
  39265. }
  39266. /* reset devId */
  39267. info->hash.sha256->devId = devIdArg;
  39268. }
  39269. else
  39270. #endif
  39271. #ifdef WOLFSSL_SHA384
  39272. if (info->hash.type == WC_HASH_TYPE_SHA384) {
  39273. if (info->hash.sha384 == NULL)
  39274. return NOT_COMPILED_IN;
  39275. #ifndef NO_SHA2_CRYPTO_CB
  39276. /* set devId to invalid, so software is used */
  39277. info->hash.sha384->devId = INVALID_DEVID;
  39278. #endif
  39279. if (info->hash.in != NULL) {
  39280. ret = wc_Sha384Update(
  39281. info->hash.sha384,
  39282. info->hash.in,
  39283. info->hash.inSz);
  39284. }
  39285. if (info->hash.digest != NULL) {
  39286. ret = wc_Sha384Final(
  39287. info->hash.sha384,
  39288. info->hash.digest);
  39289. }
  39290. #ifndef NO_SHA2_CRYPTO_CB
  39291. /* reset devId */
  39292. info->hash.sha384->devId = devIdArg;
  39293. #endif
  39294. }
  39295. else
  39296. #endif
  39297. #ifdef WOLFSSL_SHA512
  39298. if (info->hash.type == WC_HASH_TYPE_SHA512) {
  39299. if (info->hash.sha512 == NULL)
  39300. return NOT_COMPILED_IN;
  39301. #ifndef NO_SHA2_CRYPTO_CB
  39302. /* set devId to invalid, so software is used */
  39303. info->hash.sha512->devId = INVALID_DEVID;
  39304. #endif
  39305. if (info->hash.in != NULL) {
  39306. ret = wc_Sha512Update(
  39307. info->hash.sha512,
  39308. info->hash.in,
  39309. info->hash.inSz);
  39310. }
  39311. if (info->hash.digest != NULL) {
  39312. ret = wc_Sha512Final(
  39313. info->hash.sha512,
  39314. info->hash.digest);
  39315. }
  39316. #ifndef NO_SHA2_CRYPTO_CB
  39317. /* reset devId */
  39318. info->hash.sha512->devId = devIdArg;
  39319. #endif
  39320. }
  39321. else
  39322. #endif
  39323. {
  39324. }
  39325. }
  39326. #endif /* !NO_SHA || !NO_SHA256 */
  39327. #ifndef NO_HMAC
  39328. else if (info->algo_type == WC_ALGO_TYPE_HMAC) {
  39329. if (info->hmac.hmac == NULL)
  39330. return NOT_COMPILED_IN;
  39331. /* set devId to invalid, so software is used */
  39332. info->hmac.hmac->devId = INVALID_DEVID;
  39333. if (info->hash.in != NULL) {
  39334. ret = wc_HmacUpdate(
  39335. info->hmac.hmac,
  39336. info->hmac.in,
  39337. info->hmac.inSz);
  39338. }
  39339. else if (info->hash.digest != NULL) {
  39340. ret = wc_HmacFinal(
  39341. info->hmac.hmac,
  39342. info->hmac.digest);
  39343. }
  39344. /* reset devId */
  39345. info->hmac.hmac->devId = devIdArg;
  39346. }
  39347. #endif
  39348. (void)devIdArg;
  39349. (void)myCtx;
  39350. return ret;
  39351. }
  39352. WOLFSSL_TEST_SUBROUTINE int cryptocb_test(void)
  39353. {
  39354. int ret = 0;
  39355. myCryptoDevCtx myCtx;
  39356. /* example data for callback */
  39357. myCtx.exampleVar = 1;
  39358. /* set devId to something other than INVALID_DEVID */
  39359. devId = 1;
  39360. ret = wc_CryptoCb_RegisterDevice(devId, myCryptoDevCb, &myCtx);
  39361. #ifndef WC_NO_RNG
  39362. if (ret == 0)
  39363. ret = random_test();
  39364. #endif /* WC_NO_RNG */
  39365. #if !defined(NO_RSA)
  39366. PRIVATE_KEY_UNLOCK();
  39367. if (ret == 0)
  39368. ret = rsa_test();
  39369. PRIVATE_KEY_LOCK();
  39370. #endif
  39371. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  39372. PRIVATE_KEY_UNLOCK();
  39373. if (ret == 0)
  39374. ret = rsa_onlycb_test(&myCtx);
  39375. PRIVATE_KEY_LOCK();
  39376. #endif
  39377. #if defined(HAVE_ECC)
  39378. PRIVATE_KEY_UNLOCK();
  39379. if (ret == 0)
  39380. ret = ecc_test();
  39381. PRIVATE_KEY_LOCK();
  39382. #endif
  39383. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  39384. PRIVATE_KEY_UNLOCK();
  39385. if (ret == 0)
  39386. ret = ecc_onlycb_test(&myCtx);
  39387. PRIVATE_KEY_LOCK();
  39388. #endif
  39389. #ifdef HAVE_ED25519
  39390. if (ret == 0)
  39391. ret = ed25519_test();
  39392. #endif
  39393. #ifdef HAVE_CURVE25519
  39394. if (ret == 0)
  39395. ret = curve25519_test();
  39396. #endif
  39397. #ifndef NO_AES
  39398. #ifdef HAVE_AESGCM
  39399. if (ret == 0)
  39400. ret = aesgcm_test();
  39401. #endif
  39402. #ifdef HAVE_AES_CBC
  39403. if (ret == 0)
  39404. ret = aes_test();
  39405. #endif
  39406. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  39407. if (ret == 0)
  39408. ret = aesccm_test();
  39409. #endif
  39410. #endif /* !NO_AES */
  39411. #ifndef NO_DES3
  39412. if (ret == 0)
  39413. ret = des3_test();
  39414. #endif /* !NO_DES3 */
  39415. #ifndef NO_SHA
  39416. if (ret == 0)
  39417. ret = sha_test();
  39418. #endif
  39419. #ifndef NO_SHA256
  39420. if (ret == 0)
  39421. ret = sha256_test();
  39422. #endif
  39423. #ifdef WOLFSSL_SHA384
  39424. if (ret == 0)
  39425. ret = sha384_test();
  39426. #endif
  39427. #ifdef WOLFSSL_SHA512
  39428. if (ret == 0)
  39429. ret = sha512_test();
  39430. #endif
  39431. #ifndef NO_HMAC
  39432. #ifndef NO_SHA
  39433. if (ret == 0)
  39434. ret = hmac_sha_test();
  39435. #endif
  39436. #ifndef NO_SHA256
  39437. if (ret == 0)
  39438. ret = hmac_sha256_test();
  39439. #endif
  39440. #endif
  39441. #ifndef NO_PWDBASED
  39442. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256)
  39443. if (ret == 0)
  39444. ret = pbkdf2_test();
  39445. #endif
  39446. #endif
  39447. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  39448. if (ret == 0)
  39449. ret = cmac_test();
  39450. #endif
  39451. /* reset devId */
  39452. devId = INVALID_DEVID;
  39453. return ret;
  39454. }
  39455. #endif /* WOLF_CRYPTO_CB */
  39456. #ifdef WOLFSSL_CERT_PIV
  39457. WOLFSSL_TEST_SUBROUTINE int certpiv_test(void)
  39458. {
  39459. int ret;
  39460. wc_CertPIV piv;
  39461. /* Template for Identiv PIV cert, nonce and signature */
  39462. WOLFSSL_SMALL_STACK_STATIC const byte pivCertIdentiv[] = {
  39463. 0x0A, 0x0B,
  39464. 0x53, 0x09, /* NIST PIV Cert */
  39465. 0x70, 0x02, /* Certificate */
  39466. 0x30, 0x00,
  39467. 0x71, 0x01, 0x05, /* Cert Info */
  39468. 0xFE, 0x00, /* Error Detection */
  39469. 0x0B, 0x01, 0x00, /* Nonce */
  39470. 0x0C, 0x01, 0x00, /* Signed Nonce */
  39471. };
  39472. /* PIV certificate data including certificate, info and error dectection. */
  39473. WOLFSSL_SMALL_STACK_STATIC const byte pivCert[] = {
  39474. 0x53, 0x09, /* NIST PIV Cert */
  39475. 0x70, 0x02, /* Certificate */
  39476. 0x30, 0x00,
  39477. 0x71, 0x01, 0x04, /* Cert Info */
  39478. 0xFE, 0x00, /* Error Detection */
  39479. };
  39480. XMEMSET(&piv, 0, sizeof(piv));
  39481. /* Test with Identiv 0x0A, 0x0B and 0x0C markers */
  39482. ret = wc_ParseCertPIV(&piv, pivCertIdentiv, sizeof(pivCertIdentiv));
  39483. if (ret != 0) {
  39484. return -14000;
  39485. }
  39486. if (!piv.isIdentiv) {
  39487. return -14001;
  39488. }
  39489. if ((piv.cert == NULL) || (piv.certSz != 2)) {
  39490. return -14002;
  39491. }
  39492. if ((piv.certErrDet == NULL) || (piv.certErrDetSz != 0)) {
  39493. return -14003;
  39494. }
  39495. if ((piv.compression != ASN_PIV_CERT_INFO_GZIP)) {
  39496. return -14004;
  39497. }
  39498. if (!piv.isX509) {
  39499. return -14005;
  39500. }
  39501. if ((piv.nonce == NULL) || (piv.nonceSz != 1)) {
  39502. return -14006;
  39503. }
  39504. if ((piv.signedNonce == NULL) || (piv.signedNonceSz != 1)) {
  39505. return -14007;
  39506. }
  39507. XMEMSET(&piv, 0, sizeof(piv));
  39508. /* Test with NIST PIV format */
  39509. ret = wc_ParseCertPIV(&piv, pivCert, sizeof(pivCert));
  39510. if (ret != 0) {
  39511. return -14010;
  39512. }
  39513. if (piv.isIdentiv) {
  39514. return -14011;
  39515. }
  39516. if ((piv.cert == NULL) || (piv.certSz != 2)) {
  39517. return -14012;
  39518. }
  39519. if ((piv.certErrDet == NULL) || (piv.certErrDetSz != 0)) {
  39520. return -14013;
  39521. }
  39522. if ((piv.compression != 0)) {
  39523. return -14014;
  39524. }
  39525. if (!piv.isX509) {
  39526. return -14015;
  39527. }
  39528. return ret;
  39529. }
  39530. #endif /* WOLFSSL_CERT_PIV */
  39531. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  39532. static time_t time_cb(time_t* t)
  39533. {
  39534. if (t != NULL) {
  39535. *t = 99;
  39536. }
  39537. return 99;
  39538. }
  39539. WOLFSSL_TEST_SUBROUTINE int time_test(void)
  39540. {
  39541. time_t t;
  39542. if (wc_SetTimeCb(time_cb) != 0)
  39543. return -15000;
  39544. t = wc_Time(NULL);
  39545. if (t != 99)
  39546. return -15001;
  39547. if (wc_GetTime(&t, sizeof(time_t)) != 0)
  39548. return -15002;
  39549. if (t != 99)
  39550. return -15003;
  39551. if (wc_SetTimeCb(NULL) != 0)
  39552. return -15004;
  39553. return 0;
  39554. }
  39555. #endif
  39556. #ifdef WOLFSSL_AES_SIV
  39557. typedef struct {
  39558. const byte key[33];
  39559. word32 keySz;
  39560. const byte nonce[49];
  39561. word32 nonceSz;
  39562. const byte assoc[81];
  39563. word32 assocSz;
  39564. const byte plaintext[83];
  39565. word32 plaintextSz;
  39566. const byte siv[AES_BLOCK_SIZE+1];
  39567. const byte ciphertext[82];
  39568. word32 ciphertextSz;
  39569. } AesSivTestVector;
  39570. #define AES_SIV_TEST_VECTORS 7
  39571. WOLFSSL_TEST_SUBROUTINE int aes_siv_test(void)
  39572. {
  39573. /* These test vectors come from chrony 4.1's SIV unit tests. */
  39574. WOLFSSL_SMALL_STACK_STATIC const AesSivTestVector testVectors[AES_SIV_TEST_VECTORS] = {
  39575. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  39576. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  39577. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  39578. "", 0,
  39579. "", 0,
  39580. "\x22\x3e\xb5\x94\xe0\xe0\x25\x4b\x00\x25\x8e\x21\x9a\x1c\xa4\x21",
  39581. "", 0
  39582. },
  39583. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  39584. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  39585. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  39586. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  39587. "", 0,
  39588. "\xd7\x20\x19\x89\xc6\xdb\xc6\xd6\x61\xfc\x62\xbc\x86\x5e\xee\xef",
  39589. "", 0
  39590. },
  39591. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  39592. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  39593. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  39594. "", 0,
  39595. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  39596. "\xb6\xc1\x60\xe9\xc2\xfd\x2a\xe8\xde\xc5\x36\x8b\x2a\x33\xed\xe1",
  39597. "\x14\xff\xb3\x97\x34\x5c\xcb\xe4\x4a\xa4\xde\xac\xd9\x36\x90\x46", 16
  39598. },
  39599. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  39600. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  39601. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e", 15,
  39602. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c", 15,
  39603. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4", 15,
  39604. "\x03\x8c\x41\x51\xba\x7a\x8f\x77\x6e\x56\x31\x99\x42\x0b\xc7\x03",
  39605. "\xe7\x6c\x67\xc9\xda\xb7\x0d\x5b\x44\x06\x26\x5a\xd0\xd2\x3b", 15
  39606. },
  39607. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  39608. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  39609. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  39610. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  39611. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4\xa7", 16,
  39612. "\x5c\x05\x23\x65\xf4\x57\x0a\xa0\xfb\x38\x3e\xce\x9b\x75\x85\xeb",
  39613. "\x68\x85\x19\x36\x0c\x7c\x48\x11\x40\xcb\x9b\x57\x9a\x0e\x65\x32", 16
  39614. },
  39615. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  39616. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  39617. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
  39618. "\xd5", 17,
  39619. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b"
  39620. "\xa0", 17,
  39621. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4\xa7"
  39622. "\x08", 17,
  39623. "\xaf\x58\x4b\xe7\x82\x1e\x96\x19\x29\x91\x25\xe0\xdd\x80\x3b\x49",
  39624. "\xa5\x11\xcd\xb6\x08\xf3\x76\xa0\xb6\xfa\x15\x82\xf3\x95\xe1\xeb"
  39625. "\xbd", 17
  39626. },
  39627. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  39628. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  39629. "\xb0\x5a\x1b\xc7\x56\xe7\xb6\x2c\xb4\x85\xe5\x56\xa5\x28\xc0\x6c"
  39630. "\x2f\x3b\x0b\x9d\x1a\x0c\xdf\x69\x47\xe0\xcc\xc0\x87\xaa\x5c\x09"
  39631. "\x98\x48\x8d\x6a\x8e\x1e\x05\xd7\x8b\x68\x74\x83\xb5\x1d\xf1\x2c", 48,
  39632. "\xe5\x8b\xd2\x6a\x30\xc5\xc5\x61\xcc\xbd\x7c\x27\xbf\xfe\xf9\x06"
  39633. "\x00\x5b\xd7\xfc\x11\x0b\xcf\x16\x61\xef\xac\x05\xa7\xaf\xec\x27"
  39634. "\x41\xc8\x5e\x9e\x0d\xf9\x2f\xaf\x20\x79\x17\xe5\x17\x91\x2a\x27"
  39635. "\x34\x1c\xbc\xaf\xeb\xef\x7f\x52\xe7\x1e\x4c\x2a\xca\xbd\x2b\xbe"
  39636. "\x34\xd6\xfb\x69\xd3\x3e\x49\x59\x60\xb4\x26\xc9\xb8\xce\xba", 79,
  39637. "\x6c\xe7\xcf\x7e\xab\x7b\xa0\xe1\xa7\x22\xcb\x88\xde\x5e\x42\xd2"
  39638. "\xec\x79\xe0\xa2\xcf\x5f\x0f\x6f\x6b\x89\x57\xcd\xae\x17\xd4\xc2"
  39639. "\xf3\x1b\xa2\xa8\x13\x78\x23\x2f\x83\xa8\xd4\x0c\xc0\xd2\xf3\x99"
  39640. "\xae\x81\xa1\xca\x5b\x5f\x45\xa6\x6f\x0c\x8a\xf3\xd4\x67\x40\x81"
  39641. "\x26\xe2\x01\x86\xe8\x5a\xd5\xf8\x58\x80\x9f\x56\xaa\x76\x96\xbf"
  39642. "\x31", 81,
  39643. "\x9a\x06\x33\xe0\xee\x00\x6a\x9b\xc8\x20\xd5\xe2\xc2\xed\xb5\x75",
  39644. "\xfa\x9e\x42\x2a\x31\x6b\xda\xca\xaa\x7d\x31\x8b\x84\x7a\xb8\xd7"
  39645. "\x8a\x81\x25\x64\xed\x41\x9b\xa9\x77\x10\xbd\x05\x0c\x4e\xc5\x31"
  39646. "\x0c\xa2\x86\xec\x8a\x94\xc8\x24\x23\x3c\x13\xee\xa5\x51\xc9\xdf"
  39647. "\x48\xc9\x55\xc5\x2f\x40\x73\x3f\x98\xbb\x8d\x69\x78\x46\x64\x17"
  39648. "\x8d\x49\x2f\x14\x62\xa4\x7c\x2a\x57\x38\x87\xce\xc6\x72\xd3\x5c"
  39649. "\xa1", 81
  39650. }};
  39651. int i;
  39652. byte computedCiphertext[82];
  39653. byte computedPlaintext[82];
  39654. byte siv[AES_BLOCK_SIZE];
  39655. int rc = 0;
  39656. for (i = 0; i < AES_SIV_TEST_VECTORS; ++i) {
  39657. rc = wc_AesSivEncrypt(testVectors[i].key, testVectors[i].keySz,
  39658. testVectors[i].assoc, testVectors[i].assocSz,
  39659. testVectors[i].nonce, testVectors[i].nonceSz,
  39660. testVectors[i].plaintext,
  39661. testVectors[i].plaintextSz, siv,
  39662. computedCiphertext);
  39663. if (rc != 0) {
  39664. return -16000;
  39665. }
  39666. rc = XMEMCMP(siv, testVectors[i].siv, AES_BLOCK_SIZE);
  39667. if (rc != 0) {
  39668. return -16001;
  39669. }
  39670. rc = XMEMCMP(computedCiphertext, testVectors[i].ciphertext,
  39671. testVectors[i].ciphertextSz);
  39672. if (rc != 0) {
  39673. return -16002;
  39674. }
  39675. rc = wc_AesSivDecrypt(testVectors[i].key, testVectors[i].keySz,
  39676. testVectors[i].assoc, testVectors[i].assocSz,
  39677. testVectors[i].nonce, testVectors[i].nonceSz,
  39678. computedCiphertext, testVectors[i].plaintextSz,
  39679. siv, computedPlaintext);
  39680. if (rc != 0) {
  39681. return -16003;
  39682. }
  39683. rc = XMEMCMP(computedPlaintext, testVectors[i].plaintext,
  39684. testVectors[i].plaintextSz);
  39685. if (rc != 0) {
  39686. return -16004;
  39687. }
  39688. }
  39689. return 0;
  39690. }
  39691. #endif
  39692. #undef ERROR_OUT
  39693. #else
  39694. #ifndef NO_MAIN_DRIVER
  39695. int main(void) { return 0; }
  39696. #endif
  39697. #endif /* NO_CRYPT_TEST */