test.c 1.5 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931349323493334934349353493634937349383493934940349413494234943349443494534946349473494834949349503495134952349533495434955349563495734958349593496034961349623496334964349653496634967349683496934970349713497234973349743497534976349773497834979349803498134982349833498434985349863498734988349893499034991349923499334994349953499634997349983499935000350013500235003350043500535006350073500835009350103501135012350133501435015350163501735018350193502035021350223502335024350253502635027350283502935030350313503235033350343503535036350373503835039350403504135042350433504435045350463504735048350493505035051350523505335054350553505635057350583505935060350613506235063350643506535066350673506835069350703507135072350733507435075350763507735078350793508035081350823508335084350853508635087350883508935090350913509235093350943509535096350973509835099351003510135102351033510435105351063510735108351093511035111351123511335114351153511635117351183511935120351213512235123351243512535126351273512835129351303513135132351333513435135351363513735138351393514035141351423514335144351453514635147351483514935150351513515235153351543515535156351573515835159351603516135162351633516435165351663516735168351693517035171351723517335174351753517635177351783517935180351813518235183351843518535186351873518835189351903519135192351933519435195351963519735198351993520035201352023520335204352053520635207352083520935210352113521235213352143521535216352173521835219352203522135222352233522435225352263522735228352293523035231352323523335234352353523635237352383523935240352413524235243352443524535246352473524835249352503525135252352533525435255352563525735258352593526035261352623526335264352653526635267352683526935270352713527235273352743527535276352773527835279352803528135282352833528435285352863528735288352893529035291352923529335294352953529635297352983529935300353013530235303353043530535306353073530835309353103531135312353133531435315353163531735318353193532035321353223532335324353253532635327353283532935330353313533235333353343533535336353373533835339353403534135342353433534435345353463534735348353493535035351353523535335354353553535635357353583535935360353613536235363353643536535366353673536835369353703537135372353733537435375353763537735378353793538035381353823538335384353853538635387353883538935390353913539235393353943539535396353973539835399354003540135402354033540435405354063540735408354093541035411354123541335414354153541635417354183541935420354213542235423354243542535426354273542835429354303543135432354333543435435354363543735438354393544035441354423544335444354453544635447354483544935450354513545235453354543545535456354573545835459354603546135462354633546435465354663546735468354693547035471354723547335474354753547635477354783547935480354813548235483354843548535486354873548835489354903549135492354933549435495354963549735498354993550035501355023550335504355053550635507355083550935510355113551235513355143551535516355173551835519355203552135522355233552435525355263552735528355293553035531355323553335534355353553635537355383553935540355413554235543355443554535546355473554835549355503555135552355533555435555355563555735558355593556035561355623556335564355653556635567355683556935570355713557235573355743557535576355773557835579355803558135582355833558435585355863558735588355893559035591355923559335594355953559635597355983559935600356013560235603356043560535606356073560835609356103561135612356133561435615356163561735618356193562035621356223562335624356253562635627356283562935630356313563235633356343563535636356373563835639356403564135642356433564435645356463564735648356493565035651356523565335654356553565635657356583565935660356613566235663356643566535666356673566835669356703567135672356733567435675356763567735678356793568035681356823568335684356853568635687356883568935690356913569235693356943569535696356973569835699357003570135702357033570435705357063570735708357093571035711357123571335714357153571635717357183571935720357213572235723357243572535726357273572835729357303573135732357333573435735357363573735738357393574035741357423574335744357453574635747357483574935750357513575235753357543575535756357573575835759357603576135762357633576435765357663576735768357693577035771357723577335774357753577635777357783577935780357813578235783357843578535786357873578835789357903579135792357933579435795357963579735798357993580035801358023580335804358053580635807358083580935810358113581235813358143581535816358173581835819358203582135822358233582435825358263582735828358293583035831358323583335834358353583635837358383583935840358413584235843358443584535846358473584835849358503585135852358533585435855358563585735858358593586035861358623586335864358653586635867358683586935870358713587235873358743587535876358773587835879358803588135882358833588435885358863588735888358893589035891358923589335894358953589635897358983589935900359013590235903359043590535906359073590835909359103591135912359133591435915359163591735918359193592035921359223592335924359253592635927359283592935930359313593235933359343593535936359373593835939359403594135942359433594435945359463594735948359493595035951359523595335954359553595635957359583595935960359613596235963359643596535966359673596835969359703597135972359733597435975359763597735978359793598035981359823598335984359853598635987359883598935990359913599235993359943599535996359973599835999360003600136002360033600436005360063600736008360093601036011360123601336014360153601636017360183601936020360213602236023360243602536026360273602836029360303603136032360333603436035360363603736038360393604036041360423604336044360453604636047360483604936050360513605236053360543605536056360573605836059360603606136062360633606436065360663606736068360693607036071360723607336074360753607636077360783607936080360813608236083360843608536086360873608836089360903609136092360933609436095360963609736098360993610036101361023610336104361053610636107361083610936110361113611236113361143611536116361173611836119361203612136122361233612436125361263612736128361293613036131361323613336134361353613636137361383613936140361413614236143361443614536146361473614836149361503615136152361533615436155361563615736158361593616036161361623616336164361653616636167361683616936170361713617236173361743617536176361773617836179361803618136182361833618436185361863618736188361893619036191361923619336194361953619636197361983619936200362013620236203362043620536206362073620836209362103621136212362133621436215362163621736218362193622036221362223622336224362253622636227362283622936230362313623236233362343623536236362373623836239362403624136242362433624436245362463624736248362493625036251362523625336254362553625636257362583625936260362613626236263362643626536266362673626836269362703627136272362733627436275362763627736278362793628036281362823628336284362853628636287362883628936290362913629236293362943629536296362973629836299363003630136302363033630436305363063630736308363093631036311363123631336314363153631636317363183631936320363213632236323363243632536326363273632836329363303633136332363333633436335363363633736338363393634036341363423634336344363453634636347363483634936350363513635236353363543635536356363573635836359363603636136362363633636436365363663636736368363693637036371363723637336374363753637636377363783637936380363813638236383363843638536386363873638836389363903639136392363933639436395363963639736398363993640036401364023640336404364053640636407364083640936410364113641236413364143641536416364173641836419364203642136422364233642436425364263642736428364293643036431364323643336434364353643636437364383643936440364413644236443364443644536446364473644836449364503645136452364533645436455364563645736458364593646036461364623646336464364653646636467364683646936470364713647236473364743647536476364773647836479364803648136482364833648436485364863648736488364893649036491364923649336494364953649636497364983649936500365013650236503365043650536506365073650836509365103651136512365133651436515365163651736518365193652036521365223652336524365253652636527365283652936530365313653236533365343653536536365373653836539365403654136542365433654436545365463654736548365493655036551365523655336554365553655636557365583655936560365613656236563365643656536566365673656836569365703657136572365733657436575365763657736578365793658036581365823658336584365853658636587365883658936590365913659236593365943659536596365973659836599366003660136602366033660436605366063660736608366093661036611366123661336614366153661636617366183661936620366213662236623366243662536626366273662836629366303663136632366333663436635366363663736638366393664036641366423664336644366453664636647366483664936650366513665236653366543665536656366573665836659366603666136662366633666436665366663666736668366693667036671366723667336674366753667636677366783667936680366813668236683366843668536686366873668836689366903669136692366933669436695366963669736698366993670036701367023670336704367053670636707367083670936710367113671236713367143671536716367173671836719367203672136722367233672436725367263672736728367293673036731367323673336734367353673636737367383673936740367413674236743367443674536746367473674836749367503675136752367533675436755367563675736758367593676036761367623676336764367653676636767367683676936770367713677236773367743677536776367773677836779367803678136782367833678436785367863678736788367893679036791367923679336794367953679636797367983679936800368013680236803368043680536806368073680836809368103681136812368133681436815368163681736818368193682036821368223682336824368253682636827368283682936830368313683236833368343683536836368373683836839368403684136842368433684436845368463684736848368493685036851368523685336854368553685636857368583685936860368613686236863368643686536866368673686836869368703687136872368733687436875368763687736878368793688036881368823688336884368853688636887368883688936890368913689236893368943689536896368973689836899369003690136902369033690436905369063690736908369093691036911369123691336914369153691636917369183691936920369213692236923369243692536926369273692836929369303693136932369333693436935369363693736938369393694036941369423694336944369453694636947369483694936950369513695236953369543695536956369573695836959369603696136962369633696436965369663696736968369693697036971369723697336974369753697636977369783697936980369813698236983369843698536986369873698836989369903699136992369933699436995369963699736998369993700037001370023700337004370053700637007370083700937010370113701237013370143701537016370173701837019370203702137022370233702437025370263702737028370293703037031370323703337034370353703637037370383703937040370413704237043370443704537046370473704837049370503705137052370533705437055370563705737058370593706037061370623706337064370653706637067370683706937070370713707237073370743707537076370773707837079370803708137082370833708437085370863708737088370893709037091370923709337094370953709637097370983709937100371013710237103371043710537106371073710837109371103711137112371133711437115371163711737118371193712037121371223712337124371253712637127371283712937130371313713237133371343713537136371373713837139371403714137142371433714437145371463714737148371493715037151371523715337154371553715637157371583715937160371613716237163371643716537166371673716837169371703717137172371733717437175371763717737178371793718037181371823718337184371853718637187371883718937190371913719237193371943719537196371973719837199372003720137202372033720437205372063720737208372093721037211372123721337214372153721637217372183721937220372213722237223372243722537226372273722837229372303723137232372333723437235372363723737238372393724037241372423724337244372453724637247372483724937250372513725237253372543725537256372573725837259372603726137262372633726437265372663726737268372693727037271372723727337274372753727637277372783727937280372813728237283372843728537286372873728837289372903729137292372933729437295372963729737298372993730037301373023730337304373053730637307373083730937310373113731237313373143731537316373173731837319373203732137322373233732437325373263732737328373293733037331373323733337334373353733637337373383733937340373413734237343373443734537346373473734837349373503735137352373533735437355373563735737358373593736037361373623736337364373653736637367373683736937370373713737237373373743737537376373773737837379373803738137382373833738437385373863738737388373893739037391373923739337394373953739637397373983739937400374013740237403374043740537406374073740837409374103741137412374133741437415374163741737418374193742037421374223742337424374253742637427374283742937430374313743237433374343743537436374373743837439374403744137442374433744437445374463744737448374493745037451374523745337454374553745637457374583745937460374613746237463374643746537466374673746837469374703747137472374733747437475374763747737478374793748037481374823748337484374853748637487374883748937490374913749237493374943749537496374973749837499375003750137502375033750437505375063750737508375093751037511375123751337514375153751637517375183751937520375213752237523375243752537526375273752837529375303753137532375333753437535375363753737538375393754037541375423754337544375453754637547375483754937550375513755237553375543755537556375573755837559375603756137562375633756437565375663756737568375693757037571375723757337574375753757637577375783757937580375813758237583375843758537586375873758837589375903759137592375933759437595375963759737598375993760037601376023760337604376053760637607376083760937610376113761237613376143761537616376173761837619376203762137622376233762437625376263762737628376293763037631376323763337634376353763637637376383763937640376413764237643376443764537646376473764837649376503765137652376533765437655376563765737658376593766037661376623766337664376653766637667376683766937670376713767237673376743767537676376773767837679376803768137682376833768437685376863768737688376893769037691376923769337694376953769637697376983769937700377013770237703377043770537706377073770837709377103771137712377133771437715377163771737718377193772037721377223772337724377253772637727377283772937730377313773237733377343773537736377373773837739377403774137742377433774437745377463774737748377493775037751377523775337754377553775637757377583775937760377613776237763377643776537766377673776837769377703777137772377733777437775377763777737778377793778037781377823778337784377853778637787377883778937790377913779237793377943779537796377973779837799378003780137802378033780437805378063780737808378093781037811378123781337814378153781637817378183781937820378213782237823378243782537826378273782837829378303783137832378333783437835378363783737838378393784037841378423784337844378453784637847378483784937850378513785237853378543785537856378573785837859378603786137862378633786437865378663786737868378693787037871378723787337874378753787637877378783787937880378813788237883378843788537886378873788837889378903789137892378933789437895378963789737898378993790037901379023790337904379053790637907379083790937910379113791237913379143791537916379173791837919379203792137922379233792437925379263792737928379293793037931379323793337934379353793637937379383793937940379413794237943379443794537946379473794837949379503795137952379533795437955379563795737958379593796037961379623796337964379653796637967379683796937970379713797237973379743797537976379773797837979379803798137982379833798437985379863798737988379893799037991379923799337994379953799637997379983799938000380013800238003380043800538006380073800838009380103801138012380133801438015380163801738018380193802038021380223802338024380253802638027380283802938030380313803238033380343803538036380373803838039380403804138042380433804438045380463804738048380493805038051380523805338054380553805638057380583805938060380613806238063380643806538066380673806838069380703807138072380733807438075380763807738078380793808038081380823808338084380853808638087380883808938090380913809238093380943809538096380973809838099381003810138102381033810438105381063810738108381093811038111381123811338114381153811638117381183811938120381213812238123381243812538126381273812838129381303813138132381333813438135381363813738138381393814038141381423814338144381453814638147381483814938150381513815238153381543815538156381573815838159381603816138162381633816438165381663816738168381693817038171381723817338174381753817638177381783817938180381813818238183381843818538186381873818838189381903819138192381933819438195381963819738198381993820038201382023820338204382053820638207382083820938210382113821238213382143821538216382173821838219382203822138222382233822438225382263822738228382293823038231382323823338234382353823638237382383823938240382413824238243382443824538246382473824838249382503825138252382533825438255382563825738258382593826038261382623826338264382653826638267382683826938270382713827238273382743827538276382773827838279382803828138282382833828438285382863828738288382893829038291382923829338294382953829638297382983829938300383013830238303383043830538306383073830838309383103831138312383133831438315383163831738318383193832038321383223832338324383253832638327383283832938330383313833238333383343833538336383373833838339383403834138342383433834438345383463834738348383493835038351383523835338354383553835638357383583835938360383613836238363383643836538366383673836838369383703837138372383733837438375383763837738378383793838038381383823838338384383853838638387383883838938390383913839238393383943839538396383973839838399384003840138402384033840438405384063840738408384093841038411384123841338414384153841638417384183841938420384213842238423384243842538426384273842838429384303843138432384333843438435384363843738438384393844038441384423844338444384453844638447384483844938450384513845238453384543845538456384573845838459384603846138462384633846438465384663846738468384693847038471384723847338474384753847638477384783847938480384813848238483384843848538486384873848838489384903849138492384933849438495384963849738498384993850038501385023850338504385053850638507385083850938510385113851238513385143851538516385173851838519385203852138522385233852438525385263852738528385293853038531385323853338534385353853638537385383853938540385413854238543385443854538546385473854838549385503855138552385533855438555385563855738558385593856038561385623856338564385653856638567385683856938570385713857238573385743857538576385773857838579385803858138582385833858438585385863858738588385893859038591385923859338594385953859638597385983859938600386013860238603386043860538606386073860838609386103861138612386133861438615386163861738618386193862038621386223862338624386253862638627386283862938630386313863238633386343863538636386373863838639386403864138642386433864438645386463864738648386493865038651386523865338654386553865638657386583865938660386613866238663386643866538666386673866838669386703867138672386733867438675386763867738678386793868038681386823868338684386853868638687386883868938690386913869238693386943869538696386973869838699387003870138702387033870438705387063870738708387093871038711387123871338714387153871638717387183871938720387213872238723387243872538726387273872838729387303873138732387333873438735387363873738738387393874038741387423874338744387453874638747387483874938750387513875238753387543875538756387573875838759387603876138762387633876438765387663876738768387693877038771387723877338774387753877638777387783877938780387813878238783387843878538786387873878838789387903879138792387933879438795387963879738798387993880038801388023880338804388053880638807388083880938810388113881238813388143881538816388173881838819388203882138822388233882438825388263882738828388293883038831388323883338834388353883638837388383883938840388413884238843388443884538846388473884838849388503885138852388533885438855388563885738858388593886038861388623886338864388653886638867388683886938870388713887238873388743887538876388773887838879388803888138882388833888438885388863888738888388893889038891388923889338894388953889638897388983889938900389013890238903389043890538906389073890838909389103891138912389133891438915389163891738918389193892038921389223892338924389253892638927389283892938930389313893238933389343893538936389373893838939389403894138942389433894438945389463894738948389493895038951389523895338954389553895638957389583895938960389613896238963389643896538966389673896838969389703897138972389733897438975389763897738978389793898038981389823898338984389853898638987389883898938990389913899238993389943899538996389973899838999390003900139002390033900439005390063900739008390093901039011390123901339014390153901639017390183901939020390213902239023390243902539026390273902839029390303903139032390333903439035390363903739038390393904039041390423904339044390453904639047390483904939050390513905239053390543905539056390573905839059390603906139062390633906439065390663906739068390693907039071390723907339074390753907639077390783907939080390813908239083390843908539086390873908839089390903909139092390933909439095390963909739098390993910039101391023910339104391053910639107391083910939110391113911239113391143911539116391173911839119391203912139122391233912439125391263912739128391293913039131391323913339134391353913639137391383913939140391413914239143391443914539146391473914839149391503915139152391533915439155391563915739158391593916039161391623916339164391653916639167391683916939170391713917239173391743917539176391773917839179391803918139182391833918439185391863918739188391893919039191391923919339194391953919639197391983919939200392013920239203392043920539206392073920839209392103921139212392133921439215392163921739218392193922039221392223922339224392253922639227392283922939230392313923239233392343923539236392373923839239392403924139242392433924439245392463924739248392493925039251392523925339254392553925639257392583925939260392613926239263392643926539266392673926839269392703927139272392733927439275392763927739278392793928039281392823928339284392853928639287392883928939290392913929239293392943929539296392973929839299393003930139302393033930439305393063930739308393093931039311393123931339314393153931639317393183931939320393213932239323393243932539326393273932839329393303933139332393333933439335393363933739338393393934039341393423934339344393453934639347393483934939350393513935239353393543935539356393573935839359393603936139362393633936439365393663936739368393693937039371393723937339374393753937639377393783937939380393813938239383393843938539386393873938839389393903939139392393933939439395393963939739398393993940039401394023940339404394053940639407394083940939410394113941239413394143941539416394173941839419394203942139422394233942439425394263942739428394293943039431394323943339434394353943639437394383943939440394413944239443394443944539446394473944839449394503945139452394533945439455394563945739458394593946039461394623946339464394653946639467394683946939470394713947239473394743947539476394773947839479394803948139482394833948439485394863948739488394893949039491394923949339494394953949639497394983949939500395013950239503395043950539506395073950839509395103951139512395133951439515395163951739518395193952039521395223952339524395253952639527395283952939530395313953239533395343953539536395373953839539395403954139542395433954439545395463954739548395493955039551395523955339554395553955639557395583955939560395613956239563395643956539566395673956839569395703957139572395733957439575395763957739578395793958039581395823958339584395853958639587395883958939590395913959239593395943959539596395973959839599396003960139602396033960439605396063960739608396093961039611396123961339614396153961639617396183961939620396213962239623396243962539626396273962839629396303963139632396333963439635396363963739638396393964039641396423964339644396453964639647396483964939650396513965239653396543965539656396573965839659396603966139662396633966439665396663966739668396693967039671396723967339674396753967639677396783967939680396813968239683396843968539686396873968839689396903969139692396933969439695396963969739698396993970039701397023970339704397053970639707397083970939710397113971239713397143971539716397173971839719397203972139722397233972439725397263972739728397293973039731397323973339734397353973639737397383973939740397413974239743397443974539746397473974839749397503975139752397533975439755397563975739758397593976039761397623976339764397653976639767397683976939770397713977239773397743977539776397773977839779397803978139782397833978439785397863978739788397893979039791397923979339794397953979639797397983979939800398013980239803398043980539806398073980839809398103981139812398133981439815398163981739818398193982039821398223982339824398253982639827398283982939830398313983239833398343983539836398373983839839398403984139842398433984439845398463984739848398493985039851398523985339854398553985639857398583985939860398613986239863398643986539866398673986839869398703987139872398733987439875398763987739878398793988039881398823988339884398853988639887398883988939890398913989239893398943989539896398973989839899399003990139902399033990439905399063990739908399093991039911399123991339914399153991639917399183991939920399213992239923399243992539926399273992839929399303993139932399333993439935399363993739938399393994039941399423994339944399453994639947399483994939950399513995239953399543995539956399573995839959399603996139962399633996439965399663996739968399693997039971399723997339974399753997639977399783997939980399813998239983399843998539986399873998839989399903999139992399933999439995399963999739998399994000040001400024000340004400054000640007400084000940010400114001240013400144001540016400174001840019400204002140022400234002440025400264002740028400294003040031400324003340034400354003640037400384003940040400414004240043400444004540046400474004840049400504005140052400534005440055400564005740058400594006040061400624006340064400654006640067400684006940070400714007240073400744007540076400774007840079400804008140082400834008440085400864008740088400894009040091400924009340094400954009640097400984009940100401014010240103401044010540106401074010840109401104011140112401134011440115401164011740118401194012040121401224012340124401254012640127401284012940130401314013240133401344013540136401374013840139401404014140142401434014440145401464014740148401494015040151401524015340154401554015640157401584015940160401614016240163401644016540166401674016840169401704017140172401734017440175401764017740178401794018040181401824018340184401854018640187401884018940190401914019240193401944019540196401974019840199402004020140202402034020440205402064020740208402094021040211402124021340214402154021640217402184021940220402214022240223402244022540226402274022840229402304023140232402334023440235402364023740238402394024040241402424024340244402454024640247402484024940250402514025240253402544025540256402574025840259402604026140262402634026440265402664026740268402694027040271402724027340274402754027640277402784027940280402814028240283402844028540286402874028840289402904029140292402934029440295402964029740298402994030040301403024030340304403054030640307403084030940310403114031240313403144031540316403174031840319403204032140322403234032440325403264032740328403294033040331403324033340334403354033640337403384033940340403414034240343403444034540346403474034840349403504035140352403534035440355403564035740358403594036040361403624036340364403654036640367403684036940370403714037240373403744037540376403774037840379403804038140382403834038440385403864038740388403894039040391403924039340394403954039640397403984039940400404014040240403404044040540406404074040840409404104041140412404134041440415404164041740418404194042040421404224042340424404254042640427404284042940430404314043240433404344043540436404374043840439404404044140442404434044440445404464044740448404494045040451404524045340454404554045640457404584045940460404614046240463404644046540466404674046840469404704047140472404734047440475404764047740478404794048040481404824048340484404854048640487404884048940490404914049240493404944049540496404974049840499405004050140502405034050440505405064050740508405094051040511405124051340514405154051640517405184051940520405214052240523405244052540526405274052840529405304053140532405334053440535405364053740538405394054040541405424054340544405454054640547405484054940550405514055240553405544055540556405574055840559405604056140562405634056440565405664056740568405694057040571405724057340574405754057640577405784057940580405814058240583405844058540586405874058840589405904059140592405934059440595405964059740598405994060040601406024060340604406054060640607406084060940610406114061240613406144061540616406174061840619406204062140622406234062440625406264062740628406294063040631406324063340634406354063640637406384063940640406414064240643406444064540646406474064840649406504065140652406534065440655406564065740658406594066040661406624066340664406654066640667406684066940670406714067240673406744067540676406774067840679406804068140682406834068440685406864068740688406894069040691406924069340694406954069640697406984069940700407014070240703407044070540706407074070840709407104071140712407134071440715407164071740718407194072040721407224072340724407254072640727407284072940730407314073240733407344073540736407374073840739407404074140742407434074440745407464074740748407494075040751407524075340754407554075640757407584075940760407614076240763407644076540766407674076840769407704077140772407734077440775407764077740778407794078040781407824078340784407854078640787407884078940790407914079240793407944079540796407974079840799408004080140802408034080440805408064080740808408094081040811408124081340814408154081640817408184081940820408214082240823408244082540826408274082840829408304083140832408334083440835408364083740838408394084040841408424084340844408454084640847408484084940850408514085240853408544085540856408574085840859408604086140862408634086440865408664086740868408694087040871408724087340874408754087640877408784087940880408814088240883408844088540886408874088840889408904089140892408934089440895408964089740898408994090040901409024090340904409054090640907409084090940910409114091240913409144091540916409174091840919409204092140922409234092440925409264092740928409294093040931409324093340934409354093640937409384093940940409414094240943409444094540946409474094840949409504095140952409534095440955409564095740958409594096040961409624096340964409654096640967409684096940970409714097240973409744097540976409774097840979409804098140982409834098440985409864098740988409894099040991409924099340994409954099640997409984099941000410014100241003410044100541006410074100841009410104101141012410134101441015410164101741018410194102041021410224102341024410254102641027410284102941030410314103241033410344103541036410374103841039410404104141042410434104441045410464104741048410494105041051410524105341054410554105641057410584105941060410614106241063410644106541066410674106841069410704107141072410734107441075410764107741078410794108041081410824108341084410854108641087410884108941090410914109241093410944109541096410974109841099411004110141102411034110441105411064110741108411094111041111411124111341114411154111641117411184111941120411214112241123411244112541126411274112841129411304113141132411334113441135411364113741138411394114041141411424114341144411454114641147411484114941150411514115241153411544115541156411574115841159411604116141162411634116441165411664116741168411694117041171411724117341174411754117641177411784117941180411814118241183411844118541186411874118841189411904119141192411934119441195411964119741198411994120041201412024120341204412054120641207412084120941210412114121241213412144121541216412174121841219412204122141222412234122441225412264122741228412294123041231412324123341234412354123641237412384123941240412414124241243412444124541246412474124841249412504125141252412534125441255412564125741258412594126041261412624126341264412654126641267412684126941270412714127241273412744127541276412774127841279412804128141282412834128441285412864128741288412894129041291412924129341294412954129641297412984129941300413014130241303413044130541306413074130841309413104131141312413134131441315413164131741318413194132041321413224132341324413254132641327413284132941330413314133241333413344133541336413374133841339413404134141342413434134441345413464134741348413494135041351413524135341354413554135641357413584135941360413614136241363413644136541366413674136841369413704137141372413734137441375413764137741378413794138041381413824138341384413854138641387413884138941390413914139241393413944139541396413974139841399414004140141402414034140441405414064140741408414094141041411414124141341414414154141641417414184141941420414214142241423414244142541426414274142841429414304143141432414334143441435414364143741438414394144041441414424144341444414454144641447414484144941450414514145241453414544145541456414574145841459414604146141462414634146441465414664146741468414694147041471414724147341474414754147641477414784147941480414814148241483414844148541486414874148841489414904149141492414934149441495414964149741498414994150041501415024150341504415054150641507415084150941510415114151241513415144151541516415174151841519415204152141522415234152441525415264152741528415294153041531415324153341534415354153641537415384153941540415414154241543415444154541546415474154841549415504155141552415534155441555415564155741558415594156041561415624156341564415654156641567415684156941570415714157241573415744157541576415774157841579415804158141582415834158441585415864158741588415894159041591415924159341594415954159641597415984159941600416014160241603416044160541606416074160841609416104161141612416134161441615416164161741618416194162041621416224162341624416254162641627416284162941630416314163241633416344163541636416374163841639416404164141642416434164441645416464164741648416494165041651416524165341654416554165641657416584165941660416614166241663416644166541666416674166841669416704167141672416734167441675416764167741678416794168041681416824168341684416854168641687416884168941690416914169241693416944169541696416974169841699417004170141702417034170441705417064170741708417094171041711417124171341714417154171641717417184171941720417214172241723417244172541726417274172841729417304173141732417334173441735417364173741738417394174041741417424174341744417454174641747417484174941750417514175241753417544175541756417574175841759417604176141762417634176441765417664176741768417694177041771417724177341774417754177641777417784177941780417814178241783417844178541786417874178841789417904179141792417934179441795417964179741798417994180041801418024180341804418054180641807418084180941810418114181241813418144181541816418174181841819418204182141822418234182441825418264182741828418294183041831418324183341834418354183641837418384183941840418414184241843418444184541846418474184841849418504185141852418534185441855418564185741858418594186041861418624186341864418654186641867418684186941870418714187241873418744187541876418774187841879418804188141882418834188441885418864188741888418894189041891418924189341894418954189641897418984189941900419014190241903419044190541906419074190841909419104191141912419134191441915419164191741918419194192041921419224192341924419254192641927419284192941930419314193241933419344193541936419374193841939419404194141942419434194441945419464194741948419494195041951419524195341954419554195641957419584195941960419614196241963419644196541966419674196841969419704197141972419734197441975419764197741978419794198041981419824198341984419854198641987419884198941990419914199241993419944199541996419974199841999420004200142002420034200442005420064200742008420094201042011420124201342014420154201642017420184201942020420214202242023420244202542026420274202842029420304203142032420334203442035420364203742038420394204042041420424204342044420454204642047420484204942050420514205242053420544205542056420574205842059420604206142062420634206442065420664206742068420694207042071420724207342074420754207642077420784207942080420814208242083420844208542086420874208842089420904209142092420934209442095420964209742098420994210042101421024210342104421054210642107421084210942110421114211242113421144211542116421174211842119421204212142122421234212442125421264212742128421294213042131421324213342134421354213642137421384213942140421414214242143421444214542146421474214842149421504215142152421534215442155421564215742158421594216042161421624216342164421654216642167421684216942170421714217242173421744217542176421774217842179421804218142182421834218442185421864218742188421894219042191421924219342194421954219642197421984219942200422014220242203422044220542206422074220842209422104221142212422134221442215422164221742218422194222042221422224222342224422254222642227422284222942230422314223242233422344223542236422374223842239422404224142242422434224442245422464224742248422494225042251422524225342254422554225642257422584225942260422614226242263422644226542266422674226842269422704227142272422734227442275422764227742278422794228042281422824228342284422854228642287422884228942290422914229242293422944229542296422974229842299423004230142302423034230442305423064230742308423094231042311423124231342314423154231642317423184231942320423214232242323423244232542326423274232842329423304233142332423334233442335423364233742338423394234042341423424234342344423454234642347423484234942350423514235242353423544235542356423574235842359423604236142362423634236442365423664236742368423694237042371423724237342374423754237642377423784237942380423814238242383423844238542386423874238842389423904239142392423934239442395423964239742398423994240042401424024240342404424054240642407424084240942410424114241242413424144241542416424174241842419424204242142422424234242442425424264242742428424294243042431424324243342434424354243642437424384243942440424414244242443424444244542446424474244842449424504245142452424534245442455424564245742458424594246042461424624246342464424654246642467424684246942470424714247242473424744247542476424774247842479424804248142482424834248442485424864248742488424894249042491424924249342494424954249642497424984249942500425014250242503425044250542506425074250842509425104251142512425134251442515425164251742518425194252042521425224252342524425254252642527425284252942530425314253242533425344253542536425374253842539425404254142542425434254442545425464254742548425494255042551425524255342554425554255642557425584255942560425614256242563425644256542566425674256842569425704257142572425734257442575425764257742578425794258042581425824258342584425854258642587425884258942590425914259242593425944259542596425974259842599426004260142602426034260442605426064260742608426094261042611426124261342614426154261642617426184261942620426214262242623426244262542626426274262842629426304263142632426334263442635426364263742638426394264042641426424264342644426454264642647426484264942650426514265242653426544265542656426574265842659426604266142662426634266442665426664266742668426694267042671426724267342674426754267642677426784267942680426814268242683426844268542686426874268842689426904269142692426934269442695426964269742698426994270042701427024270342704427054270642707427084270942710427114271242713427144271542716427174271842719427204272142722427234272442725427264272742728427294273042731427324273342734427354273642737427384273942740427414274242743427444274542746427474274842749427504275142752427534275442755427564275742758427594276042761427624276342764427654276642767427684276942770427714277242773427744277542776427774277842779427804278142782427834278442785427864278742788427894279042791427924279342794427954279642797427984279942800428014280242803428044280542806428074280842809428104281142812428134281442815428164281742818428194282042821428224282342824428254282642827428284282942830428314283242833428344283542836428374283842839428404284142842428434284442845428464284742848428494285042851428524285342854428554285642857428584285942860428614286242863428644286542866428674286842869428704287142872428734287442875428764287742878428794288042881428824288342884428854288642887428884288942890428914289242893428944289542896428974289842899429004290142902429034290442905429064290742908429094291042911429124291342914429154291642917429184291942920429214292242923429244292542926429274292842929429304293142932429334293442935429364293742938429394294042941429424294342944429454294642947429484294942950429514295242953429544295542956429574295842959429604296142962429634296442965429664296742968429694297042971429724297342974429754297642977429784297942980429814298242983429844298542986429874298842989429904299142992429934299442995429964299742998429994300043001430024300343004430054300643007430084300943010430114301243013430144301543016430174301843019430204302143022430234302443025430264302743028430294303043031430324303343034430354303643037430384303943040430414304243043430444304543046430474304843049430504305143052430534305443055430564305743058430594306043061430624306343064430654306643067430684306943070430714307243073430744307543076430774307843079430804308143082430834308443085430864308743088430894309043091430924309343094430954309643097430984309943100431014310243103431044310543106431074310843109431104311143112431134311443115431164311743118431194312043121431224312343124431254312643127431284312943130431314313243133431344313543136431374313843139431404314143142431434314443145431464314743148431494315043151431524315343154431554315643157431584315943160431614316243163431644316543166431674316843169431704317143172431734317443175431764317743178431794318043181431824318343184431854318643187431884318943190431914319243193431944319543196431974319843199432004320143202432034320443205432064320743208432094321043211432124321343214432154321643217432184321943220432214322243223432244322543226432274322843229432304323143232432334323443235432364323743238432394324043241432424324343244432454324643247432484324943250432514325243253432544325543256432574325843259432604326143262432634326443265432664326743268432694327043271432724327343274432754327643277432784327943280432814328243283432844328543286432874328843289432904329143292432934329443295432964329743298432994330043301433024330343304433054330643307433084330943310433114331243313433144331543316433174331843319433204332143322433234332443325433264332743328433294333043331433324333343334433354333643337433384333943340433414334243343433444334543346433474334843349433504335143352433534335443355433564335743358433594336043361433624336343364433654336643367433684336943370433714337243373433744337543376433774337843379433804338143382433834338443385433864338743388433894339043391433924339343394433954339643397433984339943400434014340243403434044340543406434074340843409434104341143412434134341443415434164341743418434194342043421434224342343424434254342643427434284342943430434314343243433434344343543436434374343843439434404344143442434434344443445434464344743448434494345043451434524345343454434554345643457434584345943460434614346243463434644346543466434674346843469434704347143472434734347443475434764347743478434794348043481434824348343484434854348643487434884348943490434914349243493434944349543496434974349843499435004350143502435034350443505435064350743508435094351043511435124351343514435154351643517435184351943520435214352243523435244352543526435274352843529435304353143532435334353443535435364353743538435394354043541435424354343544435454354643547435484354943550435514355243553435544355543556435574355843559435604356143562435634356443565435664356743568435694357043571435724357343574435754357643577435784357943580435814358243583435844358543586435874358843589435904359143592435934359443595435964359743598435994360043601436024360343604436054360643607436084360943610436114361243613436144361543616436174361843619436204362143622436234362443625436264362743628436294363043631436324363343634436354363643637436384363943640436414364243643436444364543646436474364843649436504365143652436534365443655436564365743658436594366043661436624366343664436654366643667436684366943670436714367243673436744367543676436774367843679436804368143682436834368443685436864368743688436894369043691436924369343694436954369643697436984369943700437014370243703437044370543706437074370843709437104371143712437134371443715437164371743718437194372043721437224372343724437254372643727437284372943730437314373243733437344373543736437374373843739437404374143742437434374443745437464374743748437494375043751437524375343754437554375643757437584375943760437614376243763437644376543766437674376843769437704377143772437734377443775437764377743778437794378043781437824378343784437854378643787437884378943790437914379243793437944379543796437974379843799438004380143802438034380443805438064380743808438094381043811438124381343814438154381643817438184381943820438214382243823438244382543826438274382843829438304383143832438334383443835438364383743838438394384043841438424384343844438454384643847438484384943850438514385243853438544385543856438574385843859438604386143862438634386443865438664386743868438694387043871438724387343874438754387643877438784387943880438814388243883438844388543886438874388843889438904389143892438934389443895438964389743898438994390043901439024390343904439054390643907439084390943910439114391243913439144391543916439174391843919439204392143922439234392443925439264392743928439294393043931439324393343934439354393643937439384393943940439414394243943439444394543946439474394843949439504395143952439534395443955439564395743958439594396043961439624396343964439654396643967439684396943970439714397243973439744397543976439774397843979439804398143982439834398443985439864398743988439894399043991439924399343994439954399643997439984399944000440014400244003440044400544006440074400844009440104401144012440134401444015440164401744018440194402044021440224402344024440254402644027440284402944030440314403244033440344403544036440374403844039440404404144042440434404444045440464404744048440494405044051440524405344054440554405644057440584405944060440614406244063440644406544066440674406844069440704407144072440734407444075440764407744078440794408044081440824408344084440854408644087440884408944090440914409244093440944409544096440974409844099441004410144102441034410444105441064410744108441094411044111441124411344114441154411644117441184411944120441214412244123441244412544126441274412844129441304413144132441334413444135441364413744138441394414044141441424414344144441454414644147441484414944150441514415244153441544415544156441574415844159441604416144162441634416444165441664416744168441694417044171441724417344174441754417644177441784417944180441814418244183441844418544186441874418844189441904419144192441934419444195441964419744198441994420044201442024420344204442054420644207442084420944210442114421244213442144421544216442174421844219442204422144222442234422444225442264422744228442294423044231442324423344234442354423644237442384423944240442414424244243442444424544246442474424844249442504425144252442534425444255442564425744258442594426044261442624426344264442654426644267442684426944270442714427244273442744427544276442774427844279442804428144282442834428444285442864428744288442894429044291442924429344294442954429644297442984429944300443014430244303443044430544306443074430844309443104431144312443134431444315443164431744318443194432044321443224432344324443254432644327443284432944330443314433244333443344433544336443374433844339443404434144342443434434444345443464434744348443494435044351443524435344354443554435644357443584435944360443614436244363443644436544366443674436844369443704437144372443734437444375443764437744378443794438044381443824438344384443854438644387443884438944390443914439244393443944439544396443974439844399444004440144402444034440444405444064440744408444094441044411444124441344414444154441644417444184441944420444214442244423444244442544426444274442844429444304443144432444334443444435444364443744438444394444044441444424444344444444454444644447444484444944450444514445244453444544445544456444574445844459444604446144462444634446444465444664446744468444694447044471444724447344474444754447644477444784447944480444814448244483444844448544486444874448844489444904449144492444934449444495444964449744498444994450044501445024450344504445054450644507445084450944510445114451244513445144451544516445174451844519445204452144522445234452444525445264452744528445294453044531445324453344534445354453644537445384453944540445414454244543445444454544546445474454844549445504455144552445534455444555445564455744558445594456044561445624456344564445654456644567445684456944570445714457244573445744457544576445774457844579445804458144582445834458444585445864458744588445894459044591445924459344594445954459644597445984459944600446014460244603446044460544606446074460844609446104461144612446134461444615446164461744618446194462044621446224462344624446254462644627446284462944630446314463244633446344463544636446374463844639446404464144642446434464444645446464464744648446494465044651446524465344654446554465644657446584465944660446614466244663446644466544666446674466844669446704467144672446734467444675446764467744678446794468044681446824468344684446854468644687446884468944690446914469244693446944469544696446974469844699447004470144702447034470444705447064470744708447094471044711447124471344714447154471644717447184471944720447214472244723447244472544726447274472844729447304473144732447334473444735447364473744738447394474044741447424474344744447454474644747447484474944750447514475244753447544475544756447574475844759447604476144762447634476444765447664476744768447694477044771447724477344774447754477644777447784477944780447814478244783447844478544786447874478844789447904479144792447934479444795447964479744798447994480044801448024480344804448054480644807448084480944810448114481244813448144481544816448174481844819448204482144822448234482444825448264482744828448294483044831448324483344834448354483644837448384483944840448414484244843448444484544846448474484844849448504485144852448534485444855448564485744858448594486044861448624486344864448654486644867448684486944870448714487244873448744487544876448774487844879448804488144882448834488444885448864488744888448894489044891448924489344894448954489644897448984489944900449014490244903449044490544906449074490844909449104491144912449134491444915449164491744918449194492044921449224492344924449254492644927449284492944930449314493244933449344493544936449374493844939449404494144942449434494444945449464494744948449494495044951449524495344954449554495644957449584495944960449614496244963449644496544966449674496844969449704497144972449734497444975449764497744978449794498044981449824498344984449854498644987449884498944990449914499244993449944499544996449974499844999450004500145002450034500445005450064500745008450094501045011450124501345014450154501645017450184501945020450214502245023450244502545026450274502845029450304503145032450334503445035450364503745038450394504045041450424504345044450454504645047450484504945050450514505245053450544505545056450574505845059450604506145062450634506445065450664506745068450694507045071450724507345074450754507645077450784507945080450814508245083450844508545086450874508845089450904509145092450934509445095450964509745098450994510045101451024510345104451054510645107451084510945110451114511245113451144511545116451174511845119451204512145122451234512445125451264512745128451294513045131451324513345134451354513645137451384513945140451414514245143451444514545146451474514845149451504515145152451534515445155451564515745158451594516045161451624516345164451654516645167451684516945170451714517245173451744517545176451774517845179451804518145182451834518445185451864518745188451894519045191451924519345194451954519645197451984519945200452014520245203452044520545206452074520845209452104521145212452134521445215452164521745218452194522045221452224522345224452254522645227452284522945230452314523245233452344523545236452374523845239452404524145242452434524445245452464524745248452494525045251452524525345254452554525645257452584525945260452614526245263452644526545266452674526845269452704527145272452734527445275452764527745278452794528045281452824528345284452854528645287452884528945290452914529245293452944529545296452974529845299453004530145302453034530445305453064530745308453094531045311453124531345314453154531645317453184531945320453214532245323453244532545326453274532845329453304533145332453334533445335453364533745338453394534045341453424534345344453454534645347453484534945350453514535245353453544535545356453574535845359453604536145362453634536445365453664536745368453694537045371453724537345374453754537645377453784537945380453814538245383453844538545386453874538845389453904539145392453934539445395453964539745398453994540045401454024540345404454054540645407454084540945410454114541245413454144541545416454174541845419454204542145422454234542445425454264542745428454294543045431454324543345434454354543645437454384543945440454414544245443454444544545446454474544845449454504545145452454534545445455454564545745458454594546045461454624546345464454654546645467454684546945470454714547245473454744547545476454774547845479454804548145482454834548445485454864548745488454894549045491454924549345494454954549645497454984549945500455014550245503455044550545506455074550845509455104551145512455134551445515455164551745518455194552045521455224552345524455254552645527455284552945530455314553245533455344553545536455374553845539455404554145542455434554445545455464554745548455494555045551455524555345554455554555645557455584555945560455614556245563455644556545566455674556845569455704557145572455734557445575455764557745578455794558045581455824558345584455854558645587455884558945590455914559245593455944559545596455974559845599456004560145602456034560445605456064560745608456094561045611456124561345614456154561645617456184561945620456214562245623456244562545626456274562845629456304563145632456334563445635456364563745638456394564045641456424564345644456454564645647456484564945650456514565245653456544565545656456574565845659456604566145662456634566445665456664566745668456694567045671456724567345674456754567645677456784567945680456814568245683456844568545686456874568845689456904569145692456934569445695456964569745698456994570045701457024570345704457054570645707457084570945710457114571245713457144571545716457174571845719457204572145722457234572445725457264572745728457294573045731457324573345734457354573645737457384573945740457414574245743457444574545746457474574845749457504575145752457534575445755457564575745758457594576045761457624576345764457654576645767457684576945770457714577245773457744577545776457774577845779457804578145782457834578445785457864578745788457894579045791457924579345794457954579645797457984579945800458014580245803458044580545806458074580845809458104581145812458134581445815458164581745818458194582045821458224582345824
  1. /* test.c
  2. *
  3. * Copyright (C) 2006-2022 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. #elif defined(WOLFSSL_ZEPHYR)
  112. #include <stdio.h>
  113. #define printf printk
  114. #elif defined(MICRIUM)
  115. #include <os.h>
  116. #if (OS_VERSION < 50000)
  117. #include <bsp_ser.h>
  118. void BSP_Ser_Printf (CPU_CHAR* format, ...);
  119. #undef printf
  120. #define printf BSP_Ser_Printf
  121. #else
  122. #include <stdio.h>
  123. #endif
  124. #elif defined(WOLFSSL_PB)
  125. #include <stdarg.h>
  126. int wolfssl_pb_print(const char*, ...);
  127. #undef printf
  128. #define printf wolfssl_pb_print
  129. #elif defined(WOLFSSL_TELIT_M2MB)
  130. #include "wolfssl/wolfcrypt/wc_port.h" /* for m2mb headers */
  131. #include "m2m_log.h" /* for M2M_LOG_INFO - not standard API */
  132. /* remap printf */
  133. #undef printf
  134. #define printf M2M_LOG_INFO
  135. /* OS requires occasional sleep() */
  136. #ifndef TEST_SLEEP_MS
  137. #define TEST_SLEEP_MS 50
  138. #endif
  139. #define TEST_SLEEP() m2mb_os_taskSleep(M2MB_OS_MS2TICKS(TEST_SLEEP_MS))
  140. /* don't use file system for these tests, since ./certs dir isn't loaded */
  141. #undef NO_FILESYSTEM
  142. #define NO_FILESYSTEM
  143. #elif defined(THREADX) && !defined(WOLFSSL_WICED) && \
  144. !defined(THREADX_NO_DC_PRINTF)
  145. #ifndef NETOS
  146. /* since just testing, use THREADX log printf instead (NETOS prototypes
  147. * this elsewhere) */
  148. int dc_log_printf(char*, ...);
  149. #endif
  150. #undef printf
  151. #define printf dc_log_printf
  152. #elif defined(ANDROID)
  153. #ifdef XMALLOC_USER
  154. #include <stdlib.h> /* we're using malloc / free direct here */
  155. #endif
  156. #ifndef STRING_USER
  157. #include <stdio.h>
  158. #endif
  159. #include <android/log.h>
  160. #ifdef ANDROID_V454 /* See fips/android/wolfCrypt_v454_android */
  161. #ifndef NO_FILESYSTEM
  162. #define NO_FILESYSTEM /* Turn off tests that want to call SaveDerAndPem() */
  163. #endif
  164. #else
  165. #define printf(...) \
  166. __android_log_print(ANDROID_LOG_DEBUG, "[WOLFCRYPT]", __VA_ARGS__)
  167. #define fprintf(fp, ...) \
  168. __android_log_print(ANDROID_LOG_DEBUG, "[WOLFCRYPT]", __VA_ARGS__)
  169. #endif
  170. #elif defined(WOLFSSL_DEOS)
  171. #include <printx.h>
  172. #undef printf
  173. #define printf printx
  174. #else
  175. #ifdef XMALLOC_USER
  176. #include <stdlib.h> /* we're using malloc / free direct here */
  177. #endif
  178. #if !defined(STRING_USER) && !defined(WOLFSSL_LINUXKM)
  179. #include <stdio.h>
  180. #endif
  181. #if defined(WOLFSSL_LINUXKM) && !defined(WOLFSSL_LINUXKM_VERBOSE_DEBUG)
  182. #undef printf
  183. #define printf(...) ({})
  184. #endif
  185. /* enable way for customer to override test/bench printf */
  186. #ifdef XPRINTF
  187. #undef printf
  188. #define printf XPRINTF
  189. #elif !defined(printf)
  190. /* arrange for printf() to flush after every message -- this assures
  191. * redirected output (to a log file) records progress right up to the
  192. * moment of a crash/abort(); otherwise anything queued in stdout would
  193. * be lost.
  194. */
  195. #define printf(...) ( printf(__VA_ARGS__), fflush(stdout) )
  196. #endif
  197. #endif
  198. #endif /* !WOLFSSL_LOG_PRINTF */
  199. #include <wolfssl/wolfcrypt/memory.h>
  200. #include <wolfssl/wolfcrypt/wc_port.h>
  201. #include <wolfssl/wolfcrypt/logging.h>
  202. #include <wolfssl/wolfcrypt/types.h>
  203. #include <wolfssl/wolfcrypt/asn.h>
  204. #include <wolfssl/wolfcrypt/md2.h>
  205. #include <wolfssl/wolfcrypt/md5.h>
  206. #include <wolfssl/wolfcrypt/md4.h>
  207. #include <wolfssl/wolfcrypt/sha.h>
  208. #include <wolfssl/wolfcrypt/sha256.h>
  209. #include <wolfssl/wolfcrypt/sha512.h>
  210. #include <wolfssl/wolfcrypt/rc2.h>
  211. #include <wolfssl/wolfcrypt/arc4.h>
  212. #if defined(WC_NO_RNG)
  213. #include <wolfssl/wolfcrypt/integer.h>
  214. #else
  215. #include <wolfssl/wolfcrypt/random.h>
  216. #endif
  217. #include <wolfssl/wolfcrypt/coding.h>
  218. #include <wolfssl/wolfcrypt/signature.h>
  219. #include <wolfssl/wolfcrypt/rsa.h>
  220. #include <wolfssl/wolfcrypt/des3.h>
  221. #include <wolfssl/wolfcrypt/aes.h>
  222. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  223. #include <wolfssl/wolfcrypt/cmac.h>
  224. #include <wolfssl/wolfcrypt/siphash.h>
  225. #include <wolfssl/wolfcrypt/poly1305.h>
  226. #include <wolfssl/wolfcrypt/camellia.h>
  227. #include <wolfssl/wolfcrypt/hmac.h>
  228. #include <wolfssl/wolfcrypt/kdf.h>
  229. #include <wolfssl/wolfcrypt/dh.h>
  230. #include <wolfssl/wolfcrypt/dsa.h>
  231. #include <wolfssl/wolfcrypt/srp.h>
  232. #include <wolfssl/wolfcrypt/chacha.h>
  233. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  234. #include <wolfssl/wolfcrypt/pwdbased.h>
  235. #include <wolfssl/wolfcrypt/ripemd.h>
  236. #include <wolfssl/wolfcrypt/error-crypt.h>
  237. #ifdef HAVE_ECC
  238. #include <wolfssl/wolfcrypt/ecc.h>
  239. #endif
  240. #ifdef HAVE_CURVE25519
  241. #include <wolfssl/wolfcrypt/curve25519.h>
  242. #endif
  243. #ifdef HAVE_ED25519
  244. #include <wolfssl/wolfcrypt/ed25519.h>
  245. #endif
  246. #ifdef HAVE_CURVE448
  247. #include <wolfssl/wolfcrypt/curve448.h>
  248. #endif
  249. #ifdef HAVE_ED448
  250. #include <wolfssl/wolfcrypt/ed448.h>
  251. #endif
  252. #ifdef WOLFSSL_HAVE_KYBER
  253. #include <wolfssl/wolfcrypt/kyber.h>
  254. #ifdef WOLFSSL_WC_KYBER
  255. #include <wolfssl/wolfcrypt/wc_kyber.h>
  256. #endif
  257. #if defined(HAVE_LIBOQS) || defined(HAVE_PQM4)
  258. #include <wolfssl/wolfcrypt/ext_kyber.h>
  259. #endif
  260. #endif
  261. #ifdef WOLFCRYPT_HAVE_ECCSI
  262. #include <wolfssl/wolfcrypt/eccsi.h>
  263. #endif
  264. #ifdef WOLFCRYPT_HAVE_SAKKE
  265. #include <wolfssl/wolfcrypt/sakke.h>
  266. #endif
  267. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  268. #include <wolfssl/wolfcrypt/blake2.h>
  269. #endif
  270. #ifdef WOLFSSL_SHA3
  271. #include <wolfssl/wolfcrypt/sha3.h>
  272. #endif
  273. #ifdef HAVE_LIBZ
  274. #include <wolfssl/wolfcrypt/compress.h>
  275. #endif
  276. #ifdef HAVE_PKCS7
  277. #include <wolfssl/wolfcrypt/pkcs7.h>
  278. #endif
  279. #ifdef HAVE_FIPS
  280. #include <wolfssl/wolfcrypt/fips_test.h>
  281. #endif
  282. #ifdef HAVE_SELFTEST
  283. #include <wolfssl/wolfcrypt/selftest.h>
  284. #endif
  285. #ifdef WOLFSSL_ASYNC_CRYPT
  286. #include <wolfssl/wolfcrypt/async.h>
  287. #endif
  288. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  289. #include <wolfssl/wolfcrypt/logging.h>
  290. #endif
  291. #ifdef WOLFSSL_CAAM
  292. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  293. #endif
  294. #ifdef WOLF_CRYPTO_CB
  295. #include <wolfssl/wolfcrypt/cryptocb.h>
  296. #ifdef HAVE_INTEL_QA_SYNC
  297. #include <wolfssl/wolfcrypt/port/intel/quickassist_sync.h>
  298. #endif
  299. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  300. #include <wolfssl/wolfcrypt/port/cavium/cavium_octeon_sync.h>
  301. #endif
  302. #endif
  303. #ifdef _MSC_VER
  304. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  305. #pragma warning(disable: 4996)
  306. #endif
  307. #ifdef OPENSSL_EXTRA
  308. #ifndef WOLFCRYPT_ONLY
  309. #include <wolfssl/openssl/evp.h>
  310. #include <wolfssl/openssl/hmac.h>
  311. #endif
  312. #include <wolfssl/openssl/rand.h>
  313. #include <wolfssl/openssl/aes.h>
  314. #include <wolfssl/openssl/des.h>
  315. #endif
  316. #if defined(NO_FILESYSTEM) || defined(WC_NO_RNG)
  317. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  318. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  319. #define USE_CERT_BUFFERS_2048
  320. #endif
  321. #if !defined(USE_CERT_BUFFERS_256)
  322. #define USE_CERT_BUFFERS_256
  323. #endif
  324. #endif
  325. #if defined(WOLFSSL_CERT_GEN) && (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES))
  326. #define ENABLE_ECC384_CERT_GEN_TEST
  327. #endif
  328. #include <wolfssl/certs_test.h>
  329. #ifdef DEVKITPRO
  330. #include <wiiuse/wpad.h>
  331. #endif
  332. #ifdef WOLFSSL_STATIC_MEMORY
  333. static WOLFSSL_HEAP_HINT* HEAP_HINT;
  334. #else
  335. #define HEAP_HINT NULL
  336. #endif /* WOLFSSL_STATIC_MEMORY */
  337. /* these cases do not have intermediate hashing support */
  338. #if (defined(WOLFSSL_AFALG_XILINX_SHA3) && !defined(WOLFSSL_AFALG_HASH_KEEP)) \
  339. && !defined(WOLFSSL_XILINX_CRYPT) || defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  340. #define NO_INTM_HASH_TEST
  341. #endif
  342. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT) || \
  343. defined(WOLFSSL_SECO_CAAM)
  344. #define HASH_SIZE_LIMIT
  345. #endif
  346. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  347. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  348. static void initDefaultName(void);
  349. #endif
  350. /* for async devices */
  351. #ifdef WOLFSSL_CAAM_DEVID
  352. static int devId = WOLFSSL_CAAM_DEVID;
  353. #else
  354. static int devId = INVALID_DEVID;
  355. #endif
  356. #ifdef HAVE_WNR
  357. const char* wnrConfigFile = "wnr-example.conf";
  358. #endif
  359. #define TEST_STRING "Everyone gets Friday off."
  360. #define TEST_STRING_SZ 25
  361. typedef struct testVector {
  362. const char* input;
  363. const char* output;
  364. size_t inLen;
  365. size_t outLen;
  366. } testVector;
  367. #ifndef WOLFSSL_TEST_SUBROUTINE
  368. #define WOLFSSL_TEST_SUBROUTINE
  369. #endif
  370. PRAGMA_GCC("GCC diagnostic ignored \"-Wunused-function\"")
  371. PRAGMA_CLANG("clang diagnostic ignored \"-Wunused-function\"")
  372. WOLFSSL_TEST_SUBROUTINE int error_test(void);
  373. WOLFSSL_TEST_SUBROUTINE int base64_test(void);
  374. WOLFSSL_TEST_SUBROUTINE int base16_test(void);
  375. WOLFSSL_TEST_SUBROUTINE int asn_test(void);
  376. WOLFSSL_TEST_SUBROUTINE int md2_test(void);
  377. WOLFSSL_TEST_SUBROUTINE int md5_test(void);
  378. WOLFSSL_TEST_SUBROUTINE int md4_test(void);
  379. WOLFSSL_TEST_SUBROUTINE int sha_test(void);
  380. WOLFSSL_TEST_SUBROUTINE int sha224_test(void);
  381. WOLFSSL_TEST_SUBROUTINE int sha256_test(void);
  382. WOLFSSL_TEST_SUBROUTINE int sha512_test(void);
  383. WOLFSSL_TEST_SUBROUTINE int sha384_test(void);
  384. WOLFSSL_TEST_SUBROUTINE int sha3_test(void);
  385. WOLFSSL_TEST_SUBROUTINE int shake128_test(void);
  386. WOLFSSL_TEST_SUBROUTINE int shake256_test(void);
  387. WOLFSSL_TEST_SUBROUTINE int hash_test(void);
  388. WOLFSSL_TEST_SUBROUTINE int hmac_md5_test(void);
  389. WOLFSSL_TEST_SUBROUTINE int hmac_sha_test(void);
  390. WOLFSSL_TEST_SUBROUTINE int hmac_sha224_test(void);
  391. WOLFSSL_TEST_SUBROUTINE int hmac_sha256_test(void);
  392. WOLFSSL_TEST_SUBROUTINE int hmac_sha384_test(void);
  393. WOLFSSL_TEST_SUBROUTINE int hmac_sha512_test(void);
  394. WOLFSSL_TEST_SUBROUTINE int hmac_sha3_test(void);
  395. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  396. /* hkdf_test has issue with WOLFSSL_TEST_SUBROUTINE set on Xilinx with afalg */
  397. static int hkdf_test(void);
  398. #endif
  399. WOLFSSL_TEST_SUBROUTINE int sshkdf_test(void);
  400. #ifdef WOLFSSL_TLS13
  401. WOLFSSL_TEST_SUBROUTINE int tls13_kdf_test(void);
  402. #endif
  403. WOLFSSL_TEST_SUBROUTINE int x963kdf_test(void);
  404. WOLFSSL_TEST_SUBROUTINE int arc4_test(void);
  405. #ifdef WC_RC2
  406. WOLFSSL_TEST_SUBROUTINE int rc2_test(void);
  407. #endif
  408. WOLFSSL_TEST_SUBROUTINE int chacha_test(void);
  409. WOLFSSL_TEST_SUBROUTINE int XChaCha_test(void);
  410. WOLFSSL_TEST_SUBROUTINE int chacha20_poly1305_aead_test(void);
  411. WOLFSSL_TEST_SUBROUTINE int XChaCha20Poly1305_test(void);
  412. WOLFSSL_TEST_SUBROUTINE int des_test(void);
  413. WOLFSSL_TEST_SUBROUTINE int des3_test(void);
  414. WOLFSSL_TEST_SUBROUTINE int aes_test(void);
  415. WOLFSSL_TEST_SUBROUTINE int aes192_test(void);
  416. WOLFSSL_TEST_SUBROUTINE int aes256_test(void);
  417. WOLFSSL_TEST_SUBROUTINE int aesofb_test(void);
  418. WOLFSSL_TEST_SUBROUTINE int cmac_test(void);
  419. #if defined(WOLFSSL_SIPHASH)
  420. WOLFSSL_TEST_SUBROUTINE int siphash_test(void);
  421. #endif
  422. WOLFSSL_TEST_SUBROUTINE int poly1305_test(void);
  423. WOLFSSL_TEST_SUBROUTINE int aesgcm_test(void);
  424. WOLFSSL_TEST_SUBROUTINE int aesgcm_default_test(void);
  425. WOLFSSL_TEST_SUBROUTINE int gmac_test(void);
  426. WOLFSSL_TEST_SUBROUTINE int aesccm_test(void);
  427. WOLFSSL_TEST_SUBROUTINE int aeskeywrap_test(void);
  428. WOLFSSL_TEST_SUBROUTINE int camellia_test(void);
  429. #ifdef WC_RSA_NO_PADDING
  430. WOLFSSL_TEST_SUBROUTINE int rsa_no_pad_test(void);
  431. #endif
  432. WOLFSSL_TEST_SUBROUTINE int rsa_test(void);
  433. WOLFSSL_TEST_SUBROUTINE int dh_test(void);
  434. WOLFSSL_TEST_SUBROUTINE int dsa_test(void);
  435. WOLFSSL_TEST_SUBROUTINE int srp_test(void);
  436. #ifndef WC_NO_RNG
  437. WOLFSSL_TEST_SUBROUTINE int random_test(void);
  438. #endif /* WC_NO_RNG */
  439. WOLFSSL_TEST_SUBROUTINE int pwdbased_test(void);
  440. WOLFSSL_TEST_SUBROUTINE int ripemd_test(void);
  441. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  442. WOLFSSL_TEST_SUBROUTINE int openssl_test(void); /* test mini api */
  443. WOLFSSL_TEST_SUBROUTINE int openssl_pkey0_test(void);
  444. WOLFSSL_TEST_SUBROUTINE int openssl_pkey1_test(void);
  445. WOLFSSL_TEST_SUBROUTINE int openSSL_evpMD_test(void);
  446. WOLFSSL_TEST_SUBROUTINE int openssl_evpSig_test(void);
  447. #endif
  448. WOLFSSL_TEST_SUBROUTINE int pbkdf1_test(void);
  449. WOLFSSL_TEST_SUBROUTINE int pkcs12_test(void);
  450. WOLFSSL_TEST_SUBROUTINE int pbkdf2_test(void);
  451. WOLFSSL_TEST_SUBROUTINE int scrypt_test(void);
  452. #ifdef HAVE_ECC
  453. WOLFSSL_TEST_SUBROUTINE int ecc_test(void);
  454. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  455. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  456. WOLFSSL_TEST_SUBROUTINE int ecc_encrypt_test(void);
  457. #endif
  458. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  459. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  460. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  461. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  462. /* skip for ATECC508/608A, cannot import private key buffers */
  463. WOLFSSL_TEST_SUBROUTINE int ecc_test_buffers(void);
  464. #endif
  465. #endif
  466. #ifdef HAVE_CURVE25519
  467. WOLFSSL_TEST_SUBROUTINE int curve25519_test(void);
  468. #endif
  469. #ifdef HAVE_ED25519
  470. WOLFSSL_TEST_SUBROUTINE int ed25519_test(void);
  471. #endif
  472. #ifdef HAVE_CURVE448
  473. WOLFSSL_TEST_SUBROUTINE int curve448_test(void);
  474. #endif
  475. #ifdef HAVE_ED448
  476. WOLFSSL_TEST_SUBROUTINE int ed448_test(void);
  477. #endif
  478. #ifdef WOLFSSL_HAVE_KYBER
  479. WOLFSSL_TEST_SUBROUTINE int kyber_test(void);
  480. #endif
  481. #ifdef WOLFCRYPT_HAVE_ECCSI
  482. WOLFSSL_TEST_SUBROUTINE int eccsi_test(void);
  483. #endif
  484. #ifdef WOLFCRYPT_HAVE_SAKKE
  485. WOLFSSL_TEST_SUBROUTINE int sakke_test(void);
  486. #endif
  487. #ifdef HAVE_BLAKE2
  488. WOLFSSL_TEST_SUBROUTINE int blake2b_test(void);
  489. #endif
  490. #ifdef HAVE_BLAKE2S
  491. WOLFSSL_TEST_SUBROUTINE int blake2s_test(void);
  492. #endif
  493. #ifdef HAVE_LIBZ
  494. WOLFSSL_TEST_SUBROUTINE int compress_test(void);
  495. #endif
  496. #ifdef HAVE_PKCS7
  497. #ifndef NO_PKCS7_ENCRYPTED_DATA
  498. WOLFSSL_TEST_SUBROUTINE int pkcs7encrypted_test(void);
  499. #endif
  500. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  501. WOLFSSL_TEST_SUBROUTINE int pkcs7compressed_test(void);
  502. #endif
  503. WOLFSSL_TEST_SUBROUTINE int pkcs7signed_test(void);
  504. WOLFSSL_TEST_SUBROUTINE int pkcs7enveloped_test(void);
  505. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  506. WOLFSSL_TEST_SUBROUTINE int pkcs7authenveloped_test(void);
  507. #endif
  508. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  509. WOLFSSL_TEST_SUBROUTINE int pkcs7callback_test(byte* cert, word32 certSz, byte* key,
  510. word32 keySz);
  511. #endif
  512. #endif
  513. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  514. !defined(NO_FILESYSTEM)
  515. WOLFSSL_TEST_SUBROUTINE int cert_test(void);
  516. #endif
  517. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  518. !defined(NO_FILESYSTEM) && defined(WOLFSSL_CERT_GEN)
  519. WOLFSSL_TEST_SUBROUTINE int certext_test(void);
  520. #endif
  521. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  522. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  523. WOLFSSL_TEST_SUBROUTINE int decodedCertCache_test(void);
  524. #endif
  525. WOLFSSL_TEST_SUBROUTINE int memory_test(void);
  526. #ifdef HAVE_VALGRIND
  527. WOLFSSL_TEST_SUBROUTINE int mp_test(void);
  528. #endif
  529. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  530. WOLFSSL_TEST_SUBROUTINE int prime_test(void);
  531. #endif
  532. #if defined(ASN_BER_TO_DER) && \
  533. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  534. defined(OPENSSL_EXTRA_X509_SMALL))
  535. WOLFSSL_TEST_SUBROUTINE int berder_test(void);
  536. #endif
  537. WOLFSSL_TEST_SUBROUTINE int logging_test(void);
  538. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  539. WOLFSSL_TEST_SUBROUTINE int time_test(void);
  540. #endif
  541. WOLFSSL_TEST_SUBROUTINE int mutex_test(void);
  542. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  543. WOLFSSL_TEST_SUBROUTINE int memcb_test(void);
  544. #endif
  545. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  546. WOLFSSL_TEST_SUBROUTINE int blob_test(void);
  547. #endif
  548. #ifdef WOLF_CRYPTO_CB
  549. WOLFSSL_TEST_SUBROUTINE int cryptocb_test(void);
  550. #endif
  551. #ifdef WOLFSSL_CERT_PIV
  552. WOLFSSL_TEST_SUBROUTINE int certpiv_test(void);
  553. #endif
  554. #ifdef WOLFSSL_AES_SIV
  555. WOLFSSL_TEST_SUBROUTINE int aes_siv_test(void);
  556. #endif
  557. /* General big buffer size for many tests. */
  558. #define FOURK_BUF 4096
  559. #define ERROR_OUT(err, eLabel) do { ret = (err); goto eLabel; } while (0)
  560. #ifdef HAVE_STACK_SIZE
  561. static THREAD_RETURN err_sys(const char* msg, int es)
  562. #else
  563. static int err_sys(const char* msg, int es)
  564. #endif
  565. {
  566. (void)msg;
  567. (void)es;
  568. #ifdef WOLFSSL_LINUXKM
  569. lkm_printf("%s error = %d\n", msg, es);
  570. EXIT_TEST(es);
  571. #else
  572. printf("%s error = %d\n", msg, es);
  573. EXIT_TEST(-1);
  574. #endif
  575. }
  576. #ifndef HAVE_WOLFCRYPT_TEST_OPTIONS
  577. /* func_args from test.h, so don't have to pull in other stuff */
  578. typedef struct func_args {
  579. int argc;
  580. char** argv;
  581. int return_code;
  582. } func_args;
  583. #endif /* !HAVE_WOLFCRYPT_TEST_OPTIONS */
  584. #if defined(HAVE_FIPS) && !defined(WOLFSSL_LINUXKM)
  585. static void myFipsCb(int ok, int err, const char* hash)
  586. {
  587. printf("in my Fips callback, ok = %d, err = %d\n", ok, err);
  588. printf("message = %s\n", wc_GetErrorString(err));
  589. printf("hash = %s\n", hash);
  590. if (err == IN_CORE_FIPS_E) {
  591. printf("In core integrity hash check failure, copy above hash\n");
  592. printf("into verifyCore[] in fips_test.c and rebuild\n");
  593. }
  594. }
  595. #endif /* HAVE_FIPS && !WOLFSSL_LINUXKM */
  596. #ifdef WOLFSSL_STATIC_MEMORY
  597. #if defined(WOLFSSL_STATIC_MEMORY_TEST_SZ)
  598. static byte gTestMemory[WOLFSSL_STATIC_MEMORY_TEST_SZ];
  599. #elif defined(BENCH_EMBEDDED)
  600. static byte gTestMemory[14000];
  601. #elif defined(WOLFSSL_CERT_EXT)
  602. static byte gTestMemory[140000];
  603. #elif (defined(WOLFSSL_SP_MATH_ALL) || defined(USE_FAST_MATH)) && \
  604. !defined(ALT_ECC_SIZE)
  605. static byte gTestMemory[160000];
  606. #else
  607. static byte gTestMemory[80000];
  608. #endif
  609. #endif
  610. #ifdef WOLFSSL_PB
  611. static int wolfssl_pb_print(const char* msg, ...)
  612. {
  613. int ret;
  614. va_list args;
  615. char tmpBuf[80];
  616. va_start(args, msg);
  617. ret = vsprint(tmpBuf, msg, args);
  618. va_end(args);
  619. fnDumpStringToSystemLog(tmpBuf);
  620. return ret;
  621. }
  622. #endif /* WOLFSSL_PB */
  623. /* optional macro to add sleep between tests */
  624. #ifndef TEST_SLEEP
  625. #define TEST_SLEEP()
  626. #else
  627. #define TEST_PASS test_pass
  628. #include <stdarg.h> /* for var args */
  629. static WC_INLINE void test_pass(const char* fmt, ...)
  630. {
  631. va_list args;
  632. va_start(args, fmt);
  633. STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK(max_relative_stack, vprintf(fmt, args));
  634. va_end(args);
  635. PRINT_HEAP_CHECKPOINT();
  636. TEST_SLEEP();
  637. ASSERT_RESTORED_VECTOR_REGISTERS(exit(1););
  638. }
  639. #endif
  640. /* set test pass output to printf if not overriden */
  641. #ifndef TEST_PASS
  642. /* redirect to printf */
  643. #define TEST_PASS(...) { \
  644. if (STACK_SIZE_CHECKPOINT_WITH_MAX_CHECK \
  645. (max_relative_stack, printf(__VA_ARGS__)) < 0) { \
  646. return err_sys("post-test check failed", -1); \
  647. } \
  648. PRINT_HEAP_CHECKPOINT(); \
  649. ASSERT_RESTORED_VECTOR_REGISTERS(exit(1);); \
  650. }
  651. #endif
  652. #ifdef HAVE_STACK_SIZE
  653. THREAD_RETURN WOLFSSL_THREAD wolfcrypt_test(void* args)
  654. #else
  655. int wolfcrypt_test(void* args)
  656. #endif
  657. {
  658. int ret;
  659. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  660. long heap_baselineAllocs, heap_baselineBytes;
  661. #endif
  662. STACK_SIZE_INIT();
  663. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  664. (void)wolfCrypt_heap_peakAllocs_checkpoint();
  665. heap_baselineAllocs = wolfCrypt_heap_peakAllocs_checkpoint();
  666. (void)wolfCrypt_heap_peakBytes_checkpoint();
  667. heap_baselineBytes = wolfCrypt_heap_peakBytes_checkpoint();
  668. #endif
  669. printf("------------------------------------------------------------------------------\n");
  670. printf(" wolfSSL version %s\n", LIBWOLFSSL_VERSION_STRING);
  671. printf("------------------------------------------------------------------------------\n");
  672. if (args) {
  673. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  674. int ch;
  675. #endif
  676. ((func_args*)args)->return_code = -1; /* error state */
  677. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  678. while ((ch = mygetopt(((func_args*)args)->argc, ((func_args*)args)->argv, "s:m:a:h")) != -1) {
  679. switch(ch) {
  680. case 's':
  681. #ifdef HAVE_STACK_SIZE_VERBOSE
  682. max_relative_stack = (ssize_t)atoi(myoptarg);
  683. break;
  684. #else
  685. return err_sys("-s (max relative stack bytes) requires HAVE_STACK_SIZE_VERBOSE (--enable-stacksize=verbose).", -1);
  686. #endif
  687. case 'm':
  688. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  689. max_relative_heap_bytes = (ssize_t)atoi(myoptarg);
  690. break;
  691. #else
  692. return err_sys("-m (max relative heap memory bytes) requires WOLFSSL_TRACK_MEMORY_VERBOSE (--enable-trackmemory=verbose).", -1);
  693. #endif
  694. case 'a':
  695. #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
  696. max_relative_heap_allocs = (ssize_t)atoi(myoptarg);
  697. break;
  698. #else
  699. return err_sys("-a (max relative heap allocs) requires WOLFSSL_TRACK_MEMORY_VERBOSE (--enable-trackmemory=verbose).", -1);
  700. #endif
  701. case 'h':
  702. return err_sys("\
  703. options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\
  704. [-a max_relative_heap_allocs] [-h]\n", 0);
  705. default:
  706. return err_sys("unknown test option. try -h.", -1);
  707. }
  708. }
  709. #endif
  710. }
  711. #ifdef WOLFSSL_STATIC_MEMORY
  712. if (wc_LoadStaticMemory(&HEAP_HINT, gTestMemory, sizeof(gTestMemory),
  713. WOLFMEM_GENERAL, 1) != 0) {
  714. printf("unable to load static memory.\n");
  715. return(EXIT_FAILURE);
  716. }
  717. #endif
  718. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
  719. wolfSSL_Debugging_ON();
  720. #endif
  721. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  722. wc_SetLoggingHeap(HEAP_HINT);
  723. #endif
  724. #if defined(HAVE_FIPS) && !defined(WOLFSSL_LINUXKM)
  725. wolfCrypt_SetCb_fips(myFipsCb);
  726. #endif
  727. #if !defined(NO_BIG_INT)
  728. if (CheckCtcSettings() != 1) {
  729. printf("Sizeof mismatch (build) %x != (run) %lx\n",
  730. CTC_SETTINGS, (unsigned long)CheckRunTimeSettings());
  731. return err_sys("Build vs runtime math mismatch\n", -1000);
  732. }
  733. #if defined(USE_FAST_MATH) && \
  734. (!defined(NO_RSA) || !defined(NO_DH) || defined(HAVE_ECC))
  735. if (CheckFastMathSettings() != 1)
  736. return err_sys("Build vs runtime fastmath FP_MAX_BITS mismatch\n",
  737. -1001);
  738. #endif /* USE_FAST_MATH */
  739. #endif /* !NO_BIG_INT */
  740. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  741. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  742. initDefaultName();
  743. #endif
  744. #ifdef WOLFSSL_ASYNC_CRYPT
  745. ret = wolfAsync_DevOpen(&devId);
  746. if (ret < 0) {
  747. printf("Async device open failed\nRunning without async\n");
  748. }
  749. #else
  750. (void)devId;
  751. #endif /* WOLFSSL_ASYNC_CRYPT */
  752. #ifdef WOLF_CRYPTO_CB
  753. #ifdef HAVE_INTEL_QA_SYNC
  754. devId = wc_CryptoCb_InitIntelQa();
  755. if (INVALID_DEVID == devId) {
  756. printf("Couldn't init the Intel QA\n");
  757. }
  758. #endif
  759. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  760. devId = wc_CryptoCb_InitOcteon();
  761. if (INVALID_DEVID == devId) {
  762. printf("Couldn't init the Cavium Octeon\n");
  763. }
  764. #endif
  765. #endif
  766. #ifdef HAVE_SELFTEST
  767. if ( (ret = wolfCrypt_SelfTest()) != 0)
  768. return err_sys("CAVP selftest failed!\n", ret);
  769. else
  770. TEST_PASS("CAVP selftest passed!\n");
  771. #endif
  772. if ( (ret = error_test()) != 0)
  773. return err_sys("error test failed!\n", ret);
  774. else
  775. TEST_PASS("error test passed!\n");
  776. if ( (ret = memory_test()) != 0)
  777. return err_sys("MEMORY test failed!\n", ret);
  778. else
  779. TEST_PASS("MEMORY test passed!\n");
  780. #ifndef NO_CODING
  781. if ( (ret = base64_test()) != 0)
  782. return err_sys("base64 test failed!\n", ret);
  783. else
  784. TEST_PASS("base64 test passed!\n");
  785. #ifdef WOLFSSL_BASE16
  786. if ( (ret = base16_test()) != 0)
  787. return err_sys("base16 test failed!\n", ret);
  788. else
  789. TEST_PASS("base16 test passed!\n");
  790. #endif
  791. #endif /* !NO_CODING */
  792. #ifndef NO_ASN
  793. if ( (ret = asn_test()) != 0)
  794. return err_sys("asn test failed!\n", ret);
  795. else
  796. TEST_PASS("asn test passed!\n");
  797. #endif
  798. #ifndef WC_NO_RNG
  799. if ( (ret = random_test()) != 0)
  800. return err_sys("RANDOM test failed!\n", ret);
  801. else
  802. TEST_PASS("RANDOM test passed!\n");
  803. #endif /* WC_NO_RNG */
  804. #ifndef NO_MD5
  805. if ( (ret = md5_test()) != 0)
  806. return err_sys("MD5 test failed!\n", ret);
  807. else
  808. TEST_PASS("MD5 test passed!\n");
  809. #endif
  810. #ifdef WOLFSSL_MD2
  811. if ( (ret = md2_test()) != 0)
  812. return err_sys("MD2 test failed!\n", ret);
  813. else
  814. TEST_PASS("MD2 test passed!\n");
  815. #endif
  816. #ifndef NO_MD4
  817. if ( (ret = md4_test()) != 0)
  818. return err_sys("MD4 test failed!\n", ret);
  819. else
  820. TEST_PASS("MD4 test passed!\n");
  821. #endif
  822. #ifndef NO_SHA
  823. if ( (ret = sha_test()) != 0)
  824. return err_sys("SHA test failed!\n", ret);
  825. else
  826. TEST_PASS("SHA test passed!\n");
  827. #endif
  828. #ifdef WOLFSSL_SHA224
  829. if ( (ret = sha224_test()) != 0)
  830. return err_sys("SHA-224 test failed!\n", ret);
  831. else
  832. TEST_PASS("SHA-224 test passed!\n");
  833. #endif
  834. #ifndef NO_SHA256
  835. if ( (ret = sha256_test()) != 0)
  836. return err_sys("SHA-256 test failed!\n", ret);
  837. else
  838. TEST_PASS("SHA-256 test passed!\n");
  839. #endif
  840. #ifdef WOLFSSL_SHA384
  841. if ( (ret = sha384_test()) != 0)
  842. return err_sys("SHA-384 test failed!\n", ret);
  843. else
  844. TEST_PASS("SHA-384 test passed!\n");
  845. #endif
  846. #ifdef WOLFSSL_SHA512
  847. if ( (ret = sha512_test()) != 0)
  848. return err_sys("SHA-512 test failed!\n", ret);
  849. else
  850. TEST_PASS("SHA-512 test passed!\n");
  851. #endif
  852. #ifdef WOLFSSL_SHA3
  853. if ( (ret = sha3_test()) != 0)
  854. return err_sys("SHA-3 test failed!\n", ret);
  855. else
  856. TEST_PASS("SHA-3 test passed!\n");
  857. #endif
  858. #ifdef WOLFSSL_SHAKE128
  859. if ( (ret = shake128_test()) != 0)
  860. return err_sys("SHAKE128 test failed!\n", ret);
  861. else
  862. TEST_PASS("SHAKE128 test passed!\n");
  863. #endif
  864. #ifdef WOLFSSL_SHAKE256
  865. if ( (ret = shake256_test()) != 0)
  866. return err_sys("SHAKE256 test failed!\n", ret);
  867. else
  868. TEST_PASS("SHAKE256 test passed!\n");
  869. #endif
  870. #ifndef NO_HASH_WRAPPER
  871. if ( (ret = hash_test()) != 0)
  872. return err_sys("Hash test failed!\n", ret);
  873. else
  874. TEST_PASS("Hash test passed!\n");
  875. #endif
  876. #ifdef WOLFSSL_RIPEMD
  877. if ( (ret = ripemd_test()) != 0)
  878. return err_sys("RIPEMD test failed!\n", ret);
  879. else
  880. TEST_PASS("RIPEMD test passed!\n");
  881. #endif
  882. #ifdef HAVE_BLAKE2
  883. if ( (ret = blake2b_test()) != 0)
  884. return err_sys("BLAKE2b test failed!\n", ret);
  885. else
  886. TEST_PASS("BLAKE2b test passed!\n");
  887. #endif
  888. #ifdef HAVE_BLAKE2S
  889. if ( (ret = blake2s_test()) != 0)
  890. return err_sys("BLAKE2s test failed!\n", ret);
  891. else
  892. TEST_PASS("BLAKE2s test passed!\n");
  893. #endif
  894. #ifndef NO_HMAC
  895. #if !defined(NO_MD5) && !(defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) \
  896. && (HAVE_FIPS_VERSION >= 5))
  897. if ( (ret = hmac_md5_test()) != 0)
  898. return err_sys("HMAC-MD5 test failed!\n", ret);
  899. else
  900. TEST_PASS("HMAC-MD5 test passed!\n");
  901. #endif
  902. #ifndef NO_SHA
  903. if ( (ret = hmac_sha_test()) != 0)
  904. return err_sys("HMAC-SHA test failed!\n", ret);
  905. else
  906. TEST_PASS("HMAC-SHA test passed!\n");
  907. #endif
  908. #ifdef WOLFSSL_SHA224
  909. if ( (ret = hmac_sha224_test()) != 0)
  910. return err_sys("HMAC-SHA224 test failed!\n", ret);
  911. else
  912. TEST_PASS("HMAC-SHA224 test passed!\n");
  913. #endif
  914. #ifndef NO_SHA256
  915. if ( (ret = hmac_sha256_test()) != 0)
  916. return err_sys("HMAC-SHA256 test failed!\n", ret);
  917. else
  918. TEST_PASS("HMAC-SHA256 test passed!\n");
  919. #endif
  920. #ifdef WOLFSSL_SHA384
  921. if ( (ret = hmac_sha384_test()) != 0)
  922. return err_sys("HMAC-SHA384 test failed!\n", ret);
  923. else
  924. TEST_PASS("HMAC-SHA384 test passed!\n");
  925. #endif
  926. #ifdef WOLFSSL_SHA512
  927. if ( (ret = hmac_sha512_test()) != 0)
  928. return err_sys("HMAC-SHA512 test failed!\n", ret);
  929. else
  930. TEST_PASS("HMAC-SHA512 test passed!\n");
  931. #endif
  932. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  933. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  934. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  935. if ( (ret = hmac_sha3_test()) != 0)
  936. return err_sys("HMAC-SHA3 test failed!\n", ret);
  937. else
  938. TEST_PASS("HMAC-SHA3 test passed!\n");
  939. #endif
  940. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  941. PRIVATE_KEY_UNLOCK();
  942. if ( (ret = hkdf_test()) != 0)
  943. return err_sys("HMAC-KDF test failed!\n", ret);
  944. else
  945. TEST_PASS("HMAC-KDF test passed!\n");
  946. PRIVATE_KEY_LOCK();
  947. #endif
  948. #endif /* !NO_HMAC */
  949. #ifdef WOLFSSL_WOLFSSH
  950. PRIVATE_KEY_UNLOCK();
  951. if ( (ret = sshkdf_test()) != 0)
  952. return err_sys("SSH-KDF test failed!\n", ret);
  953. else
  954. TEST_PASS("SSH-KDF test passed!\n");
  955. PRIVATE_KEY_LOCK();
  956. #endif /* WOLFSSL_WOLFSSH */
  957. #ifdef WOLFSSL_TLS13
  958. PRIVATE_KEY_UNLOCK();
  959. if ( (ret = tls13_kdf_test()) != 0)
  960. return err_sys("TLSv1.3 KDF test failed!\n", ret);
  961. else
  962. TEST_PASS("TLSv1.3 KDF test passed!\n");
  963. PRIVATE_KEY_LOCK();
  964. #endif /* WOLFSSL_TLS13 */
  965. #if defined(HAVE_X963_KDF) && defined(HAVE_ECC)
  966. if ( (ret = x963kdf_test()) != 0)
  967. return err_sys("X963-KDF test failed!\n", ret);
  968. else
  969. TEST_PASS("X963-KDF test passed!\n");
  970. #endif
  971. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128) && \
  972. !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  973. if ( (ret = gmac_test()) != 0)
  974. return err_sys("GMAC test failed!\n", ret);
  975. else
  976. TEST_PASS("GMAC test passed!\n");
  977. #endif
  978. #ifdef WC_RC2
  979. if ( (ret = rc2_test()) != 0)
  980. return err_sys("RC2 test failed!\n", ret);
  981. else
  982. TEST_PASS("RC2 test passed!\n");
  983. #endif
  984. #ifndef NO_RC4
  985. if ( (ret = arc4_test()) != 0)
  986. return err_sys("ARC4 test failed!\n", ret);
  987. else
  988. TEST_PASS("ARC4 test passed!\n");
  989. #endif
  990. #ifdef HAVE_CHACHA
  991. if ( (ret = chacha_test()) != 0)
  992. return err_sys("Chacha test failed!\n", ret);
  993. else
  994. TEST_PASS("Chacha test passed!\n");
  995. #endif
  996. #ifdef HAVE_XCHACHA
  997. if ( (ret = XChaCha_test()) != 0)
  998. return err_sys("XChacha test failed!\n", ret);
  999. else
  1000. TEST_PASS("XChacha test passed!\n");
  1001. #endif
  1002. #ifdef HAVE_POLY1305
  1003. if ( (ret = poly1305_test()) != 0)
  1004. return err_sys("POLY1305 test failed!\n", ret);
  1005. else
  1006. TEST_PASS("POLY1305 test passed!\n");
  1007. #endif
  1008. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  1009. if ( (ret = chacha20_poly1305_aead_test()) != 0)
  1010. return err_sys("ChaCha20-Poly1305 AEAD test failed!\n", ret);
  1011. else
  1012. TEST_PASS("ChaCha20-Poly1305 AEAD test passed!\n");
  1013. #endif
  1014. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  1015. if ( (ret = XChaCha20Poly1305_test()) != 0)
  1016. return err_sys("XChaCha20-Poly1305 AEAD test failed!\n", ret);
  1017. else
  1018. TEST_PASS("XChaCha20-Poly1305 AEAD test passed!\n");
  1019. #endif
  1020. #ifndef NO_DES3
  1021. if ( (ret = des_test()) != 0)
  1022. return err_sys("DES test failed!\n", ret);
  1023. else
  1024. TEST_PASS("DES test passed!\n");
  1025. #endif
  1026. #ifndef NO_DES3
  1027. if ( (ret = des3_test()) != 0)
  1028. return err_sys("DES3 test failed!\n", ret);
  1029. else
  1030. TEST_PASS("DES3 test passed!\n");
  1031. #endif
  1032. #ifndef NO_AES
  1033. if ( (ret = aes_test()) != 0)
  1034. return err_sys("AES test failed!\n", ret);
  1035. else
  1036. TEST_PASS("AES test passed!\n");
  1037. #ifdef WOLFSSL_AES_192
  1038. if ( (ret = aes192_test()) != 0)
  1039. return err_sys("AES192 test failed!\n", ret);
  1040. else
  1041. TEST_PASS("AES192 test passed!\n");
  1042. #endif
  1043. #ifdef WOLFSSL_AES_256
  1044. if ( (ret = aes256_test()) != 0)
  1045. return err_sys("AES256 test failed!\n", ret);
  1046. else
  1047. TEST_PASS("AES256 test passed!\n");
  1048. #endif
  1049. #ifdef WOLFSSL_AES_OFB
  1050. if ( (ret = aesofb_test()) != 0)
  1051. return err_sys("AES-OFB test failed!\n", ret);
  1052. else
  1053. TEST_PASS("AESOFB test passed!\n");
  1054. #endif
  1055. #ifdef HAVE_AESGCM
  1056. #if !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO)
  1057. if ( (ret = aesgcm_test()) != 0)
  1058. return err_sys("AES-GCM test failed!\n", ret);
  1059. #endif
  1060. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT) && \
  1061. !defined(WOLFSSL_KCAPI_AES) && !(defined(WOLF_CRYPTO_CB) && \
  1062. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  1063. if ((ret = aesgcm_default_test()) != 0) {
  1064. return err_sys("AES-GCM test failed!\n", ret);
  1065. }
  1066. #endif
  1067. if (ret == 0) {
  1068. TEST_PASS("AES-GCM test passed!\n");
  1069. }
  1070. #endif
  1071. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  1072. if ( (ret = aesccm_test()) != 0)
  1073. return err_sys("AES-CCM test failed!\n", ret);
  1074. else
  1075. TEST_PASS("AES-CCM test passed!\n");
  1076. #endif
  1077. #ifdef HAVE_AES_KEYWRAP
  1078. if ( (ret = aeskeywrap_test()) != 0)
  1079. return err_sys("AES Key Wrap test failed!\n", ret);
  1080. else
  1081. TEST_PASS("AES Key Wrap test passed!\n");
  1082. #endif
  1083. #ifdef WOLFSSL_AES_SIV
  1084. if ( (ret = aes_siv_test()) != 0)
  1085. return err_sys("AES-SIV test failed!\n", ret);
  1086. else
  1087. TEST_PASS("AES-SIV test passed!\n");
  1088. #endif
  1089. #endif
  1090. #ifdef HAVE_CAMELLIA
  1091. if ( (ret = camellia_test()) != 0)
  1092. return err_sys("CAMELLIA test failed!\n", ret);
  1093. else
  1094. TEST_PASS("CAMELLIA test passed!\n");
  1095. #endif
  1096. #if !defined(NO_RSA)
  1097. #ifdef WC_RSA_NO_PADDING
  1098. if ( (ret = rsa_no_pad_test()) != 0)
  1099. return err_sys("RSA NOPAD test failed!\n", ret);
  1100. else
  1101. TEST_PASS("RSA NOPAD test passed!\n");
  1102. #endif
  1103. if ( (ret = rsa_test()) != 0)
  1104. return err_sys("RSA test failed!\n", ret);
  1105. else
  1106. TEST_PASS("RSA test passed!\n");
  1107. #endif
  1108. #ifndef NO_DH
  1109. PRIVATE_KEY_UNLOCK();
  1110. if ( (ret = dh_test()) != 0)
  1111. return err_sys("DH test failed!\n", ret);
  1112. else
  1113. TEST_PASS("DH test passed!\n");
  1114. PRIVATE_KEY_LOCK();
  1115. #endif
  1116. #ifndef NO_DSA
  1117. if ( (ret = dsa_test()) != 0)
  1118. return err_sys("DSA test failed!\n", ret);
  1119. else
  1120. TEST_PASS("DSA test passed!\n");
  1121. #endif
  1122. #ifdef WOLFCRYPT_HAVE_SRP
  1123. if ( (ret = srp_test()) != 0)
  1124. return err_sys("SRP test failed!\n", ret);
  1125. else
  1126. TEST_PASS("SRP test passed!\n");
  1127. #endif
  1128. #ifndef NO_PWDBASED
  1129. if ( (ret = pwdbased_test()) != 0)
  1130. return err_sys("PWDBASED test failed!\n", ret);
  1131. else
  1132. TEST_PASS("PWDBASED test passed!\n");
  1133. #endif
  1134. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  1135. if ( (ret = openssl_test()) != 0)
  1136. return err_sys("OPENSSL test failed!\n", ret);
  1137. else
  1138. TEST_PASS("OPENSSL test passed!\n");
  1139. if ( (ret = openSSL_evpMD_test()) != 0)
  1140. return err_sys("OPENSSL (EVP MD) test failed!\n", ret);
  1141. else
  1142. TEST_PASS("OPENSSL (EVP MD) passed!\n");
  1143. if ( (ret = openssl_pkey0_test()) != 0)
  1144. return err_sys("OPENSSL (PKEY0) test failed!\n", ret);
  1145. else
  1146. TEST_PASS("OPENSSL (PKEY0) passed!\n");
  1147. if ( (ret = openssl_pkey1_test()) != 0)
  1148. return err_sys("OPENSSL (PKEY1) test failed!\n", ret);
  1149. else
  1150. TEST_PASS("OPENSSL (PKEY1) passed!\n");
  1151. #if !defined(WOLF_CRYPTO_CB_ONLY_RSA) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  1152. if ( (ret = openssl_evpSig_test()) != 0)
  1153. return err_sys("OPENSSL (EVP Sign/Verify) test failed!\n", ret);
  1154. else
  1155. TEST_PASS("OPENSSL (EVP Sign/Verify) passed!\n");
  1156. #endif
  1157. #endif
  1158. #if defined(HAVE_ECC)
  1159. PRIVATE_KEY_UNLOCK();
  1160. if ( (ret = ecc_test()) != 0)
  1161. return err_sys("ECC test failed!\n", ret);
  1162. else
  1163. TEST_PASS("ECC test passed!\n");
  1164. PRIVATE_KEY_LOCK();
  1165. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  1166. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  1167. if ( (ret = ecc_encrypt_test()) != 0)
  1168. return err_sys("ECC Enc test failed!\n", ret);
  1169. else
  1170. TEST_PASS("ECC Enc test passed!\n");
  1171. #endif
  1172. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  1173. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  1174. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  1175. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  1176. /* skip for ATECC508/608A, cannot import private key buffers */
  1177. if ( (ret = ecc_test_buffers()) != 0)
  1178. return err_sys("ECC buffer test failed!\n", ret);
  1179. else
  1180. TEST_PASS("ECC buffer test passed!\n");
  1181. #endif
  1182. #endif
  1183. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  1184. !defined(NO_FILESYSTEM)
  1185. if ( (ret = cert_test()) != 0)
  1186. return err_sys("CERT test failed!\n", ret);
  1187. else
  1188. TEST_PASS("CERT test passed!\n");
  1189. #endif
  1190. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  1191. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(WOLFSSL_GEN_CERT)
  1192. if ( (ret = certext_test()) != 0)
  1193. return err_sys("CERT EXT test failed!\n", ret);
  1194. else
  1195. TEST_PASS("CERT EXT test passed!\n");
  1196. #endif
  1197. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  1198. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  1199. if ( (ret = decodedCertCache_test()) != 0)
  1200. return err_sys("DECODED CERT CACHE test failed!\n", ret);
  1201. else
  1202. TEST_PASS("DECODED CERT CACHE test passed!\n");
  1203. #endif
  1204. #ifdef HAVE_CURVE25519
  1205. if ( (ret = curve25519_test()) != 0)
  1206. return err_sys("CURVE25519 test failed!\n", ret);
  1207. else
  1208. TEST_PASS("CURVE25519 test passed!\n");
  1209. #endif
  1210. #ifdef HAVE_ED25519
  1211. if ( (ret = ed25519_test()) != 0)
  1212. return err_sys("ED25519 test failed!\n", ret);
  1213. else
  1214. TEST_PASS("ED25519 test passed!\n");
  1215. #endif
  1216. #ifdef HAVE_CURVE448
  1217. if ( (ret = curve448_test()) != 0)
  1218. return err_sys("CURVE448 test failed!\n", ret);
  1219. else
  1220. TEST_PASS("CURVE448 test passed!\n");
  1221. #endif
  1222. #ifdef HAVE_ED448
  1223. if ( (ret = ed448_test()) != 0)
  1224. return err_sys("ED448 test failed!\n", ret);
  1225. else
  1226. TEST_PASS("ED448 test passed!\n");
  1227. #endif
  1228. #ifdef WOLFSSL_HAVE_KYBER
  1229. if ( (ret = kyber_test()) != 0)
  1230. return err_sys("KYBER test failed!\n", ret);
  1231. else
  1232. TEST_PASS("KYBER test passed!\n");
  1233. #endif
  1234. #ifdef WOLFCRYPT_HAVE_ECCSI
  1235. if ( (ret = eccsi_test()) != 0)
  1236. return err_sys("ECCSI test failed!\n", ret);
  1237. else
  1238. TEST_PASS("ECCSI test passed!\n");
  1239. #endif
  1240. #ifdef WOLFCRYPT_HAVE_SAKKE
  1241. if ( (ret = sakke_test()) != 0)
  1242. return err_sys("SAKKE test failed!\n", ret);
  1243. else
  1244. TEST_PASS("SAKKE test passed!\n");
  1245. #endif
  1246. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  1247. if ( (ret = cmac_test()) != 0)
  1248. return err_sys("CMAC test failed!\n", ret);
  1249. else
  1250. TEST_PASS("CMAC test passed!\n");
  1251. #endif
  1252. #if defined(WOLFSSL_SIPHASH)
  1253. if ( (ret = siphash_test()) != 0)
  1254. return err_sys("SipHash test failed!\n", ret);
  1255. else
  1256. TEST_PASS("SipHash test passed!\n");
  1257. #endif
  1258. #ifdef HAVE_LIBZ
  1259. if ( (ret = compress_test()) != 0)
  1260. return err_sys("COMPRESS test failed!\n", ret);
  1261. else
  1262. TEST_PASS("COMPRESS test passed!\n");
  1263. #endif
  1264. #ifdef HAVE_PKCS7
  1265. #ifndef NO_PKCS7_ENCRYPTED_DATA
  1266. if ( (ret = pkcs7encrypted_test()) != 0)
  1267. return err_sys("PKCS7encrypted test failed!\n", ret);
  1268. else
  1269. TEST_PASS("PKCS7encrypted test passed!\n");
  1270. #endif
  1271. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  1272. if ( (ret = pkcs7compressed_test()) != 0)
  1273. return err_sys("PKCS7compressed test failed!\n", ret);
  1274. else
  1275. TEST_PASS("PKCS7compressed test passed!\n");
  1276. #endif
  1277. if ( (ret = pkcs7signed_test()) != 0)
  1278. return err_sys("PKCS7signed test failed!\n", ret);
  1279. else
  1280. TEST_PASS("PKCS7signed test passed!\n");
  1281. if ( (ret = pkcs7enveloped_test()) != 0)
  1282. return err_sys("PKCS7enveloped test failed!\n", ret);
  1283. else
  1284. TEST_PASS("PKCS7enveloped test passed!\n");
  1285. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  1286. if ( (ret = pkcs7authenveloped_test()) != 0)
  1287. return err_sys("PKCS7authenveloped test failed!\n", ret);
  1288. else
  1289. TEST_PASS("PKCS7authenveloped test passed!\n");
  1290. #endif
  1291. #endif
  1292. #ifdef HAVE_VALGRIND
  1293. if ( (ret = mp_test()) != 0)
  1294. return err_sys("mp test failed!\n", ret);
  1295. else
  1296. TEST_PASS("mp test passed!\n");
  1297. #endif
  1298. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  1299. if ( (ret = prime_test()) != 0)
  1300. return err_sys("prime test failed!\n", ret);
  1301. else
  1302. TEST_PASS("prime test passed!\n");
  1303. #endif
  1304. #if defined(ASN_BER_TO_DER) && \
  1305. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  1306. defined(OPENSSL_EXTRA_X509_SMALL))
  1307. if ( (ret = berder_test()) != 0)
  1308. return err_sys("ber-der test failed!\n", ret);
  1309. else
  1310. TEST_PASS("ber-der test passed!\n");
  1311. #endif
  1312. if ( (ret = logging_test()) != 0)
  1313. return err_sys("logging test failed!\n", ret);
  1314. else
  1315. TEST_PASS("logging test passed!\n");
  1316. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  1317. if ( (ret = time_test()) != 0)
  1318. return err_sys("time test failed!\n", ret);
  1319. else
  1320. TEST_PASS("time test passed!\n");
  1321. #endif
  1322. if ( (ret = mutex_test()) != 0)
  1323. return err_sys("mutex test failed!\n", ret);
  1324. else
  1325. TEST_PASS("mutex test passed!\n");
  1326. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  1327. if ( (ret = memcb_test()) != 0)
  1328. return err_sys("memcb test failed!\n", ret);
  1329. else
  1330. TEST_PASS("memcb test passed!\n");
  1331. #endif
  1332. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  1333. if ( (ret = blob_test()) != 0)
  1334. return err_sys("blob test failed!\n", ret);
  1335. else
  1336. TEST_PASS("blob test passed!\n");
  1337. #endif
  1338. #if defined(WOLF_CRYPTO_CB) && \
  1339. !(defined(HAVE_INTEL_QAT_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC) || \
  1340. defined(WOLFSSL_QNX_CAAM))
  1341. if ( (ret = cryptocb_test()) != 0)
  1342. return err_sys("crypto callback test failed!\n", ret);
  1343. else
  1344. TEST_PASS("crypto callback test passed!\n");
  1345. #endif
  1346. #ifdef WOLFSSL_CERT_PIV
  1347. if ( (ret = certpiv_test()) != 0)
  1348. return err_sys("cert piv test failed!\n", ret);
  1349. else
  1350. TEST_PASS("cert piv test passed!\n");
  1351. #endif
  1352. #ifdef WOLF_CRYPTO_CB
  1353. #ifdef HAVE_INTEL_QA_SYNC
  1354. wc_CryptoCb_CleanupIntelQa(&devId);
  1355. #endif
  1356. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  1357. wc_CryptoCb_CleanupOcteon(&devId);
  1358. #endif
  1359. #endif
  1360. #ifdef WOLFSSL_ASYNC_CRYPT
  1361. wolfAsync_DevClose(&devId);
  1362. #endif
  1363. /* cleanup the thread if fixed point cache is enabled and have thread local */
  1364. #if defined(HAVE_THREAD_LS) && defined(HAVE_ECC) && defined(FP_ECC)
  1365. wc_ecc_fp_free();
  1366. #endif
  1367. if (args)
  1368. ((func_args*)args)->return_code = ret;
  1369. TEST_PASS("Test complete\n");
  1370. EXIT_TEST(ret);
  1371. }
  1372. #ifndef NO_MAIN_DRIVER
  1373. #ifdef HAVE_WOLFCRYPT_TEST_OPTIONS
  1374. int myoptind = 0;
  1375. char* myoptarg = NULL;
  1376. #endif
  1377. /* so overall tests can pull in test function */
  1378. #if defined(WOLFSSL_ESPIDF) || defined(_WIN32_WCE)
  1379. int wolf_test_task(void)
  1380. #else
  1381. #ifndef NO_MAIN_FUNCTION
  1382. int main(int argc, char** argv)
  1383. {
  1384. return wolfcrypt_test_main(argc, argv);
  1385. }
  1386. #endif
  1387. int wolfcrypt_test_main(int argc, char** argv)
  1388. #endif
  1389. {
  1390. int ret;
  1391. func_args args;
  1392. #ifdef WOLFSSL_ESPIDF
  1393. /* set dummy wallclock time. */
  1394. struct timeval utctime;
  1395. struct timezone tz;
  1396. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1397. utctime.tv_usec = 0;
  1398. tz.tz_minuteswest = 0;
  1399. tz.tz_dsttime = 0;
  1400. settimeofday(&utctime, &tz);
  1401. #endif
  1402. #ifdef WOLFSSL_APACHE_MYNEWT
  1403. #ifdef ARCH_sim
  1404. mcu_sim_parse_args(argc, argv);
  1405. #endif
  1406. sysinit();
  1407. /* set dummy wallclock time. */
  1408. struct os_timeval utctime;
  1409. struct os_timezone tz;
  1410. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1411. utctime.tv_usec = 0;
  1412. tz.tz_minuteswest = 0;
  1413. tz.tz_dsttime = 0;
  1414. os_settimeofday(&utctime, &tz);
  1415. #endif
  1416. #ifdef WOLFSSL_ZEPHYR
  1417. /* set dummy wallclock time. */
  1418. struct timespec utctime;
  1419. utctime.tv_sec = 1521725159; /* dummy time: 2018-03-22T13:25:59+00:00 */
  1420. utctime.tv_nsec = 0;
  1421. clock_settime(CLOCK_REALTIME, &utctime);
  1422. #endif
  1423. #ifdef DEVKITPRO
  1424. void *framebuffer;
  1425. GXRModeObj *rmode = NULL;
  1426. VIDEO_Init();
  1427. WPAD_Init();
  1428. rmode = VIDEO_GetPreferredMode(NULL);
  1429. #pragma GCC diagnostic ignored "-Wbad-function-cast"
  1430. framebuffer = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
  1431. #pragma GCC diagnostic pop
  1432. console_init(framebuffer,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
  1433. VIDEO_Configure(rmode);
  1434. VIDEO_SetNextFramebuffer(framebuffer);
  1435. VIDEO_SetBlack(FALSE);
  1436. VIDEO_Flush();
  1437. VIDEO_WaitVSync();
  1438. if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
  1439. #endif
  1440. #ifdef HAVE_WNR
  1441. if (wc_InitNetRandom(wnrConfigFile, NULL, 5000) != 0) {
  1442. err_sys("Whitewood netRandom global config failed", -1001);
  1443. return -1002;
  1444. }
  1445. #endif
  1446. #ifndef WOLFSSL_ESPIDF
  1447. args.argc = argc;
  1448. args.argv = argv;
  1449. #endif
  1450. if ((ret = wolfCrypt_Init()) != 0) {
  1451. printf("wolfCrypt_Init failed %d\n", ret);
  1452. err_sys("Error with wolfCrypt_Init!\n", -1003);
  1453. }
  1454. #ifdef WC_RNG_SEED_CB
  1455. wc_SetSeed_Cb(wc_GenerateSeed);
  1456. #endif
  1457. #ifdef HAVE_STACK_SIZE
  1458. StackSizeCheck(&args, wolfcrypt_test);
  1459. #else
  1460. wolfcrypt_test(&args);
  1461. #endif
  1462. if ((ret = wolfCrypt_Cleanup()) != 0) {
  1463. printf("wolfCrypt_Cleanup failed %d\n", ret);
  1464. err_sys("Error with wolfCrypt_Cleanup!\n", -1004);
  1465. }
  1466. #ifdef HAVE_WNR
  1467. if (wc_FreeNetRandom() < 0)
  1468. err_sys("Failed to free netRandom context", -1005);
  1469. #endif /* HAVE_WNR */
  1470. #ifdef DOLPHIN_EMULATOR
  1471. /* Returning from main panics the emulator. Just hang
  1472. * and let the user force quit the emulator window. */
  1473. printf("args.return_code: %d\n", args.return_code);
  1474. printf("Testing complete. You may close the window now\n");
  1475. while (1);
  1476. #endif
  1477. #ifndef WOLFSSL_ESPIDF
  1478. printf("Exiting main with return code: %d\n", args.return_code);
  1479. return args.return_code;
  1480. #endif
  1481. }
  1482. #endif /* NO_MAIN_DRIVER */
  1483. /* helper to save DER, convert to PEM and save PEM */
  1484. #if !defined(NO_ASN) && (defined(HAVE_ECC) || !defined(NO_DSA) || \
  1485. (!defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)))) \
  1486. && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  1487. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1488. #define SaveDerAndPem(d, dSz, fD, fP, pT, eB) _SaveDerAndPem(d, dSz, fD, fP, pT, eB)
  1489. #else
  1490. #define SaveDerAndPem(d, dSz, fD, fP, pT, eB) _SaveDerAndPem(d, dSz, NULL, NULL, pT, eB)
  1491. #endif
  1492. static int _SaveDerAndPem(const byte* der, int derSz,
  1493. const char* fileDer, const char* filePem, int pemType, int errBase)
  1494. {
  1495. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1496. int ret;
  1497. XFILE derFile;
  1498. derFile = XFOPEN(fileDer, "wb");
  1499. if (!derFile) {
  1500. return errBase + 0;
  1501. }
  1502. ret = (int)XFWRITE(der, 1, derSz, derFile);
  1503. XFCLOSE(derFile);
  1504. if (ret != derSz) {
  1505. return errBase + 1;
  1506. }
  1507. #endif
  1508. #ifdef WOLFSSL_DER_TO_PEM
  1509. if (filePem) {
  1510. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1511. XFILE pemFile;
  1512. #endif
  1513. byte* pem;
  1514. int pemSz;
  1515. /* calculate PEM size */
  1516. pemSz = wc_DerToPem(der, derSz, NULL, 0, pemType);
  1517. if (pemSz < 0) {
  1518. return pemSz;
  1519. }
  1520. pem = (byte*)XMALLOC(pemSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1521. if (pem == NULL) {
  1522. return MEMORY_E;
  1523. }
  1524. /* Convert to PEM */
  1525. pemSz = wc_DerToPem(der, derSz, pem, pemSz, pemType);
  1526. if (pemSz < 0) {
  1527. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1528. return errBase + 2;
  1529. }
  1530. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  1531. pemFile = XFOPEN(filePem, "wb");
  1532. if (!pemFile) {
  1533. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1534. return errBase + 3;
  1535. }
  1536. ret = (int)XFWRITE(pem, 1, pemSz, pemFile);
  1537. XFCLOSE(pemFile);
  1538. if (ret != pemSz) {
  1539. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1540. return errBase + 4;
  1541. }
  1542. #endif
  1543. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1544. }
  1545. #endif /* WOLFSSL_DER_TO_PEM */
  1546. /* suppress unused variable warnings */
  1547. (void)der;
  1548. (void)derSz;
  1549. (void)filePem;
  1550. (void)fileDer;
  1551. (void)pemType;
  1552. (void)errBase;
  1553. return 0;
  1554. }
  1555. #endif /* WOLFSSL_KEY_GEN || WOLFSSL_CERT_GEN */
  1556. WOLFSSL_TEST_SUBROUTINE int error_test(void)
  1557. {
  1558. const char* errStr;
  1559. char out[WOLFSSL_MAX_ERROR_SZ];
  1560. const char* unknownStr = wc_GetErrorString(0);
  1561. #ifdef NO_ERROR_STRINGS
  1562. /* Ensure a valid error code's string matches an invalid code's.
  1563. * The string is that error strings are not available.
  1564. */
  1565. errStr = wc_GetErrorString(OPEN_RAN_E);
  1566. wc_ErrorString(OPEN_RAN_E, out);
  1567. if (XSTRCMP(errStr, unknownStr) != 0)
  1568. return -1100;
  1569. if (XSTRCMP(out, unknownStr) != 0)
  1570. return -1101;
  1571. #else
  1572. int i;
  1573. int j = 0;
  1574. /* Values that are not or no longer error codes. */
  1575. int missing[] = { -122, -123, -124, -127, -128, -129, -159,
  1576. -163, -164, -165, -166, -167, -168, -169, -233,
  1577. 0 };
  1578. /* Check that all errors have a string and it's the same through the two
  1579. * APIs. Check that the values that are not errors map to the unknown
  1580. * string.
  1581. */
  1582. for (i = MAX_CODE_E-1; i >= WC_LAST_E; i--) {
  1583. errStr = wc_GetErrorString(i);
  1584. wc_ErrorString(i, out);
  1585. if (i != missing[j]) {
  1586. if (XSTRCMP(errStr, unknownStr) == 0)
  1587. return -1102;
  1588. if (XSTRCMP(out, unknownStr) == 0)
  1589. return -1103;
  1590. if (XSTRCMP(errStr, out) != 0)
  1591. return -1104;
  1592. if (XSTRLEN(errStr) >= WOLFSSL_MAX_ERROR_SZ)
  1593. return -1105;
  1594. }
  1595. else {
  1596. j++;
  1597. if (XSTRCMP(errStr, unknownStr) != 0)
  1598. return -1106;
  1599. if (XSTRCMP(out, unknownStr) != 0)
  1600. return -1107;
  1601. }
  1602. }
  1603. /* Check if the next possible value has been given a string. */
  1604. errStr = wc_GetErrorString(i);
  1605. wc_ErrorString(i, out);
  1606. if (XSTRCMP(errStr, unknownStr) != 0)
  1607. return -1108;
  1608. if (XSTRCMP(out, unknownStr) != 0)
  1609. return -1109;
  1610. #endif
  1611. return 0;
  1612. }
  1613. #ifndef NO_CODING
  1614. WOLFSSL_TEST_SUBROUTINE int base64_test(void)
  1615. {
  1616. int ret;
  1617. WOLFSSL_SMALL_STACK_STATIC const byte good[] = "A+Gd\0\0\0";
  1618. WOLFSSL_SMALL_STACK_STATIC const byte goodEnd[] = "A+Gd \r\n";
  1619. WOLFSSL_SMALL_STACK_STATIC const byte good_spaces[] = " A + G d \0";
  1620. byte out[128];
  1621. word32 outLen;
  1622. #ifdef WOLFSSL_BASE64_ENCODE
  1623. byte data[3];
  1624. word32 dataLen;
  1625. byte longData[79] = { 0 };
  1626. WOLFSSL_SMALL_STACK_STATIC const byte symbols[] = "+/A=";
  1627. #endif
  1628. WOLFSSL_SMALL_STACK_STATIC const byte badSmall[] = "AAA!Gdj=";
  1629. WOLFSSL_SMALL_STACK_STATIC const byte badLarge[] = "AAA~Gdj=";
  1630. WOLFSSL_SMALL_STACK_STATIC const byte badEOL[] = "A+Gd!AA";
  1631. WOLFSSL_SMALL_STACK_STATIC const byte badPadding[] = "AA=A";
  1632. WOLFSSL_SMALL_STACK_STATIC const byte badChar[] = ",-.:;<=>?@[\\]^_`";
  1633. byte goodChar[] =
  1634. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1635. "abcdefghijklmnopqrstuvwxyz"
  1636. "0123456789+/;";
  1637. byte charTest[] = "A+Gd\0\0\0";
  1638. int i;
  1639. /* Good Base64 encodings. */
  1640. outLen = sizeof(out);
  1641. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  1642. if (ret != 0)
  1643. return -1200;
  1644. outLen = sizeof(out);
  1645. ret = Base64_Decode(goodEnd, sizeof(goodEnd), out, &outLen);
  1646. if (ret != 0)
  1647. return -1201;
  1648. outLen = sizeof(goodChar);
  1649. ret = Base64_Decode(goodChar, sizeof(goodChar), goodChar, &outLen);
  1650. if (ret != 0)
  1651. return -1235;
  1652. if (outLen != 64 / 4 * 3)
  1653. return -1236;
  1654. outLen = sizeof(out);
  1655. ret = Base64_Decode(good_spaces, sizeof(good_spaces), out, &outLen);
  1656. if (ret != 0)
  1657. return -1201;
  1658. /* Bad parameters. */
  1659. outLen = 1;
  1660. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  1661. if (ret != BAD_FUNC_ARG)
  1662. return -1202;
  1663. outLen = sizeof(out);
  1664. ret = Base64_Decode(badEOL, sizeof(badEOL), out, &outLen);
  1665. if (ret != ASN_INPUT_E)
  1666. return -1203;
  1667. outLen = sizeof(out);
  1668. ret = Base64_Decode(badPadding, sizeof(badPadding), out, &outLen);
  1669. if (ret != ASN_INPUT_E)
  1670. return -1203;
  1671. /* Bad character at each offset 0-3. */
  1672. for (i = 0; i < 4; i++) {
  1673. outLen = sizeof(out);
  1674. ret = Base64_Decode(badSmall + i, 4, out, &outLen);
  1675. if (ret != ASN_INPUT_E)
  1676. return -1204 - i;
  1677. ret = Base64_Decode(badLarge + i, 4, out, &outLen);
  1678. if (ret != ASN_INPUT_E)
  1679. return -1214 - i;
  1680. }
  1681. /* Invalid character less than 0x2b */
  1682. for (i = 1; i < 0x2b; i++) {
  1683. outLen = sizeof(out);
  1684. charTest[0] = (byte)i;
  1685. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1686. if (ret != ASN_INPUT_E)
  1687. return -1240 - i;
  1688. }
  1689. /* Bad characters in range 0x2b - 0x7a. */
  1690. for (i = 0; i < (int)sizeof(badChar) - 1; i++) {
  1691. outLen = sizeof(out);
  1692. charTest[0] = badChar[i];
  1693. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1694. if (ret != ASN_INPUT_E)
  1695. return -1270 - i;
  1696. }
  1697. /* Invalid character greater than 0x7a */
  1698. for (i = 0x7b; i < 0x100; i++) {
  1699. outLen = sizeof(out);
  1700. charTest[0] = (byte)i;
  1701. ret = Base64_Decode(charTest, sizeof(charTest), out, &outLen);
  1702. if (ret != ASN_INPUT_E)
  1703. return -1290 - i;
  1704. }
  1705. #ifdef WOLFSSL_BASE64_ENCODE
  1706. /* Decode and encode all symbols - non-alphanumeric. */
  1707. dataLen = sizeof(data);
  1708. ret = Base64_Decode(symbols, sizeof(symbols), data, &dataLen);
  1709. if (ret != 0)
  1710. return -1224;
  1711. outLen = sizeof(out);
  1712. ret = Base64_Encode(data, dataLen, NULL, &outLen);
  1713. if (ret != LENGTH_ONLY_E)
  1714. return -1225;
  1715. outLen = sizeof(out);
  1716. ret = Base64_Encode(data, dataLen, out, &outLen);
  1717. if (ret != 0)
  1718. return -1226;
  1719. outLen = 7;
  1720. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  1721. if (ret != BUFFER_E)
  1722. return -1227;
  1723. outLen = sizeof(out);
  1724. ret = Base64_EncodeEsc(data, dataLen, NULL, &outLen);
  1725. if (ret != LENGTH_ONLY_E)
  1726. return -1228;
  1727. outLen = sizeof(out);
  1728. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  1729. if (ret != 0)
  1730. return -1229;
  1731. outLen = sizeof(out);
  1732. ret = Base64_Encode_NoNl(data, dataLen, out, &outLen);
  1733. if (ret != 0)
  1734. return -1230;
  1735. /* Data that results in an encoding longer than one line. */
  1736. outLen = sizeof(out);
  1737. dataLen = sizeof(longData);
  1738. ret = Base64_Encode(longData, dataLen, out, &outLen);
  1739. if (ret != 0)
  1740. return -1231;
  1741. outLen = sizeof(out);
  1742. ret = Base64_EncodeEsc(longData, dataLen, out, &outLen);
  1743. if (ret != 0)
  1744. return -1232;
  1745. outLen = sizeof(out);
  1746. ret = Base64_Encode_NoNl(longData, dataLen, out, &outLen);
  1747. if (ret != 0)
  1748. return -1233;
  1749. #endif
  1750. return 0;
  1751. }
  1752. #ifdef WOLFSSL_BASE16
  1753. WOLFSSL_TEST_SUBROUTINE int base16_test(void)
  1754. {
  1755. int ret;
  1756. WOLFSSL_SMALL_STACK_STATIC const byte testData[] = "SomeDataToEncode\n";
  1757. WOLFSSL_SMALL_STACK_STATIC const byte encodedTestData[] = "536F6D6544617461546F456E636F64650A00";
  1758. byte encoded[40];
  1759. word32 encodedLen;
  1760. byte plain[40];
  1761. word32 len;
  1762. /* length returned includes null termination */
  1763. encodedLen = sizeof(encoded);
  1764. ret = Base16_Encode(testData, sizeof(testData), encoded, &encodedLen);
  1765. if (ret != 0)
  1766. return -1300;
  1767. len = (word32)XSTRLEN((char*)encoded);
  1768. if (len != encodedLen - 1)
  1769. return -1301;
  1770. len = sizeof(plain);
  1771. ret = Base16_Decode(encoded, encodedLen - 1, plain, &len);
  1772. if (ret != 0)
  1773. return -1302;
  1774. if (len != sizeof(testData) || XMEMCMP(testData, plain, len) != 0)
  1775. return -1303;
  1776. if (encodedLen != sizeof(encodedTestData) ||
  1777. XMEMCMP(encoded, encodedTestData, encodedLen) != 0) {
  1778. return -1304;
  1779. }
  1780. return 0;
  1781. }
  1782. #endif /* WOLFSSL_BASE16 */
  1783. #endif /* !NO_CODING */
  1784. #ifndef NO_ASN
  1785. WOLFSSL_TEST_SUBROUTINE int asn_test(void)
  1786. {
  1787. int ret;
  1788. /* ASN1 encoded date buffer */
  1789. WOLFSSL_SMALL_STACK_STATIC const byte dateBuf[] = {0x17, 0x0d, 0x31, 0x36, 0x30, 0x38, 0x31, 0x31,
  1790. 0x32, 0x30, 0x30, 0x37, 0x33, 0x37, 0x5a};
  1791. byte format;
  1792. int length;
  1793. const byte* datePart;
  1794. #ifndef NO_ASN_TIME
  1795. struct tm timearg;
  1796. time_t now;
  1797. #endif
  1798. ret = wc_GetDateInfo(dateBuf, (int)sizeof(dateBuf), &datePart, &format,
  1799. &length);
  1800. if (ret != 0)
  1801. return -1400;
  1802. #ifndef NO_ASN_TIME
  1803. /* Parameter Validation tests. */
  1804. if (wc_GetTime(NULL, sizeof(now)) != BAD_FUNC_ARG)
  1805. return -1401;
  1806. if (wc_GetTime(&now, 0) != BUFFER_E)
  1807. return -1402;
  1808. now = 0;
  1809. if (wc_GetTime(&now, sizeof(now)) != 0) {
  1810. return -1403;
  1811. }
  1812. if (now == 0) {
  1813. printf("RTC/Time not set!\n");
  1814. return -1404;
  1815. }
  1816. ret = wc_GetDateAsCalendarTime(datePart, length, format, &timearg);
  1817. if (ret != 0)
  1818. return -1405;
  1819. #endif /* !NO_ASN_TIME */
  1820. return 0;
  1821. }
  1822. #endif /* !NO_ASN */
  1823. #ifdef WOLFSSL_MD2
  1824. WOLFSSL_TEST_SUBROUTINE int md2_test(void)
  1825. {
  1826. int ret = 0;
  1827. Md2 md2;
  1828. byte hash[MD2_DIGEST_SIZE];
  1829. testVector a, b, c, d, e, f, g;
  1830. testVector test_md2[7];
  1831. int times = sizeof(test_md2) / sizeof(testVector), i;
  1832. a.input = "";
  1833. a.output = "\x83\x50\xe5\xa3\xe2\x4c\x15\x3d\xf2\x27\x5c\x9f\x80\x69"
  1834. "\x27\x73";
  1835. a.inLen = XSTRLEN(a.input);
  1836. a.outLen = MD2_DIGEST_SIZE;
  1837. b.input = "a";
  1838. b.output = "\x32\xec\x01\xec\x4a\x6d\xac\x72\xc0\xab\x96\xfb\x34\xc0"
  1839. "\xb5\xd1";
  1840. b.inLen = XSTRLEN(b.input);
  1841. b.outLen = MD2_DIGEST_SIZE;
  1842. c.input = "abc";
  1843. c.output = "\xda\x85\x3b\x0d\x3f\x88\xd9\x9b\x30\x28\x3a\x69\xe6\xde"
  1844. "\xd6\xbb";
  1845. c.inLen = XSTRLEN(c.input);
  1846. c.outLen = MD2_DIGEST_SIZE;
  1847. d.input = "message digest";
  1848. d.output = "\xab\x4f\x49\x6b\xfb\x2a\x53\x0b\x21\x9f\xf3\x30\x31\xfe"
  1849. "\x06\xb0";
  1850. d.inLen = XSTRLEN(d.input);
  1851. d.outLen = MD2_DIGEST_SIZE;
  1852. e.input = "abcdefghijklmnopqrstuvwxyz";
  1853. e.output = "\x4e\x8d\xdf\xf3\x65\x02\x92\xab\x5a\x41\x08\xc3\xaa\x47"
  1854. "\x94\x0b";
  1855. e.inLen = XSTRLEN(e.input);
  1856. e.outLen = MD2_DIGEST_SIZE;
  1857. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  1858. "6789";
  1859. f.output = "\xda\x33\xde\xf2\xa4\x2d\xf1\x39\x75\x35\x28\x46\xc3\x03"
  1860. "\x38\xcd";
  1861. f.inLen = XSTRLEN(f.input);
  1862. f.outLen = MD2_DIGEST_SIZE;
  1863. g.input = "1234567890123456789012345678901234567890123456789012345678"
  1864. "9012345678901234567890";
  1865. g.output = "\xd5\x97\x6f\x79\xd8\x3d\x3a\x0d\xc9\x80\x6c\x3c\x66\xf3"
  1866. "\xef\xd8";
  1867. g.inLen = XSTRLEN(g.input);
  1868. g.outLen = MD2_DIGEST_SIZE;
  1869. test_md2[0] = a;
  1870. test_md2[1] = b;
  1871. test_md2[2] = c;
  1872. test_md2[3] = d;
  1873. test_md2[4] = e;
  1874. test_md2[5] = f;
  1875. test_md2[6] = g;
  1876. wc_InitMd2(&md2);
  1877. for (i = 0; i < times; ++i) {
  1878. wc_Md2Update(&md2, (byte*)test_md2[i].input, (word32)test_md2[i].inLen);
  1879. wc_Md2Final(&md2, hash);
  1880. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0)
  1881. return -1500 - i;
  1882. }
  1883. for (i = 0; i < times; ++i) {
  1884. ret = wc_Md2Hash((byte*)test_md2[i].input, (word32)test_md2[i].inLen, hash);
  1885. if (ret != 0) {
  1886. return -1507 - i;
  1887. }
  1888. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0) {
  1889. return -1507 - i;
  1890. }
  1891. }
  1892. return 0;
  1893. }
  1894. #endif
  1895. #ifndef NO_MD5
  1896. WOLFSSL_TEST_SUBROUTINE int md5_test(void)
  1897. {
  1898. int ret = 0;
  1899. wc_Md5 md5, md5Copy;
  1900. byte hash[WC_MD5_DIGEST_SIZE];
  1901. byte hashcopy[WC_MD5_DIGEST_SIZE];
  1902. testVector a, b, c, d, e, f;
  1903. testVector test_md5[6];
  1904. int times = sizeof(test_md5) / sizeof(testVector), i;
  1905. a.input = "";
  1906. a.output = "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42"
  1907. "\x7e";
  1908. a.inLen = XSTRLEN(a.input);
  1909. a.outLen = WC_MD5_DIGEST_SIZE;
  1910. b.input = "abc";
  1911. b.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  1912. "\x72";
  1913. b.inLen = XSTRLEN(b.input);
  1914. b.outLen = WC_MD5_DIGEST_SIZE;
  1915. c.input = "message digest";
  1916. c.output = "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
  1917. "\xd0";
  1918. c.inLen = XSTRLEN(c.input);
  1919. c.outLen = WC_MD5_DIGEST_SIZE;
  1920. d.input = "abcdefghijklmnopqrstuvwxyz";
  1921. d.output = "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
  1922. "\x3b";
  1923. d.inLen = XSTRLEN(d.input);
  1924. d.outLen = WC_MD5_DIGEST_SIZE;
  1925. e.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  1926. "6789";
  1927. e.output = "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
  1928. "\x9f";
  1929. e.inLen = XSTRLEN(e.input);
  1930. e.outLen = WC_MD5_DIGEST_SIZE;
  1931. f.input = "1234567890123456789012345678901234567890123456789012345678"
  1932. "9012345678901234567890";
  1933. f.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  1934. "\x7a";
  1935. f.inLen = XSTRLEN(f.input);
  1936. f.outLen = WC_MD5_DIGEST_SIZE;
  1937. test_md5[0] = a;
  1938. test_md5[1] = b;
  1939. test_md5[2] = c;
  1940. test_md5[3] = d;
  1941. test_md5[4] = e;
  1942. test_md5[5] = f;
  1943. ret = wc_InitMd5_ex(&md5, HEAP_HINT, devId);
  1944. if (ret != 0)
  1945. return -1600;
  1946. ret = wc_InitMd5_ex(&md5Copy, HEAP_HINT, devId);
  1947. if (ret != 0) {
  1948. wc_Md5Free(&md5);
  1949. return -1601;
  1950. }
  1951. for (i = 0; i < times; ++i) {
  1952. ret = wc_Md5Update(&md5, (byte*)test_md5[i].input,
  1953. (word32)test_md5[i].inLen);
  1954. if (ret != 0)
  1955. ERROR_OUT(-1602 - i, exit);
  1956. ret = wc_Md5GetHash(&md5, hashcopy);
  1957. if (ret != 0)
  1958. ERROR_OUT(-1603 - i, exit);
  1959. ret = wc_Md5Copy(&md5, &md5Copy);
  1960. if (ret != 0)
  1961. ERROR_OUT(-1604 - i, exit);
  1962. ret = wc_Md5Final(&md5, hash);
  1963. if (ret != 0)
  1964. ERROR_OUT(-1605 - i, exit);
  1965. wc_Md5Free(&md5Copy);
  1966. if (XMEMCMP(hash, test_md5[i].output, WC_MD5_DIGEST_SIZE) != 0)
  1967. ERROR_OUT(-1606 - i, exit);
  1968. if (XMEMCMP(hash, hashcopy, WC_MD5_DIGEST_SIZE) != 0)
  1969. ERROR_OUT(-1607 - i, exit);
  1970. }
  1971. /* BEGIN LARGE HASH TEST */ {
  1972. byte large_input[1024];
  1973. const char* large_digest =
  1974. "\x44\xd0\x88\xce\xf1\x36\xd1\x78\xe9\xc8\xba\x84\xc3\xfd\xf6\xca";
  1975. for (i = 0; i < (int)sizeof(large_input); i++) {
  1976. large_input[i] = (byte)(i & 0xFF);
  1977. }
  1978. times = 100;
  1979. #ifdef WOLFSSL_PIC32MZ_HASH
  1980. wc_Md5SizeSet(&md5, times * sizeof(large_input));
  1981. #endif
  1982. for (i = 0; i < times; ++i) {
  1983. ret = wc_Md5Update(&md5, (byte*)large_input,
  1984. (word32)sizeof(large_input));
  1985. if (ret != 0)
  1986. ERROR_OUT(-1608, exit);
  1987. }
  1988. ret = wc_Md5Final(&md5, hash);
  1989. if (ret != 0)
  1990. ERROR_OUT(-1609, exit);
  1991. if (XMEMCMP(hash, large_digest, WC_MD5_DIGEST_SIZE) != 0)
  1992. ERROR_OUT(-1610, exit);
  1993. } /* END LARGE HASH TEST */
  1994. exit:
  1995. wc_Md5Free(&md5);
  1996. wc_Md5Free(&md5Copy);
  1997. return ret;
  1998. }
  1999. #endif /* NO_MD5 */
  2000. #ifndef NO_MD4
  2001. WOLFSSL_TEST_SUBROUTINE int md4_test(void)
  2002. {
  2003. Md4 md4;
  2004. byte hash[MD4_DIGEST_SIZE];
  2005. testVector a, b, c, d, e, f, g;
  2006. testVector test_md4[7];
  2007. int times = sizeof(test_md4) / sizeof(testVector), i;
  2008. a.input = "";
  2009. a.output = "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"
  2010. "\xc0";
  2011. a.inLen = XSTRLEN(a.input);
  2012. a.outLen = MD4_DIGEST_SIZE;
  2013. b.input = "a";
  2014. b.output = "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"
  2015. "\x24";
  2016. b.inLen = XSTRLEN(b.input);
  2017. b.outLen = MD4_DIGEST_SIZE;
  2018. c.input = "abc";
  2019. c.output = "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"
  2020. "\x9d";
  2021. c.inLen = XSTRLEN(c.input);
  2022. c.outLen = MD4_DIGEST_SIZE;
  2023. d.input = "message digest";
  2024. d.output = "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"
  2025. "\x4b";
  2026. d.inLen = XSTRLEN(d.input);
  2027. d.outLen = MD4_DIGEST_SIZE;
  2028. e.input = "abcdefghijklmnopqrstuvwxyz";
  2029. e.output = "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"
  2030. "\xa9";
  2031. e.inLen = XSTRLEN(e.input);
  2032. e.outLen = MD4_DIGEST_SIZE;
  2033. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  2034. "6789";
  2035. f.output = "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"
  2036. "\xe4";
  2037. f.inLen = XSTRLEN(f.input);
  2038. f.outLen = MD4_DIGEST_SIZE;
  2039. g.input = "1234567890123456789012345678901234567890123456789012345678"
  2040. "9012345678901234567890";
  2041. g.output = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"
  2042. "\x36";
  2043. g.inLen = XSTRLEN(g.input);
  2044. g.outLen = MD4_DIGEST_SIZE;
  2045. test_md4[0] = a;
  2046. test_md4[1] = b;
  2047. test_md4[2] = c;
  2048. test_md4[3] = d;
  2049. test_md4[4] = e;
  2050. test_md4[5] = f;
  2051. test_md4[6] = g;
  2052. wc_InitMd4(&md4);
  2053. for (i = 0; i < times; ++i) {
  2054. wc_Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen);
  2055. wc_Md4Final(&md4, hash);
  2056. if (XMEMCMP(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0)
  2057. return -1700 - i;
  2058. }
  2059. return 0;
  2060. }
  2061. #endif /* NO_MD4 */
  2062. #ifndef NO_SHA
  2063. WOLFSSL_TEST_SUBROUTINE int sha_test(void)
  2064. {
  2065. int ret = 0;
  2066. wc_Sha sha, shaCopy;
  2067. byte hash[WC_SHA_DIGEST_SIZE];
  2068. byte hashcopy[WC_SHA_DIGEST_SIZE];
  2069. testVector a, b, c, d, e;
  2070. testVector test_sha[5];
  2071. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2072. a.input = "";
  2073. a.output = "\xda\x39\xa3\xee\x5e\x6b\x4b\x0d\x32\x55\xbf\xef\x95\x60\x18"
  2074. "\x90\xaf\xd8\x07\x09";
  2075. a.inLen = XSTRLEN(a.input);
  2076. a.outLen = WC_SHA_DIGEST_SIZE;
  2077. b.input = "abc";
  2078. b.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  2079. "\x6C\x9C\xD0\xD8\x9D";
  2080. b.inLen = XSTRLEN(b.input);
  2081. b.outLen = WC_SHA_DIGEST_SIZE;
  2082. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2083. c.output = "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
  2084. "\xE5\xE5\x46\x70\xF1";
  2085. c.inLen = XSTRLEN(c.input);
  2086. c.outLen = WC_SHA_DIGEST_SIZE;
  2087. d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2088. "aaaaaa";
  2089. d.output = "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
  2090. "\x2A\x25\xEC\x64\x4D";
  2091. d.inLen = XSTRLEN(d.input);
  2092. d.outLen = WC_SHA_DIGEST_SIZE;
  2093. e.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2094. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2095. "aaaaaaaaaa";
  2096. e.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  2097. "\x53\x99\x5E\x26\xA0";
  2098. e.inLen = XSTRLEN(e.input);
  2099. e.outLen = WC_SHA_DIGEST_SIZE;
  2100. test_sha[0] = a;
  2101. test_sha[1] = b;
  2102. test_sha[2] = c;
  2103. test_sha[3] = d;
  2104. test_sha[4] = e;
  2105. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  2106. if (ret != 0)
  2107. return -1800;
  2108. ret = wc_InitSha_ex(&shaCopy, HEAP_HINT, devId);
  2109. if (ret != 0) {
  2110. wc_ShaFree(&sha);
  2111. return -1801;
  2112. }
  2113. for (i = 0; i < times; ++i) {
  2114. ret = wc_ShaUpdate(&sha, (byte*)test_sha[i].input,
  2115. (word32)test_sha[i].inLen);
  2116. if (ret != 0)
  2117. ERROR_OUT(-1802 - i, exit);
  2118. ret = wc_ShaGetHash(&sha, hashcopy);
  2119. if (ret != 0)
  2120. ERROR_OUT(-1803 - i, exit);
  2121. ret = wc_ShaCopy(&sha, &shaCopy);
  2122. if (ret != 0)
  2123. ERROR_OUT(-1804 - i, exit);
  2124. ret = wc_ShaFinal(&sha, hash);
  2125. if (ret != 0)
  2126. ERROR_OUT(-1805 - i, exit);
  2127. wc_ShaFree(&shaCopy);
  2128. if (XMEMCMP(hash, test_sha[i].output, WC_SHA_DIGEST_SIZE) != 0)
  2129. ERROR_OUT(-1806 - i, exit);
  2130. if (XMEMCMP(hash, hashcopy, WC_SHA_DIGEST_SIZE) != 0)
  2131. ERROR_OUT(-1807 - i, exit);
  2132. }
  2133. /* BEGIN LARGE HASH TEST */ {
  2134. byte large_input[1024];
  2135. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT)
  2136. const char* large_digest =
  2137. "\x1d\x6a\x5a\xf6\xe5\x7c\x86\xce\x7f\x7c\xaf\xd5\xdb\x08\xcd\x59"
  2138. "\x15\x8c\x6d\xb6";
  2139. #else
  2140. const char* large_digest =
  2141. "\x8b\x77\x02\x48\x39\xe8\xdb\xd3\x9a\xf4\x05\x24\x66\x12\x2d\x9e"
  2142. "\xc5\xd9\x0a\xac";
  2143. #endif
  2144. for (i = 0; i < (int)sizeof(large_input); i++) {
  2145. large_input[i] = (byte)(i & 0xFF);
  2146. }
  2147. #if defined(WOLFSSL_RENESAS_TSIP) || defined(WOLFSSL_RENESAS_SCEPROTECT)
  2148. times = 20;
  2149. #else
  2150. times = 100;
  2151. #endif
  2152. #ifdef WOLFSSL_PIC32MZ_HASH
  2153. wc_ShaSizeSet(&sha, times * sizeof(large_input));
  2154. #endif
  2155. for (i = 0; i < times; ++i) {
  2156. ret = wc_ShaUpdate(&sha, (byte*)large_input,
  2157. (word32)sizeof(large_input));
  2158. if (ret != 0)
  2159. ERROR_OUT(-1808, exit);
  2160. }
  2161. ret = wc_ShaFinal(&sha, hash);
  2162. if (ret != 0)
  2163. ERROR_OUT(-1809, exit);
  2164. if (XMEMCMP(hash, large_digest, WC_SHA_DIGEST_SIZE) != 0)
  2165. ERROR_OUT(-1810, exit);
  2166. } /* END LARGE HASH TEST */
  2167. exit:
  2168. wc_ShaFree(&sha);
  2169. wc_ShaFree(&shaCopy);
  2170. return ret;
  2171. }
  2172. #endif /* NO_SHA */
  2173. #ifdef WOLFSSL_RIPEMD
  2174. WOLFSSL_TEST_SUBROUTINE int ripemd_test(void)
  2175. {
  2176. RipeMd ripemd;
  2177. int ret;
  2178. byte hash[RIPEMD_DIGEST_SIZE];
  2179. testVector a, b, c, d;
  2180. testVector test_ripemd[4];
  2181. int times = sizeof(test_ripemd) / sizeof(struct testVector), i;
  2182. a.input = "abc";
  2183. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  2184. "\xb0\x87\xf1\x5a\x0b\xfc";
  2185. a.inLen = XSTRLEN(a.input);
  2186. a.outLen = RIPEMD_DIGEST_SIZE;
  2187. b.input = "message digest";
  2188. b.output = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
  2189. "\x5f\xfa\x21\x59\x5f\x36";
  2190. b.inLen = XSTRLEN(b.input);
  2191. b.outLen = RIPEMD_DIGEST_SIZE;
  2192. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2193. c.output = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
  2194. "\xf4\x9a\xda\x62\xeb\x2b";
  2195. c.inLen = XSTRLEN(c.input);
  2196. c.outLen = RIPEMD_DIGEST_SIZE;
  2197. d.input = "12345678901234567890123456789012345678901234567890123456"
  2198. "789012345678901234567890";
  2199. d.output = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
  2200. "\x82\xbf\x63\x32\x6b\xfb";
  2201. d.inLen = XSTRLEN(d.input);
  2202. d.outLen = RIPEMD_DIGEST_SIZE;
  2203. test_ripemd[0] = a;
  2204. test_ripemd[1] = b;
  2205. test_ripemd[2] = c;
  2206. test_ripemd[3] = d;
  2207. ret = wc_InitRipeMd(&ripemd);
  2208. if (ret != 0) {
  2209. return -1900;
  2210. }
  2211. for (i = 0; i < times; ++i) {
  2212. ret = wc_RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input,
  2213. (word32)test_ripemd[i].inLen);
  2214. if (ret != 0) {
  2215. return -1901 - i;
  2216. }
  2217. ret = wc_RipeMdFinal(&ripemd, hash);
  2218. if (ret != 0) {
  2219. return -1911 - i;
  2220. }
  2221. if (XMEMCMP(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0)
  2222. return -1921 - i;
  2223. }
  2224. return 0;
  2225. }
  2226. #endif /* WOLFSSL_RIPEMD */
  2227. #ifdef HAVE_BLAKE2
  2228. #define BLAKE2B_TESTS 3
  2229. static const byte blake2b_vec[BLAKE2B_TESTS][BLAKE2B_OUTBYTES] =
  2230. {
  2231. {
  2232. 0x78, 0x6A, 0x02, 0xF7, 0x42, 0x01, 0x59, 0x03,
  2233. 0xC6, 0xC6, 0xFD, 0x85, 0x25, 0x52, 0xD2, 0x72,
  2234. 0x91, 0x2F, 0x47, 0x40, 0xE1, 0x58, 0x47, 0x61,
  2235. 0x8A, 0x86, 0xE2, 0x17, 0xF7, 0x1F, 0x54, 0x19,
  2236. 0xD2, 0x5E, 0x10, 0x31, 0xAF, 0xEE, 0x58, 0x53,
  2237. 0x13, 0x89, 0x64, 0x44, 0x93, 0x4E, 0xB0, 0x4B,
  2238. 0x90, 0x3A, 0x68, 0x5B, 0x14, 0x48, 0xB7, 0x55,
  2239. 0xD5, 0x6F, 0x70, 0x1A, 0xFE, 0x9B, 0xE2, 0xCE
  2240. },
  2241. {
  2242. 0x2F, 0xA3, 0xF6, 0x86, 0xDF, 0x87, 0x69, 0x95,
  2243. 0x16, 0x7E, 0x7C, 0x2E, 0x5D, 0x74, 0xC4, 0xC7,
  2244. 0xB6, 0xE4, 0x8F, 0x80, 0x68, 0xFE, 0x0E, 0x44,
  2245. 0x20, 0x83, 0x44, 0xD4, 0x80, 0xF7, 0x90, 0x4C,
  2246. 0x36, 0x96, 0x3E, 0x44, 0x11, 0x5F, 0xE3, 0xEB,
  2247. 0x2A, 0x3A, 0xC8, 0x69, 0x4C, 0x28, 0xBC, 0xB4,
  2248. 0xF5, 0xA0, 0xF3, 0x27, 0x6F, 0x2E, 0x79, 0x48,
  2249. 0x7D, 0x82, 0x19, 0x05, 0x7A, 0x50, 0x6E, 0x4B
  2250. },
  2251. {
  2252. 0x1C, 0x08, 0x79, 0x8D, 0xC6, 0x41, 0xAB, 0xA9,
  2253. 0xDE, 0xE4, 0x35, 0xE2, 0x25, 0x19, 0xA4, 0x72,
  2254. 0x9A, 0x09, 0xB2, 0xBF, 0xE0, 0xFF, 0x00, 0xEF,
  2255. 0x2D, 0xCD, 0x8E, 0xD6, 0xF8, 0xA0, 0x7D, 0x15,
  2256. 0xEA, 0xF4, 0xAE, 0xE5, 0x2B, 0xBF, 0x18, 0xAB,
  2257. 0x56, 0x08, 0xA6, 0x19, 0x0F, 0x70, 0xB9, 0x04,
  2258. 0x86, 0xC8, 0xA7, 0xD4, 0x87, 0x37, 0x10, 0xB1,
  2259. 0x11, 0x5D, 0x3D, 0xEB, 0xBB, 0x43, 0x27, 0xB5
  2260. }
  2261. };
  2262. WOLFSSL_TEST_SUBROUTINE int blake2b_test(void)
  2263. {
  2264. Blake2b b2b;
  2265. byte digest[64];
  2266. byte input[64];
  2267. int i, ret;
  2268. for (i = 0; i < (int)sizeof(input); i++)
  2269. input[i] = (byte)i;
  2270. for (i = 0; i < BLAKE2B_TESTS; i++) {
  2271. ret = wc_InitBlake2b(&b2b, 64);
  2272. if (ret != 0)
  2273. return -2000 - i;
  2274. ret = wc_Blake2bUpdate(&b2b, input, i);
  2275. if (ret != 0)
  2276. return -2010 - 1;
  2277. ret = wc_Blake2bFinal(&b2b, digest, 64);
  2278. if (ret != 0)
  2279. return -2020 - i;
  2280. if (XMEMCMP(digest, blake2b_vec[i], 64) != 0) {
  2281. return -2030 - i;
  2282. }
  2283. }
  2284. return 0;
  2285. }
  2286. #endif /* HAVE_BLAKE2 */
  2287. #ifdef HAVE_BLAKE2S
  2288. #define BLAKE2S_TESTS 3
  2289. static const byte blake2s_vec[BLAKE2S_TESTS][BLAKE2S_OUTBYTES] =
  2290. {
  2291. {
  2292. 0x69, 0x21, 0x7a, 0x30, 0x79, 0x90, 0x80, 0x94,
  2293. 0xe1, 0x11, 0x21, 0xd0, 0x42, 0x35, 0x4a, 0x7c,
  2294. 0x1f, 0x55, 0xb6, 0x48, 0x2c, 0xa1, 0xa5, 0x1e,
  2295. 0x1b, 0x25, 0x0d, 0xfd, 0x1e, 0xd0, 0xee, 0xf9,
  2296. },
  2297. {
  2298. 0xe3, 0x4d, 0x74, 0xdb, 0xaf, 0x4f, 0xf4, 0xc6,
  2299. 0xab, 0xd8, 0x71, 0xcc, 0x22, 0x04, 0x51, 0xd2,
  2300. 0xea, 0x26, 0x48, 0x84, 0x6c, 0x77, 0x57, 0xfb,
  2301. 0xaa, 0xc8, 0x2f, 0xe5, 0x1a, 0xd6, 0x4b, 0xea,
  2302. },
  2303. {
  2304. 0xdd, 0xad, 0x9a, 0xb1, 0x5d, 0xac, 0x45, 0x49,
  2305. 0xba, 0x42, 0xf4, 0x9d, 0x26, 0x24, 0x96, 0xbe,
  2306. 0xf6, 0xc0, 0xba, 0xe1, 0xdd, 0x34, 0x2a, 0x88,
  2307. 0x08, 0xf8, 0xea, 0x26, 0x7c, 0x6e, 0x21, 0x0c,
  2308. }
  2309. };
  2310. WOLFSSL_TEST_SUBROUTINE int blake2s_test(void)
  2311. {
  2312. Blake2s b2s;
  2313. byte digest[32];
  2314. byte input[64];
  2315. int i, ret;
  2316. for (i = 0; i < (int)sizeof(input); i++)
  2317. input[i] = (byte)i;
  2318. for (i = 0; i < BLAKE2S_TESTS; i++) {
  2319. ret = wc_InitBlake2s(&b2s, 32);
  2320. if (ret != 0)
  2321. return -2100 - i;
  2322. ret = wc_Blake2sUpdate(&b2s, input, i);
  2323. if (ret != 0)
  2324. return -2110 - 1;
  2325. ret = wc_Blake2sFinal(&b2s, digest, 32);
  2326. if (ret != 0)
  2327. return -2120 - i;
  2328. if (XMEMCMP(digest, blake2s_vec[i], 32) != 0) {
  2329. return -2130 - i;
  2330. }
  2331. }
  2332. return 0;
  2333. }
  2334. #endif /* HAVE_BLAKE2S */
  2335. #ifdef WOLFSSL_SHA224
  2336. WOLFSSL_TEST_SUBROUTINE int sha224_test(void)
  2337. {
  2338. wc_Sha224 sha, shaCopy;
  2339. byte hash[WC_SHA224_DIGEST_SIZE];
  2340. byte hashcopy[WC_SHA224_DIGEST_SIZE];
  2341. int ret = 0;
  2342. testVector a, b, c;
  2343. testVector test_sha[3];
  2344. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2345. a.input = "";
  2346. a.output = "\xd1\x4a\x02\x8c\x2a\x3a\x2b\xc9\x47\x61\x02\xbb\x28\x82\x34"
  2347. "\xc4\x15\xa2\xb0\x1f\x82\x8e\xa6\x2a\xc5\xb3\xe4\x2f";
  2348. a.inLen = XSTRLEN(a.input);
  2349. a.outLen = WC_SHA224_DIGEST_SIZE;
  2350. b.input = "abc";
  2351. b.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2\x55"
  2352. "\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  2353. b.inLen = XSTRLEN(b.input);
  2354. b.outLen = WC_SHA224_DIGEST_SIZE;
  2355. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2356. c.output = "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  2357. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  2358. c.inLen = XSTRLEN(c.input);
  2359. c.outLen = WC_SHA224_DIGEST_SIZE;
  2360. test_sha[0] = a;
  2361. test_sha[1] = b;
  2362. test_sha[2] = c;
  2363. ret = wc_InitSha224_ex(&sha, HEAP_HINT, devId);
  2364. if (ret != 0)
  2365. return -2200;
  2366. ret = wc_InitSha224_ex(&shaCopy, HEAP_HINT, devId);
  2367. if (ret != 0) {
  2368. wc_Sha224Free(&sha);
  2369. return -2201;
  2370. }
  2371. for (i = 0; i < times; ++i) {
  2372. ret = wc_Sha224Update(&sha, (byte*)test_sha[i].input,
  2373. (word32)test_sha[i].inLen);
  2374. if (ret != 0)
  2375. ERROR_OUT(-2202 - i, exit);
  2376. ret = wc_Sha224GetHash(&sha, hashcopy);
  2377. if (ret != 0)
  2378. ERROR_OUT(-2203 - i, exit);
  2379. ret = wc_Sha224Copy(&sha, &shaCopy);
  2380. if (ret != 0)
  2381. ERROR_OUT(-2204 - i, exit);
  2382. ret = wc_Sha224Final(&sha, hash);
  2383. if (ret != 0)
  2384. ERROR_OUT(-2205 - i, exit);
  2385. wc_Sha224Free(&shaCopy);
  2386. if (XMEMCMP(hash, test_sha[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  2387. ERROR_OUT(-2206 - i, exit);
  2388. if (XMEMCMP(hash, hashcopy, WC_SHA224_DIGEST_SIZE) != 0)
  2389. ERROR_OUT(-2207 - i, exit);
  2390. }
  2391. exit:
  2392. wc_Sha224Free(&sha);
  2393. wc_Sha224Free(&shaCopy);
  2394. return ret;
  2395. }
  2396. #endif
  2397. #ifndef NO_SHA256
  2398. WOLFSSL_TEST_SUBROUTINE int sha256_test(void)
  2399. {
  2400. wc_Sha256 sha, shaCopy;
  2401. byte hash[WC_SHA256_DIGEST_SIZE];
  2402. byte hashcopy[WC_SHA256_DIGEST_SIZE];
  2403. int ret = 0;
  2404. testVector a, b, c;
  2405. testVector test_sha[3];
  2406. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2407. a.input = "";
  2408. a.output = "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9"
  2409. "\x24\x27\xae\x41\xe4\x64\x9b\x93\x4c\xa4\x95\x99\x1b\x78\x52"
  2410. "\xb8\x55";
  2411. a.inLen = XSTRLEN(a.input);
  2412. a.outLen = WC_SHA256_DIGEST_SIZE;
  2413. b.input = "abc";
  2414. b.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  2415. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  2416. "\x15\xAD";
  2417. b.inLen = XSTRLEN(b.input);
  2418. b.outLen = WC_SHA256_DIGEST_SIZE;
  2419. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2420. c.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  2421. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  2422. "\x06\xC1";
  2423. c.inLen = XSTRLEN(c.input);
  2424. c.outLen = WC_SHA256_DIGEST_SIZE;
  2425. test_sha[0] = a;
  2426. test_sha[1] = b;
  2427. test_sha[2] = c;
  2428. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  2429. if (ret != 0)
  2430. return -2300;
  2431. ret = wc_InitSha256_ex(&shaCopy, HEAP_HINT, devId);
  2432. if (ret != 0) {
  2433. wc_Sha256Free(&sha);
  2434. return -2301;
  2435. }
  2436. for (i = 0; i < times; ++i) {
  2437. ret = wc_Sha256Update(&sha, (byte*)test_sha[i].input,
  2438. (word32)test_sha[i].inLen);
  2439. if (ret != 0) {
  2440. ERROR_OUT(-2302 - i, exit);
  2441. }
  2442. ret = wc_Sha256GetHash(&sha, hashcopy);
  2443. if (ret != 0)
  2444. ERROR_OUT(-2303 - i, exit);
  2445. ret = wc_Sha256Copy(&sha, &shaCopy);
  2446. if (ret != 0)
  2447. ERROR_OUT(-2304 - i, exit);
  2448. ret = wc_Sha256Final(&sha, hash);
  2449. if (ret != 0)
  2450. ERROR_OUT(-2305 - i, exit);
  2451. wc_Sha256Free(&shaCopy);
  2452. if (XMEMCMP(hash, test_sha[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  2453. ERROR_OUT(-2306 - i, exit);
  2454. if (XMEMCMP(hash, hashcopy, WC_SHA256_DIGEST_SIZE) != 0)
  2455. ERROR_OUT(-2307 - i, exit);
  2456. }
  2457. /* BEGIN LARGE HASH TEST */ {
  2458. byte large_input[1024];
  2459. #ifdef HASH_SIZE_LIMIT
  2460. const char* large_digest =
  2461. "\xa4\x75\x9e\x7a\xa2\x03\x38\x32\x88\x66\xa2\xea\x17\xea\xf8\xc7"
  2462. "\xfe\x4e\xc6\xbb\xe3\xbb\x71\xce\xe7\xdf\x7c\x04\x61\xb3\xc2\x2f";
  2463. #else
  2464. const char* large_digest =
  2465. "\x27\x78\x3e\x87\x96\x3a\x4e\xfb\x68\x29\xb5\x31\xc9\xba\x57\xb4"
  2466. "\x4f\x45\x79\x7f\x67\x70\xbd\x63\x7f\xbf\x0d\x80\x7c\xbd\xba\xe0";
  2467. #endif
  2468. for (i = 0; i < (int)sizeof(large_input); i++) {
  2469. large_input[i] = (byte)(i & 0xFF);
  2470. }
  2471. #ifdef HASH_SIZE_LIMIT
  2472. times = 20;
  2473. #else
  2474. times = 100;
  2475. #endif
  2476. #ifdef WOLFSSL_PIC32MZ_HASH
  2477. wc_Sha256SizeSet(&sha, times * sizeof(large_input));
  2478. #endif
  2479. for (i = 0; i < times; ++i) {
  2480. ret = wc_Sha256Update(&sha, (byte*)large_input,
  2481. (word32)sizeof(large_input));
  2482. if (ret != 0)
  2483. ERROR_OUT(-2308, exit);
  2484. }
  2485. ret = wc_Sha256Final(&sha, hash);
  2486. if (ret != 0)
  2487. ERROR_OUT(-2309, exit);
  2488. if (XMEMCMP(hash, large_digest, WC_SHA256_DIGEST_SIZE) != 0)
  2489. ERROR_OUT(-2310, exit);
  2490. } /* END LARGE HASH TEST */
  2491. exit:
  2492. wc_Sha256Free(&sha);
  2493. wc_Sha256Free(&shaCopy);
  2494. return ret;
  2495. }
  2496. #endif
  2497. #ifdef WOLFSSL_SHA512
  2498. WOLFSSL_TEST_SUBROUTINE int sha512_test(void)
  2499. {
  2500. wc_Sha512 sha, shaCopy;
  2501. byte hash[WC_SHA512_DIGEST_SIZE];
  2502. byte hashcopy[WC_SHA512_DIGEST_SIZE];
  2503. int ret = 0;
  2504. testVector a, b, c;
  2505. testVector test_sha[3];
  2506. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2507. a.input = "";
  2508. a.output = "\xcf\x83\xe1\x35\x7e\xef\xb8\xbd\xf1\x54\x28\x50\xd6\x6d\x80"
  2509. "\x07\xd6\x20\xe4\x05\x0b\x57\x15\xdc\x83\xf4\xa9\x21\xd3\x6c"
  2510. "\xe9\xce\x47\xd0\xd1\x3c\x5d\x85\xf2\xb0\xff\x83\x18\xd2\x87"
  2511. "\x7e\xec\x2f\x63\xb9\x31\xbd\x47\x41\x7a\x81\xa5\x38\x32\x7a"
  2512. "\xf9\x27\xda\x3e";
  2513. a.inLen = XSTRLEN(a.input);
  2514. a.outLen = WC_SHA512_DIGEST_SIZE;
  2515. b.input = "abc";
  2516. b.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  2517. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  2518. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  2519. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  2520. "\xa5\x4c\xa4\x9f";
  2521. b.inLen = XSTRLEN(b.input);
  2522. b.outLen = WC_SHA512_DIGEST_SIZE;
  2523. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  2524. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  2525. c.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  2526. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  2527. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  2528. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  2529. "\x87\x4b\xe9\x09";
  2530. c.inLen = XSTRLEN(c.input);
  2531. c.outLen = WC_SHA512_DIGEST_SIZE;
  2532. test_sha[0] = a;
  2533. test_sha[1] = b;
  2534. test_sha[2] = c;
  2535. ret = wc_InitSha512_ex(&sha, HEAP_HINT, devId);
  2536. if (ret != 0)
  2537. return -2400;
  2538. ret = wc_InitSha512_ex(&shaCopy, HEAP_HINT, devId);
  2539. if (ret != 0) {
  2540. wc_Sha512Free(&sha);
  2541. return -2401;
  2542. }
  2543. for (i = 0; i < times; ++i) {
  2544. ret = wc_Sha512Update(&sha, (byte*)test_sha[i].input,
  2545. (word32)test_sha[i].inLen);
  2546. if (ret != 0)
  2547. ERROR_OUT(-2402 - i, exit);
  2548. ret = wc_Sha512GetHash(&sha, hashcopy);
  2549. if (ret != 0)
  2550. ERROR_OUT(-2403 - i, exit);
  2551. ret = wc_Sha512Copy(&sha, &shaCopy);
  2552. if (ret != 0)
  2553. ERROR_OUT(-2404 - i, exit);
  2554. ret = wc_Sha512Final(&sha, hash);
  2555. if (ret != 0)
  2556. ERROR_OUT(-2405 - i, exit);
  2557. wc_Sha512Free(&shaCopy);
  2558. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  2559. ERROR_OUT(-2406 - i, exit);
  2560. if (XMEMCMP(hash, hashcopy, WC_SHA512_DIGEST_SIZE) != 0)
  2561. ERROR_OUT(-2407 - i, exit);
  2562. }
  2563. /* BEGIN LARGE HASH TEST */ {
  2564. byte large_input[1024];
  2565. #ifdef HASH_SIZE_LIMIT
  2566. const char* large_digest =
  2567. "\x30\x9B\x96\xA6\xE9\x43\x78\x30\xA3\x71\x51\x61\xC1\xEB\xE1\xBE"
  2568. "\xC8\xA5\xF9\x13\x5A\xD6\x6D\x9E\x46\x31\x31\x67\x8D\xE2\xC0\x0B"
  2569. "\x2A\x1A\x03\xE1\xF3\x48\xA7\x33\xBD\x49\xF8\xFF\xF1\xC2\xC2\x95"
  2570. "\xCB\xF0\xAF\x87\x61\x85\x58\x63\x6A\xCA\x70\x9C\x8B\x83\x3F\x5D";
  2571. #else
  2572. const char* large_digest =
  2573. "\x5a\x1f\x73\x90\xbd\x8c\xe4\x63\x54\xce\xa0\x9b\xef\x32\x78\x2d"
  2574. "\x2e\xe7\x0d\x5e\x2f\x9d\x15\x1b\xdd\x2d\xde\x65\x0c\x7b\xfa\x83"
  2575. "\x5e\x80\x02\x13\x84\xb8\x3f\xff\x71\x62\xb5\x09\x89\x63\xe1\xdc"
  2576. "\xa5\xdc\xfc\xfa\x9d\x1a\x4d\xc0\xfa\x3a\x14\xf6\x01\x51\x90\xa4";
  2577. #endif
  2578. for (i = 0; i < (int)sizeof(large_input); i++) {
  2579. large_input[i] = (byte)(i & 0xFF);
  2580. }
  2581. #ifdef HASH_SIZE_LIMIT
  2582. times = 20;
  2583. #else
  2584. times = 100;
  2585. #endif
  2586. for (i = 0; i < times; ++i) {
  2587. ret = wc_Sha512Update(&sha, (byte*)large_input,
  2588. (word32)sizeof(large_input));
  2589. if (ret != 0)
  2590. ERROR_OUT(-2408, exit);
  2591. }
  2592. ret = wc_Sha512Final(&sha, hash);
  2593. if (ret != 0)
  2594. ERROR_OUT(-2409, exit);
  2595. if (XMEMCMP(hash, large_digest, WC_SHA512_DIGEST_SIZE) != 0)
  2596. ERROR_OUT(-2410, exit);
  2597. #ifndef NO_UNALIGNED_MEMORY_TEST
  2598. /* Unaligned memory access test */
  2599. for (i = 1; i < 16; i++) {
  2600. ret = wc_Sha512Update(&sha, (byte*)large_input + i,
  2601. (word32)sizeof(large_input) - i);
  2602. if (ret != 0)
  2603. ERROR_OUT(-2411, exit);
  2604. ret = wc_Sha512Final(&sha, hash);
  2605. }
  2606. #endif
  2607. } /* END LARGE HASH TEST */
  2608. exit:
  2609. wc_Sha512Free(&sha);
  2610. wc_Sha512Free(&shaCopy);
  2611. return ret;
  2612. }
  2613. #endif
  2614. #ifdef WOLFSSL_SHA384
  2615. WOLFSSL_TEST_SUBROUTINE int sha384_test(void)
  2616. {
  2617. wc_Sha384 sha, shaCopy;
  2618. byte hash[WC_SHA384_DIGEST_SIZE];
  2619. byte hashcopy[WC_SHA384_DIGEST_SIZE];
  2620. int ret = 0;
  2621. testVector a, b, c;
  2622. testVector test_sha[3];
  2623. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2624. a.input = "";
  2625. a.output = "\x38\xb0\x60\xa7\x51\xac\x96\x38\x4c\xd9\x32\x7e\xb1\xb1\xe3"
  2626. "\x6a\x21\xfd\xb7\x11\x14\xbe\x07\x43\x4c\x0c\xc7\xbf\x63\xf6"
  2627. "\xe1\xda\x27\x4e\xde\xbf\xe7\x6f\x65\xfb\xd5\x1a\xd2\xf1\x48"
  2628. "\x98\xb9\x5b";
  2629. a.inLen = XSTRLEN(a.input);
  2630. a.outLen = WC_SHA384_DIGEST_SIZE;
  2631. b.input = "abc";
  2632. b.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  2633. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  2634. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  2635. "\xc8\x25\xa7";
  2636. b.inLen = XSTRLEN(b.input);
  2637. b.outLen = WC_SHA384_DIGEST_SIZE;
  2638. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  2639. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  2640. c.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  2641. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  2642. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  2643. "\x74\x60\x39";
  2644. c.inLen = XSTRLEN(c.input);
  2645. c.outLen = WC_SHA384_DIGEST_SIZE;
  2646. test_sha[0] = a;
  2647. test_sha[1] = b;
  2648. test_sha[2] = c;
  2649. ret = wc_InitSha384_ex(&sha, HEAP_HINT, devId);
  2650. if (ret != 0)
  2651. return -2500;
  2652. ret = wc_InitSha384_ex(&shaCopy, HEAP_HINT, devId);
  2653. if (ret != 0) {
  2654. wc_Sha384Free(&sha);
  2655. return -2501;
  2656. }
  2657. for (i = 0; i < times; ++i) {
  2658. ret = wc_Sha384Update(&sha, (byte*)test_sha[i].input,
  2659. (word32)test_sha[i].inLen);
  2660. if (ret != 0)
  2661. ERROR_OUT(-2502 - i, exit);
  2662. ret = wc_Sha384GetHash(&sha, hashcopy);
  2663. if (ret != 0)
  2664. ERROR_OUT(-2503 - i, exit);
  2665. ret = wc_Sha384Copy(&sha, &shaCopy);
  2666. if (ret != 0)
  2667. ERROR_OUT(-2504 - i, exit);
  2668. ret = wc_Sha384Final(&sha, hash);
  2669. if (ret != 0)
  2670. ERROR_OUT(-2505 - i, exit);
  2671. wc_Sha384Free(&shaCopy);
  2672. if (XMEMCMP(hash, test_sha[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  2673. ERROR_OUT(-2506 - i, exit);
  2674. if (XMEMCMP(hash, hashcopy, WC_SHA384_DIGEST_SIZE) != 0)
  2675. ERROR_OUT(-2507 - i, exit);
  2676. }
  2677. /* BEGIN LARGE HASH TEST */ {
  2678. byte large_input[1024];
  2679. #ifdef HASH_SIZE_LIMIT
  2680. const char* large_digest =
  2681. "\xB5\xAD\x66\x6F\xD9\x58\x5E\x68\xDD\x5E\x30\xD3\x95\x72\x33\xA4"
  2682. "\xE9\x4B\x99\x3A\xEF\xF8\xE1\xBF\x1F\x05\x32\xAA\x16\x00\x82\xEC"
  2683. "\x15\xDA\xF2\x75\xEE\xE9\x06\xAF\x52\x8A\x5C\xEF\x72\x81\x80\xD6";
  2684. #else
  2685. const char* large_digest =
  2686. "\x37\x01\xdb\xff\x1e\x40\x4f\xe1\xe2\xea\x0b\x40\xbb\x3b\x39\x9a"
  2687. "\xcc\xe8\x44\x8e\x7e\xe5\x64\xb5\x6b\x7f\x56\x64\xa7\x2b\x84\xe3"
  2688. "\xc5\xd7\x79\x03\x25\x90\xf7\xa4\x58\xcb\x97\xa8\x8b\xb1\xa4\x81";
  2689. #endif
  2690. for (i = 0; i < (int)sizeof(large_input); i++) {
  2691. large_input[i] = (byte)(i & 0xFF);
  2692. }
  2693. #ifdef HASH_SIZE_LIMIT
  2694. times = 20;
  2695. #else
  2696. times = 100;
  2697. #endif
  2698. for (i = 0; i < times; ++i) {
  2699. ret = wc_Sha384Update(&sha, (byte*)large_input,
  2700. (word32)sizeof(large_input));
  2701. if (ret != 0)
  2702. ERROR_OUT(-2508, exit);
  2703. }
  2704. ret = wc_Sha384Final(&sha, hash);
  2705. if (ret != 0)
  2706. ERROR_OUT(-2509, exit);
  2707. if (XMEMCMP(hash, large_digest, WC_SHA384_DIGEST_SIZE) != 0)
  2708. ERROR_OUT(-2510, exit);
  2709. } /* END LARGE HASH TEST */
  2710. exit:
  2711. wc_Sha384Free(&sha);
  2712. wc_Sha384Free(&shaCopy);
  2713. return ret;
  2714. }
  2715. #endif /* WOLFSSL_SHA384 */
  2716. #ifdef WOLFSSL_SHA3
  2717. #ifndef WOLFSSL_NOSHA3_224
  2718. static int sha3_224_test(void)
  2719. {
  2720. wc_Sha3 sha;
  2721. byte hash[WC_SHA3_224_DIGEST_SIZE];
  2722. byte hashcopy[WC_SHA3_224_DIGEST_SIZE];
  2723. testVector a, b, c;
  2724. testVector test_sha[3];
  2725. int ret = 0;
  2726. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2727. a.input = "";
  2728. a.output = "\x6b\x4e\x03\x42\x36\x67\xdb\xb7\x3b\x6e\x15\x45\x4f\x0e\xb1"
  2729. "\xab\xd4\x59\x7f\x9a\x1b\x07\x8e\x3f\x5b\x5a\x6b\xc7";
  2730. a.inLen = XSTRLEN(a.input);
  2731. a.outLen = WC_SHA3_224_DIGEST_SIZE;
  2732. b.input = "abc";
  2733. b.output = "\xe6\x42\x82\x4c\x3f\x8c\xf2\x4a\xd0\x92\x34\xee\x7d\x3c\x76"
  2734. "\x6f\xc9\xa3\xa5\x16\x8d\x0c\x94\xad\x73\xb4\x6f\xdf";
  2735. b.inLen = XSTRLEN(b.input);
  2736. b.outLen = WC_SHA3_224_DIGEST_SIZE;
  2737. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2738. c.output = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55\x74\x49\x44\x79"
  2739. "\xba\x5c\x7e\x7a\xb7\x6e\xf2\x64\xea\xd0\xfc\xce\x33";
  2740. c.inLen = XSTRLEN(c.input);
  2741. c.outLen = WC_SHA3_224_DIGEST_SIZE;
  2742. test_sha[0] = a;
  2743. test_sha[1] = b;
  2744. test_sha[2] = c;
  2745. ret = wc_InitSha3_224(&sha, HEAP_HINT, devId);
  2746. if (ret != 0)
  2747. return -2600;
  2748. for (i = 0; i < times; ++i) {
  2749. ret = wc_Sha3_224_Update(&sha, (byte*)test_sha[i].input,
  2750. (word32)test_sha[i].inLen);
  2751. if (ret != 0)
  2752. ERROR_OUT(-2601 - i, exit);
  2753. ret = wc_Sha3_224_GetHash(&sha, hashcopy);
  2754. if (ret != 0)
  2755. ERROR_OUT(-2602 - i, exit);
  2756. ret = wc_Sha3_224_Final(&sha, hash);
  2757. if (ret != 0)
  2758. ERROR_OUT(-2603 - i, exit);
  2759. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_224_DIGEST_SIZE) != 0)
  2760. ERROR_OUT(-2604 - i, exit);
  2761. if (XMEMCMP(hash, hashcopy, WC_SHA3_224_DIGEST_SIZE) != 0)
  2762. ERROR_OUT(-2605 - i, exit);
  2763. }
  2764. /* BEGIN LARGE HASH TEST */ {
  2765. byte large_input[1024];
  2766. const char* large_digest =
  2767. "\x13\xe5\xd3\x98\x7b\x94\xda\x41\x12\xc7\x1e\x92\x3a\x19"
  2768. "\x21\x20\x86\x6f\x24\xbf\x0a\x31\xbc\xfd\xd6\x70\x36\xf3";
  2769. for (i = 0; i < (int)sizeof(large_input); i++) {
  2770. large_input[i] = (byte)(i & 0xFF);
  2771. }
  2772. times = 100;
  2773. for (i = 0; i < times; ++i) {
  2774. ret = wc_Sha3_224_Update(&sha, (byte*)large_input,
  2775. (word32)sizeof(large_input));
  2776. if (ret != 0)
  2777. ERROR_OUT(-2606, exit);
  2778. }
  2779. ret = wc_Sha3_224_Final(&sha, hash);
  2780. if (ret != 0)
  2781. ERROR_OUT(-2607, exit);
  2782. if (XMEMCMP(hash, large_digest, WC_SHA3_224_DIGEST_SIZE) != 0)
  2783. ERROR_OUT(-2608, exit);
  2784. } /* END LARGE HASH TEST */
  2785. exit:
  2786. wc_Sha3_224_Free(&sha);
  2787. return ret;
  2788. }
  2789. #endif /* WOLFSSL_NOSHA3_224 */
  2790. #ifndef WOLFSSL_NOSHA3_256
  2791. static int sha3_256_test(void)
  2792. {
  2793. wc_Sha3 sha;
  2794. byte hash[WC_SHA3_256_DIGEST_SIZE];
  2795. byte hashcopy[WC_SHA3_256_DIGEST_SIZE];
  2796. testVector a, b, c;
  2797. testVector test_sha[3];
  2798. int ret = 0;
  2799. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2800. byte large_input[1024];
  2801. const char* large_digest =
  2802. "\xdc\x90\xc0\xb1\x25\xdb\x2c\x34\x81\xa3\xff\xbc\x1e\x2e\x87\xeb"
  2803. "\x6d\x70\x85\x61\xe0\xe9\x63\x61\xff\xe5\x84\x4b\x1f\x68\x05\x15";
  2804. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  2805. /* test vector with hash of empty string */
  2806. const char* Keccak256EmptyOut =
  2807. "\xc5\xd2\x46\x01\x86\xf7\x23\x3c\x92\x7e\x7d\xb2\xdc\xc7\x03\xc0"
  2808. "\xe5\x00\xb6\x53\xca\x82\x27\x3b\x7b\xfa\xd8\x04\x5d\x85\xa4\x70";
  2809. #endif
  2810. a.input = "";
  2811. a.output = "\xa7\xff\xc6\xf8\xbf\x1e\xd7\x66\x51\xc1\x47\x56\xa0\x61\xd6"
  2812. "\x62\xf5\x80\xff\x4d\xe4\x3b\x49\xfa\x82\xd8\x0a\x4b\x80\xf8"
  2813. "\x43\x4a";
  2814. a.inLen = XSTRLEN(a.input);
  2815. a.outLen = WC_SHA3_256_DIGEST_SIZE;
  2816. b.input = "abc";
  2817. b.output = "\x3a\x98\x5d\xa7\x4f\xe2\x25\xb2\x04\x5c\x17\x2d\x6b\xd3\x90"
  2818. "\xbd\x85\x5f\x08\x6e\x3e\x9d\x52\x5b\x46\xbf\xe2\x45\x11\x43"
  2819. "\x15\x32";
  2820. b.inLen = XSTRLEN(b.input);
  2821. b.outLen = WC_SHA3_256_DIGEST_SIZE;
  2822. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2823. c.output = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8\x23\x5e"
  2824. "\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32\xdd\x97\x49\x6d"
  2825. "\x33\x76";
  2826. c.inLen = XSTRLEN(c.input);
  2827. c.outLen = WC_SHA3_256_DIGEST_SIZE;
  2828. test_sha[0] = a;
  2829. test_sha[1] = b;
  2830. test_sha[2] = c;
  2831. ret = wc_InitSha3_256(&sha, HEAP_HINT, devId);
  2832. if (ret != 0)
  2833. return -2700;
  2834. for (i = 0; i < times; ++i) {
  2835. ret = wc_Sha3_256_Update(&sha, (byte*)test_sha[i].input,
  2836. (word32)test_sha[i].inLen);
  2837. if (ret != 0)
  2838. ERROR_OUT(-2701 - i, exit);
  2839. ret = wc_Sha3_256_GetHash(&sha, hashcopy);
  2840. if (ret != 0)
  2841. ERROR_OUT(-2702 - i, exit);
  2842. ret = wc_Sha3_256_Final(&sha, hash);
  2843. if (ret != 0)
  2844. ERROR_OUT(-2703 - i, exit);
  2845. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_256_DIGEST_SIZE) != 0)
  2846. ERROR_OUT(-2704 - i, exit);
  2847. if (XMEMCMP(hash, hashcopy, WC_SHA3_256_DIGEST_SIZE) != 0)
  2848. ERROR_OUT(-2705 - i, exit);
  2849. }
  2850. /* BEGIN LARGE HASH TEST */ {
  2851. for (i = 0; i < (int)sizeof(large_input); i++) {
  2852. large_input[i] = (byte)(i & 0xFF);
  2853. }
  2854. times = 100;
  2855. for (i = 0; i < times; ++i) {
  2856. ret = wc_Sha3_256_Update(&sha, (byte*)large_input,
  2857. (word32)sizeof(large_input));
  2858. if (ret != 0)
  2859. ERROR_OUT(-2706, exit);
  2860. }
  2861. ret = wc_Sha3_256_Final(&sha, hash);
  2862. if (ret != 0)
  2863. ERROR_OUT(-2707, exit);
  2864. if (XMEMCMP(hash, large_digest, WC_SHA3_256_DIGEST_SIZE) != 0)
  2865. ERROR_OUT(-2708, exit);
  2866. } /* END LARGE HASH TEST */
  2867. /* this is a software only variant of SHA3 not supported by external hardware devices */
  2868. #if defined(WOLFSSL_HASH_FLAGS) && !defined(WOLFSSL_ASYNC_CRYPT)
  2869. /* Test for Keccak256 */
  2870. ret = wc_Sha3_SetFlags(&sha, WC_HASH_SHA3_KECCAK256);
  2871. if (ret != 0) {
  2872. ERROR_OUT(-2709, exit);
  2873. }
  2874. ret = wc_Sha3_256_Update(&sha, (byte*)"", 0);
  2875. if (ret != 0) {
  2876. ERROR_OUT(-2710, exit);
  2877. }
  2878. ret = wc_Sha3_256_Final(&sha, hash);
  2879. if (ret != 0) {
  2880. ERROR_OUT(-2711, exit);
  2881. }
  2882. if (XMEMCMP(hash, Keccak256EmptyOut, WC_SHA3_256_DIGEST_SIZE) != 0) {
  2883. ERROR_OUT(-2712, exit);
  2884. }
  2885. #endif /* WOLFSSL_HASH_FLAGS && !WOLFSSL_ASYNC_CRYPT */
  2886. exit:
  2887. wc_Sha3_256_Free(&sha);
  2888. return ret;
  2889. }
  2890. #endif /* WOLFSSL_NOSHA3_256 */
  2891. #ifndef WOLFSSL_NOSHA3_384
  2892. static int sha3_384_test(void)
  2893. {
  2894. wc_Sha3 sha;
  2895. byte hash[WC_SHA3_384_DIGEST_SIZE];
  2896. byte buf[64];
  2897. #ifndef NO_INTM_HASH_TEST
  2898. byte hashcopy[WC_SHA3_384_DIGEST_SIZE];
  2899. #endif
  2900. testVector a, b, c;
  2901. testVector test_sha[3];
  2902. int ret;
  2903. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  2904. a.input = "";
  2905. a.output = "\x0c\x63\xa7\x5b\x84\x5e\x4f\x7d\x01\x10\x7d\x85\x2e\x4c\x24"
  2906. "\x85\xc5\x1a\x50\xaa\xaa\x94\xfc\x61\x99\x5e\x71\xbb\xee\x98"
  2907. "\x3a\x2a\xc3\x71\x38\x31\x26\x4a\xdb\x47\xfb\x6b\xd1\xe0\x58"
  2908. "\xd5\xf0\x04";
  2909. a.inLen = XSTRLEN(a.input);
  2910. a.outLen = WC_SHA3_384_DIGEST_SIZE;
  2911. #if defined(WOLFSSL_AFALG_XILINX_SHA3) || defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  2912. /* NIST test vector with a length that is a multiple of 4 */
  2913. b.input = "\x7d\x80\xb1\x60\xc4\xb5\x36\xa3\xbe\xb7\x99\x80\x59\x93\x44"
  2914. "\x04\x7c\x5f\x82\xa1\xdf\xc3\xee\xd4";
  2915. b.output = "\x04\x1c\xc5\x86\x1b\xa3\x34\x56\x3c\x61\xd4\xef\x97\x10\xd4"
  2916. "\x89\x6c\x31\x1c\x92\xed\xbe\x0d\x7c\xd5\x3e\x80\x3b\xf2\xf4"
  2917. "\xeb\x60\x57\x23\x55\x70\x77\x0c\xe8\x7c\x55\x20\xd7\xec\x14"
  2918. "\x19\x87\x22";
  2919. b.inLen = XSTRLEN(b.input);
  2920. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  2921. #else
  2922. b.input = "abc";
  2923. b.output = "\xec\x01\x49\x82\x88\x51\x6f\xc9\x26\x45\x9f\x58\xe2\xc6\xad"
  2924. "\x8d\xf9\xb4\x73\xcb\x0f\xc0\x8c\x25\x96\xda\x7c\xf0\xe4\x9b"
  2925. "\xe4\xb2\x98\xd8\x8c\xea\x92\x7a\xc7\xf5\x39\xf1\xed\xf2\x28"
  2926. "\x37\x6d\x25";
  2927. b.inLen = XSTRLEN(b.input);
  2928. b.outLen = WC_SHA3_384_DIGEST_SIZE;
  2929. #endif
  2930. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  2931. c.output = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7\x8a\x49"
  2932. "\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd\xbc\x32\xb9\xd4"
  2933. "\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe\xa1\x9e\xef\x51\xac\xd0"
  2934. "\x65\x7c\x22";
  2935. c.inLen = XSTRLEN(c.input);
  2936. c.outLen = WC_SHA3_384_DIGEST_SIZE;
  2937. #if defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  2938. test_sha[0] = b; /* hardware acc. pre-Versal can not handle "" string */
  2939. #else
  2940. test_sha[0] = a;
  2941. #endif
  2942. test_sha[1] = b;
  2943. test_sha[2] = c;
  2944. ret = wc_InitSha3_384(&sha, HEAP_HINT, devId);
  2945. if (ret != 0)
  2946. return -2800;
  2947. for (i = 0; i < times; ++i) {
  2948. XMEMCPY(buf, test_sha[i].input, test_sha[i].inLen);
  2949. ret = wc_Sha3_384_Update(&sha, buf,
  2950. (word32)test_sha[i].inLen);
  2951. if (ret != 0)
  2952. ERROR_OUT(-2801 - (i * 10), exit);
  2953. #ifndef NO_INTM_HASH_TEST
  2954. ret = wc_Sha3_384_GetHash(&sha, hashcopy);
  2955. if (ret != 0)
  2956. ERROR_OUT(-2802 - (i * 10), exit);
  2957. #endif
  2958. ret = wc_Sha3_384_Final(&sha, hash);
  2959. if (ret != 0)
  2960. ERROR_OUT(-2803 - (i * 10), exit);
  2961. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_384_DIGEST_SIZE) != 0)
  2962. ERROR_OUT(-2804 - (i * 10), exit);
  2963. #ifndef NO_INTM_HASH_TEST
  2964. if (XMEMCMP(hash, hashcopy, WC_SHA3_384_DIGEST_SIZE) != 0)
  2965. ERROR_OUT(-2805 - (i * 10), exit);
  2966. #endif
  2967. }
  2968. /* BEGIN LARGE HASH TEST */ {
  2969. byte large_input[1024];
  2970. const char* large_digest =
  2971. "\x30\x44\xec\x17\xef\x47\x9f\x55\x36\x11\xd6\x3f\x8a\x31\x5a\x71"
  2972. "\x8a\x71\xa7\x1d\x8e\x84\xe8\x6c\x24\x02\x2f\x7a\x08\x4e\xea\xd7"
  2973. "\x42\x36\x5d\xa8\xc2\xb7\x42\xad\xec\x19\xfb\xca\xc6\x64\xb3\xa4";
  2974. for (i = 0; i < (int)sizeof(large_input); i++) {
  2975. large_input[i] = (byte)(i & 0xFF);
  2976. }
  2977. times = 100;
  2978. for (i = 0; i < times; ++i) {
  2979. ret = wc_Sha3_384_Update(&sha, (byte*)large_input,
  2980. (word32)sizeof(large_input));
  2981. if (ret != 0)
  2982. ERROR_OUT(-2806, exit);
  2983. }
  2984. ret = wc_Sha3_384_Final(&sha, hash);
  2985. if (ret != 0)
  2986. ERROR_OUT(-2807, exit);
  2987. if (XMEMCMP(hash, large_digest, WC_SHA3_384_DIGEST_SIZE) != 0)
  2988. ERROR_OUT(-2808, exit);
  2989. } /* END LARGE HASH TEST */
  2990. exit:
  2991. wc_Sha3_384_Free(&sha);
  2992. return ret;
  2993. }
  2994. #endif /* WOLFSSL_NOSHA3_384 */
  2995. #ifndef WOLFSSL_NOSHA3_512
  2996. static int sha3_512_test(void)
  2997. {
  2998. wc_Sha3 sha;
  2999. byte hash[WC_SHA3_512_DIGEST_SIZE];
  3000. byte hashcopy[WC_SHA3_512_DIGEST_SIZE];
  3001. testVector a, b, c;
  3002. testVector test_sha[3];
  3003. int ret;
  3004. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3005. a.input = "";
  3006. a.output = "\xa6\x9f\x73\xcc\xa2\x3a\x9a\xc5\xc8\xb5\x67\xdc\x18\x5a\x75"
  3007. "\x6e\x97\xc9\x82\x16\x4f\xe2\x58\x59\xe0\xd1\xdc\xc1\x47\x5c"
  3008. "\x80\xa6\x15\xb2\x12\x3a\xf1\xf5\xf9\x4c\x11\xe3\xe9\x40\x2c"
  3009. "\x3a\xc5\x58\xf5\x00\x19\x9d\x95\xb6\xd3\xe3\x01\x75\x85\x86"
  3010. "\x28\x1d\xcd\x26";
  3011. a.inLen = XSTRLEN(a.input);
  3012. a.outLen = WC_SHA3_512_DIGEST_SIZE;
  3013. b.input = "abc";
  3014. b.output = "\xb7\x51\x85\x0b\x1a\x57\x16\x8a\x56\x93\xcd\x92\x4b\x6b\x09"
  3015. "\x6e\x08\xf6\x21\x82\x74\x44\xf7\x0d\x88\x4f\x5d\x02\x40\xd2"
  3016. "\x71\x2e\x10\xe1\x16\xe9\x19\x2a\xf3\xc9\x1a\x7e\xc5\x76\x47"
  3017. "\xe3\x93\x40\x57\x34\x0b\x4c\xf4\x08\xd5\xa5\x65\x92\xf8\x27"
  3018. "\x4e\xec\x53\xf0";
  3019. b.inLen = XSTRLEN(b.input);
  3020. b.outLen = WC_SHA3_512_DIGEST_SIZE;
  3021. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3022. c.output = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10\xfc\xa8"
  3023. "\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7\xec\x2f\x1e\x91"
  3024. "\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53\x02\xba\x1b\x0d\x8d\xc7"
  3025. "\x8c\x08\x63\x46\xb5\x33\xb4\x9c\x03\x0d\x99\xa2\x7d\xaf\x11"
  3026. "\x39\xd6\xe7\x5e";
  3027. c.inLen = XSTRLEN(c.input);
  3028. c.outLen = WC_SHA3_512_DIGEST_SIZE;
  3029. test_sha[0] = a;
  3030. test_sha[1] = b;
  3031. test_sha[2] = c;
  3032. ret = wc_InitSha3_512(&sha, HEAP_HINT, devId);
  3033. if (ret != 0)
  3034. return -2900;
  3035. for (i = 0; i < times; ++i) {
  3036. ret = wc_Sha3_512_Update(&sha, (byte*)test_sha[i].input,
  3037. (word32)test_sha[i].inLen);
  3038. if (ret != 0)
  3039. ERROR_OUT(-2901 - i, exit);
  3040. ret = wc_Sha3_512_GetHash(&sha, hashcopy);
  3041. if (ret != 0)
  3042. ERROR_OUT(-2902 - i, exit);
  3043. ret = wc_Sha3_512_Final(&sha, hash);
  3044. if (ret != 0)
  3045. ERROR_OUT(-2903 - i, exit);
  3046. if (XMEMCMP(hash, test_sha[i].output, WC_SHA3_512_DIGEST_SIZE) != 0)
  3047. ERROR_OUT(-2904 - i, exit);
  3048. if (XMEMCMP(hash, hashcopy, WC_SHA3_512_DIGEST_SIZE) != 0)
  3049. ERROR_OUT(-2905 - i, exit);
  3050. }
  3051. /* BEGIN LARGE HASH TEST */ {
  3052. byte large_input[1024];
  3053. const char* large_digest =
  3054. "\x9c\x13\x26\xb6\x26\xb2\x94\x31\xbc\xf4\x34\xe9\x6f\xf2\xd6\x29"
  3055. "\x9a\xd0\x9b\x32\x63\x2f\x18\xa7\x5f\x23\xc9\x60\xc2\x32\x0c\xbc"
  3056. "\x57\x77\x33\xf1\x83\x81\x8a\xd3\x15\x7c\x93\xdc\x80\x9f\xed\x61"
  3057. "\x41\xa7\x5b\xfd\x32\x0e\x38\x15\xb0\x46\x3b\x7a\x4f\xfd\x44\x88";
  3058. for (i = 0; i < (int)sizeof(large_input); i++) {
  3059. large_input[i] = (byte)(i & 0xFF);
  3060. }
  3061. times = 100;
  3062. for (i = 0; i < times; ++i) {
  3063. ret = wc_Sha3_512_Update(&sha, (byte*)large_input,
  3064. (word32)sizeof(large_input));
  3065. if (ret != 0)
  3066. ERROR_OUT(-2906, exit);
  3067. }
  3068. ret = wc_Sha3_512_Final(&sha, hash);
  3069. if (ret != 0)
  3070. ERROR_OUT(-2907, exit);
  3071. if (XMEMCMP(hash, large_digest, WC_SHA3_512_DIGEST_SIZE) != 0)
  3072. ERROR_OUT(-2908, exit);
  3073. } /* END LARGE HASH TEST */
  3074. exit:
  3075. wc_Sha3_512_Free(&sha);
  3076. return ret;
  3077. }
  3078. #endif /* WOLFSSL_NOSHA3_512 */
  3079. WOLFSSL_TEST_SUBROUTINE int sha3_test(void)
  3080. {
  3081. int ret;
  3082. (void)ret;
  3083. #ifndef WOLFSSL_NOSHA3_224
  3084. if ((ret = sha3_224_test()) != 0)
  3085. return ret;
  3086. #endif
  3087. #ifndef WOLFSSL_NOSHA3_256
  3088. if ((ret = sha3_256_test()) != 0)
  3089. return ret;
  3090. #endif
  3091. #ifndef WOLFSSL_NOSHA3_384
  3092. if ((ret = sha3_384_test()) != 0)
  3093. return ret;
  3094. #endif
  3095. #ifndef WOLFSSL_NOSHA3_512
  3096. if ((ret = sha3_512_test()) != 0)
  3097. return ret;
  3098. #endif
  3099. return 0;
  3100. }
  3101. #endif /* WOLFSSL_SHA3 */
  3102. #ifdef WOLFSSL_SHAKE128
  3103. static int shake128_absorb_test(wc_Shake* sha)
  3104. {
  3105. byte hash[WC_SHA3_128_BLOCK_SIZE*2];
  3106. testVector a, b, c, d, e;
  3107. testVector test_sha[5];
  3108. int ret = 0;
  3109. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3110. byte large_input[1024];
  3111. const char* large_digest =
  3112. "\x2b\xd1\x69\x9f\xb3\x75\x40\x74\xb8\xb2\xd2\x0b\x92\x47\x9b\xfe"
  3113. "\xc9\x91\x48\xbe\xda\xa4\x09\xd7\x61\x35\x18\x05\x07\x71\xa5\x61"
  3114. "\x4d\xc4\x94\xad\xbe\x04\x7d\xad\x95\x2f\xeb\x2c\xc0\x10\x67\x43"
  3115. "\x40\xf1\x4a\x58\x1c\x54\xfa\x24\x1c\x1a\x4e\x8d\x9b\xbc\xea\xa7"
  3116. "\x32\xf2\x4c\xc7\x86\x05\x36\xdc\xb4\x42\xd8\x35\xd1\xb4\xa2\x79"
  3117. "\xa2\xe6\xee\x67\x4f\xbf\x2a\x93\x41\x88\x25\x56\x29\x90\x1a\x06"
  3118. "\xba\xfe\x9f\xa6\x1a\x74\xe8\x7e\x85\x4a\xc8\x58\x60\xb1\x7b\x18"
  3119. "\xdf\x77\x59\x46\x04\xc1\xff\x4b\x9b\xcb\xad\xfe\x91\x28\xf0\x01"
  3120. "\xc1\x33\xd0\x99\x99\x2e\x0c\x86\x84\x67\x4d\x37\xa4\x42\x45\x10"
  3121. "\xdc\x8f\xdb\x6f\xa6\x9b\xee\x8a\x60\xa5\x1f\x95\x3f\x8f\xf5\x31"
  3122. "\x4b\x1d\x48\x1e\x45\xff\x79\x5c\xbe\x72\xfc\x56\xed\x6d\x1a\x99"
  3123. "\x7f\x23\x7c\xd1\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b"
  3124. "\xa3\x60\x51\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41"
  3125. "\x77\x2c\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59"
  3126. "\x0d\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  3127. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4\xcb\x87\xd8\xb7\x9d\xa8"
  3128. "\xbf\xc5\x2e\x5e\xfc\xd3\x6c\x45\xd4\x5d\x72\x0f\x66\xeb\x67\x86"
  3129. "\xfa\x6c\xd6\x80\xa4\x23\xcb\x5d\xed\x3c\xde\xdc\x5b\x3d\xca\x95"
  3130. "\x43\x4b\xdc\xe8\x49\xd3\xe1\x01\xd4\xf1\xe4\x47\xcf\x56\xba\x71"
  3131. "\xb4\x69\xed\xe7\xdb\x0f\x89\xd6\xbb\xcd\x1a\xff\xb4\xbe\x72\x26"
  3132. "\xdc\x76\x79\xb3\x1a\x4b\xe6\x8d\x9b\x8e\xd9\xe9\xe6\xf9\xff\xa5";
  3133. a.input = "";
  3134. a.output = "\x7f\x9c\x2b\xa4\xe8\x8f\x82\x7d\x61\x60\x45\x50\x76\x05\x85"
  3135. "\x3e\xd7\x3b\x80\x93\xf6\xef\xbc\x88\xeb\x1a\x6e\xac\xfa\x66"
  3136. "\xef\x26\x3c\xb1\xee\xa9\x88\x00\x4b\x93\x10\x3c\xfb\x0a\xee"
  3137. "\xfd\x2a\x68\x6e\x01\xfa\x4a\x58\xe8\xa3\x63\x9c\xa8\xa1\xe3"
  3138. "\xf9\xae\x57\xe2\x35\xb8\xcc\x87\x3c\x23\xdc\x62\xb8\xd2\x60"
  3139. "\x16\x9a\xfa\x2f\x75\xab\x91\x6a\x58\xd9\x74\x91\x88\x35\xd2"
  3140. "\x5e\x6a\x43\x50\x85\xb2\xba\xdf\xd6\xdf\xaa\xc3\x59\xa5\xef"
  3141. "\xbb\x7b\xcc\x4b\x59\xd5\x38\xdf\x9a\x04\x30\x2e\x10\xc8\xbc"
  3142. "\x1c\xbf\x1a\x0b\x3a\x51\x20\xea\x17\xcd\xa7\xcf\xad\x76\x5f"
  3143. "\x56\x23\x47\x4d\x36\x8c\xcc\xa8\xaf\x00\x07\xcd\x9f\x5e\x4c"
  3144. "\x84\x9f\x16\x7a\x58\x0b\x14\xaa\xbd\xef\xae\xe7\xee\xf4\x7c"
  3145. "\xb0\xfc\xa9";
  3146. a.inLen = XSTRLEN(a.input);
  3147. a.outLen = WC_SHA3_128_BLOCK_SIZE;
  3148. b.input = "abc";
  3149. b.output = "\x58\x81\x09\x2d\xd8\x18\xbf\x5c\xf8\xa3\xdd\xb7\x93\xfb\xcb"
  3150. "\xa7\x40\x97\xd5\xc5\x26\xa6\xd3\x5f\x97\xb8\x33\x51\x94\x0f"
  3151. "\x2c\xc8\x44\xc5\x0a\xf3\x2a\xcd\x3f\x2c\xdd\x06\x65\x68\x70"
  3152. "\x6f\x50\x9b\xc1\xbd\xde\x58\x29\x5d\xae\x3f\x89\x1a\x9a\x0f"
  3153. "\xca\x57\x83\x78\x9a\x41\xf8\x61\x12\x14\xce\x61\x23\x94\xdf"
  3154. "\x28\x6a\x62\xd1\xa2\x25\x2a\xa9\x4d\xb9\xc5\x38\x95\x6c\x71"
  3155. "\x7d\xc2\xbe\xd4\xf2\x32\xa0\x29\x4c\x85\x7c\x73\x0a\xa1\x60"
  3156. "\x67\xac\x10\x62\xf1\x20\x1f\xb0\xd3\x77\xcf\xb9\xcd\xe4\xc6"
  3157. "\x35\x99\xb2\x7f\x34\x62\xbb\xa4\xa0\xed\x29\x6c\x80\x1f\x9f"
  3158. "\xf7\xf5\x73\x02\xbb\x30\x76\xee\x14\x5f\x97\xa3\x2a\xe6\x8e"
  3159. "\x76\xab\x66\xc4\x8d\x51\x67\x5b\xd4\x9a\xcc\x29\x08\x2f\x56"
  3160. "\x47\x58\x4e";
  3161. b.inLen = XSTRLEN(b.input);
  3162. b.outLen = WC_SHA3_128_BLOCK_SIZE;
  3163. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3164. c.output = "\x1a\x96\x18\x2b\x50\xfb\x8c\x7e\x74\xe0\xa7\x07\x78\x8f\x55"
  3165. "\xe9\x82\x09\xb8\xd9\x1f\xad\xe8\xf3\x2f\x8d\xd5\xcf\xf7\xbf"
  3166. "\x21\xf5\x4e\xe5\xf1\x95\x50\x82\x5a\x6e\x07\x00\x30\x51\x9e"
  3167. "\x94\x42\x63\xac\x1c\x67\x65\x28\x70\x65\x62\x1f\x9f\xcb\x32"
  3168. "\x01\x72\x3e\x32\x23\xb6\x3a\x46\xc2\x93\x8a\xa9\x53\xba\x84"
  3169. "\x01\xd0\xea\x77\xb8\xd2\x64\x90\x77\x55\x66\x40\x7b\x95\x67"
  3170. "\x3c\x0f\x4c\xc1\xce\x9f\xd9\x66\x14\x8d\x7e\xfd\xff\x26\xbb"
  3171. "\xf9\xf4\x8a\x21\xc6\xda\x35\xbf\xaa\x54\x56\x54\xf7\x0a\xe5"
  3172. "\x86\xff\x10\x13\x14\x20\x77\x14\x83\xec\x92\xed\xab\x40\x8c"
  3173. "\x76\x7b\xf4\xc5\xb4\xff\xfa\xa8\x0c\x8c\xa2\x14\xd8\x4c\x4d"
  3174. "\xc7\x00\xd0\xc5\x06\x30\xb2\xff\xc3\x79\x3e\xa4\xd8\x72\x58"
  3175. "\xb4\xc9\x54";
  3176. c.inLen = XSTRLEN(c.input);
  3177. c.outLen = WC_SHA3_128_BLOCK_SIZE;
  3178. /* Taken from NIST CAVP test vectors - full rate output. */
  3179. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  3180. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  3181. "\x85\xe0";
  3182. d.output = "\x93\x68\xf0\x15\x10\x92\x44\xeb\x02\x47\xfa\x3a\x0e\x57\xf5"
  3183. "\x2e\xa7\xd9\xeb\xa2\x3d\xae\x7a\x19\x7f\x0a\x29\xe9\x22\x55"
  3184. "\x06\x05\x98\x16\xb7\x84\x48\xb6\x49\x7a\x76\xeb\x96\x2d\xb3"
  3185. "\xf8\x4d\x37\x60\xf1\xfe\xb4\xbd\xc1\xfd\x4a\xc9\x4e\x91\x7a"
  3186. "\xc2\xea\x5e\x4f\x38\x37\x4a\xa5\x6e\x4f\x47\x67\xb8\xd7\x83"
  3187. "\x1b\x2d\x51\x49\x5a\xb8\xea\xb7\xc9\x82\x20\xaf\x13\x41\x5a"
  3188. "\x59\xbb\x7c\x17\x7a\xcd\x62\x8e\xf0\xff\xe3\x6c\xeb\x18\x59"
  3189. "\x5d\x14\x4c\xbf\x25\xef\xc0\x6c\xd9\x56\xa5\x78\x20\x6e\xa8"
  3190. "\xf9\x14\x5e\xf9\xce\x19\x50\x6a\x9d\x04\x4e\xc7\x00\x79\x9f"
  3191. "\xa1\x41\x9b\xaf\x60\x52\xc0\xc1\xb4\x45\xf8\x35\x17\x57\xb0"
  3192. "\xd0\x22\x87\x21\x89\xe2\xc0\x27\x3f\x82\xd9\x69\x69\x66\x3e"
  3193. "\x55\x4d\x09";
  3194. d.inLen = 32;
  3195. d.outLen = WC_SHA3_128_BLOCK_SIZE;
  3196. /* Taken from NIST CAVP test vectors - more than one output block. */
  3197. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  3198. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  3199. "\x47\xe4";
  3200. e.output = "\xe1\x7e\xab\x0d\xa4\x04\xf9\xb6\xac\xc0\x84\x97\x2f\xc5\x79"
  3201. "\xe8\x6d\xaa\x76\x10\xa5\xe1\x7c\x23\x2f\x79\x19\x83\x96\xfd"
  3202. "\x01\xc2\x4c\x34\xbb\x54\xf4\xb0\x1e\xf7\x40\xb4\x25\x33\x4a"
  3203. "\x55\xdd\x24\x81\x3d\xc8\xea\x86\xf5\x6e\xf7\x27\x67\x26\x2b"
  3204. "\xf2\x25\x74\x8c\xcc\x3d\x9f\x48\x6f\xfb\x72\x8f\x4e\xad\x29"
  3205. "\x60\xc9\x6c\x3e\x44\x63\x86\xea\xce\x21\x9c\x84\x28\x16\x11"
  3206. "\x63\x58\xb0\xf4\x2d\x7d\xff\xf7\xdd\x24\x11\xfa\x2a\x56\x79"
  3207. "\xfd\x7a\x94\x77\x45\x75\xba\xf9\xfc\xad\x68\xa1\x9e\x30\xd1"
  3208. "\x49\xb0\x59\xb5\x9c\x44\x6c\x4e\xdc\xa5\x9b\xc5\xa4\x79\x9d"
  3209. "\xc4\x65\xaa\x9e\x78\x2c\xed\x9f\x21\xc5\x5d\xe2\x42\xdd\x25"
  3210. "\xd0\xd9\xde\x60\xd0\x9f\xf8\x6a\xba\xf3\xa0\x3a\x76\x71\xb3"
  3211. "\x05\x42\xdf\xbe\x72\xfc\x56\xed\x6d\x1a\x99\x7f\x23\x7c\xd1"
  3212. "\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b\xa3\x60\x51"
  3213. "\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41\x77\x2c"
  3214. "\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59\x0d"
  3215. "\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  3216. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4\xcb\x87\xd8\xb7\x9d"
  3217. "\xa8\xbf\xc5\x2e\x5e\xfc\xd3\x6c\x45\xd4\x5d\x72\x0f\x66\xeb"
  3218. "\x67\x86\xfa\x6c\xd6\x80\xa4\x23\xcb\x5d\xed\x3c\xde\xdc\x5b"
  3219. "\x3d\xca\x95\x43\x4b\xdc\xe8\x49\xd3\xe1\x01\xd4\xf1\xe4\x47"
  3220. "\xcf\x56\xba\x71\xb4\x69\xed\xe7\xdb\x0f\x89\xd6\xbb\xcd\x1a"
  3221. "\xff\xb4\xbe\x72\x26\xdc\x76\x79\xb3\x1a\x4b\xe6\x8d\x9b\x8e"
  3222. "\xd9\xe9\xe6\xf9\xff\xa5";
  3223. e.inLen = 32;
  3224. e.outLen = 2 * WC_SHA3_128_BLOCK_SIZE;
  3225. test_sha[0] = a;
  3226. test_sha[1] = b;
  3227. test_sha[2] = c;
  3228. test_sha[3] = d;
  3229. test_sha[4] = e;
  3230. for (i = 0; i < times; ++i) {
  3231. ret = wc_InitShake128(sha, HEAP_HINT, devId);
  3232. if (ret != 0)
  3233. ERROR_OUT(-3100 - i, exit);
  3234. ret = wc_Shake128_Absorb(sha, (byte*)test_sha[i].input,
  3235. (word32)test_sha[i].inLen);
  3236. if (ret != 0)
  3237. ERROR_OUT(-3101 - i, exit);
  3238. ret = wc_Shake128_SqueezeBlocks(sha, hash,
  3239. (word32)test_sha[i].outLen / WC_SHA3_128_BLOCK_SIZE);
  3240. if (ret != 0)
  3241. ERROR_OUT(-3102 - i, exit);
  3242. if (XMEMCMP(hash, test_sha[i].output, (word32)test_sha[i].outLen) != 0)
  3243. ERROR_OUT(-3103 - i, exit);
  3244. }
  3245. /* BEGIN LARGE HASH TEST */ {
  3246. for (i = 0; i < (int)sizeof(large_input); i++) {
  3247. large_input[i] = (byte)(i & 0xFF);
  3248. }
  3249. ret = wc_InitShake128(sha, HEAP_HINT, devId);
  3250. if (ret != 0)
  3251. ERROR_OUT(-3104, exit);
  3252. /* Absorb is non-incremental. */
  3253. ret = wc_Shake128_Absorb(sha, (byte*)large_input,
  3254. (word32)sizeof(large_input));
  3255. if (ret != 0)
  3256. ERROR_OUT(-3105, exit);
  3257. /* Able to squeeze out blocks incrementally. */
  3258. ret = wc_Shake128_SqueezeBlocks(sha, hash, 1);
  3259. if (ret != 0)
  3260. ERROR_OUT(-3106, exit);
  3261. ret = wc_Shake128_SqueezeBlocks(sha, hash,
  3262. ((word32)sizeof(hash) / WC_SHA3_128_BLOCK_SIZE) - 1);
  3263. if (ret != 0)
  3264. ERROR_OUT(-3106, exit);
  3265. if (XMEMCMP(hash, large_digest, sizeof(hash)) != 0)
  3266. ERROR_OUT(-3107, exit);
  3267. } /* END LARGE HASH TEST */
  3268. exit:
  3269. return ret;
  3270. }
  3271. WOLFSSL_TEST_SUBROUTINE int shake128_test(void)
  3272. {
  3273. wc_Shake sha;
  3274. byte hash[250];
  3275. testVector a, b, c, d, e;
  3276. testVector test_sha[5];
  3277. int ret = 0;
  3278. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3279. byte large_input[1024];
  3280. const char* large_digest =
  3281. "\x88\xd7\x0e\x86\x46\x72\x6b\x3d\x7d\x22\xe1\xa9\x2d\x02\xdb\x35"
  3282. "\x92\x4f\x1b\x03\x90\xee\xa3\xce\xd1\x3a\x08\x3a\xd7\x4e\x10\xdf"
  3283. "\x09\x67\x33\x35\x4f\xdd\x38\x50\x5b\xcb\x75\xc7\xba\x65\xe5\xe8"
  3284. "\xb8\x76\xde\xc5\xee\xd7\xf1\x65\x93\x4e\x5e\xc4\xb1\xd7\x6b\xee"
  3285. "\x4b\x57\x48\xf5\x38\x49\x9e\x45\xa0\xf7\x32\xe9\x05\x26\x6a\x10"
  3286. "\x70\xd4\x7c\x19\x01\x1f\x6d\x37\xba\x7b\x74\xc2\xbc\xb6\xbc\x74"
  3287. "\xa3\x66\x6c\x9b\x11\x84\x9d\x4a\x36\xbc\x8a\x0d\x4c\xe3\x39\xfa"
  3288. "\xfa\x1b";
  3289. a.input = "";
  3290. a.output = "\x7f\x9c\x2b\xa4\xe8\x8f\x82\x7d\x61\x60\x45\x50\x76\x05\x85"
  3291. "\x3e\xd7\x3b\x80\x93\xf6\xef\xbc\x88\xeb\x1a\x6e\xac\xfa\x66"
  3292. "\xef\x26\x3c\xb1\xee\xa9\x88\x00\x4b\x93\x10\x3c\xfb\x0a\xee"
  3293. "\xfd\x2a\x68\x6e\x01\xfa\x4a\x58\xe8\xa3\x63\x9c\xa8\xa1\xe3"
  3294. "\xf9\xae\x57\xe2\x35\xb8\xcc\x87\x3c\x23\xdc\x62\xb8\xd2\x60"
  3295. "\x16\x9a\xfa\x2f\x75\xab\x91\x6a\x58\xd9\x74\x91\x88\x35\xd2"
  3296. "\x5e\x6a\x43\x50\x85\xb2\xba\xdf\xd6\xdf\xaa\xc3\x59\xa5\xef"
  3297. "\xbb\x7b\xcc\x4b\x59\xd5\x38\xdf\x9a";
  3298. a.inLen = XSTRLEN(a.input);
  3299. a.outLen = 114;
  3300. b.input = "abc";
  3301. b.output = "\x58\x81\x09\x2d\xd8\x18\xbf\x5c\xf8\xa3\xdd\xb7\x93\xfb\xcb"
  3302. "\xa7\x40\x97\xd5\xc5\x26\xa6\xd3\x5f\x97\xb8\x33\x51\x94\x0f"
  3303. "\x2c\xc8\x44\xc5\x0a\xf3\x2a\xcd\x3f\x2c\xdd\x06\x65\x68\x70"
  3304. "\x6f\x50\x9b\xc1\xbd\xde\x58\x29\x5d\xae\x3f\x89\x1a\x9a\x0f"
  3305. "\xca\x57\x83\x78\x9a\x41\xf8\x61\x12\x14\xce\x61\x23\x94\xdf"
  3306. "\x28\x6a\x62\xd1\xa2\x25\x2a\xa9\x4d\xb9\xc5\x38\x95\x6c\x71"
  3307. "\x7d\xc2\xbe\xd4\xf2\x32\xa0\x29\x4c\x85\x7c\x73\x0a\xa1\x60"
  3308. "\x67\xac\x10\x62\xf1\x20\x1f\xb0\xd3";
  3309. b.inLen = XSTRLEN(b.input);
  3310. b.outLen = 114;
  3311. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3312. c.output = "\x1a\x96\x18\x2b\x50\xfb\x8c\x7e\x74\xe0\xa7\x07\x78\x8f\x55"
  3313. "\xe9\x82\x09\xb8\xd9\x1f\xad\xe8\xf3\x2f\x8d\xd5\xcf\xf7\xbf"
  3314. "\x21\xf5\x4e\xe5\xf1\x95\x50\x82\x5a\x6e\x07\x00\x30\x51\x9e"
  3315. "\x94\x42\x63\xac\x1c\x67\x65\x28\x70\x65\x62\x1f\x9f\xcb\x32"
  3316. "\x01\x72\x3e\x32\x23\xb6\x3a\x46\xc2\x93\x8a\xa9\x53\xba\x84"
  3317. "\x01\xd0\xea\x77\xb8\xd2\x64\x90\x77\x55\x66\x40\x7b\x95\x67"
  3318. "\x3c\x0f\x4c\xc1\xce\x9f\xd9\x66\x14\x8d\x7e\xfd\xff\x26\xbb"
  3319. "\xf9\xf4\x8a\x21\xc6\xda\x35\xbf\xaa";
  3320. c.inLen = XSTRLEN(c.input);
  3321. c.outLen = 114;
  3322. /* Taken from NIST CAVP test vectors - full rate output. */
  3323. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  3324. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  3325. "\x85\xe0";
  3326. d.output = "\x93\x68\xf0\x15\x10\x92\x44\xeb\x02\x47\xfa\x3a\x0e\x57\xf5"
  3327. "\x2e\xa7\xd9\xeb\xa2\x3d\xae\x7a\x19\x7f\x0a\x29\xe9\x22\x55"
  3328. "\x06\x05\x98\x16\xb7\x84\x48\xb6\x49\x7a\x76\xeb\x96\x2d\xb3"
  3329. "\xf8\x4d\x37\x60\xf1\xfe\xb4\xbd\xc1\xfd\x4a\xc9\x4e\x91\x7a"
  3330. "\xc2\xea\x5e\x4f\x38\x37\x4a\xa5\x6e\x4f\x47\x67\xb8\xd7\x83"
  3331. "\x1b\x2d\x51\x49\x5a\xb8\xea\xb7\xc9\x82\x20\xaf\x13\x41\x5a"
  3332. "\x59\xbb\x7c\x17\x7a\xcd\x62\x8e\xf0\xff\xe3\x6c\xeb\x18\x59"
  3333. "\x5d\x14\x4c\xbf\x25\xef\xc0\x6c\xd9\x56\xa5\x78\x20\x6e\xa8"
  3334. "\xf9\x14\x5e\xf9\xce\x19\x50\x6a\x9d\x04\x4e\xc7\x00\x79\x9f"
  3335. "\xa1";
  3336. d.inLen = 32;
  3337. d.outLen = 136;
  3338. /* Taken from NIST CAVP test vectors - more than one output block. */
  3339. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  3340. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  3341. "\x47\xe4";
  3342. e.output = "\xe1\x7e\xab\x0d\xa4\x04\xf9\xb6\xac\xc0\x84\x97\x2f\xc5\x79"
  3343. "\xe8\x6d\xaa\x76\x10\xa5\xe1\x7c\x23\x2f\x79\x19\x83\x96\xfd"
  3344. "\x01\xc2\x4c\x34\xbb\x54\xf4\xb0\x1e\xf7\x40\xb4\x25\x33\x4a"
  3345. "\x55\xdd\x24\x81\x3d\xc8\xea\x86\xf5\x6e\xf7\x27\x67\x26\x2b"
  3346. "\xf2\x25\x74\x8c\xcc\x3d\x9f\x48\x6f\xfb\x72\x8f\x4e\xad\x29"
  3347. "\x60\xc9\x6c\x3e\x44\x63\x86\xea\xce\x21\x9c\x84\x28\x16\x11"
  3348. "\x63\x58\xb0\xf4\x2d\x7d\xff\xf7\xdd\x24\x11\xfa\x2a\x56\x79"
  3349. "\xfd\x7a\x94\x77\x45\x75\xba\xf9\xfc\xad\x68\xa1\x9e\x30\xd1"
  3350. "\x49\xb0\x59\xb5\x9c\x44\x6c\x4e\xdc\xa5\x9b\xc5\xa4\x79\x9d"
  3351. "\xc4\x65\xaa\x9e\x78\x2c\xed\x9f\x21\xc5\x5d\xe2\x42\xdd\x25"
  3352. "\xd0\xd9\xde\x60\xd0\x9f\xf8\x6a\xba\xf3\xa0\x3a\x76\x71\xb3"
  3353. "\x05\x42\xdf\xbe\x72\xfc\x56\xed\x6d\x1a\x99\x7f\x23\x7c\xd1"
  3354. "\xa5\x50\x9e\xb0\x4d\x61\x37\xa5\xcb\x24\x71\x3b\xa3\x60\x51"
  3355. "\x2e\x80\x83\x8b\xe0\x55\x50\xa7\x1e\xcc\x9f\xac\x41\x77\x2c"
  3356. "\x79\x22\x30\x09\x1b\x1a\x83\x5b\x2c\x48\xdc\x09\x7d\x59\x0d"
  3357. "\xf0\x54\x17\xfb\x5e\x38\x68\xde\xdb\xc5\x93\xab\x17\x5f\x4b"
  3358. "\x4d\x6d\xf2\xc7\x4e\x15\x1e\x10\x76\xc4";
  3359. e.inLen = 32;
  3360. e.outLen = 250;
  3361. test_sha[0] = a;
  3362. test_sha[1] = b;
  3363. test_sha[2] = c;
  3364. test_sha[3] = d;
  3365. test_sha[4] = e;
  3366. ret = wc_InitShake128(&sha, HEAP_HINT, devId);
  3367. if (ret != 0)
  3368. return -3100;
  3369. for (i = 0; i < times; ++i) {
  3370. ret = wc_Shake128_Update(&sha, (byte*)test_sha[i].input,
  3371. (word32)test_sha[i].inLen);
  3372. if (ret != 0)
  3373. ERROR_OUT(-3101 - i, exit);
  3374. ret = wc_Shake128_Final(&sha, hash, (word32)test_sha[i].outLen);
  3375. if (ret != 0)
  3376. ERROR_OUT(-3102 - i, exit);
  3377. if (XMEMCMP(hash, test_sha[i].output, test_sha[i].outLen) != 0)
  3378. ERROR_OUT(-3103 - i, exit);
  3379. }
  3380. /* BEGIN LARGE HASH TEST */ {
  3381. for (i = 0; i < (int)sizeof(large_input); i++) {
  3382. large_input[i] = (byte)(i & 0xFF);
  3383. }
  3384. times = 100;
  3385. for (i = 0; i < times; ++i) {
  3386. ret = wc_Shake128_Update(&sha, (byte*)large_input,
  3387. (word32)sizeof(large_input));
  3388. if (ret != 0)
  3389. ERROR_OUT(-3104, exit);
  3390. }
  3391. ret = wc_Shake128_Final(&sha, hash, (word32)sizeof(hash));
  3392. if (ret != 0)
  3393. ERROR_OUT(-3105, exit);
  3394. if (XMEMCMP(hash, large_digest, 114) != 0)
  3395. ERROR_OUT(-3106, exit);
  3396. } /* END LARGE HASH TEST */
  3397. ret = shake128_absorb_test(&sha);
  3398. exit:
  3399. wc_Shake128_Free(&sha);
  3400. return ret;
  3401. }
  3402. #endif
  3403. #ifdef WOLFSSL_SHAKE256
  3404. static int shake256_absorb_test(wc_Shake* sha, byte *large_input_buf,
  3405. size_t large_input_buf_size)
  3406. {
  3407. byte hash[WC_SHA3_256_BLOCK_SIZE*2];
  3408. testVector a, b, c, d, e;
  3409. testVector test_sha[5];
  3410. int ret = 0;
  3411. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3412. const char* large_digest =
  3413. "\x21\x25\x8e\xae\x6e\x4f\xa7\xe1\xb9\x6d\xa7\xc9\x7d\x46\x03\x69"
  3414. "\x29\x0d\x81\x49\xba\x5d\xaf\x37\xfd\xeb\x25\x52\x1d\xd9\xbd\x65"
  3415. "\xfa\x99\xb9\xd1\x70\x6b\xeb\xd4\xc1\x2c\xea\x24\x20\x27\xa7\xcd"
  3416. "\xfa\xe1\x81\xd9\xd5\xc1\x1c\xc7\xe9\x70\xc3\xc7\x21\x6f\x32\x22"
  3417. "\xe3\x27\xdb\x58\x5e\xea\x18\x2d\x63\x4d\x14\x6c\x94\xcf\x2b\x7e"
  3418. "\x6e\x2a\x74\xf3\xe0\xac\xb3\xb2\xcc\xef\x38\xe9\xe7\x35\xb3\xc5"
  3419. "\x77\x9d\xff\xe3\x08\x8e\xf8\x2c\x89\xbb\x45\x22\x16\x99\x91\xc0"
  3420. "\xe7\x71\x57\x75\xc5\xb1\xc6\xaf\x27\xcb\x64\x8c\xc4\xee\x3d\x5f"
  3421. "\x4c\x35\xfb\x1c\xf3\xf8\x0e\xfd\x5e\xfc\x07\xd8\x4d\x55\x32\x49"
  3422. "\x45\x0d\xab\x4a\x49\xc4\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93"
  3423. "\x7a\xe6\x6b\xb4\x36\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43"
  3424. "\x2f\x3b\xfc\x09\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48"
  3425. "\x3d\x0e\xda\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08"
  3426. "\xd9\xdc\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a"
  3427. "\xb7\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  3428. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c\x68\x1b\x30\xdd\xc4\xe6"
  3429. "\x83\x8b\x0f\x23\x58\x7e\x06\x5f\x4a\x2b\xed\xc9\x6c\x97\x68\x44";
  3430. a.input = "";
  3431. a.output = "\x46\xb9\xdd\x2b\x0b\xa8\x8d\x13\x23\x3b\x3f\xeb\x74\x3e\xeb"
  3432. "\x24\x3f\xcd\x52\xea\x62\xb8\x1b\x82\xb5\x0c\x27\x64\x6e\xd5"
  3433. "\x76\x2f\xd7\x5d\xc4\xdd\xd8\xc0\xf2\x00\xcb\x05\x01\x9d\x67"
  3434. "\xb5\x92\xf6\xfc\x82\x1c\x49\x47\x9a\xb4\x86\x40\x29\x2e\xac"
  3435. "\xb3\xb7\xc4\xbe\x14\x1e\x96\x61\x6f\xb1\x39\x57\x69\x2c\xc7"
  3436. "\xed\xd0\xb4\x5a\xe3\xdc\x07\x22\x3c\x8e\x92\x93\x7b\xef\x84"
  3437. "\xbc\x0e\xab\x86\x28\x53\x34\x9e\xc7\x55\x46\xf5\x8f\xb7\xc2"
  3438. "\x77\x5c\x38\x46\x2c\x50\x10\xd8\x46\xc1\x85\xc1\x51\x11\xe5"
  3439. "\x95\x52\x2a\x6b\xcd\x16\xcf\x86\xf3\xd1\x22\x10\x9e\x3b\x1f"
  3440. "\xdd";
  3441. a.inLen = XSTRLEN(a.input);
  3442. a.outLen = WC_SHA3_256_BLOCK_SIZE;
  3443. b.input = "abc";
  3444. b.output = "\x48\x33\x66\x60\x13\x60\xa8\x77\x1c\x68\x63\x08\x0c\xc4\x11"
  3445. "\x4d\x8d\xb4\x45\x30\xf8\xf1\xe1\xee\x4f\x94\xea\x37\xe7\x8b"
  3446. "\x57\x39\xd5\xa1\x5b\xef\x18\x6a\x53\x86\xc7\x57\x44\xc0\x52"
  3447. "\x7e\x1f\xaa\x9f\x87\x26\xe4\x62\xa1\x2a\x4f\xeb\x06\xbd\x88"
  3448. "\x01\xe7\x51\xe4\x13\x85\x14\x12\x04\xf3\x29\x97\x9f\xd3\x04"
  3449. "\x7a\x13\xc5\x65\x77\x24\xad\xa6\x4d\x24\x70\x15\x7b\x3c\xdc"
  3450. "\x28\x86\x20\x94\x4d\x78\xdb\xcd\xdb\xd9\x12\x99\x3f\x09\x13"
  3451. "\xf1\x64\xfb\x2c\xe9\x51\x31\xa2\xd0\x9a\x3e\x6d\x51\xcb\xfc"
  3452. "\x62\x27\x20\xd7\xa7\x5c\x63\x34\xe8\xa2\xd7\xec\x71\xa7\xcc"
  3453. "\x29";
  3454. b.inLen = XSTRLEN(b.input);
  3455. b.outLen = WC_SHA3_256_BLOCK_SIZE;
  3456. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3457. c.output = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f\x6f\x87"
  3458. "\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b\xe5\xd4\xfd\x2e"
  3459. "\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59\xaa\x80\x60\xf1\xf9\xbc"
  3460. "\x99\x6c\x05\xac\xa3\xc6\x96\xa8\xb6\x62\x79\xdc\x67\x2c\x74"
  3461. "\x0b\xb2\x24\xec\x37\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55"
  3462. "\xf5\x1d\x97\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a"
  3463. "\xf2\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67\x60"
  3464. "\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4\x9d\xd3\xef\x7e\x18\x2b"
  3465. "\x15\x24\xdf\x82\xea\x1c\xef\xe1\xc6\xc3\x96\x61\x75\xf0\x22"
  3466. "\x8d";
  3467. c.inLen = XSTRLEN(c.input);
  3468. c.outLen = WC_SHA3_256_BLOCK_SIZE;
  3469. /* Taken from NIST CAVP test vectors - full rate output. */
  3470. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  3471. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  3472. "\x85\xe0";
  3473. d.output = "\x00\x64\x8a\xfb\xc5\xe6\x51\x64\x9d\xb1\xfd\x82\x93\x6b\x00"
  3474. "\xdb\xbc\x12\x2f\xb4\xc8\x77\x86\x0d\x38\x5c\x49\x50\xd5\x6d"
  3475. "\xe7\xe0\x96\xd6\x13\xd7\xa3\xf2\x7e\xd8\xf2\x63\x34\xb0\xcc"
  3476. "\xc1\x40\x7b\x41\xdc\xcb\x23\xdf\xaa\x52\x98\x18\xd1\x12\x5c"
  3477. "\xd5\x34\x80\x92\x52\x43\x66\xb8\x5f\xab\xb9\x7c\x6c\xd1\xe6"
  3478. "\x06\x6f\x45\x9b\xcc\x56\x6d\xa8\x7e\xc9\xb7\xba\x36\x79\x2d"
  3479. "\x11\x8a\xc3\x9a\x4c\xce\xf6\x19\x2b\xbf\x3a\x54\xaf\x18\xe5"
  3480. "\x7b\x0c\x14\x61\x01\xf6\xae\xaa\x82\x2b\xc4\xb4\xc9\x70\x8b"
  3481. "\x09\xf0\xb3\xba\xb4\x1b\xcc\xe9\x64\xd9\x99\xd1\x10\x7b\xd7"
  3482. "\xc2";
  3483. d.inLen = 32;
  3484. d.outLen = WC_SHA3_256_BLOCK_SIZE;
  3485. /* Taken from NIST CAVP test vectors - more than one output block. */
  3486. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  3487. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  3488. "\x47\xe4";
  3489. e.output = "\x2e\x97\x5f\x6a\x8a\x14\xf0\x70\x4d\x51\xb1\x36\x67\xd8\x19"
  3490. "\x5c\x21\x9f\x71\xe6\x34\x56\x96\xc4\x9f\xa4\xb9\xd0\x8e\x92"
  3491. "\x25\xd3\xd3\x93\x93\x42\x51\x52\xc9\x7e\x71\xdd\x24\x60\x1c"
  3492. "\x11\xab\xcf\xa0\xf1\x2f\x53\xc6\x80\xbd\x3a\xe7\x57\xb8\x13"
  3493. "\x4a\x9c\x10\xd4\x29\x61\x58\x69\x21\x7f\xdd\x58\x85\xc4\xdb"
  3494. "\x17\x49\x85\x70\x3a\x6d\x6d\xe9\x4a\x66\x7e\xac\x30\x23\x44"
  3495. "\x3a\x83\x37\xae\x1b\xc6\x01\xb7\x6d\x7d\x38\xec\x3c\x34\x46"
  3496. "\x31\x05\xf0\xd3\x94\x9d\x78\xe5\x62\xa0\x39\xe4\x46\x95\x48"
  3497. "\xb6\x09\x39\x5d\xe5\xa4\xfd\x43\xc4\x6c\xa9\xfd\x6e\xe2\x9a"
  3498. "\xda\x5e\xfc\x07\xd8\x4d\x55\x32\x49\x45\x0d\xab\x4a\x49\xc4"
  3499. "\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93\x7a\xe6\x6b\xb4\x36"
  3500. "\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43\x2f\x3b\xfc\x09"
  3501. "\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48\x3d\x0e\xda"
  3502. "\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08\xd9\xdc"
  3503. "\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a\xb7"
  3504. "\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  3505. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c\x68\x1b\x30\xdd\xc4"
  3506. "\xe6\x83\x8b\x0f\x23\x58\x7e\x06\x5f\x4a\x2b\xed\xc9\x6c\x97"
  3507. "\x68\x44";
  3508. e.inLen = 32;
  3509. e.outLen = 2 * WC_SHA3_256_BLOCK_SIZE;
  3510. test_sha[0] = a;
  3511. test_sha[1] = b;
  3512. test_sha[2] = c;
  3513. test_sha[3] = d;
  3514. test_sha[4] = e;
  3515. for (i = 0; i < times; ++i) {
  3516. ret = wc_InitShake256(sha, HEAP_HINT, devId);
  3517. if (ret != 0)
  3518. ERROR_OUT(-3100 - i, exit);
  3519. ret = wc_Shake256_Absorb(sha, (byte*)test_sha[i].input,
  3520. (word32)test_sha[i].inLen);
  3521. if (ret != 0)
  3522. ERROR_OUT(-3101 - i, exit);
  3523. ret = wc_Shake256_SqueezeBlocks(sha, hash,
  3524. (word32)test_sha[i].outLen / WC_SHA3_256_BLOCK_SIZE);
  3525. if (ret != 0)
  3526. ERROR_OUT(-3102 - i, exit);
  3527. if (XMEMCMP(hash, test_sha[i].output, (word32)test_sha[i].outLen) != 0)
  3528. ERROR_OUT(-3103 - i, exit);
  3529. }
  3530. /* BEGIN LARGE HASH TEST */ {
  3531. for (i = 0; i < (int)large_input_buf_size; i++) {
  3532. large_input_buf[i] = (byte)(i & 0xFF);
  3533. }
  3534. ret = wc_InitShake256(sha, HEAP_HINT, devId);
  3535. if (ret != 0)
  3536. ERROR_OUT(-3104, exit);
  3537. /* Absorb is non-incremental. */
  3538. ret = wc_Shake256_Absorb(sha, large_input_buf,
  3539. (word32)large_input_buf_size);
  3540. if (ret != 0)
  3541. ERROR_OUT(-3105, exit);
  3542. /* Able to squeeze out blocks incrementally. */
  3543. ret = wc_Shake256_SqueezeBlocks(sha, hash, 1);
  3544. if (ret != 0)
  3545. ERROR_OUT(-3106, exit);
  3546. ret = wc_Shake256_SqueezeBlocks(sha, hash,
  3547. ((word32)sizeof(hash) / WC_SHA3_256_BLOCK_SIZE) - 1);
  3548. if (ret != 0)
  3549. ERROR_OUT(-3106, exit);
  3550. if (XMEMCMP(hash, large_digest, sizeof(hash)) != 0)
  3551. ERROR_OUT(-3107, exit);
  3552. } /* END LARGE HASH TEST */
  3553. exit:
  3554. return ret;
  3555. }
  3556. WOLFSSL_TEST_SUBROUTINE int shake256_test(void)
  3557. {
  3558. wc_Shake sha;
  3559. byte hash[250];
  3560. testVector a, b, c, d, e;
  3561. testVector test_sha[5];
  3562. int ret = 0;
  3563. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  3564. #define SHAKE256_LARGE_INPUT_BUFSIZ 1024
  3565. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  3566. byte *large_input = NULL;
  3567. #else
  3568. byte large_input[SHAKE256_LARGE_INPUT_BUFSIZ];
  3569. #endif
  3570. const char* large_digest =
  3571. "\x90\x32\x4a\xcc\xd1\xdf\xb8\x0b\x79\x1f\xb8\xc8\x5b\x54\xc8\xe7"
  3572. "\x45\xf5\x60\x6b\x38\x26\xb2\x0a\xee\x38\x01\xf3\xd9\xfa\x96\x9f"
  3573. "\x6a\xd7\x15\xdf\xb6\xc2\xf4\x20\x33\x44\x55\xe8\x2a\x09\x2b\x68"
  3574. "\x2e\x18\x65\x5e\x65\x93\x28\xbc\xb1\x9e\xe2\xb1\x92\xea\x98\xac"
  3575. "\x21\xef\x4c\xe1\xb4\xb7\xbe\x81\x5c\x1d\xd3\xb7\x17\xe5\xbb\xc5"
  3576. "\x8c\x68\xb7\xfb\xac\x55\x8a\x9b\x4d\x91\xe4\x9f\x72\xbb\x6e\x38"
  3577. "\xaf\x21\x7d\x21\xaa\x98\x4e\x75\xc4\xb4\x1c\x7c\x50\x45\x54\xf9"
  3578. "\xea\x26";
  3579. a.input = "";
  3580. a.output = "\x46\xb9\xdd\x2b\x0b\xa8\x8d\x13\x23\x3b\x3f\xeb\x74\x3e\xeb"
  3581. "\x24\x3f\xcd\x52\xea\x62\xb8\x1b\x82\xb5\x0c\x27\x64\x6e\xd5"
  3582. "\x76\x2f\xd7\x5d\xc4\xdd\xd8\xc0\xf2\x00\xcb\x05\x01\x9d\x67"
  3583. "\xb5\x92\xf6\xfc\x82\x1c\x49\x47\x9a\xb4\x86\x40\x29\x2e\xac"
  3584. "\xb3\xb7\xc4\xbe\x14\x1e\x96\x61\x6f\xb1\x39\x57\x69\x2c\xc7"
  3585. "\xed\xd0\xb4\x5a\xe3\xdc\x07\x22\x3c\x8e\x92\x93\x7b\xef\x84"
  3586. "\xbc\x0e\xab\x86\x28\x53\x34\x9e\xc7\x55\x46\xf5\x8f\xb7\xc2"
  3587. "\x77\x5c\x38\x46\x2c\x50\x10\xd8\x46";
  3588. a.inLen = XSTRLEN(a.input);
  3589. a.outLen = 114;
  3590. b.input = "abc";
  3591. b.output = "\x48\x33\x66\x60\x13\x60\xa8\x77\x1c\x68\x63\x08\x0c\xc4\x11"
  3592. "\x4d\x8d\xb4\x45\x30\xf8\xf1\xe1\xee\x4f\x94\xea\x37\xe7\x8b"
  3593. "\x57\x39\xd5\xa1\x5b\xef\x18\x6a\x53\x86\xc7\x57\x44\xc0\x52"
  3594. "\x7e\x1f\xaa\x9f\x87\x26\xe4\x62\xa1\x2a\x4f\xeb\x06\xbd\x88"
  3595. "\x01\xe7\x51\xe4\x13\x85\x14\x12\x04\xf3\x29\x97\x9f\xd3\x04"
  3596. "\x7a\x13\xc5\x65\x77\x24\xad\xa6\x4d\x24\x70\x15\x7b\x3c\xdc"
  3597. "\x28\x86\x20\x94\x4d\x78\xdb\xcd\xdb\xd9\x12\x99\x3f\x09\x13"
  3598. "\xf1\x64\xfb\x2c\xe9\x51\x31\xa2\xd0";
  3599. b.inLen = XSTRLEN(b.input);
  3600. b.outLen = 114;
  3601. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  3602. c.output = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f\x6f\x87"
  3603. "\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b\xe5\xd4\xfd\x2e"
  3604. "\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59\xaa\x80\x60\xf1\xf9\xbc"
  3605. "\x99\x6c\x05\xac\xa3\xc6\x96\xa8\xb6\x62\x79\xdc\x67\x2c\x74"
  3606. "\x0b\xb2\x24\xec\x37\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55"
  3607. "\xf5\x1d\x97\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a"
  3608. "\xf2\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67\x60"
  3609. "\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  3610. c.inLen = XSTRLEN(c.input);
  3611. c.outLen = 114;
  3612. /* Taken from NIST CAVP test vectors - full rate output. */
  3613. d.input = "\xdc\x88\x6d\xf3\xf6\x9c\x49\x51\x3d\xe3\x62\x7e\x94\x81\xdb"
  3614. "\x58\x71\xe8\xee\x88\xeb\x9f\x99\x61\x15\x41\x93\x0a\x8b\xc8"
  3615. "\x85\xe0";
  3616. d.output = "\x00\x64\x8a\xfb\xc5\xe6\x51\x64\x9d\xb1\xfd\x82\x93\x6b\x00"
  3617. "\xdb\xbc\x12\x2f\xb4\xc8\x77\x86\x0d\x38\x5c\x49\x50\xd5\x6d"
  3618. "\xe7\xe0\x96\xd6\x13\xd7\xa3\xf2\x7e\xd8\xf2\x63\x34\xb0\xcc"
  3619. "\xc1\x40\x7b\x41\xdc\xcb\x23\xdf\xaa\x52\x98\x18\xd1\x12\x5c"
  3620. "\xd5\x34\x80\x92\x52\x43\x66\xb8\x5f\xab\xb9\x7c\x6c\xd1\xe6"
  3621. "\x06\x6f\x45\x9b\xcc\x56\x6d\xa8\x7e\xc9\xb7\xba\x36\x79\x2d"
  3622. "\x11\x8a\xc3\x9a\x4c\xce\xf6\x19\x2b\xbf\x3a\x54\xaf\x18\xe5"
  3623. "\x7b\x0c\x14\x61\x01\xf6\xae\xaa\x82\x2b\xc4\xb4\xc9\x70\x8b"
  3624. "\x09\xf0\xb3\xba\xb4\x1b\xcc\xe9\x64\xd9\x99\xd1\x10\x7b\xd7"
  3625. "\xc2";
  3626. d.inLen = 32;
  3627. d.outLen = 136;
  3628. /* Taken from NIST CAVP test vectors - more than one output block. */
  3629. e.input = "\x8d\x80\x01\xe2\xc0\x96\xf1\xb8\x8e\x7c\x92\x24\xa0\x86\xef"
  3630. "\xd4\x79\x7f\xbf\x74\xa8\x03\x3a\x2d\x42\x2a\x2b\x6b\x8f\x67"
  3631. "\x47\xe4";
  3632. e.output = "\x2e\x97\x5f\x6a\x8a\x14\xf0\x70\x4d\x51\xb1\x36\x67\xd8\x19"
  3633. "\x5c\x21\x9f\x71\xe6\x34\x56\x96\xc4\x9f\xa4\xb9\xd0\x8e\x92"
  3634. "\x25\xd3\xd3\x93\x93\x42\x51\x52\xc9\x7e\x71\xdd\x24\x60\x1c"
  3635. "\x11\xab\xcf\xa0\xf1\x2f\x53\xc6\x80\xbd\x3a\xe7\x57\xb8\x13"
  3636. "\x4a\x9c\x10\xd4\x29\x61\x58\x69\x21\x7f\xdd\x58\x85\xc4\xdb"
  3637. "\x17\x49\x85\x70\x3a\x6d\x6d\xe9\x4a\x66\x7e\xac\x30\x23\x44"
  3638. "\x3a\x83\x37\xae\x1b\xc6\x01\xb7\x6d\x7d\x38\xec\x3c\x34\x46"
  3639. "\x31\x05\xf0\xd3\x94\x9d\x78\xe5\x62\xa0\x39\xe4\x46\x95\x48"
  3640. "\xb6\x09\x39\x5d\xe5\xa4\xfd\x43\xc4\x6c\xa9\xfd\x6e\xe2\x9a"
  3641. "\xda\x5e\xfc\x07\xd8\x4d\x55\x32\x49\x45\x0d\xab\x4a\x49\xc4"
  3642. "\x83\xde\xd2\x50\xc9\x33\x8f\x85\xcd\x93\x7a\xe6\x6b\xb4\x36"
  3643. "\xf3\xb4\x02\x6e\x85\x9f\xda\x1c\xa5\x71\x43\x2f\x3b\xfc\x09"
  3644. "\xe7\xc0\x3c\xa4\xd1\x83\xb7\x41\x11\x1c\xa0\x48\x3d\x0e\xda"
  3645. "\xbc\x03\xfe\xb2\x3b\x17\xee\x48\xe8\x44\xba\x24\x08\xd9\xdc"
  3646. "\xfd\x01\x39\xd2\xe8\xc7\x31\x01\x25\xae\xe8\x01\xc6\x1a\xb7"
  3647. "\x90\x0d\x1e\xfc\x47\xc0\x78\x28\x17\x66\xf3\x61\xc5\xe6\x11"
  3648. "\x13\x46\x23\x5e\x1d\xc3\x83\x25\x66\x6c";
  3649. e.inLen = 32;
  3650. e.outLen = 250;
  3651. test_sha[0] = a;
  3652. test_sha[1] = b;
  3653. test_sha[2] = c;
  3654. test_sha[3] = d;
  3655. test_sha[4] = e;
  3656. ret = wc_InitShake256(&sha, HEAP_HINT, devId);
  3657. if (ret != 0)
  3658. return -3100;
  3659. for (i = 0; i < times; ++i) {
  3660. ret = wc_Shake256_Update(&sha, (byte*)test_sha[i].input,
  3661. (word32)test_sha[i].inLen);
  3662. if (ret != 0)
  3663. ERROR_OUT(-3101 - i, exit);
  3664. ret = wc_Shake256_Final(&sha, hash, (word32)test_sha[i].outLen);
  3665. if (ret != 0)
  3666. ERROR_OUT(-3102 - i, exit);
  3667. if (XMEMCMP(hash, test_sha[i].output, test_sha[i].outLen) != 0)
  3668. ERROR_OUT(-3103 - i, exit);
  3669. }
  3670. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  3671. large_input = (byte *)XMALLOC(SHAKE256_LARGE_INPUT_BUFSIZ, NULL,
  3672. DYNAMIC_TYPE_TMP_BUFFER);
  3673. if (large_input == NULL)
  3674. ERROR_OUT(-3107, exit);
  3675. #endif
  3676. /* BEGIN LARGE HASH TEST */ {
  3677. for (i = 0; i < SHAKE256_LARGE_INPUT_BUFSIZ; i++) {
  3678. large_input[i] = (byte)(i & 0xFF);
  3679. }
  3680. times = 100;
  3681. for (i = 0; i < times; ++i) {
  3682. ret = wc_Shake256_Update(&sha, (byte*)large_input,
  3683. SHAKE256_LARGE_INPUT_BUFSIZ);
  3684. if (ret != 0)
  3685. ERROR_OUT(-3104, exit);
  3686. }
  3687. ret = wc_Shake256_Final(&sha, hash, (word32)sizeof(hash));
  3688. if (ret != 0)
  3689. ERROR_OUT(-3105, exit);
  3690. if (XMEMCMP(hash, large_digest, 114) != 0)
  3691. ERROR_OUT(-3106, exit);
  3692. } /* END LARGE HASH TEST */
  3693. ret = shake256_absorb_test(&sha, large_input, SHAKE256_LARGE_INPUT_BUFSIZ);
  3694. exit:
  3695. wc_Shake256_Free(&sha);
  3696. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  3697. if (large_input != NULL)
  3698. XFREE(large_input, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3699. #endif
  3700. return ret;
  3701. }
  3702. #endif
  3703. #ifndef NO_HASH_WRAPPER
  3704. WOLFSSL_TEST_SUBROUTINE int hash_test(void)
  3705. {
  3706. wc_HashAlg hash;
  3707. int ret, exp_ret;
  3708. int i, j;
  3709. int digestSz;
  3710. byte data[] = "0123456789abcdef0123456789abcdef0123456";
  3711. byte out[WC_MAX_DIGEST_SIZE];
  3712. byte hashOut[WC_MAX_DIGEST_SIZE];
  3713. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  3714. enum wc_HashType hashType;
  3715. #endif
  3716. enum wc_HashType typesGood[] = { WC_HASH_TYPE_MD5, WC_HASH_TYPE_SHA,
  3717. WC_HASH_TYPE_SHA224, WC_HASH_TYPE_SHA256,
  3718. WC_HASH_TYPE_SHA384, WC_HASH_TYPE_SHA512,
  3719. WC_HASH_TYPE_SHA3_224,
  3720. WC_HASH_TYPE_SHA3_256,
  3721. WC_HASH_TYPE_SHA3_384,
  3722. WC_HASH_TYPE_SHA3_512 };
  3723. enum wc_HashType typesNoImpl[] = {
  3724. #ifdef NO_MD5
  3725. WC_HASH_TYPE_MD5,
  3726. #endif
  3727. #ifdef NO_SHA
  3728. WC_HASH_TYPE_SHA,
  3729. #endif
  3730. #ifndef WOLFSSL_SHA224
  3731. WC_HASH_TYPE_SHA224,
  3732. #endif
  3733. #ifdef NO_SHA256
  3734. WC_HASH_TYPE_SHA256,
  3735. #endif
  3736. #ifndef WOLFSSL_SHA384
  3737. WC_HASH_TYPE_SHA384,
  3738. #endif
  3739. #ifndef WOLFSSL_SHA512
  3740. WC_HASH_TYPE_SHA512,
  3741. #endif
  3742. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_224)
  3743. WC_HASH_TYPE_SHA3_224,
  3744. #endif
  3745. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_256)
  3746. WC_HASH_TYPE_SHA3_256,
  3747. #endif
  3748. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_384)
  3749. WC_HASH_TYPE_SHA3_384,
  3750. #endif
  3751. #if !defined(WOLFSSL_SHA3) || defined(WOLFSSL_NOSHA3_512)
  3752. WC_HASH_TYPE_SHA3_512,
  3753. #endif
  3754. WC_HASH_TYPE_NONE
  3755. };
  3756. enum wc_HashType typesBad[] = { WC_HASH_TYPE_NONE, WC_HASH_TYPE_MD5_SHA,
  3757. WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4 };
  3758. enum wc_HashType typesHashBad[] = { WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4,
  3759. WC_HASH_TYPE_BLAKE2B,
  3760. WC_HASH_TYPE_NONE };
  3761. /* Parameter Validation testing. */
  3762. ret = wc_HashInit(NULL, WC_HASH_TYPE_SHA256);
  3763. if (ret != BAD_FUNC_ARG)
  3764. return -3200;
  3765. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  3766. if (ret != BAD_FUNC_ARG)
  3767. return -3201;
  3768. ret = wc_HashUpdate(&hash, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  3769. if (ret != BAD_FUNC_ARG)
  3770. return -3202;
  3771. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, data, sizeof(data));
  3772. if (ret != BAD_FUNC_ARG)
  3773. return -3203;
  3774. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, NULL);
  3775. if (ret != BAD_FUNC_ARG)
  3776. return -3204;
  3777. ret = wc_HashFinal(&hash, WC_HASH_TYPE_SHA256, NULL);
  3778. if (ret != BAD_FUNC_ARG)
  3779. return -3205;
  3780. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, out);
  3781. if (ret != BAD_FUNC_ARG)
  3782. return -3206;
  3783. /* Try invalid hash algorithms. */
  3784. for (i = 0; i < (int)(sizeof(typesBad)/sizeof(*typesBad)); i++) {
  3785. ret = wc_HashInit(&hash, typesBad[i]);
  3786. if (ret != BAD_FUNC_ARG)
  3787. return -3207 - i;
  3788. ret = wc_HashUpdate(&hash, typesBad[i], data, sizeof(data));
  3789. if (ret != BAD_FUNC_ARG)
  3790. return -3217 - i;
  3791. ret = wc_HashFinal(&hash, typesBad[i], out);
  3792. if (ret != BAD_FUNC_ARG)
  3793. return -3227 - i;
  3794. wc_HashFree(&hash, typesBad[i]);
  3795. }
  3796. /* Try valid hash algorithms. */
  3797. for (i = 0, j = 0; i < (int)(sizeof(typesGood)/sizeof(*typesGood)); i++) {
  3798. exp_ret = 0;
  3799. if (typesGood[i] == typesNoImpl[j]) {
  3800. /* Recognized but no implementation compiled in. */
  3801. exp_ret = HASH_TYPE_E;
  3802. j++;
  3803. }
  3804. ret = wc_HashInit(&hash, typesGood[i]);
  3805. if (ret != exp_ret)
  3806. return -3237 - i;
  3807. ret = wc_HashUpdate(&hash, typesGood[i], data, sizeof(data));
  3808. if (ret != exp_ret)
  3809. return -3247 - i;
  3810. ret = wc_HashFinal(&hash, typesGood[i], out);
  3811. if (ret != exp_ret)
  3812. return -3257 - i;
  3813. wc_HashFree(&hash, typesGood[i]);
  3814. digestSz = wc_HashGetDigestSize(typesGood[i]);
  3815. if (exp_ret < 0 && digestSz != exp_ret)
  3816. return -3267 - i;
  3817. if (exp_ret == 0 && digestSz < 0)
  3818. return -3277 - i;
  3819. if (exp_ret == 0) {
  3820. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut,
  3821. digestSz - 1);
  3822. if (ret != BUFFER_E)
  3823. return -3287 - i;
  3824. }
  3825. ret = wc_Hash(typesGood[i], data, sizeof(data), hashOut, digestSz);
  3826. if (ret != exp_ret)
  3827. return -3297 - i;
  3828. if (exp_ret == 0 && XMEMCMP(out, hashOut, digestSz) != 0)
  3829. return -3307 -i;
  3830. ret = wc_HashGetBlockSize(typesGood[i]);
  3831. if (exp_ret < 0 && ret != exp_ret)
  3832. return -3308 - i;
  3833. if (exp_ret == 0 && ret < 0)
  3834. return -3318 - i;
  3835. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  3836. ret = wc_HashGetOID(typesGood[i]);
  3837. if (ret == BAD_FUNC_ARG ||
  3838. (exp_ret == 0 && ret == HASH_TYPE_E) ||
  3839. (exp_ret != 0 && ret != HASH_TYPE_E)) {
  3840. return -3328 - i;
  3841. }
  3842. hashType = wc_OidGetHash(ret);
  3843. if (exp_ret == 0 && hashType != typesGood[i])
  3844. return -3338 - i;
  3845. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  3846. }
  3847. for (i = 0; i < (int)(sizeof(typesHashBad)/sizeof(*typesHashBad)); i++) {
  3848. ret = wc_Hash(typesHashBad[i], data, sizeof(data), out, sizeof(out));
  3849. if (ret != BAD_FUNC_ARG && ret != BUFFER_E)
  3850. return -3348 - i;
  3851. }
  3852. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  3853. ret = wc_HashGetOID(WC_HASH_TYPE_MD2);
  3854. #ifdef WOLFSSL_MD2
  3855. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3856. return -3358;
  3857. #else
  3858. if (ret != HASH_TYPE_E)
  3859. return -3359;
  3860. #endif
  3861. hashType = wc_OidGetHash(646); /* Md2h */
  3862. #ifdef WOLFSSL_MD2
  3863. if (hashType != WC_HASH_TYPE_MD2)
  3864. return -3360;
  3865. #else
  3866. if (hashType != WC_HASH_TYPE_NONE)
  3867. return -3361;
  3868. #endif
  3869. ret = wc_HashGetOID(WC_HASH_TYPE_MD5_SHA);
  3870. #ifndef NO_MD5
  3871. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3872. return -3362;
  3873. #else
  3874. if (ret != HASH_TYPE_E)
  3875. return -3363;
  3876. #endif
  3877. ret = wc_HashGetOID(WC_HASH_TYPE_MD4);
  3878. if (ret != BAD_FUNC_ARG)
  3879. return -3364;
  3880. ret = wc_HashGetOID(WC_HASH_TYPE_NONE);
  3881. if (ret != BAD_FUNC_ARG)
  3882. return -3365;
  3883. hashType = wc_OidGetHash(0);
  3884. if (hashType != WC_HASH_TYPE_NONE)
  3885. return -3366;
  3886. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  3887. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD2);
  3888. #ifdef WOLFSSL_MD2
  3889. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3890. return -3367;
  3891. #else
  3892. if (ret != HASH_TYPE_E)
  3893. return -3368;
  3894. #endif
  3895. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD2);
  3896. #ifdef WOLFSSL_MD2
  3897. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3898. return -3369;
  3899. #else
  3900. if (ret != HASH_TYPE_E)
  3901. return -3370;
  3902. #endif
  3903. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD4);
  3904. #ifndef NO_MD4
  3905. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3906. return -3371;
  3907. #else
  3908. if (ret != HASH_TYPE_E)
  3909. return -3372;
  3910. #endif
  3911. ret = wc_HashGetDigestSize(WC_HASH_TYPE_MD4);
  3912. #ifndef NO_MD4
  3913. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3914. return -3373;
  3915. #else
  3916. if (ret != HASH_TYPE_E)
  3917. return -3374;
  3918. #endif
  3919. ret = wc_HashGetBlockSize(WC_HASH_TYPE_MD5_SHA);
  3920. #if !defined(NO_MD5) && !defined(NO_SHA)
  3921. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3922. return -3375;
  3923. #else
  3924. if (ret != HASH_TYPE_E)
  3925. return -3376;
  3926. #endif
  3927. ret = wc_HashGetBlockSize(WC_HASH_TYPE_BLAKE2B);
  3928. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  3929. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3930. return -3377;
  3931. #else
  3932. if (ret != HASH_TYPE_E)
  3933. return -3378;
  3934. #endif
  3935. ret = wc_HashGetDigestSize(WC_HASH_TYPE_BLAKE2B);
  3936. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  3937. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  3938. return -3379;
  3939. #else
  3940. if (ret != HASH_TYPE_E)
  3941. return -3380;
  3942. #endif
  3943. ret = wc_HashGetBlockSize(WC_HASH_TYPE_NONE);
  3944. if (ret != BAD_FUNC_ARG)
  3945. return -3381;
  3946. ret = wc_HashGetDigestSize(WC_HASH_TYPE_NONE);
  3947. if (ret != BAD_FUNC_ARG)
  3948. return -3382;
  3949. #if !defined(NO_CERTS) && !defined(NO_ASN)
  3950. #if defined(WOLFSSL_MD2) && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  3951. ret = wc_GetCTC_HashOID(MD2);
  3952. if (ret == 0)
  3953. return -3383;
  3954. #endif
  3955. #ifndef NO_MD5
  3956. ret = wc_GetCTC_HashOID(WC_MD5);
  3957. if (ret == 0)
  3958. return -3384;
  3959. #endif
  3960. #ifndef NO_SHA
  3961. ret = wc_GetCTC_HashOID(WC_SHA);
  3962. if (ret == 0)
  3963. return -3385;
  3964. #endif
  3965. #ifdef WOLFSSL_SHA224
  3966. ret = wc_GetCTC_HashOID(WC_SHA224);
  3967. if (ret == 0)
  3968. return -3386;
  3969. #endif
  3970. #ifndef NO_SHA256
  3971. ret = wc_GetCTC_HashOID(WC_SHA256);
  3972. if (ret == 0)
  3973. return -3387;
  3974. #endif
  3975. #ifdef WOLFSSL_SHA384
  3976. ret = wc_GetCTC_HashOID(WC_SHA384);
  3977. if (ret == 0)
  3978. return -3388;
  3979. #endif
  3980. #ifdef WOLFSSL_SHA512
  3981. ret = wc_GetCTC_HashOID(WC_SHA512);
  3982. if (ret == 0)
  3983. return -3389;
  3984. #endif
  3985. ret = wc_GetCTC_HashOID(-1);
  3986. if (ret != 0)
  3987. return -3390;
  3988. #endif
  3989. return 0;
  3990. }
  3991. #endif /* !NO_HASH_WRAPPER */
  3992. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS) && \
  3993. defined(HAVE_FIPS_VERSION) && \
  3994. (HAVE_FIPS_VERSION >= 5))
  3995. WOLFSSL_TEST_SUBROUTINE int hmac_md5_test(void)
  3996. {
  3997. Hmac hmac;
  3998. byte hash[WC_MD5_DIGEST_SIZE];
  3999. const char* keys[]=
  4000. {
  4001. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  4002. "Jefe",
  4003. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4004. };
  4005. testVector a, b, c;
  4006. testVector test_hmac[3];
  4007. int ret;
  4008. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4009. a.input = "Hi There";
  4010. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  4011. "\x9d";
  4012. a.inLen = XSTRLEN(a.input);
  4013. a.outLen = WC_MD5_DIGEST_SIZE;
  4014. b.input = "what do ya want for nothing?";
  4015. b.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
  4016. "\x38";
  4017. b.inLen = XSTRLEN(b.input);
  4018. b.outLen = WC_MD5_DIGEST_SIZE;
  4019. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4020. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4021. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4022. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4023. c.output = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
  4024. "\xf6";
  4025. c.inLen = XSTRLEN(c.input);
  4026. c.outLen = WC_MD5_DIGEST_SIZE;
  4027. test_hmac[0] = a;
  4028. test_hmac[1] = b;
  4029. test_hmac[2] = c;
  4030. for (i = 0; i < times; ++i) {
  4031. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4032. if (i == 1) {
  4033. continue; /* cavium can't handle short keys, fips not allowed */
  4034. }
  4035. #endif
  4036. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0) {
  4037. return -3400;
  4038. }
  4039. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[i],
  4040. (word32)XSTRLEN(keys[i]));
  4041. if (ret != 0)
  4042. return -3401;
  4043. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4044. (word32)test_hmac[i].inLen);
  4045. if (ret != 0)
  4046. return -3402;
  4047. ret = wc_HmacFinal(&hmac, hash);
  4048. if (ret != 0)
  4049. return -3403;
  4050. if (XMEMCMP(hash, test_hmac[i].output, WC_MD5_DIGEST_SIZE) != 0)
  4051. return -3404 - i;
  4052. wc_HmacFree(&hmac);
  4053. }
  4054. #ifndef HAVE_FIPS
  4055. if (wc_HmacSizeByType(WC_MD5) != WC_MD5_DIGEST_SIZE)
  4056. return -3414;
  4057. #endif
  4058. return 0;
  4059. }
  4060. #endif /* !NO_HMAC && !NO_MD5 && (!HAVE_FIPS || (HAVE_FIPS_VERSION < 5)) */
  4061. #if !defined(NO_HMAC) && !defined(NO_SHA)
  4062. WOLFSSL_TEST_SUBROUTINE int hmac_sha_test(void)
  4063. {
  4064. Hmac hmac;
  4065. byte hash[WC_SHA_DIGEST_SIZE];
  4066. const char* keys[]=
  4067. {
  4068. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4069. "\x0b\x0b\x0b",
  4070. "Jefe",
  4071. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4072. "\xAA\xAA\xAA"
  4073. };
  4074. testVector a, b, c;
  4075. testVector test_hmac[3];
  4076. int ret;
  4077. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4078. a.input = "Hi There";
  4079. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  4080. "\x8e\xf1\x46\xbe\x00";
  4081. a.inLen = XSTRLEN(a.input);
  4082. a.outLen = WC_SHA_DIGEST_SIZE;
  4083. b.input = "what do ya want for nothing?";
  4084. b.output = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf"
  4085. "\x9c\x25\x9a\x7c\x79";
  4086. b.inLen = XSTRLEN(b.input);
  4087. b.outLen = WC_SHA_DIGEST_SIZE;
  4088. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4089. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4090. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4091. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4092. c.output = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b"
  4093. "\x4f\x63\xf1\x75\xd3";
  4094. c.inLen = XSTRLEN(c.input);
  4095. c.outLen = WC_SHA_DIGEST_SIZE;
  4096. test_hmac[0] = a;
  4097. test_hmac[1] = b;
  4098. test_hmac[2] = c;
  4099. for (i = 0; i < times; ++i) {
  4100. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4101. if (i == 1)
  4102. continue; /* cavium can't handle short keys, fips not allowed */
  4103. #endif
  4104. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4105. return -3500;
  4106. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[i],
  4107. (word32)XSTRLEN(keys[i]));
  4108. if (ret != 0)
  4109. return -3501;
  4110. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4111. (word32)test_hmac[i].inLen);
  4112. if (ret != 0)
  4113. return -3502;
  4114. ret = wc_HmacFinal(&hmac, hash);
  4115. if (ret != 0)
  4116. return -3503;
  4117. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA_DIGEST_SIZE) != 0)
  4118. return -3504 - i;
  4119. wc_HmacFree(&hmac);
  4120. }
  4121. #ifndef HAVE_FIPS
  4122. if (wc_HmacSizeByType(WC_SHA) != WC_SHA_DIGEST_SIZE)
  4123. return -3514;
  4124. #endif
  4125. return 0;
  4126. }
  4127. #endif
  4128. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  4129. WOLFSSL_TEST_SUBROUTINE int hmac_sha224_test(void)
  4130. {
  4131. Hmac hmac;
  4132. byte hash[WC_SHA224_DIGEST_SIZE];
  4133. const char* keys[]=
  4134. {
  4135. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4136. "\x0b\x0b\x0b",
  4137. "Jefe",
  4138. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4139. "\xAA\xAA\xAA",
  4140. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4141. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4142. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4143. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4144. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4145. };
  4146. testVector a, b, c, d;
  4147. testVector test_hmac[4];
  4148. int ret;
  4149. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4150. a.input = "Hi There";
  4151. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  4152. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  4153. a.inLen = XSTRLEN(a.input);
  4154. a.outLen = WC_SHA224_DIGEST_SIZE;
  4155. b.input = "what do ya want for nothing?";
  4156. b.output = "\xa3\x0e\x01\x09\x8b\xc6\xdb\xbf\x45\x69\x0f\x3a\x7e\x9e\x6d"
  4157. "\x0f\x8b\xbe\xa2\xa3\x9e\x61\x48\x00\x8f\xd0\x5e\x44";
  4158. b.inLen = XSTRLEN(b.input);
  4159. b.outLen = WC_SHA224_DIGEST_SIZE;
  4160. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4161. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4162. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4163. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4164. c.output = "\x7f\xb3\xcb\x35\x88\xc6\xc1\xf6\xff\xa9\x69\x4d\x7d\x6a\xd2"
  4165. "\x64\x93\x65\xb0\xc1\xf6\x5d\x69\xd1\xec\x83\x33\xea";
  4166. c.inLen = XSTRLEN(c.input);
  4167. c.outLen = WC_SHA224_DIGEST_SIZE;
  4168. d.input = "Big Key Input";
  4169. d.output = "\xe7\x4e\x2b\x8a\xa9\xf0\x37\x2f\xed\xae\x70\x0c\x49\x47\xf1"
  4170. "\x46\x54\xa7\x32\x6b\x55\x01\x87\xd2\xc8\x02\x0e\x3a";
  4171. d.inLen = XSTRLEN(d.input);
  4172. d.outLen = WC_SHA224_DIGEST_SIZE;
  4173. test_hmac[0] = a;
  4174. test_hmac[1] = b;
  4175. test_hmac[2] = c;
  4176. test_hmac[3] = d;
  4177. for (i = 0; i < times; ++i) {
  4178. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4179. if (i == 1)
  4180. continue; /* cavium can't handle short keys, fips not allowed */
  4181. #endif
  4182. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4183. return -3600;
  4184. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[i],
  4185. (word32)XSTRLEN(keys[i]));
  4186. if (ret != 0)
  4187. return -3601;
  4188. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4189. (word32)test_hmac[i].inLen);
  4190. if (ret != 0)
  4191. return -3602;
  4192. ret = wc_HmacFinal(&hmac, hash);
  4193. if (ret != 0)
  4194. return -3603;
  4195. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  4196. return -3604 - i;
  4197. wc_HmacFree(&hmac);
  4198. }
  4199. #ifndef HAVE_FIPS
  4200. if (wc_HmacSizeByType(WC_SHA224) != WC_SHA224_DIGEST_SIZE)
  4201. return -3614;
  4202. #endif
  4203. return 0;
  4204. }
  4205. #endif
  4206. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  4207. WOLFSSL_TEST_SUBROUTINE int hmac_sha256_test(void)
  4208. {
  4209. Hmac hmac;
  4210. byte hash[WC_SHA256_DIGEST_SIZE];
  4211. const char* keys[]=
  4212. {
  4213. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4214. "\x0b\x0b\x0b",
  4215. "Jefe",
  4216. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4217. "\xAA\xAA\xAA",
  4218. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4219. "\xAA\xAA\xAA",
  4220. };
  4221. testVector a, b, c, d;
  4222. testVector test_hmac[4];
  4223. int ret;
  4224. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4225. a.input = "Hi There";
  4226. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  4227. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  4228. "\xcf\xf7";
  4229. a.inLen = XSTRLEN(a.input);
  4230. a.outLen = WC_SHA256_DIGEST_SIZE;
  4231. b.input = "what do ya want for nothing?";
  4232. b.output = "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75"
  4233. "\xc7\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec"
  4234. "\x38\x43";
  4235. b.inLen = XSTRLEN(b.input);
  4236. b.outLen = WC_SHA256_DIGEST_SIZE;
  4237. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4238. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4239. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4240. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4241. c.output = "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81"
  4242. "\xa7\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5"
  4243. "\x65\xfe";
  4244. c.inLen = XSTRLEN(c.input);
  4245. c.outLen = WC_SHA256_DIGEST_SIZE;
  4246. d.input = 0;
  4247. d.output = "\x86\xe5\x4f\xd4\x48\x72\x5d\x7e\x5d\xcf\xe2\x23\x53\xc8\x28"
  4248. "\xaf\x48\x78\x1e\xb4\x8c\xae\x81\x06\xa7\xe1\xd4\x98\x94\x9f"
  4249. "\x3e\x46";
  4250. d.inLen = 0;
  4251. d.outLen = WC_SHA256_DIGEST_SIZE;
  4252. test_hmac[0] = a;
  4253. test_hmac[1] = b;
  4254. test_hmac[2] = c;
  4255. test_hmac[3] = d;
  4256. for (i = 0; i < times; ++i) {
  4257. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  4258. if (i == 1)
  4259. continue; /* cavium can't handle short keys, fips not allowed */
  4260. #endif
  4261. #if defined(HAVE_INTEL_QA) || defined(HAVE_CAVIUM)
  4262. if (i == 3)
  4263. continue; /* QuickAssist can't handle empty HMAC */
  4264. #endif
  4265. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4266. return -3700 - i;
  4267. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[i],
  4268. (word32)XSTRLEN(keys[i]));
  4269. if (ret != 0)
  4270. return -3710 - i;
  4271. if (test_hmac[i].input != NULL) {
  4272. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4273. (word32)test_hmac[i].inLen);
  4274. if (ret != 0)
  4275. return -3720 - i;
  4276. }
  4277. ret = wc_HmacFinal(&hmac, hash);
  4278. if (ret != 0)
  4279. return -3730 - i;
  4280. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  4281. return -3740 - i;
  4282. wc_HmacFree(&hmac);
  4283. }
  4284. #ifndef HAVE_FIPS
  4285. if (wc_HmacSizeByType(WC_SHA256) != WC_SHA256_DIGEST_SIZE)
  4286. return -3750;
  4287. if (wc_HmacSizeByType(20) != BAD_FUNC_ARG)
  4288. return -3751;
  4289. #endif
  4290. if (wolfSSL_GetHmacMaxSize() != WC_MAX_DIGEST_SIZE)
  4291. return -3752;
  4292. return 0;
  4293. }
  4294. #endif
  4295. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  4296. WOLFSSL_TEST_SUBROUTINE int hmac_sha384_test(void)
  4297. {
  4298. Hmac hmac;
  4299. byte hash[WC_SHA384_DIGEST_SIZE];
  4300. const char* keys[]=
  4301. {
  4302. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4303. "\x0b\x0b\x0b",
  4304. "Jefe",
  4305. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4306. "\xAA\xAA\xAA",
  4307. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4308. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4309. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4310. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4311. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4312. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4313. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4314. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4315. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4316. };
  4317. testVector a, b, c, d;
  4318. testVector test_hmac[4];
  4319. int ret;
  4320. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4321. a.input = "Hi There";
  4322. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  4323. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  4324. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  4325. "\xfa\x9c\xb6";
  4326. a.inLen = XSTRLEN(a.input);
  4327. a.outLen = WC_SHA384_DIGEST_SIZE;
  4328. b.input = "what do ya want for nothing?";
  4329. b.output = "\xaf\x45\xd2\xe3\x76\x48\x40\x31\x61\x7f\x78\xd2\xb5\x8a\x6b"
  4330. "\x1b\x9c\x7e\xf4\x64\xf5\xa0\x1b\x47\xe4\x2e\xc3\x73\x63\x22"
  4331. "\x44\x5e\x8e\x22\x40\xca\x5e\x69\xe2\xc7\x8b\x32\x39\xec\xfa"
  4332. "\xb2\x16\x49";
  4333. b.inLen = XSTRLEN(b.input);
  4334. b.outLen = WC_SHA384_DIGEST_SIZE;
  4335. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4336. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4337. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4338. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4339. c.output = "\x88\x06\x26\x08\xd3\xe6\xad\x8a\x0a\xa2\xac\xe0\x14\xc8\xa8"
  4340. "\x6f\x0a\xa6\x35\xd9\x47\xac\x9f\xeb\xe8\x3e\xf4\xe5\x59\x66"
  4341. "\x14\x4b\x2a\x5a\xb3\x9d\xc1\x38\x14\xb9\x4e\x3a\xb6\xe1\x01"
  4342. "\xa3\x4f\x27";
  4343. c.inLen = XSTRLEN(c.input);
  4344. c.outLen = WC_SHA384_DIGEST_SIZE;
  4345. d.input = "Big Key Input";
  4346. d.output = "\xd2\x3d\x29\x6e\xf5\x1e\x23\x23\x49\x18\xb3\xbf\x4c\x38\x7b"
  4347. "\x31\x21\x17\xbb\x09\x73\x27\xf8\x12\x9d\xe9\xc6\x5d\xf9\x54"
  4348. "\xd6\x38\x5a\x68\x53\x14\xee\xe0\xa6\x4f\x36\x7e\xb2\xf3\x1a"
  4349. "\x57\x41\x69";
  4350. d.inLen = XSTRLEN(d.input);
  4351. d.outLen = WC_SHA384_DIGEST_SIZE;
  4352. test_hmac[0] = a;
  4353. test_hmac[1] = b;
  4354. test_hmac[2] = c;
  4355. test_hmac[3] = d;
  4356. for (i = 0; i < times; ++i) {
  4357. #if defined(HAVE_FIPS)
  4358. if (i == 1)
  4359. continue; /* fips not allowed */
  4360. #endif
  4361. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4362. return -3800;
  4363. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[i],
  4364. (word32)XSTRLEN(keys[i]));
  4365. if (ret != 0)
  4366. return -3801;
  4367. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4368. (word32)test_hmac[i].inLen);
  4369. if (ret != 0)
  4370. return -3802;
  4371. ret = wc_HmacFinal(&hmac, hash);
  4372. if (ret != 0)
  4373. return -3803;
  4374. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  4375. return -3804 - i;
  4376. wc_HmacFree(&hmac);
  4377. }
  4378. #ifndef HAVE_FIPS
  4379. if (wc_HmacSizeByType(WC_SHA384) != WC_SHA384_DIGEST_SIZE)
  4380. return -3814;
  4381. #endif
  4382. return 0;
  4383. }
  4384. #endif
  4385. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA512)
  4386. WOLFSSL_TEST_SUBROUTINE int hmac_sha512_test(void)
  4387. {
  4388. Hmac hmac;
  4389. byte hash[WC_SHA512_DIGEST_SIZE];
  4390. const char* keys[]=
  4391. {
  4392. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4393. "\x0b\x0b\x0b",
  4394. "Jefe",
  4395. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  4396. "\xAA\xAA\xAA",
  4397. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4398. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4399. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4400. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4401. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4402. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4403. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4404. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4405. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4406. };
  4407. testVector a, b, c, d;
  4408. testVector test_hmac[4];
  4409. int ret;
  4410. int times = sizeof(test_hmac) / sizeof(testVector), i;
  4411. a.input = "Hi There";
  4412. a.output = "\x87\xaa\x7c\xde\xa5\xef\x61\x9d\x4f\xf0\xb4\x24\x1a\x1d\x6c"
  4413. "\xb0\x23\x79\xf4\xe2\xce\x4e\xc2\x78\x7a\xd0\xb3\x05\x45\xe1"
  4414. "\x7c\xde\xda\xa8\x33\xb7\xd6\xb8\xa7\x02\x03\x8b\x27\x4e\xae"
  4415. "\xa3\xf4\xe4\xbe\x9d\x91\x4e\xeb\x61\xf1\x70\x2e\x69\x6c\x20"
  4416. "\x3a\x12\x68\x54";
  4417. a.inLen = XSTRLEN(a.input);
  4418. a.outLen = WC_SHA512_DIGEST_SIZE;
  4419. b.input = "what do ya want for nothing?";
  4420. b.output = "\x16\x4b\x7a\x7b\xfc\xf8\x19\xe2\xe3\x95\xfb\xe7\x3b\x56\xe0"
  4421. "\xa3\x87\xbd\x64\x22\x2e\x83\x1f\xd6\x10\x27\x0c\xd7\xea\x25"
  4422. "\x05\x54\x97\x58\xbf\x75\xc0\x5a\x99\x4a\x6d\x03\x4f\x65\xf8"
  4423. "\xf0\xe6\xfd\xca\xea\xb1\xa3\x4d\x4a\x6b\x4b\x63\x6e\x07\x0a"
  4424. "\x38\xbc\xe7\x37";
  4425. b.inLen = XSTRLEN(b.input);
  4426. b.outLen = WC_SHA512_DIGEST_SIZE;
  4427. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4428. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4429. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  4430. "\xDD\xDD\xDD\xDD\xDD\xDD";
  4431. c.output = "\xfa\x73\xb0\x08\x9d\x56\xa2\x84\xef\xb0\xf0\x75\x6c\x89\x0b"
  4432. "\xe9\xb1\xb5\xdb\xdd\x8e\xe8\x1a\x36\x55\xf8\x3e\x33\xb2\x27"
  4433. "\x9d\x39\xbf\x3e\x84\x82\x79\xa7\x22\xc8\x06\xb4\x85\xa4\x7e"
  4434. "\x67\xc8\x07\xb9\x46\xa3\x37\xbe\xe8\x94\x26\x74\x27\x88\x59"
  4435. "\xe1\x32\x92\xfb";
  4436. c.inLen = XSTRLEN(c.input);
  4437. c.outLen = WC_SHA512_DIGEST_SIZE;
  4438. d.input = "Big Key Input";
  4439. d.output = "\x3f\xa9\xc9\xe1\xbd\xbb\x04\x55\x1f\xef\xcc\x92\x33\x08\xeb"
  4440. "\xcf\xc1\x9a\x5b\x5b\xc0\x7c\x86\x84\xae\x8c\x40\xaf\xb1\x27"
  4441. "\x87\x38\x92\x04\xa8\xed\xd7\xd7\x07\xa9\x85\xa0\xc2\xcd\x30"
  4442. "\xc0\x56\x14\x49\xbc\x2f\x69\x15\x6a\x97\xd8\x79\x2f\xb3\x3b"
  4443. "\x1e\x18\xfe\xfa";
  4444. d.inLen = XSTRLEN(d.input);
  4445. d.outLen = WC_SHA512_DIGEST_SIZE;
  4446. test_hmac[0] = a;
  4447. test_hmac[1] = b;
  4448. test_hmac[2] = c;
  4449. test_hmac[3] = d;
  4450. for (i = 0; i < times; ++i) {
  4451. #if defined(HAVE_FIPS)
  4452. if (i == 1)
  4453. continue; /* fips not allowed */
  4454. #endif
  4455. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4456. return -3900;
  4457. ret = wc_HmacSetKey(&hmac, WC_SHA512, (byte*)keys[i],
  4458. (word32)XSTRLEN(keys[i]));
  4459. if (ret != 0)
  4460. return -3901;
  4461. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  4462. (word32)test_hmac[i].inLen);
  4463. if (ret != 0)
  4464. return -3902;
  4465. ret = wc_HmacFinal(&hmac, hash);
  4466. if (ret != 0)
  4467. return -3903;
  4468. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  4469. return -3904 - i;
  4470. wc_HmacFree(&hmac);
  4471. }
  4472. #ifndef HAVE_FIPS
  4473. if (wc_HmacSizeByType(WC_SHA512) != WC_SHA512_DIGEST_SIZE)
  4474. return -3914;
  4475. #endif
  4476. return 0;
  4477. }
  4478. #endif
  4479. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA3) && \
  4480. !defined(WOLFSSL_NOSHA3_224) && !defined(WOLFSSL_NOSHA3_256) && \
  4481. !defined(WOLFSSL_NOSHA3_384) && !defined(WOLFSSL_NOSHA3_512)
  4482. WOLFSSL_TEST_SUBROUTINE int hmac_sha3_test(void)
  4483. {
  4484. Hmac hmac;
  4485. byte hash[WC_SHA3_512_DIGEST_SIZE];
  4486. const char* key[4] =
  4487. {
  4488. "Jefe",
  4489. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  4490. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  4491. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"
  4492. "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
  4493. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4494. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4495. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4496. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4497. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4498. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4499. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4500. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4501. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4502. "\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08"
  4503. };
  4504. const char* input[4] =
  4505. {
  4506. "what do ya want for nothing?",
  4507. "Hi There",
  4508. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  4509. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  4510. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  4511. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
  4512. "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd",
  4513. "Big Key Input"
  4514. };
  4515. const int hashType[4] =
  4516. {
  4517. WC_SHA3_224, WC_SHA3_256, WC_SHA3_384, WC_SHA3_512
  4518. };
  4519. const int hashSz[4] =
  4520. {
  4521. WC_SHA3_224_DIGEST_SIZE, WC_SHA3_256_DIGEST_SIZE,
  4522. WC_SHA3_384_DIGEST_SIZE, WC_SHA3_512_DIGEST_SIZE
  4523. };
  4524. const char* output[16] =
  4525. {
  4526. /* key = jefe, input = what do ya want for nothing? */
  4527. /* HMAC-SHA3-224 */
  4528. "\x7f\xdb\x8d\xd8\x8b\xd2\xf6\x0d\x1b\x79\x86\x34\xad\x38\x68\x11"
  4529. "\xc2\xcf\xc8\x5b\xfa\xf5\xd5\x2b\xba\xce\x5e\x66",
  4530. /* HMAC-SHA3-256 */
  4531. "\xc7\xd4\x07\x2e\x78\x88\x77\xae\x35\x96\xbb\xb0\xda\x73\xb8\x87"
  4532. "\xc9\x17\x1f\x93\x09\x5b\x29\x4a\xe8\x57\xfb\xe2\x64\x5e\x1b\xa5",
  4533. /* HMAC-SHA3-384 */
  4534. "\xf1\x10\x1f\x8c\xbf\x97\x66\xfd\x67\x64\xd2\xed\x61\x90\x3f\x21"
  4535. "\xca\x9b\x18\xf5\x7c\xf3\xe1\xa2\x3c\xa1\x35\x08\xa9\x32\x43\xce"
  4536. "\x48\xc0\x45\xdc\x00\x7f\x26\xa2\x1b\x3f\x5e\x0e\x9d\xf4\xc2\x0a",
  4537. /* HMAC-SHA3-512 */
  4538. "\x5a\x4b\xfe\xab\x61\x66\x42\x7c\x7a\x36\x47\xb7\x47\x29\x2b\x83"
  4539. "\x84\x53\x7c\xdb\x89\xaf\xb3\xbf\x56\x65\xe4\xc5\xe7\x09\x35\x0b"
  4540. "\x28\x7b\xae\xc9\x21\xfd\x7c\xa0\xee\x7a\x0c\x31\xd0\x22\xa9\x5e"
  4541. "\x1f\xc9\x2b\xa9\xd7\x7d\xf8\x83\x96\x02\x75\xbe\xb4\xe6\x20\x24",
  4542. /* key = 0b..., input = Hi There */
  4543. /* HMAC-SHA3-224 */
  4544. "\x3b\x16\x54\x6b\xbc\x7b\xe2\x70\x6a\x03\x1d\xca\xfd\x56\x37\x3d"
  4545. "\x98\x84\x36\x76\x41\xd8\xc5\x9a\xf3\xc8\x60\xf7",
  4546. /* HMAC-SHA3-256 */
  4547. "\xba\x85\x19\x23\x10\xdf\xfa\x96\xe2\xa3\xa4\x0e\x69\x77\x43\x51"
  4548. "\x14\x0b\xb7\x18\x5e\x12\x02\xcd\xcc\x91\x75\x89\xf9\x5e\x16\xbb",
  4549. /* HMAC-SHA3-384 */
  4550. "\x68\xd2\xdc\xf7\xfd\x4d\xdd\x0a\x22\x40\xc8\xa4\x37\x30\x5f\x61"
  4551. "\xfb\x73\x34\xcf\xb5\xd0\x22\x6e\x1b\xc2\x7d\xc1\x0a\x2e\x72\x3a"
  4552. "\x20\xd3\x70\xb4\x77\x43\x13\x0e\x26\xac\x7e\x3d\x53\x28\x86\xbd",
  4553. /* HMAC-SHA3-512 */
  4554. "\xeb\x3f\xbd\x4b\x2e\xaa\xb8\xf5\xc5\x04\xbd\x3a\x41\x46\x5a\xac"
  4555. "\xec\x15\x77\x0a\x7c\xab\xac\x53\x1e\x48\x2f\x86\x0b\x5e\xc7\xba"
  4556. "\x47\xcc\xb2\xc6\xf2\xaf\xce\x8f\x88\xd2\x2b\x6d\xc6\x13\x80\xf2"
  4557. "\x3a\x66\x8f\xd3\x88\x8b\xb8\x05\x37\xc0\xa0\xb8\x64\x07\x68\x9e",
  4558. /* key = aa..., output = dd... */
  4559. /* HMAC-SHA3-224 */
  4560. "\x67\x6c\xfc\x7d\x16\x15\x36\x38\x78\x03\x90\x69\x2b\xe1\x42\xd2"
  4561. "\xdf\x7c\xe9\x24\xb9\x09\xc0\xc0\x8d\xbf\xdc\x1a",
  4562. /* HMAC-SHA3-256 */
  4563. "\x84\xec\x79\x12\x4a\x27\x10\x78\x65\xce\xdd\x8b\xd8\x2d\xa9\x96"
  4564. "\x5e\x5e\xd8\xc3\x7b\x0a\xc9\x80\x05\xa7\xf3\x9e\xd5\x8a\x42\x07",
  4565. /* HMAC-SHA3-384 */
  4566. "\x27\x5c\xd0\xe6\x61\xbb\x8b\x15\x1c\x64\xd2\x88\xf1\xf7\x82\xfb"
  4567. "\x91\xa8\xab\xd5\x68\x58\xd7\x2b\xab\xb2\xd4\x76\xf0\x45\x83\x73"
  4568. "\xb4\x1b\x6a\xb5\xbf\x17\x4b\xec\x42\x2e\x53\xfc\x31\x35\xac\x6e",
  4569. /* HMAC-SHA3-512 */
  4570. "\x30\x9e\x99\xf9\xec\x07\x5e\xc6\xc6\xd4\x75\xed\xa1\x18\x06\x87"
  4571. "\xfc\xf1\x53\x11\x95\x80\x2a\x99\xb5\x67\x74\x49\xa8\x62\x51\x82"
  4572. "\x85\x1c\xb3\x32\xaf\xb6\xa8\x9c\x41\x13\x25\xfb\xcb\xcd\x42\xaf"
  4573. "\xcb\x7b\x6e\x5a\xab\x7e\xa4\x2c\x66\x0f\x97\xfd\x85\x84\xbf\x03",
  4574. /* key = big key, input = Big Key Input */
  4575. /* HMAC-SHA3-224 */
  4576. "\x29\xe0\x5e\x46\xc4\xa4\x5e\x46\x74\xbf\xd7\x2d\x1a\xd8\x66\xdb"
  4577. "\x2d\x0d\x10\x4e\x2b\xfa\xad\x53\x7d\x15\x69\x8b",
  4578. /* HMAC-SHA3-256 */
  4579. "\xb5\x5b\x8d\x64\xb6\x9c\x21\xd0\xbf\x20\x5c\xa2\xf7\xb9\xb1\x4e"
  4580. "\x88\x21\x61\x2c\x66\xc3\x91\xae\x6c\x95\x16\x85\x83\xe6\xf4\x9b",
  4581. /* HMAC-SHA3-384 */
  4582. "\xaa\x91\xb3\xa6\x2f\x56\xa1\xbe\x8c\x3e\x74\x38\xdb\x58\xd9\xd3"
  4583. "\x34\xde\xa0\x60\x6d\x8d\x46\xe0\xec\xa9\xf6\x06\x35\x14\xe6\xed"
  4584. "\x83\xe6\x7c\x77\x24\x6c\x11\xb5\x90\x82\xb5\x75\xda\x7b\x83\x2d",
  4585. /* HMAC-SHA3-512 */
  4586. "\x1c\xc3\xa9\x24\x4a\x4a\x3f\xbd\xc7\x20\x00\x16\x9b\x79\x47\x03"
  4587. "\x78\x75\x2c\xb5\xf1\x2e\x62\x7c\xbe\xef\x4e\x8f\x0b\x11\x2b\x32"
  4588. "\xa0\xee\xc9\xd0\x4d\x64\x64\x0b\x37\xf4\xdd\x66\xf7\x8b\xb3\xad"
  4589. "\x52\x52\x6b\x65\x12\xde\x0d\x7c\xc0\x8b\x60\x01\x6c\x37\xd7\xa8"
  4590. };
  4591. int i = 0, iMax = sizeof(input) / sizeof(input[0]),
  4592. j, jMax = sizeof(hashType) / sizeof(hashType[0]),
  4593. ret;
  4594. #ifdef HAVE_FIPS
  4595. /* FIPS requires a minimum length for HMAC keys, and "Jefe" is too
  4596. * short. Skip it in FIPS builds. */
  4597. i = 1;
  4598. #endif
  4599. for (; i < iMax; i++) {
  4600. for (j = 0; j < jMax; j++) {
  4601. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  4602. return -4000;
  4603. ret = wc_HmacSetKey(&hmac, hashType[j], (byte*)key[i],
  4604. (word32)XSTRLEN(key[i]));
  4605. if (ret != 0)
  4606. return -4001;
  4607. ret = wc_HmacUpdate(&hmac, (byte*)input[i],
  4608. (word32)XSTRLEN(input[i]));
  4609. if (ret != 0)
  4610. return -4002;
  4611. ret = wc_HmacFinal(&hmac, hash);
  4612. if (ret != 0)
  4613. return -4003;
  4614. if (XMEMCMP(hash, output[(i*jMax) + j], hashSz[j]) != 0)
  4615. return -4004;
  4616. wc_HmacFree(&hmac);
  4617. if (i > 0)
  4618. continue;
  4619. #ifndef HAVE_FIPS
  4620. ret = wc_HmacSizeByType(hashType[j]);
  4621. if (ret != hashSz[j])
  4622. return -4005;
  4623. #endif
  4624. }
  4625. }
  4626. return 0;
  4627. }
  4628. #endif
  4629. #ifdef WC_RC2
  4630. typedef struct rc2TestVector {
  4631. const char* input;
  4632. const char* output;
  4633. const char* key; /* Key, variable up to 128 bytes */
  4634. const char* iv; /* IV, 8-bytes */
  4635. int inLen;
  4636. int outLen;
  4637. int keyLen;
  4638. int effectiveKeyBits; /* Up to 1024 bits supported */
  4639. } rc2TestVector;
  4640. static int rc2_ecb_test(void)
  4641. {
  4642. int ret = 0;
  4643. byte cipher[RC2_BLOCK_SIZE];
  4644. byte plain[RC2_BLOCK_SIZE];
  4645. rc2TestVector a, b, c, d, e, f, g, h;
  4646. rc2TestVector test_rc2[8];
  4647. int times = sizeof(test_rc2) / sizeof(rc2TestVector), i;
  4648. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4649. a.output = "\xeb\xb7\x73\xf9\x93\x27\x8e\xff";
  4650. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4651. a.inLen = RC2_BLOCK_SIZE;
  4652. a.outLen = RC2_BLOCK_SIZE;
  4653. a.keyLen = 8;
  4654. a.effectiveKeyBits = 63;
  4655. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4656. b.output = "\x27\x8b\x27\xe4\x2e\x2f\x0d\x49";
  4657. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4658. b.inLen = RC2_BLOCK_SIZE;
  4659. b.outLen = RC2_BLOCK_SIZE;
  4660. b.keyLen = 8;
  4661. b.effectiveKeyBits = 64;
  4662. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01";
  4663. c.output = "\x30\x64\x9e\xdf\x9b\xe7\xd2\xc2";
  4664. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  4665. c.inLen = RC2_BLOCK_SIZE;
  4666. c.outLen = RC2_BLOCK_SIZE;
  4667. c.keyLen = 8;
  4668. c.effectiveKeyBits = 64;
  4669. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4670. d.output = "\x61\xa8\xa2\x44\xad\xac\xcc\xf0";
  4671. d.key = "\x88";
  4672. d.inLen = RC2_BLOCK_SIZE;
  4673. d.outLen = RC2_BLOCK_SIZE;
  4674. d.keyLen = 1;
  4675. d.effectiveKeyBits = 64;
  4676. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4677. e.output = "\x6c\xcf\x43\x08\x97\x4c\x26\x7f";
  4678. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  4679. e.inLen = RC2_BLOCK_SIZE;
  4680. e.outLen = RC2_BLOCK_SIZE;
  4681. e.keyLen = 7;
  4682. e.effectiveKeyBits = 64;
  4683. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4684. f.output = "\x1a\x80\x7d\x27\x2b\xbe\x5d\xb1";
  4685. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4686. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4687. f.inLen = RC2_BLOCK_SIZE;
  4688. f.outLen = RC2_BLOCK_SIZE;
  4689. f.keyLen = 16;
  4690. f.effectiveKeyBits = 64;
  4691. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4692. g.output = "\x22\x69\x55\x2a\xb0\xf8\x5c\xa6";
  4693. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4694. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4695. g.inLen = RC2_BLOCK_SIZE;
  4696. g.outLen = RC2_BLOCK_SIZE;
  4697. g.keyLen = 16;
  4698. g.effectiveKeyBits = 128;
  4699. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4700. h.output = "\x5b\x78\xd3\xa4\x3d\xff\xf1\xf1";
  4701. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4702. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  4703. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  4704. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  4705. "\x1e";
  4706. h.inLen = RC2_BLOCK_SIZE;
  4707. h.outLen = RC2_BLOCK_SIZE;
  4708. h.keyLen = 33;
  4709. h.effectiveKeyBits = 129;
  4710. a.iv = b.iv = c.iv = d.iv = e.iv = f.iv = g.iv = h.iv = NULL;
  4711. test_rc2[0] = a;
  4712. test_rc2[1] = b;
  4713. test_rc2[2] = c;
  4714. test_rc2[3] = d;
  4715. test_rc2[4] = e;
  4716. test_rc2[5] = f;
  4717. test_rc2[6] = g;
  4718. test_rc2[7] = h;
  4719. for (i = 0; i < times; ++i) {
  4720. Rc2 enc;
  4721. XMEMSET(cipher, 0, RC2_BLOCK_SIZE);
  4722. XMEMSET(plain, 0, RC2_BLOCK_SIZE);
  4723. ret = wc_Rc2SetKey(&enc, (byte*)test_rc2[i].key, test_rc2[i].keyLen,
  4724. NULL, test_rc2[i].effectiveKeyBits);
  4725. if (ret != 0) {
  4726. return -4100;
  4727. }
  4728. /* ECB encrypt */
  4729. ret = wc_Rc2EcbEncrypt(&enc, cipher, (byte*)test_rc2[i].input,
  4730. (word32)test_rc2[i].outLen);
  4731. if (ret != 0) {
  4732. return -4101;
  4733. }
  4734. if (XMEMCMP(cipher, test_rc2[i].output, test_rc2[i].outLen)) {
  4735. return -4102;
  4736. }
  4737. /* ECB decrypt */
  4738. ret = wc_Rc2EcbDecrypt(&enc, plain, cipher, RC2_BLOCK_SIZE);
  4739. if (ret != 0) {
  4740. return -4103;
  4741. }
  4742. if (XMEMCMP(plain, test_rc2[i].input, RC2_BLOCK_SIZE)) {
  4743. return -4104;
  4744. }
  4745. }
  4746. return 0;
  4747. }
  4748. static int rc2_cbc_test(void)
  4749. {
  4750. int ret = 0;
  4751. byte cipher[128];
  4752. byte plain[128];
  4753. rc2TestVector a, b, c, d, e, f, g, h, i;
  4754. rc2TestVector test_rc2[9];
  4755. int times = sizeof(test_rc2) / sizeof(rc2TestVector), j;
  4756. /* key length = 7, effective key bits = 63 */
  4757. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4758. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4759. a.output = "\xEB\xB7\x73\xF9\x93\x27\x8E\xFF"
  4760. "\xF0\x51\x77\x8B\x65\xDB\x13\x57";
  4761. a.key = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4762. a.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4763. a.inLen = RC2_BLOCK_SIZE*2;
  4764. a.outLen = RC2_BLOCK_SIZE*2;
  4765. a.keyLen = 8;
  4766. a.effectiveKeyBits = 63;
  4767. /* key length = 8, effective key bits = 64, all 0xFF */
  4768. b.input = "\xff\xff\xff\xff\xff\xff\xff\xff"
  4769. "\xff\xff\xff\xff\xff\xff\xff\xff";
  4770. b.output = "\xA3\xA1\x12\x65\x4F\x81\xC5\xCD"
  4771. "\xB6\x94\x3E\xEA\x3E\x8B\x9D\x1F";
  4772. b.key = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4773. b.iv = "\xff\xff\xff\xff\xff\xff\xff\xff";
  4774. b.inLen = RC2_BLOCK_SIZE*2;
  4775. b.outLen = RC2_BLOCK_SIZE*2;
  4776. b.keyLen = 8;
  4777. b.effectiveKeyBits = 64;
  4778. /* key length = 8, effective key bits = 64 */
  4779. c.input = "\x10\x00\x00\x00\x00\x00\x00\x01"
  4780. "\x10\x00\x00\x00\x00\x00\x00\x01";
  4781. c.output = "\xB5\x70\x14\xA2\x5F\x40\xE3\x6D"
  4782. "\x81\x99\x8D\xE0\xB5\xD5\x3A\x05";
  4783. c.key = "\x30\x00\x00\x00\x00\x00\x00\x00";
  4784. c.iv = "\x30\x00\x00\x00\x00\x00\x00\x00";
  4785. c.inLen = RC2_BLOCK_SIZE*2;
  4786. c.outLen = RC2_BLOCK_SIZE*2;
  4787. c.keyLen = 8;
  4788. c.effectiveKeyBits = 64;
  4789. /* key length = 1, effective key bits = 64 */
  4790. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4791. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4792. d.output = "\x61\xA8\xA2\x44\xAD\xAC\xCC\xF0"
  4793. "\x6D\x19\xE8\xF1\xFC\xE7\x38\x87";
  4794. d.key = "\x88";
  4795. d.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4796. d.inLen = RC2_BLOCK_SIZE*2;
  4797. d.outLen = RC2_BLOCK_SIZE*2;
  4798. d.keyLen = 1;
  4799. d.effectiveKeyBits = 64;
  4800. /* key length = 7, effective key bits = 64 */
  4801. e.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4802. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4803. e.output = "\x6C\xCF\x43\x08\x97\x4C\x26\x7F"
  4804. "\xCC\x3C\x53\x57\x7C\xA1\xA4\x4B";
  4805. e.key = "\x88\xbc\xa9\x0e\x90\x87\x5a";
  4806. e.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4807. e.inLen = RC2_BLOCK_SIZE*2;
  4808. e.outLen = RC2_BLOCK_SIZE*2;
  4809. e.keyLen = 7;
  4810. e.effectiveKeyBits = 64;
  4811. /* key length = 16, effective key bits = 64 */
  4812. f.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4813. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4814. f.output = "\x1A\x80\x7D\x27\x2B\xBE\x5D\xB1"
  4815. "\x64\xEF\xE1\xC3\xB8\xAD\xFB\xBA";
  4816. f.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4817. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4818. f.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4819. f.inLen = RC2_BLOCK_SIZE*2;
  4820. f.outLen = RC2_BLOCK_SIZE*2;
  4821. f.keyLen = 16;
  4822. f.effectiveKeyBits = 64;
  4823. /* key length = 16, effective bits = 128 */
  4824. g.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4825. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4826. g.output = "\x22\x69\x55\x2A\xB0\xF8\x5C\xA6"
  4827. "\x53\x6E\xFD\x2D\x89\xE1\x2A\x73";
  4828. g.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4829. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2";
  4830. g.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4831. g.inLen = RC2_BLOCK_SIZE*2;
  4832. g.outLen = RC2_BLOCK_SIZE*2;
  4833. g.keyLen = 16;
  4834. g.effectiveKeyBits = 128;
  4835. /* key length = 33, effective bits = 129 */
  4836. h.input = "\x00\x00\x00\x00\x00\x00\x00\x00"
  4837. "\x00\x00\x00\x00\x00\x00\x00\x00";
  4838. h.output = "\x5B\x78\xD3\xA4\x3D\xFF\xF1\xF1"
  4839. "\x45\x30\xA8\xD5\xC7\x7C\x46\x19";
  4840. h.key = "\x88\xbc\xa9\x0e\x90\x87\x5a\x7f"
  4841. "\x0f\x79\xc3\x84\x62\x7b\xaf\xb2"
  4842. "\x16\xf8\x0a\x6f\x85\x92\x05\x84"
  4843. "\xc4\x2f\xce\xb0\xbe\x25\x5d\xaf"
  4844. "\x1e";
  4845. h.iv = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4846. h.inLen = RC2_BLOCK_SIZE*2;
  4847. h.outLen = RC2_BLOCK_SIZE*2;
  4848. h.keyLen = 33;
  4849. h.effectiveKeyBits = 129;
  4850. /* key length = 10, effective bits = 40 */
  4851. i.input = "\x11\x22\x33\x44\x55\x66\x77\x88"
  4852. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00"
  4853. "\x11\x22\x33\x44\x55\x66\x77\x88"
  4854. "\x99\xAA\xBB\xCC\xDD\xEE\xFF\x00";
  4855. i.output = "\x71\x2D\x11\x99\xC9\xA0\x78\x4F"
  4856. "\xCD\xF1\x1E\x3D\xFD\x21\x7E\xDB"
  4857. "\xB2\x6E\x0D\xA4\x72\xBC\x31\x51"
  4858. "\x48\xEF\x4E\x68\x3B\xDC\xCD\x7D";
  4859. i.key = "\x26\x1E\x57\x8E\xC9\x62\xBF\xB8"
  4860. "\x3E\x96";
  4861. i.iv = "\x01\x02\x03\x04\x05\x06\x07\x08";
  4862. i.inLen = RC2_BLOCK_SIZE*4;
  4863. i.outLen = RC2_BLOCK_SIZE*4;
  4864. i.keyLen = 10;
  4865. i.effectiveKeyBits = 40;
  4866. test_rc2[0] = a;
  4867. test_rc2[1] = b;
  4868. test_rc2[2] = c;
  4869. test_rc2[3] = d;
  4870. test_rc2[4] = e;
  4871. test_rc2[5] = f;
  4872. test_rc2[6] = g;
  4873. test_rc2[7] = h;
  4874. test_rc2[8] = i;
  4875. for (j = 0; j < times; ++j) {
  4876. Rc2 rc2;
  4877. XMEMSET(cipher, 0, sizeof(cipher));
  4878. XMEMSET(plain, 0, sizeof(plain));
  4879. ret = wc_Rc2SetKey(&rc2, (byte*)test_rc2[j].key, test_rc2[j].keyLen,
  4880. (byte*)test_rc2[j].iv, test_rc2[j].effectiveKeyBits);
  4881. if (ret != 0) {
  4882. return -4200;
  4883. }
  4884. ret = wc_Rc2CbcEncrypt(&rc2, cipher, (byte*)test_rc2[j].input,
  4885. test_rc2[j].inLen);
  4886. if (ret != 0) {
  4887. return -4201;
  4888. }
  4889. if (XMEMCMP(cipher, (byte*)test_rc2[j].output, test_rc2[j].outLen)) {
  4890. return -4202;
  4891. }
  4892. /* reset IV for decrypt, since overriden by encrypt operation */
  4893. ret = wc_Rc2SetIV(&rc2, (byte*)test_rc2[j].iv);
  4894. if (ret != 0) {
  4895. return -4203;
  4896. }
  4897. ret = wc_Rc2CbcDecrypt(&rc2, plain, cipher, test_rc2[j].outLen);
  4898. if (ret != 0) {
  4899. return -4204;
  4900. }
  4901. if (XMEMCMP(plain, (byte*)test_rc2[j].input, test_rc2[j].inLen)) {
  4902. return -4205;
  4903. }
  4904. }
  4905. return 0;
  4906. }
  4907. WOLFSSL_TEST_SUBROUTINE int rc2_test(void)
  4908. {
  4909. int ret = 0;
  4910. ret = rc2_ecb_test();
  4911. if (ret != 0) {
  4912. return ret;
  4913. }
  4914. return rc2_cbc_test();
  4915. }
  4916. #endif
  4917. #ifndef NO_RC4
  4918. WOLFSSL_TEST_SUBROUTINE int arc4_test(void)
  4919. {
  4920. byte cipher[16];
  4921. byte plain[16];
  4922. const char* keys[] =
  4923. {
  4924. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  4925. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  4926. "\x00\x00\x00\x00\x00\x00\x00\x00",
  4927. "\xef\x01\x23\x45"
  4928. };
  4929. testVector a, b, c, d;
  4930. testVector test_arc4[4];
  4931. int times = sizeof(test_arc4) / sizeof(testVector), i;
  4932. a.input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  4933. a.output = "\x75\xb7\x87\x80\x99\xe0\xc5\x96";
  4934. a.inLen = 8;
  4935. a.outLen = 8;
  4936. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4937. b.output = "\x74\x94\xc2\xe7\x10\x4b\x08\x79";
  4938. b.inLen = 8;
  4939. b.outLen = 8;
  4940. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  4941. c.output = "\xde\x18\x89\x41\xa3\x37\x5d\x3a";
  4942. c.inLen = 8;
  4943. c.outLen = 8;
  4944. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  4945. d.output = "\xd6\xa1\x41\xa7\xec\x3c\x38\xdf\xbd\x61";
  4946. d.inLen = 10;
  4947. d.outLen = 10;
  4948. test_arc4[0] = a;
  4949. test_arc4[1] = b;
  4950. test_arc4[2] = c;
  4951. test_arc4[3] = d;
  4952. for (i = 0; i < times; ++i) {
  4953. Arc4 enc;
  4954. Arc4 dec;
  4955. int keylen = 8; /* XSTRLEN with key 0x00 not good */
  4956. if (i == 3)
  4957. keylen = 4;
  4958. if (wc_Arc4Init(&enc, HEAP_HINT, devId) != 0)
  4959. return -4400;
  4960. if (wc_Arc4Init(&dec, HEAP_HINT, devId) != 0)
  4961. return -4401;
  4962. wc_Arc4SetKey(&enc, (byte*)keys[i], keylen);
  4963. wc_Arc4SetKey(&dec, (byte*)keys[i], keylen);
  4964. wc_Arc4Process(&enc, cipher, (byte*)test_arc4[i].input,
  4965. (word32)test_arc4[i].outLen);
  4966. wc_Arc4Process(&dec, plain, cipher, (word32)test_arc4[i].outLen);
  4967. if (XMEMCMP(plain, test_arc4[i].input, test_arc4[i].outLen))
  4968. return -4402 - i;
  4969. if (XMEMCMP(cipher, test_arc4[i].output, test_arc4[i].outLen))
  4970. return -4412 - i;
  4971. wc_Arc4Free(&enc);
  4972. wc_Arc4Free(&dec);
  4973. }
  4974. return 0;
  4975. }
  4976. #endif
  4977. #ifdef HAVE_CHACHA
  4978. WOLFSSL_TEST_SUBROUTINE int chacha_test(void)
  4979. {
  4980. ChaCha enc;
  4981. ChaCha dec;
  4982. byte cipher[128];
  4983. byte plain[128];
  4984. byte sliver[64];
  4985. byte input[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  4986. word32 keySz = 32;
  4987. int ret = 0;
  4988. int i;
  4989. int times = 4;
  4990. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  4991. {
  4992. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4993. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4994. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  4995. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  4996. };
  4997. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  4998. {
  4999. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5000. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5001. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5002. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  5003. };
  5004. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  5005. {
  5006. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5007. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5008. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5009. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5010. };
  5011. /* 128 bit key */
  5012. WOLFSSL_SMALL_STACK_STATIC const byte key4[] =
  5013. {
  5014. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5015. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5016. };
  5017. const byte* keys[] = {key1, key2, key3, key4};
  5018. WOLFSSL_SMALL_STACK_STATIC const byte ivs1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  5019. WOLFSSL_SMALL_STACK_STATIC const byte ivs2[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  5020. WOLFSSL_SMALL_STACK_STATIC const byte ivs3[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00};
  5021. WOLFSSL_SMALL_STACK_STATIC const byte ivs4[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  5022. const byte* ivs[] = {ivs1, ivs2, ivs3, ivs4};
  5023. #ifndef BENCH_EMBEDDED
  5024. WOLFSSL_SMALL_STACK_STATIC const byte cipher_big_result[] = {
  5025. 0x06, 0xa6, 0x5d, 0x31, 0x21, 0x6c, 0xdb, 0x37, 0x48, 0x7c, 0x01, 0x9d,
  5026. 0x72, 0xdf, 0x0a, 0x5b, 0x64, 0x74, 0x20, 0xba, 0x9e, 0xe0, 0x26, 0x7a,
  5027. 0xbf, 0xdf, 0x83, 0x34, 0x3b, 0x4f, 0x94, 0x3f, 0x37, 0x89, 0xaf, 0x00,
  5028. 0xdf, 0x0f, 0x2e, 0x75, 0x16, 0x41, 0xf6, 0x7a, 0x86, 0x94, 0x9d, 0x32,
  5029. 0x56, 0xf0, 0x79, 0x71, 0x68, 0x6f, 0xa6, 0x6b, 0xc6, 0x59, 0x49, 0xf6,
  5030. 0x10, 0x34, 0x03, 0x03, 0x16, 0x53, 0x9a, 0x98, 0x2a, 0x46, 0xde, 0x17,
  5031. 0x06, 0x65, 0x70, 0xca, 0x0a, 0x1f, 0xab, 0x80, 0x26, 0x96, 0x3f, 0x3e,
  5032. 0x7a, 0x3c, 0xa8, 0x87, 0xbb, 0x65, 0xdd, 0x5e, 0x07, 0x7b, 0x34, 0xe0,
  5033. 0x56, 0xda, 0x32, 0x13, 0x30, 0xc9, 0x0c, 0xd7, 0xba, 0xe4, 0x1f, 0xa6,
  5034. 0x91, 0x4f, 0x72, 0x9f, 0xd9, 0x5c, 0x62, 0x7d, 0xa6, 0xc2, 0xbc, 0x87,
  5035. 0xae, 0x64, 0x11, 0x94, 0x3b, 0xbc, 0x6c, 0x23, 0xbd, 0x7d, 0x00, 0xb4,
  5036. 0x99, 0xf2, 0x68, 0xb5, 0x59, 0x70, 0x93, 0xad, 0x69, 0xd0, 0xb1, 0x28,
  5037. 0x70, 0x92, 0xeb, 0xec, 0x39, 0x80, 0x82, 0xde, 0x44, 0xe2, 0x8a, 0x26,
  5038. 0xb3, 0xe9, 0x45, 0xcf, 0x83, 0x76, 0x9f, 0x6a, 0xa0, 0x46, 0x4a, 0x3d,
  5039. 0x26, 0x56, 0xaf, 0x49, 0x41, 0x26, 0x1b, 0x6a, 0x41, 0x37, 0x65, 0x91,
  5040. 0x72, 0xc4, 0xe7, 0x3c, 0x17, 0x31, 0xae, 0x2e, 0x2b, 0x31, 0x45, 0xe4,
  5041. 0x93, 0xd3, 0x10, 0xaa, 0xc5, 0x62, 0xd5, 0x11, 0x4b, 0x57, 0x1d, 0xad,
  5042. 0x48, 0x06, 0xd0, 0x0d, 0x98, 0xa5, 0xc6, 0x5b, 0xd0, 0x9e, 0x22, 0xc0,
  5043. 0x00, 0x32, 0x5a, 0xf5, 0x1c, 0x89, 0x6d, 0x54, 0x97, 0x55, 0x6b, 0x46,
  5044. 0xc5, 0xc7, 0xc4, 0x48, 0x9c, 0xbf, 0x47, 0xdc, 0x03, 0xc4, 0x1b, 0xcb,
  5045. 0x65, 0xa6, 0x91, 0x9d, 0x6d, 0xf1, 0xb0, 0x7a, 0x4d, 0x3b, 0x03, 0x95,
  5046. 0xf4, 0x8b, 0x0b, 0xae, 0x39, 0xff, 0x3f, 0xf6, 0xc0, 0x14, 0x18, 0x8a,
  5047. 0xe5, 0x19, 0xbd, 0xc1, 0xb4, 0x05, 0x4e, 0x29, 0x2f, 0x0b, 0x33, 0x76,
  5048. 0x28, 0x16, 0xa4, 0xa6, 0x93, 0x04, 0xb5, 0x55, 0x6b, 0x89, 0x3d, 0xa5,
  5049. 0x0f, 0xd3, 0xad, 0xfa, 0xd9, 0xfd, 0x05, 0x5d, 0x48, 0x94, 0x25, 0x5a,
  5050. 0x2c, 0x9a, 0x94, 0x80, 0xb0, 0xe7, 0xcb, 0x4d, 0x77, 0xbf, 0xca, 0xd8,
  5051. 0x55, 0x48, 0xbd, 0x66, 0xb1, 0x85, 0x81, 0xb1, 0x37, 0x79, 0xab, 0x52,
  5052. 0x08, 0x14, 0x12, 0xac, 0xcd, 0x45, 0x4d, 0x53, 0x6b, 0xca, 0x96, 0xc7,
  5053. 0x3b, 0x2f, 0x73, 0xb1, 0x5a, 0x23, 0xbd, 0x65, 0xd5, 0xea, 0x17, 0xb3,
  5054. 0xdc, 0xa1, 0x17, 0x1b, 0x2d, 0xb3, 0x9c, 0xd0, 0xdb, 0x41, 0x77, 0xef,
  5055. 0x93, 0x20, 0x52, 0x3e, 0x9d, 0xf5, 0xbf, 0x33, 0xf7, 0x52, 0xc1, 0x90,
  5056. 0xa0, 0x15, 0x17, 0xce, 0xf7, 0xf7, 0xd0, 0x3a, 0x3b, 0xd1, 0x72, 0x56,
  5057. 0x31, 0x81, 0xae, 0x60, 0xab, 0x40, 0xc1, 0xd1, 0x28, 0x77, 0x53, 0xac,
  5058. 0x9f, 0x11, 0x0a, 0x88, 0x36, 0x4b, 0xda, 0x57, 0xa7, 0x28, 0x5c, 0x85,
  5059. 0xd3, 0x85, 0x9b, 0x79, 0xad, 0x05, 0x1c, 0x37, 0x14, 0x5e, 0x0d, 0xd0,
  5060. 0x23, 0x03, 0x42, 0x1d, 0x48, 0x5d, 0xc5, 0x3c, 0x5a, 0x08, 0xa9, 0x0d,
  5061. 0x6e, 0x82, 0x7c, 0x2e, 0x3c, 0x41, 0xcc, 0x96, 0x8e, 0xad, 0xee, 0x2a,
  5062. 0x61, 0x0b, 0x16, 0x0f, 0xa9, 0x24, 0x40, 0x85, 0xbc, 0x9f, 0x28, 0x8d,
  5063. 0xe6, 0x68, 0x4d, 0x8f, 0x30, 0x48, 0xd9, 0x73, 0x73, 0x6c, 0x9a, 0x7f,
  5064. 0x67, 0xf7, 0xde, 0x4c, 0x0a, 0x8b, 0xe4, 0xb3, 0x08, 0x2a, 0x52, 0xda,
  5065. 0x54, 0xee, 0xcd, 0xb5, 0x62, 0x4a, 0x26, 0x20, 0xfb, 0x40, 0xbb, 0x39,
  5066. 0x3a, 0x0f, 0x09, 0xe8, 0x00, 0xd1, 0x24, 0x97, 0x60, 0xe9, 0x83, 0x83,
  5067. 0xfe, 0x9f, 0x9c, 0x15, 0xcf, 0x69, 0x03, 0x9f, 0x03, 0xe1, 0xe8, 0x6e,
  5068. 0xbd, 0x87, 0x58, 0x68, 0xee, 0xec, 0xd8, 0x29, 0x46, 0x23, 0x49, 0x92,
  5069. 0x72, 0x95, 0x5b, 0x49, 0xca, 0xe0, 0x45, 0x59, 0xb2, 0xca, 0xf4, 0xfc,
  5070. 0xb7, 0x59, 0x37, 0x49, 0x28, 0xbc, 0xf3, 0xd7, 0x61, 0xbc, 0x4b, 0xf3,
  5071. 0xa9, 0x4b, 0x2f, 0x05, 0xa8, 0x01, 0xa5, 0xdc, 0x00, 0x6e, 0x01, 0xb6,
  5072. 0x45, 0x3c, 0xd5, 0x49, 0x7d, 0x5c, 0x25, 0xe8, 0x31, 0x87, 0xb2, 0xb9,
  5073. 0xbf, 0xb3, 0x01, 0x62, 0x0c, 0xd0, 0x48, 0x77, 0xa2, 0x34, 0x0f, 0x16,
  5074. 0x22, 0x28, 0xee, 0x54, 0x08, 0x93, 0x3b, 0xe4, 0xde, 0x7e, 0x63, 0xf7,
  5075. 0x97, 0x16, 0x5d, 0x71, 0x58, 0xc2, 0x2e, 0xf2, 0x36, 0xa6, 0x12, 0x65,
  5076. 0x94, 0x17, 0xac, 0x66, 0x23, 0x7e, 0xc6, 0x72, 0x79, 0x24, 0xce, 0x8f,
  5077. 0x55, 0x19, 0x97, 0x44, 0xfc, 0x55, 0xec, 0x85, 0x26, 0x27, 0xdb, 0x38,
  5078. 0xb1, 0x42, 0x0a, 0xdd, 0x05, 0x99, 0x28, 0xeb, 0x03, 0x6c, 0x9a, 0xe9,
  5079. 0x17, 0xf6, 0x2c, 0xb0, 0xfe, 0xe7, 0xa4, 0xa7, 0x31, 0xda, 0x4d, 0xb0,
  5080. 0x29, 0xdb, 0xdd, 0x8d, 0x12, 0x13, 0x9c, 0xb4, 0xcc, 0x83, 0x97, 0xfb,
  5081. 0x1a, 0xdc, 0x08, 0xd6, 0x30, 0x62, 0xe8, 0xeb, 0x8b, 0x61, 0xcb, 0x1d,
  5082. 0x06, 0xe3, 0xa5, 0x4d, 0x35, 0xdb, 0x59, 0xa8, 0x2d, 0x87, 0x27, 0x44,
  5083. 0x6f, 0xc0, 0x38, 0x97, 0xe4, 0x85, 0x00, 0x02, 0x09, 0xf6, 0x69, 0x3a,
  5084. 0xcf, 0x08, 0x1b, 0x21, 0xbb, 0x79, 0xb1, 0xa1, 0x34, 0x09, 0xe0, 0x80,
  5085. 0xca, 0xb0, 0x78, 0x8a, 0x11, 0x97, 0xd4, 0x07, 0xbe, 0x1b, 0x6a, 0x5d,
  5086. 0xdb, 0xd6, 0x1f, 0x76, 0x6b, 0x16, 0xf0, 0x58, 0x84, 0x5f, 0x59, 0xce,
  5087. 0x62, 0x34, 0xc3, 0xdf, 0x94, 0xb8, 0x2f, 0x84, 0x68, 0xf0, 0xb8, 0x51,
  5088. 0xd9, 0x6d, 0x8e, 0x4a, 0x1d, 0xe6, 0x5c, 0xd8, 0x86, 0x25, 0xe3, 0x24,
  5089. 0xfd, 0x21, 0x61, 0x13, 0x48, 0x3e, 0xf6, 0x7d, 0xa6, 0x71, 0x9b, 0xd2,
  5090. 0x6e, 0xe6, 0xd2, 0x08, 0x94, 0x62, 0x6c, 0x98, 0xfe, 0x2f, 0x9c, 0x88,
  5091. 0x7e, 0x78, 0x15, 0x02, 0x00, 0xf0, 0xba, 0x24, 0x91, 0xf2, 0xdc, 0x47,
  5092. 0x51, 0x4d, 0x15, 0x5e, 0x91, 0x5f, 0x57, 0x5b, 0x1d, 0x35, 0x24, 0x45,
  5093. 0x75, 0x9b, 0x88, 0x75, 0xf1, 0x2f, 0x85, 0xe7, 0x89, 0xd1, 0x01, 0xb4,
  5094. 0xc8, 0x18, 0xb7, 0x97, 0xef, 0x4b, 0x90, 0xf4, 0xbf, 0x10, 0x27, 0x3c,
  5095. 0x60, 0xff, 0xc4, 0x94, 0x20, 0x2f, 0x93, 0x4b, 0x4d, 0xe3, 0x80, 0xf7,
  5096. 0x2c, 0x71, 0xd9, 0xe3, 0x68, 0xb4, 0x77, 0x2b, 0xc7, 0x0d, 0x39, 0x92,
  5097. 0xef, 0x91, 0x0d, 0xb2, 0x11, 0x50, 0x0e, 0xe8, 0xad, 0x3b, 0xf6, 0xb5,
  5098. 0xc6, 0x14, 0x4d, 0x33, 0x53, 0xa7, 0x60, 0x15, 0xc7, 0x27, 0x51, 0xdc,
  5099. 0x54, 0x29, 0xa7, 0x0d, 0x6a, 0x7b, 0x72, 0x13, 0xad, 0x7d, 0x41, 0x19,
  5100. 0x4e, 0x42, 0x49, 0xcc, 0x42, 0xe4, 0xbd, 0x99, 0x13, 0xd9, 0x7f, 0xf3,
  5101. 0x38, 0xa4, 0xb6, 0x33, 0xed, 0x07, 0x48, 0x7e, 0x8e, 0x82, 0xfe, 0x3a,
  5102. 0x9d, 0x75, 0x93, 0xba, 0x25, 0x4e, 0x37, 0x3c, 0x0c, 0xd5, 0x69, 0xa9,
  5103. 0x2d, 0x9e, 0xfd, 0xe8, 0xbb, 0xf5, 0x0c, 0xe2, 0x86, 0xb9, 0x5e, 0x6f,
  5104. 0x28, 0xe4, 0x19, 0xb3, 0x0b, 0xa4, 0x86, 0xd7, 0x24, 0xd0, 0xb8, 0x89,
  5105. 0x7b, 0x76, 0xec, 0x05, 0x10, 0x5b, 0x68, 0xe9, 0x58, 0x66, 0xa3, 0xc5,
  5106. 0xb6, 0x63, 0x20, 0x0e, 0x0e, 0xea, 0x3d, 0x61, 0x5e, 0xda, 0x3d, 0x3c,
  5107. 0xf9, 0xfd, 0xed, 0xa9, 0xdb, 0x52, 0x94, 0x8a, 0x00, 0xca, 0x3c, 0x8d,
  5108. 0x66, 0x8f, 0xb0, 0xf0, 0x5a, 0xca, 0x3f, 0x63, 0x71, 0xbf, 0xca, 0x99,
  5109. 0x37, 0x9b, 0x75, 0x97, 0x89, 0x10, 0x6e, 0xcf, 0xf2, 0xf5, 0xe3, 0xd5,
  5110. 0x45, 0x9b, 0xad, 0x10, 0x71, 0x6c, 0x5f, 0x6f, 0x7f, 0x22, 0x77, 0x18,
  5111. 0x2f, 0xf9, 0x99, 0xc5, 0x69, 0x58, 0x03, 0x12, 0x86, 0x82, 0x3e, 0xbf,
  5112. 0xc2, 0x12, 0x35, 0x43, 0xa3, 0xd9, 0x18, 0x4f, 0x41, 0x11, 0x6b, 0xf3,
  5113. 0x67, 0xaf, 0x3d, 0x78, 0xe4, 0x22, 0x2d, 0xb3, 0x48, 0x43, 0x31, 0x1d,
  5114. 0xef, 0xa8, 0xba, 0x49, 0x8e, 0xa9, 0xa7, 0xb6, 0x18, 0x77, 0x84, 0xca,
  5115. 0xbd, 0xa2, 0x02, 0x1b, 0x6a, 0xf8, 0x5f, 0xda, 0xff, 0xcf, 0x01, 0x6a,
  5116. 0x86, 0x69, 0xa9, 0xe9, 0xcb, 0x60, 0x1e, 0x15, 0xdc, 0x8f, 0x5d, 0x39,
  5117. 0xb5, 0xce, 0x55, 0x5f, 0x47, 0x97, 0xb1, 0x19, 0x6e, 0x21, 0xd6, 0x13,
  5118. 0x39, 0xb2, 0x24, 0xe0, 0x62, 0x82, 0x9f, 0xed, 0x12, 0x81, 0xed, 0xee,
  5119. 0xab, 0xd0, 0x2f, 0x19, 0x89, 0x3f, 0x57, 0x2e, 0xc2, 0xe2, 0x67, 0xe8,
  5120. 0xae, 0x03, 0x56, 0xba, 0xd4, 0xd0, 0xa4, 0x89, 0x03, 0x06, 0x5b, 0xcc,
  5121. 0xf2, 0x22, 0xb8, 0x0e, 0x76, 0x79, 0x4a, 0x42, 0x1d, 0x37, 0x51, 0x5a,
  5122. 0xaa, 0x46, 0x6c, 0x2a, 0xdd, 0x66, 0xfe, 0xc6, 0x68, 0xc3, 0x38, 0xa2,
  5123. 0xae, 0x5b, 0x98, 0x24, 0x5d, 0x43, 0x05, 0x82, 0x38, 0x12, 0xd3, 0xd1,
  5124. 0x75, 0x2d, 0x4f, 0x61, 0xbd, 0xb9, 0x10, 0x87, 0x44, 0x2a, 0x78, 0x07,
  5125. 0xff, 0xf4, 0x0f, 0xa1, 0xf3, 0x68, 0x9f, 0xbe, 0xae, 0xa2, 0x91, 0xf0,
  5126. 0xc7, 0x55, 0x7a, 0x52, 0xd5, 0xa3, 0x8d, 0x6f, 0xe4, 0x90, 0x5c, 0xf3,
  5127. 0x5f, 0xce, 0x3d, 0x23, 0xf9, 0x8e, 0xae, 0x14, 0xfb, 0x82, 0x9a, 0xa3,
  5128. 0x04, 0x5f, 0xbf, 0xad, 0x3e, 0xf2, 0x97, 0x0a, 0x60, 0x40, 0x70, 0x19,
  5129. 0x72, 0xad, 0x66, 0xfb, 0x78, 0x1b, 0x84, 0x6c, 0x98, 0xbc, 0x8c, 0xf8,
  5130. 0x4f, 0xcb, 0xb5, 0xf6, 0xaf, 0x7a, 0xb7, 0x93, 0xef, 0x67, 0x48, 0x02,
  5131. 0x2c, 0xcb, 0xe6, 0x77, 0x0f, 0x7b, 0xc1, 0xee, 0xc5, 0xb6, 0x2d, 0x7e,
  5132. 0x62, 0xa0, 0xc0, 0xa7, 0xa5, 0x80, 0x31, 0x92, 0x50, 0xa1, 0x28, 0x22,
  5133. 0x95, 0x03, 0x17, 0xd1, 0x0f, 0xf6, 0x08, 0xe5, 0xec
  5134. };
  5135. #define CHACHA_BIG_TEST_SIZE 1305
  5136. #if !defined(WOLFSSL_SMALL_STACK) || defined(WOLFSSL_NO_MALLOC)
  5137. byte cipher_big[CHACHA_BIG_TEST_SIZE] = {0};
  5138. byte plain_big[CHACHA_BIG_TEST_SIZE] = {0};
  5139. byte input_big[CHACHA_BIG_TEST_SIZE] = {0};
  5140. #else
  5141. byte* cipher_big;
  5142. byte* plain_big;
  5143. byte* input_big;
  5144. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  5145. int block_size;
  5146. #endif /* BENCH_EMBEDDED */
  5147. byte a[] = {0x76,0xb8,0xe0,0xad,0xa0,0xf1,0x3d,0x90};
  5148. byte b[] = {0x45,0x40,0xf0,0x5a,0x9f,0x1f,0xb2,0x96};
  5149. byte c[] = {0xde,0x9c,0xba,0x7b,0xf3,0xd6,0x9e,0xf5};
  5150. byte d[] = {0x89,0x67,0x09,0x52,0x60,0x83,0x64,0xfd};
  5151. byte* test_chacha[4];
  5152. test_chacha[0] = a;
  5153. test_chacha[1] = b;
  5154. test_chacha[2] = c;
  5155. test_chacha[3] = d;
  5156. #ifndef BENCH_EMBEDDED
  5157. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  5158. cipher_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  5159. DYNAMIC_TYPE_TMP_BUFFER);
  5160. if (cipher_big == NULL) {
  5161. return MEMORY_E;
  5162. }
  5163. plain_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  5164. DYNAMIC_TYPE_TMP_BUFFER);
  5165. if (plain_big == NULL) {
  5166. return MEMORY_E;
  5167. }
  5168. input_big = (byte*)XMALLOC(CHACHA_BIG_TEST_SIZE, HEAP_HINT,
  5169. DYNAMIC_TYPE_TMP_BUFFER);
  5170. if (input_big == NULL) {
  5171. return MEMORY_E;
  5172. }
  5173. XMEMSET(cipher_big, 0, CHACHA_BIG_TEST_SIZE);
  5174. XMEMSET(plain_big, 0, CHACHA_BIG_TEST_SIZE);
  5175. XMEMSET(input_big, 0, CHACHA_BIG_TEST_SIZE);
  5176. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  5177. #endif /* BENCH_EMBEDDED */
  5178. for (i = 0; i < times; ++i) {
  5179. if (i < 3) {
  5180. keySz = 32;
  5181. }
  5182. else {
  5183. keySz = 16;
  5184. }
  5185. XMEMCPY(plain, keys[i], keySz);
  5186. XMEMSET(cipher, 0, 32);
  5187. XMEMCPY(cipher + 4, ivs[i], 8);
  5188. ret |= wc_Chacha_SetKey(&enc, keys[i], keySz);
  5189. ret |= wc_Chacha_SetKey(&dec, keys[i], keySz);
  5190. if (ret != 0)
  5191. return ret;
  5192. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  5193. ret |= wc_Chacha_SetIV(&dec, cipher, 0);
  5194. if (ret != 0)
  5195. return ret;
  5196. XMEMCPY(plain, input, 8);
  5197. ret |= wc_Chacha_Process(&enc, cipher, plain, (word32)8);
  5198. ret |= wc_Chacha_Process(&dec, plain, cipher, (word32)8);
  5199. if (ret != 0)
  5200. return ret;
  5201. if (XMEMCMP(test_chacha[i], cipher, 8))
  5202. return -4700 - i;
  5203. if (XMEMCMP(plain, input, 8))
  5204. return -4710 - i;
  5205. }
  5206. /* test of starting at a different counter
  5207. encrypts all of the information and decrypts starting at 2nd chunk */
  5208. XMEMSET(plain, 0, sizeof(plain));
  5209. XMEMSET(sliver, 1, sizeof(sliver)); /* set as 1's to not match plain */
  5210. XMEMSET(cipher, 0, sizeof(cipher));
  5211. XMEMCPY(cipher + 4, ivs[0], 8);
  5212. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  5213. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  5214. if (ret != 0)
  5215. return ret;
  5216. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  5217. ret |= wc_Chacha_SetIV(&dec, cipher, 1);
  5218. if (ret != 0)
  5219. return ret;
  5220. ret |= wc_Chacha_Process(&enc, cipher, plain, sizeof(plain));
  5221. ret |= wc_Chacha_Process(&dec, sliver, cipher + 64, sizeof(sliver));
  5222. if (ret != 0)
  5223. return ret;
  5224. if (XMEMCMP(plain + 64, sliver, 64))
  5225. return -4720;
  5226. #ifndef BENCH_EMBEDDED
  5227. /* test of encrypting more data */
  5228. keySz = 32;
  5229. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  5230. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  5231. if (ret != 0)
  5232. return ret;
  5233. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  5234. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  5235. if (ret != 0)
  5236. return ret;
  5237. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big, CHACHA_BIG_TEST_SIZE);
  5238. ret |= wc_Chacha_Process(&dec, plain_big, cipher_big,
  5239. CHACHA_BIG_TEST_SIZE);
  5240. if (ret != 0)
  5241. return ret;
  5242. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  5243. return -4721;
  5244. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  5245. return -4722;
  5246. for (i = 0; i < 18; ++i) {
  5247. /* this will test all paths
  5248. * block sizes: 1 3 7 15 31 63 127 255 511 (i = 0- 8)
  5249. * 2 4 8 16 32 64 128 256 512 (i = 9-17)
  5250. */
  5251. block_size = (2 << (i%9)) - (i<9?1:0);
  5252. keySz = 32;
  5253. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  5254. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  5255. if (ret != 0)
  5256. return ret;
  5257. ret |= wc_Chacha_SetIV(&enc, ivs[2], 0);
  5258. ret |= wc_Chacha_SetIV(&dec, ivs[2], 0);
  5259. if (ret != 0)
  5260. return ret;
  5261. ret |= wc_Chacha_Process(&enc, cipher_big, plain_big , block_size);
  5262. ret |= wc_Chacha_Process(&dec, plain_big , cipher_big, block_size);
  5263. if (ret != 0)
  5264. return ret;
  5265. if (XMEMCMP(plain_big, input_big, block_size))
  5266. return -4740-i*2;
  5267. if (XMEMCMP(cipher_big, cipher_big_result, block_size))
  5268. return -4741-i*2;
  5269. }
  5270. /* Streaming test */
  5271. for (i = 1; i <= (int)CHACHA_CHUNK_BYTES + 1; i++) {
  5272. int j, rem;
  5273. ret = wc_Chacha_SetKey(&enc, keys[0], keySz);
  5274. if (ret != 0)
  5275. return -4725;
  5276. ret = wc_Chacha_SetKey(&dec, keys[0], keySz);
  5277. if (ret != 0)
  5278. return -4726;
  5279. ret = wc_Chacha_SetIV(&enc, ivs[2], 0);
  5280. if (ret != 0)
  5281. return -4727;
  5282. ret = wc_Chacha_SetIV(&dec, ivs[2], 0);
  5283. if (ret != 0)
  5284. return -4728;
  5285. for (j = 0; j < CHACHA_BIG_TEST_SIZE - i; j+= i) {
  5286. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, i);
  5287. if (ret != 0)
  5288. return -4729;
  5289. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, i);
  5290. if (ret != 0)
  5291. return -4730;
  5292. }
  5293. rem = CHACHA_BIG_TEST_SIZE - j;
  5294. ret = wc_Chacha_Process(&enc, cipher_big + j, plain_big + j, rem);
  5295. if (ret != 0)
  5296. return -4731;
  5297. ret = wc_Chacha_Process(&dec, plain_big + j, cipher_big + j, rem);
  5298. if (ret != 0)
  5299. return -4732;
  5300. if (XMEMCMP(plain_big, input_big, CHACHA_BIG_TEST_SIZE))
  5301. return -4733;
  5302. if (XMEMCMP(cipher_big, cipher_big_result, CHACHA_BIG_TEST_SIZE))
  5303. return -4734;
  5304. }
  5305. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  5306. XFREE(cipher_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5307. XFREE(plain_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5308. XFREE(input_big, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5309. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  5310. #endif /* BENCH_EMBEDDED */
  5311. return 0;
  5312. }
  5313. #endif /* HAVE_CHACHA */
  5314. #ifdef HAVE_POLY1305
  5315. WOLFSSL_TEST_SUBROUTINE int poly1305_test(void)
  5316. {
  5317. int ret = 0;
  5318. int i;
  5319. byte tag[16];
  5320. Poly1305 enc;
  5321. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  5322. {
  5323. 0x43,0x72,0x79,0x70,0x74,0x6f,0x67,0x72,
  5324. 0x61,0x70,0x68,0x69,0x63,0x20,0x46,0x6f,
  5325. 0x72,0x75,0x6d,0x20,0x52,0x65,0x73,0x65,
  5326. 0x61,0x72,0x63,0x68,0x20,0x47,0x72,0x6f,
  5327. 0x75,0x70
  5328. };
  5329. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  5330. {
  5331. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x77,0x6f,0x72,
  5332. 0x6c,0x64,0x21
  5333. };
  5334. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  5335. {
  5336. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5337. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5338. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5339. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5340. };
  5341. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] =
  5342. {
  5343. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  5344. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  5345. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  5346. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  5347. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  5348. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  5349. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  5350. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  5351. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  5352. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  5353. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  5354. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  5355. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  5356. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  5357. 0x61,0x16
  5358. };
  5359. WOLFSSL_SMALL_STACK_STATIC const byte msg5[] =
  5360. {
  5361. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  5362. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  5363. };
  5364. WOLFSSL_SMALL_STACK_STATIC const byte msg6[] =
  5365. {
  5366. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  5367. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  5368. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  5369. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  5370. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  5371. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  5372. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  5373. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  5374. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  5375. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  5376. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  5377. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  5378. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  5379. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  5380. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  5381. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  5382. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  5383. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  5384. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  5385. 0x61,0x16
  5386. };
  5387. byte additional[] =
  5388. {
  5389. 0x50,0x51,0x52,0x53,0xc0,0xc1,0xc2,0xc3,
  5390. 0xc4,0xc5,0xc6,0xc7
  5391. };
  5392. WOLFSSL_SMALL_STACK_STATIC const byte correct0[] =
  5393. {
  5394. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  5395. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  5396. };
  5397. WOLFSSL_SMALL_STACK_STATIC const byte correct1[] =
  5398. {
  5399. 0xa8,0x06,0x1d,0xc1,0x30,0x51,0x36,0xc6,
  5400. 0xc2,0x2b,0x8b,0xaf,0x0c,0x01,0x27,0xa9
  5401. };
  5402. WOLFSSL_SMALL_STACK_STATIC const byte correct2[] =
  5403. {
  5404. 0xa6,0xf7,0x45,0x00,0x8f,0x81,0xc9,0x16,
  5405. 0xa2,0x0d,0xcc,0x74,0xee,0xf2,0xb2,0xf0
  5406. };
  5407. WOLFSSL_SMALL_STACK_STATIC const byte correct3[] =
  5408. {
  5409. 0x49,0xec,0x78,0x09,0x0e,0x48,0x1e,0xc6,
  5410. 0xc2,0x6b,0x33,0xb9,0x1c,0xcc,0x03,0x07
  5411. };
  5412. WOLFSSL_SMALL_STACK_STATIC const byte correct4[] =
  5413. {
  5414. 0x1a,0xe1,0x0b,0x59,0x4f,0x09,0xe2,0x6a,
  5415. 0x7e,0x90,0x2e,0xcb,0xd0,0x60,0x06,0x91
  5416. };
  5417. WOLFSSL_SMALL_STACK_STATIC const byte correct5[] =
  5418. {
  5419. 0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5420. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5421. };
  5422. WOLFSSL_SMALL_STACK_STATIC const byte correct6[] =
  5423. {
  5424. 0xea,0x11,0x5c,0x4f,0xd0,0xc0,0x10,0xae,
  5425. 0xf7,0xdf,0xda,0x77,0xa2,0xe9,0xaf,0xca
  5426. };
  5427. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  5428. 0x85,0xd6,0xbe,0x78,0x57,0x55,0x6d,0x33,
  5429. 0x7f,0x44,0x52,0xfe,0x42,0xd5,0x06,0xa8,
  5430. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  5431. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  5432. };
  5433. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  5434. 0x74,0x68,0x69,0x73,0x20,0x69,0x73,0x20,
  5435. 0x33,0x32,0x2d,0x62,0x79,0x74,0x65,0x20,
  5436. 0x6b,0x65,0x79,0x20,0x66,0x6f,0x72,0x20,
  5437. 0x50,0x6f,0x6c,0x79,0x31,0x33,0x30,0x35
  5438. };
  5439. WOLFSSL_SMALL_STACK_STATIC const byte key4[] = {
  5440. 0x7b,0xac,0x2b,0x25,0x2d,0xb4,0x47,0xaf,
  5441. 0x09,0xb6,0x7a,0x55,0xa4,0xe9,0x55,0x84,
  5442. 0x0a,0xe1,0xd6,0x73,0x10,0x75,0xd9,0xeb,
  5443. 0x2a,0x93,0x75,0x78,0x3e,0xd5,0x53,0xff
  5444. };
  5445. WOLFSSL_SMALL_STACK_STATIC const byte key5[] = {
  5446. 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5447. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5448. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  5449. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  5450. };
  5451. const byte* msgs[] = {NULL, msg1, msg2, msg3, msg5, msg6};
  5452. word32 szm[] = {0, sizeof(msg1), sizeof(msg2),
  5453. sizeof(msg3), sizeof(msg5), sizeof(msg6)};
  5454. const byte* keys[] = {key, key, key2, key2, key5, key};
  5455. const byte* tests[] = {correct0, correct1, correct2, correct3, correct5,
  5456. correct6};
  5457. for (i = 0; i < 6; i++) {
  5458. ret = wc_Poly1305SetKey(&enc, keys[i], 32);
  5459. if (ret != 0)
  5460. return -4800 - i;
  5461. ret = wc_Poly1305Update(&enc, msgs[i], szm[i]);
  5462. if (ret != 0)
  5463. return -4810 - i;
  5464. ret = wc_Poly1305Final(&enc, tag);
  5465. if (ret != 0)
  5466. return -4820 - i;
  5467. if (XMEMCMP(tag, tests[i], sizeof(tag)))
  5468. return -4830 - i;
  5469. }
  5470. /* Check TLS MAC function from 2.8.2 https://tools.ietf.org/html/rfc7539 */
  5471. XMEMSET(tag, 0, sizeof(tag));
  5472. ret = wc_Poly1305SetKey(&enc, key4, sizeof(key4));
  5473. if (ret != 0)
  5474. return -4840;
  5475. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  5476. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  5477. if (ret != 0)
  5478. return -4841;
  5479. if (XMEMCMP(tag, correct4, sizeof(tag)))
  5480. return -4842;
  5481. /* Check fail of TLS MAC function if altering additional data */
  5482. XMEMSET(tag, 0, sizeof(tag));
  5483. additional[0]++;
  5484. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  5485. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  5486. if (ret != 0)
  5487. return -4843;
  5488. if (XMEMCMP(tag, correct4, sizeof(tag)) == 0)
  5489. return -4844;
  5490. return 0;
  5491. }
  5492. #endif /* HAVE_POLY1305 */
  5493. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  5494. WOLFSSL_TEST_SUBROUTINE int chacha20_poly1305_aead_test(void)
  5495. {
  5496. /* Test #1 from Section 2.8.2 of draft-irtf-cfrg-chacha20-poly1305-10 */
  5497. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  5498. WOLFSSL_SMALL_STACK_STATIC const byte key1[] = {
  5499. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  5500. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  5501. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  5502. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  5503. };
  5504. WOLFSSL_SMALL_STACK_STATIC const byte plaintext1[] = {
  5505. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  5506. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  5507. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  5508. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  5509. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  5510. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  5511. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  5512. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  5513. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  5514. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  5515. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  5516. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  5517. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  5518. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  5519. 0x74, 0x2e
  5520. };
  5521. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] = {
  5522. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  5523. 0x44, 0x45, 0x46, 0x47
  5524. };
  5525. WOLFSSL_SMALL_STACK_STATIC const byte aad1[] = { /* additional data */
  5526. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  5527. 0xc4, 0xc5, 0xc6, 0xc7
  5528. };
  5529. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] = { /* expected output from operation */
  5530. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  5531. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  5532. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  5533. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  5534. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  5535. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  5536. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  5537. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  5538. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  5539. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  5540. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  5541. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  5542. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  5543. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  5544. 0x61, 0x16
  5545. };
  5546. WOLFSSL_SMALL_STACK_STATIC const byte authTag1[] = { /* expected output from operation */
  5547. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  5548. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  5549. };
  5550. /* Test #2 from Appendix A.2 in draft-irtf-cfrg-chacha20-poly1305-10 */
  5551. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  5552. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  5553. 0x1c, 0x92, 0x40, 0xa5, 0xeb, 0x55, 0xd3, 0x8a,
  5554. 0xf3, 0x33, 0x88, 0x86, 0x04, 0xf6, 0xb5, 0xf0,
  5555. 0x47, 0x39, 0x17, 0xc1, 0x40, 0x2b, 0x80, 0x09,
  5556. 0x9d, 0xca, 0x5c, 0xbc, 0x20, 0x70, 0x75, 0xc0
  5557. };
  5558. WOLFSSL_SMALL_STACK_STATIC const byte plaintext2[] = {
  5559. 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74,
  5560. 0x2d, 0x44, 0x72, 0x61, 0x66, 0x74, 0x73, 0x20,
  5561. 0x61, 0x72, 0x65, 0x20, 0x64, 0x72, 0x61, 0x66,
  5562. 0x74, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  5563. 0x6e, 0x74, 0x73, 0x20, 0x76, 0x61, 0x6c, 0x69,
  5564. 0x64, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x61, 0x20,
  5565. 0x6d, 0x61, 0x78, 0x69, 0x6d, 0x75, 0x6d, 0x20,
  5566. 0x6f, 0x66, 0x20, 0x73, 0x69, 0x78, 0x20, 0x6d,
  5567. 0x6f, 0x6e, 0x74, 0x68, 0x73, 0x20, 0x61, 0x6e,
  5568. 0x64, 0x20, 0x6d, 0x61, 0x79, 0x20, 0x62, 0x65,
  5569. 0x20, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64,
  5570. 0x2c, 0x20, 0x72, 0x65, 0x70, 0x6c, 0x61, 0x63,
  5571. 0x65, 0x64, 0x2c, 0x20, 0x6f, 0x72, 0x20, 0x6f,
  5572. 0x62, 0x73, 0x6f, 0x6c, 0x65, 0x74, 0x65, 0x64,
  5573. 0x20, 0x62, 0x79, 0x20, 0x6f, 0x74, 0x68, 0x65,
  5574. 0x72, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  5575. 0x6e, 0x74, 0x73, 0x20, 0x61, 0x74, 0x20, 0x61,
  5576. 0x6e, 0x79, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x2e,
  5577. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x69,
  5578. 0x6e, 0x61, 0x70, 0x70, 0x72, 0x6f, 0x70, 0x72,
  5579. 0x69, 0x61, 0x74, 0x65, 0x20, 0x74, 0x6f, 0x20,
  5580. 0x75, 0x73, 0x65, 0x20, 0x49, 0x6e, 0x74, 0x65,
  5581. 0x72, 0x6e, 0x65, 0x74, 0x2d, 0x44, 0x72, 0x61,
  5582. 0x66, 0x74, 0x73, 0x20, 0x61, 0x73, 0x20, 0x72,
  5583. 0x65, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x65,
  5584. 0x20, 0x6d, 0x61, 0x74, 0x65, 0x72, 0x69, 0x61,
  5585. 0x6c, 0x20, 0x6f, 0x72, 0x20, 0x74, 0x6f, 0x20,
  5586. 0x63, 0x69, 0x74, 0x65, 0x20, 0x74, 0x68, 0x65,
  5587. 0x6d, 0x20, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20,
  5588. 0x74, 0x68, 0x61, 0x6e, 0x20, 0x61, 0x73, 0x20,
  5589. 0x2f, 0xe2, 0x80, 0x9c, 0x77, 0x6f, 0x72, 0x6b,
  5590. 0x20, 0x69, 0x6e, 0x20, 0x70, 0x72, 0x6f, 0x67,
  5591. 0x72, 0x65, 0x73, 0x73, 0x2e, 0x2f, 0xe2, 0x80,
  5592. 0x9d
  5593. };
  5594. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  5595. 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
  5596. 0x05, 0x06, 0x07, 0x08
  5597. };
  5598. WOLFSSL_SMALL_STACK_STATIC const byte aad2[] = { /* additional data */
  5599. 0xf3, 0x33, 0x88, 0x86, 0x00, 0x00, 0x00, 0x00,
  5600. 0x00, 0x00, 0x4e, 0x91
  5601. };
  5602. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] = { /* expected output from operation */
  5603. 0x64, 0xa0, 0x86, 0x15, 0x75, 0x86, 0x1a, 0xf4,
  5604. 0x60, 0xf0, 0x62, 0xc7, 0x9b, 0xe6, 0x43, 0xbd,
  5605. 0x5e, 0x80, 0x5c, 0xfd, 0x34, 0x5c, 0xf3, 0x89,
  5606. 0xf1, 0x08, 0x67, 0x0a, 0xc7, 0x6c, 0x8c, 0xb2,
  5607. 0x4c, 0x6c, 0xfc, 0x18, 0x75, 0x5d, 0x43, 0xee,
  5608. 0xa0, 0x9e, 0xe9, 0x4e, 0x38, 0x2d, 0x26, 0xb0,
  5609. 0xbd, 0xb7, 0xb7, 0x3c, 0x32, 0x1b, 0x01, 0x00,
  5610. 0xd4, 0xf0, 0x3b, 0x7f, 0x35, 0x58, 0x94, 0xcf,
  5611. 0x33, 0x2f, 0x83, 0x0e, 0x71, 0x0b, 0x97, 0xce,
  5612. 0x98, 0xc8, 0xa8, 0x4a, 0xbd, 0x0b, 0x94, 0x81,
  5613. 0x14, 0xad, 0x17, 0x6e, 0x00, 0x8d, 0x33, 0xbd,
  5614. 0x60, 0xf9, 0x82, 0xb1, 0xff, 0x37, 0xc8, 0x55,
  5615. 0x97, 0x97, 0xa0, 0x6e, 0xf4, 0xf0, 0xef, 0x61,
  5616. 0xc1, 0x86, 0x32, 0x4e, 0x2b, 0x35, 0x06, 0x38,
  5617. 0x36, 0x06, 0x90, 0x7b, 0x6a, 0x7c, 0x02, 0xb0,
  5618. 0xf9, 0xf6, 0x15, 0x7b, 0x53, 0xc8, 0x67, 0xe4,
  5619. 0xb9, 0x16, 0x6c, 0x76, 0x7b, 0x80, 0x4d, 0x46,
  5620. 0xa5, 0x9b, 0x52, 0x16, 0xcd, 0xe7, 0xa4, 0xe9,
  5621. 0x90, 0x40, 0xc5, 0xa4, 0x04, 0x33, 0x22, 0x5e,
  5622. 0xe2, 0x82, 0xa1, 0xb0, 0xa0, 0x6c, 0x52, 0x3e,
  5623. 0xaf, 0x45, 0x34, 0xd7, 0xf8, 0x3f, 0xa1, 0x15,
  5624. 0x5b, 0x00, 0x47, 0x71, 0x8c, 0xbc, 0x54, 0x6a,
  5625. 0x0d, 0x07, 0x2b, 0x04, 0xb3, 0x56, 0x4e, 0xea,
  5626. 0x1b, 0x42, 0x22, 0x73, 0xf5, 0x48, 0x27, 0x1a,
  5627. 0x0b, 0xb2, 0x31, 0x60, 0x53, 0xfa, 0x76, 0x99,
  5628. 0x19, 0x55, 0xeb, 0xd6, 0x31, 0x59, 0x43, 0x4e,
  5629. 0xce, 0xbb, 0x4e, 0x46, 0x6d, 0xae, 0x5a, 0x10,
  5630. 0x73, 0xa6, 0x72, 0x76, 0x27, 0x09, 0x7a, 0x10,
  5631. 0x49, 0xe6, 0x17, 0xd9, 0x1d, 0x36, 0x10, 0x94,
  5632. 0xfa, 0x68, 0xf0, 0xff, 0x77, 0x98, 0x71, 0x30,
  5633. 0x30, 0x5b, 0xea, 0xba, 0x2e, 0xda, 0x04, 0xdf,
  5634. 0x99, 0x7b, 0x71, 0x4d, 0x6c, 0x6f, 0x2c, 0x29,
  5635. 0xa6, 0xad, 0x5c, 0xb4, 0x02, 0x2b, 0x02, 0x70,
  5636. 0x9b
  5637. };
  5638. WOLFSSL_SMALL_STACK_STATIC const byte authTag2[] = { /* expected output from operation */
  5639. 0xee, 0xad, 0x9d, 0x67, 0x89, 0x0c, 0xbb, 0x22,
  5640. 0x39, 0x23, 0x36, 0xfe, 0xa1, 0x85, 0x1f, 0x38
  5641. };
  5642. byte generatedCiphertext[265]; /* max plaintext2/cipher2 */
  5643. byte generatedPlaintext[265]; /* max plaintext2/cipher2 */
  5644. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  5645. int err;
  5646. ChaChaPoly_Aead aead;
  5647. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  5648. #define TEST_SMALL_CHACHA_CHUNKS 32
  5649. #else
  5650. #define TEST_SMALL_CHACHA_CHUNKS 64
  5651. #endif
  5652. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5653. word32 testLen;
  5654. #endif
  5655. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5656. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5657. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5658. /* Parameter Validation testing */
  5659. /* Encrypt */
  5660. err = wc_ChaCha20Poly1305_Encrypt(NULL, iv1, aad1, sizeof(aad1), plaintext1,
  5661. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  5662. if (err != BAD_FUNC_ARG)
  5663. return -4900;
  5664. err = wc_ChaCha20Poly1305_Encrypt(key1, NULL, aad1, sizeof(aad1),
  5665. plaintext1, sizeof(plaintext1), generatedCiphertext,
  5666. generatedAuthTag);
  5667. if (err != BAD_FUNC_ARG)
  5668. return -4901;
  5669. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL,
  5670. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  5671. if (err != BAD_FUNC_ARG)
  5672. return -4902;
  5673. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  5674. sizeof(plaintext1), NULL, generatedAuthTag);
  5675. if (err != BAD_FUNC_ARG)
  5676. return -4903;
  5677. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  5678. sizeof(plaintext1), generatedCiphertext, NULL);
  5679. if (err != BAD_FUNC_ARG)
  5680. return -4904;
  5681. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL,
  5682. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  5683. if (err != BAD_FUNC_ARG)
  5684. return -4905;
  5685. /* Decrypt */
  5686. err = wc_ChaCha20Poly1305_Decrypt(NULL, iv2, aad2, sizeof(aad2), cipher2,
  5687. sizeof(cipher2), authTag2, generatedPlaintext);
  5688. if (err != BAD_FUNC_ARG)
  5689. return -4906;
  5690. err = wc_ChaCha20Poly1305_Decrypt(key2, NULL, aad2, sizeof(aad2), cipher2,
  5691. sizeof(cipher2), authTag2, generatedPlaintext);
  5692. if (err != BAD_FUNC_ARG)
  5693. return -4907;
  5694. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL,
  5695. sizeof(cipher2), authTag2, generatedPlaintext);
  5696. if (err != BAD_FUNC_ARG)
  5697. return -4908;
  5698. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  5699. sizeof(cipher2), NULL, generatedPlaintext);
  5700. if (err != BAD_FUNC_ARG)
  5701. return -4909;
  5702. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  5703. sizeof(cipher2), authTag2, NULL);
  5704. if (err != BAD_FUNC_ARG)
  5705. return -4910;
  5706. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL,
  5707. sizeof(cipher2), authTag2, generatedPlaintext);
  5708. if (err != BAD_FUNC_ARG)
  5709. return -4911;
  5710. /* Test #1 */
  5711. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1,
  5712. aad1, sizeof(aad1),
  5713. plaintext1, sizeof(plaintext1),
  5714. generatedCiphertext, generatedAuthTag);
  5715. if (err) {
  5716. return err;
  5717. }
  5718. /* -- Check the ciphertext and authtag */
  5719. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  5720. return -4912;
  5721. }
  5722. if (XMEMCMP(generatedAuthTag, authTag1, sizeof(authTag1))) {
  5723. return -4913;
  5724. }
  5725. /* -- Verify decryption works */
  5726. err = wc_ChaCha20Poly1305_Decrypt(key1, iv1,
  5727. aad1, sizeof(aad1),
  5728. cipher1, sizeof(cipher1),
  5729. authTag1, generatedPlaintext);
  5730. if (err) {
  5731. return err;
  5732. }
  5733. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  5734. return -4914;
  5735. }
  5736. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5737. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5738. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5739. /* Test #2 */
  5740. err = wc_ChaCha20Poly1305_Encrypt(key2, iv2,
  5741. aad2, sizeof(aad2),
  5742. plaintext2, sizeof(plaintext2),
  5743. generatedCiphertext, generatedAuthTag);
  5744. if (err) {
  5745. return err;
  5746. }
  5747. /* -- Check the ciphertext and authtag */
  5748. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  5749. return -4915;
  5750. }
  5751. if (XMEMCMP(generatedAuthTag, authTag2, sizeof(authTag2))) {
  5752. return -4916;
  5753. }
  5754. /* -- Verify decryption works */
  5755. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2,
  5756. aad2, sizeof(aad2),
  5757. cipher2, sizeof(cipher2),
  5758. authTag2, generatedPlaintext);
  5759. if (err) {
  5760. return err;
  5761. }
  5762. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  5763. return -4917;
  5764. }
  5765. /* AEAD init/update/final - bad argument tests */
  5766. err = wc_ChaCha20Poly1305_Init(NULL, key1, iv1,
  5767. CHACHA20_POLY1305_AEAD_DECRYPT);
  5768. if (err != BAD_FUNC_ARG)
  5769. return -4918;
  5770. err = wc_ChaCha20Poly1305_Init(&aead, NULL, iv1,
  5771. CHACHA20_POLY1305_AEAD_DECRYPT);
  5772. if (err != BAD_FUNC_ARG)
  5773. return -4919;
  5774. err = wc_ChaCha20Poly1305_Init(&aead, key1, NULL,
  5775. CHACHA20_POLY1305_AEAD_DECRYPT);
  5776. if (err != BAD_FUNC_ARG)
  5777. return -4920;
  5778. err = wc_ChaCha20Poly1305_UpdateAad(NULL, aad1, sizeof(aad1));
  5779. if (err != BAD_FUNC_ARG)
  5780. return -4921;
  5781. err = wc_ChaCha20Poly1305_UpdateAad(&aead, NULL, sizeof(aad1));
  5782. if (err != BAD_FUNC_ARG)
  5783. return -4922;
  5784. err = wc_ChaCha20Poly1305_UpdateData(NULL, generatedPlaintext,
  5785. generatedPlaintext, sizeof(plaintext1));
  5786. if (err != BAD_FUNC_ARG)
  5787. return -4923;
  5788. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext, NULL,
  5789. sizeof(plaintext1));
  5790. if (err != BAD_FUNC_ARG)
  5791. return -4924;
  5792. err = wc_ChaCha20Poly1305_UpdateData(&aead, NULL, generatedPlaintext,
  5793. sizeof(plaintext1));
  5794. if (err != BAD_FUNC_ARG)
  5795. return -4925;
  5796. err = wc_ChaCha20Poly1305_Final(NULL, generatedAuthTag);
  5797. if (err != BAD_FUNC_ARG)
  5798. return -4926;
  5799. err = wc_ChaCha20Poly1305_Final(&aead, NULL);
  5800. if (err != BAD_FUNC_ARG)
  5801. return -4927;
  5802. /* AEAD init/update/final - bad state tests */
  5803. /* clear struct - make valgrind happy to resolve
  5804. "Conditional jump or move depends on uninitialised value(s)".
  5805. The enum is "int" size and aead.state is "byte" */
  5806. /* The wc_ChaCha20Poly1305_Init function does this normally */
  5807. XMEMSET(&aead, 0, sizeof(aead));
  5808. aead.state = CHACHA20_POLY1305_STATE_INIT;
  5809. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5810. if (err != BAD_STATE_E)
  5811. return -4928;
  5812. aead.state = CHACHA20_POLY1305_STATE_DATA;
  5813. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5814. if (err != BAD_STATE_E)
  5815. return -4929;
  5816. aead.state = CHACHA20_POLY1305_STATE_INIT;
  5817. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedPlaintext,
  5818. generatedPlaintext, sizeof(plaintext1));
  5819. if (err != BAD_STATE_E)
  5820. return -4930;
  5821. aead.state = CHACHA20_POLY1305_STATE_INIT;
  5822. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5823. if (err != BAD_STATE_E)
  5824. return -4931;
  5825. aead.state = CHACHA20_POLY1305_STATE_READY;
  5826. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5827. if (err != BAD_STATE_E)
  5828. return -4932;
  5829. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5830. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5831. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5832. /* Test 1 - Encrypt */
  5833. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  5834. CHACHA20_POLY1305_AEAD_ENCRYPT);
  5835. if (err != 0)
  5836. return -4933;
  5837. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5838. if (err != 0)
  5839. return -4934;
  5840. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5841. /* test doing data in smaller chunks */
  5842. for (testLen=0; testLen<sizeof(plaintext1); ) {
  5843. word32 dataLen = sizeof(plaintext1) - testLen;
  5844. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5845. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5846. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext1[testLen],
  5847. &generatedCiphertext[testLen], dataLen);
  5848. if (err != 0)
  5849. return -4935;
  5850. testLen += dataLen;
  5851. }
  5852. #else
  5853. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext1,
  5854. generatedCiphertext, sizeof(plaintext1));
  5855. #endif
  5856. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5857. if (err != 0)
  5858. return -4936;
  5859. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  5860. if (err != 0)
  5861. return -4937;
  5862. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  5863. return -4938;
  5864. }
  5865. /* Test 1 - Decrypt */
  5866. err = wc_ChaCha20Poly1305_Init(&aead, key1, iv1,
  5867. CHACHA20_POLY1305_AEAD_DECRYPT);
  5868. if (err != 0)
  5869. return -4939;
  5870. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad1, sizeof(aad1));
  5871. if (err != 0)
  5872. return -4940;
  5873. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5874. /* test doing data in smaller chunks */
  5875. for (testLen=0; testLen<sizeof(plaintext1); ) {
  5876. word32 dataLen = sizeof(plaintext1) - testLen;
  5877. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5878. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5879. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  5880. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  5881. dataLen);
  5882. if (err != 0)
  5883. return -4941;
  5884. testLen += dataLen;
  5885. }
  5886. #else
  5887. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  5888. generatedPlaintext, sizeof(cipher1));
  5889. #endif
  5890. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5891. if (err != 0)
  5892. return -4942;
  5893. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag1);
  5894. if (err != 0)
  5895. return -4943;
  5896. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof(plaintext1))) {
  5897. return -4944;
  5898. }
  5899. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  5900. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  5901. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  5902. /* Test 2 - Encrypt */
  5903. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  5904. CHACHA20_POLY1305_AEAD_ENCRYPT);
  5905. if (err != 0)
  5906. return -4945;
  5907. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  5908. if (err != 0)
  5909. return -4946;
  5910. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5911. /* test doing data in smaller chunks */
  5912. for (testLen=0; testLen<sizeof(plaintext2); ) {
  5913. word32 dataLen = sizeof(plaintext2) - testLen;
  5914. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5915. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5916. err = wc_ChaCha20Poly1305_UpdateData(&aead, &plaintext2[testLen],
  5917. &generatedCiphertext[testLen], dataLen);
  5918. if (err != 0)
  5919. return -4947;
  5920. testLen += dataLen;
  5921. }
  5922. #else
  5923. err = wc_ChaCha20Poly1305_UpdateData(&aead, plaintext2, generatedCiphertext,
  5924. sizeof(plaintext2));
  5925. #endif
  5926. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5927. if (err != 0)
  5928. return -4948;
  5929. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  5930. if (err != 0)
  5931. return -4949;
  5932. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  5933. return -4950;
  5934. }
  5935. /* Test 2 - Decrypt */
  5936. err = wc_ChaCha20Poly1305_Init(&aead, key2, iv2,
  5937. CHACHA20_POLY1305_AEAD_DECRYPT);
  5938. if (err != 0)
  5939. return -4951;
  5940. err = wc_ChaCha20Poly1305_UpdateAad(&aead, aad2, sizeof(aad2));
  5941. if (err != 0)
  5942. return -4952;
  5943. #ifdef TEST_SMALL_CHACHA_CHUNKS
  5944. /* test doing data in smaller chunks */
  5945. for (testLen=0; testLen<sizeof(plaintext2); ) {
  5946. word32 dataLen = sizeof(plaintext2) - testLen;
  5947. if (dataLen > TEST_SMALL_CHACHA_CHUNKS)
  5948. dataLen = TEST_SMALL_CHACHA_CHUNKS;
  5949. err = wc_ChaCha20Poly1305_UpdateData(&aead,
  5950. &generatedCiphertext[testLen], &generatedPlaintext[testLen],
  5951. dataLen);
  5952. if (err != 0)
  5953. return -4953;
  5954. testLen += dataLen;
  5955. }
  5956. #else
  5957. err = wc_ChaCha20Poly1305_UpdateData(&aead, generatedCiphertext,
  5958. generatedPlaintext, sizeof(cipher2));
  5959. #endif
  5960. err = wc_ChaCha20Poly1305_Final(&aead, generatedAuthTag);
  5961. if (err != 0)
  5962. return -4954;
  5963. err = wc_ChaCha20Poly1305_CheckTag(generatedAuthTag, authTag2);
  5964. if (err != 0)
  5965. return -4955;
  5966. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  5967. return -4956;
  5968. }
  5969. return err;
  5970. }
  5971. #endif /* HAVE_CHACHA && HAVE_POLY1305 */
  5972. #ifndef NO_DES3
  5973. WOLFSSL_TEST_SUBROUTINE int des_test(void)
  5974. {
  5975. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  5976. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  5977. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  5978. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  5979. };
  5980. byte plain[24];
  5981. byte cipher[24];
  5982. Des enc;
  5983. Des dec;
  5984. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  5985. {
  5986. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  5987. };
  5988. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  5989. {
  5990. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  5991. };
  5992. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  5993. {
  5994. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  5995. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  5996. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  5997. };
  5998. int ret;
  5999. ret = wc_Des_SetKey(&enc, key, iv, DES_ENCRYPTION);
  6000. if (ret != 0)
  6001. return -5000;
  6002. ret = wc_Des_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  6003. if (ret != 0)
  6004. return -5001;
  6005. ret = wc_Des_SetKey(&dec, key, iv, DES_DECRYPTION);
  6006. if (ret != 0)
  6007. return -5002;
  6008. ret = wc_Des_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  6009. if (ret != 0)
  6010. return -5003;
  6011. if (XMEMCMP(plain, vector, sizeof(plain)))
  6012. return -5004;
  6013. if (XMEMCMP(cipher, verify, sizeof(cipher)))
  6014. return -5005;
  6015. ret = wc_Des_CbcEncryptWithKey(cipher, vector, sizeof(vector), key, iv);
  6016. if (ret != 0)
  6017. return -5006;
  6018. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  6019. {
  6020. EncryptedInfo info;
  6021. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  6022. XMEMCPY(info.iv, iv, sizeof(iv));
  6023. info.ivSz = sizeof(iv);
  6024. info.keySz = sizeof(key);
  6025. info.cipherType = WC_CIPHER_DES;
  6026. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  6027. sizeof(key), WC_HASH_TYPE_SHA);
  6028. if (ret != 0)
  6029. return -5007;
  6030. /* Test invalid info ptr */
  6031. ret = wc_BufferKeyEncrypt(NULL, cipher, sizeof(cipher), key,
  6032. sizeof(key), WC_HASH_TYPE_SHA);
  6033. if (ret != BAD_FUNC_ARG)
  6034. return -5008;
  6035. #ifndef NO_PWDBASED
  6036. /* Test invalid hash type - only applies to wc_PBKDF1 call */
  6037. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key,
  6038. sizeof(key), WC_HASH_TYPE_NONE);
  6039. if (ret == 0)
  6040. return -5009;
  6041. #endif /* !NO_PWDBASED */
  6042. }
  6043. #endif
  6044. return 0;
  6045. }
  6046. #endif /* !NO_DES3 */
  6047. #ifndef NO_DES3
  6048. WOLFSSL_TEST_SUBROUTINE int des3_test(void)
  6049. {
  6050. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  6051. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  6052. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  6053. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  6054. };
  6055. byte plain[24];
  6056. byte cipher[24];
  6057. Des3 enc;
  6058. Des3 dec;
  6059. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6060. {
  6061. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  6062. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  6063. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  6064. };
  6065. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  6066. {
  6067. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  6068. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  6069. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  6070. };
  6071. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] =
  6072. {
  6073. 0x43,0xa0,0x29,0x7e,0xd1,0x84,0xf8,0x0e,
  6074. 0x89,0x64,0x84,0x32,0x12,0xd5,0x08,0x98,
  6075. 0x18,0x94,0x15,0x74,0x87,0x12,0x7d,0xb0
  6076. };
  6077. int ret;
  6078. if (wc_Des3Init(&enc, HEAP_HINT, devId) != 0)
  6079. return -5100;
  6080. if (wc_Des3Init(&dec, HEAP_HINT, devId) != 0)
  6081. return -5101;
  6082. ret = wc_Des3_SetKey(&enc, key3, iv3, DES_ENCRYPTION);
  6083. if (ret != 0)
  6084. return -5102;
  6085. ret = wc_Des3_SetKey(&dec, key3, iv3, DES_DECRYPTION);
  6086. if (ret != 0)
  6087. return -5103;
  6088. ret = wc_Des3_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  6089. #if defined(WOLFSSL_ASYNC_CRYPT)
  6090. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  6091. #endif
  6092. if (ret != 0)
  6093. return -5104;
  6094. ret = wc_Des3_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  6095. #if defined(WOLFSSL_ASYNC_CRYPT)
  6096. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  6097. #endif
  6098. if (ret != 0)
  6099. return -5105;
  6100. if (XMEMCMP(plain, vector, sizeof(plain)))
  6101. return -5106;
  6102. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  6103. return -5107;
  6104. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  6105. /* test the same vectors with using compatibility layer */
  6106. {
  6107. DES_key_schedule ks1;
  6108. DES_key_schedule ks2;
  6109. DES_key_schedule ks3;
  6110. DES_cblock iv4;
  6111. XMEMCPY(ks1, key3, sizeof(DES_key_schedule));
  6112. XMEMCPY(ks2, key3 + 8, sizeof(DES_key_schedule));
  6113. XMEMCPY(ks3, key3 + 16, sizeof(DES_key_schedule));
  6114. XMEMCPY(iv4, iv3, sizeof(DES_cblock));
  6115. XMEMSET(plain, 0, sizeof(plain));
  6116. XMEMSET(cipher, 0, sizeof(cipher));
  6117. DES_ede3_cbc_encrypt(vector, cipher, sizeof(vector), &ks1, &ks2, &ks3,
  6118. &iv4, DES_ENCRYPT);
  6119. DES_ede3_cbc_encrypt(cipher, plain, sizeof(cipher), &ks1, &ks2, &ks3,
  6120. &iv4, DES_DECRYPT);
  6121. if (XMEMCMP(plain, vector, sizeof(plain)))
  6122. return -5108;
  6123. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  6124. return -5109;
  6125. }
  6126. #endif /* OPENSSL_EXTRA */
  6127. wc_Des3Free(&enc);
  6128. wc_Des3Free(&dec);
  6129. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
  6130. {
  6131. EncryptedInfo info;
  6132. XMEMSET(&info, 0, sizeof(EncryptedInfo));
  6133. XMEMCPY(info.iv, iv3, sizeof(iv3));
  6134. info.ivSz = sizeof(iv3);
  6135. info.keySz = sizeof(key3);
  6136. info.cipherType = WC_CIPHER_DES3;
  6137. ret = wc_BufferKeyEncrypt(&info, cipher, sizeof(cipher), key3,
  6138. sizeof(key3), WC_HASH_TYPE_SHA);
  6139. if (ret != 0)
  6140. return -5110;
  6141. }
  6142. #endif
  6143. return 0;
  6144. }
  6145. #endif /* NO_DES3 */
  6146. #ifndef NO_AES
  6147. #if defined(WOLFSSL_AES_OFB) || defined(WOLFSSL_AES_CFB) || \
  6148. defined(WOLFSSL_AES_XTS)
  6149. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6150. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6151. /* pass in the function, key, iv, plain text and expected and this function
  6152. * tests that the encryption and decryption is successful */
  6153. static int EVP_test(const WOLFSSL_EVP_CIPHER* type, const byte* key,
  6154. const byte* iv, const byte* plain, int plainSz,
  6155. const byte* expected, int expectedSz)
  6156. {
  6157. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6158. EVP_CIPHER_CTX *ctx = NULL;
  6159. #else
  6160. EVP_CIPHER_CTX ctx[1];
  6161. #endif
  6162. int idx, ret = 0, cipherSz;
  6163. byte* cipher;
  6164. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6165. if ((ctx = wolfSSL_EVP_CIPHER_CTX_new()) == NULL)
  6166. return MEMORY_E;
  6167. #endif
  6168. cipher = (byte*)XMALLOC(plainSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6169. if (cipher == NULL) {
  6170. ret = -5120;
  6171. goto EVP_TEST_END;
  6172. }
  6173. /* test encrypt */
  6174. EVP_CIPHER_CTX_init(ctx);
  6175. if (EVP_CipherInit(ctx, type, key, iv, 1) == 0) {
  6176. ret = -5121;
  6177. goto EVP_TEST_END;
  6178. }
  6179. if (EVP_CipherUpdate(ctx, cipher, &idx, plain, expectedSz) == 0) {
  6180. ret = -5122;
  6181. goto EVP_TEST_END;
  6182. }
  6183. cipherSz = idx;
  6184. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  6185. ret = -5123;
  6186. goto EVP_TEST_END;
  6187. }
  6188. cipherSz += idx;
  6189. if (XMEMCMP(cipher, expected, plainSz)) {
  6190. ret = -5124;
  6191. goto EVP_TEST_END;
  6192. }
  6193. /* test decrypt */
  6194. EVP_CIPHER_CTX_init(ctx);
  6195. if (EVP_CipherInit(ctx, type, key, iv, 0) == 0) {
  6196. ret = -5125;
  6197. goto EVP_TEST_END;
  6198. }
  6199. if (EVP_CipherUpdate(ctx, cipher, &idx, cipher, expectedSz) == 0) {
  6200. ret = -5126;
  6201. goto EVP_TEST_END;
  6202. }
  6203. cipherSz = idx;
  6204. if (EVP_CipherFinal(ctx, cipher + cipherSz, &idx) == 0) {
  6205. ret = -5127;
  6206. goto EVP_TEST_END;
  6207. }
  6208. cipherSz += idx;
  6209. if ((expectedSz != cipherSz) || XMEMCMP(plain, cipher, plainSz)) {
  6210. ret = -5128;
  6211. goto EVP_TEST_END;
  6212. }
  6213. EVP_TEST_END:
  6214. if (cipher)
  6215. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6216. (void)cipherSz;
  6217. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6218. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  6219. #endif
  6220. return ret;
  6221. }
  6222. #endif /* OPENSSL_EXTRA */
  6223. #endif /* WOLFSSL_AES_OFB || WOLFSSL_AES_CFB */
  6224. #ifdef WOLFSSL_AES_OFB
  6225. /* test vector from https://csrc.nist.gov/Projects/cryptographic-algorithm-validation-program/Block-Ciphers */
  6226. WOLFSSL_TEST_SUBROUTINE int aesofb_test(void)
  6227. {
  6228. #ifdef WOLFSSL_AES_256
  6229. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6230. {
  6231. 0xc4,0xc7,0xfa,0xd6,0x53,0x5c,0xb8,0x71,
  6232. 0x4a,0x5c,0x40,0x77,0x9a,0x8b,0xa1,0xd2,
  6233. 0x53,0x3e,0x23,0xb4,0xb2,0x58,0x73,0x2a,
  6234. 0x5b,0x78,0x01,0xf4,0xe3,0x71,0xa7,0x94
  6235. };
  6236. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  6237. {
  6238. 0x5e,0xb9,0x33,0x13,0xb8,0x71,0xff,0x16,
  6239. 0xb9,0x8a,0x9b,0xcb,0x43,0x33,0x0d,0x6f
  6240. };
  6241. WOLFSSL_SMALL_STACK_STATIC const byte plain1[] =
  6242. {
  6243. 0x6d,0x0b,0xb0,0x79,0x63,0x84,0x71,0xe9,
  6244. 0x39,0xd4,0x53,0x14,0x86,0xc1,0x4c,0x25,
  6245. 0x9a,0xee,0xc6,0xf3,0xc0,0x0d,0xfd,0xd6,
  6246. 0xc0,0x50,0xa8,0xba,0xa8,0x20,0xdb,0x71,
  6247. 0xcc,0x12,0x2c,0x4e,0x0c,0x17,0x15,0xef,
  6248. 0x55,0xf3,0x99,0x5a,0x6b,0xf0,0x2a,0x4c
  6249. };
  6250. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  6251. {
  6252. 0x0f,0x54,0x61,0x71,0x59,0xd0,0x3f,0xfc,
  6253. 0x1b,0xfa,0xfb,0x60,0x29,0x30,0xd7,0x00,
  6254. 0xf4,0xa4,0xa8,0xe6,0xdd,0x93,0x94,0x46,
  6255. 0x64,0xd2,0x19,0xc4,0xc5,0x4d,0xde,0x1b,
  6256. 0x04,0x53,0xe1,0x73,0xf5,0x18,0x74,0xae,
  6257. 0xfd,0x64,0xa2,0xe1,0xe2,0x76,0x13,0xb0
  6258. };
  6259. #endif /* WOLFSSL_AES_256 */
  6260. #ifdef WOLFSSL_AES_128
  6261. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6262. {
  6263. 0x10,0xa5,0x88,0x69,0xd7,0x4b,0xe5,0xa3,
  6264. 0x74,0xcf,0x86,0x7c,0xfb,0x47,0x38,0x59
  6265. };
  6266. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  6267. {
  6268. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6269. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6270. };
  6271. WOLFSSL_SMALL_STACK_STATIC const byte plain2[] =
  6272. {
  6273. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6274. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6275. };
  6276. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  6277. {
  6278. 0x6d,0x25,0x1e,0x69,0x44,0xb0,0x51,0xe0,
  6279. 0x4e,0xaa,0x6f,0xb4,0xdb,0xf7,0x84,0x65
  6280. };
  6281. #endif /* WOLFSSL_AES_128 */
  6282. #ifdef WOLFSSL_AES_192
  6283. WOLFSSL_SMALL_STACK_STATIC const byte key3[] = {
  6284. 0xd0,0x77,0xa0,0x3b,0xd8,0xa3,0x89,0x73,
  6285. 0x92,0x8c,0xca,0xfe,0x4a,0x9d,0x2f,0x45,
  6286. 0x51,0x30,0xbd,0x0a,0xf5,0xae,0x46,0xa9
  6287. };
  6288. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  6289. {
  6290. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6291. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6292. };
  6293. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  6294. {
  6295. 0xab,0xc7,0x86,0xfb,0x1e,0xdb,0x50,0x45,
  6296. 0x80,0xc4,0xd8,0x82,0xef,0x29,0xa0,0xc7
  6297. };
  6298. WOLFSSL_SMALL_STACK_STATIC const byte plain3[] =
  6299. {
  6300. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  6301. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  6302. };
  6303. #endif /* WOLFSSL_AES_192 */
  6304. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6305. Aes *enc = NULL;
  6306. #else
  6307. Aes enc[1];
  6308. #endif
  6309. byte cipher[AES_BLOCK_SIZE * 4];
  6310. #ifdef HAVE_AES_DECRYPT
  6311. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6312. Aes *dec = NULL;
  6313. #else
  6314. Aes dec[1];
  6315. #endif
  6316. byte plain [AES_BLOCK_SIZE * 4];
  6317. #endif
  6318. int ret = 0;
  6319. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6320. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6321. ERROR_OUT(-1, out);
  6322. #ifdef HAVE_AES_DECRYPT
  6323. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6324. ERROR_OUT(-1, out);
  6325. #endif
  6326. #endif
  6327. XMEMSET(enc, 0, sizeof *enc);
  6328. #ifdef HAVE_AES_DECRYPT
  6329. XMEMSET(dec, 0, sizeof *dec);
  6330. #endif
  6331. #ifdef WOLFSSL_AES_128
  6332. /* 128 key size test */
  6333. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6334. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6335. ret = EVP_test(EVP_aes_128_ofb(), key2, iv2, plain2, sizeof(plain2),
  6336. cipher2, sizeof(cipher2));
  6337. if (ret != 0) {
  6338. goto out;
  6339. }
  6340. #endif
  6341. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  6342. if (ret != 0)
  6343. ERROR_OUT(-5129, out);
  6344. #ifdef HAVE_AES_DECRYPT
  6345. /* decrypt uses AES_ENCRYPTION */
  6346. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  6347. if (ret != 0)
  6348. ERROR_OUT(-5130, out);
  6349. #endif
  6350. XMEMSET(cipher, 0, sizeof(cipher));
  6351. ret = wc_AesOfbEncrypt(enc, cipher, plain2, AES_BLOCK_SIZE);
  6352. if (ret != 0)
  6353. ERROR_OUT(-5131, out);
  6354. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE))
  6355. ERROR_OUT(-5132, out);
  6356. #ifdef HAVE_AES_DECRYPT
  6357. ret = wc_AesOfbDecrypt(dec, plain, cipher2, AES_BLOCK_SIZE);
  6358. if (ret != 0)
  6359. ERROR_OUT(-5133, out);
  6360. if (XMEMCMP(plain, plain2, AES_BLOCK_SIZE))
  6361. ERROR_OUT(-5134, out);
  6362. #endif /* HAVE_AES_DECRYPT */
  6363. #endif /* WOLFSSL_AES_128 */
  6364. #ifdef WOLFSSL_AES_192
  6365. /* 192 key size test */
  6366. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6367. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6368. ret = EVP_test(EVP_aes_192_ofb(), key3, iv3, plain3, sizeof(plain3),
  6369. cipher3, sizeof(cipher3));
  6370. if (ret != 0) {
  6371. goto out;
  6372. }
  6373. #endif
  6374. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  6375. if (ret != 0)
  6376. ERROR_OUT(-5135, out);
  6377. #ifdef HAVE_AES_DECRYPT
  6378. /* decrypt uses AES_ENCRYPTION */
  6379. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  6380. if (ret != 0)
  6381. ERROR_OUT(-5136, out);
  6382. #endif
  6383. XMEMSET(cipher, 0, sizeof(cipher));
  6384. ret = wc_AesOfbEncrypt(enc, cipher, plain3, AES_BLOCK_SIZE);
  6385. if (ret != 0)
  6386. ERROR_OUT(-5137, out);
  6387. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE))
  6388. ERROR_OUT(-5138, out);
  6389. #ifdef HAVE_AES_DECRYPT
  6390. ret = wc_AesOfbDecrypt(dec, plain, cipher3, AES_BLOCK_SIZE);
  6391. if (ret != 0)
  6392. ERROR_OUT(-5139, out);
  6393. if (XMEMCMP(plain, plain3, AES_BLOCK_SIZE))
  6394. ERROR_OUT(-5140, out);
  6395. #endif /* HAVE_AES_DECRYPT */
  6396. #endif /* WOLFSSL_AES_192 */
  6397. #ifdef WOLFSSL_AES_256
  6398. /* 256 key size test */
  6399. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6400. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6401. ret = EVP_test(EVP_aes_256_ofb(), key1, iv1, plain1, sizeof(plain1),
  6402. cipher1, sizeof(cipher1));
  6403. if (ret != 0) {
  6404. goto out;
  6405. }
  6406. #endif
  6407. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6408. if (ret != 0)
  6409. ERROR_OUT(-5141, out);
  6410. #ifdef HAVE_AES_DECRYPT
  6411. /* decrypt uses AES_ENCRYPTION */
  6412. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6413. if (ret != 0)
  6414. ERROR_OUT(-5142, out);
  6415. #endif
  6416. XMEMSET(cipher, 0, sizeof(cipher));
  6417. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE);
  6418. if (ret != 0)
  6419. ERROR_OUT(-5143, out);
  6420. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE))
  6421. ERROR_OUT(-5144, out);
  6422. ret = wc_AesOfbEncrypt(enc, cipher + AES_BLOCK_SIZE,
  6423. plain1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  6424. if (ret != 0)
  6425. ERROR_OUT(-5145, out);
  6426. if (XMEMCMP(cipher + AES_BLOCK_SIZE, cipher1 + AES_BLOCK_SIZE,
  6427. AES_BLOCK_SIZE))
  6428. ERROR_OUT(-5146, out);
  6429. #ifdef HAVE_AES_DECRYPT
  6430. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE);
  6431. if (ret != 0)
  6432. ERROR_OUT(-5147, out);
  6433. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE))
  6434. ERROR_OUT(-5148, out);
  6435. ret = wc_AesOfbDecrypt(dec, plain + AES_BLOCK_SIZE,
  6436. cipher1 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  6437. if (ret != 0)
  6438. ERROR_OUT(-5149, out);
  6439. if (XMEMCMP(plain + AES_BLOCK_SIZE, plain1 + AES_BLOCK_SIZE,
  6440. AES_BLOCK_SIZE))
  6441. ERROR_OUT(-5150, out);
  6442. #endif /* HAVE_AES_DECRYPT */
  6443. /* multiple blocks at once */
  6444. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6445. if (ret != 0)
  6446. ERROR_OUT(-5151, out);
  6447. #ifdef HAVE_AES_DECRYPT
  6448. /* decrypt uses AES_ENCRYPTION */
  6449. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6450. if (ret != 0)
  6451. ERROR_OUT(-5152, out);
  6452. #endif
  6453. XMEMSET(cipher, 0, sizeof(cipher));
  6454. ret = wc_AesOfbEncrypt(enc, cipher, plain1, AES_BLOCK_SIZE * 3);
  6455. if (ret != 0)
  6456. ERROR_OUT(-5153, out);
  6457. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 3))
  6458. ERROR_OUT(-5154, out);
  6459. #ifdef HAVE_AES_DECRYPT
  6460. ret = wc_AesOfbDecrypt(dec, plain, cipher1, AES_BLOCK_SIZE * 3);
  6461. if (ret != 0)
  6462. ERROR_OUT(-5155, out);
  6463. if (XMEMCMP(plain, plain1, AES_BLOCK_SIZE * 3))
  6464. ERROR_OUT(-5156, out);
  6465. #endif /* HAVE_AES_DECRYPT */
  6466. /* inline decrypt/encrypt*/
  6467. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6468. if (ret != 0)
  6469. ERROR_OUT(-5157, out);
  6470. #ifdef HAVE_AES_DECRYPT
  6471. /* decrypt uses AES_ENCRYPTION */
  6472. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6473. if (ret != 0)
  6474. ERROR_OUT(-5158, out);
  6475. #endif
  6476. XMEMCPY(cipher, plain1, AES_BLOCK_SIZE * 2);
  6477. ret = wc_AesOfbEncrypt(enc, cipher, cipher, AES_BLOCK_SIZE * 2);
  6478. if (ret != 0)
  6479. ERROR_OUT(-5159, out);
  6480. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  6481. ERROR_OUT(-5160, out);
  6482. #ifdef HAVE_AES_DECRYPT
  6483. ret = wc_AesOfbDecrypt(dec, cipher, cipher, AES_BLOCK_SIZE * 2);
  6484. if (ret != 0)
  6485. ERROR_OUT(-5161, out);
  6486. if (XMEMCMP(cipher, plain1, AES_BLOCK_SIZE * 2))
  6487. ERROR_OUT(-5162, out);
  6488. #endif /* HAVE_AES_DECRYPT */
  6489. /* 256 key size test leftover support */
  6490. ret = wc_AesSetKey(enc, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6491. if (ret != 0)
  6492. ERROR_OUT(-5163, out);
  6493. #ifdef HAVE_AES_DECRYPT
  6494. /* decrypt uses AES_ENCRYPTION */
  6495. ret = wc_AesSetKey(dec, key1, sizeof(key1), iv1, AES_ENCRYPTION);
  6496. if (ret != 0)
  6497. ERROR_OUT(-5164, out);
  6498. #endif
  6499. XMEMSET(cipher, 0, sizeof(cipher));
  6500. ret = wc_AesOfbEncrypt(enc, cipher, plain1, 3);
  6501. if (ret != 0)
  6502. ERROR_OUT(-5165, out);
  6503. if (XMEMCMP(cipher, cipher1, 3))
  6504. ERROR_OUT(-5166, out);
  6505. ret = wc_AesOfbEncrypt(enc, cipher + 3, plain1 + 3, AES_BLOCK_SIZE);
  6506. if (ret != 0)
  6507. ERROR_OUT(-5167, out);
  6508. if (XMEMCMP(cipher + 3, cipher1 + 3, AES_BLOCK_SIZE))
  6509. ERROR_OUT(-5168, out);
  6510. #ifdef HAVE_AES_DECRYPT
  6511. ret = wc_AesOfbDecrypt(dec, plain, cipher1, 6);
  6512. if (ret != 0)
  6513. ERROR_OUT(-5169, out);
  6514. if (XMEMCMP(plain, plain1, 6))
  6515. ERROR_OUT(-5170, out);
  6516. ret = wc_AesOfbDecrypt(dec, plain + 6, cipher1 + 6, AES_BLOCK_SIZE);
  6517. if (ret != 0)
  6518. ERROR_OUT(-5171, out);
  6519. if (XMEMCMP(plain + 6, plain1 + 6, AES_BLOCK_SIZE))
  6520. ERROR_OUT(-5172, out);
  6521. #endif /* HAVE_AES_DECRYPT */
  6522. out:
  6523. wc_AesFree(enc);
  6524. wc_AesFree(dec);
  6525. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6526. if (enc)
  6527. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  6528. #ifdef HAVE_AES_DECRYPT
  6529. if (dec)
  6530. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  6531. #endif
  6532. #endif
  6533. #endif /* WOLFSSL_AES_256 */
  6534. return ret;
  6535. }
  6536. #endif /* WOLFSSL_AES_OFB */
  6537. #if defined(WOLFSSL_AES_CFB)
  6538. /* Test cases from NIST SP 800-38A, Recommendation for Block Cipher Modes of Operation Methods an*/
  6539. static int aescfb_test(void)
  6540. {
  6541. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6542. Aes *enc = NULL;
  6543. #else
  6544. Aes enc[1];
  6545. #endif
  6546. int enc_inited = 0;
  6547. byte cipher[AES_BLOCK_SIZE * 4];
  6548. #ifdef HAVE_AES_DECRYPT
  6549. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6550. Aes *dec = NULL;
  6551. #else
  6552. Aes dec[1];
  6553. #endif
  6554. int dec_inited = 0;
  6555. byte plain [AES_BLOCK_SIZE * 4];
  6556. #endif
  6557. int ret = 0;
  6558. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  6559. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  6560. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  6561. };
  6562. #ifdef WOLFSSL_AES_128
  6563. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6564. {
  6565. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  6566. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  6567. };
  6568. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  6569. {
  6570. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  6571. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  6572. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  6573. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b,
  6574. 0x26,0x75,0x1f,0x67,0xa3,0xcb,0xb1,0x40,
  6575. 0xb1,0x80,0x8c,0xf1,0x87,0xa4,0xf4,0xdf
  6576. };
  6577. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  6578. {
  6579. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  6580. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  6581. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  6582. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  6583. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  6584. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef
  6585. };
  6586. #endif /* WOLFSSL_AES_128 */
  6587. #ifdef WOLFSSL_AES_192
  6588. /* 192 size key test */
  6589. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6590. {
  6591. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  6592. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  6593. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  6594. };
  6595. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  6596. {
  6597. 0xcd,0xc8,0x0d,0x6f,0xdd,0xf1,0x8c,0xab,
  6598. 0x34,0xc2,0x59,0x09,0xc9,0x9a,0x41,0x74,
  6599. 0x67,0xce,0x7f,0x7f,0x81,0x17,0x36,0x21,
  6600. 0x96,0x1a,0x2b,0x70,0x17,0x1d,0x3d,0x7a,
  6601. 0x2e,0x1e,0x8a,0x1d,0xd5,0x9b,0x88,0xb1,
  6602. 0xc8,0xe6,0x0f,0xed,0x1e,0xfa,0xc4,0xc9,
  6603. 0xc0,0x5f,0x9f,0x9c,0xa9,0x83,0x4f,0xa0,
  6604. 0x42,0xae,0x8f,0xba,0x58,0x4b,0x09,0xff
  6605. };
  6606. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  6607. {
  6608. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  6609. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  6610. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  6611. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  6612. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  6613. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  6614. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  6615. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  6616. };
  6617. #endif /* WOLFSSL_AES_192 */
  6618. #ifdef WOLFSSL_AES_256
  6619. /* 256 size key simple test */
  6620. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6621. {
  6622. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  6623. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  6624. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  6625. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  6626. };
  6627. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  6628. {
  6629. 0xdc,0x7e,0x84,0xbf,0xda,0x79,0x16,0x4b,
  6630. 0x7e,0xcd,0x84,0x86,0x98,0x5d,0x38,0x60,
  6631. 0x39,0xff,0xed,0x14,0x3b,0x28,0xb1,0xc8,
  6632. 0x32,0x11,0x3c,0x63,0x31,0xe5,0x40,0x7b,
  6633. 0xdf,0x10,0x13,0x24,0x15,0xe5,0x4b,0x92,
  6634. 0xa1,0x3e,0xd0,0xa8,0x26,0x7a,0xe2,0xf9,
  6635. 0x75,0xa3,0x85,0x74,0x1a,0xb9,0xce,0xf8,
  6636. 0x20,0x31,0x62,0x3d,0x55,0xb1,0xe4,0x71
  6637. };
  6638. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  6639. {
  6640. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  6641. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  6642. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  6643. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  6644. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  6645. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  6646. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  6647. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  6648. };
  6649. #endif /* WOLFSSL_AES_256 */
  6650. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6651. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6652. ERROR_OUT(-1, out);
  6653. #ifdef HAVE_AES_DECRYPT
  6654. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6655. ERROR_OUT(-1, out);
  6656. #endif
  6657. #endif
  6658. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  6659. ERROR_OUT(-5173, out);
  6660. else
  6661. enc_inited = 1;
  6662. #ifdef HAVE_AES_DECRYPT
  6663. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  6664. ERROR_OUT(-5174, out);
  6665. else
  6666. dec_inited = 1;
  6667. #endif
  6668. #ifdef WOLFSSL_AES_128
  6669. /* 128 key tests */
  6670. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6671. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6672. ret = EVP_test(EVP_aes_128_cfb128(), key1, iv, msg1, sizeof(msg1),
  6673. cipher1, sizeof(cipher1));
  6674. if (ret != 0) {
  6675. return ret;
  6676. }
  6677. #endif
  6678. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6679. if (ret != 0)
  6680. ERROR_OUT(-5175, out);
  6681. #ifdef HAVE_AES_DECRYPT
  6682. /* decrypt uses AES_ENCRYPTION */
  6683. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6684. if (ret != 0)
  6685. ERROR_OUT(-5176, out);
  6686. #endif
  6687. XMEMSET(cipher, 0, sizeof(cipher));
  6688. ret = wc_AesCfbEncrypt(enc, cipher, msg1, AES_BLOCK_SIZE * 2);
  6689. if (ret != 0)
  6690. ERROR_OUT(-5177, out);
  6691. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  6692. ERROR_OUT(-5178, out);
  6693. /* test restarting encryption process */
  6694. ret = wc_AesCfbEncrypt(enc, cipher + (AES_BLOCK_SIZE * 2),
  6695. msg1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE);
  6696. if (ret != 0)
  6697. ERROR_OUT(-5179, out);
  6698. if (XMEMCMP(cipher + (AES_BLOCK_SIZE * 2),
  6699. cipher1 + (AES_BLOCK_SIZE * 2), AES_BLOCK_SIZE))
  6700. ERROR_OUT(-5180, out);
  6701. #ifdef HAVE_AES_DECRYPT
  6702. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 3);
  6703. if (ret != 0)
  6704. ERROR_OUT(-5181, out);
  6705. if (XMEMCMP(plain, msg1, AES_BLOCK_SIZE * 3))
  6706. ERROR_OUT(-5182, out);
  6707. #endif /* HAVE_AES_DECRYPT */
  6708. #endif /* WOLFSSL_AES_128 */
  6709. #ifdef WOLFSSL_AES_192
  6710. /* 192 key size test */
  6711. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6712. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6713. ret = EVP_test(EVP_aes_192_cfb128(), key2, iv, msg2, sizeof(msg2),
  6714. cipher2, sizeof(cipher2));
  6715. if (ret != 0) {
  6716. return ret;
  6717. }
  6718. #endif
  6719. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv, AES_ENCRYPTION);
  6720. if (ret != 0)
  6721. ERROR_OUT(-5183, out);
  6722. #ifdef HAVE_AES_DECRYPT
  6723. /* decrypt uses AES_ENCRYPTION */
  6724. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv, AES_ENCRYPTION);
  6725. if (ret != 0)
  6726. ERROR_OUT(-5184, out);
  6727. #endif
  6728. XMEMSET(cipher, 0, sizeof(cipher));
  6729. ret = wc_AesCfbEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE * 4);
  6730. if (ret != 0)
  6731. ERROR_OUT(-5185, out);
  6732. if (XMEMCMP(cipher, cipher2, AES_BLOCK_SIZE * 4))
  6733. ERROR_OUT(-5186, out);
  6734. #ifdef HAVE_AES_DECRYPT
  6735. ret = wc_AesCfbDecrypt(dec, plain, cipher, AES_BLOCK_SIZE * 4);
  6736. if (ret != 0)
  6737. ERROR_OUT(-5187, out);
  6738. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE * 4))
  6739. ERROR_OUT(-5188, out);
  6740. #endif /* HAVE_AES_DECRYPT */
  6741. #endif /* WOLFSSL_AES_192 */
  6742. #ifdef WOLFSSL_AES_256
  6743. /* 256 key size test */
  6744. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  6745. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6746. ret = EVP_test(EVP_aes_256_cfb128(), key3, iv, msg3, sizeof(msg3),
  6747. cipher3, sizeof(cipher3));
  6748. if (ret != 0) {
  6749. return ret;
  6750. }
  6751. #endif
  6752. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv, AES_ENCRYPTION);
  6753. if (ret != 0)
  6754. ERROR_OUT(-5189, out);
  6755. #ifdef HAVE_AES_DECRYPT
  6756. /* decrypt uses AES_ENCRYPTION */
  6757. ret = wc_AesSetKey(dec, key3, sizeof(key3), iv, AES_ENCRYPTION);
  6758. if (ret != 0)
  6759. ERROR_OUT(-5190, out);
  6760. #endif
  6761. /* test with data left overs, magic lengths are checking near edges */
  6762. XMEMSET(cipher, 0, sizeof(cipher));
  6763. ret = wc_AesCfbEncrypt(enc, cipher, msg3, 4);
  6764. if (ret != 0)
  6765. ERROR_OUT(-5191, out);
  6766. if (XMEMCMP(cipher, cipher3, 4))
  6767. ERROR_OUT(-5192, out);
  6768. ret = wc_AesCfbEncrypt(enc, cipher + 4, msg3 + 4, 27);
  6769. if (ret != 0)
  6770. ERROR_OUT(-5193, out);
  6771. if (XMEMCMP(cipher + 4, cipher3 + 4, 27))
  6772. ERROR_OUT(-5194, out);
  6773. ret = wc_AesCfbEncrypt(enc, cipher + 31, msg3 + 31,
  6774. (AES_BLOCK_SIZE * 4) - 31);
  6775. if (ret != 0)
  6776. ERROR_OUT(-5195, out);
  6777. if (XMEMCMP(cipher, cipher3, AES_BLOCK_SIZE * 4))
  6778. ERROR_OUT(-5196, out);
  6779. #ifdef HAVE_AES_DECRYPT
  6780. ret = wc_AesCfbDecrypt(dec, plain, cipher, 4);
  6781. if (ret != 0)
  6782. ERROR_OUT(-5197, out);
  6783. if (XMEMCMP(plain, msg3, 4))
  6784. ERROR_OUT(-5198, out);
  6785. ret = wc_AesCfbDecrypt(dec, plain + 4, cipher + 4, 4);
  6786. if (ret != 0)
  6787. ERROR_OUT(-5199, out);
  6788. ret = wc_AesCfbDecrypt(dec, plain + 8, cipher + 8, 23);
  6789. if (ret != 0)
  6790. ERROR_OUT(-5200, out);
  6791. if (XMEMCMP(plain + 4, msg3 + 4, 27))
  6792. ERROR_OUT(-5201, out);
  6793. ret = wc_AesCfbDecrypt(dec, plain + 31, cipher + 31,
  6794. (AES_BLOCK_SIZE * 4) - 31);
  6795. if (ret != 0)
  6796. ERROR_OUT(-5202, out);
  6797. if (XMEMCMP(plain, msg3, AES_BLOCK_SIZE * 4))
  6798. ERROR_OUT(-5203, out);
  6799. #endif /* HAVE_AES_DECRYPT */
  6800. #endif /* WOLFSSL_AES_256 */
  6801. out:
  6802. if (enc_inited)
  6803. wc_AesFree(enc);
  6804. if (dec_inited)
  6805. wc_AesFree(dec);
  6806. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6807. if (enc)
  6808. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  6809. #ifdef HAVE_AES_DECRYPT
  6810. if (dec)
  6811. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  6812. #endif
  6813. #endif
  6814. return ret;
  6815. }
  6816. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6817. static int aescfb1_test(void)
  6818. {
  6819. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6820. Aes *enc = NULL;
  6821. #else
  6822. Aes enc[1];
  6823. #endif
  6824. int enc_inited = 0;
  6825. byte cipher[AES_BLOCK_SIZE];
  6826. #ifdef HAVE_AES_DECRYPT
  6827. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6828. Aes *dec = NULL;
  6829. #else
  6830. Aes dec[1];
  6831. #endif
  6832. int dec_inited = 0;
  6833. byte plain [AES_BLOCK_SIZE];
  6834. #endif
  6835. int ret = 0;
  6836. #ifdef WOLFSSL_AES_128
  6837. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  6838. 0x4d,0xbb,0xdc,0xaa,0x59,0xf3,0x63,0xc9,
  6839. 0x2a,0x3b,0x98,0x43,0xad,0x20,0xe2,0xb7
  6840. };
  6841. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  6842. {
  6843. 0xcd,0xef,0x9d,0x06,0x61,0xba,0xe4,0x73,
  6844. 0x8d,0x1a,0x58,0xa2,0xa6,0x22,0x8b,0x66
  6845. };
  6846. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  6847. {
  6848. 0x00
  6849. };
  6850. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  6851. {
  6852. 0xC0
  6853. };
  6854. #endif /* WOLFSSL_AES_128 */
  6855. #ifdef WOLFSSL_AES_192
  6856. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  6857. 0x57,0xc6,0x89,0x7c,0x99,0x52,0x28,0x13,
  6858. 0xbf,0x67,0x9c,0xe1,0x13,0x70,0xaf,0x5e
  6859. };
  6860. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  6861. {
  6862. 0xba,0xa1,0x58,0xa1,0x6b,0x50,0x4a,0x10,
  6863. 0x8e,0xd4,0x33,0x2e,0xe7,0xf2,0x9b,0xf6,
  6864. 0xd1,0xac,0x46,0xa8,0xde,0x5a,0xfe,0x7a
  6865. };
  6866. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  6867. {
  6868. 0x30
  6869. };
  6870. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  6871. {
  6872. 0x80
  6873. };
  6874. #endif /* WOLFSSL_AES_192 */
  6875. #ifdef WOLFSSL_AES_256
  6876. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  6877. 0x63,0x2e,0x9f,0x83,0x1f,0xa3,0x80,0x5e,
  6878. 0x52,0x02,0xbc,0xe0,0x6d,0x04,0xf9,0xa0
  6879. };
  6880. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  6881. {
  6882. 0xf6,0xfa,0xe4,0xf1,0x5d,0x91,0xfc,0x50,
  6883. 0x88,0x78,0x4f,0x84,0xa5,0x37,0x12,0x7e,
  6884. 0x32,0x63,0x55,0x9c,0x62,0x73,0x88,0x20,
  6885. 0xc2,0xcf,0x3d,0xe1,0x1c,0x2a,0x30,0x40
  6886. };
  6887. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  6888. {
  6889. 0xF7, 0x00
  6890. };
  6891. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  6892. {
  6893. 0x41, 0xC0
  6894. };
  6895. #endif /* WOLFSSL_AES_256 */
  6896. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  6897. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6898. ERROR_OUT(-1, out);
  6899. #ifdef HAVE_AES_DECRYPT
  6900. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  6901. ERROR_OUT(-1, out);
  6902. #endif
  6903. #endif
  6904. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  6905. ERROR_OUT(-5204, out);
  6906. else
  6907. enc_inited = 1;
  6908. #ifdef HAVE_AES_DECRYPT
  6909. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  6910. ERROR_OUT(-5205, out);
  6911. else
  6912. dec_inited = 1;
  6913. #endif
  6914. #ifdef WOLFSSL_AES_128
  6915. /* 128 key tests */
  6916. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6917. if (ret != 0)
  6918. ERROR_OUT(-5206, out);
  6919. #ifdef HAVE_AES_DECRYPT
  6920. /* decrypt uses AES_ENCRYPTION */
  6921. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6922. if (ret != 0)
  6923. ERROR_OUT(-5207, out);
  6924. #endif
  6925. XMEMSET(cipher, 0, sizeof(cipher));
  6926. ret = wc_AesCfb1Encrypt(enc, cipher, msg1, 2);
  6927. if (ret != 0)
  6928. ERROR_OUT(-5208, out);
  6929. if (cipher[0] != cipher1[0])
  6930. ERROR_OUT(-5209, out);
  6931. #ifdef HAVE_AES_DECRYPT
  6932. ret = wc_AesCfb1Decrypt(dec, plain, cipher, 2);
  6933. if (ret != 0)
  6934. ERROR_OUT(-5210, out);
  6935. if (plain[0] != msg1[0])
  6936. ERROR_OUT(-5211, out);
  6937. #endif /* HAVE_AES_DECRYPT */
  6938. #ifdef OPENSSL_EXTRA
  6939. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  6940. if (ret != 0)
  6941. ERROR_OUT(-5212, out);
  6942. XMEMSET(cipher, 0, sizeof(cipher));
  6943. ret = wc_AesCfb1Encrypt(enc, cipher, msg1,
  6944. sizeof(msg1) * WOLFSSL_BIT_SIZE);
  6945. if (ret != 0)
  6946. ERROR_OUT(-5213, out);
  6947. #ifndef WOLFCRYPT_ONLY
  6948. ret = EVP_test(EVP_aes_128_cfb1(), key1, iv, msg1, sizeof(msg1),
  6949. cipher, sizeof(msg1));
  6950. if (ret != 0) {
  6951. goto out;
  6952. }
  6953. #endif
  6954. #endif
  6955. #endif /* WOLFSSL_AES_128 */
  6956. #ifdef WOLFSSL_AES_192
  6957. /* 192 key tests */
  6958. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  6959. if (ret != 0)
  6960. ERROR_OUT(-5214, out);
  6961. XMEMSET(cipher, 0, sizeof(cipher));
  6962. ret = wc_AesCfb1Encrypt(enc, cipher, msg2, 4);
  6963. if (ret != 0)
  6964. ERROR_OUT(-5215, out);
  6965. if (XMEMCMP(cipher, cipher2, sizeof(cipher2)) != 0)
  6966. ERROR_OUT(-5216, out);
  6967. #ifdef OPENSSL_EXTRA
  6968. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  6969. if (ret != 0)
  6970. ERROR_OUT(-5217, out);
  6971. XMEMSET(cipher, 0, sizeof(cipher));
  6972. ret = wc_AesCfb1Encrypt(enc, cipher, msg2,
  6973. sizeof(msg2) * WOLFSSL_BIT_SIZE);
  6974. if (ret != 0)
  6975. ERROR_OUT(-5218, out);
  6976. #ifndef WOLFCRYPT_ONLY
  6977. ret = EVP_test(EVP_aes_192_cfb1(), key2, iv2, msg2, sizeof(msg2),
  6978. cipher, sizeof(msg2));
  6979. if (ret != 0) {
  6980. goto out;
  6981. }
  6982. #endif
  6983. #endif
  6984. #endif /* WOLFSSL_AES_192 */
  6985. #ifdef WOLFSSL_AES_256
  6986. /* 256 key tests */
  6987. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  6988. if (ret != 0)
  6989. ERROR_OUT(-5219, out);
  6990. XMEMSET(cipher, 0, sizeof(cipher));
  6991. ret = wc_AesCfb1Encrypt(enc, cipher, msg3, 10);
  6992. if (ret != 0)
  6993. ERROR_OUT(-5220, out);
  6994. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  6995. ERROR_OUT(-5221, out);
  6996. #ifdef OPENSSL_EXTRA
  6997. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  6998. if (ret != 0)
  6999. ERROR_OUT(-5222, out);
  7000. XMEMSET(cipher, 0, sizeof(cipher));
  7001. ret = wc_AesCfb1Encrypt(enc, cipher, msg3,
  7002. sizeof(msg3) * WOLFSSL_BIT_SIZE);
  7003. if (ret != 0)
  7004. ERROR_OUT(-5223, out);
  7005. #ifndef WOLFCRYPT_ONLY
  7006. ret = EVP_test(EVP_aes_256_cfb1(), key3, iv3, msg3, sizeof(msg3),
  7007. cipher, sizeof(msg3));
  7008. if (ret != 0) {
  7009. goto out;
  7010. }
  7011. #endif
  7012. #endif
  7013. out:
  7014. if (enc_inited)
  7015. wc_AesFree(enc);
  7016. #ifdef HAVE_AES_DECRYPT
  7017. if (dec_inited)
  7018. wc_AesFree(dec);
  7019. #endif
  7020. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7021. if (enc)
  7022. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7023. #ifdef HAVE_AES_DECRYPT
  7024. if (dec)
  7025. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7026. #endif
  7027. #endif
  7028. #endif /* WOLFSSL_AES_256 */
  7029. return ret;
  7030. }
  7031. static int aescfb8_test(void)
  7032. {
  7033. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7034. Aes *enc = NULL;
  7035. #else
  7036. Aes enc[1];
  7037. #endif
  7038. int enc_inited = 0;
  7039. byte cipher[AES_BLOCK_SIZE];
  7040. #ifdef HAVE_AES_DECRYPT
  7041. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7042. Aes *dec = NULL;
  7043. #else
  7044. Aes dec[1];
  7045. #endif
  7046. int dec_inited = 0;
  7047. byte plain [AES_BLOCK_SIZE];
  7048. #endif
  7049. int ret = 0;
  7050. #ifdef WOLFSSL_AES_128
  7051. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = {
  7052. 0xf4,0x75,0xc6,0x49,0x91,0xb2,0x0e,0xae,
  7053. 0xe1,0x83,0xa2,0x26,0x29,0xe2,0x1e,0x22
  7054. };
  7055. WOLFSSL_SMALL_STACK_STATIC const byte key1[] =
  7056. {
  7057. 0xc8,0xfe,0x9b,0xf7,0x7b,0x93,0x0f,0x46,
  7058. 0xd2,0x07,0x8b,0x8c,0x0e,0x65,0x7c,0xd4
  7059. };
  7060. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  7061. {
  7062. 0xd2,0x76,0x91
  7063. };
  7064. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] =
  7065. {
  7066. 0xc9,0x06,0x35
  7067. };
  7068. #endif /* WOLFSSL_AES_128 */
  7069. #ifdef WOLFSSL_AES_192
  7070. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  7071. 0x0a,0x02,0x84,0x6b,0x62,0xab,0xb6,0x93,
  7072. 0xef,0x31,0xd7,0x54,0x84,0x2e,0xed,0x29
  7073. };
  7074. WOLFSSL_SMALL_STACK_STATIC const byte key2[] =
  7075. {
  7076. 0xba,0xf0,0x8b,0x76,0x31,0x7a,0x65,0xc5,
  7077. 0xf0,0x7a,0xe6,0xf5,0x7e,0xb0,0xe6,0x54,
  7078. 0x88,0x65,0x93,0x24,0xd2,0x97,0x09,0xe3
  7079. };
  7080. WOLFSSL_SMALL_STACK_STATIC const byte cipher2[] =
  7081. {
  7082. 0x72,0x9c,0x0b,0x6d,0xeb,0x75,0xfa,0x6e,
  7083. 0xb5,0xe8
  7084. };
  7085. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  7086. {
  7087. 0x98,0x95,0x93,0x24,0x02,0x39,0x3d,0xc3,
  7088. 0x3a,0x60
  7089. };
  7090. #endif
  7091. #ifdef WOLFSSL_AES_256
  7092. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] = {
  7093. 0x33,0x8c,0x55,0x2f,0xf1,0xec,0xa1,0x44,
  7094. 0x08,0xe0,0x5d,0x8c,0xf9,0xf3,0xb3,0x1b
  7095. };
  7096. WOLFSSL_SMALL_STACK_STATIC const byte key3[] =
  7097. {
  7098. 0x06,0x48,0x74,0x09,0x2f,0x7a,0x13,0xcc,
  7099. 0x44,0x62,0x24,0x7a,0xd4,0x23,0xd0,0xe9,
  7100. 0x6e,0xdf,0x42,0xe8,0xb6,0x7a,0x5a,0x23,
  7101. 0xb7,0xa0,0xa6,0x47,0x7b,0x09,0x8e,0x66
  7102. };
  7103. WOLFSSL_SMALL_STACK_STATIC const byte cipher3[] =
  7104. {
  7105. 0x1c,0xff,0x95
  7106. };
  7107. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] =
  7108. {
  7109. 0xb9,0x74,0xfa
  7110. };
  7111. #endif
  7112. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7113. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7114. ERROR_OUT(-5238, out);
  7115. #ifdef HAVE_AES_DECRYPT
  7116. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7117. ERROR_OUT(-5239, out);
  7118. #endif
  7119. #endif
  7120. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  7121. ERROR_OUT(-5224, out);
  7122. else
  7123. enc_inited = 1;
  7124. #ifdef HAVE_AES_DECRYPT
  7125. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  7126. ERROR_OUT(-5225, out);
  7127. else
  7128. dec_inited = 1;
  7129. #endif
  7130. #ifdef WOLFSSL_AES_128
  7131. /* 128 key tests */
  7132. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7133. ret = EVP_test(EVP_aes_128_cfb8(), key1, iv, msg1, sizeof(msg1),
  7134. cipher1, sizeof(cipher1));
  7135. if (ret != 0) {
  7136. return ret;
  7137. }
  7138. #endif
  7139. ret = wc_AesSetKey(enc, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7140. if (ret != 0)
  7141. ERROR_OUT(-5226, out);
  7142. #ifdef HAVE_AES_DECRYPT
  7143. /* decrypt uses AES_ENCRYPTION */
  7144. ret = wc_AesSetKey(dec, key1, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  7145. if (ret != 0)
  7146. ERROR_OUT(-5227, out);
  7147. #endif
  7148. XMEMSET(cipher, 0, sizeof(cipher));
  7149. ret = wc_AesCfb8Encrypt(enc, cipher, msg1, sizeof(msg1));
  7150. if (ret != 0)
  7151. ERROR_OUT(-5228, out);
  7152. if (XMEMCMP(cipher, cipher1, sizeof(cipher1)) != 0)
  7153. ERROR_OUT(-5229, out);
  7154. #ifdef HAVE_AES_DECRYPT
  7155. ret = wc_AesCfb8Decrypt(dec, plain, cipher, sizeof(msg1));
  7156. if (ret != 0)
  7157. ERROR_OUT(-5230, out);
  7158. if (XMEMCMP(plain, msg1, sizeof(msg1)) != 0)
  7159. ERROR_OUT(-5231, out);
  7160. #endif /* HAVE_AES_DECRYPT */
  7161. #endif /* WOLFSSL_AES_128 */
  7162. #ifdef WOLFSSL_AES_192
  7163. /* 192 key tests */
  7164. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  7165. if (ret != 0)
  7166. ERROR_OUT(-5232, out);
  7167. XMEMSET(cipher, 0, sizeof(cipher));
  7168. ret = wc_AesCfb8Encrypt(enc, cipher, msg2, sizeof(msg2));
  7169. if (ret != 0)
  7170. ERROR_OUT(-5233, out);
  7171. if (XMEMCMP(cipher, cipher2, sizeof(msg2)) != 0)
  7172. ERROR_OUT(-5234, out);
  7173. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7174. ret = EVP_test(EVP_aes_192_cfb8(), key2, iv2, msg2, sizeof(msg2),
  7175. cipher2, sizeof(msg2));
  7176. if (ret != 0) {
  7177. return ret;
  7178. }
  7179. #endif
  7180. #endif /* WOLFSSL_AES_192 */
  7181. #ifdef WOLFSSL_AES_256
  7182. /* 256 key tests */
  7183. ret = wc_AesSetKey(enc, key3, sizeof(key3), iv3, AES_ENCRYPTION);
  7184. if (ret != 0)
  7185. ERROR_OUT(-5235, out);
  7186. XMEMSET(cipher, 0, sizeof(cipher));
  7187. ret = wc_AesCfb8Encrypt(enc, cipher, msg3, sizeof(msg3));
  7188. if (ret != 0)
  7189. ERROR_OUT(-5236, out);
  7190. if (XMEMCMP(cipher, cipher3, sizeof(cipher3)) != 0)
  7191. ERROR_OUT(-5237, out);
  7192. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  7193. ret = EVP_test(EVP_aes_256_cfb8(), key3, iv3, msg3, sizeof(msg3),
  7194. cipher3, sizeof(msg3));
  7195. if (ret != 0) {
  7196. goto out;
  7197. }
  7198. #endif
  7199. out:
  7200. if (enc_inited)
  7201. wc_AesFree(enc);
  7202. #ifdef HAVE_AES_DECRYPT
  7203. if (dec_inited)
  7204. wc_AesFree(dec);
  7205. #endif
  7206. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7207. if (enc)
  7208. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7209. #ifdef HAVE_AES_DECRYPT
  7210. if (dec)
  7211. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7212. #endif
  7213. #endif
  7214. #endif /* WOLFSSL_AES_256 */
  7215. return ret;
  7216. }
  7217. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  7218. #endif /* WOLFSSL_AES_CFB */
  7219. static int aes_key_size_test(void)
  7220. {
  7221. int ret;
  7222. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7223. Aes *aes;
  7224. #else
  7225. Aes aes[1];
  7226. #endif
  7227. byte key16[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7228. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  7229. #ifndef WOLFSSL_CRYPTOCELL
  7230. byte key24[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7231. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  7232. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37 };
  7233. #endif
  7234. byte key32[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7235. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  7236. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  7237. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  7238. byte iv[] = "1234567890abcdef";
  7239. #ifndef HAVE_FIPS
  7240. word32 keySize;
  7241. #endif
  7242. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7243. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7244. return -5315;
  7245. #endif
  7246. #if !defined(HAVE_FIPS) || \
  7247. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  7248. /* w/ FIPS v1 (cert 2425) wc_AesInit just returns 0 always as it's not
  7249. * supported with that FIPS version */
  7250. ret = wc_AesInit(NULL, HEAP_HINT, devId);
  7251. if (ret != BAD_FUNC_ARG)
  7252. ERROR_OUT(-5300, out);
  7253. #endif
  7254. ret = wc_AesInit(aes, HEAP_HINT, devId);
  7255. /* 0 check OK for FIPSv1 */
  7256. if (ret != 0)
  7257. ERROR_OUT(-5301, out);
  7258. #ifndef HAVE_FIPS
  7259. /* Parameter Validation testing. */
  7260. ret = wc_AesGetKeySize(NULL, NULL);
  7261. if (ret != BAD_FUNC_ARG)
  7262. ERROR_OUT(-5302, out);
  7263. ret = wc_AesGetKeySize(aes, NULL);
  7264. if (ret != BAD_FUNC_ARG)
  7265. ERROR_OUT(-5303, out);
  7266. ret = wc_AesGetKeySize(NULL, &keySize);
  7267. if (ret != BAD_FUNC_ARG)
  7268. ERROR_OUT(-5304, out);
  7269. /* Crashes in FIPS */
  7270. ret = wc_AesSetKey(NULL, key16, sizeof(key16), iv, AES_ENCRYPTION);
  7271. if (ret != BAD_FUNC_ARG)
  7272. ERROR_OUT(-5305, out);
  7273. #endif
  7274. /* NULL IV indicates to use all zeros IV. */
  7275. ret = wc_AesSetKey(aes, key16, sizeof(key16), NULL, AES_ENCRYPTION);
  7276. #ifdef WOLFSSL_AES_128
  7277. if (ret != 0)
  7278. #else
  7279. if (ret != BAD_FUNC_ARG)
  7280. #endif
  7281. ERROR_OUT(-5306, out);
  7282. ret = wc_AesSetKey(aes, key32, sizeof(key32) - 1, iv, AES_ENCRYPTION);
  7283. if (ret != BAD_FUNC_ARG)
  7284. ERROR_OUT(-5307, out);
  7285. /* CryptoCell handles rounds internally */
  7286. #if !defined(HAVE_FIPS) && !defined(WOLFSSL_CRYPTOCELL)
  7287. /* PSA don't use aes->rounds */
  7288. #if !defined(WOLFSSL_HAVE_PSA) || defined(WOLFSSL_PSA_NO_AES)
  7289. /* Force invalid rounds */
  7290. aes->rounds = 16;
  7291. ret = wc_AesGetKeySize(aes, &keySize);
  7292. if (ret != BAD_FUNC_ARG)
  7293. ERROR_OUT(-5308, out);
  7294. #endif
  7295. #endif
  7296. ret = wc_AesSetKey(aes, key16, sizeof(key16), iv, AES_ENCRYPTION);
  7297. #ifdef WOLFSSL_AES_128
  7298. if (ret != 0)
  7299. #else
  7300. if (ret != BAD_FUNC_ARG)
  7301. #endif
  7302. ERROR_OUT(-5309, out);
  7303. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_128)
  7304. ret = wc_AesGetKeySize(aes, &keySize);
  7305. if (ret != 0 || keySize != sizeof(key16))
  7306. ERROR_OUT(-5310, out);
  7307. #endif
  7308. #ifndef WOLFSSL_CRYPTOCELL
  7309. /* Cryptocell only supports AES-128 key size */
  7310. ret = wc_AesSetKey(aes, key24, sizeof(key24), iv, AES_ENCRYPTION);
  7311. #ifdef WOLFSSL_AES_192
  7312. if (ret != 0)
  7313. #else
  7314. if (ret != BAD_FUNC_ARG)
  7315. #endif
  7316. ERROR_OUT(-5311, out);
  7317. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_192)
  7318. ret = wc_AesGetKeySize(aes, &keySize);
  7319. if (ret != 0 || keySize != sizeof(key24))
  7320. ERROR_OUT(-5312, out);
  7321. #endif
  7322. ret = wc_AesSetKey(aes, key32, sizeof(key32), iv, AES_ENCRYPTION);
  7323. #ifdef WOLFSSL_AES_256
  7324. if (ret != 0)
  7325. #else
  7326. if (ret != BAD_FUNC_ARG)
  7327. #endif
  7328. ERROR_OUT(-5313, out);
  7329. #if !defined(HAVE_FIPS) && defined(WOLFSSL_AES_256)
  7330. ret = wc_AesGetKeySize(aes, &keySize);
  7331. if (ret != 0 || keySize != sizeof(key32))
  7332. ERROR_OUT(-5314, out);
  7333. #endif
  7334. #endif /* !WOLFSSL_CRYPTOCELL */
  7335. ret = 0; /* success */
  7336. out:
  7337. wc_AesFree(aes);
  7338. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7339. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7340. #endif
  7341. return ret;
  7342. }
  7343. #if defined(WOLFSSL_AES_XTS)
  7344. /* test vectors from http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html */
  7345. #ifdef WOLFSSL_AES_128
  7346. static int aes_xts_128_test(void)
  7347. {
  7348. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7349. XtsAes *aes = NULL;
  7350. #else
  7351. XtsAes aes[1];
  7352. #endif
  7353. int aes_inited = 0;
  7354. int ret = 0;
  7355. unsigned char buf[AES_BLOCK_SIZE * 2];
  7356. unsigned char cipher[AES_BLOCK_SIZE * 2];
  7357. /* 128 key tests */
  7358. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7359. 0xa1, 0xb9, 0x0c, 0xba, 0x3f, 0x06, 0xac, 0x35,
  7360. 0x3b, 0x2c, 0x34, 0x38, 0x76, 0x08, 0x17, 0x62,
  7361. 0x09, 0x09, 0x23, 0x02, 0x6e, 0x91, 0x77, 0x18,
  7362. 0x15, 0xf2, 0x9d, 0xab, 0x01, 0x93, 0x2f, 0x2f
  7363. };
  7364. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  7365. 0x4f, 0xae, 0xf7, 0x11, 0x7c, 0xda, 0x59, 0xc6,
  7366. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  7367. };
  7368. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7369. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  7370. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c
  7371. };
  7372. /* plain text test of partial block is not from NIST test vector list */
  7373. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  7374. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  7375. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  7376. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  7377. };
  7378. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7379. 0x77, 0x8a, 0xe8, 0xb4, 0x3c, 0xb9, 0x8d, 0x5a,
  7380. 0x82, 0x50, 0x81, 0xd5, 0xbe, 0x47, 0x1c, 0x63
  7381. };
  7382. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  7383. 0x39, 0x25, 0x79, 0x05, 0xdf, 0xcc, 0x77, 0x76,
  7384. 0x6c, 0x87, 0x0a, 0x80, 0x6a, 0x60, 0xe3, 0xc0,
  7385. 0x93, 0xd1, 0x2a, 0xcf, 0xcb, 0x51, 0x42, 0xfa,
  7386. 0x09, 0x69, 0x89, 0x62, 0x5b, 0x60, 0xdb, 0x16
  7387. };
  7388. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  7389. 0x5c, 0xf7, 0x9d, 0xb6, 0xc5, 0xcd, 0x99, 0x1a,
  7390. 0x1c, 0x78, 0x81, 0x42, 0x24, 0x95, 0x1e, 0x84
  7391. };
  7392. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  7393. 0xbd, 0xc5, 0x46, 0x8f, 0xbc, 0x8d, 0x50, 0xa1,
  7394. 0x0d, 0x1c, 0x85, 0x7f, 0x79, 0x1c, 0x5c, 0xba,
  7395. 0xb3, 0x81, 0x0d, 0x0d, 0x73, 0xcf, 0x8f, 0x20,
  7396. 0x46, 0xb1, 0xd1, 0x9e, 0x7d, 0x5d, 0x8a, 0x56
  7397. };
  7398. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  7399. 0xd6, 0xbe, 0x04, 0x6d, 0x41, 0xf2, 0x3b, 0x5e,
  7400. 0xd7, 0x0b, 0x6b, 0x3d, 0x5c, 0x8e, 0x66, 0x23,
  7401. 0x2b, 0xe6, 0xb8, 0x07, 0xd4, 0xdc, 0xc6, 0x0e,
  7402. 0xff, 0x8d, 0xbc, 0x1d, 0x9f, 0x7f, 0xc8, 0x22
  7403. };
  7404. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7405. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7406. ERROR_OUT(-5417, out);
  7407. #endif
  7408. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7409. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7410. ret = EVP_test(EVP_aes_128_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  7411. if (ret != 0) {
  7412. printf("EVP_aes_128_xts failed!\n");
  7413. goto out;
  7414. }
  7415. #endif
  7416. XMEMSET(buf, 0, sizeof(buf));
  7417. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  7418. HEAP_HINT, devId) != 0)
  7419. ERROR_OUT(-5400, out);
  7420. else
  7421. aes_inited = 1;
  7422. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  7423. #if defined(WOLFSSL_ASYNC_CRYPT)
  7424. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7425. #endif
  7426. if (ret != 0)
  7427. ERROR_OUT(-5401, out);
  7428. if (XMEMCMP(c2, buf, sizeof(c2)))
  7429. ERROR_OUT(-5402, out);
  7430. XMEMSET(buf, 0, sizeof(buf));
  7431. wc_AesXtsFree(aes);
  7432. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7433. HEAP_HINT, devId) != 0)
  7434. ERROR_OUT(-5403, out);
  7435. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  7436. #if defined(WOLFSSL_ASYNC_CRYPT)
  7437. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7438. #endif
  7439. if (ret != 0)
  7440. ERROR_OUT(-5404, out);
  7441. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  7442. ERROR_OUT(-5405, out);
  7443. /* partial block encryption test */
  7444. XMEMSET(cipher, 0, sizeof(cipher));
  7445. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  7446. #if defined(WOLFSSL_ASYNC_CRYPT)
  7447. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7448. #endif
  7449. if (ret != 0)
  7450. ERROR_OUT(-5406, out);
  7451. wc_AesXtsFree(aes);
  7452. /* partial block decrypt test */
  7453. XMEMSET(buf, 0, sizeof(buf));
  7454. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7455. HEAP_HINT, devId) != 0)
  7456. ERROR_OUT(-5407, out);
  7457. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  7458. #if defined(WOLFSSL_ASYNC_CRYPT)
  7459. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7460. #endif
  7461. if (ret != 0)
  7462. ERROR_OUT(-5408, out);
  7463. if (XMEMCMP(pp, buf, sizeof(pp)))
  7464. ERROR_OUT(-5409, out);
  7465. /* NIST decrypt test vector */
  7466. XMEMSET(buf, 0, sizeof(buf));
  7467. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  7468. #if defined(WOLFSSL_ASYNC_CRYPT)
  7469. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7470. #endif
  7471. if (ret != 0)
  7472. ERROR_OUT(-5410, out);
  7473. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  7474. ERROR_OUT(-5411, out);
  7475. /* fail case with decrypting using wrong key */
  7476. XMEMSET(buf, 0, sizeof(buf));
  7477. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  7478. #if defined(WOLFSSL_ASYNC_CRYPT)
  7479. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7480. #endif
  7481. if (ret != 0)
  7482. ERROR_OUT(-5412, out);
  7483. if (XMEMCMP(p2, buf, sizeof(p2)) == 0) /* fail case with wrong key */
  7484. ERROR_OUT(-5413, out);
  7485. wc_AesXtsFree(aes);
  7486. /* set correct key and retest */
  7487. XMEMSET(buf, 0, sizeof(buf));
  7488. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  7489. HEAP_HINT, devId) != 0)
  7490. ERROR_OUT(-5414, out);
  7491. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  7492. #if defined(WOLFSSL_ASYNC_CRYPT)
  7493. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7494. #endif
  7495. if (ret != 0)
  7496. ERROR_OUT(-5415, out);
  7497. if (XMEMCMP(p2, buf, sizeof(p2)))
  7498. ERROR_OUT(-5416, out);
  7499. out:
  7500. if (aes_inited)
  7501. wc_AesXtsFree(aes);
  7502. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7503. if (aes)
  7504. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7505. #endif
  7506. return ret;
  7507. }
  7508. #endif /* WOLFSSL_AES_128 */
  7509. #ifdef WOLFSSL_AES_256
  7510. static int aes_xts_256_test(void)
  7511. {
  7512. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7513. XtsAes *aes = NULL;
  7514. #else
  7515. XtsAes aes[1];
  7516. #endif
  7517. int aes_inited = 0;
  7518. int ret = 0;
  7519. unsigned char buf[AES_BLOCK_SIZE * 3];
  7520. unsigned char cipher[AES_BLOCK_SIZE * 3];
  7521. /* 256 key tests */
  7522. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7523. 0x1e, 0xa6, 0x61, 0xc5, 0x8d, 0x94, 0x3a, 0x0e,
  7524. 0x48, 0x01, 0xe4, 0x2f, 0x4b, 0x09, 0x47, 0x14,
  7525. 0x9e, 0x7f, 0x9f, 0x8e, 0x3e, 0x68, 0xd0, 0xc7,
  7526. 0x50, 0x52, 0x10, 0xbd, 0x31, 0x1a, 0x0e, 0x7c,
  7527. 0xd6, 0xe1, 0x3f, 0xfd, 0xf2, 0x41, 0x8d, 0x8d,
  7528. 0x19, 0x11, 0xc0, 0x04, 0xcd, 0xa5, 0x8d, 0xa3,
  7529. 0xd6, 0x19, 0xb7, 0xe2, 0xb9, 0x14, 0x1e, 0x58,
  7530. 0x31, 0x8e, 0xea, 0x39, 0x2c, 0xf4, 0x1b, 0x08
  7531. };
  7532. WOLFSSL_SMALL_STACK_STATIC unsigned char i1[] = {
  7533. 0xad, 0xf8, 0xd9, 0x26, 0x27, 0x46, 0x4a, 0xd2,
  7534. 0xf0, 0x42, 0x8e, 0x84, 0xa9, 0xf8, 0x75, 0x64
  7535. };
  7536. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7537. 0x2e, 0xed, 0xea, 0x52, 0xcd, 0x82, 0x15, 0xe1,
  7538. 0xac, 0xc6, 0x47, 0xe8, 0x10, 0xbb, 0xc3, 0x64,
  7539. 0x2e, 0x87, 0x28, 0x7f, 0x8d, 0x2e, 0x57, 0xe3,
  7540. 0x6c, 0x0a, 0x24, 0xfb, 0xc1, 0x2a, 0x20, 0x2e
  7541. };
  7542. /* plain text test of partial block is not from NIST test vector list */
  7543. WOLFSSL_SMALL_STACK_STATIC unsigned char pp[] = {
  7544. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  7545. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  7546. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  7547. };
  7548. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7549. 0xcb, 0xaa, 0xd0, 0xe2, 0xf6, 0xce, 0xa3, 0xf5,
  7550. 0x0b, 0x37, 0xf9, 0x34, 0xd4, 0x6a, 0x9b, 0x13,
  7551. 0x0b, 0x9d, 0x54, 0xf0, 0x7e, 0x34, 0xf3, 0x6a,
  7552. 0xf7, 0x93, 0xe8, 0x6f, 0x73, 0xc6, 0xd7, 0xdb
  7553. };
  7554. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  7555. 0xad, 0x50, 0x4b, 0x85, 0xd7, 0x51, 0xbf, 0xba,
  7556. 0x69, 0x13, 0xb4, 0xcc, 0x79, 0xb6, 0x5a, 0x62,
  7557. 0xf7, 0xf3, 0x9d, 0x36, 0x0f, 0x35, 0xb5, 0xec,
  7558. 0x4a, 0x7e, 0x95, 0xbd, 0x9b, 0xa5, 0xf2, 0xec,
  7559. 0xc1, 0xd7, 0x7e, 0xa3, 0xc3, 0x74, 0xbd, 0x4b,
  7560. 0x13, 0x1b, 0x07, 0x83, 0x87, 0xdd, 0x55, 0x5a,
  7561. 0xb5, 0xb0, 0xc7, 0xe5, 0x2d, 0xb5, 0x06, 0x12,
  7562. 0xd2, 0xb5, 0x3a, 0xcb, 0x47, 0x8a, 0x53, 0xb4
  7563. };
  7564. WOLFSSL_SMALL_STACK_STATIC unsigned char i2[] = {
  7565. 0xe6, 0x42, 0x19, 0xed, 0xe0, 0xe1, 0xc2, 0xa0,
  7566. 0x0e, 0xf5, 0x58, 0x6a, 0xc4, 0x9b, 0xeb, 0x6f
  7567. };
  7568. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  7569. 0x24, 0xcb, 0x76, 0x22, 0x55, 0xb5, 0xa8, 0x00,
  7570. 0xf4, 0x6e, 0x80, 0x60, 0x56, 0x9e, 0x05, 0x53,
  7571. 0xbc, 0xfe, 0x86, 0x55, 0x3b, 0xca, 0xd5, 0x89,
  7572. 0xc7, 0x54, 0x1a, 0x73, 0xac, 0xc3, 0x9a, 0xbd,
  7573. 0x53, 0xc4, 0x07, 0x76, 0xd8, 0xe8, 0x22, 0x61,
  7574. 0x9e, 0xa9, 0xad, 0x77, 0xa0, 0x13, 0x4c, 0xfc
  7575. };
  7576. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  7577. 0xa3, 0xc6, 0xf3, 0xf3, 0x82, 0x79, 0x5b, 0x10,
  7578. 0x87, 0xd7, 0x02, 0x50, 0xdb, 0x2c, 0xd3, 0xb1,
  7579. 0xa1, 0x62, 0xa8, 0xb6, 0xdc, 0x12, 0x60, 0x61,
  7580. 0xc1, 0x0a, 0x84, 0xa5, 0x85, 0x3f, 0x3a, 0x89,
  7581. 0xe6, 0x6c, 0xdb, 0xb7, 0x9a, 0xb4, 0x28, 0x9b,
  7582. 0xc3, 0xea, 0xd8, 0x10, 0xe9, 0xc0, 0xaf, 0x92
  7583. };
  7584. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7585. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7586. ERROR_OUT(-5515, out);
  7587. #endif
  7588. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY) \
  7589. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7590. ret = EVP_test(EVP_aes_256_xts(), k2, i2, p2, sizeof(p2), c2, sizeof(c2));
  7591. if (ret != 0) {
  7592. printf("EVP_aes_256_xts failed\n");
  7593. goto out;
  7594. }
  7595. #endif
  7596. XMEMSET(buf, 0, sizeof(buf));
  7597. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  7598. HEAP_HINT, devId) != 0)
  7599. ERROR_OUT(-5500, out);
  7600. else
  7601. aes_inited = 1;
  7602. ret = wc_AesXtsEncrypt(aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  7603. #if defined(WOLFSSL_ASYNC_CRYPT)
  7604. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7605. #endif
  7606. if (ret != 0)
  7607. ERROR_OUT(-5501, out);
  7608. if (XMEMCMP(c2, buf, sizeof(c2)))
  7609. ERROR_OUT(-5502, out);
  7610. wc_AesXtsFree(aes);
  7611. XMEMSET(buf, 0, sizeof(buf));
  7612. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7613. HEAP_HINT, devId) != 0)
  7614. ERROR_OUT(-5503, out);
  7615. ret = wc_AesXtsEncrypt(aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  7616. #if defined(WOLFSSL_ASYNC_CRYPT)
  7617. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7618. #endif
  7619. if (ret != 0)
  7620. ERROR_OUT(-5504, out);
  7621. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  7622. ERROR_OUT(-5505, out);
  7623. /* partial block encryption test */
  7624. XMEMSET(cipher, 0, sizeof(cipher));
  7625. ret = wc_AesXtsEncrypt(aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  7626. #if defined(WOLFSSL_ASYNC_CRYPT)
  7627. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7628. #endif
  7629. if (ret != 0)
  7630. ERROR_OUT(-5506, out);
  7631. wc_AesXtsFree(aes);
  7632. /* partial block decrypt test */
  7633. XMEMSET(buf, 0, sizeof(buf));
  7634. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7635. HEAP_HINT, devId) != 0)
  7636. ERROR_OUT(-5507, out);
  7637. ret = wc_AesXtsDecrypt(aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  7638. #if defined(WOLFSSL_ASYNC_CRYPT)
  7639. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7640. #endif
  7641. if (ret != 0)
  7642. ERROR_OUT(-5508, out);
  7643. if (XMEMCMP(pp, buf, sizeof(pp)))
  7644. ERROR_OUT(-5509, out);
  7645. /* NIST decrypt test vector */
  7646. XMEMSET(buf, 0, sizeof(buf));
  7647. ret = wc_AesXtsDecrypt(aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  7648. #if defined(WOLFSSL_ASYNC_CRYPT)
  7649. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7650. #endif
  7651. if (ret != 0)
  7652. ERROR_OUT(-5510, out);
  7653. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  7654. ERROR_OUT(-5511, out);
  7655. wc_AesXtsFree(aes);
  7656. XMEMSET(buf, 0, sizeof(buf));
  7657. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  7658. HEAP_HINT, devId) != 0)
  7659. ERROR_OUT(-5512, out);
  7660. ret = wc_AesXtsDecrypt(aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  7661. #if defined(WOLFSSL_ASYNC_CRYPT)
  7662. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7663. #endif
  7664. if (ret != 0)
  7665. ERROR_OUT(-5513, out);
  7666. if (XMEMCMP(p2, buf, sizeof(p2)))
  7667. ERROR_OUT(-5514, out);
  7668. out:
  7669. if (aes_inited)
  7670. wc_AesXtsFree(aes);
  7671. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7672. if (aes)
  7673. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7674. #endif
  7675. return ret;
  7676. }
  7677. #endif /* WOLFSSL_AES_256 */
  7678. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  7679. /* both 128 and 256 bit key test */
  7680. static int aes_xts_sector_test(void)
  7681. {
  7682. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7683. XtsAes *aes = NULL;
  7684. #else
  7685. XtsAes aes[1];
  7686. #endif
  7687. int aes_inited = 0;
  7688. int ret = 0;
  7689. unsigned char buf[AES_BLOCK_SIZE * 2];
  7690. /* 128 key tests */
  7691. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7692. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  7693. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  7694. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  7695. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  7696. };
  7697. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7698. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  7699. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  7700. };
  7701. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7702. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  7703. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  7704. };
  7705. word64 s1 = 141;
  7706. /* 256 key tests */
  7707. WOLFSSL_SMALL_STACK_STATIC unsigned char k2[] = {
  7708. 0xef, 0x01, 0x0c, 0xa1, 0xa3, 0x66, 0x3e, 0x32,
  7709. 0x53, 0x43, 0x49, 0xbc, 0x0b, 0xae, 0x62, 0x23,
  7710. 0x2a, 0x15, 0x73, 0x34, 0x85, 0x68, 0xfb, 0x9e,
  7711. 0xf4, 0x17, 0x68, 0xa7, 0x67, 0x4f, 0x50, 0x7a,
  7712. 0x72, 0x7f, 0x98, 0x75, 0x53, 0x97, 0xd0, 0xe0,
  7713. 0xaa, 0x32, 0xf8, 0x30, 0x33, 0x8c, 0xc7, 0xa9,
  7714. 0x26, 0xc7, 0x73, 0xf0, 0x9e, 0x57, 0xb3, 0x57,
  7715. 0xcd, 0x15, 0x6a, 0xfb, 0xca, 0x46, 0xe1, 0xa0
  7716. };
  7717. WOLFSSL_SMALL_STACK_STATIC unsigned char p2[] = {
  7718. 0xed, 0x98, 0xe0, 0x17, 0x70, 0xa8, 0x53, 0xb4,
  7719. 0x9d, 0xb9, 0xe6, 0xaa, 0xf8, 0x8f, 0x0a, 0x41,
  7720. 0xb9, 0xb5, 0x6e, 0x91, 0xa5, 0xa2, 0xb1, 0x1d,
  7721. 0x40, 0x52, 0x92, 0x54, 0xf5, 0x52, 0x3e, 0x75
  7722. };
  7723. WOLFSSL_SMALL_STACK_STATIC unsigned char c2[] = {
  7724. 0xca, 0x20, 0xc5, 0x5e, 0x8d, 0xc1, 0x49, 0x68,
  7725. 0x7d, 0x25, 0x41, 0xde, 0x39, 0xc3, 0xdf, 0x63,
  7726. 0x00, 0xbb, 0x5a, 0x16, 0x3c, 0x10, 0xce, 0xd3,
  7727. 0x66, 0x6b, 0x13, 0x57, 0xdb, 0x8b, 0xd3, 0x9d
  7728. };
  7729. word64 s2 = 187;
  7730. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7731. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7732. ERROR_OUT(-5612, out);
  7733. #endif
  7734. XMEMSET(buf, 0, sizeof(buf));
  7735. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7736. HEAP_HINT, devId) != 0)
  7737. ERROR_OUT(-5600, out);
  7738. else
  7739. aes_inited = 1;
  7740. ret = wc_AesXtsEncryptSector(aes, buf, p1, sizeof(p1), s1);
  7741. #if defined(WOLFSSL_ASYNC_CRYPT)
  7742. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7743. #endif
  7744. if (ret != 0)
  7745. ERROR_OUT(-5601, out);
  7746. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  7747. ERROR_OUT(-5602, out);
  7748. wc_AesXtsFree(aes);
  7749. /* decrypt test */
  7750. XMEMSET(buf, 0, sizeof(buf));
  7751. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7752. HEAP_HINT, devId) != 0)
  7753. ERROR_OUT(-5603, out);
  7754. ret = wc_AesXtsDecryptSector(aes, buf, c1, sizeof(c1), s1);
  7755. #if defined(WOLFSSL_ASYNC_CRYPT)
  7756. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7757. #endif
  7758. if (ret != 0)
  7759. ERROR_OUT(-5604, out);
  7760. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  7761. ERROR_OUT(-5605, out);
  7762. wc_AesXtsFree(aes);
  7763. /* 256 bit key tests */
  7764. XMEMSET(buf, 0, sizeof(buf));
  7765. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_ENCRYPTION,
  7766. HEAP_HINT, devId) != 0)
  7767. ERROR_OUT(-5606, out);
  7768. ret = wc_AesXtsEncryptSector(aes, buf, p2, sizeof(p2), s2);
  7769. #if defined(WOLFSSL_ASYNC_CRYPT)
  7770. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7771. #endif
  7772. if (ret != 0)
  7773. ERROR_OUT(-5607, out);
  7774. if (XMEMCMP(c2, buf, sizeof(c2)))
  7775. ERROR_OUT(-5608, out);
  7776. wc_AesXtsFree(aes);
  7777. /* decrypt test */
  7778. XMEMSET(buf, 0, sizeof(buf));
  7779. if (wc_AesXtsSetKey(aes, k2, sizeof(k2), AES_DECRYPTION,
  7780. HEAP_HINT, devId) != 0)
  7781. ERROR_OUT(-5609, out);
  7782. ret = wc_AesXtsDecryptSector(aes, buf, c2, sizeof(c2), s2);
  7783. #if defined(WOLFSSL_ASYNC_CRYPT)
  7784. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7785. #endif
  7786. if (ret != 0)
  7787. ERROR_OUT(-5610, out);
  7788. if (XMEMCMP(p2, buf, sizeof(p2)))
  7789. ERROR_OUT(-5611, out);
  7790. out:
  7791. if (aes_inited)
  7792. wc_AesXtsFree(aes);
  7793. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7794. if (aes)
  7795. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7796. #endif
  7797. return ret;
  7798. }
  7799. #endif /* WOLFSSL_AES_128 && WOLFSSL_AES_256 */
  7800. #ifdef WOLFSSL_AES_128
  7801. /* testing of bad arguments */
  7802. static int aes_xts_args_test(void)
  7803. {
  7804. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7805. XtsAes *aes = NULL;
  7806. #else
  7807. XtsAes aes[1];
  7808. #endif
  7809. int aes_inited = 0;
  7810. int ret;
  7811. unsigned char buf[AES_BLOCK_SIZE * 2];
  7812. /* 128 key tests */
  7813. WOLFSSL_SMALL_STACK_STATIC unsigned char k1[] = {
  7814. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  7815. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  7816. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  7817. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  7818. };
  7819. WOLFSSL_SMALL_STACK_STATIC unsigned char p1[] = {
  7820. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  7821. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  7822. };
  7823. WOLFSSL_SMALL_STACK_STATIC unsigned char c1[] = {
  7824. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  7825. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  7826. };
  7827. word64 s1 = 141;
  7828. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7829. if ((aes = (XtsAes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  7830. ERROR_OUT(-5708, out);
  7831. #endif
  7832. if (wc_AesXtsSetKey(NULL, k1, sizeof(k1), AES_ENCRYPTION,
  7833. HEAP_HINT, devId) == 0)
  7834. ERROR_OUT(-5700, out);
  7835. if (wc_AesXtsSetKey(aes, NULL, sizeof(k1), AES_ENCRYPTION,
  7836. HEAP_HINT, devId) == 0)
  7837. ERROR_OUT(-5701, out);
  7838. /* encryption operations */
  7839. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  7840. HEAP_HINT, devId) != 0)
  7841. ERROR_OUT(-5702, out);
  7842. else
  7843. aes_inited = 1;
  7844. ret = wc_AesXtsEncryptSector(NULL, buf, p1, sizeof(p1), s1);
  7845. #if defined(WOLFSSL_ASYNC_CRYPT)
  7846. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7847. #endif
  7848. if (ret == 0)
  7849. ERROR_OUT(-5703, out);
  7850. ret = wc_AesXtsEncryptSector(aes, NULL, p1, sizeof(p1), s1);
  7851. #if defined(WOLFSSL_ASYNC_CRYPT)
  7852. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7853. #endif
  7854. if (ret == 0)
  7855. ERROR_OUT(-5704, out);
  7856. wc_AesXtsFree(aes);
  7857. /* decryption operations */
  7858. if (wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  7859. HEAP_HINT, devId) != 0)
  7860. ERROR_OUT(-5705, out);
  7861. ret = wc_AesXtsDecryptSector(NULL, buf, c1, sizeof(c1), s1);
  7862. #if defined(WOLFSSL_ASYNC_CRYPT)
  7863. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7864. #endif
  7865. if (ret == 0)
  7866. ERROR_OUT(-5706, out);
  7867. ret = wc_AesXtsDecryptSector(aes, NULL, c1, sizeof(c1), s1);
  7868. #if defined(WOLFSSL_ASYNC_CRYPT)
  7869. ret = wc_AsyncWait(ret, &aes->aes.asyncDev, WC_ASYNC_FLAG_NONE);
  7870. #endif
  7871. if (ret == 0)
  7872. ERROR_OUT(-5707, out);
  7873. ret = 0;
  7874. out:
  7875. if (aes_inited)
  7876. wc_AesXtsFree(aes);
  7877. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7878. if (aes)
  7879. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_AES);
  7880. #endif
  7881. return ret;
  7882. }
  7883. #endif /* WOLFSSL_AES_128 */
  7884. #endif /* WOLFSSL_AES_XTS */
  7885. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  7886. static int aes_cbc_test(void)
  7887. {
  7888. byte cipher[AES_BLOCK_SIZE];
  7889. byte plain[AES_BLOCK_SIZE];
  7890. int ret;
  7891. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  7892. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  7893. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  7894. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  7895. };
  7896. byte key[] = "0123456789abcdef "; /* align */
  7897. byte iv[] = "1234567890abcdef "; /* align */
  7898. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  7899. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  7900. /* Parameter Validation testing. */
  7901. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key, 17, NULL);
  7902. if (ret != BAD_FUNC_ARG)
  7903. return -5800;
  7904. #ifdef HAVE_AES_DECRYPT
  7905. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key, 17, NULL);
  7906. if (ret != BAD_FUNC_ARG)
  7907. return -5801;
  7908. #endif
  7909. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key,
  7910. AES_BLOCK_SIZE, iv);
  7911. if (ret != 0)
  7912. return -5802;
  7913. #ifdef HAVE_AES_DECRYPT
  7914. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key,
  7915. AES_BLOCK_SIZE, iv);
  7916. if (ret != 0)
  7917. return -5803;
  7918. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE) != 0)
  7919. return -5804;
  7920. #endif /* HAVE_AES_DECRYPT */
  7921. (void)plain;
  7922. return 0;
  7923. }
  7924. #endif
  7925. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  7926. static int aesecb_test(void)
  7927. {
  7928. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7929. Aes *enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7930. #else
  7931. Aes enc[1];
  7932. #endif
  7933. byte cipher[AES_BLOCK_SIZE * 4];
  7934. #ifdef HAVE_AES_DECRYPT
  7935. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7936. Aes *dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7937. #else
  7938. Aes dec[1];
  7939. #endif
  7940. byte plain [AES_BLOCK_SIZE * 4];
  7941. #endif /* HAVE_AES_DECRYPT */
  7942. int ret = 0;
  7943. #if defined(WOLFSSL_AES_256)
  7944. {
  7945. WOLFSSL_SMALL_STACK_STATIC const byte niPlain[] =
  7946. {
  7947. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  7948. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  7949. };
  7950. WOLFSSL_SMALL_STACK_STATIC const byte niCipher[] =
  7951. {
  7952. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  7953. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  7954. };
  7955. WOLFSSL_SMALL_STACK_STATIC const byte niKey[] =
  7956. {
  7957. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  7958. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  7959. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  7960. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  7961. };
  7962. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  7963. ERROR_OUT(-5900, out);
  7964. #if defined(HAVE_AES_DECRYPT)
  7965. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  7966. ERROR_OUT(-5901, out);
  7967. #endif
  7968. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  7969. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  7970. if (ret != 0)
  7971. ERROR_OUT(-5923, out);
  7972. if (wc_AesEcbEncrypt(enc, cipher, niPlain, AES_BLOCK_SIZE) != 0)
  7973. ERROR_OUT(-5924, out);
  7974. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  7975. ERROR_OUT(-5925, out);
  7976. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  7977. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  7978. if (ret != 0)
  7979. ERROR_OUT(-5926, out);
  7980. if (wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE) != 0)
  7981. ERROR_OUT(-5927, out);
  7982. wc_AesEcbDecrypt(dec, plain, niCipher, AES_BLOCK_SIZE);
  7983. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  7984. ERROR_OUT(-5928, out);
  7985. }
  7986. wc_AesFree(enc);
  7987. #ifdef HAVE_AES_DECRYPT
  7988. wc_AesFree(dec);
  7989. #endif
  7990. out:
  7991. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  7992. if (enc)
  7993. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  7994. #ifdef HAVE_AES_DECRYPT
  7995. if (dec)
  7996. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  7997. #endif
  7998. #endif
  7999. #endif /* WOLFSSL_AES_256 */
  8000. return ret;
  8001. }
  8002. #endif /* HAVE_AES_ECB */
  8003. #ifdef WOLFSSL_AES_COUNTER
  8004. static int aesctr_test(Aes* enc, Aes* dec, byte* cipher, byte* plain)
  8005. {
  8006. int ret;
  8007. /* test vectors from "Recommendation for Block Cipher Modes of
  8008. * Operation" NIST Special Publication 800-38A */
  8009. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  8010. {
  8011. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  8012. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  8013. };
  8014. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  8015. {
  8016. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8017. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  8018. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  8019. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  8020. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  8021. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  8022. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  8023. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  8024. };
  8025. #ifdef WOLFSSL_ARMASM
  8026. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap32[] =
  8027. {
  8028. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff,
  8029. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  8030. };
  8031. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap32_2[] =
  8032. {
  8033. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8034. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xfe
  8035. };
  8036. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap64[] =
  8037. {
  8038. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8039. 0x0f,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  8040. };
  8041. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap64_2[] =
  8042. {
  8043. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0,
  8044. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  8045. };
  8046. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap96[] =
  8047. {
  8048. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8049. 0xff,0xff,0xff,0xff,0x0f,0xff,0xff,0xff
  8050. };
  8051. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap96_2[] =
  8052. {
  8053. 0xff,0xff,0xff,0xf0,0xff,0xff,0xff,0xff,
  8054. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  8055. };
  8056. #endif
  8057. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap128[] =
  8058. {
  8059. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8060. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  8061. };
  8062. #ifdef WOLFSSL_ARMASM
  8063. WOLFSSL_SMALL_STACK_STATIC const byte ctrIvWrap128_2[] =
  8064. {
  8065. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  8066. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe
  8067. };
  8068. #endif
  8069. #ifdef WOLFSSL_AES_128
  8070. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  8071. {
  8072. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  8073. 0xc2
  8074. };
  8075. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Key[] =
  8076. {
  8077. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  8078. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  8079. };
  8080. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Cipher[] =
  8081. {
  8082. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  8083. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  8084. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  8085. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  8086. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  8087. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  8088. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  8089. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  8090. };
  8091. #ifdef WOLFSSL_ARMASM
  8092. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32Cipher[] =
  8093. {
  8094. 0xb3,0x8b,0x58,0xbc,0xce,0xf4,0x71,0x78,
  8095. 0xf6,0x7c,0xdb,0xb4,0x27,0x2b,0x0a,0xbf,
  8096. 0x7e,0xad,0xea,0x5c,0xd1
  8097. };
  8098. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32CipherLong[] =
  8099. {
  8100. 0xb3,0x8b,0x58,0xbc,0xce,0xf4,0x71,0x78,
  8101. 0xf6,0x7c,0xdb,0xb4,0x27,0x2b,0x0a,0xbf,
  8102. 0x7e,0xad,0xea,0x5c,0xd1,0xb7,0x98,0xf0,
  8103. 0x22,0x20,0xfe,0x67,0xb0,0x02,0x23,0x50
  8104. };
  8105. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap32_2CipherLong[] =
  8106. {
  8107. 0x6e,0xa1,0x27,0x4d,0xea,0x20,0x5f,0x39,
  8108. 0x68,0xc8,0xb6,0x78,0xde,0xfc,0x53,0x5c,
  8109. 0x90,0xc8,0xf6,0xc6,0xfa,0xe0,0x7b,0x09,
  8110. 0x7c,0xf8,0x9c,0x6a,0x5a,0xa5,0x17,0x7f,
  8111. 0x03,0x92,0x5f,0x4e,0x85,0xea,0x26,0xc9,
  8112. 0x5a,0xc2,0x74,0xe2,0xbf,0xe4,0x1b,0xd4
  8113. };
  8114. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64Cipher[] =
  8115. {
  8116. 0xdd,0x17,0x10,0x7c,0x45,0x04,0xac,0x43,
  8117. 0xef,0xa8,0xcc,0x32,0x34,0x87,0x88,0xd7,
  8118. 0xae,0x74,0x94,0x72,0x8e
  8119. };
  8120. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64CipherLong[] =
  8121. {
  8122. 0xdd,0x17,0x10,0x7c,0x45,0x04,0xac,0x43,
  8123. 0xef,0xa8,0xcc,0x32,0x34,0x87,0x88,0xd7,
  8124. 0xae,0x74,0x94,0x72,0x8e,0xd0,0x71,0xc0,
  8125. 0x89,0x8a,0xa1,0xb0,0x29,0xa0,0x10,0x9e
  8126. };
  8127. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap64_2CipherLong[] =
  8128. {
  8129. 0x3f,0xe7,0xd5,0xf3,0xfa,0x09,0xfe,0x40,
  8130. 0xa6,0xa1,0x32,0x8b,0x57,0x12,0xb9,0xfa,
  8131. 0xf2,0x2d,0xe4,0x3c,0x66,0x1d,0x0a,0x8e,
  8132. 0x46,0xf8,0x2e,0x33,0xce,0x8d,0x4e,0x3b,
  8133. 0x17,0x67,0x9e,0x9f,0x76,0x9e,0xc2,0x99,
  8134. 0xd5,0xd4,0x71,0xed,0xb4,0x33,0xb2,0xcd
  8135. };
  8136. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96Cipher[] =
  8137. {
  8138. 0x55,0x24,0xc2,0x73,0xca,0xa3,0x48,0x03,
  8139. 0x0b,0x72,0x8d,0xd7,0x6c,0x99,0x8e,0x04,
  8140. 0x9d,0x77,0xc9,0x5f,0x38
  8141. };
  8142. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96CipherLong[] =
  8143. {
  8144. 0x55,0x24,0xc2,0x73,0xca,0xa3,0x48,0x03,
  8145. 0x0b,0x72,0x8d,0xd7,0x6c,0x99,0x8e,0x04,
  8146. 0x9d,0x77,0xc9,0x5f,0x38,0xb5,0x6e,0x44,
  8147. 0x21,0x8e,0xda,0x57,0xe0,0x41,0xc7,0x6a
  8148. };
  8149. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap96_2CipherLong[] =
  8150. {
  8151. 0xc8,0x81,0x1a,0xbe,0xc7,0x5b,0x93,0x6f,
  8152. 0xe6,0x52,0xe4,0xb1,0x2d,0x1c,0x39,0xbc,
  8153. 0xeb,0x82,0x27,0x0a,0x7e,0xa5,0x0e,0x2d,
  8154. 0x32,0xda,0xbe,0x10,0x7a,0x10,0xcc,0xd3,
  8155. 0x6f,0xc6,0x83,0x28,0x05,0x57,0x8a,0x24,
  8156. 0x44,0x76,0x17,0x81,0xb9,0x5c,0x94,0x81
  8157. };
  8158. #endif
  8159. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128Cipher[] =
  8160. {
  8161. 0xe1,0x33,0x38,0xe3,0x6c,0xb7,0x19,0x62,
  8162. 0xe0,0x0d,0x02,0x0b,0x4c,0xed,0xbd,0x86,
  8163. 0xd3,0xda,0xe1,0x5b,0x04
  8164. };
  8165. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128CipherLong[] =
  8166. {
  8167. 0xe1,0x33,0x38,0xe3,0x6c,0xb7,0x19,0x62,
  8168. 0xe0,0x0d,0x02,0x0b,0x4c,0xed,0xbd,0x86,
  8169. 0xd3,0xda,0xe1,0x5b,0x04,0xbb,0x35,0x2f,
  8170. 0xa0,0xf5,0x9f,0xeb,0xfc,0xb4,0xda,0x3e
  8171. };
  8172. #ifdef WOLFSSL_ARMASM
  8173. WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128_2CipherLong[] =
  8174. {
  8175. 0xba,0x76,0xaa,0x54,0xd5,0xb5,0x60,0x67,
  8176. 0xc1,0xa7,0x90,0x3b,0x3f,0xdd,0xfa,0x89,
  8177. 0x24,0xdf,0x0c,0x56,0x5c,0xf4,0x2a,0x68,
  8178. 0x97,0x87,0x13,0xb6,0x7a,0xd1,0x24,0xfd,
  8179. 0x4d,0x3f,0x77,0x4a,0xb9,0xe4,0x7d,0xa2,
  8180. 0xdb,0xb9,0x31,0x5e,0xa3,0x11,0x06,0x80
  8181. };
  8182. #endif
  8183. #endif /* WOLFSSL_AES_128 */
  8184. #ifdef WOLFSSL_AES_192
  8185. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  8186. {
  8187. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  8188. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  8189. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  8190. };
  8191. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  8192. {
  8193. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  8194. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b,
  8195. 0x09,0x03,0x39,0xec,0x0a,0xa6,0xfa,0xef,
  8196. 0xd5,0xcc,0xc2,0xc6,0xf4,0xce,0x8e,0x94,
  8197. 0x1e,0x36,0xb2,0x6b,0xd1,0xeb,0xc6,0x70,
  8198. 0xd1,0xbd,0x1d,0x66,0x56,0x20,0xab,0xf7,
  8199. 0x4f,0x78,0xa7,0xf6,0xd2,0x98,0x09,0x58,
  8200. 0x5a,0x97,0xda,0xec,0x58,0xc6,0xb0,0x50
  8201. };
  8202. #ifdef WOLFSSL_ARMASM
  8203. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32Cipher[] =
  8204. {
  8205. 0x28,0xaa,0xfa,0x90,0x72,0x74,0x86,0xaf,
  8206. 0x72,0x73,0x35,0x17,0x70,0x4e,0x7d,0xca,
  8207. 0x0c,0x33,0x97,0x06,0xc0
  8208. };
  8209. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32CipherLong[] =
  8210. {
  8211. 0x28,0xaa,0xfa,0x90,0x72,0x74,0x86,0xaf,
  8212. 0x72,0x73,0x35,0x17,0x70,0x4e,0x7d,0xca,
  8213. 0x0c,0x33,0x97,0x06,0xc0,0xbe,0x83,0x87,
  8214. 0xdd,0xd3,0xff,0xd8,0xe4,0x6a,0x5b,0x84
  8215. };
  8216. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap32_2CipherLong[] =
  8217. {
  8218. 0xf5,0x00,0xa2,0x91,0x54,0xa3,0x76,0xa2,
  8219. 0xdd,0xad,0x16,0x89,0xe5,0xf0,0x1d,0x40,
  8220. 0x84,0xcd,0x74,0x84,0xcb,0x8b,0x9e,0x29,
  8221. 0xa9,0x8a,0x12,0x65,0xa0,0x79,0x5e,0xce,
  8222. 0xd9,0x50,0x65,0x21,0x86,0xb0,0x85,0x0d,
  8223. 0x98,0x2d,0x9a,0x5a,0x11,0xbe,0xa5,0x7f
  8224. };
  8225. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64Cipher[] =
  8226. {
  8227. 0xfe,0x39,0x27,0x97,0xac,0xe5,0xb8,0x74,
  8228. 0xb9,0x8c,0xbf,0x58,0x71,0xa4,0x80,0x33,
  8229. 0x3d,0xf7,0xb4,0xfd,0x8c
  8230. };
  8231. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64CipherLong[] =
  8232. {
  8233. 0xfe,0x39,0x27,0x97,0xac,0xe5,0xb8,0x74,
  8234. 0xb9,0x8c,0xbf,0x58,0x71,0xa4,0x80,0x33,
  8235. 0x3d,0xf7,0xb4,0xfd,0x8c,0x55,0x47,0x10,
  8236. 0xd5,0x91,0x35,0xbe,0xd8,0x0d,0xa5,0x9e
  8237. };
  8238. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap64_2CipherLong[] =
  8239. {
  8240. 0x59,0xf1,0xed,0x70,0x62,0x42,0xa8,0x06,
  8241. 0x07,0x36,0xe1,0xc5,0x04,0x79,0xc3,0x9b,
  8242. 0xd1,0x14,0x5c,0xcc,0x6f,0x81,0x5f,0x2f,
  8243. 0xa0,0xde,0xcf,0x61,0x55,0x18,0x7a,0xac,
  8244. 0xb0,0x59,0x37,0x90,0x53,0xb3,0x00,0x88,
  8245. 0xb4,0x49,0x90,0x7b,0x96,0xcd,0xcc,0xc3
  8246. };
  8247. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96Cipher[] =
  8248. {
  8249. 0x41,0x21,0x40,0x31,0xfb,0xc8,0xad,0x23,
  8250. 0xde,0x00,0x03,0xd8,0x96,0x45,0xc7,0xb5,
  8251. 0x47,0xb5,0xf3,0x30,0x3b
  8252. };
  8253. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96CipherLong[] =
  8254. {
  8255. 0x41,0x21,0x40,0x31,0xfb,0xc8,0xad,0x23,
  8256. 0xde,0x00,0x03,0xd8,0x96,0x45,0xc7,0xb5,
  8257. 0x47,0xb5,0xf3,0x30,0x3b,0xef,0xcd,0x80,
  8258. 0xe3,0x61,0x34,0xef,0x4e,0x1b,0x79,0xc1
  8259. };
  8260. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap96_2CipherLong[] =
  8261. {
  8262. 0x3c,0xb2,0xff,0xc0,0x24,0xe1,0xf5,0xc4,
  8263. 0x0f,0xd1,0x0a,0x1b,0xbe,0x1f,0x23,0xa1,
  8264. 0x8e,0xbf,0x2b,0x96,0xb6,0x37,0xc8,0x25,
  8265. 0x06,0x90,0xe2,0xca,0x71,0x24,0x52,0x95,
  8266. 0xaa,0x8c,0x80,0xdf,0xb7,0xd7,0x30,0xb0,
  8267. 0xcc,0x06,0x4f,0x28,0xa2,0x74,0x27,0xf8
  8268. };
  8269. #endif
  8270. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128Cipher[] =
  8271. {
  8272. 0x5c,0xc3,0x8f,0xab,0x30,0xb6,0xac,0x67,
  8273. 0xdc,0xc2,0x1e,0x7b,0x01,0x2e,0xcf,0x98,
  8274. 0x8c,0x68,0xa7,0xd9,0x57
  8275. };
  8276. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128CipherLong[] =
  8277. {
  8278. 0x5c,0xc3,0x8f,0xab,0x30,0xb6,0xac,0x67,
  8279. 0xdc,0xc2,0x1e,0x7b,0x01,0x2e,0xcf,0x98,
  8280. 0x8c,0x68,0xa7,0xd9,0x57,0xab,0x09,0x0f,
  8281. 0x01,0xc4,0x4e,0x62,0xaf,0xc2,0xdf,0x1a
  8282. };
  8283. #ifdef WOLFSSL_ARMASM
  8284. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Wrap128_2CipherLong[] =
  8285. {
  8286. 0x88,0x0a,0x26,0x4e,0xa8,0x26,0x21,0xe0,
  8287. 0xfc,0xbc,0x63,0xdc,0xd9,0x60,0x52,0xb2,
  8288. 0x99,0x2f,0xbb,0x1e,0x00,0xf5,0x9f,0x6d,
  8289. 0xab,0x48,0x0f,0xc6,0x37,0x12,0x56,0xe3,
  8290. 0x12,0x8d,0x31,0xc8,0xea,0xf4,0x41,0x82,
  8291. 0x7a,0x88,0xe0,0xd7,0xf0,0x67,0x03,0xa4
  8292. };
  8293. #endif
  8294. #endif
  8295. #ifdef WOLFSSL_AES_256
  8296. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  8297. {
  8298. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  8299. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  8300. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  8301. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  8302. };
  8303. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  8304. {
  8305. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  8306. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28,
  8307. 0xf4,0x43,0xe3,0xca,0x4d,0x62,0xb5,0x9a,
  8308. 0xca,0x84,0xe9,0x90,0xca,0xca,0xf5,0xc5,
  8309. 0x2b,0x09,0x30,0xda,0xa2,0x3d,0xe9,0x4c,
  8310. 0xe8,0x70,0x17,0xba,0x2d,0x84,0x98,0x8d,
  8311. 0xdf,0xc9,0xc5,0x8d,0xb6,0x7a,0xad,0xa6,
  8312. 0x13,0xc2,0xdd,0x08,0x45,0x79,0x41,0xa6
  8313. };
  8314. #ifdef WOLFSSL_ARMASM
  8315. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32Cipher[] =
  8316. {
  8317. 0xb0,0xa8,0xc0,0x65,0x85,0x20,0x0d,0x5c,
  8318. 0x25,0xcf,0xe7,0x58,0x63,0xc8,0xd4,0xea,
  8319. 0xa2,0x13,0x47,0x74,0xda
  8320. };
  8321. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32CipherLong[] =
  8322. {
  8323. 0xb0,0xa8,0xc0,0x65,0x85,0x20,0x0d,0x5c,
  8324. 0x25,0xcf,0xe7,0x58,0x63,0xc8,0xd4,0xea,
  8325. 0xa2,0x13,0x47,0x74,0xda,0x89,0x77,0x40,
  8326. 0x28,0x9c,0xe8,0x19,0x26,0x32,0xd8,0x1f
  8327. };
  8328. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap32_2CipherLong[] =
  8329. {
  8330. 0xf6,0xd9,0x22,0xc6,0x80,0x29,0xaf,0x14,
  8331. 0x54,0x6c,0x0a,0xce,0x42,0xea,0x3c,0xa1,
  8332. 0x7c,0xeb,0x36,0x0d,0x8e,0xd7,0x8c,0x59,
  8333. 0xa8,0x09,0x9f,0x9e,0xba,0x5b,0x95,0xfa,
  8334. 0x26,0x8c,0x37,0x59,0xf8,0xae,0x8e,0xaa,
  8335. 0x4d,0xe4,0x1c,0xfe,0x51,0xc7,0xb7,0xcc
  8336. };
  8337. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64Cipher[] =
  8338. {
  8339. 0x74,0x1a,0x52,0x41,0x76,0xb4,0x11,0x8f,
  8340. 0xfd,0x57,0x31,0xfd,0x3d,0x76,0x8f,0x07,
  8341. 0xd4,0x94,0x4c,0xcd,0x4d
  8342. };
  8343. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64CipherLong[] =
  8344. {
  8345. 0x74,0x1a,0x52,0x41,0x76,0xb4,0x11,0x8f,
  8346. 0xfd,0x57,0x31,0xfd,0x3d,0x76,0x8f,0x07,
  8347. 0xd4,0x94,0x4c,0xcd,0x4d,0x47,0x5a,0x92,
  8348. 0x26,0x49,0x81,0x7a,0xda,0x36,0x27,0x01
  8349. };
  8350. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap64_2CipherLong[] =
  8351. {
  8352. 0xf7,0x9c,0xbf,0xf6,0xa2,0xaa,0x8a,0x0a,
  8353. 0x63,0x8a,0x20,0x2f,0x12,0xf1,0x8e,0x49,
  8354. 0x30,0xc0,0x8d,0x5c,0x5f,0x8b,0xbc,0x16,
  8355. 0xdd,0x71,0xee,0x13,0x14,0x7b,0xe1,0x25,
  8356. 0xcb,0x87,0x8a,0xc6,0xdc,0x1d,0x54,0x7a,
  8357. 0xe1,0xe4,0x6f,0x0d,0x95,0x1b,0xd1,0x8b
  8358. };
  8359. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96Cipher[] =
  8360. {
  8361. 0xb9,0x07,0x02,0xb8,0xbe,0x94,0xbf,0x53,
  8362. 0xdf,0x83,0x8e,0x23,0x8c,0x67,0x0c,0x81,
  8363. 0xb8,0x69,0xa1,0x48,0x45
  8364. };
  8365. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96CipherLong[] =
  8366. {
  8367. 0xb9,0x07,0x02,0xb8,0xbe,0x94,0xbf,0x53,
  8368. 0xdf,0x83,0x8e,0x23,0x8c,0x67,0x0c,0x81,
  8369. 0xb8,0x69,0xa1,0x48,0x45,0xf1,0xc6,0x27,
  8370. 0x36,0xa8,0xb2,0x4b,0x0e,0x62,0x6b,0x72
  8371. };
  8372. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap96_2CipherLong[] =
  8373. {
  8374. 0xd5,0x56,0x73,0xaa,0xb8,0xe4,0x06,0xf6,
  8375. 0x83,0x45,0x3a,0xb4,0xb9,0x63,0xec,0xad,
  8376. 0x73,0xc5,0xab,0x78,0xb1,0x21,0xab,0xef,
  8377. 0x69,0x15,0xb7,0x0c,0xe9,0xb4,0x3a,0xe7,
  8378. 0xbc,0xc4,0x22,0xbd,0x93,0xba,0x52,0xe0,
  8379. 0x91,0x2f,0x5e,0x8d,0x6d,0x59,0xf7,0xc2
  8380. };
  8381. #endif
  8382. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128Cipher[] =
  8383. {
  8384. 0x50,0xfd,0x97,0xc3,0xe6,0x1a,0xbb,0x48,
  8385. 0x73,0xfb,0x78,0xdf,0x1e,0x8e,0x77,0xe6,
  8386. 0x4b,0x45,0x7c,0xd6,0x8a
  8387. };
  8388. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128CipherLong[] =
  8389. {
  8390. 0x50,0xfd,0x97,0xc3,0xe6,0x1a,0xbb,0x48,
  8391. 0x73,0xfb,0x78,0xdf,0x1e,0x8e,0x77,0xe6,
  8392. 0x4b,0x45,0x7c,0xd6,0x8a,0xcc,0xda,0x4a,
  8393. 0x89,0xfa,0x23,0x6c,0x06,0xbf,0x26,0x05
  8394. };
  8395. #ifdef WOLFSSL_ARMASM
  8396. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128_2CipherLong[] =
  8397. {
  8398. 0x24,0x5c,0x09,0xa0,0x3b,0x1a,0x5a,0x94,
  8399. 0x2b,0x93,0x56,0x13,0x48,0xa0,0x21,0xce,
  8400. 0x95,0x11,0xa3,0x76,0xd6,0x59,0x88,0x42,
  8401. 0x04,0x71,0x69,0x62,0x28,0xb2,0xee,0x9d,
  8402. 0xd5,0xa0,0xea,0xc7,0x37,0x93,0x92,0xc7,
  8403. 0xf2,0xb6,0x8d,0xd9,0x59,0x1a,0xfa,0xbb
  8404. };
  8405. #endif
  8406. #endif
  8407. int i;
  8408. struct {
  8409. const byte* key;
  8410. int keySz;
  8411. const byte* iv;
  8412. const byte* plain;
  8413. int len;
  8414. const byte* cipher;
  8415. } testVec[] = {
  8416. #ifdef WOLFSSL_AES_128
  8417. { ctr128Key, (int)sizeof(ctr128Key), ctrIv,
  8418. ctrPlain, (int)sizeof(ctrPlain), ctr128Cipher },
  8419. /* let's try with just 9 bytes, non block size test */
  8420. { ctr128Key, (int)sizeof(ctr128Key), ctrIv,
  8421. ctrPlain, (int)sizeof(oddCipher), ctr128Cipher },
  8422. /* and an additional 9 bytes to reuse tmp left buffer */
  8423. { NULL, 0, NULL, ctrPlain, (int)sizeof(oddCipher), oddCipher },
  8424. /* Counter wrapping */
  8425. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128,
  8426. ctrPlain, (int)sizeof(ctr128Wrap128Cipher), ctr128Wrap128Cipher },
  8427. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128,
  8428. ctrPlain, (int)sizeof(ctr128Wrap128CipherLong),
  8429. ctr128Wrap128CipherLong },
  8430. #ifdef WOLFSSL_ARMASM
  8431. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128_2,
  8432. ctrPlain, (int)sizeof(ctr128Wrap128_2CipherLong),
  8433. ctr128Wrap128_2CipherLong },
  8434. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96,
  8435. ctrPlain, (int)sizeof(ctr128Wrap96Cipher), ctr128Wrap96Cipher },
  8436. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96,
  8437. ctrPlain, (int)sizeof(ctr128Wrap96CipherLong),
  8438. ctr128Wrap96CipherLong },
  8439. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap96_2,
  8440. ctrPlain, (int)sizeof(ctr128Wrap96_2CipherLong),
  8441. ctr128Wrap96_2CipherLong },
  8442. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64,
  8443. ctrPlain, (int)sizeof(ctr128Wrap64Cipher), ctr128Wrap64Cipher },
  8444. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64,
  8445. ctrPlain, (int)sizeof(ctr128Wrap64CipherLong),
  8446. ctr128Wrap64CipherLong },
  8447. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap64_2,
  8448. ctrPlain, (int)sizeof(ctr128Wrap64_2CipherLong),
  8449. ctr128Wrap64_2CipherLong },
  8450. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32,
  8451. ctrPlain, (int)sizeof(ctr128Wrap32Cipher), ctr128Wrap32Cipher },
  8452. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32,
  8453. ctrPlain, (int)sizeof(ctr128Wrap32CipherLong),
  8454. ctr128Wrap32CipherLong },
  8455. { ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap32_2,
  8456. ctrPlain, (int)sizeof(ctr128Wrap32_2CipherLong),
  8457. ctr128Wrap32_2CipherLong },
  8458. #endif
  8459. #endif
  8460. #ifdef WOLFSSL_AES_192
  8461. { ctr192Key, (int)sizeof(ctr192Key), ctrIv,
  8462. ctrPlain, (int)sizeof(ctrPlain), ctr192Cipher },
  8463. /* let's try with just 9 bytes, non block size test */
  8464. { ctr192Key, (int)sizeof(ctr192Key), ctrIv,
  8465. ctrPlain, (int)sizeof(oddCipher), ctr192Cipher },
  8466. /* Counter wrapping */
  8467. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128,
  8468. ctrPlain, (int)sizeof(ctr192Wrap128Cipher), ctr192Wrap128Cipher },
  8469. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128,
  8470. ctrPlain, (int)sizeof(ctr192Wrap128CipherLong),
  8471. ctr192Wrap128CipherLong },
  8472. #ifdef WOLFSSL_ARMASM
  8473. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap128_2,
  8474. ctrPlain, (int)sizeof(ctr192Wrap128_2CipherLong),
  8475. ctr192Wrap128_2CipherLong },
  8476. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96,
  8477. ctrPlain, (int)sizeof(ctr192Wrap96Cipher), ctr192Wrap96Cipher },
  8478. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96,
  8479. ctrPlain, (int)sizeof(ctr192Wrap96CipherLong),
  8480. ctr192Wrap96CipherLong },
  8481. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap96_2,
  8482. ctrPlain, (int)sizeof(ctr192Wrap96_2CipherLong),
  8483. ctr192Wrap96_2CipherLong },
  8484. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64,
  8485. ctrPlain, (int)sizeof(ctr192Wrap64Cipher), ctr192Wrap64Cipher },
  8486. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64,
  8487. ctrPlain, (int)sizeof(ctr192Wrap64CipherLong),
  8488. ctr192Wrap64CipherLong },
  8489. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap64_2,
  8490. ctrPlain, (int)sizeof(ctr192Wrap64_2CipherLong),
  8491. ctr192Wrap64_2CipherLong },
  8492. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32,
  8493. ctrPlain, (int)sizeof(ctr192Wrap32Cipher), ctr192Wrap32Cipher },
  8494. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32,
  8495. ctrPlain, (int)sizeof(ctr192Wrap32CipherLong),
  8496. ctr192Wrap32CipherLong },
  8497. { ctr192Key, (int)sizeof(ctr192Key), ctrIvWrap32_2,
  8498. ctrPlain, (int)sizeof(ctr192Wrap32_2CipherLong),
  8499. ctr192Wrap32_2CipherLong },
  8500. #endif
  8501. #endif
  8502. #ifdef WOLFSSL_AES_256
  8503. { ctr256Key, (int)sizeof(ctr256Key), ctrIv,
  8504. ctrPlain, (int)sizeof(ctrPlain), ctr256Cipher },
  8505. /* let's try with just 9 bytes, non block size test */
  8506. { ctr256Key, (int)sizeof(ctr256Key), ctrIv,
  8507. ctrPlain, (int)sizeof(oddCipher), ctr256Cipher },
  8508. /* Counter wrapping */
  8509. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128,
  8510. ctrPlain, (int)sizeof(ctr256Wrap128Cipher), ctr256Wrap128Cipher },
  8511. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128,
  8512. ctrPlain, (int)sizeof(ctr256Wrap128CipherLong),
  8513. ctr256Wrap128CipherLong },
  8514. #ifdef WOLFSSL_ARMASM
  8515. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128_2,
  8516. ctrPlain, (int)sizeof(ctr256Wrap128_2CipherLong),
  8517. ctr256Wrap128_2CipherLong },
  8518. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96,
  8519. ctrPlain, (int)sizeof(ctr256Wrap96Cipher), ctr256Wrap96Cipher },
  8520. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96,
  8521. ctrPlain, (int)sizeof(ctr256Wrap96CipherLong),
  8522. ctr256Wrap96CipherLong },
  8523. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap96_2,
  8524. ctrPlain, (int)sizeof(ctr256Wrap96_2CipherLong),
  8525. ctr256Wrap96_2CipherLong },
  8526. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64,
  8527. ctrPlain, (int)sizeof(ctr256Wrap64Cipher), ctr256Wrap64Cipher },
  8528. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64,
  8529. ctrPlain, (int)sizeof(ctr256Wrap64CipherLong),
  8530. ctr256Wrap64CipherLong },
  8531. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap64_2,
  8532. ctrPlain, (int)sizeof(ctr256Wrap64_2CipherLong),
  8533. ctr256Wrap64_2CipherLong },
  8534. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32,
  8535. ctrPlain, (int)sizeof(ctr256Wrap32Cipher), ctr256Wrap32Cipher },
  8536. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32,
  8537. ctrPlain, (int)sizeof(ctr256Wrap32CipherLong),
  8538. ctr256Wrap32CipherLong },
  8539. { ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap32_2,
  8540. ctrPlain, (int)sizeof(ctr256Wrap32_2CipherLong),
  8541. ctr256Wrap32_2CipherLong },
  8542. #endif
  8543. #endif
  8544. };
  8545. #define AES_CTR_TEST_LEN (int)(sizeof(testVec) / sizeof(*testVec))
  8546. for (i = 0; i < AES_CTR_TEST_LEN; i++) {
  8547. if (testVec[i].key != NULL) {
  8548. ret = wc_AesSetKeyDirect(enc, testVec[i].key, testVec[i].keySz,
  8549. testVec[i].iv, AES_ENCRYPTION);
  8550. if (ret != 0) {
  8551. ERROR_OUT(-5930 - i * 10, out);
  8552. }
  8553. /* Ctr only uses encrypt, even on key setup */
  8554. ret = wc_AesSetKeyDirect(dec, testVec[i].key, testVec[i].keySz,
  8555. testVec[i].iv, AES_ENCRYPTION);
  8556. if (ret != 0) {
  8557. ERROR_OUT(-5931 - i * 10, out);
  8558. }
  8559. }
  8560. ret = wc_AesCtrEncrypt(enc, cipher, testVec[i].plain, testVec[i].len);
  8561. if (ret != 0) {
  8562. ERROR_OUT(-5932 - i * 10, out);
  8563. }
  8564. ret = wc_AesCtrEncrypt(dec, plain, cipher, testVec[i].len);
  8565. if (ret != 0) {
  8566. ERROR_OUT(-5933 - i * 10, out);
  8567. }
  8568. if (XMEMCMP(plain, ctrPlain, testVec[i].len)) {
  8569. ERROR_OUT(-5934 - i * 10, out);
  8570. }
  8571. #if !(FIPS_VERSION_EQ(2,0) && defined(WOLFSSL_ARMASM))
  8572. if (XMEMCMP(cipher, testVec[i].cipher, testVec[i].len)) {
  8573. ERROR_OUT(-5935 - i * 10, out);
  8574. }
  8575. #endif
  8576. }
  8577. out:
  8578. return ret;
  8579. }
  8580. #endif /* WOLFSSL_AES_COUNTER */
  8581. WOLFSSL_TEST_SUBROUTINE int aes_test(void)
  8582. {
  8583. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  8584. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8585. Aes *enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8586. #else
  8587. Aes enc[1];
  8588. #endif
  8589. byte cipher[AES_BLOCK_SIZE * 4];
  8590. #ifdef HAVE_AES_DECRYPT
  8591. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8592. Aes *dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8593. #else
  8594. Aes dec[1];
  8595. #endif
  8596. byte plain [AES_BLOCK_SIZE * 4];
  8597. #endif /* HAVE_AES_DECRYPT */
  8598. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER || WOLFSSL_AES_DIRECT */
  8599. int ret = 0;
  8600. #ifdef HAVE_AES_CBC
  8601. #ifdef WOLFSSL_AES_128
  8602. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  8603. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  8604. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  8605. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  8606. };
  8607. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  8608. {
  8609. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  8610. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  8611. };
  8612. WOLFSSL_SMALL_STACK_STATIC const byte key[] = "0123456789abcdef "; /* align */
  8613. WOLFSSL_SMALL_STACK_STATIC const byte iv[] = "1234567890abcdef "; /* align */
  8614. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8615. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  8616. if (enc == NULL)
  8617. ERROR_OUT(-5990, out);
  8618. #endif
  8619. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  8620. if (dec == NULL)
  8621. ERROR_OUT(-5991, out);
  8622. #endif
  8623. #endif
  8624. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  8625. ERROR_OUT(-5900, out); /* note this error code is used programmatically in cleanup. */
  8626. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  8627. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  8628. ERROR_OUT(-5901, out); /* note this error code is used programmatically in cleanup. */
  8629. #endif
  8630. ret = wc_AesSetKey(enc, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  8631. if (ret != 0)
  8632. ERROR_OUT(-5902, out);
  8633. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  8634. ret = wc_AesSetKey(dec, key, AES_BLOCK_SIZE, iv, AES_DECRYPTION);
  8635. if (ret != 0)
  8636. ERROR_OUT(-5903, out);
  8637. #endif
  8638. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 4);
  8639. ret = wc_AesCbcEncrypt(enc, cipher, msg, AES_BLOCK_SIZE);
  8640. #if defined(WOLFSSL_ASYNC_CRYPT)
  8641. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8642. #endif
  8643. if (ret != 0)
  8644. ERROR_OUT(-5904, out);
  8645. #ifdef HAVE_AES_DECRYPT
  8646. XMEMSET(plain, 0, AES_BLOCK_SIZE * 4);
  8647. ret = wc_AesCbcDecrypt(dec, plain, cipher, AES_BLOCK_SIZE);
  8648. #if defined(WOLFSSL_ASYNC_CRYPT)
  8649. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8650. #endif
  8651. if (ret != 0)
  8652. ERROR_OUT(-5905, out);
  8653. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  8654. ERROR_OUT(-5906, out);
  8655. #endif /* HAVE_AES_DECRYPT */
  8656. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  8657. ERROR_OUT(-5907, out);
  8658. #endif /* WOLFSSL_AES_128 */
  8659. #if defined(WOLFSSL_AESNI) && defined(HAVE_AES_DECRYPT)
  8660. {
  8661. WOLFSSL_SMALL_STACK_STATIC const byte bigMsg[] = {
  8662. /* "All work and no play makes Jack a dull boy. " */
  8663. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  8664. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  8665. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  8666. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  8667. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  8668. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  8669. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  8670. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  8671. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  8672. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  8673. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  8674. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  8675. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  8676. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  8677. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  8678. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  8679. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  8680. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  8681. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  8682. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  8683. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  8684. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  8685. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  8686. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  8687. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  8688. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  8689. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  8690. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  8691. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  8692. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  8693. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  8694. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  8695. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  8696. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  8697. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  8698. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  8699. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  8700. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  8701. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  8702. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  8703. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  8704. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  8705. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  8706. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  8707. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  8708. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  8709. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  8710. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20
  8711. };
  8712. WOLFSSL_SMALL_STACK_STATIC const byte bigKey[] = "0123456789abcdeffedcba9876543210";
  8713. word32 keySz, msgSz;
  8714. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8715. byte *bigCipher = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8716. byte *bigPlain = (byte *)XMALLOC(sizeof(bigMsg), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8717. if ((bigCipher == NULL) ||
  8718. (bigPlain == NULL)) {
  8719. if (bigCipher != NULL)
  8720. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8721. ERROR_OUT(-5992, out);
  8722. }
  8723. #else
  8724. byte bigCipher[sizeof(bigMsg)];
  8725. byte bigPlain[sizeof(bigMsg)];
  8726. #endif
  8727. /* Iterate from one AES_BLOCK_SIZE of bigMsg through the whole
  8728. * message by AES_BLOCK_SIZE for each size of AES key. */
  8729. for (keySz = 16; keySz <= 32; keySz += 8) {
  8730. for (msgSz = AES_BLOCK_SIZE;
  8731. msgSz <= sizeof(bigMsg);
  8732. msgSz += AES_BLOCK_SIZE) {
  8733. XMEMSET(bigCipher, 0, sizeof(bigMsg));
  8734. XMEMSET(bigPlain, 0, sizeof(bigMsg));
  8735. ret = wc_AesSetKey(enc, bigKey, keySz, iv, AES_ENCRYPTION);
  8736. if (ret != 0) {
  8737. ret = -5908;
  8738. break;
  8739. }
  8740. ret = wc_AesSetKey(dec, bigKey, keySz, iv, AES_DECRYPTION);
  8741. if (ret != 0) {
  8742. ret = -5909;
  8743. break;
  8744. }
  8745. ret = wc_AesCbcEncrypt(enc, bigCipher, bigMsg, msgSz);
  8746. #if defined(WOLFSSL_ASYNC_CRYPT)
  8747. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8748. #endif
  8749. if (ret != 0) {
  8750. ret = -5910;
  8751. break;
  8752. }
  8753. ret = wc_AesCbcDecrypt(dec, bigPlain, bigCipher, msgSz);
  8754. #if defined(WOLFSSL_ASYNC_CRYPT)
  8755. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8756. #endif
  8757. if (ret != 0) {
  8758. ret = -5911;
  8759. break;
  8760. }
  8761. if (XMEMCMP(bigPlain, bigMsg, msgSz)) {
  8762. ret = -5912;
  8763. break;
  8764. }
  8765. }
  8766. if (ret != 0)
  8767. break;
  8768. }
  8769. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8770. XFREE(bigCipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8771. XFREE(bigPlain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8772. #endif
  8773. if (ret != 0)
  8774. goto out;
  8775. }
  8776. #endif /* WOLFSSL_AESNI && HAVE_AES_DECRYPT */
  8777. /* Test of AES IV state with encrypt/decrypt */
  8778. #ifdef WOLFSSL_AES_128
  8779. {
  8780. /* Test Vector from "NIST Special Publication 800-38A, 2001 Edition"
  8781. * https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38a.pdf
  8782. */
  8783. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] =
  8784. {
  8785. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  8786. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  8787. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  8788. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51
  8789. };
  8790. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] =
  8791. {
  8792. 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46,
  8793. 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
  8794. 0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee,
  8795. 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2
  8796. };
  8797. WOLFSSL_SMALL_STACK_STATIC const byte key2[] = {
  8798. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  8799. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  8800. };
  8801. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] = {
  8802. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  8803. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  8804. };
  8805. ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
  8806. if (ret != 0)
  8807. ERROR_OUT(-5913, out);
  8808. XMEMSET(cipher, 0, AES_BLOCK_SIZE * 2);
  8809. ret = wc_AesCbcEncrypt(enc, cipher, msg2, AES_BLOCK_SIZE);
  8810. #if defined(WOLFSSL_ASYNC_CRYPT)
  8811. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8812. #endif
  8813. if (ret != 0)
  8814. ERROR_OUT(-5914, out);
  8815. if (XMEMCMP(cipher, verify2, AES_BLOCK_SIZE))
  8816. ERROR_OUT(-5915, out);
  8817. ret = wc_AesCbcEncrypt(enc, cipher + AES_BLOCK_SIZE,
  8818. msg2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  8819. #if defined(WOLFSSL_ASYNC_CRYPT)
  8820. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  8821. #endif
  8822. if (ret != 0)
  8823. ERROR_OUT(-5916, out);
  8824. if (XMEMCMP(cipher + AES_BLOCK_SIZE, verify2 + AES_BLOCK_SIZE,
  8825. AES_BLOCK_SIZE))
  8826. ERROR_OUT(-5917, out);
  8827. #if defined(HAVE_AES_DECRYPT)
  8828. ret = wc_AesSetKey(dec, key2, sizeof(key2), iv2, AES_DECRYPTION);
  8829. if (ret != 0)
  8830. ERROR_OUT(-5918, out);
  8831. XMEMSET(plain, 0, AES_BLOCK_SIZE * 2);
  8832. ret = wc_AesCbcDecrypt(dec, plain, verify2, AES_BLOCK_SIZE);
  8833. #if defined(WOLFSSL_ASYNC_CRYPT)
  8834. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8835. #endif
  8836. if (ret != 0)
  8837. ERROR_OUT(-5919, out);
  8838. if (XMEMCMP(plain, msg2, AES_BLOCK_SIZE))
  8839. ERROR_OUT(-5920, out);
  8840. ret = wc_AesCbcDecrypt(dec, plain + AES_BLOCK_SIZE,
  8841. verify2 + AES_BLOCK_SIZE, AES_BLOCK_SIZE);
  8842. #if defined(WOLFSSL_ASYNC_CRYPT)
  8843. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  8844. #endif
  8845. if (ret != 0)
  8846. ERROR_OUT(-5921, out);
  8847. if (XMEMCMP(plain + AES_BLOCK_SIZE, msg2 + AES_BLOCK_SIZE,
  8848. AES_BLOCK_SIZE))
  8849. ERROR_OUT(-5922, out);
  8850. #endif /* HAVE_AES_DECRYPT */
  8851. }
  8852. #endif /* WOLFSSL_AES_128 */
  8853. #endif /* HAVE_AES_CBC */
  8854. #ifdef WOLFSSL_AES_COUNTER
  8855. ret = aesctr_test(enc, dec, cipher, plain);
  8856. if (ret != 0)
  8857. return ret;
  8858. #endif
  8859. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  8860. {
  8861. WOLFSSL_SMALL_STACK_STATIC const byte niPlain[] =
  8862. {
  8863. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8864. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  8865. };
  8866. WOLFSSL_SMALL_STACK_STATIC const byte niCipher[] =
  8867. {
  8868. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  8869. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  8870. };
  8871. WOLFSSL_SMALL_STACK_STATIC const byte niKey[] =
  8872. {
  8873. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  8874. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  8875. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  8876. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  8877. };
  8878. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  8879. ret = wc_AesSetKey(enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  8880. if (ret != 0)
  8881. ERROR_OUT(-5994, out);
  8882. #if !defined(HAVE_SELFTEST) && \
  8883. (defined(WOLFSSL_LINUXKM) || \
  8884. !defined(HAVE_FIPS) || \
  8885. (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  8886. if (wc_AesEncryptDirect(enc, cipher, niPlain) != 0)
  8887. ERROR_OUT(-5995, out);
  8888. #else
  8889. wc_AesEncryptDirect(enc, cipher, niPlain);
  8890. #endif
  8891. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  8892. ERROR_OUT(-5996, out);
  8893. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  8894. ret = wc_AesSetKey(dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  8895. if (ret != 0)
  8896. ERROR_OUT(-5997, out);
  8897. #if !defined(HAVE_SELFTEST) && \
  8898. (defined(WOLFSSL_LINUXKM) || \
  8899. !defined(HAVE_FIPS) || \
  8900. (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  8901. if (wc_AesDecryptDirect(dec, plain, niCipher) != 0)
  8902. ERROR_OUT(-5998, out);
  8903. #else
  8904. wc_AesDecryptDirect(dec, plain, niCipher);
  8905. #endif
  8906. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  8907. ERROR_OUT(-5999, out);
  8908. }
  8909. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  8910. ret = aes_key_size_test();
  8911. if (ret != 0)
  8912. goto out;
  8913. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  8914. ret = aes_cbc_test();
  8915. if (ret != 0)
  8916. goto out;
  8917. #endif
  8918. #if defined(WOLFSSL_AES_XTS)
  8919. #ifdef WOLFSSL_AES_128
  8920. ret = aes_xts_128_test();
  8921. if (ret != 0)
  8922. goto out;
  8923. #endif
  8924. #ifdef WOLFSSL_AES_256
  8925. ret = aes_xts_256_test();
  8926. if (ret != 0)
  8927. goto out;
  8928. #endif
  8929. #if defined(WOLFSSL_AES_128) && defined(WOLFSSL_AES_256)
  8930. ret = aes_xts_sector_test();
  8931. if (ret != 0)
  8932. goto out;
  8933. #endif
  8934. #ifdef WOLFSSL_AES_128
  8935. ret = aes_xts_args_test();
  8936. if (ret != 0)
  8937. goto out;
  8938. #endif
  8939. #endif
  8940. #if defined(WOLFSSL_AES_CFB)
  8941. ret = aescfb_test();
  8942. if (ret != 0)
  8943. goto out;
  8944. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  8945. ret = aescfb1_test();
  8946. if (ret != 0)
  8947. goto out;
  8948. ret = aescfb8_test();
  8949. if (ret != 0)
  8950. goto out;
  8951. #endif
  8952. #endif
  8953. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  8954. ret = aesecb_test();
  8955. if (ret != 0)
  8956. goto out;
  8957. #endif
  8958. out:
  8959. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_DIRECT)
  8960. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8961. if (enc) {
  8962. if (ret != -5900) /* note this must match ERRROR_OUT() code
  8963. * for wc_AesInit(enc, ...) failure above.
  8964. */
  8965. wc_AesFree(enc);
  8966. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  8967. }
  8968. #else
  8969. if (ret != -5900)
  8970. wc_AesFree(enc);
  8971. #endif
  8972. (void)cipher;
  8973. #ifdef HAVE_AES_DECRYPT
  8974. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8975. if (dec) {
  8976. if ((ret != -5900) && (ret != -5901))
  8977. /* note these codes must match the ERRROR_OUT() codes for
  8978. * wc_AesInit() failures above.
  8979. */
  8980. wc_AesFree(dec);
  8981. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  8982. }
  8983. #else
  8984. if ((ret != -5900) && (ret != -5901))
  8985. wc_AesFree(dec);
  8986. #endif
  8987. (void)plain;
  8988. #endif /* HAVE_AES_DECRYPT */
  8989. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER || WOLFSSL_AES_DIRECT */
  8990. return ret;
  8991. }
  8992. #ifdef WOLFSSL_AES_192
  8993. WOLFSSL_TEST_SUBROUTINE int aes192_test(void)
  8994. {
  8995. #ifdef HAVE_AES_CBC
  8996. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  8997. Aes *enc = NULL;
  8998. #else
  8999. Aes enc[1];
  9000. #endif
  9001. byte cipher[AES_BLOCK_SIZE];
  9002. #ifdef HAVE_AES_DECRYPT
  9003. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9004. Aes *dec = NULL;
  9005. #else
  9006. Aes dec[1];
  9007. #endif
  9008. byte plain[AES_BLOCK_SIZE];
  9009. #endif
  9010. #endif /* HAVE_AES_CBC */
  9011. int ret = 0;
  9012. #ifdef HAVE_AES_CBC
  9013. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  9014. * Appendix F.2.3 */
  9015. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  9016. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  9017. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  9018. };
  9019. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  9020. {
  9021. 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  9022. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8
  9023. };
  9024. WOLFSSL_SMALL_STACK_STATIC byte key[] = {
  9025. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  9026. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  9027. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  9028. };
  9029. WOLFSSL_SMALL_STACK_STATIC byte iv[] = {
  9030. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  9031. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  9032. };
  9033. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9034. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9035. ERROR_OUT(-6008, out);
  9036. #ifdef HAVE_AES_DECRYPT
  9037. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9038. ERROR_OUT(-6009, out);
  9039. #endif
  9040. #endif
  9041. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  9042. ERROR_OUT(-6000, out);
  9043. #ifdef HAVE_AES_DECRYPT
  9044. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  9045. ERROR_OUT(-6001, out);
  9046. #endif
  9047. ret = wc_AesSetKey(enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  9048. if (ret != 0)
  9049. ERROR_OUT(-6002, out);
  9050. #ifdef HAVE_AES_DECRYPT
  9051. ret = wc_AesSetKey(dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  9052. if (ret != 0)
  9053. ERROR_OUT(-6003, out);
  9054. #endif
  9055. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  9056. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  9057. #if defined(WOLFSSL_ASYNC_CRYPT)
  9058. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9059. #endif
  9060. if (ret != 0)
  9061. ERROR_OUT(-6004, out);
  9062. #ifdef HAVE_AES_DECRYPT
  9063. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  9064. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  9065. #if defined(WOLFSSL_ASYNC_CRYPT)
  9066. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9067. #endif
  9068. if (ret != 0)
  9069. ERROR_OUT(-6005, out);
  9070. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  9071. ERROR_OUT(-6006, out);
  9072. }
  9073. #endif
  9074. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  9075. ERROR_OUT(-6007, out);
  9076. wc_AesFree(enc);
  9077. #ifdef HAVE_AES_DECRYPT
  9078. wc_AesFree(dec);
  9079. #endif
  9080. out:
  9081. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9082. if (enc)
  9083. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9084. #ifdef HAVE_AES_DECRYPT
  9085. if (dec)
  9086. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9087. #endif
  9088. #endif
  9089. #endif /* HAVE_AES_CBC */
  9090. return ret;
  9091. }
  9092. #endif /* WOLFSSL_AES_192 */
  9093. #ifdef WOLFSSL_AES_256
  9094. WOLFSSL_TEST_SUBROUTINE int aes256_test(void)
  9095. {
  9096. #ifdef HAVE_AES_CBC
  9097. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9098. Aes *enc = NULL;
  9099. #else
  9100. Aes enc[1];
  9101. #endif
  9102. byte cipher[AES_BLOCK_SIZE];
  9103. #ifdef HAVE_AES_DECRYPT
  9104. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9105. Aes *dec = NULL;
  9106. #else
  9107. Aes dec[1];
  9108. #endif
  9109. byte plain[AES_BLOCK_SIZE];
  9110. #endif
  9111. #endif /* HAVE_AES_CBC */
  9112. int ret = 0;
  9113. #ifdef HAVE_AES_CBC
  9114. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  9115. * Appendix F.2.5 */
  9116. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  9117. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  9118. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  9119. };
  9120. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  9121. {
  9122. 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  9123. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6
  9124. };
  9125. WOLFSSL_SMALL_STACK_STATIC byte key[] = {
  9126. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  9127. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  9128. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  9129. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  9130. };
  9131. WOLFSSL_SMALL_STACK_STATIC byte iv[] = {
  9132. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  9133. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  9134. };
  9135. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9136. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9137. ERROR_OUT(-6108, out);
  9138. #ifdef HAVE_AES_DECRYPT
  9139. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9140. ERROR_OUT(-6109, out);
  9141. #endif
  9142. #endif
  9143. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  9144. ERROR_OUT(-6100, out);
  9145. #ifdef HAVE_AES_DECRYPT
  9146. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  9147. ERROR_OUT(-6101, out);
  9148. #endif
  9149. ret = wc_AesSetKey(enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  9150. if (ret != 0)
  9151. ERROR_OUT(-6102, out);
  9152. #ifdef HAVE_AES_DECRYPT
  9153. ret = wc_AesSetKey(dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  9154. if (ret != 0)
  9155. ERROR_OUT(-6103, out);
  9156. #endif
  9157. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  9158. ret = wc_AesCbcEncrypt(enc, cipher, msg, (int) sizeof(msg));
  9159. #if defined(WOLFSSL_ASYNC_CRYPT)
  9160. ret = wc_AsyncWait(ret, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9161. #endif
  9162. if (ret != 0)
  9163. ERROR_OUT(-6104, out);
  9164. #ifdef HAVE_AES_DECRYPT
  9165. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  9166. ret = wc_AesCbcDecrypt(dec, plain, cipher, (int) sizeof(cipher));
  9167. #if defined(WOLFSSL_ASYNC_CRYPT)
  9168. ret = wc_AsyncWait(ret, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9169. #endif
  9170. if (ret != 0)
  9171. ERROR_OUT(-6105, out);
  9172. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  9173. ERROR_OUT(-6106, out);
  9174. }
  9175. #endif
  9176. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  9177. ERROR_OUT(-6107, out);
  9178. wc_AesFree(enc);
  9179. #ifdef HAVE_AES_DECRYPT
  9180. wc_AesFree(dec);
  9181. #endif
  9182. out:
  9183. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9184. if (enc)
  9185. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9186. #ifdef HAVE_AES_DECRYPT
  9187. if (dec)
  9188. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9189. #endif
  9190. #endif
  9191. #endif /* HAVE_AES_CBC */
  9192. return ret;
  9193. }
  9194. #endif /* WOLFSSL_AES_256 */
  9195. #ifdef HAVE_AESGCM
  9196. #ifdef WOLFSSL_AES_128
  9197. static int aesgcm_default_test_helper(byte* key, int keySz, byte* iv, int ivSz,
  9198. byte* plain, int plainSz, byte* cipher, int cipherSz,
  9199. byte* aad, int aadSz, byte* tag, int tagSz)
  9200. {
  9201. int ret, enc_inited = 0, dec_inited = 0;
  9202. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9203. Aes *enc = NULL;
  9204. Aes *dec = NULL;
  9205. #else
  9206. Aes enc[1];
  9207. Aes dec[1];
  9208. #endif
  9209. byte resultT[AES_BLOCK_SIZE];
  9210. byte resultP[AES_BLOCK_SIZE * 3];
  9211. byte resultC[AES_BLOCK_SIZE * 3];
  9212. int result;
  9213. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9214. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9215. ERROR_OUT(-6118, out);
  9216. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9217. ERROR_OUT(-6119, out);
  9218. #endif
  9219. XMEMSET(resultT, 0, sizeof(resultT));
  9220. XMEMSET(resultC, 0, sizeof(resultC));
  9221. XMEMSET(resultP, 0, sizeof(resultP));
  9222. if (wc_AesInit(enc, HEAP_HINT, devId) != 0)
  9223. ERROR_OUT(-6110, out);
  9224. else
  9225. enc_inited = 1;
  9226. if (wc_AesInit(dec, HEAP_HINT, devId) != 0)
  9227. ERROR_OUT(-6111, out);
  9228. else
  9229. dec_inited = 1;
  9230. result = wc_AesGcmSetKey(enc, key, keySz);
  9231. if (result != 0)
  9232. ERROR_OUT(-6112, out);
  9233. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9234. result = wc_AesGcmEncrypt(enc, resultC, plain, plainSz, iv, ivSz,
  9235. resultT, tagSz, aad, aadSz);
  9236. #if defined(WOLFSSL_ASYNC_CRYPT)
  9237. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9238. #endif
  9239. if (result != 0)
  9240. ERROR_OUT(-6113, out);
  9241. if (cipher != NULL) {
  9242. if (XMEMCMP(cipher, resultC, cipherSz))
  9243. ERROR_OUT(-6114, out);
  9244. }
  9245. if (XMEMCMP(tag, resultT, tagSz))
  9246. ERROR_OUT(-6115, out);
  9247. #ifdef HAVE_AES_DECRYPT
  9248. result = wc_AesGcmSetKey(dec, key, keySz);
  9249. if (result != 0)
  9250. ERROR_OUT(-6116, out);
  9251. result = wc_AesGcmDecrypt(dec, resultP, resultC, cipherSz,
  9252. iv, ivSz, resultT, tagSz, aad, aadSz);
  9253. #if defined(WOLFSSL_ASYNC_CRYPT)
  9254. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9255. #endif
  9256. if (result != 0)
  9257. ERROR_OUT(-6117, out);
  9258. if (plain != NULL) {
  9259. if (XMEMCMP(plain, resultP, plainSz))
  9260. ERROR_OUT(-6118, out);
  9261. }
  9262. #endif /* HAVE_AES_DECRYPT */
  9263. ret = 0;
  9264. out:
  9265. if (enc_inited)
  9266. wc_AesFree(enc);
  9267. if (dec_inited)
  9268. wc_AesFree(dec);
  9269. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9270. if (enc)
  9271. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  9272. if (dec)
  9273. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  9274. #endif
  9275. return ret;
  9276. }
  9277. #endif
  9278. /* tests that only use 12 byte IV and 16 or less byte AAD
  9279. * test vectors are from NIST SP 800-38D
  9280. * https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/CAVP-TESTING-BLOCK-CIPHER-MODES*/
  9281. WOLFSSL_TEST_SUBROUTINE int aesgcm_default_test(void)
  9282. {
  9283. #ifdef WOLFSSL_AES_128
  9284. byte key1[] = {
  9285. 0x29, 0x8e, 0xfa, 0x1c, 0xcf, 0x29, 0xcf, 0x62,
  9286. 0xae, 0x68, 0x24, 0xbf, 0xc1, 0x95, 0x57, 0xfc
  9287. };
  9288. byte iv1[] = {
  9289. 0x6f, 0x58, 0xa9, 0x3f, 0xe1, 0xd2, 0x07, 0xfa,
  9290. 0xe4, 0xed, 0x2f, 0x6d
  9291. };
  9292. ALIGN64 byte plain1[] = {
  9293. 0xcc, 0x38, 0xbc, 0xcd, 0x6b, 0xc5, 0x36, 0xad,
  9294. 0x91, 0x9b, 0x13, 0x95, 0xf5, 0xd6, 0x38, 0x01,
  9295. 0xf9, 0x9f, 0x80, 0x68, 0xd6, 0x5c, 0xa5, 0xac,
  9296. 0x63, 0x87, 0x2d, 0xaf, 0x16, 0xb9, 0x39, 0x01
  9297. };
  9298. byte aad1[] = {
  9299. 0x02, 0x1f, 0xaf, 0xd2, 0x38, 0x46, 0x39, 0x73,
  9300. 0xff, 0xe8, 0x02, 0x56, 0xe5, 0xb1, 0xc6, 0xb1
  9301. };
  9302. ALIGN64 byte cipher1[] = {
  9303. 0xdf, 0xce, 0x4e, 0x9c, 0xd2, 0x91, 0x10, 0x3d,
  9304. 0x7f, 0xe4, 0xe6, 0x33, 0x51, 0xd9, 0xe7, 0x9d,
  9305. 0x3d, 0xfd, 0x39, 0x1e, 0x32, 0x67, 0x10, 0x46,
  9306. 0x58, 0x21, 0x2d, 0xa9, 0x65, 0x21, 0xb7, 0xdb
  9307. };
  9308. byte tag1[] = {
  9309. 0x54, 0x24, 0x65, 0xef, 0x59, 0x93, 0x16, 0xf7,
  9310. 0x3a, 0x7a, 0x56, 0x05, 0x09, 0xa2, 0xd9, 0xf2
  9311. };
  9312. byte key2[] = {
  9313. 0x01, 0x6d, 0xbb, 0x38, 0xda, 0xa7, 0x6d, 0xfe,
  9314. 0x7d, 0xa3, 0x84, 0xeb, 0xf1, 0x24, 0x03, 0x64
  9315. };
  9316. byte iv2[] = {
  9317. 0x07, 0x93, 0xef, 0x3a, 0xda, 0x78, 0x2f, 0x78,
  9318. 0xc9, 0x8a, 0xff, 0xe3
  9319. };
  9320. ALIGN64 byte plain2[] = {
  9321. 0x4b, 0x34, 0xa9, 0xec, 0x57, 0x63, 0x52, 0x4b,
  9322. 0x19, 0x1d, 0x56, 0x16, 0xc5, 0x47, 0xf6, 0xb7
  9323. };
  9324. ALIGN64 byte cipher2[] = {
  9325. 0x60, 0x9a, 0xa3, 0xf4, 0x54, 0x1b, 0xc0, 0xfe,
  9326. 0x99, 0x31, 0xda, 0xad, 0x2e, 0xe1, 0x5d, 0x0c
  9327. };
  9328. byte tag2[] = {
  9329. 0x33, 0xaf, 0xec, 0x59, 0xc4, 0x5b, 0xaf, 0x68,
  9330. 0x9a, 0x5e, 0x1b, 0x13, 0xae, 0x42, 0x36, 0x19
  9331. };
  9332. byte key3[] = {
  9333. 0xb0, 0x1e, 0x45, 0xcc, 0x30, 0x88, 0xaa, 0xba,
  9334. 0x9f, 0xa4, 0x3d, 0x81, 0xd4, 0x81, 0x82, 0x3f
  9335. };
  9336. byte iv3[] = {
  9337. 0x5a, 0x2c, 0x4a, 0x66, 0x46, 0x87, 0x13, 0x45,
  9338. 0x6a, 0x4b, 0xd5, 0xe1
  9339. };
  9340. byte tag3[] = {
  9341. 0x01, 0x42, 0x80, 0xf9, 0x44, 0xf5, 0x3c, 0x68,
  9342. 0x11, 0x64, 0xb2, 0xff
  9343. };
  9344. int ret;
  9345. ret = aesgcm_default_test_helper(key1, sizeof(key1), iv1, sizeof(iv1),
  9346. plain1, sizeof(plain1), cipher1, sizeof(cipher1),
  9347. aad1, sizeof(aad1), tag1, sizeof(tag1));
  9348. if (ret != 0) {
  9349. return ret;
  9350. }
  9351. ret = aesgcm_default_test_helper(key2, sizeof(key2), iv2, sizeof(iv2),
  9352. plain2, sizeof(plain2), cipher2, sizeof(cipher2),
  9353. NULL, 0, tag2, sizeof(tag2));
  9354. if (ret != 0) {
  9355. return ret;
  9356. }
  9357. ret = aesgcm_default_test_helper(key3, sizeof(key3), iv3, sizeof(iv3),
  9358. NULL, 0, NULL, 0,
  9359. NULL, 0, tag3, sizeof(tag3));
  9360. if (ret != 0) {
  9361. return ret;
  9362. }
  9363. #endif
  9364. return 0;
  9365. }
  9366. WOLFSSL_TEST_SUBROUTINE int aesgcm_test(void)
  9367. {
  9368. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9369. Aes *enc = NULL;
  9370. Aes *dec = NULL;
  9371. #else
  9372. Aes enc[1];
  9373. Aes dec[1];
  9374. #endif
  9375. /*
  9376. * This is Test Case 16 from the document Galois/
  9377. * Counter Mode of Operation (GCM) by McGrew and
  9378. * Viega.
  9379. */
  9380. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  9381. {
  9382. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  9383. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  9384. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  9385. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  9386. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  9387. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  9388. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  9389. 0xba, 0x63, 0x7b, 0x39
  9390. };
  9391. #if defined(WOLFSSL_AES_256) || defined(WOLFSSL_AES_192)
  9392. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  9393. {
  9394. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  9395. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  9396. 0xab, 0xad, 0xda, 0xd2
  9397. };
  9398. #endif
  9399. #ifdef WOLFSSL_AES_256
  9400. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  9401. {
  9402. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  9403. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  9404. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  9405. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08
  9406. };
  9407. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  9408. {
  9409. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
  9410. 0xde, 0xca, 0xf8, 0x88
  9411. };
  9412. #endif /* WOLFSSL_AES_256 */
  9413. #if defined(WOLFSSL_AES_256) || defined(WOLFSSL_AES_192)
  9414. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  9415. {
  9416. 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
  9417. 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
  9418. 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  9419. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
  9420. 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
  9421. 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  9422. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
  9423. 0xbc, 0xc9, 0xf6, 0x62
  9424. };
  9425. #endif /* WOLFSSL_AES_256 || WOLFSSL_AES_192 */
  9426. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  9427. {
  9428. 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68,
  9429. 0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b
  9430. };
  9431. /* FIPS, QAT and PIC32MZ HW Crypto only support 12-byte IV */
  9432. #if !defined(HAVE_FIPS) && \
  9433. !defined(WOLFSSL_PIC32MZ_CRYPT) && \
  9434. !defined(FREESCALE_LTC) && !defined(FREESCALE_MMCAU) && \
  9435. !defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  9436. !defined(WOLFSSL_SILABS_SE_ACCEL) && !defined(WOLFSSL_KCAPI_AES) && \
  9437. !(defined(WOLF_CRYPTO_CB) && \
  9438. (defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)))
  9439. #define ENABLE_NON_12BYTE_IV_TEST
  9440. #ifdef WOLFSSL_AES_192
  9441. /* Test Case 12, uses same plaintext and AAD data. */
  9442. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  9443. {
  9444. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  9445. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  9446. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c
  9447. };
  9448. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  9449. {
  9450. 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5,
  9451. 0x55, 0x90, 0x9c, 0x5a, 0xff, 0x52, 0x69, 0xaa,
  9452. 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1,
  9453. 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28,
  9454. 0xc3, 0xc0, 0xc9, 0x51, 0x56, 0x80, 0x95, 0x39,
  9455. 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54,
  9456. 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57,
  9457. 0xa6, 0x37, 0xb3, 0x9b
  9458. };
  9459. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  9460. {
  9461. 0xd2, 0x7e, 0x88, 0x68, 0x1c, 0xe3, 0x24, 0x3c,
  9462. 0x48, 0x30, 0x16, 0x5a, 0x8f, 0xdc, 0xf9, 0xff,
  9463. 0x1d, 0xe9, 0xa1, 0xd8, 0xe6, 0xb4, 0x47, 0xef,
  9464. 0x6e, 0xf7, 0xb7, 0x98, 0x28, 0x66, 0x6e, 0x45,
  9465. 0x81, 0xe7, 0x90, 0x12, 0xaf, 0x34, 0xdd, 0xd9,
  9466. 0xe2, 0xf0, 0x37, 0x58, 0x9b, 0x29, 0x2d, 0xb3,
  9467. 0xe6, 0x7c, 0x03, 0x67, 0x45, 0xfa, 0x22, 0xe7,
  9468. 0xe9, 0xb7, 0x37, 0x3b
  9469. };
  9470. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  9471. {
  9472. 0xdc, 0xf5, 0x66, 0xff, 0x29, 0x1c, 0x25, 0xbb,
  9473. 0xb8, 0x56, 0x8f, 0xc3, 0xd3, 0x76, 0xa6, 0xd9
  9474. };
  9475. #endif /* WOLFSSL_AES_192 */
  9476. #ifdef WOLFSSL_AES_128
  9477. /* The following is an interesting test case from the example
  9478. * FIPS test vectors for AES-GCM. IVlen = 1 byte */
  9479. WOLFSSL_SMALL_STACK_STATIC const byte p3[] =
  9480. {
  9481. 0x57, 0xce, 0x45, 0x1f, 0xa5, 0xe2, 0x35, 0xa5,
  9482. 0x8e, 0x1a, 0xa2, 0x3b, 0x77, 0xcb, 0xaf, 0xe2
  9483. };
  9484. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  9485. {
  9486. 0xbb, 0x01, 0xd7, 0x03, 0x81, 0x1c, 0x10, 0x1a,
  9487. 0x35, 0xe0, 0xff, 0xd2, 0x91, 0xba, 0xf2, 0x4b
  9488. };
  9489. WOLFSSL_SMALL_STACK_STATIC const byte iv3[] =
  9490. {
  9491. 0xca
  9492. };
  9493. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  9494. {
  9495. 0x6b, 0x5f, 0xb3, 0x9d, 0xc1, 0xc5, 0x7a, 0x4f,
  9496. 0xf3, 0x51, 0x4d, 0xc2, 0xd5, 0xf0, 0xd0, 0x07
  9497. };
  9498. WOLFSSL_SMALL_STACK_STATIC const byte a3[] =
  9499. {
  9500. 0x40, 0xfc, 0xdc, 0xd7, 0x4a, 0xd7, 0x8b, 0xf1,
  9501. 0x3e, 0x7c, 0x60, 0x55, 0x50, 0x51, 0xdd, 0x54
  9502. };
  9503. WOLFSSL_SMALL_STACK_STATIC const byte t3[] =
  9504. {
  9505. 0x06, 0x90, 0xed, 0x01, 0x34, 0xdd, 0xc6, 0x95,
  9506. 0x31, 0x2e, 0x2a, 0xf9, 0x57, 0x7a, 0x1e, 0xa6
  9507. };
  9508. #endif /* WOLFSSL_AES_128 */
  9509. #ifdef WOLFSSL_AES_256
  9510. int ivlen;
  9511. #endif
  9512. #endif
  9513. byte resultT[sizeof(t1) + AES_BLOCK_SIZE];
  9514. byte resultP[sizeof(p) + AES_BLOCK_SIZE];
  9515. byte resultC[sizeof(p) + AES_BLOCK_SIZE];
  9516. int result = 0;
  9517. int ret;
  9518. #ifdef WOLFSSL_AES_256
  9519. int alen;
  9520. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  9521. int plen;
  9522. #endif
  9523. #endif
  9524. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  9525. byte buf[sizeof(p) + AES_BLOCK_SIZE];
  9526. byte bufA[sizeof(a) + 1];
  9527. byte *large_aad = (byte*)XMALLOC((size_t)1024 + 16, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9528. #endif
  9529. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM)
  9530. #if !defined(BENCH_AESGCM_LARGE)
  9531. #define BENCH_AESGCM_LARGE 1024
  9532. #endif
  9533. #ifndef WOLFSSL_NO_MALLOC
  9534. byte *large_input = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9535. byte *large_output = (byte *)XMALLOC(BENCH_AESGCM_LARGE + AES_BLOCK_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9536. byte *large_outdec = (byte *)XMALLOC(BENCH_AESGCM_LARGE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9537. if ((! large_input) || (! large_output) || (! large_outdec))
  9538. ERROR_OUT(MEMORY_E, out);
  9539. #else
  9540. byte large_input[BENCH_AESGCM_LARGE];
  9541. byte large_output[BENCH_AESGCM_LARGE];
  9542. byte large_outdec[BENCH_AESGCM_LARGE];
  9543. #endif
  9544. XMEMSET(large_input, 0, BENCH_AESGCM_LARGE);
  9545. XMEMSET(large_output, 0, BENCH_AESGCM_LARGE + AES_BLOCK_SIZE);
  9546. XMEMSET(large_outdec, 0, BENCH_AESGCM_LARGE);
  9547. #endif
  9548. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  9549. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9550. ERROR_OUT(-6342, out);
  9551. if ((dec = (Aes *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  9552. ERROR_OUT(-6343, out);
  9553. #endif
  9554. (void)result;
  9555. XMEMSET(resultT, 0, sizeof(resultT));
  9556. XMEMSET(resultC, 0, sizeof(resultC));
  9557. XMEMSET(resultP, 0, sizeof(resultP));
  9558. if (wc_AesInit(enc, HEAP_HINT, devId) != 0) {
  9559. ERROR_OUT(-6300, out);
  9560. }
  9561. if (wc_AesInit(dec, HEAP_HINT, devId) != 0) {
  9562. ERROR_OUT(-6301, out);
  9563. }
  9564. #ifdef WOLFSSL_AES_256
  9565. result = wc_AesGcmSetKey(enc, k1, sizeof(k1));
  9566. if (result != 0)
  9567. ERROR_OUT(-6302, out);
  9568. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9569. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  9570. resultT, sizeof(t1), a, sizeof(a));
  9571. #if defined(WOLFSSL_ASYNC_CRYPT)
  9572. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9573. #endif
  9574. if (result != 0)
  9575. ERROR_OUT(-6303, out);
  9576. if (XMEMCMP(c1, resultC, sizeof(c1)))
  9577. ERROR_OUT(-6304, out);
  9578. if (XMEMCMP(t1, resultT, sizeof(t1)))
  9579. ERROR_OUT(-6305, out);
  9580. #ifdef HAVE_AES_DECRYPT
  9581. result = wc_AesGcmSetKey(dec, k1, sizeof(k1));
  9582. if (result != 0)
  9583. ERROR_OUT(-6306, out);
  9584. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1),
  9585. iv1, sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  9586. #if defined(WOLFSSL_ASYNC_CRYPT)
  9587. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9588. #endif
  9589. if (result != 0)
  9590. ERROR_OUT(-6307, out);
  9591. if (XMEMCMP(p, resultP, sizeof(p)))
  9592. ERROR_OUT(-6308, out);
  9593. #endif /* HAVE_AES_DECRYPT */
  9594. /* Large buffer test */
  9595. #ifdef BENCH_AESGCM_LARGE
  9596. /* setup test buffer */
  9597. for (alen=0; alen<BENCH_AESGCM_LARGE; alen++)
  9598. large_input[alen] = (byte)alen;
  9599. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9600. result = wc_AesGcmEncrypt(enc, large_output, large_input,
  9601. BENCH_AESGCM_LARGE, iv1, sizeof(iv1),
  9602. resultT, sizeof(t1), a, sizeof(a));
  9603. #if defined(WOLFSSL_ASYNC_CRYPT)
  9604. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9605. #endif
  9606. if (result != 0)
  9607. ERROR_OUT(-6309, out);
  9608. #ifdef HAVE_AES_DECRYPT
  9609. result = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  9610. BENCH_AESGCM_LARGE, iv1, sizeof(iv1), resultT,
  9611. sizeof(t1), a, sizeof(a));
  9612. #if defined(WOLFSSL_ASYNC_CRYPT)
  9613. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9614. #endif
  9615. if (result != 0)
  9616. ERROR_OUT(-6310, out);
  9617. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  9618. ERROR_OUT(-6311, out);
  9619. #endif /* HAVE_AES_DECRYPT */
  9620. #endif /* BENCH_AESGCM_LARGE */
  9621. #if defined(ENABLE_NON_12BYTE_IV_TEST) && defined(WOLFSSL_AES_256)
  9622. /* Variable IV length test */
  9623. for (ivlen=1; ivlen<(int)sizeof(k1); ivlen++) {
  9624. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9625. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), k1,
  9626. (word32)ivlen, resultT, sizeof(t1), a, sizeof(a));
  9627. #if defined(WOLFSSL_ASYNC_CRYPT)
  9628. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9629. #endif
  9630. if (result != 0)
  9631. ERROR_OUT(-6312, out);
  9632. #ifdef HAVE_AES_DECRYPT
  9633. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), k1,
  9634. (word32)ivlen, resultT, sizeof(t1), a, sizeof(a));
  9635. #if defined(WOLFSSL_ASYNC_CRYPT)
  9636. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9637. #endif
  9638. if (result != 0)
  9639. ERROR_OUT(-6313, out);
  9640. #endif /* HAVE_AES_DECRYPT */
  9641. }
  9642. #endif
  9643. #if !(defined(WOLF_CRYPTO_CB) && defined(HAVE_INTEL_QA_SYNC))
  9644. /* Variable authenticated data length test */
  9645. for (alen=0; alen<(int)sizeof(p); alen++) {
  9646. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9647. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1,
  9648. sizeof(iv1), resultT, sizeof(t1), p, (word32)alen);
  9649. #if defined(WOLFSSL_ASYNC_CRYPT)
  9650. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9651. #endif
  9652. if (result != 0)
  9653. ERROR_OUT(-6314, out);
  9654. #ifdef HAVE_AES_DECRYPT
  9655. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), iv1,
  9656. sizeof(iv1), resultT, sizeof(t1), p, (word32)alen);
  9657. #if defined(WOLFSSL_ASYNC_CRYPT)
  9658. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9659. #endif
  9660. if (result != 0)
  9661. ERROR_OUT(-6315, out);
  9662. #endif /* HAVE_AES_DECRYPT */
  9663. }
  9664. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  9665. if (! large_aad)
  9666. ERROR_OUT(MEMORY_E, out);
  9667. XMEMSET(large_aad, 0, 1024+16);
  9668. /* Variable authenticated data length test */
  9669. for (alen=0; alen<=1024; alen+=16) {
  9670. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9671. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1,
  9672. sizeof(iv1), resultT, sizeof(t1), large_aad, (word32)alen);
  9673. if (result != 0)
  9674. ERROR_OUT(-6316, out);
  9675. #ifdef HAVE_AES_DECRYPT
  9676. result = wc_AesGcmDecrypt(dec, resultP, resultC, sizeof(c1), iv1,
  9677. sizeof(iv1), resultT, sizeof(t1), large_aad, (word32)alen);
  9678. if (result != 0)
  9679. ERROR_OUT(-6317, out);
  9680. #endif /* HAVE_AES_DECRYPT */
  9681. }
  9682. /* Test unaligned memory of all potential arguments */
  9683. result = wc_AesGcmSetKey(enc, k1, sizeof(k1));
  9684. if (result != 0)
  9685. ERROR_OUT(-6318, out);
  9686. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9687. XMEMCPY(&buf[1], p, sizeof(p));
  9688. XMEMCPY(&bufA[1], a, sizeof(a));
  9689. result = wc_AesGcmEncrypt(enc, &resultC[1], &buf[1], sizeof(p), iv1, sizeof(iv1),
  9690. &resultT[1], sizeof(t1), &bufA[1], sizeof(a));
  9691. if (result != 0)
  9692. ERROR_OUT(-6319, out);
  9693. if (XMEMCMP(c1, &resultC[1], sizeof(c1)))
  9694. ERROR_OUT(-6320, out);
  9695. if (XMEMCMP(t1, &resultT[1], sizeof(t1)))
  9696. ERROR_OUT(-6321, out);
  9697. #ifdef HAVE_AES_DECRYPT
  9698. result = wc_AesGcmSetKey(dec, k1, sizeof(k1));
  9699. if (result != 0)
  9700. ERROR_OUT(-6322, out);
  9701. result = wc_AesGcmDecrypt(dec, &resultP[1], &resultC[1], sizeof(c1),
  9702. iv1, sizeof(iv1), &resultT[1], sizeof(t1), &bufA[1], sizeof(a));
  9703. if (result != 0)
  9704. ERROR_OUT(-6323, out);
  9705. if (XMEMCMP(p, &resultP[1], sizeof(p)))
  9706. ERROR_OUT(-6324, out);
  9707. #endif /* HAVE_AES_DECRYPT */
  9708. #endif /* Xilinx Versal */
  9709. #endif
  9710. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  9711. #ifdef BENCH_AESGCM_LARGE
  9712. /* Variable plain text length test */
  9713. for (plen=1; plen<BENCH_AESGCM_LARGE; plen++) {
  9714. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9715. result = wc_AesGcmEncrypt(enc, large_output, large_input,
  9716. plen, iv1, sizeof(iv1), resultT,
  9717. sizeof(t1), a, sizeof(a));
  9718. #if defined(WOLFSSL_ASYNC_CRYPT)
  9719. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9720. #endif
  9721. if (result != 0)
  9722. ERROR_OUT(-6316, out);
  9723. #ifdef HAVE_AES_DECRYPT
  9724. result = wc_AesGcmDecrypt(dec, large_outdec, large_output,
  9725. plen, iv1, sizeof(iv1), resultT,
  9726. sizeof(t1), a, sizeof(a));
  9727. #if defined(WOLFSSL_ASYNC_CRYPT)
  9728. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9729. #endif
  9730. if (result != 0)
  9731. ERROR_OUT(-6317, out);
  9732. #endif /* HAVE_AES_DECRYPT */
  9733. }
  9734. #else /* BENCH_AESGCM_LARGE */
  9735. /* Variable plain text length test */
  9736. for (plen=1; plen<(int)sizeof(p); plen++) {
  9737. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9738. result = wc_AesGcmEncrypt(enc, resultC, p, (word32)plen, iv1,
  9739. sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  9740. #if defined(WOLFSSL_ASYNC_CRYPT)
  9741. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9742. #endif
  9743. if (result != 0)
  9744. ERROR_OUT(-6318, out);
  9745. #ifdef HAVE_AES_DECRYPT
  9746. result = wc_AesGcmDecrypt(dec, resultP, resultC, (word32)plen, iv1,
  9747. sizeof(iv1), resultT, sizeof(t1), a, sizeof(a));
  9748. #if defined(WOLFSSL_ASYNC_CRYPT)
  9749. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9750. #endif
  9751. if (result != 0)
  9752. ERROR_OUT(-6319, out);
  9753. #endif /* HAVE_AES_DECRYPT */
  9754. }
  9755. #endif /* BENCH_AESGCM_LARGE */
  9756. #endif
  9757. #endif /* WOLFSSL_AES_256 */
  9758. /* test with IV != 12 bytes */
  9759. #ifdef ENABLE_NON_12BYTE_IV_TEST
  9760. XMEMSET(resultT, 0, sizeof(resultT));
  9761. XMEMSET(resultC, 0, sizeof(resultC));
  9762. XMEMSET(resultP, 0, sizeof(resultP));
  9763. #ifdef WOLFSSL_AES_192
  9764. wc_AesGcmSetKey(enc, k2, sizeof(k2));
  9765. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9766. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv2, sizeof(iv2),
  9767. resultT, sizeof(t1), a, sizeof(a));
  9768. #if defined(WOLFSSL_ASYNC_CRYPT)
  9769. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9770. #endif
  9771. if (result != 0)
  9772. ERROR_OUT(-6320, out);
  9773. if (XMEMCMP(c2, resultC, sizeof(c2)))
  9774. ERROR_OUT(-6321, out);
  9775. if (XMEMCMP(t2, resultT, sizeof(t1)))
  9776. ERROR_OUT(-6322, out);
  9777. #ifdef HAVE_AES_DECRYPT
  9778. result = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c1),
  9779. iv2, sizeof(iv2), resultT, sizeof(t1), a, sizeof(a));
  9780. #if defined(WOLFSSL_ASYNC_CRYPT)
  9781. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9782. #endif
  9783. if (result != 0)
  9784. ERROR_OUT(-6323, out);
  9785. if (XMEMCMP(p, resultP, sizeof(p)))
  9786. ERROR_OUT(-6324, out);
  9787. #endif /* HAVE_AES_DECRYPT */
  9788. XMEMSET(resultT, 0, sizeof(resultT));
  9789. XMEMSET(resultC, 0, sizeof(resultC));
  9790. XMEMSET(resultP, 0, sizeof(resultP));
  9791. #endif /* WOLFSSL_AES_192 */
  9792. #ifdef WOLFSSL_AES_128
  9793. wc_AesGcmSetKey(enc, k3, sizeof(k3));
  9794. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9795. result = wc_AesGcmEncrypt(enc, resultC, p3, sizeof(p3), iv3, sizeof(iv3),
  9796. resultT, sizeof(t3), a3, sizeof(a3));
  9797. #if defined(WOLFSSL_ASYNC_CRYPT)
  9798. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9799. #endif
  9800. if (result != 0)
  9801. ERROR_OUT(-6325, out);
  9802. if (XMEMCMP(c3, resultC, sizeof(c3)))
  9803. ERROR_OUT(-6326, out);
  9804. if (XMEMCMP(t3, resultT, sizeof(t3)))
  9805. ERROR_OUT(-6327, out);
  9806. #ifdef HAVE_AES_DECRYPT
  9807. result = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(c3),
  9808. iv3, sizeof(iv3), resultT, sizeof(t3), a3, sizeof(a3));
  9809. #if defined(WOLFSSL_ASYNC_CRYPT)
  9810. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9811. #endif
  9812. if (result != 0)
  9813. ERROR_OUT(-6328, out);
  9814. if (XMEMCMP(p3, resultP, sizeof(p3)))
  9815. ERROR_OUT(-6329, out);
  9816. #endif /* HAVE_AES_DECRYPT */
  9817. #endif /* WOLFSSL_AES_128 */
  9818. #endif /* ENABLE_NON_12BYTE_IV_TEST */
  9819. #if defined(WOLFSSL_AES_256) && !defined(WOLFSSL_AFALG_XILINX_AES) && \
  9820. !defined(WOLFSSL_XILINX_CRYPT) && \
  9821. !(defined(WOLF_CRYPTO_CB) && \
  9822. defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC))
  9823. XMEMSET(resultT, 0, sizeof(resultT));
  9824. XMEMSET(resultC, 0, sizeof(resultC));
  9825. XMEMSET(resultP, 0, sizeof(resultP));
  9826. wc_AesGcmSetKey(enc, k1, sizeof(k1));
  9827. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  9828. result = wc_AesGcmEncrypt(enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  9829. resultT + 1, sizeof(t1) - 1, a, sizeof(a));
  9830. #if defined(WOLFSSL_ASYNC_CRYPT)
  9831. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9832. #endif
  9833. if (result != 0)
  9834. ERROR_OUT(-6330, out);
  9835. if (XMEMCMP(c1, resultC, sizeof(c1)))
  9836. ERROR_OUT(-6331, out);
  9837. if (XMEMCMP(t1, resultT + 1, sizeof(t1) - 1))
  9838. ERROR_OUT(-6332, out);
  9839. #ifdef HAVE_AES_DECRYPT
  9840. result = wc_AesGcmDecrypt(enc, resultP, resultC, sizeof(p),
  9841. iv1, sizeof(iv1), resultT + 1, sizeof(t1) - 1, a, sizeof(a));
  9842. #if defined(WOLFSSL_ASYNC_CRYPT)
  9843. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9844. #endif
  9845. if (result != 0)
  9846. ERROR_OUT(-6333, out);
  9847. if (XMEMCMP(p, resultP, sizeof(p)))
  9848. ERROR_OUT(-6334, out);
  9849. #endif /* HAVE_AES_DECRYPT */
  9850. #endif /* WOLFSSL_AES_256 */
  9851. #if !defined(HAVE_FIPS) || \
  9852. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  9853. /* Test encrypt with internally generated IV */
  9854. #if defined(WOLFSSL_AES_256) && !(defined(WC_NO_RNG) || defined(HAVE_SELFTEST)) \
  9855. && !(defined(WOLF_CRYPTO_CB) && defined(HAVE_CAVIUM_OCTEON_SYNC))
  9856. {
  9857. WC_RNG rng;
  9858. byte randIV[12];
  9859. result = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  9860. if (result != 0)
  9861. ERROR_OUT(-6335, out);
  9862. XMEMSET(randIV, 0, sizeof(randIV));
  9863. XMEMSET(resultT, 0, sizeof(resultT));
  9864. XMEMSET(resultC, 0, sizeof(resultC));
  9865. XMEMSET(resultP, 0, sizeof(resultP));
  9866. wc_AesGcmSetKey(enc, k1, sizeof(k1));
  9867. result = wc_AesGcmSetIV(enc, sizeof(randIV), NULL, 0, &rng);
  9868. if (result != 0)
  9869. ERROR_OUT(-6336, out);
  9870. result = wc_AesGcmEncrypt_ex(enc,
  9871. resultC, p, sizeof(p),
  9872. randIV, sizeof(randIV),
  9873. resultT, sizeof(t1),
  9874. a, sizeof(a));
  9875. #if defined(WOLFSSL_ASYNC_CRYPT)
  9876. result = wc_AsyncWait(result, &enc->asyncDev, WC_ASYNC_FLAG_NONE);
  9877. #endif
  9878. if (result != 0)
  9879. ERROR_OUT(-6337, out);
  9880. /* Check the IV has been set. */
  9881. {
  9882. word32 i, ivSum = 0;
  9883. for (i = 0; i < sizeof(randIV); i++)
  9884. ivSum += randIV[i];
  9885. if (ivSum == 0)
  9886. ERROR_OUT(-6338, out);
  9887. }
  9888. #ifdef HAVE_AES_DECRYPT
  9889. wc_AesGcmSetKey(dec, k1, sizeof(k1));
  9890. result = wc_AesGcmSetIV(dec, sizeof(randIV), NULL, 0, &rng);
  9891. if (result != 0)
  9892. ERROR_OUT(-6339, out);
  9893. result = wc_AesGcmDecrypt(dec,
  9894. resultP, resultC, sizeof(c1),
  9895. randIV, sizeof(randIV),
  9896. resultT, sizeof(t1),
  9897. a, sizeof(a));
  9898. #if defined(WOLFSSL_ASYNC_CRYPT)
  9899. result = wc_AsyncWait(result, &dec->asyncDev, WC_ASYNC_FLAG_NONE);
  9900. #endif
  9901. if (result != 0)
  9902. ERROR_OUT(-6340, out);
  9903. if (XMEMCMP(p, resultP, sizeof(p)))
  9904. ERROR_OUT(-6341, out);
  9905. #endif /* HAVE_AES_DECRYPT */
  9906. wc_FreeRng(&rng);
  9907. }
  9908. #endif /* WOLFSSL_AES_256 && !(WC_NO_RNG || HAVE_SELFTEST) */
  9909. #endif /* HAVE_FIPS_VERSION >= 2 */
  9910. #if !defined(WOLFSSL_AFALG_XILINX_AES) && !defined(WOLFSSL_XILINX_CRYPT)
  9911. #ifdef WOLFSSL_AES_256
  9912. #ifdef WOLFSSL_AESGCM_STREAM
  9913. result = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  9914. if (result != 0)
  9915. ERROR_OUT(-6360, out);
  9916. result = wc_AesGcmEncryptUpdate(enc, resultC, p, sizeof(p), a, sizeof(a));
  9917. if (result != 0)
  9918. ERROR_OUT(-6361, out);
  9919. result = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  9920. if (result != 0)
  9921. ERROR_OUT(-6362, out);
  9922. if (XMEMCMP(resultC, c1, sizeof(c1)) != 0)
  9923. ERROR_OUT(-6363, out);
  9924. if (XMEMCMP(resultT, t1, sizeof(t1)) != 0)
  9925. ERROR_OUT(-6364, out);
  9926. #ifdef HAVE_AES_DECRYPT
  9927. result = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  9928. if (result != 0)
  9929. ERROR_OUT(-6370, out);
  9930. result = wc_AesGcmDecryptUpdate(enc, resultP, c1, sizeof(c1), a, sizeof(a));
  9931. if (result != 0)
  9932. ERROR_OUT(-6371, out);
  9933. result = wc_AesGcmDecryptFinal(enc, t1, sizeof(t1));
  9934. if (result != 0)
  9935. ERROR_OUT(-6372, out);
  9936. if (XMEMCMP(resultP, p, sizeof(p)) != 0)
  9937. ERROR_OUT(-6373, out);
  9938. #endif
  9939. /* alen is the size to pass in with each update. */
  9940. for (alen = 1; alen < AES_BLOCK_SIZE + 1; alen++) {
  9941. result = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  9942. if (result != 0)
  9943. ERROR_OUT(-6380, out);
  9944. /* plen is the offset into AAD to update with. */
  9945. for (plen = 0; plen < (int)sizeof(a); plen += alen) {
  9946. int len = sizeof(a) - plen;
  9947. if (len > alen) len = alen;
  9948. result = wc_AesGcmEncryptUpdate(enc, NULL, NULL, 0, a + plen, len);
  9949. if (result != 0)
  9950. ERROR_OUT(-6381, out);
  9951. }
  9952. /* plen is the offset into plaintext to update with. */
  9953. for (plen = 0; plen < (int)sizeof(p); plen += alen) {
  9954. int len = sizeof(p) - plen;
  9955. if (len > alen) len = alen;
  9956. result = wc_AesGcmEncryptUpdate(enc, resultC + plen, p + plen, len,
  9957. NULL, 0);
  9958. if (result != 0)
  9959. ERROR_OUT(-6382, out);
  9960. }
  9961. result = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  9962. if (result != 0)
  9963. ERROR_OUT(-6383, out);
  9964. if (XMEMCMP(resultC, c1, sizeof(c1)) != 0)
  9965. ERROR_OUT(-6384, out);
  9966. if (XMEMCMP(resultT, t1, sizeof(t1)) != 0)
  9967. ERROR_OUT(-6385, out);
  9968. }
  9969. #ifdef HAVE_AES_DECRYPT
  9970. for (alen = 1; alen < AES_BLOCK_SIZE + 1; alen++) {
  9971. result = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  9972. if (result != 0)
  9973. ERROR_OUT(-6390, out);
  9974. /* plen is the offset into AAD to update with. */
  9975. for (plen = 0; plen < (int)sizeof(a); plen += alen) {
  9976. int len = sizeof(a) - plen;
  9977. if (len > alen) len = alen;
  9978. result = wc_AesGcmDecryptUpdate(enc, NULL, NULL, 0, a + plen, len);
  9979. if (result != 0)
  9980. ERROR_OUT(-6391, out);
  9981. }
  9982. /* plen is the offset into cipher text to update with. */
  9983. for (plen = 0; plen < (int)sizeof(c1); plen += alen) {
  9984. int len = sizeof(c1) - plen;
  9985. if (len > alen) len = alen;
  9986. result = wc_AesGcmDecryptUpdate(enc, resultP + plen, c1 + plen, len,
  9987. NULL, 0);
  9988. if (result != 0)
  9989. ERROR_OUT(-6392, out);
  9990. }
  9991. result = wc_AesGcmDecryptFinal(enc, t1, sizeof(t1));
  9992. if (result != 0)
  9993. ERROR_OUT(-6393, out);
  9994. if (XMEMCMP(resultP, p, sizeof(p)) != 0)
  9995. ERROR_OUT(-6394, out);
  9996. }
  9997. #endif /* HAVE_AES_DECRYPT */
  9998. #ifdef BENCH_AESGCM_LARGE
  9999. /* setup test buffer */
  10000. result = wc_AesGcmEncryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  10001. if (result != 0)
  10002. ERROR_OUT(-6360, out);
  10003. result = wc_AesGcmEncryptUpdate(enc, large_output, large_input,
  10004. BENCH_AESGCM_LARGE, a, sizeof(a));
  10005. if (result != 0)
  10006. ERROR_OUT(-6361, out);
  10007. result = wc_AesGcmEncryptFinal(enc, resultT, sizeof(t1));
  10008. if (result != 0)
  10009. ERROR_OUT(-6362, out);
  10010. #ifdef HAVE_AES_DECRYPT
  10011. result = wc_AesGcmDecryptInit(enc, k1, sizeof(k1), iv1, sizeof(iv1));
  10012. if (result != 0)
  10013. ERROR_OUT(-6363, out);
  10014. result = wc_AesGcmDecryptUpdate(enc, large_outdec, large_output,
  10015. BENCH_AESGCM_LARGE, a, sizeof(a));
  10016. if (result != 0)
  10017. ERROR_OUT(-6364, out);
  10018. result = wc_AesGcmDecryptFinal(enc, resultT, sizeof(t1));
  10019. if (result != 0)
  10020. ERROR_OUT(-6365, out);
  10021. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  10022. ERROR_OUT(-6366, out);
  10023. #endif /* HAVE_AES_DECRYPT */
  10024. #endif /* BENCH_AESGCM_LARGE */
  10025. #endif /* WOLFSSL_AESGCM_STREAM */
  10026. #endif /* WOLFSSL_AES_256 */
  10027. #endif /* !WOLFSSL_AFALG_XILINX_AES && !WOLFSSL_XILINX_CRYPT */
  10028. wc_AesFree(enc);
  10029. wc_AesFree(dec);
  10030. ret = 0;
  10031. out:
  10032. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_CAVIUM) && \
  10033. !defined(WOLFSSL_NO_MALLOC)
  10034. if (large_input)
  10035. XFREE(large_input, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10036. if (large_output)
  10037. XFREE(large_output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10038. if (large_outdec)
  10039. XFREE(large_outdec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10040. #endif
  10041. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10042. if (enc)
  10043. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  10044. if (dec)
  10045. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  10046. #endif
  10047. return ret;
  10048. }
  10049. #ifdef WOLFSSL_AES_128
  10050. WOLFSSL_TEST_SUBROUTINE int gmac_test(void)
  10051. {
  10052. int ret;
  10053. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10054. Gmac *gmac;
  10055. #else
  10056. Gmac gmac[1];
  10057. #endif
  10058. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  10059. {
  10060. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  10061. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  10062. };
  10063. WOLFSSL_SMALL_STACK_STATIC const byte iv1[] =
  10064. {
  10065. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  10066. 0xe2, 0x8c, 0x8f, 0x16
  10067. };
  10068. WOLFSSL_SMALL_STACK_STATIC const byte a1[] =
  10069. {
  10070. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  10071. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  10072. };
  10073. WOLFSSL_SMALL_STACK_STATIC const byte t1[] =
  10074. {
  10075. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  10076. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  10077. };
  10078. #if (!defined(HAVE_FIPS) || \
  10079. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
  10080. /* FIPS builds only allow 16-byte auth tags. */
  10081. /* This sample uses a 15-byte auth tag. */
  10082. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  10083. {
  10084. 0x40, 0xf7, 0xec, 0xb2, 0x52, 0x6d, 0xaa, 0xd4,
  10085. 0x74, 0x25, 0x1d, 0xf4, 0x88, 0x9e, 0xf6, 0x5b
  10086. };
  10087. WOLFSSL_SMALL_STACK_STATIC const byte iv2[] =
  10088. {
  10089. 0xee, 0x9c, 0x6e, 0x06, 0x15, 0x45, 0x45, 0x03,
  10090. 0x1a, 0x60, 0x24, 0xa7
  10091. };
  10092. WOLFSSL_SMALL_STACK_STATIC const byte a2[] =
  10093. {
  10094. 0x94, 0x81, 0x2c, 0x87, 0x07, 0x4e, 0x15, 0x18,
  10095. 0x34, 0xb8, 0x35, 0xaf, 0x1c, 0xa5, 0x7e, 0x56
  10096. };
  10097. WOLFSSL_SMALL_STACK_STATIC const byte t2[] =
  10098. {
  10099. 0xc6, 0x81, 0x79, 0x8e, 0x3d, 0xda, 0xb0, 0x9f,
  10100. 0x8d, 0x83, 0xb0, 0xbb, 0x14, 0xb6, 0x91
  10101. };
  10102. #endif
  10103. byte tag[16];
  10104. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10105. if ((gmac = (Gmac *)XMALLOC(sizeof *gmac, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10106. return -6409;
  10107. #endif
  10108. XMEMSET(gmac, 0, sizeof *gmac); /* clear context */
  10109. (void)wc_AesInit(&gmac->aes, HEAP_HINT, INVALID_DEVID); /* Make sure devId updated */
  10110. XMEMSET(tag, 0, sizeof(tag));
  10111. wc_GmacSetKey(gmac, k1, sizeof(k1));
  10112. wc_GmacUpdate(gmac, iv1, sizeof(iv1), a1, sizeof(a1), tag, sizeof(t1));
  10113. if (XMEMCMP(t1, tag, sizeof(t1)) != 0)
  10114. ERROR_OUT(-6400, out);
  10115. #if (!defined(HAVE_FIPS) || \
  10116. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)) )
  10117. XMEMSET(tag, 0, sizeof(tag));
  10118. wc_GmacSetKey(gmac, k2, sizeof(k2));
  10119. wc_GmacUpdate(gmac, iv2, sizeof(iv2), a2, sizeof(a2), tag, sizeof(t2));
  10120. if (XMEMCMP(t2, tag, sizeof(t2)) != 0)
  10121. ERROR_OUT(-6401, out);
  10122. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && !defined(NO_AES_DECRYPT)
  10123. {
  10124. WOLFSSL_SMALL_STACK_STATIC const byte badT[] =
  10125. {
  10126. 0xde, 0xad, 0xbe, 0xef, 0x17, 0x2e, 0xd0, 0x43,
  10127. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  10128. };
  10129. WC_RNG rng;
  10130. byte iv[12];
  10131. #ifndef HAVE_FIPS
  10132. if (wc_InitRng_ex(&rng, HEAP_HINT, devId) != 0)
  10133. ERROR_OUT(-6402, out);
  10134. #else
  10135. if (wc_InitRng(&rng) != 0)
  10136. ERROR_OUT(-6403, out);
  10137. #endif
  10138. if (wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  10139. t1, sizeof(t1)) != 0)
  10140. ERROR_OUT(-6404, out);
  10141. if (wc_GmacVerify(k1, sizeof(k1), iv1, sizeof(iv1), a1, sizeof(a1),
  10142. badT, sizeof(badT)) != AES_GCM_AUTH_E)
  10143. ERROR_OUT(-6405, out);
  10144. if (wc_GmacVerify(k2, sizeof(k2), iv2, sizeof(iv2), a2, sizeof(a2),
  10145. t2, sizeof(t2)) != 0)
  10146. ERROR_OUT(-6406, out);
  10147. XMEMSET(tag, 0, sizeof(tag));
  10148. XMEMSET(iv, 0, sizeof(iv));
  10149. if (wc_Gmac(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  10150. tag, sizeof(tag), &rng) != 0)
  10151. ERROR_OUT(-6407, out);
  10152. if (wc_GmacVerify(k1, sizeof(k1), iv, sizeof(iv), a1, sizeof(a1),
  10153. tag, sizeof(tag)) != 0)
  10154. ERROR_OUT(-6408, out);
  10155. wc_FreeRng(&rng);
  10156. }
  10157. #endif /* !WC_NO_RNG && !HAVE_SELFTEST && !NO_AES_DECRYPT */
  10158. #endif /* HAVE_FIPS */
  10159. ret = 0;
  10160. out:
  10161. wc_AesFree(&gmac->aes);
  10162. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10163. XFREE(gmac, HEAP_HINT, DYNAMIC_TYPE_AES);
  10164. #endif
  10165. return ret;
  10166. }
  10167. #endif /* WOLFSSL_AES_128 */
  10168. #endif /* HAVE_AESGCM */
  10169. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  10170. WOLFSSL_TEST_SUBROUTINE int aesccm_test(void)
  10171. {
  10172. int ret;
  10173. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10174. Aes *enc;
  10175. #else
  10176. Aes enc[1];
  10177. #endif
  10178. /* key */
  10179. WOLFSSL_SMALL_STACK_STATIC const byte k[] =
  10180. {
  10181. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  10182. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  10183. };
  10184. /* nonce */
  10185. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  10186. {
  10187. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  10188. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  10189. };
  10190. /* plaintext */
  10191. WOLFSSL_SMALL_STACK_STATIC const byte p[] =
  10192. {
  10193. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  10194. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10195. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  10196. };
  10197. /* plaintext - long */
  10198. WOLFSSL_SMALL_STACK_STATIC const byte pl[] =
  10199. {
  10200. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  10201. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10202. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  10203. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  10204. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  10205. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  10206. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  10207. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  10208. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  10209. 0x50
  10210. };
  10211. WOLFSSL_SMALL_STACK_STATIC const byte a[] =
  10212. {
  10213. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  10214. };
  10215. /* ciphertext */
  10216. WOLFSSL_SMALL_STACK_STATIC const byte c[] =
  10217. {
  10218. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  10219. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  10220. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  10221. };
  10222. /* tag - authentication */
  10223. WOLFSSL_SMALL_STACK_STATIC const byte t[] =
  10224. {
  10225. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  10226. };
  10227. /* ciphertext - long */
  10228. WOLFSSL_SMALL_STACK_STATIC const byte cl[] =
  10229. {
  10230. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  10231. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  10232. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84, 0xe0,
  10233. 0x44, 0x2d, 0xbe, 0x25, 0xfa, 0x48, 0x2b, 0xa8,
  10234. 0x36, 0x0b, 0xbf, 0x01, 0xc0, 0x12, 0x45, 0xa4,
  10235. 0x82, 0x9f, 0x20, 0x6c, 0xc3, 0xd6, 0xae, 0x5b,
  10236. 0x54, 0x8d, 0xd0, 0xb1, 0x69, 0x2c, 0xec, 0x5e,
  10237. 0x95, 0xa5, 0x6b, 0x48, 0xc3, 0xc6, 0xc8, 0x9e,
  10238. 0xc7, 0x92, 0x98, 0x9d, 0x26, 0x7d, 0x2a, 0x10,
  10239. 0x0b
  10240. };
  10241. /* tag - authentication - long */
  10242. WOLFSSL_SMALL_STACK_STATIC const byte tl[] =
  10243. {
  10244. 0x89, 0xd8, 0xd2, 0x02, 0xc5, 0xcf, 0xae, 0xf4
  10245. };
  10246. /* tag - authentication - empty plaintext */
  10247. WOLFSSL_SMALL_STACK_STATIC const byte t_empty[] =
  10248. {
  10249. 0xe4, 0x28, 0x8a, 0xc3, 0x78, 0x00, 0x0f, 0xf5
  10250. };
  10251. byte t2[sizeof(t)];
  10252. byte p2[sizeof(p)];
  10253. byte c2[sizeof(c)];
  10254. byte iv2[sizeof(iv)];
  10255. byte pl2[sizeof(pl)];
  10256. byte cl2[sizeof(cl)];
  10257. byte tl2[sizeof(tl)];
  10258. byte t_empty2[sizeof(t_empty)];
  10259. int result;
  10260. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10261. if ((enc = (Aes *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES)) == NULL)
  10262. return -6521;
  10263. #endif
  10264. XMEMSET(enc, 0, sizeof *enc); /* clear context */
  10265. XMEMSET(t2, 0, sizeof(t2));
  10266. XMEMSET(c2, 0, sizeof(c2));
  10267. XMEMSET(p2, 0, sizeof(p2));
  10268. result = wc_AesInit(enc, HEAP_HINT, devId);
  10269. if (result != 0)
  10270. ERROR_OUT(-6499, out);
  10271. result = wc_AesCcmSetKey(enc, k, sizeof(k));
  10272. if (result != 0)
  10273. ERROR_OUT(-6500, out);
  10274. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  10275. result = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  10276. t2, sizeof(t2), a, sizeof(a));
  10277. if (result != 0)
  10278. ERROR_OUT(-6501, out);
  10279. if (XMEMCMP(c, c2, sizeof(c2)))
  10280. ERROR_OUT(-6502, out);
  10281. if (XMEMCMP(t, t2, sizeof(t2)))
  10282. ERROR_OUT(-6503, out);
  10283. result = wc_AesCcmDecrypt(enc, p2, c2, sizeof(p2), iv, sizeof(iv),
  10284. t2, sizeof(t2), a, sizeof(a));
  10285. if (result != 0)
  10286. ERROR_OUT(-6504, out);
  10287. if (XMEMCMP(p, p2, sizeof(p2)))
  10288. ERROR_OUT(-6505, out);
  10289. /* Test the authentication failure */
  10290. t2[0]++; /* Corrupt the authentication tag. */
  10291. result = wc_AesCcmDecrypt(enc, p2, c, sizeof(p2), iv, sizeof(iv),
  10292. t2, sizeof(t2), a, sizeof(a));
  10293. if (result == 0)
  10294. ERROR_OUT(-6506, out);
  10295. /* Clear c2 to compare against p2. p2 should be set to zero in case of
  10296. * authentication fail. */
  10297. XMEMSET(c2, 0, sizeof(c2));
  10298. if (XMEMCMP(p2, c2, sizeof(p2)))
  10299. ERROR_OUT(-6507, out);
  10300. wc_AesFree(enc);
  10301. XMEMSET(enc, 0, sizeof(Aes)); /* clear context */
  10302. XMEMSET(t2, 0, sizeof(t2));
  10303. XMEMSET(c2, 0, sizeof(c2));
  10304. XMEMSET(p2, 0, sizeof(p2));
  10305. XMEMSET(iv2, 0, sizeof(iv2));
  10306. #ifndef HAVE_SELFTEST
  10307. /* selftest build does not have wc_AesCcmSetNonce() or
  10308. * wc_AesCcmEncrypt_ex() */
  10309. if (wc_AesCcmSetKey(enc, k, sizeof(k)) != 0)
  10310. ERROR_OUT(-6508, out);
  10311. if (wc_AesCcmSetNonce(enc, iv, sizeof(iv)) != 0)
  10312. ERROR_OUT(-6509, out);
  10313. if (wc_AesCcmEncrypt_ex(enc, c2, p, sizeof(c2), iv2, sizeof(iv2),
  10314. t2, sizeof(t2), a, sizeof(a)) != 0)
  10315. ERROR_OUT(-6510, out);
  10316. if (XMEMCMP(iv, iv2, sizeof(iv2)))
  10317. ERROR_OUT(-6511, out);
  10318. if (XMEMCMP(c, c2, sizeof(c2)))
  10319. ERROR_OUT(-6512, out);
  10320. if (XMEMCMP(t, t2, sizeof(t2)))
  10321. ERROR_OUT(-6513, out);
  10322. #endif
  10323. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10324. /* test fail on invalid IV sizes */
  10325. result = wc_AesCcmSetKey(enc, k, sizeof(k));
  10326. if (result != 0)
  10327. ERROR_OUT(-6514, out);
  10328. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  10329. result = wc_AesCcmEncrypt(enc, c2, p, sizeof(c2), iv, sizeof(iv),
  10330. t2, 1, a, sizeof(a));
  10331. if (result == 0) {
  10332. ERROR_OUT(-6515, out);
  10333. }
  10334. #endif
  10335. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  10336. result = wc_AesCcmEncrypt(enc, cl2, pl, sizeof(cl2), iv, sizeof(iv),
  10337. tl2, sizeof(tl2), a, sizeof(a));
  10338. if (result != 0)
  10339. ERROR_OUT(-6516, out);
  10340. if (XMEMCMP(cl, cl2, sizeof(cl2)))
  10341. ERROR_OUT(-6517, out);
  10342. if (XMEMCMP(tl, tl2, sizeof(tl2)))
  10343. ERROR_OUT(-6518, out);
  10344. result = wc_AesCcmDecrypt(enc, pl2, cl2, sizeof(pl2), iv, sizeof(iv),
  10345. tl2, sizeof(tl2), a, sizeof(a));
  10346. if (result != 0)
  10347. ERROR_OUT(-6519, out);
  10348. if (XMEMCMP(pl, pl2, sizeof(pl2)))
  10349. ERROR_OUT(-6520, out);
  10350. /* test empty message as null input or output with nonzero inSz. */
  10351. result = wc_AesCcmEncrypt(enc, pl2 /* out */, NULL /* in */, 1 /* inSz */,
  10352. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  10353. a, sizeof(a));
  10354. if (result != BAD_FUNC_ARG)
  10355. ERROR_OUT(-6527, out);
  10356. result = wc_AesCcmEncrypt(enc, NULL /* out */, (const byte *)"" /* in */, 1 /* inSz */,
  10357. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  10358. a, sizeof(a));
  10359. if (result != BAD_FUNC_ARG)
  10360. ERROR_OUT(-6528, out);
  10361. result = wc_AesCcmDecrypt(enc, pl2, NULL /* in */, 1 /* inSz */,
  10362. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  10363. sizeof(a));
  10364. if (result != BAD_FUNC_ARG)
  10365. ERROR_OUT(-6529, out);
  10366. result = wc_AesCcmDecrypt(enc, NULL /* out */, (const byte *)"" /* in */, 1 /* inSz */,
  10367. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  10368. sizeof(a));
  10369. if (result != BAD_FUNC_ARG)
  10370. ERROR_OUT(-6530, out);
  10371. /* test empty message as null input and output with zero inSz --
  10372. * must either succeed, or fail early with BAD_FUNC_ARG.
  10373. */
  10374. result = wc_AesCcmEncrypt(enc, NULL /* out */, NULL /* in */, 0 /* inSz */,
  10375. iv, sizeof(iv), t_empty2, sizeof(t_empty2),
  10376. a, sizeof(a));
  10377. if (result != BAD_FUNC_ARG) {
  10378. if (result != 0)
  10379. ERROR_OUT(-6521, out);
  10380. if (XMEMCMP(t_empty, t_empty2, sizeof(t_empty2)))
  10381. ERROR_OUT(-6522, out);
  10382. result = wc_AesCcmDecrypt(enc, NULL /* out */, NULL /* in */,
  10383. 0 /* inSz */, iv, sizeof(iv), t_empty2,
  10384. sizeof(t_empty2), a, sizeof(a));
  10385. if (result != 0)
  10386. ERROR_OUT(-6523, out);
  10387. }
  10388. /* test empty message as zero-length string -- must work. */
  10389. result = wc_AesCcmEncrypt(enc, pl2, (const byte *)"", 0 /* inSz */, iv,
  10390. sizeof(iv), t_empty2, sizeof(t_empty2), a,
  10391. sizeof(a));
  10392. if (result != 0)
  10393. ERROR_OUT(-6524, out);
  10394. if (XMEMCMP(t_empty, t_empty2, sizeof(t_empty2)))
  10395. ERROR_OUT(-6525, out);
  10396. result = wc_AesCcmDecrypt(enc, pl2, (const byte *)"", 0 /* inSz */,
  10397. iv, sizeof(iv), t_empty2, sizeof(t_empty2), a,
  10398. sizeof(a));
  10399. if (result != 0)
  10400. ERROR_OUT(-6526, out);
  10401. wc_AesFree(enc);
  10402. ret = 0;
  10403. out:
  10404. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10405. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  10406. #endif
  10407. return ret;
  10408. }
  10409. #endif /* HAVE_AESCCM WOLFSSL_AES_128 */
  10410. #ifdef HAVE_AES_KEYWRAP
  10411. #define MAX_KEYWRAP_TEST_OUTLEN 40
  10412. #define MAX_KEYWRAP_TEST_PLAINLEN 32
  10413. typedef struct keywrapVector {
  10414. const byte* kek;
  10415. const byte* data;
  10416. const byte* verify;
  10417. word32 kekLen;
  10418. word32 dataLen;
  10419. word32 verifyLen;
  10420. } keywrapVector;
  10421. WOLFSSL_TEST_SUBROUTINE int aeskeywrap_test(void)
  10422. {
  10423. int wrapSz, plainSz, testSz, i;
  10424. /* test vectors from RFC 3394 (kek, data, verify) */
  10425. #ifdef WOLFSSL_AES_128
  10426. /* Wrap 128 bits of Key Data with a 128-bit KEK */
  10427. WOLFSSL_SMALL_STACK_STATIC const byte k1[] = {
  10428. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10429. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  10430. };
  10431. WOLFSSL_SMALL_STACK_STATIC const byte d1[] = {
  10432. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10433. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  10434. };
  10435. WOLFSSL_SMALL_STACK_STATIC const byte v1[] = {
  10436. 0x1F, 0xA6, 0x8B, 0x0A, 0x81, 0x12, 0xB4, 0x47,
  10437. 0xAE, 0xF3, 0x4B, 0xD8, 0xFB, 0x5A, 0x7B, 0x82,
  10438. 0x9D, 0x3E, 0x86, 0x23, 0x71, 0xD2, 0xCF, 0xE5
  10439. };
  10440. #endif /* WOLFSSL_AES_128 */
  10441. #ifdef WOLFSSL_AES_192
  10442. /* Wrap 128 bits of Key Data with a 192-bit KEK */
  10443. WOLFSSL_SMALL_STACK_STATIC const byte k2[] = {
  10444. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10445. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  10446. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  10447. };
  10448. WOLFSSL_SMALL_STACK_STATIC const byte d2[] = {
  10449. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10450. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  10451. };
  10452. WOLFSSL_SMALL_STACK_STATIC const byte v2[] = {
  10453. 0x96, 0x77, 0x8B, 0x25, 0xAE, 0x6C, 0xA4, 0x35,
  10454. 0xF9, 0x2B, 0x5B, 0x97, 0xC0, 0x50, 0xAE, 0xD2,
  10455. 0x46, 0x8A, 0xB8, 0xA1, 0x7A, 0xD8, 0x4E, 0x5D
  10456. };
  10457. #endif
  10458. #ifdef WOLFSSL_AES_256
  10459. /* Wrap 128 bits of Key Data with a 256-bit KEK */
  10460. WOLFSSL_SMALL_STACK_STATIC const byte k3[] = {
  10461. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10462. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  10463. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10464. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  10465. };
  10466. WOLFSSL_SMALL_STACK_STATIC const byte d3[] = {
  10467. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10468. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  10469. };
  10470. WOLFSSL_SMALL_STACK_STATIC const byte v3[] = {
  10471. 0x64, 0xE8, 0xC3, 0xF9, 0xCE, 0x0F, 0x5B, 0xA2,
  10472. 0x63, 0xE9, 0x77, 0x79, 0x05, 0x81, 0x8A, 0x2A,
  10473. 0x93, 0xC8, 0x19, 0x1E, 0x7D, 0x6E, 0x8A, 0xE7
  10474. };
  10475. #endif
  10476. #ifdef WOLFSSL_AES_192
  10477. /* Wrap 192 bits of Key Data with a 192-bit KEK */
  10478. WOLFSSL_SMALL_STACK_STATIC const byte k4[] = {
  10479. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10480. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  10481. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  10482. };
  10483. WOLFSSL_SMALL_STACK_STATIC const byte d4[] = {
  10484. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10485. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  10486. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  10487. };
  10488. WOLFSSL_SMALL_STACK_STATIC const byte v4[] = {
  10489. 0x03, 0x1D, 0x33, 0x26, 0x4E, 0x15, 0xD3, 0x32,
  10490. 0x68, 0xF2, 0x4E, 0xC2, 0x60, 0x74, 0x3E, 0xDC,
  10491. 0xE1, 0xC6, 0xC7, 0xDD, 0xEE, 0x72, 0x5A, 0x93,
  10492. 0x6B, 0xA8, 0x14, 0x91, 0x5C, 0x67, 0x62, 0xD2
  10493. };
  10494. #endif
  10495. #ifdef WOLFSSL_AES_256
  10496. /* Wrap 192 bits of Key Data with a 256-bit KEK */
  10497. WOLFSSL_SMALL_STACK_STATIC const byte k5[] = {
  10498. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10499. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  10500. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10501. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  10502. };
  10503. WOLFSSL_SMALL_STACK_STATIC const byte d5[] = {
  10504. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10505. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  10506. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  10507. };
  10508. WOLFSSL_SMALL_STACK_STATIC const byte v5[] = {
  10509. 0xA8, 0xF9, 0xBC, 0x16, 0x12, 0xC6, 0x8B, 0x3F,
  10510. 0xF6, 0xE6, 0xF4, 0xFB, 0xE3, 0x0E, 0x71, 0xE4,
  10511. 0x76, 0x9C, 0x8B, 0x80, 0xA3, 0x2C, 0xB8, 0x95,
  10512. 0x8C, 0xD5, 0xD1, 0x7D, 0x6B, 0x25, 0x4D, 0xA1
  10513. };
  10514. /* Wrap 256 bits of Key Data with a 256-bit KEK */
  10515. WOLFSSL_SMALL_STACK_STATIC const byte k6[] = {
  10516. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10517. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  10518. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  10519. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  10520. };
  10521. WOLFSSL_SMALL_STACK_STATIC const byte d6[] = {
  10522. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10523. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  10524. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10525. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  10526. };
  10527. WOLFSSL_SMALL_STACK_STATIC const byte v6[] = {
  10528. 0x28, 0xC9, 0xF4, 0x04, 0xC4, 0xB8, 0x10, 0xF4,
  10529. 0xCB, 0xCC, 0xB3, 0x5C, 0xFB, 0x87, 0xF8, 0x26,
  10530. 0x3F, 0x57, 0x86, 0xE2, 0xD8, 0x0E, 0xD3, 0x26,
  10531. 0xCB, 0xC7, 0xF0, 0xE7, 0x1A, 0x99, 0xF4, 0x3B,
  10532. 0xFB, 0x98, 0x8B, 0x9B, 0x7A, 0x02, 0xDD, 0x21
  10533. };
  10534. #endif /* WOLFSSL_AES_256 */
  10535. byte output[MAX_KEYWRAP_TEST_OUTLEN];
  10536. byte plain [MAX_KEYWRAP_TEST_PLAINLEN];
  10537. const keywrapVector test_wrap[] =
  10538. {
  10539. #ifdef WOLFSSL_AES_128
  10540. {k1, d1, v1, sizeof(k1), sizeof(d1), sizeof(v1)},
  10541. #endif
  10542. #ifdef WOLFSSL_AES_192
  10543. {k2, d2, v2, sizeof(k2), sizeof(d2), sizeof(v2)},
  10544. #endif
  10545. #ifdef WOLFSSL_AES_256
  10546. {k3, d3, v3, sizeof(k3), sizeof(d3), sizeof(v3)},
  10547. #endif
  10548. #ifdef WOLFSSL_AES_192
  10549. {k4, d4, v4, sizeof(k4), sizeof(d4), sizeof(v4)},
  10550. #endif
  10551. #ifdef WOLFSSL_AES_256
  10552. {k5, d5, v5, sizeof(k5), sizeof(d5), sizeof(v5)},
  10553. {k6, d6, v6, sizeof(k6), sizeof(d6), sizeof(v6)}
  10554. #endif
  10555. };
  10556. testSz = sizeof(test_wrap) / sizeof(keywrapVector);
  10557. XMEMSET(output, 0, sizeof(output));
  10558. XMEMSET(plain, 0, sizeof(plain));
  10559. for (i = 0; i < testSz; i++) {
  10560. wrapSz = wc_AesKeyWrap(test_wrap[i].kek, test_wrap[i].kekLen,
  10561. test_wrap[i].data, test_wrap[i].dataLen,
  10562. output, sizeof(output), NULL);
  10563. if ( (wrapSz < 0) || (wrapSz != (int)test_wrap[i].verifyLen) )
  10564. return -6600;
  10565. if (XMEMCMP(output, test_wrap[i].verify, test_wrap[i].verifyLen) != 0)
  10566. return -6601;
  10567. plainSz = wc_AesKeyUnWrap((byte*)test_wrap[i].kek, test_wrap[i].kekLen,
  10568. output, wrapSz,
  10569. plain, sizeof(plain), NULL);
  10570. if ( (plainSz < 0) || (plainSz != (int)test_wrap[i].dataLen) )
  10571. return -6602;
  10572. if (XMEMCMP(plain, test_wrap[i].data, test_wrap[i].dataLen) != 0)
  10573. return -6603 - i;
  10574. }
  10575. return 0;
  10576. }
  10577. #endif /* HAVE_AES_KEYWRAP */
  10578. #endif /* NO_AES */
  10579. #ifdef HAVE_CAMELLIA
  10580. enum {
  10581. CAM_ECB_ENC, CAM_ECB_DEC, CAM_CBC_ENC, CAM_CBC_DEC
  10582. };
  10583. typedef struct {
  10584. int type;
  10585. const byte* plaintext;
  10586. const byte* iv;
  10587. const byte* ciphertext;
  10588. const byte* key;
  10589. word32 keySz;
  10590. int errorCode;
  10591. } test_vector_t;
  10592. WOLFSSL_TEST_SUBROUTINE int camellia_test(void)
  10593. {
  10594. /* Camellia ECB Test Plaintext */
  10595. WOLFSSL_SMALL_STACK_STATIC const byte pte[] =
  10596. {
  10597. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10598. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  10599. };
  10600. /* Camellia ECB Test Initialization Vector */
  10601. WOLFSSL_SMALL_STACK_STATIC const byte ive[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  10602. /* Test 1: Camellia ECB 128-bit key */
  10603. WOLFSSL_SMALL_STACK_STATIC const byte k1[] =
  10604. {
  10605. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10606. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  10607. };
  10608. WOLFSSL_SMALL_STACK_STATIC const byte c1[] =
  10609. {
  10610. 0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73,
  10611. 0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43
  10612. };
  10613. /* Test 2: Camellia ECB 192-bit key */
  10614. WOLFSSL_SMALL_STACK_STATIC const byte k2[] =
  10615. {
  10616. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10617. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  10618. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  10619. };
  10620. WOLFSSL_SMALL_STACK_STATIC const byte c2[] =
  10621. {
  10622. 0xb4, 0x99, 0x34, 0x01, 0xb3, 0xe9, 0x96, 0xf8,
  10623. 0x4e, 0xe5, 0xce, 0xe7, 0xd7, 0x9b, 0x09, 0xb9
  10624. };
  10625. /* Test 3: Camellia ECB 256-bit key */
  10626. WOLFSSL_SMALL_STACK_STATIC const byte k3[] =
  10627. {
  10628. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  10629. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  10630. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  10631. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  10632. };
  10633. WOLFSSL_SMALL_STACK_STATIC const byte c3[] =
  10634. {
  10635. 0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c,
  10636. 0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09
  10637. };
  10638. /* Camellia CBC Test Plaintext */
  10639. WOLFSSL_SMALL_STACK_STATIC const byte ptc[] =
  10640. {
  10641. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  10642. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  10643. };
  10644. /* Camellia CBC Test Initialization Vector */
  10645. WOLFSSL_SMALL_STACK_STATIC const byte ivc[] =
  10646. {
  10647. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  10648. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  10649. };
  10650. /* Test 4: Camellia-CBC 128-bit key */
  10651. WOLFSSL_SMALL_STACK_STATIC const byte k4[] =
  10652. {
  10653. 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
  10654. 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
  10655. };
  10656. WOLFSSL_SMALL_STACK_STATIC const byte c4[] =
  10657. {
  10658. 0x16, 0x07, 0xCF, 0x49, 0x4B, 0x36, 0xBB, 0xF0,
  10659. 0x0D, 0xAE, 0xB0, 0xB5, 0x03, 0xC8, 0x31, 0xAB
  10660. };
  10661. /* Test 5: Camellia-CBC 192-bit key */
  10662. WOLFSSL_SMALL_STACK_STATIC const byte k5[] =
  10663. {
  10664. 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52,
  10665. 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5,
  10666. 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B
  10667. };
  10668. WOLFSSL_SMALL_STACK_STATIC const byte c5[] =
  10669. {
  10670. 0x2A, 0x48, 0x30, 0xAB, 0x5A, 0xC4, 0xA1, 0xA2,
  10671. 0x40, 0x59, 0x55, 0xFD, 0x21, 0x95, 0xCF, 0x93
  10672. };
  10673. /* Test 6: CBC 256-bit key */
  10674. WOLFSSL_SMALL_STACK_STATIC const byte k6[] =
  10675. {
  10676. 0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE,
  10677. 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
  10678. 0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7,
  10679. 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4
  10680. };
  10681. WOLFSSL_SMALL_STACK_STATIC const byte c6[] =
  10682. {
  10683. 0xE6, 0xCF, 0xA3, 0x5F, 0xC0, 0x2B, 0x13, 0x4A,
  10684. 0x4D, 0x2C, 0x0B, 0x67, 0x37, 0xAC, 0x3E, 0xDA
  10685. };
  10686. byte out[CAMELLIA_BLOCK_SIZE];
  10687. Camellia cam;
  10688. int i, testsSz, ret;
  10689. WOLFSSL_SMALL_STACK_STATIC const test_vector_t testVectors[] =
  10690. {
  10691. {CAM_ECB_ENC, pte, ive, c1, k1, sizeof(k1), -114},
  10692. {CAM_ECB_ENC, pte, ive, c2, k2, sizeof(k2), -115},
  10693. {CAM_ECB_ENC, pte, ive, c3, k3, sizeof(k3), -116},
  10694. {CAM_ECB_DEC, pte, ive, c1, k1, sizeof(k1), -117},
  10695. {CAM_ECB_DEC, pte, ive, c2, k2, sizeof(k2), -118},
  10696. {CAM_ECB_DEC, pte, ive, c3, k3, sizeof(k3), -119},
  10697. {CAM_CBC_ENC, ptc, ivc, c4, k4, sizeof(k4), -120},
  10698. {CAM_CBC_ENC, ptc, ivc, c5, k5, sizeof(k5), -121},
  10699. {CAM_CBC_ENC, ptc, ivc, c6, k6, sizeof(k6), -122},
  10700. {CAM_CBC_DEC, ptc, ivc, c4, k4, sizeof(k4), -123},
  10701. {CAM_CBC_DEC, ptc, ivc, c5, k5, sizeof(k5), -124},
  10702. {CAM_CBC_DEC, ptc, ivc, c6, k6, sizeof(k6), -125}
  10703. };
  10704. testsSz = sizeof(testVectors)/sizeof(test_vector_t);
  10705. for (i = 0; i < testsSz; i++) {
  10706. if (wc_CamelliaSetKey(&cam, testVectors[i].key, testVectors[i].keySz,
  10707. testVectors[i].iv) != 0)
  10708. return testVectors[i].errorCode;
  10709. switch (testVectors[i].type) {
  10710. case CAM_ECB_ENC:
  10711. ret = wc_CamelliaEncryptDirect(&cam, out,
  10712. testVectors[i].plaintext);
  10713. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  10714. CAMELLIA_BLOCK_SIZE))
  10715. return testVectors[i].errorCode;
  10716. break;
  10717. case CAM_ECB_DEC:
  10718. ret = wc_CamelliaDecryptDirect(&cam, out,
  10719. testVectors[i].ciphertext);
  10720. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  10721. CAMELLIA_BLOCK_SIZE))
  10722. return testVectors[i].errorCode;
  10723. break;
  10724. case CAM_CBC_ENC:
  10725. ret = wc_CamelliaCbcEncrypt(&cam, out, testVectors[i].plaintext,
  10726. CAMELLIA_BLOCK_SIZE);
  10727. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  10728. CAMELLIA_BLOCK_SIZE))
  10729. return testVectors[i].errorCode;
  10730. break;
  10731. case CAM_CBC_DEC:
  10732. ret = wc_CamelliaCbcDecrypt(&cam, out,
  10733. testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE);
  10734. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  10735. CAMELLIA_BLOCK_SIZE))
  10736. return testVectors[i].errorCode;
  10737. break;
  10738. default:
  10739. break;
  10740. }
  10741. }
  10742. /* Setting the IV and checking it was actually set. */
  10743. ret = wc_CamelliaSetIV(&cam, ivc);
  10744. if (ret != 0 || XMEMCMP(cam.reg, ivc, CAMELLIA_BLOCK_SIZE))
  10745. return -6700;
  10746. /* Setting the IV to NULL should be same as all zeros IV */
  10747. if (wc_CamelliaSetIV(&cam, NULL) != 0 ||
  10748. XMEMCMP(cam.reg, ive, CAMELLIA_BLOCK_SIZE))
  10749. return -6701;
  10750. /* First parameter should never be null */
  10751. if (wc_CamelliaSetIV(NULL, NULL) == 0)
  10752. return -6702;
  10753. /* First parameter should never be null, check it fails */
  10754. if (wc_CamelliaSetKey(NULL, k1, sizeof(k1), NULL) == 0)
  10755. return -6703;
  10756. /* Key should have a size of 16, 24, or 32 */
  10757. if (wc_CamelliaSetKey(&cam, k1, 0, NULL) == 0)
  10758. return -6704;
  10759. return 0;
  10760. }
  10761. #endif /* HAVE_CAMELLIA */
  10762. #ifdef HAVE_XCHACHA
  10763. WOLFSSL_TEST_SUBROUTINE int XChaCha_test(void) {
  10764. int ret = -6830;
  10765. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  10766. 0x54, 0x68, 0x65, 0x20, 0x64, 0x68, 0x6f, 0x6c, 0x65, 0x20, 0x28, 0x70, 0x72, 0x6f, 0x6e, 0x6f, /* The dhole (prono */
  10767. 0x75, 0x6e, 0x63, 0x65, 0x64, 0x20, 0x22, 0x64, 0x6f, 0x6c, 0x65, 0x22, 0x29, 0x20, 0x69, 0x73, /* unced "dole") is */
  10768. 0x20, 0x61, 0x6c, 0x73, 0x6f, 0x20, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x20, 0x61, 0x73, 0x20, 0x74, /* also known as t */
  10769. 0x68, 0x65, 0x20, 0x41, 0x73, 0x69, 0x61, 0x74, 0x69, 0x63, 0x20, 0x77, 0x69, 0x6c, 0x64, 0x20, /* he Asiatic wild */
  10770. 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x72, 0x65, 0x64, 0x20, 0x64, 0x6f, 0x67, 0x2c, 0x20, 0x61, 0x6e, /* dog, red dog, an */
  10771. 0x64, 0x20, 0x77, 0x68, 0x69, 0x73, 0x74, 0x6c, 0x69, 0x6e, 0x67, 0x20, 0x64, 0x6f, 0x67, 0x2e, /* d whistling dog. */
  10772. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x61, 0x62, 0x6f, 0x75, 0x74, 0x20, 0x74, 0x68, 0x65, /* It is about the */
  10773. 0x20, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x6f, 0x66, 0x20, 0x61, 0x20, 0x47, 0x65, 0x72, 0x6d, 0x61, /* size of a Germa */
  10774. 0x6e, 0x20, 0x73, 0x68, 0x65, 0x70, 0x68, 0x65, 0x72, 0x64, 0x20, 0x62, 0x75, 0x74, 0x20, 0x6c, /* n shepherd but l */
  10775. 0x6f, 0x6f, 0x6b, 0x73, 0x20, 0x6d, 0x6f, 0x72, 0x65, 0x20, 0x6c, 0x69, 0x6b, 0x65, 0x20, 0x61, /* ooks more like a */
  10776. 0x20, 0x6c, 0x6f, 0x6e, 0x67, 0x2d, 0x6c, 0x65, 0x67, 0x67, 0x65, 0x64, 0x20, 0x66, 0x6f, 0x78, /* long-legged fox */
  10777. 0x2e, 0x20, 0x54, 0x68, 0x69, 0x73, 0x20, 0x68, 0x69, 0x67, 0x68, 0x6c, 0x79, 0x20, 0x65, 0x6c, /* . This highly el */
  10778. 0x75, 0x73, 0x69, 0x76, 0x65, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x6b, 0x69, 0x6c, 0x6c, 0x65, /* usive and skille */
  10779. 0x64, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x65, 0x72, 0x20, 0x69, 0x73, 0x20, 0x63, 0x6c, 0x61, 0x73, /* d jumper is clas */
  10780. 0x73, 0x69, 0x66, 0x69, 0x65, 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x77, 0x6f, 0x6c, 0x76, /* sified with wolv */
  10781. 0x65, 0x73, 0x2c, 0x20, 0x63, 0x6f, 0x79, 0x6f, 0x74, 0x65, 0x73, 0x2c, 0x20, 0x6a, 0x61, 0x63, /* es, coyotes, jac */
  10782. 0x6b, 0x61, 0x6c, 0x73, 0x2c, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x66, 0x6f, 0x78, 0x65, 0x73, 0x20, /* kals, and foxes */
  10783. 0x69, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x74, 0x61, 0x78, 0x6f, 0x6e, 0x6f, 0x6d, 0x69, 0x63, /* in the taxonomic */
  10784. 0x20, 0x66, 0x61, 0x6d, 0x69, 0x6c, 0x79, 0x20, 0x43, 0x61, 0x6e, 0x69, 0x64, 0x61, 0x65, 0x2e /* family Canidae. */
  10785. };
  10786. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  10787. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  10788. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  10789. };
  10790. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  10791. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  10792. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x58 }; /* PQRSTUVW */
  10793. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  10794. 0x45, 0x59, 0xab, 0xba, 0x4e, 0x48, 0xc1, 0x61, 0x02, 0xe8, 0xbb, 0x2c, 0x05, 0xe6, 0x94, 0x7f,
  10795. 0x50, 0xa7, 0x86, 0xde, 0x16, 0x2f, 0x9b, 0x0b, 0x7e, 0x59, 0x2a, 0x9b, 0x53, 0xd0, 0xd4, 0xe9,
  10796. 0x8d, 0x8d, 0x64, 0x10, 0xd5, 0x40, 0xa1, 0xa6, 0x37, 0x5b, 0x26, 0xd8, 0x0d, 0xac, 0xe4, 0xfa,
  10797. 0xb5, 0x23, 0x84, 0xc7, 0x31, 0xac, 0xbf, 0x16, 0xa5, 0x92, 0x3c, 0x0c, 0x48, 0xd3, 0x57, 0x5d,
  10798. 0x4d, 0x0d, 0x2c, 0x67, 0x3b, 0x66, 0x6f, 0xaa, 0x73, 0x10, 0x61, 0x27, 0x77, 0x01, 0x09, 0x3a,
  10799. 0x6b, 0xf7, 0xa1, 0x58, 0xa8, 0x86, 0x42, 0x92, 0xa4, 0x1c, 0x48, 0xe3, 0xa9, 0xb4, 0xc0, 0xda,
  10800. 0xec, 0xe0, 0xf8, 0xd9, 0x8d, 0x0d, 0x7e, 0x05, 0xb3, 0x7a, 0x30, 0x7b, 0xbb, 0x66, 0x33, 0x31,
  10801. 0x64, 0xec, 0x9e, 0x1b, 0x24, 0xea, 0x0d, 0x6c, 0x3f, 0xfd, 0xdc, 0xec, 0x4f, 0x68, 0xe7, 0x44,
  10802. 0x30, 0x56, 0x19, 0x3a, 0x03, 0xc8, 0x10, 0xe1, 0x13, 0x44, 0xca, 0x06, 0xd8, 0xed, 0x8a, 0x2b,
  10803. 0xfb, 0x1e, 0x8d, 0x48, 0xcf, 0xa6, 0xbc, 0x0e, 0xb4, 0xe2, 0x46, 0x4b, 0x74, 0x81, 0x42, 0x40,
  10804. 0x7c, 0x9f, 0x43, 0x1a, 0xee, 0x76, 0x99, 0x60, 0xe1, 0x5b, 0xa8, 0xb9, 0x68, 0x90, 0x46, 0x6e,
  10805. 0xf2, 0x45, 0x75, 0x99, 0x85, 0x23, 0x85, 0xc6, 0x61, 0xf7, 0x52, 0xce, 0x20, 0xf9, 0xda, 0x0c,
  10806. 0x09, 0xab, 0x6b, 0x19, 0xdf, 0x74, 0xe7, 0x6a, 0x95, 0x96, 0x74, 0x46, 0xf8, 0xd0, 0xfd, 0x41,
  10807. 0x5e, 0x7b, 0xee, 0x2a, 0x12, 0xa1, 0x14, 0xc2, 0x0e, 0xb5, 0x29, 0x2a, 0xe7, 0xa3, 0x49, 0xae,
  10808. 0x57, 0x78, 0x20, 0xd5, 0x52, 0x0a, 0x1f, 0x3f, 0xb6, 0x2a, 0x17, 0xce, 0x6a, 0x7e, 0x68, 0xfa,
  10809. 0x7c, 0x79, 0x11, 0x1d, 0x88, 0x60, 0x92, 0x0b, 0xc0, 0x48, 0xef, 0x43, 0xfe, 0x84, 0x48, 0x6c,
  10810. 0xcb, 0x87, 0xc2, 0x5f, 0x0a, 0xe0, 0x45, 0xf0, 0xcc, 0xe1, 0xe7, 0x98, 0x9a, 0x9a, 0xa2, 0x20,
  10811. 0xa2, 0x8b, 0xdd, 0x48, 0x27, 0xe7, 0x51, 0xa2, 0x4a, 0x6d, 0x5c, 0x62, 0xd7, 0x90, 0xa6, 0x63,
  10812. 0x93, 0xb9, 0x31, 0x11, 0xc1, 0xa5, 0x5d, 0xd7, 0x42, 0x1a, 0x10, 0x18, 0x49, 0x74, 0xc7, 0xc5
  10813. };
  10814. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10815. struct ChaCha *chacha = (struct ChaCha *)XMALLOC(sizeof *chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  10816. byte *buf1 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10817. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10818. if ((chacha == NULL) || (buf1 == NULL) || (buf2 == NULL))
  10819. ERROR_OUT(MEMORY_E, out);
  10820. #else
  10821. struct ChaCha chacha[1];
  10822. byte buf1[sizeof Plaintext];
  10823. byte buf2[sizeof Plaintext];
  10824. #endif
  10825. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  10826. if (ret < 0)
  10827. ERROR_OUT(-6831, out);
  10828. ret = wc_Chacha_Process(chacha, buf1, Plaintext, sizeof Plaintext);
  10829. if (ret < 0)
  10830. ERROR_OUT(-6832, out);
  10831. if (XMEMCMP(buf1, Ciphertext, sizeof Plaintext))
  10832. ERROR_OUT(-6833, out);
  10833. ret = wc_XChacha_SetKey(chacha, Key, sizeof Key, IV, sizeof IV, 0);
  10834. if (ret < 0)
  10835. ERROR_OUT(-6834, out);
  10836. ret = wc_Chacha_Process(chacha, buf2, buf1, sizeof Plaintext);
  10837. if (ret < 0)
  10838. ERROR_OUT(-6835, out);
  10839. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  10840. ERROR_OUT(-6836, out);
  10841. out:
  10842. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10843. if (chacha)
  10844. XFREE(chacha, HEAP_HINT, DYNAMIC_TYPE_CIPHER);
  10845. if (buf1)
  10846. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10847. if (buf2)
  10848. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10849. #endif
  10850. return ret;
  10851. }
  10852. #endif /* HAVE_XCHACHA */
  10853. #if defined(HAVE_XCHACHA) && defined(HAVE_POLY1305)
  10854. WOLFSSL_TEST_SUBROUTINE int XChaCha20Poly1305_test(void) {
  10855. int ret;
  10856. WOLFSSL_SMALL_STACK_STATIC const byte Plaintext[] = {
  10857. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c, /* Ladies and Gentl */
  10858. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73, /* emen of the clas */
  10859. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39, 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63, /* s of '99: If I c */
  10860. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66, 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f, /* ould offer you o */
  10861. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20, 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20, /* nly one tip for */
  10862. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75, 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73, /* the future, suns */
  10863. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69, /* creen would be i */
  10864. 0x74, 0x2e }; /* t. */
  10865. WOLFSSL_SMALL_STACK_STATIC const byte AAD[] = { 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7 }; /* PQRS........ */
  10866. WOLFSSL_SMALL_STACK_STATIC const byte Key[] = {
  10867. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  10868. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  10869. };
  10870. WOLFSSL_SMALL_STACK_STATIC const byte IV[] = {
  10871. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, /* @ABCDEFGHIJKLMNO */
  10872. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57 }; /* PQRSTUVW */
  10873. WOLFSSL_SMALL_STACK_STATIC const byte Ciphertext[] = {
  10874. 0xbd, 0x6d, 0x17, 0x9d, 0x3e, 0x83, 0xd4, 0x3b, 0x95, 0x76, 0x57, 0x94, 0x93, 0xc0, 0xe9, 0x39,
  10875. 0x57, 0x2a, 0x17, 0x00, 0x25, 0x2b, 0xfa, 0xcc, 0xbe, 0xd2, 0x90, 0x2c, 0x21, 0x39, 0x6c, 0xbb,
  10876. 0x73, 0x1c, 0x7f, 0x1b, 0x0b, 0x4a, 0xa6, 0x44, 0x0b, 0xf3, 0xa8, 0x2f, 0x4e, 0xda, 0x7e, 0x39,
  10877. 0xae, 0x64, 0xc6, 0x70, 0x8c, 0x54, 0xc2, 0x16, 0xcb, 0x96, 0xb7, 0x2e, 0x12, 0x13, 0xb4, 0x52,
  10878. 0x2f, 0x8c, 0x9b, 0xa4, 0x0d, 0xb5, 0xd9, 0x45, 0xb1, 0x1b, 0x69, 0xb9, 0x82, 0xc1, 0xbb, 0x9e,
  10879. 0x3f, 0x3f, 0xac, 0x2b, 0xc3, 0x69, 0x48, 0x8f, 0x76, 0xb2, 0x38, 0x35, 0x65, 0xd3, 0xff, 0xf9,
  10880. 0x21, 0xf9, 0x66, 0x4c, 0x97, 0x63, 0x7d, 0xa9, 0x76, 0x88, 0x12, 0xf6, 0x15, 0xc6, 0x8b, 0x13,
  10881. 0xb5, 0x2e };
  10882. WOLFSSL_SMALL_STACK_STATIC const byte Tag[] = {
  10883. 0xc0, 0x87, 0x59, 0x24, 0xc1, 0xc7, 0x98, 0x79, 0x47, 0xde, 0xaf, 0xd8, 0x78, 0x0a, 0xcf, 0x49
  10884. };
  10885. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10886. byte *buf1 = (byte *)XMALLOC(sizeof Ciphertext + sizeof Tag, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10887. byte *buf2 = (byte *)XMALLOC(sizeof Plaintext, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10888. if ((buf1 == NULL) || (buf2 == NULL))
  10889. ERROR_OUT(-6480, out);
  10890. #else
  10891. byte buf1[sizeof Ciphertext + sizeof Tag];
  10892. byte buf2[sizeof Plaintext];
  10893. #endif
  10894. ret = wc_XChaCha20Poly1305_Encrypt(buf1, sizeof Ciphertext + sizeof Tag,
  10895. Plaintext, sizeof Plaintext,
  10896. AAD, sizeof AAD,
  10897. IV, sizeof IV,
  10898. Key, sizeof Key);
  10899. if (ret < 0)
  10900. ERROR_OUT(-6841, out);
  10901. if (XMEMCMP(buf1, Ciphertext, sizeof Ciphertext))
  10902. ERROR_OUT(-6842, out);
  10903. if (XMEMCMP(buf1 + sizeof Ciphertext, Tag, CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE))
  10904. ERROR_OUT(-6843, out);
  10905. ret = wc_XChaCha20Poly1305_Decrypt(buf2, sizeof Plaintext,
  10906. buf1, sizeof Ciphertext + sizeof Tag,
  10907. AAD, sizeof AAD,
  10908. IV, sizeof IV,
  10909. Key, sizeof Key);
  10910. if (ret < 0)
  10911. ERROR_OUT(-6844, out);
  10912. if (XMEMCMP(buf2, Plaintext, sizeof Plaintext))
  10913. ERROR_OUT(-6845, out);
  10914. out:
  10915. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  10916. if (buf1 != NULL)
  10917. XFREE(buf1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10918. if (buf2 != NULL)
  10919. XFREE(buf2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10920. #endif
  10921. return ret;
  10922. }
  10923. #endif /* defined(HAVE_XCHACHA) && defined(HAVE_POLY1305) */
  10924. #ifndef WC_NO_RNG
  10925. static int _rng_test(WC_RNG* rng, int errorOffset)
  10926. {
  10927. byte block[32];
  10928. int ret, i;
  10929. XMEMSET(block, 0, sizeof(block));
  10930. ret = wc_RNG_GenerateBlock(rng, block, sizeof(block));
  10931. if (ret != 0) {
  10932. ret = -6850;
  10933. goto exit;
  10934. }
  10935. /* Check for 0's */
  10936. for (i=0; i<(int)sizeof(block); i++) {
  10937. if (block[i] == 0) {
  10938. ret++;
  10939. }
  10940. }
  10941. /* All zeros count check */
  10942. if (ret >= (int)sizeof(block)) {
  10943. ret = -6851;
  10944. goto exit;
  10945. }
  10946. ret = wc_RNG_GenerateByte(rng, block);
  10947. if (ret != 0) {
  10948. ret = -6852;
  10949. goto exit;
  10950. }
  10951. /* Parameter validation testing. */
  10952. ret = wc_RNG_GenerateBlock(NULL, block, sizeof(block));
  10953. if (ret != BAD_FUNC_ARG) {
  10954. ret = -6853;
  10955. goto exit;
  10956. }
  10957. ret = wc_RNG_GenerateBlock(rng, NULL, sizeof(block));
  10958. if (ret != BAD_FUNC_ARG) {
  10959. ret = -6854;
  10960. goto exit;
  10961. }
  10962. ret = wc_RNG_GenerateByte(NULL, block);
  10963. if (ret != BAD_FUNC_ARG) {
  10964. ret = -6855;
  10965. goto exit;
  10966. }
  10967. ret = wc_RNG_GenerateByte(rng, NULL);
  10968. if (ret != BAD_FUNC_ARG) {
  10969. ret = -6856;
  10970. goto exit;
  10971. }
  10972. ret = 0;
  10973. exit:
  10974. if (ret != 0)
  10975. ret += errorOffset;
  10976. return ret;
  10977. }
  10978. static int random_rng_test(void)
  10979. {
  10980. WC_RNG localRng;
  10981. WC_RNG* rng;
  10982. int ret;
  10983. rng = &localRng;
  10984. /* Test stack based RNG. */
  10985. #ifndef HAVE_FIPS
  10986. ret = wc_InitRng_ex(rng, HEAP_HINT, devId);
  10987. #else
  10988. ret = wc_InitRng(rng);
  10989. #endif
  10990. if (ret != 0) return -6900;
  10991. ret = _rng_test(rng, -6300);
  10992. /* Make sure and free RNG */
  10993. wc_FreeRng(rng);
  10994. if (ret != 0) return ret;
  10995. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC)
  10996. {
  10997. byte nonce[8] = { 0 };
  10998. /* Test dynamic RNG. */
  10999. rng = wc_rng_new(nonce, (word32)sizeof(nonce), HEAP_HINT);
  11000. if (rng == NULL) return -6901;
  11001. ret = _rng_test(rng, -6310);
  11002. wc_rng_free(rng);
  11003. }
  11004. #endif
  11005. return ret;
  11006. }
  11007. #if defined(HAVE_HASHDRBG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  11008. #ifdef WC_RNG_SEED_CB
  11009. static int seed_cb(OS_Seed* os, byte* output, word32 sz)
  11010. {
  11011. word32 i;
  11012. (void)os;
  11013. /* Known answer test. Set the seed to the same value every time. */
  11014. for (i = 0; i < sz; i++)
  11015. output[i] = (byte)i;
  11016. return 0;
  11017. }
  11018. static int rng_seed_test(void)
  11019. {
  11020. #ifndef HAVE_FIPS
  11021. WOLFSSL_SMALL_STACK_STATIC const byte check[] =
  11022. {
  11023. 0x83, 0x46, 0x65, 0x2f, 0x5c, 0x44, 0x16, 0x5f,
  11024. 0xb3, 0x89, 0x26, 0xde, 0x0b, 0x6b, 0xa2, 0x06,
  11025. 0x7e, 0xa7, 0x9a, 0x55, 0x22, 0x01, 0xb0, 0x22,
  11026. 0xf4, 0x7e, 0xa2, 0x66, 0xc4, 0x08, 0x6f, 0xba
  11027. };
  11028. #else
  11029. /* FIPS uses a longer seed, so different check value. */
  11030. WOLFSSL_SMALL_STACK_STATIC const byte check[] =
  11031. {
  11032. 0xaf, 0x31, 0xcc, 0xef, 0xa9, 0x29, 0x4c, 0x24,
  11033. 0xbd, 0xa5, 0xa3, 0x52, 0x69, 0xf3, 0xb9, 0xb2,
  11034. 0x1e, 0xd4, 0x52, 0x3b, 0x9a, 0x96, 0x06, 0x20,
  11035. 0xc0, 0x5f, 0x44, 0x06, 0x1f, 0x80, 0xdf, 0xe0
  11036. };
  11037. #endif
  11038. byte output[WC_SHA256_DIGEST_SIZE];
  11039. WC_RNG rng;
  11040. int ret;
  11041. ret = wc_SetSeed_Cb(seed_cb);
  11042. if (ret != 0) {
  11043. ret = -7007;
  11044. goto exit;
  11045. }
  11046. ret = wc_InitRng(&rng);
  11047. if (ret != 0) {
  11048. ret = -7008;
  11049. goto exit;
  11050. }
  11051. ret = wc_RNG_GenerateBlock(&rng, output, sizeof(output));
  11052. if (ret != 0) {
  11053. ret = -7009;
  11054. goto exit;
  11055. }
  11056. ret = XMEMCMP(output, check, sizeof(output));
  11057. if (ret != 0) {
  11058. ret = -7010;
  11059. goto exit;
  11060. }
  11061. ret = wc_FreeRng(&rng);
  11062. if (ret != 0) {
  11063. ret = -7011;
  11064. goto exit;
  11065. }
  11066. ret = wc_SetSeed_Cb(wc_GenerateSeed);
  11067. if (ret != 0) {
  11068. ret = -7012;
  11069. }
  11070. exit:
  11071. return ret;
  11072. }
  11073. #endif
  11074. WOLFSSL_TEST_SUBROUTINE int random_test(void)
  11075. {
  11076. WOLFSSL_SMALL_STACK_STATIC const byte test1Entropy[] =
  11077. {
  11078. 0xa6, 0x5a, 0xd0, 0xf3, 0x45, 0xdb, 0x4e, 0x0e, 0xff, 0xe8, 0x75, 0xc3,
  11079. 0xa2, 0xe7, 0x1f, 0x42, 0xc7, 0x12, 0x9d, 0x62, 0x0f, 0xf5, 0xc1, 0x19,
  11080. 0xa9, 0xef, 0x55, 0xf0, 0x51, 0x85, 0xe0, 0xfb, 0x85, 0x81, 0xf9, 0x31,
  11081. 0x75, 0x17, 0x27, 0x6e, 0x06, 0xe9, 0x60, 0x7d, 0xdb, 0xcb, 0xcc, 0x2e
  11082. };
  11083. WOLFSSL_SMALL_STACK_STATIC const byte test1Output[] =
  11084. {
  11085. 0xd3, 0xe1, 0x60, 0xc3, 0x5b, 0x99, 0xf3, 0x40, 0xb2, 0x62, 0x82, 0x64,
  11086. 0xd1, 0x75, 0x10, 0x60, 0xe0, 0x04, 0x5d, 0xa3, 0x83, 0xff, 0x57, 0xa5,
  11087. 0x7d, 0x73, 0xa6, 0x73, 0xd2, 0xb8, 0xd8, 0x0d, 0xaa, 0xf6, 0xa6, 0xc3,
  11088. 0x5a, 0x91, 0xbb, 0x45, 0x79, 0xd7, 0x3f, 0xd0, 0xc8, 0xfe, 0xd1, 0x11,
  11089. 0xb0, 0x39, 0x13, 0x06, 0x82, 0x8a, 0xdf, 0xed, 0x52, 0x8f, 0x01, 0x81,
  11090. 0x21, 0xb3, 0xfe, 0xbd, 0xc3, 0x43, 0xe7, 0x97, 0xb8, 0x7d, 0xbb, 0x63,
  11091. 0xdb, 0x13, 0x33, 0xde, 0xd9, 0xd1, 0xec, 0xe1, 0x77, 0xcf, 0xa6, 0xb7,
  11092. 0x1f, 0xe8, 0xab, 0x1d, 0xa4, 0x66, 0x24, 0xed, 0x64, 0x15, 0xe5, 0x1c,
  11093. 0xcd, 0xe2, 0xc7, 0xca, 0x86, 0xe2, 0x83, 0x99, 0x0e, 0xea, 0xeb, 0x91,
  11094. 0x12, 0x04, 0x15, 0x52, 0x8b, 0x22, 0x95, 0x91, 0x02, 0x81, 0xb0, 0x2d,
  11095. 0xd4, 0x31, 0xf4, 0xc9, 0xf7, 0x04, 0x27, 0xdf
  11096. };
  11097. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyA[] =
  11098. {
  11099. 0x63, 0x36, 0x33, 0x77, 0xe4, 0x1e, 0x86, 0x46, 0x8d, 0xeb, 0x0a, 0xb4,
  11100. 0xa8, 0xed, 0x68, 0x3f, 0x6a, 0x13, 0x4e, 0x47, 0xe0, 0x14, 0xc7, 0x00,
  11101. 0x45, 0x4e, 0x81, 0xe9, 0x53, 0x58, 0xa5, 0x69, 0x80, 0x8a, 0xa3, 0x8f,
  11102. 0x2a, 0x72, 0xa6, 0x23, 0x59, 0x91, 0x5a, 0x9f, 0x8a, 0x04, 0xca, 0x68
  11103. };
  11104. WOLFSSL_SMALL_STACK_STATIC const byte test2EntropyB[] =
  11105. {
  11106. 0xe6, 0x2b, 0x8a, 0x8e, 0xe8, 0xf1, 0x41, 0xb6, 0x98, 0x05, 0x66, 0xe3,
  11107. 0xbf, 0xe3, 0xc0, 0x49, 0x03, 0xda, 0xd4, 0xac, 0x2c, 0xdf, 0x9f, 0x22,
  11108. 0x80, 0x01, 0x0a, 0x67, 0x39, 0xbc, 0x83, 0xd3
  11109. };
  11110. WOLFSSL_SMALL_STACK_STATIC const byte test2Output[] =
  11111. {
  11112. 0x04, 0xee, 0xc6, 0x3b, 0xb2, 0x31, 0xdf, 0x2c, 0x63, 0x0a, 0x1a, 0xfb,
  11113. 0xe7, 0x24, 0x94, 0x9d, 0x00, 0x5a, 0x58, 0x78, 0x51, 0xe1, 0xaa, 0x79,
  11114. 0x5e, 0x47, 0x73, 0x47, 0xc8, 0xb0, 0x56, 0x62, 0x1c, 0x18, 0xbd, 0xdc,
  11115. 0xdd, 0x8d, 0x99, 0xfc, 0x5f, 0xc2, 0xb9, 0x20, 0x53, 0xd8, 0xcf, 0xac,
  11116. 0xfb, 0x0b, 0xb8, 0x83, 0x12, 0x05, 0xfa, 0xd1, 0xdd, 0xd6, 0xc0, 0x71,
  11117. 0x31, 0x8a, 0x60, 0x18, 0xf0, 0x3b, 0x73, 0xf5, 0xed, 0xe4, 0xd4, 0xd0,
  11118. 0x71, 0xf9, 0xde, 0x03, 0xfd, 0x7a, 0xea, 0x10, 0x5d, 0x92, 0x99, 0xb8,
  11119. 0xaf, 0x99, 0xaa, 0x07, 0x5b, 0xdb, 0x4d, 0xb9, 0xaa, 0x28, 0xc1, 0x8d,
  11120. 0x17, 0x4b, 0x56, 0xee, 0x2a, 0x01, 0x4d, 0x09, 0x88, 0x96, 0xff, 0x22,
  11121. 0x82, 0xc9, 0x55, 0xa8, 0x19, 0x69, 0xe0, 0x69, 0xfa, 0x8c, 0xe0, 0x07,
  11122. 0xa1, 0x80, 0x18, 0x3a, 0x07, 0xdf, 0xae, 0x17
  11123. };
  11124. byte output[WC_SHA256_DIGEST_SIZE * 4];
  11125. int ret;
  11126. ret = wc_RNG_HealthTest(0, test1Entropy, sizeof(test1Entropy), NULL, 0,
  11127. output, sizeof(output));
  11128. if (ret != 0)
  11129. return -7000;
  11130. if (XMEMCMP(test1Output, output, sizeof(output)) != 0)
  11131. return -7001;
  11132. ret = wc_RNG_HealthTest(1, test2EntropyA, sizeof(test2EntropyA),
  11133. test2EntropyB, sizeof(test2EntropyB),
  11134. output, sizeof(output));
  11135. if (ret != 0)
  11136. return -7002;
  11137. if (XMEMCMP(test2Output, output, sizeof(output)) != 0)
  11138. return -7003;
  11139. /* Basic RNG generate block test */
  11140. if ((ret = random_rng_test()) != 0)
  11141. return ret;
  11142. /* Test the seed check function. */
  11143. #if !(defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) || \
  11144. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  11145. {
  11146. word32 i, outputSz;
  11147. /* Repeat the same byte over and over. Should fail. */
  11148. outputSz = sizeof(output);
  11149. XMEMSET(output, 1, outputSz);
  11150. ret = wc_RNG_TestSeed(output, outputSz);
  11151. if (ret == 0)
  11152. return -7004;
  11153. /* Every byte of the entropy scratch is different,
  11154. * entropy is a single byte that shouldn't match. */
  11155. outputSz = (sizeof(output) / 2) + 1;
  11156. for (i = 0; i < outputSz; i++)
  11157. output[i] = (byte)i;
  11158. ret = wc_RNG_TestSeed(output, outputSz);
  11159. if (ret != 0)
  11160. return -7005;
  11161. outputSz = sizeof(output);
  11162. for (i = 0; i < outputSz; i++)
  11163. output[i] = (byte)i;
  11164. ret = wc_RNG_TestSeed(output, outputSz);
  11165. if (ret != 0)
  11166. return -7006;
  11167. }
  11168. #endif
  11169. /* Test the seed callback. */
  11170. #ifdef WC_RNG_SEED_CB
  11171. if ((ret = rng_seed_test()) != 0)
  11172. return ret;
  11173. #endif
  11174. return 0;
  11175. }
  11176. #else
  11177. WOLFSSL_TEST_SUBROUTINE int random_test(void)
  11178. {
  11179. /* Basic RNG generate block test */
  11180. return random_rng_test();
  11181. }
  11182. #endif /* HAVE_HASHDRBG && !CUSTOM_RAND_GENERATE_BLOCK */
  11183. #endif /* WC_NO_RNG */
  11184. #ifndef MEM_TEST_SZ
  11185. #define MEM_TEST_SZ 1024
  11186. #endif
  11187. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  11188. static int simple_mem_test(int sz)
  11189. {
  11190. int ret = 0;
  11191. byte* b;
  11192. int i;
  11193. b = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11194. if (b == NULL) {
  11195. return -7110;
  11196. }
  11197. /* utilize memory */
  11198. for (i = 0; i < sz; i++) {
  11199. b[i] = (byte)i;
  11200. }
  11201. /* read back and verify */
  11202. for (i = 0; i < sz; i++) {
  11203. if (b[i] != (byte)i) {
  11204. ret = -7111;
  11205. break;
  11206. }
  11207. }
  11208. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11209. return ret;
  11210. }
  11211. #endif
  11212. WOLFSSL_TEST_SUBROUTINE int memory_test(void)
  11213. {
  11214. int ret = 0;
  11215. #if defined(COMPLEX_MEM_TEST) || defined(WOLFSSL_STATIC_MEMORY)
  11216. int i;
  11217. #endif
  11218. #ifdef WOLFSSL_STATIC_MEMORY
  11219. word32 size[] = { WOLFMEM_BUCKETS };
  11220. word32 dist[] = { WOLFMEM_DIST };
  11221. byte buffer[30000]; /* make large enough to involve many bucket sizes */
  11222. int pad = -(int)((wc_ptr_t)buffer) & (WOLFSSL_STATIC_ALIGN - 1);
  11223. /* pad to account for if head of buffer is not at set memory
  11224. * alignment when tests are ran */
  11225. #endif
  11226. #ifdef WOLFSSL_STATIC_MEMORY
  11227. /* check macro settings */
  11228. if (sizeof(size)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
  11229. return -7200;
  11230. }
  11231. if (sizeof(dist)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
  11232. return -7201;
  11233. }
  11234. for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
  11235. if ((size[i] % WOLFSSL_STATIC_ALIGN) != 0) {
  11236. /* each element in array should be divisible by alignment size */
  11237. return -7202;
  11238. }
  11239. }
  11240. for (i = 1; i < WOLFMEM_MAX_BUCKETS; i++) {
  11241. if (size[i - 1] >= size[i]) {
  11242. return -7203; /* sizes should be in increasing order */
  11243. }
  11244. }
  11245. /* check that padding size returned is possible */
  11246. if (wolfSSL_MemoryPaddingSz() < WOLFSSL_STATIC_ALIGN) {
  11247. return -7204; /* no room for wc_Memory struct */
  11248. }
  11249. if (wolfSSL_MemoryPaddingSz() < 0) {
  11250. return -7205;
  11251. }
  11252. if (wolfSSL_MemoryPaddingSz() % WOLFSSL_STATIC_ALIGN != 0) {
  11253. return -7206; /* not aligned! */
  11254. }
  11255. /* check function to return optimum buffer size (rounded down) */
  11256. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_GENERAL);
  11257. if ((ret - pad) % WOLFSSL_STATIC_ALIGN != 0) {
  11258. return -7207; /* not aligned! */
  11259. }
  11260. if (ret < 0) {
  11261. return -7208;
  11262. }
  11263. if ((unsigned int)ret > sizeof(buffer)) {
  11264. return -7209; /* did not round down as expected */
  11265. }
  11266. if (ret != wolfSSL_StaticBufferSz(buffer, ret, WOLFMEM_GENERAL)) {
  11267. return -7210; /* return value changed when using suggested value */
  11268. }
  11269. ret = wolfSSL_MemoryPaddingSz();
  11270. ret += pad; /* add space that is going to be needed if buffer not aligned */
  11271. if (wolfSSL_StaticBufferSz(buffer, size[0] + ret + 1, WOLFMEM_GENERAL) !=
  11272. (ret + (int)size[0])) {
  11273. return -7211; /* did not round down to nearest bucket value */
  11274. }
  11275. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_IO_POOL);
  11276. if ((ret - pad) < 0) {
  11277. return -7212;
  11278. }
  11279. if (((ret - pad) % (WOLFMEM_IO_SZ + wolfSSL_MemoryPaddingSz())) != 0) {
  11280. return -7213; /* not even chunks of memory for IO size */
  11281. }
  11282. if (((ret - pad) % WOLFSSL_STATIC_ALIGN) != 0) {
  11283. return -7214; /* memory not aligned */
  11284. }
  11285. /* check for passing bad or unknown arguments to functions */
  11286. if (wolfSSL_StaticBufferSz(NULL, 1, WOLFMEM_GENERAL) > 0) {
  11287. return -7215;
  11288. }
  11289. if (wolfSSL_StaticBufferSz(buffer, 1, WOLFMEM_GENERAL) != 0) {
  11290. return -7216; /* should round to 0 since struct + bucket will not fit */
  11291. }
  11292. (void)dist; /* avoid static analysis warning of variable not used */
  11293. #endif
  11294. #if defined(WOLFSSL_STATIC_MEMORY) || !defined(WOLFSSL_NO_MALLOC)
  11295. /* simple test */
  11296. ret = simple_mem_test(MEM_TEST_SZ);
  11297. if (ret != 0)
  11298. return ret;
  11299. #endif
  11300. #ifdef COMPLEX_MEM_TEST
  11301. /* test various size blocks */
  11302. for (i = 1; i < MEM_TEST_SZ; i*=2) {
  11303. ret = simple_mem_test(i);
  11304. if (ret != 0)
  11305. return ret;
  11306. }
  11307. #endif
  11308. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_NO_MALLOC)
  11309. /* realloc test */
  11310. {
  11311. byte *c = NULL;
  11312. byte *b = (byte*)XMALLOC(MEM_TEST_SZ, HEAP_HINT,
  11313. DYNAMIC_TYPE_TMP_BUFFER);
  11314. if (b) {
  11315. c = (byte*)XREALLOC(b, MEM_TEST_SZ+sizeof(word32), HEAP_HINT,
  11316. DYNAMIC_TYPE_TMP_BUFFER);
  11317. if (c)
  11318. b = c;
  11319. }
  11320. if (b)
  11321. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11322. if ((b == NULL) || (c == NULL)) {
  11323. return -7217;
  11324. }
  11325. }
  11326. #endif
  11327. return ret;
  11328. }
  11329. #ifndef NO_FILESYSTEM
  11330. /* Cert Paths */
  11331. #ifdef FREESCALE_MQX
  11332. #define CERT_PREFIX "a:\\"
  11333. #define CERT_PATH_SEP "\\"
  11334. #elif defined(WOLFSSL_uTKERNEL2)
  11335. #define CERT_PREFIX "/uda/"
  11336. #define CERT_PATH_SEP "/"
  11337. #elif defined(_WIN32_WCE)
  11338. #define CERT_PREFIX "\\windows\\"
  11339. #define CERT_PATH_SEP "\\"
  11340. #endif
  11341. #ifndef CERT_PREFIX
  11342. #define CERT_PREFIX "./"
  11343. #endif
  11344. #ifndef CERT_PATH_SEP
  11345. #define CERT_PATH_SEP "/"
  11346. #endif
  11347. #ifndef CERT_WRITE_TEMP_DIR
  11348. #define CERT_WRITE_TEMP_DIR CERT_PREFIX
  11349. #endif
  11350. #define CERT_ROOT CERT_PREFIX "certs" CERT_PATH_SEP
  11351. /* Generated Test Certs */
  11352. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  11353. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  11354. #if !defined(NO_RSA) && !defined(NO_ASN)
  11355. static const char* clientKey = CERT_ROOT "client-key.der";
  11356. static const char* clientCert = CERT_ROOT "client-cert.der";
  11357. #ifdef WOLFSSL_CERT_EXT
  11358. static const char* clientKeyPub = CERT_ROOT "client-keyPub.der";
  11359. #endif
  11360. #endif /* !NO_RSA && !NO_ASN */
  11361. #endif
  11362. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  11363. #if !defined(NO_RSA) && !defined(NO_ASN)
  11364. #if defined(WOLFSSL_CERT_GEN) || defined(HAVE_PKCS7)
  11365. static const char* rsaCaKeyFile = CERT_ROOT "ca-key.der";
  11366. #ifdef WOLFSSL_CERT_GEN
  11367. static const char* rsaCaCertFile = CERT_ROOT "ca-cert.pem";
  11368. #endif
  11369. #if defined(WOLFSSL_ALT_NAMES) || defined(HAVE_PKCS7)
  11370. static const char* rsaCaCertDerFile = CERT_ROOT "ca-cert.der";
  11371. #endif
  11372. #ifdef HAVE_PKCS7
  11373. static const char* rsaServerCertDerFile =
  11374. CERT_ROOT "server-cert.der";
  11375. static const char* rsaServerKeyDerFile =
  11376. CERT_ROOT "server-key.der";
  11377. #endif
  11378. #endif
  11379. #endif /* !NO_RSA && !NO_ASN */
  11380. #endif /* !USE_CERT_BUFFER_* */
  11381. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  11382. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  11383. #if !defined(NO_ASN) && !defined(NO_DH)
  11384. static const char* dhParamsFile = CERT_ROOT "dh2048.der";
  11385. #endif
  11386. #endif
  11387. #if !defined(NO_ASN) && !defined(NO_DH)
  11388. #if defined(WOLFSSL_DH_EXTRA) && (!defined(HAVE_FIPS) || \
  11389. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  11390. static const char* dhKeyFile = CERT_ROOT "statickeys/dh-ffdhe2048.der";
  11391. static const char* dhKeyPubFile = CERT_ROOT "statickeys/dh-ffdhe2048-pub.der";
  11392. #endif
  11393. #endif
  11394. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  11395. #ifndef NO_DSA
  11396. static const char* dsaKey = CERT_ROOT "dsa2048.der";
  11397. #endif
  11398. #endif /* !USE_CERT_BUFFER_* */
  11399. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ECC256)
  11400. #ifdef HAVE_ECC
  11401. /* cert files to be used in rsa cert gen test, check if RSA enabled */
  11402. #ifdef HAVE_ECC_KEY_IMPORT
  11403. static const char* eccKeyDerFile = CERT_ROOT "ecc-key.der";
  11404. #endif
  11405. #endif
  11406. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ASN)
  11407. #if defined(HAVE_ECC) && defined(WOLFSSL_CERT_GEN)
  11408. #ifndef NO_RSA
  11409. static const char* eccKeyPubFileDer = CERT_ROOT "ecc-keyPub.der";
  11410. #endif
  11411. static const char* eccCaKeyFile = CERT_ROOT "ca-ecc-key.der";
  11412. static const char* eccCaCertFile = CERT_ROOT "ca-ecc-cert.pem";
  11413. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  11414. static const char* eccCaKey384File =
  11415. CERT_ROOT "ca-ecc384-key.der";
  11416. static const char* eccCaCert384File =
  11417. CERT_ROOT "ca-ecc384-cert.pem";
  11418. #endif
  11419. #endif
  11420. #if defined(HAVE_PKCS7) && defined(HAVE_ECC)
  11421. static const char* eccClientKey = CERT_ROOT "ecc-client-key.der";
  11422. static const char* eccClientCert = CERT_ROOT "client-ecc-cert.der";
  11423. #endif
  11424. #endif /* HAVE_ECC */
  11425. #ifdef HAVE_ED25519
  11426. #ifdef WOLFSSL_TEST_CERT
  11427. static const char* serverEd25519Cert =
  11428. CERT_ROOT "ed25519/server-ed25519.der";
  11429. static const char* caEd25519Cert =
  11430. CERT_ROOT "ed25519/ca-ed25519.der";
  11431. #endif
  11432. #endif
  11433. #ifdef HAVE_ED448
  11434. #ifdef WOLFSSL_TEST_CERT
  11435. static const char* serverEd448Cert =
  11436. CERT_ROOT "ed448/server-ed448.der";
  11437. static const char* caEd448Cert = CERT_ROOT "ed448/ca-ed448.der";
  11438. #endif
  11439. #endif
  11440. #endif /* !USE_CERT_BUFFER_* */
  11441. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  11442. !defined(NO_FILESYSTEM)
  11443. static const char* certExtNc =
  11444. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-nc.der";
  11445. static const char* certExtIa =
  11446. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-ia.der";
  11447. static const char* certExtNct =
  11448. CERT_ROOT "test" CERT_PATH_SEP "cert-ext-nct.der";
  11449. #endif
  11450. #ifndef NO_WRITE_TEMP_FILES
  11451. #ifdef HAVE_ECC
  11452. #ifdef WOLFSSL_CERT_GEN
  11453. static const char* certEccPemFile = CERT_WRITE_TEMP_DIR "certecc.pem";
  11454. static const char* certEccDerFile = CERT_WRITE_TEMP_DIR "certecc.der";
  11455. #endif
  11456. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  11457. static const char* certEccRsaPemFile = CERT_WRITE_TEMP_DIR "certeccrsa.pem";
  11458. static const char* certEccRsaDerFile = CERT_WRITE_TEMP_DIR "certeccrsa.der";
  11459. #endif
  11460. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  11461. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  11462. static const char* eccCaKeyPemFile = CERT_WRITE_TEMP_DIR "ecc-key.pem";
  11463. static const char* eccPubKeyDerFile = CERT_WRITE_TEMP_DIR "ecc-public-key.der";
  11464. static const char* eccCaKeyTempFile = CERT_WRITE_TEMP_DIR "ecc-key.der";
  11465. #if defined(HAVE_PKCS8) && !defined(WC_NO_RNG) && \
  11466. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  11467. static const char* eccPkcs8KeyDerFile = CERT_WRITE_TEMP_DIR "ecc-key-pkcs8.der";
  11468. #endif
  11469. #endif /* HAVE_ECC_KEY_EXPORT */
  11470. #endif /* HAVE_ECC */
  11471. #ifndef NO_RSA
  11472. #ifdef WOLFSSL_CERT_GEN
  11473. static const char* otherCertDerFile = CERT_WRITE_TEMP_DIR "othercert.der";
  11474. static const char* certDerFile = CERT_WRITE_TEMP_DIR "cert.der";
  11475. static const char* otherCertPemFile = CERT_WRITE_TEMP_DIR "othercert.pem";
  11476. static const char* certPemFile = CERT_WRITE_TEMP_DIR "cert.pem";
  11477. #if defined(WOLFSSL_CERT_REQ) && !defined(WOLFSSL_NO_MALLOC)
  11478. static const char* certReqDerFile = CERT_WRITE_TEMP_DIR "certreq.der";
  11479. static const char* certReqPemFile = CERT_WRITE_TEMP_DIR "certreq.pem";
  11480. #endif
  11481. #endif
  11482. #endif /* !NO_RSA */
  11483. #if !defined(NO_RSA) || !defined(NO_DSA)
  11484. #ifdef WOLFSSL_KEY_GEN
  11485. static const char* keyDerFile = CERT_WRITE_TEMP_DIR "key.der";
  11486. static const char* keyPemFile = CERT_WRITE_TEMP_DIR "key.pem";
  11487. #endif
  11488. #endif
  11489. #endif /* !NO_WRITE_TEMP_FILES */
  11490. #endif /* !NO_FILESYSTEM */
  11491. #if defined(WOLFSSL_CERT_GEN) && (!defined(NO_RSA) || defined(HAVE_ECC)) || \
  11492. (defined(WOLFSSL_TEST_CERT) && (defined(HAVE_ED25519) || defined(HAVE_ED448)))
  11493. static CertName certDefaultName;
  11494. static void initDefaultName(void)
  11495. {
  11496. #if defined(WOLFSSL_MULTI_ATTRIB) && defined(WOLFSSL_TEST_CERT)
  11497. NameAttrib* n;
  11498. #endif
  11499. XMEMCPY(certDefaultName.country, "US", sizeof("US"));
  11500. certDefaultName.countryEnc = CTC_PRINTABLE;
  11501. XMEMCPY(certDefaultName.state, "Oregon", sizeof("Oregon"));
  11502. certDefaultName.stateEnc = CTC_UTF8;
  11503. XMEMCPY(certDefaultName.street, "Main St", sizeof("Main St"));
  11504. certDefaultName.streetEnc = CTC_UTF8;
  11505. XMEMCPY(certDefaultName.locality, "Portland", sizeof("Portland"));
  11506. certDefaultName.localityEnc = CTC_UTF8;
  11507. XMEMCPY(certDefaultName.sur, "Test", sizeof("Test"));
  11508. certDefaultName.surEnc = CTC_UTF8;
  11509. XMEMCPY(certDefaultName.org, "wolfSSL", sizeof("wolfSSL"));
  11510. certDefaultName.orgEnc = CTC_UTF8;
  11511. XMEMCPY(certDefaultName.unit, "Development", sizeof("Development"));
  11512. certDefaultName.unitEnc = CTC_UTF8;
  11513. XMEMCPY(certDefaultName.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  11514. certDefaultName.commonNameEnc = CTC_UTF8;
  11515. XMEMCPY(certDefaultName.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  11516. certDefaultName.serialDevEnc = CTC_PRINTABLE;
  11517. XMEMCPY(certDefaultName.postalCode, "12-456", sizeof("12-456"));
  11518. certDefaultName.postalCodeEnc = CTC_PRINTABLE;
  11519. #ifdef WOLFSSL_CERT_EXT
  11520. XMEMCPY(certDefaultName.busCat, "Private Organization", sizeof("Private Organization"));
  11521. certDefaultName.busCatEnc = CTC_UTF8;
  11522. XMEMCPY(certDefaultName.joiSt, "US", sizeof("US"));
  11523. certDefaultName.joiStEnc = CTC_PRINTABLE;
  11524. XMEMCPY(certDefaultName.joiC, "Oregon", sizeof("Oregon"));
  11525. certDefaultName.joiCEnc = CTC_PRINTABLE;
  11526. #endif
  11527. XMEMCPY(certDefaultName.email, "info@wolfssl.com", sizeof("info@wolfssl.com"));
  11528. XMEMCPY(certDefaultName.userId, "TestUserID", sizeof("TestUserID"));
  11529. certDefaultName.userIdEnc = CTC_PRINTABLE;
  11530. #if defined(WOLFSSL_MULTI_ATTRIB) && defined(WOLFSSL_TEST_CERT)
  11531. /* test having additional OUs and setting DC */
  11532. n = &certDefaultName.name[0];
  11533. n->id = ASN_ORGUNIT_NAME;
  11534. n->type = CTC_UTF8;
  11535. n->sz = sizeof("Development-2");
  11536. XMEMCPY(n->value, "Development-2", sizeof("Development-2"));
  11537. #if CTC_MAX_ATTRIB > 3
  11538. n = &certDefaultName.name[1];
  11539. n->id = ASN_DOMAIN_COMPONENT;
  11540. n->type = CTC_UTF8;
  11541. n->sz = sizeof("com");
  11542. XMEMCPY(n->value, "com", sizeof("com"));
  11543. n = &certDefaultName.name[2];
  11544. n->id = ASN_DOMAIN_COMPONENT;
  11545. n->type = CTC_UTF8;
  11546. n->sz = sizeof("wolfssl");
  11547. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  11548. #endif
  11549. #endif /* WOLFSSL_MULTI_ATTRIB && WOLFSSL_TEST_CERT */
  11550. #ifdef WOLFSSL_CUSTOM_OID
  11551. /* TODO: Add test case for custom OID's */
  11552. #endif
  11553. }
  11554. #ifdef WOLFSSL_CERT_EXT
  11555. #if ((defined(HAVE_ED25519) || defined(HAVE_ED448)) && \
  11556. defined(WOLFSSL_TEST_CERT)) || defined(HAVE_ECC)
  11557. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage[] =
  11558. "digitalSignature,nonRepudiation";
  11559. #endif
  11560. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  11561. WOLFSSL_SMALL_STACK_STATIC const char certKeyUsage2[] =
  11562. "digitalSignature,nonRepudiation,keyEncipherment,keyAgreement";
  11563. #endif
  11564. #endif /* WOLFSSL_CERT_EXT */
  11565. #endif /* WOLFSSL_CERT_GEN */
  11566. #ifndef NO_RSA
  11567. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT) && \
  11568. !defined(NO_FILESYSTEM)
  11569. static byte minSerial[] = { 0x02, 0x01, 0x01 };
  11570. static byte minName[] = { 0x30, 0x00 };
  11571. static byte nameBad[] = {
  11572. 0x30, 0x08,
  11573. 0x31, 0x06,
  11574. 0x30, 0x04,
  11575. 0x06, 0x02,
  11576. 0x55, 0x04,
  11577. };
  11578. static byte minDates[] = {
  11579. 0x30, 0x1e,
  11580. 0x17, 0x0d,
  11581. 0x31, 0x38, 0x30, 0x34, 0x31, 0x33, 0x31, 0x35,
  11582. 0x32, 0x33, 0x31, 0x30, 0x5a,
  11583. 0x17, 0x0d,
  11584. 0x32, 0x31, 0x30, 0x31, 0x30, 0x37, 0x31, 0x35,
  11585. 0x32, 0x33, 0x31, 0x30, 0x5a
  11586. };
  11587. static byte minPubKey[] = {
  11588. 0x30, 0x1c,
  11589. 0x30, 0x0d,
  11590. 0x06, 0x09,
  11591. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  11592. 0x01,
  11593. 0x05, 0x00,
  11594. 0x03, 0x0b,
  11595. 0x00, 0x30, 0x08,
  11596. 0x02, 0x01,
  11597. 0x03,
  11598. 0x02, 0x03,
  11599. 0x01, 0x00, 0x01
  11600. };
  11601. static byte minSigAlg[] = {
  11602. 0x30, 0x0d,
  11603. 0x06, 0x09,
  11604. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  11605. 0x0b,
  11606. 0x05, 0x00
  11607. };
  11608. static byte minSig[] = {
  11609. 0x03, 0x01,
  11610. 0x00
  11611. };
  11612. static int add_seq(byte* certData, int offset, byte* data, byte length)
  11613. {
  11614. XMEMMOVE(certData + offset + 2, data, length);
  11615. certData[offset++] = 0x30;
  11616. certData[offset++] = length;
  11617. return offset + length;
  11618. }
  11619. static int add_data(byte* certData, int offset, byte* data, byte length)
  11620. {
  11621. XMEMCPY(certData + offset, data, length);
  11622. return offset + length;
  11623. }
  11624. static int cert_asn1_test(void)
  11625. {
  11626. int ret;
  11627. int len[3];
  11628. DecodedCert cert;
  11629. byte certData[114];
  11630. byte* badCert = NULL;
  11631. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  11632. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  11633. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  11634. len[2] = add_data(certData, len[2], minDates, (byte)sizeof(minDates));
  11635. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  11636. len[2] = add_data(certData, len[2], minPubKey, (byte)sizeof(minPubKey));
  11637. len[1] = add_seq(certData, 0, certData, len[2]);
  11638. len[1] = add_data(certData, len[1], minSigAlg, (byte)sizeof(minSigAlg));
  11639. len[1] = add_data(certData, len[1], minSig, (byte)sizeof(minSig));
  11640. len[0] = add_seq(certData, 0, certData, len[1]);
  11641. /* Minimal good certificate */
  11642. InitDecodedCert(&cert, certData, len[0], 0);
  11643. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  11644. FreeDecodedCert(&cert);
  11645. if (ret != 0) {
  11646. ERROR_OUT(-7300, done);
  11647. }
  11648. /* Bad issuer name */
  11649. len[2] = add_data(certData, 0, minSerial, (byte)sizeof(minSerial));
  11650. len[2] = add_data(certData, len[2], minSigAlg, (byte)sizeof(minSigAlg));
  11651. len[2] = add_data(certData, len[2], nameBad, (byte)sizeof(nameBad));
  11652. len[2] = add_data(certData, len[2], minDates, (byte)sizeof(minDates));
  11653. len[2] = add_data(certData, len[2], minName, (byte)sizeof(minName));
  11654. len[2] = add_data(certData, len[2], minPubKey, (byte)sizeof(minPubKey));
  11655. len[1] = add_seq(certData, 0, certData, len[2]);
  11656. len[1] = add_data(certData, len[1], minSigAlg, (byte)sizeof(minSigAlg));
  11657. len[1] = add_data(certData, len[1], minSig, (byte)sizeof(minSig));
  11658. len[0] = add_seq(certData, 0, certData, len[1]);
  11659. /* Put data into allocated buffer to allow access error checking. */
  11660. badCert = (byte*)XMALLOC(len[0], HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11661. XMEMCPY(badCert, certData, len[0]);
  11662. InitDecodedCert(&cert, badCert, len[0], 0);
  11663. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  11664. FreeDecodedCert(&cert);
  11665. if (ret != ASN_PARSE_E) {
  11666. ERROR_OUT(-7301, done);
  11667. }
  11668. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11669. badCert = NULL;
  11670. ret = 0;
  11671. done:
  11672. if (badCert != NULL)
  11673. XFREE(badCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11674. return ret;
  11675. }
  11676. WOLFSSL_TEST_SUBROUTINE int cert_test(void)
  11677. {
  11678. #if !defined(NO_FILESYSTEM)
  11679. DecodedCert cert;
  11680. byte* tmp;
  11681. size_t bytes;
  11682. XFILE file;
  11683. int ret;
  11684. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11685. if (tmp == NULL)
  11686. return -7400;
  11687. /* Certificate with Name Constraints extension. */
  11688. file = XFOPEN(certExtNc, "rb");
  11689. if (!file) {
  11690. ERROR_OUT(-7401, done);
  11691. }
  11692. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11693. XFCLOSE(file);
  11694. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11695. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  11696. if (ret != 0) {
  11697. ERROR_OUT(-7402, done);
  11698. }
  11699. FreeDecodedCert(&cert);
  11700. /* Certificate with Inhibit Any Policy extension. */
  11701. file = XFOPEN(certExtIa, "rb");
  11702. if (!file) {
  11703. ERROR_OUT(-7403, done);
  11704. }
  11705. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11706. XFCLOSE(file);
  11707. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11708. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  11709. if (ret != 0) {
  11710. ERROR_OUT(-7404, done);
  11711. }
  11712. FreeDecodedCert(&cert);
  11713. /* Certificate with Netscape Certificate Type extension. */
  11714. file = XFOPEN(certExtNct, "rb");
  11715. if (!file) {
  11716. ERROR_OUT(-7405, done);
  11717. }
  11718. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11719. XFCLOSE(file);
  11720. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11721. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  11722. #ifndef IGNORE_NETSCAPE_CERT_TYPE
  11723. if (ret != 0) {
  11724. ERROR_OUT(-7406, done);
  11725. }
  11726. #else
  11727. if (ret != ASN_CRIT_EXT_E) {
  11728. ERROR_OUT(-7407, done);
  11729. }
  11730. ret = 0;
  11731. #endif
  11732. done:
  11733. FreeDecodedCert(&cert);
  11734. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11735. #endif /* !NO_FILESYSTEM */
  11736. if (ret == 0)
  11737. ret = cert_asn1_test();
  11738. return ret;
  11739. }
  11740. #endif /* WOLFSSL_TEST_CERT */
  11741. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) && \
  11742. !defined(NO_FILESYSTEM) && defined(WOLFSSL_CERT_GEN)
  11743. WOLFSSL_TEST_SUBROUTINE int certext_test(void)
  11744. {
  11745. DecodedCert cert;
  11746. byte* tmp;
  11747. size_t bytes;
  11748. XFILE file;
  11749. int ret;
  11750. /* created from rsa_test : othercert.der */
  11751. byte skid_rsa[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  11752. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  11753. /* created from rsa_test : othercert.der */
  11754. byte akid_rsa[] = "\x27\x8E\x67\x11\x74\xC3\x26\x1D\x3F\xED"
  11755. "\x33\x63\xB3\xA4\xD8\x1D\x30\xE5\xE8\xD5";
  11756. #ifdef HAVE_ECC
  11757. /* created from ecc_test_cert_gen : certecc.der */
  11758. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  11759. /* Authority key id from ./certs/ca-ecc384-cert.pem */
  11760. byte akid_ecc[] = "\xAB\xE0\xC3\x26\x4C\x18\xD4\x72\xBB\xD2"
  11761. "\x84\x8C\x9C\x0A\x05\x92\x80\x12\x53\x52";
  11762. #else
  11763. /* Authority key id from ./certs/ca-ecc-cert.pem */
  11764. byte akid_ecc[] = "\x56\x8E\x9A\xC3\xF0\x42\xDE\x18\xB9\x45"
  11765. "\x55\x6E\xF9\x93\xCF\xEA\xC3\xF3\xA5\x21";
  11766. #endif
  11767. #endif /* HAVE_ECC */
  11768. /* created from rsa_test : cert.der */
  11769. byte kid_ca[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  11770. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  11771. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11772. if (tmp == NULL)
  11773. return -7500;
  11774. /* load othercert.der (Cert signed by an authority) */
  11775. file = XFOPEN(otherCertDerFile, "rb");
  11776. if (!file) {
  11777. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  11778. return -7501;
  11779. }
  11780. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11781. XFCLOSE(file);
  11782. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11783. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  11784. if (ret != 0)
  11785. return -7502;
  11786. /* check the SKID from a RSA certificate */
  11787. if (XMEMCMP(skid_rsa, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  11788. return -7503;
  11789. /* check the AKID from an RSA certificate */
  11790. if (XMEMCMP(akid_rsa, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  11791. return -7504;
  11792. /* check the Key Usage from an RSA certificate */
  11793. if (!cert.extKeyUsageSet)
  11794. return -7505;
  11795. if (cert.extKeyUsage != (KEYUSE_KEY_ENCIPHER|KEYUSE_KEY_AGREE))
  11796. return -7506;
  11797. /* check the CA Basic Constraints from an RSA certificate */
  11798. if (cert.isCA)
  11799. return -7507;
  11800. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  11801. /* check the Certificate Policies Id */
  11802. if (cert.extCertPoliciesNb != 1)
  11803. return -7508;
  11804. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  11805. return -7509;
  11806. #endif
  11807. FreeDecodedCert(&cert);
  11808. #ifdef HAVE_ECC
  11809. /* load certecc.der (Cert signed by our ECC CA test in ecc_test_cert_gen) */
  11810. file = XFOPEN(certEccDerFile, "rb");
  11811. if (!file) {
  11812. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  11813. return -7510;
  11814. }
  11815. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11816. XFCLOSE(file);
  11817. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11818. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  11819. if (ret != 0)
  11820. return -7511;
  11821. /* check the SKID from a ECC certificate - generated dynamically */
  11822. /* check the AKID from an ECC certificate */
  11823. if (XMEMCMP(akid_ecc, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  11824. return -7512;
  11825. /* check the Key Usage from an ECC certificate */
  11826. if (!cert.extKeyUsageSet)
  11827. return -7513;
  11828. if (cert.extKeyUsage != (KEYUSE_DIGITAL_SIG|KEYUSE_CONTENT_COMMIT))
  11829. return -7514;
  11830. /* check the CA Basic Constraints from an ECC certificate */
  11831. if (cert.isCA)
  11832. return -7515;
  11833. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  11834. /* check the Certificate Policies Id */
  11835. if (cert.extCertPoliciesNb != 2)
  11836. return -7516;
  11837. if (strncmp(cert.extCertPolicies[0], "2.4.589440.587.101.2.1.9632587.1", 32))
  11838. return -7517;
  11839. if (strncmp(cert.extCertPolicies[1], "1.2.13025.489.1.113549", 22))
  11840. return -7518;
  11841. #endif
  11842. FreeDecodedCert(&cert);
  11843. #endif /* HAVE_ECC */
  11844. /* load cert.der (self signed certificate) */
  11845. file = XFOPEN(certDerFile, "rb");
  11846. if (!file) {
  11847. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  11848. return -7519;
  11849. }
  11850. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  11851. XFCLOSE(file);
  11852. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  11853. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  11854. if (ret != 0)
  11855. return -7520;
  11856. /* check the SKID from a CA certificate */
  11857. if (XMEMCMP(kid_ca, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  11858. return -7521;
  11859. /* check the AKID from an CA certificate */
  11860. if (XMEMCMP(kid_ca, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  11861. return -7522;
  11862. /* check the Key Usage from CA certificate */
  11863. if (!cert.extKeyUsageSet)
  11864. return -7523;
  11865. if (cert.extKeyUsage != (KEYUSE_KEY_CERT_SIGN|KEYUSE_CRL_SIGN))
  11866. return -7524;
  11867. /* check the CA Basic Constraints CA certificate */
  11868. if (!cert.isCA)
  11869. return -7525;
  11870. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  11871. /* check the Certificate Policies Id */
  11872. if (cert.extCertPoliciesNb != 2)
  11873. return -7526;
  11874. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  11875. return -7527;
  11876. if (strncmp(cert.extCertPolicies[1], "1.2.840.113549.1.9.16.6.5", 25))
  11877. return -7528;
  11878. #endif
  11879. FreeDecodedCert(&cert);
  11880. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  11881. return 0;
  11882. }
  11883. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_TEST_CERT &&
  11884. !NO_FILESYSTEM && WOLFSSL_CERT_GEN */
  11885. #if defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) && \
  11886. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  11887. WOLFSSL_TEST_SUBROUTINE int decodedCertCache_test(void)
  11888. {
  11889. int ret = 0;
  11890. Cert cert;
  11891. FILE* file;
  11892. byte* der;
  11893. word32 derSz;
  11894. derSz = FOURK_BUF;
  11895. der = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11896. if (der == NULL)
  11897. ret = -7600;
  11898. if (ret == 0) {
  11899. /* load cert.der */
  11900. file = XFOPEN(certDerFile, "rb");
  11901. if (file != NULL) {
  11902. derSz = (word32)XFREAD(der, 1, FOURK_BUF, file);
  11903. XFCLOSE(file);
  11904. }
  11905. else
  11906. ret = -7601;
  11907. }
  11908. if (ret == 0) {
  11909. if (wc_InitCert_ex(&cert, HEAP_HINT, devId)) {
  11910. ret = -7602;
  11911. }
  11912. }
  11913. if (ret == 0) {
  11914. ret = wc_SetSubjectBuffer(&cert, der, derSz);
  11915. }
  11916. if (ret == 0) {
  11917. if(wc_SetSubjectBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  11918. ret = -7603;
  11919. }
  11920. if (ret == 0) {
  11921. if (wc_SetSubjectRaw(&cert, der, derSz) != 0)
  11922. ret = -7604;
  11923. }
  11924. if (ret == 0) {
  11925. if(wc_SetSubjectRaw(NULL, der, derSz) != BAD_FUNC_ARG)
  11926. ret = -7605;
  11927. }
  11928. if (ret == 0) {
  11929. if(wc_SetIssuerBuffer(&cert, der, derSz) != 0)
  11930. ret = -7606;
  11931. }
  11932. if (ret == 0) {
  11933. if(wc_SetIssuerBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  11934. ret = -7607;
  11935. }
  11936. if (ret == 0) {
  11937. if(wc_SetIssuerRaw(&cert, der, derSz) != 0)
  11938. ret = -7608;
  11939. }
  11940. if (ret == 0) {
  11941. if(wc_SetIssuerRaw(NULL, der, derSz) != BAD_FUNC_ARG)
  11942. ret = -7609;
  11943. }
  11944. #ifdef WOLFSSL_ALT_NAMES
  11945. if (ret == 0) {
  11946. if(wc_SetAltNamesBuffer(&cert, der, derSz) != 0)
  11947. ret = -7610;
  11948. }
  11949. if (ret == 0) {
  11950. if(wc_SetAltNamesBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  11951. ret = -7611;
  11952. }
  11953. if (ret == 0) {
  11954. if(wc_SetDatesBuffer(&cert, der, derSz) != 0)
  11955. ret = -7612;
  11956. }
  11957. if (ret == 0) {
  11958. if(wc_SetDatesBuffer(NULL, der, derSz) != BAD_FUNC_ARG)
  11959. ret = -7613;
  11960. }
  11961. #endif
  11962. if (ret == 0) {
  11963. if(wc_SetAuthKeyIdFromCert(&cert, der, derSz) != 0)
  11964. ret = -7614;
  11965. }
  11966. if (ret == 0) {
  11967. if(wc_SetAuthKeyIdFromCert(NULL, der, derSz) != BAD_FUNC_ARG)
  11968. ret = -7615;
  11969. }
  11970. wc_SetCert_Free(&cert);
  11971. if (ret == 0) {
  11972. if(cert.decodedCert != NULL)
  11973. ret = -7616;
  11974. }
  11975. XFREE(der, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  11976. return ret;
  11977. }
  11978. #endif /* defined(WOLFSSL_CERT_GEN_CACHE) && defined(WOLFSSL_TEST_CERT) &&
  11979. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) */
  11980. #define RSA_TEST_BYTES 512 /* up to 4096-bit key */
  11981. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  11982. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  11983. static int rsa_flatten_test(RsaKey* key)
  11984. {
  11985. int ret;
  11986. byte e[RSA_TEST_BYTES];
  11987. byte n[RSA_TEST_BYTES];
  11988. word32 eSz = sizeof(e);
  11989. word32 nSz = sizeof(n);
  11990. /* Parameter Validation testing. */
  11991. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  11992. #ifdef HAVE_USER_RSA
  11993. /* Implementation using IPP Libraries returns:
  11994. * -101 = USER_CRYPTO_ERROR
  11995. */
  11996. if (ret == 0)
  11997. #else
  11998. if (ret != BAD_FUNC_ARG)
  11999. #endif
  12000. return -7620;
  12001. ret = wc_RsaFlattenPublicKey(key, NULL, &eSz, n, &nSz);
  12002. #ifdef HAVE_USER_RSA
  12003. /* Implementation using IPP Libraries returns:
  12004. * -101 = USER_CRYPTO_ERROR
  12005. */
  12006. if (ret == 0)
  12007. #else
  12008. if (ret != BAD_FUNC_ARG)
  12009. #endif
  12010. return -7621;
  12011. ret = wc_RsaFlattenPublicKey(key, e, NULL, n, &nSz);
  12012. #ifdef HAVE_USER_RSA
  12013. /* Implementation using IPP Libraries returns:
  12014. * -101 = USER_CRYPTO_ERROR
  12015. */
  12016. if (ret == 0)
  12017. #else
  12018. if (ret != BAD_FUNC_ARG)
  12019. #endif
  12020. return -7622;
  12021. ret = wc_RsaFlattenPublicKey(key, e, &eSz, NULL, &nSz);
  12022. #ifdef HAVE_USER_RSA
  12023. /* Implementation using IPP Libraries returns:
  12024. * -101 = USER_CRYPTO_ERROR
  12025. */
  12026. if (ret == 0)
  12027. #else
  12028. if (ret != BAD_FUNC_ARG)
  12029. #endif
  12030. return -7623;
  12031. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, NULL);
  12032. #ifdef HAVE_USER_RSA
  12033. /* Implementation using IPP Libraries returns:
  12034. * -101 = USER_CRYPTO_ERROR
  12035. */
  12036. if (ret == 0)
  12037. #else
  12038. if (ret != BAD_FUNC_ARG)
  12039. #endif
  12040. return -7624;
  12041. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  12042. if (ret != 0)
  12043. return -7625;
  12044. eSz = 0;
  12045. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  12046. #ifdef HAVE_USER_RSA
  12047. /* Implementation using IPP Libraries returns:
  12048. * -101 = USER_CRYPTO_ERROR
  12049. */
  12050. if (ret == 0)
  12051. #elif defined(HAVE_FIPS) && \
  12052. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
  12053. if (ret != 0)
  12054. #else
  12055. if (ret != RSA_BUFFER_E)
  12056. #endif
  12057. return -7626;
  12058. eSz = sizeof(e);
  12059. nSz = 0;
  12060. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  12061. #ifdef HAVE_USER_RSA
  12062. /* Implementation using IPP Libraries returns:
  12063. * -101 = USER_CRYPTO_ERROR
  12064. */
  12065. if (ret == 0)
  12066. #else
  12067. if (ret != RSA_BUFFER_E)
  12068. #endif
  12069. return -7627;
  12070. return 0;
  12071. }
  12072. #endif /* NO_ASN */
  12073. #if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(NO_ASN) \
  12074. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  12075. static int rsa_export_key_test(RsaKey* key)
  12076. {
  12077. int ret;
  12078. byte e[3];
  12079. word32 eSz = sizeof(e);
  12080. byte n[RSA_TEST_BYTES];
  12081. word32 nSz = sizeof(n);
  12082. byte d[RSA_TEST_BYTES];
  12083. word32 dSz = sizeof(d);
  12084. byte p[RSA_TEST_BYTES/2];
  12085. word32 pSz = sizeof(p);
  12086. byte q[RSA_TEST_BYTES/2];
  12087. word32 qSz = sizeof(q);
  12088. word32 zero = 0;
  12089. ret = wc_RsaExportKey(NULL, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12090. if (ret != BAD_FUNC_ARG)
  12091. return -7630;
  12092. ret = wc_RsaExportKey(key, NULL, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12093. if (ret != BAD_FUNC_ARG)
  12094. return -7631;
  12095. ret = wc_RsaExportKey(key, e, NULL, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12096. if (ret != BAD_FUNC_ARG)
  12097. return -7632;
  12098. ret = wc_RsaExportKey(key, e, &eSz, NULL, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12099. if (ret != BAD_FUNC_ARG)
  12100. return -7633;
  12101. ret = wc_RsaExportKey(key, e, &eSz, n, NULL, d, &dSz, p, &pSz, q, &qSz);
  12102. if (ret != BAD_FUNC_ARG)
  12103. return -7634;
  12104. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, NULL, &dSz, p, &pSz, q, &qSz);
  12105. if (ret != BAD_FUNC_ARG)
  12106. return -7635;
  12107. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, NULL, p, &pSz, q, &qSz);
  12108. if (ret != BAD_FUNC_ARG)
  12109. return -7636;
  12110. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, NULL, &pSz, q, &qSz);
  12111. if (ret != BAD_FUNC_ARG)
  12112. return -7637;
  12113. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, NULL, q, &qSz);
  12114. if (ret != BAD_FUNC_ARG)
  12115. return -7638;
  12116. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, NULL, &qSz);
  12117. if (ret != BAD_FUNC_ARG)
  12118. return -7639;
  12119. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, NULL);
  12120. if (ret != BAD_FUNC_ARG)
  12121. return -7640;
  12122. ret = wc_RsaExportKey(key, e, &zero, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12123. if (ret != RSA_BUFFER_E)
  12124. return -7641;
  12125. ret = wc_RsaExportKey(key, e, &eSz, n, &zero, d, &dSz, p, &pSz, q, &qSz);
  12126. if (ret != RSA_BUFFER_E)
  12127. return -7642;
  12128. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  12129. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &zero, p, &pSz, q, &qSz);
  12130. if (ret != RSA_BUFFER_E)
  12131. return -7643;
  12132. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &zero, q, &qSz);
  12133. if (ret != RSA_BUFFER_E)
  12134. return -7644;
  12135. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &zero);
  12136. if (ret != RSA_BUFFER_E)
  12137. return -7645;
  12138. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  12139. ret = wc_RsaExportKey(key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q, &qSz);
  12140. if (ret != 0)
  12141. return -7646;
  12142. return 0;
  12143. }
  12144. #endif /* !HAVE_FIPS && !USER_RSA && !NO_ASN */
  12145. #ifndef NO_SIG_WRAPPER
  12146. static int rsa_sig_test(RsaKey* key, word32 keyLen, int modLen, WC_RNG* rng)
  12147. {
  12148. int ret;
  12149. word32 sigSz;
  12150. WOLFSSL_SMALL_STACK_STATIC const byte in[] = TEST_STRING;
  12151. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  12152. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  12153. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  12154. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  12155. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  12156. };
  12157. WOLFSSL_SMALL_STACK_STATIC const byte hashEnc[] = {
  12158. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  12159. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  12160. 0x00, 0x04, 0x20,
  12161. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  12162. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  12163. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  12164. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  12165. };
  12166. word32 inLen = (word32)XSTRLEN((char*)in);
  12167. byte out[RSA_TEST_BYTES];
  12168. /* Parameter Validation testing. */
  12169. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_NONE, key, keyLen);
  12170. if (ret != BAD_FUNC_ARG)
  12171. return -7650;
  12172. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, 0);
  12173. if (ret != BAD_FUNC_ARG)
  12174. return -7651;
  12175. sigSz = (word32)modLen;
  12176. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  12177. inLen, out, &sigSz, key, keyLen, rng);
  12178. if (ret != BAD_FUNC_ARG)
  12179. return -7652;
  12180. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12181. 0, out, &sigSz, key, keyLen, rng);
  12182. if (ret != BAD_FUNC_ARG)
  12183. return -7653;
  12184. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12185. inLen, NULL, &sigSz, key, keyLen, rng);
  12186. if (ret != BAD_FUNC_ARG)
  12187. return -7654;
  12188. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12189. inLen, out, NULL, key, keyLen, rng);
  12190. if (ret != BAD_FUNC_ARG)
  12191. return -7655;
  12192. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12193. inLen, out, &sigSz, NULL, keyLen, rng);
  12194. if (ret != BAD_FUNC_ARG)
  12195. return -7656;
  12196. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12197. inLen, out, &sigSz, key, 0, rng);
  12198. if (ret != BAD_FUNC_ARG)
  12199. return -7657;
  12200. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12201. inLen, out, &sigSz, key, keyLen, NULL);
  12202. #ifdef HAVE_USER_RSA
  12203. /* Implementation using IPP Libraries returns:
  12204. * -101 = USER_CRYPTO_ERROR
  12205. */
  12206. if (ret == 0)
  12207. #elif defined(WOLFSSL_AFALG_XILINX_RSA) || defined(WOLFSSL_XILINX_CRYPT)
  12208. /* blinding / rng handled with hardware acceleration */
  12209. if (ret != 0)
  12210. #elif defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
  12211. /* async may not require RNG */
  12212. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  12213. if (ret != NO_VALID_DEVID)
  12214. #else
  12215. if (ret != 0 && ret != MISSING_RNG_E)
  12216. #endif
  12217. #elif defined(HAVE_FIPS) || !defined(WC_RSA_BLINDING)
  12218. /* FIPS140 implementation does not do blinding */
  12219. if (ret != 0)
  12220. #elif defined(WOLFSSL_RSA_PUBLIC_ONLY) || defined(WOLFSSL_RSA_VERIFY_ONLY)
  12221. if (ret != SIG_TYPE_E)
  12222. #elif defined(WOLFSSL_CRYPTOCELL)
  12223. /* RNG is handled with the cryptocell */
  12224. if (ret != 0)
  12225. #else
  12226. if (ret != MISSING_RNG_E)
  12227. #endif
  12228. return -7658;
  12229. sigSz = 0;
  12230. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12231. inLen, out, &sigSz, key, keyLen, rng);
  12232. if (ret != BAD_FUNC_ARG)
  12233. return -7659;
  12234. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  12235. inLen, out, (word32)modLen, key, keyLen);
  12236. if (ret != BAD_FUNC_ARG)
  12237. return -7660;
  12238. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12239. 0, out, (word32)modLen, key, keyLen);
  12240. if (ret != BAD_FUNC_ARG)
  12241. return -7661;
  12242. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12243. inLen, NULL, (word32)modLen, key, keyLen);
  12244. if (ret != BAD_FUNC_ARG)
  12245. return -7662;
  12246. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12247. inLen, out, 0, key, keyLen);
  12248. if (ret != BAD_FUNC_ARG)
  12249. return -7663;
  12250. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12251. inLen, out, (word32)modLen, NULL, keyLen);
  12252. if (ret != BAD_FUNC_ARG)
  12253. return -7664;
  12254. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12255. inLen, out, (word32)modLen, key, 0);
  12256. if (ret != BAD_FUNC_ARG)
  12257. return -7665;
  12258. #ifndef HAVE_ECC
  12259. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, keyLen);
  12260. if (ret != SIG_TYPE_E)
  12261. return -7666;
  12262. #endif
  12263. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  12264. return 0;
  12265. #endif
  12266. /* Use APIs. */
  12267. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, keyLen);
  12268. if (ret != modLen)
  12269. return -7667;
  12270. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA_W_ENC, key, keyLen);
  12271. if (ret != modLen)
  12272. return -7668;
  12273. sigSz = (word32)ret;
  12274. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  12275. XMEMSET(out, 0, sizeof(out));
  12276. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12277. inLen, out, &sigSz, key, keyLen, rng);
  12278. if (ret != 0)
  12279. return -7669;
  12280. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12281. inLen, out, (word32)modLen, key, keyLen);
  12282. if (ret != 0)
  12283. return -7670;
  12284. sigSz = (word32)sizeof(out);
  12285. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  12286. in, inLen, out, &sigSz, key, keyLen, rng);
  12287. if (ret != 0)
  12288. return -7671;
  12289. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  12290. in, inLen, out, (word32)modLen, key, keyLen);
  12291. if (ret != 0)
  12292. return -7672;
  12293. /* Wrong signature type. */
  12294. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  12295. inLen, out, (word32)modLen, key, keyLen);
  12296. if (ret == 0)
  12297. return -7673;
  12298. /* check hash functions */
  12299. sigSz = (word32)sizeof(out);
  12300. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  12301. hash, (int)sizeof(hash), out, &sigSz, key, keyLen, rng);
  12302. if (ret != 0)
  12303. return -7674;
  12304. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  12305. hash, (int)sizeof(hash), out, (word32)modLen, key, keyLen);
  12306. if (ret != 0)
  12307. return -7675;
  12308. sigSz = (word32)sizeof(out);
  12309. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  12310. hashEnc, (int)sizeof(hashEnc), out, &sigSz, key, keyLen, rng);
  12311. if (ret != 0)
  12312. return -7676;
  12313. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  12314. hashEnc, (int)sizeof(hashEnc), out, (word32)modLen, key, keyLen);
  12315. if (ret != 0)
  12316. return -7677;
  12317. #else
  12318. (void)hash;
  12319. (void)hashEnc;
  12320. #endif /* !WOLFSSL_RSA_PUBLIC_ONLY && !WOLFSSL_RSA_VERIFY_ONLY */
  12321. return 0;
  12322. }
  12323. #endif /* !NO_SIG_WRAPPER */
  12324. #ifdef WC_RSA_NONBLOCK
  12325. static int rsa_nb_test(RsaKey* key, const byte* in, word32 inLen, byte* out,
  12326. word32 outSz, byte* plain, word32 plainSz, WC_RNG* rng)
  12327. {
  12328. int ret = 0, count;
  12329. int signSz = 0;
  12330. RsaNb nb;
  12331. byte* inlinePlain = NULL;
  12332. /* Enable non-blocking RSA mode - provide context */
  12333. ret = wc_RsaSetNonBlock(key, &nb);
  12334. if (ret != 0)
  12335. return ret;
  12336. #ifdef WC_RSA_NONBLOCK_TIME
  12337. /* Enable time based RSA blocking. 8 microseconds max (3.1GHz) */
  12338. ret = wc_RsaSetNonBlockTime(key, 8, 3100);
  12339. if (ret != 0)
  12340. return ret;
  12341. #endif
  12342. count = 0;
  12343. do {
  12344. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, rng);
  12345. count++; /* track number of would blocks */
  12346. if (ret == FP_WOULDBLOCK) {
  12347. /* do "other" work here */
  12348. }
  12349. } while (ret == FP_WOULDBLOCK);
  12350. if (ret < 0) {
  12351. return ret;
  12352. }
  12353. #ifdef DEBUG_WOLFSSL
  12354. printf("RSA non-block sign: %d times\n", count);
  12355. #endif
  12356. signSz = ret;
  12357. /* Test non-blocking verify */
  12358. XMEMSET(plain, 0, plainSz);
  12359. count = 0;
  12360. do {
  12361. ret = wc_RsaSSL_Verify(out, (word32)signSz, plain, plainSz, key);
  12362. count++; /* track number of would blocks */
  12363. if (ret == FP_WOULDBLOCK) {
  12364. /* do "other" work here */
  12365. }
  12366. } while (ret == FP_WOULDBLOCK);
  12367. if (ret < 0) {
  12368. return ret;
  12369. }
  12370. #ifdef DEBUG_WOLFSSL
  12371. printf("RSA non-block verify: %d times\n", count);
  12372. #endif
  12373. if (signSz == ret && XMEMCMP(plain, in, (size_t)ret)) {
  12374. return SIG_VERIFY_E;
  12375. }
  12376. /* Test inline non-blocking verify */
  12377. count = 0;
  12378. do {
  12379. ret = wc_RsaSSL_VerifyInline(out, (word32)signSz, &inlinePlain, key);
  12380. count++; /* track number of would blocks */
  12381. if (ret == FP_WOULDBLOCK) {
  12382. /* do "other" work here */
  12383. }
  12384. } while (ret == FP_WOULDBLOCK);
  12385. if (ret < 0) {
  12386. return ret;
  12387. }
  12388. #ifdef DEBUG_WOLFSSL
  12389. printf("RSA non-block inline verify: %d times\n", count);
  12390. #endif
  12391. if (signSz == ret && XMEMCMP(inlinePlain, in, (size_t)ret)) {
  12392. return SIG_VERIFY_E;
  12393. }
  12394. /* Disabling non-block RSA mode */
  12395. ret = wc_RsaSetNonBlock(key, NULL);
  12396. (void)count;
  12397. return 0;
  12398. }
  12399. #endif
  12400. #if !defined(HAVE_USER_RSA) && !defined(NO_ASN)
  12401. static int rsa_decode_test(RsaKey* keyPub)
  12402. {
  12403. int ret;
  12404. word32 inSz;
  12405. word32 inOutIdx;
  12406. WOLFSSL_SMALL_STACK_STATIC const byte n[2] = { 0x00, 0x23 };
  12407. WOLFSSL_SMALL_STACK_STATIC const byte e[2] = { 0x00, 0x03 };
  12408. WOLFSSL_SMALL_STACK_STATIC const byte good[] = { 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1,
  12409. 0x03 };
  12410. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgId[] = {
  12411. 0x30, 0x18, 0x30, 0x16,
  12412. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12413. 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12414. WOLFSSL_SMALL_STACK_STATIC const byte goodAlgIdNull[] = {
  12415. 0x30, 0x1a, 0x30, 0x18,
  12416. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12417. 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  12418. 0x02, 0x1, 0x03 };
  12419. WOLFSSL_SMALL_STACK_STATIC const byte badAlgIdNull[] = {
  12420. 0x30, 0x1b, 0x30, 0x19,
  12421. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12422. 0x05, 0x01, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  12423. 0x02, 0x1, 0x03 };
  12424. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitString[] = {
  12425. 0x30, 0x18, 0x30, 0x16,
  12426. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12427. 0x04, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12428. WOLFSSL_SMALL_STACK_STATIC const byte badBitStringLen[] = {
  12429. 0x30, 0x18, 0x30, 0x16,
  12430. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12431. 0x03, 0x0a, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12432. WOLFSSL_SMALL_STACK_STATIC const byte badNoSeq[] = {
  12433. 0x30, 0x16, 0x30, 0x14,
  12434. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12435. 0x07, 0x00, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12436. WOLFSSL_SMALL_STACK_STATIC const byte badNoObj[] = {
  12437. 0x30, 0x0f, 0x30, 0x0d, 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06,
  12438. 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12439. WOLFSSL_SMALL_STACK_STATIC const byte badIntN[] = {
  12440. 0x30, 0x06, 0x02, 0x05, 0x23, 0x02, 0x1, 0x03 };
  12441. WOLFSSL_SMALL_STACK_STATIC const byte badNotIntE[] = {
  12442. 0x30, 0x06, 0x02, 0x01, 0x23, 0x04, 0x1, 0x03 };
  12443. WOLFSSL_SMALL_STACK_STATIC const byte badLength[] = {
  12444. 0x30, 0x04, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12445. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrNoZero[] = {
  12446. 0x30, 0x17, 0x30, 0x15,
  12447. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
  12448. 0x03, 0x08, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  12449. ret = wc_InitRsaKey(keyPub, NULL);
  12450. if (ret != 0)
  12451. return -7690;
  12452. /* Parameter Validation testing. */
  12453. ret = wc_RsaPublicKeyDecodeRaw(NULL, sizeof(n), e, sizeof(e), keyPub);
  12454. if (ret != BAD_FUNC_ARG) {
  12455. ret = -7691;
  12456. goto done;
  12457. }
  12458. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), NULL, sizeof(e), keyPub);
  12459. if (ret != BAD_FUNC_ARG) {
  12460. ret = -7692;
  12461. goto done;
  12462. }
  12463. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), NULL);
  12464. if (ret != BAD_FUNC_ARG) {
  12465. ret = -7693;
  12466. goto done;
  12467. }
  12468. ret = wc_RsaPublicKeyDecodeRaw(n, (word32)-1, e, sizeof(e), keyPub);
  12469. #if !defined(WOLFSSL_SP_MATH) & !defined(WOLFSSL_SP_MATH_ALL)
  12470. if (ret != 0) {
  12471. #else
  12472. if (ret != ASN_GETINT_E) {
  12473. #endif
  12474. ret = -7694;
  12475. goto done;
  12476. }
  12477. wc_FreeRsaKey(keyPub);
  12478. ret = wc_InitRsaKey(keyPub, NULL);
  12479. if (ret != 0)
  12480. return -7695;
  12481. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, (word32)-1, keyPub);
  12482. #if !defined(WOLFSSL_SP_MATH) & !defined(WOLFSSL_SP_MATH_ALL)
  12483. if (ret != 0) {
  12484. #else
  12485. if (ret != ASN_GETINT_E) {
  12486. #endif
  12487. ret = -7696;
  12488. goto done;
  12489. }
  12490. wc_FreeRsaKey(keyPub);
  12491. ret = wc_InitRsaKey(keyPub, NULL);
  12492. if (ret != 0)
  12493. return -7697;
  12494. /* Use API. */
  12495. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), keyPub);
  12496. if (ret != 0) {
  12497. ret = -7698;
  12498. goto done;
  12499. }
  12500. wc_FreeRsaKey(keyPub);
  12501. ret = wc_InitRsaKey(keyPub, NULL);
  12502. if (ret != 0)
  12503. return -7699;
  12504. /* Parameter Validation testing. */
  12505. inSz = sizeof(good);
  12506. ret = wc_RsaPublicKeyDecode(NULL, &inOutIdx, keyPub, inSz);
  12507. if (ret != BAD_FUNC_ARG) {
  12508. ret = -7700;
  12509. goto done;
  12510. }
  12511. ret = wc_RsaPublicKeyDecode(good, NULL, keyPub, inSz);
  12512. if (ret != BAD_FUNC_ARG) {
  12513. ret = -7701;
  12514. goto done;
  12515. }
  12516. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  12517. if (ret != BAD_FUNC_ARG) {
  12518. ret = -7702;
  12519. goto done;
  12520. }
  12521. /* Use good data and offset to bad data. */
  12522. inOutIdx = 2;
  12523. inSz = sizeof(good) - inOutIdx;
  12524. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  12525. if (ret != ASN_PARSE_E) {
  12526. ret = -7703;
  12527. goto done;
  12528. }
  12529. inOutIdx = 2;
  12530. inSz = sizeof(goodAlgId) - inOutIdx;
  12531. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  12532. if (ret != ASN_PARSE_E) {
  12533. ret = -7704;
  12534. goto done;
  12535. }
  12536. inOutIdx = 2;
  12537. inSz = sizeof(goodAlgId);
  12538. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  12539. #ifndef WOLFSSL_NO_DECODE_EXTRA
  12540. if (ret != ASN_PARSE_E)
  12541. #else
  12542. if (ret != ASN_RSA_KEY_E)
  12543. #endif
  12544. {
  12545. ret = -7705;
  12546. goto done;
  12547. }
  12548. /* Try different bad data. */
  12549. inSz = sizeof(badAlgIdNull);
  12550. inOutIdx = 0;
  12551. ret = wc_RsaPublicKeyDecode(badAlgIdNull, &inOutIdx, keyPub, inSz);
  12552. if (ret != ASN_EXPECT_0_E) {
  12553. ret = -7706;
  12554. goto done;
  12555. }
  12556. inSz = sizeof(badNotBitString);
  12557. inOutIdx = 0;
  12558. ret = wc_RsaPublicKeyDecode(badNotBitString, &inOutIdx, keyPub, inSz);
  12559. if (ret != ASN_BITSTR_E) {
  12560. ret = -7707;
  12561. goto done;
  12562. }
  12563. inSz = sizeof(badBitStringLen);
  12564. inOutIdx = 0;
  12565. ret = wc_RsaPublicKeyDecode(badBitStringLen, &inOutIdx, keyPub, inSz);
  12566. if (ret != ASN_PARSE_E) {
  12567. ret = -7708;
  12568. goto done;
  12569. }
  12570. inSz = sizeof(badNoSeq);
  12571. inOutIdx = 0;
  12572. ret = wc_RsaPublicKeyDecode(badNoSeq, &inOutIdx, keyPub, inSz);
  12573. if (ret != ASN_PARSE_E) {
  12574. ret = -7709;
  12575. goto done;
  12576. }
  12577. inSz = sizeof(badNoObj);
  12578. inOutIdx = 0;
  12579. ret = wc_RsaPublicKeyDecode(badNoObj, &inOutIdx, keyPub, inSz);
  12580. if (ret != ASN_PARSE_E && ret != ASN_OBJECT_ID_E) {
  12581. ret = -7710;
  12582. goto done;
  12583. }
  12584. inSz = sizeof(badIntN);
  12585. inOutIdx = 0;
  12586. ret = wc_RsaPublicKeyDecode(badIntN, &inOutIdx, keyPub, inSz);
  12587. if (ret != ASN_RSA_KEY_E && ret != ASN_PARSE_E) {
  12588. ret = -7711;
  12589. goto done;
  12590. }
  12591. inSz = sizeof(badNotIntE);
  12592. inOutIdx = 0;
  12593. ret = wc_RsaPublicKeyDecode(badNotIntE, &inOutIdx, keyPub, inSz);
  12594. if (ret != ASN_RSA_KEY_E && ret != ASN_PARSE_E) {
  12595. ret = -7712;
  12596. goto done;
  12597. }
  12598. /* TODO: Shouldn't pass as the sequence length is too small. */
  12599. inSz = sizeof(badLength);
  12600. inOutIdx = 0;
  12601. ret = wc_RsaPublicKeyDecode(badLength, &inOutIdx, keyPub, inSz);
  12602. #ifndef WOLFSSL_ASN_TEMPLATE
  12603. if (ret != 0)
  12604. #else
  12605. if (ret != ASN_PARSE_E)
  12606. #endif
  12607. {
  12608. ret = -7713;
  12609. goto done;
  12610. }
  12611. /* TODO: Shouldn't ignore object id's data. */
  12612. wc_FreeRsaKey(keyPub);
  12613. ret = wc_InitRsaKey(keyPub, NULL);
  12614. if (ret != 0)
  12615. return -7714;
  12616. inSz = sizeof(badBitStrNoZero);
  12617. inOutIdx = 0;
  12618. ret = wc_RsaPublicKeyDecode(badBitStrNoZero, &inOutIdx, keyPub, inSz);
  12619. if (ret != ASN_EXPECT_0_E && ret != ASN_PARSE_E) {
  12620. ret = -7715;
  12621. goto done;
  12622. }
  12623. wc_FreeRsaKey(keyPub);
  12624. ret = wc_InitRsaKey(keyPub, NULL);
  12625. if (ret != 0)
  12626. return -7716;
  12627. /* Valid data cases. */
  12628. inSz = sizeof(good);
  12629. inOutIdx = 0;
  12630. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, keyPub, inSz);
  12631. if (ret != 0) {
  12632. ret = -7717;
  12633. goto done;
  12634. }
  12635. if (inOutIdx != inSz) {
  12636. ret = -7718;
  12637. goto done;
  12638. }
  12639. wc_FreeRsaKey(keyPub);
  12640. ret = wc_InitRsaKey(keyPub, NULL);
  12641. if (ret != 0)
  12642. return -7719;
  12643. inSz = sizeof(goodAlgId);
  12644. inOutIdx = 0;
  12645. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, keyPub, inSz);
  12646. if (ret != 0) {
  12647. ret = -7720;
  12648. goto done;
  12649. }
  12650. if (inOutIdx != inSz) {
  12651. ret = -7721;
  12652. goto done;
  12653. }
  12654. wc_FreeRsaKey(keyPub);
  12655. ret = wc_InitRsaKey(keyPub, NULL);
  12656. if (ret != 0)
  12657. return -7722;
  12658. inSz = sizeof(goodAlgIdNull);
  12659. inOutIdx = 0;
  12660. ret = wc_RsaPublicKeyDecode(goodAlgIdNull, &inOutIdx, keyPub, inSz);
  12661. if (ret != 0) {
  12662. ret = -7723;
  12663. goto done;
  12664. }
  12665. if (inOutIdx != inSz) {
  12666. ret = -7724;
  12667. goto done;
  12668. }
  12669. done:
  12670. wc_FreeRsaKey(keyPub);
  12671. return ret;
  12672. }
  12673. #endif
  12674. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  12675. /* Need to create known good signatures to test with this. */
  12676. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  12677. !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  12678. static int rsa_pss_test(WC_RNG* rng, RsaKey* key)
  12679. {
  12680. byte digest[WC_MAX_DIGEST_SIZE];
  12681. int ret = 0;
  12682. const char inStr[] = TEST_STRING;
  12683. word32 inLen = (word32)TEST_STRING_SZ;
  12684. word32 outSz;
  12685. word32 plainSz;
  12686. word32 digestSz;
  12687. int i, j;
  12688. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  12689. int k, l;
  12690. #endif
  12691. int len;
  12692. byte* plain;
  12693. int mgf[] = {
  12694. #ifndef NO_SHA
  12695. WC_MGF1SHA1,
  12696. #endif
  12697. #ifdef WOLFSSL_SHA224
  12698. WC_MGF1SHA224,
  12699. #endif
  12700. WC_MGF1SHA256,
  12701. #ifdef WOLFSSL_SHA384
  12702. WC_MGF1SHA384,
  12703. #endif
  12704. #ifdef WOLFSSL_SHA512
  12705. WC_MGF1SHA512
  12706. #endif
  12707. };
  12708. enum wc_HashType hash[] = {
  12709. #ifndef NO_SHA
  12710. WC_HASH_TYPE_SHA,
  12711. #endif
  12712. #ifdef WOLFSSL_SHA224
  12713. WC_HASH_TYPE_SHA224,
  12714. #endif
  12715. WC_HASH_TYPE_SHA256,
  12716. #ifdef WOLFSSL_SHA384
  12717. WC_HASH_TYPE_SHA384,
  12718. #endif
  12719. #ifdef WOLFSSL_SHA512
  12720. WC_HASH_TYPE_SHA512,
  12721. #endif
  12722. };
  12723. WC_DECLARE_VAR(in, byte, RSA_TEST_BYTES, HEAP_HINT);
  12724. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  12725. WC_DECLARE_VAR(sig, byte, RSA_TEST_BYTES, HEAP_HINT);
  12726. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  12727. if (in == NULL || out == NULL || sig == NULL)
  12728. ERROR_OUT(MEMORY_E, exit_rsa_pss);
  12729. #endif
  12730. XMEMCPY(in, inStr, inLen);
  12731. /* Test all combinations of hash and MGF. */
  12732. for (j = 0; j < (int)(sizeof(hash)/sizeof(*hash)); j++) {
  12733. /* Calculate hash of message. */
  12734. ret = wc_Hash(hash[j], in, inLen, digest, sizeof(digest));
  12735. if (ret != 0)
  12736. ERROR_OUT(-7730, exit_rsa_pss);
  12737. digestSz = wc_HashGetDigestSize(hash[j]);
  12738. for (i = 0; i < (int)(sizeof(mgf)/sizeof(*mgf)); i++) {
  12739. outSz = RSA_TEST_BYTES;
  12740. do {
  12741. #if defined(WOLFSSL_ASYNC_CRYPT)
  12742. ret = wc_AsyncWait(ret, &key->asyncDev,
  12743. WC_ASYNC_FLAG_CALL_AGAIN);
  12744. #endif
  12745. if (ret >= 0) {
  12746. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz,
  12747. hash[j], mgf[i], -1, key, rng);
  12748. }
  12749. } while (ret == WC_PENDING_E);
  12750. if (ret <= 0)
  12751. ERROR_OUT(-7731, exit_rsa_pss);
  12752. outSz = ret;
  12753. XMEMCPY(sig, out, outSz);
  12754. plain = NULL;
  12755. TEST_SLEEP();
  12756. do {
  12757. #if defined(WOLFSSL_ASYNC_CRYPT)
  12758. ret = wc_AsyncWait(ret, &key->asyncDev,
  12759. WC_ASYNC_FLAG_CALL_AGAIN);
  12760. #endif
  12761. if (ret >= 0) {
  12762. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[j],
  12763. mgf[i], -1, key);
  12764. }
  12765. } while (ret == WC_PENDING_E);
  12766. if (ret <= 0)
  12767. ERROR_OUT(-7732, exit_rsa_pss);
  12768. plainSz = ret;
  12769. TEST_SLEEP();
  12770. #if defined(HAVE_SELFTEST) && \
  12771. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  12772. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  12773. hash[j], -1);
  12774. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  12775. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  12776. hash[j], -1, 0);
  12777. #else
  12778. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz,
  12779. hash[j], -1, wc_RsaEncryptSize(key)*8, HEAP_HINT);
  12780. #endif
  12781. if (ret != 0)
  12782. ERROR_OUT(-7733, exit_rsa_pss);
  12783. #ifdef RSA_PSS_TEST_WRONG_PARAMS
  12784. for (k = 0; k < (int)(sizeof(mgf)/sizeof(*mgf)); k++) {
  12785. for (l = 0; l < (int)(sizeof(hash)/sizeof(*hash)); l++) {
  12786. if (i == k && j == l)
  12787. continue;
  12788. XMEMCPY(sig, out, outSz);
  12789. do {
  12790. #if defined(WOLFSSL_ASYNC_CRYPT)
  12791. ret = wc_AsyncWait(ret, &key->asyncDev,
  12792. WC_ASYNC_FLAG_CALL_AGAIN);
  12793. #endif
  12794. if (ret >= 0) {
  12795. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz,
  12796. (byte**)&plain, hash[l], mgf[k], -1, key);
  12797. }
  12798. } while (ret == WC_PENDING_E);
  12799. if (ret >= 0)
  12800. ERROR_OUT(-7734, exit_rsa_pss);
  12801. }
  12802. }
  12803. #endif
  12804. }
  12805. }
  12806. /* Test that a salt length of zero works. */
  12807. digestSz = wc_HashGetDigestSize(hash[0]);
  12808. outSz = RSA_TEST_BYTES;
  12809. do {
  12810. #if defined(WOLFSSL_ASYNC_CRYPT)
  12811. ret = wc_AsyncWait(ret, &key->asyncDev,
  12812. WC_ASYNC_FLAG_CALL_AGAIN);
  12813. #endif
  12814. if (ret >= 0) {
  12815. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  12816. mgf[0], 0, key, rng);
  12817. }
  12818. } while (ret == WC_PENDING_E);
  12819. if (ret <= 0)
  12820. ERROR_OUT(-7735, exit_rsa_pss);
  12821. outSz = ret;
  12822. TEST_SLEEP();
  12823. do {
  12824. #if defined(WOLFSSL_ASYNC_CRYPT)
  12825. ret = wc_AsyncWait(ret, &key->asyncDev,
  12826. WC_ASYNC_FLAG_CALL_AGAIN);
  12827. #endif
  12828. if (ret >= 0) {
  12829. ret = wc_RsaPSS_Verify_ex(out, outSz, sig, outSz, hash[0], mgf[0],
  12830. 0, key);
  12831. }
  12832. } while (ret == WC_PENDING_E);
  12833. if (ret <= 0)
  12834. ERROR_OUT(-7736, exit_rsa_pss);
  12835. plainSz = ret;
  12836. TEST_SLEEP();
  12837. do {
  12838. #if defined(WOLFSSL_ASYNC_CRYPT)
  12839. ret = wc_AsyncWait(ret, &key->asyncDev,
  12840. WC_ASYNC_FLAG_CALL_AGAIN);
  12841. #endif
  12842. if (ret >= 0) {
  12843. #if defined(HAVE_SELFTEST) && \
  12844. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  12845. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  12846. hash[0], 0);
  12847. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  12848. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, sig, plainSz,
  12849. hash[0], 0, 0);
  12850. #else
  12851. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, sig, plainSz,
  12852. hash[0], 0, 0, HEAP_HINT);
  12853. #endif
  12854. }
  12855. } while (ret == WC_PENDING_E);
  12856. if (ret != 0)
  12857. ERROR_OUT(-7737, exit_rsa_pss);
  12858. XMEMCPY(sig, out, outSz);
  12859. plain = NULL;
  12860. do {
  12861. #if defined(WOLFSSL_ASYNC_CRYPT)
  12862. ret = wc_AsyncWait(ret, &key->asyncDev,
  12863. WC_ASYNC_FLAG_CALL_AGAIN);
  12864. #endif
  12865. if (ret >= 0) {
  12866. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  12867. 0, key);
  12868. }
  12869. } while (ret == WC_PENDING_E);
  12870. if (ret <= 0)
  12871. ERROR_OUT(-7738, exit_rsa_pss);
  12872. plainSz = ret;
  12873. TEST_SLEEP();
  12874. #if defined(HAVE_SELFTEST) && \
  12875. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  12876. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  12877. hash[0], 0);
  12878. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  12879. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  12880. hash[0], 0, 0);
  12881. #else
  12882. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  12883. 0, 0, HEAP_HINT);
  12884. #endif
  12885. if (ret != 0)
  12886. ERROR_OUT(-7739, exit_rsa_pss);
  12887. /* Test bad salt lengths in various APIs. */
  12888. digestSz = wc_HashGetDigestSize(hash[0]);
  12889. outSz = RSA_TEST_BYTES;
  12890. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  12891. len = -2;
  12892. #else
  12893. len = -3;
  12894. #endif
  12895. do {
  12896. #if defined(WOLFSSL_ASYNC_CRYPT)
  12897. ret = wc_AsyncWait(ret, &key->asyncDev,
  12898. WC_ASYNC_FLAG_CALL_AGAIN);
  12899. #endif
  12900. if (ret >= 0) {
  12901. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  12902. mgf[0], len, key, rng);
  12903. }
  12904. } while (ret == WC_PENDING_E);
  12905. if (ret != PSS_SALTLEN_E)
  12906. ERROR_OUT(-7740, exit_rsa_pss);
  12907. do {
  12908. #if defined(WOLFSSL_ASYNC_CRYPT)
  12909. ret = wc_AsyncWait(ret, &key->asyncDev,
  12910. WC_ASYNC_FLAG_CALL_AGAIN);
  12911. #endif
  12912. if (ret >= 0) {
  12913. ret = wc_RsaPSS_Sign_ex(digest, digestSz, out, outSz, hash[0],
  12914. mgf[0], digestSz + 1, key, rng);
  12915. }
  12916. } while (ret == WC_PENDING_E);
  12917. if (ret != PSS_SALTLEN_E)
  12918. ERROR_OUT(-7741, exit_rsa_pss);
  12919. TEST_SLEEP();
  12920. do {
  12921. #if defined(WOLFSSL_ASYNC_CRYPT)
  12922. ret = wc_AsyncWait(ret, &key->asyncDev,
  12923. WC_ASYNC_FLAG_CALL_AGAIN);
  12924. #endif
  12925. if (ret >= 0) {
  12926. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0],
  12927. mgf[0], -2, key);
  12928. }
  12929. } while (ret == WC_PENDING_E);
  12930. if (ret != PSS_SALTLEN_E)
  12931. ERROR_OUT(-7742, exit_rsa_pss);
  12932. TEST_SLEEP();
  12933. do {
  12934. #if defined(WOLFSSL_ASYNC_CRYPT)
  12935. ret = wc_AsyncWait(ret, &key->asyncDev,
  12936. WC_ASYNC_FLAG_CALL_AGAIN);
  12937. #endif
  12938. if (ret >= 0) {
  12939. ret = wc_RsaPSS_VerifyInline_ex(sig, outSz, &plain, hash[0], mgf[0],
  12940. digestSz + 1, key);
  12941. }
  12942. } while (ret == WC_PENDING_E);
  12943. if (ret != PSS_SALTLEN_E)
  12944. ERROR_OUT(-7743, exit_rsa_pss);
  12945. TEST_SLEEP();
  12946. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  12947. len = -2;
  12948. #else
  12949. len = -3;
  12950. #endif
  12951. #if defined(HAVE_SELFTEST) && \
  12952. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  12953. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  12954. hash[0], len);
  12955. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  12956. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  12957. hash[0], len, 0);
  12958. #else
  12959. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  12960. len, 0, HEAP_HINT);
  12961. #endif
  12962. if (ret != PSS_SALTLEN_E)
  12963. ERROR_OUT(-7744, exit_rsa_pss);
  12964. #ifndef WOLFSSL_PSS_LONG_SALT
  12965. len = digestSz + 1;
  12966. #else
  12967. len = plainSz - digestSz - 1;
  12968. #endif
  12969. #if defined(HAVE_SELFTEST) && \
  12970. (!defined(HAVE_SELFTEST_VERSION) || (HAVE_SELFTEST_VERSION < 2))
  12971. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  12972. hash[0], len);
  12973. #elif defined(HAVE_SELFTEST) && (HAVE_SELFTEST_VERSION == 2)
  12974. ret = wc_RsaPSS_CheckPadding_ex(digest, digestSz, plain, plainSz,
  12975. hash[0], len, 0);
  12976. #else
  12977. ret = wc_RsaPSS_CheckPadding_ex2(digest, digestSz, plain, plainSz, hash[0],
  12978. len, 0, HEAP_HINT);
  12979. #endif
  12980. if (ret != PSS_SALTLEN_E)
  12981. ERROR_OUT(-7745, exit_rsa_pss);
  12982. ret = 0;
  12983. exit_rsa_pss:
  12984. WC_FREE_VAR(sig, HEAP_HINT);
  12985. WC_FREE_VAR(in, HEAP_HINT);
  12986. WC_FREE_VAR(out, HEAP_HINT);
  12987. return ret;
  12988. }
  12989. #endif /* !WOLFSSL_RSA_VERIFY_ONLY && !WOLFSSL_RSA_PUBLIC_ONLY */
  12990. #endif
  12991. #ifdef WC_RSA_NO_PADDING
  12992. WOLFSSL_TEST_SUBROUTINE int rsa_no_pad_test(void)
  12993. {
  12994. WC_RNG rng;
  12995. byte* tmp = NULL;
  12996. size_t bytes;
  12997. int ret;
  12998. word32 inLen = 0;
  12999. word32 idx = 0;
  13000. word32 outSz = RSA_TEST_BYTES;
  13001. word32 plainSz = RSA_TEST_BYTES;
  13002. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  13003. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  13004. !defined(NO_FILESYSTEM)
  13005. XFILE file;
  13006. #endif
  13007. WC_DECLARE_VAR(key, RsaKey, 1, HEAP_HINT);
  13008. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  13009. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  13010. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  13011. if (key == NULL || out == NULL || plain == NULL)
  13012. ERROR_OUT(MEMORY_E, exit_rsa_nopadding);
  13013. #endif
  13014. /* initialize stack structures */
  13015. XMEMSET(&rng, 0, sizeof(rng));
  13016. XMEMSET(key, 0, sizeof(RsaKey));
  13017. #ifdef USE_CERT_BUFFERS_1024
  13018. bytes = (size_t)sizeof_client_key_der_1024;
  13019. if (bytes < (size_t)sizeof_client_cert_der_1024)
  13020. bytes = (size_t)sizeof_client_cert_der_1024;
  13021. #elif defined(USE_CERT_BUFFERS_2048)
  13022. bytes = (size_t)sizeof_client_key_der_2048;
  13023. if (bytes < (size_t)sizeof_client_cert_der_2048)
  13024. bytes = (size_t)sizeof_client_cert_der_2048;
  13025. #else
  13026. bytes = FOURK_BUF;
  13027. #endif
  13028. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13029. if (tmp == NULL
  13030. #ifdef WOLFSSL_ASYNC_CRYPT
  13031. || out == NULL || plain == NULL
  13032. #endif
  13033. ) {
  13034. ERROR_OUT(-7800, exit_rsa_nopadding);
  13035. }
  13036. #ifdef USE_CERT_BUFFERS_1024
  13037. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  13038. #elif defined(USE_CERT_BUFFERS_2048)
  13039. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  13040. #elif defined(USE_CERT_BUFFERS_3072)
  13041. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  13042. #elif defined(USE_CERT_BUFFERS_4096)
  13043. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  13044. #elif !defined(NO_FILESYSTEM)
  13045. file = XFOPEN(clientKey, "rb");
  13046. if (!file) {
  13047. err_sys("can't open clientKey, Please run from wolfSSL home dir", -40);
  13048. ERROR_OUT(-7801, exit_rsa_nopadding);
  13049. }
  13050. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  13051. XFCLOSE(file);
  13052. #else
  13053. /* No key to use. */
  13054. ERROR_OUT(-7802, exit_rsa_nopadding);
  13055. #endif /* USE_CERT_BUFFERS */
  13056. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  13057. if (ret != 0) {
  13058. ERROR_OUT(-7803, exit_rsa_nopadding);
  13059. }
  13060. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  13061. if (ret != 0) {
  13062. ERROR_OUT(-7804, exit_rsa_nopadding);
  13063. }
  13064. /* after loading in key use tmp as the test buffer */
  13065. #ifndef HAVE_FIPS
  13066. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  13067. #else
  13068. ret = wc_InitRng(&rng);
  13069. #endif
  13070. if (ret != 0) {
  13071. ERROR_OUT(-7805, exit_rsa_nopadding);
  13072. }
  13073. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  13074. inLen = wc_RsaEncryptSize(key);
  13075. outSz = inLen;
  13076. plainSz = inLen;
  13077. XMEMSET(tmp, 7, inLen);
  13078. do {
  13079. #if defined(WOLFSSL_ASYNC_CRYPT)
  13080. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13081. #endif
  13082. if (ret >= 0) {
  13083. ret = wc_RsaDirect(tmp, inLen, out, &outSz, key,
  13084. RSA_PRIVATE_ENCRYPT, &rng);
  13085. }
  13086. } while (ret == WC_PENDING_E);
  13087. if (ret <= 0) {
  13088. ERROR_OUT(-7806, exit_rsa_nopadding);
  13089. }
  13090. /* encrypted result should not be the same as input */
  13091. if (XMEMCMP(out, tmp, inLen) == 0) {
  13092. ERROR_OUT(-7807, exit_rsa_nopadding);
  13093. }
  13094. TEST_SLEEP();
  13095. /* decrypt with public key and compare result */
  13096. do {
  13097. #if defined(WOLFSSL_ASYNC_CRYPT)
  13098. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13099. #endif
  13100. if (ret >= 0) {
  13101. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key,
  13102. RSA_PUBLIC_DECRYPT, &rng);
  13103. }
  13104. } while (ret == WC_PENDING_E);
  13105. if (ret <= 0) {
  13106. ERROR_OUT(-7808, exit_rsa_nopadding);
  13107. }
  13108. if (XMEMCMP(plain, tmp, inLen) != 0) {
  13109. ERROR_OUT(-7809, exit_rsa_nopadding);
  13110. }
  13111. TEST_SLEEP();
  13112. #endif
  13113. #ifdef WC_RSA_BLINDING
  13114. ret = wc_RsaSetRNG(NULL, &rng);
  13115. if (ret != BAD_FUNC_ARG) {
  13116. ERROR_OUT(-7810, exit_rsa_nopadding);
  13117. }
  13118. ret = wc_RsaSetRNG(key, &rng);
  13119. if (ret < 0) {
  13120. ERROR_OUT(-7811, exit_rsa_nopadding);
  13121. }
  13122. #endif
  13123. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  13124. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  13125. do {
  13126. #if defined(WOLFSSL_ASYNC_CRYPT)
  13127. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13128. #endif
  13129. if (ret >= 0) {
  13130. ret = wc_RsaPublicEncrypt_ex(tmp, inLen, out, (int)outSz, key, &rng,
  13131. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  13132. }
  13133. } while (ret == WC_PENDING_E);
  13134. if (ret < 0) {
  13135. ERROR_OUT(-7812, exit_rsa_nopadding);
  13136. }
  13137. TEST_SLEEP();
  13138. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  13139. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13140. do {
  13141. #if defined(WOLFSSL_ASYNC_CRYPT)
  13142. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13143. #endif
  13144. if (ret >= 0) {
  13145. ret = wc_RsaPrivateDecrypt_ex(out, outSz, plain, (int)plainSz, key,
  13146. WC_RSA_NO_PAD, WC_HASH_TYPE_NONE, WC_MGF1NONE, NULL, 0);
  13147. }
  13148. } while (ret == WC_PENDING_E);
  13149. if (ret < 0) {
  13150. ERROR_OUT(-7813, exit_rsa_nopadding);
  13151. }
  13152. if (XMEMCMP(plain, tmp, inLen) != 0) {
  13153. ERROR_OUT(-7814, exit_rsa_nopadding);
  13154. }
  13155. TEST_SLEEP();
  13156. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13157. /* test some bad arguments */
  13158. ret = wc_RsaDirect(out, outSz, plain, &plainSz, key, -1,
  13159. &rng);
  13160. if (ret != BAD_FUNC_ARG) {
  13161. ERROR_OUT(-7815, exit_rsa_nopadding);
  13162. }
  13163. ret = wc_RsaDirect(out, outSz, plain, &plainSz, NULL, RSA_PUBLIC_DECRYPT,
  13164. &rng);
  13165. if (ret != BAD_FUNC_ARG) {
  13166. ERROR_OUT(-7816, exit_rsa_nopadding);
  13167. }
  13168. ret = wc_RsaDirect(out, outSz, NULL, &plainSz, key, RSA_PUBLIC_DECRYPT,
  13169. &rng);
  13170. if (ret != LENGTH_ONLY_E || plainSz != inLen) {
  13171. ERROR_OUT(-7817, exit_rsa_nopadding);
  13172. }
  13173. ret = wc_RsaDirect(out, outSz - 10, plain, &plainSz, key,
  13174. RSA_PUBLIC_DECRYPT, &rng);
  13175. if (ret != BAD_FUNC_ARG) {
  13176. ERROR_OUT(-7818, exit_rsa_nopadding);
  13177. }
  13178. /* if making it to this point of code without hitting an ERROR_OUT then
  13179. * all tests have passed */
  13180. ret = 0;
  13181. exit_rsa_nopadding:
  13182. wc_FreeRsaKey(key);
  13183. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13184. WC_FREE_VAR(key, HEAP_HINT);
  13185. WC_FREE_VAR(out, HEAP_HINT);
  13186. WC_FREE_VAR(plain, HEAP_HINT);
  13187. wc_FreeRng(&rng);
  13188. return ret;
  13189. }
  13190. #endif /* WC_RSA_NO_PADDING */
  13191. #if defined(WOLFSSL_HAVE_SP_RSA) && defined(USE_FAST_MATH)
  13192. static int rsa_even_mod_test(WC_RNG* rng, RsaKey* key)
  13193. {
  13194. byte* tmp = NULL;
  13195. size_t bytes;
  13196. int ret;
  13197. word32 inLen = 0;
  13198. #ifndef NO_ASN
  13199. word32 idx = 0;
  13200. #endif
  13201. word32 outSz = RSA_TEST_BYTES;
  13202. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13203. word32 plainSz = RSA_TEST_BYTES;
  13204. #endif
  13205. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_3072) && \
  13206. !defined(USE_CERT_BUFFERS_4096) && !defined(NO_FILESYSTEM)
  13207. XFILE file;
  13208. #endif
  13209. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  13210. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13211. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  13212. #endif
  13213. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  13214. if (out == NULL
  13215. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13216. || plain == NULL
  13217. #endif
  13218. ) {
  13219. ERROR_OUT(MEMORY_E, exit_rsa_even_mod);
  13220. }
  13221. #endif
  13222. #if defined(USE_CERT_BUFFERS_2048)
  13223. bytes = (size_t)sizeof_client_key_der_2048;
  13224. if (bytes < (size_t)sizeof_client_cert_der_2048)
  13225. bytes = (size_t)sizeof_client_cert_der_2048;
  13226. #else
  13227. bytes = FOURK_BUF;
  13228. #endif
  13229. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13230. if (tmp == NULL
  13231. #ifdef WOLFSSL_ASYNC_CRYPT
  13232. || out == NULL || plain == NULL
  13233. #endif
  13234. ) {
  13235. ERROR_OUT(-7800, exit_rsa_even_mod);
  13236. }
  13237. #if defined(USE_CERT_BUFFERS_2048)
  13238. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  13239. #elif defined(USE_CERT_BUFFERS_3072)
  13240. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  13241. #elif defined(USE_CERT_BUFFERS_4096)
  13242. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  13243. #elif !defined(NO_FILESYSTEM)
  13244. file = XFOPEN(clientKey, "rb");
  13245. if (!file) {
  13246. err_sys("can't open ./certs/client-key.der, "
  13247. "Please run from wolfSSL home dir", -40);
  13248. ERROR_OUT(-7801, exit_rsa_even_mod);
  13249. }
  13250. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  13251. XFCLOSE(file);
  13252. #else
  13253. /* No key to use. */
  13254. ERROR_OUT(-7802, exit_rsa_even_mod);
  13255. #endif /* USE_CERT_BUFFERS */
  13256. #ifndef NO_ASN
  13257. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  13258. if (ret != 0) {
  13259. ERROR_OUT(-7804, exit_rsa_even_mod);
  13260. }
  13261. #else
  13262. #ifdef USE_CERT_BUFFERS_2048
  13263. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  13264. if (ret != 0) {
  13265. ERROR_OUT(-7804, exit_rsa_even_mod);
  13266. }
  13267. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  13268. if (ret != 0) {
  13269. ERROR_OUT(-7804, exit_rsa_even_mod);
  13270. }
  13271. #ifndef NO_SIG_WRAPPER
  13272. modLen = 2048;
  13273. #endif
  13274. #else
  13275. #error Not supported yet!
  13276. #endif
  13277. #endif
  13278. key->n.dp[0] &= (mp_digit)-2;
  13279. if (ret != 0) {
  13280. ERROR_OUT(-7804, exit_rsa_even_mod);
  13281. }
  13282. /* after loading in key use tmp as the test buffer */
  13283. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  13284. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM))) && \
  13285. !defined(WOLFSSL_XILINX_CRYPT)
  13286. /* The ARM64_ASM code that was FIPS validated did not return these expected
  13287. * failure codes. These tests cases were added after the assembly was
  13288. * in-lined in the module and validated, these tests will be available in
  13289. * the 140-3 module */
  13290. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  13291. inLen = 32;
  13292. outSz = wc_RsaEncryptSize(key);
  13293. XMEMSET(tmp, 7, plainSz);
  13294. ret = wc_RsaSSL_Sign(tmp, inLen, out, outSz, key, rng);
  13295. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  13296. ERROR_OUT(-7806, exit_rsa_even_mod);
  13297. }
  13298. ret = wc_RsaSSL_Verify(out, outSz, tmp, inLen, key);
  13299. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  13300. ERROR_OUT(-7808, exit_rsa_even_mod);
  13301. }
  13302. #endif
  13303. #ifdef WC_RSA_BLINDING
  13304. ret = wc_RsaSetRNG(key, rng);
  13305. if (ret < 0) {
  13306. ERROR_OUT(-7811, exit_rsa_even_mod);
  13307. }
  13308. #endif
  13309. /* test encrypt and decrypt using WC_RSA_NO_PAD */
  13310. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  13311. ret = wc_RsaPublicEncrypt(tmp, inLen, out, (int)outSz, key, rng);
  13312. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  13313. ERROR_OUT(-7812, exit_rsa_even_mod);
  13314. }
  13315. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  13316. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13317. ret = wc_RsaPrivateDecrypt(out, outSz, plain, (int)plainSz, key);
  13318. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_INVMOD_E) {
  13319. ERROR_OUT(-7813, exit_rsa_even_mod);
  13320. }
  13321. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  13322. #endif /* !(HAVE_FIPS_VERSION == 2 && WOLFSSL_SP_ARMxx_ASM) */
  13323. /* if making it to this point of code without hitting an ERROR_OUT then
  13324. * all tests have passed */
  13325. ret = 0;
  13326. exit_rsa_even_mod:
  13327. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13328. WC_FREE_VAR(out, HEAP_HINT);
  13329. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13330. WC_FREE_VAR(plain, HEAP_HINT);
  13331. #endif
  13332. (void)out;
  13333. (void)outSz;
  13334. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13335. (void)plain;
  13336. (void)plainSz;
  13337. #endif
  13338. (void)inLen;
  13339. (void)rng;
  13340. return ret;
  13341. }
  13342. #endif /* WOLFSSL_HAVE_SP_RSA */
  13343. #ifdef WOLFSSL_CERT_GEN
  13344. static int rsa_certgen_test(RsaKey* key, RsaKey* keypub, WC_RNG* rng, byte* tmp)
  13345. {
  13346. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13347. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13348. #ifdef WOLFSSL_TEST_CERT
  13349. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13350. #endif
  13351. #else
  13352. RsaKey caKey[1];
  13353. #ifdef WOLFSSL_TEST_CERT
  13354. DecodedCert decode[1];
  13355. #endif
  13356. #endif
  13357. byte* der = NULL;
  13358. int ret;
  13359. Cert* myCert = NULL;
  13360. int certSz;
  13361. size_t bytes3;
  13362. word32 idx3 = 0;
  13363. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  13364. XFILE file3;
  13365. #endif
  13366. #if defined(WOLFSSL_ALT_NAMES) && !defined(NO_ASN_TIME)
  13367. struct tm beforeTime;
  13368. struct tm afterTime;
  13369. #endif
  13370. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  13371. (void)keypub;
  13372. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13373. if (caKey == NULL)
  13374. ERROR_OUT(MEMORY_E, exit_rsa);
  13375. #ifdef WOLFSSL_TEST_CERT
  13376. if (decode == NULL)
  13377. ERROR_OUT(MEMORY_E, exit_rsa);
  13378. #endif
  13379. #endif
  13380. XMEMSET(caKey, 0, sizeof *caKey);
  13381. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13382. if (der == NULL) {
  13383. ERROR_OUT(-7820, exit_rsa);
  13384. }
  13385. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13386. if (myCert == NULL) {
  13387. ERROR_OUT(-7821, exit_rsa);
  13388. }
  13389. /* self signed */
  13390. if (wc_InitCert_ex(myCert, HEAP_HINT, devId)) {
  13391. ERROR_OUT(-7822, exit_rsa);
  13392. }
  13393. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  13394. XMEMCPY(myCert->serial, mySerial, sizeof(mySerial));
  13395. myCert->serialSz = (int)sizeof(mySerial);
  13396. myCert->isCA = 1;
  13397. #ifndef NO_SHA256
  13398. myCert->sigType = CTC_SHA256wRSA;
  13399. #else
  13400. myCert->sigType = CTC_SHAwRSA;
  13401. #endif
  13402. #ifdef WOLFSSL_CERT_EXT
  13403. /* add Policies */
  13404. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  13405. CTC_MAX_CERTPOL_SZ);
  13406. XSTRNCPY(myCert->certPolicies[1], "1.2.840.113549.1.9.16.6.5",
  13407. CTC_MAX_CERTPOL_SZ);
  13408. myCert->certPoliciesNb = 2;
  13409. /* add SKID from the Public Key */
  13410. if (wc_SetSubjectKeyIdFromPublicKey(myCert, keypub, NULL) != 0) {
  13411. ERROR_OUT(-7823, exit_rsa);
  13412. }
  13413. /* add AKID from the Public Key */
  13414. if (wc_SetAuthKeyIdFromPublicKey(myCert, keypub, NULL) != 0) {
  13415. ERROR_OUT(-7824, exit_rsa);
  13416. }
  13417. /* add Key Usage */
  13418. if (wc_SetKeyUsage(myCert,"cRLSign,keyCertSign") != 0) {
  13419. ERROR_OUT(-7825, exit_rsa);
  13420. }
  13421. #ifdef WOLFSSL_EKU_OID
  13422. {
  13423. const char unique[] = "2.16.840.1.111111.100.1.10.1";
  13424. if (wc_SetExtKeyUsageOID(myCert, unique, sizeof(unique), 0,
  13425. HEAP_HINT) != 0) {
  13426. ERROR_OUT(-7826, exit_rsa);
  13427. }
  13428. }
  13429. #endif /* WOLFSSL_EKU_OID */
  13430. #endif /* WOLFSSL_CERT_EXT */
  13431. ret = 0;
  13432. do {
  13433. #if defined(WOLFSSL_ASYNC_CRYPT)
  13434. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13435. #endif
  13436. if (ret >= 0) {
  13437. ret = wc_MakeSelfCert(myCert, der, FOURK_BUF, key, rng);
  13438. }
  13439. } while (ret == WC_PENDING_E);
  13440. if (ret < 0) {
  13441. ERROR_OUT(-7827, exit_rsa);
  13442. }
  13443. certSz = ret;
  13444. #ifdef WOLFSSL_TEST_CERT
  13445. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  13446. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  13447. if (ret != 0) {
  13448. FreeDecodedCert(decode);
  13449. ERROR_OUT(-7828, exit_rsa);
  13450. }
  13451. FreeDecodedCert(decode);
  13452. #endif
  13453. ret = SaveDerAndPem(der, certSz, certDerFile, certPemFile,
  13454. CERT_TYPE, -5578);
  13455. if (ret != 0) {
  13456. goto exit_rsa;
  13457. }
  13458. /* Setup Certificate */
  13459. if (wc_InitCert_ex(myCert, HEAP_HINT, devId)) {
  13460. ERROR_OUT(-7829, exit_rsa);
  13461. }
  13462. #ifdef WOLFSSL_ALT_NAMES
  13463. /* Get CA Cert for testing */
  13464. #ifdef USE_CERT_BUFFERS_1024
  13465. XMEMCPY(tmp, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  13466. bytes3 = sizeof_ca_cert_der_1024;
  13467. #elif defined(USE_CERT_BUFFERS_2048)
  13468. XMEMCPY(tmp, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  13469. bytes3 = sizeof_ca_cert_der_2048;
  13470. #else
  13471. file3 = XFOPEN(rsaCaCertDerFile, "rb");
  13472. if (!file3) {
  13473. ERROR_OUT(-7830, exit_rsa);
  13474. }
  13475. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  13476. XFCLOSE(file3);
  13477. #endif /* USE_CERT_BUFFERS */
  13478. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_1024) && \
  13479. !defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  13480. ret = wc_SetAltNames(myCert, rsaCaCertFile);
  13481. if (ret != 0) {
  13482. ERROR_OUT(-7831, exit_rsa);
  13483. }
  13484. #endif
  13485. /* get alt names from der */
  13486. ret = wc_SetAltNamesBuffer(myCert, tmp, (int)bytes3);
  13487. if (ret != 0) {
  13488. ERROR_OUT(-7832, exit_rsa);
  13489. }
  13490. /* get dates from der */
  13491. ret = wc_SetDatesBuffer(myCert, tmp, (int)bytes3);
  13492. if (ret != 0) {
  13493. ERROR_OUT(-7833, exit_rsa);
  13494. }
  13495. #ifndef NO_ASN_TIME
  13496. ret = wc_GetCertDates(myCert, &beforeTime, &afterTime);
  13497. if (ret < 0) {
  13498. ERROR_OUT(-7834, exit_rsa);
  13499. }
  13500. #endif
  13501. #endif /* WOLFSSL_ALT_NAMES */
  13502. /* Get CA Key */
  13503. #ifdef USE_CERT_BUFFERS_1024
  13504. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  13505. bytes3 = sizeof_ca_key_der_1024;
  13506. #elif defined(USE_CERT_BUFFERS_2048)
  13507. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  13508. bytes3 = sizeof_ca_key_der_2048;
  13509. #else
  13510. file3 = XFOPEN(rsaCaKeyFile, "rb");
  13511. if (!file3) {
  13512. ERROR_OUT(-7835, exit_rsa);
  13513. }
  13514. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  13515. XFCLOSE(file3);
  13516. #endif /* USE_CERT_BUFFERS */
  13517. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  13518. if (ret != 0) {
  13519. ERROR_OUT(-7836, exit_rsa);
  13520. }
  13521. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  13522. if (ret != 0) {
  13523. ERROR_OUT(-7837, exit_rsa);
  13524. }
  13525. #ifndef NO_SHA256
  13526. myCert->sigType = CTC_SHA256wRSA;
  13527. #else
  13528. myCert->sigType = CTC_SHAwRSA;
  13529. #endif
  13530. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  13531. #ifdef WOLFSSL_CERT_EXT
  13532. /* add Policies */
  13533. XSTRNCPY(myCert->certPolicies[0], "2.16.840.1.101.3.4.1.42",
  13534. CTC_MAX_CERTPOL_SZ);
  13535. myCert->certPoliciesNb =1;
  13536. /* add SKID from the Public Key */
  13537. if (wc_SetSubjectKeyIdFromPublicKey(myCert, key, NULL) != 0) {
  13538. ERROR_OUT(-7838, exit_rsa);
  13539. }
  13540. /* add AKID from the CA certificate */
  13541. #if defined(USE_CERT_BUFFERS_2048)
  13542. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  13543. sizeof_ca_cert_der_2048);
  13544. #elif defined(USE_CERT_BUFFERS_1024)
  13545. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  13546. sizeof_ca_cert_der_1024);
  13547. #else
  13548. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  13549. #endif
  13550. if (ret != 0) {
  13551. ERROR_OUT(-7839, exit_rsa);
  13552. }
  13553. /* add Key Usage */
  13554. if (wc_SetKeyUsage(myCert,"keyEncipherment,keyAgreement") != 0) {
  13555. ERROR_OUT(-7840, exit_rsa);
  13556. }
  13557. #endif /* WOLFSSL_CERT_EXT */
  13558. #if defined(USE_CERT_BUFFERS_2048)
  13559. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  13560. sizeof_ca_cert_der_2048);
  13561. #elif defined(USE_CERT_BUFFERS_1024)
  13562. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  13563. sizeof_ca_cert_der_1024);
  13564. #else
  13565. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  13566. #endif
  13567. if (ret < 0) {
  13568. ERROR_OUT(-7841, exit_rsa);
  13569. }
  13570. certSz = wc_MakeCert(myCert, der, FOURK_BUF, key, NULL, rng);
  13571. if (certSz < 0) {
  13572. ERROR_OUT(-7842, exit_rsa);
  13573. }
  13574. ret = 0;
  13575. do {
  13576. #if defined(WOLFSSL_ASYNC_CRYPT)
  13577. ret = wc_AsyncWait(ret, &caKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13578. #endif
  13579. if (ret >= 0) {
  13580. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der, FOURK_BUF,
  13581. caKey, NULL, rng);
  13582. }
  13583. } while (ret == WC_PENDING_E);
  13584. if (ret < 0) {
  13585. ERROR_OUT(-7843, exit_rsa);
  13586. }
  13587. certSz = ret;
  13588. #ifdef WOLFSSL_TEST_CERT
  13589. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  13590. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  13591. if (ret != 0) {
  13592. FreeDecodedCert(decode);
  13593. ERROR_OUT(-7844, exit_rsa);
  13594. }
  13595. FreeDecodedCert(decode);
  13596. #endif
  13597. ret = SaveDerAndPem(der, certSz, otherCertDerFile, otherCertPemFile,
  13598. CERT_TYPE, -5598);
  13599. if (ret != 0) {
  13600. goto exit_rsa;
  13601. }
  13602. exit_rsa:
  13603. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13604. if (caKey != NULL) {
  13605. wc_FreeRsaKey(caKey);
  13606. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13607. }
  13608. #ifdef WOLFSSL_TEST_CERT
  13609. if (decode != NULL)
  13610. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13611. #endif
  13612. #else
  13613. wc_FreeRsaKey(caKey);
  13614. #endif
  13615. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13616. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13617. return ret;
  13618. }
  13619. #endif
  13620. #if !defined(NO_RSA) && defined(HAVE_ECC) && defined(WOLFSSL_CERT_GEN)
  13621. /* Make Cert / Sign example for ECC cert and RSA CA */
  13622. static int rsa_ecc_certgen_test(WC_RNG* rng, byte* tmp)
  13623. {
  13624. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13625. RsaKey *caKey = (RsaKey *)XMALLOC(sizeof *caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13626. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13627. ecc_key *caEccKeyPub = (ecc_key *)XMALLOC(sizeof *caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13628. #ifdef WOLFSSL_TEST_CERT
  13629. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13630. #endif
  13631. #else
  13632. RsaKey caKey[1];
  13633. ecc_key caEccKey[1];
  13634. ecc_key caEccKeyPub[1];
  13635. #ifdef WOLFSSL_TEST_CERT
  13636. DecodedCert decode[1];
  13637. #endif
  13638. #endif
  13639. byte* der = NULL;
  13640. Cert* myCert = NULL;
  13641. int certSz;
  13642. size_t bytes3;
  13643. word32 idx3 = 0;
  13644. #if (!defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)) \
  13645. || !defined(USE_CERT_BUFFERS_256)
  13646. XFILE file3;
  13647. #endif
  13648. int ret;
  13649. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13650. if ((caKey == NULL) || (caEccKey == NULL) || (caEccKeyPub == NULL)
  13651. #ifdef WOLFSSL_TEST_CERT
  13652. || (decode == NULL)
  13653. #endif
  13654. )
  13655. ERROR_OUT(MEMORY_E, exit_rsa);
  13656. #endif
  13657. XMEMSET(caKey, 0, sizeof *caKey);
  13658. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  13659. XMEMSET(caEccKeyPub, 0, sizeof *caEccKeyPub);
  13660. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13661. if (der == NULL) {
  13662. ERROR_OUT(-7850, exit_rsa);
  13663. }
  13664. myCert = (Cert*)XMALLOC(sizeof(Cert), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13665. if (myCert == NULL) {
  13666. ERROR_OUT(-7851, exit_rsa);
  13667. }
  13668. /* Get CA Key */
  13669. #ifdef USE_CERT_BUFFERS_1024
  13670. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  13671. bytes3 = sizeof_ca_key_der_1024;
  13672. #elif defined(USE_CERT_BUFFERS_2048)
  13673. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  13674. bytes3 = sizeof_ca_key_der_2048;
  13675. #else
  13676. file3 = XFOPEN(rsaCaKeyFile, "rb");
  13677. if (!file3) {
  13678. ERROR_OUT(-7852, exit_rsa);
  13679. }
  13680. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  13681. XFCLOSE(file3);
  13682. #endif /* USE_CERT_BUFFERS */
  13683. ret = wc_InitRsaKey(caKey, HEAP_HINT);
  13684. if (ret != 0) {
  13685. ERROR_OUT(-7853, exit_rsa);
  13686. }
  13687. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, caKey, (word32)bytes3);
  13688. if (ret != 0) {
  13689. ERROR_OUT(-7854, exit_rsa);
  13690. }
  13691. /* Get Cert Key */
  13692. #ifdef USE_CERT_BUFFERS_256
  13693. XMEMCPY(tmp, ecc_key_pub_der_256, sizeof_ecc_key_pub_der_256);
  13694. bytes3 = sizeof_ecc_key_pub_der_256;
  13695. #else
  13696. file3 = XFOPEN(eccKeyPubFileDer, "rb");
  13697. if (!file3) {
  13698. ERROR_OUT(-7855, exit_rsa);
  13699. }
  13700. bytes3 = XFREAD(tmp, 1, FOURK_BUF, file3);
  13701. XFCLOSE(file3);
  13702. #endif
  13703. ret = wc_ecc_init_ex(caEccKeyPub, HEAP_HINT, devId);
  13704. if (ret != 0) {
  13705. ERROR_OUT(-7856, exit_rsa);
  13706. }
  13707. idx3 = 0;
  13708. ret = wc_EccPublicKeyDecode(tmp, &idx3, caEccKeyPub, (word32)bytes3);
  13709. if (ret != 0) {
  13710. ERROR_OUT(-7857, exit_rsa);
  13711. }
  13712. /* Setup Certificate */
  13713. if (wc_InitCert_ex(myCert, HEAP_HINT, devId)) {
  13714. ERROR_OUT(-7858, exit_rsa);
  13715. }
  13716. #ifndef NO_SHA256
  13717. myCert->sigType = CTC_SHA256wRSA;
  13718. #else
  13719. myCert->sigType = CTC_SHAwRSA;
  13720. #endif
  13721. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  13722. #ifdef WOLFSSL_CERT_EXT
  13723. /* add Policies */
  13724. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  13725. CTC_MAX_CERTPOL_SZ);
  13726. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  13727. CTC_MAX_CERTPOL_SZ);
  13728. myCert->certPoliciesNb = 2;
  13729. /* add SKID from the Public Key */
  13730. if (wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, caEccKeyPub) != 0) {
  13731. ERROR_OUT(-7859, exit_rsa);
  13732. }
  13733. /* add AKID from the CA certificate */
  13734. #if defined(USE_CERT_BUFFERS_2048)
  13735. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_2048,
  13736. sizeof_ca_cert_der_2048);
  13737. #elif defined(USE_CERT_BUFFERS_1024)
  13738. ret = wc_SetAuthKeyIdFromCert(myCert, ca_cert_der_1024,
  13739. sizeof_ca_cert_der_1024);
  13740. #else
  13741. ret = wc_SetAuthKeyId(myCert, rsaCaCertFile);
  13742. #endif
  13743. if (ret != 0) {
  13744. ERROR_OUT(-7860, exit_rsa);
  13745. }
  13746. /* add Key Usage */
  13747. if (wc_SetKeyUsage(myCert, certKeyUsage) != 0) {
  13748. ERROR_OUT(-7861, exit_rsa);
  13749. }
  13750. #endif /* WOLFSSL_CERT_EXT */
  13751. #if defined(USE_CERT_BUFFERS_2048)
  13752. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_2048,
  13753. sizeof_ca_cert_der_2048);
  13754. #elif defined(USE_CERT_BUFFERS_1024)
  13755. ret = wc_SetIssuerBuffer(myCert, ca_cert_der_1024,
  13756. sizeof_ca_cert_der_1024);
  13757. #else
  13758. ret = wc_SetIssuer(myCert, rsaCaCertFile);
  13759. #endif
  13760. if (ret < 0) {
  13761. ERROR_OUT(-7862, exit_rsa);
  13762. }
  13763. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, caEccKeyPub, rng);
  13764. if (certSz < 0) {
  13765. ERROR_OUT(-7863, exit_rsa);
  13766. }
  13767. ret = 0;
  13768. do {
  13769. #if defined(WOLFSSL_ASYNC_CRYPT)
  13770. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13771. #endif
  13772. if (ret >= 0) {
  13773. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  13774. FOURK_BUF, caKey, NULL, rng);
  13775. }
  13776. } while (ret == WC_PENDING_E);
  13777. if (ret < 0) {
  13778. ERROR_OUT(-7864, exit_rsa);
  13779. }
  13780. certSz = ret;
  13781. #ifdef WOLFSSL_TEST_CERT
  13782. InitDecodedCert(decode, der, certSz, 0);
  13783. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  13784. if (ret != 0) {
  13785. FreeDecodedCert(decode);
  13786. ERROR_OUT(-7865, exit_rsa);
  13787. }
  13788. FreeDecodedCert(decode);
  13789. #endif
  13790. ret = SaveDerAndPem(der, certSz, certEccRsaDerFile, certEccRsaPemFile,
  13791. CERT_TYPE, -5616);
  13792. if (ret != 0) {
  13793. goto exit_rsa;
  13794. }
  13795. exit_rsa:
  13796. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13797. if (caKey != NULL) {
  13798. wc_FreeRsaKey(caKey);
  13799. XFREE(caKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13800. }
  13801. if (caEccKey != NULL) {
  13802. wc_ecc_free(caEccKey);
  13803. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13804. }
  13805. if (caEccKeyPub != NULL) {
  13806. wc_ecc_free(caEccKeyPub);
  13807. XFREE(caEccKeyPub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13808. }
  13809. #ifdef WOLFSSL_TEST_CERT
  13810. if (decode != NULL)
  13811. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13812. #endif
  13813. #else
  13814. wc_FreeRsaKey(caKey);
  13815. wc_ecc_free(caEccKey);
  13816. wc_ecc_free(caEccKeyPub);
  13817. #endif
  13818. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13819. myCert = NULL;
  13820. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13821. der = NULL;
  13822. if (ret >= 0)
  13823. ret = 0;
  13824. return ret;
  13825. }
  13826. #endif /* !NO_RSA && HAVE_ECC && WOLFSSL_CERT_GEN */
  13827. #ifdef WOLFSSL_KEY_GEN
  13828. static int rsa_keygen_test(WC_RNG* rng)
  13829. {
  13830. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13831. RsaKey *genKey = (RsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13832. #else
  13833. RsaKey genKey[1];
  13834. #endif
  13835. int ret;
  13836. byte* der = NULL;
  13837. #ifndef WOLFSSL_CRYPTOCELL
  13838. word32 idx = 0;
  13839. #endif
  13840. int derSz = 0;
  13841. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FIPS)
  13842. int keySz = 1024;
  13843. #else
  13844. int keySz = 2048;
  13845. #endif
  13846. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13847. if (! genKey)
  13848. ERROR_OUT(MEMORY_E, exit_rsa);
  13849. #endif
  13850. XMEMSET(genKey, 0, sizeof *genKey);
  13851. ret = wc_InitRsaKey_ex(genKey, HEAP_HINT, devId);
  13852. if (ret != 0) {
  13853. ERROR_OUT(-7870, exit_rsa);
  13854. }
  13855. #ifdef HAVE_FIPS
  13856. for (;;) {
  13857. #endif
  13858. ret = wc_MakeRsaKey(genKey, keySz, WC_RSA_EXPONENT, rng);
  13859. #if defined(WOLFSSL_ASYNC_CRYPT)
  13860. ret = wc_AsyncWait(ret, &genKey->asyncDev, WC_ASYNC_FLAG_NONE);
  13861. #endif
  13862. #ifdef HAVE_FIPS
  13863. if (ret == PRIME_GEN_E)
  13864. continue;
  13865. break;
  13866. }
  13867. #endif
  13868. if (ret != 0) {
  13869. ERROR_OUT(-7871, exit_rsa);
  13870. }
  13871. TEST_SLEEP();
  13872. #ifdef WOLFSSL_RSA_KEY_CHECK
  13873. ret = wc_CheckRsaKey(genKey);
  13874. if (ret != 0) {
  13875. ERROR_OUT(-7872, exit_rsa);
  13876. }
  13877. #endif
  13878. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13879. if (der == NULL) {
  13880. ERROR_OUT(-7873, exit_rsa);
  13881. }
  13882. derSz = wc_RsaKeyToDer(genKey, der, FOURK_BUF);
  13883. if (derSz < 0) {
  13884. ERROR_OUT(-7874, exit_rsa);
  13885. }
  13886. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  13887. PRIVATEKEY_TYPE, -5555);
  13888. if (ret != 0) {
  13889. goto exit_rsa;
  13890. }
  13891. wc_FreeRsaKey(genKey);
  13892. ret = wc_InitRsaKey(genKey, HEAP_HINT);
  13893. if (ret != 0) {
  13894. ERROR_OUT(-7875, exit_rsa);
  13895. }
  13896. #ifndef WOLFSSL_CRYPTOCELL
  13897. idx = 0;
  13898. /* The private key part of the key gen pairs from cryptocell can't be exported */
  13899. ret = wc_RsaPrivateKeyDecode(der, &idx, genKey, derSz);
  13900. if (ret != 0) {
  13901. ERROR_OUT(-7876, exit_rsa);
  13902. }
  13903. #endif /* WOLFSSL_CRYPTOCELL */
  13904. exit_rsa:
  13905. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  13906. if (genKey) {
  13907. wc_FreeRsaKey(genKey);
  13908. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13909. }
  13910. #else
  13911. wc_FreeRsaKey(genKey);
  13912. #endif
  13913. if (der != NULL) {
  13914. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  13915. der = NULL;
  13916. }
  13917. return ret;
  13918. }
  13919. #endif
  13920. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  13921. #if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG) && \
  13922. !defined(HAVE_FAST_RSA) && !defined(HAVE_USER_RSA) && \
  13923. (!defined(HAVE_FIPS) || \
  13924. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) \
  13925. && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  13926. static int rsa_oaep_padding_test(RsaKey* key, WC_RNG* rng)
  13927. {
  13928. int ret = 0;
  13929. word32 idx = 0;
  13930. const char inStr[] = TEST_STRING;
  13931. const word32 inLen = (word32)TEST_STRING_SZ;
  13932. const word32 outSz = RSA_TEST_BYTES;
  13933. const word32 plainSz = RSA_TEST_BYTES;
  13934. byte* res = NULL;
  13935. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  13936. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  13937. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  13938. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  13939. if (in == NULL || out == NULL || plain == NULL)
  13940. ERROR_OUT(MEMORY_E, exit_rsa);
  13941. #endif
  13942. XMEMCPY(in, inStr, inLen);
  13943. #ifndef NO_SHA
  13944. do {
  13945. #if defined(WOLFSSL_ASYNC_CRYPT)
  13946. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13947. #endif
  13948. if (ret >= 0) {
  13949. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  13950. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  13951. }
  13952. } while (ret == WC_PENDING_E);
  13953. if (ret < 0) {
  13954. ERROR_OUT(-7918, exit_rsa);
  13955. }
  13956. TEST_SLEEP();
  13957. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13958. idx = (word32)ret;
  13959. do {
  13960. #if defined(WOLFSSL_ASYNC_CRYPT)
  13961. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13962. #endif
  13963. if (ret >= 0) {
  13964. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  13965. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  13966. }
  13967. } while (ret == WC_PENDING_E);
  13968. if (ret < 0) {
  13969. ERROR_OUT(-7919, exit_rsa);
  13970. }
  13971. if (XMEMCMP(plain, in, inLen)) {
  13972. ERROR_OUT(-7920, exit_rsa);
  13973. }
  13974. TEST_SLEEP();
  13975. #endif /* NO_SHA */
  13976. #endif
  13977. #ifndef NO_SHA256
  13978. XMEMSET(plain, 0, plainSz);
  13979. do {
  13980. #if defined(WOLFSSL_ASYNC_CRYPT)
  13981. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13982. #endif
  13983. if (ret >= 0) {
  13984. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  13985. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  13986. }
  13987. } while (ret == WC_PENDING_E);
  13988. if (ret < 0) {
  13989. ERROR_OUT(-7921, exit_rsa);
  13990. }
  13991. TEST_SLEEP();
  13992. idx = (word32)ret;
  13993. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  13994. do {
  13995. #if defined(WOLFSSL_ASYNC_CRYPT)
  13996. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  13997. #endif
  13998. if (ret >= 0) {
  13999. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14000. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  14001. }
  14002. } while (ret == WC_PENDING_E);
  14003. if (ret < 0) {
  14004. ERROR_OUT(-7922, exit_rsa);
  14005. }
  14006. if (XMEMCMP(plain, in, inLen)) {
  14007. ERROR_OUT(-7923, exit_rsa);
  14008. }
  14009. TEST_SLEEP();
  14010. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14011. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14012. do {
  14013. #if defined(WOLFSSL_ASYNC_CRYPT)
  14014. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14015. #endif
  14016. if (ret >= 0) {
  14017. ret = wc_RsaPrivateDecryptInline_ex(out, idx, &res, key,
  14018. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  14019. }
  14020. } while (ret == WC_PENDING_E);
  14021. if (ret < 0) {
  14022. ERROR_OUT(-7924, exit_rsa);
  14023. }
  14024. if (ret != (int)inLen) {
  14025. ERROR_OUT(-7925, exit_rsa);
  14026. }
  14027. if (XMEMCMP(res, in, inLen)) {
  14028. ERROR_OUT(-7926, exit_rsa);
  14029. }
  14030. TEST_SLEEP();
  14031. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14032. /* check fails if not using the same optional label */
  14033. XMEMSET(plain, 0, plainSz);
  14034. do {
  14035. #if defined(WOLFSSL_ASYNC_CRYPT)
  14036. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14037. #endif
  14038. if (ret >= 0) {
  14039. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14040. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  14041. }
  14042. } while (ret == WC_PENDING_E);
  14043. if (ret < 0) {
  14044. ERROR_OUT(-7927, exit_rsa);
  14045. }
  14046. TEST_SLEEP();
  14047. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  14048. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  14049. !defined(WOLFSSL_CRYPTOCELL)
  14050. /* label is unused in cryptocell so it won't detect decrypt error due to label */
  14051. idx = (word32)ret;
  14052. do {
  14053. #if defined(WOLFSSL_ASYNC_CRYPT)
  14054. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14055. #endif
  14056. if (ret >= 0) {
  14057. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14058. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  14059. }
  14060. } while (ret == WC_PENDING_E);
  14061. if (ret > 0) { /* in this case decrypt should fail */
  14062. ERROR_OUT(-7928, exit_rsa);
  14063. }
  14064. ret = 0;
  14065. TEST_SLEEP();
  14066. #endif /* !HAVE_CAVIUM */
  14067. /* check using optional label with encrypt/decrypt */
  14068. XMEMSET(plain, 0, plainSz);
  14069. do {
  14070. #if defined(WOLFSSL_ASYNC_CRYPT)
  14071. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14072. #endif
  14073. if (ret >= 0) {
  14074. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14075. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  14076. }
  14077. } while (ret == WC_PENDING_E);
  14078. if (ret < 0) {
  14079. ERROR_OUT(-7929, exit_rsa);
  14080. }
  14081. TEST_SLEEP();
  14082. idx = (word32)ret;
  14083. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14084. do {
  14085. #if defined(WOLFSSL_ASYNC_CRYPT)
  14086. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14087. #endif
  14088. if (ret >= 0) {
  14089. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14090. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  14091. }
  14092. } while (ret == WC_PENDING_E);
  14093. if (ret < 0) {
  14094. ERROR_OUT(-7930, exit_rsa);
  14095. }
  14096. if (XMEMCMP(plain, in, inLen)) {
  14097. ERROR_OUT(-7931, exit_rsa);
  14098. }
  14099. TEST_SLEEP();
  14100. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14101. #ifndef NO_SHA
  14102. /* check fail using mismatch hash algorithms */
  14103. XMEMSET(plain, 0, plainSz);
  14104. do {
  14105. #if defined(WOLFSSL_ASYNC_CRYPT)
  14106. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14107. #endif
  14108. if (ret >= 0) {
  14109. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14110. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, in, inLen);
  14111. }
  14112. } while (ret == WC_PENDING_E);
  14113. if (ret < 0) {
  14114. ERROR_OUT(-7932, exit_rsa);
  14115. }
  14116. TEST_SLEEP();
  14117. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  14118. #if !defined(HAVE_CAVIUM) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  14119. !defined(WOLFSSL_CRYPTOCELL)
  14120. idx = (word32)ret;
  14121. do {
  14122. #if defined(WOLFSSL_ASYNC_CRYPT)
  14123. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14124. #endif
  14125. if (ret >= 0) {
  14126. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14127. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256,
  14128. in, inLen);
  14129. }
  14130. } while (ret == WC_PENDING_E);
  14131. if (ret > 0) { /* should fail */
  14132. ERROR_OUT(-7933, exit_rsa);
  14133. }
  14134. ret = 0;
  14135. TEST_SLEEP();
  14136. #endif /* !HAVE_CAVIUM */
  14137. #endif /* NO_SHA */
  14138. #endif /* NO_SHA256 */
  14139. #ifdef WOLFSSL_SHA512
  14140. /* Check valid RSA key size is used while using hash length of SHA512
  14141. If key size is less than (hash length * 2) + 2 then is invalid use
  14142. and test, since OAEP padding requires this.
  14143. BAD_FUNC_ARG is returned when this case is not met */
  14144. if (wc_RsaEncryptSize(key) > ((int)WC_SHA512_DIGEST_SIZE * 2) + 2) {
  14145. XMEMSET(plain, 0, plainSz);
  14146. do {
  14147. #if defined(WOLFSSL_ASYNC_CRYPT)
  14148. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14149. #endif
  14150. if (ret >= 0) {
  14151. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14152. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  14153. }
  14154. } while (ret == WC_PENDING_E);
  14155. if (ret < 0) {
  14156. ERROR_OUT(-7934, exit_rsa);
  14157. }
  14158. TEST_SLEEP();
  14159. idx = ret;
  14160. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14161. do {
  14162. #if defined(WOLFSSL_ASYNC_CRYPT)
  14163. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14164. #endif
  14165. if (ret >= 0) {
  14166. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14167. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  14168. }
  14169. } while (ret == WC_PENDING_E);
  14170. if (ret < 0) {
  14171. ERROR_OUT(-7935, exit_rsa);
  14172. }
  14173. if (XMEMCMP(plain, in, inLen)) {
  14174. ERROR_OUT(-7936, exit_rsa);
  14175. }
  14176. TEST_SLEEP();
  14177. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14178. }
  14179. #endif /* WOLFSSL_SHA512 */
  14180. /* check using pkcsv15 padding with _ex API */
  14181. XMEMSET(plain, 0, plainSz);
  14182. do {
  14183. #if defined(WOLFSSL_ASYNC_CRYPT)
  14184. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14185. #endif
  14186. if (ret >= 0) {
  14187. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, key, rng,
  14188. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  14189. }
  14190. } while (ret == WC_PENDING_E);
  14191. if (ret < 0) {
  14192. ERROR_OUT(-7937, exit_rsa);
  14193. }
  14194. TEST_SLEEP();
  14195. idx = (word32)ret;
  14196. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  14197. do {
  14198. #if defined(WOLFSSL_ASYNC_CRYPT)
  14199. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14200. #endif
  14201. if (ret >= 0) {
  14202. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, key,
  14203. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  14204. }
  14205. } while (ret == WC_PENDING_E);
  14206. if (ret < 0) {
  14207. ERROR_OUT(-7938, exit_rsa);
  14208. }
  14209. if (XMEMCMP(plain, in, inLen)) {
  14210. ERROR_OUT(-7939, exit_rsa);
  14211. }
  14212. TEST_SLEEP();
  14213. #endif /* WOLFSSL_RSA_PUBLIC_ONLY */
  14214. exit_rsa:
  14215. WC_FREE_VAR(in, HEAP_HINT);
  14216. WC_FREE_VAR(out, HEAP_HINT);
  14217. WC_FREE_VAR(plain, HEAP_HINT);
  14218. (void)idx;
  14219. (void)inStr;
  14220. (void)res;
  14221. if (ret >= 0)
  14222. ret = 0;
  14223. return ret;
  14224. }
  14225. #endif
  14226. #endif
  14227. WOLFSSL_TEST_SUBROUTINE int rsa_test(void)
  14228. {
  14229. int ret;
  14230. size_t bytes;
  14231. WC_RNG rng;
  14232. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14233. byte* tmp = NULL;
  14234. byte* der = NULL;
  14235. RsaKey *key = (RsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14236. #else
  14237. RsaKey key[1];
  14238. byte tmp[FOURK_BUF];
  14239. #endif
  14240. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  14241. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14242. RsaKey *keypub = (RsaKey *)XMALLOC(sizeof *keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14243. #else
  14244. RsaKey keypub[1];
  14245. #endif
  14246. #endif
  14247. word32 idx = 0;
  14248. const char inStr[] = TEST_STRING;
  14249. const word32 inLen = (word32)TEST_STRING_SZ;
  14250. const word32 outSz = RSA_TEST_BYTES;
  14251. const word32 plainSz = RSA_TEST_BYTES;
  14252. byte* res = NULL;
  14253. #ifndef NO_SIG_WRAPPER
  14254. int modLen;
  14255. #endif
  14256. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  14257. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  14258. !defined(NO_FILESYSTEM)
  14259. XFILE file;
  14260. #ifdef WOLFSSL_TEST_CERT
  14261. XFILE file2;
  14262. #endif
  14263. #endif
  14264. #ifdef WOLFSSL_TEST_CERT
  14265. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14266. DecodedCert *cert = (DecodedCert *)XMALLOC(sizeof *cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14267. #else
  14268. DecodedCert cert[1];
  14269. #endif
  14270. #ifndef NO_ASN_TIME
  14271. struct tm timearg;
  14272. const byte* date;
  14273. byte dateFormat;
  14274. int dateLength;
  14275. #endif
  14276. #endif
  14277. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, HEAP_HINT);
  14278. WC_DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  14279. WC_DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  14280. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  14281. if (in == NULL || out == NULL || plain == NULL)
  14282. ERROR_OUT(MEMORY_E, exit_rsa);
  14283. #endif
  14284. XMEMCPY(in, inStr, inLen);
  14285. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14286. if (key == NULL)
  14287. ERROR_OUT(MEMORY_E, exit_rsa);
  14288. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  14289. if (keypub == NULL)
  14290. ERROR_OUT(MEMORY_E, exit_rsa);
  14291. #endif
  14292. #ifdef WOLFSSL_TEST_CERT
  14293. if (cert == NULL)
  14294. ERROR_OUT(MEMORY_E, exit_rsa);
  14295. #endif
  14296. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  14297. /* initialize stack structures */
  14298. XMEMSET(&rng, 0, sizeof(rng));
  14299. XMEMSET(key, 0, sizeof *key);
  14300. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  14301. XMEMSET(keypub, 0, sizeof *keypub);
  14302. #endif
  14303. #if !defined(HAVE_USER_RSA) && !defined(NO_ASN)
  14304. ret = rsa_decode_test(key);
  14305. if (ret != 0)
  14306. ERROR_OUT(ret, exit_rsa);
  14307. #endif
  14308. #ifdef USE_CERT_BUFFERS_1024
  14309. bytes = (size_t)sizeof_client_key_der_1024;
  14310. if (bytes < (size_t)sizeof_client_cert_der_1024)
  14311. bytes = (size_t)sizeof_client_cert_der_1024;
  14312. #elif defined(USE_CERT_BUFFERS_2048)
  14313. bytes = (size_t)sizeof_client_key_der_2048;
  14314. if (bytes < (size_t)sizeof_client_cert_der_2048)
  14315. bytes = (size_t)sizeof_client_cert_der_2048;
  14316. #elif defined(USE_CERT_BUFFERS_3072)
  14317. bytes = (size_t)sizeof_client_key_der_3072;
  14318. if (bytes < (size_t)sizeof_client_cert_der_3072)
  14319. bytes = (size_t)sizeof_client_cert_der_3072;
  14320. #elif defined(USE_CERT_BUFFERS_4096)
  14321. bytes = (size_t)sizeof_client_key_der_4096;
  14322. if (bytes < (size_t)sizeof_client_cert_der_4096)
  14323. bytes = (size_t)sizeof_client_cert_der_4096;
  14324. #else
  14325. bytes = FOURK_BUF;
  14326. #endif
  14327. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14328. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14329. if (tmp == NULL)
  14330. ERROR_OUT(-7900, exit_rsa);
  14331. #endif
  14332. #ifdef USE_CERT_BUFFERS_1024
  14333. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  14334. #elif defined(USE_CERT_BUFFERS_2048)
  14335. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  14336. #elif defined(USE_CERT_BUFFERS_3072)
  14337. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  14338. #elif defined(USE_CERT_BUFFERS_4096)
  14339. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  14340. #elif !defined(NO_FILESYSTEM)
  14341. file = XFOPEN(clientKey, "rb");
  14342. if (!file) {
  14343. err_sys("can't open ./certs/client-key.der, "
  14344. "Please run from wolfSSL home dir", -40);
  14345. ERROR_OUT(-7901, exit_rsa);
  14346. }
  14347. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  14348. XFCLOSE(file);
  14349. #else
  14350. /* No key to use. */
  14351. ERROR_OUT(-7902, exit_rsa);
  14352. #endif /* USE_CERT_BUFFERS */
  14353. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  14354. if (ret != 0) {
  14355. ERROR_OUT(-7903, exit_rsa);
  14356. }
  14357. #ifndef NO_ASN
  14358. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  14359. if (ret != 0) {
  14360. ERROR_OUT(-7904, exit_rsa);
  14361. }
  14362. #ifndef NO_SIG_WRAPPER
  14363. modLen = wc_RsaEncryptSize(key);
  14364. #endif
  14365. #else
  14366. #ifdef USE_CERT_BUFFERS_2048
  14367. ret = mp_read_unsigned_bin(&key->n, &tmp[12], 256);
  14368. if (ret != 0) {
  14369. ERROR_OUT(-7905, exit_rsa);
  14370. }
  14371. ret = mp_set_int(&key->e, WC_RSA_EXPONENT);
  14372. if (ret != 0) {
  14373. ERROR_OUT(-7906, exit_rsa);
  14374. }
  14375. #ifndef NO_SIG_WRAPPER
  14376. modLen = 2048;
  14377. #endif
  14378. #else
  14379. #error Not supported yet!
  14380. #endif
  14381. #endif
  14382. #ifndef WC_NO_RNG
  14383. #ifndef HAVE_FIPS
  14384. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  14385. #else
  14386. ret = wc_InitRng(&rng);
  14387. #endif
  14388. if (ret != 0) {
  14389. ERROR_OUT(-7907, exit_rsa);
  14390. }
  14391. #endif
  14392. #ifndef NO_SIG_WRAPPER
  14393. ret = rsa_sig_test(key, sizeof *key, modLen, &rng);
  14394. if (ret != 0)
  14395. goto exit_rsa;
  14396. #endif
  14397. #ifdef WC_RSA_NONBLOCK
  14398. ret = rsa_nb_test(key, in, inLen, out, outSz, plain, plainSz, &rng);
  14399. if (ret != 0)
  14400. goto exit_rsa;
  14401. #endif
  14402. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  14403. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14404. do {
  14405. #if defined(WOLFSSL_ASYNC_CRYPT)
  14406. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14407. #endif
  14408. if (ret >= 0) {
  14409. ret = wc_RsaPublicEncrypt(in, inLen, out, outSz, key, &rng);
  14410. }
  14411. } while (ret == WC_PENDING_E);
  14412. if (ret < 0) {
  14413. ERROR_OUT(-7908, exit_rsa);
  14414. }
  14415. TEST_SLEEP();
  14416. #ifdef WC_RSA_BLINDING
  14417. {
  14418. int tmpret = ret;
  14419. ret = wc_RsaSetRNG(key, &rng);
  14420. if (ret < 0) {
  14421. ERROR_OUT(-7909, exit_rsa);
  14422. }
  14423. ret = tmpret;
  14424. }
  14425. #endif
  14426. idx = (word32)ret; /* save off encrypted length */
  14427. do {
  14428. #if defined(WOLFSSL_ASYNC_CRYPT)
  14429. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14430. #endif
  14431. if (ret >= 0) {
  14432. ret = wc_RsaPrivateDecrypt(out, idx, plain, plainSz, key);
  14433. }
  14434. } while (ret == WC_PENDING_E);
  14435. if (ret < 0) {
  14436. ERROR_OUT(-7910, exit_rsa);
  14437. }
  14438. if (XMEMCMP(plain, in, inLen)) {
  14439. ERROR_OUT(-7911, exit_rsa);
  14440. }
  14441. TEST_SLEEP();
  14442. do {
  14443. #if defined(WOLFSSL_ASYNC_CRYPT)
  14444. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14445. #endif
  14446. if (ret >= 0) {
  14447. ret = wc_RsaPrivateDecryptInline(out, idx, &res, key);
  14448. }
  14449. } while (ret == WC_PENDING_E);
  14450. if (ret < 0) {
  14451. ERROR_OUT(-7912, exit_rsa);
  14452. }
  14453. if (ret != (int)inLen) {
  14454. ERROR_OUT(-7913, exit_rsa);
  14455. }
  14456. if (XMEMCMP(res, in, inLen)) {
  14457. ERROR_OUT(-7914, exit_rsa);
  14458. }
  14459. TEST_SLEEP();
  14460. do {
  14461. #if defined(WOLFSSL_ASYNC_CRYPT)
  14462. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14463. #endif
  14464. if (ret >= 0) {
  14465. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, key, &rng);
  14466. }
  14467. } while (ret == WC_PENDING_E);
  14468. if (ret < 0) {
  14469. ERROR_OUT(-7915, exit_rsa);
  14470. }
  14471. TEST_SLEEP();
  14472. #elif defined(WOLFSSL_PUBLIC_MP)
  14473. {
  14474. static byte signature_2048[] = {
  14475. 0x07, 0x6f, 0xc9, 0x85, 0x73, 0x9e, 0x21, 0x79,
  14476. 0x47, 0xf1, 0xa3, 0xd7, 0xf4, 0x27, 0x29, 0xbe,
  14477. 0x99, 0x5d, 0xac, 0xb2, 0x10, 0x3f, 0x95, 0xda,
  14478. 0x89, 0x23, 0xb8, 0x96, 0x13, 0x57, 0x72, 0x30,
  14479. 0xa1, 0xfe, 0x5a, 0x68, 0x9c, 0x99, 0x9d, 0x1e,
  14480. 0x05, 0xa4, 0x80, 0xb0, 0xbb, 0xd9, 0xd9, 0xa1,
  14481. 0x69, 0x97, 0x74, 0xb3, 0x41, 0x21, 0x3b, 0x47,
  14482. 0xf5, 0x51, 0xb1, 0xfb, 0xc7, 0xaa, 0xcc, 0xdc,
  14483. 0xcd, 0x76, 0xa0, 0x28, 0x4d, 0x27, 0x14, 0xa4,
  14484. 0xb9, 0x41, 0x68, 0x7c, 0xb3, 0x66, 0xe6, 0x6f,
  14485. 0x40, 0x76, 0xe4, 0x12, 0xfd, 0xae, 0x29, 0xb5,
  14486. 0x63, 0x60, 0x87, 0xce, 0x49, 0x6b, 0xf3, 0x05,
  14487. 0x9a, 0x14, 0xb5, 0xcc, 0xcd, 0xf7, 0x30, 0x95,
  14488. 0xd2, 0x72, 0x52, 0x1d, 0x5b, 0x7e, 0xef, 0x4a,
  14489. 0x02, 0x96, 0x21, 0x6c, 0x55, 0xa5, 0x15, 0xb1,
  14490. 0x57, 0x63, 0x2c, 0xa3, 0x8e, 0x9d, 0x3d, 0x45,
  14491. 0xcc, 0xb8, 0xe6, 0xa1, 0xc8, 0x59, 0xcd, 0xf5,
  14492. 0xdc, 0x0a, 0x51, 0xb6, 0x9d, 0xfb, 0xf4, 0x6b,
  14493. 0xfd, 0x32, 0x71, 0x6e, 0xcf, 0xcb, 0xb3, 0xd9,
  14494. 0xe0, 0x4a, 0x77, 0x34, 0xd6, 0x61, 0xf5, 0x7c,
  14495. 0xf9, 0xa9, 0xa4, 0xb0, 0x8e, 0x3b, 0xd6, 0x04,
  14496. 0xe0, 0xde, 0x2b, 0x5b, 0x5a, 0xbf, 0xd9, 0xef,
  14497. 0x8d, 0xa3, 0xf5, 0xb1, 0x67, 0xf3, 0xb9, 0x72,
  14498. 0x0a, 0x37, 0x12, 0x35, 0x6c, 0x8e, 0x10, 0x8b,
  14499. 0x38, 0x06, 0x16, 0x4b, 0x20, 0x20, 0x13, 0x00,
  14500. 0x2e, 0x6d, 0xc2, 0x59, 0x23, 0x67, 0x4a, 0x6d,
  14501. 0xa1, 0x46, 0x8b, 0xee, 0xcf, 0x44, 0xb4, 0x3e,
  14502. 0x56, 0x75, 0x00, 0x68, 0xb5, 0x7d, 0x0f, 0x20,
  14503. 0x79, 0x5d, 0x7f, 0x12, 0x15, 0x32, 0x89, 0x61,
  14504. 0x6b, 0x29, 0xb7, 0x52, 0xf5, 0x25, 0xd8, 0x98,
  14505. 0xe8, 0x6f, 0xf9, 0x22, 0xb4, 0xbb, 0xe5, 0xff,
  14506. 0xd0, 0x92, 0x86, 0x9a, 0x88, 0xa2, 0xaf, 0x6b
  14507. };
  14508. ret = sizeof(signature_2048);
  14509. XMEMCPY(out, signature_2048, ret);
  14510. }
  14511. #endif
  14512. #if !defined(WC_NO_RNG) && !defined(WC_NO_RSA_OAEP) && \
  14513. ((!defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || \
  14514. defined(WOLFSSL_PUBLIC_MP)) && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14515. idx = (word32)ret;
  14516. XMEMSET(plain, 0, plainSz);
  14517. do {
  14518. #if defined(WOLFSSL_ASYNC_CRYPT)
  14519. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14520. #endif
  14521. if (ret >= 0) {
  14522. #ifndef WOLFSSL_RSA_VERIFY_INLINE
  14523. #if defined(WOLFSSL_CRYPTOCELL)
  14524. /*
  14525. Cryptocell requires the input data and signature byte array to verify.
  14526. first argument must be the input data
  14527. second argument must be the length of input data
  14528. third argument must be the signature byte array or the output from
  14529. wc_RsaSSL_Sign()
  14530. fourth argument must be the length of the signature byte array
  14531. */
  14532. ret = wc_RsaSSL_Verify(in, inLen, out, outSz, key);
  14533. #else
  14534. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, key);
  14535. #endif /* WOLFSSL_CRYPTOCELL */
  14536. #else
  14537. byte* dec = NULL;
  14538. ret = wc_RsaSSL_VerifyInline(out, idx, &dec, key);
  14539. if (ret > 0) {
  14540. XMEMCPY(plain, dec, ret);
  14541. }
  14542. #endif
  14543. }
  14544. } while (ret == WC_PENDING_E);
  14545. if (ret < 0) {
  14546. ERROR_OUT(-7916, exit_rsa);
  14547. }
  14548. if (XMEMCMP(plain, in, (size_t)ret)) {
  14549. ERROR_OUT(-7917, exit_rsa);
  14550. }
  14551. TEST_SLEEP();
  14552. #endif
  14553. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  14554. #if !defined(WC_NO_RSA_OAEP) && !defined(WC_NO_RNG)
  14555. #if !defined(HAVE_FAST_RSA) && !defined(HAVE_USER_RSA) && \
  14556. (!defined(HAVE_FIPS) || \
  14557. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) \
  14558. && !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14559. ret = rsa_oaep_padding_test(key, &rng);
  14560. if (ret != 0)
  14561. return ret;
  14562. #endif /* !HAVE_FAST_RSA && !HAVE_FIPS */
  14563. #endif /* WC_NO_RSA_OAEP && !WC_NO_RNG */
  14564. #endif /* WOLFSSL_RSA_VERIFY_ONLY */
  14565. #if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(NO_ASN) \
  14566. && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  14567. ret = rsa_export_key_test(key);
  14568. if (ret != 0)
  14569. return ret;
  14570. #endif
  14571. #if !defined(NO_ASN) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  14572. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  14573. ret = rsa_flatten_test(key);
  14574. if (ret != 0)
  14575. return ret;
  14576. #endif
  14577. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_ASN) && \
  14578. !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  14579. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  14580. (void)clientCert;
  14581. #endif
  14582. #ifdef WOLFSSL_TEST_CERT
  14583. #if defined(WOLFSSL_MDK_ARM)
  14584. #define sizeof(s) XSTRLEN((char *)(s))
  14585. #endif
  14586. #ifdef USE_CERT_BUFFERS_1024
  14587. XMEMCPY(tmp, client_cert_der_1024, (size_t)sizeof_client_cert_der_1024);
  14588. bytes = (size_t)sizeof_client_cert_der_1024;
  14589. #elif defined(USE_CERT_BUFFERS_2048)
  14590. XMEMCPY(tmp, client_cert_der_2048, (size_t)sizeof_client_cert_der_2048);
  14591. bytes = (size_t)sizeof_client_cert_der_2048;
  14592. #elif defined(USE_CERT_BUFFERS_3072)
  14593. XMEMCPY(tmp, client_cert_der_3072, (size_t)sizeof_client_cert_der_3072);
  14594. bytes = (size_t)sizeof_client_cert_der_3072;
  14595. #elif defined(USE_CERT_BUFFERS_4096)
  14596. XMEMCPY(tmp, client_cert_der_4096, (size_t)sizeof_client_cert_der_4096);
  14597. bytes = (size_t)sizeof_client_cert_der_4096;
  14598. #elif !defined(NO_FILESYSTEM)
  14599. file2 = XFOPEN(clientCert, "rb");
  14600. if (!file2) {
  14601. ERROR_OUT(-7940, exit_rsa);
  14602. }
  14603. bytes = XFREAD(tmp, 1, FOURK_BUF, file2);
  14604. XFCLOSE(file2);
  14605. #else
  14606. /* No certificate to use. */
  14607. ERROR_OUT(-7941, exit_rsa);
  14608. #endif
  14609. #ifdef sizeof
  14610. #undef sizeof
  14611. #endif
  14612. InitDecodedCert(cert, tmp, (word32)bytes, NULL);
  14613. ret = ParseCert(cert, CERT_TYPE, NO_VERIFY, NULL);
  14614. if (ret != 0) {
  14615. FreeDecodedCert(cert);
  14616. ERROR_OUT(-7942, exit_rsa);
  14617. }
  14618. #ifndef NO_ASN_TIME
  14619. ret = wc_GetDateInfo(cert->afterDate, cert->afterDateLen, &date,
  14620. &dateFormat, &dateLength);
  14621. if (ret != 0) {
  14622. FreeDecodedCert(cert);
  14623. ERROR_OUT(-7943, exit_rsa);
  14624. }
  14625. ret = wc_GetDateAsCalendarTime(date, dateLength, dateFormat, &timearg);
  14626. if (ret != 0) {
  14627. FreeDecodedCert(cert);
  14628. ERROR_OUT(-7944, exit_rsa);
  14629. }
  14630. #endif
  14631. FreeDecodedCert(cert);
  14632. #endif /* WOLFSSL_TEST_CERT */
  14633. #ifdef WOLFSSL_CERT_EXT
  14634. #ifdef USE_CERT_BUFFERS_1024
  14635. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  14636. bytes = sizeof_client_keypub_der_1024;
  14637. #elif defined(USE_CERT_BUFFERS_2048)
  14638. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  14639. bytes = sizeof_client_keypub_der_2048;
  14640. #elif defined(USE_CERT_BUFFERS_3072)
  14641. XMEMCPY(tmp, client_keypub_der_3072, sizeof_client_keypub_der_3072);
  14642. bytes = sizeof_client_keypub_der_3072;
  14643. #elif defined(USE_CERT_BUFFERS_4096)
  14644. XMEMCPY(tmp, client_keypub_der_4096, sizeof_client_keypub_der_4096);
  14645. bytes = sizeof_client_keypub_der_4096;
  14646. #else
  14647. file = XFOPEN(clientKeyPub, "rb");
  14648. if (!file) {
  14649. err_sys("can't open ./certs/client-keyPub.der, "
  14650. "Please run from wolfSSL home dir", -40);
  14651. ERROR_OUT(-7945, exit_rsa);
  14652. }
  14653. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  14654. XFCLOSE(file);
  14655. #endif /* USE_CERT_BUFFERS */
  14656. ret = wc_InitRsaKey(keypub, HEAP_HINT);
  14657. if (ret != 0) {
  14658. ERROR_OUT(-7946, exit_rsa);
  14659. }
  14660. idx = 0;
  14661. ret = wc_RsaPublicKeyDecode(tmp, &idx, keypub, (word32)bytes);
  14662. if (ret != 0) {
  14663. ERROR_OUT(-7947, exit_rsa);
  14664. }
  14665. #endif /* WOLFSSL_CERT_EXT */
  14666. #ifdef WOLFSSL_KEY_GEN
  14667. ret = rsa_keygen_test(&rng);
  14668. if (ret != 0)
  14669. goto exit_rsa;
  14670. #endif
  14671. #ifdef WOLFSSL_CERT_GEN
  14672. /* Make Cert / Sign example for RSA cert and RSA CA */
  14673. ret = rsa_certgen_test(key, keypub, &rng, tmp);
  14674. if (ret != 0)
  14675. goto exit_rsa;
  14676. #if !defined(NO_RSA) && defined(HAVE_ECC)
  14677. ret = rsa_ecc_certgen_test(&rng, tmp);
  14678. if (ret != 0)
  14679. goto exit_rsa;
  14680. #endif
  14681. #if defined(WOLFSSL_CERT_REQ) && !defined(WOLFSSL_NO_MALLOC)
  14682. {
  14683. Cert *req;
  14684. int derSz;
  14685. #ifndef WOLFSSL_SMALL_STACK
  14686. byte* der = NULL;
  14687. #endif
  14688. req = (Cert *)XMALLOC(sizeof *req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14689. if (! req)
  14690. ERROR_OUT(MEMORY_E, exit_rsa);
  14691. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,DYNAMIC_TYPE_TMP_BUFFER);
  14692. if (der == NULL) {
  14693. ERROR_OUT(-7964, exit_rsa);
  14694. }
  14695. if (wc_InitCert_ex(req, HEAP_HINT, devId)) {
  14696. ERROR_OUT(-7965, exit_rsa);
  14697. }
  14698. req->version = 0;
  14699. req->isCA = 1;
  14700. XSTRNCPY(req->challengePw, "wolf123", CTC_NAME_SIZE);
  14701. XMEMCPY(&req->subject, &certDefaultName, sizeof(CertName));
  14702. #ifndef NO_SHA256
  14703. req->sigType = CTC_SHA256wRSA;
  14704. #else
  14705. req->sigType = CTC_SHAwRSA;
  14706. #endif
  14707. #ifdef WOLFSSL_CERT_EXT
  14708. /* add SKID from the Public Key */
  14709. if (wc_SetSubjectKeyIdFromPublicKey(req, keypub, NULL) != 0) {
  14710. ERROR_OUT(-7966, exit_rsa);
  14711. }
  14712. /* add Key Usage */
  14713. if (wc_SetKeyUsage(req, certKeyUsage2) != 0) {
  14714. ERROR_OUT(-7967, exit_rsa);
  14715. }
  14716. /* add Extended Key Usage */
  14717. if (wc_SetExtKeyUsage(req, "serverAuth,clientAuth,codeSigning,"
  14718. "emailProtection,timeStamping,OCSPSigning") != 0) {
  14719. ERROR_OUT(-7968, exit_rsa);
  14720. }
  14721. #ifdef WOLFSSL_EKU_OID
  14722. {
  14723. WOLFSSL_SMALL_STACK_STATIC const char unique[] = "2.16.840.1.111111.100.1.10.1";
  14724. if (wc_SetExtKeyUsageOID(req, unique, sizeof(unique), 0,
  14725. HEAP_HINT) != 0) {
  14726. ERROR_OUT(-7969, exit_rsa);
  14727. }
  14728. }
  14729. #endif /* WOLFSSL_EKU_OID */
  14730. #endif /* WOLFSSL_CERT_EXT */
  14731. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  14732. if (derSz < 0) {
  14733. ERROR_OUT(-7970, exit_rsa);
  14734. }
  14735. #ifdef WOLFSSL_CERT_EXT
  14736. /* Try again with "any" flag set, will override all others */
  14737. if (wc_SetExtKeyUsage(req, "any") != 0) {
  14738. ERROR_OUT(-7971, exit_rsa);
  14739. }
  14740. derSz = wc_MakeCertReq(req, der, FOURK_BUF, key, NULL);
  14741. if (derSz < 0) {
  14742. ERROR_OUT(-7972, exit_rsa);
  14743. }
  14744. #endif /* WOLFSSL_CERT_EXT */
  14745. ret = 0;
  14746. do {
  14747. #if defined(WOLFSSL_ASYNC_CRYPT)
  14748. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  14749. #endif
  14750. if (ret >= 0) {
  14751. ret = wc_SignCert(req->bodySz, req->sigType, der, FOURK_BUF,
  14752. key, NULL, &rng);
  14753. }
  14754. } while (ret == WC_PENDING_E);
  14755. if (ret < 0) {
  14756. ERROR_OUT(-7973, exit_rsa);
  14757. }
  14758. derSz = ret;
  14759. ret = SaveDerAndPem(der, derSz, certReqDerFile, certReqPemFile,
  14760. CERTREQ_TYPE, -5650);
  14761. if (ret != 0) {
  14762. goto exit_rsa;
  14763. }
  14764. derSz = wc_MakeCertReq_ex(req, der, FOURK_BUF, RSA_TYPE, key);
  14765. if (derSz < 0) {
  14766. ERROR_OUT(-7974, exit_rsa);
  14767. }
  14768. /* Test getting the size of the buffer without providing the buffer.
  14769. * derSz is set to the "largest buffer" we are willing to allocate. */
  14770. derSz = wc_MakeCertReq(req, NULL, 10000, key, NULL);
  14771. if (derSz < 0) {
  14772. ERROR_OUT(-7975, exit_rsa);
  14773. }
  14774. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14775. XFREE(req, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14776. der = NULL;
  14777. }
  14778. #endif /* WOLFSSL_CERT_REQ */
  14779. #endif /* WOLFSSL_CERT_GEN */
  14780. #if defined(WC_RSA_PSS) && !defined(HAVE_FIPS_VERSION) /* not supported with FIPSv1 */
  14781. /* Need to create known good signatures to test with this. */
  14782. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \
  14783. !defined(WOLF_CRYPTO_CB_ONLY_RSA)
  14784. ret = rsa_pss_test(&rng, key);
  14785. if (ret != 0)
  14786. goto exit_rsa;
  14787. #endif
  14788. #endif
  14789. #if defined(WOLFSSL_HAVE_SP_RSA) && defined(USE_FAST_MATH)
  14790. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14791. /* New key to be loaded in rsa_even_mod_test(). */
  14792. if (key != NULL)
  14793. #endif
  14794. wc_FreeRsaKey(key);
  14795. /* New key to be loaded in rsa_even_mod_test(). */
  14796. ret = rsa_even_mod_test(&rng, key);
  14797. #endif
  14798. exit_rsa:
  14799. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14800. if (key != NULL) {
  14801. wc_FreeRsaKey(key);
  14802. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14803. }
  14804. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  14805. if (keypub != NULL) {
  14806. wc_FreeRsaKey(keypub);
  14807. XFREE(keypub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14808. }
  14809. #endif
  14810. #ifdef WOLFSSL_TEST_CERT
  14811. if (cert != NULL)
  14812. XFREE(cert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14813. #endif
  14814. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14815. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14816. #else
  14817. wc_FreeRsaKey(key);
  14818. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_CERT_GEN)
  14819. wc_FreeRsaKey(keypub);
  14820. #endif
  14821. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  14822. wc_FreeRng(&rng);
  14823. WC_FREE_VAR(in, HEAP_HINT);
  14824. WC_FREE_VAR(out, HEAP_HINT);
  14825. WC_FREE_VAR(plain, HEAP_HINT);
  14826. (void)res;
  14827. (void)bytes;
  14828. (void)idx;
  14829. (void)in;
  14830. (void)out;
  14831. (void)plain;
  14832. (void)idx;
  14833. (void)inStr;
  14834. (void)inLen;
  14835. (void)outSz;
  14836. (void)plainSz;
  14837. /* ret can be greater then 0 with certgen but all negative values should
  14838. * be returned and treated as an error */
  14839. if (ret >= 0) {
  14840. return 0;
  14841. }
  14842. else {
  14843. return ret;
  14844. }
  14845. }
  14846. #endif /* !NO_RSA */
  14847. #ifndef NO_DH
  14848. static int dh_fips_generate_test(WC_RNG *rng)
  14849. {
  14850. int ret = 0;
  14851. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14852. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  14853. #else
  14854. DhKey key[1];
  14855. #endif
  14856. WOLFSSL_SMALL_STACK_STATIC const byte p[] = {
  14857. 0xc5, 0x7c, 0xa2, 0x4f, 0x4b, 0xd6, 0x8c, 0x3c,
  14858. 0xda, 0xc7, 0xba, 0xaa, 0xea, 0x2e, 0x5c, 0x1e,
  14859. 0x18, 0xb2, 0x7b, 0x8c, 0x55, 0x65, 0x9f, 0xea,
  14860. 0xe0, 0xa1, 0x36, 0x53, 0x2b, 0x36, 0xe0, 0x4e,
  14861. 0x3e, 0x64, 0xa9, 0xe4, 0xfc, 0x8f, 0x32, 0x62,
  14862. 0x97, 0xe4, 0xbe, 0xf7, 0xc1, 0xde, 0x07, 0x5a,
  14863. 0x89, 0x28, 0xf3, 0xfe, 0x4f, 0xfe, 0x68, 0xbc,
  14864. 0xfb, 0x0a, 0x7c, 0xa4, 0xb3, 0x14, 0x48, 0x89,
  14865. 0x9f, 0xaf, 0xb8, 0x43, 0xe2, 0xa0, 0x62, 0x5c,
  14866. 0xb4, 0x88, 0x3f, 0x06, 0x50, 0x11, 0xfe, 0x65,
  14867. 0x8d, 0x49, 0xd2, 0xf5, 0x4b, 0x74, 0x79, 0xdb,
  14868. 0x06, 0x62, 0x92, 0x89, 0xed, 0xda, 0xcb, 0x87,
  14869. 0x37, 0x16, 0xd2, 0xa1, 0x7a, 0xe8, 0xde, 0x92,
  14870. 0xee, 0x3e, 0x41, 0x4a, 0x91, 0x5e, 0xed, 0xf3,
  14871. 0x6c, 0x6b, 0x7e, 0xfd, 0x15, 0x92, 0x18, 0xfc,
  14872. 0xa7, 0xac, 0x42, 0x85, 0x57, 0xe9, 0xdc, 0xda,
  14873. 0x55, 0xc9, 0x8b, 0x28, 0x9e, 0xc1, 0xc4, 0x46,
  14874. 0x4d, 0x88, 0xed, 0x62, 0x8e, 0xdb, 0x3f, 0xb9,
  14875. 0xd7, 0xc8, 0xe3, 0xcf, 0xb8, 0x34, 0x2c, 0xd2,
  14876. 0x6f, 0x28, 0x06, 0x41, 0xe3, 0x66, 0x8c, 0xfc,
  14877. 0x72, 0xff, 0x26, 0x3b, 0x6b, 0x6c, 0x6f, 0x73,
  14878. 0xde, 0xf2, 0x90, 0x29, 0xe0, 0x61, 0x32, 0xc4,
  14879. 0x12, 0x74, 0x09, 0x52, 0xec, 0xf3, 0x1b, 0xa6,
  14880. 0x45, 0x98, 0xac, 0xf9, 0x1c, 0x65, 0x8e, 0x3a,
  14881. 0x91, 0x84, 0x4b, 0x23, 0x8a, 0xb2, 0x3c, 0xc9,
  14882. 0xfa, 0xea, 0xf1, 0x38, 0xce, 0xd8, 0x05, 0xe0,
  14883. 0xfa, 0x44, 0x68, 0x1f, 0xeb, 0xd9, 0x57, 0xb8,
  14884. 0x4a, 0x97, 0x5b, 0x88, 0xc5, 0xf1, 0xbb, 0xb0,
  14885. 0x49, 0xc3, 0x91, 0x7c, 0xd3, 0x13, 0xb9, 0x47,
  14886. 0xbb, 0x91, 0x8f, 0xe5, 0x26, 0x07, 0xab, 0xa9,
  14887. 0xc5, 0xd0, 0x3d, 0x95, 0x41, 0x26, 0x92, 0x9d,
  14888. 0x13, 0x67, 0xf2, 0x7e, 0x11, 0x88, 0xdc, 0x2d
  14889. };
  14890. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  14891. 0x4a, 0x1a, 0xf3, 0xa4, 0x92, 0xe9, 0xee, 0x74,
  14892. 0x6e, 0x57, 0xd5, 0x8c, 0x2c, 0x5b, 0x41, 0x41,
  14893. 0x5e, 0xd4, 0x55, 0x19, 0xdc, 0xd9, 0x32, 0x91,
  14894. 0xf7, 0xfd, 0xc2, 0x57, 0xff, 0x03, 0x14, 0xdb,
  14895. 0xf1, 0xb7, 0x60, 0x0c, 0x43, 0x59, 0x3f, 0xff,
  14896. 0xac, 0xf1, 0x80, 0x9a, 0x15, 0x6f, 0xd8, 0x6e,
  14897. 0xb7, 0x85, 0x18, 0xc8, 0xec, 0x4e, 0x59, 0x4a,
  14898. 0xe2, 0x91, 0x43, 0x4c, 0xeb, 0x95, 0xb6, 0x2e,
  14899. 0x9a, 0xea, 0x53, 0x68, 0x80, 0x64, 0x69, 0x40,
  14900. 0xf9, 0xec, 0xbd, 0x85, 0x89, 0x26, 0x97, 0x67,
  14901. 0xaf, 0xb0, 0xad, 0x00, 0x1b, 0xd4, 0xfd, 0x94,
  14902. 0xd3, 0xe9, 0x92, 0xb1, 0xb4, 0xbc, 0x5a, 0xaa,
  14903. 0x92, 0x80, 0x89, 0x3b, 0x39, 0x05, 0x6c, 0x22,
  14904. 0x26, 0xfe, 0x5a, 0x28, 0x6c, 0x37, 0x50, 0x5a,
  14905. 0x38, 0x99, 0xcf, 0xf3, 0xc1, 0x96, 0x45, 0xdc,
  14906. 0x01, 0xcb, 0x20, 0x87, 0xa5, 0x00, 0x8c, 0xf5,
  14907. 0x4d, 0xc2, 0xef, 0xb8, 0x9b, 0xd1, 0x87, 0xbe,
  14908. 0xed, 0xd5, 0x0a, 0x29, 0x15, 0x34, 0x59, 0x4c,
  14909. 0x3a, 0x05, 0x22, 0x05, 0x44, 0x4f, 0x9f, 0xc8,
  14910. 0x47, 0x12, 0x24, 0x8e, 0xa8, 0x79, 0xe4, 0x67,
  14911. 0xba, 0x4d, 0x5b, 0x75, 0x56, 0x95, 0xeb, 0xe8,
  14912. 0x8a, 0xfa, 0x8e, 0x01, 0x8c, 0x1b, 0x74, 0x63,
  14913. 0xd9, 0x2f, 0xf7, 0xd3, 0x44, 0x8f, 0xa8, 0xf5,
  14914. 0xaf, 0x6c, 0x4f, 0xdb, 0xe7, 0xc9, 0x6c, 0x71,
  14915. 0x22, 0xa3, 0x1d, 0xf1, 0x40, 0xb2, 0xe0, 0x9a,
  14916. 0xb6, 0x72, 0xc9, 0xc0, 0x13, 0x16, 0xa2, 0x4a,
  14917. 0xe1, 0x92, 0xc7, 0x54, 0x23, 0xab, 0x9d, 0xa1,
  14918. 0xa1, 0xe5, 0x0b, 0xed, 0xba, 0xe8, 0x84, 0x37,
  14919. 0xb2, 0xe7, 0xfe, 0x32, 0x8d, 0xfa, 0x1c, 0x53,
  14920. 0x77, 0x97, 0xc7, 0xf3, 0x48, 0xc9, 0xdb, 0x2d,
  14921. 0x75, 0x52, 0x9d, 0x42, 0x51, 0x78, 0x62, 0x68,
  14922. 0x05, 0x45, 0x15, 0xf8, 0xa2, 0x4e, 0xf3, 0x0b
  14923. };
  14924. WOLFSSL_SMALL_STACK_STATIC const byte q[] = {
  14925. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  14926. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  14927. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  14928. 0x40, 0x52, 0xed, 0x41
  14929. };
  14930. WOLFSSL_SMALL_STACK_STATIC const byte q0[] = {
  14931. 0x00,
  14932. 0xe0, 0x35, 0x37, 0xaf, 0xb2, 0x50, 0x91, 0x8e,
  14933. 0xf2, 0x62, 0x2b, 0xd9, 0x9f, 0x6c, 0x11, 0x75,
  14934. 0xec, 0x24, 0x5d, 0x78, 0x59, 0xe7, 0x8d, 0xb5,
  14935. 0x40, 0x52, 0xed, 0x41
  14936. };
  14937. byte priv[256];
  14938. byte pub[256];
  14939. word32 privSz = sizeof(priv);
  14940. word32 pubSz = sizeof(pub);
  14941. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  14942. if (key == NULL)
  14943. ERROR_OUT(MEMORY_E, exit_gen_test);
  14944. #endif
  14945. /* Parameter Validation testing. */
  14946. ret = wc_DhGenerateKeyPair(NULL, rng, priv, &privSz, pub, &pubSz);
  14947. if (ret != BAD_FUNC_ARG)
  14948. ERROR_OUT(-7980, exit_gen_test);
  14949. ret = wc_DhGenerateKeyPair(key, NULL, priv, &privSz, pub, &pubSz);
  14950. if (ret != BAD_FUNC_ARG)
  14951. ERROR_OUT(-7981, exit_gen_test);
  14952. ret = wc_DhGenerateKeyPair(key, rng, NULL, &privSz, pub, &pubSz);
  14953. if (ret != BAD_FUNC_ARG)
  14954. ERROR_OUT(-7982, exit_gen_test);
  14955. ret = wc_DhGenerateKeyPair(key, rng, priv, NULL, pub, &pubSz);
  14956. if (ret != BAD_FUNC_ARG)
  14957. ERROR_OUT(-7983, exit_gen_test);
  14958. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, NULL, &pubSz);
  14959. if (ret != BAD_FUNC_ARG)
  14960. ERROR_OUT(-7984, exit_gen_test);
  14961. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, NULL);
  14962. if (ret != BAD_FUNC_ARG)
  14963. ERROR_OUT(-7985, exit_gen_test);
  14964. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  14965. if (ret != 0)
  14966. ERROR_OUT(-7986, exit_gen_test);
  14967. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q0, sizeof(q0));
  14968. if (ret != 0) {
  14969. ERROR_OUT(-7987, exit_gen_test);
  14970. }
  14971. wc_FreeDhKey(key);
  14972. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  14973. if (ret != 0)
  14974. ERROR_OUT(-7988, exit_gen_test);
  14975. ret = wc_DhSetKey_ex(key, p, sizeof(p), g, sizeof(g), q, sizeof(q));
  14976. if (ret != 0) {
  14977. ERROR_OUT(-7989, exit_gen_test);
  14978. }
  14979. /* Use API. */
  14980. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  14981. #if defined(WOLFSSL_ASYNC_CRYPT)
  14982. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  14983. #endif
  14984. if (ret != 0) {
  14985. ERROR_OUT(-7990, exit_gen_test);
  14986. }
  14987. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q0, sizeof(q0));
  14988. if (ret != 0) {
  14989. ERROR_OUT(-7991, exit_gen_test);
  14990. }
  14991. wc_FreeDhKey(key);
  14992. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  14993. if (ret != 0)
  14994. ERROR_OUT(-7992, exit_gen_test);
  14995. ret = wc_DhSetKey(key, p, sizeof(p), g, sizeof(g));
  14996. if (ret != 0) {
  14997. ERROR_OUT(-7993, exit_gen_test);
  14998. }
  14999. ret = wc_DhCheckPubKey_ex(key, pub, pubSz, q, sizeof(q));
  15000. if (ret != 0) {
  15001. ERROR_OUT(-7994, exit_gen_test);
  15002. }
  15003. #ifndef HAVE_SELFTEST
  15004. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  15005. if (ret != 0) {
  15006. ERROR_OUT(-7995, exit_gen_test);
  15007. }
  15008. /* Taint the public key so the check fails. */
  15009. pub[0]++;
  15010. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  15011. if (ret != MP_CMP_E) {
  15012. ERROR_OUT(-7996, exit_gen_test);
  15013. }
  15014. #ifdef WOLFSSL_KEY_GEN
  15015. wc_FreeDhKey(key);
  15016. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15017. if (ret != 0)
  15018. ERROR_OUT(-7997, exit_gen_test);
  15019. ret = wc_DhGenerateParams(rng, 2048, key);
  15020. if (ret != 0) {
  15021. ERROR_OUT(-7998, exit_gen_test);
  15022. }
  15023. privSz = sizeof(priv);
  15024. pubSz = sizeof(pub);
  15025. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  15026. #if defined(WOLFSSL_ASYNC_CRYPT)
  15027. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15028. #endif
  15029. if (ret != 0) {
  15030. ERROR_OUT(-7999, exit_gen_test);
  15031. }
  15032. #endif /* WOLFSSL_KEY_GEN */
  15033. #endif /* HAVE_SELFTEST */
  15034. ret = 0;
  15035. exit_gen_test:
  15036. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15037. if (key) {
  15038. wc_FreeDhKey(key);
  15039. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15040. }
  15041. #else
  15042. wc_FreeDhKey(key);
  15043. #endif
  15044. return ret;
  15045. }
  15046. static int dh_generate_test(WC_RNG *rng)
  15047. {
  15048. int ret = 0;
  15049. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15050. DhKey *smallKey = NULL;
  15051. #else
  15052. DhKey smallKey[1];
  15053. #endif
  15054. byte p[2] = { 1, 7 }; /* 263 in decimal */
  15055. byte g[2] = { 0, 2 };
  15056. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FFDHE)
  15057. #ifdef WOLFSSL_DH_CONST
  15058. /* the table for constant DH lookup will round to the lowest byte size 21 */
  15059. byte priv[21];
  15060. byte pub[21];
  15061. #else
  15062. byte priv[2];
  15063. byte pub[2];
  15064. #endif
  15065. word32 privSz = sizeof(priv);
  15066. word32 pubSz = sizeof(pub);
  15067. #endif
  15068. int smallKey_inited = 0;
  15069. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15070. if ((smallKey = (DhKey *)XMALLOC(sizeof(*smallKey), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  15071. return -8019;
  15072. #endif
  15073. ret = wc_InitDhKey_ex(smallKey, HEAP_HINT, devId);
  15074. if (ret != 0)
  15075. ERROR_OUT(-8010, exit_gen_test);
  15076. smallKey_inited = 1;
  15077. /* Parameter Validation testing. */
  15078. ret = wc_InitDhKey_ex(NULL, HEAP_HINT, devId);
  15079. if (ret != BAD_FUNC_ARG)
  15080. return -8011;
  15081. wc_FreeDhKey(NULL);
  15082. ret = wc_DhSetKey(NULL, p, sizeof(p), g, sizeof(g));
  15083. if (ret != BAD_FUNC_ARG) {
  15084. ERROR_OUT(-8012, exit_gen_test);
  15085. }
  15086. ret = wc_DhSetKey(smallKey, NULL, sizeof(p), g, sizeof(g));
  15087. if (ret != BAD_FUNC_ARG) {
  15088. ERROR_OUT(-8013, exit_gen_test);
  15089. }
  15090. ret = wc_DhSetKey(smallKey, p, 0, g, sizeof(g));
  15091. if (ret != BAD_FUNC_ARG) {
  15092. ERROR_OUT(-8014, exit_gen_test);
  15093. }
  15094. ret = wc_DhSetKey(smallKey, p, sizeof(p), NULL, sizeof(g));
  15095. if (ret != BAD_FUNC_ARG) {
  15096. ERROR_OUT(-8015, exit_gen_test);
  15097. }
  15098. ret = wc_DhSetKey(smallKey, p, sizeof(p), g, 0);
  15099. if (ret != BAD_FUNC_ARG) {
  15100. ERROR_OUT(-8016, exit_gen_test);
  15101. }
  15102. ret = wc_DhSetKey(smallKey, p, sizeof(p), g, sizeof(g));
  15103. if (ret != 0) {
  15104. ERROR_OUT(-8017, exit_gen_test);
  15105. }
  15106. #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FFDHE)
  15107. /* Use API. */
  15108. ret = wc_DhGenerateKeyPair(smallKey, rng, priv, &privSz, pub, &pubSz);
  15109. #if defined(WOLFSSL_ASYNC_CRYPT)
  15110. ret = wc_AsyncWait(ret, &smallKey->asyncDev, WC_ASYNC_FLAG_NONE);
  15111. #endif
  15112. if (ret != 0) {
  15113. ret = -8018;
  15114. }
  15115. #else
  15116. (void)rng;
  15117. #if defined(HAVE_FIPS) || !defined(WOLFSSL_NO_DH186)
  15118. ret = 0;
  15119. #endif
  15120. #endif
  15121. #if !defined(HAVE_FIPS) && defined(WOLFSSL_NO_DH186)
  15122. {
  15123. byte priv[260];
  15124. byte pub[260];
  15125. word32 privSz = sizeof(priv);
  15126. word32 pubSz = sizeof(pub);
  15127. /* test odd ball param generation with DH */
  15128. wc_FreeDhKey(smallKey);
  15129. ret = wc_InitDhKey_ex(smallKey, HEAP_HINT, devId);
  15130. if (ret != 0)
  15131. ERROR_OUT(-8019, exit_gen_test);
  15132. ret = wc_DhGenerateParams(rng, 2056, smallKey);
  15133. if (ret != 0) {
  15134. ERROR_OUT(-8020, exit_gen_test);
  15135. }
  15136. privSz = sizeof(priv);
  15137. pubSz = sizeof(pub);
  15138. ret = wc_DhGenerateKeyPair(smallKey, rng, priv, &privSz, pub, &pubSz);
  15139. #if defined(WOLFSSL_ASYNC_CRYPT)
  15140. ret = wc_AsyncWait(ret, &smallKey->asyncDev, WC_ASYNC_FLAG_NONE);
  15141. #endif
  15142. if (ret != 0) {
  15143. ERROR_OUT(-8021, exit_gen_test);
  15144. }
  15145. }
  15146. #endif /* !HAVE_FIPS and WOLFSSL_NO_DH186 */
  15147. exit_gen_test:
  15148. if (smallKey_inited)
  15149. wc_FreeDhKey(smallKey);
  15150. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15151. if (smallKey != NULL)
  15152. XFREE(smallKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15153. #endif
  15154. return ret;
  15155. }
  15156. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  15157. typedef struct dh_pubvalue_test {
  15158. const byte* data;
  15159. word32 len;
  15160. } dh_pubvalue_test;
  15161. static int dh_test_check_pubvalue(void)
  15162. {
  15163. int ret;
  15164. word32 i;
  15165. WOLFSSL_SMALL_STACK_STATIC const byte prime[] = {0x01, 0x00, 0x01};
  15166. WOLFSSL_SMALL_STACK_STATIC const byte pubValZero[] = { 0x00 };
  15167. WOLFSSL_SMALL_STACK_STATIC const byte pubValZeroLong[] = { 0x00, 0x00, 0x00 };
  15168. WOLFSSL_SMALL_STACK_STATIC const byte pubValOne[] = { 0x01 };
  15169. WOLFSSL_SMALL_STACK_STATIC const byte pubValOneLong[] = { 0x00, 0x00, 0x01 };
  15170. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeMinusOne[] = { 0x01, 0x00, 0x00 };
  15171. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimeLong[] = {0x00, 0x01, 0x00, 0x01};
  15172. WOLFSSL_SMALL_STACK_STATIC const byte pubValPrimePlusOne[] = { 0x01, 0x00, 0x02 };
  15173. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig0[] = { 0x02, 0x00, 0x01 };
  15174. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig1[] = { 0x01, 0x01, 0x01 };
  15175. WOLFSSL_SMALL_STACK_STATIC const byte pubValTooLong[] = { 0x01, 0x00, 0x00, 0x01 };
  15176. const dh_pubvalue_test dh_pubval_fail[] = {
  15177. { prime, sizeof(prime) },
  15178. { pubValZero, sizeof(pubValZero) },
  15179. { pubValZeroLong, sizeof(pubValZeroLong) },
  15180. { pubValOne, sizeof(pubValOne) },
  15181. { pubValOneLong, sizeof(pubValOneLong) },
  15182. { pubValPrimeMinusOne, sizeof(pubValPrimeMinusOne) },
  15183. { pubValPrimeLong, sizeof(pubValPrimeLong) },
  15184. { pubValPrimePlusOne, sizeof(pubValPrimePlusOne) },
  15185. { pubValTooBig0, sizeof(pubValTooBig0) },
  15186. { pubValTooBig1, sizeof(pubValTooBig1) },
  15187. { pubValTooLong, sizeof(pubValTooLong) },
  15188. };
  15189. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwo[] = { 0x02 };
  15190. WOLFSSL_SMALL_STACK_STATIC const byte pubValTwoLong[] = { 0x00, 0x00, 0x02 };
  15191. WOLFSSL_SMALL_STACK_STATIC const byte pubValGood[] = { 0x12, 0x34 };
  15192. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLen[] = { 0x00, 0x12, 0x34 };
  15193. WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLong[] = { 0x00, 0x00, 0x12, 0x34 };
  15194. const dh_pubvalue_test dh_pubval_pass[] = {
  15195. { pubValTwo, sizeof(pubValTwo) },
  15196. { pubValTwoLong, sizeof(pubValTwoLong) },
  15197. { pubValGood, sizeof(pubValGood) },
  15198. { pubValGoodLen, sizeof(pubValGoodLen) },
  15199. { pubValGoodLong, sizeof(pubValGoodLong) },
  15200. };
  15201. for (i = 0; i < sizeof(dh_pubval_fail) / sizeof(*dh_pubval_fail); i++) {
  15202. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_fail[i].data,
  15203. dh_pubval_fail[i].len);
  15204. if (ret != MP_VAL)
  15205. return -8020 - (int)i;
  15206. }
  15207. for (i = 0; i < sizeof(dh_pubval_pass) / sizeof(*dh_pubval_pass); i++) {
  15208. ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_pass[i].data,
  15209. dh_pubval_pass[i].len);
  15210. if (ret != 0)
  15211. return -8030 - (int)i;
  15212. }
  15213. return 0;
  15214. }
  15215. #endif
  15216. #if defined(HAVE_FFDHE)
  15217. #if defined(HAVE_FFDHE_4096)
  15218. #define MAX_DH_PRIV_SZ 39
  15219. #define MAX_DH_KEY_SZ 512
  15220. #elif defined(HAVE_FFDHE_3072)
  15221. #define MAX_DH_PRIV_SZ 34
  15222. #define MAX_DH_KEY_SZ 384
  15223. #else
  15224. #define MAX_DH_PRIV_SZ 29
  15225. #define MAX_DH_KEY_SZ 256
  15226. #endif
  15227. #ifndef WC_NO_RNG
  15228. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  15229. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM)))
  15230. #ifdef HAVE_PUBLIC_FFDHE
  15231. static int dh_ffdhe_test(WC_RNG *rng, const DhParams* params)
  15232. #else
  15233. static int dh_ffdhe_test(WC_RNG *rng, int name)
  15234. #endif
  15235. {
  15236. int ret;
  15237. word32 privSz, pubSz, privSz2, pubSz2;
  15238. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15239. byte *priv = (byte*)XMALLOC(MAX_DH_PRIV_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15240. byte *pub = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15241. byte *priv2 = (byte*)XMALLOC(MAX_DH_PRIV_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15242. byte *pub2 = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15243. byte *agree = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15244. byte *agree2 = (byte*)XMALLOC(MAX_DH_KEY_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15245. DhKey *key = (DhKey*)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15246. DhKey *key2 = (DhKey*)XMALLOC(sizeof(*key2), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15247. #else
  15248. byte priv[MAX_DH_PRIV_SZ];
  15249. byte pub[MAX_DH_KEY_SZ];
  15250. byte priv2[MAX_DH_PRIV_SZ];
  15251. byte pub2[MAX_DH_KEY_SZ];
  15252. byte agree[MAX_DH_KEY_SZ];
  15253. byte agree2[MAX_DH_KEY_SZ];
  15254. DhKey key[1];
  15255. DhKey key2[1];
  15256. #endif
  15257. word32 agreeSz = MAX_DH_KEY_SZ;
  15258. word32 agreeSz2 = MAX_DH_KEY_SZ;
  15259. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15260. if ((priv == NULL) ||
  15261. (pub == NULL) ||
  15262. (priv2 == NULL) ||
  15263. (pub2 == NULL) ||
  15264. (agree == NULL) ||
  15265. (agree2 == NULL) ||
  15266. (key == NULL) ||
  15267. (key2 == NULL))
  15268. ERROR_OUT(-8050, done);
  15269. #endif
  15270. pubSz = MAX_DH_KEY_SZ;
  15271. pubSz2 = MAX_DH_KEY_SZ;
  15272. #ifdef HAVE_PUBLIC_FFDHE
  15273. privSz = MAX_DH_PRIV_SZ;
  15274. privSz2 = MAX_DH_PRIV_SZ;
  15275. #else
  15276. privSz = wc_DhGetNamedKeyMinSize(name);
  15277. privSz2 = privSz;
  15278. #endif
  15279. XMEMSET(key, 0, sizeof(*key));
  15280. XMEMSET(key2, 0, sizeof(*key2));
  15281. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15282. if (ret != 0) {
  15283. ERROR_OUT(-8051, done);
  15284. }
  15285. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  15286. if (ret != 0) {
  15287. ERROR_OUT(-8052, done);
  15288. }
  15289. #ifdef HAVE_PUBLIC_FFDHE
  15290. ret = wc_DhSetKey(key, params->p, params->p_len, params->g, params->g_len);
  15291. #else
  15292. ret = wc_DhSetNamedKey(key, name);
  15293. #endif
  15294. if (ret != 0) {
  15295. ERROR_OUT(-8053, done);
  15296. }
  15297. #ifdef HAVE_PUBLIC_FFDHE
  15298. ret = wc_DhSetKey(key2, params->p, params->p_len, params->g,
  15299. params->g_len);
  15300. #else
  15301. ret = wc_DhSetNamedKey(key2, name);
  15302. #endif
  15303. if (ret != 0) {
  15304. ERROR_OUT(-8054, done);
  15305. }
  15306. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  15307. #if defined(WOLFSSL_ASYNC_CRYPT)
  15308. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15309. #endif
  15310. if (ret != 0) {
  15311. ERROR_OUT(-8055, done);
  15312. }
  15313. ret = wc_DhGenerateKeyPair(key2, rng, priv2, &privSz2, pub2, &pubSz2);
  15314. #if defined(WOLFSSL_ASYNC_CRYPT)
  15315. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  15316. #endif
  15317. if (ret != 0) {
  15318. ERROR_OUT(-8056, done);
  15319. }
  15320. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  15321. #if defined(WOLFSSL_ASYNC_CRYPT)
  15322. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15323. #endif
  15324. if (ret != 0) {
  15325. ERROR_OUT(-8057, done);
  15326. }
  15327. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  15328. #if defined(WOLFSSL_ASYNC_CRYPT)
  15329. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  15330. #endif
  15331. if (ret != 0) {
  15332. ERROR_OUT(-8058, done);
  15333. }
  15334. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  15335. ERROR_OUT(-8059, done);
  15336. }
  15337. #if defined(WOLFSSL_HAVE_SP_DH) || defined(USE_FAST_MATH)
  15338. /* Make p even */
  15339. key->p.dp[0] &= (mp_digit)-2;
  15340. if (ret != 0) {
  15341. ERROR_OUT(-8058, done);
  15342. }
  15343. ret = wc_DhGenerateKeyPair(key, rng, priv, &privSz, pub, &pubSz);
  15344. #if defined(WOLFSSL_ASYNC_CRYPT)
  15345. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15346. #endif
  15347. if (ret != MP_VAL && ret != MP_EXPTMOD_E) {
  15348. ERROR_OUT(-8058, done);
  15349. }
  15350. ret = wc_DhAgree(key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  15351. #if defined(WOLFSSL_ASYNC_CRYPT)
  15352. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15353. #endif
  15354. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != ASYNC_OP_E) {
  15355. ERROR_OUT(-8057, done);
  15356. }
  15357. ret = wc_DhCheckKeyPair(key, pub, pubSz, priv, privSz);
  15358. if (ret != MP_VAL && ret != MP_EXPTMOD_E && ret != MP_CMP_E &&
  15359. ret != ASYNC_OP_E) {
  15360. ERROR_OUT(-8057, done);
  15361. }
  15362. /* Getting here means success - set ret to 0. */
  15363. ret = 0;
  15364. #endif
  15365. done:
  15366. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC) && \
  15367. !defined(WC_NO_RNG)
  15368. if (priv)
  15369. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15370. if (pub)
  15371. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15372. if (priv2)
  15373. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15374. if (pub2)
  15375. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15376. if (agree)
  15377. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15378. if (agree2)
  15379. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15380. if (key) {
  15381. wc_FreeDhKey(key);
  15382. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15383. }
  15384. if (key2) {
  15385. wc_FreeDhKey(key2);
  15386. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15387. }
  15388. #else
  15389. wc_FreeDhKey(key);
  15390. wc_FreeDhKey(key2);
  15391. #endif
  15392. return ret;
  15393. }
  15394. #endif /* !(HAVE_FIPS_VERSION == 2 && WOLFSSL_SP_ARMxx_ASM) */
  15395. #endif /* !WC_NO_RNG */
  15396. #endif /* HAVE_FFDHE */
  15397. WOLFSSL_TEST_SUBROUTINE int dh_test(void)
  15398. {
  15399. int ret;
  15400. word32 bytes;
  15401. word32 idx = 0, privSz, pubSz, privSz2, pubSz2;
  15402. #ifndef WC_NO_RNG
  15403. WC_RNG rng;
  15404. int rngInit = 0;
  15405. #endif
  15406. int keyInit = 0, key2Init = 0;
  15407. #define DH_TEST_TMP_SIZE 1024
  15408. #if !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  15409. #define DH_TEST_BUF_SIZE 256
  15410. #else
  15411. #define DH_TEST_BUF_SIZE 512
  15412. #endif
  15413. #ifndef WC_NO_RNG
  15414. word32 agreeSz = DH_TEST_BUF_SIZE;
  15415. word32 agreeSz2 = DH_TEST_BUF_SIZE;
  15416. #endif
  15417. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15418. DhKey *key = (DhKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15419. DhKey *key2 = (DhKey *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15420. byte *tmp = (byte *)XMALLOC(DH_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15421. #else
  15422. DhKey key[1];
  15423. DhKey key2[1];
  15424. byte tmp[DH_TEST_TMP_SIZE];
  15425. #endif
  15426. #ifndef WC_NO_RNG
  15427. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15428. byte *priv = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15429. byte *pub = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15430. byte *priv2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15431. byte *pub2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15432. byte *agree = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15433. byte *agree2 = (byte *)XMALLOC(DH_TEST_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15434. if (priv == NULL || pub == NULL || priv2 == NULL || pub2 == NULL ||
  15435. agree == NULL || agree2 == NULL) {
  15436. ERROR_OUT(-8100, done);
  15437. }
  15438. #else
  15439. byte priv[DH_TEST_BUF_SIZE];
  15440. byte pub[DH_TEST_BUF_SIZE];
  15441. byte priv2[DH_TEST_BUF_SIZE];
  15442. byte pub2[DH_TEST_BUF_SIZE];
  15443. byte agree[DH_TEST_BUF_SIZE];
  15444. byte agree2[DH_TEST_BUF_SIZE];
  15445. #endif
  15446. #endif /* !WC_NO_RNG */
  15447. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15448. if (key == NULL || key2 == NULL || tmp == NULL) {
  15449. ERROR_OUT(-8100, done);
  15450. }
  15451. #endif
  15452. #ifdef USE_CERT_BUFFERS_1024
  15453. XMEMCPY(tmp, dh_key_der_1024, (size_t)sizeof_dh_key_der_1024);
  15454. bytes = (size_t)sizeof_dh_key_der_1024;
  15455. #elif defined(USE_CERT_BUFFERS_2048)
  15456. XMEMCPY(tmp, dh_key_der_2048, (size_t)sizeof_dh_key_der_2048);
  15457. bytes = (size_t)sizeof_dh_key_der_2048;
  15458. #elif defined(USE_CERT_BUFFERS_3072)
  15459. XMEMCPY(tmp, dh_key_der_3072, (size_t)sizeof_dh_key_der_3072);
  15460. bytes = (size_t)sizeof_dh_key_der_3072;
  15461. #elif defined(USE_CERT_BUFFERS_4096)
  15462. XMEMCPY(tmp, dh_key_der_4096, (size_t)sizeof_dh_key_der_4096);
  15463. bytes = (size_t)sizeof_dh_key_der_4096;
  15464. #elif defined(NO_ASN)
  15465. /* don't use file, no DER parsing */
  15466. #elif !defined(NO_FILESYSTEM)
  15467. {
  15468. XFILE file = XFOPEN(dhParamsFile, "rb");
  15469. if (! file)
  15470. ERROR_OUT(-8101, done);
  15471. bytes = (word32) XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  15472. XFCLOSE(file);
  15473. }
  15474. #else
  15475. /* No DH key to use. */
  15476. ERROR_OUT(-8102, done);
  15477. #endif /* USE_CERT_BUFFERS */
  15478. (void)idx;
  15479. (void)tmp;
  15480. (void)bytes;
  15481. pubSz = DH_TEST_BUF_SIZE;
  15482. pubSz2 = DH_TEST_BUF_SIZE;
  15483. privSz = DH_TEST_BUF_SIZE;
  15484. privSz2 = DH_TEST_BUF_SIZE;
  15485. #ifndef WC_NO_RNG
  15486. XMEMSET(&rng, 0, sizeof(rng));
  15487. #endif
  15488. /* Use API for coverage. */
  15489. ret = wc_InitDhKey(key);
  15490. if (ret != 0) {
  15491. ERROR_OUT(-8103, done);
  15492. }
  15493. wc_FreeDhKey(key);
  15494. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15495. if (ret != 0) {
  15496. ERROR_OUT(-8104, done);
  15497. }
  15498. keyInit = 1;
  15499. ret = wc_InitDhKey_ex(key2, HEAP_HINT, devId);
  15500. if (ret != 0) {
  15501. ERROR_OUT(-8105, done);
  15502. }
  15503. key2Init = 1;
  15504. #ifdef NO_ASN
  15505. #ifndef WOLFSSL_SP_MATH
  15506. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  15507. if (ret != 0) {
  15508. ERROR_OUT(-8106, done);
  15509. }
  15510. ret = wc_DhSetKey(key2, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  15511. if (ret != 0) {
  15512. ERROR_OUT(-8107, done);
  15513. }
  15514. #else
  15515. ret = wc_DhSetKey(key, dh2048_p, sizeof(dh2048_p), dh2048_g,
  15516. sizeof(dh2048_g));
  15517. if (ret != 0) {
  15518. ERROR_OUT(-8106, done);
  15519. }
  15520. ret = wc_DhSetKey(key2, dh2048_p, sizeof(dh2048_p), dh2048_g,
  15521. sizeof(dh2048_g));
  15522. if (ret != 0) {
  15523. ERROR_OUT(-8107, done);
  15524. }
  15525. #endif
  15526. #else
  15527. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  15528. if (ret != 0) {
  15529. ERROR_OUT(-8108, done);
  15530. }
  15531. idx = 0;
  15532. ret = wc_DhKeyDecode(tmp, &idx, key2, bytes);
  15533. if (ret != 0) {
  15534. ERROR_OUT(-8109, done);
  15535. }
  15536. #endif
  15537. #ifndef WC_NO_RNG
  15538. #ifndef HAVE_FIPS
  15539. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  15540. #else
  15541. ret = wc_InitRng(&rng);
  15542. #endif
  15543. if (ret != 0) {
  15544. ERROR_OUT(-8110, done);
  15545. }
  15546. rngInit = 1;
  15547. ret = wc_DhGenerateKeyPair(key, &rng, priv, &privSz, pub, &pubSz);
  15548. #if defined(WOLFSSL_ASYNC_CRYPT)
  15549. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  15550. #endif
  15551. if (ret != 0) {
  15552. ERROR_OUT(-8111, done);
  15553. }
  15554. ret = wc_DhGenerateKeyPair(key2, &rng, priv2, &privSz2, pub2, &pubSz2);
  15555. #if defined(WOLFSSL_ASYNC_CRYPT)
  15556. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  15557. #endif
  15558. if (ret != 0) {
  15559. ERROR_OUT(-8112, 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 != 0) {
  15566. ERROR_OUT(-8113, done);
  15567. }
  15568. ret = wc_DhAgree(key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  15569. #if defined(WOLFSSL_ASYNC_CRYPT)
  15570. ret = wc_AsyncWait(ret, &key2->asyncDev, WC_ASYNC_FLAG_NONE);
  15571. #endif
  15572. if (ret != 0) {
  15573. ERROR_OUT(-8114, done);
  15574. }
  15575. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  15576. ERROR_OUT(-8115, done);
  15577. }
  15578. #endif /* !WC_NO_RNG */
  15579. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  15580. if (wc_DhCheckPrivKey(NULL, NULL, 0) != BAD_FUNC_ARG)
  15581. ERROR_OUT(-8116, done);
  15582. if (wc_DhCheckPrivKey(key, priv, privSz) != 0)
  15583. ERROR_OUT(-8117, done);
  15584. if (wc_DhExportParamsRaw(NULL, NULL, NULL, NULL, NULL, NULL, NULL) != BAD_FUNC_ARG)
  15585. ERROR_OUT(-8118, done);
  15586. {
  15587. word32 pSz, qSz, gSz;
  15588. if (wc_DhExportParamsRaw(key, NULL, &pSz, NULL, &qSz, NULL, &gSz) != LENGTH_ONLY_E)
  15589. ERROR_OUT(-8119, done);
  15590. }
  15591. #endif
  15592. /* Test DH key import / export */
  15593. #if defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM) && \
  15594. (!defined(HAVE_FIPS) || \
  15595. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  15596. wc_FreeDhKey(key);
  15597. ret = wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15598. if (ret != 0) {
  15599. ERROR_OUT(-8120, done);
  15600. }
  15601. #ifndef NO_ASN
  15602. {
  15603. /* DH Private - Key Export / Import */
  15604. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15605. byte *tmp2;
  15606. #else
  15607. byte tmp2[DH_TEST_TMP_SIZE];
  15608. #endif
  15609. XFILE file = XFOPEN(dhKeyFile, "rb");
  15610. if (!file)
  15611. ERROR_OUT(-8130, done);
  15612. bytes = (word32)XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  15613. XFCLOSE(file);
  15614. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15615. tmp2 = (byte*)XMALLOC(DH_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15616. if (tmp2 == NULL)
  15617. ERROR_OUT(-8131, done);
  15618. #endif
  15619. idx = 0;
  15620. XMEMSET(tmp2, 0, DH_TEST_TMP_SIZE);
  15621. /* Import DH Private key as DER */
  15622. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  15623. if (ret == 0) {
  15624. /* Export as DER */
  15625. idx = DH_TEST_TMP_SIZE;
  15626. ret = wc_DhPrivKeyToDer(key, tmp2, &idx);
  15627. }
  15628. /* Verify export matches original */
  15629. if (ret <= 0 || bytes != idx || XMEMCMP(tmp, tmp2, bytes) != 0) {
  15630. ERROR_OUT(-8132, done);
  15631. }
  15632. /* DH Public Key - Export / Import */
  15633. file = XFOPEN(dhKeyPubFile, "rb");
  15634. if (!file)
  15635. ERROR_OUT(-8133, done);
  15636. bytes = (word32)XFREAD(tmp, 1, DH_TEST_TMP_SIZE, file);
  15637. XFCLOSE(file);
  15638. /* for HAVE_WOLF_BIGINT prevent leak */
  15639. wc_FreeDhKey(key);
  15640. (void)wc_InitDhKey_ex(key, HEAP_HINT, devId);
  15641. idx = 0;
  15642. XMEMSET(tmp2, 0, DH_TEST_TMP_SIZE);
  15643. /* Import DH Public key as DER */
  15644. ret = wc_DhKeyDecode(tmp, &idx, key, bytes);
  15645. if (ret == 0) {
  15646. /* Export as DER */
  15647. idx = DH_TEST_TMP_SIZE;
  15648. ret = wc_DhPubKeyToDer(key, tmp2, &idx);
  15649. }
  15650. /* Verify export matches original */
  15651. if (ret <= 0 || bytes != idx || XMEMCMP(tmp, tmp2, bytes) != 0) {
  15652. ERROR_OUT(-8134, done);
  15653. }
  15654. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15655. XFREE(tmp2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15656. #endif
  15657. }
  15658. #else
  15659. ret = wc_DhSetKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  15660. if (ret != 0) {
  15661. ERROR_OUT(-8121, done);
  15662. }
  15663. #endif /* !NO_ASN */
  15664. privSz = DH_TEST_BUF_SIZE;
  15665. pubSz = DH_TEST_BUF_SIZE;
  15666. ret = wc_DhExportKeyPair(key, priv, &privSz, pub, &pubSz);
  15667. if (ret != 0) {
  15668. ERROR_OUT(-8122, done);
  15669. }
  15670. ret = wc_DhImportKeyPair(key2, priv, privSz, pub, pubSz);
  15671. if (ret != 0) {
  15672. ERROR_OUT(-8125, done);
  15673. }
  15674. #endif /* WOLFSSL_DH_EXTRA && !NO_FILESYSTEM && !FIPS <= 2 */
  15675. #ifndef WC_NO_RNG
  15676. ret = dh_generate_test(&rng);
  15677. if (ret != 0)
  15678. ERROR_OUT(-8123, done);
  15679. ret = dh_fips_generate_test(&rng);
  15680. if (ret != 0)
  15681. ERROR_OUT(-8124, done);
  15682. #endif /* !WC_NO_RNG */
  15683. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  15684. ret = dh_test_check_pubvalue();
  15685. if (ret != 0)
  15686. ERROR_OUT(-8125, done);
  15687. #endif
  15688. #if !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION == 2) && \
  15689. (defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM32_ASM)))
  15690. /* RNG with DH and SP_ASM code not supported in the in-lined FIPS ASM code,
  15691. * this will be available for testing in the 140-3 module */
  15692. #ifndef WC_NO_RNG
  15693. /* Specialized code for key gen when using FFDHE-2048, FFDHE-3072 and FFDHE-4096 */
  15694. #ifdef HAVE_FFDHE_2048
  15695. #ifdef HAVE_PUBLIC_FFDHE
  15696. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe2048_Get());
  15697. #else
  15698. ret = dh_ffdhe_test(&rng, WC_FFDHE_2048);
  15699. #endif
  15700. if (ret != 0)
  15701. ERROR_OUT(-8126, done);
  15702. #endif
  15703. #ifdef HAVE_FFDHE_3072
  15704. #ifdef HAVE_PUBLIC_FFDHE
  15705. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe3072_Get());
  15706. #else
  15707. ret = dh_ffdhe_test(&rng, WC_FFDHE_3072);
  15708. #endif
  15709. if (ret != 0)
  15710. ERROR_OUT(-8127, done);
  15711. #endif
  15712. #ifdef HAVE_FFDHE_4096
  15713. #ifdef HAVE_PUBLIC_FFDHE
  15714. ret = dh_ffdhe_test(&rng, wc_Dh_ffdhe4096_Get());
  15715. #else
  15716. ret = dh_ffdhe_test(&rng, WC_FFDHE_4096);
  15717. #endif
  15718. if (ret != 0)
  15719. ERROR_OUT(-8128, done);
  15720. #endif
  15721. #endif /* !WC_NO_RNG */
  15722. #endif /* HAVE_FIPS_VERSION == 2 && !WOLFSSL_SP_ARM64_ASM */
  15723. wc_FreeDhKey(key);
  15724. keyInit = 0;
  15725. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  15726. !defined(WOLFSSL_OLD_PRIME_CHECK) && !defined(WC_NO_RNG)
  15727. /* Test Check Key */
  15728. ret = wc_DhSetCheckKey(key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g),
  15729. NULL, 0, 0, &rng);
  15730. if (ret != 0)
  15731. ERROR_OUT(-8129, done);
  15732. keyInit = 1; /* DhSetCheckKey also initializes the key, free it */
  15733. #endif
  15734. done:
  15735. #ifndef WC_NO_RNG
  15736. if (rngInit)
  15737. wc_FreeRng(&rng);
  15738. #endif
  15739. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15740. if (key) {
  15741. if (keyInit)
  15742. wc_FreeDhKey(key);
  15743. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15744. }
  15745. if (key2) {
  15746. if (key2Init)
  15747. wc_FreeDhKey(key2);
  15748. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15749. }
  15750. if (tmp)
  15751. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15752. if (priv)
  15753. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15754. if (pub)
  15755. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15756. if (priv2)
  15757. XFREE(priv2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15758. if (pub2)
  15759. XFREE(pub2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15760. if (agree)
  15761. XFREE(agree, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15762. if (agree2)
  15763. XFREE(agree2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15764. #else
  15765. if (keyInit)
  15766. wc_FreeDhKey(key);
  15767. if (key2Init)
  15768. wc_FreeDhKey(key2);
  15769. #endif
  15770. (void)privSz;
  15771. (void)pubSz;
  15772. (void)pubSz2;
  15773. (void)privSz2;
  15774. return ret;
  15775. #undef DH_TEST_BUF_SIZE
  15776. #undef DH_TEST_TMP_SIZE
  15777. }
  15778. #endif /* NO_DH */
  15779. #ifndef NO_DSA
  15780. WOLFSSL_TEST_SUBROUTINE int dsa_test(void)
  15781. {
  15782. int ret = 0, answer;
  15783. word32 bytes;
  15784. word32 idx = 0;
  15785. WC_RNG rng;
  15786. wc_Sha sha;
  15787. byte hash[WC_SHA_DIGEST_SIZE];
  15788. byte signature[40];
  15789. #ifdef WOLFSSL_KEY_GEN
  15790. byte* der = 0;
  15791. #endif
  15792. #define DSA_TEST_TMP_SIZE 1024
  15793. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15794. byte *tmp = (byte *)XMALLOC(DSA_TEST_TMP_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15795. DsaKey *key = (DsaKey *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15796. #ifdef WOLFSSL_KEY_GEN
  15797. DsaKey *derIn = (DsaKey *)XMALLOC(sizeof *derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15798. DsaKey *genKey = (DsaKey *)XMALLOC(sizeof *genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15799. #endif
  15800. if ((tmp == NULL) ||
  15801. (key == NULL)
  15802. #ifdef WOLFSSL_KEY_GEN
  15803. || (derIn == NULL)
  15804. || (genKey == NULL)
  15805. #endif
  15806. ) {
  15807. ret = -8216;
  15808. goto out;
  15809. }
  15810. #else
  15811. byte tmp[1024];
  15812. DsaKey key[1];
  15813. #ifdef WOLFSSL_KEY_GEN
  15814. DsaKey derIn[1];
  15815. DsaKey genKey[1];
  15816. #endif
  15817. #endif
  15818. #ifdef USE_CERT_BUFFERS_1024
  15819. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  15820. bytes = sizeof_dsa_key_der_1024;
  15821. #elif defined(USE_CERT_BUFFERS_2048)
  15822. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  15823. bytes = sizeof_dsa_key_der_2048;
  15824. #else
  15825. {
  15826. XFILE file = XFOPEN(dsaKey, "rb");
  15827. if (!file)
  15828. ERROR_OUT(-8200, out);
  15829. bytes = (word32) XFREAD(tmp, 1, DSA_TEST_TMP_SIZE, file);
  15830. XFCLOSE(file);
  15831. }
  15832. #endif /* USE_CERT_BUFFERS */
  15833. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  15834. if (ret != 0)
  15835. ERROR_OUT(-8201, out);
  15836. wc_ShaUpdate(&sha, tmp, bytes);
  15837. wc_ShaFinal(&sha, hash);
  15838. wc_ShaFree(&sha);
  15839. ret = wc_InitDsaKey(key);
  15840. if (ret != 0)
  15841. ERROR_OUT(-8202, out);
  15842. ret = wc_DsaPrivateKeyDecode(tmp, &idx, key, bytes);
  15843. if (ret != 0)
  15844. ERROR_OUT(-8203, out);
  15845. #ifndef HAVE_FIPS
  15846. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  15847. #else
  15848. ret = wc_InitRng(&rng);
  15849. #endif
  15850. if (ret != 0)
  15851. ERROR_OUT(-8204, out);
  15852. ret = wc_DsaSign(hash, signature, key, &rng);
  15853. if (ret != 0)
  15854. ERROR_OUT(-8205, out);
  15855. ret = wc_DsaVerify(hash, signature, key, &answer);
  15856. if (ret != 0)
  15857. ERROR_OUT(-8206, out);
  15858. if (answer != 1)
  15859. ERROR_OUT(-8207, out);
  15860. wc_FreeDsaKey(key);
  15861. #ifdef WOLFSSL_KEY_GEN
  15862. {
  15863. int derSz = 0;
  15864. ret = wc_InitDsaKey(genKey);
  15865. if (ret != 0)
  15866. ERROR_OUT(-8208, out);
  15867. ret = wc_MakeDsaParameters(&rng, 1024, genKey);
  15868. if (ret != 0) {
  15869. wc_FreeDsaKey(genKey);
  15870. ERROR_OUT(-8209, out);
  15871. }
  15872. ret = wc_MakeDsaKey(&rng, genKey);
  15873. if (ret != 0) {
  15874. wc_FreeDsaKey(genKey);
  15875. ERROR_OUT(-8210, out);
  15876. }
  15877. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15878. if (der == NULL) {
  15879. wc_FreeDsaKey(genKey);
  15880. ERROR_OUT(-8211, out);
  15881. }
  15882. derSz = wc_DsaKeyToDer(genKey, der, FOURK_BUF);
  15883. if (derSz < 0) {
  15884. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15885. ERROR_OUT(-8212, out);
  15886. }
  15887. ret = SaveDerAndPem(der, derSz, keyDerFile, keyPemFile,
  15888. DSA_PRIVATEKEY_TYPE, -5814);
  15889. if (ret != 0) {
  15890. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15891. wc_FreeDsaKey(genKey);
  15892. goto out;
  15893. }
  15894. ret = wc_InitDsaKey(derIn);
  15895. if (ret != 0) {
  15896. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15897. wc_FreeDsaKey(genKey);
  15898. ERROR_OUT(-8213, out);
  15899. }
  15900. idx = 0;
  15901. ret = wc_DsaPrivateKeyDecode(der, &idx, derIn, derSz);
  15902. if (ret != 0) {
  15903. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15904. wc_FreeDsaKey(derIn);
  15905. wc_FreeDsaKey(genKey);
  15906. ERROR_OUT(-8214, out);
  15907. }
  15908. }
  15909. #endif /* WOLFSSL_KEY_GEN */
  15910. out:
  15911. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15912. if (key) {
  15913. #endif
  15914. if (wc_InitDsaKey_h(key, NULL) != 0)
  15915. ret = -8215;
  15916. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15917. }
  15918. #endif
  15919. #ifdef WOLFSSL_KEY_GEN
  15920. if (der)
  15921. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15922. #endif
  15923. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  15924. if (tmp)
  15925. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15926. if (key)
  15927. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15928. #ifdef WOLFSSL_KEY_GEN
  15929. if (derIn) {
  15930. wc_FreeDsaKey(derIn);
  15931. XFREE(derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15932. }
  15933. if (genKey) {
  15934. wc_FreeDsaKey(genKey);
  15935. XFREE(genKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  15936. }
  15937. #endif
  15938. #else /* !WOLFSSL_SMALL_STACK || WOLFSSL_NO_MALLOC */
  15939. #ifdef WOLFSSL_KEY_GEN
  15940. wc_FreeDsaKey(derIn);
  15941. wc_FreeDsaKey(genKey);
  15942. #endif
  15943. #endif
  15944. wc_FreeRng(&rng);
  15945. return ret;
  15946. }
  15947. #endif /* NO_DSA */
  15948. #ifdef WOLFCRYPT_HAVE_SRP
  15949. static int generate_random_salt(byte *buf, word32 size)
  15950. {
  15951. int ret = -8220;
  15952. WC_RNG rng;
  15953. if(NULL == buf || !size)
  15954. return -8221;
  15955. if (buf && size && wc_InitRng_ex(&rng, HEAP_HINT, devId) == 0) {
  15956. ret = wc_RNG_GenerateBlock(&rng, (byte *)buf, size);
  15957. wc_FreeRng(&rng);
  15958. }
  15959. return ret;
  15960. }
  15961. static int srp_test_digest(SrpType dgstType)
  15962. {
  15963. int r;
  15964. byte clientPubKey[192]; /* A */
  15965. byte serverPubKey[192]; /* B */
  15966. word32 clientPubKeySz = 192;
  15967. word32 serverPubKeySz = 192;
  15968. byte username[] = "user";
  15969. word32 usernameSz = 4;
  15970. byte password[] = "password";
  15971. word32 passwordSz = 8;
  15972. WOLFSSL_SMALL_STACK_STATIC const byte N[] = {
  15973. 0xfc, 0x58, 0x7a, 0x8a, 0x70, 0xfb, 0x5a, 0x9a,
  15974. 0x5d, 0x39, 0x48, 0xbf, 0x1c, 0x46, 0xd8, 0x3b,
  15975. 0x7a, 0xe9, 0x1f, 0x85, 0x36, 0x18, 0xc4, 0x35,
  15976. 0x3f, 0xf8, 0x8a, 0x8f, 0x8c, 0x10, 0x2e, 0x01,
  15977. 0x58, 0x1d, 0x41, 0xcb, 0xc4, 0x47, 0xa8, 0xaf,
  15978. 0x9a, 0x6f, 0x58, 0x14, 0xa4, 0x68, 0xf0, 0x9c,
  15979. 0xa6, 0xe7, 0xbf, 0x0d, 0xe9, 0x62, 0x0b, 0xd7,
  15980. 0x26, 0x46, 0x5b, 0x27, 0xcb, 0x4c, 0xf9, 0x7e,
  15981. 0x1e, 0x8b, 0xe6, 0xdd, 0x29, 0xb7, 0xb7, 0x15,
  15982. 0x2e, 0xcf, 0x23, 0xa6, 0x4b, 0x97, 0x9f, 0x89,
  15983. 0xd4, 0x86, 0xc4, 0x90, 0x63, 0x92, 0xf4, 0x30,
  15984. 0x26, 0x69, 0x48, 0x9d, 0x7a, 0x4f, 0xad, 0xb5,
  15985. 0x6a, 0x51, 0xad, 0xeb, 0xf9, 0x90, 0x31, 0x77,
  15986. 0x53, 0x30, 0x2a, 0x85, 0xf7, 0x11, 0x21, 0x0c,
  15987. 0xb8, 0x4b, 0x56, 0x03, 0x5e, 0xbb, 0x25, 0x33,
  15988. 0x7c, 0xd9, 0x5a, 0xd1, 0x5c, 0xb2, 0xd4, 0x53,
  15989. 0xc5, 0x16, 0x68, 0xf0, 0xdf, 0x48, 0x55, 0x3e,
  15990. 0xd4, 0x59, 0x87, 0x64, 0x59, 0xaa, 0x39, 0x01,
  15991. 0x45, 0x89, 0x9c, 0x72, 0xff, 0xdd, 0x8f, 0x6d,
  15992. 0xa0, 0x42, 0xbc, 0x6f, 0x6e, 0x62, 0x18, 0x2d,
  15993. 0x50, 0xe8, 0x18, 0x97, 0x87, 0xfc, 0xef, 0x1f,
  15994. 0xf5, 0x53, 0x68, 0xe8, 0x49, 0xd1, 0xa2, 0xe8,
  15995. 0xb9, 0x26, 0x03, 0xba, 0xb5, 0x58, 0x6f, 0x6c,
  15996. 0x8b, 0x08, 0xa1, 0x7b, 0x6f, 0x42, 0xc9, 0x53
  15997. };
  15998. WOLFSSL_SMALL_STACK_STATIC const byte g[] = {
  15999. 0x02
  16000. };
  16001. byte salt[10];
  16002. byte verifier[192];
  16003. word32 v_size = sizeof(verifier);
  16004. word32 clientProofSz = SRP_MAX_DIGEST_SIZE;
  16005. word32 serverProofSz = SRP_MAX_DIGEST_SIZE;
  16006. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16007. Srp *cli = (Srp *)XMALLOC(sizeof *cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16008. Srp *srv = (Srp *)XMALLOC(sizeof *srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16009. byte *clientProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT,
  16010. DYNAMIC_TYPE_TMP_BUFFER); /* M1 */
  16011. byte *serverProof = (byte *)XMALLOC(SRP_MAX_DIGEST_SIZE, HEAP_HINT,
  16012. DYNAMIC_TYPE_TMP_BUFFER); /* M2 */
  16013. if ((cli == NULL) ||
  16014. (srv == NULL) ||
  16015. (clientProof == NULL) ||
  16016. (serverProof == NULL)) {
  16017. r = -8222;
  16018. goto out;
  16019. }
  16020. #else
  16021. Srp cli[1], srv[1];
  16022. byte clientProof[SRP_MAX_DIGEST_SIZE]; /* M1 */
  16023. byte serverProof[SRP_MAX_DIGEST_SIZE]; /* M2 */
  16024. #endif
  16025. /* set as 0's so if second init on srv not called SrpTerm is not on
  16026. * garbage values */
  16027. XMEMSET(srv, 0, sizeof *srv);
  16028. XMEMSET(cli, 0, sizeof *cli);
  16029. /* generating random salt */
  16030. r = generate_random_salt(salt, sizeof(salt));
  16031. /* client knows username and password. */
  16032. /* server knows N, g, salt and verifier. */
  16033. if (!r) r = wc_SrpInit_ex(cli, dgstType, SRP_CLIENT_SIDE, HEAP_HINT, devId);
  16034. if (!r) r = wc_SrpSetUsername(cli, username, usernameSz);
  16035. /* loading N, g and salt in advance to generate the verifier. */
  16036. if (!r) r = wc_SrpSetParams(cli, N, sizeof(N),
  16037. g, sizeof(g),
  16038. salt, sizeof(salt));
  16039. if (!r) r = wc_SrpSetPassword(cli, password, passwordSz);
  16040. if (!r) r = wc_SrpGetVerifier(cli, verifier, &v_size);
  16041. /* client sends username to server */
  16042. if (!r) r = wc_SrpInit_ex(srv, dgstType, SRP_SERVER_SIDE, HEAP_HINT, devId);
  16043. if (!r) r = wc_SrpSetUsername(srv, username, usernameSz);
  16044. if (!r) r = wc_SrpSetParams(srv, N, sizeof(N),
  16045. g, sizeof(g),
  16046. salt, sizeof(salt));
  16047. if (!r) r = wc_SrpSetVerifier(srv, verifier, v_size);
  16048. if (!r) r = wc_SrpGetPublic(srv, serverPubKey, &serverPubKeySz);
  16049. /* server sends N, g, salt and B to client */
  16050. if (!r) r = wc_SrpGetPublic(cli, clientPubKey, &clientPubKeySz);
  16051. if (!r) r = wc_SrpComputeKey(cli, clientPubKey, clientPubKeySz,
  16052. serverPubKey, serverPubKeySz);
  16053. if (!r) r = wc_SrpGetProof(cli, clientProof, &clientProofSz);
  16054. /* client sends A and M1 to server */
  16055. if (!r) r = wc_SrpComputeKey(srv, clientPubKey, clientPubKeySz,
  16056. serverPubKey, serverPubKeySz);
  16057. if (!r) r = wc_SrpVerifyPeersProof(srv, clientProof, clientProofSz);
  16058. if (!r) r = wc_SrpGetProof(srv, serverProof, &serverProofSz);
  16059. /* server sends M2 to client */
  16060. if (!r) r = wc_SrpVerifyPeersProof(cli, serverProof, serverProofSz);
  16061. wc_SrpTerm(cli);
  16062. wc_SrpTerm(srv);
  16063. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16064. out:
  16065. if (cli)
  16066. XFREE(cli, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16067. if (srv)
  16068. XFREE(srv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16069. if (clientProof)
  16070. XFREE(clientProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16071. if (serverProof)
  16072. XFREE(serverProof, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  16073. #endif
  16074. return r;
  16075. }
  16076. WOLFSSL_TEST_SUBROUTINE int srp_test(void)
  16077. {
  16078. int ret;
  16079. #ifndef NO_SHA
  16080. ret = srp_test_digest(SRP_TYPE_SHA);
  16081. if (ret != 0)
  16082. return ret;
  16083. #endif
  16084. #ifndef NO_SHA256
  16085. ret = srp_test_digest(SRP_TYPE_SHA256);
  16086. if (ret != 0)
  16087. return ret;
  16088. #endif
  16089. #ifdef WOLFSSL_SHA384
  16090. ret = srp_test_digest(SRP_TYPE_SHA384);
  16091. if (ret != 0)
  16092. return ret;
  16093. #endif
  16094. #ifdef WOLFSSL_SHA512
  16095. ret = srp_test_digest(SRP_TYPE_SHA512);
  16096. if (ret != 0)
  16097. return ret;
  16098. #endif
  16099. return ret;
  16100. }
  16101. #endif /* WOLFCRYPT_HAVE_SRP */
  16102. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  16103. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  16104. static int openssl_aes_test(void)
  16105. {
  16106. #ifdef HAVE_AES_CBC
  16107. #ifdef WOLFSSL_AES_128
  16108. {
  16109. /* EVP_CipherUpdate test */
  16110. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  16111. {
  16112. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16113. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  16114. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  16115. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  16116. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  16117. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  16118. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  16119. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  16120. };
  16121. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16122. "0123456789abcdef "; /* align */
  16123. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  16124. "1234567890abcdef "; /* align */
  16125. byte cipher[AES_BLOCK_SIZE * 4];
  16126. byte plain [AES_BLOCK_SIZE * 4];
  16127. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16128. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  16129. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  16130. #else
  16131. EVP_CIPHER_CTX en[1];
  16132. EVP_CIPHER_CTX de[1];
  16133. #endif
  16134. int outlen ;
  16135. int total = 0;
  16136. int i;
  16137. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16138. if ((en == NULL) || (de == NULL))
  16139. return MEMORY_E;
  16140. #endif
  16141. EVP_CIPHER_CTX_init(en);
  16142. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16143. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16144. return -8400;
  16145. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  16146. (byte*)cbcPlain, 9) == 0)
  16147. return -8401;
  16148. if (outlen != 0)
  16149. return -8402;
  16150. total += outlen;
  16151. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  16152. (byte*)&cbcPlain[9] , 9) == 0)
  16153. return -8403;
  16154. if (outlen != 16)
  16155. return -8404;
  16156. total += outlen;
  16157. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  16158. return -8405;
  16159. if (outlen != 16)
  16160. return -8406;
  16161. total += outlen;
  16162. if (total != 32)
  16163. return 3408;
  16164. total = 0;
  16165. EVP_CIPHER_CTX_init(de);
  16166. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16167. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16168. return -8407;
  16169. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  16170. return -8408;
  16171. if (outlen != 0)
  16172. return -8409;
  16173. total += outlen;
  16174. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  16175. (byte*)&cipher[6], 12) == 0)
  16176. return -8410;
  16177. if (outlen != 0)
  16178. total += outlen;
  16179. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  16180. (byte*)&cipher[6+12], 14) == 0)
  16181. return -8411;
  16182. if (outlen != 16)
  16183. return -8412;
  16184. total += outlen;
  16185. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  16186. return -8413;
  16187. if (outlen != 2)
  16188. return -8414;
  16189. total += outlen;
  16190. if (total != 18)
  16191. return 3427;
  16192. if (XMEMCMP(plain, cbcPlain, 18))
  16193. return -8415;
  16194. /* test with encrypting/decrypting more than 16 bytes at once */
  16195. total = 0;
  16196. EVP_CIPHER_CTX_init(en);
  16197. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16198. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16199. return -8416;
  16200. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  16201. (byte*)cbcPlain, 17) == 0)
  16202. return -8417;
  16203. if (outlen != 16)
  16204. return -8418;
  16205. total += outlen;
  16206. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  16207. (byte*)&cbcPlain[17] , 1) == 0)
  16208. return -8419;
  16209. if (outlen != 0)
  16210. return -8420;
  16211. total += outlen;
  16212. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  16213. return -8421;
  16214. if (outlen != 16)
  16215. return -8422;
  16216. total += outlen;
  16217. if (total != 32)
  16218. return -8423;
  16219. total = 0;
  16220. EVP_CIPHER_CTX_init(de);
  16221. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16222. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16223. return -8424;
  16224. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 17) == 0)
  16225. return -8425;
  16226. if (outlen != 16)
  16227. return -8426;
  16228. total += outlen;
  16229. /* final call on non block size should fail */
  16230. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  16231. return -8427;
  16232. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  16233. (byte*)&cipher[17], 1) == 0)
  16234. return -8428;
  16235. if (outlen != 0)
  16236. total += outlen;
  16237. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  16238. (byte*)&cipher[17+1], 14) == 0)
  16239. return -8429;
  16240. if (outlen != 0)
  16241. return -8430;
  16242. total += outlen;
  16243. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  16244. return -8431;
  16245. if (outlen != 2)
  16246. return -8432;
  16247. total += outlen;
  16248. if (total != 18)
  16249. return -8433;
  16250. if (XMEMCMP(plain, cbcPlain, 18))
  16251. return -8434;
  16252. /* test byte by byte decrypt */
  16253. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  16254. plain[i] = i;
  16255. }
  16256. total = 0;
  16257. EVP_CIPHER_CTX_init(en);
  16258. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16259. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16260. return -8435;
  16261. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  16262. (byte*)plain, AES_BLOCK_SIZE * 3) == 0)
  16263. return -8436;
  16264. if (outlen != AES_BLOCK_SIZE * 3)
  16265. return -8437;
  16266. total += outlen;
  16267. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  16268. return -8438;
  16269. if (outlen != AES_BLOCK_SIZE)
  16270. return -8439;
  16271. total += outlen;
  16272. if (total != sizeof(plain))
  16273. return -8440;
  16274. total = 0;
  16275. EVP_CIPHER_CTX_init(de);
  16276. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16277. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16278. return -8441;
  16279. for (i = 0; i < AES_BLOCK_SIZE * 4; i++) {
  16280. if (EVP_CipherUpdate(de, (byte*)plain + total, &outlen,
  16281. (byte*)cipher + i, 1) == 0)
  16282. return -8442;
  16283. if (outlen > 0) {
  16284. int j;
  16285. total += outlen;
  16286. for (j = 0; j < total; j++) {
  16287. if (plain[j] != j) {
  16288. return -8443;
  16289. }
  16290. }
  16291. }
  16292. }
  16293. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  16294. return -8444;
  16295. total += outlen;
  16296. if (total != AES_BLOCK_SIZE * 3) {
  16297. return -8445;
  16298. }
  16299. for (i = 0; i < AES_BLOCK_SIZE * 3; i++) {
  16300. if (plain[i] != i) {
  16301. return -8446;
  16302. }
  16303. }
  16304. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16305. wolfSSL_EVP_CIPHER_CTX_free(en);
  16306. wolfSSL_EVP_CIPHER_CTX_free(de);
  16307. #endif
  16308. }
  16309. /* set buffers to be exact size to catch potential over read/write */
  16310. {
  16311. /* EVP_CipherUpdate test */
  16312. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  16313. {
  16314. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16315. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  16316. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  16317. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  16318. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  16319. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  16320. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  16321. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  16322. };
  16323. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16324. "0123456789abcdef "; /* align */
  16325. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  16326. "1234567890abcdef "; /* align */
  16327. #define EVP_TEST_BUF_SZ 18
  16328. #define EVP_TEST_BUF_PAD 32
  16329. byte cipher[EVP_TEST_BUF_SZ];
  16330. byte plain [EVP_TEST_BUF_SZ];
  16331. byte padded[EVP_TEST_BUF_PAD];
  16332. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16333. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  16334. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  16335. #else
  16336. EVP_CIPHER_CTX en[1];
  16337. EVP_CIPHER_CTX de[1];
  16338. #endif
  16339. int outlen ;
  16340. int total = 0;
  16341. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16342. if ((en == NULL) || (de == NULL))
  16343. return MEMORY_E;
  16344. #endif
  16345. EVP_CIPHER_CTX_init(en);
  16346. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16347. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16348. return -8447;
  16349. if (EVP_CIPHER_CTX_set_padding(en, 0) != 1)
  16350. return -8448;
  16351. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  16352. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  16353. return -8449;
  16354. if (outlen != 16)
  16355. return -8450;
  16356. total += outlen;
  16357. /* should fail here */
  16358. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) != 0)
  16359. return -8451;
  16360. /* turn padding back on and do successful encrypt */
  16361. total = 0;
  16362. EVP_CIPHER_CTX_init(en);
  16363. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  16364. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  16365. return -8452;
  16366. if (EVP_CIPHER_CTX_set_padding(en, 1) != 1)
  16367. return -8453;
  16368. if (EVP_CipherUpdate(en, (byte*)padded, &outlen,
  16369. (byte*)cbcPlain, EVP_TEST_BUF_SZ) == 0)
  16370. return -8454;
  16371. if (outlen != 16)
  16372. return -8455;
  16373. total += outlen;
  16374. if (EVP_CipherFinal(en, (byte*)&padded[total], &outlen) == 0)
  16375. return -8456;
  16376. total += outlen;
  16377. if (total != 32)
  16378. return -8457;
  16379. XMEMCPY(cipher, padded, EVP_TEST_BUF_SZ);
  16380. /* test out of bounds read on buffers w/o padding during decryption */
  16381. total = 0;
  16382. EVP_CIPHER_CTX_init(de);
  16383. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16384. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16385. return -8458;
  16386. if (EVP_CIPHER_CTX_set_padding(de, 0) != 1)
  16387. return -8459;
  16388. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher,
  16389. EVP_TEST_BUF_SZ) == 0)
  16390. return -8460;
  16391. if (outlen != 16)
  16392. return -8461;
  16393. total += outlen;
  16394. /* should fail since not using padding */
  16395. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) != 0)
  16396. return -8462;
  16397. total = 0;
  16398. EVP_CIPHER_CTX_init(de);
  16399. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  16400. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  16401. return -8463;
  16402. if (EVP_CIPHER_CTX_set_padding(de, 1) != 1)
  16403. return -8464;
  16404. if (EVP_CipherUpdate(de, (byte*)padded, &outlen, (byte*)padded,
  16405. EVP_TEST_BUF_PAD) == 0)
  16406. return -8465;
  16407. if (outlen != 16)
  16408. return -8466;
  16409. total += outlen;
  16410. if (EVP_CipherFinal(de, (byte*)&padded[total], &outlen) == 0)
  16411. return -8467;
  16412. if (XMEMCMP(padded, cbcPlain, EVP_TEST_BUF_SZ))
  16413. return -8468;
  16414. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16415. wolfSSL_EVP_CIPHER_CTX_free(en);
  16416. wolfSSL_EVP_CIPHER_CTX_free(de);
  16417. #endif
  16418. }
  16419. { /* evp_cipher test: EVP_aes_128_cbc */
  16420. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16421. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  16422. #else
  16423. EVP_CIPHER_CTX ctx[1];
  16424. #endif
  16425. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  16426. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16427. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16428. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16429. };
  16430. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  16431. {
  16432. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  16433. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  16434. };
  16435. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16436. "0123456789abcdef "; /* align */
  16437. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  16438. "1234567890abcdef "; /* align */
  16439. byte cipher[AES_BLOCK_SIZE * 4];
  16440. byte plain [AES_BLOCK_SIZE * 4];
  16441. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16442. if (ctx == NULL)
  16443. return MEMORY_E;
  16444. #endif
  16445. EVP_CIPHER_CTX_init(ctx);
  16446. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1) == 0)
  16447. return -8469;
  16448. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  16449. return -8470;
  16450. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  16451. return -8471;
  16452. EVP_CIPHER_CTX_init(ctx);
  16453. if (EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0) == 0)
  16454. return -8472;
  16455. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  16456. return -8473;
  16457. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  16458. return -8474;
  16459. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16460. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  16461. #endif
  16462. } /* end evp_cipher test: EVP_aes_128_cbc*/
  16463. #endif /* WOLFSSL_AES_128 */
  16464. #endif /* HAVE_AES_CBC */
  16465. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  16466. { /* evp_cipher test: EVP_aes_256_ecb*/
  16467. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16468. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  16469. #else
  16470. EVP_CIPHER_CTX ctx[1];
  16471. #endif
  16472. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  16473. {
  16474. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16475. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16476. };
  16477. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  16478. {
  16479. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  16480. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  16481. };
  16482. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16483. {
  16484. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  16485. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  16486. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  16487. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  16488. };
  16489. byte cipher[AES_BLOCK_SIZE * 4];
  16490. byte plain [AES_BLOCK_SIZE * 4];
  16491. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16492. if (ctx == NULL)
  16493. return MEMORY_E;
  16494. #endif
  16495. EVP_CIPHER_CTX_init(ctx);
  16496. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1) == 0)
  16497. return -8475;
  16498. if (EVP_Cipher(ctx, cipher, (byte*)msg, 16) != 16)
  16499. return -8476;
  16500. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  16501. return -8477;
  16502. EVP_CIPHER_CTX_init(ctx);
  16503. if (EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0) == 0)
  16504. return -8478;
  16505. if (EVP_Cipher(ctx, plain, cipher, 16) != 16)
  16506. return -8479;
  16507. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  16508. return -8480;
  16509. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16510. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  16511. #endif
  16512. } /* end evp_cipher test */
  16513. #endif /* HAVE_AES_ECB && WOLFSSL_AES_256 */
  16514. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  16515. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  16516. {
  16517. /* Test: AES_encrypt/decrypt/set Key */
  16518. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16519. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  16520. #ifdef HAVE_AES_DECRYPT
  16521. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  16522. #endif
  16523. #else
  16524. AES_KEY enc[1];
  16525. #ifdef HAVE_AES_DECRYPT
  16526. AES_KEY dec[1];
  16527. #endif
  16528. #endif
  16529. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  16530. {
  16531. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16532. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16533. };
  16534. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  16535. {
  16536. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  16537. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  16538. };
  16539. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16540. {
  16541. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  16542. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  16543. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  16544. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  16545. };
  16546. byte plain[sizeof(msg)];
  16547. byte cipher[sizeof(msg)];
  16548. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16549. if (enc == NULL)
  16550. return MEMORY_E;
  16551. #ifdef HAVE_AES_DECRYPT
  16552. if (dec == NULL)
  16553. return MEMORY_E;
  16554. #endif
  16555. #endif
  16556. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  16557. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  16558. AES_encrypt(msg, cipher, enc);
  16559. #ifdef HAVE_AES_DECRYPT
  16560. AES_decrypt(cipher, plain, dec);
  16561. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  16562. return -8481;
  16563. #endif /* HAVE_AES_DECRYPT */
  16564. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  16565. return -8482;
  16566. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16567. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  16568. #ifdef HAVE_AES_DECRYPT
  16569. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  16570. #endif
  16571. #endif
  16572. }
  16573. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  16574. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  16575. #ifdef WOLFSSL_AES_COUNTER
  16576. {
  16577. byte plainBuff [64];
  16578. byte cipherBuff[64];
  16579. #ifdef WOLFSSL_AES_128
  16580. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  16581. {
  16582. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  16583. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  16584. };
  16585. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  16586. {
  16587. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  16588. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  16589. };
  16590. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  16591. {
  16592. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16593. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  16594. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  16595. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  16596. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  16597. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  16598. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  16599. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  16600. };
  16601. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  16602. {
  16603. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  16604. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  16605. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  16606. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  16607. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  16608. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  16609. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  16610. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  16611. };
  16612. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  16613. {
  16614. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  16615. 0xc2
  16616. };
  16617. #endif
  16618. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  16619. * NIST Special Publication 800-38A */
  16620. #ifdef WOLFSSL_AES_192
  16621. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  16622. {
  16623. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  16624. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  16625. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  16626. };
  16627. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  16628. {
  16629. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  16630. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  16631. };
  16632. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  16633. {
  16634. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16635. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16636. };
  16637. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  16638. {
  16639. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  16640. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  16641. };
  16642. #endif /* WOLFSSL_AES_192 */
  16643. #ifdef WOLFSSL_AES_256
  16644. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  16645. * NIST Special Publication 800-38A */
  16646. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  16647. {
  16648. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  16649. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  16650. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  16651. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  16652. };
  16653. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  16654. {
  16655. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  16656. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  16657. };
  16658. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  16659. {
  16660. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16661. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  16662. };
  16663. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  16664. {
  16665. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  16666. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  16667. };
  16668. #endif /* WOLFSSL_AES_256 */
  16669. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16670. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  16671. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  16672. #else
  16673. EVP_CIPHER_CTX en[1];
  16674. EVP_CIPHER_CTX de[1];
  16675. #endif
  16676. #ifdef WOLFSSL_AES_128
  16677. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16678. EVP_CIPHER_CTX *p_en;
  16679. EVP_CIPHER_CTX *p_de;
  16680. #endif
  16681. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16682. if ((en == NULL) || (de == NULL))
  16683. return MEMORY_E;
  16684. #endif
  16685. EVP_CIPHER_CTX_init(en);
  16686. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  16687. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16688. return -8483;
  16689. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  16690. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16691. return -8484;
  16692. EVP_CIPHER_CTX_init(de);
  16693. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  16694. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16695. return -8485;
  16696. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  16697. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16698. return -8486;
  16699. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  16700. return -8487;
  16701. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  16702. return -8488;
  16703. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16704. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  16705. if (p_en == NULL)
  16706. return -8489;
  16707. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  16708. if (p_de == NULL)
  16709. return -8490;
  16710. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  16711. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16712. return -8491;
  16713. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  16714. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16715. return -8492;
  16716. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  16717. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16718. return -8493;
  16719. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  16720. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  16721. return -8494;
  16722. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  16723. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  16724. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  16725. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  16726. return -8495;
  16727. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  16728. return -8496;
  16729. EVP_CIPHER_CTX_init(en);
  16730. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  16731. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16732. return -8497;
  16733. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  16734. return -8498;
  16735. EVP_CIPHER_CTX_init(de);
  16736. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  16737. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  16738. return -8499;
  16739. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  16740. return -8500;
  16741. if (XMEMCMP(plainBuff, ctrPlain, 9))
  16742. return -8501;
  16743. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  16744. return -8502;
  16745. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  16746. return -8503;
  16747. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  16748. return -8504;
  16749. if (XMEMCMP(plainBuff, ctrPlain, 9))
  16750. return -8505;
  16751. if (XMEMCMP(cipherBuff, oddCipher, 9))
  16752. return -8506;
  16753. #endif /* WOLFSSL_AES_128 */
  16754. #ifdef WOLFSSL_AES_192
  16755. EVP_CIPHER_CTX_init(en);
  16756. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  16757. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  16758. return -8507;
  16759. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  16760. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16761. return -8508;
  16762. EVP_CIPHER_CTX_init(de);
  16763. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  16764. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  16765. return -8509;
  16766. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  16767. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  16768. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16769. return -8510;
  16770. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  16771. return -8511;
  16772. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  16773. return -8512;
  16774. #endif /* WOLFSSL_AES_192 */
  16775. #ifdef WOLFSSL_AES_256
  16776. EVP_CIPHER_CTX_init(en);
  16777. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  16778. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  16779. return -8513;
  16780. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  16781. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16782. return -8514;
  16783. EVP_CIPHER_CTX_init(de);
  16784. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  16785. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  16786. return -8515;
  16787. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  16788. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  16789. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  16790. return -8516;
  16791. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  16792. return -8517;
  16793. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  16794. return -8518;
  16795. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16796. wolfSSL_EVP_CIPHER_CTX_free(en);
  16797. wolfSSL_EVP_CIPHER_CTX_free(de);
  16798. #endif
  16799. #endif /* WOLFSSL_AES_256 */
  16800. }
  16801. #endif /* HAVE_AES_COUNTER */
  16802. #if defined(WOLFSSL_AES_CFB) && defined(WOLFSSL_AES_128)
  16803. {
  16804. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16805. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  16806. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  16807. #else
  16808. AES_KEY enc[1];
  16809. AES_KEY dec[1];
  16810. #endif
  16811. WOLFSSL_SMALL_STACK_STATIC const byte setIv[] = {
  16812. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  16813. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
  16814. };
  16815. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  16816. {
  16817. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  16818. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  16819. };
  16820. WOLFSSL_SMALL_STACK_STATIC const byte cipher1[] =
  16821. {
  16822. 0x3b,0x3f,0xd9,0x2e,0xb7,0x2d,0xad,0x20,
  16823. 0x33,0x34,0x49,0xf8,0xe8,0x3c,0xfb,0x4a,
  16824. 0xc8,0xa6,0x45,0x37,0xa0,0xb3,0xa9,0x3f,
  16825. 0xcd,0xe3,0xcd,0xad,0x9f,0x1c,0xe5,0x8b
  16826. };
  16827. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  16828. {
  16829. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  16830. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  16831. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  16832. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51
  16833. };
  16834. byte cipher[AES_BLOCK_SIZE * 2];
  16835. byte iv[AES_BLOCK_SIZE]; /* iv buffer is updeated by API */
  16836. int num = 0;
  16837. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16838. if ((enc == NULL) || (dec == NULL))
  16839. return MEMORY_E;
  16840. #endif
  16841. XMEMCPY(iv, setIv, sizeof(setIv));
  16842. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, enc);
  16843. wolfSSL_AES_set_encrypt_key(key, sizeof(key) * 8, dec);
  16844. wolfSSL_AES_cfb128_encrypt(msg, cipher, AES_BLOCK_SIZE - 1, enc, iv,
  16845. &num, AES_ENCRYPT);
  16846. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE - 1))
  16847. return -8519;
  16848. if (num != 15) /* should have used 15 of the 16 bytes */
  16849. return -8520;
  16850. wolfSSL_AES_cfb128_encrypt(msg + AES_BLOCK_SIZE - 1,
  16851. cipher + AES_BLOCK_SIZE - 1, AES_BLOCK_SIZE + 1, enc, iv,
  16852. &num, AES_ENCRYPT);
  16853. if (XMEMCMP(cipher, cipher1, AES_BLOCK_SIZE * 2))
  16854. return -8521;
  16855. if (num != 0)
  16856. return -8522;
  16857. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  16858. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  16859. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  16860. #endif
  16861. }
  16862. #endif /* WOLFSSL_AES_CFB && WOLFSSL_AES_128 */
  16863. return 0;
  16864. }
  16865. #endif /* !defined(NO_AES) && !defined(WOLFCRYPT_ONLY) */
  16866. WOLFSSL_TEST_SUBROUTINE int openssl_test(void)
  16867. {
  16868. int ret;
  16869. EVP_MD_CTX md_ctx;
  16870. testVector a, b, c, d, e, f;
  16871. byte hash[WC_SHA256_DIGEST_SIZE*2]; /* max size */
  16872. a.inLen = 0;
  16873. b.inLen = c.inLen = d.inLen = e.inLen = f.inLen = a.inLen;
  16874. (void)a;
  16875. (void)b;
  16876. (void)c;
  16877. (void)d;
  16878. (void)e;
  16879. (void)f;
  16880. /* test malloc / free , 10 is an arbitrary amount of memory chosen */
  16881. {
  16882. byte* p;
  16883. p = (byte*)CRYPTO_malloc(10, "", 0);
  16884. if (p == NULL) {
  16885. return -8600;
  16886. }
  16887. XMEMSET(p, 0, 10);
  16888. CRYPTO_free(p, "", 0);
  16889. }
  16890. #ifndef NO_MD5
  16891. a.input = "1234567890123456789012345678901234567890123456789012345678"
  16892. "9012345678901234567890";
  16893. a.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  16894. "\x7a";
  16895. a.inLen = XSTRLEN(a.input);
  16896. a.outLen = WC_MD5_DIGEST_SIZE;
  16897. EVP_MD_CTX_init(&md_ctx);
  16898. ret = EVP_DigestInit(&md_ctx, EVP_md5());
  16899. if (ret == WOLFSSL_SUCCESS) {
  16900. ret = EVP_DigestUpdate(&md_ctx, a.input, (unsigned long)a.inLen);
  16901. }
  16902. if (ret == WOLFSSL_SUCCESS) {
  16903. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  16904. }
  16905. EVP_MD_CTX_cleanup(&md_ctx);
  16906. if (ret != WOLFSSL_SUCCESS)
  16907. return -18601;
  16908. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0)
  16909. return -8601;
  16910. #endif /* NO_MD5 */
  16911. #ifndef NO_SHA
  16912. b.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  16913. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  16914. "aaaaaaaaaa";
  16915. b.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  16916. "\x53\x99\x5E\x26\xA0";
  16917. b.inLen = XSTRLEN(b.input);
  16918. b.outLen = WC_SHA_DIGEST_SIZE;
  16919. EVP_MD_CTX_init(&md_ctx);
  16920. ret = EVP_DigestInit(&md_ctx, EVP_sha1());
  16921. if (ret == WOLFSSL_SUCCESS) {
  16922. ret = EVP_DigestUpdate(&md_ctx, b.input, (unsigned long)b.inLen);
  16923. if (ret == WOLFSSL_SUCCESS)
  16924. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  16925. }
  16926. EVP_MD_CTX_cleanup(&md_ctx);
  16927. if (ret != WOLFSSL_SUCCESS)
  16928. return -18602;
  16929. if (XMEMCMP(hash, b.output, WC_SHA_DIGEST_SIZE) != 0)
  16930. return -8602;
  16931. #endif /* NO_SHA */
  16932. #ifdef WOLFSSL_SHA224
  16933. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  16934. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  16935. e.output = "\xc9\x7c\xa9\xa5\x59\x85\x0c\xe9\x7a\x04\xa9\x6d\xef\x6d\x99"
  16936. "\xa9\xe0\xe0\xe2\xab\x14\xe6\xb8\xdf\x26\x5f\xc0\xb3";
  16937. e.inLen = XSTRLEN(e.input);
  16938. e.outLen = WC_SHA224_DIGEST_SIZE;
  16939. EVP_MD_CTX_init(&md_ctx);
  16940. ret = EVP_DigestInit(&md_ctx, EVP_sha224());
  16941. if (ret == WOLFSSL_SUCCESS) {
  16942. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  16943. if (ret == WOLFSSL_SUCCESS)
  16944. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  16945. }
  16946. EVP_MD_CTX_cleanup(&md_ctx);
  16947. if (ret != WOLFSSL_SUCCESS ||
  16948. XMEMCMP(hash, e.output, WC_SHA224_DIGEST_SIZE) != 0) {
  16949. return -8603;
  16950. }
  16951. #endif /* WOLFSSL_SHA224 */
  16952. #ifndef NO_SHA256
  16953. d.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  16954. d.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  16955. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  16956. "\x06\xC1";
  16957. d.inLen = XSTRLEN(d.input);
  16958. d.outLen = WC_SHA256_DIGEST_SIZE;
  16959. EVP_MD_CTX_init(&md_ctx);
  16960. ret = EVP_DigestInit(&md_ctx, EVP_sha256());
  16961. if (ret == WOLFSSL_SUCCESS) {
  16962. ret = EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen);
  16963. if (ret == WOLFSSL_SUCCESS)
  16964. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  16965. }
  16966. EVP_MD_CTX_cleanup(&md_ctx);
  16967. if (ret != WOLFSSL_SUCCESS ||
  16968. XMEMCMP(hash, d.output, WC_SHA256_DIGEST_SIZE) != 0) {
  16969. return -8604;
  16970. }
  16971. #endif /* !NO_SHA256 */
  16972. #ifdef WOLFSSL_SHA384
  16973. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  16974. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  16975. e.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  16976. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  16977. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  16978. "\x74\x60\x39";
  16979. e.inLen = XSTRLEN(e.input);
  16980. e.outLen = WC_SHA384_DIGEST_SIZE;
  16981. EVP_MD_CTX_init(&md_ctx);
  16982. ret = EVP_DigestInit(&md_ctx, EVP_sha384());
  16983. if (ret == WOLFSSL_SUCCESS) {
  16984. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  16985. if (ret == WOLFSSL_SUCCESS)
  16986. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  16987. }
  16988. EVP_MD_CTX_cleanup(&md_ctx);
  16989. if (ret != WOLFSSL_SUCCESS ||
  16990. XMEMCMP(hash, e.output, WC_SHA384_DIGEST_SIZE) != 0) {
  16991. return -8605;
  16992. }
  16993. #endif /* WOLFSSL_SHA384 */
  16994. #ifdef WOLFSSL_SHA512
  16995. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  16996. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  16997. f.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  16998. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  16999. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  17000. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  17001. "\x87\x4b\xe9\x09";
  17002. f.inLen = XSTRLEN(f.input);
  17003. f.outLen = WC_SHA512_DIGEST_SIZE;
  17004. EVP_MD_CTX_init(&md_ctx);
  17005. ret = EVP_DigestInit(&md_ctx, EVP_sha512());
  17006. if (ret == WOLFSSL_SUCCESS) {
  17007. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  17008. if (ret == WOLFSSL_SUCCESS)
  17009. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17010. }
  17011. EVP_MD_CTX_cleanup(&md_ctx);
  17012. if (ret != WOLFSSL_SUCCESS ||
  17013. XMEMCMP(hash, f.output, WC_SHA512_DIGEST_SIZE) != 0) {
  17014. return -8606;
  17015. }
  17016. #endif /* WOLFSSL_SHA512 */
  17017. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  17018. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  17019. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17020. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17021. f.output = "\x23\xfe\xc5\xbb\x94\xd6\x0b\x23\x30\x81\x92\x64\x0b\x0c\x45"
  17022. "\x33\x35\xd6\x64\x73\x4f\xe4\x0e\x72\x68\x67\x4a\xf9";
  17023. f.inLen = XSTRLEN(f.input);
  17024. f.outLen = WC_SHA512_224_DIGEST_SIZE;
  17025. EVP_MD_CTX_init(&md_ctx);
  17026. ret = EVP_DigestInit(&md_ctx, EVP_sha512_224());
  17027. if (ret == WOLFSSL_SUCCESS) {
  17028. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  17029. if (ret == WOLFSSL_SUCCESS)
  17030. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17031. }
  17032. EVP_MD_CTX_cleanup(&md_ctx);
  17033. if (ret != WOLFSSL_SUCCESS ||
  17034. XMEMCMP(hash, f.output, WC_SHA512_224_DIGEST_SIZE) != 0) {
  17035. return -8722;
  17036. }
  17037. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  17038. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  17039. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  17040. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  17041. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17042. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17043. f.output = "\x39\x28\xe1\x84\xfb\x86\x90\xf8\x40\xda\x39\x88\x12\x1d\x31"
  17044. "\xbe\x65\xcb\x9d\x3e\xf8\x3e\xe6\x14\x6f\xea\xc8\x61\xe1\x9b"
  17045. "\x56\x3a";
  17046. f.inLen = XSTRLEN(f.input);
  17047. f.outLen = WC_SHA512_256_DIGEST_SIZE;
  17048. EVP_MD_CTX_init(&md_ctx);
  17049. ret = EVP_DigestInit(&md_ctx, EVP_sha512_256());
  17050. if (ret == WOLFSSL_SUCCESS) {
  17051. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  17052. if (ret == WOLFSSL_SUCCESS)
  17053. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17054. }
  17055. EVP_MD_CTX_cleanup(&md_ctx);
  17056. if (ret != WOLFSSL_SUCCESS ||
  17057. XMEMCMP(hash, f.output, WC_SHA512_256_DIGEST_SIZE) != 0) {
  17058. return -8723;
  17059. }
  17060. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  17061. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  17062. #ifdef WOLFSSL_SHA3
  17063. #ifndef WOLFSSL_NOSHA3_224
  17064. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17065. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17066. e.output = "\x54\x3e\x68\x68\xe1\x66\x6c\x1a\x64\x36\x30\xdf\x77\x36\x7a"
  17067. "\xe5\xa6\x2a\x85\x07\x0a\x51\xc1\x4c\xbf\x66\x5c\xbc";
  17068. e.inLen = XSTRLEN(e.input);
  17069. e.outLen = WC_SHA3_224_DIGEST_SIZE;
  17070. EVP_MD_CTX_init(&md_ctx);
  17071. ret = EVP_DigestInit(&md_ctx, EVP_sha3_224());
  17072. if (ret == WOLFSSL_SUCCESS) {
  17073. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  17074. if (ret == WOLFSSL_SUCCESS)
  17075. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17076. }
  17077. EVP_MD_CTX_cleanup(&md_ctx);
  17078. if (ret != WOLFSSL_SUCCESS ||
  17079. XMEMCMP(hash, e.output, WC_SHA3_224_DIGEST_SIZE) != 0) {
  17080. return -8607;
  17081. }
  17082. #endif /* WOLFSSL_NOSHA3_224 */
  17083. #ifndef WOLFSSL_NOSHA3_256
  17084. d.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17085. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17086. d.output = "\x91\x6f\x60\x61\xfe\x87\x97\x41\xca\x64\x69\xb4\x39\x71\xdf"
  17087. "\xdb\x28\xb1\xa3\x2d\xc3\x6c\xb3\x25\x4e\x81\x2b\xe2\x7a\xad"
  17088. "\x1d\x18";
  17089. d.inLen = XSTRLEN(d.input);
  17090. d.outLen = WC_SHA3_256_DIGEST_SIZE;
  17091. EVP_MD_CTX_init(&md_ctx);
  17092. ret = EVP_DigestInit(&md_ctx, EVP_sha3_256());
  17093. if (ret == WOLFSSL_SUCCESS) {
  17094. ret = EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen);
  17095. if (ret == WOLFSSL_SUCCESS)
  17096. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17097. }
  17098. EVP_MD_CTX_cleanup(&md_ctx);
  17099. if (ret != WOLFSSL_SUCCESS ||
  17100. XMEMCMP(hash, d.output, WC_SHA3_256_DIGEST_SIZE) != 0) {
  17101. return -8608;
  17102. }
  17103. #endif /* WOLFSSL_NOSHA3_256 */
  17104. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17105. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17106. e.output = "\x79\x40\x7d\x3b\x59\x16\xb5\x9c\x3e\x30\xb0\x98\x22\x97\x47"
  17107. "\x91\xc3\x13\xfb\x9e\xcc\x84\x9e\x40\x6f\x23\x59\x2d\x04\xf6"
  17108. "\x25\xdc\x8c\x70\x9b\x98\xb4\x3b\x38\x52\xb3\x37\x21\x61\x79"
  17109. "\xaa\x7f\xc7";
  17110. e.inLen = XSTRLEN(e.input);
  17111. e.outLen = WC_SHA3_384_DIGEST_SIZE;
  17112. EVP_MD_CTX_init(&md_ctx);
  17113. ret = EVP_DigestInit(&md_ctx, EVP_sha3_384());
  17114. if (ret == WOLFSSL_SUCCESS) {
  17115. ret = EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  17116. if (ret == WOLFSSL_SUCCESS)
  17117. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17118. }
  17119. EVP_MD_CTX_cleanup(&md_ctx);
  17120. if (ret != WOLFSSL_SUCCESS ||
  17121. XMEMCMP(hash, e.output, WC_SHA3_384_DIGEST_SIZE) != 0) {
  17122. return -8609;
  17123. }
  17124. #ifndef WOLFSSL_NOSHA3_512
  17125. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  17126. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  17127. f.output = "\xaf\xeb\xb2\xef\x54\x2e\x65\x79\xc5\x0c\xad\x06\xd2\xe5\x78"
  17128. "\xf9\xf8\xdd\x68\x81\xd7\xdc\x82\x4d\x26\x36\x0f\xee\xbf\x18"
  17129. "\xa4\xfa\x73\xe3\x26\x11\x22\x94\x8e\xfc\xfd\x49\x2e\x74\xe8"
  17130. "\x2e\x21\x89\xed\x0f\xb4\x40\xd1\x87\xf3\x82\x27\x0c\xb4\x55"
  17131. "\xf2\x1d\xd1\x85";
  17132. f.inLen = XSTRLEN(f.input);
  17133. f.outLen = WC_SHA3_512_DIGEST_SIZE;
  17134. EVP_MD_CTX_init(&md_ctx);
  17135. ret = EVP_DigestInit(&md_ctx, EVP_sha3_512());
  17136. if (ret == WOLFSSL_SUCCESS) {
  17137. ret = EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  17138. if (ret == WOLFSSL_SUCCESS)
  17139. ret = EVP_DigestFinal(&md_ctx, hash, 0);
  17140. }
  17141. EVP_MD_CTX_cleanup(&md_ctx);
  17142. if (ret != WOLFSSL_SUCCESS ||
  17143. XMEMCMP(hash, f.output, WC_SHA3_512_DIGEST_SIZE) != 0) {
  17144. return -8610;
  17145. }
  17146. #endif /* WOLFSSL_NOSHA3_512 */
  17147. #endif /* WOLFSSL_SHA3 */
  17148. #ifndef WC_NO_RNG
  17149. if (RAND_bytes(hash, sizeof(hash)) != WOLFSSL_SUCCESS)
  17150. return -8611;
  17151. #endif
  17152. #ifndef NO_MD5
  17153. c.input = "what do ya want for nothing?";
  17154. c.output = "\x55\x78\xe8\x48\x4b\xcc\x93\x80\x93\xec\x53\xaf\x22\xd6\x14"
  17155. "\x76";
  17156. c.inLen = XSTRLEN(c.input);
  17157. c.outLen = WC_MD5_DIGEST_SIZE;
  17158. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)
  17159. /* Expect failure with MD5 + HMAC when using FIPS 140-3. */
  17160. if (HMAC(EVP_md5(), "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen,
  17161. hash, 0) != NULL)
  17162. #else
  17163. if (HMAC(EVP_md5(), "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen,
  17164. hash, 0) == NULL ||
  17165. XMEMCMP(hash, c.output, WC_MD5_DIGEST_SIZE) != 0)
  17166. #endif
  17167. {
  17168. return -8612;
  17169. }
  17170. #endif /* NO_MD5 */
  17171. #ifndef NO_DES3
  17172. { /* des test */
  17173. WOLFSSL_SMALL_STACK_STATIC const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  17174. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  17175. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  17176. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  17177. };
  17178. byte plain[24];
  17179. byte cipher[24];
  17180. const_DES_cblock key = {
  17181. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  17182. };
  17183. DES_cblock iv = {
  17184. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  17185. };
  17186. DES_key_schedule sched;
  17187. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17188. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  17189. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  17190. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  17191. };
  17192. DES_key_sched(&key, &sched);
  17193. DES_cbc_encrypt(vector, cipher, sizeof(vector), &sched, &iv, DES_ENCRYPT);
  17194. DES_cbc_encrypt(cipher, plain, sizeof(vector), &sched, &iv, DES_DECRYPT);
  17195. if (XMEMCMP(plain, vector, sizeof(vector)) != 0)
  17196. return -8613;
  17197. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  17198. return -8614;
  17199. /* test changing iv */
  17200. DES_ncbc_encrypt(vector, cipher, 8, &sched, &iv, DES_ENCRYPT);
  17201. DES_ncbc_encrypt(vector + 8, cipher + 8, 16, &sched, &iv, DES_ENCRYPT);
  17202. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  17203. return -8615;
  17204. } /* end des test */
  17205. #endif /* NO_DES3 */
  17206. #if !defined(NO_AES) && !defined(WOLFCRYPT_ONLY)
  17207. if ((ret = openssl_aes_test()) != 0) {
  17208. return ret;
  17209. }
  17210. #if defined(WOLFSSL_AES_128) && defined(HAVE_AES_CBC)
  17211. { /* evp_cipher test: EVP_aes_128_cbc */
  17212. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17213. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  17214. #else
  17215. EVP_CIPHER_CTX ctx[1];
  17216. #endif
  17217. int idx, cipherSz, plainSz;
  17218. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  17219. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  17220. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  17221. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  17222. };
  17223. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17224. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  17225. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  17226. 0x3b,0x5d,0x41,0x97,0x94,0x25,0xa4,0xb4,
  17227. 0xae,0x7b,0x34,0xd0,0x3f,0x0c,0xbc,0x06
  17228. };
  17229. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  17230. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  17231. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  17232. 0x7d,0x37,0x7b,0x0b,0x44,0xaa,0xb5,0xf0,
  17233. 0x5f,0x34,0xb4,0xde,0xb5,0xbd,0x2a,0xbb
  17234. };
  17235. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  17236. "0123456789abcdef "; /* align */
  17237. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  17238. "1234567890abcdef "; /* align */
  17239. byte cipher[AES_BLOCK_SIZE * 4];
  17240. byte plain [AES_BLOCK_SIZE * 4];
  17241. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17242. if (ctx == NULL)
  17243. return MEMORY_E;
  17244. #endif
  17245. cipherSz = 0;
  17246. EVP_CIPHER_CTX_init(ctx);
  17247. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  17248. if (ret == WOLFSSL_SUCCESS) {
  17249. ret = EVP_CipherUpdate(ctx, cipher, &idx, (byte*)msg, sizeof(msg));
  17250. if (ret == WOLFSSL_SUCCESS)
  17251. cipherSz += idx;
  17252. }
  17253. if (ret == WOLFSSL_SUCCESS) {
  17254. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  17255. if (ret == WOLFSSL_SUCCESS)
  17256. cipherSz += idx;
  17257. }
  17258. EVP_CIPHER_CTX_cleanup(ctx);
  17259. if (ret != WOLFSSL_SUCCESS)
  17260. return -8617;
  17261. if (cipherSz != (int)sizeof(verify) || XMEMCMP(cipher, verify, cipherSz))
  17262. return -8618;
  17263. /* check partial decrypt (not enough padding for full block) */
  17264. plainSz = 0;
  17265. EVP_CIPHER_CTX_init(ctx);
  17266. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  17267. if (ret == WOLFSSL_SUCCESS) {
  17268. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, 1);
  17269. if (ret == WOLFSSL_SUCCESS)
  17270. plainSz += idx;
  17271. }
  17272. if (ret == WOLFSSL_SUCCESS) {
  17273. /* this test should fail... not enough padding for full block */
  17274. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  17275. if (plainSz == 0 && ret != WOLFSSL_SUCCESS)
  17276. ret = WOLFSSL_SUCCESS;
  17277. else
  17278. ret = -8619;
  17279. }
  17280. else
  17281. ret = -8620;
  17282. EVP_CIPHER_CTX_cleanup(ctx);
  17283. if (ret != WOLFSSL_SUCCESS)
  17284. return ret;
  17285. plainSz = 0;
  17286. EVP_CIPHER_CTX_init(ctx);
  17287. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 0);
  17288. if (ret == WOLFSSL_SUCCESS) {
  17289. ret = EVP_CipherUpdate(ctx, plain, &idx, cipher, cipherSz);
  17290. if (ret == WOLFSSL_SUCCESS)
  17291. plainSz += idx;
  17292. }
  17293. if (ret == WOLFSSL_SUCCESS) {
  17294. ret = EVP_CipherFinal(ctx, plain + plainSz, &idx);
  17295. if (ret == WOLFSSL_SUCCESS)
  17296. plainSz += idx;
  17297. }
  17298. EVP_CIPHER_CTX_cleanup(ctx);
  17299. if (ret != WOLFSSL_SUCCESS)
  17300. return -8621;
  17301. if (plainSz != (int)sizeof(msg) || XMEMCMP(plain, msg, sizeof(msg)))
  17302. return -8622;
  17303. cipherSz = 0;
  17304. EVP_CIPHER_CTX_init(ctx);
  17305. ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), key, iv, 1);
  17306. if (ret == WOLFSSL_SUCCESS) {
  17307. ret = EVP_CipherUpdate(ctx, cipher, &idx, msg, AES_BLOCK_SIZE);
  17308. if (ret == WOLFSSL_SUCCESS)
  17309. cipherSz += idx;
  17310. }
  17311. if (ret == WOLFSSL_SUCCESS) {
  17312. ret = EVP_CipherFinal(ctx, cipher + cipherSz, &idx);
  17313. if (ret == WOLFSSL_SUCCESS)
  17314. cipherSz += idx;
  17315. }
  17316. EVP_CIPHER_CTX_cleanup(ctx);
  17317. if (ret != WOLFSSL_SUCCESS)
  17318. return -8623;
  17319. if (cipherSz != (int)sizeof(verify2) || XMEMCMP(cipher, verify2, cipherSz))
  17320. return -8624;
  17321. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17322. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  17323. #endif
  17324. } /* end evp_cipher test: EVP_aes_128_cbc*/
  17325. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  17326. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  17327. { /* evp_cipher test: EVP_aes_256_ecb*/
  17328. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17329. EVP_CIPHER_CTX *ctx = wolfSSL_EVP_CIPHER_CTX_new();
  17330. #else
  17331. EVP_CIPHER_CTX ctx[1];
  17332. #endif
  17333. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  17334. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17335. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  17336. };
  17337. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  17338. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  17339. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  17340. };
  17341. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  17342. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  17343. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  17344. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  17345. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  17346. };
  17347. byte cipher[AES_BLOCK_SIZE * 4];
  17348. byte plain [AES_BLOCK_SIZE * 4];
  17349. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17350. if (ctx == NULL)
  17351. return MEMORY_E;
  17352. #endif
  17353. EVP_CIPHER_CTX_init(ctx);
  17354. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 1);
  17355. if (ret == WOLFSSL_SUCCESS)
  17356. ret = EVP_Cipher(ctx, cipher, (byte*)msg, 16);
  17357. EVP_CIPHER_CTX_cleanup(ctx);
  17358. if (ret != 16)
  17359. return -8625;
  17360. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  17361. return -8626;
  17362. EVP_CIPHER_CTX_init(ctx);
  17363. ret = EVP_CipherInit(ctx, EVP_aes_256_ecb(), (unsigned char*)key, NULL, 0);
  17364. if (ret == WOLFSSL_SUCCESS)
  17365. ret = EVP_Cipher(ctx, plain, cipher, 16);
  17366. EVP_CIPHER_CTX_cleanup(ctx);
  17367. if (ret != 16)
  17368. return -8627;
  17369. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  17370. return -8628;
  17371. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17372. wolfSSL_EVP_CIPHER_CTX_free(ctx);
  17373. #endif
  17374. } /* end evp_cipher test */
  17375. #endif /* HAVE_AES_ECB && WOLFSSL_AES_128 */
  17376. #define OPENSSL_TEST_ERROR (-10000)
  17377. #if defined(WOLFSSL_AES_DIRECT) && defined(WOLFSSL_AES_256)
  17378. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  17379. {
  17380. /* Test: AES_encrypt/decrypt/set Key */
  17381. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17382. AES_KEY *enc = (AES_KEY *)XMALLOC(sizeof *enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  17383. #ifdef HAVE_AES_DECRYPT
  17384. AES_KEY *dec = (AES_KEY *)XMALLOC(sizeof *dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  17385. #endif
  17386. #else
  17387. AES_KEY enc[1];
  17388. #ifdef HAVE_AES_DECRYPT
  17389. AES_KEY dec[1];
  17390. #endif
  17391. #endif
  17392. WOLFSSL_SMALL_STACK_STATIC const byte msg[] =
  17393. {
  17394. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17395. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  17396. };
  17397. WOLFSSL_SMALL_STACK_STATIC const byte verify[] =
  17398. {
  17399. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  17400. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  17401. };
  17402. WOLFSSL_SMALL_STACK_STATIC const byte key[] =
  17403. {
  17404. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  17405. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  17406. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  17407. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  17408. };
  17409. byte plain[sizeof(msg)];
  17410. byte cipher[sizeof(msg)];
  17411. printf("openSSL extra test\n") ;
  17412. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17413. if (enc == NULL)
  17414. return MEMORY_E;
  17415. #ifdef HAVE_AES_DECRYPT
  17416. if (dec == NULL)
  17417. return MEMORY_E;
  17418. #endif
  17419. #endif
  17420. AES_set_encrypt_key(key, sizeof(key)*8, enc);
  17421. AES_set_decrypt_key(key, sizeof(key)*8, dec);
  17422. AES_encrypt(msg, cipher, enc);
  17423. #ifdef HAVE_AES_DECRYPT
  17424. AES_decrypt(cipher, plain, dec);
  17425. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  17426. return OPENSSL_TEST_ERROR-60;
  17427. #endif /* HAVE_AES_DECRYPT */
  17428. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  17429. return OPENSSL_TEST_ERROR-61;
  17430. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17431. XFREE(enc, HEAP_HINT, DYNAMIC_TYPE_AES);
  17432. #ifdef HAVE_AES_DECRYPT
  17433. XFREE(dec, HEAP_HINT, DYNAMIC_TYPE_AES);
  17434. #endif
  17435. #endif
  17436. }
  17437. #endif /* WOLFSSL_AES_DIRECT && WOLFSSL_AES_256 */
  17438. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  17439. #ifdef WOLFSSL_AES_COUNTER
  17440. {
  17441. byte plainBuff [64];
  17442. byte cipherBuff[64];
  17443. #ifdef WOLFSSL_AES_128
  17444. WOLFSSL_SMALL_STACK_STATIC const byte ctrKey[] =
  17445. {
  17446. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  17447. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  17448. };
  17449. WOLFSSL_SMALL_STACK_STATIC const byte ctrIv[] =
  17450. {
  17451. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  17452. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  17453. };
  17454. WOLFSSL_SMALL_STACK_STATIC const byte ctrPlain[] =
  17455. {
  17456. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17457. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  17458. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  17459. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  17460. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  17461. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  17462. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  17463. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  17464. };
  17465. WOLFSSL_SMALL_STACK_STATIC const byte ctrCipher[] =
  17466. {
  17467. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  17468. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  17469. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  17470. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  17471. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  17472. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  17473. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  17474. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  17475. };
  17476. WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
  17477. {
  17478. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  17479. 0xc2
  17480. };
  17481. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17482. EVP_CIPHER_CTX *p_en;
  17483. EVP_CIPHER_CTX *p_de;
  17484. #endif
  17485. #endif /* WOLFSSL_AES_128 */
  17486. #ifdef WOLFSSL_AES_192
  17487. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  17488. * NIST Special Publication 800-38A */
  17489. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Key[] =
  17490. {
  17491. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  17492. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  17493. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  17494. };
  17495. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Iv[] =
  17496. {
  17497. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  17498. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  17499. };
  17500. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Plain[] =
  17501. {
  17502. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17503. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  17504. };
  17505. WOLFSSL_SMALL_STACK_STATIC const byte ctr192Cipher[] =
  17506. {
  17507. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  17508. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  17509. };
  17510. #endif /* WOLFSSL_AES_192 */
  17511. #ifdef WOLFSSL_AES_256
  17512. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  17513. * NIST Special Publication 800-38A */
  17514. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
  17515. {
  17516. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  17517. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  17518. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  17519. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  17520. };
  17521. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Iv[] =
  17522. {
  17523. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  17524. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  17525. };
  17526. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Plain[] =
  17527. {
  17528. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  17529. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  17530. };
  17531. WOLFSSL_SMALL_STACK_STATIC const byte ctr256Cipher[] =
  17532. {
  17533. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  17534. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  17535. };
  17536. #endif /* WOLFSSL_AES_256 */
  17537. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17538. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  17539. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  17540. if ((en == NULL) || (de == NULL))
  17541. return MEMORY_E;
  17542. #else
  17543. EVP_CIPHER_CTX en[1];
  17544. EVP_CIPHER_CTX de[1];
  17545. #endif
  17546. #ifdef WOLFSSL_AES_128
  17547. EVP_CIPHER_CTX_init(en);
  17548. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  17549. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17550. return -8629;
  17551. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain,
  17552. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  17553. return -8630;
  17554. EVP_CIPHER_CTX_init(de);
  17555. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  17556. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17557. return -8631;
  17558. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  17559. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  17560. return -8632;
  17561. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  17562. return -8633;
  17563. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  17564. return -8634;
  17565. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17566. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  17567. if (p_en == NULL)
  17568. return -8635;
  17569. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  17570. if (p_de == NULL)
  17571. return -8636;
  17572. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  17573. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17574. return -8637;
  17575. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  17576. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  17577. return -8638;
  17578. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  17579. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17580. return -8639;
  17581. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  17582. AES_BLOCK_SIZE*4) != AES_BLOCK_SIZE*4)
  17583. return -8640;
  17584. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  17585. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  17586. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  17587. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  17588. return -8641;
  17589. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  17590. return -8642;
  17591. EVP_CIPHER_CTX_init(en);
  17592. if (EVP_CipherInit(en, EVP_aes_128_ctr(),
  17593. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17594. return -8643;
  17595. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  17596. return -8644;
  17597. EVP_CIPHER_CTX_init(de);
  17598. if (EVP_CipherInit(de, EVP_aes_128_ctr(),
  17599. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  17600. return -8645;
  17601. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  17602. return -8646;
  17603. if (XMEMCMP(plainBuff, ctrPlain, 9))
  17604. return -8647;
  17605. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  17606. return -8648;
  17607. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctrPlain, 9) != 9)
  17608. return -8649;
  17609. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff, 9) != 9)
  17610. return -8650;
  17611. if (XMEMCMP(plainBuff, ctrPlain, 9))
  17612. return -8651;
  17613. if (XMEMCMP(cipherBuff, oddCipher, 9))
  17614. return -8652;
  17615. #endif /* WOLFSSL_AES_128 */
  17616. #ifdef WOLFSSL_AES_192
  17617. EVP_CIPHER_CTX_init(en);
  17618. if (EVP_CipherInit(en, EVP_aes_192_ctr(),
  17619. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  17620. return -8653;
  17621. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr192Plain,
  17622. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  17623. return -8654;
  17624. EVP_CIPHER_CTX_init(de);
  17625. if (EVP_CipherInit(de, EVP_aes_192_ctr(),
  17626. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  17627. return -8655;
  17628. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  17629. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  17630. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  17631. return -8656;
  17632. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  17633. return -8657;
  17634. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  17635. return -8658;
  17636. #endif /* WOLFSSL_AES_192 */
  17637. #ifdef WOLFSSL_AES_256
  17638. EVP_CIPHER_CTX_init(en);
  17639. if (EVP_CipherInit(en, EVP_aes_256_ctr(),
  17640. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  17641. return -8659;
  17642. if (EVP_Cipher(en, (byte*)cipherBuff, (byte*)ctr256Plain,
  17643. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  17644. return -8660;
  17645. EVP_CIPHER_CTX_init(de);
  17646. if (EVP_CipherInit(de, EVP_aes_256_ctr(),
  17647. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  17648. return -8661;
  17649. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  17650. if (EVP_Cipher(de, (byte*)plainBuff, (byte*)cipherBuff,
  17651. AES_BLOCK_SIZE) != AES_BLOCK_SIZE)
  17652. return -8662;
  17653. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  17654. return -8663;
  17655. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  17656. return -8664;
  17657. #endif /* WOLFSSL_AES_256 */
  17658. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17659. wolfSSL_EVP_CIPHER_CTX_free(en);
  17660. wolfSSL_EVP_CIPHER_CTX_free(de);
  17661. #endif
  17662. }
  17663. #endif /* HAVE_AES_COUNTER */
  17664. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  17665. {
  17666. /* EVP_CipherUpdate test */
  17667. WOLFSSL_SMALL_STACK_STATIC const byte cbcPlain[] =
  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. byte key[] = "0123456789abcdef "; /* align */
  17679. byte iv[] = "1234567890abcdef "; /* align */
  17680. byte cipher[AES_BLOCK_SIZE * 4];
  17681. byte plain [AES_BLOCK_SIZE * 4];
  17682. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17683. EVP_CIPHER_CTX *en = wolfSSL_EVP_CIPHER_CTX_new();
  17684. EVP_CIPHER_CTX *de = wolfSSL_EVP_CIPHER_CTX_new();
  17685. #else
  17686. EVP_CIPHER_CTX en[1];
  17687. EVP_CIPHER_CTX de[1];
  17688. #endif
  17689. int outlen ;
  17690. int total = 0;
  17691. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17692. if ((en == NULL) || (de == NULL))
  17693. return MEMORY_E;
  17694. #endif
  17695. EVP_CIPHER_CTX_init(en);
  17696. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  17697. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  17698. return -8665;
  17699. /* openSSL compatibility, if(inlen == 0)return 1; */
  17700. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  17701. (byte*)cbcPlain, 0) != 1)
  17702. return -8666;
  17703. EVP_CIPHER_CTX_init(en);
  17704. if (EVP_CipherInit(en, EVP_aes_128_cbc(),
  17705. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  17706. return -8667;
  17707. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen,
  17708. (byte*)cbcPlain, 9) == 0)
  17709. return -8668;
  17710. if(outlen != 0)
  17711. return -8669;
  17712. total += outlen;
  17713. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen,
  17714. (byte*)&cbcPlain[9] , 9) == 0)
  17715. return -8670;
  17716. if(outlen != 16)
  17717. return -8671;
  17718. total += outlen;
  17719. if (EVP_CipherFinal(en, (byte*)&cipher[total], &outlen) == 0)
  17720. return -8672;
  17721. if(outlen != 16)
  17722. return -8673;
  17723. total += outlen;
  17724. if(total != 32)
  17725. return -8674;
  17726. total = 0;
  17727. EVP_CIPHER_CTX_init(de);
  17728. if (EVP_CipherInit(de, EVP_aes_128_cbc(),
  17729. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  17730. return -8675;
  17731. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  17732. return -8676;
  17733. if(outlen != 0)
  17734. return -8677;
  17735. total += outlen;
  17736. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  17737. (byte*)&cipher[6], 12) == 0)
  17738. return -8678;
  17739. if(outlen != 0)
  17740. total += outlen;
  17741. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen,
  17742. (byte*)&cipher[6+12], 14) == 0)
  17743. return -8679;
  17744. if(outlen != 16)
  17745. return -8680;
  17746. total += outlen;
  17747. if (EVP_CipherFinal(de, (byte*)&plain[total], &outlen) == 0)
  17748. return -8681;
  17749. if(outlen != 2)
  17750. return -8682;
  17751. total += outlen;
  17752. if(total != 18)
  17753. return -8683;
  17754. if (XMEMCMP(plain, cbcPlain, 18))
  17755. return -8684;
  17756. total = 0;
  17757. EVP_CIPHER_CTX_init(en);
  17758. if (EVP_EncryptInit(en, EVP_aes_128_cbc(),
  17759. (unsigned char*)key, (unsigned char*)iv) == 0)
  17760. return -8685;
  17761. if (EVP_CipherUpdate(en, (byte*)cipher, &outlen, (byte*)cbcPlain, 9) == 0)
  17762. return -8686;
  17763. if(outlen != 0)
  17764. return -8687;
  17765. total += outlen;
  17766. if (EVP_CipherUpdate(en, (byte*)&cipher[total], &outlen, (byte*)&cbcPlain[9] , 9) == 0)
  17767. return -8688;
  17768. if(outlen != 16)
  17769. return -8689;
  17770. total += outlen;
  17771. if (EVP_EncryptFinal(en, (byte*)&cipher[total], &outlen) == 0)
  17772. return -8690;
  17773. if(outlen != 16)
  17774. return -8691;
  17775. total += outlen;
  17776. if(total != 32)
  17777. return 3438;
  17778. total = 0;
  17779. EVP_CIPHER_CTX_init(de);
  17780. if (EVP_DecryptInit(de, EVP_aes_128_cbc(),
  17781. (unsigned char*)key, (unsigned char*)iv) == 0)
  17782. return -8692;
  17783. if (EVP_CipherUpdate(de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  17784. return -8693;
  17785. if(outlen != 0)
  17786. return -8694;
  17787. total += outlen;
  17788. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6], 12) == 0)
  17789. return -8695;
  17790. if(outlen != 0)
  17791. total += outlen;
  17792. if (EVP_CipherUpdate(de, (byte*)&plain[total], &outlen, (byte*)&cipher[6+12], 14) == 0)
  17793. return -8696;
  17794. if(outlen != 16)
  17795. return -8697;
  17796. total += outlen;
  17797. if (EVP_DecryptFinal(de, (byte*)&plain[total], &outlen) == 0)
  17798. return -8698;
  17799. if(outlen != 2)
  17800. return -8699;
  17801. total += outlen;
  17802. if(total != 18)
  17803. return 3447;
  17804. if (XMEMCMP(plain, cbcPlain, 18))
  17805. return -8700;
  17806. if (EVP_CIPHER_key_length(NULL) != 0)
  17807. return -8701;
  17808. if (EVP_CIPHER_key_length(EVP_aes_128_cbc()) != 16)
  17809. return -8702;
  17810. if (EVP_CIPHER_CTX_mode(NULL) != 0)
  17811. return -8703;
  17812. if (EVP_CIPHER_CTX_mode(en) != (en->flags & WOLFSSL_EVP_CIPH_MODE))
  17813. return -8704;
  17814. EVP_CIPHER_CTX_init(en);
  17815. if (EVP_CipherInit_ex(en, EVP_aes_128_cbc(), NULL,
  17816. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  17817. return -8705;
  17818. EVP_CIPHER_CTX_init(en);
  17819. if (EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  17820. (unsigned char*)key, (unsigned char*)iv) == 0)
  17821. return -8706;
  17822. if (wolfSSL_EVP_EncryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  17823. return -8707;
  17824. if (wolfSSL_EVP_EncryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  17825. return -8708;
  17826. EVP_CIPHER_CTX_init(de);
  17827. if (EVP_DecryptInit_ex(de, EVP_aes_128_cbc(), NULL,
  17828. (unsigned char*)key, (unsigned char*)iv) == 0)
  17829. return -8709;
  17830. if (wolfSSL_EVP_DecryptFinal(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  17831. return -8710;
  17832. if (wolfSSL_EVP_DecryptFinal_ex(NULL, NULL, NULL) != WOLFSSL_FAILURE)
  17833. return -8711;
  17834. if (EVP_CIPHER_CTX_block_size(NULL) != BAD_FUNC_ARG)
  17835. return -8712;
  17836. EVP_CIPHER_CTX_init(en);
  17837. EVP_EncryptInit_ex(en, EVP_aes_128_cbc(), NULL,
  17838. (unsigned char*)key, (unsigned char*)iv);
  17839. if (EVP_CIPHER_CTX_block_size(en) != en->block_size)
  17840. return -8713;
  17841. if (EVP_CIPHER_block_size(NULL) != BAD_FUNC_ARG)
  17842. return -8714;
  17843. if (EVP_CIPHER_block_size(EVP_aes_128_cbc()) != AES_BLOCK_SIZE)
  17844. return -8715;
  17845. if (WOLFSSL_EVP_CIPHER_mode(NULL) != 0)
  17846. return -8716;
  17847. if (EVP_CIPHER_flags(EVP_aes_128_cbc()) != WOLFSSL_EVP_CIPH_CBC_MODE)
  17848. return -8717;
  17849. EVP_CIPHER_CTX_clear_flags(en, 0xFFFFFFFF);
  17850. EVP_CIPHER_CTX_set_flags(en, 42);
  17851. if (en->flags != 42)
  17852. return -8718;
  17853. if (EVP_CIPHER_CTX_set_padding(NULL, 0) != BAD_FUNC_ARG)
  17854. return -8719;
  17855. if (EVP_CIPHER_CTX_set_padding(en, 0) != WOLFSSL_SUCCESS)
  17856. return -8720;
  17857. if (EVP_CIPHER_CTX_set_padding(en, 1) != WOLFSSL_SUCCESS)
  17858. return -8721;
  17859. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  17860. wolfSSL_EVP_CIPHER_CTX_free(en);
  17861. wolfSSL_EVP_CIPHER_CTX_free(de);
  17862. #endif
  17863. }
  17864. #endif /* WOLFSSL_AES_128 && HAVE_AES_CBC */
  17865. #endif /* ifndef NO_AES */
  17866. return 0;
  17867. }
  17868. WOLFSSL_TEST_SUBROUTINE int openSSL_evpMD_test(void)
  17869. {
  17870. int ret = 0;
  17871. #if !defined(NO_SHA256) && !defined(NO_SHA)
  17872. WOLFSSL_EVP_MD_CTX* ctx;
  17873. WOLFSSL_EVP_MD_CTX* ctx2;
  17874. ctx = EVP_MD_CTX_create();
  17875. ctx2 = EVP_MD_CTX_create();
  17876. ret = EVP_DigestInit(ctx, EVP_sha256());
  17877. if (ret != SSL_SUCCESS) {
  17878. ret = -8800;
  17879. goto openSSL_evpMD_test_done;
  17880. }
  17881. ret = EVP_MD_CTX_copy(ctx2, ctx);
  17882. if (ret != SSL_SUCCESS) {
  17883. ret = -8801;
  17884. goto openSSL_evpMD_test_done;
  17885. }
  17886. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  17887. ret = -8802;
  17888. goto openSSL_evpMD_test_done;
  17889. }
  17890. ret = EVP_DigestInit(ctx, EVP_sha1());
  17891. if (ret != SSL_SUCCESS) {
  17892. ret = -8803;
  17893. goto openSSL_evpMD_test_done;
  17894. }
  17895. if (EVP_MD_type(EVP_sha256()) != EVP_MD_CTX_type(ctx2)) {
  17896. ret = -8804;
  17897. goto openSSL_evpMD_test_done;
  17898. }
  17899. ret = EVP_MD_CTX_copy_ex(ctx2, ctx);
  17900. if (ret != SSL_SUCCESS) {
  17901. ret = -8805;
  17902. goto openSSL_evpMD_test_done;
  17903. }
  17904. if (EVP_MD_type(EVP_sha256()) == EVP_MD_CTX_type(ctx2)) {
  17905. ret = -8806;
  17906. goto openSSL_evpMD_test_done;
  17907. }
  17908. if (EVP_MD_type(EVP_sha1()) != EVP_MD_CTX_type(ctx2)) {
  17909. ret = -8807;
  17910. goto openSSL_evpMD_test_done;
  17911. }
  17912. if (EVP_DigestInit_ex(ctx, EVP_sha1(), NULL) != SSL_SUCCESS) {
  17913. ret = -8808;
  17914. goto openSSL_evpMD_test_done;
  17915. }
  17916. if (EVP_add_digest(NULL) != 0) {
  17917. ret = -8809;
  17918. goto openSSL_evpMD_test_done;
  17919. }
  17920. if (wolfSSL_EVP_add_cipher(NULL) != 0) {
  17921. ret = -8810;
  17922. goto openSSL_evpMD_test_done;
  17923. }
  17924. ret = 0; /* got to success state without jumping to end with a fail */
  17925. openSSL_evpMD_test_done:
  17926. EVP_MD_CTX_destroy(ctx);
  17927. EVP_MD_CTX_destroy(ctx2);
  17928. #endif /* NO_SHA256 */
  17929. return ret;
  17930. }
  17931. #ifdef DEBUG_SIGN
  17932. static void show(const char *title, const char *p, unsigned int s) {
  17933. char* i;
  17934. printf("%s: ", title);
  17935. for (i = p;
  17936. i < p + s;
  17937. printf("%c", *i), i++);
  17938. printf("\n");
  17939. }
  17940. #else
  17941. #define show(a,b,c)
  17942. #endif
  17943. #define FOURK_BUFF 4096
  17944. #define ERR_BASE_PKEY (-5000)
  17945. WOLFSSL_TEST_SUBROUTINE int openssl_pkey0_test(void)
  17946. {
  17947. int ret = 0;
  17948. #if !defined(NO_RSA) && !defined(HAVE_USER_RSA) && !defined(NO_SHA)
  17949. byte* prvTmp;
  17950. byte* pubTmp;
  17951. int prvBytes;
  17952. int pubBytes;
  17953. RSA *prvRsa = NULL;
  17954. RSA *pubRsa = NULL;
  17955. EVP_PKEY *prvPkey = NULL;
  17956. EVP_PKEY *pubPkey = NULL;
  17957. EVP_PKEY_CTX *enc = NULL;
  17958. EVP_PKEY_CTX *dec = NULL;
  17959. byte in[] = TEST_STRING;
  17960. byte out[256];
  17961. size_t outlen;
  17962. size_t keySz;
  17963. byte plain[256];
  17964. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  17965. XFILE keyFile;
  17966. XFILE keypubFile;
  17967. char cliKey[] = "./certs/client-key.der";
  17968. char cliKeypub[] = "./certs/client-keyPub.der";
  17969. #endif
  17970. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17971. if (prvTmp == NULL)
  17972. return ERR_BASE_PKEY-1;
  17973. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  17974. if (pubTmp == NULL) {
  17975. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17976. return ERR_BASE_PKEY-2;
  17977. }
  17978. #ifdef USE_CERT_BUFFERS_1024
  17979. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  17980. prvBytes = sizeof_client_key_der_1024;
  17981. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  17982. pubBytes = sizeof_client_keypub_der_1024;
  17983. #elif defined(USE_CERT_BUFFERS_2048)
  17984. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  17985. prvBytes = sizeof_client_key_der_2048;
  17986. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  17987. pubBytes = sizeof_client_keypub_der_2048;
  17988. #else
  17989. keyFile = XFOPEN(cliKey, "rb");
  17990. if (!keyFile) {
  17991. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17992. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  17993. err_sys("can't open ./certs/client-key.der, "
  17994. "Please run from wolfSSL home dir", ERR_BASE_PKEY-3);
  17995. return ERR_BASE_PKEY-3;
  17996. }
  17997. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  17998. XFCLOSE(keyFile);
  17999. keypubFile = XFOPEN(cliKeypub, "rb");
  18000. if (!keypubFile) {
  18001. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18002. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18003. err_sys("can't open ./certs/client-cert.der, "
  18004. "Please run from wolfSSL home dir", -4);
  18005. return ERR_BASE_PKEY-4;
  18006. }
  18007. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  18008. XFCLOSE(keypubFile);
  18009. #endif /* USE_CERT_BUFFERS */
  18010. prvRsa = wolfSSL_RSA_new();
  18011. pubRsa = wolfSSL_RSA_new();
  18012. if((prvRsa == NULL) || (pubRsa == NULL)){
  18013. printf("error with RSA_new\n");
  18014. ret = ERR_BASE_PKEY-10;
  18015. goto openssl_pkey0_test_done;
  18016. }
  18017. ret = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  18018. if(ret != SSL_SUCCESS){
  18019. printf("error with RSA_LoadDer_ex\n");
  18020. ret = ERR_BASE_PKEY-11;
  18021. goto openssl_pkey0_test_done;
  18022. }
  18023. ret = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  18024. if(ret != SSL_SUCCESS){
  18025. printf("error with RSA_LoadDer_ex\n");
  18026. ret = ERR_BASE_PKEY-12;
  18027. goto openssl_pkey0_test_done;
  18028. }
  18029. keySz = (size_t)RSA_size(pubRsa);
  18030. prvPkey = wolfSSL_EVP_PKEY_new();
  18031. pubPkey = wolfSSL_EVP_PKEY_new();
  18032. if((prvPkey == NULL) || (pubPkey == NULL)){
  18033. printf("error with PKEY_new\n");
  18034. ret = ERR_BASE_PKEY-13;
  18035. goto openssl_pkey0_test_done;
  18036. }
  18037. ret = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  18038. ret += wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  18039. if(ret != 2){
  18040. printf("error with PKEY_set1_RSA\n");
  18041. ret = ERR_BASE_PKEY-14;
  18042. goto openssl_pkey0_test_done;
  18043. }
  18044. dec = EVP_PKEY_CTX_new(prvPkey, NULL);
  18045. enc = EVP_PKEY_CTX_new(pubPkey, NULL);
  18046. if((dec == NULL)||(enc==NULL)){
  18047. printf("error with EVP_PKEY_CTX_new\n");
  18048. ret = ERR_BASE_PKEY-15;
  18049. goto openssl_pkey0_test_done;
  18050. }
  18051. ret = EVP_PKEY_decrypt_init(dec);
  18052. if (ret != 1) {
  18053. printf("error with decrypt init\n");
  18054. ret = ERR_BASE_PKEY-16;
  18055. goto openssl_pkey0_test_done;
  18056. }
  18057. ret = EVP_PKEY_encrypt_init(enc);
  18058. if (ret != 1) {
  18059. printf("error with encrypt init\n");
  18060. ret = ERR_BASE_PKEY-17;
  18061. goto openssl_pkey0_test_done;
  18062. }
  18063. XMEMSET(out, 0, sizeof(out));
  18064. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  18065. if (ret != 1) {
  18066. printf("error encrypting msg\n");
  18067. ret = ERR_BASE_PKEY-18;
  18068. goto openssl_pkey0_test_done;
  18069. }
  18070. show("encrypted msg", out, outlen);
  18071. XMEMSET(plain, 0, sizeof(plain));
  18072. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  18073. if (ret != 1) {
  18074. printf("error decrypting msg\n");
  18075. ret = ERR_BASE_PKEY-19;
  18076. goto openssl_pkey0_test_done;
  18077. }
  18078. show("decrypted msg", plain, outlen);
  18079. /* RSA_PKCS1_OAEP_PADDING test */
  18080. ret = EVP_PKEY_decrypt_init(dec);
  18081. if (ret != 1) {
  18082. printf("error with decrypt init\n");
  18083. ret = ERR_BASE_PKEY-30;
  18084. goto openssl_pkey0_test_done;
  18085. }
  18086. ret = EVP_PKEY_encrypt_init(enc);
  18087. if (ret != 1) {
  18088. printf("error with encrypt init\n");
  18089. ret = ERR_BASE_PKEY-31;
  18090. goto openssl_pkey0_test_done;
  18091. }
  18092. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  18093. printf("first set rsa padding error\n");
  18094. ret = ERR_BASE_PKEY-32;
  18095. goto openssl_pkey0_test_done;
  18096. }
  18097. #ifndef HAVE_FIPS
  18098. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  18099. printf("second set rsa padding error\n");
  18100. ret = ERR_BASE_PKEY-33;
  18101. goto openssl_pkey0_test_done;
  18102. }
  18103. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  18104. printf("third set rsa padding error\n");
  18105. ret = ERR_BASE_PKEY-34;
  18106. goto openssl_pkey0_test_done;
  18107. }
  18108. #endif
  18109. XMEMSET(out, 0, sizeof(out));
  18110. ret = EVP_PKEY_encrypt(enc, out, &outlen, in, sizeof(in));
  18111. if (ret != 1) {
  18112. printf("error encrypting msg\n");
  18113. ret = ERR_BASE_PKEY-35;
  18114. goto openssl_pkey0_test_done;
  18115. }
  18116. show("encrypted msg", out, outlen);
  18117. XMEMSET(plain, 0, sizeof(plain));
  18118. ret = EVP_PKEY_decrypt(dec, plain, &outlen, out, keySz);
  18119. if (ret != 1) {
  18120. printf("error decrypting msg\n");
  18121. ret = ERR_BASE_PKEY-36;
  18122. goto openssl_pkey0_test_done;
  18123. }
  18124. show("decrypted msg", plain, outlen);
  18125. ret = 0; /* made it to this point without error then set success */
  18126. openssl_pkey0_test_done:
  18127. wolfSSL_RSA_free(prvRsa);
  18128. wolfSSL_RSA_free(pubRsa);
  18129. EVP_PKEY_free(pubPkey);
  18130. EVP_PKEY_free(prvPkey);
  18131. EVP_PKEY_CTX_free(dec);
  18132. EVP_PKEY_CTX_free(enc);
  18133. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18134. XFREE(pubTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18135. #endif /* NO_RSA */
  18136. return ret;
  18137. }
  18138. WOLFSSL_TEST_SUBROUTINE int openssl_pkey1_test(void)
  18139. {
  18140. int ret = 0;
  18141. #if !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  18142. !defined(NO_SHA)
  18143. EVP_PKEY_CTX* dec = NULL;
  18144. EVP_PKEY_CTX* enc = NULL;
  18145. EVP_PKEY* pubKey = NULL;
  18146. EVP_PKEY* prvKey = NULL;
  18147. X509* x509 = NULL;
  18148. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sugar slapped";
  18149. const unsigned char* clikey;
  18150. long cliKeySz;
  18151. size_t outlen;
  18152. int keyLenBits = 2048;
  18153. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18154. unsigned char *tmp = (unsigned char *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18155. unsigned char *cipher = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18156. unsigned char *plain = (unsigned char *)XMALLOC(RSA_TEST_BYTES, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18157. if ((tmp == NULL) ||
  18158. (cipher == NULL) ||
  18159. (plain == NULL)) {
  18160. ret = -9015;
  18161. goto openssl_pkey1_test_done;
  18162. }
  18163. #else
  18164. unsigned char tmp[FOURK_BUF];
  18165. unsigned char cipher[RSA_TEST_BYTES];
  18166. unsigned char plain[RSA_TEST_BYTES];
  18167. #endif
  18168. #if defined(USE_CERT_BUFFERS_1024)
  18169. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  18170. cliKeySz = (long)sizeof_client_key_der_1024;
  18171. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_1024,
  18172. sizeof_client_cert_der_1024, SSL_FILETYPE_ASN1);
  18173. keyLenBits = 1024;
  18174. #elif defined(USE_CERT_BUFFERS_2048)
  18175. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  18176. cliKeySz = (long)sizeof_client_key_der_2048;
  18177. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_2048,
  18178. sizeof_client_cert_der_2048, SSL_FILETYPE_ASN1);
  18179. #elif defined(USE_CERT_BUFFERS_3072)
  18180. XMEMCPY(tmp, client_key_der_3072, sizeof_client_key_der_3072);
  18181. cliKeySz = (long)sizeof_client_key_der_3072;
  18182. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_3072,
  18183. sizeof_client_cert_der_3072, SSL_FILETYPE_ASN1);
  18184. keyLenBits = 3072;
  18185. #elif defined(USE_CERT_BUFFERS_4096)
  18186. XMEMCPY(tmp, client_key_der_4096, sizeof_client_key_der_4096);
  18187. cliKeySz = (long)sizeof_client_key_der_4096;
  18188. x509 = wolfSSL_X509_load_certificate_buffer(client_cert_der_4096,
  18189. sizeof_client_cert_der_4096, SSL_FILETYPE_ASN1);
  18190. keyLenBits = 4096;
  18191. #else
  18192. {
  18193. XFILE f;
  18194. f = XFOPEN(clientKey, "rb");
  18195. if (!f) {
  18196. err_sys("can't open ./certs/client-key.der, "
  18197. "Please run from wolfSSL home dir", -41);
  18198. ret = -9000;
  18199. goto openssl_pkey1_test_done;
  18200. }
  18201. cliKeySz = (long)XFREAD(tmp, 1, FOURK_BUF, f);
  18202. XFCLOSE(f);
  18203. }
  18204. /* using existing wolfSSL api to get public and private key */
  18205. x509 = wolfSSL_X509_load_certificate_file(clientCert, SSL_FILETYPE_ASN1);
  18206. #endif /* USE_CERT_BUFFERS */
  18207. clikey = tmp;
  18208. if ((prvKey = EVP_PKEY_new()) == NULL) {
  18209. ret = -9001;
  18210. goto openssl_pkey1_test_done;
  18211. }
  18212. EVP_PKEY_free(prvKey);
  18213. prvKey = NULL;
  18214. if (x509 == NULL) {
  18215. ret = -9002;
  18216. goto openssl_pkey1_test_done;
  18217. }
  18218. pubKey = X509_get_pubkey(x509);
  18219. if (pubKey == NULL) {
  18220. ret = -9003;
  18221. goto openssl_pkey1_test_done;
  18222. }
  18223. prvKey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &clikey, cliKeySz);
  18224. if (prvKey == NULL) {
  18225. ret = -9004;
  18226. goto openssl_pkey1_test_done;
  18227. }
  18228. /* phase 2 API to create EVP_PKEY_CTX and encrypt/decrypt */
  18229. if (EVP_PKEY_bits(prvKey) != keyLenBits) {
  18230. ret = -9005;
  18231. goto openssl_pkey1_test_done;
  18232. }
  18233. if (EVP_PKEY_size(prvKey) != keyLenBits/8) {
  18234. ret = -9006;
  18235. goto openssl_pkey1_test_done;
  18236. }
  18237. dec = EVP_PKEY_CTX_new(prvKey, NULL);
  18238. enc = EVP_PKEY_CTX_new(pubKey, NULL);
  18239. if (dec == NULL || enc == NULL) {
  18240. ret = -9007;
  18241. goto openssl_pkey1_test_done;
  18242. }
  18243. if (EVP_PKEY_decrypt_init(dec) != 1) {
  18244. ret = -9008;
  18245. goto openssl_pkey1_test_done;
  18246. }
  18247. if (EVP_PKEY_encrypt_init(enc) != 1) {
  18248. ret = -9009;
  18249. goto openssl_pkey1_test_done;
  18250. }
  18251. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_PADDING) <= 0) {
  18252. ret = -9010;
  18253. goto openssl_pkey1_test_done;
  18254. }
  18255. #ifndef HAVE_FIPS
  18256. if (EVP_PKEY_CTX_set_rsa_padding(dec, RSA_PKCS1_OAEP_PADDING) <= 0){
  18257. ret = -9011;
  18258. goto openssl_pkey1_test_done;
  18259. }
  18260. if (EVP_PKEY_CTX_set_rsa_padding(enc, RSA_PKCS1_OAEP_PADDING) <= 0) {
  18261. ret = -9012;
  18262. goto openssl_pkey1_test_done;
  18263. }
  18264. #endif
  18265. XMEMSET(cipher, 0, RSA_TEST_BYTES);
  18266. outlen = keyLenBits/8;
  18267. if (EVP_PKEY_encrypt(enc, cipher, &outlen, msg, sizeof(msg)) < 0) {
  18268. ret = -9013;
  18269. goto openssl_pkey1_test_done;
  18270. }
  18271. XMEMSET(plain, 0, RSA_TEST_BYTES);
  18272. if (EVP_PKEY_decrypt(dec, plain, &outlen, cipher, outlen) != 1) {
  18273. ret = -9014;
  18274. goto openssl_pkey1_test_done;
  18275. }
  18276. openssl_pkey1_test_done:
  18277. if (pubKey != NULL) {
  18278. EVP_PKEY_free(pubKey);
  18279. }
  18280. if (prvKey != NULL) {
  18281. EVP_PKEY_free(prvKey);
  18282. }
  18283. if (dec != NULL) {
  18284. EVP_PKEY_CTX_free(dec);
  18285. }
  18286. if (enc != NULL) {
  18287. EVP_PKEY_CTX_free(enc);
  18288. }
  18289. if (x509 != NULL) {
  18290. X509_free(x509);
  18291. }
  18292. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  18293. if (tmp != NULL)
  18294. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18295. if (cipher != NULL)
  18296. XFREE(cipher, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18297. if (plain != NULL)
  18298. XFREE(plain, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18299. #endif
  18300. #endif
  18301. return ret;
  18302. }
  18303. #define ERR_BASE_EVPSIG (-5100)
  18304. WOLFSSL_TEST_SUBROUTINE int openssl_evpSig_test(void)
  18305. {
  18306. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(HAVE_USER_RSA)
  18307. byte* prvTmp;
  18308. byte* pubTmp;
  18309. int prvBytes;
  18310. int pubBytes;
  18311. RSA *prvRsa;
  18312. RSA *pubRsa;
  18313. EVP_PKEY *prvPkey;
  18314. EVP_PKEY *pubPkey;
  18315. EVP_MD_CTX* sign;
  18316. EVP_MD_CTX* verf;
  18317. char msg[] = "see spot run";
  18318. unsigned char sig[256];
  18319. unsigned int sigSz;
  18320. const void* pt;
  18321. unsigned int count;
  18322. int ret, ret1, ret2;
  18323. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  18324. XFILE keyFile;
  18325. XFILE keypubFile;
  18326. char cliKey[] = "./certs/client-key.der";
  18327. char cliKeypub[] = "./certs/client-keyPub.der";
  18328. #endif
  18329. prvTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18330. if (prvTmp == NULL)
  18331. return ERR_BASE_EVPSIG-1;
  18332. pubTmp = (byte*)XMALLOC(FOURK_BUFF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18333. if (pubTmp == NULL) {
  18334. XFREE(prvTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  18335. return ERR_BASE_EVPSIG-2;
  18336. }
  18337. #ifdef USE_CERT_BUFFERS_1024
  18338. XMEMCPY(prvTmp, client_key_der_1024, sizeof_client_key_der_1024);
  18339. prvBytes = sizeof_client_key_der_1024;
  18340. XMEMCPY(pubTmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  18341. pubBytes = sizeof_client_keypub_der_1024;
  18342. #elif defined(USE_CERT_BUFFERS_2048)
  18343. XMEMCPY(prvTmp, client_key_der_2048, sizeof_client_key_der_2048);
  18344. prvBytes = sizeof_client_key_der_2048;
  18345. XMEMCPY(pubTmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  18346. pubBytes = sizeof_client_keypub_der_2048;
  18347. #else
  18348. keyFile = XFOPEN(cliKey, "rb");
  18349. if (!keyFile) {
  18350. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18351. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18352. err_sys("can't open ./certs/client-key.der, "
  18353. "Please run from wolfSSL home dir", -40);
  18354. return ERR_BASE_EVPSIG-3;
  18355. }
  18356. prvBytes = (int)XFREAD(prvTmp, 1, (int)FOURK_BUFF, keyFile);
  18357. XFCLOSE(keyFile);
  18358. keypubFile = XFOPEN(cliKeypub, "rb");
  18359. if (!keypubFile) {
  18360. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18361. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18362. err_sys("can't open ./certs/client-cert.der, "
  18363. "Please run from wolfSSL home dir", -41);
  18364. return ERR_BASE_EVPSIG-4;
  18365. }
  18366. pubBytes = (int)XFREAD(pubTmp, 1, (int)FOURK_BUFF, keypubFile);
  18367. XFCLOSE(keypubFile);
  18368. #endif /* USE_CERT_BUFFERS */
  18369. prvRsa = wolfSSL_RSA_new();
  18370. pubRsa = wolfSSL_RSA_new();
  18371. if((prvRsa == NULL) || (pubRsa == NULL)){
  18372. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18373. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18374. err_sys("ERROR with RSA_new", -9100);
  18375. return ERR_BASE_EVPSIG-5;
  18376. }
  18377. ret1 = wolfSSL_RSA_LoadDer_ex(prvRsa, prvTmp, prvBytes, WOLFSSL_RSA_LOAD_PRIVATE);
  18378. ret2 = wolfSSL_RSA_LoadDer_ex(pubRsa, pubTmp, pubBytes, WOLFSSL_RSA_LOAD_PUBLIC);
  18379. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  18380. printf("error with RSA_LoadDer_ex\n");
  18381. return ERR_BASE_EVPSIG-6;
  18382. }
  18383. prvPkey = wolfSSL_EVP_PKEY_new();
  18384. pubPkey = wolfSSL_EVP_PKEY_new();
  18385. if((prvPkey == NULL) || (pubPkey == NULL)){
  18386. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18387. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18388. printf("error with KEY_new\n");
  18389. return ERR_BASE_EVPSIG-7;
  18390. }
  18391. ret1 = wolfSSL_EVP_PKEY_set1_RSA(prvPkey, prvRsa);
  18392. ret2 = wolfSSL_EVP_PKEY_set1_RSA(pubPkey, pubRsa);
  18393. if((ret1 != 1) || (ret2 != 1)){
  18394. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18395. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18396. printf("error with EVP_PKEY_set1_RSA\n");
  18397. return ERR_BASE_EVPSIG-8;
  18398. }
  18399. /****************** sign and verify *******************/
  18400. sign = EVP_MD_CTX_create();
  18401. verf = EVP_MD_CTX_create();
  18402. if((sign == NULL)||(verf == NULL)){
  18403. printf("error with EVP_MD_CTX_create\n");
  18404. EVP_MD_CTX_destroy(sign);
  18405. EVP_MD_CTX_destroy(verf);
  18406. return ERR_BASE_EVPSIG-10;
  18407. }
  18408. ret = EVP_SignInit(sign, EVP_sha1());
  18409. if (ret != SSL_SUCCESS){
  18410. printf("error with EVP_SignInit\n");
  18411. EVP_MD_CTX_destroy(sign);
  18412. EVP_MD_CTX_destroy(verf);
  18413. return ERR_BASE_EVPSIG-11;
  18414. }
  18415. count = sizeof(msg);
  18416. show("message = ", (char *)msg, count);
  18417. /* sign */
  18418. XMEMSET(sig, 0, sizeof(sig));
  18419. pt = (const void*)msg;
  18420. ret1 = EVP_SignUpdate(sign, pt, count);
  18421. ret2 = EVP_SignFinal(sign, sig, &sigSz, prvPkey);
  18422. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  18423. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18424. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18425. EVP_MD_CTX_destroy(sign);
  18426. EVP_MD_CTX_destroy(verf);
  18427. printf("error with EVP_MD_CTX_create\n");
  18428. return ERR_BASE_EVPSIG-12;
  18429. }
  18430. show("signature = ", (char *)sig, sigSz);
  18431. /* verify */
  18432. pt = (const void*)msg;
  18433. ret1 = EVP_VerifyInit(verf, EVP_sha1());
  18434. ret2 = EVP_VerifyUpdate(verf, pt, count);
  18435. if((ret1 != SSL_SUCCESS) || (ret2 != SSL_SUCCESS)){
  18436. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18437. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18438. EVP_MD_CTX_destroy(sign);
  18439. EVP_MD_CTX_destroy(verf);
  18440. printf("error with EVP_Verify\n");
  18441. return ERR_BASE_EVPSIG-13;
  18442. }
  18443. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) != 1) {
  18444. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18445. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18446. EVP_MD_CTX_destroy(sign);
  18447. EVP_MD_CTX_destroy(verf);
  18448. printf("error with EVP_VerifyFinal\n");
  18449. return ERR_BASE_EVPSIG-14;
  18450. }
  18451. /* expect fail without update */
  18452. EVP_VerifyInit(verf, EVP_sha1());
  18453. if (EVP_VerifyFinal(verf, sig, sigSz, pubPkey) == 1) {
  18454. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18455. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18456. EVP_MD_CTX_destroy(sign);
  18457. EVP_MD_CTX_destroy(verf);
  18458. printf("EVP_VerifyInit without update not detected\n");
  18459. return ERR_BASE_EVPSIG-15;
  18460. }
  18461. XFREE(pubTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18462. XFREE(prvTmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  18463. EVP_MD_CTX_destroy(sign);
  18464. EVP_MD_CTX_destroy(verf);
  18465. wolfSSL_RSA_free(prvRsa);
  18466. wolfSSL_RSA_free(pubRsa);
  18467. EVP_PKEY_free(pubPkey);
  18468. EVP_PKEY_free(prvPkey);
  18469. #endif /* NO_RSA */
  18470. return 0;
  18471. }
  18472. #endif /* OPENSSL_EXTRA */
  18473. #ifndef NO_PWDBASED
  18474. #ifdef HAVE_SCRYPT
  18475. /* Test vectors taken from RFC 7914: scrypt PBKDF - Section 12. */
  18476. WOLFSSL_TEST_SUBROUTINE int scrypt_test(void)
  18477. {
  18478. #ifdef HAVE_FIPS
  18479. /* RFC 7914 test vector keys are too short for FIPS. */
  18480. #else
  18481. int ret;
  18482. byte derived[64];
  18483. WOLFSSL_SMALL_STACK_STATIC const byte verify1[] = {
  18484. 0x77, 0xd6, 0x57, 0x62, 0x38, 0x65, 0x7b, 0x20,
  18485. 0x3b, 0x19, 0xca, 0x42, 0xc1, 0x8a, 0x04, 0x97,
  18486. 0xf1, 0x6b, 0x48, 0x44, 0xe3, 0x07, 0x4a, 0xe8,
  18487. 0xdf, 0xdf, 0xfa, 0x3f, 0xed, 0xe2, 0x14, 0x42,
  18488. 0xfc, 0xd0, 0x06, 0x9d, 0xed, 0x09, 0x48, 0xf8,
  18489. 0x32, 0x6a, 0x75, 0x3a, 0x0f, 0xc8, 0x1f, 0x17,
  18490. 0xe8, 0xd3, 0xe0, 0xfb, 0x2e, 0x0d, 0x36, 0x28,
  18491. 0xcf, 0x35, 0xe2, 0x0c, 0x38, 0xd1, 0x89, 0x06
  18492. };
  18493. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  18494. 0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00,
  18495. 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe,
  18496. 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30,
  18497. 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62,
  18498. 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88,
  18499. 0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda,
  18500. 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d,
  18501. 0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40
  18502. };
  18503. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  18504. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  18505. 0x70, 0x23, 0xbd, 0xcb, 0x3a, 0xfd, 0x73, 0x48,
  18506. 0x46, 0x1c, 0x06, 0xcd, 0x81, 0xfd, 0x38, 0xeb,
  18507. 0xfd, 0xa8, 0xfb, 0xba, 0x90, 0x4f, 0x8e, 0x3e,
  18508. 0xa9, 0xb5, 0x43, 0xf6, 0x54, 0x5d, 0xa1, 0xf2,
  18509. 0xd5, 0x43, 0x29, 0x55, 0x61, 0x3f, 0x0f, 0xcf,
  18510. 0x62, 0xd4, 0x97, 0x05, 0x24, 0x2a, 0x9a, 0xf9,
  18511. 0xe6, 0x1e, 0x85, 0xdc, 0x0d, 0x65, 0x1e, 0x40,
  18512. 0xdf, 0xcf, 0x01, 0x7b, 0x45, 0x57, 0x58, 0x87
  18513. };
  18514. #endif
  18515. #ifdef SCRYPT_TEST_ALL
  18516. /* Test case is very slow.
  18517. * Use for confirmation after code change or new platform.
  18518. */
  18519. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  18520. 0x21, 0x01, 0xcb, 0x9b, 0x6a, 0x51, 0x1a, 0xae,
  18521. 0xad, 0xdb, 0xbe, 0x09, 0xcf, 0x70, 0xf8, 0x81,
  18522. 0xec, 0x56, 0x8d, 0x57, 0x4a, 0x2f, 0xfd, 0x4d,
  18523. 0xab, 0xe5, 0xee, 0x98, 0x20, 0xad, 0xaa, 0x47,
  18524. 0x8e, 0x56, 0xfd, 0x8f, 0x4b, 0xa5, 0xd0, 0x9f,
  18525. 0xfa, 0x1c, 0x6d, 0x92, 0x7c, 0x40, 0xf4, 0xc3,
  18526. 0x37, 0x30, 0x40, 0x49, 0xe8, 0xa9, 0x52, 0xfb,
  18527. 0xcb, 0xf4, 0x5c, 0x6f, 0xa7, 0x7a, 0x41, 0xa4
  18528. };
  18529. #endif
  18530. ret = wc_scrypt(derived, NULL, 0, NULL, 0, 4, 1, 1, sizeof(verify1));
  18531. if (ret != 0)
  18532. return -9200;
  18533. if (XMEMCMP(derived, verify1, sizeof(verify1)) != 0)
  18534. return -9201;
  18535. ret = wc_scrypt(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 10, 8, 16,
  18536. sizeof(verify2));
  18537. if (ret != 0)
  18538. return -9202;
  18539. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  18540. return -9203;
  18541. /* Test case with parallel overflowing */
  18542. ret = wc_scrypt(derived, (byte*)"password", 16, (byte*)"NaCl", 16, 2, 4, 8388608,
  18543. sizeof(verify2));
  18544. if (ret != BAD_FUNC_ARG)
  18545. return -9210;
  18546. /* Don't run these test on embedded, since they use large mallocs */
  18547. #if !defined(BENCH_EMBEDDED) && !defined(WOLFSSL_LINUXKM) && !defined(HAVE_INTEL_QA)
  18548. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  18549. (byte*)"SodiumChloride", 14, 14, 8, 1, sizeof(verify3));
  18550. if (ret != 0)
  18551. return -9204;
  18552. if (XMEMCMP(derived, verify3, sizeof(verify3)) != 0)
  18553. return -9205;
  18554. #ifdef SCRYPT_TEST_ALL
  18555. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  18556. (byte*)"SodiumChloride", 14, 20, 8, 1, sizeof(verify4));
  18557. if (ret != 0)
  18558. return -9206;
  18559. if (XMEMCMP(derived, verify4, sizeof(verify4)) != 0)
  18560. return -9207;
  18561. #endif
  18562. #endif /* !BENCH_EMBEDDED && !defined(WOLFSSL_LINUXKM) && !HAVE_INTEL_QA */
  18563. ret = wc_scrypt_ex(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 1<<10,
  18564. 8, 16, sizeof(verify2));
  18565. if (ret != 0)
  18566. return -9208;
  18567. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  18568. return -9209;
  18569. #endif /* !HAVE_FIPS */
  18570. return 0;
  18571. }
  18572. #endif
  18573. #ifdef HAVE_PKCS12
  18574. WOLFSSL_TEST_SUBROUTINE int pkcs12_test(void)
  18575. {
  18576. WOLFSSL_SMALL_STACK_STATIC const byte passwd[] = { 0x00, 0x73, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x67,
  18577. 0x00, 0x00 };
  18578. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x0a, 0x58, 0xCF, 0x64, 0x53, 0x0d, 0x82, 0x3f };
  18579. WOLFSSL_SMALL_STACK_STATIC const byte passwd2[] = { 0x00, 0x71, 0x00, 0x75, 0x00, 0x65, 0x00, 0x65,
  18580. 0x00, 0x67, 0x00, 0x00 };
  18581. WOLFSSL_SMALL_STACK_STATIC const byte salt2[] = { 0x16, 0x82, 0xC0, 0xfC, 0x5b, 0x3f, 0x7e, 0xc5 };
  18582. byte derived[64];
  18583. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  18584. 0x27, 0xE9, 0x0D, 0x7E, 0xD5, 0xA1, 0xC4, 0x11,
  18585. 0xBA, 0x87, 0x8B, 0xC0, 0x90, 0xF5, 0xCE, 0xBE,
  18586. 0x5E, 0x9D, 0x5F, 0xE3, 0xD6, 0x2B, 0x73, 0xAA
  18587. };
  18588. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  18589. 0x90, 0x1B, 0x49, 0x70, 0xF0, 0x94, 0xF0, 0xF8,
  18590. 0x45, 0xC0, 0xF3, 0xF3, 0x13, 0x59, 0x18, 0x6A,
  18591. 0x35, 0xE3, 0x67, 0xFE, 0xD3, 0x21, 0xFD, 0x7C
  18592. };
  18593. int id = 1;
  18594. int kLen = 24;
  18595. int iterations = 1;
  18596. int ret = wc_PKCS12_PBKDF(derived, passwd, sizeof(passwd), salt, 8,
  18597. iterations, kLen, WC_SHA256, id);
  18598. if (ret < 0)
  18599. return -9300;
  18600. if (XMEMCMP(derived, verify, kLen) != 0)
  18601. return -9301;
  18602. iterations = 1000;
  18603. ret = wc_PKCS12_PBKDF(derived, passwd2, sizeof(passwd2), salt2, 8,
  18604. iterations, kLen, WC_SHA256, id);
  18605. if (ret < 0)
  18606. return -9302;
  18607. ret = wc_PKCS12_PBKDF_ex(derived, passwd2, sizeof(passwd2), salt2, 8,
  18608. iterations, kLen, WC_SHA256, id, HEAP_HINT);
  18609. if (ret < 0)
  18610. return -9303;
  18611. if (XMEMCMP(derived, verify2, 24) != 0)
  18612. return -9304;
  18613. return 0;
  18614. }
  18615. #endif /* HAVE_PKCS12 */
  18616. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  18617. WOLFSSL_TEST_SUBROUTINE int pbkdf2_test(void)
  18618. {
  18619. char passwd[] = "passwordpassword";
  18620. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  18621. int iterations = 2048;
  18622. int kLen = 24;
  18623. byte derived[64];
  18624. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  18625. 0x43, 0x6d, 0xb5, 0xe8, 0xd0, 0xfb, 0x3f, 0x35, 0x42, 0x48, 0x39, 0xbc,
  18626. 0x2d, 0xd4, 0xf9, 0x37, 0xd4, 0x95, 0x16, 0xa7, 0x2a, 0x9a, 0x21, 0xd1
  18627. };
  18628. int ret = wc_PBKDF2_ex(derived, (byte*)passwd, (int)XSTRLEN(passwd), salt,
  18629. (int)sizeof(salt), iterations, kLen, WC_SHA256, HEAP_HINT, devId);
  18630. if (ret != 0)
  18631. return ret;
  18632. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  18633. return -9400;
  18634. return 0;
  18635. }
  18636. #endif /* HAVE_PBKDF2 && !NO_SHA256 && !NO_HMAC */
  18637. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  18638. WOLFSSL_TEST_SUBROUTINE int pbkdf1_test(void)
  18639. {
  18640. char passwd[] = "password";
  18641. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  18642. int iterations = 1000;
  18643. int kLen = 16;
  18644. byte derived[16];
  18645. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  18646. 0xDC, 0x19, 0x84, 0x7E, 0x05, 0xC6, 0x4D, 0x2F,
  18647. 0xAF, 0x10, 0xEB, 0xFB, 0x4A, 0x3D, 0x2A, 0x20
  18648. };
  18649. int ret = wc_PBKDF1_ex(derived, kLen, NULL, 0, (byte*)passwd,
  18650. (int)XSTRLEN(passwd), salt, (int)sizeof(salt), iterations, WC_SHA,
  18651. HEAP_HINT);
  18652. if (ret != 0)
  18653. return ret;
  18654. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  18655. return -9500;
  18656. return 0;
  18657. }
  18658. #endif /* HAVE_PBKDF2 && !NO_SHA */
  18659. WOLFSSL_TEST_SUBROUTINE int pwdbased_test(void)
  18660. {
  18661. int ret = 0;
  18662. #if defined(HAVE_PBKDF1) && !defined(NO_SHA)
  18663. ret = pbkdf1_test();
  18664. if (ret != 0)
  18665. return ret;
  18666. #endif
  18667. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
  18668. ret = pbkdf2_test();
  18669. if (ret != 0)
  18670. return ret;
  18671. #endif
  18672. #ifdef HAVE_PKCS12
  18673. ret = pkcs12_test();
  18674. if (ret != 0)
  18675. return ret;
  18676. #endif
  18677. #ifdef HAVE_SCRYPT
  18678. ret = scrypt_test();
  18679. if (ret != 0)
  18680. return ret;
  18681. #endif
  18682. return ret;
  18683. }
  18684. #endif /* NO_PWDBASED */
  18685. #if defined(HAVE_HKDF) && !defined(NO_HMAC)
  18686. /* hkdf_test has issue with WOLFSSL_TEST_SUBROUTINE set on Xilinx with afalg */
  18687. static int hkdf_test(void)
  18688. {
  18689. int ret = 0;
  18690. #if !defined(NO_SHA) || !defined(NO_SHA256)
  18691. int L;
  18692. byte okm1[42];
  18693. byte ikm1[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  18694. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  18695. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  18696. #ifndef HAVE_FIPS
  18697. byte salt1[13] ={ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18698. 0x08, 0x09, 0x0a, 0x0b, 0x0c };
  18699. byte info1[10] ={ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  18700. 0xf8, 0xf9 };
  18701. #endif
  18702. #ifndef NO_SHA
  18703. byte res1[42] = { 0x0a, 0xc1, 0xaf, 0x70, 0x02, 0xb3, 0xd7, 0x61,
  18704. 0xd1, 0xe5, 0x52, 0x98, 0xda, 0x9d, 0x05, 0x06,
  18705. 0xb9, 0xae, 0x52, 0x05, 0x72, 0x20, 0xa3, 0x06,
  18706. 0xe0, 0x7b, 0x6b, 0x87, 0xe8, 0xdf, 0x21, 0xd0,
  18707. 0xea, 0x00, 0x03, 0x3d, 0xe0, 0x39, 0x84, 0xd3,
  18708. 0x49, 0x18 };
  18709. #ifndef HAVE_FIPS
  18710. byte res2[42] = { 0x08, 0x5a, 0x01, 0xea, 0x1b, 0x10, 0xf3, 0x69,
  18711. 0x33, 0x06, 0x8b, 0x56, 0xef, 0xa5, 0xad, 0x81,
  18712. 0xa4, 0xf1, 0x4b, 0x82, 0x2f, 0x5b, 0x09, 0x15,
  18713. 0x68, 0xa9, 0xcd, 0xd4, 0xf1, 0x55, 0xfd, 0xa2,
  18714. 0xc2, 0x2e, 0x42, 0x24, 0x78, 0xd3, 0x05, 0xf3,
  18715. 0xf8, 0x96 };
  18716. #endif
  18717. #endif /* !NO_SHA */
  18718. #ifndef NO_SHA256
  18719. byte res3[42] = { 0x8d, 0xa4, 0xe7, 0x75, 0xa5, 0x63, 0xc1, 0x8f,
  18720. 0x71, 0x5f, 0x80, 0x2a, 0x06, 0x3c, 0x5a, 0x31,
  18721. 0xb8, 0xa1, 0x1f, 0x5c, 0x5e, 0xe1, 0x87, 0x9e,
  18722. 0xc3, 0x45, 0x4e, 0x5f, 0x3c, 0x73, 0x8d, 0x2d,
  18723. 0x9d, 0x20, 0x13, 0x95, 0xfa, 0xa4, 0xb6, 0x1a,
  18724. 0x96, 0xc8 };
  18725. #ifndef HAVE_FIPS
  18726. byte res4[42] = { 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a,
  18727. 0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a,
  18728. 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c,
  18729. 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf,
  18730. 0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18,
  18731. 0x58, 0x65 };
  18732. #endif
  18733. #endif /* !NO_SHA256 */
  18734. XMEMSET(okm1, 0, sizeof(okm1));
  18735. L = (int)sizeof(okm1);
  18736. #ifndef NO_SHA
  18737. ret = wc_HKDF(WC_SHA, ikm1, (word32)sizeof(ikm1), NULL, 0, NULL, 0,
  18738. okm1, L);
  18739. if (ret != 0)
  18740. return -9700;
  18741. if (XMEMCMP(okm1, res1, L) != 0)
  18742. return -9701;
  18743. #ifndef HAVE_FIPS
  18744. /* fips can't have key size under 14 bytes, salt is key too */
  18745. L = (int)sizeof(okm1);
  18746. ret = wc_HKDF(WC_SHA, ikm1, 11, salt1, (word32)sizeof(salt1),
  18747. info1, (word32)sizeof(info1), okm1, L);
  18748. if (ret != 0)
  18749. return -9702;
  18750. if (XMEMCMP(okm1, res2, L) != 0)
  18751. return -9703;
  18752. #endif /* HAVE_FIPS */
  18753. #endif /* !NO_SHA */
  18754. #ifndef NO_SHA256
  18755. ret = wc_HKDF(WC_SHA256, ikm1, (word32)sizeof(ikm1), NULL, 0, NULL, 0,
  18756. okm1, L);
  18757. if (ret != 0)
  18758. return -9704;
  18759. if (XMEMCMP(okm1, res3, L) != 0)
  18760. return -9705;
  18761. #ifndef HAVE_FIPS
  18762. /* fips can't have key size under 14 bytes, salt is key too */
  18763. ret = wc_HKDF(WC_SHA256, ikm1, (word32)sizeof(ikm1),
  18764. salt1, (word32)sizeof(salt1), info1, (word32)sizeof(info1), okm1, L);
  18765. if (ret != 0)
  18766. return -9706;
  18767. if (XMEMCMP(okm1, res4, L) != 0)
  18768. return -9707;
  18769. #endif /* HAVE_FIPS */
  18770. #endif /* !NO_SHA256 */
  18771. #endif /* !NO_SHA || !NO_SHA256 */
  18772. return ret;
  18773. }
  18774. #endif /* HAVE_HKDF */
  18775. #ifdef WOLFSSL_WOLFSSH
  18776. typedef struct {
  18777. byte hashId;
  18778. byte keyId;
  18779. const byte* k;
  18780. word32 kSz;
  18781. const byte* h;
  18782. word32 hSz;
  18783. const byte* sessionId;
  18784. word32 sessionIdSz;
  18785. const byte* expectedKey;
  18786. word32 expectedKeySz;
  18787. } SshKdfTestVector;
  18788. /** Test Vector Set #3: SHA-256 **/
  18789. static const byte sshKdfTvSet3k[] = {
  18790. 0x6A, 0xC3, 0x82, 0xEA, 0xAC, 0xA0, 0x93, 0xE1,
  18791. 0x25, 0xE2, 0x5C, 0x24, 0xBE, 0xBC, 0x84, 0x64,
  18792. 0x0C, 0x11, 0x98, 0x75, 0x07, 0x34, 0x4B, 0x5C,
  18793. 0x73, 0x9C, 0xEB, 0x84, 0xA9, 0xE0, 0xB2, 0x22,
  18794. 0xB9, 0xA8, 0xB5, 0x1C, 0x83, 0x9E, 0x5E, 0xBE,
  18795. 0x49, 0xCF, 0xAD, 0xBF, 0xB3, 0x95, 0x99, 0x76,
  18796. 0x4E, 0xD5, 0x22, 0x09, 0x9D, 0xC9, 0x12, 0x75,
  18797. 0x19, 0x50, 0xDC, 0x7D, 0xC9, 0x7F, 0xBD, 0xC0,
  18798. 0x63, 0x28, 0xB6, 0x8F, 0x22, 0x78, 0x1F, 0xD3,
  18799. 0x15, 0xAF, 0x56, 0x80, 0x09, 0xA5, 0x50, 0x9E,
  18800. 0x5B, 0x87, 0xA1, 0x1B, 0xF5, 0x27, 0xC0, 0x56,
  18801. 0xDA, 0xFF, 0xD8, 0x2A, 0xB6, 0xCB, 0xC2, 0x5C,
  18802. 0xCA, 0x37, 0x14, 0x34, 0x59, 0xE7, 0xBC, 0x63,
  18803. 0xBC, 0xDE, 0x52, 0x75, 0x7A, 0xDE, 0xB7, 0xDF,
  18804. 0x01, 0xCF, 0x12, 0x17, 0x3F, 0x1F, 0xEF, 0x81,
  18805. 0x02, 0xEC, 0x5A, 0xB1, 0x42, 0xC2, 0x13, 0xDD,
  18806. 0x9D, 0x30, 0x69, 0x62, 0x78, 0xA8, 0xD8, 0xBC,
  18807. 0x32, 0xDD, 0xE9, 0x59, 0x2D, 0x28, 0xC0, 0x78,
  18808. 0xC6, 0xD9, 0x2B, 0x94, 0x7D, 0x82, 0x5A, 0xCA,
  18809. 0xAB, 0x64, 0x94, 0x84, 0x6A, 0x49, 0xDE, 0x24,
  18810. 0xB9, 0x62, 0x3F, 0x48, 0x89, 0xE8, 0xAD, 0xC3,
  18811. 0x8E, 0x8C, 0x66, 0x9E, 0xFF, 0xEF, 0x17, 0x60,
  18812. 0x40, 0xAD, 0x94, 0x5E, 0x90, 0xA7, 0xD3, 0xEE,
  18813. 0xC1, 0x5E, 0xFE, 0xEE, 0x78, 0xAE, 0x71, 0x04,
  18814. 0x3C, 0x96, 0x51, 0x11, 0x03, 0xA1, 0x6B, 0xA7,
  18815. 0xCA, 0xF0, 0xAC, 0xD0, 0x64, 0x2E, 0xFD, 0xBE,
  18816. 0x80, 0x99, 0x34, 0xFA, 0xA1, 0xA5, 0xF1, 0xBD,
  18817. 0x11, 0x04, 0x36, 0x49, 0xB2, 0x5C, 0xCD, 0x1F,
  18818. 0xEE, 0x2E, 0x38, 0x81, 0x5D, 0x4D, 0x5F, 0x5F,
  18819. 0xC6, 0xB4, 0x10, 0x29, 0x69, 0xF2, 0x1C, 0x22,
  18820. 0xAE, 0x1B, 0x0E, 0x7D, 0x36, 0x03, 0xA5, 0x56,
  18821. 0xA1, 0x32, 0x62, 0xFF, 0x62, 0x8D, 0xE2, 0x22
  18822. };
  18823. static const byte sshKdfTvSet3h[] = {
  18824. 0x7B, 0x70, 0x01, 0x18, 0x5E, 0x25, 0x6D, 0x44,
  18825. 0x93, 0x44, 0x5F, 0x39, 0xA5, 0x5F, 0xB9, 0x05,
  18826. 0xE6, 0x32, 0x1F, 0x4B, 0x5D, 0xD8, 0xBB, 0xF3,
  18827. 0x10, 0x0D, 0x51, 0xBA, 0x0B, 0xDA, 0x3D, 0x2D
  18828. };
  18829. static const byte sshKdfTvSet3sid[] = {
  18830. 0x7B, 0x70, 0x01, 0x18, 0x5E, 0x25, 0x6D, 0x44,
  18831. 0x93, 0x44, 0x5F, 0x39, 0xA5, 0x5F, 0xB9, 0x05,
  18832. 0xE6, 0x32, 0x1F, 0x4B, 0x5D, 0xD8, 0xBB, 0xF3,
  18833. 0x10, 0x0D, 0x51, 0xBA, 0x0B, 0xDA, 0x3D, 0x2D
  18834. };
  18835. static const byte sshKdfTvSet3a[] = {
  18836. 0x81, 0xF0, 0x33, 0x0E, 0xF6, 0xF0, 0x53, 0x61,
  18837. 0xB3, 0x82, 0x3B, 0xFD, 0xED, 0x6E, 0x1D, 0xE9
  18838. };
  18839. static const byte sshKdfTvSet3b[] = {
  18840. 0x3F, 0x6F, 0xD2, 0x06, 0x5E, 0xEB, 0x2B, 0x0B,
  18841. 0x1D, 0x93, 0x19, 0x5A, 0x1F, 0xED, 0x48, 0xA5
  18842. };
  18843. static const byte sshKdfTvSet3c[] = {
  18844. 0xC3, 0x54, 0x71, 0x03, 0x4E, 0x6F, 0xD6, 0x54,
  18845. 0x76, 0x13, 0x17, 0x8E, 0x23, 0x43, 0x5F, 0x21
  18846. };
  18847. static const byte sshKdfTvSet3d[] = {
  18848. 0x7E, 0x9D, 0x79, 0x03, 0x20, 0x90, 0xD9, 0x9F,
  18849. 0x98, 0xB0, 0x15, 0x63, 0x4D, 0xD9, 0xF4, 0x62
  18850. };
  18851. static const byte sshKdfTvSet3e[] = {
  18852. 0x24, 0xEE, 0x55, 0x9A, 0xD7, 0xCE, 0x71, 0x2B,
  18853. 0x68, 0x5D, 0x0B, 0x22, 0x71, 0xE4, 0x43, 0xC1,
  18854. 0x7A, 0xB1, 0xD1, 0xDC, 0xEB, 0x5A, 0x36, 0x05,
  18855. 0x69, 0xD2, 0x5D, 0x5D, 0xC2, 0x43, 0x00, 0x2F
  18856. };
  18857. static const byte sshKdfTvSet3f[] = {
  18858. 0xC3, 0x41, 0x9C, 0x2B, 0x96, 0x62, 0x35, 0x86,
  18859. 0x9D, 0x71, 0x4B, 0xA5, 0xAC, 0x48, 0xDD, 0xB7,
  18860. 0xD9, 0xE3, 0x5C, 0x8C, 0x19, 0xAA, 0xC7, 0x34,
  18861. 0x22, 0x33, 0x7A, 0x37, 0x34, 0x53, 0x60, 0x7E
  18862. };
  18863. static const SshKdfTestVector sshKdfTestVectors[] = {
  18864. {WC_HASH_TYPE_SHA256, 'A',
  18865. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  18866. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  18867. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  18868. sshKdfTvSet3a, sizeof(sshKdfTvSet3a)},
  18869. {WC_HASH_TYPE_SHA256, 'B',
  18870. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  18871. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  18872. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  18873. sshKdfTvSet3b, sizeof(sshKdfTvSet3b)},
  18874. {WC_HASH_TYPE_SHA256, 'C',
  18875. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  18876. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  18877. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  18878. sshKdfTvSet3c, sizeof(sshKdfTvSet3c)},
  18879. {WC_HASH_TYPE_SHA256, 'D',
  18880. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  18881. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  18882. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  18883. sshKdfTvSet3d, sizeof(sshKdfTvSet3d)},
  18884. {WC_HASH_TYPE_SHA256, 'E',
  18885. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  18886. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  18887. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  18888. sshKdfTvSet3e, sizeof(sshKdfTvSet3e)},
  18889. {WC_HASH_TYPE_SHA256, 'F',
  18890. sshKdfTvSet3k, sizeof(sshKdfTvSet3k),
  18891. sshKdfTvSet3h, sizeof(sshKdfTvSet3h),
  18892. sshKdfTvSet3sid, sizeof(sshKdfTvSet3sid),
  18893. sshKdfTvSet3f, sizeof(sshKdfTvSet3f)},
  18894. };
  18895. int sshkdf_test(void)
  18896. {
  18897. int result = 0;
  18898. word32 i;
  18899. word32 tc = sizeof(sshKdfTestVectors)/sizeof(SshKdfTestVector);
  18900. const SshKdfTestVector* tv = NULL;
  18901. byte cKey[32]; /* Greater of SHA256_DIGEST_SIZE and AES_BLOCK_SIZE */
  18902. /* sId - Session ID, eKey - Expected Key, cKey - Calculated Key */
  18903. for (i = 0, tv = sshKdfTestVectors; i < tc; i++, tv++) {
  18904. result = wc_SSH_KDF(tv->hashId, tv->keyId,
  18905. cKey, tv->expectedKeySz,
  18906. tv->k, tv->kSz, tv->h, tv->hSz,
  18907. tv->sessionId, tv->sessionIdSz);
  18908. if (result != 0) {
  18909. printf("KDF: Could not derive key.\n");
  18910. result = -101;
  18911. }
  18912. else {
  18913. if (memcmp(cKey, tv->expectedKey, tv->expectedKeySz) != 0) {
  18914. printf("KDF: Calculated Key does not match Expected Key.\n");
  18915. result = -102;
  18916. }
  18917. }
  18918. if (result != 0) break;
  18919. }
  18920. return result;
  18921. }
  18922. #endif /* WOLFSSL_WOLFSSH */
  18923. #ifdef WOLFSSL_TLS13
  18924. #define TLSV13_PSK_DHE_SZ 40
  18925. typedef struct {
  18926. enum wc_HashType hashAlg;
  18927. word32 pskSz;
  18928. word32 dheSz;
  18929. byte psk[TLSV13_PSK_DHE_SZ];
  18930. byte dhe[TLSV13_PSK_DHE_SZ];
  18931. byte hashHello1[WC_MAX_DIGEST_SIZE];
  18932. byte hashHello2[WC_MAX_DIGEST_SIZE];
  18933. byte hashFinished1[WC_MAX_DIGEST_SIZE];
  18934. byte hashFinished2[WC_MAX_DIGEST_SIZE];
  18935. /* Expected */
  18936. byte clientEarlyTrafficSecret[WC_MAX_DIGEST_SIZE];
  18937. byte earlyExporterMasterSecret[WC_MAX_DIGEST_SIZE];
  18938. byte clientHandshakeTrafficSecret[WC_MAX_DIGEST_SIZE];
  18939. byte serverHandshakeTrafficSecret[WC_MAX_DIGEST_SIZE];
  18940. byte clientApplicationTrafficSecret[WC_MAX_DIGEST_SIZE];
  18941. byte serverApplicationTrafficSecret[WC_MAX_DIGEST_SIZE];
  18942. byte exporterMasterSecret[WC_MAX_DIGEST_SIZE];
  18943. byte resumptionMasterSecret[WC_MAX_DIGEST_SIZE];
  18944. } Tls13KdfTestVector;
  18945. /* The following tests come from the CAVP test vectors we used for
  18946. * our FIPS validation. The hash values used are the components from
  18947. * the test hashed together. hashHello1 is the hash of the
  18948. * clientHelloRandom value of the test vector. hashHello2 is the hash
  18949. * of the clientHelloRandom and serverHelloRandom values from the test
  18950. * vector. hashFinished1 is clientHelloRandom, serverHelloRandom, and
  18951. * serverFinishedRandom. hashFinished2 is clientHelloRandom,
  18952. * serverHelloRandom, serverFinishedRandom, and clietnFinishedRandom
  18953. * hashed together. */
  18954. static const Tls13KdfTestVector tls13KdfTestVectors[] = {
  18955. { /* 1 */
  18956. WC_HASH_TYPE_SHA256, 35, 35,
  18957. { /* PSK */
  18958. 0x7b, 0xf1, 0x05, 0x31, 0x36, 0xfa, 0x03, 0xdc,
  18959. 0x31, 0x97, 0x88, 0x04, 0x9c, 0xbc, 0xee, 0xf7,
  18960. 0x8d, 0x84, 0x95, 0x26, 0xaf, 0x1d, 0x68, 0xb0,
  18961. 0x60, 0x7a, 0xcc, 0x4f, 0xc1, 0xd3, 0xa1, 0x68,
  18962. 0x7f, 0x6d, 0xbe
  18963. },
  18964. { /* DHE */
  18965. 0x6e, 0xa1, 0x77, 0xab, 0x2f, 0x43, 0xd2, 0x4b,
  18966. 0xe5, 0xa1, 0x09, 0xe0, 0x7a, 0xd0, 0x01, 0x35,
  18967. 0x8d, 0xf8, 0xf2, 0x5c, 0x91, 0x02, 0xb0, 0x6c,
  18968. 0x3f, 0xeb, 0xee, 0xa4, 0x42, 0x19, 0xce, 0xdc,
  18969. 0x81, 0x26, 0x40
  18970. },
  18971. { /* Hello 1 */
  18972. 0xd9, 0x4b, 0xe4, 0x17, 0xef, 0x58, 0x73, 0x7d,
  18973. 0x28, 0x3d, 0xf0, 0xcc, 0x05, 0x03, 0xaf, 0xac,
  18974. 0x3d, 0x92, 0x79, 0x48, 0xe8, 0x8c, 0xdb, 0xce,
  18975. 0x95, 0x82, 0x21, 0x31, 0x7b, 0x61, 0xd7, 0xc6
  18976. },
  18977. { /* Hello 2 */
  18978. 0xb7, 0x7f, 0x29, 0x91, 0xa4, 0x8b, 0x34, 0xdb,
  18979. 0xbd, 0xc7, 0x54, 0x1c, 0x3b, 0x86, 0xa3, 0x69,
  18980. 0xfe, 0x26, 0xe4, 0x7b, 0xac, 0x57, 0x71, 0xb3,
  18981. 0x32, 0x97, 0xed, 0xd2, 0x0e, 0x95, 0xb8, 0x63
  18982. },
  18983. { /* Finished 1 */
  18984. 0x65, 0xdb, 0x6d, 0x71, 0x71, 0xd0, 0xd8, 0x49,
  18985. 0xd0, 0x3c, 0x8e, 0x2b, 0x24, 0xdf, 0xc2, 0xe9,
  18986. 0xd6, 0xfd, 0xea, 0x04, 0x95, 0x7c, 0xf0, 0x7e,
  18987. 0x57, 0x74, 0x7c, 0xdd, 0xa3, 0x0b, 0x2b, 0x36
  18988. },
  18989. { /* Finished 2 */
  18990. 0x28, 0xf2, 0xf2, 0x79, 0xcf, 0x20, 0x52, 0x90,
  18991. 0x1d, 0x91, 0x05, 0xad, 0x44, 0x26, 0x23, 0x96,
  18992. 0x32, 0xce, 0xec, 0x61, 0xd1, 0xbf, 0x00, 0x48,
  18993. 0x4a, 0xa5, 0x60, 0xcc, 0x28, 0xb5, 0x8d, 0x98
  18994. },
  18995. { /* Client Early Traffic Secret */
  18996. 0x07, 0x14, 0x6a, 0x26, 0x5b, 0x6c, 0x7f, 0x4d, 0x6b, 0x47, 0x3f, 0xd5,
  18997. 0x03, 0x1d, 0xd2, 0x23, 0x3d, 0x89, 0x3e, 0xc6, 0x51, 0xd1, 0xac, 0xf8,
  18998. 0x28, 0xae, 0x4b, 0x76, 0xc8, 0x10, 0x7e, 0xdd
  18999. },
  19000. { /* Early Exporter Master Secret */
  19001. 0xb8, 0xd3, 0x25, 0x7e, 0x2d, 0x41, 0x7b, 0xcb, 0x5e, 0x82, 0x49, 0xf5,
  19002. 0x51, 0x3d, 0xb7, 0x59, 0x32, 0xb3, 0xdf, 0x99, 0x4e, 0x04, 0x69, 0xc6,
  19003. 0x96, 0x8e, 0xe6, 0x3d, 0x91, 0xe4, 0x81, 0x11
  19004. },
  19005. { /* Client Handshake Traffic Secret */
  19006. 0xd9, 0x3b, 0x54, 0xe2, 0xb0, 0xd1, 0x85, 0xf0, 0xfd, 0xf3, 0x48, 0x4a,
  19007. 0xf8, 0x0b, 0xa5, 0xdc, 0x4c, 0x37, 0xcb, 0xd4, 0x20, 0xaf, 0x60, 0xc7,
  19008. 0xd5, 0x50, 0x5d, 0x0c, 0x77, 0x3b, 0x6f, 0xd2
  19009. },
  19010. { /* Server Handshake Traffic Secret */
  19011. 0x4d, 0x40, 0x2b, 0xd2, 0x8c, 0x33, 0x90, 0x39, 0x67, 0x67, 0x05, 0xf7,
  19012. 0x5d, 0x37, 0x1e, 0xdc, 0x4a, 0x70, 0x6b, 0x9e, 0xf8, 0x06, 0x61, 0x89,
  19013. 0x70, 0xe1, 0x3d, 0x36, 0xad, 0x88, 0x7e, 0x5b
  19014. },
  19015. { /* Client Application Traffic Secret */
  19016. 0x74, 0x6e, 0xa0, 0x13, 0x18, 0x34, 0x48, 0x4d, 0x23, 0x31, 0xf1, 0xf9,
  19017. 0xee, 0x44, 0x6d, 0xad, 0xc1, 0xad, 0x92, 0x73, 0xca, 0x27, 0x16, 0x91,
  19018. 0xa2, 0x50, 0x9a, 0xfc, 0xec, 0xf0, 0x6b, 0x24
  19019. },
  19020. { /* Server Application Traffic Secret */
  19021. 0x89, 0x18, 0x7e, 0x34, 0x8d, 0xfc, 0x14, 0xb1, 0x4f, 0x21, 0xd8, 0x29,
  19022. 0xdb, 0x9b, 0xfb, 0x55, 0xcf, 0xa1, 0x4f, 0x95, 0xf8, 0xe0, 0xb0, 0x83,
  19023. 0xd5, 0x34, 0x9e, 0x0b, 0x83, 0x37, 0x42, 0x93
  19024. },
  19025. { /* Exporter Master Secret */
  19026. 0x7d, 0xc8, 0x88, 0x46, 0xd5, 0x57, 0x15, 0xb6, 0x24, 0x25, 0x92, 0x61,
  19027. 0xb1, 0x18, 0x86, 0x2a, 0x6d, 0xa5, 0x84, 0xeb, 0x59, 0xdf, 0x13, 0xbd,
  19028. 0x73, 0xaa, 0x5d, 0x65, 0xab, 0xd9, 0xb4, 0x56
  19029. },
  19030. { /* Resumption Master Secret */
  19031. 0x20, 0xb7, 0xd0, 0xe3, 0x82, 0x01, 0xa1, 0x04, 0xb8, 0x13, 0x29, 0xed,
  19032. 0x35, 0xe4, 0x2f, 0xbf, 0x58, 0x23, 0x7f, 0x21, 0xdb, 0x9f, 0xf8, 0xe0,
  19033. 0xe8, 0xe4, 0xab, 0xc4, 0xa1, 0x61, 0xb9, 0xbb
  19034. }
  19035. },
  19036. { /* 6 */
  19037. WC_HASH_TYPE_SHA256, 0, 33,
  19038. { 0 }, /* PSK */
  19039. { /* DHE */
  19040. 0x7a, 0x46, 0x8c, 0x5a, 0xd1, 0x8e, 0x95, 0xba,
  19041. 0x61, 0xe6, 0x6f, 0xe6, 0x76, 0x0c, 0x20, 0x43,
  19042. 0x16, 0x82, 0x15, 0xfe, 0x54, 0xa3, 0xc7, 0xfd,
  19043. 0x3b, 0x2c, 0x88, 0xb4, 0xd3, 0x42, 0x70, 0x12,
  19044. 0x18
  19045. },
  19046. { /* Hello 1 */
  19047. 0x63, 0x83, 0x58, 0xab, 0x36, 0xcd, 0x0c, 0xf3,
  19048. 0x26, 0x07, 0xb5, 0x5f, 0x0b, 0x8b, 0x45, 0xd6,
  19049. 0x7d, 0x5b, 0x42, 0xdc, 0xa8, 0xaa, 0x06, 0xfb,
  19050. 0x20, 0xa5, 0xbb, 0x85, 0xdb, 0x54, 0xd8, 0x8b
  19051. },
  19052. { /* Hello 2 */
  19053. 0xea, 0xfe, 0x9e, 0x8e, 0xff, 0x1f, 0x6f, 0x43,
  19054. 0xf9, 0x5d, 0xfd, 0xbf, 0xe2, 0x5f, 0x02, 0x2f,
  19055. 0x6d, 0x47, 0x60, 0x9a, 0x48, 0x9a, 0x75, 0xfb,
  19056. 0xb5, 0x4a, 0xbf, 0x9c, 0x4e, 0xff, 0xbf, 0x0b
  19057. },
  19058. { /* Finished 1 */
  19059. 0xca, 0x25, 0xb3, 0x53, 0x8e, 0x6d, 0xc3, 0x36,
  19060. 0x17, 0x30, 0x07, 0xdf, 0x0d, 0xd7, 0x79, 0xb0,
  19061. 0x7f, 0xcb, 0xbe, 0x7a, 0xbc, 0x2d, 0x9f, 0x2d,
  19062. 0x94, 0x44, 0x94, 0xe6, 0xa4, 0xf3, 0xe8, 0x53
  19063. },
  19064. { /* Finished 2 */
  19065. 0x2e, 0xa6, 0x5a, 0xaf, 0xb5, 0xba, 0x9f, 0x2f,
  19066. 0x74, 0x83, 0x5d, 0xbf, 0x86, 0xa4, 0xa6, 0xf6,
  19067. 0xb9, 0x89, 0xdf, 0x17, 0xe1, 0xa8, 0x14, 0xc0,
  19068. 0xe1, 0x50, 0xfa, 0xec, 0xfa, 0xae, 0x8b, 0x7b
  19069. },
  19070. {
  19071. 0x20, 0x18, 0x72, 0x7c, 0xde, 0x3a, 0x85, 0x17, 0x72, 0xdc, 0xd7, 0x72,
  19072. 0xb0, 0xfc, 0x45, 0xd0, 0x62, 0xb9, 0xbb, 0x38, 0x69, 0x05, 0x7b, 0xb4,
  19073. 0x5e, 0x58, 0x5d, 0xed, 0xcd, 0x0b, 0x96, 0xd3
  19074. },
  19075. {
  19076. 0x68, 0x10, 0x20, 0xd1, 0x5e, 0xfc, 0x0c, 0x53, 0x85, 0xbb, 0xdb, 0x18,
  19077. 0xa8, 0x78, 0xf1, 0x2b, 0x13, 0xba, 0x64, 0x1d, 0xe7, 0x09, 0xbe, 0x13,
  19078. 0x49, 0x26, 0xf9, 0x98, 0x56, 0xf1, 0x43, 0xfb
  19079. },
  19080. {
  19081. 0x24, 0x35, 0x3e, 0x10, 0x6f, 0x39, 0x50, 0xd6, 0xa2, 0x12, 0x99, 0xf2,
  19082. 0xd5, 0xf5, 0x19, 0xf5, 0x84, 0xed, 0xee, 0x78, 0x2a, 0xa6, 0xfa, 0x3d,
  19083. 0x06, 0xa8, 0xa7, 0x5d, 0x97, 0x78, 0xd6, 0x58
  19084. },
  19085. {
  19086. 0xf4, 0x57, 0xac, 0x24, 0x7a, 0xfb, 0x7c, 0x3b, 0xb6, 0x39, 0x17, 0x14,
  19087. 0xd9, 0xd4, 0x58, 0x4d, 0x46, 0xd5, 0x1b, 0xde, 0xf7, 0x9d, 0x06, 0xee,
  19088. 0x8d, 0x1a, 0x2c, 0x25, 0x6d, 0x64, 0xde, 0x89
  19089. },
  19090. {
  19091. 0xb6, 0x00, 0xce, 0x63, 0xed, 0x65, 0x8b, 0x66, 0x66, 0x42, 0xc6, 0xbd,
  19092. 0x89, 0xc4, 0x71, 0x6f, 0xce, 0x28, 0xb2, 0xac, 0x97, 0x07, 0x5b, 0xea,
  19093. 0xb8, 0x1d, 0x4c, 0xeb, 0x9e, 0x71, 0x07, 0x8f
  19094. },
  19095. {
  19096. 0xf8, 0x92, 0xc8, 0xba, 0xe7, 0x83, 0xfe, 0x68, 0xe4, 0xd6, 0x5e, 0xcb,
  19097. 0xb3, 0xef, 0x49, 0xd0, 0xe7, 0xb1, 0xac, 0xcb, 0x39, 0x19, 0xfd, 0xa7,
  19098. 0xf7, 0xca, 0xab, 0x1e, 0x42, 0x14, 0xd8, 0xe7
  19099. },
  19100. {
  19101. 0x32, 0x4a, 0x1a, 0xad, 0xe2, 0xbb, 0x55, 0x8a, 0xdd, 0xe9, 0xa5, 0x2a,
  19102. 0x46, 0x5e, 0x6c, 0x83, 0x66, 0x27, 0x27, 0x94, 0xdd, 0x68, 0x59, 0xa0,
  19103. 0xbb, 0xe8, 0x31, 0x7c, 0x39, 0xd7, 0xfd, 0x6d
  19104. },
  19105. {
  19106. 0x58, 0xbc, 0x6c, 0x5b, 0x24, 0xad, 0x82, 0xb3, 0xcc, 0xc7, 0xd1, 0xa1,
  19107. 0xaa, 0x2b, 0x98, 0x9f, 0x2f, 0x7e, 0xa9, 0x63, 0xc2, 0x8e, 0xb6, 0x06,
  19108. 0xc2, 0x2b, 0x74, 0x4b, 0x79, 0x19, 0x7e, 0x2e
  19109. }
  19110. },
  19111. { /* 11 */
  19112. WC_HASH_TYPE_SHA256, 33, 0,
  19113. { /* PSK */
  19114. 0x3d, 0x39, 0x49, 0x36, 0x98, 0xc5, 0xfd, 0xcd,
  19115. 0xa0, 0x17, 0xbd, 0x65, 0x0a, 0xdb, 0xd4, 0x07,
  19116. 0x56, 0xa2, 0x7b, 0xb8, 0x2a, 0x7e, 0xfb, 0x26,
  19117. 0x74, 0xe1, 0xbc, 0x08, 0x4b, 0xf0, 0x30, 0x14,
  19118. 0x12
  19119. },
  19120. { 0 }, /* DHE */
  19121. { /* Hello 1 */
  19122. 0xb7, 0x44, 0x74, 0x6c, 0x57, 0x1f, 0xf3, 0x84,
  19123. 0x8f, 0x63, 0xfb, 0x8c, 0x94, 0x6c, 0x16, 0x68,
  19124. 0x4b, 0xe1, 0xb5, 0xb5, 0x2a, 0x4e, 0x5f, 0xdf,
  19125. 0x4b, 0x53, 0xb2, 0x35, 0xfc, 0x30, 0xf1, 0x36
  19126. },
  19127. { /* Hello 2 */
  19128. 0xe6, 0x4f, 0x3a, 0x4f, 0xd7, 0xe0, 0x64, 0xd4,
  19129. 0x69, 0x50, 0xe4, 0x8b, 0xba, 0xbc, 0x47, 0x74,
  19130. 0xa7, 0x9b, 0x40, 0x91, 0x8f, 0xa8, 0x72, 0x22,
  19131. 0x97, 0xad, 0x43, 0xa7, 0x11, 0x86, 0xb5, 0x72
  19132. },
  19133. { /* Finished 1 */
  19134. 0x5f, 0xa6, 0x10, 0xe2, 0xa3, 0x99, 0x0b, 0x5e,
  19135. 0x57, 0xee, 0xc3, 0x3a, 0x8e, 0x04, 0xf3, 0x0e,
  19136. 0x58, 0x02, 0x09, 0xb2, 0x7e, 0x2d, 0xc6, 0xd2,
  19137. 0x08, 0xae, 0x68, 0x0a, 0x55, 0xa5, 0xda, 0x51
  19138. },
  19139. { /* Finished 2 */
  19140. 0xfc, 0x5b, 0xc0, 0x7e, 0x1b, 0xaa, 0xc0, 0xb4,
  19141. 0x34, 0x85, 0x49, 0x8e, 0x16, 0x31, 0x98, 0xdf,
  19142. 0x10, 0x54, 0x22, 0xda, 0x1e, 0x6b, 0x51, 0xf6,
  19143. 0x97, 0x57, 0xa0, 0x7a, 0x92, 0xe7, 0x47, 0x52
  19144. },
  19145. {
  19146. 0x80, 0xfa, 0x36, 0x30, 0xb8, 0x65, 0xb3, 0x2a, 0x1d, 0x68, 0x91, 0x06,
  19147. 0x98, 0xa0, 0x17, 0x8f, 0xee, 0xb7, 0x9e, 0x3d, 0xd8, 0x84, 0x99, 0x30,
  19148. 0xb9, 0xd6, 0x09, 0x25, 0x5e, 0xfb, 0x8f, 0xd3 },
  19149. {
  19150. 0xa9, 0x89, 0x29, 0x70, 0xe4, 0x55, 0xec, 0x97, 0xfb, 0x24, 0x5b, 0xf9,
  19151. 0xf1, 0xa3, 0x19, 0x3d, 0xf1, 0x31, 0x14, 0xcd, 0x2a, 0xed, 0x21, 0xc8,
  19152. 0xb1, 0x53, 0xad, 0x11, 0x0b, 0x9e, 0x5a, 0xee },
  19153. {
  19154. 0x72, 0xad, 0x8d, 0x7f, 0xfc, 0xb7, 0x68, 0xda, 0x27, 0x60, 0x37, 0xa3,
  19155. 0x4a, 0x63, 0xe8, 0xa5, 0xc8, 0xcd, 0x36, 0x6a, 0x77, 0x99, 0x0d, 0xa9,
  19156. 0xb1, 0x5b, 0x2f, 0x47, 0x2e, 0x22, 0xa7, 0x5e },
  19157. {
  19158. 0x95, 0x6e, 0x85, 0x09, 0xe5, 0x04, 0x88, 0x14, 0x28, 0x8d, 0xdf, 0xe6,
  19159. 0x0d, 0x0f, 0x0d, 0x6b, 0x4e, 0x66, 0x1c, 0x03, 0xb9, 0xaa, 0x2d, 0x45,
  19160. 0x56, 0x67, 0x5c, 0x55, 0x29, 0xd6, 0x89, 0xd0 },
  19161. {
  19162. 0xe8, 0xf2, 0x14, 0xf9, 0x9b, 0x2b, 0x9f, 0x24, 0x2b, 0x37, 0xbe, 0x86,
  19163. 0xdb, 0x23, 0x4b, 0xbe, 0x39, 0x57, 0xe8, 0xa9, 0xa5, 0xee, 0x08, 0xf2,
  19164. 0x75, 0x58, 0xdb, 0xd9, 0x51, 0xc1, 0x46, 0x02 },
  19165. {
  19166. 0x3d, 0x19, 0xaf, 0xa3, 0x0b, 0x21, 0xf7, 0x3d, 0xe7, 0x37, 0x6e, 0x32,
  19167. 0x13, 0x48, 0x9d, 0xea, 0xe0, 0x90, 0xbf, 0x64, 0x48, 0xf7, 0x1e, 0xcc,
  19168. 0xf0, 0xbc, 0x92, 0xd7, 0x8a, 0x4a, 0xa8, 0xc1 },
  19169. {
  19170. 0x16, 0x35, 0xb1, 0x66, 0x28, 0xa3, 0x3e, 0x19, 0xf5, 0x2d, 0x92, 0x22,
  19171. 0x95, 0x48, 0xe8, 0x34, 0x7b, 0x30, 0x50, 0xa2, 0xa0, 0xd9, 0xc2, 0x59,
  19172. 0x39, 0xf9, 0x8c, 0x69, 0xf2, 0x2a, 0xb9, 0xff },
  19173. {
  19174. 0x32, 0x71, 0xa6, 0x87, 0x0c, 0x97, 0x42, 0x07, 0xdd, 0x5f, 0xc9, 0x44,
  19175. 0xa5, 0x7c, 0x50, 0x14, 0xfd, 0xe7, 0x5f, 0x8b, 0xd3, 0x2f, 0xdc, 0x9b,
  19176. 0xa9, 0x93, 0x22, 0x19, 0xe6, 0xf2, 0x0c, 0xd8 }
  19177. },
  19178. #ifdef WOLFSSL_SHA384
  19179. { /* 26 */
  19180. WC_HASH_TYPE_SHA384, 35, 35,
  19181. { /* PSK */
  19182. 0x62, 0x83, 0x25, 0xc7, 0xcc, 0x08, 0x5e, 0x63,
  19183. 0x64, 0x56, 0xf0, 0xc6, 0x88, 0x27, 0x5a, 0x5b,
  19184. 0x68, 0x59, 0x0b, 0x14, 0x55, 0x13, 0x2e, 0xfd,
  19185. 0x8f, 0x28, 0x5b, 0x3d, 0xe3, 0xad, 0x67, 0xe4,
  19186. 0x68, 0xba, 0xf9
  19187. },
  19188. { /* DHE */
  19189. 0xa8, 0xb1, 0xab, 0xd8, 0xc8, 0x5b, 0x52, 0xdf,
  19190. 0x7f, 0x49, 0x10, 0xf4, 0xa1, 0x31, 0xd1, 0x91,
  19191. 0x36, 0xc1, 0x87, 0x5d, 0x42, 0x2a, 0xe7, 0x1d,
  19192. 0x2c, 0x29, 0x3d, 0x40, 0x64, 0x61, 0x63, 0x76,
  19193. 0xd8, 0x66, 0xac
  19194. },
  19195. { /* Hello 1 */
  19196. 0x6f, 0xc6, 0x4c, 0xe1, 0xc6, 0x68, 0x34, 0x8c,
  19197. 0x0a, 0xe1, 0xf8, 0xb8, 0x3e, 0xd4, 0xf8, 0x0b,
  19198. 0x54, 0x50, 0xe4, 0xc5, 0x4a, 0x33, 0x7d, 0xbd,
  19199. 0x90, 0xd2, 0xa2, 0xb9, 0xb7, 0x92, 0xed, 0xab,
  19200. 0x14, 0xf1, 0xe4, 0x86, 0x22, 0x67, 0xd7, 0x44,
  19201. 0x03, 0x21, 0xdc, 0x51, 0x52, 0x7f, 0x35, 0x80
  19202. },
  19203. { /* Hello 2 */
  19204. 0x3e, 0xcf, 0x2f, 0xc3, 0x87, 0xba, 0xc5, 0xbd,
  19205. 0x7c, 0xe8, 0x35, 0x5b, 0x95, 0x51, 0x30, 0x3b,
  19206. 0x08, 0xcc, 0x2a, 0x7d, 0xb5, 0x74, 0x7c, 0x16,
  19207. 0xb3, 0x0b, 0xe7, 0x61, 0xa3, 0x7c, 0x6c, 0xbd,
  19208. 0x39, 0x74, 0xfd, 0x1e, 0x4c, 0xff, 0xc8, 0xcc,
  19209. 0xa0, 0xef, 0x29, 0x4d, 0x94, 0xaa, 0x55, 0x6f,
  19210. },
  19211. { /* Finished 1 */
  19212. 0x06, 0xc1, 0x47, 0x78, 0x66, 0x53, 0x6f, 0x24,
  19213. 0x94, 0x61, 0x69, 0xec, 0xd8, 0x60, 0x31, 0x2f,
  19214. 0xbf, 0xd6, 0x8a, 0x29, 0x17, 0xff, 0xa3, 0x88,
  19215. 0x13, 0x09, 0x8c, 0x9d, 0x6c, 0x64, 0x84, 0x48,
  19216. 0x44, 0xdd, 0x2d, 0x29, 0x4d, 0xe6, 0x98, 0x2b,
  19217. 0x45, 0x3b, 0x84, 0x33, 0x79, 0xb2, 0x75, 0x68
  19218. },
  19219. { /* Finished 2 */
  19220. 0x28, 0x1e, 0x18, 0xf7, 0x9c, 0x32, 0xa9, 0xbf,
  19221. 0x0c, 0x24, 0x58, 0x21, 0xce, 0xbc, 0xf2, 0x44,
  19222. 0xb1, 0x18, 0xaf, 0x9d, 0xd9, 0x20, 0xf9, 0xf4,
  19223. 0xed, 0xcc, 0x53, 0x82, 0x66, 0x5c, 0x46, 0x94,
  19224. 0x8c, 0x36, 0x5e, 0xca, 0x9f, 0xd8, 0x9a, 0xd3,
  19225. 0xf0, 0xe1, 0x53, 0x71, 0xdd, 0x19, 0x1e, 0x59
  19226. },
  19227. {
  19228. 0xd0, 0xef, 0xa8, 0xcb, 0x5b, 0x14, 0x0f, 0x0a, 0x62, 0xba, 0x5a, 0xb1,
  19229. 0xc5, 0xb5, 0x3f, 0x11, 0xda, 0xa1, 0x0c, 0x9c, 0xb4, 0x32, 0x48, 0x4e,
  19230. 0xfa, 0x84, 0x4f, 0xe4, 0xe7, 0x91, 0x8f, 0x42, 0x3f, 0xc7, 0x4e, 0xd3,
  19231. 0x83, 0x3d, 0x7f, 0x70, 0x12, 0xee, 0x9a, 0x37, 0x01, 0xbb, 0x14, 0xd3
  19232. },
  19233. {
  19234. 0x48, 0x6f, 0x77, 0x1d, 0x39, 0x1b, 0xa5, 0x9a, 0x76, 0xd9, 0x1d, 0x7d,
  19235. 0xb3, 0xd9, 0xb9, 0x78, 0x35, 0x0f, 0xd0, 0xe1, 0x07, 0x1f, 0x8d, 0xe5,
  19236. 0x75, 0x00, 0xda, 0xc0, 0x19, 0x01, 0xfb, 0x08, 0x35, 0xe7, 0x18, 0x8f,
  19237. 0xf0, 0x19, 0xfb, 0x46, 0xf6, 0xa5, 0x77, 0x0e, 0x90, 0x38, 0x8b, 0x15
  19238. },
  19239. {
  19240. 0x80, 0x8c, 0xa7, 0x24, 0x97, 0xf9, 0xd3, 0x52, 0xb0, 0x69, 0x9d, 0x4b,
  19241. 0xa4, 0x19, 0x4a, 0xb1, 0x46, 0x53, 0x3a, 0xc8, 0xe4, 0x02, 0x69, 0xf2,
  19242. 0xe7, 0xb6, 0x1d, 0x33, 0x51, 0xcc, 0x14, 0x40, 0x4a, 0xb0, 0xe7, 0x58,
  19243. 0x84, 0xba, 0xc2, 0x14, 0x58, 0x6b, 0xb9, 0xdc, 0x50, 0x98, 0x67, 0x01
  19244. },
  19245. {
  19246. 0xb1, 0xa8, 0xc0, 0x06, 0xb3, 0x2e, 0xa7, 0x8a, 0x6a, 0x12, 0x88, 0x00,
  19247. 0x65, 0x88, 0x9c, 0x5d, 0x35, 0xee, 0xe5, 0x51, 0x0b, 0x62, 0xf8, 0x67,
  19248. 0xe5, 0xef, 0x15, 0x1f, 0x23, 0x02, 0x74, 0x08, 0x9c, 0xc8, 0xba, 0x27,
  19249. 0x5d, 0x32, 0x19, 0x6f, 0x6d, 0x5d, 0x72, 0x5e, 0x15, 0xde, 0x30, 0xc3
  19250. },
  19251. {
  19252. 0xfd, 0xce, 0xf5, 0x65, 0x45, 0x84, 0xfb, 0x8c, 0x79, 0xa4, 0x6c, 0x1b,
  19253. 0x0e, 0x1b, 0xfd, 0x26, 0xa2, 0x53, 0xf4, 0x4e, 0x00, 0x4d, 0x4b, 0x0b,
  19254. 0x24, 0x6d, 0x35, 0x35, 0xd9, 0x97, 0x70, 0xc5, 0xf4, 0xee, 0xe3, 0xba,
  19255. 0x31, 0x1e, 0x2a, 0x42, 0xcb, 0xdf, 0x40, 0xb1, 0x14, 0xb8, 0x53, 0xce
  19256. },
  19257. {
  19258. 0xbb, 0xb3, 0x26, 0x7c, 0x22, 0x21, 0x9b, 0x72, 0x32, 0xa1, 0x97, 0xfb,
  19259. 0x78, 0x8c, 0xbe, 0x3d, 0x71, 0x45, 0xb8, 0xf5, 0x24, 0x8f, 0x0f, 0xac,
  19260. 0x42, 0x5b, 0x81, 0xe8, 0xd0, 0x71, 0x4a, 0xcb, 0x32, 0x3f, 0x03, 0xfb,
  19261. 0xec, 0x6a, 0x1f, 0x76, 0x80, 0x65, 0x01, 0x7a, 0x3d, 0xce, 0xc4, 0xdf
  19262. },
  19263. {
  19264. 0x3f, 0xcf, 0x2f, 0x63, 0x94, 0x94, 0x99, 0xfd, 0x04, 0x3a, 0x89, 0x83,
  19265. 0xcf, 0x06, 0x05, 0xec, 0x20, 0x3e, 0x5f, 0x51, 0x9d, 0x6e, 0x4a, 0xc6,
  19266. 0xf1, 0x2b, 0x37, 0x17, 0x34, 0x72, 0x6e, 0x1d, 0x2a, 0xfd, 0xc7, 0x73,
  19267. 0xb5, 0x07, 0x22, 0x81, 0x32, 0x2e, 0x21, 0x85, 0xaf, 0x10, 0xb2, 0x73
  19268. },
  19269. {
  19270. 0x52, 0x0c, 0x3d, 0x2e, 0x2d, 0x4a, 0x11, 0xae, 0x96, 0x78, 0xe9, 0x5b,
  19271. 0xd8, 0x0f, 0x6c, 0xf4, 0xbd, 0x96, 0x13, 0x55, 0x88, 0xdd, 0xa3, 0x67,
  19272. 0x36, 0x86, 0x1e, 0x0b, 0x36, 0x41, 0xec, 0xf6, 0x04, 0xb2, 0xc4, 0x16,
  19273. 0xbc, 0x2c, 0xdb, 0x30, 0x02, 0x94, 0xd4, 0x42, 0xbf, 0x38, 0xee, 0x9d
  19274. }
  19275. },
  19276. { /* 36 */
  19277. WC_HASH_TYPE_SHA384, 0, 33,
  19278. { 0 }, /* PSK */
  19279. { /* DHE */
  19280. 0xd3, 0x00, 0x72, 0x9a, 0xa8, 0xc5, 0xf3, 0xc4,
  19281. 0xf1, 0xa0, 0x26, 0x89, 0x65, 0x70, 0xc7, 0x0b,
  19282. 0x77, 0xbb, 0xe1, 0x4b, 0x2b, 0xa8, 0x4f, 0xa6,
  19283. 0x09, 0x4b, 0xba, 0x45, 0x36, 0x15, 0xee, 0x68,
  19284. 0xfd
  19285. },
  19286. { /* Hello 1 */
  19287. 0x10, 0x9d, 0x8b, 0xa2, 0x93, 0xe7, 0xd3, 0xb9,
  19288. 0xb4, 0x0f, 0xeb, 0x6a, 0xb9, 0x69, 0xcb, 0x39,
  19289. 0x16, 0x29, 0xcc, 0xd3, 0xcc, 0x1a, 0x4c, 0x1b,
  19290. 0x53, 0x7c, 0x33, 0x88, 0x06, 0xbc, 0x0a, 0x02,
  19291. 0xa0, 0xbe, 0x62, 0xc0, 0xe6, 0x5e, 0x97, 0x5b,
  19292. 0x6a, 0xa1, 0x98, 0xf3, 0xd2, 0x1e, 0xcd, 0xc5
  19293. },
  19294. { /* Hello 2 */
  19295. 0x74, 0xc0, 0x07, 0x2c, 0xc1, 0x63, 0xcc, 0x11,
  19296. 0xad, 0x1a, 0x55, 0x63, 0xbc, 0x20, 0x77, 0x96,
  19297. 0x30, 0x1c, 0x68, 0x45, 0x1e, 0x9b, 0xa7, 0xb4,
  19298. 0xf3, 0x04, 0x45, 0x16, 0x76, 0x55, 0xf9, 0xdf,
  19299. 0x4b, 0x2f, 0x1a, 0xdf, 0x5a, 0xb0, 0x93, 0xc9,
  19300. 0xab, 0xf5, 0x32, 0x47, 0x79, 0x9c, 0x01, 0xeb
  19301. },
  19302. { /* Finished 1 */
  19303. 0x27, 0x08, 0x8e, 0xa5, 0xf1, 0x30, 0xe1, 0xd6,
  19304. 0x4f, 0xa2, 0x9e, 0x3b, 0x03, 0x2d, 0x2e, 0xa3,
  19305. 0x84, 0x75, 0x51, 0x3a, 0xc3, 0xf6, 0xee, 0x2e,
  19306. 0x37, 0x0c, 0xe3, 0x28, 0x46, 0xa5, 0x2d, 0xc7,
  19307. 0xf0, 0x64, 0x78, 0x53, 0x66, 0x43, 0x02, 0xa4,
  19308. 0x7a, 0x43, 0x66, 0x4b, 0xa7, 0xcb, 0x97, 0x16
  19309. },
  19310. { /* Finished 2 */
  19311. 0x1d, 0x0d, 0xf8, 0xe1, 0x81, 0xa5, 0xbd, 0xa8,
  19312. 0x6f, 0x9d, 0x01, 0xa4, 0x9a, 0x92, 0xe2, 0xef,
  19313. 0x08, 0xab, 0xef, 0x3e, 0x2d, 0xd4, 0x82, 0xac,
  19314. 0x68, 0x9d, 0xe0, 0x54, 0x17, 0xde, 0x1a, 0xed,
  19315. 0x57, 0xcb, 0xd9, 0x2d, 0xc8, 0xbc, 0x93, 0xe6,
  19316. 0xa3, 0xec, 0xde, 0xee, 0xa1, 0x1c, 0x41, 0x85
  19317. },
  19318. {
  19319. 0x7f, 0x1f, 0xe6, 0x7b, 0xd8, 0xf5, 0x2b, 0x37, 0xbe, 0xb7, 0xd0, 0x37,
  19320. 0xce, 0x46, 0xad, 0x04, 0x2f, 0xc7, 0xdb, 0xc9, 0x9a, 0xb6, 0x00, 0x3f,
  19321. 0xc1, 0x97, 0xe9, 0x5c, 0x5e, 0x14, 0xd1, 0x38, 0x4d, 0x55, 0xe1, 0x07,
  19322. 0xb5, 0x85, 0x6d, 0xfa, 0xa7, 0x66, 0xad, 0xfa, 0xb6, 0xad, 0x29, 0x44
  19323. },
  19324. {
  19325. 0x4e, 0x6b, 0x20, 0x99, 0x55, 0x1b, 0x21, 0x89, 0xb6, 0x70, 0xdb, 0xe8,
  19326. 0xa7, 0x16, 0x55, 0xf2, 0x93, 0x13, 0x90, 0x7d, 0xfa, 0x62, 0x65, 0x53,
  19327. 0xa0, 0x97, 0xe9, 0xb4, 0xc0, 0xf1, 0xc9, 0x1a, 0x67, 0xdd, 0xca, 0x57,
  19328. 0xbc, 0xca, 0x39, 0xe6, 0x39, 0x6b, 0x63, 0x47, 0x25, 0x08, 0x3a, 0xd7
  19329. },
  19330. {
  19331. 0x35, 0x0d, 0xac, 0xd8, 0x10, 0x6a, 0x46, 0x50, 0x66, 0xae, 0x02, 0xc9,
  19332. 0xde, 0x13, 0x48, 0xce, 0x53, 0xd4, 0x92, 0x62, 0xc5, 0x65, 0x10, 0x08,
  19333. 0xc2, 0xc2, 0x82, 0xed, 0x9d, 0xc9, 0x6f, 0xa8, 0xc3, 0xc1, 0x0b, 0x7c,
  19334. 0xe1, 0x97, 0x85, 0xd6, 0x46, 0x29, 0x0e, 0x42, 0x51, 0xc1, 0x35, 0xcf
  19335. },
  19336. {
  19337. 0x3d, 0x5d, 0x84, 0xbd, 0x16, 0x46, 0x34, 0xb3, 0xf6, 0x31, 0x49, 0x3e,
  19338. 0x8d, 0xdc, 0xcb, 0x8c, 0x6a, 0x42, 0xf4, 0x88, 0xfc, 0x19, 0xfa, 0xa2,
  19339. 0x25, 0xc7, 0xa0, 0xa4, 0xca, 0xf0, 0xea, 0x2d, 0xe8, 0xc4, 0x02, 0x14,
  19340. 0x63, 0xfb, 0xd3, 0x7b, 0x51, 0x1c, 0xce, 0xca, 0xa3, 0xc3, 0xe4, 0xa5
  19341. },
  19342. {
  19343. 0x7c, 0x3a, 0x55, 0x92, 0x2e, 0xdd, 0x75, 0xdd, 0x76, 0x54, 0x4a, 0x9f,
  19344. 0xd0, 0xa2, 0x88, 0x83, 0xe9, 0x27, 0xda, 0x30, 0xe9, 0x96, 0x58, 0xc5,
  19345. 0xb7, 0x56, 0xfc, 0x4b, 0xb8, 0x5d, 0xee, 0x46, 0x70, 0x4e, 0x1b, 0x06,
  19346. 0x86, 0xaf, 0x48, 0x5c, 0x17, 0x35, 0xfa, 0x69, 0xc2, 0x4d, 0xfb, 0x09
  19347. },
  19348. {
  19349. 0x00, 0x0e, 0x28, 0x51, 0xc1, 0x7f, 0x41, 0x89, 0x6f, 0x9a, 0xca, 0x15,
  19350. 0xee, 0xed, 0x43, 0xca, 0x6d, 0x65, 0x6f, 0x51, 0x18, 0x6c, 0x08, 0x4b,
  19351. 0x77, 0xca, 0x75, 0xc4, 0xc3, 0xde, 0x29, 0x41, 0x8b, 0xaf, 0xa7, 0x1c,
  19352. 0x28, 0x37, 0xa0, 0xa0, 0x74, 0x8e, 0x09, 0x42, 0x7a, 0x1b, 0x68, 0xdb
  19353. },
  19354. {
  19355. 0x14, 0x8f, 0xab, 0x28, 0x64, 0xea, 0x45, 0x88, 0xdb, 0xc1, 0xc6, 0xa0,
  19356. 0x48, 0xdf, 0x15, 0xd0, 0x28, 0x07, 0x2d, 0x6c, 0xb8, 0x42, 0xbb, 0x60,
  19357. 0x02, 0x08, 0x9e, 0x29, 0x9b, 0x8d, 0xd6, 0x1c, 0xaf, 0xf2, 0x1a, 0xdc,
  19358. 0xf0, 0x78, 0x0b, 0x4d, 0x90, 0xa1, 0x0c, 0xb3, 0x13, 0xde, 0xca, 0x5a
  19359. },
  19360. {
  19361. 0x4d, 0x80, 0x7d, 0x0b, 0xb9, 0x00, 0x6f, 0x65, 0x51, 0x65, 0x23, 0xde,
  19362. 0x72, 0xdc, 0x4f, 0x04, 0xa5, 0xa2, 0x90, 0x45, 0x51, 0x9e, 0xd0, 0x3a,
  19363. 0xe4, 0xd7, 0x78, 0xa3, 0x0f, 0x2d, 0x65, 0x12, 0xad, 0xc8, 0x92, 0x30,
  19364. 0x79, 0x9d, 0x9d, 0x08, 0x7a, 0x9c, 0x9f, 0x83, 0xb1, 0xca, 0x59, 0x56
  19365. }
  19366. },
  19367. { /* 41 */
  19368. WC_HASH_TYPE_SHA384, 33, 0,
  19369. { /* PSK */
  19370. 0xa4, 0x8b, 0x1b, 0x5f, 0xd0, 0xea, 0x75, 0x62,
  19371. 0x06, 0x4d, 0x68, 0x40, 0x85, 0x20, 0x45, 0x95,
  19372. 0x4a, 0x00, 0xca, 0x05, 0xeb, 0xd4, 0x1d, 0x48,
  19373. 0x81, 0x89, 0xe8, 0x86, 0x43, 0xfa, 0x28, 0x17,
  19374. 0x12
  19375. },
  19376. { 0 }, /* DHE */
  19377. { /* Hello 1 */
  19378. 0x03, 0x7c, 0x33, 0x75, 0xdc, 0xc5, 0x46, 0x3a,
  19379. 0x0d, 0x56, 0xc6, 0xfb, 0xab, 0x1e, 0x1d, 0xda,
  19380. 0x59, 0xc2, 0xb2, 0xb1, 0x7c, 0x48, 0x9b, 0x06,
  19381. 0x0a, 0x5a, 0xbb, 0xf8, 0x98, 0x53, 0x78, 0x2d,
  19382. 0xd2, 0xcc, 0x87, 0x68, 0x25, 0xdd, 0x88, 0x22,
  19383. 0xcd, 0xb7, 0x74, 0x55, 0x21, 0xf9, 0x34, 0x98
  19384. },
  19385. { /* Hello 2 */
  19386. 0x03, 0xb4, 0xfb, 0xcc, 0x28, 0x2c, 0xc1, 0x70,
  19387. 0x42, 0x73, 0x57, 0xac, 0xdb, 0x47, 0x71, 0xf6,
  19388. 0x2e, 0x11, 0x8a, 0x5b, 0x47, 0x2f, 0x02, 0x54,
  19389. 0x95, 0x34, 0xed, 0x5f, 0x19, 0xc1, 0x75, 0xe0,
  19390. 0x76, 0xad, 0xb0, 0x90, 0x57, 0xcd, 0xfd, 0xd7,
  19391. 0x58, 0x1f, 0x0d, 0x6b, 0x9e, 0x51, 0x3c, 0x08
  19392. },
  19393. { /* Finished 1 */
  19394. 0x2b, 0x50, 0xd9, 0xa7, 0x43, 0x24, 0xda, 0x2c,
  19395. 0x7a, 0xaa, 0x0e, 0x37, 0xd7, 0x6b, 0x2c, 0xab,
  19396. 0x8e, 0xb2, 0xfe, 0x31, 0x1b, 0xa8, 0x12, 0x59,
  19397. 0x5b, 0x7b, 0xdc, 0x3e, 0xa7, 0x86, 0xa5, 0x48,
  19398. 0xe4, 0x46, 0x2b, 0x4c, 0xc1, 0x66, 0x4b, 0xf3,
  19399. 0x2a, 0x99, 0x93, 0x08, 0xbc, 0x3d, 0x08, 0x76
  19400. },
  19401. { /* Finished 2 */
  19402. 0x7c, 0x34, 0xc8, 0x56, 0x17, 0xf1, 0x62, 0x1c,
  19403. 0x9f, 0x0b, 0xeb, 0xfd, 0x69, 0x72, 0x51, 0xc5,
  19404. 0xfa, 0x74, 0x87, 0xc9, 0xbd, 0x50, 0xe9, 0x48,
  19405. 0xa7, 0x3c, 0x94, 0x3e, 0x06, 0x7d, 0xe8, 0x8e,
  19406. 0xc1, 0xd1, 0x08, 0x1f, 0x5d, 0x48, 0x8a, 0x25,
  19407. 0xfc, 0xea, 0xe7, 0xd9, 0xd4, 0xd0, 0xf9, 0xad
  19408. },
  19409. {
  19410. 0x4b, 0x0b, 0xed, 0xb9, 0xc8, 0xb8, 0xa8, 0x1e, 0xb0, 0x81, 0x76, 0xd5,
  19411. 0x33, 0x22, 0x71, 0x33, 0x3a, 0x85, 0x19, 0x67, 0x7e, 0x91, 0x37, 0xf2,
  19412. 0xa6, 0x11, 0x22, 0xdf, 0x41, 0x04, 0x3d, 0xa9, 0x13, 0xb9, 0xb2, 0xb1,
  19413. 0xbb, 0xd8, 0xef, 0x23, 0x7c, 0xc2, 0xab, 0x70, 0x1b, 0x51, 0x9f, 0xc9
  19414. },
  19415. {
  19416. 0xeb, 0x96, 0x10, 0x8c, 0x7d, 0x92, 0xea, 0x80, 0x86, 0xb2, 0xf8, 0x27,
  19417. 0xf2, 0x9a, 0x09, 0xc1, 0x7c, 0x09, 0x43, 0xbc, 0xfe, 0xc8, 0x75, 0xe0,
  19418. 0x97, 0xe7, 0x6d, 0xd5, 0xb2, 0x3c, 0xed, 0x12, 0xb7, 0x74, 0x0e, 0xe3,
  19419. 0xb6, 0xe0, 0xba, 0xe1, 0x8d, 0x89, 0xcf, 0x4f, 0x57, 0xf6, 0x6d, 0x90
  19420. },
  19421. {
  19422. 0x22, 0xb0, 0x39, 0x34, 0xb6, 0x6c, 0x2d, 0x7a, 0x97, 0x1c, 0x5d, 0xcc,
  19423. 0x78, 0x84, 0x71, 0xbb, 0xc6, 0x7b, 0xb6, 0xbc, 0xcc, 0x0b, 0xf8, 0xac,
  19424. 0x8e, 0xd7, 0x20, 0xbd, 0xbe, 0x32, 0xf0, 0xd6, 0xe9, 0x69, 0x13, 0xf2,
  19425. 0x9a, 0xce, 0xfe, 0x86, 0xd3, 0xee, 0xba, 0x69, 0x51, 0xb6, 0x77, 0x56
  19426. },
  19427. {
  19428. 0x16, 0xfd, 0xda, 0xf3, 0x5e, 0xb9, 0xa6, 0x17, 0x24, 0xb2, 0x16, 0x9f,
  19429. 0xb6, 0x59, 0x13, 0x0f, 0x25, 0x5a, 0xf1, 0x5b, 0x5f, 0xe4, 0x54, 0x2a,
  19430. 0xa7, 0xbf, 0x29, 0xaf, 0x5a, 0x77, 0xf4, 0x4f, 0x25, 0xba, 0x94, 0xad,
  19431. 0x6b, 0x91, 0x3b, 0xe7, 0xd5, 0x73, 0x0d, 0xff, 0xaa, 0xe3, 0x72, 0x2c
  19432. },
  19433. {
  19434. 0x22, 0xb4, 0x94, 0xc0, 0x53, 0xd7, 0x82, 0x06, 0x38, 0x9d, 0x4a, 0xa0,
  19435. 0x3f, 0xf1, 0x5f, 0x6e, 0x23, 0x8d, 0x09, 0x62, 0xbf, 0x6f, 0x7c, 0x84,
  19436. 0xc6, 0x3e, 0x15, 0xad, 0x18, 0x37, 0x76, 0x29, 0xc7, 0xd6, 0x68, 0x0c,
  19437. 0x1e, 0xc6, 0x93, 0x31, 0xef, 0x85, 0x69, 0x30, 0x68, 0xf0, 0x1e, 0x37
  19438. },
  19439. {
  19440. 0x6d, 0x4d, 0x20, 0xaf, 0x47, 0xe8, 0x1b, 0xfa, 0xd0, 0xb6, 0xc8, 0x97,
  19441. 0xd1, 0x03, 0xfc, 0x9d, 0x59, 0xa0, 0x68, 0x9d, 0xe9, 0x17, 0x8b, 0xce,
  19442. 0x48, 0x2c, 0x77, 0x8a, 0x22, 0x4b, 0x5c, 0x54, 0x22, 0xa1, 0x15, 0x12,
  19443. 0xe1, 0x07, 0x8e, 0x15, 0xd8, 0x7b, 0x16, 0x65, 0x99, 0x6b, 0xcb, 0x71
  19444. },
  19445. {
  19446. 0x79, 0x64, 0x79, 0xdd, 0x75, 0x5c, 0x6f, 0x98, 0xac, 0x03, 0xe0, 0xcd,
  19447. 0x92, 0xba, 0x0e, 0x2d, 0xb4, 0xd1, 0x8b, 0x97, 0xd0, 0x85, 0xbb, 0x2e,
  19448. 0x4f, 0x26, 0x93, 0xf5, 0x1d, 0xf3, 0xd2, 0x43, 0x4f, 0xd2, 0x47, 0xaa,
  19449. 0x91, 0x1e, 0xf3, 0x67, 0x10, 0x18, 0x2c, 0xb9, 0x01, 0xba, 0x10, 0x9f
  19450. },
  19451. {
  19452. 0x79, 0xb6, 0x9c, 0xbe, 0xf1, 0x6a, 0xb0, 0x92, 0xa0, 0x29, 0x52, 0x61,
  19453. 0xf1, 0xcd, 0x3a, 0x67, 0xe1, 0x6b, 0xb8, 0x9d, 0x0d, 0x95, 0xb6, 0x03,
  19454. 0x80, 0x1f, 0xd5, 0x75, 0xb6, 0x1d, 0x79, 0x02, 0x93, 0x43, 0x77, 0xa7,
  19455. 0x9d, 0x2f, 0xc3, 0x84, 0xc6, 0x83, 0x76, 0x16, 0x06, 0x98, 0x7b, 0x79
  19456. }
  19457. },
  19458. #endif /* WOLFSSL_SHA384 */
  19459. };
  19460. const char protocolLabel[] = "tls13 ";
  19461. const char ceTrafficLabel[] = "c e traffic";
  19462. const char eExpMasterLabel[] = "e exp master";
  19463. const char cHsTrafficLabel[] = "c hs traffic";
  19464. const char sHsTrafficLabel[] = "s hs traffic";
  19465. const char cAppTrafficLabel[] = "c ap traffic";
  19466. const char sAppTrafficLabel[] = "s ap traffic";
  19467. const char expMasterLabel[] = "exp master";
  19468. const char resMasterLabel[] = "res master";
  19469. const char derivedLabel[] = "derived";
  19470. WOLFSSL_TEST_SUBROUTINE int tls13_kdf_test(void)
  19471. {
  19472. int ret = 0;
  19473. word32 i;
  19474. word32 tc = sizeof(tls13KdfTestVectors)/sizeof(Tls13KdfTestVector);
  19475. const Tls13KdfTestVector* tv = NULL;
  19476. for (i = 0, tv = tls13KdfTestVectors; i < tc; i++, tv++) {
  19477. byte output[WC_MAX_DIGEST_SIZE];
  19478. byte secret[WC_MAX_DIGEST_SIZE];
  19479. byte salt[WC_MAX_DIGEST_SIZE];
  19480. byte zeroes[WC_MAX_DIGEST_SIZE];
  19481. byte hashZero[WC_MAX_DIGEST_SIZE];
  19482. int hashAlgSz;
  19483. XMEMSET(zeroes, 0, sizeof zeroes);
  19484. hashAlgSz = wc_HashGetDigestSize(tv->hashAlg);
  19485. if (hashAlgSz == BAD_FUNC_ARG) break;
  19486. ret = wc_Hash(tv->hashAlg, NULL, 0, hashZero, hashAlgSz);
  19487. if (ret != 0) break;
  19488. ret = wc_Tls13_HKDF_Extract(secret, NULL, 0,
  19489. (tv->pskSz == 0) ? zeroes : (byte*)tv->psk,
  19490. tv->pskSz, tv->hashAlg);
  19491. if (ret != 0) break;
  19492. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19493. secret, hashAlgSz,
  19494. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19495. (byte*)ceTrafficLabel, (word32)strlen(ceTrafficLabel),
  19496. tv->hashHello1, hashAlgSz, tv->hashAlg);
  19497. if (ret != 0) break;
  19498. ret = XMEMCMP(tv->clientEarlyTrafficSecret, output, hashAlgSz);
  19499. if (ret != 0) break;
  19500. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19501. secret, hashAlgSz,
  19502. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19503. (byte*)eExpMasterLabel, (word32)strlen(eExpMasterLabel),
  19504. tv->hashHello1, hashAlgSz, tv->hashAlg);
  19505. if (ret != 0) break;
  19506. ret = XMEMCMP(tv->earlyExporterMasterSecret, output, hashAlgSz);
  19507. if (ret != 0) break;
  19508. ret = wc_Tls13_HKDF_Expand_Label(salt, hashAlgSz,
  19509. secret, hashAlgSz,
  19510. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19511. (byte*)derivedLabel, (word32)strlen(derivedLabel),
  19512. hashZero, hashAlgSz, tv->hashAlg);
  19513. if (ret != 0) break;
  19514. ret = wc_Tls13_HKDF_Extract(secret, salt, hashAlgSz,
  19515. (tv->dheSz == 0) ? zeroes : (byte*)tv->dhe,
  19516. tv->dheSz, tv->hashAlg);
  19517. if (ret != 0) break;
  19518. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19519. secret, hashAlgSz,
  19520. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19521. (byte*)cHsTrafficLabel, (word32)strlen(cHsTrafficLabel),
  19522. tv->hashHello2, hashAlgSz, tv->hashAlg);
  19523. if (ret != 0) break;
  19524. ret = XMEMCMP(tv->clientHandshakeTrafficSecret,
  19525. output, hashAlgSz);
  19526. if (ret != 0) break;
  19527. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19528. secret, hashAlgSz,
  19529. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19530. (byte*)sHsTrafficLabel, (word32)strlen(sHsTrafficLabel),
  19531. tv->hashHello2, hashAlgSz, tv->hashAlg);
  19532. if (ret != 0) break;
  19533. ret = XMEMCMP(tv->serverHandshakeTrafficSecret, output, hashAlgSz);
  19534. if (ret != 0) break;
  19535. ret = wc_Tls13_HKDF_Expand_Label(salt, hashAlgSz,
  19536. secret, hashAlgSz,
  19537. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19538. (byte*)derivedLabel, (word32)strlen(derivedLabel),
  19539. hashZero, hashAlgSz, tv->hashAlg);
  19540. if (ret != 0) break;
  19541. ret = wc_Tls13_HKDF_Extract(secret, salt, hashAlgSz,
  19542. zeroes, hashAlgSz, tv->hashAlg);
  19543. if (ret != 0) break;
  19544. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19545. secret, hashAlgSz,
  19546. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19547. (byte*)cAppTrafficLabel, (word32)strlen(cAppTrafficLabel),
  19548. tv->hashFinished1, hashAlgSz, tv->hashAlg);
  19549. if (ret != 0) break;
  19550. ret = XMEMCMP(tv->clientApplicationTrafficSecret, output, hashAlgSz);
  19551. if (ret != 0) break;
  19552. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19553. secret, hashAlgSz,
  19554. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19555. (byte*)sAppTrafficLabel, (word32)strlen(sAppTrafficLabel),
  19556. tv->hashFinished1, hashAlgSz, tv->hashAlg);
  19557. if (ret != 0) break;
  19558. ret = XMEMCMP(tv->serverApplicationTrafficSecret, output, hashAlgSz);
  19559. if (ret != 0) break;
  19560. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19561. secret, hashAlgSz,
  19562. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19563. (byte*)expMasterLabel, (word32)strlen(expMasterLabel),
  19564. tv->hashFinished1, hashAlgSz, tv->hashAlg);
  19565. if (ret != 0) break;
  19566. ret = XMEMCMP(tv->exporterMasterSecret, output, hashAlgSz);
  19567. if (ret != 0) break;
  19568. ret = wc_Tls13_HKDF_Expand_Label(output, hashAlgSz,
  19569. secret, hashAlgSz,
  19570. (byte*)protocolLabel, (word32)strlen(protocolLabel),
  19571. (byte*)resMasterLabel, (word32)strlen(resMasterLabel),
  19572. tv->hashFinished2, hashAlgSz, tv->hashAlg);
  19573. if (ret != 0) break;
  19574. ret = XMEMCMP(tv->resumptionMasterSecret, output, hashAlgSz);
  19575. if (ret != 0) break;
  19576. }
  19577. return ret;
  19578. }
  19579. #endif /* WOLFSSL_TLS13 */
  19580. #if defined(HAVE_ECC) && defined(HAVE_X963_KDF)
  19581. WOLFSSL_TEST_SUBROUTINE int x963kdf_test(void)
  19582. {
  19583. int ret;
  19584. byte kek[128];
  19585. #ifndef NO_SHA
  19586. /* SHA-1, COUNT = 0
  19587. * shared secret length: 192
  19588. * SharedInfo length: 0
  19589. * key data length: 128
  19590. */
  19591. WOLFSSL_SMALL_STACK_STATIC const byte Z[] = {
  19592. 0x1c, 0x7d, 0x7b, 0x5f, 0x05, 0x97, 0xb0, 0x3d,
  19593. 0x06, 0xa0, 0x18, 0x46, 0x6e, 0xd1, 0xa9, 0x3e,
  19594. 0x30, 0xed, 0x4b, 0x04, 0xdc, 0x64, 0xcc, 0xdd
  19595. };
  19596. WOLFSSL_SMALL_STACK_STATIC const byte verify[] = {
  19597. 0xbf, 0x71, 0xdf, 0xfd, 0x8f, 0x4d, 0x99, 0x22,
  19598. 0x39, 0x36, 0xbe, 0xb4, 0x6f, 0xee, 0x8c, 0xcc
  19599. };
  19600. #endif
  19601. #ifndef NO_SHA256
  19602. /* SHA-256, COUNT = 3
  19603. * shared secret length: 192
  19604. * SharedInfo length: 0
  19605. * key data length: 128
  19606. */
  19607. WOLFSSL_SMALL_STACK_STATIC const byte Z2[] = {
  19608. 0xd3, 0x8b, 0xdb, 0xe5, 0xc4, 0xfc, 0x16, 0x4c,
  19609. 0xdd, 0x96, 0x7f, 0x63, 0xc0, 0x4f, 0xe0, 0x7b,
  19610. 0x60, 0xcd, 0xe8, 0x81, 0xc2, 0x46, 0x43, 0x8c
  19611. };
  19612. WOLFSSL_SMALL_STACK_STATIC const byte verify2[] = {
  19613. 0x5e, 0x67, 0x4d, 0xb9, 0x71, 0xba, 0xc2, 0x0a,
  19614. 0x80, 0xba, 0xd0, 0xd4, 0x51, 0x4d, 0xc4, 0x84
  19615. };
  19616. #endif
  19617. #ifdef WOLFSSL_SHA512
  19618. /* SHA-512, COUNT = 0
  19619. * shared secret length: 192
  19620. * SharedInfo length: 0
  19621. * key data length: 128
  19622. */
  19623. WOLFSSL_SMALL_STACK_STATIC const byte Z3[] = {
  19624. 0x87, 0xfc, 0x0d, 0x8c, 0x44, 0x77, 0x48, 0x5b,
  19625. 0xb5, 0x74, 0xf5, 0xfc, 0xea, 0x26, 0x4b, 0x30,
  19626. 0x88, 0x5d, 0xc8, 0xd9, 0x0a, 0xd8, 0x27, 0x82
  19627. };
  19628. WOLFSSL_SMALL_STACK_STATIC const byte verify3[] = {
  19629. 0x94, 0x76, 0x65, 0xfb, 0xb9, 0x15, 0x21, 0x53,
  19630. 0xef, 0x46, 0x02, 0x38, 0x50, 0x6a, 0x02, 0x45
  19631. };
  19632. /* SHA-512, COUNT = 0
  19633. * shared secret length: 521
  19634. * SharedInfo length: 128
  19635. * key data length: 1024
  19636. */
  19637. WOLFSSL_SMALL_STACK_STATIC const byte Z4[] = {
  19638. 0x00, 0xaa, 0x5b, 0xb7, 0x9b, 0x33, 0xe3, 0x89,
  19639. 0xfa, 0x58, 0xce, 0xad, 0xc0, 0x47, 0x19, 0x7f,
  19640. 0x14, 0xe7, 0x37, 0x12, 0xf4, 0x52, 0xca, 0xa9,
  19641. 0xfc, 0x4c, 0x9a, 0xdb, 0x36, 0x93, 0x48, 0xb8,
  19642. 0x15, 0x07, 0x39, 0x2f, 0x1a, 0x86, 0xdd, 0xfd,
  19643. 0xb7, 0xc4, 0xff, 0x82, 0x31, 0xc4, 0xbd, 0x0f,
  19644. 0x44, 0xe4, 0x4a, 0x1b, 0x55, 0xb1, 0x40, 0x47,
  19645. 0x47, 0xa9, 0xe2, 0xe7, 0x53, 0xf5, 0x5e, 0xf0,
  19646. 0x5a, 0x2d
  19647. };
  19648. WOLFSSL_SMALL_STACK_STATIC const byte info4[] = {
  19649. 0xe3, 0xb5, 0xb4, 0xc1, 0xb0, 0xd5, 0xcf, 0x1d,
  19650. 0x2b, 0x3a, 0x2f, 0x99, 0x37, 0x89, 0x5d, 0x31
  19651. };
  19652. WOLFSSL_SMALL_STACK_STATIC const byte verify4[] = {
  19653. 0x44, 0x63, 0xf8, 0x69, 0xf3, 0xcc, 0x18, 0x76,
  19654. 0x9b, 0x52, 0x26, 0x4b, 0x01, 0x12, 0xb5, 0x85,
  19655. 0x8f, 0x7a, 0xd3, 0x2a, 0x5a, 0x2d, 0x96, 0xd8,
  19656. 0xcf, 0xfa, 0xbf, 0x7f, 0xa7, 0x33, 0x63, 0x3d,
  19657. 0x6e, 0x4d, 0xd2, 0xa5, 0x99, 0xac, 0xce, 0xb3,
  19658. 0xea, 0x54, 0xa6, 0x21, 0x7c, 0xe0, 0xb5, 0x0e,
  19659. 0xef, 0x4f, 0x6b, 0x40, 0xa5, 0xc3, 0x02, 0x50,
  19660. 0xa5, 0xa8, 0xee, 0xee, 0x20, 0x80, 0x02, 0x26,
  19661. 0x70, 0x89, 0xdb, 0xf3, 0x51, 0xf3, 0xf5, 0x02,
  19662. 0x2a, 0xa9, 0x63, 0x8b, 0xf1, 0xee, 0x41, 0x9d,
  19663. 0xea, 0x9c, 0x4f, 0xf7, 0x45, 0xa2, 0x5a, 0xc2,
  19664. 0x7b, 0xda, 0x33, 0xca, 0x08, 0xbd, 0x56, 0xdd,
  19665. 0x1a, 0x59, 0xb4, 0x10, 0x6c, 0xf2, 0xdb, 0xbc,
  19666. 0x0a, 0xb2, 0xaa, 0x8e, 0x2e, 0xfa, 0x7b, 0x17,
  19667. 0x90, 0x2d, 0x34, 0x27, 0x69, 0x51, 0xce, 0xcc,
  19668. 0xab, 0x87, 0xf9, 0x66, 0x1c, 0x3e, 0x88, 0x16
  19669. };
  19670. #endif
  19671. #ifndef NO_SHA
  19672. ret = wc_X963_KDF(WC_HASH_TYPE_SHA, Z, sizeof(Z), NULL, 0,
  19673. kek, sizeof(verify));
  19674. if (ret != 0)
  19675. return -9800;
  19676. if (XMEMCMP(verify, kek, sizeof(verify)) != 0)
  19677. return -9801;
  19678. #endif
  19679. #ifndef NO_SHA256
  19680. ret = wc_X963_KDF(WC_HASH_TYPE_SHA256, Z2, sizeof(Z2), NULL, 0,
  19681. kek, sizeof(verify2));
  19682. if (ret != 0)
  19683. return -9802;
  19684. if (XMEMCMP(verify2, kek, sizeof(verify2)) != 0)
  19685. return -9803;
  19686. #endif
  19687. #ifdef WOLFSSL_SHA512
  19688. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z3, sizeof(Z3), NULL, 0,
  19689. kek, sizeof(verify3));
  19690. if (ret != 0)
  19691. return -9804;
  19692. if (XMEMCMP(verify3, kek, sizeof(verify3)) != 0)
  19693. return -9805;
  19694. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z4, sizeof(Z4), info4,
  19695. sizeof(info4), kek, sizeof(verify4));
  19696. if (ret != 0)
  19697. return -9806;
  19698. if (XMEMCMP(verify4, kek, sizeof(verify4)) != 0)
  19699. return -9807;
  19700. #endif
  19701. return 0;
  19702. }
  19703. #endif /* HAVE_X963_KDF */
  19704. #ifdef HAVE_ECC
  19705. /* size to use for ECC key gen tests */
  19706. #ifndef ECC_KEYGEN_SIZE
  19707. #ifndef NO_ECC256
  19708. #define ECC_KEYGEN_SIZE 32
  19709. #elif defined(HAVE_ECC384)
  19710. #define ECC_KEYGEN_SIZE 48
  19711. #elif defined(HAVE_ECC224)
  19712. #define ECC_KEYGEN_SIZE 28
  19713. #elif defined(HAVE_ECC521)
  19714. #define ECC_KEYGEN_SIZE 66
  19715. #else
  19716. #error No ECC keygen size defined for test
  19717. #endif
  19718. #endif
  19719. #ifdef BENCH_EMBEDDED
  19720. #define ECC_SHARED_SIZE 128
  19721. #else
  19722. #define ECC_SHARED_SIZE MAX_ECC_BYTES
  19723. #endif
  19724. #if defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  19725. #define HAVE_ECC_DETERMINISTIC_K
  19726. #define ECC_DIGEST_SIZE WC_SHA256_DIGEST_SIZE
  19727. #else
  19728. #define ECC_DIGEST_SIZE MAX_ECC_BYTES
  19729. #endif
  19730. #define ECC_SIG_SIZE ECC_MAX_SIG_SIZE
  19731. #ifndef NO_ECC_VECTOR_TEST
  19732. #if (defined(HAVE_ECC192) || defined(HAVE_ECC224) ||\
  19733. !defined(NO_ECC256) || defined(HAVE_ECC384) ||\
  19734. defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES))
  19735. #define HAVE_ECC_VECTOR_TEST
  19736. #endif
  19737. #endif
  19738. #ifdef HAVE_ECC_VECTOR_TEST
  19739. typedef struct eccVector {
  19740. const char* msg; /* SHA-1 Encoded Message */
  19741. const char* Qx;
  19742. const char* Qy;
  19743. const char* d; /* Private Key */
  19744. const char* R;
  19745. const char* S;
  19746. const char* curveName;
  19747. word32 msgLen;
  19748. word32 keySize;
  19749. #ifndef NO_ASN
  19750. const byte* r;
  19751. word32 rSz;
  19752. const byte* s;
  19753. word32 sSz;
  19754. #endif
  19755. } eccVector;
  19756. #if !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  19757. static int ecc_test_vector_item(const eccVector* vector)
  19758. {
  19759. int ret = 0, verify = 0;
  19760. word32 sigSz;
  19761. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19762. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19763. #else
  19764. ecc_key userA[1];
  19765. #endif
  19766. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  19767. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  19768. word32 sigRawSz, rSz = MAX_ECC_BYTES, sSz = MAX_ECC_BYTES;
  19769. WC_DECLARE_VAR(sigRaw, byte, ECC_SIG_SIZE, HEAP_HINT);
  19770. WC_DECLARE_VAR(r, byte, MAX_ECC_BYTES, HEAP_HINT);
  19771. WC_DECLARE_VAR(s, byte, MAX_ECC_BYTES, HEAP_HINT);
  19772. #endif
  19773. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  19774. if (sig == NULL)
  19775. ERROR_OUT(MEMORY_E, done);
  19776. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  19777. if (sigRaw == NULL || r == NULL || s == NULL)
  19778. ERROR_OUT(MEMORY_E, done);
  19779. #endif
  19780. #endif
  19781. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19782. if (userA == NULL)
  19783. ERROR_OUT(MEMORY_E, done);
  19784. #endif
  19785. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  19786. if (ret != 0)
  19787. goto done;
  19788. ret = wc_ecc_import_raw(userA, vector->Qx, vector->Qy,
  19789. vector->d, vector->curveName);
  19790. if (ret != 0)
  19791. goto done;
  19792. XMEMSET(sig, 0, ECC_SIG_SIZE);
  19793. sigSz = ECC_SIG_SIZE;
  19794. ret = wc_ecc_rs_to_sig(vector->R, vector->S, sig, &sigSz);
  19795. if (ret != 0)
  19796. goto done;
  19797. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  19798. XMEMSET(sigRaw, 0, ECC_SIG_SIZE);
  19799. sigRawSz = ECC_SIG_SIZE;
  19800. ret = wc_ecc_rs_raw_to_sig(vector->r, vector->rSz, vector->s, vector->sSz,
  19801. sigRaw, &sigRawSz);
  19802. if (ret != 0)
  19803. goto done;
  19804. if (sigSz != sigRawSz || XMEMCMP(sig, sigRaw, sigSz) != 0) {
  19805. ret = -9810;
  19806. goto done;
  19807. }
  19808. ret = wc_ecc_sig_to_rs(sig, sigSz, r, &rSz, s, &sSz);
  19809. if (ret != 0)
  19810. goto done;
  19811. if (rSz != vector->rSz || XMEMCMP(r, vector->r, rSz) != 0 ||
  19812. sSz != vector->sSz || XMEMCMP(s, vector->s, sSz) != 0) {
  19813. ret = -9811;
  19814. goto done;
  19815. }
  19816. #endif
  19817. do {
  19818. #if defined(WOLFSSL_ASYNC_CRYPT)
  19819. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  19820. #endif
  19821. if (ret == 0)
  19822. ret = wc_ecc_verify_hash(sig, sigSz, (byte*)vector->msg,
  19823. vector->msgLen, &verify, userA);
  19824. } while (ret == WC_PENDING_E);
  19825. if (ret != 0)
  19826. goto done;
  19827. TEST_SLEEP();
  19828. if (verify != 1)
  19829. ret = -9812;
  19830. done:
  19831. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  19832. if (userA != NULL) {
  19833. wc_ecc_free(userA);
  19834. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  19835. }
  19836. #else
  19837. wc_ecc_free(userA);
  19838. #endif
  19839. #if !defined(NO_ASN) && !defined(HAVE_SELFTEST)
  19840. WC_FREE_VAR(sigRaw, HEAP_HINT);
  19841. WC_FREE_VAR(r, HEAP_HINT);
  19842. WC_FREE_VAR(s, HEAP_HINT);
  19843. #endif
  19844. WC_FREE_VAR(sig, HEAP_HINT);
  19845. return ret;
  19846. }
  19847. static int ecc_test_vector(int keySize)
  19848. {
  19849. int ret;
  19850. eccVector vec;
  19851. XMEMSET(&vec, 0, sizeof(vec));
  19852. vec.keySize = (word32)keySize;
  19853. switch(keySize) {
  19854. #if defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)
  19855. case 14:
  19856. return 0;
  19857. #endif /* HAVE_ECC112 */
  19858. #if defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)
  19859. case 16:
  19860. return 0;
  19861. #endif /* HAVE_ECC128 */
  19862. #if defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)
  19863. case 20:
  19864. return 0;
  19865. #endif /* HAVE_ECC160 */
  19866. #if defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)
  19867. case 24:
  19868. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  19869. #if 1
  19870. vec.msg = "\x60\x80\x79\x42\x3f\x12\x42\x1d\xe6\x16\xb7\x49\x3e\xbe\x55\x1c\xf4\xd6\x5b\x92";
  19871. vec.msgLen = 20;
  19872. #else
  19873. /* This is the raw message prior to SHA-1 */
  19874. vec.msg =
  19875. "\xeb\xf7\x48\xd7\x48\xeb\xbc\xa7\xd2\x9f\xb4\x73\x69\x8a\x6e\x6b"
  19876. "\x4f\xb1\x0c\x86\x5d\x4a\xf0\x24\xcc\x39\xae\x3d\xf3\x46\x4b\xa4"
  19877. "\xf1\xd6\xd4\x0f\x32\xbf\x96\x18\xa9\x1b\xb5\x98\x6f\xa1\xa2\xaf"
  19878. "\x04\x8a\x0e\x14\xdc\x51\xe5\x26\x7e\xb0\x5e\x12\x7d\x68\x9d\x0a"
  19879. "\xc6\xf1\xa7\xf1\x56\xce\x06\x63\x16\xb9\x71\xcc\x7a\x11\xd0\xfd"
  19880. "\x7a\x20\x93\xe2\x7c\xf2\xd0\x87\x27\xa4\xe6\x74\x8c\xc3\x2f\xd5"
  19881. "\x9c\x78\x10\xc5\xb9\x01\x9d\xf2\x1c\xdc\xc0\xbc\xa4\x32\xc0\xa3"
  19882. "\xee\xd0\x78\x53\x87\x50\x88\x77\x11\x43\x59\xce\xe4\xa0\x71\xcf";
  19883. vec.msgLen = 128;
  19884. #endif
  19885. vec.Qx = "07008ea40b08dbe76432096e80a2494c94982d2d5bcf98e6";
  19886. vec.Qy = "76fab681d00b414ea636ba215de26d98c41bd7f2e4d65477";
  19887. vec.d = "e14f37b3d1374ff8b03f41b9b3fdd2f0ebccf275d660d7f3";
  19888. vec.R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  19889. vec.S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  19890. vec.curveName = "SECP192R1";
  19891. #ifndef NO_ASN
  19892. vec.r = (byte*)"\x69\x94\xd9\x62\xbd\xd0\xd7\x93\xff\xdd\xf8\x55"
  19893. "\xec\x5b\xf2\xf9\x1a\x96\x98\xb4\x62\x58\xa6\x3e";
  19894. vec.rSz = 24;
  19895. vec.s = (byte*)"\x02\xba\x64\x65\xa2\x34\x90\x37\x44\xab\x02\xbc"
  19896. "\x85\x21\x40\x5b\x73\xcf\x5f\xc0\x0e\x1a\x9f\x41";
  19897. vec.sSz = 24;
  19898. #endif
  19899. break;
  19900. #endif /* HAVE_ECC192 */
  19901. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  19902. case 28:
  19903. /* first [P-224,SHA-1] vector from FIPS 186-3 NIST vectors */
  19904. #if 1
  19905. vec.msg = "\xb9\xa3\xb8\x6d\xb0\xba\x99\xfd\xc6\xd2\x94\x6b\xfe\xbe\x9c\xe8\x3f\x10\x74\xfc";
  19906. vec.msgLen = 20;
  19907. #else
  19908. /* This is the raw message prior to SHA-1 */
  19909. vec.msg =
  19910. "\x36\xc8\xb2\x29\x86\x48\x7f\x67\x7c\x18\xd0\x97\x2a\x9e\x20\x47"
  19911. "\xb3\xaf\xa5\x9e\xc1\x62\x76\x4e\xc3\x0b\x5b\x69\xe0\x63\x0f\x99"
  19912. "\x0d\x4e\x05\xc2\x73\xb0\xe5\xa9\xd4\x28\x27\xb6\x95\xfc\x2d\x64"
  19913. "\xd9\x13\x8b\x1c\xf4\xc1\x21\x55\x89\x4c\x42\x13\x21\xa7\xbb\x97"
  19914. "\x0b\xdc\xe0\xfb\xf0\xd2\xae\x85\x61\xaa\xd8\x71\x7f\x2e\x46\xdf"
  19915. "\xe3\xff\x8d\xea\xb4\xd7\x93\x23\x56\x03\x2c\x15\x13\x0d\x59\x9e"
  19916. "\x26\xc1\x0f\x2f\xec\x96\x30\x31\xac\x69\x38\xa1\x8d\x66\x45\x38"
  19917. "\xb9\x4d\xac\x55\x34\xef\x7b\x59\x94\x24\xd6\x9b\xe1\xf7\x1c\x20";
  19918. vec.msgLen = 128;
  19919. #endif
  19920. vec.Qx = "8a4dca35136c4b70e588e23554637ae251077d1365a6ba5db9585de7";
  19921. vec.Qy = "ad3dee06de0be8279d4af435d7245f14f3b4f82eb578e519ee0057b1";
  19922. vec.d = "97c4b796e1639dd1035b708fc00dc7ba1682cec44a1002a1a820619f";
  19923. vec.R = "147b33758321e722a0360a4719738af848449e2c1d08defebc1671a7";
  19924. vec.S = "24fc7ed7f1352ca3872aa0916191289e2e04d454935d50fe6af3ad5b";
  19925. vec.curveName = "SECP224R1";
  19926. #ifndef NO_ASN
  19927. vec.r = (byte*)"\x14\x7b\x33\x75\x83\x21\xe7\x22\xa0\x36\x0a\x47"
  19928. "\x19\x73\x8a\xf8\x48\x44\x9e\x2c\x1d\x08\xde\xfe"
  19929. "\xbc\x16\x71\xa7";
  19930. vec.rSz = 28;
  19931. vec.s = (byte*)"\x24\xfc\x7e\xd7\xf1\x35\x2c\xa3\x87\x2a\xa0\x91"
  19932. "\x61\x91\x28\x9e\x2e\x04\xd4\x54\x93\x5d\x50\xfe"
  19933. "\x6a\xf3\xad\x5b";
  19934. vec.sSz = 28;
  19935. #endif
  19936. break;
  19937. #endif /* HAVE_ECC224 */
  19938. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  19939. case 30:
  19940. return 0;
  19941. #endif /* HAVE_ECC239 */
  19942. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  19943. case 32:
  19944. /* first [P-256,SHA-1] vector from FIPS 186-3 NIST vectors */
  19945. #if 1
  19946. vec.msg = "\xa3\xf9\x1a\xe2\x1b\xa6\xb3\x03\x98\x64\x47\x2f\x18\x41\x44\xc6\xaf\x62\xcd\x0e";
  19947. vec.msgLen = 20;
  19948. #else
  19949. /* This is the raw message prior to SHA-1 */
  19950. vec.msg =
  19951. "\xa2\x4b\x21\x76\x2e\x6e\xdb\x15\x3c\xc1\x14\x38\xdb\x0e\x92\xcd"
  19952. "\xf5\x2b\x86\xb0\x6c\xa9\x70\x16\x06\x27\x59\xc7\x0d\x36\xd1\x56"
  19953. "\x2c\xc9\x63\x0d\x7f\xc7\xc7\x74\xb2\x8b\x54\xe3\x1e\xf5\x58\x72"
  19954. "\xb2\xa6\x5d\xf1\xd7\xec\x26\xde\xbb\x33\xe7\xd9\x27\xef\xcc\xf4"
  19955. "\x6b\x63\xde\x52\xa4\xf4\x31\xea\xca\x59\xb0\x5d\x2e\xde\xc4\x84"
  19956. "\x5f\xff\xc0\xee\x15\x03\x94\xd6\x1f\x3d\xfe\xcb\xcd\xbf\x6f\x5a"
  19957. "\x73\x38\xd0\xbe\x3f\x2a\x77\x34\x51\x98\x3e\xba\xeb\x48\xf6\x73"
  19958. "\x8f\xc8\x95\xdf\x35\x7e\x1a\x48\xa6\x53\xbb\x35\x5a\x31\xa1\xb4"
  19959. vec.msgLen = 128;
  19960. #endif
  19961. vec.Qx = "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  19962. vec.Qy = "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  19963. vec.d = "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  19964. vec.R = "2b826f5d44e2d0b6de531ad96b51e8f0c56fdfead3c236892e4d84eacfc3b75c";
  19965. vec.S = "a2248b62c03db35a7cd63e8a120a3521a89d3d2f61ff99035a2148ae32e3a248";
  19966. #ifndef NO_ASN
  19967. vec.r = (byte*)"\x2b\x82\x6f\x5d\x44\xe2\xd0\xb6\xde\x53\x1a\xd9"
  19968. "\x6b\x51\xe8\xf0\xc5\x6f\xdf\xea\xd3\xc2\x36\x89"
  19969. "\x2e\x4d\x84\xea\xcf\xc3\xb7\x5c";
  19970. vec.rSz = 32;
  19971. vec.s = (byte*)"\xa2\x24\x8b\x62\xc0\x3d\xb3\x5a\x7c\xd6\x3e\x8a"
  19972. "\x12\x0a\x35\x21\xa8\x9d\x3d\x2f\x61\xff\x99\x03"
  19973. "\x5a\x21\x48\xae\x32\xe3\xa2\x48";
  19974. vec.sSz = 32;
  19975. #endif
  19976. vec.curveName = "SECP256R1";
  19977. break;
  19978. #endif /* !NO_ECC256 */
  19979. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  19980. case 40:
  19981. return 0;
  19982. #endif /* HAVE_ECC320 */
  19983. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  19984. case 48:
  19985. /* first [P-384,SHA-1] vector from FIPS 186-3 NIST vectors */
  19986. #if 1
  19987. vec.msg = "\x9b\x9f\x8c\x95\x35\xa5\xca\x26\x60\x5d\xb7\xf2\xfa\x57\x3b\xdf\xc3\x2e\xab\x8b";
  19988. vec.msgLen = 20;
  19989. #else
  19990. /* This is the raw message prior to SHA-1 */
  19991. vec.msg =
  19992. "\xab\xe1\x0a\xce\x13\xe7\xe1\xd9\x18\x6c\x48\xf7\x88\x9d\x51\x47"
  19993. "\x3d\x3a\x09\x61\x98\x4b\xc8\x72\xdf\x70\x8e\xcc\x3e\xd3\xb8\x16"
  19994. "\x9d\x01\xe3\xd9\x6f\xc4\xf1\xd5\xea\x00\xa0\x36\x92\xbc\xc5\xcf"
  19995. "\xfd\x53\x78\x7c\x88\xb9\x34\xaf\x40\x4c\x03\x9d\x32\x89\xb5\xba"
  19996. "\xc5\xae\x7d\xb1\x49\x68\x75\xb5\xdc\x73\xc3\x09\xf9\x25\xc1\x3d"
  19997. "\x1c\x01\xab\xda\xaf\xeb\xcd\xac\x2c\xee\x43\x39\x39\xce\x8d\x4a"
  19998. "\x0a\x5d\x57\xbb\x70\x5f\x3b\xf6\xec\x08\x47\x95\x11\xd4\xb4\xa3"
  19999. "\x21\x1f\x61\x64\x9a\xd6\x27\x43\x14\xbf\x0d\x43\x8a\x81\xe0\x60"
  20000. vec.msgLen = 128;
  20001. #endif
  20002. vec.Qx = "e55fee6c49d8d523f5ce7bf9c0425ce4ff650708b7de5cfb095901523979a7f042602db30854735369813b5c3f5ef868";
  20003. vec.Qy = "28f59cc5dc509892a988d38a8e2519de3d0c4fd0fbdb0993e38f18506c17606c5e24249246f1ce94983a5361c5be983e";
  20004. vec.d = "a492ce8fa90084c227e1a32f7974d39e9ff67a7e8705ec3419b35fb607582bebd461e0b1520ac76ec2dd4e9b63ebae71";
  20005. vec.R = "6820b8585204648aed63bdff47f6d9acebdea62944774a7d14f0e14aa0b9a5b99545b2daee6b3c74ebf606667a3f39b7";
  20006. vec.S = "491af1d0cccd56ddd520b233775d0bc6b40a6255cc55207d8e9356741f23c96c14714221078dbd5c17f4fdd89b32a907";
  20007. vec.curveName = "SECP384R1";
  20008. #ifndef NO_ASN
  20009. vec.r = (byte*)"\x68\x20\xb8\x58\x52\x04\x64\x8a\xed\x63\xbd\xff"
  20010. "\x47\xf6\xd9\xac\xeb\xde\xa6\x29\x44\x77\x4a\x7d"
  20011. "\x14\xf0\xe1\x4a\xa0\xb9\xa5\xb9\x95\x45\xb2\xda"
  20012. "\xee\x6b\x3c\x74\xeb\xf6\x06\x66\x7a\x3f\x39\xb7";
  20013. vec.rSz = 48;
  20014. vec.s = (byte*)"\x49\x1a\xf1\xd0\xcc\xcd\x56\xdd\xd5\x20\xb2\x33"
  20015. "\x77\x5d\x0b\xc6\xb4\x0a\x62\x55\xcc\x55\x20\x7d"
  20016. "\x8e\x93\x56\x74\x1f\x23\xc9\x6c\x14\x71\x42\x21"
  20017. "\x07\x8d\xbd\x5c\x17\xf4\xfd\xd8\x9b\x32\xa9\x07";
  20018. vec.sSz = 48;
  20019. #endif
  20020. break;
  20021. #endif /* HAVE_ECC384 */
  20022. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  20023. case 64:
  20024. return 0;
  20025. #endif /* HAVE_ECC512 */
  20026. #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
  20027. case 66:
  20028. /* first [P-521,SHA-1] vector from FIPS 186-3 NIST vectors */
  20029. #if 1
  20030. vec.msg = "\x1b\xf7\x03\x9c\xca\x23\x94\x27\x3f\x11\xa1\xd4\x8d\xcc\xb4\x46\x6f\x31\x61\xdf";
  20031. vec.msgLen = 20;
  20032. #else
  20033. /* This is the raw message prior to SHA-1 */
  20034. vec.msg =
  20035. "\x50\x3f\x79\x39\x34\x0a\xc7\x23\xcd\x4a\x2f\x4e\x6c\xcc\x27\x33"
  20036. "\x38\x3a\xca\x2f\xba\x90\x02\x19\x9d\x9e\x1f\x94\x8b\xe0\x41\x21"
  20037. "\x07\xa3\xfd\xd5\x14\xd9\x0c\xd4\xf3\x7c\xc3\xac\x62\xef\x00\x3a"
  20038. "\x2d\xb1\xd9\x65\x7a\xb7\x7f\xe7\x55\xbf\x71\xfa\x59\xe4\xd9\x6e"
  20039. "\xa7\x2a\xe7\xbf\x9d\xe8\x7d\x79\x34\x3b\xc1\xa4\xbb\x14\x4d\x16"
  20040. "\x28\xd1\xe9\xe9\xc8\xed\x80\x8b\x96\x2c\x54\xe5\xf9\x6d\x53\xda"
  20041. "\x14\x7a\x96\x38\xf9\x4a\x91\x75\xd8\xed\x61\x05\x5f\x0b\xa5\x73"
  20042. "\xa8\x2b\xb7\xe0\x18\xee\xda\xc4\xea\x7b\x36\x2e\xc8\x9c\x38\x2b"
  20043. vec.msgLen = 128;
  20044. #endif
  20045. vec.Qx = "12fbcaeffa6a51f3ee4d3d2b51c5dec6d7c726ca353fc014ea2bf7cfbb9b910d32cbfa6a00fe39b6cdb8946f22775398b2e233c0cf144d78c8a7742b5c7a3bb5d23";
  20046. vec.Qy = "09cdef823dd7bf9a79e8cceacd2e4527c231d0ae5967af0958e931d7ddccf2805a3e618dc3039fec9febbd33052fe4c0fee98f033106064982d88f4e03549d4a64d";
  20047. vec.d = "1bd56bd106118eda246155bd43b42b8e13f0a6e25dd3bb376026fab4dc92b6157bc6dfec2d15dd3d0cf2a39aa68494042af48ba9601118da82c6f2108a3a203ad74";
  20048. vec.R = "0bd117b4807710898f9dd7778056485777668f0e78e6ddf5b000356121eb7a220e9493c7f9a57c077947f89ac45d5acb6661bbcd17abb3faea149ba0aa3bb1521be";
  20049. vec.S = "019cd2c5c3f9870ecdeb9b323abdf3a98cd5e231d85c6ddc5b71ab190739f7f226e6b134ba1d5889ddeb2751dabd97911dff90c34684cdbe7bb669b6c3d22f2480c";
  20050. vec.curveName = "SECP521R1";
  20051. #ifndef NO_ASN
  20052. vec.r = (byte*)"\xbd\x11\x7b\x48\x07\x71\x08\x98\xf9\xdd\x77\x78"
  20053. "\x05\x64\x85\x77\x76\x68\xf0\xe7\x8e\x6d\xdf\x5b"
  20054. "\x00\x03\x56\x12\x1e\xb7\xa2\x20\xe9\x49\x3c\x7f"
  20055. "\x9a\x57\xc0\x77\x94\x7f\x89\xac\x45\xd5\xac\xb6"
  20056. "\x66\x1b\xbc\xd1\x7a\xbb\x3f\xae\xa1\x49\xba\x0a"
  20057. "\xa3\xbb\x15\x21\xbe";
  20058. vec.rSz = 65;
  20059. vec.s = (byte*)"\x19\xcd\x2c\x5c\x3f\x98\x70\xec\xde\xb9\xb3\x23"
  20060. "\xab\xdf\x3a\x98\xcd\x5e\x23\x1d\x85\xc6\xdd\xc5"
  20061. "\xb7\x1a\xb1\x90\x73\x9f\x7f\x22\x6e\x6b\x13\x4b"
  20062. "\xa1\xd5\x88\x9d\xde\xb2\x75\x1d\xab\xd9\x79\x11"
  20063. "\xdf\xf9\x0c\x34\x68\x4c\xdb\xe7\xbb\x66\x9b\x6c"
  20064. "\x3d\x22\xf2\x48\x0c";
  20065. vec.sSz = 65;
  20066. #endif
  20067. break;
  20068. #endif /* HAVE_ECC521 */
  20069. default:
  20070. return NOT_COMPILED_IN; /* Invalid key size / Not supported */
  20071. }; /* Switch */
  20072. ret = ecc_test_vector_item(&vec);
  20073. if (ret < 0) {
  20074. return ret;
  20075. }
  20076. return 0;
  20077. }
  20078. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  20079. #if defined(HAVE_ECC_SIGN) && (defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  20080. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)) \
  20081. && (!defined(FIPS_VERSION_GE) || FIPS_VERSION_GE(5,3))
  20082. #if defined(HAVE_ECC256)
  20083. static int ecc_test_deterministic_k(WC_RNG* rng)
  20084. {
  20085. int ret;
  20086. #ifdef WOLFSSL_SMALL_STACK
  20087. ecc_key *key = NULL;
  20088. #else
  20089. ecc_key key[1];
  20090. #endif
  20091. int key_inited = 0;
  20092. byte sig[72];
  20093. word32 sigSz;
  20094. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  20095. unsigned char hash[32];
  20096. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  20097. "C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721";
  20098. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  20099. "60FED4BA255A9D31C961EB74C6356D68C049B8923B61FA6CE669622E60F29FB6";
  20100. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  20101. "7903FE1008B8BC99A41AE9E95628BC64F2F1B20C2D7E9F5177A3C294D4462299";
  20102. WOLFSSL_SMALL_STACK_STATIC const byte expSig[] = {
  20103. 0x30, 0x46, 0x02, 0x21, 0x00, 0xEF, 0xD4, 0x8B,
  20104. 0x2A, 0xAC, 0xB6, 0xA8, 0xFD, 0x11, 0x40, 0xDD,
  20105. 0x9C, 0xD4, 0x5E, 0x81, 0xD6, 0x9D, 0x2C, 0x87,
  20106. 0x7B, 0x56, 0xAA, 0xF9, 0x91, 0xC3, 0x4D, 0x0E,
  20107. 0xA8, 0x4E, 0xAF, 0x37, 0x16, 0x02, 0x21, 0x00,
  20108. 0xF7, 0xCB, 0x1C, 0x94, 0x2D, 0x65, 0x7C, 0x41,
  20109. 0xD4, 0x36, 0xC7, 0xA1, 0xB6, 0xE2, 0x9F, 0x65,
  20110. 0xF3, 0xE9, 0x00, 0xDB, 0xB9, 0xAF, 0xF4, 0x06,
  20111. 0x4D, 0xC4, 0xAB, 0x2F, 0x84, 0x3A, 0xCD, 0xA8
  20112. };
  20113. #ifdef WOLFSSL_SMALL_STACK
  20114. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20115. if (key == NULL)
  20116. return MEMORY_E;
  20117. #endif
  20118. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  20119. if (ret != 0) {
  20120. goto done;
  20121. }
  20122. key_inited = 1;
  20123. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP256R1");
  20124. if (ret != 0) {
  20125. goto done;
  20126. }
  20127. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  20128. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  20129. if (ret != 0) {
  20130. goto done;
  20131. }
  20132. ret = wc_ecc_set_deterministic(key, 1);
  20133. if (ret != 0) {
  20134. goto done;
  20135. }
  20136. sigSz = sizeof(sig);
  20137. do {
  20138. #if defined(WOLFSSL_ASYNC_CRYPT)
  20139. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20140. #endif
  20141. if (ret == 0)
  20142. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  20143. } while (ret == WC_PENDING_E);
  20144. if (ret != 0) {
  20145. goto done;
  20146. }
  20147. TEST_SLEEP();
  20148. if (sigSz != sizeof(expSig)) {
  20149. ret = -9830;
  20150. goto done;
  20151. }
  20152. if (XMEMCMP(sig, expSig, sigSz) != 0) {
  20153. ret = -9831;
  20154. goto done;
  20155. }
  20156. sigSz = sizeof(sig);
  20157. do {
  20158. #if defined(WOLFSSL_ASYNC_CRYPT)
  20159. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20160. #endif
  20161. if (ret == 0)
  20162. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  20163. } while (ret == WC_PENDING_E);
  20164. if (ret != 0) {
  20165. goto done;
  20166. }
  20167. TEST_SLEEP();
  20168. done:
  20169. if (key_inited)
  20170. wc_ecc_free(key);
  20171. #ifdef WOLFSSL_SMALL_STACK
  20172. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20173. #endif
  20174. return ret;
  20175. }
  20176. #endif
  20177. #ifdef WOLFSSL_PUBLIC_MP
  20178. #if defined(HAVE_ECC384)
  20179. /* KAT from RFC6979 */
  20180. static int ecc384_test_deterministic_k(WC_RNG* rng)
  20181. {
  20182. int ret;
  20183. #ifdef WOLFSSL_SMALL_STACK
  20184. ecc_key *key;
  20185. mp_int *r, *s, *expR, *expS;
  20186. #else
  20187. ecc_key key[1];
  20188. mp_int r[1], s[1], expR[1], expS[1];
  20189. #endif
  20190. int key_inited = 0;
  20191. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  20192. unsigned char hash[32];
  20193. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  20194. "6B9D3DAD2E1B8C1C05B19875B6659F4DE23C3B667BF297BA9AA47740787137D8"
  20195. "96D5724E4C70A825F872C9EA60D2EDF5";
  20196. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  20197. "EC3A4E415B4E19A4568618029F427FA5DA9A8BC4AE92E02E06AAE5286B300C64"
  20198. "DEF8F0EA9055866064A254515480BC13";
  20199. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  20200. "8015D9B72D7D57244EA8EF9AC0C621896708A59367F9DFB9F54CA84B3F1C9DB1"
  20201. "288B231C3AE0D4FE7344FD2533264720";
  20202. WOLFSSL_SMALL_STACK_STATIC const char* expRstr =
  20203. "21B13D1E013C7FA1392D03C5F99AF8B30C570C6F98D4EA8E354B63A21D3DAA33"
  20204. "BDE1E888E63355D92FA2B3C36D8FB2CD";
  20205. WOLFSSL_SMALL_STACK_STATIC const char* expSstr =
  20206. "F3AA443FB107745BF4BD77CB3891674632068A10CA67E3D45DB2266FA7D1FEEB"
  20207. "EFDC63ECCD1AC42EC0CB8668A4FA0AB0";
  20208. #ifdef WOLFSSL_SMALL_STACK
  20209. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20210. r = (mp_int *)XMALLOC(sizeof(*r), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20211. s = (mp_int *)XMALLOC(sizeof(*s), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20212. expR = (mp_int *)XMALLOC(sizeof(*expR), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20213. expS = (mp_int *)XMALLOC(sizeof(*expS), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20214. if ((key == NULL) ||
  20215. (r == NULL) ||
  20216. (s == NULL) ||
  20217. (expR == NULL) ||
  20218. (expS == NULL))
  20219. {
  20220. ret = MEMORY_E;
  20221. goto done;
  20222. }
  20223. #endif
  20224. ret = mp_init_multi(r, s, expR, expS, NULL, NULL);
  20225. if (ret != MP_OKAY) {
  20226. goto done;
  20227. }
  20228. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  20229. if (ret != 0) {
  20230. goto done;
  20231. }
  20232. key_inited = 1;
  20233. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP384R1");
  20234. if (ret != 0) {
  20235. goto done;
  20236. }
  20237. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  20238. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  20239. if (ret != 0) {
  20240. goto done;
  20241. }
  20242. ret = wc_ecc_set_deterministic(key, 1);
  20243. if (ret != 0) {
  20244. goto done;
  20245. }
  20246. do {
  20247. #if defined(WOLFSSL_ASYNC_CRYPT)
  20248. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20249. #endif
  20250. if (ret == 0)
  20251. ret = wc_ecc_sign_hash_ex(hash, sizeof(hash), rng, key, r, s);
  20252. } while (ret == WC_PENDING_E);
  20253. if (ret != 0) {
  20254. goto done;
  20255. }
  20256. TEST_SLEEP();
  20257. mp_read_radix(expR, expRstr, MP_RADIX_HEX);
  20258. mp_read_radix(expS, expSstr, MP_RADIX_HEX);
  20259. if (mp_cmp(r, expR) != MP_EQ) {
  20260. ret = -1;
  20261. }
  20262. done:
  20263. if (key_inited)
  20264. wc_ecc_free(key);
  20265. #ifdef WOLFSSL_SMALL_STACK
  20266. if (key != NULL)
  20267. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20268. if (r != NULL)
  20269. XFREE(r, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20270. if (s != NULL)
  20271. XFREE(s, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20272. if (expR != NULL)
  20273. XFREE(expR, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20274. if (expS != NULL)
  20275. XFREE(expS, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20276. #endif
  20277. return ret;
  20278. }
  20279. #endif /* HAVE_ECC384 */
  20280. #if defined(HAVE_ECC521)
  20281. /* KAT from RFC6979 */
  20282. static int ecc521_test_deterministic_k(WC_RNG* rng)
  20283. {
  20284. int ret;
  20285. #ifdef WOLFSSL_SMALL_STACK
  20286. ecc_key *key;
  20287. mp_int *r, *s, *expR, *expS;
  20288. #else
  20289. ecc_key key[1];
  20290. mp_int r[1], s[1], expR[1], expS[1];
  20291. #endif
  20292. int key_inited = 0;
  20293. WOLFSSL_SMALL_STACK_STATIC const unsigned char msg[] = "sample";
  20294. unsigned char hash[32];
  20295. WOLFSSL_SMALL_STACK_STATIC const char* dIUT =
  20296. "0FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75C"
  20297. "AA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83"
  20298. "538";
  20299. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx =
  20300. "1894550D0785932E00EAA23B694F213F8C3121F86DC97A04E5A7167DB4E5BCD3"
  20301. "71123D46E45DB6B5D5370A7F20FB633155D38FFA16D2BD761DCAC474B9A2F502"
  20302. "3A4";
  20303. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy =
  20304. "0493101C962CD4D2FDDF782285E64584139C2F91B47F87FF82354D6630F746A2"
  20305. "8A0DB25741B5B34A828008B22ACC23F924FAAFBD4D33F81EA66956DFEAA2BFDF"
  20306. "CF5";
  20307. WOLFSSL_SMALL_STACK_STATIC const char* expRstr =
  20308. "1511BB4D675114FE266FC4372B87682BAECC01D3CC62CF2303C92B3526012659"
  20309. "D16876E25C7C1E57648F23B73564D67F61C6F14D527D54972810421E7D87589E"
  20310. "1A7";
  20311. WOLFSSL_SMALL_STACK_STATIC const char* expSstr =
  20312. "04A171143A83163D6DF460AAF61522695F207A58B95C0644D87E52AA1A347916"
  20313. "E4F7A72930B1BC06DBE22CE3F58264AFD23704CBB63B29B931F7DE6C9D949A7E"
  20314. "CFC";
  20315. #ifdef WOLFSSL_SMALL_STACK
  20316. key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20317. r = (mp_int *)XMALLOC(sizeof(*r), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20318. s = (mp_int *)XMALLOC(sizeof(*s), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20319. expR = (mp_int *)XMALLOC(sizeof(*expR), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20320. expS = (mp_int *)XMALLOC(sizeof(*expS), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20321. if ((key == NULL) ||
  20322. (r == NULL) ||
  20323. (s == NULL) ||
  20324. (expR == NULL) ||
  20325. (expS == NULL))
  20326. {
  20327. ret = MEMORY_E;
  20328. goto done;
  20329. }
  20330. #endif
  20331. ret = mp_init_multi(r, s, expR, expS, NULL, NULL);
  20332. if (ret != MP_OKAY) {
  20333. goto done;
  20334. }
  20335. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  20336. if (ret != 0) {
  20337. return ret;
  20338. }
  20339. key_inited = 1;
  20340. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP521R1");
  20341. if (ret != 0) {
  20342. goto done;
  20343. }
  20344. ret = wc_Hash(WC_HASH_TYPE_SHA256, msg,
  20345. (word32)XSTRLEN((const char*)msg), hash, sizeof(hash));
  20346. if (ret != 0) {
  20347. goto done;
  20348. }
  20349. ret = wc_ecc_set_deterministic(key, 1);
  20350. if (ret != 0) {
  20351. goto done;
  20352. }
  20353. do {
  20354. #if defined(WOLFSSL_ASYNC_CRYPT)
  20355. ret = wc_AsyncWait(ret, key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20356. #endif
  20357. if (ret == 0)
  20358. ret = wc_ecc_sign_hash_ex(hash, sizeof(hash), rng, key, r, s);
  20359. } while (ret == WC_PENDING_E);
  20360. if (ret != 0) {
  20361. goto done;
  20362. }
  20363. TEST_SLEEP();
  20364. mp_read_radix(expR, expRstr, MP_RADIX_HEX);
  20365. mp_read_radix(expS, expSstr, MP_RADIX_HEX);
  20366. if (mp_cmp(r, expR) != MP_EQ) {
  20367. ret = -1;
  20368. }
  20369. done:
  20370. if (key_inited)
  20371. wc_ecc_free(key);
  20372. #ifdef WOLFSSL_SMALL_STACK
  20373. if (key != NULL)
  20374. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20375. if (r != NULL)
  20376. XFREE(r, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20377. if (s != NULL)
  20378. XFREE(s, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20379. if (expR != NULL)
  20380. XFREE(expR, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20381. if (expS != NULL)
  20382. XFREE(expS, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20383. #endif
  20384. return ret;
  20385. }
  20386. #endif /* HAVE_ECC521 */
  20387. #endif /* WOLFSSL_PUBLIC_MP */
  20388. #endif /* HAVE_ECC_SIGN && (WOLFSSL_ECDSA_DETERMINISTIC_K ||
  20389. WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  20390. && (!FIPS_VERSION_GE || FIPS_VERSION_GE(5,3)) */
  20391. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K) && \
  20392. !defined(WOLFSSL_KCAPI_ECC)
  20393. static int ecc_test_sign_vectors(WC_RNG* rng)
  20394. {
  20395. int ret;
  20396. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20397. ecc_key *key = NULL;
  20398. #else
  20399. ecc_key key[1];
  20400. #endif
  20401. int key_inited = 0;
  20402. byte sig[72];
  20403. word32 sigSz;
  20404. WOLFSSL_SMALL_STACK_STATIC const unsigned char hash[32] = "test wolfSSL deterministic sign";
  20405. WOLFSSL_SMALL_STACK_STATIC const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  20406. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  20407. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  20408. WOLFSSL_SMALL_STACK_STATIC const byte k[1] = { 0x02 };
  20409. WOLFSSL_SMALL_STACK_STATIC const byte expSig[71] = {
  20410. 0x30, 0x45, 0x02, 0x20, 0x7c, 0xf2, 0x7b, 0x18,
  20411. 0x8d, 0x03, 0x4f, 0x7e, 0x8a, 0x52, 0x38, 0x03,
  20412. 0x04, 0xb5, 0x1a, 0xc3, 0xc0, 0x89, 0x69, 0xe2,
  20413. 0x77, 0xf2, 0x1b, 0x35, 0xa6, 0x0b, 0x48, 0xfc,
  20414. 0x47, 0x66, 0x99, 0x78, 0x02, 0x21, 0x00, 0xa8,
  20415. 0x43, 0xa0, 0xce, 0x6c, 0x5e, 0x17, 0x8a, 0x53,
  20416. 0x4d, 0xaf, 0xd2, 0x95, 0x78, 0x9f, 0x84, 0x4f,
  20417. 0x94, 0xb8, 0x75, 0xa3, 0x19, 0xa5, 0xd4, 0xdf,
  20418. 0xe1, 0xd4, 0x5e, 0x9d, 0x97, 0xfe, 0x81
  20419. };
  20420. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20421. if ((key = (ecc_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  20422. return MEMORY_E;
  20423. #endif
  20424. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  20425. if (ret != 0) {
  20426. goto done;
  20427. }
  20428. key_inited = 1;
  20429. ret = wc_ecc_import_raw(key, QIUTx, QIUTy, dIUT, "SECP256R1");
  20430. if (ret != 0) {
  20431. goto done;
  20432. }
  20433. wc_ecc_set_flags(key, WC_ECC_FLAG_DEC_SIGN);
  20434. ret = wc_ecc_sign_set_k(k, sizeof(k), key);
  20435. if (ret != 0) {
  20436. goto done;
  20437. }
  20438. sigSz = sizeof(sig);
  20439. do {
  20440. #if defined(WOLFSSL_ASYNC_CRYPT)
  20441. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20442. #endif
  20443. if (ret == 0)
  20444. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  20445. } while (ret == WC_PENDING_E);
  20446. if (ret != 0) {
  20447. goto done;
  20448. }
  20449. TEST_SLEEP();
  20450. if (sigSz != sizeof(expSig)) {
  20451. ret = -9830;
  20452. goto done;
  20453. }
  20454. if (XMEMCMP(sig, expSig, sigSz) != 0) {
  20455. ret = -9831;
  20456. goto done;
  20457. }
  20458. sigSz = sizeof(sig);
  20459. do {
  20460. #if defined(WOLFSSL_ASYNC_CRYPT)
  20461. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20462. #endif
  20463. if (ret == 0)
  20464. ret = wc_ecc_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key);
  20465. } while (ret == WC_PENDING_E);
  20466. if (ret != 0) {
  20467. goto done;
  20468. }
  20469. TEST_SLEEP();
  20470. done:
  20471. if (key_inited)
  20472. wc_ecc_free(key);
  20473. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20474. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20475. #endif
  20476. return ret;
  20477. }
  20478. #endif
  20479. #ifdef HAVE_ECC_CDH
  20480. static int ecc_test_cdh_vectors(WC_RNG* rng)
  20481. {
  20482. int ret;
  20483. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20484. ecc_key *pub_key = (ecc_key *)XMALLOC(sizeof *pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20485. ecc_key *priv_key = (ecc_key *)XMALLOC(sizeof *priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20486. #else
  20487. ecc_key pub_key[1], priv_key[1];
  20488. #endif
  20489. byte sharedA[32] = {0}, sharedB[32] = {0};
  20490. word32 x, z;
  20491. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSx = "700c48f77f56584c5cc632ca65640db91b6bacce3a4df6b42ce7cc838833d287";
  20492. WOLFSSL_SMALL_STACK_STATIC const char* QCAVSy = "db71e509e3fd9b060ddb20ba5c51dcc5948d46fbf640dfe0441782cab85fa4ac";
  20493. WOLFSSL_SMALL_STACK_STATIC const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  20494. WOLFSSL_SMALL_STACK_STATIC const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  20495. WOLFSSL_SMALL_STACK_STATIC const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  20496. WOLFSSL_SMALL_STACK_STATIC const char* ZIUT = "46fc62106420ff012e54a434fbdd2d25ccc5852060561e68040dd7778997bd7b";
  20497. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20498. if ((pub_key == NULL) ||
  20499. (priv_key == NULL)) {
  20500. ret = MEMORY_E;
  20501. goto done;
  20502. }
  20503. #endif
  20504. XMEMSET(pub_key, 0, sizeof *pub_key);
  20505. XMEMSET(priv_key, 0, sizeof *priv_key);
  20506. /* setup private and public keys */
  20507. ret = wc_ecc_init_ex(pub_key, HEAP_HINT, devId);
  20508. if (ret != 0)
  20509. goto done;
  20510. ret = wc_ecc_init_ex(priv_key, HEAP_HINT, devId);
  20511. if (ret != 0)
  20512. goto done;
  20513. wc_ecc_set_flags(pub_key, WC_ECC_FLAG_COFACTOR);
  20514. wc_ecc_set_flags(priv_key, WC_ECC_FLAG_COFACTOR);
  20515. ret = wc_ecc_import_raw(pub_key, QCAVSx, QCAVSy, NULL, "SECP256R1");
  20516. if (ret != 0)
  20517. goto done;
  20518. ret = wc_ecc_import_raw(priv_key, QIUTx, QIUTy, dIUT, "SECP256R1");
  20519. if (ret != 0)
  20520. goto done;
  20521. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  20522. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  20523. !defined(HAVE_SELFTEST)
  20524. ret = wc_ecc_set_rng(priv_key, rng);
  20525. if (ret != 0)
  20526. goto done;
  20527. #else
  20528. (void)rng;
  20529. #endif
  20530. /* compute ECC Cofactor shared secret */
  20531. x = sizeof(sharedA);
  20532. do {
  20533. #if defined(WOLFSSL_ASYNC_CRYPT)
  20534. ret = wc_AsyncWait(ret, &priv_key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20535. #endif
  20536. if (ret == 0)
  20537. ret = wc_ecc_shared_secret(priv_key, pub_key, sharedA, &x);
  20538. } while (ret == WC_PENDING_E);
  20539. if (ret != 0) {
  20540. goto done;
  20541. }
  20542. TEST_SLEEP();
  20543. /* read in expected Z */
  20544. z = sizeof(sharedB);
  20545. ret = Base16_Decode((const byte*)ZIUT, (word32)XSTRLEN(ZIUT), sharedB, &z);
  20546. if (ret != 0)
  20547. goto done;
  20548. /* compare results */
  20549. if (x != z || XMEMCMP(sharedA, sharedB, x)) {
  20550. ERROR_OUT(-9840, done);
  20551. }
  20552. done:
  20553. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20554. if (priv_key) {
  20555. wc_ecc_free(priv_key);
  20556. XFREE(priv_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20557. }
  20558. if (pub_key) {
  20559. wc_ecc_free(pub_key);
  20560. XFREE(pub_key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20561. }
  20562. #else
  20563. wc_ecc_free(priv_key);
  20564. wc_ecc_free(pub_key);
  20565. #endif
  20566. return ret;
  20567. }
  20568. #endif /* HAVE_ECC_CDH */
  20569. #endif /* HAVE_ECC_VECTOR_TEST */
  20570. #ifdef HAVE_ECC_KEY_IMPORT
  20571. /* returns 0 on success */
  20572. static int ecc_test_make_pub(WC_RNG* rng)
  20573. {
  20574. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20575. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20576. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  20577. ecc_key *pub = (ecc_key *)XMALLOC(sizeof *pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20578. #endif
  20579. byte *exportBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20580. byte *tmp = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20581. #else
  20582. ecc_key key[1];
  20583. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  20584. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  20585. ecc_key pub[1];
  20586. #endif
  20587. byte exportBuf[ECC_BUFSIZE];
  20588. byte tmp[ECC_BUFSIZE];
  20589. #endif
  20590. const byte* msg = (const byte*)"test wolfSSL ECC public gen";
  20591. word32 x;
  20592. word32 tmpSz;
  20593. int ret = 0;
  20594. ecc_point* pubPoint = NULL;
  20595. #ifdef HAVE_ECC_VERIFY
  20596. int verify = 0;
  20597. #endif
  20598. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20599. if ((key == NULL) ||
  20600. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  20601. (pub == NULL) ||
  20602. #endif
  20603. (exportBuf == NULL) ||
  20604. (tmp == NULL))
  20605. ERROR_OUT(MEMORY_E, done);
  20606. #endif
  20607. (void)msg;
  20608. (void)verify;
  20609. (void)exportBuf;
  20610. (void)rng;
  20611. wc_ecc_init_ex(key, HEAP_HINT, devId);
  20612. #ifndef NO_ECC256
  20613. #ifdef USE_CERT_BUFFERS_256
  20614. XMEMCPY(tmp, ecc_key_der_256, (size_t)sizeof_ecc_key_der_256);
  20615. tmpSz = (size_t)sizeof_ecc_key_der_256;
  20616. #else
  20617. {
  20618. XFILE file = XFOPEN(eccKeyDerFile, "rb");
  20619. if (!file) {
  20620. ERROR_OUT(-9850, done);
  20621. }
  20622. tmpSz = (word32)XFREAD(tmp, 1, ECC_BUFSIZE, file);
  20623. XFCLOSE(file);
  20624. }
  20625. #endif /* USE_CERT_BUFFERS_256 */
  20626. /* import private only then test with */
  20627. ret = wc_ecc_import_private_key(tmp, tmpSz, NULL, 0, NULL);
  20628. if (ret == 0) {
  20629. ERROR_OUT(-9851, done);
  20630. }
  20631. ret = wc_ecc_import_private_key(NULL, tmpSz, NULL, 0, key);
  20632. if (ret == 0) {
  20633. ERROR_OUT(-9852, done);
  20634. }
  20635. x = 0;
  20636. ret = wc_EccPrivateKeyDecode(tmp, &x, key, tmpSz);
  20637. if (ret != 0) {
  20638. ERROR_OUT(-9853, done);
  20639. }
  20640. #ifdef HAVE_ECC_KEY_EXPORT
  20641. x = ECC_BUFSIZE;
  20642. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  20643. if (ret != 0) {
  20644. ERROR_OUT(-9854, done);
  20645. }
  20646. /* make private only key */
  20647. wc_ecc_free(key);
  20648. wc_ecc_init_ex(key, HEAP_HINT, devId);
  20649. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  20650. if (ret != 0) {
  20651. ERROR_OUT(-9855, done);
  20652. }
  20653. x = ECC_BUFSIZE;
  20654. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  20655. if (ret == 0) {
  20656. ERROR_OUT(-9856, done);
  20657. }
  20658. #endif /* HAVE_ECC_KEY_EXPORT */
  20659. ret = wc_ecc_make_pub(NULL, NULL);
  20660. if (ret == 0) {
  20661. ERROR_OUT(-9857, done);
  20662. }
  20663. TEST_SLEEP();
  20664. #ifndef WOLFSSL_NO_MALLOC
  20665. pubPoint = wc_ecc_new_point_h(HEAP_HINT);
  20666. if (pubPoint == NULL) {
  20667. ERROR_OUT(-9858, done);
  20668. }
  20669. #if !defined(WOLFSSL_CRYPTOCELL)
  20670. ret = wc_ecc_make_pub(key, pubPoint);
  20671. #if defined(WOLFSSL_ASYNC_CRYPT)
  20672. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  20673. #endif
  20674. if (ret != 0) {
  20675. ERROR_OUT(-9859, done);
  20676. }
  20677. #endif
  20678. TEST_SLEEP();
  20679. #ifdef HAVE_ECC_KEY_EXPORT
  20680. /* export should still fail, is private only key */
  20681. x = ECC_BUFSIZE;
  20682. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  20683. if (ret == 0) {
  20684. ERROR_OUT(-9860, done);
  20685. }
  20686. #endif /* HAVE_ECC_KEY_EXPORT */
  20687. #endif /* !WOLFSSL_NO_MALLOC */
  20688. #endif /* !NO_ECC256 */
  20689. /* create a new key since above test for loading key is not supported */
  20690. #if defined(WOLFSSL_CRYPTOCELL) || defined(NO_ECC256) || \
  20691. defined(WOLFSSL_QNX_CAAM) || defined(WOLFSSL_SE050) || \
  20692. defined(WOLFSSL_SECO_CAAM)
  20693. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  20694. if (ret != 0) {
  20695. ERROR_OUT(-9861, done);
  20696. }
  20697. #endif
  20698. #if defined(HAVE_ECC_SIGN) && (!defined(ECC_TIMING_RESISTANT) || \
  20699. (defined(ECC_TIMING_RESISTANT) && !defined(WC_NO_RNG))) && \
  20700. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(HAVE_ECC_DETERMINISTIC_K)
  20701. tmpSz = ECC_BUFSIZE;
  20702. ret = 0;
  20703. do {
  20704. #if defined(WOLFSSL_ASYNC_CRYPT)
  20705. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20706. #endif
  20707. if (ret == 0) {
  20708. ret = wc_ecc_sign_hash(msg, (word32)XSTRLEN((const char* )msg), tmp,
  20709. &tmpSz, rng, key);
  20710. }
  20711. } while (ret == WC_PENDING_E);
  20712. if (ret != 0) {
  20713. ERROR_OUT(-9862, done);
  20714. }
  20715. TEST_SLEEP();
  20716. #ifdef HAVE_ECC_VERIFY
  20717. /* try verify with private only key */
  20718. ret = 0;
  20719. do {
  20720. #if defined(WOLFSSL_ASYNC_CRYPT)
  20721. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20722. #endif
  20723. if (ret == 0) {
  20724. ret = wc_ecc_verify_hash(tmp, tmpSz, msg,
  20725. (word32)XSTRLEN((const char*)msg), &verify, key);
  20726. }
  20727. } while (ret == WC_PENDING_E);
  20728. if (ret != 0) {
  20729. ERROR_OUT(-9863, done);
  20730. }
  20731. if (verify != 1) {
  20732. ERROR_OUT(-9864, done);
  20733. }
  20734. TEST_SLEEP();
  20735. #ifdef HAVE_ECC_KEY_EXPORT
  20736. /* exporting the public part should now work */
  20737. x = ECC_BUFSIZE;
  20738. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  20739. if (ret != 0) {
  20740. ERROR_OUT(-9865, done);
  20741. }
  20742. #endif /* HAVE_ECC_KEY_EXPORT */
  20743. #endif /* HAVE_ECC_VERIFY */
  20744. #endif /* HAVE_ECC_SIGN */
  20745. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  20746. /* now test private only key with creating a shared secret */
  20747. x = ECC_BUFSIZE;
  20748. ret = wc_ecc_export_private_only(key, exportBuf, &x);
  20749. if (ret != 0) {
  20750. ERROR_OUT(-9866, done);
  20751. }
  20752. #if !defined(WOLFSSL_QNX_CAAM) && !defined(WOLFSSL_SE050)
  20753. /* make private only key */
  20754. wc_ecc_free(key);
  20755. wc_ecc_init_ex(key, HEAP_HINT, devId);
  20756. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, key);
  20757. if (ret != 0) {
  20758. ERROR_OUT(-9867, done);
  20759. }
  20760. /* check that public export fails with private only key */
  20761. x = ECC_BUFSIZE;
  20762. ret = wc_ecc_export_x963_ex(key, exportBuf, &x, 0);
  20763. if (ret == 0) {
  20764. ERROR_OUT(-9868, done);
  20765. }
  20766. #endif /* WOLFSSL_QNX_CAAM */
  20767. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  20768. /* make public key for shared secret */
  20769. wc_ecc_init_ex(pub, HEAP_HINT, devId);
  20770. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, pub);
  20771. #ifdef HAVE_ECC_CDH
  20772. wc_ecc_set_flags(key, WC_ECC_FLAG_COFACTOR);
  20773. #endif
  20774. #if defined(WOLFSSL_ASYNC_CRYPT)
  20775. ret = wc_AsyncWait(ret, &pub->asyncDev, WC_ASYNC_FLAG_NONE);
  20776. #endif
  20777. if (ret != 0) {
  20778. ERROR_OUT(-9869, done);
  20779. }
  20780. TEST_SLEEP();
  20781. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  20782. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  20783. !defined(HAVE_SELFTEST)
  20784. ret = wc_ecc_set_rng(key, rng);
  20785. if (ret != 0)
  20786. goto done;
  20787. #endif
  20788. x = ECC_BUFSIZE;
  20789. do {
  20790. #if defined(WOLFSSL_ASYNC_CRYPT)
  20791. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  20792. #endif
  20793. if (ret == 0) {
  20794. ret = wc_ecc_shared_secret(key, pub, exportBuf, &x);
  20795. }
  20796. } while (ret == WC_PENDING_E);
  20797. wc_ecc_free(pub);
  20798. if (ret != 0) {
  20799. ERROR_OUT(-9870, done);
  20800. }
  20801. TEST_SLEEP();
  20802. #endif /* HAVE_ECC_DHE && HAVE_ECC_KEY_EXPORT && !WC_NO_RNG */
  20803. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  20804. ret = 0;
  20805. done:
  20806. wc_ecc_del_point_h(pubPoint, HEAP_HINT);
  20807. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20808. if (key != NULL) {
  20809. wc_ecc_free(key);
  20810. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20811. }
  20812. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  20813. if (pub != NULL)
  20814. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20815. #endif
  20816. if (exportBuf != NULL)
  20817. XFREE(exportBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20818. if (tmp != NULL)
  20819. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20820. #else
  20821. wc_ecc_free(key);
  20822. #endif
  20823. return ret;
  20824. }
  20825. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && \
  20826. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  20827. static int ecc_test_key_decode(WC_RNG* rng, int keySize)
  20828. {
  20829. int ret;
  20830. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20831. ecc_key *eccKey = (ecc_key *)XMALLOC(sizeof *eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20832. byte *tmpBuf = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20833. #else
  20834. ecc_key eccKey[1];
  20835. byte tmpBuf[ECC_BUFSIZE];
  20836. #endif
  20837. word32 tmpSz;
  20838. word32 idx;
  20839. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20840. if ((eccKey == NULL) || (tmpBuf == NULL))
  20841. ERROR_OUT(MEMORY_E, done);
  20842. #endif
  20843. ret = wc_ecc_init(eccKey);
  20844. if (ret != 0) {
  20845. goto done;
  20846. }
  20847. ret = wc_ecc_make_key(rng, keySize, eccKey);
  20848. #if defined(WOLFSSL_ASYNC_CRYPT)
  20849. ret = wc_AsyncWait(ret, &eccKey->asyncDev, WC_ASYNC_FLAG_NONE);
  20850. #endif
  20851. if (ret != 0) {
  20852. goto done;
  20853. }
  20854. tmpSz = ECC_BUFSIZE;
  20855. ret = wc_EccKeyToDer(eccKey, tmpBuf, tmpSz);
  20856. wc_ecc_free(eccKey);
  20857. if (ret < 0) {
  20858. goto done;
  20859. }
  20860. tmpSz = ret;
  20861. ret = wc_ecc_init(eccKey);
  20862. if (ret != 0) {
  20863. goto done;
  20864. }
  20865. idx = 0;
  20866. ret = wc_EccPrivateKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  20867. if (ret != 0) {
  20868. goto done;
  20869. }
  20870. wc_ecc_free(eccKey);
  20871. ret = wc_ecc_init(eccKey);
  20872. if (ret != 0) {
  20873. goto done;
  20874. }
  20875. idx = 0;
  20876. ret = wc_EccPublicKeyDecode(tmpBuf, &idx, eccKey, tmpSz);
  20877. if (ret != 0) {
  20878. goto done;
  20879. }
  20880. ret = 0;
  20881. done:
  20882. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20883. if (eccKey != NULL) {
  20884. wc_ecc_free(eccKey);
  20885. XFREE(eccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20886. }
  20887. if (tmpBuf != NULL)
  20888. XFREE(tmpBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20889. #else
  20890. wc_ecc_free(eccKey);
  20891. #endif
  20892. return ret;
  20893. }
  20894. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  20895. #endif /* HAVE_ECC_KEY_IMPORT */
  20896. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && \
  20897. !defined(WC_NO_RNG) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  20898. static int ecc_test_key_gen(WC_RNG* rng, int keySize)
  20899. {
  20900. int ret = 0;
  20901. int derSz;
  20902. #ifdef HAVE_PKCS8
  20903. word32 pkcs8Sz;
  20904. #endif
  20905. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20906. byte *der = (byte *)XMALLOC(ECC_BUFSIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20907. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20908. #else
  20909. byte der[ECC_BUFSIZE];
  20910. ecc_key userA[1];
  20911. #endif
  20912. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20913. if ((der == NULL) || (userA == NULL))
  20914. ERROR_OUT(MEMORY_E, done);
  20915. #endif
  20916. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  20917. if (ret != 0)
  20918. goto done;
  20919. ret = wc_ecc_make_key(rng, keySize, userA);
  20920. #if defined(WOLFSSL_ASYNC_CRYPT)
  20921. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  20922. #endif
  20923. if (ret != 0)
  20924. goto done;
  20925. TEST_SLEEP();
  20926. ret = wc_ecc_check_key(userA);
  20927. if (ret != 0)
  20928. goto done;
  20929. TEST_SLEEP();
  20930. derSz = wc_EccKeyToDer(userA, der, ECC_BUFSIZE);
  20931. if (derSz < 0) {
  20932. ERROR_OUT(derSz, done);
  20933. }
  20934. ret = SaveDerAndPem(der, derSz, eccCaKeyTempFile, eccCaKeyPemFile,
  20935. ECC_PRIVATEKEY_TYPE, -8347);
  20936. if (ret != 0) {
  20937. goto done;
  20938. }
  20939. /* test export of public key */
  20940. derSz = wc_EccPublicKeyToDer(userA, der, ECC_BUFSIZE, 1);
  20941. if (derSz < 0) {
  20942. ERROR_OUT(derSz, done);
  20943. }
  20944. if (derSz == 0) {
  20945. ERROR_OUT(-9890, done);
  20946. }
  20947. #ifdef HAVE_COMP_KEY
  20948. /* test export of compressed public key */
  20949. derSz = wc_EccPublicKeyToDer_ex(userA, der, ECC_BUFSIZE, 1, 1);
  20950. if (derSz < 0) {
  20951. ERROR_OUT(derSz, done);
  20952. }
  20953. if (derSz == 0) {
  20954. ERROR_OUT(-9890, done);
  20955. }
  20956. #endif
  20957. ret = SaveDerAndPem(der, derSz, eccPubKeyDerFile, NULL, 0, -8348);
  20958. if (ret != 0) {
  20959. goto done;
  20960. }
  20961. #ifdef HAVE_PKCS8
  20962. /* test export of PKCS#8 unencrypted private key */
  20963. pkcs8Sz = FOURK_BUF;
  20964. derSz = wc_EccPrivateKeyToPKCS8(userA, der, &pkcs8Sz);
  20965. if (derSz < 0) {
  20966. ERROR_OUT(derSz, done);
  20967. }
  20968. if (derSz == 0) {
  20969. ERROR_OUT(-9891, done);
  20970. }
  20971. ret = SaveDerAndPem(der, derSz, eccPkcs8KeyDerFile, NULL, 0, -8349);
  20972. if (ret != 0) {
  20973. goto done;
  20974. }
  20975. #endif /* HAVE_PKCS8 */
  20976. done:
  20977. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  20978. if (der != NULL)
  20979. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20980. if (userA != NULL) {
  20981. wc_ecc_free(userA);
  20982. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  20983. }
  20984. #else
  20985. wc_ecc_free(userA);
  20986. #endif
  20987. return ret;
  20988. }
  20989. #endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
  20990. static int ecc_test_curve_size(WC_RNG* rng, int keySize, int testVerifyCount,
  20991. int curve_id, const ecc_set_type* dp)
  20992. {
  20993. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG) && \
  20994. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  20995. WC_DECLARE_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  20996. WC_DECLARE_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  20997. #endif
  20998. #ifdef HAVE_ECC_KEY_EXPORT
  20999. #define ECC_KEY_EXPORT_BUF_SIZE (MAX_ECC_BYTES * 2 + 32)
  21000. WC_DECLARE_VAR(exportBuf, byte, ECC_KEY_EXPORT_BUF_SIZE, HEAP_HINT);
  21001. #endif
  21002. word32 x = 0;
  21003. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG) && \
  21004. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  21005. word32 y;
  21006. #endif
  21007. #if defined(HAVE_ECC_SIGN) && !defined(WOLFSSL_KCAPI_ECC)
  21008. WC_DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  21009. WC_DECLARE_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  21010. int i;
  21011. #ifdef HAVE_ECC_VERIFY
  21012. int verify;
  21013. #endif /* HAVE_ECC_VERIFY */
  21014. #endif /* HAVE_ECC_SIGN */
  21015. int ret;
  21016. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21017. ecc_key *userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21018. ecc_key *userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21019. ecc_key *pubKey = (ecc_key *)XMALLOC(sizeof *pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21020. #else
  21021. ecc_key userA[1];
  21022. ecc_key userB[1];
  21023. ecc_key pubKey[1];
  21024. #endif
  21025. #ifndef WC_NO_RNG
  21026. int curveSize;
  21027. #endif
  21028. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  21029. #if (defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)) && !defined(WC_NO_RNG) && \
  21030. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  21031. if (sharedA == NULL || sharedB == NULL)
  21032. ERROR_OUT(-9900, done);
  21033. #endif
  21034. #ifdef HAVE_ECC_KEY_EXPORT
  21035. if (exportBuf == NULL)
  21036. ERROR_OUT(-9901, done);
  21037. #endif
  21038. #if defined(HAVE_ECC_SIGN) && !defined(WOLFSSL_KCAPI_ECC)
  21039. if (sig == NULL || digest == NULL)
  21040. ERROR_OUT(-9902, done);
  21041. #endif
  21042. #endif /* WOLFSSL_SMALL_STACK && !WOLFSSL_NO_MALLOC */
  21043. (void)testVerifyCount;
  21044. (void)dp;
  21045. (void)x;
  21046. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21047. if ((userA == NULL) ||
  21048. (userB == NULL) ||
  21049. (pubKey == NULL))
  21050. ERROR_OUT(-9903, done);
  21051. #endif
  21052. XMEMSET(userA, 0, sizeof *userA);
  21053. XMEMSET(userB, 0, sizeof *userB);
  21054. XMEMSET(pubKey, 0, sizeof *pubKey);
  21055. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  21056. if (ret != 0)
  21057. ERROR_OUT(-9904, done);
  21058. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  21059. if (ret != 0)
  21060. ERROR_OUT(-9905, done);
  21061. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  21062. if (ret != 0)
  21063. ERROR_OUT(-9906, done);
  21064. #ifdef WOLFSSL_CUSTOM_CURVES
  21065. if (dp != NULL) {
  21066. ret = wc_ecc_set_custom_curve(userA, dp);
  21067. if (ret != 0)
  21068. ERROR_OUT(-9907, done);
  21069. ret = wc_ecc_set_custom_curve(userB, dp);
  21070. if (ret != 0)
  21071. ERROR_OUT(-9908, done);
  21072. }
  21073. #endif
  21074. #ifndef WC_NO_RNG
  21075. ret = wc_ecc_make_key_ex(rng, keySize, userA, curve_id);
  21076. #if defined(WOLFSSL_ASYNC_CRYPT)
  21077. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  21078. #endif
  21079. #ifdef WOLF_CRYPTO_CB_ONLY_ECC
  21080. if (ret == NO_VALID_DEVID) {
  21081. ret = 0;
  21082. goto done; /* no software case */
  21083. }
  21084. #endif
  21085. if (ret == ECC_CURVE_OID_E)
  21086. goto done; /* catch case, where curve is not supported */
  21087. if (ret != 0)
  21088. ERROR_OUT(-9910, done);
  21089. TEST_SLEEP();
  21090. if (wc_ecc_get_curve_idx(curve_id) != -1) {
  21091. curveSize = wc_ecc_get_curve_size_from_id(userA->dp->id);
  21092. if (curveSize != userA->dp->size)
  21093. ERROR_OUT(-9911, done);
  21094. }
  21095. ret = wc_ecc_check_key(userA);
  21096. if (ret != 0)
  21097. ERROR_OUT(-9912, done);
  21098. TEST_SLEEP();
  21099. /* ATECC508/608 configuration may not support more than one ECDH key */
  21100. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  21101. ret = wc_ecc_make_key_ex(rng, keySize, userB, curve_id);
  21102. #if defined(WOLFSSL_ASYNC_CRYPT)
  21103. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  21104. #endif
  21105. if (ret != 0)
  21106. ERROR_OUT(-9914, done);
  21107. TEST_SLEEP();
  21108. /* only perform the below tests if the key size matches */
  21109. if (dp == NULL && keySize > 0 && wc_ecc_size(userA) != keySize)
  21110. ERROR_OUT(ECC_CURVE_OID_E, done);
  21111. #ifdef HAVE_ECC_DHE
  21112. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21113. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21114. !defined(HAVE_SELFTEST)
  21115. ret = wc_ecc_set_rng(userA, rng);
  21116. if (ret != 0)
  21117. ERROR_OUT(-9915, done);
  21118. ret = wc_ecc_set_rng(userB, rng);
  21119. if (ret != 0)
  21120. ERROR_OUT(-9916, done);
  21121. #endif
  21122. x = ECC_SHARED_SIZE;
  21123. do {
  21124. #if defined(WOLFSSL_ASYNC_CRYPT)
  21125. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21126. #endif
  21127. if (ret == 0)
  21128. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  21129. } while (ret == WC_PENDING_E);
  21130. if (ret != 0) {
  21131. ERROR_OUT(-9917, done);
  21132. }
  21133. TEST_SLEEP();
  21134. y = ECC_SHARED_SIZE;
  21135. do {
  21136. #if defined(WOLFSSL_ASYNC_CRYPT)
  21137. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21138. #endif
  21139. if (ret == 0)
  21140. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  21141. } while (ret == WC_PENDING_E);
  21142. if (ret != 0)
  21143. ERROR_OUT(-9918, done);
  21144. if (y != x)
  21145. ERROR_OUT(-9919, done);
  21146. if (XMEMCMP(sharedA, sharedB, x))
  21147. ERROR_OUT(-9920, done);
  21148. TEST_SLEEP();
  21149. #endif /* HAVE_ECC_DHE */
  21150. #ifdef HAVE_ECC_CDH
  21151. /* add cofactor flag */
  21152. wc_ecc_set_flags(userA, WC_ECC_FLAG_COFACTOR);
  21153. wc_ecc_set_flags(userB, WC_ECC_FLAG_COFACTOR);
  21154. x = ECC_SHARED_SIZE;
  21155. do {
  21156. #if defined(WOLFSSL_ASYNC_CRYPT)
  21157. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21158. #endif
  21159. if (ret == 0)
  21160. ret = wc_ecc_shared_secret(userA, userB, sharedA, &x);
  21161. } while (ret == WC_PENDING_E);
  21162. if (ret != 0)
  21163. ERROR_OUT(-9921, done);
  21164. TEST_SLEEP();
  21165. y = ECC_SHARED_SIZE;
  21166. do {
  21167. #if defined(WOLFSSL_ASYNC_CRYPT)
  21168. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21169. #endif
  21170. if (ret == 0)
  21171. ret = wc_ecc_shared_secret(userB, userA, sharedB, &y);
  21172. } while (ret == WC_PENDING_E);
  21173. if (ret != 0)
  21174. ERROR_OUT(-9922, done);
  21175. if (y != x)
  21176. ERROR_OUT(-9923, done);
  21177. if (XMEMCMP(sharedA, sharedB, x))
  21178. ERROR_OUT(-9924, done);
  21179. TEST_SLEEP();
  21180. /* remove cofactor flag */
  21181. wc_ecc_set_flags(userA, 0);
  21182. wc_ecc_set_flags(userB, 0);
  21183. #endif /* HAVE_ECC_CDH */
  21184. #endif /* !WOLFSSL_ATECC508A && WOLFSSL_ATECC608A */
  21185. #ifdef HAVE_ECC_KEY_EXPORT
  21186. x = ECC_KEY_EXPORT_BUF_SIZE;
  21187. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 0);
  21188. if (ret != 0)
  21189. ERROR_OUT(-9925, done);
  21190. #ifdef HAVE_ECC_KEY_IMPORT
  21191. #ifdef WOLFSSL_CUSTOM_CURVES
  21192. if (dp != NULL) {
  21193. ret = wc_ecc_set_custom_curve(pubKey, dp);
  21194. if (ret != 0)
  21195. ERROR_OUT(-9926, done);
  21196. }
  21197. #endif
  21198. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  21199. if (ret != 0)
  21200. ERROR_OUT(-9927, done);
  21201. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  21202. #ifdef HAVE_ECC_DHE
  21203. y = ECC_SHARED_SIZE;
  21204. do {
  21205. #if defined(WOLFSSL_ASYNC_CRYPT)
  21206. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21207. #endif
  21208. if (ret == 0)
  21209. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  21210. } while (ret == WC_PENDING_E);
  21211. if (ret != 0)
  21212. ERROR_OUT(-9928, done);
  21213. if (XMEMCMP(sharedA, sharedB, y))
  21214. ERROR_OUT(-9929, done);
  21215. TEST_SLEEP();
  21216. #endif /* HAVE_ECC_DHE */
  21217. #ifdef HAVE_COMP_KEY
  21218. /* try compressed export / import too */
  21219. x = ECC_KEY_EXPORT_BUF_SIZE;
  21220. ret = wc_ecc_export_x963_ex(userA, exportBuf, &x, 1);
  21221. if (ret != 0)
  21222. ERROR_OUT(-9930, done);
  21223. wc_ecc_free(pubKey);
  21224. ret = wc_ecc_init_ex(pubKey, HEAP_HINT, devId);
  21225. if (ret != 0)
  21226. ERROR_OUT(-9931, done);
  21227. #ifdef WOLFSSL_CUSTOM_CURVES
  21228. if (dp != NULL) {
  21229. ret = wc_ecc_set_custom_curve(pubKey, dp);
  21230. if (ret != 0)
  21231. ERROR_OUT(-9932, done);
  21232. }
  21233. #endif
  21234. ret = wc_ecc_import_x963_ex(exportBuf, x, pubKey, curve_id);
  21235. if (ret != 0)
  21236. ERROR_OUT(-9933, done);
  21237. #ifdef HAVE_ECC_DHE
  21238. y = ECC_SHARED_SIZE;
  21239. do {
  21240. #if defined(WOLFSSL_ASYNC_CRYPT)
  21241. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21242. #endif
  21243. if (ret == 0)
  21244. ret = wc_ecc_shared_secret(userB, pubKey, sharedB, &y);
  21245. } while (ret == WC_PENDING_E);
  21246. if (ret != 0)
  21247. ERROR_OUT(-9934, done);
  21248. if (XMEMCMP(sharedA, sharedB, y))
  21249. ERROR_OUT(-9935, done);
  21250. TEST_SLEEP();
  21251. #endif /* HAVE_ECC_DHE */
  21252. #endif /* HAVE_COMP_KEY */
  21253. #endif /* !WOLFSSL_ATECC508A && !WOLFSSL_ATECC608A */
  21254. #endif /* !WC_NO_RNG */
  21255. #endif /* HAVE_ECC_KEY_IMPORT */
  21256. #endif /* HAVE_ECC_KEY_EXPORT */
  21257. /* For KCAPI cannot sign using generated ECDH key */
  21258. #if !defined(ECC_TIMING_RESISTANT) || (defined(ECC_TIMING_RESISTANT) && \
  21259. !defined(WC_NO_RNG) && !defined(WOLFSSL_KCAPI_ECC))
  21260. #ifdef HAVE_ECC_SIGN
  21261. /* ECC w/out Shamir has issue with all 0 digest */
  21262. /* WC_BIGINT doesn't have 0 len well on hardware */
  21263. /* Cryptocell has issues with all 0 digest */
  21264. #if defined(ECC_SHAMIR) && !defined(WOLFSSL_ASYNC_CRYPT) && \
  21265. !defined(WOLFSSL_CRYPTOCELL)
  21266. /* test DSA sign hash with zeros */
  21267. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  21268. digest[i] = 0;
  21269. }
  21270. x = ECC_SIG_SIZE;
  21271. do {
  21272. #if defined(WOLFSSL_ASYNC_CRYPT)
  21273. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21274. #endif
  21275. if (ret == 0)
  21276. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng,
  21277. userA);
  21278. } while (ret == WC_PENDING_E);
  21279. if (ret != 0)
  21280. ERROR_OUT(-9936, done);
  21281. TEST_SLEEP();
  21282. #ifdef HAVE_ECC_VERIFY
  21283. for (i=0; i<testVerifyCount; i++) {
  21284. verify = 0;
  21285. do {
  21286. #if defined(WOLFSSL_ASYNC_CRYPT)
  21287. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21288. #endif
  21289. if (ret == 0)
  21290. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE,
  21291. &verify, userA);
  21292. } while (ret == WC_PENDING_E);
  21293. if (ret != 0)
  21294. ERROR_OUT(-9937, done);
  21295. if (verify != 1)
  21296. ERROR_OUT(-9938, done);
  21297. TEST_SLEEP();
  21298. }
  21299. #endif /* HAVE_ECC_VERIFY */
  21300. #endif /* ECC_SHAMIR && !WOLFSSL_ASYNC_CRYPT && !WOLFSSL_CRYPTOCELL */
  21301. /* test DSA sign hash with sequence (0,1,2,3,4,...) */
  21302. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  21303. digest[i] = (byte)i;
  21304. }
  21305. x = ECC_SIG_SIZE;
  21306. do {
  21307. #if defined(WOLFSSL_ASYNC_CRYPT)
  21308. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21309. #endif
  21310. if (ret == 0)
  21311. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng, userA);
  21312. } while (ret == WC_PENDING_E);
  21313. if (ret != 0)
  21314. ERROR_OUT(-9939, done);
  21315. TEST_SLEEP();
  21316. #ifdef HAVE_ECC_VERIFY
  21317. for (i=0; i<testVerifyCount; i++) {
  21318. verify = 0;
  21319. do {
  21320. #if defined(WOLFSSL_ASYNC_CRYPT)
  21321. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21322. #endif
  21323. if (ret == 0)
  21324. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE, &verify, userA);
  21325. } while (ret == WC_PENDING_E);
  21326. if (ret != 0)
  21327. ERROR_OUT(-9940, done);
  21328. if (verify != 1)
  21329. ERROR_OUT(-9941, done);
  21330. TEST_SLEEP();
  21331. }
  21332. #endif /* HAVE_ECC_VERIFY */
  21333. #endif /* HAVE_ECC_SIGN */
  21334. #endif /* !ECC_TIMING_RESISTANT || (ECC_TIMING_RESISTANT &&
  21335. * !WC_NO_RNG && !WOLFSSL_KCAPI_ECC) */
  21336. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG) && \
  21337. !defined(WOLFSSL_ATECC508) && !defined(WOLFSSL_ATECC608A) && \
  21338. !defined(WOLFSSL_KCAPI_ECC)
  21339. x = ECC_KEY_EXPORT_BUF_SIZE;
  21340. ret = wc_ecc_export_private_only(userA, exportBuf, &x);
  21341. if (ret != 0)
  21342. ERROR_OUT(-9942, done);
  21343. #elif defined(HAVE_ECC_KEY_EXPORT)
  21344. (void)exportBuf;
  21345. #endif /* HAVE_ECC_KEY_EXPORT */
  21346. done:
  21347. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21348. if (userA != NULL) {
  21349. wc_ecc_free(userA);
  21350. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21351. }
  21352. if (userB != NULL) {
  21353. wc_ecc_free(userB);
  21354. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21355. }
  21356. if (pubKey != NULL) {
  21357. wc_ecc_free(pubKey);
  21358. XFREE(pubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21359. }
  21360. #else
  21361. wc_ecc_free(pubKey);
  21362. wc_ecc_free(userB);
  21363. wc_ecc_free(userA);
  21364. #endif
  21365. #if defined(HAVE_ECC_DHE) || defined(HAVE_ECC_CDH)
  21366. WC_FREE_VAR(sharedA, HEAP_HINT);
  21367. WC_FREE_VAR(sharedB, HEAP_HINT);
  21368. #endif
  21369. #ifdef HAVE_ECC_KEY_EXPORT
  21370. WC_FREE_VAR(exportBuf, HEAP_HINT);
  21371. #endif
  21372. #ifdef HAVE_ECC_SIGN
  21373. WC_FREE_VAR(sig, HEAP_HINT);
  21374. WC_FREE_VAR(digest, HEAP_HINT);
  21375. #endif
  21376. (void)keySize;
  21377. (void)curve_id;
  21378. (void)rng;
  21379. return ret;
  21380. }
  21381. #undef ECC_TEST_VERIFY_COUNT
  21382. #define ECC_TEST_VERIFY_COUNT 2
  21383. static int ecc_test_curve(WC_RNG* rng, int keySize)
  21384. {
  21385. int ret;
  21386. ret = ecc_test_curve_size(rng, keySize, ECC_TEST_VERIFY_COUNT,
  21387. ECC_CURVE_DEF, NULL);
  21388. if (ret < 0) {
  21389. if (ret == ECC_CURVE_OID_E) {
  21390. /* ignore error for curves not found */
  21391. /* some curve sizes are only available with:
  21392. HAVE_ECC_SECPR2, HAVE_ECC_SECPR3, HAVE_ECC_BRAINPOOL
  21393. and HAVE_ECC_KOBLITZ */
  21394. }
  21395. else {
  21396. printf("ecc_test_curve_size %d failed!: %d\n", keySize, ret);
  21397. return ret;
  21398. }
  21399. }
  21400. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  21401. #ifdef HAVE_ECC_VECTOR_TEST
  21402. ret = ecc_test_vector(keySize);
  21403. if (ret < 0) {
  21404. printf("ecc_test_vector %d failed!: %d\n", keySize, ret);
  21405. return ret;
  21406. }
  21407. #endif
  21408. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  21409. !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  21410. ret = ecc_test_key_decode(rng, keySize);
  21411. if (ret < 0) {
  21412. if (ret == ECC_CURVE_OID_E) {
  21413. /* ignore error for curves not found */
  21414. }
  21415. else {
  21416. printf("ecc_test_key_decode %d failed!: %d\n", keySize, ret);
  21417. return ret;
  21418. }
  21419. }
  21420. #endif
  21421. #if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) && !defined(WC_NO_RNG)
  21422. ret = ecc_test_key_gen(rng, keySize);
  21423. if (ret < 0) {
  21424. if (ret == ECC_CURVE_OID_E) {
  21425. /* ignore error for curves not found */
  21426. }
  21427. else {
  21428. printf("ecc_test_key_gen %d failed!: %d\n", keySize, ret);
  21429. return ret;
  21430. }
  21431. }
  21432. #endif
  21433. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  21434. return 0;
  21435. }
  21436. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  21437. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  21438. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  21439. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21440. static int ecc_point_test(void)
  21441. {
  21442. int ret;
  21443. ecc_point* point;
  21444. ecc_point* point2;
  21445. #ifdef HAVE_COMP_KEY
  21446. ecc_point* point3;
  21447. ecc_point* point4;
  21448. #endif
  21449. word32 outLen;
  21450. byte out[65];
  21451. byte der[] = { 0x04, /* = Uncompressed */
  21452. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21453. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21454. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21455. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21456. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21457. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21458. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21459. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  21460. #if defined(HAVE_COMP_KEY) && (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  21461. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  21462. byte derComp0[] = { 0x02, /* = Compressed, y even */
  21463. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21464. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21465. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21466. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  21467. byte derComp1[] = { 0x03, /* = Compressed, y odd */
  21468. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21469. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21470. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  21471. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  21472. #endif
  21473. byte altDer[] = { 0x04, /* = Uncompressed */
  21474. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21475. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21476. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21477. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21478. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21479. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21480. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  21481. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
  21482. int curve_idx = wc_ecc_get_curve_idx(ECC_SECP256R1);
  21483. /* if curve P256 is not enabled then test should not fail */
  21484. if (curve_idx == ECC_CURVE_INVALID)
  21485. return 0;
  21486. outLen = sizeof(out);
  21487. point = wc_ecc_new_point();
  21488. if (point == NULL)
  21489. return -10000;
  21490. point2 = wc_ecc_new_point();
  21491. if (point2 == NULL) {
  21492. wc_ecc_del_point(point);
  21493. return -10001;
  21494. }
  21495. #ifdef HAVE_COMP_KEY
  21496. point3 = wc_ecc_new_point();
  21497. if (point3 == NULL) {
  21498. wc_ecc_del_point(point2);
  21499. wc_ecc_del_point(point);
  21500. return -10002;
  21501. }
  21502. point4 = wc_ecc_new_point();
  21503. if (point4 == NULL) {
  21504. wc_ecc_del_point(point3);
  21505. wc_ecc_del_point(point2);
  21506. wc_ecc_del_point(point);
  21507. return -10003;
  21508. }
  21509. #endif
  21510. /* Parameter Validation testing. */
  21511. wc_ecc_del_point(NULL);
  21512. ret = wc_ecc_import_point_der(NULL, sizeof(der), curve_idx, point);
  21513. if (ret != ECC_BAD_ARG_E) {
  21514. ret = -10004;
  21515. goto done;
  21516. }
  21517. ret = wc_ecc_import_point_der(der, sizeof(der), ECC_CURVE_INVALID, point);
  21518. if (ret != ECC_BAD_ARG_E) {
  21519. ret = -10005;
  21520. goto done;
  21521. }
  21522. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, NULL);
  21523. if (ret != ECC_BAD_ARG_E) {
  21524. ret = -10006;
  21525. goto done;
  21526. }
  21527. ret = wc_ecc_export_point_der(-1, point, out, &outLen);
  21528. if (ret != ECC_BAD_ARG_E) {
  21529. ret = -10007;
  21530. goto done;
  21531. }
  21532. ret = wc_ecc_export_point_der(curve_idx, NULL, out, &outLen);
  21533. if (ret != ECC_BAD_ARG_E) {
  21534. ret = -10008;
  21535. goto done;
  21536. }
  21537. ret = wc_ecc_export_point_der(curve_idx, point, NULL, &outLen);
  21538. if (ret != LENGTH_ONLY_E || outLen != sizeof(out)) {
  21539. ret = -10009;
  21540. goto done;
  21541. }
  21542. ret = wc_ecc_export_point_der(curve_idx, point, out, NULL);
  21543. if (ret != ECC_BAD_ARG_E) {
  21544. ret = -10010;
  21545. goto done;
  21546. }
  21547. outLen = 0;
  21548. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  21549. if (ret != BUFFER_E) {
  21550. ret = -10011;
  21551. goto done;
  21552. }
  21553. ret = wc_ecc_copy_point(NULL, NULL);
  21554. if (ret != ECC_BAD_ARG_E) {
  21555. ret = -10012;
  21556. goto done;
  21557. }
  21558. ret = wc_ecc_copy_point(NULL, point2);
  21559. if (ret != ECC_BAD_ARG_E) {
  21560. ret = -10013;
  21561. goto done;
  21562. }
  21563. ret = wc_ecc_copy_point(point, NULL);
  21564. if (ret != ECC_BAD_ARG_E) {
  21565. ret = -10014;
  21566. goto done;
  21567. }
  21568. ret = wc_ecc_cmp_point(NULL, NULL);
  21569. if (ret != BAD_FUNC_ARG) {
  21570. ret = -10015;
  21571. goto done;
  21572. }
  21573. ret = wc_ecc_cmp_point(NULL, point2);
  21574. if (ret != BAD_FUNC_ARG) {
  21575. ret = -10016;
  21576. goto done;
  21577. }
  21578. ret = wc_ecc_cmp_point(point, NULL);
  21579. if (ret != BAD_FUNC_ARG) {
  21580. ret = -10017;
  21581. goto done;
  21582. }
  21583. /* Use API. */
  21584. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, point);
  21585. if (ret != 0) {
  21586. ret = -10018;
  21587. goto done;
  21588. }
  21589. outLen = sizeof(out);
  21590. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  21591. if (ret != 0) {
  21592. ret = -10019;
  21593. goto done;
  21594. }
  21595. if (outLen != sizeof(der)) {
  21596. ret = -10020;
  21597. goto done;
  21598. }
  21599. if (XMEMCMP(out, der, outLen) != 0) {
  21600. ret = -10021;
  21601. goto done;
  21602. }
  21603. ret = wc_ecc_copy_point(point2, point);
  21604. if (ret != MP_OKAY) {
  21605. ret = -10022;
  21606. goto done;
  21607. }
  21608. ret = wc_ecc_cmp_point(point2, point);
  21609. if (ret != MP_EQ) {
  21610. ret = -10023;
  21611. goto done;
  21612. }
  21613. ret = wc_ecc_import_point_der(altDer, sizeof(altDer), curve_idx, point2);
  21614. if (ret != 0) {
  21615. ret = -10024;
  21616. goto done;
  21617. }
  21618. ret = wc_ecc_cmp_point(point2, point);
  21619. if (ret != MP_GT) {
  21620. ret = -10025;
  21621. goto done;
  21622. }
  21623. #if defined(HAVE_COMP_KEY) && (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) || \
  21624. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  21625. ret = wc_ecc_import_point_der(derComp0, sizeof(derComp0)*2-1, curve_idx, point3);
  21626. if (ret != 0) {
  21627. ret = -10026;
  21628. goto done;
  21629. }
  21630. ret = wc_ecc_import_point_der_ex(derComp0, sizeof(derComp0), curve_idx, point4, 0);
  21631. if (ret != 0) {
  21632. ret = -10027;
  21633. goto done;
  21634. }
  21635. ret = wc_ecc_cmp_point(point3, point4);
  21636. if (ret != MP_EQ) {
  21637. ret = -10028;
  21638. goto done;
  21639. }
  21640. ret = wc_ecc_import_point_der(derComp1, sizeof(derComp1)*2-1, curve_idx, point3);
  21641. if (ret != 0) {
  21642. ret = -10029;
  21643. goto done;
  21644. }
  21645. ret = wc_ecc_import_point_der_ex(derComp1, sizeof(derComp1), curve_idx, point4, 0);
  21646. if (ret != 0) {
  21647. ret = -10030;
  21648. goto done;
  21649. }
  21650. ret = wc_ecc_cmp_point(point3, point4);
  21651. if (ret != MP_EQ) {
  21652. ret = -10031;
  21653. goto done;
  21654. }
  21655. #endif
  21656. done:
  21657. #ifdef HAVE_COMP_KEY
  21658. wc_ecc_del_point(point4);
  21659. wc_ecc_del_point(point3);
  21660. #endif
  21661. wc_ecc_del_point(point2);
  21662. wc_ecc_del_point(point);
  21663. return ret;
  21664. }
  21665. #endif /* !WOLFSSL_ATECC508A && HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  21666. #if !defined(NO_SIG_WRAPPER) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21667. static int ecc_sig_test(WC_RNG* rng, ecc_key* key)
  21668. {
  21669. int ret;
  21670. word32 sigSz;
  21671. int size;
  21672. byte out[ECC_MAX_SIG_SIZE];
  21673. byte in[] = TEST_STRING;
  21674. WOLFSSL_SMALL_STACK_STATIC const byte hash[] = {
  21675. 0xf2, 0x02, 0x95, 0x65, 0xcb, 0xf6, 0x2a, 0x59,
  21676. 0x39, 0x2c, 0x05, 0xff, 0x0e, 0x29, 0xaf, 0xfe,
  21677. 0x47, 0x33, 0x8c, 0x99, 0x8d, 0x58, 0x64, 0x83,
  21678. 0xa6, 0x58, 0x0a, 0x33, 0x0b, 0x84, 0x5f, 0x5f
  21679. };
  21680. word32 inLen = (word32)XSTRLEN((char*)in);
  21681. size = wc_ecc_sig_size(key);
  21682. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, sizeof(*key));
  21683. if (ret != size)
  21684. return -10040;
  21685. sigSz = (word32)ret;
  21686. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  21687. inLen, out, &sigSz, key, sizeof(*key), rng);
  21688. if (ret != 0)
  21689. return -10041;
  21690. TEST_SLEEP();
  21691. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  21692. inLen, out, sigSz, key, sizeof(*key));
  21693. if (ret != 0)
  21694. return -10042;
  21695. TEST_SLEEP();
  21696. sigSz = (word32)sizeof(out);
  21697. ret = wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  21698. hash, (int)sizeof(hash), out, &sigSz, key, sizeof(*key), rng);
  21699. if (ret != 0)
  21700. return -10043;
  21701. TEST_SLEEP();
  21702. ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC,
  21703. hash, (int)sizeof(hash), out, sigSz, key, sizeof(*key));
  21704. if (ret != 0)
  21705. return -10044;
  21706. TEST_SLEEP();
  21707. return 0;
  21708. }
  21709. #endif
  21710. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  21711. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21712. static int ecc_exp_imp_test(ecc_key* key)
  21713. {
  21714. int ret;
  21715. int curve_id;
  21716. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21717. ecc_key *keyImp = (ecc_key *)XMALLOC(sizeof *keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21718. #else
  21719. ecc_key keyImp[1];
  21720. #endif
  21721. byte priv[32];
  21722. word32 privLen;
  21723. byte pub[65*2];
  21724. word32 pubLen, pubLenX, pubLenY;
  21725. const char qx[] = "7a4e287890a1a47ad3457e52f2f76a83"
  21726. "ce46cbc947616d0cbaa82323818a793d";
  21727. const char qy[] = "eec4084f5b29ebf29c44cce3b3059610"
  21728. "922f8b30ea6e8811742ac7238fe87308";
  21729. const char d[] = "8c14b793cb19137e323a6d2e2a870bca"
  21730. "2e7a493ec1153b3a95feb8a4873f8d08";
  21731. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21732. if (keyImp == NULL)
  21733. ERROR_OUT(-10050, done);
  21734. #endif
  21735. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  21736. privLen = sizeof(priv);
  21737. ret = wc_ecc_export_private_only(key, priv, &privLen);
  21738. if (ret != 0) {
  21739. ret = -10051;
  21740. goto done;
  21741. }
  21742. pubLen = sizeof(pub);
  21743. ret = wc_ecc_export_point_der(key->idx, &key->pubkey, pub, &pubLen);
  21744. if (ret != 0) {
  21745. ret = -10052;
  21746. goto done;
  21747. }
  21748. ret = wc_ecc_import_private_key(priv, privLen, pub, pubLen, keyImp);
  21749. if (ret != 0) {
  21750. ret = -10053;
  21751. goto done;
  21752. }
  21753. wc_ecc_free(keyImp);
  21754. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  21755. ret = wc_ecc_import_raw_ex(keyImp, qx, qy, d, ECC_SECP256R1);
  21756. if (ret != 0) {
  21757. ret = -10054;
  21758. goto done;
  21759. }
  21760. wc_ecc_free(keyImp);
  21761. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  21762. curve_id = wc_ecc_get_curve_id(key->idx);
  21763. if (curve_id < 0) {
  21764. ret = -10055;
  21765. goto done;
  21766. }
  21767. /* test import private only */
  21768. ret = wc_ecc_import_private_key_ex(priv, privLen, NULL, 0, keyImp,
  21769. curve_id);
  21770. if (ret != 0) {
  21771. ret = -10056;
  21772. goto done;
  21773. }
  21774. wc_ecc_free(keyImp);
  21775. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  21776. /* test export public raw */
  21777. pubLenX = pubLenY = 32;
  21778. ret = wc_ecc_export_public_raw(key, pub, &pubLenX, &pub[32], &pubLenY);
  21779. if (ret != 0) {
  21780. ret = -10057;
  21781. goto done;
  21782. }
  21783. #ifndef HAVE_SELFTEST
  21784. /* test import of public */
  21785. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], NULL, ECC_SECP256R1);
  21786. if (ret != 0) {
  21787. ret = -10058;
  21788. goto done;
  21789. }
  21790. #endif
  21791. wc_ecc_free(keyImp);
  21792. wc_ecc_init_ex(keyImp, HEAP_HINT, devId);
  21793. /* test export private and public raw */
  21794. pubLenX = pubLenY = privLen = 32;
  21795. ret = wc_ecc_export_private_raw(key, pub, &pubLenX, &pub[32], &pubLenY,
  21796. priv, &privLen);
  21797. if (ret != 0) {
  21798. ret = -10059;
  21799. goto done;
  21800. }
  21801. #ifndef HAVE_SELFTEST
  21802. /* test import of private and public */
  21803. ret = wc_ecc_import_unsigned(keyImp, pub, &pub[32], priv, ECC_SECP256R1);
  21804. if (ret != 0) {
  21805. ret = -10060;
  21806. goto done;
  21807. }
  21808. #endif
  21809. done:
  21810. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21811. if (keyImp != NULL) {
  21812. wc_ecc_free(keyImp);
  21813. XFREE(keyImp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21814. }
  21815. #else
  21816. wc_ecc_free(keyImp);
  21817. #endif
  21818. return ret;
  21819. }
  21820. #endif /* HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  21821. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT) && \
  21822. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21823. static int ecc_mulmod_test(ecc_key* key1)
  21824. {
  21825. int ret;
  21826. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21827. ecc_key *key2 = (ecc_key *)XMALLOC(sizeof *key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21828. ecc_key *key3 = (ecc_key *)XMALLOC(sizeof *key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21829. #else
  21830. ecc_key key2[1];
  21831. ecc_key key3[1];
  21832. #endif
  21833. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21834. if ((key2 == NULL) || (key3 == NULL))
  21835. ERROR_OUT(MEMORY_E, done);
  21836. #endif
  21837. wc_ecc_init_ex(key2, HEAP_HINT, devId);
  21838. wc_ecc_init_ex(key3, HEAP_HINT, devId);
  21839. /* TODO: Use test data, test with WOLFSSL_VALIDATE_ECC_IMPORT. */
  21840. /* Need base point (Gx,Gy) and parameter A - load them as the public and
  21841. * private key in key2.
  21842. */
  21843. ret = wc_ecc_import_raw_ex(key2, key1->dp->Gx, key1->dp->Gy, key1->dp->Af,
  21844. ECC_SECP256R1);
  21845. if (ret != 0)
  21846. goto done;
  21847. /* Need a point (Gx,Gy) and prime - load them as the public and private key
  21848. * in key3.
  21849. */
  21850. ret = wc_ecc_import_raw_ex(key3, key1->dp->Gx, key1->dp->Gy,
  21851. key1->dp->prime, ECC_SECP256R1);
  21852. if (ret != 0)
  21853. goto done;
  21854. ret = wc_ecc_mulmod(&key1->k, &key2->pubkey, &key3->pubkey, &key2->k, &key3->k,
  21855. 1);
  21856. if (ret != 0) {
  21857. ret = -10070;
  21858. goto done;
  21859. }
  21860. done:
  21861. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21862. if (key2 != NULL) {
  21863. wc_ecc_free(key2);
  21864. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21865. }
  21866. if (key3 != NULL) {
  21867. wc_ecc_free(key3);
  21868. XFREE(key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21869. }
  21870. #else
  21871. wc_ecc_free(key3);
  21872. wc_ecc_free(key2);
  21873. #endif
  21874. return ret;
  21875. }
  21876. #endif
  21877. #if defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG) && \
  21878. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21879. static int ecc_ssh_test(ecc_key* key, WC_RNG* rng)
  21880. {
  21881. int ret;
  21882. byte out[128];
  21883. word32 outLen = sizeof(out);
  21884. /* Parameter Validation testing. */
  21885. ret = wc_ecc_shared_secret_ssh(NULL, &key->pubkey, out, &outLen);
  21886. if (ret != BAD_FUNC_ARG)
  21887. return -10080;
  21888. ret = wc_ecc_shared_secret_ssh(key, NULL, out, &outLen);
  21889. if (ret != BAD_FUNC_ARG)
  21890. return -10081;
  21891. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, NULL, &outLen);
  21892. if (ret != BAD_FUNC_ARG)
  21893. return -10082;
  21894. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, NULL);
  21895. if (ret != BAD_FUNC_ARG)
  21896. return -10083;
  21897. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21898. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21899. !defined(HAVE_SELFTEST)
  21900. ret = wc_ecc_set_rng(key, rng);
  21901. if (ret != 0)
  21902. return -10084;
  21903. #else
  21904. (void)rng;
  21905. #endif
  21906. /* Use API. */
  21907. ret = 0;
  21908. do {
  21909. #if defined(WOLFSSL_ASYNC_CRYPT)
  21910. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  21911. #endif
  21912. if (ret == 0)
  21913. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, &outLen);
  21914. } while (ret == WC_PENDING_E);
  21915. if (ret != 0)
  21916. return -10085;
  21917. TEST_SLEEP();
  21918. return 0;
  21919. }
  21920. #endif /* HAVE_ECC_DHE && !WC_NO_RNG */
  21921. static int ecc_def_curve_test(WC_RNG *rng)
  21922. {
  21923. int ret;
  21924. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21925. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  21926. #else
  21927. ecc_key key[1];
  21928. #endif
  21929. #if (defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)) || \
  21930. (defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  21931. word32 idx = 0;
  21932. #endif
  21933. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  21934. if (key == NULL)
  21935. ERROR_OUT(MEMORY_E, done);
  21936. #endif
  21937. wc_ecc_init_ex(key, HEAP_HINT, devId);
  21938. /* Use API */
  21939. ret = wc_ecc_set_flags(NULL, 0);
  21940. if (ret != BAD_FUNC_ARG) {
  21941. ret = -10090;
  21942. goto done;
  21943. }
  21944. ret = wc_ecc_set_flags(key, 0);
  21945. if (ret != 0) {
  21946. ret = -10091;
  21947. goto done;
  21948. }
  21949. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  21950. #ifndef WC_NO_RNG
  21951. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  21952. #if defined(WOLFSSL_ASYNC_CRYPT)
  21953. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  21954. #endif
  21955. if (ret != 0) {
  21956. goto done;
  21957. }
  21958. #ifndef NO_SIG_WRAPPER
  21959. ret = ecc_sig_test(rng, key);
  21960. if (ret < 0)
  21961. goto done;
  21962. #endif
  21963. TEST_SLEEP();
  21964. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_CRYPTOCELL) && \
  21965. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  21966. ret = ecc_ssh_test(key, rng);
  21967. if (ret < 0)
  21968. goto done;
  21969. #endif
  21970. wc_ecc_free(key);
  21971. #else
  21972. (void)rng;
  21973. #endif /* !WC_NO_RNG */
  21974. #if (defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)) || \
  21975. (defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  21976. /* Use test ECC key - ensure real private "d" exists */
  21977. #ifdef USE_CERT_BUFFERS_256
  21978. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, key,
  21979. sizeof_ecc_key_der_256);
  21980. #else
  21981. {
  21982. XFILE file = XFOPEN(eccKeyDerFile, "rb");
  21983. byte der[128];
  21984. word32 derSz;
  21985. if (!file) {
  21986. ERROR_OUT(-10093, done);
  21987. }
  21988. derSz = (word32)XFREAD(der, 1, sizeof(der), file);
  21989. XFCLOSE(file);
  21990. ret = wc_EccPrivateKeyDecode(der, &idx, key, derSz);
  21991. }
  21992. #endif
  21993. if (ret != 0) {
  21994. goto done;
  21995. }
  21996. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)
  21997. ret = ecc_exp_imp_test(key);
  21998. if (ret < 0)
  21999. goto done;
  22000. #endif
  22001. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT) && \
  22002. !defined(WOLFSSL_CRYPTOCELL)
  22003. ret = ecc_mulmod_test(key);
  22004. if (ret < 0)
  22005. goto done;
  22006. #endif
  22007. #endif
  22008. #else
  22009. (void)rng;
  22010. (void)idx;
  22011. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  22012. done:
  22013. wc_ecc_free(key);
  22014. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22015. if (key != NULL) {
  22016. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22017. }
  22018. #endif
  22019. return ret;
  22020. }
  22021. #endif /* !NO_ECC256 || HAVE_ALL_CURVES */
  22022. #if defined(WOLFSSL_CERT_EXT) && \
  22023. (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  22024. static int ecc_decode_test(void)
  22025. {
  22026. int ret;
  22027. word32 inSz;
  22028. word32 inOutIdx;
  22029. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22030. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22031. #else
  22032. ecc_key key[1];
  22033. #endif
  22034. /* SECP256R1 OID: 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07 */
  22035. /* This is ecc_clikeypub_der_256. */
  22036. WOLFSSL_SMALL_STACK_STATIC const byte good[] = {
  22037. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce,
  22038. 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
  22039. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xbf, 0xf4,
  22040. 0x0f, 0x44, 0x50, 0x9a, 0x3d, 0xce, 0x9b, 0xb7, 0xf0, 0xc5,
  22041. 0x4d, 0xf5, 0x70, 0x7b, 0xd4, 0xec, 0x24, 0x8e, 0x19, 0x80,
  22042. 0xec, 0x5a, 0x4c, 0xa2, 0x24, 0x03, 0x62, 0x2c, 0x9b, 0xda,
  22043. 0xef, 0xa2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xc6, 0x56,
  22044. 0x95, 0x06, 0xcc, 0x01, 0xa9, 0xbd, 0xf6, 0x75, 0x1a, 0x42,
  22045. 0xf7, 0xbd, 0xa9, 0xb2, 0x36, 0x22, 0x5f, 0xc7, 0x5d, 0x7f,
  22046. 0xb4 };
  22047. WOLFSSL_SMALL_STACK_STATIC const byte badNoObjId[] = { 0x30, 0x08, 0x30, 0x06, 0x03, 0x04,
  22048. 0x00, 0x04, 0x01, 0x01 };
  22049. WOLFSSL_SMALL_STACK_STATIC const byte badOneObjId[] = { 0x30, 0x0a, 0x30, 0x08, 0x06, 0x00,
  22050. 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  22051. WOLFSSL_SMALL_STACK_STATIC const byte badObjId1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x09,
  22052. 0x06, 0x00, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  22053. WOLFSSL_SMALL_STACK_STATIC const byte badObj2d1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x00,
  22054. 0x06, 0x07, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  22055. WOLFSSL_SMALL_STACK_STATIC const byte badNotBitStr[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  22056. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  22057. 0x04, 0x04, 0x00, 0x04, 0x01, 0x01 };
  22058. WOLFSSL_SMALL_STACK_STATIC const byte badBitStrLen[] = { 0x30, 0x14, 0x30, 0x0b, 0x06, 0x00,
  22059. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  22060. 0x03, 0x05, 0x00, 0x04, 0x01, 0x01 };
  22061. WOLFSSL_SMALL_STACK_STATIC const byte badNoBitStrZero[] = { 0x30, 0x13, 0x30, 0x0a, 0x06, 0x00,
  22062. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  22063. 0x03, 0x03, 0x04, 0x01, 0x01 };
  22064. WOLFSSL_SMALL_STACK_STATIC const byte badPoint[] = { 0x30, 0x12, 0x30, 0x09, 0x06, 0x00,
  22065. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
  22066. 0x03, 0x03, 0x00, 0x04, 0x01 };
  22067. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22068. if (key == NULL)
  22069. ERROR_OUT(MEMORY_E, done);
  22070. #endif
  22071. XMEMSET(key, 0, sizeof *key);
  22072. wc_ecc_init_ex(key, HEAP_HINT, devId);
  22073. inSz = sizeof(good);
  22074. ret = wc_EccPublicKeyDecode(NULL, &inOutIdx, key, inSz);
  22075. if (ret != BAD_FUNC_ARG) {
  22076. ret = -10100;
  22077. goto done;
  22078. }
  22079. ret = wc_EccPublicKeyDecode(good, NULL, key, inSz);
  22080. if (ret != BAD_FUNC_ARG) {
  22081. ret = -10101;
  22082. goto done;
  22083. }
  22084. ret = wc_EccPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  22085. if (ret != BAD_FUNC_ARG) {
  22086. ret = -10102;
  22087. goto done;
  22088. }
  22089. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, 0);
  22090. if (ret != BAD_FUNC_ARG) {
  22091. ret = -10103;
  22092. goto done;
  22093. }
  22094. /* Change offset to produce bad input data. */
  22095. inOutIdx = 2;
  22096. inSz = sizeof(good) - inOutIdx;
  22097. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  22098. if (ret != ASN_PARSE_E) {
  22099. ret = -10104;
  22100. goto done;
  22101. }
  22102. inOutIdx = 4;
  22103. inSz = sizeof(good) - inOutIdx;
  22104. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  22105. if (ret != ASN_PARSE_E) {
  22106. ret = -10105;
  22107. goto done;
  22108. }
  22109. /* Bad data. */
  22110. inSz = sizeof(badNoObjId);
  22111. inOutIdx = 0;
  22112. ret = wc_EccPublicKeyDecode(badNoObjId, &inOutIdx, key, inSz);
  22113. if (ret != ASN_OBJECT_ID_E && ret != ASN_PARSE_E) {
  22114. ret = -10106;
  22115. goto done;
  22116. }
  22117. inSz = sizeof(badOneObjId);
  22118. inOutIdx = 0;
  22119. ret = wc_EccPublicKeyDecode(badOneObjId, &inOutIdx, key, inSz);
  22120. if (ret != ASN_OBJECT_ID_E && ret != ASN_PARSE_E) {
  22121. ret = -10107;
  22122. goto done;
  22123. }
  22124. inSz = sizeof(badObjId1Len);
  22125. inOutIdx = 0;
  22126. ret = wc_EccPublicKeyDecode(badObjId1Len, &inOutIdx, key, inSz);
  22127. if (ret != ASN_PARSE_E) {
  22128. ret = -10108;
  22129. goto done;
  22130. }
  22131. inSz = sizeof(badObj2d1Len);
  22132. inOutIdx = 0;
  22133. ret = wc_EccPublicKeyDecode(badObj2d1Len, &inOutIdx, key, inSz);
  22134. if (ret != ASN_PARSE_E) {
  22135. ret = -10109;
  22136. goto done;
  22137. }
  22138. inSz = sizeof(badNotBitStr);
  22139. inOutIdx = 0;
  22140. ret = wc_EccPublicKeyDecode(badNotBitStr, &inOutIdx, key, inSz);
  22141. if (ret != ASN_BITSTR_E && ret != ASN_PARSE_E) {
  22142. ret = -10110;
  22143. goto done;
  22144. }
  22145. inSz = sizeof(badBitStrLen);
  22146. inOutIdx = 0;
  22147. ret = wc_EccPublicKeyDecode(badBitStrLen, &inOutIdx, key, inSz);
  22148. if (ret != ASN_PARSE_E) {
  22149. ret = -10111;
  22150. goto done;
  22151. }
  22152. inSz = sizeof(badNoBitStrZero);
  22153. inOutIdx = 0;
  22154. ret = wc_EccPublicKeyDecode(badNoBitStrZero, &inOutIdx, key, inSz);
  22155. if (ret != ASN_EXPECT_0_E && ret != ASN_PARSE_E) {
  22156. ret = -10112;
  22157. goto done;
  22158. }
  22159. inSz = sizeof(badPoint);
  22160. inOutIdx = 0;
  22161. ret = wc_EccPublicKeyDecode(badPoint, &inOutIdx, key, inSz);
  22162. if (ret != ASN_ECC_KEY_E && ret != ASN_PARSE_E) {
  22163. ret = -10113;
  22164. goto done;
  22165. }
  22166. inSz = sizeof(good);
  22167. inOutIdx = 0;
  22168. ret = wc_EccPublicKeyDecode(good, &inOutIdx, key, inSz);
  22169. if (ret != 0) {
  22170. ret = -10114;
  22171. goto done;
  22172. }
  22173. done:
  22174. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22175. if (key != NULL) {
  22176. wc_ecc_free(key);
  22177. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22178. }
  22179. #else
  22180. wc_ecc_free(key);
  22181. #endif
  22182. return ret;
  22183. }
  22184. #endif /* WOLFSSL_CERT_EXT */
  22185. #ifdef WOLFSSL_CUSTOM_CURVES
  22186. static const byte eccKeyExplicitCurve[] = {
  22187. 0x30, 0x81, 0xf5, 0x30, 0x81, 0xae, 0x06, 0x07,
  22188. 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x30,
  22189. 0x81, 0xa2, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06,
  22190. 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01,
  22191. 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
  22192. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  22193. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  22194. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff,
  22195. 0xff, 0xfc, 0x2f, 0x30, 0x06, 0x04, 0x01, 0x00,
  22196. 0x04, 0x01, 0x07, 0x04, 0x41, 0x04, 0x79, 0xbe,
  22197. 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0,
  22198. 0x62, 0x95, 0xce, 0x87, 0x0b, 0x07, 0x02, 0x9b,
  22199. 0xfc, 0xdb, 0x2d, 0xce, 0x28, 0xd9, 0x59, 0xf2,
  22200. 0x81, 0x5b, 0x16, 0xf8, 0x17, 0x98, 0x48, 0x3a,
  22201. 0xda, 0x77, 0x26, 0xa3, 0xc4, 0x65, 0x5d, 0xa4,
  22202. 0xfb, 0xfc, 0x0e, 0x11, 0x08, 0xa8, 0xfd, 0x17,
  22203. 0xb4, 0x48, 0xa6, 0x85, 0x54, 0x19, 0x9c, 0x47,
  22204. 0xd0, 0x8f, 0xfb, 0x10, 0xd4, 0xb8, 0x02, 0x21,
  22205. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  22206. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  22207. 0xfe, 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0,
  22208. 0x3b, 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41,
  22209. 0x41, 0x02, 0x01, 0x01, 0x03, 0x42, 0x00, 0x04,
  22210. 0x3c, 0x4c, 0xc9, 0x5e, 0x2e, 0xa2, 0x3d, 0x49,
  22211. 0xcc, 0x5b, 0xff, 0x4f, 0xc9, 0x2e, 0x1d, 0x4a,
  22212. 0xc6, 0x21, 0xf6, 0xf3, 0xe6, 0x0b, 0x4f, 0xa9,
  22213. 0x9d, 0x74, 0x99, 0xdd, 0x97, 0xc7, 0x6e, 0xbe,
  22214. 0x14, 0x2b, 0x39, 0x9d, 0x63, 0xc7, 0x97, 0x0d,
  22215. 0x45, 0x25, 0x40, 0x30, 0x77, 0x05, 0x76, 0x88,
  22216. 0x38, 0x96, 0x29, 0x7d, 0x9c, 0xe1, 0x50, 0xbe,
  22217. 0xac, 0xf0, 0x1d, 0x86, 0xf4, 0x2f, 0x65, 0x0b
  22218. };
  22219. static int ecc_test_custom_curves(WC_RNG* rng)
  22220. {
  22221. int ret;
  22222. word32 inOutIdx;
  22223. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22224. ecc_key *key = (ecc_key *)XMALLOC(sizeof *key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22225. #else
  22226. ecc_key key[1];
  22227. #endif
  22228. /* test use of custom curve - using BRAINPOOLP256R1 for test */
  22229. #if defined(HAVE_ECC_BRAINPOOL) && !defined(HAVE_INTEL_QA)
  22230. #ifndef WOLFSSL_ECC_CURVE_STATIC
  22231. WOLFSSL_SMALL_STACK_STATIC const ecc_oid_t ecc_oid_brainpoolp256r1[] = {
  22232. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07
  22233. };
  22234. #define ecc_oid_brainpoolp256r1_sz \
  22235. (sizeof(ecc_oid_brainpoolp256r1) / sizeof(ecc_oid_t))
  22236. #else
  22237. #define ecc_oid_brainpoolp256r1 { \
  22238. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07 \
  22239. }
  22240. #define ecc_oid_brainpoolp256r1_sz 9
  22241. #endif
  22242. #define ecc_oid_brainpoolp256r1_sum 104
  22243. WOLFSSL_SMALL_STACK_STATIC const ecc_set_type ecc_dp_brainpool256r1 = {
  22244. 32, /* size/bytes */
  22245. ECC_CURVE_CUSTOM, /* ID */
  22246. "BRAINPOOLP256R1", /* curve name */
  22247. "A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377", /* prime */
  22248. "7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9", /* A */
  22249. "26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6", /* B */
  22250. "A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7", /* order */
  22251. "8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262", /* Gx */
  22252. "547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997", /* Gy */
  22253. ecc_oid_brainpoolp256r1, /* oid/oidSz */
  22254. ecc_oid_brainpoolp256r1_sz,
  22255. ecc_oid_brainpoolp256r1_sum, /* oid sum */
  22256. 1, /* cofactor */
  22257. };
  22258. #endif /* HAVE_ECC_BRAINPOOL */
  22259. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22260. if (! key) {
  22261. ret = MEMORY_E;
  22262. goto done;
  22263. }
  22264. #endif
  22265. XMEMSET(key, 0, sizeof *key);
  22266. #if defined(HAVE_ECC_BRAINPOOL) && !defined(HAVE_INTEL_QA)
  22267. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, ECC_CURVE_DEF,
  22268. &ecc_dp_brainpool256r1);
  22269. if (ret != 0) {
  22270. printf("ECC test for custom curve failed! %d\n", ret);
  22271. goto done;
  22272. }
  22273. #endif
  22274. #if defined(HAVE_ECC_BRAINPOOL) || defined(HAVE_ECC_KOBLITZ)
  22275. {
  22276. int curve_id;
  22277. #ifdef HAVE_ECC_BRAINPOOL
  22278. curve_id = ECC_BRAINPOOLP256R1;
  22279. #else
  22280. curve_id = ECC_SECP256K1;
  22281. #endif
  22282. /* Test and demonstrate use of non-SECP curve */
  22283. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, curve_id, NULL);
  22284. if (ret < 0) {
  22285. printf("ECC test for curve_id %d failed! %d\n", curve_id, ret);
  22286. goto done;
  22287. }
  22288. }
  22289. #endif
  22290. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  22291. if (ret != 0) {
  22292. ret = -10120;
  22293. goto done;
  22294. }
  22295. inOutIdx = 0;
  22296. ret = wc_EccPublicKeyDecode(eccKeyExplicitCurve, &inOutIdx, key,
  22297. sizeof(eccKeyExplicitCurve));
  22298. if (ret != 0)
  22299. ret = -10121;
  22300. done:
  22301. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22302. if (key) {
  22303. wc_ecc_free(key);
  22304. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22305. }
  22306. #else
  22307. wc_ecc_free(key);
  22308. #endif
  22309. (void)rng;
  22310. return ret;
  22311. }
  22312. #endif /* WOLFSSL_CUSTOM_CURVES */
  22313. #ifdef WOLFSSL_CERT_GEN
  22314. /* Make Cert / Sign example for ECC cert and ECC CA */
  22315. static int ecc_test_cert_gen(WC_RNG* rng)
  22316. {
  22317. int ret;
  22318. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22319. Cert *myCert = (Cert *)XMALLOC(sizeof *myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22320. #ifdef WOLFSSL_TEST_CERT
  22321. DecodedCert *decode = (DecodedCert *)XMALLOC(sizeof *decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22322. #endif
  22323. ecc_key *caEccKey = (ecc_key *)XMALLOC(sizeof *caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22324. ecc_key *certPubKey = (ecc_key *)XMALLOC(sizeof *certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22325. #else
  22326. Cert myCert[1];
  22327. #ifdef WOLFSSL_TEST_CERT
  22328. DecodedCert decode[1];
  22329. #endif
  22330. ecc_key caEccKey[1];
  22331. ecc_key certPubKey[1];
  22332. #endif
  22333. int certSz;
  22334. size_t bytes;
  22335. word32 idx = 0;
  22336. #ifndef USE_CERT_BUFFERS_256
  22337. XFILE file;
  22338. #endif
  22339. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22340. byte* der = NULL;
  22341. #else
  22342. byte der[FOURK_BUF];
  22343. #endif
  22344. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22345. if ((myCert == NULL)
  22346. #ifdef WOLFSSL_TEST_CERT
  22347. || (decode == NULL)
  22348. #endif
  22349. || (caEccKey == NULL) || (certPubKey == NULL))
  22350. ERROR_OUT(MEMORY_E, exit);
  22351. #endif
  22352. XMEMSET(caEccKey, 0, sizeof *caEccKey);
  22353. XMEMSET(certPubKey, 0, sizeof *certPubKey);
  22354. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22355. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22356. if (der == NULL) {
  22357. ERROR_OUT(-10130, exit);
  22358. }
  22359. #endif
  22360. /* Get cert private key */
  22361. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  22362. /* Get Cert Key 384 */
  22363. #ifdef USE_CERT_BUFFERS_256
  22364. XMEMCPY(der, ca_ecc_key_der_384, sizeof_ca_ecc_key_der_384);
  22365. bytes = sizeof_ca_ecc_key_der_384;
  22366. #else
  22367. file = XFOPEN(eccCaKey384File, "rb");
  22368. if (!file) {
  22369. ERROR_OUT(-10131, exit);
  22370. }
  22371. bytes = XFREAD(der, 1, FOURK_BUF, file);
  22372. XFCLOSE(file);
  22373. (void)eccCaKeyFile;
  22374. #endif /* USE_CERT_BUFFERS_256 */
  22375. #else
  22376. #ifdef USE_CERT_BUFFERS_256
  22377. XMEMCPY(der, ca_ecc_key_der_256, sizeof_ca_ecc_key_der_256);
  22378. bytes = sizeof_ca_ecc_key_der_256;
  22379. #else
  22380. file = XFOPEN(eccCaKeyFile, "rb");
  22381. if (!file) {
  22382. ERROR_OUT(-10132, exit);
  22383. }
  22384. bytes = XFREAD(der, 1, FOURK_BUF, file);
  22385. XFCLOSE(file);
  22386. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  22387. (void)eccCaKey384File;
  22388. #endif
  22389. #endif /* USE_CERT_BUFFERS_256 */
  22390. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  22391. /* Get CA Key */
  22392. ret = wc_ecc_init_ex(caEccKey, HEAP_HINT, devId);
  22393. if (ret != 0) {
  22394. ERROR_OUT(-10133, exit);
  22395. }
  22396. ret = wc_EccPrivateKeyDecode(der, &idx, caEccKey, (word32)bytes);
  22397. if (ret != 0) {
  22398. ERROR_OUT(-10134, exit);
  22399. }
  22400. /* Make a public key */
  22401. ret = wc_ecc_init_ex(certPubKey, HEAP_HINT, devId);
  22402. if (ret != 0) {
  22403. ERROR_OUT(-10135, exit);
  22404. }
  22405. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, certPubKey);
  22406. #if defined(WOLFSSL_ASYNC_CRYPT)
  22407. ret = wc_AsyncWait(ret, &certPubKey->asyncDev, WC_ASYNC_FLAG_NONE);
  22408. #endif
  22409. if (ret != 0) {
  22410. ERROR_OUT(-10136, exit);
  22411. }
  22412. TEST_SLEEP();
  22413. /* Setup Certificate */
  22414. if (wc_InitCert_ex(myCert, HEAP_HINT, devId)) {
  22415. ERROR_OUT(-10137, exit);
  22416. }
  22417. #ifndef NO_SHA256
  22418. myCert->sigType = CTC_SHA256wECDSA;
  22419. #else
  22420. myCert->sigType = CTC_SHAwECDSA;
  22421. #endif
  22422. XMEMCPY(&myCert->subject, &certDefaultName, sizeof(CertName));
  22423. #ifdef WOLFSSL_CERT_EXT
  22424. /* add Policies */
  22425. XSTRNCPY(myCert->certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  22426. CTC_MAX_CERTPOL_SZ);
  22427. XSTRNCPY(myCert->certPolicies[1], "1.2.13025.489.1.113549",
  22428. CTC_MAX_CERTPOL_SZ);
  22429. myCert->certPoliciesNb = 2;
  22430. /* add SKID from the Public Key */
  22431. if (wc_SetSubjectKeyIdFromPublicKey(myCert, NULL, certPubKey) != 0) {
  22432. ERROR_OUT(-10138, exit);
  22433. }
  22434. /* add AKID from the Public Key */
  22435. if (wc_SetAuthKeyIdFromPublicKey(myCert, NULL, caEccKey) != 0) {
  22436. ERROR_OUT(-10139, exit);
  22437. }
  22438. /* add Key Usage */
  22439. if (wc_SetKeyUsage(myCert, certKeyUsage) != 0) {
  22440. ERROR_OUT(-10140, exit);
  22441. }
  22442. #endif /* WOLFSSL_CERT_EXT */
  22443. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  22444. #if defined(USE_CERT_BUFFERS_256)
  22445. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_384,
  22446. sizeof_ca_ecc_cert_der_384);
  22447. #else
  22448. ret = wc_SetIssuer(myCert, eccCaCert384File);
  22449. (void)eccCaCertFile;
  22450. #endif
  22451. #else
  22452. #if defined(USE_CERT_BUFFERS_256)
  22453. ret = wc_SetIssuerBuffer(myCert, ca_ecc_cert_der_256,
  22454. sizeof_ca_ecc_cert_der_256);
  22455. #else
  22456. ret = wc_SetIssuer(myCert, eccCaCertFile);
  22457. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  22458. (void)eccCaCert384File;
  22459. #endif
  22460. #endif
  22461. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  22462. if (ret < 0) {
  22463. ERROR_OUT(-10141, exit);
  22464. }
  22465. certSz = wc_MakeCert(myCert, der, FOURK_BUF, NULL, certPubKey, rng);
  22466. if (certSz < 0) {
  22467. ERROR_OUT(-10142, exit);
  22468. }
  22469. ret = 0;
  22470. do {
  22471. #if defined(WOLFSSL_ASYNC_CRYPT)
  22472. ret = wc_AsyncWait(ret, &caEccKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  22473. #endif
  22474. if (ret >= 0) {
  22475. ret = wc_SignCert(myCert->bodySz, myCert->sigType, der,
  22476. FOURK_BUF, NULL, caEccKey, rng);
  22477. }
  22478. } while (ret == WC_PENDING_E);
  22479. if (ret < 0) {
  22480. ERROR_OUT(-10143, exit);
  22481. }
  22482. certSz = ret;
  22483. TEST_SLEEP();
  22484. #ifdef WOLFSSL_TEST_CERT
  22485. InitDecodedCert(decode, der, certSz, HEAP_HINT);
  22486. ret = ParseCert(decode, CERT_TYPE, NO_VERIFY, 0);
  22487. if (ret != 0) {
  22488. FreeDecodedCert(decode);
  22489. ERROR_OUT(-10144, exit);
  22490. }
  22491. FreeDecodedCert(decode);
  22492. #endif
  22493. ret = SaveDerAndPem(der, certSz, certEccDerFile, certEccPemFile,
  22494. CERT_TYPE, -6735);
  22495. if (ret != 0) {
  22496. goto exit;
  22497. }
  22498. exit:
  22499. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22500. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22501. #endif
  22502. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  22503. if (myCert != NULL)
  22504. XFREE(myCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22505. #ifdef WOLFSSL_TEST_CERT
  22506. if (decode != NULL)
  22507. XFREE(decode, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22508. #endif
  22509. if (caEccKey != NULL) {
  22510. wc_ecc_free(caEccKey);
  22511. XFREE(caEccKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22512. }
  22513. if (certPubKey != NULL) {
  22514. wc_ecc_free(certPubKey);
  22515. XFREE(certPubKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  22516. }
  22517. #else
  22518. wc_ecc_free(certPubKey);
  22519. wc_ecc_free(caEccKey);
  22520. #endif
  22521. return ret;
  22522. }
  22523. #endif /* WOLFSSL_CERT_GEN */
  22524. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  22525. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  22526. /* Test for the wc_ecc_key_new() and wc_ecc_key_free() functions. */
  22527. static int ecc_test_allocator(WC_RNG* rng)
  22528. {
  22529. int ret = 0;
  22530. ecc_key* key;
  22531. #ifdef WC_NO_RNG
  22532. word32 idx = 0;
  22533. #endif
  22534. key = wc_ecc_key_new(HEAP_HINT);
  22535. if (key == NULL) {
  22536. ERROR_OUT(-10150, exit);
  22537. }
  22538. #ifndef WC_NO_RNG
  22539. ret = wc_ecc_make_key(rng, ECC_KEYGEN_SIZE, key);
  22540. #if defined(WOLFSSL_ASYNC_CRYPT)
  22541. ret = wc_AsyncWait(ret, &key->asyncDev, WC_ASYNC_FLAG_NONE);
  22542. #endif
  22543. if (ret != 0) {
  22544. ERROR_OUT(-10151, exit);
  22545. }
  22546. #else
  22547. /* use test ECC key */
  22548. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, key,
  22549. (word32)sizeof_ecc_key_der_256);
  22550. (void)rng;
  22551. #endif
  22552. exit:
  22553. wc_ecc_key_free(key);
  22554. return ret;
  22555. }
  22556. #endif
  22557. /* ECC Non-blocking tests for Sign and Verify */
  22558. /* Requires SP math and supports P384 or P256 */
  22559. /* ./configure --enable-ecc=nonblock --enable-sp=yes,nonblock CFLAGS="-DWOLFSSL_PUBLIC_MP" */
  22560. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_HAVE_SP_ECC) && \
  22561. defined(WOLFSSL_PUBLIC_MP)
  22562. /* ECC Private Key "d" */
  22563. static const byte p256PrivKey[] = {
  22564. /* SECP256R1 */
  22565. /* d */
  22566. 0x1e, 0xe7, 0x70, 0x07, 0xd3, 0x30, 0x94, 0x39,
  22567. 0x28, 0x90, 0xdf, 0x23, 0x88, 0x2c, 0x4a, 0x34,
  22568. 0x15, 0xdb, 0x4c, 0x43, 0xcd, 0xfa, 0xe5, 0x1f,
  22569. 0x3d, 0x4c, 0x37, 0xfe, 0x59, 0x3b, 0x96, 0xd8
  22570. };
  22571. #ifdef HAVE_ECC384
  22572. static const byte p384PrivKey[] = {
  22573. /* SECP384R1 */
  22574. /* d */
  22575. 0xa4, 0xe5, 0x06, 0xe8, 0x06, 0x16, 0x3e, 0xab,
  22576. 0x89, 0xf8, 0x60, 0x43, 0xc0, 0x60, 0x25, 0xdb,
  22577. 0xba, 0x7b, 0xfe, 0x19, 0x35, 0x08, 0x55, 0x65,
  22578. 0x76, 0xe2, 0xdc, 0xe0, 0x01, 0x8b, 0x6b, 0x68,
  22579. 0xdf, 0xcf, 0x6f, 0x80, 0x12, 0xce, 0x79, 0x37,
  22580. 0xeb, 0x2b, 0x9c, 0x7b, 0xc4, 0x68, 0x1c, 0x74
  22581. };
  22582. #endif /* HAVE_ECC384 */
  22583. #ifdef HAVE_ECC521
  22584. static const byte p521PrivKey[] = {
  22585. /* SECP521R1 */
  22586. /* d */
  22587. 0x01, 0x68, 0x91, 0x33, 0x53, 0xe2, 0x90, 0x68,
  22588. 0x11, 0x8f, 0xaa, 0xa8, 0x76, 0x0c, 0xf7, 0x2a,
  22589. 0x07, 0x1b, 0x92, 0x2a, 0xa7, 0x82, 0x3d, 0xfa,
  22590. 0x83, 0xce, 0x70, 0xc8, 0xc2, 0x60, 0x82, 0xfe,
  22591. 0x18, 0x88, 0x68, 0xda, 0x6a, 0x83, 0x46, 0x78,
  22592. 0xe4, 0xe9, 0xe9, 0xcc, 0x51, 0x7f, 0xed, 0x81,
  22593. 0x02, 0x32, 0xee, 0x26, 0x87, 0xcc, 0xed, 0x63,
  22594. 0x3f, 0x39, 0x27, 0xf0, 0xd7, 0x17, 0x77, 0xa1,
  22595. 0xa4, 0x36
  22596. };
  22597. #endif /* HAVE_ECC521 */
  22598. /* ECC public key Qx/Qy */
  22599. static const byte p256PubKey[] = {
  22600. /* SECP256R1 */
  22601. /* Qx */
  22602. 0x96, 0x93, 0x1c, 0x53, 0x0b, 0x43, 0x6c, 0x42,
  22603. 0x0c, 0x52, 0x90, 0xe4, 0xa7, 0xec, 0x98, 0xb1,
  22604. 0xaf, 0xd4, 0x14, 0x49, 0xd8, 0xc1, 0x42, 0x82,
  22605. 0x04, 0x78, 0xd1, 0x90, 0xae, 0xa0, 0x6c, 0x07,
  22606. /* Qy */
  22607. 0xf2, 0x3a, 0xb5, 0x10, 0x32, 0x8d, 0xce, 0x9e,
  22608. 0x76, 0xa0, 0xd2, 0x8c, 0xf3, 0xfc, 0xa9, 0x94,
  22609. 0x43, 0x24, 0xe6, 0x82, 0x00, 0x40, 0xc6, 0xdb,
  22610. 0x1c, 0x2f, 0xcd, 0x38, 0x4b, 0x60, 0xdd, 0x61
  22611. };
  22612. #ifdef HAVE_ECC384
  22613. static const byte p384PubKey[] = {
  22614. /* SECP384R1 */
  22615. /* Qx */
  22616. 0xea, 0xcf, 0x93, 0x4f, 0x2c, 0x09, 0xbb, 0x39,
  22617. 0x14, 0x0f, 0x56, 0x64, 0xc3, 0x40, 0xb4, 0xdf,
  22618. 0x0e, 0x63, 0xae, 0xe5, 0x71, 0x4b, 0x00, 0xcc,
  22619. 0x04, 0x97, 0xff, 0xe1, 0xe9, 0x38, 0x96, 0xbb,
  22620. 0x5f, 0x91, 0xb2, 0x6a, 0xcc, 0xb5, 0x39, 0x5f,
  22621. 0x8f, 0x70, 0x59, 0xf1, 0x01, 0xf6, 0x5a, 0x2b,
  22622. /* Qy */
  22623. 0x01, 0x6c, 0x68, 0x0b, 0xcf, 0x55, 0x25, 0xaf,
  22624. 0x6d, 0x98, 0x48, 0x0a, 0xa8, 0x74, 0xc9, 0xa9,
  22625. 0x17, 0xa0, 0x0c, 0xc3, 0xfb, 0xd3, 0x23, 0x68,
  22626. 0xfe, 0x04, 0x3c, 0x63, 0x50, 0x88, 0x3b, 0xb9,
  22627. 0x4f, 0x7c, 0x67, 0x34, 0xf7, 0x3b, 0xa9, 0x73,
  22628. 0xe7, 0x1b, 0xc3, 0x51, 0x5e, 0x22, 0x18, 0xec
  22629. };
  22630. #endif
  22631. #ifdef HAVE_ECC521
  22632. static const byte p521PubKey[] = {
  22633. /* SECP521R1 */
  22634. /* Qx */
  22635. 0x01, 0x62, 0x6e, 0xf1, 0x00, 0xec, 0xd8, 0x99,
  22636. 0x58, 0x9b, 0x80, 0x6b, 0xfe, 0x2c, 0xf1, 0xb2,
  22637. 0xf0, 0xc8, 0x48, 0xdf, 0xac, 0xd2, 0x3b, 0x71,
  22638. 0x29, 0xab, 0xf0, 0x66, 0x63, 0xd8, 0x8e, 0xb5,
  22639. 0xc8, 0xc2, 0xfc, 0x99, 0x44, 0xe2, 0x45, 0xb1,
  22640. 0x5a, 0x7b, 0xb9, 0x73, 0x01, 0xda, 0x79, 0xec,
  22641. 0x9c, 0x26, 0x27, 0x34, 0x45, 0x26, 0xd5, 0x89,
  22642. 0x4b, 0x44, 0xfe, 0x69, 0x4e, 0x72, 0x14, 0xe3,
  22643. 0x8b, 0xbc,
  22644. /* Qy */
  22645. 0x00, 0x0f, 0x09, 0xa2, 0x03, 0xc3, 0x5a, 0xdc,
  22646. 0x95, 0x82, 0xf6, 0xf9, 0xf6, 0x9c, 0xff, 0xb5,
  22647. 0x6b, 0x75, 0x95, 0x4b, 0xa4, 0x28, 0x5d, 0x9e,
  22648. 0x90, 0x04, 0xd1, 0xc0, 0x1e, 0xd5, 0xfd, 0x43,
  22649. 0x9e, 0x1e, 0x83, 0xc0, 0x11, 0x2b, 0x2b, 0x07,
  22650. 0x6d, 0xa9, 0x7a, 0x10, 0xd7, 0x67, 0xe7, 0x51,
  22651. 0x37, 0x24, 0xd8, 0xbf, 0x03, 0x0d, 0x8b, 0xb5,
  22652. 0x40, 0x5c, 0x4f, 0xd6, 0x13, 0x73, 0x42, 0xbc,
  22653. 0x91, 0xd9
  22654. };
  22655. /* perform verify of signature and hash using public key */
  22656. /* key is public Qx + public Qy */
  22657. /* sig is r + s */
  22658. static int crypto_ecc_verify(const uint8_t *key, uint32_t keySz,
  22659. const uint8_t *hash, uint32_t hashSz, const uint8_t *sig, uint32_t sigSz,
  22660. uint32_t curveSz, int curveId)
  22661. {
  22662. int ret, verify_res = 0, count = 0;
  22663. mp_int r, s;
  22664. ecc_key ecc;
  22665. ecc_nb_ctx_t nb_ctx;
  22666. /* validate arguments */
  22667. if (key == NULL || hash == NULL || sig == NULL || curveSz == 0 ||
  22668. hashSz == 0 || keySz < (curveSz*2) || sigSz < (curveSz*2))
  22669. {
  22670. return BAD_FUNC_ARG;
  22671. }
  22672. /* Setup the ECC key */
  22673. ret = wc_ecc_init(&ecc);
  22674. if (ret < 0) {
  22675. return ret;
  22676. }
  22677. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  22678. if (ret != MP_OKAY) {
  22679. wc_ecc_free(&ecc);
  22680. return ret;
  22681. }
  22682. /* Setup the signature r/s variables */
  22683. ret = mp_init(&r);
  22684. if (ret != MP_OKAY) {
  22685. wc_ecc_free(&ecc);
  22686. return ret;
  22687. }
  22688. ret = mp_init(&s);
  22689. if (ret != MP_OKAY) {
  22690. mp_clear(&r);
  22691. wc_ecc_free(&ecc);
  22692. return ret;
  22693. }
  22694. /* Import public key x/y */
  22695. ret = wc_ecc_import_unsigned(
  22696. &ecc,
  22697. (byte*)key, /* Public "x" Coordinate */
  22698. (byte*)(key + curveSz), /* Public "y" Coordinate */
  22699. NULL, /* Private "d" (optional) */
  22700. curveId /* ECC Curve Id */
  22701. );
  22702. /* Make sure it was a public key imported */
  22703. if (ret == 0 && ecc.type != ECC_PUBLICKEY) {
  22704. ret = ECC_BAD_ARG_E;
  22705. }
  22706. /* Import signature r/s */
  22707. if (ret == 0) {
  22708. ret = mp_read_unsigned_bin(&r, sig, curveSz);
  22709. }
  22710. if (ret == 0) {
  22711. ret = mp_read_unsigned_bin(&s, sig + curveSz, curveSz);
  22712. }
  22713. /* Verify ECC Signature */
  22714. if (ret == 0) {
  22715. do {
  22716. ret = wc_ecc_verify_hash_ex(
  22717. &r, &s, /* r/s as mp_int */
  22718. hash, hashSz, /* computed hash digest */
  22719. &verify_res, /* verification result 1=success */
  22720. &ecc
  22721. );
  22722. count++;
  22723. /* This is where real-time work could be called */
  22724. } while (ret == FP_WOULDBLOCK);
  22725. #ifdef DEBUG_WOLFSSL
  22726. printf("ECC non-block verify: %d times\n", count);
  22727. #endif
  22728. }
  22729. /* check verify result */
  22730. if (ret == 0 && verify_res == 0) {
  22731. ret = SIG_VERIFY_E;
  22732. }
  22733. mp_clear(&r);
  22734. mp_clear(&s);
  22735. wc_ecc_free(&ecc);
  22736. (void)count;
  22737. return ret;
  22738. }
  22739. /* perform signature operation against hash using private key */
  22740. static int crypto_ecc_sign(const uint8_t *key, uint32_t keySz,
  22741. const uint8_t *hash, uint32_t hashSz, uint8_t *sig, uint32_t* sigSz,
  22742. uint32_t curveSz, int curveId, WC_RNG* rng)
  22743. {
  22744. int ret, count = 0;
  22745. mp_int r, s;
  22746. ecc_key ecc;
  22747. ecc_nb_ctx_t nb_ctx;
  22748. /* validate arguments */
  22749. if (key == NULL || hash == NULL || sig == NULL || sigSz == NULL ||
  22750. curveSz == 0 || hashSz == 0 || keySz < curveSz || *sigSz < (curveSz*2))
  22751. {
  22752. return BAD_FUNC_ARG;
  22753. }
  22754. /* Initialize signature result */
  22755. memset(sig, 0, curveSz*2);
  22756. /* Setup the ECC key */
  22757. ret = wc_ecc_init(&ecc);
  22758. if (ret < 0) {
  22759. return ret;
  22760. }
  22761. ret = wc_ecc_set_nonblock(&ecc, &nb_ctx);
  22762. if (ret != MP_OKAY) {
  22763. wc_ecc_free(&ecc);
  22764. return ret;
  22765. }
  22766. /* Setup the signature r/s variables */
  22767. ret = mp_init(&r);
  22768. if (ret != MP_OKAY) {
  22769. wc_ecc_free(&ecc);
  22770. return ret;
  22771. }
  22772. ret = mp_init(&s);
  22773. if (ret != MP_OKAY) {
  22774. mp_clear(&r);
  22775. wc_ecc_free(&ecc);
  22776. return ret;
  22777. }
  22778. /* Import private key "k" */
  22779. ret = wc_ecc_import_private_key_ex(
  22780. key, keySz, /* private key "d" */
  22781. NULL, 0, /* public (optional) */
  22782. &ecc,
  22783. curveId /* ECC Curve Id */
  22784. );
  22785. if (ret == 0) {
  22786. do {
  22787. /* Verify ECC Signature */
  22788. ret = wc_ecc_sign_hash_ex(
  22789. hash, hashSz, /* computed hash digest */
  22790. rng, &ecc, /* random and key context */
  22791. &r, &s /* r/s as mp_int */
  22792. );
  22793. count++;
  22794. /* This is where real-time work could be called */
  22795. } while (ret == FP_WOULDBLOCK);
  22796. #ifdef DEBUG_WOLFSSL
  22797. printf("ECC non-block sign: %d times\n", count);
  22798. #endif
  22799. }
  22800. if (ret == 0) {
  22801. /* export r/s */
  22802. mp_to_unsigned_bin_len(&r, sig, curveSz);
  22803. mp_to_unsigned_bin_len(&s, sig + curveSz, curveSz);
  22804. }
  22805. mp_clear(&r);
  22806. mp_clear(&s);
  22807. wc_ecc_free(&ecc);
  22808. (void)count;
  22809. return ret;
  22810. }
  22811. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  22812. /*
  22813. * This test doesn't work with WOLFSSL_VALIDATE_ECC_KEYGEN defined because we
  22814. * don't have non-blocking versions of the key checking functions, yet.
  22815. */
  22816. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_VALIDATE_ECC_KEYGEN)
  22817. static int ecc_test_nonblock_dhe(int curveId, word32 curveSz,
  22818. const byte* privKey, const byte* pubKey, WC_RNG* rng)
  22819. {
  22820. int ret;
  22821. ecc_key keyA;
  22822. ecc_key keyB;
  22823. ecc_nb_ctx_t nbCtxA;
  22824. ecc_nb_ctx_t nbCtxB;
  22825. byte secretA[ECC_SHARED_SIZE];
  22826. byte secretB[ECC_SHARED_SIZE];
  22827. word32 secretSzA = ECC_SHARED_SIZE;
  22828. word32 secretSzB = ECC_SHARED_SIZE;
  22829. int count = 0;
  22830. ret = wc_ecc_init(&keyA);
  22831. if (ret == 0) {
  22832. ret = wc_ecc_init(&keyB);
  22833. }
  22834. if (ret == 0) {
  22835. ret = wc_ecc_set_nonblock(&keyA, &nbCtxA);
  22836. }
  22837. if (ret == 0) {
  22838. ret = wc_ecc_set_nonblock(&keyB, &nbCtxB);
  22839. }
  22840. if (ret == 0) {
  22841. do {
  22842. ret = wc_ecc_make_key_ex(rng, curveSz, &keyA, curveId);
  22843. count++;
  22844. } while (ret == FP_WOULDBLOCK);
  22845. }
  22846. #ifdef DEBUG_WOLFSSL
  22847. fprintf(stderr, "ECC non-block key gen: %d times\n", count);
  22848. #endif
  22849. if (ret == 0) {
  22850. ret = wc_ecc_check_key(&keyA);
  22851. }
  22852. if (ret == 0) {
  22853. ret = wc_ecc_import_unsigned(&keyB, pubKey, pubKey + curveSz,
  22854. privKey, curveId);
  22855. }
  22856. count = 0;
  22857. if (ret == 0) {
  22858. do {
  22859. ret = wc_ecc_shared_secret(&keyA, &keyB, secretA, &secretSzA);
  22860. count++;
  22861. } while (ret == FP_WOULDBLOCK);
  22862. }
  22863. #ifdef DEBUG_WOLFSSL
  22864. fprintf(stderr, "ECC non-block shared secret: %d times\n", count);
  22865. #endif
  22866. if (ret == 0) {
  22867. do {
  22868. ret = wc_ecc_shared_secret(&keyB, &keyA, secretB, &secretSzB);
  22869. } while (ret == FP_WOULDBLOCK);
  22870. }
  22871. if (ret == 0) {
  22872. if (secretSzA != secretSzB ||
  22873. XMEMCMP(secretA, secretB, secretSzA) != 0) {
  22874. ret = -1;
  22875. }
  22876. }
  22877. wc_ecc_free(&keyA);
  22878. wc_ecc_free(&keyB);
  22879. return ret;
  22880. }
  22881. #endif /* HAVE_ECC_DHE && !WOLFSSL_VALIDATE_ECC_KEYGEN */
  22882. #if defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  22883. static int ecc_test_nonblock_ecdsa(int curveId, word32 curveSz,
  22884. const byte* privKey, word32 privKeySz, const byte* pubKey, word32 pubKeySz,
  22885. WC_RNG* rng)
  22886. {
  22887. int ret = 0;
  22888. byte* sig = NULL;
  22889. word32 sigSz = curveSz * 2;
  22890. static const byte hash[] = {
  22891. 0x8d, 0x28, 0xa3, 0x8b, 0x0b, 0xa9, 0xfe, 0xd4, 0x0e, 0x54, 0xc4, 0x17,
  22892. 0x3d, 0x54, 0x66, 0x34, 0xbf, 0x5d, 0x6f, 0x46, 0xc2, 0x20, 0xcb, 0xc3,
  22893. 0x22, 0xe9, 0xb0, 0xdf, 0xe7, 0x64, 0x3f, 0xd9
  22894. };
  22895. sig = (byte*)XMALLOC(sigSz, HEAP_HINT, DYNAMIC_TYPE_SIGNATURE);
  22896. if (sig == NULL) {
  22897. ret = -1;
  22898. }
  22899. if (ret == 0) {
  22900. /* Sign hash using private key */
  22901. /* Note: result of an ECC sign varies for each call even with same
  22902. private key and hash. This is because a new random public key is
  22903. used for each operation. */
  22904. ret = crypto_ecc_sign(privKey, privKeySz, hash, sizeof(hash), sig,
  22905. &sigSz, curveSz, curveId, rng);
  22906. }
  22907. if (ret == 0) {
  22908. /* Verify generated signature is valid */
  22909. ret = crypto_ecc_verify(pubKey, pubKeySz, hash, sizeof(hash), sig,
  22910. sigSz, curveSz, curveId);
  22911. }
  22912. if (sig != NULL) {
  22913. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_SIGNATURE);
  22914. }
  22915. return ret;
  22916. }
  22917. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  22918. static int ecc_test_nonblock(WC_RNG* rng)
  22919. {
  22920. int ret = 0;
  22921. word32 i;
  22922. int curveIds[3] = {0, 0, 0};
  22923. word32 curveSzs[3] = {0, 0, 0};
  22924. const byte* privKeys[3] = {NULL, NULL, NULL};
  22925. word32 privKeySzs[3] = {0, 0, 0};
  22926. const byte* pubKeys[3] = {NULL, NULL, NULL};
  22927. word32 pubKeySzs[3] = {0, 0, 0};
  22928. curveIds[0] = ECC_SECP256R1;
  22929. curveSzs[0] = 32;
  22930. privKeys[0] = p256PrivKey;
  22931. privKeySzs[0] = sizeof(p256PrivKey);
  22932. pubKeys[0] = p256PubKey;
  22933. pubKeySzs[0] = sizeof(p256PubKey);
  22934. #ifdef HAVE_ECC384
  22935. curveIds[1] = ECC_SECP384R1;
  22936. curveSzs[1] = 48;
  22937. privKeys[1] = p384PrivKey;
  22938. privKeySzs[1] = sizeof(p384PrivKey);
  22939. pubKeys[1] = p384PubKey;
  22940. pubKeySzs[1] = sizeof(p384PubKey);
  22941. #endif
  22942. #ifdef HAVE_ECC521
  22943. curveIds[2] = ECC_SECP521R1;
  22944. curveSzs[2] = 66;
  22945. privKeys[2] = p521PrivKey;
  22946. privKeySzs[2] = sizeof(p521PrivKey);
  22947. pubKeys[2] = p521PubKey;
  22948. pubKeySzs[2] = sizeof(p521PubKey);
  22949. #endif
  22950. for (i = 0; ret == 0 && i < sizeof(curveIds) / sizeof(curveIds[0]); ++i) {
  22951. if (curveIds[i] == 0) {
  22952. continue;
  22953. }
  22954. #if defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  22955. ret = ecc_test_nonblock_ecdsa(curveIds[i], curveSzs[i], privKeys[i],
  22956. privKeySzs[i], pubKeys[i], pubKeySzs[i], rng);
  22957. #endif /* HAVE_ECC_SIGN && HAVE_ECC_VERIFY */
  22958. #if defined(HAVE_ECC_DHE) && !defined(WOLFSSL_VALIDATE_ECC_KEYGEN)
  22959. if (ret == 0) {
  22960. ret = ecc_test_nonblock_dhe(curveIds[i], curveSzs[i], privKeys[i],
  22961. pubKeys[i], rng);
  22962. }
  22963. #endif /* HAVE_ECC_DHE && !WOLFSSL_VALIDATE_ECC_KEYGEN */
  22964. }
  22965. return ret;
  22966. }
  22967. #endif /* WC_ECC_NONBLOCK && WOLFSSL_HAVE_SP_ECC && WOLFSSL_PUBLIC_MP */
  22968. WOLFSSL_TEST_SUBROUTINE int ecc_test(void)
  22969. {
  22970. int ret;
  22971. WC_RNG rng;
  22972. #if defined(WOLFSSL_CERT_EXT) && \
  22973. (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  22974. ret = ecc_decode_test();
  22975. if (ret < 0)
  22976. return ret;
  22977. #endif
  22978. #ifndef HAVE_FIPS
  22979. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  22980. #else
  22981. ret = wc_InitRng(&rng);
  22982. #endif
  22983. #ifndef WC_NO_RNG
  22984. if (ret != 0)
  22985. return -10300;
  22986. #else
  22987. (void)ret;
  22988. #endif
  22989. #if (defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 112
  22990. ret = ecc_test_curve(&rng, 14);
  22991. if (ret < 0) {
  22992. goto done;
  22993. }
  22994. #endif /* HAVE_ECC112 */
  22995. #if (defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 128
  22996. ret = ecc_test_curve(&rng, 16);
  22997. if (ret < 0) {
  22998. goto done;
  22999. }
  23000. #endif /* HAVE_ECC128 */
  23001. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  23002. ret = ecc_test_curve(&rng, 20);
  23003. if (ret < 0) {
  23004. goto done;
  23005. }
  23006. #endif /* HAVE_ECC160 */
  23007. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  23008. ret = ecc_test_curve(&rng, 24);
  23009. if (ret < 0) {
  23010. goto done;
  23011. }
  23012. #endif /* HAVE_ECC192 */
  23013. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  23014. ret = ecc_test_curve(&rng, 28);
  23015. if (ret < 0) {
  23016. goto done;
  23017. }
  23018. #endif /* HAVE_ECC224 */
  23019. #if (defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 239
  23020. ret = ecc_test_curve(&rng, 30);
  23021. if (ret < 0) {
  23022. goto done;
  23023. }
  23024. #endif /* HAVE_ECC239 */
  23025. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  23026. ret = ecc_test_curve(&rng, 32);
  23027. if (ret < 0) {
  23028. goto done;
  23029. }
  23030. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  23031. defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \
  23032. !defined(WOLFSSL_NO_MALLOC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23033. ret = ecc_point_test();
  23034. if (ret < 0) {
  23035. goto done;
  23036. }
  23037. #endif
  23038. ret = ecc_def_curve_test(&rng);
  23039. if (ret < 0) {
  23040. goto done;
  23041. }
  23042. #endif /* !NO_ECC256 */
  23043. #if (defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 320
  23044. ret = ecc_test_curve(&rng, 40);
  23045. if (ret < 0) {
  23046. goto done;
  23047. }
  23048. #endif /* HAVE_ECC320 */
  23049. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  23050. ret = ecc_test_curve(&rng, 48);
  23051. if (ret < 0) {
  23052. goto done;
  23053. }
  23054. #endif /* HAVE_ECC384 */
  23055. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  23056. ret = ecc_test_curve(&rng, 64);
  23057. if (ret < 0) {
  23058. goto done;
  23059. }
  23060. #endif /* HAVE_ECC512 */
  23061. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  23062. ret = ecc_test_curve(&rng, 66);
  23063. if (ret < 0) {
  23064. goto done;
  23065. }
  23066. #endif /* HAVE_ECC521 */
  23067. #if defined(WOLFSSL_CUSTOM_CURVES)
  23068. ret = ecc_test_custom_curves(&rng);
  23069. if (ret != 0) {
  23070. goto done;
  23071. }
  23072. #endif
  23073. #if defined(HAVE_ECC_SIGN) && (defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  23074. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)) \
  23075. && (!defined(FIPS_VERSION_GE) || FIPS_VERSION_GE(5,3))
  23076. #ifdef HAVE_ECC256
  23077. ret = ecc_test_deterministic_k(&rng);
  23078. if (ret != 0) {
  23079. printf("ecc_test_deterministic_k failed! %d\n", ret);
  23080. goto done;
  23081. }
  23082. #endif
  23083. #ifdef WOLFSSL_PUBLIC_MP
  23084. #if defined(HAVE_ECC384)
  23085. ret = ecc384_test_deterministic_k(&rng);
  23086. if (ret != 0) {
  23087. printf("ecc384_test_deterministic_k failed! %d\n", ret);
  23088. goto done;
  23089. }
  23090. #endif
  23091. #if defined(HAVE_ECC521)
  23092. ret = ecc521_test_deterministic_k(&rng);
  23093. if (ret != 0) {
  23094. printf("ecc512_test_deterministic_k failed! %d\n", ret);
  23095. goto done;
  23096. }
  23097. #endif
  23098. #endif
  23099. #endif
  23100. #if defined(HAVE_ECC_SIGN) && defined(WOLFSSL_ECDSA_SET_K) && \
  23101. !defined(WOLFSSL_KCAPI_ECC)
  23102. ret = ecc_test_sign_vectors(&rng);
  23103. if (ret != 0) {
  23104. printf("ecc_test_sign_vectors failed! %d\n", ret);
  23105. goto done;
  23106. }
  23107. #endif
  23108. #ifdef HAVE_ECC_CDH
  23109. ret = ecc_test_cdh_vectors(&rng);
  23110. if (ret != 0) {
  23111. printf("ecc_test_cdh_vectors failed! %d\n", ret);
  23112. goto done;
  23113. }
  23114. #endif
  23115. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  23116. !defined(WOLFSSL_STM32_PKA) && !defined(WOLFSSL_SILABS_SE_ACCEL) && \
  23117. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23118. ret = ecc_test_make_pub(&rng);
  23119. if (ret != 0) {
  23120. printf("ecc_test_make_pub failed!: %d\n", ret);
  23121. goto done;
  23122. }
  23123. #elif defined(HAVE_ECC_KEY_IMPORT)
  23124. (void)ecc_test_make_pub; /* for compiler warning */
  23125. #endif
  23126. #ifdef WOLFSSL_CERT_GEN
  23127. ret = ecc_test_cert_gen(&rng);
  23128. if (ret != 0) {
  23129. printf("ecc_test_cert_gen failed!: %d\n", ret);
  23130. goto done;
  23131. }
  23132. #endif
  23133. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC) && \
  23134. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23135. ret = ecc_test_allocator(&rng);
  23136. if (ret != 0) {
  23137. printf("ecc_test_allocator failed!: %d\n", ret);
  23138. goto done;
  23139. }
  23140. #endif
  23141. #if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_PUBLIC_MP) && \
  23142. defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY)
  23143. ret = ecc_test_nonblock(&rng);
  23144. if (ret != 0) {
  23145. printf("ecc_test_nonblock failed!: %d\n", ret);
  23146. goto done;
  23147. }
  23148. #endif
  23149. done:
  23150. wc_FreeRng(&rng);
  23151. return ret;
  23152. }
  23153. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_AES_CBC) && \
  23154. (defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_256))
  23155. /* ecc_encrypt_e2e_test() uses wc_ecc_ctx_set_algo(), which was added in
  23156. * wolfFIPS 5.3.
  23157. * ecc_encrypt_kat() is used only by ecc_encrypt_e2e_test().
  23158. */
  23159. #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))
  23160. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  23161. ECC_MIN_KEY_SZ <= 256 && defined(WOLFSSL_AES_128)
  23162. static int ecc_encrypt_kat(WC_RNG *rng)
  23163. {
  23164. int ret = 0;
  23165. #ifdef WOLFSSL_ECIES_OLD
  23166. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23167. ecc_key* userA = NULL;
  23168. #else
  23169. ecc_key userA[1];
  23170. #endif
  23171. int userAInit = 0;
  23172. #endif
  23173. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23174. ecc_key* userB = NULL;
  23175. #else
  23176. ecc_key userB[1];
  23177. #endif
  23178. int userBInit = 0;
  23179. ecc_key* tmpKey;
  23180. byte plain[48];
  23181. word32 plainSz = sizeof(plain);
  23182. WOLFSSL_SMALL_STACK_STATIC const byte privKey[] = {
  23183. 0x04, 0x80, 0xef, 0x1d, 0xbe, 0x02, 0x0c, 0x20,
  23184. 0x5b, 0xab, 0x80, 0x35, 0x5b, 0x2a, 0x0f, 0x6d,
  23185. 0xd3, 0xb0, 0x7f, 0x7e, 0x7f, 0x86, 0x8a, 0x49,
  23186. 0xee, 0xb4, 0xaa, 0x09, 0x2d, 0x1e, 0x1d, 0x02
  23187. };
  23188. #if defined(WOLFSSL_ECIES_OLD) || defined(WOLFSSL_QNX_CAAM)
  23189. WOLFSSL_SMALL_STACK_STATIC const byte pubKey[] = {
  23190. 0x04,
  23191. /* X */
  23192. 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0, 0x5a,
  23193. 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c, 0x3a,
  23194. 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3, 0xc1,
  23195. 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63, 0xa0,
  23196. /* X */
  23197. 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7, 0xcd,
  23198. 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75, 0xaa,
  23199. 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe, 0xe8,
  23200. 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3, 0x28
  23201. };
  23202. #endif
  23203. WOLFSSL_SMALL_STACK_STATIC const byte enc_msg[] = {
  23204. #ifdef WOLFSSL_ECIES_OLD
  23205. 0x42, 0x70, 0xbf, 0xf9, 0xf4, 0x7e, 0x4b, 0x9b,
  23206. 0xb5, 0x4c, 0xcc, 0xc5, 0x94, 0xa7, 0xef, 0xaa,
  23207. 0xc3, 0x7c, 0x85, 0xa6, 0x51, 0x6e, 0xd3, 0xfa,
  23208. 0x56, 0xc9, 0x10, 0x4d, 0x14, 0x32, 0x61, 0xb8,
  23209. 0xbb, 0x66, 0x7a, 0xb5, 0xbc, 0x95, 0xf8, 0xca,
  23210. 0xd1, 0x2a, 0x19, 0x51, 0x44, 0xd8, 0x0e, 0x57,
  23211. 0x34, 0xed, 0x45, 0x89, 0x2e, 0x57, 0xbe, 0xd5,
  23212. 0x06, 0x22, 0xd7, 0x13, 0x0a, 0x0e, 0x40, 0x36,
  23213. 0x0d, 0x05, 0x0d, 0xb6, 0xae, 0x61, 0x37, 0x18,
  23214. 0x83, 0x90, 0x0a, 0x27, 0x95, 0x41, 0x8c, 0x45
  23215. #elif defined(WOLFSSL_ECIES_ISO18033)
  23216. 0x04, 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0,
  23217. 0x5a, 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c,
  23218. 0x3a, 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3,
  23219. 0xc1, 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63,
  23220. 0xa0, 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7,
  23221. 0xcd, 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75,
  23222. 0xaa, 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe,
  23223. 0xe8, 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3,
  23224. 0x28, 0xbb, 0x9f, 0xa8, 0x2d, 0xe1, 0xf1, 0x67,
  23225. 0x45, 0x02, 0x19, 0xdc, 0xc8, 0x24, 0x8b, 0x20,
  23226. 0x02, 0xa0, 0x8f, 0x95, 0x12, 0x55, 0x51, 0xf8,
  23227. 0x03, 0xc4, 0x54, 0x13, 0x98, 0x2d, 0xf0, 0x31,
  23228. 0x51, 0x80, 0x45, 0x24, 0xcb, 0x8b, 0x48, 0xa6,
  23229. 0x8b, 0x8e, 0x97, 0x9c, 0x56, 0x4d, 0x70, 0x00,
  23230. 0x53, 0xd3, 0x47, 0x00, 0x5a, 0x23, 0x8c, 0xf9,
  23231. 0xfd, 0xd2, 0x33, 0x2c, 0x43, 0x6e, 0x9e, 0xb2,
  23232. 0xf4, 0x95, 0xd4, 0xcf, 0x30, 0xd6, 0xa2, 0xc5,
  23233. 0x35, 0x96, 0x6a, 0xd4, 0x36, 0x15, 0xa9, 0xbd,
  23234. 0x7f
  23235. #elif defined(WOLFSSL_ECIES_GEN_IV)
  23236. /* EC P-256 point */
  23237. 0x04,
  23238. /* X */
  23239. 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0, 0x5a,
  23240. 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c, 0x3a,
  23241. 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3, 0xc1,
  23242. 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63, 0xa0,
  23243. /* Y */
  23244. 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7, 0xcd,
  23245. 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75, 0xaa,
  23246. 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe, 0xe8,
  23247. 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3, 0x28,
  23248. /* IV */
  23249. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  23250. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  23251. /* Encrypted Msg */
  23252. 0xe5, 0x17, 0xaf, 0x0d, 0x65, 0x4d, 0x3d, 0x50,
  23253. 0x96, 0x05, 0xc9, 0x63, 0x2c, 0xef, 0x1c, 0x1f,
  23254. 0x78, 0xc9, 0x90, 0x7a, 0x14, 0x00, 0xfc, 0x44,
  23255. 0x71, 0x6d, 0x57, 0x8c, 0xdf, 0x23, 0xca, 0x65,
  23256. 0xcf, 0x93, 0x06, 0xb6, 0x9a, 0xf4, 0x61, 0xbd,
  23257. 0x44, 0x1a, 0xeb, 0x52, 0x68, 0x0f, 0xd1, 0xde,
  23258. /* HMAC */
  23259. 0x5a, 0x22, 0xc1, 0x5d, 0x99, 0x66, 0x3f, 0x24,
  23260. 0x35, 0x96, 0xac, 0xf7, 0xf6, 0x28, 0x45, 0x16,
  23261. 0x52, 0x19, 0x0d, 0xe4, 0xb2, 0xca, 0x5b, 0x28,
  23262. 0x4e, 0xbb, 0xf3, 0x98, 0x57, 0xd7, 0x3b, 0xe2
  23263. #else
  23264. 0x04, 0x50, 0xf2, 0x93, 0xa2, 0x48, 0xa9, 0xc0,
  23265. 0x5a, 0x9a, 0xa7, 0x70, 0x34, 0xb7, 0x7f, 0x4c,
  23266. 0x3a, 0xad, 0xfc, 0xd8, 0xb6, 0x76, 0x0a, 0xe3,
  23267. 0xc1, 0x87, 0x17, 0x07, 0x2d, 0x8d, 0xa3, 0x63,
  23268. 0xa0, 0xc1, 0x27, 0xb2, 0x97, 0x9b, 0x84, 0xe7,
  23269. 0xcd, 0x20, 0x65, 0x8d, 0x2b, 0x6a, 0x93, 0x75,
  23270. 0xaa, 0x8b, 0xe1, 0x3a, 0x7b, 0x24, 0x1a, 0xbe,
  23271. 0xe8, 0x36, 0xd2, 0xe6, 0x34, 0x8a, 0x7a, 0xb3,
  23272. 0x28, 0xe5, 0x17, 0xaf, 0x0d, 0x65, 0x4d, 0x3d,
  23273. 0x50, 0x96, 0x05, 0xc9, 0x63, 0x2c, 0xef, 0x1c,
  23274. 0x1f, 0x78, 0xc9, 0x90, 0x7a, 0x14, 0x00, 0xfc,
  23275. 0x44, 0x71, 0x6d, 0x57, 0x8c, 0xdf, 0x23, 0xca,
  23276. 0x65, 0xcf, 0x93, 0x06, 0xb6, 0x9a, 0xf4, 0x61,
  23277. 0xbd, 0x44, 0x1a, 0xeb, 0x52, 0x68, 0x0f, 0xd1,
  23278. 0xde, 0xc7, 0x3f, 0x6f, 0xce, 0xbe, 0x49, 0x61,
  23279. 0x48, 0x01, 0x77, 0x41, 0xd0, 0xd8, 0x5b, 0x48,
  23280. 0xca, 0x4e, 0x47, 0x3e, 0x47, 0xbf, 0x1d, 0x28,
  23281. 0x4c, 0x18, 0x1a, 0xfb, 0x96, 0x95, 0xda, 0xde,
  23282. 0x55
  23283. #endif
  23284. };
  23285. WOLFSSL_SMALL_STACK_STATIC const byte msg[] = {
  23286. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  23287. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  23288. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  23289. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  23290. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  23291. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  23292. };
  23293. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23294. userB = (ecc_key *)XMALLOC(sizeof(*userB), HEAP_HINT,
  23295. DYNAMIC_TYPE_TMP_BUFFER);
  23296. if (userB == NULL) {
  23297. ret = -10451;
  23298. }
  23299. #ifdef WOLFSSL_ECIES_OLD
  23300. if (ret == 0) {
  23301. userA = (ecc_key *)XMALLOC(sizeof(*userA), HEAP_HINT,
  23302. DYNAMIC_TYPE_TMP_BUFFER);
  23303. if (userA == NULL) {
  23304. ret = -10450;
  23305. }
  23306. }
  23307. #endif
  23308. #endif
  23309. if (ret == 0) {
  23310. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  23311. if (ret != 0)
  23312. ret = -10453;
  23313. }
  23314. if (ret == 0) {
  23315. userBInit = 1;
  23316. #ifdef WOLFSSL_ECIES_OLD
  23317. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  23318. if (ret != 0)
  23319. ret = -10452;
  23320. }
  23321. if (ret == 0) {
  23322. userAInit = 1;
  23323. tmpKey = userA;
  23324. #else
  23325. tmpKey = NULL;
  23326. #endif
  23327. }
  23328. if (ret == 0) {
  23329. #ifdef WOLFSSL_QNX_CAAM
  23330. ret = wc_ecc_import_private_key_ex(privKey, sizeof(privKey), pubKey,
  23331. sizeof(pubKey), userB, ECC_SECP256R1);
  23332. #else
  23333. ret = wc_ecc_import_private_key_ex(privKey, sizeof(privKey), NULL, 0,
  23334. userB, ECC_SECP256R1);
  23335. #endif
  23336. if (ret != 0)
  23337. ret = -10454;
  23338. }
  23339. #ifdef WOLFSSL_ECIES_OLD
  23340. if (ret == 0) {
  23341. ret = wc_ecc_import_x963_ex(pubKey, sizeof(pubKey), userA,
  23342. ECC_SECP256R1);
  23343. if (ret != 0)
  23344. ret = -10455;
  23345. }
  23346. #endif
  23347. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  23348. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  23349. !defined(HAVE_SELFTEST)
  23350. if (ret == 0) {
  23351. ret = wc_ecc_set_rng(userB, rng);
  23352. if (ret != 0) {
  23353. ret = -10456;
  23354. }
  23355. }
  23356. #else
  23357. (void)rng;
  23358. #endif
  23359. if (ret == 0) {
  23360. ret = wc_ecc_decrypt(userB, tmpKey, enc_msg, sizeof(enc_msg), plain,
  23361. &plainSz, NULL);
  23362. if (ret != 0)
  23363. ret = -10457;
  23364. }
  23365. if (ret == 0) {
  23366. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  23367. ret = -10458;
  23368. }
  23369. }
  23370. if (userBInit)
  23371. wc_ecc_free(userB);
  23372. #ifdef WOLFSSL_ECIES_OLD
  23373. if (userAInit)
  23374. wc_ecc_free(userA);
  23375. #endif
  23376. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23377. if (userB != NULL) {
  23378. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23379. }
  23380. #ifdef WOLFSSL_ECIES_OLD
  23381. if (userA != NULL) {
  23382. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23383. }
  23384. #endif
  23385. #endif
  23386. return ret;
  23387. }
  23388. #endif
  23389. static int ecc_encrypt_e2e_test(WC_RNG* rng, ecc_key* userA, ecc_key* userB,
  23390. byte encAlgo, byte kdfAlgo, byte macAlgo)
  23391. {
  23392. int ret = 0;
  23393. byte msg[48];
  23394. byte plain[48];
  23395. #ifdef WOLFSSL_ECIES_OLD
  23396. byte out[80];
  23397. #elif defined(WOLFSSL_ECIES_GEN_IV)
  23398. byte out[1 + ECC_KEYGEN_SIZE * 2 + 16 + 80];
  23399. #else
  23400. byte out[1 + ECC_KEYGEN_SIZE * 2 + 80];
  23401. #endif
  23402. word32 outSz = sizeof(out);
  23403. word32 plainSz = sizeof(plain);
  23404. int i;
  23405. ecEncCtx* cliCtx = NULL;
  23406. ecEncCtx* srvCtx = NULL;
  23407. byte cliSalt[EXCHANGE_SALT_SZ];
  23408. byte srvSalt[EXCHANGE_SALT_SZ];
  23409. const byte* tmpSalt;
  23410. byte msg2[48];
  23411. byte plain2[48];
  23412. #ifdef WOLFSSL_ECIES_OLD
  23413. byte out2[80];
  23414. #elif defined(WOLFSSL_ECIES_GEN_IV)
  23415. byte out2[1 + ECC_KEYGEN_SIZE * 2 + 16 + 80];
  23416. #else
  23417. byte out2[1 + ECC_KEYGEN_SIZE * 2 + 80];
  23418. #endif
  23419. word32 outSz2 = sizeof(out2);
  23420. word32 plainSz2 = sizeof(plain2);
  23421. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23422. ecc_key *tmpKey = (ecc_key *)XMALLOC(sizeof(ecc_key), HEAP_HINT,
  23423. DYNAMIC_TYPE_TMP_BUFFER);
  23424. #else
  23425. ecc_key tmpKey[1];
  23426. #endif
  23427. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23428. if (tmpKey == NULL) {
  23429. ERROR_OUT(MEMORY_E, done);
  23430. }
  23431. #endif
  23432. ret = wc_ecc_init_ex(tmpKey, HEAP_HINT, devId);
  23433. if (ret != 0)
  23434. goto done;
  23435. /* set message to incrementing 0,1,2,etc... */
  23436. for (i = 0; i < (int)sizeof(msg); i++)
  23437. msg[i] = i;
  23438. /* encrypt msg to B */
  23439. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz, NULL);
  23440. if (ret != 0) {
  23441. ret = -10405; goto done;
  23442. }
  23443. #ifdef WOLFSSL_ECIES_OLD
  23444. tmpKey->dp = userA->dp;
  23445. ret = wc_ecc_copy_point(&userA->pubkey, &tmpKey->pubkey);
  23446. if (ret != 0) {
  23447. ret = -10413; goto done;
  23448. }
  23449. #endif
  23450. /* decrypt msg from A */
  23451. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, NULL);
  23452. if (ret != 0) {
  23453. ret = -10406; goto done;
  23454. }
  23455. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  23456. ret = -10407; goto done;
  23457. }
  23458. #ifndef WOLFSSL_ECIES_OLD
  23459. /* A decrypts msg (response) from B */
  23460. ret = wc_ecc_decrypt(userB, NULL, out, outSz, plain2, &plainSz2, NULL);
  23461. if (ret != 0)
  23462. goto done;
  23463. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  23464. ret = -10415; goto done;
  23465. }
  23466. #endif
  23467. /* let's verify message exchange works, A is client, B is server */
  23468. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  23469. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  23470. if (cliCtx == NULL || srvCtx == NULL) {
  23471. ret = -10408; goto done;
  23472. }
  23473. ret = wc_ecc_ctx_set_algo(cliCtx, encAlgo, kdfAlgo, macAlgo);
  23474. if (ret != 0)
  23475. goto done;
  23476. ret = wc_ecc_ctx_set_algo(srvCtx, encAlgo, kdfAlgo, macAlgo);
  23477. if (ret != 0)
  23478. goto done;
  23479. /* get salt to send to peer */
  23480. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  23481. if (tmpSalt == NULL) {
  23482. ret = -10409; goto done;
  23483. }
  23484. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  23485. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  23486. if (tmpSalt == NULL) {
  23487. ret = -10410; goto done;
  23488. }
  23489. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  23490. /* in actual use, we'd get the peer's salt over the transport */
  23491. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  23492. if (ret != 0)
  23493. goto done;
  23494. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  23495. if (ret != 0)
  23496. goto done;
  23497. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 11);
  23498. if (ret != 0)
  23499. goto done;
  23500. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 11);
  23501. if (ret != 0)
  23502. goto done;
  23503. /* get encrypted msg (request) to send to B */
  23504. outSz = sizeof(out);
  23505. ret = wc_ecc_encrypt(userA, userB, msg, sizeof(msg), out, &outSz,cliCtx);
  23506. if (ret != 0)
  23507. goto done;
  23508. #ifndef WOLFSSL_ECIES_OLD
  23509. wc_ecc_free(tmpKey);
  23510. #endif
  23511. /* B decrypts msg (request) from A */
  23512. plainSz = sizeof(plain);
  23513. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, srvCtx);
  23514. if (ret != 0)
  23515. goto done;
  23516. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  23517. ret = -10411; goto done;
  23518. }
  23519. /* msg2 (response) from B to A */
  23520. for (i = 0; i < (int)sizeof(msg2); i++)
  23521. msg2[i] = i + sizeof(msg2);
  23522. /* get encrypted msg (response) to send to B */
  23523. ret = wc_ecc_encrypt(userB, userA, msg2, sizeof(msg2), out2,
  23524. &outSz2, srvCtx);
  23525. if (ret != 0)
  23526. goto done;
  23527. #ifdef WOLFSSL_ECIES_OLD
  23528. tmpKey->dp = userB->dp;
  23529. ret = wc_ecc_copy_point(&userB->pubkey, &tmpKey->pubkey);
  23530. if (ret != 0) {
  23531. ret = -10414; goto done;
  23532. }
  23533. #else
  23534. wc_ecc_free(tmpKey);
  23535. #endif
  23536. /* A decrypts msg (response) from B */
  23537. ret = wc_ecc_decrypt(userA, tmpKey, out2, outSz2, plain2, &plainSz2,
  23538. cliCtx);
  23539. if (ret != 0)
  23540. goto done;
  23541. if (XMEMCMP(plain2, msg2, sizeof(msg2)) != 0) {
  23542. ret = -10412; goto done;
  23543. }
  23544. #if defined(HAVE_COMP_KEY) && \
  23545. (! defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  23546. /* Create new client and server contexts. */
  23547. wc_ecc_ctx_free(srvCtx);
  23548. wc_ecc_ctx_free(cliCtx);
  23549. /* let's verify message exchange works, A is client, B is server */
  23550. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, rng);
  23551. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, rng);
  23552. if (cliCtx == NULL || srvCtx == NULL) {
  23553. ret = -10416; goto done;
  23554. }
  23555. ret = wc_ecc_ctx_set_algo(cliCtx, encAlgo, kdfAlgo, macAlgo);
  23556. if (ret != 0)
  23557. goto done;
  23558. ret = wc_ecc_ctx_set_algo(srvCtx, encAlgo, kdfAlgo, macAlgo);
  23559. if (ret != 0)
  23560. goto done;
  23561. /* get salt to send to peer */
  23562. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  23563. if (tmpSalt == NULL) {
  23564. ret = -10417; goto done;
  23565. }
  23566. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  23567. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  23568. if (tmpSalt == NULL) {
  23569. ret = -10418; goto done;
  23570. }
  23571. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  23572. /* in actual use, we'd get the peer's salt over the transport */
  23573. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  23574. if (ret != 0)
  23575. goto done;
  23576. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  23577. if (ret != 0)
  23578. goto done;
  23579. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 12);
  23580. if (ret != 0)
  23581. goto done;
  23582. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 12);
  23583. if (ret != 0)
  23584. goto done;
  23585. /* get encrypted msg (request) to send to B - compressed public key */
  23586. outSz = sizeof(out);
  23587. ret = wc_ecc_encrypt_ex(userA, userB, msg, sizeof(msg), out, &outSz, cliCtx,
  23588. 1);
  23589. if (ret != 0)
  23590. goto done;
  23591. #ifndef WOLFSSL_ECIES_OLD
  23592. wc_ecc_free(tmpKey);
  23593. #endif
  23594. /* B decrypts msg (request) from A - out has a compressed public key */
  23595. plainSz = sizeof(plain);
  23596. ret = wc_ecc_decrypt(userB, tmpKey, out, outSz, plain, &plainSz, srvCtx);
  23597. if (ret != 0)
  23598. goto done;
  23599. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  23600. ret = -10419; goto done;
  23601. }
  23602. #endif /* HAVE_COMP_KEY && (!FIPS || FIPS>=5.3) */
  23603. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  23604. (ECC_MIN_KEY_SZ <= 256) && defined(WOLFSSL_AES_128)
  23605. ret = ecc_encrypt_kat(rng);
  23606. #endif
  23607. done:
  23608. /* cleanup */
  23609. wc_ecc_ctx_free(srvCtx);
  23610. wc_ecc_ctx_free(cliCtx);
  23611. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23612. if (tmpKey != NULL) {
  23613. wc_ecc_free(tmpKey);
  23614. XFREE(tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23615. }
  23616. #else
  23617. wc_ecc_free(tmpKey);
  23618. #endif
  23619. return ret;
  23620. }
  23621. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  23622. WOLFSSL_TEST_SUBROUTINE int ecc_encrypt_test(void)
  23623. {
  23624. WC_RNG rng;
  23625. int ret;
  23626. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23627. ecc_key *userA;
  23628. ecc_key *userB;
  23629. #else
  23630. ecc_key userA[1];
  23631. ecc_key userB[1];
  23632. #endif
  23633. #ifndef HAVE_FIPS
  23634. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  23635. #else
  23636. ret = wc_InitRng(&rng);
  23637. #endif
  23638. if (ret != 0)
  23639. return -10400;
  23640. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23641. userA = (ecc_key *)XMALLOC(sizeof *userA, HEAP_HINT,
  23642. DYNAMIC_TYPE_TMP_BUFFER);
  23643. userB = (ecc_key *)XMALLOC(sizeof *userB, HEAP_HINT,
  23644. DYNAMIC_TYPE_TMP_BUFFER);
  23645. if ((userA == NULL) || (userB == NULL)) {
  23646. ERROR_OUT(MEMORY_E, done);
  23647. }
  23648. #endif
  23649. XMEMSET(userA, 0, sizeof *userA);
  23650. XMEMSET(userB, 0, sizeof *userB);
  23651. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  23652. if (ret != 0)
  23653. goto done;
  23654. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  23655. if (ret != 0)
  23656. goto done;
  23657. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userA);
  23658. #if defined(WOLFSSL_ASYNC_CRYPT)
  23659. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  23660. #endif
  23661. if (ret != 0){
  23662. ret = -10401; goto done;
  23663. }
  23664. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, userB);
  23665. #if defined(WOLFSSL_ASYNC_CRYPT)
  23666. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  23667. #endif
  23668. if (ret != 0){
  23669. ret = -10402; goto done;
  23670. }
  23671. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  23672. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  23673. !defined(HAVE_SELFTEST)
  23674. ret = wc_ecc_set_rng(userA, &rng);
  23675. if (ret != 0) {
  23676. ret = -10403; goto done;
  23677. }
  23678. ret = wc_ecc_set_rng(userB, &rng);
  23679. if (ret != 0) {
  23680. ret = -10404; goto done;
  23681. }
  23682. #endif
  23683. #if !defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3))
  23684. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  23685. #ifdef WOLFSSL_AES_128
  23686. if (ret == 0) {
  23687. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CBC,
  23688. ecHKDF_SHA256, ecHMAC_SHA256);
  23689. if (ret != 0) {
  23690. printf("ECIES: AES_128_CBC, HKDF_SHA256, HMAC_SHA256\n");
  23691. }
  23692. }
  23693. #endif
  23694. #ifdef WOLFSSL_AES_256
  23695. if (ret == 0) {
  23696. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_256_CBC,
  23697. ecHKDF_SHA256, ecHMAC_SHA256);
  23698. if (ret != 0) {
  23699. printf("ECIES: AES_256_CBC, HKDF_SHA256, HMAC_SHA256\n");
  23700. }
  23701. }
  23702. #endif
  23703. #endif
  23704. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER)
  23705. #ifdef WOLFSSL_AES_128
  23706. if (ret == 0) {
  23707. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_128_CTR,
  23708. ecHKDF_SHA256, ecHMAC_SHA256);
  23709. if (ret != 0) {
  23710. printf("ECIES: AES_128_CTR, HKDF_SHA256, HMAC_SHA256\n");
  23711. }
  23712. }
  23713. #endif
  23714. #ifdef WOLFSSL_AES_256
  23715. if (ret == 0) {
  23716. ret = ecc_encrypt_e2e_test(&rng, userA, userB, ecAES_256_CTR,
  23717. ecHKDF_SHA256, ecHMAC_SHA256);
  23718. if (ret != 0) {
  23719. printf("ECIES: AES_256_CTR, HKDF_SHA256, HMAC_SHA256\n");
  23720. }
  23721. }
  23722. #endif
  23723. #endif
  23724. #endif /* !HAVE_FIPS || FIPS_VERSION_GE(5,3) */
  23725. done:
  23726. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23727. if (userA != NULL) {
  23728. wc_ecc_free(userA);
  23729. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23730. }
  23731. if (userB != NULL) {
  23732. wc_ecc_free(userB);
  23733. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23734. }
  23735. #else
  23736. wc_ecc_free(userB);
  23737. wc_ecc_free(userA);
  23738. #endif
  23739. wc_FreeRng(&rng);
  23740. return ret;
  23741. }
  23742. #endif /* HAVE_ECC_ENCRYPT && HAVE_AES_CBC && WOLFSSL_AES_128 */
  23743. #if defined(USE_CERT_BUFFERS_256) && !defined(WOLFSSL_ATECC508A) && \
  23744. !defined(WOLFSSL_ATECC608A) && !defined(NO_ECC256) && \
  23745. defined(HAVE_ECC_VERIFY) && defined(HAVE_ECC_SIGN) && \
  23746. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  23747. WOLFSSL_TEST_SUBROUTINE int ecc_test_buffers(void)
  23748. {
  23749. size_t bytes;
  23750. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23751. ecc_key *cliKey = (ecc_key *)XMALLOC(sizeof *cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23752. ecc_key *servKey = (ecc_key *)XMALLOC(sizeof *servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23753. ecc_key *tmpKey = (ecc_key *)XMALLOC(sizeof *tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23754. #else
  23755. ecc_key cliKey[1];
  23756. ecc_key servKey[1];
  23757. ecc_key tmpKey[1];
  23758. #endif
  23759. WC_RNG rng;
  23760. word32 idx = 0;
  23761. int ret;
  23762. /* pad our test message to 32 bytes so evenly divisible by AES_BLOCK_SZ */
  23763. byte in[] = "Everyone gets Friday off. ecc p";
  23764. word32 inLen = (word32)XSTRLEN((char*)in);
  23765. byte out[256];
  23766. byte plain[256];
  23767. int verify = 0;
  23768. word32 x;
  23769. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23770. if ((cliKey == NULL) || (servKey == NULL) || (tmpKey == NULL))
  23771. ERROR_OUT(MEMORY_E, done);
  23772. #endif
  23773. ret = wc_ecc_init_ex(cliKey, HEAP_HINT, devId);
  23774. if (ret != 0)
  23775. ERROR_OUT(-10420, done);
  23776. ret = wc_ecc_init_ex(servKey, HEAP_HINT, devId);
  23777. if (ret != 0)
  23778. ERROR_OUT(-10421, done);
  23779. ret = wc_ecc_init_ex(tmpKey, HEAP_HINT, devId);
  23780. if (ret != 0)
  23781. ERROR_OUT(-10421, done);
  23782. bytes = (size_t)sizeof_ecc_clikey_der_256;
  23783. /* place client key into ecc_key struct cliKey */
  23784. ret = wc_EccPrivateKeyDecode(ecc_clikey_der_256, &idx, cliKey,
  23785. (word32)bytes);
  23786. if (ret != 0)
  23787. ERROR_OUT(-10422, done);
  23788. idx = 0;
  23789. bytes = (size_t)sizeof_ecc_key_der_256;
  23790. /* place server key into ecc_key struct servKey */
  23791. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, servKey,
  23792. (word32)bytes);
  23793. if (ret != 0)
  23794. ERROR_OUT(-10423, done);
  23795. #ifndef WC_NO_RNG
  23796. #ifndef HAVE_FIPS
  23797. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  23798. #else
  23799. ret = wc_InitRng(&rng);
  23800. #endif
  23801. if (ret != 0)
  23802. ERROR_OUT(-10424, done);
  23803. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  23804. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  23805. !defined(HAVE_SELFTEST)
  23806. ret = wc_ecc_set_rng(cliKey, &rng);
  23807. if (ret != 0) {
  23808. ERROR_OUT(-10425, done);
  23809. }
  23810. ret = wc_ecc_set_rng(servKey, &rng);
  23811. if (ret != 0) {
  23812. ERROR_OUT(-10425, done);
  23813. }
  23814. #endif
  23815. #endif /* !WC_NO_RNG */
  23816. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_HKDF) && \
  23817. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  23818. {
  23819. word32 y;
  23820. /* test encrypt and decrypt if they're available */
  23821. x = sizeof(out);
  23822. ret = wc_ecc_encrypt(cliKey, servKey, in, sizeof(in), out, &x, NULL);
  23823. if (ret < 0)
  23824. ERROR_OUT(-10426, done);
  23825. #ifdef WOLFSSL_ECIES_OLD
  23826. tmpKey->dp = cliKey->dp;
  23827. ret = wc_ecc_copy_point(&cliKey->pubkey, &tmpKey->pubkey);
  23828. if (ret != 0) {
  23829. ret = -10414; goto done;
  23830. }
  23831. #endif
  23832. y = sizeof(plain);
  23833. ret = wc_ecc_decrypt(servKey, tmpKey, out, x, plain, &y, NULL);
  23834. if (ret < 0)
  23835. ERROR_OUT(-10427, done);
  23836. if (XMEMCMP(plain, in, inLen))
  23837. ERROR_OUT(-10428, done);
  23838. }
  23839. #endif
  23840. x = sizeof(out);
  23841. do {
  23842. #if defined(WOLFSSL_ASYNC_CRYPT)
  23843. ret = wc_AsyncWait(ret, &cliKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  23844. #endif
  23845. if (ret == 0)
  23846. ret = wc_ecc_sign_hash(in, inLen, out, &x, &rng, cliKey);
  23847. } while (ret == WC_PENDING_E);
  23848. if (ret < 0)
  23849. ERROR_OUT(-10429, done);
  23850. TEST_SLEEP();
  23851. XMEMSET(plain, 0, sizeof(plain));
  23852. do {
  23853. #if defined(WOLFSSL_ASYNC_CRYPT)
  23854. ret = wc_AsyncWait(ret, &cliKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  23855. #endif
  23856. if (ret == 0)
  23857. ret = wc_ecc_verify_hash(out, x, in, inLen, &verify,
  23858. cliKey);
  23859. } while (ret == WC_PENDING_E);
  23860. if (ret < 0)
  23861. ERROR_OUT(-10430, done);
  23862. if (verify != 1)
  23863. ERROR_OUT(-10431, done);
  23864. TEST_SLEEP();
  23865. #ifdef WOLFSSL_CERT_EXT
  23866. idx = 0;
  23867. bytes = sizeof_ecc_clikeypub_der_256;
  23868. ret = wc_EccPublicKeyDecode(ecc_clikeypub_der_256, &idx, cliKey,
  23869. (word32) bytes);
  23870. if (ret != 0)
  23871. ERROR_OUT(-10432, done);
  23872. #endif
  23873. ret = 0;
  23874. done:
  23875. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  23876. if (cliKey != NULL) {
  23877. wc_ecc_free(cliKey);
  23878. XFREE(cliKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23879. }
  23880. if (servKey != NULL) {
  23881. wc_ecc_free(servKey);
  23882. XFREE(servKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23883. }
  23884. if (tmpKey != NULL) {
  23885. wc_ecc_free(tmpKey);
  23886. XFREE(tmpKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23887. }
  23888. #else
  23889. wc_ecc_free(cliKey);
  23890. wc_ecc_free(servKey);
  23891. wc_ecc_free(tmpKey);
  23892. #endif
  23893. wc_FreeRng(&rng);
  23894. return ret;
  23895. }
  23896. #endif /* USE_CERT_BUFFERS_256 && !WOLFSSL_ATECCX08A && !NO_ECC256 */
  23897. #endif /* HAVE_ECC */
  23898. #ifdef HAVE_CURVE25519
  23899. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  23900. defined(HAVE_CURVE25519_KEY_IMPORT)
  23901. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  23902. #define X25519_TEST_CNT 5
  23903. #else
  23904. #define X25519_TEST_CNT 1
  23905. #endif
  23906. static int curve25519_overflow_test(void)
  23907. {
  23908. /* secret key for party a */
  23909. byte sa[X25519_TEST_CNT][32] = {
  23910. {
  23911. 0x8d,0xaf,0x6e,0x7a,0xc1,0xeb,0x8d,0x30,
  23912. 0x99,0x86,0xd3,0x90,0x47,0x96,0x21,0x3c,
  23913. 0x3a,0x75,0xc0,0x7b,0x75,0x01,0x75,0xa3,
  23914. 0x81,0x4b,0xff,0x5a,0xbc,0x96,0x87,0x28
  23915. },
  23916. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  23917. {
  23918. 0x9d,0x63,0x5f,0xce,0xe2,0xe8,0xd7,0xfb,
  23919. 0x68,0x77,0x0e,0x44,0xd1,0xad,0x87,0x2b,
  23920. 0xf4,0x65,0x06,0xb7,0xbb,0xdb,0xbe,0x6e,
  23921. 0x02,0x43,0x24,0xc7,0x3d,0x7b,0x88,0x60
  23922. },
  23923. {
  23924. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  23925. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  23926. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  23927. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  23928. },
  23929. {
  23930. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  23931. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  23932. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  23933. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  23934. },
  23935. {
  23936. 0x63,0xbf,0x76,0xa9,0x73,0xa0,0x09,0xb9,
  23937. 0xcc,0xc9,0x4d,0x47,0x2d,0x14,0x0e,0x52,
  23938. 0xa3,0x84,0x55,0xb8,0x7c,0xdb,0xce,0xb1,
  23939. 0xe4,0x5b,0x8a,0xb9,0x30,0xf1,0xa4,0xa0
  23940. }
  23941. #endif
  23942. };
  23943. /* public key for party b */
  23944. byte pb[X25519_TEST_CNT][32] = {
  23945. {
  23946. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  23947. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  23948. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  23949. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  23950. },
  23951. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  23952. {
  23953. /* 0xff first byte in original - invalid! */
  23954. 0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  23955. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  23956. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  23957. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf0
  23958. },
  23959. {
  23960. 0x36,0x1a,0x74,0x87,0x28,0x59,0xe0,0xb6,
  23961. 0xe4,0x2b,0x17,0x9b,0x16,0xb0,0x3b,0xf8,
  23962. 0xb8,0x9f,0x2a,0x8f,0xc5,0x33,0x68,0x4f,
  23963. 0xde,0x4d,0xd8,0x80,0x63,0xe7,0xb4,0x0a
  23964. },
  23965. {
  23966. 0x00,0x80,0x38,0x59,0x19,0x3a,0x66,0x12,
  23967. 0xfd,0xa1,0xec,0x1c,0x40,0x84,0x40,0xbd,
  23968. 0x64,0x10,0x8b,0x53,0x81,0x21,0x03,0x2d,
  23969. 0x7d,0x33,0xb4,0x01,0x57,0x0d,0xe1,0x89
  23970. },
  23971. {
  23972. 0x1d,0xf8,0xf8,0x33,0x89,0x6c,0xb7,0xba,
  23973. 0x94,0x73,0xfa,0xc2,0x36,0xac,0xbe,0x49,
  23974. 0xaf,0x85,0x3e,0x93,0x5f,0xae,0xb2,0xc0,
  23975. 0xc8,0x80,0x8f,0x4a,0xaa,0xd3,0x55,0x2b
  23976. }
  23977. #endif
  23978. };
  23979. /* expected shared key */
  23980. byte ss[X25519_TEST_CNT][32] = {
  23981. {
  23982. 0x5c,0x4c,0x85,0x5f,0xfb,0x20,0x38,0xcc,
  23983. 0x55,0x16,0x5b,0x8a,0xa7,0xed,0x57,0x6e,
  23984. 0x35,0xaa,0x71,0x67,0x85,0x1f,0xb6,0x28,
  23985. 0x17,0x07,0x7b,0xda,0x76,0xdd,0xe0,0xb4
  23986. },
  23987. #ifdef CURVE25519_OVERFLOW_ALL_TESTS
  23988. {
  23989. 0x33,0xf6,0xc1,0x34,0x62,0x92,0x06,0x02,
  23990. 0x95,0xdb,0x91,0x4c,0x5d,0x52,0x54,0xc7,
  23991. 0xd2,0x5b,0x24,0xb5,0x4f,0x33,0x59,0x79,
  23992. 0x9f,0x6d,0x7e,0x4a,0x4c,0x30,0xd6,0x38
  23993. },
  23994. {
  23995. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23996. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23997. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  23998. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02
  23999. },
  24000. {
  24001. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24002. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24003. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24004. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09
  24005. },
  24006. {
  24007. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24008. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24009. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24010. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10
  24011. }
  24012. #endif
  24013. };
  24014. int ret = 0;
  24015. int i;
  24016. word32 y;
  24017. byte shared[32];
  24018. curve25519_key userA;
  24019. wc_curve25519_init_ex(&userA, HEAP_HINT, devId);
  24020. for (i = 0; i < X25519_TEST_CNT; i++) {
  24021. if (wc_curve25519_import_private_raw(sa[i], sizeof(sa[i]), pb[i],
  24022. sizeof(pb[i]), &userA) != 0) {
  24023. ret = -10500 - i; break;
  24024. }
  24025. /* test against known test vector */
  24026. XMEMSET(shared, 0, sizeof(shared));
  24027. y = sizeof(shared);
  24028. if (wc_curve25519_shared_secret(&userA, &userA, shared, &y) != 0) {
  24029. ret = -10510 - i; break;
  24030. }
  24031. if (XMEMCMP(ss[i], shared, y)) {
  24032. ret = -10520 - i; break;
  24033. }
  24034. }
  24035. wc_curve25519_free(&userA);
  24036. return ret;
  24037. }
  24038. /* Test the wc_curve25519_check_public API.
  24039. *
  24040. * returns 0 on success and -ve on failure.
  24041. */
  24042. static int curve25519_check_public_test(void)
  24043. {
  24044. /* Little-endian values that will fail */
  24045. byte fail_le[][CURVE25519_KEYSIZE] = {
  24046. {
  24047. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24048. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24049. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24050. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  24051. },
  24052. {
  24053. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24054. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24055. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24056. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  24057. },
  24058. {
  24059. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24060. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24061. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24062. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x81
  24063. },
  24064. };
  24065. /* Big-endian values that will fail */
  24066. byte fail_be[][CURVE25519_KEYSIZE] = {
  24067. {
  24068. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24069. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24070. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24071. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  24072. },
  24073. {
  24074. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24075. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24076. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24077. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  24078. },
  24079. {
  24080. 0x81,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24081. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24082. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24083. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  24084. },
  24085. };
  24086. /* Good or valid public value */
  24087. byte good[CURVE25519_KEYSIZE] = {
  24088. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24089. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24090. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  24091. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  24092. };
  24093. int i;
  24094. /* Parameter checks */
  24095. /* NULL pointer */
  24096. if (wc_curve25519_check_public(NULL, 0, EC25519_LITTLE_ENDIAN) !=
  24097. BAD_FUNC_ARG) {
  24098. return -10600;
  24099. }
  24100. if (wc_curve25519_check_public(NULL, 0, EC25519_BIG_ENDIAN) !=
  24101. BAD_FUNC_ARG) {
  24102. return -10601;
  24103. }
  24104. /* Length of 0 treated differently to other invalid lengths for TLS */
  24105. if (wc_curve25519_check_public(good, 0, EC25519_LITTLE_ENDIAN) != BUFFER_E)
  24106. return -10602;
  24107. if (wc_curve25519_check_public(good, 0, EC25519_BIG_ENDIAN) != BUFFER_E)
  24108. return -10603;
  24109. /* Length not CURVE25519_KEYSIZE */
  24110. for (i = 1; i < CURVE25519_KEYSIZE + 2; i++) {
  24111. if (i == CURVE25519_KEYSIZE)
  24112. continue;
  24113. if (wc_curve25519_check_public(good, i, EC25519_LITTLE_ENDIAN) !=
  24114. ECC_BAD_ARG_E) {
  24115. return -10604 - i;
  24116. }
  24117. if (wc_curve25519_check_public(good, i, EC25519_BIG_ENDIAN) !=
  24118. ECC_BAD_ARG_E) {
  24119. return -10614 - i;
  24120. }
  24121. }
  24122. /* Little-endian fail cases */
  24123. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  24124. if (wc_curve25519_check_public(fail_le[i], CURVE25519_KEYSIZE,
  24125. EC25519_LITTLE_ENDIAN) == 0) {
  24126. return -10624 - i;
  24127. }
  24128. }
  24129. /* Big-endian fail cases */
  24130. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  24131. if (wc_curve25519_check_public(fail_be[i], CURVE25519_KEYSIZE,
  24132. EC25519_BIG_ENDIAN) == 0) {
  24133. return -10634 - i;
  24134. }
  24135. }
  24136. /* Check a valid public value works! */
  24137. if (wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  24138. EC25519_LITTLE_ENDIAN) != 0) {
  24139. return -10644;
  24140. }
  24141. if (wc_curve25519_check_public(good, CURVE25519_KEYSIZE,
  24142. EC25519_BIG_ENDIAN) != 0) {
  24143. return -10645;
  24144. }
  24145. return 0;
  24146. }
  24147. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  24148. #if !defined(NO_ASN) && defined(HAVE_CURVE25519_KEY_EXPORT) && \
  24149. defined(HAVE_CURVE25519_KEY_IMPORT)
  24150. static int curve255519_der_test(void)
  24151. {
  24152. int ret = 0;
  24153. /* certs/statickeys/x25519.der */
  24154. const byte kCurve25519PrivDer[] = {
  24155. 0x30, 0x2E, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x6E,
  24156. 0x04, 0x22, 0x04, 0x20, 0x78, 0x8E, 0x31, 0x5C, 0x33, 0xA9, 0x19, 0xC0,
  24157. 0x5E, 0x36, 0x70, 0x1B, 0xA4, 0xE8, 0xEF, 0xC1, 0x89, 0x8C, 0xB3, 0x15,
  24158. 0xC6, 0x79, 0xD3, 0xAC, 0x22, 0x00, 0xAE, 0xFA, 0xB3, 0xB7, 0x0F, 0x78
  24159. };
  24160. /* certs/statickeys/x25519-pub.der */
  24161. const byte kCurve25519PubDer[] = {
  24162. 0x30, 0x2A, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x6E, 0x03, 0x21, 0x00,
  24163. 0x09, 0xBC, 0x8C, 0xC7, 0x45, 0x0D, 0xC1, 0xC2, 0x02, 0x57, 0x9A, 0x68,
  24164. 0x3A, 0xFD, 0x7A, 0xA8, 0xA5, 0x2F, 0xF0, 0x99, 0x39, 0x98, 0xEA, 0x26,
  24165. 0xA2, 0x5B, 0x38, 0xFD, 0x96, 0xDB, 0x2A, 0x26
  24166. };
  24167. curve25519_key key;
  24168. byte output[128];
  24169. word32 outputSz = 128;
  24170. word32 idx;
  24171. if (wc_curve25519_init_ex(&key, HEAP_HINT, devId) != 0) {
  24172. return -10723;
  24173. }
  24174. /* Test decode / encode of Curve25519 private key only */
  24175. if (ret == 0) {
  24176. idx = 0;
  24177. ret = wc_Curve25519PrivateKeyDecode(kCurve25519PrivDer, &idx, &key,
  24178. (word32)sizeof(kCurve25519PrivDer));
  24179. }
  24180. if (ret == 0) {
  24181. outputSz = (word32)sizeof(output);
  24182. ret = wc_Curve25519PrivateKeyToDer(&key, output, outputSz);
  24183. if (ret >= 0) {
  24184. outputSz = ret;
  24185. ret = 0;
  24186. }
  24187. else {
  24188. ret = -10724;
  24189. }
  24190. }
  24191. if (ret == 0 && (outputSz != (word32)sizeof(kCurve25519PrivDer) ||
  24192. XMEMCMP(output, kCurve25519PrivDer, outputSz) != 0)) {
  24193. ret = -10725;
  24194. }
  24195. /* Test decode / encode of Curve25519 public key only */
  24196. if (ret == 0) {
  24197. idx = 0;
  24198. ret = wc_Curve25519PublicKeyDecode(kCurve25519PubDer, &idx, &key,
  24199. (word32)sizeof(kCurve25519PubDer));
  24200. }
  24201. if (ret == 0) {
  24202. outputSz = (word32)sizeof(output);
  24203. ret = wc_Curve25519PublicKeyToDer(&key, output, outputSz, 1);
  24204. if (ret >= 0) {
  24205. outputSz = ret;
  24206. ret = 0;
  24207. }
  24208. else {
  24209. ret = -10726;
  24210. }
  24211. }
  24212. if (ret == 0 && (outputSz != (word32)sizeof(kCurve25519PubDer) ||
  24213. XMEMCMP(output, kCurve25519PubDer, outputSz) != 0)) {
  24214. ret = -10727;
  24215. }
  24216. wc_curve25519_free(&key);
  24217. return ret;
  24218. }
  24219. #endif /* !NO_ASN && HAVE_CURVE25519_KEY_EXPORT && HAVE_CURVE25519_KEY_IMPORT */
  24220. WOLFSSL_TEST_SUBROUTINE int curve25519_test(void)
  24221. {
  24222. WC_RNG rng;
  24223. int ret;
  24224. #ifdef HAVE_CURVE25519_SHARED_SECRET
  24225. byte sharedA[32];
  24226. byte sharedB[32];
  24227. word32 y;
  24228. #endif
  24229. #ifdef HAVE_CURVE25519_KEY_EXPORT
  24230. byte exportBuf[32];
  24231. #endif
  24232. word32 x = 0;
  24233. curve25519_key userA, userB, pubKey;
  24234. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  24235. defined(HAVE_CURVE25519_KEY_IMPORT)
  24236. /* test vectors from
  24237. https://tools.ietf.org/html/draft-josefsson-tls-curve25519-03
  24238. */
  24239. /* secret key for party a */
  24240. byte sa[] = {
  24241. 0x5A,0xC9,0x9F,0x33,0x63,0x2E,0x5A,0x76,
  24242. 0x8D,0xE7,0xE8,0x1B,0xF8,0x54,0xC2,0x7C,
  24243. 0x46,0xE3,0xFB,0xF2,0xAB,0xBA,0xCD,0x29,
  24244. 0xEC,0x4A,0xFF,0x51,0x73,0x69,0xC6,0x60
  24245. };
  24246. /* public key for party a */
  24247. byte pa[] = {
  24248. 0x05,0x7E,0x23,0xEA,0x9F,0x1C,0xBE,0x8A,
  24249. 0x27,0x16,0x8F,0x6E,0x69,0x6A,0x79,0x1D,
  24250. 0xE6,0x1D,0xD3,0xAF,0x7A,0xCD,0x4E,0xEA,
  24251. 0xCC,0x6E,0x7B,0xA5,0x14,0xFD,0xA8,0x63
  24252. };
  24253. /* secret key for party b */
  24254. byte sb[] = {
  24255. 0x47,0xDC,0x3D,0x21,0x41,0x74,0x82,0x0E,
  24256. 0x11,0x54,0xB4,0x9B,0xC6,0xCD,0xB2,0xAB,
  24257. 0xD4,0x5E,0xE9,0x58,0x17,0x05,0x5D,0x25,
  24258. 0x5A,0xA3,0x58,0x31,0xB7,0x0D,0x32,0x60
  24259. };
  24260. /* public key for party b */
  24261. byte pb[] = {
  24262. 0x6E,0xB8,0x9D,0xA9,0x19,0x89,0xAE,0x37,
  24263. 0xC7,0xEA,0xC7,0x61,0x8D,0x9E,0x5C,0x49,
  24264. 0x51,0xDB,0xA1,0xD7,0x3C,0x28,0x5A,0xE1,
  24265. 0xCD,0x26,0xA8,0x55,0x02,0x0E,0xEF,0x04
  24266. };
  24267. /* expected shared key */
  24268. byte ss[] = {
  24269. 0x61,0x45,0x0C,0xD9,0x8E,0x36,0x01,0x6B,
  24270. 0x58,0x77,0x6A,0x89,0x7A,0x9F,0x0A,0xEF,
  24271. 0x73,0x8B,0x99,0xF0,0x94,0x68,0xB8,0xD6,
  24272. 0xB8,0x51,0x11,0x84,0xD5,0x34,0x94,0xAB
  24273. };
  24274. #endif /* HAVE_CURVE25519_SHARED_SECRET */
  24275. (void)x;
  24276. #ifndef HAVE_FIPS
  24277. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  24278. #else
  24279. ret = wc_InitRng(&rng);
  24280. #endif
  24281. if (ret != 0)
  24282. return -10700;
  24283. wc_curve25519_init_ex(&userA, HEAP_HINT, devId);
  24284. wc_curve25519_init_ex(&userB, HEAP_HINT, devId);
  24285. wc_curve25519_init_ex(&pubKey, HEAP_HINT, devId);
  24286. /* make curve25519 keys */
  24287. if (wc_curve25519_make_key(&rng, 32, &userA) != 0)
  24288. return -10701;
  24289. if (wc_curve25519_make_key(&rng, 32, &userB) != 0)
  24290. return -10702;
  24291. #ifdef HAVE_CURVE25519_SHARED_SECRET
  24292. /* find shared secret key */
  24293. x = sizeof(sharedA);
  24294. if ((ret = wc_curve25519_shared_secret(&userA, &userB, sharedA, &x)) != 0) {
  24295. printf("wc_curve25519_shared_secret 1 %d\n", ret);
  24296. return -10703;
  24297. }
  24298. y = sizeof(sharedB);
  24299. if ((ret = wc_curve25519_shared_secret(&userB, &userA, sharedB, &y)) != 0) {
  24300. printf("wc_curve25519_shared_secret 2 %d\n", ret);
  24301. return -10704;
  24302. }
  24303. /* compare shared secret keys to test they are the same */
  24304. if (y != x)
  24305. return -10705;
  24306. if (XMEMCMP(sharedA, sharedB, x))
  24307. return -10706;
  24308. #endif
  24309. #ifdef HAVE_CURVE25519_KEY_EXPORT
  24310. /* export a public key and import it for another user */
  24311. x = sizeof(exportBuf);
  24312. if (wc_curve25519_export_public(&userA, exportBuf, &x) != 0)
  24313. return -10707;
  24314. #ifdef HAVE_CURVE25519_KEY_IMPORT
  24315. if (wc_curve25519_import_public(exportBuf, x, &pubKey) != 0)
  24316. return -10708;
  24317. #endif
  24318. #endif
  24319. #if defined(HAVE_CURVE25519_SHARED_SECRET) && \
  24320. defined(HAVE_CURVE25519_KEY_IMPORT)
  24321. /* test shared key after importing a public key */
  24322. XMEMSET(sharedB, 0, sizeof(sharedB));
  24323. y = sizeof(sharedB);
  24324. if (wc_curve25519_shared_secret(&userB, &pubKey, sharedB, &y) != 0)
  24325. return -10709;
  24326. if (XMEMCMP(sharedA, sharedB, y))
  24327. return -10710;
  24328. /* import RFC test vectors and compare shared key */
  24329. if (wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  24330. != 0)
  24331. return -10711;
  24332. if (wc_curve25519_import_private_raw(sb, sizeof(sb), pb, sizeof(pb), &userB)
  24333. != 0)
  24334. return -10712;
  24335. /* test against known test vector */
  24336. XMEMSET(sharedB, 0, sizeof(sharedB));
  24337. y = sizeof(sharedB);
  24338. if (wc_curve25519_shared_secret(&userA, &userB, sharedB, &y) != 0)
  24339. return -10713;
  24340. if (XMEMCMP(ss, sharedB, y))
  24341. return -10714;
  24342. /* test swapping roles of keys and generating same shared key */
  24343. XMEMSET(sharedB, 0, sizeof(sharedB));
  24344. y = sizeof(sharedB);
  24345. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  24346. return -10715;
  24347. if (XMEMCMP(ss, sharedB, y))
  24348. return -10716;
  24349. /* test with 1 generated key and 1 from known test vector */
  24350. if (wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  24351. != 0)
  24352. return -10717;
  24353. wc_curve25519_free(&userB);
  24354. wc_curve25519_init_ex(&userB, HEAP_HINT, devId);
  24355. if (wc_curve25519_make_key(&rng, 32, &userB) != 0)
  24356. return -10718;
  24357. x = sizeof(sharedA);
  24358. if (wc_curve25519_shared_secret(&userA, &userB, sharedA, &x) != 0)
  24359. return -10719;
  24360. y = sizeof(sharedB);
  24361. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  24362. return -10720;
  24363. /* compare shared secret keys to test they are the same */
  24364. if (y != x)
  24365. return -10721;
  24366. if (XMEMCMP(sharedA, sharedB, x))
  24367. return -10722;
  24368. ret = curve25519_overflow_test();
  24369. if (ret != 0)
  24370. return ret;
  24371. ret = curve25519_check_public_test();
  24372. if (ret != 0)
  24373. return ret;
  24374. #endif /* HAVE_CURVE25519_SHARED_SECRET && HAVE_CURVE25519_KEY_IMPORT */
  24375. #if !defined(NO_ASN) && defined(HAVE_CURVE25519_KEY_EXPORT) && \
  24376. defined(HAVE_CURVE25519_KEY_IMPORT)
  24377. ret = curve255519_der_test();
  24378. if (ret != 0)
  24379. return ret;
  24380. #endif
  24381. /* clean up keys when done */
  24382. wc_curve25519_free(&pubKey);
  24383. wc_curve25519_free(&userB);
  24384. wc_curve25519_free(&userA);
  24385. wc_FreeRng(&rng);
  24386. return 0;
  24387. }
  24388. #endif /* HAVE_CURVE25519 */
  24389. #ifdef HAVE_ED25519
  24390. #ifdef WOLFSSL_TEST_CERT
  24391. static int ed25519_test_cert(void)
  24392. {
  24393. DecodedCert cert[2];
  24394. DecodedCert* serverCert = NULL;
  24395. DecodedCert* caCert = NULL;
  24396. #ifdef HAVE_ED25519_VERIFY
  24397. ed25519_key key;
  24398. ed25519_key* pubKey = NULL;
  24399. int verify;
  24400. #endif /* HAVE_ED25519_VERIFY */
  24401. int ret;
  24402. byte* tmp;
  24403. size_t bytes;
  24404. XFILE file;
  24405. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24406. if (tmp == NULL) {
  24407. ERROR_OUT(-10730, done);
  24408. }
  24409. #ifdef USE_CERT_BUFFERS_256
  24410. XMEMCPY(tmp, ca_ed25519_cert, sizeof_ca_ed25519_cert);
  24411. bytes = sizeof_ca_ed25519_cert;
  24412. #elif !defined(NO_FILESYSTEM)
  24413. file = XFOPEN(caEd25519Cert, "rb");
  24414. if (file == NULL) {
  24415. ERROR_OUT(-10731, done);
  24416. }
  24417. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  24418. XFCLOSE(file);
  24419. #else
  24420. /* No certificate to use. */
  24421. ERROR_OUT(-10732, done);
  24422. #endif
  24423. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  24424. caCert = &cert[0];
  24425. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  24426. if (ret != 0) {
  24427. ERROR_OUT(-10733, done);
  24428. }
  24429. #ifdef USE_CERT_BUFFERS_256
  24430. XMEMCPY(tmp, server_ed25519_cert, sizeof_server_ed25519_cert);
  24431. bytes = sizeof_server_ed25519_cert;
  24432. #elif !defined(NO_FILESYSTEM)
  24433. file = XFOPEN(serverEd25519Cert, "rb");
  24434. if (file == NULL) {
  24435. ERROR_OUT(-10734, done);
  24436. }
  24437. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  24438. XFCLOSE(file);
  24439. #else
  24440. /* No certificate to use. */
  24441. ERROR_OUT(-10735, done);
  24442. #endif
  24443. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  24444. serverCert = &cert[1];
  24445. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  24446. if (ret != 0) {
  24447. ERROR_OUT(-10736, done);
  24448. }
  24449. #ifdef HAVE_ED25519_VERIFY
  24450. ret = wc_ed25519_init(&key);
  24451. if (ret < 0) {
  24452. ERROR_OUT(-10737, done);
  24453. }
  24454. pubKey = &key;
  24455. ret = wc_ed25519_import_public(caCert->publicKey, caCert->pubKeySize,
  24456. pubKey);
  24457. if (ret < 0) {
  24458. ERROR_OUT(-10738, done);
  24459. }
  24460. if (wc_ed25519_verify_msg(serverCert->signature, serverCert->sigLength,
  24461. serverCert->source + serverCert->certBegin,
  24462. serverCert->sigIndex - serverCert->certBegin,
  24463. &verify, pubKey) < 0 || verify != 1) {
  24464. ERROR_OUT(-10739, done);
  24465. }
  24466. #endif /* HAVE_ED25519_VERIFY */
  24467. done:
  24468. if (tmp != NULL)
  24469. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24470. #ifdef HAVE_ED25519_VERIFY
  24471. wc_ed25519_free(pubKey);
  24472. #endif /* HAVE_ED25519_VERIFY */
  24473. if (caCert != NULL)
  24474. FreeDecodedCert(caCert);
  24475. if (serverCert != NULL)
  24476. FreeDecodedCert(serverCert);
  24477. return ret;
  24478. }
  24479. static int ed25519_test_make_cert(void)
  24480. {
  24481. WC_RNG rng;
  24482. Cert cert;
  24483. DecodedCert decode;
  24484. ed25519_key key;
  24485. ed25519_key* privKey = NULL;
  24486. int ret = 0;
  24487. byte* tmp = NULL;
  24488. wc_InitCert_ex(&cert, HEAP_HINT, devId);
  24489. #ifndef HAVE_FIPS
  24490. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  24491. #else
  24492. ret = wc_InitRng(&rng);
  24493. #endif
  24494. if (ret != 0)
  24495. return -10750;
  24496. wc_ed25519_init(&key);
  24497. privKey = &key;
  24498. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, privKey);
  24499. cert.daysValid = 365 * 2;
  24500. cert.selfSigned = 1;
  24501. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  24502. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  24503. cert.isCA = 0;
  24504. #ifdef WOLFSSL_CERT_EXT
  24505. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  24506. if (ret < 0) {
  24507. ERROR_OUT(-10751, done);
  24508. }
  24509. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  24510. if (ret < 0) {
  24511. ERROR_OUT(-10752, done);
  24512. }
  24513. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  24514. if (ret < 0) {
  24515. ERROR_OUT(-10753, done);
  24516. }
  24517. #endif
  24518. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24519. if (tmp == NULL) {
  24520. ERROR_OUT(-10754, done);
  24521. }
  24522. cert.sigType = CTC_ED25519;
  24523. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED25519_TYPE, privKey, &rng);
  24524. if (ret < 0) {
  24525. ERROR_OUT(-10755, done);
  24526. }
  24527. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF,
  24528. ED25519_TYPE, privKey, &rng);
  24529. if (ret < 0) {
  24530. ERROR_OUT(-10756, done);
  24531. }
  24532. InitDecodedCert(&decode, tmp, ret, HEAP_HINT);
  24533. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  24534. FreeDecodedCert(&decode);
  24535. if (ret != 0) {
  24536. ERROR_OUT(-10757, done);
  24537. }
  24538. done:
  24539. if (tmp != NULL)
  24540. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24541. wc_ed25519_free(privKey);
  24542. wc_FreeRng(&rng);
  24543. return ret;
  24544. }
  24545. #endif /* WOLFSSL_TEST_CERT */
  24546. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  24547. defined(HAVE_ED25519_KEY_IMPORT)
  24548. static int ed25519ctx_test(void)
  24549. {
  24550. int ret;
  24551. byte out[ED25519_SIG_SIZE];
  24552. word32 outlen;
  24553. #ifdef HAVE_ED25519_VERIFY
  24554. int verify = 0;
  24555. #endif /* HAVE_ED25519_VERIFY */
  24556. ed25519_key key;
  24557. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  24558. 0x03,0x05,0x33,0x4e,0x38,0x1a,0xf7,0x8f,
  24559. 0x14,0x1c,0xb6,0x66,0xf6,0x19,0x9f,0x57,
  24560. 0xbc,0x34,0x95,0x33,0x5a,0x25,0x6a,0x95,
  24561. 0xbd,0x2a,0x55,0xbf,0x54,0x66,0x63,0xf6
  24562. };
  24563. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  24564. 0xdf,0xc9,0x42,0x5e,0x4f,0x96,0x8f,0x7f,
  24565. 0x0c,0x29,0xf0,0x25,0x9c,0xf5,0xf9,0xae,
  24566. 0xd6,0x85,0x1c,0x2b,0xb4,0xad,0x8b,0xfb,
  24567. 0x86,0x0c,0xfe,0xe0,0xab,0x24,0x82,0x92
  24568. };
  24569. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx1[] = {
  24570. 0x55,0xa4,0xcc,0x2f,0x70,0xa5,0x4e,0x04,
  24571. 0x28,0x8c,0x5f,0x4c,0xd1,0xe4,0x5a,0x7b,
  24572. 0xb5,0x20,0xb3,0x62,0x92,0x91,0x18,0x76,
  24573. 0xca,0xda,0x73,0x23,0x19,0x8d,0xd8,0x7a,
  24574. 0x8b,0x36,0x95,0x0b,0x95,0x13,0x00,0x22,
  24575. 0x90,0x7a,0x7f,0xb7,0xc4,0xe9,0xb2,0xd5,
  24576. 0xf6,0xcc,0xa6,0x85,0xa5,0x87,0xb4,0xb2,
  24577. 0x1f,0x4b,0x88,0x8e,0x4e,0x7e,0xdb,0x0d
  24578. };
  24579. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx2[] = {
  24580. 0xcc,0x5e,0x63,0xa2,0x7e,0x94,0xaf,0xd3,
  24581. 0x41,0x83,0x38,0xd2,0x48,0x6f,0xa9,0x2a,
  24582. 0xf9,0x91,0x7c,0x2d,0x98,0x9e,0x06,0xe5,
  24583. 0x02,0x77,0x72,0x1c,0x34,0x38,0x18,0xb4,
  24584. 0x21,0x96,0xbc,0x29,0x2e,0x68,0xf3,0x4d,
  24585. 0x85,0x9b,0xbe,0xad,0x17,0x9f,0x54,0x54,
  24586. 0x2d,0x4b,0x04,0xdc,0xfb,0xfa,0x4a,0x68,
  24587. 0x4e,0x39,0x50,0xfb,0x1c,0xcd,0x8d,0x0d
  24588. };
  24589. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  24590. 0xf7,0x26,0x93,0x6d,0x19,0xc8,0x00,0x49,
  24591. 0x4e,0x3f,0xda,0xff,0x20,0xb2,0x76,0xa8
  24592. };
  24593. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  24594. 0x66,0x6f,0x6f
  24595. };
  24596. outlen = sizeof(out);
  24597. XMEMSET(out, 0, sizeof(out));
  24598. ret = wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  24599. if (ret != 0)
  24600. return 10800;
  24601. ret = wc_ed25519_import_private_key(sKeyCtx, ED25519_KEY_SIZE, pKeyCtx,
  24602. sizeof(pKeyCtx), &key);
  24603. if (ret == 0)
  24604. ret = wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  24605. contextCtx, sizeof(contextCtx));
  24606. if (ret == 0 && XMEMCMP(out, sigCtx1, 64) != 0)
  24607. ret = -10801;
  24608. #if defined(HAVE_ED25519_VERIFY)
  24609. /* test verify on good msg */
  24610. if (ret == 0)
  24611. ret = wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx),
  24612. &verify, &key, contextCtx, sizeof(contextCtx));
  24613. if (ret == 0 && verify != 1)
  24614. ret = -10802;
  24615. #endif
  24616. if (ret == 0)
  24617. ret = wc_ed25519ctx_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  24618. NULL, 0);
  24619. if (ret == 0 && XMEMCMP(out, sigCtx2, 64) != 0)
  24620. ret = -10803;
  24621. #if defined(HAVE_ED25519_VERIFY)
  24622. /* test verify on good msg */
  24623. if (ret == 0)
  24624. ret = wc_ed25519ctx_verify_msg(out, outlen, msgCtx, sizeof(msgCtx),
  24625. &verify, &key, NULL, 0);
  24626. if (ret == 0 && verify != 1)
  24627. ret = -10804;
  24628. #endif
  24629. wc_ed25519_free(&key);
  24630. return ret;
  24631. }
  24632. static int ed25519ph_test(void)
  24633. {
  24634. int ret = 0;
  24635. byte out[ED25519_SIG_SIZE];
  24636. word32 outlen;
  24637. #ifdef HAVE_ED25519_VERIFY
  24638. int verify = 0;
  24639. #endif /* HAVE_ED25519_VERIFY */
  24640. ed25519_key key;
  24641. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  24642. 0x83,0x3f,0xe6,0x24,0x09,0x23,0x7b,0x9d,
  24643. 0x62,0xec,0x77,0x58,0x75,0x20,0x91,0x1e,
  24644. 0x9a,0x75,0x9c,0xec,0x1d,0x19,0x75,0x5b,
  24645. 0x7d,0xa9,0x01,0xb9,0x6d,0xca,0x3d,0x42
  24646. };
  24647. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  24648. 0xec,0x17,0x2b,0x93,0xad,0x5e,0x56,0x3b,
  24649. 0xf4,0x93,0x2c,0x70,0xe1,0x24,0x50,0x34,
  24650. 0xc3,0x54,0x67,0xef,0x2e,0xfd,0x4d,0x64,
  24651. 0xeb,0xf8,0x19,0x68,0x34,0x67,0xe2,0xbf
  24652. };
  24653. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  24654. 0x98,0xa7,0x02,0x22,0xf0,0xb8,0x12,0x1a,
  24655. 0xa9,0xd3,0x0f,0x81,0x3d,0x68,0x3f,0x80,
  24656. 0x9e,0x46,0x2b,0x46,0x9c,0x7f,0xf8,0x76,
  24657. 0x39,0x49,0x9b,0xb9,0x4e,0x6d,0xae,0x41,
  24658. 0x31,0xf8,0x50,0x42,0x46,0x3c,0x2a,0x35,
  24659. 0x5a,0x20,0x03,0xd0,0x62,0xad,0xf5,0xaa,
  24660. 0xa1,0x0b,0x8c,0x61,0xe6,0x36,0x06,0x2a,
  24661. 0xaa,0xd1,0x1c,0x2a,0x26,0x08,0x34,0x06
  24662. };
  24663. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  24664. 0xe0,0x39,0x70,0x2b,0x4c,0x25,0x95,0xa6,
  24665. 0xa5,0x41,0xac,0x85,0x09,0x23,0x6e,0x29,
  24666. 0x90,0x47,0x47,0x95,0x33,0x0c,0x9b,0x34,
  24667. 0xa7,0x5f,0x58,0xa6,0x60,0x12,0x9e,0x08,
  24668. 0xfd,0x73,0x69,0x43,0xfb,0x19,0x43,0xa5,
  24669. 0x57,0x20,0xb9,0xe0,0x95,0x7b,0x1e,0xd6,
  24670. 0x73,0x48,0x16,0x61,0x9f,0x13,0x88,0xf4,
  24671. 0x3f,0x73,0xe6,0xe3,0xba,0xa8,0x1c,0x0e
  24672. };
  24673. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  24674. 0x61,0x62,0x63
  24675. };
  24676. /* SHA-512 hash of msgPh */
  24677. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  24678. 0xdd,0xaf,0x35,0xa1,0x93,0x61,0x7a,0xba,
  24679. 0xcc,0x41,0x73,0x49,0xae,0x20,0x41,0x31,
  24680. 0x12,0xe6,0xfa,0x4e,0x89,0xa9,0x7e,0xa2,
  24681. 0x0a,0x9e,0xee,0xe6,0x4b,0x55,0xd3,0x9a,
  24682. 0x21,0x92,0x99,0x2a,0x27,0x4f,0xc1,0xa8,
  24683. 0x36,0xba,0x3c,0x23,0xa3,0xfe,0xeb,0xbd,
  24684. 0x45,0x4d,0x44,0x23,0x64,0x3c,0xe8,0x0e,
  24685. 0x2a,0x9a,0xc9,0x4f,0xa5,0x4c,0xa4,0x9f
  24686. };
  24687. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  24688. 0x66,0x6f,0x6f
  24689. };
  24690. outlen = sizeof(out);
  24691. XMEMSET(out, 0, sizeof(out));
  24692. ret = wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  24693. if (ret != 0)
  24694. return -10900;
  24695. ret = wc_ed25519_import_private_key(sKeyPh, ED25519_KEY_SIZE, pKeyPh,
  24696. sizeof(pKeyPh), &key);
  24697. if (ret == 0)
  24698. ret = wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  24699. NULL, 0);
  24700. if (ret == 0 && XMEMCMP(out, sigPh1, 64) != 0)
  24701. ret = -10901;
  24702. #if defined(HAVE_ED25519_VERIFY)
  24703. /* test verify on good msg */
  24704. if (ret == 0)
  24705. ret = wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh),
  24706. &verify, &key, NULL, 0);
  24707. if (ret == 0 && verify != 1)
  24708. ret = -10902;
  24709. #endif
  24710. if (ret == 0)
  24711. ret = wc_ed25519ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  24712. contextPh2, sizeof(contextPh2));
  24713. if (ret == 0 && XMEMCMP(out, sigPh2, 64) != 0)
  24714. ret = -10903;
  24715. #if defined(HAVE_ED25519_VERIFY)
  24716. /* test verify on good msg */
  24717. if (ret == 0)
  24718. ret = wc_ed25519ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify,
  24719. &key, contextPh2, sizeof(contextPh2));
  24720. if (ret == 0 && verify != 1)
  24721. ret = -10904;
  24722. #endif
  24723. if (ret == 0)
  24724. ret = wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  24725. NULL, 0);
  24726. if (ret == 0 && XMEMCMP(out, sigPh1, 64) != 0)
  24727. ret = -10905;
  24728. #if defined(HAVE_ED25519_VERIFY)
  24729. if (ret == 0)
  24730. ret = wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh),
  24731. &verify, &key, NULL, 0);
  24732. if (ret == 0 && verify != 1)
  24733. ret = -10906;
  24734. #endif
  24735. if (ret == 0)
  24736. ret = wc_ed25519ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  24737. contextPh2, sizeof(contextPh2));
  24738. if (ret == 0 && XMEMCMP(out, sigPh2, 64) != 0)
  24739. ret = -10907;
  24740. #if defined(HAVE_ED25519_VERIFY)
  24741. if (ret == 0)
  24742. ret = wc_ed25519ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  24743. &key, contextPh2, sizeof(contextPh2));
  24744. if (ret == 0 && verify != 1)
  24745. ret = -10908;
  24746. #endif
  24747. wc_ed25519_free(&key);
  24748. return ret;
  24749. }
  24750. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  24751. WOLFSSL_TEST_SUBROUTINE int ed25519_test(void)
  24752. {
  24753. int ret;
  24754. WC_RNG rng;
  24755. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  24756. defined(HAVE_ED25519_KEY_IMPORT)
  24757. byte out[ED25519_SIG_SIZE];
  24758. byte exportPKey[ED25519_KEY_SIZE];
  24759. byte exportSKey[ED25519_KEY_SIZE];
  24760. word32 exportPSz;
  24761. word32 exportSSz;
  24762. int i;
  24763. word32 outlen;
  24764. #ifdef HAVE_ED25519_VERIFY
  24765. #ifdef WOLFSSL_ED25519_STREAMING_VERIFY
  24766. int j;
  24767. #endif
  24768. int verify;
  24769. #endif /* HAVE_ED25519_VERIFY */
  24770. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  24771. word32 keySz, sigSz;
  24772. ed25519_key key;
  24773. ed25519_key key2;
  24774. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  24775. defined(HAVE_ED25519_KEY_IMPORT)
  24776. /* test vectors from
  24777. https://tools.ietf.org/html/draft-josefsson-eddsa-ed25519-02
  24778. */
  24779. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  24780. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  24781. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  24782. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  24783. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  24784. };
  24785. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  24786. 0x4c,0xcd,0x08,0x9b,0x28,0xff,0x96,0xda,
  24787. 0x9d,0xb6,0xc3,0x46,0xec,0x11,0x4e,0x0f,
  24788. 0x5b,0x8a,0x31,0x9f,0x35,0xab,0xa6,0x24,
  24789. 0xda,0x8c,0xf6,0xed,0x4f,0xb8,0xa6,0xfb
  24790. };
  24791. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  24792. 0xc5,0xaa,0x8d,0xf4,0x3f,0x9f,0x83,0x7b,
  24793. 0xed,0xb7,0x44,0x2f,0x31,0xdc,0xb7,0xb1,
  24794. 0x66,0xd3,0x85,0x35,0x07,0x6f,0x09,0x4b,
  24795. 0x85,0xce,0x3a,0x2e,0x0b,0x44,0x58,0xf7
  24796. };
  24797. /* uncompressed test */
  24798. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  24799. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  24800. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  24801. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  24802. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  24803. };
  24804. /* compressed prefix test */
  24805. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  24806. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  24807. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  24808. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  24809. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  24810. };
  24811. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  24812. 0xf5,0xe5,0x76,0x7c,0xf1,0x53,0x31,0x95,
  24813. 0x17,0x63,0x0f,0x22,0x68,0x76,0xb8,0x6c,
  24814. 0x81,0x60,0xcc,0x58,0x3b,0xc0,0x13,0x74,
  24815. 0x4c,0x6b,0xf2,0x55,0xf5,0xcc,0x0e,0xe5
  24816. };
  24817. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  24818. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  24819. 0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  24820. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  24821. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  24822. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  24823. };
  24824. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  24825. 0x3d,0x40,0x17,0xc3,0xe8,0x43,0x89,0x5a,
  24826. 0x92,0xb7,0x0a,0xa7,0x4d,0x1b,0x7e,0xbc,
  24827. 0x9c,0x98,0x2c,0xcf,0x2e,0xc4,0x96,0x8c,
  24828. 0xc0,0xcd,0x55,0xf1,0x2a,0xf4,0x66,0x0c
  24829. };
  24830. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  24831. 0xfc,0x51,0xcd,0x8e,0x62,0x18,0xa1,0xa3,
  24832. 0x8d,0xa4,0x7e,0xd0,0x02,0x30,0xf0,0x58,
  24833. 0x08,0x16,0xed,0x13,0xba,0x33,0x03,0xac,
  24834. 0x5d,0xeb,0x91,0x15,0x48,0x90,0x80,0x25
  24835. };
  24836. /* uncompressed test */
  24837. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  24838. 0x04,0x55,0xd0,0xe0,0x9a,0x2b,0x9d,0x34,
  24839. 0x29,0x22,0x97,0xe0,0x8d,0x60,0xd0,0xf6,
  24840. 0x20,0xc5,0x13,0xd4,0x72,0x53,0x18,0x7c,
  24841. 0x24,0xb1,0x27,0x86,0xbd,0x77,0x76,0x45,
  24842. 0xce,0x1a,0x51,0x07,0xf7,0x68,0x1a,0x02,
  24843. 0xaf,0x25,0x23,0xa6,0xda,0xf3,0x72,0xe1,
  24844. 0x0e,0x3a,0x07,0x64,0xc9,0xd3,0xfe,0x4b,
  24845. 0xd5,0xb7,0x0a,0xb1,0x82,0x01,0x98,0x5a,
  24846. 0xd7
  24847. };
  24848. /* compressed prefix */
  24849. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  24850. 0x40,0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  24851. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  24852. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  24853. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  24854. };
  24855. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  24856. 0x27,0x81,0x17,0xfc,0x14,0x4c,0x72,0x34,
  24857. 0x0f,0x67,0xd0,0xf2,0x31,0x6e,0x83,0x86,
  24858. 0xce,0xff,0xbf,0x2b,0x24,0x28,0xc9,0xc5,
  24859. 0x1f,0xef,0x7c,0x59,0x7f,0x1d,0x42,0x6e
  24860. };
  24861. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  24862. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  24863. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  24864. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  24865. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  24866. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  24867. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  24868. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  24869. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  24870. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  24871. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  24872. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  24873. };
  24874. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  24875. 0x92,0xa0,0x09,0xa9,0xf0,0xd4,0xca,0xb8,
  24876. 0x72,0x0e,0x82,0x0b,0x5f,0x64,0x25,0x40,
  24877. 0xa2,0xb2,0x7b,0x54,0x16,0x50,0x3f,0x8f,
  24878. 0xb3,0x76,0x22,0x23,0xeb,0xdb,0x69,0xda,
  24879. 0x08,0x5a,0xc1,0xe4,0x3e,0x15,0x99,0x6e,
  24880. 0x45,0x8f,0x36,0x13,0xd0,0xf1,0x1d,0x8c,
  24881. 0x38,0x7b,0x2e,0xae,0xb4,0x30,0x2a,0xee,
  24882. 0xb0,0x0d,0x29,0x16,0x12,0xbb,0x0c,0x00
  24883. };
  24884. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  24885. 0x62,0x91,0xd6,0x57,0xde,0xec,0x24,0x02,
  24886. 0x48,0x27,0xe6,0x9c,0x3a,0xbe,0x01,0xa3,
  24887. 0x0c,0xe5,0x48,0xa2,0x84,0x74,0x3a,0x44,
  24888. 0x5e,0x36,0x80,0xd7,0xdb,0x5a,0xc3,0xac,
  24889. 0x18,0xff,0x9b,0x53,0x8d,0x16,0xf2,0x90,
  24890. 0xae,0x67,0xf7,0x60,0x98,0x4d,0xc6,0x59,
  24891. 0x4a,0x7c,0x15,0xe9,0x71,0x6e,0xd2,0x8d,
  24892. 0xc0,0x27,0xbe,0xce,0xea,0x1e,0xc4,0x0a
  24893. };
  24894. /* uncompressed test */
  24895. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  24896. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  24897. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  24898. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  24899. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  24900. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  24901. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  24902. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  24903. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  24904. };
  24905. /* compressed prefix */
  24906. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  24907. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  24908. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  24909. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  24910. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  24911. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  24912. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  24913. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  24914. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  24915. };
  24916. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  24917. 0x0a,0xab,0x4c,0x90,0x05,0x01,0xb3,0xe2,
  24918. 0x4d,0x7c,0xdf,0x46,0x63,0x32,0x6a,0x3a,
  24919. 0x87,0xdf,0x5e,0x48,0x43,0xb2,0xcb,0xdb,
  24920. 0x67,0xcb,0xf6,0xe4,0x60,0xfe,0xc3,0x50,
  24921. 0xaa,0x53,0x71,0xb1,0x50,0x8f,0x9f,0x45,
  24922. 0x28,0xec,0xea,0x23,0xc4,0x36,0xd9,0x4b,
  24923. 0x5e,0x8f,0xcd,0x4f,0x68,0x1e,0x30,0xa6,
  24924. 0xac,0x00,0xa9,0x70,0x4a,0x18,0x8a,0x03
  24925. };
  24926. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  24927. WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = {0x0 };
  24928. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = {0x72};
  24929. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = {0xAF,0x82};
  24930. /* test of a 1024 byte long message */
  24931. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  24932. 0x08,0xb8,0xb2,0xb7,0x33,0x42,0x42,0x43,
  24933. 0x76,0x0f,0xe4,0x26,0xa4,0xb5,0x49,0x08,
  24934. 0x63,0x21,0x10,0xa6,0x6c,0x2f,0x65,0x91,
  24935. 0xea,0xbd,0x33,0x45,0xe3,0xe4,0xeb,0x98,
  24936. 0xfa,0x6e,0x26,0x4b,0xf0,0x9e,0xfe,0x12,
  24937. 0xee,0x50,0xf8,0xf5,0x4e,0x9f,0x77,0xb1,
  24938. 0xe3,0x55,0xf6,0xc5,0x05,0x44,0xe2,0x3f,
  24939. 0xb1,0x43,0x3d,0xdf,0x73,0xbe,0x84,0xd8,
  24940. 0x79,0xde,0x7c,0x00,0x46,0xdc,0x49,0x96,
  24941. 0xd9,0xe7,0x73,0xf4,0xbc,0x9e,0xfe,0x57,
  24942. 0x38,0x82,0x9a,0xdb,0x26,0xc8,0x1b,0x37,
  24943. 0xc9,0x3a,0x1b,0x27,0x0b,0x20,0x32,0x9d,
  24944. 0x65,0x86,0x75,0xfc,0x6e,0xa5,0x34,0xe0,
  24945. 0x81,0x0a,0x44,0x32,0x82,0x6b,0xf5,0x8c,
  24946. 0x94,0x1e,0xfb,0x65,0xd5,0x7a,0x33,0x8b,
  24947. 0xbd,0x2e,0x26,0x64,0x0f,0x89,0xff,0xbc,
  24948. 0x1a,0x85,0x8e,0xfc,0xb8,0x55,0x0e,0xe3,
  24949. 0xa5,0xe1,0x99,0x8b,0xd1,0x77,0xe9,0x3a,
  24950. 0x73,0x63,0xc3,0x44,0xfe,0x6b,0x19,0x9e,
  24951. 0xe5,0xd0,0x2e,0x82,0xd5,0x22,0xc4,0xfe,
  24952. 0xba,0x15,0x45,0x2f,0x80,0x28,0x8a,0x82,
  24953. 0x1a,0x57,0x91,0x16,0xec,0x6d,0xad,0x2b,
  24954. 0x3b,0x31,0x0d,0xa9,0x03,0x40,0x1a,0xa6,
  24955. 0x21,0x00,0xab,0x5d,0x1a,0x36,0x55,0x3e,
  24956. 0x06,0x20,0x3b,0x33,0x89,0x0c,0xc9,0xb8,
  24957. 0x32,0xf7,0x9e,0xf8,0x05,0x60,0xcc,0xb9,
  24958. 0xa3,0x9c,0xe7,0x67,0x96,0x7e,0xd6,0x28,
  24959. 0xc6,0xad,0x57,0x3c,0xb1,0x16,0xdb,0xef,
  24960. 0xef,0xd7,0x54,0x99,0xda,0x96,0xbd,0x68,
  24961. 0xa8,0xa9,0x7b,0x92,0x8a,0x8b,0xbc,0x10,
  24962. 0x3b,0x66,0x21,0xfc,0xde,0x2b,0xec,0xa1,
  24963. 0x23,0x1d,0x20,0x6b,0xe6,0xcd,0x9e,0xc7,
  24964. 0xaf,0xf6,0xf6,0xc9,0x4f,0xcd,0x72,0x04,
  24965. 0xed,0x34,0x55,0xc6,0x8c,0x83,0xf4,0xa4,
  24966. 0x1d,0xa4,0xaf,0x2b,0x74,0xef,0x5c,0x53,
  24967. 0xf1,0xd8,0xac,0x70,0xbd,0xcb,0x7e,0xd1,
  24968. 0x85,0xce,0x81,0xbd,0x84,0x35,0x9d,0x44,
  24969. 0x25,0x4d,0x95,0x62,0x9e,0x98,0x55,0xa9,
  24970. 0x4a,0x7c,0x19,0x58,0xd1,0xf8,0xad,0xa5,
  24971. 0xd0,0x53,0x2e,0xd8,0xa5,0xaa,0x3f,0xb2,
  24972. 0xd1,0x7b,0xa7,0x0e,0xb6,0x24,0x8e,0x59,
  24973. 0x4e,0x1a,0x22,0x97,0xac,0xbb,0xb3,0x9d,
  24974. 0x50,0x2f,0x1a,0x8c,0x6e,0xb6,0xf1,0xce,
  24975. 0x22,0xb3,0xde,0x1a,0x1f,0x40,0xcc,0x24,
  24976. 0x55,0x41,0x19,0xa8,0x31,0xa9,0xaa,0xd6,
  24977. 0x07,0x9c,0xad,0x88,0x42,0x5d,0xe6,0xbd,
  24978. 0xe1,0xa9,0x18,0x7e,0xbb,0x60,0x92,0xcf,
  24979. 0x67,0xbf,0x2b,0x13,0xfd,0x65,0xf2,0x70,
  24980. 0x88,0xd7,0x8b,0x7e,0x88,0x3c,0x87,0x59,
  24981. 0xd2,0xc4,0xf5,0xc6,0x5a,0xdb,0x75,0x53,
  24982. 0x87,0x8a,0xd5,0x75,0xf9,0xfa,0xd8,0x78,
  24983. 0xe8,0x0a,0x0c,0x9b,0xa6,0x3b,0xcb,0xcc,
  24984. 0x27,0x32,0xe6,0x94,0x85,0xbb,0xc9,0xc9,
  24985. 0x0b,0xfb,0xd6,0x24,0x81,0xd9,0x08,0x9b,
  24986. 0xec,0xcf,0x80,0xcf,0xe2,0xdf,0x16,0xa2,
  24987. 0xcf,0x65,0xbd,0x92,0xdd,0x59,0x7b,0x07,
  24988. 0x07,0xe0,0x91,0x7a,0xf4,0x8b,0xbb,0x75,
  24989. 0xfe,0xd4,0x13,0xd2,0x38,0xf5,0x55,0x5a,
  24990. 0x7a,0x56,0x9d,0x80,0xc3,0x41,0x4a,0x8d,
  24991. 0x08,0x59,0xdc,0x65,0xa4,0x61,0x28,0xba,
  24992. 0xb2,0x7a,0xf8,0x7a,0x71,0x31,0x4f,0x31,
  24993. 0x8c,0x78,0x2b,0x23,0xeb,0xfe,0x80,0x8b,
  24994. 0x82,0xb0,0xce,0x26,0x40,0x1d,0x2e,0x22,
  24995. 0xf0,0x4d,0x83,0xd1,0x25,0x5d,0xc5,0x1a,
  24996. 0xdd,0xd3,0xb7,0x5a,0x2b,0x1a,0xe0,0x78,
  24997. 0x45,0x04,0xdf,0x54,0x3a,0xf8,0x96,0x9b,
  24998. 0xe3,0xea,0x70,0x82,0xff,0x7f,0xc9,0x88,
  24999. 0x8c,0x14,0x4d,0xa2,0xaf,0x58,0x42,0x9e,
  25000. 0xc9,0x60,0x31,0xdb,0xca,0xd3,0xda,0xd9,
  25001. 0xaf,0x0d,0xcb,0xaa,0xaf,0x26,0x8c,0xb8,
  25002. 0xfc,0xff,0xea,0xd9,0x4f,0x3c,0x7c,0xa4,
  25003. 0x95,0xe0,0x56,0xa9,0xb4,0x7a,0xcd,0xb7,
  25004. 0x51,0xfb,0x73,0xe6,0x66,0xc6,0xc6,0x55,
  25005. 0xad,0xe8,0x29,0x72,0x97,0xd0,0x7a,0xd1,
  25006. 0xba,0x5e,0x43,0xf1,0xbc,0xa3,0x23,0x01,
  25007. 0x65,0x13,0x39,0xe2,0x29,0x04,0xcc,0x8c,
  25008. 0x42,0xf5,0x8c,0x30,0xc0,0x4a,0xaf,0xdb,
  25009. 0x03,0x8d,0xda,0x08,0x47,0xdd,0x98,0x8d,
  25010. 0xcd,0xa6,0xf3,0xbf,0xd1,0x5c,0x4b,0x4c,
  25011. 0x45,0x25,0x00,0x4a,0xa0,0x6e,0xef,0xf8,
  25012. 0xca,0x61,0x78,0x3a,0xac,0xec,0x57,0xfb,
  25013. 0x3d,0x1f,0x92,0xb0,0xfe,0x2f,0xd1,0xa8,
  25014. 0x5f,0x67,0x24,0x51,0x7b,0x65,0xe6,0x14,
  25015. 0xad,0x68,0x08,0xd6,0xf6,0xee,0x34,0xdf,
  25016. 0xf7,0x31,0x0f,0xdc,0x82,0xae,0xbf,0xd9,
  25017. 0x04,0xb0,0x1e,0x1d,0xc5,0x4b,0x29,0x27,
  25018. 0x09,0x4b,0x2d,0xb6,0x8d,0x6f,0x90,0x3b,
  25019. 0x68,0x40,0x1a,0xde,0xbf,0x5a,0x7e,0x08,
  25020. 0xd7,0x8f,0xf4,0xef,0x5d,0x63,0x65,0x3a,
  25021. 0x65,0x04,0x0c,0xf9,0xbf,0xd4,0xac,0xa7,
  25022. 0x98,0x4a,0x74,0xd3,0x71,0x45,0x98,0x67,
  25023. 0x80,0xfc,0x0b,0x16,0xac,0x45,0x16,0x49,
  25024. 0xde,0x61,0x88,0xa7,0xdb,0xdf,0x19,0x1f,
  25025. 0x64,0xb5,0xfc,0x5e,0x2a,0xb4,0x7b,0x57,
  25026. 0xf7,0xf7,0x27,0x6c,0xd4,0x19,0xc1,0x7a,
  25027. 0x3c,0xa8,0xe1,0xb9,0x39,0xae,0x49,0xe4,
  25028. 0x88,0xac,0xba,0x6b,0x96,0x56,0x10,0xb5,
  25029. 0x48,0x01,0x09,0xc8,0xb1,0x7b,0x80,0xe1,
  25030. 0xb7,0xb7,0x50,0xdf,0xc7,0x59,0x8d,0x5d,
  25031. 0x50,0x11,0xfd,0x2d,0xcc,0x56,0x00,0xa3,
  25032. 0x2e,0xf5,0xb5,0x2a,0x1e,0xcc,0x82,0x0e,
  25033. 0x30,0x8a,0xa3,0x42,0x72,0x1a,0xac,0x09,
  25034. 0x43,0xbf,0x66,0x86,0xb6,0x4b,0x25,0x79,
  25035. 0x37,0x65,0x04,0xcc,0xc4,0x93,0xd9,0x7e,
  25036. 0x6a,0xed,0x3f,0xb0,0xf9,0xcd,0x71,0xa4,
  25037. 0x3d,0xd4,0x97,0xf0,0x1f,0x17,0xc0,0xe2,
  25038. 0xcb,0x37,0x97,0xaa,0x2a,0x2f,0x25,0x66,
  25039. 0x56,0x16,0x8e,0x6c,0x49,0x6a,0xfc,0x5f,
  25040. 0xb9,0x32,0x46,0xf6,0xb1,0x11,0x63,0x98,
  25041. 0xa3,0x46,0xf1,0xa6,0x41,0xf3,0xb0,0x41,
  25042. 0xe9,0x89,0xf7,0x91,0x4f,0x90,0xcc,0x2c,
  25043. 0x7f,0xff,0x35,0x78,0x76,0xe5,0x06,0xb5,
  25044. 0x0d,0x33,0x4b,0xa7,0x7c,0x22,0x5b,0xc3,
  25045. 0x07,0xba,0x53,0x71,0x52,0xf3,0xf1,0x61,
  25046. 0x0e,0x4e,0xaf,0xe5,0x95,0xf6,0xd9,0xd9,
  25047. 0x0d,0x11,0xfa,0xa9,0x33,0xa1,0x5e,0xf1,
  25048. 0x36,0x95,0x46,0x86,0x8a,0x7f,0x3a,0x45,
  25049. 0xa9,0x67,0x68,0xd4,0x0f,0xd9,0xd0,0x34,
  25050. 0x12,0xc0,0x91,0xc6,0x31,0x5c,0xf4,0xfd,
  25051. 0xe7,0xcb,0x68,0x60,0x69,0x37,0x38,0x0d,
  25052. 0xb2,0xea,0xaa,0x70,0x7b,0x4c,0x41,0x85,
  25053. 0xc3,0x2e,0xdd,0xcd,0xd3,0x06,0x70,0x5e,
  25054. 0x4d,0xc1,0xff,0xc8,0x72,0xee,0xee,0x47,
  25055. 0x5a,0x64,0xdf,0xac,0x86,0xab,0xa4,0x1c,
  25056. 0x06,0x18,0x98,0x3f,0x87,0x41,0xc5,0xef,
  25057. 0x68,0xd3,0xa1,0x01,0xe8,0xa3,0xb8,0xca,
  25058. 0xc6,0x0c,0x90,0x5c,0x15,0xfc,0x91,0x08,
  25059. 0x40,0xb9,0x4c,0x00,0xa0,0xb9,0xd0
  25060. };
  25061. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  25062. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  25063. sizeof(msg2),
  25064. sizeof(msg3),
  25065. 0 /*sizeof(msg1)*/,
  25066. 0 /*sizeof(msg1)*/,
  25067. sizeof(msg4)
  25068. };
  25069. #ifndef NO_ASN
  25070. static byte privateEd25519[] = {
  25071. 0x30,0x2e,0x02,0x01,0x00,0x30,0x05,0x06,
  25072. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  25073. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  25074. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  25075. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  25076. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  25077. };
  25078. static byte badPrivateEd25519[] = {
  25079. 0x30,0x52,0x02,0x01,0x00,0x30,0x05,0x06,
  25080. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  25081. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  25082. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  25083. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  25084. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60,
  25085. 0xa1,0x22,0x04,0x21,0xd7,0x5a,0x98,0x01, /* octet len 0x20 -> 0x21 */
  25086. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  25087. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  25088. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  25089. 0xf7,0x07,0x51,0x1a,
  25090. 0x00 /* add additional bytes to make the pubkey bigger */
  25091. };
  25092. static byte publicEd25519[] = {
  25093. 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,
  25094. 0x70,0x03,0x21,0x00,0xd7,0x5a,0x98,0x01,
  25095. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  25096. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  25097. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  25098. 0xf7,0x07,0x51,0x1a
  25099. };
  25100. /* size has been altered to catch if sanity check is done */
  25101. static byte badPublicEd25519[] = {
  25102. 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,
  25103. 0x70,0x03,0x21,0x00,0xd7,0x5a,0x98,0x01,
  25104. 0x82,0xb1,0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,
  25105. 0xc9,0x64,0x07,0x3a,0x0e,0xe1,0x72,0xf3,
  25106. 0xda,0xa6,0x23,0x25,0xaf,0x02,0x1a,0x68,
  25107. 0xf7,0x07,0x51,0x1a,
  25108. 0x00 /* add an additional byte to make the pubkey appear bigger */
  25109. };
  25110. static byte privPubEd25519[] = {
  25111. 0x30,0x50,0x02,0x01,0x00,0x30,0x05,0x06,
  25112. 0x03,0x2b,0x65,0x70,0x04,0x22,0x04,0x20,
  25113. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  25114. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  25115. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  25116. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60,
  25117. 0x81,0x20,0xd7,0x5a,0x98,0x01,0x82,0xb1,
  25118. 0x0a,0xb7,0xd5,0x4b,0xfe,0xd3,0xc9,0x64,
  25119. 0x07,0x3a,0x0e,0xe1,0x72,0xf3,0xda,0xa6,
  25120. 0x23,0x25,0xaf,0x02,0x1a,0x68,0xf7,0x07,
  25121. 0x51,0x1a
  25122. };
  25123. word32 idx;
  25124. #endif /* NO_ASN */
  25125. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  25126. #if !defined(NO_ASN) && defined(HAVE_ED25519_SIGN)
  25127. ed25519_key key3;
  25128. #endif
  25129. /* create ed25519 keys */
  25130. #ifndef HAVE_FIPS
  25131. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  25132. #else
  25133. ret = wc_InitRng(&rng);
  25134. #endif
  25135. if (ret != 0)
  25136. return -11000;
  25137. wc_ed25519_init_ex(&key, HEAP_HINT, devId);
  25138. wc_ed25519_init_ex(&key2, HEAP_HINT, devId);
  25139. #if !defined(NO_ASN) && defined(HAVE_ED25519_SIGN)
  25140. wc_ed25519_init_ex(&key3, HEAP_HINT, devId);
  25141. #endif
  25142. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  25143. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key2);
  25144. /* helper functions for signature and key size */
  25145. keySz = wc_ed25519_size(&key);
  25146. sigSz = wc_ed25519_sig_size(&key);
  25147. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  25148. defined(HAVE_ED25519_KEY_IMPORT)
  25149. for (i = 0; i < 6; i++) {
  25150. outlen = sizeof(out);
  25151. XMEMSET(out, 0, sizeof(out));
  25152. if (wc_ed25519_import_private_key(sKeys[i], ED25519_KEY_SIZE, pKeys[i],
  25153. pKeySz[i], &key) != 0)
  25154. return -11001 - i;
  25155. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key) != 0)
  25156. return -11011 - i;
  25157. if (XMEMCMP(out, sigs[i], 64))
  25158. return -11021 - i;
  25159. #if defined(HAVE_ED25519_VERIFY)
  25160. /* test verify on good msg */
  25161. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  25162. &key) != 0 || verify != 1)
  25163. return -11031 - i;
  25164. #ifdef WOLFSSL_ED25519_STREAMING_VERIFY
  25165. /* test verify on good msg using streaming interface directly */
  25166. if (wc_ed25519_verify_msg_init(out, outlen,
  25167. &key, (byte)Ed25519, NULL, 0) != 0)
  25168. return -11211 - i;
  25169. for (j = 0; j < msgSz[i]; j += i) {
  25170. if (wc_ed25519_verify_msg_update(msgs[i] + j, MIN(i, msgSz[i] - j), &key) != 0)
  25171. return -11221 - i;
  25172. }
  25173. if (wc_ed25519_verify_msg_final(out, outlen, &verify,
  25174. &key) != 0 || verify != 1)
  25175. return -11231 - i;
  25176. #endif /* WOLFSSL_ED25519_STREAMING_VERIFY */
  25177. /* test verify on bad msg */
  25178. out[outlen-1] = out[outlen-1] + 1;
  25179. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  25180. &key) == 0 || verify == 1)
  25181. return -11041 - i;
  25182. #endif /* HAVE_ED25519_VERIFY */
  25183. /* test api for import/exporting keys */
  25184. exportPSz = sizeof(exportPKey);
  25185. exportSSz = sizeof(exportSKey);
  25186. if (wc_ed25519_export_public(&key, exportPKey, &exportPSz) != 0)
  25187. return -11051 - i;
  25188. if (wc_ed25519_import_public_ex(exportPKey, exportPSz, &key2, 1) != 0)
  25189. return -11061 - i;
  25190. if (wc_ed25519_export_private_only(&key, exportSKey, &exportSSz) != 0)
  25191. return -11071 - i;
  25192. if (wc_ed25519_import_private_key(exportSKey, exportSSz,
  25193. exportPKey, exportPSz, &key2) != 0)
  25194. return -11081 - i;
  25195. /* clear "out" buffer and test sign with imported keys */
  25196. outlen = sizeof(out);
  25197. XMEMSET(out, 0, sizeof(out));
  25198. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key2) != 0)
  25199. return -11091 - i;
  25200. #if defined(HAVE_ED25519_VERIFY)
  25201. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  25202. &key2) != 0 || verify != 1)
  25203. return -11101 - i;
  25204. if (XMEMCMP(out, sigs[i], 64))
  25205. return -11111 - i;
  25206. #endif /* HAVE_ED25519_VERIFY */
  25207. }
  25208. ret = ed25519ctx_test();
  25209. if (ret != 0)
  25210. return ret;
  25211. ret = ed25519ph_test();
  25212. if (ret != 0)
  25213. return ret;
  25214. #ifndef NO_ASN
  25215. /* Try ASN.1 encoded private-only key and public key. */
  25216. idx = 0;
  25217. if (wc_Ed25519PrivateKeyDecode(privateEd25519, &idx, &key3,
  25218. sizeof(privateEd25519)) != 0)
  25219. return -11121;
  25220. idx = 0;
  25221. if (wc_Ed25519PrivateKeyDecode(badPrivateEd25519, &idx, &key3,
  25222. sizeof(badPrivateEd25519)) == 0)
  25223. return -11122;
  25224. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3)
  25225. != BAD_FUNC_ARG)
  25226. return -11131;
  25227. /* try with a buffer size that is too large */
  25228. idx = 0;
  25229. if (wc_Ed25519PublicKeyDecode(badPublicEd25519, &idx, &key3,
  25230. sizeof(badPublicEd25519)) == 0)
  25231. return -11140;
  25232. idx = 0;
  25233. if (wc_Ed25519PublicKeyDecode(publicEd25519, &idx, &key3,
  25234. sizeof(publicEd25519)) != 0)
  25235. return -11141;
  25236. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3) != 0)
  25237. return -11151;
  25238. if (XMEMCMP(out, sigs[0], 64))
  25239. return -11161;
  25240. #if defined(HAVE_ED25519_VERIFY)
  25241. /* test verify on good msg */
  25242. if (wc_ed25519_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, &key3)
  25243. != 0 || verify != 1)
  25244. return -11171;
  25245. #endif /* HAVE_ED25519_VERIFY */
  25246. wc_ed25519_free(&key3);
  25247. wc_ed25519_init(&key3);
  25248. idx = 0;
  25249. if (wc_Ed25519PrivateKeyDecode(privPubEd25519, &idx, &key3,
  25250. sizeof(privPubEd25519)) != 0)
  25251. return -11181;
  25252. if (wc_ed25519_sign_msg(msgs[0], msgSz[0], out, &outlen, &key3) != 0)
  25253. return -11191;
  25254. if (XMEMCMP(out, sigs[0], 64))
  25255. return -11201;
  25256. wc_ed25519_free(&key3);
  25257. #endif /* NO_ASN */
  25258. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  25259. /* clean up keys when done */
  25260. wc_ed25519_free(&key);
  25261. wc_ed25519_free(&key2);
  25262. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  25263. wc_FreeRng(&rng);
  25264. #endif
  25265. /* hush warnings of unused keySz and sigSz */
  25266. (void)keySz;
  25267. (void)sigSz;
  25268. #ifdef WOLFSSL_TEST_CERT
  25269. ret = ed25519_test_cert();
  25270. if (ret < 0)
  25271. return ret;
  25272. #ifdef WOLFSSL_CERT_GEN
  25273. ret = ed25519_test_make_cert();
  25274. if (ret < 0)
  25275. return ret;
  25276. #endif /* WOLFSSL_CERT_GEN */
  25277. #endif /* WOLFSSL_TEST_CERT */
  25278. return 0;
  25279. }
  25280. #endif /* HAVE_ED25519 */
  25281. #ifdef HAVE_CURVE448
  25282. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  25283. defined(HAVE_CURVE448_KEY_IMPORT)
  25284. /* Test the wc_curve448_check_public API.
  25285. *
  25286. * returns 0 on success and -ve on failure.
  25287. */
  25288. static int curve448_check_public_test(void)
  25289. {
  25290. /* Little-endian values that will fail */
  25291. byte fail_le[][CURVE448_KEY_SIZE] = {
  25292. {
  25293. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25294. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25295. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25296. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25297. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25298. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25299. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  25300. },
  25301. {
  25302. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25303. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25304. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25305. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25306. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25307. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25308. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  25309. },
  25310. };
  25311. /* Big-endian values that will fail */
  25312. byte fail_be[][CURVE448_KEY_SIZE] = {
  25313. {
  25314. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25315. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25316. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25317. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25318. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25319. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25320. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  25321. },
  25322. {
  25323. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25324. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25325. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25326. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25327. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25328. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25329. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  25330. },
  25331. };
  25332. /* Good or valid public value */
  25333. byte good[CURVE448_KEY_SIZE] = {
  25334. 0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25335. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25336. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25337. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25338. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25339. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  25340. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  25341. };
  25342. int i;
  25343. /* Parameter checks */
  25344. /* NULL pointer */
  25345. if (wc_curve448_check_public(NULL, 0, EC448_LITTLE_ENDIAN) !=
  25346. BAD_FUNC_ARG) {
  25347. return -11300;
  25348. }
  25349. if (wc_curve448_check_public(NULL, 0, EC448_BIG_ENDIAN) != BAD_FUNC_ARG) {
  25350. return -11301;
  25351. }
  25352. /* Length of 0 treated differently to other invalid lengths for TLS */
  25353. if (wc_curve448_check_public(good, 0, EC448_LITTLE_ENDIAN) != BUFFER_E)
  25354. return -11302;
  25355. if (wc_curve448_check_public(good, 0, EC448_BIG_ENDIAN) != BUFFER_E)
  25356. return -11303;
  25357. /* Length not CURVE448_KEY_SIZE */
  25358. for (i = 1; i < CURVE448_KEY_SIZE + 2; i++) {
  25359. if (i == CURVE448_KEY_SIZE)
  25360. continue;
  25361. if (wc_curve448_check_public(good, i, EC448_LITTLE_ENDIAN) !=
  25362. ECC_BAD_ARG_E) {
  25363. return -11304 - i;
  25364. }
  25365. if (wc_curve448_check_public(good, i, EC448_BIG_ENDIAN) !=
  25366. ECC_BAD_ARG_E) {
  25367. return -11314 - i;
  25368. }
  25369. }
  25370. /* Little-endian fail cases */
  25371. for (i = 0; i < (int)(sizeof(fail_le) / sizeof(*fail_le)); i++) {
  25372. if (wc_curve448_check_public(fail_le[i], CURVE448_KEY_SIZE,
  25373. EC448_LITTLE_ENDIAN) == 0) {
  25374. return -11324 - i;
  25375. }
  25376. }
  25377. /* Big-endian fail cases */
  25378. for (i = 0; i < (int)(sizeof(fail_be) / sizeof(*fail_be)); i++) {
  25379. if (wc_curve448_check_public(fail_be[i], CURVE448_KEY_SIZE,
  25380. EC448_BIG_ENDIAN) == 0) {
  25381. return -11334 - i;
  25382. }
  25383. }
  25384. /* Check a valid public value works! */
  25385. if (wc_curve448_check_public(good, CURVE448_KEY_SIZE,
  25386. EC448_LITTLE_ENDIAN) != 0) {
  25387. return -11344;
  25388. }
  25389. if (wc_curve448_check_public(good, CURVE448_KEY_SIZE,
  25390. EC448_BIG_ENDIAN) != 0) {
  25391. return -11345;
  25392. }
  25393. return 0;
  25394. }
  25395. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  25396. WOLFSSL_TEST_SUBROUTINE int curve448_test(void)
  25397. {
  25398. WC_RNG rng;
  25399. int ret;
  25400. #ifdef HAVE_CURVE448_SHARED_SECRET
  25401. byte sharedA[CURVE448_KEY_SIZE];
  25402. byte sharedB[CURVE448_KEY_SIZE];
  25403. word32 y;
  25404. #endif
  25405. #ifdef HAVE_CURVE448_KEY_EXPORT
  25406. byte exportBuf[CURVE448_KEY_SIZE];
  25407. #endif
  25408. word32 x;
  25409. curve448_key userA, userB, pubKey;
  25410. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  25411. defined(HAVE_CURVE448_KEY_IMPORT)
  25412. /* test vectors from
  25413. https://www.rfc-editor.org/rfc/rfc7748.html
  25414. */
  25415. /* secret key for party a */
  25416. byte sa[] = {
  25417. 0x6b, 0x72, 0x98, 0xa5, 0xc0, 0xd8, 0xc2, 0x9a,
  25418. 0x1d, 0xab, 0x27, 0xf1, 0xa6, 0x82, 0x63, 0x00,
  25419. 0x91, 0x73, 0x89, 0x44, 0x97, 0x41, 0xa9, 0x74,
  25420. 0xf5, 0xba, 0xc9, 0xd9, 0x8d, 0xc2, 0x98, 0xd4,
  25421. 0x65, 0x55, 0xbc, 0xe8, 0xba, 0xe8, 0x9e, 0xee,
  25422. 0xd4, 0x00, 0x58, 0x4b, 0xb0, 0x46, 0xcf, 0x75,
  25423. 0x57, 0x9f, 0x51, 0xd1, 0x25, 0x49, 0x8f, 0x9a,
  25424. };
  25425. /* public key for party a */
  25426. byte pa[] = {
  25427. 0xa0, 0x1f, 0xc4, 0x32, 0xe5, 0x80, 0x7f, 0x17,
  25428. 0x53, 0x0d, 0x12, 0x88, 0xda, 0x12, 0x5b, 0x0c,
  25429. 0xd4, 0x53, 0xd9, 0x41, 0x72, 0x64, 0x36, 0xc8,
  25430. 0xbb, 0xd9, 0xc5, 0x22, 0x2c, 0x3d, 0xa7, 0xfa,
  25431. 0x63, 0x9c, 0xe0, 0x3d, 0xb8, 0xd2, 0x3b, 0x27,
  25432. 0x4a, 0x07, 0x21, 0xa1, 0xae, 0xd5, 0x22, 0x7d,
  25433. 0xe6, 0xe3, 0xb7, 0x31, 0xcc, 0xf7, 0x08, 0x9b,
  25434. };
  25435. /* secret key for party b */
  25436. byte sb[] = {
  25437. 0x2d, 0x99, 0x73, 0x51, 0xb6, 0x10, 0x6f, 0x36,
  25438. 0xb0, 0xd1, 0x09, 0x1b, 0x92, 0x9c, 0x4c, 0x37,
  25439. 0x21, 0x3e, 0x0d, 0x2b, 0x97, 0xe8, 0x5e, 0xbb,
  25440. 0x20, 0xc1, 0x27, 0x69, 0x1d, 0x0d, 0xad, 0x8f,
  25441. 0x1d, 0x81, 0x75, 0xb0, 0x72, 0x37, 0x45, 0xe6,
  25442. 0x39, 0xa3, 0xcb, 0x70, 0x44, 0x29, 0x0b, 0x99,
  25443. 0xe0, 0xe2, 0xa0, 0xc2, 0x7a, 0x6a, 0x30, 0x1c,
  25444. };
  25445. /* public key for party b */
  25446. byte pb[] = {
  25447. 0x09, 0x36, 0xf3, 0x7b, 0xc6, 0xc1, 0xbd, 0x07,
  25448. 0xae, 0x3d, 0xec, 0x7a, 0xb5, 0xdc, 0x06, 0xa7,
  25449. 0x3c, 0xa1, 0x32, 0x42, 0xfb, 0x34, 0x3e, 0xfc,
  25450. 0x72, 0xb9, 0xd8, 0x27, 0x30, 0xb4, 0x45, 0xf3,
  25451. 0xd4, 0xb0, 0xbd, 0x07, 0x71, 0x62, 0xa4, 0x6d,
  25452. 0xcf, 0xec, 0x6f, 0x9b, 0x59, 0x0b, 0xfc, 0xbc,
  25453. 0xf5, 0x20, 0xcd, 0xb0, 0x29, 0xa8, 0xb7, 0x3e,
  25454. };
  25455. /* expected shared key */
  25456. byte ss[] = {
  25457. 0x9d, 0x87, 0x4a, 0x51, 0x37, 0x50, 0x9a, 0x44,
  25458. 0x9a, 0xd5, 0x85, 0x30, 0x40, 0x24, 0x1c, 0x52,
  25459. 0x36, 0x39, 0x54, 0x35, 0xc3, 0x64, 0x24, 0xfd,
  25460. 0x56, 0x0b, 0x0c, 0xb6, 0x2b, 0x28, 0x1d, 0x28,
  25461. 0x52, 0x75, 0xa7, 0x40, 0xce, 0x32, 0xa2, 0x2d,
  25462. 0xd1, 0x74, 0x0f, 0x4a, 0xa9, 0x16, 0x1c, 0xec,
  25463. 0x95, 0xcc, 0xc6, 0x1a, 0x18, 0xf4, 0xff, 0x07,
  25464. };
  25465. #endif /* HAVE_CURVE448_SHARED_SECRET */
  25466. (void)x;
  25467. #ifndef HAVE_FIPS
  25468. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  25469. #else
  25470. ret = wc_InitRng(&rng);
  25471. #endif
  25472. if (ret != 0)
  25473. return -11400;
  25474. wc_curve448_init(&userA);
  25475. wc_curve448_init(&userB);
  25476. wc_curve448_init(&pubKey);
  25477. /* make curve448 keys */
  25478. if (wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userA) != 0)
  25479. return -11401;
  25480. if (wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &userB) != 0)
  25481. return -11402;
  25482. #ifdef HAVE_CURVE448_SHARED_SECRET
  25483. /* find shared secret key */
  25484. x = sizeof(sharedA);
  25485. if (wc_curve448_shared_secret(&userA, &userB, sharedA, &x) != 0)
  25486. return -11403;
  25487. y = sizeof(sharedB);
  25488. if (wc_curve448_shared_secret(&userB, &userA, sharedB, &y) != 0)
  25489. return -11404;
  25490. /* compare shared secret keys to test they are the same */
  25491. if (y != x)
  25492. return -11405;
  25493. if (XMEMCMP(sharedA, sharedB, x))
  25494. return -11406;
  25495. #endif
  25496. #ifdef HAVE_CURVE448_KEY_EXPORT
  25497. /* export a public key and import it for another user */
  25498. x = sizeof(exportBuf);
  25499. if (wc_curve448_export_public(&userA, exportBuf, &x) != 0)
  25500. return -11407;
  25501. #ifdef HAVE_CURVE448_KEY_IMPORT
  25502. if (wc_curve448_import_public(exportBuf, x, &pubKey) != 0)
  25503. return -11408;
  25504. #endif
  25505. #endif
  25506. #if defined(HAVE_CURVE448_SHARED_SECRET) && \
  25507. defined(HAVE_CURVE448_KEY_IMPORT)
  25508. /* test shared key after importing a public key */
  25509. XMEMSET(sharedB, 0, sizeof(sharedB));
  25510. y = sizeof(sharedB);
  25511. if (wc_curve448_shared_secret(&userB, &pubKey, sharedB, &y) != 0)
  25512. return -11409;
  25513. if (XMEMCMP(sharedA, sharedB, y))
  25514. return -11410;
  25515. /* import RFC test vectors and compare shared key */
  25516. if (wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  25517. != 0)
  25518. return -11411;
  25519. if (wc_curve448_import_private_raw(sb, sizeof(sb), pb, sizeof(pb), &userB)
  25520. != 0)
  25521. return -11412;
  25522. /* test against known test vector */
  25523. XMEMSET(sharedB, 0, sizeof(sharedB));
  25524. y = sizeof(sharedB);
  25525. if (wc_curve448_shared_secret(&userA, &userB, sharedB, &y) != 0)
  25526. return -11413;
  25527. if (XMEMCMP(ss, sharedB, y))
  25528. return -11414;
  25529. /* test swapping roles of keys and generating same shared key */
  25530. XMEMSET(sharedB, 0, sizeof(sharedB));
  25531. y = sizeof(sharedB);
  25532. if (wc_curve448_shared_secret(&userB, &userA, sharedB, &y) != 0)
  25533. return -11415;
  25534. if (XMEMCMP(ss, sharedB, y))
  25535. return -11416;
  25536. /* test with 1 generated key and 1 from known test vector */
  25537. if (wc_curve448_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  25538. != 0)
  25539. return -11417;
  25540. if (wc_curve448_make_key(&rng, 56, &userB) != 0)
  25541. return -11418;
  25542. x = sizeof(sharedA);
  25543. if (wc_curve448_shared_secret(&userA, &userB, sharedA, &x) != 0)
  25544. return -11419;
  25545. y = sizeof(sharedB);
  25546. if (wc_curve448_shared_secret(&userB, &userA, sharedB, &y) != 0)
  25547. return -11420;
  25548. /* compare shared secret keys to test they are the same */
  25549. if (y != x)
  25550. return -11421;
  25551. if (XMEMCMP(sharedA, sharedB, x))
  25552. return -11422;
  25553. ret = curve448_check_public_test();
  25554. if (ret != 0)
  25555. return ret;
  25556. #endif /* HAVE_CURVE448_SHARED_SECRET && HAVE_CURVE448_KEY_IMPORT */
  25557. /* clean up keys when done */
  25558. wc_curve448_free(&pubKey);
  25559. wc_curve448_free(&userB);
  25560. wc_curve448_free(&userA);
  25561. wc_FreeRng(&rng);
  25562. return 0;
  25563. }
  25564. #endif /* HAVE_CURVE448 */
  25565. #ifdef HAVE_ED448
  25566. #ifdef WOLFSSL_TEST_CERT
  25567. static int ed448_test_cert(void)
  25568. {
  25569. DecodedCert cert[2];
  25570. DecodedCert* serverCert = NULL;
  25571. DecodedCert* caCert = NULL;
  25572. #ifdef HAVE_ED448_VERIFY
  25573. ed448_key key;
  25574. ed448_key* pubKey = NULL;
  25575. int verify;
  25576. #endif /* HAVE_ED448_VERIFY */
  25577. int ret;
  25578. byte* tmp;
  25579. size_t bytes;
  25580. XFILE file;
  25581. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25582. if (tmp == NULL) {
  25583. ERROR_OUT(-11430, done);
  25584. }
  25585. #ifdef USE_CERT_BUFFERS_256
  25586. XMEMCPY(tmp, ca_ed448_cert, sizeof_ca_ed448_cert);
  25587. bytes = sizeof_ca_ed448_cert;
  25588. #elif !defined(NO_FILESYSTEM)
  25589. file = XFOPEN(caEd448Cert, "rb");
  25590. if (file == NULL) {
  25591. ERROR_OUT(-11431, done);
  25592. }
  25593. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  25594. XFCLOSE(file);
  25595. #else
  25596. /* No certificate to use. */
  25597. ERROR_OUT(-11432, done);
  25598. #endif
  25599. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  25600. caCert = &cert[0];
  25601. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  25602. if (ret != 0) {
  25603. ERROR_OUT(-11433, done);
  25604. }
  25605. #ifdef USE_CERT_BUFFERS_256
  25606. XMEMCPY(tmp, server_ed448_cert, sizeof_server_ed448_cert);
  25607. bytes = sizeof_server_ed448_cert;
  25608. #elif !defined(NO_FILESYSTEM)
  25609. file = XFOPEN(serverEd448Cert, "rb");
  25610. if (file == NULL) {
  25611. ERROR_OUT(-11434, done);
  25612. }
  25613. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  25614. XFCLOSE(file);
  25615. #else
  25616. /* No certificate to use. */
  25617. ERROR_OUT(-11435, done);
  25618. #endif
  25619. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  25620. serverCert = &cert[1];
  25621. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  25622. if (ret != 0) {
  25623. ERROR_OUT(-11436, done);
  25624. }
  25625. #ifdef HAVE_ED448_VERIFY
  25626. ret = wc_ed448_init(&key);
  25627. if (ret < 0) {
  25628. ERROR_OUT(-11437, done);
  25629. }
  25630. pubKey = &key;
  25631. ret = wc_ed448_import_public(caCert->publicKey, caCert->pubKeySize, pubKey);
  25632. if (ret < 0) {
  25633. ERROR_OUT(-11438, done);
  25634. }
  25635. if (wc_ed448_verify_msg(serverCert->signature, serverCert->sigLength,
  25636. serverCert->source + serverCert->certBegin,
  25637. serverCert->sigIndex - serverCert->certBegin,
  25638. &verify, pubKey, NULL, 0) < 0 || verify != 1) {
  25639. ERROR_OUT(-11439, done);
  25640. }
  25641. #endif /* HAVE_ED448_VERIFY */
  25642. done:
  25643. if (tmp != NULL)
  25644. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25645. #ifdef HAVE_ED448_VERIFY
  25646. wc_ed448_free(pubKey);
  25647. #endif /* HAVE_ED448_VERIFY */
  25648. if (caCert != NULL)
  25649. FreeDecodedCert(caCert);
  25650. if (serverCert != NULL)
  25651. FreeDecodedCert(serverCert);
  25652. return ret;
  25653. }
  25654. static int ed448_test_make_cert(void)
  25655. {
  25656. WC_RNG rng;
  25657. Cert cert;
  25658. DecodedCert decode;
  25659. ed448_key key;
  25660. ed448_key* privKey = NULL;
  25661. int ret = 0;
  25662. byte* tmp = NULL;
  25663. wc_InitCert_ex(&cert, HEAP_HINT, devId);
  25664. #ifndef HAVE_FIPS
  25665. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  25666. #else
  25667. ret = wc_InitRng(&rng);
  25668. #endif
  25669. if (ret != 0)
  25670. return -11450;
  25671. wc_ed448_init(&key);
  25672. privKey = &key;
  25673. wc_ed448_make_key(&rng, ED448_KEY_SIZE, privKey);
  25674. cert.daysValid = 365 * 2;
  25675. cert.selfSigned = 1;
  25676. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  25677. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  25678. cert.isCA = 0;
  25679. #ifdef WOLFSSL_CERT_EXT
  25680. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  25681. if (ret < 0) {
  25682. ERROR_OUT(-11451, done);
  25683. }
  25684. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  25685. if (ret < 0) {
  25686. ERROR_OUT(-11452, done);
  25687. }
  25688. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, privKey);
  25689. if (ret < 0) {
  25690. ERROR_OUT(-11453, done);
  25691. }
  25692. #endif
  25693. tmp = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25694. if (tmp == NULL) {
  25695. ERROR_OUT(-11454, done);
  25696. }
  25697. cert.sigType = CTC_ED448;
  25698. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED448_TYPE, privKey, &rng);
  25699. if (ret < 0) {
  25700. ERROR_OUT(-11455, done);
  25701. }
  25702. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF, ED448_TYPE,
  25703. privKey, &rng);
  25704. if (ret < 0) {
  25705. ERROR_OUT(-11456, done);
  25706. }
  25707. InitDecodedCert(&decode, tmp, ret, HEAP_HINT);
  25708. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  25709. FreeDecodedCert(&decode);
  25710. if (ret != 0) {
  25711. ERROR_OUT(-11457, done);
  25712. }
  25713. done:
  25714. if (tmp != NULL)
  25715. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25716. wc_ed448_free(privKey);
  25717. wc_FreeRng(&rng);
  25718. return ret;
  25719. }
  25720. #endif /* WOLFSSL_TEST_CERT */
  25721. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  25722. defined(HAVE_ED448_KEY_IMPORT)
  25723. static int ed448_ctx_test(void)
  25724. {
  25725. byte out[ED448_SIG_SIZE];
  25726. word32 outlen;
  25727. #ifdef HAVE_ED448_VERIFY
  25728. int verify;
  25729. #endif /* HAVE_ED448_VERIFY */
  25730. ed448_key key;
  25731. WOLFSSL_SMALL_STACK_STATIC const byte sKeyCtx[] = {
  25732. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  25733. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  25734. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  25735. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  25736. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  25737. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  25738. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  25739. 0x4e
  25740. };
  25741. WOLFSSL_SMALL_STACK_STATIC const byte pKeyCtx[] = {
  25742. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  25743. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  25744. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  25745. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  25746. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  25747. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  25748. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  25749. 0x80
  25750. };
  25751. WOLFSSL_SMALL_STACK_STATIC const byte sigCtx[] = {
  25752. 0xd4, 0xf8, 0xf6, 0x13, 0x17, 0x70, 0xdd, 0x46,
  25753. 0xf4, 0x08, 0x67, 0xd6, 0xfd, 0x5d, 0x50, 0x55,
  25754. 0xde, 0x43, 0x54, 0x1f, 0x8c, 0x5e, 0x35, 0xab,
  25755. 0xbc, 0xd0, 0x01, 0xb3, 0x2a, 0x89, 0xf7, 0xd2,
  25756. 0x15, 0x1f, 0x76, 0x47, 0xf1, 0x1d, 0x8c, 0xa2,
  25757. 0xae, 0x27, 0x9f, 0xb8, 0x42, 0xd6, 0x07, 0x21,
  25758. 0x7f, 0xce, 0x6e, 0x04, 0x2f, 0x68, 0x15, 0xea,
  25759. 0x00, 0x0c, 0x85, 0x74, 0x1d, 0xe5, 0xc8, 0xda,
  25760. 0x11, 0x44, 0xa6, 0xa1, 0xab, 0xa7, 0xf9, 0x6d,
  25761. 0xe4, 0x25, 0x05, 0xd7, 0xa7, 0x29, 0x85, 0x24,
  25762. 0xfd, 0xa5, 0x38, 0xfc, 0xcb, 0xbb, 0x75, 0x4f,
  25763. 0x57, 0x8c, 0x1c, 0xad, 0x10, 0xd5, 0x4d, 0x0d,
  25764. 0x54, 0x28, 0x40, 0x7e, 0x85, 0xdc, 0xbc, 0x98,
  25765. 0xa4, 0x91, 0x55, 0xc1, 0x37, 0x64, 0xe6, 0x6c,
  25766. 0x3c, 0x00
  25767. };
  25768. WOLFSSL_SMALL_STACK_STATIC const byte msgCtx[] = {
  25769. 0x03
  25770. };
  25771. WOLFSSL_SMALL_STACK_STATIC const byte contextCtx[] = {
  25772. 0x66,0x6f,0x6f
  25773. };
  25774. outlen = sizeof(out);
  25775. XMEMSET(out, 0, sizeof(out));
  25776. if (wc_ed448_init_ex(&key, HEAP_HINT, devId) != 0)
  25777. return -11500;
  25778. if (wc_ed448_import_private_key(sKeyCtx, ED448_KEY_SIZE, pKeyCtx,
  25779. sizeof(pKeyCtx), &key) != 0)
  25780. return -11501;
  25781. if (wc_ed448_sign_msg(msgCtx, sizeof(msgCtx), out, &outlen, &key,
  25782. contextCtx, sizeof(contextCtx)) != 0)
  25783. return -11502;
  25784. if (XMEMCMP(out, sigCtx, sizeof(sigCtx)))
  25785. return -11503;
  25786. #if defined(HAVE_ED448_VERIFY)
  25787. /* test verify on good msg */
  25788. if (wc_ed448_verify_msg(out, outlen, msgCtx, sizeof(msgCtx), &verify, &key,
  25789. contextCtx, sizeof(contextCtx)) != 0 || verify != 1)
  25790. return -11504;
  25791. #endif
  25792. wc_ed448_free(&key);
  25793. return 0;
  25794. }
  25795. static int ed448ph_test(void)
  25796. {
  25797. byte out[ED448_SIG_SIZE];
  25798. word32 outlen;
  25799. #ifdef HAVE_ED448_VERIFY
  25800. int verify;
  25801. #endif /* HAVE_ED448_VERIFY */
  25802. ed448_key key;
  25803. WOLFSSL_SMALL_STACK_STATIC const byte sKeyPh[] = {
  25804. 0x83, 0x3f, 0xe6, 0x24, 0x09, 0x23, 0x7b, 0x9d,
  25805. 0x62, 0xec, 0x77, 0x58, 0x75, 0x20, 0x91, 0x1e,
  25806. 0x9a, 0x75, 0x9c, 0xec, 0x1d, 0x19, 0x75, 0x5b,
  25807. 0x7d, 0xa9, 0x01, 0xb9, 0x6d, 0xca, 0x3d, 0x42,
  25808. 0xef, 0x78, 0x22, 0xe0, 0xd5, 0x10, 0x41, 0x27,
  25809. 0xdc, 0x05, 0xd6, 0xdb, 0xef, 0xde, 0x69, 0xe3,
  25810. 0xab, 0x2c, 0xec, 0x7c, 0x86, 0x7c, 0x6e, 0x2c,
  25811. 0x49
  25812. };
  25813. WOLFSSL_SMALL_STACK_STATIC const byte pKeyPh[] = {
  25814. 0x25, 0x9b, 0x71, 0xc1, 0x9f, 0x83, 0xef, 0x77,
  25815. 0xa7, 0xab, 0xd2, 0x65, 0x24, 0xcb, 0xdb, 0x31,
  25816. 0x61, 0xb5, 0x90, 0xa4, 0x8f, 0x7d, 0x17, 0xde,
  25817. 0x3e, 0xe0, 0xba, 0x9c, 0x52, 0xbe, 0xb7, 0x43,
  25818. 0xc0, 0x94, 0x28, 0xa1, 0x31, 0xd6, 0xb1, 0xb5,
  25819. 0x73, 0x03, 0xd9, 0x0d, 0x81, 0x32, 0xc2, 0x76,
  25820. 0xd5, 0xed, 0x3d, 0x5d, 0x01, 0xc0, 0xf5, 0x38,
  25821. 0x80
  25822. };
  25823. WOLFSSL_SMALL_STACK_STATIC const byte sigPh1[] = {
  25824. 0x82, 0x2f, 0x69, 0x01, 0xf7, 0x48, 0x0f, 0x3d,
  25825. 0x5f, 0x56, 0x2c, 0x59, 0x29, 0x94, 0xd9, 0x69,
  25826. 0x36, 0x02, 0x87, 0x56, 0x14, 0x48, 0x32, 0x56,
  25827. 0x50, 0x56, 0x00, 0xbb, 0xc2, 0x81, 0xae, 0x38,
  25828. 0x1f, 0x54, 0xd6, 0xbc, 0xe2, 0xea, 0x91, 0x15,
  25829. 0x74, 0x93, 0x2f, 0x52, 0xa4, 0xe6, 0xca, 0xdd,
  25830. 0x78, 0x76, 0x93, 0x75, 0xec, 0x3f, 0xfd, 0x1b,
  25831. 0x80, 0x1a, 0x0d, 0x9b, 0x3f, 0x40, 0x30, 0xcd,
  25832. 0x43, 0x39, 0x64, 0xb6, 0x45, 0x7e, 0xa3, 0x94,
  25833. 0x76, 0x51, 0x12, 0x14, 0xf9, 0x74, 0x69, 0xb5,
  25834. 0x7d, 0xd3, 0x2d, 0xbc, 0x56, 0x0a, 0x9a, 0x94,
  25835. 0xd0, 0x0b, 0xff, 0x07, 0x62, 0x04, 0x64, 0xa3,
  25836. 0xad, 0x20, 0x3d, 0xf7, 0xdc, 0x7c, 0xe3, 0x60,
  25837. 0xc3, 0xcd, 0x36, 0x96, 0xd9, 0xd9, 0xfa, 0xb9,
  25838. 0x0f, 0x00
  25839. };
  25840. WOLFSSL_SMALL_STACK_STATIC const byte sigPh2[] = {
  25841. 0xc3, 0x22, 0x99, 0xd4, 0x6e, 0xc8, 0xff, 0x02,
  25842. 0xb5, 0x45, 0x40, 0x98, 0x28, 0x14, 0xdc, 0xe9,
  25843. 0xa0, 0x58, 0x12, 0xf8, 0x19, 0x62, 0xb6, 0x49,
  25844. 0xd5, 0x28, 0x09, 0x59, 0x16, 0xa2, 0xaa, 0x48,
  25845. 0x10, 0x65, 0xb1, 0x58, 0x04, 0x23, 0xef, 0x92,
  25846. 0x7e, 0xcf, 0x0a, 0xf5, 0x88, 0x8f, 0x90, 0xda,
  25847. 0x0f, 0x6a, 0x9a, 0x85, 0xad, 0x5d, 0xc3, 0xf2,
  25848. 0x80, 0xd9, 0x12, 0x24, 0xba, 0x99, 0x11, 0xa3,
  25849. 0x65, 0x3d, 0x00, 0xe4, 0x84, 0xe2, 0xce, 0x23,
  25850. 0x25, 0x21, 0x48, 0x1c, 0x86, 0x58, 0xdf, 0x30,
  25851. 0x4b, 0xb7, 0x74, 0x5a, 0x73, 0x51, 0x4c, 0xdb,
  25852. 0x9b, 0xf3, 0xe1, 0x57, 0x84, 0xab, 0x71, 0x28,
  25853. 0x4f, 0x8d, 0x07, 0x04, 0xa6, 0x08, 0xc5, 0x4a,
  25854. 0x6b, 0x62, 0xd9, 0x7b, 0xeb, 0x51, 0x1d, 0x13,
  25855. 0x21, 0x00
  25856. };
  25857. WOLFSSL_SMALL_STACK_STATIC const byte msgPh[] = {
  25858. 0x61,0x62,0x63
  25859. };
  25860. /* SHA-512 hash of msgPh */
  25861. WOLFSSL_SMALL_STACK_STATIC const byte hashPh[] = {
  25862. 0x48, 0x33, 0x66, 0x60, 0x13, 0x60, 0xa8, 0x77,
  25863. 0x1c, 0x68, 0x63, 0x08, 0x0c, 0xc4, 0x11, 0x4d,
  25864. 0x8d, 0xb4, 0x45, 0x30, 0xf8, 0xf1, 0xe1, 0xee,
  25865. 0x4f, 0x94, 0xea, 0x37, 0xe7, 0x8b, 0x57, 0x39,
  25866. 0xd5, 0xa1, 0x5b, 0xef, 0x18, 0x6a, 0x53, 0x86,
  25867. 0xc7, 0x57, 0x44, 0xc0, 0x52, 0x7e, 0x1f, 0xaa,
  25868. 0x9f, 0x87, 0x26, 0xe4, 0x62, 0xa1, 0x2a, 0x4f,
  25869. 0xeb, 0x06, 0xbd, 0x88, 0x01, 0xe7, 0x51, 0xe4
  25870. };
  25871. WOLFSSL_SMALL_STACK_STATIC const byte contextPh2[] = {
  25872. 0x66,0x6f,0x6f
  25873. };
  25874. outlen = sizeof(out);
  25875. XMEMSET(out, 0, sizeof(out));
  25876. if (wc_ed448_init_ex(&key, HEAP_HINT, devId) != 0)
  25877. return -11600;
  25878. if (wc_ed448_import_private_key(sKeyPh, ED448_KEY_SIZE, pKeyPh,
  25879. sizeof(pKeyPh), &key) != 0) {
  25880. return -11601;
  25881. }
  25882. if (wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key, NULL,
  25883. 0) != 0) {
  25884. return -11602;
  25885. }
  25886. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  25887. return -11603;
  25888. #if defined(HAVE_ED448_VERIFY)
  25889. /* test verify on good msg */
  25890. if (wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  25891. NULL, 0) != 0 || verify != 1) {
  25892. return -11604;
  25893. }
  25894. #endif
  25895. if (wc_ed448ph_sign_msg(msgPh, sizeof(msgPh), out, &outlen, &key,
  25896. contextPh2, sizeof(contextPh2)) != 0) {
  25897. return -11605;
  25898. }
  25899. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  25900. return -11606;
  25901. #if defined(HAVE_ED448_VERIFY)
  25902. /* test verify on good msg */
  25903. if (wc_ed448ph_verify_msg(out, outlen, msgPh, sizeof(msgPh), &verify, &key,
  25904. contextPh2, sizeof(contextPh2)) != 0 ||
  25905. verify != 1) {
  25906. return -11607;
  25907. }
  25908. #endif
  25909. if (wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key, NULL,
  25910. 0) != 0) {
  25911. return -11608;
  25912. }
  25913. if (XMEMCMP(out, sigPh1, sizeof(sigPh1)))
  25914. return -11609;
  25915. #if defined(HAVE_ED448_VERIFY)
  25916. if (wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  25917. &key, NULL, 0) != 0 || verify != 1) {
  25918. return -11610;
  25919. }
  25920. #endif
  25921. if (wc_ed448ph_sign_hash(hashPh, sizeof(hashPh), out, &outlen, &key,
  25922. contextPh2, sizeof(contextPh2)) != 0) {
  25923. return -11611;
  25924. }
  25925. if (XMEMCMP(out, sigPh2, sizeof(sigPh2)))
  25926. return -11612;
  25927. #if defined(HAVE_ED448_VERIFY)
  25928. if (wc_ed448ph_verify_hash(out, outlen, hashPh, sizeof(hashPh), &verify,
  25929. &key, contextPh2, sizeof(contextPh2)) != 0 ||
  25930. verify != 1) {
  25931. return -11613;
  25932. }
  25933. #endif
  25934. wc_ed448_free(&key);
  25935. return 0;
  25936. }
  25937. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  25938. WOLFSSL_TEST_SUBROUTINE int ed448_test(void)
  25939. {
  25940. int ret;
  25941. WC_RNG rng;
  25942. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  25943. defined(HAVE_ED448_KEY_IMPORT)
  25944. byte out[ED448_SIG_SIZE];
  25945. int i;
  25946. word32 outlen;
  25947. #ifdef HAVE_ED448_VERIFY
  25948. #ifdef WOLFSSL_ED448_STREAMING_VERIFY
  25949. int j;
  25950. #endif /* WOLFSSL_ED448_STREAMING_VERIFY */
  25951. int verify;
  25952. #endif /* HAVE_ED448_VERIFY */
  25953. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  25954. word32 keySz, sigSz;
  25955. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  25956. ed448_key *key = NULL;
  25957. ed448_key *key2 = NULL;
  25958. #else
  25959. ed448_key key[1];
  25960. ed448_key key2[1];
  25961. #endif
  25962. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) && \
  25963. defined(HAVE_ED448_KEY_IMPORT)
  25964. /* test vectors from
  25965. https://tools.ietf.org/html/rfc8032
  25966. */
  25967. WOLFSSL_SMALL_STACK_STATIC const byte sKey1[] = {
  25968. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  25969. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  25970. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  25971. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  25972. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  25973. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  25974. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  25975. 0x5b
  25976. };
  25977. WOLFSSL_SMALL_STACK_STATIC const byte sKey2[] = {
  25978. 0xc4, 0xea, 0xb0, 0x5d, 0x35, 0x70, 0x07, 0xc6,
  25979. 0x32, 0xf3, 0xdb, 0xb4, 0x84, 0x89, 0x92, 0x4d,
  25980. 0x55, 0x2b, 0x08, 0xfe, 0x0c, 0x35, 0x3a, 0x0d,
  25981. 0x4a, 0x1f, 0x00, 0xac, 0xda, 0x2c, 0x46, 0x3a,
  25982. 0xfb, 0xea, 0x67, 0xc5, 0xe8, 0xd2, 0x87, 0x7c,
  25983. 0x5e, 0x3b, 0xc3, 0x97, 0xa6, 0x59, 0x94, 0x9e,
  25984. 0xf8, 0x02, 0x1e, 0x95, 0x4e, 0x0a, 0x12, 0x27,
  25985. 0x4e
  25986. };
  25987. WOLFSSL_SMALL_STACK_STATIC const byte sKey3[] = {
  25988. 0x25, 0x8c, 0xdd, 0x4a, 0xda, 0x32, 0xed, 0x9c,
  25989. 0x9f, 0xf5, 0x4e, 0x63, 0x75, 0x6a, 0xe5, 0x82,
  25990. 0xfb, 0x8f, 0xab, 0x2a, 0xc7, 0x21, 0xf2, 0xc8,
  25991. 0xe6, 0x76, 0xa7, 0x27, 0x68, 0x51, 0x3d, 0x93,
  25992. 0x9f, 0x63, 0xdd, 0xdb, 0x55, 0x60, 0x91, 0x33,
  25993. 0xf2, 0x9a, 0xdf, 0x86, 0xec, 0x99, 0x29, 0xdc,
  25994. 0xcb, 0x52, 0xc1, 0xc5, 0xfd, 0x2f, 0xf7, 0xe2,
  25995. 0x1b
  25996. };
  25997. /* uncompressed test */
  25998. WOLFSSL_SMALL_STACK_STATIC const byte sKey4[] = {
  25999. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  26000. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  26001. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  26002. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  26003. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  26004. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  26005. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  26006. 0x5b
  26007. };
  26008. /* compressed prefix test */
  26009. WOLFSSL_SMALL_STACK_STATIC const byte sKey5[] = {
  26010. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  26011. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  26012. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  26013. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  26014. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  26015. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  26016. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  26017. 0x5b
  26018. };
  26019. WOLFSSL_SMALL_STACK_STATIC const byte sKey6[] = {
  26020. 0x87, 0x2d, 0x09, 0x37, 0x80, 0xf5, 0xd3, 0x73,
  26021. 0x0d, 0xf7, 0xc2, 0x12, 0x66, 0x4b, 0x37, 0xb8,
  26022. 0xa0, 0xf2, 0x4f, 0x56, 0x81, 0x0d, 0xaa, 0x83,
  26023. 0x82, 0xcd, 0x4f, 0xa3, 0xf7, 0x76, 0x34, 0xec,
  26024. 0x44, 0xdc, 0x54, 0xf1, 0xc2, 0xed, 0x9b, 0xea,
  26025. 0x86, 0xfa, 0xfb, 0x76, 0x32, 0xd8, 0xbe, 0x19,
  26026. 0x9e, 0xa1, 0x65, 0xf5, 0xad, 0x55, 0xdd, 0x9c,
  26027. 0xe8
  26028. };
  26029. WOLFSSL_SMALL_STACK_STATIC const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  26030. WOLFSSL_SMALL_STACK_STATIC const byte pKey1[] = {
  26031. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  26032. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  26033. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  26034. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  26035. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  26036. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  26037. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  26038. 0x80
  26039. };
  26040. WOLFSSL_SMALL_STACK_STATIC const byte pKey2[] = {
  26041. 0x43, 0xba, 0x28, 0xf4, 0x30, 0xcd, 0xff, 0x45,
  26042. 0x6a, 0xe5, 0x31, 0x54, 0x5f, 0x7e, 0xcd, 0x0a,
  26043. 0xc8, 0x34, 0xa5, 0x5d, 0x93, 0x58, 0xc0, 0x37,
  26044. 0x2b, 0xfa, 0x0c, 0x6c, 0x67, 0x98, 0xc0, 0x86,
  26045. 0x6a, 0xea, 0x01, 0xeb, 0x00, 0x74, 0x28, 0x02,
  26046. 0xb8, 0x43, 0x8e, 0xa4, 0xcb, 0x82, 0x16, 0x9c,
  26047. 0x23, 0x51, 0x60, 0x62, 0x7b, 0x4c, 0x3a, 0x94,
  26048. 0x80
  26049. };
  26050. WOLFSSL_SMALL_STACK_STATIC const byte pKey3[] = {
  26051. 0x3b, 0xa1, 0x6d, 0xa0, 0xc6, 0xf2, 0xcc, 0x1f,
  26052. 0x30, 0x18, 0x77, 0x40, 0x75, 0x6f, 0x5e, 0x79,
  26053. 0x8d, 0x6b, 0xc5, 0xfc, 0x01, 0x5d, 0x7c, 0x63,
  26054. 0xcc, 0x95, 0x10, 0xee, 0x3f, 0xd4, 0x4a, 0xdc,
  26055. 0x24, 0xd8, 0xe9, 0x68, 0xb6, 0xe4, 0x6e, 0x6f,
  26056. 0x94, 0xd1, 0x9b, 0x94, 0x53, 0x61, 0x72, 0x6b,
  26057. 0xd7, 0x5e, 0x14, 0x9e, 0xf0, 0x98, 0x17, 0xf5,
  26058. 0x80
  26059. };
  26060. /* uncompressed test */
  26061. WOLFSSL_SMALL_STACK_STATIC const byte pKey4[] = {
  26062. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  26063. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  26064. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  26065. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  26066. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  26067. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  26068. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  26069. 0x80
  26070. };
  26071. /* compressed prefix */
  26072. WOLFSSL_SMALL_STACK_STATIC const byte pKey5[] = {
  26073. 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd,
  26074. 0x2c, 0xe7, 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a,
  26075. 0x1d, 0xa1, 0x34, 0x24, 0x85, 0xa7, 0x0e, 0x1f,
  26076. 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, 0x67, 0x78,
  26077. 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06,
  26078. 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c,
  26079. 0xd1, 0xfa, 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61,
  26080. 0x80
  26081. };
  26082. WOLFSSL_SMALL_STACK_STATIC const byte pKey6[] = {
  26083. 0xa8, 0x1b, 0x2e, 0x8a, 0x70, 0xa5, 0xac, 0x94,
  26084. 0xff, 0xdb, 0xcc, 0x9b, 0xad, 0xfc, 0x3f, 0xeb,
  26085. 0x08, 0x01, 0xf2, 0x58, 0x57, 0x8b, 0xb1, 0x14,
  26086. 0xad, 0x44, 0xec, 0xe1, 0xec, 0x0e, 0x79, 0x9d,
  26087. 0xa0, 0x8e, 0xff, 0xb8, 0x1c, 0x5d, 0x68, 0x5c,
  26088. 0x0c, 0x56, 0xf6, 0x4e, 0xec, 0xae, 0xf8, 0xcd,
  26089. 0xf1, 0x1c, 0xc3, 0x87, 0x37, 0x83, 0x8c, 0xf4,
  26090. 0x00
  26091. };
  26092. WOLFSSL_SMALL_STACK_STATIC const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  26093. WOLFSSL_SMALL_STACK_STATIC const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  26094. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  26095. WOLFSSL_SMALL_STACK_STATIC const byte sig1[] = {
  26096. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  26097. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  26098. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  26099. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  26100. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  26101. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  26102. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  26103. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  26104. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  26105. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  26106. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  26107. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  26108. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  26109. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  26110. 0x26, 0x00
  26111. };
  26112. WOLFSSL_SMALL_STACK_STATIC const byte sig2[] = {
  26113. 0x26, 0xb8, 0xf9, 0x17, 0x27, 0xbd, 0x62, 0x89,
  26114. 0x7a, 0xf1, 0x5e, 0x41, 0xeb, 0x43, 0xc3, 0x77,
  26115. 0xef, 0xb9, 0xc6, 0x10, 0xd4, 0x8f, 0x23, 0x35,
  26116. 0xcb, 0x0b, 0xd0, 0x08, 0x78, 0x10, 0xf4, 0x35,
  26117. 0x25, 0x41, 0xb1, 0x43, 0xc4, 0xb9, 0x81, 0xb7,
  26118. 0xe1, 0x8f, 0x62, 0xde, 0x8c, 0xcd, 0xf6, 0x33,
  26119. 0xfc, 0x1b, 0xf0, 0x37, 0xab, 0x7c, 0xd7, 0x79,
  26120. 0x80, 0x5e, 0x0d, 0xbc, 0xc0, 0xaa, 0xe1, 0xcb,
  26121. 0xce, 0xe1, 0xaf, 0xb2, 0xe0, 0x27, 0xdf, 0x36,
  26122. 0xbc, 0x04, 0xdc, 0xec, 0xbf, 0x15, 0x43, 0x36,
  26123. 0xc1, 0x9f, 0x0a, 0xf7, 0xe0, 0xa6, 0x47, 0x29,
  26124. 0x05, 0xe7, 0x99, 0xf1, 0x95, 0x3d, 0x2a, 0x0f,
  26125. 0xf3, 0x34, 0x8a, 0xb2, 0x1a, 0xa4, 0xad, 0xaf,
  26126. 0xd1, 0xd2, 0x34, 0x44, 0x1c, 0xf8, 0x07, 0xc0,
  26127. 0x3a, 0x00
  26128. };
  26129. WOLFSSL_SMALL_STACK_STATIC const byte sig3[] = {
  26130. 0x7e, 0xee, 0xab, 0x7c, 0x4e, 0x50, 0xfb, 0x79,
  26131. 0x9b, 0x41, 0x8e, 0xe5, 0xe3, 0x19, 0x7f, 0xf6,
  26132. 0xbf, 0x15, 0xd4, 0x3a, 0x14, 0xc3, 0x43, 0x89,
  26133. 0xb5, 0x9d, 0xd1, 0xa7, 0xb1, 0xb8, 0x5b, 0x4a,
  26134. 0xe9, 0x04, 0x38, 0xac, 0xa6, 0x34, 0xbe, 0xa4,
  26135. 0x5e, 0x3a, 0x26, 0x95, 0xf1, 0x27, 0x0f, 0x07,
  26136. 0xfd, 0xcd, 0xf7, 0xc6, 0x2b, 0x8e, 0xfe, 0xaf,
  26137. 0x00, 0xb4, 0x5c, 0x2c, 0x96, 0xba, 0x45, 0x7e,
  26138. 0xb1, 0xa8, 0xbf, 0x07, 0x5a, 0x3d, 0xb2, 0x8e,
  26139. 0x5c, 0x24, 0xf6, 0xb9, 0x23, 0xed, 0x4a, 0xd7,
  26140. 0x47, 0xc3, 0xc9, 0xe0, 0x3c, 0x70, 0x79, 0xef,
  26141. 0xb8, 0x7c, 0xb1, 0x10, 0xd3, 0xa9, 0x98, 0x61,
  26142. 0xe7, 0x20, 0x03, 0xcb, 0xae, 0x6d, 0x6b, 0x8b,
  26143. 0x82, 0x7e, 0x4e, 0x6c, 0x14, 0x30, 0x64, 0xff,
  26144. 0x3c, 0x00
  26145. };
  26146. /* uncompressed test */
  26147. WOLFSSL_SMALL_STACK_STATIC const byte sig4[] = {
  26148. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  26149. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  26150. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  26151. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  26152. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  26153. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  26154. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  26155. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  26156. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  26157. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  26158. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  26159. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  26160. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  26161. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  26162. 0x26, 0x00
  26163. };
  26164. /* compressed prefix */
  26165. WOLFSSL_SMALL_STACK_STATIC const byte sig5[] = {
  26166. 0x53, 0x3a, 0x37, 0xf6, 0xbb, 0xe4, 0x57, 0x25,
  26167. 0x1f, 0x02, 0x3c, 0x0d, 0x88, 0xf9, 0x76, 0xae,
  26168. 0x2d, 0xfb, 0x50, 0x4a, 0x84, 0x3e, 0x34, 0xd2,
  26169. 0x07, 0x4f, 0xd8, 0x23, 0xd4, 0x1a, 0x59, 0x1f,
  26170. 0x2b, 0x23, 0x3f, 0x03, 0x4f, 0x62, 0x82, 0x81,
  26171. 0xf2, 0xfd, 0x7a, 0x22, 0xdd, 0xd4, 0x7d, 0x78,
  26172. 0x28, 0xc5, 0x9b, 0xd0, 0xa2, 0x1b, 0xfd, 0x39,
  26173. 0x80, 0xff, 0x0d, 0x20, 0x28, 0xd4, 0xb1, 0x8a,
  26174. 0x9d, 0xf6, 0x3e, 0x00, 0x6c, 0x5d, 0x1c, 0x2d,
  26175. 0x34, 0x5b, 0x92, 0x5d, 0x8d, 0xc0, 0x0b, 0x41,
  26176. 0x04, 0x85, 0x2d, 0xb9, 0x9a, 0xc5, 0xc7, 0xcd,
  26177. 0xda, 0x85, 0x30, 0xa1, 0x13, 0xa0, 0xf4, 0xdb,
  26178. 0xb6, 0x11, 0x49, 0xf0, 0x5a, 0x73, 0x63, 0x26,
  26179. 0x8c, 0x71, 0xd9, 0x58, 0x08, 0xff, 0x2e, 0x65,
  26180. 0x26, 0x00
  26181. };
  26182. WOLFSSL_SMALL_STACK_STATIC const byte sig6[] = {
  26183. 0xe3, 0x01, 0x34, 0x5a, 0x41, 0xa3, 0x9a, 0x4d,
  26184. 0x72, 0xff, 0xf8, 0xdf, 0x69, 0xc9, 0x80, 0x75,
  26185. 0xa0, 0xcc, 0x08, 0x2b, 0x80, 0x2f, 0xc9, 0xb2,
  26186. 0xb6, 0xbc, 0x50, 0x3f, 0x92, 0x6b, 0x65, 0xbd,
  26187. 0xdf, 0x7f, 0x4c, 0x8f, 0x1c, 0xb4, 0x9f, 0x63,
  26188. 0x96, 0xaf, 0xc8, 0xa7, 0x0a, 0xbe, 0x6d, 0x8a,
  26189. 0xef, 0x0d, 0xb4, 0x78, 0xd4, 0xc6, 0xb2, 0x97,
  26190. 0x00, 0x76, 0xc6, 0xa0, 0x48, 0x4f, 0xe7, 0x6d,
  26191. 0x76, 0xb3, 0xa9, 0x76, 0x25, 0xd7, 0x9f, 0x1c,
  26192. 0xe2, 0x40, 0xe7, 0xc5, 0x76, 0x75, 0x0d, 0x29,
  26193. 0x55, 0x28, 0x28, 0x6f, 0x71, 0x9b, 0x41, 0x3d,
  26194. 0xe9, 0xad, 0xa3, 0xe8, 0xeb, 0x78, 0xed, 0x57,
  26195. 0x36, 0x03, 0xce, 0x30, 0xd8, 0xbb, 0x76, 0x17,
  26196. 0x85, 0xdc, 0x30, 0xdb, 0xc3, 0x20, 0x86, 0x9e,
  26197. 0x1a, 0x00
  26198. };
  26199. WOLFSSL_SMALL_STACK_STATIC const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  26200. #define SIGSZ sizeof(sig1)
  26201. PEDANTIC_EXTENSION WOLFSSL_SMALL_STACK_STATIC const byte msg1[] = { };
  26202. WOLFSSL_SMALL_STACK_STATIC const byte msg2[] = { 0x03 };
  26203. WOLFSSL_SMALL_STACK_STATIC const byte msg3[] = { 0x64, 0xa6, 0x5f, 0x3c, 0xde, 0xdc, 0xdd,
  26204. 0x66, 0x81, 0x1e, 0x29, 0x15 };
  26205. /* test of a 1023 byte long message */
  26206. WOLFSSL_SMALL_STACK_STATIC const byte msg4[] = {
  26207. 0x6d, 0xdf, 0x80, 0x2e, 0x1a, 0xae, 0x49, 0x86,
  26208. 0x93, 0x5f, 0x7f, 0x98, 0x1b, 0xa3, 0xf0, 0x35,
  26209. 0x1d, 0x62, 0x73, 0xc0, 0xa0, 0xc2, 0x2c, 0x9c,
  26210. 0x0e, 0x83, 0x39, 0x16, 0x8e, 0x67, 0x54, 0x12,
  26211. 0xa3, 0xde, 0xbf, 0xaf, 0x43, 0x5e, 0xd6, 0x51,
  26212. 0x55, 0x80, 0x07, 0xdb, 0x43, 0x84, 0xb6, 0x50,
  26213. 0xfc, 0xc0, 0x7e, 0x3b, 0x58, 0x6a, 0x27, 0xa4,
  26214. 0xf7, 0xa0, 0x0a, 0xc8, 0xa6, 0xfe, 0xc2, 0xcd,
  26215. 0x86, 0xae, 0x4b, 0xf1, 0x57, 0x0c, 0x41, 0xe6,
  26216. 0xa4, 0x0c, 0x93, 0x1d, 0xb2, 0x7b, 0x2f, 0xaa,
  26217. 0x15, 0xa8, 0xce, 0xdd, 0x52, 0xcf, 0xf7, 0x36,
  26218. 0x2c, 0x4e, 0x6e, 0x23, 0xda, 0xec, 0x0f, 0xbc,
  26219. 0x3a, 0x79, 0xb6, 0x80, 0x6e, 0x31, 0x6e, 0xfc,
  26220. 0xc7, 0xb6, 0x81, 0x19, 0xbf, 0x46, 0xbc, 0x76,
  26221. 0xa2, 0x60, 0x67, 0xa5, 0x3f, 0x29, 0x6d, 0xaf,
  26222. 0xdb, 0xdc, 0x11, 0xc7, 0x7f, 0x77, 0x77, 0xe9,
  26223. 0x72, 0x66, 0x0c, 0xf4, 0xb6, 0xa9, 0xb3, 0x69,
  26224. 0xa6, 0x66, 0x5f, 0x02, 0xe0, 0xcc, 0x9b, 0x6e,
  26225. 0xdf, 0xad, 0x13, 0x6b, 0x4f, 0xab, 0xe7, 0x23,
  26226. 0xd2, 0x81, 0x3d, 0xb3, 0x13, 0x6c, 0xfd, 0xe9,
  26227. 0xb6, 0xd0, 0x44, 0x32, 0x2f, 0xee, 0x29, 0x47,
  26228. 0x95, 0x2e, 0x03, 0x1b, 0x73, 0xab, 0x5c, 0x60,
  26229. 0x33, 0x49, 0xb3, 0x07, 0xbd, 0xc2, 0x7b, 0xc6,
  26230. 0xcb, 0x8b, 0x8b, 0xbd, 0x7b, 0xd3, 0x23, 0x21,
  26231. 0x9b, 0x80, 0x33, 0xa5, 0x81, 0xb5, 0x9e, 0xad,
  26232. 0xeb, 0xb0, 0x9b, 0x3c, 0x4f, 0x3d, 0x22, 0x77,
  26233. 0xd4, 0xf0, 0x34, 0x36, 0x24, 0xac, 0xc8, 0x17,
  26234. 0x80, 0x47, 0x28, 0xb2, 0x5a, 0xb7, 0x97, 0x17,
  26235. 0x2b, 0x4c, 0x5c, 0x21, 0xa2, 0x2f, 0x9c, 0x78,
  26236. 0x39, 0xd6, 0x43, 0x00, 0x23, 0x2e, 0xb6, 0x6e,
  26237. 0x53, 0xf3, 0x1c, 0x72, 0x3f, 0xa3, 0x7f, 0xe3,
  26238. 0x87, 0xc7, 0xd3, 0xe5, 0x0b, 0xdf, 0x98, 0x13,
  26239. 0xa3, 0x0e, 0x5b, 0xb1, 0x2c, 0xf4, 0xcd, 0x93,
  26240. 0x0c, 0x40, 0xcf, 0xb4, 0xe1, 0xfc, 0x62, 0x25,
  26241. 0x92, 0xa4, 0x95, 0x88, 0x79, 0x44, 0x94, 0xd5,
  26242. 0x6d, 0x24, 0xea, 0x4b, 0x40, 0xc8, 0x9f, 0xc0,
  26243. 0x59, 0x6c, 0xc9, 0xeb, 0xb9, 0x61, 0xc8, 0xcb,
  26244. 0x10, 0xad, 0xde, 0x97, 0x6a, 0x5d, 0x60, 0x2b,
  26245. 0x1c, 0x3f, 0x85, 0xb9, 0xb9, 0xa0, 0x01, 0xed,
  26246. 0x3c, 0x6a, 0x4d, 0x3b, 0x14, 0x37, 0xf5, 0x20,
  26247. 0x96, 0xcd, 0x19, 0x56, 0xd0, 0x42, 0xa5, 0x97,
  26248. 0xd5, 0x61, 0xa5, 0x96, 0xec, 0xd3, 0xd1, 0x73,
  26249. 0x5a, 0x8d, 0x57, 0x0e, 0xa0, 0xec, 0x27, 0x22,
  26250. 0x5a, 0x2c, 0x4a, 0xaf, 0xf2, 0x63, 0x06, 0xd1,
  26251. 0x52, 0x6c, 0x1a, 0xf3, 0xca, 0x6d, 0x9c, 0xf5,
  26252. 0xa2, 0xc9, 0x8f, 0x47, 0xe1, 0xc4, 0x6d, 0xb9,
  26253. 0xa3, 0x32, 0x34, 0xcf, 0xd4, 0xd8, 0x1f, 0x2c,
  26254. 0x98, 0x53, 0x8a, 0x09, 0xeb, 0xe7, 0x69, 0x98,
  26255. 0xd0, 0xd8, 0xfd, 0x25, 0x99, 0x7c, 0x7d, 0x25,
  26256. 0x5c, 0x6d, 0x66, 0xec, 0xe6, 0xfa, 0x56, 0xf1,
  26257. 0x11, 0x44, 0x95, 0x0f, 0x02, 0x77, 0x95, 0xe6,
  26258. 0x53, 0x00, 0x8f, 0x4b, 0xd7, 0xca, 0x2d, 0xee,
  26259. 0x85, 0xd8, 0xe9, 0x0f, 0x3d, 0xc3, 0x15, 0x13,
  26260. 0x0c, 0xe2, 0xa0, 0x03, 0x75, 0xa3, 0x18, 0xc7,
  26261. 0xc3, 0xd9, 0x7b, 0xe2, 0xc8, 0xce, 0x5b, 0x6d,
  26262. 0xb4, 0x1a, 0x62, 0x54, 0xff, 0x26, 0x4f, 0xa6,
  26263. 0x15, 0x5b, 0xae, 0xe3, 0xb0, 0x77, 0x3c, 0x0f,
  26264. 0x49, 0x7c, 0x57, 0x3f, 0x19, 0xbb, 0x4f, 0x42,
  26265. 0x40, 0x28, 0x1f, 0x0b, 0x1f, 0x4f, 0x7b, 0xe8,
  26266. 0x57, 0xa4, 0xe5, 0x9d, 0x41, 0x6c, 0x06, 0xb4,
  26267. 0xc5, 0x0f, 0xa0, 0x9e, 0x18, 0x10, 0xdd, 0xc6,
  26268. 0xb1, 0x46, 0x7b, 0xae, 0xac, 0x5a, 0x36, 0x68,
  26269. 0xd1, 0x1b, 0x6e, 0xca, 0xa9, 0x01, 0x44, 0x00,
  26270. 0x16, 0xf3, 0x89, 0xf8, 0x0a, 0xcc, 0x4d, 0xb9,
  26271. 0x77, 0x02, 0x5e, 0x7f, 0x59, 0x24, 0x38, 0x8c,
  26272. 0x7e, 0x34, 0x0a, 0x73, 0x2e, 0x55, 0x44, 0x40,
  26273. 0xe7, 0x65, 0x70, 0xf8, 0xdd, 0x71, 0xb7, 0xd6,
  26274. 0x40, 0xb3, 0x45, 0x0d, 0x1f, 0xd5, 0xf0, 0x41,
  26275. 0x0a, 0x18, 0xf9, 0xa3, 0x49, 0x4f, 0x70, 0x7c,
  26276. 0x71, 0x7b, 0x79, 0xb4, 0xbf, 0x75, 0xc9, 0x84,
  26277. 0x00, 0xb0, 0x96, 0xb2, 0x16, 0x53, 0xb5, 0xd2,
  26278. 0x17, 0xcf, 0x35, 0x65, 0xc9, 0x59, 0x74, 0x56,
  26279. 0xf7, 0x07, 0x03, 0x49, 0x7a, 0x07, 0x87, 0x63,
  26280. 0x82, 0x9b, 0xc0, 0x1b, 0xb1, 0xcb, 0xc8, 0xfa,
  26281. 0x04, 0xea, 0xdc, 0x9a, 0x6e, 0x3f, 0x66, 0x99,
  26282. 0x58, 0x7a, 0x9e, 0x75, 0xc9, 0x4e, 0x5b, 0xab,
  26283. 0x00, 0x36, 0xe0, 0xb2, 0xe7, 0x11, 0x39, 0x2c,
  26284. 0xff, 0x00, 0x47, 0xd0, 0xd6, 0xb0, 0x5b, 0xd2,
  26285. 0xa5, 0x88, 0xbc, 0x10, 0x97, 0x18, 0x95, 0x42,
  26286. 0x59, 0xf1, 0xd8, 0x66, 0x78, 0xa5, 0x79, 0xa3,
  26287. 0x12, 0x0f, 0x19, 0xcf, 0xb2, 0x96, 0x3f, 0x17,
  26288. 0x7a, 0xeb, 0x70, 0xf2, 0xd4, 0x84, 0x48, 0x26,
  26289. 0x26, 0x2e, 0x51, 0xb8, 0x02, 0x71, 0x27, 0x20,
  26290. 0x68, 0xef, 0x5b, 0x38, 0x56, 0xfa, 0x85, 0x35,
  26291. 0xaa, 0x2a, 0x88, 0xb2, 0xd4, 0x1f, 0x2a, 0x0e,
  26292. 0x2f, 0xda, 0x76, 0x24, 0xc2, 0x85, 0x02, 0x72,
  26293. 0xac, 0x4a, 0x2f, 0x56, 0x1f, 0x8f, 0x2f, 0x7a,
  26294. 0x31, 0x8b, 0xfd, 0x5c, 0xaf, 0x96, 0x96, 0x14,
  26295. 0x9e, 0x4a, 0xc8, 0x24, 0xad, 0x34, 0x60, 0x53,
  26296. 0x8f, 0xdc, 0x25, 0x42, 0x1b, 0xee, 0xc2, 0xcc,
  26297. 0x68, 0x18, 0x16, 0x2d, 0x06, 0xbb, 0xed, 0x0c,
  26298. 0x40, 0xa3, 0x87, 0x19, 0x23, 0x49, 0xdb, 0x67,
  26299. 0xa1, 0x18, 0xba, 0xda, 0x6c, 0xd5, 0xab, 0x01,
  26300. 0x40, 0xee, 0x27, 0x32, 0x04, 0xf6, 0x28, 0xaa,
  26301. 0xd1, 0xc1, 0x35, 0xf7, 0x70, 0x27, 0x9a, 0x65,
  26302. 0x1e, 0x24, 0xd8, 0xc1, 0x4d, 0x75, 0xa6, 0x05,
  26303. 0x9d, 0x76, 0xb9, 0x6a, 0x6f, 0xd8, 0x57, 0xde,
  26304. 0xf5, 0xe0, 0xb3, 0x54, 0xb2, 0x7a, 0xb9, 0x37,
  26305. 0xa5, 0x81, 0x5d, 0x16, 0xb5, 0xfa, 0xe4, 0x07,
  26306. 0xff, 0x18, 0x22, 0x2c, 0x6d, 0x1e, 0xd2, 0x63,
  26307. 0xbe, 0x68, 0xc9, 0x5f, 0x32, 0xd9, 0x08, 0xbd,
  26308. 0x89, 0x5c, 0xd7, 0x62, 0x07, 0xae, 0x72, 0x64,
  26309. 0x87, 0x56, 0x7f, 0x9a, 0x67, 0xda, 0xd7, 0x9a,
  26310. 0xbe, 0xc3, 0x16, 0xf6, 0x83, 0xb1, 0x7f, 0x2d,
  26311. 0x02, 0xbf, 0x07, 0xe0, 0xac, 0x8b, 0x5b, 0xc6,
  26312. 0x16, 0x2c, 0xf9, 0x46, 0x97, 0xb3, 0xc2, 0x7c,
  26313. 0xd1, 0xfe, 0xa4, 0x9b, 0x27, 0xf2, 0x3b, 0xa2,
  26314. 0x90, 0x18, 0x71, 0x96, 0x25, 0x06, 0x52, 0x0c,
  26315. 0x39, 0x2d, 0xa8, 0xb6, 0xad, 0x0d, 0x99, 0xf7,
  26316. 0x01, 0x3f, 0xbc, 0x06, 0xc2, 0xc1, 0x7a, 0x56,
  26317. 0x95, 0x00, 0xc8, 0xa7, 0x69, 0x64, 0x81, 0xc1,
  26318. 0xcd, 0x33, 0xe9, 0xb1, 0x4e, 0x40, 0xb8, 0x2e,
  26319. 0x79, 0xa5, 0xf5, 0xdb, 0x82, 0x57, 0x1b, 0xa9,
  26320. 0x7b, 0xae, 0x3a, 0xd3, 0xe0, 0x47, 0x95, 0x15,
  26321. 0xbb, 0x0e, 0x2b, 0x0f, 0x3b, 0xfc, 0xd1, 0xfd,
  26322. 0x33, 0x03, 0x4e, 0xfc, 0x62, 0x45, 0xed, 0xdd,
  26323. 0x7e, 0xe2, 0x08, 0x6d, 0xda, 0xe2, 0x60, 0x0d,
  26324. 0x8c, 0xa7, 0x3e, 0x21, 0x4e, 0x8c, 0x2b, 0x0b,
  26325. 0xdb, 0x2b, 0x04, 0x7c, 0x6a, 0x46, 0x4a, 0x56,
  26326. 0x2e, 0xd7, 0x7b, 0x73, 0xd2, 0xd8, 0x41, 0xc4,
  26327. 0xb3, 0x49, 0x73, 0x55, 0x12, 0x57, 0x71, 0x3b,
  26328. 0x75, 0x36, 0x32, 0xef, 0xba, 0x34, 0x81, 0x69,
  26329. 0xab, 0xc9, 0x0a, 0x68, 0xf4, 0x26, 0x11, 0xa4,
  26330. 0x01, 0x26, 0xd7, 0xcb, 0x21, 0xb5, 0x86, 0x95,
  26331. 0x56, 0x81, 0x86, 0xf7, 0xe5, 0x69, 0xd2, 0xff,
  26332. 0x0f, 0x9e, 0x74, 0x5d, 0x04, 0x87, 0xdd, 0x2e,
  26333. 0xb9, 0x97, 0xca, 0xfc, 0x5a, 0xbf, 0x9d, 0xd1,
  26334. 0x02, 0xe6, 0x2f, 0xf6, 0x6c, 0xba, 0x87
  26335. };
  26336. WOLFSSL_SMALL_STACK_STATIC const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  26337. WOLFSSL_SMALL_STACK_STATIC const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  26338. sizeof(msg2),
  26339. sizeof(msg3),
  26340. 0 /*sizeof(msg1)*/,
  26341. 0 /*sizeof(msg1)*/,
  26342. sizeof(msg4)
  26343. };
  26344. #ifndef NO_ASN
  26345. static const byte privateEd448[] = {
  26346. 0x30, 0x47, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06,
  26347. 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04, 0x39,
  26348. 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10,
  26349. 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf,
  26350. 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c, 0x9f,
  26351. 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48, 0xa3,
  26352. 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e,
  26353. 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f,
  26354. 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9,
  26355. 0x5b
  26356. };
  26357. static const byte publicEd448[] = {
  26358. 0x30, 0x43, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65,
  26359. 0x71, 0x03, 0x3a, 0x00, 0x5f, 0xd7, 0x44, 0x9b,
  26360. 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec,
  26361. 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  26362. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d,
  26363. 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76,
  26364. 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d,
  26365. 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe,
  26366. 0xaf, 0xe8, 0x25, 0x61, 0x80
  26367. };
  26368. static const byte privPubEd448[] = {
  26369. 0x30, 0x81, 0x82, 0x02, 0x01, 0x00, 0x30, 0x05,
  26370. 0x06, 0x03, 0x2b, 0x65, 0x71, 0x04, 0x3b, 0x04,
  26371. 0x39, 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d,
  26372. 0x10, 0xd6, 0x32, 0xbe, 0x89, 0xc8, 0x51, 0x3e,
  26373. 0xbf, 0x6c, 0x92, 0x9f, 0x34, 0xdd, 0xfa, 0x8c,
  26374. 0x9f, 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, 0x48,
  26375. 0xa3, 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04,
  26376. 0x4e, 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f,
  26377. 0x8f, 0x03, 0x2e, 0x75, 0x49, 0xa2, 0x00, 0x98,
  26378. 0xf9, 0x5b, 0x81, 0x39, 0x5f, 0xd7, 0x44, 0x9b,
  26379. 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, 0x87, 0xec,
  26380. 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24,
  26381. 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d,
  26382. 0x80, 0xe9, 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76,
  26383. 0x9b, 0x46, 0xc7, 0x06, 0x1b, 0xd6, 0x78, 0x3d,
  26384. 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, 0x1a, 0xbe,
  26385. 0xaf, 0xe8, 0x25, 0x61, 0x80
  26386. };
  26387. word32 idx;
  26388. #endif /* NO_ASN */
  26389. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  26390. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  26391. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26392. ed448_key *key3 = NULL;
  26393. #else
  26394. ed448_key key3[1];
  26395. #endif
  26396. #endif
  26397. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26398. key = (ed448_key *)XMALLOC(sizeof(*key), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26399. key2 = (ed448_key *)XMALLOC(sizeof(*key2), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26400. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  26401. key3 = (ed448_key *)XMALLOC(sizeof(*key3), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26402. #endif
  26403. #endif
  26404. /* create ed448 keys */
  26405. #ifndef HAVE_FIPS
  26406. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  26407. #else
  26408. ret = wc_InitRng(&rng);
  26409. #endif
  26410. if (ret != 0) {
  26411. XMEMSET(&rng, 0, sizeof(rng));
  26412. ERROR_OUT(-11700, out);
  26413. }
  26414. if (wc_ed448_init(key) < 0)
  26415. ERROR_OUT(-11903, out);
  26416. if (wc_ed448_init(key2) < 0)
  26417. ERROR_OUT(-11904, out);
  26418. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  26419. if (wc_ed448_init(key3) < 0)
  26420. ERROR_OUT(-11905, out);
  26421. #endif
  26422. if (wc_ed448_make_key(&rng, ED448_KEY_SIZE, key) < 0)
  26423. ERROR_OUT(-11906, out);
  26424. if (wc_ed448_make_key(&rng, ED448_KEY_SIZE, key2) < 0)
  26425. ERROR_OUT(-11907, out);
  26426. /* helper functions for signature and key size */
  26427. keySz = wc_ed448_size(key);
  26428. sigSz = wc_ed448_sig_size(key);
  26429. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_EXPORT) &&\
  26430. defined(HAVE_ED448_KEY_IMPORT)
  26431. for (i = 0; i < 6; i++) {
  26432. outlen = sizeof(out);
  26433. XMEMSET(out, 0, sizeof(out));
  26434. if (wc_ed448_import_private_key(sKeys[i], ED448_KEY_SIZE, pKeys[i],
  26435. pKeySz[i], key) != 0)
  26436. ERROR_OUT(-11701 - i, out);
  26437. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, key, NULL,
  26438. 0) != 0)
  26439. ERROR_OUT(-11711 - i, out);
  26440. if (XMEMCMP(out, sigs[i], 114))
  26441. ERROR_OUT(-11721 - i, out);
  26442. #if defined(HAVE_ED448_VERIFY)
  26443. /* test verify on good msg */
  26444. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key,
  26445. NULL, 0) != 0 || verify != 1)
  26446. ERROR_OUT(-11731 - i, out);
  26447. #ifdef WOLFSSL_ED448_STREAMING_VERIFY
  26448. /* test verify on good msg using streaming interface directly */
  26449. if (wc_ed448_verify_msg_init(out, outlen,
  26450. key, (byte)Ed448, NULL, 0) != 0)
  26451. ERROR_OUT(-11911 - i, out);
  26452. for (j = 0; j < msgSz[i]; j += i) {
  26453. if (wc_ed448_verify_msg_update(msgs[i] + j, MIN(i, msgSz[i] - j), key) != 0)
  26454. ERROR_OUT(-11921 - i, out);
  26455. }
  26456. if (wc_ed448_verify_msg_final(out, outlen, &verify,
  26457. key) != 0 || verify != 1)
  26458. ERROR_OUT(-11931 - i, out);
  26459. #endif /* WOLFSSL_ED448_STREAMING_VERIFY */
  26460. /* test verify on bad msg */
  26461. out[outlen-2] = out[outlen-2] + 1;
  26462. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key,
  26463. NULL, 0) == 0 || verify == 1)
  26464. ERROR_OUT(-11741 - i, out);
  26465. #endif /* HAVE_ED448_VERIFY */
  26466. /* test api for import/exporting keys */
  26467. {
  26468. byte *exportPKey = NULL;
  26469. byte *exportSKey = NULL;
  26470. word32 exportPSz = ED448_KEY_SIZE;
  26471. word32 exportSSz = ED448_KEY_SIZE;
  26472. exportPKey = (byte *)XMALLOC(exportPSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26473. exportSKey = (byte *)XMALLOC(exportSSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26474. if ((exportPKey == NULL) || (exportSKey == NULL))
  26475. ERROR_OUT(-11902, out);
  26476. ret = 0;
  26477. do {
  26478. if (wc_ed448_export_public(key, exportPKey, &exportPSz) != 0) {
  26479. ret = -11751 - i;
  26480. break;
  26481. }
  26482. if (wc_ed448_import_public_ex(exportPKey, exportPSz, key2, 1) != 0) {
  26483. ret = -11761 - i;
  26484. break;
  26485. }
  26486. if (wc_ed448_export_private_only(key, exportSKey, &exportSSz) != 0) {
  26487. ret = -11771 - i;
  26488. break;
  26489. }
  26490. if (wc_ed448_import_private_key(exportSKey, exportSSz,
  26491. exportPKey, exportPSz, key2) != 0) {
  26492. ret = -11781 - i;
  26493. break;
  26494. }
  26495. /* clear "out" buffer and test sign with imported keys */
  26496. outlen = sizeof(out);
  26497. XMEMSET(out, 0, sizeof(out));
  26498. if (wc_ed448_sign_msg(msgs[i], msgSz[i], out, &outlen, key2, NULL,
  26499. 0) != 0) {
  26500. ret = -11791 - i;
  26501. break;
  26502. }
  26503. } while(0);
  26504. XFREE(exportPKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26505. XFREE(exportSKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26506. if (ret != 0)
  26507. goto out;
  26508. }
  26509. #if defined(HAVE_ED448_VERIFY)
  26510. if (wc_ed448_verify_msg(out, outlen, msgs[i], msgSz[i], &verify, key2,
  26511. NULL, 0) != 0 || verify != 1)
  26512. ERROR_OUT(-11801 - i, out);
  26513. if (XMEMCMP(out, sigs[i], SIGSZ))
  26514. ERROR_OUT(-11811 - i, out);
  26515. #endif /* HAVE_ED448_VERIFY */
  26516. }
  26517. ret = ed448_ctx_test();
  26518. if (ret != 0)
  26519. goto out;
  26520. ret = ed448ph_test();
  26521. if (ret != 0)
  26522. goto out;
  26523. #ifndef NO_ASN
  26524. /* Try ASN.1 encoded private-only key and public key. */
  26525. idx = 0;
  26526. if (wc_Ed448PrivateKeyDecode(privateEd448, &idx, key3,
  26527. sizeof(privateEd448)) != 0)
  26528. ERROR_OUT(-11821, out);
  26529. if (wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0)
  26530. != BAD_FUNC_ARG)
  26531. ERROR_OUT(-11831, out);
  26532. idx = 0;
  26533. if (wc_Ed448PublicKeyDecode(publicEd448, &idx, key3,
  26534. sizeof(publicEd448)) != 0)
  26535. ERROR_OUT(-11841, out);
  26536. if (wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0) != 0)
  26537. ERROR_OUT(-11851, out);
  26538. if (XMEMCMP(out, sigs[0], SIGSZ))
  26539. ERROR_OUT(-11861, out);
  26540. #if defined(HAVE_ED448_VERIFY)
  26541. /* test verify on good msg */
  26542. if (wc_ed448_verify_msg(out, outlen, msgs[0], msgSz[0], &verify, key3,
  26543. NULL, 0) != 0 || verify != 1)
  26544. ERROR_OUT(-11871, out);
  26545. #endif /* HAVE_ED448_VERIFY */
  26546. wc_ed448_free(key3);
  26547. if (wc_ed448_init(key3) < 0)
  26548. ERROR_OUT(-11908, out);
  26549. idx = 0;
  26550. if (wc_Ed448PrivateKeyDecode(privPubEd448, &idx, key3,
  26551. sizeof(privPubEd448)) != 0)
  26552. ERROR_OUT(-11881, out);
  26553. if (wc_ed448_sign_msg(msgs[0], msgSz[0], out, &outlen, key3, NULL, 0) != 0)
  26554. ERROR_OUT(-11891, out);
  26555. if (XMEMCMP(out, sigs[0], SIGSZ))
  26556. ERROR_OUT(-11901, out);
  26557. #endif /* NO_ASN */
  26558. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_EXPORT && HAVE_ED448_KEY_IMPORT */
  26559. ret = 0;
  26560. out:
  26561. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  26562. if (key) {
  26563. wc_ed448_free(key);
  26564. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26565. }
  26566. if (key2) {
  26567. wc_ed448_free(key2);
  26568. XFREE(key2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26569. }
  26570. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  26571. if (key3) {
  26572. wc_ed448_free(key3);
  26573. XFREE(key3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26574. }
  26575. #endif
  26576. #else
  26577. wc_ed448_free(key);
  26578. wc_ed448_free(key2);
  26579. #if !defined(NO_ASN) && defined(HAVE_ED448_SIGN)
  26580. wc_ed448_free(key3);
  26581. #endif
  26582. #endif
  26583. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  26584. wc_FreeRng(&rng);
  26585. #endif
  26586. if (ret < 0)
  26587. return ret;
  26588. /* hush warnings of unused keySz and sigSz */
  26589. (void)keySz;
  26590. (void)sigSz;
  26591. #ifdef WOLFSSL_TEST_CERT
  26592. ret = ed448_test_cert();
  26593. if (ret < 0)
  26594. return ret;
  26595. #ifdef WOLFSSL_CERT_GEN
  26596. ret = ed448_test_make_cert();
  26597. if (ret < 0)
  26598. return ret;
  26599. #endif /* WOLFSSL_CERT_GEN */
  26600. #endif /* WOLFSSL_TEST_CERT */
  26601. return 0;
  26602. }
  26603. #endif /* HAVE_ED448 */
  26604. #ifdef WOLFSSL_HAVE_KYBER
  26605. #ifdef WOLFSSL_WC_KYBER /* OQS and PQM4 do not support KATs */
  26606. #ifdef WOLFSSL_KYBER512
  26607. static int kyber512_kat(void)
  26608. {
  26609. KyberKey key;
  26610. int ret;
  26611. byte priv[KYBER512_PRIVATE_KEY_SIZE];
  26612. byte pub[KYBER512_PUBLIC_KEY_SIZE];
  26613. byte ct[KYBER512_CIPHER_TEXT_SIZE];
  26614. byte ss[KYBER_SS_SZ];
  26615. byte ss_dec[KYBER_SS_SZ];
  26616. const byte kyber512_rand[] = {
  26617. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  26618. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  26619. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  26620. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  26621. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  26622. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  26623. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  26624. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  26625. };
  26626. const byte kyber512enc_rand[] = {
  26627. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  26628. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  26629. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  26630. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  26631. };
  26632. #ifndef WOLFSSL_KYBER_90S
  26633. const byte kyber512_pk[] = {
  26634. 0x11, 0x5A, 0xCE, 0x0E, 0x64, 0x67, 0x7C, 0xBB,
  26635. 0x7D, 0xCF, 0xC9, 0x3C, 0x16, 0xD3, 0xA3, 0x05,
  26636. 0xF6, 0x76, 0x15, 0xA4, 0x88, 0xD7, 0x11, 0xAA,
  26637. 0x56, 0x69, 0x8C, 0x56, 0x63, 0xAB, 0x7A, 0xC9,
  26638. 0xCE, 0x66, 0xD5, 0x47, 0xC0, 0x59, 0x5F, 0x98,
  26639. 0xA4, 0x3F, 0x46, 0x50, 0xBB, 0xE0, 0x8C, 0x36,
  26640. 0x4D, 0x97, 0x67, 0x89, 0x11, 0x7D, 0x34, 0xF6,
  26641. 0xAE, 0x51, 0xAC, 0x06, 0x3C, 0xB5, 0x5C, 0x6C,
  26642. 0xA3, 0x25, 0x58, 0x22, 0x7D, 0xFE, 0xF8, 0x07,
  26643. 0xD1, 0x9C, 0x30, 0xDE, 0x41, 0x44, 0x24, 0x09,
  26644. 0x7F, 0x6A, 0xA2, 0x36, 0xA1, 0x05, 0x3B, 0x4A,
  26645. 0x07, 0xA7, 0x6B, 0xE3, 0x72, 0xA5, 0xC6, 0xB6,
  26646. 0x00, 0x27, 0x91, 0xEB, 0xE0, 0xAF, 0xDA, 0xF5,
  26647. 0x4E, 0x1C, 0xA2, 0x37, 0xFF, 0x54, 0x5B, 0xA6,
  26648. 0x83, 0x43, 0xE7, 0x45, 0xC0, 0x4A, 0xD1, 0x63,
  26649. 0x9D, 0xBC, 0x59, 0x03, 0x46, 0xB6, 0xB9, 0x56,
  26650. 0x9B, 0x56, 0xDB, 0xBF, 0xE5, 0x31, 0x51, 0x91,
  26651. 0x30, 0x66, 0xE5, 0xC8, 0x55, 0x27, 0xDC, 0x94,
  26652. 0x68, 0x11, 0x0A, 0x13, 0x6A, 0x41, 0x14, 0x97,
  26653. 0xC2, 0x27, 0xDC, 0xB8, 0xC9, 0xB2, 0x55, 0x70,
  26654. 0xB7, 0xA0, 0xE4, 0x2A, 0xAD, 0xA6, 0x70, 0x9F,
  26655. 0x23, 0x20, 0x8F, 0x5D, 0x49, 0x6E, 0xBA, 0xB7,
  26656. 0x84, 0x3F, 0x64, 0x83, 0xBF, 0x0C, 0x0C, 0x73,
  26657. 0xA4, 0x02, 0x96, 0xEC, 0x2C, 0x64, 0x40, 0x00,
  26658. 0x13, 0x94, 0xC9, 0x9C, 0xA1, 0x73, 0xD5, 0xC7,
  26659. 0x75, 0xB7, 0xF4, 0x15, 0xD0, 0x2A, 0x5A, 0x26,
  26660. 0xA0, 0x74, 0x07, 0x91, 0x85, 0x87, 0xC4, 0x11,
  26661. 0x69, 0xF2, 0xB7, 0x17, 0x87, 0x55, 0xAC, 0xC2,
  26662. 0x7F, 0xC8, 0xB1, 0x9C, 0x4C, 0x4B, 0x3F, 0xCD,
  26663. 0x41, 0x05, 0x3F, 0x2C, 0x74, 0xC8, 0xA1, 0x0A,
  26664. 0x83, 0x21, 0x24, 0x1B, 0x28, 0x02, 0x43, 0x28,
  26665. 0x75, 0xAE, 0x80, 0x8B, 0x9E, 0xF1, 0x36, 0x5C,
  26666. 0x7B, 0x8A, 0x52, 0x90, 0x2F, 0x13, 0x17, 0xBA,
  26667. 0x2F, 0xB0, 0x26, 0x9F, 0x47, 0x93, 0x06, 0x72,
  26668. 0x10, 0x7B, 0x47, 0x26, 0xFE, 0xF6, 0x45, 0x47,
  26669. 0x39, 0x4D, 0x33, 0x20, 0xC8, 0xF1, 0x20, 0xB3,
  26670. 0xC2, 0xF4, 0x72, 0x5B, 0x03, 0x05, 0xFA, 0xB8,
  26671. 0x8C, 0xC7, 0x98, 0x1F, 0xCB, 0x09, 0xA7, 0x6A,
  26672. 0x1C, 0xBF, 0x7F, 0x17, 0x9F, 0x43, 0xBB, 0x0A,
  26673. 0x4C, 0x8B, 0x05, 0x90, 0x85, 0x7F, 0x1E, 0x69,
  26674. 0x70, 0x84, 0x66, 0xC7, 0xF8, 0x60, 0x73, 0x91,
  26675. 0xE7, 0xBC, 0x52, 0x68, 0xBF, 0xD3, 0xD7, 0xA1,
  26676. 0xDF, 0xFC, 0xB4, 0xEC, 0xA2, 0xA1, 0xC9, 0xB5,
  26677. 0x97, 0x59, 0x30, 0x13, 0xD5, 0xFC, 0x42, 0x02,
  26678. 0xEC, 0x2B, 0x74, 0xE5, 0x7A, 0xB7, 0x6B, 0xBC,
  26679. 0xF3, 0x63, 0x2B, 0xBA, 0xF9, 0x7C, 0xDC, 0x41,
  26680. 0x8A, 0x6F, 0x16, 0x39, 0x28, 0x38, 0xCA, 0x9B,
  26681. 0xF4, 0x5D, 0xDF, 0x02, 0x37, 0x77, 0xB7, 0x56,
  26682. 0x18, 0x33, 0xC1, 0x05, 0x19, 0x0F, 0x94, 0xF3,
  26683. 0x02, 0xC5, 0x9B, 0x53, 0x19, 0x00, 0xBB, 0xC8,
  26684. 0x16, 0x36, 0x1F, 0xAA, 0x5B, 0x33, 0x80, 0xCA,
  26685. 0x3A, 0x89, 0x31, 0x04, 0xCA, 0x73, 0x88, 0xB1,
  26686. 0x85, 0x67, 0x1B, 0x3E, 0x5F, 0xE3, 0x79, 0x0E,
  26687. 0x9A, 0x62, 0x6E, 0xC4, 0x6D, 0x9B, 0x0B, 0x33,
  26688. 0xC7, 0xA4, 0x19, 0xAF, 0x7B, 0x32, 0xB6, 0x85,
  26689. 0x98, 0x94, 0xF5, 0x75, 0xD8, 0x2A, 0xC5, 0x45,
  26690. 0x6B, 0x54, 0x90, 0xA7, 0xAF, 0x8F, 0xE6, 0x10,
  26691. 0x46, 0x36, 0x05, 0x89, 0xEC, 0xBA, 0x72, 0x44,
  26692. 0x23, 0x6F, 0x41, 0x23, 0x11, 0x6B, 0x61, 0x74,
  26693. 0xAA, 0x17, 0x92, 0x49, 0xA4, 0x91, 0x95, 0xB3,
  26694. 0x56, 0xC7, 0x2F, 0xC6, 0x64, 0x1F, 0x02, 0x51,
  26695. 0x81, 0x2E, 0xAA, 0x98, 0x57, 0x0B, 0x04, 0x66,
  26696. 0x99, 0x07, 0x0E, 0x08, 0x19, 0xDC, 0x27, 0x13,
  26697. 0xF4, 0x69, 0x13, 0x7D, 0xFC, 0x6A, 0x3D, 0x7B,
  26698. 0x92, 0xB2, 0x98, 0x99, 0x5E, 0xE7, 0x80, 0x36,
  26699. 0x91, 0x53, 0xAC, 0x36, 0x6B, 0x06, 0xD7, 0x24,
  26700. 0x9C, 0xD0, 0x9E, 0x1B, 0x33, 0x78, 0xFB, 0x04,
  26701. 0x39, 0x9C, 0xEC, 0xB8, 0x65, 0x05, 0x81, 0xD6,
  26702. 0x37, 0xC7, 0x9A, 0xE6, 0x7D, 0x6F, 0x2C, 0xAF,
  26703. 0x6A, 0xBA, 0xCF, 0x59, 0x81, 0x59, 0xA7, 0x79,
  26704. 0x2C, 0xB3, 0xC9, 0x71, 0xD1, 0x49, 0x9D, 0x23,
  26705. 0x73, 0xAD, 0x20, 0xF6, 0x3F, 0x03, 0xBB, 0x59,
  26706. 0xED, 0x13, 0x73, 0x84, 0xAC, 0x61, 0xA7, 0x15,
  26707. 0x51, 0x43, 0xB8, 0xCA, 0x49, 0x32, 0x61, 0x2E,
  26708. 0xC9, 0x15, 0xE4, 0xCA, 0x34, 0x6A, 0x9B, 0xCE,
  26709. 0x5D, 0xD6, 0x04, 0x17, 0xC6, 0xB2, 0xA8, 0x9B,
  26710. 0x1C, 0xC4, 0x35, 0x64, 0x3F, 0x87, 0x5B, 0xDC,
  26711. 0x5A, 0x7E, 0x5B, 0x34, 0x81, 0xCF, 0x91, 0x9E,
  26712. 0xA0, 0x91, 0x72, 0xFE, 0xBC, 0x46, 0xD4, 0xFC,
  26713. 0x3F, 0xB0, 0xCB, 0x95, 0x91, 0x70, 0x4E, 0xE2,
  26714. 0xDB, 0xB6, 0x18, 0x44, 0xB2, 0xF3, 0x31, 0x4A,
  26715. 0x06, 0xBB, 0x6C, 0x6D, 0x34, 0x00, 0x5E, 0x48,
  26716. 0x5C, 0xE6, 0x67, 0xBD, 0xC7, 0xD0, 0x98, 0x58,
  26717. 0x69, 0x28, 0xD2, 0xD9, 0x13, 0x40, 0xF0, 0x04,
  26718. 0x19, 0xEA, 0x40, 0x13, 0x51, 0xA2, 0x40, 0xA0,
  26719. 0xB0, 0x41, 0x05, 0x8B, 0xEF, 0xB0, 0xC2, 0xFD,
  26720. 0x32, 0x64, 0x5B, 0x7A, 0x2D, 0xF8, 0xF5, 0xCB,
  26721. 0xFD, 0x87, 0x33, 0x27, 0xC9, 0x78, 0xD7, 0xB3,
  26722. 0x51, 0xA2, 0x80, 0x88, 0x43, 0x88, 0x37, 0x02,
  26723. 0x4C, 0x52, 0xB9, 0xC2, 0x95, 0xCD, 0x71, 0x36,
  26724. 0x46, 0xFB, 0x5D, 0x6C, 0x0C, 0xCF, 0xB4, 0x70,
  26725. 0x73, 0x4A, 0xC2, 0xB2, 0xBC, 0x81, 0x23, 0xC2,
  26726. 0xC1, 0x3D, 0xF6, 0x93, 0x8E, 0x92, 0x45, 0x5A,
  26727. 0x86, 0x26, 0x39, 0xFE, 0xB8, 0xA6, 0x4B, 0x85,
  26728. 0x16, 0x3E, 0x32, 0x70, 0x7E, 0x03, 0x7B, 0x38,
  26729. 0xD8, 0xAC, 0x39, 0x22, 0xB4, 0x51, 0x87, 0xBB,
  26730. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  26731. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  26732. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  26733. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  26734. };
  26735. const byte kyber512_sk[] = {
  26736. 0x6C, 0x89, 0x2B, 0x02, 0x97, 0xA9, 0xC7, 0x64,
  26737. 0x14, 0x93, 0xF8, 0x7D, 0xAF, 0x35, 0x33, 0xEE,
  26738. 0xD6, 0x1F, 0x07, 0xF4, 0x65, 0x20, 0x66, 0x33,
  26739. 0x7E, 0xD7, 0x40, 0x46, 0xDC, 0xC7, 0x1B, 0xA0,
  26740. 0x3F, 0x30, 0x96, 0x01, 0x03, 0x16, 0x1F, 0x7D,
  26741. 0xEB, 0x53, 0xA7, 0x1B, 0x11, 0x61, 0x72, 0x63,
  26742. 0xFE, 0x2A, 0x80, 0x97, 0x69, 0xCE, 0x6D, 0x70,
  26743. 0xA8, 0x5F, 0xE6, 0x00, 0xEC, 0xE2, 0x9D, 0x7F,
  26744. 0x36, 0xA1, 0x6D, 0x33, 0x1B, 0x8B, 0x2A, 0x9E,
  26745. 0x1D, 0xB8, 0xC0, 0x90, 0x74, 0x2D, 0xF0, 0x73,
  26746. 0x9F, 0xF0, 0x60, 0xCE, 0xB4, 0xEC, 0xC5, 0xAB,
  26747. 0x1C, 0x5E, 0x55, 0xAC, 0x97, 0xBB, 0x66, 0xA7,
  26748. 0xF8, 0x95, 0x10, 0x5D, 0x57, 0x78, 0x2B, 0x22,
  26749. 0x95, 0x38, 0xE3, 0x42, 0x15, 0x44, 0xA3, 0x42,
  26750. 0x14, 0x08, 0xDB, 0xF4, 0x49, 0x10, 0x93, 0x4C,
  26751. 0xC4, 0x23, 0x77, 0x4F, 0x16, 0x76, 0xFF, 0x1C,
  26752. 0x30, 0x6F, 0x97, 0x55, 0x5F, 0x57, 0xB4, 0xAE,
  26753. 0xD7, 0xA6, 0xBA, 0xB9, 0x50, 0xA8, 0x16, 0x3C,
  26754. 0x8D, 0x31, 0x8D, 0xEA, 0x62, 0x75, 0x1B, 0xD6,
  26755. 0xAB, 0xC5, 0x06, 0x9C, 0x06, 0xC8, 0x8F, 0x33,
  26756. 0x00, 0x26, 0xA1, 0x98, 0x06, 0xA0, 0x3B, 0x97,
  26757. 0xA7, 0x69, 0x6B, 0x56, 0xDA, 0x21, 0x82, 0x7B,
  26758. 0xB4, 0xE8, 0xDC, 0x03, 0x11, 0x52, 0xB4, 0x1B,
  26759. 0x89, 0x2A, 0x9E, 0x99, 0xAD, 0xF6, 0xE1, 0x96,
  26760. 0x3E, 0x96, 0x57, 0x88, 0x28, 0x15, 0x4F, 0x46,
  26761. 0x70, 0x33, 0x84, 0x69, 0x20, 0xFB, 0xB4, 0xB8,
  26762. 0x05, 0x44, 0xE7, 0xE8, 0xA8, 0x1A, 0xE9, 0x63,
  26763. 0xCF, 0x36, 0x8C, 0x9B, 0xA0, 0x37, 0xA8, 0xC2,
  26764. 0xAD, 0x62, 0xE3, 0x2B, 0x6E, 0x61, 0xC9, 0x1D,
  26765. 0x75, 0xCE, 0x00, 0x5A, 0xB3, 0x0F, 0x80, 0x99,
  26766. 0xA1, 0xF2, 0x9D, 0x7B, 0x63, 0x05, 0xB4, 0xDC,
  26767. 0x06, 0xE2, 0x56, 0x80, 0xBB, 0x00, 0x99, 0x2F,
  26768. 0x71, 0x7F, 0xE6, 0xC1, 0x15, 0xA8, 0x08, 0x42,
  26769. 0x31, 0xCC, 0x79, 0xDD, 0x70, 0x0E, 0xA6, 0x91,
  26770. 0x2A, 0xC7, 0xFA, 0x0D, 0x93, 0x7B, 0xB6, 0xA7,
  26771. 0x56, 0x66, 0x22, 0x30, 0x47, 0x0C, 0x18, 0x9B,
  26772. 0x5A, 0xA1, 0x65, 0x3D, 0xEB, 0x93, 0x7D, 0x5A,
  26773. 0x9C, 0x25, 0xA2, 0x1D, 0x93, 0xB1, 0x90, 0x74,
  26774. 0xFC, 0x23, 0x9D, 0x81, 0x53, 0x53, 0x97, 0x97,
  26775. 0xC7, 0xD4, 0xAB, 0x62, 0x64, 0x9D, 0x76, 0xAA,
  26776. 0x55, 0x37, 0x36, 0xA9, 0x49, 0x02, 0x2C, 0x22,
  26777. 0xC5, 0x2B, 0xAE, 0xEC, 0x60, 0x5B, 0x32, 0xCE,
  26778. 0x9E, 0x5B, 0x93, 0x84, 0x90, 0x35, 0x58, 0xCA,
  26779. 0x9D, 0x6A, 0x3A, 0xBA, 0x90, 0x42, 0x3E, 0xED,
  26780. 0xA0, 0x1C, 0x94, 0x19, 0x8B, 0x19, 0x2A, 0x8B,
  26781. 0xA9, 0x06, 0x34, 0x97, 0xA0, 0xC5, 0x01, 0x33,
  26782. 0x07, 0xDD, 0xD8, 0x63, 0x52, 0x64, 0x71, 0xA4,
  26783. 0xD9, 0x95, 0x23, 0xEB, 0x41, 0x7F, 0x29, 0x1A,
  26784. 0xAC, 0x0C, 0x3A, 0x58, 0x1B, 0x6D, 0xA0, 0x07,
  26785. 0x32, 0xE5, 0xE8, 0x1B, 0x1F, 0x7C, 0x87, 0x9B,
  26786. 0x16, 0x93, 0xC1, 0x3B, 0x6F, 0x9F, 0x79, 0x31,
  26787. 0x62, 0x24, 0x29, 0xE5, 0x42, 0xAF, 0x40, 0x69,
  26788. 0x22, 0x2F, 0x04, 0x55, 0x44, 0xE0, 0xCC, 0x4F,
  26789. 0xB2, 0x4D, 0x44, 0x48, 0xCF, 0x2C, 0x65, 0x96,
  26790. 0xF5, 0xCB, 0x08, 0x62, 0x4B, 0x11, 0x85, 0x01,
  26791. 0x3B, 0x6B, 0x02, 0x08, 0x92, 0xF9, 0x6B, 0xDF,
  26792. 0xD4, 0xAD, 0xA9, 0x17, 0x9D, 0xE7, 0x27, 0xB8,
  26793. 0xD9, 0x42, 0x6E, 0x09, 0x96, 0xB5, 0xD3, 0x49,
  26794. 0x48, 0xCE, 0x02, 0xD0, 0xC3, 0x69, 0xB3, 0x7C,
  26795. 0xBB, 0x54, 0xD3, 0x47, 0x9E, 0xD8, 0xB5, 0x82,
  26796. 0xE9, 0xE7, 0x28, 0x92, 0x9B, 0x4C, 0x71, 0xC9,
  26797. 0xBE, 0x11, 0xD4, 0x5B, 0x20, 0xC4, 0xBD, 0xC3,
  26798. 0xC7, 0x43, 0x13, 0x22, 0x3F, 0x58, 0x27, 0x4E,
  26799. 0x8B, 0xA5, 0x24, 0x44, 0x47, 0xC4, 0x95, 0x95,
  26800. 0x0B, 0x84, 0xCB, 0x0C, 0x3C, 0x27, 0x36, 0x40,
  26801. 0x10, 0x8A, 0x33, 0x97, 0x94, 0x45, 0x73, 0x27,
  26802. 0x93, 0x28, 0x99, 0x6C, 0xDC, 0x0C, 0x91, 0x3C,
  26803. 0x95, 0x8A, 0xD6, 0x20, 0xBA, 0x8B, 0x5E, 0x5E,
  26804. 0xCB, 0xBB, 0x7E, 0x13, 0xCB, 0x9C, 0x70, 0xBD,
  26805. 0x5A, 0xB3, 0x0E, 0xB7, 0x48, 0x8C, 0x97, 0x00,
  26806. 0x1C, 0x20, 0x49, 0x8F, 0x1D, 0x7C, 0xC0, 0x6D,
  26807. 0xA7, 0x6B, 0xF5, 0x20, 0xC6, 0x58, 0xCC, 0xAD,
  26808. 0xFA, 0x29, 0x56, 0x42, 0x45, 0x57, 0xAB, 0xEA,
  26809. 0x8A, 0xB8, 0x92, 0x39, 0xC1, 0x78, 0x33, 0xDC,
  26810. 0x3A, 0x49, 0xB3, 0x6A, 0x9A, 0xE9, 0xA4, 0x86,
  26811. 0x94, 0x05, 0x40, 0xEB, 0x44, 0x4F, 0x97, 0x15,
  26812. 0x23, 0x57, 0xE0, 0x20, 0x35, 0x93, 0x9D, 0x75,
  26813. 0xA3, 0xC0, 0x25, 0xF4, 0x1A, 0x40, 0x08, 0x23,
  26814. 0x82, 0xA0, 0x73, 0x3C, 0x39, 0xB0, 0x62, 0x2B,
  26815. 0x74, 0x0E, 0x40, 0x75, 0x92, 0xC6, 0x2E, 0xCA,
  26816. 0xEB, 0x14, 0x32, 0xC4, 0x45, 0xB3, 0x70, 0x3A,
  26817. 0x86, 0xF6, 0x98, 0x1A, 0x27, 0x81, 0x57, 0xEA,
  26818. 0x95, 0xA6, 0xE9, 0x2D, 0x55, 0xE4, 0xB9, 0x72,
  26819. 0xF9, 0x36, 0xC2, 0xF0, 0xA6, 0x58, 0x28, 0x0E,
  26820. 0xA2, 0xB0, 0x7A, 0x48, 0x99, 0x2D, 0xF8, 0x93,
  26821. 0x7E, 0x0A, 0x2A, 0xC1, 0xDC, 0xC9, 0x74, 0xFE,
  26822. 0x00, 0xAA, 0xE1, 0xF5, 0x61, 0xFA, 0x25, 0x8E,
  26823. 0x2D, 0x25, 0x9C, 0x3E, 0x86, 0x1D, 0xCE, 0x23,
  26824. 0x60, 0x39, 0x12, 0x76, 0x06, 0xFC, 0x1C, 0xE0,
  26825. 0x09, 0x00, 0x3A, 0x7B, 0xAC, 0x94, 0x21, 0x01,
  26826. 0xDC, 0xB8, 0x22, 0xB1, 0xF3, 0xC1, 0x2B, 0xF7,
  26827. 0x32, 0x38, 0xF5, 0x46, 0xE0, 0x1C, 0x36, 0xB5,
  26828. 0xA6, 0x93, 0x61, 0x92, 0x99, 0x5C, 0xC6, 0x9C,
  26829. 0x63, 0x23, 0x74, 0x09, 0xCB, 0x53, 0xC2, 0xE3,
  26830. 0x5D, 0x74, 0x89, 0x0D, 0x18, 0x88, 0x53, 0x76,
  26831. 0xFA, 0x55, 0x03, 0xB1, 0x07, 0xA2, 0xA3, 0x92,
  26832. 0x11, 0x5A, 0xCE, 0x0E, 0x64, 0x67, 0x7C, 0xBB,
  26833. 0x7D, 0xCF, 0xC9, 0x3C, 0x16, 0xD3, 0xA3, 0x05,
  26834. 0xF6, 0x76, 0x15, 0xA4, 0x88, 0xD7, 0x11, 0xAA,
  26835. 0x56, 0x69, 0x8C, 0x56, 0x63, 0xAB, 0x7A, 0xC9,
  26836. 0xCE, 0x66, 0xD5, 0x47, 0xC0, 0x59, 0x5F, 0x98,
  26837. 0xA4, 0x3F, 0x46, 0x50, 0xBB, 0xE0, 0x8C, 0x36,
  26838. 0x4D, 0x97, 0x67, 0x89, 0x11, 0x7D, 0x34, 0xF6,
  26839. 0xAE, 0x51, 0xAC, 0x06, 0x3C, 0xB5, 0x5C, 0x6C,
  26840. 0xA3, 0x25, 0x58, 0x22, 0x7D, 0xFE, 0xF8, 0x07,
  26841. 0xD1, 0x9C, 0x30, 0xDE, 0x41, 0x44, 0x24, 0x09,
  26842. 0x7F, 0x6A, 0xA2, 0x36, 0xA1, 0x05, 0x3B, 0x4A,
  26843. 0x07, 0xA7, 0x6B, 0xE3, 0x72, 0xA5, 0xC6, 0xB6,
  26844. 0x00, 0x27, 0x91, 0xEB, 0xE0, 0xAF, 0xDA, 0xF5,
  26845. 0x4E, 0x1C, 0xA2, 0x37, 0xFF, 0x54, 0x5B, 0xA6,
  26846. 0x83, 0x43, 0xE7, 0x45, 0xC0, 0x4A, 0xD1, 0x63,
  26847. 0x9D, 0xBC, 0x59, 0x03, 0x46, 0xB6, 0xB9, 0x56,
  26848. 0x9B, 0x56, 0xDB, 0xBF, 0xE5, 0x31, 0x51, 0x91,
  26849. 0x30, 0x66, 0xE5, 0xC8, 0x55, 0x27, 0xDC, 0x94,
  26850. 0x68, 0x11, 0x0A, 0x13, 0x6A, 0x41, 0x14, 0x97,
  26851. 0xC2, 0x27, 0xDC, 0xB8, 0xC9, 0xB2, 0x55, 0x70,
  26852. 0xB7, 0xA0, 0xE4, 0x2A, 0xAD, 0xA6, 0x70, 0x9F,
  26853. 0x23, 0x20, 0x8F, 0x5D, 0x49, 0x6E, 0xBA, 0xB7,
  26854. 0x84, 0x3F, 0x64, 0x83, 0xBF, 0x0C, 0x0C, 0x73,
  26855. 0xA4, 0x02, 0x96, 0xEC, 0x2C, 0x64, 0x40, 0x00,
  26856. 0x13, 0x94, 0xC9, 0x9C, 0xA1, 0x73, 0xD5, 0xC7,
  26857. 0x75, 0xB7, 0xF4, 0x15, 0xD0, 0x2A, 0x5A, 0x26,
  26858. 0xA0, 0x74, 0x07, 0x91, 0x85, 0x87, 0xC4, 0x11,
  26859. 0x69, 0xF2, 0xB7, 0x17, 0x87, 0x55, 0xAC, 0xC2,
  26860. 0x7F, 0xC8, 0xB1, 0x9C, 0x4C, 0x4B, 0x3F, 0xCD,
  26861. 0x41, 0x05, 0x3F, 0x2C, 0x74, 0xC8, 0xA1, 0x0A,
  26862. 0x83, 0x21, 0x24, 0x1B, 0x28, 0x02, 0x43, 0x28,
  26863. 0x75, 0xAE, 0x80, 0x8B, 0x9E, 0xF1, 0x36, 0x5C,
  26864. 0x7B, 0x8A, 0x52, 0x90, 0x2F, 0x13, 0x17, 0xBA,
  26865. 0x2F, 0xB0, 0x26, 0x9F, 0x47, 0x93, 0x06, 0x72,
  26866. 0x10, 0x7B, 0x47, 0x26, 0xFE, 0xF6, 0x45, 0x47,
  26867. 0x39, 0x4D, 0x33, 0x20, 0xC8, 0xF1, 0x20, 0xB3,
  26868. 0xC2, 0xF4, 0x72, 0x5B, 0x03, 0x05, 0xFA, 0xB8,
  26869. 0x8C, 0xC7, 0x98, 0x1F, 0xCB, 0x09, 0xA7, 0x6A,
  26870. 0x1C, 0xBF, 0x7F, 0x17, 0x9F, 0x43, 0xBB, 0x0A,
  26871. 0x4C, 0x8B, 0x05, 0x90, 0x85, 0x7F, 0x1E, 0x69,
  26872. 0x70, 0x84, 0x66, 0xC7, 0xF8, 0x60, 0x73, 0x91,
  26873. 0xE7, 0xBC, 0x52, 0x68, 0xBF, 0xD3, 0xD7, 0xA1,
  26874. 0xDF, 0xFC, 0xB4, 0xEC, 0xA2, 0xA1, 0xC9, 0xB5,
  26875. 0x97, 0x59, 0x30, 0x13, 0xD5, 0xFC, 0x42, 0x02,
  26876. 0xEC, 0x2B, 0x74, 0xE5, 0x7A, 0xB7, 0x6B, 0xBC,
  26877. 0xF3, 0x63, 0x2B, 0xBA, 0xF9, 0x7C, 0xDC, 0x41,
  26878. 0x8A, 0x6F, 0x16, 0x39, 0x28, 0x38, 0xCA, 0x9B,
  26879. 0xF4, 0x5D, 0xDF, 0x02, 0x37, 0x77, 0xB7, 0x56,
  26880. 0x18, 0x33, 0xC1, 0x05, 0x19, 0x0F, 0x94, 0xF3,
  26881. 0x02, 0xC5, 0x9B, 0x53, 0x19, 0x00, 0xBB, 0xC8,
  26882. 0x16, 0x36, 0x1F, 0xAA, 0x5B, 0x33, 0x80, 0xCA,
  26883. 0x3A, 0x89, 0x31, 0x04, 0xCA, 0x73, 0x88, 0xB1,
  26884. 0x85, 0x67, 0x1B, 0x3E, 0x5F, 0xE3, 0x79, 0x0E,
  26885. 0x9A, 0x62, 0x6E, 0xC4, 0x6D, 0x9B, 0x0B, 0x33,
  26886. 0xC7, 0xA4, 0x19, 0xAF, 0x7B, 0x32, 0xB6, 0x85,
  26887. 0x98, 0x94, 0xF5, 0x75, 0xD8, 0x2A, 0xC5, 0x45,
  26888. 0x6B, 0x54, 0x90, 0xA7, 0xAF, 0x8F, 0xE6, 0x10,
  26889. 0x46, 0x36, 0x05, 0x89, 0xEC, 0xBA, 0x72, 0x44,
  26890. 0x23, 0x6F, 0x41, 0x23, 0x11, 0x6B, 0x61, 0x74,
  26891. 0xAA, 0x17, 0x92, 0x49, 0xA4, 0x91, 0x95, 0xB3,
  26892. 0x56, 0xC7, 0x2F, 0xC6, 0x64, 0x1F, 0x02, 0x51,
  26893. 0x81, 0x2E, 0xAA, 0x98, 0x57, 0x0B, 0x04, 0x66,
  26894. 0x99, 0x07, 0x0E, 0x08, 0x19, 0xDC, 0x27, 0x13,
  26895. 0xF4, 0x69, 0x13, 0x7D, 0xFC, 0x6A, 0x3D, 0x7B,
  26896. 0x92, 0xB2, 0x98, 0x99, 0x5E, 0xE7, 0x80, 0x36,
  26897. 0x91, 0x53, 0xAC, 0x36, 0x6B, 0x06, 0xD7, 0x24,
  26898. 0x9C, 0xD0, 0x9E, 0x1B, 0x33, 0x78, 0xFB, 0x04,
  26899. 0x39, 0x9C, 0xEC, 0xB8, 0x65, 0x05, 0x81, 0xD6,
  26900. 0x37, 0xC7, 0x9A, 0xE6, 0x7D, 0x6F, 0x2C, 0xAF,
  26901. 0x6A, 0xBA, 0xCF, 0x59, 0x81, 0x59, 0xA7, 0x79,
  26902. 0x2C, 0xB3, 0xC9, 0x71, 0xD1, 0x49, 0x9D, 0x23,
  26903. 0x73, 0xAD, 0x20, 0xF6, 0x3F, 0x03, 0xBB, 0x59,
  26904. 0xED, 0x13, 0x73, 0x84, 0xAC, 0x61, 0xA7, 0x15,
  26905. 0x51, 0x43, 0xB8, 0xCA, 0x49, 0x32, 0x61, 0x2E,
  26906. 0xC9, 0x15, 0xE4, 0xCA, 0x34, 0x6A, 0x9B, 0xCE,
  26907. 0x5D, 0xD6, 0x04, 0x17, 0xC6, 0xB2, 0xA8, 0x9B,
  26908. 0x1C, 0xC4, 0x35, 0x64, 0x3F, 0x87, 0x5B, 0xDC,
  26909. 0x5A, 0x7E, 0x5B, 0x34, 0x81, 0xCF, 0x91, 0x9E,
  26910. 0xA0, 0x91, 0x72, 0xFE, 0xBC, 0x46, 0xD4, 0xFC,
  26911. 0x3F, 0xB0, 0xCB, 0x95, 0x91, 0x70, 0x4E, 0xE2,
  26912. 0xDB, 0xB6, 0x18, 0x44, 0xB2, 0xF3, 0x31, 0x4A,
  26913. 0x06, 0xBB, 0x6C, 0x6D, 0x34, 0x00, 0x5E, 0x48,
  26914. 0x5C, 0xE6, 0x67, 0xBD, 0xC7, 0xD0, 0x98, 0x58,
  26915. 0x69, 0x28, 0xD2, 0xD9, 0x13, 0x40, 0xF0, 0x04,
  26916. 0x19, 0xEA, 0x40, 0x13, 0x51, 0xA2, 0x40, 0xA0,
  26917. 0xB0, 0x41, 0x05, 0x8B, 0xEF, 0xB0, 0xC2, 0xFD,
  26918. 0x32, 0x64, 0x5B, 0x7A, 0x2D, 0xF8, 0xF5, 0xCB,
  26919. 0xFD, 0x87, 0x33, 0x27, 0xC9, 0x78, 0xD7, 0xB3,
  26920. 0x51, 0xA2, 0x80, 0x88, 0x43, 0x88, 0x37, 0x02,
  26921. 0x4C, 0x52, 0xB9, 0xC2, 0x95, 0xCD, 0x71, 0x36,
  26922. 0x46, 0xFB, 0x5D, 0x6C, 0x0C, 0xCF, 0xB4, 0x70,
  26923. 0x73, 0x4A, 0xC2, 0xB2, 0xBC, 0x81, 0x23, 0xC2,
  26924. 0xC1, 0x3D, 0xF6, 0x93, 0x8E, 0x92, 0x45, 0x5A,
  26925. 0x86, 0x26, 0x39, 0xFE, 0xB8, 0xA6, 0x4B, 0x85,
  26926. 0x16, 0x3E, 0x32, 0x70, 0x7E, 0x03, 0x7B, 0x38,
  26927. 0xD8, 0xAC, 0x39, 0x22, 0xB4, 0x51, 0x87, 0xBB,
  26928. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  26929. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  26930. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  26931. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  26932. 0x7F, 0xFA, 0xD1, 0xBC, 0x8A, 0xF7, 0x3B, 0x7E,
  26933. 0x87, 0x49, 0x56, 0xB8, 0x1C, 0x2A, 0x2E, 0xF0,
  26934. 0xBF, 0xAB, 0xE8, 0xDC, 0x93, 0xD7, 0x7B, 0x2F,
  26935. 0xBC, 0x9E, 0x0C, 0x64, 0xEF, 0xA0, 0x1E, 0x84,
  26936. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  26937. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  26938. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  26939. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  26940. };
  26941. const byte kyber512_ct[] = {
  26942. 0xED, 0xF2, 0x41, 0x45, 0xE4, 0x3B, 0x4F, 0x6D,
  26943. 0xC6, 0xBF, 0x83, 0x32, 0xF5, 0x4E, 0x02, 0xCA,
  26944. 0xB0, 0x2D, 0xBF, 0x3B, 0x56, 0x05, 0xDD, 0xC9,
  26945. 0x0A, 0x15, 0xC8, 0x86, 0xAD, 0x3E, 0xD4, 0x89,
  26946. 0x46, 0x26, 0x99, 0xE4, 0xAB, 0xED, 0x44, 0x35,
  26947. 0x0B, 0xC3, 0x75, 0x7E, 0x26, 0x96, 0xFB, 0xFB,
  26948. 0x25, 0x34, 0x41, 0x2E, 0x8D, 0xD2, 0x01, 0xF1,
  26949. 0xE4, 0x54, 0x0A, 0x39, 0x70, 0xB0, 0x55, 0xFE,
  26950. 0x3B, 0x0B, 0xEC, 0x3A, 0x71, 0xF9, 0xE1, 0x15,
  26951. 0xB3, 0xF9, 0xF3, 0x91, 0x02, 0x06, 0x5B, 0x1C,
  26952. 0xCA, 0x83, 0x14, 0xDC, 0xC7, 0x95, 0xE3, 0xC0,
  26953. 0xE8, 0xFA, 0x98, 0xEE, 0x83, 0xCA, 0x66, 0x28,
  26954. 0x45, 0x70, 0x28, 0xA4, 0xD0, 0x9E, 0x83, 0x9E,
  26955. 0x55, 0x48, 0x62, 0xCF, 0x0B, 0x7B, 0xF5, 0x6C,
  26956. 0x5C, 0x0A, 0x82, 0x9E, 0x86, 0x57, 0x94, 0x79,
  26957. 0x45, 0xFE, 0x9C, 0x22, 0x56, 0x4F, 0xBA, 0xEB,
  26958. 0xC1, 0xB3, 0xAF, 0x35, 0x0D, 0x79, 0x55, 0x50,
  26959. 0x8A, 0x26, 0xD8, 0xA8, 0xEB, 0x54, 0x7B, 0x8B,
  26960. 0x1A, 0x2C, 0xF0, 0x3C, 0xCA, 0x1A, 0xAB, 0xCE,
  26961. 0x6C, 0x34, 0x97, 0x78, 0x3B, 0x64, 0x65, 0xBA,
  26962. 0x0B, 0x6E, 0x7A, 0xCB, 0xA8, 0x21, 0x19, 0x51,
  26963. 0x24, 0xAE, 0xF0, 0x9E, 0x62, 0x83, 0x82, 0xA1,
  26964. 0xF9, 0x14, 0x04, 0x3B, 0xE7, 0x09, 0x6E, 0x95,
  26965. 0x2C, 0xBC, 0x4F, 0xB4, 0xAF, 0xED, 0x13, 0x60,
  26966. 0x90, 0x46, 0x11, 0x7C, 0x01, 0x1F, 0xD7, 0x41,
  26967. 0xEE, 0x28, 0x6C, 0x83, 0x77, 0x16, 0x90, 0xF0,
  26968. 0xAE, 0xB5, 0x0D, 0xA0, 0xD7, 0x12, 0x85, 0xA1,
  26969. 0x79, 0xB2, 0x15, 0xC6, 0x03, 0x6D, 0xEB, 0x78,
  26970. 0x0F, 0x4D, 0x16, 0x76, 0x9F, 0x72, 0xDE, 0x16,
  26971. 0xFD, 0xAD, 0xAC, 0x73, 0xBE, 0xFA, 0x5B, 0xEF,
  26972. 0x89, 0x43, 0x19, 0x7F, 0x44, 0xC5, 0x95, 0x89,
  26973. 0xDC, 0x9F, 0x49, 0x73, 0xDE, 0x14, 0x50, 0xBA,
  26974. 0x1D, 0x0C, 0x32, 0x90, 0xD6, 0xB1, 0xD6, 0x83,
  26975. 0xF2, 0x94, 0xE7, 0x59, 0xC9, 0x54, 0xAB, 0xE8,
  26976. 0xA7, 0xDA, 0x5B, 0x10, 0x54, 0xFD, 0x6D, 0x21,
  26977. 0x32, 0x9B, 0x8E, 0x73, 0xD3, 0x75, 0x6A, 0xFD,
  26978. 0xA0, 0xDC, 0xB1, 0xFC, 0x8B, 0x15, 0x82, 0xD1,
  26979. 0xF9, 0x0C, 0xF2, 0x75, 0xA1, 0x02, 0xAB, 0xC6,
  26980. 0xAC, 0x69, 0x9D, 0xF0, 0xC5, 0x87, 0x0E, 0x50,
  26981. 0xA1, 0xF9, 0x89, 0xE4, 0xE6, 0x24, 0x1B, 0x60,
  26982. 0xAA, 0xA2, 0xEC, 0xF9, 0xE8, 0xE3, 0x3E, 0x0F,
  26983. 0xFC, 0xF4, 0x0F, 0xE8, 0x31, 0xE8, 0xFD, 0xC2,
  26984. 0xE8, 0x3B, 0x52, 0xCA, 0x7A, 0xB6, 0xD9, 0x3F,
  26985. 0x14, 0x6D, 0x29, 0xDC, 0xA5, 0x3C, 0x7D, 0xA1,
  26986. 0xDB, 0x4A, 0xC4, 0xF2, 0xDB, 0x39, 0xEA, 0x12,
  26987. 0x0D, 0x90, 0xFA, 0x60, 0xF4, 0xD4, 0x37, 0xC6,
  26988. 0xD0, 0x0E, 0xF4, 0x83, 0xBC, 0x94, 0xA3, 0x17,
  26989. 0x5C, 0xDA, 0x16, 0x3F, 0xC1, 0xC2, 0x82, 0x8B,
  26990. 0xE4, 0xDB, 0xD6, 0x43, 0x05, 0x07, 0xB5, 0x84,
  26991. 0xBB, 0x51, 0x77, 0xE1, 0x71, 0xB8, 0xDD, 0xA9,
  26992. 0xA4, 0x29, 0x3C, 0x32, 0x00, 0x29, 0x5C, 0x80,
  26993. 0x3A, 0x86, 0x5D, 0x6D, 0x21, 0x66, 0xF6, 0x6B,
  26994. 0xA5, 0x40, 0x1F, 0xB7, 0xA0, 0xE8, 0x53, 0x16,
  26995. 0x86, 0x00, 0xA2, 0x94, 0x84, 0x37, 0xE0, 0x36,
  26996. 0xE3, 0xBF, 0x19, 0xE1, 0x2F, 0xD3, 0xF2, 0xA2,
  26997. 0xB8, 0xB3, 0x43, 0xF7, 0x84, 0x24, 0x8E, 0x8D,
  26998. 0x68, 0x5E, 0xB0, 0xAF, 0xDE, 0x63, 0x15, 0x33,
  26999. 0x87, 0x30, 0xE7, 0xA1, 0x00, 0x1C, 0x27, 0xD8,
  27000. 0xD2, 0xA7, 0x6F, 0xA6, 0x9D, 0x15, 0x7B, 0xA1,
  27001. 0xAC, 0x7A, 0xD5, 0x6D, 0xA5, 0xA8, 0xC7, 0x0F,
  27002. 0xE4, 0xB5, 0xB8, 0xD7, 0x86, 0xDC, 0x6F, 0xC0,
  27003. 0x56, 0x6B, 0xA8, 0xE1, 0xB8, 0x81, 0x63, 0x34,
  27004. 0xD3, 0x2A, 0x3F, 0xB1, 0xCE, 0x7D, 0x4D, 0x5E,
  27005. 0x4C, 0x33, 0x2A, 0xF7, 0xB0, 0x03, 0xD0, 0x91,
  27006. 0x74, 0x1A, 0x3D, 0x5C, 0x96, 0x52, 0x92, 0x25,
  27007. 0x5D, 0xFF, 0x8E, 0xD2, 0xBB, 0xF1, 0xF9, 0x11,
  27008. 0x6B, 0xE5, 0x0C, 0x17, 0xB8, 0xE5, 0x48, 0x74,
  27009. 0x8A, 0xD4, 0xB2, 0xE9, 0x57, 0xBB, 0xD1, 0x95,
  27010. 0x34, 0x82, 0xA2, 0xE1, 0x71, 0x8C, 0xEC, 0x66,
  27011. 0xCD, 0x2C, 0x81, 0xF5, 0x72, 0xD5, 0x52, 0xB7,
  27012. 0x18, 0x78, 0x85, 0xE6, 0xB8, 0x94, 0x3D, 0x64,
  27013. 0x31, 0x41, 0x3C, 0x59, 0xEB, 0xB7, 0xE0, 0x36,
  27014. 0x04, 0x84, 0x90, 0xBE, 0x52, 0x89, 0xE9, 0x5B,
  27015. 0x20, 0xA8, 0x9E, 0x8B, 0x15, 0x9F, 0x61, 0xA9,
  27016. 0xA9, 0x88, 0x6E, 0x14, 0x75, 0x68, 0xF4, 0xC9,
  27017. 0x02, 0x1F, 0x36, 0x2F, 0x02, 0x68, 0x8A, 0x1C,
  27018. 0x8C, 0x3B, 0xB0, 0xD2, 0x40, 0x86, 0x88, 0x0E,
  27019. 0x55, 0xB6, 0xED, 0xB4, 0x3F, 0x37, 0x45, 0xD2,
  27020. 0xC1, 0x66, 0xDC, 0x1C, 0xB7, 0x43, 0xC7, 0x6F,
  27021. 0xE6, 0xBE, 0x52, 0x3A, 0x89, 0x3C, 0xC7, 0x64,
  27022. 0xD1, 0x64, 0x35, 0xC3, 0x78, 0x51, 0x25, 0x2A,
  27023. 0x81, 0xE2, 0xFF, 0xBA, 0x0F, 0x18, 0x97, 0x1A,
  27024. 0x3D, 0xEE, 0x37, 0xD4, 0x87, 0x7C, 0xB9, 0x28,
  27025. 0xE3, 0x6E, 0x52, 0x35, 0x03, 0x7A, 0x6B, 0x20,
  27026. 0x57, 0x89, 0x7D, 0x51, 0x8A, 0x5F, 0x0E, 0x34,
  27027. 0x8E, 0x3A, 0xB6, 0xD5, 0xB5, 0x2D, 0xFC, 0x60,
  27028. 0x75, 0x7F, 0x3B, 0x41, 0xA4, 0xFE, 0xC7, 0x82,
  27029. 0x8F, 0x1D, 0xEE, 0xAF, 0x45, 0x87, 0xCC, 0xC8,
  27030. 0xEA, 0xDF, 0x64, 0x7F, 0x4D, 0x20, 0x3B, 0x2F,
  27031. 0xAA, 0x05, 0xA6, 0x49, 0xB5, 0x82, 0x34, 0x0C,
  27032. 0xB4, 0xCA, 0xCE, 0x57, 0xA3, 0x07, 0x11, 0xBE,
  27033. 0x75, 0x2F, 0xAC, 0xF0, 0x22, 0x7D, 0x0A, 0x80,
  27034. 0xC4, 0x12, 0x84, 0x42, 0xDD, 0xC5, 0x44, 0xBE,
  27035. 0x80, 0x5B, 0x9C, 0xFE, 0x8F, 0xE9, 0xB1, 0x23,
  27036. 0x7C, 0x80, 0xF9, 0x67, 0x87, 0xCD, 0x92, 0x81,
  27037. 0xCC, 0xF2, 0x70, 0xC1, 0xAF, 0xC0, 0x67, 0x0D
  27038. };
  27039. const byte kyber512_ss[] = {
  27040. 0x0A, 0x69, 0x25, 0x67, 0x6F, 0x24, 0xB2, 0x2C,
  27041. 0x28, 0x6F, 0x4C, 0x81, 0xA4, 0x22, 0x4C, 0xEC,
  27042. 0x50, 0x6C, 0x9B, 0x25, 0x7D, 0x48, 0x0E, 0x02,
  27043. 0xE3, 0xB4, 0x9F, 0x44, 0xCA, 0xA3, 0x23, 0x7F
  27044. };
  27045. #else
  27046. const byte kyber512_pk[] = {
  27047. 0x68, 0xD6, 0x83, 0xF6, 0x01, 0x8F, 0xCB, 0x38,
  27048. 0x71, 0xC0, 0x64, 0x0A, 0xD1, 0x7B, 0x7E, 0x80,
  27049. 0x02, 0x93, 0xC9, 0xF6, 0x36, 0xC1, 0xE2, 0x09,
  27050. 0x10, 0x34, 0x25, 0xAD, 0x72, 0x26, 0xDD, 0x13,
  27051. 0x90, 0x41, 0xFA, 0x84, 0x9F, 0x8C, 0x2F, 0xED,
  27052. 0x18, 0xAB, 0xEF, 0xF5, 0x2A, 0xE0, 0xB6, 0x06,
  27053. 0x86, 0x13, 0xBA, 0x05, 0x73, 0x09, 0x3D, 0xE8,
  27054. 0xAC, 0xC8, 0x56, 0x19, 0x80, 0x62, 0xCD, 0x38,
  27055. 0xDC, 0x0F, 0x80, 0x05, 0x6B, 0x8D, 0x1C, 0x42,
  27056. 0xDF, 0xA3, 0x2C, 0x4C, 0xA4, 0x0E, 0x8C, 0x57,
  27057. 0x03, 0xF3, 0x13, 0xBA, 0x5F, 0xD0, 0x5C, 0x1E,
  27058. 0x75, 0xC3, 0x78, 0xD7, 0x04, 0x77, 0x62, 0x1A,
  27059. 0xF3, 0xA0, 0x8E, 0x52, 0x0A, 0x16, 0x05, 0xC4,
  27060. 0x89, 0x42, 0x2C, 0x97, 0xD0, 0xD9, 0x2E, 0x43,
  27061. 0x12, 0xBB, 0xEF, 0xB6, 0xC0, 0x18, 0x7A, 0x45,
  27062. 0xF9, 0x24, 0x6B, 0xF8, 0xEB, 0xA6, 0x2C, 0x04,
  27063. 0x7F, 0x8C, 0xB2, 0x42, 0x17, 0xB2, 0x01, 0x78,
  27064. 0xBB, 0x09, 0x5E, 0x59, 0x63, 0x6B, 0xCB, 0xA9,
  27065. 0x13, 0x73, 0x52, 0x65, 0xC7, 0xA0, 0x90, 0x06,
  27066. 0x12, 0xEF, 0x16, 0x41, 0xC9, 0xE1, 0x4D, 0x7D,
  27067. 0x89, 0x15, 0xE3, 0xC8, 0x83, 0xB7, 0x50, 0x68,
  27068. 0xA9, 0xB1, 0x3B, 0x0C, 0xF0, 0xA1, 0x32, 0x8A,
  27069. 0x3E, 0xC5, 0xD6, 0x00, 0x1E, 0xE9, 0x6D, 0x01,
  27070. 0x05, 0x6C, 0x25, 0x09, 0xC2, 0x16, 0x51, 0x4D,
  27071. 0xA5, 0x24, 0x61, 0xF7, 0xCA, 0x6A, 0xC4, 0x1A,
  27072. 0x2C, 0x0E, 0x35, 0x2B, 0x53, 0x63, 0x68, 0x99,
  27073. 0xD0, 0x65, 0xBD, 0xF2, 0xBF, 0x14, 0xF5, 0x35,
  27074. 0xFC, 0xE9, 0x42, 0x93, 0x9B, 0x6B, 0x4B, 0x97,
  27075. 0x72, 0x04, 0xD6, 0x29, 0xA6, 0x24, 0x1F, 0xFD,
  27076. 0x99, 0x94, 0x6A, 0x70, 0xBC, 0xA8, 0x91, 0x2B,
  27077. 0x1D, 0xFA, 0x61, 0x4A, 0xE9, 0x91, 0x3C, 0xB3,
  27078. 0x65, 0x63, 0x04, 0x7F, 0x9F, 0xA0, 0xA6, 0x48,
  27079. 0x7B, 0xA4, 0x10, 0x19, 0x5F, 0x56, 0x17, 0x58,
  27080. 0x84, 0x41, 0xC6, 0xA1, 0xA1, 0x50, 0x78, 0xBA,
  27081. 0x60, 0x93, 0x36, 0x79, 0x64, 0xD2, 0x5F, 0x19,
  27082. 0xD7, 0x15, 0x1A, 0xE0, 0x9F, 0xAF, 0xA6, 0xA4,
  27083. 0xCB, 0x63, 0x09, 0x40, 0xC5, 0x8F, 0x88, 0xEB,
  27084. 0x77, 0x8B, 0xFA, 0x44, 0xD7, 0x3B, 0x30, 0x63,
  27085. 0x74, 0x46, 0x8A, 0xBB, 0x8B, 0x56, 0x16, 0x0D,
  27086. 0x49, 0xA2, 0x4E, 0x6F, 0x43, 0x2B, 0xBF, 0xE8,
  27087. 0x8D, 0xE0, 0xC5, 0x6E, 0x9A, 0xE2, 0x13, 0x23,
  27088. 0xF1, 0x2D, 0xEE, 0xFC, 0xAF, 0x32, 0xF6, 0x8C,
  27089. 0x46, 0x21, 0x2F, 0xE3, 0x68, 0x3F, 0xB0, 0x48,
  27090. 0xB7, 0xE2, 0x4C, 0xB8, 0x17, 0xF8, 0x7E, 0xC4,
  27091. 0xA4, 0x2F, 0xE1, 0x1B, 0x21, 0xEA, 0x54, 0x4E,
  27092. 0xB7, 0xD4, 0x82, 0x89, 0xAA, 0xB4, 0xB3, 0x9C,
  27093. 0x57, 0x8F, 0xF3, 0x21, 0xFF, 0xEA, 0x57, 0xE9,
  27094. 0xD8, 0x81, 0x04, 0x9A, 0x1F, 0x92, 0xB7, 0x4A,
  27095. 0xFC, 0xB5, 0x5B, 0xA5, 0x29, 0x1F, 0xF5, 0x25,
  27096. 0x5D, 0x91, 0x11, 0xAA, 0x01, 0x08, 0x52, 0x5A,
  27097. 0xA3, 0x65, 0x33, 0xB4, 0xC2, 0xF0, 0x0C, 0x49,
  27098. 0x73, 0x07, 0x75, 0x67, 0x5C, 0x17, 0x8F, 0xE2,
  27099. 0x80, 0x83, 0x0B, 0xBC, 0x0A, 0xDA, 0x08, 0xC3,
  27100. 0xB3, 0x55, 0x19, 0x90, 0x78, 0x4F, 0xD5, 0x5A,
  27101. 0x12, 0x8B, 0x6A, 0x29, 0xF9, 0xB7, 0xA0, 0xF1,
  27102. 0x68, 0xD5, 0xC2, 0x9D, 0xEC, 0xC9, 0x63, 0x82,
  27103. 0x83, 0x9A, 0xCD, 0xEA, 0x32, 0xA8, 0xD4, 0x90,
  27104. 0x48, 0xFC, 0x47, 0x1D, 0x9B, 0x23, 0xB3, 0xAC,
  27105. 0xBE, 0x7D, 0x16, 0x94, 0x4A, 0xF6, 0x5A, 0x0B,
  27106. 0x76, 0x2D, 0xFF, 0xBA, 0x03, 0xA5, 0x25, 0xCB,
  27107. 0x69, 0x9B, 0x03, 0xB0, 0x7A, 0x2C, 0xFE, 0x55,
  27108. 0x22, 0x1E, 0x52, 0x68, 0x8A, 0xA2, 0xBF, 0xCB,
  27109. 0x8A, 0xA7, 0x3D, 0x75, 0x22, 0x28, 0x29, 0x88,
  27110. 0x8F, 0xA7, 0xC9, 0x97, 0x2A, 0xCD, 0x04, 0x01,
  27111. 0x60, 0xC1, 0x43, 0x25, 0x07, 0x78, 0x65, 0x00,
  27112. 0x08, 0x8A, 0x0A, 0xD5, 0x09, 0xC4, 0x45, 0xBB,
  27113. 0x5B, 0xF1, 0xCF, 0x40, 0x92, 0xAA, 0x74, 0xE1,
  27114. 0x50, 0x34, 0x30, 0x07, 0xE9, 0x00, 0x16, 0xA2,
  27115. 0xF4, 0xBD, 0x6A, 0xE8, 0x6A, 0x71, 0x76, 0x37,
  27116. 0xED, 0x5C, 0x7C, 0x9E, 0x1C, 0xCD, 0x4C, 0x78,
  27117. 0xAF, 0xA2, 0x74, 0xA5, 0x66, 0xB1, 0x7E, 0x5F,
  27118. 0xC4, 0x33, 0x26, 0x7C, 0x40, 0xBD, 0x16, 0x40,
  27119. 0x3C, 0x53, 0x2C, 0x2B, 0x9A, 0xBF, 0xA0, 0xC6,
  27120. 0x25, 0x1B, 0x8A, 0xAD, 0x27, 0xB8, 0x97, 0x3D,
  27121. 0x54, 0x87, 0x0B, 0x50, 0xB9, 0xDE, 0x37, 0xAE,
  27122. 0x5F, 0xA5, 0x6F, 0x79, 0xF9, 0x75, 0x77, 0xE6,
  27123. 0x35, 0xC7, 0x03, 0xBC, 0xBB, 0x02, 0x82, 0x67,
  27124. 0xB5, 0x44, 0x87, 0x96, 0xA0, 0xDC, 0xE0, 0x34,
  27125. 0xE2, 0xCB, 0xBB, 0x09, 0x27, 0x6F, 0x49, 0x50,
  27126. 0xBE, 0x02, 0x23, 0xCC, 0x97, 0xC0, 0x0A, 0x66,
  27127. 0xBB, 0x32, 0x1B, 0x11, 0x4D, 0x5F, 0xB1, 0xBB,
  27128. 0x5A, 0x6C, 0x97, 0x3C, 0xEC, 0x1B, 0x53, 0xDB,
  27129. 0x26, 0x30, 0x81, 0x44, 0x74, 0x76, 0x99, 0x3E,
  27130. 0x68, 0x18, 0xA6, 0xD6, 0xCE, 0x4D, 0x05, 0x61,
  27131. 0xFD, 0xBA, 0x94, 0x1B, 0xD9, 0xBE, 0x17, 0x41,
  27132. 0x6B, 0xEA, 0xBB, 0x51, 0x1C, 0x61, 0xCD, 0xA4,
  27133. 0x12, 0x3D, 0x45, 0xB0, 0x09, 0xEA, 0x6C, 0x09,
  27134. 0xAD, 0x78, 0x09, 0xD4, 0x52, 0xBE, 0x93, 0x42,
  27135. 0x9E, 0x05, 0x39, 0x41, 0xBB, 0xA9, 0xB0, 0x65,
  27136. 0x99, 0x61, 0xBB, 0x02, 0x16, 0x60, 0x08, 0x7C,
  27137. 0xCD, 0x6C, 0x6E, 0x0D, 0xEA, 0xA6, 0xBE, 0xE5,
  27138. 0x87, 0x19, 0x3C, 0x4E, 0x1C, 0x13, 0x20, 0x04,
  27139. 0x5C, 0xB5, 0x09, 0xC7, 0xC0, 0x58, 0x4C, 0x43,
  27140. 0xCD, 0x65, 0x0F, 0xD7, 0x82, 0x65, 0x20, 0x24,
  27141. 0x36, 0xC7, 0xC9, 0x31, 0x75, 0xFB, 0xCB, 0x3B,
  27142. 0x55, 0x69, 0x4A, 0xB9, 0xB8, 0xAE, 0x99, 0xC2,
  27143. 0x69, 0x2D, 0x3C, 0x8E, 0x8C, 0x73, 0x9F, 0xB3,
  27144. 0x78, 0x27, 0x74, 0x15, 0x61, 0xD5, 0xE7, 0x66,
  27145. 0x67, 0x51, 0x82, 0xFF, 0x83, 0xC9, 0x1C, 0xF0,
  27146. 0x33, 0x65, 0x88, 0x23, 0xD8, 0xFA, 0x91, 0xE1
  27147. };
  27148. const byte kyber512_sk[] = {
  27149. 0xD3, 0xBA, 0x7A, 0x3B, 0x91, 0x56, 0x15, 0x32,
  27150. 0x1E, 0xD4, 0xE4, 0x19, 0x6B, 0xC7, 0x34, 0xD0,
  27151. 0x75, 0xB7, 0x40, 0x56, 0xC9, 0x7C, 0x83, 0x00,
  27152. 0x78, 0xF0, 0x28, 0xDD, 0xB3, 0x4E, 0xEF, 0x76,
  27153. 0x9A, 0x22, 0x31, 0xC6, 0x60, 0x05, 0x9A, 0x56,
  27154. 0xA0, 0x9A, 0x64, 0x10, 0x8C, 0x7C, 0x0B, 0xC4,
  27155. 0x8B, 0x0A, 0x5E, 0xD3, 0x47, 0xA4, 0xA8, 0xBC,
  27156. 0x7D, 0xA1, 0x70, 0x74, 0xF4, 0xE3, 0x3E, 0x8A,
  27157. 0x13, 0x0B, 0x2F, 0x67, 0x64, 0x74, 0x63, 0x50,
  27158. 0x0B, 0x50, 0x1E, 0x8C, 0x41, 0x42, 0x8C, 0x87,
  27159. 0x85, 0x63, 0x19, 0x6C, 0x99, 0x40, 0x2B, 0x1F,
  27160. 0x4A, 0x17, 0x9E, 0x18, 0xC2, 0xF8, 0x8B, 0xA5,
  27161. 0x75, 0xD1, 0x5C, 0xF2, 0xF0, 0xA8, 0xDC, 0x3C,
  27162. 0x8A, 0x28, 0x2B, 0x3E, 0xF1, 0x18, 0x1F, 0xAA,
  27163. 0x28, 0x9B, 0x81, 0xF0, 0xA9, 0xBE, 0x47, 0xC3,
  27164. 0xFA, 0xFC, 0xCB, 0x18, 0xAC, 0x08, 0xF8, 0x6C,
  27165. 0xA2, 0xF3, 0xBB, 0x06, 0x99, 0xE5, 0x7E, 0xAD,
  27166. 0x61, 0x0E, 0x6A, 0xA7, 0x02, 0xD1, 0x09, 0xC4,
  27167. 0xF5, 0x04, 0x33, 0xA4, 0x74, 0x2B, 0x74, 0xDC,
  27168. 0x75, 0x27, 0x8B, 0x22, 0x46, 0xA8, 0xC1, 0xE4,
  27169. 0xC3, 0x73, 0xDC, 0x03, 0x85, 0x17, 0xB4, 0x8A,
  27170. 0x0B, 0xDC, 0xB3, 0xB3, 0xB1, 0x9F, 0x5F, 0xFA,
  27171. 0xBD, 0x65, 0x11, 0xAA, 0xD5, 0x4C, 0xCE, 0xB6,
  27172. 0x3C, 0xB9, 0x40, 0xCC, 0xB3, 0xF9, 0x40, 0x75,
  27173. 0xDF, 0x03, 0x19, 0x7D, 0x81, 0xAD, 0x2D, 0x8A,
  27174. 0x8D, 0x35, 0xBC, 0x01, 0x1C, 0xCA, 0xCF, 0xE5,
  27175. 0xB0, 0xA8, 0x21, 0xC1, 0xAD, 0x04, 0x6B, 0xC2,
  27176. 0x0B, 0xD4, 0x48, 0xAB, 0x98, 0xAE, 0x49, 0x46,
  27177. 0x7F, 0xA7, 0xA2, 0xCB, 0x2B, 0xF1, 0x30, 0x32,
  27178. 0xA1, 0x3E, 0x98, 0x65, 0x08, 0x10, 0x0A, 0x22,
  27179. 0x1D, 0x55, 0xAD, 0x97, 0x91, 0xAE, 0x47, 0x37,
  27180. 0x91, 0xCA, 0xA3, 0x18, 0x91, 0x5B, 0x58, 0x3F,
  27181. 0xD4, 0x73, 0x91, 0xC2, 0x31, 0xD9, 0x0B, 0xB7,
  27182. 0x29, 0xB2, 0x06, 0x2A, 0x0A, 0xC4, 0x30, 0x91,
  27183. 0xAF, 0x14, 0x05, 0x02, 0xB0, 0x62, 0x42, 0xFD,
  27184. 0x65, 0x04, 0xFB, 0x74, 0x9C, 0x20, 0x93, 0xCD,
  27185. 0x78, 0x00, 0x63, 0x4B, 0x48, 0xBA, 0xFC, 0xE0,
  27186. 0xB1, 0xDC, 0x22, 0x2A, 0x42, 0xB9, 0x70, 0xB7,
  27187. 0x34, 0x26, 0x02, 0x1B, 0xC0, 0x8F, 0xE7, 0x10,
  27188. 0x98, 0x6B, 0x3E, 0xD6, 0x47, 0x9C, 0x21, 0xEB,
  27189. 0xBB, 0x9D, 0x94, 0x6D, 0x13, 0x3A, 0x97, 0x55,
  27190. 0xB7, 0x33, 0xB0, 0xBA, 0x79, 0x5D, 0x34, 0xAA,
  27191. 0x2C, 0x43, 0x6E, 0xB3, 0x04, 0x87, 0xFE, 0x19,
  27192. 0x3C, 0xDE, 0x86, 0xC7, 0x54, 0x00, 0x14, 0x85,
  27193. 0x48, 0x4C, 0x4D, 0x28, 0x89, 0xD9, 0x67, 0x66,
  27194. 0x7E, 0xD1, 0x72, 0xAB, 0xE4, 0x52, 0x5C, 0x05,
  27195. 0xA7, 0xE9, 0x53, 0x8E, 0x1B, 0x0C, 0x38, 0xBE,
  27196. 0x40, 0x7D, 0x3F, 0x09, 0x72, 0xEA, 0x82, 0xB9,
  27197. 0xA7, 0xB1, 0x55, 0x0B, 0x2A, 0x20, 0x68, 0xFA,
  27198. 0x80, 0x56, 0x86, 0xB2, 0x7F, 0x50, 0xAD, 0xCC,
  27199. 0x47, 0x30, 0xCA, 0xF9, 0x04, 0x28, 0x74, 0x11,
  27200. 0xED, 0xB4, 0x82, 0x46, 0x36, 0x6C, 0x4B, 0x02,
  27201. 0xB7, 0x95, 0x96, 0x69, 0x81, 0x56, 0xA0, 0xE4,
  27202. 0x89, 0x65, 0x2D, 0xA5, 0xC2, 0x2B, 0x51, 0x24,
  27203. 0xD7, 0x90, 0x33, 0xD9, 0x84, 0x0D, 0x51, 0x93,
  27204. 0x39, 0xF2, 0xA9, 0x90, 0x72, 0x1C, 0x73, 0x1B,
  27205. 0x02, 0xC2, 0x36, 0xBC, 0x72, 0x8C, 0x22, 0x61,
  27206. 0xFF, 0x18, 0x18, 0xA3, 0x8A, 0x4D, 0x39, 0x54,
  27207. 0x61, 0xAE, 0x20, 0xC4, 0x2E, 0x12, 0xB0, 0x1C,
  27208. 0xBC, 0x85, 0xFE, 0xC8, 0x9E, 0x9F, 0x75, 0x9D,
  27209. 0x20, 0xE3, 0x87, 0x4F, 0x37, 0x16, 0x0E, 0x19,
  27210. 0x5C, 0x4C, 0x61, 0x47, 0x54, 0xC5, 0x93, 0x03,
  27211. 0xD0, 0x9B, 0x27, 0x76, 0xAE, 0xF6, 0x8C, 0x5B,
  27212. 0xF6, 0x1C, 0x4B, 0x50, 0x47, 0x42, 0x41, 0x3A,
  27213. 0xC3, 0xD7, 0xC4, 0x95, 0x7C, 0x47, 0x0B, 0xC2,
  27214. 0x9A, 0x72, 0x2C, 0xC3, 0x7F, 0xB2, 0xB2, 0x30,
  27215. 0x45, 0x7A, 0x3A, 0x98, 0x8A, 0xBE, 0x48, 0x96,
  27216. 0x71, 0x70, 0x54, 0x4F, 0xD2, 0x57, 0xBB, 0x89,
  27217. 0xEC, 0xC3, 0x13, 0xF1, 0xBD, 0x33, 0x10, 0xCA,
  27218. 0x37, 0xB0, 0x2D, 0x6E, 0x97, 0x07, 0xCA, 0x91,
  27219. 0xB6, 0x25, 0x97, 0x7B, 0xED, 0xB3, 0x1B, 0xA8,
  27220. 0x27, 0x48, 0xEE, 0x31, 0xAF, 0x2B, 0xC7, 0x62,
  27221. 0x92, 0xA7, 0x2B, 0xCF, 0xA0, 0x46, 0xE9, 0x31,
  27222. 0x48, 0xBE, 0x8C, 0x83, 0xB2, 0x58, 0x50, 0x7D,
  27223. 0x96, 0x6F, 0x14, 0xD2, 0x02, 0x81, 0xA3, 0x33,
  27224. 0x3E, 0x69, 0xAB, 0x78, 0x60, 0x0D, 0x3F, 0x3B,
  27225. 0x6B, 0xAF, 0x7A, 0xC9, 0xAC, 0xEA, 0xB5, 0x5B,
  27226. 0xB2, 0x0C, 0xE7, 0x5C, 0x13, 0x47, 0x4A, 0x67,
  27227. 0x8E, 0x7C, 0x60, 0xC0, 0x81, 0x87, 0x1E, 0xF3,
  27228. 0x3C, 0xA9, 0xEC, 0x6C, 0xD6, 0xA3, 0x64, 0x3B,
  27229. 0x6A, 0x41, 0x60, 0xF8, 0x3A, 0x27, 0x8C, 0x21,
  27230. 0x0C, 0xA2, 0xBC, 0xB9, 0xE0, 0x12, 0x27, 0x01,
  27231. 0xC9, 0xAD, 0xF0, 0x45, 0x8E, 0xF4, 0x71, 0x9F,
  27232. 0xB5, 0xB4, 0x01, 0xB3, 0xB5, 0x16, 0xE1, 0xAD,
  27233. 0x25, 0xD7, 0xA4, 0x84, 0xA6, 0x3E, 0xE4, 0x1A,
  27234. 0x81, 0x44, 0xE1, 0xAA, 0x38, 0x8A, 0xC8, 0xAE,
  27235. 0x69, 0x00, 0xCE, 0x87, 0x9E, 0x9F, 0xD7, 0x4F,
  27236. 0x47, 0xC7, 0x30, 0x78, 0xCC, 0x6E, 0x85, 0xC7,
  27237. 0x64, 0x15, 0x0C, 0x00, 0x3A, 0x58, 0x70, 0x56,
  27238. 0x97, 0x33, 0xDA, 0x66, 0x8B, 0xDA, 0x15, 0xC5,
  27239. 0xA2, 0x69, 0x9D, 0x38, 0x25, 0x82, 0x6B, 0x36,
  27240. 0x98, 0x5A, 0x51, 0x08, 0x51, 0x0B, 0x6F, 0xC3,
  27241. 0x65, 0x4B, 0x17, 0x51, 0x02, 0xA4, 0xD9, 0x06,
  27242. 0x14, 0x47, 0xA7, 0x8B, 0xA9, 0x39, 0x40, 0x8B,
  27243. 0x9A, 0x94, 0x57, 0x61, 0xEC, 0x0C, 0x24, 0x6F,
  27244. 0x8A, 0x64, 0x18, 0xF6, 0x2F, 0x1E, 0xA3, 0x00,
  27245. 0x68, 0xD6, 0x83, 0xF6, 0x01, 0x8F, 0xCB, 0x38,
  27246. 0x71, 0xC0, 0x64, 0x0A, 0xD1, 0x7B, 0x7E, 0x80,
  27247. 0x02, 0x93, 0xC9, 0xF6, 0x36, 0xC1, 0xE2, 0x09,
  27248. 0x10, 0x34, 0x25, 0xAD, 0x72, 0x26, 0xDD, 0x13,
  27249. 0x90, 0x41, 0xFA, 0x84, 0x9F, 0x8C, 0x2F, 0xED,
  27250. 0x18, 0xAB, 0xEF, 0xF5, 0x2A, 0xE0, 0xB6, 0x06,
  27251. 0x86, 0x13, 0xBA, 0x05, 0x73, 0x09, 0x3D, 0xE8,
  27252. 0xAC, 0xC8, 0x56, 0x19, 0x80, 0x62, 0xCD, 0x38,
  27253. 0xDC, 0x0F, 0x80, 0x05, 0x6B, 0x8D, 0x1C, 0x42,
  27254. 0xDF, 0xA3, 0x2C, 0x4C, 0xA4, 0x0E, 0x8C, 0x57,
  27255. 0x03, 0xF3, 0x13, 0xBA, 0x5F, 0xD0, 0x5C, 0x1E,
  27256. 0x75, 0xC3, 0x78, 0xD7, 0x04, 0x77, 0x62, 0x1A,
  27257. 0xF3, 0xA0, 0x8E, 0x52, 0x0A, 0x16, 0x05, 0xC4,
  27258. 0x89, 0x42, 0x2C, 0x97, 0xD0, 0xD9, 0x2E, 0x43,
  27259. 0x12, 0xBB, 0xEF, 0xB6, 0xC0, 0x18, 0x7A, 0x45,
  27260. 0xF9, 0x24, 0x6B, 0xF8, 0xEB, 0xA6, 0x2C, 0x04,
  27261. 0x7F, 0x8C, 0xB2, 0x42, 0x17, 0xB2, 0x01, 0x78,
  27262. 0xBB, 0x09, 0x5E, 0x59, 0x63, 0x6B, 0xCB, 0xA9,
  27263. 0x13, 0x73, 0x52, 0x65, 0xC7, 0xA0, 0x90, 0x06,
  27264. 0x12, 0xEF, 0x16, 0x41, 0xC9, 0xE1, 0x4D, 0x7D,
  27265. 0x89, 0x15, 0xE3, 0xC8, 0x83, 0xB7, 0x50, 0x68,
  27266. 0xA9, 0xB1, 0x3B, 0x0C, 0xF0, 0xA1, 0x32, 0x8A,
  27267. 0x3E, 0xC5, 0xD6, 0x00, 0x1E, 0xE9, 0x6D, 0x01,
  27268. 0x05, 0x6C, 0x25, 0x09, 0xC2, 0x16, 0x51, 0x4D,
  27269. 0xA5, 0x24, 0x61, 0xF7, 0xCA, 0x6A, 0xC4, 0x1A,
  27270. 0x2C, 0x0E, 0x35, 0x2B, 0x53, 0x63, 0x68, 0x99,
  27271. 0xD0, 0x65, 0xBD, 0xF2, 0xBF, 0x14, 0xF5, 0x35,
  27272. 0xFC, 0xE9, 0x42, 0x93, 0x9B, 0x6B, 0x4B, 0x97,
  27273. 0x72, 0x04, 0xD6, 0x29, 0xA6, 0x24, 0x1F, 0xFD,
  27274. 0x99, 0x94, 0x6A, 0x70, 0xBC, 0xA8, 0x91, 0x2B,
  27275. 0x1D, 0xFA, 0x61, 0x4A, 0xE9, 0x91, 0x3C, 0xB3,
  27276. 0x65, 0x63, 0x04, 0x7F, 0x9F, 0xA0, 0xA6, 0x48,
  27277. 0x7B, 0xA4, 0x10, 0x19, 0x5F, 0x56, 0x17, 0x58,
  27278. 0x84, 0x41, 0xC6, 0xA1, 0xA1, 0x50, 0x78, 0xBA,
  27279. 0x60, 0x93, 0x36, 0x79, 0x64, 0xD2, 0x5F, 0x19,
  27280. 0xD7, 0x15, 0x1A, 0xE0, 0x9F, 0xAF, 0xA6, 0xA4,
  27281. 0xCB, 0x63, 0x09, 0x40, 0xC5, 0x8F, 0x88, 0xEB,
  27282. 0x77, 0x8B, 0xFA, 0x44, 0xD7, 0x3B, 0x30, 0x63,
  27283. 0x74, 0x46, 0x8A, 0xBB, 0x8B, 0x56, 0x16, 0x0D,
  27284. 0x49, 0xA2, 0x4E, 0x6F, 0x43, 0x2B, 0xBF, 0xE8,
  27285. 0x8D, 0xE0, 0xC5, 0x6E, 0x9A, 0xE2, 0x13, 0x23,
  27286. 0xF1, 0x2D, 0xEE, 0xFC, 0xAF, 0x32, 0xF6, 0x8C,
  27287. 0x46, 0x21, 0x2F, 0xE3, 0x68, 0x3F, 0xB0, 0x48,
  27288. 0xB7, 0xE2, 0x4C, 0xB8, 0x17, 0xF8, 0x7E, 0xC4,
  27289. 0xA4, 0x2F, 0xE1, 0x1B, 0x21, 0xEA, 0x54, 0x4E,
  27290. 0xB7, 0xD4, 0x82, 0x89, 0xAA, 0xB4, 0xB3, 0x9C,
  27291. 0x57, 0x8F, 0xF3, 0x21, 0xFF, 0xEA, 0x57, 0xE9,
  27292. 0xD8, 0x81, 0x04, 0x9A, 0x1F, 0x92, 0xB7, 0x4A,
  27293. 0xFC, 0xB5, 0x5B, 0xA5, 0x29, 0x1F, 0xF5, 0x25,
  27294. 0x5D, 0x91, 0x11, 0xAA, 0x01, 0x08, 0x52, 0x5A,
  27295. 0xA3, 0x65, 0x33, 0xB4, 0xC2, 0xF0, 0x0C, 0x49,
  27296. 0x73, 0x07, 0x75, 0x67, 0x5C, 0x17, 0x8F, 0xE2,
  27297. 0x80, 0x83, 0x0B, 0xBC, 0x0A, 0xDA, 0x08, 0xC3,
  27298. 0xB3, 0x55, 0x19, 0x90, 0x78, 0x4F, 0xD5, 0x5A,
  27299. 0x12, 0x8B, 0x6A, 0x29, 0xF9, 0xB7, 0xA0, 0xF1,
  27300. 0x68, 0xD5, 0xC2, 0x9D, 0xEC, 0xC9, 0x63, 0x82,
  27301. 0x83, 0x9A, 0xCD, 0xEA, 0x32, 0xA8, 0xD4, 0x90,
  27302. 0x48, 0xFC, 0x47, 0x1D, 0x9B, 0x23, 0xB3, 0xAC,
  27303. 0xBE, 0x7D, 0x16, 0x94, 0x4A, 0xF6, 0x5A, 0x0B,
  27304. 0x76, 0x2D, 0xFF, 0xBA, 0x03, 0xA5, 0x25, 0xCB,
  27305. 0x69, 0x9B, 0x03, 0xB0, 0x7A, 0x2C, 0xFE, 0x55,
  27306. 0x22, 0x1E, 0x52, 0x68, 0x8A, 0xA2, 0xBF, 0xCB,
  27307. 0x8A, 0xA7, 0x3D, 0x75, 0x22, 0x28, 0x29, 0x88,
  27308. 0x8F, 0xA7, 0xC9, 0x97, 0x2A, 0xCD, 0x04, 0x01,
  27309. 0x60, 0xC1, 0x43, 0x25, 0x07, 0x78, 0x65, 0x00,
  27310. 0x08, 0x8A, 0x0A, 0xD5, 0x09, 0xC4, 0x45, 0xBB,
  27311. 0x5B, 0xF1, 0xCF, 0x40, 0x92, 0xAA, 0x74, 0xE1,
  27312. 0x50, 0x34, 0x30, 0x07, 0xE9, 0x00, 0x16, 0xA2,
  27313. 0xF4, 0xBD, 0x6A, 0xE8, 0x6A, 0x71, 0x76, 0x37,
  27314. 0xED, 0x5C, 0x7C, 0x9E, 0x1C, 0xCD, 0x4C, 0x78,
  27315. 0xAF, 0xA2, 0x74, 0xA5, 0x66, 0xB1, 0x7E, 0x5F,
  27316. 0xC4, 0x33, 0x26, 0x7C, 0x40, 0xBD, 0x16, 0x40,
  27317. 0x3C, 0x53, 0x2C, 0x2B, 0x9A, 0xBF, 0xA0, 0xC6,
  27318. 0x25, 0x1B, 0x8A, 0xAD, 0x27, 0xB8, 0x97, 0x3D,
  27319. 0x54, 0x87, 0x0B, 0x50, 0xB9, 0xDE, 0x37, 0xAE,
  27320. 0x5F, 0xA5, 0x6F, 0x79, 0xF9, 0x75, 0x77, 0xE6,
  27321. 0x35, 0xC7, 0x03, 0xBC, 0xBB, 0x02, 0x82, 0x67,
  27322. 0xB5, 0x44, 0x87, 0x96, 0xA0, 0xDC, 0xE0, 0x34,
  27323. 0xE2, 0xCB, 0xBB, 0x09, 0x27, 0x6F, 0x49, 0x50,
  27324. 0xBE, 0x02, 0x23, 0xCC, 0x97, 0xC0, 0x0A, 0x66,
  27325. 0xBB, 0x32, 0x1B, 0x11, 0x4D, 0x5F, 0xB1, 0xBB,
  27326. 0x5A, 0x6C, 0x97, 0x3C, 0xEC, 0x1B, 0x53, 0xDB,
  27327. 0x26, 0x30, 0x81, 0x44, 0x74, 0x76, 0x99, 0x3E,
  27328. 0x68, 0x18, 0xA6, 0xD6, 0xCE, 0x4D, 0x05, 0x61,
  27329. 0xFD, 0xBA, 0x94, 0x1B, 0xD9, 0xBE, 0x17, 0x41,
  27330. 0x6B, 0xEA, 0xBB, 0x51, 0x1C, 0x61, 0xCD, 0xA4,
  27331. 0x12, 0x3D, 0x45, 0xB0, 0x09, 0xEA, 0x6C, 0x09,
  27332. 0xAD, 0x78, 0x09, 0xD4, 0x52, 0xBE, 0x93, 0x42,
  27333. 0x9E, 0x05, 0x39, 0x41, 0xBB, 0xA9, 0xB0, 0x65,
  27334. 0x99, 0x61, 0xBB, 0x02, 0x16, 0x60, 0x08, 0x7C,
  27335. 0xCD, 0x6C, 0x6E, 0x0D, 0xEA, 0xA6, 0xBE, 0xE5,
  27336. 0x87, 0x19, 0x3C, 0x4E, 0x1C, 0x13, 0x20, 0x04,
  27337. 0x5C, 0xB5, 0x09, 0xC7, 0xC0, 0x58, 0x4C, 0x43,
  27338. 0xCD, 0x65, 0x0F, 0xD7, 0x82, 0x65, 0x20, 0x24,
  27339. 0x36, 0xC7, 0xC9, 0x31, 0x75, 0xFB, 0xCB, 0x3B,
  27340. 0x55, 0x69, 0x4A, 0xB9, 0xB8, 0xAE, 0x99, 0xC2,
  27341. 0x69, 0x2D, 0x3C, 0x8E, 0x8C, 0x73, 0x9F, 0xB3,
  27342. 0x78, 0x27, 0x74, 0x15, 0x61, 0xD5, 0xE7, 0x66,
  27343. 0x67, 0x51, 0x82, 0xFF, 0x83, 0xC9, 0x1C, 0xF0,
  27344. 0x33, 0x65, 0x88, 0x23, 0xD8, 0xFA, 0x91, 0xE1,
  27345. 0xC1, 0xC0, 0x8E, 0xFF, 0x2A, 0xB6, 0xE2, 0xAF,
  27346. 0x9F, 0x29, 0xA6, 0x32, 0xC5, 0x93, 0x32, 0xE9,
  27347. 0xA6, 0x1B, 0x63, 0x14, 0x6E, 0x8A, 0xC3, 0x5E,
  27348. 0xF3, 0xD3, 0xC4, 0x5A, 0x10, 0x10, 0xAC, 0xE2,
  27349. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  27350. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  27351. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  27352. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  27353. };
  27354. const byte kyber512_ct[] = {
  27355. 0x58, 0x56, 0xAE, 0x75, 0x76, 0x21, 0xFD, 0x94,
  27356. 0x9B, 0xA5, 0x49, 0xD2, 0x49, 0x70, 0x52, 0x5F,
  27357. 0x17, 0x95, 0x60, 0xE3, 0x24, 0xF7, 0x1B, 0x3C,
  27358. 0x1F, 0xDC, 0xFC, 0xAF, 0x92, 0xFD, 0x7F, 0xED,
  27359. 0x6B, 0x35, 0x1F, 0x0D, 0xA4, 0x1C, 0x98, 0x51,
  27360. 0x24, 0x9F, 0x6E, 0x63, 0xB7, 0xBC, 0xE3, 0x4F,
  27361. 0xD4, 0x9C, 0x97, 0x70, 0x58, 0x8B, 0x94, 0x2D,
  27362. 0x1B, 0x51, 0x69, 0x57, 0xDB, 0x3A, 0xE9, 0x7C,
  27363. 0x5F, 0x94, 0x51, 0x8D, 0x40, 0xF5, 0x4B, 0x5B,
  27364. 0x78, 0xE1, 0xF3, 0x8F, 0x8D, 0x61, 0x27, 0xA5,
  27365. 0xD0, 0xD5, 0xD4, 0x24, 0x29, 0xEA, 0x79, 0x62,
  27366. 0x13, 0xC0, 0x77, 0x2D, 0x5C, 0x9B, 0x99, 0x2C,
  27367. 0xEC, 0x72, 0x1B, 0x52, 0x17, 0xB6, 0x91, 0x7F,
  27368. 0xF8, 0xC0, 0xCC, 0xBB, 0xCC, 0xFE, 0x13, 0x4B,
  27369. 0xD8, 0x9A, 0x99, 0x48, 0x0B, 0x95, 0x66, 0xE4,
  27370. 0x69, 0x60, 0xBD, 0x21, 0x8C, 0xAC, 0x2D, 0xDD,
  27371. 0x58, 0x00, 0xB0, 0x83, 0x01, 0x9F, 0x09, 0x42,
  27372. 0x0E, 0x8B, 0xA2, 0x8E, 0x1E, 0x4D, 0xCC, 0x51,
  27373. 0xD4, 0xF5, 0xF9, 0x57, 0xD4, 0x57, 0x5B, 0xC3,
  27374. 0x2C, 0xF3, 0xDD, 0x08, 0x15, 0xD7, 0xE1, 0xEC,
  27375. 0xDA, 0x47, 0x44, 0x3B, 0x34, 0xCD, 0x88, 0x68,
  27376. 0xE7, 0x39, 0xD1, 0x57, 0x90, 0x29, 0x37, 0x12,
  27377. 0xBF, 0xF0, 0x89, 0x19, 0x8D, 0xFF, 0xA8, 0x42,
  27378. 0xD9, 0x5C, 0x03, 0x68, 0x49, 0x6C, 0x3E, 0x7D,
  27379. 0xFA, 0x06, 0xC8, 0x99, 0x9B, 0x9A, 0xEA, 0x3B,
  27380. 0x09, 0xC6, 0xD3, 0x62, 0x80, 0xF7, 0x6D, 0xD9,
  27381. 0xC8, 0xC1, 0x58, 0x53, 0xB4, 0x5E, 0xBC, 0xCA,
  27382. 0x5E, 0xC8, 0xF9, 0xB1, 0xF5, 0x7C, 0xFA, 0x6A,
  27383. 0x60, 0x19, 0xA0, 0x52, 0xCD, 0x9D, 0xD9, 0x5F,
  27384. 0x6F, 0x25, 0x97, 0x3D, 0xFA, 0xB9, 0x79, 0x7B,
  27385. 0x3F, 0xDB, 0xDC, 0x2C, 0xEF, 0xCE, 0x8B, 0x0B,
  27386. 0x35, 0x33, 0xA3, 0xA8, 0x54, 0x96, 0xA4, 0x11,
  27387. 0x09, 0xF6, 0x14, 0xB0, 0x53, 0xB0, 0x8E, 0x48,
  27388. 0xFE, 0xA9, 0x2F, 0xA5, 0x61, 0x07, 0xB5, 0x24,
  27389. 0xDD, 0x28, 0x77, 0x93, 0xA0, 0x24, 0x50, 0x03,
  27390. 0xEA, 0x35, 0xA0, 0x4E, 0x03, 0xEA, 0x59, 0x3D,
  27391. 0xAA, 0xFE, 0x31, 0x24, 0xB1, 0x7D, 0x0F, 0x2D,
  27392. 0xC3, 0xBE, 0xAE, 0xD3, 0x48, 0xD8, 0x96, 0xD1,
  27393. 0x65, 0xE5, 0x24, 0x08, 0x67, 0xA2, 0xCB, 0x72,
  27394. 0x3A, 0xA3, 0x88, 0x62, 0xC0, 0x47, 0x45, 0x75,
  27395. 0x40, 0xB4, 0xA4, 0xC0, 0x06, 0xF8, 0x22, 0xBE,
  27396. 0xEF, 0xB6, 0xF2, 0x75, 0xF5, 0x82, 0x8D, 0x3B,
  27397. 0x79, 0xC5, 0x3F, 0x87, 0x5E, 0x3C, 0x5B, 0xD4,
  27398. 0xA7, 0x66, 0x5C, 0xD1, 0x0C, 0xFA, 0x25, 0x1B,
  27399. 0x79, 0xE7, 0x6D, 0x26, 0xC5, 0xDB, 0x24, 0x25,
  27400. 0x72, 0xE5, 0xD8, 0x11, 0xC4, 0x60, 0x04, 0xB0,
  27401. 0xCC, 0xAB, 0x1C, 0xE5, 0x00, 0x2C, 0xA1, 0xDE,
  27402. 0x61, 0x4F, 0x31, 0x0B, 0x30, 0x9D, 0xFA, 0x75,
  27403. 0x63, 0xD0, 0x66, 0x23, 0x03, 0x6C, 0x7C, 0x26,
  27404. 0x17, 0x96, 0x40, 0x26, 0x45, 0x03, 0x60, 0xEF,
  27405. 0x52, 0x5B, 0x1B, 0xE7, 0xD8, 0x16, 0xEA, 0x1D,
  27406. 0xDF, 0xA1, 0x07, 0x15, 0xA1, 0x14, 0x86, 0x9E,
  27407. 0x62, 0x70, 0xC0, 0x3C, 0xDC, 0x58, 0x52, 0xD7,
  27408. 0x20, 0x46, 0x81, 0xB0, 0xF4, 0xD1, 0xB2, 0xA3,
  27409. 0x88, 0xAC, 0x06, 0x1B, 0x99, 0xAE, 0x0C, 0x01,
  27410. 0x1A, 0xFF, 0x0D, 0x3D, 0x89, 0x6B, 0xFF, 0xE6,
  27411. 0x27, 0x2D, 0xCF, 0x30, 0x9B, 0xB4, 0xF8, 0x6E,
  27412. 0xF4, 0xB5, 0x8B, 0xA8, 0xF4, 0xA1, 0x41, 0x15,
  27413. 0x38, 0x83, 0x4B, 0xAE, 0x55, 0x22, 0xBB, 0x51,
  27414. 0x3B, 0xF3, 0x56, 0xA5, 0x20, 0x3F, 0xE8, 0xAA,
  27415. 0x57, 0x5A, 0xCC, 0xD0, 0x94, 0xC9, 0x55, 0x2C,
  27416. 0xC7, 0xB6, 0x40, 0x75, 0x2C, 0xDE, 0x80, 0xD1,
  27417. 0x5F, 0x7F, 0x6B, 0xE0, 0x88, 0xA1, 0x2C, 0x5C,
  27418. 0x58, 0x47, 0xDD, 0x56, 0xCA, 0x66, 0x26, 0x7C,
  27419. 0x08, 0xDC, 0xD5, 0x16, 0x70, 0x6F, 0x3A, 0xE9,
  27420. 0x0F, 0x17, 0xA7, 0x42, 0x6A, 0x1B, 0x24, 0x44,
  27421. 0x05, 0x6E, 0x89, 0x5B, 0x77, 0x6C, 0x1E, 0x7A,
  27422. 0xE1, 0x65, 0xE4, 0xE9, 0x5D, 0x62, 0x0B, 0x20,
  27423. 0x54, 0xE5, 0x00, 0x5C, 0x62, 0x2C, 0x97, 0x03,
  27424. 0x4F, 0x96, 0x39, 0x14, 0x28, 0x0A, 0x0A, 0x06,
  27425. 0x2C, 0x2A, 0x63, 0x0E, 0x65, 0xCF, 0x64, 0xF2,
  27426. 0xD7, 0x0C, 0xD6, 0x0F, 0xE1, 0x57, 0xE3, 0x20,
  27427. 0xB7, 0xE2, 0xD6, 0x33, 0x2C, 0xDB, 0x73, 0x5E,
  27428. 0xA9, 0x8B, 0xEB, 0x1E, 0x2E, 0x01, 0x5C, 0x0A,
  27429. 0x2B, 0x33, 0xD7, 0x0F, 0xE7, 0x68, 0xF1, 0x77,
  27430. 0xE6, 0x23, 0x91, 0x04, 0x88, 0xE8, 0xBB, 0x2F,
  27431. 0x83, 0x83, 0xE3, 0xE4, 0xA6, 0xE0, 0xFC, 0xA1,
  27432. 0x11, 0xF0, 0xA0, 0x2E, 0x57, 0x3B, 0xFB, 0x38,
  27433. 0xBC, 0x34, 0x73, 0x47, 0x08, 0x11, 0x62, 0x08,
  27434. 0xCA, 0x3E, 0x82, 0xEF, 0xE5, 0xC2, 0x15, 0x44,
  27435. 0x3D, 0x3F, 0xE9, 0x46, 0x3B, 0x61, 0x4D, 0xA1,
  27436. 0x7A, 0x09, 0x29, 0x37, 0xFB, 0x9C, 0xCB, 0xCE,
  27437. 0xE9, 0xC8, 0x84, 0x01, 0xCD, 0xC3, 0x55, 0x7E,
  27438. 0xF5, 0xBF, 0xFE, 0x89, 0x45, 0x2C, 0x42, 0x1A,
  27439. 0x6E, 0x8C, 0xB0, 0x0D, 0xA5, 0x00, 0xDB, 0x90,
  27440. 0x94, 0xA9, 0x92, 0xD5, 0x76, 0xAA, 0xCB, 0x2D,
  27441. 0x17, 0xB5, 0xF5, 0xEA, 0x5B, 0xC4, 0x58, 0x8E,
  27442. 0x2B, 0x2E, 0x08, 0x76, 0x9D, 0x62, 0x6C, 0x00,
  27443. 0x98, 0x0D, 0x51, 0x63, 0x9A, 0x43, 0xA0, 0xD0,
  27444. 0xEA, 0x5C, 0xCC, 0x26, 0x9E, 0x86, 0x8C, 0x1E,
  27445. 0xB1, 0xEE, 0xC8, 0x08, 0xCE, 0x64, 0x20, 0x8C,
  27446. 0x6B, 0xA0, 0x73, 0x5A, 0x0B, 0x42, 0x06, 0xB1,
  27447. 0x08, 0x28, 0xCE, 0x9B, 0x5E, 0x51, 0xCD, 0x5F,
  27448. 0x0E, 0xA5, 0x77, 0x7D, 0x0B, 0x7A, 0xCF, 0x14,
  27449. 0x42, 0xC9, 0xAA, 0xCC, 0x3E, 0x80, 0x87, 0x8C,
  27450. 0x4D, 0x7D, 0x3B, 0x6F, 0xDC, 0x56, 0xEF, 0x17
  27451. };
  27452. const byte kyber512_ss[] = {
  27453. 0x0C, 0x92, 0x39, 0xC7, 0x70, 0x5D, 0x63, 0x91,
  27454. 0x51, 0xAD, 0x1B, 0xCA, 0xDF, 0x58, 0xBD, 0x99,
  27455. 0x91, 0x0B, 0x7A, 0x12, 0x44, 0x99, 0x17, 0x21,
  27456. 0x13, 0x22, 0x8B, 0x4C, 0x75, 0xF8, 0x22, 0xE1
  27457. };
  27458. #endif
  27459. ret = wc_KyberKey_Init(KYBER512, &key, HEAP_HINT, INVALID_DEVID);
  27460. if (ret != 0)
  27461. return -20101;
  27462. ret = wc_KyberKey_MakeKeyWithRandom(&key, kyber512_rand,
  27463. sizeof(kyber512_rand));
  27464. if (ret != 0)
  27465. return -20102;
  27466. ret = wc_KyberKey_EncodePublicKey(&key, pub, sizeof(pub));
  27467. if (ret != 0)
  27468. return -20103;
  27469. ret = wc_KyberKey_EncodePrivateKey(&key, priv, sizeof(priv));
  27470. if (ret != 0)
  27471. return -20104;
  27472. if (XMEMCMP(pub, kyber512_pk, sizeof(kyber512_pk)) != 0)
  27473. return -20105;
  27474. if (XMEMCMP(priv, kyber512_sk, sizeof(kyber512_sk)) != 0)
  27475. return -20106;
  27476. ret = wc_KyberKey_EncapsulateWithRandom(&key, ct, ss, kyber512enc_rand,
  27477. sizeof(kyber512enc_rand));
  27478. if (ret != 0)
  27479. return -20107;
  27480. if (XMEMCMP(ct, kyber512_ct, sizeof(kyber512_ct)) != 0)
  27481. return -20108;
  27482. if (XMEMCMP(ss, kyber512_ss, sizeof(kyber512_ss)) != 0)
  27483. return -20109;
  27484. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, sizeof(kyber512_ct));
  27485. if (ret != 0)
  27486. return -20110;
  27487. if (XMEMCMP(ss_dec, kyber512_ss, sizeof(kyber512_ss)) != 0)
  27488. return -20111;
  27489. wc_KyberKey_Free(&key);
  27490. return 0;
  27491. }
  27492. #endif /* WOLFSSL_KYBER512 */
  27493. #ifdef WOLFSSL_KYBER768
  27494. static int kyber768_kat(void)
  27495. {
  27496. KyberKey key;
  27497. int ret;
  27498. byte priv[KYBER768_PRIVATE_KEY_SIZE];
  27499. byte pub[KYBER768_PUBLIC_KEY_SIZE];
  27500. byte ct[KYBER768_CIPHER_TEXT_SIZE];
  27501. byte ss[KYBER_SS_SZ];
  27502. byte ss_dec[KYBER_SS_SZ];
  27503. const byte kyber768_rand[] = {
  27504. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  27505. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  27506. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  27507. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  27508. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  27509. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  27510. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  27511. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  27512. };
  27513. const byte kyber768enc_rand[] = {
  27514. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  27515. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  27516. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  27517. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  27518. };
  27519. #ifndef WOLFSSL_KYBER_90S
  27520. const byte kyber768_pk[] = {
  27521. 0xA7, 0x2C, 0x2D, 0x9C, 0x84, 0x3E, 0xE9, 0xF8,
  27522. 0x31, 0x3E, 0xCC, 0x7F, 0x86, 0xD6, 0x29, 0x4D,
  27523. 0x59, 0x15, 0x9D, 0x9A, 0x87, 0x9A, 0x54, 0x2E,
  27524. 0x26, 0x09, 0x22, 0xAD, 0xF9, 0x99, 0x05, 0x1C,
  27525. 0xC4, 0x52, 0x00, 0xC9, 0xFF, 0xDB, 0x60, 0x44,
  27526. 0x9C, 0x49, 0x46, 0x59, 0x79, 0x27, 0x23, 0x67,
  27527. 0xC0, 0x83, 0xA7, 0xD6, 0x26, 0x7A, 0x3E, 0xD7,
  27528. 0xA7, 0xFD, 0x47, 0x95, 0x7C, 0x21, 0x93, 0x27,
  27529. 0xF7, 0xCA, 0x73, 0xA4, 0x00, 0x7E, 0x16, 0x27,
  27530. 0xF0, 0x0B, 0x11, 0xCC, 0x80, 0x57, 0x3C, 0x15,
  27531. 0xAE, 0xE6, 0x64, 0x0F, 0xB8, 0x56, 0x2D, 0xFA,
  27532. 0x6B, 0x24, 0x0C, 0xA0, 0xAD, 0x35, 0x1A, 0xC4,
  27533. 0xAC, 0x15, 0x5B, 0x96, 0xC1, 0x4C, 0x8A, 0xB1,
  27534. 0x3D, 0xD2, 0x62, 0xCD, 0xFD, 0x51, 0xC4, 0xBB,
  27535. 0x55, 0x72, 0xFD, 0x61, 0x65, 0x53, 0xD1, 0x7B,
  27536. 0xDD, 0x43, 0x0A, 0xCB, 0xEA, 0x3E, 0x95, 0xF0,
  27537. 0xB6, 0x98, 0xD6, 0x69, 0x90, 0xAB, 0x51, 0xE5,
  27538. 0xD0, 0x37, 0x83, 0xA8, 0xB3, 0xD2, 0x78, 0xA5,
  27539. 0x72, 0x04, 0x54, 0xCF, 0x96, 0x95, 0xCF, 0xDC,
  27540. 0xA0, 0x84, 0x85, 0xBA, 0x09, 0x9C, 0x51, 0xCD,
  27541. 0x92, 0xA7, 0xEA, 0x75, 0x87, 0xC1, 0xD1, 0x5C,
  27542. 0x28, 0xE6, 0x09, 0xA8, 0x18, 0x52, 0x60, 0x1B,
  27543. 0x06, 0x04, 0x01, 0x06, 0x79, 0xAA, 0x48, 0x2D,
  27544. 0x51, 0x26, 0x1E, 0xC3, 0x6E, 0x36, 0xB8, 0x71,
  27545. 0x96, 0x76, 0x21, 0x7F, 0xD7, 0x4C, 0x54, 0x78,
  27546. 0x64, 0x88, 0xF4, 0xB4, 0x96, 0x9C, 0x05, 0xA8,
  27547. 0xBA, 0x27, 0xCA, 0x3A, 0x77, 0xCC, 0xE7, 0x3B,
  27548. 0x96, 0x59, 0x23, 0xCA, 0x55, 0x4E, 0x42, 0x2B,
  27549. 0x9B, 0x61, 0xF4, 0x75, 0x46, 0x41, 0x60, 0x8A,
  27550. 0xC1, 0x6C, 0x9B, 0x85, 0x87, 0xA3, 0x2C, 0x1C,
  27551. 0x5D, 0xD7, 0x88, 0xF8, 0x8B, 0x36, 0xB7, 0x17,
  27552. 0xA4, 0x69, 0x65, 0x63, 0x5D, 0xEB, 0x67, 0xF4,
  27553. 0x5B, 0x12, 0x9B, 0x99, 0x07, 0x09, 0x09, 0xC9,
  27554. 0x3E, 0xB8, 0x0B, 0x42, 0xC2, 0xB3, 0xF3, 0xF7,
  27555. 0x03, 0x43, 0xA7, 0xCF, 0x37, 0xE8, 0x52, 0x0E,
  27556. 0x7B, 0xCF, 0xC4, 0x16, 0xAC, 0xA4, 0xF1, 0x8C,
  27557. 0x79, 0x81, 0x26, 0x2B, 0xA2, 0xBF, 0xC7, 0x56,
  27558. 0xAE, 0x03, 0x27, 0x8F, 0x0E, 0xC6, 0x6D, 0xC2,
  27559. 0x05, 0x76, 0x96, 0x82, 0x4B, 0xA6, 0x76, 0x98,
  27560. 0x65, 0xA6, 0x01, 0xD7, 0x14, 0x8E, 0xF6, 0xF5,
  27561. 0x4E, 0x5A, 0xF5, 0x68, 0x6A, 0xA2, 0x90, 0x6F,
  27562. 0x99, 0x4C, 0xE3, 0x8A, 0x5E, 0x0B, 0x93, 0x8F,
  27563. 0x23, 0x90, 0x07, 0x00, 0x30, 0x22, 0xC0, 0x33,
  27564. 0x92, 0xDF, 0x34, 0x01, 0xB1, 0xE4, 0xA3, 0xA7,
  27565. 0xEB, 0xC6, 0x16, 0x14, 0x49, 0xF7, 0x33, 0x74,
  27566. 0xC8, 0xB0, 0x14, 0x03, 0x69, 0x34, 0x3D, 0x92,
  27567. 0x95, 0xFD, 0xF5, 0x11, 0x84, 0x5C, 0x4A, 0x46,
  27568. 0xEB, 0xAA, 0xB6, 0xCA, 0x54, 0x92, 0xF6, 0x80,
  27569. 0x0B, 0x98, 0xC0, 0xCC, 0x80, 0x36, 0x53, 0xA4,
  27570. 0xB1, 0xD6, 0xE6, 0xAA, 0xED, 0x19, 0x32, 0xBA,
  27571. 0xCC, 0x5F, 0xEF, 0xAA, 0x81, 0x8B, 0xA5, 0x02,
  27572. 0x85, 0x9B, 0xA5, 0x49, 0x4C, 0x5F, 0x54, 0x02,
  27573. 0xC8, 0x53, 0x6A, 0x9C, 0x4C, 0x18, 0x88, 0x15,
  27574. 0x06, 0x17, 0xF8, 0x00, 0x98, 0xF6, 0xB2, 0xA9,
  27575. 0x9C, 0x39, 0xBC, 0x5D, 0xC7, 0xCF, 0x3B, 0x59,
  27576. 0x00, 0xA2, 0x13, 0x29, 0xAB, 0x59, 0x05, 0x3A,
  27577. 0xBA, 0xA6, 0x4E, 0xD1, 0x63, 0xE8, 0x59, 0xA8,
  27578. 0xB3, 0xB3, 0xCA, 0x33, 0x59, 0xB7, 0x50, 0xCC,
  27579. 0xC3, 0xE7, 0x10, 0xC7, 0xAC, 0x43, 0xC8, 0x19,
  27580. 0x1C, 0xB5, 0xD6, 0x88, 0x70, 0xC0, 0x63, 0x91,
  27581. 0xC0, 0xCB, 0x8A, 0xEC, 0x72, 0xB8, 0x97, 0xAC,
  27582. 0x6B, 0xE7, 0xFB, 0xAA, 0xCC, 0x67, 0x6E, 0xD6,
  27583. 0x63, 0x14, 0xC8, 0x36, 0x30, 0xE8, 0x94, 0x48,
  27584. 0xC8, 0x8A, 0x1D, 0xF0, 0x4A, 0xCE, 0xB2, 0x3A,
  27585. 0xBF, 0x2E, 0x40, 0x9E, 0xF3, 0x33, 0xC6, 0x22,
  27586. 0x28, 0x9C, 0x18, 0xA2, 0x13, 0x4E, 0x65, 0x0C,
  27587. 0x45, 0x25, 0x7E, 0x47, 0x47, 0x5F, 0xA3, 0x3A,
  27588. 0xA5, 0x37, 0xA5, 0xA8, 0xF7, 0x68, 0x02, 0x14,
  27589. 0x71, 0x6C, 0x50, 0xD4, 0x70, 0xE3, 0x28, 0x49,
  27590. 0x63, 0xCA, 0x64, 0xF5, 0x46, 0x77, 0xAE, 0xC5,
  27591. 0x4B, 0x52, 0x72, 0x16, 0x2B, 0xF5, 0x2B, 0xC8,
  27592. 0x14, 0x2E, 0x1D, 0x41, 0x83, 0xFC, 0x01, 0x74,
  27593. 0x54, 0xA6, 0xB5, 0xA4, 0x96, 0x83, 0x17, 0x59,
  27594. 0x06, 0x40, 0x24, 0x74, 0x59, 0x78, 0xCB, 0xD5,
  27595. 0x1A, 0x6C, 0xED, 0xC8, 0x95, 0x5D, 0xE4, 0xCC,
  27596. 0x6D, 0x36, 0x36, 0x70, 0xA4, 0x74, 0x66, 0xE8,
  27597. 0x2B, 0xE5, 0xC2, 0x36, 0x03, 0xA1, 0x7B, 0xF2,
  27598. 0x2A, 0xCD, 0xB7, 0xCC, 0x98, 0x4A, 0xF0, 0x8C,
  27599. 0x87, 0xE1, 0x4E, 0x27, 0x75, 0x3C, 0xF5, 0x87,
  27600. 0xA8, 0xEC, 0x34, 0x47, 0xE6, 0x2C, 0x64, 0x9E,
  27601. 0x88, 0x7A, 0x67, 0xC3, 0x6C, 0x9C, 0xE9, 0x87,
  27602. 0x21, 0xB6, 0x97, 0x21, 0x32, 0x75, 0x64, 0x6B,
  27603. 0x19, 0x4F, 0x36, 0x75, 0x86, 0x73, 0xA8, 0xED,
  27604. 0x11, 0x28, 0x44, 0x55, 0xAF, 0xC7, 0xA8, 0x52,
  27605. 0x9F, 0x69, 0xC9, 0x7A, 0x3C, 0x2D, 0x7B, 0x8C,
  27606. 0x63, 0x6C, 0x0B, 0xA5, 0x56, 0x14, 0xB7, 0x68,
  27607. 0xE6, 0x24, 0xE7, 0x12, 0x93, 0x0F, 0x77, 0x61,
  27608. 0x69, 0xB0, 0x17, 0x15, 0x72, 0x53, 0x51, 0xBC,
  27609. 0x74, 0xB4, 0x73, 0x95, 0xED, 0x52, 0xB2, 0x5A,
  27610. 0x13, 0x13, 0xC9, 0x51, 0x64, 0x81, 0x4C, 0x34,
  27611. 0xC9, 0x79, 0xCB, 0xDF, 0xAB, 0x85, 0x95, 0x46,
  27612. 0x62, 0xCA, 0xB4, 0x85, 0xE7, 0x50, 0x87, 0xA9,
  27613. 0x8C, 0xC7, 0x4B, 0xB8, 0x2C, 0xA2, 0xD1, 0xB5,
  27614. 0xBF, 0x28, 0x03, 0x23, 0x84, 0x80, 0x63, 0x8C,
  27615. 0x40, 0xE9, 0x0B, 0x43, 0xC7, 0x46, 0x0E, 0x7A,
  27616. 0xA9, 0x17, 0xF0, 0x10, 0x15, 0x1F, 0xAB, 0x11,
  27617. 0x69, 0x98, 0x7B, 0x37, 0x2A, 0xBB, 0x59, 0x27,
  27618. 0x1F, 0x70, 0x06, 0xC2, 0x4E, 0x60, 0x23, 0x6B,
  27619. 0x84, 0xB9, 0xDD, 0xD6, 0x00, 0x62, 0x37, 0x04,
  27620. 0x25, 0x46, 0x17, 0xFB, 0x49, 0x8D, 0x89, 0xE5,
  27621. 0x8B, 0x03, 0x68, 0xBC, 0xB2, 0x10, 0x3E, 0x79,
  27622. 0x35, 0x3E, 0xB5, 0x87, 0x86, 0x0C, 0x14, 0x22,
  27623. 0xE4, 0x76, 0x16, 0x2E, 0x42, 0x5B, 0xC2, 0x38,
  27624. 0x1D, 0xB8, 0x2C, 0x65, 0x92, 0x73, 0x7E, 0x1D,
  27625. 0xD6, 0x02, 0x86, 0x4B, 0x01, 0x67, 0xA7, 0x1E,
  27626. 0xC1, 0xF2, 0x23, 0x30, 0x5C, 0x02, 0xFE, 0x25,
  27627. 0x05, 0x2A, 0xF2, 0xB3, 0xB5, 0xA5, 0x5A, 0x0D,
  27628. 0x7A, 0x20, 0x22, 0xD9, 0xA7, 0x98, 0xDC, 0x0C,
  27629. 0x58, 0x74, 0xA9, 0x87, 0x02, 0xAA, 0xF4, 0x05,
  27630. 0x4C, 0x5D, 0x80, 0x33, 0x8A, 0x52, 0x48, 0xB5,
  27631. 0xB7, 0xBD, 0x09, 0xC5, 0x3B, 0x5E, 0x2A, 0x08,
  27632. 0x4B, 0x04, 0x7D, 0x27, 0x7A, 0x86, 0x1B, 0x1A,
  27633. 0x73, 0xBB, 0x51, 0x48, 0x8D, 0xE0, 0x4E, 0xF5,
  27634. 0x73, 0xC8, 0x52, 0x30, 0xA0, 0x47, 0x0B, 0x73,
  27635. 0x17, 0x5C, 0x9F, 0xA5, 0x05, 0x94, 0xF6, 0x6A,
  27636. 0x5F, 0x50, 0xB4, 0x15, 0x00, 0x54, 0xC9, 0x3B,
  27637. 0x68, 0x18, 0x6F, 0x8B, 0x5C, 0xBC, 0x49, 0x31,
  27638. 0x6C, 0x85, 0x48, 0xA6, 0x42, 0xB2, 0xB3, 0x6A,
  27639. 0x1D, 0x45, 0x4C, 0x74, 0x89, 0xAC, 0x33, 0xB2,
  27640. 0xD2, 0xCE, 0x66, 0x68, 0x09, 0x67, 0x82, 0xA2,
  27641. 0xC1, 0xE0, 0x86, 0x6D, 0x21, 0xA6, 0x5E, 0x16,
  27642. 0xB5, 0x85, 0xE7, 0xAF, 0x86, 0x18, 0xBD, 0xF3,
  27643. 0x18, 0x4C, 0x19, 0x86, 0x87, 0x85, 0x08, 0x91,
  27644. 0x72, 0x77, 0xB9, 0x3E, 0x10, 0x70, 0x6B, 0x16,
  27645. 0x14, 0x97, 0x2B, 0x2A, 0x94, 0xC7, 0x31, 0x0F,
  27646. 0xE9, 0xC7, 0x08, 0xC2, 0x31, 0xA1, 0xA8, 0xAC,
  27647. 0x8D, 0x93, 0x14, 0xA5, 0x29, 0xA9, 0x7F, 0x46,
  27648. 0x9B, 0xF6, 0x49, 0x62, 0xD8, 0x20, 0x64, 0x84,
  27649. 0x43, 0x09, 0x9A, 0x07, 0x6D, 0x55, 0xD4, 0xCE,
  27650. 0xA8, 0x24, 0xA5, 0x83, 0x04, 0x84, 0x4F, 0x99,
  27651. 0x49, 0x7C, 0x10, 0xA2, 0x51, 0x48, 0x61, 0x8A,
  27652. 0x31, 0x5D, 0x72, 0xCA, 0x85, 0x7D, 0x1B, 0x04,
  27653. 0xD5, 0x75, 0xB9, 0x4F, 0x85, 0xC0, 0x1D, 0x19,
  27654. 0xBE, 0xF2, 0x11, 0xBF, 0x0A, 0xA3, 0x36, 0x2E,
  27655. 0x70, 0x41, 0xFD, 0x16, 0x59, 0x6D, 0x80, 0x8E,
  27656. 0x86, 0x7B, 0x44, 0xC4, 0xC0, 0x0D, 0x1C, 0xDA,
  27657. 0x34, 0x18, 0x96, 0x77, 0x17, 0xF1, 0x47, 0xD0,
  27658. 0xEB, 0x21, 0xB4, 0x2A, 0xAE, 0xE7, 0x4A, 0xC3,
  27659. 0x5D, 0x0B, 0x92, 0x41, 0x4B, 0x95, 0x85, 0x31,
  27660. 0xAA, 0xDF, 0x46, 0x3E, 0xC6, 0x30, 0x5A, 0xE5,
  27661. 0xEC, 0xAF, 0x79, 0x17, 0x40, 0x02, 0xF2, 0x6D,
  27662. 0xDE, 0xCC, 0x81, 0x3B, 0xF3, 0x26, 0x72, 0xE8,
  27663. 0x52, 0x9D, 0x95, 0xA4, 0xE7, 0x30, 0xA7, 0xAB,
  27664. 0x4A, 0x3E, 0x8F, 0x8A, 0x8A, 0xF9, 0x79, 0xA6,
  27665. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  27666. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  27667. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  27668. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  27669. };
  27670. const byte kyber768_sk[] = {
  27671. 0x07, 0x63, 0x8F, 0xB6, 0x98, 0x68, 0xF3, 0xD3,
  27672. 0x20, 0xE5, 0x86, 0x2B, 0xD9, 0x69, 0x33, 0xFE,
  27673. 0xB3, 0x11, 0xB3, 0x62, 0x09, 0x3C, 0x9B, 0x5D,
  27674. 0x50, 0x17, 0x0B, 0xCE, 0xD4, 0x3F, 0x1B, 0x53,
  27675. 0x6D, 0x9A, 0x20, 0x4B, 0xB1, 0xF2, 0x26, 0x95,
  27676. 0x95, 0x0B, 0xA1, 0xF2, 0xA9, 0xE8, 0xEB, 0x82,
  27677. 0x8B, 0x28, 0x44, 0x88, 0x76, 0x0B, 0x3F, 0xC8,
  27678. 0x4F, 0xAB, 0xA0, 0x42, 0x75, 0xD5, 0x62, 0x8E,
  27679. 0x39, 0xC5, 0xB2, 0x47, 0x13, 0x74, 0x28, 0x3C,
  27680. 0x50, 0x32, 0x99, 0xC0, 0xAB, 0x49, 0xB6, 0x6B,
  27681. 0x8B, 0xBB, 0x56, 0xA4, 0x18, 0x66, 0x24, 0xF9,
  27682. 0x19, 0xA2, 0xBA, 0x59, 0xBB, 0x08, 0xD8, 0x55,
  27683. 0x18, 0x80, 0xC2, 0xBE, 0xFC, 0x4F, 0x87, 0xF2,
  27684. 0x5F, 0x59, 0xAB, 0x58, 0x7A, 0x79, 0xC3, 0x27,
  27685. 0xD7, 0x92, 0xD5, 0x4C, 0x97, 0x4A, 0x69, 0x26,
  27686. 0x2F, 0xF8, 0xA7, 0x89, 0x38, 0x28, 0x9E, 0x9A,
  27687. 0x87, 0xB6, 0x88, 0xB0, 0x83, 0xE0, 0x59, 0x5F,
  27688. 0xE2, 0x18, 0xB6, 0xBB, 0x15, 0x05, 0x94, 0x1C,
  27689. 0xE2, 0xE8, 0x1A, 0x5A, 0x64, 0xC5, 0xAA, 0xC6,
  27690. 0x04, 0x17, 0x25, 0x69, 0x85, 0x34, 0x9E, 0xE4,
  27691. 0x7A, 0x52, 0x42, 0x0A, 0x5F, 0x97, 0x47, 0x7B,
  27692. 0x72, 0x36, 0xAC, 0x76, 0xBC, 0x70, 0xE8, 0x28,
  27693. 0x87, 0x29, 0x28, 0x7E, 0xE3, 0xE3, 0x4A, 0x3D,
  27694. 0xBC, 0x36, 0x83, 0xC0, 0xB7, 0xB1, 0x00, 0x29,
  27695. 0xFC, 0x20, 0x34, 0x18, 0x53, 0x7E, 0x74, 0x66,
  27696. 0xBA, 0x63, 0x85, 0xA8, 0xFF, 0x30, 0x1E, 0xE1,
  27697. 0x27, 0x08, 0xF8, 0x2A, 0xAA, 0x1E, 0x38, 0x0F,
  27698. 0xC7, 0xA8, 0x8F, 0x8F, 0x20, 0x5A, 0xB7, 0xE8,
  27699. 0x8D, 0x7E, 0x95, 0x95, 0x2A, 0x55, 0xBA, 0x20,
  27700. 0xD0, 0x9B, 0x79, 0xA4, 0x71, 0x41, 0xD6, 0x2B,
  27701. 0xF6, 0xEB, 0x7D, 0xD3, 0x07, 0xB0, 0x8E, 0xCA,
  27702. 0x13, 0xA5, 0xBC, 0x5F, 0x6B, 0x68, 0x58, 0x1C,
  27703. 0x68, 0x65, 0xB2, 0x7B, 0xBC, 0xDD, 0xAB, 0x14,
  27704. 0x2F, 0x4B, 0x2C, 0xBF, 0xF4, 0x88, 0xC8, 0xA2,
  27705. 0x27, 0x05, 0xFA, 0xA9, 0x8A, 0x2B, 0x9E, 0xEA,
  27706. 0x35, 0x30, 0xC7, 0x66, 0x62, 0x33, 0x5C, 0xC7,
  27707. 0xEA, 0x3A, 0x00, 0x77, 0x77, 0x25, 0xEB, 0xCC,
  27708. 0xCD, 0x2A, 0x46, 0x36, 0xB2, 0xD9, 0x12, 0x2F,
  27709. 0xF3, 0xAB, 0x77, 0x12, 0x3C, 0xE0, 0x88, 0x3C,
  27710. 0x19, 0x11, 0x11, 0x5E, 0x50, 0xC9, 0xE8, 0xA9,
  27711. 0x41, 0x94, 0xE4, 0x8D, 0xD0, 0xD0, 0x9C, 0xFF,
  27712. 0xB3, 0xAD, 0xCD, 0x2C, 0x1E, 0x92, 0x43, 0x09,
  27713. 0x03, 0xD0, 0x7A, 0xDB, 0xF0, 0x05, 0x32, 0x03,
  27714. 0x15, 0x75, 0xAA, 0x7F, 0x9E, 0x7B, 0x5A, 0x1F,
  27715. 0x33, 0x62, 0xDE, 0xC9, 0x36, 0xD4, 0x04, 0x3C,
  27716. 0x05, 0xF2, 0x47, 0x6C, 0x07, 0x57, 0x8B, 0xC9,
  27717. 0xCB, 0xAF, 0x2A, 0xB4, 0xE3, 0x82, 0x72, 0x7A,
  27718. 0xD4, 0x16, 0x86, 0xA9, 0x6B, 0x25, 0x48, 0x82,
  27719. 0x0B, 0xB0, 0x3B, 0x32, 0xF1, 0x1B, 0x28, 0x11,
  27720. 0xAD, 0x62, 0xF4, 0x89, 0xE9, 0x51, 0x63, 0x2A,
  27721. 0xBA, 0x0D, 0x1D, 0xF8, 0x96, 0x80, 0xCC, 0x8A,
  27722. 0x8B, 0x53, 0xB4, 0x81, 0xD9, 0x2A, 0x68, 0xD7,
  27723. 0x0B, 0x4E, 0xA1, 0xC3, 0xA6, 0xA5, 0x61, 0xC0,
  27724. 0x69, 0x28, 0x82, 0xB5, 0xCA, 0x8C, 0xC9, 0x42,
  27725. 0xA8, 0xD4, 0x95, 0xAF, 0xCB, 0x06, 0xDE, 0x89,
  27726. 0x49, 0x8F, 0xB9, 0x35, 0xB7, 0x75, 0x90, 0x8F,
  27727. 0xE7, 0xA0, 0x3E, 0x32, 0x4D, 0x54, 0xCC, 0x19,
  27728. 0xD4, 0xE1, 0xAA, 0xBD, 0x35, 0x93, 0xB3, 0x8B,
  27729. 0x19, 0xEE, 0x13, 0x88, 0xFE, 0x49, 0x2B, 0x43,
  27730. 0x12, 0x7E, 0x5A, 0x50, 0x42, 0x53, 0x78, 0x6A,
  27731. 0x0D, 0x69, 0xAD, 0x32, 0x60, 0x1C, 0x28, 0xE2,
  27732. 0xC8, 0x85, 0x04, 0xA5, 0xBA, 0x59, 0x97, 0x06,
  27733. 0x02, 0x3A, 0x61, 0x36, 0x3E, 0x17, 0xC6, 0xB9,
  27734. 0xBB, 0x59, 0xBD, 0xC6, 0x97, 0x45, 0x2C, 0xD0,
  27735. 0x59, 0x45, 0x19, 0x83, 0xD7, 0x38, 0xCA, 0x3F,
  27736. 0xD0, 0x34, 0xE3, 0xF5, 0x98, 0x88, 0x54, 0xCA,
  27737. 0x05, 0x03, 0x1D, 0xB0, 0x96, 0x11, 0x49, 0x89,
  27738. 0x88, 0x19, 0x7C, 0x6B, 0x30, 0xD2, 0x58, 0xDF,
  27739. 0xE2, 0x62, 0x65, 0x54, 0x1C, 0x89, 0xA4, 0xB3,
  27740. 0x1D, 0x68, 0x64, 0xE9, 0x38, 0x9B, 0x03, 0xCB,
  27741. 0x74, 0xF7, 0xEC, 0x43, 0x23, 0xFB, 0x94, 0x21,
  27742. 0xA4, 0xB9, 0x79, 0x0A, 0x26, 0xD1, 0x7B, 0x03,
  27743. 0x98, 0xA2, 0x67, 0x67, 0x35, 0x09, 0x09, 0xF8,
  27744. 0x4D, 0x57, 0xB6, 0x69, 0x4D, 0xF8, 0x30, 0x66,
  27745. 0x4C, 0xA8, 0xB3, 0xC3, 0xC0, 0x3E, 0xD2, 0xAE,
  27746. 0x67, 0xB8, 0x90, 0x06, 0x86, 0x8A, 0x68, 0x52,
  27747. 0x7C, 0xCD, 0x66, 0x64, 0x59, 0xAB, 0x7F, 0x05,
  27748. 0x66, 0x71, 0x00, 0x0C, 0x61, 0x64, 0xD3, 0xA7,
  27749. 0xF2, 0x66, 0xA1, 0x4D, 0x97, 0xCB, 0xD7, 0x00,
  27750. 0x4D, 0x6C, 0x92, 0xCA, 0xCA, 0x77, 0x0B, 0x84,
  27751. 0x4A, 0x4F, 0xA9, 0xB1, 0x82, 0xE7, 0xB1, 0x8C,
  27752. 0xA8, 0x85, 0x08, 0x2A, 0xC5, 0x64, 0x6F, 0xCB,
  27753. 0x4A, 0x14, 0xE1, 0x68, 0x5F, 0xEB, 0x0C, 0x9C,
  27754. 0xE3, 0x37, 0x2A, 0xB9, 0x53, 0x65, 0xC0, 0x4F,
  27755. 0xD8, 0x30, 0x84, 0xF8, 0x0A, 0x23, 0xFF, 0x10,
  27756. 0xA0, 0x5B, 0xF1, 0x5F, 0x7F, 0xA5, 0xAC, 0xC6,
  27757. 0xC0, 0xCB, 0x46, 0x2C, 0x33, 0xCA, 0x52, 0x4F,
  27758. 0xA6, 0xB8, 0xBB, 0x35, 0x90, 0x43, 0xBA, 0x68,
  27759. 0x60, 0x9E, 0xAA, 0x25, 0x36, 0xE8, 0x1D, 0x08,
  27760. 0x46, 0x3B, 0x19, 0x65, 0x3B, 0x54, 0x35, 0xBA,
  27761. 0x94, 0x6C, 0x9A, 0xDD, 0xEB, 0x20, 0x2B, 0x04,
  27762. 0xB0, 0x31, 0xCC, 0x96, 0x0D, 0xCC, 0x12, 0xE4,
  27763. 0x51, 0x8D, 0x42, 0x8B, 0x32, 0xB2, 0x57, 0xA4,
  27764. 0xFC, 0x73, 0x13, 0xD3, 0xA7, 0x98, 0x0D, 0x80,
  27765. 0x08, 0x2E, 0x93, 0x4F, 0x9D, 0x95, 0xC3, 0x2B,
  27766. 0x0A, 0x01, 0x91, 0xA2, 0x36, 0x04, 0x38, 0x4D,
  27767. 0xD9, 0xE0, 0x79, 0xBB, 0xBA, 0xA2, 0x66, 0xD1,
  27768. 0x4C, 0x3F, 0x75, 0x6B, 0x9F, 0x21, 0x33, 0x10,
  27769. 0x74, 0x33, 0xA4, 0xE8, 0x3F, 0xA7, 0x18, 0x72,
  27770. 0x82, 0xA8, 0x09, 0x20, 0x3A, 0x4F, 0xAF, 0x84,
  27771. 0x18, 0x51, 0x83, 0x3D, 0x12, 0x1A, 0xC3, 0x83,
  27772. 0x84, 0x3A, 0x5E, 0x55, 0xBC, 0x23, 0x81, 0x42,
  27773. 0x5E, 0x16, 0xC7, 0xDB, 0x4C, 0xC9, 0xAB, 0x5C,
  27774. 0x1B, 0x0D, 0x91, 0xA4, 0x7E, 0x2B, 0x8D, 0xE0,
  27775. 0xE5, 0x82, 0xC8, 0x6B, 0x6B, 0x0D, 0x90, 0x7B,
  27776. 0xB3, 0x60, 0xB9, 0x7F, 0x40, 0xAB, 0x5D, 0x03,
  27777. 0x8F, 0x6B, 0x75, 0xC8, 0x14, 0xB2, 0x7D, 0x9B,
  27778. 0x96, 0x8D, 0x41, 0x98, 0x32, 0xBC, 0x8C, 0x2B,
  27779. 0xEE, 0x60, 0x5E, 0xF6, 0xE5, 0x05, 0x9D, 0x33,
  27780. 0x10, 0x0D, 0x90, 0x48, 0x5D, 0x37, 0x84, 0x50,
  27781. 0x01, 0x42, 0x21, 0x73, 0x6C, 0x07, 0x40, 0x7C,
  27782. 0xAC, 0x26, 0x04, 0x08, 0xAA, 0x64, 0x92, 0x66,
  27783. 0x19, 0x78, 0x8B, 0x86, 0x01, 0xC2, 0xA7, 0x52,
  27784. 0xD1, 0xA6, 0xCB, 0xF8, 0x20, 0xD7, 0xC7, 0xA0,
  27785. 0x47, 0x16, 0x20, 0x32, 0x25, 0xB3, 0x89, 0x5B,
  27786. 0x93, 0x42, 0xD1, 0x47, 0xA8, 0x18, 0x5C, 0xFC,
  27787. 0x1B, 0xB6, 0x5B, 0xA0, 0x6B, 0x41, 0x42, 0x33,
  27788. 0x99, 0x03, 0xC0, 0xAC, 0x46, 0x51, 0x38, 0x5B,
  27789. 0x45, 0xD9, 0x8A, 0x8B, 0x19, 0xD2, 0x8C, 0xD6,
  27790. 0xBA, 0xB0, 0x88, 0x78, 0x7F, 0x7E, 0xE1, 0xB1,
  27791. 0x24, 0x61, 0x76, 0x6B, 0x43, 0xCB, 0xCC, 0xB9,
  27792. 0x64, 0x34, 0x42, 0x7D, 0x93, 0xC0, 0x65, 0x55,
  27793. 0x06, 0x88, 0xF6, 0x94, 0x8E, 0xD1, 0xB5, 0x47,
  27794. 0x5A, 0x42, 0x5F, 0x1B, 0x85, 0x20, 0x9D, 0x06,
  27795. 0x1C, 0x08, 0xB5, 0x6C, 0x1C, 0xC0, 0x69, 0xF6,
  27796. 0xC0, 0xA7, 0xC6, 0xF2, 0x93, 0x58, 0xCA, 0xB9,
  27797. 0x11, 0x08, 0x77, 0x32, 0xA6, 0x49, 0xD2, 0x7C,
  27798. 0x9B, 0x98, 0xF9, 0xA4, 0x88, 0x79, 0x38, 0x7D,
  27799. 0x9B, 0x00, 0xC2, 0x59, 0x59, 0xA7, 0x16, 0x54,
  27800. 0xD6, 0xF6, 0xA9, 0x46, 0x16, 0x45, 0x13, 0xE4,
  27801. 0x7A, 0x75, 0xD0, 0x05, 0x98, 0x6C, 0x23, 0x63,
  27802. 0xC0, 0x9F, 0x6B, 0x53, 0x7E, 0xCA, 0x78, 0xB9,
  27803. 0x30, 0x3A, 0x5F, 0xA4, 0x57, 0x60, 0x8A, 0x58,
  27804. 0x6A, 0x65, 0x3A, 0x34, 0x7D, 0xB0, 0x4D, 0xFC,
  27805. 0xC1, 0x91, 0x75, 0xB3, 0xA3, 0x01, 0x17, 0x25,
  27806. 0x36, 0x06, 0x2A, 0x65, 0x8A, 0x95, 0x27, 0x75,
  27807. 0x70, 0xC8, 0x85, 0x2C, 0xA8, 0x97, 0x3F, 0x4A,
  27808. 0xE1, 0x23, 0xA3, 0x34, 0x04, 0x7D, 0xD7, 0x11,
  27809. 0xC8, 0x92, 0x7A, 0x63, 0x4A, 0x03, 0x38, 0x8A,
  27810. 0x52, 0x7B, 0x03, 0x4B, 0xF7, 0xA8, 0x17, 0x0F,
  27811. 0xA7, 0x02, 0xC1, 0xF7, 0xC2, 0x3E, 0xC3, 0x2D,
  27812. 0x18, 0xA2, 0x37, 0x48, 0x90, 0xBE, 0x9C, 0x78,
  27813. 0x7A, 0x94, 0x09, 0xC8, 0x2D, 0x19, 0x2C, 0x4B,
  27814. 0xB7, 0x05, 0xA2, 0xF9, 0x96, 0xCE, 0x40, 0x5D,
  27815. 0xA7, 0x2C, 0x2D, 0x9C, 0x84, 0x3E, 0xE9, 0xF8,
  27816. 0x31, 0x3E, 0xCC, 0x7F, 0x86, 0xD6, 0x29, 0x4D,
  27817. 0x59, 0x15, 0x9D, 0x9A, 0x87, 0x9A, 0x54, 0x2E,
  27818. 0x26, 0x09, 0x22, 0xAD, 0xF9, 0x99, 0x05, 0x1C,
  27819. 0xC4, 0x52, 0x00, 0xC9, 0xFF, 0xDB, 0x60, 0x44,
  27820. 0x9C, 0x49, 0x46, 0x59, 0x79, 0x27, 0x23, 0x67,
  27821. 0xC0, 0x83, 0xA7, 0xD6, 0x26, 0x7A, 0x3E, 0xD7,
  27822. 0xA7, 0xFD, 0x47, 0x95, 0x7C, 0x21, 0x93, 0x27,
  27823. 0xF7, 0xCA, 0x73, 0xA4, 0x00, 0x7E, 0x16, 0x27,
  27824. 0xF0, 0x0B, 0x11, 0xCC, 0x80, 0x57, 0x3C, 0x15,
  27825. 0xAE, 0xE6, 0x64, 0x0F, 0xB8, 0x56, 0x2D, 0xFA,
  27826. 0x6B, 0x24, 0x0C, 0xA0, 0xAD, 0x35, 0x1A, 0xC4,
  27827. 0xAC, 0x15, 0x5B, 0x96, 0xC1, 0x4C, 0x8A, 0xB1,
  27828. 0x3D, 0xD2, 0x62, 0xCD, 0xFD, 0x51, 0xC4, 0xBB,
  27829. 0x55, 0x72, 0xFD, 0x61, 0x65, 0x53, 0xD1, 0x7B,
  27830. 0xDD, 0x43, 0x0A, 0xCB, 0xEA, 0x3E, 0x95, 0xF0,
  27831. 0xB6, 0x98, 0xD6, 0x69, 0x90, 0xAB, 0x51, 0xE5,
  27832. 0xD0, 0x37, 0x83, 0xA8, 0xB3, 0xD2, 0x78, 0xA5,
  27833. 0x72, 0x04, 0x54, 0xCF, 0x96, 0x95, 0xCF, 0xDC,
  27834. 0xA0, 0x84, 0x85, 0xBA, 0x09, 0x9C, 0x51, 0xCD,
  27835. 0x92, 0xA7, 0xEA, 0x75, 0x87, 0xC1, 0xD1, 0x5C,
  27836. 0x28, 0xE6, 0x09, 0xA8, 0x18, 0x52, 0x60, 0x1B,
  27837. 0x06, 0x04, 0x01, 0x06, 0x79, 0xAA, 0x48, 0x2D,
  27838. 0x51, 0x26, 0x1E, 0xC3, 0x6E, 0x36, 0xB8, 0x71,
  27839. 0x96, 0x76, 0x21, 0x7F, 0xD7, 0x4C, 0x54, 0x78,
  27840. 0x64, 0x88, 0xF4, 0xB4, 0x96, 0x9C, 0x05, 0xA8,
  27841. 0xBA, 0x27, 0xCA, 0x3A, 0x77, 0xCC, 0xE7, 0x3B,
  27842. 0x96, 0x59, 0x23, 0xCA, 0x55, 0x4E, 0x42, 0x2B,
  27843. 0x9B, 0x61, 0xF4, 0x75, 0x46, 0x41, 0x60, 0x8A,
  27844. 0xC1, 0x6C, 0x9B, 0x85, 0x87, 0xA3, 0x2C, 0x1C,
  27845. 0x5D, 0xD7, 0x88, 0xF8, 0x8B, 0x36, 0xB7, 0x17,
  27846. 0xA4, 0x69, 0x65, 0x63, 0x5D, 0xEB, 0x67, 0xF4,
  27847. 0x5B, 0x12, 0x9B, 0x99, 0x07, 0x09, 0x09, 0xC9,
  27848. 0x3E, 0xB8, 0x0B, 0x42, 0xC2, 0xB3, 0xF3, 0xF7,
  27849. 0x03, 0x43, 0xA7, 0xCF, 0x37, 0xE8, 0x52, 0x0E,
  27850. 0x7B, 0xCF, 0xC4, 0x16, 0xAC, 0xA4, 0xF1, 0x8C,
  27851. 0x79, 0x81, 0x26, 0x2B, 0xA2, 0xBF, 0xC7, 0x56,
  27852. 0xAE, 0x03, 0x27, 0x8F, 0x0E, 0xC6, 0x6D, 0xC2,
  27853. 0x05, 0x76, 0x96, 0x82, 0x4B, 0xA6, 0x76, 0x98,
  27854. 0x65, 0xA6, 0x01, 0xD7, 0x14, 0x8E, 0xF6, 0xF5,
  27855. 0x4E, 0x5A, 0xF5, 0x68, 0x6A, 0xA2, 0x90, 0x6F,
  27856. 0x99, 0x4C, 0xE3, 0x8A, 0x5E, 0x0B, 0x93, 0x8F,
  27857. 0x23, 0x90, 0x07, 0x00, 0x30, 0x22, 0xC0, 0x33,
  27858. 0x92, 0xDF, 0x34, 0x01, 0xB1, 0xE4, 0xA3, 0xA7,
  27859. 0xEB, 0xC6, 0x16, 0x14, 0x49, 0xF7, 0x33, 0x74,
  27860. 0xC8, 0xB0, 0x14, 0x03, 0x69, 0x34, 0x3D, 0x92,
  27861. 0x95, 0xFD, 0xF5, 0x11, 0x84, 0x5C, 0x4A, 0x46,
  27862. 0xEB, 0xAA, 0xB6, 0xCA, 0x54, 0x92, 0xF6, 0x80,
  27863. 0x0B, 0x98, 0xC0, 0xCC, 0x80, 0x36, 0x53, 0xA4,
  27864. 0xB1, 0xD6, 0xE6, 0xAA, 0xED, 0x19, 0x32, 0xBA,
  27865. 0xCC, 0x5F, 0xEF, 0xAA, 0x81, 0x8B, 0xA5, 0x02,
  27866. 0x85, 0x9B, 0xA5, 0x49, 0x4C, 0x5F, 0x54, 0x02,
  27867. 0xC8, 0x53, 0x6A, 0x9C, 0x4C, 0x18, 0x88, 0x15,
  27868. 0x06, 0x17, 0xF8, 0x00, 0x98, 0xF6, 0xB2, 0xA9,
  27869. 0x9C, 0x39, 0xBC, 0x5D, 0xC7, 0xCF, 0x3B, 0x59,
  27870. 0x00, 0xA2, 0x13, 0x29, 0xAB, 0x59, 0x05, 0x3A,
  27871. 0xBA, 0xA6, 0x4E, 0xD1, 0x63, 0xE8, 0x59, 0xA8,
  27872. 0xB3, 0xB3, 0xCA, 0x33, 0x59, 0xB7, 0x50, 0xCC,
  27873. 0xC3, 0xE7, 0x10, 0xC7, 0xAC, 0x43, 0xC8, 0x19,
  27874. 0x1C, 0xB5, 0xD6, 0x88, 0x70, 0xC0, 0x63, 0x91,
  27875. 0xC0, 0xCB, 0x8A, 0xEC, 0x72, 0xB8, 0x97, 0xAC,
  27876. 0x6B, 0xE7, 0xFB, 0xAA, 0xCC, 0x67, 0x6E, 0xD6,
  27877. 0x63, 0x14, 0xC8, 0x36, 0x30, 0xE8, 0x94, 0x48,
  27878. 0xC8, 0x8A, 0x1D, 0xF0, 0x4A, 0xCE, 0xB2, 0x3A,
  27879. 0xBF, 0x2E, 0x40, 0x9E, 0xF3, 0x33, 0xC6, 0x22,
  27880. 0x28, 0x9C, 0x18, 0xA2, 0x13, 0x4E, 0x65, 0x0C,
  27881. 0x45, 0x25, 0x7E, 0x47, 0x47, 0x5F, 0xA3, 0x3A,
  27882. 0xA5, 0x37, 0xA5, 0xA8, 0xF7, 0x68, 0x02, 0x14,
  27883. 0x71, 0x6C, 0x50, 0xD4, 0x70, 0xE3, 0x28, 0x49,
  27884. 0x63, 0xCA, 0x64, 0xF5, 0x46, 0x77, 0xAE, 0xC5,
  27885. 0x4B, 0x52, 0x72, 0x16, 0x2B, 0xF5, 0x2B, 0xC8,
  27886. 0x14, 0x2E, 0x1D, 0x41, 0x83, 0xFC, 0x01, 0x74,
  27887. 0x54, 0xA6, 0xB5, 0xA4, 0x96, 0x83, 0x17, 0x59,
  27888. 0x06, 0x40, 0x24, 0x74, 0x59, 0x78, 0xCB, 0xD5,
  27889. 0x1A, 0x6C, 0xED, 0xC8, 0x95, 0x5D, 0xE4, 0xCC,
  27890. 0x6D, 0x36, 0x36, 0x70, 0xA4, 0x74, 0x66, 0xE8,
  27891. 0x2B, 0xE5, 0xC2, 0x36, 0x03, 0xA1, 0x7B, 0xF2,
  27892. 0x2A, 0xCD, 0xB7, 0xCC, 0x98, 0x4A, 0xF0, 0x8C,
  27893. 0x87, 0xE1, 0x4E, 0x27, 0x75, 0x3C, 0xF5, 0x87,
  27894. 0xA8, 0xEC, 0x34, 0x47, 0xE6, 0x2C, 0x64, 0x9E,
  27895. 0x88, 0x7A, 0x67, 0xC3, 0x6C, 0x9C, 0xE9, 0x87,
  27896. 0x21, 0xB6, 0x97, 0x21, 0x32, 0x75, 0x64, 0x6B,
  27897. 0x19, 0x4F, 0x36, 0x75, 0x86, 0x73, 0xA8, 0xED,
  27898. 0x11, 0x28, 0x44, 0x55, 0xAF, 0xC7, 0xA8, 0x52,
  27899. 0x9F, 0x69, 0xC9, 0x7A, 0x3C, 0x2D, 0x7B, 0x8C,
  27900. 0x63, 0x6C, 0x0B, 0xA5, 0x56, 0x14, 0xB7, 0x68,
  27901. 0xE6, 0x24, 0xE7, 0x12, 0x93, 0x0F, 0x77, 0x61,
  27902. 0x69, 0xB0, 0x17, 0x15, 0x72, 0x53, 0x51, 0xBC,
  27903. 0x74, 0xB4, 0x73, 0x95, 0xED, 0x52, 0xB2, 0x5A,
  27904. 0x13, 0x13, 0xC9, 0x51, 0x64, 0x81, 0x4C, 0x34,
  27905. 0xC9, 0x79, 0xCB, 0xDF, 0xAB, 0x85, 0x95, 0x46,
  27906. 0x62, 0xCA, 0xB4, 0x85, 0xE7, 0x50, 0x87, 0xA9,
  27907. 0x8C, 0xC7, 0x4B, 0xB8, 0x2C, 0xA2, 0xD1, 0xB5,
  27908. 0xBF, 0x28, 0x03, 0x23, 0x84, 0x80, 0x63, 0x8C,
  27909. 0x40, 0xE9, 0x0B, 0x43, 0xC7, 0x46, 0x0E, 0x7A,
  27910. 0xA9, 0x17, 0xF0, 0x10, 0x15, 0x1F, 0xAB, 0x11,
  27911. 0x69, 0x98, 0x7B, 0x37, 0x2A, 0xBB, 0x59, 0x27,
  27912. 0x1F, 0x70, 0x06, 0xC2, 0x4E, 0x60, 0x23, 0x6B,
  27913. 0x84, 0xB9, 0xDD, 0xD6, 0x00, 0x62, 0x37, 0x04,
  27914. 0x25, 0x46, 0x17, 0xFB, 0x49, 0x8D, 0x89, 0xE5,
  27915. 0x8B, 0x03, 0x68, 0xBC, 0xB2, 0x10, 0x3E, 0x79,
  27916. 0x35, 0x3E, 0xB5, 0x87, 0x86, 0x0C, 0x14, 0x22,
  27917. 0xE4, 0x76, 0x16, 0x2E, 0x42, 0x5B, 0xC2, 0x38,
  27918. 0x1D, 0xB8, 0x2C, 0x65, 0x92, 0x73, 0x7E, 0x1D,
  27919. 0xD6, 0x02, 0x86, 0x4B, 0x01, 0x67, 0xA7, 0x1E,
  27920. 0xC1, 0xF2, 0x23, 0x30, 0x5C, 0x02, 0xFE, 0x25,
  27921. 0x05, 0x2A, 0xF2, 0xB3, 0xB5, 0xA5, 0x5A, 0x0D,
  27922. 0x7A, 0x20, 0x22, 0xD9, 0xA7, 0x98, 0xDC, 0x0C,
  27923. 0x58, 0x74, 0xA9, 0x87, 0x02, 0xAA, 0xF4, 0x05,
  27924. 0x4C, 0x5D, 0x80, 0x33, 0x8A, 0x52, 0x48, 0xB5,
  27925. 0xB7, 0xBD, 0x09, 0xC5, 0x3B, 0x5E, 0x2A, 0x08,
  27926. 0x4B, 0x04, 0x7D, 0x27, 0x7A, 0x86, 0x1B, 0x1A,
  27927. 0x73, 0xBB, 0x51, 0x48, 0x8D, 0xE0, 0x4E, 0xF5,
  27928. 0x73, 0xC8, 0x52, 0x30, 0xA0, 0x47, 0x0B, 0x73,
  27929. 0x17, 0x5C, 0x9F, 0xA5, 0x05, 0x94, 0xF6, 0x6A,
  27930. 0x5F, 0x50, 0xB4, 0x15, 0x00, 0x54, 0xC9, 0x3B,
  27931. 0x68, 0x18, 0x6F, 0x8B, 0x5C, 0xBC, 0x49, 0x31,
  27932. 0x6C, 0x85, 0x48, 0xA6, 0x42, 0xB2, 0xB3, 0x6A,
  27933. 0x1D, 0x45, 0x4C, 0x74, 0x89, 0xAC, 0x33, 0xB2,
  27934. 0xD2, 0xCE, 0x66, 0x68, 0x09, 0x67, 0x82, 0xA2,
  27935. 0xC1, 0xE0, 0x86, 0x6D, 0x21, 0xA6, 0x5E, 0x16,
  27936. 0xB5, 0x85, 0xE7, 0xAF, 0x86, 0x18, 0xBD, 0xF3,
  27937. 0x18, 0x4C, 0x19, 0x86, 0x87, 0x85, 0x08, 0x91,
  27938. 0x72, 0x77, 0xB9, 0x3E, 0x10, 0x70, 0x6B, 0x16,
  27939. 0x14, 0x97, 0x2B, 0x2A, 0x94, 0xC7, 0x31, 0x0F,
  27940. 0xE9, 0xC7, 0x08, 0xC2, 0x31, 0xA1, 0xA8, 0xAC,
  27941. 0x8D, 0x93, 0x14, 0xA5, 0x29, 0xA9, 0x7F, 0x46,
  27942. 0x9B, 0xF6, 0x49, 0x62, 0xD8, 0x20, 0x64, 0x84,
  27943. 0x43, 0x09, 0x9A, 0x07, 0x6D, 0x55, 0xD4, 0xCE,
  27944. 0xA8, 0x24, 0xA5, 0x83, 0x04, 0x84, 0x4F, 0x99,
  27945. 0x49, 0x7C, 0x10, 0xA2, 0x51, 0x48, 0x61, 0x8A,
  27946. 0x31, 0x5D, 0x72, 0xCA, 0x85, 0x7D, 0x1B, 0x04,
  27947. 0xD5, 0x75, 0xB9, 0x4F, 0x85, 0xC0, 0x1D, 0x19,
  27948. 0xBE, 0xF2, 0x11, 0xBF, 0x0A, 0xA3, 0x36, 0x2E,
  27949. 0x70, 0x41, 0xFD, 0x16, 0x59, 0x6D, 0x80, 0x8E,
  27950. 0x86, 0x7B, 0x44, 0xC4, 0xC0, 0x0D, 0x1C, 0xDA,
  27951. 0x34, 0x18, 0x96, 0x77, 0x17, 0xF1, 0x47, 0xD0,
  27952. 0xEB, 0x21, 0xB4, 0x2A, 0xAE, 0xE7, 0x4A, 0xC3,
  27953. 0x5D, 0x0B, 0x92, 0x41, 0x4B, 0x95, 0x85, 0x31,
  27954. 0xAA, 0xDF, 0x46, 0x3E, 0xC6, 0x30, 0x5A, 0xE5,
  27955. 0xEC, 0xAF, 0x79, 0x17, 0x40, 0x02, 0xF2, 0x6D,
  27956. 0xDE, 0xCC, 0x81, 0x3B, 0xF3, 0x26, 0x72, 0xE8,
  27957. 0x52, 0x9D, 0x95, 0xA4, 0xE7, 0x30, 0xA7, 0xAB,
  27958. 0x4A, 0x3E, 0x8F, 0x8A, 0x8A, 0xF9, 0x79, 0xA6,
  27959. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  27960. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  27961. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  27962. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  27963. 0xD4, 0xEC, 0x14, 0x3B, 0x50, 0xF0, 0x14, 0x23,
  27964. 0xB1, 0x77, 0x89, 0x5E, 0xDE, 0xE2, 0x2B, 0xB7,
  27965. 0x39, 0xF6, 0x47, 0xEC, 0xF8, 0x5F, 0x50, 0xBC,
  27966. 0x25, 0xEF, 0x7B, 0x5A, 0x72, 0x5D, 0xEE, 0x86,
  27967. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  27968. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  27969. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  27970. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  27971. };
  27972. const byte kyber768_ct[] = {
  27973. 0xB5, 0x2C, 0x56, 0xB9, 0x2A, 0x4B, 0x7C, 0xE9,
  27974. 0xE4, 0xCB, 0x7C, 0x5B, 0x1B, 0x16, 0x31, 0x67,
  27975. 0xA8, 0xA1, 0x67, 0x5B, 0x2F, 0xDE, 0xF8, 0x4A,
  27976. 0x5B, 0x67, 0xCA, 0x15, 0xDB, 0x69, 0x4C, 0x9F,
  27977. 0x11, 0xBD, 0x02, 0x7C, 0x30, 0xAE, 0x22, 0xEC,
  27978. 0x92, 0x1A, 0x1D, 0x91, 0x15, 0x99, 0xAF, 0x05,
  27979. 0x85, 0xE4, 0x8D, 0x20, 0xDA, 0x70, 0xDF, 0x9F,
  27980. 0x39, 0xE3, 0x2E, 0xF9, 0x5D, 0x4C, 0x8F, 0x44,
  27981. 0xBF, 0xEF, 0xDA, 0xA5, 0xDA, 0x64, 0xF1, 0x05,
  27982. 0x46, 0x31, 0xD0, 0x4D, 0x6D, 0x3C, 0xFD, 0x0A,
  27983. 0x54, 0x0D, 0xD7, 0xBA, 0x38, 0x86, 0xE4, 0xB5,
  27984. 0xF1, 0x3E, 0x87, 0x87, 0x88, 0x60, 0x4C, 0x95,
  27985. 0xC0, 0x96, 0xEA, 0xB3, 0x91, 0x9F, 0x42, 0x75,
  27986. 0x21, 0x41, 0x9A, 0x94, 0x6C, 0x26, 0xCC, 0x04,
  27987. 0x14, 0x75, 0xD7, 0x12, 0x4C, 0xDC, 0x01, 0xD0,
  27988. 0x37, 0x3E, 0x5B, 0x09, 0xC7, 0xA7, 0x06, 0x03,
  27989. 0xCF, 0xDB, 0x4F, 0xB3, 0x40, 0x50, 0x23, 0xF2,
  27990. 0x26, 0x4D, 0xC3, 0xF9, 0x83, 0xC4, 0xFC, 0x02,
  27991. 0xA2, 0xD1, 0xB2, 0x68, 0xF2, 0x20, 0x8A, 0x1F,
  27992. 0x6E, 0x2A, 0x62, 0x09, 0xBF, 0xF1, 0x2F, 0x6F,
  27993. 0x46, 0x5F, 0x0B, 0x06, 0x9C, 0x3A, 0x7F, 0x84,
  27994. 0xF6, 0x06, 0xD8, 0xA9, 0x40, 0x64, 0x00, 0x3D,
  27995. 0x6E, 0xC1, 0x14, 0xC8, 0xE8, 0x08, 0xD3, 0x05,
  27996. 0x38, 0x84, 0xC1, 0xD5, 0xA1, 0x42, 0xFB, 0xF2,
  27997. 0x01, 0x12, 0xEB, 0x36, 0x0F, 0xDA, 0x3F, 0x0F,
  27998. 0x28, 0xB1, 0x72, 0xAE, 0x50, 0xF5, 0xE7, 0xD8,
  27999. 0x38, 0x01, 0xFB, 0x3F, 0x00, 0x64, 0xB6, 0x87,
  28000. 0x18, 0x70, 0x74, 0xBD, 0x7F, 0xE3, 0x0E, 0xDD,
  28001. 0xAA, 0x33, 0x4C, 0xF8, 0xFC, 0x04, 0xFA, 0x8C,
  28002. 0xED, 0x89, 0x9C, 0xEA, 0xDE, 0x4B, 0x4F, 0x28,
  28003. 0xB6, 0x83, 0x72, 0xBA, 0xF9, 0x8F, 0xF4, 0x82,
  28004. 0xA4, 0x15, 0xB7, 0x31, 0x15, 0x5B, 0x75, 0xCE,
  28005. 0xB9, 0x76, 0xBE, 0x0E, 0xA0, 0x28, 0x5B, 0xA0,
  28006. 0x1A, 0x27, 0xF1, 0x85, 0x7A, 0x8F, 0xB3, 0x77,
  28007. 0xA3, 0xAE, 0x0C, 0x23, 0xB2, 0xAA, 0x9A, 0x07,
  28008. 0x9B, 0xFA, 0xBF, 0xF0, 0xD5, 0xB2, 0xF1, 0xCD,
  28009. 0x9B, 0x71, 0x8B, 0xEA, 0x03, 0xC4, 0x2F, 0x34,
  28010. 0x3A, 0x39, 0xB4, 0xF1, 0x42, 0xD0, 0x1A, 0xD8,
  28011. 0xAC, 0xBB, 0x50, 0xE3, 0x88, 0x53, 0xCF, 0x9A,
  28012. 0x50, 0xC8, 0xB4, 0x4C, 0x3C, 0xF6, 0x71, 0xA4,
  28013. 0xA9, 0x04, 0x3B, 0x26, 0xDD, 0xBB, 0x24, 0x95,
  28014. 0x9A, 0xD6, 0x71, 0x5C, 0x08, 0x52, 0x18, 0x55,
  28015. 0xC7, 0x9A, 0x23, 0xB9, 0xC3, 0xD6, 0x47, 0x17,
  28016. 0x49, 0xC4, 0x07, 0x25, 0xBD, 0xD5, 0xC2, 0x77,
  28017. 0x6D, 0x43, 0xAE, 0xD2, 0x02, 0x04, 0xBA, 0xA1,
  28018. 0x41, 0xEF, 0xB3, 0x30, 0x49, 0x17, 0x47, 0x4B,
  28019. 0x7F, 0x9F, 0x7A, 0x4B, 0x08, 0xB1, 0xA9, 0x3D,
  28020. 0xAE, 0xD9, 0x8C, 0x67, 0x49, 0x53, 0x59, 0xD3,
  28021. 0x7D, 0x67, 0xF7, 0x43, 0x8B, 0xEE, 0x5E, 0x43,
  28022. 0x58, 0x56, 0x34, 0xB2, 0x6C, 0x6B, 0x38, 0x10,
  28023. 0xD7, 0xCD, 0xCB, 0xC0, 0xF6, 0xEB, 0x87, 0x7A,
  28024. 0x60, 0x87, 0xE6, 0x8A, 0xCB, 0x84, 0x80, 0xD3,
  28025. 0xA8, 0xCF, 0x69, 0x00, 0x44, 0x7E, 0x49, 0xB4,
  28026. 0x17, 0xF1, 0x5A, 0x53, 0xB6, 0x07, 0xA0, 0xE2,
  28027. 0x16, 0xB8, 0x55, 0x97, 0x0D, 0x37, 0x40, 0x68,
  28028. 0x70, 0xB4, 0x56, 0x87, 0x22, 0xDA, 0x77, 0xA4,
  28029. 0x08, 0x47, 0x03, 0x81, 0x67, 0x84, 0xE2, 0xF1,
  28030. 0x6B, 0xED, 0x18, 0x99, 0x65, 0x32, 0xC5, 0xD8,
  28031. 0xB7, 0xF5, 0xD2, 0x14, 0x46, 0x4E, 0x5F, 0x3F,
  28032. 0x6E, 0x90, 0x58, 0x67, 0xB0, 0xCE, 0x11, 0x9E,
  28033. 0x25, 0x2A, 0x66, 0x71, 0x32, 0x53, 0x54, 0x46,
  28034. 0x85, 0xD2, 0x08, 0xE1, 0x72, 0x39, 0x08, 0xA0,
  28035. 0xCE, 0x97, 0x83, 0x46, 0x52, 0xE0, 0x8A, 0xE7,
  28036. 0xBD, 0xC8, 0x81, 0xA1, 0x31, 0xB7, 0x3C, 0x71,
  28037. 0xE8, 0x4D, 0x20, 0xD6, 0x8F, 0xDE, 0xFF, 0x4F,
  28038. 0x5D, 0x70, 0xCD, 0x1A, 0xF5, 0x7B, 0x78, 0xE3,
  28039. 0x49, 0x1A, 0x98, 0x65, 0x94, 0x23, 0x21, 0x80,
  28040. 0x0A, 0x20, 0x3C, 0x05, 0xED, 0x1F, 0xEE, 0xB5,
  28041. 0xA2, 0x8E, 0x58, 0x4E, 0x19, 0xF6, 0x53, 0x5E,
  28042. 0x7F, 0x84, 0xE4, 0xA2, 0x4F, 0x84, 0xA7, 0x2D,
  28043. 0xCA, 0xF5, 0x64, 0x8B, 0x4A, 0x42, 0x35, 0xDD,
  28044. 0x66, 0x44, 0x64, 0x48, 0x2F, 0x03, 0x17, 0x6E,
  28045. 0x88, 0x8C, 0x28, 0xBF, 0xC6, 0xC1, 0xCB, 0x23,
  28046. 0x8C, 0xFF, 0xA3, 0x5A, 0x32, 0x1E, 0x71, 0x79,
  28047. 0x1D, 0x9E, 0xA8, 0xED, 0x08, 0x78, 0xC6, 0x11,
  28048. 0x21, 0xBF, 0x8D, 0x2A, 0x4A, 0xB2, 0xC1, 0xA5,
  28049. 0xE1, 0x20, 0xBC, 0x40, 0xAB, 0xB1, 0x89, 0x2D,
  28050. 0x17, 0x15, 0x09, 0x0A, 0x0E, 0xE4, 0x82, 0x52,
  28051. 0xCA, 0x29, 0x7A, 0x99, 0xAA, 0x0E, 0x51, 0x0C,
  28052. 0xF2, 0x6B, 0x1A, 0xDD, 0x06, 0xCA, 0x54, 0x3E,
  28053. 0x1C, 0x5D, 0x6B, 0xDC, 0xD3, 0xB9, 0xC5, 0x85,
  28054. 0xC8, 0x53, 0x80, 0x45, 0xDB, 0x5C, 0x25, 0x2E,
  28055. 0xC3, 0xC8, 0xC3, 0xC9, 0x54, 0xD9, 0xBE, 0x59,
  28056. 0x07, 0x09, 0x4A, 0x89, 0x4E, 0x60, 0xEA, 0xB4,
  28057. 0x35, 0x38, 0xCF, 0xEE, 0x82, 0xE8, 0xFF, 0xC0,
  28058. 0x79, 0x1B, 0x0D, 0x0F, 0x43, 0xAC, 0x16, 0x27,
  28059. 0x83, 0x0A, 0x61, 0xD5, 0x6D, 0xAD, 0x96, 0xC6,
  28060. 0x29, 0x58, 0xB0, 0xDE, 0x78, 0x0B, 0x78, 0xBD,
  28061. 0x47, 0xA6, 0x04, 0x55, 0x0D, 0xAB, 0x83, 0xFF,
  28062. 0xF2, 0x27, 0xC3, 0x24, 0x04, 0x94, 0x71, 0xF3,
  28063. 0x52, 0x48, 0xCF, 0xB8, 0x49, 0xB2, 0x57, 0x24,
  28064. 0xFF, 0x70, 0x4D, 0x52, 0x77, 0xAA, 0x35, 0x2D,
  28065. 0x55, 0x09, 0x58, 0xBE, 0x3B, 0x23, 0x7D, 0xFF,
  28066. 0x47, 0x3E, 0xC2, 0xAD, 0xBA, 0xEA, 0x48, 0xCA,
  28067. 0x26, 0x58, 0xAE, 0xFC, 0xC7, 0x7B, 0xBD, 0x42,
  28068. 0x64, 0xAB, 0x37, 0x4D, 0x70, 0xEA, 0xE5, 0xB9,
  28069. 0x64, 0x41, 0x6C, 0xE8, 0x22, 0x6A, 0x7E, 0x32,
  28070. 0x55, 0xA0, 0xF8, 0xD7, 0xE2, 0xAD, 0xCA, 0x06,
  28071. 0x2B, 0xCD, 0x6D, 0x78, 0xD6, 0x0D, 0x1B, 0x32,
  28072. 0xE1, 0x14, 0x05, 0xBE, 0x54, 0xB6, 0x6E, 0xF0,
  28073. 0xFD, 0xDD, 0x56, 0x77, 0x02, 0xA3, 0xBC, 0xCF,
  28074. 0xED, 0xE3, 0xC5, 0x84, 0x70, 0x12, 0x69, 0xED,
  28075. 0x14, 0x80, 0x9F, 0x06, 0xF8, 0x96, 0x83, 0x56,
  28076. 0xBB, 0x92, 0x67, 0xFE, 0x86, 0xE5, 0x14, 0x25,
  28077. 0x2E, 0x88, 0xBB, 0x5C, 0x30, 0xA7, 0xEC, 0xB3,
  28078. 0xD0, 0xE6, 0x21, 0x02, 0x1E, 0xE0, 0xFB, 0xF7,
  28079. 0x87, 0x1B, 0x09, 0x34, 0x2B, 0xF8, 0x4F, 0x55,
  28080. 0xC9, 0x7E, 0xAF, 0x86, 0xC4, 0x81, 0x89, 0xC7,
  28081. 0xFF, 0x4D, 0xF3, 0x89, 0xF0, 0x77, 0xE2, 0x80,
  28082. 0x6E, 0x5F, 0xA7, 0x3B, 0x3E, 0x94, 0x58, 0xA1,
  28083. 0x6C, 0x7E, 0x27, 0x5F, 0x4F, 0x60, 0x22, 0x75,
  28084. 0x58, 0x0E, 0xB7, 0xB7, 0x13, 0x5F, 0xB5, 0x37,
  28085. 0xFA, 0x0C, 0xD9, 0x5D, 0x6E, 0xA5, 0x8C, 0x10,
  28086. 0x8C, 0xD8, 0x94, 0x3D, 0x70, 0xC1, 0x64, 0x31,
  28087. 0x11, 0xF4, 0xF0, 0x1C, 0xA8, 0xA8, 0x27, 0x6A,
  28088. 0x90, 0x26, 0x66, 0xED, 0x81, 0xB7, 0x8D, 0x16,
  28089. 0x8B, 0x00, 0x6F, 0x16, 0xAA, 0xA3, 0xD8, 0xE4,
  28090. 0xCE, 0x4F, 0x4D, 0x0F, 0xB0, 0x99, 0x7E, 0x41,
  28091. 0xAE, 0xFF, 0xB5, 0xB3, 0xDA, 0xA8, 0x38, 0x73,
  28092. 0x2F, 0x35, 0x73, 0x49, 0x44, 0x7F, 0x38, 0x77,
  28093. 0x76, 0xC7, 0x93, 0xC0, 0x47, 0x9D, 0xE9, 0xE9,
  28094. 0x94, 0x98, 0xCC, 0x35, 0x6F, 0xDB, 0x00, 0x75,
  28095. 0xA7, 0x03, 0xF2, 0x3C, 0x55, 0xD4, 0x7B, 0x55,
  28096. 0x0E, 0xC8, 0x9B, 0x02, 0xAD, 0xE8, 0x93, 0x29,
  28097. 0x08, 0x6A, 0x50, 0x84, 0x34, 0x56, 0xFE, 0xDC,
  28098. 0x37, 0x88, 0xAC, 0x8D, 0x97, 0x23, 0x3C, 0x54,
  28099. 0x56, 0x04, 0x67, 0xEE, 0x1D, 0x0F, 0x02, 0x4B,
  28100. 0x18, 0x42, 0x8F, 0x0D, 0x73, 0xB3, 0x0E, 0x19,
  28101. 0xF5, 0xC6, 0x3B, 0x9A, 0xBF, 0x11, 0x41, 0x5B,
  28102. 0xEA, 0x4D, 0x01, 0x70, 0x13, 0x0B, 0xAA, 0xBD,
  28103. 0x33, 0xC0, 0x5E, 0x65, 0x24, 0xE5, 0xFB, 0x55,
  28104. 0x81, 0xB2, 0x2B, 0x04, 0x33, 0x34, 0x22, 0x48,
  28105. 0x26, 0x6D, 0x0F, 0x10, 0x53, 0xB2, 0x45, 0xCC,
  28106. 0x24, 0x62, 0xDC, 0x44, 0xD3, 0x49, 0x65, 0x10,
  28107. 0x24, 0x82, 0xA8, 0xED, 0x9E, 0x4E, 0x96, 0x4D,
  28108. 0x56, 0x83, 0xE5, 0xD4, 0x5D, 0x0C, 0x82, 0x69
  28109. };
  28110. const byte kyber768_ss[] = {
  28111. 0x91, 0x4C, 0xB6, 0x7F, 0xE5, 0xC3, 0x8E, 0x73,
  28112. 0xBF, 0x74, 0x18, 0x1C, 0x0A, 0xC5, 0x04, 0x28,
  28113. 0xDE, 0xDF, 0x77, 0x50, 0xA9, 0x80, 0x58, 0xF7,
  28114. 0xD5, 0x36, 0x70, 0x87, 0x74, 0x53, 0x5B, 0x29
  28115. };
  28116. #else
  28117. const byte kyber768_pk[] = {
  28118. 0x86, 0x1A, 0xCF, 0x24, 0x57, 0x9D, 0x8E, 0xE8,
  28119. 0x94, 0x63, 0xB4, 0x78, 0xE2, 0xE9, 0x53, 0xC7,
  28120. 0xD5, 0xCD, 0xA4, 0xD3, 0x07, 0xD1, 0x68, 0xCF,
  28121. 0xC6, 0x79, 0xB1, 0xA3, 0x79, 0x1F, 0x05, 0x83,
  28122. 0x82, 0x86, 0x0B, 0x1C, 0x77, 0x52, 0x87, 0x89,
  28123. 0xC6, 0x96, 0x4B, 0xCC, 0x40, 0x8A, 0x21, 0xCA,
  28124. 0x4E, 0x42, 0x8F, 0xE7, 0x33, 0x9F, 0x7C, 0x08,
  28125. 0x76, 0xA8, 0x58, 0xAB, 0xA9, 0x44, 0xB7, 0x3F,
  28126. 0x9C, 0xC9, 0xB8, 0xAC, 0xC4, 0xB7, 0x9A, 0x9F,
  28127. 0x72, 0xF7, 0xA0, 0x47, 0x72, 0x57, 0xB9, 0xF3,
  28128. 0x26, 0xE4, 0x4C, 0x2C, 0x5F, 0x8B, 0x8A, 0x17,
  28129. 0x58, 0x81, 0x42, 0xB0, 0x91, 0xA3, 0x12, 0x31,
  28130. 0xD0, 0x98, 0xB0, 0x57, 0x30, 0x45, 0x62, 0x90,
  28131. 0x6E, 0x96, 0x05, 0x47, 0x3D, 0x28, 0xB7, 0xB4,
  28132. 0x33, 0x2A, 0xB4, 0x49, 0x82, 0x48, 0x7B, 0x04,
  28133. 0x9E, 0x41, 0x93, 0x74, 0xA4, 0x8E, 0xAC, 0x6A,
  28134. 0xB8, 0xCB, 0x42, 0x6D, 0x13, 0xA4, 0x86, 0x28,
  28135. 0x28, 0x43, 0x02, 0xFB, 0x72, 0x71, 0xD1, 0x54,
  28136. 0xCA, 0x09, 0x06, 0xD8, 0xE2, 0x0F, 0x1D, 0x97,
  28137. 0xBE, 0x9C, 0x82, 0x2E, 0x78, 0x83, 0x79, 0x32,
  28138. 0xEB, 0x28, 0xEE, 0xB0, 0x83, 0x71, 0xBB, 0xC6,
  28139. 0x3A, 0x7A, 0x96, 0x6C, 0xAC, 0x7A, 0x05, 0xB5,
  28140. 0x50, 0x3C, 0x55, 0x1A, 0xEF, 0x80, 0x15, 0x14,
  28141. 0xE1, 0x6A, 0xF6, 0x81, 0x02, 0x64, 0x6C, 0x66,
  28142. 0x9D, 0xD8, 0xA7, 0xE8, 0xA4, 0x11, 0x39, 0x54,
  28143. 0x62, 0xC7, 0xD4, 0x83, 0x8B, 0x91, 0x96, 0x96,
  28144. 0x54, 0x16, 0x59, 0x2B, 0xCF, 0xAC, 0x97, 0x87,
  28145. 0x0C, 0x23, 0x58, 0xA8, 0x59, 0x5B, 0x2F, 0x43,
  28146. 0x55, 0xF0, 0xFA, 0x10, 0x43, 0xA1, 0x1F, 0x62,
  28147. 0x86, 0x2F, 0xD5, 0xD9, 0x7A, 0x9D, 0xE2, 0x1B,
  28148. 0xD3, 0x39, 0x85, 0x5E, 0x17, 0x8C, 0x97, 0xD3,
  28149. 0x77, 0xD1, 0xD1, 0x46, 0xEC, 0x6A, 0x96, 0xE4,
  28150. 0xFB, 0xB7, 0x15, 0xC4, 0xC0, 0x22, 0x84, 0x55,
  28151. 0xBB, 0x64, 0xA3, 0x88, 0xDC, 0xC4, 0xD3, 0x91,
  28152. 0x3A, 0x76, 0x09, 0x57, 0xD1, 0xA6, 0x9F, 0xEA,
  28153. 0xAA, 0x08, 0x92, 0x29, 0xB8, 0xCA, 0xD0, 0xBC,
  28154. 0xC9, 0x38, 0xC7, 0x80, 0x69, 0x8A, 0x5C, 0xE1,
  28155. 0x9B, 0xC5, 0x21, 0x44, 0xDB, 0x46, 0x3B, 0x6D,
  28156. 0x55, 0x4F, 0xC4, 0xEC, 0x5E, 0x18, 0x64, 0xC4,
  28157. 0xC6, 0x88, 0x75, 0x97, 0x50, 0x0F, 0x40, 0x94,
  28158. 0x18, 0xB3, 0xA3, 0x3F, 0x2C, 0x88, 0xB2, 0x6E,
  28159. 0xD9, 0x83, 0x1D, 0xB4, 0x65, 0x08, 0xE8, 0x9F,
  28160. 0x8C, 0x33, 0x5B, 0xE1, 0x94, 0x56, 0x12, 0x05,
  28161. 0x22, 0xA8, 0xB4, 0x43, 0xFE, 0xF2, 0x1A, 0x7B,
  28162. 0xC3, 0x05, 0x9A, 0xFB, 0x4A, 0xAA, 0xD5, 0x0E,
  28163. 0x6C, 0x72, 0x8B, 0xEA, 0x8A, 0xC5, 0xDC, 0x7C,
  28164. 0x6D, 0x06, 0x94, 0x76, 0x03, 0x51, 0x0A, 0x84,
  28165. 0x90, 0xAF, 0xCE, 0x30, 0x4D, 0x57, 0xA7, 0x25,
  28166. 0xC4, 0x3B, 0x5A, 0xA3, 0x71, 0x3B, 0xA0, 0x44,
  28167. 0x98, 0x75, 0xE5, 0x67, 0x55, 0x13, 0x22, 0x60,
  28168. 0x54, 0x63, 0x6C, 0xF8, 0x9E, 0x59, 0x5A, 0xC5,
  28169. 0xAE, 0xE5, 0x66, 0x78, 0x4B, 0x80, 0xFA, 0x39,
  28170. 0x64, 0x0B, 0x74, 0x0F, 0x47, 0x12, 0x22, 0x60,
  28171. 0x25, 0x11, 0x00, 0xFA, 0x8E, 0xE0, 0xF1, 0x65,
  28172. 0x27, 0x11, 0x40, 0x0C, 0x68, 0x16, 0x38, 0x3C,
  28173. 0xC5, 0xE6, 0xDA, 0xB7, 0x9C, 0xDC, 0x3E, 0x41,
  28174. 0x98, 0x37, 0x48, 0x15, 0xB2, 0xBB, 0xA2, 0x0E,
  28175. 0xFC, 0x14, 0x25, 0xCE, 0xD1, 0x9B, 0x60, 0x19,
  28176. 0x77, 0x08, 0x08, 0x00, 0xCF, 0x54, 0x9C, 0xC4,
  28177. 0x80, 0x7D, 0xE6, 0xCA, 0x84, 0xE5, 0xD3, 0xC2,
  28178. 0xCF, 0x8B, 0x11, 0xEB, 0x68, 0x56, 0xE7, 0x1C,
  28179. 0x6B, 0xA5, 0xD2, 0x96, 0x45, 0x46, 0xAA, 0x0C,
  28180. 0x82, 0x53, 0x10, 0xF7, 0x79, 0xED, 0x69, 0x53,
  28181. 0xD9, 0x22, 0x5E, 0x11, 0xF0, 0x57, 0x93, 0x48,
  28182. 0x4C, 0x6B, 0x47, 0xB6, 0xE3, 0xD0, 0x4A, 0x4B,
  28183. 0x27, 0xB7, 0x86, 0x71, 0x46, 0xF6, 0xA0, 0x64,
  28184. 0xB9, 0x8B, 0x8B, 0xDD, 0x2B, 0x69, 0x94, 0xBA,
  28185. 0x20, 0xF8, 0x14, 0x95, 0xB3, 0x74, 0x1C, 0xE1,
  28186. 0xD3, 0x07, 0xE5, 0xEC, 0x0A, 0x7C, 0x20, 0x20,
  28187. 0x2B, 0x81, 0xA1, 0x04, 0x02, 0x83, 0x4D, 0x23,
  28188. 0x83, 0xEC, 0xB2, 0x33, 0x3B, 0x2C, 0x93, 0xD6,
  28189. 0xC4, 0x37, 0xC3, 0x17, 0x58, 0xDF, 0x92, 0x21,
  28190. 0xA3, 0xFA, 0x60, 0x24, 0x81, 0x29, 0x29, 0x62,
  28191. 0x81, 0x17, 0xCB, 0xC4, 0x79, 0x22, 0x76, 0xDA,
  28192. 0x84, 0x0F, 0xAB, 0x37, 0x03, 0x54, 0x65, 0x23,
  28193. 0x0C, 0x7A, 0x79, 0xEB, 0xB1, 0xA8, 0xC2, 0xB4,
  28194. 0x49, 0x0C, 0x2C, 0x1C, 0xB8, 0xE8, 0xB5, 0x73,
  28195. 0x5B, 0xC0, 0x72, 0xFC, 0x4D, 0x0F, 0x02, 0xB5,
  28196. 0x5B, 0x01, 0x3A, 0x53, 0x86, 0x4B, 0x82, 0x5A,
  28197. 0x85, 0xE9, 0x52, 0x03, 0x76, 0xC4, 0x4A, 0x9D,
  28198. 0xBA, 0x2C, 0xE9, 0x06, 0x8F, 0x25, 0x7A, 0x11,
  28199. 0xCF, 0xB6, 0x46, 0x6A, 0xC5, 0xAB, 0xBC, 0x85,
  28200. 0xCE, 0x80, 0x0A, 0x13, 0xB9, 0x64, 0x4D, 0x6E,
  28201. 0x04, 0x70, 0x1D, 0x23, 0xA9, 0x9A, 0x7C, 0x1A,
  28202. 0x5A, 0xEA, 0xBE, 0x2D, 0xB1, 0x34, 0x16, 0xD0,
  28203. 0xC3, 0x5C, 0x6C, 0x04, 0x98, 0x8B, 0x39, 0x91,
  28204. 0x6C, 0x46, 0x67, 0x6C, 0x66, 0x51, 0xF3, 0x5E,
  28205. 0xC2, 0x28, 0x13, 0xCF, 0xE3, 0x84, 0x40, 0x69,
  28206. 0x2F, 0x43, 0x29, 0xC5, 0x85, 0x03, 0x27, 0x30,
  28207. 0xC6, 0x94, 0xF2, 0xB2, 0x41, 0x03, 0xFB, 0x78,
  28208. 0xE4, 0x3A, 0x49, 0x52, 0x70, 0x03, 0xF4, 0xFA,
  28209. 0x5B, 0x73, 0x49, 0x2A, 0x50, 0x71, 0x83, 0x55,
  28210. 0x36, 0x26, 0x78, 0x26, 0x97, 0x37, 0x65, 0x7B,
  28211. 0x7A, 0x24, 0x03, 0xD4, 0x15, 0x09, 0xD6, 0xEC,
  28212. 0x9E, 0x51, 0x65, 0x51, 0x95, 0x56, 0xCB, 0x94,
  28213. 0x92, 0xCD, 0x6D, 0x04, 0x6F, 0x2B, 0x4C, 0x91,
  28214. 0x4A, 0xF6, 0x3C, 0xBF, 0xCC, 0xA0, 0xB4, 0xCC,
  28215. 0x44, 0x6A, 0x54, 0x69, 0x54, 0x59, 0x41, 0x66,
  28216. 0x8A, 0xBD, 0x9A, 0x26, 0x7A, 0xF2, 0x41, 0x28,
  28217. 0x33, 0xD9, 0x67, 0xB8, 0xAB, 0xB4, 0x34, 0xD7,
  28218. 0xA0, 0xAF, 0x5C, 0xAB, 0x11, 0x34, 0xB0, 0x9E,
  28219. 0xDB, 0x7A, 0xA1, 0xC1, 0x60, 0x0A, 0xF1, 0x5E,
  28220. 0xF6, 0x15, 0x27, 0xC8, 0xC1, 0xA1, 0x59, 0x40,
  28221. 0xCA, 0xB1, 0x60, 0x35, 0xF0, 0x5C, 0x71, 0x77,
  28222. 0x15, 0x90, 0x9D, 0x73, 0x2D, 0x1E, 0x9B, 0x4C,
  28223. 0x59, 0x2B, 0xB3, 0x86, 0xA5, 0xC1, 0xF3, 0xB9,
  28224. 0x89, 0x17, 0x58, 0x82, 0x05, 0xD2, 0xA8, 0xB5,
  28225. 0x03, 0x8A, 0x52, 0x4B, 0x99, 0x52, 0xD5, 0x9D,
  28226. 0x85, 0xDB, 0x27, 0x3C, 0xA1, 0xA7, 0x87, 0xF0,
  28227. 0x1F, 0x9B, 0xCB, 0x07, 0xDA, 0xC3, 0x3B, 0xC4,
  28228. 0xF0, 0xB4, 0x33, 0x36, 0x38, 0xCD, 0x3A, 0x60,
  28229. 0x1D, 0x35, 0x63, 0xF3, 0x53, 0xB5, 0xDF, 0xAA,
  28230. 0xA6, 0x2C, 0x68, 0xBF, 0x92, 0xA4, 0x79, 0xD8,
  28231. 0xB9, 0x4F, 0x81, 0x54, 0x31, 0x72, 0xB8, 0x36,
  28232. 0x26, 0xCC, 0x3B, 0x48, 0x72, 0x76, 0xAA, 0x5C,
  28233. 0xCB, 0x4D, 0x10, 0xBB, 0x18, 0xC9, 0x9F, 0x48,
  28234. 0x2A, 0x88, 0x26, 0x1C, 0x21, 0xA7, 0x90, 0x0F,
  28235. 0x60, 0x73, 0xB7, 0x06, 0xAC, 0xC5, 0xD2, 0xD7,
  28236. 0x54, 0x10, 0xB3, 0xA8, 0x84, 0x06, 0x95, 0x87,
  28237. 0x2B, 0xAC, 0xC8, 0xAA, 0x36, 0x60, 0xA4, 0x7D,
  28238. 0x6B, 0x7A, 0x5A, 0xA7, 0x9A, 0x5D, 0x28, 0xB2,
  28239. 0x6B, 0xFF, 0x80, 0x63, 0x14, 0xE6, 0x92, 0x93,
  28240. 0x30, 0x4A, 0xD0, 0x13, 0xB4, 0xE6, 0x75, 0xAE,
  28241. 0x62, 0x0B, 0x52, 0xEF, 0x30, 0x79, 0x00, 0x33,
  28242. 0xCC, 0x42, 0x78, 0xC0, 0x38, 0x50, 0xB4, 0xE6,
  28243. 0x0B, 0x39, 0x63, 0x46, 0x11, 0xDC, 0x3A, 0x5F,
  28244. 0x4C, 0x79, 0x66, 0xA9, 0xF4, 0x92, 0x0B, 0xF7,
  28245. 0x08, 0xBA, 0x8A, 0x01, 0x49, 0x1C, 0x7C, 0xEC,
  28246. 0x97, 0x40, 0xFA, 0x97, 0x6E, 0x8E, 0x76, 0x13,
  28247. 0x00, 0x60, 0x5F, 0x75, 0x20, 0x88, 0x56, 0x56,
  28248. 0x5F, 0x4F, 0x55, 0x60, 0x8F, 0x61, 0x37, 0x92,
  28249. 0x99, 0xA6, 0x4C, 0x96, 0x6B, 0xAD, 0x4A, 0x0E,
  28250. 0x87, 0x92, 0xAD, 0x3D, 0x46, 0x86, 0x41, 0x28,
  28251. 0x37, 0x61, 0xA5, 0x39, 0x99, 0x25, 0x86, 0x63,
  28252. 0x30, 0xB5, 0xBA, 0x37, 0x07, 0xE8, 0x54, 0xA6,
  28253. 0x3F, 0xE7, 0xAA, 0x7F, 0x81, 0x1A, 0xA4, 0x57,
  28254. 0x54, 0x08, 0xC8, 0xC2, 0x06, 0xBB, 0x39, 0x53,
  28255. 0xA7, 0x66, 0x29, 0x9A, 0x5C, 0x26, 0xC6, 0x7A,
  28256. 0x4E, 0x58, 0x21, 0x05, 0x79, 0xB3, 0x10, 0xF7,
  28257. 0x99, 0xB0, 0x97, 0x9B, 0xB9, 0x45, 0x8A, 0xEC,
  28258. 0x21, 0x50, 0xAD, 0x27, 0x3F, 0xEB, 0xCB, 0x3E,
  28259. 0xF3, 0xB6, 0x0B, 0x38, 0x56, 0xA7, 0x16, 0xB9,
  28260. 0x78, 0x3D, 0xF8, 0x08, 0xC6, 0x9C, 0x06, 0x12,
  28261. 0xA0, 0x9E, 0xA5, 0x93, 0x16, 0x64, 0x65, 0x87,
  28262. 0x69, 0x2D, 0x3C, 0x8E, 0x8C, 0x73, 0x9F, 0xB3,
  28263. 0x78, 0x27, 0x74, 0x15, 0x61, 0xD5, 0xE7, 0x66,
  28264. 0x67, 0x51, 0x82, 0xFF, 0x83, 0xC9, 0x1C, 0xF0,
  28265. 0x33, 0x65, 0x88, 0x23, 0xD8, 0xFA, 0x91, 0xE1
  28266. };
  28267. const byte kyber768_sk[] = {
  28268. 0x1B, 0xD1, 0x97, 0x95, 0x10, 0xBC, 0xE5, 0x03,
  28269. 0x1C, 0x0F, 0x99, 0x1D, 0xE0, 0x82, 0xC3, 0x89,
  28270. 0xA1, 0xBD, 0x15, 0x57, 0x19, 0x1D, 0x81, 0x03,
  28271. 0xBD, 0xCC, 0x04, 0xC1, 0x98, 0xAE, 0x3E, 0x95,
  28272. 0x95, 0xFD, 0xB6, 0x57, 0xB5, 0xC6, 0x39, 0x97,
  28273. 0x95, 0x79, 0xAE, 0x13, 0x5B, 0x11, 0xE2, 0x4D,
  28274. 0x36, 0xE5, 0x49, 0x44, 0x67, 0x83, 0x2E, 0x6A,
  28275. 0x8A, 0x46, 0xAA, 0x8F, 0x9F, 0xF9, 0x87, 0x4A,
  28276. 0x16, 0x00, 0x27, 0x84, 0x07, 0x71, 0x56, 0x2D,
  28277. 0x74, 0x61, 0xB1, 0x6C, 0x61, 0x87, 0xF3, 0xC2,
  28278. 0xC1, 0x9C, 0x9C, 0x8F, 0xE6, 0x41, 0x49, 0x7D,
  28279. 0x8A, 0x15, 0x5C, 0x51, 0x15, 0x7D, 0x91, 0x35,
  28280. 0xD9, 0xB4, 0x5D, 0x3C, 0x7C, 0x4C, 0xB1, 0x82,
  28281. 0x31, 0x8C, 0x11, 0xAD, 0x08, 0xB3, 0x9F, 0xAC,
  28282. 0x04, 0x15, 0xE6, 0xE2, 0x6D, 0xB3, 0x43, 0x91,
  28283. 0xDE, 0xC2, 0x47, 0x1B, 0x30, 0x3C, 0x05, 0x05,
  28284. 0xB6, 0xFE, 0x45, 0x6E, 0x81, 0x50, 0x21, 0xE8,
  28285. 0x2B, 0x5F, 0x2D, 0x16, 0x6F, 0xDF, 0xB5, 0xB9,
  28286. 0x23, 0xC8, 0x3A, 0x61, 0x67, 0x30, 0x28, 0xF0,
  28287. 0xBA, 0xE0, 0x28, 0x08, 0x9F, 0x47, 0x61, 0x67,
  28288. 0xE5, 0x75, 0x6C, 0xAA, 0x3E, 0xE0, 0xF1, 0x89,
  28289. 0xC6, 0x47, 0x81, 0xC7, 0x49, 0x27, 0xBC, 0xDB,
  28290. 0x67, 0xE7, 0x75, 0xCC, 0xA8, 0x5B, 0x44, 0xB8,
  28291. 0x8B, 0x10, 0x61, 0xD8, 0x96, 0x3F, 0xE4, 0x0B,
  28292. 0x1F, 0xB4, 0x34, 0x68, 0x20, 0x09, 0x0E, 0x5C,
  28293. 0x0B, 0x7C, 0x49, 0x08, 0x89, 0x33, 0xAD, 0xE3,
  28294. 0x82, 0x96, 0x88, 0x6C, 0x43, 0x45, 0x65, 0x7C,
  28295. 0x3F, 0x07, 0xCF, 0x1A, 0xE9, 0x76, 0xE6, 0xBA,
  28296. 0x77, 0x7E, 0xF0, 0x1F, 0xB9, 0xC3, 0xB0, 0xDC,
  28297. 0xF0, 0x58, 0x3A, 0xE5, 0x44, 0xCD, 0x80, 0x3A,
  28298. 0xC1, 0x39, 0xCA, 0x61, 0x9B, 0x8D, 0xCA, 0x13,
  28299. 0x1C, 0xD2, 0x73, 0xCE, 0xCB, 0x65, 0xBC, 0xEF,
  28300. 0x00, 0x29, 0x95, 0x12, 0xB0, 0x21, 0xBA, 0x1B,
  28301. 0x2F, 0xF2, 0x08, 0xEE, 0xBB, 0x08, 0xA6, 0x28,
  28302. 0x7E, 0xC4, 0x4C, 0x21, 0x7D, 0x94, 0x76, 0x40,
  28303. 0x42, 0x5C, 0x0E, 0x97, 0x13, 0x44, 0x5B, 0xC5,
  28304. 0x2B, 0xC3, 0xCD, 0x66, 0x01, 0x2C, 0x8C, 0xB7,
  28305. 0x10, 0xA2, 0xE8, 0xAD, 0x5F, 0xC5, 0x1C, 0x5B,
  28306. 0x71, 0xCE, 0x69, 0xE9, 0x6A, 0x22, 0x06, 0x6F,
  28307. 0x74, 0x4B, 0xBA, 0xB6, 0x47, 0x2F, 0xEF, 0x3A,
  28308. 0x5B, 0xCF, 0xD6, 0x1D, 0xF1, 0xF8, 0x6A, 0x95,
  28309. 0x64, 0x80, 0x03, 0x1A, 0x99, 0x0E, 0xE0, 0x7D,
  28310. 0xE5, 0x04, 0x5E, 0xBC, 0x49, 0x8F, 0x2D, 0x3A,
  28311. 0xBD, 0xE3, 0x28, 0xC4, 0x2A, 0xCB, 0x21, 0xAD,
  28312. 0x62, 0x5D, 0x7B, 0xCB, 0x85, 0x5A, 0x8A, 0x0A,
  28313. 0xCB, 0xA2, 0x85, 0x16, 0xD3, 0x5A, 0xAF, 0x92,
  28314. 0xBE, 0xB3, 0x9C, 0x46, 0xD8, 0xF4, 0xA0, 0x1F,
  28315. 0x73, 0x20, 0x40, 0xF2, 0x7C, 0x40, 0x9B, 0xA4,
  28316. 0x9A, 0x80, 0xCF, 0x95, 0x55, 0x7C, 0x73, 0x7C,
  28317. 0x4A, 0x45, 0x11, 0x2C, 0x52, 0x2C, 0x78, 0x04,
  28318. 0xC7, 0x7A, 0x3D, 0x44, 0x53, 0xBA, 0x82, 0xC1,
  28319. 0x49, 0x08, 0xBC, 0xC0, 0x47, 0x13, 0x7E, 0x5A,
  28320. 0x7B, 0x23, 0x92, 0xC8, 0x31, 0x25, 0x4C, 0x1A,
  28321. 0x59, 0x54, 0xC8, 0x8C, 0xCF, 0xA5, 0xD8, 0x8B,
  28322. 0x3B, 0x93, 0x31, 0xBE, 0xF4, 0x62, 0x55, 0x09,
  28323. 0x14, 0xD1, 0x84, 0x17, 0x59, 0x40, 0xB4, 0xE9,
  28324. 0xB7, 0xAD, 0x3D, 0x44, 0x2E, 0xA9, 0x68, 0xB6,
  28325. 0xA4, 0x0A, 0x19, 0x72, 0x71, 0x5B, 0xCB, 0x72,
  28326. 0x82, 0x14, 0xA1, 0xBE, 0x8E, 0x04, 0x1D, 0xF5,
  28327. 0x72, 0xCC, 0x2D, 0x01, 0x6F, 0xFD, 0x6B, 0x0A,
  28328. 0xFA, 0xC8, 0x02, 0xBA, 0x97, 0x5A, 0x4A, 0x31,
  28329. 0xA4, 0x18, 0x40, 0x2E, 0x6B, 0x01, 0x41, 0xFA,
  28330. 0xD6, 0x48, 0x49, 0xE1, 0x6C, 0x80, 0x37, 0x23,
  28331. 0xA7, 0x74, 0x2B, 0x08, 0x2A, 0x7A, 0x56, 0x26,
  28332. 0x4E, 0x02, 0x60, 0xA2, 0x4A, 0x66, 0x42, 0x3D,
  28333. 0x52, 0x64, 0x82, 0x07, 0x04, 0x06, 0x5C, 0xAC,
  28334. 0x41, 0x10, 0x72, 0x48, 0x64, 0x37, 0xCA, 0xA5,
  28335. 0x47, 0x2D, 0x73, 0x3C, 0x64, 0x14, 0x5F, 0x10,
  28336. 0x0B, 0x0B, 0x16, 0xC0, 0xAB, 0x0D, 0xB8, 0x7C,
  28337. 0xC5, 0x64, 0x26, 0xC1, 0x1A, 0x69, 0x45, 0x25,
  28338. 0x95, 0x8D, 0xB8, 0x31, 0x59, 0x31, 0xC5, 0x09,
  28339. 0xD6, 0x3C, 0x48, 0x10, 0xBB, 0xA7, 0x68, 0xCA,
  28340. 0xCB, 0xF9, 0xCD, 0x85, 0x7B, 0x0D, 0x66, 0xD6,
  28341. 0x5A, 0xF7, 0xEB, 0xAC, 0x18, 0xB7, 0x9C, 0xBE,
  28342. 0xBC, 0x0A, 0x49, 0xB5, 0xC0, 0x5A, 0xA8, 0x7F,
  28343. 0x8D, 0xE8, 0x25, 0xA6, 0xA1, 0x13, 0x5F, 0xE8,
  28344. 0x64, 0xCF, 0x6C, 0x99, 0x8D, 0xDC, 0x37, 0xFC,
  28345. 0x01, 0x56, 0xBB, 0x52, 0xA7, 0x8E, 0xC2, 0x99,
  28346. 0xCA, 0x53, 0x1C, 0xF8, 0x75, 0xBE, 0xFA, 0x48,
  28347. 0x75, 0x72, 0x60, 0x28, 0xED, 0x37, 0x91, 0xBA,
  28348. 0x31, 0xC0, 0x0E, 0xC7, 0x40, 0x20, 0xCA, 0xA1,
  28349. 0x7F, 0x91, 0x19, 0x63, 0x96, 0xCF, 0xE5, 0x88,
  28350. 0x6E, 0x51, 0x1A, 0x52, 0xD0, 0xCA, 0x8D, 0xEE,
  28351. 0x25, 0xBF, 0x4E, 0x77, 0xCB, 0x92, 0xE6, 0x36,
  28352. 0x40, 0x64, 0x6D, 0xE2, 0x58, 0x6D, 0xCD, 0x00,
  28353. 0x14, 0x16, 0xCC, 0xC1, 0xEC, 0x30, 0x7C, 0xA2,
  28354. 0x75, 0x6D, 0xBE, 0xA8, 0x26, 0x36, 0x14, 0x51,
  28355. 0x3C, 0xA0, 0x5E, 0x70, 0xC7, 0x54, 0xF5, 0x6A,
  28356. 0x2C, 0x3F, 0xF7, 0x42, 0x94, 0x8C, 0x34, 0xAF,
  28357. 0x49, 0x34, 0x2F, 0x55, 0x03, 0x4C, 0x1C, 0x70,
  28358. 0xE0, 0x79, 0x6D, 0x26, 0xB2, 0x30, 0x35, 0x6B,
  28359. 0x1B, 0x74, 0xA1, 0x6C, 0x0B, 0x28, 0x37, 0x4B,
  28360. 0x79, 0xC1, 0x6B, 0x04, 0x96, 0xCA, 0x20, 0x12,
  28361. 0x3E, 0x6C, 0x67, 0x62, 0xD9, 0x01, 0xE2, 0x76,
  28362. 0x08, 0x5D, 0x81, 0x2A, 0xE9, 0xC3, 0x45, 0xAD,
  28363. 0xF5, 0x09, 0xDD, 0xC7, 0x87, 0x61, 0x62, 0xCE,
  28364. 0x34, 0xA6, 0x00, 0x3F, 0xB4, 0x5C, 0xD8, 0x66,
  28365. 0x0F, 0xDD, 0x46, 0x71, 0x6E, 0x82, 0x55, 0x06,
  28366. 0x62, 0x37, 0x5D, 0xB5, 0x5E, 0x63, 0xA2, 0x0F,
  28367. 0x01, 0x2A, 0x71, 0xE3, 0xD9, 0x72, 0x25, 0xC5,
  28368. 0x8F, 0x52, 0x34, 0xC6, 0xB5, 0x3A, 0x8A, 0xA0,
  28369. 0x33, 0x9C, 0xEF, 0xD4, 0x84, 0xE4, 0x7A, 0x3D,
  28370. 0xE6, 0x49, 0x88, 0x05, 0xC2, 0x82, 0xE0, 0x76,
  28371. 0x8E, 0x35, 0xC7, 0x5C, 0x20, 0x07, 0x76, 0x5C,
  28372. 0xA1, 0x7F, 0x01, 0xD2, 0x85, 0xA7, 0xC3, 0x04,
  28373. 0x8B, 0xC7, 0x2B, 0xA3, 0xAA, 0x57, 0x7C, 0x1C,
  28374. 0x43, 0x2A, 0x3C, 0x76, 0x9D, 0x21, 0xB8, 0x6C,
  28375. 0x01, 0x8F, 0x81, 0x7A, 0x26, 0x67, 0x35, 0x1C,
  28376. 0x2B, 0xC8, 0x5F, 0xA5, 0x53, 0x6F, 0xA7, 0xA6,
  28377. 0xCD, 0xEF, 0xCC, 0x0D, 0xD6, 0xF9, 0x1B, 0xDD,
  28378. 0xE8, 0x9D, 0x4E, 0x51, 0x6C, 0x8B, 0x75, 0x40,
  28379. 0xA7, 0xDC, 0x3F, 0xC4, 0x89, 0x8A, 0xB3, 0x30,
  28380. 0x08, 0xBC, 0xC1, 0x9F, 0x9E, 0x46, 0x3D, 0xE9,
  28381. 0x36, 0x26, 0x99, 0x4C, 0xC9, 0x27, 0xA3, 0x4A,
  28382. 0x9D, 0x70, 0x40, 0xC1, 0x31, 0xC8, 0x1F, 0x07,
  28383. 0x83, 0xDD, 0x68, 0x84, 0x29, 0x43, 0xC5, 0x5D,
  28384. 0xC5, 0x77, 0xD3, 0x67, 0xA4, 0x01, 0x83, 0x94,
  28385. 0xBB, 0x11, 0x60, 0x72, 0xAC, 0x65, 0xF2, 0xEB,
  28386. 0x9B, 0x4F, 0xF5, 0x8F, 0x41, 0x15, 0x40, 0x1B,
  28387. 0x11, 0x7D, 0x91, 0x3B, 0x75, 0x75, 0x57, 0x57,
  28388. 0x19, 0x39, 0x0C, 0x9E, 0x80, 0x06, 0x97, 0xA8,
  28389. 0x28, 0xB1, 0x25, 0x83, 0xF9, 0x4A, 0x2F, 0x8F,
  28390. 0x72, 0x0A, 0x6E, 0x87, 0x00, 0xFD, 0x40, 0x7D,
  28391. 0x73, 0xA4, 0xC7, 0x97, 0x55, 0x2B, 0x98, 0x75,
  28392. 0x7A, 0x5F, 0x57, 0x82, 0xA9, 0x50, 0x95, 0xDF,
  28393. 0xA5, 0xAC, 0xB7, 0xD9, 0x3A, 0x80, 0x5C, 0x30,
  28394. 0x18, 0x30, 0x4E, 0x5D, 0x22, 0xB7, 0x6F, 0xA5,
  28395. 0xAA, 0x51, 0x96, 0xB7, 0xEB, 0xDB, 0x7A, 0xA0,
  28396. 0xA5, 0x2D, 0x06, 0xAC, 0x4B, 0x9E, 0x4C, 0x00,
  28397. 0xC0, 0xF8, 0x5C, 0xEC, 0xB0, 0xBD, 0x95, 0x56,
  28398. 0x2D, 0x56, 0x23, 0x30, 0x78, 0x51, 0x43, 0xC7,
  28399. 0xF1, 0x29, 0xFA, 0xF9, 0x19, 0x48, 0xB0, 0xC8,
  28400. 0x8C, 0x60, 0x82, 0xE0, 0xF3, 0x03, 0x54, 0x74,
  28401. 0x12, 0x6E, 0x23, 0x6B, 0x80, 0xD2, 0x22, 0xD2,
  28402. 0x81, 0x4F, 0x6A, 0xF2, 0x4B, 0xD5, 0xD1, 0x19,
  28403. 0x86, 0x33, 0x1F, 0xAF, 0xC2, 0x1E, 0xC8, 0x4C,
  28404. 0x93, 0xA8, 0x81, 0x24, 0x72, 0x51, 0x10, 0x5A,
  28405. 0x6A, 0xB4, 0x35, 0xD7, 0x6E, 0xFD, 0x88, 0x9D,
  28406. 0x27, 0xC0, 0x0C, 0x2A, 0x14, 0xBD, 0x04, 0xA6,
  28407. 0xBD, 0x0B, 0x20, 0x07, 0xB5, 0x51, 0x5A, 0x75,
  28408. 0xF1, 0x14, 0xD1, 0x92, 0x10, 0xBC, 0x70, 0x50,
  28409. 0x85, 0x33, 0x53, 0x2F, 0x1B, 0x0A, 0x30, 0x0C,
  28410. 0x8B, 0x12, 0xF0, 0x0A, 0xC4, 0xFC, 0x46, 0x0F,
  28411. 0xA8, 0x4A, 0x2D, 0x15, 0x21, 0xBD, 0x0B, 0x42,
  28412. 0x86, 0x1A, 0xCF, 0x24, 0x57, 0x9D, 0x8E, 0xE8,
  28413. 0x94, 0x63, 0xB4, 0x78, 0xE2, 0xE9, 0x53, 0xC7,
  28414. 0xD5, 0xCD, 0xA4, 0xD3, 0x07, 0xD1, 0x68, 0xCF,
  28415. 0xC6, 0x79, 0xB1, 0xA3, 0x79, 0x1F, 0x05, 0x83,
  28416. 0x82, 0x86, 0x0B, 0x1C, 0x77, 0x52, 0x87, 0x89,
  28417. 0xC6, 0x96, 0x4B, 0xCC, 0x40, 0x8A, 0x21, 0xCA,
  28418. 0x4E, 0x42, 0x8F, 0xE7, 0x33, 0x9F, 0x7C, 0x08,
  28419. 0x76, 0xA8, 0x58, 0xAB, 0xA9, 0x44, 0xB7, 0x3F,
  28420. 0x9C, 0xC9, 0xB8, 0xAC, 0xC4, 0xB7, 0x9A, 0x9F,
  28421. 0x72, 0xF7, 0xA0, 0x47, 0x72, 0x57, 0xB9, 0xF3,
  28422. 0x26, 0xE4, 0x4C, 0x2C, 0x5F, 0x8B, 0x8A, 0x17,
  28423. 0x58, 0x81, 0x42, 0xB0, 0x91, 0xA3, 0x12, 0x31,
  28424. 0xD0, 0x98, 0xB0, 0x57, 0x30, 0x45, 0x62, 0x90,
  28425. 0x6E, 0x96, 0x05, 0x47, 0x3D, 0x28, 0xB7, 0xB4,
  28426. 0x33, 0x2A, 0xB4, 0x49, 0x82, 0x48, 0x7B, 0x04,
  28427. 0x9E, 0x41, 0x93, 0x74, 0xA4, 0x8E, 0xAC, 0x6A,
  28428. 0xB8, 0xCB, 0x42, 0x6D, 0x13, 0xA4, 0x86, 0x28,
  28429. 0x28, 0x43, 0x02, 0xFB, 0x72, 0x71, 0xD1, 0x54,
  28430. 0xCA, 0x09, 0x06, 0xD8, 0xE2, 0x0F, 0x1D, 0x97,
  28431. 0xBE, 0x9C, 0x82, 0x2E, 0x78, 0x83, 0x79, 0x32,
  28432. 0xEB, 0x28, 0xEE, 0xB0, 0x83, 0x71, 0xBB, 0xC6,
  28433. 0x3A, 0x7A, 0x96, 0x6C, 0xAC, 0x7A, 0x05, 0xB5,
  28434. 0x50, 0x3C, 0x55, 0x1A, 0xEF, 0x80, 0x15, 0x14,
  28435. 0xE1, 0x6A, 0xF6, 0x81, 0x02, 0x64, 0x6C, 0x66,
  28436. 0x9D, 0xD8, 0xA7, 0xE8, 0xA4, 0x11, 0x39, 0x54,
  28437. 0x62, 0xC7, 0xD4, 0x83, 0x8B, 0x91, 0x96, 0x96,
  28438. 0x54, 0x16, 0x59, 0x2B, 0xCF, 0xAC, 0x97, 0x87,
  28439. 0x0C, 0x23, 0x58, 0xA8, 0x59, 0x5B, 0x2F, 0x43,
  28440. 0x55, 0xF0, 0xFA, 0x10, 0x43, 0xA1, 0x1F, 0x62,
  28441. 0x86, 0x2F, 0xD5, 0xD9, 0x7A, 0x9D, 0xE2, 0x1B,
  28442. 0xD3, 0x39, 0x85, 0x5E, 0x17, 0x8C, 0x97, 0xD3,
  28443. 0x77, 0xD1, 0xD1, 0x46, 0xEC, 0x6A, 0x96, 0xE4,
  28444. 0xFB, 0xB7, 0x15, 0xC4, 0xC0, 0x22, 0x84, 0x55,
  28445. 0xBB, 0x64, 0xA3, 0x88, 0xDC, 0xC4, 0xD3, 0x91,
  28446. 0x3A, 0x76, 0x09, 0x57, 0xD1, 0xA6, 0x9F, 0xEA,
  28447. 0xAA, 0x08, 0x92, 0x29, 0xB8, 0xCA, 0xD0, 0xBC,
  28448. 0xC9, 0x38, 0xC7, 0x80, 0x69, 0x8A, 0x5C, 0xE1,
  28449. 0x9B, 0xC5, 0x21, 0x44, 0xDB, 0x46, 0x3B, 0x6D,
  28450. 0x55, 0x4F, 0xC4, 0xEC, 0x5E, 0x18, 0x64, 0xC4,
  28451. 0xC6, 0x88, 0x75, 0x97, 0x50, 0x0F, 0x40, 0x94,
  28452. 0x18, 0xB3, 0xA3, 0x3F, 0x2C, 0x88, 0xB2, 0x6E,
  28453. 0xD9, 0x83, 0x1D, 0xB4, 0x65, 0x08, 0xE8, 0x9F,
  28454. 0x8C, 0x33, 0x5B, 0xE1, 0x94, 0x56, 0x12, 0x05,
  28455. 0x22, 0xA8, 0xB4, 0x43, 0xFE, 0xF2, 0x1A, 0x7B,
  28456. 0xC3, 0x05, 0x9A, 0xFB, 0x4A, 0xAA, 0xD5, 0x0E,
  28457. 0x6C, 0x72, 0x8B, 0xEA, 0x8A, 0xC5, 0xDC, 0x7C,
  28458. 0x6D, 0x06, 0x94, 0x76, 0x03, 0x51, 0x0A, 0x84,
  28459. 0x90, 0xAF, 0xCE, 0x30, 0x4D, 0x57, 0xA7, 0x25,
  28460. 0xC4, 0x3B, 0x5A, 0xA3, 0x71, 0x3B, 0xA0, 0x44,
  28461. 0x98, 0x75, 0xE5, 0x67, 0x55, 0x13, 0x22, 0x60,
  28462. 0x54, 0x63, 0x6C, 0xF8, 0x9E, 0x59, 0x5A, 0xC5,
  28463. 0xAE, 0xE5, 0x66, 0x78, 0x4B, 0x80, 0xFA, 0x39,
  28464. 0x64, 0x0B, 0x74, 0x0F, 0x47, 0x12, 0x22, 0x60,
  28465. 0x25, 0x11, 0x00, 0xFA, 0x8E, 0xE0, 0xF1, 0x65,
  28466. 0x27, 0x11, 0x40, 0x0C, 0x68, 0x16, 0x38, 0x3C,
  28467. 0xC5, 0xE6, 0xDA, 0xB7, 0x9C, 0xDC, 0x3E, 0x41,
  28468. 0x98, 0x37, 0x48, 0x15, 0xB2, 0xBB, 0xA2, 0x0E,
  28469. 0xFC, 0x14, 0x25, 0xCE, 0xD1, 0x9B, 0x60, 0x19,
  28470. 0x77, 0x08, 0x08, 0x00, 0xCF, 0x54, 0x9C, 0xC4,
  28471. 0x80, 0x7D, 0xE6, 0xCA, 0x84, 0xE5, 0xD3, 0xC2,
  28472. 0xCF, 0x8B, 0x11, 0xEB, 0x68, 0x56, 0xE7, 0x1C,
  28473. 0x6B, 0xA5, 0xD2, 0x96, 0x45, 0x46, 0xAA, 0x0C,
  28474. 0x82, 0x53, 0x10, 0xF7, 0x79, 0xED, 0x69, 0x53,
  28475. 0xD9, 0x22, 0x5E, 0x11, 0xF0, 0x57, 0x93, 0x48,
  28476. 0x4C, 0x6B, 0x47, 0xB6, 0xE3, 0xD0, 0x4A, 0x4B,
  28477. 0x27, 0xB7, 0x86, 0x71, 0x46, 0xF6, 0xA0, 0x64,
  28478. 0xB9, 0x8B, 0x8B, 0xDD, 0x2B, 0x69, 0x94, 0xBA,
  28479. 0x20, 0xF8, 0x14, 0x95, 0xB3, 0x74, 0x1C, 0xE1,
  28480. 0xD3, 0x07, 0xE5, 0xEC, 0x0A, 0x7C, 0x20, 0x20,
  28481. 0x2B, 0x81, 0xA1, 0x04, 0x02, 0x83, 0x4D, 0x23,
  28482. 0x83, 0xEC, 0xB2, 0x33, 0x3B, 0x2C, 0x93, 0xD6,
  28483. 0xC4, 0x37, 0xC3, 0x17, 0x58, 0xDF, 0x92, 0x21,
  28484. 0xA3, 0xFA, 0x60, 0x24, 0x81, 0x29, 0x29, 0x62,
  28485. 0x81, 0x17, 0xCB, 0xC4, 0x79, 0x22, 0x76, 0xDA,
  28486. 0x84, 0x0F, 0xAB, 0x37, 0x03, 0x54, 0x65, 0x23,
  28487. 0x0C, 0x7A, 0x79, 0xEB, 0xB1, 0xA8, 0xC2, 0xB4,
  28488. 0x49, 0x0C, 0x2C, 0x1C, 0xB8, 0xE8, 0xB5, 0x73,
  28489. 0x5B, 0xC0, 0x72, 0xFC, 0x4D, 0x0F, 0x02, 0xB5,
  28490. 0x5B, 0x01, 0x3A, 0x53, 0x86, 0x4B, 0x82, 0x5A,
  28491. 0x85, 0xE9, 0x52, 0x03, 0x76, 0xC4, 0x4A, 0x9D,
  28492. 0xBA, 0x2C, 0xE9, 0x06, 0x8F, 0x25, 0x7A, 0x11,
  28493. 0xCF, 0xB6, 0x46, 0x6A, 0xC5, 0xAB, 0xBC, 0x85,
  28494. 0xCE, 0x80, 0x0A, 0x13, 0xB9, 0x64, 0x4D, 0x6E,
  28495. 0x04, 0x70, 0x1D, 0x23, 0xA9, 0x9A, 0x7C, 0x1A,
  28496. 0x5A, 0xEA, 0xBE, 0x2D, 0xB1, 0x34, 0x16, 0xD0,
  28497. 0xC3, 0x5C, 0x6C, 0x04, 0x98, 0x8B, 0x39, 0x91,
  28498. 0x6C, 0x46, 0x67, 0x6C, 0x66, 0x51, 0xF3, 0x5E,
  28499. 0xC2, 0x28, 0x13, 0xCF, 0xE3, 0x84, 0x40, 0x69,
  28500. 0x2F, 0x43, 0x29, 0xC5, 0x85, 0x03, 0x27, 0x30,
  28501. 0xC6, 0x94, 0xF2, 0xB2, 0x41, 0x03, 0xFB, 0x78,
  28502. 0xE4, 0x3A, 0x49, 0x52, 0x70, 0x03, 0xF4, 0xFA,
  28503. 0x5B, 0x73, 0x49, 0x2A, 0x50, 0x71, 0x83, 0x55,
  28504. 0x36, 0x26, 0x78, 0x26, 0x97, 0x37, 0x65, 0x7B,
  28505. 0x7A, 0x24, 0x03, 0xD4, 0x15, 0x09, 0xD6, 0xEC,
  28506. 0x9E, 0x51, 0x65, 0x51, 0x95, 0x56, 0xCB, 0x94,
  28507. 0x92, 0xCD, 0x6D, 0x04, 0x6F, 0x2B, 0x4C, 0x91,
  28508. 0x4A, 0xF6, 0x3C, 0xBF, 0xCC, 0xA0, 0xB4, 0xCC,
  28509. 0x44, 0x6A, 0x54, 0x69, 0x54, 0x59, 0x41, 0x66,
  28510. 0x8A, 0xBD, 0x9A, 0x26, 0x7A, 0xF2, 0x41, 0x28,
  28511. 0x33, 0xD9, 0x67, 0xB8, 0xAB, 0xB4, 0x34, 0xD7,
  28512. 0xA0, 0xAF, 0x5C, 0xAB, 0x11, 0x34, 0xB0, 0x9E,
  28513. 0xDB, 0x7A, 0xA1, 0xC1, 0x60, 0x0A, 0xF1, 0x5E,
  28514. 0xF6, 0x15, 0x27, 0xC8, 0xC1, 0xA1, 0x59, 0x40,
  28515. 0xCA, 0xB1, 0x60, 0x35, 0xF0, 0x5C, 0x71, 0x77,
  28516. 0x15, 0x90, 0x9D, 0x73, 0x2D, 0x1E, 0x9B, 0x4C,
  28517. 0x59, 0x2B, 0xB3, 0x86, 0xA5, 0xC1, 0xF3, 0xB9,
  28518. 0x89, 0x17, 0x58, 0x82, 0x05, 0xD2, 0xA8, 0xB5,
  28519. 0x03, 0x8A, 0x52, 0x4B, 0x99, 0x52, 0xD5, 0x9D,
  28520. 0x85, 0xDB, 0x27, 0x3C, 0xA1, 0xA7, 0x87, 0xF0,
  28521. 0x1F, 0x9B, 0xCB, 0x07, 0xDA, 0xC3, 0x3B, 0xC4,
  28522. 0xF0, 0xB4, 0x33, 0x36, 0x38, 0xCD, 0x3A, 0x60,
  28523. 0x1D, 0x35, 0x63, 0xF3, 0x53, 0xB5, 0xDF, 0xAA,
  28524. 0xA6, 0x2C, 0x68, 0xBF, 0x92, 0xA4, 0x79, 0xD8,
  28525. 0xB9, 0x4F, 0x81, 0x54, 0x31, 0x72, 0xB8, 0x36,
  28526. 0x26, 0xCC, 0x3B, 0x48, 0x72, 0x76, 0xAA, 0x5C,
  28527. 0xCB, 0x4D, 0x10, 0xBB, 0x18, 0xC9, 0x9F, 0x48,
  28528. 0x2A, 0x88, 0x26, 0x1C, 0x21, 0xA7, 0x90, 0x0F,
  28529. 0x60, 0x73, 0xB7, 0x06, 0xAC, 0xC5, 0xD2, 0xD7,
  28530. 0x54, 0x10, 0xB3, 0xA8, 0x84, 0x06, 0x95, 0x87,
  28531. 0x2B, 0xAC, 0xC8, 0xAA, 0x36, 0x60, 0xA4, 0x7D,
  28532. 0x6B, 0x7A, 0x5A, 0xA7, 0x9A, 0x5D, 0x28, 0xB2,
  28533. 0x6B, 0xFF, 0x80, 0x63, 0x14, 0xE6, 0x92, 0x93,
  28534. 0x30, 0x4A, 0xD0, 0x13, 0xB4, 0xE6, 0x75, 0xAE,
  28535. 0x62, 0x0B, 0x52, 0xEF, 0x30, 0x79, 0x00, 0x33,
  28536. 0xCC, 0x42, 0x78, 0xC0, 0x38, 0x50, 0xB4, 0xE6,
  28537. 0x0B, 0x39, 0x63, 0x46, 0x11, 0xDC, 0x3A, 0x5F,
  28538. 0x4C, 0x79, 0x66, 0xA9, 0xF4, 0x92, 0x0B, 0xF7,
  28539. 0x08, 0xBA, 0x8A, 0x01, 0x49, 0x1C, 0x7C, 0xEC,
  28540. 0x97, 0x40, 0xFA, 0x97, 0x6E, 0x8E, 0x76, 0x13,
  28541. 0x00, 0x60, 0x5F, 0x75, 0x20, 0x88, 0x56, 0x56,
  28542. 0x5F, 0x4F, 0x55, 0x60, 0x8F, 0x61, 0x37, 0x92,
  28543. 0x99, 0xA6, 0x4C, 0x96, 0x6B, 0xAD, 0x4A, 0x0E,
  28544. 0x87, 0x92, 0xAD, 0x3D, 0x46, 0x86, 0x41, 0x28,
  28545. 0x37, 0x61, 0xA5, 0x39, 0x99, 0x25, 0x86, 0x63,
  28546. 0x30, 0xB5, 0xBA, 0x37, 0x07, 0xE8, 0x54, 0xA6,
  28547. 0x3F, 0xE7, 0xAA, 0x7F, 0x81, 0x1A, 0xA4, 0x57,
  28548. 0x54, 0x08, 0xC8, 0xC2, 0x06, 0xBB, 0x39, 0x53,
  28549. 0xA7, 0x66, 0x29, 0x9A, 0x5C, 0x26, 0xC6, 0x7A,
  28550. 0x4E, 0x58, 0x21, 0x05, 0x79, 0xB3, 0x10, 0xF7,
  28551. 0x99, 0xB0, 0x97, 0x9B, 0xB9, 0x45, 0x8A, 0xEC,
  28552. 0x21, 0x50, 0xAD, 0x27, 0x3F, 0xEB, 0xCB, 0x3E,
  28553. 0xF3, 0xB6, 0x0B, 0x38, 0x56, 0xA7, 0x16, 0xB9,
  28554. 0x78, 0x3D, 0xF8, 0x08, 0xC6, 0x9C, 0x06, 0x12,
  28555. 0xA0, 0x9E, 0xA5, 0x93, 0x16, 0x64, 0x65, 0x87,
  28556. 0x69, 0x2D, 0x3C, 0x8E, 0x8C, 0x73, 0x9F, 0xB3,
  28557. 0x78, 0x27, 0x74, 0x15, 0x61, 0xD5, 0xE7, 0x66,
  28558. 0x67, 0x51, 0x82, 0xFF, 0x83, 0xC9, 0x1C, 0xF0,
  28559. 0x33, 0x65, 0x88, 0x23, 0xD8, 0xFA, 0x91, 0xE1,
  28560. 0x05, 0xFC, 0x14, 0xDA, 0x24, 0xF1, 0xC4, 0x1E,
  28561. 0xB3, 0xCC, 0xE3, 0xC7, 0x47, 0xEE, 0x30, 0x0C,
  28562. 0x31, 0x1F, 0x27, 0x85, 0x47, 0x7A, 0x01, 0x33,
  28563. 0xF7, 0x13, 0xB0, 0x28, 0x04, 0x67, 0x4E, 0x7A,
  28564. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  28565. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  28566. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  28567. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  28568. };
  28569. const byte kyber768_ct[] = {
  28570. 0xB9, 0x54, 0x84, 0x84, 0xA4, 0x87, 0x3B, 0x1F,
  28571. 0x2C, 0xFF, 0x66, 0xA3, 0x23, 0x09, 0x34, 0x60,
  28572. 0x05, 0xEB, 0x7C, 0x04, 0x44, 0x9E, 0xAD, 0xC4,
  28573. 0x66, 0xBC, 0x47, 0x0E, 0x71, 0xED, 0x6C, 0x77,
  28574. 0x1C, 0x2C, 0xF3, 0x92, 0x5E, 0xEB, 0x78, 0x91,
  28575. 0x56, 0xAC, 0xF5, 0x8D, 0x13, 0x21, 0xA5, 0x23,
  28576. 0xE4, 0xDD, 0xB1, 0x58, 0x4C, 0x6E, 0x3F, 0x4A,
  28577. 0x3E, 0xE5, 0xCC, 0x9C, 0x00, 0x7A, 0x00, 0xFF,
  28578. 0xA2, 0xBF, 0xF2, 0xD3, 0x92, 0xD2, 0x9D, 0xCB,
  28579. 0xEF, 0x44, 0x6D, 0x16, 0x51, 0xC9, 0xB3, 0x63,
  28580. 0x8B, 0x0F, 0xB7, 0xEB, 0xEC, 0xE5, 0xB0, 0x59,
  28581. 0xC9, 0x76, 0x21, 0xFF, 0x7A, 0x1E, 0x1C, 0xE7,
  28582. 0x7A, 0xD5, 0x46, 0x1C, 0x51, 0xE1, 0x3A, 0xF7,
  28583. 0x07, 0x89, 0xE4, 0x88, 0x58, 0x98, 0xEA, 0x20,
  28584. 0xFA, 0x17, 0x85, 0xA0, 0x3E, 0x3E, 0xF8, 0xA2,
  28585. 0x98, 0x33, 0xDA, 0x8D, 0x4D, 0x28, 0x32, 0x53,
  28586. 0x74, 0xE4, 0xF4, 0x67, 0x1B, 0xDA, 0x5B, 0x8F,
  28587. 0xAE, 0x9C, 0x9F, 0x68, 0x88, 0xAD, 0x5B, 0xCD,
  28588. 0x0B, 0x91, 0x16, 0x47, 0x35, 0x73, 0xFB, 0x78,
  28589. 0xDD, 0xA7, 0x9A, 0x27, 0x8D, 0x0D, 0x6A, 0xFF,
  28590. 0x71, 0xD0, 0x37, 0x7F, 0xB5, 0x0C, 0x47, 0xEC,
  28591. 0xBD, 0xB4, 0xB1, 0x42, 0x7D, 0xF8, 0xD4, 0x55,
  28592. 0xCC, 0x34, 0x29, 0xCC, 0x84, 0xA1, 0x4A, 0x99,
  28593. 0xFB, 0xE4, 0xAD, 0x1E, 0xB3, 0xE8, 0x07, 0x48,
  28594. 0x3E, 0x62, 0x26, 0xC7, 0x59, 0xAA, 0x0A, 0x6C,
  28595. 0x96, 0x04, 0x41, 0x19, 0x76, 0x8A, 0xB5, 0xA9,
  28596. 0x9B, 0x6F, 0xCD, 0x5B, 0x00, 0x7A, 0x18, 0x82,
  28597. 0xF4, 0xEC, 0xD3, 0x71, 0x39, 0x64, 0x33, 0x32,
  28598. 0x74, 0x4D, 0x08, 0x77, 0xB4, 0x47, 0x01, 0xB0,
  28599. 0xE3, 0xB5, 0xD1, 0x22, 0x8A, 0xB1, 0xE3, 0xA7,
  28600. 0xF9, 0x94, 0xA2, 0xC0, 0xC3, 0xC1, 0x6C, 0x1E,
  28601. 0x6E, 0x02, 0xDF, 0xBF, 0x44, 0x44, 0x4F, 0x6A,
  28602. 0x9D, 0x58, 0x25, 0x49, 0x2A, 0x10, 0x88, 0xD1,
  28603. 0x91, 0xB4, 0x62, 0x22, 0x76, 0x14, 0x30, 0xDD,
  28604. 0x50, 0xEB, 0xF1, 0x1C, 0xAB, 0xE9, 0x95, 0x5C,
  28605. 0x5A, 0x10, 0x79, 0xC4, 0xB7, 0xF2, 0xF4, 0x8F,
  28606. 0xB6, 0xF3, 0x89, 0x3A, 0xBE, 0x38, 0xDD, 0xB0,
  28607. 0x4A, 0xB1, 0x8C, 0xAE, 0x8D, 0x50, 0x70, 0xBD,
  28608. 0x9E, 0x05, 0x71, 0x0A, 0x7B, 0x4C, 0xBE, 0xE2,
  28609. 0xAE, 0x3F, 0xEC, 0xCF, 0xFA, 0x26, 0x65, 0xBE,
  28610. 0xC9, 0xD0, 0xBC, 0x6B, 0x57, 0x34, 0xF2, 0x4C,
  28611. 0x29, 0xB5, 0x9E, 0xF8, 0x21, 0x99, 0x77, 0x58,
  28612. 0x73, 0x2B, 0x04, 0x72, 0xFE, 0x63, 0x81, 0x63,
  28613. 0xC0, 0xF8, 0x67, 0x64, 0x48, 0x12, 0xBB, 0x95,
  28614. 0x83, 0xEE, 0xB7, 0xED, 0x64, 0x73, 0xEE, 0x97,
  28615. 0x23, 0x35, 0xA5, 0x06, 0xCE, 0x64, 0x94, 0x90,
  28616. 0x49, 0x26, 0x3E, 0x32, 0x77, 0x92, 0x88, 0xF8,
  28617. 0xD0, 0x16, 0x5E, 0xE5, 0x22, 0x79, 0xF7, 0x98,
  28618. 0xE0, 0x2F, 0x8B, 0xDE, 0xA2, 0x5F, 0x67, 0x8E,
  28619. 0x2A, 0xB9, 0xE8, 0x8A, 0x77, 0xD2, 0x89, 0x2B,
  28620. 0x79, 0x69, 0x22, 0x1B, 0x9D, 0x81, 0xFD, 0xA7,
  28621. 0x6B, 0x91, 0xD1, 0x8F, 0xE6, 0xBF, 0x14, 0xA1,
  28622. 0xC2, 0x3A, 0xAF, 0x42, 0xCB, 0x95, 0x53, 0x44,
  28623. 0x82, 0xAC, 0x2C, 0x56, 0x9C, 0x01, 0xE0, 0xC2,
  28624. 0x0E, 0x65, 0x8A, 0x1E, 0x17, 0xC1, 0xD8, 0xB3,
  28625. 0x8F, 0xF6, 0x44, 0xD5, 0x3F, 0x41, 0x10, 0x65,
  28626. 0x55, 0x14, 0x09, 0xB7, 0x4A, 0x7A, 0x26, 0x04,
  28627. 0x42, 0xE1, 0x96, 0xE1, 0x2B, 0x48, 0x33, 0x96,
  28628. 0x86, 0x3E, 0x6D, 0x67, 0xDC, 0x4E, 0x54, 0x07,
  28629. 0x4C, 0x73, 0x97, 0x9E, 0x69, 0x83, 0x07, 0xCF,
  28630. 0xA6, 0x37, 0xA3, 0xFD, 0xBB, 0xD0, 0xAA, 0xA0,
  28631. 0xA7, 0x97, 0x0B, 0x38, 0xEE, 0x38, 0xB1, 0x85,
  28632. 0xA6, 0x68, 0x71, 0x29, 0xE6, 0xA2, 0xFF, 0x62,
  28633. 0x4F, 0x65, 0x6C, 0x53, 0x80, 0x5B, 0x72, 0xF4,
  28634. 0xD2, 0x41, 0x50, 0x59, 0x01, 0x22, 0xC8, 0x25,
  28635. 0x96, 0x5C, 0xE1, 0x24, 0x7D, 0x3B, 0x4A, 0x10,
  28636. 0xEE, 0x82, 0x47, 0xDF, 0xB5, 0x60, 0x25, 0x4F,
  28637. 0x79, 0x91, 0x09, 0x82, 0xED, 0x30, 0xE7, 0xF3,
  28638. 0xE6, 0x7A, 0x9F, 0xD1, 0xF0, 0xA7, 0x15, 0x60,
  28639. 0xB9, 0xDF, 0xAB, 0x4A, 0xCC, 0x0A, 0x78, 0xC9,
  28640. 0x9A, 0x7F, 0x15, 0x26, 0x81, 0x2C, 0x16, 0xA7,
  28641. 0xDC, 0x4A, 0xA8, 0xBB, 0x9F, 0xA6, 0xCA, 0xA6,
  28642. 0x80, 0xE3, 0x60, 0x2D, 0x13, 0x3C, 0xCA, 0xE9,
  28643. 0xD1, 0xAD, 0x39, 0xFD, 0xA6, 0xA3, 0xDA, 0x7F,
  28644. 0xF1, 0x0A, 0x25, 0xBD, 0x8F, 0x05, 0xBC, 0x17,
  28645. 0x98, 0xCD, 0x01, 0x2F, 0x63, 0x16, 0xCB, 0xF1,
  28646. 0xC9, 0x9F, 0x75, 0x98, 0x62, 0x43, 0x3B, 0xB7,
  28647. 0x55, 0xCC, 0x5C, 0x40, 0xF3, 0x87, 0x55, 0xEA,
  28648. 0x03, 0xA5, 0x00, 0xB4, 0x7F, 0xDD, 0x2D, 0xBD,
  28649. 0xFE, 0x05, 0x60, 0x69, 0x42, 0xB5, 0x99, 0x1E,
  28650. 0x0E, 0xA8, 0xEC, 0x04, 0x7E, 0x6F, 0x77, 0xD4,
  28651. 0x92, 0x8F, 0xC7, 0xE0, 0xB5, 0x06, 0x05, 0x76,
  28652. 0xFA, 0x45, 0x25, 0xB1, 0xD1, 0xF4, 0x01, 0x3A,
  28653. 0x49, 0xA9, 0x93, 0x77, 0x4B, 0xEE, 0xA2, 0xE8,
  28654. 0xAA, 0x39, 0xBD, 0xD9, 0x90, 0xEB, 0x0F, 0xB5,
  28655. 0xFA, 0xB6, 0xC4, 0x10, 0x79, 0xF5, 0x6A, 0x32,
  28656. 0xFD, 0x9C, 0x38, 0xAC, 0xEC, 0x84, 0xF1, 0x62,
  28657. 0xC6, 0x71, 0xE9, 0xDC, 0x55, 0xD8, 0x40, 0x1D,
  28658. 0x29, 0x8A, 0x97, 0x02, 0x08, 0x44, 0x23, 0x27,
  28659. 0x51, 0x13, 0xC3, 0x46, 0x62, 0x4A, 0x8F, 0x0E,
  28660. 0x79, 0xFC, 0xC3, 0x59, 0x33, 0xC7, 0x75, 0x7F,
  28661. 0x24, 0xE0, 0x7A, 0x25, 0x7A, 0xBF, 0xEA, 0x28,
  28662. 0xDA, 0x2B, 0xDE, 0x70, 0x73, 0xAE, 0xC1, 0x33,
  28663. 0xE6, 0x55, 0xC5, 0xA4, 0xB8, 0x94, 0xC5, 0x95,
  28664. 0x10, 0x2E, 0x7D, 0x13, 0x80, 0x96, 0xCD, 0x6F,
  28665. 0x97, 0xF5, 0x62, 0x27, 0x62, 0x4D, 0xCC, 0xAB,
  28666. 0xFB, 0x9D, 0xCF, 0xE9, 0x0E, 0x26, 0x5A, 0x04,
  28667. 0x9B, 0x90, 0xA3, 0x2E, 0x54, 0xD4, 0x81, 0xE2,
  28668. 0xF3, 0x7B, 0x00, 0x2C, 0xFE, 0x9C, 0xC9, 0xCD,
  28669. 0x24, 0x3F, 0x83, 0xE3, 0x62, 0x09, 0x4C, 0xFB,
  28670. 0xF7, 0x0E, 0x1A, 0x94, 0x79, 0xC5, 0x7E, 0x30,
  28671. 0xB4, 0xB9, 0xEB, 0x38, 0x31, 0x6F, 0xA9, 0xE9,
  28672. 0xBE, 0xE1, 0x90, 0xC6, 0xE8, 0xA8, 0xD4, 0x5C,
  28673. 0xA3, 0x40, 0x76, 0xF4, 0x84, 0x0D, 0xBC, 0x9F,
  28674. 0x42, 0x5C, 0xD5, 0x70, 0x98, 0x8E, 0x58, 0x86,
  28675. 0xC2, 0x7B, 0x64, 0xC2, 0x28, 0x15, 0x8D, 0xA7,
  28676. 0xE2, 0x82, 0xF6, 0xC0, 0x09, 0xC1, 0x61, 0xBA,
  28677. 0xC8, 0xE4, 0xDE, 0x67, 0xCD, 0x46, 0xAB, 0x9D,
  28678. 0x5A, 0xDC, 0xF7, 0xF5, 0xF6, 0xA0, 0x6D, 0x36,
  28679. 0xB5, 0xEC, 0xA3, 0x41, 0x7C, 0x40, 0xDA, 0xAC,
  28680. 0x80, 0x55, 0xFF, 0x3D, 0x77, 0x3D, 0x14, 0x1F,
  28681. 0x50, 0x40, 0x58, 0x74, 0x48, 0xF0, 0xA9, 0x10,
  28682. 0x7C, 0x44, 0xED, 0x67, 0x1A, 0x34, 0xF2, 0x5C,
  28683. 0x4F, 0x86, 0x6F, 0xCA, 0xA6, 0xC1, 0x42, 0x4F,
  28684. 0xE9, 0x59, 0x59, 0x9E, 0xFE, 0x31, 0x63, 0x56,
  28685. 0xF9, 0x05, 0xDD, 0xD6, 0x8B, 0x27, 0xA4, 0xA0,
  28686. 0xBB, 0x17, 0x28, 0x52, 0x0D, 0x5D, 0x36, 0x77,
  28687. 0x7E, 0x22, 0xF1, 0xFE, 0xDE, 0x19, 0x3B, 0x07,
  28688. 0xB3, 0xA2, 0xD2, 0x09, 0x9C, 0x41, 0x93, 0x1E,
  28689. 0xB4, 0xE0, 0xB0, 0xFA, 0x3F, 0xA7, 0xC6, 0x66,
  28690. 0xD8, 0xA7, 0xFD, 0x90, 0xCD, 0x49, 0xA2, 0xEE,
  28691. 0x25, 0x66, 0xCE, 0x31, 0x9E, 0x03, 0x35, 0x23,
  28692. 0x32, 0x2E, 0xDA, 0x7C, 0xD3, 0x14, 0x98, 0x88,
  28693. 0xBA, 0x9B, 0xEF, 0x0C, 0x5F, 0x82, 0x6F, 0x3C,
  28694. 0x5A, 0xB8, 0x6D, 0x2D, 0xD0, 0x84, 0xA3, 0xC3,
  28695. 0x16, 0xDE, 0x98, 0xCB, 0x42, 0x68, 0x4E, 0x0D,
  28696. 0x64, 0x41, 0xE0, 0x73, 0xA5, 0x9F, 0x27, 0x67,
  28697. 0x55, 0x1E, 0x56, 0x48, 0xBF, 0xBE, 0x49, 0x8E,
  28698. 0xF3, 0xC0, 0x3B, 0x65, 0xD2, 0x9F, 0x5F, 0xA8,
  28699. 0x4D, 0x99, 0x79, 0x9F, 0x1D, 0x32, 0xC2, 0x26,
  28700. 0xFB, 0xF7, 0x39, 0x65, 0x7F, 0x02, 0x61, 0xE8,
  28701. 0x96, 0x02, 0xF7, 0xBD, 0x07, 0x73, 0x41, 0x6D,
  28702. 0xFE, 0xEA, 0x80, 0x1C, 0xE1, 0xA7, 0x55, 0x3F,
  28703. 0x1E, 0xF9, 0x34, 0x1A, 0x5F, 0x74, 0x09, 0x25,
  28704. 0x80, 0x75, 0xBE, 0xF5, 0x82, 0x83, 0x35, 0x66,
  28705. 0x1A, 0x8C, 0xB1, 0x50, 0xD5, 0x37, 0x0B, 0xEC
  28706. };
  28707. const byte kyber768_ss[] = {
  28708. 0x44, 0xF6, 0x94, 0xE4, 0x78, 0xEB, 0xAC, 0x4A,
  28709. 0x55, 0x6A, 0x38, 0xA2, 0x5C, 0x95, 0x9B, 0x62,
  28710. 0xAC, 0xC7, 0x2E, 0x17, 0xCF, 0x04, 0xB4, 0xD4,
  28711. 0x7E, 0x54, 0xB0, 0xB7, 0xFE, 0xAC, 0xEB, 0x56
  28712. };
  28713. #endif
  28714. ret = wc_KyberKey_Init(KYBER768, &key, HEAP_HINT, INVALID_DEVID);
  28715. if (ret != 0)
  28716. return -20201;
  28717. ret = wc_KyberKey_MakeKeyWithRandom(&key, kyber768_rand,
  28718. sizeof(kyber768_rand));
  28719. if (ret != 0)
  28720. return -20202;
  28721. ret = wc_KyberKey_EncodePublicKey(&key, pub, sizeof(pub));
  28722. if (ret != 0)
  28723. return -20203;
  28724. ret = wc_KyberKey_EncodePrivateKey(&key, priv, sizeof(priv));
  28725. if (ret != 0)
  28726. return -20204;
  28727. if (XMEMCMP(pub, kyber768_pk, sizeof(kyber768_pk)) != 0)
  28728. return -20205;
  28729. if (XMEMCMP(priv, kyber768_sk, sizeof(kyber768_sk)) != 0)
  28730. return -20206;
  28731. ret = wc_KyberKey_EncapsulateWithRandom(&key, ct, ss, kyber768enc_rand,
  28732. sizeof(kyber768enc_rand));
  28733. if (ret != 0)
  28734. return -20207;
  28735. if (XMEMCMP(ct, kyber768_ct, sizeof(kyber768_ct)) != 0)
  28736. return -20208;
  28737. if (XMEMCMP(ss, kyber768_ss, sizeof(kyber768_ss)) != 0)
  28738. return -20209;
  28739. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, sizeof(kyber768_ct));
  28740. if (ret != 0)
  28741. return -20210;
  28742. if (XMEMCMP(ss_dec, kyber768_ss, sizeof(kyber768_ss)) != 0)
  28743. return -20211;
  28744. wc_KyberKey_Free(&key);
  28745. return 0;
  28746. }
  28747. #endif /* WOLFSSL_KYBER768 */
  28748. #ifdef WOLFSSL_KYBER1024
  28749. static int kyber1024_kat(void)
  28750. {
  28751. KyberKey key;
  28752. int ret;
  28753. byte priv[KYBER1024_PRIVATE_KEY_SIZE];
  28754. byte pub[KYBER1024_PUBLIC_KEY_SIZE];
  28755. byte ct[KYBER1024_CIPHER_TEXT_SIZE];
  28756. byte ss[KYBER_SS_SZ];
  28757. byte ss_dec[KYBER_SS_SZ];
  28758. const byte kyber1024_rand[] = {
  28759. 0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa,
  28760. 0x0c, 0x6d, 0x10, 0xe4, 0xdb, 0x6b, 0x1a, 0xdd,
  28761. 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
  28762. 0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d,
  28763. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  28764. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  28765. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  28766. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  28767. };
  28768. const byte kyber1024enc_rand[] = {
  28769. 0x14, 0x7c, 0x03, 0xf7, 0xa5, 0xbe, 0xbb, 0xa4,
  28770. 0x06, 0xc8, 0xfa, 0xe1, 0x87, 0x4d, 0x7f, 0x13,
  28771. 0xc8, 0x0e, 0xfe, 0x79, 0xa3, 0xa9, 0xa8, 0x74,
  28772. 0xcc, 0x09, 0xfe, 0x76, 0xf6, 0x99, 0x76, 0x15
  28773. };
  28774. #ifndef WOLFSSL_KYBER_90S
  28775. const byte kyber1024_pk[] = {
  28776. 0xD2, 0x23, 0x02, 0xCB, 0xD3, 0x39, 0x9F, 0xAC,
  28777. 0xC6, 0x30, 0x99, 0x1F, 0xC8, 0xF2, 0x8B, 0xDB,
  28778. 0x43, 0x54, 0x76, 0x25, 0x41, 0x52, 0x76, 0x78,
  28779. 0xBC, 0xF6, 0x1F, 0x65, 0xC2, 0x41, 0x14, 0x6C,
  28780. 0x42, 0x6D, 0x23, 0xB9, 0xBF, 0xAA, 0x6B, 0x7D,
  28781. 0xF1, 0x8C, 0x97, 0xF2, 0x0C, 0x1B, 0x61, 0x25,
  28782. 0xBF, 0x87, 0x4B, 0x1D, 0x89, 0x47, 0x58, 0x52,
  28783. 0xC4, 0x48, 0x21, 0x5D, 0xB0, 0xEB, 0x77, 0x37,
  28784. 0xF9, 0x14, 0x80, 0xE8, 0xCE, 0xBD, 0x9A, 0x08,
  28785. 0x71, 0x57, 0x4F, 0x5A, 0xB6, 0x2D, 0x90, 0x20,
  28786. 0x17, 0x5E, 0xC6, 0x92, 0x7C, 0xA0, 0xB5, 0x4C,
  28787. 0x09, 0x81, 0x8E, 0x42, 0xCF, 0x92, 0xA3, 0x83,
  28788. 0x17, 0x24, 0x22, 0xC7, 0xDC, 0x18, 0x31, 0xD6,
  28789. 0x3B, 0x0C, 0x29, 0x5D, 0xE7, 0x51, 0x59, 0xDB,
  28790. 0x80, 0x34, 0xE9, 0xE0, 0x7F, 0x7B, 0x0B, 0x91,
  28791. 0x0C, 0x3C, 0x1E, 0x5F, 0xB6, 0x6B, 0x3D, 0xC5,
  28792. 0x23, 0xF1, 0xFA, 0x6E, 0xB4, 0x91, 0x0C, 0xB8,
  28793. 0x9A, 0x6C, 0x17, 0x56, 0x2C, 0x83, 0xAB, 0x4C,
  28794. 0x18, 0xD0, 0xCD, 0x7E, 0x07, 0x96, 0x59, 0x2A,
  28795. 0x37, 0x2A, 0xA4, 0x09, 0xB1, 0xC5, 0x57, 0x34,
  28796. 0x7C, 0xCA, 0xCD, 0xC4, 0x64, 0x4A, 0x11, 0x90,
  28797. 0x64, 0xD0, 0x6D, 0xD4, 0x74, 0x92, 0x9D, 0x1C,
  28798. 0x6F, 0xB4, 0xD6, 0x86, 0xE5, 0x49, 0x1C, 0xE4,
  28799. 0xBC, 0x89, 0xA3, 0x0B, 0xB4, 0xB8, 0xC4, 0x1B,
  28800. 0xCE, 0x51, 0x57, 0xDF, 0xC1, 0x36, 0x08, 0x23,
  28801. 0xB1, 0xAB, 0x61, 0x8C, 0x14, 0xB1, 0x0F, 0x98,
  28802. 0xC2, 0x50, 0x67, 0x39, 0x8E, 0xA7, 0x01, 0x8C,
  28803. 0x27, 0x8A, 0x4B, 0x3D, 0xF3, 0x13, 0x34, 0xD6,
  28804. 0x03, 0xB2, 0x04, 0x4E, 0xF1, 0x87, 0xCD, 0x9B,
  28805. 0xC6, 0xCE, 0x42, 0x72, 0x5B, 0xD9, 0x62, 0xC2,
  28806. 0x64, 0x98, 0x3E, 0x9E, 0x18, 0x15, 0x5A, 0x8B,
  28807. 0x9C, 0x47, 0x14, 0x3D, 0x70, 0x46, 0x0A, 0x26,
  28808. 0xA5, 0x6F, 0xE7, 0x65, 0x8C, 0x1F, 0x15, 0x03,
  28809. 0x48, 0xC6, 0x08, 0x7E, 0xF7, 0x58, 0xAD, 0x16,
  28810. 0x78, 0x87, 0x86, 0x0A, 0x00, 0x7A, 0x5F, 0xC3,
  28811. 0x73, 0x58, 0xD4, 0x3B, 0x5E, 0xBE, 0xE8, 0x20,
  28812. 0xAC, 0xEA, 0x47, 0x4F, 0x0A, 0xC0, 0x7B, 0x76,
  28813. 0x80, 0x28, 0x66, 0x19, 0x9C, 0x61, 0x23, 0x1D,
  28814. 0x5C, 0x74, 0x7C, 0x93, 0x77, 0x4D, 0x2C, 0x1E,
  28815. 0x0C, 0x1C, 0x67, 0xE6, 0xC8, 0x1B, 0x82, 0x75,
  28816. 0x21, 0x73, 0xE1, 0x25, 0xBA, 0xF3, 0x9B, 0x4F,
  28817. 0xD1, 0x9A, 0x4F, 0x45, 0x3D, 0xC5, 0x79, 0x76,
  28818. 0xB1, 0xD9, 0x7F, 0xE6, 0x99, 0x69, 0x92, 0xBB,
  28819. 0xB6, 0x5B, 0x7C, 0xB2, 0x5D, 0x07, 0x7B, 0xBA,
  28820. 0xA6, 0xA1, 0x33, 0x22, 0x89, 0x9A, 0xF6, 0x59,
  28821. 0xCF, 0x1B, 0x35, 0x58, 0xC1, 0xB5, 0x00, 0x11,
  28822. 0x54, 0xB6, 0x25, 0x80, 0x9E, 0xD8, 0x9A, 0xEE,
  28823. 0xBB, 0x89, 0xE6, 0xEA, 0x7D, 0x67, 0xF7, 0x23,
  28824. 0xD0, 0x45, 0xAB, 0x05, 0x71, 0x5C, 0x42, 0x35,
  28825. 0x5D, 0xA6, 0xA5, 0xC8, 0xDD, 0x39, 0xC8, 0xAB,
  28826. 0xE3, 0x03, 0x77, 0x51, 0xA0, 0x1E, 0xD1, 0xC7,
  28827. 0x37, 0x49, 0x19, 0xF3, 0x12, 0x1B, 0x5A, 0x52,
  28828. 0xC5, 0x3D, 0x14, 0x87, 0x31, 0x67, 0x69, 0xF8,
  28829. 0x07, 0x21, 0xDE, 0xEA, 0xAA, 0xD3, 0xC9, 0x0F,
  28830. 0x76, 0xE7, 0xAE, 0x9E, 0x12, 0xBA, 0x92, 0xB3,
  28831. 0x2B, 0x5F, 0xD4, 0x57, 0xE3, 0xC7, 0x52, 0xC2,
  28832. 0x65, 0x0D, 0xFB, 0x88, 0x57, 0x71, 0xCB, 0x77,
  28833. 0xAC, 0x3C, 0x78, 0x5A, 0x8C, 0x56, 0x2E, 0x6A,
  28834. 0x1C, 0x63, 0xC2, 0xA5, 0x5E, 0xA4, 0x7C, 0xF8,
  28835. 0xB9, 0x0E, 0xB8, 0x22, 0x5C, 0x12, 0x3C, 0x34,
  28836. 0x64, 0x52, 0x56, 0x62, 0x35, 0xB2, 0xF3, 0x18,
  28837. 0x23, 0xA3, 0x35, 0x21, 0xE0, 0x87, 0x93, 0x7A,
  28838. 0x34, 0x5D, 0x8D, 0x66, 0x3E, 0xEA, 0xA0, 0x56,
  28839. 0x58, 0x91, 0x7B, 0xBA, 0xA0, 0x08, 0xC2, 0xE3,
  28840. 0x35, 0xF8, 0x85, 0x0A, 0x90, 0xA3, 0x26, 0xD0,
  28841. 0xE6, 0x64, 0x32, 0xF4, 0x4C, 0xEB, 0x82, 0x89,
  28842. 0xE4, 0xEC, 0xB2, 0xD1, 0x29, 0x58, 0xE9, 0x84,
  28843. 0x07, 0x2E, 0xCA, 0xCB, 0x88, 0xE1, 0x34, 0x8F,
  28844. 0xF0, 0xB5, 0x56, 0x54, 0xAC, 0xBA, 0x5B, 0x54,
  28845. 0x97, 0x1C, 0xBA, 0xEB, 0xA8, 0x8E, 0xC4, 0xB9,
  28846. 0x1A, 0x94, 0xC3, 0x71, 0x92, 0xFA, 0x98, 0x2B,
  28847. 0xEC, 0xB9, 0xF3, 0xDA, 0x42, 0x16, 0x03, 0xB6,
  28848. 0x1A, 0x51, 0xBC, 0x8E, 0x36, 0xCB, 0xD0, 0x53,
  28849. 0x85, 0x1C, 0x77, 0xB1, 0xB9, 0x26, 0xB1, 0x7A,
  28850. 0x27, 0x2A, 0xA9, 0x02, 0x32, 0x46, 0xB0, 0x2B,
  28851. 0x3E, 0xD4, 0x7F, 0x66, 0xA0, 0x0B, 0xD5, 0x68,
  28852. 0x48, 0x23, 0x63, 0x4E, 0x7C, 0xE5, 0x8C, 0xF8,
  28853. 0xF3, 0x06, 0xE3, 0x5B, 0x1E, 0x53, 0x22, 0x82,
  28854. 0x4D, 0x90, 0x48, 0x01, 0xF0, 0xA2, 0xFA, 0x7C,
  28855. 0x2B, 0xC9, 0xC2, 0x52, 0xB0, 0xA5, 0x6B, 0x7B,
  28856. 0xA2, 0xAB, 0x0F, 0x63, 0x60, 0x21, 0x74, 0x5A,
  28857. 0x70, 0xA9, 0xA4, 0x3E, 0x2B, 0x0A, 0x8D, 0x61,
  28858. 0x59, 0x70, 0xB6, 0x53, 0x09, 0x62, 0x4B, 0x51,
  28859. 0x84, 0xBC, 0xC3, 0x0B, 0x91, 0x16, 0x79, 0xAE,
  28860. 0xDD, 0x76, 0x02, 0x5F, 0xE3, 0x90, 0x8F, 0xD6,
  28861. 0x78, 0x97, 0xB0, 0xCF, 0x4B, 0xE5, 0xA6, 0xF5,
  28862. 0x41, 0x3D, 0x7D, 0xD9, 0x85, 0x64, 0xB2, 0x3E,
  28863. 0x42, 0xA9, 0x3E, 0x4A, 0xA8, 0x82, 0x1C, 0xD4,
  28864. 0x50, 0x54, 0xC6, 0x43, 0xED, 0xC1, 0x15, 0x8D,
  28865. 0xB6, 0xB3, 0xDE, 0xB1, 0x3F, 0xB5, 0xA5, 0x1E,
  28866. 0xBD, 0x1A, 0x8A, 0x78, 0xB8, 0x72, 0x25, 0xA7,
  28867. 0x33, 0x8E, 0x10, 0x11, 0x04, 0xC4, 0xA2, 0x20,
  28868. 0xD9, 0xBD, 0xED, 0xD4, 0x8C, 0x85, 0xA1, 0xC2,
  28869. 0xDA, 0xE7, 0x81, 0xA8, 0x0C, 0x40, 0xE1, 0x3B,
  28870. 0x87, 0xEA, 0xC7, 0x3A, 0x76, 0x42, 0x01, 0xC9,
  28871. 0xB7, 0x60, 0xCC, 0xFB, 0x1A, 0xE3, 0x92, 0x69,
  28872. 0x9C, 0x70, 0x39, 0xD2, 0x7C, 0x39, 0x36, 0x2B,
  28873. 0x27, 0xB8, 0xFC, 0x6F, 0x07, 0xA8, 0xA3, 0xD4,
  28874. 0x41, 0x0F, 0x15, 0x47, 0xC4, 0x8A, 0x99, 0x97,
  28875. 0xF6, 0x2C, 0x61, 0x07, 0x44, 0x52, 0xEF, 0x15,
  28876. 0x15, 0xF8, 0xA6, 0x49, 0xEB, 0xCA, 0x94, 0x37,
  28877. 0x20, 0x5A, 0x4E, 0x8A, 0x61, 0x60, 0x6B, 0x41,
  28878. 0xDA, 0xF6, 0x83, 0x4D, 0x67, 0x1F, 0x4D, 0x85,
  28879. 0x2C, 0x0C, 0x9C, 0x40, 0x96, 0x61, 0x16, 0x48,
  28880. 0xC6, 0xA3, 0x17, 0x06, 0x78, 0xB1, 0x53, 0x7C,
  28881. 0xC1, 0x82, 0x8D, 0x93, 0x58, 0x0C, 0x9E, 0x58,
  28882. 0x49, 0xA9, 0x65, 0x31, 0x75, 0xAC, 0xB7, 0x53,
  28883. 0xF2, 0xBE, 0x74, 0x37, 0xBE, 0x45, 0xF6, 0xC6,
  28884. 0x03, 0xE4, 0x85, 0xF2, 0xEC, 0x30, 0x1B, 0xB4,
  28885. 0x2B, 0x6C, 0x37, 0xC2, 0x25, 0xD7, 0x49, 0x5A,
  28886. 0x58, 0x4A, 0xE2, 0x31, 0x89, 0x0A, 0xB5, 0xC8,
  28887. 0xC3, 0x5C, 0x26, 0x8C, 0xF4, 0xBB, 0xB0, 0x21,
  28888. 0x3C, 0x09, 0x60, 0x19, 0x31, 0x95, 0x61, 0xA8,
  28889. 0xA6, 0x94, 0x76, 0x37, 0xAA, 0x40, 0xD0, 0x06,
  28890. 0xB4, 0x15, 0xBB, 0x2C, 0xFA, 0x22, 0x37, 0xE0,
  28891. 0x89, 0x0B, 0x6A, 0x3B, 0xC1, 0x34, 0xAB, 0xF8,
  28892. 0xF6, 0x58, 0x5E, 0x10, 0x8D, 0x15, 0x94, 0x0F,
  28893. 0x91, 0xF4, 0xBF, 0x5B, 0x0C, 0x81, 0x80, 0x55,
  28894. 0xB2, 0x1D, 0xEA, 0x6E, 0x63, 0xB5, 0x53, 0x98,
  28895. 0x8C, 0x47, 0xF4, 0xB9, 0x4E, 0x7C, 0xF8, 0x00,
  28896. 0xA4, 0x93, 0xB4, 0x73, 0x47, 0x05, 0xED, 0xC5,
  28897. 0x6A, 0x4B, 0x60, 0x21, 0xC6, 0x29, 0x50, 0x06,
  28898. 0x75, 0x87, 0x68, 0x04, 0xCF, 0x0B, 0x95, 0x1F,
  28899. 0x03, 0x8A, 0x5C, 0x7F, 0xE5, 0x8E, 0x89, 0x77,
  28900. 0x4E, 0xF2, 0x99, 0x2F, 0xD7, 0xC6, 0x30, 0x99,
  28901. 0xD3, 0x52, 0xA7, 0xD2, 0x15, 0x60, 0xB7, 0x88,
  28902. 0xB4, 0x05, 0x70, 0x98, 0x61, 0x81, 0x7E, 0x59,
  28903. 0xA9, 0x6B, 0x3A, 0x3A, 0x83, 0xCB, 0xA8, 0x03,
  28904. 0xB1, 0x69, 0x34, 0x33, 0x10, 0x71, 0x90, 0x5B,
  28905. 0xBE, 0xC6, 0x53, 0x29, 0x00, 0x15, 0x5D, 0x8A,
  28906. 0xC8, 0x8C, 0xB3, 0x2E, 0x4E, 0x21, 0xA3, 0xBD,
  28907. 0x3A, 0x03, 0xFD, 0xEC, 0x32, 0x5A, 0x51, 0xCD,
  28908. 0x27, 0x73, 0x96, 0x4E, 0x67, 0x84, 0xFC, 0xF1,
  28909. 0x85, 0x37, 0x37, 0xAA, 0x64, 0xEB, 0x67, 0x56,
  28910. 0x47, 0x27, 0x27, 0x26, 0x61, 0xAB, 0xF8, 0x43,
  28911. 0x13, 0xA5, 0x7A, 0x44, 0xB1, 0x23, 0xC6, 0x55,
  28912. 0x09, 0xCF, 0xB7, 0xA6, 0xF6, 0x64, 0x1C, 0xDC,
  28913. 0xC3, 0xB5, 0x7F, 0xE6, 0x28, 0xC7, 0xB8, 0x19,
  28914. 0x2D, 0xB4, 0x4F, 0xFB, 0xF5, 0x79, 0x6A, 0x86,
  28915. 0x13, 0xB1, 0xFA, 0x12, 0x6F, 0x60, 0x76, 0x88,
  28916. 0x3C, 0x78, 0x3D, 0xC2, 0x4E, 0x2A, 0x44, 0x64,
  28917. 0xC4, 0x0B, 0x3A, 0x41, 0xCA, 0x70, 0xAE, 0x87,
  28918. 0x62, 0x08, 0x66, 0xCF, 0x4F, 0xCB, 0x2B, 0xD2,
  28919. 0x04, 0xBF, 0x5C, 0x28, 0x38, 0x12, 0xBA, 0x05,
  28920. 0x6A, 0xC0, 0xC3, 0x45, 0xE3, 0x79, 0xC4, 0xBA,
  28921. 0x24, 0xD7, 0x50, 0x90, 0x12, 0x79, 0xBB, 0x2F,
  28922. 0x3A, 0x16, 0xF6, 0x12, 0xBF, 0xAD, 0xB3, 0x57,
  28923. 0x03, 0x33, 0x2C, 0x7C, 0x13, 0x6F, 0x68, 0xEA,
  28924. 0xB6, 0x75, 0x5C, 0x66, 0xB6, 0xA4, 0xAD, 0x1A,
  28925. 0xAB, 0xA7, 0xB7, 0x68, 0xA5, 0x8A, 0xCA, 0xAC,
  28926. 0xC1, 0x0A, 0x45, 0x9A, 0x1C, 0xC8, 0xEF, 0x29,
  28927. 0x37, 0x7B, 0xC2, 0x00, 0xE4, 0xD3, 0x15, 0xA3,
  28928. 0x0A, 0x6B, 0xCC, 0x32, 0x56, 0xF9, 0x73, 0x4D,
  28929. 0x06, 0xE9, 0x77, 0x9C, 0xAA, 0x54, 0x42, 0xA9,
  28930. 0xA1, 0x60, 0x69, 0x08, 0x13, 0x77, 0xC7, 0x6E,
  28931. 0x75, 0x15, 0x43, 0x68, 0x07, 0x2D, 0xC4, 0x46,
  28932. 0xED, 0x6C, 0x8B, 0x8E, 0x62, 0x2A, 0x21, 0xE3,
  28933. 0x83, 0xCF, 0x9B, 0xA1, 0xFB, 0x43, 0x4E, 0x2E,
  28934. 0xCC, 0x81, 0xE7, 0xB7, 0x8C, 0xEE, 0x98, 0x6B,
  28935. 0x8F, 0xF7, 0x98, 0xAB, 0x18, 0xCF, 0x96, 0x34,
  28936. 0x54, 0x35, 0x46, 0x28, 0x4E, 0xDA, 0x2A, 0x26,
  28937. 0xB4, 0x7F, 0x05, 0xB7, 0x35, 0xBC, 0xDB, 0x12,
  28938. 0x02, 0x22, 0x00, 0x76, 0xDC, 0x8B, 0x4E, 0x4B,
  28939. 0x9F, 0x85, 0x35, 0x33, 0xC8, 0xF6, 0xC7, 0xFF,
  28940. 0x38, 0x81, 0x7B, 0xA4, 0x97, 0x12, 0x83, 0x57,
  28941. 0x85, 0xF1, 0x7F, 0x14, 0xCA, 0x01, 0xD0, 0xC1,
  28942. 0xC1, 0xE9, 0x88, 0x10, 0xFE, 0x0B, 0x36, 0xE5,
  28943. 0xB4, 0x27, 0x15, 0x7B, 0x94, 0x18, 0x44, 0x9C,
  28944. 0xED, 0xD6, 0x41, 0xA4, 0x29, 0x3C, 0x85, 0xC3,
  28945. 0x27, 0x00, 0x10, 0x2A, 0xCE, 0xC2, 0x2E, 0xBA,
  28946. 0xD9, 0x8E, 0xD1, 0x60, 0xA5, 0xF0, 0x27, 0xBD,
  28947. 0x4C, 0xDA, 0x57, 0xF1, 0xF3, 0x72, 0x0A, 0x12,
  28948. 0xC1, 0x34, 0x65, 0x4D, 0xD5, 0xE7, 0x3F, 0x82,
  28949. 0x96, 0x76, 0x49, 0x53, 0x90, 0xD0, 0xE7, 0x92,
  28950. 0x9D, 0x60, 0x34, 0xE9, 0xC5, 0x5F, 0x7D, 0x55,
  28951. 0xBA, 0x65, 0x8B, 0xC5, 0x87, 0x98, 0x8E, 0x8A,
  28952. 0xF9, 0x49, 0x60, 0xF6, 0xCF, 0xB8, 0xD5, 0xAF,
  28953. 0x7A, 0x00, 0x21, 0x53, 0x5A, 0x6E, 0x25, 0xE4,
  28954. 0x37, 0xD4, 0x9A, 0x78, 0x06, 0x98, 0xBE, 0x22,
  28955. 0xAC, 0x99, 0x53, 0x94, 0x9F, 0x57, 0x1B, 0x85,
  28956. 0xA6, 0x85, 0x72, 0x5F, 0x82, 0x07, 0xA2, 0xB0,
  28957. 0xAE, 0x84, 0x9B, 0x60, 0x1A, 0xB9, 0x1B, 0x15,
  28958. 0x9B, 0x3D, 0xF4, 0xA1, 0x54, 0xC2, 0x04, 0x1E,
  28959. 0x77, 0x60, 0x70, 0xAF, 0xC4, 0x29, 0x69, 0x32,
  28960. 0x23, 0x80, 0x91, 0x7C, 0x97, 0x51, 0x07, 0x99,
  28961. 0xF3, 0x14, 0x91, 0x31, 0x47, 0x7E, 0x16, 0x66,
  28962. 0x3D, 0x31, 0x74, 0xC7, 0xC1, 0xCA, 0xEA, 0x78,
  28963. 0x85, 0x35, 0xC6, 0xC0, 0x05, 0xA6, 0x4F, 0x28,
  28964. 0x68, 0x63, 0x1B, 0x31, 0xB6, 0x6E, 0x20, 0x5F,
  28965. 0xD3, 0x8C, 0x1D, 0x84, 0x54, 0x2D, 0x0F, 0x1B,
  28966. 0x57, 0x8F, 0x58, 0xC9, 0xBF, 0x5A, 0x0F, 0xAE,
  28967. 0xAB, 0x6A, 0xB6, 0x49, 0x48, 0x93, 0x05, 0x31,
  28968. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  28969. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  28970. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  28971. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22
  28972. };
  28973. const byte kyber1024_sk[] = {
  28974. 0x07, 0x63, 0x8F, 0xB6, 0x98, 0x68, 0xF3, 0xD3,
  28975. 0x20, 0xE5, 0x86, 0x2B, 0xD9, 0x69, 0x33, 0xFE,
  28976. 0xB3, 0x11, 0xB3, 0x62, 0x09, 0x3C, 0x9B, 0x5D,
  28977. 0x50, 0x17, 0x0B, 0xCE, 0xD4, 0x3F, 0x1B, 0x53,
  28978. 0x6D, 0x9A, 0x20, 0x4B, 0xB1, 0xF2, 0x26, 0x95,
  28979. 0x95, 0x0B, 0xA1, 0xF2, 0xA9, 0xE8, 0xEB, 0x82,
  28980. 0x8B, 0x28, 0x44, 0x88, 0x76, 0x0B, 0x3F, 0xC8,
  28981. 0x4F, 0xAB, 0xA0, 0x42, 0x75, 0xD5, 0x62, 0x8E,
  28982. 0x39, 0xC5, 0xB2, 0x47, 0x13, 0x74, 0x28, 0x3C,
  28983. 0x50, 0x32, 0x99, 0xC0, 0xAB, 0x49, 0xB6, 0x6B,
  28984. 0x8B, 0xBB, 0x56, 0xA4, 0x18, 0x66, 0x24, 0xF9,
  28985. 0x19, 0xA2, 0xBA, 0x59, 0xBB, 0x08, 0xD8, 0x55,
  28986. 0x18, 0x80, 0xC2, 0xBE, 0xFC, 0x4F, 0x87, 0xF2,
  28987. 0x5F, 0x59, 0xAB, 0x58, 0x7A, 0x79, 0xC3, 0x27,
  28988. 0xD7, 0x92, 0xD5, 0x4C, 0x97, 0x4A, 0x69, 0x26,
  28989. 0x2F, 0xF8, 0xA7, 0x89, 0x38, 0x28, 0x9E, 0x9A,
  28990. 0x87, 0xB6, 0x88, 0xB0, 0x83, 0xE0, 0x59, 0x5F,
  28991. 0xE2, 0x18, 0xB6, 0xBB, 0x15, 0x05, 0x94, 0x1C,
  28992. 0xE2, 0xE8, 0x1A, 0x5A, 0x64, 0xC5, 0xAA, 0xC6,
  28993. 0x04, 0x17, 0x25, 0x69, 0x85, 0x34, 0x9E, 0xE4,
  28994. 0x7A, 0x52, 0x42, 0x0A, 0x5F, 0x97, 0x47, 0x7B,
  28995. 0x72, 0x36, 0xAC, 0x76, 0xBC, 0x70, 0xE8, 0x28,
  28996. 0x87, 0x29, 0x28, 0x7E, 0xE3, 0xE3, 0x4A, 0x3D,
  28997. 0xBC, 0x36, 0x83, 0xC0, 0xB7, 0xB1, 0x00, 0x29,
  28998. 0xFC, 0x20, 0x34, 0x18, 0x53, 0x7E, 0x74, 0x66,
  28999. 0xBA, 0x63, 0x85, 0xA8, 0xFF, 0x30, 0x1E, 0xE1,
  29000. 0x27, 0x08, 0xF8, 0x2A, 0xAA, 0x1E, 0x38, 0x0F,
  29001. 0xC7, 0xA8, 0x8F, 0x8F, 0x20, 0x5A, 0xB7, 0xE8,
  29002. 0x8D, 0x7E, 0x95, 0x95, 0x2A, 0x55, 0xBA, 0x20,
  29003. 0xD0, 0x9B, 0x79, 0xA4, 0x71, 0x41, 0xD6, 0x2B,
  29004. 0xF6, 0xEB, 0x7D, 0xD3, 0x07, 0xB0, 0x8E, 0xCA,
  29005. 0x13, 0xA5, 0xBC, 0x5F, 0x6B, 0x68, 0x58, 0x1C,
  29006. 0x68, 0x65, 0xB2, 0x7B, 0xBC, 0xDD, 0xAB, 0x14,
  29007. 0x2F, 0x4B, 0x2C, 0xBF, 0xF4, 0x88, 0xC8, 0xA2,
  29008. 0x27, 0x05, 0xFA, 0xA9, 0x8A, 0x2B, 0x9E, 0xEA,
  29009. 0x35, 0x30, 0xC7, 0x66, 0x62, 0x33, 0x5C, 0xC7,
  29010. 0xEA, 0x3A, 0x00, 0x77, 0x77, 0x25, 0xEB, 0xCC,
  29011. 0xCD, 0x2A, 0x46, 0x36, 0xB2, 0xD9, 0x12, 0x2F,
  29012. 0xF3, 0xAB, 0x77, 0x12, 0x3C, 0xE0, 0x88, 0x3C,
  29013. 0x19, 0x11, 0x11, 0x5E, 0x50, 0xC9, 0xE8, 0xA9,
  29014. 0x41, 0x94, 0xE4, 0x8D, 0xD0, 0xD0, 0x9C, 0xFF,
  29015. 0xB3, 0xAD, 0xCD, 0x2C, 0x1E, 0x92, 0x43, 0x09,
  29016. 0x03, 0xD0, 0x7A, 0xDB, 0xF0, 0x05, 0x32, 0x03,
  29017. 0x15, 0x75, 0xAA, 0x7F, 0x9E, 0x7B, 0x5A, 0x1F,
  29018. 0x33, 0x62, 0xDE, 0xC9, 0x36, 0xD4, 0x04, 0x3C,
  29019. 0x05, 0xF2, 0x47, 0x6C, 0x07, 0x57, 0x8B, 0xC9,
  29020. 0xCB, 0xAF, 0x2A, 0xB4, 0xE3, 0x82, 0x72, 0x7A,
  29021. 0xD4, 0x16, 0x86, 0xA9, 0x6B, 0x25, 0x48, 0x82,
  29022. 0x0B, 0xB0, 0x3B, 0x32, 0xF1, 0x1B, 0x28, 0x11,
  29023. 0xAD, 0x62, 0xF4, 0x89, 0xE9, 0x51, 0x63, 0x2A,
  29024. 0xBA, 0x0D, 0x1D, 0xF8, 0x96, 0x80, 0xCC, 0x8A,
  29025. 0x8B, 0x53, 0xB4, 0x81, 0xD9, 0x2A, 0x68, 0xD7,
  29026. 0x0B, 0x4E, 0xA1, 0xC3, 0xA6, 0xA5, 0x61, 0xC0,
  29027. 0x69, 0x28, 0x82, 0xB5, 0xCA, 0x8C, 0xC9, 0x42,
  29028. 0xA8, 0xD4, 0x95, 0xAF, 0xCB, 0x06, 0xDE, 0x89,
  29029. 0x49, 0x8F, 0xB9, 0x35, 0xB7, 0x75, 0x90, 0x8F,
  29030. 0xE7, 0xA0, 0x3E, 0x32, 0x4D, 0x54, 0xCC, 0x19,
  29031. 0xD4, 0xE1, 0xAA, 0xBD, 0x35, 0x93, 0xB3, 0x8B,
  29032. 0x19, 0xEE, 0x13, 0x88, 0xFE, 0x49, 0x2B, 0x43,
  29033. 0x12, 0x7E, 0x5A, 0x50, 0x42, 0x53, 0x78, 0x6A,
  29034. 0x0D, 0x69, 0xAD, 0x32, 0x60, 0x1C, 0x28, 0xE2,
  29035. 0xC8, 0x85, 0x04, 0xA5, 0xBA, 0x59, 0x97, 0x06,
  29036. 0x02, 0x3A, 0x61, 0x36, 0x3E, 0x17, 0xC6, 0xB9,
  29037. 0xBB, 0x59, 0xBD, 0xC6, 0x97, 0x45, 0x2C, 0xD0,
  29038. 0x59, 0x45, 0x19, 0x83, 0xD7, 0x38, 0xCA, 0x3F,
  29039. 0xD0, 0x34, 0xE3, 0xF5, 0x98, 0x88, 0x54, 0xCA,
  29040. 0x05, 0x03, 0x1D, 0xB0, 0x96, 0x11, 0x49, 0x89,
  29041. 0x88, 0x19, 0x7C, 0x6B, 0x30, 0xD2, 0x58, 0xDF,
  29042. 0xE2, 0x62, 0x65, 0x54, 0x1C, 0x89, 0xA4, 0xB3,
  29043. 0x1D, 0x68, 0x64, 0xE9, 0x38, 0x9B, 0x03, 0xCB,
  29044. 0x74, 0xF7, 0xEC, 0x43, 0x23, 0xFB, 0x94, 0x21,
  29045. 0xA4, 0xB9, 0x79, 0x0A, 0x26, 0xD1, 0x7B, 0x03,
  29046. 0x98, 0xA2, 0x67, 0x67, 0x35, 0x09, 0x09, 0xF8,
  29047. 0x4D, 0x57, 0xB6, 0x69, 0x4D, 0xF8, 0x30, 0x66,
  29048. 0x4C, 0xA8, 0xB3, 0xC3, 0xC0, 0x3E, 0xD2, 0xAE,
  29049. 0x67, 0xB8, 0x90, 0x06, 0x86, 0x8A, 0x68, 0x52,
  29050. 0x7C, 0xCD, 0x66, 0x64, 0x59, 0xAB, 0x7F, 0x05,
  29051. 0x66, 0x71, 0x00, 0x0C, 0x61, 0x64, 0xD3, 0xA7,
  29052. 0xF2, 0x66, 0xA1, 0x4D, 0x97, 0xCB, 0xD7, 0x00,
  29053. 0x4D, 0x6C, 0x92, 0xCA, 0xCA, 0x77, 0x0B, 0x84,
  29054. 0x4A, 0x4F, 0xA9, 0xB1, 0x82, 0xE7, 0xB1, 0x8C,
  29055. 0xA8, 0x85, 0x08, 0x2A, 0xC5, 0x64, 0x6F, 0xCB,
  29056. 0x4A, 0x14, 0xE1, 0x68, 0x5F, 0xEB, 0x0C, 0x9C,
  29057. 0xE3, 0x37, 0x2A, 0xB9, 0x53, 0x65, 0xC0, 0x4F,
  29058. 0xD8, 0x30, 0x84, 0xF8, 0x0A, 0x23, 0xFF, 0x10,
  29059. 0xA0, 0x5B, 0xF1, 0x5F, 0x7F, 0xA5, 0xAC, 0xC6,
  29060. 0xC0, 0xCB, 0x46, 0x2C, 0x33, 0xCA, 0x52, 0x4F,
  29061. 0xA6, 0xB8, 0xBB, 0x35, 0x90, 0x43, 0xBA, 0x68,
  29062. 0x60, 0x9E, 0xAA, 0x25, 0x36, 0xE8, 0x1D, 0x08,
  29063. 0x46, 0x3B, 0x19, 0x65, 0x3B, 0x54, 0x35, 0xBA,
  29064. 0x94, 0x6C, 0x9A, 0xDD, 0xEB, 0x20, 0x2B, 0x04,
  29065. 0xB0, 0x31, 0xCC, 0x96, 0x0D, 0xCC, 0x12, 0xE4,
  29066. 0x51, 0x8D, 0x42, 0x8B, 0x32, 0xB2, 0x57, 0xA4,
  29067. 0xFC, 0x73, 0x13, 0xD3, 0xA7, 0x98, 0x0D, 0x80,
  29068. 0x08, 0x2E, 0x93, 0x4F, 0x9D, 0x95, 0xC3, 0x2B,
  29069. 0x0A, 0x01, 0x91, 0xA2, 0x36, 0x04, 0x38, 0x4D,
  29070. 0xD9, 0xE0, 0x79, 0xBB, 0xBA, 0xA2, 0x66, 0xD1,
  29071. 0x4C, 0x3F, 0x75, 0x6B, 0x9F, 0x21, 0x33, 0x10,
  29072. 0x74, 0x33, 0xA4, 0xE8, 0x3F, 0xA7, 0x18, 0x72,
  29073. 0x82, 0xA8, 0x09, 0x20, 0x3A, 0x4F, 0xAF, 0x84,
  29074. 0x18, 0x51, 0x83, 0x3D, 0x12, 0x1A, 0xC3, 0x83,
  29075. 0x84, 0x3A, 0x5E, 0x55, 0xBC, 0x23, 0x81, 0x42,
  29076. 0x5E, 0x16, 0xC7, 0xDB, 0x4C, 0xC9, 0xAB, 0x5C,
  29077. 0x1B, 0x0D, 0x91, 0xA4, 0x7E, 0x2B, 0x8D, 0xE0,
  29078. 0xE5, 0x82, 0xC8, 0x6B, 0x6B, 0x0D, 0x90, 0x7B,
  29079. 0xB3, 0x60, 0xB9, 0x7F, 0x40, 0xAB, 0x5D, 0x03,
  29080. 0x8F, 0x6B, 0x75, 0xC8, 0x14, 0xB2, 0x7D, 0x9B,
  29081. 0x96, 0x8D, 0x41, 0x98, 0x32, 0xBC, 0x8C, 0x2B,
  29082. 0xEE, 0x60, 0x5E, 0xF6, 0xE5, 0x05, 0x9D, 0x33,
  29083. 0x10, 0x0D, 0x90, 0x48, 0x5D, 0x37, 0x84, 0x50,
  29084. 0x01, 0x42, 0x21, 0x73, 0x6C, 0x07, 0x40, 0x7C,
  29085. 0xAC, 0x26, 0x04, 0x08, 0xAA, 0x64, 0x92, 0x66,
  29086. 0x19, 0x78, 0x8B, 0x86, 0x01, 0xC2, 0xA7, 0x52,
  29087. 0xD1, 0xA6, 0xCB, 0xF8, 0x20, 0xD7, 0xC7, 0xA0,
  29088. 0x47, 0x16, 0x20, 0x32, 0x25, 0xB3, 0x89, 0x5B,
  29089. 0x93, 0x42, 0xD1, 0x47, 0xA8, 0x18, 0x5C, 0xFC,
  29090. 0x1B, 0xB6, 0x5B, 0xA0, 0x6B, 0x41, 0x42, 0x33,
  29091. 0x99, 0x03, 0xC0, 0xAC, 0x46, 0x51, 0x38, 0x5B,
  29092. 0x45, 0xD9, 0x8A, 0x8B, 0x19, 0xD2, 0x8C, 0xD6,
  29093. 0xBA, 0xB0, 0x88, 0x78, 0x7F, 0x7E, 0xE1, 0xB1,
  29094. 0x24, 0x61, 0x76, 0x6B, 0x43, 0xCB, 0xCC, 0xB9,
  29095. 0x64, 0x34, 0x42, 0x7D, 0x93, 0xC0, 0x65, 0x55,
  29096. 0x06, 0x88, 0xF6, 0x94, 0x8E, 0xD1, 0xB5, 0x47,
  29097. 0x5A, 0x42, 0x5F, 0x1B, 0x85, 0x20, 0x9D, 0x06,
  29098. 0x1C, 0x08, 0xB5, 0x6C, 0x1C, 0xC0, 0x69, 0xF6,
  29099. 0xC0, 0xA7, 0xC6, 0xF2, 0x93, 0x58, 0xCA, 0xB9,
  29100. 0x11, 0x08, 0x77, 0x32, 0xA6, 0x49, 0xD2, 0x7C,
  29101. 0x9B, 0x98, 0xF9, 0xA4, 0x88, 0x79, 0x38, 0x7D,
  29102. 0x9B, 0x00, 0xC2, 0x59, 0x59, 0xA7, 0x16, 0x54,
  29103. 0xD6, 0xF6, 0xA9, 0x46, 0x16, 0x45, 0x13, 0xE4,
  29104. 0x7A, 0x75, 0xD0, 0x05, 0x98, 0x6C, 0x23, 0x63,
  29105. 0xC0, 0x9F, 0x6B, 0x53, 0x7E, 0xCA, 0x78, 0xB9,
  29106. 0x30, 0x3A, 0x5F, 0xA4, 0x57, 0x60, 0x8A, 0x58,
  29107. 0x6A, 0x65, 0x3A, 0x34, 0x7D, 0xB0, 0x4D, 0xFC,
  29108. 0xC1, 0x91, 0x75, 0xB3, 0xA3, 0x01, 0x17, 0x25,
  29109. 0x36, 0x06, 0x2A, 0x65, 0x8A, 0x95, 0x27, 0x75,
  29110. 0x70, 0xC8, 0x85, 0x2C, 0xA8, 0x97, 0x3F, 0x4A,
  29111. 0xE1, 0x23, 0xA3, 0x34, 0x04, 0x7D, 0xD7, 0x11,
  29112. 0xC8, 0x92, 0x7A, 0x63, 0x4A, 0x03, 0x38, 0x8A,
  29113. 0x52, 0x7B, 0x03, 0x4B, 0xF7, 0xA8, 0x17, 0x0F,
  29114. 0xA7, 0x02, 0xC1, 0xF7, 0xC2, 0x3E, 0xC3, 0x2D,
  29115. 0x18, 0xA2, 0x37, 0x48, 0x90, 0xBE, 0x9C, 0x78,
  29116. 0x7A, 0x94, 0x09, 0xC8, 0x2D, 0x19, 0x2C, 0x4B,
  29117. 0xB7, 0x05, 0xA2, 0xF9, 0x96, 0xCE, 0x40, 0x5D,
  29118. 0x85, 0xA4, 0xC1, 0xA1, 0xAB, 0x9B, 0x6A, 0xEB,
  29119. 0x49, 0xCC, 0xE1, 0xC2, 0xF8, 0xA9, 0x7C, 0x35,
  29120. 0x16, 0xC7, 0x2A, 0x00, 0xA4, 0x62, 0x63, 0xBA,
  29121. 0xA6, 0x96, 0xBF, 0x25, 0x72, 0x77, 0x19, 0xC3,
  29122. 0x21, 0x64, 0x23, 0x61, 0x8F, 0xF3, 0x33, 0x80,
  29123. 0x93, 0x4A, 0x6C, 0x10, 0x54, 0x5C, 0x4C, 0x5C,
  29124. 0x51, 0x55, 0xB1, 0x24, 0x86, 0x18, 0x1F, 0xC7,
  29125. 0xA2, 0x31, 0x98, 0x73, 0x97, 0x8B, 0x6A, 0x2A,
  29126. 0x67, 0x49, 0x0F, 0x82, 0x56, 0xBD, 0x21, 0x96,
  29127. 0xFE, 0x17, 0x92, 0xA4, 0xC0, 0x00, 0x77, 0xB8,
  29128. 0x12, 0xEA, 0xE8, 0xBE, 0xD3, 0x57, 0x24, 0x99,
  29129. 0x68, 0x4A, 0xB3, 0x37, 0x18, 0x76, 0x76, 0x1E,
  29130. 0x45, 0x0C, 0x9F, 0x9D, 0x27, 0x68, 0xA3, 0x68,
  29131. 0x06, 0xD7, 0xAB, 0x20, 0x46, 0xC9, 0x1F, 0x17,
  29132. 0x59, 0x9E, 0x9A, 0xC5, 0x92, 0x99, 0x08, 0x08,
  29133. 0xDC, 0xD7, 0xB4, 0xD0, 0x91, 0x90, 0x72, 0xF1,
  29134. 0x4E, 0xC3, 0x61, 0x77, 0x3B, 0x72, 0x52, 0x44,
  29135. 0x4C, 0x32, 0x3C, 0x30, 0x83, 0x26, 0xF4, 0xA3,
  29136. 0x0F, 0x86, 0x80, 0xD2, 0xF7, 0x48, 0xF5, 0x6A,
  29137. 0x13, 0x2B, 0x82, 0x67, 0x4E, 0xD0, 0x18, 0x46,
  29138. 0x20, 0xB8, 0x2A, 0xD2, 0xCB, 0x18, 0x2C, 0x97,
  29139. 0xB4, 0x81, 0x62, 0x66, 0x47, 0x49, 0x12, 0x90,
  29140. 0xA0, 0x11, 0xCC, 0x73, 0x82, 0x86, 0x85, 0xA8,
  29141. 0xC3, 0x67, 0xA5, 0xB9, 0xCF, 0x8D, 0x62, 0x1B,
  29142. 0x0D, 0x5C, 0x1E, 0xFF, 0x03, 0x17, 0x27, 0x58,
  29143. 0xBD, 0x00, 0x49, 0x78, 0xC2, 0x51, 0xCD, 0x51,
  29144. 0x34, 0x22, 0x28, 0x98, 0x9C, 0xAE, 0x63, 0x32,
  29145. 0xAC, 0x48, 0x64, 0x37, 0xCB, 0x5C, 0x57, 0xD4,
  29146. 0x30, 0x74, 0x62, 0x86, 0x52, 0x53, 0xBE, 0x21,
  29147. 0x7B, 0x35, 0x15, 0xC7, 0x3D, 0xF4, 0x05, 0xB7,
  29148. 0xF2, 0x82, 0x17, 0xAD, 0x0B, 0x8C, 0xF6, 0x0C,
  29149. 0x2F, 0xFF, 0xAA, 0x0A, 0x00, 0x48, 0xB1, 0xFB,
  29150. 0x4A, 0xCD, 0xCD, 0xC3, 0x8B, 0x52, 0x50, 0xCF,
  29151. 0xEC, 0x35, 0x6A, 0x6D, 0xE2, 0x6C, 0xFA, 0x7A,
  29152. 0x58, 0x8F, 0xDC, 0x86, 0xF9, 0x8C, 0x85, 0x4A,
  29153. 0xC6, 0x4C, 0x7B, 0xFA, 0xA9, 0x6F, 0x5A, 0x32,
  29154. 0xCC, 0x06, 0x10, 0x93, 0x4B, 0xAA, 0x6A, 0x58,
  29155. 0x6B, 0x9A, 0x20, 0x54, 0xF1, 0x3B, 0xA2, 0x74,
  29156. 0x17, 0x4A, 0xA0, 0xD2, 0xB3, 0xA8, 0x1B, 0x96,
  29157. 0xA9, 0x40, 0x66, 0x6F, 0x78, 0x9B, 0x5A, 0x6B,
  29158. 0xCD, 0xC0, 0xA6, 0xA0, 0x17, 0x8A, 0x0C, 0x9A,
  29159. 0x02, 0x57, 0x8A, 0x49, 0x3F, 0x6E, 0xEA, 0x0D,
  29160. 0x2E, 0x6C, 0x13, 0x95, 0x1C, 0x9F, 0x24, 0x9A,
  29161. 0x5E, 0x8D, 0xD7, 0x1D, 0xD4, 0x9A, 0x74, 0x2D,
  29162. 0x45, 0x1F, 0x1A, 0xBB, 0xA1, 0x9A, 0xF8, 0xC5,
  29163. 0x47, 0x85, 0x5E, 0x0A, 0xFC, 0x72, 0x8E, 0x90,
  29164. 0xAB, 0xB4, 0x99, 0xC9, 0xBE, 0xEB, 0x76, 0x6F,
  29165. 0x47, 0x29, 0xCD, 0xA2, 0x22, 0x63, 0xE3, 0x24,
  29166. 0xD2, 0x23, 0x02, 0xCB, 0xD3, 0x39, 0x9F, 0xAC,
  29167. 0xC6, 0x30, 0x99, 0x1F, 0xC8, 0xF2, 0x8B, 0xDB,
  29168. 0x43, 0x54, 0x76, 0x25, 0x41, 0x52, 0x76, 0x78,
  29169. 0xBC, 0xF6, 0x1F, 0x65, 0xC2, 0x41, 0x14, 0x6C,
  29170. 0x42, 0x6D, 0x23, 0xB9, 0xBF, 0xAA, 0x6B, 0x7D,
  29171. 0xF1, 0x8C, 0x97, 0xF2, 0x0C, 0x1B, 0x61, 0x25,
  29172. 0xBF, 0x87, 0x4B, 0x1D, 0x89, 0x47, 0x58, 0x52,
  29173. 0xC4, 0x48, 0x21, 0x5D, 0xB0, 0xEB, 0x77, 0x37,
  29174. 0xF9, 0x14, 0x80, 0xE8, 0xCE, 0xBD, 0x9A, 0x08,
  29175. 0x71, 0x57, 0x4F, 0x5A, 0xB6, 0x2D, 0x90, 0x20,
  29176. 0x17, 0x5E, 0xC6, 0x92, 0x7C, 0xA0, 0xB5, 0x4C,
  29177. 0x09, 0x81, 0x8E, 0x42, 0xCF, 0x92, 0xA3, 0x83,
  29178. 0x17, 0x24, 0x22, 0xC7, 0xDC, 0x18, 0x31, 0xD6,
  29179. 0x3B, 0x0C, 0x29, 0x5D, 0xE7, 0x51, 0x59, 0xDB,
  29180. 0x80, 0x34, 0xE9, 0xE0, 0x7F, 0x7B, 0x0B, 0x91,
  29181. 0x0C, 0x3C, 0x1E, 0x5F, 0xB6, 0x6B, 0x3D, 0xC5,
  29182. 0x23, 0xF1, 0xFA, 0x6E, 0xB4, 0x91, 0x0C, 0xB8,
  29183. 0x9A, 0x6C, 0x17, 0x56, 0x2C, 0x83, 0xAB, 0x4C,
  29184. 0x18, 0xD0, 0xCD, 0x7E, 0x07, 0x96, 0x59, 0x2A,
  29185. 0x37, 0x2A, 0xA4, 0x09, 0xB1, 0xC5, 0x57, 0x34,
  29186. 0x7C, 0xCA, 0xCD, 0xC4, 0x64, 0x4A, 0x11, 0x90,
  29187. 0x64, 0xD0, 0x6D, 0xD4, 0x74, 0x92, 0x9D, 0x1C,
  29188. 0x6F, 0xB4, 0xD6, 0x86, 0xE5, 0x49, 0x1C, 0xE4,
  29189. 0xBC, 0x89, 0xA3, 0x0B, 0xB4, 0xB8, 0xC4, 0x1B,
  29190. 0xCE, 0x51, 0x57, 0xDF, 0xC1, 0x36, 0x08, 0x23,
  29191. 0xB1, 0xAB, 0x61, 0x8C, 0x14, 0xB1, 0x0F, 0x98,
  29192. 0xC2, 0x50, 0x67, 0x39, 0x8E, 0xA7, 0x01, 0x8C,
  29193. 0x27, 0x8A, 0x4B, 0x3D, 0xF3, 0x13, 0x34, 0xD6,
  29194. 0x03, 0xB2, 0x04, 0x4E, 0xF1, 0x87, 0xCD, 0x9B,
  29195. 0xC6, 0xCE, 0x42, 0x72, 0x5B, 0xD9, 0x62, 0xC2,
  29196. 0x64, 0x98, 0x3E, 0x9E, 0x18, 0x15, 0x5A, 0x8B,
  29197. 0x9C, 0x47, 0x14, 0x3D, 0x70, 0x46, 0x0A, 0x26,
  29198. 0xA5, 0x6F, 0xE7, 0x65, 0x8C, 0x1F, 0x15, 0x03,
  29199. 0x48, 0xC6, 0x08, 0x7E, 0xF7, 0x58, 0xAD, 0x16,
  29200. 0x78, 0x87, 0x86, 0x0A, 0x00, 0x7A, 0x5F, 0xC3,
  29201. 0x73, 0x58, 0xD4, 0x3B, 0x5E, 0xBE, 0xE8, 0x20,
  29202. 0xAC, 0xEA, 0x47, 0x4F, 0x0A, 0xC0, 0x7B, 0x76,
  29203. 0x80, 0x28, 0x66, 0x19, 0x9C, 0x61, 0x23, 0x1D,
  29204. 0x5C, 0x74, 0x7C, 0x93, 0x77, 0x4D, 0x2C, 0x1E,
  29205. 0x0C, 0x1C, 0x67, 0xE6, 0xC8, 0x1B, 0x82, 0x75,
  29206. 0x21, 0x73, 0xE1, 0x25, 0xBA, 0xF3, 0x9B, 0x4F,
  29207. 0xD1, 0x9A, 0x4F, 0x45, 0x3D, 0xC5, 0x79, 0x76,
  29208. 0xB1, 0xD9, 0x7F, 0xE6, 0x99, 0x69, 0x92, 0xBB,
  29209. 0xB6, 0x5B, 0x7C, 0xB2, 0x5D, 0x07, 0x7B, 0xBA,
  29210. 0xA6, 0xA1, 0x33, 0x22, 0x89, 0x9A, 0xF6, 0x59,
  29211. 0xCF, 0x1B, 0x35, 0x58, 0xC1, 0xB5, 0x00, 0x11,
  29212. 0x54, 0xB6, 0x25, 0x80, 0x9E, 0xD8, 0x9A, 0xEE,
  29213. 0xBB, 0x89, 0xE6, 0xEA, 0x7D, 0x67, 0xF7, 0x23,
  29214. 0xD0, 0x45, 0xAB, 0x05, 0x71, 0x5C, 0x42, 0x35,
  29215. 0x5D, 0xA6, 0xA5, 0xC8, 0xDD, 0x39, 0xC8, 0xAB,
  29216. 0xE3, 0x03, 0x77, 0x51, 0xA0, 0x1E, 0xD1, 0xC7,
  29217. 0x37, 0x49, 0x19, 0xF3, 0x12, 0x1B, 0x5A, 0x52,
  29218. 0xC5, 0x3D, 0x14, 0x87, 0x31, 0x67, 0x69, 0xF8,
  29219. 0x07, 0x21, 0xDE, 0xEA, 0xAA, 0xD3, 0xC9, 0x0F,
  29220. 0x76, 0xE7, 0xAE, 0x9E, 0x12, 0xBA, 0x92, 0xB3,
  29221. 0x2B, 0x5F, 0xD4, 0x57, 0xE3, 0xC7, 0x52, 0xC2,
  29222. 0x65, 0x0D, 0xFB, 0x88, 0x57, 0x71, 0xCB, 0x77,
  29223. 0xAC, 0x3C, 0x78, 0x5A, 0x8C, 0x56, 0x2E, 0x6A,
  29224. 0x1C, 0x63, 0xC2, 0xA5, 0x5E, 0xA4, 0x7C, 0xF8,
  29225. 0xB9, 0x0E, 0xB8, 0x22, 0x5C, 0x12, 0x3C, 0x34,
  29226. 0x64, 0x52, 0x56, 0x62, 0x35, 0xB2, 0xF3, 0x18,
  29227. 0x23, 0xA3, 0x35, 0x21, 0xE0, 0x87, 0x93, 0x7A,
  29228. 0x34, 0x5D, 0x8D, 0x66, 0x3E, 0xEA, 0xA0, 0x56,
  29229. 0x58, 0x91, 0x7B, 0xBA, 0xA0, 0x08, 0xC2, 0xE3,
  29230. 0x35, 0xF8, 0x85, 0x0A, 0x90, 0xA3, 0x26, 0xD0,
  29231. 0xE6, 0x64, 0x32, 0xF4, 0x4C, 0xEB, 0x82, 0x89,
  29232. 0xE4, 0xEC, 0xB2, 0xD1, 0x29, 0x58, 0xE9, 0x84,
  29233. 0x07, 0x2E, 0xCA, 0xCB, 0x88, 0xE1, 0x34, 0x8F,
  29234. 0xF0, 0xB5, 0x56, 0x54, 0xAC, 0xBA, 0x5B, 0x54,
  29235. 0x97, 0x1C, 0xBA, 0xEB, 0xA8, 0x8E, 0xC4, 0xB9,
  29236. 0x1A, 0x94, 0xC3, 0x71, 0x92, 0xFA, 0x98, 0x2B,
  29237. 0xEC, 0xB9, 0xF3, 0xDA, 0x42, 0x16, 0x03, 0xB6,
  29238. 0x1A, 0x51, 0xBC, 0x8E, 0x36, 0xCB, 0xD0, 0x53,
  29239. 0x85, 0x1C, 0x77, 0xB1, 0xB9, 0x26, 0xB1, 0x7A,
  29240. 0x27, 0x2A, 0xA9, 0x02, 0x32, 0x46, 0xB0, 0x2B,
  29241. 0x3E, 0xD4, 0x7F, 0x66, 0xA0, 0x0B, 0xD5, 0x68,
  29242. 0x48, 0x23, 0x63, 0x4E, 0x7C, 0xE5, 0x8C, 0xF8,
  29243. 0xF3, 0x06, 0xE3, 0x5B, 0x1E, 0x53, 0x22, 0x82,
  29244. 0x4D, 0x90, 0x48, 0x01, 0xF0, 0xA2, 0xFA, 0x7C,
  29245. 0x2B, 0xC9, 0xC2, 0x52, 0xB0, 0xA5, 0x6B, 0x7B,
  29246. 0xA2, 0xAB, 0x0F, 0x63, 0x60, 0x21, 0x74, 0x5A,
  29247. 0x70, 0xA9, 0xA4, 0x3E, 0x2B, 0x0A, 0x8D, 0x61,
  29248. 0x59, 0x70, 0xB6, 0x53, 0x09, 0x62, 0x4B, 0x51,
  29249. 0x84, 0xBC, 0xC3, 0x0B, 0x91, 0x16, 0x79, 0xAE,
  29250. 0xDD, 0x76, 0x02, 0x5F, 0xE3, 0x90, 0x8F, 0xD6,
  29251. 0x78, 0x97, 0xB0, 0xCF, 0x4B, 0xE5, 0xA6, 0xF5,
  29252. 0x41, 0x3D, 0x7D, 0xD9, 0x85, 0x64, 0xB2, 0x3E,
  29253. 0x42, 0xA9, 0x3E, 0x4A, 0xA8, 0x82, 0x1C, 0xD4,
  29254. 0x50, 0x54, 0xC6, 0x43, 0xED, 0xC1, 0x15, 0x8D,
  29255. 0xB6, 0xB3, 0xDE, 0xB1, 0x3F, 0xB5, 0xA5, 0x1E,
  29256. 0xBD, 0x1A, 0x8A, 0x78, 0xB8, 0x72, 0x25, 0xA7,
  29257. 0x33, 0x8E, 0x10, 0x11, 0x04, 0xC4, 0xA2, 0x20,
  29258. 0xD9, 0xBD, 0xED, 0xD4, 0x8C, 0x85, 0xA1, 0xC2,
  29259. 0xDA, 0xE7, 0x81, 0xA8, 0x0C, 0x40, 0xE1, 0x3B,
  29260. 0x87, 0xEA, 0xC7, 0x3A, 0x76, 0x42, 0x01, 0xC9,
  29261. 0xB7, 0x60, 0xCC, 0xFB, 0x1A, 0xE3, 0x92, 0x69,
  29262. 0x9C, 0x70, 0x39, 0xD2, 0x7C, 0x39, 0x36, 0x2B,
  29263. 0x27, 0xB8, 0xFC, 0x6F, 0x07, 0xA8, 0xA3, 0xD4,
  29264. 0x41, 0x0F, 0x15, 0x47, 0xC4, 0x8A, 0x99, 0x97,
  29265. 0xF6, 0x2C, 0x61, 0x07, 0x44, 0x52, 0xEF, 0x15,
  29266. 0x15, 0xF8, 0xA6, 0x49, 0xEB, 0xCA, 0x94, 0x37,
  29267. 0x20, 0x5A, 0x4E, 0x8A, 0x61, 0x60, 0x6B, 0x41,
  29268. 0xDA, 0xF6, 0x83, 0x4D, 0x67, 0x1F, 0x4D, 0x85,
  29269. 0x2C, 0x0C, 0x9C, 0x40, 0x96, 0x61, 0x16, 0x48,
  29270. 0xC6, 0xA3, 0x17, 0x06, 0x78, 0xB1, 0x53, 0x7C,
  29271. 0xC1, 0x82, 0x8D, 0x93, 0x58, 0x0C, 0x9E, 0x58,
  29272. 0x49, 0xA9, 0x65, 0x31, 0x75, 0xAC, 0xB7, 0x53,
  29273. 0xF2, 0xBE, 0x74, 0x37, 0xBE, 0x45, 0xF6, 0xC6,
  29274. 0x03, 0xE4, 0x85, 0xF2, 0xEC, 0x30, 0x1B, 0xB4,
  29275. 0x2B, 0x6C, 0x37, 0xC2, 0x25, 0xD7, 0x49, 0x5A,
  29276. 0x58, 0x4A, 0xE2, 0x31, 0x89, 0x0A, 0xB5, 0xC8,
  29277. 0xC3, 0x5C, 0x26, 0x8C, 0xF4, 0xBB, 0xB0, 0x21,
  29278. 0x3C, 0x09, 0x60, 0x19, 0x31, 0x95, 0x61, 0xA8,
  29279. 0xA6, 0x94, 0x76, 0x37, 0xAA, 0x40, 0xD0, 0x06,
  29280. 0xB4, 0x15, 0xBB, 0x2C, 0xFA, 0x22, 0x37, 0xE0,
  29281. 0x89, 0x0B, 0x6A, 0x3B, 0xC1, 0x34, 0xAB, 0xF8,
  29282. 0xF6, 0x58, 0x5E, 0x10, 0x8D, 0x15, 0x94, 0x0F,
  29283. 0x91, 0xF4, 0xBF, 0x5B, 0x0C, 0x81, 0x80, 0x55,
  29284. 0xB2, 0x1D, 0xEA, 0x6E, 0x63, 0xB5, 0x53, 0x98,
  29285. 0x8C, 0x47, 0xF4, 0xB9, 0x4E, 0x7C, 0xF8, 0x00,
  29286. 0xA4, 0x93, 0xB4, 0x73, 0x47, 0x05, 0xED, 0xC5,
  29287. 0x6A, 0x4B, 0x60, 0x21, 0xC6, 0x29, 0x50, 0x06,
  29288. 0x75, 0x87, 0x68, 0x04, 0xCF, 0x0B, 0x95, 0x1F,
  29289. 0x03, 0x8A, 0x5C, 0x7F, 0xE5, 0x8E, 0x89, 0x77,
  29290. 0x4E, 0xF2, 0x99, 0x2F, 0xD7, 0xC6, 0x30, 0x99,
  29291. 0xD3, 0x52, 0xA7, 0xD2, 0x15, 0x60, 0xB7, 0x88,
  29292. 0xB4, 0x05, 0x70, 0x98, 0x61, 0x81, 0x7E, 0x59,
  29293. 0xA9, 0x6B, 0x3A, 0x3A, 0x83, 0xCB, 0xA8, 0x03,
  29294. 0xB1, 0x69, 0x34, 0x33, 0x10, 0x71, 0x90, 0x5B,
  29295. 0xBE, 0xC6, 0x53, 0x29, 0x00, 0x15, 0x5D, 0x8A,
  29296. 0xC8, 0x8C, 0xB3, 0x2E, 0x4E, 0x21, 0xA3, 0xBD,
  29297. 0x3A, 0x03, 0xFD, 0xEC, 0x32, 0x5A, 0x51, 0xCD,
  29298. 0x27, 0x73, 0x96, 0x4E, 0x67, 0x84, 0xFC, 0xF1,
  29299. 0x85, 0x37, 0x37, 0xAA, 0x64, 0xEB, 0x67, 0x56,
  29300. 0x47, 0x27, 0x27, 0x26, 0x61, 0xAB, 0xF8, 0x43,
  29301. 0x13, 0xA5, 0x7A, 0x44, 0xB1, 0x23, 0xC6, 0x55,
  29302. 0x09, 0xCF, 0xB7, 0xA6, 0xF6, 0x64, 0x1C, 0xDC,
  29303. 0xC3, 0xB5, 0x7F, 0xE6, 0x28, 0xC7, 0xB8, 0x19,
  29304. 0x2D, 0xB4, 0x4F, 0xFB, 0xF5, 0x79, 0x6A, 0x86,
  29305. 0x13, 0xB1, 0xFA, 0x12, 0x6F, 0x60, 0x76, 0x88,
  29306. 0x3C, 0x78, 0x3D, 0xC2, 0x4E, 0x2A, 0x44, 0x64,
  29307. 0xC4, 0x0B, 0x3A, 0x41, 0xCA, 0x70, 0xAE, 0x87,
  29308. 0x62, 0x08, 0x66, 0xCF, 0x4F, 0xCB, 0x2B, 0xD2,
  29309. 0x04, 0xBF, 0x5C, 0x28, 0x38, 0x12, 0xBA, 0x05,
  29310. 0x6A, 0xC0, 0xC3, 0x45, 0xE3, 0x79, 0xC4, 0xBA,
  29311. 0x24, 0xD7, 0x50, 0x90, 0x12, 0x79, 0xBB, 0x2F,
  29312. 0x3A, 0x16, 0xF6, 0x12, 0xBF, 0xAD, 0xB3, 0x57,
  29313. 0x03, 0x33, 0x2C, 0x7C, 0x13, 0x6F, 0x68, 0xEA,
  29314. 0xB6, 0x75, 0x5C, 0x66, 0xB6, 0xA4, 0xAD, 0x1A,
  29315. 0xAB, 0xA7, 0xB7, 0x68, 0xA5, 0x8A, 0xCA, 0xAC,
  29316. 0xC1, 0x0A, 0x45, 0x9A, 0x1C, 0xC8, 0xEF, 0x29,
  29317. 0x37, 0x7B, 0xC2, 0x00, 0xE4, 0xD3, 0x15, 0xA3,
  29318. 0x0A, 0x6B, 0xCC, 0x32, 0x56, 0xF9, 0x73, 0x4D,
  29319. 0x06, 0xE9, 0x77, 0x9C, 0xAA, 0x54, 0x42, 0xA9,
  29320. 0xA1, 0x60, 0x69, 0x08, 0x13, 0x77, 0xC7, 0x6E,
  29321. 0x75, 0x15, 0x43, 0x68, 0x07, 0x2D, 0xC4, 0x46,
  29322. 0xED, 0x6C, 0x8B, 0x8E, 0x62, 0x2A, 0x21, 0xE3,
  29323. 0x83, 0xCF, 0x9B, 0xA1, 0xFB, 0x43, 0x4E, 0x2E,
  29324. 0xCC, 0x81, 0xE7, 0xB7, 0x8C, 0xEE, 0x98, 0x6B,
  29325. 0x8F, 0xF7, 0x98, 0xAB, 0x18, 0xCF, 0x96, 0x34,
  29326. 0x54, 0x35, 0x46, 0x28, 0x4E, 0xDA, 0x2A, 0x26,
  29327. 0xB4, 0x7F, 0x05, 0xB7, 0x35, 0xBC, 0xDB, 0x12,
  29328. 0x02, 0x22, 0x00, 0x76, 0xDC, 0x8B, 0x4E, 0x4B,
  29329. 0x9F, 0x85, 0x35, 0x33, 0xC8, 0xF6, 0xC7, 0xFF,
  29330. 0x38, 0x81, 0x7B, 0xA4, 0x97, 0x12, 0x83, 0x57,
  29331. 0x85, 0xF1, 0x7F, 0x14, 0xCA, 0x01, 0xD0, 0xC1,
  29332. 0xC1, 0xE9, 0x88, 0x10, 0xFE, 0x0B, 0x36, 0xE5,
  29333. 0xB4, 0x27, 0x15, 0x7B, 0x94, 0x18, 0x44, 0x9C,
  29334. 0xED, 0xD6, 0x41, 0xA4, 0x29, 0x3C, 0x85, 0xC3,
  29335. 0x27, 0x00, 0x10, 0x2A, 0xCE, 0xC2, 0x2E, 0xBA,
  29336. 0xD9, 0x8E, 0xD1, 0x60, 0xA5, 0xF0, 0x27, 0xBD,
  29337. 0x4C, 0xDA, 0x57, 0xF1, 0xF3, 0x72, 0x0A, 0x12,
  29338. 0xC1, 0x34, 0x65, 0x4D, 0xD5, 0xE7, 0x3F, 0x82,
  29339. 0x96, 0x76, 0x49, 0x53, 0x90, 0xD0, 0xE7, 0x92,
  29340. 0x9D, 0x60, 0x34, 0xE9, 0xC5, 0x5F, 0x7D, 0x55,
  29341. 0xBA, 0x65, 0x8B, 0xC5, 0x87, 0x98, 0x8E, 0x8A,
  29342. 0xF9, 0x49, 0x60, 0xF6, 0xCF, 0xB8, 0xD5, 0xAF,
  29343. 0x7A, 0x00, 0x21, 0x53, 0x5A, 0x6E, 0x25, 0xE4,
  29344. 0x37, 0xD4, 0x9A, 0x78, 0x06, 0x98, 0xBE, 0x22,
  29345. 0xAC, 0x99, 0x53, 0x94, 0x9F, 0x57, 0x1B, 0x85,
  29346. 0xA6, 0x85, 0x72, 0x5F, 0x82, 0x07, 0xA2, 0xB0,
  29347. 0xAE, 0x84, 0x9B, 0x60, 0x1A, 0xB9, 0x1B, 0x15,
  29348. 0x9B, 0x3D, 0xF4, 0xA1, 0x54, 0xC2, 0x04, 0x1E,
  29349. 0x77, 0x60, 0x70, 0xAF, 0xC4, 0x29, 0x69, 0x32,
  29350. 0x23, 0x80, 0x91, 0x7C, 0x97, 0x51, 0x07, 0x99,
  29351. 0xF3, 0x14, 0x91, 0x31, 0x47, 0x7E, 0x16, 0x66,
  29352. 0x3D, 0x31, 0x74, 0xC7, 0xC1, 0xCA, 0xEA, 0x78,
  29353. 0x85, 0x35, 0xC6, 0xC0, 0x05, 0xA6, 0x4F, 0x28,
  29354. 0x68, 0x63, 0x1B, 0x31, 0xB6, 0x6E, 0x20, 0x5F,
  29355. 0xD3, 0x8C, 0x1D, 0x84, 0x54, 0x2D, 0x0F, 0x1B,
  29356. 0x57, 0x8F, 0x58, 0xC9, 0xBF, 0x5A, 0x0F, 0xAE,
  29357. 0xAB, 0x6A, 0xB6, 0x49, 0x48, 0x93, 0x05, 0x31,
  29358. 0x65, 0xEA, 0xFD, 0x46, 0x5F, 0xC6, 0x4A, 0x0C,
  29359. 0x5F, 0x8F, 0x3F, 0x90, 0x03, 0x48, 0x94, 0x15,
  29360. 0x89, 0x9D, 0x59, 0xA5, 0x43, 0xD8, 0x20, 0x8C,
  29361. 0x54, 0xA3, 0x16, 0x65, 0x29, 0xB5, 0x39, 0x22,
  29362. 0x8A, 0x39, 0xE8, 0x7D, 0x53, 0x1F, 0x35, 0x27,
  29363. 0xC2, 0x07, 0xED, 0xCC, 0x1D, 0xB7, 0xFA, 0xDD,
  29364. 0xCF, 0x96, 0x28, 0x39, 0x18, 0x79, 0xB3, 0x35,
  29365. 0xC7, 0x07, 0x83, 0x9A, 0x0D, 0xB0, 0x51, 0xA8,
  29366. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  29367. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  29368. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  29369. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  29370. };
  29371. const byte kyber1024_ct[] = {
  29372. 0xA6, 0xAF, 0x29, 0xD5, 0xF5, 0xB8, 0x0B, 0xD1,
  29373. 0x30, 0xF5, 0x18, 0xBA, 0xDD, 0xD6, 0xC8, 0xF1,
  29374. 0x75, 0x45, 0x41, 0x3D, 0x86, 0x0F, 0xB3, 0xDE,
  29375. 0x45, 0x19, 0x79, 0xEB, 0xFA, 0x5E, 0x4E, 0x31,
  29376. 0x12, 0xC7, 0xC0, 0xAD, 0xF9, 0x98, 0x24, 0xBB,
  29377. 0x52, 0x6F, 0x2C, 0x35, 0x50, 0x74, 0x8E, 0xD0,
  29378. 0xE1, 0x34, 0xF0, 0x45, 0x7A, 0x7C, 0x61, 0xF9,
  29379. 0xF5, 0x26, 0xF0, 0x02, 0xBA, 0xAD, 0xC0, 0x3F,
  29380. 0xC1, 0x3E, 0x38, 0x13, 0x12, 0x19, 0x51, 0x3C,
  29381. 0x3E, 0xDE, 0x06, 0x16, 0x61, 0xE7, 0x4F, 0x60,
  29382. 0x3C, 0x4F, 0xCF, 0x79, 0x51, 0xC8, 0xE5, 0x2C,
  29383. 0x9C, 0x21, 0x3B, 0x0D, 0x22, 0xD9, 0x29, 0x36,
  29384. 0x63, 0xD6, 0x69, 0xA6, 0xB5, 0x8E, 0xD8, 0xFC,
  29385. 0xEF, 0xCF, 0x82, 0x49, 0xD7, 0xBB, 0x52, 0x98,
  29386. 0xF5, 0x57, 0x61, 0x44, 0x5B, 0x2B, 0x83, 0xCE,
  29387. 0x7F, 0x00, 0x5C, 0xB0, 0x42, 0x48, 0xAE, 0xC8,
  29388. 0xBD, 0xA2, 0x2F, 0xD2, 0xD4, 0x2A, 0xA7, 0x66,
  29389. 0x32, 0x20, 0x14, 0xEA, 0x03, 0x8C, 0xC3, 0x2C,
  29390. 0x55, 0xC8, 0xE4, 0xB9, 0xE2, 0x8E, 0xC9, 0x11,
  29391. 0x9F, 0x52, 0x73, 0x41, 0xE4, 0xF6, 0x6A, 0x03,
  29392. 0x51, 0x21, 0x07, 0x3B, 0x85, 0xDE, 0x67, 0x06,
  29393. 0xDA, 0x19, 0xE0, 0x83, 0x8A, 0x9F, 0x33, 0xB7,
  29394. 0x19, 0xA6, 0x8F, 0x03, 0x9B, 0x66, 0x4D, 0xC0,
  29395. 0x02, 0x65, 0x9E, 0xAB, 0xFC, 0x39, 0x86, 0x79,
  29396. 0xAA, 0x70, 0x09, 0xCE, 0x0C, 0xD0, 0x1C, 0xDA,
  29397. 0xFB, 0x6C, 0xD2, 0xA2, 0x6F, 0xE4, 0x10, 0x16,
  29398. 0x72, 0xC9, 0x8F, 0xF5, 0x8F, 0x7C, 0x47, 0xD5,
  29399. 0xBD, 0xA2, 0x90, 0x66, 0x53, 0xB3, 0xA6, 0xF9,
  29400. 0x65, 0x1F, 0x7A, 0x12, 0x1E, 0xA7, 0x7E, 0xA7,
  29401. 0x47, 0x23, 0xFA, 0xE5, 0xB8, 0x73, 0xF9, 0xBB,
  29402. 0x7B, 0x66, 0x4F, 0x0C, 0x8A, 0x93, 0x83, 0x1E,
  29403. 0xF9, 0xD5, 0x1C, 0x7C, 0xC1, 0xEF, 0x44, 0xAC,
  29404. 0x0E, 0x55, 0xA5, 0x5C, 0xA7, 0x6D, 0x13, 0x7F,
  29405. 0xE9, 0xB7, 0x5F, 0x40, 0x50, 0x9C, 0xEF, 0x15,
  29406. 0x6E, 0x5A, 0xD1, 0x8F, 0x9F, 0xB9, 0x99, 0x68,
  29407. 0x00, 0x08, 0xE5, 0x47, 0xD5, 0x5E, 0xEC, 0xD5,
  29408. 0xB4, 0xD1, 0xCB, 0x1D, 0x9F, 0x07, 0x6C, 0xEC,
  29409. 0x21, 0x50, 0x1C, 0x74, 0x02, 0x50, 0x9E, 0xCB,
  29410. 0x77, 0xAF, 0xB2, 0xCB, 0x9A, 0x61, 0x34, 0x0A,
  29411. 0x8B, 0xD1, 0x51, 0x4C, 0x6E, 0x71, 0xB4, 0xAA,
  29412. 0x45, 0xE4, 0x7E, 0xC3, 0x75, 0x12, 0x27, 0x1B,
  29413. 0x91, 0x1F, 0x8F, 0xB4, 0x6C, 0x90, 0x82, 0xC9,
  29414. 0xDF, 0x07, 0x20, 0x4A, 0xBB, 0x5A, 0x50, 0xE6,
  29415. 0xE3, 0x64, 0x7A, 0x8A, 0xD4, 0xD8, 0xD5, 0xD7,
  29416. 0xBF, 0xF1, 0x9C, 0x8A, 0x50, 0x93, 0x08, 0xBC,
  29417. 0xFB, 0x89, 0x55, 0x36, 0xD0, 0x45, 0xCA, 0x2B,
  29418. 0x97, 0xCB, 0x16, 0xA2, 0x9B, 0xB7, 0x18, 0x1C,
  29419. 0xAD, 0x05, 0x09, 0xDD, 0xB9, 0x17, 0x35, 0x02,
  29420. 0x8E, 0xBA, 0x8C, 0x31, 0xD7, 0x4B, 0xD2, 0x75,
  29421. 0xEA, 0xA6, 0x5B, 0x53, 0x40, 0xB3, 0xA4, 0x3F,
  29422. 0xBF, 0xE0, 0xB3, 0x06, 0x1D, 0x6B, 0xAE, 0x7E,
  29423. 0x75, 0xB7, 0x09, 0x8C, 0xDA, 0xBE, 0x91, 0xD4,
  29424. 0xB3, 0x1E, 0x36, 0xC9, 0xAA, 0x7A, 0x82, 0x98,
  29425. 0x86, 0x2A, 0xD6, 0x3C, 0x8F, 0xD2, 0x82, 0xE0,
  29426. 0x3B, 0x46, 0x0B, 0x3A, 0xB4, 0x64, 0xCE, 0x0F,
  29427. 0x27, 0xB1, 0xC3, 0xD1, 0x11, 0x55, 0xAC, 0xAA,
  29428. 0x01, 0x1E, 0xB9, 0xE2, 0xAE, 0x3E, 0x6D, 0xDA,
  29429. 0x07, 0xD6, 0xF4, 0x91, 0x73, 0x7C, 0xBC, 0xE9,
  29430. 0xB0, 0x5F, 0x9B, 0xC5, 0x6B, 0xE2, 0x0E, 0x8D,
  29431. 0x32, 0x6B, 0xA1, 0x32, 0xC5, 0x7F, 0xB2, 0x35,
  29432. 0x16, 0x11, 0x44, 0x51, 0x9C, 0xDF, 0x40, 0x56,
  29433. 0x0F, 0xBE, 0x27, 0x9B, 0xDE, 0x41, 0x1E, 0x11,
  29434. 0x25, 0x31, 0xF8, 0x26, 0xD6, 0xAB, 0x10, 0xD4,
  29435. 0x54, 0x73, 0x50, 0xAD, 0xD2, 0xA9, 0xDE, 0x8D,
  29436. 0x62, 0xC2, 0xAC, 0x82, 0xCA, 0xBE, 0x68, 0x15,
  29437. 0x64, 0x6F, 0x4D, 0xC9, 0x74, 0x2B, 0xB0, 0xC2,
  29438. 0xA3, 0xF7, 0x7E, 0xC7, 0xB4, 0x6C, 0x6B, 0x53,
  29439. 0x76, 0x05, 0xFA, 0x31, 0x79, 0x8C, 0xD8, 0x92,
  29440. 0x81, 0x22, 0x1A, 0x33, 0xDF, 0xB9, 0x79, 0x6E,
  29441. 0x64, 0x43, 0x05, 0x63, 0x03, 0x32, 0xC2, 0xCB,
  29442. 0x93, 0x14, 0x08, 0xAB, 0x48, 0x1A, 0x16, 0xD9,
  29443. 0x53, 0xF6, 0xBE, 0xAE, 0x38, 0x91, 0xD6, 0xD9,
  29444. 0xAC, 0x1F, 0xAB, 0x38, 0x22, 0x2D, 0x92, 0x71,
  29445. 0x87, 0x2D, 0x9D, 0x0C, 0xAD, 0xB9, 0x1A, 0xBE,
  29446. 0x9B, 0x4E, 0x26, 0x5F, 0x75, 0xC6, 0xE5, 0xE8,
  29447. 0x29, 0xE1, 0x46, 0xC3, 0xD8, 0xCE, 0x1E, 0x9D,
  29448. 0x12, 0xE0, 0xD1, 0x29, 0x80, 0x19, 0x57, 0xF4,
  29449. 0x6B, 0x0D, 0x2D, 0xBE, 0x1F, 0x74, 0x9B, 0x1D,
  29450. 0x08, 0xE2, 0x34, 0x5F, 0x62, 0x39, 0xA7, 0x31,
  29451. 0x34, 0x2E, 0xB7, 0x5B, 0x0C, 0xF1, 0xBF, 0x41,
  29452. 0x17, 0x49, 0xBC, 0x2C, 0xAF, 0x28, 0x10, 0xB7,
  29453. 0x88, 0xC6, 0xB7, 0x23, 0x8B, 0x4D, 0x3D, 0xA2,
  29454. 0xD6, 0x31, 0x5C, 0xE9, 0x54, 0x2E, 0x24, 0x40,
  29455. 0x4F, 0x14, 0x57, 0x55, 0xA3, 0x0A, 0xB8, 0x51,
  29456. 0xE4, 0x44, 0x58, 0x41, 0xBD, 0x33, 0xF7, 0x16,
  29457. 0xA5, 0x86, 0x88, 0x48, 0x88, 0xEC, 0xC6, 0xBC,
  29458. 0x64, 0x98, 0xAA, 0x32, 0x91, 0x9A, 0xE8, 0x1D,
  29459. 0x20, 0xC2, 0x69, 0x73, 0xC2, 0xBD, 0x54, 0x58,
  29460. 0x2A, 0x0F, 0x6A, 0xD9, 0x8A, 0xBF, 0xD2, 0x62,
  29461. 0x7E, 0x15, 0x69, 0x0A, 0x72, 0x7E, 0x69, 0xF5,
  29462. 0x81, 0xDD, 0x2A, 0x71, 0x27, 0x98, 0x2A, 0x90,
  29463. 0xE3, 0x3E, 0x2D, 0x4A, 0x03, 0xFE, 0x33, 0x91,
  29464. 0x42, 0xC7, 0xE4, 0x4C, 0x32, 0x6A, 0xC4, 0x6E,
  29465. 0xD3, 0x95, 0xA2, 0x25, 0xD3, 0x03, 0x33, 0x89,
  29466. 0x91, 0x73, 0x28, 0xB4, 0x53, 0x16, 0xB1, 0x58,
  29467. 0x5A, 0x01, 0xB2, 0xC3, 0x04, 0xB2, 0x94, 0x4E,
  29468. 0x90, 0x3A, 0xBB, 0xB3, 0xEC, 0x56, 0x19, 0x44,
  29469. 0x1C, 0xFC, 0x89, 0x65, 0xA4, 0x46, 0xDF, 0x75,
  29470. 0xDE, 0xFA, 0x80, 0xC6, 0xE1, 0x5A, 0xDB, 0xD5,
  29471. 0x06, 0xB7, 0xAB, 0x2D, 0xE1, 0x2D, 0xDA, 0x9B,
  29472. 0xC8, 0x14, 0x41, 0xCF, 0xC8, 0x90, 0x52, 0xE2,
  29473. 0xE5, 0x80, 0x8F, 0x71, 0x26, 0xC6, 0xFD, 0x3A,
  29474. 0xC6, 0xAC, 0x80, 0x81, 0x25, 0x8A, 0x84, 0xA0,
  29475. 0x9A, 0xE5, 0x0F, 0x6C, 0xD7, 0xCC, 0x0F, 0x4A,
  29476. 0xF3, 0x36, 0xFD, 0x1D, 0x64, 0x3E, 0x99, 0x07,
  29477. 0x99, 0x96, 0x26, 0x8C, 0x2D, 0x32, 0xD9, 0x09,
  29478. 0xF2, 0x2E, 0x35, 0x04, 0xF0, 0x7F, 0xBB, 0x56,
  29479. 0x31, 0x96, 0xD4, 0x31, 0x2F, 0xDD, 0xB9, 0x33,
  29480. 0x5D, 0x5C, 0x1D, 0x36, 0xE8, 0xC5, 0xEE, 0xA2,
  29481. 0x27, 0x8D, 0xBA, 0x23, 0xB9, 0x4D, 0x19, 0x3C,
  29482. 0x94, 0x7C, 0xC4, 0x1C, 0xA9, 0x93, 0xDC, 0x7D,
  29483. 0xB1, 0x39, 0x63, 0x40, 0xAD, 0x9C, 0x4F, 0xE6,
  29484. 0x87, 0xDD, 0x7B, 0x8D, 0x0C, 0x7A, 0x51, 0x20,
  29485. 0xAE, 0x02, 0x04, 0xF2, 0xC6, 0x65, 0xBD, 0x5F,
  29486. 0x47, 0x3D, 0x64, 0x4C, 0x7F, 0xF2, 0x6B, 0xFF,
  29487. 0xBA, 0x7A, 0x36, 0x98, 0x08, 0x30, 0x70, 0x21,
  29488. 0x28, 0xA7, 0xE6, 0x61, 0xD6, 0x77, 0xA0, 0x92,
  29489. 0xA3, 0x6E, 0x74, 0x28, 0xA4, 0x13, 0x9F, 0xB2,
  29490. 0x9B, 0x00, 0x95, 0xCC, 0x11, 0x08, 0x6F, 0x44,
  29491. 0x7D, 0x2A, 0x9E, 0xF6, 0xC9, 0xB1, 0x61, 0xF1,
  29492. 0x89, 0xC6, 0x29, 0x9E, 0x08, 0x4C, 0xB7, 0xAA,
  29493. 0x00, 0xFA, 0xF7, 0x87, 0x79, 0x7B, 0xFB, 0x06,
  29494. 0x9F, 0xBC, 0x08, 0x7F, 0xDE, 0x26, 0x25, 0x2A,
  29495. 0x16, 0x64, 0xF1, 0x9C, 0x5A, 0x8A, 0x22, 0xEC,
  29496. 0x5E, 0xE1, 0xAE, 0xB0, 0x76, 0x35, 0x7B, 0x7D,
  29497. 0xC3, 0x7E, 0x6B, 0x0F, 0x15, 0x20, 0xF9, 0x58,
  29498. 0xF7, 0x85, 0x1B, 0xAC, 0xB9, 0x2C, 0x89, 0xFD,
  29499. 0x11, 0x4A, 0x72, 0xFE, 0xAC, 0x54, 0x65, 0x2D,
  29500. 0x45, 0xB0, 0x9E, 0x1A, 0xE7, 0x65, 0x1A, 0xBD,
  29501. 0x16, 0x4B, 0xCD, 0x53, 0x7D, 0x58, 0xFA, 0x39,
  29502. 0xD3, 0xEC, 0x8A, 0xCD, 0xCD, 0xF9, 0x84, 0x25,
  29503. 0x00, 0x58, 0x62, 0xFA, 0x59, 0x69, 0x2D, 0xE1,
  29504. 0x62, 0xB7, 0x7E, 0x62, 0x97, 0xC6, 0x62, 0x33,
  29505. 0x34, 0x84, 0x08, 0xA8, 0xAB, 0x69, 0x5C, 0xE2,
  29506. 0xF2, 0x72, 0x8D, 0xB9, 0xFB, 0xE2, 0x7E, 0x95,
  29507. 0x89, 0x67, 0xEC, 0x59, 0x74, 0x76, 0x7C, 0x5A,
  29508. 0x66, 0x02, 0x30, 0x74, 0xB4, 0xA7, 0x1A, 0xFD,
  29509. 0x26, 0x4A, 0xD2, 0x89, 0x0E, 0x97, 0x0A, 0x1F,
  29510. 0x31, 0xD6, 0xE3, 0x31, 0x1B, 0x73, 0x6F, 0x9F,
  29511. 0x94, 0x88, 0x79, 0x3D, 0xDC, 0x88, 0xF2, 0x34,
  29512. 0x58, 0x06, 0x42, 0x54, 0xC8, 0x2A, 0x1D, 0x9E,
  29513. 0x59, 0xEA, 0xD2, 0xFC, 0xEC, 0x40, 0xB4, 0x30,
  29514. 0x68, 0x7C, 0x4B, 0x7E, 0x28, 0x96, 0x09, 0x26,
  29515. 0xAF, 0xCA, 0xCC, 0x9B, 0xD7, 0x56, 0xA7, 0x10,
  29516. 0x88, 0xC7, 0x84, 0x50, 0xE2, 0x0A, 0x2E, 0x98,
  29517. 0x0A, 0xED, 0xE9, 0xEB, 0xED, 0xFE, 0x7F, 0xAB,
  29518. 0xD6, 0xAB, 0xFE, 0x96, 0xF9, 0x34, 0xC4, 0xB0,
  29519. 0x2C, 0x01, 0xCA, 0x19, 0x4D, 0x01, 0xB7, 0x3C,
  29520. 0x25, 0xD5, 0x99, 0x70, 0x39, 0xD3, 0xFC, 0xD0,
  29521. 0xF0, 0x99, 0x52, 0x1F, 0x70, 0xCA, 0xEE, 0x69,
  29522. 0x11, 0x0A, 0xC1, 0xFC, 0x5A, 0x99, 0x91, 0x7A,
  29523. 0xD7, 0x52, 0xFC, 0x96, 0xAD, 0xFA, 0xD7, 0x18,
  29524. 0x6D, 0x0A, 0x7C, 0x9C, 0xFE, 0x56, 0x01, 0xC0,
  29525. 0x75, 0x14, 0xEA, 0x64, 0x48, 0xD6, 0x61, 0xC5,
  29526. 0x7A, 0xA2, 0x02, 0x42, 0x10, 0x3C, 0x42, 0x76,
  29527. 0xA0, 0x70, 0xA4, 0x89, 0xA4, 0xCB, 0x6B, 0xCA,
  29528. 0x0F, 0x9E, 0xCC, 0x43, 0x79, 0xFB, 0x22, 0x02,
  29529. 0x15, 0xFD, 0x91, 0xF8, 0x10, 0x19, 0xD5, 0xB0,
  29530. 0xAE, 0x61, 0x93, 0x58, 0xB5, 0x24, 0x68, 0xF2,
  29531. 0x72, 0xC1, 0x78, 0xE3, 0xA7, 0x4C, 0xF6, 0x77,
  29532. 0x5A, 0xA9, 0x24, 0xFE, 0x32, 0x9C, 0x31, 0x75,
  29533. 0xD9, 0xE4, 0xC3, 0xE2, 0x1A, 0xB9, 0xEC, 0x83,
  29534. 0x6E, 0xDC, 0x3A, 0xCA, 0xB2, 0xE3, 0x89, 0x1E,
  29535. 0xE8, 0xDE, 0xDA, 0x51, 0x5D, 0x39, 0xAF, 0x9B,
  29536. 0x8D, 0xDD, 0x0E, 0xE7, 0xB0, 0x16, 0x4F, 0x80,
  29537. 0x5C, 0x38, 0x35, 0xF6, 0xD2, 0xBA, 0xBD, 0xB3,
  29538. 0x0E, 0xAB, 0x47, 0x56, 0xE7, 0xEC, 0x7F, 0x82,
  29539. 0x9E, 0xCE, 0x01, 0xE8, 0xEA, 0xDF, 0xBB, 0xED,
  29540. 0x12, 0xFC, 0x28, 0x3B, 0x3D, 0x4C, 0x69, 0xF5,
  29541. 0x75, 0xE7, 0xF8, 0x04, 0x17, 0x68, 0x9F, 0xDF,
  29542. 0xCF, 0xC7, 0xBE, 0x27, 0xEE, 0x3B, 0x8C, 0xDF,
  29543. 0x57, 0xAA, 0xEB, 0xEC, 0x4A, 0x95, 0xB7, 0xE5,
  29544. 0xBB, 0x58, 0x5B, 0x85, 0x22, 0x7F, 0x7C, 0x32,
  29545. 0xBE, 0x30, 0xDB, 0x3E, 0x65, 0xE4, 0x2E, 0x30,
  29546. 0xDC, 0xF5, 0xA5, 0xFA, 0x07, 0x3D, 0xBA, 0x39,
  29547. 0x9D, 0x94, 0x2F, 0x22, 0x22, 0xAD, 0xB9, 0xB9,
  29548. 0x89, 0x81, 0x02, 0xAF, 0xE5, 0x43, 0x2E, 0xDC,
  29549. 0x7F, 0x04, 0xAE, 0x34, 0xA8, 0xFE, 0xC2, 0xD8,
  29550. 0x1C, 0xB4, 0x9A, 0x9A, 0x9B, 0x43, 0x81, 0x4C,
  29551. 0xE7, 0x1D, 0x97, 0xF7, 0x26, 0xE2, 0xB1, 0xE8,
  29552. 0xF6, 0x4B, 0x50, 0xE6, 0x5D, 0xFB, 0x48, 0x16,
  29553. 0xE1, 0x2E, 0x82, 0xA3, 0x19, 0x74, 0x84, 0xA4,
  29554. 0xE9, 0xBB, 0xA4, 0xD2, 0xD6, 0x9E, 0x3F, 0x19,
  29555. 0xD0, 0xB7, 0x5C, 0x21, 0xE2, 0xBF, 0xFE, 0x9F,
  29556. 0xC0, 0xC9, 0x8C, 0xF4, 0x8A, 0x3A, 0xAF, 0x08,
  29557. 0xD4, 0x67, 0xF7, 0x26, 0x87, 0xDF, 0x01, 0x78,
  29558. 0x17, 0x4B, 0x78, 0x97, 0xF7, 0x34, 0x34, 0x9B,
  29559. 0x18, 0x1E, 0xCA, 0x86, 0xA5, 0x98, 0xA0, 0xC5,
  29560. 0xE8, 0xC2, 0x59, 0x46, 0xF2, 0x4D, 0xC5, 0x57,
  29561. 0x2B, 0xD3, 0x24, 0xA4, 0x04, 0x58, 0xA7, 0x88,
  29562. 0xE5, 0x13, 0x7F, 0x3C, 0x7A, 0x7C, 0x97, 0xFC,
  29563. 0x9F, 0x12, 0xA3, 0xC4, 0x63, 0xA8, 0xFE, 0x94,
  29564. 0x49, 0x10, 0x1C, 0xCE, 0x96, 0x6D, 0x7C, 0x00,
  29565. 0x93, 0x23, 0x93, 0x29, 0x98, 0xD5, 0x6E, 0xF4,
  29566. 0x30, 0xC7, 0x3B, 0xC2, 0x4F, 0x5D, 0x95, 0xF7,
  29567. 0x37, 0x85, 0x8D, 0xDC, 0x4F, 0x32, 0xC0, 0x13
  29568. };
  29569. const byte kyber1024_ss[] = {
  29570. 0xB1, 0x0F, 0x73, 0x94, 0x92, 0x6A, 0xD3, 0xB4,
  29571. 0x9C, 0x5D, 0x62, 0xD5, 0xAE, 0xB5, 0x31, 0xD5,
  29572. 0x75, 0x75, 0x38, 0xBC, 0xC0, 0xDA, 0x9E, 0x55,
  29573. 0x0D, 0x43, 0x8F, 0x1B, 0x61, 0xBD, 0x74, 0x19
  29574. };
  29575. #else
  29576. const byte kyber1024_pk[] = {
  29577. 0x68, 0xE6, 0xBC, 0x26, 0x8B, 0x9E, 0x36, 0x1B,
  29578. 0x8F, 0x6E, 0x0A, 0xBC, 0xFE, 0x88, 0x93, 0x37,
  29579. 0x87, 0x53, 0x48, 0xE4, 0x16, 0x4D, 0x66, 0x15,
  29580. 0xF3, 0x82, 0xC6, 0xC7, 0xC4, 0x17, 0x8F, 0x75,
  29581. 0x1F, 0x72, 0x2B, 0x21, 0x27, 0x74, 0x64, 0xFB,
  29582. 0x83, 0x1F, 0x8A, 0xA9, 0xA0, 0x18, 0xC1, 0x9A,
  29583. 0x51, 0xB0, 0x7F, 0xA0, 0x93, 0x9B, 0xA5, 0xC2,
  29584. 0x06, 0x68, 0x63, 0x23, 0xAF, 0xF6, 0xCA, 0x83,
  29585. 0x0B, 0x76, 0xE3, 0x80, 0x02, 0x63, 0x54, 0x47,
  29586. 0x7A, 0xAC, 0x58, 0xD8, 0x93, 0x1D, 0x80, 0x3B,
  29587. 0x6F, 0xAB, 0xD7, 0x12, 0xD1, 0x72, 0x81, 0x4A,
  29588. 0x54, 0xB8, 0x12, 0xE7, 0x92, 0x47, 0x94, 0xAA,
  29589. 0x40, 0x02, 0x49, 0xD5, 0xC0, 0x46, 0x8E, 0xA3,
  29590. 0x2F, 0xB2, 0x15, 0x54, 0xC5, 0x93, 0x67, 0x90,
  29591. 0x3C, 0x60, 0xA8, 0xB4, 0x8E, 0x23, 0x40, 0x71,
  29592. 0x2F, 0x7A, 0x04, 0x5A, 0x67, 0xC2, 0x3C, 0x72,
  29593. 0x41, 0x3A, 0x64, 0x69, 0xFC, 0x83, 0x69, 0xA2,
  29594. 0x91, 0x03, 0x3C, 0x46, 0xCE, 0xA3, 0x96, 0x76,
  29595. 0x65, 0x6B, 0x6C, 0xD8, 0x57, 0x99, 0x7C, 0x04,
  29596. 0x5C, 0x5A, 0xF0, 0x34, 0xE1, 0xE1, 0x14, 0x8A,
  29597. 0x8B, 0x40, 0xCD, 0x16, 0x68, 0x31, 0x93, 0xA3,
  29598. 0x60, 0x0C, 0x84, 0xFC, 0xE3, 0xB9, 0x65, 0x10,
  29599. 0x88, 0xDD, 0x09, 0x44, 0x57, 0x6C, 0x7E, 0xA4,
  29600. 0x07, 0x2D, 0x25, 0xF0, 0x67, 0xD2, 0x42, 0x7E,
  29601. 0x26, 0xD1, 0x86, 0xF9, 0xB7, 0x7A, 0x32, 0x79,
  29602. 0x05, 0x03, 0x3B, 0xA9, 0x40, 0x85, 0xA8, 0xAB,
  29603. 0x6A, 0x8A, 0x2B, 0x86, 0x08, 0x0C, 0x45, 0x2B,
  29604. 0x10, 0x90, 0xAC, 0xC9, 0x5A, 0x2B, 0x9F, 0x5A,
  29605. 0x6E, 0x61, 0x84, 0x6D, 0x63, 0x45, 0x66, 0xFA,
  29606. 0xC0, 0xB0, 0xFF, 0x35, 0x11, 0x18, 0x06, 0x18,
  29607. 0xAF, 0x98, 0x34, 0x87, 0x12, 0x2C, 0xFB, 0x63,
  29608. 0x61, 0xFF, 0x66, 0x0D, 0x5C, 0xA0, 0x0B, 0x9D,
  29609. 0x88, 0xB1, 0xDA, 0x18, 0x36, 0xC9, 0x78, 0x6E,
  29610. 0x55, 0x27, 0x6C, 0x88, 0x53, 0x8C, 0xC1, 0xD3,
  29611. 0x05, 0xFA, 0xAA, 0xB7, 0x79, 0x9A, 0x35, 0x46,
  29612. 0xF0, 0xBE, 0x6C, 0xC7, 0x00, 0xB0, 0x65, 0xB9,
  29613. 0x03, 0x4A, 0x91, 0x63, 0x31, 0x74, 0x66, 0xD9,
  29614. 0x83, 0xDD, 0xC2, 0xAA, 0x5B, 0xB0, 0x3F, 0x5B,
  29615. 0x67, 0x33, 0x48, 0xD5, 0x76, 0x02, 0x8A, 0x02,
  29616. 0x55, 0x15, 0x6C, 0x6A, 0x29, 0x74, 0x46, 0x98,
  29617. 0x34, 0xFB, 0x15, 0x73, 0x23, 0x74, 0x60, 0x54,
  29618. 0xC4, 0xBA, 0x96, 0x40, 0x30, 0x4A, 0x11, 0x63,
  29619. 0x04, 0xF0, 0x18, 0x21, 0x72, 0xBA, 0x8D, 0x03,
  29620. 0xAE, 0xDA, 0x39, 0x91, 0x6A, 0xF1, 0x18, 0xB9,
  29621. 0x84, 0x22, 0xB7, 0x16, 0x3A, 0xB6, 0xE2, 0x47,
  29622. 0xFC, 0xAA, 0x9D, 0x7C, 0x7C, 0xAE, 0x96, 0xBB,
  29623. 0x60, 0xB1, 0xD7, 0x86, 0x0E, 0x4C, 0x7F, 0x1D,
  29624. 0x92, 0x0D, 0x31, 0xA5, 0x41, 0x38, 0x82, 0x29,
  29625. 0xEE, 0x84, 0x2C, 0xA1, 0x59, 0x43, 0x89, 0x4A,
  29626. 0x56, 0xE5, 0x14, 0x0D, 0x29, 0x59, 0xC3, 0x6E,
  29627. 0xA2, 0x6E, 0x2D, 0xE0, 0x51, 0x21, 0x36, 0x6B,
  29628. 0xE4, 0x14, 0x02, 0x7B, 0xD1, 0x47, 0x81, 0x47,
  29629. 0x2E, 0xDD, 0xA4, 0x71, 0x10, 0xD0, 0xB3, 0xAE,
  29630. 0x2A, 0xA2, 0x55, 0xAB, 0x6D, 0x6F, 0xE0, 0x1C,
  29631. 0x26, 0xC2, 0x87, 0x63, 0x97, 0x6B, 0xBF, 0xA9,
  29632. 0x2F, 0x4F, 0x09, 0xA5, 0xB7, 0x3C, 0x47, 0x1F,
  29633. 0x8C, 0x9E, 0x53, 0x26, 0x92, 0x55, 0xDA, 0xC1,
  29634. 0xE0, 0x71, 0x5E, 0x87, 0x00, 0x06, 0x09, 0x70,
  29635. 0x60, 0xFF, 0x23, 0xC3, 0xAB, 0x43, 0xC4, 0x19,
  29636. 0xE4, 0x48, 0xC7, 0x30, 0x40, 0x10, 0xC4, 0x0F,
  29637. 0x3D, 0xC0, 0xB7, 0x26, 0x39, 0x2E, 0x46, 0x03,
  29638. 0x7E, 0xB9, 0x49, 0x04, 0x55, 0xE3, 0x49, 0xA4,
  29639. 0x35, 0xCD, 0xA9, 0x0A, 0xA3, 0xB7, 0x47, 0x8C,
  29640. 0x32, 0xE8, 0x72, 0xB2, 0xB8, 0x5C, 0xEC, 0xF0,
  29641. 0x3D, 0xDC, 0xD6, 0x16, 0x33, 0x78, 0xBF, 0x73,
  29642. 0xB1, 0x3B, 0x2E, 0x1A, 0x7B, 0x6E, 0xBC, 0x39,
  29643. 0x29, 0xEC, 0x08, 0x63, 0x89, 0x3F, 0x73, 0x8C,
  29644. 0x38, 0x9F, 0x3B, 0x3B, 0x2B, 0xB5, 0x4F, 0xC1,
  29645. 0x5B, 0xB7, 0x8A, 0x25, 0x31, 0xEE, 0xBC, 0xB7,
  29646. 0xBC, 0x10, 0x81, 0x07, 0x96, 0x59, 0x81, 0xD2,
  29647. 0x0A, 0x7D, 0x1A, 0x87, 0x21, 0xBA, 0x36, 0x40,
  29648. 0x03, 0x13, 0x2F, 0x03, 0x6C, 0xE4, 0x0C, 0x9E,
  29649. 0x75, 0x5B, 0x2A, 0xB4, 0x86, 0x1D, 0x04, 0xC1,
  29650. 0xBE, 0x0D, 0x53, 0x97, 0x91, 0x4B, 0x6C, 0xD4,
  29651. 0xAA, 0x40, 0x8C, 0x62, 0x2B, 0x21, 0xB1, 0x1B,
  29652. 0xA0, 0xEB, 0xA6, 0x17, 0x29, 0x59, 0x6F, 0x8B,
  29653. 0x0E, 0x82, 0x14, 0xAA, 0x4A, 0xA6, 0x01, 0x86,
  29654. 0x51, 0x07, 0xA2, 0xA7, 0x6A, 0xD7, 0xCC, 0x2F,
  29655. 0x97, 0x43, 0x00, 0x18, 0x28, 0xB8, 0x0B, 0x24,
  29656. 0x80, 0x2B, 0x98, 0x9D, 0x98, 0x42, 0x5E, 0x14,
  29657. 0x7C, 0x6D, 0x93, 0x32, 0xBA, 0x08, 0x4B, 0x8F,
  29658. 0xFB, 0x96, 0x27, 0xE3, 0xFB, 0xC4, 0xBE, 0x65,
  29659. 0x38, 0xEB, 0xDA, 0x1D, 0x9D, 0xB9, 0xB6, 0x75,
  29660. 0xB0, 0x16, 0x66, 0x57, 0x23, 0xD4, 0x11, 0x71,
  29661. 0x54, 0xF8, 0x13, 0x3E, 0xA9, 0x00, 0xCB, 0x47,
  29662. 0x0B, 0x1D, 0xC5, 0xB5, 0x7A, 0x78, 0x11, 0x16,
  29663. 0x72, 0x9E, 0xD6, 0x29, 0x45, 0x6E, 0x1A, 0x7D,
  29664. 0x6A, 0x37, 0x9F, 0x2E, 0x01, 0x0A, 0xCB, 0xEC,
  29665. 0x21, 0xFC, 0xD1, 0xC2, 0xB0, 0xBB, 0x61, 0xC3,
  29666. 0xDA, 0x6C, 0x73, 0x70, 0x5F, 0xD7, 0xB0, 0xC3,
  29667. 0x61, 0x2A, 0xC3, 0xB4, 0xA1, 0x2E, 0xC5, 0xD3,
  29668. 0x7F, 0x4C, 0x1A, 0x4A, 0x4D, 0xF8, 0x16, 0x6E,
  29669. 0xF6, 0x68, 0x9C, 0x06, 0xB5, 0xD4, 0xA6, 0x1A,
  29670. 0x52, 0x14, 0xAE, 0x5E, 0x5C, 0x28, 0x07, 0x4C,
  29671. 0xC5, 0xFE, 0x2A, 0x41, 0x2A, 0xC5, 0x14, 0x15,
  29672. 0x94, 0x28, 0x54, 0x85, 0x15, 0x6E, 0xCA, 0x65,
  29673. 0x64, 0x28, 0x37, 0x20, 0xB0, 0xBA, 0xD1, 0xD9,
  29674. 0x74, 0x5B, 0xA5, 0x77, 0x71, 0x1A, 0xB6, 0xF2,
  29675. 0x14, 0xC5, 0x2F, 0xB5, 0xBA, 0xAF, 0x8A, 0x1F,
  29676. 0x85, 0x33, 0x5A, 0xEB, 0x44, 0x37, 0x0B, 0x39,
  29677. 0xC5, 0x6C, 0x4C, 0x3D, 0x32, 0x75, 0x55, 0x60,
  29678. 0xCB, 0x2C, 0xA9, 0x45, 0x87, 0x51, 0xF1, 0x65,
  29679. 0x4B, 0x82, 0xC5, 0xE7, 0xFB, 0x0D, 0xB5, 0x16,
  29680. 0xBE, 0x1E, 0x42, 0x43, 0x3A, 0xD4, 0x7B, 0x6B,
  29681. 0xCC, 0xB3, 0x63, 0x61, 0xC4, 0x34, 0xEB, 0x59,
  29682. 0xCF, 0xF3, 0x38, 0x49, 0x54, 0xBB, 0x76, 0xF1,
  29683. 0x44, 0x14, 0x91, 0x67, 0x91, 0x3B, 0x85, 0x0F,
  29684. 0x75, 0x28, 0xBD, 0x76, 0x87, 0xB8, 0x8B, 0x64,
  29685. 0x84, 0xA6, 0x82, 0x68, 0x39, 0x97, 0x08, 0x42,
  29686. 0xA5, 0x93, 0xBB, 0x93, 0x45, 0x5A, 0x73, 0xBC,
  29687. 0x2C, 0x74, 0x62, 0x55, 0xC1, 0x3B, 0xC8, 0x52,
  29688. 0x1C, 0x73, 0x54, 0xE9, 0x8B, 0x98, 0x76, 0xA9,
  29689. 0x58, 0x6F, 0xC3, 0x7C, 0x5F, 0x24, 0x8D, 0xA0,
  29690. 0x55, 0x75, 0x8B, 0xD9, 0x61, 0x5E, 0x68, 0xBF,
  29691. 0x93, 0xEA, 0x0B, 0xA1, 0xD0, 0x22, 0xE2, 0xD2,
  29692. 0x22, 0x04, 0xA4, 0x45, 0x8C, 0xB6, 0x12, 0xB0,
  29693. 0x29, 0x41, 0xE8, 0x7A, 0xAC, 0xF1, 0xA3, 0x29,
  29694. 0x07, 0xD9, 0x8B, 0x4B, 0x13, 0xC6, 0xC9, 0xB4,
  29695. 0xAB, 0xD6, 0xDC, 0xCF, 0x66, 0x21, 0x06, 0xF5,
  29696. 0xD5, 0x8C, 0x2F, 0xC7, 0x51, 0x97, 0x35, 0x5E,
  29697. 0xB8, 0xCC, 0x41, 0x74, 0x2C, 0x6D, 0xA0, 0x03,
  29698. 0x40, 0x6D, 0x13, 0x56, 0xF8, 0xA0, 0x7A, 0x37,
  29699. 0x97, 0x88, 0x00, 0x60, 0x67, 0xCB, 0xF5, 0x2F,
  29700. 0x22, 0xD5, 0x3D, 0x68, 0x71, 0x22, 0x41, 0x09,
  29701. 0x34, 0x6E, 0xA8, 0x3B, 0x45, 0xCA, 0x60, 0xAF,
  29702. 0xEC, 0xCA, 0x0F, 0xA5, 0x67, 0x2B, 0x36, 0x64,
  29703. 0x89, 0xEB, 0x8D, 0xD1, 0xF7, 0x20, 0x4C, 0xDB,
  29704. 0x7D, 0x9B, 0xE1, 0x87, 0xF4, 0x39, 0x7B, 0x9B,
  29705. 0x4C, 0xA0, 0x31, 0x34, 0x75, 0x0D, 0xD5, 0x7D,
  29706. 0xAD, 0xE1, 0x88, 0x91, 0xCC, 0xA9, 0x47, 0x4B,
  29707. 0x65, 0x32, 0x60, 0x52, 0x50, 0xBC, 0xBB, 0x6E,
  29708. 0x02, 0x94, 0xEF, 0x95, 0x32, 0x6D, 0x25, 0x9B,
  29709. 0x08, 0xB2, 0x93, 0x8D, 0x8C, 0x59, 0xE2, 0x58,
  29710. 0x4B, 0x69, 0x10, 0x71, 0x85, 0xF3, 0x45, 0x30,
  29711. 0x24, 0x07, 0x35, 0x17, 0xC9, 0x89, 0x00, 0x9E,
  29712. 0xBB, 0x6B, 0x22, 0xED, 0xE9, 0x70, 0x43, 0x66,
  29713. 0xC6, 0x23, 0x2A, 0x62, 0x82, 0xF3, 0xBB, 0x09,
  29714. 0x08, 0x74, 0xA6, 0xFB, 0x0D, 0x84, 0x53, 0x6D,
  29715. 0x3F, 0x61, 0x3A, 0x21, 0xD1, 0x9D, 0x98, 0x44,
  29716. 0x17, 0xFA, 0xF0, 0x84, 0x6C, 0xB4, 0x28, 0x7B,
  29717. 0x3C, 0x70, 0x9F, 0x22, 0x62, 0x26, 0xA0, 0x9E,
  29718. 0x94, 0xFA, 0x89, 0x85, 0x43, 0x87, 0x4A, 0xE2,
  29719. 0x08, 0x11, 0xE3, 0x90, 0x80, 0xE6, 0xB2, 0xCE,
  29720. 0x49, 0x92, 0x26, 0xF5, 0x13, 0x8C, 0x14, 0x38,
  29721. 0x72, 0xDB, 0x86, 0xFB, 0xC2, 0x9C, 0xFD, 0xA2,
  29722. 0x9F, 0xD2, 0x26, 0x0E, 0xC7, 0xB9, 0xA2, 0x37,
  29723. 0xD1, 0x9A, 0xAB, 0x1B, 0x93, 0xE1, 0xDA, 0x4E,
  29724. 0xBC, 0x58, 0x9D, 0x82, 0x4C, 0x58, 0x90, 0x96,
  29725. 0x99, 0xD3, 0xE8, 0xBC, 0x7F, 0x53, 0xB3, 0x55,
  29726. 0xFA, 0x31, 0xE9, 0x6B, 0x1E, 0x35, 0x4A, 0xA4,
  29727. 0xB3, 0xC8, 0x4D, 0xF3, 0xC4, 0x7F, 0x80, 0xF9,
  29728. 0x32, 0xEA, 0x68, 0x5D, 0x96, 0xC5, 0xA5, 0x96,
  29729. 0x08, 0x22, 0xC6, 0x43, 0x64, 0x5F, 0x1A, 0x63,
  29730. 0xDD, 0x60, 0x8A, 0x09, 0x71, 0xC7, 0xF2, 0x15,
  29731. 0x7C, 0xA5, 0x24, 0x4D, 0x49, 0x2C, 0x01, 0x0B,
  29732. 0x71, 0xB7, 0x74, 0xC1, 0x0C, 0x07, 0xD3, 0xC8,
  29733. 0x4C, 0xA5, 0x4D, 0xE5, 0x00, 0x1F, 0xE0, 0xAC,
  29734. 0x4E, 0x69, 0x52, 0x81, 0x4E, 0xC1, 0x0F, 0xF9,
  29735. 0x8B, 0x19, 0xE7, 0x89, 0xC1, 0xAC, 0x68, 0x45,
  29736. 0xC7, 0x62, 0x80, 0x55, 0x27, 0x5C, 0x33, 0x34,
  29737. 0x41, 0x87, 0x1B, 0x8C, 0x30, 0x99, 0x9B, 0x05,
  29738. 0x31, 0x34, 0xC0, 0xE7, 0xB1, 0xAC, 0x6A, 0xA6,
  29739. 0x63, 0x12, 0xC1, 0xA5, 0xE2, 0x97, 0x92, 0x97,
  29740. 0x02, 0x56, 0x88, 0x14, 0x38, 0xDC, 0x46, 0x29,
  29741. 0x37, 0x4D, 0xFB, 0x9A, 0x91, 0x48, 0x62, 0x9D,
  29742. 0x14, 0x8C, 0x60, 0x1D, 0x4B, 0x6A, 0xA2, 0xC2,
  29743. 0x9C, 0xCE, 0xE8, 0x94, 0x7A, 0x27, 0x87, 0x0E,
  29744. 0x4C, 0x65, 0xE4, 0x12, 0x46, 0xD6, 0x25, 0x3D,
  29745. 0xC4, 0xF1, 0x3A, 0xEB, 0x31, 0xCD, 0x7E, 0x36,
  29746. 0x2F, 0x0F, 0x28, 0x84, 0xB9, 0xF7, 0x3B, 0xA9,
  29747. 0x93, 0x50, 0xAA, 0xA4, 0x02, 0x4A, 0x73, 0x14,
  29748. 0xBE, 0x69, 0x3A, 0xE4, 0x13, 0xA5, 0xCE, 0xA0,
  29749. 0x9A, 0x27, 0x27, 0x3A, 0x0B, 0xF6, 0xA4, 0x53,
  29750. 0xF8, 0x63, 0x9B, 0x48, 0x26, 0x8D, 0x21, 0xC9,
  29751. 0xAE, 0xC2, 0x62, 0xBE, 0x28, 0xC0, 0x7E, 0xA4,
  29752. 0xB9, 0x35, 0xA8, 0x69, 0x63, 0x26, 0xAA, 0x18,
  29753. 0xA5, 0x91, 0xCE, 0xE9, 0xC9, 0xF2, 0xA7, 0x29,
  29754. 0xBC, 0x50, 0x33, 0xDD, 0xE6, 0x37, 0x27, 0x38,
  29755. 0x85, 0x92, 0x1B, 0x73, 0xB8, 0x58, 0xA1, 0x88,
  29756. 0x33, 0xA7, 0xB8, 0x3A, 0x82, 0xFE, 0x02, 0xCC,
  29757. 0x33, 0x54, 0xC3, 0x0B, 0x0C, 0x4D, 0x0D, 0x3B,
  29758. 0xA1, 0x33, 0x1C, 0x6E, 0xC7, 0xA7, 0x0B, 0x7E,
  29759. 0x50, 0x3F, 0x76, 0x47, 0xA9, 0x52, 0xB0, 0x03,
  29760. 0x86, 0xCC, 0x90, 0xB3, 0x4A, 0x51, 0x4F, 0x99,
  29761. 0x5A, 0x56, 0x92, 0x05, 0xEB, 0x2A, 0x3F, 0xA7,
  29762. 0x9A, 0x78, 0x65, 0x21, 0x18, 0xD4, 0xBC, 0x48,
  29763. 0x46, 0x27, 0xCD, 0x02, 0x33, 0xBC, 0x5A, 0x35,
  29764. 0x16, 0x15, 0x0B, 0xAF, 0x6E, 0x10, 0x66, 0x5D,
  29765. 0xA7, 0x61, 0x8F, 0x28, 0x41, 0xB5, 0x02, 0x61,
  29766. 0x72, 0x48, 0x89, 0x3B, 0xAA, 0xB7, 0xE9, 0x03,
  29767. 0x89, 0x0D, 0xF0, 0xCA, 0xC2, 0x84, 0xCD, 0x11,
  29768. 0x65, 0xB4, 0x64, 0xC1, 0x8F, 0x4C, 0xFA, 0x8F,
  29769. 0x69, 0x2D, 0x3C, 0x8E, 0x8C, 0x73, 0x9F, 0xB3,
  29770. 0x78, 0x27, 0x74, 0x15, 0x61, 0xD5, 0xE7, 0x66,
  29771. 0x67, 0x51, 0x82, 0xFF, 0x83, 0xC9, 0x1C, 0xF0,
  29772. 0x33, 0x65, 0x88, 0x23, 0xD8, 0xFA, 0x91, 0xE1
  29773. };
  29774. const byte kyber1024_sk[] = {
  29775. 0x1B, 0xD1, 0x97, 0x95, 0x10, 0xBC, 0xE5, 0x03,
  29776. 0x1C, 0x0F, 0x99, 0x1D, 0xE0, 0x82, 0xC3, 0x89,
  29777. 0xA1, 0xBD, 0x15, 0x57, 0x19, 0x1D, 0x81, 0x03,
  29778. 0xBD, 0xCC, 0x04, 0xC1, 0x98, 0xAE, 0x3E, 0x95,
  29779. 0x95, 0xFD, 0xB6, 0x57, 0xB5, 0xC6, 0x39, 0x97,
  29780. 0x95, 0x79, 0xAE, 0x13, 0x5B, 0x11, 0xE2, 0x4D,
  29781. 0x36, 0xE5, 0x49, 0x44, 0x67, 0x83, 0x2E, 0x6A,
  29782. 0x8A, 0x46, 0xAA, 0x8F, 0x9F, 0xF9, 0x87, 0x4A,
  29783. 0x16, 0x00, 0x27, 0x84, 0x07, 0x71, 0x56, 0x2D,
  29784. 0x74, 0x61, 0xB1, 0x6C, 0x61, 0x87, 0xF3, 0xC2,
  29785. 0xC1, 0x9C, 0x9C, 0x8F, 0xE6, 0x41, 0x49, 0x7D,
  29786. 0x8A, 0x15, 0x5C, 0x51, 0x15, 0x7D, 0x91, 0x35,
  29787. 0xD9, 0xB4, 0x5D, 0x3C, 0x7C, 0x4C, 0xB1, 0x82,
  29788. 0x31, 0x8C, 0x11, 0xAD, 0x08, 0xB3, 0x9F, 0xAC,
  29789. 0x04, 0x15, 0xE6, 0xE2, 0x6D, 0xB3, 0x43, 0x91,
  29790. 0xDE, 0xC2, 0x47, 0x1B, 0x30, 0x3C, 0x05, 0x05,
  29791. 0xB6, 0xFE, 0x45, 0x6E, 0x81, 0x50, 0x21, 0xE8,
  29792. 0x2B, 0x5F, 0x2D, 0x16, 0x6F, 0xDF, 0xB5, 0xB9,
  29793. 0x23, 0xC8, 0x3A, 0x61, 0x67, 0x30, 0x28, 0xF0,
  29794. 0xBA, 0xE0, 0x28, 0x08, 0x9F, 0x47, 0x61, 0x67,
  29795. 0xE5, 0x75, 0x6C, 0xAA, 0x3E, 0xE0, 0xF1, 0x89,
  29796. 0xC6, 0x47, 0x81, 0xC7, 0x49, 0x27, 0xBC, 0xDB,
  29797. 0x67, 0xE7, 0x75, 0xCC, 0xA8, 0x5B, 0x44, 0xB8,
  29798. 0x8B, 0x10, 0x61, 0xD8, 0x96, 0x3F, 0xE4, 0x0B,
  29799. 0x1F, 0xB4, 0x34, 0x68, 0x20, 0x09, 0x0E, 0x5C,
  29800. 0x0B, 0x7C, 0x49, 0x08, 0x89, 0x33, 0xAD, 0xE3,
  29801. 0x82, 0x96, 0x88, 0x6C, 0x43, 0x45, 0x65, 0x7C,
  29802. 0x3F, 0x07, 0xCF, 0x1A, 0xE9, 0x76, 0xE6, 0xBA,
  29803. 0x77, 0x7E, 0xF0, 0x1F, 0xB9, 0xC3, 0xB0, 0xDC,
  29804. 0xF0, 0x58, 0x3A, 0xE5, 0x44, 0xCD, 0x80, 0x3A,
  29805. 0xC1, 0x39, 0xCA, 0x61, 0x9B, 0x8D, 0xCA, 0x13,
  29806. 0x1C, 0xD2, 0x73, 0xCE, 0xCB, 0x65, 0xBC, 0xEF,
  29807. 0x00, 0x29, 0x95, 0x12, 0xB0, 0x21, 0xBA, 0x1B,
  29808. 0x2F, 0xF2, 0x08, 0xEE, 0xBB, 0x08, 0xA6, 0x28,
  29809. 0x7E, 0xC4, 0x4C, 0x21, 0x7D, 0x94, 0x76, 0x40,
  29810. 0x42, 0x5C, 0x0E, 0x97, 0x13, 0x44, 0x5B, 0xC5,
  29811. 0x2B, 0xC3, 0xCD, 0x66, 0x01, 0x2C, 0x8C, 0xB7,
  29812. 0x10, 0xA2, 0xE8, 0xAD, 0x5F, 0xC5, 0x1C, 0x5B,
  29813. 0x71, 0xCE, 0x69, 0xE9, 0x6A, 0x22, 0x06, 0x6F,
  29814. 0x74, 0x4B, 0xBA, 0xB6, 0x47, 0x2F, 0xEF, 0x3A,
  29815. 0x5B, 0xCF, 0xD6, 0x1D, 0xF1, 0xF8, 0x6A, 0x95,
  29816. 0x64, 0x80, 0x03, 0x1A, 0x99, 0x0E, 0xE0, 0x7D,
  29817. 0xE5, 0x04, 0x5E, 0xBC, 0x49, 0x8F, 0x2D, 0x3A,
  29818. 0xBD, 0xE3, 0x28, 0xC4, 0x2A, 0xCB, 0x21, 0xAD,
  29819. 0x62, 0x5D, 0x7B, 0xCB, 0x85, 0x5A, 0x8A, 0x0A,
  29820. 0xCB, 0xA2, 0x85, 0x16, 0xD3, 0x5A, 0xAF, 0x92,
  29821. 0xBE, 0xB3, 0x9C, 0x46, 0xD8, 0xF4, 0xA0, 0x1F,
  29822. 0x73, 0x20, 0x40, 0xF2, 0x7C, 0x40, 0x9B, 0xA4,
  29823. 0x9A, 0x80, 0xCF, 0x95, 0x55, 0x7C, 0x73, 0x7C,
  29824. 0x4A, 0x45, 0x11, 0x2C, 0x52, 0x2C, 0x78, 0x04,
  29825. 0xC7, 0x7A, 0x3D, 0x44, 0x53, 0xBA, 0x82, 0xC1,
  29826. 0x49, 0x08, 0xBC, 0xC0, 0x47, 0x13, 0x7E, 0x5A,
  29827. 0x7B, 0x23, 0x92, 0xC8, 0x31, 0x25, 0x4C, 0x1A,
  29828. 0x59, 0x54, 0xC8, 0x8C, 0xCF, 0xA5, 0xD8, 0x8B,
  29829. 0x3B, 0x93, 0x31, 0xBE, 0xF4, 0x62, 0x55, 0x09,
  29830. 0x14, 0xD1, 0x84, 0x17, 0x59, 0x40, 0xB4, 0xE9,
  29831. 0xB7, 0xAD, 0x3D, 0x44, 0x2E, 0xA9, 0x68, 0xB6,
  29832. 0xA4, 0x0A, 0x19, 0x72, 0x71, 0x5B, 0xCB, 0x72,
  29833. 0x82, 0x14, 0xA1, 0xBE, 0x8E, 0x04, 0x1D, 0xF5,
  29834. 0x72, 0xCC, 0x2D, 0x01, 0x6F, 0xFD, 0x6B, 0x0A,
  29835. 0xFA, 0xC8, 0x02, 0xBA, 0x97, 0x5A, 0x4A, 0x31,
  29836. 0xA4, 0x18, 0x40, 0x2E, 0x6B, 0x01, 0x41, 0xFA,
  29837. 0xD6, 0x48, 0x49, 0xE1, 0x6C, 0x80, 0x37, 0x23,
  29838. 0xA7, 0x74, 0x2B, 0x08, 0x2A, 0x7A, 0x56, 0x26,
  29839. 0x4E, 0x02, 0x60, 0xA2, 0x4A, 0x66, 0x42, 0x3D,
  29840. 0x52, 0x64, 0x82, 0x07, 0x04, 0x06, 0x5C, 0xAC,
  29841. 0x41, 0x10, 0x72, 0x48, 0x64, 0x37, 0xCA, 0xA5,
  29842. 0x47, 0x2D, 0x73, 0x3C, 0x64, 0x14, 0x5F, 0x10,
  29843. 0x0B, 0x0B, 0x16, 0xC0, 0xAB, 0x0D, 0xB8, 0x7C,
  29844. 0xC5, 0x64, 0x26, 0xC1, 0x1A, 0x69, 0x45, 0x25,
  29845. 0x95, 0x8D, 0xB8, 0x31, 0x59, 0x31, 0xC5, 0x09,
  29846. 0xD6, 0x3C, 0x48, 0x10, 0xBB, 0xA7, 0x68, 0xCA,
  29847. 0xCB, 0xF9, 0xCD, 0x85, 0x7B, 0x0D, 0x66, 0xD6,
  29848. 0x5A, 0xF7, 0xEB, 0xAC, 0x18, 0xB7, 0x9C, 0xBE,
  29849. 0xBC, 0x0A, 0x49, 0xB5, 0xC0, 0x5A, 0xA8, 0x7F,
  29850. 0x8D, 0xE8, 0x25, 0xA6, 0xA1, 0x13, 0x5F, 0xE8,
  29851. 0x64, 0xCF, 0x6C, 0x99, 0x8D, 0xDC, 0x37, 0xFC,
  29852. 0x01, 0x56, 0xBB, 0x52, 0xA7, 0x8E, 0xC2, 0x99,
  29853. 0xCA, 0x53, 0x1C, 0xF8, 0x75, 0xBE, 0xFA, 0x48,
  29854. 0x75, 0x72, 0x60, 0x28, 0xED, 0x37, 0x91, 0xBA,
  29855. 0x31, 0xC0, 0x0E, 0xC7, 0x40, 0x20, 0xCA, 0xA1,
  29856. 0x7F, 0x91, 0x19, 0x63, 0x96, 0xCF, 0xE5, 0x88,
  29857. 0x6E, 0x51, 0x1A, 0x52, 0xD0, 0xCA, 0x8D, 0xEE,
  29858. 0x25, 0xBF, 0x4E, 0x77, 0xCB, 0x92, 0xE6, 0x36,
  29859. 0x40, 0x64, 0x6D, 0xE2, 0x58, 0x6D, 0xCD, 0x00,
  29860. 0x14, 0x16, 0xCC, 0xC1, 0xEC, 0x30, 0x7C, 0xA2,
  29861. 0x75, 0x6D, 0xBE, 0xA8, 0x26, 0x36, 0x14, 0x51,
  29862. 0x3C, 0xA0, 0x5E, 0x70, 0xC7, 0x54, 0xF5, 0x6A,
  29863. 0x2C, 0x3F, 0xF7, 0x42, 0x94, 0x8C, 0x34, 0xAF,
  29864. 0x49, 0x34, 0x2F, 0x55, 0x03, 0x4C, 0x1C, 0x70,
  29865. 0xE0, 0x79, 0x6D, 0x26, 0xB2, 0x30, 0x35, 0x6B,
  29866. 0x1B, 0x74, 0xA1, 0x6C, 0x0B, 0x28, 0x37, 0x4B,
  29867. 0x79, 0xC1, 0x6B, 0x04, 0x96, 0xCA, 0x20, 0x12,
  29868. 0x3E, 0x6C, 0x67, 0x62, 0xD9, 0x01, 0xE2, 0x76,
  29869. 0x08, 0x5D, 0x81, 0x2A, 0xE9, 0xC3, 0x45, 0xAD,
  29870. 0xF5, 0x09, 0xDD, 0xC7, 0x87, 0x61, 0x62, 0xCE,
  29871. 0x34, 0xA6, 0x00, 0x3F, 0xB4, 0x5C, 0xD8, 0x66,
  29872. 0x0F, 0xDD, 0x46, 0x71, 0x6E, 0x82, 0x55, 0x06,
  29873. 0x62, 0x37, 0x5D, 0xB5, 0x5E, 0x63, 0xA2, 0x0F,
  29874. 0x01, 0x2A, 0x71, 0xE3, 0xD9, 0x72, 0x25, 0xC5,
  29875. 0x8F, 0x52, 0x34, 0xC6, 0xB5, 0x3A, 0x8A, 0xA0,
  29876. 0x33, 0x9C, 0xEF, 0xD4, 0x84, 0xE4, 0x7A, 0x3D,
  29877. 0xE6, 0x49, 0x88, 0x05, 0xC2, 0x82, 0xE0, 0x76,
  29878. 0x8E, 0x35, 0xC7, 0x5C, 0x20, 0x07, 0x76, 0x5C,
  29879. 0xA1, 0x7F, 0x01, 0xD2, 0x85, 0xA7, 0xC3, 0x04,
  29880. 0x8B, 0xC7, 0x2B, 0xA3, 0xAA, 0x57, 0x7C, 0x1C,
  29881. 0x43, 0x2A, 0x3C, 0x76, 0x9D, 0x21, 0xB8, 0x6C,
  29882. 0x01, 0x8F, 0x81, 0x7A, 0x26, 0x67, 0x35, 0x1C,
  29883. 0x2B, 0xC8, 0x5F, 0xA5, 0x53, 0x6F, 0xA7, 0xA6,
  29884. 0xCD, 0xEF, 0xCC, 0x0D, 0xD6, 0xF9, 0x1B, 0xDD,
  29885. 0xE8, 0x9D, 0x4E, 0x51, 0x6C, 0x8B, 0x75, 0x40,
  29886. 0xA7, 0xDC, 0x3F, 0xC4, 0x89, 0x8A, 0xB3, 0x30,
  29887. 0x08, 0xBC, 0xC1, 0x9F, 0x9E, 0x46, 0x3D, 0xE9,
  29888. 0x36, 0x26, 0x99, 0x4C, 0xC9, 0x27, 0xA3, 0x4A,
  29889. 0x9D, 0x70, 0x40, 0xC1, 0x31, 0xC8, 0x1F, 0x07,
  29890. 0x83, 0xDD, 0x68, 0x84, 0x29, 0x43, 0xC5, 0x5D,
  29891. 0xC5, 0x77, 0xD3, 0x67, 0xA4, 0x01, 0x83, 0x94,
  29892. 0xBB, 0x11, 0x60, 0x72, 0xAC, 0x65, 0xF2, 0xEB,
  29893. 0x9B, 0x4F, 0xF5, 0x8F, 0x41, 0x15, 0x40, 0x1B,
  29894. 0x11, 0x7D, 0x91, 0x3B, 0x75, 0x75, 0x57, 0x57,
  29895. 0x19, 0x39, 0x0C, 0x9E, 0x80, 0x06, 0x97, 0xA8,
  29896. 0x28, 0xB1, 0x25, 0x83, 0xF9, 0x4A, 0x2F, 0x8F,
  29897. 0x72, 0x0A, 0x6E, 0x87, 0x00, 0xFD, 0x40, 0x7D,
  29898. 0x73, 0xA4, 0xC7, 0x97, 0x55, 0x2B, 0x98, 0x75,
  29899. 0x7A, 0x5F, 0x57, 0x82, 0xA9, 0x50, 0x95, 0xDF,
  29900. 0xA5, 0xAC, 0xB7, 0xD9, 0x3A, 0x80, 0x5C, 0x30,
  29901. 0x18, 0x30, 0x4E, 0x5D, 0x22, 0xB7, 0x6F, 0xA5,
  29902. 0xAA, 0x51, 0x96, 0xB7, 0xEB, 0xDB, 0x7A, 0xA0,
  29903. 0xA5, 0x2D, 0x06, 0xAC, 0x4B, 0x9E, 0x4C, 0x00,
  29904. 0xC0, 0xF8, 0x5C, 0xEC, 0xB0, 0xBD, 0x95, 0x56,
  29905. 0x2D, 0x56, 0x23, 0x30, 0x78, 0x51, 0x43, 0xC7,
  29906. 0xF1, 0x29, 0xFA, 0xF9, 0x19, 0x48, 0xB0, 0xC8,
  29907. 0x8C, 0x60, 0x82, 0xE0, 0xF3, 0x03, 0x54, 0x74,
  29908. 0x12, 0x6E, 0x23, 0x6B, 0x80, 0xD2, 0x22, 0xD2,
  29909. 0x81, 0x4F, 0x6A, 0xF2, 0x4B, 0xD5, 0xD1, 0x19,
  29910. 0x86, 0x33, 0x1F, 0xAF, 0xC2, 0x1E, 0xC8, 0x4C,
  29911. 0x93, 0xA8, 0x81, 0x24, 0x72, 0x51, 0x10, 0x5A,
  29912. 0x6A, 0xB4, 0x35, 0xD7, 0x6E, 0xFD, 0x88, 0x9D,
  29913. 0x27, 0xC0, 0x0C, 0x2A, 0x14, 0xBD, 0x04, 0xA6,
  29914. 0xBD, 0x0B, 0x20, 0x07, 0xB5, 0x51, 0x5A, 0x75,
  29915. 0xF1, 0x14, 0xD1, 0x92, 0x10, 0xBC, 0x70, 0x50,
  29916. 0x85, 0x33, 0x53, 0x2F, 0x1B, 0x0A, 0x30, 0x0C,
  29917. 0x8B, 0x12, 0xF0, 0x0A, 0xC4, 0xFC, 0x46, 0x0F,
  29918. 0xA8, 0x4A, 0x2D, 0x15, 0x21, 0xBD, 0x0B, 0x42,
  29919. 0x88, 0xD9, 0x2D, 0xD1, 0x2B, 0x17, 0x3E, 0x28,
  29920. 0x8B, 0x8A, 0x30, 0xC4, 0x9C, 0xA3, 0x56, 0x21,
  29921. 0x3C, 0x22, 0x4A, 0x03, 0x64, 0xDC, 0xF1, 0x8F,
  29922. 0xF0, 0x70, 0x0E, 0xAE, 0xBB, 0xB6, 0xD2, 0x02,
  29923. 0x03, 0x71, 0x14, 0x5A, 0x8A, 0xE8, 0x79, 0x59,
  29924. 0x72, 0x2D, 0x34, 0xF3, 0x3F, 0xAE, 0x00, 0x57,
  29925. 0x2D, 0xB3, 0xAC, 0x9A, 0x7B, 0xBD, 0xD4, 0x0B,
  29926. 0x75, 0x3C, 0x57, 0xA7, 0x3B, 0xA1, 0xB5, 0x43,
  29927. 0x12, 0x8A, 0x20, 0xDC, 0x98, 0x31, 0x69, 0x39,
  29928. 0xFE, 0xB8, 0x14, 0xCB, 0x17, 0x6C, 0x41, 0x64,
  29929. 0x77, 0x01, 0x2A, 0x97, 0x23, 0x67, 0x83, 0x46,
  29930. 0x7C, 0x0E, 0x15, 0x97, 0x33, 0xC5, 0xA0, 0x31,
  29931. 0x3C, 0xB2, 0x78, 0xD2, 0x31, 0x41, 0x2C, 0xB0,
  29932. 0x42, 0xC8, 0xE1, 0x16, 0xD6, 0x65, 0xA3, 0x7E,
  29933. 0xA4, 0x86, 0xEE, 0x07, 0x75, 0x44, 0xFC, 0x09,
  29934. 0xD5, 0x71, 0x56, 0x46, 0x3C, 0x1B, 0x10, 0xDC,
  29935. 0x49, 0x41, 0x0A, 0x3C, 0x0F, 0xE4, 0xB2, 0xEF,
  29936. 0x57, 0x1F, 0xAA, 0x56, 0x7A, 0xB5, 0x01, 0x59,
  29937. 0x58, 0xDB, 0xCF, 0x2C, 0xFA, 0x2C, 0xA7, 0x33,
  29938. 0x96, 0x3C, 0x34, 0x45, 0x7F, 0x85, 0x99, 0xCA,
  29939. 0x3A, 0xC8, 0xD2, 0xCB, 0x99, 0xA4, 0x57, 0x11,
  29940. 0x4E, 0x55, 0x9A, 0x9F, 0x82, 0xC4, 0x28, 0x47,
  29941. 0x99, 0xDE, 0x66, 0x15, 0x0A, 0x26, 0x56, 0x8F,
  29942. 0x95, 0xAF, 0x70, 0x65, 0x78, 0x8E, 0x62, 0x86,
  29943. 0x84, 0x60, 0x8C, 0x74, 0xDA, 0x57, 0x17, 0xB8,
  29944. 0x1F, 0x5D, 0x5C, 0x2F, 0x7D, 0x1A, 0x6C, 0xC0,
  29945. 0x71, 0x7C, 0xAB, 0xD2, 0xBA, 0xD2, 0x49, 0xB0,
  29946. 0x87, 0x58, 0x8F, 0x75, 0x65, 0x23, 0xFD, 0xF0,
  29947. 0x4E, 0x7F, 0x31, 0x76, 0x1B, 0x01, 0xAC, 0xB7,
  29948. 0xF6, 0x7E, 0xBF, 0x87, 0x62, 0xF5, 0xCB, 0x0B,
  29949. 0xC3, 0x72, 0xB4, 0xDD, 0x08, 0x3B, 0x06, 0x66,
  29950. 0x74, 0x02, 0x20, 0x4D, 0x08, 0x73, 0x9E, 0xB7,
  29951. 0x95, 0x20, 0xA9, 0x2C, 0x87, 0xC7, 0xE2, 0x2C,
  29952. 0x78, 0x77, 0x97, 0x3A, 0x0B, 0x79, 0xB0, 0xC7,
  29953. 0xAC, 0xCB, 0xBB, 0xAF, 0x88, 0xFB, 0xB0, 0x3D,
  29954. 0x4B, 0x6F, 0x5B, 0xB9, 0x87, 0x8A, 0xAB, 0x81,
  29955. 0x02, 0xF6, 0xB9, 0x1A, 0x27, 0x00, 0xD0, 0xEA,
  29956. 0xC3, 0x1D, 0x26, 0x03, 0x5B, 0xC1, 0x8A, 0x66,
  29957. 0xCB, 0x90, 0xEA, 0x24, 0x3F, 0xCF, 0x1A, 0x9A,
  29958. 0x1D, 0x24, 0x0C, 0x29, 0x0A, 0x54, 0x82, 0xA1,
  29959. 0x55, 0xC8, 0x00, 0x91, 0x13, 0xA0, 0x82, 0xDA,
  29960. 0xD5, 0xCE, 0x5C, 0xD2, 0x53, 0x03, 0x37, 0x02,
  29961. 0x51, 0x59, 0x3C, 0x12, 0x11, 0xAA, 0xB6, 0xC8,
  29962. 0x44, 0xC2, 0xC4, 0xB7, 0x99, 0xD4, 0x92, 0x80,
  29963. 0x00, 0x74, 0x63, 0x89, 0x48, 0xCB, 0x64, 0x31,
  29964. 0x0F, 0x38, 0xB4, 0x88, 0x51, 0xA6, 0x9E, 0x22,
  29965. 0x28, 0x70, 0x3C, 0x97, 0xFC, 0x20, 0x4E, 0x54,
  29966. 0x37, 0x21, 0x79, 0xAA, 0x84, 0xDE, 0xC1, 0xAC,
  29967. 0x68, 0xE6, 0xBC, 0x26, 0x8B, 0x9E, 0x36, 0x1B,
  29968. 0x8F, 0x6E, 0x0A, 0xBC, 0xFE, 0x88, 0x93, 0x37,
  29969. 0x87, 0x53, 0x48, 0xE4, 0x16, 0x4D, 0x66, 0x15,
  29970. 0xF3, 0x82, 0xC6, 0xC7, 0xC4, 0x17, 0x8F, 0x75,
  29971. 0x1F, 0x72, 0x2B, 0x21, 0x27, 0x74, 0x64, 0xFB,
  29972. 0x83, 0x1F, 0x8A, 0xA9, 0xA0, 0x18, 0xC1, 0x9A,
  29973. 0x51, 0xB0, 0x7F, 0xA0, 0x93, 0x9B, 0xA5, 0xC2,
  29974. 0x06, 0x68, 0x63, 0x23, 0xAF, 0xF6, 0xCA, 0x83,
  29975. 0x0B, 0x76, 0xE3, 0x80, 0x02, 0x63, 0x54, 0x47,
  29976. 0x7A, 0xAC, 0x58, 0xD8, 0x93, 0x1D, 0x80, 0x3B,
  29977. 0x6F, 0xAB, 0xD7, 0x12, 0xD1, 0x72, 0x81, 0x4A,
  29978. 0x54, 0xB8, 0x12, 0xE7, 0x92, 0x47, 0x94, 0xAA,
  29979. 0x40, 0x02, 0x49, 0xD5, 0xC0, 0x46, 0x8E, 0xA3,
  29980. 0x2F, 0xB2, 0x15, 0x54, 0xC5, 0x93, 0x67, 0x90,
  29981. 0x3C, 0x60, 0xA8, 0xB4, 0x8E, 0x23, 0x40, 0x71,
  29982. 0x2F, 0x7A, 0x04, 0x5A, 0x67, 0xC2, 0x3C, 0x72,
  29983. 0x41, 0x3A, 0x64, 0x69, 0xFC, 0x83, 0x69, 0xA2,
  29984. 0x91, 0x03, 0x3C, 0x46, 0xCE, 0xA3, 0x96, 0x76,
  29985. 0x65, 0x6B, 0x6C, 0xD8, 0x57, 0x99, 0x7C, 0x04,
  29986. 0x5C, 0x5A, 0xF0, 0x34, 0xE1, 0xE1, 0x14, 0x8A,
  29987. 0x8B, 0x40, 0xCD, 0x16, 0x68, 0x31, 0x93, 0xA3,
  29988. 0x60, 0x0C, 0x84, 0xFC, 0xE3, 0xB9, 0x65, 0x10,
  29989. 0x88, 0xDD, 0x09, 0x44, 0x57, 0x6C, 0x7E, 0xA4,
  29990. 0x07, 0x2D, 0x25, 0xF0, 0x67, 0xD2, 0x42, 0x7E,
  29991. 0x26, 0xD1, 0x86, 0xF9, 0xB7, 0x7A, 0x32, 0x79,
  29992. 0x05, 0x03, 0x3B, 0xA9, 0x40, 0x85, 0xA8, 0xAB,
  29993. 0x6A, 0x8A, 0x2B, 0x86, 0x08, 0x0C, 0x45, 0x2B,
  29994. 0x10, 0x90, 0xAC, 0xC9, 0x5A, 0x2B, 0x9F, 0x5A,
  29995. 0x6E, 0x61, 0x84, 0x6D, 0x63, 0x45, 0x66, 0xFA,
  29996. 0xC0, 0xB0, 0xFF, 0x35, 0x11, 0x18, 0x06, 0x18,
  29997. 0xAF, 0x98, 0x34, 0x87, 0x12, 0x2C, 0xFB, 0x63,
  29998. 0x61, 0xFF, 0x66, 0x0D, 0x5C, 0xA0, 0x0B, 0x9D,
  29999. 0x88, 0xB1, 0xDA, 0x18, 0x36, 0xC9, 0x78, 0x6E,
  30000. 0x55, 0x27, 0x6C, 0x88, 0x53, 0x8C, 0xC1, 0xD3,
  30001. 0x05, 0xFA, 0xAA, 0xB7, 0x79, 0x9A, 0x35, 0x46,
  30002. 0xF0, 0xBE, 0x6C, 0xC7, 0x00, 0xB0, 0x65, 0xB9,
  30003. 0x03, 0x4A, 0x91, 0x63, 0x31, 0x74, 0x66, 0xD9,
  30004. 0x83, 0xDD, 0xC2, 0xAA, 0x5B, 0xB0, 0x3F, 0x5B,
  30005. 0x67, 0x33, 0x48, 0xD5, 0x76, 0x02, 0x8A, 0x02,
  30006. 0x55, 0x15, 0x6C, 0x6A, 0x29, 0x74, 0x46, 0x98,
  30007. 0x34, 0xFB, 0x15, 0x73, 0x23, 0x74, 0x60, 0x54,
  30008. 0xC4, 0xBA, 0x96, 0x40, 0x30, 0x4A, 0x11, 0x63,
  30009. 0x04, 0xF0, 0x18, 0x21, 0x72, 0xBA, 0x8D, 0x03,
  30010. 0xAE, 0xDA, 0x39, 0x91, 0x6A, 0xF1, 0x18, 0xB9,
  30011. 0x84, 0x22, 0xB7, 0x16, 0x3A, 0xB6, 0xE2, 0x47,
  30012. 0xFC, 0xAA, 0x9D, 0x7C, 0x7C, 0xAE, 0x96, 0xBB,
  30013. 0x60, 0xB1, 0xD7, 0x86, 0x0E, 0x4C, 0x7F, 0x1D,
  30014. 0x92, 0x0D, 0x31, 0xA5, 0x41, 0x38, 0x82, 0x29,
  30015. 0xEE, 0x84, 0x2C, 0xA1, 0x59, 0x43, 0x89, 0x4A,
  30016. 0x56, 0xE5, 0x14, 0x0D, 0x29, 0x59, 0xC3, 0x6E,
  30017. 0xA2, 0x6E, 0x2D, 0xE0, 0x51, 0x21, 0x36, 0x6B,
  30018. 0xE4, 0x14, 0x02, 0x7B, 0xD1, 0x47, 0x81, 0x47,
  30019. 0x2E, 0xDD, 0xA4, 0x71, 0x10, 0xD0, 0xB3, 0xAE,
  30020. 0x2A, 0xA2, 0x55, 0xAB, 0x6D, 0x6F, 0xE0, 0x1C,
  30021. 0x26, 0xC2, 0x87, 0x63, 0x97, 0x6B, 0xBF, 0xA9,
  30022. 0x2F, 0x4F, 0x09, 0xA5, 0xB7, 0x3C, 0x47, 0x1F,
  30023. 0x8C, 0x9E, 0x53, 0x26, 0x92, 0x55, 0xDA, 0xC1,
  30024. 0xE0, 0x71, 0x5E, 0x87, 0x00, 0x06, 0x09, 0x70,
  30025. 0x60, 0xFF, 0x23, 0xC3, 0xAB, 0x43, 0xC4, 0x19,
  30026. 0xE4, 0x48, 0xC7, 0x30, 0x40, 0x10, 0xC4, 0x0F,
  30027. 0x3D, 0xC0, 0xB7, 0x26, 0x39, 0x2E, 0x46, 0x03,
  30028. 0x7E, 0xB9, 0x49, 0x04, 0x55, 0xE3, 0x49, 0xA4,
  30029. 0x35, 0xCD, 0xA9, 0x0A, 0xA3, 0xB7, 0x47, 0x8C,
  30030. 0x32, 0xE8, 0x72, 0xB2, 0xB8, 0x5C, 0xEC, 0xF0,
  30031. 0x3D, 0xDC, 0xD6, 0x16, 0x33, 0x78, 0xBF, 0x73,
  30032. 0xB1, 0x3B, 0x2E, 0x1A, 0x7B, 0x6E, 0xBC, 0x39,
  30033. 0x29, 0xEC, 0x08, 0x63, 0x89, 0x3F, 0x73, 0x8C,
  30034. 0x38, 0x9F, 0x3B, 0x3B, 0x2B, 0xB5, 0x4F, 0xC1,
  30035. 0x5B, 0xB7, 0x8A, 0x25, 0x31, 0xEE, 0xBC, 0xB7,
  30036. 0xBC, 0x10, 0x81, 0x07, 0x96, 0x59, 0x81, 0xD2,
  30037. 0x0A, 0x7D, 0x1A, 0x87, 0x21, 0xBA, 0x36, 0x40,
  30038. 0x03, 0x13, 0x2F, 0x03, 0x6C, 0xE4, 0x0C, 0x9E,
  30039. 0x75, 0x5B, 0x2A, 0xB4, 0x86, 0x1D, 0x04, 0xC1,
  30040. 0xBE, 0x0D, 0x53, 0x97, 0x91, 0x4B, 0x6C, 0xD4,
  30041. 0xAA, 0x40, 0x8C, 0x62, 0x2B, 0x21, 0xB1, 0x1B,
  30042. 0xA0, 0xEB, 0xA6, 0x17, 0x29, 0x59, 0x6F, 0x8B,
  30043. 0x0E, 0x82, 0x14, 0xAA, 0x4A, 0xA6, 0x01, 0x86,
  30044. 0x51, 0x07, 0xA2, 0xA7, 0x6A, 0xD7, 0xCC, 0x2F,
  30045. 0x97, 0x43, 0x00, 0x18, 0x28, 0xB8, 0x0B, 0x24,
  30046. 0x80, 0x2B, 0x98, 0x9D, 0x98, 0x42, 0x5E, 0x14,
  30047. 0x7C, 0x6D, 0x93, 0x32, 0xBA, 0x08, 0x4B, 0x8F,
  30048. 0xFB, 0x96, 0x27, 0xE3, 0xFB, 0xC4, 0xBE, 0x65,
  30049. 0x38, 0xEB, 0xDA, 0x1D, 0x9D, 0xB9, 0xB6, 0x75,
  30050. 0xB0, 0x16, 0x66, 0x57, 0x23, 0xD4, 0x11, 0x71,
  30051. 0x54, 0xF8, 0x13, 0x3E, 0xA9, 0x00, 0xCB, 0x47,
  30052. 0x0B, 0x1D, 0xC5, 0xB5, 0x7A, 0x78, 0x11, 0x16,
  30053. 0x72, 0x9E, 0xD6, 0x29, 0x45, 0x6E, 0x1A, 0x7D,
  30054. 0x6A, 0x37, 0x9F, 0x2E, 0x01, 0x0A, 0xCB, 0xEC,
  30055. 0x21, 0xFC, 0xD1, 0xC2, 0xB0, 0xBB, 0x61, 0xC3,
  30056. 0xDA, 0x6C, 0x73, 0x70, 0x5F, 0xD7, 0xB0, 0xC3,
  30057. 0x61, 0x2A, 0xC3, 0xB4, 0xA1, 0x2E, 0xC5, 0xD3,
  30058. 0x7F, 0x4C, 0x1A, 0x4A, 0x4D, 0xF8, 0x16, 0x6E,
  30059. 0xF6, 0x68, 0x9C, 0x06, 0xB5, 0xD4, 0xA6, 0x1A,
  30060. 0x52, 0x14, 0xAE, 0x5E, 0x5C, 0x28, 0x07, 0x4C,
  30061. 0xC5, 0xFE, 0x2A, 0x41, 0x2A, 0xC5, 0x14, 0x15,
  30062. 0x94, 0x28, 0x54, 0x85, 0x15, 0x6E, 0xCA, 0x65,
  30063. 0x64, 0x28, 0x37, 0x20, 0xB0, 0xBA, 0xD1, 0xD9,
  30064. 0x74, 0x5B, 0xA5, 0x77, 0x71, 0x1A, 0xB6, 0xF2,
  30065. 0x14, 0xC5, 0x2F, 0xB5, 0xBA, 0xAF, 0x8A, 0x1F,
  30066. 0x85, 0x33, 0x5A, 0xEB, 0x44, 0x37, 0x0B, 0x39,
  30067. 0xC5, 0x6C, 0x4C, 0x3D, 0x32, 0x75, 0x55, 0x60,
  30068. 0xCB, 0x2C, 0xA9, 0x45, 0x87, 0x51, 0xF1, 0x65,
  30069. 0x4B, 0x82, 0xC5, 0xE7, 0xFB, 0x0D, 0xB5, 0x16,
  30070. 0xBE, 0x1E, 0x42, 0x43, 0x3A, 0xD4, 0x7B, 0x6B,
  30071. 0xCC, 0xB3, 0x63, 0x61, 0xC4, 0x34, 0xEB, 0x59,
  30072. 0xCF, 0xF3, 0x38, 0x49, 0x54, 0xBB, 0x76, 0xF1,
  30073. 0x44, 0x14, 0x91, 0x67, 0x91, 0x3B, 0x85, 0x0F,
  30074. 0x75, 0x28, 0xBD, 0x76, 0x87, 0xB8, 0x8B, 0x64,
  30075. 0x84, 0xA6, 0x82, 0x68, 0x39, 0x97, 0x08, 0x42,
  30076. 0xA5, 0x93, 0xBB, 0x93, 0x45, 0x5A, 0x73, 0xBC,
  30077. 0x2C, 0x74, 0x62, 0x55, 0xC1, 0x3B, 0xC8, 0x52,
  30078. 0x1C, 0x73, 0x54, 0xE9, 0x8B, 0x98, 0x76, 0xA9,
  30079. 0x58, 0x6F, 0xC3, 0x7C, 0x5F, 0x24, 0x8D, 0xA0,
  30080. 0x55, 0x75, 0x8B, 0xD9, 0x61, 0x5E, 0x68, 0xBF,
  30081. 0x93, 0xEA, 0x0B, 0xA1, 0xD0, 0x22, 0xE2, 0xD2,
  30082. 0x22, 0x04, 0xA4, 0x45, 0x8C, 0xB6, 0x12, 0xB0,
  30083. 0x29, 0x41, 0xE8, 0x7A, 0xAC, 0xF1, 0xA3, 0x29,
  30084. 0x07, 0xD9, 0x8B, 0x4B, 0x13, 0xC6, 0xC9, 0xB4,
  30085. 0xAB, 0xD6, 0xDC, 0xCF, 0x66, 0x21, 0x06, 0xF5,
  30086. 0xD5, 0x8C, 0x2F, 0xC7, 0x51, 0x97, 0x35, 0x5E,
  30087. 0xB8, 0xCC, 0x41, 0x74, 0x2C, 0x6D, 0xA0, 0x03,
  30088. 0x40, 0x6D, 0x13, 0x56, 0xF8, 0xA0, 0x7A, 0x37,
  30089. 0x97, 0x88, 0x00, 0x60, 0x67, 0xCB, 0xF5, 0x2F,
  30090. 0x22, 0xD5, 0x3D, 0x68, 0x71, 0x22, 0x41, 0x09,
  30091. 0x34, 0x6E, 0xA8, 0x3B, 0x45, 0xCA, 0x60, 0xAF,
  30092. 0xEC, 0xCA, 0x0F, 0xA5, 0x67, 0x2B, 0x36, 0x64,
  30093. 0x89, 0xEB, 0x8D, 0xD1, 0xF7, 0x20, 0x4C, 0xDB,
  30094. 0x7D, 0x9B, 0xE1, 0x87, 0xF4, 0x39, 0x7B, 0x9B,
  30095. 0x4C, 0xA0, 0x31, 0x34, 0x75, 0x0D, 0xD5, 0x7D,
  30096. 0xAD, 0xE1, 0x88, 0x91, 0xCC, 0xA9, 0x47, 0x4B,
  30097. 0x65, 0x32, 0x60, 0x52, 0x50, 0xBC, 0xBB, 0x6E,
  30098. 0x02, 0x94, 0xEF, 0x95, 0x32, 0x6D, 0x25, 0x9B,
  30099. 0x08, 0xB2, 0x93, 0x8D, 0x8C, 0x59, 0xE2, 0x58,
  30100. 0x4B, 0x69, 0x10, 0x71, 0x85, 0xF3, 0x45, 0x30,
  30101. 0x24, 0x07, 0x35, 0x17, 0xC9, 0x89, 0x00, 0x9E,
  30102. 0xBB, 0x6B, 0x22, 0xED, 0xE9, 0x70, 0x43, 0x66,
  30103. 0xC6, 0x23, 0x2A, 0x62, 0x82, 0xF3, 0xBB, 0x09,
  30104. 0x08, 0x74, 0xA6, 0xFB, 0x0D, 0x84, 0x53, 0x6D,
  30105. 0x3F, 0x61, 0x3A, 0x21, 0xD1, 0x9D, 0x98, 0x44,
  30106. 0x17, 0xFA, 0xF0, 0x84, 0x6C, 0xB4, 0x28, 0x7B,
  30107. 0x3C, 0x70, 0x9F, 0x22, 0x62, 0x26, 0xA0, 0x9E,
  30108. 0x94, 0xFA, 0x89, 0x85, 0x43, 0x87, 0x4A, 0xE2,
  30109. 0x08, 0x11, 0xE3, 0x90, 0x80, 0xE6, 0xB2, 0xCE,
  30110. 0x49, 0x92, 0x26, 0xF5, 0x13, 0x8C, 0x14, 0x38,
  30111. 0x72, 0xDB, 0x86, 0xFB, 0xC2, 0x9C, 0xFD, 0xA2,
  30112. 0x9F, 0xD2, 0x26, 0x0E, 0xC7, 0xB9, 0xA2, 0x37,
  30113. 0xD1, 0x9A, 0xAB, 0x1B, 0x93, 0xE1, 0xDA, 0x4E,
  30114. 0xBC, 0x58, 0x9D, 0x82, 0x4C, 0x58, 0x90, 0x96,
  30115. 0x99, 0xD3, 0xE8, 0xBC, 0x7F, 0x53, 0xB3, 0x55,
  30116. 0xFA, 0x31, 0xE9, 0x6B, 0x1E, 0x35, 0x4A, 0xA4,
  30117. 0xB3, 0xC8, 0x4D, 0xF3, 0xC4, 0x7F, 0x80, 0xF9,
  30118. 0x32, 0xEA, 0x68, 0x5D, 0x96, 0xC5, 0xA5, 0x96,
  30119. 0x08, 0x22, 0xC6, 0x43, 0x64, 0x5F, 0x1A, 0x63,
  30120. 0xDD, 0x60, 0x8A, 0x09, 0x71, 0xC7, 0xF2, 0x15,
  30121. 0x7C, 0xA5, 0x24, 0x4D, 0x49, 0x2C, 0x01, 0x0B,
  30122. 0x71, 0xB7, 0x74, 0xC1, 0x0C, 0x07, 0xD3, 0xC8,
  30123. 0x4C, 0xA5, 0x4D, 0xE5, 0x00, 0x1F, 0xE0, 0xAC,
  30124. 0x4E, 0x69, 0x52, 0x81, 0x4E, 0xC1, 0x0F, 0xF9,
  30125. 0x8B, 0x19, 0xE7, 0x89, 0xC1, 0xAC, 0x68, 0x45,
  30126. 0xC7, 0x62, 0x80, 0x55, 0x27, 0x5C, 0x33, 0x34,
  30127. 0x41, 0x87, 0x1B, 0x8C, 0x30, 0x99, 0x9B, 0x05,
  30128. 0x31, 0x34, 0xC0, 0xE7, 0xB1, 0xAC, 0x6A, 0xA6,
  30129. 0x63, 0x12, 0xC1, 0xA5, 0xE2, 0x97, 0x92, 0x97,
  30130. 0x02, 0x56, 0x88, 0x14, 0x38, 0xDC, 0x46, 0x29,
  30131. 0x37, 0x4D, 0xFB, 0x9A, 0x91, 0x48, 0x62, 0x9D,
  30132. 0x14, 0x8C, 0x60, 0x1D, 0x4B, 0x6A, 0xA2, 0xC2,
  30133. 0x9C, 0xCE, 0xE8, 0x94, 0x7A, 0x27, 0x87, 0x0E,
  30134. 0x4C, 0x65, 0xE4, 0x12, 0x46, 0xD6, 0x25, 0x3D,
  30135. 0xC4, 0xF1, 0x3A, 0xEB, 0x31, 0xCD, 0x7E, 0x36,
  30136. 0x2F, 0x0F, 0x28, 0x84, 0xB9, 0xF7, 0x3B, 0xA9,
  30137. 0x93, 0x50, 0xAA, 0xA4, 0x02, 0x4A, 0x73, 0x14,
  30138. 0xBE, 0x69, 0x3A, 0xE4, 0x13, 0xA5, 0xCE, 0xA0,
  30139. 0x9A, 0x27, 0x27, 0x3A, 0x0B, 0xF6, 0xA4, 0x53,
  30140. 0xF8, 0x63, 0x9B, 0x48, 0x26, 0x8D, 0x21, 0xC9,
  30141. 0xAE, 0xC2, 0x62, 0xBE, 0x28, 0xC0, 0x7E, 0xA4,
  30142. 0xB9, 0x35, 0xA8, 0x69, 0x63, 0x26, 0xAA, 0x18,
  30143. 0xA5, 0x91, 0xCE, 0xE9, 0xC9, 0xF2, 0xA7, 0x29,
  30144. 0xBC, 0x50, 0x33, 0xDD, 0xE6, 0x37, 0x27, 0x38,
  30145. 0x85, 0x92, 0x1B, 0x73, 0xB8, 0x58, 0xA1, 0x88,
  30146. 0x33, 0xA7, 0xB8, 0x3A, 0x82, 0xFE, 0x02, 0xCC,
  30147. 0x33, 0x54, 0xC3, 0x0B, 0x0C, 0x4D, 0x0D, 0x3B,
  30148. 0xA1, 0x33, 0x1C, 0x6E, 0xC7, 0xA7, 0x0B, 0x7E,
  30149. 0x50, 0x3F, 0x76, 0x47, 0xA9, 0x52, 0xB0, 0x03,
  30150. 0x86, 0xCC, 0x90, 0xB3, 0x4A, 0x51, 0x4F, 0x99,
  30151. 0x5A, 0x56, 0x92, 0x05, 0xEB, 0x2A, 0x3F, 0xA7,
  30152. 0x9A, 0x78, 0x65, 0x21, 0x18, 0xD4, 0xBC, 0x48,
  30153. 0x46, 0x27, 0xCD, 0x02, 0x33, 0xBC, 0x5A, 0x35,
  30154. 0x16, 0x15, 0x0B, 0xAF, 0x6E, 0x10, 0x66, 0x5D,
  30155. 0xA7, 0x61, 0x8F, 0x28, 0x41, 0xB5, 0x02, 0x61,
  30156. 0x72, 0x48, 0x89, 0x3B, 0xAA, 0xB7, 0xE9, 0x03,
  30157. 0x89, 0x0D, 0xF0, 0xCA, 0xC2, 0x84, 0xCD, 0x11,
  30158. 0x65, 0xB4, 0x64, 0xC1, 0x8F, 0x4C, 0xFA, 0x8F,
  30159. 0x69, 0x2D, 0x3C, 0x8E, 0x8C, 0x73, 0x9F, 0xB3,
  30160. 0x78, 0x27, 0x74, 0x15, 0x61, 0xD5, 0xE7, 0x66,
  30161. 0x67, 0x51, 0x82, 0xFF, 0x83, 0xC9, 0x1C, 0xF0,
  30162. 0x33, 0x65, 0x88, 0x23, 0xD8, 0xFA, 0x91, 0xE1,
  30163. 0xE8, 0xD6, 0x62, 0xEA, 0xD3, 0x75, 0x0D, 0x71,
  30164. 0x6B, 0x91, 0x8D, 0x26, 0x78, 0x26, 0x59, 0xA5,
  30165. 0xB3, 0xD7, 0x99, 0xE4, 0x26, 0x58, 0xE1, 0x49,
  30166. 0x5F, 0x90, 0x84, 0xFF, 0xD4, 0xD1, 0x00, 0xB8,
  30167. 0x86, 0x26, 0xED, 0x79, 0xD4, 0x51, 0x14, 0x08,
  30168. 0x00, 0xE0, 0x3B, 0x59, 0xB9, 0x56, 0xF8, 0x21,
  30169. 0x0E, 0x55, 0x60, 0x67, 0x40, 0x7D, 0x13, 0xDC,
  30170. 0x90, 0xFA, 0x9E, 0x8B, 0x87, 0x2B, 0xFB, 0x8F
  30171. };
  30172. const byte kyber1024_ct[] = {
  30173. 0x17, 0x50, 0x59, 0x78, 0xD5, 0xCA, 0x04, 0x62,
  30174. 0x81, 0xC9, 0x7C, 0xA1, 0xFE, 0x89, 0xBA, 0xB3,
  30175. 0x99, 0xC1, 0xC9, 0xF8, 0xED, 0x27, 0x1D, 0xC5,
  30176. 0x1B, 0x30, 0x09, 0x43, 0xDB, 0x2E, 0xA9, 0x49,
  30177. 0x08, 0xEE, 0x94, 0x64, 0x61, 0x96, 0x02, 0xA7,
  30178. 0xF8, 0xCD, 0x38, 0xB5, 0xA3, 0x12, 0x5F, 0x46,
  30179. 0x7B, 0xF1, 0xA7, 0xA9, 0x67, 0xEE, 0xE0, 0x84,
  30180. 0x20, 0x98, 0x80, 0x14, 0x33, 0x88, 0x38, 0x92,
  30181. 0xAD, 0x0A, 0xD2, 0x99, 0x6F, 0x76, 0xCE, 0x49,
  30182. 0xA1, 0x79, 0x0A, 0x7A, 0x23, 0x0E, 0xB0, 0xDA,
  30183. 0x07, 0x72, 0xC6, 0xFC, 0x06, 0x0E, 0x75, 0x51,
  30184. 0x8C, 0x94, 0xA1, 0xA6, 0x52, 0xD8, 0x66, 0x0F,
  30185. 0xBA, 0x70, 0x1E, 0xF4, 0x82, 0xF4, 0xFA, 0x26,
  30186. 0xA7, 0xC4, 0xDB, 0x86, 0x02, 0xB7, 0x89, 0x49,
  30187. 0xB2, 0x8D, 0x99, 0xC7, 0x3B, 0x84, 0xD5, 0x5C,
  30188. 0xF6, 0x80, 0xA5, 0x90, 0x89, 0x3C, 0x63, 0xEA,
  30189. 0x8F, 0x85, 0x45, 0x8B, 0x85, 0xF7, 0x39, 0x2C,
  30190. 0x11, 0xB8, 0x90, 0x02, 0x02, 0x73, 0xB7, 0x48,
  30191. 0x33, 0xA8, 0x0C, 0xD6, 0xC7, 0x8C, 0x75, 0x51,
  30192. 0xD7, 0xCC, 0x8A, 0xA1, 0xB4, 0xC5, 0x42, 0x8D,
  30193. 0x2A, 0xA3, 0xE7, 0x43, 0x5E, 0x23, 0xDD, 0x9B,
  30194. 0x4F, 0xFA, 0xB0, 0xD0, 0x21, 0x90, 0xBB, 0xA1,
  30195. 0x36, 0x89, 0xF0, 0x15, 0xC1, 0x9A, 0x3A, 0xD9,
  30196. 0xD6, 0x62, 0x69, 0x77, 0xD1, 0xA3, 0x35, 0x58,
  30197. 0xAB, 0x6C, 0xF4, 0x6F, 0x76, 0xBD, 0x5A, 0x30,
  30198. 0xED, 0x94, 0x6D, 0x04, 0x03, 0x38, 0x8B, 0x13,
  30199. 0xA5, 0xB4, 0x95, 0x83, 0xCF, 0xB9, 0x3D, 0xEB,
  30200. 0x30, 0xC4, 0x63, 0x82, 0xAF, 0xC1, 0x28, 0x62,
  30201. 0xF0, 0x69, 0xF1, 0x36, 0xD5, 0xCF, 0x5F, 0x30,
  30202. 0x06, 0x4D, 0x90, 0xDE, 0x12, 0xF7, 0xD7, 0x6E,
  30203. 0xD7, 0xF4, 0xA5, 0x93, 0x13, 0x9C, 0xB6, 0x77,
  30204. 0x93, 0xF2, 0xAD, 0x94, 0xDB, 0xE1, 0xBA, 0xD2,
  30205. 0x3B, 0xC1, 0x7C, 0x72, 0x66, 0x9C, 0x25, 0x14,
  30206. 0x9A, 0xC8, 0xA2, 0x5C, 0x50, 0xE0, 0xE0, 0x8F,
  30207. 0x5D, 0x76, 0x4B, 0x30, 0xF2, 0x87, 0x07, 0x47,
  30208. 0x0C, 0x5E, 0xC0, 0x9E, 0xDD, 0x21, 0x4C, 0x8A,
  30209. 0x0A, 0x82, 0x8C, 0x2C, 0x8B, 0x6F, 0xCF, 0x74,
  30210. 0xE9, 0x47, 0x36, 0x62, 0xFC, 0x61, 0xC7, 0x28,
  30211. 0x1E, 0x9B, 0xA7, 0x23, 0x04, 0x6A, 0x0A, 0xE7,
  30212. 0x0D, 0x7F, 0xEB, 0x86, 0xFC, 0xFF, 0x4D, 0x87,
  30213. 0x6B, 0x10, 0xEF, 0x2B, 0x67, 0x73, 0xE2, 0x6C,
  30214. 0xB2, 0x52, 0xDF, 0x75, 0x73, 0x07, 0x68, 0x0E,
  30215. 0xCC, 0x47, 0xD8, 0xEC, 0x07, 0xB6, 0xA5, 0x09,
  30216. 0xE2, 0x74, 0x51, 0x31, 0x77, 0xC6, 0xEE, 0x1B,
  30217. 0x91, 0x7B, 0x0C, 0x3E, 0x77, 0xCC, 0xF6, 0x2F,
  30218. 0x41, 0x2A, 0x95, 0x69, 0x57, 0x3B, 0xEB, 0xA1,
  30219. 0x54, 0xB1, 0x51, 0x47, 0xEF, 0x2E, 0x52, 0xA3,
  30220. 0x9C, 0xD0, 0xC6, 0x4B, 0xFD, 0x81, 0xEA, 0xA2,
  30221. 0x1B, 0x50, 0xAB, 0x9A, 0x11, 0x56, 0xBD, 0x41,
  30222. 0xDD, 0xB9, 0x93, 0x87, 0x24, 0xD1, 0x71, 0x39,
  30223. 0x39, 0x3F, 0x2D, 0x36, 0x1A, 0xAE, 0xE6, 0x76,
  30224. 0xB7, 0xF5, 0xA2, 0xBB, 0x45, 0x6B, 0xB5, 0x9C,
  30225. 0xF0, 0x4A, 0xC1, 0x7F, 0x0B, 0xD3, 0x14, 0x83,
  30226. 0xCD, 0x45, 0xC7, 0x5B, 0xF7, 0xD5, 0xD2, 0x7D,
  30227. 0x9A, 0x3A, 0x5D, 0x4E, 0x49, 0x1F, 0x1E, 0x3C,
  30228. 0x73, 0xE9, 0xD5, 0xC3, 0x46, 0x02, 0x3F, 0x5E,
  30229. 0xAC, 0x39, 0x1B, 0xBE, 0xE3, 0x2A, 0xD8, 0x01,
  30230. 0xA5, 0x9D, 0xFC, 0x03, 0x58, 0x26, 0x51, 0x11,
  30231. 0xE0, 0x3D, 0xD6, 0x01, 0xA8, 0x66, 0x5C, 0xE3,
  30232. 0xEC, 0xE5, 0x23, 0x1F, 0x1F, 0x5F, 0x5B, 0x47,
  30233. 0xEF, 0x38, 0x1B, 0x47, 0xBC, 0x18, 0xE4, 0xDE,
  30234. 0x73, 0xD0, 0xD5, 0x68, 0x10, 0x91, 0x33, 0x5E,
  30235. 0x8A, 0xF7, 0xE1, 0x2C, 0x0E, 0xA8, 0x92, 0x24,
  30236. 0xA2, 0x0F, 0x5F, 0x18, 0x66, 0x64, 0x31, 0x9B,
  30237. 0xFB, 0x55, 0x5F, 0x5F, 0x5D, 0x07, 0x41, 0x33,
  30238. 0x77, 0xB3, 0x41, 0x6F, 0x91, 0x35, 0x66, 0xB8,
  30239. 0xFF, 0x31, 0x48, 0x11, 0xD3, 0x3D, 0x42, 0xE4,
  30240. 0xFE, 0xE5, 0x17, 0xF3, 0xD2, 0xB9, 0x82, 0x8F,
  30241. 0x76, 0x81, 0x18, 0x03, 0x67, 0x3E, 0x86, 0x5A,
  30242. 0xFB, 0xA6, 0x64, 0xB7, 0x5D, 0x4A, 0x8B, 0xC7,
  30243. 0xA1, 0x36, 0xA6, 0x8C, 0x81, 0x39, 0x1E, 0x43,
  30244. 0x20, 0x87, 0xDE, 0x28, 0x51, 0xE5, 0xCB, 0x26,
  30245. 0xBF, 0xF6, 0x5D, 0x70, 0xBC, 0x0C, 0xDC, 0x35,
  30246. 0x9F, 0xE5, 0xFA, 0x0A, 0x45, 0x94, 0x80, 0x9C,
  30247. 0xFC, 0x0F, 0xC6, 0x4F, 0x0A, 0x46, 0x98, 0x2E,
  30248. 0xA1, 0xEA, 0x78, 0xA1, 0x8E, 0x1D, 0xCE, 0x00,
  30249. 0xCD, 0x2F, 0xE0, 0x7D, 0xC4, 0xE6, 0xC7, 0x04,
  30250. 0x50, 0x4B, 0x08, 0x33, 0xAA, 0x16, 0xD3, 0xB1,
  30251. 0x5D, 0xA5, 0x68, 0x26, 0x35, 0x70, 0x46, 0x66,
  30252. 0x0D, 0x47, 0x2C, 0x66, 0xAB, 0xEB, 0xD7, 0xFD,
  30253. 0x48, 0xFD, 0x62, 0x0D, 0x37, 0x9F, 0x2A, 0xEF,
  30254. 0x93, 0x99, 0x3B, 0xB8, 0xAA, 0x56, 0x7D, 0x9A,
  30255. 0x5A, 0x12, 0x94, 0xB4, 0x69, 0x80, 0xC0, 0x02,
  30256. 0x3B, 0xB8, 0x94, 0xD3, 0x70, 0x94, 0xF2, 0x95,
  30257. 0x6C, 0x8D, 0x1D, 0x8F, 0xC3, 0xF1, 0x98, 0x09,
  30258. 0x93, 0x54, 0x7E, 0xA4, 0x8F, 0xD4, 0xCB, 0xDB,
  30259. 0xD0, 0x97, 0xB3, 0xEE, 0xFA, 0xF8, 0x65, 0x75,
  30260. 0xEB, 0xBD, 0x23, 0xDB, 0xB2, 0x63, 0x04, 0xA0,
  30261. 0x9B, 0x1D, 0xE0, 0x55, 0xB7, 0x8A, 0x5F, 0xC4,
  30262. 0xBD, 0x41, 0x13, 0x27, 0x41, 0xD3, 0x87, 0xAC,
  30263. 0x3A, 0x58, 0x22, 0x19, 0xFE, 0x39, 0xC5, 0x33,
  30264. 0x2F, 0x8A, 0xD8, 0x8E, 0xB4, 0x9F, 0x9E, 0xEA,
  30265. 0xCB, 0x7C, 0xD7, 0xD5, 0x34, 0x47, 0xAB, 0x62,
  30266. 0x48, 0xBA, 0xA8, 0x22, 0x7F, 0xA1, 0xB2, 0x4D,
  30267. 0x60, 0xE8, 0xBA, 0xAC, 0xB1, 0x9D, 0x00, 0x40,
  30268. 0x24, 0xAF, 0x18, 0x6C, 0xAD, 0x86, 0xAC, 0xA0,
  30269. 0xD6, 0xAA, 0x8E, 0x8C, 0x36, 0x4E, 0xBD, 0xCB,
  30270. 0x43, 0x27, 0xC3, 0x6D, 0x30, 0x88, 0xDC, 0x80,
  30271. 0xCE, 0x04, 0xF3, 0xA0, 0xA7, 0x9C, 0x32, 0xAB,
  30272. 0x6C, 0x3F, 0x14, 0xF4, 0xD6, 0x29, 0x14, 0x69,
  30273. 0x2E, 0x4A, 0x9A, 0xFF, 0xD1, 0x96, 0x09, 0x14,
  30274. 0x05, 0x62, 0x33, 0x3A, 0x05, 0x65, 0x96, 0x2B,
  30275. 0xD8, 0xAC, 0xD2, 0x59, 0x50, 0xF5, 0x6C, 0x94,
  30276. 0x2C, 0xE1, 0x5C, 0x38, 0x1C, 0x95, 0x03, 0xD7,
  30277. 0x1C, 0xF1, 0x7C, 0x5E, 0x97, 0xA6, 0xAE, 0xFB,
  30278. 0x3F, 0xCF, 0x48, 0x80, 0x4E, 0x3F, 0x07, 0x5D,
  30279. 0x71, 0xD5, 0x6F, 0x44, 0x49, 0x93, 0x9A, 0xF0,
  30280. 0x47, 0x86, 0xD6, 0x2D, 0x12, 0xAD, 0x67, 0x06,
  30281. 0x65, 0x3E, 0xE9, 0x68, 0x84, 0x0E, 0xFC, 0xCE,
  30282. 0x28, 0x72, 0x57, 0x64, 0xE3, 0x07, 0xC0, 0x0F,
  30283. 0x0C, 0xBA, 0x57, 0x5A, 0x33, 0xC9, 0x6C, 0x6F,
  30284. 0x5A, 0x74, 0x9E, 0x59, 0x94, 0x5B, 0x92, 0xB3,
  30285. 0x0F, 0xC8, 0x99, 0xDE, 0x61, 0xD1, 0x1C, 0xCF,
  30286. 0xBF, 0xEF, 0x98, 0x3B, 0xF8, 0x54, 0xC8, 0xF5,
  30287. 0x41, 0x06, 0xB3, 0x03, 0x62, 0x4D, 0x79, 0xF0,
  30288. 0x66, 0x58, 0xFB, 0x4B, 0xDC, 0x69, 0x6C, 0xE0,
  30289. 0x59, 0x77, 0xDE, 0x4A, 0x11, 0x5D, 0x83, 0x8D,
  30290. 0x83, 0x50, 0xF3, 0xC2, 0xBE, 0x35, 0xA6, 0x54,
  30291. 0xCA, 0xBB, 0x1E, 0xBD, 0x46, 0xA9, 0xF0, 0x79,
  30292. 0xBA, 0xE4, 0x6B, 0x31, 0xEE, 0x45, 0x74, 0x26,
  30293. 0xF3, 0x97, 0x9E, 0x36, 0x61, 0x1F, 0xAF, 0x16,
  30294. 0x87, 0x0A, 0xBE, 0x9D, 0x8F, 0x93, 0xB5, 0x0A,
  30295. 0xC1, 0x49, 0x98, 0xE1, 0xA4, 0x95, 0xF5, 0x1A,
  30296. 0x1B, 0x02, 0x05, 0x6A, 0x2F, 0xFB, 0x17, 0x11,
  30297. 0x4C, 0xA7, 0x21, 0xAC, 0x24, 0x6C, 0x29, 0x20,
  30298. 0xBC, 0xEA, 0x3A, 0x72, 0x36, 0x7A, 0x0D, 0xA5,
  30299. 0x02, 0xE3, 0xF2, 0x5F, 0xFB, 0x3A, 0x58, 0x10,
  30300. 0x01, 0xAC, 0xFC, 0xDD, 0x91, 0x7E, 0xA7, 0x89,
  30301. 0xE4, 0xEF, 0x88, 0x43, 0x4D, 0xFE, 0x20, 0xBC,
  30302. 0x54, 0x78, 0x42, 0x8D, 0xD0, 0x80, 0xA0, 0xCA,
  30303. 0xDD, 0xC6, 0x1F, 0x02, 0x32, 0x03, 0x15, 0x1C,
  30304. 0x5A, 0x82, 0xD4, 0x95, 0x21, 0x8F, 0xF3, 0xFB,
  30305. 0x1E, 0x22, 0x7D, 0xBF, 0xCA, 0x1B, 0x5A, 0xBC,
  30306. 0xE6, 0x39, 0xC4, 0x9C, 0xD8, 0xEC, 0x12, 0x95,
  30307. 0x83, 0xC5, 0x73, 0xC5, 0x17, 0x02, 0xA1, 0xB5,
  30308. 0xC1, 0x01, 0x72, 0x13, 0x7C, 0x36, 0xDD, 0x66,
  30309. 0xD1, 0x84, 0x99, 0xB0, 0x78, 0x7D, 0xAB, 0xA9,
  30310. 0x78, 0xAC, 0x76, 0xC1, 0x14, 0xD0, 0x17, 0x8C,
  30311. 0xCB, 0xF7, 0xE6, 0xE1, 0x31, 0xBD, 0x9E, 0x53,
  30312. 0x3A, 0x13, 0x33, 0xD2, 0x2B, 0xD1, 0x7B, 0x99,
  30313. 0x6F, 0x76, 0x22, 0x57, 0x5C, 0xF9, 0x8B, 0x37,
  30314. 0x0D, 0x77, 0x17, 0xB7, 0x24, 0x48, 0x7B, 0xFB,
  30315. 0x27, 0x74, 0x62, 0xC8, 0x3C, 0x1C, 0xAA, 0xF6,
  30316. 0x28, 0x85, 0x7E, 0x71, 0xD6, 0x16, 0xDD, 0x5D,
  30317. 0x52, 0x87, 0xBD, 0xF3, 0x7B, 0x19, 0xDA, 0xAA,
  30318. 0xE4, 0xD2, 0x4C, 0xCC, 0xEC, 0xB1, 0x26, 0xAE,
  30319. 0x74, 0x25, 0x19, 0x7A, 0x21, 0x51, 0x13, 0x91,
  30320. 0xBA, 0x1E, 0x7D, 0x13, 0x08, 0x49, 0x82, 0xED,
  30321. 0x29, 0x1D, 0xDD, 0x51, 0x6A, 0x60, 0xED, 0xCD,
  30322. 0x86, 0x26, 0xF0, 0x0E, 0xEF, 0x6A, 0xA4, 0xD3,
  30323. 0x76, 0xE1, 0x53, 0x90, 0x64, 0x88, 0x46, 0x64,
  30324. 0x09, 0xCC, 0x26, 0x50, 0x82, 0xAA, 0xB0, 0x4E,
  30325. 0x5E, 0xDC, 0x8B, 0xE2, 0x45, 0xF5, 0x38, 0xC3,
  30326. 0x6E, 0x7E, 0x0A, 0xE3, 0xE7, 0x10, 0x9E, 0x25,
  30327. 0xDA, 0xF8, 0x09, 0x19, 0xCE, 0xD7, 0x30, 0xA6,
  30328. 0x93, 0xFF, 0xF8, 0xD2, 0x71, 0x6F, 0xB2, 0x27,
  30329. 0x15, 0x93, 0x17, 0xE0, 0x7D, 0x9D, 0xAE, 0x5B,
  30330. 0xDA, 0x9B, 0x01, 0x05, 0xB1, 0x7B, 0xAE, 0x55,
  30331. 0xA7, 0x37, 0x56, 0x6D, 0xC4, 0x4E, 0x63, 0xA4,
  30332. 0x7E, 0xBC, 0xA5, 0xA4, 0xE9, 0xB2, 0xB8, 0x06,
  30333. 0x8C, 0xB8, 0x13, 0xBD, 0xD2, 0xC3, 0xFD, 0x73,
  30334. 0x90, 0x81, 0x0C, 0xCC, 0x2C, 0xA1, 0x48, 0xEF,
  30335. 0xAF, 0x14, 0x59, 0xD7, 0x39, 0xD2, 0x2A, 0x12,
  30336. 0x14, 0x83, 0x92, 0xEA, 0x0D, 0x9A, 0xC3, 0x76,
  30337. 0x10, 0xD5, 0x61, 0x29, 0x1E, 0xE3, 0x88, 0x06,
  30338. 0x09, 0x42, 0xB0, 0xD0, 0x12, 0xB1, 0xBC, 0x52,
  30339. 0x34, 0xF5, 0x77, 0xF5, 0xF3, 0xF3, 0x4A, 0xFA,
  30340. 0x77, 0x9C, 0x15, 0x5E, 0x72, 0x9A, 0x57, 0xD6,
  30341. 0x45, 0xF8, 0x8E, 0x88, 0x23, 0xC1, 0xE7, 0xC5,
  30342. 0x5B, 0x87, 0xB7, 0xEE, 0x06, 0x89, 0x0B, 0x9E,
  30343. 0xD8, 0x13, 0x04, 0x33, 0xC7, 0x57, 0x93, 0x2C,
  30344. 0x4A, 0xAA, 0x8E, 0x7A, 0x26, 0x3E, 0x59, 0x8C,
  30345. 0x87, 0xEE, 0x0D, 0xFE, 0x7E, 0x12, 0x0E, 0x60,
  30346. 0x7C, 0x0F, 0xED, 0x41, 0x72, 0x11, 0xC5, 0x3B,
  30347. 0x10, 0xA7, 0x12, 0x24, 0x61, 0x56, 0xCA, 0xB4,
  30348. 0xBB, 0xCB, 0x46, 0xF2, 0xA4, 0x1F, 0xBB, 0x6B,
  30349. 0xD3, 0x67, 0x3D, 0x4E, 0x89, 0xDA, 0xE8, 0x88,
  30350. 0xD0, 0x35, 0xF8, 0xE5, 0x2A, 0xEA, 0x4D, 0x58,
  30351. 0x41, 0x1D, 0x5E, 0x24, 0xFA, 0x6C, 0x4A, 0x72,
  30352. 0x17, 0x4A, 0xDA, 0xA0, 0xD8, 0x85, 0xFA, 0x64,
  30353. 0xB1, 0x2B, 0x16, 0xEF, 0x85, 0x65, 0xDD, 0xE9,
  30354. 0x84, 0x56, 0xD6, 0x72, 0xBB, 0x52, 0xF6, 0x00,
  30355. 0x7B, 0x23, 0xF4, 0x74, 0x8F, 0x63, 0xA2, 0x48,
  30356. 0xAF, 0x1A, 0xB1, 0x86, 0xEA, 0xAC, 0xBD, 0xA1,
  30357. 0xC2, 0xB0, 0xB4, 0x77, 0xC6, 0x25, 0xA0, 0x0C,
  30358. 0xFF, 0x14, 0xFA, 0xD3, 0xCF, 0xC9, 0xFC, 0x2C,
  30359. 0xF2, 0xAE, 0x7E, 0x4B, 0x55, 0xE0, 0x41, 0xF8,
  30360. 0x5E, 0x2D, 0x03, 0xD3, 0x60, 0x25, 0xAB, 0x4D,
  30361. 0xF6, 0x65, 0x12, 0xBC, 0xE9, 0x66, 0x7F, 0x2A,
  30362. 0x2D, 0xF9, 0xC9, 0xAA, 0xE2, 0x80, 0x39, 0x7F,
  30363. 0xEF, 0x0C, 0x03, 0x8B, 0x54, 0x7A, 0xE5, 0x87,
  30364. 0xE7, 0xBE, 0x80, 0x22, 0x37, 0x96, 0xE3, 0x9A,
  30365. 0x93, 0x99, 0x99, 0x9A, 0xE5, 0x7A, 0xD6, 0x80,
  30366. 0x26, 0xF1, 0x3F, 0xCA, 0x9E, 0xE6, 0x33, 0x79,
  30367. 0xC9, 0x09, 0x9A, 0xCB, 0x2A, 0x61, 0x19, 0x04,
  30368. 0x74, 0x40, 0x5B, 0x57, 0xE3, 0x48, 0x4A, 0x52
  30369. };
  30370. const byte kyber1024_ss[] = {
  30371. 0xC1, 0x4E, 0xBD, 0x6E, 0x37, 0x88, 0xA6, 0x41,
  30372. 0xD1, 0x75, 0x5B, 0x4C, 0x86, 0x9C, 0x46, 0x99,
  30373. 0x4F, 0x75, 0xAF, 0x16, 0xB4, 0x0F, 0x8F, 0x64,
  30374. 0x92, 0xCC, 0xBE, 0xC7, 0xA3, 0xDA, 0x9B, 0xFE
  30375. };
  30376. #endif
  30377. ret = wc_KyberKey_Init(KYBER1024, &key, HEAP_HINT, INVALID_DEVID);
  30378. if (ret != 0)
  30379. return -20301;
  30380. ret = wc_KyberKey_MakeKeyWithRandom(&key, kyber1024_rand,
  30381. sizeof(kyber1024_rand));
  30382. if (ret != 0)
  30383. return -20302;
  30384. ret = wc_KyberKey_EncodePublicKey(&key, pub, sizeof(pub));
  30385. if (ret != 0)
  30386. return -20303;
  30387. ret = wc_KyberKey_EncodePrivateKey(&key, priv, sizeof(priv));
  30388. if (ret != 0)
  30389. return -20304;
  30390. if (XMEMCMP(pub, kyber1024_pk, sizeof(kyber1024_pk)) != 0)
  30391. return -20305;
  30392. if (XMEMCMP(priv, kyber1024_sk, sizeof(kyber1024_sk)) != 0)
  30393. return -20306;
  30394. ret = wc_KyberKey_EncapsulateWithRandom(&key, ct, ss, kyber1024enc_rand,
  30395. sizeof(kyber1024enc_rand));
  30396. if (ret != 0)
  30397. return -20307;
  30398. if (XMEMCMP(ct, kyber1024_ct, sizeof(kyber1024_ct)) != 0)
  30399. return -20308;
  30400. if (XMEMCMP(ss, kyber1024_ss, sizeof(kyber1024_ss)) != 0)
  30401. return -20309;
  30402. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, sizeof(kyber1024_ct));
  30403. if (ret != 0)
  30404. return -20310;
  30405. if (XMEMCMP(ss_dec, kyber1024_ss, sizeof(kyber1024_ss)) != 0)
  30406. return -20311;
  30407. wc_KyberKey_Free(&key);
  30408. return 0;
  30409. }
  30410. #endif /* WOLFSSL_KYBER1024 */
  30411. #endif /* WOLFSSL_WC_KYBER */
  30412. WOLFSSL_TEST_SUBROUTINE int kyber_test(void)
  30413. {
  30414. int ret;
  30415. KyberKey key;
  30416. WC_RNG rng;
  30417. int i;
  30418. byte priv[KYBER_MAX_PRIVATE_KEY_SIZE];
  30419. byte pub[KYBER_MAX_PUBLIC_KEY_SIZE];
  30420. byte priv2[KYBER_MAX_PRIVATE_KEY_SIZE];
  30421. byte pub2[KYBER_MAX_PUBLIC_KEY_SIZE];
  30422. byte ct[KYBER_MAX_CIPHER_TEXT_SIZE];
  30423. byte ss[KYBER_SS_SZ];
  30424. byte ss_dec[KYBER_SS_SZ];
  30425. int testData[][4] = {
  30426. #ifdef WOLFSSL_KYBER512
  30427. { KYBER512, KYBER512_PRIVATE_KEY_SIZE, KYBER512_PUBLIC_KEY_SIZE,
  30428. KYBER512_CIPHER_TEXT_SIZE },
  30429. #endif
  30430. #ifdef WOLFSSL_KYBER768
  30431. { KYBER768, KYBER768_PRIVATE_KEY_SIZE, KYBER768_PUBLIC_KEY_SIZE,
  30432. KYBER768_CIPHER_TEXT_SIZE },
  30433. #endif
  30434. #ifdef WOLFSSL_KYBER1024
  30435. { KYBER1024, KYBER1024_PRIVATE_KEY_SIZE, KYBER1024_PUBLIC_KEY_SIZE,
  30436. KYBER1024_CIPHER_TEXT_SIZE },
  30437. #endif
  30438. };
  30439. #ifndef HAVE_FIPS
  30440. ret = wc_InitRng_ex(&rng, HEAP_HINT, INVALID_DEVID);
  30441. #else
  30442. ret = wc_InitRng(&rng);
  30443. #endif
  30444. if (ret != 0)
  30445. return -20000;
  30446. for (i = 0; i < (int)(sizeof(testData) / sizeof(*testData)); i++) {
  30447. ret = wc_KyberKey_Init(testData[i][0], &key, HEAP_HINT, INVALID_DEVID);
  30448. if (ret != 0)
  30449. return -20001 - i * 20;
  30450. ret = wc_KyberKey_MakeKey(&key, &rng);
  30451. if (ret != 0)
  30452. return -20002 - i * 20;
  30453. ret = wc_KyberKey_EncodePublicKey(&key, pub, testData[i][2]);
  30454. if (ret != 0)
  30455. return -20003 - i * 20;
  30456. ret = wc_KyberKey_EncodePrivateKey(&key, priv, testData[i][1]);
  30457. if (ret != 0)
  30458. return -20004 - i * 20;
  30459. ret = wc_KyberKey_Init(testData[i][0], &key, HEAP_HINT, INVALID_DEVID);
  30460. if (ret != 0)
  30461. return -20005 - i * 20;
  30462. ret = wc_KyberKey_DecodePublicKey(&key, pub, testData[i][2]);
  30463. if (ret != 0)
  30464. return -20006 - i * 20;
  30465. ret = wc_KyberKey_Encapsulate(&key, ct, ss, &rng);
  30466. if (ret != 0)
  30467. return -20007 - i * 20;
  30468. ret = wc_KyberKey_EncodePublicKey(&key, pub2, testData[i][2]);
  30469. if (ret != 0)
  30470. return -20008 - i * 20;
  30471. if (XMEMCMP(pub, pub2, testData[i][2]) != 0)
  30472. return -20009 - i * 20;
  30473. ret = wc_KyberKey_Init(testData[i][0], &key, HEAP_HINT, INVALID_DEVID);
  30474. if (ret != 0)
  30475. return -20010 - i * 20;
  30476. ret = wc_KyberKey_DecodePrivateKey(&key, priv, testData[i][1]);
  30477. if (ret != 0)
  30478. return -20011 - i * 20;
  30479. ret = wc_KyberKey_Decapsulate(&key, ss_dec, ct, testData[i][3]);
  30480. if (ret != 0)
  30481. return -20012 - i * 20;
  30482. if (XMEMCMP(ss, ss_dec, sizeof(ss)) != 0)
  30483. return -20013 - i * 20;
  30484. ret = wc_KyberKey_EncodePrivateKey(&key, priv2, testData[i][1]);
  30485. if (ret != 0)
  30486. return -20014 - i * 20;
  30487. if (XMEMCMP(priv, priv2, testData[i][2]) != 0)
  30488. return -20015 - i * 20;
  30489. wc_KyberKey_Free(&key);
  30490. }
  30491. wc_FreeRng(&rng);
  30492. #ifdef WOLFSSL_WC_KYBER
  30493. #ifdef WOLFSSL_KYBER512
  30494. ret = kyber512_kat();
  30495. if (ret != 0)
  30496. return ret;
  30497. #endif
  30498. #ifdef WOLFSSL_KYBER768
  30499. ret = kyber768_kat();
  30500. if (ret != 0)
  30501. return ret;
  30502. #endif
  30503. #ifdef WOLFSSL_KYBER1024
  30504. ret = kyber1024_kat();
  30505. if (ret != 0)
  30506. return ret;
  30507. #endif
  30508. #endif /* WOLFSSL_WC_KYBER */
  30509. return 0;
  30510. }
  30511. #endif /* WOLFSSL_HAVE_KYBER */
  30512. #ifdef WOLFCRYPT_HAVE_ECCSI
  30513. static int eccsi_api_test(WC_RNG* rng, EccsiKey* key, mp_int* ssk,
  30514. ecc_point* pvt)
  30515. {
  30516. int ret;
  30517. byte id[1] = { 0x00 };
  30518. int valid;
  30519. word32 sz;
  30520. byte data[256];
  30521. byte hash[WC_MAX_DIGEST_SIZE];
  30522. byte hashSz;
  30523. byte sig[257];
  30524. word32 sigSz;
  30525. ret = wc_InitEccsiKey_ex(NULL, 32, ECC_SECP256R1, HEAP_HINT, INVALID_DEVID);
  30526. if (ret != BAD_FUNC_ARG)
  30527. return -10023;
  30528. ret = wc_InitEccsiKey_ex(NULL, 32, ECC_SECP256R1, HEAP_HINT, INVALID_DEVID);
  30529. if (ret != BAD_FUNC_ARG)
  30530. return -10024;
  30531. ret = wc_InitEccsiKey(NULL, NULL, INVALID_DEVID);
  30532. if (ret != BAD_FUNC_ARG)
  30533. return -10025;
  30534. ret = wc_InitEccsiKey(NULL, HEAP_HINT, INVALID_DEVID);
  30535. if (ret != BAD_FUNC_ARG)
  30536. return -10026;
  30537. wc_FreeEccsiKey(NULL);
  30538. /* Create a valid key. */
  30539. ret = wc_InitEccsiKey(key, NULL, INVALID_DEVID);
  30540. if (ret != 0)
  30541. return -10027;
  30542. ret = wc_MakeEccsiKey(NULL, NULL);
  30543. if (ret != BAD_FUNC_ARG)
  30544. return -10028;
  30545. ret = wc_MakeEccsiKey(key, NULL);
  30546. if (ret != BAD_FUNC_ARG)
  30547. return -10029;
  30548. ret = wc_MakeEccsiKey(NULL, rng);
  30549. if (ret != BAD_FUNC_ARG)
  30550. return -10030;
  30551. ret = wc_MakeEccsiPair(NULL, NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL,
  30552. NULL);
  30553. if (ret != BAD_FUNC_ARG)
  30554. return -10031;
  30555. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, NULL);
  30556. if (ret != BAD_FUNC_ARG)
  30557. return -10032;
  30558. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, NULL, pvt);
  30559. if (ret != BAD_FUNC_ARG)
  30560. return -10033;
  30561. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, NULL, 1, ssk, pvt);
  30562. if (ret != BAD_FUNC_ARG)
  30563. return -10034;
  30564. ret = wc_MakeEccsiPair(key, NULL, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  30565. if (ret != BAD_FUNC_ARG)
  30566. return -10035;
  30567. ret = wc_MakeEccsiPair(NULL, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  30568. if (ret != BAD_FUNC_ARG)
  30569. return -10036;
  30570. /* No key set */
  30571. ret = wc_MakeEccsiPair(key, rng, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt);
  30572. if (ret != BAD_STATE_E)
  30573. return -10037;
  30574. ret = wc_ValidateEccsiPair(NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL, NULL,
  30575. NULL);
  30576. if (ret != BAD_FUNC_ARG)
  30577. return -10038;
  30578. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  30579. NULL);
  30580. if (ret != BAD_FUNC_ARG)
  30581. return -10039;
  30582. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, NULL,
  30583. &valid);
  30584. if (ret != BAD_FUNC_ARG)
  30585. return -10040;
  30586. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, NULL, pvt,
  30587. &valid);
  30588. if (ret != BAD_FUNC_ARG)
  30589. return -10041;
  30590. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, NULL, 1, ssk, pvt,
  30591. &valid);
  30592. if (ret != BAD_FUNC_ARG)
  30593. return -10042;
  30594. ret = wc_ValidateEccsiPair(NULL, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  30595. &valid);
  30596. if (ret != BAD_FUNC_ARG)
  30597. return -10043;
  30598. /* No key set */
  30599. ret = wc_ValidateEccsiPair(key, WC_HASH_TYPE_SHA256, id, 1, ssk, pvt,
  30600. &valid);
  30601. if (ret != BAD_STATE_E)
  30602. return -10044;
  30603. ret = wc_ValidateEccsiPvt(NULL, NULL, NULL);
  30604. if (ret != BAD_FUNC_ARG)
  30605. return -10045;
  30606. ret = wc_ValidateEccsiPvt(key, NULL, NULL);
  30607. if (ret != BAD_FUNC_ARG)
  30608. return -10046;
  30609. ret = wc_ValidateEccsiPvt(NULL, pvt, NULL);
  30610. if (ret != BAD_FUNC_ARG)
  30611. return -10047;
  30612. ret = wc_ValidateEccsiPvt(NULL, NULL, &valid);
  30613. if (ret != BAD_FUNC_ARG)
  30614. return -10048;
  30615. ret = wc_ValidateEccsiPvt(key, pvt, NULL);
  30616. if (ret != BAD_FUNC_ARG)
  30617. return -10049;
  30618. ret = wc_ValidateEccsiPvt(key, NULL, &valid);
  30619. if (ret != BAD_FUNC_ARG)
  30620. return -10050;
  30621. ret = wc_ValidateEccsiPvt(NULL, pvt, &valid);
  30622. if (ret != BAD_FUNC_ARG)
  30623. return -10051;
  30624. ret = wc_EncodeEccsiPair(NULL, NULL, NULL, data, NULL);
  30625. if (ret != BAD_FUNC_ARG)
  30626. return -10052;
  30627. ret = wc_EncodeEccsiPair(key, ssk, pvt, data, NULL);
  30628. if (ret != BAD_FUNC_ARG)
  30629. return -10053;
  30630. ret = wc_EncodeEccsiPair(key, ssk, NULL, data, &sz);
  30631. if (ret != BAD_FUNC_ARG)
  30632. return -10054;
  30633. ret = wc_EncodeEccsiPair(key, NULL, pvt, data, &sz);
  30634. if (ret != BAD_FUNC_ARG)
  30635. return -10055;
  30636. ret = wc_EncodeEccsiPair(NULL, ssk, pvt, data, &sz);
  30637. if (ret != BAD_FUNC_ARG)
  30638. return -10056;
  30639. /* No key created so no curve information. */
  30640. ret = wc_EncodeEccsiPair(key, ssk, pvt, NULL, &sz);
  30641. if (ret != LENGTH_ONLY_E)
  30642. return -10057;
  30643. ret = wc_EncodeEccsiSsk(NULL, NULL, data, NULL);
  30644. if (ret != BAD_FUNC_ARG)
  30645. return -10058;
  30646. ret = wc_EncodeEccsiSsk(key, ssk, data, NULL);
  30647. if (ret != BAD_FUNC_ARG)
  30648. return -10059;
  30649. ret = wc_EncodeEccsiSsk(key, NULL, data, &sz);
  30650. if (ret != BAD_FUNC_ARG)
  30651. return -10060;
  30652. ret = wc_EncodeEccsiSsk(NULL, ssk, data, &sz);
  30653. if (ret != BAD_FUNC_ARG)
  30654. return -10061;
  30655. ret = wc_EncodeEccsiPvt(NULL, NULL, data, NULL, 1);
  30656. if (ret != BAD_FUNC_ARG)
  30657. return -10058;
  30658. ret = wc_EncodeEccsiPvt(key, pvt, data, NULL, 1);
  30659. if (ret != BAD_FUNC_ARG)
  30660. return -10059;
  30661. ret = wc_EncodeEccsiPvt(key, NULL, data, &sz, 1);
  30662. if (ret != BAD_FUNC_ARG)
  30663. return -10060;
  30664. ret = wc_EncodeEccsiPvt(NULL, pvt, data, &sz, 1);
  30665. if (ret != BAD_FUNC_ARG)
  30666. return -10061;
  30667. ret = wc_DecodeEccsiPair(NULL, NULL, 0, NULL, NULL);
  30668. if (ret != BAD_FUNC_ARG)
  30669. return -10062;
  30670. ret = wc_DecodeEccsiPair(key, data, 0, ssk, NULL);
  30671. if (ret != BAD_FUNC_ARG)
  30672. return -10063;
  30673. ret = wc_DecodeEccsiPair(key, data, 0, NULL, pvt);
  30674. if (ret != BAD_FUNC_ARG)
  30675. return -10064;
  30676. ret = wc_DecodeEccsiPair(key, NULL, 0, ssk, pvt);
  30677. if (ret != BAD_FUNC_ARG)
  30678. return -10065;
  30679. ret = wc_DecodeEccsiPair(NULL, data, 0, ssk, pvt);
  30680. if (ret != BAD_FUNC_ARG)
  30681. return -10066;
  30682. ret = wc_DecodeEccsiSsk(NULL, NULL, 0, NULL);
  30683. if (ret != BAD_FUNC_ARG)
  30684. return -10067;
  30685. ret = wc_DecodeEccsiSsk(key, data, 0, NULL);
  30686. if (ret != BAD_FUNC_ARG)
  30687. return -10068;
  30688. ret = wc_DecodeEccsiSsk(key, NULL, 0, ssk);
  30689. if (ret != BAD_FUNC_ARG)
  30690. return -10069;
  30691. ret = wc_DecodeEccsiSsk(NULL, data, 0, ssk);
  30692. if (ret != BAD_FUNC_ARG)
  30693. return -10070;
  30694. ret = wc_DecodeEccsiPvt(NULL, NULL, 0, NULL);
  30695. if (ret != BAD_FUNC_ARG)
  30696. return -10067;
  30697. ret = wc_DecodeEccsiPvt(key, data, 0, NULL);
  30698. if (ret != BAD_FUNC_ARG)
  30699. return -10068;
  30700. ret = wc_DecodeEccsiPvt(key, NULL, 0, pvt);
  30701. if (ret != BAD_FUNC_ARG)
  30702. return -10069;
  30703. ret = wc_DecodeEccsiPvt(NULL, data, 0, pvt);
  30704. if (ret != BAD_FUNC_ARG)
  30705. return -10070;
  30706. ret = wc_DecodeEccsiPvtFromSig(NULL, NULL, 0, NULL);
  30707. if (ret != BAD_FUNC_ARG)
  30708. return -10067;
  30709. ret = wc_DecodeEccsiPvtFromSig(key, data, 0, NULL);
  30710. if (ret != BAD_FUNC_ARG)
  30711. return -10068;
  30712. ret = wc_DecodeEccsiPvtFromSig(key, NULL, 0, pvt);
  30713. if (ret != BAD_FUNC_ARG)
  30714. return -10069;
  30715. ret = wc_DecodeEccsiPvtFromSig(NULL, data, 0, pvt);
  30716. if (ret != BAD_FUNC_ARG)
  30717. return -10070;
  30718. ret = wc_ExportEccsiKey(NULL, data, NULL);
  30719. if (ret != BAD_FUNC_ARG)
  30720. return -10071;
  30721. ret = wc_ExportEccsiKey(key, data, NULL);
  30722. if (ret != BAD_FUNC_ARG)
  30723. return -10072;
  30724. ret = wc_ExportEccsiKey(NULL, data, &sz);
  30725. if (ret != BAD_FUNC_ARG)
  30726. return -10073;
  30727. /* No key to export */
  30728. ret = wc_ExportEccsiKey(key, NULL, &sz);
  30729. if (ret != BAD_STATE_E)
  30730. return -10074;
  30731. ret = wc_ImportEccsiKey(NULL, NULL, 0);
  30732. if (ret != BAD_FUNC_ARG)
  30733. return -10075;
  30734. ret = wc_ImportEccsiKey(key, NULL, 0);
  30735. if (ret != BAD_FUNC_ARG)
  30736. return -10076;
  30737. ret = wc_ImportEccsiKey(NULL, data, 0);
  30738. if (ret != BAD_FUNC_ARG)
  30739. return -10077;
  30740. ret = wc_ExportEccsiPrivateKey(NULL, data, NULL);
  30741. if (ret != BAD_FUNC_ARG)
  30742. return -10071;
  30743. ret = wc_ExportEccsiPrivateKey(key, data, NULL);
  30744. if (ret != BAD_FUNC_ARG)
  30745. return -10072;
  30746. ret = wc_ExportEccsiPrivateKey(NULL, data, &sz);
  30747. if (ret != BAD_FUNC_ARG)
  30748. return -10073;
  30749. /* No key to export */
  30750. ret = wc_ExportEccsiPrivateKey(key, NULL, &sz);
  30751. if (ret != BAD_STATE_E)
  30752. return -10074;
  30753. ret = wc_ImportEccsiPrivateKey(NULL, NULL, 0);
  30754. if (ret != BAD_FUNC_ARG)
  30755. return -10075;
  30756. ret = wc_ImportEccsiPrivateKey(key, NULL, 0);
  30757. if (ret != BAD_FUNC_ARG)
  30758. return -10076;
  30759. ret = wc_ImportEccsiPrivateKey(NULL, data, 0);
  30760. if (ret != BAD_FUNC_ARG)
  30761. return -10077;
  30762. ret = wc_ExportEccsiPublicKey(NULL, data, NULL, 1);
  30763. if (ret != BAD_FUNC_ARG)
  30764. return -10078;
  30765. ret = wc_ExportEccsiPublicKey(key, data, NULL, 1);
  30766. if (ret != BAD_FUNC_ARG)
  30767. return -10079;
  30768. ret = wc_ExportEccsiPublicKey(NULL, data, &sz, 1);
  30769. if (ret != BAD_FUNC_ARG)
  30770. return -10080;
  30771. /* No key to export */
  30772. ret = wc_ExportEccsiPublicKey(key, data, &sz, 1);
  30773. if (ret != BAD_STATE_E)
  30774. return -10081;
  30775. ret = wc_ImportEccsiPublicKey(NULL, NULL, 0, 1);
  30776. if (ret != BAD_FUNC_ARG)
  30777. return -10082;
  30778. ret = wc_ImportEccsiPublicKey(key, NULL, 0, 1);
  30779. if (ret != BAD_FUNC_ARG)
  30780. return -10083;
  30781. ret = wc_ImportEccsiPublicKey(NULL, data, 0, 1);
  30782. if (ret != BAD_FUNC_ARG)
  30783. return -10084;
  30784. ret = wc_HashEccsiId(NULL, WC_HASH_TYPE_SHA256, NULL, 1, NULL, NULL, NULL);
  30785. if (ret != BAD_FUNC_ARG)
  30786. return -10085;
  30787. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, NULL);
  30788. if (ret != BAD_FUNC_ARG)
  30789. return -10086;
  30790. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, NULL, &hashSz);
  30791. if (ret != BAD_FUNC_ARG)
  30792. return -10087;
  30793. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, NULL, hash, &hashSz);
  30794. if (ret != BAD_FUNC_ARG)
  30795. return -10088;
  30796. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, NULL, 1, pvt, hash,
  30797. &hashSz);
  30798. if (ret != BAD_FUNC_ARG)
  30799. return -10089;
  30800. ret = wc_HashEccsiId(NULL, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, &hashSz);
  30801. if (ret != BAD_FUNC_ARG)
  30802. return -10090;
  30803. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, 1, pvt, hash, &hashSz);
  30804. if (ret != BAD_STATE_E)
  30805. return -10091;
  30806. ret = wc_SetEccsiHash(NULL, NULL, 1);
  30807. if (ret != BAD_FUNC_ARG)
  30808. return -10090;
  30809. ret = wc_SetEccsiHash(key, NULL, 1);
  30810. if (ret != BAD_FUNC_ARG)
  30811. return -10090;
  30812. ret = wc_SetEccsiHash(NULL, hash, 1);
  30813. if (ret != BAD_FUNC_ARG)
  30814. return -10090;
  30815. ret = wc_SetEccsiPair(NULL, NULL, NULL);
  30816. if (ret != BAD_FUNC_ARG)
  30817. return -10090;
  30818. ret = wc_SetEccsiPair(key, NULL, NULL);
  30819. if (ret != BAD_FUNC_ARG)
  30820. return -10090;
  30821. ret = wc_SetEccsiPair(NULL, ssk, NULL);
  30822. if (ret != BAD_FUNC_ARG)
  30823. return -10090;
  30824. ret = wc_SetEccsiPair(NULL, NULL, pvt);
  30825. if (ret != BAD_FUNC_ARG)
  30826. return -10090;
  30827. ret = wc_SetEccsiPair(key, ssk, NULL);
  30828. if (ret != BAD_FUNC_ARG)
  30829. return -10090;
  30830. ret = wc_SetEccsiPair(key, NULL, pvt);
  30831. if (ret != BAD_FUNC_ARG)
  30832. return -10090;
  30833. ret = wc_SetEccsiPair(NULL, ssk, pvt);
  30834. if (ret != BAD_FUNC_ARG)
  30835. return -10090;
  30836. ret = wc_SignEccsiHash(NULL, NULL, WC_HASH_TYPE_SHA256, NULL, 0, sig, NULL);
  30837. if (ret != BAD_FUNC_ARG)
  30838. return -10092;
  30839. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, sig, NULL);
  30840. if (ret != BAD_FUNC_ARG)
  30841. return -10093;
  30842. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, NULL, 0, sig,
  30843. &sigSz);
  30844. if (ret != BAD_FUNC_ARG)
  30845. return -10096;
  30846. ret = wc_SignEccsiHash(key, NULL, WC_HASH_TYPE_SHA256, data, 0, sig,
  30847. &sigSz);
  30848. if (ret != BAD_FUNC_ARG)
  30849. return -10098;
  30850. ret = wc_SignEccsiHash(NULL, rng, WC_HASH_TYPE_SHA256, data, 0, sig,
  30851. &sigSz);
  30852. if (ret != BAD_FUNC_ARG)
  30853. return -10099;
  30854. /* Key not set. */
  30855. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, NULL,
  30856. &sigSz);
  30857. if (ret != BAD_STATE_E)
  30858. return -10100;
  30859. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0, NULL);
  30860. if (ret != BAD_FUNC_ARG)
  30861. return -10101;
  30862. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0, NULL);
  30863. if (ret != BAD_FUNC_ARG)
  30864. return -10101;
  30865. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, data, 0, NULL, 0, NULL);
  30866. if (ret != BAD_FUNC_ARG)
  30867. return -10101;
  30868. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, sig, 0, NULL);
  30869. if (ret != BAD_FUNC_ARG)
  30870. return -10101;
  30871. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, NULL, 0, NULL, 0,
  30872. &valid);
  30873. if (ret != BAD_FUNC_ARG)
  30874. return -10101;
  30875. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, sig, 0, NULL);
  30876. if (ret != BAD_FUNC_ARG)
  30877. return -10102;
  30878. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, NULL, 0,
  30879. &valid);
  30880. if (ret != BAD_FUNC_ARG)
  30881. return -10103;
  30882. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, NULL, 0, sig, 0,
  30883. &valid);
  30884. if (ret != BAD_FUNC_ARG)
  30885. return -10104;
  30886. ret = wc_VerifyEccsiHash(NULL, WC_HASH_TYPE_SHA256, data, 0, sig, 0,
  30887. &valid);
  30888. if (ret != BAD_FUNC_ARG)
  30889. return -10106;
  30890. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, data, 0, sig, 0,
  30891. &valid);
  30892. if (ret != BAD_STATE_E)
  30893. return -10106;
  30894. ret = wc_SetEccsiPair(key, ssk, pvt);
  30895. if (ret != 0)
  30896. return -10107;
  30897. /* Identity hash not set. */
  30898. ret = wc_SignEccsiHash(key, rng, WC_HASH_TYPE_SHA256, data, 0, NULL,
  30899. &sigSz);
  30900. if (ret != BAD_STATE_E)
  30901. return -10108;
  30902. wc_FreeEccsiKey(key);
  30903. return 0;
  30904. }
  30905. /* RFC 6507: Appendix A */
  30906. static int eccsi_kat_verify_test(EccsiKey* key, ecc_point* pvt)
  30907. {
  30908. int ret;
  30909. int verified;
  30910. const byte msg[] = { 0x6D, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x00 };
  30911. word32 msgSz = sizeof(msg);
  30912. byte hash[WC_SHA256_DIGEST_SIZE];
  30913. byte hashSz = WC_SHA256_DIGEST_SIZE;
  30914. static const byte id[] = {
  30915. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  30916. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  30917. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  30918. 0x33, 0x00
  30919. };
  30920. word32 idSz = sizeof(id);
  30921. static const byte sig[] = {
  30922. 0x26, 0x9D, 0x4C, 0x8F, 0xDE, 0xB6, 0x6A, 0x74,
  30923. 0xE4, 0xEF, 0x8C, 0x0D, 0x5D, 0xCC, 0x59, 0x7D,
  30924. 0xDF, 0xE6, 0x02, 0x9C, 0x2A, 0xFF, 0xC4, 0x93,
  30925. 0x60, 0x08, 0xCD, 0x2C, 0xC1, 0x04, 0x5D, 0x81,
  30926. 0xE0, 0x9B, 0x52, 0x8D, 0x0E, 0xF8, 0xD6, 0xDF,
  30927. 0x1A, 0xA3, 0xEC, 0xBF, 0x80, 0x11, 0x0C, 0xFC,
  30928. 0xEC, 0x9F, 0xC6, 0x82, 0x52, 0xCE, 0xBB, 0x67,
  30929. 0x9F, 0x41, 0x34, 0x84, 0x69, 0x40, 0xCC, 0xFD,
  30930. 0x04,
  30931. 0x75, 0x8A, 0x14, 0x27, 0x79, 0xBE, 0x89, 0xE8,
  30932. 0x29, 0xE7, 0x19, 0x84, 0xCB, 0x40, 0xEF, 0x75,
  30933. 0x8C, 0xC4, 0xAD, 0x77, 0x5F, 0xC5, 0xB9, 0xA3,
  30934. 0xE1, 0xC8, 0xED, 0x52, 0xF6, 0xFA, 0x36, 0xD9,
  30935. 0xA7, 0x9D, 0x24, 0x76, 0x92, 0xF4, 0xED, 0xA3,
  30936. 0xA6, 0xBD, 0xAB, 0x77, 0xD6, 0xAA, 0x64, 0x74,
  30937. 0xA4, 0x64, 0xAE, 0x49, 0x34, 0x66, 0x3C, 0x52,
  30938. 0x65, 0xBA, 0x70, 0x18, 0xBA, 0x09, 0x1F, 0x79
  30939. };
  30940. word32 sigSz = sizeof(sig);
  30941. static const byte pubData[] = {
  30942. 0x50, 0xD4, 0x67, 0x0B, 0xDE, 0x75, 0x24, 0x4F,
  30943. 0x28, 0xD2, 0x83, 0x8A, 0x0D, 0x25, 0x55, 0x8A,
  30944. 0x7A, 0x72, 0x68, 0x6D, 0x45, 0x22, 0xD4, 0xC8,
  30945. 0x27, 0x3F, 0xB6, 0x44, 0x2A, 0xEB, 0xFA, 0x93,
  30946. 0xDB, 0xDD, 0x37, 0x55, 0x1A, 0xFD, 0x26, 0x3B,
  30947. 0x5D, 0xFD, 0x61, 0x7F, 0x39, 0x60, 0xC6, 0x5A,
  30948. 0x8C, 0x29, 0x88, 0x50, 0xFF, 0x99, 0xF2, 0x03,
  30949. 0x66, 0xDC, 0xE7, 0xD4, 0x36, 0x72, 0x17, 0xF4
  30950. };
  30951. static const byte expHash[] = {
  30952. 0x49, 0x0f, 0x3f, 0xeb, 0xbc, 0x1c, 0x90, 0x2f,
  30953. 0x62, 0x89, 0x72, 0x3d, 0x7f, 0x8c, 0xbf, 0x79,
  30954. 0xdb, 0x88, 0x93, 0x08, 0x49, 0xd1, 0x9f, 0x38,
  30955. 0xf0, 0x29, 0x5b, 0x5c, 0x27, 0x6c, 0x14, 0xd1
  30956. };
  30957. ret = wc_ImportEccsiPublicKey(key, pubData, sizeof(pubData), 0);
  30958. if (ret != 0)
  30959. return -10108;
  30960. ret = wc_DecodeEccsiPvtFromSig(key, sig, sigSz, pvt);
  30961. if (ret != 0)
  30962. return -10109;
  30963. ret = wc_HashEccsiId(key, WC_HASH_TYPE_SHA256, id, idSz, pvt, hash,
  30964. &hashSz);
  30965. if (ret != 0)
  30966. return -10112;
  30967. if (hashSz != sizeof(expHash))
  30968. return -10113;
  30969. if (XMEMCMP(hash, expHash, hashSz) != 0)
  30970. return -10114;
  30971. ret = wc_SetEccsiHash(key, hash, hashSz);
  30972. if (ret != 0)
  30973. return -10112;
  30974. ret = wc_VerifyEccsiHash(key, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  30975. &verified);
  30976. if (ret != 0)
  30977. return -10115;
  30978. if (!verified)
  30979. return -10116;
  30980. return 0;
  30981. }
  30982. static int eccsi_enc_dec_pair_test(EccsiKey* priv, mp_int* ssk, ecc_point* pvt)
  30983. {
  30984. int ret;
  30985. byte data[32 * 3];
  30986. word32 sz;
  30987. ecc_point* decPvt = NULL;
  30988. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  30989. mp_int *decSsk = (mp_int *)XMALLOC(sizeof(*decSsk), HEAP_HINT,
  30990. DYNAMIC_TYPE_TMP_BUFFER);
  30991. if (decSsk == NULL)
  30992. return -10173;
  30993. #else
  30994. mp_int decSsk[1];
  30995. #endif
  30996. ret = mp_init(decSsk);
  30997. if (ret != 0)
  30998. ERROR_OUT(-10117, out);
  30999. decPvt = wc_ecc_new_point();
  31000. if (decPvt == NULL)
  31001. ERROR_OUT(-10118, out);
  31002. ret = wc_EncodeEccsiPair(priv, ssk, pvt, NULL, &sz);
  31003. if (ret != LENGTH_ONLY_E)
  31004. ERROR_OUT(-10119, out);
  31005. if (sz != 32 * 3)
  31006. ERROR_OUT(-10120, out);
  31007. ret = wc_EncodeEccsiPair(priv, ssk, pvt, data, &sz);
  31008. if (ret != 0)
  31009. ERROR_OUT(-10121, out);
  31010. if (sz != 32* 3)
  31011. ERROR_OUT(-10122, out);
  31012. ret = wc_DecodeEccsiPair(priv, data, sz, decSsk, decPvt);
  31013. if (ret != 0)
  31014. ERROR_OUT(-10123, out);
  31015. if (mp_cmp(ssk, decSsk) != MP_EQ)
  31016. ERROR_OUT(-10124, out);
  31017. if (wc_ecc_cmp_point(pvt, decPvt) != MP_EQ)
  31018. ERROR_OUT(-10125, out);
  31019. ret = wc_EncodeEccsiSsk(priv, ssk, NULL, &sz);
  31020. if (ret != LENGTH_ONLY_E)
  31021. ERROR_OUT(-10119, out);
  31022. if (sz != 32)
  31023. ERROR_OUT(-10120, out);
  31024. ret = wc_EncodeEccsiSsk(priv, ssk, data, &sz);
  31025. if (ret != 0)
  31026. ERROR_OUT(-10121, out);
  31027. if (sz != 32)
  31028. ERROR_OUT(-10122, out);
  31029. ret = wc_DecodeEccsiSsk(priv, data, sz, decSsk);
  31030. if (ret != 0)
  31031. ERROR_OUT(-10123, out);
  31032. if (mp_cmp(ssk, decSsk) != MP_EQ)
  31033. ERROR_OUT(-10124, out);
  31034. ret = wc_EncodeEccsiPvt(priv, pvt, NULL, &sz, 1);
  31035. if (ret != LENGTH_ONLY_E)
  31036. ERROR_OUT(-10126, out);
  31037. if (sz != 32 * 2)
  31038. ERROR_OUT(-10127, out);
  31039. ret = wc_EncodeEccsiPvt(priv, pvt, data, &sz, 1);
  31040. if (ret != 0)
  31041. ERROR_OUT(-10128, out);
  31042. if (sz != 32 * 2)
  31043. ERROR_OUT(-10129, out);
  31044. ret = wc_DecodeEccsiPvt(priv, data, sz, decPvt);
  31045. if (ret != 0)
  31046. ERROR_OUT(-10130, out);
  31047. if (wc_ecc_cmp_point(pvt, decPvt) != MP_EQ)
  31048. ERROR_OUT(-10131, out);
  31049. sz = sizeof(data);
  31050. ret = wc_EncodeEccsiPvt(priv, pvt, data, &sz, 0);
  31051. if (ret != 0)
  31052. ERROR_OUT(-10128, out);
  31053. if (sz != 32 * 2 + 1)
  31054. ERROR_OUT(-10129, out);
  31055. ret = wc_DecodeEccsiPvt(priv, data, sz, decPvt);
  31056. if (ret != 0)
  31057. ERROR_OUT(-10130, out);
  31058. if (wc_ecc_cmp_point(pvt, decPvt) != MP_EQ)
  31059. ERROR_OUT(-10131, out);
  31060. wc_ecc_del_point(decPvt);
  31061. out:
  31062. mp_free(decSsk);
  31063. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  31064. XFREE(decSsk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31065. #endif
  31066. return ret;
  31067. }
  31068. static int eccsi_imp_exp_key_test(EccsiKey* priv)
  31069. {
  31070. int ret;
  31071. byte data[32 * 3];
  31072. byte out[32 * 3];
  31073. word32 sz;
  31074. ret = wc_ExportEccsiKey(priv, NULL, &sz);
  31075. if (ret != LENGTH_ONLY_E)
  31076. return -10132;
  31077. if (sz != 32 * 3)
  31078. return -10133;
  31079. ret = wc_ExportEccsiKey(priv, data, &sz);
  31080. if (ret != 0)
  31081. return -10134;
  31082. ret = wc_ImportEccsiKey(priv, data, sz);
  31083. if (ret != 0)
  31084. return -10135;
  31085. ret = wc_ExportEccsiKey(priv, NULL, &sz);
  31086. if (ret != LENGTH_ONLY_E)
  31087. return -10132;
  31088. if (sz != 32 * 3)
  31089. return -10143;
  31090. ret = wc_ExportEccsiKey(priv, out, &sz);
  31091. if (ret != 0)
  31092. return -10144;
  31093. if (sz != 32 * 3)
  31094. return -10145;
  31095. if (XMEMCMP(data, out, sz) != 0)
  31096. return -10146;
  31097. ret = wc_ExportEccsiPrivateKey(priv, NULL, &sz);
  31098. if (ret != LENGTH_ONLY_E)
  31099. return -10156;
  31100. if (sz != 32)
  31101. return -10157;
  31102. ret = wc_ExportEccsiPrivateKey(priv, data, &sz);
  31103. if (ret != 0)
  31104. return -10158;
  31105. ret = wc_ImportEccsiPrivateKey(priv, data, sz);
  31106. if (ret != 0)
  31107. return -10159;
  31108. ret = wc_ExportEccsiPrivateKey(priv, NULL, &sz);
  31109. if (ret != LENGTH_ONLY_E)
  31110. return -10152;
  31111. if (sz != 32)
  31112. return -10163;
  31113. ret = wc_ExportEccsiPrivateKey(priv, out, &sz);
  31114. if (ret != 0)
  31115. return -10164;
  31116. if (sz != 32)
  31117. return -10165;
  31118. if (XMEMCMP(data, out, sz) != 0)
  31119. return -10166;
  31120. return 0;
  31121. }
  31122. static int eccsi_imp_exp_pubkey_test(EccsiKey* key1, EccsiKey* key2)
  31123. {
  31124. int ret;
  31125. byte data[32 * 2 + 1];
  31126. byte pubData[32 * 2 + 1];
  31127. word32 sz;
  31128. ret = wc_ExportEccsiPublicKey(key1, NULL, &sz, 1);
  31129. if (ret != LENGTH_ONLY_E)
  31130. return -10136;
  31131. if (sz != 32 * 2)
  31132. return -10137;
  31133. ret = wc_ExportEccsiPublicKey(key1, data, &sz, 1);
  31134. if (ret != 0)
  31135. return -10138;
  31136. ret = wc_ImportEccsiPublicKey(key2, data, sz, 1);
  31137. if (ret != 0)
  31138. return -10139;
  31139. sz = sizeof(pubData);
  31140. ret = wc_ExportEccsiPublicKey(key2, pubData, &sz, 1);
  31141. if (ret != 0)
  31142. return -10140;
  31143. if (sz != 32 * 2)
  31144. return -10141;
  31145. if (XMEMCMP(data, pubData, sz) != 0)
  31146. return -10142;
  31147. sz = sizeof(pubData);
  31148. ret = wc_ExportEccsiPublicKey(key2, pubData, &sz, 0);
  31149. if (ret != 0)
  31150. return -10140;
  31151. if (sz != 32 * 2 + 1)
  31152. return -10141;
  31153. if (pubData[0] != 0x04)
  31154. return -10140;
  31155. if (XMEMCMP(pubData + 1, data, sz - 1) != 0)
  31156. return -10142;
  31157. ret = wc_ImportEccsiPublicKey(key2, pubData, sz, 0);
  31158. if (ret != 0)
  31159. return -10139;
  31160. return 0;
  31161. }
  31162. static int eccsi_make_key_test(EccsiKey* priv, EccsiKey* pub, WC_RNG* rng,
  31163. mp_int* ssk, ecc_point* pvt)
  31164. {
  31165. int ret;
  31166. char mail[] = "test@wolfssl.com";
  31167. byte* id = (byte*)mail;
  31168. word32 idSz = (word32) XSTRLEN(mail);
  31169. int valid;
  31170. ret = wc_MakeEccsiKey(priv, rng);
  31171. if (ret != 0)
  31172. return -10143;
  31173. ret = eccsi_imp_exp_key_test(priv);
  31174. if (ret < 0)
  31175. return ret;
  31176. ret = eccsi_imp_exp_pubkey_test(priv, pub);
  31177. if (ret < 0)
  31178. return ret;
  31179. ret = wc_MakeEccsiPair(priv, rng, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt);
  31180. if (ret != 0)
  31181. return -10144;
  31182. ret = wc_ValidateEccsiPair(pub, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt,
  31183. &valid);
  31184. if (ret != 0)
  31185. return -10145;
  31186. if (!valid)
  31187. return -10146;
  31188. ret = eccsi_enc_dec_pair_test(priv, ssk, pvt);
  31189. if (ret != 0)
  31190. return ret;
  31191. return 0;
  31192. }
  31193. static int eccsi_sign_verify_test(EccsiKey* priv, EccsiKey* pub, WC_RNG* rng,
  31194. mp_int* ssk, ecc_point* pvt)
  31195. {
  31196. int ret;
  31197. byte hashPriv[WC_MAX_DIGEST_SIZE];
  31198. byte hashPub[WC_MAX_DIGEST_SIZE];
  31199. byte hashSz;
  31200. byte sig[144];
  31201. word32 sigSz;
  31202. int verified, valid;
  31203. char mail[] = "test@wolfssl.com";
  31204. byte* id = (byte*)mail;
  31205. word32 idSz = (word32) XSTRLEN(mail);
  31206. byte msg[] = { 0x00 };
  31207. word32 msgSz = sizeof(msg);
  31208. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA256, id, idSz, pvt, hashPriv,
  31209. &hashSz);
  31210. if (ret != 0)
  31211. return -10147;
  31212. if (hashSz != 32)
  31213. return -10148;
  31214. ret = wc_HashEccsiId(priv, WC_HASH_TYPE_SHA256, id, idSz, pvt, hashPub,
  31215. &hashSz);
  31216. if (ret != 0)
  31217. return -10149;
  31218. if (hashSz != 32)
  31219. return -10150;
  31220. if (XMEMCMP(hashPriv, hashPub, hashSz) != 0)
  31221. return -10151;
  31222. ret = wc_SetEccsiHash(priv, hashPriv, hashSz);
  31223. if (ret != 0)
  31224. return -10149;
  31225. ret = wc_SetEccsiPair(priv, ssk, pvt);
  31226. if (ret != 0)
  31227. return -10149;
  31228. ret = wc_SignEccsiHash(priv, rng, WC_HASH_TYPE_SHA256, msg, msgSz, NULL,
  31229. &sigSz);
  31230. if (ret != LENGTH_ONLY_E)
  31231. return -10152;
  31232. if (sigSz != 129)
  31233. return -10153;
  31234. ret = wc_SignEccsiHash(priv, rng, WC_HASH_TYPE_SHA256, msg, msgSz, sig,
  31235. &sigSz);
  31236. if (ret != 0)
  31237. return -10154;
  31238. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  31239. if (ret != 0)
  31240. return -10149;
  31241. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31242. &verified);
  31243. if (ret != 0)
  31244. return -10155;
  31245. if (!verified)
  31246. return -10156;
  31247. /* Check that changing HS results in verification failure. */
  31248. hashPub[0] ^= 0x80;
  31249. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  31250. if (ret != 0)
  31251. return -10149;
  31252. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31253. &verified);
  31254. if (ret != 0)
  31255. return -10157;
  31256. if (verified)
  31257. return -10158;
  31258. hashPub[0] ^= 0x80;
  31259. ret = wc_SetEccsiHash(pub, hashPub, hashSz);
  31260. if (ret != 0)
  31261. return -10149;
  31262. /* Check that changing msg results in verification failure. */
  31263. msg[0] ^= 0x80;
  31264. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31265. &verified);
  31266. if (ret != 0)
  31267. return -10159;
  31268. if (verified)
  31269. return -10160;
  31270. msg[0] ^= 0x80;
  31271. /* Check that changing signature results in verification failure. */
  31272. sig[0] ^= 0x80;
  31273. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31274. &verified);
  31275. if (ret != 0)
  31276. return -10161;
  31277. if (verified)
  31278. return -10162;
  31279. sig[0] ^= 0x80;
  31280. /* Check that key state hasn't been invalidated. */
  31281. ret = wc_VerifyEccsiHash(pub, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31282. &verified);
  31283. if (ret != 0)
  31284. return -10163;
  31285. if (!verified)
  31286. return -10164;
  31287. /* Check that verifying with the private key works. */
  31288. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31289. &verified);
  31290. if (ret != 0)
  31291. return -10165;
  31292. if (!verified)
  31293. return -10166;
  31294. /* Check that the KPAK is converted from montgomery form. */
  31295. ret = eccsi_imp_exp_key_test(priv);
  31296. if (ret != 0)
  31297. return ret;
  31298. /* Check that KPAK can converted to Montgomery form again. */
  31299. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31300. &verified);
  31301. if (ret != 0)
  31302. return -10167;
  31303. if (!verified)
  31304. return -10168;
  31305. /* Check that the KPAK is converted from montgomery form. */
  31306. ret = wc_ValidateEccsiPair(pub, WC_HASH_TYPE_SHA256, id, idSz, ssk, pvt,
  31307. &valid);
  31308. if (ret != 0)
  31309. return -10169;
  31310. if (!valid)
  31311. return -10170;
  31312. /* Check that KPAK can converted to Montgomery form again. */
  31313. ret = wc_VerifyEccsiHash(priv, WC_HASH_TYPE_SHA256, msg, msgSz, sig, sigSz,
  31314. &verified);
  31315. if (ret != 0)
  31316. return -10171;
  31317. if (!verified)
  31318. return -10172;
  31319. /* Check that the KPAK is converted from montgomery form. */
  31320. ret = eccsi_imp_exp_pubkey_test(priv, pub);
  31321. if (ret != 0)
  31322. return ret;
  31323. return 0;
  31324. }
  31325. int eccsi_test(void)
  31326. {
  31327. int ret = 0;
  31328. WC_RNG rng;
  31329. int rng_inited = 0;
  31330. EccsiKey* priv = NULL;
  31331. EccsiKey* pub = NULL;
  31332. mp_int* ssk = NULL;
  31333. ecc_point* pvt = NULL;
  31334. priv = (EccsiKey*)XMALLOC(sizeof(EccsiKey), HEAP_HINT,
  31335. DYNAMIC_TYPE_TMP_BUFFER);
  31336. if (priv == NULL)
  31337. ret = -10205;
  31338. else
  31339. XMEMSET(priv, 0, sizeof(*priv));
  31340. if (ret == 0) {
  31341. pub = (EccsiKey*)XMALLOC(sizeof(EccsiKey), HEAP_HINT,
  31342. DYNAMIC_TYPE_TMP_BUFFER);
  31343. if (pub == NULL)
  31344. ret = -10206;
  31345. else
  31346. XMEMSET(pub, 0, sizeof(*pub));
  31347. }
  31348. if (ret == 0) {
  31349. ssk = (mp_int*)XMALLOC(sizeof(mp_int), HEAP_HINT,
  31350. DYNAMIC_TYPE_TMP_BUFFER);
  31351. if (ssk == NULL)
  31352. ret = -10207;
  31353. else
  31354. XMEMSET(ssk, 0, sizeof(*ssk));
  31355. }
  31356. if (ret == 0) {
  31357. #ifndef HAVE_FIPS
  31358. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  31359. #else
  31360. ret = wc_InitRng(&rng);
  31361. #endif
  31362. if (ret != 0)
  31363. ret = -10200;
  31364. else
  31365. rng_inited = 1;
  31366. }
  31367. if (ret == 0) {
  31368. pvt = wc_ecc_new_point();
  31369. if (pvt == NULL)
  31370. ret = -10201;
  31371. }
  31372. if (ret == 0) {
  31373. ret = mp_init(ssk);
  31374. if (ret != 0)
  31375. ret = -10202;
  31376. }
  31377. if (ret == 0) {
  31378. ret = eccsi_api_test(&rng, priv, ssk, pvt);
  31379. }
  31380. if (ret == 0) {
  31381. ret = wc_InitEccsiKey(pub, HEAP_HINT, INVALID_DEVID);
  31382. if (ret != 0)
  31383. ret = -10203;
  31384. }
  31385. if (ret == 0) {
  31386. ret = wc_InitEccsiKey(priv, HEAP_HINT, INVALID_DEVID);
  31387. if (ret != 0)
  31388. ret = -10204;
  31389. }
  31390. if (ret == 0) {
  31391. ret = eccsi_kat_verify_test(pub, pvt);
  31392. }
  31393. if (ret == 0) {
  31394. ret = eccsi_make_key_test(priv, pub, &rng, ssk, pvt);
  31395. }
  31396. if (ret == 0) {
  31397. ret = eccsi_sign_verify_test(priv, pub, &rng, ssk, pvt);
  31398. }
  31399. if (pvt != NULL)
  31400. wc_ecc_del_point(pvt);
  31401. if (rng_inited)
  31402. wc_FreeRng(&rng);
  31403. if (ssk != NULL) {
  31404. mp_free(ssk);
  31405. XFREE(ssk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31406. }
  31407. if (pub != NULL) {
  31408. wc_FreeEccsiKey(pub);
  31409. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31410. }
  31411. if (priv != NULL) {
  31412. wc_FreeEccsiKey(priv);
  31413. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31414. }
  31415. return ret;
  31416. }
  31417. #endif /* WOLFCRYPT_HAVE_ECCSI */
  31418. #ifdef WOLFCRYPT_HAVE_SAKKE
  31419. static int sakke_api_test(WC_RNG* rng, SakkeKey* key, ecc_point* rsk)
  31420. {
  31421. int ret;
  31422. byte id[1] = { 0x00 };
  31423. int valid;
  31424. byte data[256];
  31425. word32 sz;
  31426. byte auth[257];
  31427. word16 authSz;
  31428. byte ssv[256];
  31429. word16 ssvSz;
  31430. word32 len;
  31431. ret = wc_InitSakkeKey_ex(NULL, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  31432. if (ret != BAD_FUNC_ARG)
  31433. return -10205;
  31434. ret = wc_InitSakkeKey_ex(NULL, 128, ECC_SAKKE_1, HEAP_HINT, INVALID_DEVID);
  31435. if (ret != BAD_FUNC_ARG)
  31436. return -10206;
  31437. wc_FreeSakkeKey(NULL);
  31438. XMEMSET(key, 0, sizeof(*key));
  31439. wc_FreeSakkeKey(key);
  31440. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, HEAP_HINT, INVALID_DEVID);
  31441. if (ret != 0)
  31442. return -10207;
  31443. ret = wc_MakeSakkeKey(NULL, NULL);
  31444. if (ret != BAD_FUNC_ARG)
  31445. return -10208;
  31446. ret = wc_MakeSakkeKey(key, NULL);
  31447. if (ret != BAD_FUNC_ARG)
  31448. return -10209;
  31449. ret = wc_MakeSakkeKey(NULL, rng);
  31450. if (ret != BAD_FUNC_ARG)
  31451. return -10210;
  31452. ret = wc_MakeSakkePublicKey(NULL, NULL);
  31453. if (ret != BAD_FUNC_ARG)
  31454. return -10211;
  31455. ret = wc_MakeSakkePublicKey(key, NULL);
  31456. if (ret != BAD_FUNC_ARG)
  31457. return -10212;
  31458. ret = wc_MakeSakkePublicKey(NULL, rsk);
  31459. if (ret != BAD_FUNC_ARG)
  31460. return -10213;
  31461. ret = wc_MakeSakkeRsk(NULL, NULL, 1, NULL);
  31462. if (ret != BAD_FUNC_ARG)
  31463. return -10214;
  31464. ret = wc_MakeSakkeRsk(key, id, 1, NULL);
  31465. if (ret != BAD_FUNC_ARG)
  31466. return -10215;
  31467. ret = wc_MakeSakkeRsk(key, NULL, 1, rsk);
  31468. if (ret != BAD_FUNC_ARG)
  31469. return -10216;
  31470. ret = wc_MakeSakkeRsk(NULL, id, 1, rsk);
  31471. if (ret != BAD_FUNC_ARG)
  31472. return -10217;
  31473. ret = wc_ValidateSakkeRsk(NULL, NULL, 1, NULL, NULL);
  31474. if (ret != BAD_FUNC_ARG)
  31475. return -10218;
  31476. ret = wc_ValidateSakkeRsk(key, id, 1, rsk, NULL);
  31477. if (ret != BAD_FUNC_ARG)
  31478. return -10219;
  31479. ret = wc_ValidateSakkeRsk(NULL, id, 1, rsk, &valid);
  31480. if (ret != BAD_FUNC_ARG)
  31481. return -10220;
  31482. ret = wc_ExportSakkeKey(NULL, NULL, NULL);
  31483. if (ret != BAD_FUNC_ARG)
  31484. return -10221;
  31485. ret = wc_ExportSakkeKey(key, data, NULL);
  31486. if (ret != BAD_FUNC_ARG)
  31487. return -10222;
  31488. ret = wc_ExportSakkeKey(NULL, data, &sz);
  31489. if (ret != BAD_FUNC_ARG)
  31490. return -10223;
  31491. ret = wc_ImportSakkeKey(NULL, NULL, 1);
  31492. if (ret != BAD_FUNC_ARG)
  31493. return -10224;
  31494. ret = wc_ImportSakkeKey(key, NULL, 1);
  31495. if (ret != BAD_FUNC_ARG)
  31496. return -10225;
  31497. ret = wc_ImportSakkeKey(NULL, data, 1);
  31498. if (ret != BAD_FUNC_ARG)
  31499. return -10226;
  31500. ret = wc_ExportSakkePrivateKey(NULL, NULL, NULL);
  31501. if (ret != BAD_FUNC_ARG)
  31502. return -10227;
  31503. ret = wc_ExportSakkePrivateKey(key, data, NULL);
  31504. if (ret != BAD_FUNC_ARG)
  31505. return -10228;
  31506. ret = wc_ExportSakkePrivateKey(NULL, data, &sz);
  31507. if (ret != BAD_FUNC_ARG)
  31508. return -10229;
  31509. ret = wc_ImportSakkePrivateKey(NULL, NULL, 1);
  31510. if (ret != BAD_FUNC_ARG)
  31511. return -10230;
  31512. ret = wc_ImportSakkePrivateKey(key, NULL, 1);
  31513. if (ret != BAD_FUNC_ARG)
  31514. return -10231;
  31515. ret = wc_ImportSakkePrivateKey(NULL, data, 1);
  31516. if (ret != BAD_FUNC_ARG)
  31517. return -10232;
  31518. sz = sizeof(data);
  31519. ret = wc_EncodeSakkeRsk(NULL, NULL, data, NULL, 1);
  31520. if (ret != BAD_FUNC_ARG)
  31521. return -10233;
  31522. ret = wc_EncodeSakkeRsk(key, rsk, data, NULL, 1);
  31523. if (ret != BAD_FUNC_ARG)
  31524. return -10234;
  31525. ret = wc_EncodeSakkeRsk(key, NULL, data, &sz, 1);
  31526. if (ret != BAD_FUNC_ARG)
  31527. return -10235;
  31528. ret = wc_EncodeSakkeRsk(NULL, rsk, data, &sz, 1);
  31529. if (ret != BAD_FUNC_ARG)
  31530. return -10236;
  31531. ret = wc_DecodeSakkeRsk(NULL, NULL, sz, NULL);
  31532. if (ret != BAD_FUNC_ARG)
  31533. return -10237;
  31534. ret = wc_DecodeSakkeRsk(key, data, sz, NULL);
  31535. if (ret != BAD_FUNC_ARG)
  31536. return -10238;
  31537. ret = wc_DecodeSakkeRsk(key, NULL, sz, rsk);
  31538. if (ret != BAD_FUNC_ARG)
  31539. return -10239;
  31540. ret = wc_DecodeSakkeRsk(NULL, data, sz, rsk);
  31541. if (ret != BAD_FUNC_ARG)
  31542. return -10240;
  31543. ret = wc_ImportSakkeRsk(NULL, NULL, sz);
  31544. if (ret != BAD_FUNC_ARG)
  31545. return -10237;
  31546. ret = wc_ImportSakkeRsk(key, NULL, sz);
  31547. if (ret != BAD_FUNC_ARG)
  31548. return -10237;
  31549. ret = wc_ImportSakkeRsk(NULL, data, sz);
  31550. if (ret != BAD_FUNC_ARG)
  31551. return -10237;
  31552. ret = wc_ImportSakkeRsk(key, data, 1);
  31553. if (ret != BUFFER_E)
  31554. return -10237;
  31555. ret = wc_GenerateSakkeRskTable(NULL, NULL, data, NULL);
  31556. if (ret != BAD_FUNC_ARG)
  31557. return -10241;
  31558. ret = wc_GenerateSakkeRskTable(key, NULL, data, NULL);
  31559. if (ret != BAD_FUNC_ARG)
  31560. return -10242;
  31561. ret = wc_GenerateSakkeRskTable(NULL, rsk, data, NULL);
  31562. if (ret != BAD_FUNC_ARG)
  31563. return -10243;
  31564. ret = wc_GenerateSakkeRskTable(NULL, NULL, data, &len);
  31565. if (ret != BAD_FUNC_ARG)
  31566. return -10244;
  31567. ret = wc_GenerateSakkeRskTable(key, rsk, data, NULL);
  31568. if (ret != BAD_FUNC_ARG)
  31569. return -10245;
  31570. ret = wc_GenerateSakkeRskTable(key, NULL, data, &len);
  31571. if (ret != BAD_FUNC_ARG)
  31572. return -10246;
  31573. ret = wc_GenerateSakkeRskTable(NULL, rsk, data, &len);
  31574. if (ret != BAD_FUNC_ARG)
  31575. return -10247;
  31576. ret = wc_GenerateSakkeRskTable(key, rsk, NULL, &len);
  31577. if (ret != LENGTH_ONLY_E)
  31578. return -10248;
  31579. len--;
  31580. ret = wc_GenerateSakkeRskTable(key, rsk, data, &len);
  31581. if (ret != BUFFER_E)
  31582. return -10249;
  31583. ret = wc_ExportSakkePublicKey(NULL, data, NULL, 1);
  31584. if (ret != BAD_FUNC_ARG)
  31585. return -10250;
  31586. ret = wc_ExportSakkePublicKey(key, data, NULL, 1);
  31587. if (ret != BAD_FUNC_ARG)
  31588. return -10251;
  31589. ret = wc_ExportSakkePublicKey(NULL, data, &sz, 1);
  31590. if (ret != BAD_FUNC_ARG)
  31591. return -10252;
  31592. ret = wc_ImportSakkePublicKey(NULL, NULL, sz, 1);
  31593. if (ret != BAD_FUNC_ARG)
  31594. return -10253;
  31595. ret = wc_ImportSakkePublicKey(key, NULL, sz, 1);
  31596. if (ret != BAD_FUNC_ARG)
  31597. return -10254;
  31598. ret = wc_ImportSakkePublicKey(NULL, data, sz, 1);
  31599. if (ret != BAD_FUNC_ARG)
  31600. return -10255;
  31601. ret = wc_GetSakkeAuthSize(NULL, NULL);
  31602. if (ret != BAD_FUNC_ARG)
  31603. return -10256;
  31604. ret = wc_GetSakkeAuthSize(key, NULL);
  31605. if (ret != BAD_FUNC_ARG)
  31606. return -10257;
  31607. ret = wc_GetSakkeAuthSize(NULL, &authSz);
  31608. if (ret != BAD_FUNC_ARG)
  31609. return -10258;
  31610. ret = wc_MakeSakkePointI(NULL, NULL, SAKKE_ID_MAX_SIZE + 1);
  31611. if (ret != BAD_FUNC_ARG)
  31612. return -10259;
  31613. ret = wc_MakeSakkePointI(key, NULL, SAKKE_ID_MAX_SIZE + 1);
  31614. if (ret != BAD_FUNC_ARG)
  31615. return -10260;
  31616. ret = wc_MakeSakkePointI(NULL, id, 1);
  31617. if (ret != BAD_FUNC_ARG)
  31618. return -10261;
  31619. ret = wc_MakeSakkePointI(NULL, NULL, 1);
  31620. if (ret != BAD_FUNC_ARG)
  31621. return -10262;
  31622. ret = wc_MakeSakkePointI(key, id, SAKKE_ID_MAX_SIZE + 1);
  31623. if (ret != BAD_FUNC_ARG)
  31624. return -10263;
  31625. ret = wc_MakeSakkePointI(key, NULL, 1);
  31626. if (ret != BAD_FUNC_ARG)
  31627. return -10264;
  31628. ret = wc_MakeSakkePointI(NULL, id, 1);
  31629. if (ret != BAD_FUNC_ARG)
  31630. return -10265;
  31631. ret = wc_GenerateSakkePointITable(NULL, data, NULL);
  31632. if (ret != BAD_FUNC_ARG)
  31633. return -10266;
  31634. ret = wc_GenerateSakkePointITable(key, data, NULL);
  31635. if (ret != BAD_FUNC_ARG)
  31636. return -10267;
  31637. ret = wc_GenerateSakkePointITable(NULL, data, &len);
  31638. if (ret != BAD_FUNC_ARG)
  31639. return -10268;
  31640. ret = wc_GenerateSakkePointITable(key, NULL, &len);
  31641. if (ret != LENGTH_ONLY_E)
  31642. return -10269;
  31643. len--;
  31644. ret = wc_GenerateSakkePointITable(key, data, &len);
  31645. if (ret != BUFFER_E)
  31646. return -10270;
  31647. ret = wc_SetSakkePointITable(NULL, NULL, 1);
  31648. if (ret != BAD_FUNC_ARG)
  31649. return -10271;
  31650. ret = wc_SetSakkePointITable(key, NULL, 1);
  31651. if (ret != BAD_FUNC_ARG)
  31652. return -10272;
  31653. ret = wc_SetSakkePointITable(NULL, data, 1);
  31654. if (ret != BAD_FUNC_ARG)
  31655. return -10273;
  31656. ret = wc_SetSakkePointITable(key, data, 1);
  31657. if (ret != BUFFER_E)
  31658. return -10274;
  31659. ret = wc_ClearSakkePointITable(NULL);
  31660. if (ret != BAD_FUNC_ARG)
  31661. return -10275;
  31662. ret = wc_GetSakkePointI(NULL, data, NULL);
  31663. if (ret != BAD_FUNC_ARG)
  31664. return -10276;
  31665. ret = wc_GetSakkePointI(key, data, NULL);
  31666. if (ret != BAD_FUNC_ARG)
  31667. return -10277;
  31668. ret = wc_GetSakkePointI(NULL, data, &sz);
  31669. if (ret != BAD_FUNC_ARG)
  31670. return -10278;
  31671. sz = 1;
  31672. ret = wc_GetSakkePointI(key, data, &sz);
  31673. if (ret != BUFFER_E)
  31674. return -10279;
  31675. sz = 256;
  31676. ret = wc_SetSakkePointI(NULL, NULL, 1, NULL, sz);
  31677. if (ret != BAD_FUNC_ARG)
  31678. return -10280;
  31679. ret = wc_SetSakkePointI(key, NULL, 1, NULL, sz);
  31680. if (ret != BAD_FUNC_ARG)
  31681. return -10281;
  31682. ret = wc_SetSakkePointI(NULL, id, 1, NULL, sz);
  31683. if (ret != BAD_FUNC_ARG)
  31684. return -10282;
  31685. ret = wc_SetSakkePointI(NULL, NULL, 1, data, sz);
  31686. if (ret != BAD_FUNC_ARG)
  31687. return -10283;
  31688. ret = wc_SetSakkePointI(key, id, 1, NULL, sz);
  31689. if (ret != BAD_FUNC_ARG)
  31690. return -10284;
  31691. ret = wc_SetSakkePointI(key, NULL, 1, data, sz);
  31692. if (ret != BAD_FUNC_ARG)
  31693. return -10285;
  31694. ret = wc_SetSakkePointI(NULL, id, 1, data, sz);
  31695. if (ret != BAD_FUNC_ARG)
  31696. return -10286;
  31697. ret = wc_SetSakkePointI(key, id, SAKKE_ID_MAX_SIZE + 1, data, sz);
  31698. if (ret != BUFFER_E)
  31699. return -10287;
  31700. ret = wc_SetSakkePointI(key, id, 1, data, sz - 1);
  31701. if (ret != BUFFER_E)
  31702. return -10288;
  31703. ret = wc_SetSakkeIdentity(NULL, NULL, 1);
  31704. if (ret != BAD_FUNC_ARG)
  31705. return -10286;
  31706. ret = wc_SetSakkeIdentity(key, NULL, 1);
  31707. if (ret != BAD_FUNC_ARG)
  31708. return -10286;
  31709. ret = wc_SetSakkeIdentity(NULL, id, 1);
  31710. if (ret != BAD_FUNC_ARG)
  31711. return -10286;
  31712. ssvSz = sizeof(ssv);
  31713. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  31714. auth, NULL);
  31715. if (ret != BAD_FUNC_ARG)
  31716. return -10289;
  31717. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  31718. auth, NULL);
  31719. if (ret != BAD_FUNC_ARG)
  31720. return -10290;
  31721. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  31722. auth, NULL);
  31723. if (ret != BAD_FUNC_ARG)
  31724. return -10291;
  31725. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  31726. auth, &authSz);
  31727. if (ret != BAD_FUNC_ARG)
  31728. return -10292;
  31729. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  31730. auth, NULL);
  31731. if (ret != BAD_FUNC_ARG)
  31732. return -10293;
  31733. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz,
  31734. auth, &authSz);
  31735. if (ret != BAD_FUNC_ARG)
  31736. return -10294;
  31737. ret = wc_MakeSakkeEncapsulatedSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  31738. auth, &authSz);
  31739. if (ret != BAD_FUNC_ARG)
  31740. return -10295;
  31741. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  31742. auth, &authSz);
  31743. if (ret != BAD_STATE_E)
  31744. return -10295;
  31745. ret = wc_GenerateSakkeSSV(NULL, NULL, data, NULL);
  31746. if (ret != BAD_FUNC_ARG)
  31747. return -10296;
  31748. ret = wc_GenerateSakkeSSV(key, rng, data, NULL);
  31749. if (ret != BAD_FUNC_ARG)
  31750. return -10297;
  31751. ret = wc_GenerateSakkeSSV(key, NULL, data, &ssvSz);
  31752. if (ret != BAD_FUNC_ARG)
  31753. return -10298;
  31754. ret = wc_GenerateSakkeSSV(NULL, rng, data, &ssvSz);
  31755. if (ret != BAD_FUNC_ARG)
  31756. return -10299;
  31757. ret = wc_SetSakkeRsk(NULL, NULL, data, 1);
  31758. if (ret != BAD_FUNC_ARG)
  31759. return -10286;
  31760. ret = wc_SetSakkeRsk(key, NULL, data, 1);
  31761. if (ret != BAD_FUNC_ARG)
  31762. return -10286;
  31763. ret = wc_SetSakkeRsk(NULL, rsk, data, 1);
  31764. if (ret != BAD_FUNC_ARG)
  31765. return -10286;
  31766. ssvSz = sizeof(ssv);
  31767. authSz = sizeof(auth);
  31768. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz, NULL,
  31769. authSz);
  31770. if (ret != BAD_FUNC_ARG)
  31771. return -10300;
  31772. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz, NULL,
  31773. authSz);
  31774. if (ret != BAD_FUNC_ARG)
  31775. return -10300;
  31776. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz, NULL,
  31777. authSz);
  31778. if (ret != BAD_FUNC_ARG)
  31779. return -10300;
  31780. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, NULL, ssvSz, auth,
  31781. authSz);
  31782. if (ret != BAD_FUNC_ARG)
  31783. return -10300;
  31784. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, NULL,
  31785. authSz);
  31786. if (ret != BAD_FUNC_ARG)
  31787. return -10300;
  31788. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, NULL, ssvSz, auth,
  31789. authSz);
  31790. if (ret != BAD_FUNC_ARG)
  31791. return -10300;
  31792. ret = wc_DeriveSakkeSSV(NULL, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  31793. authSz);
  31794. if (ret != BAD_FUNC_ARG)
  31795. return -10300;
  31796. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  31797. authSz);
  31798. if (ret != BAD_STATE_E)
  31799. return -10300;
  31800. ret = wc_SetSakkeIdentity(key, id, 1);
  31801. if (ret != 0)
  31802. return -10286;
  31803. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  31804. authSz);
  31805. if (ret != BAD_STATE_E)
  31806. return -10300;
  31807. ret = wc_SetSakkeIdentity(key, id, 0);
  31808. if (ret != 0)
  31809. return -10286;
  31810. ret = wc_SetSakkeRsk(key, rsk, data, 1);
  31811. if (ret != 0)
  31812. return -10286;
  31813. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  31814. authSz);
  31815. if (ret != BAD_STATE_E)
  31816. return -10300;
  31817. wc_FreeSakkeKey(key);
  31818. return 0;
  31819. }
  31820. static int sakke_kat_derive_test(SakkeKey* key, ecc_point* rsk)
  31821. {
  31822. WOLFSSL_SMALL_STACK_STATIC const byte pubData[] = {
  31823. 0x59, 0x58, 0xEF, 0x1B, 0x16, 0x79, 0xBF, 0x09,
  31824. 0x9B, 0x3A, 0x03, 0x0D, 0xF2, 0x55, 0xAA, 0x6A,
  31825. 0x23, 0xC1, 0xD8, 0xF1, 0x43, 0xD4, 0xD2, 0x3F,
  31826. 0x75, 0x3E, 0x69, 0xBD, 0x27, 0xA8, 0x32, 0xF3,
  31827. 0x8C, 0xB4, 0xAD, 0x53, 0xDD, 0xEF, 0x42, 0x60,
  31828. 0xB0, 0xFE, 0x8B, 0xB4, 0x5C, 0x4C, 0x1F, 0xF5,
  31829. 0x10, 0xEF, 0xFE, 0x30, 0x03, 0x67, 0xA3, 0x7B,
  31830. 0x61, 0xF7, 0x01, 0xD9, 0x14, 0xAE, 0xF0, 0x97,
  31831. 0x24, 0x82, 0x5F, 0xA0, 0x70, 0x7D, 0x61, 0xA6,
  31832. 0xDF, 0xF4, 0xFB, 0xD7, 0x27, 0x35, 0x66, 0xCD,
  31833. 0xDE, 0x35, 0x2A, 0x0B, 0x04, 0xB7, 0xC1, 0x6A,
  31834. 0x78, 0x30, 0x9B, 0xE6, 0x40, 0x69, 0x7D, 0xE7,
  31835. 0x47, 0x61, 0x3A, 0x5F, 0xC1, 0x95, 0xE8, 0xB9,
  31836. 0xF3, 0x28, 0x85, 0x2A, 0x57, 0x9D, 0xB8, 0xF9,
  31837. 0x9B, 0x1D, 0x00, 0x34, 0x47, 0x9E, 0xA9, 0xC5,
  31838. 0x59, 0x5F, 0x47, 0xC4, 0xB2, 0xF5, 0x4F, 0xF2,
  31839. 0x15, 0x08, 0xD3, 0x75, 0x14, 0xDC, 0xF7, 0xA8,
  31840. 0xE1, 0x43, 0xA6, 0x05, 0x8C, 0x09, 0xA6, 0xBF,
  31841. 0x2C, 0x98, 0x58, 0xCA, 0x37, 0xC2, 0x58, 0x06,
  31842. 0x5A, 0xE6, 0xBF, 0x75, 0x32, 0xBC, 0x8B, 0x5B,
  31843. 0x63, 0x38, 0x38, 0x66, 0xE0, 0x75, 0x3C, 0x5A,
  31844. 0xC0, 0xE7, 0x27, 0x09, 0xF8, 0x44, 0x5F, 0x2E,
  31845. 0x61, 0x78, 0xE0, 0x65, 0x85, 0x7E, 0x0E, 0xDA,
  31846. 0x10, 0xF6, 0x82, 0x06, 0xB6, 0x35, 0x05, 0xED,
  31847. 0x87, 0xE5, 0x34, 0xFB, 0x28, 0x31, 0xFF, 0x95,
  31848. 0x7F, 0xB7, 0xDC, 0x61, 0x9D, 0xAE, 0x61, 0x30,
  31849. 0x1E, 0xEA, 0xCC, 0x2F, 0xDA, 0x36, 0x80, 0xEA,
  31850. 0x49, 0x99, 0x25, 0x8A, 0x83, 0x3C, 0xEA, 0x8F,
  31851. 0xC6, 0x7C, 0x6D, 0x19, 0x48, 0x7F, 0xB4, 0x49,
  31852. 0x05, 0x9F, 0x26, 0xCC, 0x8A, 0xAB, 0x65, 0x5A,
  31853. 0xB5, 0x8B, 0x7C, 0xC7, 0x96, 0xE2, 0x4E, 0x9A,
  31854. 0x39, 0x40, 0x95, 0x75, 0x4F, 0x5F, 0x8B, 0xAE
  31855. };
  31856. WOLFSSL_SMALL_STACK_STATIC const byte rskData[] = {
  31857. 0x93, 0xAF, 0x67, 0xE5, 0x00, 0x7B, 0xA6, 0xE6,
  31858. 0xA8, 0x0D, 0xA7, 0x93, 0xDA, 0x30, 0x0F, 0xA4,
  31859. 0xB5, 0x2D, 0x0A, 0x74, 0xE2, 0x5E, 0x6E, 0x7B,
  31860. 0x2B, 0x3D, 0x6E, 0xE9, 0xD1, 0x8A, 0x9B, 0x5C,
  31861. 0x50, 0x23, 0x59, 0x7B, 0xD8, 0x2D, 0x80, 0x62,
  31862. 0xD3, 0x40, 0x19, 0x56, 0x3B, 0xA1, 0xD2, 0x5C,
  31863. 0x0D, 0xC5, 0x6B, 0x7B, 0x97, 0x9D, 0x74, 0xAA,
  31864. 0x50, 0xF2, 0x9F, 0xBF, 0x11, 0xCC, 0x2C, 0x93,
  31865. 0xF5, 0xDF, 0xCA, 0x61, 0x5E, 0x60, 0x92, 0x79,
  31866. 0xF6, 0x17, 0x5C, 0xEA, 0xDB, 0x00, 0xB5, 0x8C,
  31867. 0x6B, 0xEE, 0x1E, 0x7A, 0x2A, 0x47, 0xC4, 0xF0,
  31868. 0xC4, 0x56, 0xF0, 0x52, 0x59, 0xA6, 0xFA, 0x94,
  31869. 0xA6, 0x34, 0xA4, 0x0D, 0xAE, 0x1D, 0xF5, 0x93,
  31870. 0xD4, 0xFE, 0xCF, 0x68, 0x8D, 0x5F, 0xC6, 0x78,
  31871. 0xBE, 0x7E, 0xFC, 0x6D, 0xF3, 0xD6, 0x83, 0x53,
  31872. 0x25, 0xB8, 0x3B, 0x2C, 0x6E, 0x69, 0x03, 0x6B,
  31873. 0x15, 0x5F, 0x0A, 0x27, 0x24, 0x10, 0x94, 0xB0,
  31874. 0x4B, 0xFB, 0x0B, 0xDF, 0xAC, 0x6C, 0x67, 0x0A,
  31875. 0x65, 0xC3, 0x25, 0xD3, 0x9A, 0x06, 0x9F, 0x03,
  31876. 0x65, 0x9D, 0x44, 0xCA, 0x27, 0xD3, 0xBE, 0x8D,
  31877. 0xF3, 0x11, 0x17, 0x2B, 0x55, 0x41, 0x60, 0x18,
  31878. 0x1C, 0xBE, 0x94, 0xA2, 0xA7, 0x83, 0x32, 0x0C,
  31879. 0xED, 0x59, 0x0B, 0xC4, 0x26, 0x44, 0x70, 0x2C,
  31880. 0xF3, 0x71, 0x27, 0x1E, 0x49, 0x6B, 0xF2, 0x0F,
  31881. 0x58, 0x8B, 0x78, 0xA1, 0xBC, 0x01, 0xEC, 0xBB,
  31882. 0x65, 0x59, 0x93, 0x4B, 0xDD, 0x2F, 0xB6, 0x5D,
  31883. 0x28, 0x84, 0x31, 0x8A, 0x33, 0xD1, 0xA4, 0x2A,
  31884. 0xDF, 0x5E, 0x33, 0xCC, 0x58, 0x00, 0x28, 0x0B,
  31885. 0x28, 0x35, 0x64, 0x97, 0xF8, 0x71, 0x35, 0xBA,
  31886. 0xB9, 0x61, 0x2A, 0x17, 0x26, 0x04, 0x24, 0x40,
  31887. 0x9A, 0xC1, 0x5F, 0xEE, 0x99, 0x6B, 0x74, 0x4C,
  31888. 0x33, 0x21, 0x51, 0x23, 0x5D, 0xEC, 0xB0, 0xF5
  31889. };
  31890. WOLFSSL_SMALL_STACK_STATIC const byte id[] = {
  31891. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  31892. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  31893. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  31894. 0x33, 0x00
  31895. };
  31896. WOLFSSL_SMALL_STACK_STATIC const byte ssv[] = {
  31897. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
  31898. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0
  31899. };
  31900. WOLFSSL_SMALL_STACK_STATIC const byte auth[] = {
  31901. 0x04,
  31902. 0x44, 0xE8, 0xAD, 0x44, 0xAB, 0x85, 0x92, 0xA6,
  31903. 0xA5, 0xA3, 0xDD, 0xCA, 0x5C, 0xF8, 0x96, 0xC7,
  31904. 0x18, 0x04, 0x36, 0x06, 0xA0, 0x1D, 0x65, 0x0D,
  31905. 0xEF, 0x37, 0xA0, 0x1F, 0x37, 0xC2, 0x28, 0xC3,
  31906. 0x32, 0xFC, 0x31, 0x73, 0x54, 0xE2, 0xC2, 0x74,
  31907. 0xD4, 0xDA, 0xF8, 0xAD, 0x00, 0x10, 0x54, 0xC7,
  31908. 0x6C, 0xE5, 0x79, 0x71, 0xC6, 0xF4, 0x48, 0x6D,
  31909. 0x57, 0x23, 0x04, 0x32, 0x61, 0xC5, 0x06, 0xEB,
  31910. 0xF5, 0xBE, 0x43, 0x8F, 0x53, 0xDE, 0x04, 0xF0,
  31911. 0x67, 0xC7, 0x76, 0xE0, 0xDD, 0x3B, 0x71, 0xA6,
  31912. 0x29, 0x01, 0x33, 0x28, 0x37, 0x25, 0xA5, 0x32,
  31913. 0xF2, 0x1A, 0xF1, 0x45, 0x12, 0x6D, 0xC1, 0xD7,
  31914. 0x77, 0xEC, 0xC2, 0x7B, 0xE5, 0x08, 0x35, 0xBD,
  31915. 0x28, 0x09, 0x8B, 0x8A, 0x73, 0xD9, 0xF8, 0x01,
  31916. 0xD8, 0x93, 0x79, 0x3A, 0x41, 0xFF, 0x5C, 0x49,
  31917. 0xB8, 0x7E, 0x79, 0xF2, 0xBE, 0x4D, 0x56, 0xCE,
  31918. 0x55, 0x7E, 0x13, 0x4A, 0xD8, 0x5B, 0xB1, 0xD4,
  31919. 0xB9, 0xCE, 0x4F, 0x8B, 0xE4, 0xB0, 0x8A, 0x12,
  31920. 0xBA, 0xBF, 0x55, 0xB1, 0xD6, 0xF1, 0xD7, 0xA6,
  31921. 0x38, 0x01, 0x9E, 0xA2, 0x8E, 0x15, 0xAB, 0x1C,
  31922. 0x9F, 0x76, 0x37, 0x5F, 0xDD, 0x12, 0x10, 0xD4,
  31923. 0xF4, 0x35, 0x1B, 0x9A, 0x00, 0x94, 0x86, 0xB7,
  31924. 0xF3, 0xED, 0x46, 0xC9, 0x65, 0xDE, 0xD2, 0xD8,
  31925. 0x0D, 0xAD, 0xE4, 0xF3, 0x8C, 0x67, 0x21, 0xD5,
  31926. 0x2C, 0x3A, 0xD1, 0x03, 0xA1, 0x0E, 0xBD, 0x29,
  31927. 0x59, 0x24, 0x8B, 0x4E, 0xF0, 0x06, 0x83, 0x6B,
  31928. 0xF0, 0x97, 0x44, 0x8E, 0x61, 0x07, 0xC9, 0xED,
  31929. 0xEE, 0x9F, 0xB7, 0x04, 0x82, 0x3D, 0xF1, 0x99,
  31930. 0xF8, 0x32, 0xC9, 0x05, 0xAE, 0x45, 0xF8, 0xA2,
  31931. 0x47, 0xA0, 0x72, 0xD8, 0xEF, 0x72, 0x9E, 0xAB,
  31932. 0xC5, 0xE2, 0x75, 0x74, 0xB0, 0x77, 0x39, 0xB3,
  31933. 0x4B, 0xE7, 0x4A, 0x53, 0x2F, 0x74, 0x7B, 0x86
  31934. };
  31935. WOLFSSL_SMALL_STACK_STATIC const byte encSsv[] = {
  31936. 0x89, 0xE0, 0xBC, 0x66, 0x1A, 0xA1, 0xE9, 0x16,
  31937. 0x38, 0xE6, 0xAC, 0xC8, 0x4E, 0x49, 0x65, 0x07
  31938. };
  31939. int ret;
  31940. int valid;
  31941. byte pubKey[sizeof(pubData) + 1];
  31942. word32 sz = sizeof(pubKey);
  31943. byte tmpSsv[sizeof(encSsv)];
  31944. byte* iTable = NULL;
  31945. word32 iTableLen;
  31946. byte* table = NULL;
  31947. word32 len;
  31948. ret = wc_ImportSakkePublicKey(key, pubData, sizeof(pubData), 0);
  31949. if (ret != 0)
  31950. return -10315;
  31951. ret = wc_DecodeSakkeRsk(key, rskData, sizeof(rskData), rsk);
  31952. if (ret != 0)
  31953. return -10316;
  31954. ret = wc_ValidateSakkeRsk(key, id, sizeof(id), rsk, &valid);
  31955. if (ret != 0)
  31956. return -10317;
  31957. if (valid != 1)
  31958. return -10318;
  31959. ret = wc_SetSakkeRsk(key, rsk, NULL, 0);
  31960. if (ret != 0)
  31961. return -10319;
  31962. ret = wc_SetSakkeIdentity(key, id, sizeof(id));
  31963. if (ret != 0)
  31964. return -10319;
  31965. XMEMCPY(tmpSsv, encSsv, sizeof(encSsv));
  31966. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, tmpSsv, sizeof(tmpSsv),
  31967. auth, sizeof(auth));
  31968. if (ret != 0)
  31969. return -10322;
  31970. if (XMEMCMP(tmpSsv, ssv, sizeof(ssv)) != 0)
  31971. return -10320;
  31972. ret = wc_MakeSakkePointI(key, id, sizeof(id));
  31973. if (ret != 0)
  31974. return -10321;
  31975. iTableLen = 0;
  31976. ret = wc_GenerateSakkePointITable(key, NULL, &iTableLen);
  31977. if (ret != LENGTH_ONLY_E)
  31978. return -10322;
  31979. if (iTableLen != 0) {
  31980. iTable = (byte*)XMALLOC(iTableLen, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31981. if (iTable == NULL)
  31982. return -10323;
  31983. ret = wc_GenerateSakkePointITable(key, iTable, &iTableLen);
  31984. if (ret != 0)
  31985. return -10324;
  31986. }
  31987. len = 0;
  31988. ret = wc_GenerateSakkeRskTable(key, rsk, NULL, &len);
  31989. if (ret != LENGTH_ONLY_E)
  31990. return -10325;
  31991. if (len > 0) {
  31992. table = (byte*)XMALLOC(len, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  31993. if (table == NULL)
  31994. return -10326;
  31995. ret = wc_GenerateSakkeRskTable(key, rsk, table, &len);
  31996. if (ret != 0)
  31997. return -10327;
  31998. }
  31999. ret = wc_SetSakkeRsk(key, rsk, table, len);
  32000. if (ret != 0)
  32001. return -10319;
  32002. XMEMCPY(tmpSsv, encSsv, sizeof(encSsv));
  32003. ret = wc_DeriveSakkeSSV(key, WC_HASH_TYPE_SHA256, tmpSsv, sizeof(tmpSsv),
  32004. auth, sizeof(auth));
  32005. if (ret != 0)
  32006. return -10328;
  32007. if (XMEMCMP(tmpSsv, ssv, sizeof(ssv)) != 0)
  32008. return -10329;
  32009. /* Don't reference table that is about to be freed. */
  32010. ret = wc_ClearSakkePointITable(key);
  32011. if (ret != 0)
  32012. return -10330;
  32013. /* Dispose of tables */
  32014. if (iTable != NULL)
  32015. XFREE(iTable, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32016. if (table != NULL)
  32017. XFREE(table, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32018. /* Make sure the key public key is exportable - convert to Montgomery form
  32019. * in Validation.
  32020. */
  32021. ret = wc_ExportSakkePublicKey(key, pubKey, &sz, 1);
  32022. if (ret != 0)
  32023. return -10331;
  32024. if (sz != sizeof(pubData))
  32025. return -10332;
  32026. if (XMEMCMP(pubKey, pubData, sizeof(pubData)) != 0)
  32027. return -10333;
  32028. sz = sizeof(pubData) + 1;
  32029. ret = wc_ExportSakkePublicKey(key, pubKey, &sz, 0);
  32030. if (ret != 0)
  32031. return -10334;
  32032. if (sz != sizeof(pubData) + 1)
  32033. return -10335;
  32034. if (pubKey[0] != 0x04)
  32035. return -10336;
  32036. if (XMEMCMP(pubKey + 1, pubData, sizeof(pubData)) != 0)
  32037. return -10337;
  32038. return 0;
  32039. }
  32040. static int sakke_kat_encapsulate_test(SakkeKey* key)
  32041. {
  32042. static const byte pubData[] = {
  32043. 0x59, 0x58, 0xEF, 0x1B, 0x16, 0x79, 0xBF, 0x09,
  32044. 0x9B, 0x3A, 0x03, 0x0D, 0xF2, 0x55, 0xAA, 0x6A,
  32045. 0x23, 0xC1, 0xD8, 0xF1, 0x43, 0xD4, 0xD2, 0x3F,
  32046. 0x75, 0x3E, 0x69, 0xBD, 0x27, 0xA8, 0x32, 0xF3,
  32047. 0x8C, 0xB4, 0xAD, 0x53, 0xDD, 0xEF, 0x42, 0x60,
  32048. 0xB0, 0xFE, 0x8B, 0xB4, 0x5C, 0x4C, 0x1F, 0xF5,
  32049. 0x10, 0xEF, 0xFE, 0x30, 0x03, 0x67, 0xA3, 0x7B,
  32050. 0x61, 0xF7, 0x01, 0xD9, 0x14, 0xAE, 0xF0, 0x97,
  32051. 0x24, 0x82, 0x5F, 0xA0, 0x70, 0x7D, 0x61, 0xA6,
  32052. 0xDF, 0xF4, 0xFB, 0xD7, 0x27, 0x35, 0x66, 0xCD,
  32053. 0xDE, 0x35, 0x2A, 0x0B, 0x04, 0xB7, 0xC1, 0x6A,
  32054. 0x78, 0x30, 0x9B, 0xE6, 0x40, 0x69, 0x7D, 0xE7,
  32055. 0x47, 0x61, 0x3A, 0x5F, 0xC1, 0x95, 0xE8, 0xB9,
  32056. 0xF3, 0x28, 0x85, 0x2A, 0x57, 0x9D, 0xB8, 0xF9,
  32057. 0x9B, 0x1D, 0x00, 0x34, 0x47, 0x9E, 0xA9, 0xC5,
  32058. 0x59, 0x5F, 0x47, 0xC4, 0xB2, 0xF5, 0x4F, 0xF2,
  32059. 0x15, 0x08, 0xD3, 0x75, 0x14, 0xDC, 0xF7, 0xA8,
  32060. 0xE1, 0x43, 0xA6, 0x05, 0x8C, 0x09, 0xA6, 0xBF,
  32061. 0x2C, 0x98, 0x58, 0xCA, 0x37, 0xC2, 0x58, 0x06,
  32062. 0x5A, 0xE6, 0xBF, 0x75, 0x32, 0xBC, 0x8B, 0x5B,
  32063. 0x63, 0x38, 0x38, 0x66, 0xE0, 0x75, 0x3C, 0x5A,
  32064. 0xC0, 0xE7, 0x27, 0x09, 0xF8, 0x44, 0x5F, 0x2E,
  32065. 0x61, 0x78, 0xE0, 0x65, 0x85, 0x7E, 0x0E, 0xDA,
  32066. 0x10, 0xF6, 0x82, 0x06, 0xB6, 0x35, 0x05, 0xED,
  32067. 0x87, 0xE5, 0x34, 0xFB, 0x28, 0x31, 0xFF, 0x95,
  32068. 0x7F, 0xB7, 0xDC, 0x61, 0x9D, 0xAE, 0x61, 0x30,
  32069. 0x1E, 0xEA, 0xCC, 0x2F, 0xDA, 0x36, 0x80, 0xEA,
  32070. 0x49, 0x99, 0x25, 0x8A, 0x83, 0x3C, 0xEA, 0x8F,
  32071. 0xC6, 0x7C, 0x6D, 0x19, 0x48, 0x7F, 0xB4, 0x49,
  32072. 0x05, 0x9F, 0x26, 0xCC, 0x8A, 0xAB, 0x65, 0x5A,
  32073. 0xB5, 0x8B, 0x7C, 0xC7, 0x96, 0xE2, 0x4E, 0x9A,
  32074. 0x39, 0x40, 0x95, 0x75, 0x4F, 0x5F, 0x8B, 0xAE
  32075. };
  32076. static const byte id[] = {
  32077. 0x32, 0x30, 0x31, 0x31, 0x2D, 0x30, 0x32, 0x00,
  32078. 0x74, 0x65, 0x6C, 0x3A, 0x2B, 0x34, 0x34, 0x37,
  32079. 0x37, 0x30, 0x30, 0x39, 0x30, 0x30, 0x31, 0x32,
  32080. 0x33, 0x00
  32081. };
  32082. static word32 idSz = sizeof(id);
  32083. byte ssv[] = {
  32084. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0,
  32085. 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0
  32086. };
  32087. static word16 ssvSz = sizeof(ssv);
  32088. static const byte expAuth[] = {
  32089. 0x04,
  32090. 0x44, 0xE8, 0xAD, 0x44, 0xAB, 0x85, 0x92, 0xA6,
  32091. 0xA5, 0xA3, 0xDD, 0xCA, 0x5C, 0xF8, 0x96, 0xC7,
  32092. 0x18, 0x04, 0x36, 0x06, 0xA0, 0x1D, 0x65, 0x0D,
  32093. 0xEF, 0x37, 0xA0, 0x1F, 0x37, 0xC2, 0x28, 0xC3,
  32094. 0x32, 0xFC, 0x31, 0x73, 0x54, 0xE2, 0xC2, 0x74,
  32095. 0xD4, 0xDA, 0xF8, 0xAD, 0x00, 0x10, 0x54, 0xC7,
  32096. 0x6C, 0xE5, 0x79, 0x71, 0xC6, 0xF4, 0x48, 0x6D,
  32097. 0x57, 0x23, 0x04, 0x32, 0x61, 0xC5, 0x06, 0xEB,
  32098. 0xF5, 0xBE, 0x43, 0x8F, 0x53, 0xDE, 0x04, 0xF0,
  32099. 0x67, 0xC7, 0x76, 0xE0, 0xDD, 0x3B, 0x71, 0xA6,
  32100. 0x29, 0x01, 0x33, 0x28, 0x37, 0x25, 0xA5, 0x32,
  32101. 0xF2, 0x1A, 0xF1, 0x45, 0x12, 0x6D, 0xC1, 0xD7,
  32102. 0x77, 0xEC, 0xC2, 0x7B, 0xE5, 0x08, 0x35, 0xBD,
  32103. 0x28, 0x09, 0x8B, 0x8A, 0x73, 0xD9, 0xF8, 0x01,
  32104. 0xD8, 0x93, 0x79, 0x3A, 0x41, 0xFF, 0x5C, 0x49,
  32105. 0xB8, 0x7E, 0x79, 0xF2, 0xBE, 0x4D, 0x56, 0xCE,
  32106. 0x55, 0x7E, 0x13, 0x4A, 0xD8, 0x5B, 0xB1, 0xD4,
  32107. 0xB9, 0xCE, 0x4F, 0x8B, 0xE4, 0xB0, 0x8A, 0x12,
  32108. 0xBA, 0xBF, 0x55, 0xB1, 0xD6, 0xF1, 0xD7, 0xA6,
  32109. 0x38, 0x01, 0x9E, 0xA2, 0x8E, 0x15, 0xAB, 0x1C,
  32110. 0x9F, 0x76, 0x37, 0x5F, 0xDD, 0x12, 0x10, 0xD4,
  32111. 0xF4, 0x35, 0x1B, 0x9A, 0x00, 0x94, 0x86, 0xB7,
  32112. 0xF3, 0xED, 0x46, 0xC9, 0x65, 0xDE, 0xD2, 0xD8,
  32113. 0x0D, 0xAD, 0xE4, 0xF3, 0x8C, 0x67, 0x21, 0xD5,
  32114. 0x2C, 0x3A, 0xD1, 0x03, 0xA1, 0x0E, 0xBD, 0x29,
  32115. 0x59, 0x24, 0x8B, 0x4E, 0xF0, 0x06, 0x83, 0x6B,
  32116. 0xF0, 0x97, 0x44, 0x8E, 0x61, 0x07, 0xC9, 0xED,
  32117. 0xEE, 0x9F, 0xB7, 0x04, 0x82, 0x3D, 0xF1, 0x99,
  32118. 0xF8, 0x32, 0xC9, 0x05, 0xAE, 0x45, 0xF8, 0xA2,
  32119. 0x47, 0xA0, 0x72, 0xD8, 0xEF, 0x72, 0x9E, 0xAB,
  32120. 0xC5, 0xE2, 0x75, 0x74, 0xB0, 0x77, 0x39, 0xB3,
  32121. 0x4B, 0xE7, 0x4A, 0x53, 0x2F, 0x74, 0x7B, 0x86
  32122. };
  32123. static const byte encSsv[] = {
  32124. 0x89, 0xE0, 0xBC, 0x66, 0x1A, 0xA1, 0xE9, 0x16,
  32125. 0x38, 0xE6, 0xAC, 0xC8, 0x4E, 0x49, 0x65, 0x07
  32126. };
  32127. int ret;
  32128. byte auth[257];
  32129. word16 authSz = sizeof(auth);
  32130. ret = wc_ImportSakkePublicKey(key, pubData, sizeof(pubData), 0);
  32131. if (ret != 0)
  32132. return -10334;
  32133. ret = wc_SetSakkeIdentity(key, id, idSz);
  32134. if (ret != 0)
  32135. return -10335;
  32136. ret = wc_MakeSakkeEncapsulatedSSV(key, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  32137. auth, &authSz);
  32138. if (ret != 0)
  32139. return -10336;
  32140. if (authSz != 257)
  32141. return -10337;
  32142. if (XMEMCMP(ssv, encSsv, ssvSz) != 0)
  32143. return -10338;
  32144. if (XMEMCMP(auth, expAuth, authSz) != 0)
  32145. return -10339;
  32146. return 0;
  32147. }
  32148. static int sakke_make_key_test(SakkeKey* priv, SakkeKey* pub, SakkeKey* key,
  32149. WC_RNG* rng, ecc_point* rsk)
  32150. {
  32151. int ret;
  32152. byte data[440];
  32153. byte pubData[257];
  32154. word32 sz;
  32155. char mail[] = "test@wolfssl.com";
  32156. byte* id = (byte*)mail;
  32157. word32 idSz = (word32)XSTRLEN(mail);
  32158. int valid;
  32159. ecc_point* pubKey = rsk;
  32160. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  32161. if (ret != 0)
  32162. return -10339;
  32163. ret = wc_MakeSakkeKey(priv, rng);
  32164. if (ret != 0)
  32165. return -10340;
  32166. ret = wc_ExportSakkeKey(priv, NULL, &sz);
  32167. if (ret != LENGTH_ONLY_E)
  32168. return -10341;
  32169. if (sz != 384)
  32170. return -10342;
  32171. sz--;
  32172. ret = wc_ExportSakkeKey(priv, data, &sz);
  32173. if (ret == 0)
  32174. return -10343;
  32175. sz++;
  32176. ret = wc_ExportSakkeKey(priv, data, &sz);
  32177. if (ret != 0)
  32178. return -10344;
  32179. if (sz != 384)
  32180. return -10345;
  32181. ret = wc_ImportSakkeKey(key, data, sz - 1);
  32182. if (ret == 0)
  32183. return -10346;
  32184. ret = wc_ImportSakkeKey(key, data, sz);
  32185. if (ret != 0)
  32186. return -10347;
  32187. wc_FreeSakkeKey(key);
  32188. ret = wc_InitSakkeKey_ex(key, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  32189. if (ret != 0)
  32190. return -10348;
  32191. ret = wc_ExportSakkePrivateKey(priv, NULL, &sz);
  32192. if (ret != LENGTH_ONLY_E)
  32193. return -10349;
  32194. if (sz != 128)
  32195. return -10350;
  32196. sz--;
  32197. ret = wc_ExportSakkePrivateKey(priv, data, &sz);
  32198. if (ret == 0)
  32199. return -10351;
  32200. sz++;
  32201. ret = wc_ExportSakkePrivateKey(priv, data, &sz);
  32202. if (ret != 0)
  32203. return -10352;
  32204. if (sz != 128)
  32205. return -10353;
  32206. ret = wc_ImportSakkePrivateKey(key, data, sz - 1);
  32207. if (ret == 0)
  32208. return -10354;
  32209. ret = wc_ImportSakkePrivateKey(key, data, sz);
  32210. if (ret != 0)
  32211. return -10355;
  32212. ret = wc_MakeSakkePublicKey(key, pubKey);
  32213. if (ret != 0)
  32214. return -10356;
  32215. ret = wc_ExportSakkePublicKey(priv, NULL, &sz, 1);
  32216. if (ret != LENGTH_ONLY_E)
  32217. return -10357;
  32218. if (sz != 256)
  32219. return -10358;
  32220. sz--;
  32221. ret = wc_ExportSakkePublicKey(priv, data, &sz, 1);
  32222. if (ret == 0)
  32223. return -10359;
  32224. sz++;
  32225. ret = wc_ExportSakkePublicKey(priv, data, &sz, 1);
  32226. if (ret != 0)
  32227. return -10360;
  32228. if (sz != 256)
  32229. return -10361;
  32230. ret = wc_ImportSakkePublicKey(pub, data, sz - 1, 1);
  32231. if (ret == 0)
  32232. return -10362;
  32233. ret = wc_ImportSakkePublicKey(pub, data, sz, 1);
  32234. if (ret != 0)
  32235. return -10363;
  32236. ret = wc_ExportSakkePublicKey(pub, pubData, &sz, 1);
  32237. if (ret != 0)
  32238. return -10364;
  32239. if (sz != 256)
  32240. return -10365;
  32241. if (XMEMCMP(data, pubData, sz) != 0)
  32242. return -10366;
  32243. ret = wc_MakeSakkeRsk(priv, id, idSz, rsk);
  32244. if (ret != 0)
  32245. return -10367;
  32246. ret = wc_ValidateSakkeRsk(priv, id, idSz, rsk, &valid);
  32247. if (ret != 0)
  32248. return -10368;
  32249. if (valid != 1)
  32250. return -10369;
  32251. ret = wc_ValidateSakkeRsk(pub, id, idSz, rsk, &valid);
  32252. if (ret != 0)
  32253. return -10370;
  32254. if (valid != 1)
  32255. return -10371;
  32256. sz = sizeof(data);
  32257. ret = wc_EncodeSakkeRsk(priv, rsk, data, &sz, 1);
  32258. if (ret != 0)
  32259. return -10372;
  32260. if (sz != 256)
  32261. return -10373;
  32262. ret = wc_DecodeSakkeRsk(priv, data, sz, rsk);
  32263. if (ret != 0)
  32264. return -10374;
  32265. sz = sizeof(pubData);
  32266. ret = wc_EncodeSakkeRsk(priv, rsk, pubData, &sz, 0);
  32267. if (ret != 0)
  32268. return -10375;
  32269. if (sz != sizeof(pubData))
  32270. return -10376;
  32271. ret = wc_DecodeSakkeRsk(priv, pubData, sz, rsk);
  32272. if (ret != 0)
  32273. return -10377;
  32274. wc_FreeSakkeKey(key);
  32275. return 0;
  32276. }
  32277. static int sakke_op_test(SakkeKey* priv, SakkeKey* pub, WC_RNG* rng,
  32278. ecc_point* rsk)
  32279. {
  32280. int ret;
  32281. byte ssv[16];
  32282. word16 ssvSz;
  32283. byte auth[257];
  32284. word16 authSz;
  32285. char mail[] = "test@wolfssl.com";
  32286. byte* id = (byte*)mail;
  32287. word32 idSz = (word32)XSTRLEN(mail);
  32288. byte pointI[256];
  32289. word32 sz;
  32290. ret = wc_GenerateSakkeSSV(pub, rng, NULL, &ssvSz);
  32291. if (ret != LENGTH_ONLY_E)
  32292. return -10375;
  32293. if (ssvSz != 16)
  32294. return -10376;
  32295. ssvSz += 128;
  32296. ret = wc_GenerateSakkeSSV(pub, rng, ssv, &ssvSz);
  32297. if (ret == 0)
  32298. return -10377;
  32299. ssvSz -= 128;
  32300. ret = wc_GenerateSakkeSSV(pub, rng, ssv, &ssvSz);
  32301. if (ret != 0)
  32302. return -10378;
  32303. if (ssvSz != 16)
  32304. return -10379;
  32305. ret = wc_GetSakkeAuthSize(pub, &authSz);
  32306. if (ret != 0)
  32307. return -10380;
  32308. ret = wc_SetSakkeIdentity(pub, id, idSz);
  32309. if (ret != 0)
  32310. return -10380;
  32311. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  32312. NULL, &authSz);
  32313. if (ret != LENGTH_ONLY_E)
  32314. return -10381;
  32315. if (authSz != 257)
  32316. return -10382;
  32317. authSz--;
  32318. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  32319. auth, &authSz);
  32320. if (ret == 0)
  32321. return -10383;
  32322. authSz++;
  32323. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  32324. auth, &authSz);
  32325. if (ret != 0)
  32326. return -10384;
  32327. if (authSz != 257)
  32328. return -10385;
  32329. ret = wc_GetSakkePointI(pub, NULL, &sz);
  32330. if (ret != LENGTH_ONLY_E)
  32331. return -10386;
  32332. if (sz != 256)
  32333. return -10387;
  32334. ret = wc_GetSakkePointI(pub, pointI, &sz);
  32335. if (ret != 0)
  32336. return -10388;
  32337. if (sz != 256)
  32338. return -10389;
  32339. /* Bogus identity - make it check and regenerate I. */
  32340. ret = wc_MakeSakkePointI(pub, ssv, ssvSz);
  32341. if (ret != 0)
  32342. return -10391;
  32343. ret = wc_MakeSakkeEncapsulatedSSV(pub, WC_HASH_TYPE_SHA256, ssv, ssvSz,
  32344. auth, &authSz);
  32345. if (ret != 0)
  32346. return -10392;
  32347. if (authSz != 257)
  32348. return -10393;
  32349. ret = wc_SetSakkeRsk(priv, rsk, NULL, 0);
  32350. if (ret != 0)
  32351. return -10392;
  32352. ret = wc_SetSakkeIdentity(priv, id, idSz);
  32353. if (ret != 0)
  32354. return -10392;
  32355. authSz--;
  32356. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  32357. authSz);
  32358. if (ret == 0)
  32359. return -10394;
  32360. authSz++;
  32361. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  32362. authSz);
  32363. if (ret != 0)
  32364. return -10395;
  32365. ssv[0] ^= 0x80;
  32366. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  32367. authSz);
  32368. if (ret != SAKKE_VERIFY_FAIL_E)
  32369. return -10396;
  32370. ssv[0] ^= 0x80;
  32371. /* Bogus identity - make it check and regenerate I. */
  32372. ret = wc_MakeSakkePointI(pub, ssv, idSz);
  32373. if (ret != 0)
  32374. return -10397;
  32375. ret = wc_DeriveSakkeSSV(priv, WC_HASH_TYPE_SHA256, ssv, ssvSz, auth,
  32376. authSz);
  32377. if (ret != 0)
  32378. return -10398;
  32379. return 0;
  32380. }
  32381. int sakke_test(void)
  32382. {
  32383. int ret = 0;
  32384. WC_RNG rng;
  32385. int rng_inited = 0;
  32386. SakkeKey* priv = NULL;
  32387. SakkeKey* pub = NULL;
  32388. SakkeKey* key = NULL;
  32389. ecc_point* rsk = NULL;
  32390. priv = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  32391. DYNAMIC_TYPE_TMP_BUFFER);
  32392. if (priv == NULL)
  32393. ret = -10404;
  32394. else
  32395. XMEMSET(priv, 0, sizeof(*priv));
  32396. if (ret == 0) {
  32397. pub = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  32398. DYNAMIC_TYPE_TMP_BUFFER);
  32399. if (pub == NULL)
  32400. ret = -10405;
  32401. else
  32402. XMEMSET(pub, 0, sizeof(*pub));
  32403. }
  32404. if (ret == 0) {
  32405. key = (SakkeKey*)XMALLOC(sizeof(SakkeKey), HEAP_HINT,
  32406. DYNAMIC_TYPE_TMP_BUFFER);
  32407. if (key == NULL)
  32408. ret = -10406;
  32409. else
  32410. XMEMSET(key, 0, sizeof(*key));
  32411. }
  32412. if (ret == 0) {
  32413. #ifndef HAVE_FIPS
  32414. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  32415. #else
  32416. ret = wc_InitRng(&rng);
  32417. #endif
  32418. if (ret == 0)
  32419. rng_inited = 1;
  32420. else
  32421. ret = -10400;
  32422. }
  32423. if (ret == 0) {
  32424. rsk = wc_ecc_new_point();
  32425. if (rsk == NULL)
  32426. ret = -10401;
  32427. }
  32428. if (ret == 0) {
  32429. ret = wc_InitSakkeKey(pub, HEAP_HINT, INVALID_DEVID);
  32430. if (ret != 0)
  32431. ret = -10402;
  32432. }
  32433. if (ret == 0) {
  32434. ret = wc_InitSakkeKey(priv, HEAP_HINT, INVALID_DEVID);
  32435. if (ret != 0)
  32436. ret = -10403;
  32437. }
  32438. if (ret == 0) {
  32439. ret = sakke_api_test(&rng, key, rsk);
  32440. }
  32441. if (ret == 0) {
  32442. ret = sakke_kat_derive_test(pub, rsk);
  32443. }
  32444. if (ret == 0) {
  32445. ret = sakke_kat_encapsulate_test(pub);
  32446. }
  32447. if (ret == 0) {
  32448. ret = sakke_make_key_test(priv, pub, key, &rng, rsk);
  32449. }
  32450. if (ret == 0) {
  32451. ret = sakke_op_test(priv, pub, &rng, rsk);
  32452. }
  32453. if (rsk != NULL) {
  32454. wc_ecc_forcezero_point(rsk);
  32455. wc_ecc_del_point(rsk);
  32456. }
  32457. if (rng_inited)
  32458. wc_FreeRng(&rng);
  32459. if (key != NULL)
  32460. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32461. if (pub != NULL) {
  32462. wc_FreeSakkeKey(pub);
  32463. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32464. }
  32465. if (priv != NULL) {
  32466. wc_FreeSakkeKey(priv);
  32467. XFREE(priv, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32468. }
  32469. return ret;
  32470. }
  32471. #endif /* WOLFCRYPT_HAVE_SAKKE */
  32472. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  32473. typedef struct CMAC_Test_Case {
  32474. int type;
  32475. int partial;
  32476. const byte* m;
  32477. word32 mSz;
  32478. const byte* k;
  32479. word32 kSz;
  32480. const byte* t;
  32481. word32 tSz;
  32482. } CMAC_Test_Case;
  32483. WOLFSSL_TEST_SUBROUTINE int cmac_test(void)
  32484. {
  32485. #ifdef WOLFSSL_AES_128
  32486. WOLFSSL_SMALL_STACK_STATIC const byte k128[] =
  32487. {
  32488. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  32489. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  32490. };
  32491. #define KLEN_128 (sizeof(k128))
  32492. #endif
  32493. #ifdef WOLFSSL_AES_192
  32494. WOLFSSL_SMALL_STACK_STATIC const byte k192[] =
  32495. {
  32496. 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52,
  32497. 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
  32498. 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b
  32499. };
  32500. #define KLEN_192 (sizeof(k192))
  32501. #endif
  32502. #ifdef WOLFSSL_AES_256
  32503. WOLFSSL_SMALL_STACK_STATIC const byte k256[] =
  32504. {
  32505. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  32506. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  32507. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  32508. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  32509. };
  32510. #define KLEN_256 (sizeof(k256))
  32511. #endif
  32512. WOLFSSL_SMALL_STACK_STATIC const byte m[] =
  32513. {
  32514. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  32515. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  32516. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  32517. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  32518. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
  32519. 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  32520. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
  32521. 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
  32522. };
  32523. #define MLEN_0 (0)
  32524. #define MLEN_128 (128/8)
  32525. #define MLEN_320 (320/8)
  32526. #define MLEN_319 (MLEN_320 - 1)
  32527. #define MLEN_512 (512/8)
  32528. #ifdef WOLFSSL_AES_128
  32529. WOLFSSL_SMALL_STACK_STATIC const byte t128_0[] =
  32530. {
  32531. 0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28,
  32532. 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46
  32533. };
  32534. WOLFSSL_SMALL_STACK_STATIC const byte t128_128[] =
  32535. {
  32536. 0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
  32537. 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c
  32538. };
  32539. WOLFSSL_SMALL_STACK_STATIC const byte t128_319[] =
  32540. {
  32541. 0x2c, 0x17, 0x84, 0x4c, 0x93, 0x1c, 0x07, 0x95,
  32542. 0x15, 0x92, 0x73, 0x0a, 0x34, 0xd0, 0xd9, 0xd2
  32543. };
  32544. WOLFSSL_SMALL_STACK_STATIC const byte t128_320[] =
  32545. {
  32546. 0xdf, 0xa6, 0x67, 0x47, 0xde, 0x9a, 0xe6, 0x30,
  32547. 0x30, 0xca, 0x32, 0x61, 0x14, 0x97, 0xc8, 0x27
  32548. };
  32549. WOLFSSL_SMALL_STACK_STATIC const byte t128_512[] =
  32550. {
  32551. 0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92,
  32552. 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe
  32553. };
  32554. #endif
  32555. #ifdef WOLFSSL_AES_192
  32556. WOLFSSL_SMALL_STACK_STATIC const byte t192_0[] =
  32557. {
  32558. 0xd1, 0x7d, 0xdf, 0x46, 0xad, 0xaa, 0xcd, 0xe5,
  32559. 0x31, 0xca, 0xc4, 0x83, 0xde, 0x7a, 0x93, 0x67
  32560. };
  32561. WOLFSSL_SMALL_STACK_STATIC const byte t192_128[] =
  32562. {
  32563. 0x9e, 0x99, 0xa7, 0xbf, 0x31, 0xe7, 0x10, 0x90,
  32564. 0x06, 0x62, 0xf6, 0x5e, 0x61, 0x7c, 0x51, 0x84
  32565. };
  32566. WOLFSSL_SMALL_STACK_STATIC const byte t192_320[] =
  32567. {
  32568. 0x8a, 0x1d, 0xe5, 0xbe, 0x2e, 0xb3, 0x1a, 0xad,
  32569. 0x08, 0x9a, 0x82, 0xe6, 0xee, 0x90, 0x8b, 0x0e
  32570. };
  32571. WOLFSSL_SMALL_STACK_STATIC const byte t192_512[] =
  32572. {
  32573. 0xa1, 0xd5, 0xdf, 0x0e, 0xed, 0x79, 0x0f, 0x79,
  32574. 0x4d, 0x77, 0x58, 0x96, 0x59, 0xf3, 0x9a, 0x11
  32575. };
  32576. #endif
  32577. #ifdef WOLFSSL_AES_256
  32578. WOLFSSL_SMALL_STACK_STATIC const byte t256_0[] =
  32579. {
  32580. 0x02, 0x89, 0x62, 0xf6, 0x1b, 0x7b, 0xf8, 0x9e,
  32581. 0xfc, 0x6b, 0x55, 0x1f, 0x46, 0x67, 0xd9, 0x83
  32582. };
  32583. WOLFSSL_SMALL_STACK_STATIC const byte t256_128[] =
  32584. {
  32585. 0x28, 0xa7, 0x02, 0x3f, 0x45, 0x2e, 0x8f, 0x82,
  32586. 0xbd, 0x4b, 0xf2, 0x8d, 0x8c, 0x37, 0xc3, 0x5c
  32587. };
  32588. WOLFSSL_SMALL_STACK_STATIC const byte t256_320[] =
  32589. {
  32590. 0xaa, 0xf3, 0xd8, 0xf1, 0xde, 0x56, 0x40, 0xc2,
  32591. 0x32, 0xf5, 0xb1, 0x69, 0xb9, 0xc9, 0x11, 0xe6
  32592. };
  32593. WOLFSSL_SMALL_STACK_STATIC const byte t256_512[] =
  32594. {
  32595. 0xe1, 0x99, 0x21, 0x90, 0x54, 0x9f, 0x6e, 0xd5,
  32596. 0x69, 0x6a, 0x2c, 0x05, 0x6c, 0x31, 0x54, 0x10
  32597. };
  32598. #endif
  32599. const CMAC_Test_Case testCases[] =
  32600. {
  32601. #ifdef WOLFSSL_AES_128
  32602. {WC_CMAC_AES, 0, m, MLEN_0, k128, KLEN_128, t128_0, AES_BLOCK_SIZE},
  32603. {WC_CMAC_AES, 0, m, MLEN_128, k128, KLEN_128, t128_128, AES_BLOCK_SIZE},
  32604. {WC_CMAC_AES, 0, m, MLEN_320, k128, KLEN_128, t128_320, AES_BLOCK_SIZE},
  32605. {WC_CMAC_AES, 0, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  32606. {WC_CMAC_AES, 5, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  32607. #endif
  32608. #ifdef WOLFSSL_AES_192
  32609. {WC_CMAC_AES, 0, m, MLEN_0, k192, KLEN_192, t192_0, AES_BLOCK_SIZE},
  32610. {WC_CMAC_AES, 0, m, MLEN_128, k192, KLEN_192, t192_128, AES_BLOCK_SIZE},
  32611. {WC_CMAC_AES, 0, m, MLEN_320, k192, KLEN_192, t192_320, AES_BLOCK_SIZE},
  32612. {WC_CMAC_AES, 0, m, MLEN_512, k192, KLEN_192, t192_512, AES_BLOCK_SIZE},
  32613. #endif
  32614. #ifdef WOLFSSL_AES_256
  32615. {WC_CMAC_AES, 0, m, MLEN_0, k256, KLEN_256, t256_0, AES_BLOCK_SIZE},
  32616. {WC_CMAC_AES, 0, m, MLEN_128, k256, KLEN_256, t256_128, AES_BLOCK_SIZE},
  32617. {WC_CMAC_AES, 0, m, MLEN_320, k256, KLEN_256, t256_320, AES_BLOCK_SIZE},
  32618. {WC_CMAC_AES, 0, m, MLEN_512, k256, KLEN_256, t256_512, AES_BLOCK_SIZE},
  32619. #endif
  32620. #ifdef WOLFSSL_AES_128
  32621. {WC_CMAC_AES, 0, m, MLEN_319, k128, KLEN_128, t128_319, AES_BLOCK_SIZE}
  32622. #endif
  32623. };
  32624. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  32625. Cmac *cmac;
  32626. #else
  32627. Cmac cmac[1];
  32628. #endif
  32629. byte tag[AES_BLOCK_SIZE];
  32630. const CMAC_Test_Case* tc;
  32631. word32 i, tagSz;
  32632. int ret;
  32633. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  32634. if ((cmac = (Cmac *)XMALLOC(sizeof *cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC)) == NULL)
  32635. ERROR_OUT(-12009, out);
  32636. #endif
  32637. for (i = 0, tc = testCases;
  32638. i < sizeof(testCases)/sizeof(CMAC_Test_Case);
  32639. i++, tc++) {
  32640. XMEMSET(tag, 0, sizeof(tag));
  32641. tagSz = AES_BLOCK_SIZE;
  32642. #if !defined(HAVE_FIPS) || \
  32643. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)
  32644. if (wc_InitCmac_ex(cmac, tc->k, tc->kSz, tc->type, NULL, HEAP_HINT, devId) != 0)
  32645. #else
  32646. if (wc_InitCmac(cmac, tc->k, tc->kSz, tc->type, NULL) != 0)
  32647. #endif
  32648. {
  32649. ERROR_OUT(-12000, out);
  32650. }
  32651. if (tc->partial) {
  32652. if (wc_CmacUpdate(cmac, tc->m,
  32653. tc->mSz/2 - tc->partial) != 0)
  32654. ERROR_OUT(-12001, out);
  32655. if (wc_CmacUpdate(cmac, tc->m + tc->mSz/2 - tc->partial,
  32656. tc->mSz/2 + tc->partial) != 0)
  32657. ERROR_OUT(-12002, out);
  32658. }
  32659. else {
  32660. if (wc_CmacUpdate(cmac, tc->m, tc->mSz) != 0)
  32661. ERROR_OUT(-12003, out);
  32662. }
  32663. if (wc_CmacFinal(cmac, tag, &tagSz) != 0)
  32664. ERROR_OUT(-12004, out);
  32665. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  32666. ERROR_OUT(-12005, out);
  32667. XMEMSET(tag, 0, sizeof(tag));
  32668. tagSz = sizeof(tag);
  32669. if (wc_AesCmacGenerate(tag, &tagSz, tc->m, tc->mSz,
  32670. tc->k, tc->kSz) != 0)
  32671. ERROR_OUT(-12006, out);
  32672. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  32673. ERROR_OUT(-12007, out);
  32674. if (wc_AesCmacVerify(tc->t, tc->tSz, tc->m, tc->mSz,
  32675. tc->k, tc->kSz) != 0)
  32676. ERROR_OUT(-12008, out);
  32677. }
  32678. ret = 0;
  32679. out:
  32680. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  32681. if (cmac)
  32682. XFREE(cmac, HEAP_HINT, DYNAMIC_TYPE_CMAC);
  32683. #endif
  32684. return ret;
  32685. }
  32686. #endif /* NO_AES && WOLFSSL_CMAC */
  32687. #if defined(WOLFSSL_SIPHASH)
  32688. #if WOLFSSL_SIPHASH_CROUNDS == 2 && WOLFSSL_SIPHASH_DROUNDS == 4
  32689. /* Test vectors from:
  32690. * https://github.com/veorq/SipHash/blob/master/vectors.h
  32691. */
  32692. static const unsigned char siphash_key[SIPHASH_KEY_SIZE] = {
  32693. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  32694. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
  32695. };
  32696. static const unsigned char siphash_msg[64] = {
  32697. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  32698. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  32699. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  32700. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  32701. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  32702. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  32703. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  32704. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f
  32705. };
  32706. static const unsigned char siphash_r8[64][SIPHASH_MAC_SIZE_8] = {
  32707. { 0x31, 0x0e, 0x0e, 0xdd, 0x47, 0xdb, 0x6f, 0x72, },
  32708. { 0xfd, 0x67, 0xdc, 0x93, 0xc5, 0x39, 0xf8, 0x74, },
  32709. { 0x5a, 0x4f, 0xa9, 0xd9, 0x09, 0x80, 0x6c, 0x0d, },
  32710. { 0x2d, 0x7e, 0xfb, 0xd7, 0x96, 0x66, 0x67, 0x85, },
  32711. { 0xb7, 0x87, 0x71, 0x27, 0xe0, 0x94, 0x27, 0xcf, },
  32712. { 0x8d, 0xa6, 0x99, 0xcd, 0x64, 0x55, 0x76, 0x18, },
  32713. { 0xce, 0xe3, 0xfe, 0x58, 0x6e, 0x46, 0xc9, 0xcb, },
  32714. { 0x37, 0xd1, 0x01, 0x8b, 0xf5, 0x00, 0x02, 0xab, },
  32715. { 0x62, 0x24, 0x93, 0x9a, 0x79, 0xf5, 0xf5, 0x93, },
  32716. { 0xb0, 0xe4, 0xa9, 0x0b, 0xdf, 0x82, 0x00, 0x9e, },
  32717. { 0xf3, 0xb9, 0xdd, 0x94, 0xc5, 0xbb, 0x5d, 0x7a, },
  32718. { 0xa7, 0xad, 0x6b, 0x22, 0x46, 0x2f, 0xb3, 0xf4, },
  32719. { 0xfb, 0xe5, 0x0e, 0x86, 0xbc, 0x8f, 0x1e, 0x75, },
  32720. { 0x90, 0x3d, 0x84, 0xc0, 0x27, 0x56, 0xea, 0x14, },
  32721. { 0xee, 0xf2, 0x7a, 0x8e, 0x90, 0xca, 0x23, 0xf7, },
  32722. { 0xe5, 0x45, 0xbe, 0x49, 0x61, 0xca, 0x29, 0xa1, },
  32723. { 0xdb, 0x9b, 0xc2, 0x57, 0x7f, 0xcc, 0x2a, 0x3f, },
  32724. { 0x94, 0x47, 0xbe, 0x2c, 0xf5, 0xe9, 0x9a, 0x69, },
  32725. { 0x9c, 0xd3, 0x8d, 0x96, 0xf0, 0xb3, 0xc1, 0x4b, },
  32726. { 0xbd, 0x61, 0x79, 0xa7, 0x1d, 0xc9, 0x6d, 0xbb, },
  32727. { 0x98, 0xee, 0xa2, 0x1a, 0xf2, 0x5c, 0xd6, 0xbe, },
  32728. { 0xc7, 0x67, 0x3b, 0x2e, 0xb0, 0xcb, 0xf2, 0xd0, },
  32729. { 0x88, 0x3e, 0xa3, 0xe3, 0x95, 0x67, 0x53, 0x93, },
  32730. { 0xc8, 0xce, 0x5c, 0xcd, 0x8c, 0x03, 0x0c, 0xa8, },
  32731. { 0x94, 0xaf, 0x49, 0xf6, 0xc6, 0x50, 0xad, 0xb8, },
  32732. { 0xea, 0xb8, 0x85, 0x8a, 0xde, 0x92, 0xe1, 0xbc, },
  32733. { 0xf3, 0x15, 0xbb, 0x5b, 0xb8, 0x35, 0xd8, 0x17, },
  32734. { 0xad, 0xcf, 0x6b, 0x07, 0x63, 0x61, 0x2e, 0x2f, },
  32735. { 0xa5, 0xc9, 0x1d, 0xa7, 0xac, 0xaa, 0x4d, 0xde, },
  32736. { 0x71, 0x65, 0x95, 0x87, 0x66, 0x50, 0xa2, 0xa6, },
  32737. { 0x28, 0xef, 0x49, 0x5c, 0x53, 0xa3, 0x87, 0xad, },
  32738. { 0x42, 0xc3, 0x41, 0xd8, 0xfa, 0x92, 0xd8, 0x32, },
  32739. { 0xce, 0x7c, 0xf2, 0x72, 0x2f, 0x51, 0x27, 0x71, },
  32740. { 0xe3, 0x78, 0x59, 0xf9, 0x46, 0x23, 0xf3, 0xa7, },
  32741. { 0x38, 0x12, 0x05, 0xbb, 0x1a, 0xb0, 0xe0, 0x12, },
  32742. { 0xae, 0x97, 0xa1, 0x0f, 0xd4, 0x34, 0xe0, 0x15, },
  32743. { 0xb4, 0xa3, 0x15, 0x08, 0xbe, 0xff, 0x4d, 0x31, },
  32744. { 0x81, 0x39, 0x62, 0x29, 0xf0, 0x90, 0x79, 0x02, },
  32745. { 0x4d, 0x0c, 0xf4, 0x9e, 0xe5, 0xd4, 0xdc, 0xca, },
  32746. { 0x5c, 0x73, 0x33, 0x6a, 0x76, 0xd8, 0xbf, 0x9a, },
  32747. { 0xd0, 0xa7, 0x04, 0x53, 0x6b, 0xa9, 0x3e, 0x0e, },
  32748. { 0x92, 0x59, 0x58, 0xfc, 0xd6, 0x42, 0x0c, 0xad, },
  32749. { 0xa9, 0x15, 0xc2, 0x9b, 0xc8, 0x06, 0x73, 0x18, },
  32750. { 0x95, 0x2b, 0x79, 0xf3, 0xbc, 0x0a, 0xa6, 0xd4, },
  32751. { 0xf2, 0x1d, 0xf2, 0xe4, 0x1d, 0x45, 0x35, 0xf9, },
  32752. { 0x87, 0x57, 0x75, 0x19, 0x04, 0x8f, 0x53, 0xa9, },
  32753. { 0x10, 0xa5, 0x6c, 0xf5, 0xdf, 0xcd, 0x9a, 0xdb, },
  32754. { 0xeb, 0x75, 0x09, 0x5c, 0xcd, 0x98, 0x6c, 0xd0, },
  32755. { 0x51, 0xa9, 0xcb, 0x9e, 0xcb, 0xa3, 0x12, 0xe6, },
  32756. { 0x96, 0xaf, 0xad, 0xfc, 0x2c, 0xe6, 0x66, 0xc7, },
  32757. { 0x72, 0xfe, 0x52, 0x97, 0x5a, 0x43, 0x64, 0xee, },
  32758. { 0x5a, 0x16, 0x45, 0xb2, 0x76, 0xd5, 0x92, 0xa1, },
  32759. { 0xb2, 0x74, 0xcb, 0x8e, 0xbf, 0x87, 0x87, 0x0a, },
  32760. { 0x6f, 0x9b, 0xb4, 0x20, 0x3d, 0xe7, 0xb3, 0x81, },
  32761. { 0xea, 0xec, 0xb2, 0xa3, 0x0b, 0x22, 0xa8, 0x7f, },
  32762. { 0x99, 0x24, 0xa4, 0x3c, 0xc1, 0x31, 0x57, 0x24, },
  32763. { 0xbd, 0x83, 0x8d, 0x3a, 0xaf, 0xbf, 0x8d, 0xb7, },
  32764. { 0x0b, 0x1a, 0x2a, 0x32, 0x65, 0xd5, 0x1a, 0xea, },
  32765. { 0x13, 0x50, 0x79, 0xa3, 0x23, 0x1c, 0xe6, 0x60, },
  32766. { 0x93, 0x2b, 0x28, 0x46, 0xe4, 0xd7, 0x06, 0x66, },
  32767. { 0xe1, 0x91, 0x5f, 0x5c, 0xb1, 0xec, 0xa4, 0x6c, },
  32768. { 0xf3, 0x25, 0x96, 0x5c, 0xa1, 0x6d, 0x62, 0x9f, },
  32769. { 0x57, 0x5f, 0xf2, 0x8e, 0x60, 0x38, 0x1b, 0xe5, },
  32770. { 0x72, 0x45, 0x06, 0xeb, 0x4c, 0x32, 0x8a, 0x95, },
  32771. };
  32772. static const unsigned char siphash_r16[64][SIPHASH_MAC_SIZE_16] = {
  32773. { 0xa3, 0x81, 0x7f, 0x04, 0xba, 0x25, 0xa8, 0xe6,
  32774. 0x6d, 0xf6, 0x72, 0x14, 0xc7, 0x55, 0x02, 0x93, },
  32775. { 0xda, 0x87, 0xc1, 0xd8, 0x6b, 0x99, 0xaf, 0x44,
  32776. 0x34, 0x76, 0x59, 0x11, 0x9b, 0x22, 0xfc, 0x45, },
  32777. { 0x81, 0x77, 0x22, 0x8d, 0xa4, 0xa4, 0x5d, 0xc7,
  32778. 0xfc, 0xa3, 0x8b, 0xde, 0xf6, 0x0a, 0xff, 0xe4, },
  32779. { 0x9c, 0x70, 0xb6, 0x0c, 0x52, 0x67, 0xa9, 0x4e,
  32780. 0x5f, 0x33, 0xb6, 0xb0, 0x29, 0x85, 0xed, 0x51, },
  32781. { 0xf8, 0x81, 0x64, 0xc1, 0x2d, 0x9c, 0x8f, 0xaf,
  32782. 0x7d, 0x0f, 0x6e, 0x7c, 0x7b, 0xcd, 0x55, 0x79, },
  32783. { 0x13, 0x68, 0x87, 0x59, 0x80, 0x77, 0x6f, 0x88,
  32784. 0x54, 0x52, 0x7a, 0x07, 0x69, 0x0e, 0x96, 0x27, },
  32785. { 0x14, 0xee, 0xca, 0x33, 0x8b, 0x20, 0x86, 0x13,
  32786. 0x48, 0x5e, 0xa0, 0x30, 0x8f, 0xd7, 0xa1, 0x5e, },
  32787. { 0xa1, 0xf1, 0xeb, 0xbe, 0xd8, 0xdb, 0xc1, 0x53,
  32788. 0xc0, 0xb8, 0x4a, 0xa6, 0x1f, 0xf0, 0x82, 0x39, },
  32789. { 0x3b, 0x62, 0xa9, 0xba, 0x62, 0x58, 0xf5, 0x61,
  32790. 0x0f, 0x83, 0xe2, 0x64, 0xf3, 0x14, 0x97, 0xb4, },
  32791. { 0x26, 0x44, 0x99, 0x06, 0x0a, 0xd9, 0xba, 0xab,
  32792. 0xc4, 0x7f, 0x8b, 0x02, 0xbb, 0x6d, 0x71, 0xed, },
  32793. { 0x00, 0x11, 0x0d, 0xc3, 0x78, 0x14, 0x69, 0x56,
  32794. 0xc9, 0x54, 0x47, 0xd3, 0xf3, 0xd0, 0xfb, 0xba, },
  32795. { 0x01, 0x51, 0xc5, 0x68, 0x38, 0x6b, 0x66, 0x77,
  32796. 0xa2, 0xb4, 0xdc, 0x6f, 0x81, 0xe5, 0xdc, 0x18, },
  32797. { 0xd6, 0x26, 0xb2, 0x66, 0x90, 0x5e, 0xf3, 0x58,
  32798. 0x82, 0x63, 0x4d, 0xf6, 0x85, 0x32, 0xc1, 0x25, },
  32799. { 0x98, 0x69, 0xe2, 0x47, 0xe9, 0xc0, 0x8b, 0x10,
  32800. 0xd0, 0x29, 0x93, 0x4f, 0xc4, 0xb9, 0x52, 0xf7, },
  32801. { 0x31, 0xfc, 0xef, 0xac, 0x66, 0xd7, 0xde, 0x9c,
  32802. 0x7e, 0xc7, 0x48, 0x5f, 0xe4, 0x49, 0x49, 0x02, },
  32803. { 0x54, 0x93, 0xe9, 0x99, 0x33, 0xb0, 0xa8, 0x11,
  32804. 0x7e, 0x08, 0xec, 0x0f, 0x97, 0xcf, 0xc3, 0xd9, },
  32805. { 0x6e, 0xe2, 0xa4, 0xca, 0x67, 0xb0, 0x54, 0xbb,
  32806. 0xfd, 0x33, 0x15, 0xbf, 0x85, 0x23, 0x05, 0x77, },
  32807. { 0x47, 0x3d, 0x06, 0xe8, 0x73, 0x8d, 0xb8, 0x98,
  32808. 0x54, 0xc0, 0x66, 0xc4, 0x7a, 0xe4, 0x77, 0x40, },
  32809. { 0xa4, 0x26, 0xe5, 0xe4, 0x23, 0xbf, 0x48, 0x85,
  32810. 0x29, 0x4d, 0xa4, 0x81, 0xfe, 0xae, 0xf7, 0x23, },
  32811. { 0x78, 0x01, 0x77, 0x31, 0xcf, 0x65, 0xfa, 0xb0,
  32812. 0x74, 0xd5, 0x20, 0x89, 0x52, 0x51, 0x2e, 0xb1, },
  32813. { 0x9e, 0x25, 0xfc, 0x83, 0x3f, 0x22, 0x90, 0x73,
  32814. 0x3e, 0x93, 0x44, 0xa5, 0xe8, 0x38, 0x39, 0xeb, },
  32815. { 0x56, 0x8e, 0x49, 0x5a, 0xbe, 0x52, 0x5a, 0x21,
  32816. 0x8a, 0x22, 0x14, 0xcd, 0x3e, 0x07, 0x1d, 0x12, },
  32817. { 0x4a, 0x29, 0xb5, 0x45, 0x52, 0xd1, 0x6b, 0x9a,
  32818. 0x46, 0x9c, 0x10, 0x52, 0x8e, 0xff, 0x0a, 0xae, },
  32819. { 0xc9, 0xd1, 0x84, 0xdd, 0xd5, 0xa9, 0xf5, 0xe0,
  32820. 0xcf, 0x8c, 0xe2, 0x9a, 0x9a, 0xbf, 0x69, 0x1c, },
  32821. { 0x2d, 0xb4, 0x79, 0xae, 0x78, 0xbd, 0x50, 0xd8,
  32822. 0x88, 0x2a, 0x8a, 0x17, 0x8a, 0x61, 0x32, 0xad, },
  32823. { 0x8e, 0xce, 0x5f, 0x04, 0x2d, 0x5e, 0x44, 0x7b,
  32824. 0x50, 0x51, 0xb9, 0xea, 0xcb, 0x8d, 0x8f, 0x6f, },
  32825. { 0x9c, 0x0b, 0x53, 0xb4, 0xb3, 0xc3, 0x07, 0xe8,
  32826. 0x7e, 0xae, 0xe0, 0x86, 0x78, 0x14, 0x1f, 0x66, },
  32827. { 0xab, 0xf2, 0x48, 0xaf, 0x69, 0xa6, 0xea, 0xe4,
  32828. 0xbf, 0xd3, 0xeb, 0x2f, 0x12, 0x9e, 0xeb, 0x94, },
  32829. { 0x06, 0x64, 0xda, 0x16, 0x68, 0x57, 0x4b, 0x88,
  32830. 0xb9, 0x35, 0xf3, 0x02, 0x73, 0x58, 0xae, 0xf4, },
  32831. { 0xaa, 0x4b, 0x9d, 0xc4, 0xbf, 0x33, 0x7d, 0xe9,
  32832. 0x0c, 0xd4, 0xfd, 0x3c, 0x46, 0x7c, 0x6a, 0xb7, },
  32833. { 0xea, 0x5c, 0x7f, 0x47, 0x1f, 0xaf, 0x6b, 0xde,
  32834. 0x2b, 0x1a, 0xd7, 0xd4, 0x68, 0x6d, 0x22, 0x87, },
  32835. { 0x29, 0x39, 0xb0, 0x18, 0x32, 0x23, 0xfa, 0xfc,
  32836. 0x17, 0x23, 0xde, 0x4f, 0x52, 0xc4, 0x3d, 0x35, },
  32837. { 0x7c, 0x39, 0x56, 0xca, 0x5e, 0xea, 0xfc, 0x3e,
  32838. 0x36, 0x3e, 0x9d, 0x55, 0x65, 0x46, 0xeb, 0x68, },
  32839. { 0x77, 0xc6, 0x07, 0x71, 0x46, 0xf0, 0x1c, 0x32,
  32840. 0xb6, 0xb6, 0x9d, 0x5f, 0x4e, 0xa9, 0xff, 0xcf, },
  32841. { 0x37, 0xa6, 0x98, 0x6c, 0xb8, 0x84, 0x7e, 0xdf,
  32842. 0x09, 0x25, 0xf0, 0xf1, 0x30, 0x9b, 0x54, 0xde, },
  32843. { 0xa7, 0x05, 0xf0, 0xe6, 0x9d, 0xa9, 0xa8, 0xf9,
  32844. 0x07, 0x24, 0x1a, 0x2e, 0x92, 0x3c, 0x8c, 0xc8, },
  32845. { 0x3d, 0xc4, 0x7d, 0x1f, 0x29, 0xc4, 0x48, 0x46,
  32846. 0x1e, 0x9e, 0x76, 0xed, 0x90, 0x4f, 0x67, 0x11, },
  32847. { 0x0d, 0x62, 0xbf, 0x01, 0xe6, 0xfc, 0x0e, 0x1a,
  32848. 0x0d, 0x3c, 0x47, 0x51, 0xc5, 0xd3, 0x69, 0x2b, },
  32849. { 0x8c, 0x03, 0x46, 0x8b, 0xca, 0x7c, 0x66, 0x9e,
  32850. 0xe4, 0xfd, 0x5e, 0x08, 0x4b, 0xbe, 0xe7, 0xb5, },
  32851. { 0x52, 0x8a, 0x5b, 0xb9, 0x3b, 0xaf, 0x2c, 0x9c,
  32852. 0x44, 0x73, 0xcc, 0xe5, 0xd0, 0xd2, 0x2b, 0xd9, },
  32853. { 0xdf, 0x6a, 0x30, 0x1e, 0x95, 0xc9, 0x5d, 0xad,
  32854. 0x97, 0xae, 0x0c, 0xc8, 0xc6, 0x91, 0x3b, 0xd8, },
  32855. { 0x80, 0x11, 0x89, 0x90, 0x2c, 0x85, 0x7f, 0x39,
  32856. 0xe7, 0x35, 0x91, 0x28, 0x5e, 0x70, 0xb6, 0xdb, },
  32857. { 0xe6, 0x17, 0x34, 0x6a, 0xc9, 0xc2, 0x31, 0xbb,
  32858. 0x36, 0x50, 0xae, 0x34, 0xcc, 0xca, 0x0c, 0x5b, },
  32859. { 0x27, 0xd9, 0x34, 0x37, 0xef, 0xb7, 0x21, 0xaa,
  32860. 0x40, 0x18, 0x21, 0xdc, 0xec, 0x5a, 0xdf, 0x89, },
  32861. { 0x89, 0x23, 0x7d, 0x9d, 0xed, 0x9c, 0x5e, 0x78,
  32862. 0xd8, 0xb1, 0xc9, 0xb1, 0x66, 0xcc, 0x73, 0x42, },
  32863. { 0x4a, 0x6d, 0x80, 0x91, 0xbf, 0x5e, 0x7d, 0x65,
  32864. 0x11, 0x89, 0xfa, 0x94, 0xa2, 0x50, 0xb1, 0x4c, },
  32865. { 0x0e, 0x33, 0xf9, 0x60, 0x55, 0xe7, 0xae, 0x89,
  32866. 0x3f, 0xfc, 0x0e, 0x3d, 0xcf, 0x49, 0x29, 0x02, },
  32867. { 0xe6, 0x1c, 0x43, 0x2b, 0x72, 0x0b, 0x19, 0xd1,
  32868. 0x8e, 0xc8, 0xd8, 0x4b, 0xdc, 0x63, 0x15, 0x1b, },
  32869. { 0xf7, 0xe5, 0xae, 0xf5, 0x49, 0xf7, 0x82, 0xcf,
  32870. 0x37, 0x90, 0x55, 0xa6, 0x08, 0x26, 0x9b, 0x16, },
  32871. { 0x43, 0x8d, 0x03, 0x0f, 0xd0, 0xb7, 0xa5, 0x4f,
  32872. 0xa8, 0x37, 0xf2, 0xad, 0x20, 0x1a, 0x64, 0x03, },
  32873. { 0xa5, 0x90, 0xd3, 0xee, 0x4f, 0xbf, 0x04, 0xe3,
  32874. 0x24, 0x7e, 0x0d, 0x27, 0xf2, 0x86, 0x42, 0x3f, },
  32875. { 0x5f, 0xe2, 0xc1, 0xa1, 0x72, 0xfe, 0x93, 0xc4,
  32876. 0xb1, 0x5c, 0xd3, 0x7c, 0xae, 0xf9, 0xf5, 0x38, },
  32877. { 0x2c, 0x97, 0x32, 0x5c, 0xbd, 0x06, 0xb3, 0x6e,
  32878. 0xb2, 0x13, 0x3d, 0xd0, 0x8b, 0x3a, 0x01, 0x7c, },
  32879. { 0x92, 0xc8, 0x14, 0x22, 0x7a, 0x6b, 0xca, 0x94,
  32880. 0x9f, 0xf0, 0x65, 0x9f, 0x00, 0x2a, 0xd3, 0x9e, },
  32881. { 0xdc, 0xe8, 0x50, 0x11, 0x0b, 0xd8, 0x32, 0x8c,
  32882. 0xfb, 0xd5, 0x08, 0x41, 0xd6, 0x91, 0x1d, 0x87, },
  32883. { 0x67, 0xf1, 0x49, 0x84, 0xc7, 0xda, 0x79, 0x12,
  32884. 0x48, 0xe3, 0x2b, 0xb5, 0x92, 0x25, 0x83, 0xda, },
  32885. { 0x19, 0x38, 0xf2, 0xcf, 0x72, 0xd5, 0x4e, 0xe9,
  32886. 0x7e, 0x94, 0x16, 0x6f, 0xa9, 0x1d, 0x2a, 0x36, },
  32887. { 0x74, 0x48, 0x1e, 0x96, 0x46, 0xed, 0x49, 0xfe,
  32888. 0x0f, 0x62, 0x24, 0x30, 0x16, 0x04, 0x69, 0x8e, },
  32889. { 0x57, 0xfc, 0xa5, 0xde, 0x98, 0xa9, 0xd6, 0xd8,
  32890. 0x00, 0x64, 0x38, 0xd0, 0x58, 0x3d, 0x8a, 0x1d, },
  32891. { 0x9f, 0xec, 0xde, 0x1c, 0xef, 0xdc, 0x1c, 0xbe,
  32892. 0xd4, 0x76, 0x36, 0x74, 0xd9, 0x57, 0x53, 0x59, },
  32893. { 0xe3, 0x04, 0x0c, 0x00, 0xeb, 0x28, 0xf1, 0x53,
  32894. 0x66, 0xca, 0x73, 0xcb, 0xd8, 0x72, 0xe7, 0x40, },
  32895. { 0x76, 0x97, 0x00, 0x9a, 0x6a, 0x83, 0x1d, 0xfe,
  32896. 0xcc, 0xa9, 0x1c, 0x59, 0x93, 0x67, 0x0f, 0x7a, },
  32897. { 0x58, 0x53, 0x54, 0x23, 0x21, 0xf5, 0x67, 0xa0,
  32898. 0x05, 0xd5, 0x47, 0xa4, 0xf0, 0x47, 0x59, 0xbd, },
  32899. { 0x51, 0x50, 0xd1, 0x77, 0x2f, 0x50, 0x83, 0x4a,
  32900. 0x50, 0x3e, 0x06, 0x9a, 0x97, 0x3f, 0xbd, 0x7c, },
  32901. };
  32902. #endif
  32903. WOLFSSL_TEST_SUBROUTINE int siphash_test(void)
  32904. {
  32905. int ret = 0;
  32906. int i;
  32907. #if WOLFSSL_SIPHASH_CROUNDS == 2 && WOLFSSL_SIPHASH_DROUNDS == 4
  32908. unsigned char res[SIPHASH_MAC_SIZE_16];
  32909. SipHash siphash;
  32910. for (i = 0; i < 64; i++) {
  32911. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  32912. if (ret != 0)
  32913. return -12100 - i;
  32914. ret = wc_SipHashUpdate(&siphash, siphash_msg, i);
  32915. if (ret != 0)
  32916. return -12200 - i;
  32917. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_8);
  32918. if (ret != 0)
  32919. return -12300 - i;
  32920. if (XMEMCMP(res, siphash_r8[i], SIPHASH_MAC_SIZE_8) != 0)
  32921. return -12400 - i;
  32922. ret = wc_SipHash(siphash_key, siphash_msg, i, res, SIPHASH_MAC_SIZE_8);
  32923. if (ret != 0)
  32924. return -12500 - i;
  32925. if (XMEMCMP(res, siphash_r8[i], SIPHASH_MAC_SIZE_8) != 0)
  32926. return -12600 - i;
  32927. }
  32928. for (i = 0; i < 64; i++) {
  32929. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_16);
  32930. if (ret != 0)
  32931. return -12700 - i;
  32932. ret = wc_SipHashUpdate(&siphash, siphash_msg, i);
  32933. if (ret != 0)
  32934. return -12800 - i;
  32935. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_16);
  32936. if (ret != 0)
  32937. return -12900 - i;
  32938. if (XMEMCMP(res, siphash_r16[i], SIPHASH_MAC_SIZE_16) != 0)
  32939. return -13000 - i;
  32940. ret = wc_SipHash(siphash_key, siphash_msg, i, res, SIPHASH_MAC_SIZE_16);
  32941. if (ret != 0)
  32942. return -13100 - i;
  32943. if (XMEMCMP(res, siphash_r16[i], SIPHASH_MAC_SIZE_16) != 0)
  32944. return -13200 - i;
  32945. }
  32946. #endif
  32947. /* Testing bad parameters. */
  32948. ret = wc_InitSipHash(NULL, NULL, SIPHASH_MAC_SIZE_8);
  32949. if (ret != BAD_FUNC_ARG)
  32950. return -13300;
  32951. ret = wc_InitSipHash(NULL, siphash_key, SIPHASH_MAC_SIZE_8);
  32952. if (ret != BAD_FUNC_ARG)
  32953. return -13301;
  32954. ret = wc_InitSipHash(&siphash, NULL, SIPHASH_MAC_SIZE_8);
  32955. if (ret != BAD_FUNC_ARG)
  32956. return -13302;
  32957. ret = wc_InitSipHash(&siphash, siphash_key, 7);
  32958. if (ret != BAD_FUNC_ARG)
  32959. return -13303;
  32960. ret = wc_InitSipHash(&siphash, siphash_key, SIPHASH_MAC_SIZE_8);
  32961. if (ret != 0)
  32962. return -13304;
  32963. ret = wc_SipHashUpdate(NULL, NULL, 0);
  32964. if (ret != BAD_FUNC_ARG)
  32965. return -13305;
  32966. ret = wc_SipHashUpdate(&siphash, NULL, 1);
  32967. if (ret != BAD_FUNC_ARG)
  32968. return -13306;
  32969. ret = wc_SipHashFinal(NULL, NULL, SIPHASH_MAC_SIZE_8);
  32970. if (ret != BAD_FUNC_ARG)
  32971. return -13307;
  32972. ret = wc_SipHashFinal(&siphash, NULL, SIPHASH_MAC_SIZE_8);
  32973. if (ret != BAD_FUNC_ARG)
  32974. return -13308;
  32975. ret = wc_SipHashFinal(NULL, res, SIPHASH_MAC_SIZE_8);
  32976. if (ret != BAD_FUNC_ARG)
  32977. return -13309;
  32978. ret = wc_SipHashFinal(&siphash, res, SIPHASH_MAC_SIZE_16);
  32979. if (ret != BAD_FUNC_ARG)
  32980. return -13310;
  32981. ret = wc_SipHash(NULL, NULL, 0, NULL, SIPHASH_MAC_SIZE_16);
  32982. if (ret != BAD_FUNC_ARG)
  32983. return -13311;
  32984. ret = wc_SipHash(siphash_key, NULL, 0, NULL, SIPHASH_MAC_SIZE_16);
  32985. if (ret != BAD_FUNC_ARG)
  32986. return -13312;
  32987. ret = wc_SipHash(NULL, NULL, 0, res, SIPHASH_MAC_SIZE_16);
  32988. if (ret != BAD_FUNC_ARG)
  32989. return -13313;
  32990. ret = wc_SipHash(siphash_key, NULL, 0, res, 15);
  32991. if (ret != BAD_FUNC_ARG)
  32992. return -13314;
  32993. ret = wc_SipHash(siphash_key, NULL, 1, res, SIPHASH_MAC_SIZE_16);
  32994. if (ret != BAD_FUNC_ARG)
  32995. return -13315;
  32996. return 0;
  32997. }
  32998. #endif /* WOLFSSL_SIPHASH */
  32999. #ifdef HAVE_LIBZ
  33000. static const byte sample_text[] =
  33001. "Biodiesel cupidatat marfa, cliche aute put a bird on it incididunt elit\n"
  33002. "polaroid. Sunt tattooed bespoke reprehenderit. Sint twee organic id\n"
  33003. "marfa. Commodo veniam ad esse gastropub. 3 wolf moon sartorial vero,\n"
  33004. "plaid delectus biodiesel squid +1 vice. Post-ironic keffiyeh leggings\n"
  33005. "selfies cray fap hoodie, forage anim. Carles cupidatat shoreditch, VHS\n"
  33006. "small batch meggings kogi dolore food truck bespoke gastropub.\n"
  33007. "\n"
  33008. "Terry richardson adipisicing actually typewriter tumblr, twee whatever\n"
  33009. "four loko you probably haven't heard of them high life. Messenger bag\n"
  33010. "whatever tattooed deep v mlkshk. Brooklyn pinterest assumenda chillwave\n"
  33011. "et, banksy ullamco messenger bag umami pariatur direct trade forage.\n"
  33012. "Typewriter culpa try-hard, pariatur sint brooklyn meggings. Gentrify\n"
  33013. "food truck next level, tousled irony non semiotics PBR ethical anim cred\n"
  33014. "readymade. Mumblecore brunch lomo odd future, portland organic terry\n"
  33015. "richardson elit leggings adipisicing ennui raw denim banjo hella. Godard\n"
  33016. "mixtape polaroid, pork belly readymade organic cray typewriter helvetica\n"
  33017. "four loko whatever street art yr farm-to-table.\n"
  33018. "\n"
  33019. "Vinyl keytar vice tofu. Locavore you probably haven't heard of them pug\n"
  33020. "pickled, hella tonx labore truffaut DIY mlkshk elit cosby sweater sint\n"
  33021. "et mumblecore. Elit swag semiotics, reprehenderit DIY sartorial nisi ugh\n"
  33022. "nesciunt pug pork belly wayfarers selfies delectus. Ethical hoodie\n"
  33023. "seitan fingerstache kale chips. Terry richardson artisan williamsburg,\n"
  33024. "eiusmod fanny pack irony tonx ennui lo-fi incididunt tofu YOLO\n"
  33025. "readymade. 8-bit sed ethnic beard officia. Pour-over iphone DIY butcher,\n"
  33026. "ethnic art party qui letterpress nisi proident jean shorts mlkshk\n"
  33027. "locavore.\n"
  33028. "\n"
  33029. "Narwhal flexitarian letterpress, do gluten-free voluptate next level\n"
  33030. "banh mi tonx incididunt carles DIY. Odd future nulla 8-bit beard ut\n"
  33031. "cillum pickled velit, YOLO officia you probably haven't heard of them\n"
  33032. "trust fund gastropub. Nisi adipisicing tattooed, Austin mlkshk 90's\n"
  33033. "small batch american apparel. Put a bird on it cosby sweater before they\n"
  33034. "sold out pork belly kogi hella. Street art mollit sustainable polaroid,\n"
  33035. "DIY ethnic ea pug beard dreamcatcher cosby sweater magna scenester nisi.\n"
  33036. "Sed pork belly skateboard mollit, labore proident eiusmod. Sriracha\n"
  33037. "excepteur cosby sweater, anim deserunt laborum eu aliquip ethical et\n"
  33038. "neutra PBR selvage.\n"
  33039. "\n"
  33040. "Raw denim pork belly truffaut, irony plaid sustainable put a bird on it\n"
  33041. "next level jean shorts exercitation. Hashtag keytar whatever, nihil\n"
  33042. "authentic aliquip disrupt laborum. Tattooed selfies deserunt trust fund\n"
  33043. "wayfarers. 3 wolf moon synth church-key sartorial, gastropub leggings\n"
  33044. "tattooed. Labore high life commodo, meggings raw denim fingerstache pug\n"
  33045. "trust fund leggings seitan forage. Nostrud ullamco duis, reprehenderit\n"
  33046. "incididunt flannel sustainable helvetica pork belly pug banksy you\n"
  33047. "probably haven't heard of them nesciunt farm-to-table. Disrupt nostrud\n"
  33048. "mollit magna, sriracha sartorial helvetica.\n"
  33049. "\n"
  33050. "Nulla kogi reprehenderit, skateboard sustainable duis adipisicing viral\n"
  33051. "ad fanny pack salvia. Fanny pack trust fund you probably haven't heard\n"
  33052. "of them YOLO vice nihil. Keffiyeh cray lo-fi pinterest cardigan aliqua,\n"
  33053. "reprehenderit aute. Culpa tousled williamsburg, marfa lomo actually anim\n"
  33054. "skateboard. Iphone aliqua ugh, semiotics pariatur vero readymade\n"
  33055. "organic. Marfa squid nulla, in laborum disrupt laboris irure gastropub.\n"
  33056. "Veniam sunt food truck leggings, sint vinyl fap.\n"
  33057. "\n"
  33058. "Hella dolore pork belly, truffaut carles you probably haven't heard of\n"
  33059. "them PBR helvetica in sapiente. Fashion axe ugh bushwick american\n"
  33060. "apparel. Fingerstache sed iphone, jean shorts blue bottle nisi bushwick\n"
  33061. "flexitarian officia veniam plaid bespoke fap YOLO lo-fi. Blog\n"
  33062. "letterpress mumblecore, food truck id cray brooklyn cillum ad sed.\n"
  33063. "Assumenda chambray wayfarers vinyl mixtape sustainable. VHS vinyl\n"
  33064. "delectus, culpa williamsburg polaroid cliche swag church-key synth kogi\n"
  33065. "magna pop-up literally. Swag thundercats ennui shoreditch vegan\n"
  33066. "pitchfork neutra truffaut etsy, sed single-origin coffee craft beer.\n"
  33067. "\n"
  33068. "Odio letterpress brooklyn elit. Nulla single-origin coffee in occaecat\n"
  33069. "meggings. Irony meggings 8-bit, chillwave lo-fi adipisicing cred\n"
  33070. "dreamcatcher veniam. Put a bird on it irony umami, trust fund bushwick\n"
  33071. "locavore kale chips. Sriracha swag thundercats, chillwave disrupt\n"
  33072. "tousled beard mollit mustache leggings portland next level. Nihil esse\n"
  33073. "est, skateboard art party etsy thundercats sed dreamcatcher ut iphone\n"
  33074. "swag consectetur et. Irure skateboard banjo, nulla deserunt messenger\n"
  33075. "bag dolor terry richardson sapiente.\n";
  33076. static const byte sample_text_gz[] = {
  33077. 0x1F, 0x8B, 0x08, 0x08, 0xC5, 0x49, 0xB5, 0x5B, 0x00, 0x03, 0x63, 0x69, 0x70,
  33078. 0x68, 0x65, 0x72, 0x74, 0x65, 0x78, 0x74, 0x2E, 0x74, 0x78, 0x74, 0x00, 0x8D,
  33079. 0x58, 0xCB, 0x92, 0xE4, 0xB6, 0x11, 0xBC, 0xE3, 0x2B, 0xEA, 0xA6, 0x83, 0xD9,
  33080. 0x1D, 0x72, 0xF8, 0x22, 0x1F, 0xB5, 0x96, 0xA5, 0xDD, 0x90, 0xBC, 0xAB, 0xD0,
  33081. 0x28, 0x36, 0x42, 0x47, 0x90, 0x2C, 0x36, 0xA1, 0x06, 0x09, 0x0A, 0x8F, 0xEE,
  33082. 0xE1, 0xDF, 0x3B, 0x0B, 0xE0, 0x73, 0x2C, 0x4B, 0xBA, 0xCD, 0xCE, 0x80, 0x78,
  33083. 0x64, 0x65, 0x65, 0x66, 0xED, 0x3B, 0xE3, 0x5A, 0xC3, 0x81, 0x2D, 0x35, 0x69,
  33084. 0x32, 0xAD, 0x8E, 0x3A, 0xD2, 0xA0, 0x7D, 0xA7, 0x2B, 0x6A, 0xAC, 0x69, 0x7A,
  33085. 0x26, 0x9D, 0x22, 0xD3, 0x94, 0x22, 0x69, 0xAA, 0x8D, 0x6F, 0xC9, 0x8D, 0x64,
  33086. 0x22, 0x99, 0xB1, 0x31, 0xAD, 0x69, 0xD3, 0x18, 0x89, 0xAD, 0x89, 0x6A, 0x72,
  33087. 0x56, 0x7B, 0x67, 0xDA, 0x2B, 0xBD, 0xC8, 0xEF, 0xB0, 0x4D, 0x74, 0x8E, 0x5B,
  33088. 0xAA, 0x39, 0x4C, 0xEE, 0xCE, 0xE4, 0x79, 0xF2, 0xDC, 0xF3, 0xD8, 0xB2, 0x37,
  33089. 0x11, 0x8B, 0x8C, 0x2C, 0x7A, 0x32, 0x93, 0xF3, 0x37, 0x3D, 0x9A, 0x86, 0x4C,
  33090. 0xAB, 0xF2, 0xB9, 0x57, 0xFA, 0x97, 0x1B, 0x06, 0xD7, 0x3A, 0x7A, 0xF0, 0x68,
  33091. 0xF4, 0x40, 0xBA, 0x25, 0x0E, 0x81, 0xE9, 0xA6, 0x43, 0xF4, 0x6E, 0x4A, 0xF5,
  33092. 0x95, 0xFE, 0x41, 0x4F, 0x67, 0x3B, 0x1A, 0x1C, 0xEE, 0x12, 0xB4, 0x8F, 0xCE,
  33093. 0x1B, 0x6D, 0xB1, 0xDE, 0xBB, 0x4A, 0x4D, 0x56, 0x9B, 0x96, 0x5A, 0xB6, 0xDC,
  33094. 0xC4, 0x14, 0x70, 0xE5, 0xF5, 0x7D, 0xE1, 0xB7, 0x84, 0x3F, 0xFC, 0xED, 0xEF,
  33095. 0xF4, 0x30, 0x0D, 0x5F, 0xE9, 0x47, 0x17, 0xE2, 0xC5, 0x78, 0x27, 0x67, 0xDF,
  33096. 0xB9, 0xEB, 0xCC, 0xCC, 0x3D, 0x59, 0xBE, 0xDD, 0xCC, 0x78, 0x0B, 0x0A, 0x1F,
  33097. 0x74, 0xF8, 0x8C, 0x1A, 0xAF, 0x67, 0xEA, 0xF4, 0x44, 0xBD, 0x93, 0x7D, 0x2A,
  33098. 0xEA, 0x9C, 0xD7, 0x37, 0x80, 0x32, 0x9A, 0x01, 0x37, 0xD5, 0xDE, 0xCA, 0xA2,
  33099. 0x0D, 0xB9, 0xD0, 0x3B, 0xCF, 0xAD, 0x89, 0x4D, 0x5F, 0xD1, 0xE7, 0xF7, 0x2F,
  33100. 0x2A, 0x0C, 0xDA, 0x5A, 0xAA, 0x35, 0x7E, 0x41, 0xC3, 0xB2, 0x37, 0xDD, 0xDD,
  33101. 0xCD, 0x50, 0xEB, 0x2C, 0x96, 0x62, 0x3B, 0xD7, 0x52, 0xF4, 0xA9, 0xB9, 0x6F,
  33102. 0x48, 0xED, 0xEF, 0x54, 0xEA, 0x67, 0xF6, 0x7E, 0x26, 0x8F, 0x3A, 0x68, 0xDF,
  33103. 0x06, 0xBC, 0x56, 0xB7, 0x66, 0x32, 0xC1, 0x34, 0xD8, 0x88, 0x34, 0x1E, 0x88,
  33104. 0xED, 0x67, 0x8A, 0xF3, 0xC4, 0x4F, 0xC0, 0xCA, 0x9E, 0x62, 0x1A, 0x6A, 0xEB,
  33105. 0xAB, 0x02, 0xED, 0xB3, 0xD7, 0x91, 0x81, 0x8A, 0xEA, 0x5C, 0xF2, 0x64, 0xDD,
  33106. 0xDD, 0xD1, 0xEC, 0x12, 0x4D, 0xDE, 0xD5, 0xBA, 0xC6, 0x77, 0xBD, 0x06, 0xC4,
  33107. 0x5F, 0x44, 0xEA, 0x59, 0x4B, 0x5D, 0x3B, 0x8A, 0x3D, 0x0F, 0xD4, 0x9B, 0x1B,
  33108. 0x80, 0x30, 0x1D, 0x30, 0xFA, 0x8F, 0x00, 0x3F, 0xDE, 0xB0, 0x6F, 0xAD, 0x6F,
  33109. 0x6A, 0xDD, 0x6E, 0x2F, 0x6E, 0xCB, 0x3C, 0xD1, 0x83, 0x06, 0x7B, 0x0F, 0xFD,
  33110. 0xFD, 0x4A, 0xEF, 0xBC, 0x73, 0x77, 0x3B, 0x8F, 0x34, 0xA1, 0xBA, 0xEC, 0x39,
  33111. 0x80, 0x33, 0x21, 0xA4, 0x01, 0x55, 0xD7, 0xD4, 0xF4, 0xC6, 0xDA, 0x27, 0x4E,
  33112. 0x54, 0x1C, 0x2B, 0xEC, 0x37, 0xDE, 0xC3, 0x4C, 0xC9, 0x5A, 0x3D, 0x34, 0x0E,
  33113. 0xD8, 0x1C, 0x0E, 0xA2, 0x34, 0xE8, 0xC1, 0xD0, 0xA4, 0x51, 0xD5, 0x88, 0x8B,
  33114. 0xB7, 0xC6, 0xA3, 0x96, 0x40, 0x49, 0xB7, 0xBC, 0xE0, 0x7F, 0x55, 0x3F, 0xEF,
  33115. 0x6F, 0x6E, 0x92, 0x9D, 0x34, 0xFE, 0x3C, 0x5F, 0x04, 0xA5, 0x6A, 0xFF, 0x30,
  33116. 0x08, 0xC9, 0xEA, 0xF5, 0x52, 0x2B, 0xFE, 0x57, 0xFA, 0x8E, 0xC7, 0xE8, 0x4D,
  33117. 0x37, 0xAB, 0x03, 0xFA, 0x23, 0xBF, 0x46, 0x94, 0xFF, 0xC1, 0x16, 0xE0, 0xB9,
  33118. 0x14, 0x2C, 0x9E, 0x27, 0xEC, 0x98, 0x69, 0x14, 0x92, 0xF1, 0x60, 0x5C, 0x34,
  33119. 0x4D, 0xA0, 0x1F, 0xDF, 0xFD, 0x44, 0x1C, 0x7B, 0xD3, 0x80, 0x70, 0x42, 0x02,
  33120. 0x30, 0x84, 0x5B, 0xE5, 0x59, 0xB7, 0xF3, 0x80, 0xFB, 0x01, 0x33, 0xA9, 0x00,
  33121. 0x37, 0x52, 0xDC, 0xDA, 0xA7, 0x11, 0x85, 0xB7, 0x6E, 0x70, 0xE4, 0xDA, 0x96,
  33122. 0xBA, 0x84, 0x5B, 0x81, 0x43, 0x93, 0xF3, 0xD1, 0xEA, 0xB1, 0xDD, 0xB8, 0x1F,
  33123. 0xA5, 0xCC, 0xEA, 0x50, 0x66, 0x69, 0xA9, 0x8D, 0x8C, 0xA7, 0xA2, 0xF3, 0x38,
  33124. 0x26, 0x43, 0x5E, 0x3F, 0x01, 0xBE, 0x1C, 0x0F, 0x20, 0x7F, 0x75, 0xA8, 0x20,
  33125. 0x80, 0xC4, 0xC3, 0x5C, 0x8B, 0x0D, 0xD4, 0x60, 0x5E, 0xA3, 0x9E, 0xD0, 0xB4,
  33126. 0x4B, 0x4F, 0xE6, 0x13, 0x85, 0x60, 0x42, 0x96, 0xED, 0xAA, 0xDB, 0xE9, 0x99,
  33127. 0xE3, 0x07, 0x0E, 0x61, 0xB3, 0x07, 0xE3, 0xB1, 0xFA, 0xC0, 0x9B, 0xAD, 0xF6,
  33128. 0xE0, 0x26, 0x33, 0xEA, 0xEA, 0x23, 0xCD, 0x1E, 0x9D, 0xE1, 0x87, 0x4B, 0x74,
  33129. 0x97, 0x08, 0x3E, 0xA1, 0x28, 0xEA, 0xB3, 0x19, 0x67, 0x8B, 0x76, 0x9A, 0xA3,
  33130. 0xF6, 0xB9, 0xCF, 0x80, 0x65, 0x97, 0xAE, 0xF4, 0x83, 0x6B, 0xF4, 0x43, 0x20,
  33131. 0xF9, 0x0B, 0xFC, 0x9B, 0xD2, 0x4D, 0x4D, 0xA6, 0xB9, 0xA3, 0x02, 0x55, 0x79,
  33132. 0x18, 0x36, 0x19, 0x5F, 0xC9, 0xEA, 0x5A, 0x76, 0x40, 0xB9, 0xBA, 0x0E, 0x9A,
  33133. 0x44, 0xDF, 0x7C, 0xF8, 0x65, 0x61, 0x5E, 0x81, 0xAB, 0x71, 0xA1, 0x9E, 0x29,
  33134. 0x3C, 0x59, 0xCB, 0x23, 0xA4, 0xF6, 0x60, 0x1A, 0x0D, 0x5B, 0x39, 0xAE, 0xF4,
  33135. 0x6F, 0x59, 0x16, 0x9E, 0x60, 0xD8, 0x56, 0xCF, 0xEA, 0x2C, 0x4C, 0x79, 0xD3,
  33136. 0x5D, 0x51, 0x46, 0xA0, 0x4E, 0xE9, 0xD6, 0xAB, 0x91, 0x43, 0x63, 0x44, 0xD7,
  33137. 0x70, 0xB9, 0x23, 0x98, 0x4F, 0x3D, 0x03, 0x02, 0xF6, 0x81, 0x56, 0xC1, 0x58,
  33138. 0x85, 0x07, 0xA7, 0x2D, 0x2C, 0x29, 0xCA, 0x01, 0x45, 0x31, 0x51, 0x8F, 0xD4,
  33139. 0x19, 0xA1, 0x79, 0x88, 0x5A, 0xA4, 0xF5, 0xAE, 0x2D, 0x4B, 0x63, 0x4C, 0x58,
  33140. 0xFE, 0xBF, 0xAD, 0xEE, 0xA3, 0x09, 0xF8, 0xE2, 0x89, 0xBE, 0x81, 0x0E, 0x86,
  33141. 0x3A, 0xF9, 0x5B, 0xA5, 0xD8, 0xA4, 0x00, 0x75, 0x04, 0xF2, 0x23, 0xB8, 0x39,
  33142. 0x69, 0x50, 0xB7, 0xD0, 0x34, 0x63, 0x54, 0xD8, 0x61, 0xDD, 0xA5, 0x33, 0x47,
  33143. 0x85, 0x96, 0x22, 0xD0, 0x2F, 0x9F, 0x7E, 0xF8, 0x74, 0x24, 0xEA, 0x57, 0x97,
  33144. 0x5A, 0xE0, 0x00, 0xCF, 0xC1, 0x67, 0xE1, 0x41, 0xBD, 0x94, 0xA1, 0x03, 0xD3,
  33145. 0xB4, 0x08, 0x64, 0xF2, 0x17, 0x27, 0x35, 0x37, 0x53, 0xEF, 0x46, 0xCE, 0xD8,
  33146. 0xD4, 0x09, 0x52, 0xC6, 0x1E, 0xF7, 0x28, 0xDF, 0x08, 0x0F, 0xD0, 0x6F, 0x71,
  33147. 0xA6, 0xDF, 0xE4, 0x60, 0x8E, 0xC0, 0x1E, 0x78, 0x86, 0x50, 0xB0, 0x9B, 0x84,
  33148. 0x7E, 0xE8, 0x36, 0xFA, 0x95, 0xF1, 0x12, 0x51, 0xC7, 0x18, 0x96, 0xA2, 0x29,
  33149. 0xBB, 0x70, 0x02, 0xB4, 0xF9, 0xA8, 0x3D, 0x08, 0x66, 0xA9, 0xB3, 0xFC, 0x0A,
  33150. 0x94, 0x80, 0xFD, 0x78, 0xDC, 0xAB, 0x82, 0x5A, 0xD2, 0xCD, 0xC2, 0x87, 0xC6,
  33151. 0x4B, 0x07, 0xFA, 0xD1, 0xC3, 0xD9, 0x34, 0x41, 0x85, 0xF8, 0xD0, 0xB6, 0x0A,
  33152. 0x9D, 0x00, 0x91, 0x35, 0x05, 0x88, 0xC3, 0xE3, 0x9B, 0x22, 0xD2, 0xB8, 0xFD,
  33153. 0x95, 0x3E, 0x6D, 0x5D, 0x48, 0xA3, 0x68, 0xCF, 0x02, 0x42, 0x79, 0x79, 0x8A,
  33154. 0xAA, 0x01, 0xD6, 0x09, 0x14, 0x2C, 0xF4, 0x83, 0xA3, 0x80, 0x31, 0x55, 0x46,
  33155. 0x6E, 0xC5, 0xE5, 0x2F, 0x30, 0x58, 0x81, 0xA2, 0x90, 0xBE, 0x2E, 0xA1, 0xC3,
  33156. 0x0F, 0xA6, 0xF5, 0x51, 0x00, 0x39, 0xB6, 0xF2, 0x2A, 0xA3, 0x15, 0x7D, 0x8D,
  33157. 0xF5, 0x66, 0x5C, 0xD9, 0xFC, 0xCF, 0x2F, 0xBF, 0x08, 0x27, 0xE7, 0xD0, 0x03,
  33158. 0xB8, 0xD9, 0x00, 0x13, 0x3D, 0x01, 0x6B, 0xB6, 0xA8, 0xCD, 0x5B, 0x3B, 0x3E,
  33159. 0x93, 0xBF, 0xE6, 0x2E, 0xB7, 0x4A, 0xCF, 0xB3, 0x0A, 0xCE, 0x62, 0x11, 0xD6,
  33160. 0x1F, 0x68, 0x9B, 0x1D, 0x68, 0xD1, 0x8C, 0x97, 0xBD, 0xA1, 0x07, 0x67, 0x73,
  33161. 0x87, 0xE0, 0x36, 0xDA, 0x8C, 0xD2, 0xD2, 0xBB, 0x84, 0x28, 0xA9, 0xFE, 0x52,
  33162. 0x74, 0xD6, 0xB9, 0x0F, 0x0A, 0x6A, 0x2D, 0x28, 0x35, 0x34, 0x3A, 0xD3, 0xE2,
  33163. 0xCD, 0x35, 0x06, 0x7D, 0x1B, 0x35, 0x85, 0x86, 0xD1, 0x3E, 0xF2, 0x6F, 0xA1,
  33164. 0xC4, 0x55, 0xBD, 0x00, 0xD8, 0xC3, 0x5D, 0xC2, 0x1D, 0x6B, 0x6B, 0x27, 0x5B,
  33165. 0x95, 0xF3, 0xAB, 0xB5, 0xD3, 0x37, 0xF2, 0x2C, 0x9C, 0xC7, 0x5D, 0xBD, 0xF1,
  33166. 0x68, 0x1C, 0xAD, 0xF8, 0xB5, 0xE1, 0x29, 0x72, 0x7A, 0x73, 0x62, 0x55, 0x24,
  33167. 0xB9, 0x85, 0xDF, 0x7B, 0x29, 0x7D, 0xDE, 0x08, 0xF5, 0xE4, 0x44, 0xDA, 0x1A,
  33168. 0x30, 0x74, 0xDA, 0xB4, 0x9B, 0x23, 0x9A, 0x3A, 0xC1, 0x53, 0xB2, 0xA2, 0xA3,
  33169. 0x7B, 0x1F, 0xD9, 0x56, 0xD4, 0x4F, 0x9B, 0xB2, 0x1E, 0xEE, 0xB8, 0x6A, 0x4E,
  33170. 0xB5, 0xF4, 0x5A, 0xC9, 0x18, 0x27, 0x9C, 0xDE, 0x14, 0x44, 0xED, 0xC4, 0x3C,
  33171. 0x71, 0x9F, 0x5F, 0xD9, 0x37, 0xA0, 0x78, 0x34, 0x6E, 0xBC, 0xD2, 0x7B, 0x1D,
  33172. 0xFA, 0x08, 0x39, 0x5A, 0x04, 0x73, 0x15, 0xD9, 0x0A, 0x48, 0xC1, 0x2D, 0x15,
  33173. 0x4E, 0x84, 0x30, 0x45, 0x69, 0xB3, 0xE5, 0xF6, 0xAD, 0x09, 0x1E, 0xCC, 0x5F,
  33174. 0x1F, 0x06, 0xD5, 0x58, 0xAD, 0x78, 0xD7, 0x9F, 0xE5, 0xED, 0x3B, 0x09, 0xD5,
  33175. 0xA6, 0x52, 0x6F, 0x92, 0xD3, 0x3C, 0xC6, 0x1E, 0xF2, 0x93, 0x7C, 0xD3, 0x5F,
  33176. 0x70, 0x85, 0x5D, 0xF8, 0xAA, 0x9D, 0xB7, 0x7B, 0x24, 0x5A, 0xE9, 0x0A, 0x35,
  33177. 0x2F, 0xF5, 0xD9, 0x82, 0x02, 0x8A, 0x90, 0x13, 0x5B, 0xB5, 0x67, 0x9C, 0xDD,
  33178. 0xA0, 0x4E, 0x82, 0x27, 0xDA, 0x7E, 0xE8, 0x8E, 0xCD, 0xE1, 0x56, 0x71, 0x2C,
  33179. 0xE6, 0x4E, 0x1F, 0x91, 0xCD, 0x7C, 0x6A, 0xB7, 0x78, 0xD0, 0x26, 0xF3, 0x56,
  33180. 0xA9, 0xD5, 0xA1, 0xC3, 0x3B, 0x98, 0xE9, 0x28, 0x09, 0xEF, 0x50, 0x90, 0xCD,
  33181. 0xC4, 0x8E, 0x75, 0xCC, 0xAC, 0x2D, 0xC9, 0x03, 0x6D, 0xAC, 0xFE, 0xC4, 0x88,
  33182. 0x36, 0xD1, 0x3F, 0xBB, 0x1C, 0x7D, 0xB3, 0x14, 0x61, 0x2C, 0xB7, 0x54, 0x4B,
  33183. 0xDB, 0x64, 0xB6, 0x57, 0x14, 0x16, 0x8E, 0x1E, 0x6C, 0x64, 0xBB, 0x8B, 0x48,
  33184. 0x5D, 0x96, 0x9D, 0xDC, 0x80, 0xA7, 0xF7, 0x54, 0xC7, 0x46, 0x38, 0x3E, 0x44,
  33185. 0xDE, 0x7E, 0x92, 0x8D, 0x07, 0xF6, 0x07, 0x37, 0x4E, 0x16, 0x10, 0xB4, 0x7D,
  33186. 0x88, 0x66, 0x7F, 0xBB, 0xFF, 0xEA, 0x00, 0xF3, 0xFF, 0x97, 0x2C, 0xB5, 0xBE,
  33187. 0x35, 0x4B, 0x5C, 0x36, 0xEC, 0x4C, 0xBD, 0x2B, 0x7D, 0xBF, 0x46, 0xE2, 0x9C,
  33188. 0x0E, 0x8A, 0xA3, 0xEC, 0xB1, 0x0E, 0x9A, 0xDA, 0x9A, 0x9B, 0x28, 0x92, 0x10,
  33189. 0x53, 0x57, 0xEA, 0xEC, 0xA2, 0x32, 0x32, 0x20, 0x1D, 0x97, 0x5C, 0xB6, 0x84,
  33190. 0xA9, 0x93, 0x8D, 0x95, 0x11, 0xA3, 0x24, 0xA3, 0x2D, 0xC6, 0x4A, 0xEF, 0xAA,
  33191. 0x1D, 0x85, 0x2B, 0x7D, 0x28, 0xBE, 0x53, 0xCE, 0x10, 0x1F, 0xAE, 0x0E, 0x41,
  33192. 0x6C, 0x4B, 0x79, 0x12, 0xFB, 0xF7, 0x54, 0xA3, 0x96, 0x54, 0x83, 0x20, 0x96,
  33193. 0x8F, 0x28, 0xA9, 0x3F, 0x8B, 0x3D, 0xBA, 0x77, 0xDC, 0x24, 0xE1, 0xD4, 0x49,
  33194. 0x40, 0xD8, 0x78, 0x31, 0x85, 0x43, 0xF6, 0xFE, 0x5C, 0xA6, 0x8F, 0x90, 0x09,
  33195. 0xB0, 0xE7, 0xC4, 0x95, 0xB2, 0x55, 0x49, 0x97, 0x8F, 0x1C, 0x78, 0x30, 0x20,
  33196. 0xA0, 0xB4, 0xEF, 0x73, 0x56, 0x59, 0x82, 0xFD, 0xCE, 0xBA, 0x6A, 0x8F, 0x2C,
  33197. 0x8B, 0x15, 0xFD, 0xA1, 0x85, 0xA8, 0x5C, 0x0F, 0x11, 0xA5, 0x9D, 0xC2, 0x46,
  33198. 0xC6, 0x9C, 0xC9, 0x40, 0x0B, 0x58, 0x6A, 0x1C, 0x7A, 0x23, 0xF9, 0xE0, 0x95,
  33199. 0x05, 0x13, 0x58, 0x72, 0xE8, 0x9F, 0x30, 0xAC, 0xCD, 0x26, 0xD4, 0x66, 0x13,
  33200. 0xDF, 0x1E, 0x7B, 0x4F, 0x9C, 0xBE, 0x38, 0x79, 0x75, 0x92, 0xA4, 0xDA, 0x26,
  33201. 0x44, 0x55, 0x17, 0xA3, 0xE5, 0x62, 0xDA, 0xEB, 0x86, 0xEA, 0x68, 0xC7, 0xAB,
  33202. 0xFD, 0x2D, 0x43, 0x59, 0x51, 0xC0, 0x75, 0x64, 0x91, 0x01, 0x29, 0x33, 0x28,
  33203. 0xF3, 0x04, 0x83, 0x80, 0x75, 0x37, 0x75, 0x0C, 0x03, 0x7B, 0x0A, 0xAB, 0x8E,
  33204. 0x60, 0x62, 0x8B, 0x4C, 0xAF, 0x2D, 0xA3, 0x2F, 0xFE, 0xAB, 0x45, 0xCF, 0xDA,
  33205. 0xAB, 0xFA, 0xFA, 0x30, 0x3D, 0xE8, 0xA1, 0x96, 0xA5, 0x7B, 0xE2, 0x2A, 0xD0,
  33206. 0xAF, 0x59, 0xF7, 0xD0, 0x32, 0x57, 0x19, 0xBD, 0xCA, 0x9F, 0xD5, 0x1A, 0xC7,
  33207. 0xAA, 0x65, 0x4A, 0x38, 0xB2, 0x70, 0x33, 0xB7, 0x75, 0xD2, 0xCD, 0xD1, 0xF0,
  33208. 0xA8, 0x87, 0x59, 0x20, 0xA5, 0x57, 0x55, 0xB1, 0xB2, 0xC9, 0x4D, 0x97, 0x34,
  33209. 0x41, 0xF3, 0xF0, 0x30, 0xA1, 0x2C, 0x1C, 0x49, 0x3E, 0x89, 0x7D, 0x12, 0xE2,
  33210. 0xC3, 0x04, 0xC3, 0x92, 0xC0, 0xF6, 0x39, 0x10, 0x80, 0x81, 0x8F, 0x08, 0xB4,
  33211. 0xF8, 0xB9, 0x13, 0x4E, 0x2C, 0xAE, 0xB3, 0x71, 0x82, 0x63, 0x98, 0xAB, 0x5C,
  33212. 0x1C, 0x10, 0xEA, 0x66, 0xF9, 0x02, 0x3A, 0x82, 0x61, 0xD0, 0xD4, 0xAE, 0x43,
  33213. 0xD4, 0x01, 0x3E, 0x9D, 0x04, 0x14, 0xF6, 0x60, 0xD8, 0xA7, 0xD6, 0xB8, 0x53,
  33214. 0xC8, 0xDA, 0x80, 0x93, 0xA0, 0x02, 0xDD, 0xCC, 0xE2, 0xF2, 0xBB, 0xFB, 0xE0,
  33215. 0x27, 0xD7, 0x34, 0x9A, 0x71, 0x49, 0xB5, 0x4F, 0x42, 0x1F, 0xB2, 0x9D, 0x6D,
  33216. 0xAA, 0x9D, 0xD3, 0x50, 0xB5, 0x8F, 0x6A, 0x4B, 0xDF, 0x1F, 0xD5, 0x27, 0x8F,
  33217. 0x3B, 0x27, 0xCF, 0x2F, 0x8C, 0xF8, 0x9D, 0x4C, 0x52, 0xBC, 0x32, 0x0F, 0x73,
  33218. 0xD5, 0x51, 0x8E, 0x36, 0x7E, 0xAD, 0x09, 0xF0, 0x94, 0x83, 0x5F, 0x36, 0xFD,
  33219. 0x7C, 0x03, 0xED, 0xF1, 0x5E, 0x4B, 0xF7, 0xAA, 0x55, 0x5C, 0x4A, 0x14, 0x59,
  33220. 0x85, 0x38, 0x2D, 0x8C, 0xDF, 0xEC, 0x65, 0x1B, 0xB8, 0x76, 0x57, 0x96, 0x3C,
  33221. 0x86, 0xED, 0xF2, 0x7F, 0x2D, 0x28, 0x48, 0xDA, 0x49, 0x7F, 0xF7, 0x54, 0x2B,
  33222. 0xD5, 0x39, 0xD5, 0x57, 0x0A, 0x75, 0x7A, 0x3E, 0x5E, 0x5D, 0xBA, 0x4A, 0x15,
  33223. 0xFA, 0xB8, 0x31, 0x80, 0x71, 0x2C, 0xCA, 0xC4, 0x51, 0x10, 0x16, 0x5D, 0x39,
  33224. 0xEC, 0x9D, 0x07, 0xB6, 0x6A, 0x89, 0x9F, 0x9B, 0x5B, 0x6F, 0x03, 0xB0, 0x92,
  33225. 0x01, 0x38, 0x6B, 0x48, 0x99, 0x0A, 0x8F, 0x13, 0xC1, 0xA6, 0x01, 0xEA, 0xBF,
  33226. 0x6F, 0x86, 0x43, 0x51, 0xB6, 0x11, 0x00, 0x00
  33227. };
  33228. WOLFSSL_TEST_SUBROUTINE int compress_test(void)
  33229. {
  33230. int ret = 0;
  33231. word32 dSz = sizeof(sample_text);
  33232. word32 cSz = (dSz + (word32)(dSz * 0.001) + 12);
  33233. byte *c;
  33234. byte *d;
  33235. c = (byte *)XMALLOC(cSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33236. d = (byte *)XMALLOC(dSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33237. if (c == NULL || d == NULL) {
  33238. ERROR_OUT(-12100, exit);
  33239. }
  33240. /* follow calloc and initialize to 0 */
  33241. XMEMSET(c, 0, cSz);
  33242. XMEMSET(d, 0, dSz);
  33243. if ((ret = wc_Compress(c, cSz, sample_text, dSz, 0)) < 0) {
  33244. ERROR_OUT(-12101, exit);
  33245. }
  33246. cSz = (word32)ret;
  33247. if ((ret = wc_DeCompress(d, dSz, c, cSz)) != (int)dSz) {
  33248. ERROR_OUT(-12102, exit);
  33249. }
  33250. dSz = (word32)ret;
  33251. if (XMEMCMP(d, sample_text, dSz) != 0) {
  33252. ERROR_OUT(-12103, exit);
  33253. }
  33254. /* GZIP tests */
  33255. cSz = (dSz + (word32)(dSz * 0.001) + 12); /* reset cSz */
  33256. XMEMSET(c, 0, cSz);
  33257. XMEMSET(d, 0, dSz);
  33258. ret = wc_Compress_ex(c, cSz, sample_text, dSz, 0, LIBZ_WINBITS_GZIP);
  33259. if (ret < 0) {
  33260. ERROR_OUT(-12104, exit);
  33261. }
  33262. cSz = (word32)ret;
  33263. ret = wc_DeCompress_ex(d, dSz, c, cSz, LIBZ_WINBITS_GZIP);
  33264. if (ret < 0) {
  33265. ERROR_OUT(-12105, exit);
  33266. }
  33267. if (XMEMCMP(d, sample_text, dSz) != 0) {
  33268. ERROR_OUT(-12106, exit);
  33269. }
  33270. /* Try with gzip generated output */
  33271. XMEMSET(d, 0, dSz);
  33272. ret = wc_DeCompress_ex(d, dSz, sample_text_gz, sizeof(sample_text_gz),
  33273. LIBZ_WINBITS_GZIP);
  33274. if (ret < 0) {
  33275. ERROR_OUT(-12107, exit);
  33276. }
  33277. dSz = (word32)ret;
  33278. if (XMEMCMP(d, sample_text, dSz) != 0) {
  33279. ERROR_OUT(-12108, exit);
  33280. }
  33281. ret = 0; /* success */
  33282. exit:
  33283. if (c) XFREE(c, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33284. if (d) XFREE(d, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33285. return ret;
  33286. }
  33287. #endif /* HAVE_LIBZ */
  33288. #ifdef HAVE_PKCS7
  33289. /* External Debugging/Testing Note:
  33290. *
  33291. * PKCS#7 test functions can output generated PKCS#7/CMS bundles for
  33292. * additional testing. To dump bundles to files DER encoded files, please
  33293. * define:
  33294. *
  33295. * #define PKCS7_OUTPUT_TEST_BUNDLES
  33296. */
  33297. /* Loads certs and keys for use with PKCS7 tests, from either files
  33298. * or buffers.
  33299. *
  33300. * rsaClientCertBuf - output buffer for RSA client cert
  33301. * rsaClientCertBufSz - IN/OUT size of output buffer, size of RSA client cert
  33302. * rsaClientPrivKeyBuf - output buffer for RSA client private key
  33303. * rsaClientPrivKeyBufSz - IN/OUT size of output buffer, size of RSA client key
  33304. *
  33305. * rsaServerCertBuf - output buffer for RSA server cert
  33306. * rsaServerCertBufSz - IN/OUT size of output buffer, size of RSA server cert
  33307. * rsaServerPrivKeyBuf - output buffer for RSA server private key
  33308. * rsaServerPrivKeyBufSz - IN/OUT size of output buffer, size of RSA server key
  33309. *
  33310. * rsaCaCertBuf - output buffer for RSA CA cert
  33311. * rsaCaCertBufSz - IN/OUT size of output buffer, size of RSA ca cert
  33312. * rsaCaPrivKeyBuf - output buffer for RSA CA private key
  33313. * rsaCaPrivKeyBufSz - IN/OUT size of output buffer, size of RSA CA key
  33314. *
  33315. * eccClientCertBuf - output buffer for ECC cert
  33316. * eccClientCertBufSz - IN/OUT size of output buffer, size of ECC cert
  33317. * eccClientPrivKeyBuf - output buffer for ECC private key
  33318. * eccClientPrivKeyBufSz - IN/OUT size of output buffer, size of ECC private key
  33319. *
  33320. * Returns 0 on success, negative on error
  33321. */
  33322. static int pkcs7_load_certs_keys(
  33323. byte* rsaClientCertBuf, word32* rsaClientCertBufSz,
  33324. byte* rsaClientPrivKeyBuf, word32* rsaClientPrivKeyBufSz,
  33325. byte* rsaServerCertBuf, word32* rsaServerCertBufSz,
  33326. byte* rsaServerPrivKeyBuf, word32* rsaServerPrivKeyBufSz,
  33327. byte* rsaCaCertBuf, word32* rsaCaCertBufSz,
  33328. byte* rsaCaPrivKeyBuf, word32* rsaCaPrivKeyBufSz,
  33329. byte* eccClientCertBuf, word32* eccClientCertBufSz,
  33330. byte* eccClientPrivKeyBuf, word32* eccClientPrivKeyBufSz)
  33331. {
  33332. #ifndef NO_FILESYSTEM
  33333. XFILE certFile;
  33334. XFILE keyFile;
  33335. (void)certFile;
  33336. (void)keyFile;
  33337. #endif
  33338. #ifndef NO_RSA
  33339. if (rsaClientCertBuf == NULL || rsaClientCertBufSz == NULL ||
  33340. rsaClientPrivKeyBuf == NULL || rsaClientPrivKeyBufSz == NULL)
  33341. return BAD_FUNC_ARG;
  33342. #endif
  33343. #ifdef HAVE_ECC
  33344. if (eccClientCertBuf == NULL || eccClientCertBufSz == NULL ||
  33345. eccClientPrivKeyBuf == NULL || eccClientPrivKeyBufSz == NULL)
  33346. return BAD_FUNC_ARG;
  33347. #endif
  33348. /* RSA */
  33349. #ifndef NO_RSA
  33350. #ifdef USE_CERT_BUFFERS_1024
  33351. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_1024)
  33352. return -12110;
  33353. XMEMCPY(rsaClientCertBuf, client_cert_der_1024,
  33354. sizeof_client_cert_der_1024);
  33355. *rsaClientCertBufSz = sizeof_client_cert_der_1024;
  33356. if (rsaServerCertBuf != NULL) {
  33357. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_1024)
  33358. return -12111;
  33359. XMEMCPY(rsaServerCertBuf, server_cert_der_1024,
  33360. sizeof_server_cert_der_1024);
  33361. *rsaServerCertBufSz = sizeof_server_cert_der_1024;
  33362. }
  33363. if (rsaCaCertBuf != NULL) {
  33364. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_1024)
  33365. return -12112;
  33366. XMEMCPY(rsaCaCertBuf, ca_cert_der_1024, sizeof_ca_cert_der_1024);
  33367. *rsaCaCertBufSz = sizeof_ca_cert_der_1024;
  33368. }
  33369. #elif defined(USE_CERT_BUFFERS_2048)
  33370. if (*rsaClientCertBufSz < (word32)sizeof_client_cert_der_2048)
  33371. return -12113;
  33372. XMEMCPY(rsaClientCertBuf, client_cert_der_2048,
  33373. sizeof_client_cert_der_2048);
  33374. *rsaClientCertBufSz = sizeof_client_cert_der_2048;
  33375. if (rsaServerCertBuf != NULL) {
  33376. if (*rsaServerCertBufSz < (word32)sizeof_server_cert_der_2048)
  33377. return -12114;
  33378. XMEMCPY(rsaServerCertBuf, server_cert_der_2048,
  33379. sizeof_server_cert_der_2048);
  33380. *rsaServerCertBufSz = sizeof_server_cert_der_2048;
  33381. }
  33382. if (rsaCaCertBuf != NULL) {
  33383. if (*rsaCaCertBufSz < (word32)sizeof_ca_cert_der_2048)
  33384. return -12115;
  33385. XMEMCPY(rsaCaCertBuf, ca_cert_der_2048, sizeof_ca_cert_der_2048);
  33386. *rsaCaCertBufSz = sizeof_ca_cert_der_2048;
  33387. }
  33388. #else
  33389. certFile = XFOPEN(clientCert, "rb");
  33390. if (!certFile)
  33391. return -12116;
  33392. *rsaClientCertBufSz = (word32)XFREAD(rsaClientCertBuf, 1,
  33393. *rsaClientCertBufSz, certFile);
  33394. XFCLOSE(certFile);
  33395. if (rsaServerCertBuf != NULL) {
  33396. certFile = XFOPEN(rsaServerCertDerFile, "rb");
  33397. if (!certFile)
  33398. return -12117;
  33399. *rsaServerCertBufSz = (word32)XFREAD(rsaServerCertBuf, 1,
  33400. *rsaServerCertBufSz, certFile);
  33401. XFCLOSE(certFile);
  33402. }
  33403. if (rsaCaCertBuf != NULL) {
  33404. certFile = XFOPEN(rsaCaCertDerFile, "rb");
  33405. if (!certFile)
  33406. return -12118;
  33407. *rsaCaCertBufSz = (word32)XFREAD(rsaCaCertBuf, 1, *rsaCaCertBufSz,
  33408. certFile);
  33409. XFCLOSE(certFile);
  33410. }
  33411. #endif
  33412. #ifdef USE_CERT_BUFFERS_1024
  33413. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_1024)
  33414. return -12119;
  33415. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_1024,
  33416. sizeof_client_key_der_1024);
  33417. *rsaClientPrivKeyBufSz = sizeof_client_key_der_1024;
  33418. if (rsaServerPrivKeyBuf != NULL) {
  33419. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_1024)
  33420. return -12120;
  33421. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_1024,
  33422. sizeof_server_key_der_1024);
  33423. *rsaServerPrivKeyBufSz = sizeof_server_key_der_1024;
  33424. }
  33425. if (rsaCaPrivKeyBuf != NULL) {
  33426. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_1024)
  33427. return -12121;
  33428. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_1024, sizeof_ca_key_der_1024);
  33429. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_1024;
  33430. }
  33431. #elif defined(USE_CERT_BUFFERS_2048)
  33432. if (*rsaClientPrivKeyBufSz < (word32)sizeof_client_key_der_2048)
  33433. return -12122;
  33434. XMEMCPY(rsaClientPrivKeyBuf, client_key_der_2048,
  33435. sizeof_client_key_der_2048);
  33436. *rsaClientPrivKeyBufSz = sizeof_client_key_der_2048;
  33437. if (rsaServerPrivKeyBuf != NULL) {
  33438. if (*rsaServerPrivKeyBufSz < (word32)sizeof_server_key_der_2048)
  33439. return -12123;
  33440. XMEMCPY(rsaServerPrivKeyBuf, server_key_der_2048,
  33441. sizeof_server_key_der_2048);
  33442. *rsaServerPrivKeyBufSz = sizeof_server_key_der_2048;
  33443. }
  33444. if (rsaCaPrivKeyBuf != NULL) {
  33445. if (*rsaCaPrivKeyBufSz < (word32)sizeof_ca_key_der_2048)
  33446. return -12124;
  33447. XMEMCPY(rsaCaPrivKeyBuf, ca_key_der_2048, sizeof_ca_key_der_2048);
  33448. *rsaCaPrivKeyBufSz = sizeof_ca_key_der_2048;
  33449. }
  33450. #else
  33451. keyFile = XFOPEN(clientKey, "rb");
  33452. if (!keyFile)
  33453. return -12125;
  33454. *rsaClientPrivKeyBufSz = (word32)XFREAD(rsaClientPrivKeyBuf, 1,
  33455. *rsaClientPrivKeyBufSz, keyFile);
  33456. XFCLOSE(keyFile);
  33457. if (rsaServerPrivKeyBuf != NULL) {
  33458. keyFile = XFOPEN(rsaServerKeyDerFile, "rb");
  33459. if (!keyFile)
  33460. return -12126;
  33461. *rsaServerPrivKeyBufSz = (word32)XFREAD(rsaServerPrivKeyBuf, 1,
  33462. *rsaServerPrivKeyBufSz, keyFile);
  33463. XFCLOSE(keyFile);
  33464. }
  33465. if (rsaCaPrivKeyBuf != NULL) {
  33466. keyFile = XFOPEN(rsaCaKeyFile, "rb");
  33467. if (!keyFile)
  33468. return -12127;
  33469. *rsaCaPrivKeyBufSz = (word32)XFREAD(rsaCaPrivKeyBuf, 1,
  33470. *rsaCaPrivKeyBufSz, keyFile);
  33471. XFCLOSE(keyFile);
  33472. }
  33473. #endif /* USE_CERT_BUFFERS */
  33474. #endif /* NO_RSA */
  33475. /* ECC */
  33476. #ifdef HAVE_ECC
  33477. #ifdef USE_CERT_BUFFERS_256
  33478. if (*eccClientCertBufSz < (word32)sizeof_cliecc_cert_der_256)
  33479. return -12128;
  33480. XMEMCPY(eccClientCertBuf, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  33481. *eccClientCertBufSz = sizeof_cliecc_cert_der_256;
  33482. #else
  33483. certFile = XFOPEN(eccClientCert, "rb");
  33484. if (!certFile)
  33485. return -12129;
  33486. *eccClientCertBufSz = (word32)XFREAD(eccClientCertBuf, 1,
  33487. *eccClientCertBufSz, certFile);
  33488. XFCLOSE(certFile);
  33489. #endif /* USE_CERT_BUFFERS_256 */
  33490. #ifdef USE_CERT_BUFFERS_256
  33491. if (*eccClientPrivKeyBufSz < (word32)sizeof_ecc_clikey_der_256)
  33492. return -12130;
  33493. XMEMCPY(eccClientPrivKeyBuf, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  33494. *eccClientPrivKeyBufSz = sizeof_ecc_clikey_der_256;
  33495. #else
  33496. keyFile = XFOPEN(eccClientKey, "rb");
  33497. if (!keyFile)
  33498. return -12131;
  33499. *eccClientPrivKeyBufSz = (word32)XFREAD(eccClientPrivKeyBuf, 1,
  33500. *eccClientPrivKeyBufSz, keyFile);
  33501. XFCLOSE(keyFile);
  33502. #endif /* USE_CERT_BUFFERS_256 */
  33503. #endif /* HAVE_ECC */
  33504. #ifdef NO_RSA
  33505. (void)rsaClientCertBuf;
  33506. (void)rsaClientCertBufSz;
  33507. (void)rsaClientPrivKeyBuf;
  33508. (void)rsaClientPrivKeyBufSz;
  33509. (void)rsaServerCertBuf;
  33510. (void)rsaServerCertBufSz;
  33511. (void)rsaServerPrivKeyBuf;
  33512. (void)rsaServerPrivKeyBufSz;
  33513. (void)rsaCaCertBuf;
  33514. (void)rsaCaCertBufSz;
  33515. (void)rsaCaPrivKeyBuf;
  33516. (void)rsaCaPrivKeyBufSz;
  33517. #endif
  33518. #ifndef HAVE_ECC
  33519. (void)eccClientCertBuf;
  33520. (void)eccClientCertBufSz;
  33521. (void)eccClientPrivKeyBuf;
  33522. (void)eccClientPrivKeyBufSz;
  33523. #endif
  33524. #ifndef NO_FILESYSTEM
  33525. (void)certFile;
  33526. (void)keyFile;
  33527. #endif
  33528. return 0;
  33529. }
  33530. typedef struct {
  33531. const byte* content;
  33532. word32 contentSz;
  33533. int contentOID;
  33534. int encryptOID;
  33535. int keyWrapOID;
  33536. int keyAgreeOID;
  33537. byte* cert;
  33538. size_t certSz;
  33539. byte* privateKey;
  33540. word32 privateKeySz;
  33541. byte* optionalUkm;
  33542. word32 optionalUkmSz;
  33543. int ktriOptions; /* KTRI options flags */
  33544. int kariOptions; /* KARI options flags */
  33545. /* KEKRI specific */
  33546. const byte* secretKey; /* key, only for kekri RecipientInfo types */
  33547. word32 secretKeySz; /* size of secretKey, bytes */
  33548. const byte* secretKeyId; /* key identifier */
  33549. word32 secretKeyIdSz; /* size of key identifier, bytes */
  33550. void* timePtr; /* time_t pointer */
  33551. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  33552. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  33553. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  33554. word32 otherAttrSz; /* size of otherAttr, bytes */
  33555. int kekriOptions; /* KEKRI options flags */
  33556. /* PWRI specific */
  33557. const char* password;
  33558. word32 passwordSz;
  33559. const byte* salt;
  33560. word32 saltSz;
  33561. int kdfOID;
  33562. int hashOID;
  33563. int kdfIterations;
  33564. int pwriOptions; /* PWRI options flags */
  33565. /* ORI specific */
  33566. int isOri;
  33567. int oriOptions; /* ORI options flags */
  33568. const char* outFileName;
  33569. } pkcs7EnvelopedVector;
  33570. static const byte asnDataOid[] = {
  33571. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01
  33572. };
  33573. /* ORI encrypt callback, responsible for encrypting content-encryption key (CEK)
  33574. * and giving wolfCrypt the value for oriOID and oriValue to place in
  33575. * OtherRecipientInfo.
  33576. *
  33577. * Returns 0 on success, negative upon error. */
  33578. static int myOriEncryptCb(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* oriType,
  33579. word32* oriTypeSz, byte* oriValue, word32* oriValueSz,
  33580. void* ctx)
  33581. {
  33582. int i;
  33583. /* make sure buffers are large enough */
  33584. if ((*oriValueSz < (2 + cekSz)) || (*oriTypeSz < sizeof(oriType)))
  33585. return -12140;
  33586. /* our simple encryption algorithm will be take the bitwise complement */
  33587. oriValue[0] = 0x04; /*ASN OCTET STRING */
  33588. oriValue[1] = (byte)cekSz; /* length */
  33589. for (i = 0; i < (int)cekSz; i++) {
  33590. oriValue[2 + i] = ~cek[i];
  33591. }
  33592. *oriValueSz = 2 + cekSz;
  33593. /* set oriType to ASN.1 encoded data OID */
  33594. XMEMCPY(oriType, asnDataOid, sizeof(asnDataOid));
  33595. *oriTypeSz = sizeof(asnDataOid);
  33596. (void)pkcs7;
  33597. (void)ctx;
  33598. return 0;
  33599. }
  33600. /* ORI decrypt callback, responsible for providing a decrypted content
  33601. * encryption key (CEK) placed into decryptedKey and size placed into
  33602. * decryptedKeySz. oriOID and oriValue are given to the callback to help
  33603. * in decrypting the encrypted CEK.
  33604. *
  33605. * Returns 0 on success, negative upon error. */
  33606. static int myOriDecryptCb(PKCS7* pkcs7, byte* oriType, word32 oriTypeSz,
  33607. byte* oriValue, word32 oriValueSz, byte* decryptedKey,
  33608. word32* decryptedKeySz, void* ctx)
  33609. {
  33610. int i;
  33611. /* make sure oriType matches what we expect */
  33612. if (oriTypeSz != sizeof(asnDataOid))
  33613. return -12150;
  33614. if (XMEMCMP(oriType, asnDataOid, sizeof(asnDataOid)) != 0)
  33615. return -12151;
  33616. /* make sure decrypted buffer is large enough */
  33617. if (*decryptedKeySz < oriValueSz)
  33618. return -12152;
  33619. /* decrypt encrypted CEK using simple bitwise complement,
  33620. only for example */
  33621. for (i = 0; i < (int)oriValueSz - 2; i++) {
  33622. decryptedKey[i] = ~oriValue[2 + i];
  33623. }
  33624. *decryptedKeySz = oriValueSz - 2;
  33625. (void)pkcs7;
  33626. (void)ctx;
  33627. return 0;
  33628. }
  33629. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  33630. /* returns 0 on success */
  33631. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  33632. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  33633. byte* in, int inSz, byte* out, void* usrCtx)
  33634. {
  33635. int keyId = -1, ret, keySz;
  33636. word32 keyIdSz = 8;
  33637. const byte* key;
  33638. byte keyIdRaw[8];
  33639. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33640. Aes *aes;
  33641. #else
  33642. Aes aes[1];
  33643. #endif
  33644. /* looking for KEY ID
  33645. * fwDecryptKeyID OID "1.2.840.113549.1.9.16.2.37
  33646. */
  33647. WOLFSSL_SMALL_STACK_STATIC const unsigned char OID[] = {
  33648. /* 0x06, 0x0B do not pass in tag and length */
  33649. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  33650. 0x01, 0x09, 0x10, 0x02, 0x25
  33651. };
  33652. WOLFSSL_SMALL_STACK_STATIC const byte defKey[] = {
  33653. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33654. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33655. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33656. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  33657. };
  33658. WOLFSSL_SMALL_STACK_STATIC const byte altKey[] = {
  33659. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  33660. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  33661. };
  33662. /* test user context passed in */
  33663. if (usrCtx == NULL || *(int*)usrCtx != 1) {
  33664. return -12160;
  33665. }
  33666. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33667. if ((aes = (Aes *)XMALLOC(sizeof *aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
  33668. return -12164;
  33669. #endif
  33670. /* if needing to find keyIdSz can call with NULL */
  33671. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), NULL,
  33672. &keyIdSz);
  33673. if (ret != LENGTH_ONLY_E) {
  33674. printf("Unexpected error %d when getting keyIdSz\n", ret);
  33675. printf("Possibly no KEY ID attribute set\n");
  33676. ERROR_OUT(-12161, out);
  33677. }
  33678. else {
  33679. XMEMSET(keyIdRaw, 0, sizeof(keyIdRaw));
  33680. ret = wc_PKCS7_GetAttributeValue(pkcs7, OID, sizeof(OID), keyIdRaw,
  33681. &keyIdSz);
  33682. if (ret < 0) {
  33683. goto out;
  33684. }
  33685. if (keyIdSz < 3) {
  33686. printf("keyIdSz is smaller than expected\n");
  33687. ERROR_OUT(-12162, out);
  33688. }
  33689. if (keyIdSz > 2 + sizeof(int)) {
  33690. printf("example case was only expecting a keyId of int size\n");
  33691. ERROR_OUT(-12163, out);
  33692. }
  33693. /* keyIdRaw[0] OCTET TAG */
  33694. /* keyIdRaw[1] Length */
  33695. #ifdef BIG_ENDIAN_ORDER
  33696. if (keyIdRaw[1] == 0x01) {
  33697. keyId = 1;
  33698. }
  33699. #else
  33700. XMEMCPY(&keyId, keyIdRaw + 2, sizeof(keyId));
  33701. #endif
  33702. }
  33703. /* Use keyID here if found to select key and decrypt in HSM or in this
  33704. * example just select key and do software decryption */
  33705. if (keyId == 1) {
  33706. key = altKey;
  33707. keySz = sizeof(altKey);
  33708. }
  33709. else {
  33710. key = defKey;
  33711. keySz = sizeof(defKey);
  33712. }
  33713. switch (encryptOID) {
  33714. #ifdef WOLFSSL_AES_256
  33715. case AES256CBCb:
  33716. if ((keySz != 32 ) || (ivSz != AES_BLOCK_SIZE))
  33717. ERROR_OUT(BAD_FUNC_ARG, out);
  33718. break;
  33719. #endif
  33720. #ifdef WOLFSSL_AES_128
  33721. case AES128CBCb:
  33722. if ((keySz != 16 ) || (ivSz != AES_BLOCK_SIZE))
  33723. ERROR_OUT(BAD_FUNC_ARG, out);
  33724. break;
  33725. #endif
  33726. default:
  33727. printf("Unsupported content cipher type for example");
  33728. ERROR_OUT(ALGO_ID_E, out);
  33729. };
  33730. ret = wc_AesInit(aes, HEAP_HINT, INVALID_DEVID);
  33731. if (ret == 0) {
  33732. ret = wc_AesSetKey(aes, key, keySz, iv, AES_DECRYPTION);
  33733. if (ret == 0)
  33734. ret = wc_AesCbcDecrypt(aes, out, in, inSz);
  33735. wc_AesFree(aes);
  33736. }
  33737. out:
  33738. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  33739. XFREE(aes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33740. #endif
  33741. (void)aad;
  33742. (void)aadSz;
  33743. (void)authTag;
  33744. (void)authTagSz;
  33745. return ret;
  33746. }
  33747. #endif /* !NO_AES && HAVE_AES_CBC */
  33748. #define PKCS7_BUF_SIZE 2048
  33749. static int pkcs7enveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  33750. byte* rsaPrivKey, word32 rsaPrivKeySz,
  33751. byte* eccCert, word32 eccCertSz,
  33752. byte* eccPrivKey, word32 eccPrivKeySz)
  33753. {
  33754. int ret = 0, testSz = 0, i;
  33755. int envelopedSz, decodedSz;
  33756. byte *enveloped = NULL;
  33757. byte *decoded = NULL;
  33758. PKCS7* pkcs7 = NULL;
  33759. #ifdef ECC_TIMING_RESISTANT
  33760. WC_RNG rng;
  33761. #endif
  33762. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  33763. XFILE pkcs7File;
  33764. #endif
  33765. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  33766. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  33767. 0x72,0x6c,0x64
  33768. };
  33769. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256) && \
  33770. defined(HAVE_ECC) && defined(WOLFSSL_SHA512)
  33771. byte optionalUkm[] = {
  33772. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  33773. };
  33774. #endif /* NO_AES */
  33775. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128) && \
  33776. !defined(NO_SHA)
  33777. /* encryption key for kekri recipient types */
  33778. WOLFSSL_SMALL_STACK_STATIC const byte secretKey[] = {
  33779. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  33780. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  33781. };
  33782. /* encryption key identifier */
  33783. WOLFSSL_SMALL_STACK_STATIC const byte secretKeyId[] = {
  33784. 0x02,0x02,0x03,0x04
  33785. };
  33786. #endif
  33787. #if !defined(NO_PWDBASED) && !defined(NO_SHA) && \
  33788. !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  33789. #ifndef HAVE_FIPS
  33790. WOLFSSL_SMALL_STACK_STATIC const char password[] = "password"; /* NOTE: Password is too short for FIPS */
  33791. #else
  33792. WOLFSSL_SMALL_STACK_STATIC const char password[] = "passwordFIPS_MODE";
  33793. #endif
  33794. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = {
  33795. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  33796. };
  33797. #endif
  33798. #define MAX_TESTVECTORS_LEN 13
  33799. #define ADD_PKCS7ENVELOPEDVECTOR(...) { \
  33800. pkcs7EnvelopedVector _this_vector = { __VA_ARGS__ }; \
  33801. if (testSz == MAX_TESTVECTORS_LEN) { \
  33802. ret = -12534; \
  33803. goto out; \
  33804. } \
  33805. XMEMCPY(&testVectors[testSz++], &_this_vector, sizeof _this_vector);\
  33806. }
  33807. pkcs7EnvelopedVector *testVectors = NULL;
  33808. #ifdef ECC_TIMING_RESISTANT
  33809. XMEMSET(&rng, 0, sizeof(rng));
  33810. #endif
  33811. testVectors = (pkcs7EnvelopedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  33812. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33813. if (testVectors == NULL) {
  33814. ret = -12534;
  33815. goto out;
  33816. }
  33817. {
  33818. /* key transport key encryption technique */
  33819. #ifndef NO_RSA
  33820. #ifndef NO_DES3
  33821. ADD_PKCS7ENVELOPEDVECTOR(
  33822. data, (word32)sizeof(data), DATA, DES3b, 0, 0, rsaCert, rsaCertSz,
  33823. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  33824. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  33825. "pkcs7envelopedDataDES3.der");
  33826. #endif
  33827. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  33828. #ifdef WOLFSSL_AES_128
  33829. ADD_PKCS7ENVELOPEDVECTOR(
  33830. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, rsaCert, rsaCertSz,
  33831. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  33832. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  33833. "pkcs7envelopedDataAES128CBC.der");
  33834. #endif
  33835. #ifdef WOLFSSL_AES_192
  33836. ADD_PKCS7ENVELOPEDVECTOR(
  33837. data, (word32)sizeof(data), DATA, AES192CBCb, 0, 0, rsaCert, rsaCertSz,
  33838. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  33839. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  33840. "pkcs7envelopedDataAES192CBC.der");
  33841. #endif
  33842. #ifdef WOLFSSL_AES_256
  33843. ADD_PKCS7ENVELOPEDVECTOR(
  33844. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  33845. rsaPrivKey, rsaPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL,
  33846. 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  33847. "pkcs7envelopedDataAES256CBC.der");
  33848. /* explicitly using SKID for SubjectKeyIdentifier */
  33849. ADD_PKCS7ENVELOPEDVECTOR(
  33850. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  33851. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_SKID, 0, NULL, 0, NULL, 0, NULL,
  33852. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  33853. "pkcs7envelopedDataAES256CBC_SKID.der");
  33854. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  33855. ADD_PKCS7ENVELOPEDVECTOR(
  33856. data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  33857. rsaPrivKey, rsaPrivKeySz, NULL, 0, CMS_ISSUER_AND_SERIAL_NUMBER, 0,
  33858. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  33859. 0, 0, 0, 0, "pkcs7envelopedDataAES256CBC_IANDS.der");
  33860. #endif
  33861. #endif /* !NO_AES && HAVE_AES_CBC */
  33862. #endif
  33863. /* key agreement key encryption technique*/
  33864. #ifdef HAVE_ECC
  33865. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  33866. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  33867. ADD_PKCS7ENVELOPEDVECTOR(
  33868. data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP,
  33869. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  33870. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  33871. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  33872. "pkcs7envelopedDataAES128CBC_ECDH_SHA1KDF.der");
  33873. #endif
  33874. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  33875. ADD_PKCS7ENVELOPEDVECTOR(
  33876. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  33877. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  33878. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  33879. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  33880. "pkcs7envelopedDataAES256CBC_ECDH_SHA256KDF.der");
  33881. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  33882. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  33883. ADD_PKCS7ENVELOPEDVECTOR(
  33884. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  33885. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  33886. eccPrivKeySz, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0,
  33887. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  33888. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF.der");
  33889. /* with optional user keying material (ukm) */
  33890. ADD_PKCS7ENVELOPEDVECTOR(
  33891. data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  33892. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  33893. eccPrivKeySz, optionalUkm, sizeof(optionalUkm), 0, 0, NULL, 0,
  33894. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  33895. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF_ukm.der");
  33896. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  33897. #endif /* !NO_AES && HAVE_AES_CBC */
  33898. #endif
  33899. /* kekri (KEKRecipientInfo) recipient types */
  33900. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  33901. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  33902. ADD_PKCS7ENVELOPEDVECTOR(
  33903. data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP, 0,
  33904. NULL, 0, NULL, 0, NULL, 0, 0, 0, secretKey, sizeof(secretKey),
  33905. secretKeyId, sizeof(secretKeyId), NULL, NULL, 0, NULL, 0,
  33906. 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0,
  33907. "pkcs7envelopedDataAES128CBC_KEKRI.der");
  33908. #endif
  33909. #endif /* !NO_AES && HAVE_AES_CBC */
  33910. /* pwri (PasswordRecipientInfo) recipient types */
  33911. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  33912. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  33913. ADD_PKCS7ENVELOPEDVECTOR(
  33914. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0,
  33915. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  33916. NULL, 0, NULL, NULL, 0, NULL, 0, 0, password,
  33917. (word32)XSTRLEN(password), salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  33918. 0, 0, 0, "pkcs7envelopedDataAES128CBC_PWRI.der");
  33919. #endif
  33920. #endif
  33921. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_128)
  33922. /* ori (OtherRecipientInfo) recipient types */
  33923. ADD_PKCS7ENVELOPEDVECTOR(
  33924. data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, NULL, 0, NULL, 0,
  33925. NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0,
  33926. NULL, 0, 0, 0, 0, 0, 1, 0, "pkcs7envelopedDataAES128CBC_ORI.der");
  33927. #endif
  33928. };
  33929. #undef MAX_TESTVECTORS_LEN
  33930. #undef ADD_PKCS7ENVELOPEDVECTOR
  33931. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33932. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  33933. if ((! enveloped) || (! decoded)) {
  33934. ERROR_OUT(-12170, out);
  33935. }
  33936. #ifdef ECC_TIMING_RESISTANT
  33937. #ifndef HAVE_FIPS
  33938. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  33939. #else
  33940. ret = wc_InitRng(&rng);
  33941. #endif
  33942. if (ret != 0) {
  33943. ERROR_OUT(-12171, out);
  33944. }
  33945. #endif
  33946. for (i = 0; i < testSz; i++) {
  33947. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  33948. #ifdef WOLFSSL_ASYNC_CRYPT
  33949. INVALID_DEVID /* async PKCS7 is not supported */
  33950. #else
  33951. devId
  33952. #endif
  33953. );
  33954. if (pkcs7 == NULL) {
  33955. ERROR_OUT(-12172, out);
  33956. }
  33957. if (testVectors[i].secretKey != NULL) {
  33958. /* KEKRI recipient type */
  33959. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  33960. if (ret != 0) {
  33961. ERROR_OUT(-12173, out);
  33962. }
  33963. pkcs7->content = (byte*)testVectors[i].content;
  33964. pkcs7->contentSz = testVectors[i].contentSz;
  33965. pkcs7->contentOID = testVectors[i].contentOID;
  33966. pkcs7->encryptOID = testVectors[i].encryptOID;
  33967. pkcs7->ukm = testVectors[i].optionalUkm;
  33968. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  33969. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  33970. (byte *)testVectors[i].secretKey, testVectors[i].secretKeySz,
  33971. (byte *)testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  33972. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  33973. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  33974. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  33975. if (ret < 0) {
  33976. wc_PKCS7_Free(pkcs7);
  33977. ERROR_OUT(-12174, out);
  33978. }
  33979. /* set key, for decryption */
  33980. ret = wc_PKCS7_SetKey(pkcs7, (byte *)testVectors[i].secretKey,
  33981. testVectors[i].secretKeySz);
  33982. if (ret != 0) {
  33983. wc_PKCS7_Free(pkcs7);
  33984. ERROR_OUT(-12175, out);
  33985. }
  33986. } else if (testVectors[i].password != NULL) {
  33987. #if !defined(NO_PWDBASED) && !defined(NO_SHA)
  33988. /* PWRI recipient type */
  33989. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  33990. if (ret != 0) {
  33991. ERROR_OUT(-12176, out);
  33992. }
  33993. pkcs7->content = (byte*)testVectors[i].content;
  33994. pkcs7->contentSz = testVectors[i].contentSz;
  33995. pkcs7->contentOID = testVectors[i].contentOID;
  33996. pkcs7->encryptOID = testVectors[i].encryptOID;
  33997. pkcs7->ukm = testVectors[i].optionalUkm;
  33998. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  33999. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  34000. (byte *)testVectors[i].password, testVectors[i].passwordSz,
  34001. (byte *)testVectors[i].salt, testVectors[i].saltSz,
  34002. testVectors[i].kdfOID,
  34003. testVectors[i].hashOID, testVectors[i].kdfIterations,
  34004. testVectors[i].encryptOID, testVectors[i].pwriOptions);
  34005. if (ret < 0) {
  34006. wc_PKCS7_Free(pkcs7);
  34007. ERROR_OUT(-12177, out);
  34008. }
  34009. /* set password, for decryption */
  34010. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  34011. testVectors[i].passwordSz);
  34012. if (ret < 0) {
  34013. wc_PKCS7_Free(pkcs7);
  34014. ERROR_OUT(-12178, out);
  34015. }
  34016. #endif /* ! NO_PWDBASED && ! NO_SHA */
  34017. } else if (testVectors[i].isOri == 1) {
  34018. /* ORI recipient type */
  34019. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  34020. if (ret != 0) {
  34021. ERROR_OUT(-12179, out);
  34022. }
  34023. pkcs7->content = (byte*)testVectors[i].content;
  34024. pkcs7->contentSz = testVectors[i].contentSz;
  34025. pkcs7->contentOID = testVectors[i].contentOID;
  34026. pkcs7->encryptOID = testVectors[i].encryptOID;
  34027. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  34028. testVectors[i].oriOptions);
  34029. if (ret < 0) {
  34030. wc_PKCS7_Free(pkcs7);
  34031. ERROR_OUT(-12180, out);
  34032. }
  34033. /* set decrypt callback for decryption */
  34034. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  34035. if (ret < 0) {
  34036. wc_PKCS7_Free(pkcs7);
  34037. ERROR_OUT(-12181, out);
  34038. }
  34039. } else {
  34040. /* KTRI or KARI recipient types */
  34041. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  34042. if (ret != 0) {
  34043. ERROR_OUT(-12182, out);
  34044. }
  34045. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  34046. (word32)testVectors[i].certSz);
  34047. if (ret != 0) {
  34048. wc_PKCS7_Free(pkcs7);
  34049. ERROR_OUT(-12183, out);
  34050. }
  34051. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  34052. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  34053. pkcs7->privateKey = testVectors[i].privateKey;
  34054. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  34055. pkcs7->content = (byte*)testVectors[i].content;
  34056. pkcs7->contentSz = testVectors[i].contentSz;
  34057. pkcs7->contentOID = testVectors[i].contentOID;
  34058. pkcs7->encryptOID = testVectors[i].encryptOID;
  34059. pkcs7->ukm = testVectors[i].optionalUkm;
  34060. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  34061. /* set SubjectIdentifier type for KTRI types */
  34062. if (testVectors[i].ktriOptions & CMS_SKID) {
  34063. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  34064. if (ret != 0) {
  34065. wc_PKCS7_Free(pkcs7);
  34066. ERROR_OUT(-12184, out);
  34067. }
  34068. } else if (testVectors[i].ktriOptions &
  34069. CMS_ISSUER_AND_SERIAL_NUMBER) {
  34070. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  34071. CMS_ISSUER_AND_SERIAL_NUMBER);
  34072. if (ret != 0) {
  34073. wc_PKCS7_Free(pkcs7);
  34074. ERROR_OUT(-12185, out);
  34075. }
  34076. }
  34077. }
  34078. #ifdef ECC_TIMING_RESISTANT
  34079. pkcs7->rng = &rng;
  34080. #endif
  34081. /* encode envelopedData */
  34082. envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, enveloped,
  34083. PKCS7_BUF_SIZE);
  34084. if (envelopedSz <= 0) {
  34085. wc_PKCS7_Free(pkcs7);
  34086. ERROR_OUT(-12186, out);
  34087. }
  34088. /* decode envelopedData */
  34089. pkcs7->contentOID = 0;
  34090. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped, envelopedSz,
  34091. decoded, PKCS7_BUF_SIZE);
  34092. if (pkcs7->contentOID != testVectors[i].contentOID ||
  34093. decodedSz <= 0) {
  34094. wc_PKCS7_Free(pkcs7);
  34095. ERROR_OUT(-12187, out);
  34096. }
  34097. /* test decode result */
  34098. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  34099. wc_PKCS7_Free(pkcs7);
  34100. ERROR_OUT(-12188, out);
  34101. }
  34102. #ifndef NO_PKCS7_STREAM
  34103. { /* test reading byte by byte */
  34104. int z;
  34105. for (z = 0; z < envelopedSz; z++) {
  34106. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, enveloped + z, 1,
  34107. decoded, PKCS7_BUF_SIZE);
  34108. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  34109. printf("unexpected error %d\n", decodedSz);
  34110. ERROR_OUT(-12189, out);
  34111. }
  34112. }
  34113. /* test decode result */
  34114. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  34115. printf("stream read compare failed\n");
  34116. wc_PKCS7_Free(pkcs7);
  34117. ERROR_OUT(-12190, out);
  34118. }
  34119. }
  34120. #endif
  34121. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  34122. /* output pkcs7 envelopedData for external testing */
  34123. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  34124. if (!pkcs7File) {
  34125. wc_PKCS7_Free(pkcs7);
  34126. ERROR_OUT(-12191, out);
  34127. }
  34128. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  34129. XFCLOSE(pkcs7File);
  34130. if (ret != envelopedSz) {
  34131. wc_PKCS7_Free(pkcs7);
  34132. ERROR_OUT(-12192, out);
  34133. } else {
  34134. /* reset ret to 0 for success */
  34135. ret = 0;
  34136. }
  34137. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  34138. wc_PKCS7_Free(pkcs7);
  34139. pkcs7 = NULL;
  34140. }
  34141. #ifdef ECC_TIMING_RESISTANT
  34142. wc_FreeRng(&rng);
  34143. #endif
  34144. (void)eccCert;
  34145. (void)eccCertSz;
  34146. (void)eccPrivKey;
  34147. (void)eccPrivKeySz;
  34148. (void)rsaCert;
  34149. (void)rsaCertSz;
  34150. (void)rsaPrivKey;
  34151. (void)rsaPrivKeySz;
  34152. out:
  34153. if (testVectors)
  34154. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34155. if (enveloped)
  34156. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34157. if (decoded)
  34158. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34159. return ret;
  34160. }
  34161. WOLFSSL_TEST_SUBROUTINE int pkcs7enveloped_test(void)
  34162. {
  34163. int ret = 0;
  34164. byte* rsaCert = NULL;
  34165. byte* rsaPrivKey = NULL;
  34166. word32 rsaCertSz = 0;
  34167. word32 rsaPrivKeySz = 0;
  34168. byte* eccCert = NULL;
  34169. byte* eccPrivKey = NULL;
  34170. word32 eccCertSz = 0;
  34171. word32 eccPrivKeySz = 0;
  34172. #ifndef NO_RSA
  34173. /* read client RSA cert and key in DER format */
  34174. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34175. if (rsaCert == NULL)
  34176. return -12200;
  34177. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34178. if (rsaPrivKey == NULL) {
  34179. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34180. return -12201;
  34181. }
  34182. rsaCertSz = FOURK_BUF;
  34183. rsaPrivKeySz = FOURK_BUF;
  34184. #endif /* NO_RSA */
  34185. #ifdef HAVE_ECC
  34186. /* read client ECC cert and key in DER format */
  34187. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34188. if (eccCert == NULL) {
  34189. #ifndef NO_RSA
  34190. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34191. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34192. #endif
  34193. return -12202;
  34194. }
  34195. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34196. if (eccPrivKey == NULL) {
  34197. #ifndef NO_RSA
  34198. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34199. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34200. #endif
  34201. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34202. return -12203;
  34203. }
  34204. eccCertSz = FOURK_BUF;
  34205. eccPrivKeySz = FOURK_BUF;
  34206. #endif /* HAVE_ECC */
  34207. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  34208. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  34209. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  34210. eccPrivKey, &eccPrivKeySz);
  34211. if (ret < 0) {
  34212. #ifndef NO_RSA
  34213. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34214. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34215. #endif
  34216. #ifdef HAVE_ECC
  34217. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34218. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34219. #endif
  34220. return -12204;
  34221. }
  34222. ret = pkcs7enveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  34223. rsaPrivKey, (word32)rsaPrivKeySz,
  34224. eccCert, (word32)eccCertSz,
  34225. eccPrivKey, (word32)eccPrivKeySz);
  34226. #ifndef NO_RSA
  34227. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34228. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34229. #endif
  34230. #ifdef HAVE_ECC
  34231. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34232. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34233. #endif
  34234. return ret;
  34235. }
  34236. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  34237. typedef struct {
  34238. const byte* content;
  34239. word32 contentSz;
  34240. int contentOID;
  34241. int encryptOID;
  34242. int keyWrapOID;
  34243. int keyAgreeOID;
  34244. byte* cert;
  34245. size_t certSz;
  34246. byte* privateKey;
  34247. word32 privateKeySz;
  34248. PKCS7Attrib* authAttribs;
  34249. word32 authAttribsSz;
  34250. PKCS7Attrib* unauthAttribs;
  34251. word32 unauthAttribsSz;
  34252. /* KARI / KTRI specific */
  34253. byte* optionalUkm;
  34254. word32 optionalUkmSz;
  34255. int ktriOptions; /* KTRI options flags */
  34256. int kariOptions; /* KARI options flags */
  34257. /* KEKRI specific */
  34258. byte* secretKey; /* key, only for kekri RecipientInfo types */
  34259. word32 secretKeySz; /* size of secretKey, bytes */
  34260. byte* secretKeyId; /* key identifier */
  34261. word32 secretKeyIdSz; /* size of key identifier, bytes */
  34262. void* timePtr; /* time_t pointer */
  34263. byte* otherAttrOID; /* OPTIONAL, other attribute OID */
  34264. word32 otherAttrOIDSz; /* size of otherAttrOID, bytes */
  34265. byte* otherAttr; /* OPTIONAL, other attribute, ASN.1 encoded */
  34266. word32 otherAttrSz; /* size of otherAttr, bytes */
  34267. int kekriOptions; /* KEKRI options flags */
  34268. /* PWRI specific */
  34269. char* password; /* password */
  34270. word32 passwordSz; /* password size, bytes */
  34271. byte* salt; /* KDF salt */
  34272. word32 saltSz; /* KDF salt size, bytes */
  34273. int kdfOID; /* KDF OID */
  34274. int hashOID; /* KDF hash algorithm OID */
  34275. int kdfIterations; /* KDF iterations */
  34276. int kekEncryptOID; /* KEK encryption algorithm OID */
  34277. int pwriOptions; /* PWRI options flags */
  34278. /* ORI specific */
  34279. int isOri;
  34280. int oriOptions; /* ORI options flags */
  34281. const char* outFileName;
  34282. } pkcs7AuthEnvelopedVector;
  34283. static int pkcs7authenveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  34284. byte* rsaPrivKey, word32 rsaPrivKeySz,
  34285. byte* eccCert, word32 eccCertSz,
  34286. byte* eccPrivKey, word32 eccPrivKeySz)
  34287. {
  34288. int ret = 0, testSz = 0, i;
  34289. int envelopedSz, decodedSz;
  34290. byte *enveloped = NULL;
  34291. byte *decoded = NULL;
  34292. WC_RNG rng;
  34293. PKCS7* pkcs7;
  34294. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  34295. XFILE pkcs7File;
  34296. #endif
  34297. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  34298. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  34299. 0x72,0x6c,0x64
  34300. };
  34301. byte senderNonce[PKCS7_NONCE_SZ + 2];
  34302. #ifdef HAVE_ECC
  34303. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  34304. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  34305. WOLFSSL_SMALL_STACK_STATIC const byte senderNonceOid[] =
  34306. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  34307. 0x09, 0x05 };
  34308. PKCS7Attrib attribs[] =
  34309. {
  34310. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  34311. sizeof(senderNonce) }
  34312. };
  34313. #endif
  34314. #endif
  34315. #endif
  34316. #if !defined(NO_AES) && defined(WOLFSSL_AES_256) && defined(HAVE_ECC) && \
  34317. defined(WOLFSSL_SHA512)
  34318. WOLFSSL_SMALL_STACK_STATIC const byte optionalUkm[] = {
  34319. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  34320. };
  34321. #endif /* NO_AES */
  34322. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  34323. /* encryption key for kekri recipient types */
  34324. WOLFSSL_SMALL_STACK_STATIC const byte secretKey[] = {
  34325. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  34326. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  34327. };
  34328. /* encryption key identifier */
  34329. WOLFSSL_SMALL_STACK_STATIC const byte secretKeyId[] = {
  34330. 0x02,0x02,0x03,0x04
  34331. };
  34332. #endif
  34333. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  34334. !defined(NO_SHA) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  34335. #ifndef HAVE_FIPS
  34336. WOLFSSL_SMALL_STACK_STATIC const char password[] = "password";
  34337. #else
  34338. WOLFSSL_SMALL_STACK_STATIC const char password[] = "passwordFIPS_MODE";
  34339. #endif
  34340. WOLFSSL_SMALL_STACK_STATIC const byte salt[] = {
  34341. 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
  34342. };
  34343. #endif
  34344. #define MAX_TESTVECTORS_LEN 20
  34345. #define ADD_PKCS7AUTHENVELOPEDVECTOR(...) { \
  34346. pkcs7AuthEnvelopedVector _this_vector = { __VA_ARGS__ }; \
  34347. if (testSz == MAX_TESTVECTORS_LEN) { \
  34348. ret = -12534; \
  34349. goto out; \
  34350. } \
  34351. XMEMCPY(&testVectors[testSz++], &_this_vector, sizeof _this_vector);\
  34352. }
  34353. pkcs7AuthEnvelopedVector *testVectors = NULL;
  34354. XMEMSET(&rng, 0, sizeof(rng));
  34355. testVectors = (pkcs7AuthEnvelopedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  34356. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34357. if (testVectors == NULL) {
  34358. ret = -12534;
  34359. goto out;
  34360. }
  34361. {
  34362. /* key transport key encryption technique */
  34363. #ifndef NO_RSA
  34364. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  34365. #ifdef WOLFSSL_AES_128
  34366. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34367. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, rsaCert, rsaCertSz,
  34368. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  34369. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  34370. 0, 0, "pkcs7authEnvelopedDataAES128GCM.der");
  34371. #endif
  34372. #ifdef WOLFSSL_AES_192
  34373. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34374. data, (word32)sizeof(data), DATA, AES192GCMb, 0, 0, rsaCert, rsaCertSz,
  34375. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  34376. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  34377. 0, 0, "pkcs7authEnvelopedDataAES192GCM.der");
  34378. #endif
  34379. #ifdef WOLFSSL_AES_256
  34380. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34381. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  34382. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  34383. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0,
  34384. 0, 0, "pkcs7authEnvelopedDataAES256GCM.der");
  34385. /* test with contentType set to FirmwarePkgData */
  34386. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34387. data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, 0, 0,
  34388. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL,
  34389. 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL,
  34390. 0, 0, 0, 0, 0, 0, 0, 0,
  34391. "pkcs7authEnvelopedDataAES256GCM_firmwarePkgData.der");
  34392. /* explicitly using SKID for SubjectKeyIdentifier */
  34393. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34394. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  34395. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0, CMS_SKID, 0,
  34396. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0,
  34397. 0, 0, 0, 0, 0, "pkcs7authEnvelopedDataAES256GCM_SKID.der");
  34398. /* explicitly using IssuerAndSerialNumber for SubjectKeyIdentifier */
  34399. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34400. data, (word32)sizeof(data), DATA, AES256GCMb, 0, 0, rsaCert, rsaCertSz,
  34401. rsaPrivKey, rsaPrivKeySz, NULL, 0, NULL, 0, NULL, 0,
  34402. CMS_ISSUER_AND_SERIAL_NUMBER, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  34403. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  34404. "pkcs7authEnvelopedDataAES256GCM_IANDS.der");
  34405. #endif
  34406. #endif /* NO_AES */
  34407. #endif
  34408. /* key agreement key encryption technique*/
  34409. #ifdef HAVE_ECC
  34410. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  34411. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  34412. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34413. data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP,
  34414. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34415. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  34416. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  34417. "pkcs7authEnvelopedDataAES128GCM_ECDH_SHA1KDF.der");
  34418. #endif
  34419. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_256)
  34420. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34421. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  34422. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34423. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0,
  34424. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  34425. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF.der");
  34426. /* with authenticated attributes */
  34427. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34428. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  34429. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34430. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  34431. NULL, 0, NULL, 0, 0, 0, NULL, 0,
  34432. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  34433. 0, 0, 0,
  34434. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_authAttribs.der");
  34435. /* with unauthenticated attributes */
  34436. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34437. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  34438. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34439. eccPrivKeySz, NULL, 0, attribs,
  34440. (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0, NULL, 0,
  34441. NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0,
  34442. 0, 0, 0,
  34443. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_unauthAttribs.der");
  34444. /* with authenticated AND unauthenticated attributes */
  34445. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34446. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  34447. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34448. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  34449. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  34450. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  34451. 0, 0, 0, 0, 0, 0,
  34452. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_bothAttribs.der");
  34453. /* with authenticated AND unauthenticated attributes AND
  34454. * contentType of FirmwarePkgData */
  34455. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34456. data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256GCMb, AES256_WRAP,
  34457. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34458. eccPrivKeySz, attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)),
  34459. attribs, (sizeof(attribs) / sizeof(PKCS7Attrib)), NULL, 0, 0, 0,
  34460. NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  34461. 0, 0, 0, 0, 0, 0,
  34462. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA256KDF_fw_bothAttribs.der");
  34463. #endif /* NO_SHA256 && WOLFSSL_AES_256 */
  34464. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_AES_256)
  34465. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34466. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  34467. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34468. eccPrivKeySz, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL,
  34469. NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  34470. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF.der");
  34471. /* with optional user keying material (ukm) */
  34472. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34473. data, (word32)sizeof(data), DATA, AES256GCMb, AES256_WRAP,
  34474. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  34475. eccPrivKeySz, NULL, 0, NULL, 0, (byte *)optionalUkm, sizeof(optionalUkm), 0,
  34476. 0, NULL, 0, NULL, 0, NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0,
  34477. 0, 0, 0, 0, 0, 0,
  34478. "pkcs7authEnvelopedDataAES256GCM_ECDH_SHA512KDF_ukm.der");
  34479. #endif /* WOLFSSL_SHA512 && WOLFSSL_AES_256 */
  34480. #endif /* NO_AES */
  34481. #endif
  34482. /* kekri (KEKRecipientInfo) recipient types */
  34483. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  34484. #if !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  34485. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34486. data, (word32)sizeof(data), DATA, AES128GCMb, AES128_WRAP, 0,
  34487. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0,
  34488. (byte *)secretKey, sizeof(secretKey), (byte *)secretKeyId, sizeof(secretKeyId),
  34489. NULL, NULL, 0, NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 0, 0,
  34490. "pkcs7authEnvelopedDataAES128GCM_KEKRI.der");
  34491. #endif
  34492. #endif
  34493. /* pwri (PasswordRecipientInfo) recipient types */
  34494. #if !defined(NO_PWDBASED) && !defined(NO_AES) && defined(HAVE_AESGCM)
  34495. #if !defined(NO_SHA) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  34496. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34497. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0,
  34498. NULL, 0, NULL, 0, NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0,
  34499. NULL, 0, NULL, NULL, 0, NULL, 0, 0, (char *)password,
  34500. (word32)XSTRLEN(password), (byte *)salt, sizeof(salt), PBKDF2_OID, WC_SHA, 5,
  34501. AES128CBCb, 0, 0, 0, "pkcs7authEnvelopedDataAES128GCM_PWRI.der");
  34502. #endif
  34503. #endif
  34504. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  34505. #ifdef WOLFSSL_AES_128
  34506. /* ori (OtherRecipientInfo) recipient types */
  34507. ADD_PKCS7AUTHENVELOPEDVECTOR(
  34508. data, (word32)sizeof(data), DATA, AES128GCMb, 0, 0, NULL, 0, NULL, 0,
  34509. NULL, 0, NULL, 0, NULL, 0, 0, 0, NULL, 0, NULL, 0, NULL, NULL, 0,
  34510. NULL, 0, 0, NULL, 0, NULL, 0, 0, 0, 0, 0, 0, 1, 0,
  34511. "pkcs7authEnvelopedDataAES128GCM_ORI.der");
  34512. #endif
  34513. #endif
  34514. }
  34515. #undef MAX_TESTVECTORS_LEN
  34516. #undef ADD_PKCS7AUTHENVELOPEDVECTOR
  34517. enveloped = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34518. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34519. if ((! enveloped) || (! decoded)) {
  34520. ERROR_OUT(-12210, out);
  34521. }
  34522. /* generate senderNonce */
  34523. {
  34524. #ifndef HAVE_FIPS
  34525. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  34526. #else
  34527. ret = wc_InitRng(&rng);
  34528. #endif
  34529. if (ret != 0) {
  34530. ERROR_OUT(-12211, out);
  34531. }
  34532. senderNonce[0] = 0x04;
  34533. senderNonce[1] = PKCS7_NONCE_SZ;
  34534. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  34535. if (ret != 0) {
  34536. wc_FreeRng(&rng);
  34537. ERROR_OUT(-12212, out);
  34538. }
  34539. }
  34540. for (i = 0; i < testSz; i++) {
  34541. pkcs7 = wc_PKCS7_New(HEAP_HINT,
  34542. #ifdef WOLFSSL_ASYNC_CRYPT
  34543. INVALID_DEVID /* async PKCS7 is not supported */
  34544. #else
  34545. devId
  34546. #endif
  34547. );
  34548. if (pkcs7 == NULL) {
  34549. ERROR_OUT(-12213, out);
  34550. }
  34551. if (testVectors[i].secretKey != NULL) {
  34552. /* KEKRI recipient type */
  34553. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  34554. if (ret != 0) {
  34555. ERROR_OUT(-12214, out);
  34556. }
  34557. pkcs7->content = (byte*)testVectors[i].content;
  34558. pkcs7->contentSz = testVectors[i].contentSz;
  34559. pkcs7->contentOID = testVectors[i].contentOID;
  34560. pkcs7->encryptOID = testVectors[i].encryptOID;
  34561. pkcs7->ukm = testVectors[i].optionalUkm;
  34562. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  34563. pkcs7->authAttribs = testVectors[i].authAttribs;
  34564. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  34565. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  34566. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  34567. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, testVectors[i].keyWrapOID,
  34568. testVectors[i].secretKey, testVectors[i].secretKeySz,
  34569. testVectors[i].secretKeyId, testVectors[i].secretKeyIdSz,
  34570. testVectors[i].timePtr, testVectors[i].otherAttrOID,
  34571. testVectors[i].otherAttrOIDSz, testVectors[i].otherAttr,
  34572. testVectors[i].otherAttrSz, testVectors[i].kekriOptions);
  34573. if (ret < 0) {
  34574. wc_PKCS7_Free(pkcs7);
  34575. ERROR_OUT(-12215, out);
  34576. }
  34577. /* set key, for decryption */
  34578. ret = wc_PKCS7_SetKey(pkcs7, testVectors[i].secretKey,
  34579. testVectors[i].secretKeySz);
  34580. if (ret != 0) {
  34581. wc_PKCS7_Free(pkcs7);
  34582. ERROR_OUT(-12216, out);
  34583. }
  34584. } else if (testVectors[i].password != NULL) {
  34585. #if !defined(NO_PWDBASED) && !defined(NO_SHA)
  34586. /* PWRI recipient type */
  34587. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  34588. if (ret != 0) {
  34589. ERROR_OUT(-12217, out);
  34590. }
  34591. pkcs7->content = (byte*)testVectors[i].content;
  34592. pkcs7->contentSz = testVectors[i].contentSz;
  34593. pkcs7->contentOID = testVectors[i].contentOID;
  34594. pkcs7->encryptOID = testVectors[i].encryptOID;
  34595. pkcs7->ukm = testVectors[i].optionalUkm;
  34596. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  34597. pkcs7->authAttribs = testVectors[i].authAttribs;
  34598. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  34599. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  34600. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  34601. ret = wc_PKCS7_AddRecipient_PWRI(pkcs7,
  34602. (byte*)testVectors[i].password,
  34603. testVectors[i].passwordSz, testVectors[i].salt,
  34604. testVectors[i].saltSz, testVectors[i].kdfOID,
  34605. testVectors[i].hashOID, testVectors[i].kdfIterations,
  34606. testVectors[i].kekEncryptOID, testVectors[i].pwriOptions);
  34607. if (ret < 0) {
  34608. wc_PKCS7_Free(pkcs7);
  34609. ERROR_OUT(-12218, out);
  34610. }
  34611. /* set password, for decryption */
  34612. ret = wc_PKCS7_SetPassword(pkcs7, (byte*)testVectors[i].password,
  34613. testVectors[i].passwordSz);
  34614. if (ret < 0) {
  34615. wc_PKCS7_Free(pkcs7);
  34616. ERROR_OUT(-12219, out);
  34617. }
  34618. #endif /* ! NO_PWDBASED && ! NO_SHA */
  34619. } else if (testVectors[i].isOri == 1) {
  34620. /* ORI recipient type */
  34621. ret = wc_PKCS7_Init(pkcs7, pkcs7->heap, pkcs7->devId);
  34622. if (ret != 0) {
  34623. ERROR_OUT(-12220, out);
  34624. }
  34625. pkcs7->content = (byte*)testVectors[i].content;
  34626. pkcs7->contentSz = testVectors[i].contentSz;
  34627. pkcs7->contentOID = testVectors[i].contentOID;
  34628. pkcs7->encryptOID = testVectors[i].encryptOID;
  34629. pkcs7->authAttribs = testVectors[i].authAttribs;
  34630. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  34631. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  34632. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  34633. ret = wc_PKCS7_AddRecipient_ORI(pkcs7, myOriEncryptCb,
  34634. testVectors[i].oriOptions);
  34635. if (ret < 0) {
  34636. wc_PKCS7_Free(pkcs7);
  34637. ERROR_OUT(-12221, out);
  34638. }
  34639. /* set decrypt callback for decryption */
  34640. ret = wc_PKCS7_SetOriDecryptCb(pkcs7, myOriDecryptCb);
  34641. if (ret < 0) {
  34642. wc_PKCS7_Free(pkcs7);
  34643. ERROR_OUT(-12222, out);
  34644. }
  34645. } else {
  34646. /* KTRI or KARI recipient types */
  34647. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  34648. (word32)testVectors[i].certSz);
  34649. if (ret != 0) {
  34650. wc_PKCS7_Free(pkcs7);
  34651. ERROR_OUT(-12223, out);
  34652. }
  34653. pkcs7->keyWrapOID = testVectors[i].keyWrapOID;
  34654. pkcs7->keyAgreeOID = testVectors[i].keyAgreeOID;
  34655. pkcs7->privateKey = testVectors[i].privateKey;
  34656. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  34657. pkcs7->content = (byte*)testVectors[i].content;
  34658. pkcs7->contentSz = testVectors[i].contentSz;
  34659. pkcs7->contentOID = testVectors[i].contentOID;
  34660. pkcs7->encryptOID = testVectors[i].encryptOID;
  34661. pkcs7->ukm = testVectors[i].optionalUkm;
  34662. pkcs7->ukmSz = testVectors[i].optionalUkmSz;
  34663. pkcs7->authAttribs = testVectors[i].authAttribs;
  34664. pkcs7->authAttribsSz = testVectors[i].authAttribsSz;
  34665. pkcs7->unauthAttribs = testVectors[i].unauthAttribs;
  34666. pkcs7->unauthAttribsSz = testVectors[i].unauthAttribsSz;
  34667. /* set SubjectIdentifier type for KTRI types */
  34668. if (testVectors[i].ktriOptions & CMS_SKID) {
  34669. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  34670. if (ret != 0) {
  34671. wc_PKCS7_Free(pkcs7);
  34672. ERROR_OUT(-12224, out);
  34673. }
  34674. } else if (testVectors[i].ktriOptions &
  34675. CMS_ISSUER_AND_SERIAL_NUMBER) {
  34676. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7,
  34677. CMS_ISSUER_AND_SERIAL_NUMBER);
  34678. if (ret != 0) {
  34679. wc_PKCS7_Free(pkcs7);
  34680. ERROR_OUT(-12225, out);
  34681. }
  34682. }
  34683. }
  34684. #ifdef ECC_TIMING_RESISTANT
  34685. pkcs7->rng = &rng;
  34686. #endif
  34687. /* encode envelopedData */
  34688. envelopedSz = wc_PKCS7_EncodeAuthEnvelopedData(pkcs7, enveloped,
  34689. PKCS7_BUF_SIZE);
  34690. if (envelopedSz <= 0) {
  34691. wc_PKCS7_Free(pkcs7);
  34692. ERROR_OUT(-12226, out);
  34693. }
  34694. #ifndef NO_PKCS7_STREAM
  34695. { /* test reading byte by byte */
  34696. int z;
  34697. for (z = 0; z < envelopedSz; z++) {
  34698. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7,
  34699. enveloped + z, 1, decoded, PKCS7_BUF_SIZE);
  34700. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  34701. printf("unexpected error %d\n", decodedSz);
  34702. ERROR_OUT(-12227, out);
  34703. }
  34704. }
  34705. /* test decode result */
  34706. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  34707. printf("stream read compare failed\n");
  34708. wc_PKCS7_Free(pkcs7);
  34709. ERROR_OUT(-12228, out);
  34710. }
  34711. }
  34712. #endif
  34713. /* decode envelopedData */
  34714. decodedSz = wc_PKCS7_DecodeAuthEnvelopedData(pkcs7, enveloped,
  34715. envelopedSz, decoded,
  34716. PKCS7_BUF_SIZE);
  34717. if (decodedSz <= 0) {
  34718. wc_PKCS7_Free(pkcs7);
  34719. ERROR_OUT(-12229, out);
  34720. }
  34721. /* test decode result */
  34722. if (XMEMCMP(decoded, data, sizeof(data)) != 0){
  34723. wc_PKCS7_Free(pkcs7);
  34724. ERROR_OUT(-12230, out);
  34725. }
  34726. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  34727. /* output pkcs7 envelopedData for external testing */
  34728. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  34729. if (!pkcs7File) {
  34730. wc_PKCS7_Free(pkcs7);
  34731. ERROR_OUT(-12231, out);
  34732. }
  34733. ret = (int)XFWRITE(enveloped, 1, envelopedSz, pkcs7File);
  34734. XFCLOSE(pkcs7File);
  34735. if (ret != envelopedSz) {
  34736. wc_PKCS7_Free(pkcs7);
  34737. ERROR_OUT(-12232, out);
  34738. } else {
  34739. /* reset ret to 0 for success */
  34740. ret = 0;
  34741. }
  34742. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  34743. wc_PKCS7_Free(pkcs7);
  34744. pkcs7 = NULL;
  34745. }
  34746. wc_FreeRng(&rng);
  34747. (void)eccCert;
  34748. (void)eccCertSz;
  34749. (void)eccPrivKey;
  34750. (void)eccPrivKeySz;
  34751. #if !defined(NO_AES) && !defined(NO_SHA) && defined(WOLFSSL_AES_128)
  34752. (void)secretKey;
  34753. (void)secretKeyId;
  34754. #endif
  34755. #ifdef NO_RSA
  34756. (void)rsaCert;
  34757. (void)rsaCertSz;
  34758. (void)rsaPrivKey;
  34759. (void)rsaPrivKeySz;
  34760. #endif
  34761. out:
  34762. if (testVectors)
  34763. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34764. if (enveloped)
  34765. XFREE(enveloped, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34766. if (decoded)
  34767. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34768. return ret;
  34769. }
  34770. WOLFSSL_TEST_SUBROUTINE int pkcs7authenveloped_test(void)
  34771. {
  34772. int ret = 0;
  34773. byte* rsaCert = NULL;
  34774. byte* rsaPrivKey = NULL;
  34775. word32 rsaCertSz = 0;
  34776. word32 rsaPrivKeySz = 0;
  34777. byte* eccCert = NULL;
  34778. byte* eccPrivKey = NULL;
  34779. word32 eccCertSz = 0;
  34780. word32 eccPrivKeySz = 0;
  34781. #ifndef NO_RSA
  34782. /* read client RSA cert and key in DER format */
  34783. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34784. if (rsaCert == NULL)
  34785. return -12300;
  34786. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34787. if (rsaPrivKey == NULL) {
  34788. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34789. return -12301;
  34790. }
  34791. rsaCertSz = FOURK_BUF;
  34792. rsaPrivKeySz = FOURK_BUF;
  34793. #endif /* NO_RSA */
  34794. #ifdef HAVE_ECC
  34795. /* read client ECC cert and key in DER format */
  34796. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34797. if (eccCert == NULL) {
  34798. #ifndef NO_RSA
  34799. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34800. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34801. #endif
  34802. return -12302;
  34803. }
  34804. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34805. if (eccPrivKey == NULL) {
  34806. #ifndef NO_RSA
  34807. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34808. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34809. #endif
  34810. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34811. return -12303;
  34812. }
  34813. eccCertSz = FOURK_BUF;
  34814. eccPrivKeySz = FOURK_BUF;
  34815. #endif /* HAVE_ECC */
  34816. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  34817. &rsaPrivKeySz, NULL, NULL, NULL, NULL,
  34818. NULL, NULL, NULL, NULL, eccCert, &eccCertSz,
  34819. eccPrivKey, &eccPrivKeySz);
  34820. if (ret < 0) {
  34821. #ifndef NO_RSA
  34822. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34823. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34824. #endif
  34825. #ifdef HAVE_ECC
  34826. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34827. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34828. #endif
  34829. return -12304;
  34830. }
  34831. ret = pkcs7authenveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  34832. rsaPrivKey, (word32)rsaPrivKeySz,
  34833. eccCert, (word32)eccCertSz,
  34834. eccPrivKey, (word32)eccPrivKeySz);
  34835. #ifndef NO_RSA
  34836. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34837. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34838. #endif
  34839. #ifdef HAVE_ECC
  34840. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34841. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  34842. #endif
  34843. return ret;
  34844. }
  34845. #endif /* HAVE_AESGCM || HAVE_AESCCM */
  34846. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  34847. static const byte p7DefKey[] = {
  34848. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  34849. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  34850. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  34851. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  34852. };
  34853. static const byte p7AltKey[] = {
  34854. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  34855. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  34856. };
  34857. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  34858. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  34859. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  34860. {
  34861. int ret;
  34862. if (cek == NULL || out == NULL)
  34863. return BAD_FUNC_ARG;
  34864. /* test case sanity checks */
  34865. if (keyIdSz != 1) {
  34866. return -12310;
  34867. }
  34868. if (keyId[0] != 0x00) {
  34869. return -12311;
  34870. }
  34871. if (type != (int)PKCS7_KEKRI) {
  34872. return -12312;
  34873. }
  34874. switch (keyWrapAlgo) {
  34875. case AES256_WRAP:
  34876. ret = wc_AesKeyUnWrap(p7DefKey, sizeof(p7DefKey), cek, cekSz,
  34877. out, outSz, NULL);
  34878. if (ret <= 0)
  34879. return ret;
  34880. break;
  34881. default:
  34882. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  34883. return BAD_KEYWRAP_ALG_E;
  34884. };
  34885. (void)pkcs7;
  34886. (void)direction;
  34887. (void)orginKey; /* used with KAKRI */
  34888. (void)orginKeySz;
  34889. return ret;
  34890. }
  34891. /* returns key size on success */
  34892. static int getFirmwareKey(PKCS7* pkcs7, byte* key, word32 keySz)
  34893. {
  34894. int ret;
  34895. word32 atrSz;
  34896. byte atr[256];
  34897. /* Additionally can look for fwWrappedFirmwareKey
  34898. * 1.2.840.113529.1.9.16.1.16 */
  34899. const unsigned char fwWrappedFirmwareKey[] = {
  34900. /* 0x06, 0x0B */
  34901. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  34902. 0x01, 0x09, 0x10, 0x02, 0x27
  34903. };
  34904. /* find keyID in fwWrappedFirmwareKey */
  34905. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  34906. sizeof(fwWrappedFirmwareKey), NULL, &atrSz);
  34907. if (ret == LENGTH_ONLY_E) {
  34908. XMEMSET(atr, 0, sizeof(atr));
  34909. ret = wc_PKCS7_GetAttributeValue(pkcs7, fwWrappedFirmwareKey,
  34910. sizeof(fwWrappedFirmwareKey), atr, &atrSz);
  34911. /* keyIdRaw[0] OCTET TAG */
  34912. /* keyIdRaw[1] Length */
  34913. if (ret > 0) {
  34914. PKCS7* envPkcs7;
  34915. envPkcs7 = wc_PKCS7_New(NULL, 0);
  34916. if (envPkcs7 == NULL) {
  34917. return MEMORY_E;
  34918. }
  34919. wc_PKCS7_Init(envPkcs7, NULL, 0);
  34920. ret = wc_PKCS7_SetWrapCEKCb(envPkcs7, myCEKwrapFunc);
  34921. if (ret == 0) {
  34922. /* expecting FIRMWARE_PKG_DATA content */
  34923. envPkcs7->contentOID = FIRMWARE_PKG_DATA;
  34924. ret = wc_PKCS7_DecodeEnvelopedData(envPkcs7, atr, atrSz,
  34925. key, keySz);
  34926. if (envPkcs7->contentOID != FIRMWARE_PKG_DATA) {
  34927. /* the contentOID should have been set to the inner
  34928. * FIRMWARE_PKG_DATA content */
  34929. ret = BAD_STATE_E;
  34930. }
  34931. }
  34932. wc_PKCS7_Free(envPkcs7);
  34933. }
  34934. }
  34935. return ret;
  34936. }
  34937. /* create a KEKRI enveloped data
  34938. * return size on success */
  34939. static int envelopedData_encrypt(byte* in, word32 inSz, byte* out,
  34940. word32 outSz)
  34941. {
  34942. int ret;
  34943. PKCS7* pkcs7;
  34944. WOLFSSL_SMALL_STACK_STATIC const byte keyId[] = { 0x00 };
  34945. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  34946. if (pkcs7 == NULL)
  34947. return -12330;
  34948. pkcs7->content = in;
  34949. pkcs7->contentSz = inSz;
  34950. pkcs7->contentOID = FIRMWARE_PKG_DATA;
  34951. pkcs7->encryptOID = AES256CBCb;
  34952. pkcs7->ukm = NULL;
  34953. pkcs7->ukmSz = 0;
  34954. /* add recipient (KEKRI type) */
  34955. ret = wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP, (byte*)p7DefKey,
  34956. sizeof(p7DefKey), (byte*)keyId,
  34957. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0);
  34958. if (ret < 0) {
  34959. printf("wc_PKCS7_AddRecipient_KEKRI() failed, ret = %d\n", ret);
  34960. wc_PKCS7_Free(pkcs7);
  34961. return -12331;
  34962. }
  34963. /* encode envelopedData, returns size */
  34964. ret = wc_PKCS7_EncodeEnvelopedData(pkcs7, out, outSz);
  34965. if (ret <= 0) {
  34966. printf("wc_PKCS7_EncodeEnvelopedData() failed, ret = %d\n", ret);
  34967. wc_PKCS7_Free(pkcs7);
  34968. return -12332;
  34969. }
  34970. wc_PKCS7_Free(pkcs7);
  34971. return ret;
  34972. }
  34973. /*
  34974. * keyHint is the KeyID to be set in the fwDecryptKeyID attribute
  34975. * returns size of buffer output on success
  34976. */
  34977. static int generateBundle(byte* out, word32 *outSz, const byte* encryptKey,
  34978. word32 encryptKeySz, byte keyHint, byte* cert, word32 certSz,
  34979. byte* key, word32 keySz)
  34980. {
  34981. int ret, attribNum = 1;
  34982. PKCS7* pkcs7;
  34983. /* KEY ID
  34984. * fwDecryptKeyID OID 1.2.840.113549.1.9.16.2.37
  34985. */
  34986. const unsigned char fwDecryptKeyID[] = {
  34987. 0x06, 0x0B,
  34988. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  34989. 0x01, 0x09, 0x10, 0x02, 0x25
  34990. };
  34991. /* fwWrappedFirmwareKey 1.2.840.113529.1.9.16.1.16 */
  34992. const unsigned char fwWrappedFirmwareKey[] = {
  34993. 0x06, 0x0B, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  34994. 0x01, 0x09, 0x10, 0x02, 0x27
  34995. };
  34996. byte keyID[] = { 0x04, 0x01, 0x00 };
  34997. byte env[256];
  34998. char data[] = "Test of wolfSSL PKCS7 decrypt callback";
  34999. PKCS7Attrib attribs[] =
  35000. {
  35001. { fwDecryptKeyID, sizeof(fwDecryptKeyID), keyID, sizeof(keyID) },
  35002. { fwWrappedFirmwareKey, sizeof(fwWrappedFirmwareKey), env, 0 }
  35003. };
  35004. keyID[2] = keyHint;
  35005. /* If using keyHint 0 then create a bundle with fwWrappedFirmwareKey */
  35006. if (keyHint == 0) {
  35007. ret = envelopedData_encrypt((byte*)p7DefKey, sizeof(p7DefKey), env,
  35008. sizeof(env));
  35009. if (ret <= 0) {
  35010. return ret;
  35011. }
  35012. attribs[1].valueSz = ret;
  35013. attribNum++;
  35014. }
  35015. /* init PKCS7 */
  35016. pkcs7 = wc_PKCS7_New(NULL, INVALID_DEVID);
  35017. if (pkcs7 == NULL)
  35018. return -12340;
  35019. ret = wc_PKCS7_InitWithCert(pkcs7, cert, certSz);
  35020. if (ret != 0) {
  35021. printf("ERROR: wc_PKCS7_InitWithCert() failed, ret = %d\n", ret);
  35022. wc_PKCS7_Free(pkcs7);
  35023. return -12341;
  35024. }
  35025. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  35026. if (ret != 0) {
  35027. wc_PKCS7_Free(pkcs7);
  35028. return -12342;
  35029. }
  35030. /* encode Signed Encrypted FirmwarePkgData */
  35031. if (encryptKeySz == 16) {
  35032. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  35033. encryptKeySz, key, keySz, AES128CBCb, RSAk, SHA256h,
  35034. (byte*)data, sizeof(data), NULL, 0,
  35035. attribs, attribNum, out, *outSz);
  35036. }
  35037. else {
  35038. ret = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7, (byte*)encryptKey,
  35039. encryptKeySz, key, keySz, AES256CBCb, RSAk, SHA256h,
  35040. (byte*)data, sizeof(data), NULL, 0,
  35041. attribs, attribNum, out, *outSz);
  35042. }
  35043. if (ret <= 0) {
  35044. printf("ERROR: wc_PKCS7_EncodeSignedEncryptedFPD() failed, "
  35045. "ret = %d\n", ret);
  35046. wc_PKCS7_Free(pkcs7);
  35047. return -12343;
  35048. } else {
  35049. *outSz = ret;
  35050. }
  35051. wc_PKCS7_Free(pkcs7);
  35052. return ret;
  35053. }
  35054. /* test verification and decryption of PKCS7 bundle
  35055. * return 0 on success
  35056. */
  35057. static int verifyBundle(byte* derBuf, word32 derSz, int keyHint)
  35058. {
  35059. int ret = 0;
  35060. int usrCtx = 1; /* test value to pass as user context to callback */
  35061. PKCS7* pkcs7 = NULL;
  35062. byte* sid = NULL;
  35063. word32 sidSz;
  35064. byte key[256];
  35065. word32 keySz = sizeof(key);
  35066. byte *decoded = NULL;
  35067. int decodedSz = FOURK_BUF/2;
  35068. WOLFSSL_SMALL_STACK_STATIC const byte expectedSid[] = {
  35069. #ifdef USE_CERT_BUFFERS_1024
  35070. 0x81, 0x69, 0x0f, 0xf8, 0xdf, 0xdd, 0xcf, 0x34,
  35071. 0x29, 0xd5, 0x67, 0x75, 0x71, 0x85, 0xc7, 0x75,
  35072. 0x10, 0x69, 0x59, 0xec,
  35073. #else
  35074. 0x33, 0xD8, 0x45, 0x66, 0xD7, 0x68, 0x87, 0x18,
  35075. 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26,
  35076. 0xD7, 0x85, 0x65, 0xC0
  35077. #endif
  35078. };
  35079. decoded = (byte *)XMALLOC(decodedSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35080. if (decoded == NULL) {
  35081. ret = MEMORY_E;
  35082. goto out;
  35083. }
  35084. pkcs7 = wc_PKCS7_New(HEAP_HINT, INVALID_DEVID);
  35085. if (pkcs7 == NULL) {
  35086. ret = MEMORY_E;
  35087. goto out;
  35088. }
  35089. /* Test verify */
  35090. ret = wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID);
  35091. if (ret != 0)
  35092. goto out;
  35093. ret = wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  35094. if (ret != 0)
  35095. goto out;
  35096. ret = wc_PKCS7_VerifySignedData(pkcs7, derBuf, derSz);
  35097. if (ret != 0)
  35098. goto out;
  35099. /* Get size of SID and print it out */
  35100. ret = wc_PKCS7_GetSignerSID(pkcs7, NULL, &sidSz);
  35101. if (ret != LENGTH_ONLY_E)
  35102. goto out;
  35103. sid = (byte*)XMALLOC(sidSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35104. if (sid == NULL) {
  35105. ret = MEMORY_E;
  35106. goto out;
  35107. }
  35108. ret = wc_PKCS7_GetSignerSID(pkcs7, sid, &sidSz);
  35109. if (ret != 0)
  35110. goto out;
  35111. ret = XMEMCMP(sid, expectedSid, sidSz);
  35112. if (ret != 0) {
  35113. ret = PKCS7_NO_SIGNER_E; /* close enough */
  35114. goto out;
  35115. }
  35116. /* get expected fwWrappedFirmwareKey */
  35117. if (keyHint == 0) {
  35118. ret = getFirmwareKey(pkcs7, key, keySz);
  35119. if (ret < 0)
  35120. goto out;
  35121. pkcs7->encryptionKey = key;
  35122. pkcs7->encryptionKeySz = ret;
  35123. }
  35124. else {
  35125. decodedSz = PKCS7_BUF_SIZE;
  35126. ret = wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc);
  35127. if (ret != 0)
  35128. goto out;
  35129. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)&usrCtx);
  35130. if (ret != 0)
  35131. goto out;
  35132. }
  35133. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  35134. pkcs7->contentSz, decoded, decodedSz);
  35135. if (decodedSz < 0) {
  35136. ret = decodedSz;
  35137. goto out;
  35138. }
  35139. ret = 0;
  35140. out:
  35141. if (decoded)
  35142. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35143. if (pkcs7)
  35144. wc_PKCS7_Free(pkcs7);
  35145. if (sid)
  35146. XFREE(sid, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35147. return ret;
  35148. }
  35149. WOLFSSL_TEST_SUBROUTINE int pkcs7callback_test(byte* cert, word32 certSz, byte* key, word32 keySz)
  35150. {
  35151. int ret = 0;
  35152. word32 derSz;
  35153. byte *derBuf = (byte *)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35154. if (! derBuf)
  35155. ERROR_OUT(-12360, out);
  35156. /* Doing default generation and verify */
  35157. derSz = FOURK_BUF;
  35158. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 0, cert,
  35159. certSz, key, keySz);
  35160. if (ret <= 0) {
  35161. ERROR_OUT(-12361, out);
  35162. }
  35163. ret = verifyBundle(derBuf, derSz, 0);
  35164. if (ret != 0) {
  35165. ERROR_OUT(-12362, out);
  35166. }
  35167. /* test choosing other key with keyID */
  35168. derSz = FOURK_BUF;
  35169. ret = generateBundle(derBuf, &derSz, p7AltKey, sizeof(p7AltKey), 1,
  35170. cert, certSz, key, keySz);
  35171. if (ret <= 0) {
  35172. ERROR_OUT(-12363, out);
  35173. }
  35174. ret = verifyBundle(derBuf, derSz, 1);
  35175. if (ret != 0) {
  35176. ERROR_OUT(-12364, out);
  35177. }
  35178. /* test fail case with wrong keyID */
  35179. derSz = FOURK_BUF;
  35180. ret = generateBundle(derBuf, &derSz, p7DefKey, sizeof(p7DefKey), 1,
  35181. cert, certSz, key, keySz);
  35182. if (ret <= 0) {
  35183. ERROR_OUT(-12365, out);
  35184. }
  35185. ret = verifyBundle(derBuf, derSz, 1);
  35186. if (ret == 0) {
  35187. ERROR_OUT(-12366, out);
  35188. }
  35189. ret = 0;
  35190. out:
  35191. if (derBuf)
  35192. XFREE(derBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35193. return ret;
  35194. }
  35195. #endif /* !NO_AES && HAVE_AES_CBC */
  35196. #ifndef NO_PKCS7_ENCRYPTED_DATA
  35197. typedef struct {
  35198. const byte* content;
  35199. word32 contentSz;
  35200. int contentOID;
  35201. int encryptOID;
  35202. byte* encryptionKey;
  35203. word32 encryptionKeySz;
  35204. PKCS7Attrib* attribs;
  35205. word32 attribsSz;
  35206. const char* outFileName;
  35207. } pkcs7EncryptedVector;
  35208. WOLFSSL_TEST_SUBROUTINE int pkcs7encrypted_test(void)
  35209. {
  35210. int ret = 0;
  35211. int i, testSz;
  35212. int encryptedSz, decodedSz, attribIdx;
  35213. PKCS7* pkcs7;
  35214. byte *encrypted;
  35215. byte *decoded;
  35216. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  35217. XFILE pkcs7File;
  35218. #endif
  35219. PKCS7Attrib* expectedAttrib;
  35220. PKCS7DecodedAttrib* decodedAttrib;
  35221. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  35222. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  35223. 0x72,0x6c,0x64
  35224. };
  35225. #ifndef NO_DES3
  35226. byte desKey[] = {
  35227. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  35228. };
  35229. byte des3Key[] = {
  35230. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  35231. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  35232. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  35233. };
  35234. #endif
  35235. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  35236. #ifdef WOLFSSL_AES_128
  35237. byte aes128Key[] = {
  35238. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  35239. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  35240. };
  35241. #endif
  35242. #ifdef WOLFSSL_AES_192
  35243. byte aes192Key[] = {
  35244. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  35245. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  35246. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  35247. };
  35248. #endif
  35249. #ifdef WOLFSSL_AES_256
  35250. byte aes256Key[] = {
  35251. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  35252. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  35253. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  35254. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  35255. };
  35256. #endif
  35257. #ifdef WOLFSSL_AES_256
  35258. /* Attribute example from RFC 4134, Section 7.2
  35259. * OID = 1.2.5555
  35260. * OCTET STRING = 'This is a test General ASN Attribute, number 1.' */
  35261. static byte genAttrOid[] = { 0x06, 0x03, 0x2a, 0xab, 0x33 };
  35262. static byte genAttr[] = { 0x04, 47,
  35263. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  35264. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  35265. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  35266. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  35267. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  35268. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x31, 0x2e };
  35269. static byte genAttrOid2[] = { 0x06, 0x03, 0x2a, 0xab, 0x34 };
  35270. static byte genAttr2[] = { 0x04, 47,
  35271. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  35272. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  35273. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  35274. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  35275. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  35276. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x32, 0x2e };
  35277. PKCS7Attrib attribs[] =
  35278. {
  35279. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) }
  35280. };
  35281. PKCS7Attrib multiAttribs[] =
  35282. {
  35283. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) },
  35284. { genAttrOid2, sizeof(genAttrOid2), genAttr2, sizeof(genAttr2) }
  35285. };
  35286. #endif
  35287. #endif /* NO_AES */
  35288. const pkcs7EncryptedVector testVectors[] =
  35289. {
  35290. #ifndef NO_DES3
  35291. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key),
  35292. NULL, 0, "pkcs7encryptedDataDES3.der"},
  35293. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey),
  35294. NULL, 0, "pkcs7encryptedDataDES.der"},
  35295. #endif /* NO_DES3 */
  35296. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  35297. #ifdef WOLFSSL_AES_128
  35298. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  35299. sizeof(aes128Key), NULL, 0, "pkcs7encryptedDataAES128CBC.der"},
  35300. #endif
  35301. #ifdef WOLFSSL_AES_192
  35302. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  35303. sizeof(aes192Key), NULL, 0, "pkcs7encryptedDataAES192CBC.der"},
  35304. #endif
  35305. #ifdef WOLFSSL_AES_256
  35306. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  35307. sizeof(aes256Key), NULL, 0, "pkcs7encryptedDataAES256CBC.der"},
  35308. /* test with optional unprotected attributes */
  35309. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  35310. sizeof(aes256Key), attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35311. "pkcs7encryptedDataAES256CBC_attribs.der"},
  35312. /* test with multiple optional unprotected attributes */
  35313. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  35314. sizeof(aes256Key), multiAttribs,
  35315. (sizeof(multiAttribs)/sizeof(PKCS7Attrib)),
  35316. "pkcs7encryptedDataAES256CBC_multi_attribs.der"},
  35317. /* test with contentType set to FirmwarePkgData */
  35318. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA, AES256CBCb, aes256Key,
  35319. sizeof(aes256Key), NULL, 0,
  35320. "pkcs7encryptedDataAES256CBC_firmwarePkgData.der"},
  35321. #endif
  35322. #endif /* !NO_AES && HAVE_AES_CBC */
  35323. };
  35324. encrypted = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35325. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35326. if ((! encrypted) || (! decoded)) {
  35327. ERROR_OUT(MEMORY_E, out);
  35328. }
  35329. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  35330. for (i = 0; i < testSz; i++) {
  35331. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  35332. if (pkcs7 == NULL) {
  35333. ERROR_OUT(-12400, out);
  35334. }
  35335. pkcs7->content = (byte*)testVectors[i].content;
  35336. pkcs7->contentSz = testVectors[i].contentSz;
  35337. pkcs7->contentOID = testVectors[i].contentOID;
  35338. pkcs7->encryptOID = testVectors[i].encryptOID;
  35339. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  35340. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  35341. pkcs7->unprotectedAttribs = testVectors[i].attribs;
  35342. pkcs7->unprotectedAttribsSz = testVectors[i].attribsSz;
  35343. /* encode encryptedData */
  35344. encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  35345. PKCS7_BUF_SIZE);
  35346. if (encryptedSz <= 0) {
  35347. wc_PKCS7_Free(pkcs7);
  35348. ERROR_OUT(-12401, out);
  35349. }
  35350. /* decode encryptedData */
  35351. #ifndef NO_PKCS7_STREAM
  35352. { /* test reading byte by byte */
  35353. int z;
  35354. for (z = 0; z < encryptedSz; z++) {
  35355. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted + z, 1,
  35356. decoded, PKCS7_BUF_SIZE);
  35357. if (decodedSz <= 0 && decodedSz != WC_PKCS7_WANT_READ_E) {
  35358. printf("unexpected error %d\n", decodedSz);
  35359. ERROR_OUT(-12402, out);
  35360. }
  35361. }
  35362. /* test decode result */
  35363. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  35364. printf("stream read failed\n");
  35365. wc_PKCS7_Free(pkcs7);
  35366. ERROR_OUT(-12403, out);
  35367. }
  35368. }
  35369. #endif
  35370. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  35371. decoded, PKCS7_BUF_SIZE);
  35372. if (decodedSz <= 0){
  35373. wc_PKCS7_Free(pkcs7);
  35374. ERROR_OUT(-12404, out);
  35375. }
  35376. /* test decode result */
  35377. if (XMEMCMP(decoded, data, sizeof(data)) != 0) {
  35378. wc_PKCS7_Free(pkcs7);
  35379. ERROR_OUT(-12405, out);
  35380. }
  35381. /* verify decoded unprotected attributes */
  35382. if (pkcs7->decodedAttrib != NULL) {
  35383. decodedAttrib = pkcs7->decodedAttrib;
  35384. attribIdx = 1;
  35385. while (decodedAttrib != NULL) {
  35386. /* expected attribute, stored list is reversed */
  35387. expectedAttrib = &(pkcs7->unprotectedAttribs
  35388. [pkcs7->unprotectedAttribsSz - attribIdx]);
  35389. /* verify oid */
  35390. if (XMEMCMP(decodedAttrib->oid, expectedAttrib->oid,
  35391. decodedAttrib->oidSz) != 0) {
  35392. wc_PKCS7_Free(pkcs7);
  35393. ERROR_OUT(-12406, out);
  35394. }
  35395. /* verify value */
  35396. if (XMEMCMP(decodedAttrib->value, expectedAttrib->value,
  35397. decodedAttrib->valueSz) != 0) {
  35398. wc_PKCS7_Free(pkcs7);
  35399. ERROR_OUT(-12407, out);
  35400. }
  35401. decodedAttrib = decodedAttrib->next;
  35402. attribIdx++;
  35403. }
  35404. }
  35405. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  35406. /* output pkcs7 envelopedData for external testing */
  35407. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  35408. if (!pkcs7File) {
  35409. wc_PKCS7_Free(pkcs7);
  35410. ERROR_OUT(-12408, out);
  35411. }
  35412. ret = (int)XFWRITE(encrypted, encryptedSz, 1, pkcs7File);
  35413. XFCLOSE(pkcs7File);
  35414. if (ret > 0)
  35415. ret = 0;
  35416. #endif
  35417. wc_PKCS7_Free(pkcs7);
  35418. }
  35419. out:
  35420. if (encrypted)
  35421. XFREE(encrypted, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35422. if (decoded)
  35423. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35424. return ret;
  35425. }
  35426. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  35427. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  35428. typedef struct {
  35429. const byte* content;
  35430. word32 contentSz;
  35431. int contentOID;
  35432. const char* outFileName;
  35433. } pkcs7CompressedVector;
  35434. WOLFSSL_TEST_SUBROUTINE int pkcs7compressed_test(void)
  35435. {
  35436. int ret = 0;
  35437. int i, testSz;
  35438. int compressedSz, decodedSz;
  35439. PKCS7* pkcs7;
  35440. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  35441. byte *compressed;
  35442. byte *decoded;
  35443. #else
  35444. byte compressed[PKCS7_BUF_SIZE];
  35445. byte decoded[PKCS7_BUF_SIZE];
  35446. #endif
  35447. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  35448. XFILE pkcs7File;
  35449. #endif
  35450. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  35451. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  35452. 0x72,0x6c,0x64
  35453. };
  35454. const pkcs7CompressedVector testVectors[] =
  35455. {
  35456. {data, (word32)sizeof(data), DATA,
  35457. "pkcs7compressedData_data_zlib.der"},
  35458. {data, (word32)sizeof(data), FIRMWARE_PKG_DATA,
  35459. "pkcs7compressedData_firmwarePkgData_zlib.der"},
  35460. };
  35461. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  35462. compressed = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35463. decoded = (byte *)XMALLOC(PKCS7_BUF_SIZE, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35464. if ((! compressed) || (! decoded)) {
  35465. ERROR_OUT(MEMORY_E, out);
  35466. }
  35467. #endif
  35468. testSz = sizeof(testVectors) / sizeof(pkcs7CompressedVector);
  35469. for (i = 0; i < testSz; i++) {
  35470. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  35471. if (pkcs7 == NULL) {
  35472. ERROR_OUT(-12500, out);
  35473. }
  35474. pkcs7->content = (byte*)testVectors[i].content;
  35475. pkcs7->contentSz = testVectors[i].contentSz;
  35476. pkcs7->contentOID = testVectors[i].contentOID;
  35477. /* encode compressedData */
  35478. compressedSz = wc_PKCS7_EncodeCompressedData(pkcs7, compressed,
  35479. PKCS7_BUF_SIZE);
  35480. if (compressedSz <= 0) {
  35481. wc_PKCS7_Free(pkcs7);
  35482. ERROR_OUT(-12501, out);
  35483. }
  35484. /* decode compressedData */
  35485. decodedSz = wc_PKCS7_DecodeCompressedData(pkcs7, compressed,
  35486. compressedSz, decoded,
  35487. PKCS7_BUF_SIZE);
  35488. if (decodedSz <= 0){
  35489. wc_PKCS7_Free(pkcs7);
  35490. ERROR_OUT(-12502, out);
  35491. }
  35492. /* test decode result */
  35493. if (XMEMCMP(decoded, testVectors[i].content,
  35494. testVectors[i].contentSz) != 0) {
  35495. wc_PKCS7_Free(pkcs7);
  35496. ERROR_OUT(-12503, out);
  35497. }
  35498. /* make sure content type is the same */
  35499. if (testVectors[i].contentOID != pkcs7->contentOID) {
  35500. ERROR_OUT(-12504, out);
  35501. }
  35502. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  35503. /* output pkcs7 compressedData for external testing */
  35504. pkcs7File = XFOPEN(testVectors[i].outFileName, "wb");
  35505. if (!pkcs7File) {
  35506. wc_PKCS7_Free(pkcs7);
  35507. ERROR_OUT(-12505, out);
  35508. }
  35509. ret = (int)XFWRITE(compressed, compressedSz, 1, pkcs7File);
  35510. XFCLOSE(pkcs7File);
  35511. if (ret > 0)
  35512. ret = 0;
  35513. #endif
  35514. wc_PKCS7_Free(pkcs7);
  35515. }
  35516. out:
  35517. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  35518. if (compressed)
  35519. XFREE(compressed, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35520. if (decoded)
  35521. XFREE(decoded, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35522. #endif
  35523. return ret;
  35524. } /* pkcs7compressed_test() */
  35525. #undef PKCS7_BUF_SIZE
  35526. #endif /* HAVE_LIBZ */
  35527. typedef struct {
  35528. const byte* content;
  35529. word32 contentSz;
  35530. int hashOID;
  35531. int signOID;
  35532. byte* privateKey;
  35533. word32 privateKeySz;
  35534. byte* cert;
  35535. size_t certSz;
  35536. byte* caCert;
  35537. size_t caCertSz;
  35538. PKCS7Attrib* signedAttribs;
  35539. word32 signedAttribsSz;
  35540. const char* outFileName;
  35541. int contentOID;
  35542. byte* contentType;
  35543. word32 contentTypeSz;
  35544. int sidType;
  35545. int encryptOID; /* for single-shot encrypt alg OID */
  35546. int encCompFlag; /* for single-shot. 1 = enc, 2 = comp, 3 = both*/
  35547. byte* encryptKey; /* for single-shot, encryptedData */
  35548. word32 encryptKeySz; /* for single-shot, encryptedData */
  35549. PKCS7Attrib* unprotectedAttribs; /* for single-shot, encryptedData */
  35550. word32 unprotectedAttribsSz; /* for single-shot, encryptedData */
  35551. word16 detachedSignature; /* generate detached signature (0:1) */
  35552. } pkcs7SignedVector;
  35553. static int pkcs7signed_run_vectors(
  35554. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  35555. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  35556. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  35557. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  35558. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  35559. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  35560. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  35561. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  35562. {
  35563. int ret, testSz = 0, i;
  35564. int encodedSz;
  35565. byte* out = NULL;
  35566. word32 outSz;
  35567. WC_RNG rng;
  35568. PKCS7* pkcs7 = NULL;
  35569. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  35570. XFILE file;
  35571. #endif
  35572. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  35573. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  35574. 0x72,0x6c,0x64
  35575. };
  35576. static byte transIdOid[] =
  35577. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  35578. 0x09, 0x07 };
  35579. static byte messageTypeOid[] =
  35580. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  35581. 0x09, 0x02 };
  35582. static byte senderNonceOid[] =
  35583. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  35584. 0x09, 0x05 };
  35585. #ifndef NO_SHA
  35586. static byte transId[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  35587. #else
  35588. static byte transId[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  35589. #endif
  35590. static byte messageType[] = { 0x13, 2, '1', '9' };
  35591. static byte senderNonce[PKCS7_NONCE_SZ + 2];
  35592. static PKCS7Attrib attribs[] =
  35593. {
  35594. { transIdOid, sizeof(transIdOid), transId,
  35595. sizeof(transId) - 1 }, /* take off the null */
  35596. { messageTypeOid, sizeof(messageTypeOid), messageType,
  35597. sizeof(messageType) },
  35598. { senderNonceOid, sizeof(senderNonceOid), senderNonce,
  35599. sizeof(senderNonce) }
  35600. };
  35601. /* for testing custom contentType, FirmwarePkgData */
  35602. static byte customContentType[] = { 0x06, 0x0B, 0x2A, 0x86,
  35603. 0x48, 0x86, 0xF7, 0x0D,
  35604. 0x01, 0x09, 0x10, 0x01, 0x10 };
  35605. #define MAX_TESTVECTORS_LEN 20
  35606. #define ADD_PKCS7SIGNEDVECTOR(...) { \
  35607. pkcs7SignedVector _this_vector = { __VA_ARGS__ }; \
  35608. if (testSz == MAX_TESTVECTORS_LEN) { \
  35609. ret = -12534; \
  35610. goto out; \
  35611. } \
  35612. XMEMCPY(&testVectors[testSz++], &_this_vector, sizeof _this_vector);\
  35613. }
  35614. pkcs7SignedVector *testVectors = NULL;
  35615. XMEMSET(&rng, 0, sizeof(rng));
  35616. testVectors = (pkcs7SignedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  35617. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35618. if (testVectors == NULL) {
  35619. ret = -12567;
  35620. goto out;
  35621. }
  35622. {
  35623. #ifndef NO_RSA
  35624. #ifndef NO_SHA
  35625. /* RSA with SHA */
  35626. ADD_PKCS7SIGNEDVECTOR(
  35627. data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  35628. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  35629. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35630. "pkcs7signedData_RSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0, NULL,
  35631. 0, 0);
  35632. /* RSA with SHA, no signed attributes */
  35633. ADD_PKCS7SIGNEDVECTOR(
  35634. data, (word32)sizeof(data), SHAh, RSAk, rsaClientPrivKeyBuf,
  35635. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz,
  35636. NULL, 0, NULL, 0,
  35637. "pkcs7signedData_RSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  35638. NULL, 0, 0);
  35639. #endif
  35640. #ifdef WOLFSSL_SHA224
  35641. /* RSA with SHA224 */
  35642. ADD_PKCS7SIGNEDVECTOR(
  35643. data, (word32)sizeof(data), SHA224h, RSAk, rsaClientPrivKeyBuf,
  35644. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  35645. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35646. "pkcs7signedData_RSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  35647. NULL, 0, 0);
  35648. #endif
  35649. #ifndef NO_SHA256
  35650. /* RSA with SHA256 */
  35651. ADD_PKCS7SIGNEDVECTOR(
  35652. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  35653. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  35654. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35655. "pkcs7signedData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  35656. NULL, 0, 0);
  35657. /* RSA with SHA256, detached signature */
  35658. ADD_PKCS7SIGNEDVECTOR(
  35659. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  35660. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  35661. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35662. "pkcs7signedData_RSA_SHA256_detachedSig.der", 0, NULL, 0, 0, 0, 0,
  35663. NULL, 0, NULL, 0, 1);
  35664. /* RSA with SHA256 and SubjectKeyIdentifier in SignerIdentifier */
  35665. ADD_PKCS7SIGNEDVECTOR(
  35666. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  35667. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  35668. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35669. "pkcs7signedData_RSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  35670. NULL, 0, NULL, 0, 0);
  35671. /* RSA with SHA256 and custom contentType */
  35672. ADD_PKCS7SIGNEDVECTOR(
  35673. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  35674. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  35675. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35676. "pkcs7signedData_RSA_SHA256_custom_contentType.der", 0,
  35677. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  35678. NULL, 0, 0);
  35679. /* RSA with SHA256 and FirmwarePkgData contentType */
  35680. ADD_PKCS7SIGNEDVECTOR(
  35681. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  35682. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  35683. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35684. "pkcs7signedData_RSA_SHA256_firmwarePkgData.der",
  35685. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  35686. /* RSA with SHA256 using server cert and ca cert */
  35687. ADD_PKCS7SIGNEDVECTOR(
  35688. data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  35689. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  35690. rsaCaCertBuf, rsaCaCertBufSz,
  35691. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35692. "pkcs7signedData_RSA_SHA256_with_ca_cert.der", 0, NULL, 0, 0, 0, 0,
  35693. NULL, 0, NULL, 0, 0);
  35694. #endif
  35695. #if defined(WOLFSSL_SHA384)
  35696. /* RSA with SHA384 */
  35697. ADD_PKCS7SIGNEDVECTOR(
  35698. data, (word32)sizeof(data), SHA384h, RSAk, rsaClientPrivKeyBuf,
  35699. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  35700. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35701. "pkcs7signedData_RSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  35702. NULL, 0, 0);
  35703. #endif
  35704. #if defined(WOLFSSL_SHA512)
  35705. /* RSA with SHA512 */
  35706. ADD_PKCS7SIGNEDVECTOR(
  35707. data, (word32)sizeof(data), SHA512h, RSAk, rsaClientPrivKeyBuf,
  35708. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  35709. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35710. "pkcs7signedData_RSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  35711. NULL, 0, 0);
  35712. #endif
  35713. #endif /* NO_RSA */
  35714. #ifdef HAVE_ECC
  35715. #ifndef NO_SHA
  35716. /* ECDSA with SHA */
  35717. ADD_PKCS7SIGNEDVECTOR(
  35718. data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  35719. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  35720. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35721. "pkcs7signedData_ECDSA_SHA.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  35722. NULL, 0, 0);
  35723. /* ECDSA with SHA, no signed attributes */
  35724. ADD_PKCS7SIGNEDVECTOR(
  35725. data, (word32)sizeof(data), SHAh, ECDSAk, eccClientPrivKeyBuf,
  35726. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz,
  35727. NULL, 0, NULL, 0,
  35728. "pkcs7signedData_ECDSA_SHA_noattr.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  35729. NULL, 0, 0);
  35730. #endif
  35731. #ifdef WOLFSSL_SHA224
  35732. /* ECDSA with SHA224 */
  35733. ADD_PKCS7SIGNEDVECTOR(
  35734. data, (word32)sizeof(data), SHA224h, ECDSAk, eccClientPrivKeyBuf,
  35735. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  35736. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35737. "pkcs7signedData_ECDSA_SHA224.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  35738. NULL, 0, 0);
  35739. #endif
  35740. #ifndef NO_SHA256
  35741. /* ECDSA with SHA256 */
  35742. ADD_PKCS7SIGNEDVECTOR(
  35743. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  35744. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  35745. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35746. "pkcs7signedData_ECDSA_SHA256.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  35747. NULL, 0, 0);
  35748. /* ECDSA with SHA256 and SubjectKeyIdentifier in SigherIdentifier */
  35749. ADD_PKCS7SIGNEDVECTOR(
  35750. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  35751. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  35752. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35753. "pkcs7signedData_ECDSA_SHA256_SKID.der", 0, NULL, 0, CMS_SKID, 0, 0,
  35754. NULL, 0, NULL, 0, 0);
  35755. /* ECDSA with SHA256 and custom contentType */
  35756. ADD_PKCS7SIGNEDVECTOR(
  35757. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  35758. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  35759. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35760. "pkcs7signedData_ECDSA_SHA256_custom_contentType.der", 0,
  35761. customContentType, sizeof(customContentType), 0, 0, 0, NULL, 0,
  35762. NULL, 0, 0);
  35763. /* ECDSA with SHA256 and FirmwarePkgData contentType */
  35764. ADD_PKCS7SIGNEDVECTOR(
  35765. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  35766. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  35767. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35768. "pkcs7signedData_ECDSA_SHA256_firmwarePkgData.der",
  35769. FIRMWARE_PKG_DATA, NULL, 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  35770. #endif
  35771. #ifdef WOLFSSL_SHA384
  35772. /* ECDSA with SHA384 */
  35773. ADD_PKCS7SIGNEDVECTOR(
  35774. data, (word32)sizeof(data), SHA384h, ECDSAk, eccClientPrivKeyBuf,
  35775. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  35776. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35777. "pkcs7signedData_ECDSA_SHA384.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  35778. NULL, 0, 0);
  35779. #endif
  35780. #ifdef WOLFSSL_SHA512
  35781. /* ECDSA with SHA512 */
  35782. ADD_PKCS7SIGNEDVECTOR(
  35783. data, (word32)sizeof(data), SHA512h, ECDSAk, eccClientPrivKeyBuf,
  35784. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  35785. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  35786. "pkcs7signedData_ECDSA_SHA512.der", 0, NULL, 0, 0, 0, 0, NULL, 0,
  35787. NULL, 0, 0);
  35788. #endif
  35789. #endif /* HAVE_ECC */
  35790. };
  35791. #undef MAX_TESTVECTORS_LEN
  35792. #undef ADD_PKCS7SIGNEDVECTOR
  35793. outSz = FOURK_BUF;
  35794. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35795. if (out == NULL)
  35796. ERROR_OUT(-12510, out);
  35797. XMEMSET(out, 0, outSz);
  35798. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  35799. if (ret < 0)
  35800. ERROR_OUT(-12511, out);
  35801. #ifndef HAVE_FIPS
  35802. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  35803. #else
  35804. ret = wc_InitRng(&rng);
  35805. #endif
  35806. if (ret != 0)
  35807. ERROR_OUT(-12512, out);
  35808. for (i = 0; i < testSz; i++) {
  35809. if (pkcs7)
  35810. wc_PKCS7_Free(pkcs7);
  35811. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  35812. if (pkcs7 == NULL)
  35813. ERROR_OUT(-12513, out);
  35814. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  35815. (word32)testVectors[i].certSz);
  35816. if (ret != 0)
  35817. ERROR_OUT(-12514, out);
  35818. /* load CA certificate, if present */
  35819. if (testVectors[i].caCert != NULL) {
  35820. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  35821. (word32)testVectors[i].caCertSz);
  35822. if (ret != 0)
  35823. ERROR_OUT(-12515, out);
  35824. }
  35825. pkcs7->rng = &rng;
  35826. pkcs7->content = (byte*)testVectors[i].content;
  35827. pkcs7->contentSz = testVectors[i].contentSz;
  35828. pkcs7->contentOID = testVectors[i].contentOID;
  35829. pkcs7->hashOID = testVectors[i].hashOID;
  35830. pkcs7->encryptOID = testVectors[i].signOID;
  35831. pkcs7->privateKey = testVectors[i].privateKey;
  35832. pkcs7->privateKeySz = testVectors[i].privateKeySz;
  35833. pkcs7->signedAttribs = testVectors[i].signedAttribs;
  35834. pkcs7->signedAttribsSz = testVectors[i].signedAttribsSz;
  35835. /* optional custom contentType, default is DATA,
  35836. overrides contentOID if set */
  35837. if (testVectors[i].contentType != NULL) {
  35838. ret = wc_PKCS7_SetContentType(pkcs7, testVectors[i].contentType,
  35839. testVectors[i].contentTypeSz);
  35840. if (ret != 0)
  35841. ERROR_OUT(-12516, out);
  35842. }
  35843. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  35844. default is IssuerAndSerialNumber */
  35845. if (testVectors[i].sidType == CMS_SKID) {
  35846. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  35847. if (ret != 0)
  35848. ERROR_OUT(-12517, out);
  35849. }
  35850. /* generate senderNonce */
  35851. {
  35852. senderNonce[0] = 0x04;
  35853. senderNonce[1] = PKCS7_NONCE_SZ;
  35854. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  35855. if (ret != 0)
  35856. ERROR_OUT(-12518, out);
  35857. }
  35858. /* generate transactionID (used with SCEP) */
  35859. {
  35860. #ifndef NO_SHA
  35861. wc_Sha sha;
  35862. byte digest[WC_SHA_DIGEST_SIZE];
  35863. #else
  35864. wc_Sha256 sha;
  35865. byte digest[WC_SHA256_DIGEST_SIZE];
  35866. #endif
  35867. int j,k;
  35868. transId[0] = 0x13;
  35869. transId[1] = sizeof(digest) * 2;
  35870. #ifndef NO_SHA
  35871. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  35872. if (ret != 0)
  35873. ERROR_OUT(-12519, out);
  35874. wc_ShaUpdate(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  35875. wc_ShaFinal(&sha, digest);
  35876. wc_ShaFree(&sha);
  35877. #else
  35878. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  35879. if (ret != 0)
  35880. ERROR_OUT(-12520, out);
  35881. wc_Sha256Update(&sha, pkcs7->publicKey, pkcs7->publicKeySz);
  35882. wc_Sha256Final(&sha, digest);
  35883. wc_Sha256Free(&sha);
  35884. #endif
  35885. for (j = 0, k = 2; j < (int)sizeof(digest); j++, k += 2) {
  35886. #if defined(WOLF_C89)
  35887. XSPRINTF((char*)&transId[k], "%02x", digest[j]);
  35888. #else
  35889. (void)XSNPRINTF((char*)&transId[k], 3, "%02x", digest[j]);
  35890. #endif
  35891. }
  35892. }
  35893. /* enable detached signature generation, if set */
  35894. if (testVectors[i].detachedSignature == 1) {
  35895. ret = wc_PKCS7_SetDetached(pkcs7, 1);
  35896. if (ret != 0)
  35897. ERROR_OUT(-12521, out);
  35898. }
  35899. encodedSz = wc_PKCS7_EncodeSignedData(pkcs7, out, outSz);
  35900. if (encodedSz < 0)
  35901. ERROR_OUT(-12522, out);
  35902. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  35903. /* write PKCS#7 to output file for more testing */
  35904. file = XFOPEN(testVectors[i].outFileName, "wb");
  35905. if (!file) {
  35906. ERROR_OUT(-12523, out);
  35907. }
  35908. ret = (int)XFWRITE(out, 1, encodedSz, file);
  35909. XFCLOSE(file);
  35910. if (ret != (int)encodedSz)
  35911. ERROR_OUT(-12524, out);
  35912. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  35913. wc_PKCS7_Free(pkcs7);
  35914. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  35915. if (pkcs7 == NULL)
  35916. ERROR_OUT(-12525, out);
  35917. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  35918. if (testVectors[i].detachedSignature == 1) {
  35919. /* set content for verifying detached signatures */
  35920. pkcs7->content = (byte*)testVectors[i].content;
  35921. pkcs7->contentSz = testVectors[i].contentSz;
  35922. }
  35923. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  35924. if (ret < 0)
  35925. ERROR_OUT(-12526, out);
  35926. /* verify contentType extracted successfully for custom content types */
  35927. if (testVectors[i].contentTypeSz > 0) {
  35928. if (pkcs7->contentTypeSz != testVectors[i].contentTypeSz) {
  35929. ERROR_OUT(-12527, out);
  35930. } else if (XMEMCMP(pkcs7->contentType, testVectors[i].contentType,
  35931. pkcs7->contentTypeSz) != 0) {
  35932. ERROR_OUT(-12528, out);
  35933. }
  35934. }
  35935. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0)
  35936. ERROR_OUT(-12529, out);
  35937. {
  35938. /* check getting signed attributes */
  35939. #ifndef NO_SHA
  35940. byte buf[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  35941. #else
  35942. byte buf[(WC_SHA256_DIGEST_SIZE + 1) * 2 + 1];
  35943. #endif
  35944. byte* oidPt = transIdOid + 2; /* skip object id tag and size */
  35945. int oidSz = (int)sizeof(transIdOid) - 2;
  35946. int bufSz = 0;
  35947. if (testVectors[i].signedAttribs != NULL &&
  35948. wc_PKCS7_GetAttributeValue(pkcs7, oidPt, oidSz,
  35949. NULL, (word32*)&bufSz) != LENGTH_ONLY_E)
  35950. ERROR_OUT(-12530, out);
  35951. if (bufSz > (int)sizeof(buf))
  35952. ERROR_OUT(-12531, out);
  35953. bufSz = wc_PKCS7_GetAttributeValue(pkcs7, oidPt, oidSz,
  35954. buf, (word32*)&bufSz);
  35955. if ((testVectors[i].signedAttribs != NULL && bufSz < 0) ||
  35956. (testVectors[i].signedAttribs == NULL && bufSz > 0))
  35957. ERROR_OUT(-12532, out);
  35958. }
  35959. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  35960. file = XFOPEN("./pkcs7cert.der", "wb");
  35961. if (!file)
  35962. ERROR_OUT(-12533, out);
  35963. ret = (int)XFWRITE(pkcs7->singleCert, 1, pkcs7->singleCertSz, file);
  35964. XFCLOSE(file);
  35965. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  35966. }
  35967. out:
  35968. if (pkcs7 != NULL)
  35969. wc_PKCS7_Free(pkcs7);
  35970. if (out != NULL)
  35971. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35972. if (testVectors != NULL)
  35973. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  35974. wc_FreeRng(&rng);
  35975. if (ret > 0)
  35976. return 0;
  35977. (void)rsaClientCertBuf;
  35978. (void)rsaClientCertBufSz;
  35979. (void)rsaClientPrivKeyBuf;
  35980. (void)rsaClientPrivKeyBufSz;
  35981. (void)rsaServerCertBuf;
  35982. (void)rsaServerCertBufSz;
  35983. (void)rsaServerPrivKeyBuf;
  35984. (void)rsaServerPrivKeyBufSz;
  35985. (void)rsaCaCertBuf;
  35986. (void)rsaCaCertBufSz;
  35987. (void)rsaCaPrivKeyBuf;
  35988. (void)rsaCaPrivKeyBufSz;
  35989. (void)eccClientCertBuf;
  35990. (void)eccClientCertBufSz;
  35991. (void)eccClientPrivKeyBuf;
  35992. (void)eccClientPrivKeyBufSz;
  35993. return ret;
  35994. }
  35995. static int pkcs7signed_run_SingleShotVectors(
  35996. byte* rsaClientCertBuf, word32 rsaClientCertBufSz,
  35997. byte* rsaClientPrivKeyBuf, word32 rsaClientPrivKeyBufSz,
  35998. byte* rsaServerCertBuf, word32 rsaServerCertBufSz,
  35999. byte* rsaServerPrivKeyBuf, word32 rsaServerPrivKeyBufSz,
  36000. byte* rsaCaCertBuf, word32 rsaCaCertBufSz,
  36001. byte* rsaCaPrivKeyBuf, word32 rsaCaPrivKeyBufSz,
  36002. byte* eccClientCertBuf, word32 eccClientCertBufSz,
  36003. byte* eccClientPrivKeyBuf, word32 eccClientPrivKeyBufSz)
  36004. {
  36005. int ret, testSz = 0, i;
  36006. int encodedSz;
  36007. byte* out = NULL;
  36008. word32 outSz;
  36009. WC_RNG rng;
  36010. PKCS7* pkcs7 = NULL;
  36011. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  36012. XFILE file;
  36013. #endif
  36014. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA) && \
  36015. !defined(NO_PKCS7_ENCRYPTED_DATA)
  36016. byte* encryptedTmp = NULL;
  36017. int encryptedTmpSz;
  36018. #endif
  36019. WOLFSSL_SMALL_STACK_STATIC const byte data[] = { /* Hello World */
  36020. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  36021. 0x72,0x6c,0x64
  36022. };
  36023. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  36024. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  36025. static byte aes256Key[] = {
  36026. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  36027. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  36028. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  36029. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  36030. };
  36031. #endif
  36032. static byte messageTypeOid[] =
  36033. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  36034. 0x09, 0x02 };
  36035. static byte messageType[] = { 0x13, 2, '1', '9' };
  36036. PKCS7Attrib attribs[] =
  36037. {
  36038. { messageTypeOid, sizeof(messageTypeOid), messageType,
  36039. sizeof(messageType) },
  36040. };
  36041. #define MAX_TESTVECTORS_LEN 19
  36042. #define ADD_PKCS7SIGNEDVECTOR(...) { \
  36043. pkcs7SignedVector _this_vector = { __VA_ARGS__ }; \
  36044. if (testSz == MAX_TESTVECTORS_LEN) { \
  36045. ret = -12568; \
  36046. goto out; \
  36047. } \
  36048. XMEMCPY(&testVectors[testSz++], &_this_vector, sizeof _this_vector);\
  36049. }
  36050. pkcs7SignedVector *testVectors = NULL;
  36051. XMEMSET(&rng, 0, sizeof(rng));
  36052. testVectors = (pkcs7SignedVector *)XMALLOC(MAX_TESTVECTORS_LEN * sizeof(*testVectors),
  36053. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36054. if (testVectors == NULL) {
  36055. ret = -12567;
  36056. goto out;
  36057. }
  36058. {
  36059. #ifndef NO_RSA
  36060. #ifndef NO_SHA256
  36061. /* Signed FirmwarePkgData, RSA, SHA256, no attribs */
  36062. ADD_PKCS7SIGNEDVECTOR(
  36063. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36064. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36065. NULL, 0,
  36066. "pkcs7signedFirmwarePkgData_RSA_SHA256_noattr.der", 0, NULL, 0, 0,
  36067. 0, 0, NULL, 0, NULL, 0, 0);
  36068. /* Signed FirmwarePkgData, RSA, SHA256, attrs */
  36069. ADD_PKCS7SIGNEDVECTOR(
  36070. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36071. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36072. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36073. "pkcs7signedFirmwarePkgData_RSA_SHA256.der", 0, NULL, 0, 0, 0, 0,
  36074. NULL, 0, NULL, 0, 0);
  36075. /* Signed FirmwarePkgData, RSA, SHA256, SubjectKeyIdentifier, attrs */
  36076. ADD_PKCS7SIGNEDVECTOR(
  36077. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36078. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36079. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36080. "pkcs7signedFirmwarePkgData_RSA_SHA256_SKID.der", 0, NULL,
  36081. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0);
  36082. /* Signed FirmwraePkgData, RSA, SHA256, server cert and ca cert, attr */
  36083. ADD_PKCS7SIGNEDVECTOR(
  36084. data, (word32)sizeof(data), SHA256h, RSAk, rsaServerPrivKeyBuf,
  36085. rsaServerPrivKeyBufSz, rsaServerCertBuf, rsaServerCertBufSz,
  36086. rsaCaCertBuf, rsaCaCertBufSz,
  36087. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36088. "pkcs7signedFirmwarePkgData_RSA_SHA256_with_ca_cert.der", 0, NULL,
  36089. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  36090. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  36091. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  36092. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, no attribs */
  36093. ADD_PKCS7SIGNEDVECTOR(
  36094. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36095. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36096. NULL, 0,
  36097. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  36098. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0);
  36099. /* Signed Encrypted FirmwarePkgData, RSA, SHA256, attribs */
  36100. ADD_PKCS7SIGNEDVECTOR(
  36101. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36102. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36103. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36104. "pkcs7signedEncryptedFirmwarePkgData_RSA_SHA256.der", 0,
  36105. NULL, 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  36106. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  36107. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  36108. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  36109. /* Signed Compressed FirmwarePkgData, RSA, SHA256, no attribs */
  36110. ADD_PKCS7SIGNEDVECTOR(
  36111. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36112. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36113. NULL, 0,
  36114. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256_noattr.der", 0,
  36115. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  36116. /* Signed Compressed FirmwarePkgData, RSA, SHA256, attribs */
  36117. ADD_PKCS7SIGNEDVECTOR(
  36118. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36119. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36120. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36121. "pkcs7signedCompressedFirmwarePkgData_RSA_SHA256.der", 0,
  36122. NULL, 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  36123. #ifndef NO_PKCS7_ENCRYPTED_DATA
  36124. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  36125. no attribs */
  36126. ADD_PKCS7SIGNEDVECTOR(
  36127. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36128. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36129. NULL, 0,
  36130. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256_noattr.der",
  36131. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  36132. 0, 0);
  36133. /* Signed Encrypted Compressed FirmwarePkgData, RSA, SHA256,
  36134. attribs */
  36135. ADD_PKCS7SIGNEDVECTOR(
  36136. data, (word32)sizeof(data), SHA256h, RSAk, rsaClientPrivKeyBuf,
  36137. rsaClientPrivKeyBufSz, rsaClientCertBuf, rsaClientCertBufSz, NULL, 0,
  36138. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36139. "pkcs7signedEncryptedCompressedFirmwarePkgData_RSA_SHA256.der",
  36140. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  36141. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  36142. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  36143. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  36144. #endif /* NO_SHA256 */
  36145. #endif /* NO_RSA */
  36146. #ifdef HAVE_ECC
  36147. #ifndef NO_SHA256
  36148. /* Signed FirmwarePkgData, ECDSA, SHA256, no attribs */
  36149. ADD_PKCS7SIGNEDVECTOR(
  36150. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36151. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36152. NULL, 0,
  36153. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  36154. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  36155. /* Signed FirmwarePkgData, ECDSA, SHA256, attribs */
  36156. ADD_PKCS7SIGNEDVECTOR(
  36157. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36158. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36159. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36160. "pkcs7signedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  36161. 0, 0, 0, 0, NULL, 0, NULL, 0, 0);
  36162. /* Signed FirmwarePkgData, ECDSA, SHA256, SubjectKeyIdentifier, attr */
  36163. ADD_PKCS7SIGNEDVECTOR(
  36164. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36165. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36166. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36167. "pkcs7signedFirmwarePkgData_ECDSA_SHA256_SKID.der", 0, NULL,
  36168. 0, CMS_SKID, 0, 0, NULL, 0, NULL, 0, 0);
  36169. #if !defined(NO_PKCS7_ENCRYPTED_DATA) && \
  36170. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  36171. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, no attribs */
  36172. ADD_PKCS7SIGNEDVECTOR(
  36173. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36174. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36175. NULL, 0,
  36176. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  36177. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key), NULL, 0, 0);
  36178. /* Signed Encrypted FirmwarePkgData, ECDSA, SHA256, attribs */
  36179. ADD_PKCS7SIGNEDVECTOR(
  36180. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36181. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36182. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36183. "pkcs7signedEncryptedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  36184. 0, 0, AES256CBCb, 1, aes256Key, sizeof(aes256Key),
  36185. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  36186. #endif /* WOLFSSL_AES_256 && !NO_PKCS7_ENCRYPTED_DATA */
  36187. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  36188. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, no attribs */
  36189. ADD_PKCS7SIGNEDVECTOR(
  36190. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36191. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36192. NULL, 0,
  36193. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der", 0, NULL,
  36194. 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  36195. /* Signed Compressed FirmwarePkgData, ECDSA, SHA256, attrib */
  36196. ADD_PKCS7SIGNEDVECTOR(
  36197. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36198. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36199. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36200. "pkcs7signedCompressedFirmwarePkgData_ECDSA_SHA256.der", 0, NULL,
  36201. 0, 0, 0, 2, NULL, 0, NULL, 0, 0);
  36202. #ifndef NO_PKCS7_ENCRYPTED_DATA
  36203. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  36204. no attribs */
  36205. ADD_PKCS7SIGNEDVECTOR(
  36206. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36207. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36208. NULL, 0,
  36209. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256_noattr.der",
  36210. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key), NULL,
  36211. 0, 0);
  36212. /* Signed Encrypted Compressed FirmwarePkgData, ECDSA, SHA256,
  36213. attribs */
  36214. ADD_PKCS7SIGNEDVECTOR(
  36215. data, (word32)sizeof(data), SHA256h, ECDSAk, eccClientPrivKeyBuf,
  36216. eccClientPrivKeyBufSz, eccClientCertBuf, eccClientCertBufSz, NULL, 0,
  36217. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  36218. "pkcs7signedEncryptedCompressedFirmwarePkgData_ECDSA_SHA256.der",
  36219. 0, NULL, 0, 0, AES256CBCb, 3, aes256Key, sizeof(aes256Key),
  36220. attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)), 0);
  36221. #endif /* !NO_PKCS7_ENCRYPTED_DATA */
  36222. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  36223. #endif /* NO_SHA256 */
  36224. #endif /* HAVE_ECC */
  36225. };
  36226. #undef MAX_TESTVECTORS_LEN
  36227. #undef ADD_PKCS7SIGNEDVECTOR
  36228. outSz = FOURK_BUF;
  36229. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36230. if (out == NULL)
  36231. ERROR_OUT(-12540, out);
  36232. XMEMSET(out, 0, outSz);
  36233. ret = wc_PKCS7_PadData((byte*)data, sizeof(data), out, outSz, 16);
  36234. if (ret < 0)
  36235. ERROR_OUT(-12541, out);
  36236. #ifndef HAVE_FIPS
  36237. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  36238. #else
  36239. ret = wc_InitRng(&rng);
  36240. #endif
  36241. if (ret != 0)
  36242. ERROR_OUT(-12542, out);
  36243. for (i = 0; i < testSz; i++) {
  36244. if (pkcs7)
  36245. wc_PKCS7_Free(pkcs7);
  36246. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  36247. if (pkcs7 == NULL)
  36248. ERROR_OUT(-12543, out);
  36249. ret = wc_PKCS7_InitWithCert(pkcs7, testVectors[i].cert,
  36250. (word32)testVectors[i].certSz);
  36251. if (ret != 0)
  36252. ERROR_OUT(-12544, out);
  36253. /* load CA certificate, if present */
  36254. if (testVectors[i].caCert != NULL) {
  36255. ret = wc_PKCS7_AddCertificate(pkcs7, testVectors[i].caCert,
  36256. (word32)testVectors[i].caCertSz);
  36257. if (ret != 0)
  36258. ERROR_OUT(-12545, out);
  36259. }
  36260. /* set SignerIdentifier to use SubjectKeyIdentifier if desired,
  36261. default is IssuerAndSerialNumber */
  36262. if (testVectors[i].sidType == CMS_SKID) {
  36263. ret = wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID);
  36264. if (ret != 0)
  36265. ERROR_OUT(-12546, out);
  36266. }
  36267. if (testVectors[i].encCompFlag == 0) {
  36268. /* encode Signed FirmwarePkgData */
  36269. encodedSz = wc_PKCS7_EncodeSignedFPD(pkcs7,
  36270. testVectors[i].privateKey, testVectors[i].privateKeySz,
  36271. testVectors[i].signOID, testVectors[i].hashOID,
  36272. (byte*)testVectors[i].content, testVectors[i].contentSz,
  36273. testVectors[i].signedAttribs,
  36274. testVectors[i].signedAttribsSz, out, outSz);
  36275. if (encodedSz < 0)
  36276. ERROR_OUT(-12547, out);
  36277. #ifndef NO_PKCS7_ENCRYPTED_DATA
  36278. } else if (testVectors[i].encCompFlag == 1) {
  36279. /* encode Signed Encrypted FirmwarePkgData */
  36280. encodedSz = wc_PKCS7_EncodeSignedEncryptedFPD(pkcs7,
  36281. testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  36282. testVectors[i].privateKey, testVectors[i].privateKeySz,
  36283. testVectors[i].encryptOID, testVectors[i].signOID,
  36284. testVectors[i].hashOID, (byte*)testVectors[i].content,
  36285. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  36286. testVectors[i].unprotectedAttribsSz,
  36287. testVectors[i].signedAttribs,
  36288. testVectors[i].signedAttribsSz, out, outSz);
  36289. if (encodedSz <= 0)
  36290. ERROR_OUT(-12548, out);
  36291. #endif
  36292. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  36293. } else if (testVectors[i].encCompFlag == 2) {
  36294. /* encode Signed Compressed FirmwarePkgData */
  36295. encodedSz = wc_PKCS7_EncodeSignedCompressedFPD(pkcs7,
  36296. testVectors[i].privateKey, testVectors[i].privateKeySz,
  36297. testVectors[i].signOID, testVectors[i].hashOID,
  36298. (byte*)testVectors[i].content, testVectors[i].contentSz,
  36299. testVectors[i].signedAttribs,
  36300. testVectors[i].signedAttribsSz, out, outSz);
  36301. if (encodedSz <= 0)
  36302. ERROR_OUT(-12549, out);
  36303. #ifndef NO_PKCS7_ENCRYPTED_DATA
  36304. } else if (testVectors[i].encCompFlag == 3) {
  36305. /* encode Signed Encrypted Compressed FirmwarePkgData */
  36306. encodedSz = wc_PKCS7_EncodeSignedEncryptedCompressedFPD(pkcs7,
  36307. testVectors[i].encryptKey, testVectors[i].encryptKeySz,
  36308. testVectors[i].privateKey, testVectors[i].privateKeySz,
  36309. testVectors[i].encryptOID, testVectors[i].signOID,
  36310. testVectors[i].hashOID, (byte*)testVectors[i].content,
  36311. testVectors[i].contentSz, testVectors[i].unprotectedAttribs,
  36312. testVectors[i].unprotectedAttribsSz,
  36313. testVectors[i].signedAttribs,
  36314. testVectors[i].signedAttribsSz, out, outSz);
  36315. if (encodedSz <= 0)
  36316. ERROR_OUT(-12550, out);
  36317. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  36318. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  36319. } else {
  36320. /* unsupported SignedData single-shot combination */
  36321. ERROR_OUT(-12551, out);
  36322. }
  36323. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  36324. /* write PKCS#7 to output file for more testing */
  36325. file = XFOPEN(testVectors[i].outFileName, "wb");
  36326. if (!file)
  36327. ERROR_OUT(-12552, out);
  36328. ret = (int)XFWRITE(out, 1, encodedSz, file);
  36329. XFCLOSE(file);
  36330. file = NULL;
  36331. if (ret != (int)encodedSz)
  36332. ERROR_OUT(-12553, out);
  36333. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  36334. wc_PKCS7_Free(pkcs7);
  36335. pkcs7 = wc_PKCS7_New(HEAP_HINT, devId);
  36336. if (pkcs7 == NULL)
  36337. ERROR_OUT(-12554, out);
  36338. wc_PKCS7_InitWithCert(pkcs7, NULL, 0);
  36339. ret = wc_PKCS7_VerifySignedData(pkcs7, out, outSz);
  36340. if (ret < 0)
  36341. ERROR_OUT(-12555, out);
  36342. #ifndef NO_PKCS7_STREAM
  36343. {
  36344. word32 z;
  36345. for (z = 0; z < outSz && ret != 0; z++) {
  36346. ret = wc_PKCS7_VerifySignedData(pkcs7, out + z, 1);
  36347. if (ret < 0 && ret != WC_PKCS7_WANT_READ_E) {
  36348. printf("unexpected error %d\n", ret);
  36349. ERROR_OUT(-12556, out);
  36350. }
  36351. }
  36352. }
  36353. #endif
  36354. if (pkcs7->singleCert == NULL || pkcs7->singleCertSz == 0)
  36355. ERROR_OUT(-12557, out);
  36356. if (testVectors[i].encCompFlag == 0) {
  36357. /* verify decoded content matches expected */
  36358. if ((pkcs7->contentSz != testVectors[i].contentSz) ||
  36359. XMEMCMP(pkcs7->content, testVectors[i].content,
  36360. pkcs7->contentSz)) {
  36361. ERROR_OUT(-12558, out);
  36362. }
  36363. }
  36364. #ifndef NO_PKCS7_ENCRYPTED_DATA
  36365. else if (testVectors[i].encCompFlag == 1) {
  36366. /* decrypt inner encryptedData */
  36367. pkcs7->encryptionKey = testVectors[i].encryptKey;
  36368. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  36369. ret = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  36370. pkcs7->contentSz, out, outSz);
  36371. if (ret < 0)
  36372. ERROR_OUT(-12559, out);
  36373. /* compare decrypted to expected */
  36374. if (((word32)ret != testVectors[i].contentSz) ||
  36375. XMEMCMP(out, testVectors[i].content, ret))
  36376. ERROR_OUT(-12560, out);
  36377. }
  36378. #endif
  36379. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA)
  36380. else if (testVectors[i].encCompFlag == 2) {
  36381. /* decompress inner compressedData */
  36382. ret = wc_PKCS7_DecodeCompressedData(pkcs7, pkcs7->content,
  36383. pkcs7->contentSz, out, outSz);
  36384. if (ret < 0)
  36385. ERROR_OUT(-12561, out);
  36386. /* compare decompressed to expected */
  36387. if (((word32)ret != testVectors[i].contentSz) ||
  36388. XMEMCMP(out, testVectors[i].content, ret))
  36389. ERROR_OUT(-12562, out);
  36390. }
  36391. #ifndef NO_PKCS7_ENCRYPTED_DATA
  36392. else if (testVectors[i].encCompFlag == 3) {
  36393. encryptedTmpSz = FOURK_BUF;
  36394. encryptedTmp = (byte*)XMALLOC(encryptedTmpSz, HEAP_HINT,
  36395. DYNAMIC_TYPE_TMP_BUFFER);
  36396. if (encryptedTmp == NULL)
  36397. ERROR_OUT(-12563, out);
  36398. XMEMSET(encryptedTmp, 0, encryptedTmpSz);
  36399. /* decrypt inner encryptedData */
  36400. pkcs7->encryptionKey = testVectors[i].encryptKey;
  36401. pkcs7->encryptionKeySz = testVectors[i].encryptKeySz;
  36402. encryptedTmpSz = wc_PKCS7_DecodeEncryptedData(pkcs7, pkcs7->content,
  36403. pkcs7->contentSz, encryptedTmp,
  36404. encryptedTmpSz);
  36405. if (encryptedTmpSz < 0 || pkcs7->contentOID != COMPRESSED_DATA)
  36406. ERROR_OUT(-12564, out);
  36407. /* decompress inner compressedData */
  36408. ret = wc_PKCS7_DecodeCompressedData(pkcs7, encryptedTmp,
  36409. encryptedTmpSz, out, outSz);
  36410. if (ret < 0)
  36411. ERROR_OUT(-12565, out);
  36412. /* compare decompressed to expected */
  36413. if (((word32)ret != testVectors[i].contentSz) ||
  36414. XMEMCMP(out, testVectors[i].content, ret))
  36415. ERROR_OUT(-12566, out);
  36416. }
  36417. #endif /* NO_PKCS7_ENCRYPTED_DATA */
  36418. #endif /* HAVE_LIBZ && !NO_PKCS7_COMPRESSED_DATA */
  36419. }
  36420. out:
  36421. if (pkcs7 != NULL)
  36422. wc_PKCS7_Free(pkcs7);
  36423. if (out != NULL)
  36424. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36425. #if defined(HAVE_LIBZ) && !defined(NO_PKCS7_COMPRESSED_DATA) && \
  36426. !defined(NO_PKCS7_ENCRYPTED_DATA)
  36427. if (encryptedTmp != NULL)
  36428. XFREE(encryptedTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36429. #endif
  36430. if (testVectors != NULL)
  36431. XFREE(testVectors, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36432. wc_FreeRng(&rng);
  36433. if (ret > 0)
  36434. return 0;
  36435. (void)eccClientCertBuf;
  36436. (void)eccClientCertBufSz;
  36437. (void)eccClientPrivKeyBuf;
  36438. (void)eccClientPrivKeyBufSz;
  36439. (void)rsaClientCertBuf;
  36440. (void)rsaClientCertBufSz;
  36441. (void)rsaClientPrivKeyBuf;
  36442. (void)rsaClientPrivKeyBufSz;
  36443. (void)rsaServerCertBuf;
  36444. (void)rsaServerCertBufSz;
  36445. (void)rsaServerPrivKeyBuf;
  36446. (void)rsaServerPrivKeyBufSz;
  36447. (void)rsaCaCertBuf;
  36448. (void)rsaCaCertBufSz;
  36449. (void)rsaCaPrivKeyBuf;
  36450. (void)rsaCaPrivKeyBufSz;
  36451. return ret;
  36452. }
  36453. WOLFSSL_TEST_SUBROUTINE int pkcs7signed_test(void)
  36454. {
  36455. int ret = 0;
  36456. byte* rsaClientCertBuf = NULL;
  36457. byte* rsaServerCertBuf = NULL;
  36458. byte* rsaCaCertBuf = NULL;
  36459. byte* eccClientCertBuf = NULL;
  36460. byte* rsaClientPrivKeyBuf = NULL;
  36461. byte* rsaServerPrivKeyBuf = NULL;
  36462. byte* rsaCaPrivKeyBuf = NULL;
  36463. byte* eccClientPrivKeyBuf = NULL;
  36464. word32 rsaClientCertBufSz = 0;
  36465. word32 rsaServerCertBufSz = 0;
  36466. word32 rsaCaCertBufSz = 0;
  36467. word32 eccClientCertBufSz = 0;
  36468. word32 rsaClientPrivKeyBufSz = 0;
  36469. word32 rsaServerPrivKeyBufSz = 0;
  36470. word32 rsaCaPrivKeyBufSz = 0;
  36471. word32 eccClientPrivKeyBufSz = 0;
  36472. #ifndef NO_RSA
  36473. /* read client RSA cert and key in DER format */
  36474. rsaClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  36475. DYNAMIC_TYPE_TMP_BUFFER);
  36476. if (rsaClientCertBuf == NULL)
  36477. ret = -12600;
  36478. rsaClientPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  36479. DYNAMIC_TYPE_TMP_BUFFER);
  36480. if (ret == 0 && rsaClientPrivKeyBuf == NULL) {
  36481. ret = -12601;
  36482. }
  36483. rsaClientCertBufSz = FOURK_BUF;
  36484. rsaClientPrivKeyBufSz = FOURK_BUF;
  36485. /* read server RSA cert and key in DER format */
  36486. rsaServerCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  36487. DYNAMIC_TYPE_TMP_BUFFER);
  36488. if (ret == 0 && rsaServerCertBuf == NULL)
  36489. ret = -12602;
  36490. rsaServerPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  36491. DYNAMIC_TYPE_TMP_BUFFER);
  36492. if (ret == 0 && rsaServerPrivKeyBuf == NULL) {
  36493. ret = -12603;
  36494. }
  36495. rsaServerCertBufSz = FOURK_BUF;
  36496. rsaServerPrivKeyBufSz = FOURK_BUF;
  36497. /* read CA RSA cert and key in DER format, for use with server cert */
  36498. rsaCaCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  36499. DYNAMIC_TYPE_TMP_BUFFER);
  36500. if (ret == 0 && rsaCaCertBuf == NULL)
  36501. ret = -12604;
  36502. rsaCaPrivKeyBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  36503. DYNAMIC_TYPE_TMP_BUFFER);
  36504. if (ret == 0 && rsaCaPrivKeyBuf == NULL) {
  36505. ret = -12605;
  36506. }
  36507. rsaCaCertBufSz = FOURK_BUF;
  36508. rsaCaPrivKeyBufSz = FOURK_BUF;
  36509. #endif /* NO_RSA */
  36510. #ifdef HAVE_ECC
  36511. /* read client ECC cert and key in DER format */
  36512. eccClientCertBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  36513. DYNAMIC_TYPE_TMP_BUFFER);
  36514. if (ret == 0 && eccClientCertBuf == NULL) {
  36515. ret = -12606;
  36516. }
  36517. eccClientPrivKeyBuf =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  36518. DYNAMIC_TYPE_TMP_BUFFER);
  36519. if (ret == 0 && eccClientPrivKeyBuf == NULL) {
  36520. ret = -12607;
  36521. }
  36522. eccClientCertBufSz = FOURK_BUF;
  36523. eccClientPrivKeyBufSz = FOURK_BUF;
  36524. #endif /* HAVE_ECC */
  36525. if (ret >= 0)
  36526. ret = pkcs7_load_certs_keys(rsaClientCertBuf, &rsaClientCertBufSz,
  36527. rsaClientPrivKeyBuf, &rsaClientPrivKeyBufSz,
  36528. rsaServerCertBuf, &rsaServerCertBufSz,
  36529. rsaServerPrivKeyBuf, &rsaServerPrivKeyBufSz,
  36530. rsaCaCertBuf, &rsaCaCertBufSz,
  36531. rsaCaPrivKeyBuf, &rsaCaPrivKeyBufSz,
  36532. eccClientCertBuf, &eccClientCertBufSz,
  36533. eccClientPrivKeyBuf, &eccClientPrivKeyBufSz);
  36534. if (ret < 0) {
  36535. ret = -12608;
  36536. }
  36537. if (ret >= 0)
  36538. ret = pkcs7signed_run_vectors(rsaClientCertBuf, (word32)rsaClientCertBufSz,
  36539. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  36540. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  36541. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  36542. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  36543. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  36544. eccClientCertBuf, (word32)eccClientCertBufSz,
  36545. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  36546. if (ret >= 0)
  36547. ret = pkcs7signed_run_SingleShotVectors(
  36548. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  36549. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz,
  36550. rsaServerCertBuf, (word32)rsaServerCertBufSz,
  36551. rsaServerPrivKeyBuf, (word32)rsaServerPrivKeyBufSz,
  36552. rsaCaCertBuf, (word32)rsaCaCertBufSz,
  36553. rsaCaPrivKeyBuf, (word32)rsaCaPrivKeyBufSz,
  36554. eccClientCertBuf, (word32)eccClientCertBufSz,
  36555. eccClientPrivKeyBuf, (word32)eccClientPrivKeyBufSz);
  36556. #if !defined(NO_RSA) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  36557. if (ret >= 0)
  36558. ret = pkcs7callback_test(
  36559. rsaClientCertBuf, (word32)rsaClientCertBufSz,
  36560. rsaClientPrivKeyBuf, (word32)rsaClientPrivKeyBufSz);
  36561. #endif
  36562. XFREE(rsaClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36563. XFREE(rsaClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36564. XFREE(rsaServerCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36565. XFREE(rsaServerPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36566. XFREE(rsaCaCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36567. XFREE(rsaCaPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36568. XFREE(eccClientCertBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36569. XFREE(eccClientPrivKeyBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  36570. return ret;
  36571. }
  36572. #endif /* HAVE_PKCS7 */
  36573. #ifdef HAVE_VALGRIND
  36574. /* Need a static build to have access to symbols. */
  36575. /* Maximum number of bytes in a number to test. */
  36576. #define MP_MAX_TEST_BYTE_LEN 32
  36577. static int randNum(mp_int* n, int len, WC_RNG* rng, void* heap)
  36578. {
  36579. byte d[MP_MAX_TEST_BYTE_LEN];
  36580. int ret;
  36581. (void)heap;
  36582. do {
  36583. ret = wc_RNG_GenerateBlock(rng, d, len);
  36584. if (ret != 0)
  36585. return ret;
  36586. ret = mp_read_unsigned_bin(n, d, len);
  36587. if (ret != 0)
  36588. return ret;
  36589. } while (mp_iszero(n));
  36590. return 0;
  36591. }
  36592. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  36593. static int mp_test_div_3(mp_int* a, mp_int* r, WC_RNG* rng)
  36594. {
  36595. int i, j;
  36596. mp_digit rem;
  36597. mp_digit rem2;
  36598. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  36599. defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  36600. for (i = 0; i < 10; i++) {
  36601. for (j = 1; j < 10; j++) {
  36602. if (randNum(a, j, rng, NULL) != 0)
  36603. return -12620;
  36604. if (mp_div_3(a, r, &rem) != 0)
  36605. return -12621;
  36606. if (mp_mul_d(r, 3, r) != 0)
  36607. return -12622;
  36608. if (mp_add_d(r, rem, r) != 0)
  36609. return -12623;
  36610. if (mp_cmp(r, a) != MP_EQ)
  36611. return -12624;
  36612. }
  36613. }
  36614. if (mp_div_3(a, r, &rem) != 0)
  36615. return -12625;
  36616. if (mp_div_3(a, a, NULL) != 0)
  36617. return -12626;
  36618. if (mp_cmp(r, a) != MP_EQ)
  36619. return -12627;
  36620. #endif
  36621. #if defined(WOLFSSL_SP_MATH_ALL)
  36622. if (mp_div_d(a, 10, r, &rem) != 0)
  36623. return -12628;
  36624. if (mp_div_d(a, 10, a, NULL) != 0)
  36625. return -12629;
  36626. if (mp_cmp(r, a) != MP_EQ)
  36627. return -12630;
  36628. if (mp_div_d(a, 12, r, &rem) != 0)
  36629. return -12631;
  36630. if (mp_div_d(a, 12, a, NULL) != 0)
  36631. return -12632;
  36632. if (mp_cmp(r, a) != MP_EQ)
  36633. return -12633;
  36634. if (mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), r, &rem) != 0)
  36635. return -12634;
  36636. if (mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), NULL, &rem2) != 0)
  36637. return -12635;
  36638. if (mp_div_d(a, (mp_digit)1 << (DIGIT_BIT / 2), a, NULL) != 0)
  36639. return -12636;
  36640. if (mp_cmp(r, a) != MP_EQ)
  36641. return -12637;
  36642. if (rem != rem2)
  36643. return -12638;
  36644. #endif
  36645. (void)a;
  36646. (void)r;
  36647. (void)rng;
  36648. (void)i;
  36649. (void)j;
  36650. (void)rem;
  36651. (void)rem2;
  36652. return 0;
  36653. }
  36654. #endif /* WOLFSSL_SP_MATH || !USE_FAST_MATH */
  36655. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  36656. !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  36657. (!defined WOLFSSL_SP_MATH && !defined(WOLFSSL_SP_MATH_ALL) && \
  36658. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  36659. static int mp_test_radix_10(mp_int* a, mp_int* r, WC_RNG* rng)
  36660. {
  36661. int ret;
  36662. int i, j;
  36663. int size;
  36664. char str[30];
  36665. WOLFSSL_SMALL_STACK_STATIC const char* badStr1 = "A";
  36666. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "a";
  36667. WOLFSSL_SMALL_STACK_STATIC const char* badStr3 = " ";
  36668. WOLFSSL_SMALL_STACK_STATIC const char* zeros = "000";
  36669. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  36670. for (i = 0; i < 10; i++) {
  36671. for (j = 2; j < 12; j++) {
  36672. if (randNum(a, j, rng, NULL) != 0)
  36673. return -12640;
  36674. if (mp_radix_size(a, MP_RADIX_DEC, &size) != MP_OKAY)
  36675. return -12641;
  36676. if (mp_toradix(a, str, MP_RADIX_DEC) != MP_OKAY)
  36677. return -12660;
  36678. if ((int)XSTRLEN(str) != size - 1)
  36679. return -12642;
  36680. if (mp_read_radix(r, str, MP_RADIX_DEC) != MP_OKAY)
  36681. return -12661;
  36682. if (mp_cmp(a, r) != MP_EQ)
  36683. return -12643;
  36684. }
  36685. }
  36686. if (mp_read_radix(r, badStr1, MP_RADIX_DEC) != MP_VAL)
  36687. return -12644;
  36688. if (mp_read_radix(r, badStr2, MP_RADIX_DEC) != MP_VAL)
  36689. return -12645;
  36690. if (mp_read_radix(r, badStr3, MP_RADIX_DEC) != MP_VAL)
  36691. return -12646;
  36692. if (mp_read_radix(r, zeros, MP_RADIX_DEC) != MP_OKAY)
  36693. return -12647;
  36694. if (!mp_iszero(r))
  36695. return -12648;
  36696. mp_set(r, 1);
  36697. if (mp_read_radix(r, empty, MP_RADIX_DEC) != MP_OKAY)
  36698. return -12649;
  36699. if (!mp_iszero(r))
  36700. return -12650;
  36701. mp_zero(a);
  36702. ret = mp_radix_size(a, MP_RADIX_DEC, &size);
  36703. if (ret != 0)
  36704. return -12651;
  36705. if (size != 2)
  36706. return -12652;
  36707. ret = mp_toradix(a, str, MP_RADIX_DEC);
  36708. if (ret != 0)
  36709. return -12653;
  36710. if ((int)XSTRLEN(str) != size - 1)
  36711. return -12654;
  36712. ret = mp_read_radix(r, str, MP_RADIX_DEC);
  36713. if (ret != 0)
  36714. return -12655;
  36715. if (!mp_iszero(r))
  36716. return -12656;
  36717. return 0;
  36718. }
  36719. #endif
  36720. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  36721. defined(HAVE_ECC))
  36722. static int mp_test_radix_16(mp_int* a, mp_int* r, WC_RNG* rng)
  36723. {
  36724. int ret;
  36725. int i, j;
  36726. int size;
  36727. char str[30];
  36728. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  36729. static char longStr[2 * sizeof(a->dp) + 2];
  36730. #endif
  36731. WOLFSSL_SMALL_STACK_STATIC const char* badStr1 = " ";
  36732. WOLFSSL_SMALL_STACK_STATIC const char* badStr2 = "}";
  36733. WOLFSSL_SMALL_STACK_STATIC const char* empty = "";
  36734. for (i = 0; i < 10; i++) {
  36735. for (j = 2; j < 12; j++) {
  36736. if (randNum(a, j, rng, NULL) != 0)
  36737. return -12660;
  36738. mp_radix_size(a, MP_RADIX_HEX, &size);
  36739. mp_toradix(a, str, MP_RADIX_HEX);
  36740. if ((int)XSTRLEN(str) != size - 1)
  36741. return -12661;
  36742. mp_read_radix(r, str, MP_RADIX_HEX);
  36743. if (mp_cmp(a, r) != MP_EQ)
  36744. return -12662;
  36745. }
  36746. }
  36747. if (mp_read_radix(r, badStr1, MP_RADIX_HEX) != MP_VAL)
  36748. return -12663;
  36749. if (mp_read_radix(r, badStr2, MP_RADIX_HEX) != MP_VAL)
  36750. return -12664;
  36751. mp_set(r, 1);
  36752. if (mp_read_radix(r, empty, MP_RADIX_HEX) != MP_OKAY)
  36753. return -12665;
  36754. if (!mp_iszero(r))
  36755. return -12666;
  36756. #if defined(WOLFSSL_SP_MATH) || defined(USE_FAST_MATH)
  36757. /* Fixed MP data size - string can be too long. */
  36758. longStr[0] = '8';
  36759. XMEMSET(longStr+1, '0', sizeof(longStr) - 2);
  36760. longStr[sizeof(longStr)-1] = '\0';
  36761. if (mp_read_radix(r, longStr, MP_RADIX_HEX) != MP_VAL)
  36762. return -12667;
  36763. #endif
  36764. mp_zero(a);
  36765. ret = mp_radix_size(a, MP_RADIX_HEX, &size);
  36766. if (ret != 0)
  36767. return -12668;
  36768. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  36769. if (size != 3)
  36770. #else
  36771. if (size != 2)
  36772. #endif
  36773. return -12669;
  36774. ret = mp_toradix(a, str, MP_RADIX_HEX);
  36775. if (ret != 0)
  36776. return -12670;
  36777. if ((int)XSTRLEN(str) != size - 1)
  36778. return -12671;
  36779. ret = mp_read_radix(r, str, MP_RADIX_HEX);
  36780. if (ret != 0)
  36781. return -12672;
  36782. if (!mp_iszero(r))
  36783. return -12673;
  36784. #ifdef WOLFSSL_SP_MATH
  36785. ret = mp_toradix(a, str, 8);
  36786. if (ret != MP_VAL)
  36787. return -12674;
  36788. ret = mp_radix_size(a, 8, &size);
  36789. if (ret != MP_VAL)
  36790. return -12675;
  36791. #endif
  36792. return 0;
  36793. }
  36794. #endif
  36795. static int mp_test_shift(mp_int* a, mp_int* r1, WC_RNG* rng)
  36796. {
  36797. int i;
  36798. if (randNum(a, 4, rng, NULL) != 0)
  36799. return -12680;
  36800. for (i = 0; i < 4; i++) {
  36801. mp_copy(r1, a);
  36802. if (mp_lshd(r1, i) != MP_OKAY)
  36803. return -12681;
  36804. mp_rshd(r1, i);
  36805. if (mp_cmp(a, r1) != MP_EQ)
  36806. return -12682;
  36807. }
  36808. #ifndef WOLFSSL_SP_MATH
  36809. for (i = 0; i < DIGIT_BIT+1; i++) {
  36810. if (mp_mul_2d(a, i, r1) != MP_OKAY)
  36811. return -12683;
  36812. mp_rshb(r1, i);
  36813. if (mp_cmp(a, r1) != MP_EQ)
  36814. return -12684;
  36815. }
  36816. #endif
  36817. return 0;
  36818. }
  36819. static int mp_test_add_sub_d(mp_int* a, mp_int* r1)
  36820. {
  36821. int i, j;
  36822. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  36823. mp_zero(a);
  36824. mp_set_bit(a, i);
  36825. if (a->used != (i + DIGIT_BIT) / DIGIT_BIT)
  36826. return -12690;
  36827. for (j = 0; j < i && j < DIGIT_BIT; j++) {
  36828. mp_zero(r1);
  36829. mp_set_bit(r1, i);
  36830. if (mp_sub_d(r1, (mp_digit)1 << j, r1) != MP_OKAY)
  36831. return -12691;
  36832. if (mp_add_d(r1, (mp_digit)1 << j, r1) != MP_OKAY)
  36833. return -12692;
  36834. if (mp_cmp(a, r1) != MP_EQ)
  36835. return -12693;
  36836. }
  36837. }
  36838. mp_zero(r1);
  36839. if (mp_add_d(r1, 1, r1) != MP_OKAY)
  36840. return -12694;
  36841. if (r1->used != 1)
  36842. return -12695;
  36843. if (mp_sub_d(r1, 1, r1) != MP_OKAY)
  36844. return -12696;
  36845. if (r1->used != 0)
  36846. return -12697;
  36847. return 0;
  36848. }
  36849. static int mp_test_read_to_bin(mp_int* a)
  36850. {
  36851. WOLFSSL_SMALL_STACK_STATIC const byte in[16] = {
  36852. 0x91, 0xa2, 0xb3, 0xc4, 0xd5, 0xe6, 0xf7, 0x08,
  36853. 0x93, 0xa4, 0xb4, 0xc5, 0xd6, 0xe7, 0xf8, 0x09
  36854. };
  36855. byte out[24];
  36856. int i, j, k;
  36857. const byte* p;
  36858. int ret;
  36859. for (i = 0; i < (int)sizeof(in); i++) {
  36860. p = in + sizeof(in) - i;
  36861. ret = mp_read_unsigned_bin(a, p, i);
  36862. if (ret != 0)
  36863. return -12710;
  36864. for (j = i; j < (int)sizeof(out); j++) {
  36865. XMEMSET(out, 0xff, sizeof(out));
  36866. ret = mp_to_unsigned_bin_len(a, out, j);
  36867. if (ret != 0)
  36868. return -12711;
  36869. for (k = 0; k < j - i; k++) {
  36870. if (out[k] != 0)
  36871. return -12712;
  36872. }
  36873. for (; k < j; k++) {
  36874. if (out[k] != p[k - (j - i)])
  36875. return -12713;
  36876. }
  36877. }
  36878. }
  36879. ret = mp_read_unsigned_bin(a, NULL, 0);
  36880. if (ret != 0)
  36881. return -12714;
  36882. if (!mp_iszero(a))
  36883. return -12715;
  36884. return 0;
  36885. }
  36886. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  36887. static int mp_test_set_int(mp_int* a)
  36888. {
  36889. #if SP_ULONG_BITS == 64
  36890. unsigned long n = 0xfedcba9876543210UL;
  36891. byte exp[8] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
  36892. byte out[8] = { 0 };
  36893. #elif SP_ULONG_BITS == 32
  36894. unsigned long n = 0xfedcba98UL;
  36895. byte exp[4] = { 0xfe, 0xdc, 0xba, 0x98 };
  36896. byte out[4] = { 0 };
  36897. #elif SP_ULONG_BITS == 16
  36898. unsigned long n = 0xfedc;
  36899. byte exp[2] = { 0xfe, 0xdc };
  36900. byte out[2] = { 0 };
  36901. #elif SP_ULONG_BITS == 8
  36902. unsigned long n = 0xfe;
  36903. byte exp[1] = { 0xfe };
  36904. byte out[1] = { 0 };
  36905. #endif
  36906. int ret;
  36907. ret = mp_set_int(a, n);
  36908. if (ret != 0)
  36909. return -12720;
  36910. ret = mp_unsigned_bin_size(a);
  36911. if (ret != sizeof(exp))
  36912. return -12721;
  36913. ret = mp_to_unsigned_bin(a, out);
  36914. if (ret != 0)
  36915. return -12722;
  36916. if (XMEMCMP(exp, out, sizeof(exp)) != 0)
  36917. return -12723;
  36918. return 0;
  36919. }
  36920. #endif
  36921. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  36922. static int mp_test_param(mp_int* a, mp_int* b, mp_int* r, WC_RNG* rng)
  36923. {
  36924. byte buffer[16];
  36925. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  36926. char hexStr[] = "abcdef0123456789";
  36927. #ifndef WOLFSSL_SP_INT_NEGATIVE
  36928. char negStr[] = "-1234";
  36929. #endif
  36930. #endif
  36931. #if !defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_KEY_GEN) || \
  36932. defined(HAVE_COMP_KEY)
  36933. char decStr[] = "0987654321";
  36934. #endif
  36935. int ret;
  36936. #ifdef WOLFSSL_SP_MATH_ALL
  36937. mp_digit rho;
  36938. int size;
  36939. #endif
  36940. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  36941. int result;
  36942. #endif
  36943. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  36944. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  36945. mp_digit rd;
  36946. #endif
  36947. (void)rng;
  36948. (void)r;
  36949. ret = mp_init(NULL);
  36950. if (ret != MP_VAL)
  36951. return -12730;
  36952. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  36953. ret = mp_init_multi(NULL, NULL, NULL, NULL, NULL, NULL);
  36954. if (ret != MP_OKAY)
  36955. return -12731;
  36956. #endif
  36957. mp_free(NULL);
  36958. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || !defined(NO_DH) || defined(HAVE_ECC)
  36959. ret = mp_grow(NULL, 1);
  36960. if (ret != MP_VAL)
  36961. return -12732;
  36962. #ifdef WOLFSSL_SP_MATH
  36963. ret = mp_grow(a, SP_INT_DIGITS + 1);
  36964. if (ret != MP_MEM)
  36965. return -12733;
  36966. #endif
  36967. #endif
  36968. mp_clear(NULL);
  36969. ret = mp_abs(NULL, NULL);
  36970. if (ret != MP_VAL)
  36971. return -12734;
  36972. ret = mp_abs(a, NULL);
  36973. if (ret != MP_VAL)
  36974. return -12735;
  36975. ret = mp_abs(NULL, b);
  36976. if (ret != MP_VAL)
  36977. return -12736;
  36978. ret = mp_unsigned_bin_size(NULL);
  36979. if (ret != 0)
  36980. return -12737;
  36981. ret = mp_read_unsigned_bin(NULL, NULL, sizeof(buffer));
  36982. if (ret != MP_VAL)
  36983. return -12738;
  36984. ret = mp_read_unsigned_bin(NULL, buffer, sizeof(buffer));
  36985. if (ret != MP_VAL)
  36986. return -12739;
  36987. ret = mp_read_unsigned_bin(a, NULL, sizeof(buffer));
  36988. if (ret != MP_VAL)
  36989. return -12740;
  36990. ret = mp_read_unsigned_bin(a, buffer, SP_INT_DIGITS * SP_WORD_SIZEOF + 1);
  36991. if (ret != MP_VAL)
  36992. return -12741;
  36993. #if defined(HAVE_ECC) || defined(WOLFSSL_SP_MATH_ALL)
  36994. ret = mp_read_radix(NULL, NULL, 16);
  36995. if (ret != MP_VAL)
  36996. return -12742;
  36997. ret = mp_read_radix(a, NULL, 16);
  36998. if (ret != MP_VAL)
  36999. return -12743;
  37000. ret = mp_read_radix(NULL, hexStr, 16);
  37001. if (ret != MP_VAL)
  37002. return -12744;
  37003. #ifndef WOLFSSL_SP_INT_NEGATIVE
  37004. ret = mp_read_radix(a, negStr, 16);
  37005. if (ret != MP_VAL)
  37006. return -12745;
  37007. #ifdef WOLFSSL_SP_MATH_ALL
  37008. ret = mp_read_radix(a, negStr, 10);
  37009. if (ret != MP_VAL)
  37010. return -12746;
  37011. #endif /* WOLFSSL_SP_MATH_ALL */
  37012. #endif /* WOLFSSL_SP_INT_NEGATIVE */
  37013. #endif
  37014. #ifndef WOLFSSL_SP_MATH_ALL
  37015. /* Radix 10 only supported with ALL. */
  37016. ret = mp_read_radix(a, decStr, 10);
  37017. if (ret != MP_VAL)
  37018. return -12747;
  37019. #endif
  37020. /* Radix 8 not supported SP_INT. */
  37021. ret = mp_read_radix(a, "0123", 8);
  37022. if (ret != MP_VAL)
  37023. return -12748;
  37024. ret = mp_count_bits(NULL);
  37025. if (ret != 0)
  37026. return -12749;
  37027. ret = mp_is_bit_set(NULL, 0);
  37028. if (ret != 0)
  37029. return -12750;
  37030. ret = mp_leading_bit(NULL);
  37031. if (ret != 0)
  37032. return -12751;
  37033. mp_zero(a);
  37034. ret = mp_leading_bit(a);
  37035. if (ret != 0)
  37036. return -12752;
  37037. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  37038. defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || \
  37039. !defined(NO_RSA)
  37040. ret = mp_set_bit(NULL, 1);
  37041. if (ret != MP_VAL)
  37042. return -12753;
  37043. #endif
  37044. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  37045. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37046. ret = mp_to_unsigned_bin(NULL, NULL);
  37047. if (ret != MP_VAL)
  37048. return -12754;
  37049. ret = mp_to_unsigned_bin(a, NULL);
  37050. if (ret != MP_VAL)
  37051. return -12755;
  37052. ret = mp_to_unsigned_bin(NULL, buffer);
  37053. if (ret != MP_VAL)
  37054. return -12756;
  37055. #endif
  37056. ret = mp_to_unsigned_bin_len(NULL, NULL, 1);
  37057. if (ret != MP_VAL)
  37058. return -12757;
  37059. ret = mp_to_unsigned_bin_len(a, NULL, 1);
  37060. if (ret != MP_VAL)
  37061. return -12758;
  37062. ret = mp_to_unsigned_bin_len(NULL, buffer, 1);
  37063. if (ret != MP_VAL)
  37064. return -12759;
  37065. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  37066. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37067. ret = mp_to_unsigned_bin_at_pos(0, NULL, NULL);
  37068. if (ret != MP_VAL)
  37069. return -12760;
  37070. ret = mp_to_unsigned_bin_at_pos(0, a, NULL);
  37071. if (ret != MP_VAL)
  37072. return -12761;
  37073. ret = mp_to_unsigned_bin_at_pos(0, NULL, buffer);
  37074. if (ret != MP_VAL)
  37075. return -12762;
  37076. ret = mp_to_unsigned_bin_at_pos(0, a, buffer);
  37077. if (ret != MP_OKAY)
  37078. return -12763;
  37079. #endif
  37080. #if !defined(WOLFSSL_RSA_VERIFY_ONLY) || (!defined(NO_DH) || defined(HAVE_ECC))
  37081. ret = mp_copy(NULL, NULL);
  37082. if (ret != MP_VAL)
  37083. return -12764;
  37084. ret = mp_copy(a, NULL);
  37085. if (ret != MP_VAL)
  37086. return -12765;
  37087. ret = mp_copy(NULL, b);
  37088. if (ret != MP_VAL)
  37089. return -12766;
  37090. #endif
  37091. #if defined(WOLFSSL_KEY_GEN) || !defined(NO_DH)
  37092. ret = sp_2expt(NULL, 1);
  37093. if (ret != MP_VAL)
  37094. return -12767;
  37095. #endif
  37096. ret = mp_set(NULL, 0);
  37097. if (ret != MP_VAL)
  37098. return -12768;
  37099. ret = mp_cmp_d(NULL, 0);
  37100. if (ret != MP_LT)
  37101. return -12769;
  37102. ret = mp_cmp(NULL, NULL);
  37103. if (ret != MP_EQ)
  37104. return -12770;
  37105. ret = mp_cmp(a, NULL);
  37106. if (ret != MP_GT)
  37107. return -12771;
  37108. ret = mp_cmp(NULL, b);
  37109. if (ret != MP_LT)
  37110. return -12772;
  37111. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37112. mp_rshd(NULL, 1);
  37113. #endif
  37114. mp_zero(NULL);
  37115. #if !defined(NO_DH) || defined(HAVE_ECC) || defined(WC_RSA_BLINDING) || \
  37116. !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37117. ret = mp_lshd(NULL, 0);
  37118. if (ret != MP_VAL)
  37119. return -12773;
  37120. ret = mp_lshd(a, SP_INT_DIGITS + 1);
  37121. if (ret != MP_VAL)
  37122. return -12774;
  37123. #endif
  37124. #if defined(WOLFSSL_SP_MATH_ALL)
  37125. ret = mp_div(NULL, NULL, a, b);
  37126. if (ret != MP_VAL)
  37127. return -12775;
  37128. ret = mp_div(a, NULL, a, b);
  37129. if (ret != MP_VAL)
  37130. return -12776;
  37131. ret = mp_div(NULL, b, a, b);
  37132. if (ret != MP_VAL)
  37133. return -12777;
  37134. ret = mp_div(a, b, NULL, NULL);
  37135. if (ret != MP_VAL)
  37136. return -12778;
  37137. #endif
  37138. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  37139. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  37140. ret = mp_mod(NULL, NULL, NULL);
  37141. if (ret != MP_VAL)
  37142. return -12779;
  37143. ret = mp_mod(a, NULL, NULL);
  37144. if (ret != MP_VAL)
  37145. return -12780;
  37146. ret = mp_mod(NULL, b, NULL);
  37147. if (ret != MP_VAL)
  37148. return -12781;
  37149. ret = mp_mod(NULL, NULL, r);
  37150. if (ret != MP_VAL)
  37151. return -12782;
  37152. ret = mp_mod(a, b, NULL);
  37153. if (ret != MP_VAL)
  37154. return -12783;
  37155. ret = mp_mod(a, NULL, r);
  37156. if (ret != MP_VAL)
  37157. return -12784;
  37158. ret = mp_mod(NULL, b, r);
  37159. if (ret != MP_VAL)
  37160. return -12785;
  37161. #endif
  37162. #if !defined(NO_RSA) || defined(WOLFSSL_SP_MATH_ALL)
  37163. ret = mp_set_int(NULL, 0);
  37164. if (ret != MP_VAL)
  37165. return -12786;
  37166. #endif
  37167. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  37168. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  37169. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  37170. if (ret != MP_VAL)
  37171. return 9950;
  37172. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  37173. if (ret != MP_VAL)
  37174. return 9951;
  37175. ret = mp_exptmod_ex(NULL, a, 1, NULL, NULL);
  37176. if (ret != MP_VAL)
  37177. return 9952;
  37178. ret = mp_exptmod_ex(NULL, NULL, 1, a, NULL);
  37179. if (ret != MP_VAL)
  37180. return 9953;
  37181. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  37182. if (ret != MP_VAL)
  37183. return 9954;
  37184. ret = mp_exptmod_ex(a, a, 1, a, NULL);
  37185. if (ret != MP_VAL)
  37186. return 9955;
  37187. ret = mp_exptmod_ex(a, a, 1, NULL, a);
  37188. if (ret != MP_VAL)
  37189. return 9956;
  37190. ret = mp_exptmod_ex(a, NULL, 1, a, a);
  37191. if (ret != MP_VAL)
  37192. return 9957;
  37193. ret = mp_exptmod_ex(NULL, a, 1, a, a);
  37194. if (ret != MP_VAL)
  37195. return 9958;
  37196. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  37197. if (ret != MP_VAL)
  37198. return 9960;
  37199. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  37200. if (ret != MP_VAL)
  37201. return 9961;
  37202. ret = mp_exptmod_nct(NULL, a, NULL, NULL);
  37203. if (ret != MP_VAL)
  37204. return 9962;
  37205. ret = mp_exptmod_nct(NULL, NULL, a, NULL);
  37206. if (ret != MP_VAL)
  37207. return 9963;
  37208. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  37209. if (ret != MP_VAL)
  37210. return 9964;
  37211. ret = mp_exptmod_nct(a, a, a, NULL);
  37212. if (ret != MP_VAL)
  37213. return 9965;
  37214. ret = mp_exptmod_nct(a, a, NULL, a);
  37215. if (ret != MP_VAL)
  37216. return 9966;
  37217. ret = mp_exptmod_nct(a, NULL, a, a);
  37218. if (ret != MP_VAL)
  37219. return 9967;
  37220. ret = mp_exptmod_nct(NULL, a, a, a);
  37221. if (ret != MP_VAL)
  37222. return 9968;
  37223. #endif
  37224. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  37225. !defined(WC_NO_RNG)
  37226. ret = mp_rand_prime(NULL, 32, NULL, NULL);
  37227. if (ret != MP_VAL)
  37228. return -12787;
  37229. ret = mp_rand_prime(a, 32, NULL, NULL);
  37230. if (ret != MP_VAL)
  37231. return -12788;
  37232. ret = mp_rand_prime(NULL, 32, rng, NULL);
  37233. if (ret != MP_VAL)
  37234. return -12789;
  37235. ret = mp_rand_prime(a, 0, rng, NULL);
  37236. if (ret != MP_VAL)
  37237. return -9969;
  37238. #endif
  37239. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37240. ret = mp_mul(NULL, NULL, NULL);
  37241. if (ret != MP_VAL)
  37242. return -12790;
  37243. ret = mp_mul(a, NULL, NULL);
  37244. if (ret != MP_VAL)
  37245. return -12791;
  37246. ret = mp_mul(NULL, b, NULL);
  37247. if (ret != MP_VAL)
  37248. return -12792;
  37249. ret = mp_mul(NULL, NULL, r);
  37250. if (ret != MP_VAL)
  37251. return -12793;
  37252. ret = mp_mul(a, b, NULL);
  37253. if (ret != MP_VAL)
  37254. return -12794;
  37255. ret = mp_mul(a, NULL, r);
  37256. if (ret != MP_VAL)
  37257. return -12795;
  37258. ret = mp_mul(NULL, b, r);
  37259. if (ret != MP_VAL)
  37260. return -12796;
  37261. #endif
  37262. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  37263. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  37264. ret = mp_sqr(NULL, NULL);
  37265. if (ret != MP_VAL)
  37266. return -12797;
  37267. ret = mp_sqr(a, NULL);
  37268. if (ret != MP_VAL)
  37269. return -12798;
  37270. ret = mp_sqr(NULL, r);
  37271. if (ret != MP_VAL)
  37272. return -12799;
  37273. #endif
  37274. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37275. ret = mp_sqrmod(NULL, NULL, NULL);
  37276. if (ret != MP_VAL)
  37277. return -12800;
  37278. ret = mp_sqrmod(a, NULL, NULL);
  37279. if (ret != MP_VAL)
  37280. return -12801;
  37281. ret = mp_sqrmod(NULL, a, NULL);
  37282. if (ret != MP_VAL)
  37283. return -12802;
  37284. ret = mp_sqrmod(NULL, NULL, a);
  37285. if (ret != MP_VAL)
  37286. return -12803;
  37287. ret = mp_sqrmod(a, b, NULL);
  37288. if (ret != MP_VAL)
  37289. return -12804;
  37290. ret = mp_sqrmod(a, NULL, b);
  37291. if (ret != MP_VAL)
  37292. return -12805;
  37293. ret = mp_sqrmod(NULL, a, b);
  37294. if (ret != MP_VAL)
  37295. return -12806;
  37296. ret = mp_mulmod(NULL, NULL, NULL, NULL);
  37297. if (ret != MP_VAL)
  37298. return -12807;
  37299. ret = mp_mulmod(a, NULL, NULL, NULL);
  37300. if (ret != MP_VAL)
  37301. return -12808;
  37302. ret = mp_mulmod(NULL, a, NULL, NULL);
  37303. if (ret != MP_VAL)
  37304. return -12809;
  37305. ret = mp_mulmod(NULL, NULL, a, NULL);
  37306. if (ret != MP_VAL)
  37307. return -12810;
  37308. ret = mp_mulmod(NULL, NULL, NULL, a);
  37309. if (ret != MP_VAL)
  37310. return -12811;
  37311. ret = mp_mulmod(a, b, b, NULL);
  37312. if (ret != MP_VAL)
  37313. return -12812;
  37314. ret = mp_mulmod(a, b, NULL, a);
  37315. if (ret != MP_VAL)
  37316. return -12813;
  37317. ret = mp_mulmod(a, NULL, b, a);
  37318. if (ret != MP_VAL)
  37319. return -12814;
  37320. ret = mp_mulmod(NULL, b, b, a);
  37321. if (ret != MP_VAL)
  37322. return -12815;
  37323. #endif
  37324. #if !defined(NO_PWDBASED) || defined(WOLFSSL_KEY_GEN) || !defined(NO_DH) || \
  37325. !defined(NO_RSA) || !defined(NO_DSA)
  37326. ret = mp_add_d(NULL, 1, NULL);
  37327. if (ret != MP_VAL)
  37328. return -12816;
  37329. ret = mp_add_d(a, 1, NULL);
  37330. if (ret != MP_VAL)
  37331. return -12817;
  37332. ret = mp_add_d(NULL, 1, b);
  37333. if (ret != MP_VAL)
  37334. return -12818;
  37335. #endif
  37336. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  37337. !defined(NO_DH) || defined(HAVE_ECC) || !defined(NO_DSA)
  37338. ret = mp_sub_d(NULL, 1, NULL);
  37339. if (ret != MP_VAL)
  37340. return -12819;
  37341. ret = mp_sub_d(a, 1, NULL);
  37342. if (ret != MP_VAL)
  37343. return -12820;
  37344. ret = mp_sub_d(NULL, 1, b);
  37345. if (ret != MP_VAL)
  37346. return -12821;
  37347. #endif
  37348. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  37349. defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  37350. ret = mp_div_d(NULL, 0, NULL, NULL);
  37351. if (ret != MP_VAL)
  37352. return -12822;
  37353. ret = mp_div_d(a, 0, NULL, NULL);
  37354. if (ret != MP_VAL)
  37355. return -12823;
  37356. ret = mp_div_d(NULL, 1, NULL, NULL);
  37357. if (ret != MP_VAL)
  37358. return -12824;
  37359. #endif
  37360. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  37361. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  37362. ret = mp_mod_d(NULL, 0, NULL);
  37363. if (ret != MP_VAL)
  37364. return -12825;
  37365. ret = mp_mod_d(a, 0, NULL);
  37366. if (ret != MP_VAL)
  37367. return -12826;
  37368. ret = mp_mod_d(NULL, 0, &rd);
  37369. if (ret != MP_VAL)
  37370. return -12827;
  37371. #endif
  37372. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  37373. ret = mp_gcd(NULL, NULL, NULL);
  37374. if (ret != MP_VAL)
  37375. return -12828;
  37376. ret = mp_gcd(a, NULL, NULL);
  37377. if (ret != MP_VAL)
  37378. return -12829;
  37379. ret = mp_gcd(NULL, a, NULL);
  37380. if (ret != MP_VAL)
  37381. return -12830;
  37382. ret = mp_gcd(NULL, NULL, a);
  37383. if (ret != MP_VAL)
  37384. return -12831;
  37385. ret = mp_gcd(a, b, NULL);
  37386. if (ret != MP_VAL)
  37387. return -12832;
  37388. ret = mp_gcd(a, NULL, b);
  37389. if (ret != MP_VAL)
  37390. return -12833;
  37391. ret = mp_gcd(NULL, a, b);
  37392. if (ret != MP_VAL)
  37393. return -12834;
  37394. #endif
  37395. #if !defined(WOLFSSL_SP_MATH) && defined(HAVE_ECC)
  37396. ret = mp_div_2_mod_ct(NULL, NULL, NULL);
  37397. if (ret != MP_VAL)
  37398. return -12835;
  37399. ret = mp_div_2_mod_ct(a, NULL, NULL);
  37400. if (ret != MP_VAL)
  37401. return -12836;
  37402. ret = mp_div_2_mod_ct(NULL, b, NULL);
  37403. if (ret != MP_VAL)
  37404. return -12837;
  37405. ret = mp_div_2_mod_ct(NULL, NULL, a);
  37406. if (ret != MP_VAL)
  37407. return -12838;
  37408. ret = mp_div_2_mod_ct(a, b, NULL);
  37409. if (ret != MP_VAL)
  37410. return -12839;
  37411. ret = mp_div_2_mod_ct(a, b, NULL);
  37412. if (ret != MP_VAL)
  37413. return -12840;
  37414. ret = mp_div_2_mod_ct(NULL, b, a);
  37415. if (ret != MP_VAL)
  37416. return -12841;
  37417. ret = mp_div_2(NULL, NULL);
  37418. if (ret != MP_VAL)
  37419. return -12842;
  37420. ret = mp_div_2(a, NULL);
  37421. if (ret != MP_VAL)
  37422. return -12843;
  37423. ret = mp_div_2(NULL, a);
  37424. if (ret != MP_VAL)
  37425. return -12844;
  37426. #endif
  37427. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  37428. defined(HAVE_ECC) || !defined(NO_DSA) || defined(OPENSSL_EXTRA)
  37429. ret = mp_invmod(NULL, NULL, NULL);
  37430. if (ret != MP_VAL)
  37431. return -12845;
  37432. ret = mp_invmod(a, NULL, NULL);
  37433. if (ret != MP_VAL)
  37434. return -12846;
  37435. ret = mp_invmod(NULL, b, NULL);
  37436. if (ret != MP_VAL)
  37437. return -12847;
  37438. ret = mp_invmod(NULL, NULL, a);
  37439. if (ret != MP_VAL)
  37440. return -12848;
  37441. ret = mp_invmod(a, b, NULL);
  37442. if (ret != MP_VAL)
  37443. return -12849;
  37444. ret = mp_invmod(a, NULL, a);
  37445. if (ret != MP_VAL)
  37446. return -12850;
  37447. ret = mp_invmod(NULL, b, a);
  37448. if (ret != MP_VAL)
  37449. return -12851;
  37450. #endif
  37451. #if !defined(WOLFSSL_SP_MATH) && defined(HAVE_ECC)
  37452. ret = mp_invmod_mont_ct(NULL, NULL, NULL, 1);
  37453. if (ret != MP_VAL)
  37454. return -12852;
  37455. ret = mp_invmod_mont_ct(a, NULL, NULL, 1);
  37456. if (ret != MP_VAL)
  37457. return -12853;
  37458. ret = mp_invmod_mont_ct(NULL, b, NULL, 1);
  37459. if (ret != MP_VAL)
  37460. return -12854;
  37461. ret = mp_invmod_mont_ct(NULL, NULL, a, 1);
  37462. if (ret != MP_VAL)
  37463. return -12855;
  37464. ret = mp_invmod_mont_ct(a, b, NULL, 1);
  37465. if (ret != MP_VAL)
  37466. return -12856;
  37467. ret = mp_invmod_mont_ct(a, NULL, a, 1);
  37468. if (ret != MP_VAL)
  37469. return -12857;
  37470. ret = mp_invmod_mont_ct(NULL, b, a, 1);
  37471. if (ret != MP_VAL)
  37472. return -12858;
  37473. #endif
  37474. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  37475. ret = mp_lcm(NULL, NULL, NULL);
  37476. if (ret != MP_VAL)
  37477. return -12859;
  37478. ret = mp_lcm(a, NULL, NULL);
  37479. if (ret != MP_VAL)
  37480. return -12860;
  37481. ret = mp_lcm(NULL, b, NULL);
  37482. if (ret != MP_VAL)
  37483. return -12861;
  37484. ret = mp_lcm(NULL, NULL, a);
  37485. if (ret != MP_VAL)
  37486. return -12862;
  37487. ret = mp_lcm(a, b, NULL);
  37488. if (ret != MP_VAL)
  37489. return -12863;
  37490. ret = mp_lcm(a, NULL, a);
  37491. if (ret != MP_VAL)
  37492. return -12864;
  37493. ret = mp_lcm(NULL, b, a);
  37494. if (ret != MP_VAL)
  37495. return -12865;
  37496. #endif
  37497. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  37498. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, NULL);
  37499. if (ret != MP_VAL)
  37500. return -12866;
  37501. ret = mp_exptmod_ex(a, NULL, 1, NULL, NULL);
  37502. if (ret != MP_VAL)
  37503. return -12867;
  37504. ret = mp_exptmod_ex(NULL, b, 1, NULL, NULL);
  37505. if (ret != MP_VAL)
  37506. return -12868;
  37507. ret = mp_exptmod_ex(NULL, NULL, 1, b, NULL);
  37508. if (ret != MP_VAL)
  37509. return -12869;
  37510. ret = mp_exptmod_ex(NULL, NULL, 1, NULL, a);
  37511. if (ret != MP_VAL)
  37512. return -12870;
  37513. ret = mp_exptmod_ex(a, b, 1, b, NULL);
  37514. if (ret != MP_VAL)
  37515. return -12871;
  37516. ret = mp_exptmod_ex(a, b, 1, NULL, a);
  37517. if (ret != MP_VAL)
  37518. return -12872;
  37519. ret = mp_exptmod_ex(a, NULL, 1, b, a);
  37520. if (ret != MP_VAL)
  37521. return -12873;
  37522. ret = mp_exptmod_ex(NULL, b, 1, b, a);
  37523. if (ret != MP_VAL)
  37524. return -12874;
  37525. ret = mp_exptmod(NULL, NULL, NULL, NULL);
  37526. if (ret != MP_VAL)
  37527. return -12875;
  37528. ret = mp_exptmod(a, NULL, NULL, NULL);
  37529. if (ret != MP_VAL)
  37530. return -12876;
  37531. ret = mp_exptmod(NULL, b, NULL, NULL);
  37532. if (ret != MP_VAL)
  37533. return -12877;
  37534. ret = mp_exptmod(NULL, NULL, b, NULL);
  37535. if (ret != MP_VAL)
  37536. return -12878;
  37537. ret = mp_exptmod(NULL, NULL, NULL, a);
  37538. if (ret != MP_VAL)
  37539. return -12879;
  37540. ret = mp_exptmod(a, b, b, NULL);
  37541. if (ret != MP_VAL)
  37542. return -12880;
  37543. ret = mp_exptmod(a, b, NULL, a);
  37544. if (ret != MP_VAL)
  37545. return -12881;
  37546. ret = mp_exptmod(a, NULL, b, a);
  37547. if (ret != MP_VAL)
  37548. return -12882;
  37549. ret = mp_exptmod(NULL, b, b, a);
  37550. if (ret != MP_VAL)
  37551. return -12883;
  37552. ret = mp_exptmod_nct(NULL, NULL, NULL, NULL);
  37553. if (ret != MP_VAL)
  37554. return -12884;
  37555. ret = mp_exptmod_nct(a, NULL, NULL, NULL);
  37556. if (ret != MP_VAL)
  37557. return -12885;
  37558. ret = mp_exptmod_nct(NULL, b, NULL, NULL);
  37559. if (ret != MP_VAL)
  37560. return -12886;
  37561. ret = mp_exptmod_nct(NULL, NULL, b, NULL);
  37562. if (ret != MP_VAL)
  37563. return -12887;
  37564. ret = mp_exptmod_nct(NULL, NULL, NULL, a);
  37565. if (ret != MP_VAL)
  37566. return -12888;
  37567. ret = mp_exptmod_nct(a, b, b, NULL);
  37568. if (ret != MP_VAL)
  37569. return -12889;
  37570. ret = mp_exptmod_nct(a, b, NULL, a);
  37571. if (ret != MP_VAL)
  37572. return -12890;
  37573. ret = mp_exptmod_nct(a, NULL, b, a);
  37574. if (ret != MP_VAL)
  37575. return -12891;
  37576. ret = mp_exptmod_nct(NULL, b, b, a);
  37577. if (ret != MP_VAL)
  37578. return -12892;
  37579. #endif
  37580. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  37581. ret = mp_cnt_lsb(NULL);
  37582. if (ret != 0)
  37583. return -12893;
  37584. #endif
  37585. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH)
  37586. ret = mp_prime_is_prime(NULL, 1, NULL);
  37587. if (ret != MP_VAL)
  37588. return -12894;
  37589. ret = mp_prime_is_prime(a, 1, NULL);
  37590. if (ret != MP_VAL)
  37591. return -12895;
  37592. ret = mp_prime_is_prime(NULL, 1, &result);
  37593. if (ret != MP_VAL)
  37594. return -12896;
  37595. ret = mp_prime_is_prime(a, 0, &result);
  37596. if (ret != MP_VAL)
  37597. return -12897;
  37598. ret = mp_prime_is_prime(a, 1024, &result);
  37599. if (ret != MP_VAL)
  37600. return -12898;
  37601. ret = mp_prime_is_prime_ex(NULL, 1, NULL, NULL);
  37602. if (ret != MP_VAL)
  37603. return -12899;
  37604. ret = mp_prime_is_prime_ex(a, 1, NULL, NULL);
  37605. if (ret != MP_VAL)
  37606. return -12900;
  37607. ret = mp_prime_is_prime_ex(NULL, 1, &result, NULL);
  37608. if (ret != MP_VAL)
  37609. return -12901;
  37610. ret = mp_prime_is_prime_ex(NULL, 1, NULL, rng);
  37611. if (ret != MP_VAL)
  37612. return -12902;
  37613. ret = mp_prime_is_prime_ex(a, 1, &result, NULL);
  37614. if (ret != MP_VAL)
  37615. return -12903;
  37616. ret = mp_prime_is_prime_ex(a, 1, NULL, rng);
  37617. if (ret != MP_VAL)
  37618. return -12904;
  37619. ret = mp_prime_is_prime_ex(NULL, 1, &result, rng);
  37620. if (ret != MP_VAL)
  37621. return -12905;
  37622. #endif
  37623. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || !defined(NO_DSA)
  37624. ret = mp_exch(NULL, NULL);
  37625. if (ret != MP_VAL)
  37626. return -12906;
  37627. ret = mp_exch(a, NULL);
  37628. if (ret != MP_VAL)
  37629. return -12907;
  37630. ret = mp_exch(NULL, b);
  37631. if (ret != MP_VAL)
  37632. return -12908;
  37633. #endif
  37634. #if (defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)) || \
  37635. defined(WOLFSSL_SP_MATH_ALL)
  37636. ret = mp_mul_d(NULL, 1, NULL);
  37637. if (ret != MP_VAL)
  37638. return -12909;
  37639. ret = mp_mul_d(a, 1, NULL);
  37640. if (ret != MP_VAL)
  37641. return -12910;
  37642. ret = mp_mul_d(NULL, 1, b);
  37643. if (ret != MP_VAL)
  37644. return -12911;
  37645. #endif
  37646. #if !defined(WOLFSSL_RSA_VERIFY_ONLY)
  37647. ret = mp_add(NULL, NULL, NULL);
  37648. if (ret != MP_VAL)
  37649. return -12912;
  37650. ret = mp_add(a, NULL, NULL);
  37651. if (ret != MP_VAL)
  37652. return -12913;
  37653. ret = mp_add(NULL, b, NULL);
  37654. if (ret != MP_VAL)
  37655. return -12914;
  37656. ret = mp_add(NULL, NULL, r);
  37657. if (ret != MP_VAL)
  37658. return -12915;
  37659. ret = mp_add(a, b, NULL);
  37660. if (ret != MP_VAL)
  37661. return -12916;
  37662. ret = mp_add(a, NULL, r);
  37663. if (ret != MP_VAL)
  37664. return -12917;
  37665. ret = mp_add(NULL, b, r);
  37666. if (ret != MP_VAL)
  37667. return -12918;
  37668. #endif
  37669. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
  37670. (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  37671. ret = mp_sub(NULL, NULL, NULL);
  37672. if (ret != MP_VAL)
  37673. return -12919;
  37674. ret = mp_sub(a, NULL, NULL);
  37675. if (ret != MP_VAL)
  37676. return -12920;
  37677. ret = mp_sub(NULL, b, NULL);
  37678. if (ret != MP_VAL)
  37679. return -12921;
  37680. ret = mp_sub(NULL, NULL, r);
  37681. if (ret != MP_VAL)
  37682. return -12922;
  37683. ret = mp_sub(a, b, NULL);
  37684. if (ret != MP_VAL)
  37685. return -12923;
  37686. ret = mp_sub(a, NULL, r);
  37687. if (ret != MP_VAL)
  37688. return -12924;
  37689. ret = mp_sub(NULL, b, r);
  37690. if (ret != MP_VAL)
  37691. return -12925;
  37692. #endif
  37693. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined(WOLFSSL_SP_MATH) && \
  37694. defined(WOLFSSL_CUSTOM_CURVES))
  37695. ret = mp_addmod(NULL, NULL, NULL, NULL);
  37696. if (ret != MP_VAL)
  37697. return -12926;
  37698. ret = mp_addmod(a, NULL, NULL, NULL);
  37699. if (ret != MP_VAL)
  37700. return -12927;
  37701. ret = mp_addmod(NULL, b, NULL, NULL);
  37702. if (ret != MP_VAL)
  37703. return -12928;
  37704. ret = mp_addmod(NULL, NULL, b, NULL);
  37705. if (ret != MP_VAL)
  37706. return -12929;
  37707. ret = mp_addmod(NULL, NULL, NULL, a);
  37708. if (ret != MP_VAL)
  37709. return -12930;
  37710. ret = mp_addmod(a, b, b, NULL);
  37711. if (ret != MP_VAL)
  37712. return -12931;
  37713. ret = mp_addmod(a, b, NULL, a);
  37714. if (ret != MP_VAL)
  37715. return -12932;
  37716. ret = mp_addmod(a, NULL, b, a);
  37717. if (ret != MP_VAL)
  37718. return -12933;
  37719. ret = mp_addmod(NULL, b, b, a);
  37720. if (ret != MP_VAL)
  37721. return -12934;
  37722. #endif
  37723. #ifdef WOLFSSL_SP_MATH_ALL
  37724. ret = mp_submod(NULL, NULL, NULL, NULL);
  37725. if (ret != MP_VAL)
  37726. return -12935;
  37727. ret = mp_submod(a, NULL, NULL, NULL);
  37728. if (ret != MP_VAL)
  37729. return -12936;
  37730. ret = mp_submod(NULL, b, NULL, NULL);
  37731. if (ret != MP_VAL)
  37732. return -12937;
  37733. ret = mp_submod(NULL, NULL, b, NULL);
  37734. if (ret != MP_VAL)
  37735. return -12938;
  37736. ret = mp_submod(NULL, NULL, NULL, a);
  37737. if (ret != MP_VAL)
  37738. return -12939;
  37739. ret = mp_submod(a, b, b, NULL);
  37740. if (ret != MP_VAL)
  37741. return -12940;
  37742. ret = mp_submod(a, b, NULL, a);
  37743. if (ret != MP_VAL)
  37744. return -12941;
  37745. ret = mp_submod(a, NULL, b, a);
  37746. if (ret != MP_VAL)
  37747. return -12942;
  37748. ret = mp_submod(NULL, b, b, a);
  37749. if (ret != MP_VAL)
  37750. return -12943;
  37751. #endif
  37752. #ifdef WOLFSSL_SP_MATH_ALL
  37753. ret = mp_div_2d(NULL, 1, a, b);
  37754. if (ret != MP_VAL)
  37755. return -12944;
  37756. ret = mp_mod_2d(NULL, 1, NULL);
  37757. if (ret != MP_VAL)
  37758. return -12945;
  37759. ret = mp_mod_2d(a, 1, NULL);
  37760. if (ret != MP_VAL)
  37761. return -12946;
  37762. ret = mp_mod_2d(NULL, 1, b);
  37763. if (ret != MP_VAL)
  37764. return -12947;
  37765. ret = mp_mul_2d(NULL, 1, NULL);
  37766. if (ret != MP_VAL)
  37767. return -12948;
  37768. ret = mp_mul_2d(a, 1, NULL);
  37769. if (ret != MP_VAL)
  37770. return -12949;
  37771. ret = mp_mul_2d(NULL, 1, b);
  37772. if (ret != MP_VAL)
  37773. return -12950;
  37774. #endif
  37775. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  37776. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  37777. ret = mp_montgomery_reduce(NULL, NULL, 1);
  37778. if (ret != MP_VAL)
  37779. return -12951;
  37780. ret = mp_montgomery_reduce(a, NULL, 1);
  37781. if (ret != MP_VAL)
  37782. return -12952;
  37783. ret = mp_montgomery_reduce(NULL, b, 1);
  37784. if (ret != MP_VAL)
  37785. return -12953;
  37786. mp_zero(b);
  37787. ret = mp_montgomery_reduce(a, b, 1);
  37788. if (ret != MP_VAL)
  37789. return -12954;
  37790. #endif
  37791. #ifdef WOLFSSL_SP_MATH_ALL
  37792. ret = mp_montgomery_setup(NULL, NULL);
  37793. if (ret != MP_VAL)
  37794. return -12955;
  37795. ret = mp_montgomery_setup(a, NULL);
  37796. if (ret != MP_VAL)
  37797. return -12956;
  37798. ret = mp_montgomery_setup(NULL, &rho);
  37799. if (ret != MP_VAL)
  37800. return -12957;
  37801. ret = mp_montgomery_calc_normalization(NULL, NULL);
  37802. if (ret != MP_VAL)
  37803. return -12958;
  37804. ret = mp_montgomery_calc_normalization(a, NULL);
  37805. if (ret != MP_VAL)
  37806. return -12959;
  37807. ret = mp_montgomery_calc_normalization(NULL, b);
  37808. if (ret != MP_VAL)
  37809. return -12960;
  37810. #endif
  37811. ret = mp_unsigned_bin_size(NULL);
  37812. if (ret != 0)
  37813. return -12961;
  37814. #if defined(WC_MP_TO_RADIX) || defined(WOLFSSL_SP_MATH_ALL)
  37815. ret = mp_tohex(NULL, NULL);
  37816. if (ret != MP_VAL)
  37817. return -12962;
  37818. ret = mp_tohex(a, NULL);
  37819. if (ret != MP_VAL)
  37820. return -12963;
  37821. ret = mp_tohex(NULL, hexStr);
  37822. if (ret != MP_VAL)
  37823. return -12964;
  37824. #endif
  37825. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  37826. ret = mp_todecimal(NULL, NULL);
  37827. if (ret != MP_VAL)
  37828. return -12965;
  37829. ret = mp_todecimal(a, NULL);
  37830. if (ret != MP_VAL)
  37831. return -12966;
  37832. ret = mp_todecimal(NULL, decStr);
  37833. if (ret != MP_VAL)
  37834. return -12967;
  37835. #endif
  37836. #ifdef WOLFSSL_SP_MATH_ALL
  37837. ret = mp_toradix(NULL, NULL, MP_RADIX_HEX);
  37838. if (ret != MP_VAL)
  37839. return -12968;
  37840. ret = mp_toradix(a, NULL, MP_RADIX_HEX);
  37841. if (ret != MP_VAL)
  37842. return -12969;
  37843. ret = mp_toradix(NULL, hexStr, MP_RADIX_HEX);
  37844. if (ret != MP_VAL)
  37845. return -12970;
  37846. ret = mp_toradix(a, hexStr, 3);
  37847. if (ret != MP_VAL)
  37848. return -12971;
  37849. ret = mp_radix_size(NULL, MP_RADIX_HEX, NULL);
  37850. if (ret != MP_VAL)
  37851. return -12972;
  37852. ret = mp_radix_size(a, MP_RADIX_HEX, NULL);
  37853. if (ret != MP_VAL)
  37854. return -12973;
  37855. ret = mp_radix_size(NULL, MP_RADIX_HEX, &size);
  37856. if (ret != MP_VAL)
  37857. return -12974;
  37858. ret = mp_radix_size(a, 3, &size);
  37859. if (ret != MP_VAL)
  37860. return -12975;
  37861. #endif
  37862. return 0;
  37863. }
  37864. #endif
  37865. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  37866. static int mp_test_set_is_bit(mp_int* a)
  37867. {
  37868. int i, j;
  37869. mp_zero(a);
  37870. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  37871. if (mp_is_bit_set(a, i))
  37872. return -12980;
  37873. for (j = 0; j < i; j++) {
  37874. if (!mp_is_bit_set(a, j))
  37875. return -12981;
  37876. }
  37877. if (mp_set_bit(a, i) != 0)
  37878. return -12982;
  37879. if (!mp_is_bit_set(a, i))
  37880. return -12983;
  37881. }
  37882. mp_zero(a);
  37883. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  37884. if (mp_is_bit_set(a, i))
  37885. return -12984;
  37886. }
  37887. for (i = 0; i <= DIGIT_BIT * 2; i++) {
  37888. mp_zero(a);
  37889. if (mp_set_bit(a, i) != 0)
  37890. return -12985;
  37891. for (j = 0; j < i; j++) {
  37892. if (mp_is_bit_set(a, j))
  37893. return -12986;
  37894. }
  37895. if (!mp_is_bit_set(a, i))
  37896. return -12987;
  37897. }
  37898. #ifdef WOLFSSL_KEY_GEN
  37899. for (i = 0; i < DIGIT_BIT * 2; i++) {
  37900. mp_set(a, 1);
  37901. if (mp_2expt(a, i) != 0)
  37902. return -12988;
  37903. for (j = 0; j < i; j++) {
  37904. if (mp_is_bit_set(a, j))
  37905. return -12989;
  37906. }
  37907. if (!mp_is_bit_set(a, i))
  37908. return -12990;
  37909. }
  37910. #endif
  37911. #ifdef WOLFSSL_SP_MATH
  37912. mp_zero(a);
  37913. for (j = 1; j <= 3; j++) {
  37914. i = SP_INT_MAX_BITS - j;
  37915. if (mp_is_bit_set(a, i))
  37916. return -12991;
  37917. if (mp_set_bit(a, i) != 0)
  37918. return -12992;
  37919. if (!mp_is_bit_set(a, i))
  37920. return -12993;
  37921. #ifdef WOLFSSL_KEY_GEN
  37922. if (mp_2expt(a, i) != 0)
  37923. return -12994;
  37924. if (!mp_is_bit_set(a, i))
  37925. return -12995;
  37926. #endif
  37927. }
  37928. mp_zero(a);
  37929. for (j = 0; j <= 3; j++) {
  37930. i = SP_INT_MAX_BITS + j;
  37931. if (mp_is_bit_set(a, i))
  37932. return -12996;
  37933. if (mp_set_bit(a, i) != MP_VAL)
  37934. return -12997;
  37935. #ifdef WOLFSSL_KEY_GEN
  37936. if (mp_2expt(a, i) != MP_VAL)
  37937. return -12998;
  37938. #endif
  37939. }
  37940. #endif
  37941. return 0;
  37942. }
  37943. #endif /* !WOLFSSL_SP_MATH || WOLFSSL_SP_MATH_ALL */
  37944. static int mp_test_cmp(mp_int* a, mp_int* b)
  37945. {
  37946. int ret;
  37947. mp_zero(a);
  37948. mp_zero(b);
  37949. ret = mp_cmp_d(a, 0);
  37950. if (ret != MP_EQ)
  37951. return -13000;
  37952. ret = mp_cmp_d(a, 1);
  37953. if (ret != MP_LT)
  37954. return -13001;
  37955. ret = mp_cmp(a, b);
  37956. if (ret != MP_EQ)
  37957. return -13002;
  37958. mp_set(a, 1);
  37959. ret = mp_cmp_d(a, 0);
  37960. if (ret != MP_GT)
  37961. return -13003;
  37962. ret = mp_cmp_d(a, 1);
  37963. if (ret != MP_EQ)
  37964. return -13004;
  37965. ret = mp_cmp_d(a, 2);
  37966. if (ret != MP_LT)
  37967. return -13005;
  37968. ret = mp_cmp(a, b);
  37969. if (ret != MP_GT)
  37970. return -13006;
  37971. mp_read_radix(b, "1234567890123456789", MP_RADIX_HEX);
  37972. ret = mp_cmp_d(b, -1);
  37973. if (ret != MP_GT)
  37974. return -13007;
  37975. ret = mp_cmp(a, b);
  37976. if (ret != MP_LT)
  37977. return -13008;
  37978. ret = mp_cmp(b, a);
  37979. if (ret != MP_GT)
  37980. return -13009;
  37981. ret = mp_cmp(b, b);
  37982. if (ret != MP_EQ)
  37983. return -13010;
  37984. #if (!defined(WOLFSSL_SP_MATH) && !defined(WOLFSSL_SP_MATH_ALL)) || \
  37985. defined(WOLFSSL_SP_INT_NEGATIVE)
  37986. mp_read_radix(a, "-1", MP_RADIX_HEX);
  37987. mp_read_radix(a, "1", MP_RADIX_HEX);
  37988. ret = mp_cmp(a, b);
  37989. if (ret != MP_LT)
  37990. return -13011;
  37991. ret = mp_cmp(b, a);
  37992. if (ret != MP_GT)
  37993. return -13012;
  37994. mp_read_radix(b, "-2", MP_RADIX_HEX);
  37995. ret = mp_cmp(a, b);
  37996. if (ret != MP_GT)
  37997. return -13013;
  37998. ret = mp_cmp(b, a);
  37999. if (ret != MP_LT)
  38000. return -13014;
  38001. mp_read_radix(a, "-2", MP_RADIX_HEX);
  38002. ret = mp_cmp(a, b);
  38003. if (ret != MP_EQ)
  38004. return -13015;
  38005. #endif
  38006. return 0;
  38007. }
  38008. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  38009. static int mp_test_shbd(mp_int* a, mp_int* b, WC_RNG* rng)
  38010. {
  38011. int ret;
  38012. int i, j, k;
  38013. #ifndef WOLFSSL_SP_MATH
  38014. for (i = 0; i < 10; i++) {
  38015. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  38016. ret = randNum(a, j, rng, NULL);
  38017. if (ret != MP_OKAY)
  38018. return -13020;
  38019. mp_copy(a, b);
  38020. for (k = 0; k <= DIGIT_BIT * 2; k++) {
  38021. ret = mp_mul_2d(a, k, a);
  38022. if (ret != MP_OKAY)
  38023. return -13021;
  38024. mp_rshb(a, k);
  38025. if (mp_cmp(a, b) != MP_EQ)
  38026. return -13022;
  38027. }
  38028. }
  38029. }
  38030. #endif
  38031. for (i = 0; i < 10; i++) {
  38032. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 3; j++) {
  38033. ret = randNum(a, j, rng, NULL);
  38034. if (ret != MP_OKAY)
  38035. return -13023;
  38036. mp_copy(a, b);
  38037. for (k = 0; k < 10; k++) {
  38038. ret = mp_lshd(a, k);
  38039. if (ret != MP_OKAY)
  38040. return -13024;
  38041. mp_rshd(a, k);
  38042. if (mp_cmp(a, b) != MP_EQ)
  38043. return -13025;
  38044. }
  38045. }
  38046. }
  38047. mp_zero(a);
  38048. mp_rshd(a, 1);
  38049. if (!mp_iszero(a))
  38050. return -13026;
  38051. mp_set(a, 1);
  38052. mp_rshd(a, 1);
  38053. if (!mp_iszero(a))
  38054. return -13027;
  38055. mp_set(a, 1);
  38056. mp_rshd(a, 2);
  38057. if (!mp_iszero(a))
  38058. return -13028;
  38059. return 0;
  38060. }
  38061. #endif
  38062. #if !defined(WOLFSSL_SP_MATH) && !defined(WOLFSSL_SP_MATH_ALL)
  38063. static int mp_test_div(mp_int* a, mp_int* d, mp_int* r, mp_int* rem,
  38064. WC_RNG* rng)
  38065. {
  38066. int ret;
  38067. int i, j, k;
  38068. mp_zero(a);
  38069. mp_zero(d);
  38070. ret = mp_div(a, d, r, rem);
  38071. if (ret != MP_VAL)
  38072. return -13030;
  38073. mp_set(d, 1);
  38074. ret = mp_div(a, d, r, rem);
  38075. if (ret != MP_OKAY)
  38076. return -13031;
  38077. if (!mp_iszero(r))
  38078. return -13032;
  38079. if (!mp_iszero(rem))
  38080. return -13033;
  38081. mp_set(a, 1);
  38082. ret = mp_div(a, d, r, rem);
  38083. if (ret != MP_OKAY)
  38084. return -13034;
  38085. if (!mp_isone(r))
  38086. return -13035;
  38087. if (!mp_iszero(rem))
  38088. return -13036;
  38089. for (i = 0; i < 100; i++) {
  38090. for (j = 1; j < (DIGIT_BIT + 7) / 8 * 2; j++) {
  38091. ret = randNum(d, j, rng, NULL);
  38092. if (ret != MP_OKAY)
  38093. return -13037;
  38094. for (k = 1; k < (DIGIT_BIT + 7) / 8 * 2 + 1; k++) {
  38095. ret = randNum(a, k, rng, NULL);
  38096. if (ret != MP_OKAY)
  38097. return -13038;
  38098. ret = mp_div(a, d, NULL, rem);
  38099. if (ret != MP_OKAY)
  38100. return -13039;
  38101. ret = mp_div(a, d, r, NULL);
  38102. if (ret != MP_OKAY)
  38103. return -13040;
  38104. ret = mp_div(a, d, r, rem);
  38105. if (ret != MP_OKAY)
  38106. return -13041;
  38107. mp_mul(r, d, r);
  38108. mp_add(r, rem, r);
  38109. if (mp_cmp(r, a) != MP_EQ)
  38110. return -13042;
  38111. }
  38112. }
  38113. }
  38114. ret = randNum(d, (DIGIT_BIT + 7) / 8 * 2, rng, NULL);
  38115. if (ret != MP_OKAY)
  38116. return -13043;
  38117. mp_add(d, d, a);
  38118. mp_set(rem, 1);
  38119. mp_div(a, d, NULL, rem);
  38120. if (ret != MP_OKAY)
  38121. return -13044;
  38122. if (!mp_iszero(rem))
  38123. return -13045;
  38124. mp_set(r, 1);
  38125. mp_div(a, d, r, NULL);
  38126. if (ret != MP_OKAY)
  38127. return -13046;
  38128. if (mp_cmp_d(r, 2) != MP_EQ)
  38129. return -13047;
  38130. mp_set(r, 1);
  38131. mp_set(rem, 1);
  38132. mp_div(a, d, r, rem);
  38133. if (ret != MP_OKAY)
  38134. return -13048;
  38135. if (mp_cmp_d(r, 2) != MP_EQ)
  38136. return -13049;
  38137. if (!mp_iszero(rem))
  38138. return -13050;
  38139. mp_set(a, 0xfe);
  38140. mp_lshd(a, 3);
  38141. mp_add_d(a, 0xff, a);
  38142. mp_set(d, 0xfe);
  38143. mp_lshd(d, 2);
  38144. ret = mp_div(a, d, r, rem);
  38145. if (ret != MP_OKAY)
  38146. return -13051;
  38147. mp_mul(r, d, d);
  38148. mp_add(rem, d, d);
  38149. if (mp_cmp(a, d) != MP_EQ)
  38150. return -13052;
  38151. /* Force (hi | lo) / d to be (d | 0) / d which will would not fit in
  38152. * a digit. So mp_div must detect and handle.
  38153. * For example: 0x800000 / 0x8001, DIGIT_BIT = 8
  38154. */
  38155. mp_set(a, 1);
  38156. mp_mul_2d(a, DIGIT_BIT * 3 - 1, a);
  38157. mp_set(d, 1);
  38158. mp_mul_2d(d, DIGIT_BIT * 2 - 1, d);
  38159. mp_add_d(d, 1, d);
  38160. ret = mp_div(a, d, r, rem);
  38161. if (ret != MP_OKAY)
  38162. return -13053;
  38163. return 0;
  38164. }
  38165. #endif
  38166. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  38167. !defined(WC_NO_RNG)
  38168. static int mp_test_prime(mp_int* a, WC_RNG* rng)
  38169. {
  38170. int ret;
  38171. int res;
  38172. ret = mp_rand_prime(a, 1, rng, NULL);
  38173. #if defined(WOLFSSL_SP_MATH_ALL)
  38174. if (ret != 0)
  38175. return -13060;
  38176. #else
  38177. if (ret != MP_VAL)
  38178. return -13060;
  38179. #endif
  38180. #ifndef WOLFSSL_SP_MATH
  38181. ret = mp_rand_prime(a, -5, rng, NULL);
  38182. if (ret != 0)
  38183. return -13061;
  38184. #endif
  38185. ret = mp_prime_is_prime(a, 1, &res);
  38186. if (ret != MP_OKAY)
  38187. return -13062;
  38188. #ifndef WOLFSSL_SP_MATH
  38189. if (res != MP_YES)
  38190. return -13063;
  38191. #else
  38192. if (res != MP_NO)
  38193. return -13063;
  38194. #endif
  38195. ret = mp_prime_is_prime(a, 0, &res);
  38196. if (ret != MP_VAL)
  38197. return -13064;
  38198. ret = mp_prime_is_prime(a, -1, &res);
  38199. if (ret != MP_VAL)
  38200. return -13065;
  38201. ret = mp_prime_is_prime(a, 257, &res);
  38202. if (ret != MP_VAL)
  38203. return -13066;
  38204. mp_set(a, 1);
  38205. ret = mp_prime_is_prime(a, 1, &res);
  38206. if (ret != MP_OKAY)
  38207. return -13067;
  38208. if (res != MP_NO)
  38209. return -13068;
  38210. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  38211. if (ret != MP_OKAY)
  38212. return -13069;
  38213. if (res != MP_NO)
  38214. return -13070;
  38215. mp_set(a, 2);
  38216. ret = mp_prime_is_prime(a, 1, &res);
  38217. if (ret != MP_OKAY)
  38218. return -13071;
  38219. if (res != MP_YES)
  38220. return -13072;
  38221. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  38222. if (ret != MP_OKAY)
  38223. return -13073;
  38224. if (res != MP_YES)
  38225. return -13074;
  38226. mp_set(a, 0xfb);
  38227. ret = mp_prime_is_prime(a, 1, &res);
  38228. if (ret != MP_OKAY)
  38229. return -13075;
  38230. if (res != MP_YES)
  38231. return -13076;
  38232. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  38233. if (ret != MP_OKAY)
  38234. return -13077;
  38235. if (res != MP_YES)
  38236. return -13078;
  38237. mp_set(a, 0x6);
  38238. ret = mp_prime_is_prime(a, 1, &res);
  38239. if (ret != MP_OKAY)
  38240. return -13079;
  38241. if (res != MP_NO)
  38242. return -13080;
  38243. ret = mp_prime_is_prime_ex(a, 1, &res, rng);
  38244. if (ret != MP_OKAY)
  38245. return -13081;
  38246. if (res != MP_NO)
  38247. return -13082;
  38248. mp_set_int(a, 0x655 * 0x65b);
  38249. ret = mp_prime_is_prime(a, 10, &res);
  38250. if (ret != MP_OKAY)
  38251. return -13083;
  38252. if (res != MP_NO)
  38253. return -13084;
  38254. ret = mp_prime_is_prime_ex(a, 10, &res, rng);
  38255. if (ret != MP_OKAY)
  38256. return -13085;
  38257. if (res != MP_NO)
  38258. return -13086;
  38259. return 0;
  38260. }
  38261. #endif
  38262. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  38263. static int mp_test_lcm_gcd(mp_int* a, mp_int* b, mp_int* r, mp_int* exp,
  38264. WC_RNG* rng)
  38265. {
  38266. int ret;
  38267. int i;
  38268. WOLFSSL_SMALL_STACK_STATIC const int kat[][3] = {
  38269. { 1, 1, 1 }, { 2, 1, 2 }, { 1, 2, 2 }, { 2, 4, 4 }, { 4, 2, 4 },
  38270. { 12, 56, 168 }, { 56, 12, 168 }
  38271. };
  38272. (void)exp;
  38273. mp_set(a, 0);
  38274. mp_set(b, 1);
  38275. ret = mp_lcm(a, a, r);
  38276. if (ret != MP_VAL)
  38277. return -13090;
  38278. ret = mp_lcm(a, b, r);
  38279. if (ret != MP_VAL)
  38280. return -13091;
  38281. ret = mp_lcm(b, a, r);
  38282. if (ret != MP_VAL)
  38283. return -13092;
  38284. for (i = 0; i < (int)(sizeof(kat) / sizeof(*kat)); i++) {
  38285. mp_set(a, kat[i][0]);
  38286. mp_set(b, kat[i][1]);
  38287. ret = mp_lcm(a, b, r);
  38288. if (ret != MP_OKAY)
  38289. return -13093;
  38290. mp_set(exp, kat[i][2]);
  38291. if (mp_cmp(r, exp) != MP_EQ)
  38292. return -13094;
  38293. }
  38294. (void)rng;
  38295. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  38296. !defined(WC_NO_RNG)
  38297. if (mp_rand_prime(a, 20, rng, NULL) != MP_OKAY)
  38298. return -13095;
  38299. if (mp_rand_prime(b, 20, rng, NULL) != MP_OKAY)
  38300. return -13096;
  38301. if (mp_mul(a, b, exp) != MP_OKAY)
  38302. return -13097;
  38303. ret = mp_lcm(a, b, r);
  38304. if (ret != MP_OKAY)
  38305. return -13098;
  38306. if (mp_cmp(r, exp) != MP_EQ)
  38307. return -13099;
  38308. ret = mp_lcm(b, a, r);
  38309. if (ret != MP_OKAY)
  38310. return -13100;
  38311. if (mp_cmp(r, exp) != MP_EQ)
  38312. return -13101;
  38313. #endif
  38314. mp_set(a, 11);
  38315. mp_zero(b);
  38316. ret = mp_gcd(a, b, r);
  38317. if (ret != MP_OKAY)
  38318. return -13102;
  38319. if (mp_cmp_d(r, 11) != MP_EQ)
  38320. return -13103;
  38321. ret = mp_gcd(b, a, r);
  38322. if (ret != MP_OKAY)
  38323. return -13104;
  38324. if (mp_cmp_d(r, 11) != MP_EQ)
  38325. return -13105;
  38326. ret = mp_gcd(b, b, r);
  38327. if (ret != MP_VAL)
  38328. return -13106;
  38329. return 0;
  38330. }
  38331. #endif
  38332. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  38333. defined(WOLFSSL_SP_MATH_ALL)
  38334. static int mp_test_mod_2d(mp_int* a, mp_int* r, mp_int* t, WC_RNG* rng)
  38335. {
  38336. int ret;
  38337. int i;
  38338. int j;
  38339. mp_set(a, 10);
  38340. ret = mp_mod_2d(a, 0, r);
  38341. if (ret != MP_OKAY)
  38342. return -13110;
  38343. if (!mp_iszero(r))
  38344. return -13111;
  38345. ret = mp_mod_2d(a, 1, r);
  38346. if (ret != MP_OKAY)
  38347. return -13112;
  38348. if (!mp_iszero(r))
  38349. return -13113;
  38350. ret = mp_mod_2d(a, 2, r);
  38351. if (ret != MP_OKAY)
  38352. return -13114;
  38353. if (mp_cmp_d(r, 2))
  38354. return -13115;
  38355. for (i = 2; i < 20; i++) {
  38356. ret = randNum(a, i, rng, NULL);
  38357. if (ret != 0)
  38358. return -13116;
  38359. for (j = 1; j <= mp_count_bits(a); j++) {
  38360. /* Get top part */
  38361. ret = mp_div_2d(a, j, t, NULL);
  38362. if (ret != 0)
  38363. return -13117;
  38364. ret = mp_mul_2d(t, j, t);
  38365. if (ret != 0)
  38366. return -13118;
  38367. /* Get bottom part */
  38368. ret = mp_mod_2d(a, j, r);
  38369. if (ret != 0)
  38370. return -13119;
  38371. /* Reassemble */
  38372. ret = mp_add(t, r, r);
  38373. if (ret != 0)
  38374. return -13120;
  38375. if (mp_cmp(a, r) != MP_EQ)
  38376. return -13121;
  38377. }
  38378. }
  38379. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_SP_INT_NEGATIVE)
  38380. /* Test negative value being moded. */
  38381. for (j = 0; j < 20; j++) {
  38382. ret = randNum(a, 2, rng, NULL);
  38383. if (ret != 0)
  38384. return -13122;
  38385. a->sign = MP_NEG;
  38386. for (i = 1; i < DIGIT_BIT * 3 + 1; i++) {
  38387. ret = mp_mod_2d(a, i, r);
  38388. if (ret != 0)
  38389. return -13124;
  38390. mp_zero(t);
  38391. ret = mp_set_bit(t, i);
  38392. if (ret != 0)
  38393. return -13125;
  38394. ret = mp_mod(a, t, t);
  38395. if (ret != 0)
  38396. return -13126;
  38397. if (mp_cmp(r, t) != MP_EQ)
  38398. return -13127;
  38399. }
  38400. }
  38401. #endif
  38402. return 0;
  38403. }
  38404. #endif
  38405. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  38406. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  38407. static int mp_test_mod_d(mp_int* a)
  38408. {
  38409. int ret;
  38410. mp_digit r;
  38411. if (mp_set(a, 1) != MP_OKAY)
  38412. return -13130;
  38413. ret = mp_mod_d(a, 0, &r);
  38414. if (ret != MP_VAL)
  38415. return -13131;
  38416. mp_zero(a);
  38417. ret = mp_mod_d(a, 1, &r);
  38418. if (ret != MP_OKAY)
  38419. return -13132;
  38420. ret = mp_mod_d(a, 3, &r);
  38421. if (ret != MP_OKAY)
  38422. return -13133;
  38423. ret = mp_mod_d(a, 5, &r);
  38424. if (ret != MP_OKAY)
  38425. return -13134;
  38426. return 0;
  38427. }
  38428. #endif
  38429. static int mp_test_mul_sqr(mp_int* a, mp_int* b, mp_int* r1, mp_int* r2,
  38430. WC_RNG* rng)
  38431. {
  38432. int ret;
  38433. int i;
  38434. for (i = 1; i < 16; i++) {
  38435. ret = randNum(a, i, rng, NULL);
  38436. if (ret != 0)
  38437. return -13140;
  38438. ret = mp_mul(a, a, r1);
  38439. if (ret != 0)
  38440. return -13141;
  38441. ret = mp_sqr(a, r2);
  38442. if (ret != 0)
  38443. return -13142;
  38444. if (mp_cmp(r1, r2) != MP_EQ)
  38445. return -13143;
  38446. }
  38447. ret = mp_set(b, 0);
  38448. if (ret != MP_OKAY)
  38449. return -13144;
  38450. ret = mp_mul(a, b, r1);
  38451. if (ret != MP_OKAY)
  38452. return -13145;
  38453. if (!mp_iszero(r1))
  38454. return -13146;
  38455. ret = mp_sqr(b, r1);
  38456. if (ret != MP_OKAY)
  38457. return -13147;
  38458. if (!mp_iszero(r1))
  38459. return -13148;
  38460. #ifdef WOLFSSL_SP_MATH_ALL
  38461. ret = mp_set(a, 1);
  38462. if (ret != MP_OKAY)
  38463. return -13149;
  38464. i = (SP_INT_DIGITS / 2) + 1;
  38465. ret = mp_mul_2d(a, i * SP_WORD_SIZE - 1, a);
  38466. if (ret != MP_OKAY)
  38467. return -13150;
  38468. ret = mp_set(b, 1);
  38469. if (ret != MP_OKAY)
  38470. return -13151;
  38471. ret = mp_mul_2d(b, (SP_INT_DIGITS - 1 - i) * SP_WORD_SIZE - 1, b);
  38472. if (ret != MP_OKAY)
  38473. return -13152;
  38474. ret = mp_mul(a, b, r1);
  38475. if (ret != MP_OKAY)
  38476. return -13153;
  38477. ret = mp_mul(a, a, r1);
  38478. if (ret == MP_OKAY)
  38479. return -13154;
  38480. ret = mp_sqr(a, r1);
  38481. if (ret == MP_OKAY)
  38482. return -13155;
  38483. ret = mp_sqr(b, r1);
  38484. if (ret != MP_OKAY)
  38485. return -13156;
  38486. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  38487. (defined(HAVE_ECC) && defined(FP_ECC))
  38488. ret = mp_mulmod(a, b, b, r1);
  38489. if (ret != MP_OKAY)
  38490. return -13157;
  38491. ret = mp_mulmod(a, a, b, r1);
  38492. if (ret == MP_OKAY)
  38493. return -13158;
  38494. #if defined(HAVE_ECC) && (defined(ECC_SHAMIR) || defined(FP_ECC))
  38495. ret = mp_sqrmod(a, b, r1);
  38496. if (ret == MP_OKAY)
  38497. return -13159;
  38498. ret = mp_sqrmod(b, a, r1);
  38499. if (ret != MP_OKAY)
  38500. return -13160;
  38501. #endif /* HAVE_ECC && (ECC_SHAMIR || FP_ECC) */
  38502. #endif /* WOLFSSL_SP_MATH_ALL || WOLFSSL_HAVE_SP_DH || (HAVE_ECC && FP_ECC) */
  38503. #endif /* WOLFSSL_SP_MATH_ALL */
  38504. return 0;
  38505. }
  38506. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  38507. defined(OPENSSL_EXTRA)
  38508. static int mp_test_invmod(mp_int* a, mp_int* m, mp_int* r)
  38509. {
  38510. int ret;
  38511. mp_set(a, 0);
  38512. mp_set(m, 1);
  38513. ret = mp_invmod(a, m, r);
  38514. if (ret != MP_VAL)
  38515. return -13170;
  38516. ret = mp_invmod(m, a, r);
  38517. if (ret != MP_VAL)
  38518. return -13171;
  38519. mp_set(a, 2);
  38520. mp_set(m, 4);
  38521. ret = mp_invmod(a, m, r);
  38522. if (ret != MP_VAL)
  38523. return -13172;
  38524. mp_set(a, 1);
  38525. mp_set(m, 4);
  38526. ret = mp_invmod(a, m, r);
  38527. if (ret != MP_OKAY)
  38528. return -13173;
  38529. if (!mp_isone(r))
  38530. return -13174;
  38531. mp_set(a, 3);
  38532. mp_set(m, 4);
  38533. ret = mp_invmod(a, m, r);
  38534. if (ret != MP_OKAY)
  38535. return -13175;
  38536. if (mp_cmp_d(r, 3))
  38537. return -13176;
  38538. mp_set(a, 3);
  38539. mp_set(m, 5);
  38540. ret = mp_invmod(a, m, r);
  38541. if (ret != MP_OKAY)
  38542. return -13177;
  38543. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  38544. /* Maximum 'a' */
  38545. mp_set(a, 0);
  38546. mp_set_bit(a, (r->size / 2)* SP_WORD_SIZE - 1);
  38547. mp_sub_d(a, 1, a);
  38548. /* Modulus too big. */
  38549. mp_set(m, 0);
  38550. mp_set_bit(m, (r->size / 2) * SP_WORD_SIZE);
  38551. ret = mp_invmod(a, m, r);
  38552. if (ret != MP_VAL)
  38553. return -13178;
  38554. /* Maximum modulus - even. */
  38555. mp_set(m, 0);
  38556. mp_set_bit(m, (r->size / 2) * SP_WORD_SIZE - 1);
  38557. ret = mp_invmod(a, m, r);
  38558. if (ret != MP_OKAY)
  38559. return -13179;
  38560. #endif
  38561. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_INT_NEGATIVE)
  38562. mp_read_radix(a, "-3", 16);
  38563. ret = mp_invmod(a, m, r);
  38564. if (ret != MP_OKAY)
  38565. return -13180;
  38566. #endif
  38567. #if defined(WOLFSSL_SP_MATH_ALL) && defined(HAVE_ECC)
  38568. mp_set(a, 0);
  38569. mp_set(m, 3);
  38570. ret = mp_invmod_mont_ct(a, m, r, 1);
  38571. if (ret != MP_VAL)
  38572. return -13190;
  38573. mp_set(a, 1);
  38574. mp_set(m, 0);
  38575. ret = mp_invmod_mont_ct(a, m, r, 1);
  38576. if (ret != MP_VAL)
  38577. return -13191;
  38578. mp_set(a, 1);
  38579. mp_set(m, 1);
  38580. ret = mp_invmod_mont_ct(a, m, r, 1);
  38581. if (ret != MP_VAL)
  38582. return -13192;
  38583. mp_set(a, 1);
  38584. mp_set(m, 2);
  38585. ret = mp_invmod_mont_ct(a, m, r, 1);
  38586. if (ret != MP_VAL)
  38587. return -13193;
  38588. mp_set(a, 1);
  38589. mp_set(m, 3);
  38590. ret = mp_invmod_mont_ct(a, m, r, 1);
  38591. if (ret != MP_OKAY)
  38592. return -13194;
  38593. #endif
  38594. return 0;
  38595. }
  38596. #endif /* !NO_RSA || HAVE_ECC || !NO_DSA || OPENSSL_EXTRA */
  38597. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  38598. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  38599. static int mp_test_exptmod(mp_int* b, mp_int* e, mp_int* m, mp_int* r)
  38600. {
  38601. int ret;
  38602. mp_set(b, 0x2);
  38603. mp_set(e, 0x3);
  38604. mp_set(m, 0x0);
  38605. ret = mp_exptmod_ex(b, e, 1, m, r);
  38606. if (ret != MP_VAL)
  38607. return -13190;
  38608. ret = mp_exptmod_nct(b, e, m, r);
  38609. if (ret != MP_VAL)
  38610. return -13191;
  38611. mp_set(b, 0x2);
  38612. mp_set(e, 0x3);
  38613. mp_set(m, 0x1);
  38614. ret = mp_exptmod_ex(b, e, 1, m, r);
  38615. if (ret != MP_OKAY)
  38616. return -13192;
  38617. if (!mp_iszero(r))
  38618. return -13193;
  38619. ret = mp_exptmod_nct(b, e, m, r);
  38620. if (ret != MP_OKAY)
  38621. return -13194;
  38622. if (!mp_iszero(r))
  38623. return -13195;
  38624. mp_set(b, 0x2);
  38625. mp_set(e, 0x0);
  38626. mp_set(m, 0x7);
  38627. ret = mp_exptmod_ex(b, e, 1, m, r);
  38628. if (ret != MP_OKAY)
  38629. return -13196;
  38630. if (!mp_isone(r))
  38631. return -13197;
  38632. ret = mp_exptmod_nct(b, e, m, r);
  38633. if (ret != MP_OKAY)
  38634. return -13198;
  38635. if (!mp_isone(r))
  38636. return -13199;
  38637. mp_set(b, 0x0);
  38638. mp_set(e, 0x3);
  38639. mp_set(m, 0x7);
  38640. ret = mp_exptmod_ex(b, e, 1, m, r);
  38641. if (ret != MP_OKAY)
  38642. return -13200;
  38643. if (!mp_iszero(r))
  38644. return -13201;
  38645. ret = mp_exptmod_nct(b, e, m, r);
  38646. if (ret != MP_OKAY)
  38647. return -13202;
  38648. if (!mp_iszero(r))
  38649. return -13203;
  38650. mp_set(b, 0x10);
  38651. mp_set(e, 0x3);
  38652. mp_set(m, 0x7);
  38653. ret = mp_exptmod_ex(b, e, 1, m, r);
  38654. if (ret != MP_OKAY)
  38655. return -13204;
  38656. ret = mp_exptmod_nct(b, e, m, r);
  38657. if (ret != MP_OKAY)
  38658. return -13205;
  38659. mp_set(b, 0x7);
  38660. mp_set(e, 0x3);
  38661. mp_set(m, 0x7);
  38662. ret = mp_exptmod_ex(b, e, 1, m, r);
  38663. if (ret != MP_OKAY)
  38664. return -13206;
  38665. if (!mp_iszero(r))
  38666. return -13207;
  38667. ret = mp_exptmod_nct(b, e, m, r);
  38668. if (ret != MP_OKAY)
  38669. return -13208;
  38670. if (!mp_iszero(r))
  38671. return -13209;
  38672. #ifndef WOLFSSL_SP_MATH
  38673. mp_set(b, 0x01);
  38674. mp_mul_2d(b, DIGIT_BIT, b);
  38675. mp_add_d(b, 1, b);
  38676. mp_set(e, 0x3);
  38677. mp_copy(b, m);
  38678. ret = mp_exptmod_ex(b, e, 1, m, r);
  38679. if (ret != MP_OKAY)
  38680. return -13210;
  38681. if (!mp_iszero(r))
  38682. return -13211;
  38683. ret = mp_exptmod_nct(b, e, m, r);
  38684. if (ret != MP_OKAY)
  38685. return -13212;
  38686. if (!mp_iszero(r))
  38687. return -13213;
  38688. #endif
  38689. mp_set(b, 0x2);
  38690. mp_set(e, 0x3);
  38691. mp_set(m, 0x7);
  38692. ret = mp_exptmod_ex(b, e, 1, m, r);
  38693. if (ret != MP_OKAY)
  38694. return -13214;
  38695. ret = mp_exptmod_nct(b, e, m, r);
  38696. if (ret != MP_OKAY)
  38697. return -13215;
  38698. #ifdef WOLFSSL_SP_MATH_ALL
  38699. mp_set(b, 0x2);
  38700. mp_set(e, 0x3);
  38701. mp_set(m, 0x01);
  38702. mp_mul_2d(m, SP_WORD_SIZE * SP_INT_DIGITS / 2, m);
  38703. mp_add_d(m, 0x01, m);
  38704. ret = mp_exptmod_ex(b, e, 1, m, r);
  38705. if (ret != MP_VAL)
  38706. return -13216;
  38707. ret = mp_exptmod_nct(b, e, m, r);
  38708. if (ret != MP_VAL)
  38709. return -13217;
  38710. #endif
  38711. return 0;
  38712. }
  38713. #endif /* !NO_RSA || !NO_DSA || !NO_DH || (HAVE_ECC && HAVE_COMP_KEY) ||
  38714. * OPENSSL_EXTRA */
  38715. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  38716. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  38717. static int mp_test_mont(mp_int* a, mp_int* m, mp_int* n, mp_int* r, WC_RNG* rng)
  38718. {
  38719. int ret;
  38720. mp_digit mp;
  38721. static int exp[] = { 7, 8, 16, 27, 32, 64,
  38722. 127, 128, 255, 256,
  38723. #if defined(SP_WORD_SIZE) && SP_WORD_SIZE > 8
  38724. 383, 384, 2033, 2048
  38725. #endif
  38726. };
  38727. static mp_digit sub[] = { 0x01, 0x05, 0x0f, 0x27, 0x05, 0x3b,
  38728. 0x01, 0x9f, 0x13, 0xbd,
  38729. #if defined(SP_WORD_SIZE) && SP_WORD_SIZE > 8
  38730. 0x1f, 0x13d, 0x45, 0x615
  38731. #endif
  38732. };
  38733. int bits[] = { 256, 384,
  38734. #if defined(SP_INT_MAX_BITS) && SP_INT_MAX_BITS > 4096
  38735. 2048,
  38736. #endif
  38737. #if defined(SP_INT_MAX_BITS) && SP_INT_MAX_BITS > 6144
  38738. 3072
  38739. #endif
  38740. };
  38741. int i;
  38742. int j;
  38743. for (i = 0; i < (int)(sizeof(exp) / sizeof(*exp)); i++) {
  38744. if (exp[i] >= DIGIT_BIT)
  38745. continue;
  38746. mp_zero(m);
  38747. ret = mp_set_bit(m, exp[i]);
  38748. if (ret != MP_OKAY)
  38749. return -13220;
  38750. ret = mp_sub_d(m, sub[i], m);
  38751. if (ret != MP_OKAY)
  38752. return -13221;
  38753. ret = mp_montgomery_setup(m, &mp);
  38754. if (ret != MP_OKAY)
  38755. return -13222;
  38756. ret = mp_montgomery_calc_normalization(n, m);
  38757. if (ret != MP_OKAY)
  38758. return -13223;
  38759. for (j = 0; j < 10; j++) {
  38760. ret = randNum(a, (exp[i] + DIGIT_BIT - 1) / DIGIT_BIT, rng, NULL);
  38761. if (ret != 0)
  38762. return -13224;
  38763. ret = mp_mod(a, m, a);
  38764. if (ret != 0)
  38765. return -13225;
  38766. /* r = a * a */
  38767. ret = mp_sqrmod(a, m, r);
  38768. if (ret != MP_OKAY)
  38769. return -13226;
  38770. /* Convert to Montgomery form = a*n */
  38771. ret = mp_mulmod(a, n, m, a);
  38772. if (ret != MP_OKAY)
  38773. return -13227;
  38774. /* a*a mod m == ((a*n) * (a*n)) / n / n */
  38775. ret = mp_sqr(a, a);
  38776. if (ret != MP_OKAY)
  38777. return -13228;
  38778. ret = mp_montgomery_reduce(a, m, mp);
  38779. if (ret != MP_OKAY)
  38780. return -13229;
  38781. ret = mp_montgomery_reduce(a, m, mp);
  38782. if (ret != MP_OKAY)
  38783. return -13230;
  38784. if (mp_cmp(a, r) != MP_EQ)
  38785. return -13231;
  38786. }
  38787. }
  38788. /* Force carries. */
  38789. for (i = 0; i < (int)(sizeof(bits) / sizeof(*bits)); i++) {
  38790. /* a = 2^(bits*2) - 1 */
  38791. mp_zero(a);
  38792. mp_set_bit(a, bits[i] * 2);
  38793. mp_sub_d(a, 1, a);
  38794. /* m = 2^(bits) - 1 */
  38795. mp_zero(m);
  38796. mp_set_bit(m, bits[i]);
  38797. mp_sub_d(m, 1, m);
  38798. mp = 1;
  38799. /* result = r = 2^(bits) - 1 */
  38800. mp_zero(r);
  38801. mp_set_bit(r, bits[i]);
  38802. mp_sub_d(r, 1, r);
  38803. ret = mp_montgomery_reduce(a, m, mp);
  38804. if (ret != MP_OKAY)
  38805. return -13240;
  38806. /* Result is m or 0 if reduced to range of modulus. */
  38807. if (mp_cmp(a, r) != MP_EQ && mp_iszero(a) != MP_YES)
  38808. return -13241;
  38809. }
  38810. return 0;
  38811. }
  38812. #endif
  38813. WOLFSSL_TEST_SUBROUTINE int mp_test(void)
  38814. {
  38815. WC_RNG rng;
  38816. int ret;
  38817. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  38818. int i, j;
  38819. #ifndef WOLFSSL_SP_MATH
  38820. int k;
  38821. #endif
  38822. mp_digit d;
  38823. #endif
  38824. mp_int a, b, r1, r2, p;
  38825. ret = mp_init_multi(&a, &b, &r1, &r2, NULL, NULL);
  38826. if (ret != 0)
  38827. return -13300;
  38828. #ifdef WOLFSSL_SP_MATH_ALL
  38829. mp_init_copy(&p, &a);
  38830. #else
  38831. ret = mp_init(&p);
  38832. if (ret != 0)
  38833. return -13301;
  38834. #endif
  38835. #ifndef HAVE_FIPS
  38836. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  38837. #else
  38838. ret = wc_InitRng(&rng);
  38839. #endif
  38840. if (ret != 0)
  38841. goto done;
  38842. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  38843. mp_set_int(&a, 0);
  38844. if (a.used != 0 || a.dp[0] != 0)
  38845. return -13302;
  38846. for (j = 1; j <= MP_MAX_TEST_BYTE_LEN; j++) {
  38847. for (i = 0; i < 4 * j; i++) {
  38848. /* New values to use. */
  38849. ret = randNum(&p, j, &rng, NULL);
  38850. if (ret != 0)
  38851. return -13303;
  38852. ret = randNum(&a, j, &rng, NULL);
  38853. if (ret != 0)
  38854. return -13304;
  38855. ret = randNum(&b, j, &rng, NULL);
  38856. if (ret != 0)
  38857. return -13305;
  38858. ret = wc_RNG_GenerateBlock(&rng, (byte*)&d, sizeof(d));
  38859. if (ret != 0)
  38860. return -13306;
  38861. d &= MP_MASK;
  38862. #if !defined(WOLFSSL_SP_MATH) || (defined(HAVE_ECC) && \
  38863. (defined(ECC_SHAMIR) || defined(FP_ECC)))
  38864. /* Ensure sqrmod produce same result as mulmod. */
  38865. ret = mp_sqrmod(&a, &p, &r1);
  38866. if (ret != 0)
  38867. return -13307;
  38868. ret = mp_mulmod(&a, &a, &p, &r2);
  38869. if (ret != 0)
  38870. return -13308;
  38871. if (mp_cmp(&r1, &r2) != 0)
  38872. return -13309;
  38873. #endif
  38874. #if defined(WOLFSSL_SP_MATH_ALL) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  38875. #if defined(WOLFSSL_SP_MATH) || (defined(WOLFSSL_SP_MATH_ALL) && \
  38876. !defined(WOLFSSL_SP_INT_NEGATIVE))
  38877. ret = mp_addmod(&a, &b, &p, &r1);
  38878. if (ret != 0)
  38879. return -13310;
  38880. ret = mp_submod(&r1, &b, &p, &r2);
  38881. if (ret != 0)
  38882. return -13311;
  38883. ret = mp_mod(&a, &p, &r1);
  38884. if (ret != 0)
  38885. return -13312;
  38886. if (mp_cmp(&r1, &r2) != MP_EQ)
  38887. return -13313;
  38888. #else
  38889. /* Ensure add with mod produce same result as sub with mod. */
  38890. ret = mp_addmod(&a, &b, &p, &r1);
  38891. if (ret != 0)
  38892. return -13314;
  38893. b.sign ^= 1;
  38894. ret = mp_submod(&a, &b, &p, &r2);
  38895. if (ret != 0)
  38896. return -13315;
  38897. if (mp_cmp(&r1, &r2) != 0)
  38898. return -13316;
  38899. #endif
  38900. #endif
  38901. /* Ensure add digit produce same result as sub digit. */
  38902. ret = mp_add_d(&a, d, &r1);
  38903. if (ret != 0)
  38904. return -13317;
  38905. ret = mp_sub_d(&r1, d, &r2);
  38906. if (ret != 0)
  38907. return -13318;
  38908. if (mp_cmp(&a, &r2) != 0)
  38909. return -13319;
  38910. /* Invert - if p is even it will use the slow impl.
  38911. * - if p and a are even it will fail.
  38912. */
  38913. ret = mp_invmod(&a, &p, &r1);
  38914. if (ret != 0 && ret != MP_VAL)
  38915. return -13320;
  38916. ret = 0;
  38917. #ifndef WOLFSSL_SP_MATH
  38918. /* Shift up and down number all bits in a digit. */
  38919. for (k = 0; k < DIGIT_BIT; k++) {
  38920. mp_mul_2d(&a, k, &r1);
  38921. mp_div_2d(&r1, k, &r2, &p);
  38922. if (mp_cmp(&a, &r2) != 0)
  38923. return -13321;
  38924. if (!mp_iszero(&p))
  38925. return -13322;
  38926. mp_rshb(&r1, k);
  38927. if (mp_cmp(&a, &r1) != 0)
  38928. return -13323;
  38929. }
  38930. #endif
  38931. }
  38932. }
  38933. #if DIGIT_BIT >= 32
  38934. /* Check that setting a 32-bit digit works. */
  38935. d &= 0xffffffffU;
  38936. mp_set_int(&a, d);
  38937. if (a.used != 1 || a.dp[0] != d)
  38938. return -13324;
  38939. #endif
  38940. /* Check setting a bit and testing a bit works. */
  38941. for (i = 0; i < MP_MAX_TEST_BYTE_LEN * 8; i++) {
  38942. mp_zero(&a);
  38943. mp_set_bit(&a, i);
  38944. if (!mp_is_bit_set(&a, i))
  38945. return -13325;
  38946. }
  38947. #endif
  38948. #if defined(HAVE_ECC) && defined(HAVE_COMP_KEY)
  38949. mp_zero(&a);
  38950. i = mp_cnt_lsb(&a);
  38951. if (i != 0)
  38952. return -13326;
  38953. mp_set(&a, 1);
  38954. i = mp_cnt_lsb(&a);
  38955. if (i != 0)
  38956. return -13327;
  38957. #endif
  38958. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  38959. if ((ret = mp_test_param(&a, &b, &r1, &rng)) != 0)
  38960. return ret;
  38961. #endif
  38962. #if defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)
  38963. if ((ret = mp_test_div_3(&a, &r1, &rng)) != 0)
  38964. return ret;
  38965. #endif
  38966. #if (defined(WOLFSSL_SP_MATH_ALL) && !defined(NO_RSA) && \
  38967. !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
  38968. (!defined WOLFSSL_SP_MATH && !defined(WOLFSSL_SP_MATH_ALL) && \
  38969. (defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)))
  38970. if ((ret = mp_test_radix_10(&a, &r1, &rng)) != 0)
  38971. return ret;
  38972. #endif
  38973. #if defined(WOLFSSL_SP_MATH_ALL) || (!defined WOLFSSL_SP_MATH && \
  38974. defined(HAVE_ECC))
  38975. if ((ret = mp_test_radix_16(&a, &r1, &rng)) != 0)
  38976. return ret;
  38977. #endif
  38978. if ((ret = mp_test_shift(&a, &r1, &rng)) != 0)
  38979. return ret;
  38980. if ((ret = mp_test_add_sub_d(&a, &r1)) != 0)
  38981. return ret;
  38982. if ((ret = mp_test_read_to_bin(&a)) != 0)
  38983. return ret;
  38984. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  38985. if ((ret = mp_test_set_int(&a)) != 0)
  38986. return ret;
  38987. #endif
  38988. if ((ret = mp_test_cmp(&a, &r1)) != 0)
  38989. return ret;
  38990. #if !defined(NO_DH) || defined(HAVE_ECC) || !defined(WOLFSSL_RSA_VERIFY_ONLY)
  38991. if ((ret = mp_test_shbd(&a, &b, &rng)) != 0)
  38992. return ret;
  38993. #endif
  38994. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  38995. if ((ret = mp_test_set_is_bit(&a)) != 0)
  38996. return ret;
  38997. #endif
  38998. #if !defined(WOLFSSL_SP_MATH) && !defined(WOLFSSL_SP_MATH_ALL)
  38999. if ((ret = mp_test_div(&a, &b, &r1, &r2, &rng)) != 0)
  39000. return ret;
  39001. #endif
  39002. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA)) && \
  39003. !defined(WC_NO_RNG)
  39004. if ((ret = mp_test_prime(&a, &rng)) != 0)
  39005. return ret;
  39006. #endif
  39007. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(WC_RSA_BLINDING)
  39008. if ((ret = mp_test_lcm_gcd(&a, &b, &r1, &r2, &rng)) != 0)
  39009. return ret;
  39010. #endif
  39011. #if (!defined(WOLFSSL_SP_MATH) && !defined(USE_FAST_MATH)) || \
  39012. defined(WOLFSSL_SP_MATH_ALL)
  39013. if ((ret = mp_test_mod_2d(&a, &r1, &p, &rng)) != 0)
  39014. return ret;
  39015. #endif
  39016. #if (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || \
  39017. (defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN))
  39018. if ((ret = mp_test_mod_d(&a)) != 0)
  39019. return ret;
  39020. #endif
  39021. if ((ret = mp_test_mul_sqr(&a, &b, &r1, &r2, &rng)) != 0)
  39022. return ret;
  39023. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  39024. defined(OPENSSL_EXTRA)
  39025. if ((ret = mp_test_invmod(&a, &b, &r1)) != 0)
  39026. return ret;
  39027. #endif
  39028. #if !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH) || \
  39029. (defined(HAVE_ECC) && defined(HAVE_COMP_KEY)) || defined(OPENSSL_EXTRA)
  39030. if ((ret = mp_test_exptmod(&a, &b, &r1, &r2)) != 0)
  39031. return ret;
  39032. #endif
  39033. #if defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_HAVE_SP_DH) || \
  39034. defined(HAVE_ECC) || (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
  39035. if ((ret = mp_test_mont(&a, &b, &r1, &r2, &rng)) != 0)
  39036. return ret;
  39037. #endif
  39038. done:
  39039. mp_clear(&p);
  39040. mp_clear(&r2);
  39041. mp_clear(&r1);
  39042. mp_clear(&b);
  39043. mp_clear(&a);
  39044. wc_FreeRng(&rng);
  39045. return ret;
  39046. }
  39047. #endif
  39048. #if defined(WOLFSSL_PUBLIC_MP) && defined(WOLFSSL_KEY_GEN)
  39049. typedef struct pairs_t {
  39050. const unsigned char* coeff;
  39051. int coeffSz;
  39052. int exp;
  39053. } pairs_t;
  39054. /*
  39055. n =p1p2p3, where pi = ki(p1-1)+1 with (k2,k3) = (173,293)
  39056. p1 = 2^192 * 0x000000000000e24fd4f6d6363200bf2323ec46285cac1d3a
  39057. + 2^0 * 0x0b2488b0c29d96c5e67f8bec15b54b189ae5636efe89b45b
  39058. */
  39059. static const unsigned char c192a[] =
  39060. {
  39061. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe2, 0x4f,
  39062. 0xd4, 0xf6, 0xd6, 0x36, 0x32, 0x00, 0xbf, 0x23,
  39063. 0x23, 0xec, 0x46, 0x28, 0x5c, 0xac, 0x1d, 0x3a
  39064. };
  39065. static const unsigned char c0a[] =
  39066. {
  39067. 0x0b, 0x24, 0x88, 0xb0, 0xc2, 0x9d, 0x96, 0xc5,
  39068. 0xe6, 0x7f, 0x8b, 0xec, 0x15, 0xb5, 0x4b, 0x18,
  39069. 0x9a, 0xe5, 0x63, 0x6e, 0xfe, 0x89, 0xb4, 0x5b
  39070. };
  39071. static const pairs_t ecPairsA[] =
  39072. {
  39073. {c192a, sizeof(c192a), 192},
  39074. {c0a, sizeof(c0a), 0}
  39075. };
  39076. static const int kA[] = {173, 293};
  39077. static const unsigned char controlPrime[] = {
  39078. 0xe1, 0x76, 0x45, 0x80, 0x59, 0xb6, 0xd3, 0x49,
  39079. 0xdf, 0x0a, 0xef, 0x12, 0xd6, 0x0f, 0xf0, 0xb7,
  39080. 0xcb, 0x2a, 0x37, 0xbf, 0xa7, 0xf8, 0xb5, 0x4d,
  39081. 0xf5, 0x31, 0x35, 0xad, 0xe4, 0xa3, 0x94, 0xa1,
  39082. 0xdb, 0xf1, 0x96, 0xad, 0xb5, 0x05, 0x64, 0x85,
  39083. 0x83, 0xfc, 0x1b, 0x5b, 0x29, 0xaa, 0xbe, 0xf8,
  39084. 0x26, 0x3f, 0x76, 0x7e, 0xad, 0x1c, 0xf0, 0xcb,
  39085. 0xd7, 0x26, 0xb4, 0x1b, 0x05, 0x8e, 0x56, 0x86,
  39086. 0x7e, 0x08, 0x62, 0x21, 0xc1, 0x86, 0xd6, 0x47,
  39087. 0x79, 0x3e, 0xb7, 0x5d, 0xa4, 0xc6, 0x3a, 0xd7,
  39088. 0xb1, 0x74, 0x20, 0xf6, 0x50, 0x97, 0x41, 0x04,
  39089. 0x53, 0xed, 0x3f, 0x26, 0xd6, 0x6f, 0x91, 0xfa,
  39090. 0x68, 0x26, 0xec, 0x2a, 0xdc, 0x9a, 0xf1, 0xe7,
  39091. 0xdc, 0xfb, 0x73, 0xf0, 0x79, 0x43, 0x1b, 0x21,
  39092. 0xa3, 0x59, 0x04, 0x63, 0x52, 0x07, 0xc9, 0xd7,
  39093. 0xe6, 0xd1, 0x1b, 0x5d, 0x5e, 0x96, 0xfa, 0x53
  39094. };
  39095. static const unsigned char testOne[] = { 1 };
  39096. static int GenerateNextP(mp_int* p1, mp_int* p2, int k)
  39097. {
  39098. int ret;
  39099. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39100. mp_int *ki = (mp_int *)XMALLOC(sizeof(*ki), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39101. if (ki == NULL)
  39102. return MEMORY_E;
  39103. #else
  39104. mp_int ki[1];
  39105. #endif
  39106. ret = mp_init(ki);
  39107. if (ret == 0)
  39108. ret = mp_set(ki, k);
  39109. if (ret == 0)
  39110. ret = mp_sub_d(p1, 1, p2);
  39111. if (ret == 0)
  39112. ret = mp_mul(p2, ki, p2);
  39113. if (ret == 0)
  39114. ret = mp_add_d(p2, 1, p2);
  39115. mp_clear(ki);
  39116. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39117. XFREE(ki, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39118. #endif
  39119. return ret;
  39120. }
  39121. static int GenerateP(mp_int* p1, mp_int* p2, mp_int* p3,
  39122. const pairs_t* ecPairs, int ecPairsSz,
  39123. const int* k)
  39124. {
  39125. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39126. mp_int *x = NULL, *y = NULL;
  39127. #else
  39128. mp_int x[1], y[1];
  39129. #endif
  39130. int ret, i;
  39131. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39132. if (((x = (mp_int *)XMALLOC(sizeof(*x), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL) ||
  39133. ((y = (mp_int *)XMALLOC(sizeof(*x), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)) {
  39134. ret = MEMORY_E;
  39135. goto out;
  39136. }
  39137. #endif
  39138. ret = mp_init_multi(x, y, NULL, NULL, NULL, NULL);
  39139. if (ret != 0) {
  39140. ret = MP_MEM;
  39141. goto out;
  39142. }
  39143. for (i = 0; ret == 0 && i < ecPairsSz; i++) {
  39144. ret = mp_read_unsigned_bin(x, ecPairs[i].coeff, ecPairs[i].coeffSz);
  39145. /* p1 = 2^exp */
  39146. if (ret == 0)
  39147. ret = mp_2expt(y, ecPairs[i].exp);
  39148. /* p1 = p1 * m */
  39149. if (ret == 0)
  39150. ret = mp_mul(x, y, x);
  39151. /* p1 += */
  39152. if (ret == 0)
  39153. ret = mp_add(p1, x, p1);
  39154. mp_zero(x);
  39155. mp_zero(y);
  39156. }
  39157. if (ret == 0)
  39158. ret = GenerateNextP(p1, p2, k[0]);
  39159. if (ret == 0)
  39160. ret = GenerateNextP(p1, p3, k[1]);
  39161. out:
  39162. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39163. if (x != NULL) {
  39164. mp_clear(x);
  39165. XFREE(x, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39166. }
  39167. if (y != NULL) {
  39168. mp_clear(y);
  39169. XFREE(y, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39170. }
  39171. #else
  39172. mp_clear(x);
  39173. mp_clear(y);
  39174. #endif
  39175. return ret;
  39176. }
  39177. WOLFSSL_TEST_SUBROUTINE int prime_test(void)
  39178. {
  39179. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39180. mp_int *n = (mp_int *)XMALLOC(sizeof *n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  39181. *p1 = (mp_int *)XMALLOC(sizeof *p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  39182. *p2 = (mp_int *)XMALLOC(sizeof *p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER),
  39183. *p3 = (mp_int *)XMALLOC(sizeof *p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39184. #else
  39185. mp_int n[1],
  39186. p1[1],
  39187. p2[1],
  39188. p3[1];
  39189. #endif
  39190. int ret, isPrime = 0;
  39191. WC_RNG rng;
  39192. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39193. if ((n == NULL) ||
  39194. (p1 == NULL) ||
  39195. (p2 == NULL) ||
  39196. (p3 == NULL))
  39197. ERROR_OUT(MEMORY_E, out);
  39198. #endif
  39199. ret = wc_InitRng(&rng);
  39200. if (ret == 0)
  39201. ret = mp_init_multi(n, p1, p2, p3, NULL, NULL);
  39202. if (ret == 0)
  39203. ret = GenerateP(p1, p2, p3,
  39204. ecPairsA, sizeof(ecPairsA) / sizeof(ecPairsA[0]), kA);
  39205. if (ret == 0)
  39206. ret = mp_mul(p1, p2, n);
  39207. if (ret == 0)
  39208. ret = mp_mul(n, p3, n);
  39209. if (ret != 0)
  39210. ERROR_OUT(-13400, out);
  39211. /* Check the old prime test using the number that false positives.
  39212. * This test result should indicate as not prime. */
  39213. ret = mp_prime_is_prime(n, 40, &isPrime);
  39214. if (ret != 0)
  39215. ERROR_OUT(-13401, out);
  39216. if (isPrime)
  39217. ERROR_OUT(-13402, out);
  39218. /* This test result should fail. It should indicate the value as prime. */
  39219. ret = mp_prime_is_prime(n, 8, &isPrime);
  39220. if (ret != 0)
  39221. ERROR_OUT(-13403, out);
  39222. if (!isPrime)
  39223. ERROR_OUT(-13404, out);
  39224. /* This test result should indicate the value as not prime. */
  39225. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  39226. if (ret != 0)
  39227. ERROR_OUT(-13405, out);
  39228. if (isPrime)
  39229. ERROR_OUT(-13406, out);
  39230. ret = mp_read_unsigned_bin(n, controlPrime, sizeof(controlPrime));
  39231. if (ret != 0)
  39232. ERROR_OUT(-13407, out);
  39233. /* This test result should indicate the value as prime. */
  39234. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  39235. if (ret != 0)
  39236. ERROR_OUT(-13408, out);
  39237. if (!isPrime)
  39238. ERROR_OUT(-13409, out);
  39239. /* This test result should indicate the value as prime. */
  39240. isPrime = -1;
  39241. ret = mp_prime_is_prime(n, 8, &isPrime);
  39242. if (ret != 0)
  39243. ERROR_OUT(-13410, out);
  39244. if (!isPrime)
  39245. ERROR_OUT(-13411, out);
  39246. ret = mp_read_unsigned_bin(n, testOne, sizeof(testOne));
  39247. if (ret != 0)
  39248. ERROR_OUT(-13412, out);
  39249. /* This test result should indicate the value as not prime. */
  39250. ret = mp_prime_is_prime_ex(n, 8, &isPrime, &rng);
  39251. if (ret != 0)
  39252. ERROR_OUT(-13413, out);
  39253. if (isPrime)
  39254. ERROR_OUT(-13414, out);
  39255. ret = mp_prime_is_prime(n, 8, &isPrime);
  39256. if (ret != 0)
  39257. ERROR_OUT(-13415, out);
  39258. if (isPrime)
  39259. ERROR_OUT(-13416, out);
  39260. ret = 0;
  39261. out:
  39262. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39263. if (n != NULL) {
  39264. mp_clear(n);
  39265. XFREE(n, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39266. }
  39267. if (p1 != NULL) {
  39268. mp_clear(p1);
  39269. XFREE(p1, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39270. }
  39271. if (p2 != NULL) {
  39272. mp_clear(p2);
  39273. XFREE(p2, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39274. }
  39275. if (p3 != NULL) {
  39276. mp_clear(p3);
  39277. XFREE(p3, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39278. }
  39279. #else
  39280. mp_clear(p3);
  39281. mp_clear(p2);
  39282. mp_clear(p1);
  39283. mp_clear(n);
  39284. #endif
  39285. wc_FreeRng(&rng);
  39286. return ret;
  39287. }
  39288. #endif /* WOLFSSL_PUBLIC_MP */
  39289. #if defined(ASN_BER_TO_DER) && \
  39290. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  39291. defined(OPENSSL_EXTRA_X509_SMALL))
  39292. /* wc_BerToDer is only public facing in the case of test cert or opensslextra */
  39293. typedef struct berDerTestData {
  39294. const byte *in;
  39295. word32 inSz;
  39296. const byte *out;
  39297. word32 outSz;
  39298. } berDerTestData;
  39299. WOLFSSL_TEST_SUBROUTINE int berder_test(void)
  39300. {
  39301. int ret;
  39302. int i;
  39303. word32 len = 0, l;
  39304. byte out[32];
  39305. WOLFSSL_SMALL_STACK_STATIC const byte good1_in[] = { 0x30, 0x80, 0x00, 0x00 };
  39306. WOLFSSL_SMALL_STACK_STATIC const byte good1_out[] = { 0x30, 0x00 };
  39307. WOLFSSL_SMALL_STACK_STATIC const byte good2_in[] = { 0x30, 0x80, 0x02, 0x01, 0x01, 0x00, 0x00 };
  39308. WOLFSSL_SMALL_STACK_STATIC const byte good2_out[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  39309. WOLFSSL_SMALL_STACK_STATIC const byte good3_in[] = {
  39310. 0x24, 0x80, 0x04, 0x01, 0x01, 0x00, 0x00
  39311. };
  39312. WOLFSSL_SMALL_STACK_STATIC const byte good3_out[] = { 0x04, 0x1, 0x01 };
  39313. WOLFSSL_SMALL_STACK_STATIC const byte good4_in[] = {
  39314. 0x30, 0x80,
  39315. 0x02, 0x01, 0x01,
  39316. 0x30, 0x80,
  39317. 0x24, 0x80,
  39318. 0x04, 0x01, 0x01,
  39319. 0x04, 0x02, 0x02, 0x03,
  39320. 0x00, 0x00,
  39321. 0x06, 0x01, 0x01,
  39322. 0x00, 0x00,
  39323. 0x31, 0x80,
  39324. 0x06, 0x01, 0x01,
  39325. 0x00, 0x00,
  39326. 0x00, 0x00,
  39327. };
  39328. WOLFSSL_SMALL_STACK_STATIC const byte good4_out[] = {
  39329. 0x30, 0x12,
  39330. 0x02, 0x01, 0x01,
  39331. 0x30, 0x08,
  39332. 0x04, 0x03, 0x01, 0x02, 0x03,
  39333. 0x06, 0x01, 0x01,
  39334. 0x31, 0x03,
  39335. 0x06, 0x01, 0x01
  39336. };
  39337. WOLFSSL_SMALL_STACK_STATIC const byte good5_in[] = { 0x30, 0x03, 0x02, 0x01, 0x01 };
  39338. berDerTestData testData[] = {
  39339. { good1_in, sizeof(good1_in), good1_out, sizeof(good1_out) },
  39340. { good2_in, sizeof(good2_in), good2_out, sizeof(good2_out) },
  39341. { good3_in, sizeof(good3_in), good3_out, sizeof(good3_out) },
  39342. { good4_in, sizeof(good4_in), good4_out, sizeof(good4_out) },
  39343. { good5_in, sizeof(good5_in), good5_in , sizeof(good5_in ) },
  39344. };
  39345. for (i = 0; i < (int)(sizeof(testData) / sizeof(*testData)); i++) {
  39346. ret = wc_BerToDer(testData[i].in, testData[i].inSz, NULL, &len);
  39347. if (ret != LENGTH_ONLY_E)
  39348. return -13500 - i;
  39349. if (len != testData[i].outSz)
  39350. return -13510 - i;
  39351. len = testData[i].outSz;
  39352. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &len);
  39353. if (ret != 0)
  39354. return -13520 - i;
  39355. if (XMEMCMP(out, testData[i].out, len) != 0)
  39356. return -13530 - i;
  39357. for (l = 1; l < testData[i].inSz; l++) {
  39358. ret = wc_BerToDer(testData[i].in, l, NULL, &len);
  39359. if (ret != ASN_PARSE_E)
  39360. return -13540;
  39361. len = testData[i].outSz;
  39362. ret = wc_BerToDer(testData[i].in, l, out, &len);
  39363. if (ret != ASN_PARSE_E)
  39364. return -13541;
  39365. }
  39366. for (l = 0; l < testData[i].outSz-1; l++) {
  39367. ret = wc_BerToDer(testData[i].in, testData[i].inSz, out, &l);
  39368. if (ret != BUFFER_E)
  39369. return -13542;
  39370. }
  39371. }
  39372. ret = wc_BerToDer(NULL, 4, NULL, NULL);
  39373. if (ret != BAD_FUNC_ARG)
  39374. return -13543;
  39375. ret = wc_BerToDer(out, 4, NULL, NULL);
  39376. if (ret != BAD_FUNC_ARG)
  39377. return -13544;
  39378. ret = wc_BerToDer(NULL, 4, NULL, &len);
  39379. if (ret != BAD_FUNC_ARG)
  39380. return -13545;
  39381. ret = wc_BerToDer(NULL, 4, out, NULL);
  39382. if (ret != BAD_FUNC_ARG)
  39383. return -13546;
  39384. ret = wc_BerToDer(out, 4, out, NULL);
  39385. if (ret != BAD_FUNC_ARG)
  39386. return -13547;
  39387. ret = wc_BerToDer(NULL, 4, out, &len);
  39388. if (ret != BAD_FUNC_ARG)
  39389. return -13548;
  39390. for (l = 1; l < sizeof(good4_out); l++) {
  39391. len = l;
  39392. ret = wc_BerToDer(good4_in, sizeof(good4_in), out, &len);
  39393. if (ret != BUFFER_E)
  39394. return -13549;
  39395. }
  39396. return 0;
  39397. }
  39398. #endif
  39399. #ifdef DEBUG_WOLFSSL
  39400. static THREAD_LS_T int log_cnt = 0;
  39401. static void my_Logging_cb(const int logLevel, const char *const logMessage)
  39402. {
  39403. (void)logLevel;
  39404. (void)logMessage;
  39405. log_cnt++;
  39406. }
  39407. #endif /* DEBUG_WOLFSSL */
  39408. WOLFSSL_TEST_SUBROUTINE int logging_test(void)
  39409. {
  39410. #ifdef DEBUG_WOLFSSL
  39411. const char* msg = "Testing, testing. 1, 2, 3, 4 ...";
  39412. byte a[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  39413. byte b[256];
  39414. int i;
  39415. for (i = 0; i < (int)sizeof(b); i++)
  39416. b[i] = i;
  39417. if (wolfSSL_Debugging_ON() != 0)
  39418. return -13600;
  39419. if (wolfSSL_SetLoggingCb(my_Logging_cb) != 0)
  39420. return -13601;
  39421. WOLFSSL_MSG(msg);
  39422. WOLFSSL_BUFFER(a, sizeof(a));
  39423. WOLFSSL_BUFFER(b, sizeof(b));
  39424. WOLFSSL_BUFFER(NULL, 0);
  39425. WOLFSSL_ERROR(MEMORY_E);
  39426. WOLFSSL_ERROR_MSG(msg);
  39427. /* turn off logs */
  39428. wolfSSL_Debugging_OFF();
  39429. /* capture log count */
  39430. i = log_cnt;
  39431. /* validate no logs are output when disabled */
  39432. WOLFSSL_MSG(msg);
  39433. WOLFSSL_BUFFER(a, sizeof(a));
  39434. WOLFSSL_BUFFER(b, sizeof(b));
  39435. WOLFSSL_BUFFER(NULL, 0);
  39436. WOLFSSL_ERROR(MEMORY_E);
  39437. WOLFSSL_ERROR_MSG(msg);
  39438. /* check the logs were disabled */
  39439. if (i != log_cnt)
  39440. return -13602;
  39441. /* restore callback and leave logging enabled */
  39442. wolfSSL_SetLoggingCb(NULL);
  39443. wolfSSL_Debugging_ON();
  39444. /* suppress unused args */
  39445. (void)a;
  39446. (void)b;
  39447. #else
  39448. if (wolfSSL_Debugging_ON() != NOT_COMPILED_IN)
  39449. return -13603;
  39450. wolfSSL_Debugging_OFF();
  39451. if (wolfSSL_SetLoggingCb(NULL) != NOT_COMPILED_IN)
  39452. return -13604;
  39453. #endif /* DEBUG_WOLFSSL */
  39454. return 0;
  39455. }
  39456. WOLFSSL_TEST_SUBROUTINE int mutex_test(void)
  39457. {
  39458. #ifdef WOLFSSL_PTHREADS
  39459. wolfSSL_Mutex m;
  39460. #endif
  39461. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_USER_MUTEX)
  39462. wolfSSL_Mutex *mm = wc_InitAndAllocMutex();
  39463. if (mm == NULL)
  39464. return -13700;
  39465. wc_FreeMutex(mm);
  39466. XFREE(mm, HEAP_HINT, DYNAMIC_TYPE_MUTEX);
  39467. #endif
  39468. /* Can optionally enable advanced pthread tests using "ENABLE_PTHREAD_LOCKFREE_TESTS" */
  39469. #ifdef WOLFSSL_PTHREADS
  39470. if (wc_InitMutex(&m) != 0)
  39471. return -13701;
  39472. if (wc_LockMutex(&m) != 0)
  39473. return -13702;
  39474. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  39475. /* trying to free a locked mutex is not portable behavior with pthread */
  39476. /* Attempting to destroy a locked mutex results in undefined behavior */
  39477. if (wc_FreeMutex(&m) != BAD_MUTEX_E)
  39478. return -13703;
  39479. #endif
  39480. if (wc_UnLockMutex(&m) != 0)
  39481. return -13704;
  39482. if (wc_FreeMutex(&m) != 0)
  39483. return -13705;
  39484. #if !defined(WOLFSSL_SOLARIS) && defined(ENABLE_PTHREAD_LOCKFREE_TESTS)
  39485. /* Trying to use a pthread after free'ing is not portable behavior */
  39486. if (wc_LockMutex(&m) != BAD_MUTEX_E)
  39487. return -13706;
  39488. if (wc_UnLockMutex(&m) != BAD_MUTEX_E)
  39489. return -13707;
  39490. #endif
  39491. #endif
  39492. return 0;
  39493. }
  39494. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  39495. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  39496. !defined(WOLFSSL_STATIC_MEMORY)
  39497. static int malloc_cnt = 0;
  39498. static int realloc_cnt = 0;
  39499. static int free_cnt = 0;
  39500. #ifdef WOLFSSL_DEBUG_MEMORY
  39501. static void *my_Malloc_cb(size_t size, const char* func, unsigned int line)
  39502. {
  39503. (void) func;
  39504. (void) line;
  39505. #else
  39506. static void *my_Malloc_cb(size_t size)
  39507. {
  39508. #endif
  39509. malloc_cnt++;
  39510. #ifndef WOLFSSL_NO_MALLOC
  39511. return malloc(size);
  39512. #else
  39513. WOLFSSL_MSG("No malloc available");
  39514. (void)size;
  39515. return NULL;
  39516. #endif
  39517. }
  39518. #ifdef WOLFSSL_DEBUG_MEMORY
  39519. static void my_Free_cb(void *ptr, const char* func, unsigned int line)
  39520. {
  39521. (void) func;
  39522. (void) line;
  39523. #else
  39524. static void my_Free_cb(void *ptr)
  39525. {
  39526. #endif
  39527. free_cnt++;
  39528. #ifndef WOLFSSL_NO_MALLOC
  39529. free(ptr);
  39530. #else
  39531. WOLFSSL_MSG("No free available");
  39532. (void)ptr;
  39533. #endif
  39534. }
  39535. #ifdef WOLFSSL_DEBUG_MEMORY
  39536. static void *my_Realloc_cb(void *ptr, size_t size, const char* func, unsigned int line)
  39537. {
  39538. (void) func;
  39539. (void) line;
  39540. #else
  39541. static void *my_Realloc_cb(void *ptr, size_t size)
  39542. {
  39543. #endif
  39544. realloc_cnt++;
  39545. #ifndef WOLFSSL_NO_MALLOC
  39546. return realloc(ptr, size);
  39547. #else
  39548. WOLFSSL_MSG("No realloc available");
  39549. (void)ptr;
  39550. (void)size;
  39551. return NULL;
  39552. #endif
  39553. }
  39554. #endif /* !WOLFSSL_NO_MALLOC */
  39555. WOLFSSL_TEST_SUBROUTINE int memcb_test(void)
  39556. {
  39557. int ret = 0;
  39558. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  39559. !defined(WOLFSSL_STATIC_MEMORY)
  39560. byte* b = NULL;
  39561. #endif
  39562. wolfSSL_Malloc_cb mc;
  39563. wolfSSL_Free_cb fc;
  39564. wolfSSL_Realloc_cb rc;
  39565. /* Save existing memory callbacks */
  39566. if (wolfSSL_GetAllocators(&mc, &fc, &rc) != 0)
  39567. return -13800;
  39568. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  39569. !defined(WOLFSSL_STATIC_MEMORY)
  39570. /* test realloc */
  39571. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39572. if (b == NULL) {
  39573. ERROR_OUT(-13801, exit_memcb);
  39574. }
  39575. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39576. b = NULL;
  39577. /* Use API. */
  39578. if (wolfSSL_SetAllocators((wolfSSL_Malloc_cb)my_Malloc_cb,
  39579. (wolfSSL_Free_cb)my_Free_cb,
  39580. (wolfSSL_Realloc_cb)my_Realloc_cb) != 0) {
  39581. ERROR_OUT(-13802, exit_memcb);
  39582. }
  39583. b = (byte*)XMALLOC(1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39584. b = (byte*)XREALLOC(b, 1024, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39585. XFREE(b, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39586. #ifndef WOLFSSL_STATIC_MEMORY
  39587. #ifndef WOLFSSL_CHECK_MEM_ZERO
  39588. if (malloc_cnt != 1 || free_cnt != 1 || realloc_cnt != 1)
  39589. #else
  39590. /* Checking zeroized memory means realloc is a malloc and free. */
  39591. if (malloc_cnt != 2 || free_cnt != 2 || realloc_cnt != 0)
  39592. #endif
  39593. #else
  39594. if (malloc_cnt != 0 || free_cnt != 0 || realloc_cnt != 0)
  39595. #endif
  39596. ret = -13803;
  39597. #endif /* !WOLFSSL_NO_MALLOC */
  39598. #if !defined(WOLFSSL_NO_MALLOC) && !defined(WOLFSSL_LINUXKM) && \
  39599. !defined(WOLFSSL_STATIC_MEMORY)
  39600. exit_memcb:
  39601. /* reset malloc/free/realloc counts */
  39602. malloc_cnt = 0;
  39603. free_cnt = 0;
  39604. realloc_cnt = 0;
  39605. #endif
  39606. /* restore memory callbacks */
  39607. wolfSSL_SetAllocators(mc, fc, rc);
  39608. return ret;
  39609. }
  39610. #endif /* USE_WOLFSSL_MEMORY && !WOLFSSL_NO_MALLOC */
  39611. #ifdef WOLFSSL_IMX6_CAAM_BLOB
  39612. WOLFSSL_TEST_SUBROUTINE int blob_test(void)
  39613. {
  39614. int ret = 0;
  39615. byte out[112];
  39616. byte blob[112];
  39617. word32 outSz;
  39618. WOLFSSL_SMALL_STACK_STATIC const byte iv[] =
  39619. {
  39620. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  39621. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  39622. };
  39623. WOLFSSL_SMALL_STACK_STATIC const byte text[] =
  39624. {
  39625. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  39626. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  39627. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  39628. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  39629. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  39630. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  39631. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  39632. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  39633. };
  39634. XMEMSET(blob, 0, sizeof(blob));
  39635. XMEMSET(out, 0, sizeof(out));
  39636. outSz = sizeof(blob);
  39637. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  39638. if (ret != 0) {
  39639. ERROR_OUT(-13900, exit_blob);
  39640. }
  39641. blob[outSz - 2] += 1;
  39642. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  39643. if (ret == 0) { /* should fail with altered blob */
  39644. ERROR_OUT(-13901, exit_blob);
  39645. }
  39646. XMEMSET(blob, 0, sizeof(blob));
  39647. outSz = sizeof(blob);
  39648. ret = wc_caamCreateBlob((byte*)iv, sizeof(iv), blob, &outSz);
  39649. if (ret != 0) {
  39650. ERROR_OUT(-13902, exit_blob);
  39651. }
  39652. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  39653. if (ret != 0) {
  39654. ERROR_OUT(-13903, exit_blob);
  39655. }
  39656. if (XMEMCMP(out, iv, sizeof(iv))) {
  39657. ERROR_OUT(-13904, exit_blob);
  39658. }
  39659. XMEMSET(blob, 0, sizeof(blob));
  39660. outSz = sizeof(blob);
  39661. ret = wc_caamCreateBlob((byte*)text, sizeof(text), blob, &outSz);
  39662. if (ret != 0) {
  39663. ERROR_OUT(-13905, exit_blob);
  39664. }
  39665. ret = wc_caamOpenBlob(blob, outSz, out, &outSz);
  39666. if (ret != 0) {
  39667. ERROR_OUT(-13906, exit_blob);
  39668. }
  39669. if (XMEMCMP(out, text, sizeof(text))) {
  39670. ERROR_OUT(-13907, exit_blob);
  39671. }
  39672. exit_blob:
  39673. return ret;
  39674. }
  39675. #endif /* WOLFSSL_IMX6_CAAM_BLOB */
  39676. #ifdef WOLF_CRYPTO_CB
  39677. /* Example custom context for crypto callback */
  39678. typedef struct {
  39679. int exampleVar; /* flag for testing if only crypt is enabled. */
  39680. } myCryptoDevCtx;
  39681. #ifdef WOLF_CRYPTO_CB_ONLY_RSA
  39682. /* Testing rsa cb when CB_ONLY_RSA is enabled
  39683. * When CB_ONLY_RSA is enabled, software imple. is not available.
  39684. *
  39685. * ctx callback ctx
  39686. * returen 0 on success, otherwise return -8000 - -8007
  39687. */
  39688. static int rsa_onlycb_test(myCryptoDevCtx *ctx)
  39689. {
  39690. int ret = 0;
  39691. #if !defined(NO_RSA)
  39692. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39693. RsaKey *key = (RsaKey *)XMALLOC(sizeof *key,
  39694. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39695. byte* tmp = NULL;
  39696. #else
  39697. RsaKey key[1];
  39698. byte tmp[FOURK_BUF];
  39699. #endif
  39700. size_t bytes;
  39701. const word32 inLen = (word32)TEST_STRING_SZ;
  39702. word32 idx = 0;
  39703. word32 sigSz;
  39704. WOLFSSL_SMALL_STACK_STATIC const byte in[] = TEST_STRING;
  39705. byte out[RSA_TEST_BYTES];
  39706. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  39707. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096) && \
  39708. !defined(NO_FILESYSTEM)
  39709. XFILE file;
  39710. #endif
  39711. #ifdef WOLFSSL_KEY_GEN
  39712. WC_RNG rng;
  39713. #endif
  39714. #ifdef USE_CERT_BUFFERS_1024
  39715. bytes = (size_t)sizeof_client_key_der_1024;
  39716. if (bytes < (size_t)sizeof_client_cert_der_1024)
  39717. bytes = (size_t)sizeof_client_cert_der_1024;
  39718. #elif defined(USE_CERT_BUFFERS_2048)
  39719. bytes = (size_t)sizeof_client_key_der_2048;
  39720. if (bytes < (size_t)sizeof_client_cert_der_2048)
  39721. bytes = (size_t)sizeof_client_cert_der_2048;
  39722. #elif defined(USE_CERT_BUFFERS_3072)
  39723. bytes = (size_t)sizeof_client_key_der_3072;
  39724. if (bytes < (size_t)sizeof_client_cert_der_3072)
  39725. bytes = (size_t)sizeof_client_cert_der_3072;
  39726. #elif defined(USE_CERT_BUFFERS_4096)
  39727. bytes = (size_t)sizeof_client_key_der_4096;
  39728. if (bytes < (size_t)sizeof_client_cert_der_4096)
  39729. bytes = (size_t)sizeof_client_cert_der_4096;
  39730. #else
  39731. bytes = FOURK_BUF;
  39732. #endif
  39733. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39734. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39735. if (tmp == NULL)
  39736. ERROR_OUT(-8000, exit_onlycb);
  39737. #endif
  39738. #ifdef USE_CERT_BUFFERS_1024
  39739. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  39740. #elif defined(USE_CERT_BUFFERS_2048)
  39741. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  39742. #elif defined(USE_CERT_BUFFERS_3072)
  39743. XMEMCPY(tmp, client_key_der_3072, (size_t)sizeof_client_key_der_3072);
  39744. #elif defined(USE_CERT_BUFFERS_4096)
  39745. XMEMCPY(tmp, client_key_der_4096, (size_t)sizeof_client_key_der_4096);
  39746. #elif !defined(NO_FILESYSTEM)
  39747. file = XFOPEN(clientKey, "rb");
  39748. if (!file) {
  39749. err_sys("can't open ./certs/client-key.der, "
  39750. "Please run from wolfSSL home dir", -40);
  39751. ERROR_OUT(-8001, exit_onlycb);
  39752. }
  39753. bytes = XFREAD(tmp, 1, FOURK_BUF, file);
  39754. XFCLOSE(file);
  39755. #endif
  39756. #ifdef WOLFSSL_KEY_GEN
  39757. /* wc_CryptoCb_MakeRsaKey cb test, no actual making key
  39758. * wc_MakeRsaKey() -> rsa cb ->
  39759. * myCryptoDevCb -> wc_MakeRsaKey(CBONLY_TEST_DEVID)
  39760. * wc_MakeRsaKey(CBONLY_TEST_DEVID) expects to return 0(success)
  39761. */
  39762. ctx->exampleVar = 99;
  39763. ret = wc_MakeRsaKey(key, keySz, WC_RSA_EXPONENT, rng);
  39764. if (ret != 0) {
  39765. ERROR_OUT(-8002, exit_onlycb);
  39766. }
  39767. /* wc_MakeRsaKey() -> rsa cb ->
  39768. * myCryptoDevCb -> wc_MakeRsaKey(INVALID_DEVID)
  39769. * wc_MakeRsaKey(CBONLY_TEST_DEVID) expects to return NO_VALID_DEVID(failure)
  39770. */
  39771. ctx->exampleVar = 1;
  39772. ret = wc_MakeRsaKey(key, keySz, WC_RSA_EXPONENT, rng);
  39773. if (ret != NO_VALID_DEVID) {
  39774. ERROR_OUT(-8003, exit_onlycb);
  39775. } else
  39776. /* reset return code */
  39777. ret = 0;
  39778. #endif
  39779. ret = wc_InitRsaKey_ex(key, HEAP_HINT, devId);
  39780. if (ret != 0) {
  39781. ERROR_OUT(-8004, exit_onlycb);
  39782. }
  39783. ret = wc_RsaPrivateKeyDecode(tmp, &idx, key, (word32)bytes);
  39784. if (ret != 0) {
  39785. ERROR_OUT(-8005, exit_onlycb);
  39786. }
  39787. sigSz = (word32)wc_RsaEncryptSize(key);
  39788. /* wc_CryptoCb_Rsa cb test, no actual rsa operation */
  39789. if (ret == 0) {
  39790. /* wc_SignatureGenerate() -> rsa cb ->
  39791. * myCryptoDevCb -> wc_RsaFunction(CBONLY_TEST_DEVID)
  39792. * wc_RsaFunction(CBONLY_TEST_DEVID) expects to return 0(success)
  39793. */
  39794. ctx->exampleVar = 99;
  39795. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  39796. in, inLen, out, &sigSz, key, sizeof(*key), NULL);
  39797. if (ret != 0) {
  39798. ERROR_OUT(-8006, exit_onlycb);
  39799. }
  39800. }
  39801. if (ret == 0) {
  39802. /* wc_SignatureGenerate() -> rsa cb ->
  39803. * myCryptoDevCb -> wc_RsaFunction(INVALID_DEVID)
  39804. * wc_SignatureGenerate(INVALID_DEVID) expects to
  39805. * return NO_VALID_DEVID(failure)
  39806. */
  39807. ctx->exampleVar = 1;
  39808. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA,
  39809. in, inLen, out, &sigSz, key, sizeof(*key), NULL);
  39810. if (ret != NO_VALID_DEVID) {
  39811. ERROR_OUT(-8007, exit_onlycb);
  39812. } else
  39813. /* reset return code */
  39814. ret = 0;
  39815. }
  39816. exit_onlycb:
  39817. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39818. if (key != NULL) {
  39819. wc_FreeRsaKey(key);
  39820. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39821. }
  39822. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39823. #else
  39824. wc_FreeRsaKey(key);
  39825. #endif
  39826. #endif
  39827. return ret;
  39828. }
  39829. #endif
  39830. #ifdef WOLF_CRYPTO_CB_ONLY_ECC
  39831. /* Testing rsa cb when CB_ONLY_ECC is enabled
  39832. * When CB_ONLY_ECC is enabled, software imple. is not available.
  39833. *
  39834. * ctx callback ctx
  39835. * returen 0 on success, otherwise return -8008 - -8018
  39836. */
  39837. static int ecc_onlycb_test(myCryptoDevCtx *ctx)
  39838. {
  39839. int ret = 0;
  39840. #if defined(HAVE_ECC)
  39841. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39842. ecc_key* key = (ecc_key *)XMALLOC(sizeof *key,
  39843. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39844. ecc_key* pub = (ecc_key *)XMALLOC(sizeof *pub,
  39845. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39846. byte* out = (byte*)XMALLOC(sizeof(byte),
  39847. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  39848. #ifdef OPENSSL_EXTRA
  39849. byte* check = (byte*)XMALLOC(sizeof(byte)*(256), HEAP_HINT,
  39850. DYNAMIC_TYPE_TMP_BUFFER);
  39851. #endif
  39852. #else
  39853. ecc_key key[1];
  39854. ecc_key pub[1];
  39855. byte out[256];
  39856. #ifdef OPENSSL_EXTRA
  39857. unsigned char check[256];
  39858. #endif
  39859. #endif
  39860. #ifdef OPENSSL_EXTRA
  39861. EVP_PKEY* privKey = NULL;
  39862. EVP_PKEY* pubKey = NULL;
  39863. ecc_key* pkey;
  39864. EVP_MD_CTX mdCtx;
  39865. const char testData[] = "Hi There";
  39866. size_t checkSz = -1;
  39867. const unsigned char* cp;
  39868. const unsigned char* p;
  39869. const unsigned char check_v[256] = {
  39870. 0x30,0x45,0x02,0x20,0x1b,0x5c,0x2a,0xf0,0x18,0x09,
  39871. 0x74,0x65,0xa1,0x04,0x76,0x3a,0xce,0xcc,0xe5,0x34,
  39872. 0x5e,0x89,0xed,0x40,0x1e,0x5a,0xb1,0x53,0xb4,0xff,
  39873. 0xc7,0x18,0xfe,0x0f,0xc7,0xa6,0x02,0x21,0x00,0xe5,
  39874. 0x70,0x21,0xfc,0xf9,0x63,0x36,0xfd,0x16,0x18,0x08,
  39875. 0x9a,0x63,0x61,0x0f,0xe7,0x7c,0xa3,0xc9,0x14,0xa3,
  39876. 0x30,0x87,0xf7,0xf5,0x70,0x19,0xaf,0x56,0x96,0x9b,
  39877. 0xd8,0x64,0xcd,0xd9,0xff,0x7b,0x2a,0x55,0x52,0xca,
  39878. 0x41,0xb2,0xa6,0xa4,0x8a,0x3b,0x02,0x20,0x8c,0xc5,
  39879. 0xf9,0xc1,0x7d,0x2a,0x65,0x6c,0xe6,0x5a,0xe3,0x76,
  39880. 0x9b,0xab,0x0b,0x9f,0xaf,0x62,0x5d,0xb2,0x60,0xd7,
  39881. 0xeb,0xb4,0x1b,0x73,0xdc,0x01,0x7d,0x7b,0xab,0xc1,
  39882. 0x0c,0x74,0x96,0x41,0xe6,0x3f,0xc5,0x86,0xe6,0x7d,
  39883. 0x2b,0x9d,0x54,0x6b,0xcd,0x31,0x35,0x1f,0xdb,0x49,
  39884. 0x1f,0x32,0x34,0xf8,0x57,0x12,0x86,0x5c,0x0e,0x80,
  39885. 0x55,0x8d,0xff,0xd8,0xbd,0xdf,0x32,0x26,0x62,0x42,
  39886. 0x09,0xda,0xf7,0x74,0xf2,0x3f,0xe6,0xf1,0x77,0x82,
  39887. 0xce,0xe4,0xbb,0x61,0xa6,0xc0,0x17,0x0c,0x6c,0x47,
  39888. 0x2a,0x40,0x1c,0x2b,0xe0,0x98,0x3b,0xbf,0xc6,0xf8,
  39889. 0x6d,0xfd,0xd0,0xfa,0xc1,0x02,0xfb,0x5f,0xfb,0xb0,
  39890. 0xcb,0xd9,0xa3,0x59,0x94,0xe9,0x0f,0x74,0xbb,0x3f,
  39891. 0x64,0xa3,0x83,0xc4,0x2b,0xf7,0xd2,0x97,0xbf,0x3b,
  39892. 0xcf,0xbb,0x60,0x81,0x33,0x94,0xfa,0x0d,0x35,0xd2,
  39893. 0x3d,0xb9,0x99,0xe3,0x12,0xf8,0xf4,0xa3,0x74,0xf4,
  39894. 0x94,0x1d,0x7a,0x66,0xf8,0xd1,0x1d,0xcf,0xb0,0x48,
  39895. 0xef,0x8c,0x94,0x6f,0xdd,0x62,
  39896. };
  39897. #endif
  39898. WC_RNG rng;
  39899. EncryptedInfo encInfo;
  39900. int keyFormat = 0;
  39901. word32 keyIdx = 0;
  39902. byte in[] = "Everyone gets Friday off. ecc p";
  39903. word32 inLen = (word32)XSTRLEN((char*)in);
  39904. word32 outLen;
  39905. int verify;
  39906. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  39907. if (key == NULL || pub == NULL) {
  39908. ERROR_OUT(-8008, exit_onlycb);
  39909. }
  39910. #endif
  39911. ret = wc_ecc_init_ex(key, HEAP_HINT, devId);
  39912. if (ret != 0) {
  39913. ERROR_OUT(-8009, exit_onlycb);
  39914. }
  39915. /* wc_CryptoCb_MakeEccKey cb test, , no actual testing */
  39916. ctx->exampleVar = 99;
  39917. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, key);
  39918. if (ret != 0) {
  39919. ERROR_OUT(-8010, exit_onlycb);
  39920. }
  39921. ctx->exampleVar = 1;
  39922. ret = wc_ecc_make_key(&rng, ECC_KEYGEN_SIZE, key);
  39923. if (ret != NO_VALID_DEVID) {
  39924. ERROR_OUT(-8011, exit_onlycb);
  39925. } else
  39926. /* reset return code */
  39927. ret = 0;
  39928. #ifdef USE_CERT_BUFFERS_256
  39929. if (ret == 0) {
  39930. /* load ECC private key and perform private transform */
  39931. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &keyIdx,
  39932. key, sizeof_ecc_key_der_256);
  39933. }
  39934. if (ret != 0) {
  39935. ERROR_OUT(-8012, exit_onlycb);
  39936. }
  39937. /* wc_CryptoCb_EccSign cb test, no actual testing */
  39938. ctx->exampleVar = 99;
  39939. if (ret == 0) {
  39940. ret = wc_ecc_sign_hash(in, inLen, out, &outLen, &rng, key);
  39941. }
  39942. if (ret != 0) {
  39943. ERROR_OUT(-8013, exit_onlycb);
  39944. }
  39945. ctx->exampleVar = 1;
  39946. if (ret == 0) {
  39947. ret = wc_ecc_sign_hash(in, inLen, out, &outLen, &rng, key);
  39948. }
  39949. if (ret != NO_VALID_DEVID) {
  39950. ERROR_OUT(-8014, exit_onlycb);
  39951. }
  39952. else
  39953. ret = 0;
  39954. /* wc_CryptoCb_EccVerify cb test, no actual testing */
  39955. ctx->exampleVar = 99;
  39956. if (ret == 0) {
  39957. ret = wc_ecc_verify_hash(in, inLen, out, outLen, &verify, key);
  39958. }
  39959. if (ret != 0) {
  39960. ERROR_OUT(-8015, exit_onlycb);
  39961. }
  39962. ctx->exampleVar = 1;
  39963. if (ret == 0) {
  39964. ret = wc_ecc_verify_hash(in, inLen, out, outLen, &verify, key);
  39965. }
  39966. if (ret != NO_VALID_DEVID) {
  39967. ERROR_OUT(-8016, exit_onlycb);
  39968. }
  39969. else
  39970. ret = 0;
  39971. /* wc_CryptoCb_Ecdh cb test, no actual testing */
  39972. /* make public key for shared secret */
  39973. wc_ecc_init_ex(pub, HEAP_HINT, devId);
  39974. ctx->exampleVar = 99;
  39975. if (ret == 0) {
  39976. ret = wc_ecc_shared_secret(key, pub, out, &outLen);
  39977. }
  39978. if (ret != 0) {
  39979. ERROR_OUT(-8017, exit_onlycb);
  39980. }
  39981. ctx->exampleVar = 1;
  39982. if (ret == 0) {
  39983. ret = wc_ecc_shared_secret(key, pub, out, &outLen);
  39984. }
  39985. if (ret != NO_VALID_DEVID) {
  39986. ERROR_OUT(-8018, exit_onlycb);
  39987. }
  39988. else
  39989. ret = 0;
  39990. #ifdef OPENSSL_EXTRA
  39991. (void)pkey;
  39992. cp = ecc_clikey_der_256;
  39993. privKey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  39994. sizeof_ecc_clikey_der_256);
  39995. if (privKey == NULL) {
  39996. ERROR_OUT(-8019, exit_onlycb);
  39997. }
  39998. pkey = (ecc_key*)privKey->ecc->internal;
  39999. pkey->devId = devId;
  40000. p = ecc_clikeypub_der_256;
  40001. pubKey = d2i_PUBKEY(NULL, &p, sizeof_ecc_clikeypub_der_256);
  40002. if (pubKey == NULL) {
  40003. ERROR_OUT(-8020, exit_onlycb);
  40004. }
  40005. pkey = (ecc_key*)pubKey->ecc->internal;
  40006. pkey->devId = devId;
  40007. /* sign */
  40008. EVP_MD_CTX_init(&mdCtx);
  40009. ret = EVP_DigestSignInit(&mdCtx, NULL, EVP_sha256(), NULL, privKey);
  40010. if (ret != WOLFSSL_SUCCESS) {
  40011. ERROR_OUT(-8021, exit_onlycb);
  40012. }
  40013. ret = EVP_DigestSignUpdate(&mdCtx, testData,
  40014. (unsigned int)XSTRLEN(testData));
  40015. if (ret != WOLFSSL_SUCCESS) {
  40016. ERROR_OUT(-8022, exit_onlycb);
  40017. }
  40018. ret = EVP_DigestSignFinal(&mdCtx, NULL, &checkSz);
  40019. if (ret != WOLFSSL_SUCCESS) {
  40020. ERROR_OUT(-8023, exit_onlycb);
  40021. }
  40022. ctx->exampleVar = 99;
  40023. ret = EVP_DigestSignFinal(&mdCtx, check, &checkSz);
  40024. /* just called crypt callback as dummy
  40025. * EVP_DigestSignFinal returns 0 internally.
  40026. */
  40027. if (ret != 0) {
  40028. ERROR_OUT(-8024, exit_onlycb);
  40029. }
  40030. ctx->exampleVar = 1;
  40031. ret = EVP_DigestSignFinal(&mdCtx, check, &checkSz);
  40032. /* just called crypt callback as dummy
  40033. * EVP_DigestSignFinal returns 0 internally.
  40034. */
  40035. if (ret != 0) {
  40036. ERROR_OUT(-8025, exit_onlycb);
  40037. }
  40038. /* restore checkSz for verify */
  40039. checkSz = 71;
  40040. ret = EVP_MD_CTX_cleanup(&mdCtx);
  40041. if (ret != SSL_SUCCESS) {
  40042. ERROR_OUT(-8026, exit_onlycb);
  40043. }
  40044. /* verify */
  40045. EVP_MD_CTX_init(&mdCtx);
  40046. if (ret == SSL_SUCCESS) {
  40047. ret = EVP_DigestVerifyInit(&mdCtx, NULL, EVP_sha256(), NULL, pubKey);
  40048. }
  40049. if (ret != WOLFSSL_SUCCESS) {
  40050. ERROR_OUT(-8027, exit_onlycb);
  40051. }
  40052. if (ret == WOLFSSL_SUCCESS) {
  40053. ret = EVP_DigestVerifyUpdate(&mdCtx, testData,
  40054. (unsigned int)XSTRLEN(testData));
  40055. }
  40056. if (ret != WOLFSSL_SUCCESS) {
  40057. ERROR_OUT(-8028, exit_onlycb);
  40058. }
  40059. ctx->exampleVar = 99;
  40060. ret = EVP_DigestVerifyFinal(&mdCtx, check_v, checkSz);
  40061. /* just called crypt callback as dummy
  40062. * EVP_DigestSignFinal returns 0 internally.
  40063. */
  40064. if (ret != 0) {
  40065. ERROR_OUT(-8029, exit_onlycb);
  40066. }
  40067. ctx->exampleVar = 1;
  40068. ret = EVP_DigestVerifyFinal(&mdCtx, check_v, checkSz);
  40069. /* just called crypt callback as dummy
  40070. * EVP_DigestVerifyFinal returns -1 internally rather than NO_VALID_DEVID.
  40071. */
  40072. if (ret != -1) {
  40073. ERROR_OUT(-8030, exit_onlycb);
  40074. }
  40075. ret = EVP_MD_CTX_cleanup(&mdCtx);
  40076. if (ret != SSL_SUCCESS) {
  40077. ERROR_OUT(-8031, exit_onlycb);
  40078. } else
  40079. ret = 0;
  40080. #endif
  40081. #else
  40082. (void)verify;
  40083. (void)outLen;
  40084. (void)inLen;
  40085. (void)out;
  40086. (void)pub;
  40087. #ifdef OPENSSL_EXTRA
  40088. (void)privKey;
  40089. (void)pubKey;
  40090. (void)mdCtx;
  40091. (void)check;
  40092. (void)checkSz;
  40093. (void)p;
  40094. #endif
  40095. #endif
  40096. (void)keyFormat;
  40097. (void)encInfo;
  40098. exit_onlycb:
  40099. #if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
  40100. if (key != NULL) {
  40101. wc_ecc_free(key);
  40102. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40103. }
  40104. if (pub != NULL) {
  40105. XFREE(pub, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40106. }
  40107. if (out != NULL) {
  40108. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40109. }
  40110. #ifdef OPENSSL_EXTRA
  40111. if (check) {
  40112. FREE(check, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40113. }
  40114. #endif
  40115. #else
  40116. wc_ecc_free(key);
  40117. #ifdef OPENSSL_EXTRA
  40118. if (privKey)
  40119. EVP_PKEY_free(privKey);
  40120. if (pubKey)
  40121. EVP_PKEY_free(pubKey);
  40122. #endif
  40123. #endif
  40124. #endif /* HAVE_ECC */
  40125. return ret;
  40126. }
  40127. #endif
  40128. /* Example crypto dev callback function that calls software version */
  40129. static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
  40130. {
  40131. int ret = NOT_COMPILED_IN; /* return this to bypass HW and use SW */
  40132. myCryptoDevCtx* myCtx = (myCryptoDevCtx*)ctx;
  40133. if (info == NULL)
  40134. return BAD_FUNC_ARG;
  40135. #ifdef DEBUG_WOLFSSL
  40136. printf("CryptoDevCb: Algo Type %d\n", info->algo_type);
  40137. #endif
  40138. if (info->algo_type == WC_ALGO_TYPE_RNG) {
  40139. #ifndef WC_NO_RNG
  40140. /* set devId to invalid, so software is used */
  40141. info->rng.rng->devId = INVALID_DEVID;
  40142. ret = wc_RNG_GenerateBlock(info->rng.rng,
  40143. info->rng.out, info->rng.sz);
  40144. /* reset devId */
  40145. info->rng.rng->devId = devIdArg;
  40146. #endif
  40147. }
  40148. else if (info->algo_type == WC_ALGO_TYPE_SEED) {
  40149. #ifndef WC_NO_RNG
  40150. ALIGN32 static byte seed[sizeof(word32)] = { 0x00, 0x00, 0x00, 0x01 };
  40151. word32* seedWord32 = (word32*)seed;
  40152. word32 len;
  40153. /* wc_GenerateSeed is a local symbol so we need to fake the entropy. */
  40154. while (info->seed.sz > 0) {
  40155. len = (word32)sizeof(seed);
  40156. if (info->seed.sz < len)
  40157. len = info->seed.sz;
  40158. XMEMCPY(info->seed.seed, seed, sizeof(seed));
  40159. info->seed.seed += len;
  40160. info->seed.sz -= len;
  40161. (*seedWord32)++;
  40162. }
  40163. ret = 0;
  40164. #endif
  40165. }
  40166. else if (info->algo_type == WC_ALGO_TYPE_PK) {
  40167. #ifdef DEBUG_WOLFSSL
  40168. printf("CryptoDevCb: Pk Type %d\n", info->pk.type);
  40169. #endif
  40170. #ifndef NO_RSA
  40171. if (info->pk.type == WC_PK_TYPE_RSA) {
  40172. /* set devId to invalid, so software is used */
  40173. info->pk.rsa.key->devId = INVALID_DEVID;
  40174. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  40175. #ifdef DEBUG_WOLFSSL
  40176. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  40177. #endif
  40178. if (myCtx->exampleVar == 99) {
  40179. info->pk.rsa.key->devId = devIdArg;
  40180. return 0;
  40181. }
  40182. #endif
  40183. switch (info->pk.rsa.type) {
  40184. case RSA_PUBLIC_ENCRYPT:
  40185. case RSA_PUBLIC_DECRYPT:
  40186. /* perform software based RSA public op */
  40187. ret = wc_RsaFunction(
  40188. info->pk.rsa.in, info->pk.rsa.inLen,
  40189. info->pk.rsa.out, info->pk.rsa.outLen,
  40190. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  40191. break;
  40192. case RSA_PRIVATE_ENCRYPT:
  40193. case RSA_PRIVATE_DECRYPT:
  40194. /* perform software based RSA private op */
  40195. ret = wc_RsaFunction(
  40196. info->pk.rsa.in, info->pk.rsa.inLen,
  40197. info->pk.rsa.out, info->pk.rsa.outLen,
  40198. info->pk.rsa.type, info->pk.rsa.key, info->pk.rsa.rng);
  40199. break;
  40200. }
  40201. /* reset devId */
  40202. info->pk.rsa.key->devId = devIdArg;
  40203. }
  40204. #ifdef WOLFSSL_KEY_GEN
  40205. else if (info->pk.type == WC_PK_TYPE_RSA_KEYGEN) {
  40206. info->pk.rsakg.key->devId = INVALID_DEVID;
  40207. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  40208. #ifdef DEBUG_WOLFSSL
  40209. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  40210. #endif
  40211. if (myCtx->exampleVar == 99) {
  40212. info->pk.rsakg.key->devId = devIdArg;
  40213. return 0;
  40214. }
  40215. #endif
  40216. #ifdef HAVE_FIPS
  40217. for (;;) {
  40218. #endif
  40219. ret = wc_MakeRsaKey(info->pk.rsakg.key, info->pk.rsakg.size,
  40220. info->pk.rsakg.e, info->pk.rsakg.rng);
  40221. #ifdef HAVE_FIPS
  40222. if (ret == PRIME_GEN_E)
  40223. continue;
  40224. break;
  40225. }
  40226. #endif
  40227. /* reset devId */
  40228. info->pk.rsakg.key->devId = devIdArg;
  40229. }
  40230. #endif
  40231. #endif /* !NO_RSA */
  40232. #ifdef HAVE_ECC
  40233. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  40234. /* set devId to invalid, so software is used */
  40235. info->pk.eckg.key->devId = INVALID_DEVID;
  40236. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  40237. #ifdef DEBUG_WOLFSSL
  40238. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  40239. #endif
  40240. if (myCtx->exampleVar == 99) {
  40241. info->pk.eckg.key->devId = devIdArg;
  40242. return 0;
  40243. }
  40244. #endif
  40245. ret = wc_ecc_make_key_ex(info->pk.eckg.rng, info->pk.eckg.size,
  40246. info->pk.eckg.key, info->pk.eckg.curveId);
  40247. /* reset devId */
  40248. info->pk.eckg.key->devId = devIdArg;
  40249. }
  40250. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  40251. /* set devId to invalid, so software is used */
  40252. info->pk.eccsign.key->devId = INVALID_DEVID;
  40253. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  40254. #ifdef DEBUG_WOLFSSL
  40255. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  40256. #endif
  40257. if (myCtx->exampleVar == 99) {
  40258. info->pk.eccsign.key->devId = devIdArg;
  40259. return 0;
  40260. }
  40261. #endif
  40262. ret = wc_ecc_sign_hash(
  40263. info->pk.eccsign.in, info->pk.eccsign.inlen,
  40264. info->pk.eccsign.out, info->pk.eccsign.outlen,
  40265. info->pk.eccsign.rng, info->pk.eccsign.key);
  40266. /* reset devId */
  40267. info->pk.eccsign.key->devId = devIdArg;
  40268. }
  40269. else if (info->pk.type == WC_PK_TYPE_ECDSA_VERIFY) {
  40270. /* set devId to invalid, so software is used */
  40271. info->pk.eccverify.key->devId = INVALID_DEVID;
  40272. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  40273. #ifdef DEBUG_WOLFSSL
  40274. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  40275. #endif
  40276. if (myCtx->exampleVar == 99) {
  40277. info->pk.eccverify.key->devId = devIdArg;
  40278. return 0;
  40279. }
  40280. #endif
  40281. ret = wc_ecc_verify_hash(
  40282. info->pk.eccverify.sig, info->pk.eccverify.siglen,
  40283. info->pk.eccverify.hash, info->pk.eccverify.hashlen,
  40284. info->pk.eccverify.res, info->pk.eccverify.key);
  40285. /* reset devId */
  40286. info->pk.eccverify.key->devId = devIdArg;
  40287. }
  40288. else if (info->pk.type == WC_PK_TYPE_ECDH) {
  40289. /* set devId to invalid, so software is used */
  40290. info->pk.ecdh.private_key->devId = INVALID_DEVID;
  40291. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  40292. #ifdef DEBUG_WOLFSSL
  40293. printf("CryptoDevCb: exampleVar %d\n", myCtx->exampleVar);
  40294. #endif
  40295. if (myCtx->exampleVar == 99) {
  40296. info->pk.ecdh.private_key->devId = devIdArg;
  40297. return 0;
  40298. }
  40299. #endif
  40300. ret = wc_ecc_shared_secret(
  40301. info->pk.ecdh.private_key, info->pk.ecdh.public_key,
  40302. info->pk.ecdh.out, info->pk.ecdh.outlen);
  40303. /* reset devId */
  40304. info->pk.ecdh.private_key->devId = devIdArg;
  40305. }
  40306. #endif /* HAVE_ECC */
  40307. #ifdef HAVE_CURVE25519
  40308. if (info->pk.type == WC_PK_TYPE_CURVE25519_KEYGEN) {
  40309. /* set devId to invalid, so software is used */
  40310. info->pk.curve25519kg.key->devId = INVALID_DEVID;
  40311. ret = wc_curve25519_make_key(info->pk.curve25519kg.rng,
  40312. info->pk.curve25519kg.size, info->pk.curve25519kg.key);
  40313. /* reset devId */
  40314. info->pk.curve25519kg.key->devId = devIdArg;
  40315. }
  40316. else if (info->pk.type == WC_PK_TYPE_CURVE25519) {
  40317. /* set devId to invalid, so software is used */
  40318. info->pk.curve25519.private_key->devId = INVALID_DEVID;
  40319. ret = wc_curve25519_shared_secret_ex(
  40320. info->pk.curve25519.private_key, info->pk.curve25519.public_key,
  40321. info->pk.curve25519.out, info->pk.curve25519.outlen,
  40322. info->pk.curve25519.endian);
  40323. /* reset devId */
  40324. info->pk.curve25519.private_key->devId = devIdArg;
  40325. }
  40326. #endif /* HAVE_CURVE25519 */
  40327. #ifdef HAVE_ED25519
  40328. if (info->pk.type == WC_PK_TYPE_ED25519_KEYGEN) {
  40329. /* set devId to invalid, so software is used */
  40330. info->pk.ed25519kg.key->devId = INVALID_DEVID;
  40331. ret = wc_ed25519_make_key(info->pk.ed25519kg.rng,
  40332. info->pk.ed25519kg.size, info->pk.ed25519kg.key);
  40333. /* reset devId */
  40334. info->pk.ed25519kg.key->devId = devIdArg;
  40335. }
  40336. #ifdef HAVE_ED25519_SIGN
  40337. else if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  40338. /* set devId to invalid, so software is used */
  40339. info->pk.ed25519sign.key->devId = INVALID_DEVID;
  40340. ret = wc_ed25519_sign_msg_ex(
  40341. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  40342. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  40343. info->pk.ed25519sign.key, info->pk.ed25519sign.type,
  40344. info->pk.ed25519sign.context, info->pk.ed25519sign.contextLen);
  40345. /* reset devId */
  40346. info->pk.ed25519sign.key->devId = devIdArg;
  40347. }
  40348. #endif
  40349. #ifdef HAVE_ED25519_VERIFY
  40350. else if (info->pk.type == WC_PK_TYPE_ED25519_VERIFY) {
  40351. /* set devId to invalid, so software is used */
  40352. info->pk.ed25519verify.key->devId = INVALID_DEVID;
  40353. ret = wc_ed25519_verify_msg_ex(
  40354. info->pk.ed25519verify.sig, info->pk.ed25519verify.sigLen,
  40355. info->pk.ed25519verify.msg, info->pk.ed25519verify.msgLen,
  40356. info->pk.ed25519verify.res, info->pk.ed25519verify.key,
  40357. info->pk.ed25519verify.type, info->pk.ed25519verify.context,
  40358. info->pk.ed25519verify.contextLen);
  40359. /* reset devId */
  40360. info->pk.ed25519verify.key->devId = devIdArg;
  40361. }
  40362. #endif
  40363. #endif /* HAVE_ED25519 */
  40364. }
  40365. else if (info->algo_type == WC_ALGO_TYPE_CIPHER) {
  40366. #if !defined(NO_AES) || !defined(NO_DES3)
  40367. #ifdef HAVE_AESGCM
  40368. if (info->cipher.type == WC_CIPHER_AES_GCM) {
  40369. if (info->cipher.enc) {
  40370. /* set devId to invalid, so software is used */
  40371. info->cipher.aesgcm_enc.aes->devId = INVALID_DEVID;
  40372. ret = wc_AesGcmEncrypt(
  40373. info->cipher.aesgcm_enc.aes,
  40374. info->cipher.aesgcm_enc.out,
  40375. info->cipher.aesgcm_enc.in,
  40376. info->cipher.aesgcm_enc.sz,
  40377. info->cipher.aesgcm_enc.iv,
  40378. info->cipher.aesgcm_enc.ivSz,
  40379. info->cipher.aesgcm_enc.authTag,
  40380. info->cipher.aesgcm_enc.authTagSz,
  40381. info->cipher.aesgcm_enc.authIn,
  40382. info->cipher.aesgcm_enc.authInSz);
  40383. /* reset devId */
  40384. info->cipher.aesgcm_enc.aes->devId = devIdArg;
  40385. }
  40386. else {
  40387. /* set devId to invalid, so software is used */
  40388. info->cipher.aesgcm_dec.aes->devId = INVALID_DEVID;
  40389. ret = wc_AesGcmDecrypt(
  40390. info->cipher.aesgcm_dec.aes,
  40391. info->cipher.aesgcm_dec.out,
  40392. info->cipher.aesgcm_dec.in,
  40393. info->cipher.aesgcm_dec.sz,
  40394. info->cipher.aesgcm_dec.iv,
  40395. info->cipher.aesgcm_dec.ivSz,
  40396. info->cipher.aesgcm_dec.authTag,
  40397. info->cipher.aesgcm_dec.authTagSz,
  40398. info->cipher.aesgcm_dec.authIn,
  40399. info->cipher.aesgcm_dec.authInSz);
  40400. /* reset devId */
  40401. info->cipher.aesgcm_dec.aes->devId = devIdArg;
  40402. }
  40403. }
  40404. #endif /* HAVE_AESGCM */
  40405. #ifdef HAVE_AES_CBC
  40406. if (info->cipher.type == WC_CIPHER_AES_CBC) {
  40407. if (info->cipher.enc) {
  40408. /* set devId to invalid, so software is used */
  40409. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  40410. ret = wc_AesCbcEncrypt(
  40411. info->cipher.aescbc.aes,
  40412. info->cipher.aescbc.out,
  40413. info->cipher.aescbc.in,
  40414. info->cipher.aescbc.sz);
  40415. /* reset devId */
  40416. info->cipher.aescbc.aes->devId = devIdArg;
  40417. }
  40418. else {
  40419. /* set devId to invalid, so software is used */
  40420. info->cipher.aescbc.aes->devId = INVALID_DEVID;
  40421. ret = wc_AesCbcDecrypt(
  40422. info->cipher.aescbc.aes,
  40423. info->cipher.aescbc.out,
  40424. info->cipher.aescbc.in,
  40425. info->cipher.aescbc.sz);
  40426. /* reset devId */
  40427. info->cipher.aescbc.aes->devId = devIdArg;
  40428. }
  40429. }
  40430. #endif /* HAVE_AES_CBC */
  40431. #if defined(HAVE_AES_ECB) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  40432. if (info->cipher.type == WC_CIPHER_AES_ECB) {
  40433. if (info->cipher.enc) {
  40434. /* set devId to invalid, so software is used */
  40435. info->cipher.aesecb.aes->devId = INVALID_DEVID;
  40436. ret = wc_AesEcbEncrypt(
  40437. info->cipher.aesecb.aes,
  40438. info->cipher.aesecb.out,
  40439. info->cipher.aesecb.in,
  40440. info->cipher.aesecb.sz);
  40441. /* reset devId */
  40442. info->cipher.aesecb.aes->devId = devIdArg;
  40443. }
  40444. else {
  40445. /* set devId to invalid, so software is used */
  40446. info->cipher.aesecb.aes->devId = INVALID_DEVID;
  40447. ret = wc_AesEcbDecrypt(
  40448. info->cipher.aesecb.aes,
  40449. info->cipher.aesecb.out,
  40450. info->cipher.aesecb.in,
  40451. info->cipher.aesecb.sz);
  40452. /* reset devId */
  40453. info->cipher.aesecb.aes->devId = devIdArg;
  40454. }
  40455. }
  40456. #endif /* HAVE_AES_ECB */
  40457. #if defined(WOLFSSL_AES_COUNTER) && !defined(HAVE_FIPS) && \
  40458. !defined(HAVE_SELFTEST)
  40459. if (info->cipher.type == WC_CIPHER_AES_CTR) {
  40460. /* set devId to invalid, so software is used */
  40461. info->cipher.aesctr.aes->devId = INVALID_DEVID;
  40462. ret = wc_AesCtrEncrypt(
  40463. info->cipher.aesctr.aes,
  40464. info->cipher.aesctr.out,
  40465. info->cipher.aesctr.in,
  40466. info->cipher.aesctr.sz);
  40467. /* reset devId */
  40468. info->cipher.aesctr.aes->devId = devIdArg;
  40469. }
  40470. #endif /* WOLFSSL_AES_COUNTER */
  40471. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  40472. if (info->cipher.type == WC_CIPHER_AES_CCM) {
  40473. if (info->cipher.enc) {
  40474. /* set devId to invalid, so software is used */
  40475. info->cipher.aesccm_enc.aes->devId = INVALID_DEVID;
  40476. ret = wc_AesCcmEncrypt(
  40477. info->cipher.aesccm_enc.aes,
  40478. info->cipher.aesccm_enc.out,
  40479. info->cipher.aesccm_enc.in,
  40480. info->cipher.aesccm_enc.sz,
  40481. info->cipher.aesccm_enc.nonce,
  40482. info->cipher.aesccm_enc.nonceSz,
  40483. info->cipher.aesccm_enc.authTag,
  40484. info->cipher.aesccm_enc.authTagSz,
  40485. info->cipher.aesccm_enc.authIn,
  40486. info->cipher.aesccm_enc.authInSz);
  40487. /* reset devId */
  40488. info->cipher.aesccm_enc.aes->devId = devIdArg;
  40489. }
  40490. else {
  40491. /* set devId to invalid, so software is used */
  40492. info->cipher.aesccm_dec.aes->devId = INVALID_DEVID;
  40493. ret = wc_AesCcmDecrypt(
  40494. info->cipher.aesccm_dec.aes,
  40495. info->cipher.aesccm_dec.out,
  40496. info->cipher.aesccm_dec.in,
  40497. info->cipher.aesccm_dec.sz,
  40498. info->cipher.aesccm_dec.nonce,
  40499. info->cipher.aesccm_dec.nonceSz,
  40500. info->cipher.aesccm_dec.authTag,
  40501. info->cipher.aesccm_dec.authTagSz,
  40502. info->cipher.aesccm_dec.authIn,
  40503. info->cipher.aesccm_dec.authInSz);
  40504. /* reset devId */
  40505. info->cipher.aesccm_dec.aes->devId = devIdArg;
  40506. }
  40507. }
  40508. #endif
  40509. #ifndef NO_DES3
  40510. if (info->cipher.type == WC_CIPHER_DES3) {
  40511. if (info->cipher.enc) {
  40512. /* set devId to invalid, so software is used */
  40513. info->cipher.des3.des->devId = INVALID_DEVID;
  40514. ret = wc_Des3_CbcEncrypt(
  40515. info->cipher.des3.des,
  40516. info->cipher.des3.out,
  40517. info->cipher.des3.in,
  40518. info->cipher.des3.sz);
  40519. /* reset devId */
  40520. info->cipher.des3.des->devId = devIdArg;
  40521. }
  40522. else {
  40523. /* set devId to invalid, so software is used */
  40524. info->cipher.des3.des->devId = INVALID_DEVID;
  40525. ret = wc_Des3_CbcDecrypt(
  40526. info->cipher.des3.des,
  40527. info->cipher.des3.out,
  40528. info->cipher.des3.in,
  40529. info->cipher.des3.sz);
  40530. /* reset devId */
  40531. info->cipher.des3.des->devId = devIdArg;
  40532. }
  40533. }
  40534. #endif /* !NO_DES3 */
  40535. #endif /* !NO_AES || !NO_DES3 */
  40536. }
  40537. #if !defined(NO_SHA) || !defined(NO_SHA256) || \
  40538. defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)
  40539. else if (info->algo_type == WC_ALGO_TYPE_HASH) {
  40540. #if !defined(NO_SHA)
  40541. if (info->hash.type == WC_HASH_TYPE_SHA) {
  40542. if (info->hash.sha1 == NULL)
  40543. return NOT_COMPILED_IN;
  40544. /* set devId to invalid, so software is used */
  40545. info->hash.sha1->devId = INVALID_DEVID;
  40546. if (info->hash.in != NULL) {
  40547. ret = wc_ShaUpdate(
  40548. info->hash.sha1,
  40549. info->hash.in,
  40550. info->hash.inSz);
  40551. }
  40552. if (info->hash.digest != NULL) {
  40553. ret = wc_ShaFinal(
  40554. info->hash.sha1,
  40555. info->hash.digest);
  40556. }
  40557. /* reset devId */
  40558. info->hash.sha1->devId = devIdArg;
  40559. }
  40560. else
  40561. #endif
  40562. #if !defined(NO_SHA256)
  40563. if (info->hash.type == WC_HASH_TYPE_SHA256) {
  40564. if (info->hash.sha256 == NULL)
  40565. return NOT_COMPILED_IN;
  40566. /* set devId to invalid, so software is used */
  40567. info->hash.sha256->devId = INVALID_DEVID;
  40568. if (info->hash.in != NULL) {
  40569. ret = wc_Sha256Update(
  40570. info->hash.sha256,
  40571. info->hash.in,
  40572. info->hash.inSz);
  40573. }
  40574. if (info->hash.digest != NULL) {
  40575. ret = wc_Sha256Final(
  40576. info->hash.sha256,
  40577. info->hash.digest);
  40578. }
  40579. /* reset devId */
  40580. info->hash.sha256->devId = devIdArg;
  40581. }
  40582. else
  40583. #endif
  40584. #ifdef WOLFSSL_SHA384
  40585. if (info->hash.type == WC_HASH_TYPE_SHA384) {
  40586. if (info->hash.sha384 == NULL)
  40587. return NOT_COMPILED_IN;
  40588. #ifndef NO_SHA2_CRYPTO_CB
  40589. /* set devId to invalid, so software is used */
  40590. info->hash.sha384->devId = INVALID_DEVID;
  40591. #endif
  40592. if (info->hash.in != NULL) {
  40593. ret = wc_Sha384Update(
  40594. info->hash.sha384,
  40595. info->hash.in,
  40596. info->hash.inSz);
  40597. }
  40598. if (info->hash.digest != NULL) {
  40599. ret = wc_Sha384Final(
  40600. info->hash.sha384,
  40601. info->hash.digest);
  40602. }
  40603. #ifndef NO_SHA2_CRYPTO_CB
  40604. /* reset devId */
  40605. info->hash.sha384->devId = devIdArg;
  40606. #endif
  40607. }
  40608. else
  40609. #endif
  40610. #ifdef WOLFSSL_SHA512
  40611. if (info->hash.type == WC_HASH_TYPE_SHA512) {
  40612. if (info->hash.sha512 == NULL)
  40613. return NOT_COMPILED_IN;
  40614. #ifndef NO_SHA2_CRYPTO_CB
  40615. /* set devId to invalid, so software is used */
  40616. info->hash.sha512->devId = INVALID_DEVID;
  40617. #endif
  40618. if (info->hash.in != NULL) {
  40619. ret = wc_Sha512Update(
  40620. info->hash.sha512,
  40621. info->hash.in,
  40622. info->hash.inSz);
  40623. }
  40624. if (info->hash.digest != NULL) {
  40625. ret = wc_Sha512Final(
  40626. info->hash.sha512,
  40627. info->hash.digest);
  40628. }
  40629. #ifndef NO_SHA2_CRYPTO_CB
  40630. /* reset devId */
  40631. info->hash.sha512->devId = devIdArg;
  40632. #endif
  40633. }
  40634. else
  40635. #endif
  40636. {
  40637. }
  40638. }
  40639. #endif /* !NO_SHA || !NO_SHA256 */
  40640. #ifndef NO_HMAC
  40641. else if (info->algo_type == WC_ALGO_TYPE_HMAC) {
  40642. if (info->hmac.hmac == NULL)
  40643. return NOT_COMPILED_IN;
  40644. /* set devId to invalid, so software is used */
  40645. info->hmac.hmac->devId = INVALID_DEVID;
  40646. if (info->hash.in != NULL) {
  40647. ret = wc_HmacUpdate(
  40648. info->hmac.hmac,
  40649. info->hmac.in,
  40650. info->hmac.inSz);
  40651. }
  40652. else if (info->hash.digest != NULL) {
  40653. ret = wc_HmacFinal(
  40654. info->hmac.hmac,
  40655. info->hmac.digest);
  40656. }
  40657. /* reset devId */
  40658. info->hmac.hmac->devId = devIdArg;
  40659. }
  40660. #endif
  40661. (void)devIdArg;
  40662. (void)myCtx;
  40663. return ret;
  40664. }
  40665. WOLFSSL_TEST_SUBROUTINE int cryptocb_test(void)
  40666. {
  40667. int ret = 0;
  40668. myCryptoDevCtx myCtx;
  40669. /* example data for callback */
  40670. myCtx.exampleVar = 1;
  40671. /* set devId to something other than INVALID_DEVID */
  40672. devId = 1;
  40673. ret = wc_CryptoCb_RegisterDevice(devId, myCryptoDevCb, &myCtx);
  40674. #ifndef WC_NO_RNG
  40675. if (ret == 0)
  40676. ret = random_test();
  40677. #endif /* WC_NO_RNG */
  40678. #if !defined(NO_RSA)
  40679. PRIVATE_KEY_UNLOCK();
  40680. if (ret == 0)
  40681. ret = rsa_test();
  40682. PRIVATE_KEY_LOCK();
  40683. #endif
  40684. #if defined(WOLF_CRYPTO_CB_ONLY_RSA)
  40685. PRIVATE_KEY_UNLOCK();
  40686. if (ret == 0)
  40687. ret = rsa_onlycb_test(&myCtx);
  40688. PRIVATE_KEY_LOCK();
  40689. #endif
  40690. #if defined(HAVE_ECC)
  40691. PRIVATE_KEY_UNLOCK();
  40692. if (ret == 0)
  40693. ret = ecc_test();
  40694. PRIVATE_KEY_LOCK();
  40695. #endif
  40696. #if defined(WOLF_CRYPTO_CB_ONLY_ECC)
  40697. PRIVATE_KEY_UNLOCK();
  40698. if (ret == 0)
  40699. ret = ecc_onlycb_test(&myCtx);
  40700. PRIVATE_KEY_LOCK();
  40701. #endif
  40702. #ifdef HAVE_ED25519
  40703. if (ret == 0)
  40704. ret = ed25519_test();
  40705. #endif
  40706. #ifdef HAVE_CURVE25519
  40707. if (ret == 0)
  40708. ret = curve25519_test();
  40709. #endif
  40710. #ifndef NO_AES
  40711. #ifdef HAVE_AESGCM
  40712. if (ret == 0)
  40713. ret = aesgcm_test();
  40714. #endif
  40715. #ifdef HAVE_AES_CBC
  40716. if (ret == 0)
  40717. ret = aes_test();
  40718. #endif
  40719. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  40720. if (ret == 0)
  40721. ret = aesccm_test();
  40722. #endif
  40723. #endif /* !NO_AES */
  40724. #ifndef NO_DES3
  40725. if (ret == 0)
  40726. ret = des3_test();
  40727. #endif /* !NO_DES3 */
  40728. #ifndef NO_SHA
  40729. if (ret == 0)
  40730. ret = sha_test();
  40731. #endif
  40732. #ifndef NO_SHA256
  40733. if (ret == 0)
  40734. ret = sha256_test();
  40735. #endif
  40736. #ifdef WOLFSSL_SHA384
  40737. if (ret == 0)
  40738. ret = sha384_test();
  40739. #endif
  40740. #ifdef WOLFSSL_SHA512
  40741. if (ret == 0)
  40742. ret = sha512_test();
  40743. #endif
  40744. #ifndef NO_HMAC
  40745. #ifndef NO_SHA
  40746. if (ret == 0)
  40747. ret = hmac_sha_test();
  40748. #endif
  40749. #ifndef NO_SHA256
  40750. if (ret == 0)
  40751. ret = hmac_sha256_test();
  40752. #endif
  40753. #endif
  40754. #ifndef NO_PWDBASED
  40755. #if defined(HAVE_PBKDF2) && !defined(NO_SHA256)
  40756. if (ret == 0)
  40757. ret = pbkdf2_test();
  40758. #endif
  40759. #endif
  40760. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  40761. if (ret == 0)
  40762. ret = cmac_test();
  40763. #endif
  40764. /* reset devId */
  40765. devId = INVALID_DEVID;
  40766. return ret;
  40767. }
  40768. #endif /* WOLF_CRYPTO_CB */
  40769. #ifdef WOLFSSL_CERT_PIV
  40770. WOLFSSL_TEST_SUBROUTINE int certpiv_test(void)
  40771. {
  40772. int ret;
  40773. wc_CertPIV piv;
  40774. /* Template for Identiv PIV cert, nonce and signature */
  40775. WOLFSSL_SMALL_STACK_STATIC const byte pivCertIdentiv[] = {
  40776. 0x0A, 0x0B,
  40777. 0x53, 0x09, /* NIST PIV Cert */
  40778. 0x70, 0x02, /* Certificate */
  40779. 0x30, 0x00,
  40780. 0x71, 0x01, 0x05, /* Cert Info */
  40781. 0xFE, 0x00, /* Error Detection */
  40782. 0x0B, 0x01, 0x00, /* Nonce */
  40783. 0x0C, 0x01, 0x00, /* Signed Nonce */
  40784. };
  40785. /* PIV certificate data including certificate, info and error dectection. */
  40786. WOLFSSL_SMALL_STACK_STATIC const byte pivCert[] = {
  40787. 0x53, 0x09, /* NIST PIV Cert */
  40788. 0x70, 0x02, /* Certificate */
  40789. 0x30, 0x00,
  40790. 0x71, 0x01, 0x04, /* Cert Info */
  40791. 0xFE, 0x00, /* Error Detection */
  40792. };
  40793. XMEMSET(&piv, 0, sizeof(piv));
  40794. /* Test with Identiv 0x0A, 0x0B and 0x0C markers */
  40795. ret = wc_ParseCertPIV(&piv, pivCertIdentiv, sizeof(pivCertIdentiv));
  40796. if (ret != 0) {
  40797. return -14000;
  40798. }
  40799. if (!piv.isIdentiv) {
  40800. return -14001;
  40801. }
  40802. if ((piv.cert == NULL) || (piv.certSz != 2)) {
  40803. return -14002;
  40804. }
  40805. if ((piv.certErrDet == NULL) || (piv.certErrDetSz != 0)) {
  40806. return -14003;
  40807. }
  40808. if ((piv.compression != ASN_PIV_CERT_INFO_GZIP)) {
  40809. return -14004;
  40810. }
  40811. if (!piv.isX509) {
  40812. return -14005;
  40813. }
  40814. if ((piv.nonce == NULL) || (piv.nonceSz != 1)) {
  40815. return -14006;
  40816. }
  40817. if ((piv.signedNonce == NULL) || (piv.signedNonceSz != 1)) {
  40818. return -14007;
  40819. }
  40820. XMEMSET(&piv, 0, sizeof(piv));
  40821. /* Test with NIST PIV format */
  40822. ret = wc_ParseCertPIV(&piv, pivCert, sizeof(pivCert));
  40823. if (ret != 0) {
  40824. return -14010;
  40825. }
  40826. if (piv.isIdentiv) {
  40827. return -14011;
  40828. }
  40829. if ((piv.cert == NULL) || (piv.certSz != 2)) {
  40830. return -14012;
  40831. }
  40832. if ((piv.certErrDet == NULL) || (piv.certErrDetSz != 0)) {
  40833. return -14013;
  40834. }
  40835. if ((piv.compression != 0)) {
  40836. return -14014;
  40837. }
  40838. if (!piv.isX509) {
  40839. return -14015;
  40840. }
  40841. return ret;
  40842. }
  40843. #endif /* WOLFSSL_CERT_PIV */
  40844. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  40845. static time_t time_cb(time_t* t)
  40846. {
  40847. if (t != NULL) {
  40848. *t = 99;
  40849. }
  40850. return 99;
  40851. }
  40852. WOLFSSL_TEST_SUBROUTINE int time_test(void)
  40853. {
  40854. time_t t;
  40855. if (wc_SetTimeCb(time_cb) != 0)
  40856. return -15000;
  40857. t = wc_Time(NULL);
  40858. if (t != 99)
  40859. return -15001;
  40860. if (wc_GetTime(&t, sizeof(time_t)) != 0)
  40861. return -15002;
  40862. if (t != 99)
  40863. return -15003;
  40864. if (wc_SetTimeCb(NULL) != 0)
  40865. return -15004;
  40866. return 0;
  40867. }
  40868. #endif
  40869. #ifdef WOLFSSL_AES_SIV
  40870. typedef struct {
  40871. const byte key[33];
  40872. word32 keySz;
  40873. const byte nonce[49];
  40874. word32 nonceSz;
  40875. const byte assoc[81];
  40876. word32 assocSz;
  40877. const byte plaintext[83];
  40878. word32 plaintextSz;
  40879. const byte siv[AES_BLOCK_SIZE+1];
  40880. const byte ciphertext[82];
  40881. word32 ciphertextSz;
  40882. } AesSivTestVector;
  40883. #define AES_SIV_TEST_VECTORS 7
  40884. WOLFSSL_TEST_SUBROUTINE int aes_siv_test(void)
  40885. {
  40886. /* These test vectors come from chrony 4.1's SIV unit tests. */
  40887. WOLFSSL_SMALL_STACK_STATIC const AesSivTestVector testVectors[AES_SIV_TEST_VECTORS] = {
  40888. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  40889. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  40890. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  40891. "", 0,
  40892. "", 0,
  40893. "\x22\x3e\xb5\x94\xe0\xe0\x25\x4b\x00\x25\x8e\x21\x9a\x1c\xa4\x21",
  40894. "", 0
  40895. },
  40896. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  40897. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  40898. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  40899. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  40900. "", 0,
  40901. "\xd7\x20\x19\x89\xc6\xdb\xc6\xd6\x61\xfc\x62\xbc\x86\x5e\xee\xef",
  40902. "", 0
  40903. },
  40904. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  40905. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  40906. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  40907. "", 0,
  40908. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  40909. "\xb6\xc1\x60\xe9\xc2\xfd\x2a\xe8\xde\xc5\x36\x8b\x2a\x33\xed\xe1",
  40910. "\x14\xff\xb3\x97\x34\x5c\xcb\xe4\x4a\xa4\xde\xac\xd9\x36\x90\x46", 16
  40911. },
  40912. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  40913. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  40914. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e", 15,
  40915. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c", 15,
  40916. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4", 15,
  40917. "\x03\x8c\x41\x51\xba\x7a\x8f\x77\x6e\x56\x31\x99\x42\x0b\xc7\x03",
  40918. "\xe7\x6c\x67\xc9\xda\xb7\x0d\x5b\x44\x06\x26\x5a\xd0\xd2\x3b", 15
  40919. },
  40920. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  40921. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  40922. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f", 16,
  40923. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b", 16,
  40924. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4\xa7", 16,
  40925. "\x5c\x05\x23\x65\xf4\x57\x0a\xa0\xfb\x38\x3e\xce\x9b\x75\x85\xeb",
  40926. "\x68\x85\x19\x36\x0c\x7c\x48\x11\x40\xcb\x9b\x57\x9a\x0e\x65\x32", 16
  40927. },
  40928. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  40929. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  40930. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
  40931. "\xd5", 17,
  40932. "\x4c\x9d\x4f\xca\xed\x8a\xe2\xba\xad\x3f\x3e\xa6\xe9\x3c\x8c\x8b"
  40933. "\xa0", 17,
  40934. "\xba\x99\x79\x31\x23\x7e\x3c\x53\x58\x7e\xd4\x93\x02\xab\xe4\xa7"
  40935. "\x08", 17,
  40936. "\xaf\x58\x4b\xe7\x82\x1e\x96\x19\x29\x91\x25\xe0\xdd\x80\x3b\x49",
  40937. "\xa5\x11\xcd\xb6\x08\xf3\x76\xa0\xb6\xfa\x15\x82\xf3\x95\xe1\xeb"
  40938. "\xbd", 17
  40939. },
  40940. { "\x01\x23\x45\x67\x89\xab\xcd\xef\xf0\x12\x34\x56\x78\x9a\xbc\xde"
  40941. "\xef\x01\x23\x45\x67\x89\xab\xcd\xde\xf0\x12\x34\x56\x78\x9a\xbc", 32,
  40942. "\xb0\x5a\x1b\xc7\x56\xe7\xb6\x2c\xb4\x85\xe5\x56\xa5\x28\xc0\x6c"
  40943. "\x2f\x3b\x0b\x9d\x1a\x0c\xdf\x69\x47\xe0\xcc\xc0\x87\xaa\x5c\x09"
  40944. "\x98\x48\x8d\x6a\x8e\x1e\x05\xd7\x8b\x68\x74\x83\xb5\x1d\xf1\x2c", 48,
  40945. "\xe5\x8b\xd2\x6a\x30\xc5\xc5\x61\xcc\xbd\x7c\x27\xbf\xfe\xf9\x06"
  40946. "\x00\x5b\xd7\xfc\x11\x0b\xcf\x16\x61\xef\xac\x05\xa7\xaf\xec\x27"
  40947. "\x41\xc8\x5e\x9e\x0d\xf9\x2f\xaf\x20\x79\x17\xe5\x17\x91\x2a\x27"
  40948. "\x34\x1c\xbc\xaf\xeb\xef\x7f\x52\xe7\x1e\x4c\x2a\xca\xbd\x2b\xbe"
  40949. "\x34\xd6\xfb\x69\xd3\x3e\x49\x59\x60\xb4\x26\xc9\xb8\xce\xba", 79,
  40950. "\x6c\xe7\xcf\x7e\xab\x7b\xa0\xe1\xa7\x22\xcb\x88\xde\x5e\x42\xd2"
  40951. "\xec\x79\xe0\xa2\xcf\x5f\x0f\x6f\x6b\x89\x57\xcd\xae\x17\xd4\xc2"
  40952. "\xf3\x1b\xa2\xa8\x13\x78\x23\x2f\x83\xa8\xd4\x0c\xc0\xd2\xf3\x99"
  40953. "\xae\x81\xa1\xca\x5b\x5f\x45\xa6\x6f\x0c\x8a\xf3\xd4\x67\x40\x81"
  40954. "\x26\xe2\x01\x86\xe8\x5a\xd5\xf8\x58\x80\x9f\x56\xaa\x76\x96\xbf"
  40955. "\x31", 81,
  40956. "\x9a\x06\x33\xe0\xee\x00\x6a\x9b\xc8\x20\xd5\xe2\xc2\xed\xb5\x75",
  40957. "\xfa\x9e\x42\x2a\x31\x6b\xda\xca\xaa\x7d\x31\x8b\x84\x7a\xb8\xd7"
  40958. "\x8a\x81\x25\x64\xed\x41\x9b\xa9\x77\x10\xbd\x05\x0c\x4e\xc5\x31"
  40959. "\x0c\xa2\x86\xec\x8a\x94\xc8\x24\x23\x3c\x13\xee\xa5\x51\xc9\xdf"
  40960. "\x48\xc9\x55\xc5\x2f\x40\x73\x3f\x98\xbb\x8d\x69\x78\x46\x64\x17"
  40961. "\x8d\x49\x2f\x14\x62\xa4\x7c\x2a\x57\x38\x87\xce\xc6\x72\xd3\x5c"
  40962. "\xa1", 81
  40963. }};
  40964. int i;
  40965. byte computedCiphertext[82];
  40966. byte computedPlaintext[82];
  40967. byte siv[AES_BLOCK_SIZE];
  40968. int rc = 0;
  40969. for (i = 0; i < AES_SIV_TEST_VECTORS; ++i) {
  40970. rc = wc_AesSivEncrypt(testVectors[i].key, testVectors[i].keySz,
  40971. testVectors[i].assoc, testVectors[i].assocSz,
  40972. testVectors[i].nonce, testVectors[i].nonceSz,
  40973. testVectors[i].plaintext,
  40974. testVectors[i].plaintextSz, siv,
  40975. computedCiphertext);
  40976. if (rc != 0) {
  40977. return -16000;
  40978. }
  40979. rc = XMEMCMP(siv, testVectors[i].siv, AES_BLOCK_SIZE);
  40980. if (rc != 0) {
  40981. return -16001;
  40982. }
  40983. rc = XMEMCMP(computedCiphertext, testVectors[i].ciphertext,
  40984. testVectors[i].ciphertextSz);
  40985. if (rc != 0) {
  40986. return -16002;
  40987. }
  40988. rc = wc_AesSivDecrypt(testVectors[i].key, testVectors[i].keySz,
  40989. testVectors[i].assoc, testVectors[i].assocSz,
  40990. testVectors[i].nonce, testVectors[i].nonceSz,
  40991. computedCiphertext, testVectors[i].plaintextSz,
  40992. siv, computedPlaintext);
  40993. if (rc != 0) {
  40994. return -16003;
  40995. }
  40996. rc = XMEMCMP(computedPlaintext, testVectors[i].plaintext,
  40997. testVectors[i].plaintextSz);
  40998. if (rc != 0) {
  40999. return -16004;
  41000. }
  41001. }
  41002. return 0;
  41003. }
  41004. #endif
  41005. #undef ERROR_OUT
  41006. #else
  41007. #ifndef NO_MAIN_DRIVER
  41008. int main() { return 0; }
  41009. #endif
  41010. #endif /* NO_CRYPT_TEST */